xref: /freebsd/sys/ufs/ffs/ffs_vnops.c (revision 4b9d6057)
1 /*-
2  * SPDX-License-Identifier: (BSD-2-Clause AND BSD-3-Clause)
3  *
4  * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * This software was developed for the FreeBSD Project by Marshall
8  * Kirk McKusick and Network Associates Laboratories, the Security
9  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11  * research program
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * Copyright (c) 1982, 1986, 1989, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ...
61  */
62 
63 #include <sys/cdefs.h>
64 #include "opt_directio.h"
65 #include "opt_ffs.h"
66 #include "opt_ufs.h"
67 
68 #include <sys/param.h>
69 #include <sys/bio.h>
70 #include <sys/systm.h>
71 #include <sys/buf.h>
72 #include <sys/conf.h>
73 #include <sys/extattr.h>
74 #include <sys/kernel.h>
75 #include <sys/limits.h>
76 #include <sys/malloc.h>
77 #include <sys/mount.h>
78 #include <sys/priv.h>
79 #include <sys/rwlock.h>
80 #include <sys/stat.h>
81 #include <sys/sysctl.h>
82 #include <sys/vmmeter.h>
83 #include <sys/vnode.h>
84 
85 #include <vm/vm.h>
86 #include <vm/vm_param.h>
87 #include <vm/vm_extern.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_pager.h>
91 #include <vm/vnode_pager.h>
92 
93 #include <ufs/ufs/extattr.h>
94 #include <ufs/ufs/quota.h>
95 #include <ufs/ufs/inode.h>
96 #include <ufs/ufs/ufs_extern.h>
97 #include <ufs/ufs/ufsmount.h>
98 #include <ufs/ufs/dir.h>
99 #ifdef UFS_DIRHASH
100 #include <ufs/ufs/dirhash.h>
101 #endif
102 
103 #include <ufs/ffs/fs.h>
104 #include <ufs/ffs/ffs_extern.h>
105 
106 #define	ALIGNED_TO(ptr, s)	\
107 	(((uintptr_t)(ptr) & (_Alignof(s) - 1)) == 0)
108 
109 #ifdef DIRECTIO
110 extern int	ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
111 #endif
112 static vop_fdatasync_t	ffs_fdatasync;
113 static vop_fsync_t	ffs_fsync;
114 static vop_getpages_t	ffs_getpages;
115 static vop_getpages_async_t	ffs_getpages_async;
116 static vop_lock1_t	ffs_lock;
117 #ifdef INVARIANTS
118 static vop_unlock_t	ffs_unlock_debug;
119 #endif
120 static vop_read_t	ffs_read;
121 static vop_write_t	ffs_write;
122 static int	ffs_extread(struct vnode *vp, struct uio *uio, int ioflag);
123 static int	ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag,
124 		    struct ucred *cred);
125 static vop_strategy_t	ffsext_strategy;
126 static vop_closeextattr_t	ffs_closeextattr;
127 static vop_deleteextattr_t	ffs_deleteextattr;
128 static vop_getextattr_t	ffs_getextattr;
129 static vop_listextattr_t	ffs_listextattr;
130 static vop_openextattr_t	ffs_openextattr;
131 static vop_setextattr_t	ffs_setextattr;
132 static vop_vptofh_t	ffs_vptofh;
133 static vop_vput_pair_t	ffs_vput_pair;
134 
135 vop_fplookup_vexec_t ufs_fplookup_vexec;
136 
137 /* Global vfs data structures for ufs. */
138 struct vop_vector ffs_vnodeops1 = {
139 	.vop_default =		&ufs_vnodeops,
140 	.vop_fsync =		ffs_fsync,
141 	.vop_fdatasync =	ffs_fdatasync,
142 	.vop_getpages =		ffs_getpages,
143 	.vop_getpages_async =	ffs_getpages_async,
144 	.vop_lock1 =		ffs_lock,
145 #ifdef INVARIANTS
146 	.vop_unlock =		ffs_unlock_debug,
147 #endif
148 	.vop_read =		ffs_read,
149 	.vop_reallocblks =	ffs_reallocblks,
150 	.vop_write =		ffs_write,
151 	.vop_vptofh =		ffs_vptofh,
152 	.vop_vput_pair =	ffs_vput_pair,
153 	.vop_fplookup_vexec =	ufs_fplookup_vexec,
154 	.vop_fplookup_symlink =	VOP_EAGAIN,
155 };
156 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops1);
157 
158 struct vop_vector ffs_fifoops1 = {
159 	.vop_default =		&ufs_fifoops,
160 	.vop_fsync =		ffs_fsync,
161 	.vop_fdatasync =	ffs_fdatasync,
162 	.vop_lock1 =		ffs_lock,
163 #ifdef INVARIANTS
164 	.vop_unlock =		ffs_unlock_debug,
165 #endif
166 	.vop_vptofh =		ffs_vptofh,
167 	.vop_fplookup_vexec =   VOP_EAGAIN,
168 	.vop_fplookup_symlink = VOP_EAGAIN,
169 };
170 VFS_VOP_VECTOR_REGISTER(ffs_fifoops1);
171 
172 /* Global vfs data structures for ufs. */
173 struct vop_vector ffs_vnodeops2 = {
174 	.vop_default =		&ufs_vnodeops,
175 	.vop_fsync =		ffs_fsync,
176 	.vop_fdatasync =	ffs_fdatasync,
177 	.vop_getpages =		ffs_getpages,
178 	.vop_getpages_async =	ffs_getpages_async,
179 	.vop_lock1 =		ffs_lock,
180 #ifdef INVARIANTS
181 	.vop_unlock =		ffs_unlock_debug,
182 #endif
183 	.vop_read =		ffs_read,
184 	.vop_reallocblks =	ffs_reallocblks,
185 	.vop_write =		ffs_write,
186 	.vop_closeextattr =	ffs_closeextattr,
187 	.vop_deleteextattr =	ffs_deleteextattr,
188 	.vop_getextattr =	ffs_getextattr,
189 	.vop_listextattr =	ffs_listextattr,
190 	.vop_openextattr =	ffs_openextattr,
191 	.vop_setextattr =	ffs_setextattr,
192 	.vop_vptofh =		ffs_vptofh,
193 	.vop_vput_pair =	ffs_vput_pair,
194 	.vop_fplookup_vexec =	ufs_fplookup_vexec,
195 	.vop_fplookup_symlink =	VOP_EAGAIN,
196 };
197 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops2);
198 
199 struct vop_vector ffs_fifoops2 = {
200 	.vop_default =		&ufs_fifoops,
201 	.vop_fsync =		ffs_fsync,
202 	.vop_fdatasync =	ffs_fdatasync,
203 	.vop_lock1 =		ffs_lock,
204 #ifdef INVARIANTS
205 	.vop_unlock =		ffs_unlock_debug,
206 #endif
207 	.vop_reallocblks =	ffs_reallocblks,
208 	.vop_strategy =		ffsext_strategy,
209 	.vop_closeextattr =	ffs_closeextattr,
210 	.vop_deleteextattr =	ffs_deleteextattr,
211 	.vop_getextattr =	ffs_getextattr,
212 	.vop_listextattr =	ffs_listextattr,
213 	.vop_openextattr =	ffs_openextattr,
214 	.vop_setextattr =	ffs_setextattr,
215 	.vop_vptofh =		ffs_vptofh,
216 	.vop_fplookup_vexec =   VOP_EAGAIN,
217 	.vop_fplookup_symlink = VOP_EAGAIN,
218 };
219 VFS_VOP_VECTOR_REGISTER(ffs_fifoops2);
220 
221 /*
222  * Synch an open file.
223  */
224 /* ARGSUSED */
225 static int
226 ffs_fsync(struct vop_fsync_args *ap)
227 {
228 	struct vnode *vp;
229 	struct bufobj *bo;
230 	int error;
231 
232 	vp = ap->a_vp;
233 	bo = &vp->v_bufobj;
234 retry:
235 	error = ffs_syncvnode(vp, ap->a_waitfor, 0);
236 	if (error)
237 		return (error);
238 	if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) {
239 		error = softdep_fsync(vp);
240 		if (error)
241 			return (error);
242 
243 		/*
244 		 * The softdep_fsync() function may drop vp lock,
245 		 * allowing for dirty buffers to reappear on the
246 		 * bo_dirty list. Recheck and resync as needed.
247 		 */
248 		BO_LOCK(bo);
249 		if ((vp->v_type == VREG || vp->v_type == VDIR) &&
250 		    (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
251 			BO_UNLOCK(bo);
252 			goto retry;
253 		}
254 		BO_UNLOCK(bo);
255 	}
256 	if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), 0))
257 		return (ENXIO);
258 	return (0);
259 }
260 
261 int
262 ffs_syncvnode(struct vnode *vp, int waitfor, int flags)
263 {
264 	struct inode *ip;
265 	struct bufobj *bo;
266 	struct ufsmount *ump;
267 	struct buf *bp, *nbp;
268 	ufs_lbn_t lbn;
269 	int error, passes, wflag;
270 	bool still_dirty, unlocked, wait;
271 
272 	ip = VTOI(vp);
273 	bo = &vp->v_bufobj;
274 	ump = VFSTOUFS(vp->v_mount);
275 #ifdef WITNESS
276 	wflag = IS_SNAPSHOT(ip) ? LK_NOWITNESS : 0;
277 #else
278 	wflag = 0;
279 #endif
280 
281 	/*
282 	 * When doing MNT_WAIT we must first flush all dependencies
283 	 * on the inode.
284 	 */
285 	if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
286 	    (error = softdep_sync_metadata(vp)) != 0) {
287 		if (ffs_fsfail_cleanup(ump, error))
288 			error = 0;
289 		return (error);
290 	}
291 
292 	/*
293 	 * Flush all dirty buffers associated with a vnode.
294 	 */
295 	error = 0;
296 	passes = 0;
297 	wait = false;	/* Always do an async pass first. */
298 	unlocked = false;
299 	lbn = lblkno(ITOFS(ip), (ip->i_size + ITOFS(ip)->fs_bsize - 1));
300 	BO_LOCK(bo);
301 loop:
302 	TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
303 		bp->b_vflags &= ~BV_SCANNED;
304 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
305 		/*
306 		 * Reasons to skip this buffer: it has already been considered
307 		 * on this pass, the buffer has dependencies that will cause
308 		 * it to be redirtied and it has not already been deferred,
309 		 * or it is already being written.
310 		 */
311 		if ((bp->b_vflags & BV_SCANNED) != 0)
312 			continue;
313 		bp->b_vflags |= BV_SCANNED;
314 		/*
315 		 * Flush indirects in order, if requested.
316 		 *
317 		 * Note that if only datasync is requested, we can
318 		 * skip indirect blocks when softupdates are not
319 		 * active.  Otherwise we must flush them with data,
320 		 * since dependencies prevent data block writes.
321 		 */
322 		if (waitfor == MNT_WAIT && bp->b_lblkno <= -UFS_NDADDR &&
323 		    (lbn_level(bp->b_lblkno) >= passes ||
324 		    ((flags & DATA_ONLY) != 0 && !DOINGSOFTDEP(vp))))
325 			continue;
326 		if (bp->b_lblkno > lbn)
327 			panic("ffs_syncvnode: syncing truncated data.");
328 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
329 			BO_UNLOCK(bo);
330 		} else if (wait) {
331 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
332 			    LK_INTERLOCK | wflag, BO_LOCKPTR(bo)) != 0) {
333 				BO_LOCK(bo);
334 				bp->b_vflags &= ~BV_SCANNED;
335 				goto next_locked;
336 			}
337 		} else
338 			continue;
339 		if ((bp->b_flags & B_DELWRI) == 0)
340 			panic("ffs_fsync: not dirty");
341 		/*
342 		 * Check for dependencies and potentially complete them.
343 		 */
344 		if (!LIST_EMPTY(&bp->b_dep) &&
345 		    (error = softdep_sync_buf(vp, bp,
346 		    wait ? MNT_WAIT : MNT_NOWAIT)) != 0) {
347 			/*
348 			 * Lock order conflict, buffer was already unlocked,
349 			 * and vnode possibly unlocked.
350 			 */
351 			if (error == ERELOOKUP) {
352 				if (vp->v_data == NULL)
353 					return (EBADF);
354 				unlocked = true;
355 				if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
356 				    (error = softdep_sync_metadata(vp)) != 0) {
357 					if (ffs_fsfail_cleanup(ump, error))
358 						error = 0;
359 					return (unlocked && error == 0 ?
360 					    ERELOOKUP : error);
361 				}
362 				/* Re-evaluate inode size */
363 				lbn = lblkno(ITOFS(ip), (ip->i_size +
364 				    ITOFS(ip)->fs_bsize - 1));
365 				goto next;
366 			}
367 			/* I/O error. */
368 			if (error != EBUSY) {
369 				BUF_UNLOCK(bp);
370 				return (error);
371 			}
372 			/* If we deferred once, don't defer again. */
373 		    	if ((bp->b_flags & B_DEFERRED) == 0) {
374 				bp->b_flags |= B_DEFERRED;
375 				BUF_UNLOCK(bp);
376 				goto next;
377 			}
378 		}
379 		if (wait) {
380 			bremfree(bp);
381 			error = bwrite(bp);
382 			if (ffs_fsfail_cleanup(ump, error))
383 				error = 0;
384 			if (error != 0)
385 				return (error);
386 		} else if ((bp->b_flags & B_CLUSTEROK)) {
387 			(void) vfs_bio_awrite(bp);
388 		} else {
389 			bremfree(bp);
390 			(void) bawrite(bp);
391 		}
392 next:
393 		/*
394 		 * Since we may have slept during the I/O, we need
395 		 * to start from a known point.
396 		 */
397 		BO_LOCK(bo);
398 next_locked:
399 		nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
400 	}
401 	if (waitfor != MNT_WAIT) {
402 		BO_UNLOCK(bo);
403 		if ((flags & NO_INO_UPDT) != 0)
404 			return (unlocked ? ERELOOKUP : 0);
405 		error = ffs_update(vp, 0);
406 		if (error == 0 && unlocked)
407 			error = ERELOOKUP;
408 		return (error);
409 	}
410 	/* Drain IO to see if we're done. */
411 	bufobj_wwait(bo, 0, 0);
412 	/*
413 	 * Block devices associated with filesystems may have new I/O
414 	 * requests posted for them even if the vnode is locked, so no
415 	 * amount of trying will get them clean.  We make several passes
416 	 * as a best effort.
417 	 *
418 	 * Regular files may need multiple passes to flush all dependency
419 	 * work as it is possible that we must write once per indirect
420 	 * level, once for the leaf, and once for the inode and each of
421 	 * these will be done with one sync and one async pass.
422 	 */
423 	if (bo->bo_dirty.bv_cnt > 0) {
424 		if ((flags & DATA_ONLY) == 0) {
425 			still_dirty = true;
426 		} else {
427 			/*
428 			 * For data-only sync, dirty indirect buffers
429 			 * are ignored.
430 			 */
431 			still_dirty = false;
432 			TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
433 				if (bp->b_lblkno > -UFS_NDADDR) {
434 					still_dirty = true;
435 					break;
436 				}
437 			}
438 		}
439 
440 		if (still_dirty) {
441 			/* Write the inode after sync passes to flush deps. */
442 			if (wait && DOINGSOFTDEP(vp) &&
443 			    (flags & NO_INO_UPDT) == 0) {
444 				BO_UNLOCK(bo);
445 				ffs_update(vp, 1);
446 				BO_LOCK(bo);
447 			}
448 			/* switch between sync/async. */
449 			wait = !wait;
450 			if (wait || ++passes < UFS_NIADDR + 2)
451 				goto loop;
452 		}
453 	}
454 	BO_UNLOCK(bo);
455 	error = 0;
456 	if ((flags & DATA_ONLY) == 0) {
457 		if ((flags & NO_INO_UPDT) == 0)
458 			error = ffs_update(vp, 1);
459 		if (DOINGSUJ(vp))
460 			softdep_journal_fsync(VTOI(vp));
461 	} else if ((ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)) != 0) {
462 		error = ffs_update(vp, 1);
463 	}
464 	if (error == 0 && unlocked)
465 		error = ERELOOKUP;
466 	if (error == 0)
467 		ip->i_flag &= ~IN_NEEDSYNC;
468 	return (error);
469 }
470 
471 static int
472 ffs_fdatasync(struct vop_fdatasync_args *ap)
473 {
474 
475 	return (ffs_syncvnode(ap->a_vp, MNT_WAIT, DATA_ONLY));
476 }
477 
478 static int
479 ffs_lock(
480 	struct vop_lock1_args /* {
481 		struct vnode *a_vp;
482 		int a_flags;
483 		char *file;
484 		int line;
485 	} */ *ap)
486 {
487 #if !defined(NO_FFS_SNAPSHOT) || defined(DIAGNOSTIC)
488 	struct vnode *vp = ap->a_vp;
489 #endif	/* !NO_FFS_SNAPSHOT || DIAGNOSTIC */
490 #ifdef DIAGNOSTIC
491 	struct inode *ip;
492 #endif	/* DIAGNOSTIC */
493 	int result;
494 #ifndef NO_FFS_SNAPSHOT
495 	int flags;
496 	struct lock *lkp;
497 
498 	/*
499 	 * Adaptive spinning mixed with SU leads to trouble. use a giant hammer
500 	 * and only use it when LK_NODDLKTREAT is set. Currently this means it
501 	 * is only used during path lookup.
502 	 */
503 	if ((ap->a_flags & LK_NODDLKTREAT) != 0)
504 		ap->a_flags |= LK_ADAPTIVE;
505 	switch (ap->a_flags & LK_TYPE_MASK) {
506 	case LK_SHARED:
507 	case LK_UPGRADE:
508 	case LK_EXCLUSIVE:
509 		flags = ap->a_flags;
510 		for (;;) {
511 #ifdef DEBUG_VFS_LOCKS
512 			VNPASS(vp->v_holdcnt != 0, vp);
513 #endif	/* DEBUG_VFS_LOCKS */
514 			lkp = vp->v_vnlock;
515 			result = lockmgr_lock_flags(lkp, flags,
516 			    &VI_MTX(vp)->lock_object, ap->a_file, ap->a_line);
517 			if (lkp == vp->v_vnlock || result != 0)
518 				break;
519 			/*
520 			 * Apparent success, except that the vnode
521 			 * mutated between snapshot file vnode and
522 			 * regular file vnode while this process
523 			 * slept.  The lock currently held is not the
524 			 * right lock.  Release it, and try to get the
525 			 * new lock.
526 			 */
527 			lockmgr_unlock(lkp);
528 			if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
529 			    (LK_INTERLOCK | LK_NOWAIT))
530 				return (EBUSY);
531 			if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
532 				flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
533 			flags &= ~LK_INTERLOCK;
534 		}
535 #ifdef DIAGNOSTIC
536 		switch (ap->a_flags & LK_TYPE_MASK) {
537 		case LK_UPGRADE:
538 		case LK_EXCLUSIVE:
539 			if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
540 				ip = VTOI(vp);
541 				if (ip != NULL)
542 					ip->i_lock_gen++;
543 			}
544 		}
545 #endif	/* DIAGNOSTIC */
546 		break;
547 	default:
548 #ifdef DIAGNOSTIC
549 		if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
550 			ip = VTOI(vp);
551 			if (ip != NULL)
552 				ufs_unlock_tracker(ip);
553 		}
554 #endif	/* DIAGNOSTIC */
555 		result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
556 		break;
557 	}
558 #else	/* NO_FFS_SNAPSHOT */
559 	/*
560 	 * See above for an explanation.
561 	 */
562 	if ((ap->a_flags & LK_NODDLKTREAT) != 0)
563 		ap->a_flags |= LK_ADAPTIVE;
564 #ifdef DIAGNOSTIC
565 	if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
566 		ip = VTOI(vp);
567 		if (ip != NULL)
568 			ufs_unlock_tracker(ip);
569 	}
570 #endif	/* DIAGNOSTIC */
571 	result =  VOP_LOCK1_APV(&ufs_vnodeops, ap);
572 #endif	/* NO_FFS_SNAPSHOT */
573 #ifdef DIAGNOSTIC
574 	switch (ap->a_flags & LK_TYPE_MASK) {
575 	case LK_UPGRADE:
576 	case LK_EXCLUSIVE:
577 		if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
578 			ip = VTOI(vp);
579 			if (ip != NULL)
580 				ip->i_lock_gen++;
581 		}
582 	}
583 #endif	/* DIAGNOSTIC */
584 	return (result);
585 }
586 
587 #ifdef INVARIANTS
588 static int
589 ffs_unlock_debug(struct vop_unlock_args *ap)
590 {
591 	struct vnode *vp;
592 	struct inode *ip;
593 
594 	vp = ap->a_vp;
595 	ip = VTOI(vp);
596 	if (ip->i_flag & UFS_INODE_FLAG_LAZY_MASK_ASSERTABLE) {
597 		if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
598 			VI_LOCK(vp);
599 			VNASSERT((vp->v_mflag & VMP_LAZYLIST), vp,
600 			    ("%s: modified vnode (%x) not on lazy list",
601 			    __func__, ip->i_flag));
602 			VI_UNLOCK(vp);
603 		}
604 	}
605 	KASSERT(vp->v_type != VDIR || vp->v_vnlock->lk_recurse != 0 ||
606 	    (ip->i_flag & IN_ENDOFF) == 0,
607 	    ("ufs dir vp %p ip %p flags %#x", vp, ip, ip->i_flag));
608 #ifdef DIAGNOSTIC
609 	if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && ip != NULL &&
610 	    vp->v_vnlock->lk_recurse == 0)
611 		ufs_unlock_tracker(ip);
612 #endif
613 	return (VOP_UNLOCK_APV(&ufs_vnodeops, ap));
614 }
615 #endif
616 
617 static int
618 ffs_read_hole(struct uio *uio, long xfersize, long *size)
619 {
620 	ssize_t saved_resid, tlen;
621 	int error;
622 
623 	while (xfersize > 0) {
624 		tlen = min(xfersize, ZERO_REGION_SIZE);
625 		saved_resid = uio->uio_resid;
626 		error = vn_io_fault_uiomove(__DECONST(void *, zero_region),
627 		    tlen, uio);
628 		if (error != 0)
629 			return (error);
630 		tlen = saved_resid - uio->uio_resid;
631 		xfersize -= tlen;
632 		*size -= tlen;
633 	}
634 	return (0);
635 }
636 
637 /*
638  * Vnode op for reading.
639  */
640 static int
641 ffs_read(
642 	struct vop_read_args /* {
643 		struct vnode *a_vp;
644 		struct uio *a_uio;
645 		int a_ioflag;
646 		struct ucred *a_cred;
647 	} */ *ap)
648 {
649 	struct vnode *vp;
650 	struct inode *ip;
651 	struct uio *uio;
652 	struct fs *fs;
653 	struct buf *bp;
654 	ufs_lbn_t lbn, nextlbn;
655 	off_t bytesinfile;
656 	long size, xfersize, blkoffset;
657 	ssize_t orig_resid;
658 	int bflag, error, ioflag, seqcount;
659 
660 	vp = ap->a_vp;
661 	uio = ap->a_uio;
662 	ioflag = ap->a_ioflag;
663 	if (ap->a_ioflag & IO_EXT)
664 #ifdef notyet
665 		return (ffs_extread(vp, uio, ioflag));
666 #else
667 		panic("ffs_read+IO_EXT");
668 #endif
669 #ifdef DIRECTIO
670 	if ((ioflag & IO_DIRECT) != 0) {
671 		int workdone;
672 
673 		error = ffs_rawread(vp, uio, &workdone);
674 		if (error != 0 || workdone != 0)
675 			return error;
676 	}
677 #endif
678 
679 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
680 	ip = VTOI(vp);
681 
682 #ifdef INVARIANTS
683 	if (uio->uio_rw != UIO_READ)
684 		panic("ffs_read: mode");
685 
686 	if (vp->v_type == VLNK) {
687 		if ((int)ip->i_size < VFSTOUFS(vp->v_mount)->um_maxsymlinklen)
688 			panic("ffs_read: short symlink");
689 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
690 		panic("ffs_read: type %d",  vp->v_type);
691 #endif
692 	orig_resid = uio->uio_resid;
693 	KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
694 	if (orig_resid == 0)
695 		return (0);
696 	KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
697 	fs = ITOFS(ip);
698 	if (uio->uio_offset < ip->i_size &&
699 	    uio->uio_offset >= fs->fs_maxfilesize)
700 		return (EOVERFLOW);
701 
702 	bflag = GB_UNMAPPED | (uio->uio_segflg == UIO_NOCOPY ? 0 : GB_NOSPARSE);
703 #ifdef WITNESS
704 	bflag |= IS_SNAPSHOT(ip) ? GB_NOWITNESS : 0;
705 #endif
706 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
707 		if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
708 			break;
709 		lbn = lblkno(fs, uio->uio_offset);
710 		nextlbn = lbn + 1;
711 
712 		/*
713 		 * size of buffer.  The buffer representing the
714 		 * end of the file is rounded up to the size of
715 		 * the block type ( fragment or full block,
716 		 * depending ).
717 		 */
718 		size = blksize(fs, ip, lbn);
719 		blkoffset = blkoff(fs, uio->uio_offset);
720 
721 		/*
722 		 * The amount we want to transfer in this iteration is
723 		 * one FS block less the amount of the data before
724 		 * our startpoint (duh!)
725 		 */
726 		xfersize = fs->fs_bsize - blkoffset;
727 
728 		/*
729 		 * But if we actually want less than the block,
730 		 * or the file doesn't have a whole block more of data,
731 		 * then use the lesser number.
732 		 */
733 		if (uio->uio_resid < xfersize)
734 			xfersize = uio->uio_resid;
735 		if (bytesinfile < xfersize)
736 			xfersize = bytesinfile;
737 
738 		if (lblktosize(fs, nextlbn) >= ip->i_size) {
739 			/*
740 			 * Don't do readahead if this is the end of the file.
741 			 */
742 			error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
743 		} else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
744 			/*
745 			 * Otherwise if we are allowed to cluster,
746 			 * grab as much as we can.
747 			 *
748 			 * XXX  This may not be a win if we are not
749 			 * doing sequential access.
750 			 */
751 			error = cluster_read(vp, ip->i_size, lbn,
752 			    size, NOCRED, blkoffset + uio->uio_resid,
753 			    seqcount, bflag, &bp);
754 		} else if (seqcount > 1) {
755 			/*
756 			 * If we are NOT allowed to cluster, then
757 			 * if we appear to be acting sequentially,
758 			 * fire off a request for a readahead
759 			 * as well as a read. Note that the 4th and 5th
760 			 * arguments point to arrays of the size specified in
761 			 * the 6th argument.
762 			 */
763 			int nextsize = blksize(fs, ip, nextlbn);
764 			error = breadn_flags(vp, lbn, lbn, size, &nextlbn,
765 			    &nextsize, 1, NOCRED, bflag, NULL, &bp);
766 		} else {
767 			/*
768 			 * Failing all of the above, just read what the
769 			 * user asked for. Interestingly, the same as
770 			 * the first option above.
771 			 */
772 			error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
773 		}
774 		if (error == EJUSTRETURN) {
775 			error = ffs_read_hole(uio, xfersize, &size);
776 			if (error == 0)
777 				continue;
778 		}
779 		if (error != 0) {
780 			brelse(bp);
781 			bp = NULL;
782 			break;
783 		}
784 
785 		/*
786 		 * We should only get non-zero b_resid when an I/O error
787 		 * has occurred, which should cause us to break above.
788 		 * However, if the short read did not cause an error,
789 		 * then we want to ensure that we do not uiomove bad
790 		 * or uninitialized data.
791 		 */
792 		size -= bp->b_resid;
793 		if (size < xfersize) {
794 			if (size == 0)
795 				break;
796 			xfersize = size;
797 		}
798 
799 		if (buf_mapped(bp)) {
800 			error = vn_io_fault_uiomove((char *)bp->b_data +
801 			    blkoffset, (int)xfersize, uio);
802 		} else {
803 			error = vn_io_fault_pgmove(bp->b_pages,
804 			    blkoffset + (bp->b_offset & PAGE_MASK),
805 			    (int)xfersize, uio);
806 		}
807 		if (error)
808 			break;
809 
810 		vfs_bio_brelse(bp, ioflag);
811 	}
812 
813 	/*
814 	 * This can only happen in the case of an error
815 	 * because the loop above resets bp to NULL on each iteration
816 	 * and on normal completion has not set a new value into it.
817 	 * so it must have come from a 'break' statement
818 	 */
819 	if (bp != NULL)
820 		vfs_bio_brelse(bp, ioflag);
821 
822 	if ((error == 0 || uio->uio_resid != orig_resid) &&
823 	    (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
824 		UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
825 	return (error);
826 }
827 
828 /*
829  * Vnode op for writing.
830  */
831 static int
832 ffs_write(
833 	struct vop_write_args /* {
834 		struct vnode *a_vp;
835 		struct uio *a_uio;
836 		int a_ioflag;
837 		struct ucred *a_cred;
838 	} */ *ap)
839 {
840 	struct vnode *vp;
841 	struct uio *uio;
842 	struct inode *ip;
843 	struct fs *fs;
844 	struct buf *bp;
845 	ufs_lbn_t lbn;
846 	off_t osize;
847 	ssize_t resid, r;
848 	int seqcount;
849 	int blkoffset, error, flags, ioflag, size, xfersize;
850 
851 	vp = ap->a_vp;
852 	if (DOINGSUJ(vp))
853 		softdep_prealloc(vp, MNT_WAIT);
854 	if (vp->v_data == NULL)
855 		return (EBADF);
856 
857 	uio = ap->a_uio;
858 	ioflag = ap->a_ioflag;
859 	if (ap->a_ioflag & IO_EXT)
860 #ifdef notyet
861 		return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
862 #else
863 		panic("ffs_write+IO_EXT");
864 #endif
865 
866 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
867 	ip = VTOI(vp);
868 
869 #ifdef INVARIANTS
870 	if (uio->uio_rw != UIO_WRITE)
871 		panic("ffs_write: mode");
872 #endif
873 
874 	switch (vp->v_type) {
875 	case VREG:
876 		if (ioflag & IO_APPEND)
877 			uio->uio_offset = ip->i_size;
878 		if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
879 			return (EPERM);
880 		/* FALLTHROUGH */
881 	case VLNK:
882 		break;
883 	case VDIR:
884 		panic("ffs_write: dir write");
885 		break;
886 	default:
887 		panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
888 			(int)uio->uio_offset,
889 			(int)uio->uio_resid
890 		);
891 	}
892 
893 	KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
894 	KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
895 	fs = ITOFS(ip);
896 
897 	/*
898 	 * Maybe this should be above the vnode op call, but so long as
899 	 * file servers have no limits, I don't think it matters.
900 	 */
901 	error = vn_rlimit_fsizex(vp, uio, fs->fs_maxfilesize, &r,
902 	    uio->uio_td);
903 	if (error != 0) {
904 		vn_rlimit_fsizex_res(uio, r);
905 		return (error);
906 	}
907 
908 	resid = uio->uio_resid;
909 	osize = ip->i_size;
910 	if (seqcount > BA_SEQMAX)
911 		flags = BA_SEQMAX << BA_SEQSHIFT;
912 	else
913 		flags = seqcount << BA_SEQSHIFT;
914 	if (ioflag & IO_SYNC)
915 		flags |= IO_SYNC;
916 	flags |= BA_UNMAPPED;
917 
918 	for (error = 0; uio->uio_resid > 0;) {
919 		lbn = lblkno(fs, uio->uio_offset);
920 		blkoffset = blkoff(fs, uio->uio_offset);
921 		xfersize = fs->fs_bsize - blkoffset;
922 		if (uio->uio_resid < xfersize)
923 			xfersize = uio->uio_resid;
924 		if (uio->uio_offset + xfersize > ip->i_size)
925 			vnode_pager_setsize(vp, uio->uio_offset + xfersize);
926 
927 		/*
928 		 * We must perform a read-before-write if the transfer size
929 		 * does not cover the entire buffer.
930 		 */
931 		if (fs->fs_bsize > xfersize)
932 			flags |= BA_CLRBUF;
933 		else
934 			flags &= ~BA_CLRBUF;
935 /* XXX is uio->uio_offset the right thing here? */
936 		error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
937 		    ap->a_cred, flags, &bp);
938 		if (error != 0) {
939 			vnode_pager_setsize(vp, ip->i_size);
940 			break;
941 		}
942 		if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
943 			bp->b_flags |= B_NOCACHE;
944 
945 		if (uio->uio_offset + xfersize > ip->i_size) {
946 			ip->i_size = uio->uio_offset + xfersize;
947 			DIP_SET(ip, i_size, ip->i_size);
948 			UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
949 		}
950 
951 		size = blksize(fs, ip, lbn) - bp->b_resid;
952 		if (size < xfersize)
953 			xfersize = size;
954 
955 		if (buf_mapped(bp)) {
956 			error = vn_io_fault_uiomove((char *)bp->b_data +
957 			    blkoffset, (int)xfersize, uio);
958 		} else {
959 			error = vn_io_fault_pgmove(bp->b_pages,
960 			    blkoffset + (bp->b_offset & PAGE_MASK),
961 			    (int)xfersize, uio);
962 		}
963 		/*
964 		 * If the buffer is not already filled and we encounter an
965 		 * error while trying to fill it, we have to clear out any
966 		 * garbage data from the pages instantiated for the buffer.
967 		 * If we do not, a failed uiomove() during a write can leave
968 		 * the prior contents of the pages exposed to a userland mmap.
969 		 *
970 		 * Note that we need only clear buffers with a transfer size
971 		 * equal to the block size because buffers with a shorter
972 		 * transfer size were cleared above by the call to UFS_BALLOC()
973 		 * with the BA_CLRBUF flag set.
974 		 *
975 		 * If the source region for uiomove identically mmaps the
976 		 * buffer, uiomove() performed the NOP copy, and the buffer
977 		 * content remains valid because the page fault handler
978 		 * validated the pages.
979 		 */
980 		if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
981 		    fs->fs_bsize == xfersize)
982 			vfs_bio_clrbuf(bp);
983 
984 		vfs_bio_set_flags(bp, ioflag);
985 
986 		/*
987 		 * If IO_SYNC each buffer is written synchronously.  Otherwise
988 		 * if we have a severe page deficiency write the buffer
989 		 * asynchronously.  Otherwise try to cluster, and if that
990 		 * doesn't do it then either do an async write (if O_DIRECT),
991 		 * or a delayed write (if not).
992 		 */
993 		if (ioflag & IO_SYNC) {
994 			(void)bwrite(bp);
995 		} else if (vm_page_count_severe() ||
996 			    buf_dirty_count_severe() ||
997 			    (ioflag & IO_ASYNC)) {
998 			bp->b_flags |= B_CLUSTEROK;
999 			bawrite(bp);
1000 		} else if (xfersize + blkoffset == fs->fs_bsize) {
1001 			if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
1002 				bp->b_flags |= B_CLUSTEROK;
1003 				cluster_write(vp, &ip->i_clusterw, bp,
1004 				    ip->i_size, seqcount, GB_UNMAPPED);
1005 			} else {
1006 				bawrite(bp);
1007 			}
1008 		} else if (ioflag & IO_DIRECT) {
1009 			bp->b_flags |= B_CLUSTEROK;
1010 			bawrite(bp);
1011 		} else {
1012 			bp->b_flags |= B_CLUSTEROK;
1013 			bdwrite(bp);
1014 		}
1015 		if (error || xfersize == 0)
1016 			break;
1017 		UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1018 	}
1019 	/*
1020 	 * If we successfully wrote any data, and we are not the superuser
1021 	 * we clear the setuid and setgid bits as a precaution against
1022 	 * tampering.
1023 	 */
1024 	if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
1025 	    ap->a_cred) {
1026 		if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID)) {
1027 			vn_seqc_write_begin(vp);
1028 			UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1029 			DIP_SET(ip, i_mode, ip->i_mode);
1030 			vn_seqc_write_end(vp);
1031 		}
1032 	}
1033 	if (error) {
1034 		if (ioflag & IO_UNIT) {
1035 			(void)ffs_truncate(vp, osize,
1036 			    IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred);
1037 			uio->uio_offset -= resid - uio->uio_resid;
1038 			uio->uio_resid = resid;
1039 		}
1040 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
1041 		if (!(ioflag & IO_DATASYNC) ||
1042 		    (ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)))
1043 			error = ffs_update(vp, 1);
1044 		if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
1045 			error = ENXIO;
1046 	}
1047 	vn_rlimit_fsizex_res(uio, r);
1048 	return (error);
1049 }
1050 
1051 /*
1052  * Extended attribute area reading.
1053  */
1054 static int
1055 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
1056 {
1057 	struct inode *ip;
1058 	struct ufs2_dinode *dp;
1059 	struct fs *fs;
1060 	struct buf *bp;
1061 	ufs_lbn_t lbn, nextlbn;
1062 	off_t bytesinfile;
1063 	long size, xfersize, blkoffset;
1064 	ssize_t orig_resid;
1065 	int error;
1066 
1067 	ip = VTOI(vp);
1068 	fs = ITOFS(ip);
1069 	dp = ip->i_din2;
1070 
1071 #ifdef INVARIANTS
1072 	if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
1073 		panic("ffs_extread: mode");
1074 
1075 #endif
1076 	orig_resid = uio->uio_resid;
1077 	KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
1078 	if (orig_resid == 0)
1079 		return (0);
1080 	KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
1081 
1082 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1083 		if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
1084 			break;
1085 		lbn = lblkno(fs, uio->uio_offset);
1086 		nextlbn = lbn + 1;
1087 
1088 		/*
1089 		 * size of buffer.  The buffer representing the
1090 		 * end of the file is rounded up to the size of
1091 		 * the block type ( fragment or full block,
1092 		 * depending ).
1093 		 */
1094 		size = sblksize(fs, dp->di_extsize, lbn);
1095 		blkoffset = blkoff(fs, uio->uio_offset);
1096 
1097 		/*
1098 		 * The amount we want to transfer in this iteration is
1099 		 * one FS block less the amount of the data before
1100 		 * our startpoint (duh!)
1101 		 */
1102 		xfersize = fs->fs_bsize - blkoffset;
1103 
1104 		/*
1105 		 * But if we actually want less than the block,
1106 		 * or the file doesn't have a whole block more of data,
1107 		 * then use the lesser number.
1108 		 */
1109 		if (uio->uio_resid < xfersize)
1110 			xfersize = uio->uio_resid;
1111 		if (bytesinfile < xfersize)
1112 			xfersize = bytesinfile;
1113 
1114 		if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
1115 			/*
1116 			 * Don't do readahead if this is the end of the info.
1117 			 */
1118 			error = bread(vp, -1 - lbn, size, NOCRED, &bp);
1119 		} else {
1120 			/*
1121 			 * If we have a second block, then
1122 			 * fire off a request for a readahead
1123 			 * as well as a read. Note that the 4th and 5th
1124 			 * arguments point to arrays of the size specified in
1125 			 * the 6th argument.
1126 			 */
1127 			int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
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(uint8_t *ptr, uint64_t length, int nspace, const char *name,
1304     struct extattr **eapp, uint8_t **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(uint8_t **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 	uint64_t easize;
1338 	int error;
1339 	uint8_t *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 uint8_t *)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 	uint8_t *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 = (uint8_t *)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 	uint8_t *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 	uint8_t *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 = (uint8_t *)EXTATTR_NEXT(eap) - eae;
1866 		if (ul != ealength) {
1867 			bcopy(EXTATTR_NEXT(eap), (uint8_t *)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((uint8_t *)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 	uint64_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