xref: /freebsd/sys/fs/ext2fs/ext2_balloc.c (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
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  * Copyright (c) 1982, 1986, 1989, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  *
35  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
36  * $FreeBSD$
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bio.h>
42 #include <sys/buf.h>
43 #include <sys/lock.h>
44 #include <sys/mount.h>
45 #include <sys/vnode.h>
46 
47 #include <fs/ext2fs/inode.h>
48 #include <fs/ext2fs/ext2fs.h>
49 #include <fs/ext2fs/fs.h>
50 #include <fs/ext2fs/ext2_extern.h>
51 #include <fs/ext2fs/ext2_mount.h>
52 /*
53  * Balloc defines the structure of file system storage
54  * by allocating the physical blocks on a device given
55  * the inode and the logical block number in a file.
56  */
57 int
58 ext2_balloc(struct inode *ip, int32_t lbn, int size, struct ucred *cred,
59     struct buf **bpp, int flags)
60 {
61 	struct m_ext2fs *fs;
62 	struct ext2mount *ump;
63 	int32_t nb;
64 	struct buf *bp, *nbp;
65 	struct vnode *vp = ITOV(ip);
66 	struct indir indirs[NIADDR + 2];
67 	uint32_t newb, *bap, pref;
68 	int osize, nsize, num, i, error;
69 
70 	*bpp = NULL;
71 	if (lbn < 0)
72 		return (EFBIG);
73 	fs = ip->i_e2fs;
74 	ump = ip->i_ump;
75 
76 	/*
77 	 * check if this is a sequential block allocation.
78 	 * If so, increment next_alloc fields to allow ext2_blkpref
79 	 * to make a good guess
80 	 */
81         if (lbn == ip->i_next_alloc_block + 1) {
82 		ip->i_next_alloc_block++;
83 		ip->i_next_alloc_goal++;
84 	}
85 
86 	/*
87 	 * The first NDADDR blocks are direct blocks
88 	 */
89 	if (lbn < NDADDR) {
90 		nb = ip->i_db[lbn];
91 		/* no new block is to be allocated, and no need to expand
92 		   the file */
93 		if (nb != 0 && ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
94 			error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
95 			if (error) {
96 				brelse(bp);
97 				return (error);
98 			}
99 			bp->b_blkno = fsbtodb(fs, nb);
100 			*bpp = bp;
101 			return (0);
102 		}
103 		if (nb != 0) {
104 			/*
105 			 * Consider need to reallocate a fragment.
106 			 */
107 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
108 			nsize = fragroundup(fs, size);
109 			if (nsize <= osize) {
110 				error = bread(vp, lbn, osize, NOCRED, &bp);
111 				if (error) {
112 					brelse(bp);
113 					return (error);
114 				}
115 				bp->b_blkno = fsbtodb(fs, nb);
116 			} else {
117 			/* Godmar thinks: this shouldn't happen w/o fragments */
118 				printf("nsize %d(%d) > osize %d(%d) nb %d\n",
119 					(int)nsize, (int)size, (int)osize,
120 					(int)ip->i_size, (int)nb);
121 				panic(
122 				    "ext2_balloc: Something is terribly wrong");
123 /*
124  * please note there haven't been any changes from here on -
125  * FFS seems to work.
126  */
127 			}
128 		} else {
129 			if (ip->i_size < (lbn + 1) * fs->e2fs_bsize)
130 				nsize = fragroundup(fs, size);
131 			else
132 				nsize = fs->e2fs_bsize;
133 			EXT2_LOCK(ump);
134 			error = ext2_alloc(ip, lbn,
135 			    ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0),
136 			    nsize, cred, &newb);
137 			if (error)
138 				return (error);
139 			bp = getblk(vp, lbn, nsize, 0, 0, 0);
140 			bp->b_blkno = fsbtodb(fs, newb);
141 			if (flags & BA_CLRBUF)
142 				vfs_bio_clrbuf(bp);
143 		}
144 		ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
145 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
146 		*bpp = bp;
147 		return (0);
148 	}
149 	/*
150 	 * Determine the number of levels of indirection.
151 	 */
152 	pref = 0;
153 	if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0)
154 		return (error);
155 #ifdef DIAGNOSTIC
156 	if (num < 1)
157 		panic ("ext2_balloc: ext2_getlbns returned indirect block");
158 #endif
159 	/*
160 	 * Fetch the first indirect block allocating if necessary.
161 	 */
162 	--num;
163 	nb = ip->i_ib[indirs[0].in_off];
164 	if (nb == 0) {
165 		EXT2_LOCK(ump);
166 		pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
167 					     EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
168 	        if ((error = ext2_alloc(ip, lbn, pref,
169 			(int)fs->e2fs_bsize, cred, &newb)))
170 			return (error);
171 		nb = newb;
172 		bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0);
173 		bp->b_blkno = fsbtodb(fs, newb);
174 		vfs_bio_clrbuf(bp);
175 		/*
176 		 * Write synchronously so that indirect blocks
177 		 * never point at garbage.
178 		 */
179 		if ((error = bwrite(bp)) != 0) {
180 			ext2_blkfree(ip, nb, fs->e2fs_bsize);
181 			return (error);
182 		}
183 		ip->i_ib[indirs[0].in_off] = newb;
184 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
185 	}
186 	/*
187 	 * Fetch through the indirect blocks, allocating as necessary.
188 	 */
189 	for (i = 1;;) {
190 		error = bread(vp,
191 		    indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
192 		if (error) {
193 			brelse(bp);
194 			return (error);
195 		}
196 		bap = (int32_t *)bp->b_data;
197 		nb = bap[indirs[i].in_off];
198 		if (i == num)
199 			break;
200 		i += 1;
201 		if (nb != 0) {
202 			bqrelse(bp);
203 			continue;
204 		}
205 		EXT2_LOCK(ump);
206 		if (pref == 0)
207 			pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
208 						bp->b_lblkno);
209 		error =  ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb);
210 		if (error) {
211 			brelse(bp);
212 			return (error);
213 		}
214 		nb = newb;
215 		nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0);
216 		nbp->b_blkno = fsbtodb(fs, nb);
217 		vfs_bio_clrbuf(nbp);
218 		/*
219 		 * Write synchronously so that indirect blocks
220 		 * never point at garbage.
221 		 */
222 		if ((error = bwrite(nbp)) != 0) {
223 			ext2_blkfree(ip, nb, fs->e2fs_bsize);
224 			EXT2_UNLOCK(ump);
225 			brelse(bp);
226 			return (error);
227 		}
228 		bap[indirs[i - 1].in_off] = nb;
229 		/*
230 		 * If required, write synchronously, otherwise use
231 		 * delayed write.
232 		 */
233 		if (flags & IO_SYNC) {
234 			bwrite(bp);
235 		} else {
236 			if (bp->b_bufsize == fs->e2fs_bsize)
237 				bp->b_flags |= B_CLUSTEROK;
238 			bdwrite(bp);
239 		}
240 	}
241 	/*
242 	 * Get the data block, allocating if necessary.
243 	 */
244 	if (nb == 0) {
245 		EXT2_LOCK(ump);
246 		pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
247 				bp->b_lblkno);
248 		if ((error = ext2_alloc(ip,
249 		    lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) {
250 			brelse(bp);
251 			return (error);
252 		}
253 		nb = newb;
254 		nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
255 		nbp->b_blkno = fsbtodb(fs, nb);
256 		if (flags & BA_CLRBUF)
257 			vfs_bio_clrbuf(nbp);
258 		bap[indirs[i].in_off] = nb;
259 		/*
260 		 * If required, write synchronously, otherwise use
261 		 * delayed write.
262 		 */
263 		if (flags & IO_SYNC) {
264 			bwrite(bp);
265 		} else {
266 		if (bp->b_bufsize == fs->e2fs_bsize)
267 				bp->b_flags |= B_CLUSTEROK;
268 			bdwrite(bp);
269 		}
270 		*bpp = nbp;
271 		return (0);
272 	}
273 	brelse(bp);
274 	if (flags & BA_CLRBUF) {
275 		int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
276 		if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
277 			error = cluster_read(vp, ip->i_size, lbn,
278 			    (int)fs->e2fs_bsize, NOCRED,
279 			    MAXBSIZE, seqcount, 0, &nbp);
280 		} else {
281 			error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
282 		}
283 		if (error) {
284 			brelse(nbp);
285 			return (error);
286 		}
287 	} else {
288 		nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
289 		nbp->b_blkno = fsbtodb(fs, nb);
290 	}
291 	*bpp = nbp;
292 	return (0);
293 }
294 
295