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