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