ffs_balloc.c revision 1.7 1 /* $NetBSD: ffs_balloc.c,v 1.7 1998/02/10 14:10:53 mrg 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 "opt_uvm.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/buf.h>
43 #include <sys/proc.h>
44 #include <sys/file.h>
45 #include <sys/vnode.h>
46
47 #include <vm/vm.h>
48
49 #if defined(UVM)
50 #include <uvm/uvm_extern.h>
51 #endif
52
53 #include <ufs/ufs/quota.h>
54 #include <ufs/ufs/inode.h>
55 #include <ufs/ufs/ufs_extern.h>
56
57 #include <ufs/ffs/fs.h>
58 #include <ufs/ffs/ffs_extern.h>
59
60 /*
61 * Balloc defines the structure of file system storage
62 * by allocating the physical blocks on a device given
63 * the inode and the logical block number in a file.
64 */
65 int
66 ffs_balloc(ip, bn, size, cred, bpp, flags)
67 register struct inode *ip;
68 register daddr_t bn;
69 int size;
70 struct ucred *cred;
71 struct buf **bpp;
72 int flags;
73 {
74 register struct fs *fs;
75 register daddr_t nb;
76 struct buf *bp, *nbp;
77 struct vnode *vp = ITOV(ip);
78 struct indir indirs[NIADDR + 2];
79 daddr_t newb, lbn, *bap, pref;
80 int osize, nsize, num, i, error;
81
82 *bpp = NULL;
83 if (bn < 0)
84 return (EFBIG);
85 fs = ip->i_fs;
86 lbn = bn;
87
88 /*
89 * If the next write will extend the file into a new block,
90 * and the file is currently composed of a fragment
91 * this fragment has to be extended to be a full block.
92 */
93 nb = lblkno(fs, ip->i_ffs_size);
94 if (nb < NDADDR && nb < bn) {
95 osize = blksize(fs, ip, nb);
96 if (osize < fs->fs_bsize && osize > 0) {
97 error = ffs_realloccg(ip, nb,
98 ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
99 osize, (int)fs->fs_bsize, cred, &bp);
100 if (error)
101 return (error);
102 ip->i_ffs_size = (nb + 1) * fs->fs_bsize;
103 #if defined(UVM)
104 uvm_vnp_setsize(vp, ip->i_ffs_size);
105 #else
106 vnode_pager_setsize(vp, ip->i_ffs_size);
107 #endif
108 ip->i_ffs_db[nb] = dbtofsb(fs, bp->b_blkno);
109 ip->i_flag |= IN_CHANGE | IN_UPDATE;
110 if (flags & B_SYNC)
111 bwrite(bp);
112 else
113 bawrite(bp);
114 }
115 }
116 /*
117 * The first NDADDR blocks are direct blocks
118 */
119 if (bn < NDADDR) {
120 nb = ip->i_ffs_db[bn];
121 if (nb != 0 && ip->i_ffs_size >= (bn + 1) * fs->fs_bsize) {
122 error = bread(vp, bn, fs->fs_bsize, NOCRED, &bp);
123 if (error) {
124 brelse(bp);
125 return (error);
126 }
127 *bpp = bp;
128 return (0);
129 }
130 if (nb != 0) {
131 /*
132 * Consider need to reallocate a fragment.
133 */
134 osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
135 nsize = fragroundup(fs, size);
136 if (nsize <= osize) {
137 error = bread(vp, bn, osize, NOCRED, &bp);
138 if (error) {
139 brelse(bp);
140 return (error);
141 }
142 } else {
143 error = ffs_realloccg(ip, bn,
144 ffs_blkpref(ip, bn, (int)bn, &ip->i_ffs_db[0]),
145 osize, nsize, cred, &bp);
146 if (error)
147 return (error);
148 }
149 } else {
150 if (ip->i_ffs_size < (bn + 1) * fs->fs_bsize)
151 nsize = fragroundup(fs, size);
152 else
153 nsize = fs->fs_bsize;
154 error = ffs_alloc(ip, bn,
155 ffs_blkpref(ip, bn, (int)bn, &ip->i_ffs_db[0]),
156 nsize, cred, &newb);
157 if (error)
158 return (error);
159 bp = getblk(vp, bn, nsize, 0, 0);
160 bp->b_blkno = fsbtodb(fs, newb);
161 if (flags & B_CLRBUF)
162 clrbuf(bp);
163 }
164 ip->i_ffs_db[bn] = dbtofsb(fs, bp->b_blkno);
165 ip->i_flag |= IN_CHANGE | IN_UPDATE;
166 *bpp = bp;
167 return (0);
168 }
169 /*
170 * Determine the number of levels of indirection.
171 */
172 pref = 0;
173 if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
174 return(error);
175 #ifdef DIAGNOSTIC
176 if (num < 1)
177 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
178 #endif
179 /*
180 * Fetch the first indirect block allocating if necessary.
181 */
182 --num;
183 nb = ip->i_ffs_ib[indirs[0].in_off];
184 if (nb == 0) {
185 pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0);
186 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
187 cred, &newb);
188 if (error)
189 return (error);
190 nb = newb;
191 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
192 bp->b_blkno = fsbtodb(fs, newb);
193 clrbuf(bp);
194 /*
195 * Write synchronously so that indirect blocks
196 * never point at garbage.
197 */
198 if ((error = bwrite(bp)) != 0) {
199 ffs_blkfree(ip, nb, fs->fs_bsize);
200 return (error);
201 }
202 ip->i_ffs_ib[indirs[0].in_off] = newb;
203 ip->i_flag |= IN_CHANGE | IN_UPDATE;
204 }
205 /*
206 * Fetch through the indirect blocks, allocating as necessary.
207 */
208 for (i = 1;;) {
209 error = bread(vp,
210 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
211 if (error) {
212 brelse(bp);
213 return (error);
214 }
215 bap = (daddr_t *)bp->b_data;
216 nb = bap[indirs[i].in_off];
217 if (i == num)
218 break;
219 i += 1;
220 if (nb != 0) {
221 brelse(bp);
222 continue;
223 }
224 if (pref == 0)
225 pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0);
226 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
227 &newb);
228 if (error) {
229 brelse(bp);
230 return (error);
231 }
232 nb = newb;
233 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
234 nbp->b_blkno = fsbtodb(fs, nb);
235 clrbuf(nbp);
236 /*
237 * Write synchronously so that indirect blocks
238 * never point at garbage.
239 */
240 if ((error = bwrite(nbp)) != 0) {
241 ffs_blkfree(ip, nb, fs->fs_bsize);
242 brelse(bp);
243 return (error);
244 }
245 bap[indirs[i - 1].in_off] = nb;
246 /*
247 * If required, write synchronously, otherwise use
248 * delayed write.
249 */
250 if (flags & B_SYNC) {
251 bwrite(bp);
252 } else {
253 bdwrite(bp);
254 }
255 }
256 /*
257 * Get the data block, allocating if necessary.
258 */
259 if (nb == 0) {
260 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
261 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
262 &newb);
263 if (error) {
264 brelse(bp);
265 return (error);
266 }
267 nb = newb;
268 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
269 nbp->b_blkno = fsbtodb(fs, nb);
270 if (flags & B_CLRBUF)
271 clrbuf(nbp);
272 bap[indirs[i].in_off] = nb;
273 /*
274 * If required, write synchronously, otherwise use
275 * delayed write.
276 */
277 if (flags & B_SYNC) {
278 bwrite(bp);
279 } else {
280 bdwrite(bp);
281 }
282 *bpp = nbp;
283 return (0);
284 }
285 brelse(bp);
286 if (flags & B_CLRBUF) {
287 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
288 if (error) {
289 brelse(nbp);
290 return (error);
291 }
292 } else {
293 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
294 nbp->b_blkno = fsbtodb(fs, nb);
295 }
296 *bpp = nbp;
297 return (0);
298 }
299