ffs_balloc.c revision 1.21 1 /* $NetBSD: ffs_balloc.c,v 1.21 2000/06/28 14:16:40 mrg 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 #endif
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/buf.h>
45 #include <sys/proc.h>
46 #include <sys/file.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/ufsmount.h>
53 #include <ufs/ufs/inode.h>
54 #include <ufs/ufs/ufs_extern.h>
55 #include <ufs/ufs/ufs_bswap.h>
56
57 #include <ufs/ffs/fs.h>
58 #include <ufs/ffs/ffs_extern.h>
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 int
66 ffs_balloc(v)
67 void *v;
68 {
69 struct vop_balloc_args /* {
70 struct vnode *a_vp;
71 off_t a_startpoint;
72 int a_size;
73 struct ucred *a_cred;
74 int a_flags;
75 struct buf *a_bpp;
76 } */ *ap = v;
77 ufs_daddr_t lbn;
78 int size;
79 struct ucred *cred;
80 int flags;
81 ufs_daddr_t nb;
82 struct buf *bp, *nbp;
83 struct vnode *vp = ap->a_vp;
84 struct inode *ip = VTOI(vp);
85 struct fs *fs = ip->i_fs;
86 struct indir indirs[NIADDR + 2];
87 ufs_daddr_t newb, *bap, pref;
88 int deallocated, osize, nsize, num, i, error;
89 ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
90 int unwindidx = -1;
91 #ifdef FFS_EI
92 const int needswap = UFS_FSNEEDSWAP(fs);
93 #endif
94
95 lbn = lblkno(fs, ap->a_startoffset);
96 size = blkoff(fs, ap->a_startoffset) + ap->a_size;
97 if (size > fs->fs_bsize)
98 panic("ffs_balloc: blk too big");
99 *ap->a_bpp = NULL;
100 if (lbn < 0)
101 return (EFBIG);
102 cred = ap->a_cred;
103 flags = ap->a_flags;
104
105 /*
106 * If the next write will extend the file into a new block,
107 * and the file is currently composed of a fragment
108 * this fragment has to be extended to be a full block.
109 */
110 nb = lblkno(fs, ip->i_ffs_size);
111 if (nb < NDADDR && nb < lbn) {
112 osize = blksize(fs, ip, nb);
113 if (osize < fs->fs_bsize && osize > 0) {
114 error = ffs_realloccg(ip, nb,
115 ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
116 osize, (int)fs->fs_bsize, cred, &bp);
117 if (error)
118 return (error);
119 if (DOINGSOFTDEP(vp))
120 softdep_setup_allocdirect(ip, nb,
121 dbtofsb(fs, bp->b_blkno),
122 ufs_rw32(ip->i_ffs_db[nb], needswap),
123 fs->fs_bsize, osize, bp);
124 ip->i_ffs_size = (nb + 1) * fs->fs_bsize;
125 uvm_vnp_setsize(vp, ip->i_ffs_size);
126 ip->i_ffs_db[nb] = ufs_rw32(dbtofsb(fs, bp->b_blkno),
127 needswap);
128 ip->i_flag |= IN_CHANGE | IN_UPDATE;
129 if (flags & B_SYNC)
130 bwrite(bp);
131 else
132 bawrite(bp);
133 }
134 }
135 /*
136 * The first NDADDR blocks are direct blocks
137 */
138 if (lbn < NDADDR) {
139 nb = ufs_rw32(ip->i_ffs_db[lbn], needswap);
140 if (nb != 0 && ip->i_ffs_size >= (lbn + 1) * fs->fs_bsize) {
141 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
142 if (error) {
143 brelse(bp);
144 return (error);
145 }
146 *ap->a_bpp = bp;
147 return (0);
148 }
149 if (nb != 0) {
150 /*
151 * Consider need to reallocate a fragment.
152 */
153 osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
154 nsize = fragroundup(fs, size);
155 if (nsize <= osize) {
156 error = bread(vp, lbn, osize, NOCRED, &bp);
157 if (error) {
158 brelse(bp);
159 return (error);
160 }
161 } else {
162 error = ffs_realloccg(ip, lbn,
163 ffs_blkpref(ip, lbn, (int)lbn,
164 &ip->i_ffs_db[0]), osize, nsize, cred,
165 &bp);
166 if (error)
167 return (error);
168 if (DOINGSOFTDEP(vp))
169 softdep_setup_allocdirect(ip, lbn,
170 dbtofsb(fs, bp->b_blkno), nb,
171 nsize, osize, bp);
172 }
173 } else {
174 if (ip->i_ffs_size < (lbn + 1) * fs->fs_bsize)
175 nsize = fragroundup(fs, size);
176 else
177 nsize = fs->fs_bsize;
178 error = ffs_alloc(ip, lbn,
179 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
180 nsize, cred, &newb);
181 if (error)
182 return (error);
183 bp = getblk(vp, lbn, nsize, 0, 0);
184 bp->b_blkno = fsbtodb(fs, newb);
185 if (flags & B_CLRBUF)
186 clrbuf(bp);
187 if (DOINGSOFTDEP(vp))
188 softdep_setup_allocdirect(ip, lbn, newb, 0,
189 nsize, 0, bp);
190 }
191 ip->i_ffs_db[lbn] = ufs_rw32(dbtofsb(fs, bp->b_blkno),
192 needswap);
193 ip->i_flag |= IN_CHANGE | IN_UPDATE;
194 *ap->a_bpp = bp;
195 return (0);
196 }
197 /*
198 * Determine the number of levels of indirection.
199 */
200 pref = 0;
201 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
202 return(error);
203 #ifdef DIAGNOSTIC
204 if (num < 1)
205 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
206 #endif
207 /*
208 * Fetch the first indirect block allocating if necessary.
209 */
210 --num;
211 nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off], needswap);
212 allocib = NULL;
213 allocblk = allociblk;
214 if (nb == 0) {
215 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
216 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
217 &newb);
218 if (error)
219 return (error);
220 nb = newb;
221 *allocblk++ = nb;
222 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
223 bp->b_blkno = fsbtodb(fs, nb);
224 clrbuf(bp);
225 if (DOINGSOFTDEP(vp)) {
226 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
227 newb, 0, fs->fs_bsize, 0, bp);
228 bdwrite(bp);
229 } else {
230 /*
231 * Write synchronously so that indirect blocks
232 * never point at garbage.
233 */
234 if ((error = bwrite(bp)) != 0)
235 goto fail;
236 }
237 unwindidx = 0;
238 allocib = &ip->i_ffs_ib[indirs[0].in_off];
239 *allocib = ufs_rw32(nb, needswap);
240 ip->i_flag |= IN_CHANGE | IN_UPDATE;
241 }
242 /*
243 * Fetch through the indirect blocks, allocating as necessary.
244 */
245 for (i = 1;;) {
246 error = bread(vp,
247 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
248 if (error) {
249 brelse(bp);
250 goto fail;
251 }
252 bap = (ufs_daddr_t *)bp->b_data;
253 nb = ufs_rw32(bap[indirs[i].in_off], needswap);
254 if (i == num)
255 break;
256 i++;
257 if (nb != 0) {
258 brelse(bp);
259 continue;
260 }
261 if (pref == 0)
262 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
263 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
264 &newb);
265 if (error) {
266 brelse(bp);
267 goto fail;
268 }
269 nb = newb;
270 *allocblk++ = nb;
271 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
272 nbp->b_blkno = fsbtodb(fs, nb);
273 clrbuf(nbp);
274 if (DOINGSOFTDEP(vp)) {
275 softdep_setup_allocindir_meta(nbp, ip, bp,
276 indirs[i - 1].in_off, nb);
277 bdwrite(nbp);
278 } else {
279 /*
280 * Write synchronously so that indirect blocks
281 * never point at garbage.
282 */
283 if ((error = bwrite(nbp)) != 0) {
284 brelse(bp);
285 goto fail;
286 }
287 }
288 if (unwindidx < 0)
289 unwindidx = i - 1;
290 bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
291 /*
292 * If required, write synchronously, otherwise use
293 * delayed write.
294 */
295 if (flags & B_SYNC) {
296 bwrite(bp);
297 } else {
298 bdwrite(bp);
299 }
300 }
301 /*
302 * Get the data block, allocating if necessary.
303 */
304 if (nb == 0) {
305 pref = ffs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
306 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
307 &newb);
308 if (error) {
309 brelse(bp);
310 goto fail;
311 }
312 nb = newb;
313 *allocblk++ = nb;
314 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
315 nbp->b_blkno = fsbtodb(fs, nb);
316 if (flags & B_CLRBUF)
317 clrbuf(nbp);
318 if (DOINGSOFTDEP(vp))
319 softdep_setup_allocindir_page(ip, lbn, bp,
320 indirs[num].in_off, nb, 0, nbp);
321 bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
322 /*
323 * If required, write synchronously, otherwise use
324 * delayed write.
325 */
326 if (flags & B_SYNC) {
327 bwrite(bp);
328 } else {
329 bdwrite(bp);
330 }
331 *ap->a_bpp = nbp;
332 return (0);
333 }
334 brelse(bp);
335 if (flags & B_CLRBUF) {
336 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
337 if (error) {
338 brelse(nbp);
339 goto fail;
340 }
341 } else {
342 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
343 nbp->b_blkno = fsbtodb(fs, nb);
344 }
345 *ap->a_bpp = nbp;
346 return (0);
347 fail:
348 /*
349 * If we have failed part way through block allocation, we
350 * have to deallocate any indirect blocks that we have allocated.
351 * We have to fsync the file before we start to get rid of all
352 * of its dependencies so that we do not leave them dangling.
353 * We have to sync it at the end so that the soft updates code
354 * does not find any untracked changes. Although this is really
355 * slow, running out of disk space is not expected to be a common
356 * occurence. The error return from fsync is ignored as we already
357 * have an error to return to the user.
358 */
359 (void) VOP_FSYNC(vp, cred, FSYNC_WAIT, curproc);
360 for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
361 ffs_blkfree(ip, *blkp, fs->fs_bsize);
362 deallocated += fs->fs_bsize;
363 }
364 if (unwindidx >= 0) {
365 if (unwindidx == 0) {
366 *allocib = 0;
367 } else {
368 int r;
369
370 r = bread(vp, indirs[unwindidx].in_lbn,
371 (int)fs->fs_bsize, NOCRED, &bp);
372 if (r) {
373 panic("Could not unwind indirect block, error %d", r);
374 brelse(bp);
375 } else {
376 bap = (ufs_daddr_t *)bp->b_data;
377 bap[indirs[unwindidx].in_off] = 0;
378 if (flags & B_SYNC)
379 bwrite(bp);
380 else
381 bdwrite(bp);
382 }
383 }
384 for (i = unwindidx + 1; i <= num; i++) {
385 bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
386 0);
387 bp->b_flags |= B_INVAL;
388 brelse(bp);
389 }
390 }
391 if (deallocated) {
392 #ifdef QUOTA
393 /*
394 * Restore user's disk quota because allocation failed.
395 */
396 (void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
397 #endif
398 ip->i_ffs_blocks -= btodb(deallocated);
399 ip->i_flag |= IN_CHANGE | IN_UPDATE;
400 }
401 (void) VOP_FSYNC(vp, cred, FSYNC_WAIT, curproc);
402 return (error);
403 }
404