don't bother resolving onbld python module deps
[unleashed.git] / kernel / fs / dev / sdev_vfsops.c
blobb8b990edff536111fd93cd5c9e1ad501a70df41e
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
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 <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>
60 #include <sys/disp.h>
63 * /dev vfs operations.
67 * globals
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;
74 * static
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 *,
84 struct cred *);
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 = {
92 VFSDEF_VERSION,
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
112 _init(void)
114 int e;
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);
123 return (e);
125 return (0);
129 * dev module remained loaded for the global /dev instance
132 _fini(void)
134 return (EBUSY);
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,
150 /*ARGSUSED*/
151 static int
152 devinit(int fstype, char *name)
154 int error;
155 extern major_t getudev(void);
157 devtype = fstype;
159 error = vfs_setfsops(fstype, &dev_vfsops);
160 if (error != 0) {
161 cmn_err(CE_WARN, "devinit: bad fstype");
162 return (error);
165 if ((devmajor = getudev()) == (major_t)-1) {
166 cmn_err(CE_WARN, "%s: can't get unique dev", sdev_vfssw.name);
167 return (1);
170 /* initialize negative cache */
171 sdev_ncache_init();
173 return (0);
177 * Both mount point and backing store directory name are
178 * passed in from userland
180 static int
181 sdev_mount(struct vfs *vfsp, struct vnode *mvp, struct mounta *uap,
182 struct cred *cr)
184 struct sdev_data *sdev_data;
185 struct vnode *avp;
186 struct sdev_node *dv;
187 struct sdev_mountargs *args = NULL;
188 int error = 0;
189 dev_t devdev;
192 * security check
194 if ((secpolicy_fs_mount(cr, mvp, vfsp) != 0) ||
195 (secpolicy_sys_devices(cr) != 0))
196 return (EPERM);
199 * Sanity check the mount point
201 if (mvp->v_type != VDIR)
202 return (ENOTDIR);
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);
212 return (EBUSY);
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))
222 goto cleanup;
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);
235 goto cleanup;
238 if (avp->v_type != VDIR) {
239 VN_RELE(avp);
240 error = ENOTDIR;
241 goto cleanup;
243 } else {
244 /* use mountp as the attribute store */
245 avp = mvp;
246 VN_HOLD(avp);
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) {
258 error = ENOMEM;
259 mutex_exit(&sdev_lock);
260 VN_RELE(avp);
261 goto cleanup;
266 * handling installation
268 if (uap->flags & MS_REMOUNT) {
269 sdev_data = (struct sdev_data *)vfsp->vfs_data;
270 ASSERT(sdev_data);
272 dv = sdev_data->sdev_root;
273 ASSERT(dv == dv->sdev_dotdot);
276 * mark all existing sdev_nodes (except root node) stale
278 sdev_stale(dv);
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.
306 if (devminor == 0) {
307 mutex_exit(&sdev_lock);
308 VN_RELE(avp);
309 error = ENODEV;
310 goto cleanup;
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;
344 } else {
345 sdev_ncache_setup();
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);
354 cleanup:
355 if (args)
356 kmem_free(args, sizeof (*args));
357 return (error);
361 * unmounting the non-global /dev instances, e.g. when deleting a Kevlar zone.
363 static int
364 sdev_unmount(struct vfs *vfsp, int flag, struct cred *cr)
366 struct sdev_node *dv;
367 int error;
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))
375 return (EPERM);
377 if (flag & MS_FORCE)
378 return (ENOTSUP);
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);
385 return (EBUSY);
389 * global instance remains mounted
391 if (SDEV_IS_GLOBAL(dv)) {
392 mutex_exit(&sdev_lock);
393 return (EBUSY);
395 mutex_exit(&sdev_lock);
397 /* verify the v_count */
398 if ((error = sdev_cleandir(dv, NULL, 0)) != 0) {
399 return (error);
401 ASSERT(SDEVTOV(dv)->v_count == 1);
403 /* release hold on root node and destroy it */
404 SDEV_RELE(dv);
405 dv->sdev_nlink -= 2;
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;
417 if (prev)
418 prev->sdev_next = next;
419 else
420 sdev_mntinfo = next;
421 if (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));
430 return (0);
434 * return root vnode for given vfs
436 static int
437 sdev_root(struct vfs *vfsp, struct vnode **vpp)
439 *vpp = SDEVTOV(VFSTOSDEVFS(vfsp)->sdev_root);
440 VN_HOLD(*vpp);
441 return (0);
445 * return 'generic superblock' information to userland.
447 * not much that we can usefully admit to here
449 static int
450 sdev_statvfs(struct vfs *vfsp, struct statvfs64 *sbp)
452 dev32_t d32;
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 */
459 sbp->f_ffree = 0;
460 sbp->f_favail = 0;
462 /* no illusions that blocks are relevant to devfs */
463 sbp->f_bfree = 0;
464 sbp->f_bavail = 0;
465 sbp->f_blocks = 0;
467 (void) cmpldev(&d32, vfsp->vfs_dev);
468 sbp->f_fsid = d32;
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");
474 return (0);
477 static void
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;
483 if (sdev_mntinfo) {
484 sdev_mntinfo->sdev_prev = data;
485 } else {
486 sdev_origins = data;
488 sdev_mntinfo = data;
491 struct sdev_data *
492 sdev_find_mntinfo(char *mntpt)
494 struct sdev_data *mntinfo;
496 mutex_enter(&sdev_lock);
497 mntinfo = sdev_mntinfo;
498 while (mntinfo) {
499 if (strcmp(mntpt, mntinfo->sdev_root->sdev_name) == 0) {
500 VN_HOLD(SDEVTOV(mntinfo->sdev_root));
501 break;
503 mntinfo = mntinfo->sdev_next;
505 mutex_exit(&sdev_lock);
506 return (mntinfo);
509 void
510 sdev_mntinfo_rele(struct sdev_data *mntinfo)
512 vnode_t *vp;
514 mutex_enter(&sdev_lock);
515 vp = SDEVTOV(mntinfo->sdev_root);
516 mutex_enter(&vp->v_lock);
517 VN_RELE_LOCKED(vp);
518 mutex_exit(&vp->v_lock);
519 mutex_exit(&sdev_lock);