2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
39 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_subr.c,v 1.118 2008/09/17 21:44:18 dillon Exp $
44 * External virtual filesystem routines
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/dirent.h>
53 #include <sys/domain.h>
54 #include <sys/eventhandler.h>
55 #include <sys/fcntl.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
59 #include <sys/malloc.h>
61 #include <sys/mount.h>
64 #include <sys/reboot.h>
65 #include <sys/socket.h>
67 #include <sys/sysctl.h>
68 #include <sys/syslog.h>
69 #include <sys/unistd.h>
70 #include <sys/vmmeter.h>
71 #include <sys/vnode.h>
73 #include <machine/limits.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_pager.h>
83 #include <vm/vnode_pager.h>
84 #include <vm/vm_zone.h>
87 #include <sys/thread2.h>
88 #include <sys/sysref2.h>
89 #include <sys/mplock2.h>
91 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
94 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
96 SYSCTL_INT(_vfs, OID_AUTO, fastdev, CTLFLAG_RW, &vfs_fastdev, 0, "");
98 enum vtype iftovt_tab[16] = {
99 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
100 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
102 int vttoif_tab[9] = {
103 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
104 S_IFSOCK, S_IFIFO, S_IFMT,
107 static int reassignbufcalls;
108 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
109 &reassignbufcalls, 0, "");
110 static int reassignbufloops;
111 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
112 &reassignbufloops, 0, "");
113 static int reassignbufsortgood;
114 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
115 &reassignbufsortgood, 0, "");
116 static int reassignbufsortbad;
117 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
118 &reassignbufsortbad, 0, "");
119 static int reassignbufmethod = 1;
120 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
121 &reassignbufmethod, 0, "");
123 int nfs_mount_type = -1;
124 static struct lwkt_token spechash_token;
125 struct nfs_public nfs_pub; /* publicly exported FS */
128 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
129 &desiredvnodes, 0, "Maximum number of vnodes");
131 static void vfs_free_addrlist (struct netexport *nep);
132 static int vfs_free_netcred (struct radix_node *rn, void *w);
133 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
134 const struct export_args *argp);
137 * Red black tree functions
139 static int rb_buf_compare(struct buf *b1, struct buf *b2);
140 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
141 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
144 rb_buf_compare(struct buf *b1, struct buf *b2)
146 if (b1->b_loffset < b2->b_loffset)
148 if (b1->b_loffset > b2->b_loffset)
154 * Returns non-zero if the vnode is a candidate for lazy msyncing.
157 vshouldmsync(struct vnode *vp)
159 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
160 return (0); /* other holders */
162 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
169 * Initialize the vnode management data structures.
171 * Called from vfsinit()
177 * Desiredvnodes is kern.maxvnodes. We want to scale it
178 * according to available system memory but we may also have
179 * to limit it based on available KVM, which is capped on 32 bit
182 * WARNING! For machines with 64-256M of ram we have to be sure
183 * that the default limit scales down well due to HAMMER
184 * taking up significantly more memory per-vnode vs UFS.
185 * We want around ~5800 on a 128M machine.
187 desiredvnodes = min(maxproc + vmstats.v_page_count / 6,
189 (sizeof(struct vm_object) + sizeof(struct vnode))));
191 lwkt_token_init(&spechash_token);
195 * Knob to control the precision of file timestamps:
197 * 0 = seconds only; nanoseconds zeroed.
198 * 1 = seconds and nanoseconds, accurate within 1/HZ.
199 * 2 = seconds and nanoseconds, truncated to microseconds.
200 * >=3 = seconds and nanoseconds, maximum precision.
202 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
204 static int timestamp_precision = TSP_SEC;
205 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
206 ×tamp_precision, 0, "");
209 * Get a current timestamp.
214 vfs_timestamp(struct timespec *tsp)
218 switch (timestamp_precision) {
220 tsp->tv_sec = time_second;
228 TIMEVAL_TO_TIMESPEC(&tv, tsp);
238 * Set vnode attributes to VNOVAL
241 vattr_null(struct vattr *vap)
244 vap->va_size = VNOVAL;
245 vap->va_bytes = VNOVAL;
246 vap->va_mode = VNOVAL;
247 vap->va_nlink = VNOVAL;
248 vap->va_uid = VNOVAL;
249 vap->va_gid = VNOVAL;
250 vap->va_fsid = VNOVAL;
251 vap->va_fileid = VNOVAL;
252 vap->va_blocksize = VNOVAL;
253 vap->va_rmajor = VNOVAL;
254 vap->va_rminor = VNOVAL;
255 vap->va_atime.tv_sec = VNOVAL;
256 vap->va_atime.tv_nsec = VNOVAL;
257 vap->va_mtime.tv_sec = VNOVAL;
258 vap->va_mtime.tv_nsec = VNOVAL;
259 vap->va_ctime.tv_sec = VNOVAL;
260 vap->va_ctime.tv_nsec = VNOVAL;
261 vap->va_flags = VNOVAL;
262 vap->va_gen = VNOVAL;
264 /* va_*_uuid fields are only valid if related flags are set */
268 * Flush out and invalidate all buffers associated with a vnode.
272 static int vinvalbuf_bp(struct buf *bp, void *data);
274 struct vinvalbuf_bp_info {
282 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
284 struct vinvalbuf_bp_info info;
289 lwkt_gettoken(&vlock, &vp->v_token);
292 * If we are being asked to save, call fsync to ensure that the inode
295 if (flags & V_SAVE) {
296 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
299 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
300 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
304 * Dirty bufs may be left or generated via races
305 * in circumstances where vinvalbuf() is called on
306 * a vnode not undergoing reclamation. Only
307 * panic if we are trying to reclaim the vnode.
309 if ((vp->v_flag & VRECLAIMED) &&
310 (bio_track_active(&vp->v_track_write) ||
311 !RB_EMPTY(&vp->v_rbdirty_tree))) {
312 panic("vinvalbuf: dirty bufs");
316 info.slptimeo = slptimeo;
317 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
318 if (slpflag & PCATCH)
319 info.lkflags |= LK_PCATCH;
324 * Flush the buffer cache until nothing is left.
326 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
327 !RB_EMPTY(&vp->v_rbdirty_tree)) {
328 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
329 vinvalbuf_bp, &info);
331 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
332 vinvalbuf_bp, &info);
337 * Wait for I/O completion. We may block in the pip code so we have
341 bio_track_wait(&vp->v_track_write, 0, 0);
342 if ((object = vp->v_object) != NULL) {
343 while (object->paging_in_progress)
344 vm_object_pip_sleep(object, "vnvlbx");
346 } while (bio_track_active(&vp->v_track_write));
349 * Destroy the copy in the VM cache, too.
351 if ((object = vp->v_object) != NULL) {
352 vm_object_page_remove(object, 0, 0,
353 (flags & V_SAVE) ? TRUE : FALSE);
356 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
357 panic("vinvalbuf: flush failed");
358 if (!RB_EMPTY(&vp->v_rbhash_tree))
359 panic("vinvalbuf: flush failed, buffers still present");
362 lwkt_reltoken(&vlock);
367 vinvalbuf_bp(struct buf *bp, void *data)
369 struct vinvalbuf_bp_info *info = data;
372 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
373 error = BUF_TIMELOCK(bp, info->lkflags,
374 "vinvalbuf", info->slptimeo);
384 KKASSERT(bp->b_vp == info->vp);
387 * XXX Since there are no node locks for NFS, I
388 * believe there is a slight chance that a delayed
389 * write will occur while sleeping just above, so
390 * check for it. Note that vfs_bio_awrite expects
391 * buffers to reside on a queue, while bwrite() and
394 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
395 * check. This code will write out the buffer, period.
397 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
398 (info->flags & V_SAVE)) {
399 if (bp->b_vp == info->vp) {
400 if (bp->b_flags & B_CLUSTEROK) {
410 } else if (info->flags & V_SAVE) {
412 * Cannot set B_NOCACHE on a clean buffer as this will
413 * destroy the VM backing store which might actually
414 * be dirty (and unsynchronized).
417 bp->b_flags |= (B_INVAL | B_RELBUF);
421 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
428 * Truncate a file's buffer and pages to a specified length. This
429 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
432 * The vnode must be locked.
434 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
435 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
436 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
437 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
440 vtruncbuf(struct vnode *vp, off_t length, int blksize)
443 const char *filename;
448 * Round up to the *next* block, then destroy the buffers in question.
449 * Since we are only removing some of the buffers we must rely on the
450 * scan count to determine whether a loop is necessary.
452 if ((count = (int)(length % blksize)) != 0)
453 truncloffset = length + (blksize - count);
455 truncloffset = length;
457 lwkt_gettoken(&vlock, &vp->v_token);
459 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
460 vtruncbuf_bp_trunc_cmp,
461 vtruncbuf_bp_trunc, &truncloffset);
462 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
463 vtruncbuf_bp_trunc_cmp,
464 vtruncbuf_bp_trunc, &truncloffset);
468 * For safety, fsync any remaining metadata if the file is not being
469 * truncated to 0. Since the metadata does not represent the entire
470 * dirty list we have to rely on the hit count to ensure that we get
475 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
476 vtruncbuf_bp_metasync_cmp,
477 vtruncbuf_bp_metasync, vp);
482 * Clean out any left over VM backing store.
484 * It is possible to have in-progress I/O from buffers that were
485 * not part of the truncation. This should not happen if we
486 * are truncating to 0-length.
488 vnode_pager_setsize(vp, length);
489 bio_track_wait(&vp->v_track_write, 0, 0);
494 spin_lock_wr(&vp->v_spinlock);
495 filename = TAILQ_FIRST(&vp->v_namecache) ?
496 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
497 spin_unlock_wr(&vp->v_spinlock);
500 * Make sure no buffers were instantiated while we were trying
501 * to clean out the remaining VM pages. This could occur due
502 * to busy dirty VM pages being flushed out to disk.
505 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
506 vtruncbuf_bp_trunc_cmp,
507 vtruncbuf_bp_trunc, &truncloffset);
508 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
509 vtruncbuf_bp_trunc_cmp,
510 vtruncbuf_bp_trunc, &truncloffset);
512 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
513 "left over buffers in %s\n", count, filename);
517 lwkt_reltoken(&vlock);
523 * The callback buffer is beyond the new file EOF and must be destroyed.
524 * Note that the compare function must conform to the RB_SCAN's requirements.
528 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
530 if (bp->b_loffset >= *(off_t *)data)
537 vtruncbuf_bp_trunc(struct buf *bp, void *data)
540 * Do not try to use a buffer we cannot immediately lock, but sleep
541 * anyway to prevent a livelock. The code will loop until all buffers
544 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
545 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
549 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
556 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
557 * blocks (with a negative loffset) are scanned.
558 * Note that the compare function must conform to the RB_SCAN's requirements.
561 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
563 if (bp->b_loffset < 0)
569 vtruncbuf_bp_metasync(struct buf *bp, void *data)
571 struct vnode *vp = data;
573 if (bp->b_flags & B_DELWRI) {
575 * Do not try to use a buffer we cannot immediately lock,
576 * but sleep anyway to prevent a livelock. The code will
577 * loop until all buffers can be acted upon.
579 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
580 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
596 * vfsync - implements a multipass fsync on a file which understands
597 * dependancies and meta-data. The passed vnode must be locked. The
598 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
600 * When fsyncing data asynchronously just do one consolidated pass starting
601 * with the most negative block number. This may not get all the data due
604 * When fsyncing data synchronously do a data pass, then a metadata pass,
605 * then do additional data+metadata passes to try to get all the data out.
607 static int vfsync_wait_output(struct vnode *vp,
608 int (*waitoutput)(struct vnode *, struct thread *));
609 static int vfsync_data_only_cmp(struct buf *bp, void *data);
610 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
611 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
612 static int vfsync_bp(struct buf *bp, void *data);
621 int (*checkdef)(struct buf *);
625 vfsync(struct vnode *vp, int waitfor, int passes,
626 int (*checkdef)(struct buf *),
627 int (*waitoutput)(struct vnode *, struct thread *))
629 struct vfsync_info info;
633 bzero(&info, sizeof(info));
635 if ((info.checkdef = checkdef) == NULL)
638 lwkt_gettoken(&vlock, &vp->v_token);
643 * Lazy (filesystem syncer typ) Asynchronous plus limit the
644 * number of data (not meta) pages we try to flush to 1MB.
645 * A non-zero return means that lazy limit was reached.
647 info.lazylimit = 1024 * 1024;
649 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
650 vfsync_lazy_range_cmp, vfsync_bp, &info);
651 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
652 vfsync_meta_only_cmp, vfsync_bp, &info);
655 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
656 vn_syncer_add_to_worklist(vp, 1);
661 * Asynchronous. Do a data-only pass and a meta-only pass.
664 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
666 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
672 * Synchronous. Do a data-only pass, then a meta-data+data
673 * pass, then additional integrated passes to try to get
674 * all the dependancies flushed.
676 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
678 error = vfsync_wait_output(vp, waitoutput);
680 info.skippedbufs = 0;
681 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
683 error = vfsync_wait_output(vp, waitoutput);
684 if (info.skippedbufs)
685 kprintf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
687 while (error == 0 && passes > 0 &&
688 !RB_EMPTY(&vp->v_rbdirty_tree)
691 info.synchronous = 1;
694 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
700 error = vfsync_wait_output(vp, waitoutput);
704 lwkt_reltoken(&vlock);
709 vfsync_wait_output(struct vnode *vp,
710 int (*waitoutput)(struct vnode *, struct thread *))
714 error = bio_track_wait(&vp->v_track_write, 0, 0);
716 error = waitoutput(vp, curthread);
721 vfsync_data_only_cmp(struct buf *bp, void *data)
723 if (bp->b_loffset < 0)
729 vfsync_meta_only_cmp(struct buf *bp, void *data)
731 if (bp->b_loffset < 0)
737 vfsync_lazy_range_cmp(struct buf *bp, void *data)
739 struct vfsync_info *info = data;
740 if (bp->b_loffset < info->vp->v_lazyw)
746 vfsync_bp(struct buf *bp, void *data)
748 struct vfsync_info *info = data;
749 struct vnode *vp = info->vp;
753 * if syncdeps is not set we do not try to write buffers which have
756 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
760 * Ignore buffers that we cannot immediately lock. XXX
762 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
763 kprintf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
767 if ((bp->b_flags & B_DELWRI) == 0)
768 panic("vfsync_bp: buffer not dirty");
770 panic("vfsync_bp: buffer vp mismatch");
773 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
774 * has been written but an additional handshake with the device
775 * is required before we can dispose of the buffer. We have no idea
776 * how to do this so we have to skip these buffers.
778 if (bp->b_flags & B_NEEDCOMMIT) {
784 * Ask bioops if it is ok to sync
786 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
792 if (info->synchronous) {
794 * Synchronous flushing. An error may be returned.
800 * Asynchronous flushing. A negative return value simply
801 * stops the scan and is not considered an error. We use
802 * this to support limited MNT_LAZY flushes.
804 vp->v_lazyw = bp->b_loffset;
805 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
806 info->lazycount += vfs_bio_awrite(bp);
808 info->lazycount += bp->b_bufsize;
812 if (info->lazylimit && info->lazycount >= info->lazylimit)
821 * Associate a buffer with a vnode.
826 bgetvp(struct vnode *vp, struct buf *bp)
830 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
831 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
834 * Insert onto list for new vnode.
836 lwkt_gettoken(&vlock, &vp->v_token);
837 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
838 lwkt_reltoken(&vlock);
842 bp->b_flags |= B_HASHED;
843 bp->b_flags |= B_VNCLEAN;
844 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
845 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
847 lwkt_reltoken(&vlock);
852 * Disassociate a buffer from a vnode.
855 brelvp(struct buf *bp)
860 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
863 * Delete from old vnode list, if on one.
866 lwkt_gettoken(&vlock, &vp->v_token);
867 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
868 if (bp->b_flags & B_VNDIRTY)
869 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
871 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
872 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
874 if (bp->b_flags & B_HASHED) {
875 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
876 bp->b_flags &= ~B_HASHED;
878 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
879 vclrflags(vp, VONWORKLST);
880 LIST_REMOVE(vp, v_synclist);
883 lwkt_reltoken(&vlock);
889 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
890 * This routine is called when the state of the B_DELWRI bit is changed.
895 reassignbuf(struct buf *bp)
897 struct vnode *vp = bp->b_vp;
901 KKASSERT(vp != NULL);
905 * B_PAGING flagged buffers cannot be reassigned because their vp
906 * is not fully linked in.
908 if (bp->b_flags & B_PAGING)
909 panic("cannot reassign paging buffer");
911 lwkt_gettoken(&vlock, &vp->v_token);
912 if (bp->b_flags & B_DELWRI) {
914 * Move to the dirty list, add the vnode to the worklist
916 if (bp->b_flags & B_VNCLEAN) {
917 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
918 bp->b_flags &= ~B_VNCLEAN;
920 if ((bp->b_flags & B_VNDIRTY) == 0) {
921 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
922 panic("reassignbuf: dup lblk vp %p bp %p",
925 bp->b_flags |= B_VNDIRTY;
927 if ((vp->v_flag & VONWORKLST) == 0) {
928 switch (vp->v_type) {
935 vp->v_rdev->si_mountpoint != NULL) {
943 vn_syncer_add_to_worklist(vp, delay);
947 * Move to the clean list, remove the vnode from the worklist
948 * if no dirty blocks remain.
950 if (bp->b_flags & B_VNDIRTY) {
951 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
952 bp->b_flags &= ~B_VNDIRTY;
954 if ((bp->b_flags & B_VNCLEAN) == 0) {
955 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
956 panic("reassignbuf: dup lblk vp %p bp %p",
959 bp->b_flags |= B_VNCLEAN;
961 if ((vp->v_flag & VONWORKLST) &&
962 RB_EMPTY(&vp->v_rbdirty_tree)) {
963 vclrflags(vp, VONWORKLST);
964 LIST_REMOVE(vp, v_synclist);
967 lwkt_reltoken(&vlock);
971 * Create a vnode for a block device.
972 * Used for mounting the root file system.
974 extern struct vop_ops *devfs_vnode_dev_vops_p;
976 bdevvp(cdev_t dev, struct vnode **vpp)
986 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
997 v_associate_rdev(vp, dev);
998 vp->v_umajor = dev->si_umajor;
999 vp->v_uminor = dev->si_uminor;
1006 v_associate_rdev(struct vnode *vp, cdev_t dev)
1012 if (dev_is_good(dev) == 0)
1014 KKASSERT(vp->v_rdev == NULL);
1015 vp->v_rdev = reference_dev(dev);
1016 lwkt_gettoken(&ilock, &spechash_token);
1017 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1018 lwkt_reltoken(&ilock);
1023 v_release_rdev(struct vnode *vp)
1028 if ((dev = vp->v_rdev) != NULL) {
1029 lwkt_gettoken(&ilock, &spechash_token);
1030 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1033 lwkt_reltoken(&ilock);
1038 * Add a vnode to the alias list hung off the cdev_t. We only associate
1039 * the device number with the vnode. The actual device is not associated
1040 * until the vnode is opened (usually in spec_open()), and will be
1041 * disassociated on last close.
1044 addaliasu(struct vnode *nvp, int x, int y)
1046 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1047 panic("addaliasu on non-special vnode");
1053 * Simple call that a filesystem can make to try to get rid of a
1054 * vnode. It will fail if anyone is referencing the vnode (including
1057 * The filesystem can check whether its in-memory inode structure still
1058 * references the vp on return.
1061 vclean_unlocked(struct vnode *vp)
1064 if (sysref_isactive(&vp->v_sysref) == 0)
1070 * Disassociate a vnode from its underlying filesystem.
1072 * The vnode must be VX locked and referenced. In all normal situations
1073 * there are no active references. If vclean_vxlocked() is called while
1074 * there are active references, the vnode is being ripped out and we have
1075 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1078 vclean_vxlocked(struct vnode *vp, int flags)
1085 * If the vnode has already been reclaimed we have nothing to do.
1087 if (vp->v_flag & VRECLAIMED)
1089 vsetflags(vp, VRECLAIMED);
1092 * Scrap the vfs cache
1094 while (cache_inval_vp(vp, 0) != 0) {
1095 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1096 tsleep(vp, 0, "vclninv", 2);
1100 * Check to see if the vnode is in use. If so we have to reference it
1101 * before we clean it out so that its count cannot fall to zero and
1102 * generate a race against ourselves to recycle it.
1104 active = sysref_isactive(&vp->v_sysref);
1107 * Clean out any buffers associated with the vnode and destroy its
1108 * object, if it has one.
1110 vinvalbuf(vp, V_SAVE, 0, 0);
1113 * If purging an active vnode (typically during a forced unmount
1114 * or reboot), it must be closed and deactivated before being
1115 * reclaimed. This isn't really all that safe, but what can
1118 * Note that neither of these routines unlocks the vnode.
1120 if (active && (flags & DOCLOSE)) {
1121 while ((n = vp->v_opencount) != 0) {
1122 if (vp->v_writecount)
1123 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1125 VOP_CLOSE(vp, FNONBLOCK);
1126 if (vp->v_opencount == n) {
1127 kprintf("Warning: unable to force-close"
1135 * If the vnode has not been deactivated, deactivated it. Deactivation
1136 * can create new buffers and VM pages so we have to call vinvalbuf()
1137 * again to make sure they all get flushed.
1139 * This can occur if a file with a link count of 0 needs to be
1142 * If the vnode is already dead don't try to deactivate it.
1144 if ((vp->v_flag & VINACTIVE) == 0) {
1145 vsetflags(vp, VINACTIVE);
1148 vinvalbuf(vp, V_SAVE, 0, 0);
1152 * If the vnode has an object, destroy it.
1154 if ((object = vp->v_object) != NULL) {
1155 if (object->ref_count == 0) {
1156 if ((object->flags & OBJ_DEAD) == 0)
1157 vm_object_terminate(object);
1159 vm_pager_deallocate(object);
1161 vclrflags(vp, VOBJBUF);
1163 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1166 * Reclaim the vnode if not already dead.
1168 if (vp->v_mount && VOP_RECLAIM(vp))
1169 panic("vclean: cannot reclaim");
1172 * Done with purge, notify sleepers of the grim news.
1174 vp->v_ops = &dead_vnode_vops_p;
1179 * If we are destroying an active vnode, reactivate it now that
1180 * we have reassociated it with deadfs. This prevents the system
1181 * from crashing on the vnode due to it being unexpectedly marked
1182 * as inactive or reclaimed.
1184 if (active && (flags & DOCLOSE)) {
1185 vclrflags(vp, VINACTIVE | VRECLAIMED);
1190 * Eliminate all activity associated with the requested vnode
1191 * and with all vnodes aliased to the requested vnode.
1193 * The vnode must be referenced but should not be locked.
1196 vrevoke(struct vnode *vp, struct ucred *cred)
1205 * If the vnode has a device association, scrap all vnodes associated
1206 * with the device. Don't let the device disappear on us while we
1207 * are scrapping the vnodes.
1209 * The passed vp will probably show up in the list, do not VX lock
1212 * Releasing the vnode's rdev here can mess up specfs's call to
1213 * device close, so don't do it. The vnode has been disassociated
1214 * and the device will be closed after the last ref on the related
1215 * fp goes away (if not still open by e.g. the kernel).
1217 if (vp->v_type != VCHR) {
1218 error = fdrevoke(vp, DTYPE_VNODE, cred);
1221 if ((dev = vp->v_rdev) == NULL) {
1225 lwkt_gettoken(&ilock, &spechash_token);
1227 vqn = SLIST_FIRST(&dev->si_hlist);
1230 while ((vq = vqn) != NULL) {
1231 vqn = SLIST_NEXT(vqn, v_cdevnext);
1234 fdrevoke(vq, DTYPE_VNODE, cred);
1235 /*v_release_rdev(vq);*/
1238 lwkt_reltoken(&ilock);
1245 * This is called when the object underlying a vnode is being destroyed,
1246 * such as in a remove(). Try to recycle the vnode immediately if the
1247 * only active reference is our reference.
1249 * Directory vnodes in the namecache with children cannot be immediately
1250 * recycled because numerous VOP_N*() ops require them to be stable.
1253 vrecycle(struct vnode *vp)
1255 if (vp->v_sysref.refcnt <= 1) {
1256 if (cache_inval_vp_nonblock(vp))
1265 * Return the maximum I/O size allowed for strategy calls on VP.
1267 * If vp is VCHR or VBLK we dive the device, otherwise we use
1268 * the vp's mount info.
1271 vmaxiosize(struct vnode *vp)
1273 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1274 return(vp->v_rdev->si_iosize_max);
1276 return(vp->v_mount->mnt_iosize_max);
1281 * Eliminate all activity associated with a vnode in preparation for reuse.
1283 * The vnode must be VX locked and refd and will remain VX locked and refd
1284 * on return. This routine may be called with the vnode in any state, as
1285 * long as it is VX locked. The vnode will be cleaned out and marked
1286 * VRECLAIMED but will not actually be reused until all existing refs and
1289 * NOTE: This routine may be called on a vnode which has not yet been
1290 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1291 * already been reclaimed.
1293 * This routine is not responsible for placing us back on the freelist.
1294 * Instead, it happens automatically when the caller releases the VX lock
1295 * (assuming there aren't any other references).
1298 vgone_vxlocked(struct vnode *vp)
1301 * assert that the VX lock is held. This is an absolute requirement
1302 * now for vgone_vxlocked() to be called.
1304 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1309 * Clean out the filesystem specific data and set the VRECLAIMED
1310 * bit. Also deactivate the vnode if necessary.
1312 vclean_vxlocked(vp, DOCLOSE);
1315 * Delete from old mount point vnode list, if on one.
1317 if (vp->v_mount != NULL)
1318 insmntque(vp, NULL);
1321 * If special device, remove it from special device alias list
1322 * if it is on one. This should normally only occur if a vnode is
1323 * being revoked as the device should otherwise have been released
1326 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1338 * Lookup a vnode by device number.
1340 * Returns non-zero and *vpp set to a vref'd vnode on success.
1341 * Returns zero on failure.
1344 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1349 lwkt_gettoken(&ilock, &spechash_token);
1350 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1351 if (type == vp->v_type) {
1354 lwkt_reltoken(&ilock);
1358 lwkt_reltoken(&ilock);
1363 * Calculate the total number of references to a special device. This
1364 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1365 * an overloaded field. Since udev2dev can now return NULL, we have
1366 * to check for a NULL v_rdev.
1369 count_dev(cdev_t dev)
1375 if (SLIST_FIRST(&dev->si_hlist)) {
1376 lwkt_gettoken(&ilock, &spechash_token);
1377 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1378 count += vp->v_opencount;
1380 lwkt_reltoken(&ilock);
1386 vcount(struct vnode *vp)
1388 if (vp->v_rdev == NULL)
1390 return(count_dev(vp->v_rdev));
1394 * Initialize VMIO for a vnode. This routine MUST be called before a
1395 * VFS can issue buffer cache ops on a vnode. It is typically called
1396 * when a vnode is initialized from its inode.
1399 vinitvmio(struct vnode *vp, off_t filesize)
1405 if ((object = vp->v_object) == NULL) {
1406 object = vnode_pager_alloc(vp, filesize, 0, 0);
1408 * Dereference the reference we just created. This assumes
1409 * that the object is associated with the vp.
1411 object->ref_count--;
1414 if (object->flags & OBJ_DEAD) {
1416 vm_object_dead_sleep(object, "vodead");
1417 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1421 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1422 vsetflags(vp, VOBJBUF);
1428 * Print out a description of a vnode.
1430 static char *typename[] =
1431 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1434 vprint(char *label, struct vnode *vp)
1439 kprintf("%s: %p: ", label, (void *)vp);
1441 kprintf("%p: ", (void *)vp);
1442 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1443 typename[vp->v_type],
1444 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1446 if (vp->v_flag & VROOT)
1447 strcat(buf, "|VROOT");
1448 if (vp->v_flag & VPFSROOT)
1449 strcat(buf, "|VPFSROOT");
1450 if (vp->v_flag & VTEXT)
1451 strcat(buf, "|VTEXT");
1452 if (vp->v_flag & VSYSTEM)
1453 strcat(buf, "|VSYSTEM");
1454 if (vp->v_flag & VFREE)
1455 strcat(buf, "|VFREE");
1456 if (vp->v_flag & VOBJBUF)
1457 strcat(buf, "|VOBJBUF");
1459 kprintf(" flags (%s)", &buf[1]);
1460 if (vp->v_data == NULL) {
1469 * Do the usual access checking.
1470 * file_mode, uid and gid are from the vnode in question,
1471 * while acc_mode and cred are from the VOP_ACCESS parameter list
1474 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1475 mode_t acc_mode, struct ucred *cred)
1481 * Super-user always gets read/write access, but execute access depends
1482 * on at least one execute bit being set.
1484 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1485 if ((acc_mode & VEXEC) && type != VDIR &&
1486 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1493 /* Otherwise, check the owner. */
1494 if (cred->cr_uid == uid) {
1495 if (acc_mode & VEXEC)
1497 if (acc_mode & VREAD)
1499 if (acc_mode & VWRITE)
1501 return ((file_mode & mask) == mask ? 0 : EACCES);
1504 /* Otherwise, check the groups. */
1505 ismember = groupmember(gid, cred);
1506 if (cred->cr_svgid == gid || ismember) {
1507 if (acc_mode & VEXEC)
1509 if (acc_mode & VREAD)
1511 if (acc_mode & VWRITE)
1513 return ((file_mode & mask) == mask ? 0 : EACCES);
1516 /* Otherwise, check everyone else. */
1517 if (acc_mode & VEXEC)
1519 if (acc_mode & VREAD)
1521 if (acc_mode & VWRITE)
1523 return ((file_mode & mask) == mask ? 0 : EACCES);
1527 #include <ddb/ddb.h>
1529 static int db_show_locked_vnodes(struct mount *mp, void *data);
1532 * List all of the locked vnodes in the system.
1533 * Called when debugging the kernel.
1535 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1537 kprintf("Locked vnodes\n");
1538 mountlist_scan(db_show_locked_vnodes, NULL,
1539 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1543 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1547 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1548 if (vn_islocked(vp))
1556 * Top level filesystem related information gathering.
1558 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1561 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1563 int *name = (int *)arg1 - 1; /* XXX */
1564 u_int namelen = arg2 + 1; /* XXX */
1565 struct vfsconf *vfsp;
1568 #if 1 || defined(COMPAT_PRELITE2)
1569 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1571 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1575 /* all sysctl names at this level are at least name and field */
1577 return (ENOTDIR); /* overloaded */
1578 if (name[0] != VFS_GENERIC) {
1579 vfsp = vfsconf_find_by_typenum(name[0]);
1581 return (EOPNOTSUPP);
1582 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1583 oldp, oldlenp, newp, newlen, p));
1587 case VFS_MAXTYPENUM:
1590 maxtypenum = vfsconf_get_maxtypenum();
1591 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1594 return (ENOTDIR); /* overloaded */
1595 vfsp = vfsconf_find_by_typenum(name[2]);
1597 return (EOPNOTSUPP);
1598 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1600 return (EOPNOTSUPP);
1603 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1604 "Generic filesystem");
1606 #if 1 || defined(COMPAT_PRELITE2)
1609 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1612 struct ovfsconf ovfs;
1613 struct sysctl_req *req = (struct sysctl_req*) data;
1615 bzero(&ovfs, sizeof(ovfs));
1616 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1617 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1618 ovfs.vfc_index = vfsp->vfc_typenum;
1619 ovfs.vfc_refcount = vfsp->vfc_refcount;
1620 ovfs.vfc_flags = vfsp->vfc_flags;
1621 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1623 return error; /* abort iteration with error code */
1625 return 0; /* continue iterating with next element */
1629 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1631 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1634 #endif /* 1 || COMPAT_PRELITE2 */
1637 * Check to see if a filesystem is mounted on a block device.
1640 vfs_mountedon(struct vnode *vp)
1644 if ((dev = vp->v_rdev) == NULL) {
1645 /* if (vp->v_type != VBLK)
1646 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1648 if (dev != NULL && dev->si_mountpoint)
1654 * Unmount all filesystems. The list is traversed in reverse order
1655 * of mounting to avoid dependencies.
1658 static int vfs_umountall_callback(struct mount *mp, void *data);
1661 vfs_unmountall(void)
1666 count = mountlist_scan(vfs_umountall_callback,
1667 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1673 vfs_umountall_callback(struct mount *mp, void *data)
1677 error = dounmount(mp, MNT_FORCE);
1679 mountlist_remove(mp);
1680 kprintf("unmount of filesystem mounted from %s failed (",
1681 mp->mnt_stat.f_mntfromname);
1685 kprintf("%d)\n", error);
1691 * Checks the mount flags for parameter mp and put the names comma-separated
1692 * into a string buffer buf with a size limit specified by len.
1694 * It returns the number of bytes written into buf, and (*errorp) will be
1695 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1696 * not large enough). The buffer will be 0-terminated if len was not 0.
1699 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1700 char *buf, size_t len, int *errorp)
1702 static const struct mountctl_opt optnames[] = {
1703 { MNT_ASYNC, "asynchronous" },
1704 { MNT_EXPORTED, "NFS exported" },
1705 { MNT_LOCAL, "local" },
1706 { MNT_NOATIME, "noatime" },
1707 { MNT_NODEV, "nodev" },
1708 { MNT_NOEXEC, "noexec" },
1709 { MNT_NOSUID, "nosuid" },
1710 { MNT_NOSYMFOLLOW, "nosymfollow" },
1711 { MNT_QUOTA, "with-quotas" },
1712 { MNT_RDONLY, "read-only" },
1713 { MNT_SYNCHRONOUS, "synchronous" },
1714 { MNT_UNION, "union" },
1715 { MNT_NOCLUSTERR, "noclusterr" },
1716 { MNT_NOCLUSTERW, "noclusterw" },
1717 { MNT_SUIDDIR, "suiddir" },
1718 { MNT_SOFTDEP, "soft-updates" },
1719 { MNT_IGNORE, "ignore" },
1729 bleft = len - 1; /* leave room for trailing \0 */
1732 * Checks the size of the string. If it contains
1733 * any data, then we will append the new flags to
1736 actsize = strlen(buf);
1740 /* Default flags if no flags passed */
1744 if (bleft < 0) { /* degenerate case, 0-length buffer */
1749 for (; flags && optp->o_opt; ++optp) {
1750 if ((flags & optp->o_opt) == 0)
1752 optlen = strlen(optp->o_name);
1753 if (bwritten || actsize > 0) {
1758 buf[bwritten++] = ',';
1759 buf[bwritten++] = ' ';
1762 if (bleft < optlen) {
1766 bcopy(optp->o_name, buf + bwritten, optlen);
1769 flags &= ~optp->o_opt;
1773 * Space already reserved for trailing \0
1780 * Build hash lists of net addresses and hang them off the mount point.
1781 * Called by ufs_mount() to set up the lists of export addresses.
1784 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1785 const struct export_args *argp)
1788 struct radix_node_head *rnh;
1790 struct radix_node *rn;
1791 struct sockaddr *saddr, *smask = 0;
1795 if (argp->ex_addrlen == 0) {
1796 if (mp->mnt_flag & MNT_DEFEXPORTED)
1798 np = &nep->ne_defexported;
1799 np->netc_exflags = argp->ex_flags;
1800 np->netc_anon = argp->ex_anon;
1801 np->netc_anon.cr_ref = 1;
1802 mp->mnt_flag |= MNT_DEFEXPORTED;
1806 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1808 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1811 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1812 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1813 saddr = (struct sockaddr *) (np + 1);
1814 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1816 if (saddr->sa_len > argp->ex_addrlen)
1817 saddr->sa_len = argp->ex_addrlen;
1818 if (argp->ex_masklen) {
1819 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1820 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1823 if (smask->sa_len > argp->ex_masklen)
1824 smask->sa_len = argp->ex_masklen;
1826 i = saddr->sa_family;
1827 if ((rnh = nep->ne_rtable[i]) == 0) {
1829 * Seems silly to initialize every AF when most are not used,
1830 * do so on demand here
1832 SLIST_FOREACH(dom, &domains, dom_next)
1833 if (dom->dom_family == i && dom->dom_rtattach) {
1834 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1838 if ((rnh = nep->ne_rtable[i]) == 0) {
1843 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1845 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1849 np->netc_exflags = argp->ex_flags;
1850 np->netc_anon = argp->ex_anon;
1851 np->netc_anon.cr_ref = 1;
1854 kfree(np, M_NETADDR);
1860 vfs_free_netcred(struct radix_node *rn, void *w)
1862 struct radix_node_head *rnh = (struct radix_node_head *) w;
1864 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1865 kfree((caddr_t) rn, M_NETADDR);
1870 * Free the net address hash lists that are hanging off the mount points.
1873 vfs_free_addrlist(struct netexport *nep)
1876 struct radix_node_head *rnh;
1878 for (i = 0; i <= AF_MAX; i++)
1879 if ((rnh = nep->ne_rtable[i])) {
1880 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1882 kfree((caddr_t) rnh, M_RTABLE);
1883 nep->ne_rtable[i] = 0;
1888 vfs_export(struct mount *mp, struct netexport *nep,
1889 const struct export_args *argp)
1893 if (argp->ex_flags & MNT_DELEXPORT) {
1894 if (mp->mnt_flag & MNT_EXPUBLIC) {
1895 vfs_setpublicfs(NULL, NULL, NULL);
1896 mp->mnt_flag &= ~MNT_EXPUBLIC;
1898 vfs_free_addrlist(nep);
1899 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1901 if (argp->ex_flags & MNT_EXPORTED) {
1902 if (argp->ex_flags & MNT_EXPUBLIC) {
1903 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1905 mp->mnt_flag |= MNT_EXPUBLIC;
1907 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1909 mp->mnt_flag |= MNT_EXPORTED;
1916 * Set the publicly exported filesystem (WebNFS). Currently, only
1917 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1920 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1921 const struct export_args *argp)
1928 * mp == NULL -> invalidate the current info, the FS is
1929 * no longer exported. May be called from either vfs_export
1930 * or unmount, so check if it hasn't already been done.
1933 if (nfs_pub.np_valid) {
1934 nfs_pub.np_valid = 0;
1935 if (nfs_pub.np_index != NULL) {
1936 FREE(nfs_pub.np_index, M_TEMP);
1937 nfs_pub.np_index = NULL;
1944 * Only one allowed at a time.
1946 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1950 * Get real filehandle for root of exported FS.
1952 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1953 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1955 if ((error = VFS_ROOT(mp, &rvp)))
1958 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1964 * If an indexfile was specified, pull it in.
1966 if (argp->ex_indexfile != NULL) {
1969 error = vn_get_namelen(rvp, &namelen);
1972 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1974 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1978 * Check for illegal filenames.
1980 for (cp = nfs_pub.np_index; *cp; cp++) {
1988 FREE(nfs_pub.np_index, M_TEMP);
1993 nfs_pub.np_mount = mp;
1994 nfs_pub.np_valid = 1;
1999 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2000 struct sockaddr *nam)
2003 struct radix_node_head *rnh;
2004 struct sockaddr *saddr;
2007 if (mp->mnt_flag & MNT_EXPORTED) {
2009 * Lookup in the export list first.
2013 rnh = nep->ne_rtable[saddr->sa_family];
2015 np = (struct netcred *)
2016 (*rnh->rnh_matchaddr)((char *)saddr,
2018 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2023 * If no address match, use the default if it exists.
2025 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2026 np = &nep->ne_defexported;
2032 * perform msync on all vnodes under a mount point. The mount point must
2033 * be locked. This code is also responsible for lazy-freeing unreferenced
2034 * vnodes whos VM objects no longer contain pages.
2036 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2038 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2039 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2040 * way up in this high level function.
2042 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2043 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2046 vfs_msync(struct mount *mp, int flags)
2050 vmsc_flags = VMSC_GETVP;
2051 if (flags != MNT_WAIT)
2052 vmsc_flags |= VMSC_NOWAIT;
2053 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
2054 (void *)(intptr_t)flags);
2058 * scan1 is a fast pre-check. There could be hundreds of thousands of
2059 * vnodes, we cannot afford to do anything heavy weight until we have a
2060 * fairly good indication that there is work to do.
2064 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2066 int flags = (int)(intptr_t)data;
2068 if ((vp->v_flag & VRECLAIMED) == 0) {
2069 if (vshouldmsync(vp))
2070 return(0); /* call scan2 */
2071 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2072 (vp->v_flag & VOBJDIRTY) &&
2073 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2074 return(0); /* call scan2 */
2079 * do not call scan2, continue the loop
2085 * This callback is handed a locked vnode.
2089 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2092 int flags = (int)(intptr_t)data;
2094 if (vp->v_flag & VRECLAIMED)
2097 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2098 if ((obj = vp->v_object) != NULL) {
2099 vm_object_page_clean(obj, 0, 0,
2100 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2107 * Record a process's interest in events which might happen to
2108 * a vnode. Because poll uses the historic select-style interface
2109 * internally, this routine serves as both the ``check for any
2110 * pending events'' and the ``record my interest in future events''
2111 * functions. (These are done together, while the lock is held,
2112 * to avoid race conditions.)
2115 vn_pollrecord(struct vnode *vp, int events)
2119 KKASSERT(curthread->td_proc != NULL);
2121 lwkt_gettoken(&vlock, &vp->v_token);
2122 if (vp->v_pollinfo.vpi_revents & events) {
2124 * This leaves events we are not interested
2125 * in available for the other process which
2126 * which presumably had requested them
2127 * (otherwise they would never have been
2130 events &= vp->v_pollinfo.vpi_revents;
2131 vp->v_pollinfo.vpi_revents &= ~events;
2133 lwkt_reltoken(&vlock);
2136 vp->v_pollinfo.vpi_events |= events;
2137 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
2138 lwkt_reltoken(&vlock);
2143 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2144 * it is possible for us to miss an event due to race conditions, but
2145 * that condition is expected to be rare, so for the moment it is the
2146 * preferred interface.
2149 vn_pollevent(struct vnode *vp, int events)
2153 lwkt_gettoken(&vlock, &vp->v_token);
2154 if (vp->v_pollinfo.vpi_events & events) {
2156 * We clear vpi_events so that we don't
2157 * call selwakeup() twice if two events are
2158 * posted before the polling process(es) is
2159 * awakened. This also ensures that we take at
2160 * most one selwakeup() if the polling process
2161 * is no longer interested. However, it does
2162 * mean that only one event can be noticed at
2163 * a time. (Perhaps we should only clear those
2164 * event bits which we note?) XXX
2166 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2167 vp->v_pollinfo.vpi_revents |= events;
2168 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2170 lwkt_reltoken(&vlock);
2174 * Wake up anyone polling on vp because it is being revoked.
2175 * This depends on dead_poll() returning POLLHUP for correct
2179 vn_pollgone(struct vnode *vp)
2183 lwkt_gettoken(&vlock, &vp->v_token);
2184 if (vp->v_pollinfo.vpi_events) {
2185 vp->v_pollinfo.vpi_events = 0;
2186 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2188 lwkt_reltoken(&vlock);
2192 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2193 * (or v_rdev might be NULL).
2196 vn_todev(struct vnode *vp)
2198 if (vp->v_type != VBLK && vp->v_type != VCHR)
2200 KKASSERT(vp->v_rdev != NULL);
2201 return (vp->v_rdev);
2205 * Check if vnode represents a disk device. The vnode does not need to be
2211 vn_isdisk(struct vnode *vp, int *errp)
2215 if (vp->v_type != VCHR) {
2228 if (dev_is_good(dev) == 0) {
2233 if ((dev_dflags(dev) & D_DISK) == 0) {
2244 vn_get_namelen(struct vnode *vp, int *namelen)
2247 register_t retval[2];
2249 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2252 *namelen = (int)retval[0];
2257 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2258 uint16_t d_namlen, const char *d_name)
2263 len = _DIRENT_RECLEN(d_namlen);
2264 if (len > uio->uio_resid)
2267 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2270 dp->d_namlen = d_namlen;
2271 dp->d_type = d_type;
2272 bcopy(d_name, dp->d_name, d_namlen);
2274 *error = uiomove((caddr_t)dp, len, uio);
2282 vn_mark_atime(struct vnode *vp, struct thread *td)
2284 struct proc *p = td->td_proc;
2285 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2287 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2288 VOP_MARKATIME(vp, cred);