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