blob: 8573e2bfb78a400be5052549fcd0263a6e7dd3d5 [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
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -040026static unsigned int num_base_meta_blocks(struct super_block *sb,
27 ext4_group_t block_group);
28
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029/*
30 * balloc.c contains the blocks allocation and deallocation routines
31 */
32
33/*
Andrew Morton72b64b52006-10-11 01:21:18 -070034 * Calculate the block group number and offset, given a block number
35 */
36void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
Avantika Mathurfd2d4292008-01-28 23:58:27 -050037 ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
Andrew Morton72b64b52006-10-11 01:21:18 -070038{
Dave Kleikamp8c55e202007-05-24 13:04:54 -040039 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Andrew Morton72b64b52006-10-11 01:21:18 -070040 ext4_grpblk_t offset;
41
Dave Kleikamp8c55e202007-05-24 13:04:54 -040042 blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
Andrew Mortonf4e5bc22006-10-11 01:21:19 -070043 offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb));
Andrew Morton72b64b52006-10-11 01:21:18 -070044 if (offsetp)
45 *offsetp = offset;
46 if (blockgrpp)
Dave Kleikamp8c55e202007-05-24 13:04:54 -040047 *blockgrpp = blocknr;
Andrew Morton72b64b52006-10-11 01:21:18 -070048
49}
50
Jose R. Santos0bf7e832008-06-03 14:07:29 -040051static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block,
52 ext4_group_t block_group)
53{
54 ext4_group_t actual_group;
Aneesh Kumar K.V74778272008-07-11 19:27:31 -040055 ext4_get_group_no_and_offset(sb, block, &actual_group, NULL);
Jose R. Santos0bf7e832008-06-03 14:07:29 -040056 if (actual_group == block_group)
57 return 1;
58 return 0;
59}
60
61static int ext4_group_used_meta_blocks(struct super_block *sb,
Theodore Ts'oe187c652009-02-06 16:23:37 -050062 ext4_group_t block_group,
63 struct ext4_group_desc *gdp)
Jose R. Santos0bf7e832008-06-03 14:07:29 -040064{
65 ext4_fsblk_t tmp;
66 struct ext4_sb_info *sbi = EXT4_SB(sb);
67 /* block bitmap, inode bitmap, and inode table blocks */
68 int used_blocks = sbi->s_itb_per_group + 2;
69
70 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
Jose R. Santos0bf7e832008-06-03 14:07:29 -040071 if (!ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp),
72 block_group))
73 used_blocks--;
74
75 if (!ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp),
76 block_group))
77 used_blocks--;
78
79 tmp = ext4_inode_table(sb, gdp);
80 for (; tmp < ext4_inode_table(sb, gdp) +
81 sbi->s_itb_per_group; tmp++) {
82 if (!ext4_block_in_group(sb, tmp, block_group))
83 used_blocks -= 1;
84 }
85 }
86 return used_blocks;
87}
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -040088
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -040089static unsigned int num_blocks_in_group(struct super_block *sb,
90 ext4_group_t block_group)
91{
92 if (block_group == ext4_get_groups_count(sb) - 1) {
93 /*
94 * Even though mke2fs always initializes the first and
95 * last group, just in case some other tool was used,
96 * we need to make sure we calculate the right free
97 * blocks.
98 */
99 return ext4_blocks_count(EXT4_SB(sb)->s_es) -
100 ext4_group_first_block_no(sb, block_group);
101 } else
102 return EXT4_BLOCKS_PER_GROUP(sb);
103}
104
Andreas Dilger717d50e2007-10-16 18:38:25 -0400105/* Initializes an uninitialized block bitmap if given, and returns the
106 * number of blocks free in the group. */
107unsigned ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500108 ext4_group_t block_group, struct ext4_group_desc *gdp)
Andreas Dilger717d50e2007-10-16 18:38:25 -0400109{
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400110 unsigned int bit, bit_max = num_base_meta_blocks(sb, block_group);
Theodore Ts'o8df96752009-05-01 08:50:38 -0400111 ext4_group_t ngroups = ext4_get_groups_count(sb);
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400112 unsigned group_blocks = num_blocks_in_group(sb, block_group);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400113 struct ext4_sb_info *sbi = EXT4_SB(sb);
114
115 if (bh) {
116 J_ASSERT_BH(bh, buffer_locked(bh));
117
118 /* If checksum is bad mark all blocks used to prevent allocation
119 * essentially implementing a per-group read-only flag. */
120 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500121 ext4_error(sb, "Checksum bad for group %u",
122 block_group);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500123 ext4_free_blks_set(sb, gdp, 0);
124 ext4_free_inodes_set(sb, gdp, 0);
125 ext4_itable_unused_set(sb, gdp, 0);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400126 memset(bh->b_data, 0xff, sb->s_blocksize);
127 return 0;
128 }
129 memset(bh->b_data, 0, sb->s_blocksize);
130 }
131
Andreas Dilger717d50e2007-10-16 18:38:25 -0400132 if (bh) {
Jose R. Santos0bf7e832008-06-03 14:07:29 -0400133 ext4_fsblk_t start, tmp;
134 int flex_bg = 0;
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400135
Andreas Dilger717d50e2007-10-16 18:38:25 -0400136 for (bit = 0; bit < bit_max; bit++)
137 ext4_set_bit(bit, bh->b_data);
138
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400139 start = ext4_group_first_block_no(sb, block_group);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400140
Jose R. Santos0bf7e832008-06-03 14:07:29 -0400141 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
142 EXT4_FEATURE_INCOMPAT_FLEX_BG))
143 flex_bg = 1;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400144
Jose R. Santos0bf7e832008-06-03 14:07:29 -0400145 /* Set bits for block and inode bitmaps, and inode table */
146 tmp = ext4_block_bitmap(sb, gdp);
147 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
148 ext4_set_bit(tmp - start, bh->b_data);
149
150 tmp = ext4_inode_bitmap(sb, gdp);
151 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
152 ext4_set_bit(tmp - start, bh->b_data);
153
154 tmp = ext4_inode_table(sb, gdp);
155 for (; tmp < ext4_inode_table(sb, gdp) +
156 sbi->s_itb_per_group; tmp++) {
157 if (!flex_bg ||
158 ext4_block_in_group(sb, tmp, block_group))
159 ext4_set_bit(tmp - start, bh->b_data);
160 }
Andreas Dilger717d50e2007-10-16 18:38:25 -0400161 /*
162 * Also if the number of blocks within the group is
163 * less than the blocksize * 8 ( which is the size
164 * of bitmap ), set rest of the block bitmap to 1
165 */
Theodore Ts'o61d08672010-10-27 21:30:15 -0400166 ext4_mark_bitmap_end(group_blocks, sb->s_blocksize * 8,
167 bh->b_data);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400168 }
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400169 return group_blocks - bit_max -
170 ext4_group_used_meta_blocks(sb, block_group, gdp);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400171}
172
173
Andrew Morton72b64b52006-10-11 01:21:18 -0700174/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175 * The free blocks are managed by bitmaps. A file system contains several
176 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
177 * block for inodes, N blocks for the inode table and data blocks.
178 *
179 * The file system contains group descriptors which are located after the
180 * super block. Each descriptor contains the number of the bitmap block and
181 * the free blocks count in the block. The descriptors are loaded in memory
Aneesh Kumar K.Ve6274322007-02-20 13:57:58 -0800182 * when a file system is mounted (see ext4_fill_super).
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 */
184
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185/**
Mingming Cao617ba132006-10-11 01:20:53 -0700186 * ext4_get_group_desc() -- load group descriptor from disk
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187 * @sb: super block
188 * @block_group: given block group
189 * @bh: pointer to the buffer head to store the block
190 * group descriptor
191 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400192struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500193 ext4_group_t block_group,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400194 struct buffer_head **bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500196 unsigned int group_desc;
197 unsigned int offset;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400198 ext4_group_t ngroups = ext4_get_groups_count(sb);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400199 struct ext4_group_desc *desc;
Mingming Cao617ba132006-10-11 01:20:53 -0700200 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700201
Theodore Ts'o8df96752009-05-01 08:50:38 -0400202 if (block_group >= ngroups) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500203 ext4_error(sb, "block_group >= groups_count - block_group = %u,"
204 " groups_count = %u", block_group, ngroups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205
206 return NULL;
207 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208
Mingming Cao617ba132006-10-11 01:20:53 -0700209 group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
210 offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700211 if (!sbi->s_group_desc[group_desc]) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500212 ext4_error(sb, "Group descriptor not loaded - "
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500213 "block_group = %u, group_desc = %u, desc = %u",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400214 block_group, group_desc, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215 return NULL;
216 }
217
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -0700218 desc = (struct ext4_group_desc *)(
219 (__u8 *)sbi->s_group_desc[group_desc]->b_data +
220 offset * EXT4_DESC_SIZE(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700221 if (bh)
222 *bh = sbi->s_group_desc[group_desc];
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -0700223 return desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224}
225
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500226static int ext4_valid_block_bitmap(struct super_block *sb,
227 struct ext4_group_desc *desc,
228 unsigned int block_group,
229 struct buffer_head *bh)
230{
231 ext4_grpblk_t offset;
232 ext4_grpblk_t next_zero_bit;
233 ext4_fsblk_t bitmap_blk;
234 ext4_fsblk_t group_first_block;
235
236 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
237 /* with FLEX_BG, the inode/block bitmaps and itable
238 * blocks may not be in the group at all
239 * so the bitmap validation will be skipped for those groups
240 * or it has to also read the block group where the bitmaps
241 * are located to verify they are set.
242 */
243 return 1;
244 }
245 group_first_block = ext4_group_first_block_no(sb, block_group);
246
247 /* check whether block bitmap block number is set */
248 bitmap_blk = ext4_block_bitmap(sb, desc);
249 offset = bitmap_blk - group_first_block;
250 if (!ext4_test_bit(offset, bh->b_data))
251 /* bad block bitmap */
252 goto err_out;
253
254 /* check whether the inode bitmap block number is set */
255 bitmap_blk = ext4_inode_bitmap(sb, desc);
256 offset = bitmap_blk - group_first_block;
257 if (!ext4_test_bit(offset, bh->b_data))
258 /* bad block bitmap */
259 goto err_out;
260
261 /* check whether the inode table block number is set */
262 bitmap_blk = ext4_inode_table(sb, desc);
263 offset = bitmap_blk - group_first_block;
264 next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
265 offset + EXT4_SB(sb)->s_itb_per_group,
266 offset);
267 if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group)
268 /* good bitmap for inode tables */
269 return 1;
270
271err_out:
Eric Sandeen12062dd2010-02-15 14:19:27 -0500272 ext4_error(sb, "Invalid block bitmap - block_group = %d, block = %llu",
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500273 block_group, bitmap_blk);
274 return 0;
275}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276/**
Theodore Ts'o574ca172008-07-11 19:27:31 -0400277 * ext4_read_block_bitmap()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700278 * @sb: super block
279 * @block_group: given block group
280 *
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500281 * Read the bitmap for a given block_group,and validate the
282 * bits for block/inode/inode tables are set in the bitmaps
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283 *
284 * Return buffer_head on success or NULL in case of failure.
285 */
Andreas Dilger717d50e2007-10-16 18:38:25 -0400286struct buffer_head *
Theodore Ts'o574ca172008-07-11 19:27:31 -0400287ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400289 struct ext4_group_desc *desc;
290 struct buffer_head *bh = NULL;
Aneesh Kumar K.V7c9e69f2007-10-16 23:27:02 -0700291 ext4_fsblk_t bitmap_blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292
Andreas Dilger717d50e2007-10-16 18:38:25 -0400293 desc = ext4_get_group_desc(sb, block_group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294 if (!desc)
Aneesh Kumar K.V7c9e69f2007-10-16 23:27:02 -0700295 return NULL;
296 bitmap_blk = ext4_block_bitmap(sb, desc);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500297 bh = sb_getblk(sb, bitmap_blk);
298 if (unlikely(!bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500299 ext4_error(sb, "Cannot read block bitmap - "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500300 "block_group = %u, block_bitmap = %llu",
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400301 block_group, bitmap_blk);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500302 return NULL;
303 }
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500304
305 if (bitmap_uptodate(bh))
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500306 return bh;
307
Frederic Bohec806e682008-10-10 08:09:18 -0400308 lock_buffer(bh);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500309 if (bitmap_uptodate(bh)) {
310 unlock_buffer(bh);
311 return bh;
312 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400313 ext4_lock_group(sb, block_group);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500314 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
315 ext4_init_block_bitmap(sb, bh, block_group, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500316 set_bitmap_uptodate(bh);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500317 set_buffer_uptodate(bh);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400318 ext4_unlock_group(sb, block_group);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500319 unlock_buffer(bh);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500320 return bh;
321 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400322 ext4_unlock_group(sb, block_group);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500323 if (buffer_uptodate(bh)) {
324 /*
325 * if not uninit if bh is uptodate,
326 * bitmap is also uptodate
327 */
328 set_bitmap_uptodate(bh);
329 unlock_buffer(bh);
330 return bh;
331 }
332 /*
333 * submit the buffer_head for read. We can
334 * safely mark the bitmap as uptodate now.
335 * We do it here so the bitmap uptodate bit
336 * get set with buffer lock held.
337 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400338 trace_ext4_read_block_bitmap_load(sb, block_group);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500339 set_bitmap_uptodate(bh);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500340 if (bh_submit_read(bh) < 0) {
341 put_bh(bh);
Eric Sandeen12062dd2010-02-15 14:19:27 -0500342 ext4_error(sb, "Cannot read block bitmap - "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500343 "block_group = %u, block_bitmap = %llu",
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400344 block_group, bitmap_blk);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500345 return NULL;
346 }
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -0400347 ext4_valid_block_bitmap(sb, desc, block_group, bh);
348 /*
349 * file system mounted not to panic on error,
350 * continue with corrupt bitmap
351 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700352 return bh;
353}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700354
355/**
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400356 * ext4_has_free_blocks()
357 * @sbi: in-core super block structure.
358 * @nblocks: number of needed blocks
359 *
360 * Check if filesystem has nblocks free & available for allocation.
361 * On success return 1, return 0 on failure.
362 */
Allison Henderson55f020d2011-05-25 07:41:26 -0400363static int ext4_has_free_blocks(struct ext4_sb_info *sbi,
364 s64 nblocks, unsigned int flags)
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400365{
Eric Sandeena9960312008-10-28 00:08:17 -0400366 s64 free_blocks, dirty_blocks, root_blocks;
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400367 struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400368 struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400369
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400370 free_blocks = percpu_counter_read_positive(fbc);
371 dirty_blocks = percpu_counter_read_positive(dbc);
Eric Sandeena9960312008-10-28 00:08:17 -0400372 root_blocks = ext4_r_blocks_count(sbi->s_es);
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400373
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400374 if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
375 EXT4_FREEBLOCKS_WATERMARK) {
Andrew Morton02d21162008-12-09 13:14:14 -0800376 free_blocks = percpu_counter_sum_positive(fbc);
377 dirty_blocks = percpu_counter_sum_positive(dbc);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400378 }
379 /* Check whether we have space after
Eric Sandeena9960312008-10-28 00:08:17 -0400380 * accounting for current dirty blocks & root reserved blocks.
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400381 */
Eric Sandeena9960312008-10-28 00:08:17 -0400382 if (free_blocks >= ((root_blocks + nblocks) + dirty_blocks))
383 return 1;
Aneesh Kumar K.V5c791612008-10-08 23:12:24 -0400384
Eric Sandeena9960312008-10-28 00:08:17 -0400385 /* Hm, nope. Are (enough) root reserved blocks available? */
David Howells4c9c5442008-11-14 10:38:51 +1100386 if (sbi->s_resuid == current_fsuid() ||
Eric Sandeena9960312008-10-28 00:08:17 -0400387 ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
Allison Henderson55f020d2011-05-25 07:41:26 -0400388 capable(CAP_SYS_RESOURCE) ||
389 (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
390
Eric Sandeena9960312008-10-28 00:08:17 -0400391 if (free_blocks >= (nblocks + dirty_blocks))
392 return 1;
393 }
394
395 return 0;
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400396}
Mingming Cao07031432008-07-11 19:27:31 -0400397
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400398int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
Allison Henderson55f020d2011-05-25 07:41:26 -0400399 s64 nblocks, unsigned int flags)
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400400{
Allison Henderson55f020d2011-05-25 07:41:26 -0400401 if (ext4_has_free_blocks(sbi, nblocks, flags)) {
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400402 percpu_counter_add(&sbi->s_dirtyblocks_counter, nblocks);
403 return 0;
404 } else
405 return -ENOSPC;
406}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407
408/**
Mingming Cao617ba132006-10-11 01:20:53 -0700409 * ext4_should_retry_alloc()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410 * @sb: super block
411 * @retries number of attemps has been made
412 *
Mingming Cao617ba132006-10-11 01:20:53 -0700413 * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700414 * it is profitable to retry the operation, this function will wait
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300415 * for the current or committing transaction to complete, and then
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700416 * return TRUE.
417 *
418 * if the total number of retries exceed three times, return FALSE.
419 */
Mingming Cao617ba132006-10-11 01:20:53 -0700420int ext4_should_retry_alloc(struct super_block *sb, int *retries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700421{
Allison Henderson55f020d2011-05-25 07:41:26 -0400422 if (!ext4_has_free_blocks(EXT4_SB(sb), 1, 0) ||
Eric Sandeen8f64b322009-02-26 00:57:35 -0500423 (*retries)++ > 3 ||
424 !EXT4_SB(sb)->s_journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425 return 0;
426
427 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
428
Mingming Caodab291a2006-10-11 01:21:01 -0700429 return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700430}
431
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400432/*
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400433 * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
434 *
435 * @handle: handle to this transaction
436 * @inode: file inode
437 * @goal: given target block(filesystem wide)
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500438 * @count: pointer to total number of blocks needed
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400439 * @errp: error code
440 *
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500441 * Return 1st allocated block number on success, *count stores total account
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400442 * error stores in errp pointer
443 */
444ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400445 ext4_fsblk_t goal, unsigned int flags,
446 unsigned long *count, int *errp)
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400447{
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500448 struct ext4_allocation_request ar;
Mingming Caod2a17632008-07-14 17:52:37 -0400449 ext4_fsblk_t ret;
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500450
451 memset(&ar, 0, sizeof(ar));
452 /* Fill with neighbour allocated blocks */
453 ar.inode = inode;
454 ar.goal = goal;
455 ar.len = count ? *count : 1;
Allison Henderson55f020d2011-05-25 07:41:26 -0400456 ar.flags = flags;
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500457
458 ret = ext4_mb_new_blocks(handle, &ar, errp);
459 if (count)
460 *count = ar.len;
Mingming Caod2a17632008-07-14 17:52:37 -0400461 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400462 * Account for the allocated meta blocks. We will never
463 * fail EDQUOT for metdata, but we do account for it.
Mingming Caod2a17632008-07-14 17:52:37 -0400464 */
Theodore Ts'of2321092011-01-10 12:12:36 -0500465 if (!(*errp) &&
466 ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) {
Mingming Caod2a17632008-07-14 17:52:37 -0400467 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500468 EXT4_I(inode)->i_allocated_meta_blocks += ar.len;
Mingming Caod2a17632008-07-14 17:52:37 -0400469 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400470 dquot_alloc_block_nofail(inode, ar.len);
Mingming Caod2a17632008-07-14 17:52:37 -0400471 }
472 return ret;
473}
474
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475/**
Mingming Cao617ba132006-10-11 01:20:53 -0700476 * ext4_count_free_blocks() -- count filesystem free blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700477 * @sb: superblock
478 *
479 * Adds up the number of free blocks from each block group.
480 */
Mingming Cao617ba132006-10-11 01:20:53 -0700481ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482{
Mingming Cao617ba132006-10-11 01:20:53 -0700483 ext4_fsblk_t desc_count;
484 struct ext4_group_desc *gdp;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500485 ext4_group_t i;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400486 ext4_group_t ngroups = ext4_get_groups_count(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700487#ifdef EXT4FS_DEBUG
488 struct ext4_super_block *es;
489 ext4_fsblk_t bitmap_count;
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500490 unsigned int x;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491 struct buffer_head *bitmap_bh = NULL;
492
Mingming Cao617ba132006-10-11 01:20:53 -0700493 es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700494 desc_count = 0;
495 bitmap_count = 0;
496 gdp = NULL;
497
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700499 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 if (!gdp)
501 continue;
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -0500502 desc_count += ext4_free_blks_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503 brelse(bitmap_bh);
Theodore Ts'o574ca172008-07-11 19:27:31 -0400504 bitmap_bh = ext4_read_block_bitmap(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 if (bitmap_bh == NULL)
506 continue;
507
Mingming Cao617ba132006-10-11 01:20:53 -0700508 x = ext4_count_free(bitmap_bh, sb->s_blocksize);
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -0500509 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
510 i, ext4_free_blks_count(sb, gdp), x);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 bitmap_count += x;
512 }
513 brelse(bitmap_bh);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400514 printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu"
515 ", computed = %llu, %llu\n", ext4_free_blocks_count(es),
516 desc_count, bitmap_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517 return bitmap_count;
518#else
519 desc_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700521 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522 if (!gdp)
523 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500524 desc_count += ext4_free_blks_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 }
526
527 return desc_count;
528#endif
529}
530
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500531static inline int test_root(ext4_group_t a, int b)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532{
533 int num = b;
534
535 while (a > num)
536 num *= b;
537 return num == a;
538}
539
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500540static int ext4_group_sparse(ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541{
542 if (group <= 1)
543 return 1;
544 if (!(group & 1))
545 return 0;
546 return (test_root(group, 7) || test_root(group, 5) ||
547 test_root(group, 3));
548}
549
550/**
Mingming Cao617ba132006-10-11 01:20:53 -0700551 * ext4_bg_has_super - number of blocks used by the superblock in group
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552 * @sb: superblock for filesystem
553 * @group: group number to check
554 *
555 * Return the number of blocks used by the superblock (primary or backup)
556 * in this group. Currently this will be only 0 or 1.
557 */
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500558int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559{
Mingming Cao617ba132006-10-11 01:20:53 -0700560 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
561 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
562 !ext4_group_sparse(group))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 return 0;
564 return 1;
565}
566
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500567static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
568 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700569{
Mingming Cao617ba132006-10-11 01:20:53 -0700570 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500571 ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
572 ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573
574 if (group == first || group == first + 1 || group == last)
575 return 1;
576 return 0;
577}
578
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500579static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
580 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581{
Theodore Ts'o8dadb192009-11-23 07:24:38 -0500582 if (!ext4_bg_has_super(sb, group))
583 return 0;
584
585 if (EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG))
586 return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
587 else
588 return EXT4_SB(sb)->s_gdb_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589}
590
591/**
Mingming Cao617ba132006-10-11 01:20:53 -0700592 * ext4_bg_num_gdb - number of blocks used by the group table in group
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593 * @sb: superblock for filesystem
594 * @group: group number to check
595 *
596 * Return the number of blocks used by the group descriptor table
597 * (primary or backup) in this group. In the future there may be a
598 * different number of descriptor blocks in each group.
599 */
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500600unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700601{
602 unsigned long first_meta_bg =
Mingming Cao617ba132006-10-11 01:20:53 -0700603 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
604 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700605
Mingming Cao617ba132006-10-11 01:20:53 -0700606 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607 metagroup < first_meta_bg)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400608 return ext4_bg_num_gdb_nometa(sb, group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700609
Mingming Cao617ba132006-10-11 01:20:53 -0700610 return ext4_bg_num_gdb_meta(sb,group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611
612}
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400613
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400614/*
615 * This function returns the number of file system metadata blocks at
616 * the beginning of a block group, including the reserved gdt blocks.
617 */
618static unsigned int num_base_meta_blocks(struct super_block *sb,
619 ext4_group_t block_group)
620{
621 struct ext4_sb_info *sbi = EXT4_SB(sb);
622 int num;
623
624 /* Check for superblock and gdt backups in this group */
625 num = ext4_bg_has_super(sb, block_group);
626
627 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
628 block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
629 sbi->s_desc_per_block) {
630 if (num) {
631 num += ext4_bg_num_gdb(sb, block_group);
632 num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
633 }
634 } else { /* For META_BG_BLOCK_GROUPS */
635 num += ext4_bg_num_gdb(sb, block_group);
636 }
637 return num;
638}
Eric Sandeenf86186b2011-06-28 10:01:31 -0400639/**
640 * ext4_inode_to_goal_block - return a hint for block allocation
641 * @inode: inode for block allocation
642 *
643 * Return the ideal location to start allocating blocks for a
644 * newly created inode.
645 */
646ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
647{
648 struct ext4_inode_info *ei = EXT4_I(inode);
649 ext4_group_t block_group;
650 ext4_grpblk_t colour;
651 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
652 ext4_fsblk_t bg_start;
653 ext4_fsblk_t last_block;
654
655 block_group = ei->i_block_group;
656 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
657 /*
658 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
659 * block groups per flexgroup, reserve the first block
660 * group for directories and special files. Regular
661 * files will start at the second block group. This
662 * tends to speed up directory access and improves
663 * fsck times.
664 */
665 block_group &= ~(flex_size-1);
666 if (S_ISREG(inode->i_mode))
667 block_group++;
668 }
669 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
670 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
671
672 /*
673 * If we are doing delayed allocation, we don't need take
674 * colour into account.
675 */
676 if (test_opt(inode->i_sb, DELALLOC))
677 return bg_start;
678
679 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
680 colour = (current->pid % 16) *
681 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
682 else
683 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
684 return bg_start + colour;
685}
686