xref: /illumos-gate/usr/src/uts/common/fs/namefs/namevfs.c (revision 60405de4d8688d96dd05157c28db3ade5c9bc234)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI" /* from S5R4 1.28 */
31 
32 /*
33  * This file supports the vfs operations for the NAMEFS file system.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/debug.h>
40 #include <sys/errno.h>
41 #include <sys/kmem.h>
42 #include <sys/inline.h>
43 #include <sys/file.h>
44 #include <sys/proc.h>
45 #include <sys/stat.h>
46 #include <sys/statvfs.h>
47 #include <sys/mount.h>
48 #include <sys/sysmacros.h>
49 #include <sys/var.h>
50 #include <sys/vfs.h>
51 #include <sys/vnode.h>
52 #include <sys/mode.h>
53 #include <sys/pcb.h>
54 #include <sys/signal.h>
55 #include <sys/user.h>
56 #include <sys/uio.h>
57 #include <sys/cred.h>
58 #include <sys/fs/namenode.h>
59 #include <sys/stream.h>
60 #include <sys/strsubr.h>
61 #include <sys/cmn_err.h>
62 #include <sys/modctl.h>
63 #include <fs/fs_subr.h>
64 #include <sys/policy.h>
65 #include <sys/vmem.h>
66 #include <sys/fs/sdev_impl.h>
67 
68 #define	NM_INOQUANT		(64 * 1024)
69 
70 /*
71  * Define global data structures.
72  */
73 dev_t	namedev;
74 int	namefstype;
75 struct	namenode *nm_filevp_hash[NM_FILEVP_HASH_SIZE];
76 struct	vfs namevfs;
77 kmutex_t ntable_lock;
78 
79 static vmem_t	*nm_inoarena;	/* vmem arena to allocate inode no's from */
80 static kmutex_t	nm_inolock;
81 
82 /*
83  * Functions to allocate node id's starting from 1. Based on vmem routines.
84  * The vmem arena is extended in NM_INOQUANT chunks.
85  */
86 uint64_t
87 namenodeno_alloc(void)
88 {
89 	uint64_t nno;
90 
91 	mutex_enter(&nm_inolock);
92 	nno = (uint64_t)(uintptr_t)
93 	    vmem_alloc(nm_inoarena, 1, VM_NOSLEEP + VM_FIRSTFIT);
94 	if (nno == 0) {
95 		(void) vmem_add(nm_inoarena, (void *)(vmem_size(nm_inoarena,
96 		    VMEM_ALLOC | VMEM_FREE) + 1), NM_INOQUANT, VM_SLEEP);
97 		nno = (uint64_t)(uintptr_t)
98 		    vmem_alloc(nm_inoarena, 1, VM_SLEEP + VM_FIRSTFIT);
99 		ASSERT(nno != 0);
100 	}
101 	mutex_exit(&nm_inolock);
102 	ASSERT32(nno <= ULONG_MAX);
103 	return (nno);
104 }
105 
106 static void
107 namenodeno_init(void)
108 {
109 	nm_inoarena = vmem_create("namefs_inodes", (void *)1, NM_INOQUANT, 1,
110 	    NULL, NULL, NULL, 1, VM_SLEEP);
111 	mutex_init(&nm_inolock, NULL, MUTEX_DEFAULT, NULL);
112 }
113 
114 void
115 namenodeno_free(uint64_t nn)
116 {
117 	void *vaddr = (void *)(uintptr_t)nn;
118 
119 	ASSERT32((uint64_t)(uintptr_t)vaddr == nn);
120 
121 	mutex_enter(&nm_inolock);
122 	vmem_free(nm_inoarena, vaddr, 1);
123 	mutex_exit(&nm_inolock);
124 }
125 
126 /*
127  * Insert a namenode into the nm_filevp_hash table.
128  *
129  * Each link has a unique namenode with a unique nm_mountvp field.
130  * The nm_filevp field of the namenode need not be unique, since a
131  * file descriptor may be mounted to multiple nodes at the same time.
132  * We hash on nm_filevp since that's what discriminates the searches
133  * in namefind() and nm_unmountall().
134  */
135 void
136 nameinsert(struct namenode *nodep)
137 {
138 	struct namenode **bucket;
139 
140 	ASSERT(MUTEX_HELD(&ntable_lock));
141 
142 	bucket = NM_FILEVP_HASH(nodep->nm_filevp);
143 	nodep->nm_nextp = *bucket;
144 	*bucket = nodep;
145 }
146 
147 /*
148  * Remove a namenode from the hash table, if present.
149  */
150 void
151 nameremove(struct namenode *nodep)
152 {
153 	struct namenode *np, **npp;
154 
155 	ASSERT(MUTEX_HELD(&ntable_lock));
156 
157 	for (npp = NM_FILEVP_HASH(nodep->nm_filevp); (np = *npp) != NULL;
158 	    npp = &np->nm_nextp) {
159 		if (np == nodep) {
160 			*npp = np->nm_nextp;
161 			return;
162 		}
163 	}
164 }
165 
166 /*
167  * Search for a namenode that has a nm_filevp == vp and nm_mountpt == mnt.
168  * If mnt is NULL, return the first link with nm_filevp of vp.
169  * Returns namenode pointer on success, NULL on failure.
170  */
171 struct namenode *
172 namefind(vnode_t *vp, vnode_t *mnt)
173 {
174 	struct namenode *np;
175 
176 	ASSERT(MUTEX_HELD(&ntable_lock));
177 	for (np = *NM_FILEVP_HASH(vp); np != NULL; np = np->nm_nextp)
178 		if (np->nm_filevp == vp &&
179 		    (mnt == NULL || np->nm_mountpt == mnt))
180 			break;
181 	return (np);
182 }
183 
184 /*
185  * Force the unmouting of a file descriptor from ALL of the nodes
186  * that it was mounted to.
187  * At the present time, the only usage for this routine is in the
188  * event one end of a pipe was mounted. At the time the unmounted
189  * end gets closed down, the mounted end is forced to be unmounted.
190  *
191  * This routine searches the namenode hash list for all namenodes
192  * that have a nm_filevp field equal to vp. Each time one is found,
193  * the dounmount() routine is called. This causes the nm_unmount()
194  * routine to be called and thus, the file descriptor is unmounted
195  * from the node.
196  *
197  * At the start of this routine, the reference count for vp is
198  * incremented to protect the vnode from being released in the
199  * event the mount was the only thing keeping the vnode active.
200  * If that is the case, the VOP_CLOSE operation is applied to
201  * the vnode, prior to it being released.
202  */
203 static int
204 nm_umountall(vnode_t *vp, cred_t *crp)
205 {
206 	vfs_t *vfsp;
207 	struct namenode *nodep;
208 	int error = 0;
209 	int realerr = 0;
210 
211 	/*
212 	 * For each namenode that is associated with the file:
213 	 * If the v_vfsp field is not namevfs, dounmount it.  Otherwise,
214 	 * it was created in nm_open() and will be released in time.
215 	 * The following loop replicates some code from nm_find.  That
216 	 * routine can't be used as is since the list isn't strictly
217 	 * consumed as it is traversed.
218 	 */
219 	mutex_enter(&ntable_lock);
220 	nodep = *NM_FILEVP_HASH(vp);
221 	while (nodep) {
222 		if (nodep->nm_filevp == vp &&
223 		    (vfsp = NMTOV(nodep)->v_vfsp) != NULL && vfsp != &namevfs) {
224 
225 			/*
226 			 * If the vn_vfswlock fails, skip the vfs since
227 			 * somebody else may be unmounting it.
228 			 */
229 			if (vn_vfswlock(vfsp->vfs_vnodecovered)) {
230 				realerr = EBUSY;
231 				nodep = nodep->nm_nextp;
232 				continue;
233 			}
234 
235 			/*
236 			 * Can't hold ntable_lock across call to do_unmount
237 			 * because nm_unmount tries to acquire it.  This means
238 			 * there is a window where another mount of vp can
239 			 * happen so it is possible that after nm_unmountall
240 			 * there are still some mounts.  This situation existed
241 			 * without MT locking because dounmount can sleep
242 			 * so another mount could happen during that time.
243 			 * This situation is unlikely and doesn't really cause
244 			 * any problems.
245 			 */
246 			mutex_exit(&ntable_lock);
247 			if ((error = dounmount(vfsp, 0, crp)) != 0)
248 				realerr = error;
249 			mutex_enter(&ntable_lock);
250 			/*
251 			 * Since we dropped the ntable_lock, we
252 			 * have to start over from the beginning.
253 			 * If for some reasons dounmount() fails,
254 			 * start from beginning means that we will keep on
255 			 * trying unless another thread unmounts it for us.
256 			 */
257 			nodep = *NM_FILEVP_HASH(vp);
258 		} else
259 			nodep = nodep->nm_nextp;
260 	}
261 	mutex_exit(&ntable_lock);
262 	return (realerr);
263 }
264 
265 /*
266  * Force the unmouting of a file descriptor from ALL of the nodes
267  * that it was mounted to.  XXX: fifo_close() calls this routine.
268  *
269  * nm_umountall() may return EBUSY.
270  * nm_unmountall() will keep on trying until it succeeds.
271  */
272 int
273 nm_unmountall(vnode_t *vp, cred_t *crp)
274 {
275 	int error;
276 
277 	/*
278 	 * Nm_umuontall() returns only if it succeeds or
279 	 * return with error EBUSY.  If EBUSY, that means
280 	 * it cannot acquire the lock on the covered vnode,
281 	 * and we will keep on trying.
282 	 */
283 	for (;;) {
284 		error = nm_umountall(vp, crp);
285 		if (error != EBUSY)
286 			break;
287 		delay(1);	/* yield cpu briefly, then try again */
288 	}
289 	return (error);
290 }
291 
292 /*
293  * Mount a file descriptor onto the node in the file system.
294  * Create a new vnode, update the attributes with info from the
295  * file descriptor and the mount point.  The mask, mode, uid, gid,
296  * atime, mtime and ctime are taken from the mountpt.  Link count is
297  * set to one, the file system id is namedev and nodeid is unique
298  * for each mounted object.  Other attributes are taken from mount point.
299  * Make sure user is owner (or root) with write permissions on mount point.
300  * Hash the new vnode and return 0.
301  * Upon entry to this routine, the file descriptor is in the
302  * fd field of a struct namefd.  Copy that structure from user
303  * space and retrieve the file descriptor.
304  */
305 static int
306 nm_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *crp)
307 {
308 	struct namefd namefdp;
309 	struct vnode *filevp;		/* file descriptor vnode */
310 	struct file *fp;
311 	struct vnode *newvp;		/* vnode representing this mount */
312 	struct vnode *rvp;		/* realvp (if any) for the mountpt */
313 	struct namenode *nodep;		/* namenode for this mount */
314 	struct vattr filevattr;		/* attributes of file dec.  */
315 	struct vattr *vattrp;		/* attributes of this mount */
316 	char *resource_name;
317 	char *resource_nodetype;
318 	statvfs64_t *svfsp;
319 	int error = 0;
320 
321 	/*
322 	 * Get the file descriptor from user space.
323 	 * Make sure the file descriptor is valid and has an
324 	 * associated file pointer.
325 	 * If so, extract the vnode from the file pointer.
326 	 */
327 	if (uap->datalen != sizeof (struct namefd))
328 		return (EINVAL);
329 
330 	if (copyin(uap->dataptr, &namefdp, uap->datalen))
331 		return (EFAULT);
332 
333 	if ((fp = getf(namefdp.fd)) == NULL)
334 		return (EBADF);
335 
336 	/*
337 	 * If the mount point already has something mounted
338 	 * on it, disallow this mount.  (This restriction may
339 	 * be removed in a later release).
340 	 * Or unmount has completed but the namefs ROOT vnode
341 	 * count has not decremented to zero, disallow this mount.
342 	 */
343 	mutex_enter(&mvp->v_lock);
344 	if ((mvp->v_flag & VROOT) || (mvp->v_vfsp == &namevfs)) {
345 		mutex_exit(&mvp->v_lock);
346 		releasef(namefdp.fd);
347 		return (EBUSY);
348 	}
349 	mutex_exit(&mvp->v_lock);
350 
351 	/*
352 	 * Cannot allow users to fattach() in /dev/pts.
353 	 * First, there is no need for doing so and secondly
354 	 * we cannot allow arbitrary users to park on a
355 	 * /dev/pts node.
356 	 */
357 	rvp = NULLVP;
358 	if (vn_matchops(mvp, spec_getvnodeops()) &&
359 	    VOP_REALVP(mvp, &rvp) == 0 && rvp &&
360 	    vn_matchops(rvp, devpts_getvnodeops())) {
361 		releasef(namefdp.fd);
362 		return (ENOTSUP);
363 	}
364 
365 	filevp = fp->f_vnode;
366 	if (filevp->v_type == VDIR || filevp->v_type == VPORT) {
367 		releasef(namefdp.fd);
368 		return (EINVAL);
369 	}
370 
371 	/*
372 	 * If the fd being mounted refers to neither a door nor a stream,
373 	 * make sure the caller is privileged.
374 	 */
375 	if (filevp->v_type != VDOOR && filevp->v_stream == NULL) {
376 		if (secpolicy_fs_mount(crp, filevp, vfsp) != 0) {
377 			/* fd is neither a stream nor a door */
378 			releasef(namefdp.fd);
379 			return (EINVAL);
380 		}
381 	}
382 
383 	/*
384 	 * Make sure the file descriptor is not the root of some
385 	 * file system.
386 	 * If it's not, create a reference and allocate a namenode
387 	 * to represent this mount request.
388 	 */
389 	if (filevp->v_flag & VROOT) {
390 		releasef(namefdp.fd);
391 		return (EBUSY);
392 	}
393 
394 	nodep = kmem_zalloc(sizeof (struct namenode), KM_SLEEP);
395 
396 	mutex_init(&nodep->nm_lock, NULL, MUTEX_DEFAULT, NULL);
397 	vattrp = &nodep->nm_vattr;
398 	vattrp->va_mask = AT_ALL;
399 	if (error = VOP_GETATTR(mvp, vattrp, 0, crp))
400 		goto out;
401 
402 	filevattr.va_mask = AT_ALL;
403 	if (error = VOP_GETATTR(filevp, &filevattr, 0, crp))
404 		goto out;
405 	/*
406 	 * Make sure the user is the owner of the mount point
407 	 * or has sufficient privileges.
408 	 */
409 	if (error = secpolicy_vnode_owner(crp, vattrp->va_uid))
410 		goto out;
411 
412 	/*
413 	 * Make sure the user has write permissions on the
414 	 * mount point (or has sufficient privileges).
415 	 */
416 	if (!(vattrp->va_mode & VWRITE) &&
417 	    secpolicy_vnode_access(crp, mvp, vattrp->va_uid, VWRITE) != 0) {
418 		error = EACCES;
419 		goto out;
420 	}
421 
422 	/*
423 	 * If the file descriptor has file/record locking, don't
424 	 * allow the mount to succeed.
425 	 */
426 	if (vn_has_flocks(filevp)) {
427 		error = EACCES;
428 		goto out;
429 	}
430 
431 	/*
432 	 * Initialize the namenode.
433 	 */
434 	if (filevp->v_stream) {
435 		struct stdata *stp = filevp->v_stream;
436 		mutex_enter(&stp->sd_lock);
437 		stp->sd_flag |= STRMOUNT;
438 		mutex_exit(&stp->sd_lock);
439 	}
440 	nodep->nm_filevp = filevp;
441 	mutex_enter(&fp->f_tlock);
442 	fp->f_count++;
443 	mutex_exit(&fp->f_tlock);
444 
445 	releasef(namefdp.fd);
446 	nodep->nm_filep = fp;
447 	nodep->nm_mountpt = mvp;
448 
449 	/*
450 	 * The attributes for the mounted file descriptor were initialized
451 	 * above by applying VOP_GETATTR to the mount point.  Some of
452 	 * the fields of the attributes structure will be overwritten
453 	 * by the attributes from the file descriptor.
454 	 */
455 	vattrp->va_type    = filevattr.va_type;
456 	vattrp->va_fsid    = namedev;
457 	vattrp->va_nodeid  = namenodeno_alloc();
458 	vattrp->va_nlink   = 1;
459 	vattrp->va_size    = filevattr.va_size;
460 	vattrp->va_rdev    = filevattr.va_rdev;
461 	vattrp->va_blksize = filevattr.va_blksize;
462 	vattrp->va_nblocks = filevattr.va_nblocks;
463 	vattrp->va_seq	   = 0;
464 
465 	/*
466 	 * Initialize new vnode structure for the mounted file descriptor.
467 	 */
468 	nodep->nm_vnode = vn_alloc(KM_SLEEP);
469 	newvp = NMTOV(nodep);
470 
471 	newvp->v_flag = filevp->v_flag | VROOT | VNOMAP | VNOSWAP;
472 	vn_setops(newvp, nm_vnodeops);
473 	newvp->v_vfsp = vfsp;
474 	newvp->v_stream = filevp->v_stream;
475 	newvp->v_type = filevp->v_type;
476 	newvp->v_rdev = filevp->v_rdev;
477 	newvp->v_data = (caddr_t)nodep;
478 	vn_exists(newvp);
479 
480 	/*
481 	 * Initialize the vfs structure.
482 	 */
483 	vfsp->vfs_vnodecovered = NULL;
484 	vfsp->vfs_flag |= VFS_UNLINKABLE;
485 	vfsp->vfs_bsize = 1024;
486 	vfsp->vfs_fstype = namefstype;
487 	vfs_make_fsid(&vfsp->vfs_fsid, namedev, namefstype);
488 	vfsp->vfs_data = (caddr_t)nodep;
489 	vfsp->vfs_dev = namedev;
490 	vfsp->vfs_bcount = 0;
491 
492 	/*
493 	 * Set the name we mounted from.
494 	 */
495 	switch (filevp->v_type) {
496 	case VPROC:	/* VOP_GETATTR() translates this to VREG */
497 	case VREG:	resource_nodetype = "file"; break;
498 	case VDIR:	resource_nodetype = "directory"; break;
499 	case VBLK:	resource_nodetype = "device"; break;
500 	case VCHR:	resource_nodetype = "device"; break;
501 	case VLNK:	resource_nodetype = "link"; break;
502 	case VFIFO:	resource_nodetype = "fifo"; break;
503 	case VDOOR:	resource_nodetype = "door"; break;
504 	case VSOCK:	resource_nodetype = "socket"; break;
505 	default:	resource_nodetype = "resource"; break;
506 	}
507 
508 #define	RESOURCE_NAME_SZ 128 /* Maximum length of the resource name */
509 	resource_name = kmem_alloc(RESOURCE_NAME_SZ, KM_SLEEP);
510 	svfsp = kmem_alloc(sizeof (statvfs64_t), KM_SLEEP);
511 
512 	error = VFS_STATVFS(filevp->v_vfsp, svfsp);
513 	if (error == 0) {
514 		(void) snprintf(resource_name, RESOURCE_NAME_SZ,
515 		    "unspecified_%s_%s", svfsp->f_basetype, resource_nodetype);
516 	} else {
517 		(void) snprintf(resource_name, RESOURCE_NAME_SZ,
518 		    "unspecified_%s", resource_nodetype);
519 	}
520 
521 	vfs_setresource(vfsp, resource_name);
522 
523 	kmem_free(svfsp, sizeof (statvfs64_t));
524 	kmem_free(resource_name, RESOURCE_NAME_SZ);
525 #undef RESOURCE_NAME_SZ
526 
527 	/*
528 	 * Insert the namenode.
529 	 */
530 	mutex_enter(&ntable_lock);
531 	nameinsert(nodep);
532 	mutex_exit(&ntable_lock);
533 	return (0);
534 out:
535 	releasef(namefdp.fd);
536 	kmem_free(nodep, sizeof (struct namenode));
537 	return (error);
538 }
539 
540 /*
541  * Unmount a file descriptor from a node in the file system.
542  * If the user is not the owner of the file and is not privileged,
543  * the request is denied.
544  * Otherwise, remove the namenode from the hash list.
545  * If the mounted file descriptor was that of a stream and this
546  * was the last mount of the stream, turn off the STRMOUNT flag.
547  * If the rootvp is referenced other than through the mount,
548  * nm_inactive will clean up.
549  */
550 static int
551 nm_unmount(vfs_t *vfsp, int flag, cred_t *crp)
552 {
553 	struct namenode *nodep = (struct namenode *)vfsp->vfs_data;
554 	vnode_t *vp, *thisvp;
555 	struct file *fp = NULL;
556 
557 	ASSERT((nodep->nm_flag & NMNMNT) == 0);
558 
559 	/*
560 	 * forced unmount is not supported by this file system
561 	 * and thus, ENOTSUP, is being returned.
562 	 */
563 	if (flag & MS_FORCE) {
564 		return (ENOTSUP);
565 	}
566 
567 	vp = nodep->nm_filevp;
568 	mutex_enter(&nodep->nm_lock);
569 	if (secpolicy_vnode_owner(crp, nodep->nm_vattr.va_uid) != 0) {
570 		mutex_exit(&nodep->nm_lock);
571 		return (EPERM);
572 	}
573 
574 	mutex_exit(&nodep->nm_lock);
575 
576 	mutex_enter(&ntable_lock);
577 	nameremove(nodep);
578 	thisvp = NMTOV(nodep);
579 	mutex_enter(&thisvp->v_lock);
580 	if (thisvp->v_count-- == 1) {
581 		fp = nodep->nm_filep;
582 		mutex_exit(&thisvp->v_lock);
583 		vn_invalid(thisvp);
584 		vn_free(thisvp);
585 		namenodeno_free(nodep->nm_vattr.va_nodeid);
586 		kmem_free(nodep, sizeof (struct namenode));
587 	} else {
588 		thisvp->v_flag &= ~VROOT;
589 		thisvp->v_vfsp = &namevfs;
590 		mutex_exit(&thisvp->v_lock);
591 	}
592 	if (namefind(vp, NULLVP) == NULL && vp->v_stream) {
593 		struct stdata *stp = vp->v_stream;
594 		mutex_enter(&stp->sd_lock);
595 		stp->sd_flag &= ~STRMOUNT;
596 		mutex_exit(&stp->sd_lock);
597 	}
598 	mutex_exit(&ntable_lock);
599 	if (fp != NULL)
600 		(void) closef(fp);
601 	return (0);
602 }
603 
604 /*
605  * Create a reference to the root of a mounted file descriptor.
606  * This routine is called from lookupname() in the event a path
607  * is being searched that has a mounted file descriptor in it.
608  */
609 static int
610 nm_root(vfs_t *vfsp, vnode_t **vpp)
611 {
612 	struct namenode *nodep = (struct namenode *)vfsp->vfs_data;
613 	struct vnode *vp = NMTOV(nodep);
614 
615 	VN_HOLD(vp);
616 	*vpp = vp;
617 	return (0);
618 }
619 
620 /*
621  * Return in sp the status of this file system.
622  */
623 static int
624 nm_statvfs(vfs_t *vfsp, struct statvfs64 *sp)
625 {
626 	dev32_t d32;
627 
628 	bzero(sp, sizeof (*sp));
629 	sp->f_bsize	= 1024;
630 	sp->f_frsize	= 1024;
631 	(void) cmpldev(&d32, vfsp->vfs_dev);
632 	sp->f_fsid = d32;
633 	(void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
634 	sp->f_flag	= vf_to_stf(vfsp->vfs_flag);
635 	return (0);
636 }
637 
638 /*
639  * Since this file system has no disk blocks of its own, apply
640  * the VOP_FSYNC operation on the mounted file descriptor.
641  */
642 static int
643 nm_sync(vfs_t *vfsp, short flag, cred_t *crp)
644 {
645 	struct namenode *nodep;
646 
647 	if (vfsp == NULL)
648 		return (0);
649 
650 	nodep = (struct namenode *)vfsp->vfs_data;
651 	if (flag & SYNC_CLOSE)
652 		return (nm_umountall(nodep->nm_filevp, crp));
653 
654 	return (VOP_FSYNC(nodep->nm_filevp, FSYNC, crp));
655 }
656 
657 /*
658  * File system initialization routine. Save the file system type,
659  * establish a file system device number and initialize nm_filevp_hash[].
660  */
661 int
662 nameinit(int fstype, char *name)
663 {
664 	static const fs_operation_def_t nm_vfsops_template[] = {
665 		VFSNAME_MOUNT, nm_mount,
666 		VFSNAME_UNMOUNT, nm_unmount,
667 		VFSNAME_ROOT, nm_root,
668 		VFSNAME_STATVFS, nm_statvfs,
669 		VFSNAME_SYNC, (fs_generic_func_p) nm_sync,
670 		NULL, NULL
671 	};
672 	static const fs_operation_def_t nm_dummy_vfsops_template[] = {
673 		VFSNAME_STATVFS, nm_statvfs,
674 		VFSNAME_SYNC, (fs_generic_func_p) nm_sync,
675 		NULL, NULL
676 	};
677 	int error;
678 	int dev;
679 	vfsops_t *namefs_vfsops;
680 	vfsops_t *dummy_vfsops;
681 
682 	error = vfs_setfsops(fstype, nm_vfsops_template, &namefs_vfsops);
683 	if (error != 0) {
684 		cmn_err(CE_WARN, "nameinit: bad vfs ops template");
685 		return (error);
686 	}
687 
688 	error = vfs_makefsops(nm_dummy_vfsops_template, &dummy_vfsops);
689 	if (error != 0) {
690 		(void) vfs_freevfsops_by_type(fstype);
691 		cmn_err(CE_WARN, "nameinit: bad dummy vfs ops template");
692 		return (error);
693 	}
694 
695 	error = vn_make_ops(name, nm_vnodeops_template, &nm_vnodeops);
696 	if (error != 0) {
697 		(void) vfs_freevfsops_by_type(fstype);
698 		vfs_freevfsops(dummy_vfsops);
699 		cmn_err(CE_WARN, "nameinit: bad vnode ops template");
700 		return (error);
701 	}
702 
703 	namefstype = fstype;
704 
705 	if ((dev = getudev()) == (major_t)-1) {
706 		cmn_err(CE_WARN, "nameinit: can't get unique device");
707 		dev = 0;
708 	}
709 	mutex_init(&ntable_lock, NULL, MUTEX_DEFAULT, NULL);
710 	namedev = makedevice(dev, 0);
711 	bzero(nm_filevp_hash, sizeof (nm_filevp_hash));
712 	vfs_setops(&namevfs, dummy_vfsops);
713 	namevfs.vfs_vnodecovered = NULL;
714 	namevfs.vfs_bsize = 1024;
715 	namevfs.vfs_fstype = namefstype;
716 	vfs_make_fsid(&namevfs.vfs_fsid, namedev, namefstype);
717 	namevfs.vfs_dev = namedev;
718 	return (0);
719 }
720 
721 static mntopts_t nm_mntopts = {
722 	NULL,
723 	0
724 };
725 
726 static vfsdef_t vfw = {
727 	VFSDEF_VERSION,
728 	"namefs",
729 	nameinit,
730 	VSW_HASPROTO,
731 	&nm_mntopts
732 };
733 
734 /*
735  * Module linkage information for the kernel.
736  */
737 static struct modlfs modlfs = {
738 	&mod_fsops, "filesystem for namefs", &vfw
739 };
740 
741 static struct modlinkage modlinkage = {
742 	MODREV_1, (void *)&modlfs, NULL
743 };
744 
745 int
746 _init(void)
747 {
748 	namenodeno_init();
749 	return (mod_install(&modlinkage));
750 }
751 
752 int
753 _fini(void)
754 {
755 	return (EBUSY);
756 }
757 
758 int
759 _info(struct modinfo *modinfop)
760 {
761 	return (mod_info(&modlinkage, modinfop));
762 }
763