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