xref: /freebsd/sys/ufs/ffs/ffs_vnops.c (revision 16038816)
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 	struct ufs2_dinode *dp;
1403 	int error;
1404 
1405 	ip = VTOI(vp);
1406 
1407 	ffs_lock_ea(vp);
1408 	if (ip->i_ea_area != NULL) {
1409 		ip->i_ea_refs++;
1410 		ffs_unlock_ea(vp);
1411 		return (0);
1412 	}
1413 	dp = ip->i_din2;
1414 	error = ffs_rdextattr(&ip->i_ea_area, vp, td);
1415 	if (error) {
1416 		ffs_unlock_ea(vp);
1417 		return (error);
1418 	}
1419 	ip->i_ea_error = 0;
1420 	ip->i_ea_refs++;
1421 	ffs_unlock_ea(vp);
1422 	return (0);
1423 }
1424 
1425 /*
1426  * Vnode extattr transaction commit/abort
1427  */
1428 static int
1429 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1430 {
1431 	struct inode *ip;
1432 	struct uio luio;
1433 	struct iovec *liovec;
1434 	struct ufs2_dinode *dp;
1435 	size_t ea_len, tlen;
1436 	int error, i, lcnt;
1437 	bool truncate;
1438 
1439 	ip = VTOI(vp);
1440 
1441 	ffs_lock_ea(vp);
1442 	if (ip->i_ea_area == NULL) {
1443 		ffs_unlock_ea(vp);
1444 		return (EINVAL);
1445 	}
1446 	dp = ip->i_din2;
1447 	error = ip->i_ea_error;
1448 	truncate = false;
1449 	if (commit && error == 0) {
1450 		ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1451 		if (cred == NOCRED)
1452 			cred =  vp->v_mount->mnt_cred;
1453 
1454 		ea_len = MAX(ip->i_ea_len, dp->di_extsize);
1455 		for (lcnt = 1, tlen = ea_len - ip->i_ea_len; tlen > 0;) {
1456 			tlen -= MIN(ZERO_REGION_SIZE, tlen);
1457 			lcnt++;
1458 		}
1459 
1460 		liovec = __builtin_alloca(lcnt * sizeof(struct iovec));
1461 		luio.uio_iovcnt = lcnt;
1462 
1463 		liovec[0].iov_base = ip->i_ea_area;
1464 		liovec[0].iov_len = ip->i_ea_len;
1465 		for (i = 1, tlen = ea_len - ip->i_ea_len; i < lcnt; i++) {
1466 			liovec[i].iov_base = __DECONST(void *, zero_region);
1467 			liovec[i].iov_len = MIN(ZERO_REGION_SIZE, tlen);
1468 			tlen -= liovec[i].iov_len;
1469 		}
1470 		MPASS(tlen == 0);
1471 
1472 		luio.uio_iov = liovec;
1473 		luio.uio_offset = 0;
1474 		luio.uio_resid = ea_len;
1475 		luio.uio_segflg = UIO_SYSSPACE;
1476 		luio.uio_rw = UIO_WRITE;
1477 		luio.uio_td = td;
1478 		error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1479 		if (error == 0 && ip->i_ea_len == 0)
1480 			truncate = true;
1481 	}
1482 	if (--ip->i_ea_refs == 0) {
1483 		free(ip->i_ea_area, M_TEMP);
1484 		ip->i_ea_area = NULL;
1485 		ip->i_ea_len = 0;
1486 		ip->i_ea_error = 0;
1487 	}
1488 	ffs_unlock_ea(vp);
1489 
1490 	if (truncate)
1491 		ffs_truncate(vp, 0, IO_EXT, cred);
1492 	return (error);
1493 }
1494 
1495 /*
1496  * Vnode extattr strategy routine for fifos.
1497  *
1498  * We need to check for a read or write of the external attributes.
1499  * Otherwise we just fall through and do the usual thing.
1500  */
1501 static int
1502 ffsext_strategy(struct vop_strategy_args *ap)
1503 /*
1504 struct vop_strategy_args {
1505 	struct vnodeop_desc *a_desc;
1506 	struct vnode *a_vp;
1507 	struct buf *a_bp;
1508 };
1509 */
1510 {
1511 	struct vnode *vp;
1512 	daddr_t lbn;
1513 
1514 	vp = ap->a_vp;
1515 	lbn = ap->a_bp->b_lblkno;
1516 	if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -UFS_NXADDR)
1517 		return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1518 	if (vp->v_type == VFIFO)
1519 		return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1520 	panic("spec nodes went here");
1521 }
1522 
1523 /*
1524  * Vnode extattr transaction commit/abort
1525  */
1526 static int
1527 ffs_openextattr(struct vop_openextattr_args *ap)
1528 /*
1529 struct vop_openextattr_args {
1530 	struct vnodeop_desc *a_desc;
1531 	struct vnode *a_vp;
1532 	IN struct ucred *a_cred;
1533 	IN struct thread *a_td;
1534 };
1535 */
1536 {
1537 
1538 	if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1539 		return (EOPNOTSUPP);
1540 
1541 	return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1542 }
1543 
1544 /*
1545  * Vnode extattr transaction commit/abort
1546  */
1547 static int
1548 ffs_closeextattr(struct vop_closeextattr_args *ap)
1549 /*
1550 struct vop_closeextattr_args {
1551 	struct vnodeop_desc *a_desc;
1552 	struct vnode *a_vp;
1553 	int a_commit;
1554 	IN struct ucred *a_cred;
1555 	IN struct thread *a_td;
1556 };
1557 */
1558 {
1559 	struct vnode *vp;
1560 
1561 	vp = ap->a_vp;
1562 	if (vp->v_type == VCHR || vp->v_type == VBLK)
1563 		return (EOPNOTSUPP);
1564 	if (ap->a_commit && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0)
1565 		return (EROFS);
1566 
1567 	if (ap->a_commit && DOINGSUJ(vp)) {
1568 		ASSERT_VOP_ELOCKED(vp, "ffs_closeextattr commit");
1569 		softdep_prealloc(vp, MNT_WAIT);
1570 		if (vp->v_data == NULL)
1571 			return (EBADF);
1572 	}
1573 	return (ffs_close_ea(vp, ap->a_commit, ap->a_cred, ap->a_td));
1574 }
1575 
1576 /*
1577  * Vnode operation to remove a named attribute.
1578  */
1579 static int
1580 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1581 /*
1582 vop_deleteextattr {
1583 	IN struct vnode *a_vp;
1584 	IN int a_attrnamespace;
1585 	IN const char *a_name;
1586 	IN struct ucred *a_cred;
1587 	IN struct thread *a_td;
1588 };
1589 */
1590 {
1591 	struct vnode *vp;
1592 	struct inode *ip;
1593 	struct extattr *eap;
1594 	uint32_t ul;
1595 	int olen, error, i, easize;
1596 	u_char *eae;
1597 	void *tmp;
1598 
1599 	vp = ap->a_vp;
1600 	ip = VTOI(vp);
1601 
1602 	if (vp->v_type == VCHR || vp->v_type == VBLK)
1603 		return (EOPNOTSUPP);
1604 	if (strlen(ap->a_name) == 0)
1605 		return (EINVAL);
1606 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1607 		return (EROFS);
1608 
1609 	error = extattr_check_cred(vp, ap->a_attrnamespace,
1610 	    ap->a_cred, ap->a_td, VWRITE);
1611 	if (error) {
1612 		/*
1613 		 * ffs_lock_ea is not needed there, because the vnode
1614 		 * must be exclusively locked.
1615 		 */
1616 		if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1617 			ip->i_ea_error = error;
1618 		return (error);
1619 	}
1620 
1621 	if (DOINGSUJ(vp)) {
1622 		ASSERT_VOP_ELOCKED(vp, "ffs_deleteextattr");
1623 		softdep_prealloc(vp, MNT_WAIT);
1624 		if (vp->v_data == NULL)
1625 			return (EBADF);
1626 	}
1627 
1628 	error = ffs_open_ea(vp, ap->a_cred, ap->a_td);
1629 	if (error)
1630 		return (error);
1631 
1632 	/* CEM: delete could be done in-place instead */
1633 	eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1634 	bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1635 	easize = ip->i_ea_len;
1636 
1637 	olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1638 	    &eap, NULL);
1639 	if (olen == -1) {
1640 		/* delete but nonexistent */
1641 		free(eae, M_TEMP);
1642 		ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1643 		return (ENOATTR);
1644 	}
1645 	ul = eap->ea_length;
1646 	i = (u_char *)EXTATTR_NEXT(eap) - eae;
1647 	bcopy(EXTATTR_NEXT(eap), eap, easize - i);
1648 	easize -= ul;
1649 
1650 	tmp = ip->i_ea_area;
1651 	ip->i_ea_area = eae;
1652 	ip->i_ea_len = easize;
1653 	free(tmp, M_TEMP);
1654 	error = ffs_close_ea(vp, 1, ap->a_cred, ap->a_td);
1655 	return (error);
1656 }
1657 
1658 /*
1659  * Vnode operation to retrieve a named extended attribute.
1660  */
1661 static int
1662 ffs_getextattr(struct vop_getextattr_args *ap)
1663 /*
1664 vop_getextattr {
1665 	IN struct vnode *a_vp;
1666 	IN int a_attrnamespace;
1667 	IN const char *a_name;
1668 	INOUT struct uio *a_uio;
1669 	OUT size_t *a_size;
1670 	IN struct ucred *a_cred;
1671 	IN struct thread *a_td;
1672 };
1673 */
1674 {
1675 	struct inode *ip;
1676 	u_char *eae, *p;
1677 	unsigned easize;
1678 	int error, ealen;
1679 
1680 	ip = VTOI(ap->a_vp);
1681 
1682 	if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1683 		return (EOPNOTSUPP);
1684 
1685 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1686 	    ap->a_cred, ap->a_td, VREAD);
1687 	if (error)
1688 		return (error);
1689 
1690 	error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1691 	if (error)
1692 		return (error);
1693 
1694 	eae = ip->i_ea_area;
1695 	easize = ip->i_ea_len;
1696 
1697 	ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1698 	    NULL, &p);
1699 	if (ealen >= 0) {
1700 		error = 0;
1701 		if (ap->a_size != NULL)
1702 			*ap->a_size = ealen;
1703 		else if (ap->a_uio != NULL)
1704 			error = uiomove(p, ealen, ap->a_uio);
1705 	} else
1706 		error = ENOATTR;
1707 
1708 	ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1709 	return (error);
1710 }
1711 
1712 /*
1713  * Vnode operation to retrieve extended attributes on a vnode.
1714  */
1715 static int
1716 ffs_listextattr(struct vop_listextattr_args *ap)
1717 /*
1718 vop_listextattr {
1719 	IN struct vnode *a_vp;
1720 	IN int a_attrnamespace;
1721 	INOUT struct uio *a_uio;
1722 	OUT size_t *a_size;
1723 	IN struct ucred *a_cred;
1724 	IN struct thread *a_td;
1725 };
1726 */
1727 {
1728 	struct inode *ip;
1729 	struct extattr *eap, *eaend;
1730 	int error, ealen;
1731 
1732 	ip = VTOI(ap->a_vp);
1733 
1734 	if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1735 		return (EOPNOTSUPP);
1736 
1737 	error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1738 	    ap->a_cred, ap->a_td, VREAD);
1739 	if (error)
1740 		return (error);
1741 
1742 	error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1743 	if (error)
1744 		return (error);
1745 
1746 	error = 0;
1747 	if (ap->a_size != NULL)
1748 		*ap->a_size = 0;
1749 
1750 	KASSERT(ALIGNED_TO(ip->i_ea_area, struct extattr), ("unaligned"));
1751 	eap = (struct extattr *)ip->i_ea_area;
1752 	eaend = (struct extattr *)(ip->i_ea_area + ip->i_ea_len);
1753 	for (; error == 0 && eap < eaend; eap = EXTATTR_NEXT(eap)) {
1754 		KASSERT(EXTATTR_NEXT(eap) <= eaend,
1755 		    ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1756 		if (eap->ea_namespace != ap->a_attrnamespace)
1757 			continue;
1758 
1759 		ealen = eap->ea_namelength;
1760 		if (ap->a_size != NULL)
1761 			*ap->a_size += ealen + 1;
1762 		else if (ap->a_uio != NULL)
1763 			error = uiomove(&eap->ea_namelength, ealen + 1,
1764 			    ap->a_uio);
1765 	}
1766 
1767 	ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1768 	return (error);
1769 }
1770 
1771 /*
1772  * Vnode operation to set a named attribute.
1773  */
1774 static int
1775 ffs_setextattr(struct vop_setextattr_args *ap)
1776 /*
1777 vop_setextattr {
1778 	IN struct vnode *a_vp;
1779 	IN int a_attrnamespace;
1780 	IN const char *a_name;
1781 	INOUT struct uio *a_uio;
1782 	IN struct ucred *a_cred;
1783 	IN struct thread *a_td;
1784 };
1785 */
1786 {
1787 	struct vnode *vp;
1788 	struct inode *ip;
1789 	struct fs *fs;
1790 	struct extattr *eap;
1791 	uint32_t ealength, ul;
1792 	ssize_t ealen;
1793 	int olen, eapad1, eapad2, error, i, easize;
1794 	u_char *eae;
1795 	void *tmp;
1796 
1797 	vp = ap->a_vp;
1798 	ip = VTOI(vp);
1799 	fs = ITOFS(ip);
1800 
1801 	if (vp->v_type == VCHR || vp->v_type == VBLK)
1802 		return (EOPNOTSUPP);
1803 	if (strlen(ap->a_name) == 0)
1804 		return (EINVAL);
1805 
1806 	/* XXX Now unsupported API to delete EAs using NULL uio. */
1807 	if (ap->a_uio == NULL)
1808 		return (EOPNOTSUPP);
1809 
1810 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1811 		return (EROFS);
1812 
1813 	ealen = ap->a_uio->uio_resid;
1814 	if (ealen < 0 || ealen > lblktosize(fs, UFS_NXADDR))
1815 		return (EINVAL);
1816 
1817 	error = extattr_check_cred(vp, ap->a_attrnamespace,
1818 	    ap->a_cred, ap->a_td, VWRITE);
1819 	if (error) {
1820 		/*
1821 		 * ffs_lock_ea is not needed there, because the vnode
1822 		 * must be exclusively locked.
1823 		 */
1824 		if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1825 			ip->i_ea_error = error;
1826 		return (error);
1827 	}
1828 
1829 	if (DOINGSUJ(vp)) {
1830 		ASSERT_VOP_ELOCKED(vp, "ffs_deleteextattr");
1831 		softdep_prealloc(vp, MNT_WAIT);
1832 		if (vp->v_data == NULL)
1833 			return (EBADF);
1834 	}
1835 
1836 	error = ffs_open_ea(vp, ap->a_cred, ap->a_td);
1837 	if (error)
1838 		return (error);
1839 
1840 	ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1841 	eapad1 = roundup2(ealength, 8) - ealength;
1842 	eapad2 = roundup2(ealen, 8) - ealen;
1843 	ealength += eapad1 + ealen + eapad2;
1844 
1845 	/*
1846 	 * CEM: rewrites of the same size or smaller could be done in-place
1847 	 * instead.  (We don't acquire any fine-grained locks in here either,
1848 	 * so we could also do bigger writes in-place.)
1849 	 */
1850 	eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1851 	bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1852 	easize = ip->i_ea_len;
1853 
1854 	olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1855 	    &eap, NULL);
1856         if (olen == -1) {
1857 		/* new, append at end */
1858 		KASSERT(ALIGNED_TO(eae + easize, struct extattr),
1859 		    ("unaligned"));
1860 		eap = (struct extattr *)(eae + easize);
1861 		easize += ealength;
1862 	} else {
1863 		ul = eap->ea_length;
1864 		i = (u_char *)EXTATTR_NEXT(eap) - eae;
1865 		if (ul != ealength) {
1866 			bcopy(EXTATTR_NEXT(eap), (u_char *)eap + ealength,
1867 			    easize - i);
1868 			easize += (ealength - ul);
1869 		}
1870 	}
1871 	if (easize > lblktosize(fs, UFS_NXADDR)) {
1872 		free(eae, M_TEMP);
1873 		ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1874 		if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1875 			ip->i_ea_error = ENOSPC;
1876 		return (ENOSPC);
1877 	}
1878 	eap->ea_length = ealength;
1879 	eap->ea_namespace = ap->a_attrnamespace;
1880 	eap->ea_contentpadlen = eapad2;
1881 	eap->ea_namelength = strlen(ap->a_name);
1882 	memcpy(eap->ea_name, ap->a_name, strlen(ap->a_name));
1883 	bzero(&eap->ea_name[strlen(ap->a_name)], eapad1);
1884 	error = uiomove(EXTATTR_CONTENT(eap), ealen, ap->a_uio);
1885 	if (error) {
1886 		free(eae, M_TEMP);
1887 		ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1888 		if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1889 			ip->i_ea_error = error;
1890 		return (error);
1891 	}
1892 	bzero((u_char *)EXTATTR_CONTENT(eap) + ealen, eapad2);
1893 
1894 	tmp = ip->i_ea_area;
1895 	ip->i_ea_area = eae;
1896 	ip->i_ea_len = easize;
1897 	free(tmp, M_TEMP);
1898 	error = ffs_close_ea(vp, 1, ap->a_cred, ap->a_td);
1899 	return (error);
1900 }
1901 
1902 /*
1903  * Vnode pointer to File handle
1904  */
1905 static int
1906 ffs_vptofh(struct vop_vptofh_args *ap)
1907 /*
1908 vop_vptofh {
1909 	IN struct vnode *a_vp;
1910 	IN struct fid *a_fhp;
1911 };
1912 */
1913 {
1914 	struct inode *ip;
1915 	struct ufid *ufhp;
1916 
1917 	ip = VTOI(ap->a_vp);
1918 	ufhp = (struct ufid *)ap->a_fhp;
1919 	ufhp->ufid_len = sizeof(struct ufid);
1920 	ufhp->ufid_ino = ip->i_number;
1921 	ufhp->ufid_gen = ip->i_gen;
1922 	return (0);
1923 }
1924 
1925 SYSCTL_DECL(_vfs_ffs);
1926 static int use_buf_pager = 1;
1927 SYSCTL_INT(_vfs_ffs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN, &use_buf_pager, 0,
1928     "Always use buffer pager instead of bmap");
1929 
1930 static daddr_t
1931 ffs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1932 {
1933 
1934 	return (lblkno(VFSTOUFS(vp->v_mount)->um_fs, off));
1935 }
1936 
1937 static int
1938 ffs_gbp_getblksz(struct vnode *vp, daddr_t lbn)
1939 {
1940 
1941 	return (blksize(VFSTOUFS(vp->v_mount)->um_fs, VTOI(vp), lbn));
1942 }
1943 
1944 static int
1945 ffs_getpages(struct vop_getpages_args *ap)
1946 {
1947 	struct vnode *vp;
1948 	struct ufsmount *um;
1949 
1950 	vp = ap->a_vp;
1951 	um = VFSTOUFS(vp->v_mount);
1952 
1953 	if (!use_buf_pager && um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE)
1954 		return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1955 		    ap->a_rbehind, ap->a_rahead, NULL, NULL));
1956 	return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
1957 	    ap->a_rahead, ffs_gbp_getblkno, ffs_gbp_getblksz));
1958 }
1959 
1960 static int
1961 ffs_getpages_async(struct vop_getpages_async_args *ap)
1962 {
1963 	struct vnode *vp;
1964 	struct ufsmount *um;
1965 	bool do_iodone;
1966 	int error;
1967 
1968 	vp = ap->a_vp;
1969 	um = VFSTOUFS(vp->v_mount);
1970 	do_iodone = true;
1971 
1972 	if (um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE) {
1973 		error = vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1974 		    ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg);
1975 		if (error == 0)
1976 			do_iodone = false;
1977 	} else {
1978 		error = vfs_bio_getpages(vp, ap->a_m, ap->a_count,
1979 		    ap->a_rbehind, ap->a_rahead, ffs_gbp_getblkno,
1980 		    ffs_gbp_getblksz);
1981 	}
1982 	if (do_iodone && ap->a_iodone != NULL)
1983 		ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
1984 
1985 	return (error);
1986 }
1987 
1988 static int
1989 ffs_vput_pair(struct vop_vput_pair_args *ap)
1990 {
1991 	struct mount *mp;
1992 	struct vnode *dvp, *vp, *vp1, **vpp;
1993 	struct inode *dp, *ip;
1994 	ino_t ip_ino;
1995 	u_int64_t ip_gen;
1996 	off_t old_size;
1997 	int error, vp_locked;
1998 
1999 	dvp = ap->a_dvp;
2000 	dp = VTOI(dvp);
2001 	vpp = ap->a_vpp;
2002 	vp = vpp != NULL ? *vpp : NULL;
2003 
2004 	if ((dp->i_flag & (IN_NEEDSYNC | IN_ENDOFF)) == 0) {
2005 		vput(dvp);
2006 		if (vp != NULL && ap->a_unlock_vp)
2007 			vput(vp);
2008 		return (0);
2009 	}
2010 
2011 	mp = dvp->v_mount;
2012 	if (vp != NULL) {
2013 		if (ap->a_unlock_vp) {
2014 			vput(vp);
2015 		} else {
2016 			MPASS(vp->v_type != VNON);
2017 			vp_locked = VOP_ISLOCKED(vp);
2018 			ip = VTOI(vp);
2019 			ip_ino = ip->i_number;
2020 			ip_gen = ip->i_gen;
2021 			VOP_UNLOCK(vp);
2022 		}
2023 	}
2024 
2025 	/*
2026 	 * If compaction or fsync was requested do it in ffs_vput_pair()
2027 	 * now that other locks are no longer held.
2028          */
2029 	if ((dp->i_flag & IN_ENDOFF) != 0) {
2030 		VNASSERT(I_ENDOFF(dp) != 0 && I_ENDOFF(dp) < dp->i_size, dvp,
2031 		    ("IN_ENDOFF set but I_ENDOFF() is not"));
2032 		dp->i_flag &= ~IN_ENDOFF;
2033 		old_size = dp->i_size;
2034 		error = UFS_TRUNCATE(dvp, (off_t)I_ENDOFF(dp), IO_NORMAL |
2035 		    (DOINGASYNC(dvp) ? 0 : IO_SYNC), curthread->td_ucred);
2036 		if (error != 0 && error != ERELOOKUP) {
2037 			if (!ffs_fsfail_cleanup(VFSTOUFS(mp), error)) {
2038 				vn_printf(dvp,
2039 				    "IN_ENDOFF: failed to truncate, "
2040 				    "error %d\n", error);
2041 			}
2042 #ifdef UFS_DIRHASH
2043 			ufsdirhash_free(dp);
2044 #endif
2045 		}
2046 		SET_I_ENDOFF(dp, 0);
2047 	}
2048 	if ((dp->i_flag & IN_NEEDSYNC) != 0) {
2049 		do {
2050 			error = ffs_syncvnode(dvp, MNT_WAIT, 0);
2051 		} while (error == ERELOOKUP);
2052 	}
2053 
2054 	vput(dvp);
2055 
2056 	if (vp == NULL || ap->a_unlock_vp)
2057 		return (0);
2058 	MPASS(mp != NULL);
2059 
2060 	/*
2061 	 * It is possible that vp is reclaimed at this point. Only
2062 	 * routines that call us with a_unlock_vp == false can find
2063 	 * that their vp has been reclaimed. There are three areas
2064 	 * that are affected:
2065 	 * 1) vn_open_cred() - later VOPs could fail, but
2066 	 *    dead_open() returns 0 to simulate successful open.
2067 	 * 2) ffs_snapshot() - creation of snapshot fails with EBADF.
2068 	 * 3) NFS server (several places) - code is prepared to detect
2069 	 *    and respond to dead vnodes by returning ESTALE.
2070 	 */
2071 	VOP_LOCK(vp, vp_locked | LK_RETRY);
2072 	if (!VN_IS_DOOMED(vp))
2073 		return (0);
2074 
2075 	/*
2076 	 * Try harder to recover from reclaimed vp if reclaim was not
2077 	 * because underlying inode was cleared.  We saved inode
2078 	 * number and inode generation, so we can try to reinstantiate
2079 	 * exactly same version of inode.  If this fails, return
2080 	 * original doomed vnode and let caller to handle
2081 	 * consequences.
2082 	 *
2083 	 * Note that callers must keep write started around
2084 	 * VOP_VPUT_PAIR() calls, so it is safe to use mp without
2085 	 * busying it.
2086 	 */
2087 	VOP_UNLOCK(vp);
2088 	error = ffs_inotovp(mp, ip_ino, ip_gen, LK_EXCLUSIVE, &vp1,
2089 	    FFSV_REPLACE_DOOMED);
2090 	if (error != 0) {
2091 		VOP_LOCK(vp, vp_locked | LK_RETRY);
2092 	} else {
2093 		vrele(vp);
2094 		*vpp = vp1;
2095 	}
2096 	return (error);
2097 }
2098