2 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
65 #include <sys/param.h>
66 #include <sys/systm.h>
70 #include <sys/mount.h>
71 #include <sys/vnode.h>
73 #include <ufs/ufs/quota.h>
74 #include <ufs/ufs/inode.h>
75 #include <ufs/ufs/ufs_extern.h>
76 #include <ufs/ufs/extattr.h>
77 #include <ufs/ufs/ufsmount.h>
79 #include <ufs/ffs/fs.h>
80 #include <ufs/ffs/ffs_extern.h>
83 * Balloc defines the structure of filesystem storage
84 * by allocating the physical blocks on a device given
85 * the inode and the logical block number in a file.
86 * This is the allocation strategy for UFS1. Below is
87 * the allocation strategy for UFS2.
90 ffs_balloc_ufs1(struct vnode *vp, off_t startoffset, int size,
91 struct ucred *cred, int flags, struct buf **bpp)
94 struct ufs1_dinode *dp;
95 ufs_lbn_t lbn, lastlbn;
100 struct indir indirs[NIADDR + 2];
101 int deallocated, osize, nsize, num, i, error;
103 ufs1_daddr_t *bap, pref;
104 ufs1_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
105 ufs2_daddr_t *lbns_remfree, lbns[NIADDR + 1];
113 lbn = lblkno(fs, startoffset);
114 size = blkoff(fs, startoffset) + size;
115 if (size > fs->fs_bsize)
116 panic("ffs_balloc_ufs1: blk too big");
124 * If the next write will extend the file into a new block,
125 * and the file is currently composed of a fragment
126 * this fragment has to be extended to be a full block.
128 lastlbn = lblkno(fs, ip->i_size);
129 if (lastlbn < NDADDR && lastlbn < lbn) {
131 osize = blksize(fs, ip, nb);
132 if (osize < fs->fs_bsize && osize > 0) {
134 error = ffs_realloccg(ip, nb, dp->di_db[nb],
135 ffs_blkpref_ufs1(ip, lastlbn, (int)nb,
136 &dp->di_db[0]), osize, (int)fs->fs_bsize, flags,
140 if (DOINGSOFTDEP(vp))
141 softdep_setup_allocdirect(ip, nb,
142 dbtofsb(fs, bp->b_blkno), dp->di_db[nb],
143 fs->fs_bsize, osize, bp);
144 ip->i_size = smalllblktosize(fs, nb + 1);
145 dp->di_size = ip->i_size;
146 dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
147 ip->i_flag |= IN_CHANGE | IN_UPDATE;
155 * The first NDADDR blocks are direct blocks
158 if (flags & BA_METAONLY)
159 panic("ffs_balloc_ufs1: BA_METAONLY for direct block");
161 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
162 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
167 bp->b_blkno = fsbtodb(fs, nb);
173 * Consider need to reallocate a fragment.
175 osize = fragroundup(fs, blkoff(fs, ip->i_size));
176 nsize = fragroundup(fs, size);
177 if (nsize <= osize) {
178 error = bread(vp, lbn, osize, NOCRED, &bp);
183 bp->b_blkno = fsbtodb(fs, nb);
186 error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
187 ffs_blkpref_ufs1(ip, lbn, (int)lbn,
188 &dp->di_db[0]), osize, nsize, flags,
192 if (DOINGSOFTDEP(vp))
193 softdep_setup_allocdirect(ip, lbn,
194 dbtofsb(fs, bp->b_blkno), nb,
198 if (ip->i_size < smalllblktosize(fs, lbn + 1))
199 nsize = fragroundup(fs, size);
201 nsize = fs->fs_bsize;
203 error = ffs_alloc(ip, lbn,
204 ffs_blkpref_ufs1(ip, lbn, (int)lbn, &dp->di_db[0]),
205 nsize, flags, cred, &newb);
208 bp = getblk(vp, lbn, nsize, 0, 0, 0);
209 bp->b_blkno = fsbtodb(fs, newb);
210 if (flags & BA_CLRBUF)
212 if (DOINGSOFTDEP(vp))
213 softdep_setup_allocdirect(ip, lbn, newb, 0,
216 dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
217 ip->i_flag |= IN_CHANGE | IN_UPDATE;
222 * Determine the number of levels of indirection.
225 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
229 panic ("ffs_balloc_ufs1: ufs_getlbns returned indirect block");
231 saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
233 * Fetch the first indirect block allocating if necessary.
236 nb = dp->di_ib[indirs[0].in_off];
238 allocblk = allociblk;
242 pref = ffs_blkpref_ufs1(ip, lbn, 0, (ufs1_daddr_t *)0);
243 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
244 flags, cred, &newb)) != 0) {
245 curthread_pflags_restore(saved_inbdflush);
250 *lbns_remfree++ = indirs[1].in_lbn;
251 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, 0);
252 bp->b_blkno = fsbtodb(fs, nb);
254 if (DOINGSOFTDEP(vp)) {
255 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
256 newb, 0, fs->fs_bsize, 0, bp);
260 * Write synchronously so that indirect blocks
261 * never point at garbage.
265 else if ((error = bwrite(bp)) != 0)
268 allocib = &dp->di_ib[indirs[0].in_off];
270 ip->i_flag |= IN_CHANGE | IN_UPDATE;
273 * Fetch through the indirect blocks, allocating as necessary.
277 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
282 bap = (ufs1_daddr_t *)bp->b_data;
283 nb = bap[indirs[i].in_off];
293 pref = ffs_blkpref_ufs1(ip, lbn, 0, (ufs1_daddr_t *)0);
294 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
295 flags, cred, &newb)) != 0) {
301 *lbns_remfree++ = indirs[i].in_lbn;
302 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
303 nbp->b_blkno = fsbtodb(fs, nb);
305 if (DOINGSOFTDEP(vp)) {
306 softdep_setup_allocindir_meta(nbp, ip, bp,
307 indirs[i - 1].in_off, nb);
311 * Write synchronously so that indirect blocks
312 * never point at garbage.
314 if ((error = bwrite(nbp)) != 0) {
319 bap[indirs[i - 1].in_off] = nb;
320 if (allocib == NULL && unwindidx < 0)
323 * If required, write synchronously, otherwise use
326 if (flags & IO_SYNC) {
329 if (bp->b_bufsize == fs->fs_bsize)
330 bp->b_flags |= B_CLUSTEROK;
335 * If asked only for the indirect block, then return it.
337 if (flags & BA_METAONLY) {
338 curthread_pflags_restore(saved_inbdflush);
343 * Get the data block, allocating if necessary.
347 pref = ffs_blkpref_ufs1(ip, lbn, indirs[i].in_off, &bap[0]);
348 error = ffs_alloc(ip,
349 lbn, pref, (int)fs->fs_bsize, flags, cred, &newb);
356 *lbns_remfree++ = lbn;
357 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, 0);
358 nbp->b_blkno = fsbtodb(fs, nb);
359 if (flags & BA_CLRBUF)
361 if (DOINGSOFTDEP(vp))
362 softdep_setup_allocindir_page(ip, lbn, bp,
363 indirs[i].in_off, nb, 0, nbp);
364 bap[indirs[i].in_off] = nb;
366 * If required, write synchronously, otherwise use
369 if (flags & IO_SYNC) {
372 if (bp->b_bufsize == fs->fs_bsize)
373 bp->b_flags |= B_CLUSTEROK;
376 curthread_pflags_restore(saved_inbdflush);
381 if (flags & BA_CLRBUF) {
382 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
383 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
384 error = cluster_read(vp, ip->i_size, lbn,
385 (int)fs->fs_bsize, NOCRED,
386 MAXBSIZE, seqcount, &nbp);
388 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
395 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, 0);
396 nbp->b_blkno = fsbtodb(fs, nb);
398 curthread_pflags_restore(saved_inbdflush);
402 curthread_pflags_restore(saved_inbdflush);
404 * If we have failed to allocate any blocks, simply return the error.
405 * This is the usual case and avoids the need to fsync the file.
407 if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
410 * If we have failed part way through block allocation, we
411 * have to deallocate any indirect blocks that we have allocated.
412 * We have to fsync the file before we start to get rid of all
413 * of its dependencies so that we do not leave them dangling.
414 * We have to sync it at the end so that the soft updates code
415 * does not find any untracked changes. Although this is really
416 * slow, running out of disk space is not expected to be a common
417 * occurence. The error return from fsync is ignored as we already
418 * have an error to return to the user.
420 (void) ffs_syncvnode(vp, MNT_WAIT);
421 for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
422 blkp < allocblk; blkp++, lbns_remfree++) {
424 * We shall not leave the freed blocks on the vnode
425 * buffer object lists.
427 bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0, GB_NOCREAT);
429 bp->b_flags |= (B_INVAL | B_RELBUF);
430 bp->b_flags &= ~B_ASYNC;
433 deallocated += fs->fs_bsize;
435 if (allocib != NULL) {
437 } else if (unwindidx >= 0) {
440 r = bread(vp, indirs[unwindidx].in_lbn,
441 (int)fs->fs_bsize, NOCRED, &bp);
443 panic("Could not unwind indirect block, error %d", r);
446 bap = (ufs1_daddr_t *)bp->b_data;
447 bap[indirs[unwindidx].in_off] = 0;
448 if (flags & IO_SYNC) {
451 if (bp->b_bufsize == fs->fs_bsize)
452 bp->b_flags |= B_CLUSTEROK;
460 * Restore user's disk quota because allocation failed.
462 (void) chkdq(ip, -btodb(deallocated), cred, FORCE);
464 dp->di_blocks -= btodb(deallocated);
465 ip->i_flag |= IN_CHANGE | IN_UPDATE;
467 (void) ffs_syncvnode(vp, MNT_WAIT);
469 * After the buffers are invalidated and on-disk pointers are
470 * cleared, free the blocks.
472 for (blkp = allociblk; blkp < allocblk; blkp++) {
473 ffs_blkfree(ump, fs, ip->i_devvp, *blkp, fs->fs_bsize,
480 * Balloc defines the structure of file system storage
481 * by allocating the physical blocks on a device given
482 * the inode and the logical block number in a file.
483 * This is the allocation strategy for UFS2. Above is
484 * the allocation strategy for UFS1.
487 ffs_balloc_ufs2(struct vnode *vp, off_t startoffset, int size,
488 struct ucred *cred, int flags, struct buf **bpp)
491 struct ufs2_dinode *dp;
492 ufs_lbn_t lbn, lastlbn;
494 struct buf *bp, *nbp;
495 struct ufsmount *ump;
496 struct indir indirs[NIADDR + 2];
497 ufs2_daddr_t nb, newb, *bap, pref;
498 ufs2_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
499 ufs2_daddr_t *lbns_remfree, lbns[NIADDR + 1];
500 int deallocated, osize, nsize, num, i, error;
508 lbn = lblkno(fs, startoffset);
509 size = blkoff(fs, startoffset) + size;
510 if (size > fs->fs_bsize)
511 panic("ffs_balloc_ufs2: blk too big");
517 * Check for allocating external data.
519 if (flags & IO_EXT) {
523 * If the next write will extend the data into a new block,
524 * and the data is currently composed of a fragment
525 * this fragment has to be extended to be a full block.
527 lastlbn = lblkno(fs, dp->di_extsize);
530 osize = sblksize(fs, dp->di_extsize, nb);
531 if (osize < fs->fs_bsize && osize > 0) {
533 error = ffs_realloccg(ip, -1 - nb,
535 ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
536 &dp->di_extb[0]), osize,
537 (int)fs->fs_bsize, flags, cred, &bp);
540 if (DOINGSOFTDEP(vp))
541 softdep_setup_allocext(ip, nb,
542 dbtofsb(fs, bp->b_blkno),
544 fs->fs_bsize, osize, bp);
545 dp->di_extsize = smalllblktosize(fs, nb + 1);
546 dp->di_extb[nb] = dbtofsb(fs, bp->b_blkno);
547 bp->b_xflags |= BX_ALTDATA;
548 ip->i_flag |= IN_CHANGE;
556 * All blocks are direct blocks
558 if (flags & BA_METAONLY)
559 panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
560 nb = dp->di_extb[lbn];
561 if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
562 error = bread(vp, -1 - lbn, fs->fs_bsize, NOCRED, &bp);
567 bp->b_blkno = fsbtodb(fs, nb);
568 bp->b_xflags |= BX_ALTDATA;
574 * Consider need to reallocate a fragment.
576 osize = fragroundup(fs, blkoff(fs, dp->di_extsize));
577 nsize = fragroundup(fs, size);
578 if (nsize <= osize) {
579 error = bread(vp, -1 - lbn, osize, NOCRED, &bp);
584 bp->b_blkno = fsbtodb(fs, nb);
585 bp->b_xflags |= BX_ALTDATA;
588 error = ffs_realloccg(ip, -1 - lbn,
590 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
591 &dp->di_extb[0]), osize, nsize, flags,
595 bp->b_xflags |= BX_ALTDATA;
596 if (DOINGSOFTDEP(vp))
597 softdep_setup_allocext(ip, lbn,
598 dbtofsb(fs, bp->b_blkno), nb,
602 if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
603 nsize = fragroundup(fs, size);
605 nsize = fs->fs_bsize;
607 error = ffs_alloc(ip, lbn,
608 ffs_blkpref_ufs2(ip, lbn, (int)lbn, &dp->di_extb[0]),
609 nsize, flags, cred, &newb);
612 bp = getblk(vp, -1 - lbn, nsize, 0, 0, 0);
613 bp->b_blkno = fsbtodb(fs, newb);
614 bp->b_xflags |= BX_ALTDATA;
615 if (flags & BA_CLRBUF)
617 if (DOINGSOFTDEP(vp))
618 softdep_setup_allocext(ip, lbn, newb, 0,
621 dp->di_extb[lbn] = dbtofsb(fs, bp->b_blkno);
622 ip->i_flag |= IN_CHANGE;
627 * If the next write will extend the file into a new block,
628 * and the file is currently composed of a fragment
629 * this fragment has to be extended to be a full block.
631 lastlbn = lblkno(fs, ip->i_size);
632 if (lastlbn < NDADDR && lastlbn < lbn) {
634 osize = blksize(fs, ip, nb);
635 if (osize < fs->fs_bsize && osize > 0) {
637 error = ffs_realloccg(ip, nb, dp->di_db[nb],
638 ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
639 &dp->di_db[0]), osize, (int)fs->fs_bsize,
643 if (DOINGSOFTDEP(vp))
644 softdep_setup_allocdirect(ip, nb,
645 dbtofsb(fs, bp->b_blkno),
647 fs->fs_bsize, osize, bp);
648 ip->i_size = smalllblktosize(fs, nb + 1);
649 dp->di_size = ip->i_size;
650 dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
651 ip->i_flag |= IN_CHANGE | IN_UPDATE;
659 * The first NDADDR blocks are direct blocks
662 if (flags & BA_METAONLY)
663 panic("ffs_balloc_ufs2: BA_METAONLY for direct block");
665 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
666 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
671 bp->b_blkno = fsbtodb(fs, nb);
677 * Consider need to reallocate a fragment.
679 osize = fragroundup(fs, blkoff(fs, ip->i_size));
680 nsize = fragroundup(fs, size);
681 if (nsize <= osize) {
682 error = bread(vp, lbn, osize, NOCRED, &bp);
687 bp->b_blkno = fsbtodb(fs, nb);
690 error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
691 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
692 &dp->di_db[0]), osize, nsize, flags,
696 if (DOINGSOFTDEP(vp))
697 softdep_setup_allocdirect(ip, lbn,
698 dbtofsb(fs, bp->b_blkno), nb,
702 if (ip->i_size < smalllblktosize(fs, lbn + 1))
703 nsize = fragroundup(fs, size);
705 nsize = fs->fs_bsize;
707 error = ffs_alloc(ip, lbn,
708 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
709 &dp->di_db[0]), nsize, flags, cred, &newb);
712 bp = getblk(vp, lbn, nsize, 0, 0, 0);
713 bp->b_blkno = fsbtodb(fs, newb);
714 if (flags & BA_CLRBUF)
716 if (DOINGSOFTDEP(vp))
717 softdep_setup_allocdirect(ip, lbn, newb, 0,
720 dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
721 ip->i_flag |= IN_CHANGE | IN_UPDATE;
726 * Determine the number of levels of indirection.
729 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
733 panic ("ffs_balloc_ufs2: ufs_getlbns returned indirect block");
735 saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
737 * Fetch the first indirect block allocating if necessary.
740 nb = dp->di_ib[indirs[0].in_off];
742 allocblk = allociblk;
746 pref = ffs_blkpref_ufs2(ip, lbn, 0, (ufs2_daddr_t *)0);
747 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
748 flags, cred, &newb)) != 0) {
749 curthread_pflags_restore(saved_inbdflush);
754 *lbns_remfree++ = indirs[1].in_lbn;
755 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, 0);
756 bp->b_blkno = fsbtodb(fs, nb);
758 if (DOINGSOFTDEP(vp)) {
759 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
760 newb, 0, fs->fs_bsize, 0, bp);
764 * Write synchronously so that indirect blocks
765 * never point at garbage.
769 else if ((error = bwrite(bp)) != 0)
772 allocib = &dp->di_ib[indirs[0].in_off];
774 ip->i_flag |= IN_CHANGE | IN_UPDATE;
777 * Fetch through the indirect blocks, allocating as necessary.
781 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
786 bap = (ufs2_daddr_t *)bp->b_data;
787 nb = bap[indirs[i].in_off];
797 pref = ffs_blkpref_ufs2(ip, lbn, 0, (ufs2_daddr_t *)0);
798 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
799 flags, cred, &newb)) != 0) {
805 *lbns_remfree++ = indirs[i].in_lbn;
806 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
807 nbp->b_blkno = fsbtodb(fs, nb);
809 if (DOINGSOFTDEP(vp)) {
810 softdep_setup_allocindir_meta(nbp, ip, bp,
811 indirs[i - 1].in_off, nb);
815 * Write synchronously so that indirect blocks
816 * never point at garbage.
818 if ((error = bwrite(nbp)) != 0) {
823 bap[indirs[i - 1].in_off] = nb;
824 if (allocib == NULL && unwindidx < 0)
827 * If required, write synchronously, otherwise use
830 if (flags & IO_SYNC) {
833 if (bp->b_bufsize == fs->fs_bsize)
834 bp->b_flags |= B_CLUSTEROK;
839 * If asked only for the indirect block, then return it.
841 if (flags & BA_METAONLY) {
842 curthread_pflags_restore(saved_inbdflush);
847 * Get the data block, allocating if necessary.
851 pref = ffs_blkpref_ufs2(ip, lbn, indirs[i].in_off, &bap[0]);
852 error = ffs_alloc(ip,
853 lbn, pref, (int)fs->fs_bsize, flags, cred, &newb);
860 *lbns_remfree++ = lbn;
861 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, 0);
862 nbp->b_blkno = fsbtodb(fs, nb);
863 if (flags & BA_CLRBUF)
865 if (DOINGSOFTDEP(vp))
866 softdep_setup_allocindir_page(ip, lbn, bp,
867 indirs[i].in_off, nb, 0, nbp);
868 bap[indirs[i].in_off] = nb;
870 * If required, write synchronously, otherwise use
873 if (flags & IO_SYNC) {
876 if (bp->b_bufsize == fs->fs_bsize)
877 bp->b_flags |= B_CLUSTEROK;
880 curthread_pflags_restore(saved_inbdflush);
886 * If requested clear invalid portions of the buffer. If we
887 * have to do a read-before-write (typical if BA_CLRBUF is set),
888 * try to do some read-ahead in the sequential case to reduce
889 * the number of I/O transactions.
891 if (flags & BA_CLRBUF) {
892 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
893 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
894 error = cluster_read(vp, ip->i_size, lbn,
895 (int)fs->fs_bsize, NOCRED,
896 MAXBSIZE, seqcount, &nbp);
898 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
905 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, 0);
906 nbp->b_blkno = fsbtodb(fs, nb);
908 curthread_pflags_restore(saved_inbdflush);
912 curthread_pflags_restore(saved_inbdflush);
914 * If we have failed to allocate any blocks, simply return the error.
915 * This is the usual case and avoids the need to fsync the file.
917 if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
920 * If we have failed part way through block allocation, we
921 * have to deallocate any indirect blocks that we have allocated.
922 * We have to fsync the file before we start to get rid of all
923 * of its dependencies so that we do not leave them dangling.
924 * We have to sync it at the end so that the soft updates code
925 * does not find any untracked changes. Although this is really
926 * slow, running out of disk space is not expected to be a common
927 * occurence. The error return from fsync is ignored as we already
928 * have an error to return to the user.
930 (void) ffs_syncvnode(vp, MNT_WAIT);
931 for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
932 blkp < allocblk; blkp++, lbns_remfree++) {
934 * We shall not leave the freed blocks on the vnode
935 * buffer object lists.
937 bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0, GB_NOCREAT);
939 bp->b_flags |= (B_INVAL | B_RELBUF);
940 bp->b_flags &= ~B_ASYNC;
943 deallocated += fs->fs_bsize;
945 if (allocib != NULL) {
947 } else if (unwindidx >= 0) {
950 r = bread(vp, indirs[unwindidx].in_lbn,
951 (int)fs->fs_bsize, NOCRED, &bp);
953 panic("Could not unwind indirect block, error %d", r);
956 bap = (ufs2_daddr_t *)bp->b_data;
957 bap[indirs[unwindidx].in_off] = 0;
958 if (flags & IO_SYNC) {
961 if (bp->b_bufsize == fs->fs_bsize)
962 bp->b_flags |= B_CLUSTEROK;
970 * Restore user's disk quota because allocation failed.
972 (void) chkdq(ip, -btodb(deallocated), cred, FORCE);
974 dp->di_blocks -= btodb(deallocated);
975 ip->i_flag |= IN_CHANGE | IN_UPDATE;
977 (void) ffs_syncvnode(vp, MNT_WAIT);
979 * After the buffers are invalidated and on-disk pointers are
980 * cleared, free the blocks.
982 for (blkp = allociblk; blkp < allocblk; blkp++) {
983 ffs_blkfree(ump, fs, ip->i_devvp, *blkp, fs->fs_bsize,