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