xref: /netbsd-src/sys/ufs/lfs/ulfs_vfsops.c (revision c2e9cb9413578e44b871b44a79247f15da64f1da)
1 /*	$NetBSD: ulfs_vfsops.c,v 1.16 2020/01/17 20:08:10 ad Exp $	*/
2 /*  from NetBSD: ufs_vfsops.c,v 1.54 2015/03/17 09:39:29 hannken 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.16 2020/01/17 20:08:10 ad 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/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 #include <sys/quotactl.h>
56 
57 #include <miscfs/specfs/specdev.h>
58 
59 #include <ufs/lfs/lfs.h>
60 #include <ufs/lfs/lfs_accessors.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 /*
73  * Make a filesystem operational.
74  * Nothing to do at the moment.
75  */
76 /* ARGSUSED */
77 int
ulfs_start(struct mount * mp,int flags)78 ulfs_start(struct mount *mp, int flags)
79 {
80 
81 	return (0);
82 }
83 
84 /*
85  * Return the root of a filesystem.
86  */
87 int
ulfs_root(struct mount * mp,int lktype,struct vnode ** vpp)88 ulfs_root(struct mount *mp, int lktype, struct vnode **vpp)
89 {
90 	struct vnode *nvp;
91 	int error;
92 
93 	if ((error = VFS_VGET(mp, (ino_t)ULFS_ROOTINO, lktype, &nvp)) != 0)
94 		return (error);
95 	*vpp = nvp;
96 	return (0);
97 }
98 
99 /*
100  * Do operations associated with quotas
101  */
102 int
ulfs_quotactl(struct mount * mp,struct quotactl_args * args)103 ulfs_quotactl(struct mount *mp, struct quotactl_args *args)
104 {
105 
106 #if !defined(LFS_QUOTA) && !defined(LFS_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);
116 	if (error) {
117 		return (error);
118 	}
119 	mutex_enter(mp->mnt_updating);
120 
121 	error = lfsquota_handle_cmd(mp, l, args);
122 
123 	mutex_exit(mp->mnt_updating);
124 	vfs_unbusy(mp);
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);
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 = lfs_qsync(mp);
200 		break;
201 
202 	default:
203 		error = EINVAL;
204 	}
205 	mutex_exit(mp->mnt_updating);
206 	vfs_unbusy(mp);
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
ulfs_fhtovp(struct mount * mp,struct ulfs_ufid * ufhp,int lktype,struct vnode ** vpp)215 ulfs_fhtovp(struct mount *mp, struct ulfs_ufid *ufhp, int lktype,
216     struct vnode **vpp)
217 {
218 	struct vnode *nvp;
219 	struct inode *ip;
220 	int error;
221 
222 	if ((error = VFS_VGET(mp, ufhp->ufid_ino, lktype, &nvp)) != 0) {
223 		if (error == ENOENT)
224 			error = ESTALE;
225 		*vpp = NULLVP;
226 		return (error);
227 	}
228 	ip = VTOI(nvp);
229 	KASSERT(ip != NULL);
230 	if (ip->i_mode == 0 || ip->i_gen != ufhp->ufid_gen) {
231 		vput(nvp);
232 		*vpp = NULLVP;
233 		return (ESTALE);
234 	}
235 	*vpp = nvp;
236 	return (0);
237 }
238 
239 /*
240  * Initialize ULFS filesystems, done only once.
241  */
242 void
ulfs_init(void)243 ulfs_init(void)
244 {
245 	if (ulfs_initcount++ > 0)
246 		return;
247 
248 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
249 	lfs_dqinit();
250 #endif
251 #ifdef LFS_DIRHASH
252 	ulfsdirhash_init();
253 #endif
254 #ifdef LFS_EXTATTR
255 	ulfs_extattr_init();
256 #endif
257 }
258 
259 void
ulfs_reinit(void)260 ulfs_reinit(void)
261 {
262 
263 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
264 	lfs_dqreinit();
265 #endif
266 }
267 
268 /*
269  * Free ULFS filesystem resources, done only once.
270  */
271 void
ulfs_done(void)272 ulfs_done(void)
273 {
274 	if (--ulfs_initcount > 0)
275 		return;
276 
277 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
278 	lfs_dqdone();
279 #endif
280 #ifdef LFS_DIRHASH
281 	ulfsdirhash_done();
282 #endif
283 #ifdef LFS_EXTATTR
284 	ulfs_extattr_done();
285 #endif
286 }
287