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