move wm8400-regulator's probe function to .devinit.text
[linux-2.6/sactl.git] / fs / xfs / quota / xfs_qm_syscalls.c
blob68139b38aedef0f9e11806b3d69bab1537871a2e
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/capability.h>
21 #include "xfs.h"
22 #include "xfs_fs.h"
23 #include "xfs_bit.h"
24 #include "xfs_log.h"
25 #include "xfs_inum.h"
26 #include "xfs_trans.h"
27 #include "xfs_sb.h"
28 #include "xfs_ag.h"
29 #include "xfs_dir2.h"
30 #include "xfs_alloc.h"
31 #include "xfs_dmapi.h"
32 #include "xfs_quota.h"
33 #include "xfs_mount.h"
34 #include "xfs_bmap_btree.h"
35 #include "xfs_alloc_btree.h"
36 #include "xfs_ialloc_btree.h"
37 #include "xfs_dir2_sf.h"
38 #include "xfs_attr_sf.h"
39 #include "xfs_dinode.h"
40 #include "xfs_inode.h"
41 #include "xfs_ialloc.h"
42 #include "xfs_itable.h"
43 #include "xfs_bmap.h"
44 #include "xfs_btree.h"
45 #include "xfs_rtalloc.h"
46 #include "xfs_error.h"
47 #include "xfs_rw.h"
48 #include "xfs_acl.h"
49 #include "xfs_attr.h"
50 #include "xfs_buf_item.h"
51 #include "xfs_utils.h"
52 #include "xfs_qm.h"
54 #ifdef DEBUG
55 # define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
56 #else
57 # define qdprintk(s, args...) do { } while (0)
58 #endif
60 STATIC int xfs_qm_scall_trunc_qfiles(xfs_mount_t *, uint);
61 STATIC int xfs_qm_scall_getquota(xfs_mount_t *, xfs_dqid_t, uint,
62 fs_disk_quota_t *);
63 STATIC int xfs_qm_scall_getqstat(xfs_mount_t *, fs_quota_stat_t *);
64 STATIC int xfs_qm_scall_setqlim(xfs_mount_t *, xfs_dqid_t, uint,
65 fs_disk_quota_t *);
66 STATIC int xfs_qm_scall_quotaon(xfs_mount_t *, uint);
67 STATIC int xfs_qm_scall_quotaoff(xfs_mount_t *, uint, boolean_t);
68 STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
69 STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
70 uint);
71 STATIC uint xfs_qm_import_flags(uint);
72 STATIC uint xfs_qm_export_flags(uint);
73 STATIC uint xfs_qm_import_qtype_flags(uint);
74 STATIC uint xfs_qm_export_qtype_flags(uint);
75 STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
76 fs_disk_quota_t *);
80 * The main distribution switch of all XFS quotactl system calls.
82 int
83 xfs_qm_quotactl(
84 xfs_mount_t *mp,
85 int cmd,
86 int id,
87 xfs_caddr_t addr)
89 int error;
91 ASSERT(addr != NULL || cmd == Q_XQUOTASYNC);
94 * The following commands are valid even when quotaoff.
96 switch (cmd) {
97 case Q_XQUOTARM:
99 * Truncate quota files. quota must be off.
101 if (XFS_IS_QUOTA_ON(mp))
102 return XFS_ERROR(EINVAL);
103 if (mp->m_flags & XFS_MOUNT_RDONLY)
104 return XFS_ERROR(EROFS);
105 return (xfs_qm_scall_trunc_qfiles(mp,
106 xfs_qm_import_qtype_flags(*(uint *)addr)));
108 case Q_XGETQSTAT:
110 * Get quota status information.
112 return (xfs_qm_scall_getqstat(mp, (fs_quota_stat_t *)addr));
114 case Q_XQUOTAON:
116 * QUOTAON - enabling quota enforcement.
117 * Quota accounting must be turned on at mount time.
119 if (mp->m_flags & XFS_MOUNT_RDONLY)
120 return XFS_ERROR(EROFS);
121 return (xfs_qm_scall_quotaon(mp,
122 xfs_qm_import_flags(*(uint *)addr)));
124 case Q_XQUOTAOFF:
125 if (mp->m_flags & XFS_MOUNT_RDONLY)
126 return XFS_ERROR(EROFS);
127 break;
129 case Q_XQUOTASYNC:
130 return xfs_sync_inodes(mp, SYNC_DELWRI);
132 default:
133 break;
136 if (! XFS_IS_QUOTA_ON(mp))
137 return XFS_ERROR(ESRCH);
139 switch (cmd) {
140 case Q_XQUOTAOFF:
141 if (mp->m_flags & XFS_MOUNT_RDONLY)
142 return XFS_ERROR(EROFS);
143 error = xfs_qm_scall_quotaoff(mp,
144 xfs_qm_import_flags(*(uint *)addr),
145 B_FALSE);
146 break;
148 case Q_XGETQUOTA:
149 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_USER,
150 (fs_disk_quota_t *)addr);
151 break;
152 case Q_XGETGQUOTA:
153 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
154 (fs_disk_quota_t *)addr);
155 break;
156 case Q_XGETPQUOTA:
157 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
158 (fs_disk_quota_t *)addr);
159 break;
161 case Q_XSETQLIM:
162 if (mp->m_flags & XFS_MOUNT_RDONLY)
163 return XFS_ERROR(EROFS);
164 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_USER,
165 (fs_disk_quota_t *)addr);
166 break;
167 case Q_XSETGQLIM:
168 if (mp->m_flags & XFS_MOUNT_RDONLY)
169 return XFS_ERROR(EROFS);
170 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
171 (fs_disk_quota_t *)addr);
172 break;
173 case Q_XSETPQLIM:
174 if (mp->m_flags & XFS_MOUNT_RDONLY)
175 return XFS_ERROR(EROFS);
176 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
177 (fs_disk_quota_t *)addr);
178 break;
180 default:
181 error = XFS_ERROR(EINVAL);
182 break;
185 return (error);
189 * Turn off quota accounting and/or enforcement for all udquots and/or
190 * gdquots. Called only at unmount time.
192 * This assumes that there are no dquots of this file system cached
193 * incore, and modifies the ondisk dquot directly. Therefore, for example,
194 * it is an error to call this twice, without purging the cache.
196 STATIC int
197 xfs_qm_scall_quotaoff(
198 xfs_mount_t *mp,
199 uint flags,
200 boolean_t force)
202 uint dqtype;
203 int error;
204 uint inactivate_flags;
205 xfs_qoff_logitem_t *qoffstart;
206 int nculprits;
208 if (!force && !capable(CAP_SYS_ADMIN))
209 return XFS_ERROR(EPERM);
211 * No file system can have quotas enabled on disk but not in core.
212 * Note that quota utilities (like quotaoff) _expect_
213 * errno == EEXIST here.
215 if ((mp->m_qflags & flags) == 0)
216 return XFS_ERROR(EEXIST);
217 error = 0;
219 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
222 * We don't want to deal with two quotaoffs messing up each other,
223 * so we're going to serialize it. quotaoff isn't exactly a performance
224 * critical thing.
225 * If quotaoff, then we must be dealing with the root filesystem.
227 ASSERT(mp->m_quotainfo);
228 if (mp->m_quotainfo)
229 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
231 ASSERT(mp->m_quotainfo);
234 * If we're just turning off quota enforcement, change mp and go.
236 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
237 mp->m_qflags &= ~(flags);
239 spin_lock(&mp->m_sb_lock);
240 mp->m_sb.sb_qflags = mp->m_qflags;
241 spin_unlock(&mp->m_sb_lock);
242 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
244 /* XXX what to do if error ? Revert back to old vals incore ? */
245 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
246 return (error);
249 dqtype = 0;
250 inactivate_flags = 0;
252 * If accounting is off, we must turn enforcement off, clear the
253 * quota 'CHKD' certificate to make it known that we have to
254 * do a quotacheck the next time this quota is turned on.
256 if (flags & XFS_UQUOTA_ACCT) {
257 dqtype |= XFS_QMOPT_UQUOTA;
258 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
259 inactivate_flags |= XFS_UQUOTA_ACTIVE;
261 if (flags & XFS_GQUOTA_ACCT) {
262 dqtype |= XFS_QMOPT_GQUOTA;
263 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
264 inactivate_flags |= XFS_GQUOTA_ACTIVE;
265 } else if (flags & XFS_PQUOTA_ACCT) {
266 dqtype |= XFS_QMOPT_PQUOTA;
267 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
268 inactivate_flags |= XFS_PQUOTA_ACTIVE;
272 * Nothing to do? Don't complain. This happens when we're just
273 * turning off quota enforcement.
275 if ((mp->m_qflags & flags) == 0) {
276 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
277 return (0);
281 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
282 * and synchronously. If we fail to write, we should abort the
283 * operation as it cannot be recovered safely if we crash.
285 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
286 if (error)
287 goto out_error;
290 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
291 * to take care of the race between dqget and quotaoff. We don't take
292 * any special locks to reset these bits. All processes need to check
293 * these bits *after* taking inode lock(s) to see if the particular
294 * quota type is in the process of being turned off. If *ACTIVE, it is
295 * guaranteed that all dquot structures and all quotainode ptrs will all
296 * stay valid as long as that inode is kept locked.
298 * There is no turning back after this.
300 mp->m_qflags &= ~inactivate_flags;
303 * Give back all the dquot reference(s) held by inodes.
304 * Here we go thru every single incore inode in this file system, and
305 * do a dqrele on the i_udquot/i_gdquot that it may have.
306 * Essentially, as long as somebody has an inode locked, this guarantees
307 * that quotas will not be turned off. This is handy because in a
308 * transaction once we lock the inode(s) and check for quotaon, we can
309 * depend on the quota inodes (and other things) being valid as long as
310 * we keep the lock(s).
312 xfs_qm_dqrele_all_inodes(mp, flags);
315 * Next we make the changes in the quota flag in the mount struct.
316 * This isn't protected by a particular lock directly, because we
317 * don't want to take a mrlock everytime we depend on quotas being on.
319 mp->m_qflags &= ~(flags);
322 * Go through all the dquots of this file system and purge them,
323 * according to what was turned off. We may not be able to get rid
324 * of all dquots, because dquots can have temporary references that
325 * are not attached to inodes. eg. xfs_setattr, xfs_create.
326 * So, if we couldn't purge all the dquots from the filesystem,
327 * we can't get rid of the incore data structures.
329 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
330 delay(10 * nculprits);
333 * Transactions that had started before ACTIVE state bit was cleared
334 * could have logged many dquots, so they'd have higher LSNs than
335 * the first QUOTAOFF log record does. If we happen to crash when
336 * the tail of the log has gone past the QUOTAOFF record, but
337 * before the last dquot modification, those dquots __will__
338 * recover, and that's not good.
340 * So, we have QUOTAOFF start and end logitems; the start
341 * logitem won't get overwritten until the end logitem appears...
343 error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
344 if (error) {
345 /* We're screwed now. Shutdown is the only option. */
346 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
347 goto out_error;
351 * If quotas is completely disabled, close shop.
353 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
354 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
355 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
356 xfs_qm_destroy_quotainfo(mp);
357 return (0);
361 * Release our quotainode references, and vn_purge them,
362 * if we don't need them anymore.
364 if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
365 IRELE(XFS_QI_UQIP(mp));
366 XFS_QI_UQIP(mp) = NULL;
368 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
369 IRELE(XFS_QI_GQIP(mp));
370 XFS_QI_GQIP(mp) = NULL;
372 out_error:
373 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
375 return (error);
378 STATIC int
379 xfs_qm_scall_trunc_qfiles(
380 xfs_mount_t *mp,
381 uint flags)
383 int error = 0, error2 = 0;
384 xfs_inode_t *qip;
386 if (!capable(CAP_SYS_ADMIN))
387 return XFS_ERROR(EPERM);
388 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) {
389 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
390 return XFS_ERROR(EINVAL);
393 if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
394 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
395 if (!error) {
396 error = xfs_truncate_file(mp, qip);
397 IRELE(qip);
401 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
402 mp->m_sb.sb_gquotino != NULLFSINO) {
403 error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
404 if (!error2) {
405 error2 = xfs_truncate_file(mp, qip);
406 IRELE(qip);
410 return error ? error : error2;
415 * Switch on (a given) quota enforcement for a filesystem. This takes
416 * effect immediately.
417 * (Switching on quota accounting must be done at mount time.)
419 STATIC int
420 xfs_qm_scall_quotaon(
421 xfs_mount_t *mp,
422 uint flags)
424 int error;
425 uint qf;
426 uint accflags;
427 __int64_t sbflags;
429 if (!capable(CAP_SYS_ADMIN))
430 return XFS_ERROR(EPERM);
432 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
434 * Switching on quota accounting must be done at mount time.
436 accflags = flags & XFS_ALL_QUOTA_ACCT;
437 flags &= ~(XFS_ALL_QUOTA_ACCT);
439 sbflags = 0;
441 if (flags == 0) {
442 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
443 return XFS_ERROR(EINVAL);
446 /* No fs can turn on quotas with a delayed effect */
447 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
450 * Can't enforce without accounting. We check the superblock
451 * qflags here instead of m_qflags because rootfs can have
452 * quota acct on ondisk without m_qflags' knowing.
454 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
455 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
456 (flags & XFS_UQUOTA_ENFD))
458 ((flags & XFS_PQUOTA_ACCT) == 0 &&
459 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
460 (flags & XFS_GQUOTA_ACCT) == 0 &&
461 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
462 (flags & XFS_OQUOTA_ENFD))) {
463 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
464 flags, mp->m_sb.sb_qflags);
465 return XFS_ERROR(EINVAL);
468 * If everything's upto-date incore, then don't waste time.
470 if ((mp->m_qflags & flags) == flags)
471 return XFS_ERROR(EEXIST);
474 * Change sb_qflags on disk but not incore mp->qflags
475 * if this is the root filesystem.
477 spin_lock(&mp->m_sb_lock);
478 qf = mp->m_sb.sb_qflags;
479 mp->m_sb.sb_qflags = qf | flags;
480 spin_unlock(&mp->m_sb_lock);
483 * There's nothing to change if it's the same.
485 if ((qf & flags) == flags && sbflags == 0)
486 return XFS_ERROR(EEXIST);
487 sbflags |= XFS_SB_QFLAGS;
489 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
490 return (error);
492 * If we aren't trying to switch on quota enforcement, we are done.
494 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
495 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
496 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
497 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
498 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
499 (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
500 (flags & XFS_ALL_QUOTA_ENFD) == 0)
501 return (0);
503 if (! XFS_IS_QUOTA_RUNNING(mp))
504 return XFS_ERROR(ESRCH);
507 * Switch on quota enforcement in core.
509 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
510 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
511 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
513 return (0);
518 * Return quota status information, such as uquota-off, enforcements, etc.
520 STATIC int
521 xfs_qm_scall_getqstat(
522 xfs_mount_t *mp,
523 fs_quota_stat_t *out)
525 xfs_inode_t *uip, *gip;
526 boolean_t tempuqip, tempgqip;
528 uip = gip = NULL;
529 tempuqip = tempgqip = B_FALSE;
530 memset(out, 0, sizeof(fs_quota_stat_t));
532 out->qs_version = FS_QSTAT_VERSION;
533 if (!xfs_sb_version_hasquota(&mp->m_sb)) {
534 out->qs_uquota.qfs_ino = NULLFSINO;
535 out->qs_gquota.qfs_ino = NULLFSINO;
536 return (0);
538 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
539 (XFS_ALL_QUOTA_ACCT|
540 XFS_ALL_QUOTA_ENFD));
541 out->qs_pad = 0;
542 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
543 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
545 if (mp->m_quotainfo) {
546 uip = mp->m_quotainfo->qi_uquotaip;
547 gip = mp->m_quotainfo->qi_gquotaip;
549 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
550 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
551 0, 0, &uip, 0) == 0)
552 tempuqip = B_TRUE;
554 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
555 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
556 0, 0, &gip, 0) == 0)
557 tempgqip = B_TRUE;
559 if (uip) {
560 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
561 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
562 if (tempuqip)
563 IRELE(uip);
565 if (gip) {
566 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
567 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
568 if (tempgqip)
569 IRELE(gip);
571 if (mp->m_quotainfo) {
572 out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
573 out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
574 out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
575 out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
576 out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
577 out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
579 return (0);
583 * Adjust quota limits, and start/stop timers accordingly.
585 STATIC int
586 xfs_qm_scall_setqlim(
587 xfs_mount_t *mp,
588 xfs_dqid_t id,
589 uint type,
590 fs_disk_quota_t *newlim)
592 xfs_disk_dquot_t *ddq;
593 xfs_dquot_t *dqp;
594 xfs_trans_t *tp;
595 int error;
596 xfs_qcnt_t hard, soft;
598 if (!capable(CAP_SYS_ADMIN))
599 return XFS_ERROR(EPERM);
601 if ((newlim->d_fieldmask &
602 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
603 return (0);
605 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
606 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
607 0, 0, XFS_DEFAULT_LOG_COUNT))) {
608 xfs_trans_cancel(tp, 0);
609 return (error);
613 * We don't want to race with a quotaoff so take the quotaoff lock.
614 * (We don't hold an inode lock, so there's nothing else to stop
615 * a quotaoff from happening). (XXXThis doesn't currently happen
616 * because we take the vfslock before calling xfs_qm_sysent).
618 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
621 * Get the dquot (locked), and join it to the transaction.
622 * Allocate the dquot if this doesn't exist.
624 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
625 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
626 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
627 ASSERT(error != ENOENT);
628 return (error);
630 xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
631 xfs_trans_dqjoin(tp, dqp);
632 ddq = &dqp->q_core;
635 * Make sure that hardlimits are >= soft limits before changing.
637 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
638 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
639 be64_to_cpu(ddq->d_blk_hardlimit);
640 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
641 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
642 be64_to_cpu(ddq->d_blk_softlimit);
643 if (hard == 0 || hard >= soft) {
644 ddq->d_blk_hardlimit = cpu_to_be64(hard);
645 ddq->d_blk_softlimit = cpu_to_be64(soft);
646 if (id == 0) {
647 mp->m_quotainfo->qi_bhardlimit = hard;
648 mp->m_quotainfo->qi_bsoftlimit = soft;
650 } else {
651 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
653 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
654 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
655 be64_to_cpu(ddq->d_rtb_hardlimit);
656 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
657 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
658 be64_to_cpu(ddq->d_rtb_softlimit);
659 if (hard == 0 || hard >= soft) {
660 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
661 ddq->d_rtb_softlimit = cpu_to_be64(soft);
662 if (id == 0) {
663 mp->m_quotainfo->qi_rtbhardlimit = hard;
664 mp->m_quotainfo->qi_rtbsoftlimit = soft;
666 } else {
667 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
670 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
671 (xfs_qcnt_t) newlim->d_ino_hardlimit :
672 be64_to_cpu(ddq->d_ino_hardlimit);
673 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
674 (xfs_qcnt_t) newlim->d_ino_softlimit :
675 be64_to_cpu(ddq->d_ino_softlimit);
676 if (hard == 0 || hard >= soft) {
677 ddq->d_ino_hardlimit = cpu_to_be64(hard);
678 ddq->d_ino_softlimit = cpu_to_be64(soft);
679 if (id == 0) {
680 mp->m_quotainfo->qi_ihardlimit = hard;
681 mp->m_quotainfo->qi_isoftlimit = soft;
683 } else {
684 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
688 * Update warnings counter(s) if requested
690 if (newlim->d_fieldmask & FS_DQ_BWARNS)
691 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
692 if (newlim->d_fieldmask & FS_DQ_IWARNS)
693 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
694 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
695 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
697 if (id == 0) {
699 * Timelimits for the super user set the relative time
700 * the other users can be over quota for this file system.
701 * If it is zero a default is used. Ditto for the default
702 * soft and hard limit values (already done, above), and
703 * for warnings.
705 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
706 mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
707 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
709 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
710 mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
711 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
713 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
714 mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
715 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
717 if (newlim->d_fieldmask & FS_DQ_BWARNS)
718 mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
719 if (newlim->d_fieldmask & FS_DQ_IWARNS)
720 mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
721 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
722 mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
723 } else {
725 * If the user is now over quota, start the timelimit.
726 * The user will not be 'warned'.
727 * Note that we keep the timers ticking, whether enforcement
728 * is on or off. We don't really want to bother with iterating
729 * over all ondisk dquots and turning the timers on/off.
731 xfs_qm_adjust_dqtimers(mp, ddq);
733 dqp->dq_flags |= XFS_DQ_DIRTY;
734 xfs_trans_log_dquot(tp, dqp);
736 xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
737 error = xfs_trans_commit(tp, 0);
738 xfs_qm_dqprint(dqp);
739 xfs_qm_dqrele(dqp);
740 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
742 return error;
745 STATIC int
746 xfs_qm_scall_getquota(
747 xfs_mount_t *mp,
748 xfs_dqid_t id,
749 uint type,
750 fs_disk_quota_t *out)
752 xfs_dquot_t *dqp;
753 int error;
756 * Try to get the dquot. We don't want it allocated on disk, so
757 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
758 * exist, we'll get ENOENT back.
760 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
761 return (error);
764 xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
766 * If everything's NULL, this dquot doesn't quite exist as far as
767 * our utility programs are concerned.
769 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
770 xfs_qm_dqput(dqp);
771 return XFS_ERROR(ENOENT);
773 /* xfs_qm_dqprint(dqp); */
775 * Convert the disk dquot to the exportable format
777 xfs_qm_export_dquot(mp, &dqp->q_core, out);
778 xfs_qm_dqput(dqp);
779 return (error ? XFS_ERROR(EFAULT) : 0);
783 STATIC int
784 xfs_qm_log_quotaoff_end(
785 xfs_mount_t *mp,
786 xfs_qoff_logitem_t *startqoff,
787 uint flags)
789 xfs_trans_t *tp;
790 int error;
791 xfs_qoff_logitem_t *qoffi;
793 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
795 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
796 0, 0, XFS_DEFAULT_LOG_COUNT))) {
797 xfs_trans_cancel(tp, 0);
798 return (error);
801 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
802 flags & XFS_ALL_QUOTA_ACCT);
803 xfs_trans_log_quotaoff_item(tp, qoffi);
806 * We have to make sure that the transaction is secure on disk before we
807 * return and actually stop quota accounting. So, make it synchronous.
808 * We don't care about quotoff's performance.
810 xfs_trans_set_sync(tp);
811 error = xfs_trans_commit(tp, 0);
812 return (error);
816 STATIC int
817 xfs_qm_log_quotaoff(
818 xfs_mount_t *mp,
819 xfs_qoff_logitem_t **qoffstartp,
820 uint flags)
822 xfs_trans_t *tp;
823 int error;
824 xfs_qoff_logitem_t *qoffi=NULL;
825 uint oldsbqflag=0;
827 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
828 if ((error = xfs_trans_reserve(tp, 0,
829 sizeof(xfs_qoff_logitem_t) * 2 +
830 mp->m_sb.sb_sectsize + 128,
833 XFS_DEFAULT_LOG_COUNT))) {
834 goto error0;
837 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
838 xfs_trans_log_quotaoff_item(tp, qoffi);
840 spin_lock(&mp->m_sb_lock);
841 oldsbqflag = mp->m_sb.sb_qflags;
842 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
843 spin_unlock(&mp->m_sb_lock);
845 xfs_mod_sb(tp, XFS_SB_QFLAGS);
848 * We have to make sure that the transaction is secure on disk before we
849 * return and actually stop quota accounting. So, make it synchronous.
850 * We don't care about quotoff's performance.
852 xfs_trans_set_sync(tp);
853 error = xfs_trans_commit(tp, 0);
855 error0:
856 if (error) {
857 xfs_trans_cancel(tp, 0);
859 * No one else is modifying sb_qflags, so this is OK.
860 * We still hold the quotaofflock.
862 spin_lock(&mp->m_sb_lock);
863 mp->m_sb.sb_qflags = oldsbqflag;
864 spin_unlock(&mp->m_sb_lock);
866 *qoffstartp = qoffi;
867 return (error);
872 * Translate an internal style on-disk-dquot to the exportable format.
873 * The main differences are that the counters/limits are all in Basic
874 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
875 * to be converted to the native endianness.
877 STATIC void
878 xfs_qm_export_dquot(
879 xfs_mount_t *mp,
880 xfs_disk_dquot_t *src,
881 struct fs_disk_quota *dst)
883 memset(dst, 0, sizeof(*dst));
884 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
885 dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
886 dst->d_id = be32_to_cpu(src->d_id);
887 dst->d_blk_hardlimit =
888 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
889 dst->d_blk_softlimit =
890 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
891 dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
892 dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
893 dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
894 dst->d_icount = be64_to_cpu(src->d_icount);
895 dst->d_btimer = be32_to_cpu(src->d_btimer);
896 dst->d_itimer = be32_to_cpu(src->d_itimer);
897 dst->d_iwarns = be16_to_cpu(src->d_iwarns);
898 dst->d_bwarns = be16_to_cpu(src->d_bwarns);
899 dst->d_rtb_hardlimit =
900 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
901 dst->d_rtb_softlimit =
902 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
903 dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
904 dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
905 dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
908 * Internally, we don't reset all the timers when quota enforcement
909 * gets turned off. No need to confuse the user level code,
910 * so return zeroes in that case.
912 if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
913 (!XFS_IS_OQUOTA_ENFORCED(mp) &&
914 (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
915 dst->d_btimer = 0;
916 dst->d_itimer = 0;
917 dst->d_rtbtimer = 0;
920 #ifdef DEBUG
921 if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
922 (XFS_IS_OQUOTA_ENFORCED(mp) &&
923 (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
924 dst->d_id != 0) {
925 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
926 (dst->d_blk_softlimit > 0)) {
927 ASSERT(dst->d_btimer != 0);
929 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
930 (dst->d_ino_softlimit > 0)) {
931 ASSERT(dst->d_itimer != 0);
934 #endif
937 STATIC uint
938 xfs_qm_import_qtype_flags(
939 uint uflags)
941 uint oflags = 0;
944 * Can't be more than one, or none.
946 if (((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ==
947 (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ||
948 ((uflags & (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ==
949 (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ||
950 ((uflags & (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ==
951 (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ||
952 ((uflags & (XFS_GROUP_QUOTA|XFS_USER_QUOTA|XFS_PROJ_QUOTA)) == 0))
953 return (0);
955 oflags |= (uflags & XFS_USER_QUOTA) ? XFS_DQ_USER : 0;
956 oflags |= (uflags & XFS_PROJ_QUOTA) ? XFS_DQ_PROJ : 0;
957 oflags |= (uflags & XFS_GROUP_QUOTA) ? XFS_DQ_GROUP: 0;
958 return oflags;
961 STATIC uint
962 xfs_qm_export_qtype_flags(
963 uint flags)
966 * Can't be more than one, or none.
968 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
969 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
970 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
971 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
972 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
973 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
974 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
976 return (flags & XFS_DQ_USER) ?
977 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
978 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
981 STATIC uint
982 xfs_qm_import_flags(
983 uint uflags)
985 uint flags = 0;
987 if (uflags & XFS_QUOTA_UDQ_ACCT)
988 flags |= XFS_UQUOTA_ACCT;
989 if (uflags & XFS_QUOTA_PDQ_ACCT)
990 flags |= XFS_PQUOTA_ACCT;
991 if (uflags & XFS_QUOTA_GDQ_ACCT)
992 flags |= XFS_GQUOTA_ACCT;
993 if (uflags & XFS_QUOTA_UDQ_ENFD)
994 flags |= XFS_UQUOTA_ENFD;
995 if (uflags & (XFS_QUOTA_PDQ_ENFD|XFS_QUOTA_GDQ_ENFD))
996 flags |= XFS_OQUOTA_ENFD;
997 return (flags);
1001 STATIC uint
1002 xfs_qm_export_flags(
1003 uint flags)
1005 uint uflags;
1007 uflags = 0;
1008 if (flags & XFS_UQUOTA_ACCT)
1009 uflags |= XFS_QUOTA_UDQ_ACCT;
1010 if (flags & XFS_PQUOTA_ACCT)
1011 uflags |= XFS_QUOTA_PDQ_ACCT;
1012 if (flags & XFS_GQUOTA_ACCT)
1013 uflags |= XFS_QUOTA_GDQ_ACCT;
1014 if (flags & XFS_UQUOTA_ENFD)
1015 uflags |= XFS_QUOTA_UDQ_ENFD;
1016 if (flags & (XFS_OQUOTA_ENFD)) {
1017 uflags |= (flags & XFS_GQUOTA_ACCT) ?
1018 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
1020 return (uflags);
1025 * Release all the dquots on the inodes in an AG.
1027 STATIC void
1028 xfs_qm_dqrele_inodes_ag(
1029 xfs_mount_t *mp,
1030 int ag,
1031 uint flags)
1033 xfs_inode_t *ip = NULL;
1034 xfs_perag_t *pag = &mp->m_perag[ag];
1035 int first_index = 0;
1036 int nr_found;
1038 do {
1040 * use a gang lookup to find the next inode in the tree
1041 * as the tree is sparse and a gang lookup walks to find
1042 * the number of objects requested.
1044 read_lock(&pag->pag_ici_lock);
1045 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
1046 (void**)&ip, first_index, 1);
1048 if (!nr_found) {
1049 read_unlock(&pag->pag_ici_lock);
1050 break;
1054 * Update the index for the next lookup. Catch overflows
1055 * into the next AG range which can occur if we have inodes
1056 * in the last block of the AG and we are currently
1057 * pointing to the last inode.
1059 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1060 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
1061 read_unlock(&pag->pag_ici_lock);
1062 break;
1065 /* skip quota inodes */
1066 if (ip == XFS_QI_UQIP(mp) || ip == XFS_QI_GQIP(mp)) {
1067 ASSERT(ip->i_udquot == NULL);
1068 ASSERT(ip->i_gdquot == NULL);
1069 read_unlock(&pag->pag_ici_lock);
1070 continue;
1074 * If we can't get a reference on the inode, it must be
1075 * in reclaim. Leave it for the reclaim code to flush.
1077 if (!igrab(VFS_I(ip))) {
1078 read_unlock(&pag->pag_ici_lock);
1079 continue;
1081 read_unlock(&pag->pag_ici_lock);
1083 /* avoid new inodes though we shouldn't find any here */
1084 if (xfs_iflags_test(ip, XFS_INEW)) {
1085 IRELE(ip);
1086 continue;
1089 xfs_ilock(ip, XFS_ILOCK_EXCL);
1090 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
1091 xfs_qm_dqrele(ip->i_udquot);
1092 ip->i_udquot = NULL;
1094 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) &&
1095 ip->i_gdquot) {
1096 xfs_qm_dqrele(ip->i_gdquot);
1097 ip->i_gdquot = NULL;
1099 xfs_iput(ip, XFS_ILOCK_EXCL);
1101 } while (nr_found);
1105 * Go thru all the inodes in the file system, releasing their dquots.
1106 * Note that the mount structure gets modified to indicate that quotas are off
1107 * AFTER this, in the case of quotaoff. This also gets called from
1108 * xfs_rootumount.
1110 void
1111 xfs_qm_dqrele_all_inodes(
1112 struct xfs_mount *mp,
1113 uint flags)
1115 int i;
1117 ASSERT(mp->m_quotainfo);
1118 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
1119 if (!mp->m_perag[i].pag_ici_init)
1120 continue;
1121 xfs_qm_dqrele_inodes_ag(mp, i, flags);
1125 /*------------------------------------------------------------------------*/
1126 #ifdef DEBUG
1128 * This contains all the test functions for XFS disk quotas.
1129 * Currently it does a quota accounting check. ie. it walks through
1130 * all inodes in the file system, calculating the dquot accounting fields,
1131 * and prints out any inconsistencies.
1133 xfs_dqhash_t *qmtest_udqtab;
1134 xfs_dqhash_t *qmtest_gdqtab;
1135 int qmtest_hashmask;
1136 int qmtest_nfails;
1137 mutex_t qcheck_lock;
1139 #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
1140 (__psunsigned_t)(id)) & \
1141 (qmtest_hashmask - 1))
1143 #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
1144 (qmtest_udqtab + \
1145 DQTEST_HASHVAL(mp, id)) : \
1146 (qmtest_gdqtab + \
1147 DQTEST_HASHVAL(mp, id)))
1149 #define DQTEST_LIST_PRINT(l, NXT, title) \
1151 xfs_dqtest_t *dqp; int i = 0;\
1152 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
1153 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
1154 dqp = (xfs_dqtest_t *)dqp->NXT) { \
1155 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
1156 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
1157 dqp->d_bcount, dqp->d_icount); } \
1160 typedef struct dqtest {
1161 xfs_dqmarker_t q_lists;
1162 xfs_dqhash_t *q_hash; /* the hashchain header */
1163 xfs_mount_t *q_mount; /* filesystem this relates to */
1164 xfs_dqid_t d_id; /* user id or group id */
1165 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
1166 xfs_qcnt_t d_icount; /* # inodes owned by the user */
1167 } xfs_dqtest_t;
1169 STATIC void
1170 xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
1172 xfs_dquot_t *d;
1173 if (((d) = (h)->qh_next))
1174 (d)->HL_PREVP = &((dqp)->HL_NEXT);
1175 (dqp)->HL_NEXT = d;
1176 (dqp)->HL_PREVP = &((h)->qh_next);
1177 (h)->qh_next = (xfs_dquot_t *)dqp;
1178 (h)->qh_version++;
1179 (h)->qh_nelems++;
1181 STATIC void
1182 xfs_qm_dqtest_print(
1183 xfs_dqtest_t *d)
1185 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
1186 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
1187 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
1188 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1189 d->d_bcount, (int)d->d_bcount);
1190 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1191 d->d_icount, (int)d->d_icount);
1192 cmn_err(CE_DEBUG, "---------------------------");
1195 STATIC void
1196 xfs_qm_dqtest_failed(
1197 xfs_dqtest_t *d,
1198 xfs_dquot_t *dqp,
1199 char *reason,
1200 xfs_qcnt_t a,
1201 xfs_qcnt_t b,
1202 int error)
1204 qmtest_nfails++;
1205 if (error)
1206 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
1207 d->d_id, error, reason);
1208 else
1209 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
1210 d->d_id, reason, (int)a, (int)b);
1211 xfs_qm_dqtest_print(d);
1212 if (dqp)
1213 xfs_qm_dqprint(dqp);
1216 STATIC int
1217 xfs_dqtest_cmp2(
1218 xfs_dqtest_t *d,
1219 xfs_dquot_t *dqp)
1221 int err = 0;
1222 if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
1223 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
1224 be64_to_cpu(dqp->q_core.d_icount),
1225 d->d_icount, 0);
1226 err++;
1228 if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
1229 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
1230 be64_to_cpu(dqp->q_core.d_bcount),
1231 d->d_bcount, 0);
1232 err++;
1234 if (dqp->q_core.d_blk_softlimit &&
1235 be64_to_cpu(dqp->q_core.d_bcount) >=
1236 be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
1237 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1238 cmn_err(CE_DEBUG,
1239 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1240 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1241 err++;
1244 if (dqp->q_core.d_ino_softlimit &&
1245 be64_to_cpu(dqp->q_core.d_icount) >=
1246 be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
1247 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1248 cmn_err(CE_DEBUG,
1249 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1250 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1251 err++;
1254 #ifdef QUOTADEBUG
1255 if (!err) {
1256 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
1257 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1259 #endif
1260 return (err);
1263 STATIC void
1264 xfs_dqtest_cmp(
1265 xfs_dqtest_t *d)
1267 xfs_dquot_t *dqp;
1268 int error;
1270 /* xfs_qm_dqtest_print(d); */
1271 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1272 &dqp))) {
1273 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1274 return;
1276 xfs_dqtest_cmp2(d, dqp);
1277 xfs_qm_dqput(dqp);
1280 STATIC int
1281 xfs_qm_internalqcheck_dqget(
1282 xfs_mount_t *mp,
1283 xfs_dqid_t id,
1284 uint type,
1285 xfs_dqtest_t **O_dq)
1287 xfs_dqtest_t *d;
1288 xfs_dqhash_t *h;
1290 h = DQTEST_HASH(mp, id, type);
1291 for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
1292 d = (xfs_dqtest_t *) d->HL_NEXT) {
1293 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1294 if (d->d_id == id && mp == d->q_mount) {
1295 *O_dq = d;
1296 return (0);
1299 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1300 d->dq_flags = type;
1301 d->d_id = id;
1302 d->q_mount = mp;
1303 d->q_hash = h;
1304 xfs_qm_hashinsert(h, d);
1305 *O_dq = d;
1306 return (0);
1309 STATIC void
1310 xfs_qm_internalqcheck_get_dquots(
1311 xfs_mount_t *mp,
1312 xfs_dqid_t uid,
1313 xfs_dqid_t projid,
1314 xfs_dqid_t gid,
1315 xfs_dqtest_t **ud,
1316 xfs_dqtest_t **gd)
1318 if (XFS_IS_UQUOTA_ON(mp))
1319 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1320 if (XFS_IS_GQUOTA_ON(mp))
1321 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
1322 else if (XFS_IS_PQUOTA_ON(mp))
1323 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
1327 STATIC void
1328 xfs_qm_internalqcheck_dqadjust(
1329 xfs_inode_t *ip,
1330 xfs_dqtest_t *d)
1332 d->d_icount++;
1333 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1336 STATIC int
1337 xfs_qm_internalqcheck_adjust(
1338 xfs_mount_t *mp, /* mount point for filesystem */
1339 xfs_ino_t ino, /* inode number to get data for */
1340 void __user *buffer, /* not used */
1341 int ubsize, /* not used */
1342 void *private_data, /* not used */
1343 xfs_daddr_t bno, /* starting block of inode cluster */
1344 int *ubused, /* not used */
1345 void *dip, /* not used */
1346 int *res) /* bulkstat result code */
1348 xfs_inode_t *ip;
1349 xfs_dqtest_t *ud, *gd;
1350 uint lock_flags;
1351 boolean_t ipreleased;
1352 int error;
1354 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1356 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1357 *res = BULKSTAT_RV_NOTHING;
1358 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1359 (unsigned long long) ino,
1360 (unsigned long long) mp->m_sb.sb_uquotino,
1361 (unsigned long long) mp->m_sb.sb_gquotino);
1362 return XFS_ERROR(EINVAL);
1364 ipreleased = B_FALSE;
1365 again:
1366 lock_flags = XFS_ILOCK_SHARED;
1367 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1368 *res = BULKSTAT_RV_NOTHING;
1369 return (error);
1373 * This inode can have blocks after eof which can get released
1374 * when we send it to inactive. Since we don't check the dquot
1375 * until the after all our calculations are done, we must get rid
1376 * of those now.
1378 if (! ipreleased) {
1379 xfs_iput(ip, lock_flags);
1380 ipreleased = B_TRUE;
1381 goto again;
1383 xfs_qm_internalqcheck_get_dquots(mp,
1384 (xfs_dqid_t) ip->i_d.di_uid,
1385 (xfs_dqid_t) ip->i_d.di_projid,
1386 (xfs_dqid_t) ip->i_d.di_gid,
1387 &ud, &gd);
1388 if (XFS_IS_UQUOTA_ON(mp)) {
1389 ASSERT(ud);
1390 xfs_qm_internalqcheck_dqadjust(ip, ud);
1392 if (XFS_IS_OQUOTA_ON(mp)) {
1393 ASSERT(gd);
1394 xfs_qm_internalqcheck_dqadjust(ip, gd);
1396 xfs_iput(ip, lock_flags);
1397 *res = BULKSTAT_RV_DIDONE;
1398 return (0);
1402 /* PRIVATE, debugging */
1404 xfs_qm_internalqcheck(
1405 xfs_mount_t *mp)
1407 xfs_ino_t lastino;
1408 int done, count;
1409 int i;
1410 xfs_dqtest_t *d, *e;
1411 xfs_dqhash_t *h1;
1412 int error;
1414 lastino = 0;
1415 qmtest_hashmask = 32;
1416 count = 5;
1417 done = 0;
1418 qmtest_nfails = 0;
1420 if (! XFS_IS_QUOTA_ON(mp))
1421 return XFS_ERROR(ESRCH);
1423 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1424 XFS_bflush(mp->m_ddev_targp);
1425 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1426 XFS_bflush(mp->m_ddev_targp);
1428 mutex_lock(&qcheck_lock);
1429 /* There should be absolutely no quota activity while this
1430 is going on. */
1431 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1432 sizeof(xfs_dqhash_t), KM_SLEEP);
1433 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1434 sizeof(xfs_dqhash_t), KM_SLEEP);
1435 do {
1437 * Iterate thru all the inodes in the file system,
1438 * adjusting the corresponding dquot counters
1440 if ((error = xfs_bulkstat(mp, &lastino, &count,
1441 xfs_qm_internalqcheck_adjust, NULL,
1442 0, NULL, BULKSTAT_FG_IGET, &done))) {
1443 break;
1445 } while (! done);
1446 if (error) {
1447 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1449 cmn_err(CE_DEBUG, "Checking results against system dquots");
1450 for (i = 0; i < qmtest_hashmask; i++) {
1451 h1 = &qmtest_udqtab[i];
1452 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1453 xfs_dqtest_cmp(d);
1454 e = (xfs_dqtest_t *) d->HL_NEXT;
1455 kmem_free(d);
1456 d = e;
1458 h1 = &qmtest_gdqtab[i];
1459 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1460 xfs_dqtest_cmp(d);
1461 e = (xfs_dqtest_t *) d->HL_NEXT;
1462 kmem_free(d);
1463 d = e;
1467 if (qmtest_nfails) {
1468 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1469 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1470 } else {
1471 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1473 kmem_free(qmtest_udqtab);
1474 kmem_free(qmtest_gdqtab);
1475 mutex_unlock(&qcheck_lock);
1476 return (qmtest_nfails);
1479 #endif /* DEBUG */