xref: /minix3/sys/ufs/lfs/lfs_balloc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: lfs_balloc.c,v 1.87 2015/09/01 06:08:37 dholland Exp $	*/
2d65f6f70SBen Gras 
3d65f6f70SBen Gras /*-
4d65f6f70SBen Gras  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5d65f6f70SBen Gras  * All rights reserved.
6d65f6f70SBen Gras  *
7d65f6f70SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
8d65f6f70SBen Gras  * by Konrad E. Schroder <perseant@hhhh.org>.
9d65f6f70SBen Gras  *
10d65f6f70SBen Gras  * Redistribution and use in source and binary forms, with or without
11d65f6f70SBen Gras  * modification, are permitted provided that the following conditions
12d65f6f70SBen Gras  * are met:
13d65f6f70SBen Gras  * 1. Redistributions of source code must retain the above copyright
14d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer.
15d65f6f70SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
16d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer in the
17d65f6f70SBen Gras  *    documentation and/or other materials provided with the distribution.
18d65f6f70SBen Gras  *
19d65f6f70SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d65f6f70SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d65f6f70SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d65f6f70SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d65f6f70SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d65f6f70SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d65f6f70SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d65f6f70SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d65f6f70SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d65f6f70SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d65f6f70SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
30d65f6f70SBen Gras  */
31d65f6f70SBen Gras /*
32d65f6f70SBen Gras  * Copyright (c) 1989, 1991, 1993
33d65f6f70SBen Gras  *	The Regents of the University of California.  All rights reserved.
34d65f6f70SBen Gras  *
35d65f6f70SBen Gras  * Redistribution and use in source and binary forms, with or without
36d65f6f70SBen Gras  * modification, are permitted provided that the following conditions
37d65f6f70SBen Gras  * are met:
38d65f6f70SBen Gras  * 1. Redistributions of source code must retain the above copyright
39d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer.
40d65f6f70SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
41d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer in the
42d65f6f70SBen Gras  *    documentation and/or other materials provided with the distribution.
43d65f6f70SBen Gras  * 3. Neither the name of the University nor the names of its contributors
44d65f6f70SBen Gras  *    may be used to endorse or promote products derived from this software
45d65f6f70SBen Gras  *    without specific prior written permission.
46d65f6f70SBen Gras  *
47d65f6f70SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48d65f6f70SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49d65f6f70SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50d65f6f70SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51d65f6f70SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52d65f6f70SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53d65f6f70SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54d65f6f70SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55d65f6f70SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56d65f6f70SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57d65f6f70SBen Gras  * SUCH DAMAGE.
58d65f6f70SBen Gras  *
59d65f6f70SBen Gras  *	@(#)lfs_balloc.c	8.4 (Berkeley) 5/8/95
60d65f6f70SBen Gras  */
61d65f6f70SBen Gras 
62d65f6f70SBen Gras #include <sys/cdefs.h>
63*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.87 2015/09/01 06:08:37 dholland Exp $");
64d65f6f70SBen Gras 
65d65f6f70SBen Gras #if defined(_KERNEL_OPT)
66d65f6f70SBen Gras #include "opt_quota.h"
67d65f6f70SBen Gras #endif
68d65f6f70SBen Gras 
69d65f6f70SBen Gras #include <sys/param.h>
70d65f6f70SBen Gras #include <sys/systm.h>
71d65f6f70SBen Gras #include <sys/buf.h>
72d65f6f70SBen Gras #include <sys/proc.h>
73d65f6f70SBen Gras #include <sys/vnode.h>
74d65f6f70SBen Gras #include <sys/mount.h>
75d65f6f70SBen Gras #include <sys/resourcevar.h>
76d65f6f70SBen Gras #include <sys/tree.h>
77d65f6f70SBen Gras #include <sys/trace.h>
78d65f6f70SBen Gras #include <sys/kauth.h>
79d65f6f70SBen Gras 
80d65f6f70SBen Gras #include <miscfs/specfs/specdev.h>
81d65f6f70SBen Gras 
8284d9c625SLionel Sambuc #include <ufs/lfs/ulfs_quotacommon.h>
8384d9c625SLionel Sambuc #include <ufs/lfs/ulfs_inode.h>
8484d9c625SLionel Sambuc #include <ufs/lfs/ulfsmount.h>
8584d9c625SLionel Sambuc #include <ufs/lfs/ulfs_extern.h>
86d65f6f70SBen Gras 
87d65f6f70SBen Gras #include <ufs/lfs/lfs.h>
88*0a6a1f1dSLionel Sambuc #include <ufs/lfs/lfs_accessors.h>
89d65f6f70SBen Gras #include <ufs/lfs/lfs_extern.h>
9084d9c625SLionel Sambuc #include <ufs/lfs/lfs_kernel.h>
91d65f6f70SBen Gras 
92d65f6f70SBen Gras #include <uvm/uvm.h>
93d65f6f70SBen Gras 
94d65f6f70SBen Gras int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, kauth_cred_t);
95d65f6f70SBen Gras 
96d65f6f70SBen Gras u_int64_t locked_fakequeue_count;
97d65f6f70SBen Gras 
98d65f6f70SBen Gras /*
99d65f6f70SBen Gras  * Allocate a block, and to inode and filesystem block accounting for it
100d65f6f70SBen Gras  * and for any indirect blocks the may need to be created in order for
101d65f6f70SBen Gras  * this block to be created.
102d65f6f70SBen Gras  *
103d65f6f70SBen Gras  * Blocks which have never been accounted for (i.e., which "do not exist")
10484d9c625SLionel Sambuc  * have disk address 0, which is translated by ulfs_bmap to the special value
10584d9c625SLionel Sambuc  * UNASSIGNED == -1, as in the historical ULFS.
106d65f6f70SBen Gras  *
107d65f6f70SBen Gras  * Blocks which have been accounted for but which have not yet been written
108d65f6f70SBen Gras  * to disk are given the new special disk address UNWRITTEN == -2, so that
109d65f6f70SBen Gras  * they can be differentiated from completely new blocks.
110d65f6f70SBen Gras  */
11184d9c625SLionel Sambuc /* VOP_BWRITE ULFS_NIADDR+2 times */
112d65f6f70SBen Gras int
lfs_balloc(struct vnode * vp,off_t startoffset,int iosize,kauth_cred_t cred,int flags,struct buf ** bpp)113d65f6f70SBen Gras lfs_balloc(struct vnode *vp, off_t startoffset, int iosize, kauth_cred_t cred,
114d65f6f70SBen Gras     int flags, struct buf **bpp)
115d65f6f70SBen Gras {
116d65f6f70SBen Gras 	int offset;
117d65f6f70SBen Gras 	daddr_t daddr, idaddr;
118d65f6f70SBen Gras 	struct buf *ibp, *bp;
119d65f6f70SBen Gras 	struct inode *ip;
120d65f6f70SBen Gras 	struct lfs *fs;
12184d9c625SLionel Sambuc 	struct indir indirs[ULFS_NIADDR+2], *idp;
122d65f6f70SBen Gras 	daddr_t	lbn, lastblock;
123d65f6f70SBen Gras 	int bcount;
124d65f6f70SBen Gras 	int error, frags, i, nsize, osize, num;
125d65f6f70SBen Gras 
126d65f6f70SBen Gras 	ip = VTOI(vp);
127d65f6f70SBen Gras 	fs = ip->i_lfs;
12884d9c625SLionel Sambuc 	offset = lfs_blkoff(fs, startoffset);
129*0a6a1f1dSLionel Sambuc 	KASSERT(iosize <= lfs_sb_getbsize(fs));
13084d9c625SLionel Sambuc 	lbn = lfs_lblkno(fs, startoffset);
131d65f6f70SBen Gras 	/* (void)lfs_check(vp, lbn, 0); */
132d65f6f70SBen Gras 
133d65f6f70SBen Gras 	ASSERT_MAYBE_SEGLOCK(fs);
134d65f6f70SBen Gras 
135d65f6f70SBen Gras 	/*
136d65f6f70SBen Gras 	 * Three cases: it's a block beyond the end of file, it's a block in
137d65f6f70SBen Gras 	 * the file that may or may not have been assigned a disk address or
138d65f6f70SBen Gras 	 * we're writing an entire block.
139d65f6f70SBen Gras 	 *
140d65f6f70SBen Gras 	 * Note, if the daddr is UNWRITTEN, the block already exists in
141d65f6f70SBen Gras 	 * the cache (it was read or written earlier).	If so, make sure
142d65f6f70SBen Gras 	 * we don't count it as a new block or zero out its contents. If
143d65f6f70SBen Gras 	 * it did not, make sure we allocate any necessary indirect
144d65f6f70SBen Gras 	 * blocks.
145d65f6f70SBen Gras 	 *
146d65f6f70SBen Gras 	 * If we are writing a block beyond the end of the file, we need to
147d65f6f70SBen Gras 	 * check if the old last block was a fragment.	If it was, we need
148d65f6f70SBen Gras 	 * to rewrite it.
149d65f6f70SBen Gras 	 */
150d65f6f70SBen Gras 
151d65f6f70SBen Gras 	if (bpp)
152d65f6f70SBen Gras 		*bpp = NULL;
153d65f6f70SBen Gras 
154d65f6f70SBen Gras 	/* Check for block beyond end of file and fragment extension needed. */
15584d9c625SLionel Sambuc 	lastblock = lfs_lblkno(fs, ip->i_size);
15684d9c625SLionel Sambuc 	if (lastblock < ULFS_NDADDR && lastblock < lbn) {
15784d9c625SLionel Sambuc 		osize = lfs_blksize(fs, ip, lastblock);
158*0a6a1f1dSLionel Sambuc 		if (osize < lfs_sb_getbsize(fs) && osize > 0) {
159*0a6a1f1dSLionel Sambuc 			if ((error = lfs_fragextend(vp, osize, lfs_sb_getbsize(fs),
160d65f6f70SBen Gras 						    lastblock,
161d65f6f70SBen Gras 						    (bpp ? &bp : NULL), cred)))
162d65f6f70SBen Gras 				return (error);
163*0a6a1f1dSLionel Sambuc 			ip->i_size = (lastblock + 1) * lfs_sb_getbsize(fs);
164*0a6a1f1dSLionel Sambuc 			lfs_dino_setsize(fs, ip->i_din, ip->i_size);
165d65f6f70SBen Gras 			uvm_vnp_setsize(vp, ip->i_size);
166d65f6f70SBen Gras 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
167d65f6f70SBen Gras 			if (bpp)
168d65f6f70SBen Gras 				(void) VOP_BWRITE(bp->b_vp, bp);
169d65f6f70SBen Gras 		}
170d65f6f70SBen Gras 	}
171d65f6f70SBen Gras 
172d65f6f70SBen Gras 	/*
173d65f6f70SBen Gras 	 * If the block we are writing is a direct block, it's the last
174d65f6f70SBen Gras 	 * block in the file, and offset + iosize is less than a full
175d65f6f70SBen Gras 	 * block, we can write one or more fragments.  There are two cases:
176d65f6f70SBen Gras 	 * the block is brand new and we should allocate it the correct
177d65f6f70SBen Gras 	 * size or it already exists and contains some fragments and
178d65f6f70SBen Gras 	 * may need to extend it.
179d65f6f70SBen Gras 	 */
18084d9c625SLionel Sambuc 	if (lbn < ULFS_NDADDR && lfs_lblkno(fs, ip->i_size) <= lbn) {
18184d9c625SLionel Sambuc 		osize = lfs_blksize(fs, ip, lbn);
18284d9c625SLionel Sambuc 		nsize = lfs_fragroundup(fs, offset + iosize);
18384d9c625SLionel Sambuc 		if (lfs_lblktosize(fs, lbn) >= ip->i_size) {
184d65f6f70SBen Gras 			/* Brand new block or fragment */
18584d9c625SLionel Sambuc 			frags = lfs_numfrags(fs, nsize);
186d65f6f70SBen Gras 			if (!ISSPACE(fs, frags, cred))
187d65f6f70SBen Gras 				return ENOSPC;
188d65f6f70SBen Gras 			if (bpp) {
189d65f6f70SBen Gras 				*bpp = bp = getblk(vp, lbn, nsize, 0, 0);
190d65f6f70SBen Gras 				bp->b_blkno = UNWRITTEN;
191d65f6f70SBen Gras 				if (flags & B_CLRBUF)
192d65f6f70SBen Gras 					clrbuf(bp);
193d65f6f70SBen Gras 			}
194d65f6f70SBen Gras 			ip->i_lfs_effnblks += frags;
195d65f6f70SBen Gras 			mutex_enter(&lfs_lock);
196*0a6a1f1dSLionel Sambuc 			lfs_sb_subbfree(fs, frags);
197d65f6f70SBen Gras 			mutex_exit(&lfs_lock);
198*0a6a1f1dSLionel Sambuc 			lfs_dino_setdb(fs, ip->i_din, lbn, UNWRITTEN);
199d65f6f70SBen Gras 		} else {
200d65f6f70SBen Gras 			if (nsize <= osize) {
201d65f6f70SBen Gras 				/* No need to extend */
202d65f6f70SBen Gras 				if (bpp && (error = bread(vp, lbn, osize,
203*0a6a1f1dSLionel Sambuc 				    0, &bp)))
204d65f6f70SBen Gras 					return error;
205d65f6f70SBen Gras 			} else {
206d65f6f70SBen Gras 				/* Extend existing block */
207d65f6f70SBen Gras 				if ((error =
208d65f6f70SBen Gras 				     lfs_fragextend(vp, osize, nsize, lbn,
209d65f6f70SBen Gras 						    (bpp ? &bp : NULL), cred)))
210d65f6f70SBen Gras 					return error;
211d65f6f70SBen Gras 			}
212d65f6f70SBen Gras 			if (bpp)
213d65f6f70SBen Gras 				*bpp = bp;
214d65f6f70SBen Gras 		}
215d65f6f70SBen Gras 		return 0;
216d65f6f70SBen Gras 	}
217d65f6f70SBen Gras 
21884d9c625SLionel Sambuc 	error = ulfs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL, NULL);
219d65f6f70SBen Gras 	if (error)
220d65f6f70SBen Gras 		return (error);
221d65f6f70SBen Gras 
222*0a6a1f1dSLionel Sambuc 	KASSERT(daddr <= LFS_MAX_DADDR(fs));
223d65f6f70SBen Gras 
224d65f6f70SBen Gras 	/*
225d65f6f70SBen Gras 	 * Do byte accounting all at once, so we can gracefully fail *before*
226d65f6f70SBen Gras 	 * we start assigning blocks.
227d65f6f70SBen Gras 	 */
22884d9c625SLionel Sambuc 	frags = fs->um_seqinc;
229d65f6f70SBen Gras 	bcount = 0;
230d65f6f70SBen Gras 	if (daddr == UNASSIGNED) {
231d65f6f70SBen Gras 		bcount = frags;
232d65f6f70SBen Gras 	}
233d65f6f70SBen Gras 	for (i = 1; i < num; ++i) {
234d65f6f70SBen Gras 		if (!indirs[i].in_exists) {
235d65f6f70SBen Gras 			bcount += frags;
236d65f6f70SBen Gras 		}
237d65f6f70SBen Gras 	}
238d65f6f70SBen Gras 	if (ISSPACE(fs, bcount, cred)) {
239d65f6f70SBen Gras 		mutex_enter(&lfs_lock);
240*0a6a1f1dSLionel Sambuc 		lfs_sb_subbfree(fs, bcount);
241d65f6f70SBen Gras 		mutex_exit(&lfs_lock);
242d65f6f70SBen Gras 		ip->i_lfs_effnblks += bcount;
243d65f6f70SBen Gras 	} else {
244d65f6f70SBen Gras 		return ENOSPC;
245d65f6f70SBen Gras 	}
246d65f6f70SBen Gras 
247d65f6f70SBen Gras 	if (daddr == UNASSIGNED) {
248*0a6a1f1dSLionel Sambuc 		if (num > 0 && lfs_dino_getib(fs, ip->i_din, indirs[0].in_off) == 0) {
249*0a6a1f1dSLionel Sambuc 			lfs_dino_setib(fs, ip->i_din, indirs[0].in_off, UNWRITTEN);
250d65f6f70SBen Gras 		}
251d65f6f70SBen Gras 
252d65f6f70SBen Gras 		/*
253d65f6f70SBen Gras 		 * Create new indirect blocks if necessary
254d65f6f70SBen Gras 		 */
255d65f6f70SBen Gras 		if (num > 1) {
256*0a6a1f1dSLionel Sambuc 			idaddr = lfs_dino_getib(fs, ip->i_din, indirs[0].in_off);
257d65f6f70SBen Gras 			for (i = 1; i < num; ++i) {
258d65f6f70SBen Gras 				ibp = getblk(vp, indirs[i].in_lbn,
259*0a6a1f1dSLionel Sambuc 				    lfs_sb_getbsize(fs), 0,0);
260d65f6f70SBen Gras 				if (!indirs[i].in_exists) {
261d65f6f70SBen Gras 					clrbuf(ibp);
262d65f6f70SBen Gras 					ibp->b_blkno = UNWRITTEN;
263d65f6f70SBen Gras 				} else if (!(ibp->b_oflags & (BO_DELWRI | BO_DONE))) {
26484d9c625SLionel Sambuc 					ibp->b_blkno = LFS_FSBTODB(fs, idaddr);
265d65f6f70SBen Gras 					ibp->b_flags |= B_READ;
266d65f6f70SBen Gras 					VOP_STRATEGY(vp, ibp);
267d65f6f70SBen Gras 					biowait(ibp);
268d65f6f70SBen Gras 				}
269d65f6f70SBen Gras 				/*
270d65f6f70SBen Gras 				 * This block exists, but the next one may not.
271d65f6f70SBen Gras 				 * If that is the case mark it UNWRITTEN to keep
272d65f6f70SBen Gras 				 * the accounting straight.
273d65f6f70SBen Gras 				 */
274d65f6f70SBen Gras 				/* XXX ondisk32 */
275d65f6f70SBen Gras 				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
276d65f6f70SBen Gras 					((int32_t *)ibp->b_data)[indirs[i].in_off] =
277d65f6f70SBen Gras 						UNWRITTEN;
278d65f6f70SBen Gras 				/* XXX ondisk32 */
279d65f6f70SBen Gras 				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
280d65f6f70SBen Gras #ifdef DEBUG
281d65f6f70SBen Gras 				if (vp == fs->lfs_ivnode) {
282d65f6f70SBen Gras 					LFS_ENTER_LOG("balloc", __FILE__,
283d65f6f70SBen Gras 						__LINE__, indirs[i].in_lbn,
284d65f6f70SBen Gras 						ibp->b_flags, curproc->p_pid);
285d65f6f70SBen Gras 				}
286d65f6f70SBen Gras #endif
287d65f6f70SBen Gras 				if ((error = VOP_BWRITE(ibp->b_vp, ibp)))
288d65f6f70SBen Gras 					return error;
289d65f6f70SBen Gras 			}
290d65f6f70SBen Gras 		}
291d65f6f70SBen Gras 	}
292d65f6f70SBen Gras 
293d65f6f70SBen Gras 
294d65f6f70SBen Gras 	/*
295d65f6f70SBen Gras 	 * Get the existing block from the cache, if requested.
296d65f6f70SBen Gras 	 */
297d65f6f70SBen Gras 	if (bpp)
29884d9c625SLionel Sambuc 		*bpp = bp = getblk(vp, lbn, lfs_blksize(fs, ip, lbn), 0, 0);
299d65f6f70SBen Gras 
300d65f6f70SBen Gras 	/*
301d65f6f70SBen Gras 	 * Do accounting on blocks that represent pages.
302d65f6f70SBen Gras 	 */
303d65f6f70SBen Gras 	if (!bpp)
304d65f6f70SBen Gras 		lfs_register_block(vp, lbn);
305d65f6f70SBen Gras 
306d65f6f70SBen Gras 	/*
307d65f6f70SBen Gras 	 * The block we are writing may be a brand new block
308d65f6f70SBen Gras 	 * in which case we need to do accounting.
309d65f6f70SBen Gras 	 *
31084d9c625SLionel Sambuc 	 * We can tell a truly new block because ulfs_bmaparray will say
311d65f6f70SBen Gras 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
312d65f6f70SBen Gras 	 * disk address UNWRITTEN.
313d65f6f70SBen Gras 	 */
314d65f6f70SBen Gras 	if (daddr == UNASSIGNED) {
315d65f6f70SBen Gras 		if (bpp) {
316d65f6f70SBen Gras 			if (flags & B_CLRBUF)
317d65f6f70SBen Gras 				clrbuf(bp);
318d65f6f70SBen Gras 
319d65f6f70SBen Gras 			/* Note the new address */
320d65f6f70SBen Gras 			bp->b_blkno = UNWRITTEN;
321d65f6f70SBen Gras 		}
322d65f6f70SBen Gras 
323d65f6f70SBen Gras 		switch (num) {
324d65f6f70SBen Gras 		    case 0:
325*0a6a1f1dSLionel Sambuc 			lfs_dino_setdb(fs, ip->i_din, lbn, UNWRITTEN);
326d65f6f70SBen Gras 			break;
327d65f6f70SBen Gras 		    case 1:
328*0a6a1f1dSLionel Sambuc 			lfs_dino_setib(fs, ip->i_din, indirs[0].in_off, UNWRITTEN);
329d65f6f70SBen Gras 			break;
330d65f6f70SBen Gras 		    default:
331d65f6f70SBen Gras 			idp = &indirs[num - 1];
332*0a6a1f1dSLionel Sambuc 			if (bread(vp, idp->in_lbn, lfs_sb_getbsize(fs),
333d65f6f70SBen Gras 				  B_MODIFY, &ibp))
334d65f6f70SBen Gras 				panic("lfs_balloc: bread bno %lld",
335d65f6f70SBen Gras 				    (long long)idp->in_lbn);
336d65f6f70SBen Gras 			/* XXX ondisk32 */
337d65f6f70SBen Gras 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
338d65f6f70SBen Gras #ifdef DEBUG
339d65f6f70SBen Gras 			if (vp == fs->lfs_ivnode) {
340d65f6f70SBen Gras 				LFS_ENTER_LOG("balloc", __FILE__,
341d65f6f70SBen Gras 					__LINE__, idp->in_lbn,
342d65f6f70SBen Gras 					ibp->b_flags, curproc->p_pid);
343d65f6f70SBen Gras 			}
344d65f6f70SBen Gras #endif
345d65f6f70SBen Gras 			VOP_BWRITE(ibp->b_vp, ibp);
346d65f6f70SBen Gras 		}
347d65f6f70SBen Gras 	} else if (bpp && !(bp->b_oflags & (BO_DONE|BO_DELWRI))) {
348d65f6f70SBen Gras 		/*
349d65f6f70SBen Gras 		 * Not a brand new block, also not in the cache;
350d65f6f70SBen Gras 		 * read it in from disk.
351d65f6f70SBen Gras 		 */
352*0a6a1f1dSLionel Sambuc 		if (iosize == lfs_sb_getbsize(fs))
353d65f6f70SBen Gras 			/* Optimization: I/O is unnecessary. */
354d65f6f70SBen Gras 			bp->b_blkno = daddr;
355d65f6f70SBen Gras 		else {
356d65f6f70SBen Gras 			/*
357d65f6f70SBen Gras 			 * We need to read the block to preserve the
358d65f6f70SBen Gras 			 * existing bytes.
359d65f6f70SBen Gras 			 */
360d65f6f70SBen Gras 			bp->b_blkno = daddr;
361d65f6f70SBen Gras 			bp->b_flags |= B_READ;
362d65f6f70SBen Gras 			VOP_STRATEGY(vp, bp);
363d65f6f70SBen Gras 			return (biowait(bp));
364d65f6f70SBen Gras 		}
365d65f6f70SBen Gras 	}
366d65f6f70SBen Gras 
367d65f6f70SBen Gras 	return (0);
368d65f6f70SBen Gras }
369d65f6f70SBen Gras 
370d65f6f70SBen Gras /* VOP_BWRITE 1 time */
371d65f6f70SBen Gras int
lfs_fragextend(struct vnode * vp,int osize,int nsize,daddr_t lbn,struct buf ** bpp,kauth_cred_t cred)372d65f6f70SBen Gras lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp,
373d65f6f70SBen Gras     kauth_cred_t cred)
374d65f6f70SBen Gras {
375d65f6f70SBen Gras 	struct inode *ip;
376d65f6f70SBen Gras 	struct lfs *fs;
377d65f6f70SBen Gras 	long frags;
378d65f6f70SBen Gras 	int error;
379d65f6f70SBen Gras 	extern long locked_queue_bytes;
380d65f6f70SBen Gras 	size_t obufsize;
381d65f6f70SBen Gras 
382d65f6f70SBen Gras 	ip = VTOI(vp);
383d65f6f70SBen Gras 	fs = ip->i_lfs;
38484d9c625SLionel Sambuc 	frags = (long)lfs_numfrags(fs, nsize - osize);
385d65f6f70SBen Gras 	error = 0;
386d65f6f70SBen Gras 
387d65f6f70SBen Gras 	ASSERT_NO_SEGLOCK(fs);
388d65f6f70SBen Gras 
389d65f6f70SBen Gras 	/*
390d65f6f70SBen Gras 	 * Get the seglock so we don't enlarge blocks while a segment
391d65f6f70SBen Gras 	 * is being written.  If we're called with bpp==NULL, though,
392d65f6f70SBen Gras 	 * we are only pretending to change a buffer, so we don't have to
393d65f6f70SBen Gras 	 * lock.
394d65f6f70SBen Gras 	 */
395d65f6f70SBen Gras     top:
396d65f6f70SBen Gras 	if (bpp) {
397d65f6f70SBen Gras 		rw_enter(&fs->lfs_fraglock, RW_READER);
398d65f6f70SBen Gras 		LFS_DEBUG_COUNTLOCKED("frag");
399d65f6f70SBen Gras 	}
400d65f6f70SBen Gras 
401d65f6f70SBen Gras 	if (!ISSPACE(fs, frags, cred)) {
402d65f6f70SBen Gras 		error = ENOSPC;
403d65f6f70SBen Gras 		goto out;
404d65f6f70SBen Gras 	}
405d65f6f70SBen Gras 
406d65f6f70SBen Gras 	/*
407d65f6f70SBen Gras 	 * If we are not asked to actually return the block, all we need
408d65f6f70SBen Gras 	 * to do is allocate space for it.  UBC will handle dirtying the
409d65f6f70SBen Gras 	 * appropriate things and making sure it all goes to disk.
410d65f6f70SBen Gras 	 * Don't bother to read in that case.
411d65f6f70SBen Gras 	 */
412*0a6a1f1dSLionel Sambuc 	if (bpp && (error = bread(vp, lbn, osize, 0, bpp))) {
413d65f6f70SBen Gras 		goto out;
414d65f6f70SBen Gras 	}
41584d9c625SLionel Sambuc #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
41684d9c625SLionel Sambuc 	if ((error = lfs_chkdq(ip, frags, cred, 0))) {
417d65f6f70SBen Gras 		if (bpp)
418d65f6f70SBen Gras 			brelse(*bpp, 0);
419d65f6f70SBen Gras 		goto out;
420d65f6f70SBen Gras 	}
421d65f6f70SBen Gras #endif
422d65f6f70SBen Gras 	/*
423d65f6f70SBen Gras 	 * Adjust accounting for lfs_avail.  If there's not enough room,
424d65f6f70SBen Gras 	 * we will have to wait for the cleaner, which we can't do while
425d65f6f70SBen Gras 	 * holding a block busy or while holding the seglock.  In that case,
426d65f6f70SBen Gras 	 * release both and start over after waiting.
427d65f6f70SBen Gras 	 */
428d65f6f70SBen Gras 
429d65f6f70SBen Gras 	if (bpp && ((*bpp)->b_oflags & BO_DELWRI)) {
430d65f6f70SBen Gras 		if (!lfs_fits(fs, frags)) {
431d65f6f70SBen Gras 			if (bpp)
432d65f6f70SBen Gras 				brelse(*bpp, 0);
43384d9c625SLionel Sambuc #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
43484d9c625SLionel Sambuc 			lfs_chkdq(ip, -frags, cred, 0);
435d65f6f70SBen Gras #endif
436d65f6f70SBen Gras 			rw_exit(&fs->lfs_fraglock);
437d65f6f70SBen Gras 			lfs_availwait(fs, frags);
438d65f6f70SBen Gras 			goto top;
439d65f6f70SBen Gras 		}
440*0a6a1f1dSLionel Sambuc 		lfs_sb_subavail(fs, frags);
441d65f6f70SBen Gras 	}
442d65f6f70SBen Gras 
443d65f6f70SBen Gras 	mutex_enter(&lfs_lock);
444*0a6a1f1dSLionel Sambuc 	lfs_sb_subbfree(fs, frags);
445d65f6f70SBen Gras 	mutex_exit(&lfs_lock);
446d65f6f70SBen Gras 	ip->i_lfs_effnblks += frags;
447d65f6f70SBen Gras 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
448d65f6f70SBen Gras 
449d65f6f70SBen Gras 	if (bpp) {
450d65f6f70SBen Gras 		obufsize = (*bpp)->b_bufsize;
451d65f6f70SBen Gras 		allocbuf(*bpp, nsize, 1);
452d65f6f70SBen Gras 
453d65f6f70SBen Gras 		/* Adjust locked-list accounting */
454d65f6f70SBen Gras 		if (((*bpp)->b_flags & B_LOCKED) != 0 &&
455d65f6f70SBen Gras 		    (*bpp)->b_iodone == NULL) {
456d65f6f70SBen Gras 			mutex_enter(&lfs_lock);
457d65f6f70SBen Gras 			locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
458d65f6f70SBen Gras 			mutex_exit(&lfs_lock);
459d65f6f70SBen Gras 		}
460d65f6f70SBen Gras 
461d65f6f70SBen Gras 		memset((char *)((*bpp)->b_data) + osize, 0, (u_int)(nsize - osize));
462d65f6f70SBen Gras 	}
463d65f6f70SBen Gras 
464d65f6f70SBen Gras     out:
465d65f6f70SBen Gras 	if (bpp) {
466d65f6f70SBen Gras 		rw_exit(&fs->lfs_fraglock);
467d65f6f70SBen Gras 	}
468d65f6f70SBen Gras 	return (error);
469d65f6f70SBen Gras }
470d65f6f70SBen Gras 
471d65f6f70SBen Gras static inline int
lge(struct lbnentry * a,struct lbnentry * b)472d65f6f70SBen Gras lge(struct lbnentry *a, struct lbnentry *b)
473d65f6f70SBen Gras {
474d65f6f70SBen Gras 	return a->lbn - b->lbn;
475d65f6f70SBen Gras }
476d65f6f70SBen Gras 
477d65f6f70SBen Gras SPLAY_PROTOTYPE(lfs_splay, lbnentry, entry, lge);
478d65f6f70SBen Gras 
479d65f6f70SBen Gras SPLAY_GENERATE(lfs_splay, lbnentry, entry, lge);
480d65f6f70SBen Gras 
481d65f6f70SBen Gras /*
482d65f6f70SBen Gras  * Record this lbn as being "write pending".  We used to have this information
483d65f6f70SBen Gras  * on the buffer headers, but since pages don't have buffer headers we
484d65f6f70SBen Gras  * record it here instead.
485d65f6f70SBen Gras  */
486d65f6f70SBen Gras void
lfs_register_block(struct vnode * vp,daddr_t lbn)487d65f6f70SBen Gras lfs_register_block(struct vnode *vp, daddr_t lbn)
488d65f6f70SBen Gras {
489d65f6f70SBen Gras 	struct lfs *fs;
490d65f6f70SBen Gras 	struct inode *ip;
491d65f6f70SBen Gras 	struct lbnentry *lbp;
492d65f6f70SBen Gras 
493d65f6f70SBen Gras 	ip = VTOI(vp);
494d65f6f70SBen Gras 
495d65f6f70SBen Gras 	/* Don't count metadata */
496d65f6f70SBen Gras 	if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
497d65f6f70SBen Gras 		return;
498d65f6f70SBen Gras 
499d65f6f70SBen Gras 	fs = ip->i_lfs;
500d65f6f70SBen Gras 
501d65f6f70SBen Gras 	ASSERT_NO_SEGLOCK(fs);
502d65f6f70SBen Gras 
503d65f6f70SBen Gras 	/* If no space, wait for the cleaner */
504*0a6a1f1dSLionel Sambuc 	lfs_availwait(fs, lfs_btofsb(fs, 1 << lfs_sb_getbshift(fs)));
505d65f6f70SBen Gras 
506d65f6f70SBen Gras 	lbp = (struct lbnentry *)pool_get(&lfs_lbnentry_pool, PR_WAITOK);
507d65f6f70SBen Gras 	lbp->lbn = lbn;
508d65f6f70SBen Gras 	mutex_enter(&lfs_lock);
509d65f6f70SBen Gras 	if (SPLAY_INSERT(lfs_splay, &ip->i_lfs_lbtree, lbp) != NULL) {
510d65f6f70SBen Gras 		mutex_exit(&lfs_lock);
511d65f6f70SBen Gras 		/* Already there */
512d65f6f70SBen Gras 		pool_put(&lfs_lbnentry_pool, lbp);
513d65f6f70SBen Gras 		return;
514d65f6f70SBen Gras 	}
515d65f6f70SBen Gras 
516d65f6f70SBen Gras 	++ip->i_lfs_nbtree;
517*0a6a1f1dSLionel Sambuc 	fs->lfs_favail += lfs_btofsb(fs, (1 << lfs_sb_getbshift(fs)));
518*0a6a1f1dSLionel Sambuc 	fs->lfs_pages += lfs_sb_getbsize(fs) >> PAGE_SHIFT;
519d65f6f70SBen Gras 	++locked_fakequeue_count;
520*0a6a1f1dSLionel Sambuc 	lfs_subsys_pages += lfs_sb_getbsize(fs) >> PAGE_SHIFT;
521d65f6f70SBen Gras 	mutex_exit(&lfs_lock);
522d65f6f70SBen Gras }
523d65f6f70SBen Gras 
524d65f6f70SBen Gras static void
lfs_do_deregister(struct lfs * fs,struct inode * ip,struct lbnentry * lbp)525d65f6f70SBen Gras lfs_do_deregister(struct lfs *fs, struct inode *ip, struct lbnentry *lbp)
526d65f6f70SBen Gras {
527d65f6f70SBen Gras 	ASSERT_MAYBE_SEGLOCK(fs);
528d65f6f70SBen Gras 
529d65f6f70SBen Gras 	mutex_enter(&lfs_lock);
530d65f6f70SBen Gras 	--ip->i_lfs_nbtree;
531d65f6f70SBen Gras 	SPLAY_REMOVE(lfs_splay, &ip->i_lfs_lbtree, lbp);
532*0a6a1f1dSLionel Sambuc 	if (fs->lfs_favail > lfs_btofsb(fs, (1 << lfs_sb_getbshift(fs))))
533*0a6a1f1dSLionel Sambuc 		fs->lfs_favail -= lfs_btofsb(fs, (1 << lfs_sb_getbshift(fs)));
534*0a6a1f1dSLionel Sambuc 	fs->lfs_pages -= lfs_sb_getbsize(fs) >> PAGE_SHIFT;
535d65f6f70SBen Gras 	if (locked_fakequeue_count > 0)
536d65f6f70SBen Gras 		--locked_fakequeue_count;
537*0a6a1f1dSLionel Sambuc 	lfs_subsys_pages -= lfs_sb_getbsize(fs) >> PAGE_SHIFT;
538d65f6f70SBen Gras 	mutex_exit(&lfs_lock);
539d65f6f70SBen Gras 
540d65f6f70SBen Gras 	pool_put(&lfs_lbnentry_pool, lbp);
541d65f6f70SBen Gras }
542d65f6f70SBen Gras 
543d65f6f70SBen Gras void
lfs_deregister_block(struct vnode * vp,daddr_t lbn)544d65f6f70SBen Gras lfs_deregister_block(struct vnode *vp, daddr_t lbn)
545d65f6f70SBen Gras {
546d65f6f70SBen Gras 	struct lfs *fs;
547d65f6f70SBen Gras 	struct inode *ip;
548d65f6f70SBen Gras 	struct lbnentry *lbp;
549d65f6f70SBen Gras 	struct lbnentry tmp;
550d65f6f70SBen Gras 
551d65f6f70SBen Gras 	ip = VTOI(vp);
552d65f6f70SBen Gras 
553d65f6f70SBen Gras 	/* Don't count metadata */
554d65f6f70SBen Gras 	if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
555d65f6f70SBen Gras 		return;
556d65f6f70SBen Gras 
557d65f6f70SBen Gras 	fs = ip->i_lfs;
558d65f6f70SBen Gras 	tmp.lbn = lbn;
559d65f6f70SBen Gras 	lbp = SPLAY_FIND(lfs_splay, &ip->i_lfs_lbtree, &tmp);
560d65f6f70SBen Gras 	if (lbp == NULL)
561d65f6f70SBen Gras 		return;
562d65f6f70SBen Gras 
563d65f6f70SBen Gras 	lfs_do_deregister(fs, ip, lbp);
564d65f6f70SBen Gras }
565d65f6f70SBen Gras 
566d65f6f70SBen Gras void
lfs_deregister_all(struct vnode * vp)567d65f6f70SBen Gras lfs_deregister_all(struct vnode *vp)
568d65f6f70SBen Gras {
569d65f6f70SBen Gras 	struct lbnentry *lbp, *nlbp;
570d65f6f70SBen Gras 	struct lfs_splay *hd;
571d65f6f70SBen Gras 	struct lfs *fs;
572d65f6f70SBen Gras 	struct inode *ip;
573d65f6f70SBen Gras 
574d65f6f70SBen Gras 	ip = VTOI(vp);
575d65f6f70SBen Gras 	fs = ip->i_lfs;
576d65f6f70SBen Gras 	hd = &ip->i_lfs_lbtree;
577d65f6f70SBen Gras 
578d65f6f70SBen Gras 	for (lbp = SPLAY_MIN(lfs_splay, hd); lbp != NULL; lbp = nlbp) {
579d65f6f70SBen Gras 		nlbp = SPLAY_NEXT(lfs_splay, hd, lbp);
580d65f6f70SBen Gras 		lfs_do_deregister(fs, ip, lbp);
581d65f6f70SBen Gras 	}
582d65f6f70SBen Gras }
583