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