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 * If parent indirect has just been allocated, try to cluster
304 * immediately following it.
307 pref = ffs_blkpref_ufs1(ip, lbn, i - num - 1,
309 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
310 flags | IO_BUFLOCKED, cred, &newb)) != 0) {
312 if (++reclaimed == 1) {
314 softdep_request_cleanup(fs, vp, cred,
319 if (ppsratecheck(&lastfail, &curfail, 1)) {
320 ffs_fserr(fs, ip->i_number, "filesystem full");
321 uprintf("\n%s: write failed, filesystem "
322 "is full\n", fs->fs_fsmnt);
326 pref = newb + fs->fs_frag;
329 *lbns_remfree++ = indirs[i].in_lbn;
330 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
331 nbp->b_blkno = fsbtodb(fs, nb);
333 if (DOINGSOFTDEP(vp)) {
334 softdep_setup_allocindir_meta(nbp, ip, bp,
335 indirs[i - 1].in_off, nb);
339 * Write synchronously so that indirect blocks
340 * never point at garbage.
342 if ((error = bwrite(nbp)) != 0) {
347 bap[indirs[i - 1].in_off] = nb;
348 if (allocib == NULL && unwindidx < 0)
351 * If required, write synchronously, otherwise use
354 if (flags & IO_SYNC) {
357 if (bp->b_bufsize == fs->fs_bsize)
358 bp->b_flags |= B_CLUSTEROK;
363 * If asked only for the indirect block, then return it.
365 if (flags & BA_METAONLY) {
366 curthread_pflags_restore(saved_inbdflush);
371 * Get the data block, allocating if necessary.
376 * If allocating metadata at the front of the cylinder
377 * group and parent indirect block has just been allocated,
378 * then cluster next to it if it is the first indirect in
379 * the file. Otherwise it has been allocated in the metadata
380 * area, so we want to find our own place out in the data area.
382 if (pref == 0 || (lbn > NDADDR && fs->fs_metaspace != 0))
383 pref = ffs_blkpref_ufs1(ip, lbn, indirs[i].in_off,
385 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
386 flags | IO_BUFLOCKED, cred, &newb);
389 if (++reclaimed == 1) {
391 softdep_request_cleanup(fs, vp, cred,
396 if (ppsratecheck(&lastfail, &curfail, 1)) {
397 ffs_fserr(fs, ip->i_number, "filesystem full");
398 uprintf("\n%s: write failed, filesystem "
399 "is full\n", fs->fs_fsmnt);
405 *lbns_remfree++ = lbn;
406 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
407 nbp->b_blkno = fsbtodb(fs, nb);
408 if (flags & BA_CLRBUF)
410 if (DOINGSOFTDEP(vp))
411 softdep_setup_allocindir_page(ip, lbn, bp,
412 indirs[i].in_off, nb, 0, nbp);
413 bap[indirs[i].in_off] = nb;
415 * If required, write synchronously, otherwise use
418 if (flags & IO_SYNC) {
421 if (bp->b_bufsize == fs->fs_bsize)
422 bp->b_flags |= B_CLUSTEROK;
425 curthread_pflags_restore(saved_inbdflush);
430 if (flags & BA_CLRBUF) {
431 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
432 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
433 error = cluster_read_gb(vp, ip->i_size, lbn,
434 (int)fs->fs_bsize, NOCRED,
435 MAXBSIZE, seqcount, gbflags, &nbp);
437 error = bread_gb(vp, lbn, (int)fs->fs_bsize, NOCRED,
445 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
446 nbp->b_blkno = fsbtodb(fs, nb);
448 curthread_pflags_restore(saved_inbdflush);
452 curthread_pflags_restore(saved_inbdflush);
454 * If we have failed to allocate any blocks, simply return the error.
455 * This is the usual case and avoids the need to fsync the file.
457 if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
460 * If we have failed part way through block allocation, we
461 * have to deallocate any indirect blocks that we have allocated.
462 * We have to fsync the file before we start to get rid of all
463 * of its dependencies so that we do not leave them dangling.
464 * We have to sync it at the end so that the soft updates code
465 * does not find any untracked changes. Although this is really
466 * slow, running out of disk space is not expected to be a common
467 * occurence. The error return from fsync is ignored as we already
468 * have an error to return to the user.
470 * XXX Still have to journal the free below
472 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
473 for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
474 blkp < allocblk; blkp++, lbns_remfree++) {
476 * We shall not leave the freed blocks on the vnode
477 * buffer object lists.
479 bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0, GB_NOCREAT);
481 bp->b_flags |= B_INVAL | B_RELBUF | B_NOCACHE;
482 bp->b_flags &= ~(B_ASYNC | B_CACHE);
485 deallocated += fs->fs_bsize;
487 if (allocib != NULL) {
489 } else if (unwindidx >= 0) {
492 r = bread(vp, indirs[unwindidx].in_lbn,
493 (int)fs->fs_bsize, NOCRED, &bp);
495 panic("Could not unwind indirect block, error %d", r);
498 bap = (ufs1_daddr_t *)bp->b_data;
499 bap[indirs[unwindidx].in_off] = 0;
500 if (flags & IO_SYNC) {
503 if (bp->b_bufsize == fs->fs_bsize)
504 bp->b_flags |= B_CLUSTEROK;
512 * Restore user's disk quota because allocation failed.
514 (void) chkdq(ip, -btodb(deallocated), cred, FORCE);
516 dp->di_blocks -= btodb(deallocated);
517 ip->i_flag |= IN_CHANGE | IN_UPDATE;
519 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
521 * After the buffers are invalidated and on-disk pointers are
522 * cleared, free the blocks.
524 for (blkp = allociblk; blkp < allocblk; blkp++) {
525 ffs_blkfree(ump, fs, ip->i_devvp, *blkp, fs->fs_bsize,
526 ip->i_number, vp->v_type, NULL);
532 * Balloc defines the structure of file system storage
533 * by allocating the physical blocks on a device given
534 * the inode and the logical block number in a file.
535 * This is the allocation strategy for UFS2. Above is
536 * the allocation strategy for UFS1.
539 ffs_balloc_ufs2(struct vnode *vp, off_t startoffset, int size,
540 struct ucred *cred, int flags, struct buf **bpp)
543 struct ufs2_dinode *dp;
544 ufs_lbn_t lbn, lastlbn;
546 struct buf *bp, *nbp;
547 struct ufsmount *ump;
548 struct indir indirs[NIADDR + 2];
549 ufs2_daddr_t nb, newb, *bap, pref;
550 ufs2_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
551 ufs2_daddr_t *lbns_remfree, lbns[NIADDR + 1];
552 int deallocated, osize, nsize, num, i, error;
555 static struct timeval lastfail;
557 int gbflags, reclaimed;
563 lbn = lblkno(fs, startoffset);
564 size = blkoff(fs, startoffset) + size;
566 if (size > fs->fs_bsize)
567 panic("ffs_balloc_ufs2: blk too big");
571 gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
573 if (DOINGSOFTDEP(vp))
574 softdep_prealloc(vp, MNT_WAIT);
577 * Check for allocating external data.
579 if (flags & IO_EXT) {
583 * If the next write will extend the data into a new block,
584 * and the data is currently composed of a fragment
585 * this fragment has to be extended to be a full block.
587 lastlbn = lblkno(fs, dp->di_extsize);
590 osize = sblksize(fs, dp->di_extsize, nb);
591 if (osize < fs->fs_bsize && osize > 0) {
593 error = ffs_realloccg(ip, -1 - nb,
595 ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
596 &dp->di_extb[0]), osize,
597 (int)fs->fs_bsize, flags, cred, &bp);
600 if (DOINGSOFTDEP(vp))
601 softdep_setup_allocext(ip, nb,
602 dbtofsb(fs, bp->b_blkno),
604 fs->fs_bsize, osize, bp);
605 dp->di_extsize = smalllblktosize(fs, nb + 1);
606 dp->di_extb[nb] = dbtofsb(fs, bp->b_blkno);
607 bp->b_xflags |= BX_ALTDATA;
608 ip->i_flag |= IN_CHANGE;
616 * All blocks are direct blocks
618 if (flags & BA_METAONLY)
619 panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
620 nb = dp->di_extb[lbn];
621 if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
622 error = bread_gb(vp, -1 - lbn, fs->fs_bsize, NOCRED,
628 bp->b_blkno = fsbtodb(fs, nb);
629 bp->b_xflags |= BX_ALTDATA;
635 * Consider need to reallocate a fragment.
637 osize = fragroundup(fs, blkoff(fs, dp->di_extsize));
638 nsize = fragroundup(fs, size);
639 if (nsize <= osize) {
640 error = bread_gb(vp, -1 - lbn, osize, NOCRED,
646 bp->b_blkno = fsbtodb(fs, nb);
647 bp->b_xflags |= BX_ALTDATA;
650 error = ffs_realloccg(ip, -1 - lbn,
652 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
653 &dp->di_extb[0]), osize, nsize, flags,
657 bp->b_xflags |= BX_ALTDATA;
658 if (DOINGSOFTDEP(vp))
659 softdep_setup_allocext(ip, lbn,
660 dbtofsb(fs, bp->b_blkno), nb,
664 if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
665 nsize = fragroundup(fs, size);
667 nsize = fs->fs_bsize;
669 error = ffs_alloc(ip, lbn,
670 ffs_blkpref_ufs2(ip, lbn, (int)lbn, &dp->di_extb[0]),
671 nsize, flags, cred, &newb);
674 bp = getblk(vp, -1 - lbn, nsize, 0, 0, gbflags);
675 bp->b_blkno = fsbtodb(fs, newb);
676 bp->b_xflags |= BX_ALTDATA;
677 if (flags & BA_CLRBUF)
679 if (DOINGSOFTDEP(vp))
680 softdep_setup_allocext(ip, lbn, newb, 0,
683 dp->di_extb[lbn] = dbtofsb(fs, bp->b_blkno);
684 ip->i_flag |= IN_CHANGE;
689 * If the next write will extend the file into a new block,
690 * and the file is currently composed of a fragment
691 * this fragment has to be extended to be a full block.
693 lastlbn = lblkno(fs, ip->i_size);
694 if (lastlbn < NDADDR && lastlbn < lbn) {
696 osize = blksize(fs, ip, nb);
697 if (osize < fs->fs_bsize && osize > 0) {
699 error = ffs_realloccg(ip, nb, dp->di_db[nb],
700 ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
701 &dp->di_db[0]), osize, (int)fs->fs_bsize,
705 if (DOINGSOFTDEP(vp))
706 softdep_setup_allocdirect(ip, nb,
707 dbtofsb(fs, bp->b_blkno),
709 fs->fs_bsize, osize, bp);
710 ip->i_size = smalllblktosize(fs, nb + 1);
711 dp->di_size = ip->i_size;
712 dp->di_db[nb] = dbtofsb(fs, bp->b_blkno);
713 ip->i_flag |= IN_CHANGE | IN_UPDATE;
721 * The first NDADDR blocks are direct blocks
724 if (flags & BA_METAONLY)
725 panic("ffs_balloc_ufs2: BA_METAONLY for direct block");
727 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
728 error = bread_gb(vp, lbn, fs->fs_bsize, NOCRED,
734 bp->b_blkno = fsbtodb(fs, nb);
740 * Consider need to reallocate a fragment.
742 osize = fragroundup(fs, blkoff(fs, ip->i_size));
743 nsize = fragroundup(fs, size);
744 if (nsize <= osize) {
745 error = bread_gb(vp, lbn, osize, NOCRED,
751 bp->b_blkno = fsbtodb(fs, nb);
754 error = ffs_realloccg(ip, lbn, dp->di_db[lbn],
755 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
756 &dp->di_db[0]), osize, nsize, flags,
760 if (DOINGSOFTDEP(vp))
761 softdep_setup_allocdirect(ip, lbn,
762 dbtofsb(fs, bp->b_blkno), nb,
766 if (ip->i_size < smalllblktosize(fs, lbn + 1))
767 nsize = fragroundup(fs, size);
769 nsize = fs->fs_bsize;
771 error = ffs_alloc(ip, lbn,
772 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
773 &dp->di_db[0]), nsize, flags, cred, &newb);
776 bp = getblk(vp, lbn, nsize, 0, 0, gbflags);
777 bp->b_blkno = fsbtodb(fs, newb);
778 if (flags & BA_CLRBUF)
780 if (DOINGSOFTDEP(vp))
781 softdep_setup_allocdirect(ip, lbn, newb, 0,
784 dp->di_db[lbn] = dbtofsb(fs, bp->b_blkno);
785 ip->i_flag |= IN_CHANGE | IN_UPDATE;
790 * Determine the number of levels of indirection.
793 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
797 panic ("ffs_balloc_ufs2: ufs_getlbns returned indirect block");
799 saved_inbdflush = curthread_pflags_set(TDP_INBDFLUSH);
801 * Fetch the first indirect block allocating if necessary.
804 nb = dp->di_ib[indirs[0].in_off];
806 allocblk = allociblk;
810 pref = ffs_blkpref_ufs2(ip, lbn, -indirs[0].in_off - 1,
812 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
813 flags, cred, &newb)) != 0) {
814 curthread_pflags_restore(saved_inbdflush);
817 pref = newb + fs->fs_frag;
820 *lbns_remfree++ = indirs[1].in_lbn;
821 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0,
823 bp->b_blkno = fsbtodb(fs, nb);
825 if (DOINGSOFTDEP(vp)) {
826 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
827 newb, 0, fs->fs_bsize, 0, bp);
831 * Write synchronously so that indirect blocks
832 * never point at garbage.
836 else if ((error = bwrite(bp)) != 0)
839 allocib = &dp->di_ib[indirs[0].in_off];
841 ip->i_flag |= IN_CHANGE | IN_UPDATE;
844 * Fetch through the indirect blocks, allocating as necessary.
849 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
854 bap = (ufs2_daddr_t *)bp->b_data;
855 nb = bap[indirs[i].in_off];
865 * If parent indirect has just been allocated, try to cluster
866 * immediately following it.
869 pref = ffs_blkpref_ufs2(ip, lbn, i - num - 1,
871 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
872 flags | IO_BUFLOCKED, cred, &newb)) != 0) {
874 if (++reclaimed == 1) {
876 softdep_request_cleanup(fs, vp, cred,
881 if (ppsratecheck(&lastfail, &curfail, 1)) {
882 ffs_fserr(fs, ip->i_number, "filesystem full");
883 uprintf("\n%s: write failed, filesystem "
884 "is full\n", fs->fs_fsmnt);
888 pref = newb + fs->fs_frag;
891 *lbns_remfree++ = indirs[i].in_lbn;
892 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0,
894 nbp->b_blkno = fsbtodb(fs, nb);
896 if (DOINGSOFTDEP(vp)) {
897 softdep_setup_allocindir_meta(nbp, ip, bp,
898 indirs[i - 1].in_off, nb);
902 * Write synchronously so that indirect blocks
903 * never point at garbage.
905 if ((error = bwrite(nbp)) != 0) {
910 bap[indirs[i - 1].in_off] = nb;
911 if (allocib == NULL && unwindidx < 0)
914 * If required, write synchronously, otherwise use
917 if (flags & IO_SYNC) {
920 if (bp->b_bufsize == fs->fs_bsize)
921 bp->b_flags |= B_CLUSTEROK;
926 * If asked only for the indirect block, then return it.
928 if (flags & BA_METAONLY) {
929 curthread_pflags_restore(saved_inbdflush);
934 * Get the data block, allocating if necessary.
939 * If allocating metadata at the front of the cylinder
940 * group and parent indirect block has just been allocated,
941 * then cluster next to it if it is the first indirect in
942 * the file. Otherwise it has been allocated in the metadata
943 * area, so we want to find our own place out in the data area.
945 if (pref == 0 || (lbn > NDADDR && fs->fs_metaspace != 0))
946 pref = ffs_blkpref_ufs2(ip, lbn, indirs[i].in_off,
948 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
949 flags | IO_BUFLOCKED, cred, &newb);
952 if (++reclaimed == 1) {
954 softdep_request_cleanup(fs, vp, cred,
959 if (ppsratecheck(&lastfail, &curfail, 1)) {
960 ffs_fserr(fs, ip->i_number, "filesystem full");
961 uprintf("\n%s: write failed, filesystem "
962 "is full\n", fs->fs_fsmnt);
968 *lbns_remfree++ = lbn;
969 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
970 nbp->b_blkno = fsbtodb(fs, nb);
971 if (flags & BA_CLRBUF)
973 if (DOINGSOFTDEP(vp))
974 softdep_setup_allocindir_page(ip, lbn, bp,
975 indirs[i].in_off, nb, 0, nbp);
976 bap[indirs[i].in_off] = nb;
978 * If required, write synchronously, otherwise use
981 if (flags & IO_SYNC) {
984 if (bp->b_bufsize == fs->fs_bsize)
985 bp->b_flags |= B_CLUSTEROK;
988 curthread_pflags_restore(saved_inbdflush);
994 * If requested clear invalid portions of the buffer. If we
995 * have to do a read-before-write (typical if BA_CLRBUF is set),
996 * try to do some read-ahead in the sequential case to reduce
997 * the number of I/O transactions.
999 if (flags & BA_CLRBUF) {
1000 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
1001 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
1002 error = cluster_read_gb(vp, ip->i_size, lbn,
1003 (int)fs->fs_bsize, NOCRED,
1004 MAXBSIZE, seqcount, gbflags, &nbp);
1006 error = bread_gb(vp, lbn, (int)fs->fs_bsize,
1007 NOCRED, gbflags, &nbp);
1014 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0, gbflags);
1015 nbp->b_blkno = fsbtodb(fs, nb);
1017 curthread_pflags_restore(saved_inbdflush);
1021 curthread_pflags_restore(saved_inbdflush);
1023 * If we have failed to allocate any blocks, simply return the error.
1024 * This is the usual case and avoids the need to fsync the file.
1026 if (allocblk == allociblk && allocib == NULL && unwindidx == -1)
1029 * If we have failed part way through block allocation, we
1030 * have to deallocate any indirect blocks that we have allocated.
1031 * We have to fsync the file before we start to get rid of all
1032 * of its dependencies so that we do not leave them dangling.
1033 * We have to sync it at the end so that the soft updates code
1034 * does not find any untracked changes. Although this is really
1035 * slow, running out of disk space is not expected to be a common
1036 * occurrence. The error return from fsync is ignored as we already
1037 * have an error to return to the user.
1039 * XXX Still have to journal the free below
1041 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
1042 for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
1043 blkp < allocblk; blkp++, lbns_remfree++) {
1045 * We shall not leave the freed blocks on the vnode
1046 * buffer object lists.
1048 bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0, GB_NOCREAT);
1050 bp->b_flags |= B_INVAL | B_RELBUF | B_NOCACHE;
1051 bp->b_flags &= ~(B_ASYNC | B_CACHE);
1054 deallocated += fs->fs_bsize;
1056 if (allocib != NULL) {
1058 } else if (unwindidx >= 0) {
1061 r = bread(vp, indirs[unwindidx].in_lbn,
1062 (int)fs->fs_bsize, NOCRED, &bp);
1064 panic("Could not unwind indirect block, error %d", r);
1067 bap = (ufs2_daddr_t *)bp->b_data;
1068 bap[indirs[unwindidx].in_off] = 0;
1069 if (flags & IO_SYNC) {
1072 if (bp->b_bufsize == fs->fs_bsize)
1073 bp->b_flags |= B_CLUSTEROK;
1081 * Restore user's disk quota because allocation failed.
1083 (void) chkdq(ip, -btodb(deallocated), cred, FORCE);
1085 dp->di_blocks -= btodb(deallocated);
1086 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1088 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
1090 * After the buffers are invalidated and on-disk pointers are
1091 * cleared, free the blocks.
1093 for (blkp = allociblk; blkp < allocblk; blkp++) {
1094 ffs_blkfree(ump, fs, ip->i_devvp, *blkp, fs->fs_bsize,
1095 ip->i_number, vp->v_type, NULL);