xref: /titanic_41/usr/src/uts/common/fs/dev/sdev_vfsops.c (revision 91c7793e73e8fb0edb9c55f2828d2dfd8ff09994)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * This is the /dev (hence, the sdev_ prefix) filesystem.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/sysmacros.h>
35 #include <sys/systm.h>
36 #include <sys/kmem.h>
37 #include <sys/time.h>
38 #include <sys/pathname.h>
39 #include <sys/vfs.h>
40 #include <sys/vnode.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/uio.h>
44 #include <sys/stat.h>
45 #include <sys/errno.h>
46 #include <sys/cmn_err.h>
47 #include <sys/cred.h>
48 #include <sys/statvfs.h>
49 #include <sys/policy.h>
50 #include <sys/mount.h>
51 #include <sys/debug.h>
52 #include <sys/modctl.h>
53 #include <sys/mkdev.h>
54 #include <fs/fs_subr.h>
55 #include <sys/fs/sdev_impl.h>
56 #include <sys/fs/sdev_node.h>
57 #include <sys/fs/snode.h>
58 #include <sys/fs/dv_node.h>
59 #include <sys/sunndi.h>
60 #include <sys/mntent.h>
61 
62 /*
63  * /dev vfs operations.
64  */
65 
66 /*
67  * globals
68  */
69 struct sdev_data *sdev_origins; /* mount info for origins under /dev */
70 kmutex_t sdev_lock; /* used for mount/unmount/rename synchronization */
71 
72 /*
73  * static
74  */
75 static major_t devmajor;	/* the fictitious major we live on */
76 static major_t devminor;	/* the fictitious minor of this instance */
77 static struct sdev_data *sdev_mntinfo = NULL;	/* linked list of instances */
78 static struct vnode *sdev_stale_attrvp; /* stale root attrvp after remount */
79 static int sdev_mntinfo_cnt;	/* mntinfo reference count */
80 
81 static int sdev_mount(struct vfs *, struct vnode *, struct mounta *,
82     struct cred *);
83 static int sdev_unmount(struct vfs *, int, struct cred *);
84 static int sdev_root(struct vfs *, struct vnode **);
85 static int sdev_statvfs(struct vfs *, struct statvfs64 *);
86 static void sdev_insert_mntinfo(struct sdev_data *);
87 static int devinit(int, char *);
88 
89 static vfsdef_t sdev_vfssw = {
90 	VFSDEF_VERSION,
91 	"dev",		/* type name string */
92 	devinit,	/* init routine */
93 	VSW_CANREMOUNT,	/* flags */
94 	NULL		/* mount options table prototype */
95 };
96 
97 
98 /*
99  * Module linkage information
100  */
101 static struct modlfs modlfs = {
102 	&mod_fsops, "/dev filesystem %I%", &sdev_vfssw
103 };
104 
105 static struct modlinkage modlinkage = {
106 	MODREV_1, (void *)&modlfs, NULL
107 };
108 
109 int
110 _init(void)
111 {
112 	int e;
113 
114 	mutex_init(&sdev_lock, NULL, MUTEX_DEFAULT, NULL);
115 	sdev_node_cache_init();
116 	sdev_devfsadm_lockinit();
117 	if ((e = mod_install(&modlinkage)) != 0) {
118 		sdev_devfsadm_lockdestroy();
119 		sdev_node_cache_fini();
120 		mutex_destroy(&sdev_lock);
121 		return (e);
122 	}
123 	return (0);
124 }
125 
126 /*
127  * dev module remained loaded for the global /dev instance
128  */
129 int
130 _fini(void)
131 {
132 	return (EBUSY);
133 }
134 
135 int
136 _info(struct modinfo *modinfop)
137 {
138 	return (mod_info(&modlinkage, modinfop));
139 }
140 
141 /*ARGSUSED*/
142 static int
143 devinit(int fstype, char *name)
144 {
145 	static const fs_operation_def_t dev_vfsops_tbl[] = {
146 		VFSNAME_MOUNT, sdev_mount,	/* mount file system */
147 		VFSNAME_UNMOUNT, sdev_unmount,	/* unmount file system */
148 		VFSNAME_ROOT, sdev_root,	/* get root vnode */
149 		VFSNAME_STATVFS, sdev_statvfs,	/* get file system statistics */
150 		NULL, NULL
151 	};
152 
153 	int	error;
154 	extern major_t getudev(void);
155 
156 	devtype = fstype;
157 
158 	error = vfs_setfsops(fstype, dev_vfsops_tbl, NULL);
159 	if (error != 0) {
160 		cmn_err(CE_WARN, "devinit: bad vfs ops tbl");
161 		return (error);
162 	}
163 
164 	error = vn_make_ops("dev", sdev_vnodeops_tbl, &sdev_vnodeops);
165 	if (error != 0) {
166 		(void) vfs_freevfsops_by_type(fstype);
167 		cmn_err(CE_WARN, "devinit: bad vnode ops tbl");
168 		return (error);
169 	}
170 
171 	if ((devmajor = getudev()) == (major_t)-1) {
172 		cmn_err(CE_WARN, "%s: can't get unique dev", sdev_vfssw.name);
173 		return (1);
174 	}
175 
176 	/* initialize negative cache */
177 	sdev_ncache_init();
178 
179 	return (0);
180 }
181 
182 /*
183  * Both mount point and backing store directory name are
184  * passed in from userland
185  */
186 static int
187 sdev_mount(struct vfs *vfsp, struct vnode *mvp, struct mounta *uap,
188     struct cred *cr)
189 {
190 	struct sdev_data *sdev_data;
191 	struct vnode *avp;
192 	struct sdev_node *dv;
193 	struct sdev_mountargs *args = NULL;
194 	int	error = 0;
195 	dev_t	devdev;
196 
197 	/*
198 	 * security check
199 	 */
200 	if ((secpolicy_fs_mount(cr, mvp, vfsp) != 0) ||
201 	    (secpolicy_sys_devices(cr) != 0))
202 		return (EPERM);
203 
204 	/*
205 	 * Sanity check the mount point
206 	 */
207 	if (mvp->v_type != VDIR)
208 		return (ENOTDIR);
209 
210 	/*
211 	 * Sanity Check for overlay mount.
212 	 */
213 	mutex_enter(&mvp->v_lock);
214 	if ((uap->flags & MS_OVERLAY) == 0 &&
215 	    (uap->flags & MS_REMOUNT) == 0 &&
216 	    (mvp->v_count > 1 || (mvp->v_flag & VROOT))) {
217 		mutex_exit(&mvp->v_lock);
218 		return (EBUSY);
219 	}
220 	mutex_exit(&mvp->v_lock);
221 
222 	args = kmem_zalloc(sizeof (*args), KM_SLEEP);
223 
224 	if ((uap->flags & MS_DATA) &&
225 	    (uap->datalen != 0 && uap->dataptr != NULL)) {
226 		/* copy in the arguments */
227 		if (error = sdev_copyin_mountargs(uap, args))
228 			goto cleanup;
229 	}
230 
231 	/*
232 	 * Sanity check the backing store
233 	 */
234 	if (args->sdev_attrdir) {
235 		/* user supplied an attribute store */
236 		if (error = lookupname((char *)(uintptr_t)args->sdev_attrdir,
237 		    UIO_USERSPACE, FOLLOW, NULLVPP, &avp)) {
238 			cmn_err(CE_NOTE, "/dev fs: lookup on attribute "
239 			    "directory %s failed",
240 			    (char *)(uintptr_t)args->sdev_attrdir);
241 			goto cleanup;
242 		}
243 
244 		if (avp->v_type != VDIR) {
245 			VN_RELE(avp);
246 			error = ENOTDIR;
247 			goto cleanup;
248 		}
249 	} else {
250 		/* use mountp as the attribute store */
251 		avp = mvp;
252 		VN_HOLD(avp);
253 	}
254 
255 	mutex_enter(&sdev_lock);
256 
257 	/*
258 	 * handling installation
259 	 */
260 	if (uap->flags & MS_REMOUNT) {
261 		sdev_data = (struct sdev_data *)vfsp->vfs_data;
262 		ASSERT(sdev_data);
263 
264 		dv = sdev_data->sdev_root;
265 		ASSERT(dv == dv->sdev_dotdot);
266 
267 		/*
268 		 * mark all existing sdev_nodes (except root node) stale
269 		 */
270 		sdev_stale(dv);
271 
272 		/* Reset previous mountargs */
273 		if (sdev_data->sdev_mountargs) {
274 			kmem_free(sdev_data->sdev_mountargs,
275 			    sizeof (struct sdev_mountargs));
276 		}
277 		sdev_data->sdev_mountargs = args;
278 		args = NULL;		/* so it won't be freed below */
279 
280 		sdev_stale_attrvp = dv->sdev_attrvp;
281 		dv->sdev_attrvp = avp;
282 		vfsp->vfs_mtime = ddi_get_time();
283 
284 		mutex_exit(&sdev_lock);
285 		goto cleanup;				/* we're done */
286 	}
287 
288 	/*
289 	 * Create and initialize the vfs-private data.
290 	 */
291 	devdev = makedevice(devmajor, devminor);
292 	while (vfs_devismounted(devdev)) {
293 		devminor = (devminor + 1) & MAXMIN32;
294 
295 		/*
296 		 * All the minor numbers are used up.
297 		 */
298 		if (devminor == 0) {
299 			mutex_exit(&sdev_lock);
300 			VN_RELE(avp);
301 			error = ENODEV;
302 			goto cleanup;
303 		}
304 
305 		devdev = makedevice(devmajor, devminor);
306 	}
307 
308 	dv = sdev_mkroot(vfsp, devdev, mvp, avp, cr);
309 	sdev_data = kmem_zalloc(sizeof (struct sdev_data), KM_SLEEP);
310 	vfsp->vfs_dev = devdev;
311 	vfsp->vfs_data = (caddr_t)sdev_data;
312 	vfsp->vfs_fstype = devtype;
313 	vfsp->vfs_bsize = DEV_BSIZE;
314 	vfsp->vfs_mtime = ddi_get_time();
315 	vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, devtype);
316 
317 	ASSERT(dv == dv->sdev_dotdot);
318 
319 	sdev_data->sdev_vfsp = vfsp;
320 	sdev_data->sdev_root = dv;
321 	sdev_data->sdev_mountargs = args;
322 
323 	/* get acl flavor from attribute dir */
324 	if (VOP_PATHCONF(avp, _PC_ACL_ENABLED, &sdev_data->sdev_acl_flavor,
325 	    kcred) != 0 || sdev_data->sdev_acl_flavor == 0)
326 		sdev_data->sdev_acl_flavor = _ACL_ACLENT_ENABLED;
327 
328 	args = NULL;			/* so it won't be freed below */
329 	sdev_insert_mntinfo(sdev_data);
330 	mutex_exit(&sdev_lock);
331 
332 	if (!SDEV_IS_GLOBAL(dv)) {
333 		ASSERT(sdev_origins);
334 		dv->sdev_flags &= ~SDEV_GLOBAL;
335 		dv->sdev_origin = sdev_origins->sdev_root;
336 	} else {
337 		sdev_ncache_setup();
338 	}
339 
340 	sdev_update_timestamps(dv->sdev_attrvp,
341 		cr, AT_CTIME|AT_MTIME|AT_ATIME);
342 
343 cleanup:
344 	if (args)
345 		kmem_free(args, sizeof (*args));
346 	return (error);
347 }
348 
349 /*
350  * unmounting the non-global /dev instances, e.g. when deleting a Kevlar zone.
351  */
352 static int
353 sdev_unmount(struct vfs *vfsp, int flag, struct cred *cr)
354 {
355 	struct sdev_node *dv;
356 	int error;
357 	struct sdev_data *sdev_data, *prev, *next;
358 
359 	/*
360 	 * enforce the security policies
361 	 */
362 	if ((secpolicy_fs_unmount(cr, vfsp) != 0) ||
363 	    (secpolicy_sys_devices(cr) != 0))
364 		return (EPERM);
365 
366 	if (flag & MS_FORCE)
367 		return (ENOTSUP);
368 
369 	mutex_enter(&sdev_lock);
370 	dv = VFSTOSDEVFS(vfsp)->sdev_root;
371 	ASSERT(dv == dv->sdev_dotdot);
372 	if (SDEVTOV(dv)->v_count > 1) {
373 		mutex_exit(&sdev_lock);
374 		return (EBUSY);
375 	}
376 
377 	/*
378 	 * global instance remains mounted
379 	 */
380 	if (SDEV_IS_GLOBAL(dv)) {
381 		mutex_exit(&sdev_lock);
382 		return (EBUSY);
383 	}
384 	mutex_exit(&sdev_lock);
385 
386 	/* verify the v_count */
387 	if ((error = sdev_cleandir(dv, NULL, 0)) != 0) {
388 		return (error);
389 	}
390 	ASSERT(SDEVTOV(dv)->v_count == 1);
391 
392 	/* release hold on root node and destroy it */
393 	SDEV_RELE(dv);
394 	dv->sdev_nlink -= 2;
395 	sdev_nodedestroy(dv, 0);
396 
397 	sdev_data = (struct sdev_data *)vfsp->vfs_data;
398 	vfsp->vfs_data = (caddr_t)0;
399 
400 	/*
401 	 * XXX separate it into sdev_delete_mntinfo() if useful
402 	 */
403 	mutex_enter(&sdev_lock);
404 	prev = sdev_data->sdev_prev;
405 	next = sdev_data->sdev_next;
406 	if (prev)
407 		prev->sdev_next = next;
408 	else
409 		sdev_mntinfo = next;
410 	if (next)
411 		next->sdev_prev = prev;
412 	mutex_exit(&sdev_lock);
413 
414 	if (sdev_data->sdev_mountargs) {
415 		kmem_free(sdev_data->sdev_mountargs,
416 		    sizeof (struct sdev_mountargs));
417 	}
418 	kmem_free(sdev_data, sizeof (struct sdev_data));
419 	return (0);
420 }
421 
422 /*
423  * return root vnode for given vfs
424  */
425 static int
426 sdev_root(struct vfs *vfsp, struct vnode **vpp)
427 {
428 	*vpp = SDEVTOV(VFSTOSDEVFS(vfsp)->sdev_root);
429 	VN_HOLD(*vpp);
430 	return (0);
431 }
432 
433 /*
434  * return 'generic superblock' information to userland.
435  *
436  * not much that we can usefully admit to here
437  */
438 static int
439 sdev_statvfs(struct vfs *vfsp, struct statvfs64 *sbp)
440 {
441 	dev32_t d32;
442 
443 	bzero(sbp, sizeof (*sbp));
444 	sbp->f_frsize = sbp->f_bsize = vfsp->vfs_bsize;
445 	sbp->f_files = kmem_cache_stat(sdev_node_cache, "alloc");
446 
447 	/* no illusions that free/avail files is relevant to dev */
448 	sbp->f_ffree = 0;
449 	sbp->f_favail = 0;
450 
451 	/* no illusions that blocks are relevant to devfs */
452 	sbp->f_bfree = 0;
453 	sbp->f_bavail = 0;
454 	sbp->f_blocks = 0;
455 
456 	(void) cmpldev(&d32, vfsp->vfs_dev);
457 	sbp->f_fsid = d32;
458 	(void) strcpy(sbp->f_basetype, vfssw[devtype].vsw_name);
459 	sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
460 	sbp->f_namemax = MAXNAMELEN - 1;
461 	(void) strcpy(sbp->f_fstr, "dev");
462 
463 	return (0);
464 }
465 
466 int
467 sdev_module_register(char *mod_name, struct devname_ops *dev_ops)
468 {
469 	struct devname_nsmap *map = NULL;
470 
471 	if (strcmp(mod_name, DEVNAME_NSCONFIG) == 0) {
472 		devname_ns_ops = dev_ops;
473 		return (0);
474 	}
475 
476 	map = sdev_get_nsmap_by_module(mod_name);
477 	if (map == NULL)
478 		return (EFAULT);
479 
480 	rw_enter(&map->dir_lock, RW_WRITER);
481 	map->dir_ops = dev_ops;
482 	rw_exit(&map->dir_lock);
483 	return (0);
484 }
485 
486 static void
487 sdev_insert_mntinfo(struct sdev_data *data)
488 {
489 	ASSERT(mutex_owned(&sdev_lock));
490 	data->sdev_next = sdev_mntinfo;
491 	data->sdev_prev = NULL;
492 	if (sdev_mntinfo) {
493 		sdev_mntinfo->sdev_prev = data;
494 	} else {
495 		sdev_origins = data;
496 	}
497 	sdev_mntinfo = data;
498 }
499 
500 struct sdev_data *
501 sdev_find_mntinfo(char *mntpt)
502 {
503 	struct sdev_data *mntinfo;
504 
505 	mutex_enter(&sdev_lock);
506 	mntinfo = sdev_mntinfo;
507 	while (mntinfo) {
508 		if (strcmp(mntpt, mntinfo->sdev_root->sdev_name) == 0) {
509 			SDEVTOV(mntinfo->sdev_root)->v_count++;
510 			break;
511 		}
512 		mntinfo = mntinfo->sdev_next;
513 	}
514 	mutex_exit(&sdev_lock);
515 	return (mntinfo);
516 }
517 
518 void
519 sdev_mntinfo_rele(struct sdev_data *mntinfo)
520 {
521 	mutex_enter(&sdev_lock);
522 	SDEVTOV(mntinfo->sdev_root)->v_count--;
523 	mutex_exit(&sdev_lock);
524 }
525