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