ext2fs_balloc.c revision 1.43 1 1.43 riastrad /* $NetBSD: ext2fs_balloc.c,v 1.43 2020/09/05 16:30:13 riastradh Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1982, 1986, 1989, 1993
5 1.1 bouyer * The Regents of the University of California. All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.20 agc * 3. Neither the name of the University nor the names of its contributors
16 1.20 agc * may be used to endorse or promote products derived from this software
17 1.20 agc * without specific prior written permission.
18 1.20 agc *
19 1.20 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.20 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.20 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.20 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.20 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.20 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.20 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.20 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.20 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.20 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.20 agc * SUCH DAMAGE.
30 1.20 agc *
31 1.20 agc * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93
32 1.20 agc * Modified for ext2fs by Manuel Bouyer.
33 1.20 agc */
34 1.20 agc
35 1.20 agc /*
36 1.20 agc * Copyright (c) 1997 Manuel Bouyer.
37 1.20 agc *
38 1.20 agc * Redistribution and use in source and binary forms, with or without
39 1.20 agc * modification, are permitted provided that the following conditions
40 1.20 agc * are met:
41 1.20 agc * 1. Redistributions of source code must retain the above copyright
42 1.20 agc * notice, this list of conditions and the following disclaimer.
43 1.20 agc * 2. Redistributions in binary form must reproduce the above copyright
44 1.20 agc * notice, this list of conditions and the following disclaimer in the
45 1.20 agc * documentation and/or other materials provided with the distribution.
46 1.1 bouyer *
47 1.22 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.22 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.22 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.22 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.22 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 1.22 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 1.22 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 1.22 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 1.22 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 1.22 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 1.1 bouyer *
58 1.1 bouyer * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93
59 1.1 bouyer * Modified for ext2fs by Manuel Bouyer.
60 1.1 bouyer */
61 1.13 lukem
62 1.13 lukem #include <sys/cdefs.h>
63 1.43 riastrad __KERNEL_RCSID(0, "$NetBSD: ext2fs_balloc.c,v 1.43 2020/09/05 16:30:13 riastradh Exp $");
64 1.1 bouyer
65 1.9 mrg #if defined(_KERNEL_OPT)
66 1.7 chs #include "opt_uvmhist.h"
67 1.7 chs #endif
68 1.7 chs
69 1.1 bouyer #include <sys/param.h>
70 1.1 bouyer #include <sys/systm.h>
71 1.1 bouyer #include <sys/buf.h>
72 1.1 bouyer #include <sys/proc.h>
73 1.1 bouyer #include <sys/file.h>
74 1.1 bouyer #include <sys/vnode.h>
75 1.7 chs #include <sys/mount.h>
76 1.29 elad #include <sys/kauth.h>
77 1.7 chs
78 1.43 riastrad #ifdef UVMHIST
79 1.7 chs #include <uvm/uvm.h>
80 1.43 riastrad #endif
81 1.43 riastrad #include <uvm/uvm_stat.h>
82 1.1 bouyer
83 1.1 bouyer #include <ufs/ufs/inode.h>
84 1.1 bouyer #include <ufs/ufs/ufs_extern.h>
85 1.1 bouyer
86 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
87 1.1 bouyer #include <ufs/ext2fs/ext2fs_extern.h>
88 1.1 bouyer
89 1.1 bouyer /*
90 1.1 bouyer * Balloc defines the structure of file system storage
91 1.1 bouyer * by allocating the physical blocks on a device given
92 1.1 bouyer * the inode and the logical block number in a file.
93 1.1 bouyer */
94 1.1 bouyer int
95 1.31 christos ext2fs_balloc(struct inode *ip, daddr_t bn, int size,
96 1.30 christos kauth_cred_t cred, struct buf **bpp, int flags)
97 1.1 bouyer {
98 1.4 augustss struct m_ext2fs *fs;
99 1.19 fvdl daddr_t nb;
100 1.1 bouyer struct buf *bp, *nbp;
101 1.1 bouyer struct vnode *vp = ITOV(ip);
102 1.37 dholland struct indir indirs[EXT2FS_NIADDR + 2];
103 1.19 fvdl daddr_t newb, lbn, pref;
104 1.19 fvdl int32_t *bap; /* XXX ondisk32 */
105 1.1 bouyer int num, i, error;
106 1.3 fvdl u_int deallocated;
107 1.37 dholland daddr_t *blkp, *allocblk, allociblk[EXT2FS_NIADDR + 1];
108 1.19 fvdl int32_t *allocib; /* XXX ondisk32 */
109 1.5 mycroft int unwindidx = -1;
110 1.7 chs UVMHIST_FUNC("ext2fs_balloc"); UVMHIST_CALLED(ubchist);
111 1.1 bouyer
112 1.41 christos UVMHIST_LOG(ubchist, "bn 0x%x", bn, 0, 0, 0);
113 1.7 chs
114 1.7 chs if (bpp != NULL) {
115 1.7 chs *bpp = NULL;
116 1.7 chs }
117 1.1 bouyer if (bn < 0)
118 1.41 christos return EFBIG;
119 1.1 bouyer fs = ip->i_e2fs;
120 1.1 bouyer lbn = bn;
121 1.1 bouyer
122 1.1 bouyer /*
123 1.37 dholland * The first EXT2FS_NDADDR blocks are direct blocks
124 1.1 bouyer */
125 1.37 dholland if (bn < EXT2FS_NDADDR) {
126 1.19 fvdl /* XXX ondisk32 */
127 1.2 bouyer nb = fs2h32(ip->i_e2fs_blocks[bn]);
128 1.1 bouyer if (nb != 0) {
129 1.7 chs
130 1.7 chs /*
131 1.7 chs * the block is already allocated, just read it.
132 1.7 chs */
133 1.7 chs
134 1.7 chs if (bpp != NULL) {
135 1.40 maxv error = bread(vp, bn, fs->e2fs_bsize,
136 1.33 hannken B_MODIFY, &bp);
137 1.7 chs if (error) {
138 1.41 christos return error;
139 1.7 chs }
140 1.7 chs *bpp = bp;
141 1.1 bouyer }
142 1.41 christos return 0;
143 1.7 chs }
144 1.7 chs
145 1.7 chs /*
146 1.7 chs * allocate a new direct block.
147 1.7 chs */
148 1.7 chs
149 1.7 chs error = ext2fs_alloc(ip, bn,
150 1.7 chs ext2fs_blkpref(ip, bn, bn, &ip->i_e2fs_blocks[0]),
151 1.7 chs cred, &newb);
152 1.7 chs if (error)
153 1.41 christos return error;
154 1.7 chs ip->i_e2fs_last_lblk = lbn;
155 1.7 chs ip->i_e2fs_last_blk = newb;
156 1.19 fvdl /* XXX ondisk32 */
157 1.19 fvdl ip->i_e2fs_blocks[bn] = h2fs32((int32_t)newb);
158 1.7 chs ip->i_flag |= IN_CHANGE | IN_UPDATE;
159 1.7 chs if (bpp != NULL) {
160 1.1 bouyer bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
161 1.38 dholland bp->b_blkno = EXT2_FSBTODB(fs, newb);
162 1.1 bouyer if (flags & B_CLRBUF)
163 1.1 bouyer clrbuf(bp);
164 1.7 chs *bpp = bp;
165 1.1 bouyer }
166 1.41 christos return 0;
167 1.1 bouyer }
168 1.1 bouyer /*
169 1.1 bouyer * Determine the number of levels of indirection.
170 1.1 bouyer */
171 1.1 bouyer pref = 0;
172 1.1 bouyer if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
173 1.41 christos return error;
174 1.1 bouyer #ifdef DIAGNOSTIC
175 1.1 bouyer if (num < 1)
176 1.41 christos panic("%s: ufs_getlbns returned indirect block\n", __func__);
177 1.1 bouyer #endif
178 1.1 bouyer /*
179 1.1 bouyer * Fetch the first indirect block allocating if necessary.
180 1.1 bouyer */
181 1.1 bouyer --num;
182 1.19 fvdl /* XXX ondisk32 */
183 1.37 dholland nb = fs2h32(ip->i_e2fs_blocks[EXT2FS_NDADDR + indirs[0].in_off]);
184 1.3 fvdl allocib = NULL;
185 1.3 fvdl allocblk = allociblk;
186 1.1 bouyer if (nb == 0) {
187 1.19 fvdl pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
188 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
189 1.1 bouyer if (error)
190 1.41 christos return error;
191 1.1 bouyer nb = newb;
192 1.3 fvdl *allocblk++ = nb;
193 1.1 bouyer ip->i_e2fs_last_blk = newb;
194 1.1 bouyer bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0);
195 1.38 dholland bp->b_blkno = EXT2_FSBTODB(fs, newb);
196 1.1 bouyer clrbuf(bp);
197 1.1 bouyer /*
198 1.1 bouyer * Write synchronously so that indirect blocks
199 1.1 bouyer * never point at garbage.
200 1.1 bouyer */
201 1.3 fvdl if ((error = bwrite(bp)) != 0)
202 1.3 fvdl goto fail;
203 1.5 mycroft unwindidx = 0;
204 1.37 dholland allocib = &ip->i_e2fs_blocks[EXT2FS_NDADDR + indirs[0].in_off];
205 1.19 fvdl /* XXX ondisk32 */
206 1.19 fvdl *allocib = h2fs32((int32_t)newb);
207 1.1 bouyer ip->i_flag |= IN_CHANGE | IN_UPDATE;
208 1.1 bouyer }
209 1.1 bouyer /*
210 1.1 bouyer * Fetch through the indirect blocks, allocating as necessary.
211 1.1 bouyer */
212 1.1 bouyer for (i = 1;;) {
213 1.1 bouyer error = bread(vp,
214 1.40 maxv indirs[i].in_lbn, (int)fs->e2fs_bsize, 0, &bp);
215 1.1 bouyer if (error) {
216 1.3 fvdl goto fail;
217 1.1 bouyer }
218 1.19 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
219 1.2 bouyer nb = fs2h32(bap[indirs[i].in_off]);
220 1.1 bouyer if (i == num)
221 1.1 bouyer break;
222 1.5 mycroft i++;
223 1.1 bouyer if (nb != 0) {
224 1.32 ad brelse(bp, 0);
225 1.1 bouyer continue;
226 1.1 bouyer }
227 1.19 fvdl pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
228 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
229 1.1 bouyer if (error) {
230 1.32 ad brelse(bp, 0);
231 1.3 fvdl goto fail;
232 1.1 bouyer }
233 1.1 bouyer nb = newb;
234 1.3 fvdl *allocblk++ = nb;
235 1.1 bouyer ip->i_e2fs_last_blk = newb;
236 1.1 bouyer nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0);
237 1.38 dholland nbp->b_blkno = EXT2_FSBTODB(fs, nb);
238 1.1 bouyer clrbuf(nbp);
239 1.1 bouyer /*
240 1.1 bouyer * Write synchronously so that indirect blocks
241 1.1 bouyer * never point at garbage.
242 1.1 bouyer */
243 1.1 bouyer if ((error = bwrite(nbp)) != 0) {
244 1.32 ad brelse(bp, 0);
245 1.3 fvdl goto fail;
246 1.1 bouyer }
247 1.5 mycroft if (unwindidx < 0)
248 1.5 mycroft unwindidx = i - 1;
249 1.19 fvdl /* XXX ondisk32 */
250 1.19 fvdl bap[indirs[i - 1].in_off] = h2fs32((int32_t)nb);
251 1.1 bouyer /*
252 1.1 bouyer * If required, write synchronously, otherwise use
253 1.1 bouyer * delayed write.
254 1.1 bouyer */
255 1.1 bouyer if (flags & B_SYNC) {
256 1.1 bouyer bwrite(bp);
257 1.1 bouyer } else {
258 1.1 bouyer bdwrite(bp);
259 1.1 bouyer }
260 1.1 bouyer }
261 1.1 bouyer /*
262 1.1 bouyer * Get the data block, allocating if necessary.
263 1.1 bouyer */
264 1.1 bouyer if (nb == 0) {
265 1.5 mycroft pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
266 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
267 1.1 bouyer if (error) {
268 1.32 ad brelse(bp, 0);
269 1.3 fvdl goto fail;
270 1.1 bouyer }
271 1.1 bouyer nb = newb;
272 1.3 fvdl *allocblk++ = nb;
273 1.1 bouyer ip->i_e2fs_last_lblk = lbn;
274 1.1 bouyer ip->i_e2fs_last_blk = newb;
275 1.19 fvdl /* XXX ondisk32 */
276 1.19 fvdl bap[indirs[num].in_off] = h2fs32((int32_t)nb);
277 1.1 bouyer /*
278 1.1 bouyer * If required, write synchronously, otherwise use
279 1.1 bouyer * delayed write.
280 1.1 bouyer */
281 1.1 bouyer if (flags & B_SYNC) {
282 1.1 bouyer bwrite(bp);
283 1.1 bouyer } else {
284 1.1 bouyer bdwrite(bp);
285 1.1 bouyer }
286 1.7 chs if (bpp != NULL) {
287 1.7 chs nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
288 1.38 dholland nbp->b_blkno = EXT2_FSBTODB(fs, nb);
289 1.7 chs if (flags & B_CLRBUF)
290 1.7 chs clrbuf(nbp);
291 1.7 chs *bpp = nbp;
292 1.7 chs }
293 1.41 christos return 0;
294 1.1 bouyer }
295 1.32 ad brelse(bp, 0);
296 1.7 chs if (bpp != NULL) {
297 1.7 chs if (flags & B_CLRBUF) {
298 1.40 maxv error = bread(vp, lbn, (int)fs->e2fs_bsize,
299 1.33 hannken B_MODIFY, &nbp);
300 1.7 chs if (error) {
301 1.7 chs goto fail;
302 1.7 chs }
303 1.7 chs } else {
304 1.7 chs nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
305 1.38 dholland nbp->b_blkno = EXT2_FSBTODB(fs, nb);
306 1.1 bouyer }
307 1.7 chs *bpp = nbp;
308 1.1 bouyer }
309 1.41 christos return 0;
310 1.3 fvdl fail:
311 1.3 fvdl /*
312 1.3 fvdl * If we have failed part way through block allocation, we
313 1.3 fvdl * have to deallocate any indirect blocks that we have allocated.
314 1.3 fvdl */
315 1.3 fvdl for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
316 1.3 fvdl ext2fs_blkfree(ip, *blkp);
317 1.3 fvdl deallocated += fs->e2fs_bsize;
318 1.3 fvdl }
319 1.5 mycroft if (unwindidx >= 0) {
320 1.5 mycroft if (unwindidx == 0) {
321 1.5 mycroft *allocib = 0;
322 1.5 mycroft } else {
323 1.5 mycroft int r;
324 1.25 perry
325 1.25 perry r = bread(vp, indirs[unwindidx].in_lbn,
326 1.40 maxv (int)fs->e2fs_bsize, B_MODIFY, &bp);
327 1.5 mycroft if (r) {
328 1.41 christos panic("%s: Could not unwind indirect block, "
329 1.41 christos "error %d", __func__, r);
330 1.5 mycroft } else {
331 1.19 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
332 1.5 mycroft bap[indirs[unwindidx].in_off] = 0;
333 1.5 mycroft if (flags & B_SYNC)
334 1.5 mycroft bwrite(bp);
335 1.5 mycroft else
336 1.5 mycroft bdwrite(bp);
337 1.5 mycroft }
338 1.5 mycroft }
339 1.5 mycroft for (i = unwindidx + 1; i <= num; i++) {
340 1.5 mycroft bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
341 1.5 mycroft 0, 0);
342 1.32 ad brelse(bp, BC_INVAL);
343 1.5 mycroft }
344 1.5 mycroft }
345 1.3 fvdl if (deallocated) {
346 1.35 jakllsch ext2fs_setnblock(ip, ext2fs_nblock(ip) - btodb(deallocated));
347 1.3 fvdl ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
348 1.3 fvdl }
349 1.7 chs return error;
350 1.7 chs }
351 1.7 chs
352 1.7 chs int
353 1.11 chs ext2fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
354 1.29 elad kauth_cred_t cred)
355 1.7 chs {
356 1.7 chs struct inode *ip = VTOI(vp);
357 1.7 chs struct m_ext2fs *fs = ip->i_e2fs;
358 1.7 chs int error, delta, bshift, bsize;
359 1.11 chs UVMHIST_FUNC("ext2fs_gop_alloc"); UVMHIST_CALLED(ubchist);
360 1.7 chs
361 1.7 chs bshift = fs->e2fs_bshift;
362 1.7 chs bsize = 1 << bshift;
363 1.7 chs
364 1.7 chs delta = off & (bsize - 1);
365 1.7 chs off -= delta;
366 1.7 chs len += delta;
367 1.7 chs
368 1.7 chs while (len > 0) {
369 1.42 riastrad bsize = uimin(bsize, len);
370 1.7 chs UVMHIST_LOG(ubchist, "off 0x%x len 0x%x bsize 0x%x",
371 1.7 chs off, len, bsize, 0);
372 1.7 chs
373 1.39 dholland error = ext2fs_balloc(ip, ext2_lblkno(fs, off), bsize, cred,
374 1.11 chs NULL, flags);
375 1.7 chs if (error) {
376 1.41 christos UVMHIST_LOG(ubchist, "error %d", error, 0, 0, 0);
377 1.7 chs return error;
378 1.7 chs }
379 1.7 chs
380 1.7 chs /*
381 1.27 yamt * increase file size now, ext2fs_balloc() requires that
382 1.7 chs * EOF be up-to-date before each call.
383 1.7 chs */
384 1.7 chs
385 1.23 ws if (ext2fs_size(ip) < off + bsize) {
386 1.23 ws UVMHIST_LOG(ubchist, "old 0x%lx%8lx new 0x%lx%8lx",
387 1.23 ws /* Note that arguments are always cast to u_long. */
388 1.23 ws ext2fs_size(ip) >> 32,
389 1.23 ws ext2fs_size(ip) & 0xffffffff,
390 1.23 ws (off + bsize) >> 32,
391 1.23 ws (off + bsize) & 0xffffffff);
392 1.23 ws error = ext2fs_setsize(ip, off + bsize);
393 1.23 ws if (error) {
394 1.41 christos UVMHIST_LOG(ubchist, "error %d",
395 1.41 christos error, 0, 0, 0);
396 1.23 ws return error;
397 1.23 ws }
398 1.7 chs }
399 1.7 chs
400 1.7 chs off += bsize;
401 1.7 chs len -= bsize;
402 1.7 chs }
403 1.7 chs return 0;
404 1.1 bouyer }
405