Properly calculate the ronly flag at unmount time.
[dragonfly.git] / sys / vfs / gnu / ext2fs / ext2_balloc.c
CommitLineData
984263bc
MD
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 $
54078292 41 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_balloc.c,v 1.8 2006/03/24 18:35:33 dillon Exp $
984263bc
MD
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
1f2de5d4
MD
51#include <vfs/ufs/quota.h>
52#include <vfs/ufs/inode.h>
53#include <vfs/ufs/ufs_extern.h>
984263bc 54
1f2de5d4
MD
55#include "ext2_fs.h"
56#include "ext2_fs_sb.h"
57#include "fs.h"
58#include "ext2_extern.h"
984263bc
MD
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 */
65int
0f7f7a49
CP
66ext2_balloc(struct inode *ip, daddr_t bn, int size, struct ucred *cred,
67 struct buf **bpp, int flags)
984263bc 68{
f7aae92f
RG
69 struct ext2_sb_info *fs;
70 daddr_t nb;
984263bc
MD
71 struct buf *bp, *nbp;
72 struct vnode *vp = ITOV(ip);
73 struct indir indirs[NIADDR + 2];
74 daddr_t newb, lbn, *bap, pref;
75 int osize, nsize, num, i, error;
76/*
77ext2_debug("ext2_balloc called (%d, %d, %d)\n",
78 ip->i_number, (int)bn, (int)size);
79*/
80 *bpp = NULL;
81 if (bn < 0)
82 return (EFBIG);
83 fs = ip->i_e2fs;
84 lbn = bn;
85
86 /*
87 * check if this is a sequential block allocation.
88 * If so, increment next_alloc fields to allow ext2_blkpref
89 * to make a good guess
90 */
91 if (lbn == ip->i_next_alloc_block + 1) {
92 ip->i_next_alloc_block++;
93 ip->i_next_alloc_goal++;
94 }
95
96 /*
97 * The first NDADDR blocks are direct blocks
98 */
99 if (bn < NDADDR) {
100 nb = ip->i_db[bn];
101 /* no new block is to be allocated, and no need to expand
102 the file */
103 if (nb != 0 && ip->i_size >= (bn + 1) * fs->s_blocksize) {
54078292
MD
104 error = bread(vp, lblktodoff(fs, bn),
105 fs->s_blocksize, &bp);
984263bc
MD
106 if (error) {
107 brelse(bp);
108 return (error);
109 }
110 *bpp = bp;
111 return (0);
112 }
113 if (nb != 0) {
114 /*
115 * Consider need to reallocate a fragment.
116 */
117 osize = fragroundup(fs, blkoff(fs, ip->i_size));
118 nsize = fragroundup(fs, size);
119 if (nsize <= osize) {
54078292
MD
120 error = bread(vp, lblktodoff(fs, bn),
121 osize, &bp);
984263bc
MD
122 if (error) {
123 brelse(bp);
124 return (error);
125 }
126 } else {
127 /* Godmar thinks: this shouldn't happen w/o fragments */
128 printf("nsize %d(%d) > osize %d(%d) nb %d\n",
129 (int)nsize, (int)size, (int)osize,
130 (int)ip->i_size, (int)nb);
131 panic(
132 "ext2_balloc: Something is terribly wrong");
133/*
134 * please note there haven't been any changes from here on -
135 * FFS seems to work.
136 */
137 }
138 } else {
139 if (ip->i_size < (bn + 1) * fs->s_blocksize)
140 nsize = fragroundup(fs, size);
141 else
142 nsize = fs->s_blocksize;
143 error = ext2_alloc(ip, bn,
144 ext2_blkpref(ip, bn, (int)bn, &ip->i_db[0], 0),
145 nsize, cred, &newb);
146 if (error)
147 return (error);
54078292
MD
148 bp = getblk(vp, lblktodoff(fs, bn), nsize, 0, 0);
149 bp->b_bio2.bio_offset = fsbtodoff(fs, newb);
984263bc
MD
150 if (flags & B_CLRBUF)
151 vfs_bio_clrbuf(bp);
152 }
54078292 153 ip->i_db[bn] = dofftofsb(fs, bp->b_bio2.bio_offset);
984263bc
MD
154 ip->i_flag |= IN_CHANGE | IN_UPDATE;
155 *bpp = bp;
156 return (0);
157 }
158 /*
159 * Determine the number of levels of indirection.
160 */
161 pref = 0;
162 if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
163 return(error);
164#if DIAGNOSTIC
165 if (num < 1)
166 panic ("ext2_balloc: ufs_bmaparray returned indirect block");
167#endif
168 /*
169 * Fetch the first indirect block allocating if necessary.
170 */
171 --num;
172 nb = ip->i_ib[indirs[0].in_off];
173 if (nb == 0) {
174#if 0
175 pref = ext2_blkpref(ip, lbn, 0, (daddr_t *)0, 0);
176#else
177 /* see the comment by ext2_blkpref. What we do here is
178 to pretend that it'd be good for a block holding indirect
179 pointers to be allocated near its predecessor in terms
180 of indirection, or the last direct block.
181 We shamelessly exploit the fact that i_ib immediately
182 follows i_db.
183 Godmar thinks it make sense to allocate i_ib[0] immediately
184 after i_db[11], but it's not utterly clear whether this also
185 applies to i_ib[1] and i_ib[0]
186 */
187
188 pref = ext2_blkpref(ip, lbn, indirs[0].in_off +
189 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0);
190#endif
191 if ((error = ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize,
192 cred, &newb)) != 0)
193 return (error);
194 nb = newb;
54078292
MD
195 bp = getblk(vp, lblktodoff(fs, indirs[1].in_lbn),
196 fs->s_blocksize, 0, 0);
197 bp->b_bio2.bio_offset = fsbtodoff(fs, newb);
984263bc
MD
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,
54078292
MD
215 lblktodoff(fs, indirs[i].in_lbn),
216 (int)fs->s_blocksize, &bp);
984263bc
MD
217 if (error) {
218 brelse(bp);
219 return (error);
220 }
221 bap = (daddr_t *)bp->b_data;
222 nb = bap[indirs[i].in_off];
223 if (i == num)
224 break;
225 i += 1;
226 if (nb != 0) {
227 brelse(bp);
228 continue;
229 }
230 if (pref == 0)
231#if 1
232 /* see the comment above and by ext2_blkpref
233 * I think this implements Linux policy, but
234 * does it really make sense to allocate to
235 * block containing pointers together ?
236 * Also, will it ever succeed ?
237 */
238 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap,
54078292 239 lblkno(fs, bp->b_loffset));
984263bc
MD
240#else
241 pref = ext2_blkpref(ip, lbn, 0, (daddr_t *)0, 0);
242#endif
243 if ((error =
244 ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) {
245 brelse(bp);
246 return (error);
247 }
248 nb = newb;
54078292
MD
249 nbp = getblk(vp, lblktodoff(fs, indirs[i].in_lbn),
250 fs->s_blocksize, 0, 0);
251 nbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
984263bc
MD
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],
54078292 278 lblkno(fs, bp->b_loffset));
984263bc
MD
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;
54078292
MD
285 nbp = getblk(vp, lblktodoff(fs, lbn), fs->s_blocksize, 0, 0);
286 nbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
984263bc
MD
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) {
54078292
MD
304 error = bread(vp, lblktodoff(fs, lbn),
305 (int)fs->s_blocksize, &nbp);
984263bc
MD
306 if (error) {
307 brelse(nbp);
308 return (error);
309 }
310 } else {
54078292
MD
311 nbp = getblk(vp, lblktodoff(fs, lbn), fs->s_blocksize, 0, 0);
312 nbp->b_bio2.bio_offset = fsbtodoff(fs, nb);
984263bc
MD
313 }
314 *bpp = nbp;
315 return (0);
316}