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