xfs: fix double IRELE in xfs_dqrele_inode
[linux-2.6.git] / fs / xfs / quota / xfs_qm_syscalls.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <linux/capability.h>
20
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_attr.h"
49 #include "xfs_buf_item.h"
50 #include "xfs_utils.h"
51 #include "xfs_qm.h"
52
53 #ifdef DEBUG
54 # define qdprintk(s, args...)   cmn_err(CE_DEBUG, s, ## args)
55 #else
56 # define qdprintk(s, args...)   do { } while (0)
57 #endif
58
59 STATIC int      xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
60 STATIC int      xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
61                                         uint);
62 STATIC uint     xfs_qm_export_flags(uint);
63 STATIC uint     xfs_qm_export_qtype_flags(uint);
64 STATIC void     xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
65                                         fs_disk_quota_t *);
66
67
68 /*
69  * Turn off quota accounting and/or enforcement for all udquots and/or
70  * gdquots. Called only at unmount time.
71  *
72  * This assumes that there are no dquots of this file system cached
73  * incore, and modifies the ondisk dquot directly. Therefore, for example,
74  * it is an error to call this twice, without purging the cache.
75  */
76 int
77 xfs_qm_scall_quotaoff(
78         xfs_mount_t             *mp,
79         uint                    flags)
80 {
81         uint                    dqtype;
82         int                     error;
83         uint                    inactivate_flags;
84         xfs_qoff_logitem_t      *qoffstart;
85         int                     nculprits;
86
87         /*
88          * No file system can have quotas enabled on disk but not in core.
89          * Note that quota utilities (like quotaoff) _expect_
90          * errno == EEXIST here.
91          */
92         if ((mp->m_qflags & flags) == 0)
93                 return XFS_ERROR(EEXIST);
94         error = 0;
95
96         flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
97
98         /*
99          * We don't want to deal with two quotaoffs messing up each other,
100          * so we're going to serialize it. quotaoff isn't exactly a performance
101          * critical thing.
102          * If quotaoff, then we must be dealing with the root filesystem.
103          */
104         ASSERT(mp->m_quotainfo);
105         if (mp->m_quotainfo)
106                 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
107
108         ASSERT(mp->m_quotainfo);
109
110         /*
111          * If we're just turning off quota enforcement, change mp and go.
112          */
113         if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
114                 mp->m_qflags &= ~(flags);
115
116                 spin_lock(&mp->m_sb_lock);
117                 mp->m_sb.sb_qflags = mp->m_qflags;
118                 spin_unlock(&mp->m_sb_lock);
119                 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
120
121                 /* XXX what to do if error ? Revert back to old vals incore ? */
122                 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
123                 return (error);
124         }
125
126         dqtype = 0;
127         inactivate_flags = 0;
128         /*
129          * If accounting is off, we must turn enforcement off, clear the
130          * quota 'CHKD' certificate to make it known that we have to
131          * do a quotacheck the next time this quota is turned on.
132          */
133         if (flags & XFS_UQUOTA_ACCT) {
134                 dqtype |= XFS_QMOPT_UQUOTA;
135                 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
136                 inactivate_flags |= XFS_UQUOTA_ACTIVE;
137         }
138         if (flags & XFS_GQUOTA_ACCT) {
139                 dqtype |= XFS_QMOPT_GQUOTA;
140                 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
141                 inactivate_flags |= XFS_GQUOTA_ACTIVE;
142         } else if (flags & XFS_PQUOTA_ACCT) {
143                 dqtype |= XFS_QMOPT_PQUOTA;
144                 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
145                 inactivate_flags |= XFS_PQUOTA_ACTIVE;
146         }
147
148         /*
149          * Nothing to do?  Don't complain. This happens when we're just
150          * turning off quota enforcement.
151          */
152         if ((mp->m_qflags & flags) == 0) {
153                 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
154                 return (0);
155         }
156
157         /*
158          * Write the LI_QUOTAOFF log record, and do SB changes atomically,
159          * and synchronously. If we fail to write, we should abort the
160          * operation as it cannot be recovered safely if we crash.
161          */
162         error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
163         if (error)
164                 goto out_error;
165
166         /*
167          * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
168          * to take care of the race between dqget and quotaoff. We don't take
169          * any special locks to reset these bits. All processes need to check
170          * these bits *after* taking inode lock(s) to see if the particular
171          * quota type is in the process of being turned off. If *ACTIVE, it is
172          * guaranteed that all dquot structures and all quotainode ptrs will all
173          * stay valid as long as that inode is kept locked.
174          *
175          * There is no turning back after this.
176          */
177         mp->m_qflags &= ~inactivate_flags;
178
179         /*
180          * Give back all the dquot reference(s) held by inodes.
181          * Here we go thru every single incore inode in this file system, and
182          * do a dqrele on the i_udquot/i_gdquot that it may have.
183          * Essentially, as long as somebody has an inode locked, this guarantees
184          * that quotas will not be turned off. This is handy because in a
185          * transaction once we lock the inode(s) and check for quotaon, we can
186          * depend on the quota inodes (and other things) being valid as long as
187          * we keep the lock(s).
188          */
189         xfs_qm_dqrele_all_inodes(mp, flags);
190
191         /*
192          * Next we make the changes in the quota flag in the mount struct.
193          * This isn't protected by a particular lock directly, because we
194          * don't want to take a mrlock everytime we depend on quotas being on.
195          */
196         mp->m_qflags &= ~(flags);
197
198         /*
199          * Go through all the dquots of this file system and purge them,
200          * according to what was turned off. We may not be able to get rid
201          * of all dquots, because dquots can have temporary references that
202          * are not attached to inodes. eg. xfs_setattr, xfs_create.
203          * So, if we couldn't purge all the dquots from the filesystem,
204          * we can't get rid of the incore data structures.
205          */
206         while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
207                 delay(10 * nculprits);
208
209         /*
210          * Transactions that had started before ACTIVE state bit was cleared
211          * could have logged many dquots, so they'd have higher LSNs than
212          * the first QUOTAOFF log record does. If we happen to crash when
213          * the tail of the log has gone past the QUOTAOFF record, but
214          * before the last dquot modification, those dquots __will__
215          * recover, and that's not good.
216          *
217          * So, we have QUOTAOFF start and end logitems; the start
218          * logitem won't get overwritten until the end logitem appears...
219          */
220         error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
221         if (error) {
222                 /* We're screwed now. Shutdown is the only option. */
223                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
224                 goto out_error;
225         }
226
227         /*
228          * If quotas is completely disabled, close shop.
229          */
230         if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
231             ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
232                 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
233                 xfs_qm_destroy_quotainfo(mp);
234                 return (0);
235         }
236
237         /*
238          * Release our quotainode references, and vn_purge them,
239          * if we don't need them anymore.
240          */
241         if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
242                 IRELE(XFS_QI_UQIP(mp));
243                 XFS_QI_UQIP(mp) = NULL;
244         }
245         if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
246                 IRELE(XFS_QI_GQIP(mp));
247                 XFS_QI_GQIP(mp) = NULL;
248         }
249 out_error:
250         mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
251
252         return (error);
253 }
254
255 int
256 xfs_qm_scall_trunc_qfiles(
257         xfs_mount_t     *mp,
258         uint            flags)
259 {
260         int             error = 0, error2 = 0;
261         xfs_inode_t     *qip;
262
263         if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) {
264                 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
265                 return XFS_ERROR(EINVAL);
266         }
267
268         if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
269                 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
270                 if (!error) {
271                         error = xfs_truncate_file(mp, qip);
272                         IRELE(qip);
273                 }
274         }
275
276         if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
277             mp->m_sb.sb_gquotino != NULLFSINO) {
278                 error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
279                 if (!error2) {
280                         error2 = xfs_truncate_file(mp, qip);
281                         IRELE(qip);
282                 }
283         }
284
285         return error ? error : error2;
286 }
287
288
289 /*
290  * Switch on (a given) quota enforcement for a filesystem.  This takes
291  * effect immediately.
292  * (Switching on quota accounting must be done at mount time.)
293  */
294 int
295 xfs_qm_scall_quotaon(
296         xfs_mount_t     *mp,
297         uint            flags)
298 {
299         int             error;
300         uint            qf;
301         uint            accflags;
302         __int64_t       sbflags;
303
304         flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
305         /*
306          * Switching on quota accounting must be done at mount time.
307          */
308         accflags = flags & XFS_ALL_QUOTA_ACCT;
309         flags &= ~(XFS_ALL_QUOTA_ACCT);
310
311         sbflags = 0;
312
313         if (flags == 0) {
314                 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
315                 return XFS_ERROR(EINVAL);
316         }
317
318         /* No fs can turn on quotas with a delayed effect */
319         ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
320
321         /*
322          * Can't enforce without accounting. We check the superblock
323          * qflags here instead of m_qflags because rootfs can have
324          * quota acct on ondisk without m_qflags' knowing.
325          */
326         if (((flags & XFS_UQUOTA_ACCT) == 0 &&
327             (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
328             (flags & XFS_UQUOTA_ENFD))
329             ||
330             ((flags & XFS_PQUOTA_ACCT) == 0 &&
331             (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
332             (flags & XFS_GQUOTA_ACCT) == 0 &&
333             (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
334             (flags & XFS_OQUOTA_ENFD))) {
335                 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
336                         flags, mp->m_sb.sb_qflags);
337                 return XFS_ERROR(EINVAL);
338         }
339         /*
340          * If everything's upto-date incore, then don't waste time.
341          */
342         if ((mp->m_qflags & flags) == flags)
343                 return XFS_ERROR(EEXIST);
344
345         /*
346          * Change sb_qflags on disk but not incore mp->qflags
347          * if this is the root filesystem.
348          */
349         spin_lock(&mp->m_sb_lock);
350         qf = mp->m_sb.sb_qflags;
351         mp->m_sb.sb_qflags = qf | flags;
352         spin_unlock(&mp->m_sb_lock);
353
354         /*
355          * There's nothing to change if it's the same.
356          */
357         if ((qf & flags) == flags && sbflags == 0)
358                 return XFS_ERROR(EEXIST);
359         sbflags |= XFS_SB_QFLAGS;
360
361         if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
362                 return (error);
363         /*
364          * If we aren't trying to switch on quota enforcement, we are done.
365          */
366         if  (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
367              (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
368              ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
369              (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
370              ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
371              (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
372             (flags & XFS_ALL_QUOTA_ENFD) == 0)
373                 return (0);
374
375         if (! XFS_IS_QUOTA_RUNNING(mp))
376                 return XFS_ERROR(ESRCH);
377
378         /*
379          * Switch on quota enforcement in core.
380          */
381         mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
382         mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
383         mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
384
385         return (0);
386 }
387
388
389 /*
390  * Return quota status information, such as uquota-off, enforcements, etc.
391  */
392 int
393 xfs_qm_scall_getqstat(
394         xfs_mount_t     *mp,
395         fs_quota_stat_t *out)
396 {
397         xfs_inode_t     *uip, *gip;
398         boolean_t       tempuqip, tempgqip;
399
400         uip = gip = NULL;
401         tempuqip = tempgqip = B_FALSE;
402         memset(out, 0, sizeof(fs_quota_stat_t));
403
404         out->qs_version = FS_QSTAT_VERSION;
405         if (!xfs_sb_version_hasquota(&mp->m_sb)) {
406                 out->qs_uquota.qfs_ino = NULLFSINO;
407                 out->qs_gquota.qfs_ino = NULLFSINO;
408                 return (0);
409         }
410         out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
411                                                         (XFS_ALL_QUOTA_ACCT|
412                                                          XFS_ALL_QUOTA_ENFD));
413         out->qs_pad = 0;
414         out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
415         out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
416
417         if (mp->m_quotainfo) {
418                 uip = mp->m_quotainfo->qi_uquotaip;
419                 gip = mp->m_quotainfo->qi_gquotaip;
420         }
421         if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
422                 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
423                                         0, 0, &uip, 0) == 0)
424                         tempuqip = B_TRUE;
425         }
426         if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
427                 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
428                                         0, 0, &gip, 0) == 0)
429                         tempgqip = B_TRUE;
430         }
431         if (uip) {
432                 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
433                 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
434                 if (tempuqip)
435                         IRELE(uip);
436         }
437         if (gip) {
438                 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
439                 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
440                 if (tempgqip)
441                         IRELE(gip);
442         }
443         if (mp->m_quotainfo) {
444                 out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
445                 out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
446                 out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
447                 out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
448                 out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
449                 out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
450         }
451         return (0);
452 }
453
454 /*
455  * Adjust quota limits, and start/stop timers accordingly.
456  */
457 int
458 xfs_qm_scall_setqlim(
459         xfs_mount_t             *mp,
460         xfs_dqid_t              id,
461         uint                    type,
462         fs_disk_quota_t         *newlim)
463 {
464         xfs_disk_dquot_t        *ddq;
465         xfs_dquot_t             *dqp;
466         xfs_trans_t             *tp;
467         int                     error;
468         xfs_qcnt_t              hard, soft;
469
470         if ((newlim->d_fieldmask &
471             (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
472                 return (0);
473
474         tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
475         if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
476                                       0, 0, XFS_DEFAULT_LOG_COUNT))) {
477                 xfs_trans_cancel(tp, 0);
478                 return (error);
479         }
480
481         /*
482          * We don't want to race with a quotaoff so take the quotaoff lock.
483          * (We don't hold an inode lock, so there's nothing else to stop
484          * a quotaoff from happening). (XXXThis doesn't currently happen
485          * because we take the vfslock before calling xfs_qm_sysent).
486          */
487         mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
488
489         /*
490          * Get the dquot (locked), and join it to the transaction.
491          * Allocate the dquot if this doesn't exist.
492          */
493         if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
494                 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
495                 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
496                 ASSERT(error != ENOENT);
497                 return (error);
498         }
499         xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
500         xfs_trans_dqjoin(tp, dqp);
501         ddq = &dqp->q_core;
502
503         /*
504          * Make sure that hardlimits are >= soft limits before changing.
505          */
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);
515                 if (id == 0) {
516                         mp->m_quotainfo->qi_bhardlimit = hard;
517                         mp->m_quotainfo->qi_bsoftlimit = soft;
518                 }
519         } else {
520                 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
521         }
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);
531                 if (id == 0) {
532                         mp->m_quotainfo->qi_rtbhardlimit = hard;
533                         mp->m_quotainfo->qi_rtbsoftlimit = soft;
534                 }
535         } else {
536                 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
537         }
538
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);
548                 if (id == 0) {
549                         mp->m_quotainfo->qi_ihardlimit = hard;
550                         mp->m_quotainfo->qi_isoftlimit = soft;
551                 }
552         } else {
553                 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
554         }
555
556         /*
557          * Update warnings counter(s) if requested
558          */
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);
565
566         if (id == 0) {
567                 /*
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
572                  * for warnings.
573                  */
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);
577                 }
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);
581                 }
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);
585                 }
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;
592         } else {
593                 /*
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.
599                  */
600                 xfs_qm_adjust_dqtimers(mp, ddq);
601         }
602         dqp->dq_flags |= XFS_DQ_DIRTY;
603         xfs_trans_log_dquot(tp, dqp);
604
605         xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
606         error = xfs_trans_commit(tp, 0);
607         xfs_qm_dqprint(dqp);
608         xfs_qm_dqrele(dqp);
609         mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
610
611         return error;
612 }
613
614 int
615 xfs_qm_scall_getquota(
616         xfs_mount_t     *mp,
617         xfs_dqid_t      id,
618         uint            type,
619         fs_disk_quota_t *out)
620 {
621         xfs_dquot_t     *dqp;
622         int             error;
623
624         /*
625          * Try to get the dquot. We don't want it allocated on disk, so
626          * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
627          * exist, we'll get ENOENT back.
628          */
629         if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
630                 return (error);
631         }
632
633         xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
634         /*
635          * If everything's NULL, this dquot doesn't quite exist as far as
636          * our utility programs are concerned.
637          */
638         if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
639                 xfs_qm_dqput(dqp);
640                 return XFS_ERROR(ENOENT);
641         }
642         /* xfs_qm_dqprint(dqp); */
643         /*
644          * Convert the disk dquot to the exportable format
645          */
646         xfs_qm_export_dquot(mp, &dqp->q_core, out);
647         xfs_qm_dqput(dqp);
648         return (error ? XFS_ERROR(EFAULT) : 0);
649 }
650
651
652 STATIC int
653 xfs_qm_log_quotaoff_end(
654         xfs_mount_t             *mp,
655         xfs_qoff_logitem_t      *startqoff,
656         uint                    flags)
657 {
658         xfs_trans_t             *tp;
659         int                     error;
660         xfs_qoff_logitem_t      *qoffi;
661
662         tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
663
664         if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
665                                       0, 0, XFS_DEFAULT_LOG_COUNT))) {
666                 xfs_trans_cancel(tp, 0);
667                 return (error);
668         }
669
670         qoffi = xfs_trans_get_qoff_item(tp, startqoff,
671                                         flags & XFS_ALL_QUOTA_ACCT);
672         xfs_trans_log_quotaoff_item(tp, qoffi);
673
674         /*
675          * We have to make sure that the transaction is secure on disk before we
676          * return and actually stop quota accounting. So, make it synchronous.
677          * We don't care about quotoff's performance.
678          */
679         xfs_trans_set_sync(tp);
680         error = xfs_trans_commit(tp, 0);
681         return (error);
682 }
683
684
685 STATIC int
686 xfs_qm_log_quotaoff(
687         xfs_mount_t            *mp,
688         xfs_qoff_logitem_t     **qoffstartp,
689         uint                   flags)
690 {
691         xfs_trans_t            *tp;
692         int                     error;
693         xfs_qoff_logitem_t     *qoffi=NULL;
694         uint                    oldsbqflag=0;
695
696         tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
697         if ((error = xfs_trans_reserve(tp, 0,
698                                       sizeof(xfs_qoff_logitem_t) * 2 +
699                                       mp->m_sb.sb_sectsize + 128,
700                                       0,
701                                       0,
702                                       XFS_DEFAULT_LOG_COUNT))) {
703                 goto error0;
704         }
705
706         qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
707         xfs_trans_log_quotaoff_item(tp, qoffi);
708
709         spin_lock(&mp->m_sb_lock);
710         oldsbqflag = mp->m_sb.sb_qflags;
711         mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
712         spin_unlock(&mp->m_sb_lock);
713
714         xfs_mod_sb(tp, XFS_SB_QFLAGS);
715
716         /*
717          * We have to make sure that the transaction is secure on disk before we
718          * return and actually stop quota accounting. So, make it synchronous.
719          * We don't care about quotoff's performance.
720          */
721         xfs_trans_set_sync(tp);
722         error = xfs_trans_commit(tp, 0);
723
724 error0:
725         if (error) {
726                 xfs_trans_cancel(tp, 0);
727                 /*
728                  * No one else is modifying sb_qflags, so this is OK.
729                  * We still hold the quotaofflock.
730                  */
731                 spin_lock(&mp->m_sb_lock);
732                 mp->m_sb.sb_qflags = oldsbqflag;
733                 spin_unlock(&mp->m_sb_lock);
734         }
735         *qoffstartp = qoffi;
736         return (error);
737 }
738
739
740 /*
741  * Translate an internal style on-disk-dquot to the exportable format.
742  * The main differences are that the counters/limits are all in Basic
743  * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
744  * to be converted to the native endianness.
745  */
746 STATIC void
747 xfs_qm_export_dquot(
748         xfs_mount_t             *mp,
749         xfs_disk_dquot_t        *src,
750         struct fs_disk_quota    *dst)
751 {
752         memset(dst, 0, sizeof(*dst));
753         dst->d_version = FS_DQUOT_VERSION;  /* different from src->d_version */
754         dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
755         dst->d_id = be32_to_cpu(src->d_id);
756         dst->d_blk_hardlimit =
757                 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
758         dst->d_blk_softlimit =
759                 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
760         dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
761         dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
762         dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
763         dst->d_icount = be64_to_cpu(src->d_icount);
764         dst->d_btimer = be32_to_cpu(src->d_btimer);
765         dst->d_itimer = be32_to_cpu(src->d_itimer);
766         dst->d_iwarns = be16_to_cpu(src->d_iwarns);
767         dst->d_bwarns = be16_to_cpu(src->d_bwarns);
768         dst->d_rtb_hardlimit =
769                 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
770         dst->d_rtb_softlimit =
771                 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
772         dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
773         dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
774         dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
775
776         /*
777          * Internally, we don't reset all the timers when quota enforcement
778          * gets turned off. No need to confuse the user level code,
779          * so return zeroes in that case.
780          */
781         if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
782             (!XFS_IS_OQUOTA_ENFORCED(mp) &&
783                         (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
784                 dst->d_btimer = 0;
785                 dst->d_itimer = 0;
786                 dst->d_rtbtimer = 0;
787         }
788
789 #ifdef DEBUG
790         if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
791              (XFS_IS_OQUOTA_ENFORCED(mp) &&
792                         (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
793             dst->d_id != 0) {
794                 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
795                     (dst->d_blk_softlimit > 0)) {
796                         ASSERT(dst->d_btimer != 0);
797                 }
798                 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
799                     (dst->d_ino_softlimit > 0)) {
800                         ASSERT(dst->d_itimer != 0);
801                 }
802         }
803 #endif
804 }
805
806 STATIC uint
807 xfs_qm_export_qtype_flags(
808         uint flags)
809 {
810         /*
811          * Can't be more than one, or none.
812          */
813         ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
814                 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
815         ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
816                 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
817         ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
818                 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
819         ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
820
821         return (flags & XFS_DQ_USER) ?
822                 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
823                         XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
824 }
825
826 STATIC uint
827 xfs_qm_export_flags(
828         uint flags)
829 {
830         uint uflags;
831
832         uflags = 0;
833         if (flags & XFS_UQUOTA_ACCT)
834                 uflags |= XFS_QUOTA_UDQ_ACCT;
835         if (flags & XFS_PQUOTA_ACCT)
836                 uflags |= XFS_QUOTA_PDQ_ACCT;
837         if (flags & XFS_GQUOTA_ACCT)
838                 uflags |= XFS_QUOTA_GDQ_ACCT;
839         if (flags & XFS_UQUOTA_ENFD)
840                 uflags |= XFS_QUOTA_UDQ_ENFD;
841         if (flags & (XFS_OQUOTA_ENFD)) {
842                 uflags |= (flags & XFS_GQUOTA_ACCT) ?
843                         XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
844         }
845         return (uflags);
846 }
847
848
849 STATIC int
850 xfs_dqrele_inode(
851         struct xfs_inode        *ip,
852         struct xfs_perag        *pag,
853         int                     flags)
854 {
855         int                     error;
856
857         /* skip quota inodes */
858         if (ip == XFS_QI_UQIP(ip->i_mount) || ip == XFS_QI_GQIP(ip->i_mount)) {
859                 ASSERT(ip->i_udquot == NULL);
860                 ASSERT(ip->i_gdquot == NULL);
861                 read_unlock(&pag->pag_ici_lock);
862                 return 0;
863         }
864
865         error = xfs_sync_inode_valid(ip, pag);
866         if (error)
867                 return error;
868
869         xfs_ilock(ip, XFS_ILOCK_EXCL);
870         if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
871                 xfs_qm_dqrele(ip->i_udquot);
872                 ip->i_udquot = NULL;
873         }
874         if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
875                 xfs_qm_dqrele(ip->i_gdquot);
876                 ip->i_gdquot = NULL;
877         }
878         xfs_iput(ip, XFS_ILOCK_EXCL);
879
880         return 0;
881 }
882
883
884 /*
885  * Go thru all the inodes in the file system, releasing their dquots.
886  *
887  * Note that the mount structure gets modified to indicate that quotas are off
888  * AFTER this, in the case of quotaoff.
889  */
890 void
891 xfs_qm_dqrele_all_inodes(
892         struct xfs_mount *mp,
893         uint             flags)
894 {
895         ASSERT(mp->m_quotainfo);
896         xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, XFS_ICI_NO_TAG);
897 }
898
899 /*------------------------------------------------------------------------*/
900 #ifdef DEBUG
901 /*
902  * This contains all the test functions for XFS disk quotas.
903  * Currently it does a quota accounting check. ie. it walks through
904  * all inodes in the file system, calculating the dquot accounting fields,
905  * and prints out any inconsistencies.
906  */
907 xfs_dqhash_t *qmtest_udqtab;
908 xfs_dqhash_t *qmtest_gdqtab;
909 int           qmtest_hashmask;
910 int           qmtest_nfails;
911 struct mutex  qcheck_lock;
912
913 #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
914                                  (__psunsigned_t)(id)) & \
915                                 (qmtest_hashmask - 1))
916
917 #define DQTEST_HASH(mp, id, type)   ((type & XFS_DQ_USER) ? \
918                                      (qmtest_udqtab + \
919                                       DQTEST_HASHVAL(mp, id)) : \
920                                      (qmtest_gdqtab + \
921                                       DQTEST_HASHVAL(mp, id)))
922
923 #define DQTEST_LIST_PRINT(l, NXT, title) \
924 { \
925           xfs_dqtest_t  *dqp; int i = 0;\
926           cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
927           for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
928                dqp = (xfs_dqtest_t *)dqp->NXT) { \
929                 cmn_err(CE_DEBUG, "  %d. \"%d (%s)\"  bcnt = %d, icnt = %d", \
930                          ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp),      \
931                          dqp->d_bcount, dqp->d_icount); } \
932 }
933
934 typedef struct dqtest {
935         xfs_dqmarker_t  q_lists;
936         xfs_dqhash_t    *q_hash;        /* the hashchain header */
937         xfs_mount_t     *q_mount;       /* filesystem this relates to */
938         xfs_dqid_t      d_id;           /* user id or group id */
939         xfs_qcnt_t      d_bcount;       /* # disk blocks owned by the user */
940         xfs_qcnt_t      d_icount;       /* # inodes owned by the user */
941 } xfs_dqtest_t;
942
943 STATIC void
944 xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
945 {
946         xfs_dquot_t *d;
947         if (((d) = (h)->qh_next))
948                 (d)->HL_PREVP = &((dqp)->HL_NEXT);
949         (dqp)->HL_NEXT = d;
950         (dqp)->HL_PREVP = &((h)->qh_next);
951         (h)->qh_next = (xfs_dquot_t *)dqp;
952         (h)->qh_version++;
953         (h)->qh_nelems++;
954 }
955 STATIC void
956 xfs_qm_dqtest_print(
957         xfs_dqtest_t    *d)
958 {
959         cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
960         cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
961         cmn_err(CE_DEBUG, "---- fs       = 0x%p", d->q_mount);
962         cmn_err(CE_DEBUG, "---- bcount   = %Lu (0x%x)",
963                 d->d_bcount, (int)d->d_bcount);
964         cmn_err(CE_DEBUG, "---- icount   = %Lu (0x%x)",
965                 d->d_icount, (int)d->d_icount);
966         cmn_err(CE_DEBUG, "---------------------------");
967 }
968
969 STATIC void
970 xfs_qm_dqtest_failed(
971         xfs_dqtest_t    *d,
972         xfs_dquot_t     *dqp,
973         char            *reason,
974         xfs_qcnt_t      a,
975         xfs_qcnt_t      b,
976         int             error)
977 {
978         qmtest_nfails++;
979         if (error)
980                 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
981                        d->d_id, error, reason);
982         else
983                 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
984                        d->d_id, reason, (int)a, (int)b);
985         xfs_qm_dqtest_print(d);
986         if (dqp)
987                 xfs_qm_dqprint(dqp);
988 }
989
990 STATIC int
991 xfs_dqtest_cmp2(
992         xfs_dqtest_t    *d,
993         xfs_dquot_t     *dqp)
994 {
995         int err = 0;
996         if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
997                 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
998                         be64_to_cpu(dqp->q_core.d_icount),
999                         d->d_icount, 0);
1000                 err++;
1001         }
1002         if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
1003                 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
1004                         be64_to_cpu(dqp->q_core.d_bcount),
1005                         d->d_bcount, 0);
1006                 err++;
1007         }
1008         if (dqp->q_core.d_blk_softlimit &&
1009             be64_to_cpu(dqp->q_core.d_bcount) >=
1010             be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
1011                 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1012                         cmn_err(CE_DEBUG,
1013                                 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1014                                 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1015                         err++;
1016                 }
1017         }
1018         if (dqp->q_core.d_ino_softlimit &&
1019             be64_to_cpu(dqp->q_core.d_icount) >=
1020             be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
1021                 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1022                         cmn_err(CE_DEBUG,
1023                                 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1024                                 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1025                         err++;
1026                 }
1027         }
1028 #ifdef QUOTADEBUG
1029         if (!err) {
1030                 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
1031                         d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1032         }
1033 #endif
1034         return (err);
1035 }
1036
1037 STATIC void
1038 xfs_dqtest_cmp(
1039         xfs_dqtest_t    *d)
1040 {
1041         xfs_dquot_t     *dqp;
1042         int             error;
1043
1044         /* xfs_qm_dqtest_print(d); */
1045         if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1046                                  &dqp))) {
1047                 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1048                 return;
1049         }
1050         xfs_dqtest_cmp2(d, dqp);
1051         xfs_qm_dqput(dqp);
1052 }
1053
1054 STATIC int
1055 xfs_qm_internalqcheck_dqget(
1056         xfs_mount_t     *mp,
1057         xfs_dqid_t      id,
1058         uint            type,
1059         xfs_dqtest_t    **O_dq)
1060 {
1061         xfs_dqtest_t    *d;
1062         xfs_dqhash_t    *h;
1063
1064         h = DQTEST_HASH(mp, id, type);
1065         for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
1066              d = (xfs_dqtest_t *) d->HL_NEXT) {
1067                 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1068                 if (d->d_id == id && mp == d->q_mount) {
1069                         *O_dq = d;
1070                         return (0);
1071                 }
1072         }
1073         d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1074         d->dq_flags = type;
1075         d->d_id = id;
1076         d->q_mount = mp;
1077         d->q_hash = h;
1078         xfs_qm_hashinsert(h, d);
1079         *O_dq = d;
1080         return (0);
1081 }
1082
1083 STATIC void
1084 xfs_qm_internalqcheck_get_dquots(
1085         xfs_mount_t     *mp,
1086         xfs_dqid_t      uid,
1087         xfs_dqid_t      projid,
1088         xfs_dqid_t      gid,
1089         xfs_dqtest_t    **ud,
1090         xfs_dqtest_t    **gd)
1091 {
1092         if (XFS_IS_UQUOTA_ON(mp))
1093                 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1094         if (XFS_IS_GQUOTA_ON(mp))
1095                 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
1096         else if (XFS_IS_PQUOTA_ON(mp))
1097                 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
1098 }
1099
1100
1101 STATIC void
1102 xfs_qm_internalqcheck_dqadjust(
1103         xfs_inode_t             *ip,
1104         xfs_dqtest_t            *d)
1105 {
1106         d->d_icount++;
1107         d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1108 }
1109
1110 STATIC int
1111 xfs_qm_internalqcheck_adjust(
1112         xfs_mount_t     *mp,            /* mount point for filesystem */
1113         xfs_ino_t       ino,            /* inode number to get data for */
1114         void            __user *buffer, /* not used */
1115         int             ubsize,         /* not used */
1116         void            *private_data,  /* not used */
1117         xfs_daddr_t     bno,            /* starting block of inode cluster */
1118         int             *ubused,        /* not used */
1119         void            *dip,           /* not used */
1120         int             *res)           /* bulkstat result code */
1121 {
1122         xfs_inode_t             *ip;
1123         xfs_dqtest_t            *ud, *gd;
1124         uint                    lock_flags;
1125         boolean_t               ipreleased;
1126         int                     error;
1127
1128         ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1129
1130         if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1131                 *res = BULKSTAT_RV_NOTHING;
1132                 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1133                         (unsigned long long) ino,
1134                         (unsigned long long) mp->m_sb.sb_uquotino,
1135                         (unsigned long long) mp->m_sb.sb_gquotino);
1136                 return XFS_ERROR(EINVAL);
1137         }
1138         ipreleased = B_FALSE;
1139  again:
1140         lock_flags = XFS_ILOCK_SHARED;
1141         if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1142                 *res = BULKSTAT_RV_NOTHING;
1143                 return (error);
1144         }
1145
1146         /*
1147          * This inode can have blocks after eof which can get released
1148          * when we send it to inactive. Since we don't check the dquot
1149          * until the after all our calculations are done, we must get rid
1150          * of those now.
1151          */
1152         if (! ipreleased) {
1153                 xfs_iput(ip, lock_flags);
1154                 ipreleased = B_TRUE;
1155                 goto again;
1156         }
1157         xfs_qm_internalqcheck_get_dquots(mp,
1158                                         (xfs_dqid_t) ip->i_d.di_uid,
1159                                         (xfs_dqid_t) ip->i_d.di_projid,
1160                                         (xfs_dqid_t) ip->i_d.di_gid,
1161                                         &ud, &gd);
1162         if (XFS_IS_UQUOTA_ON(mp)) {
1163                 ASSERT(ud);
1164                 xfs_qm_internalqcheck_dqadjust(ip, ud);
1165         }
1166         if (XFS_IS_OQUOTA_ON(mp)) {
1167                 ASSERT(gd);
1168                 xfs_qm_internalqcheck_dqadjust(ip, gd);
1169         }
1170         xfs_iput(ip, lock_flags);
1171         *res = BULKSTAT_RV_DIDONE;
1172         return (0);
1173 }
1174
1175
1176 /* PRIVATE, debugging */
1177 int
1178 xfs_qm_internalqcheck(
1179         xfs_mount_t     *mp)
1180 {
1181         xfs_ino_t       lastino;
1182         int             done, count;
1183         int             i;
1184         xfs_dqtest_t    *d, *e;
1185         xfs_dqhash_t    *h1;
1186         int             error;
1187
1188         lastino = 0;
1189         qmtest_hashmask = 32;
1190         count = 5;
1191         done = 0;
1192         qmtest_nfails = 0;
1193
1194         if (! XFS_IS_QUOTA_ON(mp))
1195                 return XFS_ERROR(ESRCH);
1196
1197         xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1198         XFS_bflush(mp->m_ddev_targp);
1199         xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1200         XFS_bflush(mp->m_ddev_targp);
1201
1202         mutex_lock(&qcheck_lock);
1203         /* There should be absolutely no quota activity while this
1204            is going on. */
1205         qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1206                                     sizeof(xfs_dqhash_t), KM_SLEEP);
1207         qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1208                                     sizeof(xfs_dqhash_t), KM_SLEEP);
1209         do {
1210                 /*
1211                  * Iterate thru all the inodes in the file system,
1212                  * adjusting the corresponding dquot counters
1213                  */
1214                 if ((error = xfs_bulkstat(mp, &lastino, &count,
1215                                  xfs_qm_internalqcheck_adjust, NULL,
1216                                  0, NULL, BULKSTAT_FG_IGET, &done))) {
1217                         break;
1218                 }
1219         } while (! done);
1220         if (error) {
1221                 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1222         }
1223         cmn_err(CE_DEBUG, "Checking results against system dquots");
1224         for (i = 0; i < qmtest_hashmask; i++) {
1225                 h1 = &qmtest_udqtab[i];
1226                 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1227                         xfs_dqtest_cmp(d);
1228                         e = (xfs_dqtest_t *) d->HL_NEXT;
1229                         kmem_free(d);
1230                         d = e;
1231                 }
1232                 h1 = &qmtest_gdqtab[i];
1233                 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1234                         xfs_dqtest_cmp(d);
1235                         e = (xfs_dqtest_t *) d->HL_NEXT;
1236                         kmem_free(d);
1237                         d = e;
1238                 }
1239         }
1240
1241         if (qmtest_nfails) {
1242                 cmn_err(CE_DEBUG, "******** quotacheck failed  ********");
1243                 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1244         } else {
1245                 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1246         }
1247         kmem_free(qmtest_udqtab);
1248         kmem_free(qmtest_gdqtab);
1249         mutex_unlock(&qcheck_lock);
1250         return (qmtest_nfails);
1251 }
1252
1253 #endif /* DEBUG */