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