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