Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / vfs / gnu / 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  * $FreeBSD: src/sys/gnu/ext2fs/ext2_balloc.c,v 1.9.2.1 2000/08/03 00:52:57 peter Exp $
41  * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_balloc.c,v 1.2 2003/06/17 04:28:33 dillon Exp $
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/buf.h>
47 #include <sys/lock.h>
48 #include <sys/ucred.h>
49 #include <sys/vnode.h>
50
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ufs/ufs_extern.h>
54
55 #include <gnu/ext2fs/ext2_fs.h>
56 #include <gnu/ext2fs/ext2_fs_sb.h>
57 #include <gnu/ext2fs/fs.h>
58 #include <gnu/ext2fs/ext2_extern.h>
59
60 /*
61  * Balloc defines the structure of file system storage
62  * by allocating the physical blocks on a device given
63  * the inode and the logical block number in a file.
64  */
65 int
66 ext2_balloc(ip, bn, size, cred, bpp, flags)
67         register struct inode *ip;
68         register daddr_t bn;
69         int size;
70         struct ucred *cred;
71         struct buf **bpp;
72         int flags;
73 {
74         register struct ext2_sb_info *fs;
75         register daddr_t nb;
76         struct buf *bp, *nbp;
77         struct vnode *vp = ITOV(ip);
78         struct indir indirs[NIADDR + 2];
79         daddr_t newb, lbn, *bap, pref;
80         int osize, nsize, num, i, error;
81 /*
82 ext2_debug("ext2_balloc called (%d, %d, %d)\n", 
83         ip->i_number, (int)bn, (int)size);
84 */
85         *bpp = NULL;
86         if (bn < 0)
87                 return (EFBIG);
88         fs = ip->i_e2fs;
89         lbn = bn;
90
91         /*
92          * check if this is a sequential block allocation. 
93          * If so, increment next_alloc fields to allow ext2_blkpref 
94          * to make a good guess
95          */
96         if (lbn == ip->i_next_alloc_block + 1) {
97                 ip->i_next_alloc_block++;
98                 ip->i_next_alloc_goal++;
99         }
100
101         /*
102          * The first NDADDR blocks are direct blocks
103          */
104         if (bn < NDADDR) {
105                 nb = ip->i_db[bn];
106                 /* no new block is to be allocated, and no need to expand
107                    the file */
108                 if (nb != 0 && ip->i_size >= (bn + 1) * fs->s_blocksize) {
109                         error = bread(vp, bn, fs->s_blocksize, 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                         /* Godmar thinks: this shouldn't happen w/o fragments */
131                                 printf("nsize %d(%d) > osize %d(%d) nb %d\n", 
132                                         (int)nsize, (int)size, (int)osize, 
133                                         (int)ip->i_size, (int)nb);
134                                 panic(
135                                     "ext2_balloc: Something is terribly wrong");
136 /*
137  * please note there haven't been any changes from here on -
138  * FFS seems to work.
139  */
140                         }
141                 } else {
142                         if (ip->i_size < (bn + 1) * fs->s_blocksize)
143                                 nsize = fragroundup(fs, size);
144                         else
145                                 nsize = fs->s_blocksize;
146                         error = ext2_alloc(ip, bn,
147                             ext2_blkpref(ip, bn, (int)bn, &ip->i_db[0], 0),
148                             nsize, cred, &newb);
149                         if (error)
150                                 return (error);
151                         bp = getblk(vp, bn, nsize, 0, 0);
152                         bp->b_blkno = fsbtodb(fs, newb);
153                         if (flags & B_CLRBUF)
154                                 vfs_bio_clrbuf(bp);
155                 }
156                 ip->i_db[bn] = dbtofsb(fs, bp->b_blkno);
157                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
158                 *bpp = bp;
159                 return (0);
160         }
161         /*
162          * Determine the number of levels of indirection.
163          */
164         pref = 0;
165         if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
166                 return(error);
167 #if DIAGNOSTIC
168         if (num < 1)
169                 panic ("ext2_balloc: ufs_bmaparray returned indirect block");
170 #endif
171         /*
172          * Fetch the first indirect block allocating if necessary.
173          */
174         --num;
175         nb = ip->i_ib[indirs[0].in_off];
176         if (nb == 0) {
177 #if 0
178                 pref = ext2_blkpref(ip, lbn, 0, (daddr_t *)0, 0);
179 #else
180                 /* see the comment by ext2_blkpref. What we do here is
181                    to pretend that it'd be good for a block holding indirect
182                    pointers to be allocated near its predecessor in terms 
183                    of indirection, or the last direct block. 
184                    We shamelessly exploit the fact that i_ib immediately
185                    follows i_db. 
186                    Godmar thinks it make sense to allocate i_ib[0] immediately
187                    after i_db[11], but it's not utterly clear whether this also
188                    applies to i_ib[1] and i_ib[0]
189                 */
190
191                 pref = ext2_blkpref(ip, lbn, indirs[0].in_off + 
192                                              EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
193 #endif
194                 if ((error = ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize,
195                     cred, &newb)) != 0)
196                         return (error);
197                 nb = newb;
198                 bp = getblk(vp, indirs[1].in_lbn, fs->s_blocksize, 0, 0);
199                 bp->b_blkno = fsbtodb(fs, newb);
200                 vfs_bio_clrbuf(bp);
201                 /*
202                  * Write synchronously so that indirect blocks
203                  * never point at garbage.
204                  */
205                 if ((error = bwrite(bp)) != 0) {
206                         ext2_blkfree(ip, nb, fs->s_blocksize);
207                         return (error);
208                 }
209                 ip->i_ib[indirs[0].in_off] = newb;
210                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
211         }
212         /*
213          * Fetch through the indirect blocks, allocating as necessary.
214          */
215         for (i = 1;;) {
216                 error = bread(vp,
217                     indirs[i].in_lbn, (int)fs->s_blocksize, NOCRED, &bp);
218                 if (error) {
219                         brelse(bp);
220                         return (error);
221                 }
222                 bap = (daddr_t *)bp->b_data;
223                 nb = bap[indirs[i].in_off];
224                 if (i == num)
225                         break;
226                 i += 1;
227                 if (nb != 0) {
228                         brelse(bp);
229                         continue;
230                 }
231                 if (pref == 0) 
232 #if 1
233                         /* see the comment above and by ext2_blkpref
234                          * I think this implements Linux policy, but
235                          * does it really make sense to allocate to
236                          * block containing pointers together ?
237                          * Also, will it ever succeed ?
238                          */
239                         pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
240                                                 bp->b_lblkno);
241 #else
242                         pref = ext2_blkpref(ip, lbn, 0, (daddr_t *)0, 0);
243 #endif
244                 if ((error =
245                     ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) {
246                         brelse(bp);
247                         return (error);
248                 }
249                 nb = newb;
250                 nbp = getblk(vp, indirs[i].in_lbn, fs->s_blocksize, 0, 0);
251                 nbp->b_blkno = fsbtodb(fs, nb);
252                 vfs_bio_clrbuf(nbp);
253                 /*
254                  * Write synchronously so that indirect blocks
255                  * never point at garbage.
256                  */
257                 if ((error = bwrite(nbp)) != 0) {
258                         ext2_blkfree(ip, nb, fs->s_blocksize);
259                         brelse(bp);
260                         return (error);
261                 }
262                 bap[indirs[i - 1].in_off] = nb;
263                 /*
264                  * If required, write synchronously, otherwise use
265                  * delayed write.
266                  */
267                 if (flags & B_SYNC) {
268                         bwrite(bp);
269                 } else {
270                         bdwrite(bp);
271                 }
272         }
273         /*
274          * Get the data block, allocating if necessary.
275          */
276         if (nb == 0) {
277                 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0], 
278                                 bp->b_lblkno);
279                 if ((error = ext2_alloc(ip,
280                     lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) {
281                         brelse(bp);
282                         return (error);
283                 }
284                 nb = newb;
285                 nbp = getblk(vp, lbn, fs->s_blocksize, 0, 0);
286                 nbp->b_blkno = fsbtodb(fs, nb);
287                 if (flags & B_CLRBUF)
288                         vfs_bio_clrbuf(nbp);
289                 bap[indirs[i].in_off] = nb;
290                 /*
291                  * If required, write synchronously, otherwise use
292                  * delayed write.
293                  */
294                 if (flags & B_SYNC) {
295                         bwrite(bp);
296                 } else {
297                         bdwrite(bp);
298                 }
299                 *bpp = nbp;
300                 return (0);
301         }
302         brelse(bp);
303         if (flags & B_CLRBUF) {
304                 error = bread(vp, lbn, (int)fs->s_blocksize, NOCRED, &nbp);
305                 if (error) {
306                         brelse(nbp);
307                         return (error);
308                 }
309         } else {
310                 nbp = getblk(vp, lbn, fs->s_blocksize, 0, 0);
311                 nbp->b_blkno = fsbtodb(fs, nb);
312         }
313         *bpp = nbp;
314         return (0);
315 }