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$
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/fs.h>
48 #include <fs/ext2fs/inode.h>
49 #include <fs/ext2fs/ext2fs.h>
50 #include <fs/ext2fs/ext2_dinode.h>
51 #include <fs/ext2fs/ext2_extern.h>
52 #include <fs/ext2fs/ext2_mount.h>
53
54 /*
55 * Balloc defines the structure of filesystem storage
56 * by allocating the physical blocks on a device given
57 * the inode and the logical block number in a file.
58 */
59 int
60 ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
61 struct buf **bpp, int flags)
62 {
63 struct m_ext2fs *fs;
64 struct ext2mount *ump;
65 struct buf *bp, *nbp;
66 struct vnode *vp = ITOV(ip);
67 struct indir indirs[NIADDR + 2];
68 e4fs_daddr_t nb, newb;
69 e2fs_daddr_t *bap, pref;
70 int num, i, error;
71
72 *bpp = NULL;
73 if (lbn < 0)
74 return (EFBIG);
75 fs = ip->i_e2fs;
76 ump = ip->i_ump;
77
78 /*
79 * check if this is a sequential block allocation.
80 * If so, increment next_alloc fields to allow ext2_blkpref
81 * to make a good guess
82 */
83 if (lbn == ip->i_next_alloc_block + 1) {
84 ip->i_next_alloc_block++;
85 ip->i_next_alloc_goal++;
86 }
87 /*
88 * The first NDADDR blocks are direct blocks
89 */
90 if (lbn < NDADDR) {
91 nb = ip->i_db[lbn];
92 /*
93 * no new block is to be allocated, and no need to expand
94 * the file
95 */
96 if (nb != 0) {
97 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp);
98 if (error) {
99 brelse(bp);
100 return (error);
101 }
102 bp->b_blkno = fsbtodb(fs, nb);
103 if (ip->i_size >= (lbn + 1) * fs->e2fs_bsize) {
104 *bpp = bp;
105 return (0);
106 }
107 } else {
108 EXT2_LOCK(ump);
109 error = ext2_alloc(ip, lbn,
110 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0),
111 fs->e2fs_bsize, cred, &newb);
112 if (error)
113 return (error);
114 bp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
115 bp->b_blkno = fsbtodb(fs, newb);
116 if (flags & BA_CLRBUF)
117 vfs_bio_clrbuf(bp);
118 }
119 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
120 ip->i_flag |= IN_CHANGE | IN_UPDATE;
121 *bpp = bp;
122 return (0);
123 }
124 /*
125 * Determine the number of levels of indirection.
126 */
127 pref = 0;
128 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0)
129 return (error);
130 #ifdef INVARIANTS
131 if (num < 1)
132 panic("ext2_balloc: ext2_getlbns returned indirect block");
133 #endif
134 /*
135 * Fetch the first indirect block allocating if necessary.
136 */
137 --num;
138 nb = ip->i_ib[indirs[0].in_off];
139 if (nb == 0) {
140 EXT2_LOCK(ump);
141 pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
142 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
143 if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred,
144 &newb)))
145 return (error);
146 nb = newb;
147 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0);
148 bp->b_blkno = fsbtodb(fs, newb);
149 vfs_bio_clrbuf(bp);
150 /*
151 * Write synchronously so that indirect blocks
152 * never point at garbage.
153 */
154 if ((error = bwrite(bp)) != 0) {
155 ext2_blkfree(ip, nb, fs->e2fs_bsize);
156 return (error);
157 }
158 ip->i_ib[indirs[0].in_off] = newb;
159 ip->i_flag |= IN_CHANGE | IN_UPDATE;
160 }
161 /*
162 * Fetch through the indirect blocks, allocating as necessary.
163 */
164 for (i = 1;;) {
165 error = bread(vp,
166 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
167 if (error) {
168 brelse(bp);
169 return (error);
170 }
171 bap = (e2fs_daddr_t *)bp->b_data;
172 nb = bap[indirs[i].in_off];
173 if (i == num)
174 break;
175 i += 1;
176 if (nb != 0) {
177 bqrelse(bp);
178 continue;
179 }
180 EXT2_LOCK(ump);
181 if (pref == 0)
182 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
183 bp->b_lblkno);
184 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb);
185 if (error) {
186 brelse(bp);
187 return (error);
188 }
189 nb = newb;
190 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0);
191 nbp->b_blkno = fsbtodb(fs, nb);
192 vfs_bio_clrbuf(nbp);
193 /*
194 * Write synchronously so that indirect blocks
195 * never point at garbage.
196 */
197 if ((error = bwrite(nbp)) != 0) {
198 ext2_blkfree(ip, nb, fs->e2fs_bsize);
199 brelse(bp);
200 return (error);
201 }
202 bap[indirs[i - 1].in_off] = nb;
203 /*
204 * If required, write synchronously, otherwise use
205 * delayed write.
206 */
207 if (flags & IO_SYNC) {
208 bwrite(bp);
209 } else {
210 if (bp->b_bufsize == fs->e2fs_bsize)
211 bp->b_flags |= B_CLUSTEROK;
212 bdwrite(bp);
213 }
214 }
215 /*
216 * Get the data block, allocating if necessary.
217 */
218 if (nb == 0) {
219 EXT2_LOCK(ump);
220 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
221 bp->b_lblkno);
222 if ((error = ext2_alloc(ip,
223 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) {
224 brelse(bp);
225 return (error);
226 }
227 nb = newb;
228 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
229 nbp->b_blkno = fsbtodb(fs, nb);
230 if (flags & BA_CLRBUF)
231 vfs_bio_clrbuf(nbp);
232 bap[indirs[i].in_off] = nb;
233 /*
234 * If required, write synchronously, otherwise use
235 * delayed write.
236 */
237 if (flags & IO_SYNC) {
238 bwrite(bp);
239 } else {
240 if (bp->b_bufsize == fs->e2fs_bsize)
241 bp->b_flags |= B_CLUSTEROK;
242 bdwrite(bp);
243 }
244 *bpp = nbp;
245 return (0);
246 }
247 brelse(bp);
248 if (flags & BA_CLRBUF) {
249 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT;
250
251 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
252 error = cluster_read(vp, ip->i_size, lbn,
253 (int)fs->e2fs_bsize, NOCRED,
254 MAXBSIZE, seqcount, 0, &nbp);
255 } else {
256 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp);
257 }
258 if (error) {
259 brelse(nbp);
260 return (error);
261 }
262 } else {
263 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0);
264 nbp->b_blkno = fsbtodb(fs, nb);
265 }
266 *bpp = nbp;
267 return (0);
268 }
Cache object: bf4e8a6935deb750ca12f0934c2bd85b
|