blob: 735d9fcc72e69efa8565d45c5ab49d1bd0422a97 [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
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400105/* Initializes an uninitialized block bitmap */
106void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
107 ext4_group_t block_group,
108 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);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400111 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400112 ext4_fsblk_t start, tmp;
113 int flex_bg = 0;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400114
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400115 J_ASSERT_BH(bh, buffer_locked(bh));
Andreas Dilger717d50e2007-10-16 18:38:25 -0400116
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400117 /* If checksum is bad mark all blocks used to prevent allocation
118 * essentially implementing a per-group read-only flag. */
119 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
120 ext4_error(sb, "Checksum bad for group %u", block_group);
121 ext4_free_blks_set(sb, gdp, 0);
122 ext4_free_inodes_set(sb, gdp, 0);
123 ext4_itable_unused_set(sb, gdp, 0);
124 memset(bh->b_data, 0xff, sb->s_blocksize);
125 return;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400126 }
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400127 memset(bh->b_data, 0, sb->s_blocksize);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400128
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400129 for (bit = 0; bit < bit_max; bit++)
130 ext4_set_bit(bit, bh->b_data);
Akinobu Mitad00a6d72008-04-17 10:38:59 -0400131
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400132 start = ext4_group_first_block_no(sb, block_group);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400133
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400134 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
135 flex_bg = 1;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400136
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400137 /* Set bits for block and inode bitmaps, and inode table */
138 tmp = ext4_block_bitmap(sb, gdp);
139 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
140 ext4_set_bit(tmp - start, bh->b_data);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400141
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400142 tmp = ext4_inode_bitmap(sb, gdp);
143 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
144 ext4_set_bit(tmp - start, bh->b_data);
145
146 tmp = ext4_inode_table(sb, gdp);
147 for (; tmp < ext4_inode_table(sb, gdp) +
148 sbi->s_itb_per_group; tmp++) {
Jose R. Santos0bf7e832008-06-03 14:07:29 -0400149 if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
150 ext4_set_bit(tmp - start, bh->b_data);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400151 }
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400152 /*
153 * Also if the number of blocks within the group is less than
154 * the blocksize * 8 ( which is the size of bitmap ), set rest
155 * of the block bitmap to 1
156 */
157 ext4_mark_bitmap_end(num_blocks_in_group(sb, block_group),
158 sb->s_blocksize * 8, bh->b_data);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400159}
160
Theodore Ts'ofd034a82011-09-09 18:42:51 -0400161/* Return the number of free blocks in a block group. It is used when
162 * the block bitmap is uninitialized, so we can't just count the bits
163 * in the bitmap. */
164unsigned ext4_free_blocks_after_init(struct super_block *sb,
165 ext4_group_t block_group,
166 struct ext4_group_desc *gdp)
167{
168 return num_blocks_in_group(sb, block_group) -
169 num_base_meta_blocks(sb, block_group) -
170 ext4_group_used_meta_blocks(sb, block_group, gdp);
171}
Andreas Dilger717d50e2007-10-16 18:38:25 -0400172
Andrew Morton72b64b52006-10-11 01:21:18 -0700173/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 * The free blocks are managed by bitmaps. A file system contains several
175 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
176 * block for inodes, N blocks for the inode table and data blocks.
177 *
178 * The file system contains group descriptors which are located after the
179 * super block. Each descriptor contains the number of the bitmap block and
180 * the free blocks count in the block. The descriptors are loaded in memory
Aneesh Kumar K.Ve6274322007-02-20 13:57:58 -0800181 * when a file system is mounted (see ext4_fill_super).
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182 */
183
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184/**
Mingming Cao617ba132006-10-11 01:20:53 -0700185 * ext4_get_group_desc() -- load group descriptor from disk
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 * @sb: super block
187 * @block_group: given block group
188 * @bh: pointer to the buffer head to store the block
189 * group descriptor
190 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400191struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500192 ext4_group_t block_group,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400193 struct buffer_head **bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700194{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500195 unsigned int group_desc;
196 unsigned int offset;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400197 ext4_group_t ngroups = ext4_get_groups_count(sb);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400198 struct ext4_group_desc *desc;
Mingming Cao617ba132006-10-11 01:20:53 -0700199 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700200
Theodore Ts'o8df96752009-05-01 08:50:38 -0400201 if (block_group >= ngroups) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500202 ext4_error(sb, "block_group >= groups_count - block_group = %u,"
203 " groups_count = %u", block_group, ngroups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204
205 return NULL;
206 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207
Mingming Cao617ba132006-10-11 01:20:53 -0700208 group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
209 offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 if (!sbi->s_group_desc[group_desc]) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500211 ext4_error(sb, "Group descriptor not loaded - "
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500212 "block_group = %u, group_desc = %u, desc = %u",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400213 block_group, group_desc, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700214 return NULL;
215 }
216
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -0700217 desc = (struct ext4_group_desc *)(
218 (__u8 *)sbi->s_group_desc[group_desc]->b_data +
219 offset * EXT4_DESC_SIZE(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220 if (bh)
221 *bh = sbi->s_group_desc[group_desc];
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -0700222 return desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223}
224
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500225static int ext4_valid_block_bitmap(struct super_block *sb,
226 struct ext4_group_desc *desc,
227 unsigned int block_group,
228 struct buffer_head *bh)
229{
230 ext4_grpblk_t offset;
231 ext4_grpblk_t next_zero_bit;
232 ext4_fsblk_t bitmap_blk;
233 ext4_fsblk_t group_first_block;
234
235 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
236 /* with FLEX_BG, the inode/block bitmaps and itable
237 * blocks may not be in the group at all
238 * so the bitmap validation will be skipped for those groups
239 * or it has to also read the block group where the bitmaps
240 * are located to verify they are set.
241 */
242 return 1;
243 }
244 group_first_block = ext4_group_first_block_no(sb, block_group);
245
246 /* check whether block bitmap block number is set */
247 bitmap_blk = ext4_block_bitmap(sb, desc);
248 offset = bitmap_blk - group_first_block;
249 if (!ext4_test_bit(offset, bh->b_data))
250 /* bad block bitmap */
251 goto err_out;
252
253 /* check whether the inode bitmap block number is set */
254 bitmap_blk = ext4_inode_bitmap(sb, desc);
255 offset = bitmap_blk - group_first_block;
256 if (!ext4_test_bit(offset, bh->b_data))
257 /* bad block bitmap */
258 goto err_out;
259
260 /* check whether the inode table block number is set */
261 bitmap_blk = ext4_inode_table(sb, desc);
262 offset = bitmap_blk - group_first_block;
263 next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
264 offset + EXT4_SB(sb)->s_itb_per_group,
265 offset);
266 if (next_zero_bit >= offset + EXT4_SB(sb)->s_itb_per_group)
267 /* good bitmap for inode tables */
268 return 1;
269
270err_out:
Eric Sandeen12062dd2010-02-15 14:19:27 -0500271 ext4_error(sb, "Invalid block bitmap - block_group = %d, block = %llu",
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500272 block_group, bitmap_blk);
273 return 0;
274}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275/**
Theodore Ts'o574ca172008-07-11 19:27:31 -0400276 * ext4_read_block_bitmap()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277 * @sb: super block
278 * @block_group: given block group
279 *
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500280 * Read the bitmap for a given block_group,and validate the
281 * bits for block/inode/inode tables are set in the bitmaps
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 *
283 * Return buffer_head on success or NULL in case of failure.
284 */
Andreas Dilger717d50e2007-10-16 18:38:25 -0400285struct buffer_head *
Theodore Ts'o574ca172008-07-11 19:27:31 -0400286ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400288 struct ext4_group_desc *desc;
289 struct buffer_head *bh = NULL;
Aneesh Kumar K.V7c9e69f2007-10-16 23:27:02 -0700290 ext4_fsblk_t bitmap_blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291
Andreas Dilger717d50e2007-10-16 18:38:25 -0400292 desc = ext4_get_group_desc(sb, block_group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293 if (!desc)
Aneesh Kumar K.V7c9e69f2007-10-16 23:27:02 -0700294 return NULL;
295 bitmap_blk = ext4_block_bitmap(sb, desc);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500296 bh = sb_getblk(sb, bitmap_blk);
297 if (unlikely(!bh)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500298 ext4_error(sb, "Cannot read block bitmap - "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500299 "block_group = %u, block_bitmap = %llu",
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400300 block_group, bitmap_blk);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500301 return NULL;
302 }
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500303
304 if (bitmap_uptodate(bh))
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500305 return bh;
306
Frederic Bohec806e682008-10-10 08:09:18 -0400307 lock_buffer(bh);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500308 if (bitmap_uptodate(bh)) {
309 unlock_buffer(bh);
310 return bh;
311 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400312 ext4_lock_group(sb, block_group);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500313 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
314 ext4_init_block_bitmap(sb, bh, block_group, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500315 set_bitmap_uptodate(bh);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500316 set_buffer_uptodate(bh);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400317 ext4_unlock_group(sb, block_group);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500318 unlock_buffer(bh);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500319 return bh;
320 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400321 ext4_unlock_group(sb, block_group);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500322 if (buffer_uptodate(bh)) {
323 /*
324 * if not uninit if bh is uptodate,
325 * bitmap is also uptodate
326 */
327 set_bitmap_uptodate(bh);
328 unlock_buffer(bh);
329 return bh;
330 }
331 /*
332 * submit the buffer_head for read. We can
333 * safely mark the bitmap as uptodate now.
334 * We do it here so the bitmap uptodate bit
335 * get set with buffer lock held.
336 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400337 trace_ext4_read_block_bitmap_load(sb, block_group);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500338 set_bitmap_uptodate(bh);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500339 if (bh_submit_read(bh) < 0) {
340 put_bh(bh);
Eric Sandeen12062dd2010-02-15 14:19:27 -0500341 ext4_error(sb, "Cannot read block bitmap - "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500342 "block_group = %u, block_bitmap = %llu",
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400343 block_group, bitmap_blk);
Aneesh Kumar K.Vabcb2942008-01-28 23:58:27 -0500344 return NULL;
345 }
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -0400346 ext4_valid_block_bitmap(sb, desc, block_group, bh);
347 /*
348 * file system mounted not to panic on error,
349 * continue with corrupt bitmap
350 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351 return bh;
352}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700353
354/**
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400355 * ext4_has_free_blocks()
356 * @sbi: in-core super block structure.
357 * @nblocks: number of needed blocks
358 *
359 * Check if filesystem has nblocks free & available for allocation.
360 * On success return 1, return 0 on failure.
361 */
Allison Henderson55f020d2011-05-25 07:41:26 -0400362static int ext4_has_free_blocks(struct ext4_sb_info *sbi,
363 s64 nblocks, unsigned int flags)
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400364{
Eric Sandeena9960312008-10-28 00:08:17 -0400365 s64 free_blocks, dirty_blocks, root_blocks;
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400366 struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400367 struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400368
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400369 free_blocks = percpu_counter_read_positive(fbc);
370 dirty_blocks = percpu_counter_read_positive(dbc);
Eric Sandeena9960312008-10-28 00:08:17 -0400371 root_blocks = ext4_r_blocks_count(sbi->s_es);
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400372
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400373 if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
374 EXT4_FREEBLOCKS_WATERMARK) {
Andrew Morton02d21162008-12-09 13:14:14 -0800375 free_blocks = percpu_counter_sum_positive(fbc);
376 dirty_blocks = percpu_counter_sum_positive(dbc);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400377 }
378 /* Check whether we have space after
Eric Sandeena9960312008-10-28 00:08:17 -0400379 * accounting for current dirty blocks & root reserved blocks.
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400380 */
Eric Sandeena9960312008-10-28 00:08:17 -0400381 if (free_blocks >= ((root_blocks + nblocks) + dirty_blocks))
382 return 1;
Aneesh Kumar K.V5c791612008-10-08 23:12:24 -0400383
Eric Sandeena9960312008-10-28 00:08:17 -0400384 /* Hm, nope. Are (enough) root reserved blocks available? */
David Howells4c9c5442008-11-14 10:38:51 +1100385 if (sbi->s_resuid == current_fsuid() ||
Eric Sandeena9960312008-10-28 00:08:17 -0400386 ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
Allison Henderson55f020d2011-05-25 07:41:26 -0400387 capable(CAP_SYS_RESOURCE) ||
388 (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
389
Eric Sandeena9960312008-10-28 00:08:17 -0400390 if (free_blocks >= (nblocks + dirty_blocks))
391 return 1;
392 }
393
394 return 0;
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -0400395}
Mingming Cao07031432008-07-11 19:27:31 -0400396
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400397int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
Allison Henderson55f020d2011-05-25 07:41:26 -0400398 s64 nblocks, unsigned int flags)
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400399{
Allison Henderson55f020d2011-05-25 07:41:26 -0400400 if (ext4_has_free_blocks(sbi, nblocks, flags)) {
Eric Sandeen8c3bf8a02008-10-28 00:08:12 -0400401 percpu_counter_add(&sbi->s_dirtyblocks_counter, nblocks);
402 return 0;
403 } else
404 return -ENOSPC;
405}
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700406
407/**
Mingming Cao617ba132006-10-11 01:20:53 -0700408 * ext4_should_retry_alloc()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700409 * @sb: super block
410 * @retries number of attemps has been made
411 *
Mingming Cao617ba132006-10-11 01:20:53 -0700412 * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700413 * it is profitable to retry the operation, this function will wait
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300414 * for the current or committing transaction to complete, and then
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700415 * return TRUE.
416 *
417 * if the total number of retries exceed three times, return FALSE.
418 */
Mingming Cao617ba132006-10-11 01:20:53 -0700419int ext4_should_retry_alloc(struct super_block *sb, int *retries)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700420{
Allison Henderson55f020d2011-05-25 07:41:26 -0400421 if (!ext4_has_free_blocks(EXT4_SB(sb), 1, 0) ||
Eric Sandeen8f64b322009-02-26 00:57:35 -0500422 (*retries)++ > 3 ||
423 !EXT4_SB(sb)->s_journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700424 return 0;
425
426 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
427
Mingming Caodab291a2006-10-11 01:21:01 -0700428 return jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700429}
430
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400431/*
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400432 * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
433 *
434 * @handle: handle to this transaction
435 * @inode: file inode
436 * @goal: given target block(filesystem wide)
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500437 * @count: pointer to total number of blocks needed
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400438 * @errp: error code
439 *
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500440 * Return 1st allocated block number on success, *count stores total account
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400441 * error stores in errp pointer
442 */
443ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400444 ext4_fsblk_t goal, unsigned int flags,
445 unsigned long *count, int *errp)
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400446{
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500447 struct ext4_allocation_request ar;
Mingming Caod2a17632008-07-14 17:52:37 -0400448 ext4_fsblk_t ret;
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500449
450 memset(&ar, 0, sizeof(ar));
451 /* Fill with neighbour allocated blocks */
452 ar.inode = inode;
453 ar.goal = goal;
454 ar.len = count ? *count : 1;
Allison Henderson55f020d2011-05-25 07:41:26 -0400455 ar.flags = flags;
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500456
457 ret = ext4_mb_new_blocks(handle, &ar, errp);
458 if (count)
459 *count = ar.len;
Mingming Caod2a17632008-07-14 17:52:37 -0400460 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400461 * Account for the allocated meta blocks. We will never
462 * fail EDQUOT for metdata, but we do account for it.
Mingming Caod2a17632008-07-14 17:52:37 -0400463 */
Theodore Ts'of2321092011-01-10 12:12:36 -0500464 if (!(*errp) &&
465 ext4_test_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED)) {
Mingming Caod2a17632008-07-14 17:52:37 -0400466 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500467 EXT4_I(inode)->i_allocated_meta_blocks += ar.len;
Mingming Caod2a17632008-07-14 17:52:37 -0400468 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400469 dquot_alloc_block_nofail(inode, ar.len);
Mingming Caod2a17632008-07-14 17:52:37 -0400470 }
471 return ret;
472}
473
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474/**
Mingming Cao617ba132006-10-11 01:20:53 -0700475 * ext4_count_free_blocks() -- count filesystem free blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476 * @sb: superblock
477 *
478 * Adds up the number of free blocks from each block group.
479 */
Mingming Cao617ba132006-10-11 01:20:53 -0700480ext4_fsblk_t ext4_count_free_blocks(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481{
Mingming Cao617ba132006-10-11 01:20:53 -0700482 ext4_fsblk_t desc_count;
483 struct ext4_group_desc *gdp;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500484 ext4_group_t i;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400485 ext4_group_t ngroups = ext4_get_groups_count(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700486#ifdef EXT4FS_DEBUG
487 struct ext4_super_block *es;
488 ext4_fsblk_t bitmap_count;
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500489 unsigned int x;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490 struct buffer_head *bitmap_bh = NULL;
491
Mingming Cao617ba132006-10-11 01:20:53 -0700492 es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493 desc_count = 0;
494 bitmap_count = 0;
495 gdp = NULL;
496
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700498 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 if (!gdp)
500 continue;
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -0500501 desc_count += ext4_free_blks_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502 brelse(bitmap_bh);
Theodore Ts'o574ca172008-07-11 19:27:31 -0400503 bitmap_bh = ext4_read_block_bitmap(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504 if (bitmap_bh == NULL)
505 continue;
506
Mingming Cao617ba132006-10-11 01:20:53 -0700507 x = ext4_count_free(bitmap_bh, sb->s_blocksize);
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -0500508 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
509 i, ext4_free_blks_count(sb, gdp), x);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 bitmap_count += x;
511 }
512 brelse(bitmap_bh);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400513 printk(KERN_DEBUG "ext4_count_free_blocks: stored = %llu"
514 ", computed = %llu, %llu\n", ext4_free_blocks_count(es),
515 desc_count, bitmap_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700516 return bitmap_count;
517#else
518 desc_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -0700520 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521 if (!gdp)
522 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500523 desc_count += ext4_free_blks_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524 }
525
526 return desc_count;
527#endif
528}
529
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500530static inline int test_root(ext4_group_t a, int b)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531{
532 int num = b;
533
534 while (a > num)
535 num *= b;
536 return num == a;
537}
538
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500539static int ext4_group_sparse(ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540{
541 if (group <= 1)
542 return 1;
543 if (!(group & 1))
544 return 0;
545 return (test_root(group, 7) || test_root(group, 5) ||
546 test_root(group, 3));
547}
548
549/**
Mingming Cao617ba132006-10-11 01:20:53 -0700550 * ext4_bg_has_super - number of blocks used by the superblock in group
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 * @sb: superblock for filesystem
552 * @group: group number to check
553 *
554 * Return the number of blocks used by the superblock (primary or backup)
555 * in this group. Currently this will be only 0 or 1.
556 */
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500557int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558{
Mingming Cao617ba132006-10-11 01:20:53 -0700559 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
560 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
561 !ext4_group_sparse(group))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562 return 0;
563 return 1;
564}
565
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500566static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
567 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568{
Mingming Cao617ba132006-10-11 01:20:53 -0700569 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500570 ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
571 ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572
573 if (group == first || group == first + 1 || group == last)
574 return 1;
575 return 0;
576}
577
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500578static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
579 ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580{
Theodore Ts'o8dadb192009-11-23 07:24:38 -0500581 if (!ext4_bg_has_super(sb, group))
582 return 0;
583
584 if (EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG))
585 return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
586 else
587 return EXT4_SB(sb)->s_gdb_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588}
589
590/**
Mingming Cao617ba132006-10-11 01:20:53 -0700591 * ext4_bg_num_gdb - number of blocks used by the group table in group
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700592 * @sb: superblock for filesystem
593 * @group: group number to check
594 *
595 * Return the number of blocks used by the group descriptor table
596 * (primary or backup) in this group. In the future there may be a
597 * different number of descriptor blocks in each group.
598 */
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500599unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600{
601 unsigned long first_meta_bg =
Mingming Cao617ba132006-10-11 01:20:53 -0700602 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
603 unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700604
Mingming Cao617ba132006-10-11 01:20:53 -0700605 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 metagroup < first_meta_bg)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400607 return ext4_bg_num_gdb_nometa(sb, group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608
Mingming Cao617ba132006-10-11 01:20:53 -0700609 return ext4_bg_num_gdb_meta(sb,group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610
611}
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400612
Theodore Ts'o49f7f9a2011-09-09 18:40:51 -0400613/*
614 * This function returns the number of file system metadata blocks at
615 * the beginning of a block group, including the reserved gdt blocks.
616 */
617static unsigned int num_base_meta_blocks(struct super_block *sb,
618 ext4_group_t block_group)
619{
620 struct ext4_sb_info *sbi = EXT4_SB(sb);
621 int num;
622
623 /* Check for superblock and gdt backups in this group */
624 num = ext4_bg_has_super(sb, block_group);
625
626 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
627 block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
628 sbi->s_desc_per_block) {
629 if (num) {
630 num += ext4_bg_num_gdb(sb, block_group);
631 num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
632 }
633 } else { /* For META_BG_BLOCK_GROUPS */
634 num += ext4_bg_num_gdb(sb, block_group);
635 }
636 return num;
637}
Eric Sandeenf86186b2011-06-28 10:01:31 -0400638/**
639 * ext4_inode_to_goal_block - return a hint for block allocation
640 * @inode: inode for block allocation
641 *
642 * Return the ideal location to start allocating blocks for a
643 * newly created inode.
644 */
645ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
646{
647 struct ext4_inode_info *ei = EXT4_I(inode);
648 ext4_group_t block_group;
649 ext4_grpblk_t colour;
650 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
651 ext4_fsblk_t bg_start;
652 ext4_fsblk_t last_block;
653
654 block_group = ei->i_block_group;
655 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
656 /*
657 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
658 * block groups per flexgroup, reserve the first block
659 * group for directories and special files. Regular
660 * files will start at the second block group. This
661 * tends to speed up directory access and improves
662 * fsck times.
663 */
664 block_group &= ~(flex_size-1);
665 if (S_ISREG(inode->i_mode))
666 block_group++;
667 }
668 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
669 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
670
671 /*
672 * If we are doing delayed allocation, we don't need take
673 * colour into account.
674 */
675 if (test_opt(inode->i_sb, DELALLOC))
676 return bg_start;
677
678 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
679 colour = (current->pid % 16) *
680 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
681 else
682 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
683 return bg_start + colour;
684}
685