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