Sync usage with man page.
[netbsd-mini2440.git] / sys / ufs / lfs / lfs_balloc.c
blob7c983f0a854425afc052cf4b946ac1289a8209a0
1 /* $NetBSD: lfs_balloc.c,v 1.67 2008/05/16 09:22:00 hannken Exp $ */
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant@hhhh.org>.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 1989, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
59 * @(#)lfs_balloc.c 8.4 (Berkeley) 5/8/95
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.67 2008/05/16 09:22:00 hannken Exp $");
65 #if defined(_KERNEL_OPT)
66 #include "opt_quota.h"
67 #endif
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/buf.h>
72 #include <sys/proc.h>
73 #include <sys/vnode.h>
74 #include <sys/mount.h>
75 #include <sys/resourcevar.h>
76 #include <sys/tree.h>
77 #include <sys/trace.h>
78 #include <sys/kauth.h>
80 #include <miscfs/specfs/specdev.h>
82 #include <ufs/ufs/quota.h>
83 #include <ufs/ufs/inode.h>
84 #include <ufs/ufs/ufsmount.h>
85 #include <ufs/ufs/ufs_extern.h>
87 #include <ufs/lfs/lfs.h>
88 #include <ufs/lfs/lfs_extern.h>
90 #include <uvm/uvm.h>
92 int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, kauth_cred_t);
94 u_int64_t locked_fakequeue_count;
97 * Allocate a block, and to inode and filesystem block accounting for it
98 * and for any indirect blocks the may need to be created in order for
99 * this block to be created.
101 * Blocks which have never been accounted for (i.e., which "do not exist")
102 * have disk address 0, which is translated by ufs_bmap to the special value
103 * UNASSIGNED == -1, as in the historical UFS.
105 * Blocks which have been accounted for but which have not yet been written
106 * to disk are given the new special disk address UNWRITTEN == -2, so that
107 * they can be differentiated from completely new blocks.
109 /* VOP_BWRITE NIADDR+2 times */
111 lfs_balloc(struct vnode *vp, off_t startoffset, int iosize, kauth_cred_t cred,
112 int flags, struct buf **bpp)
114 int offset;
115 daddr_t daddr, idaddr;
116 struct buf *ibp, *bp;
117 struct inode *ip;
118 struct lfs *fs;
119 struct indir indirs[NIADDR+2], *idp;
120 daddr_t lbn, lastblock;
121 int bb, bcount;
122 int error, frags, i, nsize, osize, num;
124 ip = VTOI(vp);
125 fs = ip->i_lfs;
126 offset = blkoff(fs, startoffset);
127 KASSERT(iosize <= fs->lfs_bsize);
128 lbn = lblkno(fs, startoffset);
129 /* (void)lfs_check(vp, lbn, 0); */
131 ASSERT_MAYBE_SEGLOCK(fs);
134 * Three cases: it's a block beyond the end of file, it's a block in
135 * the file that may or may not have been assigned a disk address or
136 * we're writing an entire block.
138 * Note, if the daddr is UNWRITTEN, the block already exists in
139 * the cache (it was read or written earlier). If so, make sure
140 * we don't count it as a new block or zero out its contents. If
141 * it did not, make sure we allocate any necessary indirect
142 * blocks.
144 * If we are writing a block beyond the end of the file, we need to
145 * check if the old last block was a fragment. If it was, we need
146 * to rewrite it.
149 if (bpp)
150 *bpp = NULL;
152 /* Check for block beyond end of file and fragment extension needed. */
153 lastblock = lblkno(fs, ip->i_size);
154 if (lastblock < NDADDR && lastblock < lbn) {
155 osize = blksize(fs, ip, lastblock);
156 if (osize < fs->lfs_bsize && osize > 0) {
157 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
158 lastblock,
159 (bpp ? &bp : NULL), cred)))
160 return (error);
161 ip->i_ffs1_size = ip->i_size =
162 (lastblock + 1) * fs->lfs_bsize;
163 uvm_vnp_setsize(vp, ip->i_size);
164 ip->i_flag |= IN_CHANGE | IN_UPDATE;
165 if (bpp)
166 (void) VOP_BWRITE(bp);
171 * If the block we are writing is a direct block, it's the last
172 * block in the file, and offset + iosize is less than a full
173 * block, we can write one or more fragments. There are two cases:
174 * the block is brand new and we should allocate it the correct
175 * size or it already exists and contains some fragments and
176 * may need to extend it.
178 if (lbn < NDADDR && lblkno(fs, ip->i_size) <= lbn) {
179 osize = blksize(fs, ip, lbn);
180 nsize = fragroundup(fs, offset + iosize);
181 if (lblktosize(fs, lbn) >= ip->i_size) {
182 /* Brand new block or fragment */
183 frags = numfrags(fs, nsize);
184 bb = fragstofsb(fs, frags);
185 if (!ISSPACE(fs, bb, cred))
186 return ENOSPC;
187 if (bpp) {
188 *bpp = bp = getblk(vp, lbn, nsize, 0, 0);
189 bp->b_blkno = UNWRITTEN;
190 if (flags & B_CLRBUF)
191 clrbuf(bp);
193 ip->i_lfs_effnblks += bb;
194 mutex_enter(&lfs_lock);
195 fs->lfs_bfree -= bb;
196 mutex_exit(&lfs_lock);
197 ip->i_ffs1_db[lbn] = UNWRITTEN;
198 } else {
199 if (nsize <= osize) {
200 /* No need to extend */
201 if (bpp && (error = bread(vp, lbn, osize,
202 NOCRED, 0, &bp)))
203 return error;
204 } else {
205 /* Extend existing block */
206 if ((error =
207 lfs_fragextend(vp, osize, nsize, lbn,
208 (bpp ? &bp : NULL), cred)))
209 return error;
211 if (bpp)
212 *bpp = bp;
214 return 0;
217 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL, NULL);
218 if (error)
219 return (error);
221 daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
222 KASSERT(daddr <= LFS_MAX_DADDR);
225 * Do byte accounting all at once, so we can gracefully fail *before*
226 * we start assigning blocks.
228 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
229 bcount = 0;
230 if (daddr == UNASSIGNED) {
231 bcount = bb;
233 for (i = 1; i < num; ++i) {
234 if (!indirs[i].in_exists) {
235 bcount += bb;
238 if (ISSPACE(fs, bcount, cred)) {
239 mutex_enter(&lfs_lock);
240 fs->lfs_bfree -= bcount;
241 mutex_exit(&lfs_lock);
242 ip->i_lfs_effnblks += bcount;
243 } else {
244 return ENOSPC;
247 if (daddr == UNASSIGNED) {
248 if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
249 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
253 * Create new indirect blocks if necessary
255 if (num > 1) {
256 idaddr = ip->i_ffs1_ib[indirs[0].in_off];
257 for (i = 1; i < num; ++i) {
258 ibp = getblk(vp, indirs[i].in_lbn,
259 fs->lfs_bsize, 0,0);
260 if (!indirs[i].in_exists) {
261 clrbuf(ibp);
262 ibp->b_blkno = UNWRITTEN;
263 } else if (!(ibp->b_oflags & (BO_DELWRI | BO_DONE))) {
264 ibp->b_blkno = fsbtodb(fs, idaddr);
265 ibp->b_flags |= B_READ;
266 VOP_STRATEGY(vp, ibp);
267 biowait(ibp);
270 * This block exists, but the next one may not.
271 * If that is the case mark it UNWRITTEN to keep
272 * the accounting straight.
274 /* XXX ondisk32 */
275 if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
276 ((int32_t *)ibp->b_data)[indirs[i].in_off] =
277 UNWRITTEN;
278 /* XXX ondisk32 */
279 idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
280 #ifdef DEBUG
281 if (vp == fs->lfs_ivnode) {
282 LFS_ENTER_LOG("balloc", __FILE__,
283 __LINE__, indirs[i].in_lbn,
284 ibp->b_flags, curproc->p_pid);
286 #endif
287 if ((error = VOP_BWRITE(ibp)))
288 return error;
295 * Get the existing block from the cache, if requested.
297 frags = fsbtofrags(fs, bb);
298 if (bpp)
299 *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
302 * Do accounting on blocks that represent pages.
304 if (!bpp)
305 lfs_register_block(vp, lbn);
308 * The block we are writing may be a brand new block
309 * in which case we need to do accounting.
311 * We can tell a truly new block because ufs_bmaparray will say
312 * it is UNASSIGNED. Once we allocate it we will assign it the
313 * disk address UNWRITTEN.
315 if (daddr == UNASSIGNED) {
316 if (bpp) {
317 if (flags & B_CLRBUF)
318 clrbuf(bp);
320 /* Note the new address */
321 bp->b_blkno = UNWRITTEN;
324 switch (num) {
325 case 0:
326 ip->i_ffs1_db[lbn] = UNWRITTEN;
327 break;
328 case 1:
329 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
330 break;
331 default:
332 idp = &indirs[num - 1];
333 if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
334 B_MODIFY, &ibp))
335 panic("lfs_balloc: bread bno %lld",
336 (long long)idp->in_lbn);
337 /* XXX ondisk32 */
338 ((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
339 #ifdef DEBUG
340 if (vp == fs->lfs_ivnode) {
341 LFS_ENTER_LOG("balloc", __FILE__,
342 __LINE__, idp->in_lbn,
343 ibp->b_flags, curproc->p_pid);
345 #endif
346 VOP_BWRITE(ibp);
348 } else if (bpp && !(bp->b_oflags & (BO_DONE|BO_DELWRI))) {
350 * Not a brand new block, also not in the cache;
351 * read it in from disk.
353 if (iosize == fs->lfs_bsize)
354 /* Optimization: I/O is unnecessary. */
355 bp->b_blkno = daddr;
356 else {
358 * We need to read the block to preserve the
359 * existing bytes.
361 bp->b_blkno = daddr;
362 bp->b_flags |= B_READ;
363 VOP_STRATEGY(vp, bp);
364 return (biowait(bp));
368 return (0);
371 /* VOP_BWRITE 1 time */
373 lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp,
374 kauth_cred_t cred)
376 struct inode *ip;
377 struct lfs *fs;
378 long bb;
379 int error;
380 extern long locked_queue_bytes;
381 size_t obufsize;
383 ip = VTOI(vp);
384 fs = ip->i_lfs;
385 bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
386 error = 0;
388 ASSERT_NO_SEGLOCK(fs);
391 * Get the seglock so we don't enlarge blocks while a segment
392 * is being written. If we're called with bpp==NULL, though,
393 * we are only pretending to change a buffer, so we don't have to
394 * lock.
396 top:
397 if (bpp) {
398 rw_enter(&fs->lfs_fraglock, RW_READER);
399 LFS_DEBUG_COUNTLOCKED("frag");
402 if (!ISSPACE(fs, bb, cred)) {
403 error = ENOSPC;
404 goto out;
408 * If we are not asked to actually return the block, all we need
409 * to do is allocate space for it. UBC will handle dirtying the
410 * appropriate things and making sure it all goes to disk.
411 * Don't bother to read in that case.
413 if (bpp && (error = bread(vp, lbn, osize, NOCRED, 0, bpp))) {
414 brelse(*bpp, 0);
415 goto out;
417 #ifdef QUOTA
418 if ((error = chkdq(ip, bb, cred, 0))) {
419 if (bpp)
420 brelse(*bpp, 0);
421 goto out;
423 #endif
425 * Adjust accounting for lfs_avail. If there's not enough room,
426 * we will have to wait for the cleaner, which we can't do while
427 * holding a block busy or while holding the seglock. In that case,
428 * release both and start over after waiting.
431 if (bpp && ((*bpp)->b_oflags & BO_DELWRI)) {
432 if (!lfs_fits(fs, bb)) {
433 if (bpp)
434 brelse(*bpp, 0);
435 #ifdef QUOTA
436 chkdq(ip, -bb, cred, 0);
437 #endif
438 rw_exit(&fs->lfs_fraglock);
439 lfs_availwait(fs, bb);
440 goto top;
442 fs->lfs_avail -= bb;
445 mutex_enter(&lfs_lock);
446 fs->lfs_bfree -= bb;
447 mutex_exit(&lfs_lock);
448 ip->i_lfs_effnblks += bb;
449 ip->i_flag |= IN_CHANGE | IN_UPDATE;
451 if (bpp) {
452 obufsize = (*bpp)->b_bufsize;
453 allocbuf(*bpp, nsize, 1);
455 /* Adjust locked-list accounting */
456 if (((*bpp)->b_flags & B_LOCKED) != 0 &&
457 (*bpp)->b_iodone == NULL) {
458 mutex_enter(&lfs_lock);
459 locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
460 mutex_exit(&lfs_lock);
463 memset((char *)((*bpp)->b_data) + osize, 0, (u_int)(nsize - osize));
466 out:
467 if (bpp) {
468 rw_exit(&fs->lfs_fraglock);
470 return (error);
473 static inline int
474 lge(struct lbnentry *a, struct lbnentry *b)
476 return a->lbn - b->lbn;
479 SPLAY_PROTOTYPE(lfs_splay, lbnentry, entry, lge);
481 SPLAY_GENERATE(lfs_splay, lbnentry, entry, lge);
484 * Record this lbn as being "write pending". We used to have this information
485 * on the buffer headers, but since pages don't have buffer headers we
486 * record it here instead.
488 void
489 lfs_register_block(struct vnode *vp, daddr_t lbn)
491 struct lfs *fs;
492 struct inode *ip;
493 struct lbnentry *lbp;
495 ip = VTOI(vp);
497 /* Don't count metadata */
498 if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
499 return;
501 fs = ip->i_lfs;
503 ASSERT_NO_SEGLOCK(fs);
505 /* If no space, wait for the cleaner */
506 lfs_availwait(fs, btofsb(fs, 1 << fs->lfs_bshift));
508 lbp = (struct lbnentry *)pool_get(&lfs_lbnentry_pool, PR_WAITOK);
509 lbp->lbn = lbn;
510 mutex_enter(&lfs_lock);
511 if (SPLAY_INSERT(lfs_splay, &ip->i_lfs_lbtree, lbp) != NULL) {
512 mutex_exit(&lfs_lock);
513 /* Already there */
514 pool_put(&lfs_lbnentry_pool, lbp);
515 return;
518 ++ip->i_lfs_nbtree;
519 fs->lfs_favail += btofsb(fs, (1 << fs->lfs_bshift));
520 fs->lfs_pages += fs->lfs_bsize >> PAGE_SHIFT;
521 ++locked_fakequeue_count;
522 lfs_subsys_pages += fs->lfs_bsize >> PAGE_SHIFT;
523 mutex_exit(&lfs_lock);
526 static void
527 lfs_do_deregister(struct lfs *fs, struct inode *ip, struct lbnentry *lbp)
529 ASSERT_MAYBE_SEGLOCK(fs);
531 mutex_enter(&lfs_lock);
532 --ip->i_lfs_nbtree;
533 SPLAY_REMOVE(lfs_splay, &ip->i_lfs_lbtree, lbp);
534 if (fs->lfs_favail > btofsb(fs, (1 << fs->lfs_bshift)))
535 fs->lfs_favail -= btofsb(fs, (1 << fs->lfs_bshift));
536 fs->lfs_pages -= fs->lfs_bsize >> PAGE_SHIFT;
537 if (locked_fakequeue_count > 0)
538 --locked_fakequeue_count;
539 lfs_subsys_pages -= fs->lfs_bsize >> PAGE_SHIFT;
540 mutex_exit(&lfs_lock);
542 pool_put(&lfs_lbnentry_pool, lbp);
545 void
546 lfs_deregister_block(struct vnode *vp, daddr_t lbn)
548 struct lfs *fs;
549 struct inode *ip;
550 struct lbnentry *lbp;
551 struct lbnentry tmp;
553 ip = VTOI(vp);
555 /* Don't count metadata */
556 if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
557 return;
559 fs = ip->i_lfs;
560 tmp.lbn = lbn;
561 lbp = SPLAY_FIND(lfs_splay, &ip->i_lfs_lbtree, &tmp);
562 if (lbp == NULL)
563 return;
565 lfs_do_deregister(fs, ip, lbp);
568 void
569 lfs_deregister_all(struct vnode *vp)
571 struct lbnentry *lbp, *nlbp;
572 struct lfs_splay *hd;
573 struct lfs *fs;
574 struct inode *ip;
576 ip = VTOI(vp);
577 fs = ip->i_lfs;
578 hd = &ip->i_lfs_lbtree;
580 for (lbp = SPLAY_MIN(lfs_splay, hd); lbp != NULL; lbp = nlbp) {
581 nlbp = SPLAY_NEXT(lfs_splay, hd, lbp);
582 lfs_do_deregister(fs, ip, lbp);