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