ulfs_vfsops.c revision 1.15 1 /* $NetBSD: ulfs_vfsops.c,v 1.15 2019/12/22 19:47:35 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.15 2019/12/22 19:47:35 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
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
88 ulfs_root(struct mount *mp, struct vnode **vpp)
89 {
90 struct vnode *nvp;
91 int error;
92
93 if ((error = VFS_VGET(mp, (ino_t)ULFS_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 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
215 ulfs_fhtovp(struct mount *mp, struct ulfs_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 if (error == ENOENT)
223 error = ESTALE;
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 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
248 lfs_dqinit();
249 #endif
250 #ifdef LFS_DIRHASH
251 ulfsdirhash_init();
252 #endif
253 #ifdef LFS_EXTATTR
254 ulfs_extattr_init();
255 #endif
256 }
257
258 void
259 ulfs_reinit(void)
260 {
261
262 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
263 lfs_dqreinit();
264 #endif
265 }
266
267 /*
268 * Free ULFS filesystem resources, done only once.
269 */
270 void
271 ulfs_done(void)
272 {
273 if (--ulfs_initcount > 0)
274 return;
275
276 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
277 lfs_dqdone();
278 #endif
279 #ifdef LFS_DIRHASH
280 ulfsdirhash_done();
281 #endif
282 #ifdef LFS_EXTATTR
283 ulfs_extattr_done();
284 #endif
285 }
286