ffs_balloc.c revision 1.51.4.1 1 1.51.4.1 bouyer /* $NetBSD: ffs_balloc.c,v 1.51.4.1 2011/06/18 17:00:25 bouyer 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.51.4.1 bouyer __KERNEL_RCSID(0, "$NetBSD: ffs_balloc.c,v 1.51.4.1 2011/06/18 17:00:25 bouyer 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.51 simonb ffs_blkpref_ufs1(ip, lastlbn, nb, flags,
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.51 simonb ffs_blkpref_ufs1(ip, lbn, (int)lbn, flags,
226 1.51 simonb &ip->i_ffs1_db[0]),
227 1.51 simonb osize, nsize, cred, 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.51 simonb ffs_blkpref_ufs1(ip, lbn, (int)lbn, flags,
249 1.33 fvdl &ip->i_ffs1_db[0]),
250 1.51 simonb nsize, flags, 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.51 simonb pref = ffs_blkpref_ufs1(ip, lbn, 0, flags | B_METAONLY, NULL);
288 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
289 1.51 simonb flags | B_METAONLY, cred, &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.51.4.1 bouyer /* Try to keep snapshot indirect blocks contiguous. */
344 1.51.4.1 bouyer if (i == num && (ip->i_flags & SF_SNAPSHOT) != 0)
345 1.51.4.1 bouyer pref = ffs_blkpref_ufs1(ip, lbn, indirs[i-1].in_off,
346 1.51.4.1 bouyer flags | B_METAONLY, &bap[0]);
347 1.1 mycroft if (pref == 0)
348 1.51 simonb pref = ffs_blkpref_ufs1(ip, lbn, 0, flags | B_METAONLY,
349 1.51 simonb NULL);
350 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
351 1.51 simonb flags | B_METAONLY, cred, &newb);
352 1.3 christos if (error) {
353 1.46 ad brelse(bp, 0);
354 1.8 fvdl goto fail;
355 1.1 mycroft }
356 1.1 mycroft nb = newb;
357 1.8 fvdl *allocblk++ = nb;
358 1.49 hannken error = ffs_getblk(vp, indirs[i].in_lbn, fsbtodb(fs, nb),
359 1.49 hannken fs->fs_bsize, true, &nbp);
360 1.49 hannken if (error) {
361 1.49 hannken brelse(bp, 0);
362 1.49 hannken goto fail;
363 1.49 hannken }
364 1.15 fvdl if (DOINGSOFTDEP(vp)) {
365 1.15 fvdl softdep_setup_allocindir_meta(nbp, ip, bp,
366 1.15 fvdl indirs[i - 1].in_off, nb);
367 1.15 fvdl bdwrite(nbp);
368 1.15 fvdl } else {
369 1.29 chs
370 1.15 fvdl /*
371 1.15 fvdl * Write synchronously so that indirect blocks
372 1.15 fvdl * never point at garbage.
373 1.15 fvdl */
374 1.29 chs
375 1.15 fvdl if ((error = bwrite(nbp)) != 0) {
376 1.46 ad brelse(bp, 0);
377 1.15 fvdl goto fail;
378 1.15 fvdl }
379 1.1 mycroft }
380 1.18 mycroft if (unwindidx < 0)
381 1.18 mycroft unwindidx = i - 1;
382 1.33 fvdl bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
383 1.29 chs
384 1.1 mycroft /*
385 1.1 mycroft * If required, write synchronously, otherwise use
386 1.1 mycroft * delayed write.
387 1.1 mycroft */
388 1.29 chs
389 1.1 mycroft if (flags & B_SYNC) {
390 1.1 mycroft bwrite(bp);
391 1.1 mycroft } else {
392 1.1 mycroft bdwrite(bp);
393 1.1 mycroft }
394 1.1 mycroft }
395 1.29 chs
396 1.35 hannken if (flags & B_METAONLY) {
397 1.41 hannken KASSERT(bpp != NULL);
398 1.35 hannken *bpp = bp;
399 1.35 hannken return (0);
400 1.35 hannken }
401 1.35 hannken
402 1.1 mycroft /*
403 1.1 mycroft * Get the data block, allocating if necessary.
404 1.1 mycroft */
405 1.29 chs
406 1.1 mycroft if (nb == 0) {
407 1.49 hannken if (fscow_run(bp, true) != 0) {
408 1.49 hannken brelse(bp, 0);
409 1.49 hannken goto fail;
410 1.49 hannken }
411 1.46 ad mutex_enter(&ump->um_lock);
412 1.51 simonb pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, flags,
413 1.51 simonb &bap[0]);
414 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, flags, cred,
415 1.18 mycroft &newb);
416 1.3 christos if (error) {
417 1.46 ad brelse(bp, 0);
418 1.8 fvdl goto fail;
419 1.1 mycroft }
420 1.1 mycroft nb = newb;
421 1.8 fvdl *allocblk++ = nb;
422 1.23 chs if (bpp != NULL) {
423 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, nb),
424 1.49 hannken fs->fs_bsize, (flags & B_CLRBUF) != 0, bpp);
425 1.49 hannken if (error) {
426 1.49 hannken brelse(bp, 0);
427 1.49 hannken goto fail;
428 1.49 hannken }
429 1.23 chs }
430 1.15 fvdl if (DOINGSOFTDEP(vp))
431 1.15 fvdl softdep_setup_allocindir_page(ip, lbn, bp,
432 1.23 chs indirs[num].in_off, nb, 0, bpp ? *bpp : NULL);
433 1.33 fvdl bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
434 1.23 chs if (allocib == NULL && unwindidx < 0) {
435 1.23 chs unwindidx = i - 1;
436 1.23 chs }
437 1.29 chs
438 1.1 mycroft /*
439 1.1 mycroft * If required, write synchronously, otherwise use
440 1.1 mycroft * delayed write.
441 1.1 mycroft */
442 1.29 chs
443 1.1 mycroft if (flags & B_SYNC) {
444 1.1 mycroft bwrite(bp);
445 1.1 mycroft } else {
446 1.1 mycroft bdwrite(bp);
447 1.1 mycroft }
448 1.1 mycroft return (0);
449 1.1 mycroft }
450 1.46 ad brelse(bp, 0);
451 1.23 chs if (bpp != NULL) {
452 1.23 chs if (flags & B_CLRBUF) {
453 1.49 hannken error = bread(vp, lbn, (int)fs->fs_bsize,
454 1.49 hannken NOCRED, B_MODIFY, &nbp);
455 1.23 chs if (error) {
456 1.46 ad brelse(nbp, 0);
457 1.23 chs goto fail;
458 1.23 chs }
459 1.23 chs } else {
460 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, nb),
461 1.49 hannken fs->fs_bsize, true, &nbp);
462 1.49 hannken if (error)
463 1.49 hannken goto fail;
464 1.1 mycroft }
465 1.23 chs *bpp = nbp;
466 1.1 mycroft }
467 1.1 mycroft return (0);
468 1.27 chs
469 1.8 fvdl fail:
470 1.27 chs /*
471 1.29 chs * If we have failed part way through block allocation, we
472 1.29 chs * have to deallocate any indirect blocks that we have allocated.
473 1.27 chs */
474 1.27 chs
475 1.29 chs if (unwindidx >= 0) {
476 1.27 chs
477 1.29 chs /*
478 1.29 chs * First write out any buffers we've created to resolve their
479 1.29 chs * softdeps. This must be done in reverse order of creation
480 1.29 chs * so that we resolve the dependencies in one pass.
481 1.29 chs * Write the cylinder group buffers for these buffers too.
482 1.29 chs */
483 1.29 chs
484 1.29 chs for (i = num; i >= unwindidx; i--) {
485 1.29 chs if (i == 0) {
486 1.29 chs break;
487 1.29 chs }
488 1.50 hannken if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
489 1.50 hannken fs->fs_bsize, false, &bp) != 0)
490 1.50 hannken continue;
491 1.48 ad if (bp->b_oflags & BO_DELWRI) {
492 1.29 chs nb = fsbtodb(fs, cgtod(fs, dtog(fs,
493 1.30 chs dbtofsb(fs, bp->b_blkno))));
494 1.29 chs bwrite(bp);
495 1.50 hannken if (ffs_getblk(ip->i_devvp, nb, FFS_NOBLK,
496 1.50 hannken fs->fs_cgsize, false, &bp) != 0)
497 1.50 hannken continue;
498 1.48 ad if (bp->b_oflags & BO_DELWRI) {
499 1.29 chs bwrite(bp);
500 1.29 chs } else {
501 1.46 ad brelse(bp, BC_INVAL);
502 1.29 chs }
503 1.29 chs } else {
504 1.46 ad brelse(bp, BC_INVAL);
505 1.29 chs }
506 1.29 chs }
507 1.47 ad
508 1.47 ad /* Now flush all dependencies to disk. */
509 1.47 ad #ifdef notyet
510 1.47 ad /* XXX pages locked */
511 1.47 ad (void)softdep_sync_metadata(vp);
512 1.47 ad #endif
513 1.47 ad
514 1.36 mycroft if (DOINGSOFTDEP(vp) && unwindidx == 0) {
515 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
516 1.39 yamt ffs_update(vp, NULL, NULL, UPDATE_WAIT);
517 1.27 chs }
518 1.27 chs
519 1.29 chs /*
520 1.29 chs * Now that any dependencies that we created have been
521 1.29 chs * resolved, we can undo the partial allocation.
522 1.29 chs */
523 1.27 chs
524 1.18 mycroft if (unwindidx == 0) {
525 1.18 mycroft *allocib = 0;
526 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
527 1.36 mycroft if (DOINGSOFTDEP(vp))
528 1.39 yamt ffs_update(vp, NULL, NULL, UPDATE_WAIT);
529 1.17 fvdl } else {
530 1.18 mycroft int r;
531 1.29 chs
532 1.29 chs r = bread(vp, indirs[unwindidx].in_lbn,
533 1.49 hannken (int)fs->fs_bsize, NOCRED, 0, &bp);
534 1.18 mycroft if (r) {
535 1.18 mycroft panic("Could not unwind indirect block, error %d", r);
536 1.46 ad brelse(bp, 0);
537 1.18 mycroft } else {
538 1.31 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
539 1.18 mycroft bap[indirs[unwindidx].in_off] = 0;
540 1.29 chs bwrite(bp);
541 1.18 mycroft }
542 1.17 fvdl }
543 1.19 mycroft for (i = unwindidx + 1; i <= num; i++) {
544 1.50 hannken if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
545 1.50 hannken fs->fs_bsize, false, &bp) == 0)
546 1.50 hannken brelse(bp, BC_INVAL);
547 1.19 mycroft }
548 1.17 fvdl }
549 1.29 chs for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
550 1.35 hannken ffs_blkfree(fs, ip->i_devvp, *blkp, fs->fs_bsize, ip->i_number);
551 1.29 chs deallocated += fs->fs_bsize;
552 1.29 chs }
553 1.8 fvdl if (deallocated) {
554 1.8 fvdl #ifdef QUOTA
555 1.8 fvdl /*
556 1.8 fvdl * Restore user's disk quota because allocation failed.
557 1.8 fvdl */
558 1.33 fvdl (void)chkdq(ip, -btodb(deallocated), cred, FORCE);
559 1.33 fvdl #endif
560 1.33 fvdl ip->i_ffs1_blocks -= btodb(deallocated);
561 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
562 1.33 fvdl }
563 1.47 ad /*
564 1.47 ad * Flush all dependencies again so that the soft updates code
565 1.47 ad * doesn't find any untracked changes.
566 1.47 ad */
567 1.47 ad #ifdef notyet
568 1.47 ad /* XXX pages locked */
569 1.47 ad (void)softdep_sync_metadata(vp);
570 1.47 ad #endif
571 1.33 fvdl return (error);
572 1.33 fvdl }
573 1.33 fvdl
574 1.33 fvdl static int
575 1.43 elad ffs_balloc_ufs2(struct vnode *vp, off_t off, int size, kauth_cred_t cred,
576 1.39 yamt int flags, struct buf **bpp)
577 1.33 fvdl {
578 1.33 fvdl daddr_t lbn, lastlbn;
579 1.33 fvdl struct buf *bp, *nbp;
580 1.33 fvdl struct inode *ip = VTOI(vp);
581 1.33 fvdl struct fs *fs = ip->i_fs;
582 1.46 ad struct ufsmount *ump = ip->i_ump;
583 1.33 fvdl struct indir indirs[NIADDR + 2];
584 1.33 fvdl daddr_t newb, pref, nb;
585 1.33 fvdl int64_t *bap;
586 1.33 fvdl int deallocated, osize, nsize, num, i, error;
587 1.33 fvdl daddr_t *blkp, *allocblk, allociblk[NIADDR + 1];
588 1.33 fvdl int64_t *allocib;
589 1.33 fvdl int unwindidx = -1;
590 1.33 fvdl #ifdef FFS_EI
591 1.33 fvdl const int needswap = UFS_FSNEEDSWAP(fs);
592 1.33 fvdl #endif
593 1.33 fvdl UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
594 1.33 fvdl
595 1.39 yamt lbn = lblkno(fs, off);
596 1.39 yamt size = blkoff(fs, off) + size;
597 1.33 fvdl if (size > fs->fs_bsize)
598 1.33 fvdl panic("ffs_balloc: blk too big");
599 1.33 fvdl if (bpp != NULL) {
600 1.33 fvdl *bpp = NULL;
601 1.33 fvdl }
602 1.33 fvdl UVMHIST_LOG(ubchist, "vp %p lbn 0x%x size 0x%x", vp, lbn, size,0);
603 1.33 fvdl
604 1.33 fvdl if (lbn < 0)
605 1.33 fvdl return (EFBIG);
606 1.33 fvdl
607 1.33 fvdl #ifdef notyet
608 1.33 fvdl /*
609 1.33 fvdl * Check for allocating external data.
610 1.33 fvdl */
611 1.33 fvdl if (flags & IO_EXT) {
612 1.33 fvdl if (lbn >= NXADDR)
613 1.33 fvdl return (EFBIG);
614 1.33 fvdl /*
615 1.33 fvdl * If the next write will extend the data into a new block,
616 1.33 fvdl * and the data is currently composed of a fragment
617 1.33 fvdl * this fragment has to be extended to be a full block.
618 1.33 fvdl */
619 1.33 fvdl lastlbn = lblkno(fs, dp->di_extsize);
620 1.33 fvdl if (lastlbn < lbn) {
621 1.33 fvdl nb = lastlbn;
622 1.33 fvdl osize = sblksize(fs, dp->di_extsize, nb);
623 1.33 fvdl if (osize < fs->fs_bsize && osize > 0) {
624 1.46 ad mutex_enter(&ump->um_lock);
625 1.33 fvdl error = ffs_realloccg(ip, -1 - nb,
626 1.33 fvdl dp->di_extb[nb],
627 1.33 fvdl ffs_blkpref_ufs2(ip, lastlbn, (int)nb,
628 1.51 simonb flags, &dp->di_extb[0]),
629 1.51 simonb osize,
630 1.33 fvdl (int)fs->fs_bsize, cred, &bp);
631 1.33 fvdl if (error)
632 1.33 fvdl return (error);
633 1.33 fvdl if (DOINGSOFTDEP(vp))
634 1.33 fvdl softdep_setup_allocext(ip, nb,
635 1.33 fvdl dbtofsb(fs, bp->b_blkno),
636 1.33 fvdl dp->di_extb[nb],
637 1.33 fvdl fs->fs_bsize, osize, bp);
638 1.33 fvdl dp->di_extsize = smalllblktosize(fs, nb + 1);
639 1.33 fvdl dp->di_extb[nb] = dbtofsb(fs, bp->b_blkno);
640 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
641 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
642 1.33 fvdl if (flags & IO_SYNC)
643 1.33 fvdl bwrite(bp);
644 1.33 fvdl else
645 1.33 fvdl bawrite(bp);
646 1.33 fvdl }
647 1.33 fvdl }
648 1.33 fvdl /*
649 1.33 fvdl * All blocks are direct blocks
650 1.33 fvdl */
651 1.33 fvdl if (flags & BA_METAONLY)
652 1.33 fvdl panic("ffs_balloc_ufs2: BA_METAONLY for ext block");
653 1.33 fvdl nb = dp->di_extb[lbn];
654 1.33 fvdl if (nb != 0 && dp->di_extsize >= smalllblktosize(fs, lbn + 1)) {
655 1.49 hannken error = bread(vp, -1 - lbn, fs->fs_bsize,
656 1.49 hannken NOCRED, 0, &bp);
657 1.33 fvdl if (error) {
658 1.46 ad brelse(bp, 0);
659 1.33 fvdl return (error);
660 1.33 fvdl }
661 1.48 ad mutex_enter(&bp->b_interlock);
662 1.33 fvdl bp->b_blkno = fsbtodb(fs, nb);
663 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
664 1.48 ad mutex_exit(&bp->b_interlock);
665 1.33 fvdl *bpp = bp;
666 1.33 fvdl return (0);
667 1.33 fvdl }
668 1.33 fvdl if (nb != 0) {
669 1.33 fvdl /*
670 1.33 fvdl * Consider need to reallocate a fragment.
671 1.33 fvdl */
672 1.33 fvdl osize = fragroundup(fs, blkoff(fs, dp->di_extsize));
673 1.33 fvdl nsize = fragroundup(fs, size);
674 1.33 fvdl if (nsize <= osize) {
675 1.49 hannken error = bread(vp, -1 - lbn, osize,
676 1.49 hannken NOCRED, 0, &bp);
677 1.33 fvdl if (error) {
678 1.46 ad brelse(bp, 0);
679 1.33 fvdl return (error);
680 1.33 fvdl }
681 1.46 ad mutex_enter(&bp->b_interlock);
682 1.33 fvdl bp->b_blkno = fsbtodb(fs, nb);
683 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
684 1.46 ad mutex_exit(&bp->b_interlock);
685 1.33 fvdl } else {
686 1.46 ad mutex_enter(&ump->um_lock);
687 1.33 fvdl error = ffs_realloccg(ip, -1 - lbn,
688 1.33 fvdl dp->di_extb[lbn],
689 1.51 simonb ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
690 1.51 simonb &dp->di_extb[0]),
691 1.51 simonb osize, nsize, cred, &bp);
692 1.33 fvdl if (error)
693 1.33 fvdl return (error);
694 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
695 1.33 fvdl if (DOINGSOFTDEP(vp))
696 1.33 fvdl softdep_setup_allocext(ip, lbn,
697 1.33 fvdl dbtofsb(fs, bp->b_blkno), nb,
698 1.33 fvdl nsize, osize, bp);
699 1.33 fvdl }
700 1.33 fvdl } else {
701 1.33 fvdl if (dp->di_extsize < smalllblktosize(fs, lbn + 1))
702 1.33 fvdl nsize = fragroundup(fs, size);
703 1.33 fvdl else
704 1.33 fvdl nsize = fs->fs_bsize;
705 1.46 ad mutex_enter(&ump->um_lock);
706 1.33 fvdl error = ffs_alloc(ip, lbn,
707 1.51 simonb ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
708 1.51 simonb &dp->di_extb[0]),
709 1.51 simonb nsize, flags, cred, &newb);
710 1.33 fvdl if (error)
711 1.33 fvdl return (error);
712 1.50 hannken error = ffs_getblk(vp, -1 - lbn, fsbtodb(fs, newb),
713 1.50 hannken nsize, (flags & BA_CLRBUF) != 0, &bp);
714 1.50 hannken if (error)
715 1.50 hannken return error;
716 1.33 fvdl bp->b_xflags |= BX_ALTDATA;
717 1.33 fvdl if (DOINGSOFTDEP(vp))
718 1.33 fvdl softdep_setup_allocext(ip, lbn, newb, 0,
719 1.33 fvdl nsize, 0, bp);
720 1.33 fvdl }
721 1.33 fvdl dp->di_extb[lbn] = dbtofsb(fs, bp->b_blkno);
722 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
723 1.33 fvdl *bpp = bp;
724 1.33 fvdl return (0);
725 1.33 fvdl }
726 1.33 fvdl #endif
727 1.33 fvdl /*
728 1.33 fvdl * If the next write will extend the file into a new block,
729 1.33 fvdl * and the file is currently composed of a fragment
730 1.33 fvdl * this fragment has to be extended to be a full block.
731 1.33 fvdl */
732 1.33 fvdl
733 1.33 fvdl lastlbn = lblkno(fs, ip->i_size);
734 1.33 fvdl if (lastlbn < NDADDR && lastlbn < lbn) {
735 1.33 fvdl nb = lastlbn;
736 1.33 fvdl osize = blksize(fs, ip, nb);
737 1.33 fvdl if (osize < fs->fs_bsize && osize > 0) {
738 1.46 ad mutex_enter(&ump->um_lock);
739 1.33 fvdl error = ffs_realloccg(ip, nb,
740 1.51 simonb ffs_blkpref_ufs2(ip, lastlbn, nb, flags,
741 1.33 fvdl &ip->i_ffs2_db[0]),
742 1.33 fvdl osize, (int)fs->fs_bsize, cred, bpp, &newb);
743 1.33 fvdl if (error)
744 1.33 fvdl return (error);
745 1.33 fvdl if (DOINGSOFTDEP(vp))
746 1.33 fvdl softdep_setup_allocdirect(ip, nb, newb,
747 1.33 fvdl ufs_rw64(ip->i_ffs2_db[nb], needswap),
748 1.33 fvdl fs->fs_bsize, osize, bpp ? *bpp : NULL);
749 1.33 fvdl ip->i_size = lblktosize(fs, nb + 1);
750 1.33 fvdl ip->i_ffs2_size = ip->i_size;
751 1.33 fvdl uvm_vnp_setsize(vp, ip->i_size);
752 1.33 fvdl ip->i_ffs2_db[nb] = ufs_rw64(newb, needswap);
753 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
754 1.33 fvdl if (bpp) {
755 1.33 fvdl if (flags & B_SYNC)
756 1.33 fvdl bwrite(*bpp);
757 1.33 fvdl else
758 1.33 fvdl bawrite(*bpp);
759 1.33 fvdl }
760 1.33 fvdl }
761 1.33 fvdl }
762 1.33 fvdl
763 1.33 fvdl /*
764 1.33 fvdl * The first NDADDR blocks are direct blocks
765 1.33 fvdl */
766 1.33 fvdl
767 1.33 fvdl if (lbn < NDADDR) {
768 1.33 fvdl nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
769 1.33 fvdl if (nb != 0 && ip->i_size >= lblktosize(fs, lbn + 1)) {
770 1.33 fvdl
771 1.33 fvdl /*
772 1.33 fvdl * The block is an already-allocated direct block
773 1.33 fvdl * and the file already extends past this block,
774 1.33 fvdl * thus this must be a whole block.
775 1.33 fvdl * Just read the block (if requested).
776 1.33 fvdl */
777 1.33 fvdl
778 1.33 fvdl if (bpp != NULL) {
779 1.33 fvdl error = bread(vp, lbn, fs->fs_bsize, NOCRED,
780 1.49 hannken B_MODIFY, bpp);
781 1.33 fvdl if (error) {
782 1.46 ad brelse(*bpp, 0);
783 1.33 fvdl return (error);
784 1.33 fvdl }
785 1.33 fvdl }
786 1.33 fvdl return (0);
787 1.33 fvdl }
788 1.33 fvdl if (nb != 0) {
789 1.33 fvdl
790 1.33 fvdl /*
791 1.33 fvdl * Consider need to reallocate a fragment.
792 1.33 fvdl */
793 1.33 fvdl
794 1.33 fvdl osize = fragroundup(fs, blkoff(fs, ip->i_size));
795 1.33 fvdl nsize = fragroundup(fs, size);
796 1.33 fvdl if (nsize <= osize) {
797 1.33 fvdl
798 1.33 fvdl /*
799 1.33 fvdl * The existing block is already
800 1.33 fvdl * at least as big as we want.
801 1.33 fvdl * Just read the block (if requested).
802 1.33 fvdl */
803 1.33 fvdl
804 1.33 fvdl if (bpp != NULL) {
805 1.33 fvdl error = bread(vp, lbn, osize, NOCRED,
806 1.49 hannken B_MODIFY, bpp);
807 1.33 fvdl if (error) {
808 1.46 ad brelse(*bpp, 0);
809 1.33 fvdl return (error);
810 1.33 fvdl }
811 1.33 fvdl }
812 1.33 fvdl return 0;
813 1.33 fvdl } else {
814 1.33 fvdl
815 1.33 fvdl /*
816 1.33 fvdl * The existing block is smaller than we want,
817 1.33 fvdl * grow it.
818 1.33 fvdl */
819 1.46 ad mutex_enter(&ump->um_lock);
820 1.33 fvdl error = ffs_realloccg(ip, lbn,
821 1.51 simonb ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
822 1.51 simonb &ip->i_ffs2_db[0]),
823 1.51 simonb osize, nsize, cred, bpp, &newb);
824 1.33 fvdl if (error)
825 1.33 fvdl return (error);
826 1.33 fvdl if (DOINGSOFTDEP(vp))
827 1.33 fvdl softdep_setup_allocdirect(ip, lbn,
828 1.33 fvdl newb, nb, nsize, osize,
829 1.33 fvdl bpp ? *bpp : NULL);
830 1.33 fvdl }
831 1.33 fvdl } else {
832 1.33 fvdl
833 1.33 fvdl /*
834 1.33 fvdl * the block was not previously allocated,
835 1.33 fvdl * allocate a new block or fragment.
836 1.33 fvdl */
837 1.33 fvdl
838 1.33 fvdl if (ip->i_size < lblktosize(fs, lbn + 1))
839 1.33 fvdl nsize = fragroundup(fs, size);
840 1.33 fvdl else
841 1.33 fvdl nsize = fs->fs_bsize;
842 1.46 ad mutex_enter(&ump->um_lock);
843 1.33 fvdl error = ffs_alloc(ip, lbn,
844 1.51 simonb ffs_blkpref_ufs2(ip, lbn, (int)lbn, flags,
845 1.51 simonb &ip->i_ffs2_db[0]),
846 1.51 simonb nsize, flags, cred, &newb);
847 1.33 fvdl if (error)
848 1.33 fvdl return (error);
849 1.33 fvdl if (bpp != NULL) {
850 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, newb),
851 1.49 hannken nsize, (flags & B_CLRBUF) != 0, bpp);
852 1.49 hannken if (error)
853 1.49 hannken return error;
854 1.33 fvdl }
855 1.33 fvdl if (DOINGSOFTDEP(vp)) {
856 1.33 fvdl softdep_setup_allocdirect(ip, lbn, newb, 0,
857 1.33 fvdl nsize, 0, bpp ? *bpp : NULL);
858 1.33 fvdl }
859 1.33 fvdl }
860 1.33 fvdl ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
861 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
862 1.33 fvdl return (0);
863 1.33 fvdl }
864 1.33 fvdl
865 1.33 fvdl /*
866 1.33 fvdl * Determine the number of levels of indirection.
867 1.33 fvdl */
868 1.33 fvdl
869 1.33 fvdl pref = 0;
870 1.33 fvdl if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
871 1.33 fvdl return (error);
872 1.33 fvdl
873 1.33 fvdl /*
874 1.33 fvdl * Fetch the first indirect block allocating if necessary.
875 1.33 fvdl */
876 1.33 fvdl
877 1.33 fvdl --num;
878 1.33 fvdl nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
879 1.33 fvdl allocib = NULL;
880 1.33 fvdl allocblk = allociblk;
881 1.33 fvdl if (nb == 0) {
882 1.46 ad mutex_enter(&ump->um_lock);
883 1.51 simonb pref = ffs_blkpref_ufs2(ip, lbn, 0, flags | B_METAONLY, NULL);
884 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
885 1.51 simonb flags | B_METAONLY, cred, &newb);
886 1.33 fvdl if (error)
887 1.33 fvdl goto fail;
888 1.33 fvdl nb = newb;
889 1.33 fvdl *allocblk++ = nb;
890 1.49 hannken error = ffs_getblk(vp, indirs[1].in_lbn, fsbtodb(fs, nb),
891 1.49 hannken fs->fs_bsize, true, &bp);
892 1.49 hannken if (error)
893 1.49 hannken goto fail;
894 1.33 fvdl if (DOINGSOFTDEP(vp)) {
895 1.33 fvdl softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
896 1.33 fvdl newb, 0, fs->fs_bsize, 0, bp);
897 1.33 fvdl bdwrite(bp);
898 1.33 fvdl } else {
899 1.33 fvdl
900 1.33 fvdl /*
901 1.33 fvdl * Write synchronously so that indirect blocks
902 1.33 fvdl * never point at garbage.
903 1.33 fvdl */
904 1.33 fvdl
905 1.33 fvdl if ((error = bwrite(bp)) != 0)
906 1.33 fvdl goto fail;
907 1.33 fvdl }
908 1.33 fvdl unwindidx = 0;
909 1.33 fvdl allocib = &ip->i_ffs2_ib[indirs[0].in_off];
910 1.33 fvdl *allocib = ufs_rw64(nb, needswap);
911 1.33 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
912 1.33 fvdl }
913 1.33 fvdl
914 1.33 fvdl /*
915 1.33 fvdl * Fetch through the indirect blocks, allocating as necessary.
916 1.33 fvdl */
917 1.33 fvdl
918 1.33 fvdl for (i = 1;;) {
919 1.33 fvdl error = bread(vp,
920 1.49 hannken indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, 0, &bp);
921 1.33 fvdl if (error) {
922 1.46 ad brelse(bp, 0);
923 1.33 fvdl goto fail;
924 1.33 fvdl }
925 1.33 fvdl bap = (int64_t *)bp->b_data;
926 1.33 fvdl nb = ufs_rw64(bap[indirs[i].in_off], needswap);
927 1.33 fvdl if (i == num)
928 1.33 fvdl break;
929 1.33 fvdl i++;
930 1.33 fvdl if (nb != 0) {
931 1.46 ad brelse(bp, 0);
932 1.33 fvdl continue;
933 1.33 fvdl }
934 1.49 hannken if (fscow_run(bp, true) != 0) {
935 1.49 hannken brelse(bp, 0);
936 1.49 hannken goto fail;
937 1.49 hannken }
938 1.46 ad mutex_enter(&ump->um_lock);
939 1.51.4.1 bouyer /* Try to keep snapshot indirect blocks contiguous. */
940 1.51.4.1 bouyer if (i == num && (ip->i_flags & SF_SNAPSHOT) != 0)
941 1.51.4.1 bouyer pref = ffs_blkpref_ufs2(ip, lbn, indirs[i-1].in_off,
942 1.51.4.1 bouyer flags | B_METAONLY, &bap[0]);
943 1.33 fvdl if (pref == 0)
944 1.51 simonb pref = ffs_blkpref_ufs2(ip, lbn, 0, flags | B_METAONLY,
945 1.51 simonb NULL);
946 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
947 1.51 simonb flags | B_METAONLY, cred, &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.51 simonb pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, flags,
1009 1.51 simonb &bap[0]);
1010 1.51 simonb error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, flags, cred,
1011 1.33 fvdl &newb);
1012 1.33 fvdl if (error) {
1013 1.46 ad brelse(bp, 0);
1014 1.33 fvdl goto fail;
1015 1.33 fvdl }
1016 1.33 fvdl nb = newb;
1017 1.33 fvdl *allocblk++ = nb;
1018 1.33 fvdl if (bpp != NULL) {
1019 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, nb),
1020 1.49 hannken fs->fs_bsize, (flags & B_CLRBUF) != 0, bpp);
1021 1.49 hannken if (error) {
1022 1.49 hannken brelse(bp, 0);
1023 1.49 hannken goto fail;
1024 1.49 hannken }
1025 1.33 fvdl }
1026 1.33 fvdl if (DOINGSOFTDEP(vp))
1027 1.33 fvdl softdep_setup_allocindir_page(ip, lbn, bp,
1028 1.33 fvdl indirs[num].in_off, nb, 0, bpp ? *bpp : NULL);
1029 1.33 fvdl bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
1030 1.33 fvdl if (allocib == NULL && unwindidx < 0) {
1031 1.33 fvdl unwindidx = i - 1;
1032 1.33 fvdl }
1033 1.33 fvdl
1034 1.33 fvdl /*
1035 1.33 fvdl * If required, write synchronously, otherwise use
1036 1.33 fvdl * delayed write.
1037 1.33 fvdl */
1038 1.33 fvdl
1039 1.33 fvdl if (flags & B_SYNC) {
1040 1.33 fvdl bwrite(bp);
1041 1.33 fvdl } else {
1042 1.33 fvdl bdwrite(bp);
1043 1.33 fvdl }
1044 1.33 fvdl return (0);
1045 1.33 fvdl }
1046 1.46 ad brelse(bp, 0);
1047 1.33 fvdl if (bpp != NULL) {
1048 1.33 fvdl if (flags & B_CLRBUF) {
1049 1.49 hannken error = bread(vp, lbn, (int)fs->fs_bsize,
1050 1.49 hannken NOCRED, B_MODIFY, &nbp);
1051 1.33 fvdl if (error) {
1052 1.46 ad brelse(nbp, 0);
1053 1.33 fvdl goto fail;
1054 1.33 fvdl }
1055 1.33 fvdl } else {
1056 1.49 hannken error = ffs_getblk(vp, lbn, fsbtodb(fs, nb),
1057 1.49 hannken fs->fs_bsize, true, &nbp);
1058 1.49 hannken if (error)
1059 1.49 hannken goto fail;
1060 1.33 fvdl }
1061 1.33 fvdl *bpp = nbp;
1062 1.33 fvdl }
1063 1.33 fvdl return (0);
1064 1.33 fvdl
1065 1.33 fvdl fail:
1066 1.33 fvdl /*
1067 1.33 fvdl * If we have failed part way through block allocation, we
1068 1.33 fvdl * have to deallocate any indirect blocks that we have allocated.
1069 1.33 fvdl */
1070 1.33 fvdl
1071 1.33 fvdl if (unwindidx >= 0) {
1072 1.33 fvdl
1073 1.33 fvdl /*
1074 1.33 fvdl * First write out any buffers we've created to resolve their
1075 1.33 fvdl * softdeps. This must be done in reverse order of creation
1076 1.33 fvdl * so that we resolve the dependencies in one pass.
1077 1.33 fvdl * Write the cylinder group buffers for these buffers too.
1078 1.33 fvdl */
1079 1.33 fvdl
1080 1.33 fvdl for (i = num; i >= unwindidx; i--) {
1081 1.33 fvdl if (i == 0) {
1082 1.33 fvdl break;
1083 1.33 fvdl }
1084 1.50 hannken if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
1085 1.50 hannken fs->fs_bsize, false, &bp) != 0)
1086 1.50 hannken continue;
1087 1.48 ad if (bp->b_oflags & BO_DELWRI) {
1088 1.33 fvdl nb = fsbtodb(fs, cgtod(fs, dtog(fs,
1089 1.33 fvdl dbtofsb(fs, bp->b_blkno))));
1090 1.33 fvdl bwrite(bp);
1091 1.50 hannken if (ffs_getblk(ip->i_devvp, nb, FFS_NOBLK,
1092 1.50 hannken fs->fs_cgsize, false, &bp) != 0)
1093 1.50 hannken continue;
1094 1.48 ad if (bp->b_oflags & BO_DELWRI) {
1095 1.33 fvdl bwrite(bp);
1096 1.33 fvdl } else {
1097 1.46 ad brelse(bp, BC_INVAL);
1098 1.33 fvdl }
1099 1.33 fvdl } else {
1100 1.46 ad brelse(bp, BC_INVAL);
1101 1.33 fvdl }
1102 1.33 fvdl }
1103 1.47 ad
1104 1.47 ad /* Now flush the dependencies to disk. */
1105 1.47 ad #ifdef notyet
1106 1.47 ad /* XXX pages locked */
1107 1.47 ad (void)softdep_sync_metadata(vp);
1108 1.47 ad #endif
1109 1.47 ad
1110 1.36 mycroft if (DOINGSOFTDEP(vp) && unwindidx == 0) {
1111 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
1112 1.39 yamt ffs_update(vp, NULL, NULL, UPDATE_WAIT);
1113 1.33 fvdl }
1114 1.33 fvdl
1115 1.33 fvdl /*
1116 1.33 fvdl * Now that any dependencies that we created have been
1117 1.33 fvdl * resolved, we can undo the partial allocation.
1118 1.33 fvdl */
1119 1.33 fvdl
1120 1.33 fvdl if (unwindidx == 0) {
1121 1.33 fvdl *allocib = 0;
1122 1.36 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
1123 1.36 mycroft if (DOINGSOFTDEP(vp))
1124 1.39 yamt ffs_update(vp, NULL, NULL, UPDATE_WAIT);
1125 1.33 fvdl } else {
1126 1.33 fvdl int r;
1127 1.33 fvdl
1128 1.33 fvdl r = bread(vp, indirs[unwindidx].in_lbn,
1129 1.49 hannken (int)fs->fs_bsize, NOCRED, 0, &bp);
1130 1.33 fvdl if (r) {
1131 1.33 fvdl panic("Could not unwind indirect block, error %d", r);
1132 1.46 ad brelse(bp, 0);
1133 1.33 fvdl } else {
1134 1.33 fvdl bap = (int64_t *)bp->b_data;
1135 1.33 fvdl bap[indirs[unwindidx].in_off] = 0;
1136 1.33 fvdl bwrite(bp);
1137 1.33 fvdl }
1138 1.33 fvdl }
1139 1.33 fvdl for (i = unwindidx + 1; i <= num; i++) {
1140 1.50 hannken if (ffs_getblk(vp, indirs[i].in_lbn, FFS_NOBLK,
1141 1.50 hannken fs->fs_bsize, false, &bp) == 0)
1142 1.50 hannken brelse(bp, BC_INVAL);
1143 1.33 fvdl }
1144 1.33 fvdl }
1145 1.33 fvdl for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
1146 1.35 hannken ffs_blkfree(fs, ip->i_devvp, *blkp, fs->fs_bsize, ip->i_number);
1147 1.33 fvdl deallocated += fs->fs_bsize;
1148 1.33 fvdl }
1149 1.33 fvdl if (deallocated) {
1150 1.33 fvdl #ifdef QUOTA
1151 1.33 fvdl /*
1152 1.33 fvdl * Restore user's disk quota because allocation failed.
1153 1.33 fvdl */
1154 1.33 fvdl (void)chkdq(ip, -btodb(deallocated), cred, FORCE);
1155 1.8 fvdl #endif
1156 1.33 fvdl ip->i_ffs2_blocks -= btodb(deallocated);
1157 1.13 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
1158 1.8 fvdl }
1159 1.47 ad
1160 1.47 ad /*
1161 1.47 ad * Flush all dependencies again so that the soft updates code
1162 1.47 ad * doesn't find any untracked changes.
1163 1.47 ad */
1164 1.47 ad #ifdef notyet
1165 1.47 ad /* XXX pages locked */
1166 1.47 ad (void)softdep_sync_metadata(vp);
1167 1.47 ad #endif
1168 1.8 fvdl return (error);
1169 1.1 mycroft }
1170