ffs_quota2.c revision 1.2 1 /* $NetBSD: ffs_quota2.c,v 1.2 2011/03/06 17:08:38 bouyer Exp $ */
2 /*-
3 * Copyright (c) 2010 Manuel Bouyer
4 * All rights reserved.
5 * This software is distributed under the following condiions
6 * compliant with the NetBSD foundation policy.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ffs_quota2.c,v 1.2 2011/03/06 17:08:38 bouyer Exp $");
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/file.h>
38 #include <sys/proc.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/kauth.h>
42
43 #include <ufs/ufs/quota2.h>
44 #include <ufs/ufs/inode.h>
45 #include <ufs/ufs/ufsmount.h>
46 #include <ufs/ffs/ffs_extern.h>
47 #include <ufs/ffs/fs.h>
48
49
50 int
51 ffs_quota2_mount(struct mount *mp)
52 {
53 struct ufsmount *ump = VFSTOUFS(mp);
54 struct fs *fs = ump->um_fs;
55 int error = 0;
56 struct vnode *vp;
57 struct lwp *l = curlwp;
58
59 if ((fs->fs_flags & FS_DOQUOTA2) == 0)
60 return 0;
61
62 ump->um_flags |= UFS_QUOTA2;
63 ump->umq2_bsize = fs->fs_bsize;
64 ump->umq2_bmask = fs->fs_qbmask;
65 if (fs->fs_quota_magic != Q2_HEAD_MAGIC) {
66 printf("%s: Invalid quota magic number\n",
67 mp->mnt_stat.f_mntonname);
68 return EINVAL;
69 }
70 if ((fs->fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA)) &&
71 fs->fs_quotafile[USRQUOTA] == 0) {
72 printf("%s: no user quota inode\n",
73 mp->mnt_stat.f_mntonname);
74 error = EINVAL;
75 }
76 if ((fs->fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA)) &&
77 fs->fs_quotafile[GRPQUOTA] == 0) {
78 printf("%s: no group quota inode\n",
79 mp->mnt_stat.f_mntonname);
80 error = EINVAL;
81 }
82 if (error)
83 return error;
84
85 if (fs->fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA) &&
86 ump->um_quotas[USRQUOTA] == NULLVP) {
87 error = VFS_VGET(mp, fs->fs_quotafile[USRQUOTA], &vp);
88 if (error) {
89 printf("%s: can't vget() user quota inode: %d\n",
90 mp->mnt_stat.f_mntonname, error);
91 return error;
92 }
93 ump->um_quotas[USRQUOTA] = vp;
94 ump->um_cred[USRQUOTA] = l->l_cred;
95 mutex_enter(&vp->v_interlock);
96 vp->v_writecount++;
97 mutex_exit(&vp->v_interlock);
98 VOP_UNLOCK(vp);
99 }
100 if (fs->fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA) &&
101 ump->um_quotas[GRPQUOTA] == NULLVP) {
102 error = VFS_VGET(mp, fs->fs_quotafile[GRPQUOTA], &vp);
103 if (error) {
104 vn_close(ump->um_quotas[USRQUOTA],
105 FREAD|FWRITE, l->l_cred);
106 printf("%s: can't vget() group quota inode: %d\n",
107 mp->mnt_stat.f_mntonname, error);
108 return error;
109 }
110 ump->um_quotas[GRPQUOTA] = vp;
111 ump->um_cred[GRPQUOTA] = l->l_cred;
112 mutex_enter(&vp->v_interlock);
113 vp->v_vflag |= VV_SYSTEM;
114 vp->v_writecount++;
115 mutex_exit(&vp->v_interlock);
116 VOP_UNLOCK(vp);
117 }
118 mp->mnt_flag |= MNT_QUOTA;
119 return 0;
120 }
121