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];
108 static struct timeval lastfail;
116 lbn = lblkno(fs, startoffset);
117 size = blkoff(fs, startoffset) + size;
119 if (size > fs->fs_bsize)
120 panic("ffs_balloc_ufs1: blk too big");
127 if (DOINGSOFTDEP(vp))
128 softdep_prealloc(vp, MNT_WAIT);
130 * If the next write will extend the file into a new block,
131 * and the file is currently composed of a fragment
132 * this fragment has to be extended to be a full block.
134 lastlbn = lblkno(fs, ip->i_size);
135 if (lastlbn < NDADDR && lastlbn < lbn) {
137 osize = blksize(fs, ip, nb);
138 if (osize < fs->fs_bsize && osize > 0) {
140 error = ffs_realloccg(ip, nb, dp->di_db[nb],
141 ffs_blkpref_ufs1(ip, lastlbn, (int)nb,
142 &dp->di_db[0]), osize, (int)fs->fs_bsize, flags,
146 if (DOINGSOFTDEP(vp))
147 softdep_setup_allocdirect(ip, nb,
148 dbtofsb(fs, bp->b_blkno), dp->di_db[nb],
149 fs->fs_bsize, osize, bp);
150 ip->i_size = smalllblktosize(fs, nb + 1);
151 dp->di_size = ip->i_size;
152 dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
153 ip->i_flag |= IN_CHANGE | IN_UPDATE;
161 * The first NDADDR blocks are direct blocks
164 if (flags & BA_METAONLY)
165 panic("ffs_balloc_ufs1: BA_METAONLY for direct block");
167 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
168 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
173 bp->b_blkno = fsbtodb(fs, nb);
179 * Consider need to reallocate a fragment.
181 osize = fragroundup(fs, blkoff(fs, ip->i_size));
182 nsize = fragroundup(fs, size);
183 if (nsize <= osize) {
184 error = bread(vp, lbn, osize, NOCRED, &bp);
189 bp->b_blkno = fsbtodb(fs, nb);
192 error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
193 ffs_blkpref_ufs1(ip, lbn, (int)lbn,
194 &dp->di_db[0]), osize, nsize, flags,
198 if (DOINGSOFTDEP(vp))
199 softdep_setup_allocdirect(ip, lbn,
200 dbtofsb(fs, bp->b_blkno), nb,
204 if (ip->i_size < smalllblktosize(fs, lbn + 1))
205 nsize = fragroundup(fs, size);
207 nsize = fs->fs_bsize;
209 error = ffs_alloc(ip, lbn,
210 ffs_blkpref_ufs1(ip, lbn, (int)lbn, &dp->di_db[0]),
211 nsize, flags, cred, &newb);
214 bp = getblk(vp, lbn, nsize, 0, 0, 0);
215 bp->b_blkno = fsbtodb(fs, newb);
216 if (flags & BA_CLRBUF)
218 if (DOINGSOFTDEP(vp))
219 softdep_setup_allocdirect(ip, lbn, newb, 0,
222 dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
223 ip->i_flag |= IN_CHANGE | IN_UPDATE;
228 * Determine the number of levels of indirection.
231 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
235 panic ("ffs_balloc_ufs1: ufs_getlbns returned indirect block");
237 saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
239 * Fetch the first indirect block allocating if necessary.
242 nb = dp->di_ib[indirs[0].in_off];
244 allocblk = allociblk;
248 pref = ffs_blkpref_ufs1(ip, lbn, 0, (ufs1_daddr_t *)0);
249 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
250 flags, cred, &newb)) != 0) {
251 curthread_pflags_restore(saved_inbdflush);
256 *lbns_remfree++ = indirs[1].in_lbn;
257 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, 0);
258 bp->b_blkno = fsbtodb(fs, nb);
260 if (DOINGSOFTDEP(vp)) {
261 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
262 newb, 0, fs->fs_bsize, 0, bp);
266 * Write synchronously so that indirect blocks
267 * never point at garbage.
271 else if ((error = bwrite(bp)) != 0)
274 allocib = &dp->di_ib[indirs[0].in_off];
276 ip->i_flag |= IN_CHANGE | IN_UPDATE;
279 * Fetch through the indirect blocks, allocating as necessary.
284 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
289 bap = (ufs1_daddr_t *)bp->b_data;
290 nb = bap[indirs[i].in_off];
300 pref = ffs_blkpref_ufs1(ip, lbn, 0, (ufs1_daddr_t *)0);
301 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
302 flags | IO_BUFLOCKED, cred, &newb)) != 0) {
304 if (++reclaimed == 1) {
306 softdep_request_cleanup(fs, vp, cred,
311 if (ppsratecheck(&lastfail, &curfail, 1)) {
312 ffs_fserr(fs, ip->i_number, "filesystem full");
313 uprintf("\n%s: write failed, filesystem "
314 "is full\n", fs->fs_fsmnt);
320 *lbns_remfree++ = indirs[i].in_lbn;
321 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
322 nbp->b_blkno = fsbtodb(fs, nb);
324 if (DOINGSOFTDEP(vp)) {
325 softdep_setup_allocindir_meta(nbp, ip, bp,
326 indirs[i - 1].in_off, nb);
330 * Write synchronously so that indirect blocks
331 * never point at garbage.
333 if ((error = bwrite(nbp)) != 0) {
338 bap[indirs[i - 1].in_off] = nb;
339 if (allocib == NULL && unwindidx < 0)
342 * If required, write synchronously, otherwise use
345 if (flags & IO_SYNC) {
348 if (bp->b_bufsize == fs->fs_bsize)
349 bp->b_flags |= B_CLUSTEROK;
354 * If asked only for the indirect block, then return it.
356 if (flags & BA_METAONLY) {
357 curthread_pflags_restore(saved_inbdflush);
362 * Get the data block, allocating if necessary.
366 pref = ffs_blkpref_ufs1(ip, lbn, indirs[i].in_off, &bap[0]);
367 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
368 flags | IO_BUFLOCKED, cred, &newb);
371 if (++reclaimed == 1) {
373 softdep_request_cleanup(fs, vp, cred,
378 if (ppsratecheck(&lastfail, &curfail, 1)) {
379 ffs_fserr(fs, ip->i_number, "filesystem full");
380 uprintf("\n%s: write failed, filesystem "
381 "is full\n", fs->fs_fsmnt);
387 *lbns_remfree++ = lbn;
388 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, 0);
389 nbp->b_blkno = fsbtodb(fs, nb);
390 if (flags & BA_CLRBUF)
392 if (DOINGSOFTDEP(vp))
393 softdep_setup_allocindir_page(ip, lbn, bp,
394 indirs[i].in_off, nb, 0, nbp);
395 bap[indirs[i].in_off] = nb;
397 * If required, write synchronously, otherwise use
400 if (flags & IO_SYNC) {
403 if (bp->b_bufsize == fs->fs_bsize)
404 bp->b_flags |= B_CLUSTEROK;
407 curthread_pflags_restore(saved_inbdflush);
412 if (flags & BA_CLRBUF) {
413 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
414 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
415 error = cluster_read(vp, ip->i_size, lbn,
416 (int)fs->fs_bsize, NOCRED,
417 MAXBSIZE, seqcount, &nbp);
419 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
426 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, 0);
427 nbp->b_blkno = fsbtodb(fs, nb);
429 curthread_pflags_restore(saved_inbdflush);
433 curthread_pflags_restore(saved_inbdflush);
435 * If we have failed to allocate any blocks, simply return the error.
436 * This is the usual case and avoids the need to fsync the file.
438 if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
441 * If we have failed part way through block allocation, we
442 * have to deallocate any indirect blocks that we have allocated.
443 * We have to fsync the file before we start to get rid of all
444 * of its dependencies so that we do not leave them dangling.
445 * We have to sync it at the end so that the soft updates code
446 * does not find any untracked changes. Although this is really
447 * slow, running out of disk space is not expected to be a common
448 * occurence. The error return from fsync is ignored as we already
449 * have an error to return to the user.
451 * XXX Still have to journal the free below
453 (void) ffs_syncvnode(vp, MNT_WAIT);
454 for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
455 blkp < allocblk; blkp++, lbns_remfree++) {
457 * We shall not leave the freed blocks on the vnode
458 * buffer object lists.
460 bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0, GB_NOCREAT);
462 bp->b_flags |= (B_INVAL | B_RELBUF);
463 bp->b_flags &= ~B_ASYNC;
466 deallocated += fs->fs_bsize;
468 if (allocib != NULL) {
470 } else if (unwindidx >= 0) {
473 r = bread(vp, indirs[unwindidx].in_lbn,
474 (int)fs->fs_bsize, NOCRED, &bp);
476 panic("Could not unwind indirect block, error %d", r);
479 bap = (ufs1_daddr_t *)bp->b_data;
480 bap[indirs[unwindidx].in_off] = 0;
481 if (flags & IO_SYNC) {
484 if (bp->b_bufsize == fs->fs_bsize)
485 bp->b_flags |= B_CLUSTEROK;
493 * Restore user's disk quota because allocation failed.
495 (void) chkdq(ip, -btodb(deallocated), cred, FORCE);
497 dp->di_blocks -= btodb(deallocated);
498 ip->i_flag |= IN_CHANGE | IN_UPDATE;
500 (void) ffs_syncvnode(vp, MNT_WAIT);
502 * After the buffers are invalidated and on-disk pointers are
503 * cleared, free the blocks.
505 for (blkp = allociblk; blkp < allocblk; blkp++) {
506 ffs_blkfree(ump, fs, ip->i_devvp, *blkp, fs->fs_bsize,
507 ip->i_number, vp->v_type, NULL);
513 * Balloc defines the structure of file system storage
514 * by allocating the physical blocks on a device given
515 * the inode and the logical block number in a file.
516 * This is the allocation strategy for UFS2. Above is
517 * the allocation strategy for UFS1.
520 ffs_balloc_ufs2(struct vnode *vp, off_t startoffset, int size,
521 struct ucred *cred, int flags, struct buf **bpp)
524 struct ufs2_dinode *dp;
525 ufs_lbn_t lbn, lastlbn;
527 struct buf *bp, *nbp;
528 struct ufsmount *ump;
529 struct indir indirs[NIADDR + 2];
530 ufs2_daddr_t nb, newb, *bap, pref;
531 ufs2_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
532 ufs2_daddr_t *lbns_remfree, lbns[NIADDR + 1];
533 int deallocated, osize, nsize, num, i, error;
536 static struct timeval lastfail;
544 lbn = lblkno(fs, startoffset);
545 size = blkoff(fs, startoffset) + size;
547 if (size > fs->fs_bsize)
548 panic("ffs_balloc_ufs2: blk too big");
553 if (DOINGSOFTDEP(vp))
554 softdep_prealloc(vp, MNT_WAIT);
557 * Check for allocating external data.
559 if (flags & IO_EXT) {
563 * If the next write will extend the data into a new block,
564 * and the data is currently composed of a fragment
565 * this fragment has to be extended to be a full block.
567 lastlbn = lblkno(fs, dp->di_extsize);
570 osize = sblksize(fs, dp->di_extsize, nb);
571 if (osize < fs->fs_bsize && osize > 0) {
573 error = ffs_realloccg(ip, -1 - nb,
575 ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
576 &dp->di_extb[0]), osize,
577 (int)fs->fs_bsize, flags, cred, &bp);
580 if (DOINGSOFTDEP(vp))
581 softdep_setup_allocext(ip, nb,
582 dbtofsb(fs, bp->b_blkno),
584 fs->fs_bsize, osize, bp);
585 dp->di_extsize = smalllblktosize(fs, nb + 1);
586 dp->di_extb[nb] = dbtofsb(fs, bp->b_blkno);
587 bp->b_xflags |= BX_ALTDATA;
588 ip->i_flag |= IN_CHANGE;
596 * All blocks are direct blocks
598 if (flags & BA_METAONLY)
599 panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
600 nb = dp->di_extb[lbn];
601 if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
602 error = bread(vp, -1 - lbn, fs->fs_bsize, NOCRED, &bp);
607 bp->b_blkno = fsbtodb(fs, nb);
608 bp->b_xflags |= BX_ALTDATA;
614 * Consider need to reallocate a fragment.
616 osize = fragroundup(fs, blkoff(fs, dp->di_extsize));
617 nsize = fragroundup(fs, size);
618 if (nsize <= osize) {
619 error = bread(vp, -1 - lbn, osize, NOCRED, &bp);
624 bp->b_blkno = fsbtodb(fs, nb);
625 bp->b_xflags |= BX_ALTDATA;
628 error = ffs_realloccg(ip, -1 - lbn,
630 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
631 &dp->di_extb[0]), osize, nsize, flags,
635 bp->b_xflags |= BX_ALTDATA;
636 if (DOINGSOFTDEP(vp))
637 softdep_setup_allocext(ip, lbn,
638 dbtofsb(fs, bp->b_blkno), nb,
642 if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
643 nsize = fragroundup(fs, size);
645 nsize = fs->fs_bsize;
647 error = ffs_alloc(ip, lbn,
648 ffs_blkpref_ufs2(ip, lbn, (int)lbn, &dp->di_extb[0]),
649 nsize, flags, cred, &newb);
652 bp = getblk(vp, -1 - lbn, nsize, 0, 0, 0);
653 bp->b_blkno = fsbtodb(fs, newb);
654 bp->b_xflags |= BX_ALTDATA;
655 if (flags & BA_CLRBUF)
657 if (DOINGSOFTDEP(vp))
658 softdep_setup_allocext(ip, lbn, newb, 0,
661 dp->di_extb[lbn] = dbtofsb(fs, bp->b_blkno);
662 ip->i_flag |= IN_CHANGE;
667 * If the next write will extend the file into a new block,
668 * and the file is currently composed of a fragment
669 * this fragment has to be extended to be a full block.
671 lastlbn = lblkno(fs, ip->i_size);
672 if (lastlbn < NDADDR && lastlbn < lbn) {
674 osize = blksize(fs, ip, nb);
675 if (osize < fs->fs_bsize && osize > 0) {
677 error = ffs_realloccg(ip, nb, dp->di_db[nb],
678 ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
679 &dp->di_db[0]), osize, (int)fs->fs_bsize,
683 if (DOINGSOFTDEP(vp))
684 softdep_setup_allocdirect(ip, nb,
685 dbtofsb(fs, bp->b_blkno),
687 fs->fs_bsize, osize, bp);
688 ip->i_size = smalllblktosize(fs, nb + 1);
689 dp->di_size = ip->i_size;
690 dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
691 ip->i_flag |= IN_CHANGE | IN_UPDATE;
699 * The first NDADDR blocks are direct blocks
702 if (flags & BA_METAONLY)
703 panic("ffs_balloc_ufs2: BA_METAONLY for direct block");
705 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
706 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
711 bp->b_blkno = fsbtodb(fs, nb);
717 * Consider need to reallocate a fragment.
719 osize = fragroundup(fs, blkoff(fs, ip->i_size));
720 nsize = fragroundup(fs, size);
721 if (nsize <= osize) {
722 error = bread(vp, lbn, osize, NOCRED, &bp);
727 bp->b_blkno = fsbtodb(fs, nb);
730 error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
731 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
732 &dp->di_db[0]), osize, nsize, flags,
736 if (DOINGSOFTDEP(vp))
737 softdep_setup_allocdirect(ip, lbn,
738 dbtofsb(fs, bp->b_blkno), nb,
742 if (ip->i_size < smalllblktosize(fs, lbn + 1))
743 nsize = fragroundup(fs, size);
745 nsize = fs->fs_bsize;
747 error = ffs_alloc(ip, lbn,
748 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
749 &dp->di_db[0]), nsize, flags, cred, &newb);
752 bp = getblk(vp, lbn, nsize, 0, 0, 0);
753 bp->b_blkno = fsbtodb(fs, newb);
754 if (flags & BA_CLRBUF)
756 if (DOINGSOFTDEP(vp))
757 softdep_setup_allocdirect(ip, lbn, newb, 0,
760 dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
761 ip->i_flag |= IN_CHANGE | IN_UPDATE;
766 * Determine the number of levels of indirection.
769 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
773 panic ("ffs_balloc_ufs2: ufs_getlbns returned indirect block");
775 saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
777 * Fetch the first indirect block allocating if necessary.
780 nb = dp->di_ib[indirs[0].in_off];
782 allocblk = allociblk;
786 pref = ffs_blkpref_ufs2(ip, lbn, 0, (ufs2_daddr_t *)0);
787 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
788 flags, cred, &newb)) != 0) {
789 curthread_pflags_restore(saved_inbdflush);
794 *lbns_remfree++ = indirs[1].in_lbn;
795 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, 0);
796 bp->b_blkno = fsbtodb(fs, nb);
798 if (DOINGSOFTDEP(vp)) {
799 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
800 newb, 0, fs->fs_bsize, 0, bp);
804 * Write synchronously so that indirect blocks
805 * never point at garbage.
809 else if ((error = bwrite(bp)) != 0)
812 allocib = &dp->di_ib[indirs[0].in_off];
814 ip->i_flag |= IN_CHANGE | IN_UPDATE;
817 * Fetch through the indirect blocks, allocating as necessary.
822 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
827 bap = (ufs2_daddr_t *)bp->b_data;
828 nb = bap[indirs[i].in_off];
838 pref = ffs_blkpref_ufs2(ip, lbn, 0, (ufs2_daddr_t *)0);
839 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
840 flags | IO_BUFLOCKED, cred, &newb)) != 0) {
842 if (++reclaimed == 1) {
844 softdep_request_cleanup(fs, vp, cred,
849 if (ppsratecheck(&lastfail, &curfail, 1)) {
850 ffs_fserr(fs, ip->i_number, "filesystem full");
851 uprintf("\n%s: write failed, filesystem "
852 "is full\n", fs->fs_fsmnt);
858 *lbns_remfree++ = indirs[i].in_lbn;
859 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
860 nbp->b_blkno = fsbtodb(fs, nb);
862 if (DOINGSOFTDEP(vp)) {
863 softdep_setup_allocindir_meta(nbp, ip, bp,
864 indirs[i - 1].in_off, nb);
868 * Write synchronously so that indirect blocks
869 * never point at garbage.
871 if ((error = bwrite(nbp)) != 0) {
876 bap[indirs[i - 1].in_off] = nb;
877 if (allocib == NULL && unwindidx < 0)
880 * If required, write synchronously, otherwise use
883 if (flags & IO_SYNC) {
886 if (bp->b_bufsize == fs->fs_bsize)
887 bp->b_flags |= B_CLUSTEROK;
892 * If asked only for the indirect block, then return it.
894 if (flags & BA_METAONLY) {
895 curthread_pflags_restore(saved_inbdflush);
900 * Get the data block, allocating if necessary.
904 pref = ffs_blkpref_ufs2(ip, lbn, indirs[i].in_off, &bap[0]);
905 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
906 flags | IO_BUFLOCKED, cred, &newb);
909 if (++reclaimed == 1) {
911 softdep_request_cleanup(fs, vp, cred,
916 if (ppsratecheck(&lastfail, &curfail, 1)) {
917 ffs_fserr(fs, ip->i_number, "filesystem full");
918 uprintf("\n%s: write failed, filesystem "
919 "is full\n", fs->fs_fsmnt);
925 *lbns_remfree++ = lbn;
926 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, 0);
927 nbp->b_blkno = fsbtodb(fs, nb);
928 if (flags & BA_CLRBUF)
930 if (DOINGSOFTDEP(vp))
931 softdep_setup_allocindir_page(ip, lbn, bp,
932 indirs[i].in_off, nb, 0, nbp);
933 bap[indirs[i].in_off] = nb;
935 * If required, write synchronously, otherwise use
938 if (flags & IO_SYNC) {
941 if (bp->b_bufsize == fs->fs_bsize)
942 bp->b_flags |= B_CLUSTEROK;
945 curthread_pflags_restore(saved_inbdflush);
951 * If requested clear invalid portions of the buffer. If we
952 * have to do a read-before-write (typical if BA_CLRBUF is set),
953 * try to do some read-ahead in the sequential case to reduce
954 * the number of I/O transactions.
956 if (flags & BA_CLRBUF) {
957 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
958 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
959 error = cluster_read(vp, ip->i_size, lbn,
960 (int)fs->fs_bsize, NOCRED,
961 MAXBSIZE, seqcount, &nbp);
963 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
970 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, 0);
971 nbp->b_blkno = fsbtodb(fs, nb);
973 curthread_pflags_restore(saved_inbdflush);
977 curthread_pflags_restore(saved_inbdflush);
979 * If we have failed to allocate any blocks, simply return the error.
980 * This is the usual case and avoids the need to fsync the file.
982 if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
985 * If we have failed part way through block allocation, we
986 * have to deallocate any indirect blocks that we have allocated.
987 * We have to fsync the file before we start to get rid of all
988 * of its dependencies so that we do not leave them dangling.
989 * We have to sync it at the end so that the soft updates code
990 * does not find any untracked changes. Although this is really
991 * slow, running out of disk space is not expected to be a common
992 * occurence. The error return from fsync is ignored as we already
993 * have an error to return to the user.
995 * XXX Still have to journal the free below
997 (void) ffs_syncvnode(vp, MNT_WAIT);
998 for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
999 blkp < allocblk; blkp++, lbns_remfree++) {
1001 * We shall not leave the freed blocks on the vnode
1002 * buffer object lists.
1004 bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0, GB_NOCREAT);
1006 bp->b_flags |= (B_INVAL | B_RELBUF);
1007 bp->b_flags &= ~B_ASYNC;
1010 deallocated += fs->fs_bsize;
1012 if (allocib != NULL) {
1014 } else if (unwindidx >= 0) {
1017 r = bread(vp, indirs[unwindidx].in_lbn,
1018 (int)fs->fs_bsize, NOCRED, &bp);
1020 panic("Could not unwind indirect block, error %d", r);
1023 bap = (ufs2_daddr_t *)bp->b_data;
1024 bap[indirs[unwindidx].in_off] = 0;
1025 if (flags & IO_SYNC) {
1028 if (bp->b_bufsize == fs->fs_bsize)
1029 bp->b_flags |= B_CLUSTEROK;
1037 * Restore user's disk quota because allocation failed.
1039 (void) chkdq(ip, -btodb(deallocated), cred, FORCE);
1041 dp->di_blocks -= btodb(deallocated);
1042 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1044 (void) ffs_syncvnode(vp, MNT_WAIT);
1046 * After the buffers are invalidated and on-disk pointers are
1047 * cleared, free the blocks.
1049 for (blkp = allociblk; blkp < allocblk; blkp++) {
1050 ffs_blkfree(ump, fs, ip->i_devvp, *blkp, fs->fs_bsize,
1051 ip->i_number, vp->v_type, NULL);