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