blob: 1c6d777b35a298a7148de5ef175f5b7f2258d605 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/balloc.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
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>
Mingming Caodab291a2006-10-11 01:21:01 -070017#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070018#include <linux/quotaops.h>
19#include <linux/buffer_head.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040020#include "ext4.h"
21#include "ext4_jbd2.h"
Aneesh Kumar K.Ve21675d2009-01-05 21:36:02 -050022#include "mballoc.h"
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040023
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040024#include <trace/events/ext4.h>
25
Dave Kleikampac27a0e2006-10-11 01:20:50 -070026/*
27 * balloc.c contains the blocks allocation and deallocation routines
28 */
29
30/*
Andrew Morton72b64b52006-10-11 01:21:18 -070031 * Calculate the block group number and offset, given a block number
32 */
33void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
Avantika Mathurfd2d4292008-01-28 23:58:27 -050034 ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
Andrew Morton72b64b52006-10-11 01:21:18 -070035{
Dave Kleikamp8c55e202007-05-24 13:04:54 -040036 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Andrew Morton72b64b52006-10-11 01:21:18 -070037 ext4_grpblk_t offset;
38
Dave Kleikamp8c55e202007-05-24 13:04:54 -040039 blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
Andrew Mortonf4e5bc22006-10-11 01:21:19 -070040 offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb));
Andrew Morton72b64b52006-10-11 01:21:18 -070041 if (offsetp)
42 *offsetp = offset;
43 if (blockgrpp)
Dave Kleikamp8c55e202007-05-24 13:04:54 -040044 *blockgrpp = blocknr;
Andrew Morton72b64b52006-10-11 01:21:18 -070045
46}
47
Jose R. Santos0bf7e832008-06-03 14:07:29 -040048static 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;
Aneesh Kumar K.V74778272008-07-11 19:27:31 -040052 ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
Jose R. Santos0bf7e832008-06-03 14:07:29 -040053 if (actual_group == block_group)
54 return 1;
55 return 0;
56}
57
Theodore Ts'od5b8f312011-09-09 18:44:51 -040058/* Return the number of clusters used for file system metadata; this
59 * represents the overhead needed by the file system.
60 */
61unsigned ext4_num_overhead_clusters(struct super_block *sb,
62 ext4_group_t block_group,
63 struct ext4_group_desc *gdp)
Jose R. Santos0bf7e832008-06-03 14:07:29 -040064{
Theodore Ts'od5b8f312011-09-09 18:44:51 -040065 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;
Jose R. Santos0bf7e832008-06-03 14:07:29 -040069 struct ext4_sb_info *sbi = EXT4_SB(sb);
Jose R. Santos0bf7e832008-06-03 14:07:29 -040070
Theodore Ts'od5b8f312011-09-09 18:44:51 -040071 /* 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);
Jose R. Santos0bf7e832008-06-03 14:07:29 -040075
Theodore Ts'od5b8f312011-09-09 18:44:51 -040076 /*
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;
Jose R. Santos0bf7e832008-06-03 14:07:29 -040096 }
97 }
Theodore Ts'od5b8f312011-09-09 18:44:51 -040098
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;
123 }
124 }
125
126 if (block_cluster != -1)
127 num_clusters++;
128 if (inode_cluster != -1)
129 num_clusters++;
130
131 return num_clusters;
Jose R. Santos0bf7e832008-06-03 14:07:29 -0400132}
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400133
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400134static unsigned int num_clusters_in_group(struct super_block *sb,
135 ext4_group_t block_group)
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400136{
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400137 unsigned int blocks;
138
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400139 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 */
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400146 blocks = ext4_blocks_count(EXT4_SB(sb)->s_es) -
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400147 ext4_group_first_block_no(sb, block_group);
148 } else
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400149 blocks = EXT4_BLOCKS_PER_GROUP(sb);
150 return EXT4_NUM_B2C(EXT4_SB(sb), blocks);
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400151}
152
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400153/* Initializes an uninitialized block bitmap */
154void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
155 ext4_group_t block_group,
156 struct ext4_group_desc *gdp)
Andreas Dilger717d50e2007-10-16 18:38:25 -0400157{
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400158 unsigned int bit, bit_max;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400159 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400160 ext4_fsblk_t start, tmp;
161 int flex_bg = 0;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400162
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400163 J_ASSERT_BH(bh, buffer_locked(bh));
Andreas Dilger717d50e2007-10-16 18:38:25 -0400164
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400165 /* 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;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400174 }
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400175 memset(bh->b_data, 0, sb->s_blocksize);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400176
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400177 bit_max = ext4_num_base_meta_clusters(sb, block_group);
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400178 for (bit = 0; bit < bit_max; bit++)
179 ext4_set_bit(bit, bh->b_data);
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400180
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400181 start = ext4_group_first_block_no(sb, block_group);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400182
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400183 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
184 flex_bg = 1;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400185
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400186 /* 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))
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400189 ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400190
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400191 tmp = ext4_inode_bitmap(sb, gdp);
192 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400193 ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400194
195 tmp = ext4_inode_table(sb, gdp);
196 for (; tmp < ext4_inode_table(sb, gdp) +
197 sbi->s_itb_per_group; tmp++) {
Jose R. Santos0bf7e832008-06-03 14:07:29 -0400198 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400199 ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400200 }
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400201
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400202 /*
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 */
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400207 ext4_mark_bitmap_end(num_clusters_in_group(sb, block_group),
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400208 sb->s_blocksize * 8, bh->b_data);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400209}
210
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400211/* 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. */
214unsigned ext4_free_blocks_after_init(struct super_block *sb,
215 ext4_group_t block_group,
216 struct ext4_group_desc *gdp)
217{
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400218 return num_clusters_in_group(sb, block_group) -
219 ext4_num_overhead_clusters(sb, block_group, gdp);
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400220}
Andreas Dilger717d50e2007-10-16 18:38:25 -0400221
Andrew Morton72b64b52006-10-11 01:21:18 -0700222/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223 * 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
Aneesh Kumar K.Ve6274322007-02-20 13:57:58 -0800230 * when a file system is mounted (see ext4_fill_super).
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 */
232
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233/**
Mingming Cao617ba132006-10-11 01:20:53 -0700234 * ext4_get_group_desc() -- load group descriptor from disk
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700235 * @sb: super block
236 * @block_group: given block group
237 * @bh: pointer to the buffer head to store the block
238 * group descriptor
239 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400240struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500241 ext4_group_t block_group,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400242 struct buffer_head **bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500244 unsigned int group_desc;
245 unsigned int offset;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400246 ext4_group_t ngroups = ext4_get_groups_count(sb);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400247 struct ext4_group_desc *desc;
Mingming Cao617ba132006-10-11 01:20:53 -0700248 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249
Theodore Ts'o8df96752009-05-01 08:50:38 -0400250 if (block_group >= ngroups) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500251 ext4_error(sb, "block_group >= groups_count - block_group = %u,"
252 " groups_count = %u", block_group, ngroups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253
254 return NULL;
255 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256
Mingming Cao617ba132006-10-11 01:20:53 -0700257 group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
258 offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 if (!sbi->s_group_desc[group_desc]) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500260 ext4_error(sb, "Group descriptor not loaded - "
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500261 "block_group = %u, group_desc = %u, desc = %u",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400262 block_group, group_desc, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263 return NULL;
264 }
265
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -0700266 desc = (struct ext4_group_desc *)(
267 (__u8 *)sbi->s_group_desc[group_desc]->b_data +
268 offset * EXT4_DESC_SIZE(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700269 if (bh)
270 *bh = sbi->s_group_desc[group_desc];
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -0700271 return desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272}
273
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500274static 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
319err_out:
Eric Sandeen12062dd2010-02-15 14:19:27 -0500320 ext4_error(sb, "Invalid block bitmap - block_group = %d, block = %llu",
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500321 block_group, bitmap_blk);
322 return 0;
323}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324/**
Theodore Ts'o574ca172008-07-11 19:27:31 -0400325 * ext4_read_block_bitmap()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700326 * @sb: super block
327 * @block_group: given block group
328 *
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500329 * Read the bitmap for a given block_group,and validate the
330 * bits for block/inode/inode tables are set in the bitmaps
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 *
332 * Return buffer_head on success or NULL in case of failure.
333 */
Andreas Dilger717d50e2007-10-16 18:38:25 -0400334struct buffer_head *
Theodore Ts'o574ca172008-07-11 19:27:31 -0400335ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400337 struct ext4_group_desc *desc;
338 struct buffer_head *bh = NULL;
Aneesh Kumar K.V7c9e69f2007-10-16 23:27:02 -0700339 ext4_fsblk_t bitmap_blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700340
Andreas Dilger717d50e2007-10-16 18:38:25 -0400341 desc = ext4_get_group_desc(sb, block_group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700342 if (!desc)
Aneesh Kumar K.V7c9e69f2007-10-16 23:27:02 -0700343 return NULL;
344 bitmap_blk = ext4_block_bitmap(sb, desc);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500345 bh = sb_getblk(sb, bitmap_blk);
346 if (unlikely(!bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500347 ext4_error(sb, "Cannot read block bitmap - "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500348 "block_group = %u, block_bitmap = %llu",
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400349 block_group, bitmap_blk);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500350 return NULL;
351 }
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500352
353 if (bitmap_uptodate(bh))
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500354 return bh;
355
Frederic Bohec806e682008-10-10 08:09:18 -0400356 lock_buffer(bh);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500357 if (bitmap_uptodate(bh)) {
358 unlock_buffer(bh);
359 return bh;
360 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400361 ext4_lock_group(sb, block_group);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500362 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
363 ext4_init_block_bitmap(sb, bh, block_group, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500364 set_bitmap_uptodate(bh);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500365 set_buffer_uptodate(bh);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400366 ext4_unlock_group(sb, block_group);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500367 unlock_buffer(bh);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500368 return bh;
369 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400370 ext4_unlock_group(sb, block_group);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500371 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 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400386 trace_ext4_read_block_bitmap_load(sb, block_group);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500387 set_bitmap_uptodate(bh);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500388 if (bh_submit_read(bh) < 0) {
389 put_bh(bh);
Eric Sandeen12062dd2010-02-15 14:19:27 -0500390 ext4_error(sb, "Cannot read block bitmap - "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500391 "block_group = %u, block_bitmap = %llu",
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400392 block_group, bitmap_blk);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500393 return NULL;
394 }
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -0400395 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 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400 return bh;
401}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402
403/**
Eric Sandeen8c3bf8a2008-10-28 00:08:12 -0400404 * 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 */
Allison Henderson55f020d2011-05-25 07:41:26 -0400411static int ext4_has_free_blocks(struct ext4_sb_info *sbi,
412 s64 nblocks, unsigned int flags)
Aneesh Kumar K.Va30d5422008-10-09 10:56:23 -0400413{
Eric Sandeena9960312008-10-28 00:08:17 -0400414 s64 free_blocks, dirty_blocks, root_blocks;
Aneesh Kumar K.Va30d5422008-10-09 10:56:23 -0400415 struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400416 struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
Aneesh Kumar K.Va30d5422008-10-09 10:56:23 -0400417
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400418 free_blocks = percpu_counter_read_positive(fbc);
419 dirty_blocks = percpu_counter_read_positive(dbc);
Eric Sandeena9960312008-10-28 00:08:17 -0400420 root_blocks = ext4_r_blocks_count(sbi->s_es);
Aneesh Kumar K.Va30d5422008-10-09 10:56:23 -0400421
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400422 if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
423 EXT4_FREEBLOCKS_WATERMARK) {
Andrew Morton02d21162008-12-09 13:14:14 -0800424 free_blocks = percpu_counter_sum_positive(fbc);
425 dirty_blocks = percpu_counter_sum_positive(dbc);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400426 }
427 /* Check whether we have space after
Eric Sandeena9960312008-10-28 00:08:17 -0400428 * accounting for current dirty blocks & root reserved blocks.
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400429 */
Eric Sandeena9960312008-10-28 00:08:17 -0400430 if (free_blocks >= ((root_blocks + nblocks) + dirty_blocks))
431 return 1;
Aneesh Kumar K.V5c791612008-10-08 23:12:24 -0400432
Eric Sandeena9960312008-10-28 00:08:17 -0400433 /* Hm, nope. Are (enough) root reserved blocks available? */
David Howells4c9c5442008-11-14 10:38:51 +1100434 if (sbi->s_resuid == current_fsuid() ||
Eric Sandeena9960312008-10-28 00:08:17 -0400435 ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
Allison Henderson55f020d2011-05-25 07:41:26 -0400436 capable(CAP_SYS_RESOURCE) ||
437 (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
438
Eric Sandeena9960312008-10-28 00:08:17 -0400439 if (free_blocks >= (nblocks + dirty_blocks))
440 return 1;
441 }
442
443 return 0;
Aneesh Kumar K.Va30d5422008-10-09 10:56:23 -0400444}
Mingming Cao07031432008-07-11 19:27:31 -0400445
Eric Sandeen8c3bf8a2008-10-28 00:08:12 -0400446int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
Allison Henderson55f020d2011-05-25 07:41:26 -0400447 s64 nblocks, unsigned int flags)
Eric Sandeen8c3bf8a2008-10-28 00:08:12 -0400448{
Allison Henderson55f020d2011-05-25 07:41:26 -0400449 if (ext4_has_free_blocks(sbi, nblocks, flags)) {
Eric Sandeen8c3bf8a2008-10-28 00:08:12 -0400450 percpu_counter_add(&sbi->s_dirtyblocks_counter, nblocks);
451 return 0;
452 } else
453 return -ENOSPC;
454}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455
456/**
Mingming Cao617ba132006-10-11 01:20:53 -0700457 * ext4_should_retry_alloc()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458 * @sb: super block
459 * @retries number of attemps has been made
460 *
Mingming Cao617ba132006-10-11 01:20:53 -0700461 * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700462 * it is profitable to retry the operation, this function will wait
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300463 * for the current or committing transaction to complete, and then
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700464 * return TRUE.
465 *
466 * if the total number of retries exceed three times, return FALSE.
467 */
Mingming Cao617ba132006-10-11 01:20:53 -0700468int ext4_should_retry_alloc(struct super_block *sb, int *retries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700469{
Allison Henderson55f020d2011-05-25 07:41:26 -0400470 if (!ext4_has_free_blocks(EXT4_SB(sb), 1, 0) ||
Eric Sandeen8f64b322009-02-26 00:57:35 -0500471 (*retries)++ > 3 ||
472 !EXT4_SB(sb)->s_journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473 return 0;
474
475 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
476
Mingming Caodab291a2006-10-11 01:21:01 -0700477 return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478}
479
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400480/*
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400481 * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
482 *
483 * @handle: handle to this transaction
484 * @inode: file inode
485 * @goal: given target block(filesystem wide)
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500486 * @count: pointer to total number of blocks needed
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400487 * @errp: error code
488 *
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500489 * Return 1st allocated block number on success, *count stores total account
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400490 * error stores in errp pointer
491 */
492ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400493 ext4_fsblk_t goal, unsigned int flags,
494 unsigned long *count, int *errp)
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400495{
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500496 struct ext4_allocation_request ar;
Mingming Caod2a17632008-07-14 17:52:37 -0400497 ext4_fsblk_t ret;
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500498
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;
Allison Henderson55f020d2011-05-25 07:41:26 -0400504 ar.flags = flags;
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500505
506 ret = ext4_mb_new_blocks(handle, &ar, errp);
507 if (count)
508 *count = ar.len;
Mingming Caod2a17632008-07-14 17:52:37 -0400509 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400510 * Account for the allocated meta blocks. We will never
511 * fail EDQUOT for metdata, but we do account for it.
Mingming Caod2a17632008-07-14 17:52:37 -0400512 */
Theodore Ts'of2321092011-01-10 12:12:36 -0500513 if (!(*errp) &&
514 ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) {
Mingming Caod2a17632008-07-14 17:52:37 -0400515 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500516 EXT4_I(inode)->i_allocated_meta_blocks += ar.len;
Mingming Caod2a17632008-07-14 17:52:37 -0400517 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400518 dquot_alloc_block_nofail(inode, ar.len);
Mingming Caod2a17632008-07-14 17:52:37 -0400519 }
520 return ret;
521}
522
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523/**
Mingming Cao617ba132006-10-11 01:20:53 -0700524 * ext4_count_free_blocks() -- count filesystem free blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 * @sb: superblock
526 *
527 * Adds up the number of free blocks from each block group.
528 */
Mingming Cao617ba132006-10-11 01:20:53 -0700529ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700530{
Mingming Cao617ba132006-10-11 01:20:53 -0700531 ext4_fsblk_t desc_count;
532 struct ext4_group_desc *gdp;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500533 ext4_group_t i;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400534 ext4_group_t ngroups = ext4_get_groups_count(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700535#ifdef EXT4FS_DEBUG
536 struct ext4_super_block *es;
537 ext4_fsblk_t bitmap_count;
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500538 unsigned int x;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539 struct buffer_head *bitmap_bh = NULL;
540
Mingming Cao617ba132006-10-11 01:20:53 -0700541 es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700542 desc_count = 0;
543 bitmap_count = 0;
544 gdp = NULL;
545
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700547 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548 if (!gdp)
549 continue;
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -0500550 desc_count += ext4_free_blks_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 brelse(bitmap_bh);
Theodore Ts'o574ca172008-07-11 19:27:31 -0400552 bitmap_bh = ext4_read_block_bitmap(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553 if (bitmap_bh == NULL)
554 continue;
555
Mingming Cao617ba132006-10-11 01:20:53 -0700556 x = ext4_count_free(bitmap_bh, sb->s_blocksize);
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -0500557 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
558 i, ext4_free_blks_count(sb, gdp), x);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559 bitmap_count += x;
560 }
561 brelse(bitmap_bh);
Theodore Ts'o47760042008-09-08 23:00:52 -0400562 printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu"
563 ", computed = %llu, %llu\n", ext4_free_blocks_count(es),
564 desc_count, bitmap_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 return bitmap_count;
566#else
567 desc_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700569 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570 if (!gdp)
571 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500572 desc_count += ext4_free_blks_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573 }
574
575 return desc_count;
576#endif
577}
578
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500579static inline int test_root(ext4_group_t a, int b)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580{
581 int num = b;
582
583 while (a > num)
584 num *= b;
585 return num == a;
586}
587
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500588static int ext4_group_sparse(ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589{
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/**
Mingming Cao617ba132006-10-11 01:20:53 -0700599 * ext4_bg_has_super - number of blocks used by the superblock in group
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600 * @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 */
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500606int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607{
Mingming Cao617ba132006-10-11 01:20:53 -0700608 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
609 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
610 !ext4_group_sparse(group))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611 return 0;
612 return 1;
613}
614
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500615static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
616 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617{
Mingming Cao617ba132006-10-11 01:20:53 -0700618 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500619 ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
620 ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621
622 if (group == first || group == first + 1 || group == last)
623 return 1;
624 return 0;
625}
626
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500627static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
628 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629{
Theodore Ts'o8dadb192009-11-23 07:24:38 -0500630 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;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700637}
638
639/**
Mingming Cao617ba132006-10-11 01:20:53 -0700640 * ext4_bg_num_gdb - number of blocks used by the group table in group
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700641 * @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 */
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500648unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649{
650 unsigned long first_meta_bg =
Mingming Cao617ba132006-10-11 01:20:53 -0700651 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
652 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653
Mingming Cao617ba132006-10-11 01:20:53 -0700654 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655 metagroup < first_meta_bg)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400656 return ext4_bg_num_gdb_nometa(sb, group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657
Mingming Cao617ba132006-10-11 01:20:53 -0700658 return ext4_bg_num_gdb_meta(sb,group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659
660}
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400661
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400662/*
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400663 * This function returns the number of file system metadata clusters at
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400664 * the beginning of a block group, including the reserved gdt blocks.
665 */
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400666unsigned ext4_num_base_meta_clusters(struct super_block *sb,
667 ext4_group_t block_group)
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400668{
669 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400670 unsigned num;
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400671
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 }
Theodore Ts'od5b8f312011-09-09 18:44:51 -0400685 return EXT4_NUM_B2C(sbi, num);
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400686}
Eric Sandeenf86186b2011-06-28 10:01:31 -0400687/**
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 */
694ext4_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