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(
84 uint inactivate_flags
;
85 xfs_qoff_logitem_t
*qoffstart
;
89 * No file system can have quotas enabled on disk but not in core.
90 * Note that quota utilities (like quotaoff) _expect_
91 * errno == EEXIST here.
93 if ((mp
->m_qflags
& flags
) == 0)
94 return XFS_ERROR(EEXIST
);
97 flags
&= (XFS_ALL_QUOTA_ACCT
| XFS_ALL_QUOTA_ENFD
);
100 * We don't want to deal with two quotaoffs messing up each other,
101 * so we're going to serialize it. quotaoff isn't exactly a performance
103 * If quotaoff, then we must be dealing with the root filesystem.
105 ASSERT(mp
->m_quotainfo
);
107 mutex_lock(&(XFS_QI_QOFFLOCK(mp
)));
109 ASSERT(mp
->m_quotainfo
);
112 * If we're just turning off quota enforcement, change mp and go.
114 if ((flags
& XFS_ALL_QUOTA_ACCT
) == 0) {
115 mp
->m_qflags
&= ~(flags
);
117 spin_lock(&mp
->m_sb_lock
);
118 mp
->m_sb
.sb_qflags
= mp
->m_qflags
;
119 spin_unlock(&mp
->m_sb_lock
);
120 mutex_unlock(&(XFS_QI_QOFFLOCK(mp
)));
122 /* XXX what to do if error ? Revert back to old vals incore ? */
123 error
= xfs_qm_write_sb_changes(mp
, XFS_SB_QFLAGS
);
128 inactivate_flags
= 0;
130 * If accounting is off, we must turn enforcement off, clear the
131 * quota 'CHKD' certificate to make it known that we have to
132 * do a quotacheck the next time this quota is turned on.
134 if (flags
& XFS_UQUOTA_ACCT
) {
135 dqtype
|= XFS_QMOPT_UQUOTA
;
136 flags
|= (XFS_UQUOTA_CHKD
| XFS_UQUOTA_ENFD
);
137 inactivate_flags
|= XFS_UQUOTA_ACTIVE
;
139 if (flags
& XFS_GQUOTA_ACCT
) {
140 dqtype
|= XFS_QMOPT_GQUOTA
;
141 flags
|= (XFS_OQUOTA_CHKD
| XFS_OQUOTA_ENFD
);
142 inactivate_flags
|= XFS_GQUOTA_ACTIVE
;
143 } else if (flags
& XFS_PQUOTA_ACCT
) {
144 dqtype
|= XFS_QMOPT_PQUOTA
;
145 flags
|= (XFS_OQUOTA_CHKD
| XFS_OQUOTA_ENFD
);
146 inactivate_flags
|= XFS_PQUOTA_ACTIVE
;
150 * Nothing to do? Don't complain. This happens when we're just
151 * turning off quota enforcement.
153 if ((mp
->m_qflags
& flags
) == 0) {
154 mutex_unlock(&(XFS_QI_QOFFLOCK(mp
)));
159 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
160 * and synchronously. If we fail to write, we should abort the
161 * operation as it cannot be recovered safely if we crash.
163 error
= xfs_qm_log_quotaoff(mp
, &qoffstart
, flags
);
168 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
169 * to take care of the race between dqget and quotaoff. We don't take
170 * any special locks to reset these bits. All processes need to check
171 * these bits *after* taking inode lock(s) to see if the particular
172 * quota type is in the process of being turned off. If *ACTIVE, it is
173 * guaranteed that all dquot structures and all quotainode ptrs will all
174 * stay valid as long as that inode is kept locked.
176 * There is no turning back after this.
178 mp
->m_qflags
&= ~inactivate_flags
;
181 * Give back all the dquot reference(s) held by inodes.
182 * Here we go thru every single incore inode in this file system, and
183 * do a dqrele on the i_udquot/i_gdquot that it may have.
184 * Essentially, as long as somebody has an inode locked, this guarantees
185 * that quotas will not be turned off. This is handy because in a
186 * transaction once we lock the inode(s) and check for quotaon, we can
187 * depend on the quota inodes (and other things) being valid as long as
188 * we keep the lock(s).
190 xfs_qm_dqrele_all_inodes(mp
, flags
);
193 * Next we make the changes in the quota flag in the mount struct.
194 * This isn't protected by a particular lock directly, because we
195 * don't want to take a mrlock everytime we depend on quotas being on.
197 mp
->m_qflags
&= ~(flags
);
200 * Go through all the dquots of this file system and purge them,
201 * according to what was turned off. We may not be able to get rid
202 * of all dquots, because dquots can have temporary references that
203 * are not attached to inodes. eg. xfs_setattr, xfs_create.
204 * So, if we couldn't purge all the dquots from the filesystem,
205 * we can't get rid of the incore data structures.
207 while ((nculprits
= xfs_qm_dqpurge_all(mp
, dqtype
|XFS_QMOPT_QUOTAOFF
)))
208 delay(10 * nculprits
);
211 * Transactions that had started before ACTIVE state bit was cleared
212 * could have logged many dquots, so they'd have higher LSNs than
213 * the first QUOTAOFF log record does. If we happen to crash when
214 * the tail of the log has gone past the QUOTAOFF record, but
215 * before the last dquot modification, those dquots __will__
216 * recover, and that's not good.
218 * So, we have QUOTAOFF start and end logitems; the start
219 * logitem won't get overwritten until the end logitem appears...
221 error
= xfs_qm_log_quotaoff_end(mp
, qoffstart
, flags
);
223 /* We're screwed now. Shutdown is the only option. */
224 xfs_force_shutdown(mp
, SHUTDOWN_CORRUPT_INCORE
);
229 * If quotas is completely disabled, close shop.
231 if (((flags
& XFS_MOUNT_QUOTA_ALL
) == XFS_MOUNT_QUOTA_SET1
) ||
232 ((flags
& XFS_MOUNT_QUOTA_ALL
) == XFS_MOUNT_QUOTA_SET2
)) {
233 mutex_unlock(&(XFS_QI_QOFFLOCK(mp
)));
234 xfs_qm_destroy_quotainfo(mp
);
239 * Release our quotainode references, and vn_purge them,
240 * if we don't need them anymore.
242 if ((dqtype
& XFS_QMOPT_UQUOTA
) && XFS_QI_UQIP(mp
)) {
243 IRELE(XFS_QI_UQIP(mp
));
244 XFS_QI_UQIP(mp
) = NULL
;
246 if ((dqtype
& (XFS_QMOPT_GQUOTA
|XFS_QMOPT_PQUOTA
)) && XFS_QI_GQIP(mp
)) {
247 IRELE(XFS_QI_GQIP(mp
));
248 XFS_QI_GQIP(mp
) = NULL
;
251 mutex_unlock(&(XFS_QI_QOFFLOCK(mp
)));
257 xfs_qm_scall_trunc_qfiles(
261 int error
= 0, error2
= 0;
264 if (!xfs_sb_version_hasquota(&mp
->m_sb
) || flags
== 0) {
265 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags
, mp
->m_qflags
);
266 return XFS_ERROR(EINVAL
);
269 if ((flags
& XFS_DQ_USER
) && mp
->m_sb
.sb_uquotino
!= NULLFSINO
) {
270 error
= xfs_iget(mp
, NULL
, mp
->m_sb
.sb_uquotino
, 0, 0, &qip
, 0);
272 error
= xfs_truncate_file(mp
, qip
);
277 if ((flags
& (XFS_DQ_GROUP
|XFS_DQ_PROJ
)) &&
278 mp
->m_sb
.sb_gquotino
!= NULLFSINO
) {
279 error2
= xfs_iget(mp
, NULL
, mp
->m_sb
.sb_gquotino
, 0, 0, &qip
, 0);
281 error2
= xfs_truncate_file(mp
, qip
);
286 return error
? error
: error2
;
291 * Switch on (a given) quota enforcement for a filesystem. This takes
292 * effect immediately.
293 * (Switching on quota accounting must be done at mount time.)
296 xfs_qm_scall_quotaon(
305 flags
&= (XFS_ALL_QUOTA_ACCT
| XFS_ALL_QUOTA_ENFD
);
307 * Switching on quota accounting must be done at mount time.
309 accflags
= flags
& XFS_ALL_QUOTA_ACCT
;
310 flags
&= ~(XFS_ALL_QUOTA_ACCT
);
315 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp
->m_qflags
);
316 return XFS_ERROR(EINVAL
);
319 /* No fs can turn on quotas with a delayed effect */
320 ASSERT((flags
& XFS_ALL_QUOTA_ACCT
) == 0);
323 * Can't enforce without accounting. We check the superblock
324 * qflags here instead of m_qflags because rootfs can have
325 * quota acct on ondisk without m_qflags' knowing.
327 if (((flags
& XFS_UQUOTA_ACCT
) == 0 &&
328 (mp
->m_sb
.sb_qflags
& XFS_UQUOTA_ACCT
) == 0 &&
329 (flags
& XFS_UQUOTA_ENFD
))
331 ((flags
& XFS_PQUOTA_ACCT
) == 0 &&
332 (mp
->m_sb
.sb_qflags
& XFS_PQUOTA_ACCT
) == 0 &&
333 (flags
& XFS_GQUOTA_ACCT
) == 0 &&
334 (mp
->m_sb
.sb_qflags
& XFS_GQUOTA_ACCT
) == 0 &&
335 (flags
& XFS_OQUOTA_ENFD
))) {
336 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
337 flags
, mp
->m_sb
.sb_qflags
);
338 return XFS_ERROR(EINVAL
);
341 * If everything's upto-date incore, then don't waste time.
343 if ((mp
->m_qflags
& flags
) == flags
)
344 return XFS_ERROR(EEXIST
);
347 * Change sb_qflags on disk but not incore mp->qflags
348 * if this is the root filesystem.
350 spin_lock(&mp
->m_sb_lock
);
351 qf
= mp
->m_sb
.sb_qflags
;
352 mp
->m_sb
.sb_qflags
= qf
| flags
;
353 spin_unlock(&mp
->m_sb_lock
);
356 * There's nothing to change if it's the same.
358 if ((qf
& flags
) == flags
&& sbflags
== 0)
359 return XFS_ERROR(EEXIST
);
360 sbflags
|= XFS_SB_QFLAGS
;
362 if ((error
= xfs_qm_write_sb_changes(mp
, sbflags
)))
365 * If we aren't trying to switch on quota enforcement, we are done.
367 if (((mp
->m_sb
.sb_qflags
& XFS_UQUOTA_ACCT
) !=
368 (mp
->m_qflags
& XFS_UQUOTA_ACCT
)) ||
369 ((mp
->m_sb
.sb_qflags
& XFS_PQUOTA_ACCT
) !=
370 (mp
->m_qflags
& XFS_PQUOTA_ACCT
)) ||
371 ((mp
->m_sb
.sb_qflags
& XFS_GQUOTA_ACCT
) !=
372 (mp
->m_qflags
& XFS_GQUOTA_ACCT
)) ||
373 (flags
& XFS_ALL_QUOTA_ENFD
) == 0)
376 if (! XFS_IS_QUOTA_RUNNING(mp
))
377 return XFS_ERROR(ESRCH
);
380 * Switch on quota enforcement in core.
382 mutex_lock(&(XFS_QI_QOFFLOCK(mp
)));
383 mp
->m_qflags
|= (flags
& XFS_ALL_QUOTA_ENFD
);
384 mutex_unlock(&(XFS_QI_QOFFLOCK(mp
)));
391 * Return quota status information, such as uquota-off, enforcements, etc.
394 xfs_qm_scall_getqstat(
396 fs_quota_stat_t
*out
)
398 xfs_inode_t
*uip
, *gip
;
399 boolean_t tempuqip
, tempgqip
;
402 tempuqip
= tempgqip
= B_FALSE
;
403 memset(out
, 0, sizeof(fs_quota_stat_t
));
405 out
->qs_version
= FS_QSTAT_VERSION
;
406 if (!xfs_sb_version_hasquota(&mp
->m_sb
)) {
407 out
->qs_uquota
.qfs_ino
= NULLFSINO
;
408 out
->qs_gquota
.qfs_ino
= NULLFSINO
;
411 out
->qs_flags
= (__uint16_t
) xfs_qm_export_flags(mp
->m_qflags
&
413 XFS_ALL_QUOTA_ENFD
));
415 out
->qs_uquota
.qfs_ino
= mp
->m_sb
.sb_uquotino
;
416 out
->qs_gquota
.qfs_ino
= mp
->m_sb
.sb_gquotino
;
418 if (mp
->m_quotainfo
) {
419 uip
= mp
->m_quotainfo
->qi_uquotaip
;
420 gip
= mp
->m_quotainfo
->qi_gquotaip
;
422 if (!uip
&& mp
->m_sb
.sb_uquotino
!= NULLFSINO
) {
423 if (xfs_iget(mp
, NULL
, mp
->m_sb
.sb_uquotino
,
427 if (!gip
&& mp
->m_sb
.sb_gquotino
!= NULLFSINO
) {
428 if (xfs_iget(mp
, NULL
, mp
->m_sb
.sb_gquotino
,
433 out
->qs_uquota
.qfs_nblks
= uip
->i_d
.di_nblocks
;
434 out
->qs_uquota
.qfs_nextents
= uip
->i_d
.di_nextents
;
439 out
->qs_gquota
.qfs_nblks
= gip
->i_d
.di_nblocks
;
440 out
->qs_gquota
.qfs_nextents
= gip
->i_d
.di_nextents
;
444 if (mp
->m_quotainfo
) {
445 out
->qs_incoredqs
= XFS_QI_MPLNDQUOTS(mp
);
446 out
->qs_btimelimit
= XFS_QI_BTIMELIMIT(mp
);
447 out
->qs_itimelimit
= XFS_QI_ITIMELIMIT(mp
);
448 out
->qs_rtbtimelimit
= XFS_QI_RTBTIMELIMIT(mp
);
449 out
->qs_bwarnlimit
= XFS_QI_BWARNLIMIT(mp
);
450 out
->qs_iwarnlimit
= XFS_QI_IWARNLIMIT(mp
);
456 * Adjust quota limits, and start/stop timers accordingly.
459 xfs_qm_scall_setqlim(
463 fs_disk_quota_t
*newlim
)
465 xfs_disk_dquot_t
*ddq
;
469 xfs_qcnt_t hard
, soft
;
471 if ((newlim
->d_fieldmask
&
472 (FS_DQ_LIMIT_MASK
|FS_DQ_TIMER_MASK
|FS_DQ_WARNS_MASK
)) == 0)
475 tp
= xfs_trans_alloc(mp
, XFS_TRANS_QM_SETQLIM
);
476 if ((error
= xfs_trans_reserve(tp
, 0, sizeof(xfs_disk_dquot_t
) + 128,
477 0, 0, XFS_DEFAULT_LOG_COUNT
))) {
478 xfs_trans_cancel(tp
, 0);
483 * We don't want to race with a quotaoff so take the quotaoff lock.
484 * (We don't hold an inode lock, so there's nothing else to stop
485 * a quotaoff from happening). (XXXThis doesn't currently happen
486 * because we take the vfslock before calling xfs_qm_sysent).
488 mutex_lock(&(XFS_QI_QOFFLOCK(mp
)));
491 * Get the dquot (locked), and join it to the transaction.
492 * Allocate the dquot if this doesn't exist.
494 if ((error
= xfs_qm_dqget(mp
, NULL
, id
, type
, XFS_QMOPT_DQALLOC
, &dqp
))) {
495 xfs_trans_cancel(tp
, XFS_TRANS_ABORT
);
496 mutex_unlock(&(XFS_QI_QOFFLOCK(mp
)));
497 ASSERT(error
!= ENOENT
);
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 mp
->m_quotainfo
->qi_bhardlimit
= hard
;
517 mp
->m_quotainfo
->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 mp
->m_quotainfo
->qi_rtbhardlimit
= hard
;
533 mp
->m_quotainfo
->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 mp
->m_quotainfo
->qi_ihardlimit
= hard
;
550 mp
->m_quotainfo
->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 mp
->m_quotainfo
->qi_btimelimit
= newlim
->d_btimer
;
576 ddq
->d_btimer
= cpu_to_be32(newlim
->d_btimer
);
578 if (newlim
->d_fieldmask
& FS_DQ_ITIMER
) {
579 mp
->m_quotainfo
->qi_itimelimit
= newlim
->d_itimer
;
580 ddq
->d_itimer
= cpu_to_be32(newlim
->d_itimer
);
582 if (newlim
->d_fieldmask
& FS_DQ_RTBTIMER
) {
583 mp
->m_quotainfo
->qi_rtbtimelimit
= newlim
->d_rtbtimer
;
584 ddq
->d_rtbtimer
= cpu_to_be32(newlim
->d_rtbtimer
);
586 if (newlim
->d_fieldmask
& FS_DQ_BWARNS
)
587 mp
->m_quotainfo
->qi_bwarnlimit
= newlim
->d_bwarns
;
588 if (newlim
->d_fieldmask
& FS_DQ_IWARNS
)
589 mp
->m_quotainfo
->qi_iwarnlimit
= newlim
->d_iwarns
;
590 if (newlim
->d_fieldmask
& FS_DQ_RTBWARNS
)
591 mp
->m_quotainfo
->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);
608 mutex_unlock(&(XFS_QI_QOFFLOCK(mp
)));
614 xfs_qm_scall_getquota(
618 fs_disk_quota_t
*out
)
624 * Try to get the dquot. We don't want it allocated on disk, so
625 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
626 * exist, we'll get ENOENT back.
628 if ((error
= xfs_qm_dqget(mp
, NULL
, id
, type
, 0, &dqp
))) {
633 * If everything's NULL, this dquot doesn't quite exist as far as
634 * our utility programs are concerned.
636 if (XFS_IS_DQUOT_UNINITIALIZED(dqp
)) {
638 return XFS_ERROR(ENOENT
);
640 /* xfs_qm_dqprint(dqp); */
642 * Convert the disk dquot to the exportable format
644 xfs_qm_export_dquot(mp
, &dqp
->q_core
, out
);
646 return (error
? XFS_ERROR(EFAULT
) : 0);
651 xfs_qm_log_quotaoff_end(
653 xfs_qoff_logitem_t
*startqoff
,
658 xfs_qoff_logitem_t
*qoffi
;
660 tp
= xfs_trans_alloc(mp
, XFS_TRANS_QM_QUOTAOFF_END
);
662 if ((error
= xfs_trans_reserve(tp
, 0, sizeof(xfs_qoff_logitem_t
) * 2,
663 0, 0, XFS_DEFAULT_LOG_COUNT
))) {
664 xfs_trans_cancel(tp
, 0);
668 qoffi
= xfs_trans_get_qoff_item(tp
, startqoff
,
669 flags
& XFS_ALL_QUOTA_ACCT
);
670 xfs_trans_log_quotaoff_item(tp
, qoffi
);
673 * We have to make sure that the transaction is secure on disk before we
674 * return and actually stop quota accounting. So, make it synchronous.
675 * We don't care about quotoff's performance.
677 xfs_trans_set_sync(tp
);
678 error
= xfs_trans_commit(tp
, 0);
686 xfs_qoff_logitem_t
**qoffstartp
,
691 xfs_qoff_logitem_t
*qoffi
=NULL
;
694 tp
= xfs_trans_alloc(mp
, XFS_TRANS_QM_QUOTAOFF
);
695 if ((error
= xfs_trans_reserve(tp
, 0,
696 sizeof(xfs_qoff_logitem_t
) * 2 +
697 mp
->m_sb
.sb_sectsize
+ 128,
700 XFS_DEFAULT_LOG_COUNT
))) {
704 qoffi
= xfs_trans_get_qoff_item(tp
, NULL
, flags
& XFS_ALL_QUOTA_ACCT
);
705 xfs_trans_log_quotaoff_item(tp
, qoffi
);
707 spin_lock(&mp
->m_sb_lock
);
708 oldsbqflag
= mp
->m_sb
.sb_qflags
;
709 mp
->m_sb
.sb_qflags
= (mp
->m_qflags
& ~(flags
)) & XFS_MOUNT_QUOTA_ALL
;
710 spin_unlock(&mp
->m_sb_lock
);
712 xfs_mod_sb(tp
, XFS_SB_QFLAGS
);
715 * We have to make sure that the transaction is secure on disk before we
716 * return and actually stop quota accounting. So, make it synchronous.
717 * We don't care about quotoff's performance.
719 xfs_trans_set_sync(tp
);
720 error
= xfs_trans_commit(tp
, 0);
724 xfs_trans_cancel(tp
, 0);
726 * No one else is modifying sb_qflags, so this is OK.
727 * We still hold the quotaofflock.
729 spin_lock(&mp
->m_sb_lock
);
730 mp
->m_sb
.sb_qflags
= oldsbqflag
;
731 spin_unlock(&mp
->m_sb_lock
);
739 * Translate an internal style on-disk-dquot to the exportable format.
740 * The main differences are that the counters/limits are all in Basic
741 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
742 * to be converted to the native endianness.
747 xfs_disk_dquot_t
*src
,
748 struct fs_disk_quota
*dst
)
750 memset(dst
, 0, sizeof(*dst
));
751 dst
->d_version
= FS_DQUOT_VERSION
; /* different from src->d_version */
752 dst
->d_flags
= xfs_qm_export_qtype_flags(src
->d_flags
);
753 dst
->d_id
= be32_to_cpu(src
->d_id
);
754 dst
->d_blk_hardlimit
=
755 XFS_FSB_TO_BB(mp
, be64_to_cpu(src
->d_blk_hardlimit
));
756 dst
->d_blk_softlimit
=
757 XFS_FSB_TO_BB(mp
, be64_to_cpu(src
->d_blk_softlimit
));
758 dst
->d_ino_hardlimit
= be64_to_cpu(src
->d_ino_hardlimit
);
759 dst
->d_ino_softlimit
= be64_to_cpu(src
->d_ino_softlimit
);
760 dst
->d_bcount
= XFS_FSB_TO_BB(mp
, be64_to_cpu(src
->d_bcount
));
761 dst
->d_icount
= be64_to_cpu(src
->d_icount
);
762 dst
->d_btimer
= be32_to_cpu(src
->d_btimer
);
763 dst
->d_itimer
= be32_to_cpu(src
->d_itimer
);
764 dst
->d_iwarns
= be16_to_cpu(src
->d_iwarns
);
765 dst
->d_bwarns
= be16_to_cpu(src
->d_bwarns
);
766 dst
->d_rtb_hardlimit
=
767 XFS_FSB_TO_BB(mp
, be64_to_cpu(src
->d_rtb_hardlimit
));
768 dst
->d_rtb_softlimit
=
769 XFS_FSB_TO_BB(mp
, be64_to_cpu(src
->d_rtb_softlimit
));
770 dst
->d_rtbcount
= XFS_FSB_TO_BB(mp
, be64_to_cpu(src
->d_rtbcount
));
771 dst
->d_rtbtimer
= be32_to_cpu(src
->d_rtbtimer
);
772 dst
->d_rtbwarns
= be16_to_cpu(src
->d_rtbwarns
);
775 * Internally, we don't reset all the timers when quota enforcement
776 * gets turned off. No need to confuse the user level code,
777 * so return zeroes in that case.
779 if ((!XFS_IS_UQUOTA_ENFORCED(mp
) && src
->d_flags
== XFS_DQ_USER
) ||
780 (!XFS_IS_OQUOTA_ENFORCED(mp
) &&
781 (src
->d_flags
& (XFS_DQ_PROJ
| XFS_DQ_GROUP
)))) {
788 if (((XFS_IS_UQUOTA_ENFORCED(mp
) && dst
->d_flags
== XFS_USER_QUOTA
) ||
789 (XFS_IS_OQUOTA_ENFORCED(mp
) &&
790 (dst
->d_flags
& (XFS_PROJ_QUOTA
| XFS_GROUP_QUOTA
)))) &&
792 if (((int) dst
->d_bcount
>= (int) dst
->d_blk_softlimit
) &&
793 (dst
->d_blk_softlimit
> 0)) {
794 ASSERT(dst
->d_btimer
!= 0);
796 if (((int) dst
->d_icount
>= (int) dst
->d_ino_softlimit
) &&
797 (dst
->d_ino_softlimit
> 0)) {
798 ASSERT(dst
->d_itimer
!= 0);
805 xfs_qm_export_qtype_flags(
809 * Can't be more than one, or none.
811 ASSERT((flags
& (XFS_PROJ_QUOTA
| XFS_USER_QUOTA
)) !=
812 (XFS_PROJ_QUOTA
| XFS_USER_QUOTA
));
813 ASSERT((flags
& (XFS_PROJ_QUOTA
| XFS_GROUP_QUOTA
)) !=
814 (XFS_PROJ_QUOTA
| XFS_GROUP_QUOTA
));
815 ASSERT((flags
& (XFS_USER_QUOTA
| XFS_GROUP_QUOTA
)) !=
816 (XFS_USER_QUOTA
| XFS_GROUP_QUOTA
));
817 ASSERT((flags
& (XFS_PROJ_QUOTA
|XFS_USER_QUOTA
|XFS_GROUP_QUOTA
)) != 0);
819 return (flags
& XFS_DQ_USER
) ?
820 XFS_USER_QUOTA
: (flags
& XFS_DQ_PROJ
) ?
821 XFS_PROJ_QUOTA
: XFS_GROUP_QUOTA
;
831 if (flags
& XFS_UQUOTA_ACCT
)
832 uflags
|= XFS_QUOTA_UDQ_ACCT
;
833 if (flags
& XFS_PQUOTA_ACCT
)
834 uflags
|= XFS_QUOTA_PDQ_ACCT
;
835 if (flags
& XFS_GQUOTA_ACCT
)
836 uflags
|= XFS_QUOTA_GDQ_ACCT
;
837 if (flags
& XFS_UQUOTA_ENFD
)
838 uflags
|= XFS_QUOTA_UDQ_ENFD
;
839 if (flags
& (XFS_OQUOTA_ENFD
)) {
840 uflags
|= (flags
& XFS_GQUOTA_ACCT
) ?
841 XFS_QUOTA_GDQ_ENFD
: XFS_QUOTA_PDQ_ENFD
;
849 struct xfs_inode
*ip
,
850 struct xfs_perag
*pag
,
855 /* skip quota inodes */
856 if (ip
== XFS_QI_UQIP(ip
->i_mount
) || ip
== XFS_QI_GQIP(ip
->i_mount
)) {
857 ASSERT(ip
->i_udquot
== NULL
);
858 ASSERT(ip
->i_gdquot
== NULL
);
859 read_unlock(&pag
->pag_ici_lock
);
863 error
= xfs_sync_inode_valid(ip
, pag
);
867 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
868 if ((flags
& XFS_UQUOTA_ACCT
) && ip
->i_udquot
) {
869 xfs_qm_dqrele(ip
->i_udquot
);
872 if (flags
& (XFS_PQUOTA_ACCT
|XFS_GQUOTA_ACCT
) && ip
->i_gdquot
) {
873 xfs_qm_dqrele(ip
->i_gdquot
);
876 xfs_iput(ip
, XFS_ILOCK_EXCL
);
883 * Go thru all the inodes in the file system, releasing their dquots.
885 * Note that the mount structure gets modified to indicate that quotas are off
886 * AFTER this, in the case of quotaoff.
889 xfs_qm_dqrele_all_inodes(
890 struct xfs_mount
*mp
,
893 ASSERT(mp
->m_quotainfo
);
894 xfs_inode_ag_iterator(mp
, xfs_dqrele_inode
, flags
, XFS_ICI_NO_TAG
);
897 /*------------------------------------------------------------------------*/
900 * This contains all the test functions for XFS disk quotas.
901 * Currently it does a quota accounting check. ie. it walks through
902 * all inodes in the file system, calculating the dquot accounting fields,
903 * and prints out any inconsistencies.
905 xfs_dqhash_t
*qmtest_udqtab
;
906 xfs_dqhash_t
*qmtest_gdqtab
;
909 struct mutex qcheck_lock
;
911 #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
912 (__psunsigned_t)(id)) & \
913 (qmtest_hashmask - 1))
915 #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
917 DQTEST_HASHVAL(mp, id)) : \
919 DQTEST_HASHVAL(mp, id)))
921 #define DQTEST_LIST_PRINT(l, NXT, title) \
923 xfs_dqtest_t *dqp; int i = 0;\
924 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
925 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
926 dqp = (xfs_dqtest_t *)dqp->NXT) { \
927 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
928 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
929 dqp->d_bcount, dqp->d_icount); } \
932 typedef struct dqtest
{
933 xfs_dqmarker_t q_lists
;
934 xfs_dqhash_t
*q_hash
; /* the hashchain header */
935 xfs_mount_t
*q_mount
; /* filesystem this relates to */
936 xfs_dqid_t d_id
; /* user id or group id */
937 xfs_qcnt_t d_bcount
; /* # disk blocks owned by the user */
938 xfs_qcnt_t d_icount
; /* # inodes owned by the user */
942 xfs_qm_hashinsert(xfs_dqhash_t
*h
, xfs_dqtest_t
*dqp
)
945 if (((d
) = (h
)->qh_next
))
946 (d
)->HL_PREVP
= &((dqp
)->HL_NEXT
);
948 (dqp
)->HL_PREVP
= &((h
)->qh_next
);
949 (h
)->qh_next
= (xfs_dquot_t
*)dqp
;
957 cmn_err(CE_DEBUG
, "-----------DQTEST DQUOT----------------");
958 cmn_err(CE_DEBUG
, "---- dquot ID = %d", d
->d_id
);
959 cmn_err(CE_DEBUG
, "---- fs = 0x%p", d
->q_mount
);
960 cmn_err(CE_DEBUG
, "---- bcount = %Lu (0x%x)",
961 d
->d_bcount
, (int)d
->d_bcount
);
962 cmn_err(CE_DEBUG
, "---- icount = %Lu (0x%x)",
963 d
->d_icount
, (int)d
->d_icount
);
964 cmn_err(CE_DEBUG
, "---------------------------");
968 xfs_qm_dqtest_failed(
978 cmn_err(CE_DEBUG
, "quotacheck failed id=%d, err=%d\nreason: %s",
979 d
->d_id
, error
, reason
);
981 cmn_err(CE_DEBUG
, "quotacheck failed id=%d (%s) [%d != %d]",
982 d
->d_id
, reason
, (int)a
, (int)b
);
983 xfs_qm_dqtest_print(d
);
994 if (be64_to_cpu(dqp
->q_core
.d_icount
) != d
->d_icount
) {
995 xfs_qm_dqtest_failed(d
, dqp
, "icount mismatch",
996 be64_to_cpu(dqp
->q_core
.d_icount
),
1000 if (be64_to_cpu(dqp
->q_core
.d_bcount
) != d
->d_bcount
) {
1001 xfs_qm_dqtest_failed(d
, dqp
, "bcount mismatch",
1002 be64_to_cpu(dqp
->q_core
.d_bcount
),
1006 if (dqp
->q_core
.d_blk_softlimit
&&
1007 be64_to_cpu(dqp
->q_core
.d_bcount
) >=
1008 be64_to_cpu(dqp
->q_core
.d_blk_softlimit
)) {
1009 if (!dqp
->q_core
.d_btimer
&& dqp
->q_core
.d_id
) {
1011 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1012 d
->d_id
, DQFLAGTO_TYPESTR(d
), d
->q_mount
);
1016 if (dqp
->q_core
.d_ino_softlimit
&&
1017 be64_to_cpu(dqp
->q_core
.d_icount
) >=
1018 be64_to_cpu(dqp
->q_core
.d_ino_softlimit
)) {
1019 if (!dqp
->q_core
.d_itimer
&& dqp
->q_core
.d_id
) {
1021 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1022 d
->d_id
, DQFLAGTO_TYPESTR(d
), d
->q_mount
);
1028 cmn_err(CE_DEBUG
, "%d [%s] [0x%p] qchecked",
1029 d
->d_id
, DQFLAGTO_TYPESTR(d
), d
->q_mount
);
1042 /* xfs_qm_dqtest_print(d); */
1043 if ((error
= xfs_qm_dqget(d
->q_mount
, NULL
, d
->d_id
, d
->dq_flags
, 0,
1045 xfs_qm_dqtest_failed(d
, NULL
, "dqget failed", 0, 0, error
);
1048 xfs_dqtest_cmp2(d
, dqp
);
1053 xfs_qm_internalqcheck_dqget(
1057 xfs_dqtest_t
**O_dq
)
1062 h
= DQTEST_HASH(mp
, id
, type
);
1063 for (d
= (xfs_dqtest_t
*) h
->qh_next
; d
!= NULL
;
1064 d
= (xfs_dqtest_t
*) d
->HL_NEXT
) {
1065 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1066 if (d
->d_id
== id
&& mp
== d
->q_mount
) {
1071 d
= kmem_zalloc(sizeof(xfs_dqtest_t
), KM_SLEEP
);
1076 xfs_qm_hashinsert(h
, d
);
1082 xfs_qm_internalqcheck_get_dquots(
1090 if (XFS_IS_UQUOTA_ON(mp
))
1091 xfs_qm_internalqcheck_dqget(mp
, uid
, XFS_DQ_USER
, ud
);
1092 if (XFS_IS_GQUOTA_ON(mp
))
1093 xfs_qm_internalqcheck_dqget(mp
, gid
, XFS_DQ_GROUP
, gd
);
1094 else if (XFS_IS_PQUOTA_ON(mp
))
1095 xfs_qm_internalqcheck_dqget(mp
, projid
, XFS_DQ_PROJ
, gd
);
1100 xfs_qm_internalqcheck_dqadjust(
1105 d
->d_bcount
+= (xfs_qcnt_t
)ip
->i_d
.di_nblocks
;
1109 xfs_qm_internalqcheck_adjust(
1110 xfs_mount_t
*mp
, /* mount point for filesystem */
1111 xfs_ino_t ino
, /* inode number to get data for */
1112 void __user
*buffer
, /* not used */
1113 int ubsize
, /* not used */
1114 void *private_data
, /* not used */
1115 xfs_daddr_t bno
, /* starting block of inode cluster */
1116 int *ubused
, /* not used */
1117 void *dip
, /* not used */
1118 int *res
) /* bulkstat result code */
1121 xfs_dqtest_t
*ud
, *gd
;
1123 boolean_t ipreleased
;
1126 ASSERT(XFS_IS_QUOTA_RUNNING(mp
));
1128 if (ino
== mp
->m_sb
.sb_uquotino
|| ino
== mp
->m_sb
.sb_gquotino
) {
1129 *res
= BULKSTAT_RV_NOTHING
;
1130 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1131 (unsigned long long) ino
,
1132 (unsigned long long) mp
->m_sb
.sb_uquotino
,
1133 (unsigned long long) mp
->m_sb
.sb_gquotino
);
1134 return XFS_ERROR(EINVAL
);
1136 ipreleased
= B_FALSE
;
1138 lock_flags
= XFS_ILOCK_SHARED
;
1139 if ((error
= xfs_iget(mp
, NULL
, ino
, 0, lock_flags
, &ip
, bno
))) {
1140 *res
= BULKSTAT_RV_NOTHING
;
1145 * This inode can have blocks after eof which can get released
1146 * when we send it to inactive. Since we don't check the dquot
1147 * until the after all our calculations are done, we must get rid
1151 xfs_iput(ip
, lock_flags
);
1152 ipreleased
= B_TRUE
;
1155 xfs_qm_internalqcheck_get_dquots(mp
,
1156 (xfs_dqid_t
) ip
->i_d
.di_uid
,
1157 (xfs_dqid_t
) ip
->i_d
.di_projid
,
1158 (xfs_dqid_t
) ip
->i_d
.di_gid
,
1160 if (XFS_IS_UQUOTA_ON(mp
)) {
1162 xfs_qm_internalqcheck_dqadjust(ip
, ud
);
1164 if (XFS_IS_OQUOTA_ON(mp
)) {
1166 xfs_qm_internalqcheck_dqadjust(ip
, gd
);
1168 xfs_iput(ip
, lock_flags
);
1169 *res
= BULKSTAT_RV_DIDONE
;
1174 /* PRIVATE, debugging */
1176 xfs_qm_internalqcheck(
1182 xfs_dqtest_t
*d
, *e
;
1187 qmtest_hashmask
= 32;
1192 if (! XFS_IS_QUOTA_ON(mp
))
1193 return XFS_ERROR(ESRCH
);
1195 xfs_log_force(mp
, (xfs_lsn_t
)0, XFS_LOG_FORCE
| XFS_LOG_SYNC
);
1196 XFS_bflush(mp
->m_ddev_targp
);
1197 xfs_log_force(mp
, (xfs_lsn_t
)0, XFS_LOG_FORCE
| XFS_LOG_SYNC
);
1198 XFS_bflush(mp
->m_ddev_targp
);
1200 mutex_lock(&qcheck_lock
);
1201 /* There should be absolutely no quota activity while this
1203 qmtest_udqtab
= kmem_zalloc(qmtest_hashmask
*
1204 sizeof(xfs_dqhash_t
), KM_SLEEP
);
1205 qmtest_gdqtab
= kmem_zalloc(qmtest_hashmask
*
1206 sizeof(xfs_dqhash_t
), KM_SLEEP
);
1209 * Iterate thru all the inodes in the file system,
1210 * adjusting the corresponding dquot counters
1212 if ((error
= xfs_bulkstat(mp
, &lastino
, &count
,
1213 xfs_qm_internalqcheck_adjust
, NULL
,
1214 0, NULL
, BULKSTAT_FG_IGET
, &done
))) {
1219 cmn_err(CE_DEBUG
, "Bulkstat returned error 0x%x", error
);
1221 cmn_err(CE_DEBUG
, "Checking results against system dquots");
1222 for (i
= 0; i
< qmtest_hashmask
; i
++) {
1223 h1
= &qmtest_udqtab
[i
];
1224 for (d
= (xfs_dqtest_t
*) h1
->qh_next
; d
!= NULL
; ) {
1226 e
= (xfs_dqtest_t
*) d
->HL_NEXT
;
1230 h1
= &qmtest_gdqtab
[i
];
1231 for (d
= (xfs_dqtest_t
*) h1
->qh_next
; d
!= NULL
; ) {
1233 e
= (xfs_dqtest_t
*) d
->HL_NEXT
;
1239 if (qmtest_nfails
) {
1240 cmn_err(CE_DEBUG
, "******** quotacheck failed ********");
1241 cmn_err(CE_DEBUG
, "failures = %d", qmtest_nfails
);
1243 cmn_err(CE_DEBUG
, "******** quotacheck successful! ********");
1245 kmem_free(qmtest_udqtab
);
1246 kmem_free(qmtest_gdqtab
);
1247 mutex_unlock(&qcheck_lock
);
1248 return (qmtest_nfails
);