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