]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/gnu/fs/ext2fs/ext2_balloc.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sys / gnu / fs / ext2fs / ext2_balloc.c
1 /*
2  *  modified for Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*
8  * Copyright (c) 1982, 1986, 1989, 1993
9  *      The Regents of the University of California.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      @(#)ffs_balloc.c        8.4 (Berkeley) 9/23/93
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/buf.h>
45 #include <sys/lock.h>
46 #include <sys/ucred.h>
47 #include <sys/vnode.h>
48
49 #include <ufs/ufs/quota.h>
50 #include <ufs/ufs/inode.h>
51 #include <ufs/ufs/ufs_extern.h>
52
53 #include <gnu/ext2fs/ext2_fs.h>
54 #include <gnu/ext2fs/ext2_fs_sb.h>
55 #include <gnu/ext2fs/fs.h>
56 #include <gnu/ext2fs/ext2_extern.h>
57
58 /*
59  * Balloc defines the structure of file system storage
60  * by allocating the physical blocks on a device given
61  * the inode and the logical block number in a file.
62  */
63 int
64 ext2_balloc(ip, bn, size, cred, bpp, flags)
65         register struct inode *ip;
66         register daddr_t bn;
67         int size;
68         struct ucred *cred;
69         struct buf **bpp;
70         int flags;
71 {
72         register struct ext2_sb_info *fs;
73         register daddr_t nb;
74         struct buf *bp, *nbp;
75         struct vnode *vp = ITOV(ip);
76         struct indir indirs[NIADDR + 2];
77         daddr_t newb, lbn, *bap, pref;
78         int osize, nsize, num, i, error;
79 /*
80 ext2_debug("ext2_balloc called (%d, %d, %d)\n", 
81         ip->i_number, (int)bn, (int)size);
82 */
83         *bpp = NULL;
84         if (bn < 0)
85                 return (EFBIG);
86         fs = ip->i_e2fs;
87         lbn = bn;
88
89         /*
90          * check if this is a sequential block allocation. 
91          * If so, increment next_alloc fields to allow ext2_blkpref 
92          * to make a good guess
93          */
94         if (lbn == ip->i_next_alloc_block + 1) {
95                 ip->i_next_alloc_block++;
96                 ip->i_next_alloc_goal++;
97         }
98
99         /*
100          * The first NDADDR blocks are direct blocks
101          */
102         if (bn < NDADDR) {
103                 nb = ip->i_db[bn];
104                 /* no new block is to be allocated, and no need to expand
105                    the file */
106                 if (nb != 0 && ip->i_size >= (bn + 1) * fs->s_blocksize) {
107                         error = bread(vp, bn, fs->s_blocksize, NOCRED, &bp);
108                         if (error) {
109                                 brelse(bp);
110                                 return (error);
111                         }
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, bn, osize, NOCRED, &bp);
123                                 if (error) {
124                                         brelse(bp);
125                                         return (error);
126                                 }
127                         } else {
128                         /* Godmar thinks: this shouldn't happen w/o fragments */
129                                 printf("nsize %d(%d) > osize %d(%d) nb %d\n", 
130                                         (int)nsize, (int)size, (int)osize, 
131                                         (int)ip->i_size, (int)nb);
132                                 panic(
133                                     "ext2_balloc: Something is terribly wrong");
134 /*
135  * please note there haven't been any changes from here on -
136  * FFS seems to work.
137  */
138                         }
139                 } else {
140                         if (ip->i_size < (bn + 1) * fs->s_blocksize)
141                                 nsize = fragroundup(fs, size);
142                         else
143                                 nsize = fs->s_blocksize;
144                         error = ext2_alloc(ip, bn,
145                             ext2_blkpref(ip, bn, (int)bn, &ip->i_db[0], 0),
146                             nsize, cred, &newb);
147                         if (error)
148                                 return (error);
149                         bp = getblk(vp, bn, 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[bn] = 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, bn, indirs, &num)) != 0)
164                 return(error);
165 #if DIAGNOSTIC
166         if (num < 1)
167                 panic ("ext2_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         if (nb == 0) {
175 #if 0
176                 pref = ext2_blkpref(ip, lbn, 0, (daddr_t *)0, 0);
177 #else
178                 /* see the comment by ext2_blkpref. What we do here is
179                    to pretend that it'd be good for a block holding indirect
180                    pointers to be allocated near its predecessor in terms 
181                    of indirection, or the last direct block. 
182                    We shamelessly exploit the fact that i_ib immediately
183                    follows i_db. 
184                    Godmar thinks it make sense to allocate i_ib[0] immediately
185                    after i_db[11], but it's not utterly clear whether this also
186                    applies to i_ib[1] and i_ib[0]
187                 */
188
189                 pref = ext2_blkpref(ip, lbn, indirs[0].in_off + 
190                                              EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
191 #endif
192                 if ((error = ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize,
193                     cred, &newb)) != 0)
194                         return (error);
195                 nb = newb;
196                 bp = getblk(vp, indirs[1].in_lbn, fs->s_blocksize, 0, 0);
197                 bp->b_blkno = fsbtodb(fs, newb);
198                 vfs_bio_clrbuf(bp);
199                 /*
200                  * Write synchronously so that indirect blocks
201                  * never point at garbage.
202                  */
203                 if ((error = bwrite(bp)) != 0) {
204                         ext2_blkfree(ip, nb, fs->s_blocksize);
205                         return (error);
206                 }
207                 ip->i_ib[indirs[0].in_off] = newb;
208                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
209         }
210         /*
211          * Fetch through the indirect blocks, allocating as necessary.
212          */
213         for (i = 1;;) {
214                 error = bread(vp,
215                     indirs[i].in_lbn, (int)fs->s_blocksize, NOCRED, &bp);
216                 if (error) {
217                         brelse(bp);
218                         return (error);
219                 }
220                 bap = (daddr_t *)bp->b_data;
221                 nb = bap[indirs[i].in_off];
222                 if (i == num)
223                         break;
224                 i += 1;
225                 if (nb != 0) {
226                         brelse(bp);
227                         continue;
228                 }
229                 if (pref == 0) 
230 #if 1
231                         /* see the comment above and by ext2_blkpref
232                          * I think this implements Linux policy, but
233                          * does it really make sense to allocate to
234                          * block containing pointers together ?
235                          * Also, will it ever succeed ?
236                          */
237                         pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
238                                                 bp->b_lblkno);
239 #else
240                         pref = ext2_blkpref(ip, lbn, 0, (daddr_t *)0, 0);
241 #endif
242                 if ((error =
243                     ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) {
244                         brelse(bp);
245                         return (error);
246                 }
247                 nb = newb;
248                 nbp = getblk(vp, indirs[i].in_lbn, fs->s_blocksize, 0, 0);
249                 nbp->b_blkno = fsbtodb(fs, nb);
250                 vfs_bio_clrbuf(nbp);
251                 /*
252                  * Write synchronously so that indirect blocks
253                  * never point at garbage.
254                  */
255                 if ((error = bwrite(nbp)) != 0) {
256                         ext2_blkfree(ip, nb, fs->s_blocksize);
257                         brelse(bp);
258                         return (error);
259                 }
260                 bap[indirs[i - 1].in_off] = nb;
261                 /*
262                  * If required, write synchronously, otherwise use
263                  * delayed write.
264                  */
265                 if (flags & B_SYNC) {
266                         bwrite(bp);
267                 } else {
268                         bdwrite(bp);
269                 }
270         }
271         /*
272          * Get the data block, allocating if necessary.
273          */
274         if (nb == 0) {
275                 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0], 
276                                 bp->b_lblkno);
277                 if ((error = ext2_alloc(ip,
278                     lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) {
279                         brelse(bp);
280                         return (error);
281                 }
282                 nb = newb;
283                 nbp = getblk(vp, lbn, fs->s_blocksize, 0, 0);
284                 nbp->b_blkno = fsbtodb(fs, nb);
285                 if (flags & B_CLRBUF)
286                         vfs_bio_clrbuf(nbp);
287                 bap[indirs[i].in_off] = nb;
288                 /*
289                  * If required, write synchronously, otherwise use
290                  * delayed write.
291                  */
292                 if (flags & B_SYNC) {
293                         bwrite(bp);
294                 } else {
295                         bdwrite(bp);
296                 }
297                 *bpp = nbp;
298                 return (0);
299         }
300         brelse(bp);
301         if (flags & B_CLRBUF) {
302                 error = bread(vp, lbn, (int)fs->s_blocksize, NOCRED, &nbp);
303                 if (error) {
304                         brelse(nbp);
305                         return (error);
306                 }
307         } else {
308                 nbp = getblk(vp, lbn, fs->s_blocksize, 0, 0);
309                 nbp->b_blkno = fsbtodb(fs, nb);
310         }
311         *bpp = nbp;
312         return (0);
313 }