ffs_balloc.c revision 50477
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: head/sys/ufs/ffs/ffs_balloc.c 50477 1999-08-28 01:08:13Z peter $
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 */
56int
57ffs_balloc(ap)
58	struct vop_balloc_args /* {
59		struct inode *a_ip;
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	register struct inode *ip;
68	register 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
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		osize = blksize(fs, ip, nb);
102		if (osize < fs->fs_bsize && osize > 0) {
103			error = ffs_realloccg(ip, nb,
104				ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
105				osize, (int)fs->fs_bsize, cred, &bp);
106			if (error)
107				return (error);
108			if (DOINGSOFTDEP(vp))
109				softdep_setup_allocdirect(ip, nb,
110				    dbtofsb(fs, bp->b_blkno), ip->i_db[nb],
111				    fs->fs_bsize, osize, bp);
112			ip->i_size = smalllblktosize(fs, nb + 1);
113			ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
114			ip->i_flag |= IN_CHANGE | IN_UPDATE;
115			if (flags & B_SYNC)
116				bwrite(bp);
117			else
118				bawrite(bp);
119		}
120	}
121	/*
122	 * The first NDADDR blocks are direct blocks
123	 */
124	if (lbn < NDADDR) {
125		nb = ip->i_db[lbn];
126		if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) {
127			error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
128			if (error) {
129				brelse(bp);
130				return (error);
131			}
132			bp->b_blkno = fsbtodb(fs, nb);
133			*ap->a_bpp = bp;
134			return (0);
135		}
136		if (nb != 0) {
137			/*
138			 * Consider need to reallocate a fragment.
139			 */
140			osize = fragroundup(fs, blkoff(fs, ip->i_size));
141			nsize = fragroundup(fs, size);
142			if (nsize <= osize) {
143				error = bread(vp, lbn, osize, NOCRED, &bp);
144				if (error) {
145					brelse(bp);
146					return (error);
147				}
148				bp->b_blkno = fsbtodb(fs, nb);
149			} else {
150				error = ffs_realloccg(ip, lbn,
151				    ffs_blkpref(ip, lbn, (int)lbn,
152					&ip->i_db[0]), osize, nsize, cred, &bp);
153				if (error)
154					return (error);
155				if (DOINGSOFTDEP(vp))
156					softdep_setup_allocdirect(ip, lbn,
157					    dbtofsb(fs, bp->b_blkno), nb,
158					    nsize, osize, bp);
159			}
160		} else {
161			if (ip->i_size < smalllblktosize(fs, lbn + 1))
162				nsize = fragroundup(fs, size);
163			else
164				nsize = fs->fs_bsize;
165			error = ffs_alloc(ip, lbn,
166			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
167			    nsize, cred, &newb);
168			if (error)
169				return (error);
170			bp = getblk(vp, lbn, nsize, 0, 0);
171			bp->b_blkno = fsbtodb(fs, newb);
172			if (flags & B_CLRBUF)
173				vfs_bio_clrbuf(bp);
174			if (DOINGSOFTDEP(vp))
175				softdep_setup_allocdirect(ip, lbn, newb, 0,
176				    nsize, 0, bp);
177		}
178		ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
179		ip->i_flag |= IN_CHANGE | IN_UPDATE;
180		*ap->a_bpp = bp;
181		return (0);
182	}
183	/*
184	 * Determine the number of levels of indirection.
185	 */
186	pref = 0;
187	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
188		return(error);
189#ifdef DIAGNOSTIC
190	if (num < 1)
191		panic ("ffs_balloc: ufs_bmaparray returned indirect block");
192#endif
193	/*
194	 * Fetch the first indirect block allocating if necessary.
195	 */
196	--num;
197	nb = ip->i_ib[indirs[0].in_off];
198	allocib = NULL;
199	allocblk = allociblk;
200	if (nb == 0) {
201		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
202	        if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
203		    cred, &newb)) != 0)
204			return (error);
205		nb = newb;
206		*allocblk++ = nb;
207		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
208		bp->b_blkno = fsbtodb(fs, nb);
209		vfs_bio_clrbuf(bp);
210		if (DOINGSOFTDEP(vp)) {
211			softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off,
212			    newb, 0, fs->fs_bsize, 0, bp);
213			bdwrite(bp);
214		} else {
215			/*
216			 * Write synchronously so that indirect blocks
217			 * never point at garbage.
218			 */
219			if (DOINGASYNC(vp))
220				bdwrite(bp);
221			else if ((error = bwrite(bp)) != 0)
222				goto fail;
223		}
224		allocib = &ip->i_ib[indirs[0].in_off];
225		*allocib = nb;
226		ip->i_flag |= IN_CHANGE | IN_UPDATE;
227	}
228	/*
229	 * Fetch through the indirect blocks, allocating as necessary.
230	 */
231	for (i = 1;;) {
232		error = bread(vp,
233		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
234		if (error) {
235			brelse(bp);
236			goto fail;
237		}
238		bap = (ufs_daddr_t *)bp->b_data;
239		nb = bap[indirs[i].in_off];
240		if (i == num)
241			break;
242		i += 1;
243		if (nb != 0) {
244			bqrelse(bp);
245			continue;
246		}
247		if (pref == 0)
248			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
249		if ((error =
250		    ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) != 0) {
251			brelse(bp);
252			goto fail;
253		}
254		nb = newb;
255		*allocblk++ = nb;
256		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
257		nbp->b_blkno = fsbtodb(fs, nb);
258		vfs_bio_clrbuf(nbp);
259		if (DOINGSOFTDEP(vp)) {
260			softdep_setup_allocindir_meta(nbp, ip, bp,
261			    indirs[i - 1].in_off, nb);
262			bdwrite(nbp);
263		} else {
264			/*
265			 * Write synchronously so that indirect blocks
266			 * never point at garbage.
267			 */
268			if ((error = bwrite(nbp)) != 0) {
269				brelse(bp);
270				goto fail;
271			}
272		}
273		bap[indirs[i - 1].in_off] = nb;
274		/*
275		 * If required, write synchronously, otherwise use
276		 * delayed write.
277		 */
278		if (flags & B_SYNC) {
279			bwrite(bp);
280		} else {
281			if (bp->b_bufsize == fs->fs_bsize)
282				bp->b_flags |= B_CLUSTEROK;
283			bdwrite(bp);
284		}
285	}
286	/*
287	 * Get the data block, allocating if necessary.
288	 */
289	if (nb == 0) {
290		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
291		error = ffs_alloc(ip,
292		    lbn, pref, (int)fs->fs_bsize, cred, &newb);
293		if (error) {
294			brelse(bp);
295			goto fail;
296		}
297		nb = newb;
298		*allocblk++ = nb;
299		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
300		nbp->b_blkno = fsbtodb(fs, nb);
301		if (flags & B_CLRBUF)
302			vfs_bio_clrbuf(nbp);
303		if (DOINGSOFTDEP(vp))
304			softdep_setup_allocindir_page(ip, lbn, bp,
305			    indirs[i].in_off, nb, 0, nbp);
306		bap[indirs[i].in_off] = nb;
307		/*
308		 * If required, write synchronously, otherwise use
309		 * delayed write.
310		 */
311		if (flags & B_SYNC) {
312			bwrite(bp);
313		} else {
314			if (bp->b_bufsize == fs->fs_bsize)
315				bp->b_flags |= B_CLUSTEROK;
316			bdwrite(bp);
317		}
318		*ap->a_bpp = nbp;
319		return (0);
320	}
321	brelse(bp);
322	if (flags & B_CLRBUF) {
323		error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
324		if (error) {
325			brelse(nbp);
326			goto fail;
327		}
328	} else {
329		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
330		nbp->b_blkno = fsbtodb(fs, nb);
331	}
332	*ap->a_bpp = nbp;
333	return (0);
334fail:
335	/*
336	 * If we have failed part way through block allocation, we
337	 * have to deallocate any indirect blocks that we have allocated.
338	 */
339	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
340		ffs_blkfree(ip, *blkp, fs->fs_bsize);
341		deallocated += fs->fs_bsize;
342	}
343	if (allocib != NULL)
344		*allocib = 0;
345	if (deallocated) {
346#ifdef QUOTA
347		/*
348		 * Restore user's disk quota because allocation failed.
349		 */
350		(void) chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
351#endif
352		ip->i_blocks -= btodb(deallocated);
353		ip->i_flag |= IN_CHANGE | IN_UPDATE;
354	}
355	return (error);
356}
357