blob: e95e99f7168fdb78f6f7b724fbc66a5acdf35aef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott932f2c32006-06-09 15:29:58 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott4ce31212005-11-02 14:59:41 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_clnt.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_alloc.h"
29#include "xfs_dmapi.h"
30#include "xfs_quota.h"
31#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_dinode.h"
38#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_ialloc.h"
40#include "xfs_itable.h"
41#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_rtalloc.h"
44#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_rw.h"
46#include "xfs_acl.h"
47#include "xfs_cap.h"
48#include "xfs_mac.h"
49#include "xfs_attr.h"
50#include "xfs_buf_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "xfs_qm.h"
52
53#define MNTOPT_QUOTA "quota" /* disk quotas (user) */
54#define MNTOPT_NOQUOTA "noquota" /* no quotas */
55#define MNTOPT_USRQUOTA "usrquota" /* user quota enabled */
56#define MNTOPT_GRPQUOTA "grpquota" /* group quota enabled */
Nathan Scottc8ad20f2005-06-21 15:38:48 +100057#define MNTOPT_PRJQUOTA "prjquota" /* project quota enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define MNTOPT_UQUOTA "uquota" /* user quota (IRIX variant) */
59#define MNTOPT_GQUOTA "gquota" /* group quota (IRIX variant) */
Nathan Scottc8ad20f2005-06-21 15:38:48 +100060#define MNTOPT_PQUOTA "pquota" /* project quota (IRIX variant) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
62#define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
Nathan Scottc8ad20f2005-06-21 15:38:48 +100063#define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define MNTOPT_QUOTANOENF "qnoenforce" /* same as uqnoenforce */
65
66STATIC int
67xfs_qm_parseargs(
68 struct bhv_desc *bhv,
69 char *options,
70 struct xfs_mount_args *args,
71 int update)
72{
73 size_t length;
74 char *local_options = options;
75 char *this_char;
76 int error;
77 int referenced = update;
78
79 while ((this_char = strsep(&local_options, ",")) != NULL) {
80 length = strlen(this_char);
81 if (local_options)
82 length++;
83
84 if (!strcmp(this_char, MNTOPT_NOQUOTA)) {
85 args->flags &= ~(XFSMNT_UQUOTAENF|XFSMNT_UQUOTA);
86 args->flags &= ~(XFSMNT_GQUOTAENF|XFSMNT_GQUOTA);
87 referenced = update;
88 } else if (!strcmp(this_char, MNTOPT_QUOTA) ||
89 !strcmp(this_char, MNTOPT_UQUOTA) ||
90 !strcmp(this_char, MNTOPT_USRQUOTA)) {
91 args->flags |= XFSMNT_UQUOTA | XFSMNT_UQUOTAENF;
92 referenced = 1;
93 } else if (!strcmp(this_char, MNTOPT_QUOTANOENF) ||
94 !strcmp(this_char, MNTOPT_UQUOTANOENF)) {
95 args->flags |= XFSMNT_UQUOTA;
96 args->flags &= ~XFSMNT_UQUOTAENF;
97 referenced = 1;
Nathan Scottc8ad20f2005-06-21 15:38:48 +100098 } else if (!strcmp(this_char, MNTOPT_PQUOTA) ||
99 !strcmp(this_char, MNTOPT_PRJQUOTA)) {
100 args->flags |= XFSMNT_PQUOTA | XFSMNT_PQUOTAENF;
101 referenced = 1;
102 } else if (!strcmp(this_char, MNTOPT_PQUOTANOENF)) {
103 args->flags |= XFSMNT_PQUOTA;
104 args->flags &= ~XFSMNT_PQUOTAENF;
105 referenced = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
107 !strcmp(this_char, MNTOPT_GRPQUOTA)) {
108 args->flags |= XFSMNT_GQUOTA | XFSMNT_GQUOTAENF;
109 referenced = 1;
110 } else if (!strcmp(this_char, MNTOPT_GQUOTANOENF)) {
111 args->flags |= XFSMNT_GQUOTA;
112 args->flags &= ~XFSMNT_GQUOTAENF;
113 referenced = 1;
114 } else {
115 if (local_options)
116 *(local_options-1) = ',';
117 continue;
118 }
119
120 while (length--)
121 *this_char++ = ',';
122 }
123
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000124 if ((args->flags & XFSMNT_GQUOTA) && (args->flags & XFSMNT_PQUOTA)) {
125 cmn_err(CE_WARN,
126 "XFS: cannot mount with both project and group quota");
127 return XFS_ERROR(EINVAL);
128 }
129
Nathan Scottb83bd132006-06-09 16:48:30 +1000130 error = bhv_next_vfs_parseargs(BHV_NEXT(bhv), options, args, update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (!error && !referenced)
132 bhv_remove_vfsops(bhvtovfs(bhv), VFS_POSITION_QM);
133 return error;
134}
135
136STATIC int
137xfs_qm_showargs(
138 struct bhv_desc *bhv,
139 struct seq_file *m)
140{
Nathan Scottb83bd132006-06-09 16:48:30 +1000141 struct bhv_vfs *vfsp = bhvtovfs(bhv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct xfs_mount *mp = XFS_VFSTOM(vfsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (mp->m_qflags & XFS_UQUOTA_ACCT) {
145 (mp->m_qflags & XFS_UQUOTA_ENFD) ?
146 seq_puts(m, "," MNTOPT_USRQUOTA) :
147 seq_puts(m, "," MNTOPT_UQUOTANOENF);
148 }
149
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000150 if (mp->m_qflags & XFS_PQUOTA_ACCT) {
151 (mp->m_qflags & XFS_OQUOTA_ENFD) ?
152 seq_puts(m, "," MNTOPT_PRJQUOTA) :
153 seq_puts(m, "," MNTOPT_PQUOTANOENF);
154 }
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (mp->m_qflags & XFS_GQUOTA_ACCT) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000157 (mp->m_qflags & XFS_OQUOTA_ENFD) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 seq_puts(m, "," MNTOPT_GRPQUOTA) :
159 seq_puts(m, "," MNTOPT_GQUOTANOENF);
160 }
161
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000162 if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 seq_puts(m, "," MNTOPT_NOQUOTA);
164
Nathan Scottb83bd132006-06-09 16:48:30 +1000165 return bhv_next_vfs_showargs(BHV_NEXT(bhv), m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168STATIC int
169xfs_qm_mount(
170 struct bhv_desc *bhv,
171 struct xfs_mount_args *args,
172 struct cred *cr)
173{
Nathan Scottb83bd132006-06-09 16:48:30 +1000174 struct bhv_vfs *vfsp = bhvtovfs(bhv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct xfs_mount *mp = XFS_VFSTOM(vfsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000177 if (args->flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA | XFSMNT_PQUOTA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 xfs_qm_mount_quotainit(mp, args->flags);
Nathan Scottb83bd132006-06-09 16:48:30 +1000179 return bhv_next_vfs_mount(BHV_NEXT(bhv), args, cr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Nathan Scott932f2c32006-06-09 15:29:58 +1000182/*
183 * Directory tree accounting is implemented using project quotas, where
184 * the project identifier is inherited from parent directories.
185 * A statvfs (df, etc.) of a directory that is using project quota should
186 * return a statvfs of the project, not the entire filesystem.
187 * This makes such trees appear as if they are filesystems in themselves.
188 */
189STATIC int
190xfs_qm_statvfs(
191 struct bhv_desc *bhv,
Nathan Scott8285fb52006-06-09 17:07:12 +1000192 bhv_statvfs_t *statp,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000193 struct bhv_vnode *vnode)
Nathan Scott932f2c32006-06-09 15:29:58 +1000194{
195 xfs_mount_t *mp;
196 xfs_inode_t *ip;
197 xfs_dquot_t *dqp;
198 xfs_disk_dquot_t *dp;
199 __uint64_t limit;
200 int error;
201
Nathan Scottb83bd132006-06-09 16:48:30 +1000202 error = bhv_next_vfs_statvfs(BHV_NEXT(bhv), statp, vnode);
Nathan Scott932f2c32006-06-09 15:29:58 +1000203 if (error || !vnode)
204 return error;
205
206 mp = XFS_BHVTOM(bhv);
207 ip = xfs_vtoi(vnode);
208
209 if (!(ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
210 return 0;
211 if (!(mp->m_qflags & XFS_PQUOTA_ACCT))
212 return 0;
213 if (!(mp->m_qflags & XFS_OQUOTA_ENFD))
214 return 0;
215
216 if (xfs_qm_dqget(mp, NULL, ip->i_d.di_projid, XFS_DQ_PROJ, 0, &dqp))
217 return 0;
218 dp = &dqp->q_core;
219
220 limit = dp->d_blk_softlimit ? dp->d_blk_softlimit : dp->d_blk_hardlimit;
221 if (limit && statp->f_blocks > limit) {
222 statp->f_blocks = limit;
223 statp->f_bfree = (statp->f_blocks > dp->d_bcount) ?
224 (statp->f_blocks - dp->d_bcount) : 0;
225 }
226 limit = dp->d_ino_softlimit ? dp->d_ino_softlimit : dp->d_ino_hardlimit;
227 if (limit && statp->f_files > limit) {
228 statp->f_files = limit;
229 statp->f_ffree = (statp->f_files > dp->d_icount) ?
230 (statp->f_ffree - dp->d_icount) : 0;
231 }
232
233 xfs_qm_dqput(dqp);
234 return 0;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237STATIC int
238xfs_qm_syncall(
239 struct bhv_desc *bhv,
240 int flags,
241 cred_t *credp)
242{
Nathan Scottb83bd132006-06-09 16:48:30 +1000243 struct bhv_vfs *vfsp = bhvtovfs(bhv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct xfs_mount *mp = XFS_VFSTOM(vfsp);
245 int error;
246
247 /*
248 * Get the Quota Manager to flush the dquots.
249 */
250 if (XFS_IS_QUOTA_ON(mp)) {
251 if ((error = xfs_qm_sync(mp, flags))) {
252 /*
253 * If we got an IO error, we will be shutting down.
254 * So, there's nothing more for us to do here.
255 */
256 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
257 if (XFS_FORCED_SHUTDOWN(mp)) {
258 return XFS_ERROR(error);
259 }
260 }
261 }
Nathan Scottb83bd132006-06-09 16:48:30 +1000262 return bhv_next_vfs_sync(BHV_NEXT(bhv), flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265STATIC int
266xfs_qm_newmount(
267 xfs_mount_t *mp,
268 uint *needquotamount,
269 uint *quotaflags)
270{
271 uint quotaondisk;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000272 uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 *quotaflags = 0;
275 *needquotamount = B_FALSE;
276
277 quotaondisk = XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000278 (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (quotaondisk) {
281 uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000282 pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
284 }
285
286 /*
287 * If the device itself is read-only, we can't allow
288 * the user to change the state of quota on the mount -
289 * this would generate a transaction on the ro device,
290 * which would lead to an I/O error and shutdown
291 */
292
293 if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
294 (!uquotaondisk && XFS_IS_UQUOTA_ON(mp)) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000295 (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
296 (!pquotaondisk && XFS_IS_PQUOTA_ON(mp)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000298 (!gquotaondisk && XFS_IS_OQUOTA_ON(mp))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 xfs_dev_is_read_only(mp, "changing quota state")) {
300 cmn_err(CE_WARN,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000301 "XFS: please mount with%s%s%s%s.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 (!quotaondisk ? "out quota" : ""),
303 (uquotaondisk ? " usrquota" : ""),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000304 (pquotaondisk ? " prjquota" : ""),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 (gquotaondisk ? " grpquota" : ""));
306 return XFS_ERROR(EPERM);
307 }
308
309 if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
310 /*
311 * Call mount_quotas at this point only if we won't have to do
312 * a quotacheck.
313 */
314 if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
315 /*
316 * If an error occured, qm_mount_quotas code
317 * has already disabled quotas. So, just finish
318 * mounting, and get on with the boring life
319 * without disk quotas.
320 */
321 xfs_qm_mount_quotas(mp, 0);
322 } else {
323 /*
324 * Clear the quota flags, but remember them. This
325 * is so that the quota code doesn't get invoked
326 * before we're ready. This can happen when an
327 * inode goes inactive and wants to free blocks,
328 * or via xfs_log_mount_finish.
329 */
330 *needquotamount = B_TRUE;
331 *quotaflags = mp->m_qflags;
332 mp->m_qflags = 0;
333 }
334 }
335
336 return 0;
337}
338
339STATIC int
340xfs_qm_endmount(
341 xfs_mount_t *mp,
342 uint needquotamount,
343 uint quotaflags,
344 int mfsi_flags)
345{
346 if (needquotamount) {
347 ASSERT(mp->m_qflags == 0);
348 mp->m_qflags = quotaflags;
349 xfs_qm_mount_quotas(mp, mfsi_flags);
350 }
351
352#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
353 if (! (XFS_IS_QUOTA_ON(mp)))
354 xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
355 else
356 xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
357#endif
358
359#ifdef QUOTADEBUG
360 if (XFS_IS_QUOTA_ON(mp) && xfs_qm_internalqcheck(mp))
361 cmn_err(CE_WARN, "XFS: mount internalqcheck failed");
362#endif
363
364 return 0;
365}
366
367STATIC void
368xfs_qm_dqrele_null(
369 xfs_dquot_t *dq)
370{
371 /*
372 * Called from XFS, where we always check first for a NULL dquot.
373 */
374 if (!dq)
375 return;
376 xfs_qm_dqrele(dq);
377}
378
379
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000380STATIC struct xfs_qmops xfs_qmcore_xfs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 .xfs_qminit = xfs_qm_newmount,
382 .xfs_qmdone = xfs_qm_unmount_quotadestroy,
383 .xfs_qmmount = xfs_qm_endmount,
384 .xfs_qmunmount = xfs_qm_unmount_quotas,
385 .xfs_dqrele = xfs_qm_dqrele_null,
386 .xfs_dqattach = xfs_qm_dqattach,
387 .xfs_dqdetach = xfs_qm_dqdetach,
388 .xfs_dqpurgeall = xfs_qm_dqpurge_all,
389 .xfs_dqvopalloc = xfs_qm_vop_dqalloc,
390 .xfs_dqvopcreate = xfs_qm_vop_dqattach_and_dqmod_newinode,
391 .xfs_dqvoprename = xfs_qm_vop_rename_dqattach,
392 .xfs_dqvopchown = xfs_qm_vop_chown,
393 .xfs_dqvopchownresv = xfs_qm_vop_chown_reserve,
394 .xfs_dqtrxops = &xfs_trans_dquot_ops,
395};
396
Nathan Scottb83bd132006-06-09 16:48:30 +1000397struct bhv_module_vfsops xfs_qmops = { {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 BHV_IDENTITY_INIT(VFS_BHV_QM, VFS_POSITION_QM),
399 .vfs_parseargs = xfs_qm_parseargs,
400 .vfs_showargs = xfs_qm_showargs,
401 .vfs_mount = xfs_qm_mount,
Nathan Scott932f2c32006-06-09 15:29:58 +1000402 .vfs_statvfs = xfs_qm_statvfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .vfs_sync = xfs_qm_syncall,
404 .vfs_quotactl = xfs_qm_quotactl, },
405};
406
407
408void __init
409xfs_qm_init(void)
410{
411 static char message[] __initdata =
412 KERN_INFO "SGI XFS Quota Management subsystem\n";
413
414 printk(message);
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800415 mutex_init(&xfs_Gqm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 vfs_bhv_set_custom(&xfs_qmops, &xfs_qmcore_xfs);
417 xfs_qm_init_procfs();
418}
419
420void __exit
421xfs_qm_exit(void)
422{
423 vfs_bhv_clr_custom(&xfs_qmops);
424 xfs_qm_cleanup_procfs();
425 if (qm_dqzone)
Nathan Scott3758dee2006-03-22 12:47:28 +1100426 kmem_zone_destroy(qm_dqzone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (qm_dqtrxzone)
Nathan Scott3758dee2006-03-22 12:47:28 +1100428 kmem_zone_destroy(qm_dqtrxzone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}