ffs_balloc.c revision 1.8 1 /* $NetBSD: ffs_balloc.c,v 1.8 1998/03/01 02:23:14 fvdl 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.8 (Berkeley) 6/16/95
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, lbn, size, cred, bpp, flags)
67 register struct inode *ip;
68 register ufs_daddr_t lbn;
69 int size;
70 struct ucred *cred;
71 struct buf **bpp;
72 int flags;
73 {
74 register struct fs *fs;
75 register ufs_daddr_t nb;
76 struct buf *bp, *nbp;
77 struct vnode *vp = ITOV(ip);
78 struct indir indirs[NIADDR + 2];
79 ufs_daddr_t newb, *bap, pref;
80 int deallocated, osize, nsize, num, i, error;
81 ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
82
83 *bpp = NULL;
84 if (lbn < 0)
85 return (EFBIG);
86 fs = ip->i_fs;
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 < lbn) {
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 (lbn < NDADDR) {
120 nb = ip->i_ffs_db[lbn];
121 if (nb != 0 && ip->i_ffs_size >= (lbn + 1) * fs->fs_bsize) {
122 error = bread(vp, lbn, 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, lbn, osize, NOCRED, &bp);
138 if (error) {
139 brelse(bp);
140 return (error);
141 }
142 } else {
143 error = ffs_realloccg(ip, lbn,
144 ffs_blkpref(ip, lbn, (int)lbn,
145 &ip->i_ffs_db[0]), osize, nsize, cred,
146 &bp);
147 if (error)
148 return (error);
149 }
150 } else {
151 if (ip->i_ffs_size < (lbn + 1) * fs->fs_bsize)
152 nsize = fragroundup(fs, size);
153 else
154 nsize = fs->fs_bsize;
155 error = ffs_alloc(ip, lbn,
156 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
157 nsize, cred, &newb);
158 if (error)
159 return (error);
160 bp = getblk(vp, lbn, nsize, 0, 0);
161 bp->b_blkno = fsbtodb(fs, newb);
162 if (flags & B_CLRBUF)
163 clrbuf(bp);
164 }
165 ip->i_ffs_db[lbn] = dbtofsb(fs, bp->b_blkno);
166 ip->i_flag |= IN_CHANGE | IN_UPDATE;
167 *bpp = bp;
168 return (0);
169 }
170 /*
171 * Determine the number of levels of indirection.
172 */
173 pref = 0;
174 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
175 return(error);
176 #ifdef DIAGNOSTIC
177 if (num < 1)
178 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
179 #endif
180 /*
181 * Fetch the first indirect block allocating if necessary.
182 */
183 --num;
184 nb = ip->i_ffs_ib[indirs[0].in_off];
185 allocib = NULL;
186 allocblk = allociblk;
187 if (nb == 0) {
188 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
189 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
190 cred, &newb);
191 if (error)
192 return (error);
193 nb = newb;
194 *allocblk++ = nb;
195 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
196 bp->b_blkno = fsbtodb(fs, nb);
197 clrbuf(bp);
198 /*
199 * Write synchronously so that indirect blocks
200 * never point at garbage.
201 */
202 if ((error = bwrite(bp)) != 0)
203 goto fail;
204 allocib = &ip->i_ffs_ib[indirs[0].in_off];
205 *allocib = nb;
206 ip->i_flag |= IN_CHANGE | IN_UPDATE;
207 }
208 /*
209 * Fetch through the indirect blocks, allocating as necessary.
210 */
211 for (i = 1;;) {
212 error = bread(vp,
213 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
214 if (error) {
215 brelse(bp);
216 goto fail;
217 }
218 bap = (ufs_daddr_t *)bp->b_data;
219 nb = bap[indirs[i].in_off];
220 if (i == num)
221 break;
222 i += 1;
223 if (nb != 0) {
224 brelse(bp);
225 continue;
226 }
227 if (pref == 0)
228 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
229 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
230 &newb);
231 if (error) {
232 brelse(bp);
233 goto fail;
234 }
235 nb = newb;
236 *allocblk++ = nb;
237 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
238 nbp->b_blkno = fsbtodb(fs, nb);
239 clrbuf(nbp);
240 /*
241 * Write synchronously so that indirect blocks
242 * never point at garbage.
243 */
244 if ((error = bwrite(nbp)) != 0) {
245 brelse(bp);
246 goto fail;
247 }
248 bap[indirs[i - 1].in_off] = nb;
249 /*
250 * If required, write synchronously, otherwise use
251 * delayed write.
252 */
253 if (flags & B_SYNC) {
254 bwrite(bp);
255 } else {
256 bdwrite(bp);
257 }
258 }
259 /*
260 * Get the data block, allocating if necessary.
261 */
262 if (nb == 0) {
263 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
264 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
265 &newb);
266 if (error) {
267 brelse(bp);
268 goto fail;
269 }
270 nb = newb;
271 *allocblk++ = nb;
272 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
273 nbp->b_blkno = fsbtodb(fs, nb);
274 if (flags & B_CLRBUF)
275 clrbuf(nbp);
276 bap[indirs[i].in_off] = nb;
277 /*
278 * If required, write synchronously, otherwise use
279 * delayed write.
280 */
281 if (flags & B_SYNC) {
282 bwrite(bp);
283 } else {
284 bdwrite(bp);
285 }
286 *bpp = nbp;
287 return (0);
288 }
289 brelse(bp);
290 if (flags & B_CLRBUF) {
291 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
292 if (error) {
293 brelse(nbp);
294 goto fail;
295 }
296 } else {
297 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
298 nbp->b_blkno = fsbtodb(fs, nb);
299 }
300 *bpp = nbp;
301 return (0);
302 fail:
303 /*
304 * If we have failed part way through block allocation, we
305 * have to deallocate any indirect blocks that we have allocated.
306 */
307 for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
308 ffs_blkfree(ip, *blkp, fs->fs_bsize);
309 deallocated += fs->fs_bsize;
310 }
311 if (allocib != NULL)
312 *allocib = 0;
313 if (deallocated) {
314 #ifdef QUOTA
315 /*
316 * Restore user's disk quota because allocation failed.
317 */
318 (void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
319 #endif
320 ip->i_ffs_blocks -= btodb(deallocated);
321 ip->i_ffs_flags |= IN_CHANGE | IN_UPDATE;
322 }
323 return (error);
324 }
325