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/mount.h>
45 #include <sys/vnode.h>
47 #include <fs/ext2fs/fs.h>
48 #include <fs/ext2fs/inode.h>
49 #include <fs/ext2fs/ext2fs.h>
50 #include <fs/ext2fs/ext2_dinode.h>
51 #include <fs/ext2fs/ext2_extern.h>
52 #include <fs/ext2fs/ext2_mount.h>
55 * Balloc defines the structure of filesystem storage
56 * by allocating the physical blocks on a device given
57 * the inode and the logical block number in a file.
60 ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
61 struct buf **bpp, int flags)
64 struct ext2mount *ump;
66 struct vnode *vp = ITOV(ip);
67 struct indir indirs[NIADDR + 2];
68 e4fs_daddr_t nb, newb;
69 e2fs_daddr_t *bap, pref;
70 int osize, nsize, num, i, error;
79 * check if this is a sequential block allocation.
80 * If so, increment next_alloc fields to allow ext2_blkpref
81 * to make a good guess
83 if (lbn == ip->i_next_alloc_block + 1) {
84 ip->i_next_alloc_block++;
85 ip->i_next_alloc_goal++;
89 * The first NDADDR blocks are direct blocks
93 /* no new block is to be allocated, and no need to expand
95 if (nb != 0 && ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
96 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
101 bp->b_blkno = fsbtodb(fs, nb);
107 * Consider need to reallocate a fragment.
109 osize = fragroundup(fs, blkoff(fs, ip->i_size));
110 nsize = fragroundup(fs, size);
111 if (nsize <= osize) {
112 error = bread(vp, lbn, osize, NOCRED, &bp);
117 bp->b_blkno = fsbtodb(fs, nb);
119 /* Godmar thinks: this shouldn't happen w/o fragments */
120 printf("nsize %d(%d) > osize %d(%d) nb %d\n",
121 (int)nsize, (int)size, (int)osize,
122 (int)ip->i_size, (int)nb);
124 "ext2_balloc: Something is terribly wrong");
126 * please note there haven't been any changes from here on -
131 if (ip->i_size < (lbn + 1) * fs->e2fs_bsize)
132 nsize = fragroundup(fs, size);
134 nsize = fs->e2fs_bsize;
136 error = ext2_alloc(ip, lbn,
137 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0),
141 bp = getblk(vp, lbn, nsize, 0, 0, 0);
142 bp->b_blkno = fsbtodb(fs, newb);
143 if (flags & BA_CLRBUF)
146 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
147 ip->i_flag |= IN_CHANGE | IN_UPDATE;
152 * Determine the number of levels of indirection.
155 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0)
159 panic ("ext2_balloc: ext2_getlbns returned indirect block");
162 * Fetch the first indirect block allocating if necessary.
165 nb = ip->i_ib[indirs[0].in_off];
168 pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
169 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
170 if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred,
174 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0);
175 bp->b_blkno = fsbtodb(fs, newb);
178 * Write synchronously so that indirect blocks
179 * never point at garbage.
181 if ((error = bwrite(bp)) != 0) {
182 ext2_blkfree(ip, nb, fs->e2fs_bsize);
185 ip->i_ib[indirs[0].in_off] = newb;
186 ip->i_flag |= IN_CHANGE | IN_UPDATE;
189 * Fetch through the indirect blocks, allocating as necessary.
193 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
198 bap = (e2fs_daddr_t *)bp->b_data;
199 nb = bap[indirs[i].in_off];
209 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
211 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb);
217 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0);
218 nbp->b_blkno = fsbtodb(fs, nb);
221 * Write synchronously so that indirect blocks
222 * never point at garbage.
224 if ((error = bwrite(nbp)) != 0) {
225 ext2_blkfree(ip, nb, fs->e2fs_bsize);
230 bap[indirs[i - 1].in_off] = nb;
232 * If required, write synchronously, otherwise use
235 if (flags & IO_SYNC) {
238 if (bp->b_bufsize == fs->e2fs_bsize)
239 bp->b_flags |= B_CLUSTEROK;
244 * Get the data block, allocating if necessary.
248 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
250 if ((error = ext2_alloc(ip,
251 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) {
256 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
257 nbp->b_blkno = fsbtodb(fs, nb);
258 if (flags & BA_CLRBUF)
260 bap[indirs[i].in_off] = nb;
262 * If required, write synchronously, otherwise use
265 if (flags & IO_SYNC) {
268 if (bp->b_bufsize == fs->e2fs_bsize)
269 bp->b_flags |= B_CLUSTEROK;
276 if (flags & BA_CLRBUF) {
277 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
278 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
279 error = cluster_read(vp, ip->i_size, lbn,
280 (int)fs->e2fs_bsize, NOCRED,
281 MAXBSIZE, seqcount, 0, &nbp);
283 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
290 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
291 nbp->b_blkno = fsbtodb(fs, nb);