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