2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
34 * $FreeBSD: src/sys/ufs/ffs/ffs_balloc.c,v 1.26.2.1 2002/10/10 19:48:20 dillon Exp $
35 * $DragonFly: src/sys/vfs/ufs/ffs_balloc.c,v 1.2 2003/06/17 04:28:59 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
45 #include <ufs/ufs/quota.h>
46 #include <ufs/ufs/inode.h>
47 #include <ufs/ufs/ufs_extern.h>
49 #include <ufs/ffs/fs.h>
50 #include <ufs/ffs/ffs_extern.h>
53 * Balloc defines the structure of file system storage
54 * by allocating the physical blocks on a device given
55 * the inode and the logical block number in a file.
59 struct vop_balloc_args /* {
77 struct indir indirs[NIADDR + 2];
78 ufs_daddr_t newb, *bap, pref;
79 int deallocated, osize, nsize, num, i, error;
80 ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
82 struct proc *p = curproc; /* XXX */
87 lbn = lblkno(fs, ap->a_startoffset);
88 size = blkoff(fs, ap->a_startoffset) + ap->a_size;
89 if (size > fs->fs_bsize)
90 panic("ffs_balloc: blk too big");
98 * If the next write will extend the file into a new block,
99 * and the file is currently composed of a fragment
100 * this fragment has to be extended to be a full block.
102 nb = lblkno(fs, ip->i_size);
103 if (nb < NDADDR && nb < lbn) {
104 osize = blksize(fs, ip, nb);
105 if (osize < fs->fs_bsize && osize > 0) {
106 error = ffs_realloccg(ip, nb,
107 ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
108 osize, (int)fs->fs_bsize, cred, &bp);
111 if (DOINGSOFTDEP(vp))
112 softdep_setup_allocdirect(ip, nb,
113 dbtofsb(fs, bp->b_blkno), ip->i_db[nb],
114 fs->fs_bsize, osize, bp);
115 ip->i_size = smalllblktosize(fs, nb + 1);
116 ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
117 ip->i_flag |= IN_CHANGE | IN_UPDATE;
125 * The first NDADDR blocks are direct blocks
129 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
130 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
135 bp->b_blkno = fsbtodb(fs, nb);
141 * Consider need to reallocate a fragment.
143 osize = fragroundup(fs, blkoff(fs, ip->i_size));
144 nsize = fragroundup(fs, size);
145 if (nsize <= osize) {
146 error = bread(vp, lbn, osize, NOCRED, &bp);
151 bp->b_blkno = fsbtodb(fs, nb);
153 error = ffs_realloccg(ip, lbn,
154 ffs_blkpref(ip, lbn, (int)lbn,
155 &ip->i_db[0]), osize, nsize, cred, &bp);
158 if (DOINGSOFTDEP(vp))
159 softdep_setup_allocdirect(ip, lbn,
160 dbtofsb(fs, bp->b_blkno), nb,
164 if (ip->i_size < smalllblktosize(fs, lbn + 1))
165 nsize = fragroundup(fs, size);
167 nsize = fs->fs_bsize;
168 error = ffs_alloc(ip, lbn,
169 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
173 bp = getblk(vp, lbn, nsize, 0, 0);
174 bp->b_blkno = fsbtodb(fs, newb);
175 if (flags & B_CLRBUF)
177 if (DOINGSOFTDEP(vp))
178 softdep_setup_allocdirect(ip, lbn, newb, 0,
181 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
182 ip->i_flag |= IN_CHANGE | IN_UPDATE;
187 * Determine the number of levels of indirection.
190 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
194 panic ("ffs_balloc: ufs_bmaparray returned indirect block");
197 * Fetch the first indirect block allocating if necessary.
200 nb = ip->i_ib[indirs[0].in_off];
202 allocblk = allociblk;
204 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
205 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
210 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
211 bp->b_blkno = fsbtodb(fs, nb);
213 if (DOINGSOFTDEP(vp)) {
214 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
215 newb, 0, fs->fs_bsize, 0, bp);
219 * Write synchronously so that indirect blocks
220 * never point at garbage.
224 else if ((error = bwrite(bp)) != 0)
227 allocib = &ip->i_ib[indirs[0].in_off];
229 ip->i_flag |= IN_CHANGE | IN_UPDATE;
232 * Fetch through the indirect blocks, allocating as necessary.
236 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
241 bap = (ufs_daddr_t *)bp->b_data;
242 nb = bap[indirs[i].in_off];
251 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
253 ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) != 0) {
259 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
260 nbp->b_blkno = fsbtodb(fs, nb);
262 if (DOINGSOFTDEP(vp)) {
263 softdep_setup_allocindir_meta(nbp, ip, bp,
264 indirs[i - 1].in_off, nb);
268 * Write synchronously so that indirect blocks
269 * never point at garbage.
271 if ((error = bwrite(nbp)) != 0) {
276 bap[indirs[i - 1].in_off] = nb;
277 if (allocib == NULL && unwindidx < 0)
280 * If required, write synchronously, otherwise use
283 if (flags & B_SYNC) {
286 if (bp->b_bufsize == fs->fs_bsize)
287 bp->b_flags |= B_CLUSTEROK;
292 * Get the data block, allocating if necessary.
295 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
296 error = ffs_alloc(ip,
297 lbn, pref, (int)fs->fs_bsize, cred, &newb);
304 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
305 nbp->b_blkno = fsbtodb(fs, nb);
306 if (flags & B_CLRBUF)
308 if (DOINGSOFTDEP(vp))
309 softdep_setup_allocindir_page(ip, lbn, bp,
310 indirs[i].in_off, nb, 0, nbp);
311 bap[indirs[i].in_off] = nb;
313 * If required, write synchronously, otherwise use
316 if (flags & B_SYNC) {
319 if (bp->b_bufsize == fs->fs_bsize)
320 bp->b_flags |= B_CLUSTEROK;
328 * If requested clear invalid portions of the buffer. If we
329 * have to do a read-before-write (typical if B_CLRBUF is set),
330 * try to do some read-ahead in the sequential case to reduce
331 * the number of I/O transactions.
333 if (flags & B_CLRBUF) {
334 int seqcount = (flags & B_SEQMASK) >> B_SEQSHIFT;
336 (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
337 error = cluster_read(vp, ip->i_size, lbn,
338 (int)fs->fs_bsize, NOCRED,
339 MAXBSIZE, seqcount, &nbp);
341 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
348 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
349 nbp->b_blkno = fsbtodb(fs, nb);
355 * If we have failed part way through block allocation, we
356 * have to deallocate any indirect blocks that we have allocated.
357 * We have to fsync the file before we start to get rid of all
358 * of its dependencies so that we do not leave them dangling.
359 * We have to sync it at the end so that the soft updates code
360 * does not find any untracked changes. Although this is really
361 * slow, running out of disk space is not expected to be a common
362 * occurence. The error return from fsync is ignored as we already
363 * have an error to return to the user.
365 (void) VOP_FSYNC(vp, cred, MNT_WAIT, p);
366 for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
367 ffs_blkfree(ip, *blkp, fs->fs_bsize);
368 deallocated += fs->fs_bsize;
370 if (allocib != NULL) {
372 } else if (unwindidx >= 0) {
375 r = bread(vp, indirs[unwindidx].in_lbn,
376 (int)fs->fs_bsize, NOCRED, &bp);
378 panic("Could not unwind indirect block, error %d", r);
381 bap = (ufs_daddr_t *)bp->b_data;
382 bap[indirs[unwindidx].in_off] = 0;
383 if (flags & B_SYNC) {
386 if (bp->b_bufsize == fs->fs_bsize)
387 bp->b_flags |= B_CLUSTEROK;
395 * Restore user's disk quota because allocation failed.
397 (void) chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
399 ip->i_blocks -= btodb(deallocated);
400 ip->i_flag |= IN_CHANGE | IN_UPDATE;
402 (void) VOP_FSYNC(vp, cred, MNT_WAIT, p);