xref: /freebsd/sys/ufs/ffs/ffs_balloc.c (revision 036d2e814bf0f5d88ffb4b24c159320894541757)
1 /*-
2  * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND BSD-3-Clause)
3  *
4  * Copyright (c) 2002 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  *	@(#)ffs_balloc.c	8.8 (Berkeley) 6/16/95
62  */
63 
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/bio.h>
70 #include <sys/buf.h>
71 #include <sys/lock.h>
72 #include <sys/mount.h>
73 #include <sys/vnode.h>
74 #include <sys/vmmeter.h>
75 
76 #include <ufs/ufs/quota.h>
77 #include <ufs/ufs/inode.h>
78 #include <ufs/ufs/ufs_extern.h>
79 #include <ufs/ufs/extattr.h>
80 #include <ufs/ufs/ufsmount.h>
81 
82 #include <ufs/ffs/fs.h>
83 #include <ufs/ffs/ffs_extern.h>
84 
85 /*
86  * Balloc defines the structure of filesystem storage
87  * by allocating the physical blocks on a device given
88  * the inode and the logical block number in a file.
89  * This is the allocation strategy for UFS1. Below is
90  * the allocation strategy for UFS2.
91  */
92 int
93 ffs_balloc_ufs1(struct vnode *vp, off_t startoffset, int size,
94     struct ucred *cred, int flags, struct buf **bpp)
95 {
96 	struct inode *ip;
97 	struct ufs1_dinode *dp;
98 	ufs_lbn_t lbn, lastlbn;
99 	struct fs *fs;
100 	ufs1_daddr_t nb;
101 	struct buf *bp, *nbp;
102 	struct mount *mp;
103 	struct ufsmount *ump;
104 	struct indir indirs[UFS_NIADDR + 2];
105 	int deallocated, osize, nsize, num, i, error;
106 	ufs2_daddr_t newb;
107 	ufs1_daddr_t *bap, pref;
108 	ufs1_daddr_t *allocib, *blkp, *allocblk, allociblk[UFS_NIADDR + 1];
109 	ufs2_daddr_t *lbns_remfree, lbns[UFS_NIADDR + 1];
110 	int unwindidx = -1;
111 	int saved_inbdflush;
112 	int gbflags, reclaimed;
113 
114 	ip = VTOI(vp);
115 	dp = ip->i_din1;
116 	fs = ITOFS(ip);
117 	mp = ITOVFS(ip);
118 	ump = ITOUMP(ip);
119 	lbn = lblkno(fs, startoffset);
120 	size = blkoff(fs, startoffset) + size;
121 	reclaimed = 0;
122 	if (size > fs->fs_bsize)
123 		panic("ffs_balloc_ufs1: blk too big");
124 	*bpp = NULL;
125 	if (flags & IO_EXT)
126 		return (EOPNOTSUPP);
127 	if (lbn < 0)
128 		return (EFBIG);
129 	gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
130 
131 	if (DOINGSOFTDEP(vp))
132 		softdep_prealloc(vp, MNT_WAIT);
133 	/*
134 	 * If the next write will extend the file into a new block,
135 	 * and the file is currently composed of a fragment
136 	 * this fragment has to be extended to be a full block.
137 	 */
138 	lastlbn = lblkno(fs, ip->i_size);
139 	if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
140 		nb = lastlbn;
141 		osize = blksize(fs, ip, nb);
142 		if (osize < fs->fs_bsize && osize > 0) {
143 			UFS_LOCK(ump);
144 			error = ffs_realloccg(ip, nb, dp->di_db[nb],
145 			   ffs_blkpref_ufs1(ip, lastlbn, (int)nb,
146 			   &dp->di_db[0]), osize, (int)fs->fs_bsize, flags,
147 			   cred, &bp);
148 			if (error)
149 				return (error);
150 			if (DOINGSOFTDEP(vp))
151 				softdep_setup_allocdirect(ip, nb,
152 				    dbtofsb(fs, bp->b_blkno), dp->di_db[nb],
153 				    fs->fs_bsize, osize, bp);
154 			ip->i_size = smalllblktosize(fs, nb + 1);
155 			dp->di_size = ip->i_size;
156 			dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
157 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
158 			if (flags & IO_SYNC)
159 				bwrite(bp);
160 			else if (DOINGASYNC(vp))
161 				bdwrite(bp);
162 			else
163 				bawrite(bp);
164 		}
165 	}
166 	/*
167 	 * The first UFS_NDADDR blocks are direct blocks
168 	 */
169 	if (lbn < UFS_NDADDR) {
170 		if (flags & BA_METAONLY)
171 			panic("ffs_balloc_ufs1: BA_METAONLY for direct block");
172 		nb = dp->di_db[lbn];
173 		if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
174 			error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
175 			if (error) {
176 				return (error);
177 			}
178 			bp->b_blkno = fsbtodb(fs, nb);
179 			*bpp = bp;
180 			return (0);
181 		}
182 		if (nb != 0) {
183 			/*
184 			 * Consider need to reallocate a fragment.
185 			 */
186 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
187 			nsize = fragroundup(fs, size);
188 			if (nsize <= osize) {
189 				error = bread(vp, lbn, osize, NOCRED, &bp);
190 				if (error) {
191 					return (error);
192 				}
193 				bp->b_blkno = fsbtodb(fs, nb);
194 			} else {
195 				UFS_LOCK(ump);
196 				error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
197 				    ffs_blkpref_ufs1(ip, lbn, (int)lbn,
198 				    &dp->di_db[0]), osize, nsize, flags,
199 				    cred, &bp);
200 				if (error)
201 					return (error);
202 				if (DOINGSOFTDEP(vp))
203 					softdep_setup_allocdirect(ip, lbn,
204 					    dbtofsb(fs, bp->b_blkno), nb,
205 					    nsize, osize, bp);
206 			}
207 		} else {
208 			if (ip->i_size < smalllblktosize(fs, lbn + 1))
209 				nsize = fragroundup(fs, size);
210 			else
211 				nsize = fs->fs_bsize;
212 			UFS_LOCK(ump);
213 			error = ffs_alloc(ip, lbn,
214 			    ffs_blkpref_ufs1(ip, lbn, (int)lbn, &dp->di_db[0]),
215 			    nsize, flags, cred, &newb);
216 			if (error)
217 				return (error);
218 			bp = getblk(vp, lbn, nsize, 0, 0, gbflags);
219 			bp->b_blkno = fsbtodb(fs, newb);
220 			if (flags & BA_CLRBUF)
221 				vfs_bio_clrbuf(bp);
222 			if (DOINGSOFTDEP(vp))
223 				softdep_setup_allocdirect(ip, lbn, newb, 0,
224 				    nsize, 0, bp);
225 		}
226 		dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
227 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
228 		*bpp = bp;
229 		return (0);
230 	}
231 	/*
232 	 * Determine the number of levels of indirection.
233 	 */
234 	pref = 0;
235 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
236 		return(error);
237 #ifdef INVARIANTS
238 	if (num < 1)
239 		panic ("ffs_balloc_ufs1: ufs_getlbns returned indirect block");
240 #endif
241 	saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
242 	/*
243 	 * Fetch the first indirect block allocating if necessary.
244 	 */
245 	--num;
246 	nb = dp->di_ib[indirs[0].in_off];
247 	allocib = NULL;
248 	allocblk = allociblk;
249 	lbns_remfree = lbns;
250 	if (nb == 0) {
251 		UFS_LOCK(ump);
252 		pref = ffs_blkpref_ufs1(ip, lbn, -indirs[0].in_off - 1,
253 		    (ufs1_daddr_t *)0);
254 		if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
255 		    flags, cred, &newb)) != 0) {
256 			curthread_pflags_restore(saved_inbdflush);
257 			return (error);
258 		}
259 		pref = newb + fs->fs_frag;
260 		nb = newb;
261 		MPASS(allocblk < allociblk + nitems(allociblk));
262 		MPASS(lbns_remfree < lbns + nitems(lbns));
263 		*allocblk++ = nb;
264 		*lbns_remfree++ = indirs[1].in_lbn;
265 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, gbflags);
266 		bp->b_blkno = fsbtodb(fs, nb);
267 		vfs_bio_clrbuf(bp);
268 		if (DOINGSOFTDEP(vp)) {
269 			softdep_setup_allocdirect(ip,
270 			    UFS_NDADDR + indirs[0].in_off, newb, 0,
271 			    fs->fs_bsize, 0, bp);
272 			bdwrite(bp);
273 		} else if ((flags & IO_SYNC) == 0 && DOINGASYNC(vp)) {
274 			if (bp->b_bufsize == fs->fs_bsize)
275 				bp->b_flags |= B_CLUSTEROK;
276 			bdwrite(bp);
277 		} else {
278 			if ((error = bwrite(bp)) != 0)
279 				goto fail;
280 		}
281 		allocib = &dp->di_ib[indirs[0].in_off];
282 		*allocib = nb;
283 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
284 	}
285 	/*
286 	 * Fetch through the indirect blocks, allocating as necessary.
287 	 */
288 retry:
289 	for (i = 1;;) {
290 		error = bread(vp,
291 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
292 		if (error) {
293 			goto fail;
294 		}
295 		bap = (ufs1_daddr_t *)bp->b_data;
296 		nb = bap[indirs[i].in_off];
297 		if ((error = UFS_CHECK_BLKNO(mp, ip->i_number, nb,
298 		    fs->fs_bsize)) != 0) {
299 			brelse(bp);
300 			goto fail;
301 		}
302 		if (i == num)
303 			break;
304 		i += 1;
305 		if (nb != 0) {
306 			bqrelse(bp);
307 			continue;
308 		}
309 		UFS_LOCK(ump);
310 		/*
311 		 * If parent indirect has just been allocated, try to cluster
312 		 * immediately following it.
313 		 */
314 		if (pref == 0)
315 			pref = ffs_blkpref_ufs1(ip, lbn, i - num - 1,
316 			    (ufs1_daddr_t *)0);
317 		if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
318 		    flags | IO_BUFLOCKED, cred, &newb)) != 0) {
319 			brelse(bp);
320 			UFS_LOCK(ump);
321 			if (DOINGSOFTDEP(vp) && ++reclaimed == 1) {
322 				softdep_request_cleanup(fs, vp, cred,
323 				    FLUSH_BLOCKS_WAIT);
324 				UFS_UNLOCK(ump);
325 				goto retry;
326 			}
327 			if (ppsratecheck(&ump->um_last_fullmsg,
328 			    &ump->um_secs_fullmsg, 1)) {
329 				UFS_UNLOCK(ump);
330 				ffs_fserr(fs, ip->i_number, "filesystem full");
331 				uprintf("\n%s: write failed, filesystem "
332 				    "is full\n", fs->fs_fsmnt);
333 			} else {
334 				UFS_UNLOCK(ump);
335 			}
336 			goto fail;
337 		}
338 		pref = newb + fs->fs_frag;
339 		nb = newb;
340 		MPASS(allocblk < allociblk + nitems(allociblk));
341 		MPASS(lbns_remfree < lbns + nitems(lbns));
342 		*allocblk++ = nb;
343 		*lbns_remfree++ = indirs[i].in_lbn;
344 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
345 		nbp->b_blkno = fsbtodb(fs, nb);
346 		vfs_bio_clrbuf(nbp);
347 		if (DOINGSOFTDEP(vp)) {
348 			softdep_setup_allocindir_meta(nbp, ip, bp,
349 			    indirs[i - 1].in_off, nb);
350 			bdwrite(nbp);
351 		} else if ((flags & IO_SYNC) == 0 && DOINGASYNC(vp)) {
352 			if (nbp->b_bufsize == fs->fs_bsize)
353 				nbp->b_flags |= B_CLUSTEROK;
354 			bdwrite(nbp);
355 		} else {
356 			if ((error = bwrite(nbp)) != 0) {
357 				brelse(bp);
358 				goto fail;
359 			}
360 		}
361 		bap[indirs[i - 1].in_off] = nb;
362 		if (allocib == NULL && unwindidx < 0)
363 			unwindidx = i - 1;
364 		/*
365 		 * If required, write synchronously, otherwise use
366 		 * delayed write.
367 		 */
368 		if (flags & IO_SYNC) {
369 			bwrite(bp);
370 		} else {
371 			if (bp->b_bufsize == fs->fs_bsize)
372 				bp->b_flags |= B_CLUSTEROK;
373 			bdwrite(bp);
374 		}
375 	}
376 	/*
377 	 * If asked only for the indirect block, then return it.
378 	 */
379 	if (flags & BA_METAONLY) {
380 		curthread_pflags_restore(saved_inbdflush);
381 		*bpp = bp;
382 		return (0);
383 	}
384 	/*
385 	 * Get the data block, allocating if necessary.
386 	 */
387 	if (nb == 0) {
388 		UFS_LOCK(ump);
389 		/*
390 		 * If allocating metadata at the front of the cylinder
391 		 * group and parent indirect block has just been allocated,
392 		 * then cluster next to it if it is the first indirect in
393 		 * the file. Otherwise it has been allocated in the metadata
394 		 * area, so we want to find our own place out in the data area.
395 		 */
396 		if (pref == 0 || (lbn > UFS_NDADDR && fs->fs_metaspace != 0))
397 			pref = ffs_blkpref_ufs1(ip, lbn, indirs[i].in_off,
398 			    &bap[0]);
399 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
400 		    flags | IO_BUFLOCKED, cred, &newb);
401 		if (error) {
402 			brelse(bp);
403 			UFS_LOCK(ump);
404 			if (DOINGSOFTDEP(vp) && ++reclaimed == 1) {
405 				softdep_request_cleanup(fs, vp, cred,
406 				    FLUSH_BLOCKS_WAIT);
407 				UFS_UNLOCK(ump);
408 				goto retry;
409 			}
410 			if (ppsratecheck(&ump->um_last_fullmsg,
411 			    &ump->um_secs_fullmsg, 1)) {
412 				UFS_UNLOCK(ump);
413 				ffs_fserr(fs, ip->i_number, "filesystem full");
414 				uprintf("\n%s: write failed, filesystem "
415 				    "is full\n", fs->fs_fsmnt);
416 			} else {
417 				UFS_UNLOCK(ump);
418 			}
419 			goto fail;
420 		}
421 		nb = newb;
422 		MPASS(allocblk < allociblk + nitems(allociblk));
423 		MPASS(lbns_remfree < lbns + nitems(lbns));
424 		*allocblk++ = nb;
425 		*lbns_remfree++ = lbn;
426 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
427 		nbp->b_blkno = fsbtodb(fs, nb);
428 		if (flags & BA_CLRBUF)
429 			vfs_bio_clrbuf(nbp);
430 		if (DOINGSOFTDEP(vp))
431 			softdep_setup_allocindir_page(ip, lbn, bp,
432 			    indirs[i].in_off, nb, 0, nbp);
433 		bap[indirs[i].in_off] = nb;
434 		/*
435 		 * If required, write synchronously, otherwise use
436 		 * delayed write.
437 		 */
438 		if (flags & IO_SYNC) {
439 			bwrite(bp);
440 		} else {
441 			if (bp->b_bufsize == fs->fs_bsize)
442 				bp->b_flags |= B_CLUSTEROK;
443 			bdwrite(bp);
444 		}
445 		curthread_pflags_restore(saved_inbdflush);
446 		*bpp = nbp;
447 		return (0);
448 	}
449 	brelse(bp);
450 	if (flags & BA_CLRBUF) {
451 		int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
452 		if (seqcount != 0 &&
453 		    (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0 &&
454 		    !(vm_page_count_severe() || buf_dirty_count_severe())) {
455 			error = cluster_read(vp, ip->i_size, lbn,
456 			    (int)fs->fs_bsize, NOCRED,
457 			    MAXBSIZE, seqcount, gbflags, &nbp);
458 		} else {
459 			error = bread_gb(vp, lbn, (int)fs->fs_bsize, NOCRED,
460 			    gbflags, &nbp);
461 		}
462 		if (error) {
463 			brelse(nbp);
464 			goto fail;
465 		}
466 	} else {
467 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
468 		nbp->b_blkno = fsbtodb(fs, nb);
469 	}
470 	curthread_pflags_restore(saved_inbdflush);
471 	*bpp = nbp;
472 	return (0);
473 fail:
474 	curthread_pflags_restore(saved_inbdflush);
475 	/*
476 	 * If we have failed to allocate any blocks, simply return the error.
477 	 * This is the usual case and avoids the need to fsync the file.
478 	 */
479 	if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
480 		return (error);
481 	/*
482 	 * If we have failed part way through block allocation, we
483 	 * have to deallocate any indirect blocks that we have allocated.
484 	 * We have to fsync the file before we start to get rid of all
485 	 * of its dependencies so that we do not leave them dangling.
486 	 * We have to sync it at the end so that the soft updates code
487 	 * does not find any untracked changes. Although this is really
488 	 * slow, running out of disk space is not expected to be a common
489 	 * occurrence. The error return from fsync is ignored as we already
490 	 * have an error to return to the user.
491 	 *
492 	 * XXX Still have to journal the free below
493 	 */
494 	(void) ffs_syncvnode(vp, MNT_WAIT, 0);
495 	for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
496 	     blkp < allocblk; blkp++, lbns_remfree++) {
497 		/*
498 		 * We shall not leave the freed blocks on the vnode
499 		 * buffer object lists.
500 		 */
501 		bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0,
502 		    GB_NOCREAT | GB_UNMAPPED);
503 		if (bp != NULL) {
504 			KASSERT(bp->b_blkno == fsbtodb(fs, *blkp),
505 			    ("mismatch1 l %jd %jd b %ju %ju",
506 			    (intmax_t)bp->b_lblkno, (uintmax_t)*lbns_remfree,
507 			    (uintmax_t)bp->b_blkno,
508 			    (uintmax_t)fsbtodb(fs, *blkp)));
509 			bp->b_flags |= B_INVAL | B_RELBUF | B_NOCACHE;
510 			bp->b_flags &= ~(B_ASYNC | B_CACHE);
511 			brelse(bp);
512 		}
513 		deallocated += fs->fs_bsize;
514 	}
515 	if (allocib != NULL) {
516 		*allocib = 0;
517 	} else if (unwindidx >= 0) {
518 		int r;
519 
520 		r = bread(vp, indirs[unwindidx].in_lbn,
521 		    (int)fs->fs_bsize, NOCRED, &bp);
522 		if (r) {
523 			panic("Could not unwind indirect block, error %d", r);
524 			brelse(bp);
525 		} else {
526 			bap = (ufs1_daddr_t *)bp->b_data;
527 			bap[indirs[unwindidx].in_off] = 0;
528 			if (flags & IO_SYNC) {
529 				bwrite(bp);
530 			} else {
531 				if (bp->b_bufsize == fs->fs_bsize)
532 					bp->b_flags |= B_CLUSTEROK;
533 				bdwrite(bp);
534 			}
535 		}
536 	}
537 	if (deallocated) {
538 #ifdef QUOTA
539 		/*
540 		 * Restore user's disk quota because allocation failed.
541 		 */
542 		(void) chkdq(ip, -btodb(deallocated), cred, FORCE);
543 #endif
544 		dp->di_blocks -= btodb(deallocated);
545 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
546 	}
547 	(void) ffs_syncvnode(vp, MNT_WAIT, 0);
548 	/*
549 	 * After the buffers are invalidated and on-disk pointers are
550 	 * cleared, free the blocks.
551 	 */
552 	for (blkp = allociblk; blkp < allocblk; blkp++) {
553 #ifdef INVARIANTS
554 		if (blkp == allociblk)
555 			lbns_remfree = lbns;
556 		bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0,
557 		    GB_NOCREAT | GB_UNMAPPED);
558 		if (bp != NULL) {
559 			panic("zombie1 %jd %ju %ju",
560 			    (intmax_t)bp->b_lblkno, (uintmax_t)bp->b_blkno,
561 			    (uintmax_t)fsbtodb(fs, *blkp));
562 		}
563 		lbns_remfree++;
564 #endif
565 		ffs_blkfree(ump, fs, ump->um_devvp, *blkp, fs->fs_bsize,
566 		    ip->i_number, vp->v_type, NULL, SINGLETON_KEY);
567 	}
568 	return (error);
569 }
570 
571 /*
572  * Balloc defines the structure of file system storage
573  * by allocating the physical blocks on a device given
574  * the inode and the logical block number in a file.
575  * This is the allocation strategy for UFS2. Above is
576  * the allocation strategy for UFS1.
577  */
578 int
579 ffs_balloc_ufs2(struct vnode *vp, off_t startoffset, int size,
580     struct ucred *cred, int flags, struct buf **bpp)
581 {
582 	struct inode *ip;
583 	struct ufs2_dinode *dp;
584 	ufs_lbn_t lbn, lastlbn;
585 	struct fs *fs;
586 	struct buf *bp, *nbp;
587 	struct mount *mp;
588 	struct ufsmount *ump;
589 	struct indir indirs[UFS_NIADDR + 2];
590 	ufs2_daddr_t nb, newb, *bap, pref;
591 	ufs2_daddr_t *allocib, *blkp, *allocblk, allociblk[UFS_NIADDR + 1];
592 	ufs2_daddr_t *lbns_remfree, lbns[UFS_NIADDR + 1];
593 	int deallocated, osize, nsize, num, i, error;
594 	int unwindidx = -1;
595 	int saved_inbdflush;
596 	int gbflags, reclaimed;
597 
598 	ip = VTOI(vp);
599 	dp = ip->i_din2;
600 	fs = ITOFS(ip);
601 	mp = ITOVFS(ip);
602 	ump = ITOUMP(ip);
603 	lbn = lblkno(fs, startoffset);
604 	size = blkoff(fs, startoffset) + size;
605 	reclaimed = 0;
606 	if (size > fs->fs_bsize)
607 		panic("ffs_balloc_ufs2: blk too big");
608 	*bpp = NULL;
609 	if (lbn < 0)
610 		return (EFBIG);
611 	gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
612 
613 	if (DOINGSOFTDEP(vp))
614 		softdep_prealloc(vp, MNT_WAIT);
615 
616 	/*
617 	 * Check for allocating external data.
618 	 */
619 	if (flags & IO_EXT) {
620 		if (lbn >= UFS_NXADDR)
621 			return (EFBIG);
622 		/*
623 		 * If the next write will extend the data into a new block,
624 		 * and the data is currently composed of a fragment
625 		 * this fragment has to be extended to be a full block.
626 		 */
627 		lastlbn = lblkno(fs, dp->di_extsize);
628 		if (lastlbn < lbn) {
629 			nb = lastlbn;
630 			osize = sblksize(fs, dp->di_extsize, nb);
631 			if (osize < fs->fs_bsize && osize > 0) {
632 				UFS_LOCK(ump);
633 				error = ffs_realloccg(ip, -1 - nb,
634 				    dp->di_extb[nb],
635 				    ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
636 				    &dp->di_extb[0]), osize,
637 				    (int)fs->fs_bsize, flags, cred, &bp);
638 				if (error)
639 					return (error);
640 				if (DOINGSOFTDEP(vp))
641 					softdep_setup_allocext(ip, nb,
642 					    dbtofsb(fs, bp->b_blkno),
643 					    dp->di_extb[nb],
644 					    fs->fs_bsize, osize, bp);
645 				dp->di_extsize = smalllblktosize(fs, nb + 1);
646 				dp->di_extb[nb] = dbtofsb(fs, bp->b_blkno);
647 				bp->b_xflags |= BX_ALTDATA;
648 				ip->i_flag |= IN_CHANGE;
649 				if (flags & IO_SYNC)
650 					bwrite(bp);
651 				else
652 					bawrite(bp);
653 			}
654 		}
655 		/*
656 		 * All blocks are direct blocks
657 		 */
658 		if (flags & BA_METAONLY)
659 			panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
660 		nb = dp->di_extb[lbn];
661 		if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
662 			error = bread_gb(vp, -1 - lbn, fs->fs_bsize, NOCRED,
663 			    gbflags, &bp);
664 			if (error) {
665 				return (error);
666 			}
667 			bp->b_blkno = fsbtodb(fs, nb);
668 			bp->b_xflags |= BX_ALTDATA;
669 			*bpp = bp;
670 			return (0);
671 		}
672 		if (nb != 0) {
673 			/*
674 			 * Consider need to reallocate a fragment.
675 			 */
676 			osize = fragroundup(fs, blkoff(fs, dp->di_extsize));
677 			nsize = fragroundup(fs, size);
678 			if (nsize <= osize) {
679 				error = bread_gb(vp, -1 - lbn, osize, NOCRED,
680 				    gbflags, &bp);
681 				if (error) {
682 					return (error);
683 				}
684 				bp->b_blkno = fsbtodb(fs, nb);
685 				bp->b_xflags |= BX_ALTDATA;
686 			} else {
687 				UFS_LOCK(ump);
688 				error = ffs_realloccg(ip, -1 - lbn,
689 				    dp->di_extb[lbn],
690 				    ffs_blkpref_ufs2(ip, lbn, (int)lbn,
691 				    &dp->di_extb[0]), osize, nsize, flags,
692 				    cred, &bp);
693 				if (error)
694 					return (error);
695 				bp->b_xflags |= BX_ALTDATA;
696 				if (DOINGSOFTDEP(vp))
697 					softdep_setup_allocext(ip, lbn,
698 					    dbtofsb(fs, bp->b_blkno), nb,
699 					    nsize, osize, bp);
700 			}
701 		} else {
702 			if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
703 				nsize = fragroundup(fs, size);
704 			else
705 				nsize = fs->fs_bsize;
706 			UFS_LOCK(ump);
707 			error = ffs_alloc(ip, lbn,
708 			   ffs_blkpref_ufs2(ip, lbn, (int)lbn, &dp->di_extb[0]),
709 			   nsize, flags, cred, &newb);
710 			if (error)
711 				return (error);
712 			bp = getblk(vp, -1 - lbn, nsize, 0, 0, gbflags);
713 			bp->b_blkno = fsbtodb(fs, newb);
714 			bp->b_xflags |= BX_ALTDATA;
715 			if (flags & BA_CLRBUF)
716 				vfs_bio_clrbuf(bp);
717 			if (DOINGSOFTDEP(vp))
718 				softdep_setup_allocext(ip, lbn, newb, 0,
719 				    nsize, 0, bp);
720 		}
721 		dp->di_extb[lbn] = dbtofsb(fs, bp->b_blkno);
722 		ip->i_flag |= IN_CHANGE;
723 		*bpp = bp;
724 		return (0);
725 	}
726 	/*
727 	 * If the next write will extend the file into a new block,
728 	 * and the file is currently composed of a fragment
729 	 * this fragment has to be extended to be a full block.
730 	 */
731 	lastlbn = lblkno(fs, ip->i_size);
732 	if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
733 		nb = lastlbn;
734 		osize = blksize(fs, ip, nb);
735 		if (osize < fs->fs_bsize && osize > 0) {
736 			UFS_LOCK(ump);
737 			error = ffs_realloccg(ip, nb, dp->di_db[nb],
738 			    ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
739 			    &dp->di_db[0]), osize, (int)fs->fs_bsize,
740 			    flags, cred, &bp);
741 			if (error)
742 				return (error);
743 			if (DOINGSOFTDEP(vp))
744 				softdep_setup_allocdirect(ip, nb,
745 				    dbtofsb(fs, bp->b_blkno),
746 				    dp->di_db[nb],
747 				    fs->fs_bsize, osize, bp);
748 			ip->i_size = smalllblktosize(fs, nb + 1);
749 			dp->di_size = ip->i_size;
750 			dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
751 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
752 			if (flags & IO_SYNC)
753 				bwrite(bp);
754 			else
755 				bawrite(bp);
756 		}
757 	}
758 	/*
759 	 * The first UFS_NDADDR blocks are direct blocks
760 	 */
761 	if (lbn < UFS_NDADDR) {
762 		if (flags & BA_METAONLY)
763 			panic("ffs_balloc_ufs2: BA_METAONLY for direct block");
764 		nb = dp->di_db[lbn];
765 		if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
766 			error = bread_gb(vp, lbn, fs->fs_bsize, NOCRED,
767 			    gbflags, &bp);
768 			if (error) {
769 				return (error);
770 			}
771 			bp->b_blkno = fsbtodb(fs, nb);
772 			*bpp = bp;
773 			return (0);
774 		}
775 		if (nb != 0) {
776 			/*
777 			 * Consider need to reallocate a fragment.
778 			 */
779 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
780 			nsize = fragroundup(fs, size);
781 			if (nsize <= osize) {
782 				error = bread_gb(vp, lbn, osize, NOCRED,
783 				    gbflags, &bp);
784 				if (error) {
785 					return (error);
786 				}
787 				bp->b_blkno = fsbtodb(fs, nb);
788 			} else {
789 				UFS_LOCK(ump);
790 				error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
791 				    ffs_blkpref_ufs2(ip, lbn, (int)lbn,
792 				    &dp->di_db[0]), osize, nsize, flags,
793 				    cred, &bp);
794 				if (error)
795 					return (error);
796 				if (DOINGSOFTDEP(vp))
797 					softdep_setup_allocdirect(ip, lbn,
798 					    dbtofsb(fs, bp->b_blkno), nb,
799 					    nsize, osize, bp);
800 			}
801 		} else {
802 			if (ip->i_size < smalllblktosize(fs, lbn + 1))
803 				nsize = fragroundup(fs, size);
804 			else
805 				nsize = fs->fs_bsize;
806 			UFS_LOCK(ump);
807 			error = ffs_alloc(ip, lbn,
808 			    ffs_blkpref_ufs2(ip, lbn, (int)lbn,
809 				&dp->di_db[0]), nsize, flags, cred, &newb);
810 			if (error)
811 				return (error);
812 			bp = getblk(vp, lbn, nsize, 0, 0, gbflags);
813 			bp->b_blkno = fsbtodb(fs, newb);
814 			if (flags & BA_CLRBUF)
815 				vfs_bio_clrbuf(bp);
816 			if (DOINGSOFTDEP(vp))
817 				softdep_setup_allocdirect(ip, lbn, newb, 0,
818 				    nsize, 0, bp);
819 		}
820 		dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
821 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
822 		*bpp = bp;
823 		return (0);
824 	}
825 	/*
826 	 * Determine the number of levels of indirection.
827 	 */
828 	pref = 0;
829 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
830 		return(error);
831 #ifdef INVARIANTS
832 	if (num < 1)
833 		panic ("ffs_balloc_ufs2: ufs_getlbns returned indirect block");
834 #endif
835 	saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
836 	/*
837 	 * Fetch the first indirect block allocating if necessary.
838 	 */
839 	--num;
840 	nb = dp->di_ib[indirs[0].in_off];
841 	allocib = NULL;
842 	allocblk = allociblk;
843 	lbns_remfree = lbns;
844 	if (nb == 0) {
845 		UFS_LOCK(ump);
846 		pref = ffs_blkpref_ufs2(ip, lbn, -indirs[0].in_off - 1,
847 		    (ufs2_daddr_t *)0);
848 		if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
849 		    flags, cred, &newb)) != 0) {
850 			curthread_pflags_restore(saved_inbdflush);
851 			return (error);
852 		}
853 		pref = newb + fs->fs_frag;
854 		nb = newb;
855 		MPASS(allocblk < allociblk + nitems(allociblk));
856 		MPASS(lbns_remfree < lbns + nitems(lbns));
857 		*allocblk++ = nb;
858 		*lbns_remfree++ = indirs[1].in_lbn;
859 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0,
860 		    GB_UNMAPPED);
861 		bp->b_blkno = fsbtodb(fs, nb);
862 		vfs_bio_clrbuf(bp);
863 		if (DOINGSOFTDEP(vp)) {
864 			softdep_setup_allocdirect(ip,
865 			    UFS_NDADDR + indirs[0].in_off, newb, 0,
866 			    fs->fs_bsize, 0, bp);
867 			bdwrite(bp);
868 		} else if ((flags & IO_SYNC) == 0 && DOINGASYNC(vp)) {
869 			if (bp->b_bufsize == fs->fs_bsize)
870 				bp->b_flags |= B_CLUSTEROK;
871 			bdwrite(bp);
872 		} else {
873 			if ((error = bwrite(bp)) != 0)
874 				goto fail;
875 		}
876 		allocib = &dp->di_ib[indirs[0].in_off];
877 		*allocib = nb;
878 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
879 	}
880 	/*
881 	 * Fetch through the indirect blocks, allocating as necessary.
882 	 */
883 retry:
884 	for (i = 1;;) {
885 		error = bread(vp,
886 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
887 		if (error) {
888 			goto fail;
889 		}
890 		bap = (ufs2_daddr_t *)bp->b_data;
891 		nb = bap[indirs[i].in_off];
892 		if ((error = UFS_CHECK_BLKNO(mp, ip->i_number, nb,
893 		    fs->fs_bsize)) != 0) {
894 			brelse(bp);
895 			goto fail;
896 		}
897 		if (i == num)
898 			break;
899 		i += 1;
900 		if (nb != 0) {
901 			bqrelse(bp);
902 			continue;
903 		}
904 		UFS_LOCK(ump);
905 		/*
906 		 * If parent indirect has just been allocated, try to cluster
907 		 * immediately following it.
908 		 */
909 		if (pref == 0)
910 			pref = ffs_blkpref_ufs2(ip, lbn, i - num - 1,
911 			    (ufs2_daddr_t *)0);
912 		if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
913 		    flags | IO_BUFLOCKED, cred, &newb)) != 0) {
914 			brelse(bp);
915 			UFS_LOCK(ump);
916 			if (DOINGSOFTDEP(vp) && ++reclaimed == 1) {
917 				softdep_request_cleanup(fs, vp, cred,
918 				    FLUSH_BLOCKS_WAIT);
919 				UFS_UNLOCK(ump);
920 				goto retry;
921 			}
922 			if (ppsratecheck(&ump->um_last_fullmsg,
923 			    &ump->um_secs_fullmsg, 1)) {
924 				UFS_UNLOCK(ump);
925 				ffs_fserr(fs, ip->i_number, "filesystem full");
926 				uprintf("\n%s: write failed, filesystem "
927 				    "is full\n", fs->fs_fsmnt);
928 			} else {
929 				UFS_UNLOCK(ump);
930 			}
931 			goto fail;
932 		}
933 		pref = newb + fs->fs_frag;
934 		nb = newb;
935 		MPASS(allocblk < allociblk + nitems(allociblk));
936 		MPASS(lbns_remfree < lbns + nitems(lbns));
937 		*allocblk++ = nb;
938 		*lbns_remfree++ = indirs[i].in_lbn;
939 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0,
940 		    GB_UNMAPPED);
941 		nbp->b_blkno = fsbtodb(fs, nb);
942 		vfs_bio_clrbuf(nbp);
943 		if (DOINGSOFTDEP(vp)) {
944 			softdep_setup_allocindir_meta(nbp, ip, bp,
945 			    indirs[i - 1].in_off, nb);
946 			bdwrite(nbp);
947 		} else if ((flags & IO_SYNC) == 0 && DOINGASYNC(vp)) {
948 			if (nbp->b_bufsize == fs->fs_bsize)
949 				nbp->b_flags |= B_CLUSTEROK;
950 			bdwrite(nbp);
951 		} else {
952 			if ((error = bwrite(nbp)) != 0) {
953 				brelse(bp);
954 				goto fail;
955 			}
956 		}
957 		bap[indirs[i - 1].in_off] = nb;
958 		if (allocib == NULL && unwindidx < 0)
959 			unwindidx = i - 1;
960 		/*
961 		 * If required, write synchronously, otherwise use
962 		 * delayed write.
963 		 */
964 		if (flags & IO_SYNC) {
965 			bwrite(bp);
966 		} else {
967 			if (bp->b_bufsize == fs->fs_bsize)
968 				bp->b_flags |= B_CLUSTEROK;
969 			bdwrite(bp);
970 		}
971 	}
972 	/*
973 	 * If asked only for the indirect block, then return it.
974 	 */
975 	if (flags & BA_METAONLY) {
976 		curthread_pflags_restore(saved_inbdflush);
977 		*bpp = bp;
978 		return (0);
979 	}
980 	/*
981 	 * Get the data block, allocating if necessary.
982 	 */
983 	if (nb == 0) {
984 		UFS_LOCK(ump);
985 		/*
986 		 * If allocating metadata at the front of the cylinder
987 		 * group and parent indirect block has just been allocated,
988 		 * then cluster next to it if it is the first indirect in
989 		 * the file. Otherwise it has been allocated in the metadata
990 		 * area, so we want to find our own place out in the data area.
991 		 */
992 		if (pref == 0 || (lbn > UFS_NDADDR && fs->fs_metaspace != 0))
993 			pref = ffs_blkpref_ufs2(ip, lbn, indirs[i].in_off,
994 			    &bap[0]);
995 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
996 		    flags | IO_BUFLOCKED, cred, &newb);
997 		if (error) {
998 			brelse(bp);
999 			UFS_LOCK(ump);
1000 			if (DOINGSOFTDEP(vp) && ++reclaimed == 1) {
1001 				softdep_request_cleanup(fs, vp, cred,
1002 				    FLUSH_BLOCKS_WAIT);
1003 				UFS_UNLOCK(ump);
1004 				goto retry;
1005 			}
1006 			if (ppsratecheck(&ump->um_last_fullmsg,
1007 			    &ump->um_secs_fullmsg, 1)) {
1008 				UFS_UNLOCK(ump);
1009 				ffs_fserr(fs, ip->i_number, "filesystem full");
1010 				uprintf("\n%s: write failed, filesystem "
1011 				    "is full\n", fs->fs_fsmnt);
1012 			} else {
1013 				UFS_UNLOCK(ump);
1014 			}
1015 			goto fail;
1016 		}
1017 		nb = newb;
1018 		MPASS(allocblk < allociblk + nitems(allociblk));
1019 		MPASS(lbns_remfree < lbns + nitems(lbns));
1020 		*allocblk++ = nb;
1021 		*lbns_remfree++ = lbn;
1022 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
1023 		nbp->b_blkno = fsbtodb(fs, nb);
1024 		if (flags & BA_CLRBUF)
1025 			vfs_bio_clrbuf(nbp);
1026 		if (DOINGSOFTDEP(vp))
1027 			softdep_setup_allocindir_page(ip, lbn, bp,
1028 			    indirs[i].in_off, nb, 0, nbp);
1029 		bap[indirs[i].in_off] = nb;
1030 		/*
1031 		 * If required, write synchronously, otherwise use
1032 		 * delayed write.
1033 		 */
1034 		if (flags & IO_SYNC) {
1035 			bwrite(bp);
1036 		} else {
1037 			if (bp->b_bufsize == fs->fs_bsize)
1038 				bp->b_flags |= B_CLUSTEROK;
1039 			bdwrite(bp);
1040 		}
1041 		curthread_pflags_restore(saved_inbdflush);
1042 		*bpp = nbp;
1043 		return (0);
1044 	}
1045 	brelse(bp);
1046 	/*
1047 	 * If requested clear invalid portions of the buffer.  If we
1048 	 * have to do a read-before-write (typical if BA_CLRBUF is set),
1049 	 * try to do some read-ahead in the sequential case to reduce
1050 	 * the number of I/O transactions.
1051 	 */
1052 	if (flags & BA_CLRBUF) {
1053 		int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
1054 		if (seqcount != 0 &&
1055 		    (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0 &&
1056 		    !(vm_page_count_severe() || buf_dirty_count_severe())) {
1057 			error = cluster_read(vp, ip->i_size, lbn,
1058 			    (int)fs->fs_bsize, NOCRED,
1059 			    MAXBSIZE, seqcount, gbflags, &nbp);
1060 		} else {
1061 			error = bread_gb(vp, lbn, (int)fs->fs_bsize,
1062 			    NOCRED, gbflags, &nbp);
1063 		}
1064 		if (error) {
1065 			brelse(nbp);
1066 			goto fail;
1067 		}
1068 	} else {
1069 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
1070 		nbp->b_blkno = fsbtodb(fs, nb);
1071 	}
1072 	curthread_pflags_restore(saved_inbdflush);
1073 	*bpp = nbp;
1074 	return (0);
1075 fail:
1076 	curthread_pflags_restore(saved_inbdflush);
1077 	/*
1078 	 * If we have failed to allocate any blocks, simply return the error.
1079 	 * This is the usual case and avoids the need to fsync the file.
1080 	 */
1081 	if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
1082 		return (error);
1083 	/*
1084 	 * If we have failed part way through block allocation, we
1085 	 * have to deallocate any indirect blocks that we have allocated.
1086 	 * We have to fsync the file before we start to get rid of all
1087 	 * of its dependencies so that we do not leave them dangling.
1088 	 * We have to sync it at the end so that the soft updates code
1089 	 * does not find any untracked changes. Although this is really
1090 	 * slow, running out of disk space is not expected to be a common
1091 	 * occurrence. The error return from fsync is ignored as we already
1092 	 * have an error to return to the user.
1093 	 *
1094 	 * XXX Still have to journal the free below
1095 	 */
1096 	(void) ffs_syncvnode(vp, MNT_WAIT, 0);
1097 	for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
1098 	     blkp < allocblk; blkp++, lbns_remfree++) {
1099 		/*
1100 		 * We shall not leave the freed blocks on the vnode
1101 		 * buffer object lists.
1102 		 */
1103 		bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0,
1104 		    GB_NOCREAT | GB_UNMAPPED);
1105 		if (bp != NULL) {
1106 			KASSERT(bp->b_blkno == fsbtodb(fs, *blkp),
1107 			    ("mismatch2 l %jd %jd b %ju %ju",
1108 			    (intmax_t)bp->b_lblkno, (uintmax_t)*lbns_remfree,
1109 			    (uintmax_t)bp->b_blkno,
1110 			    (uintmax_t)fsbtodb(fs, *blkp)));
1111 			bp->b_flags |= B_INVAL | B_RELBUF | B_NOCACHE;
1112 			bp->b_flags &= ~(B_ASYNC | B_CACHE);
1113 			brelse(bp);
1114 		}
1115 		deallocated += fs->fs_bsize;
1116 	}
1117 	if (allocib != NULL) {
1118 		*allocib = 0;
1119 	} else if (unwindidx >= 0) {
1120 		int r;
1121 
1122 		r = bread(vp, indirs[unwindidx].in_lbn,
1123 		    (int)fs->fs_bsize, NOCRED, &bp);
1124 		if (r) {
1125 			panic("Could not unwind indirect block, error %d", r);
1126 			brelse(bp);
1127 		} else {
1128 			bap = (ufs2_daddr_t *)bp->b_data;
1129 			bap[indirs[unwindidx].in_off] = 0;
1130 			if (flags & IO_SYNC) {
1131 				bwrite(bp);
1132 			} else {
1133 				if (bp->b_bufsize == fs->fs_bsize)
1134 					bp->b_flags |= B_CLUSTEROK;
1135 				bdwrite(bp);
1136 			}
1137 		}
1138 	}
1139 	if (deallocated) {
1140 #ifdef QUOTA
1141 		/*
1142 		 * Restore user's disk quota because allocation failed.
1143 		 */
1144 		(void) chkdq(ip, -btodb(deallocated), cred, FORCE);
1145 #endif
1146 		dp->di_blocks -= btodb(deallocated);
1147 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1148 	}
1149 	(void) ffs_syncvnode(vp, MNT_WAIT, 0);
1150 	/*
1151 	 * After the buffers are invalidated and on-disk pointers are
1152 	 * cleared, free the blocks.
1153 	 */
1154 	for (blkp = allociblk; blkp < allocblk; blkp++) {
1155 #ifdef INVARIANTS
1156 		if (blkp == allociblk)
1157 			lbns_remfree = lbns;
1158 		bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0,
1159 		    GB_NOCREAT | GB_UNMAPPED);
1160 		if (bp != NULL) {
1161 			panic("zombie2 %jd %ju %ju",
1162 			    (intmax_t)bp->b_lblkno, (uintmax_t)bp->b_blkno,
1163 			    (uintmax_t)fsbtodb(fs, *blkp));
1164 		}
1165 		lbns_remfree++;
1166 #endif
1167 		ffs_blkfree(ump, fs, ump->um_devvp, *blkp, fs->fs_bsize,
1168 		    ip->i_number, vp->v_type, NULL, SINGLETON_KEY);
1169 	}
1170 	return (error);
1171 }
1172