xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_balloc.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: ext2fs_balloc.c,v 1.28 2005/12/11 12:25:25 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
32  * Modified for ext2fs by Manuel Bouyer.
33  */
34 
35 /*
36  * Copyright (c) 1997 Manuel Bouyer.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by Manuel Bouyer.
49  * 4. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *	@(#)ffs_balloc.c	8.4 (Berkeley) 9/23/93
64  * Modified for ext2fs by Manuel Bouyer.
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_balloc.c,v 1.28 2005/12/11 12:25:25 christos Exp $");
69 
70 #if defined(_KERNEL_OPT)
71 #include "opt_uvmhist.h"
72 #endif
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/buf.h>
77 #include <sys/proc.h>
78 #include <sys/file.h>
79 #include <sys/vnode.h>
80 #include <sys/mount.h>
81 
82 #include <uvm/uvm.h>
83 
84 #include <ufs/ufs/inode.h>
85 #include <ufs/ufs/ufs_extern.h>
86 
87 #include <ufs/ext2fs/ext2fs.h>
88 #include <ufs/ext2fs/ext2fs_extern.h>
89 
90 /*
91  * Balloc defines the structure of file system storage
92  * by allocating the physical blocks on a device given
93  * the inode and the logical block number in a file.
94  */
95 int
96 ext2fs_balloc(struct inode *ip, daddr_t bn, int size, struct ucred *cred,
97 		struct buf **bpp, int flags)
98 {
99 	struct m_ext2fs *fs;
100 	daddr_t nb;
101 	struct buf *bp, *nbp;
102 	struct vnode *vp = ITOV(ip);
103 	struct indir indirs[NIADDR + 2];
104 	daddr_t newb, lbn, pref;
105 	int32_t *bap;	/* XXX ondisk32 */
106 	int num, i, error;
107 	u_int deallocated;
108 	daddr_t *blkp, *allocblk, allociblk[NIADDR + 1];
109 	int32_t *allocib;	/* XXX ondisk32 */
110 	int unwindidx = -1;
111 	UVMHIST_FUNC("ext2fs_balloc"); UVMHIST_CALLED(ubchist);
112 
113 	UVMHIST_LOG(ubchist, "bn 0x%x", bn,0,0,0);
114 
115 	if (bpp != NULL) {
116 		*bpp = NULL;
117 	}
118 	if (bn < 0)
119 		return (EFBIG);
120 	fs = ip->i_e2fs;
121 	lbn = bn;
122 
123 	/*
124 	 * The first NDADDR blocks are direct blocks
125 	 */
126 	if (bn < NDADDR) {
127 		/* XXX ondisk32 */
128 		nb = fs2h32(ip->i_e2fs_blocks[bn]);
129 		if (nb != 0) {
130 
131 			/*
132 			 * the block is already allocated, just read it.
133 			 */
134 
135 			if (bpp != NULL) {
136 				error = bread(vp, bn, fs->e2fs_bsize, NOCRED,
137 					      &bp);
138 				if (error) {
139 					brelse(bp);
140 					return (error);
141 				}
142 				*bpp = bp;
143 			}
144 			return (0);
145 		}
146 
147 		/*
148 		 * allocate a new direct block.
149 		 */
150 
151 		error = ext2fs_alloc(ip, bn,
152 		    ext2fs_blkpref(ip, bn, bn, &ip->i_e2fs_blocks[0]),
153 		    cred, &newb);
154 		if (error)
155 			return (error);
156 		ip->i_e2fs_last_lblk = lbn;
157 		ip->i_e2fs_last_blk = newb;
158 		/* XXX ondisk32 */
159 		ip->i_e2fs_blocks[bn] = h2fs32((int32_t)newb);
160 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
161 		if (bpp != NULL) {
162 			bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
163 			bp->b_blkno = fsbtodb(fs, newb);
164 			if (flags & B_CLRBUF)
165 				clrbuf(bp);
166 			*bpp = bp;
167 		}
168 		return (0);
169 	}
170 	/*
171 	 * Determine the number of levels of indirection.
172 	 */
173 	pref = 0;
174 	if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
175 		return(error);
176 #ifdef DIAGNOSTIC
177 	if (num < 1)
178 		panic ("ext2fs_balloc: ufs_getlbns returned indirect block\n");
179 #endif
180 	/*
181 	 * Fetch the first indirect block allocating if necessary.
182 	 */
183 	--num;
184 	/* XXX ondisk32 */
185 	nb = fs2h32(ip->i_e2fs_blocks[NDADDR + indirs[0].in_off]);
186 	allocib = NULL;
187 	allocblk = allociblk;
188 	if (nb == 0) {
189 		pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
190 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
191 		if (error)
192 			return (error);
193 		nb = newb;
194 		*allocblk++ = nb;
195 		ip->i_e2fs_last_blk = newb;
196 		bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0);
197 		bp->b_blkno = fsbtodb(fs, newb);
198 		clrbuf(bp);
199 		/*
200 		 * Write synchronously so that indirect blocks
201 		 * never point at garbage.
202 		 */
203 		if ((error = bwrite(bp)) != 0)
204 			goto fail;
205 		unwindidx = 0;
206 		allocib = &ip->i_e2fs_blocks[NDADDR + indirs[0].in_off];
207 		/* XXX ondisk32 */
208 		*allocib = h2fs32((int32_t)newb);
209 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
210 	}
211 	/*
212 	 * Fetch through the indirect blocks, allocating as necessary.
213 	 */
214 	for (i = 1;;) {
215 		error = bread(vp,
216 		    indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
217 		if (error) {
218 			brelse(bp);
219 			goto fail;
220 		}
221 		bap = (int32_t *)bp->b_data;	/* XXX ondisk32 */
222 		nb = fs2h32(bap[indirs[i].in_off]);
223 		if (i == num)
224 			break;
225 		i++;
226 		if (nb != 0) {
227 			brelse(bp);
228 			continue;
229 		}
230 		pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
231 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
232 		if (error) {
233 			brelse(bp);
234 			goto fail;
235 		}
236 		nb = newb;
237 		*allocblk++ = nb;
238 		ip->i_e2fs_last_blk = newb;
239 		nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0);
240 		nbp->b_blkno = fsbtodb(fs, nb);
241 		clrbuf(nbp);
242 		/*
243 		 * Write synchronously so that indirect blocks
244 		 * never point at garbage.
245 		 */
246 		if ((error = bwrite(nbp)) != 0) {
247 			brelse(bp);
248 			goto fail;
249 		}
250 		if (unwindidx < 0)
251 			unwindidx = i - 1;
252 		/* XXX ondisk32 */
253 		bap[indirs[i - 1].in_off] = h2fs32((int32_t)nb);
254 		/*
255 		 * If required, write synchronously, otherwise use
256 		 * delayed write.
257 		 */
258 		if (flags & B_SYNC) {
259 			bwrite(bp);
260 		} else {
261 			bdwrite(bp);
262 		}
263 	}
264 	/*
265 	 * Get the data block, allocating if necessary.
266 	 */
267 	if (nb == 0) {
268 		pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
269 		error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
270 		if (error) {
271 			brelse(bp);
272 			goto fail;
273 		}
274 		nb = newb;
275 		*allocblk++ = nb;
276 		ip->i_e2fs_last_lblk = lbn;
277 		ip->i_e2fs_last_blk = newb;
278 		/* XXX ondisk32 */
279 		bap[indirs[num].in_off] = h2fs32((int32_t)nb);
280 		/*
281 		 * If required, write synchronously, otherwise use
282 		 * delayed write.
283 		 */
284 		if (flags & B_SYNC) {
285 			bwrite(bp);
286 		} else {
287 			bdwrite(bp);
288 		}
289 		if (bpp != NULL) {
290 			nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
291 			nbp->b_blkno = fsbtodb(fs, nb);
292 			if (flags & B_CLRBUF)
293 				clrbuf(nbp);
294 			*bpp = nbp;
295 		}
296 		return (0);
297 	}
298 	brelse(bp);
299 	if (bpp != NULL) {
300 		if (flags & B_CLRBUF) {
301 			error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED,
302 				      &nbp);
303 			if (error) {
304 				brelse(nbp);
305 				goto fail;
306 			}
307 		} else {
308 			nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
309 			nbp->b_blkno = fsbtodb(fs, nb);
310 		}
311 		*bpp = nbp;
312 	}
313 	return (0);
314 fail:
315 	/*
316 	 * If we have failed part way through block allocation, we
317 	 * have to deallocate any indirect blocks that we have allocated.
318 	 */
319 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
320 		ext2fs_blkfree(ip, *blkp);
321 		deallocated += fs->e2fs_bsize;
322 	}
323 	if (unwindidx >= 0) {
324 		if (unwindidx == 0) {
325 			*allocib = 0;
326 		} else {
327 			int r;
328 
329 			r = bread(vp, indirs[unwindidx].in_lbn,
330 			    (int)fs->e2fs_bsize, NOCRED, &bp);
331 			if (r) {
332 				panic("Could not unwind indirect block, error %d", r);
333 				brelse(bp);
334 			} else {
335 				bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
336 				bap[indirs[unwindidx].in_off] = 0;
337 				if (flags & B_SYNC)
338 					bwrite(bp);
339 				else
340 					bdwrite(bp);
341 			}
342 		}
343 		for (i = unwindidx + 1; i <= num; i++) {
344 			bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
345 			    0, 0);
346 			bp->b_flags |= B_INVAL;
347 			brelse(bp);
348 		}
349 	}
350 	if (deallocated) {
351 		ip->i_e2fs_nblock -= btodb(deallocated);
352 		ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
353 	}
354 	return error;
355 }
356 
357 int
358 ext2fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
359     struct ucred *cred)
360 {
361 	struct inode *ip = VTOI(vp);
362 	struct m_ext2fs *fs = ip->i_e2fs;
363 	int error, delta, bshift, bsize;
364 	UVMHIST_FUNC("ext2fs_gop_alloc"); UVMHIST_CALLED(ubchist);
365 
366 	bshift = fs->e2fs_bshift;
367 	bsize = 1 << bshift;
368 
369 	delta = off & (bsize - 1);
370 	off -= delta;
371 	len += delta;
372 
373 	while (len > 0) {
374 		bsize = min(bsize, len);
375 		UVMHIST_LOG(ubchist, "off 0x%x len 0x%x bsize 0x%x",
376 			    off, len, bsize, 0);
377 
378 		error = ext2fs_balloc(ip, lblkno(fs, off), bsize, cred,
379 		    NULL, flags);
380 		if (error) {
381 			UVMHIST_LOG(ubchist, "error %d", error, 0,0,0);
382 			return error;
383 		}
384 
385 		/*
386 		 * increase file size now, ext2fs_balloc() requires that
387 		 * EOF be up-to-date before each call.
388 		 */
389 
390 		if (ext2fs_size(ip) < off + bsize) {
391 			UVMHIST_LOG(ubchist, "old 0x%lx%8lx new 0x%lx%8lx",
392 			    /* Note that arguments are always cast to u_long. */
393 				    ext2fs_size(ip) >> 32,
394 				    ext2fs_size(ip) & 0xffffffff,
395 				    (off + bsize) >> 32,
396 				    (off + bsize) & 0xffffffff);
397 			error = ext2fs_setsize(ip, off + bsize);
398 			if (error) {
399 				UVMHIST_LOG(ubchist, "error %d", error, 0,0,0);
400 				return error;
401 			}
402 		}
403 
404 		off += bsize;
405 		len -= bsize;
406 	}
407 	return 0;
408 }
409