ffs_balloc.c revision 1.8 1 /* $NetBSD: ffs_balloc.c,v 1.8 2002/01/08 06:00:14 lukem 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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef __lint
41 __RCSID("$NetBSD: ffs_balloc.c,v 1.8 2002/01/08 06:00:14 lukem Exp $");
42 #endif /* !__lint */
43
44 #include <sys/param.h>
45 #include <sys/time.h>
46
47 #include <assert.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "makefs.h"
55
56 #include <ufs/ufs/dinode.h>
57 #include <ufs/ufs/ufs_bswap.h>
58 #include <ufs/ffs/fs.h>
59
60 #include "ffs/buf.h"
61 #include "ffs/ufs_inode.h"
62 #include "ffs/ffs_extern.h"
63
64 /*
65 * Balloc defines the structure of file system storage
66 * by allocating the physical blocks on a device given
67 * the inode and the logical block number in a file.
68 *
69 * Assume: flags == B_SYNC | B_CLRBUF
70 */
71 int
72 ffs_balloc(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
73 {
74 ufs_daddr_t lbn;
75 int size;
76 ufs_daddr_t nb;
77 struct buf *bp, *nbp;
78 struct fs *fs = ip->i_fs;
79 struct indir indirs[NIADDR + 2];
80 ufs_daddr_t newb, *bap, pref;
81 int osize, nsize, num, i, error;
82 ufs_daddr_t *allocib, *allocblk, allociblk[NIADDR + 1];
83 const int needswap = UFS_FSNEEDSWAP(fs);
84
85 lbn = lblkno(fs, offset);
86 size = blkoff(fs, offset) + bufsize;
87 if (bpp != NULL) {
88 *bpp = NULL;
89 }
90
91 assert(size <= fs->fs_bsize);
92 if (lbn < 0)
93 return (EFBIG);
94
95 /*
96 * If the next write will extend the file into a new block,
97 * and the file is currently composed of a fragment
98 * this fragment has to be extended to be a full block.
99 */
100
101 nb = lblkno(fs, ip->i_ffs_size);
102 if (nb < NDADDR && nb < lbn) {
103 osize = blksize(fs, ip, nb);
104 if (osize < fs->fs_bsize && osize > 0) {
105 warnx("need to ffs_realloccg; not supported!");
106 abort();
107 }
108 }
109
110 /*
111 * The first NDADDR blocks are direct blocks
112 */
113
114 if (lbn < NDADDR) {
115 nb = ufs_rw32(ip->i_ffs_db[lbn], needswap);
116 if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
117
118 /*
119 * The block is an already-allocated direct block
120 * and the file already extends past this block,
121 * thus this must be a whole block.
122 * Just read the block (if requested).
123 */
124
125 if (bpp != NULL) {
126 error = bread(ip->i_fd, ip->i_fs, lbn,
127 fs->fs_bsize, bpp);
128 if (error) {
129 brelse(*bpp);
130 return (error);
131 }
132 }
133 return (0);
134 }
135 if (nb != 0) {
136
137 /*
138 * Consider need to reallocate a fragment.
139 */
140
141 osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
142 nsize = fragroundup(fs, size);
143 if (nsize <= osize) {
144
145 /*
146 * The existing block is already
147 * at least as big as we want.
148 * Just read the block (if requested).
149 */
150
151 if (bpp != NULL) {
152 error = bread(ip->i_fd, ip->i_fs, lbn,
153 osize, bpp);
154 if (error) {
155 brelse(*bpp);
156 return (error);
157 }
158 }
159 return 0;
160 } else {
161 warnx("need to ffs_realloccg; not supported!");
162 abort();
163 }
164 } else {
165
166 /*
167 * the block was not previously allocated,
168 * allocate a new block or fragment.
169 */
170
171 if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
172 nsize = fragroundup(fs, size);
173 else
174 nsize = fs->fs_bsize;
175 error = ffs_alloc(ip, lbn,
176 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
177 nsize, &newb);
178 if (error)
179 return (error);
180 if (bpp != NULL) {
181 bp = getblk(ip->i_fd, ip->i_fs, lbn, nsize);
182 bp->b_blkno = fsbtodb(fs, newb);
183 clrbuf(bp);
184 *bpp = bp;
185 }
186 }
187 ip->i_ffs_db[lbn] = ufs_rw32(newb, needswap);
188 return (0);
189 }
190 /*
191 * Determine the number of levels of indirection.
192 */
193 pref = 0;
194 if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
195 return(error);
196
197 if (num < 1) {
198 warnx("ffs_balloc: ufs_getlbns returned indirect block");
199 abort();
200 }
201 /*
202 * Fetch the first indirect block allocating if necessary.
203 */
204 --num;
205 nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off], needswap);
206 allocib = NULL;
207 allocblk = allociblk;
208 if (nb == 0) {
209 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
210 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
211 if (error)
212 return (error);
213 nb = newb;
214 *allocblk++ = nb;
215 bp = getblk(ip->i_fd, ip->i_fs, indirs[1].in_lbn, fs->fs_bsize);
216 bp->b_blkno = fsbtodb(fs, nb);
217 clrbuf(bp);
218 /*
219 * Write synchronously so that indirect blocks
220 * never point at garbage.
221 */
222 if ((error = bwrite(bp)) != 0)
223 goto fail;
224 allocib = &ip->i_ffs_ib[indirs[0].in_off];
225 *allocib = ufs_rw32(nb, needswap);
226 }
227 /*
228 * Fetch through the indirect blocks, allocating as necessary.
229 */
230 for (i = 1;;) {
231 error = bread(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
232 (int)fs->fs_bsize, &bp);
233 if (error) {
234 brelse(bp);
235 goto fail;
236 }
237 bap = (ufs_daddr_t *)bp->b_data;
238 nb = ufs_rw32(bap[indirs[i].in_off], needswap);
239 if (i == num)
240 break;
241 i++;
242 if (nb != 0) {
243 brelse(bp);
244 continue;
245 }
246 if (pref == 0)
247 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
248 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
249 if (error) {
250 brelse(bp);
251 goto fail;
252 }
253 nb = newb;
254 *allocblk++ = nb;
255 nbp = getblk(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
256 fs->fs_bsize);
257 nbp->b_blkno = fsbtodb(fs, nb);
258 clrbuf(nbp);
259 /*
260 * Write synchronously so that indirect blocks
261 * never point at garbage.
262 */
263 if ((error = bwrite(nbp)) != 0) {
264 brelse(bp);
265 goto fail;
266 }
267 bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
268 /*
269 * If required, write synchronously, otherwise use
270 * delayed write.
271 */
272 bwrite(bp);
273 }
274 /*
275 * Get the data block, allocating if necessary.
276 */
277 if (nb == 0) {
278 pref = ffs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
279 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
280 if (error) {
281 brelse(bp);
282 goto fail;
283 }
284 nb = newb;
285 *allocblk++ = nb;
286 if (bpp != NULL) {
287 nbp = getblk(ip->i_fd, ip->i_fs, lbn, fs->fs_bsize);
288 nbp->b_blkno = fsbtodb(fs, nb);
289 clrbuf(nbp);
290 *bpp = nbp;
291 }
292 bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
293 /*
294 * If required, write synchronously, otherwise use
295 * delayed write.
296 */
297 bwrite(bp);
298 return (0);
299 }
300 brelse(bp);
301 if (bpp != NULL) {
302 error = bread(ip->i_fd, ip->i_fs, lbn,
303 (int)fs->fs_bsize, &nbp);
304 if (error) {
305 brelse(nbp);
306 goto fail;
307 }
308 *bpp = nbp;
309 }
310 return (0);
311 fail:
312 return (error);
313 }
314