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