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