2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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>
26 #include "xfs_trans.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"
44 #include "xfs_btree.h"
45 #include "xfs_rtalloc.h"
46 #include "xfs_error.h"
49 #include "xfs_buf_item.h"
50 #include "xfs_utils.h"
52 #include "xfs_trace.h"
55 # define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
57 # define qdprintk(s, args...) do { } while (0)
60 STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
61 STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
63 STATIC uint xfs_qm_export_flags(uint);
64 STATIC uint xfs_qm_export_qtype_flags(uint);
65 STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
70 * Turn off quota accounting and/or enforcement for all udquots and/or
71 * gdquots. Called only at unmount time.
73 * This assumes that there are no dquots of this file system cached
74 * incore, and modifies the ondisk dquot directly. Therefore, for example,
75 * it is an error to call this twice, without purging the cache.
78 xfs_qm_scall_quotaoff(
82 struct xfs_quotainfo *q = mp->m_quotainfo;
85 uint inactivate_flags;
86 xfs_qoff_logitem_t *qoffstart;
90 * No file system can have quotas enabled on disk but not in core.
91 * Note that quota utilities (like quotaoff) _expect_
92 * errno == EEXIST here.
94 if ((mp->m_qflags & flags) == 0)
95 return XFS_ERROR(EEXIST);
98 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
101 * We don't want to deal with two quotaoffs messing up each other,
102 * so we're going to serialize it. quotaoff isn't exactly a performance
104 * If quotaoff, then we must be dealing with the root filesystem.
107 mutex_lock(&q->qi_quotaofflock);
110 * If we're just turning off quota enforcement, change mp and go.
112 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
113 mp->m_qflags &= ~(flags);
115 spin_lock(&mp->m_sb_lock);
116 mp->m_sb.sb_qflags = mp->m_qflags;
117 spin_unlock(&mp->m_sb_lock);
118 mutex_unlock(&q->qi_quotaofflock);
120 /* XXX what to do if error ? Revert back to old vals incore ? */
121 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
126 inactivate_flags = 0;
128 * If accounting is off, we must turn enforcement off, clear the
129 * quota 'CHKD' certificate to make it known that we have to
130 * do a quotacheck the next time this quota is turned on.
132 if (flags & XFS_UQUOTA_ACCT) {
133 dqtype |= XFS_QMOPT_UQUOTA;
134 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
135 inactivate_flags |= XFS_UQUOTA_ACTIVE;
137 if (flags & XFS_GQUOTA_ACCT) {
138 dqtype |= XFS_QMOPT_GQUOTA;
139 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
140 inactivate_flags |= XFS_GQUOTA_ACTIVE;
141 } else if (flags & XFS_PQUOTA_ACCT) {
142 dqtype |= XFS_QMOPT_PQUOTA;
143 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
144 inactivate_flags |= XFS_PQUOTA_ACTIVE;
148 * Nothing to do? Don't complain. This happens when we're just
149 * turning off quota enforcement.
151 if ((mp->m_qflags & flags) == 0)
155 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
156 * and synchronously. If we fail to write, we should abort the
157 * operation as it cannot be recovered safely if we crash.
159 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
164 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
165 * to take care of the race between dqget and quotaoff. We don't take
166 * any special locks to reset these bits. All processes need to check
167 * these bits *after* taking inode lock(s) to see if the particular
168 * quota type is in the process of being turned off. If *ACTIVE, it is
169 * guaranteed that all dquot structures and all quotainode ptrs will all
170 * stay valid as long as that inode is kept locked.
172 * There is no turning back after this.
174 mp->m_qflags &= ~inactivate_flags;
177 * Give back all the dquot reference(s) held by inodes.
178 * Here we go thru every single incore inode in this file system, and
179 * do a dqrele on the i_udquot/i_gdquot that it may have.
180 * Essentially, as long as somebody has an inode locked, this guarantees
181 * that quotas will not be turned off. This is handy because in a
182 * transaction once we lock the inode(s) and check for quotaon, we can
183 * depend on the quota inodes (and other things) being valid as long as
184 * we keep the lock(s).
186 xfs_qm_dqrele_all_inodes(mp, flags);
189 * Next we make the changes in the quota flag in the mount struct.
190 * This isn't protected by a particular lock directly, because we
191 * don't want to take a mrlock everytime we depend on quotas being on.
193 mp->m_qflags &= ~(flags);
196 * Go through all the dquots of this file system and purge them,
197 * according to what was turned off. We may not be able to get rid
198 * of all dquots, because dquots can have temporary references that
199 * are not attached to inodes. eg. xfs_setattr, xfs_create.
200 * So, if we couldn't purge all the dquots from the filesystem,
201 * we can't get rid of the incore data structures.
203 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype)))
204 delay(10 * nculprits);
207 * Transactions that had started before ACTIVE state bit was cleared
208 * could have logged many dquots, so they'd have higher LSNs than
209 * the first QUOTAOFF log record does. If we happen to crash when
210 * the tail of the log has gone past the QUOTAOFF record, but
211 * before the last dquot modification, those dquots __will__
212 * recover, and that's not good.
214 * So, we have QUOTAOFF start and end logitems; the start
215 * logitem won't get overwritten until the end logitem appears...
217 error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
219 /* We're screwed now. Shutdown is the only option. */
220 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
225 * If quotas is completely disabled, close shop.
227 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
228 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
229 mutex_unlock(&q->qi_quotaofflock);
230 xfs_qm_destroy_quotainfo(mp);
235 * Release our quotainode references if we don't need them anymore.
237 if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
238 IRELE(q->qi_uquotaip);
239 q->qi_uquotaip = NULL;
241 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && q->qi_gquotaip) {
242 IRELE(q->qi_gquotaip);
243 q->qi_gquotaip = NULL;
247 mutex_unlock(&q->qi_quotaofflock);
252 xfs_qm_scall_trunc_qfiles(
256 int error = 0, error2 = 0;
259 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) {
260 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
261 return XFS_ERROR(EINVAL);
264 if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
265 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip);
267 error = xfs_truncate_file(mp, qip);
272 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
273 mp->m_sb.sb_gquotino != NULLFSINO) {
274 error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip);
276 error2 = xfs_truncate_file(mp, qip);
281 return error ? error : error2;
286 * Switch on (a given) quota enforcement for a filesystem. This takes
287 * effect immediately.
288 * (Switching on quota accounting must be done at mount time.)
291 xfs_qm_scall_quotaon(
300 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
302 * Switching on quota accounting must be done at mount time.
304 accflags = flags & XFS_ALL_QUOTA_ACCT;
305 flags &= ~(XFS_ALL_QUOTA_ACCT);
310 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
311 return XFS_ERROR(EINVAL);
314 /* No fs can turn on quotas with a delayed effect */
315 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
318 * Can't enforce without accounting. We check the superblock
319 * qflags here instead of m_qflags because rootfs can have
320 * quota acct on ondisk without m_qflags' knowing.
322 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
323 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
324 (flags & XFS_UQUOTA_ENFD))
326 ((flags & XFS_PQUOTA_ACCT) == 0 &&
327 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
328 (flags & XFS_GQUOTA_ACCT) == 0 &&
329 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
330 (flags & XFS_OQUOTA_ENFD))) {
331 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
332 flags, mp->m_sb.sb_qflags);
333 return XFS_ERROR(EINVAL);
336 * If everything's upto-date incore, then don't waste time.
338 if ((mp->m_qflags & flags) == flags)
339 return XFS_ERROR(EEXIST);
342 * Change sb_qflags on disk but not incore mp->qflags
343 * if this is the root filesystem.
345 spin_lock(&mp->m_sb_lock);
346 qf = mp->m_sb.sb_qflags;
347 mp->m_sb.sb_qflags = qf | flags;
348 spin_unlock(&mp->m_sb_lock);
351 * There's nothing to change if it's the same.
353 if ((qf & flags) == flags && sbflags == 0)
354 return XFS_ERROR(EEXIST);
355 sbflags |= XFS_SB_QFLAGS;
357 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
360 * If we aren't trying to switch on quota enforcement, we are done.
362 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
363 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
364 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
365 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
366 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
367 (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
368 (flags & XFS_ALL_QUOTA_ENFD) == 0)
371 if (! XFS_IS_QUOTA_RUNNING(mp))
372 return XFS_ERROR(ESRCH);
375 * Switch on quota enforcement in core.
377 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
378 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
379 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
386 * Return quota status information, such as uquota-off, enforcements, etc.
389 xfs_qm_scall_getqstat(
390 struct xfs_mount *mp,
391 struct fs_quota_stat *out)
393 struct xfs_quotainfo *q = mp->m_quotainfo;
394 struct xfs_inode *uip, *gip;
395 boolean_t tempuqip, tempgqip;
398 tempuqip = tempgqip = B_FALSE;
399 memset(out, 0, sizeof(fs_quota_stat_t));
401 out->qs_version = FS_QSTAT_VERSION;
402 if (!xfs_sb_version_hasquota(&mp->m_sb)) {
403 out->qs_uquota.qfs_ino = NULLFSINO;
404 out->qs_gquota.qfs_ino = NULLFSINO;
407 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
409 XFS_ALL_QUOTA_ENFD));
411 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
412 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
415 uip = q->qi_uquotaip;
416 gip = q->qi_gquotaip;
418 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
419 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
423 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
424 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
429 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
430 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
435 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
436 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
441 out->qs_incoredqs = q->qi_dquots;
442 out->qs_btimelimit = q->qi_btimelimit;
443 out->qs_itimelimit = q->qi_itimelimit;
444 out->qs_rtbtimelimit = q->qi_rtbtimelimit;
445 out->qs_bwarnlimit = q->qi_bwarnlimit;
446 out->qs_iwarnlimit = q->qi_iwarnlimit;
451 #define XFS_DQ_MASK \
452 (FS_DQ_LIMIT_MASK | FS_DQ_TIMER_MASK | FS_DQ_WARNS_MASK)
455 * Adjust quota limits, and start/stop timers accordingly.
458 xfs_qm_scall_setqlim(
462 fs_disk_quota_t *newlim)
464 struct xfs_quotainfo *q = mp->m_quotainfo;
465 xfs_disk_dquot_t *ddq;
469 xfs_qcnt_t hard, soft;
471 if (newlim->d_fieldmask & ~XFS_DQ_MASK)
473 if ((newlim->d_fieldmask & XFS_DQ_MASK) == 0)
476 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
477 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
478 0, 0, XFS_DEFAULT_LOG_COUNT))) {
479 xfs_trans_cancel(tp, 0);
484 * We don't want to race with a quotaoff so take the quotaoff lock.
485 * (We don't hold an inode lock, so there's nothing else to stop
486 * a quotaoff from happening). (XXXThis doesn't currently happen
487 * because we take the vfslock before calling xfs_qm_sysent).
489 mutex_lock(&q->qi_quotaofflock);
492 * Get the dquot (locked), and join it to the transaction.
493 * Allocate the dquot if this doesn't exist.
495 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
496 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
497 ASSERT(error != ENOENT);
500 xfs_trans_dqjoin(tp, dqp);
504 * Make sure that hardlimits are >= soft limits before changing.
506 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
507 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
508 be64_to_cpu(ddq->d_blk_hardlimit);
509 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
510 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
511 be64_to_cpu(ddq->d_blk_softlimit);
512 if (hard == 0 || hard >= soft) {
513 ddq->d_blk_hardlimit = cpu_to_be64(hard);
514 ddq->d_blk_softlimit = cpu_to_be64(soft);
516 q->qi_bhardlimit = hard;
517 q->qi_bsoftlimit = soft;
520 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
522 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
523 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
524 be64_to_cpu(ddq->d_rtb_hardlimit);
525 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
526 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
527 be64_to_cpu(ddq->d_rtb_softlimit);
528 if (hard == 0 || hard >= soft) {
529 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
530 ddq->d_rtb_softlimit = cpu_to_be64(soft);
532 q->qi_rtbhardlimit = hard;
533 q->qi_rtbsoftlimit = soft;
536 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
539 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
540 (xfs_qcnt_t) newlim->d_ino_hardlimit :
541 be64_to_cpu(ddq->d_ino_hardlimit);
542 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
543 (xfs_qcnt_t) newlim->d_ino_softlimit :
544 be64_to_cpu(ddq->d_ino_softlimit);
545 if (hard == 0 || hard >= soft) {
546 ddq->d_ino_hardlimit = cpu_to_be64(hard);
547 ddq->d_ino_softlimit = cpu_to_be64(soft);
549 q->qi_ihardlimit = hard;
550 q->qi_isoftlimit = soft;
553 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
557 * Update warnings counter(s) if requested
559 if (newlim->d_fieldmask & FS_DQ_BWARNS)
560 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
561 if (newlim->d_fieldmask & FS_DQ_IWARNS)
562 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
563 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
564 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
568 * Timelimits for the super user set the relative time
569 * the other users can be over quota for this file system.
570 * If it is zero a default is used. Ditto for the default
571 * soft and hard limit values (already done, above), and
574 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
575 q->qi_btimelimit = newlim->d_btimer;
576 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
578 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
579 q->qi_itimelimit = newlim->d_itimer;
580 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
582 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
583 q->qi_rtbtimelimit = newlim->d_rtbtimer;
584 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
586 if (newlim->d_fieldmask & FS_DQ_BWARNS)
587 q->qi_bwarnlimit = newlim->d_bwarns;
588 if (newlim->d_fieldmask & FS_DQ_IWARNS)
589 q->qi_iwarnlimit = newlim->d_iwarns;
590 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
591 q->qi_rtbwarnlimit = newlim->d_rtbwarns;
594 * If the user is now over quota, start the timelimit.
595 * The user will not be 'warned'.
596 * Note that we keep the timers ticking, whether enforcement
597 * is on or off. We don't really want to bother with iterating
598 * over all ondisk dquots and turning the timers on/off.
600 xfs_qm_adjust_dqtimers(mp, ddq);
602 dqp->dq_flags |= XFS_DQ_DIRTY;
603 xfs_trans_log_dquot(tp, dqp);
605 error = xfs_trans_commit(tp, 0);
610 mutex_unlock(&q->qi_quotaofflock);
615 xfs_qm_scall_getquota(
619 fs_disk_quota_t *out)
625 * Try to get the dquot. We don't want it allocated on disk, so
626 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
627 * exist, we'll get ENOENT back.
629 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
634 * If everything's NULL, this dquot doesn't quite exist as far as
635 * our utility programs are concerned.
637 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
639 return XFS_ERROR(ENOENT);
641 /* xfs_qm_dqprint(dqp); */
643 * Convert the disk dquot to the exportable format
645 xfs_qm_export_dquot(mp, &dqp->q_core, out);
647 return (error ? XFS_ERROR(EFAULT) : 0);
652 xfs_qm_log_quotaoff_end(
654 xfs_qoff_logitem_t *startqoff,
659 xfs_qoff_logitem_t *qoffi;
661 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
663 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
664 0, 0, XFS_DEFAULT_LOG_COUNT))) {
665 xfs_trans_cancel(tp, 0);
669 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
670 flags & XFS_ALL_QUOTA_ACCT);
671 xfs_trans_log_quotaoff_item(tp, qoffi);
674 * We have to make sure that the transaction is secure on disk before we
675 * return and actually stop quota accounting. So, make it synchronous.
676 * We don't care about quotoff's performance.
678 xfs_trans_set_sync(tp);
679 error = xfs_trans_commit(tp, 0);
687 xfs_qoff_logitem_t **qoffstartp,
692 xfs_qoff_logitem_t *qoffi=NULL;
695 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
696 if ((error = xfs_trans_reserve(tp, 0,
697 sizeof(xfs_qoff_logitem_t) * 2 +
698 mp->m_sb.sb_sectsize + 128,
701 XFS_DEFAULT_LOG_COUNT))) {
705 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
706 xfs_trans_log_quotaoff_item(tp, qoffi);
708 spin_lock(&mp->m_sb_lock);
709 oldsbqflag = mp->m_sb.sb_qflags;
710 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
711 spin_unlock(&mp->m_sb_lock);
713 xfs_mod_sb(tp, XFS_SB_QFLAGS);
716 * We have to make sure that the transaction is secure on disk before we
717 * return and actually stop quota accounting. So, make it synchronous.
718 * We don't care about quotoff's performance.
720 xfs_trans_set_sync(tp);
721 error = xfs_trans_commit(tp, 0);
725 xfs_trans_cancel(tp, 0);
727 * No one else is modifying sb_qflags, so this is OK.
728 * We still hold the quotaofflock.
730 spin_lock(&mp->m_sb_lock);
731 mp->m_sb.sb_qflags = oldsbqflag;
732 spin_unlock(&mp->m_sb_lock);
740 * Translate an internal style on-disk-dquot to the exportable format.
741 * The main differences are that the counters/limits are all in Basic
742 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
743 * to be converted to the native endianness.
748 xfs_disk_dquot_t *src,
749 struct fs_disk_quota *dst)
751 memset(dst, 0, sizeof(*dst));
752 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
753 dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
754 dst->d_id = be32_to_cpu(src->d_id);
755 dst->d_blk_hardlimit =
756 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
757 dst->d_blk_softlimit =
758 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
759 dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
760 dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
761 dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
762 dst->d_icount = be64_to_cpu(src->d_icount);
763 dst->d_btimer = be32_to_cpu(src->d_btimer);
764 dst->d_itimer = be32_to_cpu(src->d_itimer);
765 dst->d_iwarns = be16_to_cpu(src->d_iwarns);
766 dst->d_bwarns = be16_to_cpu(src->d_bwarns);
767 dst->d_rtb_hardlimit =
768 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
769 dst->d_rtb_softlimit =
770 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
771 dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
772 dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
773 dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
776 * Internally, we don't reset all the timers when quota enforcement
777 * gets turned off. No need to confuse the user level code,
778 * so return zeroes in that case.
780 if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
781 (!XFS_IS_OQUOTA_ENFORCED(mp) &&
782 (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
789 if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
790 (XFS_IS_OQUOTA_ENFORCED(mp) &&
791 (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
793 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
794 (dst->d_blk_softlimit > 0)) {
795 ASSERT(dst->d_btimer != 0);
797 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
798 (dst->d_ino_softlimit > 0)) {
799 ASSERT(dst->d_itimer != 0);
806 xfs_qm_export_qtype_flags(
810 * Can't be more than one, or none.
812 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
813 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
814 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
815 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
816 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
817 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
818 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
820 return (flags & XFS_DQ_USER) ?
821 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
822 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
832 if (flags & XFS_UQUOTA_ACCT)
833 uflags |= XFS_QUOTA_UDQ_ACCT;
834 if (flags & XFS_PQUOTA_ACCT)
835 uflags |= XFS_QUOTA_PDQ_ACCT;
836 if (flags & XFS_GQUOTA_ACCT)
837 uflags |= XFS_QUOTA_GDQ_ACCT;
838 if (flags & XFS_UQUOTA_ENFD)
839 uflags |= XFS_QUOTA_UDQ_ENFD;
840 if (flags & (XFS_OQUOTA_ENFD)) {
841 uflags |= (flags & XFS_GQUOTA_ACCT) ?
842 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
850 struct xfs_inode *ip,
851 struct xfs_perag *pag,
856 /* skip quota inodes */
857 if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
858 ip == ip->i_mount->m_quotainfo->qi_gquotaip) {
859 ASSERT(ip->i_udquot == NULL);
860 ASSERT(ip->i_gdquot == NULL);
861 read_unlock(&pag->pag_ici_lock);
865 error = xfs_sync_inode_valid(ip, pag);
869 xfs_ilock(ip, XFS_ILOCK_EXCL);
870 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
871 xfs_qm_dqrele(ip->i_udquot);
874 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
875 xfs_qm_dqrele(ip->i_gdquot);
878 xfs_iput(ip, XFS_ILOCK_EXCL);
885 * Go thru all the inodes in the file system, releasing their dquots.
887 * Note that the mount structure gets modified to indicate that quotas are off
888 * AFTER this, in the case of quotaoff.
891 xfs_qm_dqrele_all_inodes(
892 struct xfs_mount *mp,
895 ASSERT(mp->m_quotainfo);
896 xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags,
897 XFS_ICI_NO_TAG, 0, NULL);
900 /*------------------------------------------------------------------------*/
903 * This contains all the test functions for XFS disk quotas.
904 * Currently it does a quota accounting check. ie. it walks through
905 * all inodes in the file system, calculating the dquot accounting fields,
906 * and prints out any inconsistencies.
908 xfs_dqhash_t *qmtest_udqtab;
909 xfs_dqhash_t *qmtest_gdqtab;
912 struct mutex qcheck_lock;
914 #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
915 (__psunsigned_t)(id)) & \
916 (qmtest_hashmask - 1))
918 #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
920 DQTEST_HASHVAL(mp, id)) : \
922 DQTEST_HASHVAL(mp, id)))
924 #define DQTEST_LIST_PRINT(l, NXT, title) \
926 xfs_dqtest_t *dqp; int i = 0;\
927 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
928 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
929 dqp = (xfs_dqtest_t *)dqp->NXT) { \
930 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
931 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
932 dqp->d_bcount, dqp->d_icount); } \
935 typedef struct dqtest {
936 uint dq_flags; /* various flags (XFS_DQ_*) */
937 struct list_head q_hashlist;
938 xfs_dqhash_t *q_hash; /* the hashchain header */
939 xfs_mount_t *q_mount; /* filesystem this relates to */
940 xfs_dqid_t d_id; /* user id or group id */
941 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
942 xfs_qcnt_t d_icount; /* # inodes owned by the user */
946 xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
948 list_add(&dqp->q_hashlist, &h->qh_list);
956 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
957 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
958 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
959 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
960 d->d_bcount, (int)d->d_bcount);
961 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
962 d->d_icount, (int)d->d_icount);
963 cmn_err(CE_DEBUG, "---------------------------");
967 xfs_qm_dqtest_failed(
977 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
978 d->d_id, error, reason);
980 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
981 d->d_id, reason, (int)a, (int)b);
982 xfs_qm_dqtest_print(d);
993 if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
994 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
995 be64_to_cpu(dqp->q_core.d_icount),
999 if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
1000 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
1001 be64_to_cpu(dqp->q_core.d_bcount),
1005 if (dqp->q_core.d_blk_softlimit &&
1006 be64_to_cpu(dqp->q_core.d_bcount) >=
1007 be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
1008 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1010 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1011 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1015 if (dqp->q_core.d_ino_softlimit &&
1016 be64_to_cpu(dqp->q_core.d_icount) >=
1017 be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
1018 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1020 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1021 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1027 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
1028 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1041 /* xfs_qm_dqtest_print(d); */
1042 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1044 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1047 xfs_dqtest_cmp2(d, dqp);
1052 xfs_qm_internalqcheck_dqget(
1056 xfs_dqtest_t **O_dq)
1061 h = DQTEST_HASH(mp, id, type);
1062 list_for_each_entry(d, &h->qh_list, q_hashlist) {
1063 if (d->d_id == id && mp == d->q_mount) {
1068 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1073 INIT_LIST_HEAD(&d->q_hashlist);
1074 xfs_qm_hashinsert(h, d);
1080 xfs_qm_internalqcheck_get_dquots(
1088 if (XFS_IS_UQUOTA_ON(mp))
1089 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1090 if (XFS_IS_GQUOTA_ON(mp))
1091 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
1092 else if (XFS_IS_PQUOTA_ON(mp))
1093 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
1098 xfs_qm_internalqcheck_dqadjust(
1103 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1107 xfs_qm_internalqcheck_adjust(
1108 xfs_mount_t *mp, /* mount point for filesystem */
1109 xfs_ino_t ino, /* inode number to get data for */
1110 void __user *buffer, /* not used */
1111 int ubsize, /* not used */
1112 int *ubused, /* not used */
1113 int *res) /* bulkstat result code */
1116 xfs_dqtest_t *ud, *gd;
1118 boolean_t ipreleased;
1121 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1123 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1124 *res = BULKSTAT_RV_NOTHING;
1125 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1126 (unsigned long long) ino,
1127 (unsigned long long) mp->m_sb.sb_uquotino,
1128 (unsigned long long) mp->m_sb.sb_gquotino);
1129 return XFS_ERROR(EINVAL);
1131 ipreleased = B_FALSE;
1133 lock_flags = XFS_ILOCK_SHARED;
1134 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip))) {
1135 *res = BULKSTAT_RV_NOTHING;
1140 * This inode can have blocks after eof which can get released
1141 * when we send it to inactive. Since we don't check the dquot
1142 * until the after all our calculations are done, we must get rid
1146 xfs_iput(ip, lock_flags);
1147 ipreleased = B_TRUE;
1150 xfs_qm_internalqcheck_get_dquots(mp,
1151 (xfs_dqid_t) ip->i_d.di_uid,
1152 (xfs_dqid_t) ip->i_d.di_projid,
1153 (xfs_dqid_t) ip->i_d.di_gid,
1155 if (XFS_IS_UQUOTA_ON(mp)) {
1157 xfs_qm_internalqcheck_dqadjust(ip, ud);
1159 if (XFS_IS_OQUOTA_ON(mp)) {
1161 xfs_qm_internalqcheck_dqadjust(ip, gd);
1163 xfs_iput(ip, lock_flags);
1164 *res = BULKSTAT_RV_DIDONE;
1169 /* PRIVATE, debugging */
1171 xfs_qm_internalqcheck(
1180 qmtest_hashmask = 32;
1185 if (! XFS_IS_QUOTA_ON(mp))
1186 return XFS_ERROR(ESRCH);
1188 xfs_log_force(mp, XFS_LOG_SYNC);
1189 XFS_bflush(mp->m_ddev_targp);
1190 xfs_log_force(mp, XFS_LOG_SYNC);
1191 XFS_bflush(mp->m_ddev_targp);
1193 mutex_lock(&qcheck_lock);
1194 /* There should be absolutely no quota activity while this
1196 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1197 sizeof(xfs_dqhash_t), KM_SLEEP);
1198 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1199 sizeof(xfs_dqhash_t), KM_SLEEP);
1202 * Iterate thru all the inodes in the file system,
1203 * adjusting the corresponding dquot counters
1205 error = xfs_bulkstat(mp, &lastino, &count,
1206 xfs_qm_internalqcheck_adjust,
1209 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1214 cmn_err(CE_DEBUG, "Checking results against system dquots");
1215 for (i = 0; i < qmtest_hashmask; i++) {
1216 xfs_dqtest_t *d, *n;
1219 h = &qmtest_udqtab[i];
1220 list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) {
1224 h = &qmtest_gdqtab[i];
1225 list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) {
1231 if (qmtest_nfails) {
1232 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1233 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1235 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1237 kmem_free(qmtest_udqtab);
1238 kmem_free(qmtest_gdqtab);
1239 mutex_unlock(&qcheck_lock);
1240 return (qmtest_nfails);