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