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