ffs_balloc.c revision 1.63 1 1.63 pgoyette /* $NetBSD: ffs_balloc.c,v 1.63 2017/10/28 00:37:13 pgoyette Exp $ */
2 1.2 cgd
3 1.1 mycroft /*
4 1.33 fvdl * Copyright (c) 2002 Networks Associates Technology, Inc.
5 1.33 fvdl * All rights reserved.
6 1.33 fvdl *
7 1.33 fvdl * This software was developed for the FreeBSD Project by Marshall
8 1.33 fvdl * Kirk McKusick and Network Associates Laboratories, the Security
9 1.33 fvdl * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10 1.33 fvdl * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 1.33 fvdl * research program
12 1.33 fvdl *
13 1.1 mycroft * Copyright (c) 1982, 1986, 1989, 1993
14 1.1 mycroft * The Regents of the University of California. All rights reserved.
15 1.1 mycroft *
16 1.1 mycroft * Redistribution and use in source and binary forms, with or without
17 1.1 mycroft * modification, are permitted provided that the following conditions
18 1.1 mycroft * are met:
19 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
20 1.1 mycroft * notice, this list of conditions and the following disclaimer.
21 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
23 1.1 mycroft * documentation and/or other materials provided with the distribution.
24 1.34 agc * 3. Neither the name of the University nor the names of its contributors
25 1.1 mycroft * may be used to endorse or promote products derived from this software
26 1.1 mycroft * without specific prior written permission.
27 1.1 mycroft *
28 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 mycroft * SUCH DAMAGE.
39 1.1 mycroft *
40 1.8 fvdl * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
41 1.1 mycroft */
42 1.28 lukem
43 1.28 lukem #include <sys/cdefs.h>
44 1.63 pgoyette __KERNEL_RCSID(0, "$NetBSD: ffs_balloc.c,v 1.63 2017/10/28 00:37:13 pgoyette Exp $");
45 1.7 mrg
46 1.24 mrg #if defined(_KERNEL_OPT)
47 1.10 scottr #include "opt_quota.h"
48 1.11 scottr #endif
49 1.1 mycroft
50 1.1 mycroft #include <sys/param.h>
51 1.1 mycroft #include <sys/systm.h>
52 1.1 mycroft #include <sys/buf.h>
53 1.1 mycroft #include <sys/file.h>
54 1.15 fvdl #include <sys/mount.h>
55 1.1 mycroft #include <sys/vnode.h>
56 1.43 elad #include <sys/kauth.h>
57 1.49 hannken #include <sys/fstrans.h>
58 1.6 mrg
59 1.1 mycroft #include <ufs/ufs/quota.h>
60 1.9 bouyer #include <ufs/ufs/ufsmount.h>
61 1.1 mycroft #include <ufs/ufs/inode.h>
62 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
63 1.9 bouyer #include <ufs/ufs/ufs_bswap.h>
64 1.1 mycroft
65 1.1 mycroft #include <ufs/ffs/fs.h>
66 1.1 mycroft #include <ufs/ffs/ffs_extern.h>
67 1.1 mycroft
68 1.23 chs #include <uvm/uvm.h>
69 1.23 chs
70 1.43 elad static int ffs_balloc_ufs1(struct vnode *, off_t, int, kauth_cred_t, int,
71 1.39 yamt struct buf **);
72 1.43 elad static int ffs_balloc_ufs2(struct vnode *, off_t, int, kauth_cred_t, int,
73 1.39 yamt struct buf **);
74 1.33 fvdl
75 1.1 mycroft /*
76 1.1 mycroft * Balloc defines the structure of file system storage
77 1.1 mycroft * by allocating the physical blocks on a device given
78 1.1 mycroft * the inode and the logical block number in a file.
79 1.1 mycroft */
80 1.33 fvdl
81 1.3 christos int
82 1.43 elad ffs_balloc(struct vnode *vp, off_t off, int size, kauth_cred_t cred, int flags,
83 1.39 yamt struct buf **bpp)
84 1.15 fvdl {
85 1.49 hannken int error;
86 1.33 fvdl
87 1.39 yamt if (VTOI(vp)->i_fs->fs_magic == FS_UFS2_MAGIC)
88 1.49 hannken error = ffs_balloc_ufs2(vp, off, size, cred, flags, bpp);
89 1.33 fvdl else
90 1.49 hannken error = ffs_balloc_ufs1(vp, off, size, cred, flags, bpp);
91 1.49 hannken
92 1.49 hannken if (error == 0 && bpp != NULL && (error = fscow_run(*bpp, false)) != 0)
93 1.49 hannken brelse(*bpp, 0);
94 1.49 hannken
95 1.49 hannken return error;
96 1.49 hannken }
97 1.49 hannken
98 1.49 hannken static int
99 1.43 elad ffs_balloc_ufs1(struct vnode *vp, off_t off, int size, kauth_cred_t cred,
100 1.39 yamt int flags, struct buf **bpp)
101 1.33 fvdl {
102 1.33 fvdl daddr_t lbn, lastlbn;
103 1.1 mycroft struct buf *bp, *nbp;
104 1.15 fvdl struct inode *ip = VTOI(vp);
105 1.15 fvdl struct fs *fs = ip->i_fs;
106 1.46 ad struct ufsmount *ump = ip->i_ump;
107 1.56 dholland struct indir indirs[UFS_NIADDR + 2];
108 1.37 mycroft daddr_t newb, pref, nb;
109 1.31 fvdl int32_t *bap; /* XXX ondisk32 */
110 1.8 fvdl int deallocated, osize, nsize, num, i, error;
111 1.56 dholland int32_t *blkp, *allocblk, allociblk[UFS_NIADDR + 1];
112 1.33 fvdl int32_t *allocib;
113 1.17 fvdl int unwindidx = -1;
114 1.15 fvdl const int needswap = UFS_FSNEEDSWAP(fs);
115 1.23 chs UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
116 1.1 mycroft
117 1.59 dholland lbn = ffs_lblkno(fs, off);
118 1.57 dholland size = ffs_blkoff(fs, off) + size;
119 1.15 fvdl if (size > fs->fs_bsize)
120 1.15 fvdl panic("ffs_balloc: blk too big");
121 1.23 chs if (bpp != NULL) {
122 1.23 chs *bpp = NULL;
123 1.23 chs }
124 1.63 pgoyette UVMHIST_LOG(ubchist, "vp %#jx lbn 0x%jx size 0x%jx", (uintptr_t)vp,
125 1.63 pgoyette lbn, size, 0);
126 1.23 chs
127 1.8 fvdl if (lbn < 0)
128 1.1 mycroft return (EFBIG);
129 1.1 mycroft
130 1.1 mycroft /*
131 1.1 mycroft * If the next write will extend the file into a new block,
132 1.1 mycroft * and the file is currently composed of a fragment
133 1.1 mycroft * this fragment has to be extended to be a full block.
134 1.1 mycroft */
135 1.23 chs
136 1.59 dholland lastlbn = ffs_lblkno(fs, ip->i_size);
137 1.56 dholland if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
138 1.33 fvdl nb = lastlbn;
139 1.57 dholland osize = ffs_blksize(fs, ip, nb);
140 1.1 mycroft if (osize < fs->fs_bsize && osize > 0) {
141 1.46 ad mutex_enter(&ump->um_lock);
142 1.1 mycroft error = ffs_realloccg(ip, nb,
143 1.51 simonb ffs_blkpref_ufs1(ip, lastlbn, nb, flags,
144 1.33 fvdl &ip->i_ffs1_db[0]),
145 1.33 fvdl osize, (int)fs->fs_bsize, cred, bpp, &newb);
146 1.1 mycroft if (error)
147 1.1 mycroft return (error);
148 1.59 dholland ip->i_size = ffs_lblktosize(fs, nb + 1);
149 1.33 fvdl ip->i_ffs1_size = ip->i_size;
150 1.33 fvdl uvm_vnp_setsize(vp, ip->i_ffs1_size);
151 1.37 mycroft ip->i_ffs1_db[nb] = ufs_rw32((u_int32_t)newb, needswap);
152 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
153 1.42 christos if (bpp && *bpp) {
154 1.23 chs if (flags & B_SYNC)
155 1.23 chs bwrite(*bpp);
156 1.23 chs else
157 1.23 chs bawrite(*bpp);
158 1.23 chs }
159 1.1 mycroft }
160 1.1 mycroft }
161 1.23 chs
162 1.1 mycroft /*
163 1.56 dholland * The first UFS_NDADDR blocks are direct blocks
164 1.1 mycroft */
165 1.23 chs
166 1.56 dholland if (lbn < UFS_NDADDR) {
167 1.33 fvdl nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
168 1.59 dholland if (nb != 0 && ip->i_size >= ffs_lblktosize(fs, lbn + 1)) {
169 1.23 chs
170 1.23 chs /*
171 1.23 chs * The block is an already-allocated direct block
172 1.23 chs * and the file already extends past this block,
173 1.23 chs * thus this must be a whole block.
174 1.23 chs * Just read the block (if requested).
175 1.23 chs */
176 1.23 chs
177 1.23 chs if (bpp != NULL) {
178 1.61 maxv error = bread(vp, lbn, fs->fs_bsize,
179 1.49 hannken B_MODIFY, bpp);
180 1.23 chs if (error) {
181 1.23 chs return (error);
182 1.23 chs }
183 1.1 mycroft }
184 1.1 mycroft return (0);
185 1.1 mycroft }
186 1.1 mycroft if (nb != 0) {
187 1.23 chs
188 1.1 mycroft /*
189 1.1 mycroft * Consider need to reallocate a fragment.
190 1.1 mycroft */
191 1.23 chs
192 1.59 dholland osize = ffs_fragroundup(fs, ffs_blkoff(fs, ip->i_size));
193 1.59 dholland nsize = ffs_fragroundup(fs, size);
194 1.1 mycroft if (nsize <= osize) {
195 1.23 chs
196 1.23 chs /*
197 1.23 chs * The existing block is already
198 1.23 chs * at least as big as we want.
199 1.23 chs * Just read the block (if requested).
200 1.23 chs */
201 1.23 chs
202 1.23 chs if (bpp != NULL) {
203 1.61 maxv error = bread(vp, lbn, osize,
204 1.49 hannken B_MODIFY, bpp);
205 1.23 chs if (error) {
206 1.23 chs return (error);
207 1.23 chs }
208 1.1 mycroft }
209 1.23 chs return 0;
210 1.1 mycroft } else {
211 1.23 chs
212 1.23 chs /*
213 1.23 chs * The existing block is smaller than we want,
214 1.23 chs * grow it.
215 1.23 chs */
216 1.46 ad mutex_enter(&ump->um_lock);
217 1.8 fvdl error = ffs_realloccg(ip, lbn,
218 1.51 simonb ffs_blkpref_ufs1(ip, lbn, (int)lbn, flags,
219 1.51 simonb &ip->i_ffs1_db[0]),
220 1.51 simonb osize, nsize, cred, bpp, &newb);
221 1.1 mycroft if (error)
222 1.1 mycroft return (error);
223 1.1 mycroft }
224 1.1 mycroft } else {
225 1.23 chs
226 1.23 chs /*
227 1.23 chs * the block was not previously allocated,
228 1.23 chs * allocate a new block or fragment.
229 1.23 chs */
230 1.23 chs
231 1.59 dholland if (ip->i_size < ffs_lblktosize(fs, lbn + 1))
232 1.59 dholland nsize = ffs_fragroundup(fs, size);
233 1.1 mycroft else
234 1.1 mycroft nsize = fs->fs_bsize;
235 1.46 ad mutex_enter(&ump->um_lock);
236 1.8 fvdl error = ffs_alloc(ip, lbn,
237 1.51 simonb ffs_blkpref_ufs1(ip, lbn, (int)lbn, flags,
238 1.33 fvdl &ip->i_ffs1_db[0]),
239 1.51 simonb nsize, flags, cred, &newb);
240 1.1 mycroft if (error)
241 1.1 mycroft return (error);
242 1.23 chs if (bpp != NULL) {
243 1.58 dholland error = ffs_getblk(vp, lbn, FFS_FSBTODB(fs, newb),
244 1.49 hannken nsize, (flags & B_CLRBUF) != 0, bpp);
245 1.49 hannken if (error)
246 1.49 hannken return error;
247 1.23 chs }
248 1.1 mycroft }
249 1.37 mycroft ip->i_ffs1_db[lbn] = ufs_rw32((u_int32_t)newb, needswap);
250 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
251 1.1 mycroft return (0);
252 1.1 mycroft }
253 1.29 chs
254 1.1 mycroft /*
255 1.1 mycroft * Determine the number of levels of indirection.
256 1.1 mycroft */
257 1.29 chs
258 1.1 mycroft pref = 0;
259 1.8 fvdl if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
260 1.29 chs return (error);
261 1.23 chs
262 1.1 mycroft /*
263 1.1 mycroft * Fetch the first indirect block allocating if necessary.
264 1.1 mycroft */
265 1.29 chs
266 1.1 mycroft --num;
267 1.33 fvdl nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
268 1.8 fvdl allocib = NULL;
269 1.8 fvdl allocblk = allociblk;
270 1.1 mycroft if (nb == 0) {
271 1.46 ad mutex_enter(&ump->um_lock);
272 1.51 simonb pref = ffs_blkpref_ufs1(ip, lbn, 0, flags | B_METAONLY, NULL);
273 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
274 1.51 simonb flags | B_METAONLY, cred, &newb);
275 1.3 christos if (error)
276 1.27 chs goto fail;
277 1.1 mycroft nb = newb;
278 1.8 fvdl *allocblk++ = nb;
279 1.58 dholland error = ffs_getblk(vp, indirs[1].in_lbn, FFS_FSBTODB(fs, nb),
280 1.49 hannken fs->fs_bsize, true, &bp);
281 1.49 hannken if (error)
282 1.49 hannken goto fail;
283 1.52 ad /*
284 1.52 ad * Write synchronously so that indirect blocks
285 1.52 ad * never point at garbage.
286 1.52 ad */
287 1.52 ad if ((error = bwrite(bp)) != 0)
288 1.52 ad goto fail;
289 1.18 mycroft unwindidx = 0;
290 1.33 fvdl allocib = &ip->i_ffs1_ib[indirs[0].in_off];
291 1.33 fvdl *allocib = ufs_rw32(nb, needswap);
292 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
293 1.1 mycroft }
294 1.29 chs
295 1.1 mycroft /*
296 1.1 mycroft * Fetch through the indirect blocks, allocating as necessary.
297 1.1 mycroft */
298 1.29 chs
299 1.1 mycroft for (i = 1;;) {
300 1.1 mycroft error = bread(vp,
301 1.61 maxv indirs[i].in_lbn, (int)fs->fs_bsize, 0, &bp);
302 1.1 mycroft if (error) {
303 1.8 fvdl goto fail;
304 1.1 mycroft }
305 1.31 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
306 1.15 fvdl nb = ufs_rw32(bap[indirs[i].in_off], needswap);
307 1.1 mycroft if (i == num)
308 1.1 mycroft break;
309 1.18 mycroft i++;
310 1.1 mycroft if (nb != 0) {
311 1.46 ad brelse(bp, 0);
312 1.1 mycroft continue;
313 1.1 mycroft }
314 1.49 hannken if (fscow_run(bp, true) != 0) {
315 1.49 hannken brelse(bp, 0);
316 1.49 hannken goto fail;
317 1.49 hannken }
318 1.46 ad mutex_enter(&ump->um_lock);
319 1.54 hannken /* Try to keep snapshot indirect blocks contiguous. */
320 1.54 hannken if (i == num && (ip->i_flags & SF_SNAPSHOT) != 0)
321 1.54 hannken pref = ffs_blkpref_ufs1(ip, lbn, indirs[i-1].in_off,
322 1.54 hannken flags | B_METAONLY, &bap[0]);
323 1.1 mycroft if (pref == 0)
324 1.51 simonb pref = ffs_blkpref_ufs1(ip, lbn, 0, flags | B_METAONLY,
325 1.51 simonb NULL);
326 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
327 1.51 simonb flags | B_METAONLY, cred, &newb);
328 1.3 christos if (error) {
329 1.46 ad brelse(bp, 0);
330 1.8 fvdl goto fail;
331 1.1 mycroft }
332 1.1 mycroft nb = newb;
333 1.8 fvdl *allocblk++ = nb;
334 1.58 dholland error = ffs_getblk(vp, indirs[i].in_lbn, FFS_FSBTODB(fs, nb),
335 1.49 hannken fs->fs_bsize, true, &nbp);
336 1.49 hannken if (error) {
337 1.49 hannken brelse(bp, 0);
338 1.49 hannken goto fail;
339 1.49 hannken }
340 1.52 ad /*
341 1.52 ad * Write synchronously so that indirect blocks
342 1.52 ad * never point at garbage.
343 1.52 ad */
344 1.52 ad if ((error = bwrite(nbp)) != 0) {
345 1.52 ad brelse(bp, 0);
346 1.52 ad goto fail;
347 1.1 mycroft }
348 1.18 mycroft if (unwindidx < 0)
349 1.18 mycroft unwindidx = i - 1;
350 1.33 fvdl bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
351 1.29 chs
352 1.1 mycroft /*
353 1.1 mycroft * If required, write synchronously, otherwise use
354 1.1 mycroft * delayed write.
355 1.1 mycroft */
356 1.29 chs
357 1.1 mycroft if (flags & B_SYNC) {
358 1.1 mycroft bwrite(bp);
359 1.1 mycroft } else {
360 1.1 mycroft bdwrite(bp);
361 1.1 mycroft }
362 1.1 mycroft }
363 1.29 chs
364 1.35 hannken if (flags & B_METAONLY) {
365 1.41 hannken KASSERT(bpp != NULL);
366 1.35 hannken *bpp = bp;
367 1.35 hannken return (0);
368 1.35 hannken }
369 1.35 hannken
370 1.1 mycroft /*
371 1.1 mycroft * Get the data block, allocating if necessary.
372 1.1 mycroft */
373 1.29 chs
374 1.1 mycroft if (nb == 0) {
375 1.49 hannken if (fscow_run(bp, true) != 0) {
376 1.49 hannken brelse(bp, 0);
377 1.49 hannken goto fail;
378 1.49 hannken }
379 1.46 ad mutex_enter(&ump->um_lock);
380 1.51 simonb pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, flags,
381 1.51 simonb &bap[0]);
382 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, flags, cred,
383 1.18 mycroft &newb);
384 1.3 christos if (error) {
385 1.46 ad brelse(bp, 0);
386 1.8 fvdl goto fail;
387 1.1 mycroft }
388 1.1 mycroft nb = newb;
389 1.8 fvdl *allocblk++ = nb;
390 1.23 chs if (bpp != NULL) {
391 1.58 dholland error = ffs_getblk(vp, lbn, FFS_FSBTODB(fs, nb),
392 1.49 hannken fs->fs_bsize, (flags & B_CLRBUF) != 0, bpp);
393 1.49 hannken if (error) {
394 1.49 hannken brelse(bp, 0);
395 1.49 hannken goto fail;
396 1.49 hannken }
397 1.23 chs }
398 1.33 fvdl bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
399 1.23 chs if (allocib == NULL && unwindidx < 0) {
400 1.23 chs unwindidx = i - 1;
401 1.23 chs }
402 1.29 chs
403 1.1 mycroft /*
404 1.1 mycroft * If required, write synchronously, otherwise use
405 1.1 mycroft * delayed write.
406 1.1 mycroft */
407 1.29 chs
408 1.1 mycroft if (flags & B_SYNC) {
409 1.1 mycroft bwrite(bp);
410 1.1 mycroft } else {
411 1.1 mycroft bdwrite(bp);
412 1.1 mycroft }
413 1.1 mycroft return (0);
414 1.1 mycroft }
415 1.46 ad brelse(bp, 0);
416 1.23 chs if (bpp != NULL) {
417 1.23 chs if (flags & B_CLRBUF) {
418 1.49 hannken error = bread(vp, lbn, (int)fs->fs_bsize,
419 1.61 maxv B_MODIFY, &nbp);
420 1.23 chs if (error) {
421 1.23 chs goto fail;
422 1.23 chs }
423 1.23 chs } else {
424 1.58 dholland error = ffs_getblk(vp, lbn, FFS_FSBTODB(fs, nb),
425 1.49 hannken fs->fs_bsize, true, &nbp);
426 1.49 hannken if (error)
427 1.49 hannken goto fail;
428 1.1 mycroft }
429 1.23 chs *bpp = nbp;
430 1.1 mycroft }
431 1.1 mycroft return (0);
432 1.27 chs
433 1.8 fvdl fail:
434 1.27 chs /*
435 1.29 chs * If we have failed part way through block allocation, we
436 1.29 chs * have to deallocate any indirect blocks that we have allocated.
437 1.27 chs */
438 1.27 chs
439 1.29 chs if (unwindidx >= 0) {
440 1.27 chs
441 1.29 chs /*
442 1.29 chs * First write out any buffers we've created to resolve their
443 1.29 chs * softdeps. This must be done in reverse order of creation
444 1.29 chs * so that we resolve the dependencies in one pass.
445 1.29 chs * Write the cylinder group buffers for these buffers too.
446 1.29 chs */
447 1.29 chs
448 1.29 chs for (i = num; i >= unwindidx; i--) {
449 1.29 chs if (i == 0) {
450 1.29 chs break;
451 1.29 chs }
452 1.50 hannken if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
453 1.50 hannken fs->fs_bsize, false, &bp) != 0)
454 1.50 hannken continue;
455 1.48 ad if (bp->b_oflags & BO_DELWRI) {
456 1.58 dholland nb = FFS_FSBTODB(fs, cgtod(fs, dtog(fs,
457 1.58 dholland FFS_DBTOFSB(fs, bp->b_blkno))));
458 1.29 chs bwrite(bp);
459 1.50 hannken if (ffs_getblk(ip->i_devvp, nb, FFS_NOBLK,
460 1.50 hannken fs->fs_cgsize, false, &bp) != 0)
461 1.50 hannken continue;
462 1.48 ad if (bp->b_oflags & BO_DELWRI) {
463 1.29 chs bwrite(bp);
464 1.29 chs } else {
465 1.46 ad brelse(bp, BC_INVAL);
466 1.29 chs }
467 1.29 chs } else {
468 1.46 ad brelse(bp, BC_INVAL);
469 1.29 chs }
470 1.29 chs }
471 1.47 ad
472 1.29 chs /*
473 1.52 ad * Undo the partial allocation.
474 1.29 chs */
475 1.18 mycroft if (unwindidx == 0) {
476 1.18 mycroft *allocib = 0;
477 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
478 1.17 fvdl } else {
479 1.18 mycroft int r;
480 1.29 chs
481 1.29 chs r = bread(vp, indirs[unwindidx].in_lbn,
482 1.61 maxv (int)fs->fs_bsize, 0, &bp);
483 1.18 mycroft if (r) {
484 1.18 mycroft panic("Could not unwind indirect block, error %d", r);
485 1.18 mycroft } else {
486 1.31 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
487 1.18 mycroft bap[indirs[unwindidx].in_off] = 0;
488 1.29 chs bwrite(bp);
489 1.18 mycroft }
490 1.17 fvdl }
491 1.19 mycroft for (i = unwindidx + 1; i <= num; i++) {
492 1.50 hannken if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
493 1.50 hannken fs->fs_bsize, false, &bp) == 0)
494 1.50 hannken brelse(bp, BC_INVAL);
495 1.19 mycroft }
496 1.17 fvdl }
497 1.29 chs for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
498 1.35 hannken ffs_blkfree(fs, ip->i_devvp, *blkp, fs->fs_bsize, ip->i_number);
499 1.29 chs deallocated += fs->fs_bsize;
500 1.29 chs }
501 1.8 fvdl if (deallocated) {
502 1.53 bouyer #if defined(QUOTA) || defined(QUOTA2)
503 1.8 fvdl /*
504 1.8 fvdl * Restore user's disk quota because allocation failed.
505 1.8 fvdl */
506 1.33 fvdl (void)chkdq(ip, -btodb(deallocated), cred, FORCE);
507 1.33 fvdl #endif
508 1.33 fvdl ip->i_ffs1_blocks -= btodb(deallocated);
509 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
510 1.33 fvdl }
511 1.33 fvdl return (error);
512 1.33 fvdl }
513 1.33 fvdl
514 1.33 fvdl static int
515 1.43 elad ffs_balloc_ufs2(struct vnode *vp, off_t off, int size, kauth_cred_t cred,
516 1.39 yamt int flags, struct buf **bpp)
517 1.33 fvdl {
518 1.33 fvdl daddr_t lbn, lastlbn;
519 1.33 fvdl struct buf *bp, *nbp;
520 1.33 fvdl struct inode *ip = VTOI(vp);
521 1.33 fvdl struct fs *fs = ip->i_fs;
522 1.46 ad struct ufsmount *ump = ip->i_ump;
523 1.56 dholland struct indir indirs[UFS_NIADDR + 2];
524 1.33 fvdl daddr_t newb, pref, nb;
525 1.33 fvdl int64_t *bap;
526 1.33 fvdl int deallocated, osize, nsize, num, i, error;
527 1.56 dholland daddr_t *blkp, *allocblk, allociblk[UFS_NIADDR + 1];
528 1.33 fvdl int64_t *allocib;
529 1.33 fvdl int unwindidx = -1;
530 1.33 fvdl const int needswap = UFS_FSNEEDSWAP(fs);
531 1.33 fvdl UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
532 1.33 fvdl
533 1.59 dholland lbn = ffs_lblkno(fs, off);
534 1.57 dholland size = ffs_blkoff(fs, off) + size;
535 1.33 fvdl if (size > fs->fs_bsize)
536 1.33 fvdl panic("ffs_balloc: blk too big");
537 1.33 fvdl if (bpp != NULL) {
538 1.33 fvdl *bpp = NULL;
539 1.33 fvdl }
540 1.63 pgoyette UVMHIST_LOG(ubchist, "vp %#jx lbn 0x%jx size 0x%jx", (uintptr_t)vp,
541 1.63 pgoyette lbn, size, 0);
542 1.33 fvdl
543 1.33 fvdl if (lbn < 0)
544 1.33 fvdl return (EFBIG);
545 1.33 fvdl
546 1.33 fvdl #ifdef notyet
547 1.33 fvdl /*
548 1.33 fvdl * Check for allocating external data.
549 1.33 fvdl */
550 1.33 fvdl if (flags & IO_EXT) {
551 1.56 dholland if (lbn >= UFS_NXADDR)
552 1.33 fvdl return (EFBIG);
553 1.33 fvdl /*
554 1.33 fvdl * If the next write will extend the data into a new block,
555 1.33 fvdl * and the data is currently composed of a fragment
556 1.33 fvdl * this fragment has to be extended to be a full block.
557 1.33 fvdl */
558 1.59 dholland lastlbn = ffs_lblkno(fs, dp->di_extsize);
559 1.33 fvdl if (lastlbn < lbn) {
560 1.33 fvdl nb = lastlbn;
561 1.57 dholland osize = ffs_sblksize(fs, dp->di_extsize, nb);
562 1.33 fvdl if (osize < fs->fs_bsize && osize > 0) {
563 1.46 ad mutex_enter(&ump->um_lock);
564 1.33 fvdl error = ffs_realloccg(ip, -1 - nb,
565 1.33 fvdl dp->di_extb[nb],
566 1.33 fvdl ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
567 1.51 simonb flags, &dp->di_extb[0]),
568 1.51 simonb osize,
569 1.33 fvdl (int)fs->fs_bsize, cred, &bp);
570 1.33 fvdl if (error)
571 1.33 fvdl return (error);
572 1.33 fvdl dp->di_extsize = smalllblktosize(fs, nb + 1);
573 1.58 dholland dp->di_extb[nb] = FFS_DBTOFSB(fs, bp->b_blkno);
574 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
575 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
576 1.33 fvdl if (flags & IO_SYNC)
577 1.33 fvdl bwrite(bp);
578 1.33 fvdl else
579 1.33 fvdl bawrite(bp);
580 1.33 fvdl }
581 1.33 fvdl }
582 1.33 fvdl /*
583 1.33 fvdl * All blocks are direct blocks
584 1.33 fvdl */
585 1.33 fvdl if (flags & BA_METAONLY)
586 1.33 fvdl panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
587 1.33 fvdl nb = dp->di_extb[lbn];
588 1.33 fvdl if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
589 1.49 hannken error = bread(vp, -1 - lbn, fs->fs_bsize,
590 1.61 maxv 0, &bp);
591 1.33 fvdl if (error) {
592 1.33 fvdl return (error);
593 1.33 fvdl }
594 1.48 ad mutex_enter(&bp->b_interlock);
595 1.58 dholland bp->b_blkno = FFS_FSBTODB(fs, nb);
596 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
597 1.48 ad mutex_exit(&bp->b_interlock);
598 1.33 fvdl *bpp = bp;
599 1.33 fvdl return (0);
600 1.33 fvdl }
601 1.33 fvdl if (nb != 0) {
602 1.33 fvdl /*
603 1.33 fvdl * Consider need to reallocate a fragment.
604 1.33 fvdl */
605 1.59 dholland osize = ffs_fragroundup(fs, ffs_blkoff(fs, dp->di_extsize));
606 1.59 dholland nsize = ffs_fragroundup(fs, size);
607 1.33 fvdl if (nsize <= osize) {
608 1.49 hannken error = bread(vp, -1 - lbn, osize,
609 1.61 maxv 0, &bp);
610 1.33 fvdl if (error) {
611 1.33 fvdl return (error);
612 1.33 fvdl }
613 1.46 ad mutex_enter(&bp->b_interlock);
614 1.58 dholland bp->b_blkno = FFS_FSBTODB(fs, nb);
615 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
616 1.46 ad mutex_exit(&bp->b_interlock);
617 1.33 fvdl } else {
618 1.46 ad mutex_enter(&ump->um_lock);
619 1.33 fvdl error = ffs_realloccg(ip, -1 - lbn,
620 1.33 fvdl dp->di_extb[lbn],
621 1.51 simonb ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
622 1.51 simonb &dp->di_extb[0]),
623 1.51 simonb osize, nsize, cred, &bp);
624 1.33 fvdl if (error)
625 1.33 fvdl return (error);
626 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
627 1.33 fvdl }
628 1.33 fvdl } else {
629 1.33 fvdl if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
630 1.59 dholland nsize = ffs_fragroundup(fs, size);
631 1.33 fvdl else
632 1.33 fvdl nsize = fs->fs_bsize;
633 1.46 ad mutex_enter(&ump->um_lock);
634 1.33 fvdl error = ffs_alloc(ip, lbn,
635 1.51 simonb ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
636 1.51 simonb &dp->di_extb[0]),
637 1.51 simonb nsize, flags, cred, &newb);
638 1.33 fvdl if (error)
639 1.33 fvdl return (error);
640 1.58 dholland error = ffs_getblk(vp, -1 - lbn, FFS_FSBTODB(fs, newb),
641 1.62 jdolecek nsize, (flags & B_CLRBUF) != 0, &bp);
642 1.50 hannken if (error)
643 1.50 hannken return error;
644 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
645 1.33 fvdl }
646 1.58 dholland dp->di_extb[lbn] = FFS_DBTOFSB(fs, bp->b_blkno);
647 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
648 1.33 fvdl *bpp = bp;
649 1.33 fvdl return (0);
650 1.33 fvdl }
651 1.33 fvdl #endif
652 1.33 fvdl /*
653 1.33 fvdl * If the next write will extend the file into a new block,
654 1.33 fvdl * and the file is currently composed of a fragment
655 1.33 fvdl * this fragment has to be extended to be a full block.
656 1.33 fvdl */
657 1.33 fvdl
658 1.59 dholland lastlbn = ffs_lblkno(fs, ip->i_size);
659 1.56 dholland if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
660 1.33 fvdl nb = lastlbn;
661 1.57 dholland osize = ffs_blksize(fs, ip, nb);
662 1.33 fvdl if (osize < fs->fs_bsize && osize > 0) {
663 1.46 ad mutex_enter(&ump->um_lock);
664 1.33 fvdl error = ffs_realloccg(ip, nb,
665 1.51 simonb ffs_blkpref_ufs2(ip, lastlbn, nb, flags,
666 1.33 fvdl &ip->i_ffs2_db[0]),
667 1.33 fvdl osize, (int)fs->fs_bsize, cred, bpp, &newb);
668 1.33 fvdl if (error)
669 1.33 fvdl return (error);
670 1.59 dholland ip->i_size = ffs_lblktosize(fs, nb + 1);
671 1.33 fvdl ip->i_ffs2_size = ip->i_size;
672 1.33 fvdl uvm_vnp_setsize(vp, ip->i_size);
673 1.33 fvdl ip->i_ffs2_db[nb] = ufs_rw64(newb, needswap);
674 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
675 1.33 fvdl if (bpp) {
676 1.33 fvdl if (flags & B_SYNC)
677 1.33 fvdl bwrite(*bpp);
678 1.33 fvdl else
679 1.33 fvdl bawrite(*bpp);
680 1.33 fvdl }
681 1.33 fvdl }
682 1.33 fvdl }
683 1.33 fvdl
684 1.33 fvdl /*
685 1.56 dholland * The first UFS_NDADDR blocks are direct blocks
686 1.33 fvdl */
687 1.33 fvdl
688 1.56 dholland if (lbn < UFS_NDADDR) {
689 1.33 fvdl nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
690 1.59 dholland if (nb != 0 && ip->i_size >= ffs_lblktosize(fs, lbn + 1)) {
691 1.33 fvdl
692 1.33 fvdl /*
693 1.33 fvdl * The block is an already-allocated direct block
694 1.33 fvdl * and the file already extends past this block,
695 1.33 fvdl * thus this must be a whole block.
696 1.33 fvdl * Just read the block (if requested).
697 1.33 fvdl */
698 1.33 fvdl
699 1.33 fvdl if (bpp != NULL) {
700 1.61 maxv error = bread(vp, lbn, fs->fs_bsize,
701 1.49 hannken B_MODIFY, bpp);
702 1.33 fvdl if (error) {
703 1.33 fvdl return (error);
704 1.33 fvdl }
705 1.33 fvdl }
706 1.33 fvdl return (0);
707 1.33 fvdl }
708 1.33 fvdl if (nb != 0) {
709 1.33 fvdl
710 1.33 fvdl /*
711 1.33 fvdl * Consider need to reallocate a fragment.
712 1.33 fvdl */
713 1.33 fvdl
714 1.59 dholland osize = ffs_fragroundup(fs, ffs_blkoff(fs, ip->i_size));
715 1.59 dholland nsize = ffs_fragroundup(fs, size);
716 1.33 fvdl if (nsize <= osize) {
717 1.33 fvdl
718 1.33 fvdl /*
719 1.33 fvdl * The existing block is already
720 1.33 fvdl * at least as big as we want.
721 1.33 fvdl * Just read the block (if requested).
722 1.33 fvdl */
723 1.33 fvdl
724 1.33 fvdl if (bpp != NULL) {
725 1.61 maxv error = bread(vp, lbn, osize,
726 1.49 hannken B_MODIFY, bpp);
727 1.33 fvdl if (error) {
728 1.33 fvdl return (error);
729 1.33 fvdl }
730 1.33 fvdl }
731 1.33 fvdl return 0;
732 1.33 fvdl } else {
733 1.33 fvdl
734 1.33 fvdl /*
735 1.33 fvdl * The existing block is smaller than we want,
736 1.33 fvdl * grow it.
737 1.33 fvdl */
738 1.46 ad mutex_enter(&ump->um_lock);
739 1.33 fvdl error = ffs_realloccg(ip, lbn,
740 1.51 simonb ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
741 1.51 simonb &ip->i_ffs2_db[0]),
742 1.51 simonb osize, nsize, cred, bpp, &newb);
743 1.33 fvdl if (error)
744 1.33 fvdl return (error);
745 1.33 fvdl }
746 1.33 fvdl } else {
747 1.33 fvdl
748 1.33 fvdl /*
749 1.33 fvdl * the block was not previously allocated,
750 1.33 fvdl * allocate a new block or fragment.
751 1.33 fvdl */
752 1.33 fvdl
753 1.59 dholland if (ip->i_size < ffs_lblktosize(fs, lbn + 1))
754 1.59 dholland nsize = ffs_fragroundup(fs, size);
755 1.33 fvdl else
756 1.33 fvdl nsize = fs->fs_bsize;
757 1.46 ad mutex_enter(&ump->um_lock);
758 1.33 fvdl error = ffs_alloc(ip, lbn,
759 1.51 simonb ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
760 1.51 simonb &ip->i_ffs2_db[0]),
761 1.51 simonb nsize, flags, cred, &newb);
762 1.33 fvdl if (error)
763 1.33 fvdl return (error);
764 1.33 fvdl if (bpp != NULL) {
765 1.58 dholland error = ffs_getblk(vp, lbn, FFS_FSBTODB(fs, newb),
766 1.49 hannken nsize, (flags & B_CLRBUF) != 0, bpp);
767 1.49 hannken if (error)
768 1.49 hannken return error;
769 1.33 fvdl }
770 1.33 fvdl }
771 1.33 fvdl ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
772 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
773 1.33 fvdl return (0);
774 1.33 fvdl }
775 1.33 fvdl
776 1.33 fvdl /*
777 1.33 fvdl * Determine the number of levels of indirection.
778 1.33 fvdl */
779 1.33 fvdl
780 1.33 fvdl pref = 0;
781 1.33 fvdl if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
782 1.33 fvdl return (error);
783 1.33 fvdl
784 1.33 fvdl /*
785 1.33 fvdl * Fetch the first indirect block allocating if necessary.
786 1.33 fvdl */
787 1.33 fvdl
788 1.33 fvdl --num;
789 1.33 fvdl nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
790 1.33 fvdl allocib = NULL;
791 1.33 fvdl allocblk = allociblk;
792 1.33 fvdl if (nb == 0) {
793 1.46 ad mutex_enter(&ump->um_lock);
794 1.51 simonb pref = ffs_blkpref_ufs2(ip, lbn, 0, flags | B_METAONLY, NULL);
795 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
796 1.51 simonb flags | B_METAONLY, cred, &newb);
797 1.33 fvdl if (error)
798 1.33 fvdl goto fail;
799 1.33 fvdl nb = newb;
800 1.33 fvdl *allocblk++ = nb;
801 1.58 dholland error = ffs_getblk(vp, indirs[1].in_lbn, FFS_FSBTODB(fs, nb),
802 1.49 hannken fs->fs_bsize, true, &bp);
803 1.49 hannken if (error)
804 1.49 hannken goto fail;
805 1.52 ad /*
806 1.52 ad * Write synchronously so that indirect blocks
807 1.52 ad * never point at garbage.
808 1.52 ad */
809 1.52 ad if ((error = bwrite(bp)) != 0)
810 1.52 ad goto fail;
811 1.33 fvdl unwindidx = 0;
812 1.33 fvdl allocib = &ip->i_ffs2_ib[indirs[0].in_off];
813 1.33 fvdl *allocib = ufs_rw64(nb, needswap);
814 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
815 1.33 fvdl }
816 1.33 fvdl
817 1.33 fvdl /*
818 1.33 fvdl * Fetch through the indirect blocks, allocating as necessary.
819 1.33 fvdl */
820 1.33 fvdl
821 1.33 fvdl for (i = 1;;) {
822 1.33 fvdl error = bread(vp,
823 1.61 maxv indirs[i].in_lbn, (int)fs->fs_bsize, 0, &bp);
824 1.33 fvdl if (error) {
825 1.33 fvdl goto fail;
826 1.33 fvdl }
827 1.33 fvdl bap = (int64_t *)bp->b_data;
828 1.33 fvdl nb = ufs_rw64(bap[indirs[i].in_off], needswap);
829 1.33 fvdl if (i == num)
830 1.33 fvdl break;
831 1.33 fvdl i++;
832 1.33 fvdl if (nb != 0) {
833 1.46 ad brelse(bp, 0);
834 1.33 fvdl continue;
835 1.33 fvdl }
836 1.49 hannken if (fscow_run(bp, true) != 0) {
837 1.49 hannken brelse(bp, 0);
838 1.49 hannken goto fail;
839 1.49 hannken }
840 1.46 ad mutex_enter(&ump->um_lock);
841 1.54 hannken /* Try to keep snapshot indirect blocks contiguous. */
842 1.54 hannken if (i == num && (ip->i_flags & SF_SNAPSHOT) != 0)
843 1.54 hannken pref = ffs_blkpref_ufs2(ip, lbn, indirs[i-1].in_off,
844 1.54 hannken flags | B_METAONLY, &bap[0]);
845 1.33 fvdl if (pref == 0)
846 1.51 simonb pref = ffs_blkpref_ufs2(ip, lbn, 0, flags | B_METAONLY,
847 1.51 simonb NULL);
848 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
849 1.51 simonb flags | B_METAONLY, cred, &newb);
850 1.33 fvdl if (error) {
851 1.46 ad brelse(bp, 0);
852 1.33 fvdl goto fail;
853 1.33 fvdl }
854 1.33 fvdl nb = newb;
855 1.33 fvdl *allocblk++ = nb;
856 1.58 dholland error = ffs_getblk(vp, indirs[i].in_lbn, FFS_FSBTODB(fs, nb),
857 1.49 hannken fs->fs_bsize, true, &nbp);
858 1.49 hannken if (error) {
859 1.49 hannken brelse(bp, 0);
860 1.49 hannken goto fail;
861 1.49 hannken }
862 1.52 ad /*
863 1.52 ad * Write synchronously so that indirect blocks
864 1.52 ad * never point at garbage.
865 1.52 ad */
866 1.52 ad if ((error = bwrite(nbp)) != 0) {
867 1.52 ad brelse(bp, 0);
868 1.52 ad goto fail;
869 1.33 fvdl }
870 1.33 fvdl if (unwindidx < 0)
871 1.33 fvdl unwindidx = i - 1;
872 1.33 fvdl bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
873 1.33 fvdl
874 1.33 fvdl /*
875 1.33 fvdl * If required, write synchronously, otherwise use
876 1.33 fvdl * delayed write.
877 1.33 fvdl */
878 1.33 fvdl
879 1.33 fvdl if (flags & B_SYNC) {
880 1.33 fvdl bwrite(bp);
881 1.33 fvdl } else {
882 1.33 fvdl bdwrite(bp);
883 1.33 fvdl }
884 1.33 fvdl }
885 1.33 fvdl
886 1.35 hannken if (flags & B_METAONLY) {
887 1.41 hannken KASSERT(bpp != NULL);
888 1.35 hannken *bpp = bp;
889 1.35 hannken return (0);
890 1.35 hannken }
891 1.35 hannken
892 1.33 fvdl /*
893 1.33 fvdl * Get the data block, allocating if necessary.
894 1.33 fvdl */
895 1.33 fvdl
896 1.33 fvdl if (nb == 0) {
897 1.49 hannken if (fscow_run(bp, true) != 0) {
898 1.49 hannken brelse(bp, 0);
899 1.49 hannken goto fail;
900 1.49 hannken }
901 1.46 ad mutex_enter(&ump->um_lock);
902 1.51 simonb pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, flags,
903 1.51 simonb &bap[0]);
904 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, flags, cred,
905 1.33 fvdl &newb);
906 1.33 fvdl if (error) {
907 1.46 ad brelse(bp, 0);
908 1.33 fvdl goto fail;
909 1.33 fvdl }
910 1.33 fvdl nb = newb;
911 1.33 fvdl *allocblk++ = nb;
912 1.33 fvdl if (bpp != NULL) {
913 1.58 dholland error = ffs_getblk(vp, lbn, FFS_FSBTODB(fs, nb),
914 1.49 hannken fs->fs_bsize, (flags & B_CLRBUF) != 0, bpp);
915 1.49 hannken if (error) {
916 1.49 hannken brelse(bp, 0);
917 1.49 hannken goto fail;
918 1.49 hannken }
919 1.33 fvdl }
920 1.33 fvdl bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
921 1.33 fvdl if (allocib == NULL && unwindidx < 0) {
922 1.33 fvdl unwindidx = i - 1;
923 1.33 fvdl }
924 1.33 fvdl
925 1.33 fvdl /*
926 1.33 fvdl * If required, write synchronously, otherwise use
927 1.33 fvdl * delayed write.
928 1.33 fvdl */
929 1.33 fvdl
930 1.33 fvdl if (flags & B_SYNC) {
931 1.33 fvdl bwrite(bp);
932 1.33 fvdl } else {
933 1.33 fvdl bdwrite(bp);
934 1.33 fvdl }
935 1.33 fvdl return (0);
936 1.33 fvdl }
937 1.46 ad brelse(bp, 0);
938 1.33 fvdl if (bpp != NULL) {
939 1.33 fvdl if (flags & B_CLRBUF) {
940 1.49 hannken error = bread(vp, lbn, (int)fs->fs_bsize,
941 1.61 maxv B_MODIFY, &nbp);
942 1.33 fvdl if (error) {
943 1.33 fvdl goto fail;
944 1.33 fvdl }
945 1.33 fvdl } else {
946 1.58 dholland error = ffs_getblk(vp, lbn, FFS_FSBTODB(fs, nb),
947 1.49 hannken fs->fs_bsize, true, &nbp);
948 1.49 hannken if (error)
949 1.49 hannken goto fail;
950 1.33 fvdl }
951 1.33 fvdl *bpp = nbp;
952 1.33 fvdl }
953 1.33 fvdl return (0);
954 1.33 fvdl
955 1.33 fvdl fail:
956 1.33 fvdl /*
957 1.33 fvdl * If we have failed part way through block allocation, we
958 1.33 fvdl * have to deallocate any indirect blocks that we have allocated.
959 1.33 fvdl */
960 1.33 fvdl
961 1.33 fvdl if (unwindidx >= 0) {
962 1.33 fvdl
963 1.33 fvdl /*
964 1.33 fvdl * First write out any buffers we've created to resolve their
965 1.33 fvdl * softdeps. This must be done in reverse order of creation
966 1.33 fvdl * so that we resolve the dependencies in one pass.
967 1.33 fvdl * Write the cylinder group buffers for these buffers too.
968 1.33 fvdl */
969 1.33 fvdl
970 1.33 fvdl for (i = num; i >= unwindidx; i--) {
971 1.33 fvdl if (i == 0) {
972 1.33 fvdl break;
973 1.33 fvdl }
974 1.50 hannken if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
975 1.50 hannken fs->fs_bsize, false, &bp) != 0)
976 1.50 hannken continue;
977 1.48 ad if (bp->b_oflags & BO_DELWRI) {
978 1.58 dholland nb = FFS_FSBTODB(fs, cgtod(fs, dtog(fs,
979 1.58 dholland FFS_DBTOFSB(fs, bp->b_blkno))));
980 1.33 fvdl bwrite(bp);
981 1.50 hannken if (ffs_getblk(ip->i_devvp, nb, FFS_NOBLK,
982 1.50 hannken fs->fs_cgsize, false, &bp) != 0)
983 1.50 hannken continue;
984 1.48 ad if (bp->b_oflags & BO_DELWRI) {
985 1.33 fvdl bwrite(bp);
986 1.33 fvdl } else {
987 1.46 ad brelse(bp, BC_INVAL);
988 1.33 fvdl }
989 1.33 fvdl } else {
990 1.46 ad brelse(bp, BC_INVAL);
991 1.33 fvdl }
992 1.33 fvdl }
993 1.47 ad
994 1.33 fvdl /*
995 1.33 fvdl * Now that any dependencies that we created have been
996 1.33 fvdl * resolved, we can undo the partial allocation.
997 1.33 fvdl */
998 1.33 fvdl
999 1.33 fvdl if (unwindidx == 0) {
1000 1.33 fvdl *allocib = 0;
1001 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
1002 1.33 fvdl } else {
1003 1.33 fvdl int r;
1004 1.33 fvdl
1005 1.33 fvdl r = bread(vp, indirs[unwindidx].in_lbn,
1006 1.61 maxv (int)fs->fs_bsize, 0, &bp);
1007 1.33 fvdl if (r) {
1008 1.33 fvdl panic("Could not unwind indirect block, error %d", r);
1009 1.33 fvdl } else {
1010 1.33 fvdl bap = (int64_t *)bp->b_data;
1011 1.33 fvdl bap[indirs[unwindidx].in_off] = 0;
1012 1.33 fvdl bwrite(bp);
1013 1.33 fvdl }
1014 1.33 fvdl }
1015 1.33 fvdl for (i = unwindidx + 1; i <= num; i++) {
1016 1.50 hannken if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
1017 1.50 hannken fs->fs_bsize, false, &bp) == 0)
1018 1.50 hannken brelse(bp, BC_INVAL);
1019 1.33 fvdl }
1020 1.33 fvdl }
1021 1.33 fvdl for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
1022 1.35 hannken ffs_blkfree(fs, ip->i_devvp, *blkp, fs->fs_bsize, ip->i_number);
1023 1.33 fvdl deallocated += fs->fs_bsize;
1024 1.33 fvdl }
1025 1.33 fvdl if (deallocated) {
1026 1.53 bouyer #if defined(QUOTA) || defined(QUOTA2)
1027 1.33 fvdl /*
1028 1.33 fvdl * Restore user's disk quota because allocation failed.
1029 1.33 fvdl */
1030 1.33 fvdl (void)chkdq(ip, -btodb(deallocated), cred, FORCE);
1031 1.8 fvdl #endif
1032 1.33 fvdl ip->i_ffs2_blocks -= btodb(deallocated);
1033 1.13 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
1034 1.8 fvdl }
1035 1.47 ad
1036 1.8 fvdl return (error);
1037 1.1 mycroft }
1038