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