xref: /freebsd/sys/ufs/ffs/ffs_vnops.c (revision def7fe87e9b28032572ca6f820a260677fd0c2d5)
1 /*-
2  * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * Copyright (c) 1982, 1986, 1989, 1993
33  *	The Regents of the University of California.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 4. Neither the name of the University nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  *
59  *	from: @(#)ufs_readwrite.c	8.11 (Berkeley) 5/8/95
60  * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ...
61  *	@(#)ffs_vnops.c	8.15 (Berkeley) 5/14/95
62  */
63 
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
66 
67 #include <sys/param.h>
68 #include <sys/bio.h>
69 #include <sys/systm.h>
70 #include <sys/buf.h>
71 #include <sys/conf.h>
72 #include <sys/extattr.h>
73 #include <sys/kernel.h>
74 #include <sys/limits.h>
75 #include <sys/malloc.h>
76 #include <sys/mount.h>
77 #include <sys/priv.h>
78 #include <sys/rwlock.h>
79 #include <sys/stat.h>
80 #include <sys/sysctl.h>
81 #include <sys/vmmeter.h>
82 #include <sys/vnode.h>
83 
84 #include <vm/vm.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_pager.h>
90 #include <vm/vm_pageout.h>
91 #include <vm/vnode_pager.h>
92 
93 #include <ufs/ufs/extattr.h>
94 #include <ufs/ufs/quota.h>
95 #include <ufs/ufs/inode.h>
96 #include <ufs/ufs/ufs_extern.h>
97 #include <ufs/ufs/ufsmount.h>
98 
99 #include <ufs/ffs/fs.h>
100 #include <ufs/ffs/ffs_extern.h>
101 #include "opt_directio.h"
102 #include "opt_ffs.h"
103 
104 #ifdef DIRECTIO
105 extern int	ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
106 #endif
107 static vop_fdatasync_t	ffs_fdatasync;
108 static vop_fsync_t	ffs_fsync;
109 static vop_getpages_t	ffs_getpages;
110 static vop_lock1_t	ffs_lock;
111 static vop_read_t	ffs_read;
112 static vop_write_t	ffs_write;
113 static int	ffs_extread(struct vnode *vp, struct uio *uio, int ioflag);
114 static int	ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag,
115 		    struct ucred *cred);
116 static vop_strategy_t	ffsext_strategy;
117 static vop_closeextattr_t	ffs_closeextattr;
118 static vop_deleteextattr_t	ffs_deleteextattr;
119 static vop_getextattr_t	ffs_getextattr;
120 static vop_listextattr_t	ffs_listextattr;
121 static vop_openextattr_t	ffs_openextattr;
122 static vop_setextattr_t	ffs_setextattr;
123 static vop_vptofh_t	ffs_vptofh;
124 
125 /* Global vfs data structures for ufs. */
126 struct vop_vector ffs_vnodeops1 = {
127 	.vop_default =		&ufs_vnodeops,
128 	.vop_fsync =		ffs_fsync,
129 	.vop_fdatasync =	ffs_fdatasync,
130 	.vop_getpages =		ffs_getpages,
131 	.vop_getpages_async =	vnode_pager_local_getpages_async,
132 	.vop_lock1 =		ffs_lock,
133 	.vop_read =		ffs_read,
134 	.vop_reallocblks =	ffs_reallocblks,
135 	.vop_write =		ffs_write,
136 	.vop_vptofh =		ffs_vptofh,
137 };
138 
139 struct vop_vector ffs_fifoops1 = {
140 	.vop_default =		&ufs_fifoops,
141 	.vop_fsync =		ffs_fsync,
142 	.vop_fdatasync =	ffs_fdatasync,
143 	.vop_reallocblks =	ffs_reallocblks, /* XXX: really ??? */
144 	.vop_vptofh =		ffs_vptofh,
145 };
146 
147 /* Global vfs data structures for ufs. */
148 struct vop_vector ffs_vnodeops2 = {
149 	.vop_default =		&ufs_vnodeops,
150 	.vop_fsync =		ffs_fsync,
151 	.vop_fdatasync =	ffs_fdatasync,
152 	.vop_getpages =		ffs_getpages,
153 	.vop_getpages_async =	vnode_pager_local_getpages_async,
154 	.vop_lock1 =		ffs_lock,
155 	.vop_read =		ffs_read,
156 	.vop_reallocblks =	ffs_reallocblks,
157 	.vop_write =		ffs_write,
158 	.vop_closeextattr =	ffs_closeextattr,
159 	.vop_deleteextattr =	ffs_deleteextattr,
160 	.vop_getextattr =	ffs_getextattr,
161 	.vop_listextattr =	ffs_listextattr,
162 	.vop_openextattr =	ffs_openextattr,
163 	.vop_setextattr =	ffs_setextattr,
164 	.vop_vptofh =		ffs_vptofh,
165 };
166 
167 struct vop_vector ffs_fifoops2 = {
168 	.vop_default =		&ufs_fifoops,
169 	.vop_fsync =		ffs_fsync,
170 	.vop_fdatasync =	ffs_fdatasync,
171 	.vop_lock1 =		ffs_lock,
172 	.vop_reallocblks =	ffs_reallocblks,
173 	.vop_strategy =		ffsext_strategy,
174 	.vop_closeextattr =	ffs_closeextattr,
175 	.vop_deleteextattr =	ffs_deleteextattr,
176 	.vop_getextattr =	ffs_getextattr,
177 	.vop_listextattr =	ffs_listextattr,
178 	.vop_openextattr =	ffs_openextattr,
179 	.vop_setextattr =	ffs_setextattr,
180 	.vop_vptofh =		ffs_vptofh,
181 };
182 
183 /*
184  * Synch an open file.
185  */
186 /* ARGSUSED */
187 static int
188 ffs_fsync(struct vop_fsync_args *ap)
189 {
190 	struct vnode *vp;
191 	struct bufobj *bo;
192 	int error;
193 
194 	vp = ap->a_vp;
195 	bo = &vp->v_bufobj;
196 retry:
197 	error = ffs_syncvnode(vp, ap->a_waitfor, 0);
198 	if (error)
199 		return (error);
200 	if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) {
201 		error = softdep_fsync(vp);
202 		if (error)
203 			return (error);
204 
205 		/*
206 		 * The softdep_fsync() function may drop vp lock,
207 		 * allowing for dirty buffers to reappear on the
208 		 * bo_dirty list. Recheck and resync as needed.
209 		 */
210 		BO_LOCK(bo);
211 		if ((vp->v_type == VREG || vp->v_type == VDIR) &&
212 		    (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
213 			BO_UNLOCK(bo);
214 			goto retry;
215 		}
216 		BO_UNLOCK(bo);
217 	}
218 	return (0);
219 }
220 
221 int
222 ffs_syncvnode(struct vnode *vp, int waitfor, int flags)
223 {
224 	struct inode *ip;
225 	struct bufobj *bo;
226 	struct buf *bp, *nbp;
227 	ufs_lbn_t lbn;
228 	int error, passes;
229 	bool still_dirty, wait;
230 
231 	ip = VTOI(vp);
232 	ip->i_flag &= ~IN_NEEDSYNC;
233 	bo = &vp->v_bufobj;
234 
235 	/*
236 	 * When doing MNT_WAIT we must first flush all dependencies
237 	 * on the inode.
238 	 */
239 	if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
240 	    (error = softdep_sync_metadata(vp)) != 0)
241 		return (error);
242 
243 	/*
244 	 * Flush all dirty buffers associated with a vnode.
245 	 */
246 	error = 0;
247 	passes = 0;
248 	wait = false;	/* Always do an async pass first. */
249 	lbn = lblkno(ITOFS(ip), (ip->i_size + ITOFS(ip)->fs_bsize - 1));
250 	BO_LOCK(bo);
251 loop:
252 	TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
253 		bp->b_vflags &= ~BV_SCANNED;
254 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
255 		/*
256 		 * Reasons to skip this buffer: it has already been considered
257 		 * on this pass, the buffer has dependencies that will cause
258 		 * it to be redirtied and it has not already been deferred,
259 		 * or it is already being written.
260 		 */
261 		if ((bp->b_vflags & BV_SCANNED) != 0)
262 			continue;
263 		bp->b_vflags |= BV_SCANNED;
264 		/*
265 		 * Flush indirects in order, if requested.
266 		 *
267 		 * Note that if only datasync is requested, we can
268 		 * skip indirect blocks when softupdates are not
269 		 * active.  Otherwise we must flush them with data,
270 		 * since dependencies prevent data block writes.
271 		 */
272 		if (waitfor == MNT_WAIT && bp->b_lblkno <= -NDADDR &&
273 		    (lbn_level(bp->b_lblkno) >= passes ||
274 		    ((flags & DATA_ONLY) != 0 && !DOINGSOFTDEP(vp))))
275 			continue;
276 		if (bp->b_lblkno > lbn)
277 			panic("ffs_syncvnode: syncing truncated data.");
278 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
279 			BO_UNLOCK(bo);
280 		} else if (wait) {
281 			if (BUF_LOCK(bp,
282 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
283 			    BO_LOCKPTR(bo)) != 0) {
284 				bp->b_vflags &= ~BV_SCANNED;
285 				goto next;
286 			}
287 		} else
288 			continue;
289 		if ((bp->b_flags & B_DELWRI) == 0)
290 			panic("ffs_fsync: not dirty");
291 		/*
292 		 * Check for dependencies and potentially complete them.
293 		 */
294 		if (!LIST_EMPTY(&bp->b_dep) &&
295 		    (error = softdep_sync_buf(vp, bp,
296 		    wait ? MNT_WAIT : MNT_NOWAIT)) != 0) {
297 			/* I/O error. */
298 			if (error != EBUSY) {
299 				BUF_UNLOCK(bp);
300 				return (error);
301 			}
302 			/* If we deferred once, don't defer again. */
303 		    	if ((bp->b_flags & B_DEFERRED) == 0) {
304 				bp->b_flags |= B_DEFERRED;
305 				BUF_UNLOCK(bp);
306 				goto next;
307 			}
308 		}
309 		if (wait) {
310 			bremfree(bp);
311 			if ((error = bwrite(bp)) != 0)
312 				return (error);
313 		} else if ((bp->b_flags & B_CLUSTEROK)) {
314 			(void) vfs_bio_awrite(bp);
315 		} else {
316 			bremfree(bp);
317 			(void) bawrite(bp);
318 		}
319 next:
320 		/*
321 		 * Since we may have slept during the I/O, we need
322 		 * to start from a known point.
323 		 */
324 		BO_LOCK(bo);
325 		nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
326 	}
327 	if (waitfor != MNT_WAIT) {
328 		BO_UNLOCK(bo);
329 		if ((flags & NO_INO_UPDT) != 0)
330 			return (0);
331 		else
332 			return (ffs_update(vp, 0));
333 	}
334 	/* Drain IO to see if we're done. */
335 	bufobj_wwait(bo, 0, 0);
336 	/*
337 	 * Block devices associated with filesystems may have new I/O
338 	 * requests posted for them even if the vnode is locked, so no
339 	 * amount of trying will get them clean.  We make several passes
340 	 * as a best effort.
341 	 *
342 	 * Regular files may need multiple passes to flush all dependency
343 	 * work as it is possible that we must write once per indirect
344 	 * level, once for the leaf, and once for the inode and each of
345 	 * these will be done with one sync and one async pass.
346 	 */
347 	if (bo->bo_dirty.bv_cnt > 0) {
348 		if ((flags & DATA_ONLY) == 0) {
349 			still_dirty = true;
350 		} else {
351 			/*
352 			 * For data-only sync, dirty indirect buffers
353 			 * are ignored.
354 			 */
355 			still_dirty = false;
356 			TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
357 				if (bp->b_lblkno > -NDADDR) {
358 					still_dirty = true;
359 					break;
360 				}
361 			}
362 		}
363 
364 		if (still_dirty) {
365 			/* Write the inode after sync passes to flush deps. */
366 			if (wait && DOINGSOFTDEP(vp) &&
367 			    (flags & NO_INO_UPDT) == 0) {
368 				BO_UNLOCK(bo);
369 				ffs_update(vp, 1);
370 				BO_LOCK(bo);
371 			}
372 			/* switch between sync/async. */
373 			wait = !wait;
374 			if (wait || ++passes < NIADDR + 2)
375 				goto loop;
376 #ifdef INVARIANTS
377 			if (!vn_isdisk(vp, NULL))
378 				vn_printf(vp, "ffs_fsync: dirty ");
379 #endif
380 		}
381 	}
382 	BO_UNLOCK(bo);
383 	error = 0;
384 	if ((flags & DATA_ONLY) == 0) {
385 		if ((flags & NO_INO_UPDT) == 0)
386 			error = ffs_update(vp, 1);
387 		if (DOINGSUJ(vp))
388 			softdep_journal_fsync(VTOI(vp));
389 	}
390 	return (error);
391 }
392 
393 static int
394 ffs_fdatasync(struct vop_fdatasync_args *ap)
395 {
396 
397 	return (ffs_syncvnode(ap->a_vp, MNT_WAIT, DATA_ONLY));
398 }
399 
400 static int
401 ffs_lock(ap)
402 	struct vop_lock1_args /* {
403 		struct vnode *a_vp;
404 		int a_flags;
405 		struct thread *a_td;
406 		char *file;
407 		int line;
408 	} */ *ap;
409 {
410 #ifndef NO_FFS_SNAPSHOT
411 	struct vnode *vp;
412 	int flags;
413 	struct lock *lkp;
414 	int result;
415 
416 	switch (ap->a_flags & LK_TYPE_MASK) {
417 	case LK_SHARED:
418 	case LK_UPGRADE:
419 	case LK_EXCLUSIVE:
420 		vp = ap->a_vp;
421 		flags = ap->a_flags;
422 		for (;;) {
423 #ifdef DEBUG_VFS_LOCKS
424 			KASSERT(vp->v_holdcnt != 0,
425 			    ("ffs_lock %p: zero hold count", vp));
426 #endif
427 			lkp = vp->v_vnlock;
428 			result = _lockmgr_args(lkp, flags, VI_MTX(vp),
429 			    LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT,
430 			    ap->a_file, ap->a_line);
431 			if (lkp == vp->v_vnlock || result != 0)
432 				break;
433 			/*
434 			 * Apparent success, except that the vnode
435 			 * mutated between snapshot file vnode and
436 			 * regular file vnode while this process
437 			 * slept.  The lock currently held is not the
438 			 * right lock.  Release it, and try to get the
439 			 * new lock.
440 			 */
441 			(void) _lockmgr_args(lkp, LK_RELEASE, NULL,
442 			    LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT,
443 			    ap->a_file, ap->a_line);
444 			if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
445 			    (LK_INTERLOCK | LK_NOWAIT))
446 				return (EBUSY);
447 			if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
448 				flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
449 			flags &= ~LK_INTERLOCK;
450 		}
451 		break;
452 	default:
453 		result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
454 	}
455 	return (result);
456 #else
457 	return (VOP_LOCK1_APV(&ufs_vnodeops, ap));
458 #endif
459 }
460 
461 /*
462  * Vnode op for reading.
463  */
464 static int
465 ffs_read(ap)
466 	struct vop_read_args /* {
467 		struct vnode *a_vp;
468 		struct uio *a_uio;
469 		int a_ioflag;
470 		struct ucred *a_cred;
471 	} */ *ap;
472 {
473 	struct vnode *vp;
474 	struct inode *ip;
475 	struct uio *uio;
476 	struct fs *fs;
477 	struct buf *bp;
478 	ufs_lbn_t lbn, nextlbn;
479 	off_t bytesinfile;
480 	long size, xfersize, blkoffset;
481 	ssize_t orig_resid;
482 	int error;
483 	int seqcount;
484 	int ioflag;
485 
486 	vp = ap->a_vp;
487 	uio = ap->a_uio;
488 	ioflag = ap->a_ioflag;
489 	if (ap->a_ioflag & IO_EXT)
490 #ifdef notyet
491 		return (ffs_extread(vp, uio, ioflag));
492 #else
493 		panic("ffs_read+IO_EXT");
494 #endif
495 #ifdef DIRECTIO
496 	if ((ioflag & IO_DIRECT) != 0) {
497 		int workdone;
498 
499 		error = ffs_rawread(vp, uio, &workdone);
500 		if (error != 0 || workdone != 0)
501 			return error;
502 	}
503 #endif
504 
505 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
506 	ip = VTOI(vp);
507 
508 #ifdef INVARIANTS
509 	if (uio->uio_rw != UIO_READ)
510 		panic("ffs_read: mode");
511 
512 	if (vp->v_type == VLNK) {
513 		if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
514 			panic("ffs_read: short symlink");
515 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
516 		panic("ffs_read: type %d",  vp->v_type);
517 #endif
518 	orig_resid = uio->uio_resid;
519 	KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
520 	if (orig_resid == 0)
521 		return (0);
522 	KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
523 	fs = ITOFS(ip);
524 	if (uio->uio_offset < ip->i_size &&
525 	    uio->uio_offset >= fs->fs_maxfilesize)
526 		return (EOVERFLOW);
527 
528 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
529 		if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
530 			break;
531 		lbn = lblkno(fs, uio->uio_offset);
532 		nextlbn = lbn + 1;
533 
534 		/*
535 		 * size of buffer.  The buffer representing the
536 		 * end of the file is rounded up to the size of
537 		 * the block type ( fragment or full block,
538 		 * depending ).
539 		 */
540 		size = blksize(fs, ip, lbn);
541 		blkoffset = blkoff(fs, uio->uio_offset);
542 
543 		/*
544 		 * The amount we want to transfer in this iteration is
545 		 * one FS block less the amount of the data before
546 		 * our startpoint (duh!)
547 		 */
548 		xfersize = fs->fs_bsize - blkoffset;
549 
550 		/*
551 		 * But if we actually want less than the block,
552 		 * or the file doesn't have a whole block more of data,
553 		 * then use the lesser number.
554 		 */
555 		if (uio->uio_resid < xfersize)
556 			xfersize = uio->uio_resid;
557 		if (bytesinfile < xfersize)
558 			xfersize = bytesinfile;
559 
560 		if (lblktosize(fs, nextlbn) >= ip->i_size) {
561 			/*
562 			 * Don't do readahead if this is the end of the file.
563 			 */
564 			error = bread_gb(vp, lbn, size, NOCRED,
565 			    GB_UNMAPPED, &bp);
566 		} else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
567 			/*
568 			 * Otherwise if we are allowed to cluster,
569 			 * grab as much as we can.
570 			 *
571 			 * XXX  This may not be a win if we are not
572 			 * doing sequential access.
573 			 */
574 			error = cluster_read(vp, ip->i_size, lbn,
575 			    size, NOCRED, blkoffset + uio->uio_resid,
576 			    seqcount, GB_UNMAPPED, &bp);
577 		} else if (seqcount > 1) {
578 			/*
579 			 * If we are NOT allowed to cluster, then
580 			 * if we appear to be acting sequentially,
581 			 * fire off a request for a readahead
582 			 * as well as a read. Note that the 4th and 5th
583 			 * arguments point to arrays of the size specified in
584 			 * the 6th argument.
585 			 */
586 			u_int nextsize = blksize(fs, ip, nextlbn);
587 			error = breadn_flags(vp, lbn, size, &nextlbn,
588 			    &nextsize, 1, NOCRED, GB_UNMAPPED, &bp);
589 		} else {
590 			/*
591 			 * Failing all of the above, just read what the
592 			 * user asked for. Interestingly, the same as
593 			 * the first option above.
594 			 */
595 			error = bread_gb(vp, lbn, size, NOCRED,
596 			    GB_UNMAPPED, &bp);
597 		}
598 		if (error) {
599 			brelse(bp);
600 			bp = NULL;
601 			break;
602 		}
603 
604 		/*
605 		 * If IO_DIRECT then set B_DIRECT for the buffer.  This
606 		 * will cause us to attempt to release the buffer later on
607 		 * and will cause the buffer cache to attempt to free the
608 		 * underlying pages.
609 		 */
610 		if (ioflag & IO_DIRECT)
611 			bp->b_flags |= B_DIRECT;
612 
613 		/*
614 		 * We should only get non-zero b_resid when an I/O error
615 		 * has occurred, which should cause us to break above.
616 		 * However, if the short read did not cause an error,
617 		 * then we want to ensure that we do not uiomove bad
618 		 * or uninitialized data.
619 		 */
620 		size -= bp->b_resid;
621 		if (size < xfersize) {
622 			if (size == 0)
623 				break;
624 			xfersize = size;
625 		}
626 
627 		if (buf_mapped(bp)) {
628 			error = vn_io_fault_uiomove((char *)bp->b_data +
629 			    blkoffset, (int)xfersize, uio);
630 		} else {
631 			error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
632 			    (int)xfersize, uio);
633 		}
634 		if (error)
635 			break;
636 
637 		if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
638 		   (LIST_EMPTY(&bp->b_dep))) {
639 			/*
640 			 * If there are no dependencies, and it's VMIO,
641 			 * then we don't need the buf, mark it available
642 			 * for freeing.  For non-direct VMIO reads, the VM
643 			 * has the data.
644 			 */
645 			bp->b_flags |= B_RELBUF;
646 			brelse(bp);
647 		} else {
648 			/*
649 			 * Otherwise let whoever
650 			 * made the request take care of
651 			 * freeing it. We just queue
652 			 * it onto another list.
653 			 */
654 			bqrelse(bp);
655 		}
656 	}
657 
658 	/*
659 	 * This can only happen in the case of an error
660 	 * because the loop above resets bp to NULL on each iteration
661 	 * and on normal completion has not set a new value into it.
662 	 * so it must have come from a 'break' statement
663 	 */
664 	if (bp != NULL) {
665 		if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
666 		   (LIST_EMPTY(&bp->b_dep))) {
667 			bp->b_flags |= B_RELBUF;
668 			brelse(bp);
669 		} else {
670 			bqrelse(bp);
671 		}
672 	}
673 
674 	if ((error == 0 || uio->uio_resid != orig_resid) &&
675 	    (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0 &&
676 	    (ip->i_flag & IN_ACCESS) == 0) {
677 		VI_LOCK(vp);
678 		ip->i_flag |= IN_ACCESS;
679 		VI_UNLOCK(vp);
680 	}
681 	return (error);
682 }
683 
684 /*
685  * Vnode op for writing.
686  */
687 static int
688 ffs_write(ap)
689 	struct vop_write_args /* {
690 		struct vnode *a_vp;
691 		struct uio *a_uio;
692 		int a_ioflag;
693 		struct ucred *a_cred;
694 	} */ *ap;
695 {
696 	struct vnode *vp;
697 	struct uio *uio;
698 	struct inode *ip;
699 	struct fs *fs;
700 	struct buf *bp;
701 	ufs_lbn_t lbn;
702 	off_t osize;
703 	ssize_t resid;
704 	int seqcount;
705 	int blkoffset, error, flags, ioflag, size, xfersize;
706 
707 	vp = ap->a_vp;
708 	uio = ap->a_uio;
709 	ioflag = ap->a_ioflag;
710 	if (ap->a_ioflag & IO_EXT)
711 #ifdef notyet
712 		return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
713 #else
714 		panic("ffs_write+IO_EXT");
715 #endif
716 
717 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
718 	ip = VTOI(vp);
719 
720 #ifdef INVARIANTS
721 	if (uio->uio_rw != UIO_WRITE)
722 		panic("ffs_write: mode");
723 #endif
724 
725 	switch (vp->v_type) {
726 	case VREG:
727 		if (ioflag & IO_APPEND)
728 			uio->uio_offset = ip->i_size;
729 		if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
730 			return (EPERM);
731 		/* FALLTHROUGH */
732 	case VLNK:
733 		break;
734 	case VDIR:
735 		panic("ffs_write: dir write");
736 		break;
737 	default:
738 		panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
739 			(int)uio->uio_offset,
740 			(int)uio->uio_resid
741 		);
742 	}
743 
744 	KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
745 	KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
746 	fs = ITOFS(ip);
747 	if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
748 		return (EFBIG);
749 	/*
750 	 * Maybe this should be above the vnode op call, but so long as
751 	 * file servers have no limits, I don't think it matters.
752 	 */
753 	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
754 		return (EFBIG);
755 
756 	resid = uio->uio_resid;
757 	osize = ip->i_size;
758 	if (seqcount > BA_SEQMAX)
759 		flags = BA_SEQMAX << BA_SEQSHIFT;
760 	else
761 		flags = seqcount << BA_SEQSHIFT;
762 	if (ioflag & IO_SYNC)
763 		flags |= IO_SYNC;
764 	flags |= BA_UNMAPPED;
765 
766 	for (error = 0; uio->uio_resid > 0;) {
767 		lbn = lblkno(fs, uio->uio_offset);
768 		blkoffset = blkoff(fs, uio->uio_offset);
769 		xfersize = fs->fs_bsize - blkoffset;
770 		if (uio->uio_resid < xfersize)
771 			xfersize = uio->uio_resid;
772 		if (uio->uio_offset + xfersize > ip->i_size)
773 			vnode_pager_setsize(vp, uio->uio_offset + xfersize);
774 
775 		/*
776 		 * We must perform a read-before-write if the transfer size
777 		 * does not cover the entire buffer.
778 		 */
779 		if (fs->fs_bsize > xfersize)
780 			flags |= BA_CLRBUF;
781 		else
782 			flags &= ~BA_CLRBUF;
783 /* XXX is uio->uio_offset the right thing here? */
784 		error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
785 		    ap->a_cred, flags, &bp);
786 		if (error != 0) {
787 			vnode_pager_setsize(vp, ip->i_size);
788 			break;
789 		}
790 		if (ioflag & IO_DIRECT)
791 			bp->b_flags |= B_DIRECT;
792 		if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
793 			bp->b_flags |= B_NOCACHE;
794 
795 		if (uio->uio_offset + xfersize > ip->i_size) {
796 			ip->i_size = uio->uio_offset + xfersize;
797 			DIP_SET(ip, i_size, ip->i_size);
798 		}
799 
800 		size = blksize(fs, ip, lbn) - bp->b_resid;
801 		if (size < xfersize)
802 			xfersize = size;
803 
804 		if (buf_mapped(bp)) {
805 			error = vn_io_fault_uiomove((char *)bp->b_data +
806 			    blkoffset, (int)xfersize, uio);
807 		} else {
808 			error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
809 			    (int)xfersize, uio);
810 		}
811 		/*
812 		 * If the buffer is not already filled and we encounter an
813 		 * error while trying to fill it, we have to clear out any
814 		 * garbage data from the pages instantiated for the buffer.
815 		 * If we do not, a failed uiomove() during a write can leave
816 		 * the prior contents of the pages exposed to a userland mmap.
817 		 *
818 		 * Note that we need only clear buffers with a transfer size
819 		 * equal to the block size because buffers with a shorter
820 		 * transfer size were cleared above by the call to UFS_BALLOC()
821 		 * with the BA_CLRBUF flag set.
822 		 *
823 		 * If the source region for uiomove identically mmaps the
824 		 * buffer, uiomove() performed the NOP copy, and the buffer
825 		 * content remains valid because the page fault handler
826 		 * validated the pages.
827 		 */
828 		if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
829 		    fs->fs_bsize == xfersize)
830 			vfs_bio_clrbuf(bp);
831 		if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
832 		   (LIST_EMPTY(&bp->b_dep))) {
833 			bp->b_flags |= B_RELBUF;
834 		}
835 
836 		/*
837 		 * If IO_SYNC each buffer is written synchronously.  Otherwise
838 		 * if we have a severe page deficiency write the buffer
839 		 * asynchronously.  Otherwise try to cluster, and if that
840 		 * doesn't do it then either do an async write (if O_DIRECT),
841 		 * or a delayed write (if not).
842 		 */
843 		if (ioflag & IO_SYNC) {
844 			(void)bwrite(bp);
845 		} else if (vm_page_count_severe() ||
846 			    buf_dirty_count_severe() ||
847 			    (ioflag & IO_ASYNC)) {
848 			bp->b_flags |= B_CLUSTEROK;
849 			bawrite(bp);
850 		} else if (xfersize + blkoffset == fs->fs_bsize) {
851 			if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
852 				bp->b_flags |= B_CLUSTEROK;
853 				cluster_write(vp, bp, ip->i_size, seqcount,
854 				    GB_UNMAPPED);
855 			} else {
856 				bawrite(bp);
857 			}
858 		} else if (ioflag & IO_DIRECT) {
859 			bp->b_flags |= B_CLUSTEROK;
860 			bawrite(bp);
861 		} else {
862 			bp->b_flags |= B_CLUSTEROK;
863 			bdwrite(bp);
864 		}
865 		if (error || xfersize == 0)
866 			break;
867 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
868 	}
869 	/*
870 	 * If we successfully wrote any data, and we are not the superuser
871 	 * we clear the setuid and setgid bits as a precaution against
872 	 * tampering.
873 	 */
874 	if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
875 	    ap->a_cred) {
876 		if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0)) {
877 			ip->i_mode &= ~(ISUID | ISGID);
878 			DIP_SET(ip, i_mode, ip->i_mode);
879 		}
880 	}
881 	if (error) {
882 		if (ioflag & IO_UNIT) {
883 			(void)ffs_truncate(vp, osize,
884 			    IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred);
885 			uio->uio_offset -= resid - uio->uio_resid;
886 			uio->uio_resid = resid;
887 		}
888 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
889 		error = ffs_update(vp, 1);
890 	return (error);
891 }
892 
893 /*
894  * Extended attribute area reading.
895  */
896 static int
897 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
898 {
899 	struct inode *ip;
900 	struct ufs2_dinode *dp;
901 	struct fs *fs;
902 	struct buf *bp;
903 	ufs_lbn_t lbn, nextlbn;
904 	off_t bytesinfile;
905 	long size, xfersize, blkoffset;
906 	ssize_t orig_resid;
907 	int error;
908 
909 	ip = VTOI(vp);
910 	fs = ITOFS(ip);
911 	dp = ip->i_din2;
912 
913 #ifdef INVARIANTS
914 	if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
915 		panic("ffs_extread: mode");
916 
917 #endif
918 	orig_resid = uio->uio_resid;
919 	KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
920 	if (orig_resid == 0)
921 		return (0);
922 	KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
923 
924 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
925 		if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
926 			break;
927 		lbn = lblkno(fs, uio->uio_offset);
928 		nextlbn = lbn + 1;
929 
930 		/*
931 		 * size of buffer.  The buffer representing the
932 		 * end of the file is rounded up to the size of
933 		 * the block type ( fragment or full block,
934 		 * depending ).
935 		 */
936 		size = sblksize(fs, dp->di_extsize, lbn);
937 		blkoffset = blkoff(fs, uio->uio_offset);
938 
939 		/*
940 		 * The amount we want to transfer in this iteration is
941 		 * one FS block less the amount of the data before
942 		 * our startpoint (duh!)
943 		 */
944 		xfersize = fs->fs_bsize - blkoffset;
945 
946 		/*
947 		 * But if we actually want less than the block,
948 		 * or the file doesn't have a whole block more of data,
949 		 * then use the lesser number.
950 		 */
951 		if (uio->uio_resid < xfersize)
952 			xfersize = uio->uio_resid;
953 		if (bytesinfile < xfersize)
954 			xfersize = bytesinfile;
955 
956 		if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
957 			/*
958 			 * Don't do readahead if this is the end of the info.
959 			 */
960 			error = bread(vp, -1 - lbn, size, NOCRED, &bp);
961 		} else {
962 			/*
963 			 * If we have a second block, then
964 			 * fire off a request for a readahead
965 			 * as well as a read. Note that the 4th and 5th
966 			 * arguments point to arrays of the size specified in
967 			 * the 6th argument.
968 			 */
969 			u_int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
970 
971 			nextlbn = -1 - nextlbn;
972 			error = breadn(vp, -1 - lbn,
973 			    size, &nextlbn, &nextsize, 1, NOCRED, &bp);
974 		}
975 		if (error) {
976 			brelse(bp);
977 			bp = NULL;
978 			break;
979 		}
980 
981 		/*
982 		 * If IO_DIRECT then set B_DIRECT for the buffer.  This
983 		 * will cause us to attempt to release the buffer later on
984 		 * and will cause the buffer cache to attempt to free the
985 		 * underlying pages.
986 		 */
987 		if (ioflag & IO_DIRECT)
988 			bp->b_flags |= B_DIRECT;
989 
990 		/*
991 		 * We should only get non-zero b_resid when an I/O error
992 		 * has occurred, which should cause us to break above.
993 		 * However, if the short read did not cause an error,
994 		 * then we want to ensure that we do not uiomove bad
995 		 * or uninitialized data.
996 		 */
997 		size -= bp->b_resid;
998 		if (size < xfersize) {
999 			if (size == 0)
1000 				break;
1001 			xfersize = size;
1002 		}
1003 
1004 		error = uiomove((char *)bp->b_data + blkoffset,
1005 					(int)xfersize, uio);
1006 		if (error)
1007 			break;
1008 
1009 		if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
1010 		   (LIST_EMPTY(&bp->b_dep))) {
1011 			/*
1012 			 * If there are no dependencies, and it's VMIO,
1013 			 * then we don't need the buf, mark it available
1014 			 * for freeing.  For non-direct VMIO reads, the VM
1015 			 * has the data.
1016 			 */
1017 			bp->b_flags |= B_RELBUF;
1018 			brelse(bp);
1019 		} else {
1020 			/*
1021 			 * Otherwise let whoever
1022 			 * made the request take care of
1023 			 * freeing it. We just queue
1024 			 * it onto another list.
1025 			 */
1026 			bqrelse(bp);
1027 		}
1028 	}
1029 
1030 	/*
1031 	 * This can only happen in the case of an error
1032 	 * because the loop above resets bp to NULL on each iteration
1033 	 * and on normal completion has not set a new value into it.
1034 	 * so it must have come from a 'break' statement
1035 	 */
1036 	if (bp != NULL) {
1037 		if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
1038 		   (LIST_EMPTY(&bp->b_dep))) {
1039 			bp->b_flags |= B_RELBUF;
1040 			brelse(bp);
1041 		} else {
1042 			bqrelse(bp);
1043 		}
1044 	}
1045 	return (error);
1046 }
1047 
1048 /*
1049  * Extended attribute area writing.
1050  */
1051 static int
1052 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1053 {
1054 	struct inode *ip;
1055 	struct ufs2_dinode *dp;
1056 	struct fs *fs;
1057 	struct buf *bp;
1058 	ufs_lbn_t lbn;
1059 	off_t osize;
1060 	ssize_t resid;
1061 	int blkoffset, error, flags, size, xfersize;
1062 
1063 	ip = VTOI(vp);
1064 	fs = ITOFS(ip);
1065 	dp = ip->i_din2;
1066 
1067 #ifdef INVARIANTS
1068 	if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1069 		panic("ffs_extwrite: mode");
1070 #endif
1071 
1072 	if (ioflag & IO_APPEND)
1073 		uio->uio_offset = dp->di_extsize;
1074 	KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1075 	KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1076 	if ((uoff_t)uio->uio_offset + uio->uio_resid > NXADDR * fs->fs_bsize)
1077 		return (EFBIG);
1078 
1079 	resid = uio->uio_resid;
1080 	osize = dp->di_extsize;
1081 	flags = IO_EXT;
1082 	if (ioflag & IO_SYNC)
1083 		flags |= IO_SYNC;
1084 
1085 	for (error = 0; uio->uio_resid > 0;) {
1086 		lbn = lblkno(fs, uio->uio_offset);
1087 		blkoffset = blkoff(fs, uio->uio_offset);
1088 		xfersize = fs->fs_bsize - blkoffset;
1089 		if (uio->uio_resid < xfersize)
1090 			xfersize = uio->uio_resid;
1091 
1092 		/*
1093 		 * We must perform a read-before-write if the transfer size
1094 		 * does not cover the entire buffer.
1095 		 */
1096 		if (fs->fs_bsize > xfersize)
1097 			flags |= BA_CLRBUF;
1098 		else
1099 			flags &= ~BA_CLRBUF;
1100 		error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1101 		    ucred, flags, &bp);
1102 		if (error != 0)
1103 			break;
1104 		/*
1105 		 * If the buffer is not valid we have to clear out any
1106 		 * garbage data from the pages instantiated for the buffer.
1107 		 * If we do not, a failed uiomove() during a write can leave
1108 		 * the prior contents of the pages exposed to a userland
1109 		 * mmap().  XXX deal with uiomove() errors a better way.
1110 		 */
1111 		if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1112 			vfs_bio_clrbuf(bp);
1113 		if (ioflag & IO_DIRECT)
1114 			bp->b_flags |= B_DIRECT;
1115 
1116 		if (uio->uio_offset + xfersize > dp->di_extsize)
1117 			dp->di_extsize = uio->uio_offset + xfersize;
1118 
1119 		size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1120 		if (size < xfersize)
1121 			xfersize = size;
1122 
1123 		error =
1124 		    uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1125 		if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
1126 		   (LIST_EMPTY(&bp->b_dep))) {
1127 			bp->b_flags |= B_RELBUF;
1128 		}
1129 
1130 		/*
1131 		 * If IO_SYNC each buffer is written synchronously.  Otherwise
1132 		 * if we have a severe page deficiency write the buffer
1133 		 * asynchronously.  Otherwise try to cluster, and if that
1134 		 * doesn't do it then either do an async write (if O_DIRECT),
1135 		 * or a delayed write (if not).
1136 		 */
1137 		if (ioflag & IO_SYNC) {
1138 			(void)bwrite(bp);
1139 		} else if (vm_page_count_severe() ||
1140 			    buf_dirty_count_severe() ||
1141 			    xfersize + blkoffset == fs->fs_bsize ||
1142 			    (ioflag & (IO_ASYNC | IO_DIRECT)))
1143 			bawrite(bp);
1144 		else
1145 			bdwrite(bp);
1146 		if (error || xfersize == 0)
1147 			break;
1148 		ip->i_flag |= IN_CHANGE;
1149 	}
1150 	/*
1151 	 * If we successfully wrote any data, and we are not the superuser
1152 	 * we clear the setuid and setgid bits as a precaution against
1153 	 * tampering.
1154 	 */
1155 	if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1156 		if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID, 0)) {
1157 			ip->i_mode &= ~(ISUID | ISGID);
1158 			dp->di_mode = ip->i_mode;
1159 		}
1160 	}
1161 	if (error) {
1162 		if (ioflag & IO_UNIT) {
1163 			(void)ffs_truncate(vp, osize,
1164 			    IO_EXT | (ioflag&IO_SYNC), ucred);
1165 			uio->uio_offset -= resid - uio->uio_resid;
1166 			uio->uio_resid = resid;
1167 		}
1168 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1169 		error = ffs_update(vp, 1);
1170 	return (error);
1171 }
1172 
1173 
1174 /*
1175  * Vnode operating to retrieve a named extended attribute.
1176  *
1177  * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1178  * the length of the EA, and possibly the pointer to the entry and to the data.
1179  */
1180 static int
1181 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name, u_char **eap, u_char **eac)
1182 {
1183 	u_char *p, *pe, *pn, *p0;
1184 	int eapad1, eapad2, ealength, ealen, nlen;
1185 	uint32_t ul;
1186 
1187 	pe = ptr + length;
1188 	nlen = strlen(name);
1189 
1190 	for (p = ptr; p < pe; p = pn) {
1191 		p0 = p;
1192 		bcopy(p, &ul, sizeof(ul));
1193 		pn = p + ul;
1194 		/* make sure this entry is complete */
1195 		if (pn > pe)
1196 			break;
1197 		p += sizeof(uint32_t);
1198 		if (*p != nspace)
1199 			continue;
1200 		p++;
1201 		eapad2 = *p++;
1202 		if (*p != nlen)
1203 			continue;
1204 		p++;
1205 		if (bcmp(p, name, nlen))
1206 			continue;
1207 		ealength = sizeof(uint32_t) + 3 + nlen;
1208 		eapad1 = 8 - (ealength % 8);
1209 		if (eapad1 == 8)
1210 			eapad1 = 0;
1211 		ealength += eapad1;
1212 		ealen = ul - ealength - eapad2;
1213 		p += nlen + eapad1;
1214 		if (eap != NULL)
1215 			*eap = p0;
1216 		if (eac != NULL)
1217 			*eac = p;
1218 		return (ealen);
1219 	}
1220 	return(-1);
1221 }
1222 
1223 static int
1224 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td, int extra)
1225 {
1226 	struct inode *ip;
1227 	struct ufs2_dinode *dp;
1228 	struct fs *fs;
1229 	struct uio luio;
1230 	struct iovec liovec;
1231 	u_int easize;
1232 	int error;
1233 	u_char *eae;
1234 
1235 	ip = VTOI(vp);
1236 	fs = ITOFS(ip);
1237 	dp = ip->i_din2;
1238 	easize = dp->di_extsize;
1239 	if ((uoff_t)easize + extra > NXADDR * fs->fs_bsize)
1240 		return (EFBIG);
1241 
1242 	eae = malloc(easize + extra, M_TEMP, M_WAITOK);
1243 
1244 	liovec.iov_base = eae;
1245 	liovec.iov_len = easize;
1246 	luio.uio_iov = &liovec;
1247 	luio.uio_iovcnt = 1;
1248 	luio.uio_offset = 0;
1249 	luio.uio_resid = easize;
1250 	luio.uio_segflg = UIO_SYSSPACE;
1251 	luio.uio_rw = UIO_READ;
1252 	luio.uio_td = td;
1253 
1254 	error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1255 	if (error) {
1256 		free(eae, M_TEMP);
1257 		return(error);
1258 	}
1259 	*p = eae;
1260 	return (0);
1261 }
1262 
1263 static void
1264 ffs_lock_ea(struct vnode *vp)
1265 {
1266 	struct inode *ip;
1267 
1268 	ip = VTOI(vp);
1269 	VI_LOCK(vp);
1270 	while (ip->i_flag & IN_EA_LOCKED) {
1271 		ip->i_flag |= IN_EA_LOCKWAIT;
1272 		msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea",
1273 		    0);
1274 	}
1275 	ip->i_flag |= IN_EA_LOCKED;
1276 	VI_UNLOCK(vp);
1277 }
1278 
1279 static void
1280 ffs_unlock_ea(struct vnode *vp)
1281 {
1282 	struct inode *ip;
1283 
1284 	ip = VTOI(vp);
1285 	VI_LOCK(vp);
1286 	if (ip->i_flag & IN_EA_LOCKWAIT)
1287 		wakeup(&ip->i_ea_refs);
1288 	ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT);
1289 	VI_UNLOCK(vp);
1290 }
1291 
1292 static int
1293 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1294 {
1295 	struct inode *ip;
1296 	struct ufs2_dinode *dp;
1297 	int error;
1298 
1299 	ip = VTOI(vp);
1300 
1301 	ffs_lock_ea(vp);
1302 	if (ip->i_ea_area != NULL) {
1303 		ip->i_ea_refs++;
1304 		ffs_unlock_ea(vp);
1305 		return (0);
1306 	}
1307 	dp = ip->i_din2;
1308 	error = ffs_rdextattr(&ip->i_ea_area, vp, td, 0);
1309 	if (error) {
1310 		ffs_unlock_ea(vp);
1311 		return (error);
1312 	}
1313 	ip->i_ea_len = dp->di_extsize;
1314 	ip->i_ea_error = 0;
1315 	ip->i_ea_refs++;
1316 	ffs_unlock_ea(vp);
1317 	return (0);
1318 }
1319 
1320 /*
1321  * Vnode extattr transaction commit/abort
1322  */
1323 static int
1324 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1325 {
1326 	struct inode *ip;
1327 	struct uio luio;
1328 	struct iovec liovec;
1329 	int error;
1330 	struct ufs2_dinode *dp;
1331 
1332 	ip = VTOI(vp);
1333 
1334 	ffs_lock_ea(vp);
1335 	if (ip->i_ea_area == NULL) {
1336 		ffs_unlock_ea(vp);
1337 		return (EINVAL);
1338 	}
1339 	dp = ip->i_din2;
1340 	error = ip->i_ea_error;
1341 	if (commit && error == 0) {
1342 		ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1343 		if (cred == NOCRED)
1344 			cred =  vp->v_mount->mnt_cred;
1345 		liovec.iov_base = ip->i_ea_area;
1346 		liovec.iov_len = ip->i_ea_len;
1347 		luio.uio_iov = &liovec;
1348 		luio.uio_iovcnt = 1;
1349 		luio.uio_offset = 0;
1350 		luio.uio_resid = ip->i_ea_len;
1351 		luio.uio_segflg = UIO_SYSSPACE;
1352 		luio.uio_rw = UIO_WRITE;
1353 		luio.uio_td = td;
1354 		/* XXX: I'm not happy about truncating to zero size */
1355 		if (ip->i_ea_len < dp->di_extsize)
1356 			error = ffs_truncate(vp, 0, IO_EXT, cred);
1357 		error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1358 	}
1359 	if (--ip->i_ea_refs == 0) {
1360 		free(ip->i_ea_area, M_TEMP);
1361 		ip->i_ea_area = NULL;
1362 		ip->i_ea_len = 0;
1363 		ip->i_ea_error = 0;
1364 	}
1365 	ffs_unlock_ea(vp);
1366 	return (error);
1367 }
1368 
1369 /*
1370  * Vnode extattr strategy routine for fifos.
1371  *
1372  * We need to check for a read or write of the external attributes.
1373  * Otherwise we just fall through and do the usual thing.
1374  */
1375 static int
1376 ffsext_strategy(struct vop_strategy_args *ap)
1377 /*
1378 struct vop_strategy_args {
1379 	struct vnodeop_desc *a_desc;
1380 	struct vnode *a_vp;
1381 	struct buf *a_bp;
1382 };
1383 */
1384 {
1385 	struct vnode *vp;
1386 	daddr_t lbn;
1387 
1388 	vp = ap->a_vp;
1389 	lbn = ap->a_bp->b_lblkno;
1390 	if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -NXADDR)
1391 		return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1392 	if (vp->v_type == VFIFO)
1393 		return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1394 	panic("spec nodes went here");
1395 }
1396 
1397 /*
1398  * Vnode extattr transaction commit/abort
1399  */
1400 static int
1401 ffs_openextattr(struct vop_openextattr_args *ap)
1402 /*
1403 struct vop_openextattr_args {
1404 	struct vnodeop_desc *a_desc;
1405 	struct vnode *a_vp;
1406 	IN struct ucred *a_cred;
1407 	IN struct thread *a_td;
1408 };
1409 */
1410 {
1411 
1412 	if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1413 		return (EOPNOTSUPP);
1414 
1415 	return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1416 }
1417 
1418 
1419 /*
1420  * Vnode extattr transaction commit/abort
1421  */
1422 static int
1423 ffs_closeextattr(struct vop_closeextattr_args *ap)
1424 /*
1425 struct vop_closeextattr_args {
1426 	struct vnodeop_desc *a_desc;
1427 	struct vnode *a_vp;
1428 	int a_commit;
1429 	IN struct ucred *a_cred;
1430 	IN struct thread *a_td;
1431 };
1432 */
1433 {
1434 
1435 	if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1436 		return (EOPNOTSUPP);
1437 
1438 	if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY))
1439 		return (EROFS);
1440 
1441 	return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td));
1442 }
1443 
1444 /*
1445  * Vnode operation to remove a named attribute.
1446  */
1447 static int
1448 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1449 /*
1450 vop_deleteextattr {
1451 	IN struct vnode *a_vp;
1452 	IN int a_attrnamespace;
1453 	IN const char *a_name;
1454 	IN struct ucred *a_cred;
1455 	IN struct thread *a_td;
1456 };
1457 */
1458 {
1459 	struct inode *ip;
1460 	struct fs *fs;
1461 	uint32_t ealength, ul;
1462 	int ealen, olen, eapad1, eapad2, error, i, easize;
1463 	u_char *eae, *p;
1464 
1465 	ip = VTOI(ap->a_vp);
1466 	fs = ITOFS(ip);
1467 
1468 	if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1469 		return (EOPNOTSUPP);
1470 
1471 	if (strlen(ap->a_name) == 0)
1472 		return (EINVAL);
1473 
1474 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1475 		return (EROFS);
1476 
1477 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1478 	    ap->a_cred, ap->a_td, VWRITE);
1479 	if (error) {
1480 
1481 		/*
1482 		 * ffs_lock_ea is not needed there, because the vnode
1483 		 * must be exclusively locked.
1484 		 */
1485 		if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1486 			ip->i_ea_error = error;
1487 		return (error);
1488 	}
1489 
1490 	error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1491 	if (error)
1492 		return (error);
1493 
1494 	ealength = eapad1 = ealen = eapad2 = 0;
1495 
1496 	eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1497 	bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1498 	easize = ip->i_ea_len;
1499 
1500 	olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1501 	    &p, NULL);
1502 	if (olen == -1) {
1503 		/* delete but nonexistent */
1504 		free(eae, M_TEMP);
1505 		ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1506 		return(ENOATTR);
1507 	}
1508 	bcopy(p, &ul, sizeof ul);
1509 	i = p - eae + ul;
1510 	if (ul != ealength) {
1511 		bcopy(p + ul, p + ealength, easize - i);
1512 		easize += (ealength - ul);
1513 	}
1514 	if (easize > NXADDR * fs->fs_bsize) {
1515 		free(eae, M_TEMP);
1516 		ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1517 		if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1518 			ip->i_ea_error = ENOSPC;
1519 		return(ENOSPC);
1520 	}
1521 	p = ip->i_ea_area;
1522 	ip->i_ea_area = eae;
1523 	ip->i_ea_len = easize;
1524 	free(p, M_TEMP);
1525 	error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1526 	return(error);
1527 }
1528 
1529 /*
1530  * Vnode operation to retrieve a named extended attribute.
1531  */
1532 static int
1533 ffs_getextattr(struct vop_getextattr_args *ap)
1534 /*
1535 vop_getextattr {
1536 	IN struct vnode *a_vp;
1537 	IN int a_attrnamespace;
1538 	IN const char *a_name;
1539 	INOUT struct uio *a_uio;
1540 	OUT size_t *a_size;
1541 	IN struct ucred *a_cred;
1542 	IN struct thread *a_td;
1543 };
1544 */
1545 {
1546 	struct inode *ip;
1547 	u_char *eae, *p;
1548 	unsigned easize;
1549 	int error, ealen;
1550 
1551 	ip = VTOI(ap->a_vp);
1552 
1553 	if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1554 		return (EOPNOTSUPP);
1555 
1556 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1557 	    ap->a_cred, ap->a_td, VREAD);
1558 	if (error)
1559 		return (error);
1560 
1561 	error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1562 	if (error)
1563 		return (error);
1564 
1565 	eae = ip->i_ea_area;
1566 	easize = ip->i_ea_len;
1567 
1568 	ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1569 	    NULL, &p);
1570 	if (ealen >= 0) {
1571 		error = 0;
1572 		if (ap->a_size != NULL)
1573 			*ap->a_size = ealen;
1574 		else if (ap->a_uio != NULL)
1575 			error = uiomove(p, ealen, ap->a_uio);
1576 	} else
1577 		error = ENOATTR;
1578 
1579 	ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1580 	return(error);
1581 }
1582 
1583 /*
1584  * Vnode operation to retrieve extended attributes on a vnode.
1585  */
1586 static int
1587 ffs_listextattr(struct vop_listextattr_args *ap)
1588 /*
1589 vop_listextattr {
1590 	IN struct vnode *a_vp;
1591 	IN int a_attrnamespace;
1592 	INOUT struct uio *a_uio;
1593 	OUT size_t *a_size;
1594 	IN struct ucred *a_cred;
1595 	IN struct thread *a_td;
1596 };
1597 */
1598 {
1599 	struct inode *ip;
1600 	u_char *eae, *p, *pe, *pn;
1601 	unsigned easize;
1602 	uint32_t ul;
1603 	int error, ealen;
1604 
1605 	ip = VTOI(ap->a_vp);
1606 
1607 	if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1608 		return (EOPNOTSUPP);
1609 
1610 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1611 	    ap->a_cred, ap->a_td, VREAD);
1612 	if (error)
1613 		return (error);
1614 
1615 	error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1616 	if (error)
1617 		return (error);
1618 	eae = ip->i_ea_area;
1619 	easize = ip->i_ea_len;
1620 
1621 	error = 0;
1622 	if (ap->a_size != NULL)
1623 		*ap->a_size = 0;
1624 	pe = eae + easize;
1625 	for(p = eae; error == 0 && p < pe; p = pn) {
1626 		bcopy(p, &ul, sizeof(ul));
1627 		pn = p + ul;
1628 		if (pn > pe)
1629 			break;
1630 		p += sizeof(ul);
1631 		if (*p++ != ap->a_attrnamespace)
1632 			continue;
1633 		p++;	/* pad2 */
1634 		ealen = *p;
1635 		if (ap->a_size != NULL) {
1636 			*ap->a_size += ealen + 1;
1637 		} else if (ap->a_uio != NULL) {
1638 			error = uiomove(p, ealen + 1, ap->a_uio);
1639 		}
1640 	}
1641 	ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1642 	return(error);
1643 }
1644 
1645 /*
1646  * Vnode operation to set a named attribute.
1647  */
1648 static int
1649 ffs_setextattr(struct vop_setextattr_args *ap)
1650 /*
1651 vop_setextattr {
1652 	IN struct vnode *a_vp;
1653 	IN int a_attrnamespace;
1654 	IN const char *a_name;
1655 	INOUT struct uio *a_uio;
1656 	IN struct ucred *a_cred;
1657 	IN struct thread *a_td;
1658 };
1659 */
1660 {
1661 	struct inode *ip;
1662 	struct fs *fs;
1663 	uint32_t ealength, ul;
1664 	ssize_t ealen;
1665 	int olen, eapad1, eapad2, error, i, easize;
1666 	u_char *eae, *p;
1667 
1668 	ip = VTOI(ap->a_vp);
1669 	fs = ITOFS(ip);
1670 
1671 	if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1672 		return (EOPNOTSUPP);
1673 
1674 	if (strlen(ap->a_name) == 0)
1675 		return (EINVAL);
1676 
1677 	/* XXX Now unsupported API to delete EAs using NULL uio. */
1678 	if (ap->a_uio == NULL)
1679 		return (EOPNOTSUPP);
1680 
1681 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1682 		return (EROFS);
1683 
1684 	ealen = ap->a_uio->uio_resid;
1685 	if (ealen < 0 || ealen > lblktosize(fs, NXADDR))
1686 		return (EINVAL);
1687 
1688 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1689 	    ap->a_cred, ap->a_td, VWRITE);
1690 	if (error) {
1691 
1692 		/*
1693 		 * ffs_lock_ea is not needed there, because the vnode
1694 		 * must be exclusively locked.
1695 		 */
1696 		if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1697 			ip->i_ea_error = error;
1698 		return (error);
1699 	}
1700 
1701 	error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1702 	if (error)
1703 		return (error);
1704 
1705 	ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1706 	eapad1 = 8 - (ealength % 8);
1707 	if (eapad1 == 8)
1708 		eapad1 = 0;
1709 	eapad2 = 8 - (ealen % 8);
1710 	if (eapad2 == 8)
1711 		eapad2 = 0;
1712 	ealength += eapad1 + ealen + eapad2;
1713 
1714 	eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1715 	bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1716 	easize = ip->i_ea_len;
1717 
1718 	olen = ffs_findextattr(eae, easize,
1719 	    ap->a_attrnamespace, ap->a_name, &p, NULL);
1720         if (olen == -1) {
1721 		/* new, append at end */
1722 		p = eae + easize;
1723 		easize += ealength;
1724 	} else {
1725 		bcopy(p, &ul, sizeof ul);
1726 		i = p - eae + ul;
1727 		if (ul != ealength) {
1728 			bcopy(p + ul, p + ealength, easize - i);
1729 			easize += (ealength - ul);
1730 		}
1731 	}
1732 	if (easize > lblktosize(fs, NXADDR)) {
1733 		free(eae, M_TEMP);
1734 		ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1735 		if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1736 			ip->i_ea_error = ENOSPC;
1737 		return(ENOSPC);
1738 	}
1739 	bcopy(&ealength, p, sizeof(ealength));
1740 	p += sizeof(ealength);
1741 	*p++ = ap->a_attrnamespace;
1742 	*p++ = eapad2;
1743 	*p++ = strlen(ap->a_name);
1744 	strcpy(p, ap->a_name);
1745 	p += strlen(ap->a_name);
1746 	bzero(p, eapad1);
1747 	p += eapad1;
1748 	error = uiomove(p, ealen, ap->a_uio);
1749 	if (error) {
1750 		free(eae, M_TEMP);
1751 		ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1752 		if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1753 			ip->i_ea_error = error;
1754 		return(error);
1755 	}
1756 	p += ealen;
1757 	bzero(p, eapad2);
1758 
1759 	p = ip->i_ea_area;
1760 	ip->i_ea_area = eae;
1761 	ip->i_ea_len = easize;
1762 	free(p, M_TEMP);
1763 	error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1764 	return(error);
1765 }
1766 
1767 /*
1768  * Vnode pointer to File handle
1769  */
1770 static int
1771 ffs_vptofh(struct vop_vptofh_args *ap)
1772 /*
1773 vop_vptofh {
1774 	IN struct vnode *a_vp;
1775 	IN struct fid *a_fhp;
1776 };
1777 */
1778 {
1779 	struct inode *ip;
1780 	struct ufid *ufhp;
1781 
1782 	ip = VTOI(ap->a_vp);
1783 	ufhp = (struct ufid *)ap->a_fhp;
1784 	ufhp->ufid_len = sizeof(struct ufid);
1785 	ufhp->ufid_ino = ip->i_number;
1786 	ufhp->ufid_gen = ip->i_gen;
1787 	return (0);
1788 }
1789 
1790 SYSCTL_DECL(_vfs_ffs);
1791 static int use_buf_pager = 1;
1792 SYSCTL_INT(_vfs_ffs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN, &use_buf_pager, 0,
1793     "Always use buffer pager instead of bmap");
1794 static int buf_pager_relbuf;
1795 SYSCTL_INT(_vfs_ffs, OID_AUTO, buf_pager_relbuf, CTLFLAG_RWTUN,
1796     &buf_pager_relbuf, 0,
1797     "Make buffer pager release buffers after reading");
1798 
1799 /*
1800  * The FFS pager.  It uses buffer reads to validate pages.
1801  *
1802  * In contrast to the generic local pager from vm/vnode_pager.c, this
1803  * pager correctly and easily handles volumes where the underlying
1804  * device block size is greater than the machine page size.  The
1805  * buffer cache transparently extends the requested page run to be
1806  * aligned at the block boundary, and does the necessary bogus page
1807  * replacements in the addends to avoid obliterating already valid
1808  * pages.
1809  *
1810  * The only non-trivial issue is that the exclusive busy state for
1811  * pages, which is assumed by the vm_pager_getpages() interface, is
1812  * incompatible with the VMIO buffer cache's desire to share-busy the
1813  * pages.  This function performs a trivial downgrade of the pages'
1814  * state before reading buffers, and a less trivial upgrade from the
1815  * shared-busy to excl-busy state after the read.
1816  */
1817 static int
1818 ffs_getpages(struct vop_getpages_args *ap)
1819 {
1820 	struct vnode *vp;
1821 	vm_page_t *ma, m;
1822 	vm_object_t object;
1823 	struct buf *bp;
1824 	struct ufsmount *um;
1825 	ufs_lbn_t lbn, lbnp;
1826 	vm_ooffset_t la, lb;
1827 	long bsize;
1828 	int bo_bs, count, error, i;
1829 	bool redo, lpart;
1830 
1831 	vp = ap->a_vp;
1832 	ma = ap->a_m;
1833 	count = ap->a_count;
1834 
1835 	um = VFSTOUFS(ap->a_vp->v_mount);
1836 	bo_bs = um->um_devvp->v_bufobj.bo_bsize;
1837 	if (!use_buf_pager && bo_bs <= PAGE_SIZE)
1838 		return (vnode_pager_generic_getpages(vp, ma, count,
1839 		    ap->a_rbehind, ap->a_rahead, NULL, NULL));
1840 
1841 	object = vp->v_object;
1842 	la = IDX_TO_OFF(ma[count - 1]->pindex);
1843 	if (la >= object->un_pager.vnp.vnp_size)
1844 		return (VM_PAGER_BAD);
1845 	lpart = la + PAGE_SIZE > object->un_pager.vnp.vnp_size;
1846 	if (ap->a_rbehind != NULL) {
1847 		lb = IDX_TO_OFF(ma[0]->pindex);
1848 		*ap->a_rbehind = OFF_TO_IDX(lb - rounddown2(lb, bo_bs));
1849 	}
1850 	if (ap->a_rahead != NULL) {
1851 		*ap->a_rahead = OFF_TO_IDX(roundup2(la, bo_bs) - la);
1852 		if (la + IDX_TO_OFF(*ap->a_rahead) >=
1853 		    object->un_pager.vnp.vnp_size) {
1854 			*ap->a_rahead = OFF_TO_IDX(roundup2(object->un_pager.
1855 			    vnp.vnp_size, PAGE_SIZE) - la);
1856 		}
1857 	}
1858 	VM_OBJECT_WLOCK(object);
1859 again:
1860 	for (i = 0; i < count; i++)
1861 		vm_page_busy_downgrade(ma[i]);
1862 	VM_OBJECT_WUNLOCK(object);
1863 
1864 	lbnp = -1;
1865 	for (i = 0; i < count; i++) {
1866 		m = ma[i];
1867 
1868 		/*
1869 		 * Pages are shared busy and the object lock is not
1870 		 * owned, which together allow for the pages'
1871 		 * invalidation.  The racy test for validity avoids
1872 		 * useless creation of the buffer for the most typical
1873 		 * case when invalidation is not used in redo or for
1874 		 * parallel read.  The shared->excl upgrade loop at
1875 		 * the end of the function catches the race in a
1876 		 * reliable way (protected by the object lock).
1877 		 */
1878 		if (m->valid == VM_PAGE_BITS_ALL)
1879 			continue;
1880 
1881 		lbn = lblkno(um->um_fs, IDX_TO_OFF(m->pindex));
1882 		if (lbn != lbnp) {
1883 			bsize = blksize(um->um_fs, VTOI(vp), lbn);
1884 			error = bread_gb(vp, lbn, bsize, NOCRED, GB_UNMAPPED,
1885 			    &bp);
1886 			if (error != 0)
1887 				break;
1888 			KASSERT(1 /* racy, enable for debugging */ ||
1889 			    m->valid == VM_PAGE_BITS_ALL || i == count - 1,
1890 			    ("buf %d %p invalid", i, m));
1891 			if (i == count - 1 && lpart) {
1892 				VM_OBJECT_WLOCK(object);
1893 				if (m->valid != 0 &&
1894 				    m->valid != VM_PAGE_BITS_ALL)
1895 					vm_page_zero_invalid(m, TRUE);
1896 				VM_OBJECT_WUNLOCK(object);
1897 			}
1898 			if (LIST_EMPTY(&bp->b_dep)) {
1899 				/*
1900 				 * Invalidation clears m->valid, but
1901 				 * may leave B_CACHE flag if the
1902 				 * buffer existed at the invalidation
1903 				 * time.  In this case, recycle the
1904 				 * buffer to do real read on next
1905 				 * bread() after redo.
1906 				 *
1907 				 * Otherwise B_RELBUF is not strictly
1908 				 * necessary, enable to reduce buf
1909 				 * cache pressure.
1910 				 */
1911 				if (buf_pager_relbuf ||
1912 				    m->valid != VM_PAGE_BITS_ALL)
1913 					bp->b_flags |= B_RELBUF;
1914 
1915 				bp->b_flags &= ~B_NOCACHE;
1916 				brelse(bp);
1917 			} else {
1918 				bqrelse(bp);
1919 			}
1920 			lbnp = lbn;
1921 		}
1922 	}
1923 
1924 	VM_OBJECT_WLOCK(object);
1925 	redo = false;
1926 	for (i = 0; i < count; i++) {
1927 		vm_page_sunbusy(ma[i]);
1928 		ma[i] = vm_page_grab(object, ma[i]->pindex, VM_ALLOC_NORMAL);
1929 
1930 		/*
1931 		 * Since the pages were only sbusy while neither the
1932 		 * buffer nor the object lock was held by us, or
1933 		 * reallocated while vm_page_grab() slept for busy
1934 		 * relinguish, they could have been invalidated.
1935 		 * Recheck the valid bits and re-read as needed.
1936 		 *
1937 		 * Note that the last page is made fully valid in the
1938 		 * read loop, and partial validity for the page at
1939 		 * index count - 1 could mean that the page was
1940 		 * invalidated or removed, so we must restart for
1941 		 * safety as well.
1942 		 */
1943 		if (ma[i]->valid != VM_PAGE_BITS_ALL)
1944 			redo = true;
1945 	}
1946 	if (redo && error == 0)
1947 		goto again;
1948 	VM_OBJECT_WUNLOCK(object);
1949 	return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1950 }
1951