xref: /freebsd/sys/ufs/ffs/ffs_balloc.c (revision cb166ce422ac2bc81f42c2a2e2cd68625c11478d)
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$
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/buf.h>
40 #include <sys/lock.h>
41 #include <sys/mount.h>
42 #include <sys/vnode.h>
43 
44 #include <ufs/ufs/quota.h>
45 #include <ufs/ufs/inode.h>
46 #include <ufs/ufs/ufs_extern.h>
47 
48 #include <ufs/ffs/fs.h>
49 #include <ufs/ffs/ffs_extern.h>
50 
51 /*
52  * Balloc defines the structure of file system storage
53  * by allocating the physical blocks on a device given
54  * the inode and the logical block number in a file.
55  */
56 int
57 ffs_balloc(ap)
58 	struct vop_balloc_args /* {
59 		struct vnode *a_vp;
60 		ufs_daddr_t a_lbn;
61 		int a_size;
62 		struct ucred *a_cred;
63 		int a_flags;
64 		struct buf *a_bpp;
65 	} */ *ap;
66 {
67 	struct inode *ip;
68 	ufs_daddr_t lbn;
69 	int size;
70 	struct ucred *cred;
71 	int flags;
72 	struct fs *fs;
73 	ufs_daddr_t nb;
74 	struct buf *bp, *nbp;
75 	struct vnode *vp;
76 	struct indir indirs[NIADDR + 2];
77 	ufs_daddr_t newb, *bap, pref;
78 	int deallocated, osize, nsize, num, i, error;
79 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
80 	int unwindidx = -1;
81 	struct proc *p = curproc;	/* XXX */
82 
83 	vp = ap->a_vp;
84 	ip = VTOI(vp);
85 	fs = ip->i_fs;
86 	lbn = lblkno(fs, ap->a_startoffset);
87 	size = blkoff(fs, ap->a_startoffset) + ap->a_size;
88 	if (size > fs->fs_bsize)
89 		panic("ffs_balloc: blk too big");
90 	*ap->a_bpp = NULL;
91 	if (lbn < 0)
92 		return (EFBIG);
93 	cred = ap->a_cred;
94 	flags = ap->a_flags;
95 
96 	/*
97 	 * If the next write will extend the file into a new block,
98 	 * and the file is currently composed of a fragment
99 	 * this fragment has to be extended to be a full block.
100 	 */
101 	nb = lblkno(fs, ip->i_size);
102 	if (nb < NDADDR && nb < lbn) {
103 		osize = blksize(fs, ip, nb);
104 		if (osize < fs->fs_bsize && osize > 0) {
105 			error = ffs_realloccg(ip, nb,
106 				ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
107 				osize, (int)fs->fs_bsize, cred, &bp);
108 			if (error)
109 				return (error);
110 			if (DOINGSOFTDEP(vp))
111 				softdep_setup_allocdirect(ip, nb,
112 				    dbtofsb(fs, bp->b_blkno), ip->i_db[nb],
113 				    fs->fs_bsize, osize, bp);
114 			ip->i_size = smalllblktosize(fs, nb + 1);
115 			ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
116 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
117 			if (flags & B_SYNC)
118 				bwrite(bp);
119 			else
120 				bawrite(bp);
121 		}
122 	}
123 	/*
124 	 * The first NDADDR blocks are direct blocks
125 	 */
126 	if (lbn < NDADDR) {
127 		nb = ip->i_db[lbn];
128 		if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
129 			error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
130 			if (error) {
131 				brelse(bp);
132 				return (error);
133 			}
134 			bp->b_blkno = fsbtodb(fs, nb);
135 			*ap->a_bpp = bp;
136 			return (0);
137 		}
138 		if (nb != 0) {
139 			/*
140 			 * Consider need to reallocate a fragment.
141 			 */
142 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
143 			nsize = fragroundup(fs, size);
144 			if (nsize <= osize) {
145 				error = bread(vp, lbn, osize, NOCRED, &bp);
146 				if (error) {
147 					brelse(bp);
148 					return (error);
149 				}
150 				bp->b_blkno = fsbtodb(fs, nb);
151 			} else {
152 				error = ffs_realloccg(ip, lbn,
153 				    ffs_blkpref(ip, lbn, (int)lbn,
154 					&ip->i_db[0]), osize, nsize, cred, &bp);
155 				if (error)
156 					return (error);
157 				if (DOINGSOFTDEP(vp))
158 					softdep_setup_allocdirect(ip, lbn,
159 					    dbtofsb(fs, bp->b_blkno), nb,
160 					    nsize, osize, bp);
161 			}
162 		} else {
163 			if (ip->i_size < smalllblktosize(fs, lbn + 1))
164 				nsize = fragroundup(fs, size);
165 			else
166 				nsize = fs->fs_bsize;
167 			error = ffs_alloc(ip, lbn,
168 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
169 			    nsize, cred, &newb);
170 			if (error)
171 				return (error);
172 			bp = getblk(vp, lbn, nsize, 0, 0);
173 			bp->b_blkno = fsbtodb(fs, newb);
174 			if (flags & B_CLRBUF)
175 				vfs_bio_clrbuf(bp);
176 			if (DOINGSOFTDEP(vp))
177 				softdep_setup_allocdirect(ip, lbn, newb, 0,
178 				    nsize, 0, bp);
179 		}
180 		ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
181 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
182 		*ap->a_bpp = bp;
183 		return (0);
184 	}
185 	/*
186 	 * Determine the number of levels of indirection.
187 	 */
188 	pref = 0;
189 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
190 		return(error);
191 #ifdef DIAGNOSTIC
192 	if (num < 1)
193 		panic ("ffs_balloc: ufs_bmaparray returned indirect block");
194 #endif
195 	/*
196 	 * Fetch the first indirect block allocating if necessary.
197 	 */
198 	--num;
199 	nb = ip->i_ib[indirs[0].in_off];
200 	allocib = NULL;
201 	allocblk = allociblk;
202 	if (nb == 0) {
203 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
204 	        if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
205 		    cred, &newb)) != 0)
206 			return (error);
207 		nb = newb;
208 		*allocblk++ = nb;
209 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
210 		bp->b_blkno = fsbtodb(fs, nb);
211 		vfs_bio_clrbuf(bp);
212 		if (DOINGSOFTDEP(vp)) {
213 			softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
214 			    newb, 0, fs->fs_bsize, 0, bp);
215 			bdwrite(bp);
216 		} else {
217 			/*
218 			 * Write synchronously so that indirect blocks
219 			 * never point at garbage.
220 			 */
221 			if (DOINGASYNC(vp))
222 				bdwrite(bp);
223 			else if ((error = bwrite(bp)) != 0)
224 				goto fail;
225 		}
226 		allocib = &ip->i_ib[indirs[0].in_off];
227 		*allocib = nb;
228 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
229 	}
230 	/*
231 	 * Fetch through the indirect blocks, allocating as necessary.
232 	 */
233 	for (i = 1;;) {
234 		error = bread(vp,
235 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
236 		if (error) {
237 			brelse(bp);
238 			goto fail;
239 		}
240 		bap = (ufs_daddr_t *)bp->b_data;
241 		nb = bap[indirs[i].in_off];
242 		if (i == num)
243 			break;
244 		i += 1;
245 		if (nb != 0) {
246 			bqrelse(bp);
247 			continue;
248 		}
249 		if (pref == 0)
250 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
251 		if ((error =
252 		    ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) != 0) {
253 			brelse(bp);
254 			goto fail;
255 		}
256 		nb = newb;
257 		*allocblk++ = nb;
258 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
259 		nbp->b_blkno = fsbtodb(fs, nb);
260 		vfs_bio_clrbuf(nbp);
261 		if (DOINGSOFTDEP(vp)) {
262 			softdep_setup_allocindir_meta(nbp, ip, bp,
263 			    indirs[i - 1].in_off, nb);
264 			bdwrite(nbp);
265 		} else {
266 			/*
267 			 * Write synchronously so that indirect blocks
268 			 * never point at garbage.
269 			 */
270 			if ((error = bwrite(nbp)) != 0) {
271 				brelse(bp);
272 				goto fail;
273 			}
274 		}
275 		bap[indirs[i - 1].in_off] = nb;
276 		if (allocib == NULL && unwindidx < 0)
277 			unwindidx = i - 1;
278 		/*
279 		 * If required, write synchronously, otherwise use
280 		 * delayed write.
281 		 */
282 		if (flags & B_SYNC) {
283 			bwrite(bp);
284 		} else {
285 			if (bp->b_bufsize == fs->fs_bsize)
286 				bp->b_flags |= B_CLUSTEROK;
287 			bdwrite(bp);
288 		}
289 	}
290 	/*
291 	 * Get the data block, allocating if necessary.
292 	 */
293 	if (nb == 0) {
294 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
295 		error = ffs_alloc(ip,
296 		    lbn, pref, (int)fs->fs_bsize, cred, &newb);
297 		if (error) {
298 			brelse(bp);
299 			goto fail;
300 		}
301 		nb = newb;
302 		*allocblk++ = nb;
303 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
304 		nbp->b_blkno = fsbtodb(fs, nb);
305 		if (flags & B_CLRBUF)
306 			vfs_bio_clrbuf(nbp);
307 		if (DOINGSOFTDEP(vp))
308 			softdep_setup_allocindir_page(ip, lbn, bp,
309 			    indirs[i].in_off, nb, 0, nbp);
310 		bap[indirs[i].in_off] = nb;
311 		/*
312 		 * If required, write synchronously, otherwise use
313 		 * delayed write.
314 		 */
315 		if (flags & B_SYNC) {
316 			bwrite(bp);
317 		} else {
318 			if (bp->b_bufsize == fs->fs_bsize)
319 				bp->b_flags |= B_CLUSTEROK;
320 			bdwrite(bp);
321 		}
322 		*ap->a_bpp = nbp;
323 		return (0);
324 	}
325 	brelse(bp);
326 	if (flags & B_CLRBUF) {
327 		error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
328 		if (error) {
329 			brelse(nbp);
330 			goto fail;
331 		}
332 	} else {
333 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
334 		nbp->b_blkno = fsbtodb(fs, nb);
335 	}
336 	*ap->a_bpp = nbp;
337 	return (0);
338 fail:
339 	/*
340 	 * If we have failed part way through block allocation, we
341 	 * have to deallocate any indirect blocks that we have allocated.
342 	 * We have to fsync the file before we start to get rid of all
343 	 * of its dependencies so that we do not leave them dangling.
344 	 * We have to sync it at the end so that the soft updates code
345 	 * does not find any untracked changes. Although this is really
346 	 * slow, running out of disk space is not expected to be a common
347 	 * occurence. The error return from fsync is ignored as we already
348 	 * have an error to return to the user.
349 	 */
350 	(void) VOP_FSYNC(vp, cred, MNT_WAIT, p);
351 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
352 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
353 		deallocated += fs->fs_bsize;
354 	}
355 	if (allocib != NULL) {
356 		*allocib = 0;
357 	} else if (unwindidx >= 0) {
358 		int r;
359 
360 		r = bread(vp, indirs[unwindidx].in_lbn,
361 		    (int)fs->fs_bsize, NOCRED, &bp);
362 		if (r) {
363 			panic("Could not unwind indirect block, error %d", r);
364 			brelse(bp);
365 		} else {
366 			bap = (ufs_daddr_t *)bp->b_data;
367 			bap[indirs[unwindidx].in_off] = 0;
368 			if (flags & B_SYNC) {
369 				bwrite(bp);
370 			} else {
371 				if (bp->b_bufsize == fs->fs_bsize)
372 					bp->b_flags |= B_CLUSTEROK;
373 				bdwrite(bp);
374 			}
375 		}
376 	}
377 	if (deallocated) {
378 #ifdef QUOTA
379 		/*
380 		 * Restore user's disk quota because allocation failed.
381 		 */
382 		(void) chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
383 #endif
384 		ip->i_blocks -= btodb(deallocated);
385 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
386 	}
387 	(void) VOP_FSYNC(vp, cred, MNT_WAIT, p);
388 	return (error);
389 }
390