lfs_balloc.c revision 1.40 1 /* $NetBSD: lfs_balloc.c,v 1.40 2003/04/02 10:39:40 fvdl Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 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 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.40 2003/04/02 10:39:40 fvdl Exp $");
75
76 #if defined(_KERNEL_OPT)
77 #include "opt_quota.h"
78 #endif
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/buf.h>
83 #include <sys/proc.h>
84 #include <sys/vnode.h>
85 #include <sys/mount.h>
86 #include <sys/resourcevar.h>
87 #include <sys/trace.h>
88
89 #include <miscfs/specfs/specdev.h>
90
91 #include <ufs/ufs/quota.h>
92 #include <ufs/ufs/inode.h>
93 #include <ufs/ufs/ufsmount.h>
94 #include <ufs/ufs/ufs_extern.h>
95
96 #include <ufs/lfs/lfs.h>
97 #include <ufs/lfs/lfs_extern.h>
98
99 #include <uvm/uvm.h>
100
101 int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, struct ucred *);
102
103 /*
104 * Allocate a block, and to inode and filesystem block accounting for it
105 * and for any indirect blocks the may need to be created in order for
106 * this block to be created.
107 *
108 * Blocks which have never been accounted for (i.e., which "do not exist")
109 * have disk address 0, which is translated by ufs_bmap to the special value
110 * UNASSIGNED == -1, as in the historical UFS.
111 *
112 * Blocks which have been accounted for but which have not yet been written
113 * to disk are given the new special disk address UNWRITTEN == -2, so that
114 * they can be differentiated from completely new blocks.
115 */
116 /* VOP_BWRITE NIADDR+2 times */
117 int
118 lfs_balloc(void *v)
119 {
120 struct vop_balloc_args /* {
121 struct vnode *a_vp;
122 off_t a_startoffset;
123 int a_size;
124 struct ucred *a_cred;
125 int a_flags;
126 struct buf *a_bpp;
127 } */ *ap = v;
128 struct vnode *vp;
129 int offset;
130 u_long iosize;
131 daddr_t daddr, idaddr;
132 struct buf *ibp, *bp, **bpp;
133 struct inode *ip;
134 struct lfs *fs;
135 struct indir indirs[NIADDR+2], *idp;
136 daddr_t lbn, lastblock;
137 int bb, bcount;
138 int error, frags, i, nsize, osize, num;
139
140 vp = ap->a_vp;
141 ip = VTOI(vp);
142 fs = ip->i_lfs;
143 offset = blkoff(fs, ap->a_startoffset);
144 iosize = ap->a_size;
145 lbn = lblkno(fs, ap->a_startoffset);
146 /* (void)lfs_check(vp, lbn, 0); */
147 bpp = ap->a_bpp;
148
149 /*
150 * Three cases: it's a block beyond the end of file, it's a block in
151 * the file that may or may not have been assigned a disk address or
152 * we're writing an entire block.
153 *
154 * Note, if the daddr is UNWRITTEN, the block already exists in
155 * the cache (it was read or written earlier). If so, make sure
156 * we don't count it as a new block or zero out its contents. If
157 * it did not, make sure we allocate any necessary indirect
158 * blocks.
159 *
160 * If we are writing a block beyond the end of the file, we need to
161 * check if the old last block was a fragment. If it was, we need
162 * to rewrite it.
163 */
164
165 if (bpp)
166 *bpp = NULL;
167
168 /* Check for block beyond end of file and fragment extension needed. */
169 lastblock = lblkno(fs, ip->i_size);
170 if (lastblock < NDADDR && lastblock < lbn) {
171 osize = blksize(fs, ip, lastblock);
172 if (osize < fs->lfs_bsize && osize > 0) {
173 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
174 lastblock,
175 (bpp ? &bp : NULL),
176 ap->a_cred)))
177 return (error);
178 ip->i_ffs1_size = ip->i_size =
179 (lastblock + 1) * fs->lfs_bsize;
180 uvm_vnp_setsize(vp, ip->i_size);
181 ip->i_flag |= IN_CHANGE | IN_UPDATE;
182 if (bpp)
183 (void) VOP_BWRITE(bp);
184 }
185 }
186
187 /*
188 * If the block we are writing is a direct block, it's the last
189 * block in the file, and offset + iosize is less than a full
190 * block, we can write one or more fragments. There are two cases:
191 * the block is brand new and we should allocate it the correct
192 * size or it already exists and contains some fragments and
193 * may need to extend it.
194 */
195 if (lbn < NDADDR && lblkno(fs, ip->i_size) <= lbn) {
196 osize = blksize(fs, ip, lbn);
197 nsize = fragroundup(fs, offset + iosize);
198 if (lblktosize(fs, lbn) >= ip->i_size) {
199 /* Brand new block or fragment */
200 frags = numfrags(fs, nsize);
201 bb = fragstofsb(fs, frags);
202 if (bpp) {
203 *ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
204 bp->b_blkno = UNWRITTEN;
205 if (ap->a_flags & B_CLRBUF)
206 clrbuf(bp);
207 }
208 ip->i_lfs_effnblks += bb;
209 ip->i_lfs->lfs_bfree -= bb;
210 ip->i_ffs1_db[lbn] = UNWRITTEN;
211 } else {
212 if (nsize <= osize) {
213 /* No need to extend */
214 if (bpp && (error = bread(vp, lbn, osize, NOCRED, &bp)))
215 return error;
216 } else {
217 /* Extend existing block */
218 if ((error =
219 lfs_fragextend(vp, osize, nsize, lbn,
220 (bpp ? &bp : NULL),
221 ap->a_cred)))
222 return error;
223 }
224 if (bpp)
225 *bpp = bp;
226 }
227 return 0;
228 }
229
230 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
231 if (error)
232 return (error);
233 /*
234 * Do byte accounting all at once, so we can gracefully fail *before*
235 * we start assigning blocks.
236 */
237 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
238 bcount = 0;
239 if (daddr == UNASSIGNED) {
240 bcount = bb;
241 }
242 for (i = 1; i < num; ++i) {
243 if (!indirs[i].in_exists) {
244 bcount += bb;
245 }
246 }
247 if (ISSPACE(fs, bcount, ap->a_cred)) {
248 ip->i_lfs->lfs_bfree -= bcount;
249 ip->i_lfs_effnblks += bcount;
250 } else {
251 return ENOSPC;
252 }
253
254 if (daddr == UNASSIGNED) {
255 if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
256 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
257 }
258
259 /*
260 * Create new indirect blocks if necessary
261 */
262 if (num > 1)
263 idaddr = ip->i_ffs1_ib[indirs[0].in_off];
264 for (i = 1; i < num; ++i) {
265 ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize, 0,0);
266 if (!indirs[i].in_exists) {
267 clrbuf(ibp);
268 ibp->b_blkno = UNWRITTEN;
269 } else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
270 ibp->b_blkno = fsbtodb(fs, idaddr);
271 ibp->b_flags |= B_READ;
272 VOP_STRATEGY(ibp);
273 biowait(ibp);
274 }
275 /*
276 * This block exists, but the next one may not.
277 * If that is the case mark it UNWRITTEN to keep
278 * the accounting straight.
279 */
280 /* XXX ondisk32 */
281 if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
282 ((int32_t *)ibp->b_data)[indirs[i].in_off] =
283 UNWRITTEN;
284 /* XXX ondisk32 */
285 idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
286 if ((error = VOP_BWRITE(ibp))) {
287 return error;
288 }
289 }
290 }
291
292
293 /*
294 * Get the existing block from the cache, if requested.
295 */
296 frags = fsbtofrags(fs, bb);
297 if (bpp)
298 *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
299
300 /*
301 * The block we are writing may be a brand new block
302 * in which case we need to do accounting.
303 *
304 * We can tell a truly new block because ufs_bmaparray will say
305 * it is UNASSIGNED. Once we allocate it we will assign it the
306 * disk address UNWRITTEN.
307 */
308 if (daddr == UNASSIGNED) {
309 if (bpp) {
310 if (ap->a_flags & B_CLRBUF)
311 clrbuf(bp);
312
313 /* Note the new address */
314 bp->b_blkno = UNWRITTEN;
315 }
316
317 switch (num) {
318 case 0:
319 ip->i_ffs1_db[lbn] = UNWRITTEN;
320 break;
321 case 1:
322 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
323 break;
324 default:
325 idp = &indirs[num - 1];
326 if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
327 &ibp))
328 panic("lfs_balloc: bread bno %lld",
329 (long long)idp->in_lbn);
330 /* XXX ondisk32 */
331 ((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
332 VOP_BWRITE(ibp);
333 }
334 } else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
335 /*
336 * Not a brand new block, also not in the cache;
337 * read it in from disk.
338 */
339 if (iosize == fs->lfs_bsize)
340 /* Optimization: I/O is unnecessary. */
341 bp->b_blkno = daddr;
342 else {
343 /*
344 * We need to read the block to preserve the
345 * existing bytes.
346 */
347 bp->b_blkno = daddr;
348 bp->b_flags |= B_READ;
349 VOP_STRATEGY(bp);
350 return (biowait(bp));
351 }
352 }
353
354 return (0);
355 }
356
357 /* VOP_BWRITE 1 time */
358 int
359 lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp, struct ucred *cred)
360 {
361 struct inode *ip;
362 struct lfs *fs;
363 long bb;
364 int error;
365 extern long locked_queue_bytes;
366 size_t obufsize;
367
368 ip = VTOI(vp);
369 fs = ip->i_lfs;
370 bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
371 error = 0;
372
373 /*
374 * Get the seglock so we don't enlarge blocks while a segment
375 * is being written. If we're called with bpp==NULL, though,
376 * we are only pretending to change a buffer, so we don't have to
377 * lock.
378 */
379 top:
380 if (bpp) {
381 lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
382 }
383
384 if (!ISSPACE(fs, bb, cred)) {
385 error = ENOSPC;
386 goto out;
387 }
388
389 /*
390 * If we are not asked to actually return the block, all we need
391 * to do is allocate space for it. UBC will handle dirtying the
392 * appropriate things and making sure it all goes to disk.
393 * Don't bother to read in that case.
394 */
395 if (bpp && (error = bread(vp, lbn, osize, NOCRED, bpp))) {
396 brelse(*bpp);
397 goto out;
398 }
399 #ifdef QUOTA
400 if ((error = chkdq(ip, bb, cred, 0))) {
401 if (bpp)
402 brelse(*bpp);
403 goto out;
404 }
405 #endif
406 /*
407 * Adjust accounting for lfs_avail. If there's not enough room,
408 * we will have to wait for the cleaner, which we can't do while
409 * holding a block busy or while holding the seglock. In that case,
410 * release both and start over after waiting.
411 */
412
413 if (bpp && ((*bpp)->b_flags & B_DELWRI)) {
414 if (!lfs_fits(fs, bb)) {
415 if (bpp)
416 brelse(*bpp);
417 #ifdef QUOTA
418 chkdq(ip, -bb, cred, 0);
419 #endif
420 lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
421 lfs_availwait(fs, bb);
422 goto top;
423 }
424 fs->lfs_avail -= bb;
425 }
426
427 fs->lfs_bfree -= bb;
428 ip->i_lfs_effnblks += bb;
429 ip->i_flag |= IN_CHANGE | IN_UPDATE;
430
431 if (bpp) {
432 LFS_DEBUG_COUNTLOCKED("frag1");
433
434 obufsize = (*bpp)->b_bufsize;
435 allocbuf(*bpp, nsize);
436
437 /* Adjust locked-list accounting */
438 if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
439 locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
440
441 LFS_DEBUG_COUNTLOCKED("frag2");
442
443 bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
444 }
445
446 out:
447 if (bpp) {
448 lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
449 }
450 return (error);
451 }
452