Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/balloc.c |
ac27a0ec DK |
3 | * |
4 | * Copyright (C) 1992, 1993, 1994, 1995 | |
5 | * Remy Card (card@masi.ibp.fr) | |
6 | * Laboratoire MASI - Institut Blaise Pascal | |
7 | * Universite Pierre et Marie Curie (Paris VI) | |
8 | * | |
9 | * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993 | |
10 | * Big-endian to little-endian byte-swapping/bitmaps by | |
11 | * David S. Miller (davem@caip.rutgers.edu), 1995 | |
12 | */ | |
13 | ||
14 | #include <linux/time.h> | |
15 | #include <linux/capability.h> | |
16 | #include <linux/fs.h> | |
dab291af | 17 | #include <linux/jbd2.h> |
ac27a0ec DK |
18 | #include <linux/quotaops.h> |
19 | #include <linux/buffer_head.h> | |
3dcf5451 CH |
20 | #include "ext4.h" |
21 | #include "ext4_jbd2.h" | |
717d50e4 | 22 | #include "group.h" |
3dcf5451 | 23 | |
ac27a0ec DK |
24 | /* |
25 | * balloc.c contains the blocks allocation and deallocation routines | |
26 | */ | |
27 | ||
72b64b59 AM |
28 | /* |
29 | * Calculate the block group number and offset, given a block number | |
30 | */ | |
31 | void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr, | |
fd2d4291 | 32 | ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp) |
72b64b59 | 33 | { |
8c55e204 | 34 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
72b64b59 AM |
35 | ext4_grpblk_t offset; |
36 | ||
8c55e204 | 37 | blocknr = blocknr - le32_to_cpu(es->s_first_data_block); |
f4e5bc24 | 38 | offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)); |
72b64b59 AM |
39 | if (offsetp) |
40 | *offsetp = offset; | |
41 | if (blockgrpp) | |
8c55e204 | 42 | *blockgrpp = blocknr; |
72b64b59 AM |
43 | |
44 | } | |
45 | ||
0bf7e837 JS |
46 | static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block, |
47 | ext4_group_t block_group) | |
48 | { | |
49 | ext4_group_t actual_group; | |
7477827f | 50 | ext4_get_group_no_and_offset(sb, block, &actual_group, NULL); |
0bf7e837 JS |
51 | if (actual_group == block_group) |
52 | return 1; | |
53 | return 0; | |
54 | } | |
55 | ||
56 | static int ext4_group_used_meta_blocks(struct super_block *sb, | |
57 | ext4_group_t block_group) | |
58 | { | |
59 | ext4_fsblk_t tmp; | |
60 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
61 | /* block bitmap, inode bitmap, and inode table blocks */ | |
62 | int used_blocks = sbi->s_itb_per_group + 2; | |
63 | ||
64 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) { | |
65 | struct ext4_group_desc *gdp; | |
66 | struct buffer_head *bh; | |
67 | ||
68 | gdp = ext4_get_group_desc(sb, block_group, &bh); | |
69 | if (!ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp), | |
70 | block_group)) | |
71 | used_blocks--; | |
72 | ||
73 | if (!ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp), | |
74 | block_group)) | |
75 | used_blocks--; | |
76 | ||
77 | tmp = ext4_inode_table(sb, gdp); | |
78 | for (; tmp < ext4_inode_table(sb, gdp) + | |
79 | sbi->s_itb_per_group; tmp++) { | |
80 | if (!ext4_block_in_group(sb, tmp, block_group)) | |
81 | used_blocks -= 1; | |
82 | } | |
83 | } | |
84 | return used_blocks; | |
85 | } | |
c2ea3fde | 86 | |
717d50e4 AD |
87 | /* Initializes an uninitialized block bitmap if given, and returns the |
88 | * number of blocks free in the group. */ | |
89 | unsigned ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh, | |
fd2d4291 | 90 | ext4_group_t block_group, struct ext4_group_desc *gdp) |
717d50e4 | 91 | { |
717d50e4 AD |
92 | int bit, bit_max; |
93 | unsigned free_blocks, group_blocks; | |
94 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
95 | ||
96 | if (bh) { | |
97 | J_ASSERT_BH(bh, buffer_locked(bh)); | |
98 | ||
99 | /* If checksum is bad mark all blocks used to prevent allocation | |
100 | * essentially implementing a per-group read-only flag. */ | |
101 | if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { | |
46e665e9 | 102 | ext4_error(sb, __func__, |
fd2d4291 | 103 | "Checksum bad for group %lu\n", block_group); |
717d50e4 AD |
104 | gdp->bg_free_blocks_count = 0; |
105 | gdp->bg_free_inodes_count = 0; | |
106 | gdp->bg_itable_unused = 0; | |
107 | memset(bh->b_data, 0xff, sb->s_blocksize); | |
108 | return 0; | |
109 | } | |
110 | memset(bh->b_data, 0, sb->s_blocksize); | |
111 | } | |
112 | ||
113 | /* Check for superblock and gdt backups in this group */ | |
114 | bit_max = ext4_bg_has_super(sb, block_group); | |
115 | ||
116 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) || | |
117 | block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) * | |
118 | sbi->s_desc_per_block) { | |
119 | if (bit_max) { | |
120 | bit_max += ext4_bg_num_gdb(sb, block_group); | |
121 | bit_max += | |
122 | le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks); | |
123 | } | |
124 | } else { /* For META_BG_BLOCK_GROUPS */ | |
6afd6707 | 125 | bit_max += ext4_bg_num_gdb(sb, block_group); |
717d50e4 AD |
126 | } |
127 | ||
128 | if (block_group == sbi->s_groups_count - 1) { | |
129 | /* | |
130 | * Even though mke2fs always initialize first and last group | |
131 | * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need | |
132 | * to make sure we calculate the right free blocks | |
133 | */ | |
134 | group_blocks = ext4_blocks_count(sbi->s_es) - | |
135 | le32_to_cpu(sbi->s_es->s_first_data_block) - | |
af5bc92d | 136 | (EXT4_BLOCKS_PER_GROUP(sb) * (sbi->s_groups_count - 1)); |
717d50e4 AD |
137 | } else { |
138 | group_blocks = EXT4_BLOCKS_PER_GROUP(sb); | |
139 | } | |
140 | ||
141 | free_blocks = group_blocks - bit_max; | |
142 | ||
143 | if (bh) { | |
0bf7e837 JS |
144 | ext4_fsblk_t start, tmp; |
145 | int flex_bg = 0; | |
d00a6d7b | 146 | |
717d50e4 AD |
147 | for (bit = 0; bit < bit_max; bit++) |
148 | ext4_set_bit(bit, bh->b_data); | |
149 | ||
d00a6d7b | 150 | start = ext4_group_first_block_no(sb, block_group); |
717d50e4 | 151 | |
0bf7e837 JS |
152 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, |
153 | EXT4_FEATURE_INCOMPAT_FLEX_BG)) | |
154 | flex_bg = 1; | |
717d50e4 | 155 | |
0bf7e837 JS |
156 | /* Set bits for block and inode bitmaps, and inode table */ |
157 | tmp = ext4_block_bitmap(sb, gdp); | |
158 | if (!flex_bg || ext4_block_in_group(sb, tmp, block_group)) | |
159 | ext4_set_bit(tmp - start, bh->b_data); | |
160 | ||
161 | tmp = ext4_inode_bitmap(sb, gdp); | |
162 | if (!flex_bg || ext4_block_in_group(sb, tmp, block_group)) | |
163 | ext4_set_bit(tmp - start, bh->b_data); | |
164 | ||
165 | tmp = ext4_inode_table(sb, gdp); | |
166 | for (; tmp < ext4_inode_table(sb, gdp) + | |
167 | sbi->s_itb_per_group; tmp++) { | |
168 | if (!flex_bg || | |
169 | ext4_block_in_group(sb, tmp, block_group)) | |
170 | ext4_set_bit(tmp - start, bh->b_data); | |
171 | } | |
717d50e4 AD |
172 | /* |
173 | * Also if the number of blocks within the group is | |
174 | * less than the blocksize * 8 ( which is the size | |
175 | * of bitmap ), set rest of the block bitmap to 1 | |
176 | */ | |
177 | mark_bitmap_end(group_blocks, sb->s_blocksize * 8, bh->b_data); | |
178 | } | |
0bf7e837 | 179 | return free_blocks - ext4_group_used_meta_blocks(sb, block_group); |
717d50e4 AD |
180 | } |
181 | ||
182 | ||
ac27a0ec DK |
183 | /* |
184 | * The free blocks are managed by bitmaps. A file system contains several | |
185 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap | |
186 | * block for inodes, N blocks for the inode table and data blocks. | |
187 | * | |
188 | * The file system contains group descriptors which are located after the | |
189 | * super block. Each descriptor contains the number of the bitmap block and | |
190 | * the free blocks count in the block. The descriptors are loaded in memory | |
e627432c | 191 | * when a file system is mounted (see ext4_fill_super). |
ac27a0ec DK |
192 | */ |
193 | ||
194 | ||
195 | #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) | |
196 | ||
197 | /** | |
617ba13b | 198 | * ext4_get_group_desc() -- load group descriptor from disk |
ac27a0ec DK |
199 | * @sb: super block |
200 | * @block_group: given block group | |
201 | * @bh: pointer to the buffer head to store the block | |
202 | * group descriptor | |
203 | */ | |
af5bc92d | 204 | struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb, |
fd2d4291 | 205 | ext4_group_t block_group, |
af5bc92d | 206 | struct buffer_head **bh) |
ac27a0ec DK |
207 | { |
208 | unsigned long group_desc; | |
209 | unsigned long offset; | |
af5bc92d | 210 | struct ext4_group_desc *desc; |
617ba13b | 211 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
ac27a0ec DK |
212 | |
213 | if (block_group >= sbi->s_groups_count) { | |
af5bc92d TT |
214 | ext4_error(sb, "ext4_get_group_desc", |
215 | "block_group >= groups_count - " | |
216 | "block_group = %lu, groups_count = %lu", | |
217 | block_group, sbi->s_groups_count); | |
ac27a0ec DK |
218 | |
219 | return NULL; | |
220 | } | |
221 | smp_rmb(); | |
222 | ||
617ba13b MC |
223 | group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb); |
224 | offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1); | |
ac27a0ec | 225 | if (!sbi->s_group_desc[group_desc]) { |
af5bc92d TT |
226 | ext4_error(sb, "ext4_get_group_desc", |
227 | "Group descriptor not loaded - " | |
228 | "block_group = %lu, group_desc = %lu, desc = %lu", | |
229 | block_group, group_desc, offset); | |
ac27a0ec DK |
230 | return NULL; |
231 | } | |
232 | ||
0d1ee42f AR |
233 | desc = (struct ext4_group_desc *)( |
234 | (__u8 *)sbi->s_group_desc[group_desc]->b_data + | |
235 | offset * EXT4_DESC_SIZE(sb)); | |
ac27a0ec DK |
236 | if (bh) |
237 | *bh = sbi->s_group_desc[group_desc]; | |
0d1ee42f | 238 | return desc; |
ac27a0ec DK |
239 | } |
240 | ||
abcb2947 AK |
241 | static int ext4_valid_block_bitmap(struct super_block *sb, |
242 | struct ext4_group_desc *desc, | |
243 | unsigned int block_group, | |
244 | struct buffer_head *bh) | |
245 | { | |
246 | ext4_grpblk_t offset; | |
247 | ext4_grpblk_t next_zero_bit; | |
248 | ext4_fsblk_t bitmap_blk; | |
249 | ext4_fsblk_t group_first_block; | |
250 | ||
251 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) { | |
252 | /* with FLEX_BG, the inode/block bitmaps and itable | |
253 | * blocks may not be in the group at all | |
254 | * so the bitmap validation will be skipped for those groups | |
255 | * or it has to also read the block group where the bitmaps | |
256 | * are located to verify they are set. | |
257 | */ | |
258 | return 1; | |
259 | } | |
260 | group_first_block = ext4_group_first_block_no(sb, block_group); | |
261 | ||
262 | /* check whether block bitmap block number is set */ | |
263 | bitmap_blk = ext4_block_bitmap(sb, desc); | |
264 | offset = bitmap_blk - group_first_block; | |
265 | if (!ext4_test_bit(offset, bh->b_data)) | |
266 | /* bad block bitmap */ | |
267 | goto err_out; | |
268 | ||
269 | /* check whether the inode bitmap block number is set */ | |
270 | bitmap_blk = ext4_inode_bitmap(sb, desc); | |
271 | offset = bitmap_blk - group_first_block; | |
272 | if (!ext4_test_bit(offset, bh->b_data)) | |
273 | /* bad block bitmap */ | |
274 | goto err_out; | |
275 | ||
276 | /* check whether the inode table block number is set */ | |
277 | bitmap_blk = ext4_inode_table(sb, desc); | |
278 | offset = bitmap_blk - group_first_block; | |
279 | next_zero_bit = ext4_find_next_zero_bit(bh->b_data, | |
280 | offset + EXT4_SB(sb)->s_itb_per_group, | |
281 | offset); | |
282 | if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group) | |
283 | /* good bitmap for inode tables */ | |
284 | return 1; | |
285 | ||
286 | err_out: | |
46e665e9 | 287 | ext4_error(sb, __func__, |
abcb2947 AK |
288 | "Invalid block bitmap - " |
289 | "block_group = %d, block = %llu", | |
290 | block_group, bitmap_blk); | |
291 | return 0; | |
292 | } | |
ac27a0ec | 293 | /** |
574ca174 | 294 | * ext4_read_block_bitmap() |
ac27a0ec DK |
295 | * @sb: super block |
296 | * @block_group: given block group | |
297 | * | |
abcb2947 AK |
298 | * Read the bitmap for a given block_group,and validate the |
299 | * bits for block/inode/inode tables are set in the bitmaps | |
ac27a0ec DK |
300 | * |
301 | * Return buffer_head on success or NULL in case of failure. | |
302 | */ | |
717d50e4 | 303 | struct buffer_head * |
574ca174 | 304 | ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group) |
ac27a0ec | 305 | { |
af5bc92d TT |
306 | struct ext4_group_desc *desc; |
307 | struct buffer_head *bh = NULL; | |
7c9e69fa | 308 | ext4_fsblk_t bitmap_blk; |
ac27a0ec | 309 | |
717d50e4 | 310 | desc = ext4_get_group_desc(sb, block_group, NULL); |
ac27a0ec | 311 | if (!desc) |
7c9e69fa AK |
312 | return NULL; |
313 | bitmap_blk = ext4_block_bitmap(sb, desc); | |
abcb2947 AK |
314 | bh = sb_getblk(sb, bitmap_blk); |
315 | if (unlikely(!bh)) { | |
46e665e9 | 316 | ext4_error(sb, __func__, |
abcb2947 | 317 | "Cannot read block bitmap - " |
e29d1cde ES |
318 | "block_group = %lu, block_bitmap = %llu", |
319 | block_group, bitmap_blk); | |
abcb2947 AK |
320 | return NULL; |
321 | } | |
c806e68f FB |
322 | if (buffer_uptodate(bh) && |
323 | !(desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) | |
abcb2947 AK |
324 | return bh; |
325 | ||
c806e68f | 326 | lock_buffer(bh); |
b5f10eed | 327 | spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
717d50e4 | 328 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
abcb2947 AK |
329 | ext4_init_block_bitmap(sb, bh, block_group, desc); |
330 | set_buffer_uptodate(bh); | |
331 | unlock_buffer(bh); | |
b5f10eed | 332 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
abcb2947 | 333 | return bh; |
717d50e4 | 334 | } |
b5f10eed | 335 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
abcb2947 AK |
336 | if (bh_submit_read(bh) < 0) { |
337 | put_bh(bh); | |
46e665e9 | 338 | ext4_error(sb, __func__, |
ac27a0ec | 339 | "Cannot read block bitmap - " |
e29d1cde ES |
340 | "block_group = %lu, block_bitmap = %llu", |
341 | block_group, bitmap_blk); | |
abcb2947 AK |
342 | return NULL; |
343 | } | |
519deca0 AK |
344 | ext4_valid_block_bitmap(sb, desc, block_group, bh); |
345 | /* | |
346 | * file system mounted not to panic on error, | |
347 | * continue with corrupt bitmap | |
348 | */ | |
ac27a0ec DK |
349 | return bh; |
350 | } | |
ac27a0ec DK |
351 | |
352 | /** | |
617ba13b | 353 | * ext4_free_blocks_sb() -- Free given blocks and update quota |
ac27a0ec DK |
354 | * @handle: handle to this transaction |
355 | * @sb: super block | |
356 | * @block: start physcial block to free | |
357 | * @count: number of blocks to free | |
358 | * @pdquot_freed_blocks: pointer to quota | |
c2ea3fde TT |
359 | * |
360 | * XXX This function is only used by the on-line resizing code, which | |
361 | * should probably be fixed up to call the mballoc variant. There | |
362 | * this needs to be cleaned up later; in fact, I'm not convinced this | |
363 | * is 100% correct in the face of the mballoc code. The online resizing | |
364 | * code needs to be fixed up to more tightly (and correctly) interlock | |
365 | * with the mballoc code. | |
ac27a0ec | 366 | */ |
617ba13b MC |
367 | void ext4_free_blocks_sb(handle_t *handle, struct super_block *sb, |
368 | ext4_fsblk_t block, unsigned long count, | |
ac27a0ec DK |
369 | unsigned long *pdquot_freed_blocks) |
370 | { | |
371 | struct buffer_head *bitmap_bh = NULL; | |
372 | struct buffer_head *gd_bh; | |
fd2d4291 | 373 | ext4_group_t block_group; |
617ba13b | 374 | ext4_grpblk_t bit; |
ac27a0ec DK |
375 | unsigned long i; |
376 | unsigned long overflow; | |
af5bc92d TT |
377 | struct ext4_group_desc *desc; |
378 | struct ext4_super_block *es; | |
617ba13b | 379 | struct ext4_sb_info *sbi; |
ac27a0ec | 380 | int err = 0, ret; |
617ba13b | 381 | ext4_grpblk_t group_freed; |
ac27a0ec DK |
382 | |
383 | *pdquot_freed_blocks = 0; | |
617ba13b | 384 | sbi = EXT4_SB(sb); |
ac27a0ec DK |
385 | es = sbi->s_es; |
386 | if (block < le32_to_cpu(es->s_first_data_block) || | |
387 | block + count < block || | |
bd81d8ee | 388 | block + count > ext4_blocks_count(es)) { |
af5bc92d TT |
389 | ext4_error(sb, "ext4_free_blocks", |
390 | "Freeing blocks not in datazone - " | |
391 | "block = %llu, count = %lu", block, count); | |
ac27a0ec DK |
392 | goto error_return; |
393 | } | |
394 | ||
af5bc92d | 395 | ext4_debug("freeing block(s) %llu-%llu\n", block, block + count - 1); |
ac27a0ec DK |
396 | |
397 | do_more: | |
398 | overflow = 0; | |
3a5b2ecd | 399 | ext4_get_group_no_and_offset(sb, block, &block_group, &bit); |
ac27a0ec DK |
400 | /* |
401 | * Check to see if we are freeing blocks across a group | |
402 | * boundary. | |
403 | */ | |
617ba13b MC |
404 | if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) { |
405 | overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb); | |
ac27a0ec DK |
406 | count -= overflow; |
407 | } | |
408 | brelse(bitmap_bh); | |
574ca174 | 409 | bitmap_bh = ext4_read_block_bitmap(sb, block_group); |
ac27a0ec DK |
410 | if (!bitmap_bh) |
411 | goto error_return; | |
af5bc92d | 412 | desc = ext4_get_group_desc(sb, block_group, &gd_bh); |
ac27a0ec DK |
413 | if (!desc) |
414 | goto error_return; | |
415 | ||
8fadc143 AR |
416 | if (in_range(ext4_block_bitmap(sb, desc), block, count) || |
417 | in_range(ext4_inode_bitmap(sb, desc), block, count) || | |
418 | in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) || | |
419 | in_range(block + count - 1, ext4_inode_table(sb, desc), | |
cb47dce7 | 420 | sbi->s_itb_per_group)) { |
af5bc92d TT |
421 | ext4_error(sb, "ext4_free_blocks", |
422 | "Freeing blocks in system zones - " | |
423 | "Block = %llu, count = %lu", | |
424 | block, count); | |
cb47dce7 AK |
425 | goto error_return; |
426 | } | |
ac27a0ec DK |
427 | |
428 | /* | |
429 | * We are about to start releasing blocks in the bitmap, | |
430 | * so we need undo access. | |
431 | */ | |
432 | /* @@@ check errors */ | |
433 | BUFFER_TRACE(bitmap_bh, "getting undo access"); | |
617ba13b | 434 | err = ext4_journal_get_undo_access(handle, bitmap_bh); |
ac27a0ec DK |
435 | if (err) |
436 | goto error_return; | |
437 | ||
438 | /* | |
439 | * We are about to modify some metadata. Call the journal APIs | |
440 | * to unshare ->b_data if a currently-committing transaction is | |
441 | * using it | |
442 | */ | |
443 | BUFFER_TRACE(gd_bh, "get_write_access"); | |
617ba13b | 444 | err = ext4_journal_get_write_access(handle, gd_bh); |
ac27a0ec DK |
445 | if (err) |
446 | goto error_return; | |
447 | ||
448 | jbd_lock_bh_state(bitmap_bh); | |
449 | ||
450 | for (i = 0, group_freed = 0; i < count; i++) { | |
451 | /* | |
452 | * An HJ special. This is expensive... | |
453 | */ | |
e23291b9 | 454 | #ifdef CONFIG_JBD2_DEBUG |
ac27a0ec DK |
455 | jbd_unlock_bh_state(bitmap_bh); |
456 | { | |
457 | struct buffer_head *debug_bh; | |
458 | debug_bh = sb_find_get_block(sb, block + i); | |
459 | if (debug_bh) { | |
460 | BUFFER_TRACE(debug_bh, "Deleted!"); | |
461 | if (!bh2jh(bitmap_bh)->b_committed_data) | |
462 | BUFFER_TRACE(debug_bh, | |
463 | "No commited data in bitmap"); | |
464 | BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap"); | |
465 | __brelse(debug_bh); | |
466 | } | |
467 | } | |
468 | jbd_lock_bh_state(bitmap_bh); | |
469 | #endif | |
470 | if (need_resched()) { | |
471 | jbd_unlock_bh_state(bitmap_bh); | |
472 | cond_resched(); | |
473 | jbd_lock_bh_state(bitmap_bh); | |
474 | } | |
475 | /* @@@ This prevents newly-allocated data from being | |
476 | * freed and then reallocated within the same | |
477 | * transaction. | |
478 | * | |
479 | * Ideally we would want to allow that to happen, but to | |
dab291af | 480 | * do so requires making jbd2_journal_forget() capable of |
ac27a0ec DK |
481 | * revoking the queued write of a data block, which |
482 | * implies blocking on the journal lock. *forget() | |
483 | * cannot block due to truncate races. | |
484 | * | |
dab291af | 485 | * Eventually we can fix this by making jbd2_journal_forget() |
ac27a0ec DK |
486 | * return a status indicating whether or not it was able |
487 | * to revoke the buffer. On successful revoke, it is | |
488 | * safe not to set the allocation bit in the committed | |
489 | * bitmap, because we know that there is no outstanding | |
490 | * activity on the buffer any more and so it is safe to | |
491 | * reallocate it. | |
492 | */ | |
493 | BUFFER_TRACE(bitmap_bh, "set in b_committed_data"); | |
494 | J_ASSERT_BH(bitmap_bh, | |
495 | bh2jh(bitmap_bh)->b_committed_data != NULL); | |
617ba13b | 496 | ext4_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i, |
ac27a0ec DK |
497 | bh2jh(bitmap_bh)->b_committed_data); |
498 | ||
499 | /* | |
500 | * We clear the bit in the bitmap after setting the committed | |
501 | * data bit, because this is the reverse order to that which | |
502 | * the allocator uses. | |
503 | */ | |
504 | BUFFER_TRACE(bitmap_bh, "clear bit"); | |
617ba13b | 505 | if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), |
ac27a0ec DK |
506 | bit + i, bitmap_bh->b_data)) { |
507 | jbd_unlock_bh_state(bitmap_bh); | |
46e665e9 | 508 | ext4_error(sb, __func__, |
2ae02107 | 509 | "bit already cleared for block %llu", |
bd81d8ee | 510 | (ext4_fsblk_t)(block + i)); |
ac27a0ec DK |
511 | jbd_lock_bh_state(bitmap_bh); |
512 | BUFFER_TRACE(bitmap_bh, "bit already cleared"); | |
513 | } else { | |
514 | group_freed++; | |
515 | } | |
516 | } | |
517 | jbd_unlock_bh_state(bitmap_bh); | |
518 | ||
519 | spin_lock(sb_bgl_lock(sbi, block_group)); | |
e8546d06 | 520 | le16_add_cpu(&desc->bg_free_blocks_count, group_freed); |
717d50e4 | 521 | desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc); |
ac27a0ec | 522 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
aa0dff2d | 523 | percpu_counter_add(&sbi->s_freeblocks_counter, count); |
ac27a0ec | 524 | |
772cb7c8 JS |
525 | if (sbi->s_log_groups_per_flex) { |
526 | ext4_group_t flex_group = ext4_flex_group(sbi, block_group); | |
527 | spin_lock(sb_bgl_lock(sbi, flex_group)); | |
528 | sbi->s_flex_groups[flex_group].free_blocks += count; | |
529 | spin_unlock(sb_bgl_lock(sbi, flex_group)); | |
530 | } | |
531 | ||
ac27a0ec DK |
532 | /* We dirtied the bitmap block */ |
533 | BUFFER_TRACE(bitmap_bh, "dirtied bitmap block"); | |
617ba13b | 534 | err = ext4_journal_dirty_metadata(handle, bitmap_bh); |
ac27a0ec DK |
535 | |
536 | /* And the group descriptor block */ | |
537 | BUFFER_TRACE(gd_bh, "dirtied group descriptor block"); | |
617ba13b | 538 | ret = ext4_journal_dirty_metadata(handle, gd_bh); |
ac27a0ec DK |
539 | if (!err) err = ret; |
540 | *pdquot_freed_blocks += group_freed; | |
541 | ||
542 | if (overflow && !err) { | |
543 | block += count; | |
544 | count = overflow; | |
545 | goto do_more; | |
546 | } | |
547 | sb->s_dirt = 1; | |
548 | error_return: | |
549 | brelse(bitmap_bh); | |
617ba13b | 550 | ext4_std_error(sb, err); |
ac27a0ec DK |
551 | return; |
552 | } | |
553 | ||
554 | /** | |
617ba13b | 555 | * ext4_free_blocks() -- Free given blocks and update quota |
ac27a0ec DK |
556 | * @handle: handle for this transaction |
557 | * @inode: inode | |
558 | * @block: start physical block to free | |
559 | * @count: number of blocks to count | |
c9de560d | 560 | * @metadata: Are these metadata blocks |
ac27a0ec | 561 | */ |
617ba13b | 562 | void ext4_free_blocks(handle_t *handle, struct inode *inode, |
c9de560d AT |
563 | ext4_fsblk_t block, unsigned long count, |
564 | int metadata) | |
ac27a0ec | 565 | { |
af5bc92d | 566 | struct super_block *sb; |
ac27a0ec DK |
567 | unsigned long dquot_freed_blocks; |
568 | ||
c9de560d AT |
569 | /* this isn't the right place to decide whether block is metadata |
570 | * inode.c/extents.c knows better, but for safety ... */ | |
571 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) || | |
572 | ext4_should_journal_data(inode)) | |
573 | metadata = 1; | |
574 | ||
ac27a0ec | 575 | sb = inode->i_sb; |
c9de560d | 576 | |
c2ea3fde TT |
577 | ext4_mb_free_blocks(handle, inode, block, count, |
578 | metadata, &dquot_freed_blocks); | |
ac27a0ec DK |
579 | if (dquot_freed_blocks) |
580 | DQUOT_FREE_BLOCK(inode, dquot_freed_blocks); | |
581 | return; | |
582 | } | |
583 | ||
a30d542a | 584 | int ext4_claim_free_blocks(struct ext4_sb_info *sbi, |
5c791616 | 585 | s64 nblocks) |
a30d542a | 586 | { |
6bc6e63f | 587 | s64 free_blocks, dirty_blocks; |
5c791616 | 588 | s64 root_blocks = 0; |
a30d542a | 589 | struct percpu_counter *fbc = &sbi->s_freeblocks_counter; |
6bc6e63f | 590 | struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter; |
a30d542a | 591 | |
6bc6e63f AK |
592 | free_blocks = percpu_counter_read_positive(fbc); |
593 | dirty_blocks = percpu_counter_read_positive(dbc); | |
a30d542a AK |
594 | |
595 | if (!capable(CAP_SYS_RESOURCE) && | |
596 | sbi->s_resuid != current->fsuid && | |
597 | (sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid))) | |
598 | root_blocks = ext4_r_blocks_count(sbi->s_es); | |
599 | ||
6bc6e63f AK |
600 | if (free_blocks - (nblocks + root_blocks + dirty_blocks) < |
601 | EXT4_FREEBLOCKS_WATERMARK) { | |
602 | free_blocks = percpu_counter_sum(fbc); | |
603 | dirty_blocks = percpu_counter_sum(dbc); | |
604 | if (dirty_blocks < 0) { | |
605 | printk(KERN_CRIT "Dirty block accounting " | |
606 | "went wrong %lld\n", | |
607 | dirty_blocks); | |
608 | } | |
609 | } | |
610 | /* Check whether we have space after | |
611 | * accounting for current dirty blocks | |
612 | */ | |
5c791616 | 613 | if (free_blocks < ((root_blocks + nblocks) + dirty_blocks)) |
a30d542a AK |
614 | /* we don't have free space */ |
615 | return -ENOSPC; | |
616 | ||
6bc6e63f AK |
617 | /* Add the blocks to nblocks */ |
618 | percpu_counter_add(dbc, nblocks); | |
a30d542a AK |
619 | return 0; |
620 | } | |
621 | ||
ac27a0ec | 622 | /** |
617ba13b | 623 | * ext4_has_free_blocks() |
07031431 MC |
624 | * @sbi: in-core super block structure. |
625 | * @nblocks: number of neeed blocks | |
ac27a0ec | 626 | * |
07031431 MC |
627 | * Check if filesystem has free blocks available for allocation. |
628 | * Return the number of blocks avaible for allocation for this request | |
629 | * On success, return nblocks | |
ac27a0ec | 630 | */ |
07031431 | 631 | ext4_fsblk_t ext4_has_free_blocks(struct ext4_sb_info *sbi, |
5c791616 | 632 | s64 nblocks) |
ac27a0ec | 633 | { |
5c791616 AK |
634 | s64 free_blocks, dirty_blocks; |
635 | s64 root_blocks = 0; | |
6bc6e63f AK |
636 | struct percpu_counter *fbc = &sbi->s_freeblocks_counter; |
637 | struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter; | |
ac27a0ec | 638 | |
6bc6e63f AK |
639 | free_blocks = percpu_counter_read_positive(fbc); |
640 | dirty_blocks = percpu_counter_read_positive(dbc); | |
07031431 MC |
641 | |
642 | if (!capable(CAP_SYS_RESOURCE) && | |
ac27a0ec | 643 | sbi->s_resuid != current->fsuid && |
07031431 MC |
644 | (sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid))) |
645 | root_blocks = ext4_r_blocks_count(sbi->s_es); | |
a30d542a | 646 | |
6bc6e63f AK |
647 | if (free_blocks - (nblocks + root_blocks + dirty_blocks) < |
648 | EXT4_FREEBLOCKS_WATERMARK) { | |
5c791616 AK |
649 | free_blocks = percpu_counter_sum(fbc); |
650 | dirty_blocks = percpu_counter_sum(dbc); | |
6bc6e63f AK |
651 | } |
652 | if (free_blocks <= (root_blocks + dirty_blocks)) | |
16eb7295 AK |
653 | /* we don't have free space */ |
654 | return 0; | |
5c791616 | 655 | |
6bc6e63f | 656 | if (free_blocks - (root_blocks + dirty_blocks) < nblocks) |
5c791616 | 657 | return free_blocks - (root_blocks + dirty_blocks); |
07031431 | 658 | return nblocks; |
a30d542a | 659 | } |
07031431 | 660 | |
ac27a0ec DK |
661 | |
662 | /** | |
617ba13b | 663 | * ext4_should_retry_alloc() |
ac27a0ec DK |
664 | * @sb: super block |
665 | * @retries number of attemps has been made | |
666 | * | |
617ba13b | 667 | * ext4_should_retry_alloc() is called when ENOSPC is returned, and if |
ac27a0ec DK |
668 | * it is profitable to retry the operation, this function will wait |
669 | * for the current or commiting transaction to complete, and then | |
670 | * return TRUE. | |
671 | * | |
672 | * if the total number of retries exceed three times, return FALSE. | |
673 | */ | |
617ba13b | 674 | int ext4_should_retry_alloc(struct super_block *sb, int *retries) |
ac27a0ec | 675 | { |
07031431 | 676 | if (!ext4_has_free_blocks(EXT4_SB(sb), 1) || (*retries)++ > 3) |
ac27a0ec DK |
677 | return 0; |
678 | ||
679 | jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id); | |
680 | ||
dab291af | 681 | return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal); |
ac27a0ec DK |
682 | } |
683 | ||
654b4908 | 684 | #define EXT4_META_BLOCK 0x1 |
c9de560d | 685 | |
654b4908 | 686 | static ext4_fsblk_t do_blk_alloc(handle_t *handle, struct inode *inode, |
7061eba7 | 687 | ext4_lblk_t iblock, ext4_fsblk_t goal, |
654b4908 | 688 | unsigned long *count, int *errp, int flags) |
ac27a0ec | 689 | { |
c9de560d AT |
690 | struct ext4_allocation_request ar; |
691 | ext4_fsblk_t ret; | |
ac27a0ec | 692 | |
c9de560d | 693 | memset(&ar, 0, sizeof(ar)); |
7061eba7 | 694 | /* Fill with neighbour allocated blocks */ |
7061eba7 | 695 | |
c9de560d AT |
696 | ar.inode = inode; |
697 | ar.goal = goal; | |
698 | ar.len = *count; | |
7061eba7 | 699 | ar.logical = iblock; |
654b4908 AK |
700 | |
701 | if (S_ISREG(inode->i_mode) && !(flags & EXT4_META_BLOCK)) | |
702 | /* enable in-core preallocation for data block allocation */ | |
7061eba7 AK |
703 | ar.flags = EXT4_MB_HINT_DATA; |
704 | else | |
705 | /* disable in-core preallocation for non-regular files */ | |
706 | ar.flags = 0; | |
654b4908 | 707 | |
c9de560d AT |
708 | ret = ext4_mb_new_blocks(handle, &ar, errp); |
709 | *count = ar.len; | |
710 | return ret; | |
ac27a0ec DK |
711 | } |
712 | ||
654b4908 | 713 | /* |
d2a17637 | 714 | * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks |
654b4908 AK |
715 | * |
716 | * @handle: handle to this transaction | |
717 | * @inode: file inode | |
718 | * @goal: given target block(filesystem wide) | |
d2a17637 | 719 | * @count: total number of blocks need |
654b4908 AK |
720 | * @errp: error code |
721 | * | |
d2a17637 MC |
722 | * Return 1st allocated block numberon success, *count stores total account |
723 | * error stores in errp pointer | |
654b4908 | 724 | */ |
d2a17637 MC |
725 | ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode, |
726 | ext4_fsblk_t goal, unsigned long *count, int *errp) | |
654b4908 | 727 | { |
d2a17637 MC |
728 | ext4_fsblk_t ret; |
729 | ret = do_blk_alloc(handle, inode, 0, goal, | |
730 | count, errp, EXT4_META_BLOCK); | |
731 | /* | |
732 | * Account for the allocated meta blocks | |
733 | */ | |
166348dd | 734 | if (!(*errp) && EXT4_I(inode)->i_delalloc_reserved_flag) { |
d2a17637 MC |
735 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
736 | EXT4_I(inode)->i_allocated_meta_blocks += *count; | |
737 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | |
738 | } | |
739 | return ret; | |
654b4908 AK |
740 | } |
741 | ||
742 | /* | |
d2a17637 | 743 | * ext4_new_meta_block() -- allocate block for meta data (indexing) blocks |
654b4908 AK |
744 | * |
745 | * @handle: handle to this transaction | |
746 | * @inode: file inode | |
747 | * @goal: given target block(filesystem wide) | |
654b4908 AK |
748 | * @errp: error code |
749 | * | |
d2a17637 | 750 | * Return allocated block number on success |
654b4908 | 751 | */ |
d2a17637 MC |
752 | ext4_fsblk_t ext4_new_meta_block(handle_t *handle, struct inode *inode, |
753 | ext4_fsblk_t goal, int *errp) | |
654b4908 | 754 | { |
d2a17637 MC |
755 | unsigned long count = 1; |
756 | return ext4_new_meta_blocks(handle, inode, goal, &count, errp); | |
654b4908 AK |
757 | } |
758 | ||
759 | /* | |
760 | * ext4_new_blocks() -- allocate data blocks | |
761 | * | |
762 | * @handle: handle to this transaction | |
763 | * @inode: file inode | |
764 | * @goal: given target block(filesystem wide) | |
765 | * @count: total number of blocks need | |
766 | * @errp: error code | |
767 | * | |
768 | * Return 1st allocated block numberon success, *count stores total account | |
769 | * error stores in errp pointer | |
770 | */ | |
771 | ||
772 | ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode, | |
773 | ext4_lblk_t iblock, ext4_fsblk_t goal, | |
774 | unsigned long *count, int *errp) | |
775 | { | |
776 | return do_blk_alloc(handle, inode, iblock, goal, count, errp, 0); | |
777 | } | |
c9de560d | 778 | |
ac27a0ec | 779 | /** |
617ba13b | 780 | * ext4_count_free_blocks() -- count filesystem free blocks |
ac27a0ec DK |
781 | * @sb: superblock |
782 | * | |
783 | * Adds up the number of free blocks from each block group. | |
784 | */ | |
617ba13b | 785 | ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb) |
ac27a0ec | 786 | { |
617ba13b MC |
787 | ext4_fsblk_t desc_count; |
788 | struct ext4_group_desc *gdp; | |
fd2d4291 AM |
789 | ext4_group_t i; |
790 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; | |
617ba13b MC |
791 | #ifdef EXT4FS_DEBUG |
792 | struct ext4_super_block *es; | |
793 | ext4_fsblk_t bitmap_count; | |
ac27a0ec DK |
794 | unsigned long x; |
795 | struct buffer_head *bitmap_bh = NULL; | |
796 | ||
617ba13b | 797 | es = EXT4_SB(sb)->s_es; |
ac27a0ec DK |
798 | desc_count = 0; |
799 | bitmap_count = 0; | |
800 | gdp = NULL; | |
801 | ||
802 | smp_rmb(); | |
803 | for (i = 0; i < ngroups; i++) { | |
617ba13b | 804 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
805 | if (!gdp) |
806 | continue; | |
807 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); | |
808 | brelse(bitmap_bh); | |
574ca174 | 809 | bitmap_bh = ext4_read_block_bitmap(sb, i); |
ac27a0ec DK |
810 | if (bitmap_bh == NULL) |
811 | continue; | |
812 | ||
617ba13b | 813 | x = ext4_count_free(bitmap_bh, sb->s_blocksize); |
fd2d4291 | 814 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
ac27a0ec DK |
815 | i, le16_to_cpu(gdp->bg_free_blocks_count), x); |
816 | bitmap_count += x; | |
817 | } | |
818 | brelse(bitmap_bh); | |
4776004f TT |
819 | printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu" |
820 | ", computed = %llu, %llu\n", ext4_free_blocks_count(es), | |
821 | desc_count, bitmap_count); | |
ac27a0ec DK |
822 | return bitmap_count; |
823 | #else | |
824 | desc_count = 0; | |
825 | smp_rmb(); | |
826 | for (i = 0; i < ngroups; i++) { | |
617ba13b | 827 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
828 | if (!gdp) |
829 | continue; | |
830 | desc_count += le16_to_cpu(gdp->bg_free_blocks_count); | |
831 | } | |
832 | ||
833 | return desc_count; | |
834 | #endif | |
835 | } | |
836 | ||
fd2d4291 | 837 | static inline int test_root(ext4_group_t a, int b) |
ac27a0ec DK |
838 | { |
839 | int num = b; | |
840 | ||
841 | while (a > num) | |
842 | num *= b; | |
843 | return num == a; | |
844 | } | |
845 | ||
fd2d4291 | 846 | static int ext4_group_sparse(ext4_group_t group) |
ac27a0ec DK |
847 | { |
848 | if (group <= 1) | |
849 | return 1; | |
850 | if (!(group & 1)) | |
851 | return 0; | |
852 | return (test_root(group, 7) || test_root(group, 5) || | |
853 | test_root(group, 3)); | |
854 | } | |
855 | ||
856 | /** | |
617ba13b | 857 | * ext4_bg_has_super - number of blocks used by the superblock in group |
ac27a0ec DK |
858 | * @sb: superblock for filesystem |
859 | * @group: group number to check | |
860 | * | |
861 | * Return the number of blocks used by the superblock (primary or backup) | |
862 | * in this group. Currently this will be only 0 or 1. | |
863 | */ | |
fd2d4291 | 864 | int ext4_bg_has_super(struct super_block *sb, ext4_group_t group) |
ac27a0ec | 865 | { |
617ba13b MC |
866 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
867 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) && | |
868 | !ext4_group_sparse(group)) | |
ac27a0ec DK |
869 | return 0; |
870 | return 1; | |
871 | } | |
872 | ||
fd2d4291 AM |
873 | static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb, |
874 | ext4_group_t group) | |
ac27a0ec | 875 | { |
617ba13b | 876 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); |
fd2d4291 AM |
877 | ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb); |
878 | ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1; | |
ac27a0ec DK |
879 | |
880 | if (group == first || group == first + 1 || group == last) | |
881 | return 1; | |
882 | return 0; | |
883 | } | |
884 | ||
fd2d4291 AM |
885 | static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb, |
886 | ext4_group_t group) | |
ac27a0ec | 887 | { |
859cb936 | 888 | return ext4_bg_has_super(sb, group) ? EXT4_SB(sb)->s_gdb_count : 0; |
ac27a0ec DK |
889 | } |
890 | ||
891 | /** | |
617ba13b | 892 | * ext4_bg_num_gdb - number of blocks used by the group table in group |
ac27a0ec DK |
893 | * @sb: superblock for filesystem |
894 | * @group: group number to check | |
895 | * | |
896 | * Return the number of blocks used by the group descriptor table | |
897 | * (primary or backup) in this group. In the future there may be a | |
898 | * different number of descriptor blocks in each group. | |
899 | */ | |
fd2d4291 | 900 | unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group) |
ac27a0ec DK |
901 | { |
902 | unsigned long first_meta_bg = | |
617ba13b MC |
903 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg); |
904 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); | |
ac27a0ec | 905 | |
617ba13b | 906 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) || |
ac27a0ec | 907 | metagroup < first_meta_bg) |
af5bc92d | 908 | return ext4_bg_num_gdb_nometa(sb, group); |
ac27a0ec | 909 | |
617ba13b | 910 | return ext4_bg_num_gdb_meta(sb,group); |
ac27a0ec DK |
911 | |
912 | } | |
c2ea3fde | 913 |