xref: /freebsd/sys/ufs/ffs/ffs_balloc.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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  * $Id: ffs_balloc.c,v 1.14 1997/04/10 13:17:09 bde Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/buf.h>
40 #include <sys/vnode.h>
41 
42 #include <ufs/ufs/quota.h>
43 #include <ufs/ufs/inode.h>
44 #include <ufs/ufs/ufs_extern.h>
45 
46 #include <ufs/ffs/fs.h>
47 #include <ufs/ffs/ffs_extern.h>
48 
49 /*
50  * Balloc defines the structure of file system storage
51  * by allocating the physical blocks on a device given
52  * the inode and the logical block number in a file.
53  */
54 int
55 ffs_balloc(ip, lbn, size, cred, bpp, flags)
56 	register struct inode *ip;
57 	register ufs_daddr_t lbn;
58 	int size;
59 	struct ucred *cred;
60 	struct buf **bpp;
61 	int flags;
62 {
63 	register struct fs *fs;
64 	register ufs_daddr_t nb;
65 	struct buf *bp, *nbp;
66 	struct vnode *vp = ITOV(ip);
67 	struct indir indirs[NIADDR + 2];
68 	ufs_daddr_t newb, *bap, pref;
69 	int deallocated, osize, nsize, num, i, error;
70 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
71 
72 	*bpp = NULL;
73 	if (lbn < 0)
74 		return (EFBIG);
75 	fs = ip->i_fs;
76 
77 	/*
78 	 * If the next write will extend the file into a new block,
79 	 * and the file is currently composed of a fragment
80 	 * this fragment has to be extended to be a full block.
81 	 */
82 	nb = lblkno(fs, ip->i_size);
83 	if (nb < NDADDR && nb < lbn) {
84 		osize = blksize(fs, ip, nb);
85 		if (osize < fs->fs_bsize && osize > 0) {
86 			error = ffs_realloccg(ip, nb,
87 				ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
88 				osize, (int)fs->fs_bsize, cred, &bp);
89 			if (error)
90 				return (error);
91 			ip->i_size = smalllblktosize(fs, nb + 1);
92 			ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
93 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
94 			if (flags & B_SYNC)
95 				bwrite(bp);
96 			else
97 				bawrite(bp);
98 		}
99 	}
100 	/*
101 	 * The first NDADDR blocks are direct blocks
102 	 */
103 	if (lbn < NDADDR) {
104 		nb = ip->i_db[lbn];
105 		if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
106 			error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
107 			if (error) {
108 				brelse(bp);
109 				return (error);
110 			}
111 			bp->b_blkno = fsbtodb(fs, nb);
112 			*bpp = bp;
113 			return (0);
114 		}
115 		if (nb != 0) {
116 			/*
117 			 * Consider need to reallocate a fragment.
118 			 */
119 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
120 			nsize = fragroundup(fs, size);
121 			if (nsize <= osize) {
122 				error = bread(vp, lbn, osize, NOCRED, &bp);
123 				if (error) {
124 					brelse(bp);
125 					return (error);
126 				}
127 				bp->b_blkno = fsbtodb(fs, nb);
128 			} else {
129 				error = ffs_realloccg(ip, lbn,
130 				    ffs_blkpref(ip, lbn, (int)lbn,
131 					&ip->i_db[0]), osize, nsize, cred, &bp);
132 				if (error)
133 					return (error);
134 			}
135 		} else {
136 			if (ip->i_size < smalllblktosize(fs, lbn + 1))
137 				nsize = fragroundup(fs, size);
138 			else
139 				nsize = fs->fs_bsize;
140 			error = ffs_alloc(ip, lbn,
141 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
142 			    nsize, cred, &newb);
143 			if (error)
144 				return (error);
145 			bp = getblk(vp, lbn, nsize, 0, 0);
146 			bp->b_blkno = fsbtodb(fs, newb);
147 			if (flags & B_CLRBUF)
148 				vfs_bio_clrbuf(bp);
149 		}
150 		ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
151 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
152 		*bpp = bp;
153 		return (0);
154 	}
155 	/*
156 	 * Determine the number of levels of indirection.
157 	 */
158 	pref = 0;
159 	if (error = ufs_getlbns(vp, lbn, indirs, &num))
160 		return(error);
161 #ifdef DIAGNOSTIC
162 	if (num < 1)
163 		panic ("ffs_balloc: ufs_bmaparray returned indirect block");
164 #endif
165 	/*
166 	 * Fetch the first indirect block allocating if necessary.
167 	 */
168 	--num;
169 	nb = ip->i_ib[indirs[0].in_off];
170 	allocib = NULL;
171 	allocblk = allociblk;
172 	if (nb == 0) {
173 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
174 	        if (error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
175 		    cred, &newb))
176 			return (error);
177 		nb = newb;
178 		*allocblk++ = nb;
179 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
180 		bp->b_blkno = fsbtodb(fs, nb);
181 		vfs_bio_clrbuf(bp);
182 		/*
183 		 * Write synchronously so that indirect blocks
184 		 * never point at garbage.
185 		 */
186 		if (error = bwrite(bp))
187 			goto fail;
188 		allocib = &ip->i_ib[indirs[0].in_off];
189 		*allocib = nb;
190 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
191 	}
192 	/*
193 	 * Fetch through the indirect blocks, allocating as necessary.
194 	 */
195 	for (i = 1;;) {
196 		error = bread(vp,
197 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
198 		if (error) {
199 			brelse(bp);
200 			goto fail;
201 		}
202 		bap = (ufs_daddr_t *)bp->b_data;
203 		nb = bap[indirs[i].in_off];
204 		if (i == num)
205 			break;
206 		i += 1;
207 		if (nb != 0) {
208 			bqrelse(bp);
209 			continue;
210 		}
211 		if (pref == 0)
212 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
213 		if (error =
214 		    ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) {
215 			brelse(bp);
216 			goto fail;
217 		}
218 		nb = newb;
219 		*allocblk++ = nb;
220 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
221 		nbp->b_blkno = fsbtodb(fs, nb);
222 		vfs_bio_clrbuf(nbp);
223 		/*
224 		 * Write synchronously so that indirect blocks
225 		 * never point at garbage.
226 		 */
227 		if (error = bwrite(nbp)) {
228 			brelse(bp);
229 			goto fail;
230 		}
231 		bap[indirs[i - 1].in_off] = nb;
232 		/*
233 		 * If required, write synchronously, otherwise use
234 		 * delayed write.
235 		 */
236 		if (flags & B_SYNC) {
237 			bwrite(bp);
238 		} else {
239 			bdwrite(bp);
240 		}
241 	}
242 	/*
243 	 * Get the data block, allocating if necessary.
244 	 */
245 	if (nb == 0) {
246 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
247 		error = ffs_alloc(ip,
248 		    lbn, pref, (int)fs->fs_bsize, cred, &newb);
249 		if (error) {
250 			brelse(bp);
251 			goto fail;
252 		}
253 		nb = newb;
254 		*allocblk++ = nb;
255 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
256 		nbp->b_blkno = fsbtodb(fs, nb);
257 		if (flags & B_CLRBUF)
258 			vfs_bio_clrbuf(nbp);
259 		bap[indirs[i].in_off] = nb;
260 		/*
261 		 * If required, write synchronously, otherwise use
262 		 * delayed write.
263 		 */
264 		if (flags & B_SYNC) {
265 			bwrite(bp);
266 		} else {
267 			bdwrite(bp);
268 		}
269 		*bpp = nbp;
270 		return (0);
271 	}
272 	brelse(bp);
273 	if (flags & B_CLRBUF) {
274 		error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
275 		if (error) {
276 			brelse(nbp);
277 			goto fail;
278 		}
279 	} else {
280 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
281 		nbp->b_blkno = fsbtodb(fs, nb);
282 	}
283 	*bpp = nbp;
284 	return (0);
285 fail:
286 	/*
287 	 * If we have failed part way through block allocation, we
288 	 * have to deallocate any indirect blocks that we have allocated.
289 	 */
290 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
291 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
292 		deallocated += fs->fs_bsize;
293 	}
294 	if (allocib != NULL)
295 		*allocib = 0;
296 	if (deallocated) {
297 #ifdef QUOTA
298 		/*
299 		 * Restore user's disk quota because allocation failed.
300 		 */
301 		(void) chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
302 #endif
303 		ip->i_blocks -= btodb(deallocated);
304 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
305 	}
306 	return (error);
307 }
308