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" | |
e21675d4 | 22 | #include "mballoc.h" |
3dcf5451 | 23 | |
0562e0ba JZ |
24 | #include <trace/events/ext4.h> |
25 | ||
ac27a0ec DK |
26 | /* |
27 | * balloc.c contains the blocks allocation and deallocation routines | |
28 | */ | |
29 | ||
72b64b59 AM |
30 | /* |
31 | * Calculate the block group number and offset, given a block number | |
32 | */ | |
33 | void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr, | |
fd2d4291 | 34 | ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp) |
72b64b59 | 35 | { |
8c55e204 | 36 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
72b64b59 AM |
37 | ext4_grpblk_t offset; |
38 | ||
8c55e204 | 39 | blocknr = blocknr - le32_to_cpu(es->s_first_data_block); |
f4e5bc24 | 40 | offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)); |
72b64b59 AM |
41 | if (offsetp) |
42 | *offsetp = offset; | |
43 | if (blockgrpp) | |
8c55e204 | 44 | *blockgrpp = blocknr; |
72b64b59 AM |
45 | |
46 | } | |
47 | ||
0bf7e837 JS |
48 | static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block, |
49 | ext4_group_t block_group) | |
50 | { | |
51 | ext4_group_t actual_group; | |
7477827f | 52 | ext4_get_group_no_and_offset(sb, block, &actual_group, NULL); |
0bf7e837 JS |
53 | if (actual_group == block_group) |
54 | return 1; | |
55 | return 0; | |
56 | } | |
57 | ||
d5b8f310 TT |
58 | /* Return the number of clusters used for file system metadata; this |
59 | * represents the overhead needed by the file system. | |
60 | */ | |
61 | unsigned ext4_num_overhead_clusters(struct super_block *sb, | |
62 | ext4_group_t block_group, | |
63 | struct ext4_group_desc *gdp) | |
0bf7e837 | 64 | { |
d5b8f310 TT |
65 | unsigned num_clusters; |
66 | int block_cluster = -1, inode_cluster = -1, itbl_cluster = -1, i, c; | |
67 | ext4_fsblk_t start = ext4_group_first_block_no(sb, block_group); | |
68 | ext4_fsblk_t itbl_blk; | |
0bf7e837 | 69 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
0bf7e837 | 70 | |
d5b8f310 TT |
71 | /* This is the number of clusters used by the superblock, |
72 | * block group descriptors, and reserved block group | |
73 | * descriptor blocks */ | |
74 | num_clusters = ext4_num_base_meta_clusters(sb, block_group); | |
75 | ||
76 | /* | |
77 | * For the allocation bitmaps and inode table, we first need | |
78 | * to check to see if the block is in the block group. If it | |
79 | * is, then check to see if the cluster is already accounted | |
80 | * for in the clusters used for the base metadata cluster, or | |
81 | * if we can increment the base metadata cluster to include | |
82 | * that block. Otherwise, we will have to track the cluster | |
83 | * used for the allocation bitmap or inode table explicitly. | |
84 | * Normally all of these blocks are contiguous, so the special | |
85 | * case handling shouldn't be necessary except for *very* | |
86 | * unusual file system layouts. | |
87 | */ | |
88 | if (ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp), block_group)) { | |
89 | block_cluster = EXT4_B2C(sbi, (start - | |
90 | ext4_block_bitmap(sb, gdp))); | |
91 | if (block_cluster < num_clusters) | |
92 | block_cluster = -1; | |
93 | else if (block_cluster == num_clusters) { | |
94 | num_clusters++; | |
95 | block_cluster = -1; | |
96 | } | |
97 | } | |
98 | ||
99 | if (ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp), block_group)) { | |
100 | inode_cluster = EXT4_B2C(sbi, | |
101 | start - ext4_inode_bitmap(sb, gdp)); | |
102 | if (inode_cluster < num_clusters) | |
103 | inode_cluster = -1; | |
104 | else if (inode_cluster == num_clusters) { | |
105 | num_clusters++; | |
106 | inode_cluster = -1; | |
107 | } | |
108 | } | |
109 | ||
110 | itbl_blk = ext4_inode_table(sb, gdp); | |
111 | for (i = 0; i < sbi->s_itb_per_group; i++) { | |
112 | if (ext4_block_in_group(sb, itbl_blk + i, block_group)) { | |
113 | c = EXT4_B2C(sbi, start - itbl_blk + i); | |
114 | if ((c < num_clusters) || (c == inode_cluster) || | |
115 | (c == block_cluster) || (c == itbl_cluster)) | |
116 | continue; | |
117 | if (c == num_clusters) { | |
118 | num_clusters++; | |
119 | continue; | |
120 | } | |
121 | num_clusters++; | |
122 | itbl_cluster = c; | |
0bf7e837 JS |
123 | } |
124 | } | |
d5b8f310 TT |
125 | |
126 | if (block_cluster != -1) | |
127 | num_clusters++; | |
128 | if (inode_cluster != -1) | |
129 | num_clusters++; | |
130 | ||
131 | return num_clusters; | |
0bf7e837 | 132 | } |
c2ea3fde | 133 | |
d5b8f310 TT |
134 | static unsigned int num_clusters_in_group(struct super_block *sb, |
135 | ext4_group_t block_group) | |
49f7f9af | 136 | { |
d5b8f310 TT |
137 | unsigned int blocks; |
138 | ||
49f7f9af TT |
139 | if (block_group == ext4_get_groups_count(sb) - 1) { |
140 | /* | |
141 | * Even though mke2fs always initializes the first and | |
142 | * last group, just in case some other tool was used, | |
143 | * we need to make sure we calculate the right free | |
144 | * blocks. | |
145 | */ | |
d5b8f310 | 146 | blocks = ext4_blocks_count(EXT4_SB(sb)->s_es) - |
49f7f9af TT |
147 | ext4_group_first_block_no(sb, block_group); |
148 | } else | |
d5b8f310 TT |
149 | blocks = EXT4_BLOCKS_PER_GROUP(sb); |
150 | return EXT4_NUM_B2C(EXT4_SB(sb), blocks); | |
49f7f9af TT |
151 | } |
152 | ||
fd034a84 TT |
153 | /* Initializes an uninitialized block bitmap */ |
154 | void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh, | |
155 | ext4_group_t block_group, | |
156 | struct ext4_group_desc *gdp) | |
717d50e4 | 157 | { |
d5b8f310 | 158 | unsigned int bit, bit_max; |
717d50e4 | 159 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
fd034a84 TT |
160 | ext4_fsblk_t start, tmp; |
161 | int flex_bg = 0; | |
162 | ||
163 | J_ASSERT_BH(bh, buffer_locked(bh)); | |
164 | ||
165 | /* If checksum is bad mark all blocks used to prevent allocation | |
166 | * essentially implementing a per-group read-only flag. */ | |
167 | if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { | |
168 | ext4_error(sb, "Checksum bad for group %u", block_group); | |
169 | ext4_free_blks_set(sb, gdp, 0); | |
170 | ext4_free_inodes_set(sb, gdp, 0); | |
171 | ext4_itable_unused_set(sb, gdp, 0); | |
172 | memset(bh->b_data, 0xff, sb->s_blocksize); | |
173 | return; | |
717d50e4 | 174 | } |
fd034a84 | 175 | memset(bh->b_data, 0, sb->s_blocksize); |
717d50e4 | 176 | |
d5b8f310 | 177 | bit_max = ext4_num_base_meta_clusters(sb, block_group); |
fd034a84 TT |
178 | for (bit = 0; bit < bit_max; bit++) |
179 | ext4_set_bit(bit, bh->b_data); | |
d00a6d7b | 180 | |
fd034a84 | 181 | start = ext4_group_first_block_no(sb, block_group); |
717d50e4 | 182 | |
fd034a84 TT |
183 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) |
184 | flex_bg = 1; | |
717d50e4 | 185 | |
fd034a84 TT |
186 | /* Set bits for block and inode bitmaps, and inode table */ |
187 | tmp = ext4_block_bitmap(sb, gdp); | |
188 | if (!flex_bg || ext4_block_in_group(sb, tmp, block_group)) | |
d5b8f310 | 189 | ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data); |
717d50e4 | 190 | |
fd034a84 TT |
191 | tmp = ext4_inode_bitmap(sb, gdp); |
192 | if (!flex_bg || ext4_block_in_group(sb, tmp, block_group)) | |
d5b8f310 | 193 | ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data); |
0bf7e837 | 194 | |
fd034a84 TT |
195 | tmp = ext4_inode_table(sb, gdp); |
196 | for (; tmp < ext4_inode_table(sb, gdp) + | |
197 | sbi->s_itb_per_group; tmp++) { | |
0bf7e837 | 198 | if (!flex_bg || ext4_block_in_group(sb, tmp, block_group)) |
d5b8f310 | 199 | ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data); |
717d50e4 | 200 | } |
d5b8f310 | 201 | |
fd034a84 TT |
202 | /* |
203 | * Also if the number of blocks within the group is less than | |
204 | * the blocksize * 8 ( which is the size of bitmap ), set rest | |
205 | * of the block bitmap to 1 | |
206 | */ | |
d5b8f310 | 207 | ext4_mark_bitmap_end(num_clusters_in_group(sb, block_group), |
fd034a84 | 208 | sb->s_blocksize * 8, bh->b_data); |
717d50e4 AD |
209 | } |
210 | ||
fd034a84 TT |
211 | /* Return the number of free blocks in a block group. It is used when |
212 | * the block bitmap is uninitialized, so we can't just count the bits | |
213 | * in the bitmap. */ | |
214 | unsigned ext4_free_blocks_after_init(struct super_block *sb, | |
215 | ext4_group_t block_group, | |
216 | struct ext4_group_desc *gdp) | |
217 | { | |
d5b8f310 TT |
218 | return num_clusters_in_group(sb, block_group) - |
219 | ext4_num_overhead_clusters(sb, block_group, gdp); | |
fd034a84 | 220 | } |
717d50e4 | 221 | |
ac27a0ec DK |
222 | /* |
223 | * The free blocks are managed by bitmaps. A file system contains several | |
224 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap | |
225 | * block for inodes, N blocks for the inode table and data blocks. | |
226 | * | |
227 | * The file system contains group descriptors which are located after the | |
228 | * super block. Each descriptor contains the number of the bitmap block and | |
229 | * the free blocks count in the block. The descriptors are loaded in memory | |
e627432c | 230 | * when a file system is mounted (see ext4_fill_super). |
ac27a0ec DK |
231 | */ |
232 | ||
ac27a0ec | 233 | /** |
617ba13b | 234 | * ext4_get_group_desc() -- load group descriptor from disk |
ac27a0ec DK |
235 | * @sb: super block |
236 | * @block_group: given block group | |
237 | * @bh: pointer to the buffer head to store the block | |
238 | * group descriptor | |
239 | */ | |
af5bc92d | 240 | struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb, |
fd2d4291 | 241 | ext4_group_t block_group, |
af5bc92d | 242 | struct buffer_head **bh) |
ac27a0ec | 243 | { |
498e5f24 TT |
244 | unsigned int group_desc; |
245 | unsigned int offset; | |
8df9675f | 246 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
af5bc92d | 247 | struct ext4_group_desc *desc; |
617ba13b | 248 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
ac27a0ec | 249 | |
8df9675f | 250 | if (block_group >= ngroups) { |
12062ddd ES |
251 | ext4_error(sb, "block_group >= groups_count - block_group = %u," |
252 | " groups_count = %u", block_group, ngroups); | |
ac27a0ec DK |
253 | |
254 | return NULL; | |
255 | } | |
ac27a0ec | 256 | |
617ba13b MC |
257 | group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb); |
258 | offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1); | |
ac27a0ec | 259 | if (!sbi->s_group_desc[group_desc]) { |
12062ddd | 260 | ext4_error(sb, "Group descriptor not loaded - " |
498e5f24 | 261 | "block_group = %u, group_desc = %u, desc = %u", |
af5bc92d | 262 | block_group, group_desc, offset); |
ac27a0ec DK |
263 | return NULL; |
264 | } | |
265 | ||
0d1ee42f AR |
266 | desc = (struct ext4_group_desc *)( |
267 | (__u8 *)sbi->s_group_desc[group_desc]->b_data + | |
268 | offset * EXT4_DESC_SIZE(sb)); | |
ac27a0ec DK |
269 | if (bh) |
270 | *bh = sbi->s_group_desc[group_desc]; | |
0d1ee42f | 271 | return desc; |
ac27a0ec DK |
272 | } |
273 | ||
abcb2947 AK |
274 | static int ext4_valid_block_bitmap(struct super_block *sb, |
275 | struct ext4_group_desc *desc, | |
276 | unsigned int block_group, | |
277 | struct buffer_head *bh) | |
278 | { | |
279 | ext4_grpblk_t offset; | |
280 | ext4_grpblk_t next_zero_bit; | |
281 | ext4_fsblk_t bitmap_blk; | |
282 | ext4_fsblk_t group_first_block; | |
283 | ||
284 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) { | |
285 | /* with FLEX_BG, the inode/block bitmaps and itable | |
286 | * blocks may not be in the group at all | |
287 | * so the bitmap validation will be skipped for those groups | |
288 | * or it has to also read the block group where the bitmaps | |
289 | * are located to verify they are set. | |
290 | */ | |
291 | return 1; | |
292 | } | |
293 | group_first_block = ext4_group_first_block_no(sb, block_group); | |
294 | ||
295 | /* check whether block bitmap block number is set */ | |
296 | bitmap_blk = ext4_block_bitmap(sb, desc); | |
297 | offset = bitmap_blk - group_first_block; | |
298 | if (!ext4_test_bit(offset, bh->b_data)) | |
299 | /* bad block bitmap */ | |
300 | goto err_out; | |
301 | ||
302 | /* check whether the inode bitmap block number is set */ | |
303 | bitmap_blk = ext4_inode_bitmap(sb, desc); | |
304 | offset = bitmap_blk - group_first_block; | |
305 | if (!ext4_test_bit(offset, bh->b_data)) | |
306 | /* bad block bitmap */ | |
307 | goto err_out; | |
308 | ||
309 | /* check whether the inode table block number is set */ | |
310 | bitmap_blk = ext4_inode_table(sb, desc); | |
311 | offset = bitmap_blk - group_first_block; | |
312 | next_zero_bit = ext4_find_next_zero_bit(bh->b_data, | |
313 | offset + EXT4_SB(sb)->s_itb_per_group, | |
314 | offset); | |
315 | if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group) | |
316 | /* good bitmap for inode tables */ | |
317 | return 1; | |
318 | ||
319 | err_out: | |
12062ddd | 320 | ext4_error(sb, "Invalid block bitmap - block_group = %d, block = %llu", |
abcb2947 AK |
321 | block_group, bitmap_blk); |
322 | return 0; | |
323 | } | |
ac27a0ec | 324 | /** |
574ca174 | 325 | * ext4_read_block_bitmap() |
ac27a0ec DK |
326 | * @sb: super block |
327 | * @block_group: given block group | |
328 | * | |
abcb2947 AK |
329 | * Read the bitmap for a given block_group,and validate the |
330 | * bits for block/inode/inode tables are set in the bitmaps | |
ac27a0ec DK |
331 | * |
332 | * Return buffer_head on success or NULL in case of failure. | |
333 | */ | |
717d50e4 | 334 | struct buffer_head * |
574ca174 | 335 | ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group) |
ac27a0ec | 336 | { |
af5bc92d TT |
337 | struct ext4_group_desc *desc; |
338 | struct buffer_head *bh = NULL; | |
7c9e69fa | 339 | ext4_fsblk_t bitmap_blk; |
ac27a0ec | 340 | |
717d50e4 | 341 | desc = ext4_get_group_desc(sb, block_group, NULL); |
ac27a0ec | 342 | if (!desc) |
7c9e69fa AK |
343 | return NULL; |
344 | bitmap_blk = ext4_block_bitmap(sb, desc); | |
abcb2947 AK |
345 | bh = sb_getblk(sb, bitmap_blk); |
346 | if (unlikely(!bh)) { | |
12062ddd | 347 | ext4_error(sb, "Cannot read block bitmap - " |
a9df9a49 | 348 | "block_group = %u, block_bitmap = %llu", |
e29d1cde | 349 | block_group, bitmap_blk); |
abcb2947 AK |
350 | return NULL; |
351 | } | |
2ccb5fb9 AK |
352 | |
353 | if (bitmap_uptodate(bh)) | |
abcb2947 AK |
354 | return bh; |
355 | ||
c806e68f | 356 | lock_buffer(bh); |
2ccb5fb9 AK |
357 | if (bitmap_uptodate(bh)) { |
358 | unlock_buffer(bh); | |
359 | return bh; | |
360 | } | |
955ce5f5 | 361 | ext4_lock_group(sb, block_group); |
717d50e4 | 362 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
abcb2947 | 363 | ext4_init_block_bitmap(sb, bh, block_group, desc); |
2ccb5fb9 | 364 | set_bitmap_uptodate(bh); |
abcb2947 | 365 | set_buffer_uptodate(bh); |
955ce5f5 | 366 | ext4_unlock_group(sb, block_group); |
3300beda | 367 | unlock_buffer(bh); |
abcb2947 | 368 | return bh; |
717d50e4 | 369 | } |
955ce5f5 | 370 | ext4_unlock_group(sb, block_group); |
2ccb5fb9 AK |
371 | if (buffer_uptodate(bh)) { |
372 | /* | |
373 | * if not uninit if bh is uptodate, | |
374 | * bitmap is also uptodate | |
375 | */ | |
376 | set_bitmap_uptodate(bh); | |
377 | unlock_buffer(bh); | |
378 | return bh; | |
379 | } | |
380 | /* | |
381 | * submit the buffer_head for read. We can | |
382 | * safely mark the bitmap as uptodate now. | |
383 | * We do it here so the bitmap uptodate bit | |
384 | * get set with buffer lock held. | |
385 | */ | |
0562e0ba | 386 | trace_ext4_read_block_bitmap_load(sb, block_group); |
2ccb5fb9 | 387 | set_bitmap_uptodate(bh); |
abcb2947 AK |
388 | if (bh_submit_read(bh) < 0) { |
389 | put_bh(bh); | |
12062ddd | 390 | ext4_error(sb, "Cannot read block bitmap - " |
a9df9a49 | 391 | "block_group = %u, block_bitmap = %llu", |
e29d1cde | 392 | block_group, bitmap_blk); |
abcb2947 AK |
393 | return NULL; |
394 | } | |
519deca0 AK |
395 | ext4_valid_block_bitmap(sb, desc, block_group, bh); |
396 | /* | |
397 | * file system mounted not to panic on error, | |
398 | * continue with corrupt bitmap | |
399 | */ | |
ac27a0ec DK |
400 | return bh; |
401 | } | |
ac27a0ec | 402 | |
8c3bf8a0 ES |
403 | /** |
404 | * ext4_has_free_blocks() | |
405 | * @sbi: in-core super block structure. | |
406 | * @nblocks: number of needed blocks | |
407 | * | |
408 | * Check if filesystem has nblocks free & available for allocation. | |
409 | * On success return 1, return 0 on failure. | |
410 | */ | |
55f020db AH |
411 | static int ext4_has_free_blocks(struct ext4_sb_info *sbi, |
412 | s64 nblocks, unsigned int flags) | |
a30d542a | 413 | { |
a996031c | 414 | s64 free_blocks, dirty_blocks, root_blocks; |
a30d542a | 415 | struct percpu_counter *fbc = &sbi->s_freeblocks_counter; |
6bc6e63f | 416 | struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter; |
a30d542a | 417 | |
6bc6e63f AK |
418 | free_blocks = percpu_counter_read_positive(fbc); |
419 | dirty_blocks = percpu_counter_read_positive(dbc); | |
a996031c | 420 | root_blocks = ext4_r_blocks_count(sbi->s_es); |
a30d542a | 421 | |
6bc6e63f AK |
422 | if (free_blocks - (nblocks + root_blocks + dirty_blocks) < |
423 | EXT4_FREEBLOCKS_WATERMARK) { | |
02d21168 AM |
424 | free_blocks = percpu_counter_sum_positive(fbc); |
425 | dirty_blocks = percpu_counter_sum_positive(dbc); | |
6bc6e63f AK |
426 | } |
427 | /* Check whether we have space after | |
a996031c | 428 | * accounting for current dirty blocks & root reserved blocks. |
6bc6e63f | 429 | */ |
a996031c ES |
430 | if (free_blocks >= ((root_blocks + nblocks) + dirty_blocks)) |
431 | return 1; | |
a30d542a | 432 | |
a996031c | 433 | /* Hm, nope. Are (enough) root reserved blocks available? */ |
4c9c544e | 434 | if (sbi->s_resuid == current_fsuid() || |
a996031c | 435 | ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) || |
55f020db AH |
436 | capable(CAP_SYS_RESOURCE) || |
437 | (flags & EXT4_MB_USE_ROOT_BLOCKS)) { | |
438 | ||
a996031c ES |
439 | if (free_blocks >= (nblocks + dirty_blocks)) |
440 | return 1; | |
441 | } | |
442 | ||
443 | return 0; | |
a30d542a AK |
444 | } |
445 | ||
8c3bf8a0 | 446 | int ext4_claim_free_blocks(struct ext4_sb_info *sbi, |
55f020db | 447 | s64 nblocks, unsigned int flags) |
ac27a0ec | 448 | { |
55f020db | 449 | if (ext4_has_free_blocks(sbi, nblocks, flags)) { |
8c3bf8a0 | 450 | percpu_counter_add(&sbi->s_dirtyblocks_counter, nblocks); |
16eb7295 | 451 | return 0; |
8c3bf8a0 ES |
452 | } else |
453 | return -ENOSPC; | |
a30d542a | 454 | } |
07031431 | 455 | |
ac27a0ec | 456 | /** |
617ba13b | 457 | * ext4_should_retry_alloc() |
ac27a0ec DK |
458 | * @sb: super block |
459 | * @retries number of attemps has been made | |
460 | * | |
617ba13b | 461 | * ext4_should_retry_alloc() is called when ENOSPC is returned, and if |
ac27a0ec | 462 | * it is profitable to retry the operation, this function will wait |
25985edc | 463 | * for the current or committing transaction to complete, and then |
ac27a0ec DK |
464 | * return TRUE. |
465 | * | |
466 | * if the total number of retries exceed three times, return FALSE. | |
467 | */ | |
617ba13b | 468 | int ext4_should_retry_alloc(struct super_block *sb, int *retries) |
ac27a0ec | 469 | { |
55f020db | 470 | if (!ext4_has_free_blocks(EXT4_SB(sb), 1, 0) || |
8f64b32e ES |
471 | (*retries)++ > 3 || |
472 | !EXT4_SB(sb)->s_journal) | |
ac27a0ec DK |
473 | return 0; |
474 | ||
475 | jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id); | |
476 | ||
dab291af | 477 | return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal); |
ac27a0ec DK |
478 | } |
479 | ||
654b4908 | 480 | /* |
d2a17637 | 481 | * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks |
654b4908 AK |
482 | * |
483 | * @handle: handle to this transaction | |
484 | * @inode: file inode | |
485 | * @goal: given target block(filesystem wide) | |
97df5d15 | 486 | * @count: pointer to total number of blocks needed |
654b4908 AK |
487 | * @errp: error code |
488 | * | |
97df5d15 | 489 | * Return 1st allocated block number on success, *count stores total account |
d2a17637 | 490 | * error stores in errp pointer |
654b4908 | 491 | */ |
d2a17637 | 492 | ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode, |
55f020db AH |
493 | ext4_fsblk_t goal, unsigned int flags, |
494 | unsigned long *count, int *errp) | |
654b4908 | 495 | { |
97df5d15 | 496 | struct ext4_allocation_request ar; |
d2a17637 | 497 | ext4_fsblk_t ret; |
97df5d15 TT |
498 | |
499 | memset(&ar, 0, sizeof(ar)); | |
500 | /* Fill with neighbour allocated blocks */ | |
501 | ar.inode = inode; | |
502 | ar.goal = goal; | |
503 | ar.len = count ? *count : 1; | |
55f020db | 504 | ar.flags = flags; |
97df5d15 TT |
505 | |
506 | ret = ext4_mb_new_blocks(handle, &ar, errp); | |
507 | if (count) | |
508 | *count = ar.len; | |
d2a17637 | 509 | /* |
72b8ab9d ES |
510 | * Account for the allocated meta blocks. We will never |
511 | * fail EDQUOT for metdata, but we do account for it. | |
d2a17637 | 512 | */ |
f2321097 TT |
513 | if (!(*errp) && |
514 | ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) { | |
d2a17637 | 515 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
97df5d15 | 516 | EXT4_I(inode)->i_allocated_meta_blocks += ar.len; |
d2a17637 | 517 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
72b8ab9d | 518 | dquot_alloc_block_nofail(inode, ar.len); |
d2a17637 MC |
519 | } |
520 | return ret; | |
654b4908 AK |
521 | } |
522 | ||
ac27a0ec | 523 | /** |
617ba13b | 524 | * ext4_count_free_blocks() -- count filesystem free blocks |
ac27a0ec DK |
525 | * @sb: superblock |
526 | * | |
527 | * Adds up the number of free blocks from each block group. | |
528 | */ | |
617ba13b | 529 | ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb) |
ac27a0ec | 530 | { |
617ba13b MC |
531 | ext4_fsblk_t desc_count; |
532 | struct ext4_group_desc *gdp; | |
fd2d4291 | 533 | ext4_group_t i; |
8df9675f | 534 | ext4_group_t ngroups = ext4_get_groups_count(sb); |
617ba13b MC |
535 | #ifdef EXT4FS_DEBUG |
536 | struct ext4_super_block *es; | |
537 | ext4_fsblk_t bitmap_count; | |
498e5f24 | 538 | unsigned int x; |
ac27a0ec DK |
539 | struct buffer_head *bitmap_bh = NULL; |
540 | ||
617ba13b | 541 | es = EXT4_SB(sb)->s_es; |
ac27a0ec DK |
542 | desc_count = 0; |
543 | bitmap_count = 0; | |
544 | gdp = NULL; | |
545 | ||
ac27a0ec | 546 | for (i = 0; i < ngroups; i++) { |
617ba13b | 547 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
548 | if (!gdp) |
549 | continue; | |
9fd9784c | 550 | desc_count += ext4_free_blks_count(sb, gdp); |
ac27a0ec | 551 | brelse(bitmap_bh); |
574ca174 | 552 | bitmap_bh = ext4_read_block_bitmap(sb, i); |
ac27a0ec DK |
553 | if (bitmap_bh == NULL) |
554 | continue; | |
555 | ||
617ba13b | 556 | x = ext4_count_free(bitmap_bh, sb->s_blocksize); |
9fd9784c TLSC |
557 | printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n", |
558 | i, ext4_free_blks_count(sb, gdp), x); | |
ac27a0ec DK |
559 | bitmap_count += x; |
560 | } | |
561 | brelse(bitmap_bh); | |
4776004f TT |
562 | printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu" |
563 | ", computed = %llu, %llu\n", ext4_free_blocks_count(es), | |
564 | desc_count, bitmap_count); | |
ac27a0ec DK |
565 | return bitmap_count; |
566 | #else | |
567 | desc_count = 0; | |
ac27a0ec | 568 | for (i = 0; i < ngroups; i++) { |
617ba13b | 569 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
570 | if (!gdp) |
571 | continue; | |
560671a0 | 572 | desc_count += ext4_free_blks_count(sb, gdp); |
ac27a0ec DK |
573 | } |
574 | ||
575 | return desc_count; | |
576 | #endif | |
577 | } | |
578 | ||
fd2d4291 | 579 | static inline int test_root(ext4_group_t a, int b) |
ac27a0ec DK |
580 | { |
581 | int num = b; | |
582 | ||
583 | while (a > num) | |
584 | num *= b; | |
585 | return num == a; | |
586 | } | |
587 | ||
fd2d4291 | 588 | static int ext4_group_sparse(ext4_group_t group) |
ac27a0ec DK |
589 | { |
590 | if (group <= 1) | |
591 | return 1; | |
592 | if (!(group & 1)) | |
593 | return 0; | |
594 | return (test_root(group, 7) || test_root(group, 5) || | |
595 | test_root(group, 3)); | |
596 | } | |
597 | ||
598 | /** | |
617ba13b | 599 | * ext4_bg_has_super - number of blocks used by the superblock in group |
ac27a0ec DK |
600 | * @sb: superblock for filesystem |
601 | * @group: group number to check | |
602 | * | |
603 | * Return the number of blocks used by the superblock (primary or backup) | |
604 | * in this group. Currently this will be only 0 or 1. | |
605 | */ | |
fd2d4291 | 606 | int ext4_bg_has_super(struct super_block *sb, ext4_group_t group) |
ac27a0ec | 607 | { |
617ba13b MC |
608 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
609 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) && | |
610 | !ext4_group_sparse(group)) | |
ac27a0ec DK |
611 | return 0; |
612 | return 1; | |
613 | } | |
614 | ||
fd2d4291 AM |
615 | static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb, |
616 | ext4_group_t group) | |
ac27a0ec | 617 | { |
617ba13b | 618 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); |
fd2d4291 AM |
619 | ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb); |
620 | ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1; | |
ac27a0ec DK |
621 | |
622 | if (group == first || group == first + 1 || group == last) | |
623 | return 1; | |
624 | return 0; | |
625 | } | |
626 | ||
fd2d4291 AM |
627 | static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb, |
628 | ext4_group_t group) | |
ac27a0ec | 629 | { |
8dadb198 TT |
630 | if (!ext4_bg_has_super(sb, group)) |
631 | return 0; | |
632 | ||
633 | if (EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG)) | |
634 | return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg); | |
635 | else | |
636 | return EXT4_SB(sb)->s_gdb_count; | |
ac27a0ec DK |
637 | } |
638 | ||
639 | /** | |
617ba13b | 640 | * ext4_bg_num_gdb - number of blocks used by the group table in group |
ac27a0ec DK |
641 | * @sb: superblock for filesystem |
642 | * @group: group number to check | |
643 | * | |
644 | * Return the number of blocks used by the group descriptor table | |
645 | * (primary or backup) in this group. In the future there may be a | |
646 | * different number of descriptor blocks in each group. | |
647 | */ | |
fd2d4291 | 648 | unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group) |
ac27a0ec DK |
649 | { |
650 | unsigned long first_meta_bg = | |
617ba13b MC |
651 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg); |
652 | unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb); | |
ac27a0ec | 653 | |
617ba13b | 654 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) || |
ac27a0ec | 655 | metagroup < first_meta_bg) |
af5bc92d | 656 | return ext4_bg_num_gdb_nometa(sb, group); |
ac27a0ec | 657 | |
617ba13b | 658 | return ext4_bg_num_gdb_meta(sb,group); |
ac27a0ec DK |
659 | |
660 | } | |
c2ea3fde | 661 | |
49f7f9af | 662 | /* |
d5b8f310 | 663 | * This function returns the number of file system metadata clusters at |
49f7f9af TT |
664 | * the beginning of a block group, including the reserved gdt blocks. |
665 | */ | |
d5b8f310 TT |
666 | unsigned ext4_num_base_meta_clusters(struct super_block *sb, |
667 | ext4_group_t block_group) | |
49f7f9af TT |
668 | { |
669 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
d5b8f310 | 670 | unsigned num; |
49f7f9af TT |
671 | |
672 | /* Check for superblock and gdt backups in this group */ | |
673 | num = ext4_bg_has_super(sb, block_group); | |
674 | ||
675 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) || | |
676 | block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) * | |
677 | sbi->s_desc_per_block) { | |
678 | if (num) { | |
679 | num += ext4_bg_num_gdb(sb, block_group); | |
680 | num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks); | |
681 | } | |
682 | } else { /* For META_BG_BLOCK_GROUPS */ | |
683 | num += ext4_bg_num_gdb(sb, block_group); | |
684 | } | |
d5b8f310 | 685 | return EXT4_NUM_B2C(sbi, num); |
49f7f9af | 686 | } |
f86186b4 ES |
687 | /** |
688 | * ext4_inode_to_goal_block - return a hint for block allocation | |
689 | * @inode: inode for block allocation | |
690 | * | |
691 | * Return the ideal location to start allocating blocks for a | |
692 | * newly created inode. | |
693 | */ | |
694 | ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode) | |
695 | { | |
696 | struct ext4_inode_info *ei = EXT4_I(inode); | |
697 | ext4_group_t block_group; | |
698 | ext4_grpblk_t colour; | |
699 | int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); | |
700 | ext4_fsblk_t bg_start; | |
701 | ext4_fsblk_t last_block; | |
702 | ||
703 | block_group = ei->i_block_group; | |
704 | if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { | |
705 | /* | |
706 | * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME | |
707 | * block groups per flexgroup, reserve the first block | |
708 | * group for directories and special files. Regular | |
709 | * files will start at the second block group. This | |
710 | * tends to speed up directory access and improves | |
711 | * fsck times. | |
712 | */ | |
713 | block_group &= ~(flex_size-1); | |
714 | if (S_ISREG(inode->i_mode)) | |
715 | block_group++; | |
716 | } | |
717 | bg_start = ext4_group_first_block_no(inode->i_sb, block_group); | |
718 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; | |
719 | ||
720 | /* | |
721 | * If we are doing delayed allocation, we don't need take | |
722 | * colour into account. | |
723 | */ | |
724 | if (test_opt(inode->i_sb, DELALLOC)) | |
725 | return bg_start; | |
726 | ||
727 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) | |
728 | colour = (current->pid % 16) * | |
729 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); | |
730 | else | |
731 | colour = (current->pid % 16) * ((last_block - bg_start) / 16); | |
732 | return bg_start + colour; | |
733 | } | |
734 |