xref: /freebsd/usr.sbin/makefs/ffs/ffs_balloc.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*	$NetBSD: ffs_balloc.c,v 1.13 2004/06/20 22:20:18 jmc Exp $	*/
2 /* From NetBSD: ffs_balloc.c,v 1.25 2001/08/08 08:36:36 lukem Exp */
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)ffs_balloc.c	8.8 (Berkeley) 6/16/95
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/time.h>
40 
41 #include <assert.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "makefs.h"
48 
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ffs/fs.h>
51 
52 #include "ffs/ufs_bswap.h"
53 #include "ffs/buf.h"
54 #include "ffs/ufs_inode.h"
55 #include "ffs/ffs_extern.h"
56 
57 static int ffs_balloc_ufs1(struct inode *, off_t, int, struct buf **);
58 static int ffs_balloc_ufs2(struct inode *, off_t, int, struct buf **);
59 
60 /*
61  * Balloc defines the structure of file system storage
62  * by allocating the physical blocks on a device given
63  * the inode and the logical block number in a file.
64  *
65  * Assume: flags == B_SYNC | B_CLRBUF
66  */
67 
68 int
69 ffs_balloc(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
70 {
71 	if (ip->i_fs->fs_magic == FS_UFS2_MAGIC)
72 		return ffs_balloc_ufs2(ip, offset, bufsize, bpp);
73 	else
74 		return ffs_balloc_ufs1(ip, offset, bufsize, bpp);
75 }
76 
77 static int
78 ffs_balloc_ufs1(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
79 {
80 	daddr_t lbn, lastlbn;
81 	int size;
82 	int32_t nb;
83 	struct buf *bp, *nbp;
84 	struct fs *fs = ip->i_fs;
85 	struct indir indirs[UFS_NIADDR + 2];
86 	daddr_t newb, pref;
87 	int32_t *bap;
88 	int osize, nsize, num, i, error;
89 	int32_t *allocblk, allociblk[UFS_NIADDR + 1];
90 	int32_t *allocib;
91 	const int needswap = UFS_FSNEEDSWAP(fs);
92 
93 	lbn = lblkno(fs, offset);
94 	size = blkoff(fs, offset) + bufsize;
95 	if (bpp != NULL) {
96 		*bpp = NULL;
97 	}
98 
99 	assert(size <= fs->fs_bsize);
100 	if (lbn < 0)
101 		return (EFBIG);
102 
103 	/*
104 	 * If the next write will extend the file into a new block,
105 	 * and the file is currently composed of a fragment
106 	 * this fragment has to be extended to be a full block.
107 	 */
108 
109 	lastlbn = lblkno(fs, ip->i_ffs1_size);
110 	if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
111 		nb = lastlbn;
112 		osize = blksize(fs, ip, nb);
113 		if (osize < fs->fs_bsize && osize > 0) {
114 			warnx("need to ffs_realloccg; not supported!");
115 			abort();
116 		}
117 	}
118 
119 	/*
120 	 * The first UFS_NDADDR blocks are direct blocks
121 	 */
122 
123 	if (lbn < UFS_NDADDR) {
124 		nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
125 		if (nb != 0 && ip->i_ffs1_size >=
126 		    (uint64_t)lblktosize(fs, lbn + 1)) {
127 
128 			/*
129 			 * The block is an already-allocated direct block
130 			 * and the file already extends past this block,
131 			 * thus this must be a whole block.
132 			 * Just read the block (if requested).
133 			 */
134 
135 			if (bpp != NULL) {
136 				error = bread(ip->i_devvp, lbn, fs->fs_bsize,
137 				    NULL, bpp);
138 				if (error) {
139 					brelse(*bpp, 0);
140 					return (error);
141 				}
142 			}
143 			return (0);
144 		}
145 		if (nb != 0) {
146 
147 			/*
148 			 * Consider need to reallocate a fragment.
149 			 */
150 
151 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs1_size));
152 			nsize = fragroundup(fs, size);
153 			if (nsize <= osize) {
154 
155 				/*
156 				 * The existing block is already
157 				 * at least as big as we want.
158 				 * Just read the block (if requested).
159 				 */
160 
161 				if (bpp != NULL) {
162 					error = bread(ip->i_devvp, lbn, osize,
163 					    NULL, bpp);
164 					if (error) {
165 						brelse(*bpp, 0);
166 						return (error);
167 					}
168 				}
169 				return 0;
170 			} else {
171 				warnx("need to ffs_realloccg; not supported!");
172 				abort();
173 			}
174 		} else {
175 
176 			/*
177 			 * the block was not previously allocated,
178 			 * allocate a new block or fragment.
179 			 */
180 
181 			if (ip->i_ffs1_size < (uint64_t)lblktosize(fs, lbn + 1))
182 				nsize = fragroundup(fs, size);
183 			else
184 				nsize = fs->fs_bsize;
185 			error = ffs_alloc(ip, lbn,
186 			    ffs_blkpref_ufs1(ip, lbn, (int)lbn,
187 				&ip->i_ffs1_db[0]),
188 				nsize, &newb);
189 			if (error)
190 				return (error);
191 			if (bpp != NULL) {
192 				bp = getblk(ip->i_devvp, lbn, nsize, 0, 0, 0);
193 				bp->b_blkno = fsbtodb(fs, newb);
194 				clrbuf(bp);
195 				*bpp = bp;
196 			}
197 		}
198 		ip->i_ffs1_db[lbn] = ufs_rw32((int32_t)newb, needswap);
199 		return (0);
200 	}
201 
202 	/*
203 	 * Determine the number of levels of indirection.
204 	 */
205 
206 	pref = 0;
207 	if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
208 		return (error);
209 
210 	if (num < 1) {
211 		warnx("ffs_balloc: ufs_getlbns returned indirect block");
212 		abort();
213 	}
214 
215 	/*
216 	 * Fetch the first indirect block allocating if necessary.
217 	 */
218 
219 	--num;
220 	nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
221 	allocib = NULL;
222 	allocblk = allociblk;
223 	if (nb == 0) {
224 		pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
225 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
226 		if (error)
227 			return error;
228 		nb = newb;
229 		*allocblk++ = nb;
230 		bp = getblk(ip->i_devvp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, 0);
231 		bp->b_blkno = fsbtodb(fs, nb);
232 		clrbuf(bp);
233 		/*
234 		 * Write synchronously so that indirect blocks
235 		 * never point at garbage.
236 		 */
237 		if ((error = bwrite(bp)) != 0)
238 			return error;
239 		allocib = &ip->i_ffs1_ib[indirs[0].in_off];
240 		*allocib = ufs_rw32((int32_t)nb, needswap);
241 	}
242 
243 	/*
244 	 * Fetch through the indirect blocks, allocating as necessary.
245 	 */
246 
247 	for (i = 1;;) {
248 		error = bread(ip->i_devvp, indirs[i].in_lbn, fs->fs_bsize,
249 		    NULL, &bp);
250 		if (error) {
251 			brelse(bp, 0);
252 			return error;
253 		}
254 		bap = (int32_t *)bp->b_data;
255 		nb = ufs_rw32(bap[indirs[i].in_off], needswap);
256 		if (i == num)
257 			break;
258 		i++;
259 		if (nb != 0) {
260 			brelse(bp, 0);
261 			continue;
262 		}
263 		if (pref == 0)
264 			pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
265 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
266 		if (error) {
267 			brelse(bp, 0);
268 			return error;
269 		}
270 		nb = newb;
271 		*allocblk++ = nb;
272 		nbp = getblk(ip->i_devvp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
273 		nbp->b_blkno = fsbtodb(fs, nb);
274 		clrbuf(nbp);
275 		/*
276 		 * Write synchronously so that indirect blocks
277 		 * never point at garbage.
278 		 */
279 
280 		if ((error = bwrite(nbp)) != 0) {
281 			brelse(bp, 0);
282 			return error;
283 		}
284 		bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
285 
286 		bwrite(bp);
287 	}
288 
289 	/*
290 	 * Get the data block, allocating if necessary.
291 	 */
292 
293 	if (nb == 0) {
294 		pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]);
295 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
296 		if (error) {
297 			brelse(bp, 0);
298 			return error;
299 		}
300 		nb = newb;
301 		*allocblk++ = nb;
302 		if (bpp != NULL) {
303 			nbp = getblk(ip->i_devvp, lbn, fs->fs_bsize, 0, 0, 0);
304 			nbp->b_blkno = fsbtodb(fs, nb);
305 			clrbuf(nbp);
306 			*bpp = nbp;
307 		}
308 		bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
309 
310 		/*
311 		 * If required, write synchronously, otherwise use
312 		 * delayed write.
313 		 */
314 		bwrite(bp);
315 		return (0);
316 	}
317 	brelse(bp, 0);
318 	if (bpp != NULL) {
319 		error = bread(ip->i_devvp, lbn, (int)fs->fs_bsize, NULL, &nbp);
320 		if (error) {
321 			brelse(nbp, 0);
322 			return error;
323 		}
324 		*bpp = nbp;
325 	}
326 	return (0);
327 }
328 
329 static int
330 ffs_balloc_ufs2(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
331 {
332 	daddr_t lbn, lastlbn;
333 	int size;
334 	struct buf *bp, *nbp;
335 	struct fs *fs = ip->i_fs;
336 	struct indir indirs[UFS_NIADDR + 2];
337 	daddr_t newb, pref, nb;
338 	int64_t *bap;
339 	int osize, nsize, num, i, error;
340 	int64_t *allocblk, allociblk[UFS_NIADDR + 1];
341 	int64_t *allocib;
342 	const int needswap = UFS_FSNEEDSWAP(fs);
343 
344 	lbn = lblkno(fs, offset);
345 	size = blkoff(fs, offset) + bufsize;
346 	if (bpp != NULL) {
347 		*bpp = NULL;
348 	}
349 
350 	assert(size <= fs->fs_bsize);
351 	if (lbn < 0)
352 		return (EFBIG);
353 
354 	/*
355 	 * If the next write will extend the file into a new block,
356 	 * and the file is currently composed of a fragment
357 	 * this fragment has to be extended to be a full block.
358 	 */
359 
360 	lastlbn = lblkno(fs, ip->i_ffs2_size);
361 	if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
362 		nb = lastlbn;
363 		osize = blksize(fs, ip, nb);
364 		if (osize < fs->fs_bsize && osize > 0) {
365 			warnx("need to ffs_realloccg; not supported!");
366 			abort();
367 		}
368 	}
369 
370 	/*
371 	 * The first UFS_NDADDR blocks are direct blocks
372 	 */
373 
374 	if (lbn < UFS_NDADDR) {
375 		nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
376 		if (nb != 0 && ip->i_ffs2_size >=
377 		    (uint64_t)lblktosize(fs, lbn + 1)) {
378 
379 			/*
380 			 * The block is an already-allocated direct block
381 			 * and the file already extends past this block,
382 			 * thus this must be a whole block.
383 			 * Just read the block (if requested).
384 			 */
385 
386 			if (bpp != NULL) {
387 				error = bread(ip->i_devvp, lbn, fs->fs_bsize,
388 				    NULL, bpp);
389 				if (error) {
390 					brelse(*bpp, 0);
391 					return (error);
392 				}
393 			}
394 			return (0);
395 		}
396 		if (nb != 0) {
397 
398 			/*
399 			 * Consider need to reallocate a fragment.
400 			 */
401 
402 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs2_size));
403 			nsize = fragroundup(fs, size);
404 			if (nsize <= osize) {
405 
406 				/*
407 				 * The existing block is already
408 				 * at least as big as we want.
409 				 * Just read the block (if requested).
410 				 */
411 
412 				if (bpp != NULL) {
413 					error = bread(ip->i_devvp, lbn, osize,
414 					    NULL, bpp);
415 					if (error) {
416 						brelse(*bpp, 0);
417 						return (error);
418 					}
419 				}
420 				return 0;
421 			} else {
422 				warnx("need to ffs_realloccg; not supported!");
423 				abort();
424 			}
425 		} else {
426 
427 			/*
428 			 * the block was not previously allocated,
429 			 * allocate a new block or fragment.
430 			 */
431 
432 			if (ip->i_ffs2_size < (uint64_t)lblktosize(fs, lbn + 1))
433 				nsize = fragroundup(fs, size);
434 			else
435 				nsize = fs->fs_bsize;
436 			error = ffs_alloc(ip, lbn,
437 			    ffs_blkpref_ufs2(ip, lbn, (int)lbn,
438 				&ip->i_ffs2_db[0]),
439 				nsize, &newb);
440 			if (error)
441 				return (error);
442 			if (bpp != NULL) {
443 				bp = getblk(ip->i_devvp, lbn, nsize, 0, 0, 0);
444 				bp->b_blkno = fsbtodb(fs, newb);
445 				clrbuf(bp);
446 				*bpp = bp;
447 			}
448 		}
449 		ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
450 		return (0);
451 	}
452 
453 	/*
454 	 * Determine the number of levels of indirection.
455 	 */
456 
457 	pref = 0;
458 	if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
459 		return (error);
460 
461 	if (num < 1) {
462 		warnx("ffs_balloc: ufs_getlbns returned indirect block");
463 		abort();
464 	}
465 
466 	/*
467 	 * Fetch the first indirect block allocating if necessary.
468 	 */
469 
470 	--num;
471 	nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
472 	allocib = NULL;
473 	allocblk = allociblk;
474 	if (nb == 0) {
475 		pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
476 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
477 		if (error)
478 			return error;
479 		nb = newb;
480 		*allocblk++ = nb;
481 		bp = getblk(ip->i_devvp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, 0);
482 		bp->b_blkno = fsbtodb(fs, nb);
483 		clrbuf(bp);
484 		/*
485 		 * Write synchronously so that indirect blocks
486 		 * never point at garbage.
487 		 */
488 		if ((error = bwrite(bp)) != 0)
489 			return error;
490 		allocib = &ip->i_ffs2_ib[indirs[0].in_off];
491 		*allocib = ufs_rw64(nb, needswap);
492 	}
493 
494 	/*
495 	 * Fetch through the indirect blocks, allocating as necessary.
496 	 */
497 
498 	for (i = 1;;) {
499 		error = bread(ip->i_devvp, indirs[i].in_lbn, fs->fs_bsize,
500 		    NULL, &bp);
501 		if (error) {
502 			brelse(bp, 0);
503 			return error;
504 		}
505 		bap = (int64_t *)bp->b_data;
506 		nb = ufs_rw64(bap[indirs[i].in_off], needswap);
507 		if (i == num)
508 			break;
509 		i++;
510 		if (nb != 0) {
511 			brelse(bp, 0);
512 			continue;
513 		}
514 		if (pref == 0)
515 			pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
516 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
517 		if (error) {
518 			brelse(bp, 0);
519 			return error;
520 		}
521 		nb = newb;
522 		*allocblk++ = nb;
523 		nbp = getblk(ip->i_devvp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
524 		nbp->b_blkno = fsbtodb(fs, nb);
525 		clrbuf(nbp);
526 		/*
527 		 * Write synchronously so that indirect blocks
528 		 * never point at garbage.
529 		 */
530 
531 		if ((error = bwrite(nbp)) != 0) {
532 			brelse(bp, 0);
533 			return error;
534 		}
535 		bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
536 
537 		bwrite(bp);
538 	}
539 
540 	/*
541 	 * Get the data block, allocating if necessary.
542 	 */
543 
544 	if (nb == 0) {
545 		pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]);
546 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
547 		if (error) {
548 			brelse(bp, 0);
549 			return error;
550 		}
551 		nb = newb;
552 		*allocblk++ = nb;
553 		if (bpp != NULL) {
554 			nbp = getblk(ip->i_devvp, lbn, fs->fs_bsize, 0, 0, 0);
555 			nbp->b_blkno = fsbtodb(fs, nb);
556 			clrbuf(nbp);
557 			*bpp = nbp;
558 		}
559 		bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
560 
561 		/*
562 		 * If required, write synchronously, otherwise use
563 		 * delayed write.
564 		 */
565 		bwrite(bp);
566 		return (0);
567 	}
568 	brelse(bp, 0);
569 	if (bpp != NULL) {
570 		error = bread(ip->i_devvp, lbn, (int)fs->fs_bsize, NULL, &nbp);
571 		if (error) {
572 			brelse(nbp, 0);
573 			return error;
574 		}
575 		*bpp = nbp;
576 	}
577 	return (0);
578 }
579