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