FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_subr.c
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: releng/5.1/sys/kern/vfs_subr.c 115266 2003-05-23 19:54:02Z alc $
40 */
41
42 /*
43 * External virtual filesystem routines
44 */
45 #include "opt_ddb.h"
46 #include "opt_mac.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/bio.h>
51 #include <sys/buf.h>
52 #include <sys/conf.h>
53 #include <sys/eventhandler.h>
54 #include <sys/extattr.h>
55 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/kthread.h>
58 #include <sys/mac.h>
59 #include <sys/malloc.h>
60 #include <sys/mount.h>
61 #include <sys/namei.h>
62 #include <sys/stat.h>
63 #include <sys/sysctl.h>
64 #include <sys/syslog.h>
65 #include <sys/vmmeter.h>
66 #include <sys/vnode.h>
67
68 #include <vm/vm.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_extern.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_kern.h>
75 #include <vm/uma.h>
76
77 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
78
79 static void addalias(struct vnode *vp, dev_t nvp_rdev);
80 static void insmntque(struct vnode *vp, struct mount *mp);
81 static void vclean(struct vnode *vp, int flags, struct thread *td);
82 static void vlruvp(struct vnode *vp);
83 static int flushbuflist(struct buf *blist, int flags, struct vnode *vp,
84 int slpflag, int slptimeo, int *errorp);
85 static int vcanrecycle(struct vnode *vp, struct mount **vnmpp);
86
87
88 /*
89 * Number of vnodes in existence. Increased whenever getnewvnode()
90 * allocates a new vnode, never decreased.
91 */
92 static unsigned long numvnodes;
93
94 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
95
96 /*
97 * Conversion tables for conversion from vnode types to inode formats
98 * and back.
99 */
100 enum vtype iftovt_tab[16] = {
101 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
102 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
103 };
104 int vttoif_tab[9] = {
105 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
106 S_IFSOCK, S_IFIFO, S_IFMT,
107 };
108
109 /*
110 * List of vnodes that are ready for recycling.
111 */
112 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
113
114 /*
115 * Minimum number of free vnodes. If there are fewer than this free vnodes,
116 * getnewvnode() will return a newly allocated vnode.
117 */
118 static u_long wantfreevnodes = 25;
119 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
120 /* Number of vnodes in the free list. */
121 static u_long freevnodes;
122 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
123
124 /*
125 * Various variables used for debugging the new implementation of
126 * reassignbuf().
127 * XXX these are probably of (very) limited utility now.
128 */
129 static int reassignbufcalls;
130 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
131 static int nameileafonly;
132 SYSCTL_INT(_vfs, OID_AUTO, nameileafonly, CTLFLAG_RW, &nameileafonly, 0, "");
133
134 /*
135 * Cache for the mount type id assigned to NFS. This is used for
136 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
137 */
138 int nfs_mount_type = -1;
139
140 /* To keep more than one thread at a time from running vfs_getnewfsid */
141 static struct mtx mntid_mtx;
142
143 /*
144 * Lock for any access to the following:
145 * vnode_free_list
146 * numvnodes
147 * freevnodes
148 */
149 static struct mtx vnode_free_list_mtx;
150
151 /*
152 * For any iteration/modification of dev->si_hlist (linked through
153 * v_specnext)
154 */
155 static struct mtx spechash_mtx;
156
157 /* Publicly exported FS */
158 struct nfs_public nfs_pub;
159
160 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
161 static uma_zone_t vnode_zone;
162 static uma_zone_t vnodepoll_zone;
163
164 /* Set to 1 to print out reclaim of active vnodes */
165 int prtactive;
166
167 /*
168 * The workitem queue.
169 *
170 * It is useful to delay writes of file data and filesystem metadata
171 * for tens of seconds so that quickly created and deleted files need
172 * not waste disk bandwidth being created and removed. To realize this,
173 * we append vnodes to a "workitem" queue. When running with a soft
174 * updates implementation, most pending metadata dependencies should
175 * not wait for more than a few seconds. Thus, mounted on block devices
176 * are delayed only about a half the time that file data is delayed.
177 * Similarly, directory updates are more critical, so are only delayed
178 * about a third the time that file data is delayed. Thus, there are
179 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
180 * one each second (driven off the filesystem syncer process). The
181 * syncer_delayno variable indicates the next queue that is to be processed.
182 * Items that need to be processed soon are placed in this queue:
183 *
184 * syncer_workitem_pending[syncer_delayno]
185 *
186 * A delay of fifteen seconds is done by placing the request fifteen
187 * entries later in the queue:
188 *
189 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
190 *
191 */
192 static int syncer_delayno;
193 static long syncer_mask;
194 LIST_HEAD(synclist, vnode);
195 static struct synclist *syncer_workitem_pending;
196 /*
197 * The sync_mtx protects:
198 * vp->v_synclist
199 * syncer_delayno
200 * syncer_workitem_pending
201 * rushjob
202 */
203 static struct mtx sync_mtx;
204
205 #define SYNCER_MAXDELAY 32
206 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
207 static int syncdelay = 30; /* max time to delay syncing data */
208 static int filedelay = 30; /* time to delay syncing files */
209 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
210 static int dirdelay = 29; /* time to delay syncing directories */
211 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
212 static int metadelay = 28; /* time to delay syncing metadata */
213 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
214 static int rushjob; /* number of slots to run ASAP */
215 static int stat_rush_requests; /* number of times I/O speeded up */
216 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
217
218 /*
219 * Number of vnodes we want to exist at any one time. This is mostly used
220 * to size hash tables in vnode-related code. It is normally not used in
221 * getnewvnode(), as wantfreevnodes is normally nonzero.)
222 *
223 * XXX desiredvnodes is historical cruft and should not exist.
224 */
225 int desiredvnodes;
226 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
227 &desiredvnodes, 0, "Maximum number of vnodes");
228 static int minvnodes;
229 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
230 &minvnodes, 0, "Minimum number of vnodes");
231 static int vnlru_nowhere;
232 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0,
233 "Number of times the vnlru process ran without success");
234
235 /* Hook for calling soft updates */
236 int (*softdep_process_worklist_hook)(struct mount *);
237
238 /*
239 * This only exists to supress warnings from unlocked specfs accesses. It is
240 * no longer ok to have an unlocked VFS.
241 */
242 #define IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD)
243
244 /* Print lock violations */
245 int vfs_badlock_print = 1;
246
247 /* Panic on violation */
248 int vfs_badlock_panic = 1;
249
250 /* Check for interlock across VOPs */
251 int vfs_badlock_mutex = 1;
252
253 static void
254 vfs_badlock(char *msg, char *str, struct vnode *vp)
255 {
256 if (vfs_badlock_print)
257 printf("%s: %p %s\n", str, vp, msg);
258 if (vfs_badlock_panic)
259 Debugger("Lock violation.\n");
260 }
261
262 void
263 assert_vi_unlocked(struct vnode *vp, char *str)
264 {
265 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
266 vfs_badlock("interlock is locked but should not be", str, vp);
267 }
268
269 void
270 assert_vi_locked(struct vnode *vp, char *str)
271 {
272 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
273 vfs_badlock("interlock is not locked but should be", str, vp);
274 }
275
276 void
277 assert_vop_locked(struct vnode *vp, char *str)
278 {
279 if (vp && !IGNORE_LOCK(vp) && !VOP_ISLOCKED(vp, NULL))
280 vfs_badlock("is not locked but should be", str, vp);
281 }
282
283 void
284 assert_vop_unlocked(struct vnode *vp, char *str)
285 {
286 if (vp && !IGNORE_LOCK(vp) &&
287 VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE)
288 vfs_badlock("is locked but should not be", str, vp);
289 }
290
291 void
292 assert_vop_elocked(struct vnode *vp, char *str)
293 {
294 if (vp && !IGNORE_LOCK(vp) &&
295 VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE)
296 vfs_badlock("is not exclusive locked but should be", str, vp);
297 }
298
299 void
300 assert_vop_elocked_other(struct vnode *vp, char *str)
301 {
302 if (vp && !IGNORE_LOCK(vp) &&
303 VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER)
304 vfs_badlock("is not exclusive locked by another thread",
305 str, vp);
306 }
307
308 void
309 assert_vop_slocked(struct vnode *vp, char *str)
310 {
311 if (vp && !IGNORE_LOCK(vp) &&
312 VOP_ISLOCKED(vp, curthread) != LK_SHARED)
313 vfs_badlock("is not locked shared but should be", str, vp);
314 }
315
316 void
317 vop_rename_pre(void *ap)
318 {
319 struct vop_rename_args *a = ap;
320
321 if (a->a_tvp)
322 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
323 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
324 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
325 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
326
327 /* Check the source (from) */
328 if (a->a_tdvp != a->a_fdvp)
329 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked.\n");
330 if (a->a_tvp != a->a_fvp)
331 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: tvp locked.\n");
332
333 /* Check the target */
334 if (a->a_tvp)
335 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked.\n");
336
337 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked.\n");
338 }
339
340 void
341 vop_strategy_pre(void *ap)
342 {
343 struct vop_strategy_args *a = ap;
344 struct buf *bp;
345
346 bp = a->a_bp;
347
348 /*
349 * Cluster ops lock their component buffers but not the IO container.
350 */
351 if ((bp->b_flags & B_CLUSTER) != 0)
352 return;
353
354 if (BUF_REFCNT(bp) < 1) {
355 if (vfs_badlock_print)
356 printf("VOP_STRATEGY: bp is not locked but should be.\n");
357 if (vfs_badlock_panic)
358 Debugger("Lock violation.\n");
359 }
360 }
361
362 void
363 vop_lookup_pre(void *ap)
364 {
365 struct vop_lookup_args *a = ap;
366 struct vnode *dvp;
367
368 dvp = a->a_dvp;
369
370 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
371 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
372 }
373
374 void
375 vop_lookup_post(void *ap, int rc)
376 {
377 struct vop_lookup_args *a = ap;
378 struct componentname *cnp;
379 struct vnode *dvp;
380 struct vnode *vp;
381 int flags;
382
383 dvp = a->a_dvp;
384 cnp = a->a_cnp;
385 vp = *(a->a_vpp);
386 flags = cnp->cn_flags;
387
388
389 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
390 /*
391 * If this is the last path component for this lookup and LOCPARENT
392 * is set, OR if there is an error the directory has to be locked.
393 */
394 if ((flags & LOCKPARENT) && (flags & ISLASTCN))
395 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (LOCKPARENT)");
396 else if (rc != 0)
397 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (error)");
398 else if (dvp != vp)
399 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (dvp)");
400
401 if (flags & PDIRUNLOCK)
402 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (PDIRUNLOCK)");
403 }
404
405 void
406 vop_unlock_pre(void *ap)
407 {
408 struct vop_unlock_args *a = ap;
409
410 if (a->a_flags & LK_INTERLOCK)
411 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
412
413 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
414 }
415
416 void
417 vop_unlock_post(void *ap, int rc)
418 {
419 struct vop_unlock_args *a = ap;
420
421 if (a->a_flags & LK_INTERLOCK)
422 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
423 }
424
425 void
426 vop_lock_pre(void *ap)
427 {
428 struct vop_lock_args *a = ap;
429
430 if ((a->a_flags & LK_INTERLOCK) == 0)
431 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
432 else
433 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
434 }
435
436 void
437 vop_lock_post(void *ap, int rc)
438 {
439 struct vop_lock_args *a;
440
441 a = ap;
442
443 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
444 if (rc == 0)
445 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
446 }
447
448 void
449 v_addpollinfo(struct vnode *vp)
450 {
451 vp->v_pollinfo = uma_zalloc(vnodepoll_zone, M_WAITOK);
452 mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
453 }
454
455 /*
456 * Initialize the vnode management data structures.
457 */
458 static void
459 vntblinit(void *dummy __unused)
460 {
461
462 /*
463 * Desiredvnodes is a function of the physical memory size and
464 * the kernel's heap size. Specifically, desiredvnodes scales
465 * in proportion to the physical memory size until two fifths
466 * of the kernel's heap size is consumed by vnodes and vm
467 * objects.
468 */
469 desiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size /
470 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
471 minvnodes = desiredvnodes / 4;
472 mtx_init(&mountlist_mtx, "mountlist", NULL, MTX_DEF);
473 mtx_init(&mntvnode_mtx, "mntvnode", NULL, MTX_DEF);
474 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
475 mtx_init(&spechash_mtx, "spechash", NULL, MTX_DEF);
476 TAILQ_INIT(&vnode_free_list);
477 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
478 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
479 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
480 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
481 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
482 /*
483 * Initialize the filesystem syncer.
484 */
485 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
486 &syncer_mask);
487 syncer_maxdelay = syncer_mask + 1;
488 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
489 }
490 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL)
491
492
493 /*
494 * Mark a mount point as busy. Used to synchronize access and to delay
495 * unmounting. Interlock is not released on failure.
496 */
497 int
498 vfs_busy(mp, flags, interlkp, td)
499 struct mount *mp;
500 int flags;
501 struct mtx *interlkp;
502 struct thread *td;
503 {
504 int lkflags;
505
506 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
507 if (flags & LK_NOWAIT)
508 return (ENOENT);
509 mp->mnt_kern_flag |= MNTK_MWAIT;
510 /*
511 * Since all busy locks are shared except the exclusive
512 * lock granted when unmounting, the only place that a
513 * wakeup needs to be done is at the release of the
514 * exclusive lock at the end of dounmount.
515 */
516 msleep(mp, interlkp, PVFS, "vfs_busy", 0);
517 return (ENOENT);
518 }
519 lkflags = LK_SHARED | LK_NOPAUSE;
520 if (interlkp)
521 lkflags |= LK_INTERLOCK;
522 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td))
523 panic("vfs_busy: unexpected lock failure");
524 return (0);
525 }
526
527 /*
528 * Free a busy filesystem.
529 */
530 void
531 vfs_unbusy(mp, td)
532 struct mount *mp;
533 struct thread *td;
534 {
535
536 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
537 }
538
539 /*
540 * Lookup a mount point by filesystem identifier.
541 */
542 struct mount *
543 vfs_getvfs(fsid)
544 fsid_t *fsid;
545 {
546 register struct mount *mp;
547
548 mtx_lock(&mountlist_mtx);
549 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
550 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
551 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
552 mtx_unlock(&mountlist_mtx);
553 return (mp);
554 }
555 }
556 mtx_unlock(&mountlist_mtx);
557 return ((struct mount *) 0);
558 }
559
560 /*
561 * Get a new unique fsid. Try to make its val[0] unique, since this value
562 * will be used to create fake device numbers for stat(). Also try (but
563 * not so hard) make its val[0] unique mod 2^16, since some emulators only
564 * support 16-bit device numbers. We end up with unique val[0]'s for the
565 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
566 *
567 * Keep in mind that several mounts may be running in parallel. Starting
568 * the search one past where the previous search terminated is both a
569 * micro-optimization and a defense against returning the same fsid to
570 * different mounts.
571 */
572 void
573 vfs_getnewfsid(mp)
574 struct mount *mp;
575 {
576 static u_int16_t mntid_base;
577 fsid_t tfsid;
578 int mtype;
579
580 mtx_lock(&mntid_mtx);
581 mtype = mp->mnt_vfc->vfc_typenum;
582 tfsid.val[1] = mtype;
583 mtype = (mtype & 0xFF) << 24;
584 for (;;) {
585 tfsid.val[0] = makeudev(255,
586 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
587 mntid_base++;
588 if (vfs_getvfs(&tfsid) == NULL)
589 break;
590 }
591 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
592 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
593 mtx_unlock(&mntid_mtx);
594 }
595
596 /*
597 * Knob to control the precision of file timestamps:
598 *
599 * 0 = seconds only; nanoseconds zeroed.
600 * 1 = seconds and nanoseconds, accurate within 1/HZ.
601 * 2 = seconds and nanoseconds, truncated to microseconds.
602 * >=3 = seconds and nanoseconds, maximum precision.
603 */
604 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
605
606 static int timestamp_precision = TSP_SEC;
607 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
608 ×tamp_precision, 0, "");
609
610 /*
611 * Get a current timestamp.
612 */
613 void
614 vfs_timestamp(tsp)
615 struct timespec *tsp;
616 {
617 struct timeval tv;
618
619 switch (timestamp_precision) {
620 case TSP_SEC:
621 tsp->tv_sec = time_second;
622 tsp->tv_nsec = 0;
623 break;
624 case TSP_HZ:
625 getnanotime(tsp);
626 break;
627 case TSP_USEC:
628 microtime(&tv);
629 TIMEVAL_TO_TIMESPEC(&tv, tsp);
630 break;
631 case TSP_NSEC:
632 default:
633 nanotime(tsp);
634 break;
635 }
636 }
637
638 /*
639 * Set vnode attributes to VNOVAL
640 */
641 void
642 vattr_null(vap)
643 register struct vattr *vap;
644 {
645
646 vap->va_type = VNON;
647 vap->va_size = VNOVAL;
648 vap->va_bytes = VNOVAL;
649 vap->va_mode = VNOVAL;
650 vap->va_nlink = VNOVAL;
651 vap->va_uid = VNOVAL;
652 vap->va_gid = VNOVAL;
653 vap->va_fsid = VNOVAL;
654 vap->va_fileid = VNOVAL;
655 vap->va_blocksize = VNOVAL;
656 vap->va_rdev = VNOVAL;
657 vap->va_atime.tv_sec = VNOVAL;
658 vap->va_atime.tv_nsec = VNOVAL;
659 vap->va_mtime.tv_sec = VNOVAL;
660 vap->va_mtime.tv_nsec = VNOVAL;
661 vap->va_ctime.tv_sec = VNOVAL;
662 vap->va_ctime.tv_nsec = VNOVAL;
663 vap->va_birthtime.tv_sec = VNOVAL;
664 vap->va_birthtime.tv_nsec = VNOVAL;
665 vap->va_flags = VNOVAL;
666 vap->va_gen = VNOVAL;
667 vap->va_vaflags = 0;
668 }
669
670 /*
671 * This routine is called when we have too many vnodes. It attempts
672 * to free <count> vnodes and will potentially free vnodes that still
673 * have VM backing store (VM backing store is typically the cause
674 * of a vnode blowout so we want to do this). Therefore, this operation
675 * is not considered cheap.
676 *
677 * A number of conditions may prevent a vnode from being reclaimed.
678 * the buffer cache may have references on the vnode, a directory
679 * vnode may still have references due to the namei cache representing
680 * underlying files, or the vnode may be in active use. It is not
681 * desireable to reuse such vnodes. These conditions may cause the
682 * number of vnodes to reach some minimum value regardless of what
683 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
684 */
685 static int
686 vlrureclaim(struct mount *mp)
687 {
688 struct vnode *vp;
689 int done;
690 int trigger;
691 int usevnodes;
692 int count;
693
694 /*
695 * Calculate the trigger point, don't allow user
696 * screwups to blow us up. This prevents us from
697 * recycling vnodes with lots of resident pages. We
698 * aren't trying to free memory, we are trying to
699 * free vnodes.
700 */
701 usevnodes = desiredvnodes;
702 if (usevnodes <= 0)
703 usevnodes = 1;
704 trigger = cnt.v_page_count * 2 / usevnodes;
705
706 done = 0;
707 mtx_lock(&mntvnode_mtx);
708 count = mp->mnt_nvnodelistsize / 10 + 1;
709 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
710 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
711 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
712
713 if (vp->v_type != VNON &&
714 vp->v_type != VBAD &&
715 VI_TRYLOCK(vp)) {
716 if (VMIGHTFREE(vp) && /* critical path opt */
717 (vp->v_object == NULL ||
718 vp->v_object->resident_page_count < trigger)) {
719 mtx_unlock(&mntvnode_mtx);
720 vgonel(vp, curthread);
721 done++;
722 mtx_lock(&mntvnode_mtx);
723 } else
724 VI_UNLOCK(vp);
725 }
726 --count;
727 }
728 mtx_unlock(&mntvnode_mtx);
729 return done;
730 }
731
732 /*
733 * Attempt to recycle vnodes in a context that is always safe to block.
734 * Calling vlrurecycle() from the bowels of filesystem code has some
735 * interesting deadlock problems.
736 */
737 static struct proc *vnlruproc;
738 static int vnlruproc_sig;
739
740 static void
741 vnlru_proc(void)
742 {
743 struct mount *mp, *nmp;
744 int s;
745 int done;
746 struct proc *p = vnlruproc;
747 struct thread *td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */
748
749 mtx_lock(&Giant);
750
751 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
752 SHUTDOWN_PRI_FIRST);
753
754 s = splbio();
755 for (;;) {
756 kthread_suspend_check(p);
757 mtx_lock(&vnode_free_list_mtx);
758 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) {
759 mtx_unlock(&vnode_free_list_mtx);
760 vnlruproc_sig = 0;
761 wakeup(&vnlruproc_sig);
762 tsleep(vnlruproc, PVFS, "vlruwt", hz);
763 continue;
764 }
765 mtx_unlock(&vnode_free_list_mtx);
766 done = 0;
767 mtx_lock(&mountlist_mtx);
768 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
769 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
770 nmp = TAILQ_NEXT(mp, mnt_list);
771 continue;
772 }
773 done += vlrureclaim(mp);
774 mtx_lock(&mountlist_mtx);
775 nmp = TAILQ_NEXT(mp, mnt_list);
776 vfs_unbusy(mp, td);
777 }
778 mtx_unlock(&mountlist_mtx);
779 if (done == 0) {
780 #if 0
781 /* These messages are temporary debugging aids */
782 if (vnlru_nowhere < 5)
783 printf("vnlru process getting nowhere..\n");
784 else if (vnlru_nowhere == 5)
785 printf("vnlru process messages stopped.\n");
786 #endif
787 vnlru_nowhere++;
788 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
789 }
790 }
791 splx(s);
792 }
793
794 static struct kproc_desc vnlru_kp = {
795 "vnlru",
796 vnlru_proc,
797 &vnlruproc
798 };
799 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
800
801
802 /*
803 * Routines having to do with the management of the vnode table.
804 */
805
806 /*
807 * Check to see if a free vnode can be recycled. If it can,
808 * return it locked with the vn lock, but not interlock. Also
809 * get the vn_start_write lock. Otherwise indicate the error.
810 */
811 static int
812 vcanrecycle(struct vnode *vp, struct mount **vnmpp)
813 {
814 struct thread *td = curthread;
815 vm_object_t object;
816 int error;
817
818 /* Don't recycle if we can't get the interlock */
819 if (!VI_TRYLOCK(vp))
820 return (EWOULDBLOCK);
821
822 /* We should be able to immediately acquire this */
823 /* XXX This looks like it should panic if it fails */
824 if (vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td) != 0) {
825 if (VOP_ISLOCKED(vp, td))
826 panic("vcanrecycle: locked vnode");
827 return (EWOULDBLOCK);
828 }
829
830 /*
831 * Don't recycle if its filesystem is being suspended.
832 */
833 if (vn_start_write(vp, vnmpp, V_NOWAIT) != 0) {
834 error = EBUSY;
835 goto done;
836 }
837
838 /*
839 * Don't recycle if we still have cached pages.
840 */
841 if (VOP_GETVOBJECT(vp, &object) == 0) {
842 VM_OBJECT_LOCK(object);
843 if (object->resident_page_count ||
844 object->ref_count) {
845 VM_OBJECT_UNLOCK(object);
846 error = EBUSY;
847 goto done;
848 }
849 VM_OBJECT_UNLOCK(object);
850 }
851 if (LIST_FIRST(&vp->v_cache_src)) {
852 /*
853 * note: nameileafonly sysctl is temporary,
854 * for debugging only, and will eventually be
855 * removed.
856 */
857 if (nameileafonly > 0) {
858 /*
859 * Do not reuse namei-cached directory
860 * vnodes that have cached
861 * subdirectories.
862 */
863 if (cache_leaf_test(vp) < 0) {
864 error = EISDIR;
865 goto done;
866 }
867 } else if (nameileafonly < 0 ||
868 vmiodirenable == 0) {
869 /*
870 * Do not reuse namei-cached directory
871 * vnodes if nameileafonly is -1 or
872 * if VMIO backing for directories is
873 * turned off (otherwise we reuse them
874 * too quickly).
875 */
876 error = EBUSY;
877 goto done;
878 }
879 }
880 return (0);
881 done:
882 VOP_UNLOCK(vp, 0, td);
883 return (error);
884 }
885
886 /*
887 * Return the next vnode from the free list.
888 */
889 int
890 getnewvnode(tag, mp, vops, vpp)
891 const char *tag;
892 struct mount *mp;
893 vop_t **vops;
894 struct vnode **vpp;
895 {
896 int s;
897 struct thread *td = curthread; /* XXX */
898 struct vnode *vp = NULL;
899 struct vpollinfo *pollinfo = NULL;
900 struct mount *vnmp;
901
902 s = splbio();
903 mtx_lock(&vnode_free_list_mtx);
904
905 /*
906 * Try to reuse vnodes if we hit the max. This situation only
907 * occurs in certain large-memory (2G+) situations. We cannot
908 * attempt to directly reclaim vnodes due to nasty recursion
909 * problems.
910 */
911 while (numvnodes - freevnodes > desiredvnodes) {
912 if (vnlruproc_sig == 0) {
913 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
914 wakeup(vnlruproc);
915 }
916 mtx_unlock(&vnode_free_list_mtx);
917 tsleep(&vnlruproc_sig, PVFS, "vlruwk", hz);
918 mtx_lock(&vnode_free_list_mtx);
919 }
920
921 /*
922 * Attempt to reuse a vnode already on the free list, allocating
923 * a new vnode if we can't find one or if we have not reached a
924 * good minimum for good LRU performance.
925 */
926
927 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) {
928 int error;
929 int count;
930
931 for (count = 0; count < freevnodes; count++) {
932 vp = TAILQ_FIRST(&vnode_free_list);
933
934 KASSERT(vp->v_usecount == 0 &&
935 (vp->v_iflag & VI_DOINGINACT) == 0,
936 ("getnewvnode: free vnode isn't"));
937
938 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
939 /*
940 * We have to drop the free list mtx to avoid lock
941 * order reversals with interlock.
942 */
943 mtx_unlock(&vnode_free_list_mtx);
944 error = vcanrecycle(vp, &vnmp);
945 mtx_lock(&vnode_free_list_mtx);
946 if (error == 0)
947 break;
948 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
949 vp = NULL;
950 }
951 }
952 if (vp) {
953 freevnodes--;
954 mtx_unlock(&vnode_free_list_mtx);
955
956 cache_purge(vp);
957 VI_LOCK(vp);
958 vp->v_iflag |= VI_DOOMED;
959 vp->v_iflag &= ~VI_FREE;
960 if (vp->v_type != VBAD) {
961 VOP_UNLOCK(vp, 0, td);
962 vgonel(vp, td);
963 VI_LOCK(vp);
964 } else {
965 VOP_UNLOCK(vp, 0, td);
966 }
967 vn_finished_write(vnmp);
968
969 #ifdef INVARIANTS
970 {
971 if (vp->v_data)
972 panic("cleaned vnode isn't");
973 if (vp->v_numoutput)
974 panic("Clean vnode has pending I/O's");
975 if (vp->v_writecount != 0)
976 panic("Non-zero write count");
977 }
978 #endif
979 if ((pollinfo = vp->v_pollinfo) != NULL) {
980 /*
981 * To avoid lock order reversals, the call to
982 * uma_zfree() must be delayed until the vnode
983 * interlock is released.
984 */
985 vp->v_pollinfo = NULL;
986 }
987 #ifdef MAC
988 mac_destroy_vnode(vp);
989 #endif
990 vp->v_iflag = 0;
991 vp->v_vflag = 0;
992 vp->v_lastw = 0;
993 vp->v_lasta = 0;
994 vp->v_cstart = 0;
995 vp->v_clen = 0;
996 vp->v_socket = 0;
997 lockdestroy(vp->v_vnlock);
998 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE);
999 KASSERT(vp->v_cleanbufcnt == 0, ("cleanbufcnt not 0"));
1000 KASSERT(vp->v_cleanblkroot == NULL, ("cleanblkroot not NULL"));
1001 KASSERT(vp->v_dirtybufcnt == 0, ("dirtybufcnt not 0"));
1002 KASSERT(vp->v_dirtyblkroot == NULL, ("dirtyblkroot not NULL"));
1003 } else {
1004 numvnodes++;
1005 mtx_unlock(&vnode_free_list_mtx);
1006
1007 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1008 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1009 VI_LOCK(vp);
1010 vp->v_dd = vp;
1011 vp->v_vnlock = &vp->v_lock;
1012 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE);
1013 cache_purge(vp);
1014 LIST_INIT(&vp->v_cache_src);
1015 TAILQ_INIT(&vp->v_cache_dst);
1016 }
1017
1018 TAILQ_INIT(&vp->v_cleanblkhd);
1019 TAILQ_INIT(&vp->v_dirtyblkhd);
1020 vp->v_type = VNON;
1021 vp->v_tag = tag;
1022 vp->v_op = vops;
1023 *vpp = vp;
1024 vp->v_usecount = 1;
1025 vp->v_data = 0;
1026 vp->v_cachedid = -1;
1027 VI_UNLOCK(vp);
1028 if (pollinfo != NULL) {
1029 mtx_destroy(&pollinfo->vpi_lock);
1030 uma_zfree(vnodepoll_zone, pollinfo);
1031 }
1032 #ifdef MAC
1033 mac_init_vnode(vp);
1034 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1035 mac_associate_vnode_singlelabel(mp, vp);
1036 #endif
1037 insmntque(vp, mp);
1038
1039 return (0);
1040 }
1041
1042 /*
1043 * Move a vnode from one mount queue to another.
1044 */
1045 static void
1046 insmntque(vp, mp)
1047 register struct vnode *vp;
1048 register struct mount *mp;
1049 {
1050
1051 mtx_lock(&mntvnode_mtx);
1052 /*
1053 * Delete from old mount point vnode list, if on one.
1054 */
1055 if (vp->v_mount != NULL) {
1056 KASSERT(vp->v_mount->mnt_nvnodelistsize > 0,
1057 ("bad mount point vnode list size"));
1058 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes);
1059 vp->v_mount->mnt_nvnodelistsize--;
1060 }
1061 /*
1062 * Insert into list of vnodes for the new mount point, if available.
1063 */
1064 if ((vp->v_mount = mp) == NULL) {
1065 mtx_unlock(&mntvnode_mtx);
1066 return;
1067 }
1068 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1069 mp->mnt_nvnodelistsize++;
1070 mtx_unlock(&mntvnode_mtx);
1071 }
1072
1073 /*
1074 * Update outstanding I/O count and do wakeup if requested.
1075 */
1076 void
1077 vwakeup(bp)
1078 register struct buf *bp;
1079 {
1080 register struct vnode *vp;
1081
1082 bp->b_flags &= ~B_WRITEINPROG;
1083 if ((vp = bp->b_vp)) {
1084 VI_LOCK(vp);
1085 vp->v_numoutput--;
1086 if (vp->v_numoutput < 0)
1087 panic("vwakeup: neg numoutput");
1088 if ((vp->v_numoutput == 0) && (vp->v_iflag & VI_BWAIT)) {
1089 vp->v_iflag &= ~VI_BWAIT;
1090 wakeup(&vp->v_numoutput);
1091 }
1092 VI_UNLOCK(vp);
1093 }
1094 }
1095
1096 /*
1097 * Flush out and invalidate all buffers associated with a vnode.
1098 * Called with the underlying object locked.
1099 */
1100 int
1101 vinvalbuf(vp, flags, cred, td, slpflag, slptimeo)
1102 struct vnode *vp;
1103 int flags;
1104 struct ucred *cred;
1105 struct thread *td;
1106 int slpflag, slptimeo;
1107 {
1108 struct buf *blist;
1109 int s, error;
1110 vm_object_t object;
1111
1112 GIANT_REQUIRED;
1113
1114 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1115
1116 VI_LOCK(vp);
1117 if (flags & V_SAVE) {
1118 s = splbio();
1119 while (vp->v_numoutput) {
1120 vp->v_iflag |= VI_BWAIT;
1121 error = msleep(&vp->v_numoutput, VI_MTX(vp),
1122 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
1123 if (error) {
1124 VI_UNLOCK(vp);
1125 splx(s);
1126 return (error);
1127 }
1128 }
1129 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1130 splx(s);
1131 VI_UNLOCK(vp);
1132 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, td)) != 0)
1133 return (error);
1134 /*
1135 * XXX We could save a lock/unlock if this was only
1136 * enabled under INVARIANTS
1137 */
1138 VI_LOCK(vp);
1139 s = splbio();
1140 if (vp->v_numoutput > 0 ||
1141 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
1142 panic("vinvalbuf: dirty bufs");
1143 }
1144 splx(s);
1145 }
1146 s = splbio();
1147 /*
1148 * If you alter this loop please notice that interlock is dropped and
1149 * reacquired in flushbuflist. Special care is needed to ensure that
1150 * no race conditions occur from this.
1151 */
1152 for (error = 0;;) {
1153 if ((blist = TAILQ_FIRST(&vp->v_cleanblkhd)) != 0 &&
1154 flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) {
1155 if (error)
1156 break;
1157 continue;
1158 }
1159 if ((blist = TAILQ_FIRST(&vp->v_dirtyblkhd)) != 0 &&
1160 flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) {
1161 if (error)
1162 break;
1163 continue;
1164 }
1165 break;
1166 }
1167 if (error) {
1168 splx(s);
1169 VI_UNLOCK(vp);
1170 return (error);
1171 }
1172
1173 /*
1174 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1175 * have write I/O in-progress but if there is a VM object then the
1176 * VM object can also have read-I/O in-progress.
1177 */
1178 do {
1179 while (vp->v_numoutput > 0) {
1180 vp->v_iflag |= VI_BWAIT;
1181 msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vnvlbv", 0);
1182 }
1183 VI_UNLOCK(vp);
1184 if (VOP_GETVOBJECT(vp, &object) == 0) {
1185 VM_OBJECT_LOCK(object);
1186 vm_object_pip_wait(object, "vnvlbx");
1187 VM_OBJECT_UNLOCK(object);
1188 }
1189 VI_LOCK(vp);
1190 } while (vp->v_numoutput > 0);
1191 VI_UNLOCK(vp);
1192
1193 splx(s);
1194
1195 /*
1196 * Destroy the copy in the VM cache, too.
1197 */
1198 if (VOP_GETVOBJECT(vp, &object) == 0) {
1199 VM_OBJECT_LOCK(object);
1200 vm_object_page_remove(object, 0, 0,
1201 (flags & V_SAVE) ? TRUE : FALSE);
1202 VM_OBJECT_UNLOCK(object);
1203 }
1204
1205 #ifdef INVARIANTS
1206 VI_LOCK(vp);
1207 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1208 (!TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
1209 !TAILQ_EMPTY(&vp->v_cleanblkhd)))
1210 panic("vinvalbuf: flush failed");
1211 VI_UNLOCK(vp);
1212 #endif
1213 return (0);
1214 }
1215
1216 /*
1217 * Flush out buffers on the specified list.
1218 *
1219 */
1220 static int
1221 flushbuflist(blist, flags, vp, slpflag, slptimeo, errorp)
1222 struct buf *blist;
1223 int flags;
1224 struct vnode *vp;
1225 int slpflag, slptimeo;
1226 int *errorp;
1227 {
1228 struct buf *bp, *nbp;
1229 int found, error;
1230
1231 ASSERT_VI_LOCKED(vp, "flushbuflist");
1232
1233 for (found = 0, bp = blist; bp; bp = nbp) {
1234 nbp = TAILQ_NEXT(bp, b_vnbufs);
1235 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1236 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1237 continue;
1238 }
1239 found += 1;
1240 error = BUF_TIMELOCK(bp,
1241 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, VI_MTX(vp),
1242 "flushbuf", slpflag, slptimeo);
1243 if (error) {
1244 if (error != ENOLCK)
1245 *errorp = error;
1246 goto done;
1247 }
1248 /*
1249 * XXX Since there are no node locks for NFS, I
1250 * believe there is a slight chance that a delayed
1251 * write will occur while sleeping just above, so
1252 * check for it. Note that vfs_bio_awrite expects
1253 * buffers to reside on a queue, while BUF_WRITE and
1254 * brelse do not.
1255 */
1256 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1257 (flags & V_SAVE)) {
1258
1259 if (bp->b_vp == vp) {
1260 if (bp->b_flags & B_CLUSTEROK) {
1261 vfs_bio_awrite(bp);
1262 } else {
1263 bremfree(bp);
1264 bp->b_flags |= B_ASYNC;
1265 BUF_WRITE(bp);
1266 }
1267 } else {
1268 bremfree(bp);
1269 (void) BUF_WRITE(bp);
1270 }
1271 goto done;
1272 }
1273 bremfree(bp);
1274 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
1275 bp->b_flags &= ~B_ASYNC;
1276 brelse(bp);
1277 VI_LOCK(vp);
1278 }
1279 return (found);
1280 done:
1281 VI_LOCK(vp);
1282 return (found);
1283 }
1284
1285 /*
1286 * Truncate a file's buffer and pages to a specified length. This
1287 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1288 * sync activity.
1289 */
1290 int
1291 vtruncbuf(vp, cred, td, length, blksize)
1292 register struct vnode *vp;
1293 struct ucred *cred;
1294 struct thread *td;
1295 off_t length;
1296 int blksize;
1297 {
1298 register struct buf *bp;
1299 struct buf *nbp;
1300 int s, anyfreed;
1301 int trunclbn;
1302
1303 /*
1304 * Round up to the *next* lbn.
1305 */
1306 trunclbn = (length + blksize - 1) / blksize;
1307
1308 s = splbio();
1309 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1310 restart:
1311 VI_LOCK(vp);
1312 anyfreed = 1;
1313 for (;anyfreed;) {
1314 anyfreed = 0;
1315 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
1316 nbp = TAILQ_NEXT(bp, b_vnbufs);
1317 if (bp->b_lblkno >= trunclbn) {
1318 if (BUF_LOCK(bp,
1319 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1320 VI_MTX(vp)) == ENOLCK)
1321 goto restart;
1322
1323 bremfree(bp);
1324 bp->b_flags |= (B_INVAL | B_RELBUF);
1325 bp->b_flags &= ~B_ASYNC;
1326 brelse(bp);
1327 anyfreed = 1;
1328
1329 if (nbp &&
1330 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1331 (nbp->b_vp != vp) ||
1332 (nbp->b_flags & B_DELWRI))) {
1333 goto restart;
1334 }
1335 VI_LOCK(vp);
1336 }
1337 }
1338
1339 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1340 nbp = TAILQ_NEXT(bp, b_vnbufs);
1341 if (bp->b_lblkno >= trunclbn) {
1342 if (BUF_LOCK(bp,
1343 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1344 VI_MTX(vp)) == ENOLCK)
1345 goto restart;
1346 bremfree(bp);
1347 bp->b_flags |= (B_INVAL | B_RELBUF);
1348 bp->b_flags &= ~B_ASYNC;
1349 brelse(bp);
1350 anyfreed = 1;
1351 if (nbp &&
1352 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1353 (nbp->b_vp != vp) ||
1354 (nbp->b_flags & B_DELWRI) == 0)) {
1355 goto restart;
1356 }
1357 VI_LOCK(vp);
1358 }
1359 }
1360 }
1361
1362 if (length > 0) {
1363 restartsync:
1364 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1365 nbp = TAILQ_NEXT(bp, b_vnbufs);
1366 if (bp->b_lblkno > 0)
1367 continue;
1368 /*
1369 * Since we hold the vnode lock this should only
1370 * fail if we're racing with the buf daemon.
1371 */
1372 if (BUF_LOCK(bp,
1373 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1374 VI_MTX(vp)) == ENOLCK) {
1375 goto restart;
1376 }
1377 KASSERT((bp->b_flags & B_DELWRI),
1378 ("buf(%p) on dirty queue without DELWRI.", bp));
1379
1380 bremfree(bp);
1381 bawrite(bp);
1382 VI_LOCK(vp);
1383 goto restartsync;
1384 }
1385 }
1386
1387 while (vp->v_numoutput > 0) {
1388 vp->v_iflag |= VI_BWAIT;
1389 msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vbtrunc", 0);
1390 }
1391 VI_UNLOCK(vp);
1392 splx(s);
1393
1394 vnode_pager_setsize(vp, length);
1395
1396 return (0);
1397 }
1398
1399 /*
1400 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1401 * a vnode.
1402 *
1403 * NOTE: We have to deal with the special case of a background bitmap
1404 * buffer, a situation where two buffers will have the same logical
1405 * block offset. We want (1) only the foreground buffer to be accessed
1406 * in a lookup and (2) must differentiate between the foreground and
1407 * background buffer in the splay tree algorithm because the splay
1408 * tree cannot normally handle multiple entities with the same 'index'.
1409 * We accomplish this by adding differentiating flags to the splay tree's
1410 * numerical domain.
1411 */
1412 static
1413 struct buf *
1414 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1415 {
1416 struct buf dummy;
1417 struct buf *lefttreemax, *righttreemin, *y;
1418
1419 if (root == NULL)
1420 return (NULL);
1421 lefttreemax = righttreemin = &dummy;
1422 for (;;) {
1423 if (lblkno < root->b_lblkno ||
1424 (lblkno == root->b_lblkno &&
1425 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1426 if ((y = root->b_left) == NULL)
1427 break;
1428 if (lblkno < y->b_lblkno) {
1429 /* Rotate right. */
1430 root->b_left = y->b_right;
1431 y->b_right = root;
1432 root = y;
1433 if ((y = root->b_left) == NULL)
1434 break;
1435 }
1436 /* Link into the new root's right tree. */
1437 righttreemin->b_left = root;
1438 righttreemin = root;
1439 } else if (lblkno > root->b_lblkno ||
1440 (lblkno == root->b_lblkno &&
1441 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1442 if ((y = root->b_right) == NULL)
1443 break;
1444 if (lblkno > y->b_lblkno) {
1445 /* Rotate left. */
1446 root->b_right = y->b_left;
1447 y->b_left = root;
1448 root = y;
1449 if ((y = root->b_right) == NULL)
1450 break;
1451 }
1452 /* Link into the new root's left tree. */
1453 lefttreemax->b_right = root;
1454 lefttreemax = root;
1455 } else {
1456 break;
1457 }
1458 root = y;
1459 }
1460 /* Assemble the new root. */
1461 lefttreemax->b_right = root->b_left;
1462 righttreemin->b_left = root->b_right;
1463 root->b_left = dummy.b_right;
1464 root->b_right = dummy.b_left;
1465 return (root);
1466 }
1467
1468 static
1469 void
1470 buf_vlist_remove(struct buf *bp)
1471 {
1472 struct vnode *vp = bp->b_vp;
1473 struct buf *root;
1474
1475 ASSERT_VI_LOCKED(vp, "buf_vlist_remove");
1476 if (bp->b_xflags & BX_VNDIRTY) {
1477 if (bp != vp->v_dirtyblkroot) {
1478 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot);
1479 KASSERT(root == bp, ("splay lookup failed during dirty remove"));
1480 }
1481 if (bp->b_left == NULL) {
1482 root = bp->b_right;
1483 } else {
1484 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1485 root->b_right = bp->b_right;
1486 }
1487 vp->v_dirtyblkroot = root;
1488 TAILQ_REMOVE(&vp->v_dirtyblkhd, bp, b_vnbufs);
1489 vp->v_dirtybufcnt--;
1490 } else {
1491 /* KASSERT(bp->b_xflags & BX_VNCLEAN, ("bp wasn't clean")); */
1492 if (bp != vp->v_cleanblkroot) {
1493 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot);
1494 KASSERT(root == bp, ("splay lookup failed during clean remove"));
1495 }
1496 if (bp->b_left == NULL) {
1497 root = bp->b_right;
1498 } else {
1499 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1500 root->b_right = bp->b_right;
1501 }
1502 vp->v_cleanblkroot = root;
1503 TAILQ_REMOVE(&vp->v_cleanblkhd, bp, b_vnbufs);
1504 vp->v_cleanbufcnt--;
1505 }
1506 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1507 }
1508
1509 /*
1510 * Add the buffer to the sorted clean or dirty block list using a
1511 * splay tree algorithm.
1512 *
1513 * NOTE: xflags is passed as a constant, optimizing this inline function!
1514 */
1515 static
1516 void
1517 buf_vlist_add(struct buf *bp, struct vnode *vp, b_xflags_t xflags)
1518 {
1519 struct buf *root;
1520
1521 ASSERT_VI_LOCKED(vp, "buf_vlist_add");
1522 bp->b_xflags |= xflags;
1523 if (xflags & BX_VNDIRTY) {
1524 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot);
1525 if (root == NULL) {
1526 bp->b_left = NULL;
1527 bp->b_right = NULL;
1528 TAILQ_INSERT_TAIL(&vp->v_dirtyblkhd, bp, b_vnbufs);
1529 } else if (bp->b_lblkno < root->b_lblkno ||
1530 (bp->b_lblkno == root->b_lblkno &&
1531 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1532 bp->b_left = root->b_left;
1533 bp->b_right = root;
1534 root->b_left = NULL;
1535 TAILQ_INSERT_BEFORE(root, bp, b_vnbufs);
1536 } else {
1537 bp->b_right = root->b_right;
1538 bp->b_left = root;
1539 root->b_right = NULL;
1540 TAILQ_INSERT_AFTER(&vp->v_dirtyblkhd,
1541 root, bp, b_vnbufs);
1542 }
1543 vp->v_dirtybufcnt++;
1544 vp->v_dirtyblkroot = bp;
1545 } else {
1546 /* KASSERT(xflags & BX_VNCLEAN, ("xflags not clean")); */
1547 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot);
1548 if (root == NULL) {
1549 bp->b_left = NULL;
1550 bp->b_right = NULL;
1551 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
1552 } else if (bp->b_lblkno < root->b_lblkno ||
1553 (bp->b_lblkno == root->b_lblkno &&
1554 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1555 bp->b_left = root->b_left;
1556 bp->b_right = root;
1557 root->b_left = NULL;
1558 TAILQ_INSERT_BEFORE(root, bp, b_vnbufs);
1559 } else {
1560 bp->b_right = root->b_right;
1561 bp->b_left = root;
1562 root->b_right = NULL;
1563 TAILQ_INSERT_AFTER(&vp->v_cleanblkhd,
1564 root, bp, b_vnbufs);
1565 }
1566 vp->v_cleanbufcnt++;
1567 vp->v_cleanblkroot = bp;
1568 }
1569 }
1570
1571 /*
1572 * Lookup a buffer using the splay tree. Note that we specifically avoid
1573 * shadow buffers used in background bitmap writes.
1574 *
1575 * This code isn't quite efficient as it could be because we are maintaining
1576 * two sorted lists and do not know which list the block resides in.
1577 *
1578 * During a "make buildworld" the desired buffer is found at one of
1579 * the roots more than 60% of the time. Thus, checking both roots
1580 * before performing either splay eliminates unnecessary splays on the
1581 * first tree splayed.
1582 */
1583 struct buf *
1584 gbincore(struct vnode *vp, daddr_t lblkno)
1585 {
1586 struct buf *bp;
1587
1588 GIANT_REQUIRED;
1589
1590 ASSERT_VI_LOCKED(vp, "gbincore");
1591 if ((bp = vp->v_cleanblkroot) != NULL &&
1592 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1593 return (bp);
1594 if ((bp = vp->v_dirtyblkroot) != NULL &&
1595 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1596 return (bp);
1597 if ((bp = vp->v_cleanblkroot) != NULL) {
1598 vp->v_cleanblkroot = bp = buf_splay(lblkno, 0, bp);
1599 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1600 return (bp);
1601 }
1602 if ((bp = vp->v_dirtyblkroot) != NULL) {
1603 vp->v_dirtyblkroot = bp = buf_splay(lblkno, 0, bp);
1604 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1605 return (bp);
1606 }
1607 return (NULL);
1608 }
1609
1610 /*
1611 * Associate a buffer with a vnode.
1612 */
1613 void
1614 bgetvp(vp, bp)
1615 register struct vnode *vp;
1616 register struct buf *bp;
1617 {
1618 int s;
1619
1620 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
1621
1622 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1623 ("bgetvp: bp already attached! %p", bp));
1624
1625 ASSERT_VI_LOCKED(vp, "bgetvp");
1626 vholdl(vp);
1627 bp->b_vp = vp;
1628 bp->b_dev = vn_todev(vp);
1629 /*
1630 * Insert onto list for new vnode.
1631 */
1632 s = splbio();
1633 buf_vlist_add(bp, vp, BX_VNCLEAN);
1634 splx(s);
1635 }
1636
1637 /*
1638 * Disassociate a buffer from a vnode.
1639 */
1640 void
1641 brelvp(bp)
1642 register struct buf *bp;
1643 {
1644 struct vnode *vp;
1645 int s;
1646
1647 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1648
1649 /*
1650 * Delete from old vnode list, if on one.
1651 */
1652 vp = bp->b_vp;
1653 s = splbio();
1654 VI_LOCK(vp);
1655 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1656 buf_vlist_remove(bp);
1657 if ((vp->v_iflag & VI_ONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
1658 vp->v_iflag &= ~VI_ONWORKLST;
1659 mtx_lock(&sync_mtx);
1660 LIST_REMOVE(vp, v_synclist);
1661 mtx_unlock(&sync_mtx);
1662 }
1663 vdropl(vp);
1664 VI_UNLOCK(vp);
1665 bp->b_vp = (struct vnode *) 0;
1666 if (bp->b_object)
1667 bp->b_object = NULL;
1668 splx(s);
1669 }
1670
1671 /*
1672 * Add an item to the syncer work queue.
1673 */
1674 static void
1675 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
1676 {
1677 int s, slot;
1678
1679 s = splbio();
1680 ASSERT_VI_LOCKED(vp, "vn_syncer_add_to_worklist");
1681
1682 mtx_lock(&sync_mtx);
1683 if (vp->v_iflag & VI_ONWORKLST)
1684 LIST_REMOVE(vp, v_synclist);
1685 else
1686 vp->v_iflag |= VI_ONWORKLST;
1687
1688 if (delay > syncer_maxdelay - 2)
1689 delay = syncer_maxdelay - 2;
1690 slot = (syncer_delayno + delay) & syncer_mask;
1691
1692 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
1693 mtx_unlock(&sync_mtx);
1694
1695 splx(s);
1696 }
1697
1698 struct proc *updateproc;
1699 static void sched_sync(void);
1700 static struct kproc_desc up_kp = {
1701 "syncer",
1702 sched_sync,
1703 &updateproc
1704 };
1705 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1706
1707 /*
1708 * System filesystem synchronizer daemon.
1709 */
1710 static void
1711 sched_sync(void)
1712 {
1713 struct synclist *slp;
1714 struct vnode *vp;
1715 struct mount *mp;
1716 long starttime;
1717 int s;
1718 struct thread *td = FIRST_THREAD_IN_PROC(updateproc); /* XXXKSE */
1719
1720 mtx_lock(&Giant);
1721
1722 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, td->td_proc,
1723 SHUTDOWN_PRI_LAST);
1724
1725 for (;;) {
1726 kthread_suspend_check(td->td_proc);
1727
1728 starttime = time_second;
1729
1730 /*
1731 * Push files whose dirty time has expired. Be careful
1732 * of interrupt race on slp queue.
1733 */
1734 s = splbio();
1735 mtx_lock(&sync_mtx);
1736 slp = &syncer_workitem_pending[syncer_delayno];
1737 syncer_delayno += 1;
1738 if (syncer_delayno == syncer_maxdelay)
1739 syncer_delayno = 0;
1740 splx(s);
1741
1742 while ((vp = LIST_FIRST(slp)) != NULL) {
1743 mtx_unlock(&sync_mtx);
1744 if (VOP_ISLOCKED(vp, NULL) == 0 &&
1745 vn_start_write(vp, &mp, V_NOWAIT) == 0) {
1746 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1747 (void) VOP_FSYNC(vp, td->td_ucred, MNT_LAZY, td);
1748 VOP_UNLOCK(vp, 0, td);
1749 vn_finished_write(mp);
1750 }
1751 s = splbio();
1752 mtx_lock(&sync_mtx);
1753 if (LIST_FIRST(slp) == vp) {
1754 mtx_unlock(&sync_mtx);
1755 /*
1756 * Note: VFS vnodes can remain on the
1757 * worklist too with no dirty blocks, but
1758 * since sync_fsync() moves it to a different
1759 * slot we are safe.
1760 */
1761 VI_LOCK(vp);
1762 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1763 !vn_isdisk(vp, NULL)) {
1764 panic("sched_sync: fsync failed "
1765 "vp %p tag %s", vp, vp->v_tag);
1766 }
1767 /*
1768 * Put us back on the worklist. The worklist
1769 * routine will remove us from our current
1770 * position and then add us back in at a later
1771 * position.
1772 */
1773 vn_syncer_add_to_worklist(vp, syncdelay);
1774 VI_UNLOCK(vp);
1775 mtx_lock(&sync_mtx);
1776 }
1777 splx(s);
1778 }
1779 mtx_unlock(&sync_mtx);
1780
1781 /*
1782 * Do soft update processing.
1783 */
1784 if (softdep_process_worklist_hook != NULL)
1785 (*softdep_process_worklist_hook)(NULL);
1786
1787 /*
1788 * The variable rushjob allows the kernel to speed up the
1789 * processing of the filesystem syncer process. A rushjob
1790 * value of N tells the filesystem syncer to process the next
1791 * N seconds worth of work on its queue ASAP. Currently rushjob
1792 * is used by the soft update code to speed up the filesystem
1793 * syncer process when the incore state is getting so far
1794 * ahead of the disk that the kernel memory pool is being
1795 * threatened with exhaustion.
1796 */
1797 mtx_lock(&sync_mtx);
1798 if (rushjob > 0) {
1799 rushjob -= 1;
1800 mtx_unlock(&sync_mtx);
1801 continue;
1802 }
1803 mtx_unlock(&sync_mtx);
1804 /*
1805 * If it has taken us less than a second to process the
1806 * current work, then wait. Otherwise start right over
1807 * again. We can still lose time if any single round
1808 * takes more than two seconds, but it does not really
1809 * matter as we are just trying to generally pace the
1810 * filesystem activity.
1811 */
1812 if (time_second == starttime)
1813 tsleep(&lbolt, PPAUSE, "syncer", 0);
1814 }
1815 }
1816
1817 /*
1818 * Request the syncer daemon to speed up its work.
1819 * We never push it to speed up more than half of its
1820 * normal turn time, otherwise it could take over the cpu.
1821 * XXXKSE only one update?
1822 */
1823 int
1824 speedup_syncer()
1825 {
1826 struct thread *td;
1827 int ret = 0;
1828
1829 td = FIRST_THREAD_IN_PROC(updateproc);
1830 mtx_lock_spin(&sched_lock);
1831 if (td->td_wchan == &lbolt) {
1832 unsleep(td);
1833 TD_CLR_SLEEPING(td);
1834 setrunnable(td);
1835 }
1836 mtx_unlock_spin(&sched_lock);
1837 mtx_lock(&sync_mtx);
1838 if (rushjob < syncdelay / 2) {
1839 rushjob += 1;
1840 stat_rush_requests += 1;
1841 ret = 1;
1842 }
1843 mtx_unlock(&sync_mtx);
1844 return (ret);
1845 }
1846
1847 /*
1848 * Associate a p-buffer with a vnode.
1849 *
1850 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1851 * with the buffer. i.e. the bp has not been linked into the vnode or
1852 * ref-counted.
1853 */
1854 void
1855 pbgetvp(vp, bp)
1856 register struct vnode *vp;
1857 register struct buf *bp;
1858 {
1859
1860 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1861
1862 bp->b_vp = vp;
1863 bp->b_flags |= B_PAGING;
1864 bp->b_dev = vn_todev(vp);
1865 }
1866
1867 /*
1868 * Disassociate a p-buffer from a vnode.
1869 */
1870 void
1871 pbrelvp(bp)
1872 register struct buf *bp;
1873 {
1874
1875 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1876
1877 /* XXX REMOVE ME */
1878 VI_LOCK(bp->b_vp);
1879 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) {
1880 panic(
1881 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1882 bp,
1883 (int)bp->b_flags
1884 );
1885 }
1886 VI_UNLOCK(bp->b_vp);
1887 bp->b_vp = (struct vnode *) 0;
1888 bp->b_flags &= ~B_PAGING;
1889 }
1890
1891 /*
1892 * Reassign a buffer from one vnode to another.
1893 * Used to assign file specific control information
1894 * (indirect blocks) to the vnode to which they belong.
1895 */
1896 void
1897 reassignbuf(bp, newvp)
1898 register struct buf *bp;
1899 register struct vnode *newvp;
1900 {
1901 int delay;
1902 int s;
1903
1904 if (newvp == NULL) {
1905 printf("reassignbuf: NULL");
1906 return;
1907 }
1908 ++reassignbufcalls;
1909
1910 /*
1911 * B_PAGING flagged buffers cannot be reassigned because their vp
1912 * is not fully linked in.
1913 */
1914 if (bp->b_flags & B_PAGING)
1915 panic("cannot reassign paging buffer");
1916
1917 s = splbio();
1918 /*
1919 * Delete from old vnode list, if on one.
1920 */
1921 VI_LOCK(bp->b_vp);
1922 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1923 buf_vlist_remove(bp);
1924 if (bp->b_vp != newvp) {
1925 vdropl(bp->b_vp);
1926 bp->b_vp = NULL; /* for clarification */
1927 }
1928 }
1929 VI_UNLOCK(bp->b_vp);
1930 /*
1931 * If dirty, put on list of dirty buffers; otherwise insert onto list
1932 * of clean buffers.
1933 */
1934 VI_LOCK(newvp);
1935 if (bp->b_flags & B_DELWRI) {
1936 if ((newvp->v_iflag & VI_ONWORKLST) == 0) {
1937 switch (newvp->v_type) {
1938 case VDIR:
1939 delay = dirdelay;
1940 break;
1941 case VCHR:
1942 if (newvp->v_rdev->si_mountpoint != NULL) {
1943 delay = metadelay;
1944 break;
1945 }
1946 /* FALLTHROUGH */
1947 default:
1948 delay = filedelay;
1949 }
1950 vn_syncer_add_to_worklist(newvp, delay);
1951 }
1952 buf_vlist_add(bp, newvp, BX_VNDIRTY);
1953 } else {
1954 buf_vlist_add(bp, newvp, BX_VNCLEAN);
1955
1956 if ((newvp->v_iflag & VI_ONWORKLST) &&
1957 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1958 mtx_lock(&sync_mtx);
1959 LIST_REMOVE(newvp, v_synclist);
1960 mtx_unlock(&sync_mtx);
1961 newvp->v_iflag &= ~VI_ONWORKLST;
1962 }
1963 }
1964 if (bp->b_vp != newvp) {
1965 bp->b_vp = newvp;
1966 vholdl(bp->b_vp);
1967 }
1968 VI_UNLOCK(newvp);
1969 splx(s);
1970 }
1971
1972 /*
1973 * Create a vnode for a device.
1974 * Used for mounting the root filesystem.
1975 */
1976 int
1977 bdevvp(dev, vpp)
1978 dev_t dev;
1979 struct vnode **vpp;
1980 {
1981 register struct vnode *vp;
1982 struct vnode *nvp;
1983 int error;
1984
1985 if (dev == NODEV) {
1986 *vpp = NULLVP;
1987 return (ENXIO);
1988 }
1989 if (vfinddev(dev, VCHR, vpp))
1990 return (0);
1991 error = getnewvnode("none", (struct mount *)0, spec_vnodeop_p, &nvp);
1992 if (error) {
1993 *vpp = NULLVP;
1994 return (error);
1995 }
1996 vp = nvp;
1997 vp->v_type = VCHR;
1998 addalias(vp, dev);
1999 *vpp = vp;
2000 return (0);
2001 }
2002
2003 static void
2004 v_incr_usecount(struct vnode *vp, int delta)
2005 {
2006 vp->v_usecount += delta;
2007 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2008 mtx_lock(&spechash_mtx);
2009 vp->v_rdev->si_usecount += delta;
2010 mtx_unlock(&spechash_mtx);
2011 }
2012 }
2013
2014 /*
2015 * Add vnode to the alias list hung off the dev_t.
2016 *
2017 * The reason for this gunk is that multiple vnodes can reference
2018 * the same physical device, so checking vp->v_usecount to see
2019 * how many users there are is inadequate; the v_usecount for
2020 * the vnodes need to be accumulated. vcount() does that.
2021 */
2022 struct vnode *
2023 addaliasu(nvp, nvp_rdev)
2024 struct vnode *nvp;
2025 udev_t nvp_rdev;
2026 {
2027 struct vnode *ovp;
2028 vop_t **ops;
2029 dev_t dev;
2030
2031 if (nvp->v_type == VBLK)
2032 return (nvp);
2033 if (nvp->v_type != VCHR)
2034 panic("addaliasu on non-special vnode");
2035 dev = udev2dev(nvp_rdev, 0);
2036 /*
2037 * Check to see if we have a bdevvp vnode with no associated
2038 * filesystem. If so, we want to associate the filesystem of
2039 * the new newly instigated vnode with the bdevvp vnode and
2040 * discard the newly created vnode rather than leaving the
2041 * bdevvp vnode lying around with no associated filesystem.
2042 */
2043 if (vfinddev(dev, nvp->v_type, &ovp) == 0 || ovp->v_data != NULL) {
2044 addalias(nvp, dev);
2045 return (nvp);
2046 }
2047 /*
2048 * Discard unneeded vnode, but save its node specific data.
2049 * Note that if there is a lock, it is carried over in the
2050 * node specific data to the replacement vnode.
2051 */
2052 vref(ovp);
2053 ovp->v_data = nvp->v_data;
2054 ovp->v_tag = nvp->v_tag;
2055 nvp->v_data = NULL;
2056 lockdestroy(ovp->v_vnlock);
2057 lockinit(ovp->v_vnlock, PVFS, nvp->v_vnlock->lk_wmesg,
2058 nvp->v_vnlock->lk_timo, nvp->v_vnlock->lk_flags & LK_EXTFLG_MASK);
2059 ops = ovp->v_op;
2060 ovp->v_op = nvp->v_op;
2061 if (VOP_ISLOCKED(nvp, curthread)) {
2062 VOP_UNLOCK(nvp, 0, curthread);
2063 vn_lock(ovp, LK_EXCLUSIVE | LK_RETRY, curthread);
2064 }
2065 nvp->v_op = ops;
2066 insmntque(ovp, nvp->v_mount);
2067 vrele(nvp);
2068 vgone(nvp);
2069 return (ovp);
2070 }
2071
2072 /* This is a local helper function that do the same as addaliasu, but for a
2073 * dev_t instead of an udev_t. */
2074 static void
2075 addalias(nvp, dev)
2076 struct vnode *nvp;
2077 dev_t dev;
2078 {
2079
2080 KASSERT(nvp->v_type == VCHR, ("addalias on non-special vnode"));
2081 nvp->v_rdev = dev;
2082 VI_LOCK(nvp);
2083 mtx_lock(&spechash_mtx);
2084 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
2085 dev->si_usecount += nvp->v_usecount;
2086 mtx_unlock(&spechash_mtx);
2087 VI_UNLOCK(nvp);
2088 }
2089
2090 /*
2091 * Grab a particular vnode from the free list, increment its
2092 * reference count and lock it. The vnode lock bit is set if the
2093 * vnode is being eliminated in vgone. The process is awakened
2094 * when the transition is completed, and an error returned to
2095 * indicate that the vnode is no longer usable (possibly having
2096 * been changed to a new filesystem type).
2097 */
2098 int
2099 vget(vp, flags, td)
2100 register struct vnode *vp;
2101 int flags;
2102 struct thread *td;
2103 {
2104 int error;
2105
2106 /*
2107 * If the vnode is in the process of being cleaned out for
2108 * another use, we wait for the cleaning to finish and then
2109 * return failure. Cleaning is determined by checking that
2110 * the VI_XLOCK flag is set.
2111 */
2112 if ((flags & LK_INTERLOCK) == 0)
2113 VI_LOCK(vp);
2114 if (vp->v_iflag & VI_XLOCK && vp->v_vxproc != curthread) {
2115 vp->v_iflag |= VI_XWANT;
2116 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vget", 0);
2117 return (ENOENT);
2118 }
2119
2120 v_incr_usecount(vp, 1);
2121
2122 if (VSHOULDBUSY(vp))
2123 vbusy(vp);
2124 if (flags & LK_TYPE_MASK) {
2125 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
2126 /*
2127 * must expand vrele here because we do not want
2128 * to call VOP_INACTIVE if the reference count
2129 * drops back to zero since it was never really
2130 * active. We must remove it from the free list
2131 * before sleeping so that multiple processes do
2132 * not try to recycle it.
2133 */
2134 VI_LOCK(vp);
2135 v_incr_usecount(vp, -1);
2136 if (VSHOULDFREE(vp))
2137 vfree(vp);
2138 else
2139 vlruvp(vp);
2140 VI_UNLOCK(vp);
2141 }
2142 return (error);
2143 }
2144 VI_UNLOCK(vp);
2145 return (0);
2146 }
2147
2148 /*
2149 * Increase the reference count of a vnode.
2150 */
2151 void
2152 vref(struct vnode *vp)
2153 {
2154 VI_LOCK(vp);
2155 v_incr_usecount(vp, 1);
2156 VI_UNLOCK(vp);
2157 }
2158
2159 /*
2160 * Return reference count of a vnode.
2161 *
2162 * The results of this call are only guaranteed when some mechanism other
2163 * than the VI lock is used to stop other processes from gaining references
2164 * to the vnode. This may be the case if the caller holds the only reference.
2165 * This is also useful when stale data is acceptable as race conditions may
2166 * be accounted for by some other means.
2167 */
2168 int
2169 vrefcnt(struct vnode *vp)
2170 {
2171 int usecnt;
2172
2173 VI_LOCK(vp);
2174 usecnt = vp->v_usecount;
2175 VI_UNLOCK(vp);
2176
2177 return (usecnt);
2178 }
2179
2180
2181 /*
2182 * Vnode put/release.
2183 * If count drops to zero, call inactive routine and return to freelist.
2184 */
2185 void
2186 vrele(vp)
2187 struct vnode *vp;
2188 {
2189 struct thread *td = curthread; /* XXX */
2190
2191 KASSERT(vp != NULL, ("vrele: null vp"));
2192
2193 VI_LOCK(vp);
2194
2195 /* Skip this v_writecount check if we're going to panic below. */
2196 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1,
2197 ("vrele: missed vn_close"));
2198
2199 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2200 vp->v_usecount == 1)) {
2201 v_incr_usecount(vp, -1);
2202 VI_UNLOCK(vp);
2203
2204 return;
2205 }
2206
2207 if (vp->v_usecount == 1) {
2208 v_incr_usecount(vp, -1);
2209 /*
2210 * We must call VOP_INACTIVE with the node locked. Mark
2211 * as VI_DOINGINACT to avoid recursion.
2212 */
2213 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) {
2214 VI_LOCK(vp);
2215 vp->v_iflag |= VI_DOINGINACT;
2216 VI_UNLOCK(vp);
2217 VOP_INACTIVE(vp, td);
2218 VI_LOCK(vp);
2219 KASSERT(vp->v_iflag & VI_DOINGINACT,
2220 ("vrele: lost VI_DOINGINACT"));
2221 vp->v_iflag &= ~VI_DOINGINACT;
2222 VI_UNLOCK(vp);
2223 }
2224 VI_LOCK(vp);
2225 if (VSHOULDFREE(vp))
2226 vfree(vp);
2227 else
2228 vlruvp(vp);
2229 VI_UNLOCK(vp);
2230
2231 } else {
2232 #ifdef DIAGNOSTIC
2233 vprint("vrele: negative ref count", vp);
2234 #endif
2235 VI_UNLOCK(vp);
2236 panic("vrele: negative ref cnt");
2237 }
2238 }
2239
2240 /*
2241 * Release an already locked vnode. This give the same effects as
2242 * unlock+vrele(), but takes less time and avoids releasing and
2243 * re-aquiring the lock (as vrele() aquires the lock internally.)
2244 */
2245 void
2246 vput(vp)
2247 struct vnode *vp;
2248 {
2249 struct thread *td = curthread; /* XXX */
2250
2251 GIANT_REQUIRED;
2252
2253 KASSERT(vp != NULL, ("vput: null vp"));
2254 VI_LOCK(vp);
2255 /* Skip this v_writecount check if we're going to panic below. */
2256 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1,
2257 ("vput: missed vn_close"));
2258
2259 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2260 vp->v_usecount == 1)) {
2261 v_incr_usecount(vp, -1);
2262 VOP_UNLOCK(vp, LK_INTERLOCK, td);
2263 return;
2264 }
2265
2266 if (vp->v_usecount == 1) {
2267 v_incr_usecount(vp, -1);
2268 /*
2269 * We must call VOP_INACTIVE with the node locked, so
2270 * we just need to release the vnode mutex. Mark as
2271 * as VI_DOINGINACT to avoid recursion.
2272 */
2273 vp->v_iflag |= VI_DOINGINACT;
2274 VI_UNLOCK(vp);
2275 VOP_INACTIVE(vp, td);
2276 VI_LOCK(vp);
2277 KASSERT(vp->v_iflag & VI_DOINGINACT,
2278 ("vput: lost VI_DOINGINACT"));
2279 vp->v_iflag &= ~VI_DOINGINACT;
2280 if (VSHOULDFREE(vp))
2281 vfree(vp);
2282 else
2283 vlruvp(vp);
2284 VI_UNLOCK(vp);
2285
2286 } else {
2287 #ifdef DIAGNOSTIC
2288 vprint("vput: negative ref count", vp);
2289 #endif
2290 panic("vput: negative ref cnt");
2291 }
2292 }
2293
2294 /*
2295 * Somebody doesn't want the vnode recycled.
2296 */
2297 void
2298 vhold(struct vnode *vp)
2299 {
2300 VI_LOCK(vp);
2301 vholdl(vp);
2302 VI_UNLOCK(vp);
2303 }
2304
2305 void
2306 vholdl(vp)
2307 register struct vnode *vp;
2308 {
2309 int s;
2310
2311 s = splbio();
2312 vp->v_holdcnt++;
2313 if (VSHOULDBUSY(vp))
2314 vbusy(vp);
2315 splx(s);
2316 }
2317
2318 /*
2319 * Note that there is one less who cares about this vnode. vdrop() is the
2320 * opposite of vhold().
2321 */
2322 void
2323 vdrop(struct vnode *vp)
2324 {
2325 VI_LOCK(vp);
2326 vdropl(vp);
2327 VI_UNLOCK(vp);
2328 }
2329
2330 void
2331 vdropl(vp)
2332 register struct vnode *vp;
2333 {
2334 int s;
2335
2336 s = splbio();
2337 if (vp->v_holdcnt <= 0)
2338 panic("vdrop: holdcnt");
2339 vp->v_holdcnt--;
2340 if (VSHOULDFREE(vp))
2341 vfree(vp);
2342 else
2343 vlruvp(vp);
2344 splx(s);
2345 }
2346
2347 /*
2348 * Remove any vnodes in the vnode table belonging to mount point mp.
2349 *
2350 * If FORCECLOSE is not specified, there should not be any active ones,
2351 * return error if any are found (nb: this is a user error, not a
2352 * system error). If FORCECLOSE is specified, detach any active vnodes
2353 * that are found.
2354 *
2355 * If WRITECLOSE is set, only flush out regular file vnodes open for
2356 * writing.
2357 *
2358 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2359 *
2360 * `rootrefs' specifies the base reference count for the root vnode
2361 * of this filesystem. The root vnode is considered busy if its
2362 * v_usecount exceeds this value. On a successful return, vflush()
2363 * will call vrele() on the root vnode exactly rootrefs times.
2364 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2365 * be zero.
2366 */
2367 #ifdef DIAGNOSTIC
2368 static int busyprt = 0; /* print out busy vnodes */
2369 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
2370 #endif
2371
2372 int
2373 vflush(mp, rootrefs, flags)
2374 struct mount *mp;
2375 int rootrefs;
2376 int flags;
2377 {
2378 struct thread *td = curthread; /* XXX */
2379 struct vnode *vp, *nvp, *rootvp = NULL;
2380 struct vattr vattr;
2381 int busy = 0, error;
2382
2383 if (rootrefs > 0) {
2384 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2385 ("vflush: bad args"));
2386 /*
2387 * Get the filesystem root vnode. We can vput() it
2388 * immediately, since with rootrefs > 0, it won't go away.
2389 */
2390 if ((error = VFS_ROOT(mp, &rootvp)) != 0)
2391 return (error);
2392 vput(rootvp);
2393
2394 }
2395 mtx_lock(&mntvnode_mtx);
2396 loop:
2397 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2398 /*
2399 * Make sure this vnode wasn't reclaimed in getnewvnode().
2400 * Start over if it has (it won't be on the list anymore).
2401 */
2402 if (vp->v_mount != mp)
2403 goto loop;
2404 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2405
2406 VI_LOCK(vp);
2407 mtx_unlock(&mntvnode_mtx);
2408 vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, td);
2409 /*
2410 * This vnode could have been reclaimed while we were
2411 * waiting for the lock since we are not holding a
2412 * reference.
2413 * Start over if the vnode was reclaimed.
2414 */
2415 if (vp->v_mount != mp) {
2416 VOP_UNLOCK(vp, 0, td);
2417 mtx_lock(&mntvnode_mtx);
2418 goto loop;
2419 }
2420 /*
2421 * Skip over a vnodes marked VV_SYSTEM.
2422 */
2423 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2424 VOP_UNLOCK(vp, 0, td);
2425 mtx_lock(&mntvnode_mtx);
2426 continue;
2427 }
2428 /*
2429 * If WRITECLOSE is set, flush out unlinked but still open
2430 * files (even if open only for reading) and regular file
2431 * vnodes open for writing.
2432 */
2433 if (flags & WRITECLOSE) {
2434 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
2435 VI_LOCK(vp);
2436
2437 if ((vp->v_type == VNON ||
2438 (error == 0 && vattr.va_nlink > 0)) &&
2439 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2440 VOP_UNLOCK(vp, LK_INTERLOCK, td);
2441 mtx_lock(&mntvnode_mtx);
2442 continue;
2443 }
2444 } else
2445 VI_LOCK(vp);
2446
2447 VOP_UNLOCK(vp, 0, td);
2448
2449 /*
2450 * With v_usecount == 0, all we need to do is clear out the
2451 * vnode data structures and we are done.
2452 */
2453 if (vp->v_usecount == 0) {
2454 vgonel(vp, td);
2455 mtx_lock(&mntvnode_mtx);
2456 continue;
2457 }
2458
2459 /*
2460 * If FORCECLOSE is set, forcibly close the vnode. For block
2461 * or character devices, revert to an anonymous device. For
2462 * all other files, just kill them.
2463 */
2464 if (flags & FORCECLOSE) {
2465 if (vp->v_type != VCHR) {
2466 vgonel(vp, td);
2467 } else {
2468 vclean(vp, 0, td);
2469 VI_UNLOCK(vp);
2470 vp->v_op = spec_vnodeop_p;
2471 insmntque(vp, (struct mount *) 0);
2472 }
2473 mtx_lock(&mntvnode_mtx);
2474 continue;
2475 }
2476 #ifdef DIAGNOSTIC
2477 if (busyprt)
2478 vprint("vflush: busy vnode", vp);
2479 #endif
2480 VI_UNLOCK(vp);
2481 mtx_lock(&mntvnode_mtx);
2482 busy++;
2483 }
2484 mtx_unlock(&mntvnode_mtx);
2485 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2486 /*
2487 * If just the root vnode is busy, and if its refcount
2488 * is equal to `rootrefs', then go ahead and kill it.
2489 */
2490 VI_LOCK(rootvp);
2491 KASSERT(busy > 0, ("vflush: not busy"));
2492 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs"));
2493 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2494 vgonel(rootvp, td);
2495 busy = 0;
2496 } else
2497 VI_UNLOCK(rootvp);
2498 }
2499 if (busy)
2500 return (EBUSY);
2501 for (; rootrefs > 0; rootrefs--)
2502 vrele(rootvp);
2503 return (0);
2504 }
2505
2506 /*
2507 * This moves a now (likely recyclable) vnode to the end of the
2508 * mountlist. XXX However, it is temporarily disabled until we
2509 * can clean up ffs_sync() and friends, which have loop restart
2510 * conditions which this code causes to operate O(N^2).
2511 */
2512 static void
2513 vlruvp(struct vnode *vp)
2514 {
2515 #if 0
2516 struct mount *mp;
2517
2518 if ((mp = vp->v_mount) != NULL) {
2519 mtx_lock(&mntvnode_mtx);
2520 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
2521 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
2522 mtx_unlock(&mntvnode_mtx);
2523 }
2524 #endif
2525 }
2526
2527 /*
2528 * Disassociate the underlying filesystem from a vnode.
2529 */
2530 static void
2531 vclean(vp, flags, td)
2532 struct vnode *vp;
2533 int flags;
2534 struct thread *td;
2535 {
2536 int active;
2537
2538 ASSERT_VI_LOCKED(vp, "vclean");
2539 /*
2540 * Check to see if the vnode is in use. If so we have to reference it
2541 * before we clean it out so that its count cannot fall to zero and
2542 * generate a race against ourselves to recycle it.
2543 */
2544 if ((active = vp->v_usecount))
2545 v_incr_usecount(vp, 1);
2546
2547 /*
2548 * Prevent the vnode from being recycled or brought into use while we
2549 * clean it out.
2550 */
2551 if (vp->v_iflag & VI_XLOCK)
2552 panic("vclean: deadlock");
2553 vp->v_iflag |= VI_XLOCK;
2554 vp->v_vxproc = curthread;
2555 /*
2556 * Even if the count is zero, the VOP_INACTIVE routine may still
2557 * have the object locked while it cleans it out. The VOP_LOCK
2558 * ensures that the VOP_INACTIVE routine is done with its work.
2559 * For active vnodes, it ensures that no other activity can
2560 * occur while the underlying object is being cleaned out.
2561 */
2562 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td);
2563
2564 /*
2565 * Clean out any buffers associated with the vnode.
2566 * If the flush fails, just toss the buffers.
2567 */
2568 if (flags & DOCLOSE) {
2569 struct buf *bp;
2570 VI_LOCK(vp);
2571 bp = TAILQ_FIRST(&vp->v_dirtyblkhd);
2572 VI_UNLOCK(vp);
2573 if (bp != NULL)
2574 (void) vn_write_suspend_wait(vp, NULL, V_WAIT);
2575 if (vinvalbuf(vp, V_SAVE, NOCRED, td, 0, 0) != 0)
2576 vinvalbuf(vp, 0, NOCRED, td, 0, 0);
2577 }
2578
2579 VOP_DESTROYVOBJECT(vp);
2580
2581 /*
2582 * Any other processes trying to obtain this lock must first
2583 * wait for VXLOCK to clear, then call the new lock operation.
2584 */
2585 VOP_UNLOCK(vp, 0, td);
2586
2587 /*
2588 * If purging an active vnode, it must be closed and
2589 * deactivated before being reclaimed. Note that the
2590 * VOP_INACTIVE will unlock the vnode.
2591 */
2592 if (active) {
2593 if (flags & DOCLOSE)
2594 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2595 VI_LOCK(vp);
2596 if ((vp->v_iflag & VI_DOINGINACT) == 0) {
2597 vp->v_iflag |= VI_DOINGINACT;
2598 VI_UNLOCK(vp);
2599 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0)
2600 panic("vclean: cannot relock.");
2601 VOP_INACTIVE(vp, td);
2602 VI_LOCK(vp);
2603 KASSERT(vp->v_iflag & VI_DOINGINACT,
2604 ("vclean: lost VI_DOINGINACT"));
2605 vp->v_iflag &= ~VI_DOINGINACT;
2606 }
2607 VI_UNLOCK(vp);
2608 }
2609
2610 /*
2611 * Reclaim the vnode.
2612 */
2613 if (VOP_RECLAIM(vp, td))
2614 panic("vclean: cannot reclaim");
2615
2616 if (active) {
2617 /*
2618 * Inline copy of vrele() since VOP_INACTIVE
2619 * has already been called.
2620 */
2621 VI_LOCK(vp);
2622 v_incr_usecount(vp, -1);
2623 if (vp->v_usecount <= 0) {
2624 #ifdef DIAGNOSTIC
2625 if (vp->v_usecount < 0 || vp->v_writecount != 0) {
2626 vprint("vclean: bad ref count", vp);
2627 panic("vclean: ref cnt");
2628 }
2629 #endif
2630 vfree(vp);
2631 }
2632 VI_UNLOCK(vp);
2633 }
2634
2635 cache_purge(vp);
2636 VI_LOCK(vp);
2637 if (VSHOULDFREE(vp))
2638 vfree(vp);
2639
2640 /*
2641 * Done with purge, reset to the standard lock and
2642 * notify sleepers of the grim news.
2643 */
2644 vp->v_vnlock = &vp->v_lock;
2645 vp->v_op = dead_vnodeop_p;
2646 if (vp->v_pollinfo != NULL)
2647 vn_pollgone(vp);
2648 vp->v_tag = "none";
2649 vp->v_iflag &= ~VI_XLOCK;
2650 vp->v_vxproc = NULL;
2651 if (vp->v_iflag & VI_XWANT) {
2652 vp->v_iflag &= ~VI_XWANT;
2653 wakeup(vp);
2654 }
2655 }
2656
2657 /*
2658 * Eliminate all activity associated with the requested vnode
2659 * and with all vnodes aliased to the requested vnode.
2660 */
2661 int
2662 vop_revoke(ap)
2663 struct vop_revoke_args /* {
2664 struct vnode *a_vp;
2665 int a_flags;
2666 } */ *ap;
2667 {
2668 struct vnode *vp, *vq;
2669 dev_t dev;
2670
2671 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
2672 vp = ap->a_vp;
2673 KASSERT((vp->v_type == VCHR), ("vop_revoke: not VCHR"));
2674
2675 VI_LOCK(vp);
2676 /*
2677 * If a vgone (or vclean) is already in progress,
2678 * wait until it is done and return.
2679 */
2680 if (vp->v_iflag & VI_XLOCK) {
2681 vp->v_iflag |= VI_XWANT;
2682 msleep(vp, VI_MTX(vp), PINOD | PDROP,
2683 "vop_revokeall", 0);
2684 return (0);
2685 }
2686 VI_UNLOCK(vp);
2687 dev = vp->v_rdev;
2688 for (;;) {
2689 mtx_lock(&spechash_mtx);
2690 vq = SLIST_FIRST(&dev->si_hlist);
2691 mtx_unlock(&spechash_mtx);
2692 if (!vq)
2693 break;
2694 vgone(vq);
2695 }
2696 return (0);
2697 }
2698
2699 /*
2700 * Recycle an unused vnode to the front of the free list.
2701 * Release the passed interlock if the vnode will be recycled.
2702 */
2703 int
2704 vrecycle(vp, inter_lkp, td)
2705 struct vnode *vp;
2706 struct mtx *inter_lkp;
2707 struct thread *td;
2708 {
2709
2710 VI_LOCK(vp);
2711 if (vp->v_usecount == 0) {
2712 if (inter_lkp) {
2713 mtx_unlock(inter_lkp);
2714 }
2715 vgonel(vp, td);
2716 return (1);
2717 }
2718 VI_UNLOCK(vp);
2719 return (0);
2720 }
2721
2722 /*
2723 * Eliminate all activity associated with a vnode
2724 * in preparation for reuse.
2725 */
2726 void
2727 vgone(vp)
2728 register struct vnode *vp;
2729 {
2730 struct thread *td = curthread; /* XXX */
2731
2732 VI_LOCK(vp);
2733 vgonel(vp, td);
2734 }
2735
2736 /*
2737 * vgone, with the vp interlock held.
2738 */
2739 void
2740 vgonel(vp, td)
2741 struct vnode *vp;
2742 struct thread *td;
2743 {
2744 int s;
2745
2746 /*
2747 * If a vgone (or vclean) is already in progress,
2748 * wait until it is done and return.
2749 */
2750 ASSERT_VI_LOCKED(vp, "vgonel");
2751 if (vp->v_iflag & VI_XLOCK) {
2752 vp->v_iflag |= VI_XWANT;
2753 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vgone", 0);
2754 return;
2755 }
2756
2757 /*
2758 * Clean out the filesystem specific data.
2759 */
2760 vclean(vp, DOCLOSE, td);
2761 VI_UNLOCK(vp);
2762
2763 /*
2764 * Delete from old mount point vnode list, if on one.
2765 */
2766 if (vp->v_mount != NULL)
2767 insmntque(vp, (struct mount *)0);
2768 /*
2769 * If special device, remove it from special device alias list
2770 * if it is on one.
2771 */
2772 if (vp->v_type == VCHR && vp->v_rdev != NULL && vp->v_rdev != NODEV) {
2773 VI_LOCK(vp);
2774 mtx_lock(&spechash_mtx);
2775 SLIST_REMOVE(&vp->v_rdev->si_hlist, vp, vnode, v_specnext);
2776 vp->v_rdev->si_usecount -= vp->v_usecount;
2777 mtx_unlock(&spechash_mtx);
2778 VI_UNLOCK(vp);
2779 vp->v_rdev = NULL;
2780 }
2781
2782 /*
2783 * If it is on the freelist and not already at the head,
2784 * move it to the head of the list. The test of the
2785 * VDOOMED flag and the reference count of zero is because
2786 * it will be removed from the free list by getnewvnode,
2787 * but will not have its reference count incremented until
2788 * after calling vgone. If the reference count were
2789 * incremented first, vgone would (incorrectly) try to
2790 * close the previous instance of the underlying object.
2791 */
2792 VI_LOCK(vp);
2793 if (vp->v_usecount == 0 && !(vp->v_iflag & VI_DOOMED)) {
2794 s = splbio();
2795 mtx_lock(&vnode_free_list_mtx);
2796 if (vp->v_iflag & VI_FREE) {
2797 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2798 } else {
2799 vp->v_iflag |= VI_FREE;
2800 freevnodes++;
2801 }
2802 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2803 mtx_unlock(&vnode_free_list_mtx);
2804 splx(s);
2805 }
2806
2807 vp->v_type = VBAD;
2808 VI_UNLOCK(vp);
2809 }
2810
2811 /*
2812 * Lookup a vnode by device number.
2813 */
2814 int
2815 vfinddev(dev, type, vpp)
2816 dev_t dev;
2817 enum vtype type;
2818 struct vnode **vpp;
2819 {
2820 struct vnode *vp;
2821
2822 mtx_lock(&spechash_mtx);
2823 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
2824 if (type == vp->v_type) {
2825 *vpp = vp;
2826 mtx_unlock(&spechash_mtx);
2827 return (1);
2828 }
2829 }
2830 mtx_unlock(&spechash_mtx);
2831 return (0);
2832 }
2833
2834 /*
2835 * Calculate the total number of references to a special device.
2836 */
2837 int
2838 vcount(vp)
2839 struct vnode *vp;
2840 {
2841 int count;
2842
2843 mtx_lock(&spechash_mtx);
2844 count = vp->v_rdev->si_usecount;
2845 mtx_unlock(&spechash_mtx);
2846 return (count);
2847 }
2848
2849 /*
2850 * Same as above, but using the dev_t as argument
2851 */
2852 int
2853 count_dev(dev)
2854 dev_t dev;
2855 {
2856 struct vnode *vp;
2857
2858 vp = SLIST_FIRST(&dev->si_hlist);
2859 if (vp == NULL)
2860 return (0);
2861 return(vcount(vp));
2862 }
2863
2864 /*
2865 * Print out a description of a vnode.
2866 */
2867 static char *typename[] =
2868 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2869
2870 void
2871 vprint(label, vp)
2872 char *label;
2873 struct vnode *vp;
2874 {
2875 char buf[96];
2876
2877 if (label != NULL)
2878 printf("%s: %p: ", label, (void *)vp);
2879 else
2880 printf("%p: ", (void *)vp);
2881 printf("tag %s, type %s, usecount %d, writecount %d, refcount %d,",
2882 vp->v_tag, typename[vp->v_type], vp->v_usecount,
2883 vp->v_writecount, vp->v_holdcnt);
2884 buf[0] = '\0';
2885 if (vp->v_vflag & VV_ROOT)
2886 strcat(buf, "|VV_ROOT");
2887 if (vp->v_vflag & VV_TEXT)
2888 strcat(buf, "|VV_TEXT");
2889 if (vp->v_vflag & VV_SYSTEM)
2890 strcat(buf, "|VV_SYSTEM");
2891 if (vp->v_iflag & VI_XLOCK)
2892 strcat(buf, "|VI_XLOCK");
2893 if (vp->v_iflag & VI_XWANT)
2894 strcat(buf, "|VI_XWANT");
2895 if (vp->v_iflag & VI_BWAIT)
2896 strcat(buf, "|VI_BWAIT");
2897 if (vp->v_iflag & VI_DOOMED)
2898 strcat(buf, "|VI_DOOMED");
2899 if (vp->v_iflag & VI_FREE)
2900 strcat(buf, "|VI_FREE");
2901 if (vp->v_vflag & VV_OBJBUF)
2902 strcat(buf, "|VV_OBJBUF");
2903 if (buf[0] != '\0')
2904 printf(" flags (%s),", &buf[1]);
2905 lockmgr_printinfo(vp->v_vnlock);
2906 printf("\n");
2907 if (vp->v_data != NULL)
2908 VOP_PRINT(vp);
2909 }
2910
2911 #ifdef DDB
2912 #include <ddb/ddb.h>
2913 /*
2914 * List all of the locked vnodes in the system.
2915 * Called when debugging the kernel.
2916 */
2917 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2918 {
2919 struct mount *mp, *nmp;
2920 struct vnode *vp;
2921
2922 /*
2923 * Note: because this is DDB, we can't obey the locking semantics
2924 * for these structures, which means we could catch an inconsistent
2925 * state and dereference a nasty pointer. Not much to be done
2926 * about that.
2927 */
2928 printf("Locked vnodes\n");
2929 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2930 nmp = TAILQ_NEXT(mp, mnt_list);
2931 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2932 if (VOP_ISLOCKED(vp, NULL))
2933 vprint(NULL, vp);
2934 }
2935 nmp = TAILQ_NEXT(mp, mnt_list);
2936 }
2937 }
2938 #endif
2939
2940 /*
2941 * Fill in a struct xvfsconf based on a struct vfsconf.
2942 */
2943 static void
2944 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2945 {
2946
2947 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2948 xvfsp->vfc_typenum = vfsp->vfc_typenum;
2949 xvfsp->vfc_refcount = vfsp->vfc_refcount;
2950 xvfsp->vfc_flags = vfsp->vfc_flags;
2951 /*
2952 * These are unused in userland, we keep them
2953 * to not break binary compatibility.
2954 */
2955 xvfsp->vfc_vfsops = NULL;
2956 xvfsp->vfc_next = NULL;
2957 }
2958
2959 static int
2960 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2961 {
2962 struct vfsconf *vfsp;
2963 struct xvfsconf *xvfsp;
2964 int cnt, error, i;
2965
2966 cnt = 0;
2967 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
2968 cnt++;
2969 xvfsp = malloc(sizeof(struct xvfsconf) * cnt, M_TEMP, M_WAITOK);
2970 /*
2971 * Handle the race that we will have here when struct vfsconf
2972 * will be locked down by using both cnt and checking vfc_next
2973 * against NULL to determine the end of the loop. The race will
2974 * happen because we will have to unlock before calling malloc().
2975 * We are protected by Giant for now.
2976 */
2977 i = 0;
2978 for (vfsp = vfsconf; vfsp != NULL && i < cnt; vfsp = vfsp->vfc_next) {
2979 vfsconf2x(vfsp, xvfsp + i);
2980 i++;
2981 }
2982 error = SYSCTL_OUT(req, xvfsp, sizeof(struct xvfsconf) * i);
2983 free(xvfsp, M_TEMP);
2984 return (error);
2985 }
2986
2987 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
2988 "S,xvfsconf", "List of all configured filesystems");
2989
2990 /*
2991 * Top level filesystem related information gathering.
2992 */
2993 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
2994
2995 static int
2996 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2997 {
2998 int *name = (int *)arg1 - 1; /* XXX */
2999 u_int namelen = arg2 + 1; /* XXX */
3000 struct vfsconf *vfsp;
3001 struct xvfsconf xvfsp;
3002
3003 printf("WARNING: userland calling deprecated sysctl, "
3004 "please rebuild world\n");
3005
3006 #if 1 || defined(COMPAT_PRELITE2)
3007 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3008 if (namelen == 1)
3009 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3010 #endif
3011
3012 switch (name[1]) {
3013 case VFS_MAXTYPENUM:
3014 if (namelen != 2)
3015 return (ENOTDIR);
3016 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3017 case VFS_CONF:
3018 if (namelen != 3)
3019 return (ENOTDIR); /* overloaded */
3020 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
3021 if (vfsp->vfc_typenum == name[2])
3022 break;
3023 if (vfsp == NULL)
3024 return (EOPNOTSUPP);
3025 vfsconf2x(vfsp, &xvfsp);
3026 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3027 }
3028 return (EOPNOTSUPP);
3029 }
3030
3031 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, vfs_sysctl,
3032 "Generic filesystem");
3033
3034 #if 1 || defined(COMPAT_PRELITE2)
3035
3036 static int
3037 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3038 {
3039 int error;
3040 struct vfsconf *vfsp;
3041 struct ovfsconf ovfs;
3042
3043 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
3044 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3045 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3046 ovfs.vfc_index = vfsp->vfc_typenum;
3047 ovfs.vfc_refcount = vfsp->vfc_refcount;
3048 ovfs.vfc_flags = vfsp->vfc_flags;
3049 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3050 if (error)
3051 return error;
3052 }
3053 return 0;
3054 }
3055
3056 #endif /* 1 || COMPAT_PRELITE2 */
3057
3058 #define KINFO_VNODESLOP 10
3059 #ifdef notyet
3060 /*
3061 * Dump vnode list (via sysctl).
3062 */
3063 /* ARGSUSED */
3064 static int
3065 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3066 {
3067 struct xvnode *xvn;
3068 struct thread *td = req->td;
3069 struct mount *mp;
3070 struct vnode *vp;
3071 int error, len, n;
3072
3073 /*
3074 * Stale numvnodes access is not fatal here.
3075 */
3076 req->lock = 0;
3077 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3078 if (!req->oldptr)
3079 /* Make an estimate */
3080 return (SYSCTL_OUT(req, 0, len));
3081
3082 sysctl_wire_old_buffer(req, 0);
3083 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3084 n = 0;
3085 mtx_lock(&mountlist_mtx);
3086 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3087 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td))
3088 continue;
3089 mtx_lock(&mntvnode_mtx);
3090 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3091 if (n == len)
3092 break;
3093 vref(vp);
3094 xvn[n].xv_size = sizeof *xvn;
3095 xvn[n].xv_vnode = vp;
3096 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3097 XV_COPY(usecount);
3098 XV_COPY(writecount);
3099 XV_COPY(holdcnt);
3100 XV_COPY(id);
3101 XV_COPY(mount);
3102 XV_COPY(numoutput);
3103 XV_COPY(type);
3104 #undef XV_COPY
3105 xvn[n].xv_flag = vp->v_vflag;
3106
3107 switch (vp->v_type) {
3108 case VREG:
3109 case VDIR:
3110 case VLNK:
3111 xvn[n].xv_dev = vp->v_cachedfs;
3112 xvn[n].xv_ino = vp->v_cachedid;
3113 break;
3114 case VBLK:
3115 case VCHR:
3116 if (vp->v_rdev == NULL) {
3117 vrele(vp);
3118 continue;
3119 }
3120 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3121 break;
3122 case VSOCK:
3123 xvn[n].xv_socket = vp->v_socket;
3124 break;
3125 case VFIFO:
3126 xvn[n].xv_fifo = vp->v_fifoinfo;
3127 break;
3128 case VNON:
3129 case VBAD:
3130 default:
3131 /* shouldn't happen? */
3132 vrele(vp);
3133 continue;
3134 }
3135 vrele(vp);
3136 ++n;
3137 }
3138 mtx_unlock(&mntvnode_mtx);
3139 mtx_lock(&mountlist_mtx);
3140 vfs_unbusy(mp, td);
3141 if (n == len)
3142 break;
3143 }
3144 mtx_unlock(&mountlist_mtx);
3145
3146 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3147 free(xvn, M_TEMP);
3148 return (error);
3149 }
3150
3151 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3152 0, 0, sysctl_vnode, "S,xvnode", "");
3153 #endif
3154
3155 /*
3156 * Check to see if a filesystem is mounted on a block device.
3157 */
3158 int
3159 vfs_mountedon(vp)
3160 struct vnode *vp;
3161 {
3162
3163 if (vp->v_rdev->si_mountpoint != NULL)
3164 return (EBUSY);
3165 return (0);
3166 }
3167
3168 /*
3169 * Unmount all filesystems. The list is traversed in reverse order
3170 * of mounting to avoid dependencies.
3171 */
3172 void
3173 vfs_unmountall()
3174 {
3175 struct mount *mp;
3176 struct thread *td;
3177 int error;
3178
3179 if (curthread != NULL)
3180 td = curthread;
3181 else
3182 td = FIRST_THREAD_IN_PROC(initproc); /* XXX XXX proc0? */
3183 /*
3184 * Since this only runs when rebooting, it is not interlocked.
3185 */
3186 while(!TAILQ_EMPTY(&mountlist)) {
3187 mp = TAILQ_LAST(&mountlist, mntlist);
3188 error = dounmount(mp, MNT_FORCE, td);
3189 if (error) {
3190 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3191 printf("unmount of %s failed (",
3192 mp->mnt_stat.f_mntonname);
3193 if (error == EBUSY)
3194 printf("BUSY)\n");
3195 else
3196 printf("%d)\n", error);
3197 } else {
3198 /* The unmount has removed mp from the mountlist */
3199 }
3200 }
3201 }
3202
3203 /*
3204 * perform msync on all vnodes under a mount point
3205 * the mount point must be locked.
3206 */
3207 void
3208 vfs_msync(struct mount *mp, int flags)
3209 {
3210 struct vnode *vp, *nvp;
3211 struct vm_object *obj;
3212 int tries;
3213
3214 GIANT_REQUIRED;
3215
3216 tries = 5;
3217 mtx_lock(&mntvnode_mtx);
3218 loop:
3219 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
3220 if (vp->v_mount != mp) {
3221 if (--tries > 0)
3222 goto loop;
3223 break;
3224 }
3225 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
3226
3227 VI_LOCK(vp);
3228 if (vp->v_iflag & VI_XLOCK) { /* XXX: what if MNT_WAIT? */
3229 VI_UNLOCK(vp);
3230 continue;
3231 }
3232
3233 if ((vp->v_iflag & VI_OBJDIRTY) &&
3234 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
3235 mtx_unlock(&mntvnode_mtx);
3236 if (!vget(vp,
3237 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3238 curthread)) {
3239 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3240 vput(vp);
3241 mtx_lock(&mntvnode_mtx);
3242 continue;
3243 }
3244
3245 if (VOP_GETVOBJECT(vp, &obj) == 0) {
3246 VM_OBJECT_LOCK(obj);
3247 vm_object_page_clean(obj, 0, 0,
3248 flags == MNT_WAIT ?
3249 OBJPC_SYNC : OBJPC_NOSYNC);
3250 VM_OBJECT_UNLOCK(obj);
3251 }
3252 vput(vp);
3253 }
3254 mtx_lock(&mntvnode_mtx);
3255 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
3256 if (--tries > 0)
3257 goto loop;
3258 break;
3259 }
3260 } else
3261 VI_UNLOCK(vp);
3262 }
3263 mtx_unlock(&mntvnode_mtx);
3264 }
3265
3266 /*
3267 * Create the VM object needed for VMIO and mmap support. This
3268 * is done for all VREG files in the system. Some filesystems might
3269 * afford the additional metadata buffering capability of the
3270 * VMIO code by making the device node be VMIO mode also.
3271 *
3272 * vp must be locked when vfs_object_create is called.
3273 */
3274 int
3275 vfs_object_create(vp, td, cred)
3276 struct vnode *vp;
3277 struct thread *td;
3278 struct ucred *cred;
3279 {
3280 GIANT_REQUIRED;
3281 return (VOP_CREATEVOBJECT(vp, cred, td));
3282 }
3283
3284 /*
3285 * Mark a vnode as free, putting it up for recycling.
3286 */
3287 void
3288 vfree(vp)
3289 struct vnode *vp;
3290 {
3291 int s;
3292
3293 ASSERT_VI_LOCKED(vp, "vfree");
3294 s = splbio();
3295 mtx_lock(&vnode_free_list_mtx);
3296 KASSERT((vp->v_iflag & VI_FREE) == 0, ("vnode already free"));
3297 if (vp->v_iflag & VI_AGE) {
3298 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
3299 } else {
3300 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
3301 }
3302 freevnodes++;
3303 mtx_unlock(&vnode_free_list_mtx);
3304 vp->v_iflag &= ~VI_AGE;
3305 vp->v_iflag |= VI_FREE;
3306 splx(s);
3307 }
3308
3309 /*
3310 * Opposite of vfree() - mark a vnode as in use.
3311 */
3312 void
3313 vbusy(vp)
3314 struct vnode *vp;
3315 {
3316 int s;
3317
3318 s = splbio();
3319 ASSERT_VI_LOCKED(vp, "vbusy");
3320 KASSERT((vp->v_iflag & VI_FREE) != 0, ("vnode not free"));
3321
3322 mtx_lock(&vnode_free_list_mtx);
3323 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3324 freevnodes--;
3325 mtx_unlock(&vnode_free_list_mtx);
3326
3327 vp->v_iflag &= ~(VI_FREE|VI_AGE);
3328 splx(s);
3329 }
3330
3331 /*
3332 * Record a process's interest in events which might happen to
3333 * a vnode. Because poll uses the historic select-style interface
3334 * internally, this routine serves as both the ``check for any
3335 * pending events'' and the ``record my interest in future events''
3336 * functions. (These are done together, while the lock is held,
3337 * to avoid race conditions.)
3338 */
3339 int
3340 vn_pollrecord(vp, td, events)
3341 struct vnode *vp;
3342 struct thread *td;
3343 short events;
3344 {
3345
3346 if (vp->v_pollinfo == NULL)
3347 v_addpollinfo(vp);
3348 mtx_lock(&vp->v_pollinfo->vpi_lock);
3349 if (vp->v_pollinfo->vpi_revents & events) {
3350 /*
3351 * This leaves events we are not interested
3352 * in available for the other process which
3353 * which presumably had requested them
3354 * (otherwise they would never have been
3355 * recorded).
3356 */
3357 events &= vp->v_pollinfo->vpi_revents;
3358 vp->v_pollinfo->vpi_revents &= ~events;
3359
3360 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3361 return events;
3362 }
3363 vp->v_pollinfo->vpi_events |= events;
3364 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3365 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3366 return 0;
3367 }
3368
3369 /*
3370 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
3371 * it is possible for us to miss an event due to race conditions, but
3372 * that condition is expected to be rare, so for the moment it is the
3373 * preferred interface.
3374 */
3375 void
3376 vn_pollevent(vp, events)
3377 struct vnode *vp;
3378 short events;
3379 {
3380
3381 if (vp->v_pollinfo == NULL)
3382 v_addpollinfo(vp);
3383 mtx_lock(&vp->v_pollinfo->vpi_lock);
3384 if (vp->v_pollinfo->vpi_events & events) {
3385 /*
3386 * We clear vpi_events so that we don't
3387 * call selwakeup() twice if two events are
3388 * posted before the polling process(es) is
3389 * awakened. This also ensures that we take at
3390 * most one selwakeup() if the polling process
3391 * is no longer interested. However, it does
3392 * mean that only one event can be noticed at
3393 * a time. (Perhaps we should only clear those
3394 * event bits which we note?) XXX
3395 */
3396 vp->v_pollinfo->vpi_events = 0; /* &= ~events ??? */
3397 vp->v_pollinfo->vpi_revents |= events;
3398 selwakeup(&vp->v_pollinfo->vpi_selinfo);
3399 }
3400 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3401 }
3402
3403 /*
3404 * Wake up anyone polling on vp because it is being revoked.
3405 * This depends on dead_poll() returning POLLHUP for correct
3406 * behavior.
3407 */
3408 void
3409 vn_pollgone(vp)
3410 struct vnode *vp;
3411 {
3412
3413 mtx_lock(&vp->v_pollinfo->vpi_lock);
3414 VN_KNOTE(vp, NOTE_REVOKE);
3415 if (vp->v_pollinfo->vpi_events) {
3416 vp->v_pollinfo->vpi_events = 0;
3417 selwakeup(&vp->v_pollinfo->vpi_selinfo);
3418 }
3419 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3420 }
3421
3422
3423
3424 /*
3425 * Routine to create and manage a filesystem syncer vnode.
3426 */
3427 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3428 static int sync_fsync(struct vop_fsync_args *);
3429 static int sync_inactive(struct vop_inactive_args *);
3430 static int sync_reclaim(struct vop_reclaim_args *);
3431
3432 static vop_t **sync_vnodeop_p;
3433 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
3434 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
3435 { &vop_close_desc, (vop_t *) sync_close }, /* close */
3436 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
3437 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
3438 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
3439 { &vop_lock_desc, (vop_t *) vop_stdlock }, /* lock */
3440 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, /* unlock */
3441 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, /* islocked */
3442 { NULL, NULL }
3443 };
3444 static struct vnodeopv_desc sync_vnodeop_opv_desc =
3445 { &sync_vnodeop_p, sync_vnodeop_entries };
3446
3447 VNODEOP_SET(sync_vnodeop_opv_desc);
3448
3449 /*
3450 * Create a new filesystem syncer vnode for the specified mount point.
3451 */
3452 int
3453 vfs_allocate_syncvnode(mp)
3454 struct mount *mp;
3455 {
3456 struct vnode *vp;
3457 static long start, incr, next;
3458 int error;
3459
3460 /* Allocate a new vnode */
3461 if ((error = getnewvnode("syncer", mp, sync_vnodeop_p, &vp)) != 0) {
3462 mp->mnt_syncer = NULL;
3463 return (error);
3464 }
3465 vp->v_type = VNON;
3466 /*
3467 * Place the vnode onto the syncer worklist. We attempt to
3468 * scatter them about on the list so that they will go off
3469 * at evenly distributed times even if all the filesystems
3470 * are mounted at once.
3471 */
3472 next += incr;
3473 if (next == 0 || next > syncer_maxdelay) {
3474 start /= 2;
3475 incr /= 2;
3476 if (start == 0) {
3477 start = syncer_maxdelay / 2;
3478 incr = syncer_maxdelay;
3479 }
3480 next = start;
3481 }
3482 VI_LOCK(vp);
3483 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
3484 VI_UNLOCK(vp);
3485 mp->mnt_syncer = vp;
3486 return (0);
3487 }
3488
3489 /*
3490 * Do a lazy sync of the filesystem.
3491 */
3492 static int
3493 sync_fsync(ap)
3494 struct vop_fsync_args /* {
3495 struct vnode *a_vp;
3496 struct ucred *a_cred;
3497 int a_waitfor;
3498 struct thread *a_td;
3499 } */ *ap;
3500 {
3501 struct vnode *syncvp = ap->a_vp;
3502 struct mount *mp = syncvp->v_mount;
3503 struct thread *td = ap->a_td;
3504 int error, asyncflag;
3505
3506 /*
3507 * We only need to do something if this is a lazy evaluation.
3508 */
3509 if (ap->a_waitfor != MNT_LAZY)
3510 return (0);
3511
3512 /*
3513 * Move ourselves to the back of the sync list.
3514 */
3515 VI_LOCK(syncvp);
3516 vn_syncer_add_to_worklist(syncvp, syncdelay);
3517 VI_UNLOCK(syncvp);
3518
3519 /*
3520 * Walk the list of vnodes pushing all that are dirty and
3521 * not already on the sync list.
3522 */
3523 mtx_lock(&mountlist_mtx);
3524 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) {
3525 mtx_unlock(&mountlist_mtx);
3526 return (0);
3527 }
3528 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3529 vfs_unbusy(mp, td);
3530 return (0);
3531 }
3532 asyncflag = mp->mnt_flag & MNT_ASYNC;
3533 mp->mnt_flag &= ~MNT_ASYNC;
3534 vfs_msync(mp, MNT_NOWAIT);
3535 error = VFS_SYNC(mp, MNT_LAZY, ap->a_cred, td);
3536 if (asyncflag)
3537 mp->mnt_flag |= MNT_ASYNC;
3538 vn_finished_write(mp);
3539 vfs_unbusy(mp, td);
3540 return (error);
3541 }
3542
3543 /*
3544 * The syncer vnode is no referenced.
3545 */
3546 static int
3547 sync_inactive(ap)
3548 struct vop_inactive_args /* {
3549 struct vnode *a_vp;
3550 struct thread *a_td;
3551 } */ *ap;
3552 {
3553
3554 VOP_UNLOCK(ap->a_vp, 0, ap->a_td);
3555 vgone(ap->a_vp);
3556 return (0);
3557 }
3558
3559 /*
3560 * The syncer vnode is no longer needed and is being decommissioned.
3561 *
3562 * Modifications to the worklist must be protected at splbio().
3563 */
3564 static int
3565 sync_reclaim(ap)
3566 struct vop_reclaim_args /* {
3567 struct vnode *a_vp;
3568 } */ *ap;
3569 {
3570 struct vnode *vp = ap->a_vp;
3571 int s;
3572
3573 s = splbio();
3574 vp->v_mount->mnt_syncer = NULL;
3575 VI_LOCK(vp);
3576 if (vp->v_iflag & VI_ONWORKLST) {
3577 mtx_lock(&sync_mtx);
3578 LIST_REMOVE(vp, v_synclist);
3579 mtx_unlock(&sync_mtx);
3580 vp->v_iflag &= ~VI_ONWORKLST;
3581 }
3582 VI_UNLOCK(vp);
3583 splx(s);
3584
3585 return (0);
3586 }
3587
3588 /*
3589 * extract the dev_t from a VCHR
3590 */
3591 dev_t
3592 vn_todev(vp)
3593 struct vnode *vp;
3594 {
3595 if (vp->v_type != VCHR)
3596 return (NODEV);
3597 return (vp->v_rdev);
3598 }
3599
3600 /*
3601 * Check if vnode represents a disk device
3602 */
3603 int
3604 vn_isdisk(vp, errp)
3605 struct vnode *vp;
3606 int *errp;
3607 {
3608 struct cdevsw *cdevsw;
3609
3610 if (vp->v_type != VCHR) {
3611 if (errp != NULL)
3612 *errp = ENOTBLK;
3613 return (0);
3614 }
3615 if (vp->v_rdev == NULL) {
3616 if (errp != NULL)
3617 *errp = ENXIO;
3618 return (0);
3619 }
3620 cdevsw = devsw(vp->v_rdev);
3621 if (cdevsw == NULL) {
3622 if (errp != NULL)
3623 *errp = ENXIO;
3624 return (0);
3625 }
3626 if (!(cdevsw->d_flags & D_DISK)) {
3627 if (errp != NULL)
3628 *errp = ENOTBLK;
3629 return (0);
3630 }
3631 if (errp != NULL)
3632 *errp = 0;
3633 return (1);
3634 }
3635
3636 /*
3637 * Free data allocated by namei(); see namei(9) for details.
3638 */
3639 void
3640 NDFREE(ndp, flags)
3641 struct nameidata *ndp;
3642 const uint flags;
3643 {
3644 if (!(flags & NDF_NO_FREE_PNBUF) &&
3645 (ndp->ni_cnd.cn_flags & HASBUF)) {
3646 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
3647 ndp->ni_cnd.cn_flags &= ~HASBUF;
3648 }
3649 if (!(flags & NDF_NO_DVP_UNLOCK) &&
3650 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
3651 ndp->ni_dvp != ndp->ni_vp)
3652 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread);
3653 if (!(flags & NDF_NO_DVP_RELE) &&
3654 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
3655 vrele(ndp->ni_dvp);
3656 ndp->ni_dvp = NULL;
3657 }
3658 if (!(flags & NDF_NO_VP_UNLOCK) &&
3659 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
3660 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread);
3661 if (!(flags & NDF_NO_VP_RELE) &&
3662 ndp->ni_vp) {
3663 vrele(ndp->ni_vp);
3664 ndp->ni_vp = NULL;
3665 }
3666 if (!(flags & NDF_NO_STARTDIR_RELE) &&
3667 (ndp->ni_cnd.cn_flags & SAVESTART)) {
3668 vrele(ndp->ni_startdir);
3669 ndp->ni_startdir = NULL;
3670 }
3671 }
3672
3673 /*
3674 * Common filesystem object access control check routine. Accepts a
3675 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3676 * and optional call-by-reference privused argument allowing vaccess()
3677 * to indicate to the caller whether privilege was used to satisfy the
3678 * request (obsoleted). Returns 0 on success, or an errno on failure.
3679 */
3680 int
3681 vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused)
3682 enum vtype type;
3683 mode_t file_mode;
3684 uid_t file_uid;
3685 gid_t file_gid;
3686 mode_t acc_mode;
3687 struct ucred *cred;
3688 int *privused;
3689 {
3690 mode_t dac_granted;
3691 #ifdef CAPABILITIES
3692 mode_t cap_granted;
3693 #endif
3694
3695 /*
3696 * Look for a normal, non-privileged way to access the file/directory
3697 * as requested. If it exists, go with that.
3698 */
3699
3700 if (privused != NULL)
3701 *privused = 0;
3702
3703 dac_granted = 0;
3704
3705 /* Check the owner. */
3706 if (cred->cr_uid == file_uid) {
3707 dac_granted |= VADMIN;
3708 if (file_mode & S_IXUSR)
3709 dac_granted |= VEXEC;
3710 if (file_mode & S_IRUSR)
3711 dac_granted |= VREAD;
3712 if (file_mode & S_IWUSR)
3713 dac_granted |= (VWRITE | VAPPEND);
3714
3715 if ((acc_mode & dac_granted) == acc_mode)
3716 return (0);
3717
3718 goto privcheck;
3719 }
3720
3721 /* Otherwise, check the groups (first match) */
3722 if (groupmember(file_gid, cred)) {
3723 if (file_mode & S_IXGRP)
3724 dac_granted |= VEXEC;
3725 if (file_mode & S_IRGRP)
3726 dac_granted |= VREAD;
3727 if (file_mode & S_IWGRP)
3728 dac_granted |= (VWRITE | VAPPEND);
3729
3730 if ((acc_mode & dac_granted) == acc_mode)
3731 return (0);
3732
3733 goto privcheck;
3734 }
3735
3736 /* Otherwise, check everyone else. */
3737 if (file_mode & S_IXOTH)
3738 dac_granted |= VEXEC;
3739 if (file_mode & S_IROTH)
3740 dac_granted |= VREAD;
3741 if (file_mode & S_IWOTH)
3742 dac_granted |= (VWRITE | VAPPEND);
3743 if ((acc_mode & dac_granted) == acc_mode)
3744 return (0);
3745
3746 privcheck:
3747 if (!suser_cred(cred, PRISON_ROOT)) {
3748 /* XXX audit: privilege used */
3749 if (privused != NULL)
3750 *privused = 1;
3751 return (0);
3752 }
3753
3754 #ifdef CAPABILITIES
3755 /*
3756 * Build a capability mask to determine if the set of capabilities
3757 * satisfies the requirements when combined with the granted mask
3758 * from above.
3759 * For each capability, if the capability is required, bitwise
3760 * or the request type onto the cap_granted mask.
3761 */
3762 cap_granted = 0;
3763
3764 if (type == VDIR) {
3765 /*
3766 * For directories, use CAP_DAC_READ_SEARCH to satisfy
3767 * VEXEC requests, instead of CAP_DAC_EXECUTE.
3768 */
3769 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3770 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT))
3771 cap_granted |= VEXEC;
3772 } else {
3773 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3774 !cap_check(cred, NULL, CAP_DAC_EXECUTE, PRISON_ROOT))
3775 cap_granted |= VEXEC;
3776 }
3777
3778 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) &&
3779 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT))
3780 cap_granted |= VREAD;
3781
3782 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3783 !cap_check(cred, NULL, CAP_DAC_WRITE, PRISON_ROOT))
3784 cap_granted |= (VWRITE | VAPPEND);
3785
3786 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3787 !cap_check(cred, NULL, CAP_FOWNER, PRISON_ROOT))
3788 cap_granted |= VADMIN;
3789
3790 if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) {
3791 /* XXX audit: privilege used */
3792 if (privused != NULL)
3793 *privused = 1;
3794 return (0);
3795 }
3796 #endif
3797
3798 return ((acc_mode & VADMIN) ? EPERM : EACCES);
3799 }
3800
3801 /*
3802 * Credential check based on process requesting service, and per-attribute
3803 * permissions.
3804 */
3805 int
3806 extattr_check_cred(struct vnode *vp, int attrnamespace,
3807 struct ucred *cred, struct thread *td, int access)
3808 {
3809
3810 /*
3811 * Kernel-invoked always succeeds.
3812 */
3813 if (cred == NOCRED)
3814 return (0);
3815
3816 /*
3817 * Do not allow privileged processes in jail to directly
3818 * manipulate system attributes.
3819 *
3820 * XXX What capability should apply here?
3821 * Probably CAP_SYS_SETFFLAG.
3822 */
3823 switch (attrnamespace) {
3824 case EXTATTR_NAMESPACE_SYSTEM:
3825 /* Potentially should be: return (EPERM); */
3826 return (suser_cred(cred, 0));
3827 case EXTATTR_NAMESPACE_USER:
3828 return (VOP_ACCESS(vp, access, cred, td));
3829 default:
3830 return (EPERM);
3831 }
3832 }
Cache object: 1eb08b35b2d93d17f131f1f1560092b7
|