reiserfs: fix up lockdep warnings
[linux-2.6/mini2440.git] / fs / xfs / quota / xfs_qm_syscalls.c
blobad5579d4eac479e1c26266a06fb9ad081211bdba
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
19 #include <linux/capability.h>
21 #include "xfs.h"
22 #include "xfs_fs.h"
23 #include "xfs_bit.h"
24 #include "xfs_log.h"
25 #include "xfs_inum.h"
26 #include "xfs_trans.h"
27 #include "xfs_sb.h"
28 #include "xfs_ag.h"
29 #include "xfs_dir2.h"
30 #include "xfs_alloc.h"
31 #include "xfs_dmapi.h"
32 #include "xfs_quota.h"
33 #include "xfs_mount.h"
34 #include "xfs_bmap_btree.h"
35 #include "xfs_alloc_btree.h"
36 #include "xfs_ialloc_btree.h"
37 #include "xfs_dir2_sf.h"
38 #include "xfs_attr_sf.h"
39 #include "xfs_dinode.h"
40 #include "xfs_inode.h"
41 #include "xfs_ialloc.h"
42 #include "xfs_itable.h"
43 #include "xfs_bmap.h"
44 #include "xfs_btree.h"
45 #include "xfs_rtalloc.h"
46 #include "xfs_error.h"
47 #include "xfs_rw.h"
48 #include "xfs_acl.h"
49 #include "xfs_attr.h"
50 #include "xfs_buf_item.h"
51 #include "xfs_utils.h"
52 #include "xfs_qm.h"
54 #ifdef DEBUG
55 # define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
56 #else
57 # define qdprintk(s, args...) do { } while (0)
58 #endif
60 STATIC int xfs_qm_scall_trunc_qfiles(xfs_mount_t *, uint);
61 STATIC int xfs_qm_scall_getquota(xfs_mount_t *, xfs_dqid_t, uint,
62 fs_disk_quota_t *);
63 STATIC int xfs_qm_scall_getqstat(xfs_mount_t *, fs_quota_stat_t *);
64 STATIC int xfs_qm_scall_setqlim(xfs_mount_t *, xfs_dqid_t, uint,
65 fs_disk_quota_t *);
66 STATIC int xfs_qm_scall_quotaon(xfs_mount_t *, uint);
67 STATIC int xfs_qm_scall_quotaoff(xfs_mount_t *, uint, boolean_t);
68 STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
69 STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
70 uint);
71 STATIC uint xfs_qm_import_flags(uint);
72 STATIC uint xfs_qm_export_flags(uint);
73 STATIC uint xfs_qm_import_qtype_flags(uint);
74 STATIC uint xfs_qm_export_qtype_flags(uint);
75 STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
76 fs_disk_quota_t *);
80 * The main distribution switch of all XFS quotactl system calls.
82 int
83 xfs_qm_quotactl(
84 xfs_mount_t *mp,
85 int cmd,
86 int id,
87 xfs_caddr_t addr)
89 int error;
91 ASSERT(addr != NULL || cmd == Q_XQUOTASYNC);
94 * The following commands are valid even when quotaoff.
96 switch (cmd) {
97 case Q_XQUOTARM:
99 * Truncate quota files. quota must be off.
101 if (XFS_IS_QUOTA_ON(mp))
102 return XFS_ERROR(EINVAL);
103 if (mp->m_flags & XFS_MOUNT_RDONLY)
104 return XFS_ERROR(EROFS);
105 return (xfs_qm_scall_trunc_qfiles(mp,
106 xfs_qm_import_qtype_flags(*(uint *)addr)));
108 case Q_XGETQSTAT:
110 * Get quota status information.
112 return (xfs_qm_scall_getqstat(mp, (fs_quota_stat_t *)addr));
114 case Q_XQUOTAON:
116 * QUOTAON - enabling quota enforcement.
117 * Quota accounting must be turned on at mount time.
119 if (mp->m_flags & XFS_MOUNT_RDONLY)
120 return XFS_ERROR(EROFS);
121 return (xfs_qm_scall_quotaon(mp,
122 xfs_qm_import_flags(*(uint *)addr)));
124 case Q_XQUOTAOFF:
125 if (mp->m_flags & XFS_MOUNT_RDONLY)
126 return XFS_ERROR(EROFS);
127 break;
129 case Q_XQUOTASYNC:
130 return (xfs_sync_inodes(mp, SYNC_DELWRI, NULL));
132 default:
133 break;
136 if (! XFS_IS_QUOTA_ON(mp))
137 return XFS_ERROR(ESRCH);
139 switch (cmd) {
140 case Q_XQUOTAOFF:
141 if (mp->m_flags & XFS_MOUNT_RDONLY)
142 return XFS_ERROR(EROFS);
143 error = xfs_qm_scall_quotaoff(mp,
144 xfs_qm_import_flags(*(uint *)addr),
145 B_FALSE);
146 break;
148 case Q_XGETQUOTA:
149 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_USER,
150 (fs_disk_quota_t *)addr);
151 break;
152 case Q_XGETGQUOTA:
153 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
154 (fs_disk_quota_t *)addr);
155 break;
156 case Q_XGETPQUOTA:
157 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
158 (fs_disk_quota_t *)addr);
159 break;
161 case Q_XSETQLIM:
162 if (mp->m_flags & XFS_MOUNT_RDONLY)
163 return XFS_ERROR(EROFS);
164 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_USER,
165 (fs_disk_quota_t *)addr);
166 break;
167 case Q_XSETGQLIM:
168 if (mp->m_flags & XFS_MOUNT_RDONLY)
169 return XFS_ERROR(EROFS);
170 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
171 (fs_disk_quota_t *)addr);
172 break;
173 case Q_XSETPQLIM:
174 if (mp->m_flags & XFS_MOUNT_RDONLY)
175 return XFS_ERROR(EROFS);
176 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
177 (fs_disk_quota_t *)addr);
178 break;
180 default:
181 error = XFS_ERROR(EINVAL);
182 break;
185 return (error);
189 * Turn off quota accounting and/or enforcement for all udquots and/or
190 * gdquots. Called only at unmount time.
192 * This assumes that there are no dquots of this file system cached
193 * incore, and modifies the ondisk dquot directly. Therefore, for example,
194 * it is an error to call this twice, without purging the cache.
196 STATIC int
197 xfs_qm_scall_quotaoff(
198 xfs_mount_t *mp,
199 uint flags,
200 boolean_t force)
202 uint dqtype;
203 unsigned long s;
204 int error;
205 uint inactivate_flags;
206 xfs_qoff_logitem_t *qoffstart;
207 int nculprits;
209 if (!force && !capable(CAP_SYS_ADMIN))
210 return XFS_ERROR(EPERM);
212 * No file system can have quotas enabled on disk but not in core.
213 * Note that quota utilities (like quotaoff) _expect_
214 * errno == EEXIST here.
216 if ((mp->m_qflags & flags) == 0)
217 return XFS_ERROR(EEXIST);
218 error = 0;
220 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
223 * We don't want to deal with two quotaoffs messing up each other,
224 * so we're going to serialize it. quotaoff isn't exactly a performance
225 * critical thing.
226 * If quotaoff, then we must be dealing with the root filesystem.
228 ASSERT(mp->m_quotainfo);
229 if (mp->m_quotainfo)
230 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
232 ASSERT(mp->m_quotainfo);
235 * If we're just turning off quota enforcement, change mp and go.
237 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
238 mp->m_qflags &= ~(flags);
240 s = XFS_SB_LOCK(mp);
241 mp->m_sb.sb_qflags = mp->m_qflags;
242 XFS_SB_UNLOCK(mp, s);
243 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
245 /* XXX what to do if error ? Revert back to old vals incore ? */
246 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
247 return (error);
250 dqtype = 0;
251 inactivate_flags = 0;
253 * If accounting is off, we must turn enforcement off, clear the
254 * quota 'CHKD' certificate to make it known that we have to
255 * do a quotacheck the next time this quota is turned on.
257 if (flags & XFS_UQUOTA_ACCT) {
258 dqtype |= XFS_QMOPT_UQUOTA;
259 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
260 inactivate_flags |= XFS_UQUOTA_ACTIVE;
262 if (flags & XFS_GQUOTA_ACCT) {
263 dqtype |= XFS_QMOPT_GQUOTA;
264 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
265 inactivate_flags |= XFS_GQUOTA_ACTIVE;
266 } else if (flags & XFS_PQUOTA_ACCT) {
267 dqtype |= XFS_QMOPT_PQUOTA;
268 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
269 inactivate_flags |= XFS_PQUOTA_ACTIVE;
273 * Nothing to do? Don't complain. This happens when we're just
274 * turning off quota enforcement.
276 if ((mp->m_qflags & flags) == 0) {
277 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
278 return (0);
282 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
283 * and synchronously.
285 xfs_qm_log_quotaoff(mp, &qoffstart, flags);
288 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
289 * to take care of the race between dqget and quotaoff. We don't take
290 * any special locks to reset these bits. All processes need to check
291 * these bits *after* taking inode lock(s) to see if the particular
292 * quota type is in the process of being turned off. If *ACTIVE, it is
293 * guaranteed that all dquot structures and all quotainode ptrs will all
294 * stay valid as long as that inode is kept locked.
296 * There is no turning back after this.
298 mp->m_qflags &= ~inactivate_flags;
301 * Give back all the dquot reference(s) held by inodes.
302 * Here we go thru every single incore inode in this file system, and
303 * do a dqrele on the i_udquot/i_gdquot that it may have.
304 * Essentially, as long as somebody has an inode locked, this guarantees
305 * that quotas will not be turned off. This is handy because in a
306 * transaction once we lock the inode(s) and check for quotaon, we can
307 * depend on the quota inodes (and other things) being valid as long as
308 * we keep the lock(s).
310 xfs_qm_dqrele_all_inodes(mp, flags);
313 * Next we make the changes in the quota flag in the mount struct.
314 * This isn't protected by a particular lock directly, because we
315 * don't want to take a mrlock everytime we depend on quotas being on.
317 mp->m_qflags &= ~(flags);
320 * Go through all the dquots of this file system and purge them,
321 * according to what was turned off. We may not be able to get rid
322 * of all dquots, because dquots can have temporary references that
323 * are not attached to inodes. eg. xfs_setattr, xfs_create.
324 * So, if we couldn't purge all the dquots from the filesystem,
325 * we can't get rid of the incore data structures.
327 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
328 delay(10 * nculprits);
331 * Transactions that had started before ACTIVE state bit was cleared
332 * could have logged many dquots, so they'd have higher LSNs than
333 * the first QUOTAOFF log record does. If we happen to crash when
334 * the tail of the log has gone past the QUOTAOFF record, but
335 * before the last dquot modification, those dquots __will__
336 * recover, and that's not good.
338 * So, we have QUOTAOFF start and end logitems; the start
339 * logitem won't get overwritten until the end logitem appears...
341 xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
344 * If quotas is completely disabled, close shop.
346 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
347 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
348 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
349 xfs_qm_destroy_quotainfo(mp);
350 return (0);
354 * Release our quotainode references, and vn_purge them,
355 * if we don't need them anymore.
357 if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
358 XFS_PURGE_INODE(XFS_QI_UQIP(mp));
359 XFS_QI_UQIP(mp) = NULL;
361 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
362 XFS_PURGE_INODE(XFS_QI_GQIP(mp));
363 XFS_QI_GQIP(mp) = NULL;
365 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
367 return (error);
370 STATIC int
371 xfs_qm_scall_trunc_qfiles(
372 xfs_mount_t *mp,
373 uint flags)
375 int error;
376 xfs_inode_t *qip;
378 if (!capable(CAP_SYS_ADMIN))
379 return XFS_ERROR(EPERM);
380 error = 0;
381 if (!XFS_SB_VERSION_HASQUOTA(&mp->m_sb) || flags == 0) {
382 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
383 return XFS_ERROR(EINVAL);
386 if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
387 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
388 if (! error) {
389 (void) xfs_truncate_file(mp, qip);
390 VN_RELE(XFS_ITOV(qip));
394 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
395 mp->m_sb.sb_gquotino != NULLFSINO) {
396 error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
397 if (! error) {
398 (void) xfs_truncate_file(mp, qip);
399 VN_RELE(XFS_ITOV(qip));
403 return (error);
408 * Switch on (a given) quota enforcement for a filesystem. This takes
409 * effect immediately.
410 * (Switching on quota accounting must be done at mount time.)
412 STATIC int
413 xfs_qm_scall_quotaon(
414 xfs_mount_t *mp,
415 uint flags)
417 int error;
418 unsigned long s;
419 uint qf;
420 uint accflags;
421 __int64_t sbflags;
423 if (!capable(CAP_SYS_ADMIN))
424 return XFS_ERROR(EPERM);
426 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
428 * Switching on quota accounting must be done at mount time.
430 accflags = flags & XFS_ALL_QUOTA_ACCT;
431 flags &= ~(XFS_ALL_QUOTA_ACCT);
433 sbflags = 0;
435 if (flags == 0) {
436 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
437 return XFS_ERROR(EINVAL);
440 /* No fs can turn on quotas with a delayed effect */
441 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
444 * Can't enforce without accounting. We check the superblock
445 * qflags here instead of m_qflags because rootfs can have
446 * quota acct on ondisk without m_qflags' knowing.
448 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
449 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
450 (flags & XFS_UQUOTA_ENFD))
452 ((flags & XFS_PQUOTA_ACCT) == 0 &&
453 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
454 (flags & XFS_GQUOTA_ACCT) == 0 &&
455 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
456 (flags & XFS_OQUOTA_ENFD))) {
457 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
458 flags, mp->m_sb.sb_qflags);
459 return XFS_ERROR(EINVAL);
462 * If everything's upto-date incore, then don't waste time.
464 if ((mp->m_qflags & flags) == flags)
465 return XFS_ERROR(EEXIST);
468 * Change sb_qflags on disk but not incore mp->qflags
469 * if this is the root filesystem.
471 s = XFS_SB_LOCK(mp);
472 qf = mp->m_sb.sb_qflags;
473 mp->m_sb.sb_qflags = qf | flags;
474 XFS_SB_UNLOCK(mp, s);
477 * There's nothing to change if it's the same.
479 if ((qf & flags) == flags && sbflags == 0)
480 return XFS_ERROR(EEXIST);
481 sbflags |= XFS_SB_QFLAGS;
483 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
484 return (error);
486 * If we aren't trying to switch on quota enforcement, we are done.
488 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
489 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
490 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
491 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
492 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
493 (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
494 (flags & XFS_ALL_QUOTA_ENFD) == 0)
495 return (0);
497 if (! XFS_IS_QUOTA_RUNNING(mp))
498 return XFS_ERROR(ESRCH);
501 * Switch on quota enforcement in core.
503 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
504 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
505 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
507 return (0);
512 * Return quota status information, such as uquota-off, enforcements, etc.
514 STATIC int
515 xfs_qm_scall_getqstat(
516 xfs_mount_t *mp,
517 fs_quota_stat_t *out)
519 xfs_inode_t *uip, *gip;
520 boolean_t tempuqip, tempgqip;
522 uip = gip = NULL;
523 tempuqip = tempgqip = B_FALSE;
524 memset(out, 0, sizeof(fs_quota_stat_t));
526 out->qs_version = FS_QSTAT_VERSION;
527 if (! XFS_SB_VERSION_HASQUOTA(&mp->m_sb)) {
528 out->qs_uquota.qfs_ino = NULLFSINO;
529 out->qs_gquota.qfs_ino = NULLFSINO;
530 return (0);
532 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
533 (XFS_ALL_QUOTA_ACCT|
534 XFS_ALL_QUOTA_ENFD));
535 out->qs_pad = 0;
536 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
537 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
539 if (mp->m_quotainfo) {
540 uip = mp->m_quotainfo->qi_uquotaip;
541 gip = mp->m_quotainfo->qi_gquotaip;
543 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
544 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
545 0, 0, &uip, 0) == 0)
546 tempuqip = B_TRUE;
548 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
549 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
550 0, 0, &gip, 0) == 0)
551 tempgqip = B_TRUE;
553 if (uip) {
554 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
555 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
556 if (tempuqip)
557 VN_RELE(XFS_ITOV(uip));
559 if (gip) {
560 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
561 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
562 if (tempgqip)
563 VN_RELE(XFS_ITOV(gip));
565 if (mp->m_quotainfo) {
566 out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
567 out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
568 out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
569 out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
570 out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
571 out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
573 return (0);
577 * Adjust quota limits, and start/stop timers accordingly.
579 STATIC int
580 xfs_qm_scall_setqlim(
581 xfs_mount_t *mp,
582 xfs_dqid_t id,
583 uint type,
584 fs_disk_quota_t *newlim)
586 xfs_disk_dquot_t *ddq;
587 xfs_dquot_t *dqp;
588 xfs_trans_t *tp;
589 int error;
590 xfs_qcnt_t hard, soft;
592 if (!capable(CAP_SYS_ADMIN))
593 return XFS_ERROR(EPERM);
595 if ((newlim->d_fieldmask &
596 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
597 return (0);
599 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
600 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
601 0, 0, XFS_DEFAULT_LOG_COUNT))) {
602 xfs_trans_cancel(tp, 0);
603 return (error);
607 * We don't want to race with a quotaoff so take the quotaoff lock.
608 * (We don't hold an inode lock, so there's nothing else to stop
609 * a quotaoff from happening). (XXXThis doesn't currently happen
610 * because we take the vfslock before calling xfs_qm_sysent).
612 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
615 * Get the dquot (locked), and join it to the transaction.
616 * Allocate the dquot if this doesn't exist.
618 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
619 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
620 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
621 ASSERT(error != ENOENT);
622 return (error);
624 xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
625 xfs_trans_dqjoin(tp, dqp);
626 ddq = &dqp->q_core;
629 * Make sure that hardlimits are >= soft limits before changing.
631 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
632 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
633 be64_to_cpu(ddq->d_blk_hardlimit);
634 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
635 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
636 be64_to_cpu(ddq->d_blk_softlimit);
637 if (hard == 0 || hard >= soft) {
638 ddq->d_blk_hardlimit = cpu_to_be64(hard);
639 ddq->d_blk_softlimit = cpu_to_be64(soft);
640 if (id == 0) {
641 mp->m_quotainfo->qi_bhardlimit = hard;
642 mp->m_quotainfo->qi_bsoftlimit = soft;
644 } else {
645 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
647 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
648 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
649 be64_to_cpu(ddq->d_rtb_hardlimit);
650 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
651 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
652 be64_to_cpu(ddq->d_rtb_softlimit);
653 if (hard == 0 || hard >= soft) {
654 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
655 ddq->d_rtb_softlimit = cpu_to_be64(soft);
656 if (id == 0) {
657 mp->m_quotainfo->qi_rtbhardlimit = hard;
658 mp->m_quotainfo->qi_rtbsoftlimit = soft;
660 } else {
661 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
664 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
665 (xfs_qcnt_t) newlim->d_ino_hardlimit :
666 be64_to_cpu(ddq->d_ino_hardlimit);
667 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
668 (xfs_qcnt_t) newlim->d_ino_softlimit :
669 be64_to_cpu(ddq->d_ino_softlimit);
670 if (hard == 0 || hard >= soft) {
671 ddq->d_ino_hardlimit = cpu_to_be64(hard);
672 ddq->d_ino_softlimit = cpu_to_be64(soft);
673 if (id == 0) {
674 mp->m_quotainfo->qi_ihardlimit = hard;
675 mp->m_quotainfo->qi_isoftlimit = soft;
677 } else {
678 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
682 * Update warnings counter(s) if requested
684 if (newlim->d_fieldmask & FS_DQ_BWARNS)
685 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
686 if (newlim->d_fieldmask & FS_DQ_IWARNS)
687 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
688 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
689 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
691 if (id == 0) {
693 * Timelimits for the super user set the relative time
694 * the other users can be over quota for this file system.
695 * If it is zero a default is used. Ditto for the default
696 * soft and hard limit values (already done, above), and
697 * for warnings.
699 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
700 mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
701 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
703 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
704 mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
705 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
707 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
708 mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
709 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
711 if (newlim->d_fieldmask & FS_DQ_BWARNS)
712 mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
713 if (newlim->d_fieldmask & FS_DQ_IWARNS)
714 mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
715 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
716 mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
717 } else {
719 * If the user is now over quota, start the timelimit.
720 * The user will not be 'warned'.
721 * Note that we keep the timers ticking, whether enforcement
722 * is on or off. We don't really want to bother with iterating
723 * over all ondisk dquots and turning the timers on/off.
725 xfs_qm_adjust_dqtimers(mp, ddq);
727 dqp->dq_flags |= XFS_DQ_DIRTY;
728 xfs_trans_log_dquot(tp, dqp);
730 xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
731 xfs_trans_commit(tp, 0);
732 xfs_qm_dqprint(dqp);
733 xfs_qm_dqrele(dqp);
734 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
736 return (0);
739 STATIC int
740 xfs_qm_scall_getquota(
741 xfs_mount_t *mp,
742 xfs_dqid_t id,
743 uint type,
744 fs_disk_quota_t *out)
746 xfs_dquot_t *dqp;
747 int error;
750 * Try to get the dquot. We don't want it allocated on disk, so
751 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
752 * exist, we'll get ENOENT back.
754 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
755 return (error);
758 xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
760 * If everything's NULL, this dquot doesn't quite exist as far as
761 * our utility programs are concerned.
763 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
764 xfs_qm_dqput(dqp);
765 return XFS_ERROR(ENOENT);
767 /* xfs_qm_dqprint(dqp); */
769 * Convert the disk dquot to the exportable format
771 xfs_qm_export_dquot(mp, &dqp->q_core, out);
772 xfs_qm_dqput(dqp);
773 return (error ? XFS_ERROR(EFAULT) : 0);
777 STATIC int
778 xfs_qm_log_quotaoff_end(
779 xfs_mount_t *mp,
780 xfs_qoff_logitem_t *startqoff,
781 uint flags)
783 xfs_trans_t *tp;
784 int error;
785 xfs_qoff_logitem_t *qoffi;
787 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
789 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
790 0, 0, XFS_DEFAULT_LOG_COUNT))) {
791 xfs_trans_cancel(tp, 0);
792 return (error);
795 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
796 flags & XFS_ALL_QUOTA_ACCT);
797 xfs_trans_log_quotaoff_item(tp, qoffi);
800 * We have to make sure that the transaction is secure on disk before we
801 * return and actually stop quota accounting. So, make it synchronous.
802 * We don't care about quotoff's performance.
804 xfs_trans_set_sync(tp);
805 error = xfs_trans_commit(tp, 0);
806 return (error);
810 STATIC int
811 xfs_qm_log_quotaoff(
812 xfs_mount_t *mp,
813 xfs_qoff_logitem_t **qoffstartp,
814 uint flags)
816 xfs_trans_t *tp;
817 int error;
818 unsigned long s;
819 xfs_qoff_logitem_t *qoffi=NULL;
820 uint oldsbqflag=0;
822 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
823 if ((error = xfs_trans_reserve(tp, 0,
824 sizeof(xfs_qoff_logitem_t) * 2 +
825 mp->m_sb.sb_sectsize + 128,
828 XFS_DEFAULT_LOG_COUNT))) {
829 goto error0;
832 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
833 xfs_trans_log_quotaoff_item(tp, qoffi);
835 s = XFS_SB_LOCK(mp);
836 oldsbqflag = mp->m_sb.sb_qflags;
837 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
838 XFS_SB_UNLOCK(mp, s);
840 xfs_mod_sb(tp, XFS_SB_QFLAGS);
843 * We have to make sure that the transaction is secure on disk before we
844 * return and actually stop quota accounting. So, make it synchronous.
845 * We don't care about quotoff's performance.
847 xfs_trans_set_sync(tp);
848 error = xfs_trans_commit(tp, 0);
850 error0:
851 if (error) {
852 xfs_trans_cancel(tp, 0);
854 * No one else is modifying sb_qflags, so this is OK.
855 * We still hold the quotaofflock.
857 s = XFS_SB_LOCK(mp);
858 mp->m_sb.sb_qflags = oldsbqflag;
859 XFS_SB_UNLOCK(mp, s);
861 *qoffstartp = qoffi;
862 return (error);
867 * Translate an internal style on-disk-dquot to the exportable format.
868 * The main differences are that the counters/limits are all in Basic
869 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
870 * to be converted to the native endianness.
872 STATIC void
873 xfs_qm_export_dquot(
874 xfs_mount_t *mp,
875 xfs_disk_dquot_t *src,
876 struct fs_disk_quota *dst)
878 memset(dst, 0, sizeof(*dst));
879 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
880 dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
881 dst->d_id = be32_to_cpu(src->d_id);
882 dst->d_blk_hardlimit =
883 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
884 dst->d_blk_softlimit =
885 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
886 dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
887 dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
888 dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
889 dst->d_icount = be64_to_cpu(src->d_icount);
890 dst->d_btimer = be32_to_cpu(src->d_btimer);
891 dst->d_itimer = be32_to_cpu(src->d_itimer);
892 dst->d_iwarns = be16_to_cpu(src->d_iwarns);
893 dst->d_bwarns = be16_to_cpu(src->d_bwarns);
894 dst->d_rtb_hardlimit =
895 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
896 dst->d_rtb_softlimit =
897 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
898 dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
899 dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
900 dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
903 * Internally, we don't reset all the timers when quota enforcement
904 * gets turned off. No need to confuse the user level code,
905 * so return zeroes in that case.
907 if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
908 (!XFS_IS_OQUOTA_ENFORCED(mp) &&
909 (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
910 dst->d_btimer = 0;
911 dst->d_itimer = 0;
912 dst->d_rtbtimer = 0;
915 #ifdef DEBUG
916 if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
917 (XFS_IS_OQUOTA_ENFORCED(mp) &&
918 (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
919 dst->d_id != 0) {
920 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
921 (dst->d_blk_softlimit > 0)) {
922 ASSERT(dst->d_btimer != 0);
924 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
925 (dst->d_ino_softlimit > 0)) {
926 ASSERT(dst->d_itimer != 0);
929 #endif
932 STATIC uint
933 xfs_qm_import_qtype_flags(
934 uint uflags)
936 uint oflags = 0;
939 * Can't be more than one, or none.
941 if (((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ==
942 (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ||
943 ((uflags & (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ==
944 (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ||
945 ((uflags & (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ==
946 (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ||
947 ((uflags & (XFS_GROUP_QUOTA|XFS_USER_QUOTA|XFS_PROJ_QUOTA)) == 0))
948 return (0);
950 oflags |= (uflags & XFS_USER_QUOTA) ? XFS_DQ_USER : 0;
951 oflags |= (uflags & XFS_PROJ_QUOTA) ? XFS_DQ_PROJ : 0;
952 oflags |= (uflags & XFS_GROUP_QUOTA) ? XFS_DQ_GROUP: 0;
953 return oflags;
956 STATIC uint
957 xfs_qm_export_qtype_flags(
958 uint flags)
961 * Can't be more than one, or none.
963 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
964 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
965 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
966 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
967 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
968 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
969 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
971 return (flags & XFS_DQ_USER) ?
972 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
973 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
976 STATIC uint
977 xfs_qm_import_flags(
978 uint uflags)
980 uint flags = 0;
982 if (uflags & XFS_QUOTA_UDQ_ACCT)
983 flags |= XFS_UQUOTA_ACCT;
984 if (uflags & XFS_QUOTA_PDQ_ACCT)
985 flags |= XFS_PQUOTA_ACCT;
986 if (uflags & XFS_QUOTA_GDQ_ACCT)
987 flags |= XFS_GQUOTA_ACCT;
988 if (uflags & XFS_QUOTA_UDQ_ENFD)
989 flags |= XFS_UQUOTA_ENFD;
990 if (uflags & (XFS_QUOTA_PDQ_ENFD|XFS_QUOTA_GDQ_ENFD))
991 flags |= XFS_OQUOTA_ENFD;
992 return (flags);
996 STATIC uint
997 xfs_qm_export_flags(
998 uint flags)
1000 uint uflags;
1002 uflags = 0;
1003 if (flags & XFS_UQUOTA_ACCT)
1004 uflags |= XFS_QUOTA_UDQ_ACCT;
1005 if (flags & XFS_PQUOTA_ACCT)
1006 uflags |= XFS_QUOTA_PDQ_ACCT;
1007 if (flags & XFS_GQUOTA_ACCT)
1008 uflags |= XFS_QUOTA_GDQ_ACCT;
1009 if (flags & XFS_UQUOTA_ENFD)
1010 uflags |= XFS_QUOTA_UDQ_ENFD;
1011 if (flags & (XFS_OQUOTA_ENFD)) {
1012 uflags |= (flags & XFS_GQUOTA_ACCT) ?
1013 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
1015 return (uflags);
1020 * Go thru all the inodes in the file system, releasing their dquots.
1021 * Note that the mount structure gets modified to indicate that quotas are off
1022 * AFTER this, in the case of quotaoff. This also gets called from
1023 * xfs_rootumount.
1025 void
1026 xfs_qm_dqrele_all_inodes(
1027 struct xfs_mount *mp,
1028 uint flags)
1030 xfs_inode_t *ip, *topino;
1031 uint ireclaims;
1032 bhv_vnode_t *vp;
1033 boolean_t vnode_refd;
1035 ASSERT(mp->m_quotainfo);
1037 XFS_MOUNT_ILOCK(mp);
1038 again:
1039 ip = mp->m_inodes;
1040 if (ip == NULL) {
1041 XFS_MOUNT_IUNLOCK(mp);
1042 return;
1044 do {
1045 /* Skip markers inserted by xfs_sync */
1046 if (ip->i_mount == NULL) {
1047 ip = ip->i_mnext;
1048 continue;
1050 /* Root inode, rbmip and rsumip have associated blocks */
1051 if (ip == XFS_QI_UQIP(mp) || ip == XFS_QI_GQIP(mp)) {
1052 ASSERT(ip->i_udquot == NULL);
1053 ASSERT(ip->i_gdquot == NULL);
1054 ip = ip->i_mnext;
1055 continue;
1057 vp = XFS_ITOV_NULL(ip);
1058 if (!vp) {
1059 ASSERT(ip->i_udquot == NULL);
1060 ASSERT(ip->i_gdquot == NULL);
1061 ip = ip->i_mnext;
1062 continue;
1064 vnode_refd = B_FALSE;
1065 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1066 ireclaims = mp->m_ireclaims;
1067 topino = mp->m_inodes;
1068 vp = vn_grab(vp);
1069 if (!vp)
1070 goto again;
1072 XFS_MOUNT_IUNLOCK(mp);
1073 /* XXX restart limit ? */
1074 xfs_ilock(ip, XFS_ILOCK_EXCL);
1075 vnode_refd = B_TRUE;
1076 } else {
1077 ireclaims = mp->m_ireclaims;
1078 topino = mp->m_inodes;
1079 XFS_MOUNT_IUNLOCK(mp);
1083 * We don't keep the mountlock across the dqrele() call,
1084 * since it can take a while..
1086 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
1087 xfs_qm_dqrele(ip->i_udquot);
1088 ip->i_udquot = NULL;
1090 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
1091 xfs_qm_dqrele(ip->i_gdquot);
1092 ip->i_gdquot = NULL;
1094 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1096 * Wait until we've dropped the ilock and mountlock to
1097 * do the vn_rele. Or be condemned to an eternity in the
1098 * inactive code in hell.
1100 if (vnode_refd)
1101 VN_RELE(vp);
1102 XFS_MOUNT_ILOCK(mp);
1104 * If an inode was inserted or removed, we gotta
1105 * start over again.
1107 if (topino != mp->m_inodes || mp->m_ireclaims != ireclaims) {
1108 /* XXX use a sentinel */
1109 goto again;
1111 ip = ip->i_mnext;
1112 } while (ip != mp->m_inodes);
1114 XFS_MOUNT_IUNLOCK(mp);
1117 /*------------------------------------------------------------------------*/
1118 #ifdef DEBUG
1120 * This contains all the test functions for XFS disk quotas.
1121 * Currently it does a quota accounting check. ie. it walks through
1122 * all inodes in the file system, calculating the dquot accounting fields,
1123 * and prints out any inconsistencies.
1125 xfs_dqhash_t *qmtest_udqtab;
1126 xfs_dqhash_t *qmtest_gdqtab;
1127 int qmtest_hashmask;
1128 int qmtest_nfails;
1129 mutex_t qcheck_lock;
1131 #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
1132 (__psunsigned_t)(id)) & \
1133 (qmtest_hashmask - 1))
1135 #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
1136 (qmtest_udqtab + \
1137 DQTEST_HASHVAL(mp, id)) : \
1138 (qmtest_gdqtab + \
1139 DQTEST_HASHVAL(mp, id)))
1141 #define DQTEST_LIST_PRINT(l, NXT, title) \
1143 xfs_dqtest_t *dqp; int i = 0;\
1144 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
1145 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
1146 dqp = (xfs_dqtest_t *)dqp->NXT) { \
1147 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
1148 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
1149 dqp->d_bcount, dqp->d_icount); } \
1152 typedef struct dqtest {
1153 xfs_dqmarker_t q_lists;
1154 xfs_dqhash_t *q_hash; /* the hashchain header */
1155 xfs_mount_t *q_mount; /* filesystem this relates to */
1156 xfs_dqid_t d_id; /* user id or group id */
1157 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
1158 xfs_qcnt_t d_icount; /* # inodes owned by the user */
1159 } xfs_dqtest_t;
1161 STATIC void
1162 xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
1164 xfs_dquot_t *d;
1165 if (((d) = (h)->qh_next))
1166 (d)->HL_PREVP = &((dqp)->HL_NEXT);
1167 (dqp)->HL_NEXT = d;
1168 (dqp)->HL_PREVP = &((h)->qh_next);
1169 (h)->qh_next = (xfs_dquot_t *)dqp;
1170 (h)->qh_version++;
1171 (h)->qh_nelems++;
1173 STATIC void
1174 xfs_qm_dqtest_print(
1175 xfs_dqtest_t *d)
1177 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
1178 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
1179 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
1180 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1181 d->d_bcount, (int)d->d_bcount);
1182 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1183 d->d_icount, (int)d->d_icount);
1184 cmn_err(CE_DEBUG, "---------------------------");
1187 STATIC void
1188 xfs_qm_dqtest_failed(
1189 xfs_dqtest_t *d,
1190 xfs_dquot_t *dqp,
1191 char *reason,
1192 xfs_qcnt_t a,
1193 xfs_qcnt_t b,
1194 int error)
1196 qmtest_nfails++;
1197 if (error)
1198 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
1199 d->d_id, error, reason);
1200 else
1201 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
1202 d->d_id, reason, (int)a, (int)b);
1203 xfs_qm_dqtest_print(d);
1204 if (dqp)
1205 xfs_qm_dqprint(dqp);
1208 STATIC int
1209 xfs_dqtest_cmp2(
1210 xfs_dqtest_t *d,
1211 xfs_dquot_t *dqp)
1213 int err = 0;
1214 if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
1215 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
1216 be64_to_cpu(dqp->q_core.d_icount),
1217 d->d_icount, 0);
1218 err++;
1220 if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
1221 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
1222 be64_to_cpu(dqp->q_core.d_bcount),
1223 d->d_bcount, 0);
1224 err++;
1226 if (dqp->q_core.d_blk_softlimit &&
1227 be64_to_cpu(dqp->q_core.d_bcount) >=
1228 be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
1229 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1230 cmn_err(CE_DEBUG,
1231 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1232 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1233 err++;
1236 if (dqp->q_core.d_ino_softlimit &&
1237 be64_to_cpu(dqp->q_core.d_icount) >=
1238 be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
1239 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1240 cmn_err(CE_DEBUG,
1241 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1242 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1243 err++;
1246 #ifdef QUOTADEBUG
1247 if (!err) {
1248 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
1249 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1251 #endif
1252 return (err);
1255 STATIC void
1256 xfs_dqtest_cmp(
1257 xfs_dqtest_t *d)
1259 xfs_dquot_t *dqp;
1260 int error;
1262 /* xfs_qm_dqtest_print(d); */
1263 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1264 &dqp))) {
1265 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1266 return;
1268 xfs_dqtest_cmp2(d, dqp);
1269 xfs_qm_dqput(dqp);
1272 STATIC int
1273 xfs_qm_internalqcheck_dqget(
1274 xfs_mount_t *mp,
1275 xfs_dqid_t id,
1276 uint type,
1277 xfs_dqtest_t **O_dq)
1279 xfs_dqtest_t *d;
1280 xfs_dqhash_t *h;
1282 h = DQTEST_HASH(mp, id, type);
1283 for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
1284 d = (xfs_dqtest_t *) d->HL_NEXT) {
1285 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1286 if (d->d_id == id && mp == d->q_mount) {
1287 *O_dq = d;
1288 return (0);
1291 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1292 d->dq_flags = type;
1293 d->d_id = id;
1294 d->q_mount = mp;
1295 d->q_hash = h;
1296 xfs_qm_hashinsert(h, d);
1297 *O_dq = d;
1298 return (0);
1301 STATIC void
1302 xfs_qm_internalqcheck_get_dquots(
1303 xfs_mount_t *mp,
1304 xfs_dqid_t uid,
1305 xfs_dqid_t projid,
1306 xfs_dqid_t gid,
1307 xfs_dqtest_t **ud,
1308 xfs_dqtest_t **gd)
1310 if (XFS_IS_UQUOTA_ON(mp))
1311 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1312 if (XFS_IS_GQUOTA_ON(mp))
1313 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
1314 else if (XFS_IS_PQUOTA_ON(mp))
1315 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
1319 STATIC void
1320 xfs_qm_internalqcheck_dqadjust(
1321 xfs_inode_t *ip,
1322 xfs_dqtest_t *d)
1324 d->d_icount++;
1325 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1328 STATIC int
1329 xfs_qm_internalqcheck_adjust(
1330 xfs_mount_t *mp, /* mount point for filesystem */
1331 xfs_ino_t ino, /* inode number to get data for */
1332 void __user *buffer, /* not used */
1333 int ubsize, /* not used */
1334 void *private_data, /* not used */
1335 xfs_daddr_t bno, /* starting block of inode cluster */
1336 int *ubused, /* not used */
1337 void *dip, /* not used */
1338 int *res) /* bulkstat result code */
1340 xfs_inode_t *ip;
1341 xfs_dqtest_t *ud, *gd;
1342 uint lock_flags;
1343 boolean_t ipreleased;
1344 int error;
1346 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1348 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1349 *res = BULKSTAT_RV_NOTHING;
1350 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1351 (unsigned long long) ino,
1352 (unsigned long long) mp->m_sb.sb_uquotino,
1353 (unsigned long long) mp->m_sb.sb_gquotino);
1354 return XFS_ERROR(EINVAL);
1356 ipreleased = B_FALSE;
1357 again:
1358 lock_flags = XFS_ILOCK_SHARED;
1359 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1360 *res = BULKSTAT_RV_NOTHING;
1361 return (error);
1364 if (ip->i_d.di_mode == 0) {
1365 xfs_iput_new(ip, lock_flags);
1366 *res = BULKSTAT_RV_NOTHING;
1367 return XFS_ERROR(ENOENT);
1371 * This inode can have blocks after eof which can get released
1372 * when we send it to inactive. Since we don't check the dquot
1373 * until the after all our calculations are done, we must get rid
1374 * of those now.
1376 if (! ipreleased) {
1377 xfs_iput(ip, lock_flags);
1378 ipreleased = B_TRUE;
1379 goto again;
1381 xfs_qm_internalqcheck_get_dquots(mp,
1382 (xfs_dqid_t) ip->i_d.di_uid,
1383 (xfs_dqid_t) ip->i_d.di_projid,
1384 (xfs_dqid_t) ip->i_d.di_gid,
1385 &ud, &gd);
1386 if (XFS_IS_UQUOTA_ON(mp)) {
1387 ASSERT(ud);
1388 xfs_qm_internalqcheck_dqadjust(ip, ud);
1390 if (XFS_IS_OQUOTA_ON(mp)) {
1391 ASSERT(gd);
1392 xfs_qm_internalqcheck_dqadjust(ip, gd);
1394 xfs_iput(ip, lock_flags);
1395 *res = BULKSTAT_RV_DIDONE;
1396 return (0);
1400 /* PRIVATE, debugging */
1402 xfs_qm_internalqcheck(
1403 xfs_mount_t *mp)
1405 xfs_ino_t lastino;
1406 int done, count;
1407 int i;
1408 xfs_dqtest_t *d, *e;
1409 xfs_dqhash_t *h1;
1410 int error;
1412 lastino = 0;
1413 qmtest_hashmask = 32;
1414 count = 5;
1415 done = 0;
1416 qmtest_nfails = 0;
1418 if (! XFS_IS_QUOTA_ON(mp))
1419 return XFS_ERROR(ESRCH);
1421 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1422 XFS_bflush(mp->m_ddev_targp);
1423 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1424 XFS_bflush(mp->m_ddev_targp);
1426 mutex_lock(&qcheck_lock);
1427 /* There should be absolutely no quota activity while this
1428 is going on. */
1429 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1430 sizeof(xfs_dqhash_t), KM_SLEEP);
1431 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1432 sizeof(xfs_dqhash_t), KM_SLEEP);
1433 do {
1435 * Iterate thru all the inodes in the file system,
1436 * adjusting the corresponding dquot counters
1438 if ((error = xfs_bulkstat(mp, &lastino, &count,
1439 xfs_qm_internalqcheck_adjust, NULL,
1440 0, NULL, BULKSTAT_FG_IGET, &done))) {
1441 break;
1443 } while (! done);
1444 if (error) {
1445 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1447 cmn_err(CE_DEBUG, "Checking results against system dquots");
1448 for (i = 0; i < qmtest_hashmask; i++) {
1449 h1 = &qmtest_udqtab[i];
1450 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1451 xfs_dqtest_cmp(d);
1452 e = (xfs_dqtest_t *) d->HL_NEXT;
1453 kmem_free(d, sizeof(xfs_dqtest_t));
1454 d = e;
1456 h1 = &qmtest_gdqtab[i];
1457 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1458 xfs_dqtest_cmp(d);
1459 e = (xfs_dqtest_t *) d->HL_NEXT;
1460 kmem_free(d, sizeof(xfs_dqtest_t));
1461 d = e;
1465 if (qmtest_nfails) {
1466 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1467 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1468 } else {
1469 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1471 kmem_free(qmtest_udqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1472 kmem_free(qmtest_gdqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1473 mutex_unlock(&qcheck_lock);
1474 return (qmtest_nfails);
1477 #endif /* DEBUG */