blob: b00c8d484aa988916bbc919a50b737fb8892cbe0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott4ce31212005-11-02 14:59:41 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018
19#include <linux/capability.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs.h"
22#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_trans.h"
27#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dir2.h"
30#include "xfs_alloc.h"
31#include "xfs_dmapi.h"
32#include "xfs_quota.h"
33#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_dinode.h"
40#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_ialloc.h"
42#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_bmap.h"
Nathan Scotta844f452005-11-02 14:38:42 +110044#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_rtalloc.h"
46#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_rw.h"
48#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_attr.h"
50#include "xfs_buf_item.h"
51#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "xfs_qm.h"
53
54#ifdef DEBUG
55# define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
56#else
57# define qdprintk(s, args...) do { } while (0)
58#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
61STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
62 uint);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063STATIC uint xfs_qm_export_flags(uint);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064STATIC uint xfs_qm_export_qtype_flags(uint);
65STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
66 fs_disk_quota_t *);
67
68
69/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * Turn off quota accounting and/or enforcement for all udquots and/or
71 * gdquots. Called only at unmount time.
72 *
73 * This assumes that there are no dquots of this file system cached
74 * incore, and modifies the ondisk dquot directly. Therefore, for example,
75 * it is an error to call this twice, without purging the cache.
76 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +010077int
Linus Torvalds1da177e2005-04-16 15:20:36 -070078xfs_qm_scall_quotaoff(
79 xfs_mount_t *mp,
Christoph Hellwigfcafb712009-02-09 08:47:34 +010080 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 uint dqtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int error;
84 uint inactivate_flags;
85 xfs_qoff_logitem_t *qoffstart;
86 int nculprits;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /*
89 * No file system can have quotas enabled on disk but not in core.
90 * Note that quota utilities (like quotaoff) _expect_
91 * errno == EEXIST here.
92 */
93 if ((mp->m_qflags & flags) == 0)
94 return XFS_ERROR(EEXIST);
95 error = 0;
96
97 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
98
99 /*
100 * We don't want to deal with two quotaoffs messing up each other,
101 * so we're going to serialize it. quotaoff isn't exactly a performance
102 * critical thing.
103 * If quotaoff, then we must be dealing with the root filesystem.
104 */
105 ASSERT(mp->m_quotainfo);
106 if (mp->m_quotainfo)
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800107 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 ASSERT(mp->m_quotainfo);
110
111 /*
112 * If we're just turning off quota enforcement, change mp and go.
113 */
114 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
115 mp->m_qflags &= ~(flags);
116
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000117 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 mp->m_sb.sb_qflags = mp->m_qflags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000119 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
121
122 /* XXX what to do if error ? Revert back to old vals incore ? */
123 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
124 return (error);
125 }
126
127 dqtype = 0;
128 inactivate_flags = 0;
129 /*
130 * If accounting is off, we must turn enforcement off, clear the
131 * quota 'CHKD' certificate to make it known that we have to
132 * do a quotacheck the next time this quota is turned on.
133 */
134 if (flags & XFS_UQUOTA_ACCT) {
135 dqtype |= XFS_QMOPT_UQUOTA;
136 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
137 inactivate_flags |= XFS_UQUOTA_ACTIVE;
138 }
139 if (flags & XFS_GQUOTA_ACCT) {
140 dqtype |= XFS_QMOPT_GQUOTA;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000141 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 inactivate_flags |= XFS_GQUOTA_ACTIVE;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000143 } else if (flags & XFS_PQUOTA_ACCT) {
144 dqtype |= XFS_QMOPT_PQUOTA;
145 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
146 inactivate_flags |= XFS_PQUOTA_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
149 /*
150 * Nothing to do? Don't complain. This happens when we're just
151 * turning off quota enforcement.
152 */
153 if ((mp->m_qflags & flags) == 0) {
154 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
155 return (0);
156 }
157
158 /*
159 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
David Chinnercb6edc22008-04-10 12:20:45 +1000160 * and synchronously. If we fail to write, we should abort the
161 * operation as it cannot be recovered safely if we crash.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
David Chinnercb6edc22008-04-10 12:20:45 +1000163 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
164 if (error)
165 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /*
168 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
169 * to take care of the race between dqget and quotaoff. We don't take
170 * any special locks to reset these bits. All processes need to check
171 * these bits *after* taking inode lock(s) to see if the particular
172 * quota type is in the process of being turned off. If *ACTIVE, it is
173 * guaranteed that all dquot structures and all quotainode ptrs will all
174 * stay valid as long as that inode is kept locked.
175 *
176 * There is no turning back after this.
177 */
178 mp->m_qflags &= ~inactivate_flags;
179
180 /*
181 * Give back all the dquot reference(s) held by inodes.
182 * Here we go thru every single incore inode in this file system, and
183 * do a dqrele on the i_udquot/i_gdquot that it may have.
184 * Essentially, as long as somebody has an inode locked, this guarantees
185 * that quotas will not be turned off. This is handy because in a
186 * transaction once we lock the inode(s) and check for quotaon, we can
187 * depend on the quota inodes (and other things) being valid as long as
188 * we keep the lock(s).
189 */
190 xfs_qm_dqrele_all_inodes(mp, flags);
191
192 /*
193 * Next we make the changes in the quota flag in the mount struct.
194 * This isn't protected by a particular lock directly, because we
195 * don't want to take a mrlock everytime we depend on quotas being on.
196 */
197 mp->m_qflags &= ~(flags);
198
199 /*
200 * Go through all the dquots of this file system and purge them,
201 * according to what was turned off. We may not be able to get rid
202 * of all dquots, because dquots can have temporary references that
203 * are not attached to inodes. eg. xfs_setattr, xfs_create.
204 * So, if we couldn't purge all the dquots from the filesystem,
205 * we can't get rid of the incore data structures.
206 */
207 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
208 delay(10 * nculprits);
209
210 /*
211 * Transactions that had started before ACTIVE state bit was cleared
212 * could have logged many dquots, so they'd have higher LSNs than
213 * the first QUOTAOFF log record does. If we happen to crash when
214 * the tail of the log has gone past the QUOTAOFF record, but
215 * before the last dquot modification, those dquots __will__
216 * recover, and that's not good.
217 *
218 * So, we have QUOTAOFF start and end logitems; the start
219 * logitem won't get overwritten until the end logitem appears...
220 */
David Chinnercb6edc22008-04-10 12:20:45 +1000221 error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
222 if (error) {
223 /* We're screwed now. Shutdown is the only option. */
224 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
225 goto out_error;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /*
229 * If quotas is completely disabled, close shop.
230 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000231 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
232 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
234 xfs_qm_destroy_quotainfo(mp);
235 return (0);
236 }
237
238 /*
239 * Release our quotainode references, and vn_purge them,
240 * if we don't need them anymore.
241 */
242 if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000243 IRELE(XFS_QI_UQIP(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 XFS_QI_UQIP(mp) = NULL;
245 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000246 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
Christoph Hellwig26cc0022008-07-18 17:12:43 +1000247 IRELE(XFS_QI_GQIP(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 XFS_QI_GQIP(mp) = NULL;
249 }
David Chinnercb6edc22008-04-10 12:20:45 +1000250out_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
252
253 return (error);
254}
255
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100256int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257xfs_qm_scall_trunc_qfiles(
258 xfs_mount_t *mp,
259 uint flags)
260{
David Chinner88ab0202008-04-10 12:20:51 +1000261 int error = 0, error2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 xfs_inode_t *qip;
263
Eric Sandeen62118702008-03-06 13:44:28 +1100264 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
266 return XFS_ERROR(EINVAL);
267 }
268
269 if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
270 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
David Chinner88ab0202008-04-10 12:20:51 +1000271 if (!error) {
272 error = xfs_truncate_file(mp, qip);
Christoph Hellwig43355092008-03-27 18:01:08 +1100273 IRELE(qip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 }
276
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000277 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
278 mp->m_sb.sb_gquotino != NULLFSINO) {
David Chinner88ab0202008-04-10 12:20:51 +1000279 error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
280 if (!error2) {
281 error2 = xfs_truncate_file(mp, qip);
Christoph Hellwig43355092008-03-27 18:01:08 +1100282 IRELE(qip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 }
285
David Chinner88ab0202008-04-10 12:20:51 +1000286 return error ? error : error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289
290/*
291 * Switch on (a given) quota enforcement for a filesystem. This takes
292 * effect immediately.
293 * (Switching on quota accounting must be done at mount time.)
294 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100295int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296xfs_qm_scall_quotaon(
297 xfs_mount_t *mp,
298 uint flags)
299{
300 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 uint qf;
302 uint accflags;
303 __int64_t sbflags;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
306 /*
307 * Switching on quota accounting must be done at mount time.
308 */
309 accflags = flags & XFS_ALL_QUOTA_ACCT;
310 flags &= ~(XFS_ALL_QUOTA_ACCT);
311
312 sbflags = 0;
313
314 if (flags == 0) {
315 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
316 return XFS_ERROR(EINVAL);
317 }
318
319 /* No fs can turn on quotas with a delayed effect */
320 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
321
322 /*
323 * Can't enforce without accounting. We check the superblock
324 * qflags here instead of m_qflags because rootfs can have
325 * quota acct on ondisk without m_qflags' knowing.
326 */
327 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
328 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
329 (flags & XFS_UQUOTA_ENFD))
330 ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000331 ((flags & XFS_PQUOTA_ACCT) == 0 &&
332 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
Donald Douwsma424ea912007-05-08 13:49:15 +1000333 (flags & XFS_GQUOTA_ACCT) == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000335 (flags & XFS_OQUOTA_ENFD))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
337 flags, mp->m_sb.sb_qflags);
338 return XFS_ERROR(EINVAL);
339 }
340 /*
341 * If everything's upto-date incore, then don't waste time.
342 */
343 if ((mp->m_qflags & flags) == flags)
344 return XFS_ERROR(EEXIST);
345
346 /*
347 * Change sb_qflags on disk but not incore mp->qflags
348 * if this is the root filesystem.
349 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000350 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 qf = mp->m_sb.sb_qflags;
352 mp->m_sb.sb_qflags = qf | flags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000353 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /*
356 * There's nothing to change if it's the same.
357 */
358 if ((qf & flags) == flags && sbflags == 0)
359 return XFS_ERROR(EEXIST);
360 sbflags |= XFS_SB_QFLAGS;
361
362 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
363 return (error);
364 /*
365 * If we aren't trying to switch on quota enforcement, we are done.
366 */
367 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
368 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000369 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
370 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
371 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
372 (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 (flags & XFS_ALL_QUOTA_ENFD) == 0)
374 return (0);
375
376 if (! XFS_IS_QUOTA_RUNNING(mp))
377 return XFS_ERROR(ESRCH);
378
379 /*
380 * Switch on quota enforcement in core.
381 */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800382 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
384 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
385
386 return (0);
387}
388
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
391 * Return quota status information, such as uquota-off, enforcements, etc.
392 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100393int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394xfs_qm_scall_getqstat(
395 xfs_mount_t *mp,
396 fs_quota_stat_t *out)
397{
398 xfs_inode_t *uip, *gip;
399 boolean_t tempuqip, tempgqip;
400
401 uip = gip = NULL;
402 tempuqip = tempgqip = B_FALSE;
403 memset(out, 0, sizeof(fs_quota_stat_t));
404
405 out->qs_version = FS_QSTAT_VERSION;
Eric Sandeen62118702008-03-06 13:44:28 +1100406 if (!xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 out->qs_uquota.qfs_ino = NULLFSINO;
408 out->qs_gquota.qfs_ino = NULLFSINO;
409 return (0);
410 }
411 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
412 (XFS_ALL_QUOTA_ACCT|
413 XFS_ALL_QUOTA_ENFD));
414 out->qs_pad = 0;
415 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
416 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
417
418 if (mp->m_quotainfo) {
419 uip = mp->m_quotainfo->qi_uquotaip;
420 gip = mp->m_quotainfo->qi_gquotaip;
421 }
422 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
423 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
424 0, 0, &uip, 0) == 0)
425 tempuqip = B_TRUE;
426 }
427 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
428 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
429 0, 0, &gip, 0) == 0)
430 tempgqip = B_TRUE;
431 }
432 if (uip) {
433 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
434 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
435 if (tempuqip)
Christoph Hellwig43355092008-03-27 18:01:08 +1100436 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 if (gip) {
439 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
440 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
441 if (tempgqip)
Christoph Hellwig43355092008-03-27 18:01:08 +1100442 IRELE(gip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444 if (mp->m_quotainfo) {
445 out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
446 out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
447 out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
448 out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
449 out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
450 out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
451 }
452 return (0);
453}
454
455/*
456 * Adjust quota limits, and start/stop timers accordingly.
457 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100458int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459xfs_qm_scall_setqlim(
460 xfs_mount_t *mp,
461 xfs_dqid_t id,
462 uint type,
463 fs_disk_quota_t *newlim)
464{
465 xfs_disk_dquot_t *ddq;
466 xfs_dquot_t *dqp;
467 xfs_trans_t *tp;
468 int error;
469 xfs_qcnt_t hard, soft;
470
Nathan Scott754002b2005-06-21 15:49:06 +1000471 if ((newlim->d_fieldmask &
472 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return (0);
474
475 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
476 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
477 0, 0, XFS_DEFAULT_LOG_COUNT))) {
478 xfs_trans_cancel(tp, 0);
479 return (error);
480 }
481
482 /*
483 * We don't want to race with a quotaoff so take the quotaoff lock.
484 * (We don't hold an inode lock, so there's nothing else to stop
485 * a quotaoff from happening). (XXXThis doesn't currently happen
486 * because we take the vfslock before calling xfs_qm_sysent).
487 */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800488 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /*
491 * Get the dquot (locked), and join it to the transaction.
492 * Allocate the dquot if this doesn't exist.
493 */
494 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
495 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
496 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
497 ASSERT(error != ENOENT);
498 return (error);
499 }
500 xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
501 xfs_trans_dqjoin(tp, dqp);
502 ddq = &dqp->q_core;
503
504 /*
505 * Make sure that hardlimits are >= soft limits before changing.
506 */
507 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
508 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100509 be64_to_cpu(ddq->d_blk_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
511 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100512 be64_to_cpu(ddq->d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100514 ddq->d_blk_hardlimit = cpu_to_be64(hard);
515 ddq->d_blk_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (id == 0) {
517 mp->m_quotainfo->qi_bhardlimit = hard;
518 mp->m_quotainfo->qi_bsoftlimit = soft;
519 }
520 } else {
521 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
522 }
523 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
524 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100525 be64_to_cpu(ddq->d_rtb_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
527 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100528 be64_to_cpu(ddq->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100530 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
531 ddq->d_rtb_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (id == 0) {
533 mp->m_quotainfo->qi_rtbhardlimit = hard;
534 mp->m_quotainfo->qi_rtbsoftlimit = soft;
535 }
536 } else {
537 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
538 }
539
540 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
541 (xfs_qcnt_t) newlim->d_ino_hardlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100542 be64_to_cpu(ddq->d_ino_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
544 (xfs_qcnt_t) newlim->d_ino_softlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100545 be64_to_cpu(ddq->d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100547 ddq->d_ino_hardlimit = cpu_to_be64(hard);
548 ddq->d_ino_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (id == 0) {
550 mp->m_quotainfo->qi_ihardlimit = hard;
551 mp->m_quotainfo->qi_isoftlimit = soft;
552 }
553 } else {
554 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
555 }
556
Nathan Scott754002b2005-06-21 15:49:06 +1000557 /*
558 * Update warnings counter(s) if requested
559 */
560 if (newlim->d_fieldmask & FS_DQ_BWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100561 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000562 if (newlim->d_fieldmask & FS_DQ_IWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100563 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000564 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100565 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (id == 0) {
568 /*
569 * Timelimits for the super user set the relative time
570 * the other users can be over quota for this file system.
571 * If it is zero a default is used. Ditto for the default
Nathan Scott754002b2005-06-21 15:49:06 +1000572 * soft and hard limit values (already done, above), and
573 * for warnings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
575 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
576 mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100577 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
580 mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100581 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
584 mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100585 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Nathan Scott754002b2005-06-21 15:49:06 +1000587 if (newlim->d_fieldmask & FS_DQ_BWARNS)
588 mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
589 if (newlim->d_fieldmask & FS_DQ_IWARNS)
590 mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
591 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
592 mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
593 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * If the user is now over quota, start the timelimit.
596 * The user will not be 'warned'.
597 * Note that we keep the timers ticking, whether enforcement
598 * is on or off. We don't really want to bother with iterating
599 * over all ondisk dquots and turning the timers on/off.
600 */
601 xfs_qm_adjust_dqtimers(mp, ddq);
602 }
603 dqp->dq_flags |= XFS_DQ_DIRTY;
604 xfs_trans_log_dquot(tp, dqp);
605
606 xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
David Chinnere5720ee2008-04-10 12:21:18 +1000607 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 xfs_qm_dqprint(dqp);
609 xfs_qm_dqrele(dqp);
610 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
611
David Chinnere5720ee2008-04-10 12:21:18 +1000612 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100615int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616xfs_qm_scall_getquota(
617 xfs_mount_t *mp,
618 xfs_dqid_t id,
619 uint type,
620 fs_disk_quota_t *out)
621{
622 xfs_dquot_t *dqp;
623 int error;
624
625 /*
626 * Try to get the dquot. We don't want it allocated on disk, so
627 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
628 * exist, we'll get ENOENT back.
629 */
630 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
631 return (error);
632 }
633
634 xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
635 /*
636 * If everything's NULL, this dquot doesn't quite exist as far as
637 * our utility programs are concerned.
638 */
639 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
640 xfs_qm_dqput(dqp);
641 return XFS_ERROR(ENOENT);
642 }
643 /* xfs_qm_dqprint(dqp); */
644 /*
645 * Convert the disk dquot to the exportable format
646 */
647 xfs_qm_export_dquot(mp, &dqp->q_core, out);
648 xfs_qm_dqput(dqp);
649 return (error ? XFS_ERROR(EFAULT) : 0);
650}
651
652
653STATIC int
654xfs_qm_log_quotaoff_end(
655 xfs_mount_t *mp,
656 xfs_qoff_logitem_t *startqoff,
657 uint flags)
658{
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000659 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000661 xfs_qoff_logitem_t *qoffi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
664
665 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
666 0, 0, XFS_DEFAULT_LOG_COUNT))) {
667 xfs_trans_cancel(tp, 0);
668 return (error);
669 }
670
671 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
672 flags & XFS_ALL_QUOTA_ACCT);
673 xfs_trans_log_quotaoff_item(tp, qoffi);
674
675 /*
676 * We have to make sure that the transaction is secure on disk before we
677 * return and actually stop quota accounting. So, make it synchronous.
678 * We don't care about quotoff's performance.
679 */
680 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000681 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return (error);
683}
684
685
686STATIC int
687xfs_qm_log_quotaoff(
688 xfs_mount_t *mp,
689 xfs_qoff_logitem_t **qoffstartp,
690 uint flags)
691{
692 xfs_trans_t *tp;
693 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 xfs_qoff_logitem_t *qoffi=NULL;
695 uint oldsbqflag=0;
696
697 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
698 if ((error = xfs_trans_reserve(tp, 0,
699 sizeof(xfs_qoff_logitem_t) * 2 +
700 mp->m_sb.sb_sectsize + 128,
701 0,
702 0,
703 XFS_DEFAULT_LOG_COUNT))) {
704 goto error0;
705 }
706
707 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
708 xfs_trans_log_quotaoff_item(tp, qoffi);
709
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000710 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 oldsbqflag = mp->m_sb.sb_qflags;
712 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000713 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 xfs_mod_sb(tp, XFS_SB_QFLAGS);
716
717 /*
718 * We have to make sure that the transaction is secure on disk before we
719 * return and actually stop quota accounting. So, make it synchronous.
720 * We don't care about quotoff's performance.
721 */
722 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000723 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725error0:
726 if (error) {
727 xfs_trans_cancel(tp, 0);
728 /*
729 * No one else is modifying sb_qflags, so this is OK.
730 * We still hold the quotaofflock.
731 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000732 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 mp->m_sb.sb_qflags = oldsbqflag;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000734 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736 *qoffstartp = qoffi;
737 return (error);
738}
739
740
741/*
742 * Translate an internal style on-disk-dquot to the exportable format.
743 * The main differences are that the counters/limits are all in Basic
744 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
745 * to be converted to the native endianness.
746 */
747STATIC void
748xfs_qm_export_dquot(
749 xfs_mount_t *mp,
750 xfs_disk_dquot_t *src,
751 struct fs_disk_quota *dst)
752{
753 memset(dst, 0, sizeof(*dst));
754 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100755 dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
756 dst->d_id = be32_to_cpu(src->d_id);
757 dst->d_blk_hardlimit =
758 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
759 dst->d_blk_softlimit =
760 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
761 dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
762 dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
763 dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
764 dst->d_icount = be64_to_cpu(src->d_icount);
765 dst->d_btimer = be32_to_cpu(src->d_btimer);
766 dst->d_itimer = be32_to_cpu(src->d_itimer);
767 dst->d_iwarns = be16_to_cpu(src->d_iwarns);
768 dst->d_bwarns = be16_to_cpu(src->d_bwarns);
769 dst->d_rtb_hardlimit =
770 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
771 dst->d_rtb_softlimit =
772 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
773 dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
774 dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
775 dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /*
778 * Internally, we don't reset all the timers when quota enforcement
Nathan Scottc41564b2006-03-29 08:55:14 +1000779 * gets turned off. No need to confuse the user level code,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * so return zeroes in that case.
781 */
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000782 if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
783 (!XFS_IS_OQUOTA_ENFORCED(mp) &&
784 (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 dst->d_btimer = 0;
786 dst->d_itimer = 0;
787 dst->d_rtbtimer = 0;
788 }
789
790#ifdef DEBUG
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000791 if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
792 (XFS_IS_OQUOTA_ENFORCED(mp) &&
793 (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
794 dst->d_id != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
796 (dst->d_blk_softlimit > 0)) {
797 ASSERT(dst->d_btimer != 0);
798 }
799 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
800 (dst->d_ino_softlimit > 0)) {
801 ASSERT(dst->d_itimer != 0);
802 }
803 }
804#endif
805}
806
807STATIC uint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808xfs_qm_export_qtype_flags(
809 uint flags)
810{
811 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000812 * Can't be more than one, or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000814 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
815 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
816 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
817 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
818 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
819 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
820 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 return (flags & XFS_DQ_USER) ?
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000823 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
824 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827STATIC uint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828xfs_qm_export_flags(
829 uint flags)
830{
831 uint uflags;
832
833 uflags = 0;
834 if (flags & XFS_UQUOTA_ACCT)
835 uflags |= XFS_QUOTA_UDQ_ACCT;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000836 if (flags & XFS_PQUOTA_ACCT)
837 uflags |= XFS_QUOTA_PDQ_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (flags & XFS_GQUOTA_ACCT)
839 uflags |= XFS_QUOTA_GDQ_ACCT;
840 if (flags & XFS_UQUOTA_ENFD)
841 uflags |= XFS_QUOTA_UDQ_ENFD;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000842 if (flags & (XFS_OQUOTA_ENFD)) {
843 uflags |= (flags & XFS_GQUOTA_ACCT) ?
844 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return (uflags);
847}
848
849
850/*
David Chinner5b4d89a2008-10-30 17:08:03 +1100851 * Release all the dquots on the inodes in an AG.
852 */
853STATIC void
854xfs_qm_dqrele_inodes_ag(
855 xfs_mount_t *mp,
856 int ag,
857 uint flags)
858{
859 xfs_inode_t *ip = NULL;
David Chinner5b4d89a2008-10-30 17:08:03 +1100860 xfs_perag_t *pag = &mp->m_perag[ag];
861 int first_index = 0;
862 int nr_found;
863
864 do {
David Chinner5b4d89a2008-10-30 17:08:03 +1100865 /*
866 * use a gang lookup to find the next inode in the tree
867 * as the tree is sparse and a gang lookup walks to find
868 * the number of objects requested.
869 */
870 read_lock(&pag->pag_ici_lock);
871 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
872 (void**)&ip, first_index, 1);
873
874 if (!nr_found) {
875 read_unlock(&pag->pag_ici_lock);
876 break;
877 }
878
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100879 /*
880 * Update the index for the next lookup. Catch overflows
881 * into the next AG range which can occur if we have inodes
882 * in the last block of the AG and we are currently
883 * pointing to the last inode.
884 */
David Chinner5b4d89a2008-10-30 17:08:03 +1100885 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100886 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
887 read_unlock(&pag->pag_ici_lock);
888 break;
889 }
David Chinner5b4d89a2008-10-30 17:08:03 +1100890
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100891 /* skip quota inodes */
892 if (ip == XFS_QI_UQIP(mp) || ip == XFS_QI_GQIP(mp)) {
David Chinner5b4d89a2008-10-30 17:08:03 +1100893 ASSERT(ip->i_udquot == NULL);
894 ASSERT(ip->i_gdquot == NULL);
895 read_unlock(&pag->pag_ici_lock);
896 continue;
897 }
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100898
899 /*
900 * If we can't get a reference on the inode, it must be
901 * in reclaim. Leave it for the reclaim code to flush.
902 */
903 if (!igrab(VFS_I(ip))) {
David Chinner5b4d89a2008-10-30 17:08:03 +1100904 read_unlock(&pag->pag_ici_lock);
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100905 continue;
David Chinner5b4d89a2008-10-30 17:08:03 +1100906 }
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100907 read_unlock(&pag->pag_ici_lock);
908
Dave Chinner63070912008-11-10 17:13:23 +1100909 /* avoid new inodes though we shouldn't find any here */
910 if (xfs_iflags_test(ip, XFS_INEW)) {
911 IRELE(ip);
912 continue;
913 }
914
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100915 xfs_ilock(ip, XFS_ILOCK_EXCL);
David Chinner5b4d89a2008-10-30 17:08:03 +1100916 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
917 xfs_qm_dqrele(ip->i_udquot);
918 ip->i_udquot = NULL;
919 }
920 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) &&
921 ip->i_gdquot) {
922 xfs_qm_dqrele(ip->i_gdquot);
923 ip->i_gdquot = NULL;
924 }
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100925 xfs_iput(ip, XFS_ILOCK_EXCL);
926
David Chinner5b4d89a2008-10-30 17:08:03 +1100927 } while (nr_found);
928}
929
930/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * Go thru all the inodes in the file system, releasing their dquots.
932 * Note that the mount structure gets modified to indicate that quotas are off
933 * AFTER this, in the case of quotaoff. This also gets called from
934 * xfs_rootumount.
935 */
936void
937xfs_qm_dqrele_all_inodes(
938 struct xfs_mount *mp,
939 uint flags)
940{
David Chinner5b4d89a2008-10-30 17:08:03 +1100941 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 ASSERT(mp->m_quotainfo);
David Chinner5b4d89a2008-10-30 17:08:03 +1100944 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
945 if (!mp->m_perag[i].pag_ici_init)
946 continue;
947 xfs_qm_dqrele_inodes_ag(mp, i, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951/*------------------------------------------------------------------------*/
952#ifdef DEBUG
953/*
954 * This contains all the test functions for XFS disk quotas.
955 * Currently it does a quota accounting check. ie. it walks through
956 * all inodes in the file system, calculating the dquot accounting fields,
957 * and prints out any inconsistencies.
958 */
959xfs_dqhash_t *qmtest_udqtab;
960xfs_dqhash_t *qmtest_gdqtab;
961int qmtest_hashmask;
962int qmtest_nfails;
963mutex_t qcheck_lock;
964
965#define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
966 (__psunsigned_t)(id)) & \
967 (qmtest_hashmask - 1))
968
969#define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
970 (qmtest_udqtab + \
971 DQTEST_HASHVAL(mp, id)) : \
972 (qmtest_gdqtab + \
973 DQTEST_HASHVAL(mp, id)))
974
975#define DQTEST_LIST_PRINT(l, NXT, title) \
976{ \
977 xfs_dqtest_t *dqp; int i = 0;\
978 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
979 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
980 dqp = (xfs_dqtest_t *)dqp->NXT) { \
981 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
982 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
983 dqp->d_bcount, dqp->d_icount); } \
984}
985
986typedef struct dqtest {
987 xfs_dqmarker_t q_lists;
988 xfs_dqhash_t *q_hash; /* the hashchain header */
989 xfs_mount_t *q_mount; /* filesystem this relates to */
990 xfs_dqid_t d_id; /* user id or group id */
991 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
992 xfs_qcnt_t d_icount; /* # inodes owned by the user */
993} xfs_dqtest_t;
994
995STATIC void
996xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
997{
998 xfs_dquot_t *d;
999 if (((d) = (h)->qh_next))
1000 (d)->HL_PREVP = &((dqp)->HL_NEXT);
1001 (dqp)->HL_NEXT = d;
1002 (dqp)->HL_PREVP = &((h)->qh_next);
1003 (h)->qh_next = (xfs_dquot_t *)dqp;
1004 (h)->qh_version++;
1005 (h)->qh_nelems++;
1006}
1007STATIC void
1008xfs_qm_dqtest_print(
1009 xfs_dqtest_t *d)
1010{
1011 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
1012 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
1014 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1015 d->d_bcount, (int)d->d_bcount);
1016 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1017 d->d_icount, (int)d->d_icount);
1018 cmn_err(CE_DEBUG, "---------------------------");
1019}
1020
1021STATIC void
1022xfs_qm_dqtest_failed(
1023 xfs_dqtest_t *d,
1024 xfs_dquot_t *dqp,
1025 char *reason,
1026 xfs_qcnt_t a,
1027 xfs_qcnt_t b,
1028 int error)
1029{
1030 qmtest_nfails++;
1031 if (error)
1032 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001033 d->d_id, error, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 else
1035 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001036 d->d_id, reason, (int)a, (int)b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 xfs_qm_dqtest_print(d);
1038 if (dqp)
1039 xfs_qm_dqprint(dqp);
1040}
1041
1042STATIC int
1043xfs_dqtest_cmp2(
1044 xfs_dqtest_t *d,
1045 xfs_dquot_t *dqp)
1046{
1047 int err = 0;
Christoph Hellwig1149d962005-11-02 15:01:12 +11001048 if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001050 be64_to_cpu(dqp->q_core.d_icount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 d->d_icount, 0);
1052 err++;
1053 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001054 if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001056 be64_to_cpu(dqp->q_core.d_bcount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 d->d_bcount, 0);
1058 err++;
1059 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001060 if (dqp->q_core.d_blk_softlimit &&
1061 be64_to_cpu(dqp->q_core.d_bcount) >=
1062 be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1064 cmn_err(CE_DEBUG,
1065 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1066 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1067 err++;
1068 }
1069 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001070 if (dqp->q_core.d_ino_softlimit &&
1071 be64_to_cpu(dqp->q_core.d_icount) >=
1072 be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1074 cmn_err(CE_DEBUG,
1075 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1076 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1077 err++;
1078 }
1079 }
1080#ifdef QUOTADEBUG
1081 if (!err) {
1082 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001083 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085#endif
1086 return (err);
1087}
1088
1089STATIC void
1090xfs_dqtest_cmp(
1091 xfs_dqtest_t *d)
1092{
1093 xfs_dquot_t *dqp;
1094 int error;
1095
1096 /* xfs_qm_dqtest_print(d); */
1097 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1098 &dqp))) {
1099 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1100 return;
1101 }
1102 xfs_dqtest_cmp2(d, dqp);
1103 xfs_qm_dqput(dqp);
1104}
1105
1106STATIC int
1107xfs_qm_internalqcheck_dqget(
1108 xfs_mount_t *mp,
1109 xfs_dqid_t id,
1110 uint type,
1111 xfs_dqtest_t **O_dq)
1112{
1113 xfs_dqtest_t *d;
1114 xfs_dqhash_t *h;
1115
1116 h = DQTEST_HASH(mp, id, type);
1117 for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
1118 d = (xfs_dqtest_t *) d->HL_NEXT) {
1119 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1120 if (d->d_id == id && mp == d->q_mount) {
1121 *O_dq = d;
1122 return (0);
1123 }
1124 }
1125 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1126 d->dq_flags = type;
1127 d->d_id = id;
1128 d->q_mount = mp;
1129 d->q_hash = h;
1130 xfs_qm_hashinsert(h, d);
1131 *O_dq = d;
1132 return (0);
1133}
1134
1135STATIC void
1136xfs_qm_internalqcheck_get_dquots(
1137 xfs_mount_t *mp,
1138 xfs_dqid_t uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001139 xfs_dqid_t projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xfs_dqid_t gid,
1141 xfs_dqtest_t **ud,
1142 xfs_dqtest_t **gd)
1143{
1144 if (XFS_IS_UQUOTA_ON(mp))
1145 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1146 if (XFS_IS_GQUOTA_ON(mp))
1147 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001148 else if (XFS_IS_PQUOTA_ON(mp))
1149 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152
1153STATIC void
1154xfs_qm_internalqcheck_dqadjust(
1155 xfs_inode_t *ip,
1156 xfs_dqtest_t *d)
1157{
1158 d->d_icount++;
1159 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1160}
1161
1162STATIC int
1163xfs_qm_internalqcheck_adjust(
1164 xfs_mount_t *mp, /* mount point for filesystem */
1165 xfs_ino_t ino, /* inode number to get data for */
1166 void __user *buffer, /* not used */
1167 int ubsize, /* not used */
1168 void *private_data, /* not used */
1169 xfs_daddr_t bno, /* starting block of inode cluster */
1170 int *ubused, /* not used */
1171 void *dip, /* not used */
1172 int *res) /* bulkstat result code */
1173{
1174 xfs_inode_t *ip;
1175 xfs_dqtest_t *ud, *gd;
1176 uint lock_flags;
1177 boolean_t ipreleased;
1178 int error;
1179
1180 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1181
1182 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1183 *res = BULKSTAT_RV_NOTHING;
1184 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1185 (unsigned long long) ino,
1186 (unsigned long long) mp->m_sb.sb_uquotino,
1187 (unsigned long long) mp->m_sb.sb_gquotino);
1188 return XFS_ERROR(EINVAL);
1189 }
1190 ipreleased = B_FALSE;
1191 again:
1192 lock_flags = XFS_ILOCK_SHARED;
1193 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1194 *res = BULKSTAT_RV_NOTHING;
1195 return (error);
1196 }
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /*
1199 * This inode can have blocks after eof which can get released
1200 * when we send it to inactive. Since we don't check the dquot
1201 * until the after all our calculations are done, we must get rid
1202 * of those now.
1203 */
1204 if (! ipreleased) {
1205 xfs_iput(ip, lock_flags);
1206 ipreleased = B_TRUE;
1207 goto again;
1208 }
1209 xfs_qm_internalqcheck_get_dquots(mp,
1210 (xfs_dqid_t) ip->i_d.di_uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001211 (xfs_dqid_t) ip->i_d.di_projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 (xfs_dqid_t) ip->i_d.di_gid,
1213 &ud, &gd);
1214 if (XFS_IS_UQUOTA_ON(mp)) {
1215 ASSERT(ud);
1216 xfs_qm_internalqcheck_dqadjust(ip, ud);
1217 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001218 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 ASSERT(gd);
1220 xfs_qm_internalqcheck_dqadjust(ip, gd);
1221 }
1222 xfs_iput(ip, lock_flags);
1223 *res = BULKSTAT_RV_DIDONE;
1224 return (0);
1225}
1226
1227
1228/* PRIVATE, debugging */
1229int
1230xfs_qm_internalqcheck(
1231 xfs_mount_t *mp)
1232{
1233 xfs_ino_t lastino;
1234 int done, count;
1235 int i;
1236 xfs_dqtest_t *d, *e;
1237 xfs_dqhash_t *h1;
1238 int error;
1239
1240 lastino = 0;
1241 qmtest_hashmask = 32;
1242 count = 5;
1243 done = 0;
1244 qmtest_nfails = 0;
1245
1246 if (! XFS_IS_QUOTA_ON(mp))
1247 return XFS_ERROR(ESRCH);
1248
1249 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1250 XFS_bflush(mp->m_ddev_targp);
1251 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1252 XFS_bflush(mp->m_ddev_targp);
1253
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001254 mutex_lock(&qcheck_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* There should be absolutely no quota activity while this
1256 is going on. */
1257 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1258 sizeof(xfs_dqhash_t), KM_SLEEP);
1259 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1260 sizeof(xfs_dqhash_t), KM_SLEEP);
1261 do {
1262 /*
1263 * Iterate thru all the inodes in the file system,
1264 * adjusting the corresponding dquot counters
1265 */
1266 if ((error = xfs_bulkstat(mp, &lastino, &count,
1267 xfs_qm_internalqcheck_adjust, NULL,
1268 0, NULL, BULKSTAT_FG_IGET, &done))) {
1269 break;
1270 }
1271 } while (! done);
1272 if (error) {
1273 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1274 }
1275 cmn_err(CE_DEBUG, "Checking results against system dquots");
1276 for (i = 0; i < qmtest_hashmask; i++) {
1277 h1 = &qmtest_udqtab[i];
1278 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1279 xfs_dqtest_cmp(d);
1280 e = (xfs_dqtest_t *) d->HL_NEXT;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001281 kmem_free(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d = e;
1283 }
1284 h1 = &qmtest_gdqtab[i];
1285 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1286 xfs_dqtest_cmp(d);
1287 e = (xfs_dqtest_t *) d->HL_NEXT;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001288 kmem_free(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 d = e;
1290 }
1291 }
1292
1293 if (qmtest_nfails) {
1294 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1295 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1296 } else {
1297 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1298 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001299 kmem_free(qmtest_udqtab);
1300 kmem_free(qmtest_gdqtab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 mutex_unlock(&qcheck_lock);
1302 return (qmtest_nfails);
1303}
1304
1305#endif /* DEBUG */