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