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;
110 int gbflags, reclaimed;
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");
126 gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
128 if (DOINGSOFTDEP(vp))
129 softdep_prealloc(vp, MNT_WAIT);
131 * If the next write will extend the file into a new block,
132 * and the file is currently composed of a fragment
133 * this fragment has to be extended to be a full block.
135 lastlbn = lblkno(fs, ip->i_size);
136 if (lastlbn < NDADDR && lastlbn < lbn) {
138 osize = blksize(fs, ip, nb);
139 if (osize < fs->fs_bsize && osize > 0) {
141 error = ffs_realloccg(ip, nb, dp->di_db[nb],
142 ffs_blkpref_ufs1(ip, lastlbn, (int)nb,
143 &dp->di_db[0]), osize, (int)fs->fs_bsize, flags,
147 if (DOINGSOFTDEP(vp))
148 softdep_setup_allocdirect(ip, nb,
149 dbtofsb(fs, bp->b_blkno), dp->di_db[nb],
150 fs->fs_bsize, osize, bp);
151 ip->i_size = smalllblktosize(fs, nb + 1);
152 dp->di_size = ip->i_size;
153 dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
154 ip->i_flag |= IN_CHANGE | IN_UPDATE;
162 * The first NDADDR blocks are direct blocks
165 if (flags & BA_METAONLY)
166 panic("ffs_balloc_ufs1: BA_METAONLY for direct block");
168 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
169 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
174 bp->b_blkno = fsbtodb(fs, nb);
180 * Consider need to reallocate a fragment.
182 osize = fragroundup(fs, blkoff(fs, ip->i_size));
183 nsize = fragroundup(fs, size);
184 if (nsize <= osize) {
185 error = bread(vp, lbn, osize, NOCRED, &bp);
190 bp->b_blkno = fsbtodb(fs, nb);
193 error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
194 ffs_blkpref_ufs1(ip, lbn, (int)lbn,
195 &dp->di_db[0]), osize, nsize, flags,
199 if (DOINGSOFTDEP(vp))
200 softdep_setup_allocdirect(ip, lbn,
201 dbtofsb(fs, bp->b_blkno), nb,
205 if (ip->i_size < smalllblktosize(fs, lbn + 1))
206 nsize = fragroundup(fs, size);
208 nsize = fs->fs_bsize;
210 error = ffs_alloc(ip, lbn,
211 ffs_blkpref_ufs1(ip, lbn, (int)lbn, &dp->di_db[0]),
212 nsize, flags, cred, &newb);
215 bp = getblk(vp, lbn, nsize, 0, 0, gbflags);
216 bp->b_blkno = fsbtodb(fs, newb);
217 if (flags & BA_CLRBUF)
219 if (DOINGSOFTDEP(vp))
220 softdep_setup_allocdirect(ip, lbn, newb, 0,
223 dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
224 ip->i_flag |= IN_CHANGE | IN_UPDATE;
229 * Determine the number of levels of indirection.
232 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
236 panic ("ffs_balloc_ufs1: ufs_getlbns returned indirect block");
238 saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
240 * Fetch the first indirect block allocating if necessary.
243 nb = dp->di_ib[indirs[0].in_off];
245 allocblk = allociblk;
249 pref = ffs_blkpref_ufs1(ip, lbn, -indirs[0].in_off - 1,
251 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
252 flags, cred, &newb)) != 0) {
253 curthread_pflags_restore(saved_inbdflush);
256 pref = newb + fs->fs_frag;
259 *lbns_remfree++ = indirs[1].in_lbn;
260 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, gbflags);
261 bp->b_blkno = fsbtodb(fs, nb);
263 if (DOINGSOFTDEP(vp)) {
264 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
265 newb, 0, fs->fs_bsize, 0, bp);
269 * Write synchronously so that indirect blocks
270 * never point at garbage.
274 else if ((error = bwrite(bp)) != 0)
277 allocib = &dp->di_ib[indirs[0].in_off];
279 ip->i_flag |= IN_CHANGE | IN_UPDATE;
282 * Fetch through the indirect blocks, allocating as necessary.
287 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
292 bap = (ufs1_daddr_t *)bp->b_data;
293 nb = bap[indirs[i].in_off];
303 pref = ffs_blkpref_ufs1(ip, lbn, i - num - 1,
305 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
306 flags | IO_BUFLOCKED, cred, &newb)) != 0) {
308 if (++reclaimed == 1) {
310 softdep_request_cleanup(fs, vp, cred,
315 if (ppsratecheck(&lastfail, &curfail, 1)) {
316 ffs_fserr(fs, ip->i_number, "filesystem full");
317 uprintf("\n%s: write failed, filesystem "
318 "is full\n", fs->fs_fsmnt);
322 pref = newb + fs->fs_frag;
325 *lbns_remfree++ = indirs[i].in_lbn;
326 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
327 nbp->b_blkno = fsbtodb(fs, nb);
329 if (DOINGSOFTDEP(vp)) {
330 softdep_setup_allocindir_meta(nbp, ip, bp,
331 indirs[i - 1].in_off, nb);
335 * Write synchronously so that indirect blocks
336 * never point at garbage.
338 if ((error = bwrite(nbp)) != 0) {
343 bap[indirs[i - 1].in_off] = nb;
344 if (allocib == NULL && unwindidx < 0)
347 * If required, write synchronously, otherwise use
350 if (flags & IO_SYNC) {
353 if (bp->b_bufsize == fs->fs_bsize)
354 bp->b_flags |= B_CLUSTEROK;
359 * If asked only for the indirect block, then return it.
361 if (flags & BA_METAONLY) {
362 curthread_pflags_restore(saved_inbdflush);
367 * Get the data block, allocating if necessary.
372 pref = ffs_blkpref_ufs1(ip, lbn, indirs[i].in_off,
374 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
375 flags | IO_BUFLOCKED, cred, &newb);
378 if (++reclaimed == 1) {
380 softdep_request_cleanup(fs, vp, cred,
385 if (ppsratecheck(&lastfail, &curfail, 1)) {
386 ffs_fserr(fs, ip->i_number, "filesystem full");
387 uprintf("\n%s: write failed, filesystem "
388 "is full\n", fs->fs_fsmnt);
394 *lbns_remfree++ = lbn;
395 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
396 nbp->b_blkno = fsbtodb(fs, nb);
397 if (flags & BA_CLRBUF)
399 if (DOINGSOFTDEP(vp))
400 softdep_setup_allocindir_page(ip, lbn, bp,
401 indirs[i].in_off, nb, 0, nbp);
402 bap[indirs[i].in_off] = nb;
404 * If required, write synchronously, otherwise use
407 if (flags & IO_SYNC) {
410 if (bp->b_bufsize == fs->fs_bsize)
411 bp->b_flags |= B_CLUSTEROK;
414 curthread_pflags_restore(saved_inbdflush);
419 if (flags & BA_CLRBUF) {
420 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
421 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
422 error = cluster_read(vp, ip->i_size, lbn,
423 (int)fs->fs_bsize, NOCRED,
424 MAXBSIZE, seqcount, gbflags, &nbp);
426 error = bread_gb(vp, lbn, (int)fs->fs_bsize, NOCRED,
434 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
435 nbp->b_blkno = fsbtodb(fs, nb);
437 curthread_pflags_restore(saved_inbdflush);
441 curthread_pflags_restore(saved_inbdflush);
443 * If we have failed to allocate any blocks, simply return the error.
444 * This is the usual case and avoids the need to fsync the file.
446 if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
449 * If we have failed part way through block allocation, we
450 * have to deallocate any indirect blocks that we have allocated.
451 * We have to fsync the file before we start to get rid of all
452 * of its dependencies so that we do not leave them dangling.
453 * We have to sync it at the end so that the soft updates code
454 * does not find any untracked changes. Although this is really
455 * slow, running out of disk space is not expected to be a common
456 * occurrence. The error return from fsync is ignored as we already
457 * have an error to return to the user.
459 * XXX Still have to journal the free below
461 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
462 for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
463 blkp < allocblk; blkp++, lbns_remfree++) {
465 * We shall not leave the freed blocks on the vnode
466 * buffer object lists.
468 bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0, GB_NOCREAT);
470 bp->b_flags |= (B_INVAL | B_RELBUF);
471 bp->b_flags &= ~B_ASYNC;
474 deallocated += fs->fs_bsize;
476 if (allocib != NULL) {
478 } else if (unwindidx >= 0) {
481 r = bread(vp, indirs[unwindidx].in_lbn,
482 (int)fs->fs_bsize, NOCRED, &bp);
484 panic("Could not unwind indirect block, error %d", r);
487 bap = (ufs1_daddr_t *)bp->b_data;
488 bap[indirs[unwindidx].in_off] = 0;
489 if (flags & IO_SYNC) {
492 if (bp->b_bufsize == fs->fs_bsize)
493 bp->b_flags |= B_CLUSTEROK;
501 * Restore user's disk quota because allocation failed.
503 (void) chkdq(ip, -btodb(deallocated), cred, FORCE);
505 dp->di_blocks -= btodb(deallocated);
506 ip->i_flag |= IN_CHANGE | IN_UPDATE;
508 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
510 * After the buffers are invalidated and on-disk pointers are
511 * cleared, free the blocks.
513 for (blkp = allociblk; blkp < allocblk; blkp++) {
514 ffs_blkfree(ump, fs, ip->i_devvp, *blkp, fs->fs_bsize,
515 ip->i_number, vp->v_type, NULL);
521 * Balloc defines the structure of file system storage
522 * by allocating the physical blocks on a device given
523 * the inode and the logical block number in a file.
524 * This is the allocation strategy for UFS2. Above is
525 * the allocation strategy for UFS1.
528 ffs_balloc_ufs2(struct vnode *vp, off_t startoffset, int size,
529 struct ucred *cred, int flags, struct buf **bpp)
532 struct ufs2_dinode *dp;
533 ufs_lbn_t lbn, lastlbn;
535 struct buf *bp, *nbp;
536 struct ufsmount *ump;
537 struct indir indirs[NIADDR + 2];
538 ufs2_daddr_t nb, newb, *bap, pref;
539 ufs2_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
540 ufs2_daddr_t *lbns_remfree, lbns[NIADDR + 1];
541 int deallocated, osize, nsize, num, i, error;
544 static struct timeval lastfail;
546 int gbflags, reclaimed;
552 lbn = lblkno(fs, startoffset);
553 size = blkoff(fs, startoffset) + size;
555 if (size > fs->fs_bsize)
556 panic("ffs_balloc_ufs2: blk too big");
560 gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
562 if (DOINGSOFTDEP(vp))
563 softdep_prealloc(vp, MNT_WAIT);
566 * Check for allocating external data.
568 if (flags & IO_EXT) {
572 * If the next write will extend the data into a new block,
573 * and the data is currently composed of a fragment
574 * this fragment has to be extended to be a full block.
576 lastlbn = lblkno(fs, dp->di_extsize);
579 osize = sblksize(fs, dp->di_extsize, nb);
580 if (osize < fs->fs_bsize && osize > 0) {
582 error = ffs_realloccg(ip, -1 - nb,
584 ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
585 &dp->di_extb[0]), osize,
586 (int)fs->fs_bsize, flags, cred, &bp);
589 if (DOINGSOFTDEP(vp))
590 softdep_setup_allocext(ip, nb,
591 dbtofsb(fs, bp->b_blkno),
593 fs->fs_bsize, osize, bp);
594 dp->di_extsize = smalllblktosize(fs, nb + 1);
595 dp->di_extb[nb] = dbtofsb(fs, bp->b_blkno);
596 bp->b_xflags |= BX_ALTDATA;
597 ip->i_flag |= IN_CHANGE;
605 * All blocks are direct blocks
607 if (flags & BA_METAONLY)
608 panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
609 nb = dp->di_extb[lbn];
610 if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
611 error = bread_gb(vp, -1 - lbn, fs->fs_bsize, NOCRED,
617 bp->b_blkno = fsbtodb(fs, nb);
618 bp->b_xflags |= BX_ALTDATA;
624 * Consider need to reallocate a fragment.
626 osize = fragroundup(fs, blkoff(fs, dp->di_extsize));
627 nsize = fragroundup(fs, size);
628 if (nsize <= osize) {
629 error = bread_gb(vp, -1 - lbn, osize, NOCRED,
635 bp->b_blkno = fsbtodb(fs, nb);
636 bp->b_xflags |= BX_ALTDATA;
639 error = ffs_realloccg(ip, -1 - lbn,
641 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
642 &dp->di_extb[0]), osize, nsize, flags,
646 bp->b_xflags |= BX_ALTDATA;
647 if (DOINGSOFTDEP(vp))
648 softdep_setup_allocext(ip, lbn,
649 dbtofsb(fs, bp->b_blkno), nb,
653 if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
654 nsize = fragroundup(fs, size);
656 nsize = fs->fs_bsize;
658 error = ffs_alloc(ip, lbn,
659 ffs_blkpref_ufs2(ip, lbn, (int)lbn, &dp->di_extb[0]),
660 nsize, flags, cred, &newb);
663 bp = getblk(vp, -1 - lbn, nsize, 0, 0, gbflags);
664 bp->b_blkno = fsbtodb(fs, newb);
665 bp->b_xflags |= BX_ALTDATA;
666 if (flags & BA_CLRBUF)
668 if (DOINGSOFTDEP(vp))
669 softdep_setup_allocext(ip, lbn, newb, 0,
672 dp->di_extb[lbn] = dbtofsb(fs, bp->b_blkno);
673 ip->i_flag |= IN_CHANGE;
678 * If the next write will extend the file into a new block,
679 * and the file is currently composed of a fragment
680 * this fragment has to be extended to be a full block.
682 lastlbn = lblkno(fs, ip->i_size);
683 if (lastlbn < NDADDR && lastlbn < lbn) {
685 osize = blksize(fs, ip, nb);
686 if (osize < fs->fs_bsize && osize > 0) {
688 error = ffs_realloccg(ip, nb, dp->di_db[nb],
689 ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
690 &dp->di_db[0]), osize, (int)fs->fs_bsize,
694 if (DOINGSOFTDEP(vp))
695 softdep_setup_allocdirect(ip, nb,
696 dbtofsb(fs, bp->b_blkno),
698 fs->fs_bsize, osize, bp);
699 ip->i_size = smalllblktosize(fs, nb + 1);
700 dp->di_size = ip->i_size;
701 dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
702 ip->i_flag |= IN_CHANGE | IN_UPDATE;
710 * The first NDADDR blocks are direct blocks
713 if (flags & BA_METAONLY)
714 panic("ffs_balloc_ufs2: BA_METAONLY for direct block");
716 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
717 error = bread_gb(vp, lbn, fs->fs_bsize, NOCRED,
723 bp->b_blkno = fsbtodb(fs, nb);
729 * Consider need to reallocate a fragment.
731 osize = fragroundup(fs, blkoff(fs, ip->i_size));
732 nsize = fragroundup(fs, size);
733 if (nsize <= osize) {
734 error = bread_gb(vp, lbn, osize, NOCRED,
740 bp->b_blkno = fsbtodb(fs, nb);
743 error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
744 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
745 &dp->di_db[0]), osize, nsize, flags,
749 if (DOINGSOFTDEP(vp))
750 softdep_setup_allocdirect(ip, lbn,
751 dbtofsb(fs, bp->b_blkno), nb,
755 if (ip->i_size < smalllblktosize(fs, lbn + 1))
756 nsize = fragroundup(fs, size);
758 nsize = fs->fs_bsize;
760 error = ffs_alloc(ip, lbn,
761 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
762 &dp->di_db[0]), nsize, flags, cred, &newb);
765 bp = getblk(vp, lbn, nsize, 0, 0, gbflags);
766 bp->b_blkno = fsbtodb(fs, newb);
767 if (flags & BA_CLRBUF)
769 if (DOINGSOFTDEP(vp))
770 softdep_setup_allocdirect(ip, lbn, newb, 0,
773 dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
774 ip->i_flag |= IN_CHANGE | IN_UPDATE;
779 * Determine the number of levels of indirection.
782 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
786 panic ("ffs_balloc_ufs2: ufs_getlbns returned indirect block");
788 saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
790 * Fetch the first indirect block allocating if necessary.
793 nb = dp->di_ib[indirs[0].in_off];
795 allocblk = allociblk;
799 pref = ffs_blkpref_ufs2(ip, lbn, -indirs[0].in_off - 1,
801 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
802 flags, cred, &newb)) != 0) {
803 curthread_pflags_restore(saved_inbdflush);
806 pref = newb + fs->fs_frag;
809 *lbns_remfree++ = indirs[1].in_lbn;
810 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0,
812 bp->b_blkno = fsbtodb(fs, nb);
814 if (DOINGSOFTDEP(vp)) {
815 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
816 newb, 0, fs->fs_bsize, 0, bp);
820 * Write synchronously so that indirect blocks
821 * never point at garbage.
825 else if ((error = bwrite(bp)) != 0)
828 allocib = &dp->di_ib[indirs[0].in_off];
830 ip->i_flag |= IN_CHANGE | IN_UPDATE;
833 * Fetch through the indirect blocks, allocating as necessary.
838 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
843 bap = (ufs2_daddr_t *)bp->b_data;
844 nb = bap[indirs[i].in_off];
854 pref = ffs_blkpref_ufs2(ip, lbn, i - num - 1,
856 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
857 flags | IO_BUFLOCKED, cred, &newb)) != 0) {
859 if (++reclaimed == 1) {
861 softdep_request_cleanup(fs, vp, cred,
866 if (ppsratecheck(&lastfail, &curfail, 1)) {
867 ffs_fserr(fs, ip->i_number, "filesystem full");
868 uprintf("\n%s: write failed, filesystem "
869 "is full\n", fs->fs_fsmnt);
873 pref = newb + fs->fs_frag;
876 *lbns_remfree++ = indirs[i].in_lbn;
877 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0,
879 nbp->b_blkno = fsbtodb(fs, nb);
881 if (DOINGSOFTDEP(vp)) {
882 softdep_setup_allocindir_meta(nbp, ip, bp,
883 indirs[i - 1].in_off, nb);
887 * Write synchronously so that indirect blocks
888 * never point at garbage.
890 if ((error = bwrite(nbp)) != 0) {
895 bap[indirs[i - 1].in_off] = nb;
896 if (allocib == NULL && unwindidx < 0)
899 * If required, write synchronously, otherwise use
902 if (flags & IO_SYNC) {
905 if (bp->b_bufsize == fs->fs_bsize)
906 bp->b_flags |= B_CLUSTEROK;
911 * If asked only for the indirect block, then return it.
913 if (flags & BA_METAONLY) {
914 curthread_pflags_restore(saved_inbdflush);
919 * Get the data block, allocating if necessary.
924 pref = ffs_blkpref_ufs2(ip, lbn, indirs[i].in_off,
926 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
927 flags | IO_BUFLOCKED, cred, &newb);
930 if (++reclaimed == 1) {
932 softdep_request_cleanup(fs, vp, cred,
937 if (ppsratecheck(&lastfail, &curfail, 1)) {
938 ffs_fserr(fs, ip->i_number, "filesystem full");
939 uprintf("\n%s: write failed, filesystem "
940 "is full\n", fs->fs_fsmnt);
946 *lbns_remfree++ = lbn;
947 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
948 nbp->b_blkno = fsbtodb(fs, nb);
949 if (flags & BA_CLRBUF)
951 if (DOINGSOFTDEP(vp))
952 softdep_setup_allocindir_page(ip, lbn, bp,
953 indirs[i].in_off, nb, 0, nbp);
954 bap[indirs[i].in_off] = nb;
956 * If required, write synchronously, otherwise use
959 if (flags & IO_SYNC) {
962 if (bp->b_bufsize == fs->fs_bsize)
963 bp->b_flags |= B_CLUSTEROK;
966 curthread_pflags_restore(saved_inbdflush);
972 * If requested clear invalid portions of the buffer. If we
973 * have to do a read-before-write (typical if BA_CLRBUF is set),
974 * try to do some read-ahead in the sequential case to reduce
975 * the number of I/O transactions.
977 if (flags & BA_CLRBUF) {
978 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
979 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
980 error = cluster_read(vp, ip->i_size, lbn,
981 (int)fs->fs_bsize, NOCRED,
982 MAXBSIZE, seqcount, gbflags, &nbp);
984 error = bread_gb(vp, lbn, (int)fs->fs_bsize,
985 NOCRED, gbflags, &nbp);
992 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
993 nbp->b_blkno = fsbtodb(fs, nb);
995 curthread_pflags_restore(saved_inbdflush);
999 curthread_pflags_restore(saved_inbdflush);
1001 * If we have failed to allocate any blocks, simply return the error.
1002 * This is the usual case and avoids the need to fsync the file.
1004 if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
1007 * If we have failed part way through block allocation, we
1008 * have to deallocate any indirect blocks that we have allocated.
1009 * We have to fsync the file before we start to get rid of all
1010 * of its dependencies so that we do not leave them dangling.
1011 * We have to sync it at the end so that the soft updates code
1012 * does not find any untracked changes. Although this is really
1013 * slow, running out of disk space is not expected to be a common
1014 * occurrence. The error return from fsync is ignored as we already
1015 * have an error to return to the user.
1017 * XXX Still have to journal the free below
1019 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
1020 for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
1021 blkp < allocblk; blkp++, lbns_remfree++) {
1023 * We shall not leave the freed blocks on the vnode
1024 * buffer object lists.
1026 bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0, GB_NOCREAT);
1028 bp->b_flags |= (B_INVAL | B_RELBUF);
1029 bp->b_flags &= ~B_ASYNC;
1032 deallocated += fs->fs_bsize;
1034 if (allocib != NULL) {
1036 } else if (unwindidx >= 0) {
1039 r = bread(vp, indirs[unwindidx].in_lbn,
1040 (int)fs->fs_bsize, NOCRED, &bp);
1042 panic("Could not unwind indirect block, error %d", r);
1045 bap = (ufs2_daddr_t *)bp->b_data;
1046 bap[indirs[unwindidx].in_off] = 0;
1047 if (flags & IO_SYNC) {
1050 if (bp->b_bufsize == fs->fs_bsize)
1051 bp->b_flags |= B_CLUSTEROK;
1059 * Restore user's disk quota because allocation failed.
1061 (void) chkdq(ip, -btodb(deallocated), cred, FORCE);
1063 dp->di_blocks -= btodb(deallocated);
1064 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1066 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
1068 * After the buffers are invalidated and on-disk pointers are
1069 * cleared, free the blocks.
1071 for (blkp = allociblk; blkp < allocblk; blkp++) {
1072 ffs_blkfree(ump, fs, ip->i_devvp, *blkp, fs->fs_bsize,
1073 ip->i_number, vp->v_type, NULL);