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