xref: /dragonfly/sys/vfs/ufs/ffs_balloc.c (revision c03f08f3)
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.18 2006/08/12 00:26:21 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  * Balloc defines the structure of filesystem storage
55  * by allocating the physical blocks on a device given
56  * the inode and the logical block number in a file.
57  *
58  * ffs_balloc(struct vnode *a_vp, ufs_daddr_t a_lbn, int a_size,
59  *	      struct ucred *a_cred, int a_flags, struct buf *a_bpp)
60  */
61 int
62 ffs_balloc(struct vop_balloc_args *ap)
63 {
64 	struct inode *ip;
65 	ufs_daddr_t lbn;
66 	int size;
67 	struct ucred *cred;
68 	int flags;
69 	struct fs *fs;
70 	ufs_daddr_t nb;
71 	struct buf *bp, *nbp, *dbp;
72 	struct vnode *vp;
73 	struct indir indirs[NIADDR + 2];
74 	ufs_daddr_t newb, *bap, pref;
75 	int deallocated, osize, nsize, num, i, error;
76 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
77 	int unwindidx;
78 	int seqcount;
79 
80 	vp = ap->a_vp;
81 	ip = VTOI(vp);
82 	fs = ip->i_fs;
83 	lbn = lblkno(fs, ap->a_startoffset);
84 	size = blkoff(fs, ap->a_startoffset) + ap->a_size;
85 	if (size > fs->fs_bsize)
86 		panic("ffs_balloc: blk too big");
87 	*ap->a_bpp = NULL;
88 	if (lbn < 0)
89 		return (EFBIG);
90 	cred = ap->a_cred;
91 	flags = ap->a_flags;
92 
93 	/*
94 	 * The vnode must be locked for us to be able to safely mess
95 	 * around with the inode.
96 	 */
97 	if (vn_islocked(vp) != LK_EXCLUSIVE) {
98 		panic("ffs_balloc: vnode %p not exclusively locked!", vp);
99 	}
100 
101 	/*
102 	 * If the next write will extend the file into a new block,
103 	 * and the file is currently composed of a fragment
104 	 * this fragment has to be extended to be a full block.
105 	 */
106 	nb = lblkno(fs, ip->i_size);
107 	if (nb < NDADDR && nb < lbn) {
108 		/*
109 		 * The filesize prior to this write can fit in direct
110 		 * blocks (ex. fragmentation is possibly done)
111 		 * we are now extending the file write beyond
112 		 * the block which has end of the file prior to this write.
113 		 */
114 		osize = blksize(fs, ip, nb);
115 		/*
116 		 * osize gives disk allocated size in the last block. It is
117 		 * either in fragments or a file system block size.
118 		 */
119 		if (osize < fs->fs_bsize && osize > 0) {
120 			/* A few fragments are already allocated, since the
121 			 * current extends beyond this block allocated the
122 			 * complete block as fragments are on in last block.
123 			 */
124 			error = ffs_realloccg(ip, nb,
125 				ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
126 				osize, (int)fs->fs_bsize, cred, &bp);
127 			if (error)
128 				return (error);
129 			if (DOINGSOFTDEP(vp))
130 				softdep_setup_allocdirect(ip, nb,
131 				    dofftofsb(fs, bp->b_bio2.bio_offset),
132 				    ip->i_db[nb], fs->fs_bsize, osize, bp);
133 			/* adjust the inode size, we just grew */
134 			ip->i_size = smalllblktosize(fs, nb + 1);
135 			ip->i_db[nb] = dofftofsb(fs, bp->b_bio2.bio_offset);
136 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
137 			if (flags & B_SYNC)
138 				bwrite(bp);
139 			else
140 				bawrite(bp);
141 			/* bp is already released here */
142 		}
143 	}
144 	/*
145 	 * The first NDADDR blocks are direct blocks
146 	 */
147 	if (lbn < NDADDR) {
148 		nb = ip->i_db[lbn];
149 		if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
150 			error = bread(vp, lblktodoff(fs, lbn), fs->fs_bsize, &bp);
151 			if (error) {
152 				brelse(bp);
153 				return (error);
154 			}
155 			bp->b_bio2.bio_offset = fsbtodoff(fs, nb);
156 			*ap->a_bpp = bp;
157 			return (0);
158 		}
159 		if (nb != 0) {
160 			/*
161 			 * Consider need to reallocate a fragment.
162 			 */
163 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
164 			nsize = fragroundup(fs, size);
165 			if (nsize <= osize) {
166 				error = bread(vp, lblktodoff(fs, lbn),
167 					      osize, &bp);
168 				if (error) {
169 					brelse(bp);
170 					return (error);
171 				}
172 				bp->b_bio2.bio_offset = fsbtodoff(fs, nb);
173 			} else {
174 				error = ffs_realloccg(ip, lbn,
175 				    ffs_blkpref(ip, lbn, (int)lbn,
176 					&ip->i_db[0]), osize, nsize, cred, &bp);
177 				if (error)
178 					return (error);
179 				if (DOINGSOFTDEP(vp))
180 					softdep_setup_allocdirect(ip, lbn,
181 					    dofftofsb(fs, bp->b_bio2.bio_offset),
182 					    nb, nsize, osize, bp);
183 			}
184 		} else {
185 			if (ip->i_size < smalllblktosize(fs, lbn + 1))
186 				nsize = fragroundup(fs, size);
187 			else
188 				nsize = fs->fs_bsize;
189 			error = ffs_alloc(ip, lbn,
190 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
191 			    nsize, cred, &newb);
192 			if (error)
193 				return (error);
194 			bp = getblk(vp, lblktodoff(fs, lbn), nsize, 0, 0);
195 			bp->b_bio2.bio_offset = fsbtodoff(fs, newb);
196 			if (flags & B_CLRBUF)
197 				vfs_bio_clrbuf(bp);
198 			if (DOINGSOFTDEP(vp))
199 				softdep_setup_allocdirect(ip, lbn, newb, 0,
200 				    nsize, 0, bp);
201 		}
202 		ip->i_db[lbn] = dofftofsb(fs, bp->b_bio2.bio_offset);
203 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
204 		*ap->a_bpp = bp;
205 		return (0);
206 	}
207 	/*
208 	 * Determine the number of levels of indirection.
209 	 */
210 	pref = 0;
211 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
212 		return(error);
213 #ifdef DIAGNOSTIC
214 	if (num < 1)
215 		panic ("ffs_balloc: ufs_bmaparray returned indirect block");
216 #endif
217 	/*
218 	 * Get a handle on the data block buffer before working through
219 	 * indirect blocks to avoid a deadlock between the VM system holding
220 	 * a locked VM page and issuing a BMAP (which tries to lock the
221 	 * indirect blocks), and the filesystem holding a locked indirect
222 	 * block and then trying to read a data block (which tries to lock
223 	 * the underlying VM pages).
224 	 */
225 	dbp = getblk(vp, lblktodoff(fs, lbn), fs->fs_bsize, 0, 0);
226 
227 	/*
228 	 * Setup undo history
229 	 */
230 	allocib = NULL;
231 	allocblk = allociblk;
232 	unwindidx = -1;
233 
234 	/*
235 	 * Fetch the first indirect block directly from the inode, allocating
236 	 * one if necessary.
237 	 */
238 	--num;
239 	nb = ip->i_ib[indirs[0].in_off];
240 	if (nb == 0) {
241 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
242 		/*
243 		 * If the filesystem has run out of space we can skip the
244 		 * full fsync/undo of the main [fail] case since no undo
245 		 * history has been built yet.  Hence the goto fail2.
246 		 */
247 	        if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
248 		    cred, &newb)) != 0)
249 			goto fail2;
250 		nb = newb;
251 		*allocblk++ = nb;
252 		bp = getblk(vp, lblktodoff(fs, indirs[1].in_lbn),
253 			    fs->fs_bsize, 0, 0);
254 		bp->b_bio2.bio_offset = fsbtodoff(fs, nb);
255 		vfs_bio_clrbuf(bp);
256 		if (DOINGSOFTDEP(vp)) {
257 			softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
258 			    newb, 0, fs->fs_bsize, 0, bp);
259 			bdwrite(bp);
260 		} else {
261 			/*
262 			 * Write synchronously so that indirect blocks
263 			 * never point at garbage.
264 			 */
265 			if (DOINGASYNC(vp))
266 				bdwrite(bp);
267 			else if ((error = bwrite(bp)) != 0)
268 				goto fail;
269 		}
270 		allocib = &ip->i_ib[indirs[0].in_off];
271 		*allocib = nb;
272 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
273 	}
274 
275 	/*
276 	 * Fetch through the indirect blocks, allocating as necessary.
277 	 */
278 	for (i = 1;;) {
279 		error = bread(vp, lblktodoff(fs, indirs[i].in_lbn), (int)fs->fs_bsize, &bp);
280 		if (error) {
281 			brelse(bp);
282 			goto fail;
283 		}
284 		bap = (ufs_daddr_t *)bp->b_data;
285 		nb = bap[indirs[i].in_off];
286 		if (i == num)
287 			break;
288 		i += 1;
289 		if (nb != 0) {
290 			bqrelse(bp);
291 			continue;
292 		}
293 		if (pref == 0)
294 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
295 		if ((error =
296 		    ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) != 0) {
297 			brelse(bp);
298 			goto fail;
299 		}
300 		nb = newb;
301 		*allocblk++ = nb;
302 		nbp = getblk(vp, lblktodoff(fs, indirs[i].in_lbn),
303 			     fs->fs_bsize, 0, 0);
304 		nbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
305 		vfs_bio_clrbuf(nbp);
306 		if (DOINGSOFTDEP(vp)) {
307 			softdep_setup_allocindir_meta(nbp, ip, bp,
308 			    indirs[i - 1].in_off, nb);
309 			bdwrite(nbp);
310 		} else {
311 			/*
312 			 * Write synchronously so that indirect blocks
313 			 * never point at garbage.
314 			 */
315 			if ((error = bwrite(nbp)) != 0) {
316 				brelse(bp);
317 				goto fail;
318 			}
319 		}
320 		bap[indirs[i - 1].in_off] = nb;
321 		if (allocib == NULL && unwindidx < 0)
322 			unwindidx = i - 1;
323 		/*
324 		 * If required, write synchronously, otherwise use
325 		 * delayed write.
326 		 */
327 		if (flags & B_SYNC) {
328 			bwrite(bp);
329 		} else {
330 			if (bp->b_bufsize == fs->fs_bsize)
331 				bp->b_flags |= B_CLUSTEROK;
332 			bdwrite(bp);
333 		}
334 	}
335 
336 	/*
337 	 * Get the data block, allocating if necessary.  We have already
338 	 * called getblk() on the data block buffer, dbp.  If we have to
339 	 * allocate it and B_CLRBUF has been set the inference is an intention
340 	 * to zero out the related disk blocks, so we do not have to issue
341 	 * a read.  Instead we simply call vfs_bio_clrbuf().  If B_CLRBUF is
342 	 * not set the caller intends to overwrite the entire contents of the
343 	 * buffer and we don't waste time trying to clean up the contents.
344 	 *
345 	 * bp references the current indirect block.  When allocating,
346 	 * the block must be updated.
347 	 */
348 	if (nb == 0) {
349 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
350 		error = ffs_alloc(ip,
351 		    lbn, pref, (int)fs->fs_bsize, cred, &newb);
352 		if (error) {
353 			brelse(bp);
354 			goto fail;
355 		}
356 		nb = newb;
357 		*allocblk++ = nb;
358 		dbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
359 		if (flags & B_CLRBUF)
360 			vfs_bio_clrbuf(dbp);
361 		if (DOINGSOFTDEP(vp))
362 			softdep_setup_allocindir_page(ip, lbn, bp,
363 			    indirs[i].in_off, nb, 0, dbp);
364 		bap[indirs[i].in_off] = nb;
365 		/*
366 		 * If required, write synchronously, otherwise use
367 		 * delayed write.
368 		 */
369 		if (flags & B_SYNC) {
370 			bwrite(bp);
371 		} else {
372 			if (bp->b_bufsize == fs->fs_bsize)
373 				bp->b_flags |= B_CLUSTEROK;
374 			bdwrite(bp);
375 		}
376 		*ap->a_bpp = dbp;
377 		return (0);
378 	}
379 	brelse(bp);
380 
381 	/*
382 	 * At this point all related indirect blocks have been allocated
383 	 * if necessary and released.  bp is no longer valid.  dbp holds
384 	 * our getblk()'d data block.
385 	 *
386 	 * XXX we previously performed a cluster_read operation here.
387 	 */
388 	if (flags & B_CLRBUF) {
389 		/*
390 		 * If B_CLRBUF is set we must validate the invalid portions
391 		 * of the buffer.  This typically requires a read-before-
392 		 * write.  The strategy call will fill in bio_offset in that
393 		 * case.
394 		 *
395 		 * If we hit this case we do a cluster read if possible
396 		 * since nearby data blocks are likely to be accessed soon
397 		 * too.
398 		 */
399 		if ((dbp->b_flags & B_CACHE) == 0) {
400 			bqrelse(dbp);
401 			seqcount = (flags & B_SEQMASK) >> B_SEQSHIFT;
402 			if (seqcount &&
403 			    (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
404 				error = cluster_read(vp, (off_t)ip->i_size,
405 					    lblktodoff(fs, lbn),
406 					    (int)fs->fs_bsize,
407 					    MAXBSIZE, seqcount, &dbp);
408 			} else {
409 				error = bread(vp, lblktodoff(fs, lbn), (int)fs->fs_bsize, &dbp);
410 			}
411 			if (error)
412 				goto fail;
413 		} else {
414 			dbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
415 		}
416 	} else {
417 		/*
418 		 * If B_CLRBUF is not set the caller intends to overwrite
419 		 * the entire contents of the buffer.  We can simply set
420 		 * bio_offset and we are done.
421 		 */
422 		dbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
423 	}
424 	*ap->a_bpp = dbp;
425 	return (0);
426 fail:
427 	/*
428 	 * If we have failed part way through block allocation, we
429 	 * have to deallocate any indirect blocks that we have allocated.
430 	 * We have to fsync the file before we start to get rid of all
431 	 * of its dependencies so that we do not leave them dangling.
432 	 * We have to sync it at the end so that the soft updates code
433 	 * does not find any untracked changes. Although this is really
434 	 * slow, running out of disk space is not expected to be a common
435 	 * occurence. The error return from fsync is ignored as we already
436 	 * have an error to return to the user.
437 	 */
438 	(void) VOP_FSYNC(vp, MNT_WAIT);
439 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
440 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
441 		deallocated += fs->fs_bsize;
442 	}
443 	if (allocib != NULL) {
444 		*allocib = 0;
445 	} else if (unwindidx >= 0) {
446 		int r;
447 
448 		r = bread(vp, lblktodoff(fs, indirs[unwindidx].in_lbn), (int)fs->fs_bsize, &bp);
449 		if (r) {
450 			panic("Could not unwind indirect block, error %d", r);
451 			brelse(bp);
452 		} else {
453 			bap = (ufs_daddr_t *)bp->b_data;
454 			bap[indirs[unwindidx].in_off] = 0;
455 			if (flags & B_SYNC) {
456 				bwrite(bp);
457 			} else {
458 				if (bp->b_bufsize == fs->fs_bsize)
459 					bp->b_flags |= B_CLUSTEROK;
460 				bdwrite(bp);
461 			}
462 		}
463 	}
464 	if (deallocated) {
465 #ifdef QUOTA
466 		/*
467 		 * Restore user's disk quota because allocation failed.
468 		 */
469 		(void) ufs_chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
470 #endif
471 		ip->i_blocks -= btodb(deallocated);
472 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
473 	}
474 	(void) VOP_FSYNC(vp, MNT_WAIT);
475 
476 	/*
477 	 * Cleanup the data block we getblk()'d before returning.
478 	 */
479 fail2:
480 	brelse(dbp);
481 	return (error);
482 }
483 
484