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