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