[XFS] Merge a few minor fixes to the quota warning code.
[linux-2.6] / fs / xfs / quota / xfs_qm_bhv.c
1 /*
2  * Copyright (c) 2000-2004 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_clnt.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_dir.h"
41 #include "xfs_dir2.h"
42 #include "xfs_alloc.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_quota.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
56 #include "xfs_bmap.h"
57 #include "xfs_bit.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
61 #include "xfs_rw.h"
62 #include "xfs_acl.h"
63 #include "xfs_cap.h"
64 #include "xfs_mac.h"
65 #include "xfs_attr.h"
66 #include "xfs_buf_item.h"
67
68 #include "xfs_qm.h"
69
70 #define MNTOPT_QUOTA    "quota"         /* disk quotas (user) */
71 #define MNTOPT_NOQUOTA  "noquota"       /* no quotas */
72 #define MNTOPT_USRQUOTA "usrquota"      /* user quota enabled */
73 #define MNTOPT_GRPQUOTA "grpquota"      /* group quota enabled */
74 #define MNTOPT_PRJQUOTA "prjquota"      /* project quota enabled */
75 #define MNTOPT_UQUOTA   "uquota"        /* user quota (IRIX variant) */
76 #define MNTOPT_GQUOTA   "gquota"        /* group quota (IRIX variant) */
77 #define MNTOPT_PQUOTA   "pquota"        /* project quota (IRIX variant) */
78 #define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
79 #define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
80 #define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
81 #define MNTOPT_QUOTANOENF  "qnoenforce" /* same as uqnoenforce */
82
83 STATIC int
84 xfs_qm_parseargs(
85         struct bhv_desc         *bhv,
86         char                    *options,
87         struct xfs_mount_args   *args,
88         int                     update)
89 {
90         size_t                  length;
91         char                    *local_options = options;
92         char                    *this_char;
93         int                     error;
94         int                     referenced = update;
95
96         while ((this_char = strsep(&local_options, ",")) != NULL) {
97                 length = strlen(this_char);
98                 if (local_options)
99                         length++;
100
101                 if (!strcmp(this_char, MNTOPT_NOQUOTA)) {
102                         args->flags &= ~(XFSMNT_UQUOTAENF|XFSMNT_UQUOTA);
103                         args->flags &= ~(XFSMNT_GQUOTAENF|XFSMNT_GQUOTA);
104                         referenced = update;
105                 } else if (!strcmp(this_char, MNTOPT_QUOTA) ||
106                            !strcmp(this_char, MNTOPT_UQUOTA) ||
107                            !strcmp(this_char, MNTOPT_USRQUOTA)) {
108                         args->flags |= XFSMNT_UQUOTA | XFSMNT_UQUOTAENF;
109                         referenced = 1;
110                 } else if (!strcmp(this_char, MNTOPT_QUOTANOENF) ||
111                            !strcmp(this_char, MNTOPT_UQUOTANOENF)) {
112                         args->flags |= XFSMNT_UQUOTA;
113                         args->flags &= ~XFSMNT_UQUOTAENF;
114                         referenced = 1;
115                 } else if (!strcmp(this_char, MNTOPT_PQUOTA) ||
116                            !strcmp(this_char, MNTOPT_PRJQUOTA)) {
117                         args->flags |= XFSMNT_PQUOTA | XFSMNT_PQUOTAENF;
118                         referenced = 1;
119                 } else if (!strcmp(this_char, MNTOPT_PQUOTANOENF)) {
120                         args->flags |= XFSMNT_PQUOTA;
121                         args->flags &= ~XFSMNT_PQUOTAENF;
122                         referenced = 1;
123                 } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
124                            !strcmp(this_char, MNTOPT_GRPQUOTA)) {
125                         args->flags |= XFSMNT_GQUOTA | XFSMNT_GQUOTAENF;
126                         referenced = 1;
127                 } else if (!strcmp(this_char, MNTOPT_GQUOTANOENF)) {
128                         args->flags |= XFSMNT_GQUOTA;
129                         args->flags &= ~XFSMNT_GQUOTAENF;
130                         referenced = 1;
131                 } else {
132                         if (local_options)
133                                 *(local_options-1) = ',';
134                         continue;
135                 }
136
137                 while (length--)
138                         *this_char++ = ',';
139         }
140
141         if ((args->flags & XFSMNT_GQUOTA) && (args->flags & XFSMNT_PQUOTA)) {
142                 cmn_err(CE_WARN,
143                         "XFS: cannot mount with both project and group quota");
144                 return XFS_ERROR(EINVAL);
145         }
146
147         PVFS_PARSEARGS(BHV_NEXT(bhv), options, args, update, error);
148         if (!error && !referenced)
149                 bhv_remove_vfsops(bhvtovfs(bhv), VFS_POSITION_QM);
150         return error;
151 }
152
153 STATIC int
154 xfs_qm_showargs(
155         struct bhv_desc         *bhv,
156         struct seq_file         *m)
157 {
158         struct vfs              *vfsp = bhvtovfs(bhv);
159         struct xfs_mount        *mp = XFS_VFSTOM(vfsp);
160         int                     error;
161
162         if (mp->m_qflags & XFS_UQUOTA_ACCT) {
163                 (mp->m_qflags & XFS_UQUOTA_ENFD) ?
164                         seq_puts(m, "," MNTOPT_USRQUOTA) :
165                         seq_puts(m, "," MNTOPT_UQUOTANOENF);
166         }
167
168         if (mp->m_qflags & XFS_PQUOTA_ACCT) {
169                 (mp->m_qflags & XFS_OQUOTA_ENFD) ?
170                         seq_puts(m, "," MNTOPT_PRJQUOTA) :
171                         seq_puts(m, "," MNTOPT_PQUOTANOENF);
172         }
173
174         if (mp->m_qflags & XFS_GQUOTA_ACCT) {
175                 (mp->m_qflags & XFS_OQUOTA_ENFD) ?
176                         seq_puts(m, "," MNTOPT_GRPQUOTA) :
177                         seq_puts(m, "," MNTOPT_GQUOTANOENF);
178         }
179
180         if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
181                 seq_puts(m, "," MNTOPT_NOQUOTA);
182
183         PVFS_SHOWARGS(BHV_NEXT(bhv), m, error);
184         return error;
185 }
186
187 STATIC int
188 xfs_qm_mount(
189         struct bhv_desc         *bhv,
190         struct xfs_mount_args   *args,
191         struct cred             *cr)
192 {
193         struct vfs              *vfsp = bhvtovfs(bhv);
194         struct xfs_mount        *mp = XFS_VFSTOM(vfsp);
195         int                     error;
196
197         if (args->flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA | XFSMNT_PQUOTA))
198                 xfs_qm_mount_quotainit(mp, args->flags);
199         PVFS_MOUNT(BHV_NEXT(bhv), args, cr, error);
200         return error;
201 }
202
203 STATIC int
204 xfs_qm_syncall(
205         struct bhv_desc         *bhv,
206         int                     flags,
207         cred_t                  *credp)
208 {
209         struct vfs              *vfsp = bhvtovfs(bhv);
210         struct xfs_mount        *mp = XFS_VFSTOM(vfsp);
211         int                     error;
212
213         /*
214          * Get the Quota Manager to flush the dquots.
215          */
216         if (XFS_IS_QUOTA_ON(mp)) {
217                 if ((error = xfs_qm_sync(mp, flags))) {
218                         /*
219                          * If we got an IO error, we will be shutting down.
220                          * So, there's nothing more for us to do here.
221                          */
222                         ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
223                         if (XFS_FORCED_SHUTDOWN(mp)) {
224                                 return XFS_ERROR(error);
225                         }
226                 }
227         }
228         PVFS_SYNC(BHV_NEXT(bhv), flags, credp, error);
229         return error;
230 }
231
232 /*
233  * Clear the quotaflags in memory and in the superblock.
234  */
235 void
236 xfs_mount_reset_sbqflags(
237         xfs_mount_t             *mp)
238 {
239         xfs_trans_t             *tp;
240         unsigned long           s;
241
242         mp->m_qflags = 0;
243         /*
244          * It is OK to look at sb_qflags here in mount path,
245          * without SB_LOCK.
246          */
247         if (mp->m_sb.sb_qflags == 0)
248                 return;
249         s = XFS_SB_LOCK(mp);
250         mp->m_sb.sb_qflags = 0;
251         XFS_SB_UNLOCK(mp, s);
252
253         /*
254          * if the fs is readonly, let the incore superblock run
255          * with quotas off but don't flush the update out to disk
256          */
257         if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
258                 return;
259 #ifdef QUOTADEBUG
260         xfs_fs_cmn_err(CE_NOTE, mp, "Writing superblock quota changes");
261 #endif
262         tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
263         if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
264                                       XFS_DEFAULT_LOG_COUNT)) {
265                 xfs_trans_cancel(tp, 0);
266                 xfs_fs_cmn_err(CE_ALERT, mp,
267                         "xfs_mount_reset_sbqflags: Superblock update failed!");
268                 return;
269         }
270         xfs_mod_sb(tp, XFS_SB_QFLAGS);
271         xfs_trans_commit(tp, 0, NULL);
272 }
273
274 STATIC int
275 xfs_qm_newmount(
276         xfs_mount_t     *mp,
277         uint            *needquotamount,
278         uint            *quotaflags)
279 {
280         uint            quotaondisk;
281         uint            uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
282
283         *quotaflags = 0;
284         *needquotamount = B_FALSE;
285
286         quotaondisk = XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
287                                 (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
288
289         if (quotaondisk) {
290                 uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
291                 pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
292                 gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
293         }
294
295         /*
296          * If the device itself is read-only, we can't allow
297          * the user to change the state of quota on the mount -
298          * this would generate a transaction on the ro device,
299          * which would lead to an I/O error and shutdown
300          */
301
302         if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
303             (!uquotaondisk &&  XFS_IS_UQUOTA_ON(mp)) ||
304              (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
305             (!pquotaondisk &&  XFS_IS_PQUOTA_ON(mp)) ||
306              (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
307             (!gquotaondisk &&  XFS_IS_OQUOTA_ON(mp)))  &&
308             xfs_dev_is_read_only(mp, "changing quota state")) {
309                 cmn_err(CE_WARN,
310                         "XFS: please mount with%s%s%s%s.",
311                         (!quotaondisk ? "out quota" : ""),
312                         (uquotaondisk ? " usrquota" : ""),
313                         (pquotaondisk ? " prjquota" : ""),
314                         (gquotaondisk ? " grpquota" : ""));
315                 return XFS_ERROR(EPERM);
316         }
317
318         if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
319                 /*
320                  * Call mount_quotas at this point only if we won't have to do
321                  * a quotacheck.
322                  */
323                 if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
324                         /*
325                          * If an error occured, qm_mount_quotas code
326                          * has already disabled quotas. So, just finish
327                          * mounting, and get on with the boring life
328                          * without disk quotas.
329                          */
330                         xfs_qm_mount_quotas(mp, 0);
331                 } else {
332                         /*
333                          * Clear the quota flags, but remember them. This
334                          * is so that the quota code doesn't get invoked
335                          * before we're ready. This can happen when an
336                          * inode goes inactive and wants to free blocks,
337                          * or via xfs_log_mount_finish.
338                          */
339                         *needquotamount = B_TRUE;
340                         *quotaflags = mp->m_qflags;
341                         mp->m_qflags = 0;
342                 }
343         }
344
345         return 0;
346 }
347
348 STATIC int
349 xfs_qm_endmount(
350         xfs_mount_t     *mp,
351         uint            needquotamount,
352         uint            quotaflags,
353         int             mfsi_flags)
354 {
355         if (needquotamount) {
356                 ASSERT(mp->m_qflags == 0);
357                 mp->m_qflags = quotaflags;
358                 xfs_qm_mount_quotas(mp, mfsi_flags);
359         }
360
361 #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
362         if (! (XFS_IS_QUOTA_ON(mp)))
363                 xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
364         else
365                 xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
366 #endif
367
368 #ifdef QUOTADEBUG
369         if (XFS_IS_QUOTA_ON(mp) && xfs_qm_internalqcheck(mp))
370                 cmn_err(CE_WARN, "XFS: mount internalqcheck failed");
371 #endif
372
373         return 0;
374 }
375
376 STATIC void
377 xfs_qm_dqrele_null(
378         xfs_dquot_t     *dq)
379 {
380         /*
381          * Called from XFS, where we always check first for a NULL dquot.
382          */
383         if (!dq)
384                 return;
385         xfs_qm_dqrele(dq);
386 }
387
388
389 STATIC struct xfs_qmops xfs_qmcore_xfs = {
390         .xfs_qminit             = xfs_qm_newmount,
391         .xfs_qmdone             = xfs_qm_unmount_quotadestroy,
392         .xfs_qmmount            = xfs_qm_endmount,
393         .xfs_qmunmount          = xfs_qm_unmount_quotas,
394         .xfs_dqrele             = xfs_qm_dqrele_null,
395         .xfs_dqattach           = xfs_qm_dqattach,
396         .xfs_dqdetach           = xfs_qm_dqdetach,
397         .xfs_dqpurgeall         = xfs_qm_dqpurge_all,
398         .xfs_dqvopalloc         = xfs_qm_vop_dqalloc,
399         .xfs_dqvopcreate        = xfs_qm_vop_dqattach_and_dqmod_newinode,
400         .xfs_dqvoprename        = xfs_qm_vop_rename_dqattach,
401         .xfs_dqvopchown         = xfs_qm_vop_chown,
402         .xfs_dqvopchownresv     = xfs_qm_vop_chown_reserve,
403         .xfs_dqtrxops           = &xfs_trans_dquot_ops,
404 };
405
406 struct bhv_vfsops xfs_qmops = { {
407         BHV_IDENTITY_INIT(VFS_BHV_QM, VFS_POSITION_QM),
408         .vfs_parseargs          = xfs_qm_parseargs,
409         .vfs_showargs           = xfs_qm_showargs,
410         .vfs_mount              = xfs_qm_mount,
411         .vfs_sync               = xfs_qm_syncall,
412         .vfs_quotactl           = xfs_qm_quotactl, },
413 };
414
415
416 void __init
417 xfs_qm_init(void)
418 {
419         static char     message[] __initdata =
420                 KERN_INFO "SGI XFS Quota Management subsystem\n";
421
422         printk(message);
423         mutex_init(&xfs_Gqm_lock, MUTEX_DEFAULT, "xfs_qmlock");
424         vfs_bhv_set_custom(&xfs_qmops, &xfs_qmcore_xfs);
425         xfs_qm_init_procfs();
426 }
427
428 void __exit
429 xfs_qm_exit(void)
430 {
431         vfs_bhv_clr_custom(&xfs_qmops);
432         xfs_qm_cleanup_procfs();
433         if (qm_dqzone)
434                 kmem_cache_destroy(qm_dqzone);
435         if (qm_dqtrxzone)
436                 kmem_cache_destroy(qm_dqtrxzone);
437 }