Deleted Added
full compact
ffs_balloc.c (33134) ffs_balloc.c (34266)
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 17 unchanged lines hidden (view full) ---

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
31 * SUCH DAMAGE.
32 *
33 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 17 unchanged lines hidden (view full) ---

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
31 * SUCH DAMAGE.
32 *
33 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
34 * $Id: ffs_balloc.c,v 1.18 1998/02/04 22:33:31 eivind Exp $
34 * $Id: ffs_balloc.c,v 1.19 1998/02/06 12:14:14 eivind Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/buf.h>
40#include <sys/lock.h>
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/buf.h>
40#include <sys/lock.h>
41#include <sys/mount.h>
41#include <sys/vnode.h>
42
43#include <ufs/ufs/quota.h>
44#include <ufs/ufs/inode.h>
45#include <ufs/ufs/ufs_extern.h>
46
47#include <ufs/ffs/fs.h>
48#include <ufs/ffs/ffs_extern.h>
49
50/*
51 * Balloc defines the structure of file system storage
52 * by allocating the physical blocks on a device given
53 * the inode and the logical block number in a file.
54 */
55int
42#include <sys/vnode.h>
43
44#include <ufs/ufs/quota.h>
45#include <ufs/ufs/inode.h>
46#include <ufs/ufs/ufs_extern.h>
47
48#include <ufs/ffs/fs.h>
49#include <ufs/ffs/ffs_extern.h>
50
51/*
52 * Balloc defines the structure of file system storage
53 * by allocating the physical blocks on a device given
54 * the inode and the logical block number in a file.
55 */
56int
56ffs_balloc(ip, lbn, size, cred, bpp, flags)
57ffs_balloc(ap)
58 struct vop_balloc_args /* {
59 struct inode *a_ip;
60 ufs_daddr_t a_lbn;
61 int a_size;
62 struct ucred *a_cred;
63 int a_flags;
64 struct buf *a_bpp;
65 } */ *ap;
66{
57 register struct inode *ip;
58 register ufs_daddr_t lbn;
59 int size;
60 struct ucred *cred;
67 register struct inode *ip;
68 register ufs_daddr_t lbn;
69 int size;
70 struct ucred *cred;
61 struct buf **bpp;
62 int flags;
71 int flags;
63{
64 register struct fs *fs;
65 register ufs_daddr_t nb;
72 struct fs *fs;
73 ufs_daddr_t nb;
66 struct buf *bp, *nbp;
67 struct vnode *vp = ITOV(ip);
68 struct indir indirs[NIADDR + 2];
69 ufs_daddr_t newb, *bap, pref;
70 int deallocated, osize, nsize, num, i, error;
71 ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
72
74 struct buf *bp, *nbp;
75 struct vnode *vp = ITOV(ip);
76 struct indir indirs[NIADDR + 2];
77 ufs_daddr_t newb, *bap, pref;
78 int deallocated, osize, nsize, num, i, error;
79 ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
80
73 *bpp = NULL;
81 vp = ap->a_vp;
82 ip = VTOI(vp);
83 fs = ip->i_fs;
84 lbn = lblkno(fs, ap->a_startoffset);
85 size = blkoff(fs, ap->a_startoffset) + ap->a_size;
86 if (size > fs->fs_bsize)
87 panic("ffs_balloc: blk too big");
88 *ap->a_bpp = NULL;
74 if (lbn < 0)
75 return (EFBIG);
89 if (lbn < 0)
90 return (EFBIG);
76 fs = ip->i_fs;
91 cred = ap->a_cred;
92 flags = ap->a_flags;
77
78 /*
79 * If the next write will extend the file into a new block,
80 * and the file is currently composed of a fragment
81 * this fragment has to be extended to be a full block.
82 */
83 nb = lblkno(fs, ip->i_size);
84 if (nb < NDADDR && nb < lbn) {
85 osize = blksize(fs, ip, nb);
86 if (osize < fs->fs_bsize && osize > 0) {
87 error = ffs_realloccg(ip, nb,
88 ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
89 osize, (int)fs->fs_bsize, cred, &bp);
90 if (error)
91 return (error);
93
94 /*
95 * If the next write will extend the file into a new block,
96 * and the file is currently composed of a fragment
97 * this fragment has to be extended to be a full block.
98 */
99 nb = lblkno(fs, ip->i_size);
100 if (nb < NDADDR && nb < lbn) {
101 osize = blksize(fs, ip, nb);
102 if (osize < fs->fs_bsize && osize > 0) {
103 error = ffs_realloccg(ip, nb,
104 ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
105 osize, (int)fs->fs_bsize, cred, &bp);
106 if (error)
107 return (error);
108 if (DOINGSOFTDEP(vp))
109 softdep_setup_allocdirect(ip, nb,
110 dbtofsb(fs, bp->b_blkno), ip->i_db[nb],
111 fs->fs_bsize, osize, bp);
92 ip->i_size = smalllblktosize(fs, nb + 1);
93 ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
94 ip->i_flag |= IN_CHANGE | IN_UPDATE;
95 if (flags & B_SYNC)
96 bwrite(bp);
97 else
98 bawrite(bp);
99 }

--- 5 unchanged lines hidden (view full) ---

105 nb = ip->i_db[lbn];
106 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
107 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
108 if (error) {
109 brelse(bp);
110 return (error);
111 }
112 bp->b_blkno = fsbtodb(fs, nb);
112 ip->i_size = smalllblktosize(fs, nb + 1);
113 ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
114 ip->i_flag |= IN_CHANGE | IN_UPDATE;
115 if (flags & B_SYNC)
116 bwrite(bp);
117 else
118 bawrite(bp);
119 }

--- 5 unchanged lines hidden (view full) ---

125 nb = ip->i_db[lbn];
126 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
127 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
128 if (error) {
129 brelse(bp);
130 return (error);
131 }
132 bp->b_blkno = fsbtodb(fs, nb);
113 *bpp = bp;
133 *ap->a_bpp = bp;
114 return (0);
115 }
116 if (nb != 0) {
117 /*
118 * Consider need to reallocate a fragment.
119 */
120 osize = fragroundup(fs, blkoff(fs, ip->i_size));
121 nsize = fragroundup(fs, size);

--- 5 unchanged lines hidden (view full) ---

127 }
128 bp->b_blkno = fsbtodb(fs, nb);
129 } else {
130 error = ffs_realloccg(ip, lbn,
131 ffs_blkpref(ip, lbn, (int)lbn,
132 &ip->i_db[0]), osize, nsize, cred, &bp);
133 if (error)
134 return (error);
134 return (0);
135 }
136 if (nb != 0) {
137 /*
138 * Consider need to reallocate a fragment.
139 */
140 osize = fragroundup(fs, blkoff(fs, ip->i_size));
141 nsize = fragroundup(fs, size);

--- 5 unchanged lines hidden (view full) ---

147 }
148 bp->b_blkno = fsbtodb(fs, nb);
149 } else {
150 error = ffs_realloccg(ip, lbn,
151 ffs_blkpref(ip, lbn, (int)lbn,
152 &ip->i_db[0]), osize, nsize, cred, &bp);
153 if (error)
154 return (error);
155 if (DOINGSOFTDEP(vp))
156 softdep_setup_allocdirect(ip, lbn,
157 dbtofsb(fs, bp->b_blkno), nb,
158 nsize, osize, bp);
135 }
136 } else {
137 if (ip->i_size < smalllblktosize(fs, lbn + 1))
138 nsize = fragroundup(fs, size);
139 else
140 nsize = fs->fs_bsize;
141 error = ffs_alloc(ip, lbn,
142 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
143 nsize, cred, &newb);
144 if (error)
145 return (error);
146 bp = getblk(vp, lbn, nsize, 0, 0);
147 bp->b_blkno = fsbtodb(fs, newb);
148 if (flags & B_CLRBUF)
149 vfs_bio_clrbuf(bp);
159 }
160 } else {
161 if (ip->i_size < smalllblktosize(fs, lbn + 1))
162 nsize = fragroundup(fs, size);
163 else
164 nsize = fs->fs_bsize;
165 error = ffs_alloc(ip, lbn,
166 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
167 nsize, cred, &newb);
168 if (error)
169 return (error);
170 bp = getblk(vp, lbn, nsize, 0, 0);
171 bp->b_blkno = fsbtodb(fs, newb);
172 if (flags & B_CLRBUF)
173 vfs_bio_clrbuf(bp);
174 if (DOINGSOFTDEP(vp))
175 softdep_setup_allocdirect(ip, lbn, newb, 0,
176 nsize, 0, bp);
150 }
151 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
152 ip->i_flag |= IN_CHANGE | IN_UPDATE;
177 }
178 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
179 ip->i_flag |= IN_CHANGE | IN_UPDATE;
153 *bpp = bp;
180 *ap->a_bpp = bp;
154 return (0);
155 }
156 /*
157 * Determine the number of levels of indirection.
158 */
159 pref = 0;
160 if (error = ufs_getlbns(vp, lbn, indirs, &num))
161 return(error);

