81b1d4b9a4270d44549356c39237df25169aea5f
[dragonfly.git] / sys / gnu / vfs / 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  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/lock.h>
47 #include <sys/ucred.h>
48 #include <sys/vnode.h>
49
50 #include <sys/buf2.h>
51
52 #include "quota.h"
53 #include "inode.h"
54 #include "ext2_fs.h"
55 #include "ext2_fs_sb.h"
56 #include "fs.h"
57 #include "ext2_extern.h"
58
59 /*
60  * Balloc defines the structure of file system storage
61  * by allocating the physical blocks on a device given
62  * the inode and the logical block number in a file.
63  */
64 int
65 ext2_balloc(struct inode *ip, daddr_t bn, int size, struct ucred *cred,
66             struct buf **bpp, int flags)
67 {
68         struct ext2_sb_info *fs;
69         daddr_t nb;
70         struct buf *bp, *nbp;
71         struct vnode *vp = ITOV(ip);
72         struct indir indirs[NIADDR + 2];
73         daddr_t newb, lbn, *bap, pref;
74         int osize, nsize, num, i, error;
75 /*
76 ext2_debug("ext2_balloc called (%d, %d, %d)\n",
77         ip->i_number, (int)bn, (int)size);
78 */
79         *bpp = NULL;
80         if (bn < 0)
81                 return (EFBIG);
82         fs = ip->i_e2fs;
83         lbn = bn;
84
85         /*
86          * check if this is a sequential block allocation.
87          * If so, increment next_alloc fields to allow ext2_blkpref
88          * to make a good guess
89          */
90         if (lbn == ip->i_next_alloc_block + 1) {
91                 ip->i_next_alloc_block++;
92                 ip->i_next_alloc_goal++;
93         }
94
95         /*
96          * The first NDADDR blocks are direct blocks
97          */
98         if (bn < NDADDR) {
99                 nb = ip->i_db[bn];
100                 /* no new block is to be allocated, and no need to expand
101                    the file */
102                 if (nb != 0 && ip->i_size >= (bn + 1) * fs->s_blocksize) {
103                         error = bread(vp, lblktodoff(fs, bn),
104                                       fs->s_blocksize, &bp);
105                         if (error) {
106                                 brelse(bp);
107                                 return (error);
108                         }
109                         *bpp = bp;
110                         return (0);
111                 }
112                 if (nb != 0) {
113                         /*
114                          * Consider need to reallocate a fragment.
115                          */
116                         osize = fragroundup(fs, blkoff(fs, ip->i_size));
117                         nsize = fragroundup(fs, size);
118                         if (nsize <= osize) {
119                                 error = bread(vp, lblktodoff(fs, bn),
120                                               osize, &bp);
121                                 if (error) {
122                                         brelse(bp);
123                                         return (error);
124                                 }
125                         } else {
126                         /* Godmar thinks: this shouldn't happen w/o fragments */
127                                 kprintf("nsize %d(%d) > osize %d(%d) nb %d\n",
128                                         (int)nsize, (int)size, (int)osize,
129                                         (int)ip->i_size, (int)nb);
130                                 panic(
131                                     "ext2_balloc: Something is terribly wrong");
132 /*
133  * please note there haven't been any changes from here on -
134  * FFS seems to work.
135  */
136                         }
137                 } else {
138                         if (ip->i_size < (bn + 1) * fs->s_blocksize)
139                                 nsize = fragroundup(fs, size);
140                         else
141                                 nsize = fs->s_blocksize;
142                         error = ext2_alloc(ip, bn,
143                             ext2_blkpref(ip, bn, (int)bn, &ip->i_db[0], 0),
144                             nsize, cred, &newb);
145                         if (error)
146                                 return (error);
147                         bp = getblk(vp, lblktodoff(fs, bn), nsize, 0, 0);
148                         bp->b_bio2.bio_offset = fsbtodoff(fs, newb);
149                         if (flags & B_CLRBUF)
150                                 vfs_bio_clrbuf(bp);
151                 }
152                 ip->i_db[bn] = dofftofsb(fs, bp->b_bio2.bio_offset);
153                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
154                 *bpp = bp;
155                 return (0);
156         }
157         /*
158          * Determine the number of levels of indirection.
159          */
160         pref = 0;
161         if ((error = ext2_getlbns(vp, bn, indirs, &num)) != 0)
162                 return(error);
163 #if DIAGNOSTIC
164         if (num < 1)
165                 panic ("ext2_balloc: ext2_bmaparray returned indirect block");
166 #endif
167         /*
168          * Fetch the first indirect block allocating if necessary.
169          */
170         --num;
171         nb = ip->i_ib[indirs[0].in_off];
172         if (nb == 0) {
173 #if 0
174                 pref = ext2_blkpref(ip, lbn, 0, NULL, 0);
175 #else
176                 /* see the comment by ext2_blkpref. What we do here is
177                    to pretend that it'd be good for a block holding indirect
178                    pointers to be allocated near its predecessor in terms
179                    of indirection, or the last direct block.
180                    We shamelessly exploit the fact that i_ib immediately
181                    follows i_db.
182                    Godmar thinks it make sense to allocate i_ib[0] immediately
183                    after i_db[11], but it's not utterly clear whether this also
184                    applies to i_ib[1] and i_ib[0]
185                 */
186
187                 pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
188                                              EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
189 #endif
190                 if ((error = ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize,
191                     cred, &newb)) != 0)
192                         return (error);
193                 nb = newb;
194                 bp = getblk(vp, lblktodoff(fs, indirs[1].in_lbn),
195                             fs->s_blocksize, 0, 0);
196                 bp->b_bio2.bio_offset = fsbtodoff(fs, newb);
197                 vfs_bio_clrbuf(bp);
198                 /*
199                  * Write synchronously so that indirect blocks
200                  * never point at garbage.
201                  */
202                 if ((error = bwrite(bp)) != 0) {
203                         ext2_blkfree(ip, nb, fs->s_blocksize);
204                         return (error);
205                 }
206                 ip->i_ib[indirs[0].in_off] = newb;
207                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
208         }
209         /*
210          * Fetch through the indirect blocks, allocating as necessary.
211          */
212         for (i = 1;;) {
213                 error = bread(vp,
214                               lblktodoff(fs, indirs[i].in_lbn),
215                               (int)fs->s_blocksize, &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                                                 lblkno(fs, bp->b_loffset));
239 #else
240                         pref = ext2_blkpref(ip, lbn, 0, NULL, 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, lblktodoff(fs, indirs[i].in_lbn),
249                              fs->s_blocksize, 0, 0);
250                 nbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
251                 vfs_bio_clrbuf(nbp);
252                 /*
253                  * Write synchronously so that indirect blocks
254                  * never point at garbage.
255                  */
256                 if ((error = bwrite(nbp)) != 0) {
257                         ext2_blkfree(ip, nb, fs->s_blocksize);
258                         brelse(bp);
259                         return (error);
260                 }
261                 bap[indirs[i - 1].in_off] = nb;
262                 /*
263                  * If required, write synchronously, otherwise use
264                  * delayed write.
265                  */
266                 if (flags & B_SYNC) {
267                         bwrite(bp);
268                 } else {
269                         bdwrite(bp);
270                 }
271         }
272         /*
273          * Get the data block, allocating if necessary.
274          */
275         if (nb == 0) {
276                 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0],
277                                 lblkno(fs, bp->b_loffset));
278                 if ((error = ext2_alloc(ip,
279                     lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) {
280                         brelse(bp);
281                         return (error);
282                 }
283                 nb = newb;
284                 nbp = getblk(vp, lblktodoff(fs, lbn), fs->s_blocksize, 0, 0);
285                 nbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
286                 if (flags & B_CLRBUF)
287                         vfs_bio_clrbuf(nbp);
288                 bap[indirs[i].in_off] = nb;
289                 /*
290                  * If required, write synchronously, otherwise use
291                  * delayed write.
292                  */
293                 if (flags & B_SYNC) {
294                         bwrite(bp);
295                 } else {
296                         bdwrite(bp);
297                 }
298                 *bpp = nbp;
299                 return (0);
300         }
301         brelse(bp);
302         if (flags & B_CLRBUF) {
303                 error = bread(vp, lblktodoff(fs, lbn),
304                               (int)fs->s_blocksize, &nbp);
305                 if (error) {
306                         brelse(nbp);
307                         return (error);
308                 }
309         } else {
310                 nbp = getblk(vp, lblktodoff(fs, lbn), fs->s_blocksize, 0, 0);
311                 nbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
312         }
313         *bpp = nbp;
314         return (0);
315 }