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