Deleted Added
full compact
ext2_balloc.c (228583) ext2_balloc.c (245820)
1/*-
2 * modified for Lites 1.1
3 *
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
6 */
7/*-
8 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93
1/*-
2 * modified for Lites 1.1
3 *
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
6 */
7/*-
8 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93
36 * $FreeBSD: head/sys/fs/ext2fs/ext2_balloc.c 228583 2011-12-16 15:47:43Z pfg $
36 * $FreeBSD: head/sys/fs/ext2fs/ext2_balloc.c 245820 2013-01-22 18:54:03Z pfg $
37 */
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/bio.h>
42#include <sys/buf.h>
43#include <sys/lock.h>
44#include <sys/mount.h>
45#include <sys/vnode.h>
46
47#include <fs/ext2fs/inode.h>
48#include <fs/ext2fs/ext2fs.h>
49#include <fs/ext2fs/fs.h>
50#include <fs/ext2fs/ext2_extern.h>
51#include <fs/ext2fs/ext2_mount.h>
52/*
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.
56 */
57int
58ext2_balloc(ip, lbn, size, cred, bpp, flags)
59 struct inode *ip;
60 int32_t lbn;
61 int size;
62 struct ucred *cred;
63 struct buf **bpp;
64 int flags;
65{
66 struct m_ext2fs *fs;
67 struct ext2mount *ump;
68 int32_t nb;
69 struct buf *bp, *nbp;
70 struct vnode *vp = ITOV(ip);
71 struct indir indirs[NIADDR + 2];
37 */
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/bio.h>
42#include <sys/buf.h>
43#include <sys/lock.h>
44#include <sys/mount.h>
45#include <sys/vnode.h>
46
47#include <fs/ext2fs/inode.h>
48#include <fs/ext2fs/ext2fs.h>
49#include <fs/ext2fs/fs.h>
50#include <fs/ext2fs/ext2_extern.h>
51#include <fs/ext2fs/ext2_mount.h>
52/*
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.
56 */
57int
58ext2_balloc(ip, lbn, size, cred, bpp, flags)
59 struct inode *ip;
60 int32_t lbn;
61 int size;
62 struct ucred *cred;
63 struct buf **bpp;
64 int flags;
65{
66 struct m_ext2fs *fs;
67 struct ext2mount *ump;
68 int32_t nb;
69 struct buf *bp, *nbp;
70 struct vnode *vp = ITOV(ip);
71 struct indir indirs[NIADDR + 2];
72 int32_t newb, *bap, pref;
72 uint32_t newb, *bap, pref;
73 int osize, nsize, num, i, error;
74
75 *bpp = NULL;
76 if (lbn < 0)
77 return (EFBIG);
78 fs = ip->i_e2fs;
79 ump = ip->i_ump;
80
81 /*
82 * check if this is a sequential block allocation.
83 * If so, increment next_alloc fields to allow ext2_blkpref
84 * to make a good guess
85 */
86 if (lbn == ip->i_next_alloc_block + 1) {
87 ip->i_next_alloc_block++;
88 ip->i_next_alloc_goal++;
89 }
90
91 /*
92 * The first NDADDR blocks are direct blocks
93 */
94 if (lbn < NDADDR) {
95 nb = ip->i_db[lbn];
96 /* no new block is to be allocated, and no need to expand
97 the file */
98 if (nb != 0 && ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
99 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
100 if (error) {
101 brelse(bp);
102 return (error);
103 }
104 bp->b_blkno = fsbtodb(fs, nb);
105 *bpp = bp;
106 return (0);
107 }
108 if (nb != 0) {
109 /*
110 * Consider need to reallocate a fragment.
111 */
112 osize = fragroundup(fs, blkoff(fs, ip->i_size));
113 nsize = fragroundup(fs, size);
114 if (nsize <= osize) {
115 error = bread(vp, lbn, osize, NOCRED, &bp);
116 if (error) {
117 brelse(bp);
118 return (error);
119 }
120 bp->b_blkno = fsbtodb(fs, nb);
121 } else {
122 /* Godmar thinks: this shouldn't happen w/o fragments */
123 printf("nsize %d(%d) > osize %d(%d) nb %d\n",
124 (int)nsize, (int)size, (int)osize,
125 (int)ip->i_size, (int)nb);
126 panic(
127 "ext2_balloc: Something is terribly wrong");
128/*
129 * please note there haven't been any changes from here on -
130 * FFS seems to work.
131 */
132 }
133 } else {
134 if (ip->i_size < (lbn + 1) * fs->e2fs_bsize)
135 nsize = fragroundup(fs, size);
136 else
137 nsize = fs->e2fs_bsize;
138 EXT2_LOCK(ump);
139 error = ext2_alloc(ip, lbn,
140 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0),
141 nsize, cred, &newb);
142 if (error)
143 return (error);
144 bp = getblk(vp, lbn, nsize, 0, 0, 0);
145 bp->b_blkno = fsbtodb(fs, newb);
146 if (flags & BA_CLRBUF)
147 vfs_bio_clrbuf(bp);
148 }
149 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
150 ip->i_flag |= IN_CHANGE | IN_UPDATE;
151 *bpp = bp;
152 return (0);
153 }
154 /*
155 * Determine the number of levels of indirection.
156 */
157 pref = 0;
158 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0)
159 return (error);
160#ifdef DIAGNOSTIC
161 if (num < 1)
162 panic ("ext2_balloc: ext2_getlbns returned indirect block");
163#endif
164 /*
165 * Fetch the first indirect block allocating if necessary.
166 */
167 --num;
168 nb = ip->i_ib[indirs[0].in_off];
169 if (nb == 0) {
170 EXT2_LOCK(ump);
171 pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
172 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
173 if ((error = ext2_alloc(ip, lbn, pref,
174 (int)fs->e2fs_bsize, cred, &newb)))
175 return (error);
176 nb = newb;
177 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0);
178 bp->b_blkno = fsbtodb(fs, newb);
179 vfs_bio_clrbuf(bp);
180 /*
181 * Write synchronously so that indirect blocks
182 * never point at garbage.
183 */
184 if ((error = bwrite(bp)) != 0) {
185 ext2_blkfree(ip, nb, fs->e2fs_bsize);
186 return (error);
187 }
188 ip->i_ib[indirs[0].in_off] = newb;
189 ip->i_flag |= IN_CHANGE | IN_UPDATE;
190 }
191 /*
192 * Fetch through the indirect blocks, allocating as necessary.
193 */
194 for (i = 1;;) {
195 error = bread(vp,
196 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
197 if (error) {
198 brelse(bp);
199 return (error);
200 }
201 bap = (int32_t *)bp->b_data;
202 nb = bap[indirs[i].in_off];
203 if (i == num)
204 break;
205 i += 1;
206 if (nb != 0) {
207 bqrelse(bp);
208 continue;
209 }
210 EXT2_LOCK(ump);
211 if (pref == 0)
212 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
213 bp->b_lblkno);
214 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb);
215 if (error) {
216 brelse(bp);
217 return (error);
218 }
219 nb = newb;
220 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0);
221 nbp->b_blkno = fsbtodb(fs, nb);
222 vfs_bio_clrbuf(nbp);
223 /*
224 * Write synchronously so that indirect blocks
225 * never point at garbage.
226 */
227 if ((error = bwrite(nbp)) != 0) {
228 ext2_blkfree(ip, nb, fs->e2fs_bsize);
229 EXT2_UNLOCK(ump);
230 brelse(bp);
231 return (error);
232 }
233 bap[indirs[i - 1].in_off] = nb;
234 /*
235 * If required, write synchronously, otherwise use
236 * delayed write.
237 */
238 if (flags & IO_SYNC) {
239 bwrite(bp);
240 } else {
241 if (bp->b_bufsize == fs->e2fs_bsize)
242 bp->b_flags |= B_CLUSTEROK;
243 bdwrite(bp);
244 }
245 }
246 /*
247 * Get the data block, allocating if necessary.
248 */
249 if (nb == 0) {
250 EXT2_LOCK(ump);
251 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
252 bp->b_lblkno);
253 if ((error = ext2_alloc(ip,
254 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) {
255 brelse(bp);
256 return (error);
257 }
258 nb = newb;
259 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
260 nbp->b_blkno = fsbtodb(fs, nb);
261 if (flags & BA_CLRBUF)
262 vfs_bio_clrbuf(nbp);
263 bap[indirs[i].in_off] = nb;
264 /*
265 * If required, write synchronously, otherwise use
266 * delayed write.
267 */
268 if (flags & IO_SYNC) {
269 bwrite(bp);
270 } else {
271 if (bp->b_bufsize == fs->e2fs_bsize)
272 bp->b_flags |= B_CLUSTEROK;
273 bdwrite(bp);
274 }
275 *bpp = nbp;
276 return (0);
277 }
278 brelse(bp);
279 if (flags & BA_CLRBUF) {
280 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
281 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
282 error = cluster_read(vp, ip->i_size, lbn,
283 (int)fs->e2fs_bsize, NOCRED,
284 MAXBSIZE, seqcount, &nbp);
285 } else {
286 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
287 }
288 if (error) {
289 brelse(nbp);
290 return (error);
291 }
292 } else {
293 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
294 nbp->b_blkno = fsbtodb(fs, nb);
295 }
296 *bpp = nbp;
297 return (0);
298}
299
73 int osize, nsize, num, i, error;
74
75 *bpp = NULL;
76 if (lbn < 0)
77 return (EFBIG);
78 fs = ip->i_e2fs;
79 ump = ip->i_ump;
80
81 /*
82 * check if this is a sequential block allocation.
83 * If so, increment next_alloc fields to allow ext2_blkpref
84 * to make a good guess
85 */
86 if (lbn == ip->i_next_alloc_block + 1) {
87 ip->i_next_alloc_block++;
88 ip->i_next_alloc_goal++;
89 }
90
91 /*
92 * The first NDADDR blocks are direct blocks
93 */
94 if (lbn < NDADDR) {
95 nb = ip->i_db[lbn];
96 /* no new block is to be allocated, and no need to expand
97 the file */
98 if (nb != 0 && ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
99 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
100 if (error) {
101 brelse(bp);
102 return (error);
103 }
104 bp->b_blkno = fsbtodb(fs, nb);
105 *bpp = bp;
106 return (0);
107 }
108 if (nb != 0) {
109 /*
110 * Consider need to reallocate a fragment.
111 */
112 osize = fragroundup(fs, blkoff(fs, ip->i_size));
113 nsize = fragroundup(fs, size);
114 if (nsize <= osize) {
115 error = bread(vp, lbn, osize, NOCRED, &bp);
116 if (error) {
117 brelse(bp);
118 return (error);
119 }
120 bp->b_blkno = fsbtodb(fs, nb);
121 } else {
122 /* Godmar thinks: this shouldn't happen w/o fragments */
123 printf("nsize %d(%d) > osize %d(%d) nb %d\n",
124 (int)nsize, (int)size, (int)osize,
125 (int)ip->i_size, (int)nb);
126 panic(
127 "ext2_balloc: Something is terribly wrong");
128/*
129 * please note there haven't been any changes from here on -
130 * FFS seems to work.
131 */
132 }
133 } else {
134 if (ip->i_size < (lbn + 1) * fs->e2fs_bsize)
135 nsize = fragroundup(fs, size);
136 else
137 nsize = fs->e2fs_bsize;
138 EXT2_LOCK(ump);
139 error = ext2_alloc(ip, lbn,
140 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0),
141 nsize, cred, &newb);
142 if (error)
143 return (error);
144 bp = getblk(vp, lbn, nsize, 0, 0, 0);
145 bp->b_blkno = fsbtodb(fs, newb);
146 if (flags & BA_CLRBUF)
147 vfs_bio_clrbuf(bp);
148 }
149 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
150 ip->i_flag |= IN_CHANGE | IN_UPDATE;
151 *bpp = bp;
152 return (0);
153 }
154 /*
155 * Determine the number of levels of indirection.
156 */
157 pref = 0;
158 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0)
159 return (error);
160#ifdef DIAGNOSTIC
161 if (num < 1)
162 panic ("ext2_balloc: ext2_getlbns returned indirect block");
163#endif
164 /*
165 * Fetch the first indirect block allocating if necessary.
166 */
167 --num;
168 nb = ip->i_ib[indirs[0].in_off];
169 if (nb == 0) {
170 EXT2_LOCK(ump);
171 pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
172 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
173 if ((error = ext2_alloc(ip, lbn, pref,
174 (int)fs->e2fs_bsize, cred, &newb)))
175 return (error);
176 nb = newb;
177 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0);
178 bp->b_blkno = fsbtodb(fs, newb);
179 vfs_bio_clrbuf(bp);
180 /*
181 * Write synchronously so that indirect blocks
182 * never point at garbage.
183 */
184 if ((error = bwrite(bp)) != 0) {
185 ext2_blkfree(ip, nb, fs->e2fs_bsize);
186 return (error);
187 }
188 ip->i_ib[indirs[0].in_off] = newb;
189 ip->i_flag |= IN_CHANGE | IN_UPDATE;
190 }
191 /*
192 * Fetch through the indirect blocks, allocating as necessary.
193 */
194 for (i = 1;;) {
195 error = bread(vp,
196 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
197 if (error) {
198 brelse(bp);
199 return (error);
200 }
201 bap = (int32_t *)bp->b_data;
202 nb = bap[indirs[i].in_off];
203 if (i == num)
204 break;
205 i += 1;
206 if (nb != 0) {
207 bqrelse(bp);
208 continue;
209 }
210 EXT2_LOCK(ump);
211 if (pref == 0)
212 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
213 bp->b_lblkno);
214 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb);
215 if (error) {
216 brelse(bp);
217 return (error);
218 }
219 nb = newb;
220 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0);
221 nbp->b_blkno = fsbtodb(fs, nb);
222 vfs_bio_clrbuf(nbp);
223 /*
224 * Write synchronously so that indirect blocks
225 * never point at garbage.
226 */
227 if ((error = bwrite(nbp)) != 0) {
228 ext2_blkfree(ip, nb, fs->e2fs_bsize);
229 EXT2_UNLOCK(ump);
230 brelse(bp);
231 return (error);
232 }
233 bap[indirs[i - 1].in_off] = nb;
234 /*
235 * If required, write synchronously, otherwise use
236 * delayed write.
237 */
238 if (flags & IO_SYNC) {
239 bwrite(bp);
240 } else {
241 if (bp->b_bufsize == fs->e2fs_bsize)
242 bp->b_flags |= B_CLUSTEROK;
243 bdwrite(bp);
244 }
245 }
246 /*
247 * Get the data block, allocating if necessary.
248 */
249 if (nb == 0) {
250 EXT2_LOCK(ump);
251 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
252 bp->b_lblkno);
253 if ((error = ext2_alloc(ip,
254 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) {
255 brelse(bp);
256 return (error);
257 }
258 nb = newb;
259 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
260 nbp->b_blkno = fsbtodb(fs, nb);
261 if (flags & BA_CLRBUF)
262 vfs_bio_clrbuf(nbp);
263 bap[indirs[i].in_off] = nb;
264 /*
265 * If required, write synchronously, otherwise use
266 * delayed write.
267 */
268 if (flags & IO_SYNC) {
269 bwrite(bp);
270 } else {
271 if (bp->b_bufsize == fs->e2fs_bsize)
272 bp->b_flags |= B_CLUSTEROK;
273 bdwrite(bp);
274 }
275 *bpp = nbp;
276 return (0);
277 }
278 brelse(bp);
279 if (flags & BA_CLRBUF) {
280 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
281 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
282 error = cluster_read(vp, ip->i_size, lbn,
283 (int)fs->e2fs_bsize, NOCRED,
284 MAXBSIZE, seqcount, &nbp);
285 } else {
286 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
287 }
288 if (error) {
289 brelse(nbp);
290 return (error);
291 }
292 } else {
293 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
294 nbp->b_blkno = fsbtodb(fs, nb);
295 }
296 *bpp = nbp;
297 return (0);
298}
299