lfs_balloc.c revision 1.12.4.3 1 /* $NetBSD: lfs_balloc.c,v 1.12.4.3 1999/08/31 21:03:45 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1989, 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_balloc.c 8.4 (Berkeley) 5/8/95
71 */
72
73 #if defined(_KERNEL) && !defined(_LKM)
74 #include "opt_quota.h"
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/buf.h>
80 #include <sys/proc.h>
81 #include <sys/vnode.h>
82 #include <sys/mount.h>
83 #include <sys/resourcevar.h>
84 #include <sys/trace.h>
85
86 #include <miscfs/specfs/specdev.h>
87
88 #include <ufs/ufs/quota.h>
89 #include <ufs/ufs/inode.h>
90 #include <ufs/ufs/ufsmount.h>
91 #include <ufs/ufs/ufs_extern.h>
92
93 #include <ufs/lfs/lfs.h>
94 #include <ufs/lfs/lfs_extern.h>
95
96 #include <vm/vm.h>
97 #include <uvm/uvm_extern.h>
98
99 int lfs_fragextend __P((struct vnode *, int, int, ufs_daddr_t, struct buf **));
100
101 /*
102 * Balloc defines the structure of file system storage
103 * by allocating the physical blocks on a device given
104 * the inode and the logical block number in a file.
105 */
106
107 int
108 lfs_balloc(v)
109 void *v;
110 {
111 struct vop_balloc_args /* {
112 struct vnode *a_vp;
113 off_t a_offset;
114 off_t a_length;
115 struct ucred *a_cred;
116 int a_flags;
117 } */ *ap = v;
118
119 off_t off, len;
120 struct vnode *vp = ap->a_vp;
121 struct inode *ip = VTOI(vp);
122 struct lfs *fs = ip->i_lfs;
123 int error, delta, bshift, bsize;
124
125 bshift = fs->lfs_bshift;
126 bsize = 1 << bshift;
127
128 off = ap->a_offset;
129 len = ap->a_length;
130
131 delta = off & (bsize - 1);
132 off -= delta;
133 len += delta;
134
135 while (len > 0) {
136 bsize = min(bsize, len);
137
138 if ((error = lfs_balloc1(vp, blkoff(fs,off), bsize,
139 lblkno(fs, off), NULL))) {
140 return error;
141 }
142
143 /*
144 * increase file size now, VOP_BALLOC() requires that
145 * EOF be up-to-date before each call.
146 */
147
148 if (ip->i_ffs_size < off + bsize) {
149 ip->i_ffs_size = off + bsize;
150 if (vp->v_uvm.u_size < ip->i_ffs_size) {
151 uvm_vnp_setsize(vp, ip->i_ffs_size);
152 }
153 }
154
155 off += bsize;
156 len -= bsize;
157 }
158 return 0;
159 }
160
161 int
162 lfs_balloc1(vp, offset, iosize, lbn, bpp)
163 struct vnode *vp;
164 int offset;
165 u_long iosize;
166 ufs_daddr_t lbn;
167 struct buf **bpp;
168 {
169 struct buf *ibp, *bp;
170 struct inode *ip;
171 struct lfs *fs;
172 struct indir *ap, indirs[NIADDR+2];
173 ufs_daddr_t daddr, lastblock;
174 int bb; /* number of disk blocks in a block disk blocks */
175 int error, frags, i, nsize, osize, num;
176
177 ip = VTOI(vp);
178 fs = ip->i_lfs;
179
180 /* XXX printf("lfs_balloc1(%p,%d,%ld,%x,%p)\n",
181 vp, offset, iosize, lbn, bpp); */
182
183 #ifdef DEBUG
184 if(!VOP_ISLOCKED(vp)) {
185 printf("lfs_balloc: warning: ino %d not locked\n",ip->i_number);
186 }
187 #endif
188
189 /*
190 * Three cases: it's a block beyond the end of file, it's a block in
191 * the file that may or may not have been assigned a disk address or
192 * we're writing an entire block. Note, if the daddr is unassigned,
193 * the block might still have existed in the cache (if it was read
194 * or written earlier). If it did, make sure we don't count it as a
195 * new block or zero out its contents. If it did not, make sure
196 * we allocate any necessary indirect blocks.
197 * If we are writing a block beyond the end of the file, we need to
198 * check if the old last block was a fragment. If it was, we need
199 * to rewrite it.
200 */
201
202 if (bpp != NULL) {
203 *bpp = NULL;
204 }
205 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
206 if (error)
207 return (error);
208
209 /* Check for block beyond end of file and fragment extension needed. */
210 lastblock = lblkno(fs, ip->i_ffs_size);
211 if (lastblock < NDADDR && lastblock < lbn) {
212 osize = blksize(fs, ip, lastblock);
213 if (osize < fs->lfs_bsize && osize > 0) {
214 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
215 lastblock, (bpp ? &bp : NULL))))
216 return(error);
217 ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
218 uvm_vnp_setsize(vp, ip->i_ffs_size);
219 ip->i_flag |= IN_CHANGE | IN_UPDATE;
220 VOP_BWRITE(bp);
221 }
222 }
223
224 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
225 if (daddr == UNASSIGNED) {
226 /* May need to allocate indirect blocks */
227 for (i = 1; i < num; ++i) {
228 if (!indirs[i].in_exists) {
229 ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
230 0, 0);
231 if ((ibp->b_flags & (B_DONE | B_DELWRI)))
232 panic ("Indirect block should not exist");
233
234 if (!ISSPACE(fs, bb, curproc->p_ucred)){
235 ibp->b_flags |= B_INVAL;
236 brelse(ibp);
237 return(ENOSPC);
238 } else {
239 ip->i_ffs_blocks += bb;
240 ip->i_lfs->lfs_bfree -= bb;
241 clrbuf(ibp);
242 if ((error = VOP_BWRITE(ibp)))
243 return(error);
244 }
245 }
246 }
247 }
248
249 /*
250 * If the block we are writing is a direct block, it's the last
251 * block in the file, and offset + iosize is less than a full
252 * block, we can write one or more fragments. There are two cases:
253 * the block is brand new and we should allocate it the correct
254 * size or it already exists and contains some fragments and
255 * may need to extend it.
256 */
257 if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
258 osize = blksize(fs, ip, lbn);
259 nsize = fragroundup(fs, offset + iosize);
260 frags = numfrags(fs, nsize);
261 bb = fragstodb(fs, frags);
262 if (lblktosize(fs, lbn) >= ip->i_ffs_size) {
263 /* Brand new block or fragment */
264 if (bpp != NULL) {
265 *bpp = bp = getblk(vp, lbn, nsize, 0, 0);
266 }
267 } else {
268 if (nsize <= osize) {
269 /* No need to extend */
270 /* XXX KS - Are we wasting space? */
271 if (bpp != NULL && (error = bread(vp, lbn, osize, NOCRED, &bp)))
272 return error;
273 } else {
274 /* Extend existing block */
275 if (bpp != NULL && (error =
276 lfs_fragextend(vp, osize, nsize, lbn, &bp)))
277 return(error);
278 }
279 if (bpp != NULL) {
280 *bpp = bp;
281 }
282 }
283 } else {
284 /*
285 * Get the existing block from the cache either because the
286 * block 1) is not a direct block or 2) is not the last
287 * block in the file.
288 */
289 frags = dbtofrags(fs, bb);
290 if (bpp != NULL) {
291 *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
292 }
293 }
294
295 /*
296 * The block we are writing may be a brand new block
297 * in which case we need to do accounting (i.e. check
298 * for free space and update the inode number of blocks.
299 */
300 if (bpp == NULL && daddr == UNASSIGNED) {
301 /*
302 * If bpp is NULL we are being called from UBC. In this
303 * case, all we have to do is block accounting for the
304 * inode and the filesystem.
305 */
306 if (!ISSPACE(fs, bb, curproc->p_ucred))
307 return(ENOSPC);
308
309 ip->i_ffs_blocks += bb;
310 ip->i_lfs->lfs_bfree -= bb;
311
312 /* Assign a daddr != UNASSIGNED, so we don't overcount */
313 switch (num) {
314 case 0:
315 ip->i_ffs_db[lbn] = UNWRITTEN;
316 break;
317 case 1:
318 ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
319 break;
320 default:
321 ap = &indirs[num - 1];
322 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
323 panic("lfs_balloc: bread bno %d", ap->in_lbn);
324 /* The indirect block exists, no need to account it */
325 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = UNWRITTEN;
326 VOP_BWRITE(bp);
327 }
328 } else if (bpp && !(bp->b_flags & (B_DONE | B_DELWRI))) {
329 if (daddr == UNASSIGNED) {
330 if (!ISSPACE(fs, bb, curproc->p_ucred)) {
331 bp->b_flags |= B_INVAL;
332 brelse(bp);
333 return(ENOSPC);
334 } else {
335 ip->i_ffs_blocks += bb;
336 ip->i_lfs->lfs_bfree -= bb;
337 if (iosize != fs->lfs_bsize)
338 clrbuf(bp);
339 }
340 } else if (iosize == fs->lfs_bsize) {
341 /* Optimization: I/O is unnecessary. */
342 bp->b_blkno = daddr;
343 } else {
344 /*
345 * We need to read the block to preserve the
346 * existing bytes.
347 */
348 bp->b_blkno = daddr;
349 bp->b_flags |= B_READ;
350 VOP_STRATEGY(bp);
351 return(biowait(bp));
352 }
353 }
354
355 return (0);
356 }
357
358 int
359 lfs_fragextend(vp, osize, nsize, lbn, bpp)
360 struct vnode *vp;
361 int osize;
362 int nsize;
363 ufs_daddr_t lbn;
364 struct buf **bpp;
365 {
366 struct inode *ip;
367 struct lfs *fs;
368 long bb;
369 int error;
370 extern long locked_queue_bytes;
371 struct buf *ibp;
372 SEGUSE *sup;
373 daddr_t daddr;
374
375 ip = VTOI(vp);
376 fs = ip->i_lfs;
377
378 bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
379 top:
380 if (!ISSPACE(fs, bb, curproc->p_ucred)) {
381 return(ENOSPC);
382 }
383 if (bpp) {
384 if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
385 brelse(*bpp);
386 return(error);
387 }
388 daddr = (*bpp)->b_blkno;
389 } else {
390 /* We still need the daddr, just not contents. */
391 if((error = VOP_BMAP(vp, lbn, NULL, &daddr, NULL)))
392 return (error);
393 }
394
395 /*
396 * Fix the allocation for this fragment so that it looks like the
397 * source segment contained a block of the new size. This overcounts;
398 * but the overcount only lasts until the block in question
399 * is written, so the on-disk live bytes count is always correct.
400 */
401 LFS_SEGENTRY(sup, fs, datosn(fs,daddr), ibp);
402 sup->su_nbytes += (nsize-osize);
403 VOP_BWRITE(ibp);
404
405 #ifdef QUOTA
406 if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
407 if (bpp)
408 brelse(*bpp);
409 return (error);
410 }
411 #endif
412 /*
413 * XXX - KS - Don't change size while we're gathered, as we could
414 * then overlap another buffer in lfs_writeseg.
415 */
416 if(bpp && (*bpp)->b_flags & B_GATHERED) {
417 (*bpp)->b_flags |= B_NEEDCOMMIT; /* XXX KS - what flag to use? */
418 brelse(*bpp);
419 tsleep(*bpp, (PRIBIO+1), "lfs_fragextend", 0);
420 goto top;
421 }
422 ip->i_ffs_blocks += bb;
423 ip->i_flag |= IN_CHANGE | IN_UPDATE;
424 fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
425 if (bpp) {
426 if((*bpp)->b_flags & B_LOCKED)
427 locked_queue_bytes += (nsize - osize);
428 allocbuf(*bpp, nsize);
429 bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
430 }
431 return(0);
432 }
433