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