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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2015 Joyent, Inc. All rights reserved.
25 * Copyright (c) 2017 by Delphix. All rights reserved.
29 * This is the /dev (hence, the sdev_ prefix) filesystem.
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/sysmacros.h>
35 #include <sys/systm.h>
38 #include <sys/pathname.h>
40 #include <sys/vnode.h>
45 #include <sys/errno.h>
46 #include <sys/cmn_err.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 <sys/fs_subr.h>
55 #include <sys/fs/sdev_impl.h>
56 #include <sys/fs/snode.h>
57 #include <sys/fs/dv_node.h>
58 #include <sys/sunndi.h>
59 #include <sys/mntent.h>
63 * /dev vfs operations.
69 struct sdev_data
*sdev_origins
; /* mount info for origins under /dev */
70 kmutex_t sdev_lock
; /* used for mount/unmount/rename synchronization */
71 taskq_t
*sdev_taskq
= NULL
;
76 static major_t devmajor
; /* the fictitious major we live on */
77 static major_t devminor
; /* the fictitious minor of this instance */
78 static struct sdev_data
*sdev_mntinfo
= NULL
; /* linked list of instances */
80 /* LINTED E_STATIC_UNUSED */ /* useful for debugging */
81 static struct vnode
*sdev_stale_attrvp
; /* stale root attrvp after remount */
83 static int sdev_mount(struct vfs
*, struct vnode
*, struct mounta
*,
85 static int sdev_unmount(struct vfs
*, int, struct cred
*);
86 static int sdev_root(struct vfs
*, struct vnode
**);
87 static int sdev_statvfs(struct vfs
*, struct statvfs64
*);
88 static void sdev_insert_mntinfo(struct sdev_data
*);
89 static int devinit(int, char *);
91 static vfsdef_t sdev_vfssw
= {
93 "dev", /* type name string */
94 devinit
, /* init routine */
95 VSW_CANREMOUNT
, /* flags */
96 NULL
/* mount options table prototype */
101 * Module linkage information
103 static struct modlfs modlfs
= {
104 &mod_fsops
, "/dev filesystem", &sdev_vfssw
107 static struct modlinkage modlinkage
= {
108 MODREV_1
, (void *)&modlfs
, NULL
116 mutex_init(&sdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
117 sdev_node_cache_init();
118 sdev_devfsadm_lockinit();
119 if ((e
= mod_install(&modlinkage
)) != 0) {
120 sdev_devfsadm_lockdestroy();
121 sdev_node_cache_fini();
122 mutex_destroy(&sdev_lock
);
129 * dev module remained loaded for the global /dev instance
138 _info(struct modinfo
*modinfop
)
140 return (mod_info(&modlinkage
, modinfop
));
143 static const struct vfsops dev_vfsops
= {
144 .vfs_mount
= sdev_mount
,
145 .vfs_unmount
= sdev_unmount
,
146 .vfs_root
= sdev_root
,
147 .vfs_statvfs
= sdev_statvfs
,
152 devinit(int fstype
, char *name
)
155 extern major_t
getudev(void);
159 error
= vfs_setfsops(fstype
, &dev_vfsops
);
161 cmn_err(CE_WARN
, "devinit: bad fstype");
165 if ((devmajor
= getudev()) == (major_t
)-1) {
166 cmn_err(CE_WARN
, "%s: can't get unique dev", sdev_vfssw
.name
);
170 /* initialize negative cache */
177 * Both mount point and backing store directory name are
178 * passed in from userland
181 sdev_mount(struct vfs
*vfsp
, struct vnode
*mvp
, struct mounta
*uap
,
184 struct sdev_data
*sdev_data
;
186 struct sdev_node
*dv
;
187 struct sdev_mountargs
*args
= NULL
;
194 if ((secpolicy_fs_mount(cr
, mvp
, vfsp
) != 0) ||
195 (secpolicy_sys_devices(cr
) != 0))
199 * Sanity check the mount point
201 if (mvp
->v_type
!= VDIR
)
205 * Sanity Check for overlay mount.
207 mutex_enter(&mvp
->v_lock
);
208 if ((uap
->flags
& MS_OVERLAY
) == 0 &&
209 (uap
->flags
& MS_REMOUNT
) == 0 &&
210 (mvp
->v_count
> 1 || (mvp
->v_flag
& VROOT
))) {
211 mutex_exit(&mvp
->v_lock
);
214 mutex_exit(&mvp
->v_lock
);
216 args
= kmem_zalloc(sizeof (*args
), KM_SLEEP
);
218 if ((uap
->flags
& MS_DATA
) &&
219 (uap
->datalen
!= 0 && uap
->dataptr
!= NULL
)) {
220 /* copy in the arguments */
221 if (error
= sdev_copyin_mountargs(uap
, args
))
226 * Sanity check the backing store
228 if (args
->sdev_attrdir
) {
229 /* user supplied an attribute store */
230 if (error
= lookupname((char *)(uintptr_t)args
->sdev_attrdir
,
231 UIO_USERSPACE
, FOLLOW
, NULLVPP
, &avp
)) {
232 cmn_err(CE_NOTE
, "/dev fs: lookup on attribute "
233 "directory %s failed",
234 (char *)(uintptr_t)args
->sdev_attrdir
);
238 if (avp
->v_type
!= VDIR
) {
244 /* use mountp as the attribute store */
249 mutex_enter(&sdev_lock
);
252 * Check that the taskq has been created. We can't do this in our
253 * _init or devinit because they run too early for ddi_taskq_create.
255 if (sdev_taskq
== NULL
) {
256 sdev_taskq
= taskq_create("sdev", 1, minclsyspri
, 1, 1, 0);
257 if (sdev_taskq
== NULL
) {
259 mutex_exit(&sdev_lock
);
266 * handling installation
268 if (uap
->flags
& MS_REMOUNT
) {
269 sdev_data
= (struct sdev_data
*)vfsp
->vfs_data
;
272 dv
= sdev_data
->sdev_root
;
273 ASSERT(dv
== dv
->sdev_dotdot
);
276 * mark all existing sdev_nodes (except root node) stale
280 /* Reset previous mountargs */
281 if (sdev_data
->sdev_mountargs
) {
282 kmem_free(sdev_data
->sdev_mountargs
,
283 sizeof (struct sdev_mountargs
));
285 sdev_data
->sdev_mountargs
= args
;
286 args
= NULL
; /* so it won't be freed below */
288 sdev_stale_attrvp
= dv
->sdev_attrvp
;
289 dv
->sdev_attrvp
= avp
;
290 vfsp
->vfs_mtime
= ddi_get_time();
292 mutex_exit(&sdev_lock
);
293 goto cleanup
; /* we're done */
297 * Create and initialize the vfs-private data.
299 devdev
= makedevice(devmajor
, devminor
);
300 while (vfs_devismounted(devdev
)) {
301 devminor
= (devminor
+ 1) & MAXMIN32
;
304 * All the minor numbers are used up.
307 mutex_exit(&sdev_lock
);
313 devdev
= makedevice(devmajor
, devminor
);
316 dv
= sdev_mkroot(vfsp
, devdev
, mvp
, avp
, cr
);
317 sdev_data
= kmem_zalloc(sizeof (struct sdev_data
), KM_SLEEP
);
318 vfsp
->vfs_dev
= devdev
;
319 vfsp
->vfs_data
= (caddr_t
)sdev_data
;
320 vfsp
->vfs_fstype
= devtype
;
321 vfsp
->vfs_bsize
= DEV_BSIZE
;
322 vfsp
->vfs_mtime
= ddi_get_time();
323 vfs_make_fsid(&vfsp
->vfs_fsid
, vfsp
->vfs_dev
, devtype
);
325 ASSERT(dv
== dv
->sdev_dotdot
);
327 sdev_data
->sdev_vfsp
= vfsp
;
328 sdev_data
->sdev_root
= dv
;
329 sdev_data
->sdev_mountargs
= args
;
331 /* get acl flavor from attribute dir */
332 if (fop_pathconf(avp
, _PC_ACL_ENABLED
, &sdev_data
->sdev_acl_flavor
,
333 kcred
, NULL
) != 0 || sdev_data
->sdev_acl_flavor
== 0)
334 sdev_data
->sdev_acl_flavor
= _ACL_ACLENT_ENABLED
;
336 args
= NULL
; /* so it won't be freed below */
337 sdev_insert_mntinfo(sdev_data
);
338 mutex_exit(&sdev_lock
);
340 if (!SDEV_IS_GLOBAL(dv
)) {
341 ASSERT(sdev_origins
);
342 dv
->sdev_flags
&= ~SDEV_GLOBAL
;
343 dv
->sdev_origin
= sdev_origins
->sdev_root
;
346 rw_enter(&dv
->sdev_contents
, RW_WRITER
);
347 sdev_filldir_dynamic(dv
);
348 rw_exit(&dv
->sdev_contents
);
351 sdev_update_timestamps(dv
->sdev_attrvp
,
352 cr
, AT_CTIME
|AT_MTIME
|AT_ATIME
);
356 kmem_free(args
, sizeof (*args
));
361 * unmounting the non-global /dev instances, e.g. when deleting a Kevlar zone.
364 sdev_unmount(struct vfs
*vfsp
, int flag
, struct cred
*cr
)
366 struct sdev_node
*dv
;
368 struct sdev_data
*sdev_data
, *prev
, *next
;
371 * enforce the security policies
373 if ((secpolicy_fs_unmount(cr
, vfsp
) != 0) ||
374 (secpolicy_sys_devices(cr
) != 0))
380 mutex_enter(&sdev_lock
);
381 dv
= VFSTOSDEVFS(vfsp
)->sdev_root
;
382 ASSERT(dv
== dv
->sdev_dotdot
);
383 if (SDEVTOV(dv
)->v_count
> 1) {
384 mutex_exit(&sdev_lock
);
389 * global instance remains mounted
391 if (SDEV_IS_GLOBAL(dv
)) {
392 mutex_exit(&sdev_lock
);
395 mutex_exit(&sdev_lock
);
397 /* verify the v_count */
398 if ((error
= sdev_cleandir(dv
, NULL
, 0)) != 0) {
401 ASSERT(SDEVTOV(dv
)->v_count
== 1);
403 /* release hold on root node and destroy it */
406 sdev_nodedestroy(dv
, 0);
408 sdev_data
= (struct sdev_data
*)vfsp
->vfs_data
;
409 vfsp
->vfs_data
= (caddr_t
)0;
412 * XXX separate it into sdev_delete_mntinfo() if useful
414 mutex_enter(&sdev_lock
);
415 prev
= sdev_data
->sdev_prev
;
416 next
= sdev_data
->sdev_next
;
418 prev
->sdev_next
= next
;
422 next
->sdev_prev
= prev
;
423 mutex_exit(&sdev_lock
);
425 if (sdev_data
->sdev_mountargs
) {
426 kmem_free(sdev_data
->sdev_mountargs
,
427 sizeof (struct sdev_mountargs
));
429 kmem_free(sdev_data
, sizeof (struct sdev_data
));
434 * return root vnode for given vfs
437 sdev_root(struct vfs
*vfsp
, struct vnode
**vpp
)
439 *vpp
= SDEVTOV(VFSTOSDEVFS(vfsp
)->sdev_root
);
445 * return 'generic superblock' information to userland.
447 * not much that we can usefully admit to here
450 sdev_statvfs(struct vfs
*vfsp
, struct statvfs64
*sbp
)
454 bzero(sbp
, sizeof (*sbp
));
455 sbp
->f_frsize
= sbp
->f_bsize
= vfsp
->vfs_bsize
;
456 sbp
->f_files
= kmem_cache_stat(sdev_node_cache
, "alloc");
458 /* no illusions that free/avail files is relevant to dev */
462 /* no illusions that blocks are relevant to devfs */
467 (void) cmpldev(&d32
, vfsp
->vfs_dev
);
469 (void) strcpy(sbp
->f_basetype
, vfssw
[devtype
].vsw_name
);
470 sbp
->f_flag
= vf_to_stf(vfsp
->vfs_flag
);
471 sbp
->f_namemax
= MAXNAMELEN
- 1;
472 (void) strcpy(sbp
->f_fstr
, "dev");
478 sdev_insert_mntinfo(struct sdev_data
*data
)
480 ASSERT(mutex_owned(&sdev_lock
));
481 data
->sdev_next
= sdev_mntinfo
;
482 data
->sdev_prev
= NULL
;
484 sdev_mntinfo
->sdev_prev
= data
;
492 sdev_find_mntinfo(char *mntpt
)
494 struct sdev_data
*mntinfo
;
496 mutex_enter(&sdev_lock
);
497 mntinfo
= sdev_mntinfo
;
499 if (strcmp(mntpt
, mntinfo
->sdev_root
->sdev_name
) == 0) {
500 VN_HOLD(SDEVTOV(mntinfo
->sdev_root
));
503 mntinfo
= mntinfo
->sdev_next
;
505 mutex_exit(&sdev_lock
);
510 sdev_mntinfo_rele(struct sdev_data
*mntinfo
)
514 mutex_enter(&sdev_lock
);
515 vp
= SDEVTOV(mntinfo
->sdev_root
);
516 mutex_enter(&vp
->v_lock
);
518 mutex_exit(&vp
->v_lock
);
519 mutex_exit(&sdev_lock
);