ffs_balloc.c revision 1.13.2.1 1 /* $NetBSD: ffs_balloc.c,v 1.13.2.1 1998/11/09 06:06:35 chs 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
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.8 (Berkeley) 6/16/95
36 */
37
38 #if defined(_KERNEL) && !defined(_LKM)
39 #include "opt_quota.h"
40 #include "opt_uvm.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/proc.h>
47 #include <sys/file.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50
51 #include <vm/vm.h>
52
53 #if defined(UVM)
54 #include <uvm/uvm.h>
55 #endif
56
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/inode.h>
60 #include <ufs/ufs/ufs_extern.h>
61 #include <ufs/ufs/ufs_bswap.h>
62
63 #include <ufs/ffs/fs.h>
64 #include <ufs/ffs/ffs_extern.h>
65
66 /*
67 * Balloc defines the structure of file system storage
68 * by allocating the physical blocks on a device given
69 * the inode and the logical block number in a file.
70 */
71 int
72 ffs_balloc(ip, lbn, size, cred, bpp, flags)
73 register struct inode *ip;
74 register ufs_daddr_t lbn;
75 int size;
76 struct ucred *cred;
77 struct buf **bpp;
78 int flags;
79 {
80 register struct fs *fs;
81 register ufs_daddr_t nb;
82 struct buf *bp, *nbp;
83 struct vnode *vp = ITOV(ip);
84 struct indir indirs[NIADDR + 2];
85 ufs_daddr_t newb, *bap, pref;
86 int deallocated, osize, nsize, num, i, error;
87 ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
88
89 if (bpp != NULL) {
90 *bpp = NULL;
91 }
92
93 if (lbn < 0)
94 return (EFBIG);
95 fs = ip->i_fs;
96
97 /*
98 * If the file currently ends with a fragment and
99 * the block we're allocating now is after the current EOF,
100 * this fragment has to be extended to be a full block.
101 */
102 nb = lblkno(fs, ip->i_ffs_size);
103 if (nb < NDADDR && nb < lbn) {
104 osize = blksize(fs, ip, nb);
105 if (osize < fs->fs_bsize && osize > 0) {
106 error = ffs_realloccg(ip, nb,
107 ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
108 osize, (int)fs->fs_bsize, cred, bpp, &newb);
109 if (error)
110 return (error);
111 ip->i_ffs_size = lblktosize(fs, nb + 1);
112 #if defined(UVM)
113 uvm_vnp_setsize(vp, ip->i_ffs_size);
114 #else
115 vnode_pager_setsize(vp, ip->i_ffs_size);
116 #endif
117 ip->i_ffs_db[nb] = ufs_rw32(newb,
118 UFS_MPNEEDSWAP(vp->v_mount));
119 ip->i_flag |= IN_CHANGE | IN_UPDATE;
120
121 if (bpp) {
122 if (flags & B_SYNC)
123 bwrite(*bpp);
124 else
125 bawrite(*bpp);
126 }
127 else {
128 /*
129 * XXX the data in the frag might be
130 * moving to a new disk location.
131 * we need to flush pages to the
132 * new disk locations.
133 * XXX we could do this in realloccg
134 * except for the sync flag.
135 */
136 (vp->v_uvm.u_obj.pgops->pgo_flush)
137 (&vp->v_uvm.u_obj, lblktosize(fs, nb),
138 lblktosize(fs, nb + 1),
139 flags & B_SYNC ? PGO_SYNCIO : 0);
140 }
141 }
142 }
143 /*
144 * The first NDADDR blocks are direct blocks
145 */
146 if (lbn < NDADDR) {
147
148 nb = ufs_rw32(ip->i_ffs_db[lbn], UFS_MPNEEDSWAP(vp->v_mount));
149 if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
150
151 /*
152 * the block is an already-allocated direct block
153 * and the file already extends past this block,
154 * thus this must be a whole block.
155 * just read the block (if requested).
156 */
157
158 justread:
159 if (bpp != NULL) {
160 error = bread(vp, lbn, fs->fs_bsize, NOCRED,
161 &bp);
162 if (error) {
163 brelse(bp);
164 return (error);
165 }
166 *bpp = bp;
167 }
168 return (0);
169 }
170 if (nb != 0) {
171 /*
172 * Consider need to reallocate a fragment.
173 */
174 osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
175 nsize = fragroundup(fs, size);
176 if (nsize <= osize) {
177
178 /*
179 * the existing block is already
180 * at least as big as we want.
181 * just read the block (if requested).
182 */
183
184 goto justread;
185 } else {
186
187 /*
188 * the existing block is smaller than we want,
189 * grow it.
190 */
191
192 error = ffs_realloccg(ip, lbn,
193 ffs_blkpref(ip, lbn, (int)lbn,
194 &ip->i_ffs_db[0]), osize, nsize, cred,
195 bpp, &newb);
196 if (error)
197 return (error);
198 ip->i_ffs_db[lbn] = ufs_rw32(newb,
199 UFS_MPNEEDSWAP(vp->v_mount));
200 ip->i_flag |= IN_CHANGE | IN_UPDATE;
201 }
202 } else {
203
204 /*
205 * the block was not previously allocated,
206 * allocate a new block or fragment.
207 */
208
209 if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
210 nsize = fragroundup(fs, size);
211 else
212 nsize = fs->fs_bsize;
213 error = ffs_alloc(ip, lbn,
214 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
215 nsize, cred, &newb);
216 if (error)
217 return (error);
218
219 ip->i_ffs_db[lbn] = ufs_rw32(newb,
220 UFS_MPNEEDSWAP(vp->v_mount));
221 ip->i_flag |= IN_CHANGE | IN_UPDATE;
222
223 if (bpp != NULL) {
224 bp = getblk(vp, lbn, nsize, 0, 0);
225 bp->b_blkno = fsbtodb(fs, newb);
226 if (flags & B_CLRBUF)
227 clrbuf(bp);
228 *bpp = bp;
229 }
230 }
231 return (0);
232 }
233 /*
234 * Determine the number of levels of indirection.
235 */
236 pref = 0;
237 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
238 return(error);
239 #ifdef DIAGNOSTIC
240 if (num < 1)
241 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
242 #endif
243 /*
244 * Fetch the first indirect block allocating if necessary.
245 */
246 --num;
247 nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off],
248 UFS_MPNEEDSWAP(vp->v_mount));
249 allocib = NULL;
250 allocblk = allociblk;
251 if (nb == 0) {
252 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
253 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
254 cred, &newb);
255 if (error)
256 return (error);
257 nb = newb;
258 *allocblk++ = nb;
259 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
260 bp->b_blkno = fsbtodb(fs, nb);
261 clrbuf(bp);
262 /*
263 * Write synchronously so that indirect blocks
264 * never point at garbage.
265 */
266 if ((error = bwrite(bp)) != 0)
267 goto fail;
268 allocib = &ip->i_ffs_ib[indirs[0].in_off];
269 *allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount));
270 ip->i_flag |= IN_CHANGE | IN_UPDATE;
271 }
272 /*
273 * Fetch through the indirect blocks, allocating as necessary.
274 */
275 for (i = 1;;) {
276 error = bread(vp,
277 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
278 if (error) {
279 brelse(bp);
280 goto fail;
281 }
282 bap = (ufs_daddr_t *)bp->b_data;
283 nb = ufs_rw32(bap[indirs[i].in_off],
284 UFS_MPNEEDSWAP(vp->v_mount));
285 if (i == num)
286 break;
287 i += 1;
288 if (nb != 0) {
289 brelse(bp);
290 continue;
291 }
292 if (pref == 0)
293 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
294 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
295 &newb);
296 if (error) {
297 brelse(bp);
298 goto fail;
299 }
300 nb = newb;
301 *allocblk++ = nb;
302 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
303 nbp->b_blkno = fsbtodb(fs, nb);
304 clrbuf(nbp);
305 /*
306 * Write synchronously so that indirect blocks
307 * never point at garbage.
308 */
309 if ((error = bwrite(nbp)) != 0) {
310 brelse(bp);
311 goto fail;
312 }
313 bap[indirs[i - 1].in_off] = ufs_rw32(nb,
314 UFS_MPNEEDSWAP(vp->v_mount));
315 /*
316 * If required, write synchronously, otherwise use
317 * delayed write.
318 */
319 if (flags & B_SYNC) {
320 bwrite(bp);
321 } else {
322 bdwrite(bp);
323 }
324 }
325 /*
326 * Get the data block, allocating if necessary.
327 */
328 if (nb == 0) {
329 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
330 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
331 &newb);
332 if (error) {
333 brelse(bp);
334 goto fail;
335 }
336 nb = newb;
337 *allocblk++ = nb;
338 bap[indirs[i].in_off] = ufs_rw32(nb,
339 UFS_MPNEEDSWAP(vp->v_mount));
340 /*
341 * If required, write synchronously, otherwise use
342 * delayed write.
343 */
344 if (flags & B_SYNC) {
345 bwrite(bp);
346 } else {
347 bdwrite(bp);
348 }
349 if (bpp != NULL) {
350 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
351 nbp->b_blkno = fsbtodb(fs, nb);
352 if (flags & B_CLRBUF)
353 clrbuf(nbp);
354 *bpp = nbp;
355 }
356 return (0);
357 }
358
359 brelse(bp);
360
361 if (bpp != NULL) {
362 if (flags & B_CLRBUF) {
363 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
364 if (error) {
365 brelse(nbp);
366 goto fail;
367 }
368 } else {
369 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
370 nbp->b_blkno = fsbtodb(fs, nb);
371 clrbuf(nbp);
372 }
373 *bpp = nbp;
374 }
375
376 return (0);
377 fail:
378 /*
379 * If we have failed part way through block allocation, we
380 * have to deallocate any indirect blocks that we have allocated.
381 */
382 for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
383 ffs_blkfree(ip, *blkp, fs->fs_bsize);
384 deallocated += fs->fs_bsize;
385 }
386 if (allocib != NULL)
387 *allocib = 0;
388 if (deallocated) {
389 #ifdef QUOTA
390 /*
391 * Restore user's disk quota because allocation failed.
392 */
393 (void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
394 #endif
395 ip->i_ffs_blocks -= btodb(deallocated);
396 ip->i_flag |= IN_CHANGE | IN_UPDATE;
397 }
398 return (error);
399 }
400