blob: 86a1d09f48d56bc1a4a569553362ed523fff6c0c [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir.h"
27#include "xfs_dir2.h"
28#include "xfs_alloc.h"
29#include "xfs_dmapi.h"
30#include "xfs_quota.h"
31#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir_sf.h"
36#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_dinode.h"
39#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110040#include "xfs_ialloc.h"
41#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_bmap.h"
Nathan Scotta844f452005-11-02 14:38:42 +110043#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_rtalloc.h"
45#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_rw.h"
47#include "xfs_acl.h"
48#include "xfs_cap.h"
49#include "xfs_mac.h"
50#include "xfs_attr.h"
51#include "xfs_buf_item.h"
52#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "xfs_qm.h"
54
55#ifdef DEBUG
56# define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
57#else
58# define qdprintk(s, args...) do { } while (0)
59#endif
60
61STATIC int xfs_qm_scall_trunc_qfiles(xfs_mount_t *, uint);
62STATIC int xfs_qm_scall_getquota(xfs_mount_t *, xfs_dqid_t, uint,
63 fs_disk_quota_t *);
64STATIC int xfs_qm_scall_getqstat(xfs_mount_t *, fs_quota_stat_t *);
65STATIC int xfs_qm_scall_setqlim(xfs_mount_t *, xfs_dqid_t, uint,
66 fs_disk_quota_t *);
67STATIC int xfs_qm_scall_quotaon(xfs_mount_t *, uint);
68STATIC int xfs_qm_scall_quotaoff(xfs_mount_t *, uint, boolean_t);
69STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
70STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
71 uint);
72STATIC uint xfs_qm_import_flags(uint);
73STATIC uint xfs_qm_export_flags(uint);
74STATIC uint xfs_qm_import_qtype_flags(uint);
75STATIC uint xfs_qm_export_qtype_flags(uint);
76STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
77 fs_disk_quota_t *);
78
79
80/*
81 * The main distribution switch of all XFS quotactl system calls.
82 */
83int
84xfs_qm_quotactl(
85 struct bhv_desc *bdp,
86 int cmd,
87 int id,
88 xfs_caddr_t addr)
89{
90 xfs_mount_t *mp;
91 int error;
92 struct vfs *vfsp;
93
94 vfsp = bhvtovfs(bdp);
95 mp = XFS_VFSTOM(vfsp);
96
Nathan Scottee348072005-11-02 10:32:38 +110097 ASSERT(addr != NULL || cmd == Q_XQUOTASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /*
100 * The following commands are valid even when quotaoff.
101 */
102 switch (cmd) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000103 case Q_XQUOTARM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000105 * Truncate quota files. quota must be off.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
Nathan Scottc310ab62005-11-02 10:31:41 +1100107 if (XFS_IS_QUOTA_ON(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return XFS_ERROR(EINVAL);
109 if (vfsp->vfs_flag & VFS_RDONLY)
110 return XFS_ERROR(EROFS);
111 return (xfs_qm_scall_trunc_qfiles(mp,
112 xfs_qm_import_qtype_flags(*(uint *)addr)));
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000113
114 case Q_XGETQSTAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * Get quota status information.
117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return (xfs_qm_scall_getqstat(mp, (fs_quota_stat_t *)addr));
119
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000120 case Q_XQUOTAON:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000122 * QUOTAON - enabling quota enforcement.
123 * Quota accounting must be turned on at mount time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (vfsp->vfs_flag & VFS_RDONLY)
126 return XFS_ERROR(EROFS);
127 return (xfs_qm_scall_quotaon(mp,
128 xfs_qm_import_flags(*(uint *)addr)));
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000129
130 case Q_XQUOTAOFF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (vfsp->vfs_flag & VFS_RDONLY)
132 return XFS_ERROR(EROFS);
133 break;
134
Nathan Scottee348072005-11-02 10:32:38 +1100135 case Q_XQUOTASYNC:
136 return (xfs_sync_inodes(mp, SYNC_DELWRI, 0, NULL));
137
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000138 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 break;
140 }
141
142 if (! XFS_IS_QUOTA_ON(mp))
143 return XFS_ERROR(ESRCH);
144
145 switch (cmd) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000146 case Q_XQUOTAOFF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (vfsp->vfs_flag & VFS_RDONLY)
148 return XFS_ERROR(EROFS);
149 error = xfs_qm_scall_quotaoff(mp,
150 xfs_qm_import_flags(*(uint *)addr),
151 B_FALSE);
152 break;
153
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000154 case Q_XGETQUOTA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_USER,
156 (fs_disk_quota_t *)addr);
157 break;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000158 case Q_XGETGQUOTA:
159 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
160 (fs_disk_quota_t *)addr);
161 break;
162 case Q_XGETPQUOTA:
163 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
164 (fs_disk_quota_t *)addr);
165 break;
166
167 case Q_XSETQLIM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (vfsp->vfs_flag & VFS_RDONLY)
169 return XFS_ERROR(EROFS);
170 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_USER,
171 (fs_disk_quota_t *)addr);
172 break;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000173 case Q_XSETGQLIM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (vfsp->vfs_flag & VFS_RDONLY)
175 return XFS_ERROR(EROFS);
176 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
177 (fs_disk_quota_t *)addr);
178 break;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000179 case Q_XSETPQLIM:
180 if (vfsp->vfs_flag & VFS_RDONLY)
181 return XFS_ERROR(EROFS);
182 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
183 (fs_disk_quota_t *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000186 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 error = XFS_ERROR(EINVAL);
188 break;
189 }
190
191 return (error);
192}
193
194/*
195 * Turn off quota accounting and/or enforcement for all udquots and/or
196 * gdquots. Called only at unmount time.
197 *
198 * This assumes that there are no dquots of this file system cached
199 * incore, and modifies the ondisk dquot directly. Therefore, for example,
200 * it is an error to call this twice, without purging the cache.
201 */
202STATIC int
203xfs_qm_scall_quotaoff(
204 xfs_mount_t *mp,
205 uint flags,
206 boolean_t force)
207{
208 uint dqtype;
209 unsigned long s;
210 int error;
211 uint inactivate_flags;
212 xfs_qoff_logitem_t *qoffstart;
213 int nculprits;
214
215 if (!force && !capable(CAP_SYS_ADMIN))
216 return XFS_ERROR(EPERM);
217 /*
218 * No file system can have quotas enabled on disk but not in core.
219 * Note that quota utilities (like quotaoff) _expect_
220 * errno == EEXIST here.
221 */
222 if ((mp->m_qflags & flags) == 0)
223 return XFS_ERROR(EEXIST);
224 error = 0;
225
226 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
227
228 /*
229 * We don't want to deal with two quotaoffs messing up each other,
230 * so we're going to serialize it. quotaoff isn't exactly a performance
231 * critical thing.
232 * If quotaoff, then we must be dealing with the root filesystem.
233 */
234 ASSERT(mp->m_quotainfo);
235 if (mp->m_quotainfo)
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800236 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 ASSERT(mp->m_quotainfo);
239
240 /*
241 * If we're just turning off quota enforcement, change mp and go.
242 */
243 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
244 mp->m_qflags &= ~(flags);
245
246 s = XFS_SB_LOCK(mp);
247 mp->m_sb.sb_qflags = mp->m_qflags;
248 XFS_SB_UNLOCK(mp, s);
249 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
250
251 /* XXX what to do if error ? Revert back to old vals incore ? */
252 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
253 return (error);
254 }
255
256 dqtype = 0;
257 inactivate_flags = 0;
258 /*
259 * If accounting is off, we must turn enforcement off, clear the
260 * quota 'CHKD' certificate to make it known that we have to
261 * do a quotacheck the next time this quota is turned on.
262 */
263 if (flags & XFS_UQUOTA_ACCT) {
264 dqtype |= XFS_QMOPT_UQUOTA;
265 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
266 inactivate_flags |= XFS_UQUOTA_ACTIVE;
267 }
268 if (flags & XFS_GQUOTA_ACCT) {
269 dqtype |= XFS_QMOPT_GQUOTA;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000270 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 inactivate_flags |= XFS_GQUOTA_ACTIVE;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000272 } else if (flags & XFS_PQUOTA_ACCT) {
273 dqtype |= XFS_QMOPT_PQUOTA;
274 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
275 inactivate_flags |= XFS_PQUOTA_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
278 /*
279 * Nothing to do? Don't complain. This happens when we're just
280 * turning off quota enforcement.
281 */
282 if ((mp->m_qflags & flags) == 0) {
283 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
284 return (0);
285 }
286
287 /*
288 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
289 * and synchronously.
290 */
291 xfs_qm_log_quotaoff(mp, &qoffstart, flags);
292
293 /*
294 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
295 * to take care of the race between dqget and quotaoff. We don't take
296 * any special locks to reset these bits. All processes need to check
297 * these bits *after* taking inode lock(s) to see if the particular
298 * quota type is in the process of being turned off. If *ACTIVE, it is
299 * guaranteed that all dquot structures and all quotainode ptrs will all
300 * stay valid as long as that inode is kept locked.
301 *
302 * There is no turning back after this.
303 */
304 mp->m_qflags &= ~inactivate_flags;
305
306 /*
307 * Give back all the dquot reference(s) held by inodes.
308 * Here we go thru every single incore inode in this file system, and
309 * do a dqrele on the i_udquot/i_gdquot that it may have.
310 * Essentially, as long as somebody has an inode locked, this guarantees
311 * that quotas will not be turned off. This is handy because in a
312 * transaction once we lock the inode(s) and check for quotaon, we can
313 * depend on the quota inodes (and other things) being valid as long as
314 * we keep the lock(s).
315 */
316 xfs_qm_dqrele_all_inodes(mp, flags);
317
318 /*
319 * Next we make the changes in the quota flag in the mount struct.
320 * This isn't protected by a particular lock directly, because we
321 * don't want to take a mrlock everytime we depend on quotas being on.
322 */
323 mp->m_qflags &= ~(flags);
324
325 /*
326 * Go through all the dquots of this file system and purge them,
327 * according to what was turned off. We may not be able to get rid
328 * of all dquots, because dquots can have temporary references that
329 * are not attached to inodes. eg. xfs_setattr, xfs_create.
330 * So, if we couldn't purge all the dquots from the filesystem,
331 * we can't get rid of the incore data structures.
332 */
333 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
334 delay(10 * nculprits);
335
336 /*
337 * Transactions that had started before ACTIVE state bit was cleared
338 * could have logged many dquots, so they'd have higher LSNs than
339 * the first QUOTAOFF log record does. If we happen to crash when
340 * the tail of the log has gone past the QUOTAOFF record, but
341 * before the last dquot modification, those dquots __will__
342 * recover, and that's not good.
343 *
344 * So, we have QUOTAOFF start and end logitems; the start
345 * logitem won't get overwritten until the end logitem appears...
346 */
347 xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
348
349 /*
350 * If quotas is completely disabled, close shop.
351 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000352 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
353 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
355 xfs_qm_destroy_quotainfo(mp);
356 return (0);
357 }
358
359 /*
360 * Release our quotainode references, and vn_purge them,
361 * if we don't need them anymore.
362 */
363 if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
364 XFS_PURGE_INODE(XFS_QI_UQIP(mp));
365 XFS_QI_UQIP(mp) = NULL;
366 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000367 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 XFS_PURGE_INODE(XFS_QI_GQIP(mp));
369 XFS_QI_GQIP(mp) = NULL;
370 }
371 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
372
373 return (error);
374}
375
376STATIC int
377xfs_qm_scall_trunc_qfiles(
378 xfs_mount_t *mp,
379 uint flags)
380{
381 int error;
382 xfs_inode_t *qip;
383
384 if (!capable(CAP_SYS_ADMIN))
385 return XFS_ERROR(EPERM);
386 error = 0;
387 if (!XFS_SB_VERSION_HASQUOTA(&mp->m_sb) || flags == 0) {
388 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
389 return XFS_ERROR(EINVAL);
390 }
391
392 if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
393 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
394 if (! error) {
395 (void) xfs_truncate_file(mp, qip);
396 VN_RELE(XFS_ITOV(qip));
397 }
398 }
399
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000400 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
401 mp->m_sb.sb_gquotino != NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
403 if (! error) {
404 (void) xfs_truncate_file(mp, qip);
405 VN_RELE(XFS_ITOV(qip));
406 }
407 }
408
409 return (error);
410}
411
412
413/*
414 * Switch on (a given) quota enforcement for a filesystem. This takes
415 * effect immediately.
416 * (Switching on quota accounting must be done at mount time.)
417 */
418STATIC int
419xfs_qm_scall_quotaon(
420 xfs_mount_t *mp,
421 uint flags)
422{
423 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000424 unsigned long s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 uint qf;
426 uint accflags;
427 __int64_t sbflags;
428
429 if (!capable(CAP_SYS_ADMIN))
430 return XFS_ERROR(EPERM);
431
432 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
433 /*
434 * Switching on quota accounting must be done at mount time.
435 */
436 accflags = flags & XFS_ALL_QUOTA_ACCT;
437 flags &= ~(XFS_ALL_QUOTA_ACCT);
438
439 sbflags = 0;
440
441 if (flags == 0) {
442 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
443 return XFS_ERROR(EINVAL);
444 }
445
446 /* No fs can turn on quotas with a delayed effect */
447 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
448
449 /*
450 * Can't enforce without accounting. We check the superblock
451 * qflags here instead of m_qflags because rootfs can have
452 * quota acct on ondisk without m_qflags' knowing.
453 */
454 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
455 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
456 (flags & XFS_UQUOTA_ENFD))
457 ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000458 ((flags & XFS_PQUOTA_ACCT) == 0 &&
459 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
460 (flags & XFS_OQUOTA_ENFD))
461 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 ((flags & XFS_GQUOTA_ACCT) == 0 &&
463 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000464 (flags & XFS_OQUOTA_ENFD))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
466 flags, mp->m_sb.sb_qflags);
467 return XFS_ERROR(EINVAL);
468 }
469 /*
470 * If everything's upto-date incore, then don't waste time.
471 */
472 if ((mp->m_qflags & flags) == flags)
473 return XFS_ERROR(EEXIST);
474
475 /*
476 * Change sb_qflags on disk but not incore mp->qflags
477 * if this is the root filesystem.
478 */
479 s = XFS_SB_LOCK(mp);
480 qf = mp->m_sb.sb_qflags;
481 mp->m_sb.sb_qflags = qf | flags;
482 XFS_SB_UNLOCK(mp, s);
483
484 /*
485 * There's nothing to change if it's the same.
486 */
487 if ((qf & flags) == flags && sbflags == 0)
488 return XFS_ERROR(EEXIST);
489 sbflags |= XFS_SB_QFLAGS;
490
491 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
492 return (error);
493 /*
494 * If we aren't trying to switch on quota enforcement, we are done.
495 */
496 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
497 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000498 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
499 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
500 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
501 (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 (flags & XFS_ALL_QUOTA_ENFD) == 0)
503 return (0);
504
505 if (! XFS_IS_QUOTA_RUNNING(mp))
506 return XFS_ERROR(ESRCH);
507
508 /*
509 * Switch on quota enforcement in core.
510 */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800511 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
513 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
514
515 return (0);
516}
517
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/*
520 * Return quota status information, such as uquota-off, enforcements, etc.
521 */
522STATIC int
523xfs_qm_scall_getqstat(
524 xfs_mount_t *mp,
525 fs_quota_stat_t *out)
526{
527 xfs_inode_t *uip, *gip;
528 boolean_t tempuqip, tempgqip;
529
530 uip = gip = NULL;
531 tempuqip = tempgqip = B_FALSE;
532 memset(out, 0, sizeof(fs_quota_stat_t));
533
534 out->qs_version = FS_QSTAT_VERSION;
535 if (! XFS_SB_VERSION_HASQUOTA(&mp->m_sb)) {
536 out->qs_uquota.qfs_ino = NULLFSINO;
537 out->qs_gquota.qfs_ino = NULLFSINO;
538 return (0);
539 }
540 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
541 (XFS_ALL_QUOTA_ACCT|
542 XFS_ALL_QUOTA_ENFD));
543 out->qs_pad = 0;
544 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
545 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
546
547 if (mp->m_quotainfo) {
548 uip = mp->m_quotainfo->qi_uquotaip;
549 gip = mp->m_quotainfo->qi_gquotaip;
550 }
551 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
552 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
553 0, 0, &uip, 0) == 0)
554 tempuqip = B_TRUE;
555 }
556 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
557 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
558 0, 0, &gip, 0) == 0)
559 tempgqip = B_TRUE;
560 }
561 if (uip) {
562 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
563 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
564 if (tempuqip)
565 VN_RELE(XFS_ITOV(uip));
566 }
567 if (gip) {
568 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
569 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
570 if (tempgqip)
571 VN_RELE(XFS_ITOV(gip));
572 }
573 if (mp->m_quotainfo) {
574 out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
575 out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
576 out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
577 out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
578 out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
579 out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
580 }
581 return (0);
582}
583
584/*
585 * Adjust quota limits, and start/stop timers accordingly.
586 */
587STATIC int
588xfs_qm_scall_setqlim(
589 xfs_mount_t *mp,
590 xfs_dqid_t id,
591 uint type,
592 fs_disk_quota_t *newlim)
593{
594 xfs_disk_dquot_t *ddq;
595 xfs_dquot_t *dqp;
596 xfs_trans_t *tp;
597 int error;
598 xfs_qcnt_t hard, soft;
599
600 if (!capable(CAP_SYS_ADMIN))
601 return XFS_ERROR(EPERM);
602
Nathan Scott754002b2005-06-21 15:49:06 +1000603 if ((newlim->d_fieldmask &
604 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return (0);
606
607 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
608 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
609 0, 0, XFS_DEFAULT_LOG_COUNT))) {
610 xfs_trans_cancel(tp, 0);
611 return (error);
612 }
613
614 /*
615 * We don't want to race with a quotaoff so take the quotaoff lock.
616 * (We don't hold an inode lock, so there's nothing else to stop
617 * a quotaoff from happening). (XXXThis doesn't currently happen
618 * because we take the vfslock before calling xfs_qm_sysent).
619 */
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800620 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /*
623 * Get the dquot (locked), and join it to the transaction.
624 * Allocate the dquot if this doesn't exist.
625 */
626 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
627 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
628 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
629 ASSERT(error != ENOENT);
630 return (error);
631 }
632 xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
633 xfs_trans_dqjoin(tp, dqp);
634 ddq = &dqp->q_core;
635
636 /*
637 * Make sure that hardlimits are >= soft limits before changing.
638 */
639 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
640 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100641 be64_to_cpu(ddq->d_blk_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
643 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100644 be64_to_cpu(ddq->d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100646 ddq->d_blk_hardlimit = cpu_to_be64(hard);
647 ddq->d_blk_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (id == 0) {
649 mp->m_quotainfo->qi_bhardlimit = hard;
650 mp->m_quotainfo->qi_bsoftlimit = soft;
651 }
652 } else {
653 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
654 }
655 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
656 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100657 be64_to_cpu(ddq->d_rtb_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
659 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100660 be64_to_cpu(ddq->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100662 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
663 ddq->d_rtb_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (id == 0) {
665 mp->m_quotainfo->qi_rtbhardlimit = hard;
666 mp->m_quotainfo->qi_rtbsoftlimit = soft;
667 }
668 } else {
669 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
670 }
671
672 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
673 (xfs_qcnt_t) newlim->d_ino_hardlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100674 be64_to_cpu(ddq->d_ino_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
676 (xfs_qcnt_t) newlim->d_ino_softlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100677 be64_to_cpu(ddq->d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100679 ddq->d_ino_hardlimit = cpu_to_be64(hard);
680 ddq->d_ino_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (id == 0) {
682 mp->m_quotainfo->qi_ihardlimit = hard;
683 mp->m_quotainfo->qi_isoftlimit = soft;
684 }
685 } else {
686 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
687 }
688
Nathan Scott754002b2005-06-21 15:49:06 +1000689 /*
690 * Update warnings counter(s) if requested
691 */
692 if (newlim->d_fieldmask & FS_DQ_BWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100693 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000694 if (newlim->d_fieldmask & FS_DQ_IWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100695 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000696 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100697 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (id == 0) {
700 /*
701 * Timelimits for the super user set the relative time
702 * the other users can be over quota for this file system.
703 * If it is zero a default is used. Ditto for the default
Nathan Scott754002b2005-06-21 15:49:06 +1000704 * soft and hard limit values (already done, above), and
705 * for warnings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
707 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
708 mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100709 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
712 mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100713 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
716 mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100717 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Nathan Scott754002b2005-06-21 15:49:06 +1000719 if (newlim->d_fieldmask & FS_DQ_BWARNS)
720 mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
721 if (newlim->d_fieldmask & FS_DQ_IWARNS)
722 mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
723 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
724 mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
725 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * If the user is now over quota, start the timelimit.
728 * The user will not be 'warned'.
729 * Note that we keep the timers ticking, whether enforcement
730 * is on or off. We don't really want to bother with iterating
731 * over all ondisk dquots and turning the timers on/off.
732 */
733 xfs_qm_adjust_dqtimers(mp, ddq);
734 }
735 dqp->dq_flags |= XFS_DQ_DIRTY;
736 xfs_trans_log_dquot(tp, dqp);
737
738 xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
739 xfs_trans_commit(tp, 0, NULL);
740 xfs_qm_dqprint(dqp);
741 xfs_qm_dqrele(dqp);
742 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
743
744 return (0);
745}
746
747STATIC int
748xfs_qm_scall_getquota(
749 xfs_mount_t *mp,
750 xfs_dqid_t id,
751 uint type,
752 fs_disk_quota_t *out)
753{
754 xfs_dquot_t *dqp;
755 int error;
756
757 /*
758 * Try to get the dquot. We don't want it allocated on disk, so
759 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
760 * exist, we'll get ENOENT back.
761 */
762 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
763 return (error);
764 }
765
766 xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
767 /*
768 * If everything's NULL, this dquot doesn't quite exist as far as
769 * our utility programs are concerned.
770 */
771 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
772 xfs_qm_dqput(dqp);
773 return XFS_ERROR(ENOENT);
774 }
775 /* xfs_qm_dqprint(dqp); */
776 /*
777 * Convert the disk dquot to the exportable format
778 */
779 xfs_qm_export_dquot(mp, &dqp->q_core, out);
780 xfs_qm_dqput(dqp);
781 return (error ? XFS_ERROR(EFAULT) : 0);
782}
783
784
785STATIC int
786xfs_qm_log_quotaoff_end(
787 xfs_mount_t *mp,
788 xfs_qoff_logitem_t *startqoff,
789 uint flags)
790{
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000791 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000793 xfs_qoff_logitem_t *qoffi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
796
797 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
798 0, 0, XFS_DEFAULT_LOG_COUNT))) {
799 xfs_trans_cancel(tp, 0);
800 return (error);
801 }
802
803 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
804 flags & XFS_ALL_QUOTA_ACCT);
805 xfs_trans_log_quotaoff_item(tp, qoffi);
806
807 /*
808 * We have to make sure that the transaction is secure on disk before we
809 * return and actually stop quota accounting. So, make it synchronous.
810 * We don't care about quotoff's performance.
811 */
812 xfs_trans_set_sync(tp);
813 error = xfs_trans_commit(tp, 0, NULL);
814 return (error);
815}
816
817
818STATIC int
819xfs_qm_log_quotaoff(
820 xfs_mount_t *mp,
821 xfs_qoff_logitem_t **qoffstartp,
822 uint flags)
823{
824 xfs_trans_t *tp;
825 int error;
826 unsigned long s;
827 xfs_qoff_logitem_t *qoffi=NULL;
828 uint oldsbqflag=0;
829
830 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
831 if ((error = xfs_trans_reserve(tp, 0,
832 sizeof(xfs_qoff_logitem_t) * 2 +
833 mp->m_sb.sb_sectsize + 128,
834 0,
835 0,
836 XFS_DEFAULT_LOG_COUNT))) {
837 goto error0;
838 }
839
840 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
841 xfs_trans_log_quotaoff_item(tp, qoffi);
842
843 s = XFS_SB_LOCK(mp);
844 oldsbqflag = mp->m_sb.sb_qflags;
845 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
846 XFS_SB_UNLOCK(mp, s);
847
848 xfs_mod_sb(tp, XFS_SB_QFLAGS);
849
850 /*
851 * We have to make sure that the transaction is secure on disk before we
852 * return and actually stop quota accounting. So, make it synchronous.
853 * We don't care about quotoff's performance.
854 */
855 xfs_trans_set_sync(tp);
856 error = xfs_trans_commit(tp, 0, NULL);
857
858error0:
859 if (error) {
860 xfs_trans_cancel(tp, 0);
861 /*
862 * No one else is modifying sb_qflags, so this is OK.
863 * We still hold the quotaofflock.
864 */
865 s = XFS_SB_LOCK(mp);
866 mp->m_sb.sb_qflags = oldsbqflag;
867 XFS_SB_UNLOCK(mp, s);
868 }
869 *qoffstartp = qoffi;
870 return (error);
871}
872
873
874/*
875 * Translate an internal style on-disk-dquot to the exportable format.
876 * The main differences are that the counters/limits are all in Basic
877 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
878 * to be converted to the native endianness.
879 */
880STATIC void
881xfs_qm_export_dquot(
882 xfs_mount_t *mp,
883 xfs_disk_dquot_t *src,
884 struct fs_disk_quota *dst)
885{
886 memset(dst, 0, sizeof(*dst));
887 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100888 dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
889 dst->d_id = be32_to_cpu(src->d_id);
890 dst->d_blk_hardlimit =
891 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
892 dst->d_blk_softlimit =
893 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
894 dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
895 dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
896 dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
897 dst->d_icount = be64_to_cpu(src->d_icount);
898 dst->d_btimer = be32_to_cpu(src->d_btimer);
899 dst->d_itimer = be32_to_cpu(src->d_itimer);
900 dst->d_iwarns = be16_to_cpu(src->d_iwarns);
901 dst->d_bwarns = be16_to_cpu(src->d_bwarns);
902 dst->d_rtb_hardlimit =
903 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
904 dst->d_rtb_softlimit =
905 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
906 dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
907 dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
908 dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /*
911 * Internally, we don't reset all the timers when quota enforcement
912 * gets turned off. No need to confuse the userlevel code,
913 * so return zeroes in that case.
914 */
915 if (! XFS_IS_QUOTA_ENFORCED(mp)) {
916 dst->d_btimer = 0;
917 dst->d_itimer = 0;
918 dst->d_rtbtimer = 0;
919 }
920
921#ifdef DEBUG
922 if (XFS_IS_QUOTA_ENFORCED(mp) && dst->d_id != 0) {
923 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
924 (dst->d_blk_softlimit > 0)) {
925 ASSERT(dst->d_btimer != 0);
926 }
927 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
928 (dst->d_ino_softlimit > 0)) {
929 ASSERT(dst->d_itimer != 0);
930 }
931 }
932#endif
933}
934
935STATIC uint
936xfs_qm_import_qtype_flags(
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000937 uint uflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000939 uint oflags = 0;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000942 * Can't be more than one, or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 */
944 if (((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ==
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000945 (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ||
946 ((uflags & (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ==
947 (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ||
948 ((uflags & (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ==
949 (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ||
950 ((uflags & (XFS_GROUP_QUOTA|XFS_USER_QUOTA|XFS_PROJ_QUOTA)) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return (0);
952
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000953 oflags |= (uflags & XFS_USER_QUOTA) ? XFS_DQ_USER : 0;
954 oflags |= (uflags & XFS_PROJ_QUOTA) ? XFS_DQ_PROJ : 0;
955 oflags |= (uflags & XFS_GROUP_QUOTA) ? XFS_DQ_GROUP: 0;
956 return oflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959STATIC uint
960xfs_qm_export_qtype_flags(
961 uint flags)
962{
963 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000964 * Can't be more than one, or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000966 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
967 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
968 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
969 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
970 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
971 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
972 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 return (flags & XFS_DQ_USER) ?
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000975 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
976 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979STATIC uint
980xfs_qm_import_flags(
981 uint uflags)
982{
983 uint flags = 0;
984
985 if (uflags & XFS_QUOTA_UDQ_ACCT)
986 flags |= XFS_UQUOTA_ACCT;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000987 if (uflags & XFS_QUOTA_PDQ_ACCT)
988 flags |= XFS_PQUOTA_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (uflags & XFS_QUOTA_GDQ_ACCT)
990 flags |= XFS_GQUOTA_ACCT;
991 if (uflags & XFS_QUOTA_UDQ_ENFD)
992 flags |= XFS_UQUOTA_ENFD;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000993 if (uflags & (XFS_QUOTA_PDQ_ENFD|XFS_QUOTA_GDQ_ENFD))
994 flags |= XFS_OQUOTA_ENFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return (flags);
996}
997
998
999STATIC uint
1000xfs_qm_export_flags(
1001 uint flags)
1002{
1003 uint uflags;
1004
1005 uflags = 0;
1006 if (flags & XFS_UQUOTA_ACCT)
1007 uflags |= XFS_QUOTA_UDQ_ACCT;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001008 if (flags & XFS_PQUOTA_ACCT)
1009 uflags |= XFS_QUOTA_PDQ_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (flags & XFS_GQUOTA_ACCT)
1011 uflags |= XFS_QUOTA_GDQ_ACCT;
1012 if (flags & XFS_UQUOTA_ENFD)
1013 uflags |= XFS_QUOTA_UDQ_ENFD;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001014 if (flags & (XFS_OQUOTA_ENFD)) {
1015 uflags |= (flags & XFS_GQUOTA_ACCT) ?
1016 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return (uflags);
1019}
1020
1021
1022/*
1023 * Go thru all the inodes in the file system, releasing their dquots.
1024 * Note that the mount structure gets modified to indicate that quotas are off
1025 * AFTER this, in the case of quotaoff. This also gets called from
1026 * xfs_rootumount.
1027 */
1028void
1029xfs_qm_dqrele_all_inodes(
1030 struct xfs_mount *mp,
1031 uint flags)
1032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 xfs_inode_t *ip, *topino;
1034 uint ireclaims;
1035 vnode_t *vp;
1036 boolean_t vnode_refd;
1037
1038 ASSERT(mp->m_quotainfo);
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 XFS_MOUNT_ILOCK(mp);
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001041again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 ip = mp->m_inodes;
1043 if (ip == NULL) {
1044 XFS_MOUNT_IUNLOCK(mp);
1045 return;
1046 }
1047 do {
1048 /* Skip markers inserted by xfs_sync */
1049 if (ip->i_mount == NULL) {
1050 ip = ip->i_mnext;
1051 continue;
1052 }
1053 /* Root inode, rbmip and rsumip have associated blocks */
1054 if (ip == XFS_QI_UQIP(mp) || ip == XFS_QI_GQIP(mp)) {
1055 ASSERT(ip->i_udquot == NULL);
1056 ASSERT(ip->i_gdquot == NULL);
1057 ip = ip->i_mnext;
1058 continue;
1059 }
1060 vp = XFS_ITOV_NULL(ip);
1061 if (!vp) {
1062 ASSERT(ip->i_udquot == NULL);
1063 ASSERT(ip->i_gdquot == NULL);
1064 ip = ip->i_mnext;
1065 continue;
1066 }
1067 vnode_refd = B_FALSE;
1068 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ireclaims = mp->m_ireclaims;
1070 topino = mp->m_inodes;
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001071 vp = vn_grab(vp);
1072 if (!vp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 goto again;
Christoph Hellwigcdb62682005-09-02 16:24:19 +10001074
1075 XFS_MOUNT_IUNLOCK(mp);
1076 /* XXX restart limit ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 xfs_ilock(ip, XFS_ILOCK_EXCL);
1078 vnode_refd = B_TRUE;
1079 } else {
1080 ireclaims = mp->m_ireclaims;
1081 topino = mp->m_inodes;
1082 XFS_MOUNT_IUNLOCK(mp);
1083 }
1084
1085 /*
1086 * We don't keep the mountlock across the dqrele() call,
1087 * since it can take a while..
1088 */
1089 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
1090 xfs_qm_dqrele(ip->i_udquot);
1091 ip->i_udquot = NULL;
1092 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001093 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 xfs_qm_dqrele(ip->i_gdquot);
1095 ip->i_gdquot = NULL;
1096 }
1097 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1098 /*
1099 * Wait until we've dropped the ilock and mountlock to
1100 * do the vn_rele. Or be condemned to an eternity in the
1101 * inactive code in hell.
1102 */
1103 if (vnode_refd)
1104 VN_RELE(vp);
1105 XFS_MOUNT_ILOCK(mp);
1106 /*
1107 * If an inode was inserted or removed, we gotta
1108 * start over again.
1109 */
1110 if (topino != mp->m_inodes || mp->m_ireclaims != ireclaims) {
1111 /* XXX use a sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 goto again;
1113 }
1114 ip = ip->i_mnext;
1115 } while (ip != mp->m_inodes);
1116
1117 XFS_MOUNT_IUNLOCK(mp);
1118}
1119
1120/*------------------------------------------------------------------------*/
1121#ifdef DEBUG
1122/*
1123 * This contains all the test functions for XFS disk quotas.
1124 * Currently it does a quota accounting check. ie. it walks through
1125 * all inodes in the file system, calculating the dquot accounting fields,
1126 * and prints out any inconsistencies.
1127 */
1128xfs_dqhash_t *qmtest_udqtab;
1129xfs_dqhash_t *qmtest_gdqtab;
1130int qmtest_hashmask;
1131int qmtest_nfails;
1132mutex_t qcheck_lock;
1133
1134#define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
1135 (__psunsigned_t)(id)) & \
1136 (qmtest_hashmask - 1))
1137
1138#define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
1139 (qmtest_udqtab + \
1140 DQTEST_HASHVAL(mp, id)) : \
1141 (qmtest_gdqtab + \
1142 DQTEST_HASHVAL(mp, id)))
1143
1144#define DQTEST_LIST_PRINT(l, NXT, title) \
1145{ \
1146 xfs_dqtest_t *dqp; int i = 0;\
1147 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
1148 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
1149 dqp = (xfs_dqtest_t *)dqp->NXT) { \
1150 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
1151 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
1152 dqp->d_bcount, dqp->d_icount); } \
1153}
1154
1155typedef struct dqtest {
1156 xfs_dqmarker_t q_lists;
1157 xfs_dqhash_t *q_hash; /* the hashchain header */
1158 xfs_mount_t *q_mount; /* filesystem this relates to */
1159 xfs_dqid_t d_id; /* user id or group id */
1160 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
1161 xfs_qcnt_t d_icount; /* # inodes owned by the user */
1162} xfs_dqtest_t;
1163
1164STATIC void
1165xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
1166{
1167 xfs_dquot_t *d;
1168 if (((d) = (h)->qh_next))
1169 (d)->HL_PREVP = &((dqp)->HL_NEXT);
1170 (dqp)->HL_NEXT = d;
1171 (dqp)->HL_PREVP = &((h)->qh_next);
1172 (h)->qh_next = (xfs_dquot_t *)dqp;
1173 (h)->qh_version++;
1174 (h)->qh_nelems++;
1175}
1176STATIC void
1177xfs_qm_dqtest_print(
1178 xfs_dqtest_t *d)
1179{
1180 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
1181 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
1183 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1184 d->d_bcount, (int)d->d_bcount);
1185 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1186 d->d_icount, (int)d->d_icount);
1187 cmn_err(CE_DEBUG, "---------------------------");
1188}
1189
1190STATIC void
1191xfs_qm_dqtest_failed(
1192 xfs_dqtest_t *d,
1193 xfs_dquot_t *dqp,
1194 char *reason,
1195 xfs_qcnt_t a,
1196 xfs_qcnt_t b,
1197 int error)
1198{
1199 qmtest_nfails++;
1200 if (error)
1201 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001202 d->d_id, error, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 else
1204 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001205 d->d_id, reason, (int)a, (int)b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 xfs_qm_dqtest_print(d);
1207 if (dqp)
1208 xfs_qm_dqprint(dqp);
1209}
1210
1211STATIC int
1212xfs_dqtest_cmp2(
1213 xfs_dqtest_t *d,
1214 xfs_dquot_t *dqp)
1215{
1216 int err = 0;
Christoph Hellwig1149d962005-11-02 15:01:12 +11001217 if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001219 be64_to_cpu(dqp->q_core.d_icount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 d->d_icount, 0);
1221 err++;
1222 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001223 if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001225 be64_to_cpu(dqp->q_core.d_bcount),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 d->d_bcount, 0);
1227 err++;
1228 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001229 if (dqp->q_core.d_blk_softlimit &&
1230 be64_to_cpu(dqp->q_core.d_bcount) >=
1231 be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1233 cmn_err(CE_DEBUG,
1234 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1235 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1236 err++;
1237 }
1238 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11001239 if (dqp->q_core.d_ino_softlimit &&
1240 be64_to_cpu(dqp->q_core.d_icount) >=
1241 be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1243 cmn_err(CE_DEBUG,
1244 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1245 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1246 err++;
1247 }
1248 }
1249#ifdef QUOTADEBUG
1250 if (!err) {
1251 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001252 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254#endif
1255 return (err);
1256}
1257
1258STATIC void
1259xfs_dqtest_cmp(
1260 xfs_dqtest_t *d)
1261{
1262 xfs_dquot_t *dqp;
1263 int error;
1264
1265 /* xfs_qm_dqtest_print(d); */
1266 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1267 &dqp))) {
1268 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1269 return;
1270 }
1271 xfs_dqtest_cmp2(d, dqp);
1272 xfs_qm_dqput(dqp);
1273}
1274
1275STATIC int
1276xfs_qm_internalqcheck_dqget(
1277 xfs_mount_t *mp,
1278 xfs_dqid_t id,
1279 uint type,
1280 xfs_dqtest_t **O_dq)
1281{
1282 xfs_dqtest_t *d;
1283 xfs_dqhash_t *h;
1284
1285 h = DQTEST_HASH(mp, id, type);
1286 for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
1287 d = (xfs_dqtest_t *) d->HL_NEXT) {
1288 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1289 if (d->d_id == id && mp == d->q_mount) {
1290 *O_dq = d;
1291 return (0);
1292 }
1293 }
1294 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1295 d->dq_flags = type;
1296 d->d_id = id;
1297 d->q_mount = mp;
1298 d->q_hash = h;
1299 xfs_qm_hashinsert(h, d);
1300 *O_dq = d;
1301 return (0);
1302}
1303
1304STATIC void
1305xfs_qm_internalqcheck_get_dquots(
1306 xfs_mount_t *mp,
1307 xfs_dqid_t uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001308 xfs_dqid_t projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 xfs_dqid_t gid,
1310 xfs_dqtest_t **ud,
1311 xfs_dqtest_t **gd)
1312{
1313 if (XFS_IS_UQUOTA_ON(mp))
1314 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1315 if (XFS_IS_GQUOTA_ON(mp))
1316 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001317 else if (XFS_IS_PQUOTA_ON(mp))
1318 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
1321
1322STATIC void
1323xfs_qm_internalqcheck_dqadjust(
1324 xfs_inode_t *ip,
1325 xfs_dqtest_t *d)
1326{
1327 d->d_icount++;
1328 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1329}
1330
1331STATIC int
1332xfs_qm_internalqcheck_adjust(
1333 xfs_mount_t *mp, /* mount point for filesystem */
1334 xfs_ino_t ino, /* inode number to get data for */
1335 void __user *buffer, /* not used */
1336 int ubsize, /* not used */
1337 void *private_data, /* not used */
1338 xfs_daddr_t bno, /* starting block of inode cluster */
1339 int *ubused, /* not used */
1340 void *dip, /* not used */
1341 int *res) /* bulkstat result code */
1342{
1343 xfs_inode_t *ip;
1344 xfs_dqtest_t *ud, *gd;
1345 uint lock_flags;
1346 boolean_t ipreleased;
1347 int error;
1348
1349 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1350
1351 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1352 *res = BULKSTAT_RV_NOTHING;
1353 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1354 (unsigned long long) ino,
1355 (unsigned long long) mp->m_sb.sb_uquotino,
1356 (unsigned long long) mp->m_sb.sb_gquotino);
1357 return XFS_ERROR(EINVAL);
1358 }
1359 ipreleased = B_FALSE;
1360 again:
1361 lock_flags = XFS_ILOCK_SHARED;
1362 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1363 *res = BULKSTAT_RV_NOTHING;
1364 return (error);
1365 }
1366
1367 if (ip->i_d.di_mode == 0) {
1368 xfs_iput_new(ip, lock_flags);
1369 *res = BULKSTAT_RV_NOTHING;
1370 return XFS_ERROR(ENOENT);
1371 }
1372
1373 /*
1374 * This inode can have blocks after eof which can get released
1375 * when we send it to inactive. Since we don't check the dquot
1376 * until the after all our calculations are done, we must get rid
1377 * of those now.
1378 */
1379 if (! ipreleased) {
1380 xfs_iput(ip, lock_flags);
1381 ipreleased = B_TRUE;
1382 goto again;
1383 }
1384 xfs_qm_internalqcheck_get_dquots(mp,
1385 (xfs_dqid_t) ip->i_d.di_uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001386 (xfs_dqid_t) ip->i_d.di_projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 (xfs_dqid_t) ip->i_d.di_gid,
1388 &ud, &gd);
1389 if (XFS_IS_UQUOTA_ON(mp)) {
1390 ASSERT(ud);
1391 xfs_qm_internalqcheck_dqadjust(ip, ud);
1392 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001393 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 ASSERT(gd);
1395 xfs_qm_internalqcheck_dqadjust(ip, gd);
1396 }
1397 xfs_iput(ip, lock_flags);
1398 *res = BULKSTAT_RV_DIDONE;
1399 return (0);
1400}
1401
1402
1403/* PRIVATE, debugging */
1404int
1405xfs_qm_internalqcheck(
1406 xfs_mount_t *mp)
1407{
1408 xfs_ino_t lastino;
1409 int done, count;
1410 int i;
1411 xfs_dqtest_t *d, *e;
1412 xfs_dqhash_t *h1;
1413 int error;
1414
1415 lastino = 0;
1416 qmtest_hashmask = 32;
1417 count = 5;
1418 done = 0;
1419 qmtest_nfails = 0;
1420
1421 if (! XFS_IS_QUOTA_ON(mp))
1422 return XFS_ERROR(ESRCH);
1423
1424 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1425 XFS_bflush(mp->m_ddev_targp);
1426 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1427 XFS_bflush(mp->m_ddev_targp);
1428
Jes Sorensen794ee1b2006-01-09 15:59:21 -08001429 mutex_lock(&qcheck_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* There should be absolutely no quota activity while this
1431 is going on. */
1432 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1433 sizeof(xfs_dqhash_t), KM_SLEEP);
1434 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1435 sizeof(xfs_dqhash_t), KM_SLEEP);
1436 do {
1437 /*
1438 * Iterate thru all the inodes in the file system,
1439 * adjusting the corresponding dquot counters
1440 */
1441 if ((error = xfs_bulkstat(mp, &lastino, &count,
1442 xfs_qm_internalqcheck_adjust, NULL,
1443 0, NULL, BULKSTAT_FG_IGET, &done))) {
1444 break;
1445 }
1446 } while (! done);
1447 if (error) {
1448 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1449 }
1450 cmn_err(CE_DEBUG, "Checking results against system dquots");
1451 for (i = 0; i < qmtest_hashmask; i++) {
1452 h1 = &qmtest_udqtab[i];
1453 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1454 xfs_dqtest_cmp(d);
1455 e = (xfs_dqtest_t *) d->HL_NEXT;
1456 kmem_free(d, sizeof(xfs_dqtest_t));
1457 d = e;
1458 }
1459 h1 = &qmtest_gdqtab[i];
1460 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1461 xfs_dqtest_cmp(d);
1462 e = (xfs_dqtest_t *) d->HL_NEXT;
1463 kmem_free(d, sizeof(xfs_dqtest_t));
1464 d = e;
1465 }
1466 }
1467
1468 if (qmtest_nfails) {
1469 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1470 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1471 } else {
1472 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1473 }
1474 kmem_free(qmtest_udqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1475 kmem_free(qmtest_gdqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1476 mutex_unlock(&qcheck_lock);
1477 return (qmtest_nfails);
1478}
1479
1480#endif /* DEBUG */