blob: 47b84e8df56859ca1e741cfb6543b5966ee45657 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/ialloc.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 * BSD ufs-inspired inode and directory allocation by
10 * Stephen Tweedie (sct@redhat.com), 1993
11 * Big-endian to little-endian byte-swapping/bitmaps by
12 * David S. Miller (davem@caip.rutgers.edu), 1995
13 */
14
15#include <linux/time.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/stat.h>
19#include <linux/string.h>
20#include <linux/quotaops.h>
21#include <linux/buffer_head.h>
22#include <linux/random.h>
23#include <linux/bitops.h>
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070024#include <linux/blkdev.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070025#include <asm/byteorder.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040026#include "ext4.h"
27#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070028#include "xattr.h"
29#include "acl.h"
Andreas Dilger717d50e2007-10-16 18:38:25 -040030#include "group.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031
32/*
33 * ialloc.c contains the inodes allocation and deallocation routines
34 */
35
36/*
37 * The free inodes are managed by bitmaps. A file system contains several
38 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
39 * block for inodes, N blocks for the inode table and data blocks.
40 *
41 * The file system contains group descriptors which are located after the
42 * super block. Each descriptor contains the number of the bitmap block and
43 * the free blocks count in the block.
44 */
45
Andreas Dilger717d50e2007-10-16 18:38:25 -040046/*
47 * To avoid calling the atomic setbit hundreds or thousands of times, we only
48 * need to use it within a single byte (to ensure we get endianness right).
49 * We can use memset for the rest of the bitmap as there are no other users.
50 */
51void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
52{
53 int i;
54
55 if (start_bit >= end_bit)
56 return;
57
58 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
59 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
60 ext4_set_bit(i, bitmap);
61 if (i < end_bit)
62 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
63}
64
65/* Initializes an uninitialized inode bitmap */
Avantika Mathurfd2d4292008-01-28 23:58:27 -050066unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
67 ext4_group_t block_group,
Andreas Dilger717d50e2007-10-16 18:38:25 -040068 struct ext4_group_desc *gdp)
69{
70 struct ext4_sb_info *sbi = EXT4_SB(sb);
71
72 J_ASSERT_BH(bh, buffer_locked(bh));
73
74 /* If checksum is bad mark all blocks and inodes use to prevent
75 * allocation, essentially implementing a per-group read-only flag. */
76 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -050077 ext4_error(sb, __func__, "Checksum bad for group %u",
Andreas Dilger717d50e2007-10-16 18:38:25 -040078 block_group);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -050079 ext4_free_blks_set(sb, gdp, 0);
80 ext4_free_inodes_set(sb, gdp, 0);
81 ext4_itable_unused_set(sb, gdp, 0);
Andreas Dilger717d50e2007-10-16 18:38:25 -040082 memset(bh->b_data, 0xff, sb->s_blocksize);
83 return 0;
84 }
85
86 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
Aneesh Kumar K.V648f5872009-01-05 21:46:04 -050087 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
Andreas Dilger717d50e2007-10-16 18:38:25 -040088 bh->b_data);
89
90 return EXT4_INODES_PER_GROUP(sb);
91}
Dave Kleikampac27a0e2006-10-11 01:20:50 -070092
93/*
94 * Read the inode allocation bitmap for a given block_group, reading
95 * into the specified slot in the superblock's bitmap cache.
96 *
97 * Return buffer_head of bitmap on success or NULL.
98 */
99static struct buffer_head *
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400100ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700101{
Mingming Cao617ba132006-10-11 01:20:53 -0700102 struct ext4_group_desc *desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700103 struct buffer_head *bh = NULL;
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400104 ext4_fsblk_t bitmap_blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105
Mingming Cao617ba132006-10-11 01:20:53 -0700106 desc = ext4_get_group_desc(sb, block_group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700107 if (!desc)
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400108 return NULL;
109 bitmap_blk = ext4_inode_bitmap(sb, desc);
110 bh = sb_getblk(sb, bitmap_blk);
111 if (unlikely(!bh)) {
112 ext4_error(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700113 "Cannot read inode bitmap - "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500114 "block_group = %u, inode_bitmap = %llu",
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400115 block_group, bitmap_blk);
116 return NULL;
117 }
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500118 if (bitmap_uptodate(bh))
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400119 return bh;
120
Frederic Bohec806e682008-10-10 08:09:18 -0400121 lock_buffer(bh);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500122 if (bitmap_uptodate(bh)) {
123 unlock_buffer(bh);
124 return bh;
125 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400126 spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400127 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
128 ext4_init_inode_bitmap(sb, bh, block_group, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500129 set_bitmap_uptodate(bh);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400130 set_buffer_uptodate(bh);
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400131 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500132 unlock_buffer(bh);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400133 return bh;
134 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400135 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500136 if (buffer_uptodate(bh)) {
137 /*
138 * if not uninit if bh is uptodate,
139 * bitmap is also uptodate
140 */
141 set_bitmap_uptodate(bh);
142 unlock_buffer(bh);
143 return bh;
144 }
145 /*
146 * submit the buffer_head for read. We can
147 * safely mark the bitmap as uptodate now.
148 * We do it here so the bitmap uptodate bit
149 * get set with buffer lock held.
150 */
151 set_bitmap_uptodate(bh);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400152 if (bh_submit_read(bh) < 0) {
153 put_bh(bh);
154 ext4_error(sb, __func__,
155 "Cannot read inode bitmap - "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500156 "block_group = %u, inode_bitmap = %llu",
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400157 block_group, bitmap_blk);
158 return NULL;
159 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160 return bh;
161}
162
163/*
164 * NOTE! When we get the inode, we're the only people
165 * that have access to it, and as such there are no
166 * race conditions we have to worry about. The inode
167 * is not on the hash-lists, and it cannot be reached
168 * through the filesystem because the directory entry
169 * has been deleted earlier.
170 *
171 * HOWEVER: we must make sure that we get no aliases,
172 * which means that we have to call "clear_inode()"
173 * _before_ we mark the inode not in use in the inode
174 * bitmaps. Otherwise a newly created file might use
175 * the same inode number (not actually the same pointer
176 * though), and then we'd have two inodes sharing the
177 * same inode number and space on the harddisk.
178 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400179void ext4_free_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400181 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182 int is_directory;
183 unsigned long ino;
184 struct buffer_head *bitmap_bh = NULL;
185 struct buffer_head *bh2;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500186 ext4_group_t block_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187 unsigned long bit;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400188 struct ext4_group_desc *gdp;
189 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -0700190 struct ext4_sb_info *sbi;
Eric Sandeen7ce9d5d2009-03-04 18:38:18 -0500191 int fatal = 0, err, count, cleared;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192
193 if (atomic_read(&inode->i_count) > 1) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400194 printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
195 atomic_read(&inode->i_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700196 return;
197 }
198 if (inode->i_nlink) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400199 printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
200 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700201 return;
202 }
203 if (!sb) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400204 printk(KERN_ERR "ext4_free_inode: inode on "
205 "nonexistent device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700206 return;
207 }
Mingming Cao617ba132006-10-11 01:20:53 -0700208 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700209
210 ino = inode->i_ino;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400211 ext4_debug("freeing inode %lu\n", ino);
Theodore Ts'oba80b102009-01-03 20:03:21 -0500212 trace_mark(ext4_free_inode,
213 "dev %s ino %lu mode %d uid %lu gid %lu bocks %llu",
214 sb->s_id, inode->i_ino, inode->i_mode,
215 (unsigned long) inode->i_uid, (unsigned long) inode->i_gid,
216 (unsigned long long) inode->i_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700217
218 /*
219 * Note: we must free any quota before locking the superblock,
220 * as writing the quota to disk may need the lock as well.
221 */
Jan Karaa269eb12009-01-26 17:04:39 +0100222 vfs_dq_init(inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700223 ext4_xattr_delete_inode(handle, inode);
Jan Karaa269eb12009-01-26 17:04:39 +0100224 vfs_dq_free_inode(inode);
225 vfs_dq_drop(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226
227 is_directory = S_ISDIR(inode->i_mode);
228
229 /* Do this BEFORE marking the inode not in use or returning an error */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400230 clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231
Mingming Cao617ba132006-10-11 01:20:53 -0700232 es = EXT4_SB(sb)->s_es;
233 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400234 ext4_error(sb, "ext4_free_inode",
235 "reserved or nonexistent inode %lu", ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700236 goto error_return;
237 }
Mingming Cao617ba132006-10-11 01:20:53 -0700238 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
239 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400240 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241 if (!bitmap_bh)
242 goto error_return;
243
244 BUFFER_TRACE(bitmap_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700245 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246 if (fatal)
247 goto error_return;
248
249 /* Ok, now we can actually update the inode bitmaps.. */
Eric Sandeen7ce9d5d2009-03-04 18:38:18 -0500250 spin_lock(sb_bgl_lock(sbi, block_group));
251 cleared = ext4_clear_bit(bit, bitmap_bh->b_data);
252 spin_unlock(sb_bgl_lock(sbi, block_group));
253 if (!cleared)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400254 ext4_error(sb, "ext4_free_inode",
255 "bit already cleared for inode %lu", ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 else {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400257 gdp = ext4_get_group_desc(sb, block_group, &bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700258
259 BUFFER_TRACE(bh2, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700260 fatal = ext4_journal_get_write_access(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 if (fatal) goto error_return;
262
263 if (gdp) {
264 spin_lock(sb_bgl_lock(sbi, block_group));
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500265 count = ext4_free_inodes_count(sb, gdp) + 1;
266 ext4_free_inodes_set(sb, gdp, count);
267 if (is_directory) {
268 count = ext4_used_dirs_count(sb, gdp) - 1;
269 ext4_used_dirs_set(sb, gdp, count);
Theodore Ts'o7d39db12009-03-04 19:31:53 -0500270 if (sbi->s_log_groups_per_flex) {
271 ext4_group_t f;
272
273 f = ext4_flex_group(sbi, block_group);
274 atomic_dec(&sbi->s_flex_groups[f].free_inodes);
275 }
276
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500277 }
Andreas Dilger717d50e2007-10-16 18:38:25 -0400278 gdp->bg_checksum = ext4_group_desc_csum(sbi,
279 block_group, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700280 spin_unlock(sb_bgl_lock(sbi, block_group));
281 percpu_counter_inc(&sbi->s_freeinodes_counter);
282 if (is_directory)
283 percpu_counter_dec(&sbi->s_dirs_counter);
284
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400285 if (sbi->s_log_groups_per_flex) {
Theodore Ts'o9f24e422009-03-04 19:09:10 -0500286 ext4_group_t f;
287
288 f = ext4_flex_group(sbi, block_group);
289 atomic_inc(&sbi->s_flex_groups[f].free_inodes);
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400290 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291 }
Frank Mayhar03901312009-01-07 00:06:22 -0500292 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
293 err = ext4_handle_dirty_metadata(handle, NULL, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294 if (!fatal) fatal = err;
295 }
Frank Mayhar03901312009-01-07 00:06:22 -0500296 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
297 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298 if (!fatal)
299 fatal = err;
300 sb->s_dirt = 1;
301error_return:
302 brelse(bitmap_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700303 ext4_std_error(sb, fatal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304}
305
306/*
307 * There are two policies for allocating an inode. If the new inode is
308 * a directory, then a forward search is made for a block group with both
309 * free space and a low directory-to-inode ratio; if that fails, then of
310 * the groups with above-average free space, that group with the fewest
311 * directories already is chosen.
312 *
313 * For other inodes, search forward from the parent directory\'s block
314 * group to find a free inode.
315 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500316static int find_group_dir(struct super_block *sb, struct inode *parent,
317 ext4_group_t *best_group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700318{
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500319 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320 unsigned int freei, avefreei;
Mingming Cao617ba132006-10-11 01:20:53 -0700321 struct ext4_group_desc *desc, *best_desc = NULL;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500322 ext4_group_t group;
323 int ret = -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324
Mingming Cao617ba132006-10-11 01:20:53 -0700325 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700326 avefreei = freei / ngroups;
327
328 for (group = 0; group < ngroups; group++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400329 desc = ext4_get_group_desc(sb, group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500330 if (!desc || !ext4_free_inodes_count(sb, desc))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500332 if (ext4_free_inodes_count(sb, desc) < avefreei)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700333 continue;
334 if (!best_desc ||
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500335 (ext4_free_blks_count(sb, desc) >
336 ext4_free_blks_count(sb, best_desc))) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500337 *best_group = group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338 best_desc = desc;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500339 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700340 }
341 }
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500342 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700343}
344
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400345#define free_block_ratio 10
346
347static int find_group_flex(struct super_block *sb, struct inode *parent,
348 ext4_group_t *best_group)
349{
350 struct ext4_sb_info *sbi = EXT4_SB(sb);
351 struct ext4_group_desc *desc;
352 struct buffer_head *bh;
353 struct flex_groups *flex_group = sbi->s_flex_groups;
354 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
355 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
356 ext4_group_t ngroups = sbi->s_groups_count;
357 int flex_size = ext4_flex_bg_size(sbi);
358 ext4_group_t best_flex = parent_fbg_group;
359 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
360 int flexbg_free_blocks;
361 int flex_freeb_ratio;
362 ext4_group_t n_fbg_groups;
363 ext4_group_t i;
364
365 n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
366 sbi->s_log_groups_per_flex;
367
368find_close_to_parent:
Theodore Ts'o9f24e422009-03-04 19:09:10 -0500369 flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400370 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
Theodore Ts'o9f24e422009-03-04 19:09:10 -0500371 if (atomic_read(&flex_group[best_flex].free_inodes) &&
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400372 flex_freeb_ratio > free_block_ratio)
373 goto found_flexbg;
374
375 if (best_flex && best_flex == parent_fbg_group) {
376 best_flex--;
377 goto find_close_to_parent;
378 }
379
380 for (i = 0; i < n_fbg_groups; i++) {
381 if (i == parent_fbg_group || i == parent_fbg_group - 1)
382 continue;
383
Theodore Ts'o9f24e422009-03-04 19:09:10 -0500384 flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400385 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
386
387 if (flex_freeb_ratio > free_block_ratio &&
Theodore Ts'o9f24e422009-03-04 19:09:10 -0500388 (atomic_read(&flex_group[i].free_inodes))) {
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400389 best_flex = i;
390 goto found_flexbg;
391 }
392
Theodore Ts'o9f24e422009-03-04 19:09:10 -0500393 if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) ||
394 ((atomic_read(&flex_group[i].free_blocks) >
395 atomic_read(&flex_group[best_flex].free_blocks)) &&
396 atomic_read(&flex_group[i].free_inodes)))
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400397 best_flex = i;
398 }
399
Theodore Ts'o9f24e422009-03-04 19:09:10 -0500400 if (!atomic_read(&flex_group[best_flex].free_inodes) ||
401 !atomic_read(&flex_group[best_flex].free_blocks))
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400402 return -1;
403
404found_flexbg:
405 for (i = best_flex * flex_size; i < ngroups &&
406 i < (best_flex + 1) * flex_size; i++) {
407 desc = ext4_get_group_desc(sb, i, &bh);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500408 if (ext4_free_inodes_count(sb, desc)) {
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400409 *best_group = i;
410 goto out;
411 }
412 }
413
414 return -1;
415out:
416 return 0;
417}
418
Theodore Ts'oa4912122009-03-12 12:18:34 -0400419struct orlov_stats {
420 __u32 free_inodes;
421 __u32 free_blocks;
422 __u32 used_dirs;
423};
424
425/*
426 * Helper function for Orlov's allocator; returns critical information
427 * for a particular block group or flex_bg. If flex_size is 1, then g
428 * is a block group number; otherwise it is flex_bg number.
429 */
430void get_orlov_stats(struct super_block *sb, ext4_group_t g,
431 int flex_size, struct orlov_stats *stats)
432{
433 struct ext4_group_desc *desc;
Theodore Ts'o7d39db12009-03-04 19:31:53 -0500434 struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400435
Theodore Ts'o7d39db12009-03-04 19:31:53 -0500436 if (flex_size > 1) {
437 stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
438 stats->free_blocks = atomic_read(&flex_group[g].free_blocks);
439 stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
440 return;
441 }
Theodore Ts'oa4912122009-03-12 12:18:34 -0400442
Theodore Ts'o7d39db12009-03-04 19:31:53 -0500443 desc = ext4_get_group_desc(sb, g, NULL);
444 if (desc) {
445 stats->free_inodes = ext4_free_inodes_count(sb, desc);
446 stats->free_blocks = ext4_free_blks_count(sb, desc);
447 stats->used_dirs = ext4_used_dirs_count(sb, desc);
448 } else {
449 stats->free_inodes = 0;
450 stats->free_blocks = 0;
451 stats->used_dirs = 0;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400452 }
453}
454
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455/*
456 * Orlov's allocator for directories.
457 *
458 * We always try to spread first-level directories.
459 *
460 * If there are blockgroups with both free inodes and free blocks counts
461 * not worse than average we return one with smallest directory count.
462 * Otherwise we simply return a random group.
463 *
464 * For the rest rules look so:
465 *
466 * It's OK to put directory into a group unless
467 * it has too many directories already (max_dirs) or
468 * it has too few free inodes left (min_inodes) or
469 * it has too few free blocks left (min_blocks) or
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000470 * Parent's group is preferred, if it doesn't satisfy these
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 * conditions we search cyclically through the rest. If none
472 * of the groups look good we just look for a group with more
473 * free inodes than average (starting at parent's group).
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474 */
475
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500476static int find_group_orlov(struct super_block *sb, struct inode *parent,
Theodore Ts'oa4912122009-03-12 12:18:34 -0400477 ext4_group_t *group, int mode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478{
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500479 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
Mingming Cao617ba132006-10-11 01:20:53 -0700480 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500481 ext4_group_t ngroups = sbi->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700482 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 unsigned int freei, avefreei;
Mingming Cao617ba132006-10-11 01:20:53 -0700484 ext4_fsblk_t freeb, avefreeb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700485 unsigned int ndirs;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400486 int max_dirs, min_inodes;
Mingming Cao617ba132006-10-11 01:20:53 -0700487 ext4_grpblk_t min_blocks;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400488 ext4_group_t i, grp, g;
Mingming Cao617ba132006-10-11 01:20:53 -0700489 struct ext4_group_desc *desc;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400490 struct orlov_stats stats;
491 int flex_size = ext4_flex_bg_size(sbi);
492
493 if (flex_size > 1) {
494 ngroups = (ngroups + flex_size - 1) >>
495 sbi->s_log_groups_per_flex;
496 parent_group >>= sbi->s_log_groups_per_flex;
497 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498
499 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
500 avefreei = freei / ngroups;
501 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700502 avefreeb = freeb;
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700503 do_div(avefreeb, ngroups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
505
Theodore Ts'oa4912122009-03-12 12:18:34 -0400506 if (S_ISDIR(mode) &&
507 ((parent == sb->s_root->d_inode) ||
508 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509 int best_ndir = inodes_per_group;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500510 int ret = -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500512 get_random_bytes(&grp, sizeof(grp));
513 parent_group = (unsigned)grp % ngroups;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 for (i = 0; i < ngroups; i++) {
Theodore Ts'oa4912122009-03-12 12:18:34 -0400515 g = (parent_group + i) % ngroups;
516 get_orlov_stats(sb, g, flex_size, &stats);
517 if (!stats.free_inodes)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518 continue;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400519 if (stats.used_dirs >= best_ndir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520 continue;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400521 if (stats.free_inodes < avefreei)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522 continue;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400523 if (stats.free_blocks < avefreeb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524 continue;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400525 grp = g;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500526 ret = 0;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400527 best_ndir = stats.used_dirs;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528 }
Theodore Ts'oa4912122009-03-12 12:18:34 -0400529 if (ret)
530 goto fallback;
531 found_flex_bg:
532 if (flex_size == 1) {
533 *group = grp;
534 return 0;
535 }
536
537 /*
538 * We pack inodes at the beginning of the flexgroup's
539 * inode tables. Block allocation decisions will do
540 * something similar, although regular files will
541 * start at 2nd block group of the flexgroup. See
542 * ext4_ext_find_goal() and ext4_find_near().
543 */
544 grp *= flex_size;
545 for (i = 0; i < flex_size; i++) {
546 if (grp+i >= sbi->s_groups_count)
547 break;
548 desc = ext4_get_group_desc(sb, grp+i, NULL);
549 if (desc && ext4_free_inodes_count(sb, desc)) {
550 *group = grp+i;
551 return 0;
552 }
553 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 goto fallback;
555 }
556
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557 max_dirs = ndirs / ngroups + inodes_per_group / 16;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400558 min_inodes = avefreei - inodes_per_group*flex_size / 4;
559 if (min_inodes < 1)
560 min_inodes = 1;
561 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb)*flex_size / 4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562
Theodore Ts'oa4912122009-03-12 12:18:34 -0400563 /*
564 * Start looking in the flex group where we last allocated an
565 * inode for this parent directory
566 */
567 if (EXT4_I(parent)->i_last_alloc_group != ~0) {
568 parent_group = EXT4_I(parent)->i_last_alloc_group;
569 if (flex_size > 1)
570 parent_group >>= sbi->s_log_groups_per_flex;
571 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572
573 for (i = 0; i < ngroups; i++) {
Theodore Ts'oa4912122009-03-12 12:18:34 -0400574 grp = (parent_group + i) % ngroups;
575 get_orlov_stats(sb, grp, flex_size, &stats);
576 if (stats.used_dirs >= max_dirs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577 continue;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400578 if (stats.free_inodes < min_inodes)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579 continue;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400580 if (stats.free_blocks < min_blocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581 continue;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400582 goto found_flex_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583 }
584
585fallback:
Theodore Ts'oa4912122009-03-12 12:18:34 -0400586 ngroups = sbi->s_groups_count;
587 avefreei = freei / ngroups;
588 parent_group = EXT4_I(parent)->i_block_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 for (i = 0; i < ngroups; i++) {
Theodore Ts'oa4912122009-03-12 12:18:34 -0400590 grp = (parent_group + i) % ngroups;
591 desc = ext4_get_group_desc(sb, grp, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500592 if (desc && ext4_free_inodes_count(sb, desc) &&
Theodore Ts'oa4912122009-03-12 12:18:34 -0400593 ext4_free_inodes_count(sb, desc) >= avefreei) {
594 *group = grp;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500595 return 0;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400596 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597 }
598
599 if (avefreei) {
600 /*
601 * The free-inodes counter is approximate, and for really small
602 * filesystems the above test can fail to find any blockgroups
603 */
604 avefreei = 0;
605 goto fallback;
606 }
607
608 return -1;
609}
610
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500611static int find_group_other(struct super_block *sb, struct inode *parent,
Theodore Ts'oa4912122009-03-12 12:18:34 -0400612 ext4_group_t *group, int mode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700613{
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500614 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
615 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700616 struct ext4_group_desc *desc;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400617 ext4_group_t i, last;
618 int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
619
620 /*
621 * Try to place the inode is the same flex group as its
622 * parent. If we can't find space, use the Orlov algorithm to
623 * find another flex group, and store that information in the
624 * parent directory's inode information so that use that flex
625 * group for future allocations.
626 */
627 if (flex_size > 1) {
628 int retry = 0;
629
630 try_again:
631 parent_group &= ~(flex_size-1);
632 last = parent_group + flex_size;
633 if (last > ngroups)
634 last = ngroups;
635 for (i = parent_group; i < last; i++) {
636 desc = ext4_get_group_desc(sb, i, NULL);
637 if (desc && ext4_free_inodes_count(sb, desc)) {
638 *group = i;
639 return 0;
640 }
641 }
642 if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
643 retry = 1;
644 parent_group = EXT4_I(parent)->i_last_alloc_group;
645 goto try_again;
646 }
647 /*
648 * If this didn't work, use the Orlov search algorithm
649 * to find a new flex group; we pass in the mode to
650 * avoid the topdir algorithms.
651 */
652 *group = parent_group + flex_size;
653 if (*group > ngroups)
654 *group = 0;
655 return find_group_orlov(sb, parent, group, mode);
656 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657
658 /*
659 * Try to place the inode in its parent directory
660 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500661 *group = parent_group;
662 desc = ext4_get_group_desc(sb, *group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500663 if (desc && ext4_free_inodes_count(sb, desc) &&
664 ext4_free_blks_count(sb, desc))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500665 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700666
667 /*
668 * We're going to place this inode in a different blockgroup from its
669 * parent. We want to cause files in a common directory to all land in
670 * the same blockgroup. But we want files which are in a different
671 * directory which shares a blockgroup with our parent to land in a
672 * different blockgroup.
673 *
674 * So add our directory's i_ino into the starting point for the hash.
675 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500676 *group = (*group + parent->i_ino) % ngroups;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700677
678 /*
679 * Use a quadratic hash to find a group with a free inode and some free
680 * blocks.
681 */
682 for (i = 1; i < ngroups; i <<= 1) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500683 *group += i;
684 if (*group >= ngroups)
685 *group -= ngroups;
686 desc = ext4_get_group_desc(sb, *group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500687 if (desc && ext4_free_inodes_count(sb, desc) &&
688 ext4_free_blks_count(sb, desc))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500689 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700690 }
691
692 /*
693 * That failed: try linear search for a free inode, even if that group
694 * has no free blocks.
695 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500696 *group = parent_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697 for (i = 0; i < ngroups; i++) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500698 if (++*group >= ngroups)
699 *group = 0;
700 desc = ext4_get_group_desc(sb, *group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500701 if (desc && ext4_free_inodes_count(sb, desc))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500702 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703 }
704
705 return -1;
706}
707
708/*
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500709 * claim the inode from the inode bitmap. If the group
710 * is uninit we need to take the groups's sb_bgl_lock
711 * and clear the uninit flag. The inode bitmap update
712 * and group desc uninit flag clear should be done
713 * after holding sb_bgl_lock so that ext4_read_inode_bitmap
714 * doesn't race with the ext4_claim_inode
715 */
716static int ext4_claim_inode(struct super_block *sb,
717 struct buffer_head *inode_bitmap_bh,
718 unsigned long ino, ext4_group_t group, int mode)
719{
720 int free = 0, retval = 0, count;
721 struct ext4_sb_info *sbi = EXT4_SB(sb);
722 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
723
724 spin_lock(sb_bgl_lock(sbi, group));
725 if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
726 /* not a free inode */
727 retval = 1;
728 goto err_ret;
729 }
730 ino++;
731 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
732 ino > EXT4_INODES_PER_GROUP(sb)) {
733 spin_unlock(sb_bgl_lock(sbi, group));
734 ext4_error(sb, __func__,
735 "reserved inode or inode > inodes count - "
736 "block_group = %u, inode=%lu", group,
737 ino + group * EXT4_INODES_PER_GROUP(sb));
738 return 1;
739 }
740 /* If we didn't allocate from within the initialized part of the inode
741 * table then we need to initialize up to this inode. */
742 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
743
744 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
745 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
746 /* When marking the block group with
747 * ~EXT4_BG_INODE_UNINIT we don't want to depend
748 * on the value of bg_itable_unused even though
749 * mke2fs could have initialized the same for us.
750 * Instead we calculated the value below
751 */
752
753 free = 0;
754 } else {
755 free = EXT4_INODES_PER_GROUP(sb) -
756 ext4_itable_unused_count(sb, gdp);
757 }
758
759 /*
760 * Check the relative inode number against the last used
761 * relative inode number in this group. if it is greater
762 * we need to update the bg_itable_unused count
763 *
764 */
765 if (ino > free)
766 ext4_itable_unused_set(sb, gdp,
767 (EXT4_INODES_PER_GROUP(sb) - ino));
768 }
769 count = ext4_free_inodes_count(sb, gdp) - 1;
770 ext4_free_inodes_set(sb, gdp, count);
771 if (S_ISDIR(mode)) {
772 count = ext4_used_dirs_count(sb, gdp) + 1;
773 ext4_used_dirs_set(sb, gdp, count);
Theodore Ts'o7d39db12009-03-04 19:31:53 -0500774 if (sbi->s_log_groups_per_flex) {
775 ext4_group_t f = ext4_flex_group(sbi, group);
776
777 atomic_inc(&sbi->s_flex_groups[f].free_inodes);
778 }
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500779 }
780 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
781err_ret:
782 spin_unlock(sb_bgl_lock(sbi, group));
783 return retval;
784}
785
786/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787 * There are two policies for allocating an inode. If the new inode is
788 * a directory, then a forward search is made for a block group with both
789 * free space and a low directory-to-inode ratio; if that fails, then of
790 * the groups with above-average free space, that group with the fewest
791 * directories already is chosen.
792 *
793 * For other inodes, search forward from the parent directory's block
794 * group to find a free inode.
795 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400796struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700797{
798 struct super_block *sb;
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500799 struct buffer_head *inode_bitmap_bh = NULL;
800 struct buffer_head *group_desc_bh;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500801 ext4_group_t group = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700802 unsigned long ino = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400803 struct inode *inode;
804 struct ext4_group_desc *gdp = NULL;
805 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -0700806 struct ext4_inode_info *ei;
807 struct ext4_sb_info *sbi;
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500808 int ret2, err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 struct inode *ret;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500810 ext4_group_t i;
811 int free = 0;
Theodore Ts'o2842c3b2009-03-12 12:20:01 -0400812 static int once = 1;
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400813 ext4_group_t flex_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814
815 /* Cannot create files in a deleted directory */
816 if (!dir || !dir->i_nlink)
817 return ERR_PTR(-EPERM);
818
819 sb = dir->i_sb;
Theodore Ts'oba80b102009-01-03 20:03:21 -0500820 trace_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id,
821 dir->i_ino, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700822 inode = new_inode(sb);
823 if (!inode)
824 return ERR_PTR(-ENOMEM);
Mingming Cao617ba132006-10-11 01:20:53 -0700825 ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700826
Mingming Cao617ba132006-10-11 01:20:53 -0700827 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700828 es = sbi->s_es;
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400829
Theodore Ts'oa4912122009-03-12 12:18:34 -0400830 if (sbi->s_log_groups_per_flex && test_opt(sb, OLDALLOC)) {
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400831 ret2 = find_group_flex(sb, dir, &group);
Theodore Ts'o05bf9e82009-02-21 12:13:24 -0500832 if (ret2 == -1) {
Theodore Ts'oa4912122009-03-12 12:18:34 -0400833 ret2 = find_group_other(sb, dir, &group, mode);
Theodore Ts'o2842c3b2009-03-12 12:20:01 -0400834 if (ret2 == 0 && once)
835 once = 0;
Theodore Ts'o05bf9e82009-02-21 12:13:24 -0500836 printk(KERN_NOTICE "ext4: find_group_flex "
837 "failed, fallback succeeded dir %lu\n",
838 dir->i_ino);
839 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400840 goto got_group;
841 }
842
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700843 if (S_ISDIR(mode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400844 if (test_opt(sb, OLDALLOC))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500845 ret2 = find_group_dir(sb, dir, &group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 else
Theodore Ts'oa4912122009-03-12 12:18:34 -0400847 ret2 = find_group_orlov(sb, dir, &group, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700848 } else
Theodore Ts'oa4912122009-03-12 12:18:34 -0400849 ret2 = find_group_other(sb, dir, &group, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400851got_group:
Theodore Ts'oa4912122009-03-12 12:18:34 -0400852 EXT4_I(dir)->i_last_alloc_group = group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700853 err = -ENOSPC;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500854 if (ret2 == -1)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855 goto out;
856
857 for (i = 0; i < sbi->s_groups_count; i++) {
858 err = -EIO;
859
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500860 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700861 if (!gdp)
862 goto fail;
863
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500864 brelse(inode_bitmap_bh);
865 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
866 if (!inode_bitmap_bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867 goto fail;
868
869 ino = 0;
870
871repeat_in_this_group:
Mingming Cao617ba132006-10-11 01:20:53 -0700872 ino = ext4_find_next_zero_bit((unsigned long *)
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500873 inode_bitmap_bh->b_data,
874 EXT4_INODES_PER_GROUP(sb), ino);
875
Mingming Cao617ba132006-10-11 01:20:53 -0700876 if (ino < EXT4_INODES_PER_GROUP(sb)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500878 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
879 err = ext4_journal_get_write_access(handle,
880 inode_bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881 if (err)
882 goto fail;
883
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500884 BUFFER_TRACE(group_desc_bh, "get_write_access");
885 err = ext4_journal_get_write_access(handle,
886 group_desc_bh);
887 if (err)
888 goto fail;
889 if (!ext4_claim_inode(sb, inode_bitmap_bh,
890 ino, group, mode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 /* we won it */
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500892 BUFFER_TRACE(inode_bitmap_bh,
Frank Mayhar03901312009-01-07 00:06:22 -0500893 "call ext4_handle_dirty_metadata");
894 err = ext4_handle_dirty_metadata(handle,
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500895 inode,
896 inode_bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897 if (err)
898 goto fail;
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500899 /* zero bit is inode number 1*/
900 ino++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 goto got;
902 }
903 /* we lost it */
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500904 ext4_handle_release_buffer(handle, inode_bitmap_bh);
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500905 ext4_handle_release_buffer(handle, group_desc_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906
Mingming Cao617ba132006-10-11 01:20:53 -0700907 if (++ino < EXT4_INODES_PER_GROUP(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 goto repeat_in_this_group;
909 }
910
911 /*
912 * This case is possible in concurrent environment. It is very
913 * rare. We cannot repeat the find_group_xxx() call because
914 * that will simply return the same blockgroup, because the
915 * group descriptor metadata has not yet been updated.
916 * So we just go onto the next blockgroup.
917 */
918 if (++group == sbi->s_groups_count)
919 group = 0;
920 }
921 err = -ENOSPC;
922 goto out;
923
924got:
Andreas Dilger717d50e2007-10-16 18:38:25 -0400925 /* We may have to initialize the block bitmap if it isn't already */
926 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
927 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500928 struct buffer_head *block_bitmap_bh;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400929
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500930 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
931 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
932 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400933 if (err) {
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500934 brelse(block_bitmap_bh);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400935 goto fail;
936 }
937
938 free = 0;
939 spin_lock(sb_bgl_lock(sbi, group));
940 /* recheck and clear flag under lock if we still need to */
941 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
Andreas Dilger717d50e2007-10-16 18:38:25 -0400942 free = ext4_free_blocks_after_init(sb, group, gdp);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500943 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500944 ext4_free_blks_set(sb, gdp, free);
Frederic Bohe23712a92008-11-07 09:21:01 -0500945 gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
946 gdp);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400947 }
948 spin_unlock(sb_bgl_lock(sbi, group));
949
950 /* Don't need to dirty bitmap block if we didn't change it */
951 if (free) {
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500952 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
Frank Mayhar03901312009-01-07 00:06:22 -0500953 err = ext4_handle_dirty_metadata(handle,
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500954 NULL, block_bitmap_bh);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400955 }
956
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500957 brelse(block_bitmap_bh);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400958 if (err)
959 goto fail;
960 }
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500961 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
962 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500963 if (err)
964 goto fail;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965
966 percpu_counter_dec(&sbi->s_freeinodes_counter);
967 if (S_ISDIR(mode))
968 percpu_counter_inc(&sbi->s_dirs_counter);
969 sb->s_dirt = 1;
970
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400971 if (sbi->s_log_groups_per_flex) {
972 flex_group = ext4_flex_group(sbi, group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -0500973 atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400974 }
975
David Howells4c9c5442008-11-14 10:38:51 +1100976 inode->i_uid = current_fsuid();
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400977 if (test_opt(sb, GRPID))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 inode->i_gid = dir->i_gid;
979 else if (dir->i_mode & S_ISGID) {
980 inode->i_gid = dir->i_gid;
981 if (S_ISDIR(mode))
982 mode |= S_ISGID;
983 } else
David Howells4c9c5442008-11-14 10:38:51 +1100984 inode->i_gid = current_fsgid();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985 inode->i_mode = mode;
986
Andreas Dilger717d50e2007-10-16 18:38:25 -0400987 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700988 /* This is the optimal IO size (for stat), not the fs block size */
989 inode->i_blocks = 0;
Kalpak Shahef7f3832007-07-18 09:15:20 -0400990 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
991 ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992
993 memset(ei->i_data, 0, sizeof(ei->i_data));
994 ei->i_dir_start_lookup = 0;
995 ei->i_disksize = 0;
996
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -0500997 /*
Duane Griffin2dc6b0d2009-02-15 18:09:20 -0500998 * Don't inherit extent flag from directory, amongst others. We set
999 * extent flag on newly created directory and file only if -o extent
1000 * mount option is specified
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -05001001 */
Duane Griffin2dc6b0d2009-02-15 18:09:20 -05001002 ei->i_flags =
1003 ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004 ei->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 ei->i_dtime = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001006 ei->i_block_group = group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001007 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001008
Mingming Cao617ba132006-10-11 01:20:53 -07001009 ext4_set_inode_flags(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010 if (IS_DIRSYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05001011 ext4_handle_sync(handle);
Al Viro6b38e842008-12-30 02:03:31 -05001012 if (insert_inode_locked(inode) < 0) {
1013 err = -EINVAL;
1014 goto fail_drop;
1015 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016 spin_lock(&sbi->s_next_gen_lock);
1017 inode->i_generation = sbi->s_next_generation++;
1018 spin_unlock(&sbi->s_next_gen_lock);
1019
Mingming Cao617ba132006-10-11 01:20:53 -07001020 ei->i_state = EXT4_STATE_NEW;
Kalpak Shahef7f3832007-07-18 09:15:20 -04001021
1022 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001023
1024 ret = inode;
Jan Karaa269eb12009-01-26 17:04:39 +01001025 if (vfs_dq_alloc_inode(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001026 err = -EDQUOT;
1027 goto fail_drop;
1028 }
1029
Mingming Cao617ba132006-10-11 01:20:53 -07001030 err = ext4_init_acl(handle, inode, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001031 if (err)
1032 goto fail_free_drop;
1033
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001034 err = ext4_init_security(handle, inode, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035 if (err)
1036 goto fail_free_drop;
1037
Theodore Ts'o83982b62009-01-06 14:53:16 -05001038 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Eric Sandeene4079a12008-07-11 19:27:31 -04001039 /* set extent flag only for directory, file and normal symlink*/
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -04001040 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -05001041 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
1042 ext4_ext_tree_init(handle, inode);
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -05001043 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001044 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001045
Aneesh Kumar K.V8753e882008-04-29 22:00:36 -04001046 err = ext4_mark_inode_dirty(handle, inode);
1047 if (err) {
1048 ext4_std_error(sb, err);
1049 goto fail_free_drop;
1050 }
1051
Mingming Cao617ba132006-10-11 01:20:53 -07001052 ext4_debug("allocating inode %lu\n", inode->i_ino);
Theodore Ts'oba80b102009-01-03 20:03:21 -05001053 trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d",
1054 sb->s_id, inode->i_ino, dir->i_ino, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 goto really_out;
1056fail:
Mingming Cao617ba132006-10-11 01:20:53 -07001057 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058out:
1059 iput(inode);
1060 ret = ERR_PTR(err);
1061really_out:
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -05001062 brelse(inode_bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 return ret;
1064
1065fail_free_drop:
Jan Karaa269eb12009-01-26 17:04:39 +01001066 vfs_dq_free_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067
1068fail_drop:
Jan Karaa269eb12009-01-26 17:04:39 +01001069 vfs_dq_drop(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 inode->i_flags |= S_NOQUOTA;
1071 inode->i_nlink = 0;
Al Viro6b38e842008-12-30 02:03:31 -05001072 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001073 iput(inode);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -05001074 brelse(inode_bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075 return ERR_PTR(err);
1076}
1077
1078/* Verify that we are loading a valid orphan from disk */
Mingming Cao617ba132006-10-11 01:20:53 -07001079struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001080{
Mingming Cao617ba132006-10-11 01:20:53 -07001081 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001082 ext4_group_t block_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001083 int bit;
David Howells1d1fe1e2008-02-07 00:15:37 -08001084 struct buffer_head *bitmap_bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001085 struct inode *inode = NULL;
David Howells1d1fe1e2008-02-07 00:15:37 -08001086 long err = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001087
1088 /* Error cases - e2fsck has already cleaned up for us */
1089 if (ino > max_ino) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001090 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091 "bad orphan ino %lu! e2fsck was run?", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001092 goto error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001093 }
1094
Mingming Cao617ba132006-10-11 01:20:53 -07001095 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1096 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
Eric Sandeene29d1cd2008-08-02 21:21:02 -04001097 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098 if (!bitmap_bh) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001099 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001100 "inode bitmap error for orphan %lu", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -08001101 goto error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 }
1103
1104 /* Having the inode bit set should be a 100% indicator that this
1105 * is a valid orphan (no e2fsck run on fs). Orphans also include
1106 * inodes that were being truncated, so we can't check i_nlink==0.
1107 */
David Howells1d1fe1e2008-02-07 00:15:37 -08001108 if (!ext4_test_bit(bit, bitmap_bh->b_data))
1109 goto bad_orphan;
1110
1111 inode = ext4_iget(sb, ino);
1112 if (IS_ERR(inode))
1113 goto iget_failed;
1114
Duane Griffin91ef4ca2008-07-11 19:27:31 -04001115 /*
1116 * If the orphans has i_nlinks > 0 then it should be able to be
1117 * truncated, otherwise it won't be removed from the orphan list
1118 * during processing and an infinite loop will result.
1119 */
1120 if (inode->i_nlink && !ext4_can_truncate(inode))
1121 goto bad_orphan;
1122
David Howells1d1fe1e2008-02-07 00:15:37 -08001123 if (NEXT_ORPHAN(inode) > max_ino)
1124 goto bad_orphan;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001125 brelse(bitmap_bh);
1126 return inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08001127
1128iget_failed:
1129 err = PTR_ERR(inode);
1130 inode = NULL;
1131bad_orphan:
Harvey Harrison46e665e2008-04-17 10:38:59 -04001132 ext4_warning(sb, __func__,
David Howells1d1fe1e2008-02-07 00:15:37 -08001133 "bad orphan inode %lu! e2fsck was run?", ino);
1134 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1135 bit, (unsigned long long)bitmap_bh->b_blocknr,
1136 ext4_test_bit(bit, bitmap_bh->b_data));
1137 printk(KERN_NOTICE "inode=%p\n", inode);
1138 if (inode) {
1139 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
1140 is_bad_inode(inode));
1141 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
1142 NEXT_ORPHAN(inode));
1143 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
Duane Griffin91ef4ca2008-07-11 19:27:31 -04001144 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
David Howells1d1fe1e2008-02-07 00:15:37 -08001145 /* Avoid freeing blocks if we got a bad deleted inode */
1146 if (inode->i_nlink == 0)
1147 inode->i_blocks = 0;
1148 iput(inode);
1149 }
1150 brelse(bitmap_bh);
1151error:
1152 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001153}
1154
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001155unsigned long ext4_count_free_inodes(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001156{
1157 unsigned long desc_count;
Mingming Cao617ba132006-10-11 01:20:53 -07001158 struct ext4_group_desc *gdp;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001159 ext4_group_t i;
Mingming Cao617ba132006-10-11 01:20:53 -07001160#ifdef EXT4FS_DEBUG
1161 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001162 unsigned long bitmap_count, x;
1163 struct buffer_head *bitmap_bh = NULL;
1164
Mingming Cao617ba132006-10-11 01:20:53 -07001165 es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166 desc_count = 0;
1167 bitmap_count = 0;
1168 gdp = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001169 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001170 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171 if (!gdp)
1172 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001173 desc_count += ext4_free_inodes_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 brelse(bitmap_bh);
Eric Sandeene29d1cd2008-08-02 21:21:02 -04001175 bitmap_bh = ext4_read_inode_bitmap(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001176 if (!bitmap_bh)
1177 continue;
1178
Mingming Cao617ba132006-10-11 01:20:53 -07001179 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
Eric Sandeenc549a952008-01-28 23:58:27 -05001180 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001181 i, ext4_free_inodes_count(sb, gdp), x);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001182 bitmap_count += x;
1183 }
1184 brelse(bitmap_bh);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001185 printk(KERN_DEBUG "ext4_count_free_inodes: "
1186 "stored = %u, computed = %lu, %lu\n",
1187 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188 return desc_count;
1189#else
1190 desc_count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001191 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001192 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001193 if (!gdp)
1194 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001195 desc_count += ext4_free_inodes_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001196 cond_resched();
1197 }
1198 return desc_count;
1199#endif
1200}
1201
1202/* Called at mount-time, super-block is locked */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001203unsigned long ext4_count_dirs(struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204{
1205 unsigned long count = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001206 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001207
Mingming Cao617ba132006-10-11 01:20:53 -07001208 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001209 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001210 if (!gdp)
1211 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001212 count += ext4_used_dirs_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001213 }
1214 return count;
1215}