minor code optimization.
[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 $
3020e3be 40 * $DragonFly: src/sys/kern/vfs_subr.c,v 1.3 2003/06/19 01:55:06 dillon Exp $
984263bc
MD
41 */
42
43/*
44 * External virtual filesystem routines
45 */
46#include "opt_ddb.h"
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/buf.h>
51#include <sys/conf.h>
52#include <sys/dirent.h>
53#include <sys/domain.h>
54#include <sys/eventhandler.h>
55#include <sys/fcntl.h>
56#include <sys/kernel.h>
57#include <sys/kthread.h>
58#include <sys/malloc.h>
59#include <sys/mbuf.h>
60#include <sys/mount.h>
61#include <sys/namei.h>
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>
68#include <sys/vmmeter.h>
69#include <sys/vnode.h>
70
71#include <machine/limits.h>
72
73#include <vm/vm.h>
74#include <vm/vm_object.h>
75#include <vm/vm_extern.h>
76#include <vm/pmap.h>
77#include <vm/vm_map.h>
78#include <vm/vm_page.h>
79#include <vm/vm_pager.h>
80#include <vm/vnode_pager.h>
81#include <vm/vm_zone.h>
82
3020e3be
MD
83#include <sys/buf2.h>
84
984263bc
MD
85static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
86
87static void insmntque __P((struct vnode *vp, struct mount *mp));
88static void vclean __P((struct vnode *vp, int flags, struct proc *p));
89static unsigned long numvnodes;
90static void vlruvp(struct vnode *vp);
91SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
92
93enum vtype iftovt_tab[16] = {
94 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
95 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
96};
97int vttoif_tab[9] = {
98 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
99 S_IFSOCK, S_IFIFO, S_IFMT,
100};
101
102static TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
103
104static u_long wantfreevnodes = 25;
105SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
106static u_long freevnodes = 0;
107SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
108
109static int reassignbufcalls;
110SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
111static int reassignbufloops;
112SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, "");
113static int reassignbufsortgood;
114SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, "");
115static int reassignbufsortbad;
116SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, "");
117static int reassignbufmethod = 1;
118SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 0, "");
119static int nameileafonly = 0;
120SYSCTL_INT(_vfs, OID_AUTO, nameileafonly, CTLFLAG_RW, &nameileafonly, 0, "");
121
122#ifdef ENABLE_VFS_IOOPT
123int vfs_ioopt = 0;
124SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
125#endif
126
127struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); /* mounted fs */
128struct simplelock mountlist_slock;
129struct simplelock mntvnode_slock;
130int nfs_mount_type = -1;
131#ifndef NULL_SIMPLELOCKS
132static struct simplelock mntid_slock;
133static struct simplelock vnode_free_list_slock;
134static struct simplelock spechash_slock;
135#endif
136struct nfs_public nfs_pub; /* publicly exported FS */
137static vm_zone_t vnode_zone;
138
139/*
140 * The workitem queue.
141 */
142#define SYNCER_MAXDELAY 32
143static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
144time_t syncdelay = 30; /* max time to delay syncing data */
145time_t filedelay = 30; /* time to delay syncing files */
146SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
147time_t dirdelay = 29; /* time to delay syncing directories */
148SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
149time_t metadelay = 28; /* time to delay syncing metadata */
150SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
151static int rushjob; /* number of slots to run ASAP */
152static int stat_rush_requests; /* number of times I/O speeded up */
153SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
154
155static int syncer_delayno = 0;
156static long syncer_mask;
157LIST_HEAD(synclist, vnode);
158static struct synclist *syncer_workitem_pending;
159
160int desiredvnodes;
161SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
162 &desiredvnodes, 0, "Maximum number of vnodes");
163static int minvnodes;
164SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
165 &minvnodes, 0, "Minimum number of vnodes");
166static int vnlru_nowhere = 0;
167SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0,
168 "Number of times the vnlru process ran without success");
169
170static void vfs_free_addrlist __P((struct netexport *nep));
171static int vfs_free_netcred __P((struct radix_node *rn, void *w));
172static int vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
173 struct export_args *argp));
174
175/*
176 * Initialize the vnode management data structures.
177 */
178void
179vntblinit()
180{
181
182 desiredvnodes = maxproc + cnt.v_page_count / 4;
183 minvnodes = desiredvnodes / 4;
184 simple_lock_init(&mntvnode_slock);
185 simple_lock_init(&mntid_slock);
186 simple_lock_init(&spechash_slock);
187 TAILQ_INIT(&vnode_free_list);
188 simple_lock_init(&vnode_free_list_slock);
189 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
190 /*
191 * Initialize the filesystem syncer.
192 */
193 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
194 &syncer_mask);
195 syncer_maxdelay = syncer_mask + 1;
196}
197
198/*
199 * Mark a mount point as busy. Used to synchronize access and to delay
200 * unmounting. Interlock is not released on failure.
201 */
202int
203vfs_busy(mp, flags, interlkp, p)
204 struct mount *mp;
205 int flags;
206 struct simplelock *interlkp;
207 struct proc *p;
208{
209 int lkflags;
210
211 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
212 if (flags & LK_NOWAIT)
213 return (ENOENT);
214 mp->mnt_kern_flag |= MNTK_MWAIT;
215 if (interlkp) {
216 simple_unlock(interlkp);
217 }
218 /*
219 * Since all busy locks are shared except the exclusive
220 * lock granted when unmounting, the only place that a
221 * wakeup needs to be done is at the release of the
222 * exclusive lock at the end of dounmount.
223 */
224 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
225 if (interlkp) {
226 simple_lock(interlkp);
227 }
228 return (ENOENT);
229 }
230 lkflags = LK_SHARED | LK_NOPAUSE;
231 if (interlkp)
232 lkflags |= LK_INTERLOCK;
233 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p))
234 panic("vfs_busy: unexpected lock failure");
235 return (0);
236}
237
238/*
239 * Free a busy filesystem.
240 */
241void
242vfs_unbusy(mp, p)
243 struct mount *mp;
244 struct proc *p;
245{
246
247 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p);
248}
249
250/*
251 * Lookup a filesystem type, and if found allocate and initialize
252 * a mount structure for it.
253 *
254 * Devname is usually updated by mount(8) after booting.
255 */
256int
257vfs_rootmountalloc(fstypename, devname, mpp)
258 char *fstypename;
259 char *devname;
260 struct mount **mpp;
261{
262 struct proc *p = curproc; /* XXX */
263 struct vfsconf *vfsp;
264 struct mount *mp;
265
266 if (fstypename == NULL)
267 return (ENODEV);
268 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
269 if (!strcmp(vfsp->vfc_name, fstypename))
270 break;
271 if (vfsp == NULL)
272 return (ENODEV);
273 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
274 bzero((char *)mp, (u_long)sizeof(struct mount));
275 lockinit(&mp->mnt_lock, PVFS, "vfslock", VLKTIMEOUT, LK_NOPAUSE);
276 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
277 TAILQ_INIT(&mp->mnt_nvnodelist);
278 TAILQ_INIT(&mp->mnt_reservedvnlist);
279 mp->mnt_nvnodelistsize = 0;
280 mp->mnt_vfc = vfsp;
281 mp->mnt_op = vfsp->vfc_vfsops;
282 mp->mnt_flag = MNT_RDONLY;
283 mp->mnt_vnodecovered = NULLVP;
284 vfsp->vfc_refcount++;
285 mp->mnt_iosize_max = DFLTPHYS;
286 mp->mnt_stat.f_type = vfsp->vfc_typenum;
287 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
288 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
289 mp->mnt_stat.f_mntonname[0] = '/';
290 mp->mnt_stat.f_mntonname[1] = 0;
291 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
292 *mpp = mp;
293 return (0);
294}
295
296/*
297 * Find an appropriate filesystem to use for the root. If a filesystem
298 * has not been preselected, walk through the list of known filesystems
299 * trying those that have mountroot routines, and try them until one
300 * works or we have tried them all.
301 */
302#ifdef notdef /* XXX JH */
303int
304lite2_vfs_mountroot()
305{
306 struct vfsconf *vfsp;
307 extern int (*lite2_mountroot) __P((void));
308 int error;
309
310 if (lite2_mountroot != NULL)
311 return ((*lite2_mountroot)());
312 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
313 if (vfsp->vfc_mountroot == NULL)
314 continue;
315 if ((error = (*vfsp->vfc_mountroot)()) == 0)
316 return (0);
317 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
318 }
319 return (ENODEV);
320}
321#endif
322
323/*
324 * Lookup a mount point by filesystem identifier.
325 */
326struct mount *
327vfs_getvfs(fsid)
328 fsid_t *fsid;
329{
330 register struct mount *mp;
331
332 simple_lock(&mountlist_slock);
333 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
334 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
335 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
336 simple_unlock(&mountlist_slock);
337 return (mp);
338 }
339 }
340 simple_unlock(&mountlist_slock);
341 return ((struct mount *) 0);
342}
343
344/*
345 * Get a new unique fsid. Try to make its val[0] unique, since this value
346 * will be used to create fake device numbers for stat(). Also try (but
347 * not so hard) make its val[0] unique mod 2^16, since some emulators only
348 * support 16-bit device numbers. We end up with unique val[0]'s for the
349 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
350 *
351 * Keep in mind that several mounts may be running in parallel. Starting
352 * the search one past where the previous search terminated is both a
353 * micro-optimization and a defense against returning the same fsid to
354 * different mounts.
355 */
356void
357vfs_getnewfsid(mp)
358 struct mount *mp;
359{
360 static u_int16_t mntid_base;
361 fsid_t tfsid;
362 int mtype;
363
364 simple_lock(&mntid_slock);
365 mtype = mp->mnt_vfc->vfc_typenum;
366 tfsid.val[1] = mtype;
367 mtype = (mtype & 0xFF) << 24;
368 for (;;) {
369 tfsid.val[0] = makeudev(255,
370 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
371 mntid_base++;
372 if (vfs_getvfs(&tfsid) == NULL)
373 break;
374 }
375 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
376 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
377 simple_unlock(&mntid_slock);
378}
379
380/*
381 * Knob to control the precision of file timestamps:
382 *
383 * 0 = seconds only; nanoseconds zeroed.
384 * 1 = seconds and nanoseconds, accurate within 1/HZ.
385 * 2 = seconds and nanoseconds, truncated to microseconds.
386 * >=3 = seconds and nanoseconds, maximum precision.
387 */
388enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
389
390static int timestamp_precision = TSP_SEC;
391SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
392 &timestamp_precision, 0, "");
393
394/*
395 * Get a current timestamp.
396 */
397void
398vfs_timestamp(tsp)
399 struct timespec *tsp;
400{
401 struct timeval tv;
402
403 switch (timestamp_precision) {
404 case TSP_SEC:
405 tsp->tv_sec = time_second;
406 tsp->tv_nsec = 0;
407 break;
408 case TSP_HZ:
409 getnanotime(tsp);
410 break;
411 case TSP_USEC:
412 microtime(&tv);
413 TIMEVAL_TO_TIMESPEC(&tv, tsp);
414 break;
415 case TSP_NSEC:
416 default:
417 nanotime(tsp);
418 break;
419 }
420}
421
422/*
423 * Set vnode attributes to VNOVAL
424 */
425void
426vattr_null(vap)
427 register struct vattr *vap;
428{
429
430 vap->va_type = VNON;
431 vap->va_size = VNOVAL;
432 vap->va_bytes = VNOVAL;
433 vap->va_mode = VNOVAL;
434 vap->va_nlink = VNOVAL;
435 vap->va_uid = VNOVAL;
436 vap->va_gid = VNOVAL;
437 vap->va_fsid = VNOVAL;
438 vap->va_fileid = VNOVAL;
439 vap->va_blocksize = VNOVAL;
440 vap->va_rdev = VNOVAL;
441 vap->va_atime.tv_sec = VNOVAL;
442 vap->va_atime.tv_nsec = VNOVAL;
443 vap->va_mtime.tv_sec = VNOVAL;
444 vap->va_mtime.tv_nsec = VNOVAL;
445 vap->va_ctime.tv_sec = VNOVAL;
446 vap->va_ctime.tv_nsec = VNOVAL;
447 vap->va_flags = VNOVAL;
448 vap->va_gen = VNOVAL;
449 vap->va_vaflags = 0;
450}
451
452/*
453 * This routine is called when we have too many vnodes. It attempts
454 * to free <count> vnodes and will potentially free vnodes that still
455 * have VM backing store (VM backing store is typically the cause
456 * of a vnode blowout so we want to do this). Therefore, this operation
457 * is not considered cheap.
458 *
459 * A number of conditions may prevent a vnode from being reclaimed.
460 * the buffer cache may have references on the vnode, a directory
461 * vnode may still have references due to the namei cache representing
462 * underlying files, or the vnode may be in active use. It is not
463 * desireable to reuse such vnodes. These conditions may cause the
464 * number of vnodes to reach some minimum value regardless of what
465 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
466 */
467static int
468vlrureclaim(struct mount *mp)
469{
470 struct vnode *vp;
471 int done;
472 int trigger;
473 int usevnodes;
474 int count;
475
476 /*
477 * Calculate the trigger point, don't allow user
478 * screwups to blow us up. This prevents us from
479 * recycling vnodes with lots of resident pages. We
480 * aren't trying to free memory, we are trying to
481 * free vnodes.
482 */
483 usevnodes = desiredvnodes;
484 if (usevnodes <= 0)
485 usevnodes = 1;
486 trigger = cnt.v_page_count * 2 / usevnodes;
487
488 done = 0;
489 simple_lock(&mntvnode_slock);
490 count = mp->mnt_nvnodelistsize / 10 + 1;
491 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
492 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
493 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
494
495 if (vp->v_type != VNON &&
496 vp->v_type != VBAD &&
497 VMIGHTFREE(vp) && /* critical path opt */
498 (vp->v_object == NULL || vp->v_object->resident_page_count < trigger) &&
499 simple_lock_try(&vp->v_interlock)
500 ) {
501 simple_unlock(&mntvnode_slock);
502 if (VMIGHTFREE(vp)) {
503 vgonel(vp, curproc);
504 done++;
505 } else {
506 simple_unlock(&vp->v_interlock);
507 }
508 simple_lock(&mntvnode_slock);
509 }
510 --count;
511 }
512 simple_unlock(&mntvnode_slock);
513 return done;
514}
515
516/*
517 * Attempt to recycle vnodes in a context that is always safe to block.
518 * Calling vlrurecycle() from the bowels of file system code has some
519 * interesting deadlock problems.
520 */
521static struct proc *vnlruproc;
522static int vnlruproc_sig;
523
524static void
525vnlru_proc(void)
526{
527 struct mount *mp, *nmp;
528 int s;
529 int done;
530 struct proc *p = vnlruproc;
531
532 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, p,
533 SHUTDOWN_PRI_FIRST);
534
535 s = splbio();
536 for (;;) {
537 kproc_suspend_loop(p);
538 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
539 vnlruproc_sig = 0;
540 wakeup(&vnlruproc_sig);
541 tsleep(vnlruproc, PVFS, "vlruwt", hz);
542 continue;
543 }
544 done = 0;
545 simple_lock(&mountlist_slock);
546 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
547 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
548 nmp = TAILQ_NEXT(mp, mnt_list);
549 continue;
550 }
551 done += vlrureclaim(mp);
552 simple_lock(&mountlist_slock);
553 nmp = TAILQ_NEXT(mp, mnt_list);
554 vfs_unbusy(mp, p);
555 }
556 simple_unlock(&mountlist_slock);
557 if (done == 0) {
558 vnlru_nowhere++;
559 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
560 }
561 }
562 splx(s);
563}
564
565static struct kproc_desc vnlru_kp = {
566 "vnlru",
567 vnlru_proc,
568 &vnlruproc
569};
570SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
571
572/*
573 * Routines having to do with the management of the vnode table.
574 */
575extern vop_t **dead_vnodeop_p;
576
577/*
578 * Return the next vnode from the free list.
579 */
580int
581getnewvnode(tag, mp, vops, vpp)
582 enum vtagtype tag;
583 struct mount *mp;
584 vop_t **vops;
585 struct vnode **vpp;
586{
587 int s;
588 struct proc *p = curproc; /* XXX */
589 struct vnode *vp = NULL;
590 vm_object_t object;
591
592 s = splbio();
593
594 /*
595 * Try to reuse vnodes if we hit the max. This situation only
596 * occurs in certain large-memory (2G+) situations. We cannot
597 * attempt to directly reclaim vnodes due to nasty recursion
598 * problems.
599 */
600 while (numvnodes - freevnodes > desiredvnodes) {
601 if (vnlruproc_sig == 0) {
602 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
603 wakeup(vnlruproc);
604 }
605 tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz);
606 }
607
608
609 /*
610 * Attempt to reuse a vnode already on the free list, allocating
611 * a new vnode if we can't find one or if we have not reached a
612 * good minimum for good LRU performance.
613 */
614 simple_lock(&vnode_free_list_slock);
615 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
616 int count;
617
618 for (count = 0; count < freevnodes; count++) {
619 vp = TAILQ_FIRST(&vnode_free_list);
620 if (vp == NULL || vp->v_usecount)
621 panic("getnewvnode: free vnode isn't");
622
623 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
624 if ((VOP_GETVOBJECT(vp, &object) == 0 &&
625 (object->resident_page_count || object->ref_count)) ||
626 !simple_lock_try(&vp->v_interlock)) {
627 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
628 vp = NULL;
629 continue;
630 }
631 if (LIST_FIRST(&vp->v_cache_src)) {
632 /*
633 * note: nameileafonly sysctl is temporary,
634 * for debugging only, and will eventually be
635 * removed.
636 */
637 if (nameileafonly > 0) {
638 /*
639 * Do not reuse namei-cached directory
640 * vnodes that have cached
641 * subdirectories.
642 */
643 if (cache_leaf_test(vp) < 0) {
644 simple_unlock(&vp->v_interlock);
645 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
646 vp = NULL;
647 continue;
648 }
649 } else if (nameileafonly < 0 ||
650 vmiodirenable == 0) {
651 /*
652 * Do not reuse namei-cached directory
653 * vnodes if nameileafonly is -1 or
654 * if VMIO backing for directories is
655 * turned off (otherwise we reuse them
656 * too quickly).
657 */
658 simple_unlock(&vp->v_interlock);
659 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
660 vp = NULL;
661 continue;
662 }
663 }
664 break;
665 }
666 }
667
668 if (vp) {
669 vp->v_flag |= VDOOMED;
670 vp->v_flag &= ~VFREE;
671 freevnodes--;
672 simple_unlock(&vnode_free_list_slock);
673 cache_purge(vp);
674 vp->v_lease = NULL;
675 if (vp->v_type != VBAD) {
676 vgonel(vp, p);
677 } else {
678 simple_unlock(&vp->v_interlock);
679 }
680
681#ifdef INVARIANTS
682 {
683 int s;
684
685 if (vp->v_data)
686 panic("cleaned vnode isn't");
687 s = splbio();
688 if (vp->v_numoutput)
689 panic("Clean vnode has pending I/O's");
690 splx(s);
691 }
692#endif
693 vp->v_flag = 0;
694 vp->v_lastw = 0;
695 vp->v_lasta = 0;
696 vp->v_cstart = 0;
697 vp->v_clen = 0;
698 vp->v_socket = 0;
699 vp->v_writecount = 0; /* XXX */
700 } else {
701 simple_unlock(&vnode_free_list_slock);
702 vp = (struct vnode *) zalloc(vnode_zone);
703 bzero((char *) vp, sizeof *vp);
704 simple_lock_init(&vp->v_interlock);
705 vp->v_dd = vp;
706 cache_purge(vp);
707 LIST_INIT(&vp->v_cache_src);
708 TAILQ_INIT(&vp->v_cache_dst);
709 numvnodes++;
710 }
711
712 TAILQ_INIT(&vp->v_cleanblkhd);
713 TAILQ_INIT(&vp->v_dirtyblkhd);
714 vp->v_type = VNON;
715 vp->v_tag = tag;
716 vp->v_op = vops;
717 insmntque(vp, mp);
718 *vpp = vp;
719 vp->v_usecount = 1;
720 vp->v_data = 0;
721 splx(s);
722
723 vfs_object_create(vp, p, p->p_ucred);
724 return (0);
725}
726
727/*
728 * Move a vnode from one mount queue to another.
729 */
730static void
731insmntque(vp, mp)
732 register struct vnode *vp;
733 register struct mount *mp;
734{
735
736 simple_lock(&mntvnode_slock);
737 /*
738 * Delete from old mount point vnode list, if on one.
739 */
740 if (vp->v_mount != NULL) {
741 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
742 ("bad mount point vnode list size"));
743 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
744 vp->v_mount->mnt_nvnodelistsize--;
745 }
746 /*
747 * Insert into list of vnodes for the new mount point, if available.
748 */
749 if ((vp->v_mount = mp) == NULL) {
750 simple_unlock(&mntvnode_slock);
751 return;
752 }
753 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
754 mp->mnt_nvnodelistsize++;
755 simple_unlock(&mntvnode_slock);
756}
757
758/*
759 * Update outstanding I/O count and do wakeup if requested.
760 */
761void
762vwakeup(bp)
763 register struct buf *bp;
764{
765 register struct vnode *vp;
766
767 bp->b_flags &= ~B_WRITEINPROG;
768 if ((vp = bp->b_vp)) {
769 vp->v_numoutput--;
770 if (vp->v_numoutput < 0)
771 panic("vwakeup: neg numoutput");
772 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
773 vp->v_flag &= ~VBWAIT;
774 wakeup((caddr_t) &vp->v_numoutput);
775 }
776 }
777}
778
779/*
780 * Flush out and invalidate all buffers associated with a vnode.
781 * Called with the underlying object locked.
782 */
783int
784vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
785 register struct vnode *vp;
786 int flags;
787 struct ucred *cred;
788 struct proc *p;
789 int slpflag, slptimeo;
790{
791 register struct buf *bp;
792 struct buf *nbp, *blist;
793 int s, error;
794 vm_object_t object;
795
796 if (flags & V_SAVE) {
797 s = splbio();
798 while (vp->v_numoutput) {
799 vp->v_flag |= VBWAIT;
800 error = tsleep((caddr_t)&vp->v_numoutput,
801 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
802 if (error) {
803 splx(s);
804 return (error);
805 }
806 }
807 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
808 splx(s);
809 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
810 return (error);
811 s = splbio();
812 if (vp->v_numoutput > 0 ||
813 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
814 panic("vinvalbuf: dirty bufs");
815 }
816 splx(s);
817 }
818 s = splbio();
819 for (;;) {
820 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
821 if (!blist)
822 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
823 if (!blist)
824 break;
825
826 for (bp = blist; bp; bp = nbp) {
827 nbp = TAILQ_NEXT(bp, b_vnbufs);
828 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
829 error = BUF_TIMELOCK(bp,
830 LK_EXCLUSIVE | LK_SLEEPFAIL,
831 "vinvalbuf", slpflag, slptimeo);
832 if (error == ENOLCK)
833 break;
834 splx(s);
835 return (error);
836 }
837 /*
838 * XXX Since there are no node locks for NFS, I
839 * believe there is a slight chance that a delayed
840 * write will occur while sleeping just above, so
841 * check for it. Note that vfs_bio_awrite expects
842 * buffers to reside on a queue, while VOP_BWRITE and
843 * brelse do not.
844 */
845 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
846 (flags & V_SAVE)) {
847
848 if (bp->b_vp == vp) {
849 if (bp->b_flags & B_CLUSTEROK) {
850 BUF_UNLOCK(bp);
851 vfs_bio_awrite(bp);
852 } else {
853 bremfree(bp);
854 bp->b_flags |= B_ASYNC;
855 VOP_BWRITE(bp->b_vp, bp);
856 }
857 } else {
858 bremfree(bp);
859 (void) VOP_BWRITE(bp->b_vp, bp);
860 }
861 break;
862 }
863 bremfree(bp);
864 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
865 bp->b_flags &= ~B_ASYNC;
866 brelse(bp);
867 }
868 }
869
870 /*
871 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
872 * have write I/O in-progress but if there is a VM object then the
873 * VM object can also have read-I/O in-progress.
874 */
875 do {
876 while (vp->v_numoutput > 0) {
877 vp->v_flag |= VBWAIT;
878 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
879 }
880 if (VOP_GETVOBJECT(vp, &object) == 0) {
881 while (object->paging_in_progress)
882 vm_object_pip_sleep(object, "vnvlbx");
883 }
884 } while (vp->v_numoutput > 0);
885
886 splx(s);
887
888 /*
889 * Destroy the copy in the VM cache, too.
890 */
891 simple_lock(&vp->v_interlock);
892 if (VOP_GETVOBJECT(vp, &object) == 0) {
893 vm_object_page_remove(object, 0, 0,
894 (flags & V_SAVE) ? TRUE : FALSE);
895 }
896 simple_unlock(&vp->v_interlock);
897
898 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
899 panic("vinvalbuf: flush failed");
900 return (0);
901}
902
903/*
904 * Truncate a file's buffer and pages to a specified length. This
905 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
906 * sync activity.
907 */
908int
909vtruncbuf(vp, cred, p, length, blksize)
910 register struct vnode *vp;
911 struct ucred *cred;
912 struct proc *p;
913 off_t length;
914 int blksize;
915{
916 register struct buf *bp;
917 struct buf *nbp;
918 int s, anyfreed;
919 int trunclbn;
920
921 /*
922 * Round up to the *next* lbn.
923 */
924 trunclbn = (length + blksize - 1) / blksize;
925
926 s = splbio();
927restart:
928 anyfreed = 1;
929 for (;anyfreed;) {
930 anyfreed = 0;
931 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
932 nbp = TAILQ_NEXT(bp, b_vnbufs);
933 if (bp->b_lblkno >= trunclbn) {
934 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
935 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
936 goto restart;
937 } else {
938 bremfree(bp);
939 bp->b_flags |= (B_INVAL | B_RELBUF);
940 bp->b_flags &= ~B_ASYNC;
941 brelse(bp);
942 anyfreed = 1;
943 }
944 if (nbp &&
945 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
946 (nbp->b_vp != vp) ||
947 (nbp->b_flags & B_DELWRI))) {
948 goto restart;
949 }
950 }
951 }
952
953 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
954 nbp = TAILQ_NEXT(bp, b_vnbufs);
955 if (bp->b_lblkno >= trunclbn) {
956 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
957 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
958 goto restart;
959 } else {
960 bremfree(bp);
961 bp->b_flags |= (B_INVAL | B_RELBUF);
962 bp->b_flags &= ~B_ASYNC;
963 brelse(bp);
964 anyfreed = 1;
965 }
966 if (nbp &&
967 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
968 (nbp->b_vp != vp) ||
969 (nbp->b_flags & B_DELWRI) == 0)) {
970 goto restart;
971 }
972 }
973 }
974 }
975
976 if (length > 0) {
977restartsync:
978 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
979 nbp = TAILQ_NEXT(bp, b_vnbufs);
980 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
981 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
982 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
983 goto restart;
984 } else {
985 bremfree(bp);
986 if (bp->b_vp == vp) {
987 bp->b_flags |= B_ASYNC;
988 } else {
989 bp->b_flags &= ~B_ASYNC;
990 }
991 VOP_BWRITE(bp->b_vp, bp);
992 }
993 goto restartsync;
994 }
995
996 }
997 }
998
999 while (vp->v_numoutput > 0) {
1000 vp->v_flag |= VBWAIT;
1001 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
1002 }
1003
1004 splx(s);
1005
1006 vnode_pager_setsize(vp, length);
1007
1008 return (0);
1009}
1010
1011/*
1012 * Associate a buffer with a vnode.
1013 */
1014void
1015bgetvp(vp, bp)
1016 register struct vnode *vp;
1017 register struct buf *bp;
1018{
1019 int s;
1020
1021 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1022
1023 vhold(vp);
1024 bp->b_vp = vp;
1025 bp->b_dev = vn_todev(vp);
1026 /*
1027 * Insert onto list for new vnode.
1028 */
1029 s = splbio();
1030 bp->b_xflags |= BX_VNCLEAN;
1031 bp->b_xflags &= ~BX_VNDIRTY;
1032 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1033 splx(s);
1034}
1035
1036/*
1037 * Disassociate a buffer from a vnode.
1038 */
1039void
1040brelvp(bp)
1041 register struct buf *bp;
1042{
1043 struct vnode *vp;
1044 struct buflists *listheadp;
1045 int s;
1046
1047 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1048
1049 /*
1050 * Delete from old vnode list, if on one.
1051 */
1052 vp = bp->b_vp;
1053 s = splbio();
1054 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1055 if (bp->b_xflags & BX_VNDIRTY)
1056 listheadp = &vp->v_dirtyblkhd;
1057 else
1058 listheadp = &vp->v_cleanblkhd;
1059 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1060 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1061 }
1062 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1063 vp->v_flag &= ~VONWORKLST;
1064 LIST_REMOVE(vp, v_synclist);
1065 }
1066 splx(s);
1067 bp->b_vp = (struct vnode *) 0;
1068 vdrop(vp);
1069}
1070
1071/*
1072 * The workitem queue.
1073 *
1074 * It is useful to delay writes of file data and filesystem metadata
1075 * for tens of seconds so that quickly created and deleted files need
1076 * not waste disk bandwidth being created and removed. To realize this,
1077 * we append vnodes to a "workitem" queue. When running with a soft
1078 * updates implementation, most pending metadata dependencies should
1079 * not wait for more than a few seconds. Thus, mounted on block devices
1080 * are delayed only about a half the time that file data is delayed.
1081 * Similarly, directory updates are more critical, so are only delayed
1082 * about a third the time that file data is delayed. Thus, there are
1083 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
1084 * one each second (driven off the filesystem syncer process). The
1085 * syncer_delayno variable indicates the next queue that is to be processed.
1086 * Items that need to be processed soon are placed in this queue:
1087 *
1088 * syncer_workitem_pending[syncer_delayno]
1089 *
1090 * A delay of fifteen seconds is done by placing the request fifteen
1091 * entries later in the queue:
1092 *
1093 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
1094 *
1095 */
1096
1097/*
1098 * Add an item to the syncer work queue.
1099 */
1100static void
1101vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1102{
1103 int s, slot;
1104
1105 s = splbio();
1106
1107 if (vp->v_flag & VONWORKLST) {
1108 LIST_REMOVE(vp, v_synclist);
1109 }
1110
1111 if (delay > syncer_maxdelay - 2)
1112 delay = syncer_maxdelay - 2;
1113 slot = (syncer_delayno + delay) & syncer_mask;
1114
1115 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1116 vp->v_flag |= VONWORKLST;
1117 splx(s);
1118}
1119
1120struct proc *updateproc;
1121static void sched_sync __P((void));
1122static struct kproc_desc up_kp = {
1123 "syncer",
1124 sched_sync,
1125 &updateproc
1126};
1127SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1128
1129/*
1130 * System filesystem synchronizer daemon.
1131 */
1132void
1133sched_sync(void)
1134{
1135 struct synclist *slp;
1136 struct vnode *vp;
1137 long starttime;
1138 int s;
1139 struct proc *p = updateproc;
1140
1141 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, p,
1142 SHUTDOWN_PRI_LAST);
1143
1144 for (;;) {
1145 kproc_suspend_loop(p);
1146
1147 starttime = time_second;
1148
1149 /*
1150 * Push files whose dirty time has expired. Be careful
1151 * of interrupt race on slp queue.
1152 */
1153 s = splbio();
1154 slp = &syncer_workitem_pending[syncer_delayno];
1155 syncer_delayno += 1;
1156 if (syncer_delayno == syncer_maxdelay)
1157 syncer_delayno = 0;
1158 splx(s);
1159
1160 while ((vp = LIST_FIRST(slp)) != NULL) {
1161 if (VOP_ISLOCKED(vp, NULL) == 0) {
1162 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1163 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
1164 VOP_UNLOCK(vp, 0, p);
1165 }
1166 s = splbio();
1167 if (LIST_FIRST(slp) == vp) {
1168 /*
1169 * Note: v_tag VT_VFS vps can remain on the
1170 * worklist too with no dirty blocks, but
1171 * since sync_fsync() moves it to a different
1172 * slot we are safe.
1173 */
1174 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1175 !vn_isdisk(vp, NULL))
1176 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1177 /*
1178 * Put us back on the worklist. The worklist
1179 * routine will remove us from our current
1180 * position and then add us back in at a later
1181 * position.
1182 */
1183 vn_syncer_add_to_worklist(vp, syncdelay);
1184 }
1185 splx(s);
1186 }
1187
1188 /*
1189 * Do soft update processing.
1190 */
1191 if (bioops.io_sync)
1192 (*bioops.io_sync)(NULL);
1193
1194 /*
1195 * The variable rushjob allows the kernel to speed up the
1196 * processing of the filesystem syncer process. A rushjob
1197 * value of N tells the filesystem syncer to process the next
1198 * N seconds worth of work on its queue ASAP. Currently rushjob
1199 * is used by the soft update code to speed up the filesystem
1200 * syncer process when the incore state is getting so far
1201 * ahead of the disk that the kernel memory pool is being
1202 * threatened with exhaustion.
1203 */
1204 if (rushjob > 0) {
1205 rushjob -= 1;
1206 continue;
1207 }
1208 /*
1209 * If it has taken us less than a second to process the
1210 * current work, then wait. Otherwise start right over
1211 * again. We can still lose time if any single round
1212 * takes more than two seconds, but it does not really
1213 * matter as we are just trying to generally pace the
1214 * filesystem activity.
1215 */
1216 if (time_second == starttime)
1217 tsleep(&lbolt, PPAUSE, "syncer", 0);
1218 }
1219}
1220
1221/*
1222 * Request the syncer daemon to speed up its work.
1223 * We never push it to speed up more than half of its
1224 * normal turn time, otherwise it could take over the cpu.
1225 */
1226int
1227speedup_syncer()
1228{
1229 int s;
1230
1231 s = splhigh();
1232 if (updateproc->p_wchan == &lbolt)
1233 setrunnable(updateproc);
1234 splx(s);
1235 if (rushjob < syncdelay / 2) {
1236 rushjob += 1;
1237 stat_rush_requests += 1;
1238 return (1);
1239 }
1240 return(0);
1241}
1242
1243/*
1244 * Associate a p-buffer with a vnode.
1245 *
1246 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1247 * with the buffer. i.e. the bp has not been linked into the vnode or
1248 * ref-counted.
1249 */
1250void
1251pbgetvp(vp, bp)
1252 register struct vnode *vp;
1253 register struct buf *bp;
1254{
1255
1256 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1257
1258 bp->b_vp = vp;
1259 bp->b_flags |= B_PAGING;
1260 bp->b_dev = vn_todev(vp);
1261}
1262
1263/*
1264 * Disassociate a p-buffer from a vnode.
1265 */
1266void
1267pbrelvp(bp)
1268 register struct buf *bp;
1269{
1270
1271 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1272
1273 /* XXX REMOVE ME */
1274 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1275 panic(
1276 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1277 bp,
1278 (int)bp->b_flags
1279 );
1280 }
1281 bp->b_vp = (struct vnode *) 0;
1282 bp->b_flags &= ~B_PAGING;
1283}
1284
1285void
1286pbreassignbuf(bp, newvp)
1287 struct buf *bp;
1288 struct vnode *newvp;
1289{
1290 if ((bp->b_flags & B_PAGING) == 0) {
1291 panic(
1292 "pbreassignbuf() on non phys bp %p",
1293 bp
1294 );
1295 }
1296 bp->b_vp = newvp;
1297}
1298
1299/*
1300 * Reassign a buffer from one vnode to another.
1301 * Used to assign file specific control information
1302 * (indirect blocks) to the vnode to which they belong.
1303 */
1304void
1305reassignbuf(bp, newvp)
1306 register struct buf *bp;
1307 register struct vnode *newvp;
1308{
1309 struct buflists *listheadp;
1310 int delay;
1311 int s;
1312
1313 if (newvp == NULL) {
1314 printf("reassignbuf: NULL");
1315 return;
1316 }
1317 ++reassignbufcalls;
1318
1319 /*
1320 * B_PAGING flagged buffers cannot be reassigned because their vp
1321 * is not fully linked in.
1322 */
1323 if (bp->b_flags & B_PAGING)
1324 panic("cannot reassign paging buffer");
1325
1326 s = splbio();
1327 /*
1328 * Delete from old vnode list, if on one.
1329 */
1330 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1331 if (bp->b_xflags & BX_VNDIRTY)
1332 listheadp = &bp->b_vp->v_dirtyblkhd;
1333 else
1334 listheadp = &bp->b_vp->v_cleanblkhd;
1335 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1336 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1337 if (bp->b_vp != newvp) {
1338 vdrop(bp->b_vp);
1339 bp->b_vp = NULL; /* for clarification */
1340 }
1341 }
1342 /*
1343 * If dirty, put on list of dirty buffers; otherwise insert onto list
1344 * of clean buffers.
1345 */
1346 if (bp->b_flags & B_DELWRI) {
1347 struct buf *tbp;
1348
1349 listheadp = &newvp->v_dirtyblkhd;
1350 if ((newvp->v_flag & VONWORKLST) == 0) {
1351 switch (newvp->v_type) {
1352 case VDIR:
1353 delay = dirdelay;
1354 break;
1355 case VCHR:
1356 case VBLK:
1357 if (newvp->v_specmountpoint != NULL) {
1358 delay = metadelay;
1359 break;
1360 }
1361 /* fall through */
1362 default:
1363 delay = filedelay;
1364 }
1365 vn_syncer_add_to_worklist(newvp, delay);
1366 }
1367 bp->b_xflags |= BX_VNDIRTY;
1368 tbp = TAILQ_FIRST(listheadp);
1369 if (tbp == NULL ||
1370 bp->b_lblkno == 0 ||
1371 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1372 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1373 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1374 ++reassignbufsortgood;
1375 } else if (bp->b_lblkno < 0) {
1376 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1377 ++reassignbufsortgood;
1378 } else if (reassignbufmethod == 1) {
1379 /*
1380 * New sorting algorithm, only handle sequential case,
1381 * otherwise append to end (but before metadata)
1382 */
1383 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1384 (tbp->b_xflags & BX_VNDIRTY)) {
1385 /*
1386 * Found the best place to insert the buffer
1387 */
1388 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1389 ++reassignbufsortgood;
1390 } else {
1391 /*
1392 * Missed, append to end, but before meta-data.
1393 * We know that the head buffer in the list is
1394 * not meta-data due to prior conditionals.
1395 *
1396 * Indirect effects: NFS second stage write
1397 * tends to wind up here, giving maximum
1398 * distance between the unstable write and the
1399 * commit rpc.
1400 */
1401 tbp = TAILQ_LAST(listheadp, buflists);
1402 while (tbp && tbp->b_lblkno < 0)
1403 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1404 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1405 ++reassignbufsortbad;
1406 }
1407 } else {
1408 /*
1409 * Old sorting algorithm, scan queue and insert
1410 */
1411 struct buf *ttbp;
1412 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1413 (ttbp->b_lblkno < bp->b_lblkno)) {
1414 ++reassignbufloops;
1415 tbp = ttbp;
1416 }
1417 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1418 }
1419 } else {
1420 bp->b_xflags |= BX_VNCLEAN;
1421 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1422 if ((newvp->v_flag & VONWORKLST) &&
1423 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1424 newvp->v_flag &= ~VONWORKLST;
1425 LIST_REMOVE(newvp, v_synclist);
1426 }
1427 }
1428 if (bp->b_vp != newvp) {
1429 bp->b_vp = newvp;
1430 vhold(bp->b_vp);
1431 }
1432 splx(s);
1433}
1434
1435/*
1436 * Create a vnode for a block device.
1437 * Used for mounting the root file system.
1438 */
1439int
1440bdevvp(dev, vpp)
1441 dev_t dev;
1442 struct vnode **vpp;
1443{
1444 register struct vnode *vp;
1445 struct vnode *nvp;
1446 int error;
1447
1448 if (dev == NODEV) {
1449 *vpp = NULLVP;
1450 return (ENXIO);
1451 }
1452 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1453 if (error) {
1454 *vpp = NULLVP;
1455 return (error);
1456 }
1457 vp = nvp;
1458 vp->v_type = VBLK;
1459 addalias(vp, dev);
1460 *vpp = vp;
1461 return (0);
1462}
1463
1464/*
1465 * Add vnode to the alias list hung off the dev_t.
1466 *
1467 * The reason for this gunk is that multiple vnodes can reference
1468 * the same physical device, so checking vp->v_usecount to see
1469 * how many users there are is inadequate; the v_usecount for
1470 * the vnodes need to be accumulated. vcount() does that.
1471 */
1472void
1473addaliasu(nvp, nvp_rdev)
1474 struct vnode *nvp;
1475 udev_t nvp_rdev;
1476{
1477
1478 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1479 panic("addaliasu on non-special vnode");
1480 addalias(nvp, udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0));
1481}
1482
1483void
1484addalias(nvp, dev)
1485 struct vnode *nvp;
1486 dev_t dev;
1487{
1488
1489 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1490 panic("addalias on non-special vnode");
1491
1492 nvp->v_rdev = dev;
1493 simple_lock(&spechash_slock);
1494 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1495 simple_unlock(&spechash_slock);
1496}
1497
1498/*
1499 * Grab a particular vnode from the free list, increment its
1500 * reference count and lock it. The vnode lock bit is set if the
1501 * vnode is being eliminated in vgone. The process is awakened
1502 * when the transition is completed, and an error returned to
1503 * indicate that the vnode is no longer usable (possibly having
1504 * been changed to a new file system type).
1505 */
1506int
1507vget(vp, flags, p)
1508 register struct vnode *vp;
1509 int flags;
1510 struct proc *p;
1511{
1512 int error;
1513
1514 /*
1515 * If the vnode is in the process of being cleaned out for
1516 * another use, we wait for the cleaning to finish and then
1517 * return failure. Cleaning is determined by checking that
1518 * the VXLOCK flag is set.
1519 */
1520 if ((flags & LK_INTERLOCK) == 0) {
1521 simple_lock(&vp->v_interlock);
1522 }
1523 if (vp->v_flag & VXLOCK) {
1524 if (vp->v_vxproc == curproc) {
1525#if 0
1526 /* this can now occur in normal operation */
1527 log(LOG_INFO, "VXLOCK interlock avoided\n");
1528#endif
1529 } else {
1530 vp->v_flag |= VXWANT;
1531 simple_unlock(&vp->v_interlock);
1532 tsleep((caddr_t)vp, PINOD, "vget", 0);
1533 return (ENOENT);
1534 }
1535 }
1536
1537 vp->v_usecount++;
1538
1539 if (VSHOULDBUSY(vp))
1540 vbusy(vp);
1541 if (flags & LK_TYPE_MASK) {
1542 if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) {
1543 /*
1544 * must expand vrele here because we do not want
1545 * to call VOP_INACTIVE if the reference count
1546 * drops back to zero since it was never really
1547 * active. We must remove it from the free list
1548 * before sleeping so that multiple processes do
1549 * not try to recycle it.
1550 */
1551 simple_lock(&vp->v_interlock);
1552 vp->v_usecount--;
1553 if (VSHOULDFREE(vp))
1554 vfree(vp);
1555 else
1556 vlruvp(vp);
1557 simple_unlock(&vp->v_interlock);
1558 }
1559 return (error);
1560 }
1561 simple_unlock(&vp->v_interlock);
1562 return (0);
1563}
1564
1565void
1566vref(struct vnode *vp)
1567{
1568 simple_lock(&vp->v_interlock);
1569 vp->v_usecount++;
1570 simple_unlock(&vp->v_interlock);
1571}
1572
1573/*
1574 * Vnode put/release.
1575 * If count drops to zero, call inactive routine and return to freelist.
1576 */
1577void
1578vrele(vp)
1579 struct vnode *vp;
1580{
1581 struct proc *p = curproc; /* XXX */
1582
1583 KASSERT(vp != NULL, ("vrele: null vp"));
1584
1585 simple_lock(&vp->v_interlock);
1586
1587 if (vp->v_usecount > 1) {
1588
1589 vp->v_usecount--;
1590 simple_unlock(&vp->v_interlock);
1591
1592 return;
1593 }
1594
1595 if (vp->v_usecount == 1) {
1596 vp->v_usecount--;
1597 /*
1598 * We must call VOP_INACTIVE with the node locked.
1599 * If we are doing a vpu, the node is already locked,
1600 * but, in the case of vrele, we must explicitly lock
1601 * the vnode before calling VOP_INACTIVE
1602 */
1603
1604 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0)
1605 VOP_INACTIVE(vp, p);
1606 if (VSHOULDFREE(vp))
1607 vfree(vp);
1608 else
1609 vlruvp(vp);
1610 } else {
1611#ifdef DIAGNOSTIC
1612 vprint("vrele: negative ref count", vp);
1613 simple_unlock(&vp->v_interlock);
1614#endif
1615 panic("vrele: negative ref cnt");
1616 }
1617}
1618
1619void
1620vput(vp)
1621 struct vnode *vp;
1622{
1623 struct proc *p = curproc; /* XXX */
1624
1625 KASSERT(vp != NULL, ("vput: null vp"));
1626
1627 simple_lock(&vp->v_interlock);
1628
1629 if (vp->v_usecount > 1) {
1630 vp->v_usecount--;
1631 VOP_UNLOCK(vp, LK_INTERLOCK, p);
1632 return;
1633 }
1634
1635 if (vp->v_usecount == 1) {
1636 vp->v_usecount--;
1637 /*
1638 * We must call VOP_INACTIVE with the node locked.
1639 * If we are doing a vpu, the node is already locked,
1640 * so we just need to release the vnode mutex.
1641 */
1642 simple_unlock(&vp->v_interlock);
1643 VOP_INACTIVE(vp, p);
1644 if (VSHOULDFREE(vp))
1645 vfree(vp);
1646 else
1647 vlruvp(vp);
1648 } else {
1649#ifdef DIAGNOSTIC
1650 vprint("vput: negative ref count", vp);
1651#endif
1652 panic("vput: negative ref cnt");
1653 }
1654}
1655
1656/*
1657 * Somebody doesn't want the vnode recycled.
1658 */
1659void
1660vhold(vp)
1661 register struct vnode *vp;
1662{
1663 int s;
1664
1665 s = splbio();
1666 vp->v_holdcnt++;
1667 if (VSHOULDBUSY(vp))
1668 vbusy(vp);
1669 splx(s);
1670}
1671
1672/*
1673 * One less who cares about this vnode.
1674 */
1675void
1676vdrop(vp)
1677 register struct vnode *vp;
1678{
1679 int s;
1680
1681 s = splbio();
1682 if (vp->v_holdcnt <= 0)
1683 panic("vdrop: holdcnt");
1684 vp->v_holdcnt--;
1685 if (VSHOULDFREE(vp))
1686 vfree(vp);
1687 splx(s);
1688}
1689
1690/*
1691 * Remove any vnodes in the vnode table belonging to mount point mp.
1692 *
1693 * If FORCECLOSE is not specified, there should not be any active ones,
1694 * return error if any are found (nb: this is a user error, not a
1695 * system error). If FORCECLOSE is specified, detach any active vnodes
1696 * that are found.
1697 *
1698 * If WRITECLOSE is set, only flush out regular file vnodes open for
1699 * writing.
1700 *
1701 * SKIPSYSTEM causes any vnodes marked VSYSTEM to be skipped.
1702 *
1703 * `rootrefs' specifies the base reference count for the root vnode
1704 * of this filesystem. The root vnode is considered busy if its
1705 * v_usecount exceeds this value. On a successful return, vflush()
1706 * will call vrele() on the root vnode exactly rootrefs times.
1707 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
1708 * be zero.
1709 */
1710#ifdef DIAGNOSTIC
1711static int busyprt = 0; /* print out busy vnodes */
1712SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1713#endif
1714
1715int
1716vflush(mp, rootrefs, flags)
1717 struct mount *mp;
1718 int rootrefs;
1719 int flags;
1720{
1721 struct proc *p = curproc; /* XXX */
1722 struct vnode *vp, *nvp, *rootvp = NULL;
1723 struct vattr vattr;
1724 int busy = 0, error;
1725
1726 if (rootrefs > 0) {
1727 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
1728 ("vflush: bad args"));
1729 /*
1730 * Get the filesystem root vnode. We can vput() it
1731 * immediately, since with rootrefs > 0, it won't go away.
1732 */
1733 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
1734 return (error);
1735 vput(rootvp);
1736 }
1737 simple_lock(&mntvnode_slock);
1738loop:
1739 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
1740 /*
1741 * Make sure this vnode wasn't reclaimed in getnewvnode().
1742 * Start over if it has (it won't be on the list anymore).
1743 */
1744 if (vp->v_mount != mp)
1745 goto loop;
1746 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
1747
1748 simple_lock(&vp->v_interlock);
1749 /*
1750 * Skip over a vnodes marked VSYSTEM.
1751 */
1752 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1753 simple_unlock(&vp->v_interlock);
1754 continue;
1755 }
1756 /*
1757 * If WRITECLOSE is set, flush out unlinked but still open
1758 * files (even if open only for reading) and regular file
1759 * vnodes open for writing.
1760 */
1761 if ((flags & WRITECLOSE) &&
1762 (vp->v_type == VNON ||
1763 (VOP_GETATTR(vp, &vattr, p->p_ucred, p) == 0 &&
1764 vattr.va_nlink > 0)) &&
1765 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1766 simple_unlock(&vp->v_interlock);
1767 continue;
1768 }
1769
1770 /*
1771 * With v_usecount == 0, all we need to do is clear out the
1772 * vnode data structures and we are done.
1773 */
1774 if (vp->v_usecount == 0) {
1775 simple_unlock(&mntvnode_slock);
1776 vgonel(vp, p);
1777 simple_lock(&mntvnode_slock);
1778 continue;
1779 }
1780
1781 /*
1782 * If FORCECLOSE is set, forcibly close the vnode. For block
1783 * or character devices, revert to an anonymous device. For
1784 * all other files, just kill them.
1785 */
1786 if (flags & FORCECLOSE) {
1787 simple_unlock(&mntvnode_slock);
1788 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1789 vgonel(vp, p);
1790 } else {
1791 vclean(vp, 0, p);
1792 vp->v_op = spec_vnodeop_p;
1793 insmntque(vp, (struct mount *) 0);
1794 }
1795 simple_lock(&mntvnode_slock);
1796 continue;
1797 }
1798#ifdef DIAGNOSTIC
1799 if (busyprt)
1800 vprint("vflush: busy vnode", vp);
1801#endif
1802 simple_unlock(&vp->v_interlock);
1803 busy++;
1804 }
1805 simple_unlock(&mntvnode_slock);
1806 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
1807 /*
1808 * If just the root vnode is busy, and if its refcount
1809 * is equal to `rootrefs', then go ahead and kill it.
1810 */
1811 simple_lock(&rootvp->v_interlock);
1812 KASSERT(busy > 0, ("vflush: not busy"));
1813 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
1814 if (busy == 1 && rootvp->v_usecount == rootrefs) {
1815 vgonel(rootvp, p);
1816 busy = 0;
1817 } else
1818 simple_unlock(&rootvp->v_interlock);
1819 }
1820 if (busy)
1821 return (EBUSY);
1822 for (; rootrefs > 0; rootrefs--)
1823 vrele(rootvp);
1824 return (0);
1825}
1826
1827/*
1828 * We do not want to recycle the vnode too quickly.
1829 *
1830 * XXX we can't move vp's around the nvnodelist without really screwing
1831 * up the efficiency of filesystem SYNC and friends. This code is
1832 * disabled until we fix the syncing code's scanning algorithm.
1833 */
1834static void
1835vlruvp(struct vnode *vp)
1836{
1837#if 0
1838 struct mount *mp;
1839
1840 if ((mp = vp->v_mount) != NULL) {
1841 simple_lock(&mntvnode_slock);
1842 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1843 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1844 simple_unlock(&mntvnode_slock);
1845 }
1846#endif
1847}
1848
1849/*
1850 * Disassociate the underlying file system from a vnode.
1851 */
1852static void
1853vclean(vp, flags, p)
1854 struct vnode *vp;
1855 int flags;
1856 struct proc *p;
1857{
1858 int active;
1859
1860 /*
1861 * Check to see if the vnode is in use. If so we have to reference it
1862 * before we clean it out so that its count cannot fall to zero and
1863 * generate a race against ourselves to recycle it.
1864 */
1865 if ((active = vp->v_usecount))
1866 vp->v_usecount++;
1867
1868 /*
1869 * Prevent the vnode from being recycled or brought into use while we
1870 * clean it out.
1871 */
1872 if (vp->v_flag & VXLOCK)
1873 panic("vclean: deadlock");
1874 vp->v_flag |= VXLOCK;
1875 vp->v_vxproc = curproc;
1876 /*
1877 * Even if the count is zero, the VOP_INACTIVE routine may still
1878 * have the object locked while it cleans it out. The VOP_LOCK
1879 * ensures that the VOP_INACTIVE routine is done with its work.
1880 * For active vnodes, it ensures that no other activity can
1881 * occur while the underlying object is being cleaned out.
1882 */
1883 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1884
1885 /*
1886 * Clean out any buffers associated with the vnode.
1887 */
1888 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1889
1890 VOP_DESTROYVOBJECT(vp);
1891
1892 /*
1893 * If purging an active vnode, it must be closed and
1894 * deactivated before being reclaimed. Note that the
1895 * VOP_INACTIVE will unlock the vnode.
1896 */
1897 if (active) {
1898 if (flags & DOCLOSE)
1899 VOP_CLOSE(vp, FNONBLOCK, NOCRED, p);
1900 VOP_INACTIVE(vp, p);
1901 } else {
1902 /*
1903 * Any other processes trying to obtain this lock must first
1904 * wait for VXLOCK to clear, then call the new lock operation.
1905 */
1906 VOP_UNLOCK(vp, 0, p);
1907 }
1908 /*
1909 * Reclaim the vnode.
1910 */
1911 if (VOP_RECLAIM(vp, p))
1912 panic("vclean: cannot reclaim");
1913
1914 if (active) {
1915 /*
1916 * Inline copy of vrele() since VOP_INACTIVE
1917 * has already been called.
1918 */
1919 simple_lock(&vp->v_interlock);
1920 if (--vp->v_usecount <= 0) {
1921#ifdef DIAGNOSTIC
1922 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
1923 vprint("vclean: bad ref count", vp);
1924 panic("vclean: ref cnt");
1925 }
1926#endif
1927 vfree(vp);
1928 }
1929 simple_unlock(&vp->v_interlock);
1930 }
1931
1932 cache_purge(vp);
1933 vp->v_vnlock = NULL;
1934
1935 if (VSHOULDFREE(vp))
1936 vfree(vp);
1937
1938 /*
1939 * Done with purge, notify sleepers of the grim news.
1940 */
1941 vp->v_op = dead_vnodeop_p;
1942 vn_pollgone(vp);
1943 vp->v_tag = VT_NON;
1944 vp->v_flag &= ~VXLOCK;
1945 vp->v_vxproc = NULL;
1946 if (vp->v_flag & VXWANT) {
1947 vp->v_flag &= ~VXWANT;
1948 wakeup((caddr_t) vp);
1949 }
1950}
1951
1952/*
1953 * Eliminate all activity associated with the requested vnode
1954 * and with all vnodes aliased to the requested vnode.
1955 */
1956int
1957vop_revoke(ap)
1958 struct vop_revoke_args /* {
1959 struct vnode *a_vp;
1960 int a_flags;
1961 } */ *ap;
1962{
1963 struct vnode *vp, *vq;
1964 dev_t dev;
1965
1966 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1967
1968 vp = ap->a_vp;
1969 /*
1970 * If a vgone (or vclean) is already in progress,
1971 * wait until it is done and return.
1972 */
1973 if (vp->v_flag & VXLOCK) {
1974 vp->v_flag |= VXWANT;
1975 simple_unlock(&vp->v_interlock);
1976 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1977 return (0);
1978 }
1979 dev = vp->v_rdev;
1980 for (;;) {
1981 simple_lock(&spechash_slock);
1982 vq = SLIST_FIRST(&dev->si_hlist);
1983 simple_unlock(&spechash_slock);
1984 if (!vq)
1985 break;
1986 vgone(vq);
1987 }
1988 return (0);
1989}
1990
1991/*
1992 * Recycle an unused vnode to the front of the free list.
1993 * Release the passed interlock if the vnode will be recycled.
1994 */
1995int
1996vrecycle(vp, inter_lkp, p)
1997 struct vnode *vp;
1998 struct simplelock *inter_lkp;
1999 struct proc *p;
2000{
2001
2002 simple_lock(&vp->v_interlock);
2003 if (vp->v_usecount == 0) {
2004 if (inter_lkp) {
2005 simple_unlock(inter_lkp);
2006 }
2007 vgonel(vp, p);
2008 return (1);
2009 }
2010 simple_unlock(&vp->v_interlock);
2011 return (0);
2012}
2013
2014/*
2015 * Eliminate all activity associated with a vnode
2016 * in preparation for reuse.
2017 */
2018void
2019vgone(vp)
2020 register struct vnode *vp;
2021{
2022 struct proc *p = curproc; /* XXX */
2023
2024 simple_lock(&vp->v_interlock);
2025 vgonel(vp, p);
2026}
2027
2028/*
2029 * vgone, with the vp interlock held.
2030 */
2031void
2032vgonel(vp, p)
2033 struct vnode *vp;
2034 struct proc *p;
2035{
2036 int s;
2037
2038 /*
2039 * If a vgone (or vclean) is already in progress,
2040 * wait until it is done and return.
2041 */
2042 if (vp->v_flag & VXLOCK) {
2043 vp->v_flag |= VXWANT;
2044 simple_unlock(&vp->v_interlock);
2045 tsleep((caddr_t)vp, PINOD, "vgone", 0);
2046 return;
2047 }
2048
2049 /*
2050 * Clean out the filesystem specific data.
2051 */
2052 vclean(vp, DOCLOSE, p);
2053 simple_lock(&vp->v_interlock);
2054
2055 /*
2056 * Delete from old mount point vnode list, if on one.
2057 */
2058 if (vp->v_mount != NULL)
2059 insmntque(vp, (struct mount *)0);
2060 /*
2061 * If special device, remove it from special device alias list
2062 * if it is on one.
2063 */
2064 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
2065 simple_lock(&spechash_slock);
2066 SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
2067 freedev(vp->v_rdev);
2068 simple_unlock(&spechash_slock);
2069 vp->v_rdev = NULL;
2070 }
2071
2072 /*
2073 * If it is on the freelist and not already at the head,
2074 * move it to the head of the list. The test of the
2075 * VDOOMED flag and the reference count of zero is because
2076 * it will be removed from the free list by getnewvnode,
2077 * but will not have its reference count incremented until
2078 * after calling vgone. If the reference count were
2079 * incremented first, vgone would (incorrectly) try to
2080 * close the previous instance of the underlying object.
2081 */
2082 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
2083 s = splbio();
2084 simple_lock(&vnode_free_list_slock);
2085 if (vp->v_flag & VFREE)
2086 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2087 else
2088 freevnodes++;
2089 vp->v_flag |= VFREE;
2090 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2091 simple_unlock(&vnode_free_list_slock);
2092 splx(s);
2093 }
2094
2095 vp->v_type = VBAD;
2096 simple_unlock(&vp->v_interlock);
2097}
2098
2099/*
2100 * Lookup a vnode by device number.
2101 */
2102int
2103vfinddev(dev, type, vpp)
2104 dev_t dev;
2105 enum vtype type;
2106 struct vnode **vpp;
2107{
2108 struct vnode *vp;
2109
2110 simple_lock(&spechash_slock);
2111 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2112 if (type == vp->v_type) {
2113 *vpp = vp;
2114 simple_unlock(&spechash_slock);
2115 return (1);
2116 }
2117 }
2118 simple_unlock(&spechash_slock);
2119 return (0);
2120}
2121
2122/*
2123 * Calculate the total number of references to a special device.
2124 */
2125int
2126vcount(vp)
2127 struct vnode *vp;
2128{
2129 struct vnode *vq;
2130 int count;
2131
2132 count = 0;
2133 simple_lock(&spechash_slock);
2134 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
2135 count += vq->v_usecount;
2136 simple_unlock(&spechash_slock);
2137 return (count);
2138}
2139
2140/*
2141 * Same as above, but using the dev_t as argument
2142 */
2143
2144int
2145count_dev(dev)
2146 dev_t dev;
2147{
2148 struct vnode *vp;
2149
2150 vp = SLIST_FIRST(&dev->si_hlist);
2151 if (vp == NULL)
2152 return (0);
2153 return(vcount(vp));
2154}
2155
2156/*
2157 * Print out a description of a vnode.
2158 */
2159static char *typename[] =
2160{"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2161
2162void
2163vprint(label, vp)
2164 char *label;
2165 struct vnode *vp;
2166{
2167 char buf[96];
2168
2169 if (label != NULL)
2170 printf("%s: %p: ", label, (void *)vp);
2171 else
2172 printf("%p: ", (void *)vp);
2173 printf("type %s, usecount %d, writecount %d, refcount %d,",
2174 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
2175 vp->v_holdcnt);
2176 buf[0] = '\0';
2177 if (vp->v_flag & VROOT)
2178 strcat(buf, "|VROOT");
2179 if (vp->v_flag & VTEXT)
2180 strcat(buf, "|VTEXT");
2181 if (vp->v_flag & VSYSTEM)
2182 strcat(buf, "|VSYSTEM");
2183 if (vp->v_flag & VXLOCK)
2184 strcat(buf, "|VXLOCK");
2185 if (vp->v_flag & VXWANT)
2186 strcat(buf, "|VXWANT");
2187 if (vp->v_flag & VBWAIT)
2188 strcat(buf, "|VBWAIT");
2189 if (vp->v_flag & VDOOMED)
2190 strcat(buf, "|VDOOMED");
2191 if (vp->v_flag & VFREE)
2192 strcat(buf, "|VFREE");
2193 if (vp->v_flag & VOBJBUF)
2194 strcat(buf, "|VOBJBUF");
2195 if (buf[0] != '\0')
2196 printf(" flags (%s)", &buf[1]);
2197 if (vp->v_data == NULL) {
2198 printf("\n");
2199 } else {
2200 printf("\n\t");
2201 VOP_PRINT(vp);
2202 }
2203}
2204
2205#ifdef DDB
2206#include <ddb/ddb.h>
2207/*
2208 * List all of the locked vnodes in the system.
2209 * Called when debugging the kernel.
2210 */
2211DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2212{
2213 struct proc *p = curproc; /* XXX */
2214 struct mount *mp, *nmp;
2215 struct vnode *vp;
2216
2217 printf("Locked vnodes\n");
2218 simple_lock(&mountlist_slock);
2219 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2220 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2221 nmp = TAILQ_NEXT(mp, mnt_list);
2222 continue;
2223 }
2224 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2225 if (VOP_ISLOCKED(vp, NULL))
2226 vprint((char *)0, vp);
2227 }
2228 simple_lock(&mountlist_slock);
2229 nmp = TAILQ_NEXT(mp, mnt_list);
2230 vfs_unbusy(mp, p);
2231 }
2232 simple_unlock(&mountlist_slock);
2233}
2234#endif
2235
2236/*
2237 * Top level filesystem related information gathering.
2238 */
2239static int sysctl_ovfs_conf __P((SYSCTL_HANDLER_ARGS));
2240
2241static int
2242vfs_sysctl(SYSCTL_HANDLER_ARGS)
2243{
2244 int *name = (int *)arg1 - 1; /* XXX */
2245 u_int namelen = arg2 + 1; /* XXX */
2246 struct vfsconf *vfsp;
2247
2248#if 1 || defined(COMPAT_PRELITE2)
2249 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2250 if (namelen == 1)
2251 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2252#endif
2253
2254#ifdef notyet
2255 /* all sysctl names at this level are at least name and field */
2256 if (namelen < 2)
2257 return (ENOTDIR); /* overloaded */
2258 if (name[0] != VFS_GENERIC) {
2259 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2260 if (vfsp->vfc_typenum == name[0])
2261 break;
2262 if (vfsp == NULL)
2263 return (EOPNOTSUPP);
2264 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2265 oldp, oldlenp, newp, newlen, p));
2266 }
2267#endif
2268 switch (name[1]) {
2269 case VFS_MAXTYPENUM:
2270 if (namelen != 2)
2271 return (ENOTDIR);
2272 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2273 case VFS_CONF:
2274 if (namelen != 3)
2275 return (ENOTDIR); /* overloaded */
2276 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2277 if (vfsp->vfc_typenum == name[2])
2278 break;
2279 if (vfsp == NULL)
2280 return (EOPNOTSUPP);
2281 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2282 }
2283 return (EOPNOTSUPP);
2284}
2285
2286SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2287 "Generic filesystem");
2288
2289#if 1 || defined(COMPAT_PRELITE2)
2290
2291static int
2292sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2293{
2294 int error;
2295 struct vfsconf *vfsp;
2296 struct ovfsconf ovfs;
2297
2298 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2299 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2300 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2301 ovfs.vfc_index = vfsp->vfc_typenum;
2302 ovfs.vfc_refcount = vfsp->vfc_refcount;
2303 ovfs.vfc_flags = vfsp->vfc_flags;
2304 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2305 if (error)
2306 return error;
2307 }
2308 return 0;
2309}
2310
2311#endif /* 1 || COMPAT_PRELITE2 */
2312
2313#if 0
2314#define KINFO_VNODESLOP 10
2315/*
2316 * Dump vnode list (via sysctl).
2317 * Copyout address of vnode followed by vnode.
2318 */
2319/* ARGSUSED */
2320static int
2321sysctl_vnode(SYSCTL_HANDLER_ARGS)
2322{
2323 struct proc *p = curproc; /* XXX */
2324 struct mount *mp, *nmp;
2325 struct vnode *nvp, *vp;
2326 int error;
2327
2328#define VPTRSZ sizeof (struct vnode *)
2329#define VNODESZ sizeof (struct vnode)
2330
2331 req->lock = 0;
2332 if (!req->oldptr) /* Make an estimate */
2333 return (SYSCTL_OUT(req, 0,
2334 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2335
2336 simple_lock(&mountlist_slock);
2337 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2338 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2339 nmp = TAILQ_NEXT(mp, mnt_list);
2340 continue;
2341 }
2342again:
2343 simple_lock(&mntvnode_slock);
2344 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2345 vp != NULL;
2346 vp = nvp) {
2347 /*
2348 * Check that the vp is still associated with
2349 * this filesystem. RACE: could have been
2350 * recycled onto the same filesystem.
2351 */
2352 if (vp->v_mount != mp) {
2353 simple_unlock(&mntvnode_slock);
2354 goto again;
2355 }
2356 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2357 simple_unlock(&mntvnode_slock);
2358 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2359 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2360 return (error);
2361 simple_lock(&mntvnode_slock);
2362 }
2363 simple_unlock(&mntvnode_slock);
2364 simple_lock(&mountlist_slock);
2365 nmp = TAILQ_NEXT(mp, mnt_list);
2366 vfs_unbusy(mp, p);
2367 }
2368 simple_unlock(&mountlist_slock);
2369
2370 return (0);
2371}
2372#endif
2373
2374/*
2375 * XXX
2376 * Exporting the vnode list on large systems causes them to crash.
2377 * Exporting the vnode list on medium systems causes sysctl to coredump.
2378 */
2379#if 0
2380SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2381 0, 0, sysctl_vnode, "S,vnode", "");
2382#endif
2383
2384/*
2385 * Check to see if a filesystem is mounted on a block device.
2386 */
2387int
2388vfs_mountedon(vp)
2389 struct vnode *vp;
2390{
2391
2392 if (vp->v_specmountpoint != NULL)
2393 return (EBUSY);
2394 return (0);
2395}
2396
2397/*
2398 * Unmount all filesystems. The list is traversed in reverse order
2399 * of mounting to avoid dependencies.
2400 */
2401void
2402vfs_unmountall()
2403{
2404 struct mount *mp;
2405 struct proc *p;
2406 int error;
2407
2408 if (curproc != NULL)
2409 p = curproc;
2410 else
2411 p = initproc; /* XXX XXX should this be proc0? */
2412 /*
2413 * Since this only runs when rebooting, it is not interlocked.
2414 */
2415 while(!TAILQ_EMPTY(&mountlist)) {
2416 mp = TAILQ_LAST(&mountlist, mntlist);
2417 error = dounmount(mp, MNT_FORCE, p);
2418 if (error) {
2419 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2420 printf("unmount of %s failed (",
2421 mp->mnt_stat.f_mntonname);
2422 if (error == EBUSY)
2423 printf("BUSY)\n");
2424 else
2425 printf("%d)\n", error);
2426 } else {
2427 /* The unmount has removed mp from the mountlist */
2428 }
2429 }
2430}
2431
2432/*
2433 * Build hash lists of net addresses and hang them off the mount point.
2434 * Called by ufs_mount() to set up the lists of export addresses.
2435 */
2436static int
2437vfs_hang_addrlist(mp, nep, argp)
2438 struct mount *mp;
2439 struct netexport *nep;
2440 struct export_args *argp;
2441{
2442 register struct netcred *np;
2443 register struct radix_node_head *rnh;
2444 register int i;
2445 struct radix_node *rn;
2446 struct sockaddr *saddr, *smask = 0;
2447 struct domain *dom;
2448 int error;
2449
2450 if (argp->ex_addrlen == 0) {
2451 if (mp->mnt_flag & MNT_DEFEXPORTED)
2452 return (EPERM);
2453 np = &nep->ne_defexported;
2454 np->netc_exflags = argp->ex_flags;
2455 np->netc_anon = argp->ex_anon;
2456 np->netc_anon.cr_ref = 1;
2457 mp->mnt_flag |= MNT_DEFEXPORTED;
2458 return (0);
2459 }
2460
2461 if (argp->ex_addrlen > MLEN)
2462 return (EINVAL);
2463
2464 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2465 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2466 bzero((caddr_t) np, i);
2467 saddr = (struct sockaddr *) (np + 1);
2468 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2469 goto out;
2470 if (saddr->sa_len > argp->ex_addrlen)
2471 saddr->sa_len = argp->ex_addrlen;
2472 if (argp->ex_masklen) {
2473 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2474 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2475 if (error)
2476 goto out;
2477 if (smask->sa_len > argp->ex_masklen)
2478 smask->sa_len = argp->ex_masklen;
2479 }
2480 i = saddr->sa_family;
2481 if ((rnh = nep->ne_rtable[i]) == 0) {
2482 /*
2483 * Seems silly to initialize every AF when most are not used,
2484 * do so on demand here
2485 */
2486 for (dom = domains; dom; dom = dom->dom_next)
2487 if (dom->dom_family == i && dom->dom_rtattach) {
2488 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2489 dom->dom_rtoffset);
2490 break;
2491 }
2492 if ((rnh = nep->ne_rtable[i]) == 0) {
2493 error = ENOBUFS;
2494 goto out;
2495 }
2496 }
2497 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2498 np->netc_rnodes);
2499 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2500 error = EPERM;
2501 goto out;
2502 }
2503 np->netc_exflags = argp->ex_flags;
2504 np->netc_anon = argp->ex_anon;
2505 np->netc_anon.cr_ref = 1;
2506 return (0);
2507out:
2508 free(np, M_NETADDR);
2509 return (error);
2510}
2511
2512/* ARGSUSED */
2513static int
2514vfs_free_netcred(rn, w)
2515 struct radix_node *rn;
2516 void *w;
2517{
2518 register struct radix_node_head *rnh = (struct radix_node_head *) w;
2519
2520 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2521 free((caddr_t) rn, M_NETADDR);
2522 return (0);
2523}
2524
2525/*
2526 * Free the net address hash lists that are hanging off the mount points.
2527 */
2528static void
2529vfs_free_addrlist(nep)
2530 struct netexport *nep;
2531{
2532 register int i;
2533 register struct radix_node_head *rnh;
2534
2535 for (i = 0; i <= AF_MAX; i++)
2536 if ((rnh = nep->ne_rtable[i])) {
2537 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2538 (caddr_t) rnh);
2539 free((caddr_t) rnh, M_RTABLE);
2540 nep->ne_rtable[i] = 0;
2541 }
2542}
2543
2544int
2545vfs_export(mp, nep, argp)
2546 struct mount *mp;
2547 struct netexport *nep;
2548 struct export_args *argp;
2549{
2550 int error;
2551
2552 if (argp->ex_flags & MNT_DELEXPORT) {
2553 if (mp->mnt_flag & MNT_EXPUBLIC) {
2554 vfs_setpublicfs(NULL, NULL, NULL);
2555 mp->mnt_flag &= ~MNT_EXPUBLIC;
2556 }
2557 vfs_free_addrlist(nep);
2558 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2559 }
2560 if (argp->ex_flags & MNT_EXPORTED) {
2561 if (argp->ex_flags & MNT_EXPUBLIC) {
2562 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2563 return (error);
2564 mp->mnt_flag |= MNT_EXPUBLIC;
2565 }
2566 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2567 return (error);
2568 mp->mnt_flag |= MNT_EXPORTED;
2569 }
2570 return (0);
2571}
2572
2573
2574/*
2575 * Set the publicly exported filesystem (WebNFS). Currently, only
2576 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2577 */
2578int
2579vfs_setpublicfs(mp, nep, argp)
2580 struct mount *mp;
2581 struct netexport *nep;
2582 struct export_args *argp;
2583{
2584 int error;
2585 struct vnode *rvp;
2586 char *cp;
2587
2588 /*
2589 * mp == NULL -> invalidate the current info, the FS is
2590 * no longer exported. May be called from either vfs_export
2591 * or unmount, so check if it hasn't already been done.
2592 */
2593 if (mp == NULL) {
2594 if (nfs_pub.np_valid) {
2595 nfs_pub.np_valid = 0;
2596 if (nfs_pub.np_index != NULL) {
2597 FREE(nfs_pub.np_index, M_TEMP);
2598 nfs_pub.np_index = NULL;
2599 }
2600 }
2601 return (0);
2602 }
2603
2604 /*
2605 * Only one allowed at a time.
2606 */
2607 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2608 return (EBUSY);
2609
2610 /*
2611 * Get real filehandle for root of exported FS.
2612 */
2613 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2614 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2615
2616 if ((error = VFS_ROOT(mp, &rvp)))
2617 return (error);
2618
2619 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2620 return (error);
2621
2622 vput(rvp);
2623
2624 /*
2625 * If an indexfile was specified, pull it in.
2626 */
2627 if (argp->ex_indexfile != NULL) {
2628 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2629 M_WAITOK);
2630 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2631 MAXNAMLEN, (size_t *)0);
2632 if (!error) {
2633 /*
2634 * Check for illegal filenames.
2635 */
2636 for (cp = nfs_pub.np_index; *cp; cp++) {
2637 if (*cp == '/') {
2638 error = EINVAL;
2639 break;
2640 }
2641 }
2642 }
2643 if (error) {
2644 FREE(nfs_pub.np_index, M_TEMP);
2645 return (error);
2646 }
2647 }
2648
2649 nfs_pub.np_mount = mp;
2650 nfs_pub.np_valid = 1;
2651 return (0);
2652}
2653
2654struct netcred *
2655vfs_export_lookup(mp, nep, nam)
2656 register struct mount *mp;
2657 struct netexport *nep;
2658 struct sockaddr *nam;
2659{
2660 register struct netcred *np;
2661 register struct radix_node_head *rnh;
2662 struct sockaddr *saddr;
2663
2664 np = NULL;
2665 if (mp->mnt_flag & MNT_EXPORTED) {
2666 /*
2667 * Lookup in the export list first.
2668 */
2669 if (nam != NULL) {
2670 saddr = nam;
2671 rnh = nep->ne_rtable[saddr->sa_family];
2672 if (rnh != NULL) {
2673 np = (struct netcred *)
2674 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2675 rnh);
2676 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2677 np = NULL;
2678 }
2679 }
2680 /*
2681 * If no address match, use the default if it exists.
2682 */
2683 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2684 np = &nep->ne_defexported;
2685 }
2686 return (np);
2687}
2688
2689/*
2690 * perform msync on all vnodes under a mount point
2691 * the mount point must be locked.
2692 */
2693void
2694vfs_msync(struct mount *mp, int flags)
2695{
2696 struct vnode *vp, *nvp;
2697 struct vm_object *obj;
2698 int tries;
2699
2700 tries = 5;
2701 simple_lock(&mntvnode_slock);
2702loop:
2703 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
2704 if (vp->v_mount != mp) {
2705 if (--tries > 0)
2706 goto loop;
2707 break;
2708 }
2709 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2710
2711 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2712 continue;
2713
2714 /*
2715 * There could be hundreds of thousands of vnodes, we cannot
2716 * afford to do anything heavy-weight until we have a fairly
2717 * good indication that there is something to do.
2718 */
2719 if ((vp->v_flag & VOBJDIRTY) &&
2720 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2721 simple_unlock(&mntvnode_slock);
2722 if (!vget(vp,
2723 LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2724 if (VOP_GETVOBJECT(vp, &obj) == 0) {
2725 vm_object_page_clean(obj, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2726 }
2727 vput(vp);
2728 }
2729 simple_lock(&mntvnode_slock);
2730 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
2731 if (--tries > 0)
2732 goto loop;
2733 break;
2734 }
2735 }
2736 }
2737 simple_unlock(&mntvnode_slock);
2738}
2739
2740/*
2741 * Create the VM object needed for VMIO and mmap support. This
2742 * is done for all VREG files in the system. Some filesystems might
2743 * afford the additional metadata buffering capability of the
2744 * VMIO code by making the device node be VMIO mode also.
2745 *
2746 * vp must be locked when vfs_object_create is called.
2747 */
2748int
2749vfs_object_create(vp, p, cred)
2750 struct vnode *vp;
2751 struct proc *p;
2752 struct ucred *cred;
2753{
2754 return (VOP_CREATEVOBJECT(vp, cred, p));
2755}
2756
2757void
2758vfree(vp)
2759 struct vnode *vp;
2760{
2761 int s;
2762
2763 s = splbio();
2764 simple_lock(&vnode_free_list_slock);
2765 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free"));
2766 if (vp->v_flag & VAGE) {
2767 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2768 } else {
2769 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2770 }
2771 freevnodes++;
2772 simple_unlock(&vnode_free_list_slock);
2773 vp->v_flag &= ~VAGE;
2774 vp->v_flag |= VFREE;
2775 splx(s);
2776}
2777
2778void
2779vbusy(vp)
2780 struct vnode *vp;
2781{
2782 int s;
2783
2784 s = splbio();
2785 simple_lock(&vnode_free_list_slock);
2786 KASSERT((vp->v_flag & VFREE) != 0, ("vnode not free"));
2787 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2788 freevnodes--;
2789 simple_unlock(&vnode_free_list_slock);
2790 vp->v_flag &= ~(VFREE|VAGE);
2791 splx(s);
2792}
2793
2794/*
2795 * Record a process's interest in events which might happen to
2796 * a vnode. Because poll uses the historic select-style interface
2797 * internally, this routine serves as both the ``check for any
2798 * pending events'' and the ``record my interest in future events''
2799 * functions. (These are done together, while the lock is held,
2800 * to avoid race conditions.)
2801 */
2802int
2803vn_pollrecord(vp, p, events)
2804 struct vnode *vp;
2805 struct proc *p;
2806 short events;
2807{
2808 simple_lock(&vp->v_pollinfo.vpi_lock);
2809 if (vp->v_pollinfo.vpi_revents & events) {
2810 /*
2811 * This leaves events we are not interested
2812 * in available for the other process which
2813 * which presumably had requested them
2814 * (otherwise they would never have been
2815 * recorded).
2816 */
2817 events &= vp->v_pollinfo.vpi_revents;
2818 vp->v_pollinfo.vpi_revents &= ~events;
2819
2820 simple_unlock(&vp->v_pollinfo.vpi_lock);
2821 return events;
2822 }
2823 vp->v_pollinfo.vpi_events |= events;
2824 selrecord(p, &vp->v_pollinfo.vpi_selinfo);
2825 simple_unlock(&vp->v_pollinfo.vpi_lock);
2826 return 0;
2827}
2828
2829/*
2830 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2831 * it is possible for us to miss an event due to race conditions, but
2832 * that condition is expected to be rare, so for the moment it is the
2833 * preferred interface.
2834 */
2835void
2836vn_pollevent(vp, events)
2837 struct vnode *vp;
2838 short events;
2839{
2840 simple_lock(&vp->v_pollinfo.vpi_lock);
2841 if (vp->v_pollinfo.vpi_events & events) {
2842 /*
2843 * We clear vpi_events so that we don't
2844 * call selwakeup() twice if two events are
2845 * posted before the polling process(es) is
2846 * awakened. This also ensures that we take at
2847 * most one selwakeup() if the polling process
2848 * is no longer interested. However, it does
2849 * mean that only one event can be noticed at
2850 * a time. (Perhaps we should only clear those
2851 * event bits which we note?) XXX
2852 */
2853 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2854 vp->v_pollinfo.vpi_revents |= events;
2855 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2856 }
2857 simple_unlock(&vp->v_pollinfo.vpi_lock);
2858}
2859
2860/*
2861 * Wake up anyone polling on vp because it is being revoked.
2862 * This depends on dead_poll() returning POLLHUP for correct
2863 * behavior.
2864 */
2865void
2866vn_pollgone(vp)
2867 struct vnode *vp;
2868{
2869 simple_lock(&vp->v_pollinfo.vpi_lock);
2870 if (vp->v_pollinfo.vpi_events) {
2871 vp->v_pollinfo.vpi_events = 0;
2872 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2873 }
2874 simple_unlock(&vp->v_pollinfo.vpi_lock);
2875}
2876
2877
2878
2879/*
2880 * Routine to create and manage a filesystem syncer vnode.
2881 */
2882#define sync_close ((int (*) __P((struct vop_close_args *)))nullop)
2883static int sync_fsync __P((struct vop_fsync_args *));
2884static int sync_inactive __P((struct vop_inactive_args *));
2885static int sync_reclaim __P((struct vop_reclaim_args *));
2886#define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock)
2887#define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
2888static int sync_print __P((struct vop_print_args *));
2889#define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2890
2891static vop_t **sync_vnodeop_p;
2892static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2893 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2894 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2895 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2896 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2897 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2898 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2899 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2900 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2901 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2902 { NULL, NULL }
2903};
2904static struct vnodeopv_desc sync_vnodeop_opv_desc =
2905 { &sync_vnodeop_p, sync_vnodeop_entries };
2906
2907VNODEOP_SET(sync_vnodeop_opv_desc);
2908
2909/*
2910 * Create a new filesystem syncer vnode for the specified mount point.
2911 */
2912int
2913vfs_allocate_syncvnode(mp)
2914 struct mount *mp;
2915{
2916 struct vnode *vp;
2917 static long start, incr, next;
2918 int error;
2919
2920 /* Allocate a new vnode */
2921 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2922 mp->mnt_syncer = NULL;
2923 return (error);
2924 }
2925 vp->v_type = VNON;
2926 /*
2927 * Place the vnode onto the syncer worklist. We attempt to
2928 * scatter them about on the list so that they will go off
2929 * at evenly distributed times even if all the filesystems
2930 * are mounted at once.
2931 */
2932 next += incr;
2933 if (next == 0 || next > syncer_maxdelay) {
2934 start /= 2;
2935 incr /= 2;
2936 if (start == 0) {
2937 start = syncer_maxdelay / 2;
2938 incr = syncer_maxdelay;
2939 }
2940 next = start;
2941 }
2942 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2943 mp->mnt_syncer = vp;
2944 return (0);
2945}
2946
2947/*
2948 * Do a lazy sync of the filesystem.
2949 */
2950static int
2951sync_fsync(ap)
2952 struct vop_fsync_args /* {
2953 struct vnode *a_vp;
2954 struct ucred *a_cred;
2955 int a_waitfor;
2956 struct proc *a_p;
2957 } */ *ap;
2958{
2959 struct vnode *syncvp = ap->a_vp;
2960 struct mount *mp = syncvp->v_mount;
2961 struct proc *p = ap->a_p;
2962 int asyncflag;
2963
2964 /*
2965 * We only need to do something if this is a lazy evaluation.
2966 */
2967 if (ap->a_waitfor != MNT_LAZY)
2968 return (0);
2969
2970 /*
2971 * Move ourselves to the back of the sync list.
2972 */
2973 vn_syncer_add_to_worklist(syncvp, syncdelay);
2974
2975 /*
2976 * Walk the list of vnodes pushing all that are dirty and
2977 * not already on the sync list.
2978 */
2979 simple_lock(&mountlist_slock);
2980 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, p) != 0) {
2981 simple_unlock(&mountlist_slock);
2982 return (0);
2983 }
2984 asyncflag = mp->mnt_flag & MNT_ASYNC;
2985 mp->mnt_flag &= ~MNT_ASYNC;
2986 vfs_msync(mp, MNT_NOWAIT);
2987 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p);
2988 if (asyncflag)
2989 mp->mnt_flag |= MNT_ASYNC;
2990 vfs_unbusy(mp, p);
2991 return (0);
2992}
2993
2994/*
2995 * The syncer vnode is no referenced.
2996 */
2997static int
2998sync_inactive(ap)
2999 struct vop_inactive_args /* {
3000 struct vnode *a_vp;
3001 struct proc *a_p;
3002 } */ *ap;
3003{
3004
3005 vgone(ap->a_vp);
3006 return (0);
3007}
3008
3009/*
3010 * The syncer vnode is no longer needed and is being decommissioned.
3011 *
3012 * Modifications to the worklist must be protected at splbio().
3013 */
3014static int
3015sync_reclaim(ap)
3016 struct vop_reclaim_args /* {
3017 struct vnode *a_vp;
3018 } */ *ap;
3019{
3020 struct vnode *vp = ap->a_vp;
3021 int s;
3022
3023 s = splbio();
3024 vp->v_mount->mnt_syncer = NULL;
3025 if (vp->v_flag & VONWORKLST) {
3026 LIST_REMOVE(vp, v_synclist);
3027 vp->v_flag &= ~VONWORKLST;
3028 }
3029 splx(s);
3030
3031 return (0);
3032}
3033
3034/*
3035 * Print out a syncer vnode.
3036 */
3037static int
3038sync_print(ap)
3039 struct vop_print_args /* {
3040 struct vnode *a_vp;
3041 } */ *ap;
3042{
3043 struct vnode *vp = ap->a_vp;
3044
3045 printf("syncer vnode");
3046 if (vp->v_vnlock != NULL)
3047 lockmgr_printinfo(vp->v_vnlock);
3048 printf("\n");
3049 return (0);
3050}
3051
3052/*
3053 * extract the dev_t from a VBLK or VCHR
3054 */
3055dev_t
3056vn_todev(vp)
3057 struct vnode *vp;
3058{
3059 if (vp->v_type != VBLK && vp->v_type != VCHR)
3060 return (NODEV);
3061 return (vp->v_rdev);
3062}
3063
3064/*
3065 * Check if vnode represents a disk device
3066 */
3067int
3068vn_isdisk(vp, errp)
3069 struct vnode *vp;
3070 int *errp;
3071{
3072 if (vp->v_type != VBLK && vp->v_type != VCHR) {
3073 if (errp != NULL)
3074 *errp = ENOTBLK;
3075 return (0);
3076 }
3077 if (vp->v_rdev == NULL) {
3078 if (errp != NULL)
3079 *errp = ENXIO;
3080 return (0);
3081 }
3082 if (!devsw(vp->v_rdev)) {
3083 if (errp != NULL)
3084 *errp = ENXIO;
3085 return (0);
3086 }
3087 if (!(devsw(vp->v_rdev)->d_flags & D_DISK)) {
3088 if (errp != NULL)
3089 *errp = ENOTBLK;
3090 return (0);
3091 }
3092 if (errp != NULL)
3093 *errp = 0;
3094 return (1);
3095}
3096
3097void
3098NDFREE(ndp, flags)
3099 struct nameidata *ndp;
3100 const uint flags;
3101{
3102 if (!(flags & NDF_NO_FREE_PNBUF) &&
3103 (ndp->ni_cnd.cn_flags & HASBUF)) {
3104 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3105 ndp->ni_cnd.cn_flags &= ~HASBUF;
3106 }
3107 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3108 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
3109 ndp->ni_dvp != ndp->ni_vp)
3110 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_proc);
3111 if (!(flags & NDF_NO_DVP_RELE) &&
3112 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
3113 vrele(ndp->ni_dvp);
3114 ndp->ni_dvp = NULL;
3115 }
3116 if (!(flags & NDF_NO_VP_UNLOCK) &&
3117 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
3118 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_proc);
3119 if (!(flags & NDF_NO_VP_RELE) &&
3120 ndp->ni_vp) {
3121 vrele(ndp->ni_vp);
3122 ndp->ni_vp = NULL;
3123 }
3124 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3125 (ndp->ni_cnd.cn_flags & SAVESTART)) {
3126 vrele(ndp->ni_startdir);
3127 ndp->ni_startdir = NULL;
3128 }
3129}