--- 13 unchanged lines hidden (view full) ---

175 if (error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
176 cred, &newb))
177 return (error);
178 nb = newb;
179 *allocblk++ = nb;
180 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
181 bp->b_blkno = fsbtodb(fs, nb);
182 vfs_bio_clrbuf(bp);
181 return (0);
182 }
183 /*
184 * Determine the number of levels of indirection.
185 */
186 pref = 0;
187 if (error = ufs_getlbns(vp, lbn, indirs, &num))
188 return(error);

--- 13 unchanged lines hidden (view full) ---

202 if (error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
203 cred, &newb))
204 return (error);
205 nb = newb;
206 *allocblk++ = nb;
207 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
208 bp->b_blkno = fsbtodb(fs, nb);
209 vfs_bio_clrbuf(bp);
183 /*
184 * Write synchronously so that indirect blocks
185 * never point at garbage.
186 */
187 if (error = bwrite(bp))
188 goto fail;
210 if (DOINGSOFTDEP(vp)) {
211 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
212 newb, 0, fs->fs_bsize, 0, bp);
213 bdwrite(bp);
214 } else {
215 /*
216 * Write synchronously so that indirect blocks
217 * never point at garbage.
218 */
219 if (error = bwrite(bp))
220 goto fail;
221 }
189 allocib = &ip->i_ib[indirs[0].in_off];
190 *allocib = nb;
191 ip->i_flag |= IN_CHANGE | IN_UPDATE;
192 }
193 /*
194 * Fetch through the indirect blocks, allocating as necessary.
195 */
196 for (i = 1;;) {

--- 19 unchanged lines hidden (view full) ---

216 brelse(bp);
217 goto fail;
218 }
219 nb = newb;
220 *allocblk++ = nb;
221 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
222 nbp->b_blkno = fsbtodb(fs, nb);
223 vfs_bio_clrbuf(nbp);
222 allocib = &ip->i_ib[indirs[0].in_off];
223 *allocib = nb;
224 ip->i_flag |= IN_CHANGE | IN_UPDATE;
225 }
226 /*
227 * Fetch through the indirect blocks, allocating as necessary.
228 */
229 for (i = 1;;) {

--- 19 unchanged lines hidden (view full) ---

249 brelse(bp);
250 goto fail;
251 }
252 nb = newb;
253 *allocblk++ = nb;
254 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
255 nbp->b_blkno = fsbtodb(fs, nb);
256 vfs_bio_clrbuf(nbp);
224 /*
225 * Write synchronously so that indirect blocks
226 * never point at garbage.
227 */
228 if (error = bwrite(nbp)) {
229 brelse(bp);
230 goto fail;
257 if (DOINGSOFTDEP(vp)) {
258 softdep_setup_allocindir_meta(nbp, ip, bp,
259 indirs[i - 1].in_off, nb);
260 bdwrite(nbp);
261 } else {
262 /*
263 * Write synchronously so that indirect blocks
264 * never point at garbage.
265 */
266 if (error = bwrite(nbp)) {
267 brelse(bp);
268 goto fail;
269 }
231 }
232 bap[indirs[i - 1].in_off] = nb;
233 /*
234 * If required, write synchronously, otherwise use
235 * delayed write.
236 */
237 if (flags & B_SYNC) {
238 bwrite(bp);

--- 15 unchanged lines hidden (view full) ---

254 goto fail;
255 }
256 nb = newb;
257 *allocblk++ = nb;
258 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
259 nbp->b_blkno = fsbtodb(fs, nb);
260 if (flags & B_CLRBUF)
261 vfs_bio_clrbuf(nbp);
270 }
271 bap[indirs[i - 1].in_off] = nb;
272 /*
273 * If required, write synchronously, otherwise use
274 * delayed write.
275 */
276 if (flags & B_SYNC) {
277 bwrite(bp);

--- 15 unchanged lines hidden (view full) ---

293 goto fail;
294 }
295 nb = newb;
296 *allocblk++ = nb;
297 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
298 nbp->b_blkno = fsbtodb(fs, nb);
299 if (flags & B_CLRBUF)
300 vfs_bio_clrbuf(nbp);
301 if (DOINGSOFTDEP(vp))
302 softdep_setup_allocindir_page(ip, lbn, bp,
303 indirs[i].in_off, nb, 0, nbp);
262 bap[indirs[i].in_off] = nb;
263 /*
264 * If required, write synchronously, otherwise use
265 * delayed write.
266 */
267 if (flags & B_SYNC) {
268 bwrite(bp);
269 } else {
270 if (bp->b_bufsize == fs->fs_bsize)
271 bp->b_flags |= B_CLUSTEROK;
272 bdwrite(bp);
273 }
304 bap[indirs[i].in_off] = nb;
305 /*
306 * If required, write synchronously, otherwise use
307 * delayed write.
308 */
309 if (flags & B_SYNC) {
310 bwrite(bp);
311 } else {
312 if (bp->b_bufsize == fs->fs_bsize)
313 bp->b_flags |= B_CLUSTEROK;
314 bdwrite(bp);
315 }
274 *bpp = nbp;
316 *ap->a_bpp = nbp;
275 return (0);
276 }
277 brelse(bp);
278 if (flags & B_CLRBUF) {
279 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
280 if (error) {
281 brelse(nbp);
282 goto fail;
283 }
284 } else {
285 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
286 nbp->b_blkno = fsbtodb(fs, nb);
287 }
317 return (0);
318 }
319 brelse(bp);
320 if (flags & B_CLRBUF) {
321 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
322 if (error) {
323 brelse(nbp);
324 goto fail;
325 }
326 } else {
327 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
328 nbp->b_blkno = fsbtodb(fs, nb);
329 }
288 *bpp = nbp;
330 *ap->a_bpp = nbp;
289 return (0);
290fail:
291 /*
292 * If we have failed part way through block allocation, we
293 * have to deallocate any indirect blocks that we have allocated.
294 */
295 for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
296 ffs_blkfree(ip, *blkp, fs->fs_bsize);

--- 16 unchanged lines hidden ---
331 return (0);
332fail:
333 /*
334 * If we have failed part way through block allocation, we
335 * have to deallocate any indirect blocks that we have allocated.
336 */
337 for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
338 ffs_blkfree(ip, *blkp, fs->fs_bsize);

--- 16 unchanged lines hidden ---