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