2 * modified for Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * SPDX-License-Identifier: BSD-3-Clause
10 * Copyright (c) 1982, 1986, 1989, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93
41 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <sys/limits.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
50 #include <fs/ext2fs/fs.h>
51 #include <fs/ext2fs/inode.h>
52 #include <fs/ext2fs/ext2fs.h>
53 #include <fs/ext2fs/ext2_dinode.h>
54 #include <fs/ext2fs/ext2_extern.h>
55 #include <fs/ext2fs/ext2_mount.h>
58 ext2_ext_balloc(struct inode *ip, uint32_t lbn, int size,
59 struct ucred *cred, struct buf **bpp, int flags)
62 struct buf *bp = NULL;
63 struct vnode *vp = ITOV(ip);
65 int osize, nsize, blks, error, allocated;
68 blks = howmany(size, fs->e2fs_bsize);
70 error = ext4_ext_get_blocks(ip, lbn, blks, cred, NULL, &allocated, &newblk);
75 if (ip->i_size < (lbn + 1) * fs->e2fs_bsize)
76 nsize = fragroundup(fs, size);
78 nsize = fs->e2fs_bsize;
80 bp = getblk(vp, lbn, nsize, 0, 0, 0);
84 bp->b_blkno = fsbtodb(fs, newblk);
85 if (flags & BA_CLRBUF)
88 if (ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
90 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
95 bp->b_blkno = fsbtodb(fs, newblk);
101 * Consider need to reallocate a fragment.
103 osize = fragroundup(fs, blkoff(fs, ip->i_size));
104 nsize = fragroundup(fs, size);
106 error = bread(vp, lbn, osize, NOCRED, &bp);
108 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
113 bp->b_blkno = fsbtodb(fs, newblk);
122 * Balloc defines the structure of filesystem storage
123 * by allocating the physical blocks on a device given
124 * the inode and the logical block number in a file.
127 ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
128 struct buf **bpp, int flags)
131 struct ext2mount *ump;
132 struct buf *bp, *nbp;
133 struct vnode *vp = ITOV(ip);
134 struct indir indirs[EXT2_NIADDR + 2];
135 e4fs_daddr_t nb, newb;
136 e2fs_daddr_t *bap, pref;
137 int osize, nsize, num, i, error;
146 * check if this is a sequential block allocation.
147 * If so, increment next_alloc fields to allow ext2_blkpref
148 * to make a good guess
150 if (lbn == ip->i_next_alloc_block + 1) {
151 ip->i_next_alloc_block++;
152 ip->i_next_alloc_goal++;
155 if (ip->i_flag & IN_E4EXTENTS)
156 return (ext2_ext_balloc(ip, lbn, size, cred, bpp, flags));
159 * The first EXT2_NDADDR blocks are direct blocks
161 if (lbn < EXT2_NDADDR) {
164 * no new block is to be allocated, and no need to expand
167 if (nb != 0 && ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
168 error = bread(vp, lbn, fs->e2fs_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 * Godmar thinks: this shouldn't happen w/o
195 printf("nsize %d(%d) > osize %d(%d) nb %d\n",
196 (int)nsize, (int)size, (int)osize,
197 (int)ip->i_size, (int)nb);
199 "ext2_balloc: Something is terribly wrong");
201 * please note there haven't been any changes from here on -
206 if (ip->i_size < (lbn + 1) * fs->e2fs_bsize)
207 nsize = fragroundup(fs, size);
209 nsize = fs->e2fs_bsize;
211 error = ext2_alloc(ip, lbn,
212 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0),
217 * If the newly allocated block exceeds 32-bit limit,
218 * we can not use it in file block maps.
222 bp = getblk(vp, lbn, nsize, 0, 0, 0);
223 bp->b_blkno = fsbtodb(fs, newb);
224 if (flags & BA_CLRBUF)
227 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
228 ip->i_flag |= IN_CHANGE | IN_UPDATE;
233 * Determine the number of levels of indirection.
236 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0)
240 panic("ext2_balloc: ext2_getlbns returned indirect block");
243 * Fetch the first indirect block allocating if necessary.
246 nb = ip->i_ib[indirs[0].in_off];
249 pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
250 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
251 if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred,
257 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0);
258 bp->b_blkno = fsbtodb(fs, newb);
261 * Write synchronously so that indirect blocks
262 * never point at garbage.
264 if ((error = bwrite(bp)) != 0) {
265 ext2_blkfree(ip, nb, fs->e2fs_bsize);
268 ip->i_ib[indirs[0].in_off] = newb;
269 ip->i_flag |= IN_CHANGE | IN_UPDATE;
272 * Fetch through the indirect blocks, allocating as necessary.
276 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
281 bap = (e2fs_daddr_t *)bp->b_data;
282 nb = bap[indirs[i].in_off];
292 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
294 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb);
302 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0);
303 nbp->b_blkno = fsbtodb(fs, nb);
306 * Write synchronously so that indirect blocks
307 * never point at garbage.
309 if ((error = bwrite(nbp)) != 0) {
310 ext2_blkfree(ip, nb, fs->e2fs_bsize);
315 bap[indirs[i - 1].in_off] = nb;
317 * If required, write synchronously, otherwise use
320 if (flags & IO_SYNC) {
323 if (bp->b_bufsize == fs->e2fs_bsize)
324 bp->b_flags |= B_CLUSTEROK;
329 * Get the data block, allocating if necessary.
333 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
335 if ((error = ext2_alloc(ip,
336 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) {
343 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
344 nbp->b_blkno = fsbtodb(fs, nb);
345 if (flags & BA_CLRBUF)
347 bap[indirs[i].in_off] = nb;
349 * If required, write synchronously, otherwise use
352 if (flags & IO_SYNC) {
355 if (bp->b_bufsize == fs->e2fs_bsize)
356 bp->b_flags |= B_CLUSTEROK;
363 if (flags & BA_CLRBUF) {
364 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
366 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
367 error = cluster_read(vp, ip->i_size, lbn,
368 (int)fs->e2fs_bsize, NOCRED,
369 MAXBSIZE, seqcount, 0, &nbp);
371 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
378 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
379 nbp->b_blkno = fsbtodb(fs, nb);