| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 2 | * Copyright (c) 1989, 1993 | |
| 3 | * The Regents of the University of California. All rights reserved. | |
| 4 | * (c) UNIX System Laboratories, Inc. | |
| 5 | * All or some portions of this file are derived from material licensed | |
| 6 | * to the University of California by American Telephone and Telegraph | |
| 7 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
| 8 | * the permission of UNIX System Laboratories, Inc. | |
| 9 | * | |
| 10 | * Redistribution and use in source and binary forms, with or without | |
| 11 | * modification, are permitted provided that the following conditions | |
| 12 | * are met: | |
| 13 | * 1. Redistributions of source code must retain the above copyright | |
| 14 | * notice, this list of conditions and the following disclaimer. | |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 16 | * notice, this list of conditions and the following disclaimer in the | |
| 17 | * documentation and/or other materials provided with the distribution. | |
| 18 | * 3. All advertising materials mentioning features or use of this software | |
| 19 | * must display the following acknowledgement: | |
| 20 | * This product includes software developed by the University of | |
| 21 | * California, Berkeley and its contributors. | |
| 22 | * 4. Neither the name of the University nor the names of its contributors | |
| 23 | * may be used to endorse or promote products derived from this software | |
| 24 | * without specific prior written permission. | |
| 25 | * | |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 36 | * SUCH DAMAGE. | |
| 37 | * | |
| 38 | * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 | |
| 39 | * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $ | |
| 40 | */ | |
| 41 | ||
| 42 | /* | |
| 43 | * External virtual filesystem routines | |
| 44 | */ | |
| 45 | #include "opt_ddb.h" | |
| 46 | ||
| 47 | #include <sys/param.h> | |
| 48 | #include <sys/systm.h> | |
| 49 | #include <sys/buf.h> | |
| 50 | #include <sys/conf.h> | |
| 51 | #include <sys/dirent.h> | |
| 52 | #include <sys/domain.h> | |
| 53 | #include <sys/eventhandler.h> | |
| 54 | #include <sys/fcntl.h> | |
| b8477cda | 55 | #include <sys/file.h> |
| 984263bc MD |
56 | #include <sys/kernel.h> |
| 57 | #include <sys/kthread.h> | |
| 58 | #include <sys/malloc.h> | |
| 59 | #include <sys/mbuf.h> | |
| 60 | #include <sys/mount.h> | |
| 3b0783db | 61 | #include <sys/priv.h> |
| 984263bc MD |
62 | #include <sys/proc.h> |
| 63 | #include <sys/reboot.h> | |
| 64 | #include <sys/socket.h> | |
| 65 | #include <sys/stat.h> | |
| 66 | #include <sys/sysctl.h> | |
| 67 | #include <sys/syslog.h> | |
| 5d72d6ed | 68 | #include <sys/unistd.h> |
| 984263bc MD |
69 | #include <sys/vmmeter.h> |
| 70 | #include <sys/vnode.h> | |
| 71 | ||
| 72 | #include <machine/limits.h> | |
| 73 | ||
| 74 | #include <vm/vm.h> | |
| 75 | #include <vm/vm_object.h> | |
| 76 | #include <vm/vm_extern.h> | |
| 6ef943a3 | 77 | #include <vm/vm_kern.h> |
| 984263bc MD |
78 | #include <vm/pmap.h> |
| 79 | #include <vm/vm_map.h> | |
| 80 | #include <vm/vm_page.h> | |
| 81 | #include <vm/vm_pager.h> | |
| 82 | #include <vm/vnode_pager.h> | |
| 83 | #include <vm/vm_zone.h> | |
| 84 | ||
| 3020e3be | 85 | #include <sys/buf2.h> |
| f5865223 | 86 | #include <sys/thread2.h> |
| 3c37c940 | 87 | #include <sys/sysref2.h> |
| 2247fe02 | 88 | #include <sys/mplock2.h> |
| 3020e3be | 89 | |
| 984263bc MD |
90 | static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure"); |
| 91 | ||
| 5fd012e0 | 92 | int numvnodes; |
| 0c52fa62 SG |
93 | SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, |
| 94 | "Number of vnodes allocated"); | |
| 28271622 MD |
95 | int verbose_reclaims; |
| 96 | SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0, | |
| 97 | "Output filename of reclaimed vnode(s)"); | |
| 984263bc MD |
98 | |
| 99 | enum vtype iftovt_tab[16] = { | |
| 100 | VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, | |
| 101 | VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, | |
| 102 | }; | |
| 103 | int vttoif_tab[9] = { | |
| 104 | 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, | |
| 105 | S_IFSOCK, S_IFIFO, S_IFMT, | |
| 106 | }; | |
| 107 | ||
| 984263bc | 108 | static int reassignbufcalls; |
| 093e85dc SG |
109 | SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, |
| 110 | 0, "Number of times buffers have been reassigned to the proper list"); | |
| 111 | ||
| 7608650f | 112 | static int check_buf_overlap = 2; /* invasive check */ |
| 093e85dc SG |
113 | SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap, |
| 114 | 0, "Enable overlapping buffer checks"); | |
| 984263bc | 115 | |
| 984263bc | 116 | int nfs_mount_type = -1; |
| 8a8d5d85 | 117 | static struct lwkt_token spechash_token; |
| 984263bc | 118 | struct nfs_public nfs_pub; /* publicly exported FS */ |
| 984263bc MD |
119 | |
| 120 | int desiredvnodes; | |
| 121 | SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, | |
| dd98570a | 122 | &desiredvnodes, 0, "Maximum number of vnodes"); |
| 984263bc | 123 | |
| 402ed7e1 RG |
124 | static void vfs_free_addrlist (struct netexport *nep); |
| 125 | static int vfs_free_netcred (struct radix_node *rn, void *w); | |
| 126 | static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep, | |
| 1aa89f17 | 127 | const struct export_args *argp); |
| 984263bc | 128 | |
| 41a01a4d | 129 | /* |
| 6bae6177 MD |
130 | * Red black tree functions |
| 131 | */ | |
| 132 | static int rb_buf_compare(struct buf *b1, struct buf *b2); | |
| 54078292 MD |
133 | RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset); |
| 134 | RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset); | |
| 6bae6177 MD |
135 | |
| 136 | static int | |
| 137 | rb_buf_compare(struct buf *b1, struct buf *b2) | |
| 138 | { | |
| 54078292 | 139 | if (b1->b_loffset < b2->b_loffset) |
| 6bae6177 | 140 | return(-1); |
| 54078292 | 141 | if (b1->b_loffset > b2->b_loffset) |
| 6bae6177 MD |
142 | return(1); |
| 143 | return(0); | |
| 144 | } | |
| 145 | ||
| 146 | /* | |
| 44b1cf3d | 147 | * Returns non-zero if the vnode is a candidate for lazy msyncing. |
| aed76ef1 MD |
148 | * |
| 149 | * NOTE: v_object is not stable (this scan can race), however the | |
| 150 | * mntvnodescan code holds vmobj_token so any VM object we | |
| 151 | * do find will remain stable storage. | |
| 41a01a4d | 152 | */ |
| 5fd012e0 | 153 | static __inline int |
| 3c37c940 | 154 | vshouldmsync(struct vnode *vp) |
| 41a01a4d | 155 | { |
| aed76ef1 MD |
156 | vm_object_t object; |
| 157 | ||
| 3c37c940 | 158 | if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0) |
| 44b1cf3d | 159 | return (0); /* other holders */ |
| aed76ef1 MD |
160 | object = vp->v_object; |
| 161 | cpu_ccfence(); | |
| 162 | if (object && (object->ref_count || object->resident_page_count)) | |
| 163 | return(0); | |
| 5fd012e0 | 164 | return (1); |
| 41a01a4d | 165 | } |
| 5fd012e0 | 166 | |
| 984263bc | 167 | /* |
| 5fd012e0 MD |
168 | * Initialize the vnode management data structures. |
| 169 | * | |
| 170 | * Called from vfsinit() | |
| 984263bc MD |
171 | */ |
| 172 | void | |
| 5fd012e0 | 173 | vfs_subr_init(void) |
| 984263bc | 174 | { |
| 7c457ac8 MD |
175 | int factor1; |
| 176 | int factor2; | |
| 177 | ||
| 6ef943a3 | 178 | /* |
| 55d3a838 MD |
179 | * Desiredvnodes is kern.maxvnodes. We want to scale it |
| 180 | * according to available system memory but we may also have | |
| 181 | * to limit it based on available KVM, which is capped on 32 bit | |
| 61c07505 | 182 | * systems, to ~80K vnodes or so. |
| b867f3d9 MD |
183 | * |
| 184 | * WARNING! For machines with 64-256M of ram we have to be sure | |
| 185 | * that the default limit scales down well due to HAMMER | |
| 186 | * taking up significantly more memory per-vnode vs UFS. | |
| 187 | * We want around ~5800 on a 128M machine. | |
| 6ef943a3 | 188 | */ |
| 7c457ac8 | 189 | factor1 = 20 * (sizeof(struct vm_object) + sizeof(struct vnode)); |
| 61c07505 | 190 | factor2 = 25 * (sizeof(struct vm_object) + sizeof(struct vnode)); |
| 7c457ac8 MD |
191 | desiredvnodes = |
| 192 | imin((int64_t)vmstats.v_page_count * PAGE_SIZE / factor1, | |
| 193 | KvaSize / factor2); | |
| 194 | desiredvnodes = imax(desiredvnodes, maxproc * 8); | |
| 6ef943a3 | 195 | |
| a3c18566 | 196 | lwkt_token_init(&spechash_token, "spechash"); |
| 984263bc MD |
197 | } |
| 198 | ||
| 199 | /* | |
| 200 | * Knob to control the precision of file timestamps: | |
| 201 | * | |
| 202 | * 0 = seconds only; nanoseconds zeroed. | |
| 203 | * 1 = seconds and nanoseconds, accurate within 1/HZ. | |
| 204 | * 2 = seconds and nanoseconds, truncated to microseconds. | |
| 205 | * >=3 = seconds and nanoseconds, maximum precision. | |
| 206 | */ | |
| 207 | enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; | |
| 208 | ||
| 209 | static int timestamp_precision = TSP_SEC; | |
| 210 | SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, | |
| 093e85dc | 211 | ×tamp_precision, 0, "Precision of file timestamps"); |
| 984263bc MD |
212 | |
| 213 | /* | |
| 214 | * Get a current timestamp. | |
| 627531fa MD |
215 | * |
| 216 | * MPSAFE | |
| 984263bc MD |
217 | */ |
| 218 | void | |
| dd98570a | 219 | vfs_timestamp(struct timespec *tsp) |
| 984263bc MD |
220 | { |
| 221 | struct timeval tv; | |
| 222 | ||
| 223 | switch (timestamp_precision) { | |
| 224 | case TSP_SEC: | |
| 225 | tsp->tv_sec = time_second; | |
| 226 | tsp->tv_nsec = 0; | |
| 227 | break; | |
| 228 | case TSP_HZ: | |
| 229 | getnanotime(tsp); | |
| 230 | break; | |
| 231 | case TSP_USEC: | |
| 232 | microtime(&tv); | |
| 233 | TIMEVAL_TO_TIMESPEC(&tv, tsp); | |
| 234 | break; | |
| 235 | case TSP_NSEC: | |
| 236 | default: | |
| 237 | nanotime(tsp); | |
| 238 | break; | |
| 239 | } | |
| 240 | } | |
| 241 | ||
| 242 | /* | |
| 243 | * Set vnode attributes to VNOVAL | |
| 244 | */ | |
| 245 | void | |
| dd98570a | 246 | vattr_null(struct vattr *vap) |
| 984263bc | 247 | { |
| 984263bc MD |
248 | vap->va_type = VNON; |
| 249 | vap->va_size = VNOVAL; | |
| 250 | vap->va_bytes = VNOVAL; | |
| 251 | vap->va_mode = VNOVAL; | |
| 252 | vap->va_nlink = VNOVAL; | |
| 253 | vap->va_uid = VNOVAL; | |
| 254 | vap->va_gid = VNOVAL; | |
| 255 | vap->va_fsid = VNOVAL; | |
| 256 | vap->va_fileid = VNOVAL; | |
| 257 | vap->va_blocksize = VNOVAL; | |
| 0e9b9130 MD |
258 | vap->va_rmajor = VNOVAL; |
| 259 | vap->va_rminor = VNOVAL; | |
| 984263bc MD |
260 | vap->va_atime.tv_sec = VNOVAL; |
| 261 | vap->va_atime.tv_nsec = VNOVAL; | |
| 262 | vap->va_mtime.tv_sec = VNOVAL; | |
| 263 | vap->va_mtime.tv_nsec = VNOVAL; | |
| 264 | vap->va_ctime.tv_sec = VNOVAL; | |
| 265 | vap->va_ctime.tv_nsec = VNOVAL; | |
| 266 | vap->va_flags = VNOVAL; | |
| 267 | vap->va_gen = VNOVAL; | |
| 268 | vap->va_vaflags = 0; | |
| 50626622 | 269 | /* va_*_uuid fields are only valid if related flags are set */ |
| 984263bc MD |
270 | } |
| 271 | ||
| 272 | /* | |
| 984263bc | 273 | * Flush out and invalidate all buffers associated with a vnode. |
| 5fd012e0 MD |
274 | * |
| 275 | * vp must be locked. | |
| 984263bc | 276 | */ |
| 6bae6177 MD |
277 | static int vinvalbuf_bp(struct buf *bp, void *data); |
| 278 | ||
| 279 | struct vinvalbuf_bp_info { | |
| 280 | struct vnode *vp; | |
| 281 | int slptimeo; | |
| f2770c70 | 282 | int lkflags; |
| 6bae6177 | 283 | int flags; |
| c5724852 | 284 | int clean; |
| 6bae6177 MD |
285 | }; |
| 286 | ||
| 984263bc | 287 | int |
| 87de5057 | 288 | vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo) |
| 984263bc | 289 | { |
| 6bae6177 | 290 | struct vinvalbuf_bp_info info; |
| 984263bc | 291 | vm_object_t object; |
| 0202303b MD |
292 | int error; |
| 293 | ||
| 3b998fa9 | 294 | lwkt_gettoken(&vp->v_token); |
| 984263bc | 295 | |
| 6bae6177 MD |
296 | /* |
| 297 | * If we are being asked to save, call fsync to ensure that the inode | |
| 298 | * is updated. | |
| 299 | */ | |
| 984263bc | 300 | if (flags & V_SAVE) { |
| a9a20f98 | 301 | error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo); |
| 0202303b | 302 | if (error) |
| a9a20f98 | 303 | goto done; |
| 6bae6177 | 304 | if (!RB_EMPTY(&vp->v_rbdirty_tree)) { |
| 52174f71 | 305 | if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0) |
| a9a20f98 | 306 | goto done; |
| 1bb61199 MD |
307 | |
| 308 | /* | |
| 309 | * Dirty bufs may be left or generated via races | |
| 310 | * in circumstances where vinvalbuf() is called on | |
| 311 | * a vnode not undergoing reclamation. Only | |
| 312 | * panic if we are trying to reclaim the vnode. | |
| 313 | */ | |
| 314 | if ((vp->v_flag & VRECLAIMED) && | |
| a9a20f98 | 315 | (bio_track_active(&vp->v_track_write) || |
| 1bb61199 | 316 | !RB_EMPTY(&vp->v_rbdirty_tree))) { |
| 984263bc | 317 | panic("vinvalbuf: dirty bufs"); |
| 1bb61199 | 318 | } |
| 984263bc | 319 | } |
| 984263bc | 320 | } |
| 6bae6177 | 321 | info.slptimeo = slptimeo; |
| f2770c70 MD |
322 | info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL; |
| 323 | if (slpflag & PCATCH) | |
| 324 | info.lkflags |= LK_PCATCH; | |
| 6bae6177 MD |
325 | info.flags = flags; |
| 326 | info.vp = vp; | |
| 327 | ||
| 328 | /* | |
| 329 | * Flush the buffer cache until nothing is left. | |
| 330 | */ | |
| 331 | while (!RB_EMPTY(&vp->v_rbclean_tree) || | |
| 0202303b | 332 | !RB_EMPTY(&vp->v_rbdirty_tree)) { |
| c5724852 | 333 | info.clean = 1; |
| 6bae6177 | 334 | error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL, |
| 65c6c519 | 335 | vinvalbuf_bp, &info); |
| 6bae6177 | 336 | if (error == 0) { |
| c5724852 | 337 | info.clean = 0; |
| 6bae6177 MD |
338 | error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, |
| 339 | vinvalbuf_bp, &info); | |
| 984263bc MD |
340 | } |
| 341 | } | |
| 342 | ||
| 343 | /* | |
| a9a20f98 MD |
344 | * Wait for I/O completion. We may block in the pip code so we have |
| 345 | * to re-check. | |
| 984263bc MD |
346 | */ |
| 347 | do { | |
| a9a20f98 | 348 | bio_track_wait(&vp->v_track_write, 0, 0); |
| 7540ab49 | 349 | if ((object = vp->v_object) != NULL) { |
| 00db03f1 | 350 | refcount_wait(&object->paging_in_progress, "vnvlbx"); |
| 984263bc | 351 | } |
| a9a20f98 | 352 | } while (bio_track_active(&vp->v_track_write)); |
| 984263bc | 353 | |
| 984263bc MD |
354 | /* |
| 355 | * Destroy the copy in the VM cache, too. | |
| 356 | */ | |
| 7540ab49 | 357 | if ((object = vp->v_object) != NULL) { |
| 984263bc MD |
358 | vm_object_page_remove(object, 0, 0, |
| 359 | (flags & V_SAVE) ? TRUE : FALSE); | |
| 360 | } | |
| 984263bc | 361 | |
| 6bae6177 | 362 | if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree)) |
| 984263bc | 363 | panic("vinvalbuf: flush failed"); |
| 1f1ea522 MD |
364 | if (!RB_EMPTY(&vp->v_rbhash_tree)) |
| 365 | panic("vinvalbuf: flush failed, buffers still present"); | |
| a9a20f98 MD |
366 | error = 0; |
| 367 | done: | |
| 3b998fa9 | 368 | lwkt_reltoken(&vp->v_token); |
| a9a20f98 | 369 | return (error); |
| 984263bc MD |
370 | } |
| 371 | ||
| 6bae6177 MD |
372 | static int |
| 373 | vinvalbuf_bp(struct buf *bp, void *data) | |
| 374 | { | |
| 375 | struct vinvalbuf_bp_info *info = data; | |
| 376 | int error; | |
| 377 | ||
| 378 | if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { | |
| c5724852 | 379 | atomic_add_int(&bp->b_refs, 1); |
| f2770c70 MD |
380 | error = BUF_TIMELOCK(bp, info->lkflags, |
| 381 | "vinvalbuf", info->slptimeo); | |
| c5724852 | 382 | atomic_subtract_int(&bp->b_refs, 1); |
| 6bae6177 MD |
383 | if (error == 0) { |
| 384 | BUF_UNLOCK(bp); | |
| 385 | error = ENOLCK; | |
| 386 | } | |
| 387 | if (error == ENOLCK) | |
| 388 | return(0); | |
| 389 | return (-error); | |
| 390 | } | |
| 65c6c519 MD |
391 | KKASSERT(bp->b_vp == info->vp); |
| 392 | ||
| 6bae6177 | 393 | /* |
| c5724852 MD |
394 | * Must check clean/dirty status after successfully locking as |
| 395 | * it may race. | |
| 396 | */ | |
| 397 | if ((info->clean && (bp->b_flags & B_DELWRI)) || | |
| 398 | (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) { | |
| 399 | BUF_UNLOCK(bp); | |
| 400 | return(0); | |
| 401 | } | |
| 402 | ||
| 403 | /* | |
| 27bc0cb1 MD |
404 | * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite() |
| 405 | * check. This code will write out the buffer, period. | |
| 6bae6177 | 406 | */ |
| 9de13b88 | 407 | bremfree(bp); |
| 6bae6177 MD |
408 | if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && |
| 409 | (info->flags & V_SAVE)) { | |
| 9de13b88 | 410 | cluster_awrite(bp); |
| 135bd6a8 MD |
411 | } else if (info->flags & V_SAVE) { |
| 412 | /* | |
| 413 | * Cannot set B_NOCACHE on a clean buffer as this will | |
| 414 | * destroy the VM backing store which might actually | |
| 415 | * be dirty (and unsynchronized). | |
| 416 | */ | |
| 135bd6a8 | 417 | bp->b_flags |= (B_INVAL | B_RELBUF); |
| 135bd6a8 | 418 | brelse(bp); |
| 6bae6177 | 419 | } else { |
| 6bae6177 | 420 | bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); |
| 6bae6177 MD |
421 | brelse(bp); |
| 422 | } | |
| 423 | return(0); | |
| 424 | } | |
| 425 | ||
| 984263bc MD |
426 | /* |
| 427 | * Truncate a file's buffer and pages to a specified length. This | |
| 428 | * is in lieu of the old vinvalbuf mechanism, which performed unneeded | |
| 429 | * sync activity. | |
| 5fd012e0 MD |
430 | * |
| 431 | * The vnode must be locked. | |
| 984263bc | 432 | */ |
| 6bae6177 MD |
433 | static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data); |
| 434 | static int vtruncbuf_bp_trunc(struct buf *bp, void *data); | |
| 435 | static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data); | |
| 436 | static int vtruncbuf_bp_metasync(struct buf *bp, void *data); | |
| 437 | ||
| c5724852 MD |
438 | struct vtruncbuf_info { |
| 439 | struct vnode *vp; | |
| 440 | off_t truncloffset; | |
| 441 | int clean; | |
| 442 | }; | |
| 443 | ||
| 984263bc | 444 | int |
| 87de5057 | 445 | vtruncbuf(struct vnode *vp, off_t length, int blksize) |
| 984263bc | 446 | { |
| c5724852 | 447 | struct vtruncbuf_info info; |
| c4b46cb4 | 448 | const char *filename; |
| 0202303b | 449 | int count; |
| 984263bc MD |
450 | |
| 451 | /* | |
| 54078292 | 452 | * Round up to the *next* block, then destroy the buffers in question. |
| 6bae6177 MD |
453 | * Since we are only removing some of the buffers we must rely on the |
| 454 | * scan count to determine whether a loop is necessary. | |
| 984263bc | 455 | */ |
| 54078292 | 456 | if ((count = (int)(length % blksize)) != 0) |
| c5724852 | 457 | info.truncloffset = length + (blksize - count); |
| 54078292 | 458 | else |
| c5724852 MD |
459 | info.truncloffset = length; |
| 460 | info.vp = vp; | |
| 984263bc | 461 | |
| 3b998fa9 | 462 | lwkt_gettoken(&vp->v_token); |
| 6bae6177 | 463 | do { |
| c5724852 | 464 | info.clean = 1; |
| 6bae6177 MD |
465 | count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, |
| 466 | vtruncbuf_bp_trunc_cmp, | |
| c5724852 MD |
467 | vtruncbuf_bp_trunc, &info); |
| 468 | info.clean = 0; | |
| 6bae6177 MD |
469 | count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, |
| 470 | vtruncbuf_bp_trunc_cmp, | |
| c5724852 | 471 | vtruncbuf_bp_trunc, &info); |
| 6bae6177 | 472 | } while(count); |
| 984263bc | 473 | |
| 6bae6177 MD |
474 | /* |
| 475 | * For safety, fsync any remaining metadata if the file is not being | |
| 476 | * truncated to 0. Since the metadata does not represent the entire | |
| 477 | * dirty list we have to rely on the hit count to ensure that we get | |
| 478 | * all of it. | |
| 479 | */ | |
| 984263bc | 480 | if (length > 0) { |
| 6bae6177 MD |
481 | do { |
| 482 | count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, | |
| 483 | vtruncbuf_bp_metasync_cmp, | |
| c5724852 | 484 | vtruncbuf_bp_metasync, &info); |
| 6bae6177 | 485 | } while (count); |
| 984263bc MD |
486 | } |
| 487 | ||
| 6bae6177 | 488 | /* |
| c4b46cb4 | 489 | * Clean out any left over VM backing store. |
| 0202303b | 490 | * |
| 135bd6a8 MD |
491 | * It is possible to have in-progress I/O from buffers that were |
| 492 | * not part of the truncation. This should not happen if we | |
| 493 | * are truncating to 0-length. | |
| 6bae6177 | 494 | */ |
| 0202303b MD |
495 | vnode_pager_setsize(vp, length); |
| 496 | bio_track_wait(&vp->v_track_write, 0, 0); | |
| 497 | ||
| f63911bf MD |
498 | /* |
| 499 | * Debugging only | |
| 500 | */ | |
| b12defdc | 501 | spin_lock(&vp->v_spin); |
| c4b46cb4 MD |
502 | filename = TAILQ_FIRST(&vp->v_namecache) ? |
| 503 | TAILQ_FIRST(&vp->v_namecache)->nc_name : "?"; | |
| b12defdc | 504 | spin_unlock(&vp->v_spin); |
| c4b46cb4 | 505 | |
| c4b46cb4 MD |
506 | /* |
| 507 | * Make sure no buffers were instantiated while we were trying | |
| 508 | * to clean out the remaining VM pages. This could occur due | |
| 509 | * to busy dirty VM pages being flushed out to disk. | |
| 510 | */ | |
| 511 | do { | |
| c5724852 | 512 | info.clean = 1; |
| c4b46cb4 MD |
513 | count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, |
| 514 | vtruncbuf_bp_trunc_cmp, | |
| c5724852 MD |
515 | vtruncbuf_bp_trunc, &info); |
| 516 | info.clean = 0; | |
| c4b46cb4 MD |
517 | count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, |
| 518 | vtruncbuf_bp_trunc_cmp, | |
| c5724852 | 519 | vtruncbuf_bp_trunc, &info); |
| c4b46cb4 | 520 | if (count) { |
| 6ea70f76 | 521 | kprintf("Warning: vtruncbuf(): Had to re-clean %d " |
| c4b46cb4 MD |
522 | "left over buffers in %s\n", count, filename); |
| 523 | } | |
| 524 | } while(count); | |
| 984263bc | 525 | |
| 3b998fa9 | 526 | lwkt_reltoken(&vp->v_token); |
| b1f72a5c | 527 | |
| 984263bc MD |
528 | return (0); |
| 529 | } | |
| 530 | ||
| 531 | /* | |
| 6bae6177 MD |
532 | * The callback buffer is beyond the new file EOF and must be destroyed. |
| 533 | * Note that the compare function must conform to the RB_SCAN's requirements. | |
| 534 | */ | |
| 535 | static | |
| 536 | int | |
| 537 | vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data) | |
| 538 | { | |
| c5724852 MD |
539 | struct vtruncbuf_info *info = data; |
| 540 | ||
| 541 | if (bp->b_loffset >= info->truncloffset) | |
| 6bae6177 MD |
542 | return(0); |
| 543 | return(-1); | |
| 544 | } | |
| 545 | ||
| 546 | static | |
| 547 | int | |
| 548 | vtruncbuf_bp_trunc(struct buf *bp, void *data) | |
| 549 | { | |
| c5724852 MD |
550 | struct vtruncbuf_info *info = data; |
| 551 | ||
| 6bae6177 MD |
552 | /* |
| 553 | * Do not try to use a buffer we cannot immediately lock, but sleep | |
| 554 | * anyway to prevent a livelock. The code will loop until all buffers | |
| 555 | * can be acted upon. | |
| c5724852 MD |
556 | * |
| 557 | * We must always revalidate the buffer after locking it to deal | |
| 558 | * with MP races. | |
| 6bae6177 MD |
559 | */ |
| 560 | if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { | |
| c5724852 | 561 | atomic_add_int(&bp->b_refs, 1); |
| 6bae6177 MD |
562 | if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) |
| 563 | BUF_UNLOCK(bp); | |
| c5724852 MD |
564 | atomic_subtract_int(&bp->b_refs, 1); |
| 565 | } else if ((info->clean && (bp->b_flags & B_DELWRI)) || | |
| 566 | (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) || | |
| 567 | bp->b_vp != info->vp || | |
| 568 | vtruncbuf_bp_trunc_cmp(bp, data)) { | |
| 569 | BUF_UNLOCK(bp); | |
| 6bae6177 MD |
570 | } else { |
| 571 | bremfree(bp); | |
| 135bd6a8 | 572 | bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE); |
| 6bae6177 MD |
573 | brelse(bp); |
| 574 | } | |
| 575 | return(1); | |
| 576 | } | |
| 577 | ||
| 578 | /* | |
| 579 | * Fsync all meta-data after truncating a file to be non-zero. Only metadata | |
| 54078292 | 580 | * blocks (with a negative loffset) are scanned. |
| 6bae6177 MD |
581 | * Note that the compare function must conform to the RB_SCAN's requirements. |
| 582 | */ | |
| 583 | static int | |
| c5724852 | 584 | vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused) |
| 6bae6177 | 585 | { |
| 54078292 | 586 | if (bp->b_loffset < 0) |
| 6bae6177 MD |
587 | return(0); |
| 588 | return(1); | |
| 589 | } | |
| 590 | ||
| 591 | static int | |
| 592 | vtruncbuf_bp_metasync(struct buf *bp, void *data) | |
| 593 | { | |
| c5724852 | 594 | struct vtruncbuf_info *info = data; |
| 6bae6177 | 595 | |
| c5724852 MD |
596 | if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { |
| 597 | atomic_add_int(&bp->b_refs, 1); | |
| 598 | if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0) | |
| 599 | BUF_UNLOCK(bp); | |
| 600 | atomic_subtract_int(&bp->b_refs, 1); | |
| 601 | } else if ((bp->b_flags & B_DELWRI) == 0 || | |
| 602 | bp->b_vp != info->vp || | |
| 603 | vtruncbuf_bp_metasync_cmp(bp, data)) { | |
| 604 | BUF_UNLOCK(bp); | |
| 6bae6177 | 605 | } else { |
| c5724852 MD |
606 | bremfree(bp); |
| 607 | if (bp->b_vp == info->vp) | |
| 608 | bawrite(bp); | |
| 609 | else | |
| 610 | bwrite(bp); | |
| 6bae6177 | 611 | } |
| c5724852 | 612 | return(1); |
| 6bae6177 MD |
613 | } |
| 614 | ||
| 615 | /* | |
| 616 | * vfsync - implements a multipass fsync on a file which understands | |
| 617 | * dependancies and meta-data. The passed vnode must be locked. The | |
| 618 | * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY. | |
| 619 | * | |
| 620 | * When fsyncing data asynchronously just do one consolidated pass starting | |
| 621 | * with the most negative block number. This may not get all the data due | |
| 622 | * to dependancies. | |
| 623 | * | |
| 624 | * When fsyncing data synchronously do a data pass, then a metadata pass, | |
| 625 | * then do additional data+metadata passes to try to get all the data out. | |
| 626 | */ | |
| 627 | static int vfsync_wait_output(struct vnode *vp, | |
| 628 | int (*waitoutput)(struct vnode *, struct thread *)); | |
| c5724852 | 629 | static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused); |
| 6bae6177 MD |
630 | static int vfsync_data_only_cmp(struct buf *bp, void *data); |
| 631 | static int vfsync_meta_only_cmp(struct buf *bp, void *data); | |
| 632 | static int vfsync_lazy_range_cmp(struct buf *bp, void *data); | |
| 633 | static int vfsync_bp(struct buf *bp, void *data); | |
| 634 | ||
| 635 | struct vfsync_info { | |
| 636 | struct vnode *vp; | |
| 637 | int synchronous; | |
| 638 | int syncdeps; | |
| 639 | int lazycount; | |
| 640 | int lazylimit; | |
| 54078292 | 641 | int skippedbufs; |
| 6bae6177 | 642 | int (*checkdef)(struct buf *); |
| c5724852 | 643 | int (*cmpfunc)(struct buf *, void *); |
| 6bae6177 MD |
644 | }; |
| 645 | ||
| 646 | int | |
| 4e0ecc94 | 647 | vfsync(struct vnode *vp, int waitfor, int passes, |
| 6bae6177 MD |
648 | int (*checkdef)(struct buf *), |
| 649 | int (*waitoutput)(struct vnode *, struct thread *)) | |
| 650 | { | |
| 651 | struct vfsync_info info; | |
| 652 | int error; | |
| 653 | ||
| 654 | bzero(&info, sizeof(info)); | |
| 655 | info.vp = vp; | |
| 6bae6177 MD |
656 | if ((info.checkdef = checkdef) == NULL) |
| 657 | info.syncdeps = 1; | |
| 658 | ||
| 3b998fa9 | 659 | lwkt_gettoken(&vp->v_token); |
| e43a034f | 660 | |
| 6bae6177 | 661 | switch(waitfor) { |
| 28271622 | 662 | case MNT_LAZY | MNT_NOWAIT: |
| 6bae6177 MD |
663 | case MNT_LAZY: |
| 664 | /* | |
| 665 | * Lazy (filesystem syncer typ) Asynchronous plus limit the | |
| 666 | * number of data (not meta) pages we try to flush to 1MB. | |
| 667 | * A non-zero return means that lazy limit was reached. | |
| 668 | */ | |
| 669 | info.lazylimit = 1024 * 1024; | |
| 670 | info.syncdeps = 1; | |
| c5724852 | 671 | info.cmpfunc = vfsync_lazy_range_cmp; |
| 6bae6177 MD |
672 | error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, |
| 673 | vfsync_lazy_range_cmp, vfsync_bp, &info); | |
| c5724852 | 674 | info.cmpfunc = vfsync_meta_only_cmp; |
| 6bae6177 | 675 | RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, |
| c5724852 | 676 | vfsync_meta_only_cmp, vfsync_bp, &info); |
| 6bae6177 MD |
677 | if (error == 0) |
| 678 | vp->v_lazyw = 0; | |
| 679 | else if (!RB_EMPTY(&vp->v_rbdirty_tree)) | |
| 77912481 | 680 | vn_syncer_add(vp, 1); |
| 6bae6177 MD |
681 | error = 0; |
| 682 | break; | |
| 683 | case MNT_NOWAIT: | |
| 684 | /* | |
| 685 | * Asynchronous. Do a data-only pass and a meta-only pass. | |
| 686 | */ | |
| 687 | info.syncdeps = 1; | |
| c5724852 | 688 | info.cmpfunc = vfsync_data_only_cmp; |
| 6bae6177 MD |
689 | RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, |
| 690 | vfsync_bp, &info); | |
| c5724852 | 691 | info.cmpfunc = vfsync_meta_only_cmp; |
| 6bae6177 MD |
692 | RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp, |
| 693 | vfsync_bp, &info); | |
| 694 | error = 0; | |
| 695 | break; | |
| 696 | default: | |
| 697 | /* | |
| 698 | * Synchronous. Do a data-only pass, then a meta-data+data | |
| 699 | * pass, then additional integrated passes to try to get | |
| 700 | * all the dependancies flushed. | |
| 701 | */ | |
| c5724852 | 702 | info.cmpfunc = vfsync_data_only_cmp; |
| 6bae6177 MD |
703 | RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp, |
| 704 | vfsync_bp, &info); | |
| 705 | error = vfsync_wait_output(vp, waitoutput); | |
| 706 | if (error == 0) { | |
| 54078292 | 707 | info.skippedbufs = 0; |
| c5724852 | 708 | info.cmpfunc = vfsync_dummy_cmp; |
| 6bae6177 MD |
709 | RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, |
| 710 | vfsync_bp, &info); | |
| 711 | error = vfsync_wait_output(vp, waitoutput); | |
| 20045e8c MD |
712 | if (info.skippedbufs) { |
| 713 | kprintf("Warning: vfsync skipped %d dirty " | |
| 714 | "bufs in pass2!\n", info.skippedbufs); | |
| 715 | } | |
| 6bae6177 MD |
716 | } |
| 717 | while (error == 0 && passes > 0 && | |
| 0202303b MD |
718 | !RB_EMPTY(&vp->v_rbdirty_tree) |
| 719 | ) { | |
| 6bae6177 MD |
720 | if (--passes == 0) { |
| 721 | info.synchronous = 1; | |
| 722 | info.syncdeps = 1; | |
| 723 | } | |
| c5724852 | 724 | info.cmpfunc = vfsync_dummy_cmp; |
| 6bae6177 | 725 | error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, |
| c5724852 | 726 | vfsync_bp, &info); |
| 6bae6177 MD |
727 | if (error < 0) |
| 728 | error = -error; | |
| 729 | info.syncdeps = 1; | |
| 730 | if (error == 0) | |
| 731 | error = vfsync_wait_output(vp, waitoutput); | |
| 732 | } | |
| 733 | break; | |
| 734 | } | |
| 3b998fa9 | 735 | lwkt_reltoken(&vp->v_token); |
| 6bae6177 MD |
736 | return(error); |
| 737 | } | |
| 738 | ||
| 739 | static int | |
| a9a20f98 MD |
740 | vfsync_wait_output(struct vnode *vp, |
| 741 | int (*waitoutput)(struct vnode *, struct thread *)) | |
| 6bae6177 | 742 | { |
| a9a20f98 | 743 | int error; |
| 6bae6177 | 744 | |
| a9a20f98 | 745 | error = bio_track_wait(&vp->v_track_write, 0, 0); |
| 6bae6177 MD |
746 | if (waitoutput) |
| 747 | error = waitoutput(vp, curthread); | |
| 748 | return(error); | |
| 749 | } | |
| 750 | ||
| 751 | static int | |
| c5724852 MD |
752 | vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused) |
| 753 | { | |
| 754 | return(0); | |
| 755 | } | |
| 756 | ||
| 757 | static int | |
| 6bae6177 MD |
758 | vfsync_data_only_cmp(struct buf *bp, void *data) |
| 759 | { | |
| 54078292 | 760 | if (bp->b_loffset < 0) |
| 6bae6177 MD |
761 | return(-1); |
| 762 | return(0); | |
| 763 | } | |
| 764 | ||
| 765 | static int | |
| 766 | vfsync_meta_only_cmp(struct buf *bp, void *data) | |
| 767 | { | |
| 54078292 | 768 | if (bp->b_loffset < 0) |
| 6bae6177 MD |
769 | return(0); |
| 770 | return(1); | |
| 771 | } | |
| 772 | ||
| 773 | static int | |
| 774 | vfsync_lazy_range_cmp(struct buf *bp, void *data) | |
| 775 | { | |
| 776 | struct vfsync_info *info = data; | |
| c5724852 | 777 | |
| 54078292 | 778 | if (bp->b_loffset < info->vp->v_lazyw) |
| 6bae6177 MD |
779 | return(-1); |
| 780 | return(0); | |
| 781 | } | |
| 782 | ||
| 783 | static int | |
| 784 | vfsync_bp(struct buf *bp, void *data) | |
| 785 | { | |
| 786 | struct vfsync_info *info = data; | |
| 787 | struct vnode *vp = info->vp; | |
| 788 | int error; | |
| 789 | ||
| 790 | /* | |
| c5724852 MD |
791 | * Ignore buffers that we cannot immediately lock. |
| 792 | */ | |
| 793 | if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { | |
| c5724852 MD |
794 | ++info->skippedbufs; |
| 795 | return(0); | |
| 796 | } | |
| 797 | ||
| 798 | /* | |
| 799 | * We must revalidate the buffer after locking. | |
| 6bae6177 | 800 | */ |
| c5724852 MD |
801 | if ((bp->b_flags & B_DELWRI) == 0 || |
| 802 | bp->b_vp != info->vp || | |
| 803 | info->cmpfunc(bp, data)) { | |
| 804 | BUF_UNLOCK(bp); | |
| 6bae6177 | 805 | return(0); |
| c5724852 | 806 | } |
| 6bae6177 MD |
807 | |
| 808 | /* | |
| c5724852 MD |
809 | * If syncdeps is not set we do not try to write buffers which have |
| 810 | * dependancies. | |
| 6bae6177 | 811 | */ |
| c5724852 MD |
812 | if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) { |
| 813 | BUF_UNLOCK(bp); | |
| 6bae6177 | 814 | return(0); |
| 54078292 | 815 | } |
| 6bae6177 MD |
816 | |
| 817 | /* | |
| 818 | * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer | |
| 819 | * has been written but an additional handshake with the device | |
| 820 | * is required before we can dispose of the buffer. We have no idea | |
| 821 | * how to do this so we have to skip these buffers. | |
| 822 | */ | |
| 823 | if (bp->b_flags & B_NEEDCOMMIT) { | |
| 824 | BUF_UNLOCK(bp); | |
| 825 | return(0); | |
| 826 | } | |
| 827 | ||
| 17a8ba12 | 828 | /* |
| c5724852 MD |
829 | * Ask bioops if it is ok to sync. If not the VFS may have |
| 830 | * set B_LOCKED so we have to cycle the buffer. | |
| 17a8ba12 MD |
831 | */ |
| 832 | if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) { | |
| 833 | bremfree(bp); | |
| 834 | brelse(bp); | |
| 835 | return(0); | |
| 836 | } | |
| 837 | ||
| 6bae6177 MD |
838 | if (info->synchronous) { |
| 839 | /* | |
| 840 | * Synchronous flushing. An error may be returned. | |
| 841 | */ | |
| 842 | bremfree(bp); | |
| 6bae6177 | 843 | error = bwrite(bp); |
| 6bae6177 MD |
844 | } else { |
| 845 | /* | |
| 846 | * Asynchronous flushing. A negative return value simply | |
| 847 | * stops the scan and is not considered an error. We use | |
| 848 | * this to support limited MNT_LAZY flushes. | |
| 849 | */ | |
| 54078292 | 850 | vp->v_lazyw = bp->b_loffset; |
| 9de13b88 MD |
851 | bremfree(bp); |
| 852 | info->lazycount += cluster_awrite(bp); | |
| aa1bfd98 | 853 | waitrunningbufspace(); |
| 55b50bd5 | 854 | vm_wait_nominal(); |
| 6bae6177 MD |
855 | if (info->lazylimit && info->lazycount >= info->lazylimit) |
| 856 | error = 1; | |
| 857 | else | |
| 858 | error = 0; | |
| 859 | } | |
| 860 | return(-error); | |
| 861 | } | |
| 862 | ||
| 863 | /* | |
| 984263bc | 864 | * Associate a buffer with a vnode. |
| b1c20cfa MD |
865 | * |
| 866 | * MPSAFE | |
| 984263bc | 867 | */ |
| b1c20cfa | 868 | int |
| 7608650f | 869 | bgetvp(struct vnode *vp, struct buf *bp, int testsize) |
| 984263bc | 870 | { |
| 984263bc | 871 | KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); |
| 9e45bec7 | 872 | KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0); |
| 984263bc | 873 | |
| 0202303b | 874 | /* |
| 984263bc MD |
875 | * Insert onto list for new vnode. |
| 876 | */ | |
| 3b998fa9 | 877 | lwkt_gettoken(&vp->v_token); |
| c5724852 | 878 | |
| b1c20cfa | 879 | if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) { |
| 3b998fa9 | 880 | lwkt_reltoken(&vp->v_token); |
| b1c20cfa MD |
881 | return (EEXIST); |
| 882 | } | |
| 7608650f MD |
883 | |
| 884 | /* | |
| 885 | * Diagnostics (mainly for HAMMER debugging). Check for | |
| 886 | * overlapping buffers. | |
| 887 | */ | |
| 888 | if (check_buf_overlap) { | |
| 889 | struct buf *bx; | |
| 890 | bx = buf_rb_hash_RB_PREV(bp); | |
| 891 | if (bx) { | |
| 892 | if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) { | |
| 893 | kprintf("bgetvp: overlapl %016jx/%d %016jx " | |
| 894 | "bx %p bp %p\n", | |
| 895 | (intmax_t)bx->b_loffset, | |
| 896 | bx->b_bufsize, | |
| 897 | (intmax_t)bp->b_loffset, | |
| 898 | bx, bp); | |
| 899 | if (check_buf_overlap > 1) | |
| 900 | panic("bgetvp - overlapping buffer"); | |
| 901 | } | |
| 902 | } | |
| 903 | bx = buf_rb_hash_RB_NEXT(bp); | |
| 904 | if (bx) { | |
| 905 | if (bp->b_loffset + testsize > bx->b_loffset) { | |
| 906 | kprintf("bgetvp: overlapr %016jx/%d %016jx " | |
| 907 | "bp %p bx %p\n", | |
| 908 | (intmax_t)bp->b_loffset, | |
| 909 | testsize, | |
| 910 | (intmax_t)bx->b_loffset, | |
| 911 | bp, bx); | |
| 912 | if (check_buf_overlap > 1) | |
| 913 | panic("bgetvp - overlapping buffer"); | |
| 914 | } | |
| 915 | } | |
| 916 | } | |
| 1f1ea522 MD |
917 | bp->b_vp = vp; |
| 918 | bp->b_flags |= B_HASHED; | |
| 9e45bec7 | 919 | bp->b_flags |= B_VNCLEAN; |
| 6bae6177 | 920 | if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) |
| 1f1ea522 | 921 | panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp); |
| b1c20cfa | 922 | vhold(vp); |
| 3b998fa9 | 923 | lwkt_reltoken(&vp->v_token); |
| b1c20cfa | 924 | return(0); |
| 984263bc MD |
925 | } |
| 926 | ||
| 927 | /* | |
| 928 | * Disassociate a buffer from a vnode. | |
| 77912481 MD |
929 | * |
| 930 | * MPSAFE | |
| 984263bc MD |
931 | */ |
| 932 | void | |
| dd98570a | 933 | brelvp(struct buf *bp) |
| 984263bc MD |
934 | { |
| 935 | struct vnode *vp; | |
| 984263bc MD |
936 | |
| 937 | KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); | |
| 938 | ||
| 939 | /* | |
| 940 | * Delete from old vnode list, if on one. | |
| 941 | */ | |
| 942 | vp = bp->b_vp; | |
| 3b998fa9 | 943 | lwkt_gettoken(&vp->v_token); |
| 9e45bec7 MD |
944 | if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) { |
| 945 | if (bp->b_flags & B_VNDIRTY) | |
| 6bae6177 MD |
946 | buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); |
| 947 | else | |
| 948 | buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); | |
| 9e45bec7 | 949 | bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN); |
| 984263bc | 950 | } |
| 1f1ea522 MD |
951 | if (bp->b_flags & B_HASHED) { |
| 952 | buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp); | |
| 953 | bp->b_flags &= ~B_HASHED; | |
| 954 | } | |
| 77912481 MD |
955 | if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) |
| 956 | vn_syncer_remove(vp); | |
| 5fd012e0 | 957 | bp->b_vp = NULL; |
| 77912481 | 958 | |
| 3b998fa9 | 959 | lwkt_reltoken(&vp->v_token); |
| 0202303b | 960 | |
| 5fd012e0 | 961 | vdrop(vp); |
| 984263bc MD |
962 | } |
| 963 | ||
| 964 | /* | |
| 1f1ea522 MD |
965 | * Reassign the buffer to the proper clean/dirty list based on B_DELWRI. |
| 966 | * This routine is called when the state of the B_DELWRI bit is changed. | |
| b1c20cfa | 967 | * |
| c5724852 | 968 | * Must be called with vp->v_token held. |
| b1c20cfa | 969 | * MPSAFE |
| 984263bc MD |
970 | */ |
| 971 | void | |
| 1f1ea522 | 972 | reassignbuf(struct buf *bp) |
| 984263bc | 973 | { |
| 1f1ea522 | 974 | struct vnode *vp = bp->b_vp; |
| 984263bc | 975 | int delay; |
| 984263bc | 976 | |
| c5724852 | 977 | ASSERT_LWKT_TOKEN_HELD(&vp->v_token); |
| 984263bc MD |
978 | ++reassignbufcalls; |
| 979 | ||
| 980 | /* | |
| 981 | * B_PAGING flagged buffers cannot be reassigned because their vp | |
| 982 | * is not fully linked in. | |
| 983 | */ | |
| 984 | if (bp->b_flags & B_PAGING) | |
| 985 | panic("cannot reassign paging buffer"); | |
| 986 | ||
| 984263bc | 987 | if (bp->b_flags & B_DELWRI) { |
| 1f1ea522 MD |
988 | /* |
| 989 | * Move to the dirty list, add the vnode to the worklist | |
| 990 | */ | |
| 9e45bec7 | 991 | if (bp->b_flags & B_VNCLEAN) { |
| 1f1ea522 | 992 | buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp); |
| 9e45bec7 | 993 | bp->b_flags &= ~B_VNCLEAN; |
| 1f1ea522 | 994 | } |
| 9e45bec7 | 995 | if ((bp->b_flags & B_VNDIRTY) == 0) { |
| 1f1ea522 MD |
996 | if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) { |
| 997 | panic("reassignbuf: dup lblk vp %p bp %p", | |
| 998 | vp, bp); | |
| 999 | } | |
| 9e45bec7 | 1000 | bp->b_flags |= B_VNDIRTY; |
| 1f1ea522 MD |
1001 | } |
| 1002 | if ((vp->v_flag & VONWORKLST) == 0) { | |
| 1003 | switch (vp->v_type) { | |
| 984263bc MD |
1004 | case VDIR: |
| 1005 | delay = dirdelay; | |
| 1006 | break; | |
| 1007 | case VCHR: | |
| 1008 | case VBLK: | |
| 1f1ea522 MD |
1009 | if (vp->v_rdev && |
| 1010 | vp->v_rdev->si_mountpoint != NULL) { | |
| 984263bc MD |
1011 | delay = metadelay; |
| 1012 | break; | |
| 1013 | } | |
| 1014 | /* fall through */ | |
| 1015 | default: | |
| 1016 | delay = filedelay; | |
| 1017 | } | |
| 77912481 | 1018 | vn_syncer_add(vp, delay); |
| 984263bc | 1019 | } |
| 984263bc | 1020 | } else { |
| 1f1ea522 MD |
1021 | /* |
| 1022 | * Move to the clean list, remove the vnode from the worklist | |
| 1023 | * if no dirty blocks remain. | |
| 1024 | */ | |
| 9e45bec7 | 1025 | if (bp->b_flags & B_VNDIRTY) { |
| 1f1ea522 | 1026 | buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp); |
| 9e45bec7 | 1027 | bp->b_flags &= ~B_VNDIRTY; |
| 1f1ea522 | 1028 | } |
| 9e45bec7 | 1029 | if ((bp->b_flags & B_VNCLEAN) == 0) { |
| 1f1ea522 MD |
1030 | if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) { |
| 1031 | panic("reassignbuf: dup lblk vp %p bp %p", | |
| 1032 | vp, bp); | |
| 1033 | } | |
| 9e45bec7 | 1034 | bp->b_flags |= B_VNCLEAN; |
| 1f1ea522 MD |
1035 | } |
| 1036 | if ((vp->v_flag & VONWORKLST) && | |
| 1037 | RB_EMPTY(&vp->v_rbdirty_tree)) { | |
| 77912481 | 1038 | vn_syncer_remove(vp); |
| 984263bc | 1039 | } |
| 984263bc | 1040 | } |
| 984263bc MD |
1041 | } |
| 1042 | ||
| 1043 | /* | |
| 2d5beb81 MD |
1044 | * Create a vnode for a block device. Used for mounting the root file |
| 1045 | * system. | |
| 1046 | * | |
| 1047 | * A vref()'d vnode is returned. | |
| 984263bc | 1048 | */ |
| cd29885a | 1049 | extern struct vop_ops *devfs_vnode_dev_vops_p; |
| 984263bc | 1050 | int |
| b13267a5 | 1051 | bdevvp(cdev_t dev, struct vnode **vpp) |
| 984263bc | 1052 | { |
| 1fd87d54 | 1053 | struct vnode *vp; |
| 984263bc MD |
1054 | struct vnode *nvp; |
| 1055 | int error; | |
| 1056 | ||
| 028066b1 | 1057 | if (dev == NULL) { |
| 984263bc MD |
1058 | *vpp = NULLVP; |
| 1059 | return (ENXIO); | |
| 1060 | } | |
| aec8eea4 MD |
1061 | error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p, |
| 1062 | &nvp, 0, 0); | |
| 984263bc MD |
1063 | if (error) { |
| 1064 | *vpp = NULLVP; | |
| 1065 | return (error); | |
| 1066 | } | |
| 1067 | vp = nvp; | |
| e4c9c0c8 | 1068 | vp->v_type = VCHR; |
| 9b823501 | 1069 | #if 0 |
| cd29885a | 1070 | vp->v_rdev = dev; |
| 9b823501 AH |
1071 | #endif |
| 1072 | v_associate_rdev(vp, dev); | |
| 0e9b9130 MD |
1073 | vp->v_umajor = dev->si_umajor; |
| 1074 | vp->v_uminor = dev->si_uminor; | |
| 5fd012e0 | 1075 | vx_unlock(vp); |
| 984263bc MD |
1076 | *vpp = vp; |
| 1077 | return (0); | |
| 5fd012e0 | 1078 | } |
| 41a01a4d | 1079 | |
| 984263bc | 1080 | int |
| b13267a5 | 1081 | v_associate_rdev(struct vnode *vp, cdev_t dev) |
| 984263bc | 1082 | { |
| 0e9b9130 | 1083 | if (dev == NULL) |
| 5fd012e0 MD |
1084 | return(ENXIO); |
| 1085 | if (dev_is_good(dev) == 0) | |
| 1086 | return(ENXIO); | |
| 1087 | KKASSERT(vp->v_rdev == NULL); | |
| 5fd012e0 | 1088 | vp->v_rdev = reference_dev(dev); |
| 3b998fa9 | 1089 | lwkt_gettoken(&spechash_token); |
| 0de08e6d | 1090 | SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext); |
| 3b998fa9 | 1091 | lwkt_reltoken(&spechash_token); |
| 5fd012e0 MD |
1092 | return(0); |
| 1093 | } | |
| 984263bc | 1094 | |
| 5fd012e0 MD |
1095 | void |
| 1096 | v_release_rdev(struct vnode *vp) | |
| 1097 | { | |
| b13267a5 | 1098 | cdev_t dev; |
| 984263bc | 1099 | |
| 5fd012e0 | 1100 | if ((dev = vp->v_rdev) != NULL) { |
| 3b998fa9 | 1101 | lwkt_gettoken(&spechash_token); |
| 0de08e6d | 1102 | SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext); |
| 5fd012e0 | 1103 | vp->v_rdev = NULL; |
| 5fd012e0 | 1104 | release_dev(dev); |
| 3b998fa9 | 1105 | lwkt_reltoken(&spechash_token); |
| 984263bc | 1106 | } |
| 984263bc MD |
1107 | } |
| 1108 | ||
| 1109 | /* | |
| b13267a5 | 1110 | * Add a vnode to the alias list hung off the cdev_t. We only associate |
| 5fd012e0 MD |
1111 | * the device number with the vnode. The actual device is not associated |
| 1112 | * until the vnode is opened (usually in spec_open()), and will be | |
| 1113 | * disassociated on last close. | |
| 984263bc | 1114 | */ |
| 5fd012e0 | 1115 | void |
| 0e9b9130 | 1116 | addaliasu(struct vnode *nvp, int x, int y) |
| 984263bc | 1117 | { |
| 5fd012e0 MD |
1118 | if (nvp->v_type != VBLK && nvp->v_type != VCHR) |
| 1119 | panic("addaliasu on non-special vnode"); | |
| 0e9b9130 MD |
1120 | nvp->v_umajor = x; |
| 1121 | nvp->v_uminor = y; | |
| 984263bc MD |
1122 | } |
| 1123 | ||
| 1124 | /* | |
| cf683bae MD |
1125 | * Simple call that a filesystem can make to try to get rid of a |
| 1126 | * vnode. It will fail if anyone is referencing the vnode (including | |
| 1127 | * the caller). | |
| 1128 | * | |
| 1129 | * The filesystem can check whether its in-memory inode structure still | |
| 1130 | * references the vp on return. | |
| 1131 | */ | |
| 1132 | void | |
| 1133 | vclean_unlocked(struct vnode *vp) | |
| 1134 | { | |
| 1135 | vx_get(vp); | |
| 1136 | if (sysref_isactive(&vp->v_sysref) == 0) | |
| 2b4ed70b | 1137 | vgone_vxlocked(vp); |
| cf683bae MD |
1138 | vx_put(vp); |
| 1139 | } | |
| 1140 | ||
| 1141 | /* | |
| 5fd012e0 MD |
1142 | * Disassociate a vnode from its underlying filesystem. |
| 1143 | * | |
| 3c37c940 MD |
1144 | * The vnode must be VX locked and referenced. In all normal situations |
| 1145 | * there are no active references. If vclean_vxlocked() is called while | |
| 1146 | * there are active references, the vnode is being ripped out and we have | |
| 1147 | * to call VOP_CLOSE() as appropriate before we can reclaim it. | |
| 984263bc | 1148 | */ |
| 5fd012e0 | 1149 | void |
| 3c37c940 | 1150 | vclean_vxlocked(struct vnode *vp, int flags) |
| 984263bc MD |
1151 | { |
| 1152 | int active; | |
| 8ddc6004 | 1153 | int n; |
| 7540ab49 | 1154 | vm_object_t object; |
| 28271622 | 1155 | struct namecache *ncp; |
| 984263bc MD |
1156 | |
| 1157 | /* | |
| 5fd012e0 | 1158 | * If the vnode has already been reclaimed we have nothing to do. |
| 984263bc | 1159 | */ |
| 3c37c940 | 1160 | if (vp->v_flag & VRECLAIMED) |
| 5fd012e0 | 1161 | return; |
| 2247fe02 | 1162 | vsetflags(vp, VRECLAIMED); |
| 984263bc | 1163 | |
| 28271622 MD |
1164 | if (verbose_reclaims) { |
| 1165 | if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL) | |
| 1166 | kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name); | |
| 1167 | } | |
| 1168 | ||
| 984263bc | 1169 | /* |
| 5fd012e0 | 1170 | * Scrap the vfs cache |
| 984263bc | 1171 | */ |
| 6b008938 | 1172 | while (cache_inval_vp(vp, 0) != 0) { |
| 28271622 MD |
1173 | kprintf("Warning: vnode %p clean/cache_resolution " |
| 1174 | "race detected\n", vp); | |
| 25cb3304 MD |
1175 | tsleep(vp, 0, "vclninv", 2); |
| 1176 | } | |
| 41a01a4d | 1177 | |
| 984263bc | 1178 | /* |
| 5fd012e0 MD |
1179 | * Check to see if the vnode is in use. If so we have to reference it |
| 1180 | * before we clean it out so that its count cannot fall to zero and | |
| 1181 | * generate a race against ourselves to recycle it. | |
| 984263bc | 1182 | */ |
| 3c37c940 | 1183 | active = sysref_isactive(&vp->v_sysref); |
| 984263bc MD |
1184 | |
| 1185 | /* | |
| 5fd012e0 | 1186 | * Clean out any buffers associated with the vnode and destroy its |
| 7540ab49 | 1187 | * object, if it has one. |
| 984263bc | 1188 | */ |
| 87de5057 | 1189 | vinvalbuf(vp, V_SAVE, 0, 0); |
| 7540ab49 | 1190 | |
| 984263bc | 1191 | /* |
| 8ddc6004 MD |
1192 | * If purging an active vnode (typically during a forced unmount |
| 1193 | * or reboot), it must be closed and deactivated before being | |
| 1194 | * reclaimed. This isn't really all that safe, but what can | |
| 1195 | * we do? XXX. | |
| 5fd012e0 MD |
1196 | * |
| 1197 | * Note that neither of these routines unlocks the vnode. | |
| 984263bc | 1198 | */ |
| 8ddc6004 MD |
1199 | if (active && (flags & DOCLOSE)) { |
| 1200 | while ((n = vp->v_opencount) != 0) { | |
| 1201 | if (vp->v_writecount) | |
| 87de5057 | 1202 | VOP_CLOSE(vp, FWRITE|FNONBLOCK); |
| 8ddc6004 | 1203 | else |
| 87de5057 | 1204 | VOP_CLOSE(vp, FNONBLOCK); |
| 8ddc6004 | 1205 | if (vp->v_opencount == n) { |
| 6ea70f76 | 1206 | kprintf("Warning: unable to force-close" |
| 8ddc6004 MD |
1207 | " vnode %p\n", vp); |
| 1208 | break; | |
| 1209 | } | |
| 1210 | } | |
| 5fd012e0 MD |
1211 | } |
| 1212 | ||
| 1213 | /* | |
| 64e0b2d3 | 1214 | * If the vnode has not been deactivated, deactivated it. Deactivation |
| e3bc9a94 MD |
1215 | * can create new buffers and VM pages so we have to call vinvalbuf() |
| 1216 | * again to make sure they all get flushed. | |
| 1217 | * | |
| 1218 | * This can occur if a file with a link count of 0 needs to be | |
| 1219 | * truncated. | |
| 2247fe02 MD |
1220 | * |
| 1221 | * If the vnode is already dead don't try to deactivate it. | |
| 5fd012e0 MD |
1222 | */ |
| 1223 | if ((vp->v_flag & VINACTIVE) == 0) { | |
| 2247fe02 MD |
1224 | vsetflags(vp, VINACTIVE); |
| 1225 | if (vp->v_mount) | |
| 1226 | VOP_INACTIVE(vp); | |
| e3bc9a94 MD |
1227 | vinvalbuf(vp, V_SAVE, 0, 0); |
| 1228 | } | |
| 1229 | ||
| 1230 | /* | |
| 1231 | * If the vnode has an object, destroy it. | |
| 1232 | */ | |
| b12defdc MD |
1233 | while ((object = vp->v_object) != NULL) { |
| 1234 | vm_object_hold(object); | |
| 1235 | if (object == vp->v_object) | |
| 1236 | break; | |
| 1237 | vm_object_drop(object); | |
| 1238 | } | |
| 1239 | ||
| 398c240d | 1240 | if (object != NULL) { |
| e3bc9a94 | 1241 | if (object->ref_count == 0) { |
| b12defdc | 1242 | if ((object->flags & OBJ_DEAD) == 0) |
| e3bc9a94 | 1243 | vm_object_terminate(object); |
| b12defdc | 1244 | vm_object_drop(object); |
| 212f39f5 | 1245 | vclrflags(vp, VOBJBUF); |
| e3bc9a94 MD |
1246 | } else { |
| 1247 | vm_pager_deallocate(object); | |
| 212f39f5 | 1248 | vclrflags(vp, VOBJBUF); |
| b12defdc | 1249 | vm_object_drop(object); |
| e3bc9a94 | 1250 | } |
| 984263bc | 1251 | } |
| e3bc9a94 MD |
1252 | KKASSERT((vp->v_flag & VOBJBUF) == 0); |
| 1253 | ||
| 984263bc | 1254 | /* |
| 2247fe02 | 1255 | * Reclaim the vnode if not already dead. |
| 984263bc | 1256 | */ |
| 2247fe02 | 1257 | if (vp->v_mount && VOP_RECLAIM(vp)) |
| 984263bc MD |
1258 | panic("vclean: cannot reclaim"); |
| 1259 | ||
| 984263bc MD |
1260 | /* |
| 1261 | * Done with purge, notify sleepers of the grim news. | |
| 1262 | */ | |
| 66a1ddf5 | 1263 | vp->v_ops = &dead_vnode_vops_p; |
| 22a90887 | 1264 | vn_gone(vp); |
| 984263bc | 1265 | vp->v_tag = VT_NON; |
| 64e0b2d3 MD |
1266 | |
| 1267 | /* | |
| 1268 | * If we are destroying an active vnode, reactivate it now that | |
| 1269 | * we have reassociated it with deadfs. This prevents the system | |
| 1270 | * from crashing on the vnode due to it being unexpectedly marked | |
| 1271 | * as inactive or reclaimed. | |
| 1272 | */ | |
| 1273 | if (active && (flags & DOCLOSE)) { | |
| 2247fe02 | 1274 | vclrflags(vp, VINACTIVE | VRECLAIMED); |
| 64e0b2d3 | 1275 | } |
| 984263bc MD |
1276 | } |
| 1277 | ||
| 1278 | /* | |
| 1279 | * Eliminate all activity associated with the requested vnode | |
| 1280 | * and with all vnodes aliased to the requested vnode. | |
| dd98570a | 1281 | * |
| b8477cda | 1282 | * The vnode must be referenced but should not be locked. |
| 984263bc MD |
1283 | */ |
| 1284 | int | |
| b8477cda | 1285 | vrevoke(struct vnode *vp, struct ucred *cred) |
| 984263bc | 1286 | { |
| b8477cda | 1287 | struct vnode *vq; |
| a32446b7 | 1288 | struct vnode *vqn; |
| b13267a5 | 1289 | cdev_t dev; |
| b8477cda | 1290 | int error; |
| e4c9c0c8 MD |
1291 | |
| 1292 | /* | |
| 1293 | * If the vnode has a device association, scrap all vnodes associated | |
| 1294 | * with the device. Don't let the device disappear on us while we | |
| 1295 | * are scrapping the vnodes. | |
| 5fd012e0 MD |
1296 | * |
| 1297 | * The passed vp will probably show up in the list, do not VX lock | |
| 1298 | * it twice! | |
| a32446b7 MD |
1299 | * |
| 1300 | * Releasing the vnode's rdev here can mess up specfs's call to | |
| 1301 | * device close, so don't do it. The vnode has been disassociated | |
| 1302 | * and the device will be closed after the last ref on the related | |
| 1303 | * fp goes away (if not still open by e.g. the kernel). | |
| e4c9c0c8 | 1304 | */ |
| b8477cda MD |
1305 | if (vp->v_type != VCHR) { |
| 1306 | error = fdrevoke(vp, DTYPE_VNODE, cred); | |
| 1307 | return (error); | |
| 1308 | } | |
| e4c9c0c8 | 1309 | if ((dev = vp->v_rdev) == NULL) { |
| cd29885a | 1310 | return(0); |
| e4c9c0c8 MD |
1311 | } |
| 1312 | reference_dev(dev); | |
| 3b998fa9 | 1313 | lwkt_gettoken(&spechash_token); |
| a32446b7 | 1314 | |
| d557dd6d | 1315 | restart: |
| a32446b7 MD |
1316 | vqn = SLIST_FIRST(&dev->si_hlist); |
| 1317 | if (vqn) | |
| d557dd6d | 1318 | vhold(vqn); |
| a32446b7 | 1319 | while ((vq = vqn) != NULL) { |
| d557dd6d MD |
1320 | if (sysref_isactive(&vq->v_sysref)) { |
| 1321 | vref(vq); | |
| 1322 | fdrevoke(vq, DTYPE_VNODE, cred); | |
| 1323 | /*v_release_rdev(vq);*/ | |
| 1324 | vrele(vq); | |
| 1325 | if (vq->v_rdev != dev) { | |
| 1326 | vdrop(vq); | |
| 1327 | goto restart; | |
| 1328 | } | |
| 1329 | } | |
| 1330 | vqn = SLIST_NEXT(vq, v_cdevnext); | |
| a32446b7 | 1331 | if (vqn) |
| d557dd6d MD |
1332 | vhold(vqn); |
| 1333 | vdrop(vq); | |
| 984263bc | 1334 | } |
| 3b998fa9 | 1335 | lwkt_reltoken(&spechash_token); |
| a32446b7 | 1336 | dev_drevoke(dev); |
| 9b823501 | 1337 | release_dev(dev); |
| 984263bc MD |
1338 | return (0); |
| 1339 | } | |
| 1340 | ||
| 1341 | /* | |
| 3c37c940 MD |
1342 | * This is called when the object underlying a vnode is being destroyed, |
| 1343 | * such as in a remove(). Try to recycle the vnode immediately if the | |
| 1344 | * only active reference is our reference. | |
| c0c70b27 MD |
1345 | * |
| 1346 | * Directory vnodes in the namecache with children cannot be immediately | |
| 1347 | * recycled because numerous VOP_N*() ops require them to be stable. | |
| 1b7df30a MD |
1348 | * |
| 1349 | * To avoid recursive recycling from VOP_INACTIVE implemenetations this | |
| 1350 | * function is a NOP if VRECLAIMED is already set. | |
| 984263bc MD |
1351 | */ |
| 1352 | int | |
| 87de5057 | 1353 | vrecycle(struct vnode *vp) |
| 984263bc | 1354 | { |
| 1b7df30a | 1355 | if (vp->v_sysref.refcnt <= 1 && (vp->v_flag & VRECLAIMED) == 0) { |
| c0c70b27 MD |
1356 | if (cache_inval_vp_nonblock(vp)) |
| 1357 | return(0); | |
| 3c37c940 | 1358 | vgone_vxlocked(vp); |
| 984263bc MD |
1359 | return (1); |
| 1360 | } | |
| 984263bc MD |
1361 | return (0); |
| 1362 | } | |
| 1363 | ||
| 1364 | /* | |
| 2ec4b00d MD |
1365 | * Return the maximum I/O size allowed for strategy calls on VP. |
| 1366 | * | |
| 1367 | * If vp is VCHR or VBLK we dive the device, otherwise we use | |
| 1368 | * the vp's mount info. | |
| 201cd612 MD |
1369 | * |
| 1370 | * The returned value is clamped at MAXPHYS as most callers cannot use | |
| 1371 | * buffers larger than that size. | |
| 2ec4b00d MD |
1372 | */ |
| 1373 | int | |
| 1374 | vmaxiosize(struct vnode *vp) | |
| 1375 | { | |
| 201cd612 MD |
1376 | int maxiosize; |
| 1377 | ||
| 1378 | if (vp->v_type == VBLK || vp->v_type == VCHR) | |
| 1379 | maxiosize = vp->v_rdev->si_iosize_max; | |
| 1380 | else | |
| 1381 | maxiosize = vp->v_mount->mnt_iosize_max; | |
| 1382 | ||
| 1383 | if (maxiosize > MAXPHYS) | |
| 1384 | maxiosize = MAXPHYS; | |
| 1385 | return (maxiosize); | |
| 2ec4b00d MD |
1386 | } |
| 1387 | ||
| 1388 | /* | |
| 5fd012e0 MD |
1389 | * Eliminate all activity associated with a vnode in preparation for reuse. |
| 1390 | * | |
| 57ac0c99 MD |
1391 | * The vnode must be VX locked and refd and will remain VX locked and refd |
| 1392 | * on return. This routine may be called with the vnode in any state, as | |
| 1393 | * long as it is VX locked. The vnode will be cleaned out and marked | |
| 1394 | * VRECLAIMED but will not actually be reused until all existing refs and | |
| 1395 | * holds go away. | |
| 5fd012e0 MD |
1396 | * |
| 1397 | * NOTE: This routine may be called on a vnode which has not yet been | |
| 1398 | * already been deactivated (VOP_INACTIVE), or on a vnode which has | |
| 1399 | * already been reclaimed. | |
| 1400 | * | |
| 1401 | * This routine is not responsible for placing us back on the freelist. | |
| 1402 | * Instead, it happens automatically when the caller releases the VX lock | |
| 1403 | * (assuming there aren't any other references). | |
| 984263bc | 1404 | */ |
| e3332475 | 1405 | void |
| 3c37c940 | 1406 | vgone_vxlocked(struct vnode *vp) |
| e3332475 | 1407 | { |
| 984263bc | 1408 | /* |
| 5fd012e0 | 1409 | * assert that the VX lock is held. This is an absolute requirement |
| 3c37c940 | 1410 | * now for vgone_vxlocked() to be called. |
| 984263bc | 1411 | */ |
| 5fd012e0 | 1412 | KKASSERT(vp->v_lock.lk_exclusivecount == 1); |
| 984263bc MD |
1413 | |
| 1414 | /* | |
| 5fd012e0 | 1415 | * Clean out the filesystem specific data and set the VRECLAIMED |
| e3332475 | 1416 | * bit. Also deactivate the vnode if necessary. |
| 984263bc | 1417 | */ |
| 3c37c940 | 1418 | vclean_vxlocked(vp, DOCLOSE); |
| 984263bc MD |
1419 | |
| 1420 | /* | |
| 1421 | * Delete from old mount point vnode list, if on one. | |
| 1422 | */ | |
| 1b7df30a MD |
1423 | if (vp->v_mount != NULL) { |
| 1424 | KKASSERT(vp->v_data == NULL); | |
| 5fd012e0 | 1425 | insmntque(vp, NULL); |
| 1b7df30a | 1426 | } |
| dd98570a | 1427 | |
| 984263bc MD |
1428 | /* |
| 1429 | * If special device, remove it from special device alias list | |
| 1fbb5fc0 MD |
1430 | * if it is on one. This should normally only occur if a vnode is |
| 1431 | * being revoked as the device should otherwise have been released | |
| 1432 | * naturally. | |
| 984263bc MD |
1433 | */ |
| 1434 | if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) { | |
| e4c9c0c8 | 1435 | v_release_rdev(vp); |
| 984263bc MD |
1436 | } |
| 1437 | ||
| 1438 | /* | |
| 5fd012e0 | 1439 | * Set us to VBAD |
| 984263bc | 1440 | */ |
| 984263bc | 1441 | vp->v_type = VBAD; |
| 984263bc MD |
1442 | } |
| 1443 | ||
| 1444 | /* | |
| 1445 | * Lookup a vnode by device number. | |
| 3875f5b0 MD |
1446 | * |
| 1447 | * Returns non-zero and *vpp set to a vref'd vnode on success. | |
| 1448 | * Returns zero on failure. | |
| 984263bc MD |
1449 | */ |
| 1450 | int | |
| b13267a5 | 1451 | vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp) |
| 984263bc MD |
1452 | { |
| 1453 | struct vnode *vp; | |
| 1454 | ||
| 3b998fa9 | 1455 | lwkt_gettoken(&spechash_token); |
| 0de08e6d | 1456 | SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) { |
| 984263bc MD |
1457 | if (type == vp->v_type) { |
| 1458 | *vpp = vp; | |
| 3875f5b0 | 1459 | vref(vp); |
| 3b998fa9 | 1460 | lwkt_reltoken(&spechash_token); |
| 984263bc MD |
1461 | return (1); |
| 1462 | } | |
| 1463 | } | |
| 3b998fa9 | 1464 | lwkt_reltoken(&spechash_token); |
| 984263bc MD |
1465 | return (0); |
| 1466 | } | |
| 1467 | ||
| 1468 | /* | |
| e4c9c0c8 MD |
1469 | * Calculate the total number of references to a special device. This |
| 1470 | * routine may only be called for VBLK and VCHR vnodes since v_rdev is | |
| 028066b1 | 1471 | * an overloaded field. Since udev2dev can now return NULL, we have |
| e4c9c0c8 | 1472 | * to check for a NULL v_rdev. |
| 984263bc MD |
1473 | */ |
| 1474 | int | |
| b13267a5 | 1475 | count_dev(cdev_t dev) |
| 984263bc | 1476 | { |
| e4c9c0c8 MD |
1477 | struct vnode *vp; |
| 1478 | int count = 0; | |
| 984263bc | 1479 | |
| e4c9c0c8 | 1480 | if (SLIST_FIRST(&dev->si_hlist)) { |
| 3b998fa9 | 1481 | lwkt_gettoken(&spechash_token); |
| 0de08e6d | 1482 | SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) { |
| 9b823501 | 1483 | count += vp->v_opencount; |
| e4c9c0c8 | 1484 | } |
| 3b998fa9 | 1485 | lwkt_reltoken(&spechash_token); |
| e4c9c0c8 MD |
1486 | } |
| 1487 | return(count); | |
| 984263bc MD |
1488 | } |
| 1489 | ||
| 984263bc | 1490 | int |
| e4c9c0c8 MD |
1491 | vcount(struct vnode *vp) |
| 1492 | { | |
| 1493 | if (vp->v_rdev == NULL) | |
| 1494 | return(0); | |
| 1495 | return(count_dev(vp->v_rdev)); | |
| 984263bc MD |
1496 | } |
| 1497 | ||
| 1498 | /* | |
| 1c843a13 MD |
1499 | * Initialize VMIO for a vnode. This routine MUST be called before a |
| 1500 | * VFS can issue buffer cache ops on a vnode. It is typically called | |
| 1501 | * when a vnode is initialized from its inode. | |
| 7540ab49 MD |
1502 | */ |
| 1503 | int | |
| b0d18f7d | 1504 | vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff) |
| 7540ab49 | 1505 | { |
| 7540ab49 MD |
1506 | vm_object_t object; |
| 1507 | int error = 0; | |
| 1508 | ||
| 1509 | retry: | |
| b12defdc MD |
1510 | while ((object = vp->v_object) != NULL) { |
| 1511 | vm_object_hold(object); | |
| 1512 | if (object == vp->v_object) | |
| 1513 | break; | |
| 1514 | vm_object_drop(object); | |
| 1515 | } | |
| 1516 | ||
| 1517 | if (object == NULL) { | |
| b0d18f7d | 1518 | object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff); |
| b12defdc | 1519 | |
| 7540ab49 MD |
1520 | /* |
| 1521 | * Dereference the reference we just created. This assumes | |
| 1522 | * that the object is associated with the vp. | |
| 1523 | */ | |
| b12defdc | 1524 | vm_object_hold(object); |
| 7540ab49 | 1525 | object->ref_count--; |
| 3c37c940 | 1526 | vrele(vp); |
| 7540ab49 MD |
1527 | } else { |
| 1528 | if (object->flags & OBJ_DEAD) { | |
| a11aaa81 | 1529 | vn_unlock(vp); |
| 2de4f77e MD |
1530 | if (vp->v_object == object) |
| 1531 | vm_object_dead_sleep(object, "vodead"); | |
| ca466bae | 1532 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| b12defdc | 1533 | vm_object_drop(object); |
| 7540ab49 MD |
1534 | goto retry; |
| 1535 | } | |
| 1536 | } | |
| 1537 | KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object")); | |
| 2247fe02 | 1538 | vsetflags(vp, VOBJBUF); |
| b12defdc | 1539 | vm_object_drop(object); |
| 2de4f77e | 1540 | |
| 7540ab49 MD |
1541 | return (error); |
| 1542 | } | |
| 1543 | ||
| 1544 | ||
| 1545 | /* | |
| 984263bc MD |
1546 | * Print out a description of a vnode. |
| 1547 | */ | |
| 1548 | static char *typename[] = | |
| 1549 | {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; | |
| 1550 | ||
| 1551 | void | |
| dd98570a | 1552 | vprint(char *label, struct vnode *vp) |
| 984263bc MD |
1553 | { |
| 1554 | char buf[96]; | |
| 1555 | ||
| 1556 | if (label != NULL) | |
| 6ea70f76 | 1557 | kprintf("%s: %p: ", label, (void *)vp); |
| 984263bc | 1558 | else |
| 6ea70f76 | 1559 | kprintf("%p: ", (void *)vp); |
| 3c37c940 MD |
1560 | kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,", |
| 1561 | typename[vp->v_type], | |
| 1562 | vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs); | |
| 984263bc MD |
1563 | buf[0] = '\0'; |
| 1564 | if (vp->v_flag & VROOT) | |
| 1565 | strcat(buf, "|VROOT"); | |
| 67863d04 MD |
1566 | if (vp->v_flag & VPFSROOT) |
| 1567 | strcat(buf, "|VPFSROOT"); | |
| 984263bc MD |
1568 | if (vp->v_flag & VTEXT) |
| 1569 | strcat(buf, "|VTEXT"); | |
| 1570 | if (vp->v_flag & VSYSTEM) | |
| 1571 | strcat(buf, "|VSYSTEM"); | |
| 984263bc MD |
1572 | if (vp->v_flag & VFREE) |
| 1573 | strcat(buf, "|VFREE"); | |
| 1574 | if (vp->v_flag & VOBJBUF) | |
| 1575 | strcat(buf, "|VOBJBUF"); | |
| 1576 | if (buf[0] != '\0') | |
| 6ea70f76 | 1577 | kprintf(" flags (%s)", &buf[1]); |
| 984263bc | 1578 | if (vp->v_data == NULL) { |
| 6ea70f76 | 1579 | kprintf("\n"); |
| 984263bc | 1580 | } else { |
| 6ea70f76 | 1581 | kprintf("\n\t"); |
| 984263bc MD |
1582 | VOP_PRINT(vp); |
| 1583 | } | |
| 1584 | } | |
| 1585 | ||
| 3b0783db SK |
1586 | /* |
| 1587 | * Do the usual access checking. | |
| 1588 | * file_mode, uid and gid are from the vnode in question, | |
| 1589 | * while acc_mode and cred are from the VOP_ACCESS parameter list | |
| 1590 | */ | |
| 1591 | int | |
| 1592 | vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, | |
| 1593 | mode_t acc_mode, struct ucred *cred) | |
| 1594 | { | |
| 1595 | mode_t mask; | |
| aa8969cf | 1596 | int ismember; |
| 3b0783db SK |
1597 | |
| 1598 | /* | |
| 1599 | * Super-user always gets read/write access, but execute access depends | |
| 1600 | * on at least one execute bit being set. | |
| 1601 | */ | |
| 1602 | if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) { | |
| 1603 | if ((acc_mode & VEXEC) && type != VDIR && | |
| 1604 | (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) | |
| 1605 | return (EACCES); | |
| 1606 | return (0); | |
| 1607 | } | |
| 1608 | ||
| 1609 | mask = 0; | |
| 1610 | ||
| 1611 | /* Otherwise, check the owner. */ | |
| 1612 | if (cred->cr_uid == uid) { | |
| 1613 | if (acc_mode & VEXEC) | |
| 1614 | mask |= S_IXUSR; | |
| 1615 | if (acc_mode & VREAD) | |
| 1616 | mask |= S_IRUSR; | |
| 1617 | if (acc_mode & VWRITE) | |
| 1618 | mask |= S_IWUSR; | |
| 1619 | return ((file_mode & mask) == mask ? 0 : EACCES); | |
| 1620 | } | |
| 1621 | ||
| 1622 | /* Otherwise, check the groups. */ | |
| 1623 | ismember = groupmember(gid, cred); | |
| 1624 | if (cred->cr_svgid == gid || ismember) { | |
| 1625 | if (acc_mode & VEXEC) | |
| 1626 | mask |= S_IXGRP; | |
| 1627 | if (acc_mode & VREAD) | |
| 1628 | mask |= S_IRGRP; | |
| 1629 | if (acc_mode & VWRITE) | |
| 1630 | mask |= S_IWGRP; | |
| 1631 | return ((file_mode & mask) == mask ? 0 : EACCES); | |
| 1632 | } | |
| 1633 | ||
| 1634 | /* Otherwise, check everyone else. */ | |
| 1635 | if (acc_mode & VEXEC) | |
| 1636 | mask |= S_IXOTH; | |
| 1637 | if (acc_mode & VREAD) | |
| 1638 | mask |= S_IROTH; | |
| 1639 | if (acc_mode & VWRITE) | |
| 1640 | mask |= S_IWOTH; | |
| 1641 | return ((file_mode & mask) == mask ? 0 : EACCES); | |
| 1642 | } | |
| 1643 | ||
| 984263bc MD |
1644 | #ifdef DDB |
| 1645 | #include <ddb/ddb.h> | |
| 861905fb MD |
1646 | |
| 1647 | static int db_show_locked_vnodes(struct mount *mp, void *data); | |
| 1648 | ||
| 984263bc MD |
1649 | /* |
| 1650 | * List all of the locked vnodes in the system. | |
| 1651 | * Called when debugging the kernel. | |
| 1652 | */ | |
| 1653 | DB_SHOW_COMMAND(lockedvnodes, lockedvnodes) | |
| 1654 | { | |
| 6ea70f76 | 1655 | kprintf("Locked vnodes\n"); |
| 861905fb MD |
1656 | mountlist_scan(db_show_locked_vnodes, NULL, |
| 1657 | MNTSCAN_FORWARD|MNTSCAN_NOBUSY); | |
| 1658 | } | |
| 1659 | ||
| 1660 | static int | |
| 1661 | db_show_locked_vnodes(struct mount *mp, void *data __unused) | |
| 1662 | { | |
| 984263bc MD |
1663 | struct vnode *vp; |
| 1664 | ||
| 861905fb | 1665 | TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { |
| a11aaa81 | 1666 | if (vn_islocked(vp)) |
| 60233e58 | 1667 | vprint(NULL, vp); |
| 984263bc | 1668 | } |
| 861905fb | 1669 | return(0); |
| 984263bc MD |
1670 | } |
| 1671 | #endif | |
| 1672 | ||
| 1673 | /* | |
| 1674 | * Top level filesystem related information gathering. | |
| 1675 | */ | |
| 402ed7e1 | 1676 | static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS); |
| 984263bc MD |
1677 | |
| 1678 | static int | |
| 1679 | vfs_sysctl(SYSCTL_HANDLER_ARGS) | |
| 1680 | { | |
| 1681 | int *name = (int *)arg1 - 1; /* XXX */ | |
| 1682 | u_int namelen = arg2 + 1; /* XXX */ | |
| 1683 | struct vfsconf *vfsp; | |
| 2613053d | 1684 | int maxtypenum; |
| 984263bc MD |
1685 | |
| 1686 | #if 1 || defined(COMPAT_PRELITE2) | |
| 1687 | /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ | |
| 1688 | if (namelen == 1) | |
| 1689 | return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); | |
| 1690 | #endif | |
| 1691 | ||
| 1692 | #ifdef notyet | |
| 1693 | /* all sysctl names at this level are at least name and field */ | |
| 1694 | if (namelen < 2) | |
| 1695 | return (ENOTDIR); /* overloaded */ | |
| 1696 | if (name[0] != VFS_GENERIC) { | |
| 2613053d | 1697 | vfsp = vfsconf_find_by_typenum(name[0]); |
| 984263bc MD |
1698 | if (vfsp == NULL) |
| 1699 | return (EOPNOTSUPP); | |
| 1700 | return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, | |
| 1701 | oldp, oldlenp, newp, newlen, p)); | |
| 1702 | } | |
| 1703 | #endif | |
| 1704 | switch (name[1]) { | |
| 1705 | case VFS_MAXTYPENUM: | |
| 1706 | if (namelen != 2) | |
| 1707 | return (ENOTDIR); | |
| 2613053d MN |
1708 | maxtypenum = vfsconf_get_maxtypenum(); |
| 1709 | return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum))); | |
| 984263bc MD |
1710 | case VFS_CONF: |
| 1711 | if (namelen != 3) | |
| 1712 | return (ENOTDIR); /* overloaded */ | |
| 2613053d | 1713 | vfsp = vfsconf_find_by_typenum(name[2]); |
| 984263bc MD |
1714 | if (vfsp == NULL) |
| 1715 | return (EOPNOTSUPP); | |
| 1716 | return (SYSCTL_OUT(req, vfsp, sizeof *vfsp)); | |
| 1717 | } | |
| 1718 | return (EOPNOTSUPP); | |
| 1719 | } | |
| 1720 | ||
| 1721 | SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl, | |
| 1722 | "Generic filesystem"); | |
| 1723 | ||
| 1724 | #if 1 || defined(COMPAT_PRELITE2) | |
| 1725 | ||
| 1726 | static int | |
| 2613053d | 1727 | sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data) |
| 984263bc MD |
1728 | { |
| 1729 | int error; | |
| 984263bc | 1730 | struct ovfsconf ovfs; |
| 2613053d MN |
1731 | struct sysctl_req *req = (struct sysctl_req*) data; |
| 1732 | ||
| 1733 | bzero(&ovfs, sizeof(ovfs)); | |
| 1734 | ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ | |
| 1735 | strcpy(ovfs.vfc_name, vfsp->vfc_name); | |
| 1736 | ovfs.vfc_index = vfsp->vfc_typenum; | |
| 1737 | ovfs.vfc_refcount = vfsp->vfc_refcount; | |
| 1738 | ovfs.vfc_flags = vfsp->vfc_flags; | |
| 1739 | error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); | |
| 1740 | if (error) | |
| 1741 | return error; /* abort iteration with error code */ | |
| 1742 | else | |
| 1743 | return 0; /* continue iterating with next element */ | |
| 1744 | } | |
| 984263bc | 1745 | |
| 2613053d MN |
1746 | static int |
| 1747 | sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) | |
| 1748 | { | |
| 1749 | return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req); | |
| 984263bc MD |
1750 | } |
| 1751 | ||
| 1752 | #endif /* 1 || COMPAT_PRELITE2 */ | |
| 1753 | ||
| 984263bc MD |
1754 | /* |
| 1755 | * Check to see if a filesystem is mounted on a block device. | |
| 1756 | */ | |
| 1757 | int | |
| e4c9c0c8 | 1758 | vfs_mountedon(struct vnode *vp) |
| 984263bc | 1759 | { |
| b13267a5 | 1760 | cdev_t dev; |
| 984263bc | 1761 | |
| 0e9b9130 | 1762 | if ((dev = vp->v_rdev) == NULL) { |
| cd29885a MD |
1763 | /* if (vp->v_type != VBLK) |
| 1764 | dev = get_dev(vp->v_uminor, vp->v_umajor); */ | |
| 0e9b9130 | 1765 | } |
| 028066b1 | 1766 | if (dev != NULL && dev->si_mountpoint) |
| 984263bc MD |
1767 | return (EBUSY); |
| 1768 | return (0); | |
| 1769 | } | |
| 1770 | ||
| 1771 | /* | |
| 1772 | * Unmount all filesystems. The list is traversed in reverse order | |
| 1773 | * of mounting to avoid dependencies. | |
| 1774 | */ | |
| 861905fb MD |
1775 | |
| 1776 | static int vfs_umountall_callback(struct mount *mp, void *data); | |
| 1777 | ||
| 984263bc | 1778 | void |
| dd98570a | 1779 | vfs_unmountall(void) |
| 984263bc | 1780 | { |
| 861905fb | 1781 | int count; |
| 984263bc | 1782 | |
| 861905fb MD |
1783 | do { |
| 1784 | count = mountlist_scan(vfs_umountall_callback, | |
| acde96db | 1785 | NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY); |
| 861905fb MD |
1786 | } while (count); |
| 1787 | } | |
| 1788 | ||
| 1789 | static | |
| 1790 | int | |
| 1791 | vfs_umountall_callback(struct mount *mp, void *data) | |
| 1792 | { | |
| 861905fb MD |
1793 | int error; |
| 1794 | ||
| acde96db | 1795 | error = dounmount(mp, MNT_FORCE); |
| 861905fb MD |
1796 | if (error) { |
| 1797 | mountlist_remove(mp); | |
| 6ea70f76 | 1798 | kprintf("unmount of filesystem mounted from %s failed (", |
| 861905fb MD |
1799 | mp->mnt_stat.f_mntfromname); |
| 1800 | if (error == EBUSY) | |
| 6ea70f76 | 1801 | kprintf("BUSY)\n"); |
| 861905fb | 1802 | else |
| 6ea70f76 | 1803 | kprintf("%d)\n", error); |
| 984263bc | 1804 | } |
| 861905fb | 1805 | return(1); |
| 984263bc MD |
1806 | } |
| 1807 | ||
| 1808 | /* | |
| 177403a9 MD |
1809 | * Checks the mount flags for parameter mp and put the names comma-separated |
| 1810 | * into a string buffer buf with a size limit specified by len. | |
| 1811 | * | |
| 1812 | * It returns the number of bytes written into buf, and (*errorp) will be | |
| 1813 | * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was | |
| 1814 | * not large enough). The buffer will be 0-terminated if len was not 0. | |
| 1815 | */ | |
| 177403a9 | 1816 | size_t |
| dad088a5 MD |
1817 | vfs_flagstostr(int flags, const struct mountctl_opt *optp, |
| 1818 | char *buf, size_t len, int *errorp) | |
| 177403a9 MD |
1819 | { |
| 1820 | static const struct mountctl_opt optnames[] = { | |
| 1821 | { MNT_ASYNC, "asynchronous" }, | |
| 1822 | { MNT_EXPORTED, "NFS exported" }, | |
| 1823 | { MNT_LOCAL, "local" }, | |
| 1824 | { MNT_NOATIME, "noatime" }, | |
| 1825 | { MNT_NODEV, "nodev" }, | |
| 1826 | { MNT_NOEXEC, "noexec" }, | |
| 1827 | { MNT_NOSUID, "nosuid" }, | |
| 1828 | { MNT_NOSYMFOLLOW, "nosymfollow" }, | |
| 1829 | { MNT_QUOTA, "with-quotas" }, | |
| 1830 | { MNT_RDONLY, "read-only" }, | |
| 1831 | { MNT_SYNCHRONOUS, "synchronous" }, | |
| 1832 | { MNT_UNION, "union" }, | |
| 1833 | { MNT_NOCLUSTERR, "noclusterr" }, | |
| 1834 | { MNT_NOCLUSTERW, "noclusterw" }, | |
| 1835 | { MNT_SUIDDIR, "suiddir" }, | |
| 1836 | { MNT_SOFTDEP, "soft-updates" }, | |
| dad088a5 MD |
1837 | { MNT_IGNORE, "ignore" }, |
| 1838 | { 0, NULL} | |
| 177403a9 | 1839 | }; |
| 177403a9 MD |
1840 | int bwritten; |
| 1841 | int bleft; | |
| 1842 | int optlen; | |
| eac446c5 | 1843 | int actsize; |
| dad088a5 | 1844 | |
| 177403a9 | 1845 | *errorp = 0; |
| 177403a9 MD |
1846 | bwritten = 0; |
| 1847 | bleft = len - 1; /* leave room for trailing \0 */ | |
| eac446c5 MD |
1848 | |
| 1849 | /* | |
| 1850 | * Checks the size of the string. If it contains | |
| 1851 | * any data, then we will append the new flags to | |
| 1852 | * it. | |
| 1853 | */ | |
| 1854 | actsize = strlen(buf); | |
| 1855 | if (actsize > 0) | |
| 1856 | buf += actsize; | |
| 1857 | ||
| 1858 | /* Default flags if no flags passed */ | |
| 1859 | if (optp == NULL) | |
| 1860 | optp = optnames; | |
| 1861 | ||
| 177403a9 MD |
1862 | if (bleft < 0) { /* degenerate case, 0-length buffer */ |
| 1863 | *errorp = EINVAL; | |
| 1864 | return(0); | |
| 1865 | } | |
| 1866 | ||
| dad088a5 MD |
1867 | for (; flags && optp->o_opt; ++optp) { |
| 1868 | if ((flags & optp->o_opt) == 0) | |
| 177403a9 | 1869 | continue; |
| dad088a5 | 1870 | optlen = strlen(optp->o_name); |
| eac446c5 | 1871 | if (bwritten || actsize > 0) { |
| dad088a5 | 1872 | if (bleft < 2) { |
| 177403a9 MD |
1873 | *errorp = ENOSPC; |
| 1874 | break; | |
| 1875 | } | |
| 1876 | buf[bwritten++] = ','; | |
| dad088a5 MD |
1877 | buf[bwritten++] = ' '; |
| 1878 | bleft -= 2; | |
| 177403a9 MD |
1879 | } |
| 1880 | if (bleft < optlen) { | |
| 1881 | *errorp = ENOSPC; | |
| 1882 | break; | |
| 1883 | } | |
| dad088a5 | 1884 | bcopy(optp->o_name, buf + bwritten, optlen); |
| 177403a9 MD |
1885 | bwritten += optlen; |
| 1886 | bleft -= optlen; | |
| dad088a5 | 1887 | flags &= ~optp->o_opt; |
| 177403a9 MD |
1888 | } |
| 1889 | ||
| 1890 | /* | |
| 1891 | * Space already reserved for trailing \0 | |
| 1892 | */ | |
| 1893 | buf[bwritten] = 0; | |
| 1894 | return (bwritten); | |
| 1895 | } | |
| 1896 | ||
| 177403a9 | 1897 | /* |
| 984263bc MD |
1898 | * Build hash lists of net addresses and hang them off the mount point. |
| 1899 | * Called by ufs_mount() to set up the lists of export addresses. | |
| 1900 | */ | |
| 1901 | static int | |
| dd98570a | 1902 | vfs_hang_addrlist(struct mount *mp, struct netexport *nep, |
| 1aa89f17 | 1903 | const struct export_args *argp) |
| 984263bc | 1904 | { |
| 1fd87d54 RG |
1905 | struct netcred *np; |
| 1906 | struct radix_node_head *rnh; | |
| 1907 | int i; | |
| 984263bc | 1908 | struct radix_node *rn; |
| 4090d6ff | 1909 | struct sockaddr *saddr, *smask = NULL; |
| 984263bc MD |
1910 | struct domain *dom; |
| 1911 | int error; | |
| 1912 | ||
| 1913 | if (argp->ex_addrlen == 0) { | |
| 1914 | if (mp->mnt_flag & MNT_DEFEXPORTED) | |
| 1915 | return (EPERM); | |
| 1916 | np = &nep->ne_defexported; | |
| 1917 | np->netc_exflags = argp->ex_flags; | |
| 1918 | np->netc_anon = argp->ex_anon; | |
| 1919 | np->netc_anon.cr_ref = 1; | |
| 1920 | mp->mnt_flag |= MNT_DEFEXPORTED; | |
| 1921 | return (0); | |
| 1922 | } | |
| 1923 | ||
| 0260ddf9 MD |
1924 | if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN) |
| 1925 | return (EINVAL); | |
| 1926 | if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN) | |
| 984263bc MD |
1927 | return (EINVAL); |
| 1928 | ||
| 1929 | i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; | |
| e7b4468c | 1930 | np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO); |
| 984263bc MD |
1931 | saddr = (struct sockaddr *) (np + 1); |
| 1932 | if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen))) | |
| 1933 | goto out; | |
| 1934 | if (saddr->sa_len > argp->ex_addrlen) | |
| 1935 | saddr->sa_len = argp->ex_addrlen; | |
| 1936 | if (argp->ex_masklen) { | |
| dd98570a MD |
1937 | smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); |
| 1938 | error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen); | |
| 984263bc MD |
1939 | if (error) |
| 1940 | goto out; | |
| 1941 | if (smask->sa_len > argp->ex_masklen) | |
| 1942 | smask->sa_len = argp->ex_masklen; | |
| 1943 | } | |
| 1944 | i = saddr->sa_family; | |
| 4090d6ff | 1945 | if ((rnh = nep->ne_rtable[i]) == NULL) { |
| 984263bc MD |
1946 | /* |
| 1947 | * Seems silly to initialize every AF when most are not used, | |
| 1948 | * do so on demand here | |
| 1949 | */ | |
| 9c70fe43 | 1950 | SLIST_FOREACH(dom, &domains, dom_next) |
| 984263bc MD |
1951 | if (dom->dom_family == i && dom->dom_rtattach) { |
| 1952 | dom->dom_rtattach((void **) &nep->ne_rtable[i], | |
| 1953 | dom->dom_rtoffset); | |
| 1954 | break; | |
| 1955 | } | |
| 4090d6ff | 1956 | if ((rnh = nep->ne_rtable[i]) == NULL) { |
| 984263bc MD |
1957 | error = ENOBUFS; |
| 1958 | goto out; | |
| 1959 | } | |
| 1960 | } | |
| 2e9572df | 1961 | rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh, |
| 984263bc | 1962 | np->netc_rnodes); |
| 4090d6ff | 1963 | if (rn == NULL || np != (struct netcred *) rn) { /* already exists */ |
| 984263bc MD |
1964 | error = EPERM; |
| 1965 | goto out; | |
| 1966 | } | |
| 1967 | np->netc_exflags = argp->ex_flags; | |
| 1968 | np->netc_anon = argp->ex_anon; | |
| 1969 | np->netc_anon.cr_ref = 1; | |
| 1970 | return (0); | |
| 1971 | out: | |
| efda3bd0 | 1972 | kfree(np, M_NETADDR); |
| 984263bc MD |
1973 | return (error); |
| 1974 | } | |
| 1975 | ||
| 1976 | /* ARGSUSED */ | |
| 1977 | static int | |
| dd98570a | 1978 | vfs_free_netcred(struct radix_node *rn, void *w) |
| 984263bc | 1979 | { |
| 1fd87d54 | 1980 | struct radix_node_head *rnh = (struct radix_node_head *) w; |
| 984263bc MD |
1981 | |
| 1982 | (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh); | |
| efda3bd0 | 1983 | kfree((caddr_t) rn, M_NETADDR); |
| 984263bc MD |
1984 | return (0); |
| 1985 | } | |
| 1986 | ||
| 1987 | /* | |
| 1988 | * Free the net address hash lists that are hanging off the mount points. | |
| 1989 | */ | |
| 1990 | static void | |
| dd98570a | 1991 | vfs_free_addrlist(struct netexport *nep) |
| 984263bc | 1992 | { |
| 1fd87d54 RG |
1993 | int i; |
| 1994 | struct radix_node_head *rnh; | |
| 984263bc MD |
1995 | |
| 1996 | for (i = 0; i <= AF_MAX; i++) | |
| 1997 | if ((rnh = nep->ne_rtable[i])) { | |
| 1998 | (*rnh->rnh_walktree) (rnh, vfs_free_netcred, | |
| 1999 | (caddr_t) rnh); | |
| efda3bd0 | 2000 | kfree((caddr_t) rnh, M_RTABLE); |
| 984263bc MD |
2001 | nep->ne_rtable[i] = 0; |
| 2002 | } | |
| 2003 | } | |
| 2004 | ||
| 2005 | int | |
| 1aa89f17 MD |
2006 | vfs_export(struct mount *mp, struct netexport *nep, |
| 2007 | const struct export_args *argp) | |
| 984263bc MD |
2008 | { |
| 2009 | int error; | |
| 2010 | ||
| 2011 | if (argp->ex_flags & MNT_DELEXPORT) { | |
| 2012 | if (mp->mnt_flag & MNT_EXPUBLIC) { | |
| 2013 | vfs_setpublicfs(NULL, NULL, NULL); | |
| 2014 | mp->mnt_flag &= ~MNT_EXPUBLIC; | |
| 2015 | } | |
| 2016 | vfs_free_addrlist(nep); | |
| 2017 | mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); | |
| 2018 | } | |
| 2019 | if (argp->ex_flags & MNT_EXPORTED) { | |
| 2020 | if (argp->ex_flags & MNT_EXPUBLIC) { | |
| 2021 | if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) | |
| 2022 | return (error); | |
| 2023 | mp->mnt_flag |= MNT_EXPUBLIC; | |
| 2024 | } | |
| 2025 | if ((error = vfs_hang_addrlist(mp, nep, argp))) | |
| 2026 | return (error); | |
| 2027 | mp->mnt_flag |= MNT_EXPORTED; | |
| 2028 | } | |
| 2029 | return (0); | |
| 2030 | } | |
| 2031 | ||
| 2032 | ||
| 2033 | /* | |
| 2034 | * Set the publicly exported filesystem (WebNFS). Currently, only | |
| 2035 | * one public filesystem is possible in the spec (RFC 2054 and 2055) | |
| 2036 | */ | |
| 2037 | int | |
| dd98570a | 2038 | vfs_setpublicfs(struct mount *mp, struct netexport *nep, |
| 1aa89f17 | 2039 | const struct export_args *argp) |
| 984263bc MD |
2040 | { |
| 2041 | int error; | |
| 2042 | struct vnode *rvp; | |
| 2043 | char *cp; | |
| 2044 | ||
| 2045 | /* | |
| 2046 | * mp == NULL -> invalidate the current info, the FS is | |
| 2047 | * no longer exported. May be called from either vfs_export | |
| 2048 | * or unmount, so check if it hasn't already been done. | |
| 2049 | */ | |
| 2050 | if (mp == NULL) { | |
| 2051 | if (nfs_pub.np_valid) { | |
| 2052 | nfs_pub.np_valid = 0; | |
| 2053 | if (nfs_pub.np_index != NULL) { | |
| 884717e1 | 2054 | kfree(nfs_pub.np_index, M_TEMP); |
| 984263bc MD |
2055 | nfs_pub.np_index = NULL; |
| 2056 | } | |
| 2057 | } | |
| 2058 | return (0); | |
| 2059 | } | |
| 2060 | ||
| 2061 | /* | |
| 2062 | * Only one allowed at a time. | |
| 2063 | */ | |
| 2064 | if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) | |
| 2065 | return (EBUSY); | |
| 2066 | ||
| 2067 | /* | |
| 2068 | * Get real filehandle for root of exported FS. | |
| 2069 | */ | |
| 2070 | bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); | |
| 2071 | nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; | |
| 2072 | ||
| 2073 | if ((error = VFS_ROOT(mp, &rvp))) | |
| 2074 | return (error); | |
| 2075 | ||
| 2076 | if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) | |
| 2077 | return (error); | |
| 2078 | ||
| 2079 | vput(rvp); | |
| 2080 | ||
| 2081 | /* | |
| 2082 | * If an indexfile was specified, pull it in. | |
| 2083 | */ | |
| 2084 | if (argp->ex_indexfile != NULL) { | |
| b80c9733 JS |
2085 | int namelen; |
| 2086 | ||
| 2087 | error = vn_get_namelen(rvp, &namelen); | |
| 2088 | if (error) | |
| 2089 | return (error); | |
| 884717e1 | 2090 | nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK); |
| 984263bc | 2091 | error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, |
| 60233e58 | 2092 | namelen, NULL); |
| 984263bc MD |
2093 | if (!error) { |
| 2094 | /* | |
| 2095 | * Check for illegal filenames. | |
| 2096 | */ | |
| 2097 | for (cp = nfs_pub.np_index; *cp; cp++) { | |
| 2098 | if (*cp == '/') { | |
| 2099 | error = EINVAL; | |
| 2100 | break; | |
| 2101 | } | |
| 2102 | } | |
| 2103 | } | |
| 2104 | if (error) { | |
| 884717e1 | 2105 | kfree(nfs_pub.np_index, M_TEMP); |
| 984263bc MD |
2106 | return (error); |
| 2107 | } | |
| 2108 | } | |
| 2109 | ||
| 2110 | nfs_pub.np_mount = mp; | |
| 2111 | nfs_pub.np_valid = 1; | |
| 2112 | return (0); | |
| 2113 | } | |
| 2114 | ||
| 2115 | struct netcred * | |
| dd98570a MD |
2116 | vfs_export_lookup(struct mount *mp, struct netexport *nep, |
| 2117 | struct sockaddr *nam) | |
| 984263bc | 2118 | { |
| 1fd87d54 RG |
2119 | struct netcred *np; |
| 2120 | struct radix_node_head *rnh; | |
| 984263bc MD |
2121 | struct sockaddr *saddr; |
| 2122 | ||
| 2123 | np = NULL; | |
| 2124 | if (mp->mnt_flag & MNT_EXPORTED) { | |
| 2125 | /* | |
| 2126 | * Lookup in the export list first. | |
| 2127 | */ | |
| 2128 | if (nam != NULL) { | |
| 2129 | saddr = nam; | |
| 2130 | rnh = nep->ne_rtable[saddr->sa_family]; | |
| 2131 | if (rnh != NULL) { | |
| 2132 | np = (struct netcred *) | |
| 2e9572df | 2133 | (*rnh->rnh_matchaddr)((char *)saddr, |
| 984263bc MD |
2134 | rnh); |
| 2135 | if (np && np->netc_rnodes->rn_flags & RNF_ROOT) | |
| 2136 | np = NULL; | |
| 2137 | } | |
| 2138 | } | |
| 2139 | /* | |
| 2140 | * If no address match, use the default if it exists. | |
| 2141 | */ | |
| 2142 | if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) | |
| 2143 | np = &nep->ne_defexported; | |
| 2144 | } | |
| 2145 | return (np); | |
| 2146 | } | |
| 2147 | ||
| 2148 | /* | |
| 41a01a4d MD |
2149 | * perform msync on all vnodes under a mount point. The mount point must |
| 2150 | * be locked. This code is also responsible for lazy-freeing unreferenced | |
| 2151 | * vnodes whos VM objects no longer contain pages. | |
| 2152 | * | |
| 2153 | * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state. | |
| 03a964e9 MD |
2154 | * |
| 2155 | * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked, | |
| 2156 | * but vnode_pager_putpages() doesn't lock the vnode. We have to do it | |
| 2157 | * way up in this high level function. | |
| 984263bc | 2158 | */ |
| 41a01a4d | 2159 | static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data); |
| 5fd012e0 | 2160 | static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data); |
| 41a01a4d | 2161 | |
| 984263bc MD |
2162 | void |
| 2163 | vfs_msync(struct mount *mp, int flags) | |
| 2164 | { | |
| 03a964e9 MD |
2165 | int vmsc_flags; |
| 2166 | ||
| 2bc7505b MD |
2167 | /* |
| 2168 | * tmpfs sets this flag to prevent msync(), sync, and the | |
| 2169 | * filesystem periodic syncer from trying to flush VM pages | |
| 2170 | * to swap. Only pure memory pressure flushes tmpfs VM pages | |
| 2171 | * to swap. | |
| 2172 | */ | |
| 2173 | if (mp->mnt_kern_flag & MNTK_NOMSYNC) | |
| 2174 | return; | |
| 2175 | ||
| 2176 | /* | |
| 2177 | * Ok, scan the vnodes for work. | |
| 2178 | */ | |
| 03a964e9 MD |
2179 | vmsc_flags = VMSC_GETVP; |
| 2180 | if (flags != MNT_WAIT) | |
| 2181 | vmsc_flags |= VMSC_NOWAIT; | |
| 72a0763a MD |
2182 | vmntvnodescan(mp, vmsc_flags, |
| 2183 | vfs_msync_scan1, vfs_msync_scan2, | |
| 2184 | (void *)(intptr_t)flags); | |
| 41a01a4d | 2185 | } |
| 984263bc | 2186 | |
| 41a01a4d MD |
2187 | /* |
| 2188 | * scan1 is a fast pre-check. There could be hundreds of thousands of | |
| 2189 | * vnodes, we cannot afford to do anything heavy weight until we have a | |
| 2190 | * fairly good indication that there is work to do. | |
| 2191 | */ | |
| 2192 | static | |
| 2193 | int | |
| 2194 | vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data) | |
| 2195 | { | |
| 973c11b9 | 2196 | int flags = (int)(intptr_t)data; |
| 984263bc | 2197 | |
| 5fd012e0 | 2198 | if ((vp->v_flag & VRECLAIMED) == 0) { |
| 3c37c940 | 2199 | if (vshouldmsync(vp)) |
| 5fd012e0 | 2200 | return(0); /* call scan2 */ |
| 41a01a4d MD |
2201 | if ((mp->mnt_flag & MNT_RDONLY) == 0 && |
| 2202 | (vp->v_flag & VOBJDIRTY) && | |
| a11aaa81 | 2203 | (flags == MNT_WAIT || vn_islocked(vp) == 0)) { |
| 5fd012e0 | 2204 | return(0); /* call scan2 */ |
| 41a01a4d MD |
2205 | } |
| 2206 | } | |
| 5fd012e0 MD |
2207 | |
| 2208 | /* | |
| 2209 | * do not call scan2, continue the loop | |
| 2210 | */ | |
| 41a01a4d MD |
2211 | return(-1); |
| 2212 | } | |
| 2213 | ||
| 03a964e9 MD |
2214 | /* |
| 2215 | * This callback is handed a locked vnode. | |
| 2216 | */ | |
| 41a01a4d MD |
2217 | static |
| 2218 | int | |
| 5fd012e0 | 2219 | vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data) |
| 41a01a4d MD |
2220 | { |
| 2221 | vm_object_t obj; | |
| 973c11b9 | 2222 | int flags = (int)(intptr_t)data; |
| 41a01a4d | 2223 | |
| 5fd012e0 | 2224 | if (vp->v_flag & VRECLAIMED) |
| 41a01a4d MD |
2225 | return(0); |
| 2226 | ||
| 7540ab49 MD |
2227 | if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) { |
| 2228 | if ((obj = vp->v_object) != NULL) { | |
| 5fd012e0 MD |
2229 | vm_object_page_clean(obj, 0, 0, |
| 2230 | flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC); | |
| 984263bc MD |
2231 | } |
| 2232 | } | |
| 41a01a4d | 2233 | return(0); |
| 984263bc MD |
2234 | } |
| 2235 | ||
| 2236 | /* | |
| 22a90887 | 2237 | * Wake up anyone interested in vp because it is being revoked. |
| 984263bc MD |
2238 | */ |
| 2239 | void | |
| 22a90887 | 2240 | vn_gone(struct vnode *vp) |
| 984263bc | 2241 | { |
| 3b998fa9 | 2242 | lwkt_gettoken(&vp->v_token); |
| 5b22f1a7 | 2243 | KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE); |
| 3b998fa9 | 2244 | lwkt_reltoken(&vp->v_token); |
| 984263bc MD |
2245 | } |
| 2246 | ||
| 984263bc | 2247 | /* |
| b13267a5 | 2248 | * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened |
| e4c9c0c8 | 2249 | * (or v_rdev might be NULL). |
| 984263bc | 2250 | */ |
| b13267a5 | 2251 | cdev_t |
| e4c9c0c8 | 2252 | vn_todev(struct vnode *vp) |
| 984263bc MD |
2253 | { |
| 2254 | if (vp->v_type != VBLK && vp->v_type != VCHR) | |
| 028066b1 | 2255 | return (NULL); |
| e4c9c0c8 | 2256 | KKASSERT(vp->v_rdev != NULL); |
| 984263bc MD |
2257 | return (vp->v_rdev); |
| 2258 | } | |
| 2259 | ||
| 2260 | /* | |
| e4c9c0c8 MD |
2261 | * Check if vnode represents a disk device. The vnode does not need to be |
| 2262 | * opened. | |
| 2ad080fe MD |
2263 | * |
| 2264 | * MPALMOSTSAFE | |
| 984263bc MD |
2265 | */ |
| 2266 | int | |
| e4c9c0c8 | 2267 | vn_isdisk(struct vnode *vp, int *errp) |
| 984263bc | 2268 | { |
| b13267a5 | 2269 | cdev_t dev; |
| e4c9c0c8 | 2270 | |
| 0e9b9130 | 2271 | if (vp->v_type != VCHR) { |
| 984263bc MD |
2272 | if (errp != NULL) |
| 2273 | *errp = ENOTBLK; | |
| 2274 | return (0); | |
| 2275 | } | |
| e4c9c0c8 | 2276 | |
| cd29885a | 2277 | dev = vp->v_rdev; |
| 0e9b9130 | 2278 | |
| 028066b1 | 2279 | if (dev == NULL) { |
| 984263bc MD |
2280 | if (errp != NULL) |
| 2281 | *errp = ENXIO; | |
| 2282 | return (0); | |
| 2283 | } | |
| e4c9c0c8 | 2284 | if (dev_is_good(dev) == 0) { |
| 984263bc MD |
2285 | if (errp != NULL) |
| 2286 | *errp = ENXIO; | |
| 2287 | return (0); | |
| 2288 | } | |
| e4c9c0c8 | 2289 | if ((dev_dflags(dev) & D_DISK) == 0) { |
| 984263bc MD |
2290 | if (errp != NULL) |
| 2291 | *errp = ENOTBLK; | |
| 2292 | return (0); | |
| 2293 | } | |
| 2294 | if (errp != NULL) | |
| 2295 | *errp = 0; | |
| 2296 | return (1); | |
| 2297 | } | |
| 2298 | ||
| 5d72d6ed JS |
2299 | int |
| 2300 | vn_get_namelen(struct vnode *vp, int *namelen) | |
| 2301 | { | |
| 973c11b9 MD |
2302 | int error; |
| 2303 | register_t retval[2]; | |
| 5d72d6ed JS |
2304 | |
| 2305 | error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval); | |
| 2306 | if (error) | |
| 2307 | return (error); | |
| 973c11b9 | 2308 | *namelen = (int)retval[0]; |
| 5d72d6ed JS |
2309 | return (0); |
| 2310 | } | |
| fc46f680 JS |
2311 | |
| 2312 | int | |
| b45c5139 MD |
2313 | vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type, |
| 2314 | uint16_t d_namlen, const char *d_name) | |
| fc46f680 | 2315 | { |
| 01f31ab3 JS |
2316 | struct dirent *dp; |
| 2317 | size_t len; | |
| fc46f680 | 2318 | |
| 01f31ab3 JS |
2319 | len = _DIRENT_RECLEN(d_namlen); |
| 2320 | if (len > uio->uio_resid) | |
| fc46f680 JS |
2321 | return(1); |
| 2322 | ||
| efda3bd0 | 2323 | dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO); |
| 01f31ab3 JS |
2324 | |
| 2325 | dp->d_ino = d_ino; | |
| 2326 | dp->d_namlen = d_namlen; | |
| fc46f680 JS |
2327 | dp->d_type = d_type; |
| 2328 | bcopy(d_name, dp->d_name, d_namlen); | |
| fc46f680 | 2329 | |
| 01f31ab3 JS |
2330 | *error = uiomove((caddr_t)dp, len, uio); |
| 2331 | ||
| efda3bd0 | 2332 | kfree(dp, M_TEMP); |
| fc46f680 JS |
2333 | |
| 2334 | return(0); | |
| 2335 | } | |
| 7540ab49 | 2336 | |
| 349433c9 MD |
2337 | void |
| 2338 | vn_mark_atime(struct vnode *vp, struct thread *td) | |
| 2339 | { | |
| 2340 | struct proc *p = td->td_proc; | |
| 2341 | struct ucred *cred = p ? p->p_ucred : proc0.p_ucred; | |
| 2342 | ||
| 2343 | if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) { | |
| 2344 | VOP_MARKATIME(vp, cred); | |
| 2345 | } | |
| 2346 | } |