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