1 /*-
2 * modified for Lites 1.1
3 *
4 * Aug 1995, Godmar Back ([email protected])
5 * University of Utah, Department of Computer Science
6 */
7 /*-
8 * SPDX-License-Identifier: BSD-3-Clause
9 *
10 * Copyright (c) 1982, 1986, 1989, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
24 *
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
35 * SUCH DAMAGE.
36 *
37 * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/endian.h>
43 #include <sys/bio.h>
44 #include <sys/buf.h>
45 #include <sys/limits.h>
46 #include <sys/lock.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49
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>
56
57 static int
ext2_ext_balloc(struct inode * ip,uint32_t lbn,int size,struct ucred * cred,struct buf ** bpp,int flags)58 ext2_ext_balloc(struct inode *ip, uint32_t lbn, int size,
59 struct ucred *cred, struct buf **bpp, int flags)
60 {
61 struct m_ext2fs *fs;
62 struct buf *bp = NULL;
63 struct vnode *vp = ITOV(ip);
64 daddr_t newblk;
65 int blks, error, allocated;
66
67 fs = ip->i_e2fs;
68 blks = howmany(size, fs->e2fs_bsize);
69
70 error = ext4_ext_get_blocks(ip, lbn, blks, cred, NULL, &allocated, &newblk);
71 if (error)
72 return (error);
73
74 if (allocated) {
75 bp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
76 if(!bp)
77 return (EIO);
78 } else {
79 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
80 if (error) {
81 return (error);
82 }
83 }
84
85 bp->b_blkno = fsbtodb(fs, newblk);
86 if (flags & BA_CLRBUF)
87 vfs_bio_clrbuf(bp);
88
89 *bpp = bp;
90
91 return (error);
92 }
93
94 /*
95 * Balloc defines the structure of filesystem storage
96 * by allocating the physical blocks on a device given
97 * the inode and the logical block number in a file.
98 */
99 int
ext2_balloc(struct inode * ip,e2fs_lbn_t lbn,int size,struct ucred * cred,struct buf ** bpp,int flags)100 ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
101 struct buf **bpp, int flags)
102 {
103 struct m_ext2fs *fs;
104 struct ext2mount *ump;
105 struct buf *bp, *nbp;
106 struct vnode *vp = ITOV(ip);
107 struct indir indirs[EXT2_NIADDR + 2];
108 e4fs_daddr_t nb, newb;
109 e2fs_daddr_t *bap, pref;
110 int num, i, error;
111
112 *bpp = NULL;
113 if (lbn < 0)
114 return (EFBIG);
115 fs = ip->i_e2fs;
116 ump = ip->i_ump;
117
118 /*
119 * check if this is a sequential block allocation.
120 * If so, increment next_alloc fields to allow ext2_blkpref
121 * to make a good guess
122 */
123 if (lbn == ip->i_next_alloc_block + 1) {
124 ip->i_next_alloc_block++;
125 ip->i_next_alloc_goal++;
126 }
127
128 if (ip->i_flag & IN_E4EXTENTS)
129 return (ext2_ext_balloc(ip, lbn, size, cred, bpp, flags));
130
131 /*
132 * The first EXT2_NDADDR blocks are direct blocks
133 */
134 if (lbn < EXT2_NDADDR) {
135 nb = ip->i_db[lbn];
136 /*
137 * no new block is to be allocated, and no need to expand
138 * the file
139 */
140 if (nb != 0) {
141 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
142 if (error) {
143 return (error);
144 }
145 bp->b_blkno = fsbtodb(fs, nb);
146 if (ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
147 *bpp = bp;
148 return (0);
149 }
150 } else {
151 EXT2_LOCK(ump);
152 error = ext2_alloc(ip, lbn,
153 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0),
154 fs->e2fs_bsize, cred, &newb);
155 if (error)
156 return (error);
157 /*
158 * If the newly allocated block exceeds 32-bit limit,
159 * we can not use it in file block maps.
160 */
161 if (newb > UINT_MAX)
162 return (EFBIG);
163 bp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
164 bp->b_blkno = fsbtodb(fs, newb);
165 if (flags & BA_CLRBUF)
166 vfs_bio_clrbuf(bp);
167 }
168 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
169 ip->i_flag |= IN_CHANGE | IN_UPDATE;
170 *bpp = bp;
171 return (0);
172 }
173 /*
174 * Determine the number of levels of indirection.
175 */
176 pref = 0;
177 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0)
178 return (error);
179 #ifdef INVARIANTS
180 if (num < 1)
181 panic("ext2_balloc: ext2_getlbns returned indirect block");
182 #endif
183 /*
184 * Fetch the first indirect block allocating if necessary.
185 */
186 --num;
187 nb = ip->i_ib[indirs[0].in_off];
188 if (nb == 0) {
189 EXT2_LOCK(ump);
190 pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
191 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
192 if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred,
193 &newb)))
194 return (error);
195 if (newb > UINT_MAX)
196 return (EFBIG);
197 nb = newb;
198 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0);
199 bp->b_blkno = fsbtodb(fs, newb);
200 vfs_bio_clrbuf(bp);
201 /*
202 * Write synchronously so that indirect blocks
203 * never point at garbage.
204 */
205 if ((error = bwrite(bp)) != 0) {
206 ext2_blkfree(ip, nb, fs->e2fs_bsize);
207 return (error);
208 }
209 ip->i_ib[indirs[0].in_off] = newb;
210 ip->i_flag |= IN_CHANGE | IN_UPDATE;
211 }
212 /*
213 * Fetch through the indirect blocks, allocating as necessary.
214 */
215 for (i = 1;;) {
216 error = bread(vp,
217 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
218 if (error) {
219 return (error);
220 }
221 bap = (e2fs_daddr_t *)bp->b_data;
222 nb = le32toh(bap[indirs[i].in_off]);
223 if (i == num)
224 break;
225 i += 1;
226 if (nb != 0) {
227 bqrelse(bp);
228 continue;
229 }
230 EXT2_LOCK(ump);
231 if (pref == 0)
232 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
233 bp->b_lblkno);
234 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb);
235 if (error) {
236 brelse(bp);
237 return (error);
238 }
239 if (newb > UINT_MAX)
240 return (EFBIG);
241 nb = newb;
242 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0);
243 nbp->b_blkno = fsbtodb(fs, nb);
244 vfs_bio_clrbuf(nbp);
245 /*
246 * Write synchronously so that indirect blocks
247 * never point at garbage.
248 */
249 if ((error = bwrite(nbp)) != 0) {
250 ext2_blkfree(ip, nb, fs->e2fs_bsize);
251 brelse(bp);
252 return (error);
253 }
254 bap[indirs[i - 1].in_off] = htole32(nb);
255 /*
256 * If required, write synchronously, otherwise use
257 * delayed write.
258 */
259 if (flags & IO_SYNC) {
260 bwrite(bp);
261 } else {
262 if (bp->b_bufsize == fs->e2fs_bsize)
263 bp->b_flags |= B_CLUSTEROK;
264 bdwrite(bp);
265 }
266 }
267 /*
268 * Get the data block, allocating if necessary.
269 */
270 if (nb == 0) {
271 EXT2_LOCK(ump);
272 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
273 bp->b_lblkno);
274 if ((error = ext2_alloc(ip,
275 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) {
276 brelse(bp);
277 return (error);
278 }
279 if (newb > UINT_MAX)
280 return (EFBIG);
281 nb = newb;
282 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
283 nbp->b_blkno = fsbtodb(fs, nb);
284 if (flags & BA_CLRBUF)
285 vfs_bio_clrbuf(nbp);
286 bap[indirs[i].in_off] = htole32(nb);
287 /*
288 * If required, write synchronously, otherwise use
289 * delayed write.
290 */
291 if (flags & IO_SYNC) {
292 bwrite(bp);
293 } else {
294 if (bp->b_bufsize == fs->e2fs_bsize)
295 bp->b_flags |= B_CLUSTEROK;
296 bdwrite(bp);
297 }
298 *bpp = nbp;
299 return (0);
300 }
301 brelse(bp);
302 if (flags & BA_CLRBUF) {
303 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
304
305 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
306 error = cluster_read(vp, ip->i_size, lbn,
307 (int)fs->e2fs_bsize, NOCRED,
308 MAXBSIZE, seqcount, 0, &nbp);
309 } else {
310 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
311 }
312 if (error) {
313 brelse(nbp);
314 return (error);
315 }
316 } else {
317 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
318 nbp->b_blkno = fsbtodb(fs, nb);
319 }
320 *bpp = nbp;
321 return (0);
322 }
323