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