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