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