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