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