ffs_balloc.c revision 1.5 1 /* $NetBSD: ffs_balloc.c,v 1.5 1997/07/04 20:22:15 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
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 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/proc.h>
42 #include <sys/file.h>
43 #include <sys/vnode.h>
44
45 #include <vm/vm.h>
46
47 #include <ufs/ufs/quota.h>
48 #include <ufs/ufs/inode.h>
49 #include <ufs/ufs/ufs_extern.h>
50
51 #include <ufs/ffs/fs.h>
52 #include <ufs/ffs/ffs_extern.h>
53
54 /*
55 * Balloc defines the structure of file system 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 ffs_balloc(ip, bn, size, cred, bpp, flags)
61 register struct inode *ip;
62 register daddr_t bn;
63 int size;
64 struct ucred *cred;
65 struct buf **bpp;
66 int flags;
67 {
68 register struct fs *fs;
69 register daddr_t nb;
70 struct buf *bp, *nbp;
71 struct vnode *vp = ITOV(ip);
72 struct indir indirs[NIADDR + 2];
73 daddr_t newb, lbn, *bap, pref;
74 int osize, nsize, num, i, error;
75
76 *bpp = NULL;
77 if (bn < 0)
78 return (EFBIG);
79 fs = ip->i_fs;
80 lbn = bn;
81
82 /*
83 * If the next write will extend the file into a new block,
84 * and the file is currently composed of a fragment
85 * this fragment has to be extended to be a full block.
86 */
87 nb = lblkno(fs, ip->i_ffs_size);
88 if (nb < NDADDR && nb < bn) {
89 osize = blksize(fs, ip, nb);
90 if (osize < fs->fs_bsize && osize > 0) {
91 error = ffs_realloccg(ip, nb,
92 ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
93 osize, (int)fs->fs_bsize, cred, &bp);
94 if (error)
95 return (error);
96 ip->i_ffs_size = (nb + 1) * fs->fs_bsize;
97 vnode_pager_setsize(vp, ip->i_ffs_size);
98 ip->i_ffs_db[nb] = dbtofsb(fs, bp->b_blkno);
99 ip->i_flag |= IN_CHANGE | IN_UPDATE;
100 if (flags & B_SYNC)
101 bwrite(bp);
102 else
103 bawrite(bp);
104 }
105 }
106 /*
107 * The first NDADDR blocks are direct blocks
108 */
109 if (bn < NDADDR) {
110 nb = ip->i_ffs_db[bn];
111 if (nb != 0 && ip->i_ffs_size >= (bn + 1) * fs->fs_bsize) {
112 error = bread(vp, bn, fs->fs_bsize, NOCRED, &bp);
113 if (error) {
114 brelse(bp);
115 return (error);
116 }
117 *bpp = bp;
118 return (0);
119 }
120 if (nb != 0) {
121 /*
122 * Consider need to reallocate a fragment.
123 */
124 osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
125 nsize = fragroundup(fs, size);
126 if (nsize <= osize) {
127 error = bread(vp, bn, osize, NOCRED, &bp);
128 if (error) {
129 brelse(bp);
130 return (error);
131 }
132 } else {
133 error = ffs_realloccg(ip, bn,
134 ffs_blkpref(ip, bn, (int)bn, &ip->i_ffs_db[0]),
135 osize, nsize, cred, &bp);
136 if (error)
137 return (error);
138 }
139 } else {
140 if (ip->i_ffs_size < (bn + 1) * fs->fs_bsize)
141 nsize = fragroundup(fs, size);
142 else
143 nsize = fs->fs_bsize;
144 error = ffs_alloc(ip, bn,
145 ffs_blkpref(ip, bn, (int)bn, &ip->i_ffs_db[0]),
146 nsize, cred, &newb);
147 if (error)
148 return (error);
149 bp = getblk(vp, bn, nsize, 0, 0);
150 bp->b_blkno = fsbtodb(fs, newb);
151 if (flags & B_CLRBUF)
152 clrbuf(bp);
153 }
154 ip->i_ffs_db[bn] = dbtofsb(fs, bp->b_blkno);
155 ip->i_flag |= IN_CHANGE | IN_UPDATE;
156 *bpp = bp;
157 return (0);
158 }
159 /*
160 * Determine the number of levels of indirection.
161 */
162 pref = 0;
163 if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
164 return(error);
165 #ifdef DIAGNOSTIC
166 if (num < 1)
167 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
168 #endif
169 /*
170 * Fetch the first indirect block allocating if necessary.
171 */
172 --num;
173 nb = ip->i_ffs_ib[indirs[0].in_off];
174 if (nb == 0) {
175 pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0);
176 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
177 cred, &newb);
178 if (error)
179 return (error);
180 nb = newb;
181 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
182 bp->b_blkno = fsbtodb(fs, newb);
183 clrbuf(bp);
184 /*
185 * Write synchronously so that indirect blocks
186 * never point at garbage.
187 */
188 if ((error = bwrite(bp)) != 0) {
189 ffs_blkfree(ip, nb, fs->fs_bsize);
190 return (error);
191 }
192 ip->i_ffs_ib[indirs[0].in_off] = newb;
193 ip->i_flag |= IN_CHANGE | IN_UPDATE;
194 }
195 /*
196 * Fetch through the indirect blocks, allocating as necessary.
197 */
198 for (i = 1;;) {
199 error = bread(vp,
200 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
201 if (error) {
202 brelse(bp);
203 return (error);
204 }
205 bap = (daddr_t *)bp->b_data;
206 nb = bap[indirs[i].in_off];
207 if (i == num)
208 break;
209 i += 1;
210 if (nb != 0) {
211 brelse(bp);
212 continue;
213 }
214 if (pref == 0)
215 pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0);
216 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
217 &newb);
218 if (error) {
219 brelse(bp);
220 return (error);
221 }
222 nb = newb;
223 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
224 nbp->b_blkno = fsbtodb(fs, nb);
225 clrbuf(nbp);
226 /*
227 * Write synchronously so that indirect blocks
228 * never point at garbage.
229 */
230 if ((error = bwrite(nbp)) != 0) {
231 ffs_blkfree(ip, nb, fs->fs_bsize);
232 brelse(bp);
233 return (error);
234 }
235 bap[indirs[i - 1].in_off] = nb;
236 /*
237 * If required, write synchronously, otherwise use
238 * delayed write.
239 */
240 if (flags & B_SYNC) {
241 bwrite(bp);
242 } else {
243 bdwrite(bp);
244 }
245 }
246 /*
247 * Get the data block, allocating if necessary.
248 */
249 if (nb == 0) {
250 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
251 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
252 &newb);
253 if (error) {
254 brelse(bp);
255 return (error);
256 }
257 nb = newb;
258 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
259 nbp->b_blkno = fsbtodb(fs, nb);
260 if (flags & B_CLRBUF)
261 clrbuf(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 bdwrite(bp);
271 }
272 *bpp = nbp;
273 return (0);
274 }
275 brelse(bp);
276 if (flags & B_CLRBUF) {
277 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
278 if (error) {
279 brelse(nbp);
280 return (error);
281 }
282 } else {
283 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
284 nbp->b_blkno = fsbtodb(fs, nb);
285 }
286 *bpp = nbp;
287 return (0);
288 }
289