xref: /dragonfly/sys/vfs/ufs/ffs_balloc.c (revision 8a0bcd56)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)ffs_balloc.c	8.8 (Berkeley) 6/16/95
34  * $FreeBSD: src/sys/ufs/ffs/ffs_balloc.c,v 1.26.2.1 2002/10/10 19:48:20 dillon Exp $
35  * $DragonFly: src/sys/vfs/ufs/ffs_balloc.c,v 1.19 2008/05/21 18:49:49 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/buf.h>
42 #include <sys/lock.h>
43 #include <sys/mount.h>
44 #include <sys/vnode.h>
45 
46 #include "quota.h"
47 #include "inode.h"
48 #include "ufs_extern.h"
49 
50 #include "fs.h"
51 #include "ffs_extern.h"
52 
53 /*
54  * ffs_balloc(struct vnode *a_vp, ufs_daddr_t a_lbn, int a_size,
55  *	      struct ucred *a_cred, int a_flags, struct buf *a_bpp)
56  *
57  * Balloc defines the structure of filesystem storage by allocating
58  * the physical blocks on a device given the inode and the logical
59  * block number in a file.
60  *
61  * NOTE: B_CLRBUF - this flag tells balloc to clear invalid portions
62  *	 of the buffer.  However, any dirty bits will override missing
63  *	 valid bits.  This case occurs when writable mmaps are truncated
64  *	 and then extended.
65  */
66 int
67 ffs_balloc(struct vop_balloc_args *ap)
68 {
69 	struct inode *ip;
70 	ufs_daddr_t lbn;
71 	int size;
72 	struct ucred *cred;
73 	int flags;
74 	struct fs *fs;
75 	ufs_daddr_t nb;
76 	struct buf *bp, *nbp, *dbp;
77 	struct vnode *vp;
78 	struct indir indirs[NIADDR + 2];
79 	ufs_daddr_t newb, *bap, pref;
80 	int deallocated, osize, nsize, num, i, error;
81 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
82 	ufs_daddr_t *lbns_remfree, lbns[NIADDR + 1];
83 	int unwindidx;
84 	int seqcount;
85 
86 	vp = ap->a_vp;
87 	ip = VTOI(vp);
88 	fs = ip->i_fs;
89 	lbn = lblkno(fs, ap->a_startoffset);
90 	size = blkoff(fs, ap->a_startoffset) + ap->a_size;
91 	if (size > fs->fs_bsize)
92 		panic("ffs_balloc: blk too big");
93 	*ap->a_bpp = NULL;
94 	if (lbn < 0)
95 		return (EFBIG);
96 	cred = ap->a_cred;
97 	flags = ap->a_flags;
98 
99 	/*
100 	 * The vnode must be locked for us to be able to safely mess
101 	 * around with the inode.
102 	 */
103 	if (vn_islocked(vp) != LK_EXCLUSIVE) {
104 		panic("ffs_balloc: vnode %p not exclusively locked!", vp);
105 	}
106 
107 	/*
108 	 * If the next write will extend the file into a new block,
109 	 * and the file is currently composed of a fragment
110 	 * this fragment has to be extended to be a full block.
111 	 */
112 	nb = lblkno(fs, ip->i_size);
113 	if (nb < NDADDR && nb < lbn) {
114 		/*
115 		 * The filesize prior to this write can fit in direct
116 		 * blocks (ex. fragmentation is possibly done)
117 		 * we are now extending the file write beyond
118 		 * the block which has end of the file prior to this write.
119 		 */
120 		osize = blksize(fs, ip, nb);
121 		/*
122 		 * osize gives disk allocated size in the last block. It is
123 		 * either in fragments or a file system block size.
124 		 */
125 		if (osize < fs->fs_bsize && osize > 0) {
126 			/* A few fragments are already allocated, since the
127 			 * current extends beyond this block allocated the
128 			 * complete block as fragments are on in last block.
129 			 */
130 			error = ffs_realloccg(ip, nb,
131 				ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
132 				osize, (int)fs->fs_bsize, cred, &bp);
133 			if (error)
134 				return (error);
135 			if (DOINGSOFTDEP(vp))
136 				softdep_setup_allocdirect(ip, nb,
137 				    dofftofsb(fs, bp->b_bio2.bio_offset),
138 				    ip->i_db[nb], fs->fs_bsize, osize, bp);
139 			/* adjust the inode size, we just grew */
140 			ip->i_size = smalllblktosize(fs, nb + 1);
141 			ip->i_db[nb] = dofftofsb(fs, bp->b_bio2.bio_offset);
142 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
143 			if (flags & B_SYNC)
144 				bwrite(bp);
145 			else
146 				bawrite(bp);
147 			/* bp is already released here */
148 		}
149 	}
150 	/*
151 	 * The first NDADDR blocks are direct blocks
152 	 */
153 	if (lbn < NDADDR) {
154 		nb = ip->i_db[lbn];
155 		if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
156 			error = bread(vp, lblktodoff(fs, lbn), fs->fs_bsize, &bp);
157 			if (error) {
158 				brelse(bp);
159 				return (error);
160 			}
161 			bp->b_bio2.bio_offset = fsbtodoff(fs, nb);
162 			*ap->a_bpp = bp;
163 			return (0);
164 		}
165 		if (nb != 0) {
166 			/*
167 			 * Consider need to reallocate a fragment.
168 			 */
169 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
170 			nsize = fragroundup(fs, size);
171 			if (nsize <= osize) {
172 				error = bread(vp, lblktodoff(fs, lbn),
173 					      osize, &bp);
174 				if (error) {
175 					brelse(bp);
176 					return (error);
177 				}
178 				bp->b_bio2.bio_offset = fsbtodoff(fs, nb);
179 			} else {
180 				/*
181 				 * NOTE: ffs_realloccg() issues a bread().
182 				 */
183 				error = ffs_realloccg(ip, lbn,
184 				    ffs_blkpref(ip, lbn, (int)lbn,
185 					&ip->i_db[0]), osize, nsize, cred, &bp);
186 				if (error)
187 					return (error);
188 				if (DOINGSOFTDEP(vp))
189 					softdep_setup_allocdirect(ip, lbn,
190 					    dofftofsb(fs, bp->b_bio2.bio_offset),
191 					    nb, nsize, osize, bp);
192 			}
193 		} else {
194 			if (ip->i_size < smalllblktosize(fs, lbn + 1))
195 				nsize = fragroundup(fs, size);
196 			else
197 				nsize = fs->fs_bsize;
198 			error = ffs_alloc(ip, lbn,
199 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
200 			    nsize, cred, &newb);
201 			if (error)
202 				return (error);
203 			bp = getblk(vp, lblktodoff(fs, lbn), nsize, 0, 0);
204 			bp->b_bio2.bio_offset = fsbtodoff(fs, newb);
205 			if (flags & B_CLRBUF)
206 				vfs_bio_clrbuf(bp);
207 			if (DOINGSOFTDEP(vp))
208 				softdep_setup_allocdirect(ip, lbn, newb, 0,
209 				    nsize, 0, bp);
210 		}
211 		ip->i_db[lbn] = dofftofsb(fs, bp->b_bio2.bio_offset);
212 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
213 		*ap->a_bpp = bp;
214 		return (0);
215 	}
216 	/*
217 	 * Determine the number of levels of indirection.
218 	 */
219 	pref = 0;
220 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
221 		return(error);
222 #ifdef DIAGNOSTIC
223 	if (num < 1)
224 		panic ("ffs_balloc: ufs_bmaparray returned indirect block");
225 #endif
226 	/*
227 	 * Get a handle on the data block buffer before working through
228 	 * indirect blocks to avoid a deadlock between the VM system holding
229 	 * a locked VM page and issuing a BMAP (which tries to lock the
230 	 * indirect blocks), and the filesystem holding a locked indirect
231 	 * block and then trying to read a data block (which tries to lock
232 	 * the underlying VM pages).
233 	 */
234 	dbp = getblk(vp, lblktodoff(fs, lbn), fs->fs_bsize, 0, 0);
235 
236 	/*
237 	 * Setup undo history
238 	 */
239 	allocib = NULL;
240 	allocblk = allociblk;
241 	lbns_remfree = lbns;
242 
243 	unwindidx = -1;
244 
245 	/*
246 	 * Fetch the first indirect block directly from the inode, allocating
247 	 * one if necessary.
248 	 */
249 	--num;
250 	nb = ip->i_ib[indirs[0].in_off];
251 	if (nb == 0) {
252 		pref = ffs_blkpref(ip, lbn, 0, NULL);
253 		/*
254 		 * If the filesystem has run out of space we can skip the
255 		 * full fsync/undo of the main [fail] case since no undo
256 		 * history has been built yet.  Hence the goto fail2.
257 		 */
258 	        if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
259 		    cred, &newb)) != 0)
260 			goto fail2;
261 		nb = newb;
262 		*allocblk++ = nb;
263 		*lbns_remfree++ = indirs[1].in_lbn;
264 		bp = getblk(vp, lblktodoff(fs, indirs[1].in_lbn),
265 			    fs->fs_bsize, 0, 0);
266 		bp->b_bio2.bio_offset = fsbtodoff(fs, nb);
267 		vfs_bio_clrbuf(bp);
268 		if (DOINGSOFTDEP(vp)) {
269 			softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
270 			    newb, 0, fs->fs_bsize, 0, bp);
271 			bdwrite(bp);
272 		} else {
273 			/*
274 			 * Write synchronously so that indirect blocks
275 			 * never point at garbage.
276 			 */
277 			if (DOINGASYNC(vp))
278 				bdwrite(bp);
279 			else if ((error = bwrite(bp)) != 0)
280 				goto fail;
281 		}
282 		allocib = &ip->i_ib[indirs[0].in_off];
283 		*allocib = nb;
284 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
285 	}
286 
287 	/*
288 	 * Fetch through the indirect blocks, allocating as necessary.
289 	 */
290 	for (i = 1;;) {
291 		error = bread(vp, lblktodoff(fs, indirs[i].in_lbn), (int)fs->fs_bsize, &bp);
292 		if (error) {
293 			brelse(bp);
294 			goto fail;
295 		}
296 		bap = (ufs_daddr_t *)bp->b_data;
297 		nb = bap[indirs[i].in_off];
298 		if (i == num)
299 			break;
300 		i += 1;
301 		if (nb != 0) {
302 			bqrelse(bp);
303 			continue;
304 		}
305 		if (pref == 0)
306 			pref = ffs_blkpref(ip, lbn, 0, NULL);
307 		if ((error =
308 		    ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) != 0) {
309 			brelse(bp);
310 			goto fail;
311 		}
312 		nb = newb;
313 		*allocblk++ = nb;
314 		*lbns_remfree++ = indirs[i].in_lbn;
315 		nbp = getblk(vp, lblktodoff(fs, indirs[i].in_lbn),
316 			     fs->fs_bsize, 0, 0);
317 		nbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
318 		vfs_bio_clrbuf(nbp);
319 		if (DOINGSOFTDEP(vp)) {
320 			softdep_setup_allocindir_meta(nbp, ip, bp,
321 			    indirs[i - 1].in_off, nb);
322 			bdwrite(nbp);
323 		} else {
324 			/*
325 			 * Write synchronously so that indirect blocks
326 			 * never point at garbage.
327 			 */
328 			if ((error = bwrite(nbp)) != 0) {
329 				brelse(bp);
330 				goto fail;
331 			}
332 		}
333 		bap[indirs[i - 1].in_off] = nb;
334 		if (allocib == NULL && unwindidx < 0)
335 			unwindidx = i - 1;
336 		/*
337 		 * If required, write synchronously, otherwise use
338 		 * delayed write.
339 		 */
340 		if (flags & B_SYNC) {
341 			bwrite(bp);
342 		} else {
343 			if (bp->b_bufsize == fs->fs_bsize)
344 				bp->b_flags |= B_CLUSTEROK;
345 			bdwrite(bp);
346 		}
347 	}
348 
349 	/*
350 	 * Get the data block, allocating if necessary.  We have already
351 	 * called getblk() on the data block buffer, dbp.  If we have to
352 	 * allocate it and B_CLRBUF has been set the inference is an intention
353 	 * to zero out the related disk blocks, so we do not have to issue
354 	 * a read.  Instead we simply call vfs_bio_clrbuf().  If B_CLRBUF is
355 	 * not set the caller intends to overwrite the entire contents of the
356 	 * buffer and we don't waste time trying to clean up the contents.
357 	 *
358 	 * bp references the current indirect block.  When allocating,
359 	 * the block must be updated.
360 	 */
361 	if (nb == 0) {
362 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
363 		error = ffs_alloc(ip,
364 		    lbn, pref, (int)fs->fs_bsize, cred, &newb);
365 		if (error) {
366 			brelse(bp);
367 			goto fail;
368 		}
369 		nb = newb;
370 		*allocblk++ = nb;
371 		*lbns_remfree++ = lbn;
372 		dbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
373 		if (flags & B_CLRBUF)
374 			vfs_bio_clrbuf(dbp);
375 		if (DOINGSOFTDEP(vp))
376 			softdep_setup_allocindir_page(ip, lbn, bp,
377 			    indirs[i].in_off, nb, 0, dbp);
378 		bap[indirs[i].in_off] = nb;
379 		/*
380 		 * If required, write synchronously, otherwise use
381 		 * delayed write.
382 		 */
383 		if (flags & B_SYNC) {
384 			bwrite(bp);
385 		} else {
386 			if (bp->b_bufsize == fs->fs_bsize)
387 				bp->b_flags |= B_CLUSTEROK;
388 			bdwrite(bp);
389 		}
390 		*ap->a_bpp = dbp;
391 		return (0);
392 	}
393 	brelse(bp);
394 
395 	/*
396 	 * At this point all related indirect blocks have been allocated
397 	 * if necessary and released.  bp is no longer valid.  dbp holds
398 	 * our getblk()'d data block.
399 	 *
400 	 * XXX we previously performed a cluster_read operation here.
401 	 */
402 	if (flags & B_CLRBUF) {
403 		/*
404 		 * If B_CLRBUF is set we must validate the invalid portions
405 		 * of the buffer.  This typically requires a read-before-
406 		 * write.  The strategy call will fill in bio_offset in that
407 		 * case.
408 		 *
409 		 * If we hit this case we do a cluster read if possible
410 		 * since nearby data blocks are likely to be accessed soon
411 		 * too.
412 		 */
413 		if ((dbp->b_flags & B_CACHE) == 0) {
414 			bqrelse(dbp);
415 			seqcount = (flags & B_SEQMASK) >> B_SEQSHIFT;
416 			if (seqcount &&
417 			    (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
418 				error = cluster_read(vp, (off_t)ip->i_size,
419 					    lblktodoff(fs, lbn),
420 					    (int)fs->fs_bsize,
421 					    fs->fs_bsize,
422 					    seqcount * BKVASIZE,
423 					    &dbp);
424 			} else {
425 				error = bread(vp, lblktodoff(fs, lbn),
426 					      (int)fs->fs_bsize, &dbp);
427 			}
428 			if (error)
429 				goto fail;
430 		} else {
431 			dbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
432 		}
433 	} else {
434 		/*
435 		 * If B_CLRBUF is not set the caller intends to overwrite
436 		 * the entire contents of the buffer.  We can simply set
437 		 * bio_offset and we are done.
438 		 */
439 		dbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
440 	}
441 	*ap->a_bpp = dbp;
442 	return (0);
443 fail:
444 	/*
445 	 * If we have failed part way through block allocation, we
446 	 * have to deallocate any indirect blocks that we have allocated.
447 	 * We have to fsync the file before we start to get rid of all
448 	 * of its dependencies so that we do not leave them dangling.
449 	 * We have to sync it at the end so that the soft updates code
450 	 * does not find any untracked changes. Although this is really
451 	 * slow, running out of disk space is not expected to be a common
452 	 * occurence. The error return from fsync is ignored as we already
453 	 * have an error to return to the user.
454 	 */
455 	VOP_FSYNC(vp, MNT_WAIT, 0);
456 	for (deallocated = 0, blkp = allociblk, lbns_remfree = lbns;
457 	     blkp < allocblk; blkp++, lbns_remfree++) {
458 		/*
459 		 * We shall not leave the freed blocks on the vnode
460 		 * buffer object lists.
461 		 */
462 		bp = getblk(vp, *lbns_remfree, fs->fs_bsize, 0, 0);
463 		bp->b_flags |= (B_INVAL | B_RELBUF);
464 		brelse(bp);
465 		deallocated += fs->fs_bsize;
466 	}
467 
468 	if (allocib != NULL) {
469 		*allocib = 0;
470 	} else if (unwindidx >= 0) {
471 		int r;
472 
473 		r = bread(vp, lblktodoff(fs, indirs[unwindidx].in_lbn), (int)fs->fs_bsize, &bp);
474 		if (r) {
475 			panic("Could not unwind indirect block, error %d", r);
476 			brelse(bp);
477 		} else {
478 			bap = (ufs_daddr_t *)bp->b_data;
479 			bap[indirs[unwindidx].in_off] = 0;
480 			if (flags & B_SYNC) {
481 				bwrite(bp);
482 			} else {
483 				if (bp->b_bufsize == fs->fs_bsize)
484 					bp->b_flags |= B_CLUSTEROK;
485 				bdwrite(bp);
486 			}
487 		}
488 	}
489 	if (deallocated) {
490 #ifdef QUOTA
491 		/*
492 		 * Restore user's disk quota because allocation failed.
493 		 */
494 		(void) ufs_chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
495 #endif
496 		ip->i_blocks -= btodb(deallocated);
497 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
498 	}
499 	VOP_FSYNC(vp, MNT_WAIT, 0);
500 
501 	/*
502 	 * After the buffers are invalidated and on-disk pointers are
503 	 * cleared, free the blocks.
504 	 */
505 	for (blkp = allociblk; blkp < allocblk; blkp++) {
506 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
507 	}
508 
509 	/*
510 	 * Cleanup the data block we getblk()'d before returning.
511 	 */
512 fail2:
513 	brelse(dbp);
514 	return (error);
515 }
516 
517