ffs_balloc.c revision 1.14.4.3 1 1.14.4.3 chs /* $NetBSD: ffs_balloc.c,v 1.14.4.3 1999/07/06 14:52:08 chs Exp $ */
2 1.2 cgd
3 1.1 mycroft /*
4 1.1 mycroft * Copyright (c) 1982, 1986, 1989, 1993
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
16 1.1 mycroft * must display the following acknowledgement:
17 1.1 mycroft * This product includes software developed by the University of
18 1.1 mycroft * California, Berkeley and its contributors.
19 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
20 1.1 mycroft * may be used to endorse or promote products derived from this software
21 1.1 mycroft * without specific prior written permission.
22 1.1 mycroft *
23 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 mycroft * SUCH DAMAGE.
34 1.1 mycroft *
35 1.8 fvdl * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
36 1.1 mycroft */
37 1.7 mrg
38 1.11 scottr #if defined(_KERNEL) && !defined(_LKM)
39 1.10 scottr #include "opt_quota.h"
40 1.11 scottr #endif
41 1.1 mycroft
42 1.1 mycroft #include <sys/param.h>
43 1.1 mycroft #include <sys/systm.h>
44 1.1 mycroft #include <sys/buf.h>
45 1.1 mycroft #include <sys/proc.h>
46 1.1 mycroft #include <sys/file.h>
47 1.1 mycroft #include <sys/vnode.h>
48 1.9 bouyer #include <sys/mount.h>
49 1.1 mycroft
50 1.1 mycroft #include <vm/vm.h>
51 1.6 mrg #include <uvm/uvm_extern.h>
52 1.14.4.1 chs #include <uvm/uvm.h>
53 1.6 mrg
54 1.1 mycroft #include <ufs/ufs/quota.h>
55 1.9 bouyer #include <ufs/ufs/ufsmount.h>
56 1.1 mycroft #include <ufs/ufs/inode.h>
57 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
58 1.9 bouyer #include <ufs/ufs/ufs_bswap.h>
59 1.1 mycroft
60 1.1 mycroft #include <ufs/ffs/fs.h>
61 1.1 mycroft #include <ufs/ffs/ffs_extern.h>
62 1.1 mycroft
63 1.1 mycroft /*
64 1.1 mycroft * Balloc defines the structure of file system storage
65 1.1 mycroft * by allocating the physical blocks on a device given
66 1.1 mycroft * the inode and the logical block number in a file.
67 1.1 mycroft */
68 1.3 christos int
69 1.14.4.2 chs ffs_balloc(v)
70 1.14.4.2 chs void *v;
71 1.1 mycroft {
72 1.14.4.2 chs struct vop_balloc_args /* {
73 1.14.4.2 chs struct vnode *a_vp;
74 1.14.4.2 chs off_t a_offset;
75 1.14.4.2 chs int a_size;
76 1.14.4.2 chs struct ucred *a_cred;
77 1.14.4.2 chs int a_flags;
78 1.14.4.2 chs struct buf **a_bpp;
79 1.14.4.2 chs } */ *ap = v;
80 1.14.4.2 chs
81 1.14.4.2 chs struct vnode *vp = ap->a_vp;
82 1.14.4.2 chs struct inode *ip = VTOI(vp);
83 1.14.4.2 chs struct fs *fs = ip->i_fs;
84 1.14.4.2 chs ufs_daddr_t lbn = lblkno(fs, ap->a_offset);
85 1.14.4.2 chs int size = ap->a_size;
86 1.14.4.2 chs struct ucred *cred = ap->a_cred;
87 1.14.4.2 chs int flags = ap->a_flags;
88 1.14.4.2 chs struct buf **bpp = ap->a_bpp;
89 1.14.4.2 chs
90 1.14.4.1 chs ufs_daddr_t nb;
91 1.1 mycroft struct buf *bp, *nbp;
92 1.1 mycroft struct indir indirs[NIADDR + 2];
93 1.8 fvdl ufs_daddr_t newb, *bap, pref;
94 1.8 fvdl int deallocated, osize, nsize, num, i, error;
95 1.8 fvdl ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
96 1.14.4.2 chs UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
97 1.14.4.2 chs
98 1.14.4.2 chs UVMHIST_LOG(ubchist, "vp %p off 0x%x size 0x%x",
99 1.14.4.2 chs vp, (int)ap->a_offset, ap->a_size,0);
100 1.1 mycroft
101 1.14.4.1 chs if (bpp != NULL) {
102 1.14.4.1 chs *bpp = NULL;
103 1.14.4.1 chs }
104 1.14.4.1 chs
105 1.8 fvdl if (lbn < 0)
106 1.1 mycroft return (EFBIG);
107 1.1 mycroft fs = ip->i_fs;
108 1.1 mycroft
109 1.1 mycroft /*
110 1.14.4.1 chs * If the file currently ends with a fragment and
111 1.14.4.1 chs * the block we're allocating now is after the current EOF,
112 1.1 mycroft * this fragment has to be extended to be a full block.
113 1.1 mycroft */
114 1.4 bouyer nb = lblkno(fs, ip->i_ffs_size);
115 1.8 fvdl if (nb < NDADDR && nb < lbn) {
116 1.1 mycroft osize = blksize(fs, ip, nb);
117 1.1 mycroft if (osize < fs->fs_bsize && osize > 0) {
118 1.1 mycroft error = ffs_realloccg(ip, nb,
119 1.4 bouyer ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
120 1.14.4.1 chs osize, (int)fs->fs_bsize, cred, bpp, &newb);
121 1.1 mycroft if (error)
122 1.1 mycroft return (error);
123 1.14.4.1 chs ip->i_ffs_size = lblktosize(fs, nb + 1);
124 1.6 mrg uvm_vnp_setsize(vp, ip->i_ffs_size);
125 1.14.4.1 chs ip->i_ffs_db[nb] = ufs_rw32(newb,
126 1.12 kleink UFS_MPNEEDSWAP(vp->v_mount));
127 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
128 1.14.4.1 chs
129 1.14.4.1 chs if (bpp) {
130 1.14.4.1 chs if (flags & B_SYNC)
131 1.14.4.1 chs bwrite(*bpp);
132 1.14.4.1 chs else
133 1.14.4.1 chs bawrite(*bpp);
134 1.14.4.1 chs }
135 1.14.4.1 chs else {
136 1.14.4.1 chs /*
137 1.14.4.1 chs * XXX the data in the frag might be
138 1.14.4.1 chs * moving to a new disk location.
139 1.14.4.1 chs * we need to flush pages to the
140 1.14.4.1 chs * new disk locations.
141 1.14.4.1 chs * XXX we could do this in realloccg
142 1.14.4.1 chs * except for the sync flag.
143 1.14.4.1 chs */
144 1.14.4.1 chs (vp->v_uvm.u_obj.pgops->pgo_flush)
145 1.14.4.1 chs (&vp->v_uvm.u_obj, lblktosize(fs, nb),
146 1.14.4.1 chs lblktosize(fs, nb + 1),
147 1.14.4.1 chs flags & B_SYNC ? PGO_SYNCIO : 0);
148 1.14.4.1 chs }
149 1.1 mycroft }
150 1.1 mycroft }
151 1.1 mycroft /*
152 1.1 mycroft * The first NDADDR blocks are direct blocks
153 1.1 mycroft */
154 1.8 fvdl if (lbn < NDADDR) {
155 1.14.4.1 chs
156 1.9 bouyer nb = ufs_rw32(ip->i_ffs_db[lbn], UFS_MPNEEDSWAP(vp->v_mount));
157 1.14.4.1 chs if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
158 1.14.4.1 chs
159 1.14.4.1 chs /*
160 1.14.4.1 chs * the block is an already-allocated direct block
161 1.14.4.1 chs * and the file already extends past this block,
162 1.14.4.1 chs * thus this must be a whole block.
163 1.14.4.1 chs * just read the block (if requested).
164 1.14.4.1 chs */
165 1.14.4.1 chs
166 1.14.4.1 chs if (bpp != NULL) {
167 1.14.4.1 chs error = bread(vp, lbn, fs->fs_bsize, NOCRED,
168 1.14.4.1 chs &bp);
169 1.14.4.1 chs if (error) {
170 1.14.4.1 chs brelse(bp);
171 1.14.4.1 chs return (error);
172 1.14.4.1 chs }
173 1.14.4.1 chs *bpp = bp;
174 1.14.4.1 chs }
175 1.1 mycroft return (0);
176 1.1 mycroft }
177 1.1 mycroft if (nb != 0) {
178 1.14.4.2 chs
179 1.1 mycroft /*
180 1.1 mycroft * Consider need to reallocate a fragment.
181 1.1 mycroft */
182 1.14.4.2 chs
183 1.4 bouyer osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
184 1.1 mycroft nsize = fragroundup(fs, size);
185 1.1 mycroft if (nsize <= osize) {
186 1.14.4.1 chs
187 1.14.4.1 chs /*
188 1.14.4.1 chs * the existing block is already
189 1.14.4.1 chs * at least as big as we want.
190 1.14.4.1 chs * just read the block (if requested).
191 1.14.4.1 chs */
192 1.14.4.1 chs
193 1.14.4.1 chs if (bpp != NULL) {
194 1.14.4.1 chs error = bread(vp, lbn, osize, NOCRED,
195 1.14.4.1 chs &bp);
196 1.14.4.1 chs if (error) {
197 1.14.4.1 chs brelse(bp);
198 1.14.4.1 chs return (error);
199 1.14.4.1 chs }
200 1.14.4.1 chs *bpp = bp;
201 1.1 mycroft }
202 1.14.4.1 chs return 0;
203 1.1 mycroft } else {
204 1.14.4.1 chs
205 1.14.4.1 chs /*
206 1.14.4.1 chs * the existing block is smaller than we want,
207 1.14.4.1 chs * grow it.
208 1.14.4.1 chs */
209 1.14.4.1 chs
210 1.8 fvdl error = ffs_realloccg(ip, lbn,
211 1.8 fvdl ffs_blkpref(ip, lbn, (int)lbn,
212 1.8 fvdl &ip->i_ffs_db[0]), osize, nsize, cred,
213 1.14.4.1 chs bpp, &newb);
214 1.1 mycroft if (error)
215 1.1 mycroft return (error);
216 1.14.4.2 chs if (vp->v_type == VREG) {
217 1.14.4.2 chs uvm_vnp_zerorange(vp,
218 1.14.4.2 chs lblktosize(fs, lbn) +
219 1.14.4.3 chs osize, nsize - osize);
220 1.14.4.2 chs }
221 1.1 mycroft }
222 1.1 mycroft } else {
223 1.14.4.1 chs
224 1.14.4.1 chs /*
225 1.14.4.1 chs * the block was not previously allocated,
226 1.14.4.1 chs * allocate a new block or fragment.
227 1.14.4.1 chs */
228 1.14.4.1 chs
229 1.14.4.1 chs if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
230 1.1 mycroft nsize = fragroundup(fs, size);
231 1.1 mycroft else
232 1.1 mycroft nsize = fs->fs_bsize;
233 1.8 fvdl error = ffs_alloc(ip, lbn,
234 1.8 fvdl ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
235 1.8 fvdl nsize, cred, &newb);
236 1.1 mycroft if (error)
237 1.1 mycroft return (error);
238 1.14.4.1 chs if (bpp != NULL) {
239 1.14.4.1 chs bp = getblk(vp, lbn, nsize, 0, 0);
240 1.14.4.1 chs bp->b_blkno = fsbtodb(fs, newb);
241 1.14.4.1 chs if (flags & B_CLRBUF)
242 1.14.4.1 chs clrbuf(bp);
243 1.14.4.1 chs *bpp = bp;
244 1.14.4.1 chs }
245 1.14.4.2 chs if (vp->v_type == VREG) {
246 1.14.4.2 chs uvm_vnp_zerorange(vp, lblktosize(fs, lbn),
247 1.14.4.2 chs nsize);
248 1.14.4.2 chs }
249 1.1 mycroft }
250 1.14.4.1 chs ip->i_ffs_db[lbn] = ufs_rw32(newb, UFS_MPNEEDSWAP(vp->v_mount));
251 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
252 1.1 mycroft return (0);
253 1.1 mycroft }
254 1.14.4.1 chs
255 1.1 mycroft /*
256 1.1 mycroft * Determine the number of levels of indirection.
257 1.1 mycroft */
258 1.14.4.1 chs
259 1.1 mycroft pref = 0;
260 1.8 fvdl if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
261 1.1 mycroft return(error);
262 1.1 mycroft #ifdef DIAGNOSTIC
263 1.1 mycroft if (num < 1)
264 1.1 mycroft panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
265 1.1 mycroft #endif
266 1.1 mycroft /*
267 1.1 mycroft * Fetch the first indirect block allocating if necessary.
268 1.1 mycroft */
269 1.1 mycroft --num;
270 1.9 bouyer nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off],
271 1.12 kleink UFS_MPNEEDSWAP(vp->v_mount));
272 1.8 fvdl allocib = NULL;
273 1.8 fvdl allocblk = allociblk;
274 1.1 mycroft if (nb == 0) {
275 1.8 fvdl pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
276 1.9 bouyer error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
277 1.9 bouyer cred, &newb);
278 1.3 christos if (error)
279 1.1 mycroft return (error);
280 1.1 mycroft nb = newb;
281 1.8 fvdl *allocblk++ = nb;
282 1.1 mycroft bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
283 1.8 fvdl bp->b_blkno = fsbtodb(fs, nb);
284 1.1 mycroft clrbuf(bp);
285 1.1 mycroft /*
286 1.1 mycroft * Write synchronously so that indirect blocks
287 1.1 mycroft * never point at garbage.
288 1.1 mycroft */
289 1.8 fvdl if ((error = bwrite(bp)) != 0)
290 1.8 fvdl goto fail;
291 1.8 fvdl allocib = &ip->i_ffs_ib[indirs[0].in_off];
292 1.9 bouyer *allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount));
293 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
294 1.1 mycroft }
295 1.1 mycroft /*
296 1.1 mycroft * Fetch through the indirect blocks, allocating as necessary.
297 1.1 mycroft */
298 1.1 mycroft for (i = 1;;) {
299 1.1 mycroft error = bread(vp,
300 1.1 mycroft indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
301 1.1 mycroft if (error) {
302 1.1 mycroft brelse(bp);
303 1.8 fvdl goto fail;
304 1.1 mycroft }
305 1.8 fvdl bap = (ufs_daddr_t *)bp->b_data;
306 1.12 kleink nb = ufs_rw32(bap[indirs[i].in_off],
307 1.12 kleink UFS_MPNEEDSWAP(vp->v_mount));
308 1.1 mycroft if (i == num)
309 1.1 mycroft break;
310 1.1 mycroft i += 1;
311 1.1 mycroft if (nb != 0) {
312 1.1 mycroft brelse(bp);
313 1.1 mycroft continue;
314 1.1 mycroft }
315 1.1 mycroft if (pref == 0)
316 1.8 fvdl pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
317 1.3 christos error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
318 1.3 christos &newb);
319 1.3 christos if (error) {
320 1.1 mycroft brelse(bp);
321 1.8 fvdl goto fail;
322 1.1 mycroft }
323 1.1 mycroft nb = newb;
324 1.8 fvdl *allocblk++ = nb;
325 1.1 mycroft nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
326 1.1 mycroft nbp->b_blkno = fsbtodb(fs, nb);
327 1.1 mycroft clrbuf(nbp);
328 1.1 mycroft /*
329 1.1 mycroft * Write synchronously so that indirect blocks
330 1.1 mycroft * never point at garbage.
331 1.1 mycroft */
332 1.3 christos if ((error = bwrite(nbp)) != 0) {
333 1.1 mycroft brelse(bp);
334 1.8 fvdl goto fail;
335 1.1 mycroft }
336 1.9 bouyer bap[indirs[i - 1].in_off] = ufs_rw32(nb,
337 1.12 kleink UFS_MPNEEDSWAP(vp->v_mount));
338 1.1 mycroft /*
339 1.1 mycroft * If required, write synchronously, otherwise use
340 1.1 mycroft * delayed write.
341 1.1 mycroft */
342 1.1 mycroft if (flags & B_SYNC) {
343 1.1 mycroft bwrite(bp);
344 1.1 mycroft } else {
345 1.1 mycroft bdwrite(bp);
346 1.1 mycroft }
347 1.1 mycroft }
348 1.1 mycroft /*
349 1.1 mycroft * Get the data block, allocating if necessary.
350 1.1 mycroft */
351 1.1 mycroft if (nb == 0) {
352 1.1 mycroft pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
353 1.3 christos error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
354 1.3 christos &newb);
355 1.3 christos if (error) {
356 1.1 mycroft brelse(bp);
357 1.8 fvdl goto fail;
358 1.1 mycroft }
359 1.1 mycroft nb = newb;
360 1.8 fvdl *allocblk++ = nb;
361 1.12 kleink bap[indirs[i].in_off] = ufs_rw32(nb,
362 1.12 kleink UFS_MPNEEDSWAP(vp->v_mount));
363 1.1 mycroft /*
364 1.1 mycroft * If required, write synchronously, otherwise use
365 1.1 mycroft * delayed write.
366 1.1 mycroft */
367 1.1 mycroft if (flags & B_SYNC) {
368 1.1 mycroft bwrite(bp);
369 1.1 mycroft } else {
370 1.1 mycroft bdwrite(bp);
371 1.1 mycroft }
372 1.14.4.1 chs if (bpp != NULL) {
373 1.14.4.1 chs nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
374 1.14.4.1 chs nbp->b_blkno = fsbtodb(fs, nb);
375 1.14.4.1 chs if (flags & B_CLRBUF)
376 1.14.4.1 chs clrbuf(nbp);
377 1.14.4.1 chs *bpp = nbp;
378 1.14.4.1 chs }
379 1.14.4.2 chs if (vp->v_type == VREG) {
380 1.14.4.2 chs uvm_vnp_zerorange(vp, lblktosize(fs, lbn), ap->a_size);
381 1.14.4.1 chs }
382 1.1 mycroft return (0);
383 1.1 mycroft }
384 1.14.4.1 chs
385 1.1 mycroft brelse(bp);
386 1.14.4.1 chs
387 1.14.4.1 chs if (bpp != NULL) {
388 1.14.4.1 chs if (flags & B_CLRBUF) {
389 1.14.4.1 chs error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
390 1.14.4.1 chs if (error) {
391 1.14.4.1 chs brelse(nbp);
392 1.14.4.1 chs goto fail;
393 1.14.4.1 chs }
394 1.14.4.1 chs } else {
395 1.14.4.1 chs nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
396 1.14.4.1 chs nbp->b_blkno = fsbtodb(fs, nb);
397 1.14.4.1 chs clrbuf(nbp);
398 1.1 mycroft }
399 1.14.4.1 chs *bpp = nbp;
400 1.14.4.1 chs }
401 1.1 mycroft return (0);
402 1.8 fvdl fail:
403 1.8 fvdl /*
404 1.8 fvdl * If we have failed part way through block allocation, we
405 1.8 fvdl * have to deallocate any indirect blocks that we have allocated.
406 1.8 fvdl */
407 1.8 fvdl for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
408 1.8 fvdl ffs_blkfree(ip, *blkp, fs->fs_bsize);
409 1.8 fvdl deallocated += fs->fs_bsize;
410 1.8 fvdl }
411 1.8 fvdl if (allocib != NULL)
412 1.8 fvdl *allocib = 0;
413 1.8 fvdl if (deallocated) {
414 1.8 fvdl #ifdef QUOTA
415 1.8 fvdl /*
416 1.8 fvdl * Restore user's disk quota because allocation failed.
417 1.8 fvdl */
418 1.8 fvdl (void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
419 1.8 fvdl #endif
420 1.8 fvdl ip->i_ffs_blocks -= btodb(deallocated);
421 1.13 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
422 1.8 fvdl }
423 1.8 fvdl return (error);
424 1.1 mycroft }
425