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