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