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