1 Index: linux-2.4.24/fs/ext3/balloc.c
2 ===================================================================
3 --- linux-2.4.24.orig/fs/ext3/balloc.c 2004-01-10 17:04:42.000000000 +0300
4 +++ linux-2.4.24/fs/ext3/balloc.c 2004-02-05 20:35:11.000000000 +0300
6 * David S. Miller (davem@caip.rutgers.edu), 1995
9 +#include <linux/module.h>
10 #include <linux/config.h>
11 #include <linux/sched.h>
13 @@ -1007,3 +1008,298 @@
20 +#define mbdebug(fmt,a...) printk(fmt, ##a)
22 +#define mbdebug(fmt,a...)
26 + * in alloc_status we track allocation: the best found extent, how many
27 + * extents we've skipped, etc
29 +struct alloc_status {
30 + struct inode *inode;
31 + struct super_block *sb;
32 + int goal; /* goal for allocation */
33 + int target_len; /* goal for len */
34 + int start, len; /* the best found extent */
35 + int num; /* number of extent: to limit searching */
39 + * ext3_test_extent() compares requested extent with existing in as.
40 + * if requested extent is better than that, then replace old one.
41 + * then it tries to understand is new extent suitable or not
42 + * return 1 if caller can complete searching
44 +inline int ext3_test_extent(struct alloc_status *as, int group,
47 + struct ext3_super_block * es = EXT3_SB(as->sb)->s_es;
50 + J_ASSERT(as->target_len >= len);
52 + mbdebug("found extent %d:%d\n", start, len);
53 + tmp = start + group * EXT3_BLOCKS_PER_GROUP(as->sb)
54 + + le32_to_cpu(es->s_first_data_block);
56 + /* account requests in order to limit searching */
62 + /* if hit goal, then searching may complete right now */
63 + if (tmp == as->goal) {
70 + /* if found extent has lenght we need, return it right now */
71 + if (as->target_len == len)
74 + /* first, check is found extent better than we have in as */
75 + if (as->len > len) {
82 + /* FIXME: more checks! */
86 + /* 1) closeness to goal */
88 + /* 2) extent lenght */
89 + /* 3) number of tested extent (we check all found extents) */
95 + * this routine tries to find upto *len free contig. blocks
96 + * return number of found block (+ lenght of extent in *len)
97 + * or -1 if no free blocks at all
99 +int ext3_find_free_extent(struct buffer_head *bh, int goal, int *len, int max)
106 + /* find first free block */
107 + i = ext3_find_next_zero_bit(bh->b_data, max, goal);
109 + /* no free block */
112 + /* check upto len block for ability to be allocated */
113 + while (l < *len && i + l < max) {
114 + if (!ext3_test_allocatable(i + l, bh))
127 + * this routine loops over group, finds free extents and tests them
128 + * for some criterias
129 + * it may return negative value if group can't be loaded, 0 - if
130 + * no good extent can be found, 1 - if good extent found
132 +int ext3_find_extent_in_group(struct alloc_status *as, int group,
133 + unsigned long goal, int len)
135 + int k, i, l, bitmap_nr, found = 0;
136 + struct super_block *sb = as->sb;
137 + int max = EXT3_BLOCKS_PER_GROUP(sb);
138 + struct buffer_head *bh, *bmbh;
139 + struct ext3_group_desc *gdp;
141 + mbdebug("look for %d blocks in group %d starting from %lu\n",
144 + gdp = ext3_get_group_desc(as->sb, group, &bh);
148 + if (le16_to_cpu(gdp->bg_free_blocks_count) == 0)
151 + bitmap_nr = load_block_bitmap(as->sb, group);
155 + bmbh = EXT3_SB(sb)->s_block_bitmap[bitmap_nr];
158 + /* scan from goal to the end */
162 + k = ext3_find_free_extent(bmbh, i, &l, max);
166 + if (ext3_test_extent(as, group, k, l)) {
173 + /* scan from 0 upto goal */
174 + mbdebug("repeat from %lu in %d\n", goal, group);
183 +#define check_in_committed(bh,j) \
184 + J_ASSERT_BH((bh), !ext3_test_bit((j), bh2jh((bh))->b_committed_data))
186 +int ext3_new_blocks(handle_t *handle, struct inode *inode, int *num,
187 + unsigned long goal, int *errp)
189 + struct super_block *sb = inode->i_sb;
190 + int first_group, group, bitmap_nr;
191 + struct buffer_head *bh, *bmbh;
192 + struct ext3_super_block *es;
193 + struct ext3_group_desc *gdp;
194 + struct alloc_status as;
199 + J_ASSERT(num && *num > 0);
201 + if (DQUOT_ALLOC_BLOCK(inode, *num)) {
206 + es = EXT3_SB(inode->i_sb)->s_es;
209 + as.target_len = *num;
218 + first_group = (goal - le32_to_cpu(es->s_first_data_block)) /
219 + EXT3_BLOCKS_PER_GROUP(sb);
220 + goal = (goal - le32_to_cpu(es->s_first_data_block)) %
221 + EXT3_BLOCKS_PER_GROUP(sb);
222 + group = first_group;
225 + err = ext3_find_extent_in_group(&as, group, goal, *num);
231 + /* reset goal for next groups */
234 + /* try next group */
235 + if (++group == EXT3_SB(sb)->s_groups_count)
237 + } while (group != first_group);
244 + /* in the end we've found something, allocate it */
245 + mbdebug("best extent: %u:%u\n", as.start, as.len);
247 + group = (as.start - le32_to_cpu(es->s_first_data_block)) /
248 + EXT3_BLOCKS_PER_GROUP(sb);
249 + bit = (as.start - le32_to_cpu(es->s_first_data_block)) %
250 + EXT3_BLOCKS_PER_GROUP(sb);
251 + gdp = ext3_get_group_desc(sb, group, &bh);
257 + /* mark block(s) used in bitmap ... */
258 + bitmap_nr = load_block_bitmap(sb, group);
259 + if (bitmap_nr < 0) {
263 + bmbh = EXT3_SB(sb)->s_block_bitmap[bitmap_nr];
264 + err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
267 + err = ext3_journal_get_write_access(handle, bmbh);
270 + err = ext3_journal_get_write_access(handle, bh);
273 + for (i = 0; i < as.len; i++) {
274 + J_ASSERT(!ext3_test_bit(bit + i, bmbh->b_data));
275 + if (buffer_jbd(bmbh) && bh2jh(bmbh)->b_committed_data)
276 + check_in_committed(bmbh, bit + i);
277 + set_bit(bit + i, bmbh->b_data);
279 + err = ext3_journal_dirty_metadata(handle, bmbh);
283 + /* ... and correct group descriptor */
284 + gdp->bg_free_blocks_count =
285 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) - as.len);
286 + es->s_free_blocks_count =
287 + cpu_to_le32(le32_to_cpu(es->s_free_blocks_count) - as.len);
288 + err = ext3_journal_dirty_metadata(handle, bmbh);
291 + err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
296 + if (*num != as.len)
297 + DQUOT_FREE_BLOCK(inode, *num - as.len);
310 +EXPORT_SYMBOL(ext3_new_blocks);
312 Index: linux-2.4.24/fs/ext3/file.c
313 ===================================================================
314 --- linux-2.4.24.orig/fs/ext3/file.c 2004-01-31 02:06:18.000000000 +0300
315 +++ linux-2.4.24/fs/ext3/file.c 2004-02-05 20:57:07.000000000 +0300
318 struct inode *inode = file->f_dentry->d_inode;
321 + /* allocate all the space to be written */
322 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
323 + int blocksize = inode->i_sb->s_blocksize;
324 + unsigned long start, end;
326 + start = (unsigned long) *ppos / blocksize;
327 + end = ((unsigned long) *ppos + count + blocksize - 1)
329 + ext3_ext_allocate_nblocks(inode, start, end - start);
332 ret = generic_file_write(file, buf, count, ppos);
334 /* Skip file flushing code if there was an error, or if nothing
335 Index: linux-2.4.24/fs/ext3/Makefile
336 ===================================================================
337 --- linux-2.4.24.orig/fs/ext3/Makefile 2004-02-05 18:44:25.000000000 +0300
338 +++ linux-2.4.24/fs/ext3/Makefile 2004-02-05 20:35:11.000000000 +0300
340 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
341 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
342 xattr_trusted.o extents.o
343 -export-objs += extents.o
344 +export-objs += extents.o balloc.o
348 Index: linux-2.4.24/include/linux/ext3_fs.h
349 ===================================================================
350 --- linux-2.4.24.orig/include/linux/ext3_fs.h 2004-01-30 00:09:37.000000000 +0300
351 +++ linux-2.4.24/include/linux/ext3_fs.h 2004-02-05 20:35:11.000000000 +0300
353 #define ext3_debug(f, a...) do {} while (0)
356 +#define EXT3_MULTIBLOCK_ALLOCATOR 1
359 * Special inodes numbers
362 extern struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
363 unsigned int block_group,
364 struct buffer_head ** bh);
365 +extern int ext3_new_blocks(handle_t*, struct inode*, int*, unsigned long, int*);
368 extern int ext3_check_dir_entry(const char *, struct inode *,