vfs_init.c revision 1.58 1 /* $NetBSD: vfs_init.c,v 1.58 2022/10/26 23:40:20 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1989, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed
38 * to Berkeley by John Heidemann of the UCLA Ficus project.
39 *
40 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_init.c 8.5 (Berkeley) 5/11/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: vfs_init.c,v 1.58 2022/10/26 23:40:20 riastradh Exp $");
71
72 #include <sys/param.h>
73 #include <sys/mount.h>
74 #include <sys/time.h>
75 #include <sys/vnode.h>
76 #include <sys/stat.h>
77 #include <sys/namei.h>
78 #include <sys/ucred.h>
79 #include <sys/buf.h>
80 #include <sys/errno.h>
81 #include <sys/kmem.h>
82 #include <sys/systm.h>
83 #include <sys/module.h>
84 #include <sys/dirhash.h>
85 #include <sys/sysctl.h>
86 #include <sys/kauth.h>
87
88 #include <miscfs/deadfs/deadfs.h>
89 #include <miscfs/fifofs/fifo.h>
90 #include <miscfs/specfs/specdev.h>
91
92 /*
93 * Sigh, such primitive tools are these...
94 */
95 #if 0
96 #define DODEBUG(A) A
97 #else
98 #define DODEBUG(A)
99 #endif
100
101 pool_cache_t pnbuf_cache;
102
103 /*
104 * The global list of vnode operations.
105 */
106 extern const struct vnodeop_desc * const vfs_op_descs[];
107
108 /*
109 * These vnodeopv_descs are listed here because they are not
110 * associated with any particular file system, and thus cannot
111 * be initialized by vfs_attach().
112 */
113 const struct vnodeopv_desc * const vfs_special_vnodeopv_descs[] = {
114 &dead_vnodeop_opv_desc,
115 &fifo_vnodeop_opv_desc,
116 &spec_vnodeop_opv_desc,
117 NULL,
118 };
119
120 struct vfs_list_head vfs_list = /* vfs list */
121 LIST_HEAD_INITIALIZER(vfs_list);
122
123 static kauth_listener_t mount_listener;
124
125 /*
126 * This code doesn't work if the defn is **vnodop_defns with cc.
127 * The problem is because of the compiler sometimes putting in an
128 * extra level of indirection for arrays. It's an interesting
129 * "feature" of C.
130 */
131 typedef int (*PFI)(void *);
132
133 /*
134 * A miscellaneous routine.
135 * A generic "default" routine that just returns an error.
136 */
137 /*ARGSUSED*/
138 int
139 vn_default_error(void *v)
140 {
141
142 return (EOPNOTSUPP);
143 }
144
145 static struct sysctllog *vfs_sysctllog;
146
147 /*
148 * Top level filesystem related information gathering.
149 */
150 static void
151 sysctl_vfs_setup(void)
152 {
153
154 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
155 CTLFLAG_PERMANENT,
156 CTLTYPE_NODE, "generic",
157 SYSCTL_DESCR("Non-specific vfs related information"),
158 NULL, 0, NULL, 0,
159 CTL_VFS, VFS_GENERIC, CTL_EOL);
160 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
161 CTLFLAG_PERMANENT,
162 CTLTYPE_STRING, "fstypes",
163 SYSCTL_DESCR("List of file systems present"),
164 sysctl_vfs_generic_fstypes, 0, NULL, 0,
165 CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL);
166 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
167 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
168 CTLTYPE_INT, "magiclinks",
169 SYSCTL_DESCR("Whether \"magic\" symlinks are expanded"),
170 NULL, 0, &vfs_magiclinks, 0,
171 CTL_VFS, VFS_GENERIC, VFS_MAGICLINKS, CTL_EOL);
172 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
173 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
174 CTLTYPE_INT, "timestamp_precision",
175 SYSCTL_DESCR("File timestamp precision"),
176 NULL, 0, &vfs_timestamp_precision, 0,
177 CTL_VFS, VFS_GENERIC, VFS_TIMESTAMP_PRECISION,
178 CTL_EOL);
179 }
180
181
182 /*
183 * vfs_init.c
184 *
185 * Allocate and fill in operations vectors.
186 *
187 * An undocumented feature of this approach to defining operations is that
188 * there can be multiple entries in vfs_opv_descs for the same operations
189 * vector. This allows third parties to extend the set of operations
190 * supported by another layer in a binary compatibile way. For example,
191 * assume that NFS needed to be modified to support Ficus. NFS has an entry
192 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
193 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
194 * listing those new operations Ficus adds to NFS, all without modifying the
195 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
196 * that is a(whole)nother story.) This is a feature.
197 */
198
199 /*
200 * Init the vector, if it needs it.
201 * Also handle backwards compatibility.
202 */
203 static void
204 vfs_opv_init_explicit(const struct vnodeopv_desc *vfs_opv_desc)
205 {
206 int (**opv_desc_vector)(void *);
207 const struct vnodeopv_entry_desc *opve_descp;
208
209 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
210
211 for (opve_descp = vfs_opv_desc->opv_desc_ops;
212 opve_descp->opve_op;
213 opve_descp++) {
214 /*
215 * Sanity check: is this operation listed
216 * in the list of operations? We check this
217 * by seeing if its offset is zero. Since
218 * the default routine should always be listed
219 * first, it should be the only one with a zero
220 * offset. Any other operation with a zero
221 * offset is probably not listed in
222 * vfs_op_descs, and so is probably an error.
223 *
224 * A panic here means the layer programmer
225 * has committed the all-too common bug
226 * of adding a new operation to the layer's
227 * list of vnode operations but
228 * not adding the operation to the system-wide
229 * list of supported operations.
230 */
231 if (opve_descp->opve_op->vdesc_offset == 0 &&
232 opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
233 printf("operation %s not listed in %s.\n",
234 opve_descp->opve_op->vdesc_name, "vfs_op_descs");
235 panic ("vfs_opv_init: bad operation");
236 }
237
238 /*
239 * Fill in this entry.
240 */
241 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
242 opve_descp->opve_impl;
243 }
244 }
245
246 static void
247 vfs_opv_init_default(const struct vnodeopv_desc *vfs_opv_desc)
248 {
249 int j;
250 int (**opv_desc_vector)(void *);
251
252 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
253
254 /*
255 * Force every operations vector to have a default routine.
256 */
257 if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
258 panic("vfs_opv_init: operation vector without default routine.");
259
260 for (j = 0; j < VNODE_OPS_COUNT; j++)
261 if (opv_desc_vector[j] == NULL)
262 opv_desc_vector[j] =
263 opv_desc_vector[VOFFSET(vop_default)];
264 }
265
266 void
267 vfs_opv_init(const struct vnodeopv_desc * const *vopvdpp)
268 {
269 int (**opv_desc_vector)(void *);
270 int i;
271
272 /*
273 * Allocate the vectors.
274 */
275 for (i = 0; vopvdpp[i] != NULL; i++) {
276 opv_desc_vector =
277 kmem_alloc(VNODE_OPS_COUNT * sizeof(PFI), KM_SLEEP);
278 memset(opv_desc_vector, 0, VNODE_OPS_COUNT * sizeof(PFI));
279 *(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector;
280 DODEBUG(printf("vector at %p allocated\n",
281 opv_desc_vector_p));
282 }
283
284 /*
285 * ...and fill them in.
286 */
287 for (i = 0; vopvdpp[i] != NULL; i++)
288 vfs_opv_init_explicit(vopvdpp[i]);
289
290 /*
291 * Finally, go back and replace unfilled routines
292 * with their default.
293 */
294 for (i = 0; vopvdpp[i] != NULL; i++)
295 vfs_opv_init_default(vopvdpp[i]);
296 }
297
298 void
299 vfs_opv_free(const struct vnodeopv_desc * const *vopvdpp)
300 {
301 int i;
302
303 /*
304 * Free the vectors allocated in vfs_opv_init().
305 */
306 for (i = 0; vopvdpp[i] != NULL; i++) {
307 kmem_free(*(vopvdpp[i]->opv_desc_vector_p),
308 VNODE_OPS_COUNT * sizeof(PFI));
309 *(vopvdpp[i]->opv_desc_vector_p) = NULL;
310 }
311 }
312
313 #ifdef DEBUG
314 static void
315 vfs_op_check(void)
316 {
317 int i;
318
319 DODEBUG(printf("Vnode_interface_init.\n"));
320
321 /*
322 * Check offset of each op.
323 */
324 for (i = 0; vfs_op_descs[i]; i++) {
325 if (vfs_op_descs[i]->vdesc_offset != i)
326 panic("vfs_op_check: vfs_op_desc[] offset mismatch");
327 }
328
329 if (i != VNODE_OPS_COUNT) {
330 panic("vfs_op_check: vnode ops count mismatch (%d != %d)",
331 i, VNODE_OPS_COUNT);
332 }
333
334 DODEBUG(printf ("vfs_opv_numops=%d\n", VNODE_OPS_COUNT));
335 }
336 #endif /* DEBUG */
337
338 /*
339 * Common routine to check if an unprivileged mount is allowed.
340 *
341 * We export just this part (i.e., without the access control) so that if a
342 * secmodel wants to implement finer grained user mounts it can do so without
343 * copying too much code. More elaborate policies (i.e., specific users allowed
344 * to also create devices and/or introduce set-id binaries, or export
345 * file-systems) will require a different implementation.
346 *
347 * This routine is intended to be called from listener context, and as such
348 * does not take credentials as an argument.
349 */
350 int
351 usermount_common_policy(struct mount *mp, u_long flags)
352 {
353
354 /* No exporting if unprivileged. */
355 if (flags & MNT_EXPORTED)
356 return EPERM;
357
358 /* Must have 'nosuid' and 'nodev'. */
359 if ((flags & MNT_NODEV) == 0 || (flags & MNT_NOSUID) == 0)
360 return EPERM;
361
362 /* Retain 'noexec'. */
363 if ((mp->mnt_flag & MNT_NOEXEC) && (flags & MNT_NOEXEC) == 0)
364 return EPERM;
365
366 return 0;
367 }
368
369 static int
370 mount_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
371 void *arg0, void *arg1, void *arg2, void *arg3)
372 {
373 int result;
374 enum kauth_system_req req;
375
376 result = KAUTH_RESULT_DEFER;
377 req = (enum kauth_system_req)(uintptr_t)(uintptr_t)arg0;
378
379 if (action != KAUTH_SYSTEM_MOUNT)
380 return result;
381
382 if (req == KAUTH_REQ_SYSTEM_MOUNT_GET)
383 result = KAUTH_RESULT_ALLOW;
384 else if (req == KAUTH_REQ_SYSTEM_MOUNT_DEVICE) {
385 vnode_t *devvp = arg2;
386 accmode_t accmode = (accmode_t)(unsigned long)arg3;
387 int error;
388
389 error = VOP_ACCESS(devvp, accmode, cred);
390 if (!error)
391 result = KAUTH_RESULT_ALLOW;
392 }
393
394 return result;
395 }
396
397 /*
398 * Initialize the vnode structures and initialize each file system type.
399 */
400 void
401 vfsinit(void)
402 {
403
404 /*
405 * Attach sysctl nodes
406 */
407 sysctl_vfs_setup();
408
409 /*
410 * Initialize the namei pathname buffer pool and cache.
411 */
412 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
413 NULL, IPL_NONE, NULL, NULL, NULL);
414 KASSERT(pnbuf_cache != NULL);
415
416 /*
417 * Initialize the vnode table
418 */
419 vntblinit();
420
421 /*
422 * Initialize the vnode name cache
423 */
424 nchinit();
425
426 #ifdef DEBUG
427 /*
428 * Check the list of vnode operations.
429 */
430 vfs_op_check();
431 #endif
432
433 /*
434 * Initialize the special vnode operations.
435 */
436 vfs_opv_init(vfs_special_vnodeopv_descs);
437
438 /*
439 * Initialise generic dirhash.
440 */
441 dirhash_init();
442
443 /*
444 * Initialise VFS hooks.
445 */
446 vfs_hooks_init();
447
448 mount_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
449 mount_listener_cb, NULL);
450
451 /*
452 * Establish each file system which was statically
453 * included in the kernel.
454 */
455 module_init_class(MODULE_CLASS_VFS);
456
457 /*
458 * Initialize EVFILT_FS for kqueue.
459 */
460 vfs_evfilt_fs_init();
461 }
462
463 /*
464 * Drop a reference to a file system type.
465 */
466 void
467 vfs_delref(struct vfsops *vfs)
468 {
469
470 mutex_enter(&vfs_list_lock);
471 vfs->vfs_refcount--;
472 mutex_exit(&vfs_list_lock);
473 }
474
475 /*
476 * Establish a file system and initialize it.
477 */
478 int
479 vfs_attach(struct vfsops *vfs)
480 {
481 struct vfsops *v;
482 int error = 0;
483
484 mutex_enter(&vfs_list_lock);
485
486 /*
487 * Make sure this file system doesn't already exist.
488 */
489 LIST_FOREACH(v, &vfs_list, vfs_list) {
490 if (strcmp(vfs->vfs_name, v->vfs_name) == 0) {
491 error = EEXIST;
492 goto out;
493 }
494 }
495
496 /*
497 * Initialize the vnode operations for this file system.
498 */
499 vfs_opv_init(vfs->vfs_opv_descs);
500
501 /*
502 * Now initialize the file system itself.
503 */
504 (*vfs->vfs_init)();
505
506 /*
507 * ...and link it into the kernel's list.
508 */
509 LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list);
510
511 /*
512 * Sanity: make sure the reference count is 0.
513 */
514 vfs->vfs_refcount = 0;
515 out:
516 mutex_exit(&vfs_list_lock);
517 return (error);
518 }
519
520 /*
521 * Remove a file system from the kernel.
522 */
523 int
524 vfs_detach(struct vfsops *vfs)
525 {
526 struct vfsops *v;
527 int error = 0;
528
529 mutex_enter(&vfs_list_lock);
530
531 /*
532 * Make sure no one is using the filesystem.
533 */
534 if (vfs->vfs_refcount != 0) {
535 error = EBUSY;
536 goto out;
537 }
538
539 /*
540 * ...and remove it from the kernel's list.
541 */
542 LIST_FOREACH(v, &vfs_list, vfs_list) {
543 if (v == vfs) {
544 LIST_REMOVE(v, vfs_list);
545 break;
546 }
547 }
548
549 if (v == NULL) {
550 error = ESRCH;
551 goto out;
552 }
553
554 /*
555 * Now run the file system-specific cleanups.
556 */
557 (*vfs->vfs_done)();
558
559 /*
560 * Free the vnode operations vector.
561 */
562 vfs_opv_free(vfs->vfs_opv_descs);
563 out:
564 mutex_exit(&vfs_list_lock);
565 return (error);
566 }
567
568 void
569 vfs_reinit(void)
570 {
571 struct vfsops *vfs;
572
573 mutex_enter(&vfs_list_lock);
574 LIST_FOREACH(vfs, &vfs_list, vfs_list) {
575 if (vfs->vfs_reinit) {
576 vfs->vfs_refcount++;
577 mutex_exit(&vfs_list_lock);
578 (*vfs->vfs_reinit)();
579 mutex_enter(&vfs_list_lock);
580 vfs->vfs_refcount--;
581 }
582 }
583 mutex_exit(&vfs_list_lock);
584 }
585