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