lfs_balloc.c revision 1.51 1 /* $NetBSD: lfs_balloc.c,v 1.51 2005/03/02 21:16:09 perseant 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. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)lfs_balloc.c 8.4 (Berkeley) 5/8/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.51 2005/03/02 21:16:09 perseant Exp $");
71
72 #if defined(_KERNEL_OPT)
73 #include "opt_quota.h"
74 #endif
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/buf.h>
79 #include <sys/proc.h>
80 #include <sys/vnode.h>
81 #include <sys/mount.h>
82 #include <sys/resourcevar.h>
83 #include <sys/trace.h>
84 #include <sys/malloc.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 <uvm/uvm.h>
97
98 int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, struct ucred *);
99
100 u_int64_t locked_fakequeue_count;
101
102 /*
103 * Allocate a block, and to inode and filesystem block accounting for it
104 * and for any indirect blocks the may need to be created in order for
105 * this block to be created.
106 *
107 * Blocks which have never been accounted for (i.e., which "do not exist")
108 * have disk address 0, which is translated by ufs_bmap to the special value
109 * UNASSIGNED == -1, as in the historical UFS.
110 *
111 * Blocks which have been accounted for but which have not yet been written
112 * to disk are given the new special disk address UNWRITTEN == -2, so that
113 * they can be differentiated from completely new blocks.
114 */
115 /* VOP_BWRITE NIADDR+2 times */
116 int
117 lfs_balloc(void *v)
118 {
119 struct vop_balloc_args /* {
120 struct vnode *a_vp;
121 off_t a_startoffset;
122 int a_size;
123 struct ucred *a_cred;
124 int a_flags;
125 struct buf *a_bpp;
126 } */ *ap = v;
127 struct vnode *vp;
128 int offset;
129 u_long iosize;
130 daddr_t daddr, idaddr;
131 struct buf *ibp, *bp, **bpp;
132 struct inode *ip;
133 struct lfs *fs;
134 struct indir indirs[NIADDR+2], *idp;
135 daddr_t lbn, lastblock;
136 int bb, bcount;
137 int error, frags, i, nsize, osize, num;
138
139 vp = ap->a_vp;
140 ip = VTOI(vp);
141 fs = ip->i_lfs;
142 offset = blkoff(fs, ap->a_startoffset);
143 iosize = ap->a_size;
144 KASSERT(iosize <= fs->lfs_bsize);
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 (!ISSPACE(fs, bb, ap->a_cred))
203 return ENOSPC;
204 if (bpp) {
205 *ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
206 bp->b_blkno = UNWRITTEN;
207 if (ap->a_flags & B_CLRBUF)
208 clrbuf(bp);
209 }
210 ip->i_lfs_effnblks += bb;
211 ip->i_lfs->lfs_bfree -= bb;
212 ip->i_ffs1_db[lbn] = UNWRITTEN;
213 } else {
214 if (nsize <= osize) {
215 /* No need to extend */
216 if (bpp && (error = bread(vp, lbn, osize, NOCRED, &bp)))
217 return error;
218 } else {
219 /* Extend existing block */
220 if ((error =
221 lfs_fragextend(vp, osize, nsize, lbn,
222 (bpp ? &bp : NULL),
223 ap->a_cred)))
224 return error;
225 }
226 if (bpp)
227 *bpp = bp;
228 }
229 return 0;
230 }
231
232 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL, NULL);
233 if (error)
234 return (error);
235
236 daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
237 KASSERT(daddr <= LFS_MAX_DADDR);
238
239 /*
240 * Do byte accounting all at once, so we can gracefully fail *before*
241 * we start assigning blocks.
242 */
243 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
244 bcount = 0;
245 if (daddr == UNASSIGNED) {
246 bcount = bb;
247 }
248 for (i = 1; i < num; ++i) {
249 if (!indirs[i].in_exists) {
250 bcount += bb;
251 }
252 }
253 if (ISSPACE(fs, bcount, ap->a_cred)) {
254 ip->i_lfs->lfs_bfree -= bcount;
255 ip->i_lfs_effnblks += bcount;
256 } else {
257 return ENOSPC;
258 }
259
260 if (daddr == UNASSIGNED) {
261 if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
262 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
263 }
264
265 /*
266 * Create new indirect blocks if necessary
267 */
268 if (num > 1) {
269 idaddr = ip->i_ffs1_ib[indirs[0].in_off];
270 for (i = 1; i < num; ++i) {
271 ibp = getblk(vp, indirs[i].in_lbn,
272 fs->lfs_bsize, 0,0);
273 if (!indirs[i].in_exists) {
274 clrbuf(ibp);
275 ibp->b_blkno = UNWRITTEN;
276 } else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
277 ibp->b_blkno = fsbtodb(fs, idaddr);
278 ibp->b_flags |= B_READ;
279 VOP_STRATEGY(vp, ibp);
280 biowait(ibp);
281 }
282 /*
283 * This block exists, but the next one may not.
284 * If that is the case mark it UNWRITTEN to keep
285 * the accounting straight.
286 */
287 /* XXX ondisk32 */
288 if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
289 ((int32_t *)ibp->b_data)[indirs[i].in_off] =
290 UNWRITTEN;
291 /* XXX ondisk32 */
292 idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
293 if ((error = VOP_BWRITE(ibp)))
294 return error;
295 }
296 }
297 }
298
299
300 /*
301 * Get the existing block from the cache, if requested.
302 */
303 frags = fsbtofrags(fs, bb);
304 if (bpp)
305 *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
306
307 /*
308 * Do accounting on blocks that represent pages.
309 */
310 if (!bpp)
311 lfs_register_block(vp, lbn);
312
313 /*
314 * The block we are writing may be a brand new block
315 * in which case we need to do accounting.
316 *
317 * We can tell a truly new block because ufs_bmaparray will say
318 * it is UNASSIGNED. Once we allocate it we will assign it the
319 * disk address UNWRITTEN.
320 */
321 if (daddr == UNASSIGNED) {
322 if (bpp) {
323 if (ap->a_flags & B_CLRBUF)
324 clrbuf(bp);
325
326 /* Note the new address */
327 bp->b_blkno = UNWRITTEN;
328 }
329
330 switch (num) {
331 case 0:
332 ip->i_ffs1_db[lbn] = UNWRITTEN;
333 break;
334 case 1:
335 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
336 break;
337 default:
338 idp = &indirs[num - 1];
339 if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
340 &ibp))
341 panic("lfs_balloc: bread bno %lld",
342 (long long)idp->in_lbn);
343 /* XXX ondisk32 */
344 ((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
345 VOP_BWRITE(ibp);
346 }
347 } else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
348 /*
349 * Not a brand new block, also not in the cache;
350 * read it in from disk.
351 */
352 if (iosize == fs->lfs_bsize)
353 /* Optimization: I/O is unnecessary. */
354 bp->b_blkno = daddr;
355 else {
356 /*
357 * We need to read the block to preserve the
358 * existing bytes.
359 */
360 bp->b_blkno = daddr;
361 bp->b_flags |= B_READ;
362 VOP_STRATEGY(vp, bp);
363 return (biowait(bp));
364 }
365 }
366
367 return (0);
368 }
369
370 /* VOP_BWRITE 1 time */
371 int
372 lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp, struct ucred *cred)
373 {
374 struct inode *ip;
375 struct lfs *fs;
376 long bb;
377 int error;
378 extern long locked_queue_bytes;
379 size_t obufsize;
380
381 ip = VTOI(vp);
382 fs = ip->i_lfs;
383 bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
384 error = 0;
385
386 /*
387 * Get the seglock so we don't enlarge blocks while a segment
388 * is being written. If we're called with bpp==NULL, though,
389 * we are only pretending to change a buffer, so we don't have to
390 * lock.
391 */
392 top:
393 if (bpp) {
394 lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
395 LFS_DEBUG_COUNTLOCKED("frag");
396 }
397
398 if (!ISSPACE(fs, bb, cred)) {
399 error = ENOSPC;
400 goto out;
401 }
402
403 /*
404 * If we are not asked to actually return the block, all we need
405 * to do is allocate space for it. UBC will handle dirtying the
406 * appropriate things and making sure it all goes to disk.
407 * Don't bother to read in that case.
408 */
409 if (bpp && (error = bread(vp, lbn, osize, NOCRED, bpp))) {
410 brelse(*bpp);
411 goto out;
412 }
413 #ifdef QUOTA
414 if ((error = chkdq(ip, bb, cred, 0))) {
415 if (bpp)
416 brelse(*bpp);
417 goto out;
418 }
419 #endif
420 /*
421 * Adjust accounting for lfs_avail. If there's not enough room,
422 * we will have to wait for the cleaner, which we can't do while
423 * holding a block busy or while holding the seglock. In that case,
424 * release both and start over after waiting.
425 */
426
427 if (bpp && ((*bpp)->b_flags & B_DELWRI)) {
428 if (!lfs_fits(fs, bb)) {
429 if (bpp)
430 brelse(*bpp);
431 #ifdef QUOTA
432 chkdq(ip, -bb, cred, 0);
433 #endif
434 lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
435 lfs_availwait(fs, bb);
436 goto top;
437 }
438 fs->lfs_avail -= bb;
439 }
440
441 fs->lfs_bfree -= bb;
442 ip->i_lfs_effnblks += bb;
443 ip->i_flag |= IN_CHANGE | IN_UPDATE;
444
445 if (bpp) {
446 obufsize = (*bpp)->b_bufsize;
447 allocbuf(*bpp, nsize, 1);
448
449 /* Adjust locked-list accounting */
450 if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
451 locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
452
453 bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
454 }
455
456 out:
457 if (bpp) {
458 lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
459 }
460 return (error);
461 }
462
463 /*
464 * Record this lbn as being "write pending". We used to have this information
465 * on the buffer headers, but since pages don't have buffer headers we
466 * record it here instead.
467 */
468
469 void
470 lfs_register_block(struct vnode *vp, daddr_t lbn)
471 {
472 struct lfs *fs;
473 struct inode *ip;
474 struct lbnentry *lbp;
475 int hash;
476
477 /* Don't count metadata */
478 if (lbn < 0 || vp->v_type != VREG || VTOI(vp)->i_number == LFS_IFILE_INUM)
479 return;
480
481 ip = VTOI(vp);
482 fs = ip->i_lfs;
483
484 /* If no space, wait for the cleaner */
485 lfs_availwait(fs, btofsb(fs, 1 << fs->lfs_bshift));
486
487 hash = lbn % LFS_BLIST_HASH_WIDTH;
488 LIST_FOREACH(lbp, &(ip->i_lfs_blist[hash]), entry) {
489 if (lbp->lbn == lbn)
490 return;
491 }
492
493 lbp = (struct lbnentry *)pool_get(&lfs_lbnentry_pool, PR_WAITOK);
494 lbp->lbn = lbn;
495 LIST_INSERT_HEAD(&(ip->i_lfs_blist[hash]), lbp, entry);
496 fs->lfs_favail += btofsb(fs, (1 << fs->lfs_bshift));
497 ++locked_fakequeue_count;
498 }
499
500 static void
501 lfs_do_deregister(struct lfs *fs, struct lbnentry *lbp)
502 {
503 LIST_REMOVE(lbp, entry);
504 pool_put(&lfs_lbnentry_pool, lbp);
505 if (fs->lfs_favail > btofsb(fs, (1 << fs->lfs_bshift)))
506 fs->lfs_favail -= btofsb(fs, (1 << fs->lfs_bshift));
507 if (locked_fakequeue_count > 0)
508 --locked_fakequeue_count;
509 }
510
511 void
512 lfs_deregister_block(struct vnode *vp, daddr_t lbn)
513 {
514 struct lfs *fs;
515 struct inode *ip;
516 struct lbnentry *lbp;
517 int hash;
518
519 /* Don't count metadata */
520 if (lbn < 0 || vp->v_type != VREG || VTOI(vp)->i_number == LFS_IFILE_INUM)
521 return;
522
523 ip = VTOI(vp);
524 fs = ip->i_lfs;
525 hash = lbn % LFS_BLIST_HASH_WIDTH;
526 LIST_FOREACH(lbp, &(ip->i_lfs_blist[hash]), entry) {
527 if (lbp->lbn == lbn)
528 break;
529 }
530 if (lbp == NULL)
531 return;
532
533 lfs_do_deregister(fs, lbp);
534 }
535
536 void
537 lfs_deregister_all(struct vnode *vp)
538 {
539 struct lbnentry *lbp;
540 struct lfs *fs;
541 struct inode *ip;
542 int i;
543
544 ip = VTOI(vp);
545 fs = ip->i_lfs;
546 for (i = 0; i < LFS_BLIST_HASH_WIDTH; i++)
547 while((lbp = LIST_FIRST(&(ip->i_lfs_blist[i]))) != NULL)
548 lfs_do_deregister(fs, lbp);
549 }
550