Get rid of the weird FSMID update path in the vnode and namecache code.
[dragonfly.git] / sys / kern / vfs_subr.c
... / ...
CommitLineData
1/*
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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
25 *
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
36 * SUCH DAMAGE.
37 *
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.78 2006/04/25 22:11:28 dillon Exp $
41 */
42
43/*
44 * External virtual filesystem routines
45 */
46#include "opt_ddb.h"
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/buf.h>
51#include <sys/conf.h>
52#include <sys/dirent.h>
53#include <sys/domain.h>
54#include <sys/eventhandler.h>
55#include <sys/fcntl.h>
56#include <sys/kernel.h>
57#include <sys/kthread.h>
58#include <sys/malloc.h>
59#include <sys/mbuf.h>
60#include <sys/mount.h>
61#include <sys/proc.h>
62#include <sys/reboot.h>
63#include <sys/socket.h>
64#include <sys/stat.h>
65#include <sys/sysctl.h>
66#include <sys/syslog.h>
67#include <sys/unistd.h>
68#include <sys/vmmeter.h>
69#include <sys/vnode.h>
70
71#include <machine/limits.h>
72
73#include <vm/vm.h>
74#include <vm/vm_object.h>
75#include <vm/vm_extern.h>
76#include <vm/vm_kern.h>
77#include <vm/pmap.h>
78#include <vm/vm_map.h>
79#include <vm/vm_page.h>
80#include <vm/vm_pager.h>
81#include <vm/vnode_pager.h>
82#include <vm/vm_zone.h>
83
84#include <sys/buf2.h>
85#include <sys/thread2.h>
86
87static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
88
89int numvnodes;
90SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
91int vfs_fastdev = 1;
92SYSCTL_INT(_vfs, OID_AUTO, fastdev, CTLFLAG_RW, &vfs_fastdev, 0, "");
93
94enum vtype iftovt_tab[16] = {
95 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
96 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
97};
98int vttoif_tab[9] = {
99 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
100 S_IFSOCK, S_IFIFO, S_IFMT,
101};
102
103static int reassignbufcalls;
104SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
105 &reassignbufcalls, 0, "");
106static int reassignbufloops;
107SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
108 &reassignbufloops, 0, "");
109static int reassignbufsortgood;
110SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
111 &reassignbufsortgood, 0, "");
112static int reassignbufsortbad;
113SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
114 &reassignbufsortbad, 0, "");
115static int reassignbufmethod = 1;
116SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
117 &reassignbufmethod, 0, "");
118
119int nfs_mount_type = -1;
120static struct lwkt_token spechash_token;
121struct nfs_public nfs_pub; /* publicly exported FS */
122
123int desiredvnodes;
124SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
125 &desiredvnodes, 0, "Maximum number of vnodes");
126
127static void vfs_free_addrlist (struct netexport *nep);
128static int vfs_free_netcred (struct radix_node *rn, void *w);
129static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
130 struct export_args *argp);
131
132extern int dev_ref_debug;
133extern struct vnodeopv_entry_desc spec_vnodeop_entries[];
134
135/*
136 * Red black tree functions
137 */
138static int rb_buf_compare(struct buf *b1, struct buf *b2);
139RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
140RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
141
142static int
143rb_buf_compare(struct buf *b1, struct buf *b2)
144{
145 if (b1->b_loffset < b2->b_loffset)
146 return(-1);
147 if (b1->b_loffset > b2->b_loffset)
148 return(1);
149 return(0);
150}
151
152/*
153 * Return 0 if the vnode is already on the free list or cannot be placed
154 * on the free list. Return 1 if the vnode can be placed on the free list.
155 */
156static __inline int
157vshouldfree(struct vnode *vp, int usecount)
158{
159 if (vp->v_flag & VFREE)
160 return (0); /* already free */
161 if (vp->v_holdcnt != 0 || vp->v_usecount != usecount)
162 return (0); /* other holderse */
163 if (vp->v_object &&
164 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
165 return (0);
166 }
167 return (1);
168}
169
170/*
171 * Initialize the vnode management data structures.
172 *
173 * Called from vfsinit()
174 */
175void
176vfs_subr_init(void)
177{
178 /*
179 * Desired vnodes is a result of the physical page count
180 * and the size of kernel's heap. It scales in proportion
181 * to the amount of available physical memory. This can
182 * cause trouble on 64-bit and large memory platforms.
183 */
184 /* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
185 desiredvnodes =
186 min(maxproc + vmstats.v_page_count /4,
187 2 * (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
188 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
189
190 lwkt_token_init(&spechash_token);
191}
192
193/*
194 * Knob to control the precision of file timestamps:
195 *
196 * 0 = seconds only; nanoseconds zeroed.
197 * 1 = seconds and nanoseconds, accurate within 1/HZ.
198 * 2 = seconds and nanoseconds, truncated to microseconds.
199 * >=3 = seconds and nanoseconds, maximum precision.
200 */
201enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
202
203static int timestamp_precision = TSP_SEC;
204SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
205 &timestamp_precision, 0, "");
206
207/*
208 * Get a current timestamp.
209 */
210void
211vfs_timestamp(struct timespec *tsp)
212{
213 struct timeval tv;
214
215 switch (timestamp_precision) {
216 case TSP_SEC:
217 tsp->tv_sec = time_second;
218 tsp->tv_nsec = 0;
219 break;
220 case TSP_HZ:
221 getnanotime(tsp);
222 break;
223 case TSP_USEC:
224 microtime(&tv);
225 TIMEVAL_TO_TIMESPEC(&tv, tsp);
226 break;
227 case TSP_NSEC:
228 default:
229 nanotime(tsp);
230 break;
231 }
232}
233
234/*
235 * Set vnode attributes to VNOVAL
236 */
237void
238vattr_null(struct vattr *vap)
239{
240 vap->va_type = VNON;
241 vap->va_size = VNOVAL;
242 vap->va_bytes = VNOVAL;
243 vap->va_mode = VNOVAL;
244 vap->va_nlink = VNOVAL;
245 vap->va_uid = VNOVAL;
246 vap->va_gid = VNOVAL;
247 vap->va_fsid = VNOVAL;
248 vap->va_fileid = VNOVAL;
249 vap->va_blocksize = VNOVAL;
250 vap->va_rdev = VNOVAL;
251 vap->va_atime.tv_sec = VNOVAL;
252 vap->va_atime.tv_nsec = VNOVAL;
253 vap->va_mtime.tv_sec = VNOVAL;
254 vap->va_mtime.tv_nsec = VNOVAL;
255 vap->va_ctime.tv_sec = VNOVAL;
256 vap->va_ctime.tv_nsec = VNOVAL;
257 vap->va_flags = VNOVAL;
258 vap->va_gen = VNOVAL;
259 vap->va_vaflags = 0;
260 vap->va_fsmid = VNOVAL;
261}
262
263/*
264 * Flush out and invalidate all buffers associated with a vnode.
265 *
266 * vp must be locked.
267 */
268static int vinvalbuf_bp(struct buf *bp, void *data);
269
270struct vinvalbuf_bp_info {
271 struct vnode *vp;
272 int slptimeo;
273 int lkflags;
274 int flags;
275};
276
277void
278vupdatefsmid(struct vnode *vp)
279{
280 atomic_set_int(&vp->v_flag, VFSMID);
281}
282
283int
284vinvalbuf(struct vnode *vp, int flags, struct thread *td,
285 int slpflag, int slptimeo)
286{
287 struct vinvalbuf_bp_info info;
288 int error;
289 vm_object_t object;
290
291 /*
292 * If we are being asked to save, call fsync to ensure that the inode
293 * is updated.
294 */
295 if (flags & V_SAVE) {
296 crit_enter();
297 while (vp->v_track_write.bk_active) {
298 vp->v_track_write.bk_waitflag = 1;
299 error = tsleep(&vp->v_track_write, slpflag,
300 "vinvlbuf", slptimeo);
301 if (error) {
302 crit_exit();
303 return (error);
304 }
305 }
306 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
307 crit_exit();
308 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0)
309 return (error);
310 crit_enter();
311 if (vp->v_track_write.bk_active > 0 ||
312 !RB_EMPTY(&vp->v_rbdirty_tree))
313 panic("vinvalbuf: dirty bufs");
314 }
315 crit_exit();
316 }
317 crit_enter();
318 info.slptimeo = slptimeo;
319 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
320 if (slpflag & PCATCH)
321 info.lkflags |= LK_PCATCH;
322 info.flags = flags;
323 info.vp = vp;
324
325 /*
326 * Flush the buffer cache until nothing is left.
327 */
328 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
329 !RB_EMPTY(&vp->v_rbdirty_tree)) {
330 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
331 vinvalbuf_bp, &info);
332 if (error == 0) {
333 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
334 vinvalbuf_bp, &info);
335 }
336 }
337
338 /*
339 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
340 * have write I/O in-progress but if there is a VM object then the
341 * VM object can also have read-I/O in-progress.
342 */
343 do {
344 while (vp->v_track_write.bk_active > 0) {
345 vp->v_track_write.bk_waitflag = 1;
346 tsleep(&vp->v_track_write, 0, "vnvlbv", 0);
347 }
348 if ((object = vp->v_object) != NULL) {
349 while (object->paging_in_progress)
350 vm_object_pip_sleep(object, "vnvlbx");
351 }
352 } while (vp->v_track_write.bk_active > 0);
353
354 crit_exit();
355
356 /*
357 * Destroy the copy in the VM cache, too.
358 */
359 if ((object = vp->v_object) != NULL) {
360 vm_object_page_remove(object, 0, 0,
361 (flags & V_SAVE) ? TRUE : FALSE);
362 }
363
364 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
365 panic("vinvalbuf: flush failed");
366 if (!RB_EMPTY(&vp->v_rbhash_tree))
367 panic("vinvalbuf: flush failed, buffers still present");
368 return (0);
369}
370
371static int
372vinvalbuf_bp(struct buf *bp, void *data)
373{
374 struct vinvalbuf_bp_info *info = data;
375 int error;
376
377 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
378 error = BUF_TIMELOCK(bp, info->lkflags,
379 "vinvalbuf", info->slptimeo);
380 if (error == 0) {
381 BUF_UNLOCK(bp);
382 error = ENOLCK;
383 }
384 if (error == ENOLCK)
385 return(0);
386 return (-error);
387 }
388
389 KKASSERT(bp->b_vp == info->vp);
390
391 /*
392 * XXX Since there are no node locks for NFS, I
393 * believe there is a slight chance that a delayed
394 * write will occur while sleeping just above, so
395 * check for it. Note that vfs_bio_awrite expects
396 * buffers to reside on a queue, while VOP_BWRITE and
397 * brelse do not.
398 */
399 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
400 (info->flags & V_SAVE)) {
401 if (bp->b_vp == info->vp) {
402 if (bp->b_flags & B_CLUSTEROK) {
403 vfs_bio_awrite(bp);
404 } else {
405 bremfree(bp);
406 bp->b_flags |= B_ASYNC;
407 VOP_BWRITE(bp->b_vp, bp);
408 }
409 } else {
410 bremfree(bp);
411 VOP_BWRITE(bp->b_vp, bp);
412 }
413 } else {
414 bremfree(bp);
415 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
416 bp->b_flags &= ~B_ASYNC;
417 brelse(bp);
418 }
419 return(0);
420}
421
422/*
423 * Truncate a file's buffer and pages to a specified length. This
424 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
425 * sync activity.
426 *
427 * The vnode must be locked.
428 */
429static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
430static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
431static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
432static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
433
434int
435vtruncbuf(struct vnode *vp, struct thread *td, off_t length, int blksize)
436{
437 off_t truncloffset;
438 int count;
439
440 /*
441 * Round up to the *next* block, then destroy the buffers in question.
442 * Since we are only removing some of the buffers we must rely on the
443 * scan count to determine whether a loop is necessary.
444 */
445 if ((count = (int)(length % blksize)) != 0)
446 truncloffset = length + (blksize - count);
447 else
448 truncloffset = length;
449
450 crit_enter();
451 do {
452 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
453 vtruncbuf_bp_trunc_cmp,
454 vtruncbuf_bp_trunc, &truncloffset);
455 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
456 vtruncbuf_bp_trunc_cmp,
457 vtruncbuf_bp_trunc, &truncloffset);
458 } while(count);
459
460 /*
461 * For safety, fsync any remaining metadata if the file is not being
462 * truncated to 0. Since the metadata does not represent the entire
463 * dirty list we have to rely on the hit count to ensure that we get
464 * all of it.
465 */
466 if (length > 0) {
467 do {
468 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
469 vtruncbuf_bp_metasync_cmp,
470 vtruncbuf_bp_metasync, vp);
471 } while (count);
472 }
473
474 /*
475 * Wait for any in-progress I/O to complete before returning (why?)
476 */
477 while (vp->v_track_write.bk_active > 0) {
478 vp->v_track_write.bk_waitflag = 1;
479 tsleep(&vp->v_track_write, 0, "vbtrunc", 0);
480 }
481
482 crit_exit();
483
484 vnode_pager_setsize(vp, length);
485
486 return (0);
487}
488
489/*
490 * The callback buffer is beyond the new file EOF and must be destroyed.
491 * Note that the compare function must conform to the RB_SCAN's requirements.
492 */
493static
494int
495vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
496{
497 if (bp->b_loffset >= *(off_t *)data)
498 return(0);
499 return(-1);
500}
501
502static
503int
504vtruncbuf_bp_trunc(struct buf *bp, void *data)
505{
506 /*
507 * Do not try to use a buffer we cannot immediately lock, but sleep
508 * anyway to prevent a livelock. The code will loop until all buffers
509 * can be acted upon.
510 */
511 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
512 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
513 BUF_UNLOCK(bp);
514 } else {
515 bremfree(bp);
516 bp->b_flags |= (B_INVAL | B_RELBUF);
517 bp->b_flags &= ~B_ASYNC;
518 brelse(bp);
519 }
520 return(1);
521}
522
523/*
524 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
525 * blocks (with a negative loffset) are scanned.
526 * Note that the compare function must conform to the RB_SCAN's requirements.
527 */
528static int
529vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
530{
531 if (bp->b_loffset < 0)
532 return(0);
533 return(1);
534}
535
536static int
537vtruncbuf_bp_metasync(struct buf *bp, void *data)
538{
539 struct vnode *vp = data;
540
541 if (bp->b_flags & B_DELWRI) {
542 /*
543 * Do not try to use a buffer we cannot immediately lock,
544 * but sleep anyway to prevent a livelock. The code will
545 * loop until all buffers can be acted upon.
546 */
547 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
548 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
549 BUF_UNLOCK(bp);
550 } else {
551 bremfree(bp);
552 if (bp->b_vp == vp) {
553 bp->b_flags |= B_ASYNC;
554 } else {
555 bp->b_flags &= ~B_ASYNC;
556 }
557 VOP_BWRITE(bp->b_vp, bp);
558 }
559 return(1);
560 } else {
561 return(0);
562 }
563}
564
565/*
566 * vfsync - implements a multipass fsync on a file which understands
567 * dependancies and meta-data. The passed vnode must be locked. The
568 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
569 *
570 * When fsyncing data asynchronously just do one consolidated pass starting
571 * with the most negative block number. This may not get all the data due
572 * to dependancies.
573 *
574 * When fsyncing data synchronously do a data pass, then a metadata pass,
575 * then do additional data+metadata passes to try to get all the data out.
576 */
577static int vfsync_wait_output(struct vnode *vp,
578 int (*waitoutput)(struct vnode *, struct thread *));
579static int vfsync_data_only_cmp(struct buf *bp, void *data);
580static int vfsync_meta_only_cmp(struct buf *bp, void *data);
581static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
582static int vfsync_bp(struct buf *bp, void *data);
583
584struct vfsync_info {
585 struct vnode *vp;
586 int synchronous;
587 int syncdeps;
588 int lazycount;
589 int lazylimit;
590 int skippedbufs;
591 int (*checkdef)(struct buf *);
592};
593
594int
595vfsync(struct vnode *vp, int waitfor, int passes,
596 int (*checkdef)(struct buf *),
597 int (*waitoutput)(struct vnode *, struct thread *))
598{
599 struct vfsync_info info;
600 int error;
601
602 bzero(&info, sizeof(info));
603 info.vp = vp;
604 if ((info.checkdef = checkdef) == NULL)
605 info.syncdeps = 1;
606
607 crit_enter();
608
609 switch(waitfor) {
610 case MNT_LAZY:
611 /*
612 * Lazy (filesystem syncer typ) Asynchronous plus limit the
613 * number of data (not meta) pages we try to flush to 1MB.
614 * A non-zero return means that lazy limit was reached.
615 */
616 info.lazylimit = 1024 * 1024;
617 info.syncdeps = 1;
618 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
619 vfsync_lazy_range_cmp, vfsync_bp, &info);
620 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
621 vfsync_meta_only_cmp, vfsync_bp, &info);
622 if (error == 0)
623 vp->v_lazyw = 0;
624 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
625 vn_syncer_add_to_worklist(vp, 1);
626 error = 0;
627 break;
628 case MNT_NOWAIT:
629 /*
630 * Asynchronous. Do a data-only pass and a meta-only pass.
631 */
632 info.syncdeps = 1;
633 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
634 vfsync_bp, &info);
635 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
636 vfsync_bp, &info);
637 error = 0;
638 break;
639 default:
640 /*
641 * Synchronous. Do a data-only pass, then a meta-data+data
642 * pass, then additional integrated passes to try to get
643 * all the dependancies flushed.
644 */
645 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
646 vfsync_bp, &info);
647 error = vfsync_wait_output(vp, waitoutput);
648 if (error == 0) {
649 info.skippedbufs = 0;
650 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
651 vfsync_bp, &info);
652 error = vfsync_wait_output(vp, waitoutput);
653 if (info.skippedbufs)
654 printf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
655 }
656 while (error == 0 && passes > 0 &&
657 !RB_EMPTY(&vp->v_rbdirty_tree)) {
658 if (--passes == 0) {
659 info.synchronous = 1;
660 info.syncdeps = 1;
661 }
662 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
663 vfsync_bp, &info);
664 if (error < 0)
665 error = -error;
666 info.syncdeps = 1;
667 if (error == 0)
668 error = vfsync_wait_output(vp, waitoutput);
669 }
670 break;
671 }
672 crit_exit();
673 return(error);
674}
675
676static int
677vfsync_wait_output(struct vnode *vp, int (*waitoutput)(struct vnode *, struct thread *))
678{
679 int error = 0;
680
681 while (vp->v_track_write.bk_active) {
682 vp->v_track_write.bk_waitflag = 1;
683 tsleep(&vp->v_track_write, 0, "fsfsn", 0);
684 }
685 if (waitoutput)
686 error = waitoutput(vp, curthread);
687 return(error);
688}
689
690static int
691vfsync_data_only_cmp(struct buf *bp, void *data)
692{
693 if (bp->b_loffset < 0)
694 return(-1);
695 return(0);
696}
697
698static int
699vfsync_meta_only_cmp(struct buf *bp, void *data)
700{
701 if (bp->b_loffset < 0)
702 return(0);
703 return(1);
704}
705
706static int
707vfsync_lazy_range_cmp(struct buf *bp, void *data)
708{
709 struct vfsync_info *info = data;
710 if (bp->b_loffset < info->vp->v_lazyw)
711 return(-1);
712 return(0);
713}
714
715static int
716vfsync_bp(struct buf *bp, void *data)
717{
718 struct vfsync_info *info = data;
719 struct vnode *vp = info->vp;
720 int error;
721
722 /*
723 * if syncdeps is not set we do not try to write buffers which have
724 * dependancies.
725 */
726 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
727 return(0);
728
729 /*
730 * Ignore buffers that we cannot immediately lock. XXX
731 */
732 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
733 printf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
734 ++info->skippedbufs;
735 return(0);
736 }
737 if ((bp->b_flags & B_DELWRI) == 0)
738 panic("vfsync_bp: buffer not dirty");
739 if (vp != bp->b_vp)
740 panic("vfsync_bp: buffer vp mismatch");
741
742 /*
743 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
744 * has been written but an additional handshake with the device
745 * is required before we can dispose of the buffer. We have no idea
746 * how to do this so we have to skip these buffers.
747 */
748 if (bp->b_flags & B_NEEDCOMMIT) {
749 BUF_UNLOCK(bp);
750 return(0);
751 }
752
753 if (info->synchronous) {
754 /*
755 * Synchronous flushing. An error may be returned.
756 */
757 bremfree(bp);
758 crit_exit();
759 error = bwrite(bp);
760 crit_enter();
761 } else {
762 /*
763 * Asynchronous flushing. A negative return value simply
764 * stops the scan and is not considered an error. We use
765 * this to support limited MNT_LAZY flushes.
766 */
767 vp->v_lazyw = bp->b_loffset;
768 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
769 info->lazycount += vfs_bio_awrite(bp);
770 } else {
771 info->lazycount += bp->b_bufsize;
772 bremfree(bp);
773 crit_exit();
774 bawrite(bp);
775 crit_enter();
776 }
777 if (info->lazylimit && info->lazycount >= info->lazylimit)
778 error = 1;
779 else
780 error = 0;
781 }
782 return(-error);
783}
784
785/*
786 * Associate a buffer with a vnode.
787 */
788void
789bgetvp(struct vnode *vp, struct buf *bp)
790{
791 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
792 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI)) == 0);
793 KKASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0);
794
795 vhold(vp);
796 /*
797 * Insert onto list for new vnode.
798 */
799 crit_enter();
800 bp->b_vp = vp;
801 bp->b_flags |= B_HASHED;
802 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp))
803 panic("reassignbuf: dup lblk vp %p bp %p", vp, bp);
804
805 bp->b_xflags |= BX_VNCLEAN;
806 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
807 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
808 crit_exit();
809}
810
811/*
812 * Disassociate a buffer from a vnode.
813 */
814void
815brelvp(struct buf *bp)
816{
817 struct vnode *vp;
818
819 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
820
821 /*
822 * Delete from old vnode list, if on one.
823 */
824 vp = bp->b_vp;
825 crit_enter();
826 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
827 if (bp->b_xflags & BX_VNDIRTY)
828 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
829 else
830 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
831 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
832 }
833 if (bp->b_flags & B_HASHED) {
834 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
835 bp->b_flags &= ~B_HASHED;
836 }
837 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
838 vp->v_flag &= ~VONWORKLST;
839 LIST_REMOVE(vp, v_synclist);
840 }
841 crit_exit();
842 bp->b_vp = NULL;
843 vdrop(vp);
844}
845
846/*
847 * Associate a p-buffer with a vnode.
848 *
849 * Also sets B_PAGING flag to indicate that vnode is not fully associated
850 * with the buffer. i.e. the bp has not been linked into the vnode or
851 * ref-counted.
852 */
853void
854pbgetvp(struct vnode *vp, struct buf *bp)
855{
856 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
857 KKASSERT((bp->b_flags & B_HASHED) == 0);
858
859 bp->b_vp = vp;
860 bp->b_flags |= B_PAGING;
861}
862
863/*
864 * Disassociate a p-buffer from a vnode.
865 */
866void
867pbrelvp(struct buf *bp)
868{
869 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
870 KKASSERT((bp->b_flags & B_HASHED) == 0);
871
872 bp->b_vp = NULL;
873 bp->b_flags &= ~B_PAGING;
874}
875
876/*
877 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
878 * This routine is called when the state of the B_DELWRI bit is changed.
879 */
880void
881reassignbuf(struct buf *bp)
882{
883 struct vnode *vp = bp->b_vp;
884 int delay;
885
886 KKASSERT(vp != NULL);
887 ++reassignbufcalls;
888
889 /*
890 * B_PAGING flagged buffers cannot be reassigned because their vp
891 * is not fully linked in.
892 */
893 if (bp->b_flags & B_PAGING)
894 panic("cannot reassign paging buffer");
895
896 crit_enter();
897 if (bp->b_flags & B_DELWRI) {
898 /*
899 * Move to the dirty list, add the vnode to the worklist
900 */
901 if (bp->b_xflags & BX_VNCLEAN) {
902 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
903 bp->b_xflags &= ~BX_VNCLEAN;
904 }
905 if ((bp->b_xflags & BX_VNDIRTY) == 0) {
906 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
907 panic("reassignbuf: dup lblk vp %p bp %p",
908 vp, bp);
909 }
910 bp->b_xflags |= BX_VNDIRTY;
911 }
912 if ((vp->v_flag & VONWORKLST) == 0) {
913 switch (vp->v_type) {
914 case VDIR:
915 delay = dirdelay;
916 break;
917 case VCHR:
918 case VBLK:
919 if (vp->v_rdev &&
920 vp->v_rdev->si_mountpoint != NULL) {
921 delay = metadelay;
922 break;
923 }
924 /* fall through */
925 default:
926 delay = filedelay;
927 }
928 vn_syncer_add_to_worklist(vp, delay);
929 }
930 } else {
931 /*
932 * Move to the clean list, remove the vnode from the worklist
933 * if no dirty blocks remain.
934 */
935 if (bp->b_xflags & BX_VNDIRTY) {
936 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
937 bp->b_xflags &= ~BX_VNDIRTY;
938 }
939 if ((bp->b_xflags & BX_VNCLEAN) == 0) {
940 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
941 panic("reassignbuf: dup lblk vp %p bp %p",
942 vp, bp);
943 }
944 bp->b_xflags |= BX_VNCLEAN;
945 }
946 if ((vp->v_flag & VONWORKLST) &&
947 RB_EMPTY(&vp->v_rbdirty_tree)) {
948 vp->v_flag &= ~VONWORKLST;
949 LIST_REMOVE(vp, v_synclist);
950 }
951 }
952 crit_exit();
953}
954
955/*
956 * Create a vnode for a block device.
957 * Used for mounting the root file system.
958 */
959int
960bdevvp(dev_t dev, struct vnode **vpp)
961{
962 struct vnode *vp;
963 struct vnode *nvp;
964 int error;
965
966 if (dev == NODEV) {
967 *vpp = NULLVP;
968 return (ENXIO);
969 }
970 error = getspecialvnode(VT_NON, NULL, &spec_vnode_vops, &nvp, 0, 0);
971 if (error) {
972 *vpp = NULLVP;
973 return (error);
974 }
975 vp = nvp;
976 vp->v_type = VCHR;
977 vp->v_udev = dev->si_udev;
978 vx_unlock(vp);
979 *vpp = vp;
980 return (0);
981}
982
983int
984v_associate_rdev(struct vnode *vp, dev_t dev)
985{
986 lwkt_tokref ilock;
987
988 if (dev == NULL || dev == NODEV)
989 return(ENXIO);
990 if (dev_is_good(dev) == 0)
991 return(ENXIO);
992 KKASSERT(vp->v_rdev == NULL);
993 if (dev_ref_debug)
994 printf("Z1");
995 vp->v_rdev = reference_dev(dev);
996 lwkt_gettoken(&ilock, &spechash_token);
997 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_specnext);
998 lwkt_reltoken(&ilock);
999 return(0);
1000}
1001
1002void
1003v_release_rdev(struct vnode *vp)
1004{
1005 lwkt_tokref ilock;
1006 dev_t dev;
1007
1008 if ((dev = vp->v_rdev) != NULL) {
1009 lwkt_gettoken(&ilock, &spechash_token);
1010 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_specnext);
1011 vp->v_rdev = NULL;
1012 release_dev(dev);
1013 lwkt_reltoken(&ilock);
1014 }
1015}
1016
1017/*
1018 * Add a vnode to the alias list hung off the dev_t. We only associate
1019 * the device number with the vnode. The actual device is not associated
1020 * until the vnode is opened (usually in spec_open()), and will be
1021 * disassociated on last close.
1022 */
1023void
1024addaliasu(struct vnode *nvp, udev_t nvp_udev)
1025{
1026 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1027 panic("addaliasu on non-special vnode");
1028 nvp->v_udev = nvp_udev;
1029}
1030
1031/*
1032 * Disassociate a vnode from its underlying filesystem.
1033 *
1034 * The vnode must be VX locked and refd
1035 *
1036 * If there are v_usecount references to the vnode other then ours we have
1037 * to VOP_CLOSE the vnode before we can deactivate and reclaim it.
1038 */
1039void
1040vclean(struct vnode *vp, int flags, struct thread *td)
1041{
1042 int active;
1043 int n;
1044 vm_object_t object;
1045
1046 /*
1047 * If the vnode has already been reclaimed we have nothing to do.
1048 */
1049 if (vp->v_flag & VRECLAIMED)
1050 return;
1051 vp->v_flag |= VRECLAIMED;
1052
1053 /*
1054 * Scrap the vfs cache
1055 */
1056 while (cache_inval_vp(vp, 0) != 0) {
1057 printf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1058 tsleep(vp, 0, "vclninv", 2);
1059 }
1060
1061 /*
1062 * Check to see if the vnode is in use. If so we have to reference it
1063 * before we clean it out so that its count cannot fall to zero and
1064 * generate a race against ourselves to recycle it.
1065 */
1066 active = (vp->v_usecount > 1);
1067
1068 /*
1069 * Clean out any buffers associated with the vnode and destroy its
1070 * object, if it has one.
1071 */
1072 vinvalbuf(vp, V_SAVE, td, 0, 0);
1073
1074 if ((object = vp->v_object) != NULL) {
1075 if (object->ref_count == 0) {
1076 if ((object->flags & OBJ_DEAD) == 0)
1077 vm_object_terminate(object);
1078 } else {
1079 vm_pager_deallocate(object);
1080 }
1081 vp->v_flag &= ~VOBJBUF;
1082 }
1083 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1084
1085 /*
1086 * If purging an active vnode (typically during a forced unmount
1087 * or reboot), it must be closed and deactivated before being
1088 * reclaimed. This isn't really all that safe, but what can
1089 * we do? XXX.
1090 *
1091 * Note that neither of these routines unlocks the vnode.
1092 */
1093 if (active && (flags & DOCLOSE)) {
1094 while ((n = vp->v_opencount) != 0) {
1095 if (vp->v_writecount)
1096 VOP_CLOSE(vp, FWRITE|FNONBLOCK, td);
1097 else
1098 VOP_CLOSE(vp, FNONBLOCK, td);
1099 if (vp->v_opencount == n) {
1100 printf("Warning: unable to force-close"
1101 " vnode %p\n", vp);
1102 break;
1103 }
1104 }
1105 }
1106
1107 /*
1108 * If the vnode has not be deactivated, deactivated it.
1109 */
1110 if ((vp->v_flag & VINACTIVE) == 0) {
1111 vp->v_flag |= VINACTIVE;
1112 VOP_INACTIVE(vp, td);
1113 }
1114
1115 /*
1116 * Reclaim the vnode.
1117 */
1118 if (VOP_RECLAIM(vp, td))
1119 panic("vclean: cannot reclaim");
1120
1121 /*
1122 * Done with purge, notify sleepers of the grim news.
1123 */
1124 vp->v_ops = &dead_vnode_vops;
1125 vn_pollgone(vp);
1126 vp->v_tag = VT_NON;
1127}
1128
1129/*
1130 * Eliminate all activity associated with the requested vnode
1131 * and with all vnodes aliased to the requested vnode.
1132 *
1133 * The vnode must be referenced and vx_lock()'d
1134 *
1135 * revoke { struct vnode *a_vp, int a_flags }
1136 */
1137int
1138vop_stdrevoke(struct vop_revoke_args *ap)
1139{
1140 struct vnode *vp, *vq;
1141 lwkt_tokref ilock;
1142 dev_t dev;
1143
1144 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1145
1146 vp = ap->a_vp;
1147
1148 /*
1149 * If the vnode is already dead don't try to revoke it
1150 */
1151 if (vp->v_flag & VRECLAIMED)
1152 return (0);
1153
1154 /*
1155 * If the vnode has a device association, scrap all vnodes associated
1156 * with the device. Don't let the device disappear on us while we
1157 * are scrapping the vnodes.
1158 *
1159 * The passed vp will probably show up in the list, do not VX lock
1160 * it twice!
1161 */
1162 if (vp->v_type != VCHR && vp->v_type != VBLK)
1163 return(0);
1164 if ((dev = vp->v_rdev) == NULL) {
1165 if ((dev = udev2dev(vp->v_udev, vp->v_type == VBLK)) == NODEV)
1166 return(0);
1167 }
1168 reference_dev(dev);
1169 lwkt_gettoken(&ilock, &spechash_token);
1170 while ((vq = SLIST_FIRST(&dev->si_hlist)) != NULL) {
1171 if (vp == vq || vx_get(vq) == 0) {
1172 if (vq == SLIST_FIRST(&dev->si_hlist))
1173 vgone(vq);
1174 if (vp != vq)
1175 vx_put(vq);
1176 }
1177 }
1178 lwkt_reltoken(&ilock);
1179 release_dev(dev);
1180 return (0);
1181}
1182
1183/*
1184 * Recycle an unused vnode to the front of the free list.
1185 *
1186 * Returns 1 if we were successfully able to recycle the vnode,
1187 * 0 otherwise.
1188 */
1189int
1190vrecycle(struct vnode *vp, struct thread *td)
1191{
1192 if (vp->v_usecount == 1) {
1193 vgone(vp);
1194 return (1);
1195 }
1196 return (0);
1197}
1198
1199/*
1200 * Eliminate all activity associated with a vnode in preparation for reuse.
1201 *
1202 * The vnode must be VX locked and refd and will remain VX locked and refd
1203 * on return. This routine may be called with the vnode in any state, as
1204 * long as it is VX locked. The vnode will be cleaned out and marked
1205 * VRECLAIMED but will not actually be reused until all existing refs and
1206 * holds go away.
1207 *
1208 * NOTE: This routine may be called on a vnode which has not yet been
1209 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1210 * already been reclaimed.
1211 *
1212 * This routine is not responsible for placing us back on the freelist.
1213 * Instead, it happens automatically when the caller releases the VX lock
1214 * (assuming there aren't any other references).
1215 */
1216void
1217vgone(struct vnode *vp)
1218{
1219 /*
1220 * assert that the VX lock is held. This is an absolute requirement
1221 * now for vgone() to be called.
1222 */
1223 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1224
1225 /*
1226 * Clean out the filesystem specific data and set the VRECLAIMED
1227 * bit. Also deactivate the vnode if necessary.
1228 */
1229 vclean(vp, DOCLOSE, curthread);
1230
1231 /*
1232 * Delete from old mount point vnode list, if on one.
1233 */
1234 if (vp->v_mount != NULL)
1235 insmntque(vp, NULL);
1236
1237 /*
1238 * If special device, remove it from special device alias list
1239 * if it is on one. This should normally only occur if a vnode is
1240 * being revoked as the device should otherwise have been released
1241 * naturally.
1242 */
1243 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1244 v_release_rdev(vp);
1245 }
1246
1247 /*
1248 * Set us to VBAD
1249 */
1250 vp->v_type = VBAD;
1251}
1252
1253/*
1254 * Lookup a vnode by device number.
1255 */
1256int
1257vfinddev(dev_t dev, enum vtype type, struct vnode **vpp)
1258{
1259 lwkt_tokref ilock;
1260 struct vnode *vp;
1261
1262 lwkt_gettoken(&ilock, &spechash_token);
1263 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1264 if (type == vp->v_type) {
1265 *vpp = vp;
1266 lwkt_reltoken(&ilock);
1267 return (1);
1268 }
1269 }
1270 lwkt_reltoken(&ilock);
1271 return (0);
1272}
1273
1274/*
1275 * Calculate the total number of references to a special device. This
1276 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1277 * an overloaded field. Since udev2dev can now return NODEV, we have
1278 * to check for a NULL v_rdev.
1279 */
1280int
1281count_dev(dev_t dev)
1282{
1283 lwkt_tokref ilock;
1284 struct vnode *vp;
1285 int count = 0;
1286
1287 if (SLIST_FIRST(&dev->si_hlist)) {
1288 lwkt_gettoken(&ilock, &spechash_token);
1289 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1290 count += vp->v_usecount;
1291 }
1292 lwkt_reltoken(&ilock);
1293 }
1294 return(count);
1295}
1296
1297int
1298count_udev(udev_t udev)
1299{
1300 dev_t dev;
1301
1302 if ((dev = udev2dev(udev, 0)) == NODEV)
1303 return(0);
1304 return(count_dev(dev));
1305}
1306
1307int
1308vcount(struct vnode *vp)
1309{
1310 if (vp->v_rdev == NULL)
1311 return(0);
1312 return(count_dev(vp->v_rdev));
1313}
1314
1315/*
1316 * Initialize VMIO for a vnode. This routine MUST be called before a
1317 * VFS can issue buffer cache ops on a vnode. It is typically called
1318 * when a vnode is initialized from its inode.
1319 */
1320int
1321vinitvmio(struct vnode *vp, off_t filesize)
1322{
1323 thread_t td = curthread;
1324 vm_object_t object;
1325 int error = 0;
1326
1327retry:
1328 if ((object = vp->v_object) == NULL) {
1329 object = vnode_pager_alloc(vp, filesize, 0, 0);
1330 /*
1331 * Dereference the reference we just created. This assumes
1332 * that the object is associated with the vp.
1333 */
1334 object->ref_count--;
1335 vp->v_usecount--;
1336 } else {
1337 if (object->flags & OBJ_DEAD) {
1338 VOP_UNLOCK(vp, 0, td);
1339 tsleep(object, 0, "vodead", 0);
1340 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1341 goto retry;
1342 }
1343 }
1344 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1345 vp->v_flag |= VOBJBUF;
1346 return (error);
1347}
1348
1349
1350/*
1351 * Print out a description of a vnode.
1352 */
1353static char *typename[] =
1354{"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1355
1356void
1357vprint(char *label, struct vnode *vp)
1358{
1359 char buf[96];
1360
1361 if (label != NULL)
1362 printf("%s: %p: ", label, (void *)vp);
1363 else
1364 printf("%p: ", (void *)vp);
1365 printf("type %s, usecount %d, writecount %d, refcount %d,",
1366 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1367 vp->v_holdcnt);
1368 buf[0] = '\0';
1369 if (vp->v_flag & VROOT)
1370 strcat(buf, "|VROOT");
1371 if (vp->v_flag & VTEXT)
1372 strcat(buf, "|VTEXT");
1373 if (vp->v_flag & VSYSTEM)
1374 strcat(buf, "|VSYSTEM");
1375 if (vp->v_flag & VFREE)
1376 strcat(buf, "|VFREE");
1377 if (vp->v_flag & VOBJBUF)
1378 strcat(buf, "|VOBJBUF");
1379 if (buf[0] != '\0')
1380 printf(" flags (%s)", &buf[1]);
1381 if (vp->v_data == NULL) {
1382 printf("\n");
1383 } else {
1384 printf("\n\t");
1385 VOP_PRINT(vp);
1386 }
1387}
1388
1389#ifdef DDB
1390#include <ddb/ddb.h>
1391
1392static int db_show_locked_vnodes(struct mount *mp, void *data);
1393
1394/*
1395 * List all of the locked vnodes in the system.
1396 * Called when debugging the kernel.
1397 */
1398DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1399{
1400 printf("Locked vnodes\n");
1401 mountlist_scan(db_show_locked_vnodes, NULL,
1402 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1403}
1404
1405static int
1406db_show_locked_vnodes(struct mount *mp, void *data __unused)
1407{
1408 struct vnode *vp;
1409
1410 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1411 if (VOP_ISLOCKED(vp, NULL))
1412 vprint((char *)0, vp);
1413 }
1414 return(0);
1415}
1416#endif
1417
1418/*
1419 * Top level filesystem related information gathering.
1420 */
1421static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1422
1423static int
1424vfs_sysctl(SYSCTL_HANDLER_ARGS)
1425{
1426 int *name = (int *)arg1 - 1; /* XXX */
1427 u_int namelen = arg2 + 1; /* XXX */
1428 struct vfsconf *vfsp;
1429
1430#if 1 || defined(COMPAT_PRELITE2)
1431 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1432 if (namelen == 1)
1433 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1434#endif
1435
1436#ifdef notyet
1437 /* all sysctl names at this level are at least name and field */
1438 if (namelen < 2)
1439 return (ENOTDIR); /* overloaded */
1440 if (name[0] != VFS_GENERIC) {
1441 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1442 if (vfsp->vfc_typenum == name[0])
1443 break;
1444 if (vfsp == NULL)
1445 return (EOPNOTSUPP);
1446 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1447 oldp, oldlenp, newp, newlen, p));
1448 }
1449#endif
1450 switch (name[1]) {
1451 case VFS_MAXTYPENUM:
1452 if (namelen != 2)
1453 return (ENOTDIR);
1454 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
1455 case VFS_CONF:
1456 if (namelen != 3)
1457 return (ENOTDIR); /* overloaded */
1458 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1459 if (vfsp->vfc_typenum == name[2])
1460 break;
1461 if (vfsp == NULL)
1462 return (EOPNOTSUPP);
1463 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1464 }
1465 return (EOPNOTSUPP);
1466}
1467
1468SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1469 "Generic filesystem");
1470
1471#if 1 || defined(COMPAT_PRELITE2)
1472
1473static int
1474sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1475{
1476 int error;
1477 struct vfsconf *vfsp;
1478 struct ovfsconf ovfs;
1479
1480 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1481 bzero(&ovfs, sizeof(ovfs));
1482 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1483 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1484 ovfs.vfc_index = vfsp->vfc_typenum;
1485 ovfs.vfc_refcount = vfsp->vfc_refcount;
1486 ovfs.vfc_flags = vfsp->vfc_flags;
1487 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1488 if (error)
1489 return error;
1490 }
1491 return 0;
1492}
1493
1494#endif /* 1 || COMPAT_PRELITE2 */
1495
1496/*
1497 * Check to see if a filesystem is mounted on a block device.
1498 */
1499int
1500vfs_mountedon(struct vnode *vp)
1501{
1502 dev_t dev;
1503
1504 if ((dev = vp->v_rdev) == NULL)
1505 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
1506 if (dev != NODEV && dev->si_mountpoint)
1507 return (EBUSY);
1508 return (0);
1509}
1510
1511/*
1512 * Unmount all filesystems. The list is traversed in reverse order
1513 * of mounting to avoid dependencies.
1514 */
1515
1516static int vfs_umountall_callback(struct mount *mp, void *data);
1517
1518void
1519vfs_unmountall(void)
1520{
1521 struct thread *td = curthread;
1522 int count;
1523
1524 if (td->td_proc == NULL)
1525 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
1526
1527 do {
1528 count = mountlist_scan(vfs_umountall_callback,
1529 &td, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1530 } while (count);
1531}
1532
1533static
1534int
1535vfs_umountall_callback(struct mount *mp, void *data)
1536{
1537 struct thread *td = *(struct thread **)data;
1538 int error;
1539
1540 error = dounmount(mp, MNT_FORCE, td);
1541 if (error) {
1542 mountlist_remove(mp);
1543 printf("unmount of filesystem mounted from %s failed (",
1544 mp->mnt_stat.f_mntfromname);
1545 if (error == EBUSY)
1546 printf("BUSY)\n");
1547 else
1548 printf("%d)\n", error);
1549 }
1550 return(1);
1551}
1552
1553/*
1554 * Build hash lists of net addresses and hang them off the mount point.
1555 * Called by ufs_mount() to set up the lists of export addresses.
1556 */
1557static int
1558vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1559 struct export_args *argp)
1560{
1561 struct netcred *np;
1562 struct radix_node_head *rnh;
1563 int i;
1564 struct radix_node *rn;
1565 struct sockaddr *saddr, *smask = 0;
1566 struct domain *dom;
1567 int error;
1568
1569 if (argp->ex_addrlen == 0) {
1570 if (mp->mnt_flag & MNT_DEFEXPORTED)
1571 return (EPERM);
1572 np = &nep->ne_defexported;
1573 np->netc_exflags = argp->ex_flags;
1574 np->netc_anon = argp->ex_anon;
1575 np->netc_anon.cr_ref = 1;
1576 mp->mnt_flag |= MNT_DEFEXPORTED;
1577 return (0);
1578 }
1579
1580 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1581 return (EINVAL);
1582 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1583 return (EINVAL);
1584
1585 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1586 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
1587 bzero((caddr_t) np, i);
1588 saddr = (struct sockaddr *) (np + 1);
1589 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1590 goto out;
1591 if (saddr->sa_len > argp->ex_addrlen)
1592 saddr->sa_len = argp->ex_addrlen;
1593 if (argp->ex_masklen) {
1594 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1595 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1596 if (error)
1597 goto out;
1598 if (smask->sa_len > argp->ex_masklen)
1599 smask->sa_len = argp->ex_masklen;
1600 }
1601 i = saddr->sa_family;
1602 if ((rnh = nep->ne_rtable[i]) == 0) {
1603 /*
1604 * Seems silly to initialize every AF when most are not used,
1605 * do so on demand here
1606 */
1607 SLIST_FOREACH(dom, &domains, dom_next)
1608 if (dom->dom_family == i && dom->dom_rtattach) {
1609 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1610 dom->dom_rtoffset);
1611 break;
1612 }
1613 if ((rnh = nep->ne_rtable[i]) == 0) {
1614 error = ENOBUFS;
1615 goto out;
1616 }
1617 }
1618 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1619 np->netc_rnodes);
1620 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1621 error = EPERM;
1622 goto out;
1623 }
1624 np->netc_exflags = argp->ex_flags;
1625 np->netc_anon = argp->ex_anon;
1626 np->netc_anon.cr_ref = 1;
1627 return (0);
1628out:
1629 free(np, M_NETADDR);
1630 return (error);
1631}
1632
1633/* ARGSUSED */
1634static int
1635vfs_free_netcred(struct radix_node *rn, void *w)
1636{
1637 struct radix_node_head *rnh = (struct radix_node_head *) w;
1638
1639 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1640 free((caddr_t) rn, M_NETADDR);
1641 return (0);
1642}
1643
1644/*
1645 * Free the net address hash lists that are hanging off the mount points.
1646 */
1647static void
1648vfs_free_addrlist(struct netexport *nep)
1649{
1650 int i;
1651 struct radix_node_head *rnh;
1652
1653 for (i = 0; i <= AF_MAX; i++)
1654 if ((rnh = nep->ne_rtable[i])) {
1655 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1656 (caddr_t) rnh);
1657 free((caddr_t) rnh, M_RTABLE);
1658 nep->ne_rtable[i] = 0;
1659 }
1660}
1661
1662int
1663vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp)
1664{
1665 int error;
1666
1667 if (argp->ex_flags & MNT_DELEXPORT) {
1668 if (mp->mnt_flag & MNT_EXPUBLIC) {
1669 vfs_setpublicfs(NULL, NULL, NULL);
1670 mp->mnt_flag &= ~MNT_EXPUBLIC;
1671 }
1672 vfs_free_addrlist(nep);
1673 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1674 }
1675 if (argp->ex_flags & MNT_EXPORTED) {
1676 if (argp->ex_flags & MNT_EXPUBLIC) {
1677 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1678 return (error);
1679 mp->mnt_flag |= MNT_EXPUBLIC;
1680 }
1681 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1682 return (error);
1683 mp->mnt_flag |= MNT_EXPORTED;
1684 }
1685 return (0);
1686}
1687
1688
1689/*
1690 * Set the publicly exported filesystem (WebNFS). Currently, only
1691 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1692 */
1693int
1694vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1695 struct export_args *argp)
1696{
1697 int error;
1698 struct vnode *rvp;
1699 char *cp;
1700
1701 /*
1702 * mp == NULL -> invalidate the current info, the FS is
1703 * no longer exported. May be called from either vfs_export
1704 * or unmount, so check if it hasn't already been done.
1705 */
1706 if (mp == NULL) {
1707 if (nfs_pub.np_valid) {
1708 nfs_pub.np_valid = 0;
1709 if (nfs_pub.np_index != NULL) {
1710 FREE(nfs_pub.np_index, M_TEMP);
1711 nfs_pub.np_index = NULL;
1712 }
1713 }
1714 return (0);
1715 }
1716
1717 /*
1718 * Only one allowed at a time.
1719 */
1720 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1721 return (EBUSY);
1722
1723 /*
1724 * Get real filehandle for root of exported FS.
1725 */
1726 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1727 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1728
1729 if ((error = VFS_ROOT(mp, &rvp)))
1730 return (error);
1731
1732 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1733 return (error);
1734
1735 vput(rvp);
1736
1737 /*
1738 * If an indexfile was specified, pull it in.
1739 */
1740 if (argp->ex_indexfile != NULL) {
1741 int namelen;
1742
1743 error = vn_get_namelen(rvp, &namelen);
1744 if (error)
1745 return (error);
1746 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1747 M_WAITOK);
1748 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1749 namelen, (size_t *)0);
1750 if (!error) {
1751 /*
1752 * Check for illegal filenames.
1753 */
1754 for (cp = nfs_pub.np_index; *cp; cp++) {
1755 if (*cp == '/') {
1756 error = EINVAL;
1757 break;
1758 }
1759 }
1760 }
1761 if (error) {
1762 FREE(nfs_pub.np_index, M_TEMP);
1763 return (error);
1764 }
1765 }
1766
1767 nfs_pub.np_mount = mp;
1768 nfs_pub.np_valid = 1;
1769 return (0);
1770}
1771
1772struct netcred *
1773vfs_export_lookup(struct mount *mp, struct netexport *nep,
1774 struct sockaddr *nam)
1775{
1776 struct netcred *np;
1777 struct radix_node_head *rnh;
1778 struct sockaddr *saddr;
1779
1780 np = NULL;
1781 if (mp->mnt_flag & MNT_EXPORTED) {
1782 /*
1783 * Lookup in the export list first.
1784 */
1785 if (nam != NULL) {
1786 saddr = nam;
1787 rnh = nep->ne_rtable[saddr->sa_family];
1788 if (rnh != NULL) {
1789 np = (struct netcred *)
1790 (*rnh->rnh_matchaddr)((char *)saddr,
1791 rnh);
1792 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1793 np = NULL;
1794 }
1795 }
1796 /*
1797 * If no address match, use the default if it exists.
1798 */
1799 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1800 np = &nep->ne_defexported;
1801 }
1802 return (np);
1803}
1804
1805/*
1806 * perform msync on all vnodes under a mount point. The mount point must
1807 * be locked. This code is also responsible for lazy-freeing unreferenced
1808 * vnodes whos VM objects no longer contain pages.
1809 *
1810 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1811 *
1812 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1813 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1814 * way up in this high level function.
1815 */
1816static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1817static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1818
1819void
1820vfs_msync(struct mount *mp, int flags)
1821{
1822 int vmsc_flags;
1823
1824 vmsc_flags = VMSC_GETVP;
1825 if (flags != MNT_WAIT)
1826 vmsc_flags |= VMSC_NOWAIT;
1827 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1828 (void *)flags);
1829}
1830
1831/*
1832 * scan1 is a fast pre-check. There could be hundreds of thousands of
1833 * vnodes, we cannot afford to do anything heavy weight until we have a
1834 * fairly good indication that there is work to do.
1835 */
1836static
1837int
1838vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1839{
1840 int flags = (int)data;
1841
1842 if ((vp->v_flag & VRECLAIMED) == 0) {
1843 if (vshouldfree(vp, 0))
1844 return(0); /* call scan2 */
1845 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1846 (vp->v_flag & VOBJDIRTY) &&
1847 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
1848 return(0); /* call scan2 */
1849 }
1850 }
1851
1852 /*
1853 * do not call scan2, continue the loop
1854 */
1855 return(-1);
1856}
1857
1858/*
1859 * This callback is handed a locked vnode.
1860 */
1861static
1862int
1863vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1864{
1865 vm_object_t obj;
1866 int flags = (int)data;
1867
1868 if (vp->v_flag & VRECLAIMED)
1869 return(0);
1870
1871 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1872 if ((obj = vp->v_object) != NULL) {
1873 vm_object_page_clean(obj, 0, 0,
1874 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1875 }
1876 }
1877 return(0);
1878}
1879
1880/*
1881 * Record a process's interest in events which might happen to
1882 * a vnode. Because poll uses the historic select-style interface
1883 * internally, this routine serves as both the ``check for any
1884 * pending events'' and the ``record my interest in future events''
1885 * functions. (These are done together, while the lock is held,
1886 * to avoid race conditions.)
1887 */
1888int
1889vn_pollrecord(struct vnode *vp, struct thread *td, int events)
1890{
1891 lwkt_tokref ilock;
1892
1893 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1894 if (vp->v_pollinfo.vpi_revents & events) {
1895 /*
1896 * This leaves events we are not interested
1897 * in available for the other process which
1898 * which presumably had requested them
1899 * (otherwise they would never have been
1900 * recorded).
1901 */
1902 events &= vp->v_pollinfo.vpi_revents;
1903 vp->v_pollinfo.vpi_revents &= ~events;
1904
1905 lwkt_reltoken(&ilock);
1906 return events;
1907 }
1908 vp->v_pollinfo.vpi_events |= events;
1909 selrecord(td, &vp->v_pollinfo.vpi_selinfo);
1910 lwkt_reltoken(&ilock);
1911 return 0;
1912}
1913
1914/*
1915 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1916 * it is possible for us to miss an event due to race conditions, but
1917 * that condition is expected to be rare, so for the moment it is the
1918 * preferred interface.
1919 */
1920void
1921vn_pollevent(struct vnode *vp, int events)
1922{
1923 lwkt_tokref ilock;
1924
1925 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1926 if (vp->v_pollinfo.vpi_events & events) {
1927 /*
1928 * We clear vpi_events so that we don't
1929 * call selwakeup() twice if two events are
1930 * posted before the polling process(es) is
1931 * awakened. This also ensures that we take at
1932 * most one selwakeup() if the polling process
1933 * is no longer interested. However, it does
1934 * mean that only one event can be noticed at
1935 * a time. (Perhaps we should only clear those
1936 * event bits which we note?) XXX
1937 */
1938 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
1939 vp->v_pollinfo.vpi_revents |= events;
1940 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1941 }
1942 lwkt_reltoken(&ilock);
1943}
1944
1945/*
1946 * Wake up anyone polling on vp because it is being revoked.
1947 * This depends on dead_poll() returning POLLHUP for correct
1948 * behavior.
1949 */
1950void
1951vn_pollgone(struct vnode *vp)
1952{
1953 lwkt_tokref ilock;
1954
1955 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1956 if (vp->v_pollinfo.vpi_events) {
1957 vp->v_pollinfo.vpi_events = 0;
1958 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1959 }
1960 lwkt_reltoken(&ilock);
1961}
1962
1963/*
1964 * extract the dev_t from a VBLK or VCHR. The vnode must have been opened
1965 * (or v_rdev might be NULL).
1966 */
1967dev_t
1968vn_todev(struct vnode *vp)
1969{
1970 if (vp->v_type != VBLK && vp->v_type != VCHR)
1971 return (NODEV);
1972 KKASSERT(vp->v_rdev != NULL);
1973 return (vp->v_rdev);
1974}
1975
1976/*
1977 * Check if vnode represents a disk device. The vnode does not need to be
1978 * opened.
1979 */
1980int
1981vn_isdisk(struct vnode *vp, int *errp)
1982{
1983 dev_t dev;
1984
1985 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1986 if (errp != NULL)
1987 *errp = ENOTBLK;
1988 return (0);
1989 }
1990
1991 if ((dev = vp->v_rdev) == NULL)
1992 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
1993 if (dev == NULL || dev == NODEV) {
1994 if (errp != NULL)
1995 *errp = ENXIO;
1996 return (0);
1997 }
1998 if (dev_is_good(dev) == 0) {
1999 if (errp != NULL)
2000 *errp = ENXIO;
2001 return (0);
2002 }
2003 if ((dev_dflags(dev) & D_DISK) == 0) {
2004 if (errp != NULL)
2005 *errp = ENOTBLK;
2006 return (0);
2007 }
2008 if (errp != NULL)
2009 *errp = 0;
2010 return (1);
2011}
2012
2013#ifdef DEBUG_VFS_LOCKS
2014
2015void
2016assert_vop_locked(struct vnode *vp, const char *str)
2017{
2018 if (vp && IS_LOCKING_VFS(vp) && !VOP_ISLOCKED(vp, NULL)) {
2019 panic("%s: %p is not locked shared but should be", str, vp);
2020 }
2021}
2022
2023void
2024assert_vop_unlocked(struct vnode *vp, const char *str)
2025{
2026 if (vp && IS_LOCKING_VFS(vp)) {
2027 if (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) {
2028 panic("%s: %p is locked but should not be", str, vp);
2029 }
2030 }
2031}
2032
2033#endif
2034
2035int
2036vn_get_namelen(struct vnode *vp, int *namelen)
2037{
2038 int error, retval[2];
2039
2040 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2041 if (error)
2042 return (error);
2043 *namelen = *retval;
2044 return (0);
2045}
2046
2047int
2048vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2049 uint16_t d_namlen, const char *d_name)
2050{
2051 struct dirent *dp;
2052 size_t len;
2053
2054 len = _DIRENT_RECLEN(d_namlen);
2055 if (len > uio->uio_resid)
2056 return(1);
2057
2058 dp = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
2059
2060 dp->d_ino = d_ino;
2061 dp->d_namlen = d_namlen;
2062 dp->d_type = d_type;
2063 bcopy(d_name, dp->d_name, d_namlen);
2064
2065 *error = uiomove((caddr_t)dp, len, uio);
2066
2067 free(dp, M_TEMP);
2068
2069 return(0);
2070}
2071