ulfs_vfsops.c revision 1.7 1 /* $NetBSD: ulfs_vfsops.c,v 1.7 2013/06/06 00:52:14 dholland Exp $ */
2 /* from NetBSD: ufs_vfsops.c,v 1.52 2013/01/22 09:39:18 dholland Exp */
3
4 /*
5 * Copyright (c) 1991, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)ufs_vfsops.c 8.8 (Berkeley) 5/20/95
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ulfs_vfsops.c,v 1.7 2013/06/06 00:52:14 dholland Exp $");
42
43 #if defined(_KERNEL_OPT)
44 #include "opt_lfs.h"
45 #include "opt_quota.h"
46 #endif
47
48 #include <sys/param.h>
49 #include <sys/mbuf.h>
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/buf.h>
53 #include <sys/vnode.h>
54 #include <sys/kmem.h>
55 #include <sys/kauth.h>
56 #include <sys/quotactl.h>
57
58 #include <miscfs/specfs/specdev.h>
59
60 #include <ufs/lfs/lfs.h>
61 #include <ufs/lfs/ulfs_quotacommon.h>
62 #include <ufs/lfs/ulfs_inode.h>
63 #include <ufs/lfs/ulfsmount.h>
64 #include <ufs/lfs/ulfs_extern.h>
65 #ifdef LFS_DIRHASH
66 #include <ufs/lfs/ulfs_dirhash.h>
67 #endif
68
69 /* how many times ulfs_init() was called */
70 static int ulfs_initcount = 0;
71
72 pool_cache_t ulfs_direct_cache;
73
74 /*
75 * Make a filesystem operational.
76 * Nothing to do at the moment.
77 */
78 /* ARGSUSED */
79 int
80 ulfs_start(struct mount *mp, int flags)
81 {
82
83 return (0);
84 }
85
86 /*
87 * Return the root of a filesystem.
88 */
89 int
90 ulfs_root(struct mount *mp, struct vnode **vpp)
91 {
92 struct vnode *nvp;
93 int error;
94
95 if ((error = VFS_VGET(mp, (ino_t)ULFS_ROOTINO, &nvp)) != 0)
96 return (error);
97 *vpp = nvp;
98 return (0);
99 }
100
101 /*
102 * Do operations associated with quotas
103 */
104 int
105 ulfs_quotactl(struct mount *mp, struct quotactl_args *args)
106 {
107
108 #if !defined(LFS_QUOTA) && !defined(LFS_QUOTA2)
109 (void) mp;
110 (void) args;
111 return (EOPNOTSUPP);
112 #else
113 struct lwp *l = curlwp;
114 int error;
115
116 /* Mark the mount busy, as we're passing it to kauth(9). */
117 error = vfs_busy(mp, NULL);
118 if (error) {
119 return (error);
120 }
121 mutex_enter(&mp->mnt_updating);
122
123 error = lfsquota_handle_cmd(mp, l, args);
124
125 mutex_exit(&mp->mnt_updating);
126 vfs_unbusy(mp, false, NULL);
127 return (error);
128 #endif
129 }
130
131 #if 0
132 switch (cmd) {
133 case Q_SYNC:
134 break;
135
136 case Q_GETQUOTA:
137 /* The user can always query about his own quota. */
138 if (uid == kauth_cred_getuid(l->l_cred))
139 break;
140
141 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
142 KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, KAUTH_ARG(uid), NULL);
143
144 break;
145
146 case Q_QUOTAON:
147 case Q_QUOTAOFF:
148 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
149 KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
150
151 break;
152
153 case Q_SETQUOTA:
154 case Q_SETUSE:
155 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
156 KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(uid), NULL);
157
158 break;
159
160 default:
161 error = EINVAL;
162 break;
163 }
164
165 type = cmds & SUBCMDMASK;
166 if (!error) {
167 /* Only check if there was no error above. */
168 if ((u_int)type >= MAXQUOTAS)
169 error = EINVAL;
170 }
171
172 if (error) {
173 vfs_unbusy(mp, false, NULL);
174 return (error);
175 }
176
177 mutex_enter(&mp->mnt_updating);
178 switch (cmd) {
179
180 case Q_QUOTAON:
181 error = quotaon(l, mp, type, arg);
182 break;
183
184 case Q_QUOTAOFF:
185 error = quotaoff(l, mp, type);
186 break;
187
188 case Q_SETQUOTA:
189 error = setquota(mp, uid, type, arg);
190 break;
191
192 case Q_SETUSE:
193 error = setuse(mp, uid, type, arg);
194 break;
195
196 case Q_GETQUOTA:
197 error = getquota(mp, uid, type, arg);
198 break;
199
200 case Q_SYNC:
201 error = lfs_qsync(mp);
202 break;
203
204 default:
205 error = EINVAL;
206 }
207 mutex_exit(&mp->mnt_updating);
208 vfs_unbusy(mp, false, NULL);
209 return (error);
210 #endif
211
212 /*
213 * This is the generic part of fhtovp called after the underlying
214 * filesystem has validated the file handle.
215 */
216 int
217 ulfs_fhtovp(struct mount *mp, struct ulfs_ufid *ufhp, struct vnode **vpp)
218 {
219 struct vnode *nvp;
220 struct inode *ip;
221 int error;
222
223 if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
224 *vpp = NULLVP;
225 return (error);
226 }
227 ip = VTOI(nvp);
228 KASSERT(ip != NULL);
229 if (ip->i_mode == 0 || ip->i_gen != ufhp->ufid_gen) {
230 vput(nvp);
231 *vpp = NULLVP;
232 return (ESTALE);
233 }
234 *vpp = nvp;
235 return (0);
236 }
237
238 /*
239 * Initialize ULFS filesystems, done only once.
240 */
241 void
242 ulfs_init(void)
243 {
244 if (ulfs_initcount++ > 0)
245 return;
246
247 ulfs_direct_cache = pool_cache_init(sizeof(struct direct), 0, 0, 0,
248 "ulfsdir", NULL, IPL_NONE, NULL, NULL, NULL);
249
250 ulfs_ihashinit();
251 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
252 lfs_dqinit();
253 #endif
254 #ifdef LFS_DIRHASH
255 ulfsdirhash_init();
256 #endif
257 #ifdef LFS_EXTATTR
258 ulfs_extattr_init();
259 #endif
260 }
261
262 void
263 ulfs_reinit(void)
264 {
265 ulfs_ihashreinit();
266 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
267 lfs_dqreinit();
268 #endif
269 }
270
271 /*
272 * Free ULFS filesystem resources, done only once.
273 */
274 void
275 ulfs_done(void)
276 {
277 if (--ulfs_initcount > 0)
278 return;
279
280 ulfs_ihashdone();
281 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
282 lfs_dqdone();
283 #endif
284 pool_cache_destroy(ulfs_direct_cache);
285 #ifdef LFS_DIRHASH
286 ulfsdirhash_done();
287 #endif
288 #ifdef LFS_EXTATTR
289 ulfs_extattr_done();
290 #endif
291 }
292