xref: /netbsd-src/sys/ufs/lfs/lfs_balloc.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: lfs_balloc.c,v 1.17 2000/05/30 04:08:41 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@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 
98 #include <uvm/uvm_extern.h>
99 
100 int lfs_fragextend __P((struct vnode *, int, int, ufs_daddr_t, struct buf **));
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 int
116 lfs_balloc(v)
117 	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 lbn;
131 	struct buf *ibp, *bp;
132 	struct inode *ip;
133 	struct lfs *fs;
134 	struct indir indirs[NIADDR+2], *idp;
135 	ufs_daddr_t	daddr, lastblock;
136 	int bb;		/* number of disk blocks in a block disk blocks */
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 	lbn = lblkno(fs, ap->a_startoffset);
145 	(void)lfs_check(vp, lbn, 0);
146 
147 #ifdef DEBUG
148 	if(!VOP_ISLOCKED(vp)) {
149 		printf("lfs_balloc: warning: ino %d not locked\n",ip->i_number);
150 	}
151 #endif
152 
153 	/*
154 	 * Three cases: it's a block beyond the end of file, it's a block in
155 	 * the file that may or may not have been assigned a disk address or
156 	 * we're writing an entire block.  Note, if the daddr is unassigned,
157 	 * the block might still have existed in the cache (if it was read
158 	 * or written earlier).	 If it did, make sure we don't count it as a
159 	 * new block or zero out its contents.	If it did not, make sure
160 	 * we allocate any necessary indirect blocks.
161 	 * If we are writing a block beyond the end of the file, we need to
162 	 * check if the old last block was a fragment.	If it was, we need
163 	 * to rewrite it.
164 	 */
165 
166 	*ap->a_bpp = NULL;
167 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
168 	if (error)
169 		return (error);
170 
171 	/* Check for block beyond end of file and fragment extension needed. */
172 	lastblock = lblkno(fs, ip->i_ffs_size);
173 	if (lastblock < NDADDR && lastblock < lbn) {
174 		osize = blksize(fs, ip, lastblock);
175 		if (osize < fs->lfs_bsize && osize > 0) {
176 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
177 						    lastblock, &bp)))
178 				return(error);
179 			ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
180 			uvm_vnp_setsize(vp, ip->i_ffs_size);
181 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
182 			VOP_BWRITE(bp);
183 		}
184 	}
185 
186 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
187 	if (daddr == UNASSIGNED) {
188 		if (num > 0 && ip->i_ffs_ib[indirs[0].in_off] == 0) {
189 			ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
190 		}
191 		/* May need to allocate indirect blocks */
192 		for (i = 1; i < num; ++i) {
193 			ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize, 0,0);
194 			if (!indirs[i].in_exists) {
195 #ifdef DIAGNOSTIC
196 				if ((ibp->b_flags & (B_DONE | B_DELWRI)))
197 					panic ("Indirect block should not exist");
198 #endif
199 
200 				if (!ISSPACE(fs, bb, curproc->p_ucred)){
201 					ibp->b_flags |= B_INVAL;
202 					brelse(ibp);
203 					/* XXX might leave some UNWRITTENs hanging, do this better */
204 					return(ENOSPC);
205 				} else {
206 					ip->i_ffs_blocks += bb;
207 					ip->i_lfs->lfs_bfree -= bb;
208 					clrbuf(ibp);
209 					((ufs_daddr_t *)ibp->b_data)[indirs[i].in_off] = UNWRITTEN;
210 					ibp->b_blkno = UNWRITTEN;
211 				}
212 			} else {
213 				/*
214 				 * This block exists, but the next one may not.
215 				 * If that is the case mark it UNWRITTEN to
216 				 * keep the accounting straight.
217 				 */
218 				if ( ((ufs_daddr_t *)ibp->b_data)[indirs[i].in_off] == 0) {
219 					((ufs_daddr_t *)ibp->b_data)[indirs[i].in_off] = UNWRITTEN;
220 				}
221 			}
222 			if ((error = VOP_BWRITE(ibp)))
223 				return(error);
224 		}
225 	}
226 	/*
227 	 * If the block we are writing is a direct block, it's the last
228 	 * block in the file, and offset + iosize is less than a full
229 	 * block, we can write one or more fragments.  There are two cases:
230 	 * the block is brand new and we should allocate it the correct
231 	 * size or it already exists and contains some fragments and
232 	 * may need to extend it.
233 	 */
234 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
235 		osize = blksize(fs, ip, lbn);
236 		nsize = fragroundup(fs, offset + iosize);
237 		frags = numfrags(fs, nsize);
238 		bb = fragstodb(fs, frags);
239 		if (lblktosize(fs, lbn) >= ip->i_ffs_size)
240 			/* Brand new block or fragment */
241 			*ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
242 		else {
243 			if (nsize <= osize) {
244 				/* No need to extend */
245 				/* XXX KS - Are we wasting space? */
246 				if ((error = bread(vp, lbn, osize, NOCRED, &bp)))
247 					return error;
248 			} else {
249 				/* Extend existing block */
250 				if ((error =
251 				     lfs_fragextend(vp, osize, nsize, lbn, &bp)))
252 					return(error);
253 			}
254 			*ap->a_bpp = bp;
255 		}
256 	} else {
257 		/*
258 		 * Get the existing block from the cache either because the
259 		 * block 1) is not a direct block or 2) is not the last
260 		 * block in the file.
261 		 */
262 		frags = dbtofrags(fs, bb);
263 		*ap->a_bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
264 	}
265 
266 	/*
267 	 * The block we are writing may be a brand new block
268 	 * in which case we need to do accounting (i.e. check
269 	 * for free space and update the inode number of blocks.
270 	 *
271 	 * We can tell a truly new block because (1) ufs_bmaparray
272 	 * will say it is UNASSIGNED.  Once we allocate it we will assign
273 	 * it the disk address UNWRITTEN.
274 	 */
275 	if (daddr == UNASSIGNED) {
276 		if (!ISSPACE(fs, bb, curproc->p_ucred)) {
277 			bp->b_flags |= B_INVAL;
278 			brelse(bp);
279 			return(ENOSPC);
280 		} else {
281 			ip->i_ffs_blocks += bb;
282 			ip->i_lfs->lfs_bfree -= bb;
283 			if (iosize != fs->lfs_bsize)
284 				clrbuf(bp);
285 
286 			/* Note the new address */
287 			bp->b_blkno = UNWRITTEN;
288 
289 			switch (num) {
290 			    case 0:
291 				ip->i_ffs_db[lbn] = UNWRITTEN;
292 				break;
293 			    case 1:
294 				ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
295 				break;
296 			    default:
297 				idp = &indirs[num - 1];
298 				if (bread(vp, idp->in_lbn, fs->lfs_bsize,
299 					  NOCRED, &ibp))
300 					panic("lfs_balloc: bread bno %d",
301 					      idp->in_lbn);
302 				((ufs_daddr_t *)ibp->b_data)[idp->in_off] =
303 					UNWRITTEN;
304 				VOP_BWRITE(ibp);
305 			}
306 		}
307 	} else if (!(bp->b_flags & (B_DONE|B_DELWRI))) {
308 		/*
309 		 * Not a brand new block, also not in the cache;
310 		 * read it in from disk.
311 		 */
312 		if (iosize == fs->lfs_bsize)
313 			/* Optimization: I/O is unnecessary. */
314 			bp->b_blkno = daddr;
315 		else {
316 			/*
317 			 * We need to read the block to preserve the
318 			 * existing bytes.
319 			 */
320 			bp->b_blkno = daddr;
321 			bp->b_flags |= B_READ;
322 			VOP_STRATEGY(bp);
323 			return(biowait(bp));
324 		}
325 	}
326 
327 	return (0);
328 }
329 
330 int
331 lfs_fragextend(vp, osize, nsize, lbn, bpp)
332 	struct vnode *vp;
333 	int osize;
334 	int nsize;
335 	ufs_daddr_t lbn;
336 	struct buf **bpp;
337 {
338 	struct inode *ip;
339 	struct lfs *fs;
340 	long bb;
341 	int error;
342 	extern long locked_queue_bytes;
343 	struct buf *ibp;
344 	SEGUSE *sup;
345 
346 	ip = VTOI(vp);
347 	fs = ip->i_lfs;
348 
349 	bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
350  top:
351 	if (!ISSPACE(fs, bb, curproc->p_ucred)) {
352 		return(ENOSPC);
353 	}
354 	if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
355 		brelse(*bpp);
356 		return(error);
357 	}
358 
359 	/*
360  	 * Fix the allocation for this fragment so that it looks like the
361          * source segment contained a block of the new size.  This overcounts;
362 	 * but the overcount only lasts until the block in question
363 	 * is written, so the on-disk live bytes count is always correct.
364 	 */
365 	if ((*bpp)->b_blkno > 0) {
366 		LFS_SEGENTRY(sup, fs, datosn(fs,(*bpp)->b_blkno), ibp);
367 		sup->su_nbytes += (nsize-osize);
368 		VOP_BWRITE(ibp);
369 	}
370 
371 #ifdef QUOTA
372 	if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
373 		brelse(*bpp);
374 		return (error);
375 	}
376 #endif
377 	/*
378 	 * XXX - KS - Don't change size while we're gathered, as we could
379 	 * then overlap another buffer in lfs_writeseg.
380 	 */
381 	if((*bpp)->b_flags & B_GATHERED) {
382 		(*bpp)->b_flags |= B_NEEDCOMMIT; /* XXX KS - what flag to use? */
383 		brelse(*bpp);
384 		tsleep(*bpp, (PRIBIO+1), "lfs_fragextend", 0);
385 		goto top;
386 	}
387 	ip->i_ffs_blocks += bb;
388 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
389 	fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
390 	if((*bpp)->b_flags & B_LOCKED)
391 		locked_queue_bytes += (nsize - osize);
392 	allocbuf(*bpp, nsize);
393 	bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
394 
395 	return(0);
396 }
397