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