xref: /freebsd/sys/ufs/ffs/ffs_balloc.c (revision 0c43d89a0d8e976ca494d4837f4c1f3734d2c300)
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.4 (Berkeley) 9/23/93
34  * $Id$
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, bn, size, cred, bpp, flags)
60 	register struct inode *ip;
61 	register daddr_t bn;
62 	int size;
63 	struct ucred *cred;
64 	struct buf **bpp;
65 	int flags;
66 {
67 	register struct fs *fs;
68 	register daddr_t nb;
69 	struct buf *bp, *nbp;
70 	struct vnode *vp = ITOV(ip);
71 	struct indir indirs[NIADDR + 2];
72 	daddr_t newb, lbn, *bap, pref;
73 	int osize, nsize, num, i, error;
74 
75 	*bpp = NULL;
76 	if (bn < 0)
77 		return (EFBIG);
78 	fs = ip->i_fs;
79 	lbn = bn;
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 < bn) {
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 			vnode_pager_setsize(vp, (u_long)ip->i_size);
97 			ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
98 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
99 			if (flags & B_SYNC)
100 				bwrite(bp);
101 			else
102 				bawrite(bp);
103 		}
104 	}
105 	/*
106 	 * The first NDADDR blocks are direct blocks
107 	 */
108 	if (bn < NDADDR) {
109 		nb = ip->i_db[bn];
110 		if (nb != 0 && ip->i_size >= (bn + 1) * fs->fs_bsize) {
111 			error = bread(vp, bn, fs->fs_bsize, NOCRED, &bp);
112 			if (error) {
113 				brelse(bp);
114 				return (error);
115 			}
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, bn, osize, NOCRED, &bp);
127 				if (error) {
128 					brelse(bp);
129 					return (error);
130 				}
131 			} else {
132 				error = ffs_realloccg(ip, bn,
133 				    ffs_blkpref(ip, bn, (int)bn, &ip->i_db[0]),
134 				    osize, nsize, cred, &bp);
135 				if (error)
136 					return (error);
137 			}
138 		} else {
139 			if (ip->i_size < (bn + 1) * fs->fs_bsize)
140 				nsize = fragroundup(fs, size);
141 			else
142 				nsize = fs->fs_bsize;
143 			error = ffs_alloc(ip, bn,
144 			    ffs_blkpref(ip, bn, (int)bn, &ip->i_db[0]),
145 			    nsize, cred, &newb);
146 			if (error)
147 				return (error);
148 			bp = getblk(vp, bn, nsize, 0, 0);
149 			bp->b_blkno = fsbtodb(fs, newb);
150 			if (flags & B_CLRBUF)
151 				clrbuf(bp);
152 		}
153 		ip->i_db[bn] = dbtofsb(fs, bp->b_blkno);
154 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
155 		*bpp = bp;
156 		return (0);
157 	}
158 	/*
159 	 * Determine the number of levels of indirection.
160 	 */
161 	pref = 0;
162 	if (error = ufs_getlbns(vp, bn, indirs, &num))
163 		return(error);
164 #ifdef DIAGNOSTIC
165 	if (num < 1)
166 		panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
167 #endif
168 	/*
169 	 * Fetch the first indirect block allocating if necessary.
170 	 */
171 	--num;
172 	nb = ip->i_ib[indirs[0].in_off];
173 	if (nb == 0) {
174 		pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0);
175 	        if (error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
176 		    cred, &newb))
177 			return (error);
178 		nb = newb;
179 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
180 		bp->b_blkno = fsbtodb(fs, newb);
181 		clrbuf(bp);
182 		/*
183 		 * Write synchronously so that indirect blocks
184 		 * never point at garbage.
185 		 */
186 		if (error = bwrite(bp)) {
187 			ffs_blkfree(ip, nb, fs->fs_bsize);
188 			return (error);
189 		}
190 		ip->i_ib[indirs[0].in_off] = newb;
191 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
192 	}
193 	/*
194 	 * Fetch through the indirect blocks, allocating as necessary.
195 	 */
196 	for (i = 1;;) {
197 		error = bread(vp,
198 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
199 		if (error) {
200 			brelse(bp);
201 			return (error);
202 		}
203 		bap = (daddr_t *)bp->b_data;
204 		nb = bap[indirs[i].in_off];
205 		if (i == num)
206 			break;
207 		i += 1;
208 		if (nb != 0) {
209 			brelse(bp);
210 			continue;
211 		}
212 		if (pref == 0)
213 			pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0);
214 		if (error =
215 		    ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) {
216 			brelse(bp);
217 			return (error);
218 		}
219 		nb = newb;
220 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
221 		nbp->b_blkno = fsbtodb(fs, nb);
222 		clrbuf(nbp);
223 		/*
224 		 * Write synchronously so that indirect blocks
225 		 * never point at garbage.
226 		 */
227 		if (error = bwrite(nbp)) {
228 			ffs_blkfree(ip, nb, fs->fs_bsize);
229 			brelse(bp);
230 			return (error);
231 		}
232 		bap[indirs[i - 1].in_off] = nb;
233 		/*
234 		 * If required, write synchronously, otherwise use
235 		 * delayed write.
236 		 */
237 		if (flags & B_SYNC) {
238 			bwrite(bp);
239 		} else {
240 			bdwrite(bp);
241 		}
242 	}
243 	/*
244 	 * Get the data block, allocating if necessary.
245 	 */
246 	if (nb == 0) {
247 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
248 		if (error = ffs_alloc(ip,
249 		    lbn, pref, (int)fs->fs_bsize, cred, &newb)) {
250 			brelse(bp);
251 			return (error);
252 		}
253 		nb = newb;
254 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
255 		nbp->b_blkno = fsbtodb(fs, nb);
256 		if (flags & B_CLRBUF)
257 			clrbuf(nbp);
258 		bap[indirs[i].in_off] = nb;
259 		/*
260 		 * If required, write synchronously, otherwise use
261 		 * delayed write.
262 		 */
263 		if (flags & B_SYNC) {
264 			bwrite(bp);
265 		} else {
266 			bdwrite(bp);
267 		}
268 		*bpp = nbp;
269 		return (0);
270 	}
271 	brelse(bp);
272 	if (flags & B_CLRBUF) {
273 		error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
274 		if (error) {
275 			brelse(nbp);
276 			return (error);
277 		}
278 	} else {
279 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
280 		nbp->b_blkno = fsbtodb(fs, nb);
281 	}
282 	*bpp = nbp;
283 	return (0);
284 }
285