[XFS] All xfs_disk_dquot_t values are (as the name says) disk endian.
[deliverable/linux.git] / fs / xfs / quota / xfs_qm_bhv.c
CommitLineData
1da177e4 1/*
932f2c32 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4ce3121f 3 * All Rights Reserved.
1da177e4 4 *
4ce3121f
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
4ce3121f
NS
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.
1da177e4 13 *
4ce3121f
NS
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
1da177e4 17 */
1da177e4
LT
18#include "xfs.h"
19#include "xfs_fs.h"
a844f451 20#include "xfs_bit.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_clnt.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
a844f451 26#include "xfs_ag.h"
1da177e4
LT
27#include "xfs_dir2.h"
28#include "xfs_alloc.h"
29#include "xfs_dmapi.h"
30#include "xfs_quota.h"
31#include "xfs_mount.h"
1da177e4 32#include "xfs_bmap_btree.h"
a844f451 33#include "xfs_alloc_btree.h"
1da177e4 34#include "xfs_ialloc_btree.h"
1da177e4 35#include "xfs_dir2_sf.h"
a844f451 36#include "xfs_attr_sf.h"
1da177e4
LT
37#include "xfs_dinode.h"
38#include "xfs_inode.h"
a844f451
NS
39#include "xfs_ialloc.h"
40#include "xfs_itable.h"
41#include "xfs_btree.h"
1da177e4 42#include "xfs_bmap.h"
1da177e4
LT
43#include "xfs_rtalloc.h"
44#include "xfs_error.h"
1da177e4
LT
45#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"
1da177e4
LT
51#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 */
c8ad20ff 57#define MNTOPT_PRJQUOTA "prjquota" /* project quota enabled */
1da177e4
LT
58#define MNTOPT_UQUOTA "uquota" /* user quota (IRIX variant) */
59#define MNTOPT_GQUOTA "gquota" /* group quota (IRIX variant) */
c8ad20ff 60#define MNTOPT_PQUOTA "pquota" /* project quota (IRIX variant) */
1da177e4
LT
61#define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
62#define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
c8ad20ff 63#define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
1da177e4
LT
64#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;
c8ad20ff
NS
98 } 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;
1da177e4
LT
106 } 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
c8ad20ff
NS
124 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
b83bd138 130 error = bhv_next_vfs_parseargs(BHV_NEXT(bhv), options, args, update);
1da177e4
LT
131 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{
b83bd138 141 struct bhv_vfs *vfsp = bhvtovfs(bhv);
1da177e4 142 struct xfs_mount *mp = XFS_VFSTOM(vfsp);
1da177e4
LT
143
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
c8ad20ff
NS
150 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
1da177e4 156 if (mp->m_qflags & XFS_GQUOTA_ACCT) {
c8ad20ff 157 (mp->m_qflags & XFS_OQUOTA_ENFD) ?
1da177e4
LT
158 seq_puts(m, "," MNTOPT_GRPQUOTA) :
159 seq_puts(m, "," MNTOPT_GQUOTANOENF);
160 }
161
c8ad20ff 162 if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
1da177e4
LT
163 seq_puts(m, "," MNTOPT_NOQUOTA);
164
b83bd138 165 return bhv_next_vfs_showargs(BHV_NEXT(bhv), m);
1da177e4
LT
166}
167
168STATIC int
169xfs_qm_mount(
170 struct bhv_desc *bhv,
171 struct xfs_mount_args *args,
172 struct cred *cr)
173{
b83bd138 174 struct bhv_vfs *vfsp = bhvtovfs(bhv);
1da177e4 175 struct xfs_mount *mp = XFS_VFSTOM(vfsp);
1da177e4 176
c8ad20ff 177 if (args->flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA | XFSMNT_PQUOTA))
1da177e4 178 xfs_qm_mount_quotainit(mp, args->flags);
b83bd138 179 return bhv_next_vfs_mount(BHV_NEXT(bhv), args, cr);
1da177e4
LT
180}
181
932f2c32
NS
182/*
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,
8285fb58 192 bhv_statvfs_t *statp,
67fcaa73 193 struct bhv_vnode *vnode)
932f2c32
NS
194{
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
b83bd138 202 error = bhv_next_vfs_statvfs(BHV_NEXT(bhv), statp, vnode);
932f2c32
NS
203 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
2a293b7d
CH
220 limit = dp->d_blk_softlimit ?
221 be64_to_cpu(dp->d_blk_softlimit) :
222 be64_to_cpu(dp->d_blk_hardlimit);
932f2c32
NS
223 if (limit && statp->f_blocks > limit) {
224 statp->f_blocks = limit;
2a293b7d
CH
225 statp->f_bfree =
226 (statp->f_blocks > be64_to_cpu(dp->d_bcount)) ?
227 (statp->f_blocks - be64_to_cpu(dp->d_bcount)) : 0;
932f2c32 228 }
2a293b7d
CH
229
230 limit = dp->d_ino_softlimit ?
231 be64_to_cpu(dp->d_ino_softlimit) :
232 be64_to_cpu(dp->d_ino_hardlimit);
932f2c32
NS
233 if (limit && statp->f_files > limit) {
234 statp->f_files = limit;
2a293b7d
CH
235 statp->f_ffree =
236 (statp->f_files > be64_to_cpu(dp->d_icount)) ?
237 (statp->f_ffree - be64_to_cpu(dp->d_icount)) : 0;
932f2c32
NS
238 }
239
240 xfs_qm_dqput(dqp);
241 return 0;
242}
243
1da177e4
LT
244STATIC int
245xfs_qm_syncall(
246 struct bhv_desc *bhv,
247 int flags,
248 cred_t *credp)
249{
b83bd138 250 struct bhv_vfs *vfsp = bhvtovfs(bhv);
1da177e4
LT
251 struct xfs_mount *mp = XFS_VFSTOM(vfsp);
252 int error;
253
254 /*
255 * Get the Quota Manager to flush the dquots.
256 */
257 if (XFS_IS_QUOTA_ON(mp)) {
258 if ((error = xfs_qm_sync(mp, flags))) {
259 /*
260 * If we got an IO error, we will be shutting down.
261 * So, there's nothing more for us to do here.
262 */
263 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
264 if (XFS_FORCED_SHUTDOWN(mp)) {
265 return XFS_ERROR(error);
266 }
267 }
268 }
b83bd138 269 return bhv_next_vfs_sync(BHV_NEXT(bhv), flags, credp);
1da177e4
LT
270}
271
1da177e4
LT
272STATIC int
273xfs_qm_newmount(
274 xfs_mount_t *mp,
275 uint *needquotamount,
276 uint *quotaflags)
277{
278 uint quotaondisk;
c8ad20ff 279 uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
1da177e4
LT
280
281 *quotaflags = 0;
282 *needquotamount = B_FALSE;
283
284 quotaondisk = XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
c8ad20ff 285 (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
1da177e4
LT
286
287 if (quotaondisk) {
288 uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
c8ad20ff 289 pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
1da177e4
LT
290 gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
291 }
292
293 /*
294 * If the device itself is read-only, we can't allow
295 * the user to change the state of quota on the mount -
296 * this would generate a transaction on the ro device,
297 * which would lead to an I/O error and shutdown
298 */
299
300 if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
301 (!uquotaondisk && XFS_IS_UQUOTA_ON(mp)) ||
c8ad20ff
NS
302 (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
303 (!pquotaondisk && XFS_IS_PQUOTA_ON(mp)) ||
1da177e4 304 (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
c8ad20ff 305 (!gquotaondisk && XFS_IS_OQUOTA_ON(mp))) &&
1da177e4
LT
306 xfs_dev_is_read_only(mp, "changing quota state")) {
307 cmn_err(CE_WARN,
c8ad20ff 308 "XFS: please mount with%s%s%s%s.",
1da177e4
LT
309 (!quotaondisk ? "out quota" : ""),
310 (uquotaondisk ? " usrquota" : ""),
c8ad20ff 311 (pquotaondisk ? " prjquota" : ""),
1da177e4
LT
312 (gquotaondisk ? " grpquota" : ""));
313 return XFS_ERROR(EPERM);
314 }
315
316 if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
317 /*
318 * Call mount_quotas at this point only if we won't have to do
319 * a quotacheck.
320 */
321 if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
322 /*
323 * If an error occured, qm_mount_quotas code
324 * has already disabled quotas. So, just finish
325 * mounting, and get on with the boring life
326 * without disk quotas.
327 */
328 xfs_qm_mount_quotas(mp, 0);
329 } else {
330 /*
331 * Clear the quota flags, but remember them. This
332 * is so that the quota code doesn't get invoked
333 * before we're ready. This can happen when an
334 * inode goes inactive and wants to free blocks,
335 * or via xfs_log_mount_finish.
336 */
337 *needquotamount = B_TRUE;
338 *quotaflags = mp->m_qflags;
339 mp->m_qflags = 0;
340 }
341 }
342
343 return 0;
344}
345
346STATIC int
347xfs_qm_endmount(
348 xfs_mount_t *mp,
349 uint needquotamount,
350 uint quotaflags,
351 int mfsi_flags)
352{
353 if (needquotamount) {
354 ASSERT(mp->m_qflags == 0);
355 mp->m_qflags = quotaflags;
356 xfs_qm_mount_quotas(mp, mfsi_flags);
357 }
358
359#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
360 if (! (XFS_IS_QUOTA_ON(mp)))
361 xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
362 else
363 xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
364#endif
365
366#ifdef QUOTADEBUG
367 if (XFS_IS_QUOTA_ON(mp) && xfs_qm_internalqcheck(mp))
368 cmn_err(CE_WARN, "XFS: mount internalqcheck failed");
369#endif
370
371 return 0;
372}
373
374STATIC void
375xfs_qm_dqrele_null(
376 xfs_dquot_t *dq)
377{
378 /*
379 * Called from XFS, where we always check first for a NULL dquot.
380 */
381 if (!dq)
382 return;
383 xfs_qm_dqrele(dq);
384}
385
386
ba0f32d4 387STATIC struct xfs_qmops xfs_qmcore_xfs = {
1da177e4
LT
388 .xfs_qminit = xfs_qm_newmount,
389 .xfs_qmdone = xfs_qm_unmount_quotadestroy,
390 .xfs_qmmount = xfs_qm_endmount,
391 .xfs_qmunmount = xfs_qm_unmount_quotas,
392 .xfs_dqrele = xfs_qm_dqrele_null,
393 .xfs_dqattach = xfs_qm_dqattach,
394 .xfs_dqdetach = xfs_qm_dqdetach,
395 .xfs_dqpurgeall = xfs_qm_dqpurge_all,
396 .xfs_dqvopalloc = xfs_qm_vop_dqalloc,
397 .xfs_dqvopcreate = xfs_qm_vop_dqattach_and_dqmod_newinode,
398 .xfs_dqvoprename = xfs_qm_vop_rename_dqattach,
399 .xfs_dqvopchown = xfs_qm_vop_chown,
400 .xfs_dqvopchownresv = xfs_qm_vop_chown_reserve,
401 .xfs_dqtrxops = &xfs_trans_dquot_ops,
402};
403
b83bd138 404struct bhv_module_vfsops xfs_qmops = { {
1da177e4
LT
405 BHV_IDENTITY_INIT(VFS_BHV_QM, VFS_POSITION_QM),
406 .vfs_parseargs = xfs_qm_parseargs,
407 .vfs_showargs = xfs_qm_showargs,
408 .vfs_mount = xfs_qm_mount,
932f2c32 409 .vfs_statvfs = xfs_qm_statvfs,
1da177e4
LT
410 .vfs_sync = xfs_qm_syncall,
411 .vfs_quotactl = xfs_qm_quotactl, },
412};
413
414
415void __init
416xfs_qm_init(void)
417{
418 static char message[] __initdata =
419 KERN_INFO "SGI XFS Quota Management subsystem\n";
420
421 printk(message);
794ee1ba 422 mutex_init(&xfs_Gqm_lock);
1da177e4
LT
423 vfs_bhv_set_custom(&xfs_qmops, &xfs_qmcore_xfs);
424 xfs_qm_init_procfs();
425}
426
427void __exit
428xfs_qm_exit(void)
429{
430 vfs_bhv_clr_custom(&xfs_qmops);
431 xfs_qm_cleanup_procfs();
432 if (qm_dqzone)
3758dee9 433 kmem_zone_destroy(qm_dqzone);
1da177e4 434 if (qm_dqtrxzone)
3758dee9 435 kmem_zone_destroy(qm_dqtrxzone);
1da177e4 436}
This page took 0.167303 seconds and 5 git commands to generate.