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