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