vfs_mount.c revision 1.53 1 /* $NetBSD: vfs_mount.c,v 1.53 2017/04/12 10:35:10 hannken Exp $ */
2
3 /*-
4 * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1989, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.53 2017/04/12 10:35:10 hannken Exp $");
71
72 #include <sys/param.h>
73 #include <sys/kernel.h>
74
75 #include <sys/atomic.h>
76 #include <sys/buf.h>
77 #include <sys/conf.h>
78 #include <sys/fcntl.h>
79 #include <sys/filedesc.h>
80 #include <sys/device.h>
81 #include <sys/kauth.h>
82 #include <sys/kmem.h>
83 #include <sys/module.h>
84 #include <sys/mount.h>
85 #include <sys/fstrans.h>
86 #include <sys/namei.h>
87 #include <sys/extattr.h>
88 #include <sys/syscallargs.h>
89 #include <sys/sysctl.h>
90 #include <sys/systm.h>
91 #include <sys/vfs_syscalls.h>
92 #include <sys/vnode_impl.h>
93
94 #include <miscfs/genfs/genfs.h>
95 #include <miscfs/specfs/specdev.h>
96
97 enum mountlist_type {
98 ME_MOUNT,
99 ME_MARKER
100 };
101 struct mountlist_entry {
102 TAILQ_ENTRY(mountlist_entry) me_list; /* Mount list. */
103 struct mount *me_mount; /* Actual mount if ME_MOUNT,
104 current mount else. */
105 enum mountlist_type me_type; /* Mount or marker. */
106 };
107 struct mount_iterator {
108 struct mountlist_entry mi_entry;
109 };
110
111 static TAILQ_HEAD(mountlist, mountlist_entry) mount_list;
112
113 static struct vnode *vfs_vnode_iterator_next1(struct vnode_iterator *,
114 bool (*)(void *, struct vnode *), void *, bool);
115
116 /* Root filesystem. */
117 vnode_t * rootvnode;
118
119 /* Mounted filesystem list. */
120 struct mntlist mountlist;
121 kmutex_t mountlist_lock;
122 int vnode_offset_next_by_mount /* XXX: ugly hack for pstat.c */
123 = offsetof(vnode_impl_t, vi_mntvnodes.tqe_next);
124
125 kmutex_t mntvnode_lock;
126 kmutex_t vfs_list_lock;
127
128 static specificdata_domain_t mount_specificdata_domain;
129 static kmutex_t mntid_lock;
130
131 static kmutex_t mountgen_lock;
132 static uint64_t mountgen;
133
134 void
135 vfs_mount_sysinit(void)
136 {
137
138 TAILQ_INIT(&mount_list);
139 TAILQ_INIT(&mountlist);
140 mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE);
141 mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE);
142 mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE);
143
144 mount_specificdata_domain = specificdata_domain_create();
145 mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE);
146 mutex_init(&mountgen_lock, MUTEX_DEFAULT, IPL_NONE);
147 mountgen = 0;
148 }
149
150 struct mount *
151 vfs_mountalloc(struct vfsops *vfsops, vnode_t *vp)
152 {
153 struct mount *mp;
154 int error __diagused;
155
156 mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
157 if (mp == NULL)
158 return NULL;
159
160 mp->mnt_op = vfsops;
161 mp->mnt_refcnt = 1;
162 TAILQ_INIT(&mp->mnt_vnodelist);
163 mutex_init(&mp->mnt_unmounting, MUTEX_DEFAULT, IPL_NONE);
164 mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
165 mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
166 error = vfs_busy(mp, NULL);
167 KASSERT(error == 0);
168 mp->mnt_vnodecovered = vp;
169 mount_initspecific(mp);
170
171 mutex_enter(&mountgen_lock);
172 mp->mnt_gen = mountgen++;
173 mutex_exit(&mountgen_lock);
174
175 return mp;
176 }
177
178 /*
179 * vfs_rootmountalloc: lookup a filesystem type, and if found allocate and
180 * initialize a mount structure for it.
181 *
182 * Devname is usually updated by mount(8) after booting.
183 */
184 int
185 vfs_rootmountalloc(const char *fstypename, const char *devname,
186 struct mount **mpp)
187 {
188 struct vfsops *vfsp = NULL;
189 struct mount *mp;
190
191 mutex_enter(&vfs_list_lock);
192 LIST_FOREACH(vfsp, &vfs_list, vfs_list)
193 if (!strncmp(vfsp->vfs_name, fstypename,
194 sizeof(mp->mnt_stat.f_fstypename)))
195 break;
196 if (vfsp == NULL) {
197 mutex_exit(&vfs_list_lock);
198 return (ENODEV);
199 }
200 vfsp->vfs_refcount++;
201 mutex_exit(&vfs_list_lock);
202
203 if ((mp = vfs_mountalloc(vfsp, NULL)) == NULL)
204 return ENOMEM;
205 mp->mnt_flag = MNT_RDONLY;
206 (void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name,
207 sizeof(mp->mnt_stat.f_fstypename));
208 mp->mnt_stat.f_mntonname[0] = '/';
209 mp->mnt_stat.f_mntonname[1] = '\0';
210 mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] =
211 '\0';
212 (void)copystr(devname, mp->mnt_stat.f_mntfromname,
213 sizeof(mp->mnt_stat.f_mntfromname) - 1, 0);
214 *mpp = mp;
215 return 0;
216 }
217
218 /*
219 * vfs_getnewfsid: get a new unique fsid.
220 */
221 void
222 vfs_getnewfsid(struct mount *mp)
223 {
224 static u_short xxxfs_mntid;
225 fsid_t tfsid;
226 int mtype;
227
228 mutex_enter(&mntid_lock);
229 mtype = makefstype(mp->mnt_op->vfs_name);
230 mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0);
231 mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype;
232 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
233 if (xxxfs_mntid == 0)
234 ++xxxfs_mntid;
235 tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid);
236 tfsid.__fsid_val[1] = mtype;
237 while (vfs_getvfs(&tfsid)) {
238 tfsid.__fsid_val[0]++;
239 xxxfs_mntid++;
240 }
241 mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
242 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
243 mutex_exit(&mntid_lock);
244 }
245
246 /*
247 * Lookup a mount point by filesystem identifier.
248 *
249 * XXX Needs to add a reference to the mount point.
250 */
251 struct mount *
252 vfs_getvfs(fsid_t *fsid)
253 {
254 mount_iterator_t *iter;
255 struct mount *mp;
256
257 mountlist_iterator_init(&iter);
258 while ((mp = mountlist_iterator_next(iter)) != NULL) {
259 if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
260 mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
261 mountlist_iterator_destroy(iter);
262 return mp;
263 }
264 }
265 mountlist_iterator_destroy(iter);
266 return NULL;
267 }
268
269 /*
270 * Drop a reference to a mount structure, freeing if the last reference.
271 */
272 void
273 vfs_destroy(struct mount *mp)
274 {
275
276 if (__predict_true((int)atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) {
277 return;
278 }
279
280 /*
281 * Nothing else has visibility of the mount: we can now
282 * free the data structures.
283 */
284 KASSERT(mp->mnt_refcnt == 0);
285 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
286 mutex_destroy(&mp->mnt_unmounting);
287 mutex_destroy(&mp->mnt_updating);
288 mutex_destroy(&mp->mnt_renamelock);
289 if (mp->mnt_op != NULL) {
290 vfs_delref(mp->mnt_op);
291 }
292 kmem_free(mp, sizeof(*mp));
293 }
294
295 /*
296 * Mark a mount point as busy, and gain a new reference to it. Used to
297 * prevent the file system from being unmounted during critical sections.
298 *
299 * vfs_busy can be called multiple times and by multiple threads
300 * and must be accompanied by the same number of vfs_unbusy calls.
301 *
302 * => The caller must hold a pre-existing reference to the mount.
303 * => Will fail if the file system is being unmounted, or is unmounted.
304 */
305 int
306 vfs_busy(struct mount *mp, struct mount **nextp)
307 {
308
309 KASSERT(mp->mnt_refcnt > 0);
310
311 mutex_enter(&mp->mnt_unmounting);
312 if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) {
313 mutex_exit(&mp->mnt_unmounting);
314 if (nextp != NULL) {
315 KASSERT(mutex_owned(&mountlist_lock));
316 *nextp = TAILQ_NEXT(mp, mnt_list);
317 }
318 return ENOENT;
319 }
320 ++mp->mnt_busynest;
321 KASSERT(mp->mnt_busynest != 0);
322 mutex_exit(&mp->mnt_unmounting);
323 if (nextp != NULL) {
324 mutex_exit(&mountlist_lock);
325 }
326 atomic_inc_uint(&mp->mnt_refcnt);
327 return 0;
328 }
329
330 /*
331 * Unbusy a busy filesystem.
332 *
333 * Every successful vfs_busy() call must be undone by a vfs_unbusy() call.
334 *
335 * => If keepref is true, preserve reference added by vfs_busy().
336 * => If nextp != NULL, acquire mountlist_lock.
337 */
338 void
339 vfs_unbusy(struct mount *mp, bool keepref, struct mount **nextp)
340 {
341
342 KASSERT(mp->mnt_refcnt > 0);
343
344 if (nextp != NULL) {
345 mutex_enter(&mountlist_lock);
346 }
347 mutex_enter(&mp->mnt_unmounting);
348 KASSERT(mp->mnt_busynest != 0);
349 mp->mnt_busynest--;
350 mutex_exit(&mp->mnt_unmounting);
351 if (!keepref) {
352 vfs_destroy(mp);
353 }
354 if (nextp != NULL) {
355 KASSERT(mutex_owned(&mountlist_lock));
356 *nextp = TAILQ_NEXT(mp, mnt_list);
357 }
358 }
359
360 struct vnode_iterator {
361 vnode_impl_t vi_vnode;
362 };
363
364 void
365 vfs_vnode_iterator_init(struct mount *mp, struct vnode_iterator **vnip)
366 {
367 vnode_t *vp;
368 vnode_impl_t *vip;
369
370 vp = vnalloc_marker(mp);
371 vip = VNODE_TO_VIMPL(vp);
372
373 mutex_enter(&mntvnode_lock);
374 TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vip, vi_mntvnodes);
375 vp->v_usecount = 1;
376 mutex_exit(&mntvnode_lock);
377
378 *vnip = (struct vnode_iterator *)vip;
379 }
380
381 void
382 vfs_vnode_iterator_destroy(struct vnode_iterator *vni)
383 {
384 vnode_impl_t *mvip = &vni->vi_vnode;
385 vnode_t *mvp = VIMPL_TO_VNODE(mvip);
386
387 mutex_enter(&mntvnode_lock);
388 KASSERT(vnis_marker(mvp));
389 if (mvp->v_usecount != 0) {
390 TAILQ_REMOVE(&mvp->v_mount->mnt_vnodelist, mvip, vi_mntvnodes);
391 mvp->v_usecount = 0;
392 }
393 mutex_exit(&mntvnode_lock);
394 vnfree_marker(mvp);
395 }
396
397 static struct vnode *
398 vfs_vnode_iterator_next1(struct vnode_iterator *vni,
399 bool (*f)(void *, struct vnode *), void *cl, bool do_wait)
400 {
401 vnode_impl_t *mvip = &vni->vi_vnode;
402 struct mount *mp = VIMPL_TO_VNODE(mvip)->v_mount;
403 vnode_t *vp;
404 vnode_impl_t *vip;
405 int error;
406
407 KASSERT(vnis_marker(VIMPL_TO_VNODE(mvip)));
408
409 do {
410 mutex_enter(&mntvnode_lock);
411 vip = TAILQ_NEXT(mvip, vi_mntvnodes);
412 TAILQ_REMOVE(&mp->mnt_vnodelist, mvip, vi_mntvnodes);
413 VIMPL_TO_VNODE(mvip)->v_usecount = 0;
414 again:
415 vp = VIMPL_TO_VNODE(vip);
416 if (vp == NULL) {
417 mutex_exit(&mntvnode_lock);
418 return NULL;
419 }
420 mutex_enter(vp->v_interlock);
421 if (vnis_marker(vp) ||
422 vdead_check(vp, (do_wait ? 0 : VDEAD_NOWAIT)) ||
423 (f && !(*f)(cl, vp))) {
424 mutex_exit(vp->v_interlock);
425 vip = TAILQ_NEXT(vip, vi_mntvnodes);
426 goto again;
427 }
428
429 TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vip, mvip, vi_mntvnodes);
430 VIMPL_TO_VNODE(mvip)->v_usecount = 1;
431 mutex_exit(&mntvnode_lock);
432 error = vcache_vget(vp);
433 KASSERT(error == 0 || error == ENOENT);
434 } while (error != 0);
435
436 return vp;
437 }
438
439 struct vnode *
440 vfs_vnode_iterator_next(struct vnode_iterator *vni,
441 bool (*f)(void *, struct vnode *), void *cl)
442 {
443
444 return vfs_vnode_iterator_next1(vni, f, cl, false);
445 }
446
447 /*
448 * Move a vnode from one mount queue to another.
449 */
450 void
451 vfs_insmntque(vnode_t *vp, struct mount *mp)
452 {
453 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
454 struct mount *omp;
455
456 KASSERT(mp == NULL || (mp->mnt_iflag & IMNT_UNMOUNT) == 0 ||
457 vp->v_tag == VT_VFS);
458
459 mutex_enter(&mntvnode_lock);
460 /*
461 * Delete from old mount point vnode list, if on one.
462 */
463 if ((omp = vp->v_mount) != NULL)
464 TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vip, vi_mntvnodes);
465 /*
466 * Insert into list of vnodes for the new mount point, if
467 * available. The caller must take a reference on the mount
468 * structure and donate to the vnode.
469 */
470 if ((vp->v_mount = mp) != NULL)
471 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vip, vi_mntvnodes);
472 mutex_exit(&mntvnode_lock);
473
474 if (omp != NULL) {
475 /* Release reference to old mount. */
476 vfs_destroy(omp);
477 }
478 }
479
480 /*
481 * Remove any vnodes in the vnode table belonging to mount point mp.
482 *
483 * If FORCECLOSE is not specified, there should not be any active ones,
484 * return error if any are found (nb: this is a user error, not a
485 * system error). If FORCECLOSE is specified, detach any active vnodes
486 * that are found.
487 *
488 * If WRITECLOSE is set, only flush out regular file vnodes open for
489 * writing.
490 *
491 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
492 */
493 #ifdef DEBUG
494 int busyprt = 0; /* print out busy vnodes */
495 struct ctldebug debug1 = { "busyprt", &busyprt };
496 #endif
497
498 static vnode_t *
499 vflushnext(struct vnode_iterator *marker, int *when)
500 {
501 if (hardclock_ticks > *when) {
502 yield();
503 *when = hardclock_ticks + hz / 10;
504 }
505 return vfs_vnode_iterator_next1(marker, NULL, NULL, true);
506 }
507
508 /*
509 * Flush one vnode. Referenced on entry, unreferenced on return.
510 */
511 static int
512 vflush_one(vnode_t *vp, vnode_t *skipvp, int flags)
513 {
514 int error;
515 struct vattr vattr;
516
517 if (vp == skipvp ||
518 ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM))) {
519 vrele(vp);
520 return 0;
521 }
522 /*
523 * If WRITECLOSE is set, only flush out regular file
524 * vnodes open for writing or open and unlinked.
525 */
526 if ((flags & WRITECLOSE)) {
527 if (vp->v_type != VREG) {
528 vrele(vp);
529 return 0;
530 }
531 error = vn_lock(vp, LK_EXCLUSIVE);
532 if (error) {
533 KASSERT(error == ENOENT);
534 vrele(vp);
535 return 0;
536 }
537 error = VOP_FSYNC(vp, curlwp->l_cred, FSYNC_WAIT, 0, 0);
538 if (error == 0)
539 error = VOP_GETATTR(vp, &vattr, curlwp->l_cred);
540 VOP_UNLOCK(vp);
541 if (error) {
542 vrele(vp);
543 return error;
544 }
545 if (vp->v_writecount == 0 && vattr.va_nlink > 0) {
546 vrele(vp);
547 return 0;
548 }
549 }
550 /*
551 * First try to recycle the vnode.
552 */
553 if (vrecycle(vp))
554 return 0;
555 /*
556 * If FORCECLOSE is set, forcibly close the vnode.
557 */
558 if (flags & FORCECLOSE) {
559 vgone(vp);
560 return 0;
561 }
562 vrele(vp);
563 return EBUSY;
564 }
565
566 int
567 vflush(struct mount *mp, vnode_t *skipvp, int flags)
568 {
569 vnode_t *vp;
570 struct vnode_iterator *marker;
571 int busy, error, when, retries = 2;
572
573 do {
574 busy = error = when = 0;
575
576 /*
577 * First, flush out any vnode references from the
578 * deferred vrele list.
579 */
580 vfs_drainvnodes();
581
582 vfs_vnode_iterator_init(mp, &marker);
583
584 while ((vp = vflushnext(marker, &when)) != NULL) {
585 error = vflush_one(vp, skipvp, flags);
586 if (error == EBUSY) {
587 error = 0;
588 busy++;
589 #ifdef DEBUG
590 if (busyprt && retries == 0)
591 vprint("vflush: busy vnode", vp);
592 #endif
593 } else if (error != 0) {
594 break;
595 }
596 }
597
598 vfs_vnode_iterator_destroy(marker);
599 } while (error == 0 && busy > 0 && retries-- > 0);
600
601 if (error)
602 return error;
603 if (busy)
604 return EBUSY;
605 return 0;
606 }
607
608 /*
609 * Mount a file system.
610 */
611
612 /*
613 * Scan all active processes to see if any of them have a current or root
614 * directory onto which the new filesystem has just been mounted. If so,
615 * replace them with the new mount point.
616 */
617 static void
618 mount_checkdirs(vnode_t *olddp)
619 {
620 vnode_t *newdp, *rele1, *rele2;
621 struct cwdinfo *cwdi;
622 struct proc *p;
623 bool retry;
624
625 if (olddp->v_usecount == 1) {
626 return;
627 }
628 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
629 panic("mount: lost mount");
630
631 do {
632 retry = false;
633 mutex_enter(proc_lock);
634 PROCLIST_FOREACH(p, &allproc) {
635 if ((cwdi = p->p_cwdi) == NULL)
636 continue;
637 /*
638 * Cannot change to the old directory any more,
639 * so even if we see a stale value it is not a
640 * problem.
641 */
642 if (cwdi->cwdi_cdir != olddp &&
643 cwdi->cwdi_rdir != olddp)
644 continue;
645 retry = true;
646 rele1 = NULL;
647 rele2 = NULL;
648 atomic_inc_uint(&cwdi->cwdi_refcnt);
649 mutex_exit(proc_lock);
650 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
651 if (cwdi->cwdi_cdir == olddp) {
652 rele1 = cwdi->cwdi_cdir;
653 vref(newdp);
654 cwdi->cwdi_cdir = newdp;
655 }
656 if (cwdi->cwdi_rdir == olddp) {
657 rele2 = cwdi->cwdi_rdir;
658 vref(newdp);
659 cwdi->cwdi_rdir = newdp;
660 }
661 rw_exit(&cwdi->cwdi_lock);
662 cwdfree(cwdi);
663 if (rele1 != NULL)
664 vrele(rele1);
665 if (rele2 != NULL)
666 vrele(rele2);
667 mutex_enter(proc_lock);
668 break;
669 }
670 mutex_exit(proc_lock);
671 } while (retry);
672
673 if (rootvnode == olddp) {
674 vrele(rootvnode);
675 vref(newdp);
676 rootvnode = newdp;
677 }
678 vput(newdp);
679 }
680
681 /*
682 * Start extended attributes
683 */
684 static int
685 start_extattr(struct mount *mp)
686 {
687 int error;
688
689 error = VFS_EXTATTRCTL(mp, EXTATTR_CMD_START, NULL, 0, NULL);
690 if (error)
691 printf("%s: failed to start extattr: error = %d\n",
692 mp->mnt_stat.f_mntonname, error);
693
694 return error;
695 }
696
697 int
698 mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops,
699 const char *path, int flags, void *data, size_t *data_len)
700 {
701 vnode_t *vp = *vpp;
702 struct mount *mp;
703 struct pathbuf *pb;
704 struct nameidata nd;
705 int error;
706
707 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
708 KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data);
709 if (error) {
710 vfs_delref(vfsops);
711 return error;
712 }
713
714 /* Cannot make a non-dir a mount-point (from here anyway). */
715 if (vp->v_type != VDIR) {
716 vfs_delref(vfsops);
717 return ENOTDIR;
718 }
719
720 if (flags & MNT_EXPORTED) {
721 vfs_delref(vfsops);
722 return EINVAL;
723 }
724
725 if ((mp = vfs_mountalloc(vfsops, vp)) == NULL) {
726 vfs_delref(vfsops);
727 return ENOMEM;
728 }
729
730 if ((error = fstrans_mount(mp)) != 0) {
731 vfs_unbusy(mp, false, NULL);
732 vfs_destroy(mp);
733 return error;
734 }
735
736 mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
737
738 /*
739 * The underlying file system may refuse the mount for
740 * various reasons. Allow the user to force it to happen.
741 *
742 * Set the mount level flags.
743 */
744 mp->mnt_flag = flags & (MNT_BASIC_FLAGS | MNT_FORCE | MNT_IGNORE);
745
746 mutex_enter(&mp->mnt_updating);
747 error = VFS_MOUNT(mp, path, data, data_len);
748 mp->mnt_flag &= ~MNT_OP_FLAGS;
749
750 if (error != 0)
751 goto err_unmounted;
752
753 /*
754 * Validate and prepare the mount point.
755 */
756 error = pathbuf_copyin(path, &pb);
757 if (error != 0) {
758 goto err_mounted;
759 }
760 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
761 error = namei(&nd);
762 pathbuf_destroy(pb);
763 if (error != 0) {
764 goto err_mounted;
765 }
766 if (nd.ni_vp != vp) {
767 vput(nd.ni_vp);
768 error = EINVAL;
769 goto err_mounted;
770 }
771 if (vp->v_mountedhere != NULL) {
772 vput(nd.ni_vp);
773 error = EBUSY;
774 goto err_mounted;
775 }
776 error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0);
777 if (error != 0) {
778 vput(nd.ni_vp);
779 goto err_mounted;
780 }
781
782 /*
783 * Put the new filesystem on the mount list after root.
784 */
785 cache_purge(vp);
786 mp->mnt_iflag &= ~IMNT_WANTRDWR;
787
788 mountlist_append(mp);
789 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
790 vfs_syncer_add_to_worklist(mp);
791 vp->v_mountedhere = mp;
792 vput(nd.ni_vp);
793
794 mount_checkdirs(vp);
795 mutex_exit(&mp->mnt_updating);
796
797 /* Hold an additional reference to the mount across VFS_START(). */
798 vfs_unbusy(mp, true, NULL);
799 (void) VFS_STATVFS(mp, &mp->mnt_stat);
800 error = VFS_START(mp, 0);
801 if (error) {
802 vrele(vp);
803 } else if (flags & MNT_EXTATTR) {
804 (void)start_extattr(mp);
805 }
806 /* Drop reference held for VFS_START(). */
807 vfs_destroy(mp);
808 *vpp = NULL;
809 return error;
810
811 err_mounted:
812 if (VFS_UNMOUNT(mp, MNT_FORCE) != 0)
813 panic("Unmounting fresh file system failed");
814
815 err_unmounted:
816 vp->v_mountedhere = NULL;
817 mutex_exit(&mp->mnt_updating);
818 fstrans_unmount(mp);
819 vfs_unbusy(mp, false, NULL);
820 vfs_destroy(mp);
821
822 return error;
823 }
824
825 /*
826 * Do the actual file system unmount. File system is assumed to have
827 * been locked by the caller.
828 *
829 * => Caller hold reference to the mount, explicitly for dounmount().
830 */
831 int
832 dounmount(struct mount *mp, int flags, struct lwp *l)
833 {
834 mount_iterator_t *iter;
835 struct mount *cmp;
836 vnode_t *coveredvp;
837 int error, async, used_syncer, used_extattr;
838
839 #if NVERIEXEC > 0
840 error = veriexec_unmountchk(mp);
841 if (error)
842 return (error);
843 #endif /* NVERIEXEC > 0 */
844
845 /*
846 * No unmount below layered mounts.
847 */
848 mountlist_iterator_init(&iter);
849 while ((cmp = mountlist_iterator_next(iter)) != NULL) {
850 if (cmp->mnt_lower == mp) {
851 mountlist_iterator_destroy(iter);
852 return EBUSY;
853 }
854 }
855 mountlist_iterator_destroy(iter);
856
857 /*
858 * XXX Freeze syncer. Must do this before locking the
859 * mount point. See dounmount() for details.
860 */
861 mutex_enter(&syncer_mutex);
862
863 /*
864 * Abort unmount attempt when the filesystem is in use
865 */
866 mutex_enter(&mp->mnt_unmounting);
867 if (mp->mnt_busynest != 0) {
868 mutex_exit(&mp->mnt_unmounting);
869 mutex_exit(&syncer_mutex);
870 return EBUSY;
871 }
872
873 /*
874 * Abort unmount attempt when the filesystem is not mounted
875 */
876 if ((mp->mnt_iflag & IMNT_GONE) != 0) {
877 mutex_exit(&mp->mnt_unmounting);
878 mutex_exit(&syncer_mutex);
879 return ENOENT;
880 }
881
882 used_syncer = (mp->mnt_iflag & IMNT_ONWORKLIST) != 0;
883 used_extattr = mp->mnt_flag & MNT_EXTATTR;
884
885 /*
886 * XXX Syncer must be frozen when we get here. This should really
887 * be done on a per-mountpoint basis, but the syncer doesn't work
888 * like that.
889 *
890 * The caller of dounmount() must acquire syncer_mutex because
891 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
892 * order, and we must preserve that order to avoid deadlock.
893 *
894 * So, if the file system did not use the syncer, now is
895 * the time to release the syncer_mutex.
896 */
897 if (used_syncer == 0) {
898 mutex_exit(&syncer_mutex);
899 }
900 mp->mnt_iflag |= IMNT_UNMOUNT;
901 mutex_enter(&mp->mnt_updating);
902 async = mp->mnt_flag & MNT_ASYNC;
903 mp->mnt_flag &= ~MNT_ASYNC;
904 cache_purgevfs(mp); /* remove cache entries for this file sys */
905 if (used_syncer)
906 vfs_syncer_remove_from_worklist(mp);
907 error = 0;
908 if (((mp->mnt_flag & MNT_RDONLY) == 0) && ((flags & MNT_FORCE) == 0)) {
909 error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
910 }
911 if (error == 0 || (flags & MNT_FORCE)) {
912 error = VFS_UNMOUNT(mp, flags);
913 }
914 if (error) {
915 mp->mnt_iflag &= ~IMNT_UNMOUNT;
916 mutex_exit(&mp->mnt_unmounting);
917 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
918 vfs_syncer_add_to_worklist(mp);
919 mp->mnt_flag |= async;
920 mutex_exit(&mp->mnt_updating);
921 if (used_syncer)
922 mutex_exit(&syncer_mutex);
923 if (used_extattr) {
924 if (start_extattr(mp) != 0)
925 mp->mnt_flag &= ~MNT_EXTATTR;
926 else
927 mp->mnt_flag |= MNT_EXTATTR;
928 }
929 return (error);
930 }
931 mutex_exit(&mp->mnt_updating);
932
933 /*
934 * release mnt_umounting lock here, because other code calls
935 * vfs_busy() while holding the mountlist_lock.
936 *
937 * mark filesystem as gone to prevent further umounts
938 * after mnt_umounting lock is gone, this also prevents
939 * vfs_busy() from succeeding.
940 */
941 mp->mnt_iflag |= IMNT_GONE;
942 mutex_exit(&mp->mnt_unmounting);
943
944 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
945 vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY);
946 coveredvp->v_mountedhere = NULL;
947 VOP_UNLOCK(coveredvp);
948 }
949 mountlist_remove(mp);
950 if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
951 panic("unmount: dangling vnode");
952 if (used_syncer)
953 mutex_exit(&syncer_mutex);
954 vfs_hooks_unmount(mp);
955
956 fstrans_unmount(mp);
957 vfs_destroy(mp); /* reference from mount() */
958 if (coveredvp != NULLVP) {
959 vrele(coveredvp);
960 }
961 return (0);
962 }
963
964 /*
965 * Unmount all file systems.
966 * We traverse the list in reverse order under the assumption that doing so
967 * will avoid needing to worry about dependencies.
968 */
969 bool
970 vfs_unmountall(struct lwp *l)
971 {
972
973 printf("unmounting file systems...\n");
974 return vfs_unmountall1(l, true, true);
975 }
976
977 static void
978 vfs_unmount_print(struct mount *mp, const char *pfx)
979 {
980
981 aprint_verbose("%sunmounted %s on %s type %s\n", pfx,
982 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname,
983 mp->mnt_stat.f_fstypename);
984 }
985
986 /*
987 * Return the mount with the highest generation less than "gen".
988 */
989 static struct mount *
990 vfs_unmount_next(uint64_t gen)
991 {
992 mount_iterator_t *iter;
993 struct mount *mp, *nmp;
994
995 nmp = NULL;
996
997 mountlist_iterator_init(&iter);
998 while ((mp = mountlist_iterator_next(iter)) != NULL) {
999 if ((nmp == NULL || mp->mnt_gen > nmp->mnt_gen) &&
1000 mp->mnt_gen < gen) {
1001 if (nmp != NULL)
1002 vfs_destroy(nmp);
1003 nmp = mp;
1004 atomic_inc_uint(&nmp->mnt_refcnt);
1005 }
1006 }
1007 mountlist_iterator_destroy(iter);
1008
1009 return nmp;
1010 }
1011
1012 bool
1013 vfs_unmount_forceone(struct lwp *l)
1014 {
1015 struct mount *mp;
1016 int error;
1017
1018 mp = vfs_unmount_next(mountgen);
1019 if (mp == NULL) {
1020 return false;
1021 }
1022
1023 #ifdef DEBUG
1024 printf("forcefully unmounting %s (%s)...\n",
1025 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
1026 #endif
1027 if ((error = dounmount(mp, MNT_FORCE, l)) == 0) {
1028 vfs_unmount_print(mp, "forcefully ");
1029 return true;
1030 } else {
1031 vfs_destroy(mp);
1032 }
1033
1034 #ifdef DEBUG
1035 printf("forceful unmount of %s failed with error %d\n",
1036 mp->mnt_stat.f_mntonname, error);
1037 #endif
1038
1039 return false;
1040 }
1041
1042 bool
1043 vfs_unmountall1(struct lwp *l, bool force, bool verbose)
1044 {
1045 struct mount *mp;
1046 bool any_error = false, progress = false;
1047 uint64_t gen;
1048 int error;
1049
1050 gen = mountgen;
1051 for (;;) {
1052 mp = vfs_unmount_next(gen);
1053 if (mp == NULL)
1054 break;
1055 gen = mp->mnt_gen;
1056
1057 #ifdef DEBUG
1058 printf("unmounting %p %s (%s)...\n",
1059 (void *)mp, mp->mnt_stat.f_mntonname,
1060 mp->mnt_stat.f_mntfromname);
1061 #endif
1062 if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) {
1063 vfs_unmount_print(mp, "");
1064 progress = true;
1065 } else {
1066 vfs_destroy(mp);
1067 if (verbose) {
1068 printf("unmount of %s failed with error %d\n",
1069 mp->mnt_stat.f_mntonname, error);
1070 }
1071 any_error = true;
1072 }
1073 }
1074 if (verbose) {
1075 printf("unmounting done\n");
1076 }
1077 if (any_error && verbose) {
1078 printf("WARNING: some file systems would not unmount\n");
1079 }
1080 return progress;
1081 }
1082
1083 void
1084 vfs_sync_all(struct lwp *l)
1085 {
1086 printf("syncing disks... ");
1087
1088 /* remove user processes from run queue */
1089 suspendsched();
1090 (void)spl0();
1091
1092 /* avoid coming back this way again if we panic. */
1093 doing_shutdown = 1;
1094
1095 do_sys_sync(l);
1096
1097 /* Wait for sync to finish. */
1098 if (buf_syncwait() != 0) {
1099 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
1100 Debugger();
1101 #endif
1102 printf("giving up\n");
1103 return;
1104 } else
1105 printf("done\n");
1106 }
1107
1108 /*
1109 * Sync and unmount file systems before shutting down.
1110 */
1111 void
1112 vfs_shutdown(void)
1113 {
1114 lwp_t *l = curlwp;
1115
1116 vfs_sync_all(l);
1117
1118 /*
1119 * If we have paniced - do not make the situation potentially
1120 * worse by unmounting the file systems.
1121 */
1122 if (panicstr != NULL) {
1123 return;
1124 }
1125
1126 /* Unmount file systems. */
1127 vfs_unmountall(l);
1128 }
1129
1130 /*
1131 * Print a list of supported file system types (used by vfs_mountroot)
1132 */
1133 static void
1134 vfs_print_fstypes(void)
1135 {
1136 struct vfsops *v;
1137 int cnt = 0;
1138
1139 mutex_enter(&vfs_list_lock);
1140 LIST_FOREACH(v, &vfs_list, vfs_list)
1141 ++cnt;
1142 mutex_exit(&vfs_list_lock);
1143
1144 if (cnt == 0) {
1145 printf("WARNING: No file system modules have been loaded.\n");
1146 return;
1147 }
1148
1149 printf("Supported file systems:");
1150 mutex_enter(&vfs_list_lock);
1151 LIST_FOREACH(v, &vfs_list, vfs_list) {
1152 printf(" %s", v->vfs_name);
1153 }
1154 mutex_exit(&vfs_list_lock);
1155 printf("\n");
1156 }
1157
1158 /*
1159 * Mount the root file system. If the operator didn't specify a
1160 * file system to use, try all possible file systems until one
1161 * succeeds.
1162 */
1163 int
1164 vfs_mountroot(void)
1165 {
1166 struct vfsops *v;
1167 int error = ENODEV;
1168
1169 if (root_device == NULL)
1170 panic("vfs_mountroot: root device unknown");
1171
1172 switch (device_class(root_device)) {
1173 case DV_IFNET:
1174 if (rootdev != NODEV)
1175 panic("vfs_mountroot: rootdev set for DV_IFNET "
1176 "(0x%llx -> %llu,%llu)",
1177 (unsigned long long)rootdev,
1178 (unsigned long long)major(rootdev),
1179 (unsigned long long)minor(rootdev));
1180 break;
1181
1182 case DV_DISK:
1183 if (rootdev == NODEV)
1184 panic("vfs_mountroot: rootdev not set for DV_DISK");
1185 if (bdevvp(rootdev, &rootvp))
1186 panic("vfs_mountroot: can't get vnode for rootdev");
1187 error = VOP_OPEN(rootvp, FREAD, FSCRED);
1188 if (error) {
1189 printf("vfs_mountroot: can't open root device\n");
1190 return (error);
1191 }
1192 break;
1193
1194 case DV_VIRTUAL:
1195 break;
1196
1197 default:
1198 printf("%s: inappropriate for root file system\n",
1199 device_xname(root_device));
1200 return (ENODEV);
1201 }
1202
1203 /*
1204 * If user specified a root fs type, use it. Make sure the
1205 * specified type exists and has a mount_root()
1206 */
1207 if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) {
1208 v = vfs_getopsbyname(rootfstype);
1209 error = EFTYPE;
1210 if (v != NULL) {
1211 if (v->vfs_mountroot != NULL) {
1212 error = (v->vfs_mountroot)();
1213 }
1214 v->vfs_refcount--;
1215 }
1216 goto done;
1217 }
1218
1219 /*
1220 * Try each file system currently configured into the kernel.
1221 */
1222 mutex_enter(&vfs_list_lock);
1223 LIST_FOREACH(v, &vfs_list, vfs_list) {
1224 if (v->vfs_mountroot == NULL)
1225 continue;
1226 #ifdef DEBUG
1227 aprint_normal("mountroot: trying %s...\n", v->vfs_name);
1228 #endif
1229 v->vfs_refcount++;
1230 mutex_exit(&vfs_list_lock);
1231 error = (*v->vfs_mountroot)();
1232 mutex_enter(&vfs_list_lock);
1233 v->vfs_refcount--;
1234 if (!error) {
1235 aprint_normal("root file system type: %s\n",
1236 v->vfs_name);
1237 break;
1238 }
1239 }
1240 mutex_exit(&vfs_list_lock);
1241
1242 if (v == NULL) {
1243 vfs_print_fstypes();
1244 printf("no file system for %s", device_xname(root_device));
1245 if (device_class(root_device) == DV_DISK)
1246 printf(" (dev 0x%llx)", (unsigned long long)rootdev);
1247 printf("\n");
1248 error = EFTYPE;
1249 }
1250
1251 done:
1252 if (error && device_class(root_device) == DV_DISK) {
1253 VOP_CLOSE(rootvp, FREAD, FSCRED);
1254 vrele(rootvp);
1255 }
1256 if (error == 0) {
1257 mount_iterator_t *iter;
1258 struct mount *mp;
1259 extern struct cwdinfo cwdi0;
1260
1261 mountlist_iterator_init(&iter);
1262 mp = mountlist_iterator_next(iter);
1263 KASSERT(mp != NULL);
1264 mountlist_iterator_destroy(iter);
1265
1266 mp->mnt_flag |= MNT_ROOTFS;
1267 mp->mnt_op->vfs_refcount++;
1268 error = fstrans_mount(mp);
1269 KASSERT(error == 0);
1270
1271 /*
1272 * Get the vnode for '/'. Set cwdi0.cwdi_cdir to
1273 * reference it.
1274 */
1275 error = VFS_ROOT(mp, &rootvnode);
1276 if (error)
1277 panic("cannot find root vnode, error=%d", error);
1278 cwdi0.cwdi_cdir = rootvnode;
1279 vref(cwdi0.cwdi_cdir);
1280 VOP_UNLOCK(rootvnode);
1281 cwdi0.cwdi_rdir = NULL;
1282
1283 /*
1284 * Now that root is mounted, we can fixup initproc's CWD
1285 * info. All other processes are kthreads, which merely
1286 * share proc0's CWD info.
1287 */
1288 initproc->p_cwdi->cwdi_cdir = rootvnode;
1289 vref(initproc->p_cwdi->cwdi_cdir);
1290 initproc->p_cwdi->cwdi_rdir = NULL;
1291 /*
1292 * Enable loading of modules from the filesystem
1293 */
1294 module_load_vfs_init();
1295
1296 }
1297 return (error);
1298 }
1299
1300 /*
1301 * mount_specific_key_create --
1302 * Create a key for subsystem mount-specific data.
1303 */
1304 int
1305 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
1306 {
1307
1308 return specificdata_key_create(mount_specificdata_domain, keyp, dtor);
1309 }
1310
1311 /*
1312 * mount_specific_key_delete --
1313 * Delete a key for subsystem mount-specific data.
1314 */
1315 void
1316 mount_specific_key_delete(specificdata_key_t key)
1317 {
1318
1319 specificdata_key_delete(mount_specificdata_domain, key);
1320 }
1321
1322 /*
1323 * mount_initspecific --
1324 * Initialize a mount's specificdata container.
1325 */
1326 void
1327 mount_initspecific(struct mount *mp)
1328 {
1329 int error __diagused;
1330
1331 error = specificdata_init(mount_specificdata_domain,
1332 &mp->mnt_specdataref);
1333 KASSERT(error == 0);
1334 }
1335
1336 /*
1337 * mount_finispecific --
1338 * Finalize a mount's specificdata container.
1339 */
1340 void
1341 mount_finispecific(struct mount *mp)
1342 {
1343
1344 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
1345 }
1346
1347 /*
1348 * mount_getspecific --
1349 * Return mount-specific data corresponding to the specified key.
1350 */
1351 void *
1352 mount_getspecific(struct mount *mp, specificdata_key_t key)
1353 {
1354
1355 return specificdata_getspecific(mount_specificdata_domain,
1356 &mp->mnt_specdataref, key);
1357 }
1358
1359 /*
1360 * mount_setspecific --
1361 * Set mount-specific data corresponding to the specified key.
1362 */
1363 void
1364 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data)
1365 {
1366
1367 specificdata_setspecific(mount_specificdata_domain,
1368 &mp->mnt_specdataref, key, data);
1369 }
1370
1371 /*
1372 * Check to see if a filesystem is mounted on a block device.
1373 */
1374 int
1375 vfs_mountedon(vnode_t *vp)
1376 {
1377 vnode_t *vq;
1378 int error = 0;
1379
1380 if (vp->v_type != VBLK)
1381 return ENOTBLK;
1382 if (spec_node_getmountedfs(vp) != NULL)
1383 return EBUSY;
1384 if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, &vq) == 0) {
1385 if (spec_node_getmountedfs(vq) != NULL)
1386 error = EBUSY;
1387 vrele(vq);
1388 }
1389
1390 return error;
1391 }
1392
1393 /*
1394 * Check if a device pointed to by vp is mounted.
1395 *
1396 * Returns:
1397 * EINVAL if it's not a disk
1398 * EBUSY if it's a disk and mounted
1399 * 0 if it's a disk and not mounted
1400 */
1401 int
1402 rawdev_mounted(vnode_t *vp, vnode_t **bvpp)
1403 {
1404 vnode_t *bvp;
1405 dev_t dev;
1406 int d_type;
1407
1408 bvp = NULL;
1409 d_type = D_OTHER;
1410
1411 if (iskmemvp(vp))
1412 return EINVAL;
1413
1414 switch (vp->v_type) {
1415 case VCHR: {
1416 const struct cdevsw *cdev;
1417
1418 dev = vp->v_rdev;
1419 cdev = cdevsw_lookup(dev);
1420 if (cdev != NULL) {
1421 dev_t blkdev;
1422
1423 blkdev = devsw_chr2blk(dev);
1424 if (blkdev != NODEV) {
1425 if (vfinddev(blkdev, VBLK, &bvp) != 0) {
1426 d_type = (cdev->d_flag & D_TYPEMASK);
1427 /* XXX: what if bvp disappears? */
1428 vrele(bvp);
1429 }
1430 }
1431 }
1432
1433 break;
1434 }
1435
1436 case VBLK: {
1437 const struct bdevsw *bdev;
1438
1439 dev = vp->v_rdev;
1440 bdev = bdevsw_lookup(dev);
1441 if (bdev != NULL)
1442 d_type = (bdev->d_flag & D_TYPEMASK);
1443
1444 bvp = vp;
1445
1446 break;
1447 }
1448
1449 default:
1450 break;
1451 }
1452
1453 if (d_type != D_DISK)
1454 return EINVAL;
1455
1456 if (bvpp != NULL)
1457 *bvpp = bvp;
1458
1459 /*
1460 * XXX: This is bogus. We should be failing the request
1461 * XXX: not only if this specific slice is mounted, but
1462 * XXX: if it's on a disk with any other mounted slice.
1463 */
1464 if (vfs_mountedon(bvp))
1465 return EBUSY;
1466
1467 return 0;
1468 }
1469
1470 /*
1471 * Make a 'unique' number from a mount type name.
1472 */
1473 long
1474 makefstype(const char *type)
1475 {
1476 long rv;
1477
1478 for (rv = 0; *type; type++) {
1479 rv <<= 2;
1480 rv ^= *type;
1481 }
1482 return rv;
1483 }
1484
1485 static struct mountlist_entry *
1486 mountlist_alloc(enum mountlist_type type, struct mount *mp)
1487 {
1488 struct mountlist_entry *me;
1489
1490 me = kmem_zalloc(sizeof(*me), KM_SLEEP);
1491 me->me_mount = mp;
1492 me->me_type = type;
1493
1494 return me;
1495 }
1496
1497 static void
1498 mountlist_free(struct mountlist_entry *me)
1499 {
1500
1501 kmem_free(me, sizeof(*me));
1502 }
1503
1504 void
1505 mountlist_iterator_init(mount_iterator_t **mip)
1506 {
1507 struct mountlist_entry *me;
1508
1509 me = mountlist_alloc(ME_MARKER, NULL);
1510 mutex_enter(&mountlist_lock);
1511 TAILQ_INSERT_HEAD(&mount_list, me, me_list);
1512 mutex_exit(&mountlist_lock);
1513 *mip = (mount_iterator_t *)me;
1514 }
1515
1516 void
1517 mountlist_iterator_destroy(mount_iterator_t *mi)
1518 {
1519 struct mountlist_entry *marker = &mi->mi_entry;
1520
1521 if (marker->me_mount != NULL)
1522 vfs_unbusy(marker->me_mount, false, NULL);
1523
1524 mutex_enter(&mountlist_lock);
1525 TAILQ_REMOVE(&mount_list, marker, me_list);
1526 mutex_exit(&mountlist_lock);
1527
1528 mountlist_free(marker);
1529
1530 }
1531
1532 /*
1533 * Return the next mount or NULL for this iterator.
1534 * Mark it busy on success.
1535 */
1536 struct mount *
1537 mountlist_iterator_next(mount_iterator_t *mi)
1538 {
1539 struct mountlist_entry *me, *marker = &mi->mi_entry;
1540 struct mount *mp;
1541
1542 if (marker->me_mount != NULL) {
1543 vfs_unbusy(marker->me_mount, false, NULL);
1544 marker->me_mount = NULL;
1545 }
1546
1547 mutex_enter(&mountlist_lock);
1548 for (;;) {
1549 KASSERT(marker->me_type == ME_MARKER);
1550
1551 me = TAILQ_NEXT(marker, me_list);
1552 if (me == NULL) {
1553 /* End of list: keep marker and return. */
1554 mutex_exit(&mountlist_lock);
1555 return NULL;
1556 }
1557 TAILQ_REMOVE(&mount_list, marker, me_list);
1558 TAILQ_INSERT_AFTER(&mount_list, me, marker, me_list);
1559
1560 /* Skip other markers. */
1561 if (me->me_type != ME_MOUNT)
1562 continue;
1563
1564 /* Take an initial reference for vfs_busy() below. */
1565 mp = me->me_mount;
1566 KASSERT(mp != NULL);
1567 atomic_inc_uint(&mp->mnt_refcnt);
1568 mutex_exit(&mountlist_lock);
1569
1570 /* Try to mark this mount busy and return on success. */
1571 if (vfs_busy(mp, NULL) == 0) {
1572 vfs_destroy(mp);
1573 marker->me_mount = mp;
1574 return mp;
1575 }
1576 vfs_destroy(mp);
1577 mutex_enter(&mountlist_lock);
1578 }
1579 }
1580
1581 /*
1582 * Attach new mount to the end of the mount list.
1583 */
1584 void
1585 mountlist_append(struct mount *mp)
1586 {
1587 struct mountlist_entry *me;
1588
1589 me = mountlist_alloc(ME_MOUNT, mp);
1590 mutex_enter(&mountlist_lock);
1591 TAILQ_INSERT_TAIL(&mount_list, me, me_list);
1592 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1593 mutex_exit(&mountlist_lock);
1594 }
1595
1596 /*
1597 * Remove mount from mount list.
1598 */void
1599 mountlist_remove(struct mount *mp)
1600 {
1601 struct mountlist_entry *me;
1602
1603 mutex_enter(&mountlist_lock);
1604 TAILQ_FOREACH(me, &mount_list, me_list)
1605 if (me->me_type == ME_MOUNT && me->me_mount == mp)
1606 break;
1607 KASSERT(me != NULL);
1608 TAILQ_REMOVE(&mount_list, me, me_list);
1609 TAILQ_REMOVE(&mountlist, mp, mnt_list);
1610 mutex_exit(&mountlist_lock);
1611 mountlist_free(me);
1612 }
1613
1614 /*
1615 * Unlocked variant to traverse the mountlist.
1616 * To be used from DDB only.
1617 */
1618 struct mount *
1619 _mountlist_next(struct mount *mp)
1620 {
1621 struct mountlist_entry *me;
1622
1623 if (mp == NULL) {
1624 me = TAILQ_FIRST(&mount_list);
1625 } else {
1626 TAILQ_FOREACH(me, &mount_list, me_list)
1627 if (me->me_type == ME_MOUNT && me->me_mount == mp)
1628 break;
1629 if (me != NULL)
1630 me = TAILQ_NEXT(me, me_list);
1631 }
1632
1633 while (me != NULL && me->me_type != ME_MOUNT)
1634 me = TAILQ_NEXT(me, me_list);
1635
1636 return (me ? me->me_mount : NULL);
1637 }
1638