[PATCH] UFS: inode->i_sem is not released in error path
[linux-2.6/verdex.git] / fs / xfs / quota / xfs_qm_syscalls.c
blob24690e1af659e4d333c973630bba5499330374c7
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * 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.
14 * 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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #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"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_itable.h"
42 #include "xfs_bmap.h"
43 #include "xfs_btree.h"
44 #include "xfs_rtalloc.h"
45 #include "xfs_error.h"
46 #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"
53 #include "xfs_qm.h"
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
61 STATIC int xfs_qm_scall_trunc_qfiles(xfs_mount_t *, uint);
62 STATIC int xfs_qm_scall_getquota(xfs_mount_t *, xfs_dqid_t, uint,
63 fs_disk_quota_t *);
64 STATIC int xfs_qm_scall_getqstat(xfs_mount_t *, fs_quota_stat_t *);
65 STATIC int xfs_qm_scall_setqlim(xfs_mount_t *, xfs_dqid_t, uint,
66 fs_disk_quota_t *);
67 STATIC int xfs_qm_scall_quotaon(xfs_mount_t *, uint);
68 STATIC int xfs_qm_scall_quotaoff(xfs_mount_t *, uint, boolean_t);
69 STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
70 STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
71 uint);
72 STATIC uint xfs_qm_import_flags(uint);
73 STATIC uint xfs_qm_export_flags(uint);
74 STATIC uint xfs_qm_import_qtype_flags(uint);
75 STATIC uint xfs_qm_export_qtype_flags(uint);
76 STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
77 fs_disk_quota_t *);
81 * The main distribution switch of all XFS quotactl system calls.
83 int
84 xfs_qm_quotactl(
85 struct bhv_desc *bdp,
86 int cmd,
87 int id,
88 xfs_caddr_t addr)
90 xfs_mount_t *mp;
91 int error;
92 struct vfs *vfsp;
94 vfsp = bhvtovfs(bdp);
95 mp = XFS_VFSTOM(vfsp);
97 ASSERT(addr != NULL || cmd == Q_XQUOTASYNC);
100 * The following commands are valid even when quotaoff.
102 switch (cmd) {
103 case Q_XQUOTARM:
105 * Truncate quota files. quota must be off.
107 if (XFS_IS_QUOTA_ON(mp))
108 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)));
114 case Q_XGETQSTAT:
116 * Get quota status information.
118 return (xfs_qm_scall_getqstat(mp, (fs_quota_stat_t *)addr));
120 case Q_XQUOTAON:
122 * QUOTAON - enabling quota enforcement.
123 * Quota accounting must be turned on at mount time.
125 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)));
130 case Q_XQUOTAOFF:
131 if (vfsp->vfs_flag & VFS_RDONLY)
132 return XFS_ERROR(EROFS);
133 break;
135 case Q_XQUOTASYNC:
136 return (xfs_sync_inodes(mp, SYNC_DELWRI, 0, NULL));
138 default:
139 break;
142 if (! XFS_IS_QUOTA_ON(mp))
143 return XFS_ERROR(ESRCH);
145 switch (cmd) {
146 case Q_XQUOTAOFF:
147 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;
154 case Q_XGETQUOTA:
155 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_USER,
156 (fs_disk_quota_t *)addr);
157 break;
158 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;
167 case Q_XSETQLIM:
168 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;
173 case Q_XSETGQLIM:
174 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;
179 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);
184 break;
186 default:
187 error = XFS_ERROR(EINVAL);
188 break;
191 return (error);
195 * Turn off quota accounting and/or enforcement for all udquots and/or
196 * gdquots. Called only at unmount time.
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.
202 STATIC int
203 xfs_qm_scall_quotaoff(
204 xfs_mount_t *mp,
205 uint flags,
206 boolean_t force)
208 uint dqtype;
209 unsigned long s;
210 int error;
211 uint inactivate_flags;
212 xfs_qoff_logitem_t *qoffstart;
213 int nculprits;
215 if (!force && !capable(CAP_SYS_ADMIN))
216 return XFS_ERROR(EPERM);
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.
222 if ((mp->m_qflags & flags) == 0)
223 return XFS_ERROR(EEXIST);
224 error = 0;
226 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
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.
234 ASSERT(mp->m_quotainfo);
235 if (mp->m_quotainfo)
236 mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
238 ASSERT(mp->m_quotainfo);
241 * If we're just turning off quota enforcement, change mp and go.
243 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
244 mp->m_qflags &= ~(flags);
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)));
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);
256 dqtype = 0;
257 inactivate_flags = 0;
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.
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;
268 if (flags & XFS_GQUOTA_ACCT) {
269 dqtype |= XFS_QMOPT_GQUOTA;
270 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
271 inactivate_flags |= XFS_GQUOTA_ACTIVE;
272 } 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;
279 * Nothing to do? Don't complain. This happens when we're just
280 * turning off quota enforcement.
282 if ((mp->m_qflags & flags) == 0) {
283 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
284 return (0);
288 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
289 * and synchronously.
291 xfs_qm_log_quotaoff(mp, &qoffstart, flags);
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.
302 * There is no turning back after this.
304 mp->m_qflags &= ~inactivate_flags;
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).
316 xfs_qm_dqrele_all_inodes(mp, flags);
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.
323 mp->m_qflags &= ~(flags);
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.
333 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
334 delay(10 * nculprits);
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.
344 * So, we have QUOTAOFF start and end logitems; the start
345 * logitem won't get overwritten until the end logitem appears...
347 xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
350 * If quotas is completely disabled, close shop.
352 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
353 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
354 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
355 xfs_qm_destroy_quotainfo(mp);
356 return (0);
360 * Release our quotainode references, and vn_purge them,
361 * if we don't need them anymore.
363 if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
364 XFS_PURGE_INODE(XFS_QI_UQIP(mp));
365 XFS_QI_UQIP(mp) = NULL;
367 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
368 XFS_PURGE_INODE(XFS_QI_GQIP(mp));
369 XFS_QI_GQIP(mp) = NULL;
371 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
373 return (error);
376 STATIC int
377 xfs_qm_scall_trunc_qfiles(
378 xfs_mount_t *mp,
379 uint flags)
381 int error;
382 xfs_inode_t *qip;
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);
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));
400 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
401 mp->m_sb.sb_gquotino != NULLFSINO) {
402 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));
409 return (error);
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.)
418 STATIC int
419 xfs_qm_scall_quotaon(
420 xfs_mount_t *mp,
421 uint flags)
423 int error;
424 unsigned long s;
425 uint qf;
426 uint accflags;
427 __int64_t sbflags;
429 if (!capable(CAP_SYS_ADMIN))
430 return XFS_ERROR(EPERM);
432 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
434 * Switching on quota accounting must be done at mount time.
436 accflags = flags & XFS_ALL_QUOTA_ACCT;
437 flags &= ~(XFS_ALL_QUOTA_ACCT);
439 sbflags = 0;
441 if (flags == 0) {
442 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
443 return XFS_ERROR(EINVAL);
446 /* No fs can turn on quotas with a delayed effect */
447 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
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.
454 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
455 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
456 (flags & XFS_UQUOTA_ENFD))
458 ((flags & XFS_PQUOTA_ACCT) == 0 &&
459 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
460 (flags & XFS_OQUOTA_ENFD))
462 ((flags & XFS_GQUOTA_ACCT) == 0 &&
463 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
464 (flags & XFS_OQUOTA_ENFD))) {
465 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
466 flags, mp->m_sb.sb_qflags);
467 return XFS_ERROR(EINVAL);
470 * If everything's upto-date incore, then don't waste time.
472 if ((mp->m_qflags & flags) == flags)
473 return XFS_ERROR(EEXIST);
476 * Change sb_qflags on disk but not incore mp->qflags
477 * if this is the root filesystem.
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);
485 * There's nothing to change if it's the same.
487 if ((qf & flags) == flags && sbflags == 0)
488 return XFS_ERROR(EEXIST);
489 sbflags |= XFS_SB_QFLAGS;
491 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
492 return (error);
494 * If we aren't trying to switch on quota enforcement, we are done.
496 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
497 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
498 ((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)) ||
502 (flags & XFS_ALL_QUOTA_ENFD) == 0)
503 return (0);
505 if (! XFS_IS_QUOTA_RUNNING(mp))
506 return XFS_ERROR(ESRCH);
509 * Switch on quota enforcement in core.
511 mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
512 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
513 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
515 return (0);
520 * Return quota status information, such as uquota-off, enforcements, etc.
522 STATIC int
523 xfs_qm_scall_getqstat(
524 xfs_mount_t *mp,
525 fs_quota_stat_t *out)
527 xfs_inode_t *uip, *gip;
528 boolean_t tempuqip, tempgqip;
530 uip = gip = NULL;
531 tempuqip = tempgqip = B_FALSE;
532 memset(out, 0, sizeof(fs_quota_stat_t));
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);
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;
547 if (mp->m_quotainfo) {
548 uip = mp->m_quotainfo->qi_uquotaip;
549 gip = mp->m_quotainfo->qi_gquotaip;
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;
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;
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));
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));
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);
581 return (0);
585 * Adjust quota limits, and start/stop timers accordingly.
587 STATIC int
588 xfs_qm_scall_setqlim(
589 xfs_mount_t *mp,
590 xfs_dqid_t id,
591 uint type,
592 fs_disk_quota_t *newlim)
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;
600 if (!capable(CAP_SYS_ADMIN))
601 return XFS_ERROR(EPERM);
603 if ((newlim->d_fieldmask &
604 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
605 return (0);
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);
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).
620 mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
623 * Get the dquot (locked), and join it to the transaction.
624 * Allocate the dquot if this doesn't exist.
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);
632 xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
633 xfs_trans_dqjoin(tp, dqp);
634 ddq = &dqp->q_core;
637 * Make sure that hardlimits are >= soft limits before changing.
639 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
640 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
641 be64_to_cpu(ddq->d_blk_hardlimit);
642 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
643 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
644 be64_to_cpu(ddq->d_blk_softlimit);
645 if (hard == 0 || hard >= soft) {
646 ddq->d_blk_hardlimit = cpu_to_be64(hard);
647 ddq->d_blk_softlimit = cpu_to_be64(soft);
648 if (id == 0) {
649 mp->m_quotainfo->qi_bhardlimit = hard;
650 mp->m_quotainfo->qi_bsoftlimit = soft;
652 } else {
653 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
655 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
656 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
657 be64_to_cpu(ddq->d_rtb_hardlimit);
658 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
659 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
660 be64_to_cpu(ddq->d_rtb_softlimit);
661 if (hard == 0 || hard >= soft) {
662 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
663 ddq->d_rtb_softlimit = cpu_to_be64(soft);
664 if (id == 0) {
665 mp->m_quotainfo->qi_rtbhardlimit = hard;
666 mp->m_quotainfo->qi_rtbsoftlimit = soft;
668 } else {
669 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
672 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
673 (xfs_qcnt_t) newlim->d_ino_hardlimit :
674 be64_to_cpu(ddq->d_ino_hardlimit);
675 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
676 (xfs_qcnt_t) newlim->d_ino_softlimit :
677 be64_to_cpu(ddq->d_ino_softlimit);
678 if (hard == 0 || hard >= soft) {
679 ddq->d_ino_hardlimit = cpu_to_be64(hard);
680 ddq->d_ino_softlimit = cpu_to_be64(soft);
681 if (id == 0) {
682 mp->m_quotainfo->qi_ihardlimit = hard;
683 mp->m_quotainfo->qi_isoftlimit = soft;
685 } else {
686 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
690 * Update warnings counter(s) if requested
692 if (newlim->d_fieldmask & FS_DQ_BWARNS)
693 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
694 if (newlim->d_fieldmask & FS_DQ_IWARNS)
695 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
696 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
697 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
699 if (id == 0) {
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
704 * soft and hard limit values (already done, above), and
705 * for warnings.
707 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
708 mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
709 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
711 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
712 mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
713 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
715 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
716 mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
717 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
719 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 {
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.
733 xfs_qm_adjust_dqtimers(mp, ddq);
735 dqp->dq_flags |= XFS_DQ_DIRTY;
736 xfs_trans_log_dquot(tp, dqp);
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)));
744 return (0);
747 STATIC int
748 xfs_qm_scall_getquota(
749 xfs_mount_t *mp,
750 xfs_dqid_t id,
751 uint type,
752 fs_disk_quota_t *out)
754 xfs_dquot_t *dqp;
755 int error;
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.
762 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
763 return (error);
766 xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
768 * If everything's NULL, this dquot doesn't quite exist as far as
769 * our utility programs are concerned.
771 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
772 xfs_qm_dqput(dqp);
773 return XFS_ERROR(ENOENT);
775 /* xfs_qm_dqprint(dqp); */
777 * Convert the disk dquot to the exportable format
779 xfs_qm_export_dquot(mp, &dqp->q_core, out);
780 xfs_qm_dqput(dqp);
781 return (error ? XFS_ERROR(EFAULT) : 0);
785 STATIC int
786 xfs_qm_log_quotaoff_end(
787 xfs_mount_t *mp,
788 xfs_qoff_logitem_t *startqoff,
789 uint flags)
791 xfs_trans_t *tp;
792 int error;
793 xfs_qoff_logitem_t *qoffi;
795 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
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);
803 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
804 flags & XFS_ALL_QUOTA_ACCT);
805 xfs_trans_log_quotaoff_item(tp, qoffi);
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.
812 xfs_trans_set_sync(tp);
813 error = xfs_trans_commit(tp, 0, NULL);
814 return (error);
818 STATIC int
819 xfs_qm_log_quotaoff(
820 xfs_mount_t *mp,
821 xfs_qoff_logitem_t **qoffstartp,
822 uint flags)
824 xfs_trans_t *tp;
825 int error;
826 unsigned long s;
827 xfs_qoff_logitem_t *qoffi=NULL;
828 uint oldsbqflag=0;
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,
836 XFS_DEFAULT_LOG_COUNT))) {
837 goto error0;
840 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
841 xfs_trans_log_quotaoff_item(tp, qoffi);
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);
848 xfs_mod_sb(tp, XFS_SB_QFLAGS);
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.
855 xfs_trans_set_sync(tp);
856 error = xfs_trans_commit(tp, 0, NULL);
858 error0:
859 if (error) {
860 xfs_trans_cancel(tp, 0);
862 * No one else is modifying sb_qflags, so this is OK.
863 * We still hold the quotaofflock.
865 s = XFS_SB_LOCK(mp);
866 mp->m_sb.sb_qflags = oldsbqflag;
867 XFS_SB_UNLOCK(mp, s);
869 *qoffstartp = qoffi;
870 return (error);
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.
880 STATIC void
881 xfs_qm_export_dquot(
882 xfs_mount_t *mp,
883 xfs_disk_dquot_t *src,
884 struct fs_disk_quota *dst)
886 memset(dst, 0, sizeof(*dst));
887 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
888 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);
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.
915 if (! XFS_IS_QUOTA_ENFORCED(mp)) {
916 dst->d_btimer = 0;
917 dst->d_itimer = 0;
918 dst->d_rtbtimer = 0;
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);
927 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
928 (dst->d_ino_softlimit > 0)) {
929 ASSERT(dst->d_itimer != 0);
932 #endif
935 STATIC uint
936 xfs_qm_import_qtype_flags(
937 uint uflags)
939 uint oflags = 0;
942 * Can't be more than one, or none.
944 if (((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ==
945 (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))
951 return (0);
953 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;
959 STATIC uint
960 xfs_qm_export_qtype_flags(
961 uint flags)
964 * Can't be more than one, or none.
966 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);
974 return (flags & XFS_DQ_USER) ?
975 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
976 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
979 STATIC uint
980 xfs_qm_import_flags(
981 uint uflags)
983 uint flags = 0;
985 if (uflags & XFS_QUOTA_UDQ_ACCT)
986 flags |= XFS_UQUOTA_ACCT;
987 if (uflags & XFS_QUOTA_PDQ_ACCT)
988 flags |= XFS_PQUOTA_ACCT;
989 if (uflags & XFS_QUOTA_GDQ_ACCT)
990 flags |= XFS_GQUOTA_ACCT;
991 if (uflags & XFS_QUOTA_UDQ_ENFD)
992 flags |= XFS_UQUOTA_ENFD;
993 if (uflags & (XFS_QUOTA_PDQ_ENFD|XFS_QUOTA_GDQ_ENFD))
994 flags |= XFS_OQUOTA_ENFD;
995 return (flags);
999 STATIC uint
1000 xfs_qm_export_flags(
1001 uint flags)
1003 uint uflags;
1005 uflags = 0;
1006 if (flags & XFS_UQUOTA_ACCT)
1007 uflags |= XFS_QUOTA_UDQ_ACCT;
1008 if (flags & XFS_PQUOTA_ACCT)
1009 uflags |= XFS_QUOTA_PDQ_ACCT;
1010 if (flags & XFS_GQUOTA_ACCT)
1011 uflags |= XFS_QUOTA_GDQ_ACCT;
1012 if (flags & XFS_UQUOTA_ENFD)
1013 uflags |= XFS_QUOTA_UDQ_ENFD;
1014 if (flags & (XFS_OQUOTA_ENFD)) {
1015 uflags |= (flags & XFS_GQUOTA_ACCT) ?
1016 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
1018 return (uflags);
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.
1028 void
1029 xfs_qm_dqrele_all_inodes(
1030 struct xfs_mount *mp,
1031 uint flags)
1033 xfs_inode_t *ip, *topino;
1034 uint ireclaims;
1035 vnode_t *vp;
1036 boolean_t vnode_refd;
1038 ASSERT(mp->m_quotainfo);
1040 XFS_MOUNT_ILOCK(mp);
1041 again:
1042 ip = mp->m_inodes;
1043 if (ip == NULL) {
1044 XFS_MOUNT_IUNLOCK(mp);
1045 return;
1047 do {
1048 /* Skip markers inserted by xfs_sync */
1049 if (ip->i_mount == NULL) {
1050 ip = ip->i_mnext;
1051 continue;
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;
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;
1067 vnode_refd = B_FALSE;
1068 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1069 ireclaims = mp->m_ireclaims;
1070 topino = mp->m_inodes;
1071 vp = vn_grab(vp);
1072 if (!vp)
1073 goto again;
1075 XFS_MOUNT_IUNLOCK(mp);
1076 /* XXX restart limit ? */
1077 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);
1086 * We don't keep the mountlock across the dqrele() call,
1087 * since it can take a while..
1089 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
1090 xfs_qm_dqrele(ip->i_udquot);
1091 ip->i_udquot = NULL;
1093 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
1094 xfs_qm_dqrele(ip->i_gdquot);
1095 ip->i_gdquot = NULL;
1097 xfs_iunlock(ip, XFS_ILOCK_EXCL);
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.
1103 if (vnode_refd)
1104 VN_RELE(vp);
1105 XFS_MOUNT_ILOCK(mp);
1107 * If an inode was inserted or removed, we gotta
1108 * start over again.
1110 if (topino != mp->m_inodes || mp->m_ireclaims != ireclaims) {
1111 /* XXX use a sentinel */
1112 goto again;
1114 ip = ip->i_mnext;
1115 } while (ip != mp->m_inodes);
1117 XFS_MOUNT_IUNLOCK(mp);
1120 /*------------------------------------------------------------------------*/
1121 #ifdef DEBUG
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.
1128 xfs_dqhash_t *qmtest_udqtab;
1129 xfs_dqhash_t *qmtest_gdqtab;
1130 int qmtest_hashmask;
1131 int qmtest_nfails;
1132 mutex_t qcheck_lock;
1134 #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
1135 (__psunsigned_t)(id)) & \
1136 (qmtest_hashmask - 1))
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)))
1144 #define DQTEST_LIST_PRINT(l, NXT, title) \
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); } \
1155 typedef 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;
1164 STATIC void
1165 xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
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++;
1176 STATIC void
1177 xfs_qm_dqtest_print(
1178 xfs_dqtest_t *d)
1180 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
1181 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
1182 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, "---------------------------");
1190 STATIC void
1191 xfs_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)
1199 qmtest_nfails++;
1200 if (error)
1201 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
1202 d->d_id, error, reason);
1203 else
1204 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
1205 d->d_id, reason, (int)a, (int)b);
1206 xfs_qm_dqtest_print(d);
1207 if (dqp)
1208 xfs_qm_dqprint(dqp);
1211 STATIC int
1212 xfs_dqtest_cmp2(
1213 xfs_dqtest_t *d,
1214 xfs_dquot_t *dqp)
1216 int err = 0;
1217 if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
1218 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
1219 be64_to_cpu(dqp->q_core.d_icount),
1220 d->d_icount, 0);
1221 err++;
1223 if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
1224 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
1225 be64_to_cpu(dqp->q_core.d_bcount),
1226 d->d_bcount, 0);
1227 err++;
1229 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)) {
1232 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++;
1239 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)) {
1242 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++;
1249 #ifdef QUOTADEBUG
1250 if (!err) {
1251 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
1252 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1254 #endif
1255 return (err);
1258 STATIC void
1259 xfs_dqtest_cmp(
1260 xfs_dqtest_t *d)
1262 xfs_dquot_t *dqp;
1263 int error;
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;
1271 xfs_dqtest_cmp2(d, dqp);
1272 xfs_qm_dqput(dqp);
1275 STATIC int
1276 xfs_qm_internalqcheck_dqget(
1277 xfs_mount_t *mp,
1278 xfs_dqid_t id,
1279 uint type,
1280 xfs_dqtest_t **O_dq)
1282 xfs_dqtest_t *d;
1283 xfs_dqhash_t *h;
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);
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);
1304 STATIC void
1305 xfs_qm_internalqcheck_get_dquots(
1306 xfs_mount_t *mp,
1307 xfs_dqid_t uid,
1308 xfs_dqid_t projid,
1309 xfs_dqid_t gid,
1310 xfs_dqtest_t **ud,
1311 xfs_dqtest_t **gd)
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);
1317 else if (XFS_IS_PQUOTA_ON(mp))
1318 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
1322 STATIC void
1323 xfs_qm_internalqcheck_dqadjust(
1324 xfs_inode_t *ip,
1325 xfs_dqtest_t *d)
1327 d->d_icount++;
1328 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1331 STATIC int
1332 xfs_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 */
1343 xfs_inode_t *ip;
1344 xfs_dqtest_t *ud, *gd;
1345 uint lock_flags;
1346 boolean_t ipreleased;
1347 int error;
1349 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
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);
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);
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);
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.
1379 if (! ipreleased) {
1380 xfs_iput(ip, lock_flags);
1381 ipreleased = B_TRUE;
1382 goto again;
1384 xfs_qm_internalqcheck_get_dquots(mp,
1385 (xfs_dqid_t) ip->i_d.di_uid,
1386 (xfs_dqid_t) ip->i_d.di_projid,
1387 (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);
1393 if (XFS_IS_OQUOTA_ON(mp)) {
1394 ASSERT(gd);
1395 xfs_qm_internalqcheck_dqadjust(ip, gd);
1397 xfs_iput(ip, lock_flags);
1398 *res = BULKSTAT_RV_DIDONE;
1399 return (0);
1403 /* PRIVATE, debugging */
1405 xfs_qm_internalqcheck(
1406 xfs_mount_t *mp)
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;
1415 lastino = 0;
1416 qmtest_hashmask = 32;
1417 count = 5;
1418 done = 0;
1419 qmtest_nfails = 0;
1421 if (! XFS_IS_QUOTA_ON(mp))
1422 return XFS_ERROR(ESRCH);
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);
1429 mutex_lock(&qcheck_lock, PINOD);
1430 /* 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 {
1438 * Iterate thru all the inodes in the file system,
1439 * adjusting the corresponding dquot counters
1441 if ((error = xfs_bulkstat(mp, &lastino, &count,
1442 xfs_qm_internalqcheck_adjust, NULL,
1443 0, NULL, BULKSTAT_FG_IGET, &done))) {
1444 break;
1446 } while (! done);
1447 if (error) {
1448 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
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;
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;
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! ********");
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);
1480 #endif /* DEBUG */