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