[PATCH] sched: cleanup wake_idle
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / quota / xfs_qm_syscalls.c
blob68e98962dbefe0db3f303204c97c53b82b6da975
1 /*
2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
34 #include "xfs_fs.h"
35 #include "xfs_inum.h"
36 #include "xfs_log.h"
37 #include "xfs_trans.h"
38 #include "xfs_sb.h"
39 #include "xfs_dir.h"
40 #include "xfs_dir2.h"
41 #include "xfs_alloc.h"
42 #include "xfs_dmapi.h"
43 #include "xfs_quota.h"
44 #include "xfs_mount.h"
45 #include "xfs_alloc_btree.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_btree.h"
49 #include "xfs_ialloc.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dir_sf.h"
52 #include "xfs_dir2_sf.h"
53 #include "xfs_dinode.h"
54 #include "xfs_inode.h"
55 #include "xfs_bmap.h"
56 #include "xfs_bit.h"
57 #include "xfs_rtalloc.h"
58 #include "xfs_error.h"
59 #include "xfs_itable.h"
60 #include "xfs_rw.h"
61 #include "xfs_acl.h"
62 #include "xfs_cap.h"
63 #include "xfs_mac.h"
64 #include "xfs_attr.h"
65 #include "xfs_buf_item.h"
66 #include "xfs_utils.h"
68 #include "xfs_qm.h"
70 #ifdef DEBUG
71 # define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
72 #else
73 # define qdprintk(s, args...) do { } while (0)
74 #endif
76 STATIC int xfs_qm_scall_trunc_qfiles(xfs_mount_t *, uint);
77 STATIC int xfs_qm_scall_getquota(xfs_mount_t *, xfs_dqid_t, uint,
78 fs_disk_quota_t *);
79 STATIC int xfs_qm_scall_getqstat(xfs_mount_t *, fs_quota_stat_t *);
80 STATIC int xfs_qm_scall_setqlim(xfs_mount_t *, xfs_dqid_t, uint,
81 fs_disk_quota_t *);
82 STATIC int xfs_qm_scall_quotaon(xfs_mount_t *, uint);
83 STATIC int xfs_qm_scall_quotaoff(xfs_mount_t *, uint, boolean_t);
84 STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
85 STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
86 uint);
87 STATIC uint xfs_qm_import_flags(uint);
88 STATIC uint xfs_qm_export_flags(uint);
89 STATIC uint xfs_qm_import_qtype_flags(uint);
90 STATIC uint xfs_qm_export_qtype_flags(uint);
91 STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
92 fs_disk_quota_t *);
96 * The main distribution switch of all XFS quotactl system calls.
98 int
99 xfs_qm_quotactl(
100 struct bhv_desc *bdp,
101 int cmd,
102 int id,
103 xfs_caddr_t addr)
105 xfs_mount_t *mp;
106 int error;
107 struct vfs *vfsp;
109 vfsp = bhvtovfs(bdp);
110 mp = XFS_VFSTOM(vfsp);
112 if (addr == NULL && cmd != Q_SYNC)
113 return XFS_ERROR(EINVAL);
114 if (id < 0 && cmd != Q_SYNC)
115 return XFS_ERROR(EINVAL);
118 * The following commands are valid even when quotaoff.
120 switch (cmd) {
121 case Q_XQUOTARM:
123 * Truncate quota files. quota must be off.
125 if (XFS_IS_QUOTA_ON(mp) || addr == NULL)
126 return XFS_ERROR(EINVAL);
127 if (vfsp->vfs_flag & VFS_RDONLY)
128 return XFS_ERROR(EROFS);
129 return (xfs_qm_scall_trunc_qfiles(mp,
130 xfs_qm_import_qtype_flags(*(uint *)addr)));
132 case Q_XGETQSTAT:
134 * Get quota status information.
136 return (xfs_qm_scall_getqstat(mp, (fs_quota_stat_t *)addr));
138 case Q_XQUOTAON:
140 * QUOTAON - enabling quota enforcement.
141 * Quota accounting must be turned on at mount time.
143 if (addr == NULL)
144 return XFS_ERROR(EINVAL);
145 if (vfsp->vfs_flag & VFS_RDONLY)
146 return XFS_ERROR(EROFS);
147 return (xfs_qm_scall_quotaon(mp,
148 xfs_qm_import_flags(*(uint *)addr)));
150 case Q_XQUOTAOFF:
151 if (vfsp->vfs_flag & VFS_RDONLY)
152 return XFS_ERROR(EROFS);
153 break;
155 default:
156 break;
159 if (! XFS_IS_QUOTA_ON(mp))
160 return XFS_ERROR(ESRCH);
162 switch (cmd) {
163 case Q_XQUOTAOFF:
164 if (vfsp->vfs_flag & VFS_RDONLY)
165 return XFS_ERROR(EROFS);
166 error = xfs_qm_scall_quotaoff(mp,
167 xfs_qm_import_flags(*(uint *)addr),
168 B_FALSE);
169 break;
171 case Q_XGETQUOTA:
172 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_USER,
173 (fs_disk_quota_t *)addr);
174 break;
175 case Q_XGETGQUOTA:
176 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
177 (fs_disk_quota_t *)addr);
178 break;
179 case Q_XGETPQUOTA:
180 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
181 (fs_disk_quota_t *)addr);
182 break;
184 case Q_XSETQLIM:
185 if (vfsp->vfs_flag & VFS_RDONLY)
186 return XFS_ERROR(EROFS);
187 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_USER,
188 (fs_disk_quota_t *)addr);
189 break;
190 case Q_XSETGQLIM:
191 if (vfsp->vfs_flag & VFS_RDONLY)
192 return XFS_ERROR(EROFS);
193 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
194 (fs_disk_quota_t *)addr);
195 break;
196 case Q_XSETPQLIM:
197 if (vfsp->vfs_flag & VFS_RDONLY)
198 return XFS_ERROR(EROFS);
199 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
200 (fs_disk_quota_t *)addr);
201 break;
203 default:
204 error = XFS_ERROR(EINVAL);
205 break;
208 return (error);
212 * Turn off quota accounting and/or enforcement for all udquots and/or
213 * gdquots. Called only at unmount time.
215 * This assumes that there are no dquots of this file system cached
216 * incore, and modifies the ondisk dquot directly. Therefore, for example,
217 * it is an error to call this twice, without purging the cache.
219 STATIC int
220 xfs_qm_scall_quotaoff(
221 xfs_mount_t *mp,
222 uint flags,
223 boolean_t force)
225 uint dqtype;
226 unsigned long s;
227 int error;
228 uint inactivate_flags;
229 xfs_qoff_logitem_t *qoffstart;
230 int nculprits;
232 if (!force && !capable(CAP_SYS_ADMIN))
233 return XFS_ERROR(EPERM);
235 * No file system can have quotas enabled on disk but not in core.
236 * Note that quota utilities (like quotaoff) _expect_
237 * errno == EEXIST here.
239 if ((mp->m_qflags & flags) == 0)
240 return XFS_ERROR(EEXIST);
241 error = 0;
243 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
246 * We don't want to deal with two quotaoffs messing up each other,
247 * so we're going to serialize it. quotaoff isn't exactly a performance
248 * critical thing.
249 * If quotaoff, then we must be dealing with the root filesystem.
251 ASSERT(mp->m_quotainfo);
252 if (mp->m_quotainfo)
253 mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
255 ASSERT(mp->m_quotainfo);
258 * If we're just turning off quota enforcement, change mp and go.
260 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
261 mp->m_qflags &= ~(flags);
263 s = XFS_SB_LOCK(mp);
264 mp->m_sb.sb_qflags = mp->m_qflags;
265 XFS_SB_UNLOCK(mp, s);
266 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
268 /* XXX what to do if error ? Revert back to old vals incore ? */
269 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
270 return (error);
273 dqtype = 0;
274 inactivate_flags = 0;
276 * If accounting is off, we must turn enforcement off, clear the
277 * quota 'CHKD' certificate to make it known that we have to
278 * do a quotacheck the next time this quota is turned on.
280 if (flags & XFS_UQUOTA_ACCT) {
281 dqtype |= XFS_QMOPT_UQUOTA;
282 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
283 inactivate_flags |= XFS_UQUOTA_ACTIVE;
285 if (flags & XFS_GQUOTA_ACCT) {
286 dqtype |= XFS_QMOPT_GQUOTA;
287 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
288 inactivate_flags |= XFS_GQUOTA_ACTIVE;
289 } else if (flags & XFS_PQUOTA_ACCT) {
290 dqtype |= XFS_QMOPT_PQUOTA;
291 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
292 inactivate_flags |= XFS_PQUOTA_ACTIVE;
296 * Nothing to do? Don't complain. This happens when we're just
297 * turning off quota enforcement.
299 if ((mp->m_qflags & flags) == 0) {
300 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
301 return (0);
305 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
306 * and synchronously.
308 xfs_qm_log_quotaoff(mp, &qoffstart, flags);
311 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
312 * to take care of the race between dqget and quotaoff. We don't take
313 * any special locks to reset these bits. All processes need to check
314 * these bits *after* taking inode lock(s) to see if the particular
315 * quota type is in the process of being turned off. If *ACTIVE, it is
316 * guaranteed that all dquot structures and all quotainode ptrs will all
317 * stay valid as long as that inode is kept locked.
319 * There is no turning back after this.
321 mp->m_qflags &= ~inactivate_flags;
324 * Give back all the dquot reference(s) held by inodes.
325 * Here we go thru every single incore inode in this file system, and
326 * do a dqrele on the i_udquot/i_gdquot that it may have.
327 * Essentially, as long as somebody has an inode locked, this guarantees
328 * that quotas will not be turned off. This is handy because in a
329 * transaction once we lock the inode(s) and check for quotaon, we can
330 * depend on the quota inodes (and other things) being valid as long as
331 * we keep the lock(s).
333 xfs_qm_dqrele_all_inodes(mp, flags);
336 * Next we make the changes in the quota flag in the mount struct.
337 * This isn't protected by a particular lock directly, because we
338 * don't want to take a mrlock everytime we depend on quotas being on.
340 mp->m_qflags &= ~(flags);
343 * Go through all the dquots of this file system and purge them,
344 * according to what was turned off. We may not be able to get rid
345 * of all dquots, because dquots can have temporary references that
346 * are not attached to inodes. eg. xfs_setattr, xfs_create.
347 * So, if we couldn't purge all the dquots from the filesystem,
348 * we can't get rid of the incore data structures.
350 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
351 delay(10 * nculprits);
354 * Transactions that had started before ACTIVE state bit was cleared
355 * could have logged many dquots, so they'd have higher LSNs than
356 * the first QUOTAOFF log record does. If we happen to crash when
357 * the tail of the log has gone past the QUOTAOFF record, but
358 * before the last dquot modification, those dquots __will__
359 * recover, and that's not good.
361 * So, we have QUOTAOFF start and end logitems; the start
362 * logitem won't get overwritten until the end logitem appears...
364 xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
367 * If quotas is completely disabled, close shop.
369 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
370 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
371 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
372 xfs_qm_destroy_quotainfo(mp);
373 return (0);
377 * Release our quotainode references, and vn_purge them,
378 * if we don't need them anymore.
380 if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
381 XFS_PURGE_INODE(XFS_QI_UQIP(mp));
382 XFS_QI_UQIP(mp) = NULL;
384 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
385 XFS_PURGE_INODE(XFS_QI_GQIP(mp));
386 XFS_QI_GQIP(mp) = NULL;
388 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
390 return (error);
393 STATIC int
394 xfs_qm_scall_trunc_qfiles(
395 xfs_mount_t *mp,
396 uint flags)
398 int error;
399 xfs_inode_t *qip;
401 if (!capable(CAP_SYS_ADMIN))
402 return XFS_ERROR(EPERM);
403 error = 0;
404 if (!XFS_SB_VERSION_HASQUOTA(&mp->m_sb) || flags == 0) {
405 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
406 return XFS_ERROR(EINVAL);
409 if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
410 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
411 if (! error) {
412 (void) xfs_truncate_file(mp, qip);
413 VN_RELE(XFS_ITOV(qip));
417 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
418 mp->m_sb.sb_gquotino != NULLFSINO) {
419 error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
420 if (! error) {
421 (void) xfs_truncate_file(mp, qip);
422 VN_RELE(XFS_ITOV(qip));
426 return (error);
431 * Switch on (a given) quota enforcement for a filesystem. This takes
432 * effect immediately.
433 * (Switching on quota accounting must be done at mount time.)
435 STATIC int
436 xfs_qm_scall_quotaon(
437 xfs_mount_t *mp,
438 uint flags)
440 int error;
441 unsigned long s;
442 uint qf;
443 uint accflags;
444 __int64_t sbflags;
446 if (!capable(CAP_SYS_ADMIN))
447 return XFS_ERROR(EPERM);
449 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
451 * Switching on quota accounting must be done at mount time.
453 accflags = flags & XFS_ALL_QUOTA_ACCT;
454 flags &= ~(XFS_ALL_QUOTA_ACCT);
456 sbflags = 0;
458 if (flags == 0) {
459 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
460 return XFS_ERROR(EINVAL);
463 /* No fs can turn on quotas with a delayed effect */
464 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
467 * Can't enforce without accounting. We check the superblock
468 * qflags here instead of m_qflags because rootfs can have
469 * quota acct on ondisk without m_qflags' knowing.
471 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
472 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
473 (flags & XFS_UQUOTA_ENFD))
475 ((flags & XFS_PQUOTA_ACCT) == 0 &&
476 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
477 (flags & XFS_OQUOTA_ENFD))
479 ((flags & XFS_GQUOTA_ACCT) == 0 &&
480 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
481 (flags & XFS_OQUOTA_ENFD))) {
482 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
483 flags, mp->m_sb.sb_qflags);
484 return XFS_ERROR(EINVAL);
487 * If everything's upto-date incore, then don't waste time.
489 if ((mp->m_qflags & flags) == flags)
490 return XFS_ERROR(EEXIST);
493 * Change sb_qflags on disk but not incore mp->qflags
494 * if this is the root filesystem.
496 s = XFS_SB_LOCK(mp);
497 qf = mp->m_sb.sb_qflags;
498 mp->m_sb.sb_qflags = qf | flags;
499 XFS_SB_UNLOCK(mp, s);
502 * There's nothing to change if it's the same.
504 if ((qf & flags) == flags && sbflags == 0)
505 return XFS_ERROR(EEXIST);
506 sbflags |= XFS_SB_QFLAGS;
508 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
509 return (error);
511 * If we aren't trying to switch on quota enforcement, we are done.
513 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
514 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
515 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
516 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
517 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
518 (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
519 (flags & XFS_ALL_QUOTA_ENFD) == 0)
520 return (0);
522 if (! XFS_IS_QUOTA_RUNNING(mp))
523 return XFS_ERROR(ESRCH);
526 * Switch on quota enforcement in core.
528 mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
529 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
530 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
532 return (0);
537 * Return quota status information, such as uquota-off, enforcements, etc.
539 STATIC int
540 xfs_qm_scall_getqstat(
541 xfs_mount_t *mp,
542 fs_quota_stat_t *out)
544 xfs_inode_t *uip, *gip;
545 boolean_t tempuqip, tempgqip;
547 uip = gip = NULL;
548 tempuqip = tempgqip = B_FALSE;
549 memset(out, 0, sizeof(fs_quota_stat_t));
551 out->qs_version = FS_QSTAT_VERSION;
552 if (! XFS_SB_VERSION_HASQUOTA(&mp->m_sb)) {
553 out->qs_uquota.qfs_ino = NULLFSINO;
554 out->qs_gquota.qfs_ino = NULLFSINO;
555 return (0);
557 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
558 (XFS_ALL_QUOTA_ACCT|
559 XFS_ALL_QUOTA_ENFD));
560 out->qs_pad = 0;
561 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
562 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
564 if (mp->m_quotainfo) {
565 uip = mp->m_quotainfo->qi_uquotaip;
566 gip = mp->m_quotainfo->qi_gquotaip;
568 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
569 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
570 0, 0, &uip, 0) == 0)
571 tempuqip = B_TRUE;
573 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
574 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
575 0, 0, &gip, 0) == 0)
576 tempgqip = B_TRUE;
578 if (uip) {
579 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
580 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
581 if (tempuqip)
582 VN_RELE(XFS_ITOV(uip));
584 if (gip) {
585 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
586 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
587 if (tempgqip)
588 VN_RELE(XFS_ITOV(gip));
590 if (mp->m_quotainfo) {
591 out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
592 out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
593 out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
594 out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
595 out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
596 out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
598 return (0);
602 * Adjust quota limits, and start/stop timers accordingly.
604 STATIC int
605 xfs_qm_scall_setqlim(
606 xfs_mount_t *mp,
607 xfs_dqid_t id,
608 uint type,
609 fs_disk_quota_t *newlim)
611 xfs_disk_dquot_t *ddq;
612 xfs_dquot_t *dqp;
613 xfs_trans_t *tp;
614 int error;
615 xfs_qcnt_t hard, soft;
617 if (!capable(CAP_SYS_ADMIN))
618 return XFS_ERROR(EPERM);
620 if ((newlim->d_fieldmask &
621 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
622 return (0);
624 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
625 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
626 0, 0, XFS_DEFAULT_LOG_COUNT))) {
627 xfs_trans_cancel(tp, 0);
628 return (error);
632 * We don't want to race with a quotaoff so take the quotaoff lock.
633 * (We don't hold an inode lock, so there's nothing else to stop
634 * a quotaoff from happening). (XXXThis doesn't currently happen
635 * because we take the vfslock before calling xfs_qm_sysent).
637 mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
640 * Get the dquot (locked), and join it to the transaction.
641 * Allocate the dquot if this doesn't exist.
643 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
644 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
645 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
646 ASSERT(error != ENOENT);
647 return (error);
649 xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
650 xfs_trans_dqjoin(tp, dqp);
651 ddq = &dqp->q_core;
654 * Make sure that hardlimits are >= soft limits before changing.
656 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
657 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
658 INT_GET(ddq->d_blk_hardlimit, ARCH_CONVERT);
659 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
660 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
661 INT_GET(ddq->d_blk_softlimit, ARCH_CONVERT);
662 if (hard == 0 || hard >= soft) {
663 INT_SET(ddq->d_blk_hardlimit, ARCH_CONVERT, hard);
664 INT_SET(ddq->d_blk_softlimit, ARCH_CONVERT, soft);
665 if (id == 0) {
666 mp->m_quotainfo->qi_bhardlimit = hard;
667 mp->m_quotainfo->qi_bsoftlimit = soft;
669 } else {
670 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
672 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
673 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
674 INT_GET(ddq->d_rtb_hardlimit, ARCH_CONVERT);
675 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
676 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
677 INT_GET(ddq->d_rtb_softlimit, ARCH_CONVERT);
678 if (hard == 0 || hard >= soft) {
679 INT_SET(ddq->d_rtb_hardlimit, ARCH_CONVERT, hard);
680 INT_SET(ddq->d_rtb_softlimit, ARCH_CONVERT, soft);
681 if (id == 0) {
682 mp->m_quotainfo->qi_rtbhardlimit = hard;
683 mp->m_quotainfo->qi_rtbsoftlimit = soft;
685 } else {
686 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
689 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
690 (xfs_qcnt_t) newlim->d_ino_hardlimit :
691 INT_GET(ddq->d_ino_hardlimit, ARCH_CONVERT);
692 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
693 (xfs_qcnt_t) newlim->d_ino_softlimit :
694 INT_GET(ddq->d_ino_softlimit, ARCH_CONVERT);
695 if (hard == 0 || hard >= soft) {
696 INT_SET(ddq->d_ino_hardlimit, ARCH_CONVERT, hard);
697 INT_SET(ddq->d_ino_softlimit, ARCH_CONVERT, soft);
698 if (id == 0) {
699 mp->m_quotainfo->qi_ihardlimit = hard;
700 mp->m_quotainfo->qi_isoftlimit = soft;
702 } else {
703 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
707 * Update warnings counter(s) if requested
709 if (newlim->d_fieldmask & FS_DQ_BWARNS)
710 INT_SET(ddq->d_bwarns, ARCH_CONVERT, newlim->d_bwarns);
711 if (newlim->d_fieldmask & FS_DQ_IWARNS)
712 INT_SET(ddq->d_iwarns, ARCH_CONVERT, newlim->d_iwarns);
713 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
714 INT_SET(ddq->d_rtbwarns, ARCH_CONVERT, newlim->d_rtbwarns);
716 if (id == 0) {
718 * Timelimits for the super user set the relative time
719 * the other users can be over quota for this file system.
720 * If it is zero a default is used. Ditto for the default
721 * soft and hard limit values (already done, above), and
722 * for warnings.
724 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
725 mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
726 INT_SET(ddq->d_btimer, ARCH_CONVERT, newlim->d_btimer);
728 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
729 mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
730 INT_SET(ddq->d_itimer, ARCH_CONVERT, newlim->d_itimer);
732 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
733 mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
734 INT_SET(ddq->d_rtbtimer, ARCH_CONVERT, newlim->d_rtbtimer);
736 if (newlim->d_fieldmask & FS_DQ_BWARNS)
737 mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
738 if (newlim->d_fieldmask & FS_DQ_IWARNS)
739 mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
740 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
741 mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
742 } else {
744 * If the user is now over quota, start the timelimit.
745 * The user will not be 'warned'.
746 * Note that we keep the timers ticking, whether enforcement
747 * is on or off. We don't really want to bother with iterating
748 * over all ondisk dquots and turning the timers on/off.
750 xfs_qm_adjust_dqtimers(mp, ddq);
752 dqp->dq_flags |= XFS_DQ_DIRTY;
753 xfs_trans_log_dquot(tp, dqp);
755 xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
756 xfs_trans_commit(tp, 0, NULL);
757 xfs_qm_dqprint(dqp);
758 xfs_qm_dqrele(dqp);
759 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
761 return (0);
764 STATIC int
765 xfs_qm_scall_getquota(
766 xfs_mount_t *mp,
767 xfs_dqid_t id,
768 uint type,
769 fs_disk_quota_t *out)
771 xfs_dquot_t *dqp;
772 int error;
775 * Try to get the dquot. We don't want it allocated on disk, so
776 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
777 * exist, we'll get ENOENT back.
779 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
780 return (error);
783 xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
785 * If everything's NULL, this dquot doesn't quite exist as far as
786 * our utility programs are concerned.
788 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
789 xfs_qm_dqput(dqp);
790 return XFS_ERROR(ENOENT);
792 /* xfs_qm_dqprint(dqp); */
794 * Convert the disk dquot to the exportable format
796 xfs_qm_export_dquot(mp, &dqp->q_core, out);
797 xfs_qm_dqput(dqp);
798 return (error ? XFS_ERROR(EFAULT) : 0);
802 STATIC int
803 xfs_qm_log_quotaoff_end(
804 xfs_mount_t *mp,
805 xfs_qoff_logitem_t *startqoff,
806 uint flags)
808 xfs_trans_t *tp;
809 int error;
810 xfs_qoff_logitem_t *qoffi;
812 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
814 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
815 0, 0, XFS_DEFAULT_LOG_COUNT))) {
816 xfs_trans_cancel(tp, 0);
817 return (error);
820 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
821 flags & XFS_ALL_QUOTA_ACCT);
822 xfs_trans_log_quotaoff_item(tp, qoffi);
825 * We have to make sure that the transaction is secure on disk before we
826 * return and actually stop quota accounting. So, make it synchronous.
827 * We don't care about quotoff's performance.
829 xfs_trans_set_sync(tp);
830 error = xfs_trans_commit(tp, 0, NULL);
831 return (error);
835 STATIC int
836 xfs_qm_log_quotaoff(
837 xfs_mount_t *mp,
838 xfs_qoff_logitem_t **qoffstartp,
839 uint flags)
841 xfs_trans_t *tp;
842 int error;
843 unsigned long s;
844 xfs_qoff_logitem_t *qoffi=NULL;
845 uint oldsbqflag=0;
847 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
848 if ((error = xfs_trans_reserve(tp, 0,
849 sizeof(xfs_qoff_logitem_t) * 2 +
850 mp->m_sb.sb_sectsize + 128,
853 XFS_DEFAULT_LOG_COUNT))) {
854 goto error0;
857 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
858 xfs_trans_log_quotaoff_item(tp, qoffi);
860 s = XFS_SB_LOCK(mp);
861 oldsbqflag = mp->m_sb.sb_qflags;
862 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
863 XFS_SB_UNLOCK(mp, s);
865 xfs_mod_sb(tp, XFS_SB_QFLAGS);
868 * We have to make sure that the transaction is secure on disk before we
869 * return and actually stop quota accounting. So, make it synchronous.
870 * We don't care about quotoff's performance.
872 xfs_trans_set_sync(tp);
873 error = xfs_trans_commit(tp, 0, NULL);
875 error0:
876 if (error) {
877 xfs_trans_cancel(tp, 0);
879 * No one else is modifying sb_qflags, so this is OK.
880 * We still hold the quotaofflock.
882 s = XFS_SB_LOCK(mp);
883 mp->m_sb.sb_qflags = oldsbqflag;
884 XFS_SB_UNLOCK(mp, s);
886 *qoffstartp = qoffi;
887 return (error);
892 * Translate an internal style on-disk-dquot to the exportable format.
893 * The main differences are that the counters/limits are all in Basic
894 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
895 * to be converted to the native endianness.
897 STATIC void
898 xfs_qm_export_dquot(
899 xfs_mount_t *mp,
900 xfs_disk_dquot_t *src,
901 struct fs_disk_quota *dst)
903 memset(dst, 0, sizeof(*dst));
904 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
905 dst->d_flags =
906 xfs_qm_export_qtype_flags(INT_GET(src->d_flags, ARCH_CONVERT));
907 dst->d_id = INT_GET(src->d_id, ARCH_CONVERT);
908 dst->d_blk_hardlimit = (__uint64_t)
909 XFS_FSB_TO_BB(mp, INT_GET(src->d_blk_hardlimit, ARCH_CONVERT));
910 dst->d_blk_softlimit = (__uint64_t)
911 XFS_FSB_TO_BB(mp, INT_GET(src->d_blk_softlimit, ARCH_CONVERT));
912 dst->d_ino_hardlimit = (__uint64_t)
913 INT_GET(src->d_ino_hardlimit, ARCH_CONVERT);
914 dst->d_ino_softlimit = (__uint64_t)
915 INT_GET(src->d_ino_softlimit, ARCH_CONVERT);
916 dst->d_bcount = (__uint64_t)
917 XFS_FSB_TO_BB(mp, INT_GET(src->d_bcount, ARCH_CONVERT));
918 dst->d_icount = (__uint64_t) INT_GET(src->d_icount, ARCH_CONVERT);
919 dst->d_btimer = (__uint32_t) INT_GET(src->d_btimer, ARCH_CONVERT);
920 dst->d_itimer = (__uint32_t) INT_GET(src->d_itimer, ARCH_CONVERT);
921 dst->d_iwarns = INT_GET(src->d_iwarns, ARCH_CONVERT);
922 dst->d_bwarns = INT_GET(src->d_bwarns, ARCH_CONVERT);
924 dst->d_rtb_hardlimit = (__uint64_t)
925 XFS_FSB_TO_BB(mp, INT_GET(src->d_rtb_hardlimit, ARCH_CONVERT));
926 dst->d_rtb_softlimit = (__uint64_t)
927 XFS_FSB_TO_BB(mp, INT_GET(src->d_rtb_softlimit, ARCH_CONVERT));
928 dst->d_rtbcount = (__uint64_t)
929 XFS_FSB_TO_BB(mp, INT_GET(src->d_rtbcount, ARCH_CONVERT));
930 dst->d_rtbtimer = (__uint32_t) INT_GET(src->d_rtbtimer, ARCH_CONVERT);
931 dst->d_rtbwarns = INT_GET(src->d_rtbwarns, ARCH_CONVERT);
934 * Internally, we don't reset all the timers when quota enforcement
935 * gets turned off. No need to confuse the userlevel code,
936 * so return zeroes in that case.
938 if (! XFS_IS_QUOTA_ENFORCED(mp)) {
939 dst->d_btimer = 0;
940 dst->d_itimer = 0;
941 dst->d_rtbtimer = 0;
944 #ifdef DEBUG
945 if (XFS_IS_QUOTA_ENFORCED(mp) && dst->d_id != 0) {
946 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
947 (dst->d_blk_softlimit > 0)) {
948 ASSERT(dst->d_btimer != 0);
950 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
951 (dst->d_ino_softlimit > 0)) {
952 ASSERT(dst->d_itimer != 0);
955 #endif
958 STATIC uint
959 xfs_qm_import_qtype_flags(
960 uint uflags)
962 uint oflags = 0;
965 * Can't be more than one, or none.
967 if (((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ==
968 (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ||
969 ((uflags & (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ==
970 (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ||
971 ((uflags & (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ==
972 (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ||
973 ((uflags & (XFS_GROUP_QUOTA|XFS_USER_QUOTA|XFS_PROJ_QUOTA)) == 0))
974 return (0);
976 oflags |= (uflags & XFS_USER_QUOTA) ? XFS_DQ_USER : 0;
977 oflags |= (uflags & XFS_PROJ_QUOTA) ? XFS_DQ_PROJ : 0;
978 oflags |= (uflags & XFS_GROUP_QUOTA) ? XFS_DQ_GROUP: 0;
979 return oflags;
982 STATIC uint
983 xfs_qm_export_qtype_flags(
984 uint flags)
987 * Can't be more than one, or none.
989 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
990 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
991 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
992 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
993 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
994 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
995 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
997 return (flags & XFS_DQ_USER) ?
998 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
999 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
1002 STATIC uint
1003 xfs_qm_import_flags(
1004 uint uflags)
1006 uint flags = 0;
1008 if (uflags & XFS_QUOTA_UDQ_ACCT)
1009 flags |= XFS_UQUOTA_ACCT;
1010 if (uflags & XFS_QUOTA_PDQ_ACCT)
1011 flags |= XFS_PQUOTA_ACCT;
1012 if (uflags & XFS_QUOTA_GDQ_ACCT)
1013 flags |= XFS_GQUOTA_ACCT;
1014 if (uflags & XFS_QUOTA_UDQ_ENFD)
1015 flags |= XFS_UQUOTA_ENFD;
1016 if (uflags & (XFS_QUOTA_PDQ_ENFD|XFS_QUOTA_GDQ_ENFD))
1017 flags |= XFS_OQUOTA_ENFD;
1018 return (flags);
1022 STATIC uint
1023 xfs_qm_export_flags(
1024 uint flags)
1026 uint uflags;
1028 uflags = 0;
1029 if (flags & XFS_UQUOTA_ACCT)
1030 uflags |= XFS_QUOTA_UDQ_ACCT;
1031 if (flags & XFS_PQUOTA_ACCT)
1032 uflags |= XFS_QUOTA_PDQ_ACCT;
1033 if (flags & XFS_GQUOTA_ACCT)
1034 uflags |= XFS_QUOTA_GDQ_ACCT;
1035 if (flags & XFS_UQUOTA_ENFD)
1036 uflags |= XFS_QUOTA_UDQ_ENFD;
1037 if (flags & (XFS_OQUOTA_ENFD)) {
1038 uflags |= (flags & XFS_GQUOTA_ACCT) ?
1039 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
1041 return (uflags);
1046 * Go thru all the inodes in the file system, releasing their dquots.
1047 * Note that the mount structure gets modified to indicate that quotas are off
1048 * AFTER this, in the case of quotaoff. This also gets called from
1049 * xfs_rootumount.
1051 void
1052 xfs_qm_dqrele_all_inodes(
1053 struct xfs_mount *mp,
1054 uint flags)
1056 vmap_t vmap;
1057 xfs_inode_t *ip, *topino;
1058 uint ireclaims;
1059 vnode_t *vp;
1060 boolean_t vnode_refd;
1062 ASSERT(mp->m_quotainfo);
1064 again:
1065 XFS_MOUNT_ILOCK(mp);
1066 ip = mp->m_inodes;
1067 if (ip == NULL) {
1068 XFS_MOUNT_IUNLOCK(mp);
1069 return;
1071 do {
1072 /* Skip markers inserted by xfs_sync */
1073 if (ip->i_mount == NULL) {
1074 ip = ip->i_mnext;
1075 continue;
1077 /* Root inode, rbmip and rsumip have associated blocks */
1078 if (ip == XFS_QI_UQIP(mp) || ip == XFS_QI_GQIP(mp)) {
1079 ASSERT(ip->i_udquot == NULL);
1080 ASSERT(ip->i_gdquot == NULL);
1081 ip = ip->i_mnext;
1082 continue;
1084 vp = XFS_ITOV_NULL(ip);
1085 if (!vp) {
1086 ASSERT(ip->i_udquot == NULL);
1087 ASSERT(ip->i_gdquot == NULL);
1088 ip = ip->i_mnext;
1089 continue;
1091 vnode_refd = B_FALSE;
1092 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1094 * Sample vp mapping while holding the mplock, lest
1095 * we come across a non-existent vnode.
1097 VMAP(vp, vmap);
1098 ireclaims = mp->m_ireclaims;
1099 topino = mp->m_inodes;
1100 XFS_MOUNT_IUNLOCK(mp);
1102 /* XXX restart limit ? */
1103 if ( ! (vp = vn_get(vp, &vmap)))
1104 goto again;
1105 xfs_ilock(ip, XFS_ILOCK_EXCL);
1106 vnode_refd = B_TRUE;
1107 } else {
1108 ireclaims = mp->m_ireclaims;
1109 topino = mp->m_inodes;
1110 XFS_MOUNT_IUNLOCK(mp);
1114 * We don't keep the mountlock across the dqrele() call,
1115 * since it can take a while..
1117 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
1118 xfs_qm_dqrele(ip->i_udquot);
1119 ip->i_udquot = NULL;
1121 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
1122 xfs_qm_dqrele(ip->i_gdquot);
1123 ip->i_gdquot = NULL;
1125 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1127 * Wait until we've dropped the ilock and mountlock to
1128 * do the vn_rele. Or be condemned to an eternity in the
1129 * inactive code in hell.
1131 if (vnode_refd)
1132 VN_RELE(vp);
1133 XFS_MOUNT_ILOCK(mp);
1135 * If an inode was inserted or removed, we gotta
1136 * start over again.
1138 if (topino != mp->m_inodes || mp->m_ireclaims != ireclaims) {
1139 /* XXX use a sentinel */
1140 XFS_MOUNT_IUNLOCK(mp);
1141 goto again;
1143 ip = ip->i_mnext;
1144 } while (ip != mp->m_inodes);
1146 XFS_MOUNT_IUNLOCK(mp);
1149 /*------------------------------------------------------------------------*/
1150 #ifdef DEBUG
1152 * This contains all the test functions for XFS disk quotas.
1153 * Currently it does a quota accounting check. ie. it walks through
1154 * all inodes in the file system, calculating the dquot accounting fields,
1155 * and prints out any inconsistencies.
1157 xfs_dqhash_t *qmtest_udqtab;
1158 xfs_dqhash_t *qmtest_gdqtab;
1159 int qmtest_hashmask;
1160 int qmtest_nfails;
1161 mutex_t qcheck_lock;
1163 #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
1164 (__psunsigned_t)(id)) & \
1165 (qmtest_hashmask - 1))
1167 #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
1168 (qmtest_udqtab + \
1169 DQTEST_HASHVAL(mp, id)) : \
1170 (qmtest_gdqtab + \
1171 DQTEST_HASHVAL(mp, id)))
1173 #define DQTEST_LIST_PRINT(l, NXT, title) \
1175 xfs_dqtest_t *dqp; int i = 0;\
1176 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
1177 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
1178 dqp = (xfs_dqtest_t *)dqp->NXT) { \
1179 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
1180 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
1181 dqp->d_bcount, dqp->d_icount); } \
1184 typedef struct dqtest {
1185 xfs_dqmarker_t q_lists;
1186 xfs_dqhash_t *q_hash; /* the hashchain header */
1187 xfs_mount_t *q_mount; /* filesystem this relates to */
1188 xfs_dqid_t d_id; /* user id or group id */
1189 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
1190 xfs_qcnt_t d_icount; /* # inodes owned by the user */
1191 } xfs_dqtest_t;
1193 STATIC void
1194 xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
1196 xfs_dquot_t *d;
1197 if (((d) = (h)->qh_next))
1198 (d)->HL_PREVP = &((dqp)->HL_NEXT);
1199 (dqp)->HL_NEXT = d;
1200 (dqp)->HL_PREVP = &((h)->qh_next);
1201 (h)->qh_next = (xfs_dquot_t *)dqp;
1202 (h)->qh_version++;
1203 (h)->qh_nelems++;
1205 STATIC void
1206 xfs_qm_dqtest_print(
1207 xfs_dqtest_t *d)
1209 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
1210 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
1211 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
1212 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1213 d->d_bcount, (int)d->d_bcount);
1214 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1215 d->d_icount, (int)d->d_icount);
1216 cmn_err(CE_DEBUG, "---------------------------");
1219 STATIC void
1220 xfs_qm_dqtest_failed(
1221 xfs_dqtest_t *d,
1222 xfs_dquot_t *dqp,
1223 char *reason,
1224 xfs_qcnt_t a,
1225 xfs_qcnt_t b,
1226 int error)
1228 qmtest_nfails++;
1229 if (error)
1230 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
1231 INT_GET(d->d_id, ARCH_CONVERT), error, reason);
1232 else
1233 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
1234 INT_GET(d->d_id, ARCH_CONVERT), reason, (int)a, (int)b);
1235 xfs_qm_dqtest_print(d);
1236 if (dqp)
1237 xfs_qm_dqprint(dqp);
1240 STATIC int
1241 xfs_dqtest_cmp2(
1242 xfs_dqtest_t *d,
1243 xfs_dquot_t *dqp)
1245 int err = 0;
1246 if (INT_GET(dqp->q_core.d_icount, ARCH_CONVERT) != d->d_icount) {
1247 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
1248 INT_GET(dqp->q_core.d_icount, ARCH_CONVERT),
1249 d->d_icount, 0);
1250 err++;
1252 if (INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT) != d->d_bcount) {
1253 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
1254 INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT),
1255 d->d_bcount, 0);
1256 err++;
1258 if (INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT) &&
1259 INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT) >=
1260 INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT)) {
1261 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1262 cmn_err(CE_DEBUG,
1263 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1264 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1265 err++;
1268 if (INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT) &&
1269 INT_GET(dqp->q_core.d_icount, ARCH_CONVERT) >=
1270 INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT)) {
1271 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1272 cmn_err(CE_DEBUG,
1273 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1274 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1275 err++;
1278 #ifdef QUOTADEBUG
1279 if (!err) {
1280 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
1281 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1283 #endif
1284 return (err);
1287 STATIC void
1288 xfs_dqtest_cmp(
1289 xfs_dqtest_t *d)
1291 xfs_dquot_t *dqp;
1292 int error;
1294 /* xfs_qm_dqtest_print(d); */
1295 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1296 &dqp))) {
1297 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1298 return;
1300 xfs_dqtest_cmp2(d, dqp);
1301 xfs_qm_dqput(dqp);
1304 STATIC int
1305 xfs_qm_internalqcheck_dqget(
1306 xfs_mount_t *mp,
1307 xfs_dqid_t id,
1308 uint type,
1309 xfs_dqtest_t **O_dq)
1311 xfs_dqtest_t *d;
1312 xfs_dqhash_t *h;
1314 h = DQTEST_HASH(mp, id, type);
1315 for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
1316 d = (xfs_dqtest_t *) d->HL_NEXT) {
1317 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1318 if (d->d_id == id && mp == d->q_mount) {
1319 *O_dq = d;
1320 return (0);
1323 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1324 d->dq_flags = type;
1325 d->d_id = id;
1326 d->q_mount = mp;
1327 d->q_hash = h;
1328 xfs_qm_hashinsert(h, d);
1329 *O_dq = d;
1330 return (0);
1333 STATIC void
1334 xfs_qm_internalqcheck_get_dquots(
1335 xfs_mount_t *mp,
1336 xfs_dqid_t uid,
1337 xfs_dqid_t projid,
1338 xfs_dqid_t gid,
1339 xfs_dqtest_t **ud,
1340 xfs_dqtest_t **gd)
1342 if (XFS_IS_UQUOTA_ON(mp))
1343 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1344 if (XFS_IS_GQUOTA_ON(mp))
1345 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
1346 else if (XFS_IS_PQUOTA_ON(mp))
1347 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
1351 STATIC void
1352 xfs_qm_internalqcheck_dqadjust(
1353 xfs_inode_t *ip,
1354 xfs_dqtest_t *d)
1356 d->d_icount++;
1357 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1360 STATIC int
1361 xfs_qm_internalqcheck_adjust(
1362 xfs_mount_t *mp, /* mount point for filesystem */
1363 xfs_ino_t ino, /* inode number to get data for */
1364 void __user *buffer, /* not used */
1365 int ubsize, /* not used */
1366 void *private_data, /* not used */
1367 xfs_daddr_t bno, /* starting block of inode cluster */
1368 int *ubused, /* not used */
1369 void *dip, /* not used */
1370 int *res) /* bulkstat result code */
1372 xfs_inode_t *ip;
1373 xfs_dqtest_t *ud, *gd;
1374 uint lock_flags;
1375 boolean_t ipreleased;
1376 int error;
1378 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1380 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1381 *res = BULKSTAT_RV_NOTHING;
1382 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1383 (unsigned long long) ino,
1384 (unsigned long long) mp->m_sb.sb_uquotino,
1385 (unsigned long long) mp->m_sb.sb_gquotino);
1386 return XFS_ERROR(EINVAL);
1388 ipreleased = B_FALSE;
1389 again:
1390 lock_flags = XFS_ILOCK_SHARED;
1391 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1392 *res = BULKSTAT_RV_NOTHING;
1393 return (error);
1396 if (ip->i_d.di_mode == 0) {
1397 xfs_iput_new(ip, lock_flags);
1398 *res = BULKSTAT_RV_NOTHING;
1399 return XFS_ERROR(ENOENT);
1403 * This inode can have blocks after eof which can get released
1404 * when we send it to inactive. Since we don't check the dquot
1405 * until the after all our calculations are done, we must get rid
1406 * of those now.
1408 if (! ipreleased) {
1409 xfs_iput(ip, lock_flags);
1410 ipreleased = B_TRUE;
1411 goto again;
1413 xfs_qm_internalqcheck_get_dquots(mp,
1414 (xfs_dqid_t) ip->i_d.di_uid,
1415 (xfs_dqid_t) ip->i_d.di_projid,
1416 (xfs_dqid_t) ip->i_d.di_gid,
1417 &ud, &gd);
1418 if (XFS_IS_UQUOTA_ON(mp)) {
1419 ASSERT(ud);
1420 xfs_qm_internalqcheck_dqadjust(ip, ud);
1422 if (XFS_IS_OQUOTA_ON(mp)) {
1423 ASSERT(gd);
1424 xfs_qm_internalqcheck_dqadjust(ip, gd);
1426 xfs_iput(ip, lock_flags);
1427 *res = BULKSTAT_RV_DIDONE;
1428 return (0);
1432 /* PRIVATE, debugging */
1434 xfs_qm_internalqcheck(
1435 xfs_mount_t *mp)
1437 xfs_ino_t lastino;
1438 int done, count;
1439 int i;
1440 xfs_dqtest_t *d, *e;
1441 xfs_dqhash_t *h1;
1442 int error;
1444 lastino = 0;
1445 qmtest_hashmask = 32;
1446 count = 5;
1447 done = 0;
1448 qmtest_nfails = 0;
1450 if (! XFS_IS_QUOTA_ON(mp))
1451 return XFS_ERROR(ESRCH);
1453 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1454 XFS_bflush(mp->m_ddev_targp);
1455 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1456 XFS_bflush(mp->m_ddev_targp);
1458 mutex_lock(&qcheck_lock, PINOD);
1459 /* There should be absolutely no quota activity while this
1460 is going on. */
1461 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1462 sizeof(xfs_dqhash_t), KM_SLEEP);
1463 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1464 sizeof(xfs_dqhash_t), KM_SLEEP);
1465 do {
1467 * Iterate thru all the inodes in the file system,
1468 * adjusting the corresponding dquot counters
1470 if ((error = xfs_bulkstat(mp, &lastino, &count,
1471 xfs_qm_internalqcheck_adjust, NULL,
1472 0, NULL, BULKSTAT_FG_IGET, &done))) {
1473 break;
1475 } while (! done);
1476 if (error) {
1477 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1479 cmn_err(CE_DEBUG, "Checking results against system dquots");
1480 for (i = 0; i < qmtest_hashmask; i++) {
1481 h1 = &qmtest_udqtab[i];
1482 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1483 xfs_dqtest_cmp(d);
1484 e = (xfs_dqtest_t *) d->HL_NEXT;
1485 kmem_free(d, sizeof(xfs_dqtest_t));
1486 d = e;
1488 h1 = &qmtest_gdqtab[i];
1489 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1490 xfs_dqtest_cmp(d);
1491 e = (xfs_dqtest_t *) d->HL_NEXT;
1492 kmem_free(d, sizeof(xfs_dqtest_t));
1493 d = e;
1497 if (qmtest_nfails) {
1498 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1499 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1500 } else {
1501 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1503 kmem_free(qmtest_udqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1504 kmem_free(qmtest_gdqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1505 mutex_unlock(&qcheck_lock);
1506 return (qmtest_nfails);
1509 #endif /* DEBUG */