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