2 * modified for Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
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.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93
39 #include <sys/param.h>
40 #include <sys/systm.h>
44 #include <sys/ucred.h>
45 #include <sys/vnode.h>
47 #include <gnu/fs/ext2fs/inode.h>
48 #include <gnu/fs/ext2fs/ext2_fs.h>
49 #include <gnu/fs/ext2fs/ext2_fs_sb.h>
50 #include <gnu/fs/ext2fs/fs.h>
51 #include <gnu/fs/ext2fs/ext2_extern.h>
54 * Balloc defines the structure of file system storage
55 * by allocating the physical blocks on a device given
56 * the inode and the logical block number in a file.
59 ext2_balloc(ip, bn, size, cred, bpp, flags)
67 struct ext2_sb_info *fs;
70 struct vnode *vp = ITOV(ip);
71 struct indir indirs[NIADDR + 2];
72 int32_t newb, lbn, *bap, pref;
73 int osize, nsize, num, i, error;
75 ext2_debug("ext2_balloc called (%d, %d, %d)\n",
76 ip->i_number, (int)bn, (int)size);
85 * check if this is a sequential block allocation.
86 * If so, increment next_alloc fields to allow ext2_blkpref
87 * to make a good guess
89 if (lbn == ip->i_next_alloc_block + 1) {
90 ip->i_next_alloc_block++;
91 ip->i_next_alloc_goal++;
95 * The first NDADDR blocks are direct blocks
99 /* no new block is to be allocated, and no need to expand
101 if (nb != 0 && ip->i_size >= (bn + 1) * fs->s_blocksize) {
102 error = bread(vp, bn, fs->s_blocksize, NOCRED, &bp);
107 bp->b_blkno = fsbtodb(fs, nb);
113 * Consider need to reallocate a fragment.
115 osize = fragroundup(fs, blkoff(fs, ip->i_size));
116 nsize = fragroundup(fs, size);
117 if (nsize <= osize) {
118 error = bread(vp, bn, osize, NOCRED, &bp);
123 bp->b_blkno = fsbtodb(fs, nb);
125 /* Godmar thinks: this shouldn't happen w/o fragments */
126 printf("nsize %d(%d) > osize %d(%d) nb %d\n",
127 (int)nsize, (int)size, (int)osize,
128 (int)ip->i_size, (int)nb);
130 "ext2_balloc: Something is terribly wrong");
132 * please note there haven't been any changes from here on -
137 if (ip->i_size < (bn + 1) * fs->s_blocksize)
138 nsize = fragroundup(fs, size);
140 nsize = fs->s_blocksize;
141 error = ext2_alloc(ip, bn,
142 ext2_blkpref(ip, bn, (int)bn, &ip->i_db[0], 0),
146 bp = getblk(vp, bn, nsize, 0, 0, 0);
147 bp->b_blkno = fsbtodb(fs, newb);
148 if (flags & B_CLRBUF)
151 ip->i_db[bn] = dbtofsb(fs, bp->b_blkno);
152 ip->i_flag |= IN_CHANGE | IN_UPDATE;
157 * Determine the number of levels of indirection.
160 if ((error = ext2_getlbns(vp, bn, indirs, &num)) != 0)
164 panic ("ext2_balloc: ext2_getlbns returned indirect block");
167 * Fetch the first indirect block allocating if necessary.
170 nb = ip->i_ib[indirs[0].in_off];
173 pref = ext2_blkpref(ip, lbn, 0, (int32_t *)0, 0);
175 /* see the comment by ext2_blkpref. What we do here is
176 to pretend that it'd be good for a block holding indirect
177 pointers to be allocated near its predecessor in terms
178 of indirection, or the last direct block.
179 We shamelessly exploit the fact that i_ib immediately
181 Godmar thinks it make sense to allocate i_ib[0] immediately
182 after i_db[11], but it's not utterly clear whether this also
183 applies to i_ib[1] and i_ib[0]
186 pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
187 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
189 if ((error = ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize,
193 bp = getblk(vp, indirs[1].in_lbn, fs->s_blocksize, 0, 0, 0);
194 bp->b_blkno = fsbtodb(fs, newb);
197 * Write synchronously so that indirect blocks
198 * never point at garbage.
200 if ((error = bwrite(bp)) != 0) {
201 ext2_blkfree(ip, nb, fs->s_blocksize);
204 ip->i_ib[indirs[0].in_off] = newb;
205 ip->i_flag |= IN_CHANGE | IN_UPDATE;
208 * Fetch through the indirect blocks, allocating as necessary.
212 indirs[i].in_lbn, (int)fs->s_blocksize, NOCRED, &bp);
217 bap = (int32_t *)bp->b_data;
218 nb = bap[indirs[i].in_off];
228 /* see the comment above and by ext2_blkpref
229 * I think this implements Linux policy, but
230 * does it really make sense to allocate to
231 * block containing pointers together ?
232 * Also, will it ever succeed ?
234 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
237 pref = ext2_blkpref(ip, lbn, 0, (int32_t *)0, 0);
240 ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) {
245 nbp = getblk(vp, indirs[i].in_lbn, fs->s_blocksize, 0, 0, 0);
246 nbp->b_blkno = fsbtodb(fs, nb);
249 * Write synchronously so that indirect blocks
250 * never point at garbage.
252 if ((error = bwrite(nbp)) != 0) {
253 ext2_blkfree(ip, nb, fs->s_blocksize);
257 bap[indirs[i - 1].in_off] = nb;
259 * If required, write synchronously, otherwise use
262 if (flags & B_SYNC) {
269 * Get the data block, allocating if necessary.
272 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
274 if ((error = ext2_alloc(ip,
275 lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) {
280 nbp = getblk(vp, lbn, fs->s_blocksize, 0, 0, 0);
281 nbp->b_blkno = fsbtodb(fs, nb);
282 if (flags & B_CLRBUF)
284 bap[indirs[i].in_off] = nb;
286 * If required, write synchronously, otherwise use
289 if (flags & B_SYNC) {
298 if (flags & B_CLRBUF) {
299 error = bread(vp, lbn, (int)fs->s_blocksize, NOCRED, &nbp);
305 nbp = getblk(vp, lbn, fs->s_blocksize, 0, 0, 0);
306 nbp->b_blkno = fsbtodb(fs, nb);