ffs_balloc.c revision 1.49 1 1.49 hannken /* $NetBSD: ffs_balloc.c,v 1.49 2008/05/16 09:22:00 hannken 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.49 hannken __KERNEL_RCSID(0, "$NetBSD: ffs_balloc.c,v 1.49 2008/05/16 09:22:00 hannken 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.49 hannken static int ffs_getblk(struct vnode *, daddr_t, daddr_t, int, bool, buf_t **);
71 1.43 elad static int ffs_balloc_ufs1(struct vnode *, off_t, int, kauth_cred_t, int,
72 1.39 yamt struct buf **);
73 1.43 elad static int ffs_balloc_ufs2(struct vnode *, off_t, int, kauth_cred_t, int,
74 1.39 yamt struct buf **);
75 1.33 fvdl
76 1.1 mycroft /*
77 1.1 mycroft * Balloc defines the structure of file system storage
78 1.1 mycroft * by allocating the physical blocks on a device given
79 1.1 mycroft * the inode and the logical block number in a file.
80 1.1 mycroft */
81 1.33 fvdl
82 1.3 christos int
83 1.43 elad ffs_balloc(struct vnode *vp, off_t off, int size, kauth_cred_t cred, int flags,
84 1.39 yamt struct buf **bpp)
85 1.15 fvdl {
86 1.49 hannken int error;
87 1.33 fvdl
88 1.39 yamt if (VTOI(vp)->i_fs->fs_magic == FS_UFS2_MAGIC)
89 1.49 hannken error = ffs_balloc_ufs2(vp, off, size, cred, flags, bpp);
90 1.33 fvdl else
91 1.49 hannken error = ffs_balloc_ufs1(vp, off, size, cred, flags, bpp);
92 1.49 hannken
93 1.49 hannken if (error == 0 && bpp != NULL && (error = fscow_run(*bpp, false)) != 0)
94 1.49 hannken brelse(*bpp, 0);
95 1.49 hannken
96 1.49 hannken return error;
97 1.49 hannken }
98 1.49 hannken
99 1.49 hannken static int
100 1.49 hannken ffs_getblk(struct vnode *vp, daddr_t lblkno, daddr_t blkno, int size,
101 1.49 hannken bool clearbuf, buf_t **bpp)
102 1.49 hannken {
103 1.49 hannken int error;
104 1.49 hannken
105 1.49 hannken if ((*bpp = getblk(vp, lblkno, size, 0, 0)) == NULL)
106 1.49 hannken return ENOMEM;
107 1.49 hannken (*bpp)->b_blkno = blkno;
108 1.49 hannken if (clearbuf)
109 1.49 hannken clrbuf(*bpp);
110 1.49 hannken if ((error = fscow_run(*bpp, false)) != 0)
111 1.49 hannken brelse(*bpp, BC_INVAL);
112 1.49 hannken return error;
113 1.33 fvdl }
114 1.33 fvdl
115 1.33 fvdl static int
116 1.43 elad ffs_balloc_ufs1(struct vnode *vp, off_t off, int size, kauth_cred_t cred,
117 1.39 yamt int flags, struct buf **bpp)
118 1.33 fvdl {
119 1.33 fvdl daddr_t lbn, lastlbn;
120 1.1 mycroft struct buf *bp, *nbp;
121 1.15 fvdl struct inode *ip = VTOI(vp);
122 1.15 fvdl struct fs *fs = ip->i_fs;
123 1.46 ad struct ufsmount *ump = ip->i_ump;
124 1.1 mycroft struct indir indirs[NIADDR + 2];
125 1.37 mycroft daddr_t newb, pref, nb;
126 1.31 fvdl int32_t *bap; /* XXX ondisk32 */
127 1.8 fvdl int deallocated, osize, nsize, num, i, error;
128 1.33 fvdl int32_t *blkp, *allocblk, allociblk[NIADDR + 1];
129 1.33 fvdl int32_t *allocib;
130 1.17 fvdl int unwindidx = -1;
131 1.15 fvdl #ifdef FFS_EI
132 1.15 fvdl const int needswap = UFS_FSNEEDSWAP(fs);
133 1.15 fvdl #endif
134 1.23 chs UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
135 1.1 mycroft
136 1.39 yamt lbn = lblkno(fs, off);
137 1.39 yamt size = blkoff(fs, off) + size;
138 1.15 fvdl if (size > fs->fs_bsize)
139 1.15 fvdl panic("ffs_balloc: blk too big");
140 1.23 chs if (bpp != NULL) {
141 1.23 chs *bpp = NULL;
142 1.23 chs }
143 1.23 chs UVMHIST_LOG(ubchist, "vp %p lbn 0x%x size 0x%x", vp, lbn, size,0);
144 1.23 chs
145 1.8 fvdl if (lbn < 0)
146 1.1 mycroft return (EFBIG);
147 1.1 mycroft
148 1.1 mycroft /*
149 1.1 mycroft * If the next write will extend the file into a new block,
150 1.1 mycroft * and the file is currently composed of a fragment
151 1.1 mycroft * this fragment has to be extended to be a full block.
152 1.1 mycroft */
153 1.23 chs
154 1.33 fvdl lastlbn = lblkno(fs, ip->i_size);
155 1.33 fvdl if (lastlbn < NDADDR && lastlbn < lbn) {
156 1.33 fvdl nb = lastlbn;
157 1.1 mycroft osize = blksize(fs, ip, nb);
158 1.1 mycroft if (osize < fs->fs_bsize && osize > 0) {
159 1.46 ad mutex_enter(&ump->um_lock);
160 1.1 mycroft error = ffs_realloccg(ip, nb,
161 1.33 fvdl ffs_blkpref_ufs1(ip, lastlbn, nb,
162 1.33 fvdl &ip->i_ffs1_db[0]),
163 1.33 fvdl osize, (int)fs->fs_bsize, cred, bpp, &newb);
164 1.1 mycroft if (error)
165 1.1 mycroft return (error);
166 1.15 fvdl if (DOINGSOFTDEP(vp))
167 1.23 chs softdep_setup_allocdirect(ip, nb, newb,
168 1.33 fvdl ufs_rw32(ip->i_ffs1_db[nb], needswap),
169 1.23 chs fs->fs_bsize, osize, bpp ? *bpp : NULL);
170 1.33 fvdl ip->i_size = lblktosize(fs, nb + 1);
171 1.33 fvdl ip->i_ffs1_size = ip->i_size;
172 1.33 fvdl uvm_vnp_setsize(vp, ip->i_ffs1_size);
173 1.37 mycroft ip->i_ffs1_db[nb] = ufs_rw32((u_int32_t)newb, needswap);
174 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
175 1.42 christos if (bpp && *bpp) {
176 1.23 chs if (flags & B_SYNC)
177 1.23 chs bwrite(*bpp);
178 1.23 chs else
179 1.23 chs bawrite(*bpp);
180 1.23 chs }
181 1.1 mycroft }
182 1.1 mycroft }
183 1.23 chs
184 1.1 mycroft /*
185 1.1 mycroft * The first NDADDR blocks are direct blocks
186 1.1 mycroft */
187 1.23 chs
188 1.8 fvdl if (lbn < NDADDR) {
189 1.33 fvdl nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
190 1.33 fvdl if (nb != 0 && ip->i_size >= lblktosize(fs, lbn + 1)) {
191 1.23 chs
192 1.23 chs /*
193 1.23 chs * The block is an already-allocated direct block
194 1.23 chs * and the file already extends past this block,
195 1.23 chs * thus this must be a whole block.
196 1.23 chs * Just read the block (if requested).
197 1.23 chs */
198 1.23 chs
199 1.23 chs if (bpp != NULL) {
200 1.23 chs error = bread(vp, lbn, fs->fs_bsize, NOCRED,
201 1.49 hannken B_MODIFY, bpp);
202 1.23 chs if (error) {
203 1.46 ad brelse(*bpp, 0);
204 1.23 chs return (error);
205 1.23 chs }
206 1.1 mycroft }
207 1.1 mycroft return (0);
208 1.1 mycroft }
209 1.1 mycroft if (nb != 0) {
210 1.23 chs
211 1.1 mycroft /*
212 1.1 mycroft * Consider need to reallocate a fragment.
213 1.1 mycroft */
214 1.23 chs
215 1.33 fvdl osize = fragroundup(fs, blkoff(fs, ip->i_size));
216 1.1 mycroft nsize = fragroundup(fs, size);
217 1.1 mycroft if (nsize <= osize) {
218 1.23 chs
219 1.23 chs /*
220 1.23 chs * The existing block is already
221 1.23 chs * at least as big as we want.
222 1.23 chs * Just read the block (if requested).
223 1.23 chs */
224 1.23 chs
225 1.23 chs if (bpp != NULL) {
226 1.23 chs error = bread(vp, lbn, osize, NOCRED,
227 1.49 hannken B_MODIFY, bpp);
228 1.23 chs if (error) {
229 1.46 ad brelse(*bpp, 0);
230 1.23 chs return (error);
231 1.23 chs }
232 1.1 mycroft }
233 1.23 chs return 0;
234 1.1 mycroft } else {
235 1.23 chs
236 1.23 chs /*
237 1.23 chs * The existing block is smaller than we want,
238 1.23 chs * grow it.
239 1.23 chs */
240 1.46 ad mutex_enter(&ump->um_lock);
241 1.8 fvdl error = ffs_realloccg(ip, lbn,
242 1.33 fvdl ffs_blkpref_ufs1(ip, lbn, (int)lbn,
243 1.33 fvdl &ip->i_ffs1_db[0]), osize, nsize, cred,
244 1.23 chs bpp, &newb);
245 1.1 mycroft if (error)
246 1.1 mycroft return (error);
247 1.15 fvdl if (DOINGSOFTDEP(vp))
248 1.15 fvdl softdep_setup_allocdirect(ip, lbn,
249 1.23 chs newb, nb, nsize, osize,
250 1.23 chs bpp ? *bpp : NULL);
251 1.1 mycroft }
252 1.1 mycroft } else {
253 1.23 chs
254 1.23 chs /*
255 1.23 chs * the block was not previously allocated,
256 1.23 chs * allocate a new block or fragment.
257 1.23 chs */
258 1.23 chs
259 1.33 fvdl if (ip->i_size < lblktosize(fs, lbn + 1))
260 1.1 mycroft nsize = fragroundup(fs, size);
261 1.1 mycroft else
262 1.1 mycroft nsize = fs->fs_bsize;
263 1.46 ad mutex_enter(&ump->um_lock);
264 1.8 fvdl error = ffs_alloc(ip, lbn,
265 1.33 fvdl ffs_blkpref_ufs1(ip, lbn, (int)lbn,
266 1.33 fvdl &ip->i_ffs1_db[0]),
267 1.8 fvdl nsize, cred, &newb);
268 1.1 mycroft if (error)
269 1.1 mycroft return (error);
270 1.23 chs if (bpp != NULL) {
271 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, newb),
272 1.49 hannken nsize, (flags & B_CLRBUF) != 0, bpp);
273 1.49 hannken if (error)
274 1.49 hannken return error;
275 1.23 chs }
276 1.23 chs if (DOINGSOFTDEP(vp)) {
277 1.15 fvdl softdep_setup_allocdirect(ip, lbn, newb, 0,
278 1.23 chs nsize, 0, bpp ? *bpp : NULL);
279 1.23 chs }
280 1.1 mycroft }
281 1.37 mycroft ip->i_ffs1_db[lbn] = ufs_rw32((u_int32_t)newb, needswap);
282 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
283 1.1 mycroft return (0);
284 1.1 mycroft }
285 1.29 chs
286 1.1 mycroft /*
287 1.1 mycroft * Determine the number of levels of indirection.
288 1.1 mycroft */
289 1.29 chs
290 1.1 mycroft pref = 0;
291 1.8 fvdl if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
292 1.29 chs return (error);
293 1.23 chs
294 1.1 mycroft /*
295 1.1 mycroft * Fetch the first indirect block allocating if necessary.
296 1.1 mycroft */
297 1.29 chs
298 1.1 mycroft --num;
299 1.33 fvdl nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
300 1.8 fvdl allocib = NULL;
301 1.8 fvdl allocblk = allociblk;
302 1.1 mycroft if (nb == 0) {
303 1.46 ad mutex_enter(&ump->um_lock);
304 1.33 fvdl pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
305 1.18 mycroft error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
306 1.18 mycroft &newb);
307 1.3 christos if (error)
308 1.27 chs goto fail;
309 1.1 mycroft nb = newb;
310 1.8 fvdl *allocblk++ = nb;
311 1.49 hannken error = ffs_getblk(vp, indirs[1].in_lbn, fsbtodb(fs, nb),
312 1.49 hannken fs->fs_bsize, true, &bp);
313 1.49 hannken if (error)
314 1.49 hannken goto fail;
315 1.15 fvdl if (DOINGSOFTDEP(vp)) {
316 1.15 fvdl softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
317 1.15 fvdl newb, 0, fs->fs_bsize, 0, bp);
318 1.15 fvdl bdwrite(bp);
319 1.15 fvdl } else {
320 1.29 chs
321 1.15 fvdl /*
322 1.15 fvdl * Write synchronously so that indirect blocks
323 1.15 fvdl * never point at garbage.
324 1.15 fvdl */
325 1.29 chs
326 1.15 fvdl if ((error = bwrite(bp)) != 0)
327 1.15 fvdl goto fail;
328 1.15 fvdl }
329 1.18 mycroft unwindidx = 0;
330 1.33 fvdl allocib = &ip->i_ffs1_ib[indirs[0].in_off];
331 1.33 fvdl *allocib = ufs_rw32(nb, needswap);
332 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
333 1.1 mycroft }
334 1.29 chs
335 1.1 mycroft /*
336 1.1 mycroft * Fetch through the indirect blocks, allocating as necessary.
337 1.1 mycroft */
338 1.29 chs
339 1.1 mycroft for (i = 1;;) {
340 1.1 mycroft error = bread(vp,
341 1.49 hannken indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, 0, &bp);
342 1.1 mycroft if (error) {
343 1.46 ad brelse(bp, 0);
344 1.8 fvdl goto fail;
345 1.1 mycroft }
346 1.31 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
347 1.15 fvdl nb = ufs_rw32(bap[indirs[i].in_off], needswap);
348 1.1 mycroft if (i == num)
349 1.1 mycroft break;
350 1.18 mycroft i++;
351 1.1 mycroft if (nb != 0) {
352 1.46 ad brelse(bp, 0);
353 1.1 mycroft continue;
354 1.1 mycroft }
355 1.49 hannken if (fscow_run(bp, true) != 0) {
356 1.49 hannken brelse(bp, 0);
357 1.49 hannken goto fail;
358 1.49 hannken }
359 1.46 ad mutex_enter(&ump->um_lock);
360 1.1 mycroft if (pref == 0)
361 1.33 fvdl pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
362 1.3 christos error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
363 1.18 mycroft &newb);
364 1.3 christos if (error) {
365 1.46 ad brelse(bp, 0);
366 1.8 fvdl goto fail;
367 1.1 mycroft }
368 1.1 mycroft nb = newb;
369 1.8 fvdl *allocblk++ = nb;
370 1.49 hannken error = ffs_getblk(vp, indirs[i].in_lbn, fsbtodb(fs, nb),
371 1.49 hannken fs->fs_bsize, true, &nbp);
372 1.49 hannken if (error) {
373 1.49 hannken brelse(bp, 0);
374 1.49 hannken goto fail;
375 1.49 hannken }
376 1.15 fvdl if (DOINGSOFTDEP(vp)) {
377 1.15 fvdl softdep_setup_allocindir_meta(nbp, ip, bp,
378 1.15 fvdl indirs[i - 1].in_off, nb);
379 1.15 fvdl bdwrite(nbp);
380 1.15 fvdl } else {
381 1.29 chs
382 1.15 fvdl /*
383 1.15 fvdl * Write synchronously so that indirect blocks
384 1.15 fvdl * never point at garbage.
385 1.15 fvdl */
386 1.29 chs
387 1.15 fvdl if ((error = bwrite(nbp)) != 0) {
388 1.46 ad brelse(bp, 0);
389 1.15 fvdl goto fail;
390 1.15 fvdl }
391 1.1 mycroft }
392 1.18 mycroft if (unwindidx < 0)
393 1.18 mycroft unwindidx = i - 1;
394 1.33 fvdl bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
395 1.29 chs
396 1.1 mycroft /*
397 1.1 mycroft * If required, write synchronously, otherwise use
398 1.1 mycroft * delayed write.
399 1.1 mycroft */
400 1.29 chs
401 1.1 mycroft if (flags & B_SYNC) {
402 1.1 mycroft bwrite(bp);
403 1.1 mycroft } else {
404 1.1 mycroft bdwrite(bp);
405 1.1 mycroft }
406 1.1 mycroft }
407 1.29 chs
408 1.35 hannken if (flags & B_METAONLY) {
409 1.41 hannken KASSERT(bpp != NULL);
410 1.35 hannken *bpp = bp;
411 1.35 hannken return (0);
412 1.35 hannken }
413 1.35 hannken
414 1.1 mycroft /*
415 1.1 mycroft * Get the data block, allocating if necessary.
416 1.1 mycroft */
417 1.29 chs
418 1.1 mycroft if (nb == 0) {
419 1.49 hannken if (fscow_run(bp, true) != 0) {
420 1.49 hannken brelse(bp, 0);
421 1.49 hannken goto fail;
422 1.49 hannken }
423 1.46 ad mutex_enter(&ump->um_lock);
424 1.33 fvdl pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]);
425 1.3 christos error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
426 1.18 mycroft &newb);
427 1.3 christos if (error) {
428 1.46 ad brelse(bp, 0);
429 1.8 fvdl goto fail;
430 1.1 mycroft }
431 1.1 mycroft nb = newb;
432 1.8 fvdl *allocblk++ = nb;
433 1.23 chs if (bpp != NULL) {
434 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, nb),
435 1.49 hannken fs->fs_bsize, (flags & B_CLRBUF) != 0, bpp);
436 1.49 hannken if (error) {
437 1.49 hannken brelse(bp, 0);
438 1.49 hannken goto fail;
439 1.49 hannken }
440 1.23 chs }
441 1.15 fvdl if (DOINGSOFTDEP(vp))
442 1.15 fvdl softdep_setup_allocindir_page(ip, lbn, bp,
443 1.23 chs indirs[num].in_off, nb, 0, bpp ? *bpp : NULL);
444 1.33 fvdl bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
445 1.23 chs if (allocib == NULL && unwindidx < 0) {
446 1.23 chs unwindidx = i - 1;
447 1.23 chs }
448 1.29 chs
449 1.1 mycroft /*
450 1.1 mycroft * If required, write synchronously, otherwise use
451 1.1 mycroft * delayed write.
452 1.1 mycroft */
453 1.29 chs
454 1.1 mycroft if (flags & B_SYNC) {
455 1.1 mycroft bwrite(bp);
456 1.1 mycroft } else {
457 1.1 mycroft bdwrite(bp);
458 1.1 mycroft }
459 1.1 mycroft return (0);
460 1.1 mycroft }
461 1.46 ad brelse(bp, 0);
462 1.23 chs if (bpp != NULL) {
463 1.23 chs if (flags & B_CLRBUF) {
464 1.49 hannken error = bread(vp, lbn, (int)fs->fs_bsize,
465 1.49 hannken NOCRED, B_MODIFY, &nbp);
466 1.23 chs if (error) {
467 1.46 ad brelse(nbp, 0);
468 1.23 chs goto fail;
469 1.23 chs }
470 1.23 chs } else {
471 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, nb),
472 1.49 hannken fs->fs_bsize, true, &nbp);
473 1.49 hannken if (error)
474 1.49 hannken goto fail;
475 1.1 mycroft }
476 1.23 chs *bpp = nbp;
477 1.1 mycroft }
478 1.1 mycroft return (0);
479 1.27 chs
480 1.8 fvdl fail:
481 1.27 chs /*
482 1.29 chs * If we have failed part way through block allocation, we
483 1.29 chs * have to deallocate any indirect blocks that we have allocated.
484 1.27 chs */
485 1.27 chs
486 1.29 chs if (unwindidx >= 0) {
487 1.27 chs
488 1.29 chs /*
489 1.29 chs * First write out any buffers we've created to resolve their
490 1.29 chs * softdeps. This must be done in reverse order of creation
491 1.29 chs * so that we resolve the dependencies in one pass.
492 1.29 chs * Write the cylinder group buffers for these buffers too.
493 1.29 chs */
494 1.29 chs
495 1.29 chs for (i = num; i >= unwindidx; i--) {
496 1.29 chs if (i == 0) {
497 1.29 chs break;
498 1.29 chs }
499 1.29 chs bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
500 1.29 chs 0);
501 1.48 ad if (bp->b_oflags & BO_DELWRI) {
502 1.29 chs nb = fsbtodb(fs, cgtod(fs, dtog(fs,
503 1.30 chs dbtofsb(fs, bp->b_blkno))));
504 1.29 chs bwrite(bp);
505 1.29 chs bp = getblk(ip->i_devvp, nb, (int)fs->fs_cgsize,
506 1.29 chs 0, 0);
507 1.48 ad if (bp->b_oflags & BO_DELWRI) {
508 1.29 chs bwrite(bp);
509 1.29 chs } else {
510 1.46 ad brelse(bp, BC_INVAL);
511 1.29 chs }
512 1.29 chs } else {
513 1.46 ad brelse(bp, BC_INVAL);
514 1.29 chs }
515 1.29 chs }
516 1.47 ad
517 1.47 ad /* Now flush all dependencies to disk. */
518 1.47 ad #ifdef notyet
519 1.47 ad /* XXX pages locked */
520 1.47 ad (void)softdep_sync_metadata(vp);
521 1.47 ad #endif
522 1.47 ad
523 1.36 mycroft if (DOINGSOFTDEP(vp) && unwindidx == 0) {
524 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
525 1.39 yamt ffs_update(vp, NULL, NULL, UPDATE_WAIT);
526 1.27 chs }
527 1.27 chs
528 1.29 chs /*
529 1.29 chs * Now that any dependencies that we created have been
530 1.29 chs * resolved, we can undo the partial allocation.
531 1.29 chs */
532 1.27 chs
533 1.18 mycroft if (unwindidx == 0) {
534 1.18 mycroft *allocib = 0;
535 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
536 1.36 mycroft if (DOINGSOFTDEP(vp))
537 1.39 yamt ffs_update(vp, NULL, NULL, UPDATE_WAIT);
538 1.17 fvdl } else {
539 1.18 mycroft int r;
540 1.29 chs
541 1.29 chs r = bread(vp, indirs[unwindidx].in_lbn,
542 1.49 hannken (int)fs->fs_bsize, NOCRED, 0, &bp);
543 1.18 mycroft if (r) {
544 1.18 mycroft panic("Could not unwind indirect block, error %d", r);
545 1.46 ad brelse(bp, 0);
546 1.18 mycroft } else {
547 1.31 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
548 1.18 mycroft bap[indirs[unwindidx].in_off] = 0;
549 1.29 chs bwrite(bp);
550 1.18 mycroft }
551 1.17 fvdl }
552 1.19 mycroft for (i = unwindidx + 1; i <= num; i++) {
553 1.19 mycroft bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
554 1.19 mycroft 0);
555 1.46 ad brelse(bp, BC_INVAL);
556 1.19 mycroft }
557 1.17 fvdl }
558 1.29 chs for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
559 1.35 hannken ffs_blkfree(fs, ip->i_devvp, *blkp, fs->fs_bsize, ip->i_number);
560 1.29 chs deallocated += fs->fs_bsize;
561 1.29 chs }
562 1.8 fvdl if (deallocated) {
563 1.8 fvdl #ifdef QUOTA
564 1.8 fvdl /*
565 1.8 fvdl * Restore user's disk quota because allocation failed.
566 1.8 fvdl */
567 1.33 fvdl (void)chkdq(ip, -btodb(deallocated), cred, FORCE);
568 1.33 fvdl #endif
569 1.33 fvdl ip->i_ffs1_blocks -= btodb(deallocated);
570 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
571 1.33 fvdl }
572 1.47 ad /*
573 1.47 ad * Flush all dependencies again so that the soft updates code
574 1.47 ad * doesn't find any untracked changes.
575 1.47 ad */
576 1.47 ad #ifdef notyet
577 1.47 ad /* XXX pages locked */
578 1.47 ad (void)softdep_sync_metadata(vp);
579 1.47 ad #endif
580 1.33 fvdl return (error);
581 1.33 fvdl }
582 1.33 fvdl
583 1.33 fvdl static int
584 1.43 elad ffs_balloc_ufs2(struct vnode *vp, off_t off, int size, kauth_cred_t cred,
585 1.39 yamt int flags, struct buf **bpp)
586 1.33 fvdl {
587 1.33 fvdl daddr_t lbn, lastlbn;
588 1.33 fvdl struct buf *bp, *nbp;
589 1.33 fvdl struct inode *ip = VTOI(vp);
590 1.33 fvdl struct fs *fs = ip->i_fs;
591 1.46 ad struct ufsmount *ump = ip->i_ump;
592 1.33 fvdl struct indir indirs[NIADDR + 2];
593 1.33 fvdl daddr_t newb, pref, nb;
594 1.33 fvdl int64_t *bap;
595 1.33 fvdl int deallocated, osize, nsize, num, i, error;
596 1.33 fvdl daddr_t *blkp, *allocblk, allociblk[NIADDR + 1];
597 1.33 fvdl int64_t *allocib;
598 1.33 fvdl int unwindidx = -1;
599 1.33 fvdl #ifdef FFS_EI
600 1.33 fvdl const int needswap = UFS_FSNEEDSWAP(fs);
601 1.33 fvdl #endif
602 1.33 fvdl UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
603 1.33 fvdl
604 1.39 yamt lbn = lblkno(fs, off);
605 1.39 yamt size = blkoff(fs, off) + size;
606 1.33 fvdl if (size > fs->fs_bsize)
607 1.33 fvdl panic("ffs_balloc: blk too big");
608 1.33 fvdl if (bpp != NULL) {
609 1.33 fvdl *bpp = NULL;
610 1.33 fvdl }
611 1.33 fvdl UVMHIST_LOG(ubchist, "vp %p lbn 0x%x size 0x%x", vp, lbn, size,0);
612 1.33 fvdl
613 1.33 fvdl if (lbn < 0)
614 1.33 fvdl return (EFBIG);
615 1.33 fvdl
616 1.33 fvdl #ifdef notyet
617 1.33 fvdl /*
618 1.33 fvdl * Check for allocating external data.
619 1.33 fvdl */
620 1.33 fvdl if (flags & IO_EXT) {
621 1.33 fvdl if (lbn >= NXADDR)
622 1.33 fvdl return (EFBIG);
623 1.33 fvdl /*
624 1.33 fvdl * If the next write will extend the data into a new block,
625 1.33 fvdl * and the data is currently composed of a fragment
626 1.33 fvdl * this fragment has to be extended to be a full block.
627 1.33 fvdl */
628 1.33 fvdl lastlbn = lblkno(fs, dp->di_extsize);
629 1.33 fvdl if (lastlbn < lbn) {
630 1.33 fvdl nb = lastlbn;
631 1.33 fvdl osize = sblksize(fs, dp->di_extsize, nb);
632 1.33 fvdl if (osize < fs->fs_bsize && osize > 0) {
633 1.46 ad mutex_enter(&ump->um_lock);
634 1.33 fvdl error = ffs_realloccg(ip, -1 - nb,
635 1.33 fvdl dp->di_extb[nb],
636 1.33 fvdl ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
637 1.33 fvdl &dp->di_extb[0]), osize,
638 1.33 fvdl (int)fs->fs_bsize, cred, &bp);
639 1.33 fvdl if (error)
640 1.33 fvdl return (error);
641 1.33 fvdl if (DOINGSOFTDEP(vp))
642 1.33 fvdl softdep_setup_allocext(ip, nb,
643 1.33 fvdl dbtofsb(fs, bp->b_blkno),
644 1.33 fvdl dp->di_extb[nb],
645 1.33 fvdl fs->fs_bsize, osize, bp);
646 1.33 fvdl dp->di_extsize = smalllblktosize(fs, nb + 1);
647 1.33 fvdl dp->di_extb[nb] = dbtofsb(fs, bp->b_blkno);
648 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
649 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
650 1.33 fvdl if (flags & IO_SYNC)
651 1.33 fvdl bwrite(bp);
652 1.33 fvdl else
653 1.33 fvdl bawrite(bp);
654 1.33 fvdl }
655 1.33 fvdl }
656 1.33 fvdl /*
657 1.33 fvdl * All blocks are direct blocks
658 1.33 fvdl */
659 1.33 fvdl if (flags & BA_METAONLY)
660 1.33 fvdl panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
661 1.33 fvdl nb = dp->di_extb[lbn];
662 1.33 fvdl if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
663 1.49 hannken error = bread(vp, -1 - lbn, fs->fs_bsize,
664 1.49 hannken NOCRED, 0, &bp);
665 1.33 fvdl if (error) {
666 1.46 ad brelse(bp, 0);
667 1.33 fvdl return (error);
668 1.33 fvdl }
669 1.48 ad mutex_enter(&bp->b_interlock);
670 1.33 fvdl bp->b_blkno = fsbtodb(fs, nb);
671 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
672 1.48 ad mutex_exit(&bp->b_interlock);
673 1.33 fvdl *bpp = bp;
674 1.33 fvdl return (0);
675 1.33 fvdl }
676 1.33 fvdl if (nb != 0) {
677 1.33 fvdl /*
678 1.33 fvdl * Consider need to reallocate a fragment.
679 1.33 fvdl */
680 1.33 fvdl osize = fragroundup(fs, blkoff(fs, dp->di_extsize));
681 1.33 fvdl nsize = fragroundup(fs, size);
682 1.33 fvdl if (nsize <= osize) {
683 1.49 hannken error = bread(vp, -1 - lbn, osize,
684 1.49 hannken NOCRED, 0, &bp);
685 1.33 fvdl if (error) {
686 1.46 ad brelse(bp, 0);
687 1.33 fvdl return (error);
688 1.33 fvdl }
689 1.46 ad mutex_enter(&bp->b_interlock);
690 1.33 fvdl bp->b_blkno = fsbtodb(fs, nb);
691 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
692 1.46 ad mutex_exit(&bp->b_interlock);
693 1.33 fvdl } else {
694 1.46 ad mutex_enter(&ump->um_lock);
695 1.33 fvdl error = ffs_realloccg(ip, -1 - lbn,
696 1.33 fvdl dp->di_extb[lbn],
697 1.33 fvdl ffs_blkpref_ufs2(ip, lbn, (int)lbn,
698 1.33 fvdl &dp->di_extb[0]), osize, nsize, cred, &bp);
699 1.33 fvdl if (error)
700 1.33 fvdl return (error);
701 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
702 1.33 fvdl if (DOINGSOFTDEP(vp))
703 1.33 fvdl softdep_setup_allocext(ip, lbn,
704 1.33 fvdl dbtofsb(fs, bp->b_blkno), nb,
705 1.33 fvdl nsize, osize, bp);
706 1.33 fvdl }
707 1.33 fvdl } else {
708 1.33 fvdl if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
709 1.33 fvdl nsize = fragroundup(fs, size);
710 1.33 fvdl else
711 1.33 fvdl nsize = fs->fs_bsize;
712 1.46 ad mutex_enter(&ump->um_lock);
713 1.33 fvdl error = ffs_alloc(ip, lbn,
714 1.33 fvdl ffs_blkpref_ufs2(ip, lbn, (int)lbn, &dp->di_extb[0]),
715 1.33 fvdl nsize, cred, &newb);
716 1.33 fvdl if (error)
717 1.33 fvdl return (error);
718 1.33 fvdl bp = getblk(vp, -1 - lbn, nsize, 0, 0);
719 1.33 fvdl bp->b_blkno = fsbtodb(fs, newb);
720 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
721 1.33 fvdl if (flags & BA_CLRBUF)
722 1.33 fvdl vfs_bio_clrbuf(bp);
723 1.33 fvdl if (DOINGSOFTDEP(vp))
724 1.33 fvdl softdep_setup_allocext(ip, lbn, newb, 0,
725 1.33 fvdl nsize, 0, bp);
726 1.33 fvdl }
727 1.33 fvdl dp->di_extb[lbn] = dbtofsb(fs, bp->b_blkno);
728 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
729 1.33 fvdl *bpp = bp;
730 1.33 fvdl return (0);
731 1.33 fvdl }
732 1.33 fvdl #endif
733 1.33 fvdl /*
734 1.33 fvdl * If the next write will extend the file into a new block,
735 1.33 fvdl * and the file is currently composed of a fragment
736 1.33 fvdl * this fragment has to be extended to be a full block.
737 1.33 fvdl */
738 1.33 fvdl
739 1.33 fvdl lastlbn = lblkno(fs, ip->i_size);
740 1.33 fvdl if (lastlbn < NDADDR && lastlbn < lbn) {
741 1.33 fvdl nb = lastlbn;
742 1.33 fvdl osize = blksize(fs, ip, nb);
743 1.33 fvdl if (osize < fs->fs_bsize && osize > 0) {
744 1.46 ad mutex_enter(&ump->um_lock);
745 1.33 fvdl error = ffs_realloccg(ip, nb,
746 1.33 fvdl ffs_blkpref_ufs2(ip, lastlbn, nb,
747 1.33 fvdl &ip->i_ffs2_db[0]),
748 1.33 fvdl osize, (int)fs->fs_bsize, cred, bpp, &newb);
749 1.33 fvdl if (error)
750 1.33 fvdl return (error);
751 1.33 fvdl if (DOINGSOFTDEP(vp))
752 1.33 fvdl softdep_setup_allocdirect(ip, nb, newb,
753 1.33 fvdl ufs_rw64(ip->i_ffs2_db[nb], needswap),
754 1.33 fvdl fs->fs_bsize, osize, bpp ? *bpp : NULL);
755 1.33 fvdl ip->i_size = lblktosize(fs, nb + 1);
756 1.33 fvdl ip->i_ffs2_size = ip->i_size;
757 1.33 fvdl uvm_vnp_setsize(vp, ip->i_size);
758 1.33 fvdl ip->i_ffs2_db[nb] = ufs_rw64(newb, needswap);
759 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
760 1.33 fvdl if (bpp) {
761 1.33 fvdl if (flags & B_SYNC)
762 1.33 fvdl bwrite(*bpp);
763 1.33 fvdl else
764 1.33 fvdl bawrite(*bpp);
765 1.33 fvdl }
766 1.33 fvdl }
767 1.33 fvdl }
768 1.33 fvdl
769 1.33 fvdl /*
770 1.33 fvdl * The first NDADDR blocks are direct blocks
771 1.33 fvdl */
772 1.33 fvdl
773 1.33 fvdl if (lbn < NDADDR) {
774 1.33 fvdl nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
775 1.33 fvdl if (nb != 0 && ip->i_size >= lblktosize(fs, lbn + 1)) {
776 1.33 fvdl
777 1.33 fvdl /*
778 1.33 fvdl * The block is an already-allocated direct block
779 1.33 fvdl * and the file already extends past this block,
780 1.33 fvdl * thus this must be a whole block.
781 1.33 fvdl * Just read the block (if requested).
782 1.33 fvdl */
783 1.33 fvdl
784 1.33 fvdl if (bpp != NULL) {
785 1.33 fvdl error = bread(vp, lbn, fs->fs_bsize, NOCRED,
786 1.49 hannken B_MODIFY, bpp);
787 1.33 fvdl if (error) {
788 1.46 ad brelse(*bpp, 0);
789 1.33 fvdl return (error);
790 1.33 fvdl }
791 1.33 fvdl }
792 1.33 fvdl return (0);
793 1.33 fvdl }
794 1.33 fvdl if (nb != 0) {
795 1.33 fvdl
796 1.33 fvdl /*
797 1.33 fvdl * Consider need to reallocate a fragment.
798 1.33 fvdl */
799 1.33 fvdl
800 1.33 fvdl osize = fragroundup(fs, blkoff(fs, ip->i_size));
801 1.33 fvdl nsize = fragroundup(fs, size);
802 1.33 fvdl if (nsize <= osize) {
803 1.33 fvdl
804 1.33 fvdl /*
805 1.33 fvdl * The existing block is already
806 1.33 fvdl * at least as big as we want.
807 1.33 fvdl * Just read the block (if requested).
808 1.33 fvdl */
809 1.33 fvdl
810 1.33 fvdl if (bpp != NULL) {
811 1.33 fvdl error = bread(vp, lbn, osize, NOCRED,
812 1.49 hannken B_MODIFY, bpp);
813 1.33 fvdl if (error) {
814 1.46 ad brelse(*bpp, 0);
815 1.33 fvdl return (error);
816 1.33 fvdl }
817 1.33 fvdl }
818 1.33 fvdl return 0;
819 1.33 fvdl } else {
820 1.33 fvdl
821 1.33 fvdl /*
822 1.33 fvdl * The existing block is smaller than we want,
823 1.33 fvdl * grow it.
824 1.33 fvdl */
825 1.46 ad mutex_enter(&ump->um_lock);
826 1.33 fvdl error = ffs_realloccg(ip, lbn,
827 1.33 fvdl ffs_blkpref_ufs2(ip, lbn, (int)lbn,
828 1.33 fvdl &ip->i_ffs2_db[0]), osize, nsize, cred,
829 1.33 fvdl bpp, &newb);
830 1.33 fvdl if (error)
831 1.33 fvdl return (error);
832 1.33 fvdl if (DOINGSOFTDEP(vp))
833 1.33 fvdl softdep_setup_allocdirect(ip, lbn,
834 1.33 fvdl newb, nb, nsize, osize,
835 1.33 fvdl bpp ? *bpp : NULL);
836 1.33 fvdl }
837 1.33 fvdl } else {
838 1.33 fvdl
839 1.33 fvdl /*
840 1.33 fvdl * the block was not previously allocated,
841 1.33 fvdl * allocate a new block or fragment.
842 1.33 fvdl */
843 1.33 fvdl
844 1.33 fvdl if (ip->i_size < lblktosize(fs, lbn + 1))
845 1.33 fvdl nsize = fragroundup(fs, size);
846 1.33 fvdl else
847 1.33 fvdl nsize = fs->fs_bsize;
848 1.46 ad mutex_enter(&ump->um_lock);
849 1.33 fvdl error = ffs_alloc(ip, lbn,
850 1.33 fvdl ffs_blkpref_ufs2(ip, lbn, (int)lbn,
851 1.33 fvdl &ip->i_ffs2_db[0]), nsize, cred, &newb);
852 1.33 fvdl if (error)
853 1.33 fvdl return (error);
854 1.33 fvdl if (bpp != NULL) {
855 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, newb),
856 1.49 hannken nsize, (flags & B_CLRBUF) != 0, bpp);
857 1.49 hannken if (error)
858 1.49 hannken return error;
859 1.33 fvdl }
860 1.33 fvdl if (DOINGSOFTDEP(vp)) {
861 1.33 fvdl softdep_setup_allocdirect(ip, lbn, newb, 0,
862 1.33 fvdl nsize, 0, bpp ? *bpp : NULL);
863 1.33 fvdl }
864 1.33 fvdl }
865 1.33 fvdl ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
866 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
867 1.33 fvdl return (0);
868 1.33 fvdl }
869 1.33 fvdl
870 1.33 fvdl /*
871 1.33 fvdl * Determine the number of levels of indirection.
872 1.33 fvdl */
873 1.33 fvdl
874 1.33 fvdl pref = 0;
875 1.33 fvdl if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
876 1.33 fvdl return (error);
877 1.33 fvdl
878 1.33 fvdl /*
879 1.33 fvdl * Fetch the first indirect block allocating if necessary.
880 1.33 fvdl */
881 1.33 fvdl
882 1.33 fvdl --num;
883 1.33 fvdl nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
884 1.33 fvdl allocib = NULL;
885 1.33 fvdl allocblk = allociblk;
886 1.33 fvdl if (nb == 0) {
887 1.46 ad mutex_enter(&ump->um_lock);
888 1.33 fvdl pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
889 1.33 fvdl error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
890 1.33 fvdl &newb);
891 1.33 fvdl if (error)
892 1.33 fvdl goto fail;
893 1.33 fvdl nb = newb;
894 1.33 fvdl *allocblk++ = nb;
895 1.49 hannken error = ffs_getblk(vp, indirs[1].in_lbn, fsbtodb(fs, nb),
896 1.49 hannken fs->fs_bsize, true, &bp);
897 1.49 hannken if (error)
898 1.49 hannken goto fail;
899 1.33 fvdl if (DOINGSOFTDEP(vp)) {
900 1.33 fvdl softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
901 1.33 fvdl newb, 0, fs->fs_bsize, 0, bp);
902 1.33 fvdl bdwrite(bp);
903 1.33 fvdl } else {
904 1.33 fvdl
905 1.33 fvdl /*
906 1.33 fvdl * Write synchronously so that indirect blocks
907 1.33 fvdl * never point at garbage.
908 1.33 fvdl */
909 1.33 fvdl
910 1.33 fvdl if ((error = bwrite(bp)) != 0)
911 1.33 fvdl goto fail;
912 1.33 fvdl }
913 1.33 fvdl unwindidx = 0;
914 1.33 fvdl allocib = &ip->i_ffs2_ib[indirs[0].in_off];
915 1.33 fvdl *allocib = ufs_rw64(nb, needswap);
916 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
917 1.33 fvdl }
918 1.33 fvdl
919 1.33 fvdl /*
920 1.33 fvdl * Fetch through the indirect blocks, allocating as necessary.
921 1.33 fvdl */
922 1.33 fvdl
923 1.33 fvdl for (i = 1;;) {
924 1.33 fvdl error = bread(vp,
925 1.49 hannken indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, 0, &bp);
926 1.33 fvdl if (error) {
927 1.46 ad brelse(bp, 0);
928 1.33 fvdl goto fail;
929 1.33 fvdl }
930 1.33 fvdl bap = (int64_t *)bp->b_data;
931 1.33 fvdl nb = ufs_rw64(bap[indirs[i].in_off], needswap);
932 1.33 fvdl if (i == num)
933 1.33 fvdl break;
934 1.33 fvdl i++;
935 1.33 fvdl if (nb != 0) {
936 1.46 ad brelse(bp, 0);
937 1.33 fvdl continue;
938 1.33 fvdl }
939 1.49 hannken if (fscow_run(bp, true) != 0) {
940 1.49 hannken brelse(bp, 0);
941 1.49 hannken goto fail;
942 1.49 hannken }
943 1.46 ad mutex_enter(&ump->um_lock);
944 1.33 fvdl if (pref == 0)
945 1.33 fvdl pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
946 1.33 fvdl error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
947 1.33 fvdl &newb);
948 1.33 fvdl if (error) {
949 1.46 ad brelse(bp, 0);
950 1.33 fvdl goto fail;
951 1.33 fvdl }
952 1.33 fvdl nb = newb;
953 1.33 fvdl *allocblk++ = nb;
954 1.49 hannken error = ffs_getblk(vp, indirs[i].in_lbn, fsbtodb(fs, nb),
955 1.49 hannken fs->fs_bsize, true, &nbp);
956 1.49 hannken if (error) {
957 1.49 hannken brelse(bp, 0);
958 1.49 hannken goto fail;
959 1.49 hannken }
960 1.33 fvdl if (DOINGSOFTDEP(vp)) {
961 1.33 fvdl softdep_setup_allocindir_meta(nbp, ip, bp,
962 1.33 fvdl indirs[i - 1].in_off, nb);
963 1.33 fvdl bdwrite(nbp);
964 1.33 fvdl } else {
965 1.33 fvdl
966 1.33 fvdl /*
967 1.33 fvdl * Write synchronously so that indirect blocks
968 1.33 fvdl * never point at garbage.
969 1.33 fvdl */
970 1.33 fvdl
971 1.33 fvdl if ((error = bwrite(nbp)) != 0) {
972 1.46 ad brelse(bp, 0);
973 1.33 fvdl goto fail;
974 1.33 fvdl }
975 1.33 fvdl }
976 1.33 fvdl if (unwindidx < 0)
977 1.33 fvdl unwindidx = i - 1;
978 1.33 fvdl bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
979 1.33 fvdl
980 1.33 fvdl /*
981 1.33 fvdl * If required, write synchronously, otherwise use
982 1.33 fvdl * delayed write.
983 1.33 fvdl */
984 1.33 fvdl
985 1.33 fvdl if (flags & B_SYNC) {
986 1.33 fvdl bwrite(bp);
987 1.33 fvdl } else {
988 1.33 fvdl bdwrite(bp);
989 1.33 fvdl }
990 1.33 fvdl }
991 1.33 fvdl
992 1.35 hannken if (flags & B_METAONLY) {
993 1.41 hannken KASSERT(bpp != NULL);
994 1.35 hannken *bpp = bp;
995 1.35 hannken return (0);
996 1.35 hannken }
997 1.35 hannken
998 1.33 fvdl /*
999 1.33 fvdl * Get the data block, allocating if necessary.
1000 1.33 fvdl */
1001 1.33 fvdl
1002 1.33 fvdl if (nb == 0) {
1003 1.49 hannken if (fscow_run(bp, true) != 0) {
1004 1.49 hannken brelse(bp, 0);
1005 1.49 hannken goto fail;
1006 1.49 hannken }
1007 1.46 ad mutex_enter(&ump->um_lock);
1008 1.33 fvdl pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]);
1009 1.33 fvdl error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
1010 1.33 fvdl &newb);
1011 1.33 fvdl if (error) {
1012 1.46 ad brelse(bp, 0);
1013 1.33 fvdl goto fail;
1014 1.33 fvdl }
1015 1.33 fvdl nb = newb;
1016 1.33 fvdl *allocblk++ = nb;
1017 1.33 fvdl if (bpp != NULL) {
1018 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, nb),
1019 1.49 hannken fs->fs_bsize, (flags & B_CLRBUF) != 0, bpp);
1020 1.49 hannken if (error) {
1021 1.49 hannken brelse(bp, 0);
1022 1.49 hannken goto fail;
1023 1.49 hannken }
1024 1.33 fvdl }
1025 1.33 fvdl if (DOINGSOFTDEP(vp))
1026 1.33 fvdl softdep_setup_allocindir_page(ip, lbn, bp,
1027 1.33 fvdl indirs[num].in_off, nb, 0, bpp ? *bpp : NULL);
1028 1.33 fvdl bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
1029 1.33 fvdl if (allocib == NULL && unwindidx < 0) {
1030 1.33 fvdl unwindidx = i - 1;
1031 1.33 fvdl }
1032 1.33 fvdl
1033 1.33 fvdl /*
1034 1.33 fvdl * If required, write synchronously, otherwise use
1035 1.33 fvdl * delayed write.
1036 1.33 fvdl */
1037 1.33 fvdl
1038 1.33 fvdl if (flags & B_SYNC) {
1039 1.33 fvdl bwrite(bp);
1040 1.33 fvdl } else {
1041 1.33 fvdl bdwrite(bp);
1042 1.33 fvdl }
1043 1.33 fvdl return (0);
1044 1.33 fvdl }
1045 1.46 ad brelse(bp, 0);
1046 1.33 fvdl if (bpp != NULL) {
1047 1.33 fvdl if (flags & B_CLRBUF) {
1048 1.49 hannken error = bread(vp, lbn, (int)fs->fs_bsize,
1049 1.49 hannken NOCRED, B_MODIFY, &nbp);
1050 1.33 fvdl if (error) {
1051 1.46 ad brelse(nbp, 0);
1052 1.33 fvdl goto fail;
1053 1.33 fvdl }
1054 1.33 fvdl } else {
1055 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, nb),
1056 1.49 hannken fs->fs_bsize, true, &nbp);
1057 1.49 hannken if (error)
1058 1.49 hannken goto fail;
1059 1.33 fvdl }
1060 1.33 fvdl *bpp = nbp;
1061 1.33 fvdl }
1062 1.33 fvdl return (0);
1063 1.33 fvdl
1064 1.33 fvdl fail:
1065 1.33 fvdl /*
1066 1.33 fvdl * If we have failed part way through block allocation, we
1067 1.33 fvdl * have to deallocate any indirect blocks that we have allocated.
1068 1.33 fvdl */
1069 1.33 fvdl
1070 1.33 fvdl if (unwindidx >= 0) {
1071 1.33 fvdl
1072 1.33 fvdl /*
1073 1.33 fvdl * First write out any buffers we've created to resolve their
1074 1.33 fvdl * softdeps. This must be done in reverse order of creation
1075 1.33 fvdl * so that we resolve the dependencies in one pass.
1076 1.33 fvdl * Write the cylinder group buffers for these buffers too.
1077 1.33 fvdl */
1078 1.33 fvdl
1079 1.33 fvdl for (i = num; i >= unwindidx; i--) {
1080 1.33 fvdl if (i == 0) {
1081 1.33 fvdl break;
1082 1.33 fvdl }
1083 1.33 fvdl bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
1084 1.33 fvdl 0);
1085 1.48 ad if (bp->b_oflags & BO_DELWRI) {
1086 1.33 fvdl nb = fsbtodb(fs, cgtod(fs, dtog(fs,
1087 1.33 fvdl dbtofsb(fs, bp->b_blkno))));
1088 1.33 fvdl bwrite(bp);
1089 1.33 fvdl bp = getblk(ip->i_devvp, nb, (int)fs->fs_cgsize,
1090 1.33 fvdl 0, 0);
1091 1.48 ad if (bp->b_oflags & BO_DELWRI) {
1092 1.33 fvdl bwrite(bp);
1093 1.33 fvdl } else {
1094 1.46 ad brelse(bp, BC_INVAL);
1095 1.33 fvdl }
1096 1.33 fvdl } else {
1097 1.46 ad brelse(bp, BC_INVAL);
1098 1.33 fvdl }
1099 1.33 fvdl }
1100 1.47 ad
1101 1.47 ad /* Now flush the dependencies to disk. */
1102 1.47 ad #ifdef notyet
1103 1.47 ad /* XXX pages locked */
1104 1.47 ad (void)softdep_sync_metadata(vp);
1105 1.47 ad #endif
1106 1.47 ad
1107 1.36 mycroft if (DOINGSOFTDEP(vp) && unwindidx == 0) {
1108 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
1109 1.39 yamt ffs_update(vp, NULL, NULL, UPDATE_WAIT);
1110 1.33 fvdl }
1111 1.33 fvdl
1112 1.33 fvdl /*
1113 1.33 fvdl * Now that any dependencies that we created have been
1114 1.33 fvdl * resolved, we can undo the partial allocation.
1115 1.33 fvdl */
1116 1.33 fvdl
1117 1.33 fvdl if (unwindidx == 0) {
1118 1.33 fvdl *allocib = 0;
1119 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
1120 1.36 mycroft if (DOINGSOFTDEP(vp))
1121 1.39 yamt ffs_update(vp, NULL, NULL, UPDATE_WAIT);
1122 1.33 fvdl } else {
1123 1.33 fvdl int r;
1124 1.33 fvdl
1125 1.33 fvdl r = bread(vp, indirs[unwindidx].in_lbn,
1126 1.49 hannken (int)fs->fs_bsize, NOCRED, 0, &bp);
1127 1.33 fvdl if (r) {
1128 1.33 fvdl panic("Could not unwind indirect block, error %d", r);
1129 1.46 ad brelse(bp, 0);
1130 1.33 fvdl } else {
1131 1.33 fvdl bap = (int64_t *)bp->b_data;
1132 1.33 fvdl bap[indirs[unwindidx].in_off] = 0;
1133 1.33 fvdl bwrite(bp);
1134 1.33 fvdl }
1135 1.33 fvdl }
1136 1.33 fvdl for (i = unwindidx + 1; i <= num; i++) {
1137 1.33 fvdl bp = getblk(vp, indirs[i].in_lbn, (int)fs->fs_bsize, 0,
1138 1.33 fvdl 0);
1139 1.46 ad brelse(bp, BC_INVAL);
1140 1.33 fvdl }
1141 1.33 fvdl }
1142 1.33 fvdl for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
1143 1.35 hannken ffs_blkfree(fs, ip->i_devvp, *blkp, fs->fs_bsize, ip->i_number);
1144 1.33 fvdl deallocated += fs->fs_bsize;
1145 1.33 fvdl }
1146 1.33 fvdl if (deallocated) {
1147 1.33 fvdl #ifdef QUOTA
1148 1.33 fvdl /*
1149 1.33 fvdl * Restore user's disk quota because allocation failed.
1150 1.33 fvdl */
1151 1.33 fvdl (void)chkdq(ip, -btodb(deallocated), cred, FORCE);
1152 1.8 fvdl #endif
1153 1.33 fvdl ip->i_ffs2_blocks -= btodb(deallocated);
1154 1.13 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
1155 1.8 fvdl }
1156 1.47 ad
1157 1.47 ad /*
1158 1.47 ad * Flush all dependencies again so that the soft updates code
1159 1.47 ad * doesn't find any untracked changes.
1160 1.47 ad */
1161 1.47 ad #ifdef notyet
1162 1.47 ad /* XXX pages locked */
1163 1.47 ad (void)softdep_sync_metadata(vp);
1164 1.47 ad #endif
1165 1.8 fvdl return (error);
1166 1.1 mycroft }
1167