ffs_balloc.c revision 1.14.8.2 1 1.14.8.2 bouyer /* $NetBSD: ffs_balloc.c,v 1.14.8.2 2000/12/08 09:20:11 bouyer 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.14.8.1 bouyer #include <sys/mount.h>
48 1.1 mycroft #include <sys/vnode.h>
49 1.9 bouyer #include <sys/mount.h>
50 1.1 mycroft
51 1.1 mycroft #include <ufs/ufs/quota.h>
52 1.9 bouyer #include <ufs/ufs/ufsmount.h>
53 1.1 mycroft #include <ufs/ufs/inode.h>
54 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
55 1.9 bouyer #include <ufs/ufs/ufs_bswap.h>
56 1.1 mycroft
57 1.1 mycroft #include <ufs/ffs/fs.h>
58 1.1 mycroft #include <ufs/ffs/ffs_extern.h>
59 1.1 mycroft
60 1.14.8.2 bouyer #include <uvm/uvm.h>
61 1.14.8.2 bouyer
62 1.1 mycroft /*
63 1.1 mycroft * Balloc defines the structure of file system storage
64 1.1 mycroft * by allocating the physical blocks on a device given
65 1.1 mycroft * the inode and the logical block number in a file.
66 1.1 mycroft */
67 1.3 christos int
68 1.14.8.1 bouyer ffs_balloc(v)
69 1.14.8.1 bouyer void *v;
70 1.14.8.1 bouyer {
71 1.14.8.1 bouyer struct vop_balloc_args /* {
72 1.14.8.1 bouyer struct vnode *a_vp;
73 1.14.8.1 bouyer off_t a_startpoint;
74 1.14.8.1 bouyer int a_size;
75 1.14.8.1 bouyer struct ucred *a_cred;
76 1.14.8.1 bouyer int a_flags;
77 1.14.8.2 bouyer struct buf **a_bpp;
78 1.14.8.1 bouyer } */ *ap = v;
79 1.14.8.1 bouyer ufs_daddr_t lbn;
80 1.1 mycroft int size;
81 1.1 mycroft struct ucred *cred;
82 1.1 mycroft int flags;
83 1.14.8.1 bouyer ufs_daddr_t nb;
84 1.1 mycroft struct buf *bp, *nbp;
85 1.14.8.1 bouyer struct vnode *vp = ap->a_vp;
86 1.14.8.1 bouyer struct inode *ip = VTOI(vp);
87 1.14.8.1 bouyer struct fs *fs = ip->i_fs;
88 1.1 mycroft struct indir indirs[NIADDR + 2];
89 1.8 fvdl ufs_daddr_t newb, *bap, pref;
90 1.8 fvdl int deallocated, osize, nsize, num, i, error;
91 1.8 fvdl ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
92 1.14.8.1 bouyer int unwindidx = -1;
93 1.14.8.2 bouyer struct buf **bpp = ap->a_bpp;
94 1.14.8.1 bouyer #ifdef FFS_EI
95 1.14.8.1 bouyer const int needswap = UFS_FSNEEDSWAP(fs);
96 1.14.8.1 bouyer #endif
97 1.14.8.2 bouyer UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
98 1.1 mycroft
99 1.14.8.1 bouyer lbn = lblkno(fs, ap->a_startoffset);
100 1.14.8.1 bouyer size = blkoff(fs, ap->a_startoffset) + ap->a_size;
101 1.14.8.1 bouyer if (size > fs->fs_bsize)
102 1.14.8.1 bouyer panic("ffs_balloc: blk too big");
103 1.14.8.2 bouyer if (bpp != NULL) {
104 1.14.8.2 bouyer *bpp = NULL;
105 1.14.8.2 bouyer }
106 1.14.8.2 bouyer UVMHIST_LOG(ubchist, "vp %p lbn 0x%x size 0x%x", vp, lbn, size,0);
107 1.14.8.2 bouyer
108 1.14.8.2 bouyer KASSERT(size <= fs->fs_bsize);
109 1.8 fvdl if (lbn < 0)
110 1.1 mycroft return (EFBIG);
111 1.14.8.1 bouyer cred = ap->a_cred;
112 1.14.8.1 bouyer flags = ap->a_flags;
113 1.1 mycroft
114 1.1 mycroft /*
115 1.1 mycroft * If the next write will extend the file into a new block,
116 1.1 mycroft * and the file is currently composed of a fragment
117 1.1 mycroft * this fragment has to be extended to be a full block.
118 1.1 mycroft */
119 1.14.8.2 bouyer
120 1.4 bouyer nb = lblkno(fs, ip->i_ffs_size);
121 1.8 fvdl if (nb < NDADDR && nb < lbn) {
122 1.1 mycroft osize = blksize(fs, ip, nb);
123 1.1 mycroft if (osize < fs->fs_bsize && osize > 0) {
124 1.1 mycroft error = ffs_realloccg(ip, nb,
125 1.4 bouyer ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
126 1.14.8.2 bouyer osize, (int)fs->fs_bsize, cred, bpp, &newb);
127 1.1 mycroft if (error)
128 1.1 mycroft return (error);
129 1.14.8.1 bouyer if (DOINGSOFTDEP(vp))
130 1.14.8.2 bouyer softdep_setup_allocdirect(ip, nb, newb,
131 1.14.8.1 bouyer ufs_rw32(ip->i_ffs_db[nb], needswap),
132 1.14.8.2 bouyer fs->fs_bsize, osize, bpp ? *bpp : NULL);
133 1.14.8.2 bouyer ip->i_ffs_size = lblktosize(fs, nb + 1);
134 1.6 mrg uvm_vnp_setsize(vp, ip->i_ffs_size);
135 1.14.8.2 bouyer ip->i_ffs_db[nb] = ufs_rw32(newb, needswap);
136 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
137 1.14.8.2 bouyer if (bpp) {
138 1.14.8.2 bouyer if (flags & B_SYNC)
139 1.14.8.2 bouyer bwrite(*bpp);
140 1.14.8.2 bouyer else
141 1.14.8.2 bouyer bawrite(*bpp);
142 1.14.8.2 bouyer }
143 1.1 mycroft }
144 1.1 mycroft }
145 1.14.8.2 bouyer
146 1.1 mycroft /*
147 1.1 mycroft * The first NDADDR blocks are direct blocks
148 1.1 mycroft */
149 1.14.8.2 bouyer
150 1.8 fvdl if (lbn < NDADDR) {
151 1.14.8.1 bouyer nb = ufs_rw32(ip->i_ffs_db[lbn], needswap);
152 1.14.8.2 bouyer if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
153 1.14.8.2 bouyer
154 1.14.8.2 bouyer /*
155 1.14.8.2 bouyer * The block is an already-allocated direct block
156 1.14.8.2 bouyer * and the file already extends past this block,
157 1.14.8.2 bouyer * thus this must be a whole block.
158 1.14.8.2 bouyer * Just read the block (if requested).
159 1.14.8.2 bouyer */
160 1.14.8.2 bouyer
161 1.14.8.2 bouyer if (bpp != NULL) {
162 1.14.8.2 bouyer error = bread(vp, lbn, fs->fs_bsize, NOCRED,
163 1.14.8.2 bouyer bpp);
164 1.14.8.2 bouyer if (error) {
165 1.14.8.2 bouyer brelse(*bpp);
166 1.14.8.2 bouyer return (error);
167 1.14.8.2 bouyer }
168 1.1 mycroft }
169 1.1 mycroft return (0);
170 1.1 mycroft }
171 1.1 mycroft if (nb != 0) {
172 1.14.8.2 bouyer
173 1.1 mycroft /*
174 1.1 mycroft * Consider need to reallocate a fragment.
175 1.1 mycroft */
176 1.14.8.2 bouyer
177 1.4 bouyer osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
178 1.1 mycroft nsize = fragroundup(fs, size);
179 1.1 mycroft if (nsize <= osize) {
180 1.14.8.2 bouyer
181 1.14.8.2 bouyer /*
182 1.14.8.2 bouyer * The existing block is already
183 1.14.8.2 bouyer * at least as big as we want.
184 1.14.8.2 bouyer * Just read the block (if requested).
185 1.14.8.2 bouyer */
186 1.14.8.2 bouyer
187 1.14.8.2 bouyer if (bpp != NULL) {
188 1.14.8.2 bouyer error = bread(vp, lbn, osize, NOCRED,
189 1.14.8.2 bouyer bpp);
190 1.14.8.2 bouyer if (error) {
191 1.14.8.2 bouyer brelse(*bpp);
192 1.14.8.2 bouyer return (error);
193 1.14.8.2 bouyer }
194 1.1 mycroft }
195 1.14.8.2 bouyer return 0;
196 1.1 mycroft } else {
197 1.14.8.2 bouyer
198 1.14.8.2 bouyer /*
199 1.14.8.2 bouyer * The existing block is smaller than we want,
200 1.14.8.2 bouyer * grow it.
201 1.14.8.2 bouyer */
202 1.14.8.2 bouyer
203 1.8 fvdl error = ffs_realloccg(ip, lbn,
204 1.8 fvdl ffs_blkpref(ip, lbn, (int)lbn,
205 1.8 fvdl &ip->i_ffs_db[0]), osize, nsize, cred,
206 1.14.8.2 bouyer bpp, &newb);
207 1.1 mycroft if (error)
208 1.1 mycroft return (error);
209 1.14.8.1 bouyer if (DOINGSOFTDEP(vp))
210 1.14.8.1 bouyer softdep_setup_allocdirect(ip, lbn,
211 1.14.8.2 bouyer newb, nb, nsize, osize,
212 1.14.8.2 bouyer bpp ? *bpp : NULL);
213 1.1 mycroft }
214 1.1 mycroft } else {
215 1.14.8.2 bouyer
216 1.14.8.2 bouyer /*
217 1.14.8.2 bouyer * the block was not previously allocated,
218 1.14.8.2 bouyer * allocate a new block or fragment.
219 1.14.8.2 bouyer */
220 1.14.8.2 bouyer
221 1.14.8.2 bouyer if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
222 1.1 mycroft nsize = fragroundup(fs, size);
223 1.1 mycroft else
224 1.1 mycroft nsize = fs->fs_bsize;
225 1.8 fvdl error = ffs_alloc(ip, lbn,
226 1.8 fvdl ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
227 1.8 fvdl nsize, cred, &newb);
228 1.1 mycroft if (error)
229 1.1 mycroft return (error);
230 1.14.8.2 bouyer if (bpp != NULL) {
231 1.14.8.2 bouyer bp = getblk(vp, lbn, nsize, 0, 0);
232 1.14.8.2 bouyer bp->b_blkno = fsbtodb(fs, newb);
233 1.14.8.2 bouyer if (flags & B_CLRBUF)
234 1.14.8.2 bouyer clrbuf(bp);
235 1.14.8.2 bouyer *bpp = bp;
236 1.14.8.2 bouyer }
237 1.14.8.2 bouyer if (DOINGSOFTDEP(vp)) {
238 1.14.8.1 bouyer softdep_setup_allocdirect(ip, lbn, newb, 0,
239 1.14.8.2 bouyer nsize, 0, bpp ? *bpp : NULL);
240 1.14.8.2 bouyer }
241 1.1 mycroft }
242 1.14.8.2 bouyer ip->i_ffs_db[lbn] = ufs_rw32(newb, needswap);
243 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
244 1.1 mycroft return (0);
245 1.1 mycroft }
246 1.1 mycroft /*
247 1.1 mycroft * Determine the number of levels of indirection.
248 1.1 mycroft */
249 1.1 mycroft pref = 0;
250 1.8 fvdl if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
251 1.1 mycroft return(error);
252 1.14.8.2 bouyer
253 1.1 mycroft #ifdef DIAGNOSTIC
254 1.1 mycroft if (num < 1)
255 1.1 mycroft panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
256 1.1 mycroft #endif
257 1.1 mycroft /*
258 1.1 mycroft * Fetch the first indirect block allocating if necessary.
259 1.1 mycroft */
260 1.1 mycroft --num;
261 1.14.8.1 bouyer nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off], needswap);
262 1.8 fvdl allocib = NULL;
263 1.8 fvdl allocblk = allociblk;
264 1.1 mycroft if (nb == 0) {
265 1.8 fvdl pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
266 1.14.8.1 bouyer error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
267 1.14.8.1 bouyer &newb);
268 1.3 christos if (error)
269 1.1 mycroft return (error);
270 1.1 mycroft nb = newb;
271 1.8 fvdl *allocblk++ = nb;
272 1.1 mycroft bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
273 1.8 fvdl bp->b_blkno = fsbtodb(fs, nb);
274 1.1 mycroft clrbuf(bp);
275 1.14.8.1 bouyer if (DOINGSOFTDEP(vp)) {
276 1.14.8.1 bouyer softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
277 1.14.8.1 bouyer newb, 0, fs->fs_bsize, 0, bp);
278 1.14.8.1 bouyer bdwrite(bp);
279 1.14.8.1 bouyer } else {
280 1.14.8.1 bouyer /*
281 1.14.8.1 bouyer * Write synchronously so that indirect blocks
282 1.14.8.1 bouyer * never point at garbage.
283 1.14.8.1 bouyer */
284 1.14.8.1 bouyer if ((error = bwrite(bp)) != 0)
285 1.14.8.1 bouyer goto fail;
286 1.14.8.1 bouyer }
287 1.14.8.1 bouyer unwindidx = 0;
288 1.8 fvdl allocib = &ip->i_ffs_ib[indirs[0].in_off];
289 1.14.8.1 bouyer *allocib = ufs_rw32(nb, needswap);
290 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
291 1.1 mycroft }
292 1.1 mycroft /*
293 1.1 mycroft * Fetch through the indirect blocks, allocating as necessary.
294 1.1 mycroft */
295 1.1 mycroft for (i = 1;;) {
296 1.1 mycroft error = bread(vp,
297 1.1 mycroft indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
298 1.1 mycroft if (error) {
299 1.1 mycroft brelse(bp);
300 1.8 fvdl goto fail;
301 1.1 mycroft }
302 1.8 fvdl bap = (ufs_daddr_t *)bp->b_data;
303 1.14.8.1 bouyer nb = ufs_rw32(bap[indirs[i].in_off], needswap);
304 1.1 mycroft if (i == num)
305 1.1 mycroft break;
306 1.14.8.1 bouyer i++;
307 1.1 mycroft if (nb != 0) {
308 1.1 mycroft brelse(bp);
309 1.1 mycroft continue;
310 1.1 mycroft }
311 1.1 mycroft if (pref == 0)
312 1.8 fvdl pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
313 1.3 christos error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
314 1.14.8.1 bouyer &newb);
315 1.3 christos if (error) {
316 1.1 mycroft brelse(bp);
317 1.8 fvdl goto fail;
318 1.1 mycroft }
319 1.1 mycroft nb = newb;
320 1.8 fvdl *allocblk++ = nb;
321 1.1 mycroft nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
322 1.1 mycroft nbp->b_blkno = fsbtodb(fs, nb);
323 1.1 mycroft clrbuf(nbp);
324 1.14.8.1 bouyer if (DOINGSOFTDEP(vp)) {
325 1.14.8.1 bouyer softdep_setup_allocindir_meta(nbp, ip, bp,
326 1.14.8.1 bouyer indirs[i - 1].in_off, nb);
327 1.14.8.1 bouyer bdwrite(nbp);
328 1.14.8.1 bouyer } else {
329 1.14.8.1 bouyer /*
330 1.14.8.1 bouyer * Write synchronously so that indirect blocks
331 1.14.8.1 bouyer * never point at garbage.
332 1.14.8.1 bouyer */
333 1.14.8.1 bouyer if ((error = bwrite(nbp)) != 0) {
334 1.14.8.1 bouyer brelse(bp);
335 1.14.8.1 bouyer goto fail;
336 1.14.8.1 bouyer }
337 1.1 mycroft }
338 1.14.8.1 bouyer if (unwindidx < 0)
339 1.14.8.1 bouyer unwindidx = i - 1;
340 1.14.8.1 bouyer bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
341 1.1 mycroft /*
342 1.1 mycroft * If required, write synchronously, otherwise use
343 1.1 mycroft * delayed write.
344 1.1 mycroft */
345 1.1 mycroft if (flags & B_SYNC) {
346 1.1 mycroft bwrite(bp);
347 1.1 mycroft } else {
348 1.1 mycroft bdwrite(bp);
349 1.1 mycroft }
350 1.1 mycroft }
351 1.1 mycroft /*
352 1.1 mycroft * Get the data block, allocating if necessary.
353 1.1 mycroft */
354 1.1 mycroft if (nb == 0) {
355 1.14.8.1 bouyer pref = ffs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
356 1.3 christos error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
357 1.14.8.1 bouyer &newb);
358 1.3 christos if (error) {
359 1.1 mycroft brelse(bp);
360 1.8 fvdl goto fail;
361 1.1 mycroft }
362 1.1 mycroft nb = newb;
363 1.8 fvdl *allocblk++ = nb;
364 1.14.8.2 bouyer if (bpp != NULL) {
365 1.14.8.2 bouyer nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
366 1.14.8.2 bouyer nbp->b_blkno = fsbtodb(fs, nb);
367 1.14.8.2 bouyer if (flags & B_CLRBUF)
368 1.14.8.2 bouyer clrbuf(nbp);
369 1.14.8.2 bouyer *bpp = nbp;
370 1.14.8.2 bouyer }
371 1.14.8.1 bouyer if (DOINGSOFTDEP(vp))
372 1.14.8.1 bouyer softdep_setup_allocindir_page(ip, lbn, bp,
373 1.14.8.2 bouyer indirs[num].in_off, nb, 0, bpp ? *bpp : NULL);
374 1.14.8.1 bouyer bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
375 1.14.8.2 bouyer if (allocib == NULL && unwindidx < 0) {
376 1.14.8.2 bouyer unwindidx = i - 1;
377 1.14.8.2 bouyer }
378 1.1 mycroft /*
379 1.1 mycroft * If required, write synchronously, otherwise use
380 1.1 mycroft * delayed write.
381 1.1 mycroft */
382 1.1 mycroft if (flags & B_SYNC) {
383 1.1 mycroft bwrite(bp);
384 1.1 mycroft } else {
385 1.1 mycroft bdwrite(bp);
386 1.1 mycroft }
387 1.1 mycroft return (0);
388 1.1 mycroft }
389 1.1 mycroft brelse(bp);
390 1.14.8.2 bouyer if (bpp != NULL) {
391 1.14.8.2 bouyer if (flags & B_CLRBUF) {
392 1.14.8.2 bouyer error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
393 1.14.8.2 bouyer if (error) {
394 1.14.8.2 bouyer brelse(nbp);
395 1.14.8.2 bouyer goto fail;
396 1.14.8.2 bouyer }
397 1.14.8.2 bouyer } else {
398 1.14.8.2 bouyer nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
399 1.14.8.2 bouyer nbp->b_blkno = fsbtodb(fs, nb);
400 1.14.8.2 bouyer clrbuf(nbp);
401 1.1 mycroft }
402 1.14.8.2 bouyer *bpp = nbp;
403 1.1 mycroft }
404 1.1 mycroft return (0);
405 1.8 fvdl fail:
406 1.8 fvdl /*
407 1.8 fvdl * If we have failed part way through block allocation, we
408 1.8 fvdl * have to deallocate any indirect blocks that we have allocated.
409 1.14.8.1 bouyer * We have to fsync the file before we start to get rid of all
410 1.14.8.1 bouyer * of its dependencies so that we do not leave them dangling.
411 1.14.8.1 bouyer * We have to sync it at the end so that the soft updates code
412 1.14.8.1 bouyer * does not find any untracked changes. Although this is really
413 1.14.8.1 bouyer * slow, running out of disk space is not expected to be a common
414 1.14.8.1 bouyer * occurence. The error return from fsync is ignored as we already
415 1.14.8.1 bouyer * have an error to return to the user.
416 1.8 fvdl */
417 1.14.8.1 bouyer (void) VOP_FSYNC(vp, cred, FSYNC_WAIT, 0, 0, curproc);
418 1.8 fvdl for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
419 1.8 fvdl ffs_blkfree(ip, *blkp, fs->fs_bsize);
420 1.8 fvdl deallocated += fs->fs_bsize;
421 1.8 fvdl }
422 1.14.8.1 bouyer if (unwindidx >= 0) {
423 1.14.8.1 bouyer if (unwindidx == 0) {
424 1.14.8.1 bouyer *allocib = 0;
425 1.14.8.1 bouyer } else {
426 1.14.8.1 bouyer int r;
427 1.14.8.1 bouyer
428 1.14.8.1 bouyer r = bread(vp, indirs[unwindidx].in_lbn,
429 1.14.8.1 bouyer (int)fs->fs_bsize, NOCRED, &bp);
430 1.14.8.1 bouyer if (r) {
431 1.14.8.1 bouyer panic("Could not unwind indirect block, error %d", r);
432 1.14.8.1 bouyer brelse(bp);
433 1.14.8.1 bouyer } else {
434 1.14.8.1 bouyer bap = (ufs_daddr_t *)bp->b_data;
435 1.14.8.1 bouyer bap[indirs[unwindidx].in_off] = 0;
436 1.14.8.1 bouyer if (flags & B_SYNC)
437 1.14.8.1 bouyer bwrite(bp);
438 1.14.8.1 bouyer else
439 1.14.8.1 bouyer bdwrite(bp);
440 1.14.8.1 bouyer }
441 1.14.8.1 bouyer }
442 1.14.8.1 bouyer for (i = unwindidx + 1; i <= num; i++) {
443 1.14.8.1 bouyer bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
444 1.14.8.1 bouyer 0);
445 1.14.8.1 bouyer bp->b_flags |= B_INVAL;
446 1.14.8.1 bouyer brelse(bp);
447 1.14.8.1 bouyer }
448 1.14.8.1 bouyer }
449 1.8 fvdl if (deallocated) {
450 1.8 fvdl #ifdef QUOTA
451 1.8 fvdl /*
452 1.8 fvdl * Restore user's disk quota because allocation failed.
453 1.8 fvdl */
454 1.8 fvdl (void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
455 1.8 fvdl #endif
456 1.8 fvdl ip->i_ffs_blocks -= btodb(deallocated);
457 1.13 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
458 1.8 fvdl }
459 1.14.8.1 bouyer (void) VOP_FSYNC(vp, cred, FSYNC_WAIT, 0, 0, curproc);
460 1.8 fvdl return (error);
461 1.14.8.2 bouyer }
462 1.14.8.2 bouyer
463 1.14.8.2 bouyer
464 1.14.8.2 bouyer int
465 1.14.8.2 bouyer ffs_ballocn(v)
466 1.14.8.2 bouyer void *v;
467 1.14.8.2 bouyer {
468 1.14.8.2 bouyer struct vop_ballocn_args /* {
469 1.14.8.2 bouyer struct vnode *a_vp;
470 1.14.8.2 bouyer off_t a_offset;
471 1.14.8.2 bouyer off_t a_length;
472 1.14.8.2 bouyer struct ucred *a_cred;
473 1.14.8.2 bouyer int a_flags;
474 1.14.8.2 bouyer } */ *ap = v;
475 1.14.8.2 bouyer
476 1.14.8.2 bouyer off_t off, len;
477 1.14.8.2 bouyer struct vnode *vp = ap->a_vp;
478 1.14.8.2 bouyer struct inode *ip = VTOI(vp);
479 1.14.8.2 bouyer struct fs *fs = ip->i_fs;
480 1.14.8.2 bouyer int error, delta, bshift, bsize;
481 1.14.8.2 bouyer
482 1.14.8.2 bouyer error = 0;
483 1.14.8.2 bouyer bshift = fs->fs_bshift;
484 1.14.8.2 bouyer bsize = 1 << bshift;
485 1.14.8.2 bouyer
486 1.14.8.2 bouyer off = ap->a_offset;
487 1.14.8.2 bouyer len = ap->a_length;
488 1.14.8.2 bouyer
489 1.14.8.2 bouyer delta = off & (bsize - 1);
490 1.14.8.2 bouyer off -= delta;
491 1.14.8.2 bouyer len += delta;
492 1.14.8.2 bouyer
493 1.14.8.2 bouyer while (len > 0) {
494 1.14.8.2 bouyer bsize = min(bsize, len);
495 1.14.8.2 bouyer
496 1.14.8.2 bouyer error = VOP_BALLOC(vp, off, bsize, ap->a_cred, ap->a_flags,
497 1.14.8.2 bouyer NULL);
498 1.14.8.2 bouyer if (error) {
499 1.14.8.2 bouyer goto out;
500 1.14.8.2 bouyer }
501 1.14.8.2 bouyer
502 1.14.8.2 bouyer /*
503 1.14.8.2 bouyer * increase file size now, VOP_BALLOC() requires that
504 1.14.8.2 bouyer * EOF be up-to-date before each call.
505 1.14.8.2 bouyer */
506 1.14.8.2 bouyer
507 1.14.8.2 bouyer if (ip->i_ffs_size < off + bsize) {
508 1.14.8.2 bouyer ip->i_ffs_size = off + bsize;
509 1.14.8.2 bouyer if (vp->v_uvm.u_size < ip->i_ffs_size) {
510 1.14.8.2 bouyer uvm_vnp_setsize(vp, ip->i_ffs_size);
511 1.14.8.2 bouyer }
512 1.14.8.2 bouyer }
513 1.14.8.2 bouyer
514 1.14.8.2 bouyer off += bsize;
515 1.14.8.2 bouyer len -= bsize;
516 1.14.8.2 bouyer }
517 1.14.8.2 bouyer
518 1.14.8.2 bouyer out:
519 1.14.8.2 bouyer return error;
520 1.1 mycroft }
521