Commit 0b832a4b93932103d73c0c3f35ef1153e288327b
1 parent
325d22df7b
Exists in
master
and in
7 other branches
Revert "ext2/ext3/ext4: add block bitmap validation"
This reverts commit 7c9e69faa28027913ee059c285a5ea8382e24b5d, fixing up conflicts in fs/ext4/balloc.c manually. The cost of doing the bitmap validation on each lookup - even when the bitmap is cached - is absolutely prohibitive. We could, and probably should, do it only when adding the bitmap to the buffer cache. However, right now we are better off just reverting it. Peter Zijlstra measured the cost of this extra validation as a 85% decrease in cached iozone, and while I had a patch that took it down to just 17% by not being _quite_ so stupid in the validation, it was still a big slowdown that could have been avoided by just doing it right. Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Aneesh Kumar <aneesh.kumar@linux.vnet.ibm.com> Cc: Andreas Dilger <adilger@clusterfs.com> Cc: Mingming Cao <cmm@us.ibm.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Showing 3 changed files with 9 additions and 130 deletions Inline Diff
fs/ext2/balloc.c
1 | /* | 1 | /* |
2 | * linux/fs/ext2/balloc.c | 2 | * linux/fs/ext2/balloc.c |
3 | * | 3 | * |
4 | * Copyright (C) 1992, 1993, 1994, 1995 | 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
5 | * Remy Card (card@masi.ibp.fr) | 5 | * Remy Card (card@masi.ibp.fr) |
6 | * Laboratoire MASI - Institut Blaise Pascal | 6 | * Laboratoire MASI - Institut Blaise Pascal |
7 | * Universite Pierre et Marie Curie (Paris VI) | 7 | * Universite Pierre et Marie Curie (Paris VI) |
8 | * | 8 | * |
9 | * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993 | 9 | * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993 |
10 | * Big-endian to little-endian byte-swapping/bitmaps by | 10 | * Big-endian to little-endian byte-swapping/bitmaps by |
11 | * David S. Miller (davem@caip.rutgers.edu), 1995 | 11 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include "ext2.h" | 14 | #include "ext2.h" |
15 | #include <linux/quotaops.h> | 15 | #include <linux/quotaops.h> |
16 | #include <linux/sched.h> | 16 | #include <linux/sched.h> |
17 | #include <linux/buffer_head.h> | 17 | #include <linux/buffer_head.h> |
18 | #include <linux/capability.h> | 18 | #include <linux/capability.h> |
19 | 19 | ||
20 | /* | 20 | /* |
21 | * balloc.c contains the blocks allocation and deallocation routines | 21 | * balloc.c contains the blocks allocation and deallocation routines |
22 | */ | 22 | */ |
23 | 23 | ||
24 | /* | 24 | /* |
25 | * The free blocks are managed by bitmaps. A file system contains several | 25 | * The free blocks are managed by bitmaps. A file system contains several |
26 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap | 26 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap |
27 | * block for inodes, N blocks for the inode table and data blocks. | 27 | * block for inodes, N blocks for the inode table and data blocks. |
28 | * | 28 | * |
29 | * The file system contains group descriptors which are located after the | 29 | * The file system contains group descriptors which are located after the |
30 | * super block. Each descriptor contains the number of the bitmap block and | 30 | * super block. Each descriptor contains the number of the bitmap block and |
31 | * the free blocks count in the block. The descriptors are loaded in memory | 31 | * the free blocks count in the block. The descriptors are loaded in memory |
32 | * when a file system is mounted (see ext2_fill_super). | 32 | * when a file system is mounted (see ext2_fill_super). |
33 | */ | 33 | */ |
34 | 34 | ||
35 | 35 | ||
36 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) | 36 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) |
37 | 37 | ||
38 | struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb, | 38 | struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb, |
39 | unsigned int block_group, | 39 | unsigned int block_group, |
40 | struct buffer_head ** bh) | 40 | struct buffer_head ** bh) |
41 | { | 41 | { |
42 | unsigned long group_desc; | 42 | unsigned long group_desc; |
43 | unsigned long offset; | 43 | unsigned long offset; |
44 | struct ext2_group_desc * desc; | 44 | struct ext2_group_desc * desc; |
45 | struct ext2_sb_info *sbi = EXT2_SB(sb); | 45 | struct ext2_sb_info *sbi = EXT2_SB(sb); |
46 | 46 | ||
47 | if (block_group >= sbi->s_groups_count) { | 47 | if (block_group >= sbi->s_groups_count) { |
48 | ext2_error (sb, "ext2_get_group_desc", | 48 | ext2_error (sb, "ext2_get_group_desc", |
49 | "block_group >= groups_count - " | 49 | "block_group >= groups_count - " |
50 | "block_group = %d, groups_count = %lu", | 50 | "block_group = %d, groups_count = %lu", |
51 | block_group, sbi->s_groups_count); | 51 | block_group, sbi->s_groups_count); |
52 | 52 | ||
53 | return NULL; | 53 | return NULL; |
54 | } | 54 | } |
55 | 55 | ||
56 | group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb); | 56 | group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb); |
57 | offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1); | 57 | offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1); |
58 | if (!sbi->s_group_desc[group_desc]) { | 58 | if (!sbi->s_group_desc[group_desc]) { |
59 | ext2_error (sb, "ext2_get_group_desc", | 59 | ext2_error (sb, "ext2_get_group_desc", |
60 | "Group descriptor not loaded - " | 60 | "Group descriptor not loaded - " |
61 | "block_group = %d, group_desc = %lu, desc = %lu", | 61 | "block_group = %d, group_desc = %lu, desc = %lu", |
62 | block_group, group_desc, offset); | 62 | block_group, group_desc, offset); |
63 | return NULL; | 63 | return NULL; |
64 | } | 64 | } |
65 | 65 | ||
66 | desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data; | 66 | desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data; |
67 | if (bh) | 67 | if (bh) |
68 | *bh = sbi->s_group_desc[group_desc]; | 68 | *bh = sbi->s_group_desc[group_desc]; |
69 | return desc + offset; | 69 | return desc + offset; |
70 | } | 70 | } |
71 | 71 | ||
72 | static inline int | ||
73 | block_in_use(unsigned long block, struct super_block *sb, unsigned char *map) | ||
74 | { | ||
75 | return ext2_test_bit ((block - | ||
76 | le32_to_cpu(EXT2_SB(sb)->s_es->s_first_data_block)) % | ||
77 | EXT2_BLOCKS_PER_GROUP(sb), map); | ||
78 | } | ||
79 | |||
80 | /* | 72 | /* |
81 | * Read the bitmap for a given block_group, reading into the specified | 73 | * Read the bitmap for a given block_group, reading into the specified |
82 | * slot in the superblock's bitmap cache. | 74 | * slot in the superblock's bitmap cache. |
83 | * | 75 | * |
84 | * Return buffer_head on success or NULL in case of failure. | 76 | * Return buffer_head on success or NULL in case of failure. |
85 | */ | 77 | */ |
86 | static struct buffer_head * | 78 | static struct buffer_head * |
87 | read_block_bitmap(struct super_block *sb, unsigned int block_group) | 79 | read_block_bitmap(struct super_block *sb, unsigned int block_group) |
88 | { | 80 | { |
89 | int i; | ||
90 | struct ext2_group_desc * desc; | 81 | struct ext2_group_desc * desc; |
91 | struct buffer_head * bh = NULL; | 82 | struct buffer_head * bh = NULL; |
92 | unsigned int bitmap_blk; | 83 | |
93 | |||
94 | desc = ext2_get_group_desc (sb, block_group, NULL); | 84 | desc = ext2_get_group_desc (sb, block_group, NULL); |
95 | if (!desc) | 85 | if (!desc) |
96 | return NULL; | 86 | goto error_out; |
97 | bitmap_blk = le32_to_cpu(desc->bg_block_bitmap); | 87 | bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap)); |
98 | bh = sb_bread(sb, bitmap_blk); | ||
99 | if (!bh) | 88 | if (!bh) |
100 | ext2_error (sb, __FUNCTION__, | 89 | ext2_error (sb, "read_block_bitmap", |
101 | "Cannot read block bitmap - " | 90 | "Cannot read block bitmap - " |
102 | "block_group = %d, block_bitmap = %u", | 91 | "block_group = %d, block_bitmap = %u", |
103 | block_group, le32_to_cpu(desc->bg_block_bitmap)); | 92 | block_group, le32_to_cpu(desc->bg_block_bitmap)); |
104 | |||
105 | /* check whether block bitmap block number is set */ | ||
106 | if (!block_in_use(bitmap_blk, sb, bh->b_data)) { | ||
107 | /* bad block bitmap */ | ||
108 | goto error_out; | ||
109 | } | ||
110 | /* check whether the inode bitmap block number is set */ | ||
111 | bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap); | ||
112 | if (!block_in_use(bitmap_blk, sb, bh->b_data)) { | ||
113 | /* bad block bitmap */ | ||
114 | goto error_out; | ||
115 | } | ||
116 | /* check whether the inode table block number is set */ | ||
117 | bitmap_blk = le32_to_cpu(desc->bg_inode_table); | ||
118 | for (i = 0; i < EXT2_SB(sb)->s_itb_per_group; i++, bitmap_blk++) { | ||
119 | if (!block_in_use(bitmap_blk, sb, bh->b_data)) { | ||
120 | /* bad block bitmap */ | ||
121 | goto error_out; | ||
122 | } | ||
123 | } | ||
124 | |||
125 | return bh; | ||
126 | |||
127 | error_out: | 93 | error_out: |
128 | brelse(bh); | 94 | return bh; |
129 | ext2_error(sb, __FUNCTION__, | ||
130 | "Invalid block bitmap - " | ||
131 | "block_group = %d, block = %u", | ||
132 | block_group, bitmap_blk); | ||
133 | return NULL; | ||
134 | } | 95 | } |
135 | 96 | ||
136 | static void release_blocks(struct super_block *sb, int count) | 97 | static void release_blocks(struct super_block *sb, int count) |
137 | { | 98 | { |
138 | if (count) { | 99 | if (count) { |
139 | struct ext2_sb_info *sbi = EXT2_SB(sb); | 100 | struct ext2_sb_info *sbi = EXT2_SB(sb); |
140 | 101 | ||
141 | percpu_counter_add(&sbi->s_freeblocks_counter, count); | 102 | percpu_counter_add(&sbi->s_freeblocks_counter, count); |
142 | sb->s_dirt = 1; | 103 | sb->s_dirt = 1; |
143 | } | 104 | } |
144 | } | 105 | } |
145 | 106 | ||
146 | static void group_adjust_blocks(struct super_block *sb, int group_no, | 107 | static void group_adjust_blocks(struct super_block *sb, int group_no, |
147 | struct ext2_group_desc *desc, struct buffer_head *bh, int count) | 108 | struct ext2_group_desc *desc, struct buffer_head *bh, int count) |
148 | { | 109 | { |
149 | if (count) { | 110 | if (count) { |
150 | struct ext2_sb_info *sbi = EXT2_SB(sb); | 111 | struct ext2_sb_info *sbi = EXT2_SB(sb); |
151 | unsigned free_blocks; | 112 | unsigned free_blocks; |
152 | 113 | ||
153 | spin_lock(sb_bgl_lock(sbi, group_no)); | 114 | spin_lock(sb_bgl_lock(sbi, group_no)); |
154 | free_blocks = le16_to_cpu(desc->bg_free_blocks_count); | 115 | free_blocks = le16_to_cpu(desc->bg_free_blocks_count); |
155 | desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count); | 116 | desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count); |
156 | spin_unlock(sb_bgl_lock(sbi, group_no)); | 117 | spin_unlock(sb_bgl_lock(sbi, group_no)); |
157 | sb->s_dirt = 1; | 118 | sb->s_dirt = 1; |
158 | mark_buffer_dirty(bh); | 119 | mark_buffer_dirty(bh); |
159 | } | 120 | } |
160 | } | 121 | } |
161 | 122 | ||
162 | /* | 123 | /* |
163 | * The reservation window structure operations | 124 | * The reservation window structure operations |
164 | * -------------------------------------------- | 125 | * -------------------------------------------- |
165 | * Operations include: | 126 | * Operations include: |
166 | * dump, find, add, remove, is_empty, find_next_reservable_window, etc. | 127 | * dump, find, add, remove, is_empty, find_next_reservable_window, etc. |
167 | * | 128 | * |
168 | * We use a red-black tree to represent per-filesystem reservation | 129 | * We use a red-black tree to represent per-filesystem reservation |
169 | * windows. | 130 | * windows. |
170 | * | 131 | * |
171 | */ | 132 | */ |
172 | 133 | ||
173 | /** | 134 | /** |
174 | * __rsv_window_dump() -- Dump the filesystem block allocation reservation map | 135 | * __rsv_window_dump() -- Dump the filesystem block allocation reservation map |
175 | * @rb_root: root of per-filesystem reservation rb tree | 136 | * @rb_root: root of per-filesystem reservation rb tree |
176 | * @verbose: verbose mode | 137 | * @verbose: verbose mode |
177 | * @fn: function which wishes to dump the reservation map | 138 | * @fn: function which wishes to dump the reservation map |
178 | * | 139 | * |
179 | * If verbose is turned on, it will print the whole block reservation | 140 | * If verbose is turned on, it will print the whole block reservation |
180 | * windows(start, end). Otherwise, it will only print out the "bad" windows, | 141 | * windows(start, end). Otherwise, it will only print out the "bad" windows, |
181 | * those windows that overlap with their immediate neighbors. | 142 | * those windows that overlap with their immediate neighbors. |
182 | */ | 143 | */ |
183 | #if 1 | 144 | #if 1 |
184 | static void __rsv_window_dump(struct rb_root *root, int verbose, | 145 | static void __rsv_window_dump(struct rb_root *root, int verbose, |
185 | const char *fn) | 146 | const char *fn) |
186 | { | 147 | { |
187 | struct rb_node *n; | 148 | struct rb_node *n; |
188 | struct ext2_reserve_window_node *rsv, *prev; | 149 | struct ext2_reserve_window_node *rsv, *prev; |
189 | int bad; | 150 | int bad; |
190 | 151 | ||
191 | restart: | 152 | restart: |
192 | n = rb_first(root); | 153 | n = rb_first(root); |
193 | bad = 0; | 154 | bad = 0; |
194 | prev = NULL; | 155 | prev = NULL; |
195 | 156 | ||
196 | printk("Block Allocation Reservation Windows Map (%s):\n", fn); | 157 | printk("Block Allocation Reservation Windows Map (%s):\n", fn); |
197 | while (n) { | 158 | while (n) { |
198 | rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node); | 159 | rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node); |
199 | if (verbose) | 160 | if (verbose) |
200 | printk("reservation window 0x%p " | 161 | printk("reservation window 0x%p " |
201 | "start: %lu, end: %lu\n", | 162 | "start: %lu, end: %lu\n", |
202 | rsv, rsv->rsv_start, rsv->rsv_end); | 163 | rsv, rsv->rsv_start, rsv->rsv_end); |
203 | if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) { | 164 | if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) { |
204 | printk("Bad reservation %p (start >= end)\n", | 165 | printk("Bad reservation %p (start >= end)\n", |
205 | rsv); | 166 | rsv); |
206 | bad = 1; | 167 | bad = 1; |
207 | } | 168 | } |
208 | if (prev && prev->rsv_end >= rsv->rsv_start) { | 169 | if (prev && prev->rsv_end >= rsv->rsv_start) { |
209 | printk("Bad reservation %p (prev->end >= start)\n", | 170 | printk("Bad reservation %p (prev->end >= start)\n", |
210 | rsv); | 171 | rsv); |
211 | bad = 1; | 172 | bad = 1; |
212 | } | 173 | } |
213 | if (bad) { | 174 | if (bad) { |
214 | if (!verbose) { | 175 | if (!verbose) { |
215 | printk("Restarting reservation walk in verbose mode\n"); | 176 | printk("Restarting reservation walk in verbose mode\n"); |
216 | verbose = 1; | 177 | verbose = 1; |
217 | goto restart; | 178 | goto restart; |
218 | } | 179 | } |
219 | } | 180 | } |
220 | n = rb_next(n); | 181 | n = rb_next(n); |
221 | prev = rsv; | 182 | prev = rsv; |
222 | } | 183 | } |
223 | printk("Window map complete.\n"); | 184 | printk("Window map complete.\n"); |
224 | if (bad) | 185 | if (bad) |
225 | BUG(); | 186 | BUG(); |
226 | } | 187 | } |
227 | #define rsv_window_dump(root, verbose) \ | 188 | #define rsv_window_dump(root, verbose) \ |
228 | __rsv_window_dump((root), (verbose), __FUNCTION__) | 189 | __rsv_window_dump((root), (verbose), __FUNCTION__) |
229 | #else | 190 | #else |
230 | #define rsv_window_dump(root, verbose) do {} while (0) | 191 | #define rsv_window_dump(root, verbose) do {} while (0) |
231 | #endif | 192 | #endif |
232 | 193 | ||
233 | /** | 194 | /** |
234 | * goal_in_my_reservation() | 195 | * goal_in_my_reservation() |
235 | * @rsv: inode's reservation window | 196 | * @rsv: inode's reservation window |
236 | * @grp_goal: given goal block relative to the allocation block group | 197 | * @grp_goal: given goal block relative to the allocation block group |
237 | * @group: the current allocation block group | 198 | * @group: the current allocation block group |
238 | * @sb: filesystem super block | 199 | * @sb: filesystem super block |
239 | * | 200 | * |
240 | * Test if the given goal block (group relative) is within the file's | 201 | * Test if the given goal block (group relative) is within the file's |
241 | * own block reservation window range. | 202 | * own block reservation window range. |
242 | * | 203 | * |
243 | * If the reservation window is outside the goal allocation group, return 0; | 204 | * If the reservation window is outside the goal allocation group, return 0; |
244 | * grp_goal (given goal block) could be -1, which means no specific | 205 | * grp_goal (given goal block) could be -1, which means no specific |
245 | * goal block. In this case, always return 1. | 206 | * goal block. In this case, always return 1. |
246 | * If the goal block is within the reservation window, return 1; | 207 | * If the goal block is within the reservation window, return 1; |
247 | * otherwise, return 0; | 208 | * otherwise, return 0; |
248 | */ | 209 | */ |
249 | static int | 210 | static int |
250 | goal_in_my_reservation(struct ext2_reserve_window *rsv, ext2_grpblk_t grp_goal, | 211 | goal_in_my_reservation(struct ext2_reserve_window *rsv, ext2_grpblk_t grp_goal, |
251 | unsigned int group, struct super_block * sb) | 212 | unsigned int group, struct super_block * sb) |
252 | { | 213 | { |
253 | ext2_fsblk_t group_first_block, group_last_block; | 214 | ext2_fsblk_t group_first_block, group_last_block; |
254 | 215 | ||
255 | group_first_block = ext2_group_first_block_no(sb, group); | 216 | group_first_block = ext2_group_first_block_no(sb, group); |
256 | group_last_block = group_first_block + EXT2_BLOCKS_PER_GROUP(sb) - 1; | 217 | group_last_block = group_first_block + EXT2_BLOCKS_PER_GROUP(sb) - 1; |
257 | 218 | ||
258 | if ((rsv->_rsv_start > group_last_block) || | 219 | if ((rsv->_rsv_start > group_last_block) || |
259 | (rsv->_rsv_end < group_first_block)) | 220 | (rsv->_rsv_end < group_first_block)) |
260 | return 0; | 221 | return 0; |
261 | if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start) | 222 | if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start) |
262 | || (grp_goal + group_first_block > rsv->_rsv_end))) | 223 | || (grp_goal + group_first_block > rsv->_rsv_end))) |
263 | return 0; | 224 | return 0; |
264 | return 1; | 225 | return 1; |
265 | } | 226 | } |
266 | 227 | ||
267 | /** | 228 | /** |
268 | * search_reserve_window() | 229 | * search_reserve_window() |
269 | * @rb_root: root of reservation tree | 230 | * @rb_root: root of reservation tree |
270 | * @goal: target allocation block | 231 | * @goal: target allocation block |
271 | * | 232 | * |
272 | * Find the reserved window which includes the goal, or the previous one | 233 | * Find the reserved window which includes the goal, or the previous one |
273 | * if the goal is not in any window. | 234 | * if the goal is not in any window. |
274 | * Returns NULL if there are no windows or if all windows start after the goal. | 235 | * Returns NULL if there are no windows or if all windows start after the goal. |
275 | */ | 236 | */ |
276 | static struct ext2_reserve_window_node * | 237 | static struct ext2_reserve_window_node * |
277 | search_reserve_window(struct rb_root *root, ext2_fsblk_t goal) | 238 | search_reserve_window(struct rb_root *root, ext2_fsblk_t goal) |
278 | { | 239 | { |
279 | struct rb_node *n = root->rb_node; | 240 | struct rb_node *n = root->rb_node; |
280 | struct ext2_reserve_window_node *rsv; | 241 | struct ext2_reserve_window_node *rsv; |
281 | 242 | ||
282 | if (!n) | 243 | if (!n) |
283 | return NULL; | 244 | return NULL; |
284 | 245 | ||
285 | do { | 246 | do { |
286 | rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node); | 247 | rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node); |
287 | 248 | ||
288 | if (goal < rsv->rsv_start) | 249 | if (goal < rsv->rsv_start) |
289 | n = n->rb_left; | 250 | n = n->rb_left; |
290 | else if (goal > rsv->rsv_end) | 251 | else if (goal > rsv->rsv_end) |
291 | n = n->rb_right; | 252 | n = n->rb_right; |
292 | else | 253 | else |
293 | return rsv; | 254 | return rsv; |
294 | } while (n); | 255 | } while (n); |
295 | /* | 256 | /* |
296 | * We've fallen off the end of the tree: the goal wasn't inside | 257 | * We've fallen off the end of the tree: the goal wasn't inside |
297 | * any particular node. OK, the previous node must be to one | 258 | * any particular node. OK, the previous node must be to one |
298 | * side of the interval containing the goal. If it's the RHS, | 259 | * side of the interval containing the goal. If it's the RHS, |
299 | * we need to back up one. | 260 | * we need to back up one. |
300 | */ | 261 | */ |
301 | if (rsv->rsv_start > goal) { | 262 | if (rsv->rsv_start > goal) { |
302 | n = rb_prev(&rsv->rsv_node); | 263 | n = rb_prev(&rsv->rsv_node); |
303 | rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node); | 264 | rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node); |
304 | } | 265 | } |
305 | return rsv; | 266 | return rsv; |
306 | } | 267 | } |
307 | 268 | ||
308 | /* | 269 | /* |
309 | * ext2_rsv_window_add() -- Insert a window to the block reservation rb tree. | 270 | * ext2_rsv_window_add() -- Insert a window to the block reservation rb tree. |
310 | * @sb: super block | 271 | * @sb: super block |
311 | * @rsv: reservation window to add | 272 | * @rsv: reservation window to add |
312 | * | 273 | * |
313 | * Must be called with rsv_lock held. | 274 | * Must be called with rsv_lock held. |
314 | */ | 275 | */ |
315 | void ext2_rsv_window_add(struct super_block *sb, | 276 | void ext2_rsv_window_add(struct super_block *sb, |
316 | struct ext2_reserve_window_node *rsv) | 277 | struct ext2_reserve_window_node *rsv) |
317 | { | 278 | { |
318 | struct rb_root *root = &EXT2_SB(sb)->s_rsv_window_root; | 279 | struct rb_root *root = &EXT2_SB(sb)->s_rsv_window_root; |
319 | struct rb_node *node = &rsv->rsv_node; | 280 | struct rb_node *node = &rsv->rsv_node; |
320 | ext2_fsblk_t start = rsv->rsv_start; | 281 | ext2_fsblk_t start = rsv->rsv_start; |
321 | 282 | ||
322 | struct rb_node ** p = &root->rb_node; | 283 | struct rb_node ** p = &root->rb_node; |
323 | struct rb_node * parent = NULL; | 284 | struct rb_node * parent = NULL; |
324 | struct ext2_reserve_window_node *this; | 285 | struct ext2_reserve_window_node *this; |
325 | 286 | ||
326 | while (*p) | 287 | while (*p) |
327 | { | 288 | { |
328 | parent = *p; | 289 | parent = *p; |
329 | this = rb_entry(parent, struct ext2_reserve_window_node, rsv_node); | 290 | this = rb_entry(parent, struct ext2_reserve_window_node, rsv_node); |
330 | 291 | ||
331 | if (start < this->rsv_start) | 292 | if (start < this->rsv_start) |
332 | p = &(*p)->rb_left; | 293 | p = &(*p)->rb_left; |
333 | else if (start > this->rsv_end) | 294 | else if (start > this->rsv_end) |
334 | p = &(*p)->rb_right; | 295 | p = &(*p)->rb_right; |
335 | else { | 296 | else { |
336 | rsv_window_dump(root, 1); | 297 | rsv_window_dump(root, 1); |
337 | BUG(); | 298 | BUG(); |
338 | } | 299 | } |
339 | } | 300 | } |
340 | 301 | ||
341 | rb_link_node(node, parent, p); | 302 | rb_link_node(node, parent, p); |
342 | rb_insert_color(node, root); | 303 | rb_insert_color(node, root); |
343 | } | 304 | } |
344 | 305 | ||
345 | /** | 306 | /** |
346 | * rsv_window_remove() -- unlink a window from the reservation rb tree | 307 | * rsv_window_remove() -- unlink a window from the reservation rb tree |
347 | * @sb: super block | 308 | * @sb: super block |
348 | * @rsv: reservation window to remove | 309 | * @rsv: reservation window to remove |
349 | * | 310 | * |
350 | * Mark the block reservation window as not allocated, and unlink it | 311 | * Mark the block reservation window as not allocated, and unlink it |
351 | * from the filesystem reservation window rb tree. Must be called with | 312 | * from the filesystem reservation window rb tree. Must be called with |
352 | * rsv_lock held. | 313 | * rsv_lock held. |
353 | */ | 314 | */ |
354 | static void rsv_window_remove(struct super_block *sb, | 315 | static void rsv_window_remove(struct super_block *sb, |
355 | struct ext2_reserve_window_node *rsv) | 316 | struct ext2_reserve_window_node *rsv) |
356 | { | 317 | { |
357 | rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; | 318 | rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; |
358 | rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; | 319 | rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; |
359 | rsv->rsv_alloc_hit = 0; | 320 | rsv->rsv_alloc_hit = 0; |
360 | rb_erase(&rsv->rsv_node, &EXT2_SB(sb)->s_rsv_window_root); | 321 | rb_erase(&rsv->rsv_node, &EXT2_SB(sb)->s_rsv_window_root); |
361 | } | 322 | } |
362 | 323 | ||
363 | /* | 324 | /* |
364 | * rsv_is_empty() -- Check if the reservation window is allocated. | 325 | * rsv_is_empty() -- Check if the reservation window is allocated. |
365 | * @rsv: given reservation window to check | 326 | * @rsv: given reservation window to check |
366 | * | 327 | * |
367 | * returns 1 if the end block is EXT2_RESERVE_WINDOW_NOT_ALLOCATED. | 328 | * returns 1 if the end block is EXT2_RESERVE_WINDOW_NOT_ALLOCATED. |
368 | */ | 329 | */ |
369 | static inline int rsv_is_empty(struct ext2_reserve_window *rsv) | 330 | static inline int rsv_is_empty(struct ext2_reserve_window *rsv) |
370 | { | 331 | { |
371 | /* a valid reservation end block could not be 0 */ | 332 | /* a valid reservation end block could not be 0 */ |
372 | return (rsv->_rsv_end == EXT2_RESERVE_WINDOW_NOT_ALLOCATED); | 333 | return (rsv->_rsv_end == EXT2_RESERVE_WINDOW_NOT_ALLOCATED); |
373 | } | 334 | } |
374 | 335 | ||
375 | /** | 336 | /** |
376 | * ext2_init_block_alloc_info() | 337 | * ext2_init_block_alloc_info() |
377 | * @inode: file inode structure | 338 | * @inode: file inode structure |
378 | * | 339 | * |
379 | * Allocate and initialize the reservation window structure, and | 340 | * Allocate and initialize the reservation window structure, and |
380 | * link the window to the ext2 inode structure at last | 341 | * link the window to the ext2 inode structure at last |
381 | * | 342 | * |
382 | * The reservation window structure is only dynamically allocated | 343 | * The reservation window structure is only dynamically allocated |
383 | * and linked to ext2 inode the first time the open file | 344 | * and linked to ext2 inode the first time the open file |
384 | * needs a new block. So, before every ext2_new_block(s) call, for | 345 | * needs a new block. So, before every ext2_new_block(s) call, for |
385 | * regular files, we should check whether the reservation window | 346 | * regular files, we should check whether the reservation window |
386 | * structure exists or not. In the latter case, this function is called. | 347 | * structure exists or not. In the latter case, this function is called. |
387 | * Fail to do so will result in block reservation being turned off for that | 348 | * Fail to do so will result in block reservation being turned off for that |
388 | * open file. | 349 | * open file. |
389 | * | 350 | * |
390 | * This function is called from ext2_get_blocks_handle(), also called | 351 | * This function is called from ext2_get_blocks_handle(), also called |
391 | * when setting the reservation window size through ioctl before the file | 352 | * when setting the reservation window size through ioctl before the file |
392 | * is open for write (needs block allocation). | 353 | * is open for write (needs block allocation). |
393 | * | 354 | * |
394 | * Needs truncate_mutex protection prior to calling this function. | 355 | * Needs truncate_mutex protection prior to calling this function. |
395 | */ | 356 | */ |
396 | void ext2_init_block_alloc_info(struct inode *inode) | 357 | void ext2_init_block_alloc_info(struct inode *inode) |
397 | { | 358 | { |
398 | struct ext2_inode_info *ei = EXT2_I(inode); | 359 | struct ext2_inode_info *ei = EXT2_I(inode); |
399 | struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info; | 360 | struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info; |
400 | struct super_block *sb = inode->i_sb; | 361 | struct super_block *sb = inode->i_sb; |
401 | 362 | ||
402 | block_i = kmalloc(sizeof(*block_i), GFP_NOFS); | 363 | block_i = kmalloc(sizeof(*block_i), GFP_NOFS); |
403 | if (block_i) { | 364 | if (block_i) { |
404 | struct ext2_reserve_window_node *rsv = &block_i->rsv_window_node; | 365 | struct ext2_reserve_window_node *rsv = &block_i->rsv_window_node; |
405 | 366 | ||
406 | rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; | 367 | rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; |
407 | rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; | 368 | rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; |
408 | 369 | ||
409 | /* | 370 | /* |
410 | * if filesystem is mounted with NORESERVATION, the goal | 371 | * if filesystem is mounted with NORESERVATION, the goal |
411 | * reservation window size is set to zero to indicate | 372 | * reservation window size is set to zero to indicate |
412 | * block reservation is off | 373 | * block reservation is off |
413 | */ | 374 | */ |
414 | if (!test_opt(sb, RESERVATION)) | 375 | if (!test_opt(sb, RESERVATION)) |
415 | rsv->rsv_goal_size = 0; | 376 | rsv->rsv_goal_size = 0; |
416 | else | 377 | else |
417 | rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS; | 378 | rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS; |
418 | rsv->rsv_alloc_hit = 0; | 379 | rsv->rsv_alloc_hit = 0; |
419 | block_i->last_alloc_logical_block = 0; | 380 | block_i->last_alloc_logical_block = 0; |
420 | block_i->last_alloc_physical_block = 0; | 381 | block_i->last_alloc_physical_block = 0; |
421 | } | 382 | } |
422 | ei->i_block_alloc_info = block_i; | 383 | ei->i_block_alloc_info = block_i; |
423 | } | 384 | } |
424 | 385 | ||
425 | /** | 386 | /** |
426 | * ext2_discard_reservation() | 387 | * ext2_discard_reservation() |
427 | * @inode: inode | 388 | * @inode: inode |
428 | * | 389 | * |
429 | * Discard(free) block reservation window on last file close, or truncate | 390 | * Discard(free) block reservation window on last file close, or truncate |
430 | * or at last iput(). | 391 | * or at last iput(). |
431 | * | 392 | * |
432 | * It is being called in three cases: | 393 | * It is being called in three cases: |
433 | * ext2_release_file(): last writer closes the file | 394 | * ext2_release_file(): last writer closes the file |
434 | * ext2_clear_inode(): last iput(), when nobody links to this file. | 395 | * ext2_clear_inode(): last iput(), when nobody links to this file. |
435 | * ext2_truncate(): when the block indirect map is about to change. | 396 | * ext2_truncate(): when the block indirect map is about to change. |
436 | */ | 397 | */ |
437 | void ext2_discard_reservation(struct inode *inode) | 398 | void ext2_discard_reservation(struct inode *inode) |
438 | { | 399 | { |
439 | struct ext2_inode_info *ei = EXT2_I(inode); | 400 | struct ext2_inode_info *ei = EXT2_I(inode); |
440 | struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info; | 401 | struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info; |
441 | struct ext2_reserve_window_node *rsv; | 402 | struct ext2_reserve_window_node *rsv; |
442 | spinlock_t *rsv_lock = &EXT2_SB(inode->i_sb)->s_rsv_window_lock; | 403 | spinlock_t *rsv_lock = &EXT2_SB(inode->i_sb)->s_rsv_window_lock; |
443 | 404 | ||
444 | if (!block_i) | 405 | if (!block_i) |
445 | return; | 406 | return; |
446 | 407 | ||
447 | rsv = &block_i->rsv_window_node; | 408 | rsv = &block_i->rsv_window_node; |
448 | if (!rsv_is_empty(&rsv->rsv_window)) { | 409 | if (!rsv_is_empty(&rsv->rsv_window)) { |
449 | spin_lock(rsv_lock); | 410 | spin_lock(rsv_lock); |
450 | if (!rsv_is_empty(&rsv->rsv_window)) | 411 | if (!rsv_is_empty(&rsv->rsv_window)) |
451 | rsv_window_remove(inode->i_sb, rsv); | 412 | rsv_window_remove(inode->i_sb, rsv); |
452 | spin_unlock(rsv_lock); | 413 | spin_unlock(rsv_lock); |
453 | } | 414 | } |
454 | } | 415 | } |
455 | 416 | ||
456 | /** | 417 | /** |
457 | * ext2_free_blocks_sb() -- Free given blocks and update quota and i_blocks | 418 | * ext2_free_blocks_sb() -- Free given blocks and update quota and i_blocks |
458 | * @inode: inode | 419 | * @inode: inode |
459 | * @block: start physcial block to free | 420 | * @block: start physcial block to free |
460 | * @count: number of blocks to free | 421 | * @count: number of blocks to free |
461 | */ | 422 | */ |
462 | void ext2_free_blocks (struct inode * inode, unsigned long block, | 423 | void ext2_free_blocks (struct inode * inode, unsigned long block, |
463 | unsigned long count) | 424 | unsigned long count) |
464 | { | 425 | { |
465 | struct buffer_head *bitmap_bh = NULL; | 426 | struct buffer_head *bitmap_bh = NULL; |
466 | struct buffer_head * bh2; | 427 | struct buffer_head * bh2; |
467 | unsigned long block_group; | 428 | unsigned long block_group; |
468 | unsigned long bit; | 429 | unsigned long bit; |
469 | unsigned long i; | 430 | unsigned long i; |
470 | unsigned long overflow; | 431 | unsigned long overflow; |
471 | struct super_block * sb = inode->i_sb; | 432 | struct super_block * sb = inode->i_sb; |
472 | struct ext2_sb_info * sbi = EXT2_SB(sb); | 433 | struct ext2_sb_info * sbi = EXT2_SB(sb); |
473 | struct ext2_group_desc * desc; | 434 | struct ext2_group_desc * desc; |
474 | struct ext2_super_block * es = sbi->s_es; | 435 | struct ext2_super_block * es = sbi->s_es; |
475 | unsigned freed = 0, group_freed; | 436 | unsigned freed = 0, group_freed; |
476 | 437 | ||
477 | if (block < le32_to_cpu(es->s_first_data_block) || | 438 | if (block < le32_to_cpu(es->s_first_data_block) || |
478 | block + count < block || | 439 | block + count < block || |
479 | block + count > le32_to_cpu(es->s_blocks_count)) { | 440 | block + count > le32_to_cpu(es->s_blocks_count)) { |
480 | ext2_error (sb, "ext2_free_blocks", | 441 | ext2_error (sb, "ext2_free_blocks", |
481 | "Freeing blocks not in datazone - " | 442 | "Freeing blocks not in datazone - " |
482 | "block = %lu, count = %lu", block, count); | 443 | "block = %lu, count = %lu", block, count); |
483 | goto error_return; | 444 | goto error_return; |
484 | } | 445 | } |
485 | 446 | ||
486 | ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1); | 447 | ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1); |
487 | 448 | ||
488 | do_more: | 449 | do_more: |
489 | overflow = 0; | 450 | overflow = 0; |
490 | block_group = (block - le32_to_cpu(es->s_first_data_block)) / | 451 | block_group = (block - le32_to_cpu(es->s_first_data_block)) / |
491 | EXT2_BLOCKS_PER_GROUP(sb); | 452 | EXT2_BLOCKS_PER_GROUP(sb); |
492 | bit = (block - le32_to_cpu(es->s_first_data_block)) % | 453 | bit = (block - le32_to_cpu(es->s_first_data_block)) % |
493 | EXT2_BLOCKS_PER_GROUP(sb); | 454 | EXT2_BLOCKS_PER_GROUP(sb); |
494 | /* | 455 | /* |
495 | * Check to see if we are freeing blocks across a group | 456 | * Check to see if we are freeing blocks across a group |
496 | * boundary. | 457 | * boundary. |
497 | */ | 458 | */ |
498 | if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) { | 459 | if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) { |
499 | overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb); | 460 | overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb); |
500 | count -= overflow; | 461 | count -= overflow; |
501 | } | 462 | } |
502 | brelse(bitmap_bh); | 463 | brelse(bitmap_bh); |
503 | bitmap_bh = read_block_bitmap(sb, block_group); | 464 | bitmap_bh = read_block_bitmap(sb, block_group); |
504 | if (!bitmap_bh) | 465 | if (!bitmap_bh) |
505 | goto error_return; | 466 | goto error_return; |
506 | 467 | ||
507 | desc = ext2_get_group_desc (sb, block_group, &bh2); | 468 | desc = ext2_get_group_desc (sb, block_group, &bh2); |
508 | if (!desc) | 469 | if (!desc) |
509 | goto error_return; | 470 | goto error_return; |
510 | 471 | ||
511 | if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) || | 472 | if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) || |
512 | in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) || | 473 | in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) || |
513 | in_range (block, le32_to_cpu(desc->bg_inode_table), | 474 | in_range (block, le32_to_cpu(desc->bg_inode_table), |
514 | sbi->s_itb_per_group) || | 475 | sbi->s_itb_per_group) || |
515 | in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table), | 476 | in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table), |
516 | sbi->s_itb_per_group)) | 477 | sbi->s_itb_per_group)) |
517 | ext2_error (sb, "ext2_free_blocks", | 478 | ext2_error (sb, "ext2_free_blocks", |
518 | "Freeing blocks in system zones - " | 479 | "Freeing blocks in system zones - " |
519 | "Block = %lu, count = %lu", | 480 | "Block = %lu, count = %lu", |
520 | block, count); | 481 | block, count); |
521 | 482 | ||
522 | for (i = 0, group_freed = 0; i < count; i++) { | 483 | for (i = 0, group_freed = 0; i < count; i++) { |
523 | if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group), | 484 | if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group), |
524 | bit + i, bitmap_bh->b_data)) { | 485 | bit + i, bitmap_bh->b_data)) { |
525 | ext2_error(sb, __FUNCTION__, | 486 | ext2_error(sb, __FUNCTION__, |
526 | "bit already cleared for block %lu", block + i); | 487 | "bit already cleared for block %lu", block + i); |
527 | } else { | 488 | } else { |
528 | group_freed++; | 489 | group_freed++; |
529 | } | 490 | } |
530 | } | 491 | } |
531 | 492 | ||
532 | mark_buffer_dirty(bitmap_bh); | 493 | mark_buffer_dirty(bitmap_bh); |
533 | if (sb->s_flags & MS_SYNCHRONOUS) | 494 | if (sb->s_flags & MS_SYNCHRONOUS) |
534 | sync_dirty_buffer(bitmap_bh); | 495 | sync_dirty_buffer(bitmap_bh); |
535 | 496 | ||
536 | group_adjust_blocks(sb, block_group, desc, bh2, group_freed); | 497 | group_adjust_blocks(sb, block_group, desc, bh2, group_freed); |
537 | freed += group_freed; | 498 | freed += group_freed; |
538 | 499 | ||
539 | if (overflow) { | 500 | if (overflow) { |
540 | block += count; | 501 | block += count; |
541 | count = overflow; | 502 | count = overflow; |
542 | goto do_more; | 503 | goto do_more; |
543 | } | 504 | } |
544 | error_return: | 505 | error_return: |
545 | brelse(bitmap_bh); | 506 | brelse(bitmap_bh); |
546 | release_blocks(sb, freed); | 507 | release_blocks(sb, freed); |
547 | DQUOT_FREE_BLOCK(inode, freed); | 508 | DQUOT_FREE_BLOCK(inode, freed); |
548 | } | 509 | } |
549 | 510 | ||
550 | /** | 511 | /** |
551 | * bitmap_search_next_usable_block() | 512 | * bitmap_search_next_usable_block() |
552 | * @start: the starting block (group relative) of the search | 513 | * @start: the starting block (group relative) of the search |
553 | * @bh: bufferhead contains the block group bitmap | 514 | * @bh: bufferhead contains the block group bitmap |
554 | * @maxblocks: the ending block (group relative) of the reservation | 515 | * @maxblocks: the ending block (group relative) of the reservation |
555 | * | 516 | * |
556 | * The bitmap search --- search forward through the actual bitmap on disk until | 517 | * The bitmap search --- search forward through the actual bitmap on disk until |
557 | * we find a bit free. | 518 | * we find a bit free. |
558 | */ | 519 | */ |
559 | static ext2_grpblk_t | 520 | static ext2_grpblk_t |
560 | bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh, | 521 | bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh, |
561 | ext2_grpblk_t maxblocks) | 522 | ext2_grpblk_t maxblocks) |
562 | { | 523 | { |
563 | ext2_grpblk_t next; | 524 | ext2_grpblk_t next; |
564 | 525 | ||
565 | next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start); | 526 | next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start); |
566 | if (next >= maxblocks) | 527 | if (next >= maxblocks) |
567 | return -1; | 528 | return -1; |
568 | return next; | 529 | return next; |
569 | } | 530 | } |
570 | 531 | ||
571 | /** | 532 | /** |
572 | * find_next_usable_block() | 533 | * find_next_usable_block() |
573 | * @start: the starting block (group relative) to find next | 534 | * @start: the starting block (group relative) to find next |
574 | * allocatable block in bitmap. | 535 | * allocatable block in bitmap. |
575 | * @bh: bufferhead contains the block group bitmap | 536 | * @bh: bufferhead contains the block group bitmap |
576 | * @maxblocks: the ending block (group relative) for the search | 537 | * @maxblocks: the ending block (group relative) for the search |
577 | * | 538 | * |
578 | * Find an allocatable block in a bitmap. We perform the "most | 539 | * Find an allocatable block in a bitmap. We perform the "most |
579 | * appropriate allocation" algorithm of looking for a free block near | 540 | * appropriate allocation" algorithm of looking for a free block near |
580 | * the initial goal; then for a free byte somewhere in the bitmap; | 541 | * the initial goal; then for a free byte somewhere in the bitmap; |
581 | * then for any free bit in the bitmap. | 542 | * then for any free bit in the bitmap. |
582 | */ | 543 | */ |
583 | static ext2_grpblk_t | 544 | static ext2_grpblk_t |
584 | find_next_usable_block(int start, struct buffer_head *bh, int maxblocks) | 545 | find_next_usable_block(int start, struct buffer_head *bh, int maxblocks) |
585 | { | 546 | { |
586 | ext2_grpblk_t here, next; | 547 | ext2_grpblk_t here, next; |
587 | char *p, *r; | 548 | char *p, *r; |
588 | 549 | ||
589 | if (start > 0) { | 550 | if (start > 0) { |
590 | /* | 551 | /* |
591 | * The goal was occupied; search forward for a free | 552 | * The goal was occupied; search forward for a free |
592 | * block within the next XX blocks. | 553 | * block within the next XX blocks. |
593 | * | 554 | * |
594 | * end_goal is more or less random, but it has to be | 555 | * end_goal is more or less random, but it has to be |
595 | * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the | 556 | * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the |
596 | * next 64-bit boundary is simple.. | 557 | * next 64-bit boundary is simple.. |
597 | */ | 558 | */ |
598 | ext2_grpblk_t end_goal = (start + 63) & ~63; | 559 | ext2_grpblk_t end_goal = (start + 63) & ~63; |
599 | if (end_goal > maxblocks) | 560 | if (end_goal > maxblocks) |
600 | end_goal = maxblocks; | 561 | end_goal = maxblocks; |
601 | here = ext2_find_next_zero_bit(bh->b_data, end_goal, start); | 562 | here = ext2_find_next_zero_bit(bh->b_data, end_goal, start); |
602 | if (here < end_goal) | 563 | if (here < end_goal) |
603 | return here; | 564 | return here; |
604 | ext2_debug("Bit not found near goal\n"); | 565 | ext2_debug("Bit not found near goal\n"); |
605 | } | 566 | } |
606 | 567 | ||
607 | here = start; | 568 | here = start; |
608 | if (here < 0) | 569 | if (here < 0) |
609 | here = 0; | 570 | here = 0; |
610 | 571 | ||
611 | p = ((char *)bh->b_data) + (here >> 3); | 572 | p = ((char *)bh->b_data) + (here >> 3); |
612 | r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3)); | 573 | r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3)); |
613 | next = (r - ((char *)bh->b_data)) << 3; | 574 | next = (r - ((char *)bh->b_data)) << 3; |
614 | 575 | ||
615 | if (next < maxblocks && next >= here) | 576 | if (next < maxblocks && next >= here) |
616 | return next; | 577 | return next; |
617 | 578 | ||
618 | here = bitmap_search_next_usable_block(here, bh, maxblocks); | 579 | here = bitmap_search_next_usable_block(here, bh, maxblocks); |
619 | return here; | 580 | return here; |
620 | } | 581 | } |
621 | 582 | ||
622 | /* | 583 | /* |
623 | * ext2_try_to_allocate() | 584 | * ext2_try_to_allocate() |
624 | * @sb: superblock | 585 | * @sb: superblock |
625 | * @handle: handle to this transaction | 586 | * @handle: handle to this transaction |
626 | * @group: given allocation block group | 587 | * @group: given allocation block group |
627 | * @bitmap_bh: bufferhead holds the block bitmap | 588 | * @bitmap_bh: bufferhead holds the block bitmap |
628 | * @grp_goal: given target block within the group | 589 | * @grp_goal: given target block within the group |
629 | * @count: target number of blocks to allocate | 590 | * @count: target number of blocks to allocate |
630 | * @my_rsv: reservation window | 591 | * @my_rsv: reservation window |
631 | * | 592 | * |
632 | * Attempt to allocate blocks within a give range. Set the range of allocation | 593 | * Attempt to allocate blocks within a give range. Set the range of allocation |
633 | * first, then find the first free bit(s) from the bitmap (within the range), | 594 | * first, then find the first free bit(s) from the bitmap (within the range), |
634 | * and at last, allocate the blocks by claiming the found free bit as allocated. | 595 | * and at last, allocate the blocks by claiming the found free bit as allocated. |
635 | * | 596 | * |
636 | * To set the range of this allocation: | 597 | * To set the range of this allocation: |
637 | * if there is a reservation window, only try to allocate block(s) | 598 | * if there is a reservation window, only try to allocate block(s) |
638 | * from the file's own reservation window; | 599 | * from the file's own reservation window; |
639 | * Otherwise, the allocation range starts from the give goal block, | 600 | * Otherwise, the allocation range starts from the give goal block, |
640 | * ends at the block group's last block. | 601 | * ends at the block group's last block. |
641 | * | 602 | * |
642 | * If we failed to allocate the desired block then we may end up crossing to a | 603 | * If we failed to allocate the desired block then we may end up crossing to a |
643 | * new bitmap. | 604 | * new bitmap. |
644 | */ | 605 | */ |
645 | static int | 606 | static int |
646 | ext2_try_to_allocate(struct super_block *sb, int group, | 607 | ext2_try_to_allocate(struct super_block *sb, int group, |
647 | struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal, | 608 | struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal, |
648 | unsigned long *count, | 609 | unsigned long *count, |
649 | struct ext2_reserve_window *my_rsv) | 610 | struct ext2_reserve_window *my_rsv) |
650 | { | 611 | { |
651 | ext2_fsblk_t group_first_block; | 612 | ext2_fsblk_t group_first_block; |
652 | ext2_grpblk_t start, end; | 613 | ext2_grpblk_t start, end; |
653 | unsigned long num = 0; | 614 | unsigned long num = 0; |
654 | 615 | ||
655 | /* we do allocation within the reservation window if we have a window */ | 616 | /* we do allocation within the reservation window if we have a window */ |
656 | if (my_rsv) { | 617 | if (my_rsv) { |
657 | group_first_block = ext2_group_first_block_no(sb, group); | 618 | group_first_block = ext2_group_first_block_no(sb, group); |
658 | if (my_rsv->_rsv_start >= group_first_block) | 619 | if (my_rsv->_rsv_start >= group_first_block) |
659 | start = my_rsv->_rsv_start - group_first_block; | 620 | start = my_rsv->_rsv_start - group_first_block; |
660 | else | 621 | else |
661 | /* reservation window cross group boundary */ | 622 | /* reservation window cross group boundary */ |
662 | start = 0; | 623 | start = 0; |
663 | end = my_rsv->_rsv_end - group_first_block + 1; | 624 | end = my_rsv->_rsv_end - group_first_block + 1; |
664 | if (end > EXT2_BLOCKS_PER_GROUP(sb)) | 625 | if (end > EXT2_BLOCKS_PER_GROUP(sb)) |
665 | /* reservation window crosses group boundary */ | 626 | /* reservation window crosses group boundary */ |
666 | end = EXT2_BLOCKS_PER_GROUP(sb); | 627 | end = EXT2_BLOCKS_PER_GROUP(sb); |
667 | if ((start <= grp_goal) && (grp_goal < end)) | 628 | if ((start <= grp_goal) && (grp_goal < end)) |
668 | start = grp_goal; | 629 | start = grp_goal; |
669 | else | 630 | else |
670 | grp_goal = -1; | 631 | grp_goal = -1; |
671 | } else { | 632 | } else { |
672 | if (grp_goal > 0) | 633 | if (grp_goal > 0) |
673 | start = grp_goal; | 634 | start = grp_goal; |
674 | else | 635 | else |
675 | start = 0; | 636 | start = 0; |
676 | end = EXT2_BLOCKS_PER_GROUP(sb); | 637 | end = EXT2_BLOCKS_PER_GROUP(sb); |
677 | } | 638 | } |
678 | 639 | ||
679 | BUG_ON(start > EXT2_BLOCKS_PER_GROUP(sb)); | 640 | BUG_ON(start > EXT2_BLOCKS_PER_GROUP(sb)); |
680 | 641 | ||
681 | repeat: | 642 | repeat: |
682 | if (grp_goal < 0) { | 643 | if (grp_goal < 0) { |
683 | grp_goal = find_next_usable_block(start, bitmap_bh, end); | 644 | grp_goal = find_next_usable_block(start, bitmap_bh, end); |
684 | if (grp_goal < 0) | 645 | if (grp_goal < 0) |
685 | goto fail_access; | 646 | goto fail_access; |
686 | if (!my_rsv) { | 647 | if (!my_rsv) { |
687 | int i; | 648 | int i; |
688 | 649 | ||
689 | for (i = 0; i < 7 && grp_goal > start && | 650 | for (i = 0; i < 7 && grp_goal > start && |
690 | !ext2_test_bit(grp_goal - 1, | 651 | !ext2_test_bit(grp_goal - 1, |
691 | bitmap_bh->b_data); | 652 | bitmap_bh->b_data); |
692 | i++, grp_goal--) | 653 | i++, grp_goal--) |
693 | ; | 654 | ; |
694 | } | 655 | } |
695 | } | 656 | } |
696 | start = grp_goal; | 657 | start = grp_goal; |
697 | 658 | ||
698 | if (ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group), grp_goal, | 659 | if (ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group), grp_goal, |
699 | bitmap_bh->b_data)) { | 660 | bitmap_bh->b_data)) { |
700 | /* | 661 | /* |
701 | * The block was allocated by another thread, or it was | 662 | * The block was allocated by another thread, or it was |
702 | * allocated and then freed by another thread | 663 | * allocated and then freed by another thread |
703 | */ | 664 | */ |
704 | start++; | 665 | start++; |
705 | grp_goal++; | 666 | grp_goal++; |
706 | if (start >= end) | 667 | if (start >= end) |
707 | goto fail_access; | 668 | goto fail_access; |
708 | goto repeat; | 669 | goto repeat; |
709 | } | 670 | } |
710 | num++; | 671 | num++; |
711 | grp_goal++; | 672 | grp_goal++; |
712 | while (num < *count && grp_goal < end | 673 | while (num < *count && grp_goal < end |
713 | && !ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group), | 674 | && !ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group), |
714 | grp_goal, bitmap_bh->b_data)) { | 675 | grp_goal, bitmap_bh->b_data)) { |
715 | num++; | 676 | num++; |
716 | grp_goal++; | 677 | grp_goal++; |
717 | } | 678 | } |
718 | *count = num; | 679 | *count = num; |
719 | return grp_goal - num; | 680 | return grp_goal - num; |
720 | fail_access: | 681 | fail_access: |
721 | *count = num; | 682 | *count = num; |
722 | return -1; | 683 | return -1; |
723 | } | 684 | } |
724 | 685 | ||
725 | /** | 686 | /** |
726 | * find_next_reservable_window(): | 687 | * find_next_reservable_window(): |
727 | * find a reservable space within the given range. | 688 | * find a reservable space within the given range. |
728 | * It does not allocate the reservation window for now: | 689 | * It does not allocate the reservation window for now: |
729 | * alloc_new_reservation() will do the work later. | 690 | * alloc_new_reservation() will do the work later. |
730 | * | 691 | * |
731 | * @search_head: the head of the searching list; | 692 | * @search_head: the head of the searching list; |
732 | * This is not necessarily the list head of the whole filesystem | 693 | * This is not necessarily the list head of the whole filesystem |
733 | * | 694 | * |
734 | * We have both head and start_block to assist the search | 695 | * We have both head and start_block to assist the search |
735 | * for the reservable space. The list starts from head, | 696 | * for the reservable space. The list starts from head, |
736 | * but we will shift to the place where start_block is, | 697 | * but we will shift to the place where start_block is, |
737 | * then start from there, when looking for a reservable space. | 698 | * then start from there, when looking for a reservable space. |
738 | * | 699 | * |
739 | * @size: the target new reservation window size | 700 | * @size: the target new reservation window size |
740 | * | 701 | * |
741 | * @group_first_block: the first block we consider to start | 702 | * @group_first_block: the first block we consider to start |
742 | * the real search from | 703 | * the real search from |
743 | * | 704 | * |
744 | * @last_block: | 705 | * @last_block: |
745 | * the maximum block number that our goal reservable space | 706 | * the maximum block number that our goal reservable space |
746 | * could start from. This is normally the last block in this | 707 | * could start from. This is normally the last block in this |
747 | * group. The search will end when we found the start of next | 708 | * group. The search will end when we found the start of next |
748 | * possible reservable space is out of this boundary. | 709 | * possible reservable space is out of this boundary. |
749 | * This could handle the cross boundary reservation window | 710 | * This could handle the cross boundary reservation window |
750 | * request. | 711 | * request. |
751 | * | 712 | * |
752 | * basically we search from the given range, rather than the whole | 713 | * basically we search from the given range, rather than the whole |
753 | * reservation double linked list, (start_block, last_block) | 714 | * reservation double linked list, (start_block, last_block) |
754 | * to find a free region that is of my size and has not | 715 | * to find a free region that is of my size and has not |
755 | * been reserved. | 716 | * been reserved. |
756 | * | 717 | * |
757 | */ | 718 | */ |
758 | static int find_next_reservable_window( | 719 | static int find_next_reservable_window( |
759 | struct ext2_reserve_window_node *search_head, | 720 | struct ext2_reserve_window_node *search_head, |
760 | struct ext2_reserve_window_node *my_rsv, | 721 | struct ext2_reserve_window_node *my_rsv, |
761 | struct super_block * sb, | 722 | struct super_block * sb, |
762 | ext2_fsblk_t start_block, | 723 | ext2_fsblk_t start_block, |
763 | ext2_fsblk_t last_block) | 724 | ext2_fsblk_t last_block) |
764 | { | 725 | { |
765 | struct rb_node *next; | 726 | struct rb_node *next; |
766 | struct ext2_reserve_window_node *rsv, *prev; | 727 | struct ext2_reserve_window_node *rsv, *prev; |
767 | ext2_fsblk_t cur; | 728 | ext2_fsblk_t cur; |
768 | int size = my_rsv->rsv_goal_size; | 729 | int size = my_rsv->rsv_goal_size; |
769 | 730 | ||
770 | /* TODO: make the start of the reservation window byte-aligned */ | 731 | /* TODO: make the start of the reservation window byte-aligned */ |
771 | /* cur = *start_block & ~7;*/ | 732 | /* cur = *start_block & ~7;*/ |
772 | cur = start_block; | 733 | cur = start_block; |
773 | rsv = search_head; | 734 | rsv = search_head; |
774 | if (!rsv) | 735 | if (!rsv) |
775 | return -1; | 736 | return -1; |
776 | 737 | ||
777 | while (1) { | 738 | while (1) { |
778 | if (cur <= rsv->rsv_end) | 739 | if (cur <= rsv->rsv_end) |
779 | cur = rsv->rsv_end + 1; | 740 | cur = rsv->rsv_end + 1; |
780 | 741 | ||
781 | /* TODO? | 742 | /* TODO? |
782 | * in the case we could not find a reservable space | 743 | * in the case we could not find a reservable space |
783 | * that is what is expected, during the re-search, we could | 744 | * that is what is expected, during the re-search, we could |
784 | * remember what's the largest reservable space we could have | 745 | * remember what's the largest reservable space we could have |
785 | * and return that one. | 746 | * and return that one. |
786 | * | 747 | * |
787 | * For now it will fail if we could not find the reservable | 748 | * For now it will fail if we could not find the reservable |
788 | * space with expected-size (or more)... | 749 | * space with expected-size (or more)... |
789 | */ | 750 | */ |
790 | if (cur > last_block) | 751 | if (cur > last_block) |
791 | return -1; /* fail */ | 752 | return -1; /* fail */ |
792 | 753 | ||
793 | prev = rsv; | 754 | prev = rsv; |
794 | next = rb_next(&rsv->rsv_node); | 755 | next = rb_next(&rsv->rsv_node); |
795 | rsv = rb_entry(next,struct ext2_reserve_window_node,rsv_node); | 756 | rsv = rb_entry(next,struct ext2_reserve_window_node,rsv_node); |
796 | 757 | ||
797 | /* | 758 | /* |
798 | * Reached the last reservation, we can just append to the | 759 | * Reached the last reservation, we can just append to the |
799 | * previous one. | 760 | * previous one. |
800 | */ | 761 | */ |
801 | if (!next) | 762 | if (!next) |
802 | break; | 763 | break; |
803 | 764 | ||
804 | if (cur + size <= rsv->rsv_start) { | 765 | if (cur + size <= rsv->rsv_start) { |
805 | /* | 766 | /* |
806 | * Found a reserveable space big enough. We could | 767 | * Found a reserveable space big enough. We could |
807 | * have a reservation across the group boundary here | 768 | * have a reservation across the group boundary here |
808 | */ | 769 | */ |
809 | break; | 770 | break; |
810 | } | 771 | } |
811 | } | 772 | } |
812 | /* | 773 | /* |
813 | * we come here either : | 774 | * we come here either : |
814 | * when we reach the end of the whole list, | 775 | * when we reach the end of the whole list, |
815 | * and there is empty reservable space after last entry in the list. | 776 | * and there is empty reservable space after last entry in the list. |
816 | * append it to the end of the list. | 777 | * append it to the end of the list. |
817 | * | 778 | * |
818 | * or we found one reservable space in the middle of the list, | 779 | * or we found one reservable space in the middle of the list, |
819 | * return the reservation window that we could append to. | 780 | * return the reservation window that we could append to. |
820 | * succeed. | 781 | * succeed. |
821 | */ | 782 | */ |
822 | 783 | ||
823 | if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window))) | 784 | if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window))) |
824 | rsv_window_remove(sb, my_rsv); | 785 | rsv_window_remove(sb, my_rsv); |
825 | 786 | ||
826 | /* | 787 | /* |
827 | * Let's book the whole avaliable window for now. We will check the | 788 | * Let's book the whole avaliable window for now. We will check the |
828 | * disk bitmap later and then, if there are free blocks then we adjust | 789 | * disk bitmap later and then, if there are free blocks then we adjust |
829 | * the window size if it's larger than requested. | 790 | * the window size if it's larger than requested. |
830 | * Otherwise, we will remove this node from the tree next time | 791 | * Otherwise, we will remove this node from the tree next time |
831 | * call find_next_reservable_window. | 792 | * call find_next_reservable_window. |
832 | */ | 793 | */ |
833 | my_rsv->rsv_start = cur; | 794 | my_rsv->rsv_start = cur; |
834 | my_rsv->rsv_end = cur + size - 1; | 795 | my_rsv->rsv_end = cur + size - 1; |
835 | my_rsv->rsv_alloc_hit = 0; | 796 | my_rsv->rsv_alloc_hit = 0; |
836 | 797 | ||
837 | if (prev != my_rsv) | 798 | if (prev != my_rsv) |
838 | ext2_rsv_window_add(sb, my_rsv); | 799 | ext2_rsv_window_add(sb, my_rsv); |
839 | 800 | ||
840 | return 0; | 801 | return 0; |
841 | } | 802 | } |
842 | 803 | ||
843 | /** | 804 | /** |
844 | * alloc_new_reservation()--allocate a new reservation window | 805 | * alloc_new_reservation()--allocate a new reservation window |
845 | * | 806 | * |
846 | * To make a new reservation, we search part of the filesystem | 807 | * To make a new reservation, we search part of the filesystem |
847 | * reservation list (the list that inside the group). We try to | 808 | * reservation list (the list that inside the group). We try to |
848 | * allocate a new reservation window near the allocation goal, | 809 | * allocate a new reservation window near the allocation goal, |
849 | * or the beginning of the group, if there is no goal. | 810 | * or the beginning of the group, if there is no goal. |
850 | * | 811 | * |
851 | * We first find a reservable space after the goal, then from | 812 | * We first find a reservable space after the goal, then from |
852 | * there, we check the bitmap for the first free block after | 813 | * there, we check the bitmap for the first free block after |
853 | * it. If there is no free block until the end of group, then the | 814 | * it. If there is no free block until the end of group, then the |
854 | * whole group is full, we failed. Otherwise, check if the free | 815 | * whole group is full, we failed. Otherwise, check if the free |
855 | * block is inside the expected reservable space, if so, we | 816 | * block is inside the expected reservable space, if so, we |
856 | * succeed. | 817 | * succeed. |
857 | * If the first free block is outside the reservable space, then | 818 | * If the first free block is outside the reservable space, then |
858 | * start from the first free block, we search for next available | 819 | * start from the first free block, we search for next available |
859 | * space, and go on. | 820 | * space, and go on. |
860 | * | 821 | * |
861 | * on succeed, a new reservation will be found and inserted into the list | 822 | * on succeed, a new reservation will be found and inserted into the list |
862 | * It contains at least one free block, and it does not overlap with other | 823 | * It contains at least one free block, and it does not overlap with other |
863 | * reservation windows. | 824 | * reservation windows. |
864 | * | 825 | * |
865 | * failed: we failed to find a reservation window in this group | 826 | * failed: we failed to find a reservation window in this group |
866 | * | 827 | * |
867 | * @rsv: the reservation | 828 | * @rsv: the reservation |
868 | * | 829 | * |
869 | * @grp_goal: The goal (group-relative). It is where the search for a | 830 | * @grp_goal: The goal (group-relative). It is where the search for a |
870 | * free reservable space should start from. | 831 | * free reservable space should start from. |
871 | * if we have a goal(goal >0 ), then start from there, | 832 | * if we have a goal(goal >0 ), then start from there, |
872 | * no goal(goal = -1), we start from the first block | 833 | * no goal(goal = -1), we start from the first block |
873 | * of the group. | 834 | * of the group. |
874 | * | 835 | * |
875 | * @sb: the super block | 836 | * @sb: the super block |
876 | * @group: the group we are trying to allocate in | 837 | * @group: the group we are trying to allocate in |
877 | * @bitmap_bh: the block group block bitmap | 838 | * @bitmap_bh: the block group block bitmap |
878 | * | 839 | * |
879 | */ | 840 | */ |
880 | static int alloc_new_reservation(struct ext2_reserve_window_node *my_rsv, | 841 | static int alloc_new_reservation(struct ext2_reserve_window_node *my_rsv, |
881 | ext2_grpblk_t grp_goal, struct super_block *sb, | 842 | ext2_grpblk_t grp_goal, struct super_block *sb, |
882 | unsigned int group, struct buffer_head *bitmap_bh) | 843 | unsigned int group, struct buffer_head *bitmap_bh) |
883 | { | 844 | { |
884 | struct ext2_reserve_window_node *search_head; | 845 | struct ext2_reserve_window_node *search_head; |
885 | ext2_fsblk_t group_first_block, group_end_block, start_block; | 846 | ext2_fsblk_t group_first_block, group_end_block, start_block; |
886 | ext2_grpblk_t first_free_block; | 847 | ext2_grpblk_t first_free_block; |
887 | struct rb_root *fs_rsv_root = &EXT2_SB(sb)->s_rsv_window_root; | 848 | struct rb_root *fs_rsv_root = &EXT2_SB(sb)->s_rsv_window_root; |
888 | unsigned long size; | 849 | unsigned long size; |
889 | int ret; | 850 | int ret; |
890 | spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock; | 851 | spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock; |
891 | 852 | ||
892 | group_first_block = ext2_group_first_block_no(sb, group); | 853 | group_first_block = ext2_group_first_block_no(sb, group); |
893 | group_end_block = group_first_block + (EXT2_BLOCKS_PER_GROUP(sb) - 1); | 854 | group_end_block = group_first_block + (EXT2_BLOCKS_PER_GROUP(sb) - 1); |
894 | 855 | ||
895 | if (grp_goal < 0) | 856 | if (grp_goal < 0) |
896 | start_block = group_first_block; | 857 | start_block = group_first_block; |
897 | else | 858 | else |
898 | start_block = grp_goal + group_first_block; | 859 | start_block = grp_goal + group_first_block; |
899 | 860 | ||
900 | size = my_rsv->rsv_goal_size; | 861 | size = my_rsv->rsv_goal_size; |
901 | 862 | ||
902 | if (!rsv_is_empty(&my_rsv->rsv_window)) { | 863 | if (!rsv_is_empty(&my_rsv->rsv_window)) { |
903 | /* | 864 | /* |
904 | * if the old reservation is cross group boundary | 865 | * if the old reservation is cross group boundary |
905 | * and if the goal is inside the old reservation window, | 866 | * and if the goal is inside the old reservation window, |
906 | * we will come here when we just failed to allocate from | 867 | * we will come here when we just failed to allocate from |
907 | * the first part of the window. We still have another part | 868 | * the first part of the window. We still have another part |
908 | * that belongs to the next group. In this case, there is no | 869 | * that belongs to the next group. In this case, there is no |
909 | * point to discard our window and try to allocate a new one | 870 | * point to discard our window and try to allocate a new one |
910 | * in this group(which will fail). we should | 871 | * in this group(which will fail). we should |
911 | * keep the reservation window, just simply move on. | 872 | * keep the reservation window, just simply move on. |
912 | * | 873 | * |
913 | * Maybe we could shift the start block of the reservation | 874 | * Maybe we could shift the start block of the reservation |
914 | * window to the first block of next group. | 875 | * window to the first block of next group. |
915 | */ | 876 | */ |
916 | 877 | ||
917 | if ((my_rsv->rsv_start <= group_end_block) && | 878 | if ((my_rsv->rsv_start <= group_end_block) && |
918 | (my_rsv->rsv_end > group_end_block) && | 879 | (my_rsv->rsv_end > group_end_block) && |
919 | (start_block >= my_rsv->rsv_start)) | 880 | (start_block >= my_rsv->rsv_start)) |
920 | return -1; | 881 | return -1; |
921 | 882 | ||
922 | if ((my_rsv->rsv_alloc_hit > | 883 | if ((my_rsv->rsv_alloc_hit > |
923 | (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) { | 884 | (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) { |
924 | /* | 885 | /* |
925 | * if the previously allocation hit ratio is | 886 | * if the previously allocation hit ratio is |
926 | * greater than 1/2, then we double the size of | 887 | * greater than 1/2, then we double the size of |
927 | * the reservation window the next time, | 888 | * the reservation window the next time, |
928 | * otherwise we keep the same size window | 889 | * otherwise we keep the same size window |
929 | */ | 890 | */ |
930 | size = size * 2; | 891 | size = size * 2; |
931 | if (size > EXT2_MAX_RESERVE_BLOCKS) | 892 | if (size > EXT2_MAX_RESERVE_BLOCKS) |
932 | size = EXT2_MAX_RESERVE_BLOCKS; | 893 | size = EXT2_MAX_RESERVE_BLOCKS; |
933 | my_rsv->rsv_goal_size= size; | 894 | my_rsv->rsv_goal_size= size; |
934 | } | 895 | } |
935 | } | 896 | } |
936 | 897 | ||
937 | spin_lock(rsv_lock); | 898 | spin_lock(rsv_lock); |
938 | /* | 899 | /* |
939 | * shift the search start to the window near the goal block | 900 | * shift the search start to the window near the goal block |
940 | */ | 901 | */ |
941 | search_head = search_reserve_window(fs_rsv_root, start_block); | 902 | search_head = search_reserve_window(fs_rsv_root, start_block); |
942 | 903 | ||
943 | /* | 904 | /* |
944 | * find_next_reservable_window() simply finds a reservable window | 905 | * find_next_reservable_window() simply finds a reservable window |
945 | * inside the given range(start_block, group_end_block). | 906 | * inside the given range(start_block, group_end_block). |
946 | * | 907 | * |
947 | * To make sure the reservation window has a free bit inside it, we | 908 | * To make sure the reservation window has a free bit inside it, we |
948 | * need to check the bitmap after we found a reservable window. | 909 | * need to check the bitmap after we found a reservable window. |
949 | */ | 910 | */ |
950 | retry: | 911 | retry: |
951 | ret = find_next_reservable_window(search_head, my_rsv, sb, | 912 | ret = find_next_reservable_window(search_head, my_rsv, sb, |
952 | start_block, group_end_block); | 913 | start_block, group_end_block); |
953 | 914 | ||
954 | if (ret == -1) { | 915 | if (ret == -1) { |
955 | if (!rsv_is_empty(&my_rsv->rsv_window)) | 916 | if (!rsv_is_empty(&my_rsv->rsv_window)) |
956 | rsv_window_remove(sb, my_rsv); | 917 | rsv_window_remove(sb, my_rsv); |
957 | spin_unlock(rsv_lock); | 918 | spin_unlock(rsv_lock); |
958 | return -1; | 919 | return -1; |
959 | } | 920 | } |
960 | 921 | ||
961 | /* | 922 | /* |
962 | * On success, find_next_reservable_window() returns the | 923 | * On success, find_next_reservable_window() returns the |
963 | * reservation window where there is a reservable space after it. | 924 | * reservation window where there is a reservable space after it. |
964 | * Before we reserve this reservable space, we need | 925 | * Before we reserve this reservable space, we need |
965 | * to make sure there is at least a free block inside this region. | 926 | * to make sure there is at least a free block inside this region. |
966 | * | 927 | * |
967 | * Search the first free bit on the block bitmap. Search starts from | 928 | * Search the first free bit on the block bitmap. Search starts from |
968 | * the start block of the reservable space we just found. | 929 | * the start block of the reservable space we just found. |
969 | */ | 930 | */ |
970 | spin_unlock(rsv_lock); | 931 | spin_unlock(rsv_lock); |
971 | first_free_block = bitmap_search_next_usable_block( | 932 | first_free_block = bitmap_search_next_usable_block( |
972 | my_rsv->rsv_start - group_first_block, | 933 | my_rsv->rsv_start - group_first_block, |
973 | bitmap_bh, group_end_block - group_first_block + 1); | 934 | bitmap_bh, group_end_block - group_first_block + 1); |
974 | 935 | ||
975 | if (first_free_block < 0) { | 936 | if (first_free_block < 0) { |
976 | /* | 937 | /* |
977 | * no free block left on the bitmap, no point | 938 | * no free block left on the bitmap, no point |
978 | * to reserve the space. return failed. | 939 | * to reserve the space. return failed. |
979 | */ | 940 | */ |
980 | spin_lock(rsv_lock); | 941 | spin_lock(rsv_lock); |
981 | if (!rsv_is_empty(&my_rsv->rsv_window)) | 942 | if (!rsv_is_empty(&my_rsv->rsv_window)) |
982 | rsv_window_remove(sb, my_rsv); | 943 | rsv_window_remove(sb, my_rsv); |
983 | spin_unlock(rsv_lock); | 944 | spin_unlock(rsv_lock); |
984 | return -1; /* failed */ | 945 | return -1; /* failed */ |
985 | } | 946 | } |
986 | 947 | ||
987 | start_block = first_free_block + group_first_block; | 948 | start_block = first_free_block + group_first_block; |
988 | /* | 949 | /* |
989 | * check if the first free block is within the | 950 | * check if the first free block is within the |
990 | * free space we just reserved | 951 | * free space we just reserved |
991 | */ | 952 | */ |
992 | if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end) | 953 | if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end) |
993 | return 0; /* success */ | 954 | return 0; /* success */ |
994 | /* | 955 | /* |
995 | * if the first free bit we found is out of the reservable space | 956 | * if the first free bit we found is out of the reservable space |
996 | * continue search for next reservable space, | 957 | * continue search for next reservable space, |
997 | * start from where the free block is, | 958 | * start from where the free block is, |
998 | * we also shift the list head to where we stopped last time | 959 | * we also shift the list head to where we stopped last time |
999 | */ | 960 | */ |
1000 | search_head = my_rsv; | 961 | search_head = my_rsv; |
1001 | spin_lock(rsv_lock); | 962 | spin_lock(rsv_lock); |
1002 | goto retry; | 963 | goto retry; |
1003 | } | 964 | } |
1004 | 965 | ||
1005 | /** | 966 | /** |
1006 | * try_to_extend_reservation() | 967 | * try_to_extend_reservation() |
1007 | * @my_rsv: given reservation window | 968 | * @my_rsv: given reservation window |
1008 | * @sb: super block | 969 | * @sb: super block |
1009 | * @size: the delta to extend | 970 | * @size: the delta to extend |
1010 | * | 971 | * |
1011 | * Attempt to expand the reservation window large enough to have | 972 | * Attempt to expand the reservation window large enough to have |
1012 | * required number of free blocks | 973 | * required number of free blocks |
1013 | * | 974 | * |
1014 | * Since ext2_try_to_allocate() will always allocate blocks within | 975 | * Since ext2_try_to_allocate() will always allocate blocks within |
1015 | * the reservation window range, if the window size is too small, | 976 | * the reservation window range, if the window size is too small, |
1016 | * multiple blocks allocation has to stop at the end of the reservation | 977 | * multiple blocks allocation has to stop at the end of the reservation |
1017 | * window. To make this more efficient, given the total number of | 978 | * window. To make this more efficient, given the total number of |
1018 | * blocks needed and the current size of the window, we try to | 979 | * blocks needed and the current size of the window, we try to |
1019 | * expand the reservation window size if necessary on a best-effort | 980 | * expand the reservation window size if necessary on a best-effort |
1020 | * basis before ext2_new_blocks() tries to allocate blocks. | 981 | * basis before ext2_new_blocks() tries to allocate blocks. |
1021 | */ | 982 | */ |
1022 | static void try_to_extend_reservation(struct ext2_reserve_window_node *my_rsv, | 983 | static void try_to_extend_reservation(struct ext2_reserve_window_node *my_rsv, |
1023 | struct super_block *sb, int size) | 984 | struct super_block *sb, int size) |
1024 | { | 985 | { |
1025 | struct ext2_reserve_window_node *next_rsv; | 986 | struct ext2_reserve_window_node *next_rsv; |
1026 | struct rb_node *next; | 987 | struct rb_node *next; |
1027 | spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock; | 988 | spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock; |
1028 | 989 | ||
1029 | if (!spin_trylock(rsv_lock)) | 990 | if (!spin_trylock(rsv_lock)) |
1030 | return; | 991 | return; |
1031 | 992 | ||
1032 | next = rb_next(&my_rsv->rsv_node); | 993 | next = rb_next(&my_rsv->rsv_node); |
1033 | 994 | ||
1034 | if (!next) | 995 | if (!next) |
1035 | my_rsv->rsv_end += size; | 996 | my_rsv->rsv_end += size; |
1036 | else { | 997 | else { |
1037 | next_rsv = rb_entry(next, struct ext2_reserve_window_node, rsv_node); | 998 | next_rsv = rb_entry(next, struct ext2_reserve_window_node, rsv_node); |
1038 | 999 | ||
1039 | if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size) | 1000 | if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size) |
1040 | my_rsv->rsv_end += size; | 1001 | my_rsv->rsv_end += size; |
1041 | else | 1002 | else |
1042 | my_rsv->rsv_end = next_rsv->rsv_start - 1; | 1003 | my_rsv->rsv_end = next_rsv->rsv_start - 1; |
1043 | } | 1004 | } |
1044 | spin_unlock(rsv_lock); | 1005 | spin_unlock(rsv_lock); |
1045 | } | 1006 | } |
1046 | 1007 | ||
1047 | /** | 1008 | /** |
1048 | * ext2_try_to_allocate_with_rsv() | 1009 | * ext2_try_to_allocate_with_rsv() |
1049 | * @sb: superblock | 1010 | * @sb: superblock |
1050 | * @group: given allocation block group | 1011 | * @group: given allocation block group |
1051 | * @bitmap_bh: bufferhead holds the block bitmap | 1012 | * @bitmap_bh: bufferhead holds the block bitmap |
1052 | * @grp_goal: given target block within the group | 1013 | * @grp_goal: given target block within the group |
1053 | * @count: target number of blocks to allocate | 1014 | * @count: target number of blocks to allocate |
1054 | * @my_rsv: reservation window | 1015 | * @my_rsv: reservation window |
1055 | * | 1016 | * |
1056 | * This is the main function used to allocate a new block and its reservation | 1017 | * This is the main function used to allocate a new block and its reservation |
1057 | * window. | 1018 | * window. |
1058 | * | 1019 | * |
1059 | * Each time when a new block allocation is need, first try to allocate from | 1020 | * Each time when a new block allocation is need, first try to allocate from |
1060 | * its own reservation. If it does not have a reservation window, instead of | 1021 | * its own reservation. If it does not have a reservation window, instead of |
1061 | * looking for a free bit on bitmap first, then look up the reservation list to | 1022 | * looking for a free bit on bitmap first, then look up the reservation list to |
1062 | * see if it is inside somebody else's reservation window, we try to allocate a | 1023 | * see if it is inside somebody else's reservation window, we try to allocate a |
1063 | * reservation window for it starting from the goal first. Then do the block | 1024 | * reservation window for it starting from the goal first. Then do the block |
1064 | * allocation within the reservation window. | 1025 | * allocation within the reservation window. |
1065 | * | 1026 | * |
1066 | * This will avoid keeping on searching the reservation list again and | 1027 | * This will avoid keeping on searching the reservation list again and |
1067 | * again when somebody is looking for a free block (without | 1028 | * again when somebody is looking for a free block (without |
1068 | * reservation), and there are lots of free blocks, but they are all | 1029 | * reservation), and there are lots of free blocks, but they are all |
1069 | * being reserved. | 1030 | * being reserved. |
1070 | * | 1031 | * |
1071 | * We use a red-black tree for the per-filesystem reservation list. | 1032 | * We use a red-black tree for the per-filesystem reservation list. |
1072 | */ | 1033 | */ |
1073 | static ext2_grpblk_t | 1034 | static ext2_grpblk_t |
1074 | ext2_try_to_allocate_with_rsv(struct super_block *sb, unsigned int group, | 1035 | ext2_try_to_allocate_with_rsv(struct super_block *sb, unsigned int group, |
1075 | struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal, | 1036 | struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal, |
1076 | struct ext2_reserve_window_node * my_rsv, | 1037 | struct ext2_reserve_window_node * my_rsv, |
1077 | unsigned long *count) | 1038 | unsigned long *count) |
1078 | { | 1039 | { |
1079 | ext2_fsblk_t group_first_block, group_last_block; | 1040 | ext2_fsblk_t group_first_block, group_last_block; |
1080 | ext2_grpblk_t ret = 0; | 1041 | ext2_grpblk_t ret = 0; |
1081 | unsigned long num = *count; | 1042 | unsigned long num = *count; |
1082 | 1043 | ||
1083 | /* | 1044 | /* |
1084 | * we don't deal with reservation when | 1045 | * we don't deal with reservation when |
1085 | * filesystem is mounted without reservation | 1046 | * filesystem is mounted without reservation |
1086 | * or the file is not a regular file | 1047 | * or the file is not a regular file |
1087 | * or last attempt to allocate a block with reservation turned on failed | 1048 | * or last attempt to allocate a block with reservation turned on failed |
1088 | */ | 1049 | */ |
1089 | if (my_rsv == NULL) { | 1050 | if (my_rsv == NULL) { |
1090 | return ext2_try_to_allocate(sb, group, bitmap_bh, | 1051 | return ext2_try_to_allocate(sb, group, bitmap_bh, |
1091 | grp_goal, count, NULL); | 1052 | grp_goal, count, NULL); |
1092 | } | 1053 | } |
1093 | /* | 1054 | /* |
1094 | * grp_goal is a group relative block number (if there is a goal) | 1055 | * grp_goal is a group relative block number (if there is a goal) |
1095 | * 0 <= grp_goal < EXT2_BLOCKS_PER_GROUP(sb) | 1056 | * 0 <= grp_goal < EXT2_BLOCKS_PER_GROUP(sb) |
1096 | * first block is a filesystem wide block number | 1057 | * first block is a filesystem wide block number |
1097 | * first block is the block number of the first block in this group | 1058 | * first block is the block number of the first block in this group |
1098 | */ | 1059 | */ |
1099 | group_first_block = ext2_group_first_block_no(sb, group); | 1060 | group_first_block = ext2_group_first_block_no(sb, group); |
1100 | group_last_block = group_first_block + (EXT2_BLOCKS_PER_GROUP(sb) - 1); | 1061 | group_last_block = group_first_block + (EXT2_BLOCKS_PER_GROUP(sb) - 1); |
1101 | 1062 | ||
1102 | /* | 1063 | /* |
1103 | * Basically we will allocate a new block from inode's reservation | 1064 | * Basically we will allocate a new block from inode's reservation |
1104 | * window. | 1065 | * window. |
1105 | * | 1066 | * |
1106 | * We need to allocate a new reservation window, if: | 1067 | * We need to allocate a new reservation window, if: |
1107 | * a) inode does not have a reservation window; or | 1068 | * a) inode does not have a reservation window; or |
1108 | * b) last attempt to allocate a block from existing reservation | 1069 | * b) last attempt to allocate a block from existing reservation |
1109 | * failed; or | 1070 | * failed; or |
1110 | * c) we come here with a goal and with a reservation window | 1071 | * c) we come here with a goal and with a reservation window |
1111 | * | 1072 | * |
1112 | * We do not need to allocate a new reservation window if we come here | 1073 | * We do not need to allocate a new reservation window if we come here |
1113 | * at the beginning with a goal and the goal is inside the window, or | 1074 | * at the beginning with a goal and the goal is inside the window, or |
1114 | * we don't have a goal but already have a reservation window. | 1075 | * we don't have a goal but already have a reservation window. |
1115 | * then we could go to allocate from the reservation window directly. | 1076 | * then we could go to allocate from the reservation window directly. |
1116 | */ | 1077 | */ |
1117 | while (1) { | 1078 | while (1) { |
1118 | if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) || | 1079 | if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) || |
1119 | !goal_in_my_reservation(&my_rsv->rsv_window, | 1080 | !goal_in_my_reservation(&my_rsv->rsv_window, |
1120 | grp_goal, group, sb)) { | 1081 | grp_goal, group, sb)) { |
1121 | if (my_rsv->rsv_goal_size < *count) | 1082 | if (my_rsv->rsv_goal_size < *count) |
1122 | my_rsv->rsv_goal_size = *count; | 1083 | my_rsv->rsv_goal_size = *count; |
1123 | ret = alloc_new_reservation(my_rsv, grp_goal, sb, | 1084 | ret = alloc_new_reservation(my_rsv, grp_goal, sb, |
1124 | group, bitmap_bh); | 1085 | group, bitmap_bh); |
1125 | if (ret < 0) | 1086 | if (ret < 0) |
1126 | break; /* failed */ | 1087 | break; /* failed */ |
1127 | 1088 | ||
1128 | if (!goal_in_my_reservation(&my_rsv->rsv_window, | 1089 | if (!goal_in_my_reservation(&my_rsv->rsv_window, |
1129 | grp_goal, group, sb)) | 1090 | grp_goal, group, sb)) |
1130 | grp_goal = -1; | 1091 | grp_goal = -1; |
1131 | } else if (grp_goal >= 0) { | 1092 | } else if (grp_goal >= 0) { |
1132 | int curr = my_rsv->rsv_end - | 1093 | int curr = my_rsv->rsv_end - |
1133 | (grp_goal + group_first_block) + 1; | 1094 | (grp_goal + group_first_block) + 1; |
1134 | 1095 | ||
1135 | if (curr < *count) | 1096 | if (curr < *count) |
1136 | try_to_extend_reservation(my_rsv, sb, | 1097 | try_to_extend_reservation(my_rsv, sb, |
1137 | *count - curr); | 1098 | *count - curr); |
1138 | } | 1099 | } |
1139 | 1100 | ||
1140 | if ((my_rsv->rsv_start > group_last_block) || | 1101 | if ((my_rsv->rsv_start > group_last_block) || |
1141 | (my_rsv->rsv_end < group_first_block)) { | 1102 | (my_rsv->rsv_end < group_first_block)) { |
1142 | rsv_window_dump(&EXT2_SB(sb)->s_rsv_window_root, 1); | 1103 | rsv_window_dump(&EXT2_SB(sb)->s_rsv_window_root, 1); |
1143 | BUG(); | 1104 | BUG(); |
1144 | } | 1105 | } |
1145 | ret = ext2_try_to_allocate(sb, group, bitmap_bh, grp_goal, | 1106 | ret = ext2_try_to_allocate(sb, group, bitmap_bh, grp_goal, |
1146 | &num, &my_rsv->rsv_window); | 1107 | &num, &my_rsv->rsv_window); |
1147 | if (ret >= 0) { | 1108 | if (ret >= 0) { |
1148 | my_rsv->rsv_alloc_hit += num; | 1109 | my_rsv->rsv_alloc_hit += num; |
1149 | *count = num; | 1110 | *count = num; |
1150 | break; /* succeed */ | 1111 | break; /* succeed */ |
1151 | } | 1112 | } |
1152 | num = *count; | 1113 | num = *count; |
1153 | } | 1114 | } |
1154 | return ret; | 1115 | return ret; |
1155 | } | 1116 | } |
1156 | 1117 | ||
1157 | /** | 1118 | /** |
1158 | * ext2_has_free_blocks() | 1119 | * ext2_has_free_blocks() |
1159 | * @sbi: in-core super block structure. | 1120 | * @sbi: in-core super block structure. |
1160 | * | 1121 | * |
1161 | * Check if filesystem has at least 1 free block available for allocation. | 1122 | * Check if filesystem has at least 1 free block available for allocation. |
1162 | */ | 1123 | */ |
1163 | static int ext2_has_free_blocks(struct ext2_sb_info *sbi) | 1124 | static int ext2_has_free_blocks(struct ext2_sb_info *sbi) |
1164 | { | 1125 | { |
1165 | ext2_fsblk_t free_blocks, root_blocks; | 1126 | ext2_fsblk_t free_blocks, root_blocks; |
1166 | 1127 | ||
1167 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); | 1128 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
1168 | root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count); | 1129 | root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count); |
1169 | if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) && | 1130 | if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) && |
1170 | sbi->s_resuid != current->fsuid && | 1131 | sbi->s_resuid != current->fsuid && |
1171 | (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) { | 1132 | (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) { |
1172 | return 0; | 1133 | return 0; |
1173 | } | 1134 | } |
1174 | return 1; | 1135 | return 1; |
1175 | } | 1136 | } |
1176 | 1137 | ||
1177 | /* | 1138 | /* |
1178 | * ext2_new_blocks() -- core block(s) allocation function | 1139 | * ext2_new_blocks() -- core block(s) allocation function |
1179 | * @inode: file inode | 1140 | * @inode: file inode |
1180 | * @goal: given target block(filesystem wide) | 1141 | * @goal: given target block(filesystem wide) |
1181 | * @count: target number of blocks to allocate | 1142 | * @count: target number of blocks to allocate |
1182 | * @errp: error code | 1143 | * @errp: error code |
1183 | * | 1144 | * |
1184 | * ext2_new_blocks uses a goal block to assist allocation. If the goal is | 1145 | * ext2_new_blocks uses a goal block to assist allocation. If the goal is |
1185 | * free, or there is a free block within 32 blocks of the goal, that block | 1146 | * free, or there is a free block within 32 blocks of the goal, that block |
1186 | * is allocated. Otherwise a forward search is made for a free block; within | 1147 | * is allocated. Otherwise a forward search is made for a free block; within |
1187 | * each block group the search first looks for an entire free byte in the block | 1148 | * each block group the search first looks for an entire free byte in the block |
1188 | * bitmap, and then for any free bit if that fails. | 1149 | * bitmap, and then for any free bit if that fails. |
1189 | * This function also updates quota and i_blocks field. | 1150 | * This function also updates quota and i_blocks field. |
1190 | */ | 1151 | */ |
1191 | ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal, | 1152 | ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal, |
1192 | unsigned long *count, int *errp) | 1153 | unsigned long *count, int *errp) |
1193 | { | 1154 | { |
1194 | struct buffer_head *bitmap_bh = NULL; | 1155 | struct buffer_head *bitmap_bh = NULL; |
1195 | struct buffer_head *gdp_bh; | 1156 | struct buffer_head *gdp_bh; |
1196 | int group_no; | 1157 | int group_no; |
1197 | int goal_group; | 1158 | int goal_group; |
1198 | ext2_grpblk_t grp_target_blk; /* blockgroup relative goal block */ | 1159 | ext2_grpblk_t grp_target_blk; /* blockgroup relative goal block */ |
1199 | ext2_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/ | 1160 | ext2_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/ |
1200 | ext2_fsblk_t ret_block; /* filesyetem-wide allocated block */ | 1161 | ext2_fsblk_t ret_block; /* filesyetem-wide allocated block */ |
1201 | int bgi; /* blockgroup iteration index */ | 1162 | int bgi; /* blockgroup iteration index */ |
1202 | int performed_allocation = 0; | 1163 | int performed_allocation = 0; |
1203 | ext2_grpblk_t free_blocks; /* number of free blocks in a group */ | 1164 | ext2_grpblk_t free_blocks; /* number of free blocks in a group */ |
1204 | struct super_block *sb; | 1165 | struct super_block *sb; |
1205 | struct ext2_group_desc *gdp; | 1166 | struct ext2_group_desc *gdp; |
1206 | struct ext2_super_block *es; | 1167 | struct ext2_super_block *es; |
1207 | struct ext2_sb_info *sbi; | 1168 | struct ext2_sb_info *sbi; |
1208 | struct ext2_reserve_window_node *my_rsv = NULL; | 1169 | struct ext2_reserve_window_node *my_rsv = NULL; |
1209 | struct ext2_block_alloc_info *block_i; | 1170 | struct ext2_block_alloc_info *block_i; |
1210 | unsigned short windowsz = 0; | 1171 | unsigned short windowsz = 0; |
1211 | unsigned long ngroups; | 1172 | unsigned long ngroups; |
1212 | unsigned long num = *count; | 1173 | unsigned long num = *count; |
1213 | 1174 | ||
1214 | *errp = -ENOSPC; | 1175 | *errp = -ENOSPC; |
1215 | sb = inode->i_sb; | 1176 | sb = inode->i_sb; |
1216 | if (!sb) { | 1177 | if (!sb) { |
1217 | printk("ext2_new_blocks: nonexistent device"); | 1178 | printk("ext2_new_blocks: nonexistent device"); |
1218 | return 0; | 1179 | return 0; |
1219 | } | 1180 | } |
1220 | 1181 | ||
1221 | /* | 1182 | /* |
1222 | * Check quota for allocation of this block. | 1183 | * Check quota for allocation of this block. |
1223 | */ | 1184 | */ |
1224 | if (DQUOT_ALLOC_BLOCK(inode, num)) { | 1185 | if (DQUOT_ALLOC_BLOCK(inode, num)) { |
1225 | *errp = -EDQUOT; | 1186 | *errp = -EDQUOT; |
1226 | return 0; | 1187 | return 0; |
1227 | } | 1188 | } |
1228 | 1189 | ||
1229 | sbi = EXT2_SB(sb); | 1190 | sbi = EXT2_SB(sb); |
1230 | es = EXT2_SB(sb)->s_es; | 1191 | es = EXT2_SB(sb)->s_es; |
1231 | ext2_debug("goal=%lu.\n", goal); | 1192 | ext2_debug("goal=%lu.\n", goal); |
1232 | /* | 1193 | /* |
1233 | * Allocate a block from reservation only when | 1194 | * Allocate a block from reservation only when |
1234 | * filesystem is mounted with reservation(default,-o reservation), and | 1195 | * filesystem is mounted with reservation(default,-o reservation), and |
1235 | * it's a regular file, and | 1196 | * it's a regular file, and |
1236 | * the desired window size is greater than 0 (One could use ioctl | 1197 | * the desired window size is greater than 0 (One could use ioctl |
1237 | * command EXT2_IOC_SETRSVSZ to set the window size to 0 to turn off | 1198 | * command EXT2_IOC_SETRSVSZ to set the window size to 0 to turn off |
1238 | * reservation on that particular file) | 1199 | * reservation on that particular file) |
1239 | */ | 1200 | */ |
1240 | block_i = EXT2_I(inode)->i_block_alloc_info; | 1201 | block_i = EXT2_I(inode)->i_block_alloc_info; |
1241 | if (block_i) { | 1202 | if (block_i) { |
1242 | windowsz = block_i->rsv_window_node.rsv_goal_size; | 1203 | windowsz = block_i->rsv_window_node.rsv_goal_size; |
1243 | if (windowsz > 0) | 1204 | if (windowsz > 0) |
1244 | my_rsv = &block_i->rsv_window_node; | 1205 | my_rsv = &block_i->rsv_window_node; |
1245 | } | 1206 | } |
1246 | 1207 | ||
1247 | if (!ext2_has_free_blocks(sbi)) { | 1208 | if (!ext2_has_free_blocks(sbi)) { |
1248 | *errp = -ENOSPC; | 1209 | *errp = -ENOSPC; |
1249 | goto out; | 1210 | goto out; |
1250 | } | 1211 | } |
1251 | 1212 | ||
1252 | /* | 1213 | /* |
1253 | * First, test whether the goal block is free. | 1214 | * First, test whether the goal block is free. |
1254 | */ | 1215 | */ |
1255 | if (goal < le32_to_cpu(es->s_first_data_block) || | 1216 | if (goal < le32_to_cpu(es->s_first_data_block) || |
1256 | goal >= le32_to_cpu(es->s_blocks_count)) | 1217 | goal >= le32_to_cpu(es->s_blocks_count)) |
1257 | goal = le32_to_cpu(es->s_first_data_block); | 1218 | goal = le32_to_cpu(es->s_first_data_block); |
1258 | group_no = (goal - le32_to_cpu(es->s_first_data_block)) / | 1219 | group_no = (goal - le32_to_cpu(es->s_first_data_block)) / |
1259 | EXT2_BLOCKS_PER_GROUP(sb); | 1220 | EXT2_BLOCKS_PER_GROUP(sb); |
1260 | goal_group = group_no; | 1221 | goal_group = group_no; |
1261 | retry_alloc: | 1222 | retry_alloc: |
1262 | gdp = ext2_get_group_desc(sb, group_no, &gdp_bh); | 1223 | gdp = ext2_get_group_desc(sb, group_no, &gdp_bh); |
1263 | if (!gdp) | 1224 | if (!gdp) |
1264 | goto io_error; | 1225 | goto io_error; |
1265 | 1226 | ||
1266 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); | 1227 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); |
1267 | /* | 1228 | /* |
1268 | * if there is not enough free blocks to make a new resevation | 1229 | * if there is not enough free blocks to make a new resevation |
1269 | * turn off reservation for this allocation | 1230 | * turn off reservation for this allocation |
1270 | */ | 1231 | */ |
1271 | if (my_rsv && (free_blocks < windowsz) | 1232 | if (my_rsv && (free_blocks < windowsz) |
1272 | && (rsv_is_empty(&my_rsv->rsv_window))) | 1233 | && (rsv_is_empty(&my_rsv->rsv_window))) |
1273 | my_rsv = NULL; | 1234 | my_rsv = NULL; |
1274 | 1235 | ||
1275 | if (free_blocks > 0) { | 1236 | if (free_blocks > 0) { |
1276 | grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) % | 1237 | grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) % |
1277 | EXT2_BLOCKS_PER_GROUP(sb)); | 1238 | EXT2_BLOCKS_PER_GROUP(sb)); |
1278 | bitmap_bh = read_block_bitmap(sb, group_no); | 1239 | bitmap_bh = read_block_bitmap(sb, group_no); |
1279 | if (!bitmap_bh) | 1240 | if (!bitmap_bh) |
1280 | goto io_error; | 1241 | goto io_error; |
1281 | grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no, | 1242 | grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no, |
1282 | bitmap_bh, grp_target_blk, | 1243 | bitmap_bh, grp_target_blk, |
1283 | my_rsv, &num); | 1244 | my_rsv, &num); |
1284 | if (grp_alloc_blk >= 0) | 1245 | if (grp_alloc_blk >= 0) |
1285 | goto allocated; | 1246 | goto allocated; |
1286 | } | 1247 | } |
1287 | 1248 | ||
1288 | ngroups = EXT2_SB(sb)->s_groups_count; | 1249 | ngroups = EXT2_SB(sb)->s_groups_count; |
1289 | smp_rmb(); | 1250 | smp_rmb(); |
1290 | 1251 | ||
1291 | /* | 1252 | /* |
1292 | * Now search the rest of the groups. We assume that | 1253 | * Now search the rest of the groups. We assume that |
1293 | * i and gdp correctly point to the last group visited. | 1254 | * i and gdp correctly point to the last group visited. |
1294 | */ | 1255 | */ |
1295 | for (bgi = 0; bgi < ngroups; bgi++) { | 1256 | for (bgi = 0; bgi < ngroups; bgi++) { |
1296 | group_no++; | 1257 | group_no++; |
1297 | if (group_no >= ngroups) | 1258 | if (group_no >= ngroups) |
1298 | group_no = 0; | 1259 | group_no = 0; |
1299 | gdp = ext2_get_group_desc(sb, group_no, &gdp_bh); | 1260 | gdp = ext2_get_group_desc(sb, group_no, &gdp_bh); |
1300 | if (!gdp) | 1261 | if (!gdp) |
1301 | goto io_error; | 1262 | goto io_error; |
1302 | 1263 | ||
1303 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); | 1264 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); |
1304 | /* | 1265 | /* |
1305 | * skip this group if the number of | 1266 | * skip this group if the number of |
1306 | * free blocks is less than half of the reservation | 1267 | * free blocks is less than half of the reservation |
1307 | * window size. | 1268 | * window size. |
1308 | */ | 1269 | */ |
1309 | if (free_blocks <= (windowsz/2)) | 1270 | if (free_blocks <= (windowsz/2)) |
1310 | continue; | 1271 | continue; |
1311 | 1272 | ||
1312 | brelse(bitmap_bh); | 1273 | brelse(bitmap_bh); |
1313 | bitmap_bh = read_block_bitmap(sb, group_no); | 1274 | bitmap_bh = read_block_bitmap(sb, group_no); |
1314 | if (!bitmap_bh) | 1275 | if (!bitmap_bh) |
1315 | goto io_error; | 1276 | goto io_error; |
1316 | /* | 1277 | /* |
1317 | * try to allocate block(s) from this group, without a goal(-1). | 1278 | * try to allocate block(s) from this group, without a goal(-1). |
1318 | */ | 1279 | */ |
1319 | grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no, | 1280 | grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no, |
1320 | bitmap_bh, -1, my_rsv, &num); | 1281 | bitmap_bh, -1, my_rsv, &num); |
1321 | if (grp_alloc_blk >= 0) | 1282 | if (grp_alloc_blk >= 0) |
1322 | goto allocated; | 1283 | goto allocated; |
1323 | } | 1284 | } |
1324 | /* | 1285 | /* |
1325 | * We may end up a bogus ealier ENOSPC error due to | 1286 | * We may end up a bogus ealier ENOSPC error due to |
1326 | * filesystem is "full" of reservations, but | 1287 | * filesystem is "full" of reservations, but |
1327 | * there maybe indeed free blocks avaliable on disk | 1288 | * there maybe indeed free blocks avaliable on disk |
1328 | * In this case, we just forget about the reservations | 1289 | * In this case, we just forget about the reservations |
1329 | * just do block allocation as without reservations. | 1290 | * just do block allocation as without reservations. |
1330 | */ | 1291 | */ |
1331 | if (my_rsv) { | 1292 | if (my_rsv) { |
1332 | my_rsv = NULL; | 1293 | my_rsv = NULL; |
1333 | windowsz = 0; | 1294 | windowsz = 0; |
1334 | group_no = goal_group; | 1295 | group_no = goal_group; |
1335 | goto retry_alloc; | 1296 | goto retry_alloc; |
1336 | } | 1297 | } |
1337 | /* No space left on the device */ | 1298 | /* No space left on the device */ |
1338 | *errp = -ENOSPC; | 1299 | *errp = -ENOSPC; |
1339 | goto out; | 1300 | goto out; |
1340 | 1301 | ||
1341 | allocated: | 1302 | allocated: |
1342 | 1303 | ||
1343 | ext2_debug("using block group %d(%d)\n", | 1304 | ext2_debug("using block group %d(%d)\n", |
1344 | group_no, gdp->bg_free_blocks_count); | 1305 | group_no, gdp->bg_free_blocks_count); |
1345 | 1306 | ||
1346 | ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no); | 1307 | ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no); |
1347 | 1308 | ||
1348 | if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) || | 1309 | if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) || |
1349 | in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) || | 1310 | in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) || |
1350 | in_range(ret_block, le32_to_cpu(gdp->bg_inode_table), | 1311 | in_range(ret_block, le32_to_cpu(gdp->bg_inode_table), |
1351 | EXT2_SB(sb)->s_itb_per_group) || | 1312 | EXT2_SB(sb)->s_itb_per_group) || |
1352 | in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table), | 1313 | in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table), |
1353 | EXT2_SB(sb)->s_itb_per_group)) | 1314 | EXT2_SB(sb)->s_itb_per_group)) |
1354 | ext2_error(sb, "ext2_new_blocks", | 1315 | ext2_error(sb, "ext2_new_blocks", |
1355 | "Allocating block in system zone - " | 1316 | "Allocating block in system zone - " |
1356 | "blocks from "E2FSBLK", length %lu", | 1317 | "blocks from "E2FSBLK", length %lu", |
1357 | ret_block, num); | 1318 | ret_block, num); |
1358 | 1319 | ||
1359 | performed_allocation = 1; | 1320 | performed_allocation = 1; |
1360 | 1321 | ||
1361 | if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) { | 1322 | if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) { |
1362 | ext2_error(sb, "ext2_new_blocks", | 1323 | ext2_error(sb, "ext2_new_blocks", |
1363 | "block("E2FSBLK") >= blocks count(%d) - " | 1324 | "block("E2FSBLK") >= blocks count(%d) - " |
1364 | "block_group = %d, es == %p ", ret_block, | 1325 | "block_group = %d, es == %p ", ret_block, |
1365 | le32_to_cpu(es->s_blocks_count), group_no, es); | 1326 | le32_to_cpu(es->s_blocks_count), group_no, es); |
1366 | goto out; | 1327 | goto out; |
1367 | } | 1328 | } |
1368 | 1329 | ||
1369 | group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num); | 1330 | group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num); |
1370 | percpu_counter_sub(&sbi->s_freeblocks_counter, num); | 1331 | percpu_counter_sub(&sbi->s_freeblocks_counter, num); |
1371 | 1332 | ||
1372 | mark_buffer_dirty(bitmap_bh); | 1333 | mark_buffer_dirty(bitmap_bh); |
1373 | if (sb->s_flags & MS_SYNCHRONOUS) | 1334 | if (sb->s_flags & MS_SYNCHRONOUS) |
1374 | sync_dirty_buffer(bitmap_bh); | 1335 | sync_dirty_buffer(bitmap_bh); |
1375 | 1336 | ||
1376 | *errp = 0; | 1337 | *errp = 0; |
1377 | brelse(bitmap_bh); | 1338 | brelse(bitmap_bh); |
1378 | DQUOT_FREE_BLOCK(inode, *count-num); | 1339 | DQUOT_FREE_BLOCK(inode, *count-num); |
1379 | *count = num; | 1340 | *count = num; |
1380 | return ret_block; | 1341 | return ret_block; |
1381 | 1342 | ||
1382 | io_error: | 1343 | io_error: |
1383 | *errp = -EIO; | 1344 | *errp = -EIO; |
1384 | out: | 1345 | out: |
1385 | /* | 1346 | /* |
1386 | * Undo the block allocation | 1347 | * Undo the block allocation |
1387 | */ | 1348 | */ |
1388 | if (!performed_allocation) | 1349 | if (!performed_allocation) |
1389 | DQUOT_FREE_BLOCK(inode, *count); | 1350 | DQUOT_FREE_BLOCK(inode, *count); |
1390 | brelse(bitmap_bh); | 1351 | brelse(bitmap_bh); |
1391 | return 0; | 1352 | return 0; |
1392 | } | 1353 | } |
1393 | 1354 | ||
1394 | ext2_fsblk_t ext2_new_block(struct inode *inode, unsigned long goal, int *errp) | 1355 | ext2_fsblk_t ext2_new_block(struct inode *inode, unsigned long goal, int *errp) |
1395 | { | 1356 | { |
1396 | unsigned long count = 1; | 1357 | unsigned long count = 1; |
1397 | 1358 | ||
1398 | return ext2_new_blocks(inode, goal, &count, errp); | 1359 | return ext2_new_blocks(inode, goal, &count, errp); |
1399 | } | 1360 | } |
1400 | 1361 | ||
1401 | #ifdef EXT2FS_DEBUG | 1362 | #ifdef EXT2FS_DEBUG |
1402 | 1363 | ||
1403 | static const int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0}; | 1364 | static const int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0}; |
1404 | 1365 | ||
1405 | unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars) | 1366 | unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars) |
1406 | { | 1367 | { |
1407 | unsigned int i; | 1368 | unsigned int i; |
1408 | unsigned long sum = 0; | 1369 | unsigned long sum = 0; |
1409 | 1370 | ||
1410 | if (!map) | 1371 | if (!map) |
1411 | return (0); | 1372 | return (0); |
1412 | for (i = 0; i < numchars; i++) | 1373 | for (i = 0; i < numchars; i++) |
1413 | sum += nibblemap[map->b_data[i] & 0xf] + | 1374 | sum += nibblemap[map->b_data[i] & 0xf] + |
1414 | nibblemap[(map->b_data[i] >> 4) & 0xf]; | 1375 | nibblemap[(map->b_data[i] >> 4) & 0xf]; |
1415 | return (sum); | 1376 | return (sum); |
1416 | } | 1377 | } |
1417 | 1378 | ||
1418 | #endif /* EXT2FS_DEBUG */ | 1379 | #endif /* EXT2FS_DEBUG */ |
1419 | 1380 | ||
1420 | unsigned long ext2_count_free_blocks (struct super_block * sb) | 1381 | unsigned long ext2_count_free_blocks (struct super_block * sb) |
1421 | { | 1382 | { |
1422 | struct ext2_group_desc * desc; | 1383 | struct ext2_group_desc * desc; |
1423 | unsigned long desc_count = 0; | 1384 | unsigned long desc_count = 0; |
1424 | int i; | 1385 | int i; |
1425 | #ifdef EXT2FS_DEBUG | 1386 | #ifdef EXT2FS_DEBUG |
1426 | unsigned long bitmap_count, x; | 1387 | unsigned long bitmap_count, x; |
1427 | struct ext2_super_block *es; | 1388 | struct ext2_super_block *es; |
1428 | 1389 | ||
1429 | es = EXT2_SB(sb)->s_es; | 1390 | es = EXT2_SB(sb)->s_es; |
1430 | desc_count = 0; | 1391 | desc_count = 0; |
1431 | bitmap_count = 0; | 1392 | bitmap_count = 0; |
1432 | desc = NULL; | 1393 | desc = NULL; |
1433 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { | 1394 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { |
1434 | struct buffer_head *bitmap_bh; | 1395 | struct buffer_head *bitmap_bh; |
1435 | desc = ext2_get_group_desc (sb, i, NULL); | 1396 | desc = ext2_get_group_desc (sb, i, NULL); |
1436 | if (!desc) | 1397 | if (!desc) |
1437 | continue; | 1398 | continue; |
1438 | desc_count += le16_to_cpu(desc->bg_free_blocks_count); | 1399 | desc_count += le16_to_cpu(desc->bg_free_blocks_count); |
1439 | bitmap_bh = read_block_bitmap(sb, i); | 1400 | bitmap_bh = read_block_bitmap(sb, i); |
1440 | if (!bitmap_bh) | 1401 | if (!bitmap_bh) |
1441 | continue; | 1402 | continue; |
1442 | 1403 | ||
1443 | x = ext2_count_free(bitmap_bh, sb->s_blocksize); | 1404 | x = ext2_count_free(bitmap_bh, sb->s_blocksize); |
1444 | printk ("group %d: stored = %d, counted = %lu\n", | 1405 | printk ("group %d: stored = %d, counted = %lu\n", |
1445 | i, le16_to_cpu(desc->bg_free_blocks_count), x); | 1406 | i, le16_to_cpu(desc->bg_free_blocks_count), x); |
1446 | bitmap_count += x; | 1407 | bitmap_count += x; |
1447 | brelse(bitmap_bh); | 1408 | brelse(bitmap_bh); |
1448 | } | 1409 | } |
1449 | printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n", | 1410 | printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n", |
1450 | (long)le32_to_cpu(es->s_free_blocks_count), | 1411 | (long)le32_to_cpu(es->s_free_blocks_count), |
1451 | desc_count, bitmap_count); | 1412 | desc_count, bitmap_count); |
1452 | return bitmap_count; | 1413 | return bitmap_count; |
1453 | #else | 1414 | #else |
1454 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { | 1415 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { |
1455 | desc = ext2_get_group_desc (sb, i, NULL); | 1416 | desc = ext2_get_group_desc (sb, i, NULL); |
1456 | if (!desc) | 1417 | if (!desc) |
1457 | continue; | 1418 | continue; |
1458 | desc_count += le16_to_cpu(desc->bg_free_blocks_count); | 1419 | desc_count += le16_to_cpu(desc->bg_free_blocks_count); |
1459 | } | 1420 | } |
1460 | return desc_count; | 1421 | return desc_count; |
1461 | #endif | 1422 | #endif |
1462 | } | 1423 | } |
1463 | |||
1464 | 1424 | ||
1465 | static inline int test_root(int a, int b) | 1425 | static inline int test_root(int a, int b) |
1466 | { | 1426 | { |
1467 | int num = b; | 1427 | int num = b; |
1468 | 1428 | ||
1469 | while (a > num) | 1429 | while (a > num) |
1470 | num *= b; | 1430 | num *= b; |
1471 | return num == a; | 1431 | return num == a; |
1472 | } | 1432 | } |
1473 | 1433 | ||
1474 | static int ext2_group_sparse(int group) | 1434 | static int ext2_group_sparse(int group) |
1475 | { | 1435 | { |
1476 | if (group <= 1) | 1436 | if (group <= 1) |
1477 | return 1; | 1437 | return 1; |
1478 | return (test_root(group, 3) || test_root(group, 5) || | 1438 | return (test_root(group, 3) || test_root(group, 5) || |
1479 | test_root(group, 7)); | 1439 | test_root(group, 7)); |
1480 | } | 1440 | } |
1481 | 1441 | ||
1482 | /** | 1442 | /** |
1483 | * ext2_bg_has_super - number of blocks used by the superblock in group | 1443 | * ext2_bg_has_super - number of blocks used by the superblock in group |
1484 | * @sb: superblock for filesystem | 1444 | * @sb: superblock for filesystem |
1485 | * @group: group number to check | 1445 | * @group: group number to check |
1486 | * | 1446 | * |
1487 | * Return the number of blocks used by the superblock (primary or backup) | 1447 | * Return the number of blocks used by the superblock (primary or backup) |
1488 | * in this group. Currently this will be only 0 or 1. | 1448 | * in this group. Currently this will be only 0 or 1. |
1489 | */ | 1449 | */ |
1490 | int ext2_bg_has_super(struct super_block *sb, int group) | 1450 | int ext2_bg_has_super(struct super_block *sb, int group) |
1491 | { | 1451 | { |
1492 | if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&& | 1452 | if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&& |
1493 | !ext2_group_sparse(group)) | 1453 | !ext2_group_sparse(group)) |
1494 | return 0; | 1454 | return 0; |
1495 | return 1; | 1455 | return 1; |
1496 | } | 1456 | } |
1497 | 1457 | ||
1498 | /** | 1458 | /** |
1499 | * ext2_bg_num_gdb - number of blocks used by the group table in group | 1459 | * ext2_bg_num_gdb - number of blocks used by the group table in group |
1500 | * @sb: superblock for filesystem | 1460 | * @sb: superblock for filesystem |
1501 | * @group: group number to check | 1461 | * @group: group number to check |
1502 | * | 1462 | * |
1503 | * Return the number of blocks used by the group descriptor table | 1463 | * Return the number of blocks used by the group descriptor table |
1504 | * (primary or backup) in this group. In the future there may be a | 1464 | * (primary or backup) in this group. In the future there may be a |
1505 | * different number of descriptor blocks in each group. | 1465 | * different number of descriptor blocks in each group. |
1506 | */ | 1466 | */ |
1507 | unsigned long ext2_bg_num_gdb(struct super_block *sb, int group) | 1467 | unsigned long ext2_bg_num_gdb(struct super_block *sb, int group) |
1508 | { | 1468 | { |
1509 | if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&& | 1469 | if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&& |
1510 | !ext2_group_sparse(group)) | 1470 | !ext2_group_sparse(group)) |
1511 | return 0; | 1471 | return 0; |
1512 | return EXT2_SB(sb)->s_gdb_count; | 1472 | return EXT2_SB(sb)->s_gdb_count; |
1513 | } | 1473 | } |
1514 | 1474 | ||
1515 | 1475 |
fs/ext3/balloc.c
1 | /* | 1 | /* |
2 | * linux/fs/ext3/balloc.c | 2 | * linux/fs/ext3/balloc.c |
3 | * | 3 | * |
4 | * Copyright (C) 1992, 1993, 1994, 1995 | 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
5 | * Remy Card (card@masi.ibp.fr) | 5 | * Remy Card (card@masi.ibp.fr) |
6 | * Laboratoire MASI - Institut Blaise Pascal | 6 | * Laboratoire MASI - Institut Blaise Pascal |
7 | * Universite Pierre et Marie Curie (Paris VI) | 7 | * Universite Pierre et Marie Curie (Paris VI) |
8 | * | 8 | * |
9 | * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993 | 9 | * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993 |
10 | * Big-endian to little-endian byte-swapping/bitmaps by | 10 | * Big-endian to little-endian byte-swapping/bitmaps by |
11 | * David S. Miller (davem@caip.rutgers.edu), 1995 | 11 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include <linux/time.h> | 14 | #include <linux/time.h> |
15 | #include <linux/capability.h> | 15 | #include <linux/capability.h> |
16 | #include <linux/fs.h> | 16 | #include <linux/fs.h> |
17 | #include <linux/jbd.h> | 17 | #include <linux/jbd.h> |
18 | #include <linux/ext3_fs.h> | 18 | #include <linux/ext3_fs.h> |
19 | #include <linux/ext3_jbd.h> | 19 | #include <linux/ext3_jbd.h> |
20 | #include <linux/quotaops.h> | 20 | #include <linux/quotaops.h> |
21 | #include <linux/buffer_head.h> | 21 | #include <linux/buffer_head.h> |
22 | 22 | ||
23 | /* | 23 | /* |
24 | * balloc.c contains the blocks allocation and deallocation routines | 24 | * balloc.c contains the blocks allocation and deallocation routines |
25 | */ | 25 | */ |
26 | 26 | ||
27 | /* | 27 | /* |
28 | * The free blocks are managed by bitmaps. A file system contains several | 28 | * The free blocks are managed by bitmaps. A file system contains several |
29 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap | 29 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap |
30 | * block for inodes, N blocks for the inode table and data blocks. | 30 | * block for inodes, N blocks for the inode table and data blocks. |
31 | * | 31 | * |
32 | * The file system contains group descriptors which are located after the | 32 | * The file system contains group descriptors which are located after the |
33 | * super block. Each descriptor contains the number of the bitmap block and | 33 | * super block. Each descriptor contains the number of the bitmap block and |
34 | * the free blocks count in the block. The descriptors are loaded in memory | 34 | * the free blocks count in the block. The descriptors are loaded in memory |
35 | * when a file system is mounted (see ext3_fill_super). | 35 | * when a file system is mounted (see ext3_fill_super). |
36 | */ | 36 | */ |
37 | 37 | ||
38 | 38 | ||
39 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) | 39 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) |
40 | 40 | ||
41 | /** | 41 | /** |
42 | * ext3_get_group_desc() -- load group descriptor from disk | 42 | * ext3_get_group_desc() -- load group descriptor from disk |
43 | * @sb: super block | 43 | * @sb: super block |
44 | * @block_group: given block group | 44 | * @block_group: given block group |
45 | * @bh: pointer to the buffer head to store the block | 45 | * @bh: pointer to the buffer head to store the block |
46 | * group descriptor | 46 | * group descriptor |
47 | */ | 47 | */ |
48 | struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb, | 48 | struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb, |
49 | unsigned int block_group, | 49 | unsigned int block_group, |
50 | struct buffer_head ** bh) | 50 | struct buffer_head ** bh) |
51 | { | 51 | { |
52 | unsigned long group_desc; | 52 | unsigned long group_desc; |
53 | unsigned long offset; | 53 | unsigned long offset; |
54 | struct ext3_group_desc * desc; | 54 | struct ext3_group_desc * desc; |
55 | struct ext3_sb_info *sbi = EXT3_SB(sb); | 55 | struct ext3_sb_info *sbi = EXT3_SB(sb); |
56 | 56 | ||
57 | if (block_group >= sbi->s_groups_count) { | 57 | if (block_group >= sbi->s_groups_count) { |
58 | ext3_error (sb, "ext3_get_group_desc", | 58 | ext3_error (sb, "ext3_get_group_desc", |
59 | "block_group >= groups_count - " | 59 | "block_group >= groups_count - " |
60 | "block_group = %d, groups_count = %lu", | 60 | "block_group = %d, groups_count = %lu", |
61 | block_group, sbi->s_groups_count); | 61 | block_group, sbi->s_groups_count); |
62 | 62 | ||
63 | return NULL; | 63 | return NULL; |
64 | } | 64 | } |
65 | smp_rmb(); | 65 | smp_rmb(); |
66 | 66 | ||
67 | group_desc = block_group >> EXT3_DESC_PER_BLOCK_BITS(sb); | 67 | group_desc = block_group >> EXT3_DESC_PER_BLOCK_BITS(sb); |
68 | offset = block_group & (EXT3_DESC_PER_BLOCK(sb) - 1); | 68 | offset = block_group & (EXT3_DESC_PER_BLOCK(sb) - 1); |
69 | if (!sbi->s_group_desc[group_desc]) { | 69 | if (!sbi->s_group_desc[group_desc]) { |
70 | ext3_error (sb, "ext3_get_group_desc", | 70 | ext3_error (sb, "ext3_get_group_desc", |
71 | "Group descriptor not loaded - " | 71 | "Group descriptor not loaded - " |
72 | "block_group = %d, group_desc = %lu, desc = %lu", | 72 | "block_group = %d, group_desc = %lu, desc = %lu", |
73 | block_group, group_desc, offset); | 73 | block_group, group_desc, offset); |
74 | return NULL; | 74 | return NULL; |
75 | } | 75 | } |
76 | 76 | ||
77 | desc = (struct ext3_group_desc *) sbi->s_group_desc[group_desc]->b_data; | 77 | desc = (struct ext3_group_desc *) sbi->s_group_desc[group_desc]->b_data; |
78 | if (bh) | 78 | if (bh) |
79 | *bh = sbi->s_group_desc[group_desc]; | 79 | *bh = sbi->s_group_desc[group_desc]; |
80 | return desc + offset; | 80 | return desc + offset; |
81 | } | 81 | } |
82 | 82 | ||
83 | static inline int | ||
84 | block_in_use(ext3_fsblk_t block, struct super_block *sb, unsigned char *map) | ||
85 | { | ||
86 | return ext3_test_bit ((block - | ||
87 | le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) % | ||
88 | EXT3_BLOCKS_PER_GROUP(sb), map); | ||
89 | } | ||
90 | |||
91 | /** | 83 | /** |
92 | * read_block_bitmap() | 84 | * read_block_bitmap() |
93 | * @sb: super block | 85 | * @sb: super block |
94 | * @block_group: given block group | 86 | * @block_group: given block group |
95 | * | 87 | * |
96 | * Read the bitmap for a given block_group, reading into the specified | 88 | * Read the bitmap for a given block_group, reading into the specified |
97 | * slot in the superblock's bitmap cache. | 89 | * slot in the superblock's bitmap cache. |
98 | * | 90 | * |
99 | * Return buffer_head on success or NULL in case of failure. | 91 | * Return buffer_head on success or NULL in case of failure. |
100 | */ | 92 | */ |
101 | static struct buffer_head * | 93 | static struct buffer_head * |
102 | read_block_bitmap(struct super_block *sb, unsigned int block_group) | 94 | read_block_bitmap(struct super_block *sb, unsigned int block_group) |
103 | { | 95 | { |
104 | int i; | ||
105 | struct ext3_group_desc * desc; | 96 | struct ext3_group_desc * desc; |
106 | struct buffer_head * bh = NULL; | 97 | struct buffer_head * bh = NULL; |
107 | ext3_fsblk_t bitmap_blk; | ||
108 | 98 | ||
109 | desc = ext3_get_group_desc (sb, block_group, NULL); | 99 | desc = ext3_get_group_desc (sb, block_group, NULL); |
110 | if (!desc) | 100 | if (!desc) |
111 | return NULL; | 101 | goto error_out; |
112 | bitmap_blk = le32_to_cpu(desc->bg_block_bitmap); | 102 | bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap)); |
113 | bh = sb_bread(sb, bitmap_blk); | ||
114 | if (!bh) | 103 | if (!bh) |
115 | ext3_error (sb, __FUNCTION__, | 104 | ext3_error (sb, "read_block_bitmap", |
116 | "Cannot read block bitmap - " | 105 | "Cannot read block bitmap - " |
117 | "block_group = %d, block_bitmap = %u", | 106 | "block_group = %d, block_bitmap = %u", |
118 | block_group, le32_to_cpu(desc->bg_block_bitmap)); | 107 | block_group, le32_to_cpu(desc->bg_block_bitmap)); |
119 | |||
120 | /* check whether block bitmap block number is set */ | ||
121 | if (!block_in_use(bitmap_blk, sb, bh->b_data)) { | ||
122 | /* bad block bitmap */ | ||
123 | goto error_out; | ||
124 | } | ||
125 | /* check whether the inode bitmap block number is set */ | ||
126 | bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap); | ||
127 | if (!block_in_use(bitmap_blk, sb, bh->b_data)) { | ||
128 | /* bad block bitmap */ | ||
129 | goto error_out; | ||
130 | } | ||
131 | /* check whether the inode table block number is set */ | ||
132 | bitmap_blk = le32_to_cpu(desc->bg_inode_table); | ||
133 | for (i = 0; i < EXT3_SB(sb)->s_itb_per_group; i++, bitmap_blk++) { | ||
134 | if (!block_in_use(bitmap_blk, sb, bh->b_data)) { | ||
135 | /* bad block bitmap */ | ||
136 | goto error_out; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | return bh; | ||
141 | |||
142 | error_out: | 108 | error_out: |
143 | brelse(bh); | 109 | return bh; |
144 | ext3_error(sb, __FUNCTION__, | ||
145 | "Invalid block bitmap - " | ||
146 | "block_group = %d, block = %lu", | ||
147 | block_group, bitmap_blk); | ||
148 | return NULL; | ||
149 | } | 110 | } |
150 | /* | 111 | /* |
151 | * The reservation window structure operations | 112 | * The reservation window structure operations |
152 | * -------------------------------------------- | 113 | * -------------------------------------------- |
153 | * Operations include: | 114 | * Operations include: |
154 | * dump, find, add, remove, is_empty, find_next_reservable_window, etc. | 115 | * dump, find, add, remove, is_empty, find_next_reservable_window, etc. |
155 | * | 116 | * |
156 | * We use a red-black tree to represent per-filesystem reservation | 117 | * We use a red-black tree to represent per-filesystem reservation |
157 | * windows. | 118 | * windows. |
158 | * | 119 | * |
159 | */ | 120 | */ |
160 | 121 | ||
161 | /** | 122 | /** |
162 | * __rsv_window_dump() -- Dump the filesystem block allocation reservation map | 123 | * __rsv_window_dump() -- Dump the filesystem block allocation reservation map |
163 | * @rb_root: root of per-filesystem reservation rb tree | 124 | * @rb_root: root of per-filesystem reservation rb tree |
164 | * @verbose: verbose mode | 125 | * @verbose: verbose mode |
165 | * @fn: function which wishes to dump the reservation map | 126 | * @fn: function which wishes to dump the reservation map |
166 | * | 127 | * |
167 | * If verbose is turned on, it will print the whole block reservation | 128 | * If verbose is turned on, it will print the whole block reservation |
168 | * windows(start, end). Otherwise, it will only print out the "bad" windows, | 129 | * windows(start, end). Otherwise, it will only print out the "bad" windows, |
169 | * those windows that overlap with their immediate neighbors. | 130 | * those windows that overlap with their immediate neighbors. |
170 | */ | 131 | */ |
171 | #if 1 | 132 | #if 1 |
172 | static void __rsv_window_dump(struct rb_root *root, int verbose, | 133 | static void __rsv_window_dump(struct rb_root *root, int verbose, |
173 | const char *fn) | 134 | const char *fn) |
174 | { | 135 | { |
175 | struct rb_node *n; | 136 | struct rb_node *n; |
176 | struct ext3_reserve_window_node *rsv, *prev; | 137 | struct ext3_reserve_window_node *rsv, *prev; |
177 | int bad; | 138 | int bad; |
178 | 139 | ||
179 | restart: | 140 | restart: |
180 | n = rb_first(root); | 141 | n = rb_first(root); |
181 | bad = 0; | 142 | bad = 0; |
182 | prev = NULL; | 143 | prev = NULL; |
183 | 144 | ||
184 | printk("Block Allocation Reservation Windows Map (%s):\n", fn); | 145 | printk("Block Allocation Reservation Windows Map (%s):\n", fn); |
185 | while (n) { | 146 | while (n) { |
186 | rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node); | 147 | rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node); |
187 | if (verbose) | 148 | if (verbose) |
188 | printk("reservation window 0x%p " | 149 | printk("reservation window 0x%p " |
189 | "start: %lu, end: %lu\n", | 150 | "start: %lu, end: %lu\n", |
190 | rsv, rsv->rsv_start, rsv->rsv_end); | 151 | rsv, rsv->rsv_start, rsv->rsv_end); |
191 | if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) { | 152 | if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) { |
192 | printk("Bad reservation %p (start >= end)\n", | 153 | printk("Bad reservation %p (start >= end)\n", |
193 | rsv); | 154 | rsv); |
194 | bad = 1; | 155 | bad = 1; |
195 | } | 156 | } |
196 | if (prev && prev->rsv_end >= rsv->rsv_start) { | 157 | if (prev && prev->rsv_end >= rsv->rsv_start) { |
197 | printk("Bad reservation %p (prev->end >= start)\n", | 158 | printk("Bad reservation %p (prev->end >= start)\n", |
198 | rsv); | 159 | rsv); |
199 | bad = 1; | 160 | bad = 1; |
200 | } | 161 | } |
201 | if (bad) { | 162 | if (bad) { |
202 | if (!verbose) { | 163 | if (!verbose) { |
203 | printk("Restarting reservation walk in verbose mode\n"); | 164 | printk("Restarting reservation walk in verbose mode\n"); |
204 | verbose = 1; | 165 | verbose = 1; |
205 | goto restart; | 166 | goto restart; |
206 | } | 167 | } |
207 | } | 168 | } |
208 | n = rb_next(n); | 169 | n = rb_next(n); |
209 | prev = rsv; | 170 | prev = rsv; |
210 | } | 171 | } |
211 | printk("Window map complete.\n"); | 172 | printk("Window map complete.\n"); |
212 | if (bad) | 173 | if (bad) |
213 | BUG(); | 174 | BUG(); |
214 | } | 175 | } |
215 | #define rsv_window_dump(root, verbose) \ | 176 | #define rsv_window_dump(root, verbose) \ |
216 | __rsv_window_dump((root), (verbose), __FUNCTION__) | 177 | __rsv_window_dump((root), (verbose), __FUNCTION__) |
217 | #else | 178 | #else |
218 | #define rsv_window_dump(root, verbose) do {} while (0) | 179 | #define rsv_window_dump(root, verbose) do {} while (0) |
219 | #endif | 180 | #endif |
220 | 181 | ||
221 | /** | 182 | /** |
222 | * goal_in_my_reservation() | 183 | * goal_in_my_reservation() |
223 | * @rsv: inode's reservation window | 184 | * @rsv: inode's reservation window |
224 | * @grp_goal: given goal block relative to the allocation block group | 185 | * @grp_goal: given goal block relative to the allocation block group |
225 | * @group: the current allocation block group | 186 | * @group: the current allocation block group |
226 | * @sb: filesystem super block | 187 | * @sb: filesystem super block |
227 | * | 188 | * |
228 | * Test if the given goal block (group relative) is within the file's | 189 | * Test if the given goal block (group relative) is within the file's |
229 | * own block reservation window range. | 190 | * own block reservation window range. |
230 | * | 191 | * |
231 | * If the reservation window is outside the goal allocation group, return 0; | 192 | * If the reservation window is outside the goal allocation group, return 0; |
232 | * grp_goal (given goal block) could be -1, which means no specific | 193 | * grp_goal (given goal block) could be -1, which means no specific |
233 | * goal block. In this case, always return 1. | 194 | * goal block. In this case, always return 1. |
234 | * If the goal block is within the reservation window, return 1; | 195 | * If the goal block is within the reservation window, return 1; |
235 | * otherwise, return 0; | 196 | * otherwise, return 0; |
236 | */ | 197 | */ |
237 | static int | 198 | static int |
238 | goal_in_my_reservation(struct ext3_reserve_window *rsv, ext3_grpblk_t grp_goal, | 199 | goal_in_my_reservation(struct ext3_reserve_window *rsv, ext3_grpblk_t grp_goal, |
239 | unsigned int group, struct super_block * sb) | 200 | unsigned int group, struct super_block * sb) |
240 | { | 201 | { |
241 | ext3_fsblk_t group_first_block, group_last_block; | 202 | ext3_fsblk_t group_first_block, group_last_block; |
242 | 203 | ||
243 | group_first_block = ext3_group_first_block_no(sb, group); | 204 | group_first_block = ext3_group_first_block_no(sb, group); |
244 | group_last_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1); | 205 | group_last_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1); |
245 | 206 | ||
246 | if ((rsv->_rsv_start > group_last_block) || | 207 | if ((rsv->_rsv_start > group_last_block) || |
247 | (rsv->_rsv_end < group_first_block)) | 208 | (rsv->_rsv_end < group_first_block)) |
248 | return 0; | 209 | return 0; |
249 | if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start) | 210 | if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start) |
250 | || (grp_goal + group_first_block > rsv->_rsv_end))) | 211 | || (grp_goal + group_first_block > rsv->_rsv_end))) |
251 | return 0; | 212 | return 0; |
252 | return 1; | 213 | return 1; |
253 | } | 214 | } |
254 | 215 | ||
255 | /** | 216 | /** |
256 | * search_reserve_window() | 217 | * search_reserve_window() |
257 | * @rb_root: root of reservation tree | 218 | * @rb_root: root of reservation tree |
258 | * @goal: target allocation block | 219 | * @goal: target allocation block |
259 | * | 220 | * |
260 | * Find the reserved window which includes the goal, or the previous one | 221 | * Find the reserved window which includes the goal, or the previous one |
261 | * if the goal is not in any window. | 222 | * if the goal is not in any window. |
262 | * Returns NULL if there are no windows or if all windows start after the goal. | 223 | * Returns NULL if there are no windows or if all windows start after the goal. |
263 | */ | 224 | */ |
264 | static struct ext3_reserve_window_node * | 225 | static struct ext3_reserve_window_node * |
265 | search_reserve_window(struct rb_root *root, ext3_fsblk_t goal) | 226 | search_reserve_window(struct rb_root *root, ext3_fsblk_t goal) |
266 | { | 227 | { |
267 | struct rb_node *n = root->rb_node; | 228 | struct rb_node *n = root->rb_node; |
268 | struct ext3_reserve_window_node *rsv; | 229 | struct ext3_reserve_window_node *rsv; |
269 | 230 | ||
270 | if (!n) | 231 | if (!n) |
271 | return NULL; | 232 | return NULL; |
272 | 233 | ||
273 | do { | 234 | do { |
274 | rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node); | 235 | rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node); |
275 | 236 | ||
276 | if (goal < rsv->rsv_start) | 237 | if (goal < rsv->rsv_start) |
277 | n = n->rb_left; | 238 | n = n->rb_left; |
278 | else if (goal > rsv->rsv_end) | 239 | else if (goal > rsv->rsv_end) |
279 | n = n->rb_right; | 240 | n = n->rb_right; |
280 | else | 241 | else |
281 | return rsv; | 242 | return rsv; |
282 | } while (n); | 243 | } while (n); |
283 | /* | 244 | /* |
284 | * We've fallen off the end of the tree: the goal wasn't inside | 245 | * We've fallen off the end of the tree: the goal wasn't inside |
285 | * any particular node. OK, the previous node must be to one | 246 | * any particular node. OK, the previous node must be to one |
286 | * side of the interval containing the goal. If it's the RHS, | 247 | * side of the interval containing the goal. If it's the RHS, |
287 | * we need to back up one. | 248 | * we need to back up one. |
288 | */ | 249 | */ |
289 | if (rsv->rsv_start > goal) { | 250 | if (rsv->rsv_start > goal) { |
290 | n = rb_prev(&rsv->rsv_node); | 251 | n = rb_prev(&rsv->rsv_node); |
291 | rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node); | 252 | rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node); |
292 | } | 253 | } |
293 | return rsv; | 254 | return rsv; |
294 | } | 255 | } |
295 | 256 | ||
296 | /** | 257 | /** |
297 | * ext3_rsv_window_add() -- Insert a window to the block reservation rb tree. | 258 | * ext3_rsv_window_add() -- Insert a window to the block reservation rb tree. |
298 | * @sb: super block | 259 | * @sb: super block |
299 | * @rsv: reservation window to add | 260 | * @rsv: reservation window to add |
300 | * | 261 | * |
301 | * Must be called with rsv_lock hold. | 262 | * Must be called with rsv_lock hold. |
302 | */ | 263 | */ |
303 | void ext3_rsv_window_add(struct super_block *sb, | 264 | void ext3_rsv_window_add(struct super_block *sb, |
304 | struct ext3_reserve_window_node *rsv) | 265 | struct ext3_reserve_window_node *rsv) |
305 | { | 266 | { |
306 | struct rb_root *root = &EXT3_SB(sb)->s_rsv_window_root; | 267 | struct rb_root *root = &EXT3_SB(sb)->s_rsv_window_root; |
307 | struct rb_node *node = &rsv->rsv_node; | 268 | struct rb_node *node = &rsv->rsv_node; |
308 | ext3_fsblk_t start = rsv->rsv_start; | 269 | ext3_fsblk_t start = rsv->rsv_start; |
309 | 270 | ||
310 | struct rb_node ** p = &root->rb_node; | 271 | struct rb_node ** p = &root->rb_node; |
311 | struct rb_node * parent = NULL; | 272 | struct rb_node * parent = NULL; |
312 | struct ext3_reserve_window_node *this; | 273 | struct ext3_reserve_window_node *this; |
313 | 274 | ||
314 | while (*p) | 275 | while (*p) |
315 | { | 276 | { |
316 | parent = *p; | 277 | parent = *p; |
317 | this = rb_entry(parent, struct ext3_reserve_window_node, rsv_node); | 278 | this = rb_entry(parent, struct ext3_reserve_window_node, rsv_node); |
318 | 279 | ||
319 | if (start < this->rsv_start) | 280 | if (start < this->rsv_start) |
320 | p = &(*p)->rb_left; | 281 | p = &(*p)->rb_left; |
321 | else if (start > this->rsv_end) | 282 | else if (start > this->rsv_end) |
322 | p = &(*p)->rb_right; | 283 | p = &(*p)->rb_right; |
323 | else { | 284 | else { |
324 | rsv_window_dump(root, 1); | 285 | rsv_window_dump(root, 1); |
325 | BUG(); | 286 | BUG(); |
326 | } | 287 | } |
327 | } | 288 | } |
328 | 289 | ||
329 | rb_link_node(node, parent, p); | 290 | rb_link_node(node, parent, p); |
330 | rb_insert_color(node, root); | 291 | rb_insert_color(node, root); |
331 | } | 292 | } |
332 | 293 | ||
333 | /** | 294 | /** |
334 | * ext3_rsv_window_remove() -- unlink a window from the reservation rb tree | 295 | * ext3_rsv_window_remove() -- unlink a window from the reservation rb tree |
335 | * @sb: super block | 296 | * @sb: super block |
336 | * @rsv: reservation window to remove | 297 | * @rsv: reservation window to remove |
337 | * | 298 | * |
338 | * Mark the block reservation window as not allocated, and unlink it | 299 | * Mark the block reservation window as not allocated, and unlink it |
339 | * from the filesystem reservation window rb tree. Must be called with | 300 | * from the filesystem reservation window rb tree. Must be called with |
340 | * rsv_lock hold. | 301 | * rsv_lock hold. |
341 | */ | 302 | */ |
342 | static void rsv_window_remove(struct super_block *sb, | 303 | static void rsv_window_remove(struct super_block *sb, |
343 | struct ext3_reserve_window_node *rsv) | 304 | struct ext3_reserve_window_node *rsv) |
344 | { | 305 | { |
345 | rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED; | 306 | rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED; |
346 | rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED; | 307 | rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED; |
347 | rsv->rsv_alloc_hit = 0; | 308 | rsv->rsv_alloc_hit = 0; |
348 | rb_erase(&rsv->rsv_node, &EXT3_SB(sb)->s_rsv_window_root); | 309 | rb_erase(&rsv->rsv_node, &EXT3_SB(sb)->s_rsv_window_root); |
349 | } | 310 | } |
350 | 311 | ||
351 | /* | 312 | /* |
352 | * rsv_is_empty() -- Check if the reservation window is allocated. | 313 | * rsv_is_empty() -- Check if the reservation window is allocated. |
353 | * @rsv: given reservation window to check | 314 | * @rsv: given reservation window to check |
354 | * | 315 | * |
355 | * returns 1 if the end block is EXT3_RESERVE_WINDOW_NOT_ALLOCATED. | 316 | * returns 1 if the end block is EXT3_RESERVE_WINDOW_NOT_ALLOCATED. |
356 | */ | 317 | */ |
357 | static inline int rsv_is_empty(struct ext3_reserve_window *rsv) | 318 | static inline int rsv_is_empty(struct ext3_reserve_window *rsv) |
358 | { | 319 | { |
359 | /* a valid reservation end block could not be 0 */ | 320 | /* a valid reservation end block could not be 0 */ |
360 | return rsv->_rsv_end == EXT3_RESERVE_WINDOW_NOT_ALLOCATED; | 321 | return rsv->_rsv_end == EXT3_RESERVE_WINDOW_NOT_ALLOCATED; |
361 | } | 322 | } |
362 | 323 | ||
363 | /** | 324 | /** |
364 | * ext3_init_block_alloc_info() | 325 | * ext3_init_block_alloc_info() |
365 | * @inode: file inode structure | 326 | * @inode: file inode structure |
366 | * | 327 | * |
367 | * Allocate and initialize the reservation window structure, and | 328 | * Allocate and initialize the reservation window structure, and |
368 | * link the window to the ext3 inode structure at last | 329 | * link the window to the ext3 inode structure at last |
369 | * | 330 | * |
370 | * The reservation window structure is only dynamically allocated | 331 | * The reservation window structure is only dynamically allocated |
371 | * and linked to ext3 inode the first time the open file | 332 | * and linked to ext3 inode the first time the open file |
372 | * needs a new block. So, before every ext3_new_block(s) call, for | 333 | * needs a new block. So, before every ext3_new_block(s) call, for |
373 | * regular files, we should check whether the reservation window | 334 | * regular files, we should check whether the reservation window |
374 | * structure exists or not. In the latter case, this function is called. | 335 | * structure exists or not. In the latter case, this function is called. |
375 | * Fail to do so will result in block reservation being turned off for that | 336 | * Fail to do so will result in block reservation being turned off for that |
376 | * open file. | 337 | * open file. |
377 | * | 338 | * |
378 | * This function is called from ext3_get_blocks_handle(), also called | 339 | * This function is called from ext3_get_blocks_handle(), also called |
379 | * when setting the reservation window size through ioctl before the file | 340 | * when setting the reservation window size through ioctl before the file |
380 | * is open for write (needs block allocation). | 341 | * is open for write (needs block allocation). |
381 | * | 342 | * |
382 | * Needs truncate_mutex protection prior to call this function. | 343 | * Needs truncate_mutex protection prior to call this function. |
383 | */ | 344 | */ |
384 | void ext3_init_block_alloc_info(struct inode *inode) | 345 | void ext3_init_block_alloc_info(struct inode *inode) |
385 | { | 346 | { |
386 | struct ext3_inode_info *ei = EXT3_I(inode); | 347 | struct ext3_inode_info *ei = EXT3_I(inode); |
387 | struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info; | 348 | struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info; |
388 | struct super_block *sb = inode->i_sb; | 349 | struct super_block *sb = inode->i_sb; |
389 | 350 | ||
390 | block_i = kmalloc(sizeof(*block_i), GFP_NOFS); | 351 | block_i = kmalloc(sizeof(*block_i), GFP_NOFS); |
391 | if (block_i) { | 352 | if (block_i) { |
392 | struct ext3_reserve_window_node *rsv = &block_i->rsv_window_node; | 353 | struct ext3_reserve_window_node *rsv = &block_i->rsv_window_node; |
393 | 354 | ||
394 | rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED; | 355 | rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED; |
395 | rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED; | 356 | rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED; |
396 | 357 | ||
397 | /* | 358 | /* |
398 | * if filesystem is mounted with NORESERVATION, the goal | 359 | * if filesystem is mounted with NORESERVATION, the goal |
399 | * reservation window size is set to zero to indicate | 360 | * reservation window size is set to zero to indicate |
400 | * block reservation is off | 361 | * block reservation is off |
401 | */ | 362 | */ |
402 | if (!test_opt(sb, RESERVATION)) | 363 | if (!test_opt(sb, RESERVATION)) |
403 | rsv->rsv_goal_size = 0; | 364 | rsv->rsv_goal_size = 0; |
404 | else | 365 | else |
405 | rsv->rsv_goal_size = EXT3_DEFAULT_RESERVE_BLOCKS; | 366 | rsv->rsv_goal_size = EXT3_DEFAULT_RESERVE_BLOCKS; |
406 | rsv->rsv_alloc_hit = 0; | 367 | rsv->rsv_alloc_hit = 0; |
407 | block_i->last_alloc_logical_block = 0; | 368 | block_i->last_alloc_logical_block = 0; |
408 | block_i->last_alloc_physical_block = 0; | 369 | block_i->last_alloc_physical_block = 0; |
409 | } | 370 | } |
410 | ei->i_block_alloc_info = block_i; | 371 | ei->i_block_alloc_info = block_i; |
411 | } | 372 | } |
412 | 373 | ||
413 | /** | 374 | /** |
414 | * ext3_discard_reservation() | 375 | * ext3_discard_reservation() |
415 | * @inode: inode | 376 | * @inode: inode |
416 | * | 377 | * |
417 | * Discard(free) block reservation window on last file close, or truncate | 378 | * Discard(free) block reservation window on last file close, or truncate |
418 | * or at last iput(). | 379 | * or at last iput(). |
419 | * | 380 | * |
420 | * It is being called in three cases: | 381 | * It is being called in three cases: |
421 | * ext3_release_file(): last writer close the file | 382 | * ext3_release_file(): last writer close the file |
422 | * ext3_clear_inode(): last iput(), when nobody link to this file. | 383 | * ext3_clear_inode(): last iput(), when nobody link to this file. |
423 | * ext3_truncate(): when the block indirect map is about to change. | 384 | * ext3_truncate(): when the block indirect map is about to change. |
424 | * | 385 | * |
425 | */ | 386 | */ |
426 | void ext3_discard_reservation(struct inode *inode) | 387 | void ext3_discard_reservation(struct inode *inode) |
427 | { | 388 | { |
428 | struct ext3_inode_info *ei = EXT3_I(inode); | 389 | struct ext3_inode_info *ei = EXT3_I(inode); |
429 | struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info; | 390 | struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info; |
430 | struct ext3_reserve_window_node *rsv; | 391 | struct ext3_reserve_window_node *rsv; |
431 | spinlock_t *rsv_lock = &EXT3_SB(inode->i_sb)->s_rsv_window_lock; | 392 | spinlock_t *rsv_lock = &EXT3_SB(inode->i_sb)->s_rsv_window_lock; |
432 | 393 | ||
433 | if (!block_i) | 394 | if (!block_i) |
434 | return; | 395 | return; |
435 | 396 | ||
436 | rsv = &block_i->rsv_window_node; | 397 | rsv = &block_i->rsv_window_node; |
437 | if (!rsv_is_empty(&rsv->rsv_window)) { | 398 | if (!rsv_is_empty(&rsv->rsv_window)) { |
438 | spin_lock(rsv_lock); | 399 | spin_lock(rsv_lock); |
439 | if (!rsv_is_empty(&rsv->rsv_window)) | 400 | if (!rsv_is_empty(&rsv->rsv_window)) |
440 | rsv_window_remove(inode->i_sb, rsv); | 401 | rsv_window_remove(inode->i_sb, rsv); |
441 | spin_unlock(rsv_lock); | 402 | spin_unlock(rsv_lock); |
442 | } | 403 | } |
443 | } | 404 | } |
444 | 405 | ||
445 | /** | 406 | /** |
446 | * ext3_free_blocks_sb() -- Free given blocks and update quota | 407 | * ext3_free_blocks_sb() -- Free given blocks and update quota |
447 | * @handle: handle to this transaction | 408 | * @handle: handle to this transaction |
448 | * @sb: super block | 409 | * @sb: super block |
449 | * @block: start physcial block to free | 410 | * @block: start physcial block to free |
450 | * @count: number of blocks to free | 411 | * @count: number of blocks to free |
451 | * @pdquot_freed_blocks: pointer to quota | 412 | * @pdquot_freed_blocks: pointer to quota |
452 | */ | 413 | */ |
453 | void ext3_free_blocks_sb(handle_t *handle, struct super_block *sb, | 414 | void ext3_free_blocks_sb(handle_t *handle, struct super_block *sb, |
454 | ext3_fsblk_t block, unsigned long count, | 415 | ext3_fsblk_t block, unsigned long count, |
455 | unsigned long *pdquot_freed_blocks) | 416 | unsigned long *pdquot_freed_blocks) |
456 | { | 417 | { |
457 | struct buffer_head *bitmap_bh = NULL; | 418 | struct buffer_head *bitmap_bh = NULL; |
458 | struct buffer_head *gd_bh; | 419 | struct buffer_head *gd_bh; |
459 | unsigned long block_group; | 420 | unsigned long block_group; |
460 | ext3_grpblk_t bit; | 421 | ext3_grpblk_t bit; |
461 | unsigned long i; | 422 | unsigned long i; |
462 | unsigned long overflow; | 423 | unsigned long overflow; |
463 | struct ext3_group_desc * desc; | 424 | struct ext3_group_desc * desc; |
464 | struct ext3_super_block * es; | 425 | struct ext3_super_block * es; |
465 | struct ext3_sb_info *sbi; | 426 | struct ext3_sb_info *sbi; |
466 | int err = 0, ret; | 427 | int err = 0, ret; |
467 | ext3_grpblk_t group_freed; | 428 | ext3_grpblk_t group_freed; |
468 | 429 | ||
469 | *pdquot_freed_blocks = 0; | 430 | *pdquot_freed_blocks = 0; |
470 | sbi = EXT3_SB(sb); | 431 | sbi = EXT3_SB(sb); |
471 | es = sbi->s_es; | 432 | es = sbi->s_es; |
472 | if (block < le32_to_cpu(es->s_first_data_block) || | 433 | if (block < le32_to_cpu(es->s_first_data_block) || |
473 | block + count < block || | 434 | block + count < block || |
474 | block + count > le32_to_cpu(es->s_blocks_count)) { | 435 | block + count > le32_to_cpu(es->s_blocks_count)) { |
475 | ext3_error (sb, "ext3_free_blocks", | 436 | ext3_error (sb, "ext3_free_blocks", |
476 | "Freeing blocks not in datazone - " | 437 | "Freeing blocks not in datazone - " |
477 | "block = "E3FSBLK", count = %lu", block, count); | 438 | "block = "E3FSBLK", count = %lu", block, count); |
478 | goto error_return; | 439 | goto error_return; |
479 | } | 440 | } |
480 | 441 | ||
481 | ext3_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1); | 442 | ext3_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1); |
482 | 443 | ||
483 | do_more: | 444 | do_more: |
484 | overflow = 0; | 445 | overflow = 0; |
485 | block_group = (block - le32_to_cpu(es->s_first_data_block)) / | 446 | block_group = (block - le32_to_cpu(es->s_first_data_block)) / |
486 | EXT3_BLOCKS_PER_GROUP(sb); | 447 | EXT3_BLOCKS_PER_GROUP(sb); |
487 | bit = (block - le32_to_cpu(es->s_first_data_block)) % | 448 | bit = (block - le32_to_cpu(es->s_first_data_block)) % |
488 | EXT3_BLOCKS_PER_GROUP(sb); | 449 | EXT3_BLOCKS_PER_GROUP(sb); |
489 | /* | 450 | /* |
490 | * Check to see if we are freeing blocks across a group | 451 | * Check to see if we are freeing blocks across a group |
491 | * boundary. | 452 | * boundary. |
492 | */ | 453 | */ |
493 | if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) { | 454 | if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) { |
494 | overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb); | 455 | overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb); |
495 | count -= overflow; | 456 | count -= overflow; |
496 | } | 457 | } |
497 | brelse(bitmap_bh); | 458 | brelse(bitmap_bh); |
498 | bitmap_bh = read_block_bitmap(sb, block_group); | 459 | bitmap_bh = read_block_bitmap(sb, block_group); |
499 | if (!bitmap_bh) | 460 | if (!bitmap_bh) |
500 | goto error_return; | 461 | goto error_return; |
501 | desc = ext3_get_group_desc (sb, block_group, &gd_bh); | 462 | desc = ext3_get_group_desc (sb, block_group, &gd_bh); |
502 | if (!desc) | 463 | if (!desc) |
503 | goto error_return; | 464 | goto error_return; |
504 | 465 | ||
505 | if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) || | 466 | if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) || |
506 | in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) || | 467 | in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) || |
507 | in_range (block, le32_to_cpu(desc->bg_inode_table), | 468 | in_range (block, le32_to_cpu(desc->bg_inode_table), |
508 | sbi->s_itb_per_group) || | 469 | sbi->s_itb_per_group) || |
509 | in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table), | 470 | in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table), |
510 | sbi->s_itb_per_group)) | 471 | sbi->s_itb_per_group)) |
511 | ext3_error (sb, "ext3_free_blocks", | 472 | ext3_error (sb, "ext3_free_blocks", |
512 | "Freeing blocks in system zones - " | 473 | "Freeing blocks in system zones - " |
513 | "Block = "E3FSBLK", count = %lu", | 474 | "Block = "E3FSBLK", count = %lu", |
514 | block, count); | 475 | block, count); |
515 | 476 | ||
516 | /* | 477 | /* |
517 | * We are about to start releasing blocks in the bitmap, | 478 | * We are about to start releasing blocks in the bitmap, |
518 | * so we need undo access. | 479 | * so we need undo access. |
519 | */ | 480 | */ |
520 | /* @@@ check errors */ | 481 | /* @@@ check errors */ |
521 | BUFFER_TRACE(bitmap_bh, "getting undo access"); | 482 | BUFFER_TRACE(bitmap_bh, "getting undo access"); |
522 | err = ext3_journal_get_undo_access(handle, bitmap_bh); | 483 | err = ext3_journal_get_undo_access(handle, bitmap_bh); |
523 | if (err) | 484 | if (err) |
524 | goto error_return; | 485 | goto error_return; |
525 | 486 | ||
526 | /* | 487 | /* |
527 | * We are about to modify some metadata. Call the journal APIs | 488 | * We are about to modify some metadata. Call the journal APIs |
528 | * to unshare ->b_data if a currently-committing transaction is | 489 | * to unshare ->b_data if a currently-committing transaction is |
529 | * using it | 490 | * using it |
530 | */ | 491 | */ |
531 | BUFFER_TRACE(gd_bh, "get_write_access"); | 492 | BUFFER_TRACE(gd_bh, "get_write_access"); |
532 | err = ext3_journal_get_write_access(handle, gd_bh); | 493 | err = ext3_journal_get_write_access(handle, gd_bh); |
533 | if (err) | 494 | if (err) |
534 | goto error_return; | 495 | goto error_return; |
535 | 496 | ||
536 | jbd_lock_bh_state(bitmap_bh); | 497 | jbd_lock_bh_state(bitmap_bh); |
537 | 498 | ||
538 | for (i = 0, group_freed = 0; i < count; i++) { | 499 | for (i = 0, group_freed = 0; i < count; i++) { |
539 | /* | 500 | /* |
540 | * An HJ special. This is expensive... | 501 | * An HJ special. This is expensive... |
541 | */ | 502 | */ |
542 | #ifdef CONFIG_JBD_DEBUG | 503 | #ifdef CONFIG_JBD_DEBUG |
543 | jbd_unlock_bh_state(bitmap_bh); | 504 | jbd_unlock_bh_state(bitmap_bh); |
544 | { | 505 | { |
545 | struct buffer_head *debug_bh; | 506 | struct buffer_head *debug_bh; |
546 | debug_bh = sb_find_get_block(sb, block + i); | 507 | debug_bh = sb_find_get_block(sb, block + i); |
547 | if (debug_bh) { | 508 | if (debug_bh) { |
548 | BUFFER_TRACE(debug_bh, "Deleted!"); | 509 | BUFFER_TRACE(debug_bh, "Deleted!"); |
549 | if (!bh2jh(bitmap_bh)->b_committed_data) | 510 | if (!bh2jh(bitmap_bh)->b_committed_data) |
550 | BUFFER_TRACE(debug_bh, | 511 | BUFFER_TRACE(debug_bh, |
551 | "No commited data in bitmap"); | 512 | "No commited data in bitmap"); |
552 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap"); | 513 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap"); |
553 | __brelse(debug_bh); | 514 | __brelse(debug_bh); |
554 | } | 515 | } |
555 | } | 516 | } |
556 | jbd_lock_bh_state(bitmap_bh); | 517 | jbd_lock_bh_state(bitmap_bh); |
557 | #endif | 518 | #endif |
558 | if (need_resched()) { | 519 | if (need_resched()) { |
559 | jbd_unlock_bh_state(bitmap_bh); | 520 | jbd_unlock_bh_state(bitmap_bh); |
560 | cond_resched(); | 521 | cond_resched(); |
561 | jbd_lock_bh_state(bitmap_bh); | 522 | jbd_lock_bh_state(bitmap_bh); |
562 | } | 523 | } |
563 | /* @@@ This prevents newly-allocated data from being | 524 | /* @@@ This prevents newly-allocated data from being |
564 | * freed and then reallocated within the same | 525 | * freed and then reallocated within the same |
565 | * transaction. | 526 | * transaction. |
566 | * | 527 | * |
567 | * Ideally we would want to allow that to happen, but to | 528 | * Ideally we would want to allow that to happen, but to |
568 | * do so requires making journal_forget() capable of | 529 | * do so requires making journal_forget() capable of |
569 | * revoking the queued write of a data block, which | 530 | * revoking the queued write of a data block, which |
570 | * implies blocking on the journal lock. *forget() | 531 | * implies blocking on the journal lock. *forget() |
571 | * cannot block due to truncate races. | 532 | * cannot block due to truncate races. |
572 | * | 533 | * |
573 | * Eventually we can fix this by making journal_forget() | 534 | * Eventually we can fix this by making journal_forget() |
574 | * return a status indicating whether or not it was able | 535 | * return a status indicating whether or not it was able |
575 | * to revoke the buffer. On successful revoke, it is | 536 | * to revoke the buffer. On successful revoke, it is |
576 | * safe not to set the allocation bit in the committed | 537 | * safe not to set the allocation bit in the committed |
577 | * bitmap, because we know that there is no outstanding | 538 | * bitmap, because we know that there is no outstanding |
578 | * activity on the buffer any more and so it is safe to | 539 | * activity on the buffer any more and so it is safe to |
579 | * reallocate it. | 540 | * reallocate it. |
580 | */ | 541 | */ |
581 | BUFFER_TRACE(bitmap_bh, "set in b_committed_data"); | 542 | BUFFER_TRACE(bitmap_bh, "set in b_committed_data"); |
582 | J_ASSERT_BH(bitmap_bh, | 543 | J_ASSERT_BH(bitmap_bh, |
583 | bh2jh(bitmap_bh)->b_committed_data != NULL); | 544 | bh2jh(bitmap_bh)->b_committed_data != NULL); |
584 | ext3_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i, | 545 | ext3_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i, |
585 | bh2jh(bitmap_bh)->b_committed_data); | 546 | bh2jh(bitmap_bh)->b_committed_data); |
586 | 547 | ||
587 | /* | 548 | /* |
588 | * We clear the bit in the bitmap after setting the committed | 549 | * We clear the bit in the bitmap after setting the committed |
589 | * data bit, because this is the reverse order to that which | 550 | * data bit, because this is the reverse order to that which |
590 | * the allocator uses. | 551 | * the allocator uses. |
591 | */ | 552 | */ |
592 | BUFFER_TRACE(bitmap_bh, "clear bit"); | 553 | BUFFER_TRACE(bitmap_bh, "clear bit"); |
593 | if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group), | 554 | if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group), |
594 | bit + i, bitmap_bh->b_data)) { | 555 | bit + i, bitmap_bh->b_data)) { |
595 | jbd_unlock_bh_state(bitmap_bh); | 556 | jbd_unlock_bh_state(bitmap_bh); |
596 | ext3_error(sb, __FUNCTION__, | 557 | ext3_error(sb, __FUNCTION__, |
597 | "bit already cleared for block "E3FSBLK, | 558 | "bit already cleared for block "E3FSBLK, |
598 | block + i); | 559 | block + i); |
599 | jbd_lock_bh_state(bitmap_bh); | 560 | jbd_lock_bh_state(bitmap_bh); |
600 | BUFFER_TRACE(bitmap_bh, "bit already cleared"); | 561 | BUFFER_TRACE(bitmap_bh, "bit already cleared"); |
601 | } else { | 562 | } else { |
602 | group_freed++; | 563 | group_freed++; |
603 | } | 564 | } |
604 | } | 565 | } |
605 | jbd_unlock_bh_state(bitmap_bh); | 566 | jbd_unlock_bh_state(bitmap_bh); |
606 | 567 | ||
607 | spin_lock(sb_bgl_lock(sbi, block_group)); | 568 | spin_lock(sb_bgl_lock(sbi, block_group)); |
608 | desc->bg_free_blocks_count = | 569 | desc->bg_free_blocks_count = |
609 | cpu_to_le16(le16_to_cpu(desc->bg_free_blocks_count) + | 570 | cpu_to_le16(le16_to_cpu(desc->bg_free_blocks_count) + |
610 | group_freed); | 571 | group_freed); |
611 | spin_unlock(sb_bgl_lock(sbi, block_group)); | 572 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
612 | percpu_counter_add(&sbi->s_freeblocks_counter, count); | 573 | percpu_counter_add(&sbi->s_freeblocks_counter, count); |
613 | 574 | ||
614 | /* We dirtied the bitmap block */ | 575 | /* We dirtied the bitmap block */ |
615 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); | 576 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); |
616 | err = ext3_journal_dirty_metadata(handle, bitmap_bh); | 577 | err = ext3_journal_dirty_metadata(handle, bitmap_bh); |
617 | 578 | ||
618 | /* And the group descriptor block */ | 579 | /* And the group descriptor block */ |
619 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); | 580 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); |
620 | ret = ext3_journal_dirty_metadata(handle, gd_bh); | 581 | ret = ext3_journal_dirty_metadata(handle, gd_bh); |
621 | if (!err) err = ret; | 582 | if (!err) err = ret; |
622 | *pdquot_freed_blocks += group_freed; | 583 | *pdquot_freed_blocks += group_freed; |
623 | 584 | ||
624 | if (overflow && !err) { | 585 | if (overflow && !err) { |
625 | block += count; | 586 | block += count; |
626 | count = overflow; | 587 | count = overflow; |
627 | goto do_more; | 588 | goto do_more; |
628 | } | 589 | } |
629 | sb->s_dirt = 1; | 590 | sb->s_dirt = 1; |
630 | error_return: | 591 | error_return: |
631 | brelse(bitmap_bh); | 592 | brelse(bitmap_bh); |
632 | ext3_std_error(sb, err); | 593 | ext3_std_error(sb, err); |
633 | return; | 594 | return; |
634 | } | 595 | } |
635 | 596 | ||
636 | /** | 597 | /** |
637 | * ext3_free_blocks() -- Free given blocks and update quota | 598 | * ext3_free_blocks() -- Free given blocks and update quota |
638 | * @handle: handle for this transaction | 599 | * @handle: handle for this transaction |
639 | * @inode: inode | 600 | * @inode: inode |
640 | * @block: start physical block to free | 601 | * @block: start physical block to free |
641 | * @count: number of blocks to count | 602 | * @count: number of blocks to count |
642 | */ | 603 | */ |
643 | void ext3_free_blocks(handle_t *handle, struct inode *inode, | 604 | void ext3_free_blocks(handle_t *handle, struct inode *inode, |
644 | ext3_fsblk_t block, unsigned long count) | 605 | ext3_fsblk_t block, unsigned long count) |
645 | { | 606 | { |
646 | struct super_block * sb; | 607 | struct super_block * sb; |
647 | unsigned long dquot_freed_blocks; | 608 | unsigned long dquot_freed_blocks; |
648 | 609 | ||
649 | sb = inode->i_sb; | 610 | sb = inode->i_sb; |
650 | if (!sb) { | 611 | if (!sb) { |
651 | printk ("ext3_free_blocks: nonexistent device"); | 612 | printk ("ext3_free_blocks: nonexistent device"); |
652 | return; | 613 | return; |
653 | } | 614 | } |
654 | ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks); | 615 | ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks); |
655 | if (dquot_freed_blocks) | 616 | if (dquot_freed_blocks) |
656 | DQUOT_FREE_BLOCK(inode, dquot_freed_blocks); | 617 | DQUOT_FREE_BLOCK(inode, dquot_freed_blocks); |
657 | return; | 618 | return; |
658 | } | 619 | } |
659 | 620 | ||
660 | /** | 621 | /** |
661 | * ext3_test_allocatable() | 622 | * ext3_test_allocatable() |
662 | * @nr: given allocation block group | 623 | * @nr: given allocation block group |
663 | * @bh: bufferhead contains the bitmap of the given block group | 624 | * @bh: bufferhead contains the bitmap of the given block group |
664 | * | 625 | * |
665 | * For ext3 allocations, we must not reuse any blocks which are | 626 | * For ext3 allocations, we must not reuse any blocks which are |
666 | * allocated in the bitmap buffer's "last committed data" copy. This | 627 | * allocated in the bitmap buffer's "last committed data" copy. This |
667 | * prevents deletes from freeing up the page for reuse until we have | 628 | * prevents deletes from freeing up the page for reuse until we have |
668 | * committed the delete transaction. | 629 | * committed the delete transaction. |
669 | * | 630 | * |
670 | * If we didn't do this, then deleting something and reallocating it as | 631 | * If we didn't do this, then deleting something and reallocating it as |
671 | * data would allow the old block to be overwritten before the | 632 | * data would allow the old block to be overwritten before the |
672 | * transaction committed (because we force data to disk before commit). | 633 | * transaction committed (because we force data to disk before commit). |
673 | * This would lead to corruption if we crashed between overwriting the | 634 | * This would lead to corruption if we crashed between overwriting the |
674 | * data and committing the delete. | 635 | * data and committing the delete. |
675 | * | 636 | * |
676 | * @@@ We may want to make this allocation behaviour conditional on | 637 | * @@@ We may want to make this allocation behaviour conditional on |
677 | * data-writes at some point, and disable it for metadata allocations or | 638 | * data-writes at some point, and disable it for metadata allocations or |
678 | * sync-data inodes. | 639 | * sync-data inodes. |
679 | */ | 640 | */ |
680 | static int ext3_test_allocatable(ext3_grpblk_t nr, struct buffer_head *bh) | 641 | static int ext3_test_allocatable(ext3_grpblk_t nr, struct buffer_head *bh) |
681 | { | 642 | { |
682 | int ret; | 643 | int ret; |
683 | struct journal_head *jh = bh2jh(bh); | 644 | struct journal_head *jh = bh2jh(bh); |
684 | 645 | ||
685 | if (ext3_test_bit(nr, bh->b_data)) | 646 | if (ext3_test_bit(nr, bh->b_data)) |
686 | return 0; | 647 | return 0; |
687 | 648 | ||
688 | jbd_lock_bh_state(bh); | 649 | jbd_lock_bh_state(bh); |
689 | if (!jh->b_committed_data) | 650 | if (!jh->b_committed_data) |
690 | ret = 1; | 651 | ret = 1; |
691 | else | 652 | else |
692 | ret = !ext3_test_bit(nr, jh->b_committed_data); | 653 | ret = !ext3_test_bit(nr, jh->b_committed_data); |
693 | jbd_unlock_bh_state(bh); | 654 | jbd_unlock_bh_state(bh); |
694 | return ret; | 655 | return ret; |
695 | } | 656 | } |
696 | 657 | ||
697 | /** | 658 | /** |
698 | * bitmap_search_next_usable_block() | 659 | * bitmap_search_next_usable_block() |
699 | * @start: the starting block (group relative) of the search | 660 | * @start: the starting block (group relative) of the search |
700 | * @bh: bufferhead contains the block group bitmap | 661 | * @bh: bufferhead contains the block group bitmap |
701 | * @maxblocks: the ending block (group relative) of the reservation | 662 | * @maxblocks: the ending block (group relative) of the reservation |
702 | * | 663 | * |
703 | * The bitmap search --- search forward alternately through the actual | 664 | * The bitmap search --- search forward alternately through the actual |
704 | * bitmap on disk and the last-committed copy in journal, until we find a | 665 | * bitmap on disk and the last-committed copy in journal, until we find a |
705 | * bit free in both bitmaps. | 666 | * bit free in both bitmaps. |
706 | */ | 667 | */ |
707 | static ext3_grpblk_t | 668 | static ext3_grpblk_t |
708 | bitmap_search_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh, | 669 | bitmap_search_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh, |
709 | ext3_grpblk_t maxblocks) | 670 | ext3_grpblk_t maxblocks) |
710 | { | 671 | { |
711 | ext3_grpblk_t next; | 672 | ext3_grpblk_t next; |
712 | struct journal_head *jh = bh2jh(bh); | 673 | struct journal_head *jh = bh2jh(bh); |
713 | 674 | ||
714 | while (start < maxblocks) { | 675 | while (start < maxblocks) { |
715 | next = ext3_find_next_zero_bit(bh->b_data, maxblocks, start); | 676 | next = ext3_find_next_zero_bit(bh->b_data, maxblocks, start); |
716 | if (next >= maxblocks) | 677 | if (next >= maxblocks) |
717 | return -1; | 678 | return -1; |
718 | if (ext3_test_allocatable(next, bh)) | 679 | if (ext3_test_allocatable(next, bh)) |
719 | return next; | 680 | return next; |
720 | jbd_lock_bh_state(bh); | 681 | jbd_lock_bh_state(bh); |
721 | if (jh->b_committed_data) | 682 | if (jh->b_committed_data) |
722 | start = ext3_find_next_zero_bit(jh->b_committed_data, | 683 | start = ext3_find_next_zero_bit(jh->b_committed_data, |
723 | maxblocks, next); | 684 | maxblocks, next); |
724 | jbd_unlock_bh_state(bh); | 685 | jbd_unlock_bh_state(bh); |
725 | } | 686 | } |
726 | return -1; | 687 | return -1; |
727 | } | 688 | } |
728 | 689 | ||
729 | /** | 690 | /** |
730 | * find_next_usable_block() | 691 | * find_next_usable_block() |
731 | * @start: the starting block (group relative) to find next | 692 | * @start: the starting block (group relative) to find next |
732 | * allocatable block in bitmap. | 693 | * allocatable block in bitmap. |
733 | * @bh: bufferhead contains the block group bitmap | 694 | * @bh: bufferhead contains the block group bitmap |
734 | * @maxblocks: the ending block (group relative) for the search | 695 | * @maxblocks: the ending block (group relative) for the search |
735 | * | 696 | * |
736 | * Find an allocatable block in a bitmap. We honor both the bitmap and | 697 | * Find an allocatable block in a bitmap. We honor both the bitmap and |
737 | * its last-committed copy (if that exists), and perform the "most | 698 | * its last-committed copy (if that exists), and perform the "most |
738 | * appropriate allocation" algorithm of looking for a free block near | 699 | * appropriate allocation" algorithm of looking for a free block near |
739 | * the initial goal; then for a free byte somewhere in the bitmap; then | 700 | * the initial goal; then for a free byte somewhere in the bitmap; then |
740 | * for any free bit in the bitmap. | 701 | * for any free bit in the bitmap. |
741 | */ | 702 | */ |
742 | static ext3_grpblk_t | 703 | static ext3_grpblk_t |
743 | find_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh, | 704 | find_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh, |
744 | ext3_grpblk_t maxblocks) | 705 | ext3_grpblk_t maxblocks) |
745 | { | 706 | { |
746 | ext3_grpblk_t here, next; | 707 | ext3_grpblk_t here, next; |
747 | char *p, *r; | 708 | char *p, *r; |
748 | 709 | ||
749 | if (start > 0) { | 710 | if (start > 0) { |
750 | /* | 711 | /* |
751 | * The goal was occupied; search forward for a free | 712 | * The goal was occupied; search forward for a free |
752 | * block within the next XX blocks. | 713 | * block within the next XX blocks. |
753 | * | 714 | * |
754 | * end_goal is more or less random, but it has to be | 715 | * end_goal is more or less random, but it has to be |
755 | * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the | 716 | * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the |
756 | * next 64-bit boundary is simple.. | 717 | * next 64-bit boundary is simple.. |
757 | */ | 718 | */ |
758 | ext3_grpblk_t end_goal = (start + 63) & ~63; | 719 | ext3_grpblk_t end_goal = (start + 63) & ~63; |
759 | if (end_goal > maxblocks) | 720 | if (end_goal > maxblocks) |
760 | end_goal = maxblocks; | 721 | end_goal = maxblocks; |
761 | here = ext3_find_next_zero_bit(bh->b_data, end_goal, start); | 722 | here = ext3_find_next_zero_bit(bh->b_data, end_goal, start); |
762 | if (here < end_goal && ext3_test_allocatable(here, bh)) | 723 | if (here < end_goal && ext3_test_allocatable(here, bh)) |
763 | return here; | 724 | return here; |
764 | ext3_debug("Bit not found near goal\n"); | 725 | ext3_debug("Bit not found near goal\n"); |
765 | } | 726 | } |
766 | 727 | ||
767 | here = start; | 728 | here = start; |
768 | if (here < 0) | 729 | if (here < 0) |
769 | here = 0; | 730 | here = 0; |
770 | 731 | ||
771 | p = ((char *)bh->b_data) + (here >> 3); | 732 | p = ((char *)bh->b_data) + (here >> 3); |
772 | r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3)); | 733 | r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3)); |
773 | next = (r - ((char *)bh->b_data)) << 3; | 734 | next = (r - ((char *)bh->b_data)) << 3; |
774 | 735 | ||
775 | if (next < maxblocks && next >= start && ext3_test_allocatable(next, bh)) | 736 | if (next < maxblocks && next >= start && ext3_test_allocatable(next, bh)) |
776 | return next; | 737 | return next; |
777 | 738 | ||
778 | /* | 739 | /* |
779 | * The bitmap search --- search forward alternately through the actual | 740 | * The bitmap search --- search forward alternately through the actual |
780 | * bitmap and the last-committed copy until we find a bit free in | 741 | * bitmap and the last-committed copy until we find a bit free in |
781 | * both | 742 | * both |
782 | */ | 743 | */ |
783 | here = bitmap_search_next_usable_block(here, bh, maxblocks); | 744 | here = bitmap_search_next_usable_block(here, bh, maxblocks); |
784 | return here; | 745 | return here; |
785 | } | 746 | } |
786 | 747 | ||
787 | /** | 748 | /** |
788 | * claim_block() | 749 | * claim_block() |
789 | * @block: the free block (group relative) to allocate | 750 | * @block: the free block (group relative) to allocate |
790 | * @bh: the bufferhead containts the block group bitmap | 751 | * @bh: the bufferhead containts the block group bitmap |
791 | * | 752 | * |
792 | * We think we can allocate this block in this bitmap. Try to set the bit. | 753 | * We think we can allocate this block in this bitmap. Try to set the bit. |
793 | * If that succeeds then check that nobody has allocated and then freed the | 754 | * If that succeeds then check that nobody has allocated and then freed the |
794 | * block since we saw that is was not marked in b_committed_data. If it _was_ | 755 | * block since we saw that is was not marked in b_committed_data. If it _was_ |
795 | * allocated and freed then clear the bit in the bitmap again and return | 756 | * allocated and freed then clear the bit in the bitmap again and return |
796 | * zero (failure). | 757 | * zero (failure). |
797 | */ | 758 | */ |
798 | static inline int | 759 | static inline int |
799 | claim_block(spinlock_t *lock, ext3_grpblk_t block, struct buffer_head *bh) | 760 | claim_block(spinlock_t *lock, ext3_grpblk_t block, struct buffer_head *bh) |
800 | { | 761 | { |
801 | struct journal_head *jh = bh2jh(bh); | 762 | struct journal_head *jh = bh2jh(bh); |
802 | int ret; | 763 | int ret; |
803 | 764 | ||
804 | if (ext3_set_bit_atomic(lock, block, bh->b_data)) | 765 | if (ext3_set_bit_atomic(lock, block, bh->b_data)) |
805 | return 0; | 766 | return 0; |
806 | jbd_lock_bh_state(bh); | 767 | jbd_lock_bh_state(bh); |
807 | if (jh->b_committed_data && ext3_test_bit(block,jh->b_committed_data)) { | 768 | if (jh->b_committed_data && ext3_test_bit(block,jh->b_committed_data)) { |
808 | ext3_clear_bit_atomic(lock, block, bh->b_data); | 769 | ext3_clear_bit_atomic(lock, block, bh->b_data); |
809 | ret = 0; | 770 | ret = 0; |
810 | } else { | 771 | } else { |
811 | ret = 1; | 772 | ret = 1; |
812 | } | 773 | } |
813 | jbd_unlock_bh_state(bh); | 774 | jbd_unlock_bh_state(bh); |
814 | return ret; | 775 | return ret; |
815 | } | 776 | } |
816 | 777 | ||
817 | /** | 778 | /** |
818 | * ext3_try_to_allocate() | 779 | * ext3_try_to_allocate() |
819 | * @sb: superblock | 780 | * @sb: superblock |
820 | * @handle: handle to this transaction | 781 | * @handle: handle to this transaction |
821 | * @group: given allocation block group | 782 | * @group: given allocation block group |
822 | * @bitmap_bh: bufferhead holds the block bitmap | 783 | * @bitmap_bh: bufferhead holds the block bitmap |
823 | * @grp_goal: given target block within the group | 784 | * @grp_goal: given target block within the group |
824 | * @count: target number of blocks to allocate | 785 | * @count: target number of blocks to allocate |
825 | * @my_rsv: reservation window | 786 | * @my_rsv: reservation window |
826 | * | 787 | * |
827 | * Attempt to allocate blocks within a give range. Set the range of allocation | 788 | * Attempt to allocate blocks within a give range. Set the range of allocation |
828 | * first, then find the first free bit(s) from the bitmap (within the range), | 789 | * first, then find the first free bit(s) from the bitmap (within the range), |
829 | * and at last, allocate the blocks by claiming the found free bit as allocated. | 790 | * and at last, allocate the blocks by claiming the found free bit as allocated. |
830 | * | 791 | * |
831 | * To set the range of this allocation: | 792 | * To set the range of this allocation: |
832 | * if there is a reservation window, only try to allocate block(s) from the | 793 | * if there is a reservation window, only try to allocate block(s) from the |
833 | * file's own reservation window; | 794 | * file's own reservation window; |
834 | * Otherwise, the allocation range starts from the give goal block, ends at | 795 | * Otherwise, the allocation range starts from the give goal block, ends at |
835 | * the block group's last block. | 796 | * the block group's last block. |
836 | * | 797 | * |
837 | * If we failed to allocate the desired block then we may end up crossing to a | 798 | * If we failed to allocate the desired block then we may end up crossing to a |
838 | * new bitmap. In that case we must release write access to the old one via | 799 | * new bitmap. In that case we must release write access to the old one via |
839 | * ext3_journal_release_buffer(), else we'll run out of credits. | 800 | * ext3_journal_release_buffer(), else we'll run out of credits. |
840 | */ | 801 | */ |
841 | static ext3_grpblk_t | 802 | static ext3_grpblk_t |
842 | ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group, | 803 | ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group, |
843 | struct buffer_head *bitmap_bh, ext3_grpblk_t grp_goal, | 804 | struct buffer_head *bitmap_bh, ext3_grpblk_t grp_goal, |
844 | unsigned long *count, struct ext3_reserve_window *my_rsv) | 805 | unsigned long *count, struct ext3_reserve_window *my_rsv) |
845 | { | 806 | { |
846 | ext3_fsblk_t group_first_block; | 807 | ext3_fsblk_t group_first_block; |
847 | ext3_grpblk_t start, end; | 808 | ext3_grpblk_t start, end; |
848 | unsigned long num = 0; | 809 | unsigned long num = 0; |
849 | 810 | ||
850 | /* we do allocation within the reservation window if we have a window */ | 811 | /* we do allocation within the reservation window if we have a window */ |
851 | if (my_rsv) { | 812 | if (my_rsv) { |
852 | group_first_block = ext3_group_first_block_no(sb, group); | 813 | group_first_block = ext3_group_first_block_no(sb, group); |
853 | if (my_rsv->_rsv_start >= group_first_block) | 814 | if (my_rsv->_rsv_start >= group_first_block) |
854 | start = my_rsv->_rsv_start - group_first_block; | 815 | start = my_rsv->_rsv_start - group_first_block; |
855 | else | 816 | else |
856 | /* reservation window cross group boundary */ | 817 | /* reservation window cross group boundary */ |
857 | start = 0; | 818 | start = 0; |
858 | end = my_rsv->_rsv_end - group_first_block + 1; | 819 | end = my_rsv->_rsv_end - group_first_block + 1; |
859 | if (end > EXT3_BLOCKS_PER_GROUP(sb)) | 820 | if (end > EXT3_BLOCKS_PER_GROUP(sb)) |
860 | /* reservation window crosses group boundary */ | 821 | /* reservation window crosses group boundary */ |
861 | end = EXT3_BLOCKS_PER_GROUP(sb); | 822 | end = EXT3_BLOCKS_PER_GROUP(sb); |
862 | if ((start <= grp_goal) && (grp_goal < end)) | 823 | if ((start <= grp_goal) && (grp_goal < end)) |
863 | start = grp_goal; | 824 | start = grp_goal; |
864 | else | 825 | else |
865 | grp_goal = -1; | 826 | grp_goal = -1; |
866 | } else { | 827 | } else { |
867 | if (grp_goal > 0) | 828 | if (grp_goal > 0) |
868 | start = grp_goal; | 829 | start = grp_goal; |
869 | else | 830 | else |
870 | start = 0; | 831 | start = 0; |
871 | end = EXT3_BLOCKS_PER_GROUP(sb); | 832 | end = EXT3_BLOCKS_PER_GROUP(sb); |
872 | } | 833 | } |
873 | 834 | ||
874 | BUG_ON(start > EXT3_BLOCKS_PER_GROUP(sb)); | 835 | BUG_ON(start > EXT3_BLOCKS_PER_GROUP(sb)); |
875 | 836 | ||
876 | repeat: | 837 | repeat: |
877 | if (grp_goal < 0 || !ext3_test_allocatable(grp_goal, bitmap_bh)) { | 838 | if (grp_goal < 0 || !ext3_test_allocatable(grp_goal, bitmap_bh)) { |
878 | grp_goal = find_next_usable_block(start, bitmap_bh, end); | 839 | grp_goal = find_next_usable_block(start, bitmap_bh, end); |
879 | if (grp_goal < 0) | 840 | if (grp_goal < 0) |
880 | goto fail_access; | 841 | goto fail_access; |
881 | if (!my_rsv) { | 842 | if (!my_rsv) { |
882 | int i; | 843 | int i; |
883 | 844 | ||
884 | for (i = 0; i < 7 && grp_goal > start && | 845 | for (i = 0; i < 7 && grp_goal > start && |
885 | ext3_test_allocatable(grp_goal - 1, | 846 | ext3_test_allocatable(grp_goal - 1, |
886 | bitmap_bh); | 847 | bitmap_bh); |
887 | i++, grp_goal--) | 848 | i++, grp_goal--) |
888 | ; | 849 | ; |
889 | } | 850 | } |
890 | } | 851 | } |
891 | start = grp_goal; | 852 | start = grp_goal; |
892 | 853 | ||
893 | if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group), | 854 | if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group), |
894 | grp_goal, bitmap_bh)) { | 855 | grp_goal, bitmap_bh)) { |
895 | /* | 856 | /* |
896 | * The block was allocated by another thread, or it was | 857 | * The block was allocated by another thread, or it was |
897 | * allocated and then freed by another thread | 858 | * allocated and then freed by another thread |
898 | */ | 859 | */ |
899 | start++; | 860 | start++; |
900 | grp_goal++; | 861 | grp_goal++; |
901 | if (start >= end) | 862 | if (start >= end) |
902 | goto fail_access; | 863 | goto fail_access; |
903 | goto repeat; | 864 | goto repeat; |
904 | } | 865 | } |
905 | num++; | 866 | num++; |
906 | grp_goal++; | 867 | grp_goal++; |
907 | while (num < *count && grp_goal < end | 868 | while (num < *count && grp_goal < end |
908 | && ext3_test_allocatable(grp_goal, bitmap_bh) | 869 | && ext3_test_allocatable(grp_goal, bitmap_bh) |
909 | && claim_block(sb_bgl_lock(EXT3_SB(sb), group), | 870 | && claim_block(sb_bgl_lock(EXT3_SB(sb), group), |
910 | grp_goal, bitmap_bh)) { | 871 | grp_goal, bitmap_bh)) { |
911 | num++; | 872 | num++; |
912 | grp_goal++; | 873 | grp_goal++; |
913 | } | 874 | } |
914 | *count = num; | 875 | *count = num; |
915 | return grp_goal - num; | 876 | return grp_goal - num; |
916 | fail_access: | 877 | fail_access: |
917 | *count = num; | 878 | *count = num; |
918 | return -1; | 879 | return -1; |
919 | } | 880 | } |
920 | 881 | ||
921 | /** | 882 | /** |
922 | * find_next_reservable_window(): | 883 | * find_next_reservable_window(): |
923 | * find a reservable space within the given range. | 884 | * find a reservable space within the given range. |
924 | * It does not allocate the reservation window for now: | 885 | * It does not allocate the reservation window for now: |
925 | * alloc_new_reservation() will do the work later. | 886 | * alloc_new_reservation() will do the work later. |
926 | * | 887 | * |
927 | * @search_head: the head of the searching list; | 888 | * @search_head: the head of the searching list; |
928 | * This is not necessarily the list head of the whole filesystem | 889 | * This is not necessarily the list head of the whole filesystem |
929 | * | 890 | * |
930 | * We have both head and start_block to assist the search | 891 | * We have both head and start_block to assist the search |
931 | * for the reservable space. The list starts from head, | 892 | * for the reservable space. The list starts from head, |
932 | * but we will shift to the place where start_block is, | 893 | * but we will shift to the place where start_block is, |
933 | * then start from there, when looking for a reservable space. | 894 | * then start from there, when looking for a reservable space. |
934 | * | 895 | * |
935 | * @size: the target new reservation window size | 896 | * @size: the target new reservation window size |
936 | * | 897 | * |
937 | * @group_first_block: the first block we consider to start | 898 | * @group_first_block: the first block we consider to start |
938 | * the real search from | 899 | * the real search from |
939 | * | 900 | * |
940 | * @last_block: | 901 | * @last_block: |
941 | * the maximum block number that our goal reservable space | 902 | * the maximum block number that our goal reservable space |
942 | * could start from. This is normally the last block in this | 903 | * could start from. This is normally the last block in this |
943 | * group. The search will end when we found the start of next | 904 | * group. The search will end when we found the start of next |
944 | * possible reservable space is out of this boundary. | 905 | * possible reservable space is out of this boundary. |
945 | * This could handle the cross boundary reservation window | 906 | * This could handle the cross boundary reservation window |
946 | * request. | 907 | * request. |
947 | * | 908 | * |
948 | * basically we search from the given range, rather than the whole | 909 | * basically we search from the given range, rather than the whole |
949 | * reservation double linked list, (start_block, last_block) | 910 | * reservation double linked list, (start_block, last_block) |
950 | * to find a free region that is of my size and has not | 911 | * to find a free region that is of my size and has not |
951 | * been reserved. | 912 | * been reserved. |
952 | * | 913 | * |
953 | */ | 914 | */ |
954 | static int find_next_reservable_window( | 915 | static int find_next_reservable_window( |
955 | struct ext3_reserve_window_node *search_head, | 916 | struct ext3_reserve_window_node *search_head, |
956 | struct ext3_reserve_window_node *my_rsv, | 917 | struct ext3_reserve_window_node *my_rsv, |
957 | struct super_block * sb, | 918 | struct super_block * sb, |
958 | ext3_fsblk_t start_block, | 919 | ext3_fsblk_t start_block, |
959 | ext3_fsblk_t last_block) | 920 | ext3_fsblk_t last_block) |
960 | { | 921 | { |
961 | struct rb_node *next; | 922 | struct rb_node *next; |
962 | struct ext3_reserve_window_node *rsv, *prev; | 923 | struct ext3_reserve_window_node *rsv, *prev; |
963 | ext3_fsblk_t cur; | 924 | ext3_fsblk_t cur; |
964 | int size = my_rsv->rsv_goal_size; | 925 | int size = my_rsv->rsv_goal_size; |
965 | 926 | ||
966 | /* TODO: make the start of the reservation window byte-aligned */ | 927 | /* TODO: make the start of the reservation window byte-aligned */ |
967 | /* cur = *start_block & ~7;*/ | 928 | /* cur = *start_block & ~7;*/ |
968 | cur = start_block; | 929 | cur = start_block; |
969 | rsv = search_head; | 930 | rsv = search_head; |
970 | if (!rsv) | 931 | if (!rsv) |
971 | return -1; | 932 | return -1; |
972 | 933 | ||
973 | while (1) { | 934 | while (1) { |
974 | if (cur <= rsv->rsv_end) | 935 | if (cur <= rsv->rsv_end) |
975 | cur = rsv->rsv_end + 1; | 936 | cur = rsv->rsv_end + 1; |
976 | 937 | ||
977 | /* TODO? | 938 | /* TODO? |
978 | * in the case we could not find a reservable space | 939 | * in the case we could not find a reservable space |
979 | * that is what is expected, during the re-search, we could | 940 | * that is what is expected, during the re-search, we could |
980 | * remember what's the largest reservable space we could have | 941 | * remember what's the largest reservable space we could have |
981 | * and return that one. | 942 | * and return that one. |
982 | * | 943 | * |
983 | * For now it will fail if we could not find the reservable | 944 | * For now it will fail if we could not find the reservable |
984 | * space with expected-size (or more)... | 945 | * space with expected-size (or more)... |
985 | */ | 946 | */ |
986 | if (cur > last_block) | 947 | if (cur > last_block) |
987 | return -1; /* fail */ | 948 | return -1; /* fail */ |
988 | 949 | ||
989 | prev = rsv; | 950 | prev = rsv; |
990 | next = rb_next(&rsv->rsv_node); | 951 | next = rb_next(&rsv->rsv_node); |
991 | rsv = rb_entry(next,struct ext3_reserve_window_node,rsv_node); | 952 | rsv = rb_entry(next,struct ext3_reserve_window_node,rsv_node); |
992 | 953 | ||
993 | /* | 954 | /* |
994 | * Reached the last reservation, we can just append to the | 955 | * Reached the last reservation, we can just append to the |
995 | * previous one. | 956 | * previous one. |
996 | */ | 957 | */ |
997 | if (!next) | 958 | if (!next) |
998 | break; | 959 | break; |
999 | 960 | ||
1000 | if (cur + size <= rsv->rsv_start) { | 961 | if (cur + size <= rsv->rsv_start) { |
1001 | /* | 962 | /* |
1002 | * Found a reserveable space big enough. We could | 963 | * Found a reserveable space big enough. We could |
1003 | * have a reservation across the group boundary here | 964 | * have a reservation across the group boundary here |
1004 | */ | 965 | */ |
1005 | break; | 966 | break; |
1006 | } | 967 | } |
1007 | } | 968 | } |
1008 | /* | 969 | /* |
1009 | * we come here either : | 970 | * we come here either : |
1010 | * when we reach the end of the whole list, | 971 | * when we reach the end of the whole list, |
1011 | * and there is empty reservable space after last entry in the list. | 972 | * and there is empty reservable space after last entry in the list. |
1012 | * append it to the end of the list. | 973 | * append it to the end of the list. |
1013 | * | 974 | * |
1014 | * or we found one reservable space in the middle of the list, | 975 | * or we found one reservable space in the middle of the list, |
1015 | * return the reservation window that we could append to. | 976 | * return the reservation window that we could append to. |
1016 | * succeed. | 977 | * succeed. |
1017 | */ | 978 | */ |
1018 | 979 | ||
1019 | if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window))) | 980 | if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window))) |
1020 | rsv_window_remove(sb, my_rsv); | 981 | rsv_window_remove(sb, my_rsv); |
1021 | 982 | ||
1022 | /* | 983 | /* |
1023 | * Let's book the whole avaliable window for now. We will check the | 984 | * Let's book the whole avaliable window for now. We will check the |
1024 | * disk bitmap later and then, if there are free blocks then we adjust | 985 | * disk bitmap later and then, if there are free blocks then we adjust |
1025 | * the window size if it's larger than requested. | 986 | * the window size if it's larger than requested. |
1026 | * Otherwise, we will remove this node from the tree next time | 987 | * Otherwise, we will remove this node from the tree next time |
1027 | * call find_next_reservable_window. | 988 | * call find_next_reservable_window. |
1028 | */ | 989 | */ |
1029 | my_rsv->rsv_start = cur; | 990 | my_rsv->rsv_start = cur; |
1030 | my_rsv->rsv_end = cur + size - 1; | 991 | my_rsv->rsv_end = cur + size - 1; |
1031 | my_rsv->rsv_alloc_hit = 0; | 992 | my_rsv->rsv_alloc_hit = 0; |
1032 | 993 | ||
1033 | if (prev != my_rsv) | 994 | if (prev != my_rsv) |
1034 | ext3_rsv_window_add(sb, my_rsv); | 995 | ext3_rsv_window_add(sb, my_rsv); |
1035 | 996 | ||
1036 | return 0; | 997 | return 0; |
1037 | } | 998 | } |
1038 | 999 | ||
1039 | /** | 1000 | /** |
1040 | * alloc_new_reservation()--allocate a new reservation window | 1001 | * alloc_new_reservation()--allocate a new reservation window |
1041 | * | 1002 | * |
1042 | * To make a new reservation, we search part of the filesystem | 1003 | * To make a new reservation, we search part of the filesystem |
1043 | * reservation list (the list that inside the group). We try to | 1004 | * reservation list (the list that inside the group). We try to |
1044 | * allocate a new reservation window near the allocation goal, | 1005 | * allocate a new reservation window near the allocation goal, |
1045 | * or the beginning of the group, if there is no goal. | 1006 | * or the beginning of the group, if there is no goal. |
1046 | * | 1007 | * |
1047 | * We first find a reservable space after the goal, then from | 1008 | * We first find a reservable space after the goal, then from |
1048 | * there, we check the bitmap for the first free block after | 1009 | * there, we check the bitmap for the first free block after |
1049 | * it. If there is no free block until the end of group, then the | 1010 | * it. If there is no free block until the end of group, then the |
1050 | * whole group is full, we failed. Otherwise, check if the free | 1011 | * whole group is full, we failed. Otherwise, check if the free |
1051 | * block is inside the expected reservable space, if so, we | 1012 | * block is inside the expected reservable space, if so, we |
1052 | * succeed. | 1013 | * succeed. |
1053 | * If the first free block is outside the reservable space, then | 1014 | * If the first free block is outside the reservable space, then |
1054 | * start from the first free block, we search for next available | 1015 | * start from the first free block, we search for next available |
1055 | * space, and go on. | 1016 | * space, and go on. |
1056 | * | 1017 | * |
1057 | * on succeed, a new reservation will be found and inserted into the list | 1018 | * on succeed, a new reservation will be found and inserted into the list |
1058 | * It contains at least one free block, and it does not overlap with other | 1019 | * It contains at least one free block, and it does not overlap with other |
1059 | * reservation windows. | 1020 | * reservation windows. |
1060 | * | 1021 | * |
1061 | * failed: we failed to find a reservation window in this group | 1022 | * failed: we failed to find a reservation window in this group |
1062 | * | 1023 | * |
1063 | * @rsv: the reservation | 1024 | * @rsv: the reservation |
1064 | * | 1025 | * |
1065 | * @grp_goal: The goal (group-relative). It is where the search for a | 1026 | * @grp_goal: The goal (group-relative). It is where the search for a |
1066 | * free reservable space should start from. | 1027 | * free reservable space should start from. |
1067 | * if we have a grp_goal(grp_goal >0 ), then start from there, | 1028 | * if we have a grp_goal(grp_goal >0 ), then start from there, |
1068 | * no grp_goal(grp_goal = -1), we start from the first block | 1029 | * no grp_goal(grp_goal = -1), we start from the first block |
1069 | * of the group. | 1030 | * of the group. |
1070 | * | 1031 | * |
1071 | * @sb: the super block | 1032 | * @sb: the super block |
1072 | * @group: the group we are trying to allocate in | 1033 | * @group: the group we are trying to allocate in |
1073 | * @bitmap_bh: the block group block bitmap | 1034 | * @bitmap_bh: the block group block bitmap |
1074 | * | 1035 | * |
1075 | */ | 1036 | */ |
1076 | static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv, | 1037 | static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv, |
1077 | ext3_grpblk_t grp_goal, struct super_block *sb, | 1038 | ext3_grpblk_t grp_goal, struct super_block *sb, |
1078 | unsigned int group, struct buffer_head *bitmap_bh) | 1039 | unsigned int group, struct buffer_head *bitmap_bh) |
1079 | { | 1040 | { |
1080 | struct ext3_reserve_window_node *search_head; | 1041 | struct ext3_reserve_window_node *search_head; |
1081 | ext3_fsblk_t group_first_block, group_end_block, start_block; | 1042 | ext3_fsblk_t group_first_block, group_end_block, start_block; |
1082 | ext3_grpblk_t first_free_block; | 1043 | ext3_grpblk_t first_free_block; |
1083 | struct rb_root *fs_rsv_root = &EXT3_SB(sb)->s_rsv_window_root; | 1044 | struct rb_root *fs_rsv_root = &EXT3_SB(sb)->s_rsv_window_root; |
1084 | unsigned long size; | 1045 | unsigned long size; |
1085 | int ret; | 1046 | int ret; |
1086 | spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock; | 1047 | spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock; |
1087 | 1048 | ||
1088 | group_first_block = ext3_group_first_block_no(sb, group); | 1049 | group_first_block = ext3_group_first_block_no(sb, group); |
1089 | group_end_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1); | 1050 | group_end_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1); |
1090 | 1051 | ||
1091 | if (grp_goal < 0) | 1052 | if (grp_goal < 0) |
1092 | start_block = group_first_block; | 1053 | start_block = group_first_block; |
1093 | else | 1054 | else |
1094 | start_block = grp_goal + group_first_block; | 1055 | start_block = grp_goal + group_first_block; |
1095 | 1056 | ||
1096 | size = my_rsv->rsv_goal_size; | 1057 | size = my_rsv->rsv_goal_size; |
1097 | 1058 | ||
1098 | if (!rsv_is_empty(&my_rsv->rsv_window)) { | 1059 | if (!rsv_is_empty(&my_rsv->rsv_window)) { |
1099 | /* | 1060 | /* |
1100 | * if the old reservation is cross group boundary | 1061 | * if the old reservation is cross group boundary |
1101 | * and if the goal is inside the old reservation window, | 1062 | * and if the goal is inside the old reservation window, |
1102 | * we will come here when we just failed to allocate from | 1063 | * we will come here when we just failed to allocate from |
1103 | * the first part of the window. We still have another part | 1064 | * the first part of the window. We still have another part |
1104 | * that belongs to the next group. In this case, there is no | 1065 | * that belongs to the next group. In this case, there is no |
1105 | * point to discard our window and try to allocate a new one | 1066 | * point to discard our window and try to allocate a new one |
1106 | * in this group(which will fail). we should | 1067 | * in this group(which will fail). we should |
1107 | * keep the reservation window, just simply move on. | 1068 | * keep the reservation window, just simply move on. |
1108 | * | 1069 | * |
1109 | * Maybe we could shift the start block of the reservation | 1070 | * Maybe we could shift the start block of the reservation |
1110 | * window to the first block of next group. | 1071 | * window to the first block of next group. |
1111 | */ | 1072 | */ |
1112 | 1073 | ||
1113 | if ((my_rsv->rsv_start <= group_end_block) && | 1074 | if ((my_rsv->rsv_start <= group_end_block) && |
1114 | (my_rsv->rsv_end > group_end_block) && | 1075 | (my_rsv->rsv_end > group_end_block) && |
1115 | (start_block >= my_rsv->rsv_start)) | 1076 | (start_block >= my_rsv->rsv_start)) |
1116 | return -1; | 1077 | return -1; |
1117 | 1078 | ||
1118 | if ((my_rsv->rsv_alloc_hit > | 1079 | if ((my_rsv->rsv_alloc_hit > |
1119 | (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) { | 1080 | (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) { |
1120 | /* | 1081 | /* |
1121 | * if the previously allocation hit ratio is | 1082 | * if the previously allocation hit ratio is |
1122 | * greater than 1/2, then we double the size of | 1083 | * greater than 1/2, then we double the size of |
1123 | * the reservation window the next time, | 1084 | * the reservation window the next time, |
1124 | * otherwise we keep the same size window | 1085 | * otherwise we keep the same size window |
1125 | */ | 1086 | */ |
1126 | size = size * 2; | 1087 | size = size * 2; |
1127 | if (size > EXT3_MAX_RESERVE_BLOCKS) | 1088 | if (size > EXT3_MAX_RESERVE_BLOCKS) |
1128 | size = EXT3_MAX_RESERVE_BLOCKS; | 1089 | size = EXT3_MAX_RESERVE_BLOCKS; |
1129 | my_rsv->rsv_goal_size= size; | 1090 | my_rsv->rsv_goal_size= size; |
1130 | } | 1091 | } |
1131 | } | 1092 | } |
1132 | 1093 | ||
1133 | spin_lock(rsv_lock); | 1094 | spin_lock(rsv_lock); |
1134 | /* | 1095 | /* |
1135 | * shift the search start to the window near the goal block | 1096 | * shift the search start to the window near the goal block |
1136 | */ | 1097 | */ |
1137 | search_head = search_reserve_window(fs_rsv_root, start_block); | 1098 | search_head = search_reserve_window(fs_rsv_root, start_block); |
1138 | 1099 | ||
1139 | /* | 1100 | /* |
1140 | * find_next_reservable_window() simply finds a reservable window | 1101 | * find_next_reservable_window() simply finds a reservable window |
1141 | * inside the given range(start_block, group_end_block). | 1102 | * inside the given range(start_block, group_end_block). |
1142 | * | 1103 | * |
1143 | * To make sure the reservation window has a free bit inside it, we | 1104 | * To make sure the reservation window has a free bit inside it, we |
1144 | * need to check the bitmap after we found a reservable window. | 1105 | * need to check the bitmap after we found a reservable window. |
1145 | */ | 1106 | */ |
1146 | retry: | 1107 | retry: |
1147 | ret = find_next_reservable_window(search_head, my_rsv, sb, | 1108 | ret = find_next_reservable_window(search_head, my_rsv, sb, |
1148 | start_block, group_end_block); | 1109 | start_block, group_end_block); |
1149 | 1110 | ||
1150 | if (ret == -1) { | 1111 | if (ret == -1) { |
1151 | if (!rsv_is_empty(&my_rsv->rsv_window)) | 1112 | if (!rsv_is_empty(&my_rsv->rsv_window)) |
1152 | rsv_window_remove(sb, my_rsv); | 1113 | rsv_window_remove(sb, my_rsv); |
1153 | spin_unlock(rsv_lock); | 1114 | spin_unlock(rsv_lock); |
1154 | return -1; | 1115 | return -1; |
1155 | } | 1116 | } |
1156 | 1117 | ||
1157 | /* | 1118 | /* |
1158 | * On success, find_next_reservable_window() returns the | 1119 | * On success, find_next_reservable_window() returns the |
1159 | * reservation window where there is a reservable space after it. | 1120 | * reservation window where there is a reservable space after it. |
1160 | * Before we reserve this reservable space, we need | 1121 | * Before we reserve this reservable space, we need |
1161 | * to make sure there is at least a free block inside this region. | 1122 | * to make sure there is at least a free block inside this region. |
1162 | * | 1123 | * |
1163 | * searching the first free bit on the block bitmap and copy of | 1124 | * searching the first free bit on the block bitmap and copy of |
1164 | * last committed bitmap alternatively, until we found a allocatable | 1125 | * last committed bitmap alternatively, until we found a allocatable |
1165 | * block. Search start from the start block of the reservable space | 1126 | * block. Search start from the start block of the reservable space |
1166 | * we just found. | 1127 | * we just found. |
1167 | */ | 1128 | */ |
1168 | spin_unlock(rsv_lock); | 1129 | spin_unlock(rsv_lock); |
1169 | first_free_block = bitmap_search_next_usable_block( | 1130 | first_free_block = bitmap_search_next_usable_block( |
1170 | my_rsv->rsv_start - group_first_block, | 1131 | my_rsv->rsv_start - group_first_block, |
1171 | bitmap_bh, group_end_block - group_first_block + 1); | 1132 | bitmap_bh, group_end_block - group_first_block + 1); |
1172 | 1133 | ||
1173 | if (first_free_block < 0) { | 1134 | if (first_free_block < 0) { |
1174 | /* | 1135 | /* |
1175 | * no free block left on the bitmap, no point | 1136 | * no free block left on the bitmap, no point |
1176 | * to reserve the space. return failed. | 1137 | * to reserve the space. return failed. |
1177 | */ | 1138 | */ |
1178 | spin_lock(rsv_lock); | 1139 | spin_lock(rsv_lock); |
1179 | if (!rsv_is_empty(&my_rsv->rsv_window)) | 1140 | if (!rsv_is_empty(&my_rsv->rsv_window)) |
1180 | rsv_window_remove(sb, my_rsv); | 1141 | rsv_window_remove(sb, my_rsv); |
1181 | spin_unlock(rsv_lock); | 1142 | spin_unlock(rsv_lock); |
1182 | return -1; /* failed */ | 1143 | return -1; /* failed */ |
1183 | } | 1144 | } |
1184 | 1145 | ||
1185 | start_block = first_free_block + group_first_block; | 1146 | start_block = first_free_block + group_first_block; |
1186 | /* | 1147 | /* |
1187 | * check if the first free block is within the | 1148 | * check if the first free block is within the |
1188 | * free space we just reserved | 1149 | * free space we just reserved |
1189 | */ | 1150 | */ |
1190 | if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end) | 1151 | if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end) |
1191 | return 0; /* success */ | 1152 | return 0; /* success */ |
1192 | /* | 1153 | /* |
1193 | * if the first free bit we found is out of the reservable space | 1154 | * if the first free bit we found is out of the reservable space |
1194 | * continue search for next reservable space, | 1155 | * continue search for next reservable space, |
1195 | * start from where the free block is, | 1156 | * start from where the free block is, |
1196 | * we also shift the list head to where we stopped last time | 1157 | * we also shift the list head to where we stopped last time |
1197 | */ | 1158 | */ |
1198 | search_head = my_rsv; | 1159 | search_head = my_rsv; |
1199 | spin_lock(rsv_lock); | 1160 | spin_lock(rsv_lock); |
1200 | goto retry; | 1161 | goto retry; |
1201 | } | 1162 | } |
1202 | 1163 | ||
1203 | /** | 1164 | /** |
1204 | * try_to_extend_reservation() | 1165 | * try_to_extend_reservation() |
1205 | * @my_rsv: given reservation window | 1166 | * @my_rsv: given reservation window |
1206 | * @sb: super block | 1167 | * @sb: super block |
1207 | * @size: the delta to extend | 1168 | * @size: the delta to extend |
1208 | * | 1169 | * |
1209 | * Attempt to expand the reservation window large enough to have | 1170 | * Attempt to expand the reservation window large enough to have |
1210 | * required number of free blocks | 1171 | * required number of free blocks |
1211 | * | 1172 | * |
1212 | * Since ext3_try_to_allocate() will always allocate blocks within | 1173 | * Since ext3_try_to_allocate() will always allocate blocks within |
1213 | * the reservation window range, if the window size is too small, | 1174 | * the reservation window range, if the window size is too small, |
1214 | * multiple blocks allocation has to stop at the end of the reservation | 1175 | * multiple blocks allocation has to stop at the end of the reservation |
1215 | * window. To make this more efficient, given the total number of | 1176 | * window. To make this more efficient, given the total number of |
1216 | * blocks needed and the current size of the window, we try to | 1177 | * blocks needed and the current size of the window, we try to |
1217 | * expand the reservation window size if necessary on a best-effort | 1178 | * expand the reservation window size if necessary on a best-effort |
1218 | * basis before ext3_new_blocks() tries to allocate blocks, | 1179 | * basis before ext3_new_blocks() tries to allocate blocks, |
1219 | */ | 1180 | */ |
1220 | static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv, | 1181 | static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv, |
1221 | struct super_block *sb, int size) | 1182 | struct super_block *sb, int size) |
1222 | { | 1183 | { |
1223 | struct ext3_reserve_window_node *next_rsv; | 1184 | struct ext3_reserve_window_node *next_rsv; |
1224 | struct rb_node *next; | 1185 | struct rb_node *next; |
1225 | spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock; | 1186 | spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock; |
1226 | 1187 | ||
1227 | if (!spin_trylock(rsv_lock)) | 1188 | if (!spin_trylock(rsv_lock)) |
1228 | return; | 1189 | return; |
1229 | 1190 | ||
1230 | next = rb_next(&my_rsv->rsv_node); | 1191 | next = rb_next(&my_rsv->rsv_node); |
1231 | 1192 | ||
1232 | if (!next) | 1193 | if (!next) |
1233 | my_rsv->rsv_end += size; | 1194 | my_rsv->rsv_end += size; |
1234 | else { | 1195 | else { |
1235 | next_rsv = rb_entry(next, struct ext3_reserve_window_node, rsv_node); | 1196 | next_rsv = rb_entry(next, struct ext3_reserve_window_node, rsv_node); |
1236 | 1197 | ||
1237 | if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size) | 1198 | if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size) |
1238 | my_rsv->rsv_end += size; | 1199 | my_rsv->rsv_end += size; |
1239 | else | 1200 | else |
1240 | my_rsv->rsv_end = next_rsv->rsv_start - 1; | 1201 | my_rsv->rsv_end = next_rsv->rsv_start - 1; |
1241 | } | 1202 | } |
1242 | spin_unlock(rsv_lock); | 1203 | spin_unlock(rsv_lock); |
1243 | } | 1204 | } |
1244 | 1205 | ||
1245 | /** | 1206 | /** |
1246 | * ext3_try_to_allocate_with_rsv() | 1207 | * ext3_try_to_allocate_with_rsv() |
1247 | * @sb: superblock | 1208 | * @sb: superblock |
1248 | * @handle: handle to this transaction | 1209 | * @handle: handle to this transaction |
1249 | * @group: given allocation block group | 1210 | * @group: given allocation block group |
1250 | * @bitmap_bh: bufferhead holds the block bitmap | 1211 | * @bitmap_bh: bufferhead holds the block bitmap |
1251 | * @grp_goal: given target block within the group | 1212 | * @grp_goal: given target block within the group |
1252 | * @count: target number of blocks to allocate | 1213 | * @count: target number of blocks to allocate |
1253 | * @my_rsv: reservation window | 1214 | * @my_rsv: reservation window |
1254 | * @errp: pointer to store the error code | 1215 | * @errp: pointer to store the error code |
1255 | * | 1216 | * |
1256 | * This is the main function used to allocate a new block and its reservation | 1217 | * This is the main function used to allocate a new block and its reservation |
1257 | * window. | 1218 | * window. |
1258 | * | 1219 | * |
1259 | * Each time when a new block allocation is need, first try to allocate from | 1220 | * Each time when a new block allocation is need, first try to allocate from |
1260 | * its own reservation. If it does not have a reservation window, instead of | 1221 | * its own reservation. If it does not have a reservation window, instead of |
1261 | * looking for a free bit on bitmap first, then look up the reservation list to | 1222 | * looking for a free bit on bitmap first, then look up the reservation list to |
1262 | * see if it is inside somebody else's reservation window, we try to allocate a | 1223 | * see if it is inside somebody else's reservation window, we try to allocate a |
1263 | * reservation window for it starting from the goal first. Then do the block | 1224 | * reservation window for it starting from the goal first. Then do the block |
1264 | * allocation within the reservation window. | 1225 | * allocation within the reservation window. |
1265 | * | 1226 | * |
1266 | * This will avoid keeping on searching the reservation list again and | 1227 | * This will avoid keeping on searching the reservation list again and |
1267 | * again when somebody is looking for a free block (without | 1228 | * again when somebody is looking for a free block (without |
1268 | * reservation), and there are lots of free blocks, but they are all | 1229 | * reservation), and there are lots of free blocks, but they are all |
1269 | * being reserved. | 1230 | * being reserved. |
1270 | * | 1231 | * |
1271 | * We use a red-black tree for the per-filesystem reservation list. | 1232 | * We use a red-black tree for the per-filesystem reservation list. |
1272 | * | 1233 | * |
1273 | */ | 1234 | */ |
1274 | static ext3_grpblk_t | 1235 | static ext3_grpblk_t |
1275 | ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle, | 1236 | ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle, |
1276 | unsigned int group, struct buffer_head *bitmap_bh, | 1237 | unsigned int group, struct buffer_head *bitmap_bh, |
1277 | ext3_grpblk_t grp_goal, | 1238 | ext3_grpblk_t grp_goal, |
1278 | struct ext3_reserve_window_node * my_rsv, | 1239 | struct ext3_reserve_window_node * my_rsv, |
1279 | unsigned long *count, int *errp) | 1240 | unsigned long *count, int *errp) |
1280 | { | 1241 | { |
1281 | ext3_fsblk_t group_first_block, group_last_block; | 1242 | ext3_fsblk_t group_first_block, group_last_block; |
1282 | ext3_grpblk_t ret = 0; | 1243 | ext3_grpblk_t ret = 0; |
1283 | int fatal; | 1244 | int fatal; |
1284 | unsigned long num = *count; | 1245 | unsigned long num = *count; |
1285 | 1246 | ||
1286 | *errp = 0; | 1247 | *errp = 0; |
1287 | 1248 | ||
1288 | /* | 1249 | /* |
1289 | * Make sure we use undo access for the bitmap, because it is critical | 1250 | * Make sure we use undo access for the bitmap, because it is critical |
1290 | * that we do the frozen_data COW on bitmap buffers in all cases even | 1251 | * that we do the frozen_data COW on bitmap buffers in all cases even |
1291 | * if the buffer is in BJ_Forget state in the committing transaction. | 1252 | * if the buffer is in BJ_Forget state in the committing transaction. |
1292 | */ | 1253 | */ |
1293 | BUFFER_TRACE(bitmap_bh, "get undo access for new block"); | 1254 | BUFFER_TRACE(bitmap_bh, "get undo access for new block"); |
1294 | fatal = ext3_journal_get_undo_access(handle, bitmap_bh); | 1255 | fatal = ext3_journal_get_undo_access(handle, bitmap_bh); |
1295 | if (fatal) { | 1256 | if (fatal) { |
1296 | *errp = fatal; | 1257 | *errp = fatal; |
1297 | return -1; | 1258 | return -1; |
1298 | } | 1259 | } |
1299 | 1260 | ||
1300 | /* | 1261 | /* |
1301 | * we don't deal with reservation when | 1262 | * we don't deal with reservation when |
1302 | * filesystem is mounted without reservation | 1263 | * filesystem is mounted without reservation |
1303 | * or the file is not a regular file | 1264 | * or the file is not a regular file |
1304 | * or last attempt to allocate a block with reservation turned on failed | 1265 | * or last attempt to allocate a block with reservation turned on failed |
1305 | */ | 1266 | */ |
1306 | if (my_rsv == NULL ) { | 1267 | if (my_rsv == NULL ) { |
1307 | ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh, | 1268 | ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh, |
1308 | grp_goal, count, NULL); | 1269 | grp_goal, count, NULL); |
1309 | goto out; | 1270 | goto out; |
1310 | } | 1271 | } |
1311 | /* | 1272 | /* |
1312 | * grp_goal is a group relative block number (if there is a goal) | 1273 | * grp_goal is a group relative block number (if there is a goal) |
1313 | * 0 <= grp_goal < EXT3_BLOCKS_PER_GROUP(sb) | 1274 | * 0 <= grp_goal < EXT3_BLOCKS_PER_GROUP(sb) |
1314 | * first block is a filesystem wide block number | 1275 | * first block is a filesystem wide block number |
1315 | * first block is the block number of the first block in this group | 1276 | * first block is the block number of the first block in this group |
1316 | */ | 1277 | */ |
1317 | group_first_block = ext3_group_first_block_no(sb, group); | 1278 | group_first_block = ext3_group_first_block_no(sb, group); |
1318 | group_last_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1); | 1279 | group_last_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1); |
1319 | 1280 | ||
1320 | /* | 1281 | /* |
1321 | * Basically we will allocate a new block from inode's reservation | 1282 | * Basically we will allocate a new block from inode's reservation |
1322 | * window. | 1283 | * window. |
1323 | * | 1284 | * |
1324 | * We need to allocate a new reservation window, if: | 1285 | * We need to allocate a new reservation window, if: |
1325 | * a) inode does not have a reservation window; or | 1286 | * a) inode does not have a reservation window; or |
1326 | * b) last attempt to allocate a block from existing reservation | 1287 | * b) last attempt to allocate a block from existing reservation |
1327 | * failed; or | 1288 | * failed; or |
1328 | * c) we come here with a goal and with a reservation window | 1289 | * c) we come here with a goal and with a reservation window |
1329 | * | 1290 | * |
1330 | * We do not need to allocate a new reservation window if we come here | 1291 | * We do not need to allocate a new reservation window if we come here |
1331 | * at the beginning with a goal and the goal is inside the window, or | 1292 | * at the beginning with a goal and the goal is inside the window, or |
1332 | * we don't have a goal but already have a reservation window. | 1293 | * we don't have a goal but already have a reservation window. |
1333 | * then we could go to allocate from the reservation window directly. | 1294 | * then we could go to allocate from the reservation window directly. |
1334 | */ | 1295 | */ |
1335 | while (1) { | 1296 | while (1) { |
1336 | if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) || | 1297 | if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) || |
1337 | !goal_in_my_reservation(&my_rsv->rsv_window, | 1298 | !goal_in_my_reservation(&my_rsv->rsv_window, |
1338 | grp_goal, group, sb)) { | 1299 | grp_goal, group, sb)) { |
1339 | if (my_rsv->rsv_goal_size < *count) | 1300 | if (my_rsv->rsv_goal_size < *count) |
1340 | my_rsv->rsv_goal_size = *count; | 1301 | my_rsv->rsv_goal_size = *count; |
1341 | ret = alloc_new_reservation(my_rsv, grp_goal, sb, | 1302 | ret = alloc_new_reservation(my_rsv, grp_goal, sb, |
1342 | group, bitmap_bh); | 1303 | group, bitmap_bh); |
1343 | if (ret < 0) | 1304 | if (ret < 0) |
1344 | break; /* failed */ | 1305 | break; /* failed */ |
1345 | 1306 | ||
1346 | if (!goal_in_my_reservation(&my_rsv->rsv_window, | 1307 | if (!goal_in_my_reservation(&my_rsv->rsv_window, |
1347 | grp_goal, group, sb)) | 1308 | grp_goal, group, sb)) |
1348 | grp_goal = -1; | 1309 | grp_goal = -1; |
1349 | } else if (grp_goal >= 0) { | 1310 | } else if (grp_goal >= 0) { |
1350 | int curr = my_rsv->rsv_end - | 1311 | int curr = my_rsv->rsv_end - |
1351 | (grp_goal + group_first_block) + 1; | 1312 | (grp_goal + group_first_block) + 1; |
1352 | 1313 | ||
1353 | if (curr < *count) | 1314 | if (curr < *count) |
1354 | try_to_extend_reservation(my_rsv, sb, | 1315 | try_to_extend_reservation(my_rsv, sb, |
1355 | *count - curr); | 1316 | *count - curr); |
1356 | } | 1317 | } |
1357 | 1318 | ||
1358 | if ((my_rsv->rsv_start > group_last_block) || | 1319 | if ((my_rsv->rsv_start > group_last_block) || |
1359 | (my_rsv->rsv_end < group_first_block)) { | 1320 | (my_rsv->rsv_end < group_first_block)) { |
1360 | rsv_window_dump(&EXT3_SB(sb)->s_rsv_window_root, 1); | 1321 | rsv_window_dump(&EXT3_SB(sb)->s_rsv_window_root, 1); |
1361 | BUG(); | 1322 | BUG(); |
1362 | } | 1323 | } |
1363 | ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh, | 1324 | ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh, |
1364 | grp_goal, &num, &my_rsv->rsv_window); | 1325 | grp_goal, &num, &my_rsv->rsv_window); |
1365 | if (ret >= 0) { | 1326 | if (ret >= 0) { |
1366 | my_rsv->rsv_alloc_hit += num; | 1327 | my_rsv->rsv_alloc_hit += num; |
1367 | *count = num; | 1328 | *count = num; |
1368 | break; /* succeed */ | 1329 | break; /* succeed */ |
1369 | } | 1330 | } |
1370 | num = *count; | 1331 | num = *count; |
1371 | } | 1332 | } |
1372 | out: | 1333 | out: |
1373 | if (ret >= 0) { | 1334 | if (ret >= 0) { |
1374 | BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for " | 1335 | BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for " |
1375 | "bitmap block"); | 1336 | "bitmap block"); |
1376 | fatal = ext3_journal_dirty_metadata(handle, bitmap_bh); | 1337 | fatal = ext3_journal_dirty_metadata(handle, bitmap_bh); |
1377 | if (fatal) { | 1338 | if (fatal) { |
1378 | *errp = fatal; | 1339 | *errp = fatal; |
1379 | return -1; | 1340 | return -1; |
1380 | } | 1341 | } |
1381 | return ret; | 1342 | return ret; |
1382 | } | 1343 | } |
1383 | 1344 | ||
1384 | BUFFER_TRACE(bitmap_bh, "journal_release_buffer"); | 1345 | BUFFER_TRACE(bitmap_bh, "journal_release_buffer"); |
1385 | ext3_journal_release_buffer(handle, bitmap_bh); | 1346 | ext3_journal_release_buffer(handle, bitmap_bh); |
1386 | return ret; | 1347 | return ret; |
1387 | } | 1348 | } |
1388 | 1349 | ||
1389 | /** | 1350 | /** |
1390 | * ext3_has_free_blocks() | 1351 | * ext3_has_free_blocks() |
1391 | * @sbi: in-core super block structure. | 1352 | * @sbi: in-core super block structure. |
1392 | * | 1353 | * |
1393 | * Check if filesystem has at least 1 free block available for allocation. | 1354 | * Check if filesystem has at least 1 free block available for allocation. |
1394 | */ | 1355 | */ |
1395 | static int ext3_has_free_blocks(struct ext3_sb_info *sbi) | 1356 | static int ext3_has_free_blocks(struct ext3_sb_info *sbi) |
1396 | { | 1357 | { |
1397 | ext3_fsblk_t free_blocks, root_blocks; | 1358 | ext3_fsblk_t free_blocks, root_blocks; |
1398 | 1359 | ||
1399 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); | 1360 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
1400 | root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count); | 1361 | root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count); |
1401 | if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) && | 1362 | if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) && |
1402 | sbi->s_resuid != current->fsuid && | 1363 | sbi->s_resuid != current->fsuid && |
1403 | (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) { | 1364 | (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) { |
1404 | return 0; | 1365 | return 0; |
1405 | } | 1366 | } |
1406 | return 1; | 1367 | return 1; |
1407 | } | 1368 | } |
1408 | 1369 | ||
1409 | /** | 1370 | /** |
1410 | * ext3_should_retry_alloc() | 1371 | * ext3_should_retry_alloc() |
1411 | * @sb: super block | 1372 | * @sb: super block |
1412 | * @retries number of attemps has been made | 1373 | * @retries number of attemps has been made |
1413 | * | 1374 | * |
1414 | * ext3_should_retry_alloc() is called when ENOSPC is returned, and if | 1375 | * ext3_should_retry_alloc() is called when ENOSPC is returned, and if |
1415 | * it is profitable to retry the operation, this function will wait | 1376 | * it is profitable to retry the operation, this function will wait |
1416 | * for the current or commiting transaction to complete, and then | 1377 | * for the current or commiting transaction to complete, and then |
1417 | * return TRUE. | 1378 | * return TRUE. |
1418 | * | 1379 | * |
1419 | * if the total number of retries exceed three times, return FALSE. | 1380 | * if the total number of retries exceed three times, return FALSE. |
1420 | */ | 1381 | */ |
1421 | int ext3_should_retry_alloc(struct super_block *sb, int *retries) | 1382 | int ext3_should_retry_alloc(struct super_block *sb, int *retries) |
1422 | { | 1383 | { |
1423 | if (!ext3_has_free_blocks(EXT3_SB(sb)) || (*retries)++ > 3) | 1384 | if (!ext3_has_free_blocks(EXT3_SB(sb)) || (*retries)++ > 3) |
1424 | return 0; | 1385 | return 0; |
1425 | 1386 | ||
1426 | jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id); | 1387 | jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id); |
1427 | 1388 | ||
1428 | return journal_force_commit_nested(EXT3_SB(sb)->s_journal); | 1389 | return journal_force_commit_nested(EXT3_SB(sb)->s_journal); |
1429 | } | 1390 | } |
1430 | 1391 | ||
1431 | /** | 1392 | /** |
1432 | * ext3_new_blocks() -- core block(s) allocation function | 1393 | * ext3_new_blocks() -- core block(s) allocation function |
1433 | * @handle: handle to this transaction | 1394 | * @handle: handle to this transaction |
1434 | * @inode: file inode | 1395 | * @inode: file inode |
1435 | * @goal: given target block(filesystem wide) | 1396 | * @goal: given target block(filesystem wide) |
1436 | * @count: target number of blocks to allocate | 1397 | * @count: target number of blocks to allocate |
1437 | * @errp: error code | 1398 | * @errp: error code |
1438 | * | 1399 | * |
1439 | * ext3_new_blocks uses a goal block to assist allocation. It tries to | 1400 | * ext3_new_blocks uses a goal block to assist allocation. It tries to |
1440 | * allocate block(s) from the block group contains the goal block first. If that | 1401 | * allocate block(s) from the block group contains the goal block first. If that |
1441 | * fails, it will try to allocate block(s) from other block groups without | 1402 | * fails, it will try to allocate block(s) from other block groups without |
1442 | * any specific goal block. | 1403 | * any specific goal block. |
1443 | * | 1404 | * |
1444 | */ | 1405 | */ |
1445 | ext3_fsblk_t ext3_new_blocks(handle_t *handle, struct inode *inode, | 1406 | ext3_fsblk_t ext3_new_blocks(handle_t *handle, struct inode *inode, |
1446 | ext3_fsblk_t goal, unsigned long *count, int *errp) | 1407 | ext3_fsblk_t goal, unsigned long *count, int *errp) |
1447 | { | 1408 | { |
1448 | struct buffer_head *bitmap_bh = NULL; | 1409 | struct buffer_head *bitmap_bh = NULL; |
1449 | struct buffer_head *gdp_bh; | 1410 | struct buffer_head *gdp_bh; |
1450 | int group_no; | 1411 | int group_no; |
1451 | int goal_group; | 1412 | int goal_group; |
1452 | ext3_grpblk_t grp_target_blk; /* blockgroup relative goal block */ | 1413 | ext3_grpblk_t grp_target_blk; /* blockgroup relative goal block */ |
1453 | ext3_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/ | 1414 | ext3_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/ |
1454 | ext3_fsblk_t ret_block; /* filesyetem-wide allocated block */ | 1415 | ext3_fsblk_t ret_block; /* filesyetem-wide allocated block */ |
1455 | int bgi; /* blockgroup iteration index */ | 1416 | int bgi; /* blockgroup iteration index */ |
1456 | int fatal = 0, err; | 1417 | int fatal = 0, err; |
1457 | int performed_allocation = 0; | 1418 | int performed_allocation = 0; |
1458 | ext3_grpblk_t free_blocks; /* number of free blocks in a group */ | 1419 | ext3_grpblk_t free_blocks; /* number of free blocks in a group */ |
1459 | struct super_block *sb; | 1420 | struct super_block *sb; |
1460 | struct ext3_group_desc *gdp; | 1421 | struct ext3_group_desc *gdp; |
1461 | struct ext3_super_block *es; | 1422 | struct ext3_super_block *es; |
1462 | struct ext3_sb_info *sbi; | 1423 | struct ext3_sb_info *sbi; |
1463 | struct ext3_reserve_window_node *my_rsv = NULL; | 1424 | struct ext3_reserve_window_node *my_rsv = NULL; |
1464 | struct ext3_block_alloc_info *block_i; | 1425 | struct ext3_block_alloc_info *block_i; |
1465 | unsigned short windowsz = 0; | 1426 | unsigned short windowsz = 0; |
1466 | #ifdef EXT3FS_DEBUG | 1427 | #ifdef EXT3FS_DEBUG |
1467 | static int goal_hits, goal_attempts; | 1428 | static int goal_hits, goal_attempts; |
1468 | #endif | 1429 | #endif |
1469 | unsigned long ngroups; | 1430 | unsigned long ngroups; |
1470 | unsigned long num = *count; | 1431 | unsigned long num = *count; |
1471 | 1432 | ||
1472 | *errp = -ENOSPC; | 1433 | *errp = -ENOSPC; |
1473 | sb = inode->i_sb; | 1434 | sb = inode->i_sb; |
1474 | if (!sb) { | 1435 | if (!sb) { |
1475 | printk("ext3_new_block: nonexistent device"); | 1436 | printk("ext3_new_block: nonexistent device"); |
1476 | return 0; | 1437 | return 0; |
1477 | } | 1438 | } |
1478 | 1439 | ||
1479 | /* | 1440 | /* |
1480 | * Check quota for allocation of this block. | 1441 | * Check quota for allocation of this block. |
1481 | */ | 1442 | */ |
1482 | if (DQUOT_ALLOC_BLOCK(inode, num)) { | 1443 | if (DQUOT_ALLOC_BLOCK(inode, num)) { |
1483 | *errp = -EDQUOT; | 1444 | *errp = -EDQUOT; |
1484 | return 0; | 1445 | return 0; |
1485 | } | 1446 | } |
1486 | 1447 | ||
1487 | sbi = EXT3_SB(sb); | 1448 | sbi = EXT3_SB(sb); |
1488 | es = EXT3_SB(sb)->s_es; | 1449 | es = EXT3_SB(sb)->s_es; |
1489 | ext3_debug("goal=%lu.\n", goal); | 1450 | ext3_debug("goal=%lu.\n", goal); |
1490 | /* | 1451 | /* |
1491 | * Allocate a block from reservation only when | 1452 | * Allocate a block from reservation only when |
1492 | * filesystem is mounted with reservation(default,-o reservation), and | 1453 | * filesystem is mounted with reservation(default,-o reservation), and |
1493 | * it's a regular file, and | 1454 | * it's a regular file, and |
1494 | * the desired window size is greater than 0 (One could use ioctl | 1455 | * the desired window size is greater than 0 (One could use ioctl |
1495 | * command EXT3_IOC_SETRSVSZ to set the window size to 0 to turn off | 1456 | * command EXT3_IOC_SETRSVSZ to set the window size to 0 to turn off |
1496 | * reservation on that particular file) | 1457 | * reservation on that particular file) |
1497 | */ | 1458 | */ |
1498 | block_i = EXT3_I(inode)->i_block_alloc_info; | 1459 | block_i = EXT3_I(inode)->i_block_alloc_info; |
1499 | if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0)) | 1460 | if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0)) |
1500 | my_rsv = &block_i->rsv_window_node; | 1461 | my_rsv = &block_i->rsv_window_node; |
1501 | 1462 | ||
1502 | if (!ext3_has_free_blocks(sbi)) { | 1463 | if (!ext3_has_free_blocks(sbi)) { |
1503 | *errp = -ENOSPC; | 1464 | *errp = -ENOSPC; |
1504 | goto out; | 1465 | goto out; |
1505 | } | 1466 | } |
1506 | 1467 | ||
1507 | /* | 1468 | /* |
1508 | * First, test whether the goal block is free. | 1469 | * First, test whether the goal block is free. |
1509 | */ | 1470 | */ |
1510 | if (goal < le32_to_cpu(es->s_first_data_block) || | 1471 | if (goal < le32_to_cpu(es->s_first_data_block) || |
1511 | goal >= le32_to_cpu(es->s_blocks_count)) | 1472 | goal >= le32_to_cpu(es->s_blocks_count)) |
1512 | goal = le32_to_cpu(es->s_first_data_block); | 1473 | goal = le32_to_cpu(es->s_first_data_block); |
1513 | group_no = (goal - le32_to_cpu(es->s_first_data_block)) / | 1474 | group_no = (goal - le32_to_cpu(es->s_first_data_block)) / |
1514 | EXT3_BLOCKS_PER_GROUP(sb); | 1475 | EXT3_BLOCKS_PER_GROUP(sb); |
1515 | goal_group = group_no; | 1476 | goal_group = group_no; |
1516 | retry_alloc: | 1477 | retry_alloc: |
1517 | gdp = ext3_get_group_desc(sb, group_no, &gdp_bh); | 1478 | gdp = ext3_get_group_desc(sb, group_no, &gdp_bh); |
1518 | if (!gdp) | 1479 | if (!gdp) |
1519 | goto io_error; | 1480 | goto io_error; |
1520 | 1481 | ||
1521 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); | 1482 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); |
1522 | /* | 1483 | /* |
1523 | * if there is not enough free blocks to make a new resevation | 1484 | * if there is not enough free blocks to make a new resevation |
1524 | * turn off reservation for this allocation | 1485 | * turn off reservation for this allocation |
1525 | */ | 1486 | */ |
1526 | if (my_rsv && (free_blocks < windowsz) | 1487 | if (my_rsv && (free_blocks < windowsz) |
1527 | && (rsv_is_empty(&my_rsv->rsv_window))) | 1488 | && (rsv_is_empty(&my_rsv->rsv_window))) |
1528 | my_rsv = NULL; | 1489 | my_rsv = NULL; |
1529 | 1490 | ||
1530 | if (free_blocks > 0) { | 1491 | if (free_blocks > 0) { |
1531 | grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) % | 1492 | grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) % |
1532 | EXT3_BLOCKS_PER_GROUP(sb)); | 1493 | EXT3_BLOCKS_PER_GROUP(sb)); |
1533 | bitmap_bh = read_block_bitmap(sb, group_no); | 1494 | bitmap_bh = read_block_bitmap(sb, group_no); |
1534 | if (!bitmap_bh) | 1495 | if (!bitmap_bh) |
1535 | goto io_error; | 1496 | goto io_error; |
1536 | grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle, | 1497 | grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle, |
1537 | group_no, bitmap_bh, grp_target_blk, | 1498 | group_no, bitmap_bh, grp_target_blk, |
1538 | my_rsv, &num, &fatal); | 1499 | my_rsv, &num, &fatal); |
1539 | if (fatal) | 1500 | if (fatal) |
1540 | goto out; | 1501 | goto out; |
1541 | if (grp_alloc_blk >= 0) | 1502 | if (grp_alloc_blk >= 0) |
1542 | goto allocated; | 1503 | goto allocated; |
1543 | } | 1504 | } |
1544 | 1505 | ||
1545 | ngroups = EXT3_SB(sb)->s_groups_count; | 1506 | ngroups = EXT3_SB(sb)->s_groups_count; |
1546 | smp_rmb(); | 1507 | smp_rmb(); |
1547 | 1508 | ||
1548 | /* | 1509 | /* |
1549 | * Now search the rest of the groups. We assume that | 1510 | * Now search the rest of the groups. We assume that |
1550 | * i and gdp correctly point to the last group visited. | 1511 | * i and gdp correctly point to the last group visited. |
1551 | */ | 1512 | */ |
1552 | for (bgi = 0; bgi < ngroups; bgi++) { | 1513 | for (bgi = 0; bgi < ngroups; bgi++) { |
1553 | group_no++; | 1514 | group_no++; |
1554 | if (group_no >= ngroups) | 1515 | if (group_no >= ngroups) |
1555 | group_no = 0; | 1516 | group_no = 0; |
1556 | gdp = ext3_get_group_desc(sb, group_no, &gdp_bh); | 1517 | gdp = ext3_get_group_desc(sb, group_no, &gdp_bh); |
1557 | if (!gdp) | 1518 | if (!gdp) |
1558 | goto io_error; | 1519 | goto io_error; |
1559 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); | 1520 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); |
1560 | /* | 1521 | /* |
1561 | * skip this group if the number of | 1522 | * skip this group if the number of |
1562 | * free blocks is less than half of the reservation | 1523 | * free blocks is less than half of the reservation |
1563 | * window size. | 1524 | * window size. |
1564 | */ | 1525 | */ |
1565 | if (free_blocks <= (windowsz/2)) | 1526 | if (free_blocks <= (windowsz/2)) |
1566 | continue; | 1527 | continue; |
1567 | 1528 | ||
1568 | brelse(bitmap_bh); | 1529 | brelse(bitmap_bh); |
1569 | bitmap_bh = read_block_bitmap(sb, group_no); | 1530 | bitmap_bh = read_block_bitmap(sb, group_no); |
1570 | if (!bitmap_bh) | 1531 | if (!bitmap_bh) |
1571 | goto io_error; | 1532 | goto io_error; |
1572 | /* | 1533 | /* |
1573 | * try to allocate block(s) from this group, without a goal(-1). | 1534 | * try to allocate block(s) from this group, without a goal(-1). |
1574 | */ | 1535 | */ |
1575 | grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle, | 1536 | grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle, |
1576 | group_no, bitmap_bh, -1, my_rsv, | 1537 | group_no, bitmap_bh, -1, my_rsv, |
1577 | &num, &fatal); | 1538 | &num, &fatal); |
1578 | if (fatal) | 1539 | if (fatal) |
1579 | goto out; | 1540 | goto out; |
1580 | if (grp_alloc_blk >= 0) | 1541 | if (grp_alloc_blk >= 0) |
1581 | goto allocated; | 1542 | goto allocated; |
1582 | } | 1543 | } |
1583 | /* | 1544 | /* |
1584 | * We may end up a bogus ealier ENOSPC error due to | 1545 | * We may end up a bogus ealier ENOSPC error due to |
1585 | * filesystem is "full" of reservations, but | 1546 | * filesystem is "full" of reservations, but |
1586 | * there maybe indeed free blocks avaliable on disk | 1547 | * there maybe indeed free blocks avaliable on disk |
1587 | * In this case, we just forget about the reservations | 1548 | * In this case, we just forget about the reservations |
1588 | * just do block allocation as without reservations. | 1549 | * just do block allocation as without reservations. |
1589 | */ | 1550 | */ |
1590 | if (my_rsv) { | 1551 | if (my_rsv) { |
1591 | my_rsv = NULL; | 1552 | my_rsv = NULL; |
1592 | windowsz = 0; | 1553 | windowsz = 0; |
1593 | group_no = goal_group; | 1554 | group_no = goal_group; |
1594 | goto retry_alloc; | 1555 | goto retry_alloc; |
1595 | } | 1556 | } |
1596 | /* No space left on the device */ | 1557 | /* No space left on the device */ |
1597 | *errp = -ENOSPC; | 1558 | *errp = -ENOSPC; |
1598 | goto out; | 1559 | goto out; |
1599 | 1560 | ||
1600 | allocated: | 1561 | allocated: |
1601 | 1562 | ||
1602 | ext3_debug("using block group %d(%d)\n", | 1563 | ext3_debug("using block group %d(%d)\n", |
1603 | group_no, gdp->bg_free_blocks_count); | 1564 | group_no, gdp->bg_free_blocks_count); |
1604 | 1565 | ||
1605 | BUFFER_TRACE(gdp_bh, "get_write_access"); | 1566 | BUFFER_TRACE(gdp_bh, "get_write_access"); |
1606 | fatal = ext3_journal_get_write_access(handle, gdp_bh); | 1567 | fatal = ext3_journal_get_write_access(handle, gdp_bh); |
1607 | if (fatal) | 1568 | if (fatal) |
1608 | goto out; | 1569 | goto out; |
1609 | 1570 | ||
1610 | ret_block = grp_alloc_blk + ext3_group_first_block_no(sb, group_no); | 1571 | ret_block = grp_alloc_blk + ext3_group_first_block_no(sb, group_no); |
1611 | 1572 | ||
1612 | if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) || | 1573 | if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) || |
1613 | in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) || | 1574 | in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) || |
1614 | in_range(ret_block, le32_to_cpu(gdp->bg_inode_table), | 1575 | in_range(ret_block, le32_to_cpu(gdp->bg_inode_table), |
1615 | EXT3_SB(sb)->s_itb_per_group) || | 1576 | EXT3_SB(sb)->s_itb_per_group) || |
1616 | in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table), | 1577 | in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table), |
1617 | EXT3_SB(sb)->s_itb_per_group)) | 1578 | EXT3_SB(sb)->s_itb_per_group)) |
1618 | ext3_error(sb, "ext3_new_block", | 1579 | ext3_error(sb, "ext3_new_block", |
1619 | "Allocating block in system zone - " | 1580 | "Allocating block in system zone - " |
1620 | "blocks from "E3FSBLK", length %lu", | 1581 | "blocks from "E3FSBLK", length %lu", |
1621 | ret_block, num); | 1582 | ret_block, num); |
1622 | 1583 | ||
1623 | performed_allocation = 1; | 1584 | performed_allocation = 1; |
1624 | 1585 | ||
1625 | #ifdef CONFIG_JBD_DEBUG | 1586 | #ifdef CONFIG_JBD_DEBUG |
1626 | { | 1587 | { |
1627 | struct buffer_head *debug_bh; | 1588 | struct buffer_head *debug_bh; |
1628 | 1589 | ||
1629 | /* Record bitmap buffer state in the newly allocated block */ | 1590 | /* Record bitmap buffer state in the newly allocated block */ |
1630 | debug_bh = sb_find_get_block(sb, ret_block); | 1591 | debug_bh = sb_find_get_block(sb, ret_block); |
1631 | if (debug_bh) { | 1592 | if (debug_bh) { |
1632 | BUFFER_TRACE(debug_bh, "state when allocated"); | 1593 | BUFFER_TRACE(debug_bh, "state when allocated"); |
1633 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state"); | 1594 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state"); |
1634 | brelse(debug_bh); | 1595 | brelse(debug_bh); |
1635 | } | 1596 | } |
1636 | } | 1597 | } |
1637 | jbd_lock_bh_state(bitmap_bh); | 1598 | jbd_lock_bh_state(bitmap_bh); |
1638 | spin_lock(sb_bgl_lock(sbi, group_no)); | 1599 | spin_lock(sb_bgl_lock(sbi, group_no)); |
1639 | if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) { | 1600 | if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) { |
1640 | int i; | 1601 | int i; |
1641 | 1602 | ||
1642 | for (i = 0; i < num; i++) { | 1603 | for (i = 0; i < num; i++) { |
1643 | if (ext3_test_bit(grp_alloc_blk+i, | 1604 | if (ext3_test_bit(grp_alloc_blk+i, |
1644 | bh2jh(bitmap_bh)->b_committed_data)) { | 1605 | bh2jh(bitmap_bh)->b_committed_data)) { |
1645 | printk("%s: block was unexpectedly set in " | 1606 | printk("%s: block was unexpectedly set in " |
1646 | "b_committed_data\n", __FUNCTION__); | 1607 | "b_committed_data\n", __FUNCTION__); |
1647 | } | 1608 | } |
1648 | } | 1609 | } |
1649 | } | 1610 | } |
1650 | ext3_debug("found bit %d\n", grp_alloc_blk); | 1611 | ext3_debug("found bit %d\n", grp_alloc_blk); |
1651 | spin_unlock(sb_bgl_lock(sbi, group_no)); | 1612 | spin_unlock(sb_bgl_lock(sbi, group_no)); |
1652 | jbd_unlock_bh_state(bitmap_bh); | 1613 | jbd_unlock_bh_state(bitmap_bh); |
1653 | #endif | 1614 | #endif |
1654 | 1615 | ||
1655 | if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) { | 1616 | if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) { |
1656 | ext3_error(sb, "ext3_new_block", | 1617 | ext3_error(sb, "ext3_new_block", |
1657 | "block("E3FSBLK") >= blocks count(%d) - " | 1618 | "block("E3FSBLK") >= blocks count(%d) - " |
1658 | "block_group = %d, es == %p ", ret_block, | 1619 | "block_group = %d, es == %p ", ret_block, |
1659 | le32_to_cpu(es->s_blocks_count), group_no, es); | 1620 | le32_to_cpu(es->s_blocks_count), group_no, es); |
1660 | goto out; | 1621 | goto out; |
1661 | } | 1622 | } |
1662 | 1623 | ||
1663 | /* | 1624 | /* |
1664 | * It is up to the caller to add the new buffer to a journal | 1625 | * It is up to the caller to add the new buffer to a journal |
1665 | * list of some description. We don't know in advance whether | 1626 | * list of some description. We don't know in advance whether |
1666 | * the caller wants to use it as metadata or data. | 1627 | * the caller wants to use it as metadata or data. |
1667 | */ | 1628 | */ |
1668 | ext3_debug("allocating block %lu. Goal hits %d of %d.\n", | 1629 | ext3_debug("allocating block %lu. Goal hits %d of %d.\n", |
1669 | ret_block, goal_hits, goal_attempts); | 1630 | ret_block, goal_hits, goal_attempts); |
1670 | 1631 | ||
1671 | spin_lock(sb_bgl_lock(sbi, group_no)); | 1632 | spin_lock(sb_bgl_lock(sbi, group_no)); |
1672 | gdp->bg_free_blocks_count = | 1633 | gdp->bg_free_blocks_count = |
1673 | cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)-num); | 1634 | cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)-num); |
1674 | spin_unlock(sb_bgl_lock(sbi, group_no)); | 1635 | spin_unlock(sb_bgl_lock(sbi, group_no)); |
1675 | percpu_counter_sub(&sbi->s_freeblocks_counter, num); | 1636 | percpu_counter_sub(&sbi->s_freeblocks_counter, num); |
1676 | 1637 | ||
1677 | BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor"); | 1638 | BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor"); |
1678 | err = ext3_journal_dirty_metadata(handle, gdp_bh); | 1639 | err = ext3_journal_dirty_metadata(handle, gdp_bh); |
1679 | if (!fatal) | 1640 | if (!fatal) |
1680 | fatal = err; | 1641 | fatal = err; |
1681 | 1642 | ||
1682 | sb->s_dirt = 1; | 1643 | sb->s_dirt = 1; |
1683 | if (fatal) | 1644 | if (fatal) |
1684 | goto out; | 1645 | goto out; |
1685 | 1646 | ||
1686 | *errp = 0; | 1647 | *errp = 0; |
1687 | brelse(bitmap_bh); | 1648 | brelse(bitmap_bh); |
1688 | DQUOT_FREE_BLOCK(inode, *count-num); | 1649 | DQUOT_FREE_BLOCK(inode, *count-num); |
1689 | *count = num; | 1650 | *count = num; |
1690 | return ret_block; | 1651 | return ret_block; |
1691 | 1652 | ||
1692 | io_error: | 1653 | io_error: |
1693 | *errp = -EIO; | 1654 | *errp = -EIO; |
1694 | out: | 1655 | out: |
1695 | if (fatal) { | 1656 | if (fatal) { |
1696 | *errp = fatal; | 1657 | *errp = fatal; |
1697 | ext3_std_error(sb, fatal); | 1658 | ext3_std_error(sb, fatal); |
1698 | } | 1659 | } |
1699 | /* | 1660 | /* |
1700 | * Undo the block allocation | 1661 | * Undo the block allocation |
1701 | */ | 1662 | */ |
1702 | if (!performed_allocation) | 1663 | if (!performed_allocation) |
1703 | DQUOT_FREE_BLOCK(inode, *count); | 1664 | DQUOT_FREE_BLOCK(inode, *count); |
1704 | brelse(bitmap_bh); | 1665 | brelse(bitmap_bh); |
1705 | return 0; | 1666 | return 0; |
1706 | } | 1667 | } |
1707 | 1668 | ||
1708 | ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode, | 1669 | ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode, |
1709 | ext3_fsblk_t goal, int *errp) | 1670 | ext3_fsblk_t goal, int *errp) |
1710 | { | 1671 | { |
1711 | unsigned long count = 1; | 1672 | unsigned long count = 1; |
1712 | 1673 | ||
1713 | return ext3_new_blocks(handle, inode, goal, &count, errp); | 1674 | return ext3_new_blocks(handle, inode, goal, &count, errp); |
1714 | } | 1675 | } |
1715 | 1676 | ||
1716 | /** | 1677 | /** |
1717 | * ext3_count_free_blocks() -- count filesystem free blocks | 1678 | * ext3_count_free_blocks() -- count filesystem free blocks |
1718 | * @sb: superblock | 1679 | * @sb: superblock |
1719 | * | 1680 | * |
1720 | * Adds up the number of free blocks from each block group. | 1681 | * Adds up the number of free blocks from each block group. |
1721 | */ | 1682 | */ |
1722 | ext3_fsblk_t ext3_count_free_blocks(struct super_block *sb) | 1683 | ext3_fsblk_t ext3_count_free_blocks(struct super_block *sb) |
1723 | { | 1684 | { |
1724 | ext3_fsblk_t desc_count; | 1685 | ext3_fsblk_t desc_count; |
1725 | struct ext3_group_desc *gdp; | 1686 | struct ext3_group_desc *gdp; |
1726 | int i; | 1687 | int i; |
1727 | unsigned long ngroups = EXT3_SB(sb)->s_groups_count; | 1688 | unsigned long ngroups = EXT3_SB(sb)->s_groups_count; |
1728 | #ifdef EXT3FS_DEBUG | 1689 | #ifdef EXT3FS_DEBUG |
1729 | struct ext3_super_block *es; | 1690 | struct ext3_super_block *es; |
1730 | ext3_fsblk_t bitmap_count; | 1691 | ext3_fsblk_t bitmap_count; |
1731 | unsigned long x; | 1692 | unsigned long x; |
1732 | struct buffer_head *bitmap_bh = NULL; | 1693 | struct buffer_head *bitmap_bh = NULL; |
1733 | 1694 | ||
1734 | es = EXT3_SB(sb)->s_es; | 1695 | es = EXT3_SB(sb)->s_es; |
1735 | desc_count = 0; | 1696 | desc_count = 0; |
1736 | bitmap_count = 0; | 1697 | bitmap_count = 0; |
1737 | gdp = NULL; | 1698 | gdp = NULL; |
1738 | 1699 | ||
1739 | smp_rmb(); | 1700 | smp_rmb(); |
1740 | for (i = 0; i < ngroups; i++) { | 1701 | for (i = 0; i < ngroups; i++) { |
1741 | gdp = ext3_get_group_desc(sb, i, NULL); | 1702 | gdp = ext3_get_group_desc(sb, i, NULL); |
1742 | if (!gdp) | 1703 | if (!gdp) |
1743 | continue; | 1704 | continue; |
1744 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); | 1705 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); |
1745 | brelse(bitmap_bh); | 1706 | brelse(bitmap_bh); |
1746 | bitmap_bh = read_block_bitmap(sb, i); | 1707 | bitmap_bh = read_block_bitmap(sb, i); |
1747 | if (bitmap_bh == NULL) | 1708 | if (bitmap_bh == NULL) |
1748 | continue; | 1709 | continue; |
1749 | 1710 | ||
1750 | x = ext3_count_free(bitmap_bh, sb->s_blocksize); | 1711 | x = ext3_count_free(bitmap_bh, sb->s_blocksize); |
1751 | printk("group %d: stored = %d, counted = %lu\n", | 1712 | printk("group %d: stored = %d, counted = %lu\n", |
1752 | i, le16_to_cpu(gdp->bg_free_blocks_count), x); | 1713 | i, le16_to_cpu(gdp->bg_free_blocks_count), x); |
1753 | bitmap_count += x; | 1714 | bitmap_count += x; |
1754 | } | 1715 | } |
1755 | brelse(bitmap_bh); | 1716 | brelse(bitmap_bh); |
1756 | printk("ext3_count_free_blocks: stored = "E3FSBLK | 1717 | printk("ext3_count_free_blocks: stored = "E3FSBLK |
1757 | ", computed = "E3FSBLK", "E3FSBLK"\n", | 1718 | ", computed = "E3FSBLK", "E3FSBLK"\n", |
1758 | le32_to_cpu(es->s_free_blocks_count), | 1719 | le32_to_cpu(es->s_free_blocks_count), |
1759 | desc_count, bitmap_count); | 1720 | desc_count, bitmap_count); |
1760 | return bitmap_count; | 1721 | return bitmap_count; |
1761 | #else | 1722 | #else |
1762 | desc_count = 0; | 1723 | desc_count = 0; |
1763 | smp_rmb(); | 1724 | smp_rmb(); |
1764 | for (i = 0; i < ngroups; i++) { | 1725 | for (i = 0; i < ngroups; i++) { |
1765 | gdp = ext3_get_group_desc(sb, i, NULL); | 1726 | gdp = ext3_get_group_desc(sb, i, NULL); |
1766 | if (!gdp) | 1727 | if (!gdp) |
1767 | continue; | 1728 | continue; |
1768 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); | 1729 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); |
1769 | } | 1730 | } |
1770 | 1731 | ||
1771 | return desc_count; | 1732 | return desc_count; |
1772 | #endif | 1733 | #endif |
1773 | } | 1734 | } |
1774 | |||
1775 | 1735 | ||
1776 | static inline int test_root(int a, int b) | 1736 | static inline int test_root(int a, int b) |
1777 | { | 1737 | { |
1778 | int num = b; | 1738 | int num = b; |
1779 | 1739 | ||
1780 | while (a > num) | 1740 | while (a > num) |
1781 | num *= b; | 1741 | num *= b; |
1782 | return num == a; | 1742 | return num == a; |
1783 | } | 1743 | } |
1784 | 1744 | ||
1785 | static int ext3_group_sparse(int group) | 1745 | static int ext3_group_sparse(int group) |
1786 | { | 1746 | { |
1787 | if (group <= 1) | 1747 | if (group <= 1) |
1788 | return 1; | 1748 | return 1; |
1789 | if (!(group & 1)) | 1749 | if (!(group & 1)) |
1790 | return 0; | 1750 | return 0; |
1791 | return (test_root(group, 7) || test_root(group, 5) || | 1751 | return (test_root(group, 7) || test_root(group, 5) || |
1792 | test_root(group, 3)); | 1752 | test_root(group, 3)); |
1793 | } | 1753 | } |
1794 | 1754 | ||
1795 | /** | 1755 | /** |
1796 | * ext3_bg_has_super - number of blocks used by the superblock in group | 1756 | * ext3_bg_has_super - number of blocks used by the superblock in group |
1797 | * @sb: superblock for filesystem | 1757 | * @sb: superblock for filesystem |
1798 | * @group: group number to check | 1758 | * @group: group number to check |
1799 | * | 1759 | * |
1800 | * Return the number of blocks used by the superblock (primary or backup) | 1760 | * Return the number of blocks used by the superblock (primary or backup) |
1801 | * in this group. Currently this will be only 0 or 1. | 1761 | * in this group. Currently this will be only 0 or 1. |
1802 | */ | 1762 | */ |
1803 | int ext3_bg_has_super(struct super_block *sb, int group) | 1763 | int ext3_bg_has_super(struct super_block *sb, int group) |
1804 | { | 1764 | { |
1805 | if (EXT3_HAS_RO_COMPAT_FEATURE(sb, | 1765 | if (EXT3_HAS_RO_COMPAT_FEATURE(sb, |
1806 | EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) && | 1766 | EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) && |
1807 | !ext3_group_sparse(group)) | 1767 | !ext3_group_sparse(group)) |
1808 | return 0; | 1768 | return 0; |
1809 | return 1; | 1769 | return 1; |
1810 | } | 1770 | } |
1811 | 1771 | ||
1812 | static unsigned long ext3_bg_num_gdb_meta(struct super_block *sb, int group) | 1772 | static unsigned long ext3_bg_num_gdb_meta(struct super_block *sb, int group) |
1813 | { | 1773 | { |
1814 | unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb); | 1774 | unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb); |
1815 | unsigned long first = metagroup * EXT3_DESC_PER_BLOCK(sb); | 1775 | unsigned long first = metagroup * EXT3_DESC_PER_BLOCK(sb); |
1816 | unsigned long last = first + EXT3_DESC_PER_BLOCK(sb) - 1; | 1776 | unsigned long last = first + EXT3_DESC_PER_BLOCK(sb) - 1; |
1817 | 1777 | ||
1818 | if (group == first || group == first + 1 || group == last) | 1778 | if (group == first || group == first + 1 || group == last) |
1819 | return 1; | 1779 | return 1; |
1820 | return 0; | 1780 | return 0; |
1821 | } | 1781 | } |
1822 | 1782 | ||
1823 | static unsigned long ext3_bg_num_gdb_nometa(struct super_block *sb, int group) | 1783 | static unsigned long ext3_bg_num_gdb_nometa(struct super_block *sb, int group) |
1824 | { | 1784 | { |
1825 | if (EXT3_HAS_RO_COMPAT_FEATURE(sb, | 1785 | if (EXT3_HAS_RO_COMPAT_FEATURE(sb, |
1826 | EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) && | 1786 | EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) && |
1827 | !ext3_group_sparse(group)) | 1787 | !ext3_group_sparse(group)) |
1828 | return 0; | 1788 | return 0; |
1829 | return EXT3_SB(sb)->s_gdb_count; | 1789 | return EXT3_SB(sb)->s_gdb_count; |
1830 | } | 1790 | } |
1831 | 1791 | ||
1832 | /** | 1792 | /** |
1833 | * ext3_bg_num_gdb - number of blocks used by the group table in group | 1793 | * ext3_bg_num_gdb - number of blocks used by the group table in group |
1834 | * @sb: superblock for filesystem | 1794 | * @sb: superblock for filesystem |
1835 | * @group: group number to check | 1795 | * @group: group number to check |
1836 | * | 1796 | * |
1837 | * Return the number of blocks used by the group descriptor table | 1797 | * Return the number of blocks used by the group descriptor table |
1838 | * (primary or backup) in this group. In the future there may be a | 1798 | * (primary or backup) in this group. In the future there may be a |
1839 | * different number of descriptor blocks in each group. | 1799 | * different number of descriptor blocks in each group. |
1840 | */ | 1800 | */ |
1841 | unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) | 1801 | unsigned long ext3_bg_num_gdb(struct super_block *sb, int group) |
1842 | { | 1802 | { |
1843 | unsigned long first_meta_bg = | 1803 | unsigned long first_meta_bg = |
1844 | le32_to_cpu(EXT3_SB(sb)->s_es->s_first_meta_bg); | 1804 | le32_to_cpu(EXT3_SB(sb)->s_es->s_first_meta_bg); |
1845 | unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb); | 1805 | unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb); |
1846 | 1806 | ||
1847 | if (!EXT3_HAS_INCOMPAT_FEATURE(sb,EXT3_FEATURE_INCOMPAT_META_BG) || | 1807 | if (!EXT3_HAS_INCOMPAT_FEATURE(sb,EXT3_FEATURE_INCOMPAT_META_BG) || |
1848 | metagroup < first_meta_bg) | 1808 | metagroup < first_meta_bg) |
1849 | return ext3_bg_num_gdb_nometa(sb,group); | 1809 | return ext3_bg_num_gdb_nometa(sb,group); |
1850 | 1810 | ||
1851 | return ext3_bg_num_gdb_meta(sb,group); | 1811 | return ext3_bg_num_gdb_meta(sb,group); |
1852 | 1812 | ||
1853 | } | 1813 | } |
1854 | 1814 |
fs/ext4/balloc.c
1 | /* | 1 | /* |
2 | * linux/fs/ext4/balloc.c | 2 | * linux/fs/ext4/balloc.c |
3 | * | 3 | * |
4 | * Copyright (C) 1992, 1993, 1994, 1995 | 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
5 | * Remy Card (card@masi.ibp.fr) | 5 | * Remy Card (card@masi.ibp.fr) |
6 | * Laboratoire MASI - Institut Blaise Pascal | 6 | * Laboratoire MASI - Institut Blaise Pascal |
7 | * Universite Pierre et Marie Curie (Paris VI) | 7 | * Universite Pierre et Marie Curie (Paris VI) |
8 | * | 8 | * |
9 | * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993 | 9 | * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993 |
10 | * Big-endian to little-endian byte-swapping/bitmaps by | 10 | * Big-endian to little-endian byte-swapping/bitmaps by |
11 | * David S. Miller (davem@caip.rutgers.edu), 1995 | 11 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include <linux/time.h> | 14 | #include <linux/time.h> |
15 | #include <linux/capability.h> | 15 | #include <linux/capability.h> |
16 | #include <linux/fs.h> | 16 | #include <linux/fs.h> |
17 | #include <linux/jbd2.h> | 17 | #include <linux/jbd2.h> |
18 | #include <linux/ext4_fs.h> | 18 | #include <linux/ext4_fs.h> |
19 | #include <linux/ext4_jbd2.h> | 19 | #include <linux/ext4_jbd2.h> |
20 | #include <linux/quotaops.h> | 20 | #include <linux/quotaops.h> |
21 | #include <linux/buffer_head.h> | 21 | #include <linux/buffer_head.h> |
22 | 22 | ||
23 | #include "group.h" | 23 | #include "group.h" |
24 | /* | 24 | /* |
25 | * balloc.c contains the blocks allocation and deallocation routines | 25 | * balloc.c contains the blocks allocation and deallocation routines |
26 | */ | 26 | */ |
27 | 27 | ||
28 | /* | 28 | /* |
29 | * Calculate the block group number and offset, given a block number | 29 | * Calculate the block group number and offset, given a block number |
30 | */ | 30 | */ |
31 | void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr, | 31 | void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr, |
32 | unsigned long *blockgrpp, ext4_grpblk_t *offsetp) | 32 | unsigned long *blockgrpp, ext4_grpblk_t *offsetp) |
33 | { | 33 | { |
34 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; | 34 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
35 | ext4_grpblk_t offset; | 35 | ext4_grpblk_t offset; |
36 | 36 | ||
37 | blocknr = blocknr - le32_to_cpu(es->s_first_data_block); | 37 | blocknr = blocknr - le32_to_cpu(es->s_first_data_block); |
38 | offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)); | 38 | offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)); |
39 | if (offsetp) | 39 | if (offsetp) |
40 | *offsetp = offset; | 40 | *offsetp = offset; |
41 | if (blockgrpp) | 41 | if (blockgrpp) |
42 | *blockgrpp = blocknr; | 42 | *blockgrpp = blocknr; |
43 | 43 | ||
44 | } | 44 | } |
45 | 45 | ||
46 | /* Initializes an uninitialized block bitmap if given, and returns the | 46 | /* Initializes an uninitialized block bitmap if given, and returns the |
47 | * number of blocks free in the group. */ | 47 | * number of blocks free in the group. */ |
48 | unsigned ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh, | 48 | unsigned ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh, |
49 | int block_group, struct ext4_group_desc *gdp) | 49 | int block_group, struct ext4_group_desc *gdp) |
50 | { | 50 | { |
51 | unsigned long start; | 51 | unsigned long start; |
52 | int bit, bit_max; | 52 | int bit, bit_max; |
53 | unsigned free_blocks, group_blocks; | 53 | unsigned free_blocks, group_blocks; |
54 | struct ext4_sb_info *sbi = EXT4_SB(sb); | 54 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
55 | 55 | ||
56 | if (bh) { | 56 | if (bh) { |
57 | J_ASSERT_BH(bh, buffer_locked(bh)); | 57 | J_ASSERT_BH(bh, buffer_locked(bh)); |
58 | 58 | ||
59 | /* If checksum is bad mark all blocks used to prevent allocation | 59 | /* If checksum is bad mark all blocks used to prevent allocation |
60 | * essentially implementing a per-group read-only flag. */ | 60 | * essentially implementing a per-group read-only flag. */ |
61 | if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { | 61 | if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { |
62 | ext4_error(sb, __FUNCTION__, | 62 | ext4_error(sb, __FUNCTION__, |
63 | "Checksum bad for group %u\n", block_group); | 63 | "Checksum bad for group %u\n", block_group); |
64 | gdp->bg_free_blocks_count = 0; | 64 | gdp->bg_free_blocks_count = 0; |
65 | gdp->bg_free_inodes_count = 0; | 65 | gdp->bg_free_inodes_count = 0; |
66 | gdp->bg_itable_unused = 0; | 66 | gdp->bg_itable_unused = 0; |
67 | memset(bh->b_data, 0xff, sb->s_blocksize); | 67 | memset(bh->b_data, 0xff, sb->s_blocksize); |
68 | return 0; | 68 | return 0; |
69 | } | 69 | } |
70 | memset(bh->b_data, 0, sb->s_blocksize); | 70 | memset(bh->b_data, 0, sb->s_blocksize); |
71 | } | 71 | } |
72 | 72 | ||
73 | /* Check for superblock and gdt backups in this group */ | 73 | /* Check for superblock and gdt backups in this group */ |
74 | bit_max = ext4_bg_has_super(sb, block_group); | 74 | bit_max = ext4_bg_has_super(sb, block_group); |
75 | 75 | ||
76 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) || | 76 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) || |
77 | block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) * | 77 | block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) * |
78 | sbi->s_desc_per_block) { | 78 | sbi->s_desc_per_block) { |
79 | if (bit_max) { | 79 | if (bit_max) { |
80 | bit_max += ext4_bg_num_gdb(sb, block_group); | 80 | bit_max += ext4_bg_num_gdb(sb, block_group); |
81 | bit_max += | 81 | bit_max += |
82 | le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks); | 82 | le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks); |
83 | } | 83 | } |
84 | } else { /* For META_BG_BLOCK_GROUPS */ | 84 | } else { /* For META_BG_BLOCK_GROUPS */ |
85 | int group_rel = (block_group - | 85 | int group_rel = (block_group - |
86 | le32_to_cpu(sbi->s_es->s_first_meta_bg)) % | 86 | le32_to_cpu(sbi->s_es->s_first_meta_bg)) % |
87 | EXT4_DESC_PER_BLOCK(sb); | 87 | EXT4_DESC_PER_BLOCK(sb); |
88 | if (group_rel == 0 || group_rel == 1 || | 88 | if (group_rel == 0 || group_rel == 1 || |
89 | (group_rel == EXT4_DESC_PER_BLOCK(sb) - 1)) | 89 | (group_rel == EXT4_DESC_PER_BLOCK(sb) - 1)) |
90 | bit_max += 1; | 90 | bit_max += 1; |
91 | } | 91 | } |
92 | 92 | ||
93 | if (block_group == sbi->s_groups_count - 1) { | 93 | if (block_group == sbi->s_groups_count - 1) { |
94 | /* | 94 | /* |
95 | * Even though mke2fs always initialize first and last group | 95 | * Even though mke2fs always initialize first and last group |
96 | * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need | 96 | * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need |
97 | * to make sure we calculate the right free blocks | 97 | * to make sure we calculate the right free blocks |
98 | */ | 98 | */ |
99 | group_blocks = ext4_blocks_count(sbi->s_es) - | 99 | group_blocks = ext4_blocks_count(sbi->s_es) - |
100 | le32_to_cpu(sbi->s_es->s_first_data_block) - | 100 | le32_to_cpu(sbi->s_es->s_first_data_block) - |
101 | (EXT4_BLOCKS_PER_GROUP(sb) * (sbi->s_groups_count -1)); | 101 | (EXT4_BLOCKS_PER_GROUP(sb) * (sbi->s_groups_count -1)); |
102 | } else { | 102 | } else { |
103 | group_blocks = EXT4_BLOCKS_PER_GROUP(sb); | 103 | group_blocks = EXT4_BLOCKS_PER_GROUP(sb); |
104 | } | 104 | } |
105 | 105 | ||
106 | free_blocks = group_blocks - bit_max; | 106 | free_blocks = group_blocks - bit_max; |
107 | 107 | ||
108 | if (bh) { | 108 | if (bh) { |
109 | for (bit = 0; bit < bit_max; bit++) | 109 | for (bit = 0; bit < bit_max; bit++) |
110 | ext4_set_bit(bit, bh->b_data); | 110 | ext4_set_bit(bit, bh->b_data); |
111 | 111 | ||
112 | start = block_group * EXT4_BLOCKS_PER_GROUP(sb) + | 112 | start = block_group * EXT4_BLOCKS_PER_GROUP(sb) + |
113 | le32_to_cpu(sbi->s_es->s_first_data_block); | 113 | le32_to_cpu(sbi->s_es->s_first_data_block); |
114 | 114 | ||
115 | /* Set bits for block and inode bitmaps, and inode table */ | 115 | /* Set bits for block and inode bitmaps, and inode table */ |
116 | ext4_set_bit(ext4_block_bitmap(sb, gdp) - start, bh->b_data); | 116 | ext4_set_bit(ext4_block_bitmap(sb, gdp) - start, bh->b_data); |
117 | ext4_set_bit(ext4_inode_bitmap(sb, gdp) - start, bh->b_data); | 117 | ext4_set_bit(ext4_inode_bitmap(sb, gdp) - start, bh->b_data); |
118 | for (bit = (ext4_inode_table(sb, gdp) - start), | 118 | for (bit = (ext4_inode_table(sb, gdp) - start), |
119 | bit_max = bit + sbi->s_itb_per_group; bit < bit_max; bit++) | 119 | bit_max = bit + sbi->s_itb_per_group; bit < bit_max; bit++) |
120 | ext4_set_bit(bit, bh->b_data); | 120 | ext4_set_bit(bit, bh->b_data); |
121 | 121 | ||
122 | /* | 122 | /* |
123 | * Also if the number of blocks within the group is | 123 | * Also if the number of blocks within the group is |
124 | * less than the blocksize * 8 ( which is the size | 124 | * less than the blocksize * 8 ( which is the size |
125 | * of bitmap ), set rest of the block bitmap to 1 | 125 | * of bitmap ), set rest of the block bitmap to 1 |
126 | */ | 126 | */ |
127 | mark_bitmap_end(group_blocks, sb->s_blocksize * 8, bh->b_data); | 127 | mark_bitmap_end(group_blocks, sb->s_blocksize * 8, bh->b_data); |
128 | } | 128 | } |
129 | 129 | ||
130 | return free_blocks - sbi->s_itb_per_group - 2; | 130 | return free_blocks - sbi->s_itb_per_group - 2; |
131 | } | 131 | } |
132 | 132 | ||
133 | 133 | ||
134 | /* | 134 | /* |
135 | * The free blocks are managed by bitmaps. A file system contains several | 135 | * The free blocks are managed by bitmaps. A file system contains several |
136 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap | 136 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap |
137 | * block for inodes, N blocks for the inode table and data blocks. | 137 | * block for inodes, N blocks for the inode table and data blocks. |
138 | * | 138 | * |
139 | * The file system contains group descriptors which are located after the | 139 | * The file system contains group descriptors which are located after the |
140 | * super block. Each descriptor contains the number of the bitmap block and | 140 | * super block. Each descriptor contains the number of the bitmap block and |
141 | * the free blocks count in the block. The descriptors are loaded in memory | 141 | * the free blocks count in the block. The descriptors are loaded in memory |
142 | * when a file system is mounted (see ext4_fill_super). | 142 | * when a file system is mounted (see ext4_fill_super). |
143 | */ | 143 | */ |
144 | 144 | ||
145 | 145 | ||
146 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) | 146 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) |
147 | 147 | ||
148 | /** | 148 | /** |
149 | * ext4_get_group_desc() -- load group descriptor from disk | 149 | * ext4_get_group_desc() -- load group descriptor from disk |
150 | * @sb: super block | 150 | * @sb: super block |
151 | * @block_group: given block group | 151 | * @block_group: given block group |
152 | * @bh: pointer to the buffer head to store the block | 152 | * @bh: pointer to the buffer head to store the block |
153 | * group descriptor | 153 | * group descriptor |
154 | */ | 154 | */ |
155 | struct ext4_group_desc * ext4_get_group_desc(struct super_block * sb, | 155 | struct ext4_group_desc * ext4_get_group_desc(struct super_block * sb, |
156 | unsigned int block_group, | 156 | unsigned int block_group, |
157 | struct buffer_head ** bh) | 157 | struct buffer_head ** bh) |
158 | { | 158 | { |
159 | unsigned long group_desc; | 159 | unsigned long group_desc; |
160 | unsigned long offset; | 160 | unsigned long offset; |
161 | struct ext4_group_desc * desc; | 161 | struct ext4_group_desc * desc; |
162 | struct ext4_sb_info *sbi = EXT4_SB(sb); | 162 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
163 | 163 | ||
164 | if (block_group >= sbi->s_groups_count) { | 164 | if (block_group >= sbi->s_groups_count) { |
165 | ext4_error (sb, "ext4_get_group_desc", | 165 | ext4_error (sb, "ext4_get_group_desc", |
166 | "block_group >= groups_count - " | 166 | "block_group >= groups_count - " |
167 | "block_group = %d, groups_count = %lu", | 167 | "block_group = %d, groups_count = %lu", |
168 | block_group, sbi->s_groups_count); | 168 | block_group, sbi->s_groups_count); |
169 | 169 | ||
170 | return NULL; | 170 | return NULL; |
171 | } | 171 | } |
172 | smp_rmb(); | 172 | smp_rmb(); |
173 | 173 | ||
174 | group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb); | 174 | group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb); |
175 | offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1); | 175 | offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1); |
176 | if (!sbi->s_group_desc[group_desc]) { | 176 | if (!sbi->s_group_desc[group_desc]) { |
177 | ext4_error (sb, "ext4_get_group_desc", | 177 | ext4_error (sb, "ext4_get_group_desc", |
178 | "Group descriptor not loaded - " | 178 | "Group descriptor not loaded - " |
179 | "block_group = %d, group_desc = %lu, desc = %lu", | 179 | "block_group = %d, group_desc = %lu, desc = %lu", |
180 | block_group, group_desc, offset); | 180 | block_group, group_desc, offset); |
181 | return NULL; | 181 | return NULL; |
182 | } | 182 | } |
183 | 183 | ||
184 | desc = (struct ext4_group_desc *)( | 184 | desc = (struct ext4_group_desc *)( |
185 | (__u8 *)sbi->s_group_desc[group_desc]->b_data + | 185 | (__u8 *)sbi->s_group_desc[group_desc]->b_data + |
186 | offset * EXT4_DESC_SIZE(sb)); | 186 | offset * EXT4_DESC_SIZE(sb)); |
187 | if (bh) | 187 | if (bh) |
188 | *bh = sbi->s_group_desc[group_desc]; | 188 | *bh = sbi->s_group_desc[group_desc]; |
189 | return desc; | 189 | return desc; |
190 | } | 190 | } |
191 | 191 | ||
192 | static inline int | ||
193 | block_in_use(ext4_fsblk_t block, struct super_block *sb, unsigned char *map) | ||
194 | { | ||
195 | ext4_grpblk_t offset; | ||
196 | |||
197 | ext4_get_group_no_and_offset(sb, block, NULL, &offset); | ||
198 | return ext4_test_bit (offset, map); | ||
199 | } | ||
200 | |||
201 | /** | 192 | /** |
202 | * read_block_bitmap() | 193 | * read_block_bitmap() |
203 | * @sb: super block | 194 | * @sb: super block |
204 | * @block_group: given block group | 195 | * @block_group: given block group |
205 | * | 196 | * |
206 | * Read the bitmap for a given block_group, reading into the specified | 197 | * Read the bitmap for a given block_group, reading into the specified |
207 | * slot in the superblock's bitmap cache. | 198 | * slot in the superblock's bitmap cache. |
208 | * | 199 | * |
209 | * Return buffer_head on success or NULL in case of failure. | 200 | * Return buffer_head on success or NULL in case of failure. |
210 | */ | 201 | */ |
211 | struct buffer_head * | 202 | struct buffer_head * |
212 | read_block_bitmap(struct super_block *sb, unsigned int block_group) | 203 | read_block_bitmap(struct super_block *sb, unsigned int block_group) |
213 | { | 204 | { |
214 | int i; | ||
215 | struct ext4_group_desc * desc; | 205 | struct ext4_group_desc * desc; |
216 | struct buffer_head * bh = NULL; | 206 | struct buffer_head * bh = NULL; |
217 | ext4_fsblk_t bitmap_blk; | 207 | ext4_fsblk_t bitmap_blk; |
218 | 208 | ||
219 | desc = ext4_get_group_desc(sb, block_group, NULL); | 209 | desc = ext4_get_group_desc(sb, block_group, NULL); |
220 | if (!desc) | 210 | if (!desc) |
221 | return NULL; | 211 | return NULL; |
222 | bitmap_blk = ext4_block_bitmap(sb, desc); | 212 | bitmap_blk = ext4_block_bitmap(sb, desc); |
223 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { | 213 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
224 | bh = sb_getblk(sb, bitmap_blk); | 214 | bh = sb_getblk(sb, bitmap_blk); |
225 | if (!buffer_uptodate(bh)) { | 215 | if (!buffer_uptodate(bh)) { |
226 | lock_buffer(bh); | 216 | lock_buffer(bh); |
227 | if (!buffer_uptodate(bh)) { | 217 | if (!buffer_uptodate(bh)) { |
228 | ext4_init_block_bitmap(sb, bh, block_group, | 218 | ext4_init_block_bitmap(sb, bh, block_group, |
229 | desc); | 219 | desc); |
230 | set_buffer_uptodate(bh); | 220 | set_buffer_uptodate(bh); |
231 | } | 221 | } |
232 | unlock_buffer(bh); | 222 | unlock_buffer(bh); |
233 | } | 223 | } |
234 | } else { | 224 | } else { |
235 | bh = sb_bread(sb, bitmap_blk); | 225 | bh = sb_bread(sb, bitmap_blk); |
236 | } | 226 | } |
237 | if (!bh) | 227 | if (!bh) |
238 | ext4_error (sb, __FUNCTION__, | 228 | ext4_error (sb, __FUNCTION__, |
239 | "Cannot read block bitmap - " | 229 | "Cannot read block bitmap - " |
240 | "block_group = %d, block_bitmap = %llu", | 230 | "block_group = %d, block_bitmap = %llu", |
241 | block_group, bitmap_blk); | 231 | block_group, bitmap_blk); |
242 | |||
243 | /* check whether block bitmap block number is set */ | ||
244 | if (!block_in_use(bitmap_blk, sb, bh->b_data)) { | ||
245 | /* bad block bitmap */ | ||
246 | goto error_out; | ||
247 | } | ||
248 | |||
249 | /* check whether the inode bitmap block number is set */ | ||
250 | bitmap_blk = ext4_inode_bitmap(sb, desc); | ||
251 | if (!block_in_use(bitmap_blk, sb, bh->b_data)) { | ||
252 | /* bad block bitmap */ | ||
253 | goto error_out; | ||
254 | } | ||
255 | /* check whether the inode table block number is set */ | ||
256 | bitmap_blk = ext4_inode_table(sb, desc); | ||
257 | for (i = 0; i < EXT4_SB(sb)->s_itb_per_group; i++, bitmap_blk++) { | ||
258 | if (!block_in_use(bitmap_blk, sb, bh->b_data)) { | ||
259 | /* bad block bitmap */ | ||
260 | goto error_out; | ||
261 | } | ||
262 | } | ||
263 | |||
264 | return bh; | 232 | return bh; |
265 | |||
266 | error_out: | ||
267 | brelse(bh); | ||
268 | ext4_error(sb, __FUNCTION__, | ||
269 | "Invalid block bitmap - " | ||
270 | "block_group = %d, block = %llu", | ||
271 | block_group, bitmap_blk); | ||
272 | return NULL; | ||
273 | |||
274 | } | 233 | } |
275 | /* | 234 | /* |
276 | * The reservation window structure operations | 235 | * The reservation window structure operations |
277 | * -------------------------------------------- | 236 | * -------------------------------------------- |
278 | * Operations include: | 237 | * Operations include: |
279 | * dump, find, add, remove, is_empty, find_next_reservable_window, etc. | 238 | * dump, find, add, remove, is_empty, find_next_reservable_window, etc. |
280 | * | 239 | * |
281 | * We use a red-black tree to represent per-filesystem reservation | 240 | * We use a red-black tree to represent per-filesystem reservation |
282 | * windows. | 241 | * windows. |
283 | * | 242 | * |
284 | */ | 243 | */ |
285 | 244 | ||
286 | /** | 245 | /** |
287 | * __rsv_window_dump() -- Dump the filesystem block allocation reservation map | 246 | * __rsv_window_dump() -- Dump the filesystem block allocation reservation map |
288 | * @rb_root: root of per-filesystem reservation rb tree | 247 | * @rb_root: root of per-filesystem reservation rb tree |
289 | * @verbose: verbose mode | 248 | * @verbose: verbose mode |
290 | * @fn: function which wishes to dump the reservation map | 249 | * @fn: function which wishes to dump the reservation map |
291 | * | 250 | * |
292 | * If verbose is turned on, it will print the whole block reservation | 251 | * If verbose is turned on, it will print the whole block reservation |
293 | * windows(start, end). Otherwise, it will only print out the "bad" windows, | 252 | * windows(start, end). Otherwise, it will only print out the "bad" windows, |
294 | * those windows that overlap with their immediate neighbors. | 253 | * those windows that overlap with their immediate neighbors. |
295 | */ | 254 | */ |
296 | #if 1 | 255 | #if 1 |
297 | static void __rsv_window_dump(struct rb_root *root, int verbose, | 256 | static void __rsv_window_dump(struct rb_root *root, int verbose, |
298 | const char *fn) | 257 | const char *fn) |
299 | { | 258 | { |
300 | struct rb_node *n; | 259 | struct rb_node *n; |
301 | struct ext4_reserve_window_node *rsv, *prev; | 260 | struct ext4_reserve_window_node *rsv, *prev; |
302 | int bad; | 261 | int bad; |
303 | 262 | ||
304 | restart: | 263 | restart: |
305 | n = rb_first(root); | 264 | n = rb_first(root); |
306 | bad = 0; | 265 | bad = 0; |
307 | prev = NULL; | 266 | prev = NULL; |
308 | 267 | ||
309 | printk("Block Allocation Reservation Windows Map (%s):\n", fn); | 268 | printk("Block Allocation Reservation Windows Map (%s):\n", fn); |
310 | while (n) { | 269 | while (n) { |
311 | rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node); | 270 | rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node); |
312 | if (verbose) | 271 | if (verbose) |
313 | printk("reservation window 0x%p " | 272 | printk("reservation window 0x%p " |
314 | "start: %llu, end: %llu\n", | 273 | "start: %llu, end: %llu\n", |
315 | rsv, rsv->rsv_start, rsv->rsv_end); | 274 | rsv, rsv->rsv_start, rsv->rsv_end); |
316 | if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) { | 275 | if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) { |
317 | printk("Bad reservation %p (start >= end)\n", | 276 | printk("Bad reservation %p (start >= end)\n", |
318 | rsv); | 277 | rsv); |
319 | bad = 1; | 278 | bad = 1; |
320 | } | 279 | } |
321 | if (prev && prev->rsv_end >= rsv->rsv_start) { | 280 | if (prev && prev->rsv_end >= rsv->rsv_start) { |
322 | printk("Bad reservation %p (prev->end >= start)\n", | 281 | printk("Bad reservation %p (prev->end >= start)\n", |
323 | rsv); | 282 | rsv); |
324 | bad = 1; | 283 | bad = 1; |
325 | } | 284 | } |
326 | if (bad) { | 285 | if (bad) { |
327 | if (!verbose) { | 286 | if (!verbose) { |
328 | printk("Restarting reservation walk in verbose mode\n"); | 287 | printk("Restarting reservation walk in verbose mode\n"); |
329 | verbose = 1; | 288 | verbose = 1; |
330 | goto restart; | 289 | goto restart; |
331 | } | 290 | } |
332 | } | 291 | } |
333 | n = rb_next(n); | 292 | n = rb_next(n); |
334 | prev = rsv; | 293 | prev = rsv; |
335 | } | 294 | } |
336 | printk("Window map complete.\n"); | 295 | printk("Window map complete.\n"); |
337 | if (bad) | 296 | if (bad) |
338 | BUG(); | 297 | BUG(); |
339 | } | 298 | } |
340 | #define rsv_window_dump(root, verbose) \ | 299 | #define rsv_window_dump(root, verbose) \ |
341 | __rsv_window_dump((root), (verbose), __FUNCTION__) | 300 | __rsv_window_dump((root), (verbose), __FUNCTION__) |
342 | #else | 301 | #else |
343 | #define rsv_window_dump(root, verbose) do {} while (0) | 302 | #define rsv_window_dump(root, verbose) do {} while (0) |
344 | #endif | 303 | #endif |
345 | 304 | ||
346 | /** | 305 | /** |
347 | * goal_in_my_reservation() | 306 | * goal_in_my_reservation() |
348 | * @rsv: inode's reservation window | 307 | * @rsv: inode's reservation window |
349 | * @grp_goal: given goal block relative to the allocation block group | 308 | * @grp_goal: given goal block relative to the allocation block group |
350 | * @group: the current allocation block group | 309 | * @group: the current allocation block group |
351 | * @sb: filesystem super block | 310 | * @sb: filesystem super block |
352 | * | 311 | * |
353 | * Test if the given goal block (group relative) is within the file's | 312 | * Test if the given goal block (group relative) is within the file's |
354 | * own block reservation window range. | 313 | * own block reservation window range. |
355 | * | 314 | * |
356 | * If the reservation window is outside the goal allocation group, return 0; | 315 | * If the reservation window is outside the goal allocation group, return 0; |
357 | * grp_goal (given goal block) could be -1, which means no specific | 316 | * grp_goal (given goal block) could be -1, which means no specific |
358 | * goal block. In this case, always return 1. | 317 | * goal block. In this case, always return 1. |
359 | * If the goal block is within the reservation window, return 1; | 318 | * If the goal block is within the reservation window, return 1; |
360 | * otherwise, return 0; | 319 | * otherwise, return 0; |
361 | */ | 320 | */ |
362 | static int | 321 | static int |
363 | goal_in_my_reservation(struct ext4_reserve_window *rsv, ext4_grpblk_t grp_goal, | 322 | goal_in_my_reservation(struct ext4_reserve_window *rsv, ext4_grpblk_t grp_goal, |
364 | unsigned int group, struct super_block * sb) | 323 | unsigned int group, struct super_block * sb) |
365 | { | 324 | { |
366 | ext4_fsblk_t group_first_block, group_last_block; | 325 | ext4_fsblk_t group_first_block, group_last_block; |
367 | 326 | ||
368 | group_first_block = ext4_group_first_block_no(sb, group); | 327 | group_first_block = ext4_group_first_block_no(sb, group); |
369 | group_last_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1); | 328 | group_last_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1); |
370 | 329 | ||
371 | if ((rsv->_rsv_start > group_last_block) || | 330 | if ((rsv->_rsv_start > group_last_block) || |
372 | (rsv->_rsv_end < group_first_block)) | 331 | (rsv->_rsv_end < group_first_block)) |
373 | return 0; | 332 | return 0; |
374 | if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start) | 333 | if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start) |
375 | || (grp_goal + group_first_block > rsv->_rsv_end))) | 334 | || (grp_goal + group_first_block > rsv->_rsv_end))) |
376 | return 0; | 335 | return 0; |
377 | return 1; | 336 | return 1; |
378 | } | 337 | } |
379 | 338 | ||
380 | /** | 339 | /** |
381 | * search_reserve_window() | 340 | * search_reserve_window() |
382 | * @rb_root: root of reservation tree | 341 | * @rb_root: root of reservation tree |
383 | * @goal: target allocation block | 342 | * @goal: target allocation block |
384 | * | 343 | * |
385 | * Find the reserved window which includes the goal, or the previous one | 344 | * Find the reserved window which includes the goal, or the previous one |
386 | * if the goal is not in any window. | 345 | * if the goal is not in any window. |
387 | * Returns NULL if there are no windows or if all windows start after the goal. | 346 | * Returns NULL if there are no windows or if all windows start after the goal. |
388 | */ | 347 | */ |
389 | static struct ext4_reserve_window_node * | 348 | static struct ext4_reserve_window_node * |
390 | search_reserve_window(struct rb_root *root, ext4_fsblk_t goal) | 349 | search_reserve_window(struct rb_root *root, ext4_fsblk_t goal) |
391 | { | 350 | { |
392 | struct rb_node *n = root->rb_node; | 351 | struct rb_node *n = root->rb_node; |
393 | struct ext4_reserve_window_node *rsv; | 352 | struct ext4_reserve_window_node *rsv; |
394 | 353 | ||
395 | if (!n) | 354 | if (!n) |
396 | return NULL; | 355 | return NULL; |
397 | 356 | ||
398 | do { | 357 | do { |
399 | rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node); | 358 | rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node); |
400 | 359 | ||
401 | if (goal < rsv->rsv_start) | 360 | if (goal < rsv->rsv_start) |
402 | n = n->rb_left; | 361 | n = n->rb_left; |
403 | else if (goal > rsv->rsv_end) | 362 | else if (goal > rsv->rsv_end) |
404 | n = n->rb_right; | 363 | n = n->rb_right; |
405 | else | 364 | else |
406 | return rsv; | 365 | return rsv; |
407 | } while (n); | 366 | } while (n); |
408 | /* | 367 | /* |
409 | * We've fallen off the end of the tree: the goal wasn't inside | 368 | * We've fallen off the end of the tree: the goal wasn't inside |
410 | * any particular node. OK, the previous node must be to one | 369 | * any particular node. OK, the previous node must be to one |
411 | * side of the interval containing the goal. If it's the RHS, | 370 | * side of the interval containing the goal. If it's the RHS, |
412 | * we need to back up one. | 371 | * we need to back up one. |
413 | */ | 372 | */ |
414 | if (rsv->rsv_start > goal) { | 373 | if (rsv->rsv_start > goal) { |
415 | n = rb_prev(&rsv->rsv_node); | 374 | n = rb_prev(&rsv->rsv_node); |
416 | rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node); | 375 | rsv = rb_entry(n, struct ext4_reserve_window_node, rsv_node); |
417 | } | 376 | } |
418 | return rsv; | 377 | return rsv; |
419 | } | 378 | } |
420 | 379 | ||
421 | /** | 380 | /** |
422 | * ext4_rsv_window_add() -- Insert a window to the block reservation rb tree. | 381 | * ext4_rsv_window_add() -- Insert a window to the block reservation rb tree. |
423 | * @sb: super block | 382 | * @sb: super block |
424 | * @rsv: reservation window to add | 383 | * @rsv: reservation window to add |
425 | * | 384 | * |
426 | * Must be called with rsv_lock hold. | 385 | * Must be called with rsv_lock hold. |
427 | */ | 386 | */ |
428 | void ext4_rsv_window_add(struct super_block *sb, | 387 | void ext4_rsv_window_add(struct super_block *sb, |
429 | struct ext4_reserve_window_node *rsv) | 388 | struct ext4_reserve_window_node *rsv) |
430 | { | 389 | { |
431 | struct rb_root *root = &EXT4_SB(sb)->s_rsv_window_root; | 390 | struct rb_root *root = &EXT4_SB(sb)->s_rsv_window_root; |
432 | struct rb_node *node = &rsv->rsv_node; | 391 | struct rb_node *node = &rsv->rsv_node; |
433 | ext4_fsblk_t start = rsv->rsv_start; | 392 | ext4_fsblk_t start = rsv->rsv_start; |
434 | 393 | ||
435 | struct rb_node ** p = &root->rb_node; | 394 | struct rb_node ** p = &root->rb_node; |
436 | struct rb_node * parent = NULL; | 395 | struct rb_node * parent = NULL; |
437 | struct ext4_reserve_window_node *this; | 396 | struct ext4_reserve_window_node *this; |
438 | 397 | ||
439 | while (*p) | 398 | while (*p) |
440 | { | 399 | { |
441 | parent = *p; | 400 | parent = *p; |
442 | this = rb_entry(parent, struct ext4_reserve_window_node, rsv_node); | 401 | this = rb_entry(parent, struct ext4_reserve_window_node, rsv_node); |
443 | 402 | ||
444 | if (start < this->rsv_start) | 403 | if (start < this->rsv_start) |
445 | p = &(*p)->rb_left; | 404 | p = &(*p)->rb_left; |
446 | else if (start > this->rsv_end) | 405 | else if (start > this->rsv_end) |
447 | p = &(*p)->rb_right; | 406 | p = &(*p)->rb_right; |
448 | else { | 407 | else { |
449 | rsv_window_dump(root, 1); | 408 | rsv_window_dump(root, 1); |
450 | BUG(); | 409 | BUG(); |
451 | } | 410 | } |
452 | } | 411 | } |
453 | 412 | ||
454 | rb_link_node(node, parent, p); | 413 | rb_link_node(node, parent, p); |
455 | rb_insert_color(node, root); | 414 | rb_insert_color(node, root); |
456 | } | 415 | } |
457 | 416 | ||
458 | /** | 417 | /** |
459 | * ext4_rsv_window_remove() -- unlink a window from the reservation rb tree | 418 | * ext4_rsv_window_remove() -- unlink a window from the reservation rb tree |
460 | * @sb: super block | 419 | * @sb: super block |
461 | * @rsv: reservation window to remove | 420 | * @rsv: reservation window to remove |
462 | * | 421 | * |
463 | * Mark the block reservation window as not allocated, and unlink it | 422 | * Mark the block reservation window as not allocated, and unlink it |
464 | * from the filesystem reservation window rb tree. Must be called with | 423 | * from the filesystem reservation window rb tree. Must be called with |
465 | * rsv_lock hold. | 424 | * rsv_lock hold. |
466 | */ | 425 | */ |
467 | static void rsv_window_remove(struct super_block *sb, | 426 | static void rsv_window_remove(struct super_block *sb, |
468 | struct ext4_reserve_window_node *rsv) | 427 | struct ext4_reserve_window_node *rsv) |
469 | { | 428 | { |
470 | rsv->rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED; | 429 | rsv->rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED; |
471 | rsv->rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED; | 430 | rsv->rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED; |
472 | rsv->rsv_alloc_hit = 0; | 431 | rsv->rsv_alloc_hit = 0; |
473 | rb_erase(&rsv->rsv_node, &EXT4_SB(sb)->s_rsv_window_root); | 432 | rb_erase(&rsv->rsv_node, &EXT4_SB(sb)->s_rsv_window_root); |
474 | } | 433 | } |
475 | 434 | ||
476 | /* | 435 | /* |
477 | * rsv_is_empty() -- Check if the reservation window is allocated. | 436 | * rsv_is_empty() -- Check if the reservation window is allocated. |
478 | * @rsv: given reservation window to check | 437 | * @rsv: given reservation window to check |
479 | * | 438 | * |
480 | * returns 1 if the end block is EXT4_RESERVE_WINDOW_NOT_ALLOCATED. | 439 | * returns 1 if the end block is EXT4_RESERVE_WINDOW_NOT_ALLOCATED. |
481 | */ | 440 | */ |
482 | static inline int rsv_is_empty(struct ext4_reserve_window *rsv) | 441 | static inline int rsv_is_empty(struct ext4_reserve_window *rsv) |
483 | { | 442 | { |
484 | /* a valid reservation end block could not be 0 */ | 443 | /* a valid reservation end block could not be 0 */ |
485 | return rsv->_rsv_end == EXT4_RESERVE_WINDOW_NOT_ALLOCATED; | 444 | return rsv->_rsv_end == EXT4_RESERVE_WINDOW_NOT_ALLOCATED; |
486 | } | 445 | } |
487 | 446 | ||
488 | /** | 447 | /** |
489 | * ext4_init_block_alloc_info() | 448 | * ext4_init_block_alloc_info() |
490 | * @inode: file inode structure | 449 | * @inode: file inode structure |
491 | * | 450 | * |
492 | * Allocate and initialize the reservation window structure, and | 451 | * Allocate and initialize the reservation window structure, and |
493 | * link the window to the ext4 inode structure at last | 452 | * link the window to the ext4 inode structure at last |
494 | * | 453 | * |
495 | * The reservation window structure is only dynamically allocated | 454 | * The reservation window structure is only dynamically allocated |
496 | * and linked to ext4 inode the first time the open file | 455 | * and linked to ext4 inode the first time the open file |
497 | * needs a new block. So, before every ext4_new_block(s) call, for | 456 | * needs a new block. So, before every ext4_new_block(s) call, for |
498 | * regular files, we should check whether the reservation window | 457 | * regular files, we should check whether the reservation window |
499 | * structure exists or not. In the latter case, this function is called. | 458 | * structure exists or not. In the latter case, this function is called. |
500 | * Fail to do so will result in block reservation being turned off for that | 459 | * Fail to do so will result in block reservation being turned off for that |
501 | * open file. | 460 | * open file. |
502 | * | 461 | * |
503 | * This function is called from ext4_get_blocks_handle(), also called | 462 | * This function is called from ext4_get_blocks_handle(), also called |
504 | * when setting the reservation window size through ioctl before the file | 463 | * when setting the reservation window size through ioctl before the file |
505 | * is open for write (needs block allocation). | 464 | * is open for write (needs block allocation). |
506 | * | 465 | * |
507 | * Needs truncate_mutex protection prior to call this function. | 466 | * Needs truncate_mutex protection prior to call this function. |
508 | */ | 467 | */ |
509 | void ext4_init_block_alloc_info(struct inode *inode) | 468 | void ext4_init_block_alloc_info(struct inode *inode) |
510 | { | 469 | { |
511 | struct ext4_inode_info *ei = EXT4_I(inode); | 470 | struct ext4_inode_info *ei = EXT4_I(inode); |
512 | struct ext4_block_alloc_info *block_i = ei->i_block_alloc_info; | 471 | struct ext4_block_alloc_info *block_i = ei->i_block_alloc_info; |
513 | struct super_block *sb = inode->i_sb; | 472 | struct super_block *sb = inode->i_sb; |
514 | 473 | ||
515 | block_i = kmalloc(sizeof(*block_i), GFP_NOFS); | 474 | block_i = kmalloc(sizeof(*block_i), GFP_NOFS); |
516 | if (block_i) { | 475 | if (block_i) { |
517 | struct ext4_reserve_window_node *rsv = &block_i->rsv_window_node; | 476 | struct ext4_reserve_window_node *rsv = &block_i->rsv_window_node; |
518 | 477 | ||
519 | rsv->rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED; | 478 | rsv->rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED; |
520 | rsv->rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED; | 479 | rsv->rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED; |
521 | 480 | ||
522 | /* | 481 | /* |
523 | * if filesystem is mounted with NORESERVATION, the goal | 482 | * if filesystem is mounted with NORESERVATION, the goal |
524 | * reservation window size is set to zero to indicate | 483 | * reservation window size is set to zero to indicate |
525 | * block reservation is off | 484 | * block reservation is off |
526 | */ | 485 | */ |
527 | if (!test_opt(sb, RESERVATION)) | 486 | if (!test_opt(sb, RESERVATION)) |
528 | rsv->rsv_goal_size = 0; | 487 | rsv->rsv_goal_size = 0; |
529 | else | 488 | else |
530 | rsv->rsv_goal_size = EXT4_DEFAULT_RESERVE_BLOCKS; | 489 | rsv->rsv_goal_size = EXT4_DEFAULT_RESERVE_BLOCKS; |
531 | rsv->rsv_alloc_hit = 0; | 490 | rsv->rsv_alloc_hit = 0; |
532 | block_i->last_alloc_logical_block = 0; | 491 | block_i->last_alloc_logical_block = 0; |
533 | block_i->last_alloc_physical_block = 0; | 492 | block_i->last_alloc_physical_block = 0; |
534 | } | 493 | } |
535 | ei->i_block_alloc_info = block_i; | 494 | ei->i_block_alloc_info = block_i; |
536 | } | 495 | } |
537 | 496 | ||
538 | /** | 497 | /** |
539 | * ext4_discard_reservation() | 498 | * ext4_discard_reservation() |
540 | * @inode: inode | 499 | * @inode: inode |
541 | * | 500 | * |
542 | * Discard(free) block reservation window on last file close, or truncate | 501 | * Discard(free) block reservation window on last file close, or truncate |
543 | * or at last iput(). | 502 | * or at last iput(). |
544 | * | 503 | * |
545 | * It is being called in three cases: | 504 | * It is being called in three cases: |
546 | * ext4_release_file(): last writer close the file | 505 | * ext4_release_file(): last writer close the file |
547 | * ext4_clear_inode(): last iput(), when nobody link to this file. | 506 | * ext4_clear_inode(): last iput(), when nobody link to this file. |
548 | * ext4_truncate(): when the block indirect map is about to change. | 507 | * ext4_truncate(): when the block indirect map is about to change. |
549 | * | 508 | * |
550 | */ | 509 | */ |
551 | void ext4_discard_reservation(struct inode *inode) | 510 | void ext4_discard_reservation(struct inode *inode) |
552 | { | 511 | { |
553 | struct ext4_inode_info *ei = EXT4_I(inode); | 512 | struct ext4_inode_info *ei = EXT4_I(inode); |
554 | struct ext4_block_alloc_info *block_i = ei->i_block_alloc_info; | 513 | struct ext4_block_alloc_info *block_i = ei->i_block_alloc_info; |
555 | struct ext4_reserve_window_node *rsv; | 514 | struct ext4_reserve_window_node *rsv; |
556 | spinlock_t *rsv_lock = &EXT4_SB(inode->i_sb)->s_rsv_window_lock; | 515 | spinlock_t *rsv_lock = &EXT4_SB(inode->i_sb)->s_rsv_window_lock; |
557 | 516 | ||
558 | if (!block_i) | 517 | if (!block_i) |
559 | return; | 518 | return; |
560 | 519 | ||
561 | rsv = &block_i->rsv_window_node; | 520 | rsv = &block_i->rsv_window_node; |
562 | if (!rsv_is_empty(&rsv->rsv_window)) { | 521 | if (!rsv_is_empty(&rsv->rsv_window)) { |
563 | spin_lock(rsv_lock); | 522 | spin_lock(rsv_lock); |
564 | if (!rsv_is_empty(&rsv->rsv_window)) | 523 | if (!rsv_is_empty(&rsv->rsv_window)) |
565 | rsv_window_remove(inode->i_sb, rsv); | 524 | rsv_window_remove(inode->i_sb, rsv); |
566 | spin_unlock(rsv_lock); | 525 | spin_unlock(rsv_lock); |
567 | } | 526 | } |
568 | } | 527 | } |
569 | 528 | ||
570 | /** | 529 | /** |
571 | * ext4_free_blocks_sb() -- Free given blocks and update quota | 530 | * ext4_free_blocks_sb() -- Free given blocks and update quota |
572 | * @handle: handle to this transaction | 531 | * @handle: handle to this transaction |
573 | * @sb: super block | 532 | * @sb: super block |
574 | * @block: start physcial block to free | 533 | * @block: start physcial block to free |
575 | * @count: number of blocks to free | 534 | * @count: number of blocks to free |
576 | * @pdquot_freed_blocks: pointer to quota | 535 | * @pdquot_freed_blocks: pointer to quota |
577 | */ | 536 | */ |
578 | void ext4_free_blocks_sb(handle_t *handle, struct super_block *sb, | 537 | void ext4_free_blocks_sb(handle_t *handle, struct super_block *sb, |
579 | ext4_fsblk_t block, unsigned long count, | 538 | ext4_fsblk_t block, unsigned long count, |
580 | unsigned long *pdquot_freed_blocks) | 539 | unsigned long *pdquot_freed_blocks) |
581 | { | 540 | { |
582 | struct buffer_head *bitmap_bh = NULL; | 541 | struct buffer_head *bitmap_bh = NULL; |
583 | struct buffer_head *gd_bh; | 542 | struct buffer_head *gd_bh; |
584 | unsigned long block_group; | 543 | unsigned long block_group; |
585 | ext4_grpblk_t bit; | 544 | ext4_grpblk_t bit; |
586 | unsigned long i; | 545 | unsigned long i; |
587 | unsigned long overflow; | 546 | unsigned long overflow; |
588 | struct ext4_group_desc * desc; | 547 | struct ext4_group_desc * desc; |
589 | struct ext4_super_block * es; | 548 | struct ext4_super_block * es; |
590 | struct ext4_sb_info *sbi; | 549 | struct ext4_sb_info *sbi; |
591 | int err = 0, ret; | 550 | int err = 0, ret; |
592 | ext4_grpblk_t group_freed; | 551 | ext4_grpblk_t group_freed; |
593 | 552 | ||
594 | *pdquot_freed_blocks = 0; | 553 | *pdquot_freed_blocks = 0; |
595 | sbi = EXT4_SB(sb); | 554 | sbi = EXT4_SB(sb); |
596 | es = sbi->s_es; | 555 | es = sbi->s_es; |
597 | if (block < le32_to_cpu(es->s_first_data_block) || | 556 | if (block < le32_to_cpu(es->s_first_data_block) || |
598 | block + count < block || | 557 | block + count < block || |
599 | block + count > ext4_blocks_count(es)) { | 558 | block + count > ext4_blocks_count(es)) { |
600 | ext4_error (sb, "ext4_free_blocks", | 559 | ext4_error (sb, "ext4_free_blocks", |
601 | "Freeing blocks not in datazone - " | 560 | "Freeing blocks not in datazone - " |
602 | "block = %llu, count = %lu", block, count); | 561 | "block = %llu, count = %lu", block, count); |
603 | goto error_return; | 562 | goto error_return; |
604 | } | 563 | } |
605 | 564 | ||
606 | ext4_debug ("freeing block(s) %llu-%llu\n", block, block + count - 1); | 565 | ext4_debug ("freeing block(s) %llu-%llu\n", block, block + count - 1); |
607 | 566 | ||
608 | do_more: | 567 | do_more: |
609 | overflow = 0; | 568 | overflow = 0; |
610 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); | 569 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
611 | /* | 570 | /* |
612 | * Check to see if we are freeing blocks across a group | 571 | * Check to see if we are freeing blocks across a group |
613 | * boundary. | 572 | * boundary. |
614 | */ | 573 | */ |
615 | if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) { | 574 | if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) { |
616 | overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb); | 575 | overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb); |
617 | count -= overflow; | 576 | count -= overflow; |
618 | } | 577 | } |
619 | brelse(bitmap_bh); | 578 | brelse(bitmap_bh); |
620 | bitmap_bh = read_block_bitmap(sb, block_group); | 579 | bitmap_bh = read_block_bitmap(sb, block_group); |
621 | if (!bitmap_bh) | 580 | if (!bitmap_bh) |
622 | goto error_return; | 581 | goto error_return; |
623 | desc = ext4_get_group_desc (sb, block_group, &gd_bh); | 582 | desc = ext4_get_group_desc (sb, block_group, &gd_bh); |
624 | if (!desc) | 583 | if (!desc) |
625 | goto error_return; | 584 | goto error_return; |
626 | 585 | ||
627 | if (in_range(ext4_block_bitmap(sb, desc), block, count) || | 586 | if (in_range(ext4_block_bitmap(sb, desc), block, count) || |
628 | in_range(ext4_inode_bitmap(sb, desc), block, count) || | 587 | in_range(ext4_inode_bitmap(sb, desc), block, count) || |
629 | in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) || | 588 | in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) || |
630 | in_range(block + count - 1, ext4_inode_table(sb, desc), | 589 | in_range(block + count - 1, ext4_inode_table(sb, desc), |
631 | sbi->s_itb_per_group)) | 590 | sbi->s_itb_per_group)) |
632 | ext4_error (sb, "ext4_free_blocks", | 591 | ext4_error (sb, "ext4_free_blocks", |
633 | "Freeing blocks in system zones - " | 592 | "Freeing blocks in system zones - " |
634 | "Block = %llu, count = %lu", | 593 | "Block = %llu, count = %lu", |
635 | block, count); | 594 | block, count); |
636 | 595 | ||
637 | /* | 596 | /* |
638 | * We are about to start releasing blocks in the bitmap, | 597 | * We are about to start releasing blocks in the bitmap, |
639 | * so we need undo access. | 598 | * so we need undo access. |
640 | */ | 599 | */ |
641 | /* @@@ check errors */ | 600 | /* @@@ check errors */ |
642 | BUFFER_TRACE(bitmap_bh, "getting undo access"); | 601 | BUFFER_TRACE(bitmap_bh, "getting undo access"); |
643 | err = ext4_journal_get_undo_access(handle, bitmap_bh); | 602 | err = ext4_journal_get_undo_access(handle, bitmap_bh); |
644 | if (err) | 603 | if (err) |
645 | goto error_return; | 604 | goto error_return; |
646 | 605 | ||
647 | /* | 606 | /* |
648 | * We are about to modify some metadata. Call the journal APIs | 607 | * We are about to modify some metadata. Call the journal APIs |
649 | * to unshare ->b_data if a currently-committing transaction is | 608 | * to unshare ->b_data if a currently-committing transaction is |
650 | * using it | 609 | * using it |
651 | */ | 610 | */ |
652 | BUFFER_TRACE(gd_bh, "get_write_access"); | 611 | BUFFER_TRACE(gd_bh, "get_write_access"); |
653 | err = ext4_journal_get_write_access(handle, gd_bh); | 612 | err = ext4_journal_get_write_access(handle, gd_bh); |
654 | if (err) | 613 | if (err) |
655 | goto error_return; | 614 | goto error_return; |
656 | 615 | ||
657 | jbd_lock_bh_state(bitmap_bh); | 616 | jbd_lock_bh_state(bitmap_bh); |
658 | 617 | ||
659 | for (i = 0, group_freed = 0; i < count; i++) { | 618 | for (i = 0, group_freed = 0; i < count; i++) { |
660 | /* | 619 | /* |
661 | * An HJ special. This is expensive... | 620 | * An HJ special. This is expensive... |
662 | */ | 621 | */ |
663 | #ifdef CONFIG_JBD2_DEBUG | 622 | #ifdef CONFIG_JBD2_DEBUG |
664 | jbd_unlock_bh_state(bitmap_bh); | 623 | jbd_unlock_bh_state(bitmap_bh); |
665 | { | 624 | { |
666 | struct buffer_head *debug_bh; | 625 | struct buffer_head *debug_bh; |
667 | debug_bh = sb_find_get_block(sb, block + i); | 626 | debug_bh = sb_find_get_block(sb, block + i); |
668 | if (debug_bh) { | 627 | if (debug_bh) { |
669 | BUFFER_TRACE(debug_bh, "Deleted!"); | 628 | BUFFER_TRACE(debug_bh, "Deleted!"); |
670 | if (!bh2jh(bitmap_bh)->b_committed_data) | 629 | if (!bh2jh(bitmap_bh)->b_committed_data) |
671 | BUFFER_TRACE(debug_bh, | 630 | BUFFER_TRACE(debug_bh, |
672 | "No commited data in bitmap"); | 631 | "No commited data in bitmap"); |
673 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap"); | 632 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap"); |
674 | __brelse(debug_bh); | 633 | __brelse(debug_bh); |
675 | } | 634 | } |
676 | } | 635 | } |
677 | jbd_lock_bh_state(bitmap_bh); | 636 | jbd_lock_bh_state(bitmap_bh); |
678 | #endif | 637 | #endif |
679 | if (need_resched()) { | 638 | if (need_resched()) { |
680 | jbd_unlock_bh_state(bitmap_bh); | 639 | jbd_unlock_bh_state(bitmap_bh); |
681 | cond_resched(); | 640 | cond_resched(); |
682 | jbd_lock_bh_state(bitmap_bh); | 641 | jbd_lock_bh_state(bitmap_bh); |
683 | } | 642 | } |
684 | /* @@@ This prevents newly-allocated data from being | 643 | /* @@@ This prevents newly-allocated data from being |
685 | * freed and then reallocated within the same | 644 | * freed and then reallocated within the same |
686 | * transaction. | 645 | * transaction. |
687 | * | 646 | * |
688 | * Ideally we would want to allow that to happen, but to | 647 | * Ideally we would want to allow that to happen, but to |
689 | * do so requires making jbd2_journal_forget() capable of | 648 | * do so requires making jbd2_journal_forget() capable of |
690 | * revoking the queued write of a data block, which | 649 | * revoking the queued write of a data block, which |
691 | * implies blocking on the journal lock. *forget() | 650 | * implies blocking on the journal lock. *forget() |
692 | * cannot block due to truncate races. | 651 | * cannot block due to truncate races. |
693 | * | 652 | * |
694 | * Eventually we can fix this by making jbd2_journal_forget() | 653 | * Eventually we can fix this by making jbd2_journal_forget() |
695 | * return a status indicating whether or not it was able | 654 | * return a status indicating whether or not it was able |
696 | * to revoke the buffer. On successful revoke, it is | 655 | * to revoke the buffer. On successful revoke, it is |
697 | * safe not to set the allocation bit in the committed | 656 | * safe not to set the allocation bit in the committed |
698 | * bitmap, because we know that there is no outstanding | 657 | * bitmap, because we know that there is no outstanding |
699 | * activity on the buffer any more and so it is safe to | 658 | * activity on the buffer any more and so it is safe to |
700 | * reallocate it. | 659 | * reallocate it. |
701 | */ | 660 | */ |
702 | BUFFER_TRACE(bitmap_bh, "set in b_committed_data"); | 661 | BUFFER_TRACE(bitmap_bh, "set in b_committed_data"); |
703 | J_ASSERT_BH(bitmap_bh, | 662 | J_ASSERT_BH(bitmap_bh, |
704 | bh2jh(bitmap_bh)->b_committed_data != NULL); | 663 | bh2jh(bitmap_bh)->b_committed_data != NULL); |
705 | ext4_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i, | 664 | ext4_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i, |
706 | bh2jh(bitmap_bh)->b_committed_data); | 665 | bh2jh(bitmap_bh)->b_committed_data); |
707 | 666 | ||
708 | /* | 667 | /* |
709 | * We clear the bit in the bitmap after setting the committed | 668 | * We clear the bit in the bitmap after setting the committed |
710 | * data bit, because this is the reverse order to that which | 669 | * data bit, because this is the reverse order to that which |
711 | * the allocator uses. | 670 | * the allocator uses. |
712 | */ | 671 | */ |
713 | BUFFER_TRACE(bitmap_bh, "clear bit"); | 672 | BUFFER_TRACE(bitmap_bh, "clear bit"); |
714 | if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), | 673 | if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), |
715 | bit + i, bitmap_bh->b_data)) { | 674 | bit + i, bitmap_bh->b_data)) { |
716 | jbd_unlock_bh_state(bitmap_bh); | 675 | jbd_unlock_bh_state(bitmap_bh); |
717 | ext4_error(sb, __FUNCTION__, | 676 | ext4_error(sb, __FUNCTION__, |
718 | "bit already cleared for block %llu", | 677 | "bit already cleared for block %llu", |
719 | (ext4_fsblk_t)(block + i)); | 678 | (ext4_fsblk_t)(block + i)); |
720 | jbd_lock_bh_state(bitmap_bh); | 679 | jbd_lock_bh_state(bitmap_bh); |
721 | BUFFER_TRACE(bitmap_bh, "bit already cleared"); | 680 | BUFFER_TRACE(bitmap_bh, "bit already cleared"); |
722 | } else { | 681 | } else { |
723 | group_freed++; | 682 | group_freed++; |
724 | } | 683 | } |
725 | } | 684 | } |
726 | jbd_unlock_bh_state(bitmap_bh); | 685 | jbd_unlock_bh_state(bitmap_bh); |
727 | 686 | ||
728 | spin_lock(sb_bgl_lock(sbi, block_group)); | 687 | spin_lock(sb_bgl_lock(sbi, block_group)); |
729 | desc->bg_free_blocks_count = | 688 | desc->bg_free_blocks_count = |
730 | cpu_to_le16(le16_to_cpu(desc->bg_free_blocks_count) + | 689 | cpu_to_le16(le16_to_cpu(desc->bg_free_blocks_count) + |
731 | group_freed); | 690 | group_freed); |
732 | desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc); | 691 | desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc); |
733 | spin_unlock(sb_bgl_lock(sbi, block_group)); | 692 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
734 | percpu_counter_add(&sbi->s_freeblocks_counter, count); | 693 | percpu_counter_add(&sbi->s_freeblocks_counter, count); |
735 | 694 | ||
736 | /* We dirtied the bitmap block */ | 695 | /* We dirtied the bitmap block */ |
737 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); | 696 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); |
738 | err = ext4_journal_dirty_metadata(handle, bitmap_bh); | 697 | err = ext4_journal_dirty_metadata(handle, bitmap_bh); |
739 | 698 | ||
740 | /* And the group descriptor block */ | 699 | /* And the group descriptor block */ |
741 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); | 700 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); |
742 | ret = ext4_journal_dirty_metadata(handle, gd_bh); | 701 | ret = ext4_journal_dirty_metadata(handle, gd_bh); |
743 | if (!err) err = ret; | 702 | if (!err) err = ret; |
744 | *pdquot_freed_blocks += group_freed; | 703 | *pdquot_freed_blocks += group_freed; |
745 | 704 | ||
746 | if (overflow && !err) { | 705 | if (overflow && !err) { |
747 | block += count; | 706 | block += count; |
748 | count = overflow; | 707 | count = overflow; |
749 | goto do_more; | 708 | goto do_more; |
750 | } | 709 | } |
751 | sb->s_dirt = 1; | 710 | sb->s_dirt = 1; |
752 | error_return: | 711 | error_return: |
753 | brelse(bitmap_bh); | 712 | brelse(bitmap_bh); |
754 | ext4_std_error(sb, err); | 713 | ext4_std_error(sb, err); |
755 | return; | 714 | return; |
756 | } | 715 | } |
757 | 716 | ||
758 | /** | 717 | /** |
759 | * ext4_free_blocks() -- Free given blocks and update quota | 718 | * ext4_free_blocks() -- Free given blocks and update quota |
760 | * @handle: handle for this transaction | 719 | * @handle: handle for this transaction |
761 | * @inode: inode | 720 | * @inode: inode |
762 | * @block: start physical block to free | 721 | * @block: start physical block to free |
763 | * @count: number of blocks to count | 722 | * @count: number of blocks to count |
764 | */ | 723 | */ |
765 | void ext4_free_blocks(handle_t *handle, struct inode *inode, | 724 | void ext4_free_blocks(handle_t *handle, struct inode *inode, |
766 | ext4_fsblk_t block, unsigned long count) | 725 | ext4_fsblk_t block, unsigned long count) |
767 | { | 726 | { |
768 | struct super_block * sb; | 727 | struct super_block * sb; |
769 | unsigned long dquot_freed_blocks; | 728 | unsigned long dquot_freed_blocks; |
770 | 729 | ||
771 | sb = inode->i_sb; | 730 | sb = inode->i_sb; |
772 | if (!sb) { | 731 | if (!sb) { |
773 | printk ("ext4_free_blocks: nonexistent device"); | 732 | printk ("ext4_free_blocks: nonexistent device"); |
774 | return; | 733 | return; |
775 | } | 734 | } |
776 | ext4_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks); | 735 | ext4_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks); |
777 | if (dquot_freed_blocks) | 736 | if (dquot_freed_blocks) |
778 | DQUOT_FREE_BLOCK(inode, dquot_freed_blocks); | 737 | DQUOT_FREE_BLOCK(inode, dquot_freed_blocks); |
779 | return; | 738 | return; |
780 | } | 739 | } |
781 | 740 | ||
782 | /** | 741 | /** |
783 | * ext4_test_allocatable() | 742 | * ext4_test_allocatable() |
784 | * @nr: given allocation block group | 743 | * @nr: given allocation block group |
785 | * @bh: bufferhead contains the bitmap of the given block group | 744 | * @bh: bufferhead contains the bitmap of the given block group |
786 | * | 745 | * |
787 | * For ext4 allocations, we must not reuse any blocks which are | 746 | * For ext4 allocations, we must not reuse any blocks which are |
788 | * allocated in the bitmap buffer's "last committed data" copy. This | 747 | * allocated in the bitmap buffer's "last committed data" copy. This |
789 | * prevents deletes from freeing up the page for reuse until we have | 748 | * prevents deletes from freeing up the page for reuse until we have |
790 | * committed the delete transaction. | 749 | * committed the delete transaction. |
791 | * | 750 | * |
792 | * If we didn't do this, then deleting something and reallocating it as | 751 | * If we didn't do this, then deleting something and reallocating it as |
793 | * data would allow the old block to be overwritten before the | 752 | * data would allow the old block to be overwritten before the |
794 | * transaction committed (because we force data to disk before commit). | 753 | * transaction committed (because we force data to disk before commit). |
795 | * This would lead to corruption if we crashed between overwriting the | 754 | * This would lead to corruption if we crashed between overwriting the |
796 | * data and committing the delete. | 755 | * data and committing the delete. |
797 | * | 756 | * |
798 | * @@@ We may want to make this allocation behaviour conditional on | 757 | * @@@ We may want to make this allocation behaviour conditional on |
799 | * data-writes at some point, and disable it for metadata allocations or | 758 | * data-writes at some point, and disable it for metadata allocations or |
800 | * sync-data inodes. | 759 | * sync-data inodes. |
801 | */ | 760 | */ |
802 | static int ext4_test_allocatable(ext4_grpblk_t nr, struct buffer_head *bh) | 761 | static int ext4_test_allocatable(ext4_grpblk_t nr, struct buffer_head *bh) |
803 | { | 762 | { |
804 | int ret; | 763 | int ret; |
805 | struct journal_head *jh = bh2jh(bh); | 764 | struct journal_head *jh = bh2jh(bh); |
806 | 765 | ||
807 | if (ext4_test_bit(nr, bh->b_data)) | 766 | if (ext4_test_bit(nr, bh->b_data)) |
808 | return 0; | 767 | return 0; |
809 | 768 | ||
810 | jbd_lock_bh_state(bh); | 769 | jbd_lock_bh_state(bh); |
811 | if (!jh->b_committed_data) | 770 | if (!jh->b_committed_data) |
812 | ret = 1; | 771 | ret = 1; |
813 | else | 772 | else |
814 | ret = !ext4_test_bit(nr, jh->b_committed_data); | 773 | ret = !ext4_test_bit(nr, jh->b_committed_data); |
815 | jbd_unlock_bh_state(bh); | 774 | jbd_unlock_bh_state(bh); |
816 | return ret; | 775 | return ret; |
817 | } | 776 | } |
818 | 777 | ||
819 | /** | 778 | /** |
820 | * bitmap_search_next_usable_block() | 779 | * bitmap_search_next_usable_block() |
821 | * @start: the starting block (group relative) of the search | 780 | * @start: the starting block (group relative) of the search |
822 | * @bh: bufferhead contains the block group bitmap | 781 | * @bh: bufferhead contains the block group bitmap |
823 | * @maxblocks: the ending block (group relative) of the reservation | 782 | * @maxblocks: the ending block (group relative) of the reservation |
824 | * | 783 | * |
825 | * The bitmap search --- search forward alternately through the actual | 784 | * The bitmap search --- search forward alternately through the actual |
826 | * bitmap on disk and the last-committed copy in journal, until we find a | 785 | * bitmap on disk and the last-committed copy in journal, until we find a |
827 | * bit free in both bitmaps. | 786 | * bit free in both bitmaps. |
828 | */ | 787 | */ |
829 | static ext4_grpblk_t | 788 | static ext4_grpblk_t |
830 | bitmap_search_next_usable_block(ext4_grpblk_t start, struct buffer_head *bh, | 789 | bitmap_search_next_usable_block(ext4_grpblk_t start, struct buffer_head *bh, |
831 | ext4_grpblk_t maxblocks) | 790 | ext4_grpblk_t maxblocks) |
832 | { | 791 | { |
833 | ext4_grpblk_t next; | 792 | ext4_grpblk_t next; |
834 | struct journal_head *jh = bh2jh(bh); | 793 | struct journal_head *jh = bh2jh(bh); |
835 | 794 | ||
836 | while (start < maxblocks) { | 795 | while (start < maxblocks) { |
837 | next = ext4_find_next_zero_bit(bh->b_data, maxblocks, start); | 796 | next = ext4_find_next_zero_bit(bh->b_data, maxblocks, start); |
838 | if (next >= maxblocks) | 797 | if (next >= maxblocks) |
839 | return -1; | 798 | return -1; |
840 | if (ext4_test_allocatable(next, bh)) | 799 | if (ext4_test_allocatable(next, bh)) |
841 | return next; | 800 | return next; |
842 | jbd_lock_bh_state(bh); | 801 | jbd_lock_bh_state(bh); |
843 | if (jh->b_committed_data) | 802 | if (jh->b_committed_data) |
844 | start = ext4_find_next_zero_bit(jh->b_committed_data, | 803 | start = ext4_find_next_zero_bit(jh->b_committed_data, |
845 | maxblocks, next); | 804 | maxblocks, next); |
846 | jbd_unlock_bh_state(bh); | 805 | jbd_unlock_bh_state(bh); |
847 | } | 806 | } |
848 | return -1; | 807 | return -1; |
849 | } | 808 | } |
850 | 809 | ||
851 | /** | 810 | /** |
852 | * find_next_usable_block() | 811 | * find_next_usable_block() |
853 | * @start: the starting block (group relative) to find next | 812 | * @start: the starting block (group relative) to find next |
854 | * allocatable block in bitmap. | 813 | * allocatable block in bitmap. |
855 | * @bh: bufferhead contains the block group bitmap | 814 | * @bh: bufferhead contains the block group bitmap |
856 | * @maxblocks: the ending block (group relative) for the search | 815 | * @maxblocks: the ending block (group relative) for the search |
857 | * | 816 | * |
858 | * Find an allocatable block in a bitmap. We honor both the bitmap and | 817 | * Find an allocatable block in a bitmap. We honor both the bitmap and |
859 | * its last-committed copy (if that exists), and perform the "most | 818 | * its last-committed copy (if that exists), and perform the "most |
860 | * appropriate allocation" algorithm of looking for a free block near | 819 | * appropriate allocation" algorithm of looking for a free block near |
861 | * the initial goal; then for a free byte somewhere in the bitmap; then | 820 | * the initial goal; then for a free byte somewhere in the bitmap; then |
862 | * for any free bit in the bitmap. | 821 | * for any free bit in the bitmap. |
863 | */ | 822 | */ |
864 | static ext4_grpblk_t | 823 | static ext4_grpblk_t |
865 | find_next_usable_block(ext4_grpblk_t start, struct buffer_head *bh, | 824 | find_next_usable_block(ext4_grpblk_t start, struct buffer_head *bh, |
866 | ext4_grpblk_t maxblocks) | 825 | ext4_grpblk_t maxblocks) |
867 | { | 826 | { |
868 | ext4_grpblk_t here, next; | 827 | ext4_grpblk_t here, next; |
869 | char *p, *r; | 828 | char *p, *r; |
870 | 829 | ||
871 | if (start > 0) { | 830 | if (start > 0) { |
872 | /* | 831 | /* |
873 | * The goal was occupied; search forward for a free | 832 | * The goal was occupied; search forward for a free |
874 | * block within the next XX blocks. | 833 | * block within the next XX blocks. |
875 | * | 834 | * |
876 | * end_goal is more or less random, but it has to be | 835 | * end_goal is more or less random, but it has to be |
877 | * less than EXT4_BLOCKS_PER_GROUP. Aligning up to the | 836 | * less than EXT4_BLOCKS_PER_GROUP. Aligning up to the |
878 | * next 64-bit boundary is simple.. | 837 | * next 64-bit boundary is simple.. |
879 | */ | 838 | */ |
880 | ext4_grpblk_t end_goal = (start + 63) & ~63; | 839 | ext4_grpblk_t end_goal = (start + 63) & ~63; |
881 | if (end_goal > maxblocks) | 840 | if (end_goal > maxblocks) |
882 | end_goal = maxblocks; | 841 | end_goal = maxblocks; |
883 | here = ext4_find_next_zero_bit(bh->b_data, end_goal, start); | 842 | here = ext4_find_next_zero_bit(bh->b_data, end_goal, start); |
884 | if (here < end_goal && ext4_test_allocatable(here, bh)) | 843 | if (here < end_goal && ext4_test_allocatable(here, bh)) |
885 | return here; | 844 | return here; |
886 | ext4_debug("Bit not found near goal\n"); | 845 | ext4_debug("Bit not found near goal\n"); |
887 | } | 846 | } |
888 | 847 | ||
889 | here = start; | 848 | here = start; |
890 | if (here < 0) | 849 | if (here < 0) |
891 | here = 0; | 850 | here = 0; |
892 | 851 | ||
893 | p = ((char *)bh->b_data) + (here >> 3); | 852 | p = ((char *)bh->b_data) + (here >> 3); |
894 | r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3)); | 853 | r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3)); |
895 | next = (r - ((char *)bh->b_data)) << 3; | 854 | next = (r - ((char *)bh->b_data)) << 3; |
896 | 855 | ||
897 | if (next < maxblocks && next >= start && ext4_test_allocatable(next, bh)) | 856 | if (next < maxblocks && next >= start && ext4_test_allocatable(next, bh)) |
898 | return next; | 857 | return next; |
899 | 858 | ||
900 | /* | 859 | /* |
901 | * The bitmap search --- search forward alternately through the actual | 860 | * The bitmap search --- search forward alternately through the actual |
902 | * bitmap and the last-committed copy until we find a bit free in | 861 | * bitmap and the last-committed copy until we find a bit free in |
903 | * both | 862 | * both |
904 | */ | 863 | */ |
905 | here = bitmap_search_next_usable_block(here, bh, maxblocks); | 864 | here = bitmap_search_next_usable_block(here, bh, maxblocks); |
906 | return here; | 865 | return here; |
907 | } | 866 | } |
908 | 867 | ||
909 | /** | 868 | /** |
910 | * claim_block() | 869 | * claim_block() |
911 | * @block: the free block (group relative) to allocate | 870 | * @block: the free block (group relative) to allocate |
912 | * @bh: the bufferhead containts the block group bitmap | 871 | * @bh: the bufferhead containts the block group bitmap |
913 | * | 872 | * |
914 | * We think we can allocate this block in this bitmap. Try to set the bit. | 873 | * We think we can allocate this block in this bitmap. Try to set the bit. |
915 | * If that succeeds then check that nobody has allocated and then freed the | 874 | * If that succeeds then check that nobody has allocated and then freed the |
916 | * block since we saw that is was not marked in b_committed_data. If it _was_ | 875 | * block since we saw that is was not marked in b_committed_data. If it _was_ |
917 | * allocated and freed then clear the bit in the bitmap again and return | 876 | * allocated and freed then clear the bit in the bitmap again and return |
918 | * zero (failure). | 877 | * zero (failure). |
919 | */ | 878 | */ |
920 | static inline int | 879 | static inline int |
921 | claim_block(spinlock_t *lock, ext4_grpblk_t block, struct buffer_head *bh) | 880 | claim_block(spinlock_t *lock, ext4_grpblk_t block, struct buffer_head *bh) |
922 | { | 881 | { |
923 | struct journal_head *jh = bh2jh(bh); | 882 | struct journal_head *jh = bh2jh(bh); |
924 | int ret; | 883 | int ret; |
925 | 884 | ||
926 | if (ext4_set_bit_atomic(lock, block, bh->b_data)) | 885 | if (ext4_set_bit_atomic(lock, block, bh->b_data)) |
927 | return 0; | 886 | return 0; |
928 | jbd_lock_bh_state(bh); | 887 | jbd_lock_bh_state(bh); |
929 | if (jh->b_committed_data && ext4_test_bit(block,jh->b_committed_data)) { | 888 | if (jh->b_committed_data && ext4_test_bit(block,jh->b_committed_data)) { |
930 | ext4_clear_bit_atomic(lock, block, bh->b_data); | 889 | ext4_clear_bit_atomic(lock, block, bh->b_data); |
931 | ret = 0; | 890 | ret = 0; |
932 | } else { | 891 | } else { |
933 | ret = 1; | 892 | ret = 1; |
934 | } | 893 | } |
935 | jbd_unlock_bh_state(bh); | 894 | jbd_unlock_bh_state(bh); |
936 | return ret; | 895 | return ret; |
937 | } | 896 | } |
938 | 897 | ||
939 | /** | 898 | /** |
940 | * ext4_try_to_allocate() | 899 | * ext4_try_to_allocate() |
941 | * @sb: superblock | 900 | * @sb: superblock |
942 | * @handle: handle to this transaction | 901 | * @handle: handle to this transaction |
943 | * @group: given allocation block group | 902 | * @group: given allocation block group |
944 | * @bitmap_bh: bufferhead holds the block bitmap | 903 | * @bitmap_bh: bufferhead holds the block bitmap |
945 | * @grp_goal: given target block within the group | 904 | * @grp_goal: given target block within the group |
946 | * @count: target number of blocks to allocate | 905 | * @count: target number of blocks to allocate |
947 | * @my_rsv: reservation window | 906 | * @my_rsv: reservation window |
948 | * | 907 | * |
949 | * Attempt to allocate blocks within a give range. Set the range of allocation | 908 | * Attempt to allocate blocks within a give range. Set the range of allocation |
950 | * first, then find the first free bit(s) from the bitmap (within the range), | 909 | * first, then find the first free bit(s) from the bitmap (within the range), |
951 | * and at last, allocate the blocks by claiming the found free bit as allocated. | 910 | * and at last, allocate the blocks by claiming the found free bit as allocated. |
952 | * | 911 | * |
953 | * To set the range of this allocation: | 912 | * To set the range of this allocation: |
954 | * if there is a reservation window, only try to allocate block(s) from the | 913 | * if there is a reservation window, only try to allocate block(s) from the |
955 | * file's own reservation window; | 914 | * file's own reservation window; |
956 | * Otherwise, the allocation range starts from the give goal block, ends at | 915 | * Otherwise, the allocation range starts from the give goal block, ends at |
957 | * the block group's last block. | 916 | * the block group's last block. |
958 | * | 917 | * |
959 | * If we failed to allocate the desired block then we may end up crossing to a | 918 | * If we failed to allocate the desired block then we may end up crossing to a |
960 | * new bitmap. In that case we must release write access to the old one via | 919 | * new bitmap. In that case we must release write access to the old one via |
961 | * ext4_journal_release_buffer(), else we'll run out of credits. | 920 | * ext4_journal_release_buffer(), else we'll run out of credits. |
962 | */ | 921 | */ |
963 | static ext4_grpblk_t | 922 | static ext4_grpblk_t |
964 | ext4_try_to_allocate(struct super_block *sb, handle_t *handle, int group, | 923 | ext4_try_to_allocate(struct super_block *sb, handle_t *handle, int group, |
965 | struct buffer_head *bitmap_bh, ext4_grpblk_t grp_goal, | 924 | struct buffer_head *bitmap_bh, ext4_grpblk_t grp_goal, |
966 | unsigned long *count, struct ext4_reserve_window *my_rsv) | 925 | unsigned long *count, struct ext4_reserve_window *my_rsv) |
967 | { | 926 | { |
968 | ext4_fsblk_t group_first_block; | 927 | ext4_fsblk_t group_first_block; |
969 | ext4_grpblk_t start, end; | 928 | ext4_grpblk_t start, end; |
970 | unsigned long num = 0; | 929 | unsigned long num = 0; |
971 | 930 | ||
972 | /* we do allocation within the reservation window if we have a window */ | 931 | /* we do allocation within the reservation window if we have a window */ |
973 | if (my_rsv) { | 932 | if (my_rsv) { |
974 | group_first_block = ext4_group_first_block_no(sb, group); | 933 | group_first_block = ext4_group_first_block_no(sb, group); |
975 | if (my_rsv->_rsv_start >= group_first_block) | 934 | if (my_rsv->_rsv_start >= group_first_block) |
976 | start = my_rsv->_rsv_start - group_first_block; | 935 | start = my_rsv->_rsv_start - group_first_block; |
977 | else | 936 | else |
978 | /* reservation window cross group boundary */ | 937 | /* reservation window cross group boundary */ |
979 | start = 0; | 938 | start = 0; |
980 | end = my_rsv->_rsv_end - group_first_block + 1; | 939 | end = my_rsv->_rsv_end - group_first_block + 1; |
981 | if (end > EXT4_BLOCKS_PER_GROUP(sb)) | 940 | if (end > EXT4_BLOCKS_PER_GROUP(sb)) |
982 | /* reservation window crosses group boundary */ | 941 | /* reservation window crosses group boundary */ |
983 | end = EXT4_BLOCKS_PER_GROUP(sb); | 942 | end = EXT4_BLOCKS_PER_GROUP(sb); |
984 | if ((start <= grp_goal) && (grp_goal < end)) | 943 | if ((start <= grp_goal) && (grp_goal < end)) |
985 | start = grp_goal; | 944 | start = grp_goal; |
986 | else | 945 | else |
987 | grp_goal = -1; | 946 | grp_goal = -1; |
988 | } else { | 947 | } else { |
989 | if (grp_goal > 0) | 948 | if (grp_goal > 0) |
990 | start = grp_goal; | 949 | start = grp_goal; |
991 | else | 950 | else |
992 | start = 0; | 951 | start = 0; |
993 | end = EXT4_BLOCKS_PER_GROUP(sb); | 952 | end = EXT4_BLOCKS_PER_GROUP(sb); |
994 | } | 953 | } |
995 | 954 | ||
996 | BUG_ON(start > EXT4_BLOCKS_PER_GROUP(sb)); | 955 | BUG_ON(start > EXT4_BLOCKS_PER_GROUP(sb)); |
997 | 956 | ||
998 | repeat: | 957 | repeat: |
999 | if (grp_goal < 0 || !ext4_test_allocatable(grp_goal, bitmap_bh)) { | 958 | if (grp_goal < 0 || !ext4_test_allocatable(grp_goal, bitmap_bh)) { |
1000 | grp_goal = find_next_usable_block(start, bitmap_bh, end); | 959 | grp_goal = find_next_usable_block(start, bitmap_bh, end); |
1001 | if (grp_goal < 0) | 960 | if (grp_goal < 0) |
1002 | goto fail_access; | 961 | goto fail_access; |
1003 | if (!my_rsv) { | 962 | if (!my_rsv) { |
1004 | int i; | 963 | int i; |
1005 | 964 | ||
1006 | for (i = 0; i < 7 && grp_goal > start && | 965 | for (i = 0; i < 7 && grp_goal > start && |
1007 | ext4_test_allocatable(grp_goal - 1, | 966 | ext4_test_allocatable(grp_goal - 1, |
1008 | bitmap_bh); | 967 | bitmap_bh); |
1009 | i++, grp_goal--) | 968 | i++, grp_goal--) |
1010 | ; | 969 | ; |
1011 | } | 970 | } |
1012 | } | 971 | } |
1013 | start = grp_goal; | 972 | start = grp_goal; |
1014 | 973 | ||
1015 | if (!claim_block(sb_bgl_lock(EXT4_SB(sb), group), | 974 | if (!claim_block(sb_bgl_lock(EXT4_SB(sb), group), |
1016 | grp_goal, bitmap_bh)) { | 975 | grp_goal, bitmap_bh)) { |
1017 | /* | 976 | /* |
1018 | * The block was allocated by another thread, or it was | 977 | * The block was allocated by another thread, or it was |
1019 | * allocated and then freed by another thread | 978 | * allocated and then freed by another thread |
1020 | */ | 979 | */ |
1021 | start++; | 980 | start++; |
1022 | grp_goal++; | 981 | grp_goal++; |
1023 | if (start >= end) | 982 | if (start >= end) |
1024 | goto fail_access; | 983 | goto fail_access; |
1025 | goto repeat; | 984 | goto repeat; |
1026 | } | 985 | } |
1027 | num++; | 986 | num++; |
1028 | grp_goal++; | 987 | grp_goal++; |
1029 | while (num < *count && grp_goal < end | 988 | while (num < *count && grp_goal < end |
1030 | && ext4_test_allocatable(grp_goal, bitmap_bh) | 989 | && ext4_test_allocatable(grp_goal, bitmap_bh) |
1031 | && claim_block(sb_bgl_lock(EXT4_SB(sb), group), | 990 | && claim_block(sb_bgl_lock(EXT4_SB(sb), group), |
1032 | grp_goal, bitmap_bh)) { | 991 | grp_goal, bitmap_bh)) { |
1033 | num++; | 992 | num++; |
1034 | grp_goal++; | 993 | grp_goal++; |
1035 | } | 994 | } |
1036 | *count = num; | 995 | *count = num; |
1037 | return grp_goal - num; | 996 | return grp_goal - num; |
1038 | fail_access: | 997 | fail_access: |
1039 | *count = num; | 998 | *count = num; |
1040 | return -1; | 999 | return -1; |
1041 | } | 1000 | } |
1042 | 1001 | ||
1043 | /** | 1002 | /** |
1044 | * find_next_reservable_window(): | 1003 | * find_next_reservable_window(): |
1045 | * find a reservable space within the given range. | 1004 | * find a reservable space within the given range. |
1046 | * It does not allocate the reservation window for now: | 1005 | * It does not allocate the reservation window for now: |
1047 | * alloc_new_reservation() will do the work later. | 1006 | * alloc_new_reservation() will do the work later. |
1048 | * | 1007 | * |
1049 | * @search_head: the head of the searching list; | 1008 | * @search_head: the head of the searching list; |
1050 | * This is not necessarily the list head of the whole filesystem | 1009 | * This is not necessarily the list head of the whole filesystem |
1051 | * | 1010 | * |
1052 | * We have both head and start_block to assist the search | 1011 | * We have both head and start_block to assist the search |
1053 | * for the reservable space. The list starts from head, | 1012 | * for the reservable space. The list starts from head, |
1054 | * but we will shift to the place where start_block is, | 1013 | * but we will shift to the place where start_block is, |
1055 | * then start from there, when looking for a reservable space. | 1014 | * then start from there, when looking for a reservable space. |
1056 | * | 1015 | * |
1057 | * @size: the target new reservation window size | 1016 | * @size: the target new reservation window size |
1058 | * | 1017 | * |
1059 | * @group_first_block: the first block we consider to start | 1018 | * @group_first_block: the first block we consider to start |
1060 | * the real search from | 1019 | * the real search from |
1061 | * | 1020 | * |
1062 | * @last_block: | 1021 | * @last_block: |
1063 | * the maximum block number that our goal reservable space | 1022 | * the maximum block number that our goal reservable space |
1064 | * could start from. This is normally the last block in this | 1023 | * could start from. This is normally the last block in this |
1065 | * group. The search will end when we found the start of next | 1024 | * group. The search will end when we found the start of next |
1066 | * possible reservable space is out of this boundary. | 1025 | * possible reservable space is out of this boundary. |
1067 | * This could handle the cross boundary reservation window | 1026 | * This could handle the cross boundary reservation window |
1068 | * request. | 1027 | * request. |
1069 | * | 1028 | * |
1070 | * basically we search from the given range, rather than the whole | 1029 | * basically we search from the given range, rather than the whole |
1071 | * reservation double linked list, (start_block, last_block) | 1030 | * reservation double linked list, (start_block, last_block) |
1072 | * to find a free region that is of my size and has not | 1031 | * to find a free region that is of my size and has not |
1073 | * been reserved. | 1032 | * been reserved. |
1074 | * | 1033 | * |
1075 | */ | 1034 | */ |
1076 | static int find_next_reservable_window( | 1035 | static int find_next_reservable_window( |
1077 | struct ext4_reserve_window_node *search_head, | 1036 | struct ext4_reserve_window_node *search_head, |
1078 | struct ext4_reserve_window_node *my_rsv, | 1037 | struct ext4_reserve_window_node *my_rsv, |
1079 | struct super_block * sb, | 1038 | struct super_block * sb, |
1080 | ext4_fsblk_t start_block, | 1039 | ext4_fsblk_t start_block, |
1081 | ext4_fsblk_t last_block) | 1040 | ext4_fsblk_t last_block) |
1082 | { | 1041 | { |
1083 | struct rb_node *next; | 1042 | struct rb_node *next; |
1084 | struct ext4_reserve_window_node *rsv, *prev; | 1043 | struct ext4_reserve_window_node *rsv, *prev; |
1085 | ext4_fsblk_t cur; | 1044 | ext4_fsblk_t cur; |
1086 | int size = my_rsv->rsv_goal_size; | 1045 | int size = my_rsv->rsv_goal_size; |
1087 | 1046 | ||
1088 | /* TODO: make the start of the reservation window byte-aligned */ | 1047 | /* TODO: make the start of the reservation window byte-aligned */ |
1089 | /* cur = *start_block & ~7;*/ | 1048 | /* cur = *start_block & ~7;*/ |
1090 | cur = start_block; | 1049 | cur = start_block; |
1091 | rsv = search_head; | 1050 | rsv = search_head; |
1092 | if (!rsv) | 1051 | if (!rsv) |
1093 | return -1; | 1052 | return -1; |
1094 | 1053 | ||
1095 | while (1) { | 1054 | while (1) { |
1096 | if (cur <= rsv->rsv_end) | 1055 | if (cur <= rsv->rsv_end) |
1097 | cur = rsv->rsv_end + 1; | 1056 | cur = rsv->rsv_end + 1; |
1098 | 1057 | ||
1099 | /* TODO? | 1058 | /* TODO? |
1100 | * in the case we could not find a reservable space | 1059 | * in the case we could not find a reservable space |
1101 | * that is what is expected, during the re-search, we could | 1060 | * that is what is expected, during the re-search, we could |
1102 | * remember what's the largest reservable space we could have | 1061 | * remember what's the largest reservable space we could have |
1103 | * and return that one. | 1062 | * and return that one. |
1104 | * | 1063 | * |
1105 | * For now it will fail if we could not find the reservable | 1064 | * For now it will fail if we could not find the reservable |
1106 | * space with expected-size (or more)... | 1065 | * space with expected-size (or more)... |
1107 | */ | 1066 | */ |
1108 | if (cur > last_block) | 1067 | if (cur > last_block) |
1109 | return -1; /* fail */ | 1068 | return -1; /* fail */ |
1110 | 1069 | ||
1111 | prev = rsv; | 1070 | prev = rsv; |
1112 | next = rb_next(&rsv->rsv_node); | 1071 | next = rb_next(&rsv->rsv_node); |
1113 | rsv = rb_entry(next,struct ext4_reserve_window_node,rsv_node); | 1072 | rsv = rb_entry(next,struct ext4_reserve_window_node,rsv_node); |
1114 | 1073 | ||
1115 | /* | 1074 | /* |
1116 | * Reached the last reservation, we can just append to the | 1075 | * Reached the last reservation, we can just append to the |
1117 | * previous one. | 1076 | * previous one. |
1118 | */ | 1077 | */ |
1119 | if (!next) | 1078 | if (!next) |
1120 | break; | 1079 | break; |
1121 | 1080 | ||
1122 | if (cur + size <= rsv->rsv_start) { | 1081 | if (cur + size <= rsv->rsv_start) { |
1123 | /* | 1082 | /* |
1124 | * Found a reserveable space big enough. We could | 1083 | * Found a reserveable space big enough. We could |
1125 | * have a reservation across the group boundary here | 1084 | * have a reservation across the group boundary here |
1126 | */ | 1085 | */ |
1127 | break; | 1086 | break; |
1128 | } | 1087 | } |
1129 | } | 1088 | } |
1130 | /* | 1089 | /* |
1131 | * we come here either : | 1090 | * we come here either : |
1132 | * when we reach the end of the whole list, | 1091 | * when we reach the end of the whole list, |
1133 | * and there is empty reservable space after last entry in the list. | 1092 | * and there is empty reservable space after last entry in the list. |
1134 | * append it to the end of the list. | 1093 | * append it to the end of the list. |
1135 | * | 1094 | * |
1136 | * or we found one reservable space in the middle of the list, | 1095 | * or we found one reservable space in the middle of the list, |
1137 | * return the reservation window that we could append to. | 1096 | * return the reservation window that we could append to. |
1138 | * succeed. | 1097 | * succeed. |
1139 | */ | 1098 | */ |
1140 | 1099 | ||
1141 | if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window))) | 1100 | if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window))) |
1142 | rsv_window_remove(sb, my_rsv); | 1101 | rsv_window_remove(sb, my_rsv); |
1143 | 1102 | ||
1144 | /* | 1103 | /* |
1145 | * Let's book the whole avaliable window for now. We will check the | 1104 | * Let's book the whole avaliable window for now. We will check the |
1146 | * disk bitmap later and then, if there are free blocks then we adjust | 1105 | * disk bitmap later and then, if there are free blocks then we adjust |
1147 | * the window size if it's larger than requested. | 1106 | * the window size if it's larger than requested. |
1148 | * Otherwise, we will remove this node from the tree next time | 1107 | * Otherwise, we will remove this node from the tree next time |
1149 | * call find_next_reservable_window. | 1108 | * call find_next_reservable_window. |
1150 | */ | 1109 | */ |
1151 | my_rsv->rsv_start = cur; | 1110 | my_rsv->rsv_start = cur; |
1152 | my_rsv->rsv_end = cur + size - 1; | 1111 | my_rsv->rsv_end = cur + size - 1; |
1153 | my_rsv->rsv_alloc_hit = 0; | 1112 | my_rsv->rsv_alloc_hit = 0; |
1154 | 1113 | ||
1155 | if (prev != my_rsv) | 1114 | if (prev != my_rsv) |
1156 | ext4_rsv_window_add(sb, my_rsv); | 1115 | ext4_rsv_window_add(sb, my_rsv); |
1157 | 1116 | ||
1158 | return 0; | 1117 | return 0; |
1159 | } | 1118 | } |
1160 | 1119 | ||
1161 | /** | 1120 | /** |
1162 | * alloc_new_reservation()--allocate a new reservation window | 1121 | * alloc_new_reservation()--allocate a new reservation window |
1163 | * | 1122 | * |
1164 | * To make a new reservation, we search part of the filesystem | 1123 | * To make a new reservation, we search part of the filesystem |
1165 | * reservation list (the list that inside the group). We try to | 1124 | * reservation list (the list that inside the group). We try to |
1166 | * allocate a new reservation window near the allocation goal, | 1125 | * allocate a new reservation window near the allocation goal, |
1167 | * or the beginning of the group, if there is no goal. | 1126 | * or the beginning of the group, if there is no goal. |
1168 | * | 1127 | * |
1169 | * We first find a reservable space after the goal, then from | 1128 | * We first find a reservable space after the goal, then from |
1170 | * there, we check the bitmap for the first free block after | 1129 | * there, we check the bitmap for the first free block after |
1171 | * it. If there is no free block until the end of group, then the | 1130 | * it. If there is no free block until the end of group, then the |
1172 | * whole group is full, we failed. Otherwise, check if the free | 1131 | * whole group is full, we failed. Otherwise, check if the free |
1173 | * block is inside the expected reservable space, if so, we | 1132 | * block is inside the expected reservable space, if so, we |
1174 | * succeed. | 1133 | * succeed. |
1175 | * If the first free block is outside the reservable space, then | 1134 | * If the first free block is outside the reservable space, then |
1176 | * start from the first free block, we search for next available | 1135 | * start from the first free block, we search for next available |
1177 | * space, and go on. | 1136 | * space, and go on. |
1178 | * | 1137 | * |
1179 | * on succeed, a new reservation will be found and inserted into the list | 1138 | * on succeed, a new reservation will be found and inserted into the list |
1180 | * It contains at least one free block, and it does not overlap with other | 1139 | * It contains at least one free block, and it does not overlap with other |
1181 | * reservation windows. | 1140 | * reservation windows. |
1182 | * | 1141 | * |
1183 | * failed: we failed to find a reservation window in this group | 1142 | * failed: we failed to find a reservation window in this group |
1184 | * | 1143 | * |
1185 | * @rsv: the reservation | 1144 | * @rsv: the reservation |
1186 | * | 1145 | * |
1187 | * @grp_goal: The goal (group-relative). It is where the search for a | 1146 | * @grp_goal: The goal (group-relative). It is where the search for a |
1188 | * free reservable space should start from. | 1147 | * free reservable space should start from. |
1189 | * if we have a grp_goal(grp_goal >0 ), then start from there, | 1148 | * if we have a grp_goal(grp_goal >0 ), then start from there, |
1190 | * no grp_goal(grp_goal = -1), we start from the first block | 1149 | * no grp_goal(grp_goal = -1), we start from the first block |
1191 | * of the group. | 1150 | * of the group. |
1192 | * | 1151 | * |
1193 | * @sb: the super block | 1152 | * @sb: the super block |
1194 | * @group: the group we are trying to allocate in | 1153 | * @group: the group we are trying to allocate in |
1195 | * @bitmap_bh: the block group block bitmap | 1154 | * @bitmap_bh: the block group block bitmap |
1196 | * | 1155 | * |
1197 | */ | 1156 | */ |
1198 | static int alloc_new_reservation(struct ext4_reserve_window_node *my_rsv, | 1157 | static int alloc_new_reservation(struct ext4_reserve_window_node *my_rsv, |
1199 | ext4_grpblk_t grp_goal, struct super_block *sb, | 1158 | ext4_grpblk_t grp_goal, struct super_block *sb, |
1200 | unsigned int group, struct buffer_head *bitmap_bh) | 1159 | unsigned int group, struct buffer_head *bitmap_bh) |
1201 | { | 1160 | { |
1202 | struct ext4_reserve_window_node *search_head; | 1161 | struct ext4_reserve_window_node *search_head; |
1203 | ext4_fsblk_t group_first_block, group_end_block, start_block; | 1162 | ext4_fsblk_t group_first_block, group_end_block, start_block; |
1204 | ext4_grpblk_t first_free_block; | 1163 | ext4_grpblk_t first_free_block; |
1205 | struct rb_root *fs_rsv_root = &EXT4_SB(sb)->s_rsv_window_root; | 1164 | struct rb_root *fs_rsv_root = &EXT4_SB(sb)->s_rsv_window_root; |
1206 | unsigned long size; | 1165 | unsigned long size; |
1207 | int ret; | 1166 | int ret; |
1208 | spinlock_t *rsv_lock = &EXT4_SB(sb)->s_rsv_window_lock; | 1167 | spinlock_t *rsv_lock = &EXT4_SB(sb)->s_rsv_window_lock; |
1209 | 1168 | ||
1210 | group_first_block = ext4_group_first_block_no(sb, group); | 1169 | group_first_block = ext4_group_first_block_no(sb, group); |
1211 | group_end_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1); | 1170 | group_end_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1); |
1212 | 1171 | ||
1213 | if (grp_goal < 0) | 1172 | if (grp_goal < 0) |
1214 | start_block = group_first_block; | 1173 | start_block = group_first_block; |
1215 | else | 1174 | else |
1216 | start_block = grp_goal + group_first_block; | 1175 | start_block = grp_goal + group_first_block; |
1217 | 1176 | ||
1218 | size = my_rsv->rsv_goal_size; | 1177 | size = my_rsv->rsv_goal_size; |
1219 | 1178 | ||
1220 | if (!rsv_is_empty(&my_rsv->rsv_window)) { | 1179 | if (!rsv_is_empty(&my_rsv->rsv_window)) { |
1221 | /* | 1180 | /* |
1222 | * if the old reservation is cross group boundary | 1181 | * if the old reservation is cross group boundary |
1223 | * and if the goal is inside the old reservation window, | 1182 | * and if the goal is inside the old reservation window, |
1224 | * we will come here when we just failed to allocate from | 1183 | * we will come here when we just failed to allocate from |
1225 | * the first part of the window. We still have another part | 1184 | * the first part of the window. We still have another part |
1226 | * that belongs to the next group. In this case, there is no | 1185 | * that belongs to the next group. In this case, there is no |
1227 | * point to discard our window and try to allocate a new one | 1186 | * point to discard our window and try to allocate a new one |
1228 | * in this group(which will fail). we should | 1187 | * in this group(which will fail). we should |
1229 | * keep the reservation window, just simply move on. | 1188 | * keep the reservation window, just simply move on. |
1230 | * | 1189 | * |
1231 | * Maybe we could shift the start block of the reservation | 1190 | * Maybe we could shift the start block of the reservation |
1232 | * window to the first block of next group. | 1191 | * window to the first block of next group. |
1233 | */ | 1192 | */ |
1234 | 1193 | ||
1235 | if ((my_rsv->rsv_start <= group_end_block) && | 1194 | if ((my_rsv->rsv_start <= group_end_block) && |
1236 | (my_rsv->rsv_end > group_end_block) && | 1195 | (my_rsv->rsv_end > group_end_block) && |
1237 | (start_block >= my_rsv->rsv_start)) | 1196 | (start_block >= my_rsv->rsv_start)) |
1238 | return -1; | 1197 | return -1; |
1239 | 1198 | ||
1240 | if ((my_rsv->rsv_alloc_hit > | 1199 | if ((my_rsv->rsv_alloc_hit > |
1241 | (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) { | 1200 | (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) { |
1242 | /* | 1201 | /* |
1243 | * if the previously allocation hit ratio is | 1202 | * if the previously allocation hit ratio is |
1244 | * greater than 1/2, then we double the size of | 1203 | * greater than 1/2, then we double the size of |
1245 | * the reservation window the next time, | 1204 | * the reservation window the next time, |
1246 | * otherwise we keep the same size window | 1205 | * otherwise we keep the same size window |
1247 | */ | 1206 | */ |
1248 | size = size * 2; | 1207 | size = size * 2; |
1249 | if (size > EXT4_MAX_RESERVE_BLOCKS) | 1208 | if (size > EXT4_MAX_RESERVE_BLOCKS) |
1250 | size = EXT4_MAX_RESERVE_BLOCKS; | 1209 | size = EXT4_MAX_RESERVE_BLOCKS; |
1251 | my_rsv->rsv_goal_size= size; | 1210 | my_rsv->rsv_goal_size= size; |
1252 | } | 1211 | } |
1253 | } | 1212 | } |
1254 | 1213 | ||
1255 | spin_lock(rsv_lock); | 1214 | spin_lock(rsv_lock); |
1256 | /* | 1215 | /* |
1257 | * shift the search start to the window near the goal block | 1216 | * shift the search start to the window near the goal block |
1258 | */ | 1217 | */ |
1259 | search_head = search_reserve_window(fs_rsv_root, start_block); | 1218 | search_head = search_reserve_window(fs_rsv_root, start_block); |
1260 | 1219 | ||
1261 | /* | 1220 | /* |
1262 | * find_next_reservable_window() simply finds a reservable window | 1221 | * find_next_reservable_window() simply finds a reservable window |
1263 | * inside the given range(start_block, group_end_block). | 1222 | * inside the given range(start_block, group_end_block). |
1264 | * | 1223 | * |
1265 | * To make sure the reservation window has a free bit inside it, we | 1224 | * To make sure the reservation window has a free bit inside it, we |
1266 | * need to check the bitmap after we found a reservable window. | 1225 | * need to check the bitmap after we found a reservable window. |
1267 | */ | 1226 | */ |
1268 | retry: | 1227 | retry: |
1269 | ret = find_next_reservable_window(search_head, my_rsv, sb, | 1228 | ret = find_next_reservable_window(search_head, my_rsv, sb, |
1270 | start_block, group_end_block); | 1229 | start_block, group_end_block); |
1271 | 1230 | ||
1272 | if (ret == -1) { | 1231 | if (ret == -1) { |
1273 | if (!rsv_is_empty(&my_rsv->rsv_window)) | 1232 | if (!rsv_is_empty(&my_rsv->rsv_window)) |
1274 | rsv_window_remove(sb, my_rsv); | 1233 | rsv_window_remove(sb, my_rsv); |
1275 | spin_unlock(rsv_lock); | 1234 | spin_unlock(rsv_lock); |
1276 | return -1; | 1235 | return -1; |
1277 | } | 1236 | } |
1278 | 1237 | ||
1279 | /* | 1238 | /* |
1280 | * On success, find_next_reservable_window() returns the | 1239 | * On success, find_next_reservable_window() returns the |
1281 | * reservation window where there is a reservable space after it. | 1240 | * reservation window where there is a reservable space after it. |
1282 | * Before we reserve this reservable space, we need | 1241 | * Before we reserve this reservable space, we need |
1283 | * to make sure there is at least a free block inside this region. | 1242 | * to make sure there is at least a free block inside this region. |
1284 | * | 1243 | * |
1285 | * searching the first free bit on the block bitmap and copy of | 1244 | * searching the first free bit on the block bitmap and copy of |
1286 | * last committed bitmap alternatively, until we found a allocatable | 1245 | * last committed bitmap alternatively, until we found a allocatable |
1287 | * block. Search start from the start block of the reservable space | 1246 | * block. Search start from the start block of the reservable space |
1288 | * we just found. | 1247 | * we just found. |
1289 | */ | 1248 | */ |
1290 | spin_unlock(rsv_lock); | 1249 | spin_unlock(rsv_lock); |
1291 | first_free_block = bitmap_search_next_usable_block( | 1250 | first_free_block = bitmap_search_next_usable_block( |
1292 | my_rsv->rsv_start - group_first_block, | 1251 | my_rsv->rsv_start - group_first_block, |
1293 | bitmap_bh, group_end_block - group_first_block + 1); | 1252 | bitmap_bh, group_end_block - group_first_block + 1); |
1294 | 1253 | ||
1295 | if (first_free_block < 0) { | 1254 | if (first_free_block < 0) { |
1296 | /* | 1255 | /* |
1297 | * no free block left on the bitmap, no point | 1256 | * no free block left on the bitmap, no point |
1298 | * to reserve the space. return failed. | 1257 | * to reserve the space. return failed. |
1299 | */ | 1258 | */ |
1300 | spin_lock(rsv_lock); | 1259 | spin_lock(rsv_lock); |
1301 | if (!rsv_is_empty(&my_rsv->rsv_window)) | 1260 | if (!rsv_is_empty(&my_rsv->rsv_window)) |
1302 | rsv_window_remove(sb, my_rsv); | 1261 | rsv_window_remove(sb, my_rsv); |
1303 | spin_unlock(rsv_lock); | 1262 | spin_unlock(rsv_lock); |
1304 | return -1; /* failed */ | 1263 | return -1; /* failed */ |
1305 | } | 1264 | } |
1306 | 1265 | ||
1307 | start_block = first_free_block + group_first_block; | 1266 | start_block = first_free_block + group_first_block; |
1308 | /* | 1267 | /* |
1309 | * check if the first free block is within the | 1268 | * check if the first free block is within the |
1310 | * free space we just reserved | 1269 | * free space we just reserved |
1311 | */ | 1270 | */ |
1312 | if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end) | 1271 | if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end) |
1313 | return 0; /* success */ | 1272 | return 0; /* success */ |
1314 | /* | 1273 | /* |
1315 | * if the first free bit we found is out of the reservable space | 1274 | * if the first free bit we found is out of the reservable space |
1316 | * continue search for next reservable space, | 1275 | * continue search for next reservable space, |
1317 | * start from where the free block is, | 1276 | * start from where the free block is, |
1318 | * we also shift the list head to where we stopped last time | 1277 | * we also shift the list head to where we stopped last time |
1319 | */ | 1278 | */ |
1320 | search_head = my_rsv; | 1279 | search_head = my_rsv; |
1321 | spin_lock(rsv_lock); | 1280 | spin_lock(rsv_lock); |
1322 | goto retry; | 1281 | goto retry; |
1323 | } | 1282 | } |
1324 | 1283 | ||
1325 | /** | 1284 | /** |
1326 | * try_to_extend_reservation() | 1285 | * try_to_extend_reservation() |
1327 | * @my_rsv: given reservation window | 1286 | * @my_rsv: given reservation window |
1328 | * @sb: super block | 1287 | * @sb: super block |
1329 | * @size: the delta to extend | 1288 | * @size: the delta to extend |
1330 | * | 1289 | * |
1331 | * Attempt to expand the reservation window large enough to have | 1290 | * Attempt to expand the reservation window large enough to have |
1332 | * required number of free blocks | 1291 | * required number of free blocks |
1333 | * | 1292 | * |
1334 | * Since ext4_try_to_allocate() will always allocate blocks within | 1293 | * Since ext4_try_to_allocate() will always allocate blocks within |
1335 | * the reservation window range, if the window size is too small, | 1294 | * the reservation window range, if the window size is too small, |
1336 | * multiple blocks allocation has to stop at the end of the reservation | 1295 | * multiple blocks allocation has to stop at the end of the reservation |
1337 | * window. To make this more efficient, given the total number of | 1296 | * window. To make this more efficient, given the total number of |
1338 | * blocks needed and the current size of the window, we try to | 1297 | * blocks needed and the current size of the window, we try to |
1339 | * expand the reservation window size if necessary on a best-effort | 1298 | * expand the reservation window size if necessary on a best-effort |
1340 | * basis before ext4_new_blocks() tries to allocate blocks, | 1299 | * basis before ext4_new_blocks() tries to allocate blocks, |
1341 | */ | 1300 | */ |
1342 | static void try_to_extend_reservation(struct ext4_reserve_window_node *my_rsv, | 1301 | static void try_to_extend_reservation(struct ext4_reserve_window_node *my_rsv, |
1343 | struct super_block *sb, int size) | 1302 | struct super_block *sb, int size) |
1344 | { | 1303 | { |
1345 | struct ext4_reserve_window_node *next_rsv; | 1304 | struct ext4_reserve_window_node *next_rsv; |
1346 | struct rb_node *next; | 1305 | struct rb_node *next; |
1347 | spinlock_t *rsv_lock = &EXT4_SB(sb)->s_rsv_window_lock; | 1306 | spinlock_t *rsv_lock = &EXT4_SB(sb)->s_rsv_window_lock; |
1348 | 1307 | ||
1349 | if (!spin_trylock(rsv_lock)) | 1308 | if (!spin_trylock(rsv_lock)) |
1350 | return; | 1309 | return; |
1351 | 1310 | ||
1352 | next = rb_next(&my_rsv->rsv_node); | 1311 | next = rb_next(&my_rsv->rsv_node); |
1353 | 1312 | ||
1354 | if (!next) | 1313 | if (!next) |
1355 | my_rsv->rsv_end += size; | 1314 | my_rsv->rsv_end += size; |
1356 | else { | 1315 | else { |
1357 | next_rsv = rb_entry(next, struct ext4_reserve_window_node, rsv_node); | 1316 | next_rsv = rb_entry(next, struct ext4_reserve_window_node, rsv_node); |
1358 | 1317 | ||
1359 | if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size) | 1318 | if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size) |
1360 | my_rsv->rsv_end += size; | 1319 | my_rsv->rsv_end += size; |
1361 | else | 1320 | else |
1362 | my_rsv->rsv_end = next_rsv->rsv_start - 1; | 1321 | my_rsv->rsv_end = next_rsv->rsv_start - 1; |
1363 | } | 1322 | } |
1364 | spin_unlock(rsv_lock); | 1323 | spin_unlock(rsv_lock); |
1365 | } | 1324 | } |
1366 | 1325 | ||
1367 | /** | 1326 | /** |
1368 | * ext4_try_to_allocate_with_rsv() | 1327 | * ext4_try_to_allocate_with_rsv() |
1369 | * @sb: superblock | 1328 | * @sb: superblock |
1370 | * @handle: handle to this transaction | 1329 | * @handle: handle to this transaction |
1371 | * @group: given allocation block group | 1330 | * @group: given allocation block group |
1372 | * @bitmap_bh: bufferhead holds the block bitmap | 1331 | * @bitmap_bh: bufferhead holds the block bitmap |
1373 | * @grp_goal: given target block within the group | 1332 | * @grp_goal: given target block within the group |
1374 | * @count: target number of blocks to allocate | 1333 | * @count: target number of blocks to allocate |
1375 | * @my_rsv: reservation window | 1334 | * @my_rsv: reservation window |
1376 | * @errp: pointer to store the error code | 1335 | * @errp: pointer to store the error code |
1377 | * | 1336 | * |
1378 | * This is the main function used to allocate a new block and its reservation | 1337 | * This is the main function used to allocate a new block and its reservation |
1379 | * window. | 1338 | * window. |
1380 | * | 1339 | * |
1381 | * Each time when a new block allocation is need, first try to allocate from | 1340 | * Each time when a new block allocation is need, first try to allocate from |
1382 | * its own reservation. If it does not have a reservation window, instead of | 1341 | * its own reservation. If it does not have a reservation window, instead of |
1383 | * looking for a free bit on bitmap first, then look up the reservation list to | 1342 | * looking for a free bit on bitmap first, then look up the reservation list to |
1384 | * see if it is inside somebody else's reservation window, we try to allocate a | 1343 | * see if it is inside somebody else's reservation window, we try to allocate a |
1385 | * reservation window for it starting from the goal first. Then do the block | 1344 | * reservation window for it starting from the goal first. Then do the block |
1386 | * allocation within the reservation window. | 1345 | * allocation within the reservation window. |
1387 | * | 1346 | * |
1388 | * This will avoid keeping on searching the reservation list again and | 1347 | * This will avoid keeping on searching the reservation list again and |
1389 | * again when somebody is looking for a free block (without | 1348 | * again when somebody is looking for a free block (without |
1390 | * reservation), and there are lots of free blocks, but they are all | 1349 | * reservation), and there are lots of free blocks, but they are all |
1391 | * being reserved. | 1350 | * being reserved. |
1392 | * | 1351 | * |
1393 | * We use a red-black tree for the per-filesystem reservation list. | 1352 | * We use a red-black tree for the per-filesystem reservation list. |
1394 | * | 1353 | * |
1395 | */ | 1354 | */ |
1396 | static ext4_grpblk_t | 1355 | static ext4_grpblk_t |
1397 | ext4_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle, | 1356 | ext4_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle, |
1398 | unsigned int group, struct buffer_head *bitmap_bh, | 1357 | unsigned int group, struct buffer_head *bitmap_bh, |
1399 | ext4_grpblk_t grp_goal, | 1358 | ext4_grpblk_t grp_goal, |
1400 | struct ext4_reserve_window_node * my_rsv, | 1359 | struct ext4_reserve_window_node * my_rsv, |
1401 | unsigned long *count, int *errp) | 1360 | unsigned long *count, int *errp) |
1402 | { | 1361 | { |
1403 | ext4_fsblk_t group_first_block, group_last_block; | 1362 | ext4_fsblk_t group_first_block, group_last_block; |
1404 | ext4_grpblk_t ret = 0; | 1363 | ext4_grpblk_t ret = 0; |
1405 | int fatal; | 1364 | int fatal; |
1406 | unsigned long num = *count; | 1365 | unsigned long num = *count; |
1407 | 1366 | ||
1408 | *errp = 0; | 1367 | *errp = 0; |
1409 | 1368 | ||
1410 | /* | 1369 | /* |
1411 | * Make sure we use undo access for the bitmap, because it is critical | 1370 | * Make sure we use undo access for the bitmap, because it is critical |
1412 | * that we do the frozen_data COW on bitmap buffers in all cases even | 1371 | * that we do the frozen_data COW on bitmap buffers in all cases even |
1413 | * if the buffer is in BJ_Forget state in the committing transaction. | 1372 | * if the buffer is in BJ_Forget state in the committing transaction. |
1414 | */ | 1373 | */ |
1415 | BUFFER_TRACE(bitmap_bh, "get undo access for new block"); | 1374 | BUFFER_TRACE(bitmap_bh, "get undo access for new block"); |
1416 | fatal = ext4_journal_get_undo_access(handle, bitmap_bh); | 1375 | fatal = ext4_journal_get_undo_access(handle, bitmap_bh); |
1417 | if (fatal) { | 1376 | if (fatal) { |
1418 | *errp = fatal; | 1377 | *errp = fatal; |
1419 | return -1; | 1378 | return -1; |
1420 | } | 1379 | } |
1421 | 1380 | ||
1422 | /* | 1381 | /* |
1423 | * we don't deal with reservation when | 1382 | * we don't deal with reservation when |
1424 | * filesystem is mounted without reservation | 1383 | * filesystem is mounted without reservation |
1425 | * or the file is not a regular file | 1384 | * or the file is not a regular file |
1426 | * or last attempt to allocate a block with reservation turned on failed | 1385 | * or last attempt to allocate a block with reservation turned on failed |
1427 | */ | 1386 | */ |
1428 | if (my_rsv == NULL ) { | 1387 | if (my_rsv == NULL ) { |
1429 | ret = ext4_try_to_allocate(sb, handle, group, bitmap_bh, | 1388 | ret = ext4_try_to_allocate(sb, handle, group, bitmap_bh, |
1430 | grp_goal, count, NULL); | 1389 | grp_goal, count, NULL); |
1431 | goto out; | 1390 | goto out; |
1432 | } | 1391 | } |
1433 | /* | 1392 | /* |
1434 | * grp_goal is a group relative block number (if there is a goal) | 1393 | * grp_goal is a group relative block number (if there is a goal) |
1435 | * 0 <= grp_goal < EXT4_BLOCKS_PER_GROUP(sb) | 1394 | * 0 <= grp_goal < EXT4_BLOCKS_PER_GROUP(sb) |
1436 | * first block is a filesystem wide block number | 1395 | * first block is a filesystem wide block number |
1437 | * first block is the block number of the first block in this group | 1396 | * first block is the block number of the first block in this group |
1438 | */ | 1397 | */ |
1439 | group_first_block = ext4_group_first_block_no(sb, group); | 1398 | group_first_block = ext4_group_first_block_no(sb, group); |
1440 | group_last_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1); | 1399 | group_last_block = group_first_block + (EXT4_BLOCKS_PER_GROUP(sb) - 1); |
1441 | 1400 | ||
1442 | /* | 1401 | /* |
1443 | * Basically we will allocate a new block from inode's reservation | 1402 | * Basically we will allocate a new block from inode's reservation |
1444 | * window. | 1403 | * window. |
1445 | * | 1404 | * |
1446 | * We need to allocate a new reservation window, if: | 1405 | * We need to allocate a new reservation window, if: |
1447 | * a) inode does not have a reservation window; or | 1406 | * a) inode does not have a reservation window; or |
1448 | * b) last attempt to allocate a block from existing reservation | 1407 | * b) last attempt to allocate a block from existing reservation |
1449 | * failed; or | 1408 | * failed; or |
1450 | * c) we come here with a goal and with a reservation window | 1409 | * c) we come here with a goal and with a reservation window |
1451 | * | 1410 | * |
1452 | * We do not need to allocate a new reservation window if we come here | 1411 | * We do not need to allocate a new reservation window if we come here |
1453 | * at the beginning with a goal and the goal is inside the window, or | 1412 | * at the beginning with a goal and the goal is inside the window, or |
1454 | * we don't have a goal but already have a reservation window. | 1413 | * we don't have a goal but already have a reservation window. |
1455 | * then we could go to allocate from the reservation window directly. | 1414 | * then we could go to allocate from the reservation window directly. |
1456 | */ | 1415 | */ |
1457 | while (1) { | 1416 | while (1) { |
1458 | if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) || | 1417 | if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) || |
1459 | !goal_in_my_reservation(&my_rsv->rsv_window, | 1418 | !goal_in_my_reservation(&my_rsv->rsv_window, |
1460 | grp_goal, group, sb)) { | 1419 | grp_goal, group, sb)) { |
1461 | if (my_rsv->rsv_goal_size < *count) | 1420 | if (my_rsv->rsv_goal_size < *count) |
1462 | my_rsv->rsv_goal_size = *count; | 1421 | my_rsv->rsv_goal_size = *count; |
1463 | ret = alloc_new_reservation(my_rsv, grp_goal, sb, | 1422 | ret = alloc_new_reservation(my_rsv, grp_goal, sb, |
1464 | group, bitmap_bh); | 1423 | group, bitmap_bh); |
1465 | if (ret < 0) | 1424 | if (ret < 0) |
1466 | break; /* failed */ | 1425 | break; /* failed */ |
1467 | 1426 | ||
1468 | if (!goal_in_my_reservation(&my_rsv->rsv_window, | 1427 | if (!goal_in_my_reservation(&my_rsv->rsv_window, |
1469 | grp_goal, group, sb)) | 1428 | grp_goal, group, sb)) |
1470 | grp_goal = -1; | 1429 | grp_goal = -1; |
1471 | } else if (grp_goal >= 0) { | 1430 | } else if (grp_goal >= 0) { |
1472 | int curr = my_rsv->rsv_end - | 1431 | int curr = my_rsv->rsv_end - |
1473 | (grp_goal + group_first_block) + 1; | 1432 | (grp_goal + group_first_block) + 1; |
1474 | 1433 | ||
1475 | if (curr < *count) | 1434 | if (curr < *count) |
1476 | try_to_extend_reservation(my_rsv, sb, | 1435 | try_to_extend_reservation(my_rsv, sb, |
1477 | *count - curr); | 1436 | *count - curr); |
1478 | } | 1437 | } |
1479 | 1438 | ||
1480 | if ((my_rsv->rsv_start > group_last_block) || | 1439 | if ((my_rsv->rsv_start > group_last_block) || |
1481 | (my_rsv->rsv_end < group_first_block)) { | 1440 | (my_rsv->rsv_end < group_first_block)) { |
1482 | rsv_window_dump(&EXT4_SB(sb)->s_rsv_window_root, 1); | 1441 | rsv_window_dump(&EXT4_SB(sb)->s_rsv_window_root, 1); |
1483 | BUG(); | 1442 | BUG(); |
1484 | } | 1443 | } |
1485 | ret = ext4_try_to_allocate(sb, handle, group, bitmap_bh, | 1444 | ret = ext4_try_to_allocate(sb, handle, group, bitmap_bh, |
1486 | grp_goal, &num, &my_rsv->rsv_window); | 1445 | grp_goal, &num, &my_rsv->rsv_window); |
1487 | if (ret >= 0) { | 1446 | if (ret >= 0) { |
1488 | my_rsv->rsv_alloc_hit += num; | 1447 | my_rsv->rsv_alloc_hit += num; |
1489 | *count = num; | 1448 | *count = num; |
1490 | break; /* succeed */ | 1449 | break; /* succeed */ |
1491 | } | 1450 | } |
1492 | num = *count; | 1451 | num = *count; |
1493 | } | 1452 | } |
1494 | out: | 1453 | out: |
1495 | if (ret >= 0) { | 1454 | if (ret >= 0) { |
1496 | BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for " | 1455 | BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for " |
1497 | "bitmap block"); | 1456 | "bitmap block"); |
1498 | fatal = ext4_journal_dirty_metadata(handle, bitmap_bh); | 1457 | fatal = ext4_journal_dirty_metadata(handle, bitmap_bh); |
1499 | if (fatal) { | 1458 | if (fatal) { |
1500 | *errp = fatal; | 1459 | *errp = fatal; |
1501 | return -1; | 1460 | return -1; |
1502 | } | 1461 | } |
1503 | return ret; | 1462 | return ret; |
1504 | } | 1463 | } |
1505 | 1464 | ||
1506 | BUFFER_TRACE(bitmap_bh, "journal_release_buffer"); | 1465 | BUFFER_TRACE(bitmap_bh, "journal_release_buffer"); |
1507 | ext4_journal_release_buffer(handle, bitmap_bh); | 1466 | ext4_journal_release_buffer(handle, bitmap_bh); |
1508 | return ret; | 1467 | return ret; |
1509 | } | 1468 | } |
1510 | 1469 | ||
1511 | /** | 1470 | /** |
1512 | * ext4_has_free_blocks() | 1471 | * ext4_has_free_blocks() |
1513 | * @sbi: in-core super block structure. | 1472 | * @sbi: in-core super block structure. |
1514 | * | 1473 | * |
1515 | * Check if filesystem has at least 1 free block available for allocation. | 1474 | * Check if filesystem has at least 1 free block available for allocation. |
1516 | */ | 1475 | */ |
1517 | static int ext4_has_free_blocks(struct ext4_sb_info *sbi) | 1476 | static int ext4_has_free_blocks(struct ext4_sb_info *sbi) |
1518 | { | 1477 | { |
1519 | ext4_fsblk_t free_blocks, root_blocks; | 1478 | ext4_fsblk_t free_blocks, root_blocks; |
1520 | 1479 | ||
1521 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); | 1480 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
1522 | root_blocks = ext4_r_blocks_count(sbi->s_es); | 1481 | root_blocks = ext4_r_blocks_count(sbi->s_es); |
1523 | if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) && | 1482 | if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) && |
1524 | sbi->s_resuid != current->fsuid && | 1483 | sbi->s_resuid != current->fsuid && |
1525 | (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) { | 1484 | (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) { |
1526 | return 0; | 1485 | return 0; |
1527 | } | 1486 | } |
1528 | return 1; | 1487 | return 1; |
1529 | } | 1488 | } |
1530 | 1489 | ||
1531 | /** | 1490 | /** |
1532 | * ext4_should_retry_alloc() | 1491 | * ext4_should_retry_alloc() |
1533 | * @sb: super block | 1492 | * @sb: super block |
1534 | * @retries number of attemps has been made | 1493 | * @retries number of attemps has been made |
1535 | * | 1494 | * |
1536 | * ext4_should_retry_alloc() is called when ENOSPC is returned, and if | 1495 | * ext4_should_retry_alloc() is called when ENOSPC is returned, and if |
1537 | * it is profitable to retry the operation, this function will wait | 1496 | * it is profitable to retry the operation, this function will wait |
1538 | * for the current or commiting transaction to complete, and then | 1497 | * for the current or commiting transaction to complete, and then |
1539 | * return TRUE. | 1498 | * return TRUE. |
1540 | * | 1499 | * |
1541 | * if the total number of retries exceed three times, return FALSE. | 1500 | * if the total number of retries exceed three times, return FALSE. |
1542 | */ | 1501 | */ |
1543 | int ext4_should_retry_alloc(struct super_block *sb, int *retries) | 1502 | int ext4_should_retry_alloc(struct super_block *sb, int *retries) |
1544 | { | 1503 | { |
1545 | if (!ext4_has_free_blocks(EXT4_SB(sb)) || (*retries)++ > 3) | 1504 | if (!ext4_has_free_blocks(EXT4_SB(sb)) || (*retries)++ > 3) |
1546 | return 0; | 1505 | return 0; |
1547 | 1506 | ||
1548 | jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id); | 1507 | jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id); |
1549 | 1508 | ||
1550 | return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal); | 1509 | return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal); |
1551 | } | 1510 | } |
1552 | 1511 | ||
1553 | /** | 1512 | /** |
1554 | * ext4_new_blocks() -- core block(s) allocation function | 1513 | * ext4_new_blocks() -- core block(s) allocation function |
1555 | * @handle: handle to this transaction | 1514 | * @handle: handle to this transaction |
1556 | * @inode: file inode | 1515 | * @inode: file inode |
1557 | * @goal: given target block(filesystem wide) | 1516 | * @goal: given target block(filesystem wide) |
1558 | * @count: target number of blocks to allocate | 1517 | * @count: target number of blocks to allocate |
1559 | * @errp: error code | 1518 | * @errp: error code |
1560 | * | 1519 | * |
1561 | * ext4_new_blocks uses a goal block to assist allocation. It tries to | 1520 | * ext4_new_blocks uses a goal block to assist allocation. It tries to |
1562 | * allocate block(s) from the block group contains the goal block first. If that | 1521 | * allocate block(s) from the block group contains the goal block first. If that |
1563 | * fails, it will try to allocate block(s) from other block groups without | 1522 | * fails, it will try to allocate block(s) from other block groups without |
1564 | * any specific goal block. | 1523 | * any specific goal block. |
1565 | * | 1524 | * |
1566 | */ | 1525 | */ |
1567 | ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode, | 1526 | ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode, |
1568 | ext4_fsblk_t goal, unsigned long *count, int *errp) | 1527 | ext4_fsblk_t goal, unsigned long *count, int *errp) |
1569 | { | 1528 | { |
1570 | struct buffer_head *bitmap_bh = NULL; | 1529 | struct buffer_head *bitmap_bh = NULL; |
1571 | struct buffer_head *gdp_bh; | 1530 | struct buffer_head *gdp_bh; |
1572 | unsigned long group_no; | 1531 | unsigned long group_no; |
1573 | int goal_group; | 1532 | int goal_group; |
1574 | ext4_grpblk_t grp_target_blk; /* blockgroup relative goal block */ | 1533 | ext4_grpblk_t grp_target_blk; /* blockgroup relative goal block */ |
1575 | ext4_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/ | 1534 | ext4_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/ |
1576 | ext4_fsblk_t ret_block; /* filesyetem-wide allocated block */ | 1535 | ext4_fsblk_t ret_block; /* filesyetem-wide allocated block */ |
1577 | int bgi; /* blockgroup iteration index */ | 1536 | int bgi; /* blockgroup iteration index */ |
1578 | int fatal = 0, err; | 1537 | int fatal = 0, err; |
1579 | int performed_allocation = 0; | 1538 | int performed_allocation = 0; |
1580 | ext4_grpblk_t free_blocks; /* number of free blocks in a group */ | 1539 | ext4_grpblk_t free_blocks; /* number of free blocks in a group */ |
1581 | struct super_block *sb; | 1540 | struct super_block *sb; |
1582 | struct ext4_group_desc *gdp; | 1541 | struct ext4_group_desc *gdp; |
1583 | struct ext4_super_block *es; | 1542 | struct ext4_super_block *es; |
1584 | struct ext4_sb_info *sbi; | 1543 | struct ext4_sb_info *sbi; |
1585 | struct ext4_reserve_window_node *my_rsv = NULL; | 1544 | struct ext4_reserve_window_node *my_rsv = NULL; |
1586 | struct ext4_block_alloc_info *block_i; | 1545 | struct ext4_block_alloc_info *block_i; |
1587 | unsigned short windowsz = 0; | 1546 | unsigned short windowsz = 0; |
1588 | #ifdef EXT4FS_DEBUG | 1547 | #ifdef EXT4FS_DEBUG |
1589 | static int goal_hits, goal_attempts; | 1548 | static int goal_hits, goal_attempts; |
1590 | #endif | 1549 | #endif |
1591 | unsigned long ngroups; | 1550 | unsigned long ngroups; |
1592 | unsigned long num = *count; | 1551 | unsigned long num = *count; |
1593 | 1552 | ||
1594 | *errp = -ENOSPC; | 1553 | *errp = -ENOSPC; |
1595 | sb = inode->i_sb; | 1554 | sb = inode->i_sb; |
1596 | if (!sb) { | 1555 | if (!sb) { |
1597 | printk("ext4_new_block: nonexistent device"); | 1556 | printk("ext4_new_block: nonexistent device"); |
1598 | return 0; | 1557 | return 0; |
1599 | } | 1558 | } |
1600 | 1559 | ||
1601 | /* | 1560 | /* |
1602 | * Check quota for allocation of this block. | 1561 | * Check quota for allocation of this block. |
1603 | */ | 1562 | */ |
1604 | if (DQUOT_ALLOC_BLOCK(inode, num)) { | 1563 | if (DQUOT_ALLOC_BLOCK(inode, num)) { |
1605 | *errp = -EDQUOT; | 1564 | *errp = -EDQUOT; |
1606 | return 0; | 1565 | return 0; |
1607 | } | 1566 | } |
1608 | 1567 | ||
1609 | sbi = EXT4_SB(sb); | 1568 | sbi = EXT4_SB(sb); |
1610 | es = EXT4_SB(sb)->s_es; | 1569 | es = EXT4_SB(sb)->s_es; |
1611 | ext4_debug("goal=%lu.\n", goal); | 1570 | ext4_debug("goal=%lu.\n", goal); |
1612 | /* | 1571 | /* |
1613 | * Allocate a block from reservation only when | 1572 | * Allocate a block from reservation only when |
1614 | * filesystem is mounted with reservation(default,-o reservation), and | 1573 | * filesystem is mounted with reservation(default,-o reservation), and |
1615 | * it's a regular file, and | 1574 | * it's a regular file, and |
1616 | * the desired window size is greater than 0 (One could use ioctl | 1575 | * the desired window size is greater than 0 (One could use ioctl |
1617 | * command EXT4_IOC_SETRSVSZ to set the window size to 0 to turn off | 1576 | * command EXT4_IOC_SETRSVSZ to set the window size to 0 to turn off |
1618 | * reservation on that particular file) | 1577 | * reservation on that particular file) |
1619 | */ | 1578 | */ |
1620 | block_i = EXT4_I(inode)->i_block_alloc_info; | 1579 | block_i = EXT4_I(inode)->i_block_alloc_info; |
1621 | if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0)) | 1580 | if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0)) |
1622 | my_rsv = &block_i->rsv_window_node; | 1581 | my_rsv = &block_i->rsv_window_node; |
1623 | 1582 | ||
1624 | if (!ext4_has_free_blocks(sbi)) { | 1583 | if (!ext4_has_free_blocks(sbi)) { |
1625 | *errp = -ENOSPC; | 1584 | *errp = -ENOSPC; |
1626 | goto out; | 1585 | goto out; |
1627 | } | 1586 | } |
1628 | 1587 | ||
1629 | /* | 1588 | /* |
1630 | * First, test whether the goal block is free. | 1589 | * First, test whether the goal block is free. |
1631 | */ | 1590 | */ |
1632 | if (goal < le32_to_cpu(es->s_first_data_block) || | 1591 | if (goal < le32_to_cpu(es->s_first_data_block) || |
1633 | goal >= ext4_blocks_count(es)) | 1592 | goal >= ext4_blocks_count(es)) |
1634 | goal = le32_to_cpu(es->s_first_data_block); | 1593 | goal = le32_to_cpu(es->s_first_data_block); |
1635 | ext4_get_group_no_and_offset(sb, goal, &group_no, &grp_target_blk); | 1594 | ext4_get_group_no_and_offset(sb, goal, &group_no, &grp_target_blk); |
1636 | goal_group = group_no; | 1595 | goal_group = group_no; |
1637 | retry_alloc: | 1596 | retry_alloc: |
1638 | gdp = ext4_get_group_desc(sb, group_no, &gdp_bh); | 1597 | gdp = ext4_get_group_desc(sb, group_no, &gdp_bh); |
1639 | if (!gdp) | 1598 | if (!gdp) |
1640 | goto io_error; | 1599 | goto io_error; |
1641 | 1600 | ||
1642 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); | 1601 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); |
1643 | /* | 1602 | /* |
1644 | * if there is not enough free blocks to make a new resevation | 1603 | * if there is not enough free blocks to make a new resevation |
1645 | * turn off reservation for this allocation | 1604 | * turn off reservation for this allocation |
1646 | */ | 1605 | */ |
1647 | if (my_rsv && (free_blocks < windowsz) | 1606 | if (my_rsv && (free_blocks < windowsz) |
1648 | && (rsv_is_empty(&my_rsv->rsv_window))) | 1607 | && (rsv_is_empty(&my_rsv->rsv_window))) |
1649 | my_rsv = NULL; | 1608 | my_rsv = NULL; |
1650 | 1609 | ||
1651 | if (free_blocks > 0) { | 1610 | if (free_blocks > 0) { |
1652 | bitmap_bh = read_block_bitmap(sb, group_no); | 1611 | bitmap_bh = read_block_bitmap(sb, group_no); |
1653 | if (!bitmap_bh) | 1612 | if (!bitmap_bh) |
1654 | goto io_error; | 1613 | goto io_error; |
1655 | grp_alloc_blk = ext4_try_to_allocate_with_rsv(sb, handle, | 1614 | grp_alloc_blk = ext4_try_to_allocate_with_rsv(sb, handle, |
1656 | group_no, bitmap_bh, grp_target_blk, | 1615 | group_no, bitmap_bh, grp_target_blk, |
1657 | my_rsv, &num, &fatal); | 1616 | my_rsv, &num, &fatal); |
1658 | if (fatal) | 1617 | if (fatal) |
1659 | goto out; | 1618 | goto out; |
1660 | if (grp_alloc_blk >= 0) | 1619 | if (grp_alloc_blk >= 0) |
1661 | goto allocated; | 1620 | goto allocated; |
1662 | } | 1621 | } |
1663 | 1622 | ||
1664 | ngroups = EXT4_SB(sb)->s_groups_count; | 1623 | ngroups = EXT4_SB(sb)->s_groups_count; |
1665 | smp_rmb(); | 1624 | smp_rmb(); |
1666 | 1625 | ||
1667 | /* | 1626 | /* |
1668 | * Now search the rest of the groups. We assume that | 1627 | * Now search the rest of the groups. We assume that |
1669 | * i and gdp correctly point to the last group visited. | 1628 | * i and gdp correctly point to the last group visited. |
1670 | */ | 1629 | */ |
1671 | for (bgi = 0; bgi < ngroups; bgi++) { | 1630 | for (bgi = 0; bgi < ngroups; bgi++) { |
1672 | group_no++; | 1631 | group_no++; |
1673 | if (group_no >= ngroups) | 1632 | if (group_no >= ngroups) |
1674 | group_no = 0; | 1633 | group_no = 0; |
1675 | gdp = ext4_get_group_desc(sb, group_no, &gdp_bh); | 1634 | gdp = ext4_get_group_desc(sb, group_no, &gdp_bh); |
1676 | if (!gdp) | 1635 | if (!gdp) |
1677 | goto io_error; | 1636 | goto io_error; |
1678 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); | 1637 | free_blocks = le16_to_cpu(gdp->bg_free_blocks_count); |
1679 | /* | 1638 | /* |
1680 | * skip this group if the number of | 1639 | * skip this group if the number of |
1681 | * free blocks is less than half of the reservation | 1640 | * free blocks is less than half of the reservation |
1682 | * window size. | 1641 | * window size. |
1683 | */ | 1642 | */ |
1684 | if (free_blocks <= (windowsz/2)) | 1643 | if (free_blocks <= (windowsz/2)) |
1685 | continue; | 1644 | continue; |
1686 | 1645 | ||
1687 | brelse(bitmap_bh); | 1646 | brelse(bitmap_bh); |
1688 | bitmap_bh = read_block_bitmap(sb, group_no); | 1647 | bitmap_bh = read_block_bitmap(sb, group_no); |
1689 | if (!bitmap_bh) | 1648 | if (!bitmap_bh) |
1690 | goto io_error; | 1649 | goto io_error; |
1691 | /* | 1650 | /* |
1692 | * try to allocate block(s) from this group, without a goal(-1). | 1651 | * try to allocate block(s) from this group, without a goal(-1). |
1693 | */ | 1652 | */ |
1694 | grp_alloc_blk = ext4_try_to_allocate_with_rsv(sb, handle, | 1653 | grp_alloc_blk = ext4_try_to_allocate_with_rsv(sb, handle, |
1695 | group_no, bitmap_bh, -1, my_rsv, | 1654 | group_no, bitmap_bh, -1, my_rsv, |
1696 | &num, &fatal); | 1655 | &num, &fatal); |
1697 | if (fatal) | 1656 | if (fatal) |
1698 | goto out; | 1657 | goto out; |
1699 | if (grp_alloc_blk >= 0) | 1658 | if (grp_alloc_blk >= 0) |
1700 | goto allocated; | 1659 | goto allocated; |
1701 | } | 1660 | } |
1702 | /* | 1661 | /* |
1703 | * We may end up a bogus ealier ENOSPC error due to | 1662 | * We may end up a bogus ealier ENOSPC error due to |
1704 | * filesystem is "full" of reservations, but | 1663 | * filesystem is "full" of reservations, but |
1705 | * there maybe indeed free blocks avaliable on disk | 1664 | * there maybe indeed free blocks avaliable on disk |
1706 | * In this case, we just forget about the reservations | 1665 | * In this case, we just forget about the reservations |
1707 | * just do block allocation as without reservations. | 1666 | * just do block allocation as without reservations. |
1708 | */ | 1667 | */ |
1709 | if (my_rsv) { | 1668 | if (my_rsv) { |
1710 | my_rsv = NULL; | 1669 | my_rsv = NULL; |
1711 | windowsz = 0; | 1670 | windowsz = 0; |
1712 | group_no = goal_group; | 1671 | group_no = goal_group; |
1713 | goto retry_alloc; | 1672 | goto retry_alloc; |
1714 | } | 1673 | } |
1715 | /* No space left on the device */ | 1674 | /* No space left on the device */ |
1716 | *errp = -ENOSPC; | 1675 | *errp = -ENOSPC; |
1717 | goto out; | 1676 | goto out; |
1718 | 1677 | ||
1719 | allocated: | 1678 | allocated: |
1720 | 1679 | ||
1721 | ext4_debug("using block group %d(%d)\n", | 1680 | ext4_debug("using block group %d(%d)\n", |
1722 | group_no, gdp->bg_free_blocks_count); | 1681 | group_no, gdp->bg_free_blocks_count); |
1723 | 1682 | ||
1724 | BUFFER_TRACE(gdp_bh, "get_write_access"); | 1683 | BUFFER_TRACE(gdp_bh, "get_write_access"); |
1725 | fatal = ext4_journal_get_write_access(handle, gdp_bh); | 1684 | fatal = ext4_journal_get_write_access(handle, gdp_bh); |
1726 | if (fatal) | 1685 | if (fatal) |
1727 | goto out; | 1686 | goto out; |
1728 | 1687 | ||
1729 | ret_block = grp_alloc_blk + ext4_group_first_block_no(sb, group_no); | 1688 | ret_block = grp_alloc_blk + ext4_group_first_block_no(sb, group_no); |
1730 | 1689 | ||
1731 | if (in_range(ext4_block_bitmap(sb, gdp), ret_block, num) || | 1690 | if (in_range(ext4_block_bitmap(sb, gdp), ret_block, num) || |
1732 | in_range(ext4_inode_bitmap(sb, gdp), ret_block, num) || | 1691 | in_range(ext4_inode_bitmap(sb, gdp), ret_block, num) || |
1733 | in_range(ret_block, ext4_inode_table(sb, gdp), | 1692 | in_range(ret_block, ext4_inode_table(sb, gdp), |
1734 | EXT4_SB(sb)->s_itb_per_group) || | 1693 | EXT4_SB(sb)->s_itb_per_group) || |
1735 | in_range(ret_block + num - 1, ext4_inode_table(sb, gdp), | 1694 | in_range(ret_block + num - 1, ext4_inode_table(sb, gdp), |
1736 | EXT4_SB(sb)->s_itb_per_group)) | 1695 | EXT4_SB(sb)->s_itb_per_group)) |
1737 | ext4_error(sb, "ext4_new_block", | 1696 | ext4_error(sb, "ext4_new_block", |
1738 | "Allocating block in system zone - " | 1697 | "Allocating block in system zone - " |
1739 | "blocks from %llu, length %lu", | 1698 | "blocks from %llu, length %lu", |
1740 | ret_block, num); | 1699 | ret_block, num); |
1741 | 1700 | ||
1742 | performed_allocation = 1; | 1701 | performed_allocation = 1; |
1743 | 1702 | ||
1744 | #ifdef CONFIG_JBD2_DEBUG | 1703 | #ifdef CONFIG_JBD2_DEBUG |
1745 | { | 1704 | { |
1746 | struct buffer_head *debug_bh; | 1705 | struct buffer_head *debug_bh; |
1747 | 1706 | ||
1748 | /* Record bitmap buffer state in the newly allocated block */ | 1707 | /* Record bitmap buffer state in the newly allocated block */ |
1749 | debug_bh = sb_find_get_block(sb, ret_block); | 1708 | debug_bh = sb_find_get_block(sb, ret_block); |
1750 | if (debug_bh) { | 1709 | if (debug_bh) { |
1751 | BUFFER_TRACE(debug_bh, "state when allocated"); | 1710 | BUFFER_TRACE(debug_bh, "state when allocated"); |
1752 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state"); | 1711 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state"); |
1753 | brelse(debug_bh); | 1712 | brelse(debug_bh); |
1754 | } | 1713 | } |
1755 | } | 1714 | } |
1756 | jbd_lock_bh_state(bitmap_bh); | 1715 | jbd_lock_bh_state(bitmap_bh); |
1757 | spin_lock(sb_bgl_lock(sbi, group_no)); | 1716 | spin_lock(sb_bgl_lock(sbi, group_no)); |
1758 | if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) { | 1717 | if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) { |
1759 | int i; | 1718 | int i; |
1760 | 1719 | ||
1761 | for (i = 0; i < num; i++) { | 1720 | for (i = 0; i < num; i++) { |
1762 | if (ext4_test_bit(grp_alloc_blk+i, | 1721 | if (ext4_test_bit(grp_alloc_blk+i, |
1763 | bh2jh(bitmap_bh)->b_committed_data)) { | 1722 | bh2jh(bitmap_bh)->b_committed_data)) { |
1764 | printk("%s: block was unexpectedly set in " | 1723 | printk("%s: block was unexpectedly set in " |
1765 | "b_committed_data\n", __FUNCTION__); | 1724 | "b_committed_data\n", __FUNCTION__); |
1766 | } | 1725 | } |
1767 | } | 1726 | } |
1768 | } | 1727 | } |
1769 | ext4_debug("found bit %d\n", grp_alloc_blk); | 1728 | ext4_debug("found bit %d\n", grp_alloc_blk); |
1770 | spin_unlock(sb_bgl_lock(sbi, group_no)); | 1729 | spin_unlock(sb_bgl_lock(sbi, group_no)); |
1771 | jbd_unlock_bh_state(bitmap_bh); | 1730 | jbd_unlock_bh_state(bitmap_bh); |
1772 | #endif | 1731 | #endif |
1773 | 1732 | ||
1774 | if (ret_block + num - 1 >= ext4_blocks_count(es)) { | 1733 | if (ret_block + num - 1 >= ext4_blocks_count(es)) { |
1775 | ext4_error(sb, "ext4_new_block", | 1734 | ext4_error(sb, "ext4_new_block", |
1776 | "block(%llu) >= blocks count(%llu) - " | 1735 | "block(%llu) >= blocks count(%llu) - " |
1777 | "block_group = %lu, es == %p ", ret_block, | 1736 | "block_group = %lu, es == %p ", ret_block, |
1778 | ext4_blocks_count(es), group_no, es); | 1737 | ext4_blocks_count(es), group_no, es); |
1779 | goto out; | 1738 | goto out; |
1780 | } | 1739 | } |
1781 | 1740 | ||
1782 | /* | 1741 | /* |
1783 | * It is up to the caller to add the new buffer to a journal | 1742 | * It is up to the caller to add the new buffer to a journal |
1784 | * list of some description. We don't know in advance whether | 1743 | * list of some description. We don't know in advance whether |
1785 | * the caller wants to use it as metadata or data. | 1744 | * the caller wants to use it as metadata or data. |
1786 | */ | 1745 | */ |
1787 | ext4_debug("allocating block %lu. Goal hits %d of %d.\n", | 1746 | ext4_debug("allocating block %lu. Goal hits %d of %d.\n", |
1788 | ret_block, goal_hits, goal_attempts); | 1747 | ret_block, goal_hits, goal_attempts); |
1789 | 1748 | ||
1790 | spin_lock(sb_bgl_lock(sbi, group_no)); | 1749 | spin_lock(sb_bgl_lock(sbi, group_no)); |
1791 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) | 1750 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) |
1792 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); | 1751 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
1793 | gdp->bg_free_blocks_count = | 1752 | gdp->bg_free_blocks_count = |
1794 | cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)-num); | 1753 | cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)-num); |
1795 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group_no, gdp); | 1754 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group_no, gdp); |
1796 | spin_unlock(sb_bgl_lock(sbi, group_no)); | 1755 | spin_unlock(sb_bgl_lock(sbi, group_no)); |
1797 | percpu_counter_sub(&sbi->s_freeblocks_counter, num); | 1756 | percpu_counter_sub(&sbi->s_freeblocks_counter, num); |
1798 | 1757 | ||
1799 | BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor"); | 1758 | BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor"); |
1800 | err = ext4_journal_dirty_metadata(handle, gdp_bh); | 1759 | err = ext4_journal_dirty_metadata(handle, gdp_bh); |
1801 | if (!fatal) | 1760 | if (!fatal) |
1802 | fatal = err; | 1761 | fatal = err; |
1803 | 1762 | ||
1804 | sb->s_dirt = 1; | 1763 | sb->s_dirt = 1; |
1805 | if (fatal) | 1764 | if (fatal) |
1806 | goto out; | 1765 | goto out; |
1807 | 1766 | ||
1808 | *errp = 0; | 1767 | *errp = 0; |
1809 | brelse(bitmap_bh); | 1768 | brelse(bitmap_bh); |
1810 | DQUOT_FREE_BLOCK(inode, *count-num); | 1769 | DQUOT_FREE_BLOCK(inode, *count-num); |
1811 | *count = num; | 1770 | *count = num; |
1812 | return ret_block; | 1771 | return ret_block; |
1813 | 1772 | ||
1814 | io_error: | 1773 | io_error: |
1815 | *errp = -EIO; | 1774 | *errp = -EIO; |
1816 | out: | 1775 | out: |
1817 | if (fatal) { | 1776 | if (fatal) { |
1818 | *errp = fatal; | 1777 | *errp = fatal; |
1819 | ext4_std_error(sb, fatal); | 1778 | ext4_std_error(sb, fatal); |
1820 | } | 1779 | } |
1821 | /* | 1780 | /* |
1822 | * Undo the block allocation | 1781 | * Undo the block allocation |
1823 | */ | 1782 | */ |
1824 | if (!performed_allocation) | 1783 | if (!performed_allocation) |
1825 | DQUOT_FREE_BLOCK(inode, *count); | 1784 | DQUOT_FREE_BLOCK(inode, *count); |
1826 | brelse(bitmap_bh); | 1785 | brelse(bitmap_bh); |
1827 | return 0; | 1786 | return 0; |
1828 | } | 1787 | } |
1829 | 1788 | ||
1830 | ext4_fsblk_t ext4_new_block(handle_t *handle, struct inode *inode, | 1789 | ext4_fsblk_t ext4_new_block(handle_t *handle, struct inode *inode, |
1831 | ext4_fsblk_t goal, int *errp) | 1790 | ext4_fsblk_t goal, int *errp) |
1832 | { | 1791 | { |
1833 | unsigned long count = 1; | 1792 | unsigned long count = 1; |
1834 | 1793 | ||
1835 | return ext4_new_blocks(handle, inode, goal, &count, errp); | 1794 | return ext4_new_blocks(handle, inode, goal, &count, errp); |
1836 | } | 1795 | } |
1837 | 1796 | ||
1838 | /** | 1797 | /** |
1839 | * ext4_count_free_blocks() -- count filesystem free blocks | 1798 | * ext4_count_free_blocks() -- count filesystem free blocks |
1840 | * @sb: superblock | 1799 | * @sb: superblock |
1841 | * | 1800 | * |
1842 | * Adds up the number of free blocks from each block group. | 1801 | * Adds up the number of free blocks from each block group. |
1843 | */ | 1802 | */ |
1844 | ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb) | 1803 | ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb) |
1845 | { | 1804 | { |
1846 | ext4_fsblk_t desc_count; | 1805 | ext4_fsblk_t desc_count; |
1847 | struct ext4_group_desc *gdp; | 1806 | struct ext4_group_desc *gdp; |
1848 | int i; | 1807 | int i; |
1849 | unsigned long ngroups = EXT4_SB(sb)->s_groups_count; | 1808 | unsigned long ngroups = EXT4_SB(sb)->s_groups_count; |
1850 | #ifdef EXT4FS_DEBUG | 1809 | #ifdef EXT4FS_DEBUG |
1851 | struct ext4_super_block *es; | 1810 | struct ext4_super_block *es; |
1852 | ext4_fsblk_t bitmap_count; | 1811 | ext4_fsblk_t bitmap_count; |
1853 | unsigned long x; | 1812 | unsigned long x; |
1854 | struct buffer_head *bitmap_bh = NULL; | 1813 | struct buffer_head *bitmap_bh = NULL; |
1855 | 1814 | ||
1856 | es = EXT4_SB(sb)->s_es; | 1815 | es = EXT4_SB(sb)->s_es; |
1857 | desc_count = 0; | 1816 | desc_count = 0; |
1858 | bitmap_count = 0; | 1817 | bitmap_count = 0; |
1859 | gdp = NULL; | 1818 | gdp = NULL; |
1860 | 1819 | ||
1861 | smp_rmb(); | 1820 | smp_rmb(); |
1862 | for (i = 0; i < ngroups; i++) { | 1821 | for (i = 0; i < ngroups; i++) { |
1863 | gdp = ext4_get_group_desc(sb, i, NULL); | 1822 | gdp = ext4_get_group_desc(sb, i, NULL); |
1864 | if (!gdp) | 1823 | if (!gdp) |
1865 | continue; | 1824 | continue; |
1866 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); | 1825 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); |
1867 | brelse(bitmap_bh); | 1826 | brelse(bitmap_bh); |
1868 | bitmap_bh = read_block_bitmap(sb, i); | 1827 | bitmap_bh = read_block_bitmap(sb, i); |
1869 | if (bitmap_bh == NULL) | 1828 | if (bitmap_bh == NULL) |
1870 | continue; | 1829 | continue; |
1871 | 1830 | ||
1872 | x = ext4_count_free(bitmap_bh, sb->s_blocksize); | 1831 | x = ext4_count_free(bitmap_bh, sb->s_blocksize); |
1873 | printk("group %d: stored = %d, counted = %lu\n", | 1832 | printk("group %d: stored = %d, counted = %lu\n", |
1874 | i, le16_to_cpu(gdp->bg_free_blocks_count), x); | 1833 | i, le16_to_cpu(gdp->bg_free_blocks_count), x); |
1875 | bitmap_count += x; | 1834 | bitmap_count += x; |
1876 | } | 1835 | } |
1877 | brelse(bitmap_bh); | 1836 | brelse(bitmap_bh); |
1878 | printk("ext4_count_free_blocks: stored = %llu" | 1837 | printk("ext4_count_free_blocks: stored = %llu" |
1879 | ", computed = %llu, %llu\n", | 1838 | ", computed = %llu, %llu\n", |
1880 | EXT4_FREE_BLOCKS_COUNT(es), | 1839 | EXT4_FREE_BLOCKS_COUNT(es), |
1881 | desc_count, bitmap_count); | 1840 | desc_count, bitmap_count); |
1882 | return bitmap_count; | 1841 | return bitmap_count; |
1883 | #else | 1842 | #else |
1884 | desc_count = 0; | 1843 | desc_count = 0; |
1885 | smp_rmb(); | 1844 | smp_rmb(); |
1886 | for (i = 0; i < ngroups; i++) { | 1845 | for (i = 0; i < ngroups; i++) { |
1887 | gdp = ext4_get_group_desc(sb, i, NULL); | 1846 | gdp = ext4_get_group_desc(sb, i, NULL); |
1888 | if (!gdp) | 1847 | if (!gdp) |
1889 | continue; | 1848 | continue; |
1890 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); | 1849 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); |
1891 | } | 1850 | } |
1892 | 1851 | ||
1893 | return desc_count; | 1852 | return desc_count; |
1894 | #endif | 1853 | #endif |
1895 | } | 1854 | } |
1896 | 1855 | ||
1897 | static inline int test_root(int a, int b) | 1856 | static inline int test_root(int a, int b) |
1898 | { | 1857 | { |
1899 | int num = b; | 1858 | int num = b; |
1900 | 1859 | ||
1901 | while (a > num) | 1860 | while (a > num) |
1902 | num *= b; | 1861 | num *= b; |
1903 | return num == a; | 1862 | return num == a; |
1904 | } | 1863 | } |
1905 | 1864 | ||
1906 | static int ext4_group_sparse(int group) | 1865 | static int ext4_group_sparse(int group) |
1907 | { | 1866 | { |
1908 | if (group <= 1) | 1867 | if (group <= 1) |
1909 | return 1; | 1868 | return 1; |
1910 | if (!(group & 1)) | 1869 | if (!(group & 1)) |
1911 | return 0; | 1870 | return 0; |
1912 | return (test_root(group, 7) || test_root(group, 5) || | 1871 | return (test_root(group, 7) || test_root(group, 5) || |
1913 | test_root(group, 3)); | 1872 | test_root(group, 3)); |
1914 | } | 1873 | } |
1915 | 1874 | ||
1916 | /** | 1875 | /** |
1917 | * ext4_bg_has_super - number of blocks used by the superblock in group | 1876 | * ext4_bg_has_super - number of blocks used by the superblock in group |
1918 | * @sb: superblock for filesystem | 1877 | * @sb: superblock for filesystem |
1919 | * @group: group number to check | 1878 | * @group: group number to check |
1920 | * | 1879 | * |
1921 | * Return the number of blocks used by the superblock (primary or backup) | 1880 | * Return the number of blocks used by the superblock (primary or backup) |
1922 | * in this group. Currently this will be only 0 or 1. | 1881 | * in this group. Currently this will be only 0 or 1. |
1923 | */ | 1882 | */ |
1924 | int ext4_bg_has_super(struct super_block *sb, int group) | 1883 | int ext4_bg_has_super(struct super_block *sb, int group) |
1925 | { | 1884 | { |
1926 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | 1885 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
1927 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) && | 1886 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) && |
1928 | !ext4_group_sparse(group)) | 1887 | !ext4_group_sparse(group)) |
1929 | return 0; | 1888 | return 0; |
1930 | return 1; | 1889 | return 1; |
1931 | } | 1890 | } |
1932 | 1891 | ||
1933 | static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb, int group) | 1892 | static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb, int group) |
1934 | { | 1893 | { |
1935 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); | 1894 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); |
1936 | unsigned long first = metagroup * EXT4_DESC_PER_BLOCK(sb); | 1895 | unsigned long first = metagroup * EXT4_DESC_PER_BLOCK(sb); |
1937 | unsigned long last = first + EXT4_DESC_PER_BLOCK(sb) - 1; | 1896 | unsigned long last = first + EXT4_DESC_PER_BLOCK(sb) - 1; |
1938 | 1897 | ||
1939 | if (group == first || group == first + 1 || group == last) | 1898 | if (group == first || group == first + 1 || group == last) |
1940 | return 1; | 1899 | return 1; |
1941 | return 0; | 1900 | return 0; |
1942 | } | 1901 | } |
1943 | 1902 | ||
1944 | static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb, int group) | 1903 | static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb, int group) |
1945 | { | 1904 | { |
1946 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | 1905 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
1947 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) && | 1906 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) && |
1948 | !ext4_group_sparse(group)) | 1907 | !ext4_group_sparse(group)) |
1949 | return 0; | 1908 | return 0; |
1950 | return EXT4_SB(sb)->s_gdb_count; | 1909 | return EXT4_SB(sb)->s_gdb_count; |
1951 | } | 1910 | } |
1952 | 1911 | ||
1953 | /** | 1912 | /** |
1954 | * ext4_bg_num_gdb - number of blocks used by the group table in group | 1913 | * ext4_bg_num_gdb - number of blocks used by the group table in group |
1955 | * @sb: superblock for filesystem | 1914 | * @sb: superblock for filesystem |
1956 | * @group: group number to check | 1915 | * @group: group number to check |
1957 | * | 1916 | * |
1958 | * Return the number of blocks used by the group descriptor table | 1917 | * Return the number of blocks used by the group descriptor table |
1959 | * (primary or backup) in this group. In the future there may be a | 1918 | * (primary or backup) in this group. In the future there may be a |
1960 | * different number of descriptor blocks in each group. | 1919 | * different number of descriptor blocks in each group. |
1961 | */ | 1920 | */ |
1962 | unsigned long ext4_bg_num_gdb(struct super_block *sb, int group) | 1921 | unsigned long ext4_bg_num_gdb(struct super_block *sb, int group) |
1963 | { | 1922 | { |
1964 | unsigned long first_meta_bg = | 1923 | unsigned long first_meta_bg = |
1965 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg); | 1924 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg); |
1966 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); | 1925 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); |
1967 | 1926 | ||
1968 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) || | 1927 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) || |
1969 | metagroup < first_meta_bg) | 1928 | metagroup < first_meta_bg) |
1970 | return ext4_bg_num_gdb_nometa(sb,group); | 1929 | return ext4_bg_num_gdb_nometa(sb,group); |
1971 | 1930 | ||
1972 | return ext4_bg_num_gdb_meta(sb,group); | 1931 | return ext4_bg_num_gdb_meta(sb,group); |
1973 | 1932 | ||
1974 | } | 1933 | } |
1975 | 1934 |