blob: f18a919be70becf286c74364345a464e4d6e75e6 [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;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500191 int fatal = 0, err, count;
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400192 ext4_group_t flex_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193
194 if (atomic_read(&inode->i_count) > 1) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400195 printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
196 atomic_read(&inode->i_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197 return;
198 }
199 if (inode->i_nlink) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400200 printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
201 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202 return;
203 }
204 if (!sb) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400205 printk(KERN_ERR "ext4_free_inode: inode on "
206 "nonexistent device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207 return;
208 }
Mingming Cao617ba132006-10-11 01:20:53 -0700209 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210
211 ino = inode->i_ino;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400212 ext4_debug("freeing inode %lu\n", ino);
Theodore Ts'oba80b102009-01-03 20:03:21 -0500213 trace_mark(ext4_free_inode,
214 "dev %s ino %lu mode %d uid %lu gid %lu bocks %llu",
215 sb->s_id, inode->i_ino, inode->i_mode,
216 (unsigned long) inode->i_uid, (unsigned long) inode->i_gid,
217 (unsigned long long) inode->i_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218
219 /*
220 * Note: we must free any quota before locking the superblock,
221 * as writing the quota to disk may need the lock as well.
222 */
223 DQUOT_INIT(inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700224 ext4_xattr_delete_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 DQUOT_FREE_INODE(inode);
226 DQUOT_DROP(inode);
227
228 is_directory = S_ISDIR(inode->i_mode);
229
230 /* Do this BEFORE marking the inode not in use or returning an error */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400231 clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700232
Mingming Cao617ba132006-10-11 01:20:53 -0700233 es = EXT4_SB(sb)->s_es;
234 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400235 ext4_error(sb, "ext4_free_inode",
236 "reserved or nonexistent inode %lu", ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237 goto error_return;
238 }
Mingming Cao617ba132006-10-11 01:20:53 -0700239 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
240 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400241 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 if (!bitmap_bh)
243 goto error_return;
244
245 BUFFER_TRACE(bitmap_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700246 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700247 if (fatal)
248 goto error_return;
249
250 /* Ok, now we can actually update the inode bitmaps.. */
Mingming Cao617ba132006-10-11 01:20:53 -0700251 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252 bit, bitmap_bh->b_data))
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400253 ext4_error(sb, "ext4_free_inode",
254 "bit already cleared for inode %lu", ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700255 else {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400256 gdp = ext4_get_group_desc(sb, block_group, &bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257
258 BUFFER_TRACE(bh2, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700259 fatal = ext4_journal_get_write_access(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260 if (fatal) goto error_return;
261
262 if (gdp) {
263 spin_lock(sb_bgl_lock(sbi, block_group));
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500264 count = ext4_free_inodes_count(sb, gdp) + 1;
265 ext4_free_inodes_set(sb, gdp, count);
266 if (is_directory) {
267 count = ext4_used_dirs_count(sb, gdp) - 1;
268 ext4_used_dirs_set(sb, gdp, count);
269 }
Andreas Dilger717d50e2007-10-16 18:38:25 -0400270 gdp->bg_checksum = ext4_group_desc_csum(sbi,
271 block_group, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 spin_unlock(sb_bgl_lock(sbi, block_group));
273 percpu_counter_inc(&sbi->s_freeinodes_counter);
274 if (is_directory)
275 percpu_counter_dec(&sbi->s_dirs_counter);
276
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400277 if (sbi->s_log_groups_per_flex) {
278 flex_group = ext4_flex_group(sbi, block_group);
279 spin_lock(sb_bgl_lock(sbi, flex_group));
280 sbi->s_flex_groups[flex_group].free_inodes++;
281 spin_unlock(sb_bgl_lock(sbi, flex_group));
282 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283 }
Frank Mayhar03901312009-01-07 00:06:22 -0500284 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
285 err = ext4_handle_dirty_metadata(handle, NULL, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 if (!fatal) fatal = err;
287 }
Frank Mayhar03901312009-01-07 00:06:22 -0500288 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
289 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 if (!fatal)
291 fatal = err;
292 sb->s_dirt = 1;
293error_return:
294 brelse(bitmap_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700295 ext4_std_error(sb, fatal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700296}
297
298/*
299 * There are two policies for allocating an inode. If the new inode is
300 * a directory, then a forward search is made for a block group with both
301 * free space and a low directory-to-inode ratio; if that fails, then of
302 * the groups with above-average free space, that group with the fewest
303 * directories already is chosen.
304 *
305 * For other inodes, search forward from the parent directory\'s block
306 * group to find a free inode.
307 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500308static int find_group_dir(struct super_block *sb, struct inode *parent,
309 ext4_group_t *best_group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310{
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500311 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 unsigned int freei, avefreei;
Mingming Cao617ba132006-10-11 01:20:53 -0700313 struct ext4_group_desc *desc, *best_desc = NULL;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500314 ext4_group_t group;
315 int ret = -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316
Mingming Cao617ba132006-10-11 01:20:53 -0700317 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700318 avefreei = freei / ngroups;
319
320 for (group = 0; group < ngroups; group++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400321 desc = ext4_get_group_desc(sb, group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500322 if (!desc || !ext4_free_inodes_count(sb, desc))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500324 if (ext4_free_inodes_count(sb, desc) < avefreei)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700325 continue;
326 if (!best_desc ||
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500327 (ext4_free_blks_count(sb, desc) >
328 ext4_free_blks_count(sb, best_desc))) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500329 *best_group = group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700330 best_desc = desc;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500331 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700332 }
333 }
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500334 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700335}
336
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400337#define free_block_ratio 10
338
339static int find_group_flex(struct super_block *sb, struct inode *parent,
340 ext4_group_t *best_group)
341{
342 struct ext4_sb_info *sbi = EXT4_SB(sb);
343 struct ext4_group_desc *desc;
344 struct buffer_head *bh;
345 struct flex_groups *flex_group = sbi->s_flex_groups;
346 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
347 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
348 ext4_group_t ngroups = sbi->s_groups_count;
349 int flex_size = ext4_flex_bg_size(sbi);
350 ext4_group_t best_flex = parent_fbg_group;
351 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
352 int flexbg_free_blocks;
353 int flex_freeb_ratio;
354 ext4_group_t n_fbg_groups;
355 ext4_group_t i;
356
357 n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
358 sbi->s_log_groups_per_flex;
359
360find_close_to_parent:
361 flexbg_free_blocks = flex_group[best_flex].free_blocks;
362 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
363 if (flex_group[best_flex].free_inodes &&
364 flex_freeb_ratio > free_block_ratio)
365 goto found_flexbg;
366
367 if (best_flex && best_flex == parent_fbg_group) {
368 best_flex--;
369 goto find_close_to_parent;
370 }
371
372 for (i = 0; i < n_fbg_groups; i++) {
373 if (i == parent_fbg_group || i == parent_fbg_group - 1)
374 continue;
375
376 flexbg_free_blocks = flex_group[i].free_blocks;
377 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
378
379 if (flex_freeb_ratio > free_block_ratio &&
380 flex_group[i].free_inodes) {
381 best_flex = i;
382 goto found_flexbg;
383 }
384
Eric Sandeenc0010772008-08-19 22:19:50 -0400385 if (flex_group[best_flex].free_inodes == 0 ||
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400386 (flex_group[i].free_blocks >
387 flex_group[best_flex].free_blocks &&
388 flex_group[i].free_inodes))
389 best_flex = i;
390 }
391
392 if (!flex_group[best_flex].free_inodes ||
393 !flex_group[best_flex].free_blocks)
394 return -1;
395
396found_flexbg:
397 for (i = best_flex * flex_size; i < ngroups &&
398 i < (best_flex + 1) * flex_size; i++) {
399 desc = ext4_get_group_desc(sb, i, &bh);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500400 if (ext4_free_inodes_count(sb, desc)) {
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400401 *best_group = i;
402 goto out;
403 }
404 }
405
406 return -1;
407out:
408 return 0;
409}
410
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700411/*
412 * Orlov's allocator for directories.
413 *
414 * We always try to spread first-level directories.
415 *
416 * If there are blockgroups with both free inodes and free blocks counts
417 * not worse than average we return one with smallest directory count.
418 * Otherwise we simply return a random group.
419 *
420 * For the rest rules look so:
421 *
422 * It's OK to put directory into a group unless
423 * it has too many directories already (max_dirs) or
424 * it has too few free inodes left (min_inodes) or
425 * it has too few free blocks left (min_blocks) or
426 * it's already running too large debt (max_debt).
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000427 * Parent's group is preferred, if it doesn't satisfy these
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700428 * conditions we search cyclically through the rest. If none
429 * of the groups look good we just look for a group with more
430 * free inodes than average (starting at parent's group).
431 *
432 * Debt is incremented each time we allocate a directory and decremented
433 * when we allocate an inode, within 0--255.
434 */
435
436#define INODE_COST 64
437#define BLOCK_COST 256
438
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500439static int find_group_orlov(struct super_block *sb, struct inode *parent,
440 ext4_group_t *group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441{
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500442 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
Mingming Cao617ba132006-10-11 01:20:53 -0700443 struct ext4_sb_info *sbi = EXT4_SB(sb);
444 struct ext4_super_block *es = sbi->s_es;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500445 ext4_group_t ngroups = sbi->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700446 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 unsigned int freei, avefreei;
Mingming Cao617ba132006-10-11 01:20:53 -0700448 ext4_fsblk_t freeb, avefreeb;
449 ext4_fsblk_t blocks_per_dir;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450 unsigned int ndirs;
451 int max_debt, max_dirs, min_inodes;
Mingming Cao617ba132006-10-11 01:20:53 -0700452 ext4_grpblk_t min_blocks;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500453 ext4_group_t i;
Mingming Cao617ba132006-10-11 01:20:53 -0700454 struct ext4_group_desc *desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455
456 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
457 avefreei = freei / ngroups;
458 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700459 avefreeb = freeb;
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700460 do_div(avefreeb, ngroups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
462
463 if ((parent == sb->s_root->d_inode) ||
Mingming Cao617ba132006-10-11 01:20:53 -0700464 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700465 int best_ndir = inodes_per_group;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500466 ext4_group_t grp;
467 int ret = -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500469 get_random_bytes(&grp, sizeof(grp));
470 parent_group = (unsigned)grp % ngroups;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 for (i = 0; i < ngroups; i++) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500472 grp = (parent_group + i) % ngroups;
473 desc = ext4_get_group_desc(sb, grp, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500474 if (!desc || !ext4_free_inodes_count(sb, desc))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500476 if (ext4_used_dirs_count(sb, desc) >= best_ndir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700477 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500478 if (ext4_free_inodes_count(sb, desc) < avefreei)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500480 if (ext4_free_blks_count(sb, desc) < avefreeb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481 continue;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500482 *group = grp;
483 ret = 0;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500484 best_ndir = ext4_used_dirs_count(sb, desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700485 }
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500486 if (ret == 0)
487 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700488 goto fallback;
489 }
490
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700491 blocks_per_dir = ext4_blocks_count(es) - freeb;
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700492 do_div(blocks_per_dir, ndirs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493
494 max_dirs = ndirs / ngroups + inodes_per_group / 16;
495 min_inodes = avefreei - inodes_per_group / 4;
Mingming Cao617ba132006-10-11 01:20:53 -0700496 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700498 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700499 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500 if (max_debt * INODE_COST > inodes_per_group)
501 max_debt = inodes_per_group / INODE_COST;
502 if (max_debt > 255)
503 max_debt = 255;
504 if (max_debt == 0)
505 max_debt = 1;
506
507 for (i = 0; i < ngroups; i++) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500508 *group = (parent_group + i) % ngroups;
509 desc = ext4_get_group_desc(sb, *group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500510 if (!desc || !ext4_free_inodes_count(sb, desc))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500512 if (ext4_used_dirs_count(sb, desc) >= max_dirs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700513 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500514 if (ext4_free_inodes_count(sb, desc) < min_inodes)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500516 if (ext4_free_blks_count(sb, desc) < min_blocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700517 continue;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500518 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519 }
520
521fallback:
522 for (i = 0; i < ngroups; i++) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500523 *group = (parent_group + i) % ngroups;
524 desc = ext4_get_group_desc(sb, *group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500525 if (desc && ext4_free_inodes_count(sb, desc) &&
526 ext4_free_inodes_count(sb, desc) >= avefreei)
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500527 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528 }
529
530 if (avefreei) {
531 /*
532 * The free-inodes counter is approximate, and for really small
533 * filesystems the above test can fail to find any blockgroups
534 */
535 avefreei = 0;
536 goto fallback;
537 }
538
539 return -1;
540}
541
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500542static int find_group_other(struct super_block *sb, struct inode *parent,
543 ext4_group_t *group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544{
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500545 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
546 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700547 struct ext4_group_desc *desc;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500548 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549
550 /*
551 * Try to place the inode in its parent directory
552 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500553 *group = parent_group;
554 desc = ext4_get_group_desc(sb, *group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500555 if (desc && ext4_free_inodes_count(sb, desc) &&
556 ext4_free_blks_count(sb, desc))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500557 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558
559 /*
560 * We're going to place this inode in a different blockgroup from its
561 * parent. We want to cause files in a common directory to all land in
562 * the same blockgroup. But we want files which are in a different
563 * directory which shares a blockgroup with our parent to land in a
564 * different blockgroup.
565 *
566 * So add our directory's i_ino into the starting point for the hash.
567 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500568 *group = (*group + parent->i_ino) % ngroups;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700569
570 /*
571 * Use a quadratic hash to find a group with a free inode and some free
572 * blocks.
573 */
574 for (i = 1; i < ngroups; i <<= 1) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500575 *group += i;
576 if (*group >= ngroups)
577 *group -= ngroups;
578 desc = ext4_get_group_desc(sb, *group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500579 if (desc && ext4_free_inodes_count(sb, desc) &&
580 ext4_free_blks_count(sb, desc))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500581 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700582 }
583
584 /*
585 * That failed: try linear search for a free inode, even if that group
586 * has no free blocks.
587 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500588 *group = parent_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 for (i = 0; i < ngroups; i++) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500590 if (++*group >= ngroups)
591 *group = 0;
592 desc = ext4_get_group_desc(sb, *group, NULL);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500593 if (desc && ext4_free_inodes_count(sb, desc))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500594 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700595 }
596
597 return -1;
598}
599
600/*
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500601 * claim the inode from the inode bitmap. If the group
602 * is uninit we need to take the groups's sb_bgl_lock
603 * and clear the uninit flag. The inode bitmap update
604 * and group desc uninit flag clear should be done
605 * after holding sb_bgl_lock so that ext4_read_inode_bitmap
606 * doesn't race with the ext4_claim_inode
607 */
608static int ext4_claim_inode(struct super_block *sb,
609 struct buffer_head *inode_bitmap_bh,
610 unsigned long ino, ext4_group_t group, int mode)
611{
612 int free = 0, retval = 0, count;
613 struct ext4_sb_info *sbi = EXT4_SB(sb);
614 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
615
616 spin_lock(sb_bgl_lock(sbi, group));
617 if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
618 /* not a free inode */
619 retval = 1;
620 goto err_ret;
621 }
622 ino++;
623 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
624 ino > EXT4_INODES_PER_GROUP(sb)) {
625 spin_unlock(sb_bgl_lock(sbi, group));
626 ext4_error(sb, __func__,
627 "reserved inode or inode > inodes count - "
628 "block_group = %u, inode=%lu", group,
629 ino + group * EXT4_INODES_PER_GROUP(sb));
630 return 1;
631 }
632 /* If we didn't allocate from within the initialized part of the inode
633 * table then we need to initialize up to this inode. */
634 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
635
636 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
637 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
638 /* When marking the block group with
639 * ~EXT4_BG_INODE_UNINIT we don't want to depend
640 * on the value of bg_itable_unused even though
641 * mke2fs could have initialized the same for us.
642 * Instead we calculated the value below
643 */
644
645 free = 0;
646 } else {
647 free = EXT4_INODES_PER_GROUP(sb) -
648 ext4_itable_unused_count(sb, gdp);
649 }
650
651 /*
652 * Check the relative inode number against the last used
653 * relative inode number in this group. if it is greater
654 * we need to update the bg_itable_unused count
655 *
656 */
657 if (ino > free)
658 ext4_itable_unused_set(sb, gdp,
659 (EXT4_INODES_PER_GROUP(sb) - ino));
660 }
661 count = ext4_free_inodes_count(sb, gdp) - 1;
662 ext4_free_inodes_set(sb, gdp, count);
663 if (S_ISDIR(mode)) {
664 count = ext4_used_dirs_count(sb, gdp) + 1;
665 ext4_used_dirs_set(sb, gdp, count);
666 }
667 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
668err_ret:
669 spin_unlock(sb_bgl_lock(sbi, group));
670 return retval;
671}
672
673/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674 * There are two policies for allocating an inode. If the new inode is
675 * a directory, then a forward search is made for a block group with both
676 * free space and a low directory-to-inode ratio; if that fails, then of
677 * the groups with above-average free space, that group with the fewest
678 * directories already is chosen.
679 *
680 * For other inodes, search forward from the parent directory's block
681 * group to find a free inode.
682 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400683struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700684{
685 struct super_block *sb;
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500686 struct buffer_head *inode_bitmap_bh = NULL;
687 struct buffer_head *group_desc_bh;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500688 ext4_group_t group = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700689 unsigned long ino = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400690 struct inode *inode;
691 struct ext4_group_desc *gdp = NULL;
692 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -0700693 struct ext4_inode_info *ei;
694 struct ext4_sb_info *sbi;
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500695 int ret2, err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 struct inode *ret;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500697 ext4_group_t i;
698 int free = 0;
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400699 ext4_group_t flex_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700
701 /* Cannot create files in a deleted directory */
702 if (!dir || !dir->i_nlink)
703 return ERR_PTR(-EPERM);
704
705 sb = dir->i_sb;
Theodore Ts'oba80b102009-01-03 20:03:21 -0500706 trace_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id,
707 dir->i_ino, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 inode = new_inode(sb);
709 if (!inode)
710 return ERR_PTR(-ENOMEM);
Mingming Cao617ba132006-10-11 01:20:53 -0700711 ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700712
Mingming Cao617ba132006-10-11 01:20:53 -0700713 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 es = sbi->s_es;
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400715
716 if (sbi->s_log_groups_per_flex) {
717 ret2 = find_group_flex(sb, dir, &group);
Theodore Ts'o05bf9e82009-02-21 12:13:24 -0500718 if (ret2 == -1) {
719 ret2 = find_group_other(sb, dir, &group);
720 if (ret2 == 0 && printk_ratelimit())
721 printk(KERN_NOTICE "ext4: find_group_flex "
722 "failed, fallback succeeded dir %lu\n",
723 dir->i_ino);
724 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400725 goto got_group;
726 }
727
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 if (S_ISDIR(mode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400729 if (test_opt(sb, OLDALLOC))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500730 ret2 = find_group_dir(sb, dir, &group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700731 else
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500732 ret2 = find_group_orlov(sb, dir, &group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 } else
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500734 ret2 = find_group_other(sb, dir, &group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400736got_group:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737 err = -ENOSPC;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500738 if (ret2 == -1)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 goto out;
740
741 for (i = 0; i < sbi->s_groups_count; i++) {
742 err = -EIO;
743
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500744 gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 if (!gdp)
746 goto fail;
747
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500748 brelse(inode_bitmap_bh);
749 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
750 if (!inode_bitmap_bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751 goto fail;
752
753 ino = 0;
754
755repeat_in_this_group:
Mingming Cao617ba132006-10-11 01:20:53 -0700756 ino = ext4_find_next_zero_bit((unsigned long *)
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500757 inode_bitmap_bh->b_data,
758 EXT4_INODES_PER_GROUP(sb), ino);
759
Mingming Cao617ba132006-10-11 01:20:53 -0700760 if (ino < EXT4_INODES_PER_GROUP(sb)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500762 BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
763 err = ext4_journal_get_write_access(handle,
764 inode_bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765 if (err)
766 goto fail;
767
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500768 BUFFER_TRACE(group_desc_bh, "get_write_access");
769 err = ext4_journal_get_write_access(handle,
770 group_desc_bh);
771 if (err)
772 goto fail;
773 if (!ext4_claim_inode(sb, inode_bitmap_bh,
774 ino, group, mode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775 /* we won it */
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500776 BUFFER_TRACE(inode_bitmap_bh,
Frank Mayhar03901312009-01-07 00:06:22 -0500777 "call ext4_handle_dirty_metadata");
778 err = ext4_handle_dirty_metadata(handle,
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500779 inode,
780 inode_bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781 if (err)
782 goto fail;
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500783 /* zero bit is inode number 1*/
784 ino++;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 goto got;
786 }
787 /* we lost it */
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500788 ext4_handle_release_buffer(handle, inode_bitmap_bh);
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500789 ext4_handle_release_buffer(handle, group_desc_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700790
Mingming Cao617ba132006-10-11 01:20:53 -0700791 if (++ino < EXT4_INODES_PER_GROUP(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 goto repeat_in_this_group;
793 }
794
795 /*
796 * This case is possible in concurrent environment. It is very
797 * rare. We cannot repeat the find_group_xxx() call because
798 * that will simply return the same blockgroup, because the
799 * group descriptor metadata has not yet been updated.
800 * So we just go onto the next blockgroup.
801 */
802 if (++group == sbi->s_groups_count)
803 group = 0;
804 }
805 err = -ENOSPC;
806 goto out;
807
808got:
Andreas Dilger717d50e2007-10-16 18:38:25 -0400809 /* We may have to initialize the block bitmap if it isn't already */
810 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
811 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500812 struct buffer_head *block_bitmap_bh;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400813
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500814 block_bitmap_bh = ext4_read_block_bitmap(sb, group);
815 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
816 err = ext4_journal_get_write_access(handle, block_bitmap_bh);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400817 if (err) {
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500818 brelse(block_bitmap_bh);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400819 goto fail;
820 }
821
822 free = 0;
823 spin_lock(sb_bgl_lock(sbi, group));
824 /* recheck and clear flag under lock if we still need to */
825 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
Andreas Dilger717d50e2007-10-16 18:38:25 -0400826 free = ext4_free_blocks_after_init(sb, group, gdp);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500827 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500828 ext4_free_blks_set(sb, gdp, free);
Frederic Bohe23712a92008-11-07 09:21:01 -0500829 gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
830 gdp);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400831 }
832 spin_unlock(sb_bgl_lock(sbi, group));
833
834 /* Don't need to dirty bitmap block if we didn't change it */
835 if (free) {
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500836 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
Frank Mayhar03901312009-01-07 00:06:22 -0500837 err = ext4_handle_dirty_metadata(handle,
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500838 NULL, block_bitmap_bh);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400839 }
840
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500841 brelse(block_bitmap_bh);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400842 if (err)
843 goto fail;
844 }
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500845 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
846 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
Aneesh Kumar K.V39341862009-01-05 21:38:14 -0500847 if (err)
848 goto fail;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700849
850 percpu_counter_dec(&sbi->s_freeinodes_counter);
851 if (S_ISDIR(mode))
852 percpu_counter_inc(&sbi->s_dirs_counter);
853 sb->s_dirt = 1;
854
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400855 if (sbi->s_log_groups_per_flex) {
856 flex_group = ext4_flex_group(sbi, group);
857 spin_lock(sb_bgl_lock(sbi, flex_group));
858 sbi->s_flex_groups[flex_group].free_inodes--;
859 spin_unlock(sb_bgl_lock(sbi, flex_group));
860 }
861
David Howells4c9c5442008-11-14 10:38:51 +1100862 inode->i_uid = current_fsuid();
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400863 if (test_opt(sb, GRPID))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864 inode->i_gid = dir->i_gid;
865 else if (dir->i_mode & S_ISGID) {
866 inode->i_gid = dir->i_gid;
867 if (S_ISDIR(mode))
868 mode |= S_ISGID;
869 } else
David Howells4c9c5442008-11-14 10:38:51 +1100870 inode->i_gid = current_fsgid();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871 inode->i_mode = mode;
872
Andreas Dilger717d50e2007-10-16 18:38:25 -0400873 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700874 /* This is the optimal IO size (for stat), not the fs block size */
875 inode->i_blocks = 0;
Kalpak Shahef7f3832007-07-18 09:15:20 -0400876 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
877 ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878
879 memset(ei->i_data, 0, sizeof(ei->i_data));
880 ei->i_dir_start_lookup = 0;
881 ei->i_disksize = 0;
882
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -0500883 /*
884 * Don't inherit extent flag from directory. We set extent flag on
885 * newly created directory and file only if -o extent mount option is
886 * specified
887 */
888 ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 if (S_ISLNK(mode))
Mingming Cao617ba132006-10-11 01:20:53 -0700890 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 /* dirsync only applies to directories */
892 if (!S_ISDIR(mode))
Mingming Cao617ba132006-10-11 01:20:53 -0700893 ei->i_flags &= ~EXT4_DIRSYNC_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700894 ei->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895 ei->i_dtime = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896 ei->i_block_group = group;
897
Mingming Cao617ba132006-10-11 01:20:53 -0700898 ext4_set_inode_flags(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700899 if (IS_DIRSYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500900 ext4_handle_sync(handle);
Al Viro6b38e842008-12-30 02:03:31 -0500901 if (insert_inode_locked(inode) < 0) {
902 err = -EINVAL;
903 goto fail_drop;
904 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700905 spin_lock(&sbi->s_next_gen_lock);
906 inode->i_generation = sbi->s_next_generation++;
907 spin_unlock(&sbi->s_next_gen_lock);
908
Mingming Cao617ba132006-10-11 01:20:53 -0700909 ei->i_state = EXT4_STATE_NEW;
Kalpak Shahef7f3832007-07-18 09:15:20 -0400910
911 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912
913 ret = inode;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400914 if (DQUOT_ALLOC_INODE(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915 err = -EDQUOT;
916 goto fail_drop;
917 }
918
Mingming Cao617ba132006-10-11 01:20:53 -0700919 err = ext4_init_acl(handle, inode, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700920 if (err)
921 goto fail_free_drop;
922
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400923 err = ext4_init_security(handle, inode, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700924 if (err)
925 goto fail_free_drop;
926
Theodore Ts'o83982b62009-01-06 14:53:16 -0500927 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Eric Sandeene4079a12008-07-11 19:27:31 -0400928 /* set extent flag only for directory, file and normal symlink*/
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -0400929 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -0500930 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
931 ext4_ext_tree_init(handle, inode);
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -0500932 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700933 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934
Aneesh Kumar K.V8753e882008-04-29 22:00:36 -0400935 err = ext4_mark_inode_dirty(handle, inode);
936 if (err) {
937 ext4_std_error(sb, err);
938 goto fail_free_drop;
939 }
940
Mingming Cao617ba132006-10-11 01:20:53 -0700941 ext4_debug("allocating inode %lu\n", inode->i_ino);
Theodore Ts'oba80b102009-01-03 20:03:21 -0500942 trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d",
943 sb->s_id, inode->i_ino, dir->i_ino, mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700944 goto really_out;
945fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700946 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947out:
948 iput(inode);
949 ret = ERR_PTR(err);
950really_out:
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500951 brelse(inode_bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 return ret;
953
954fail_free_drop:
955 DQUOT_FREE_INODE(inode);
956
957fail_drop:
958 DQUOT_DROP(inode);
959 inode->i_flags |= S_NOQUOTA;
960 inode->i_nlink = 0;
Al Viro6b38e842008-12-30 02:03:31 -0500961 unlock_new_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962 iput(inode);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500963 brelse(inode_bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964 return ERR_PTR(err);
965}
966
967/* Verify that we are loading a valid orphan from disk */
Mingming Cao617ba132006-10-11 01:20:53 -0700968struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700969{
Mingming Cao617ba132006-10-11 01:20:53 -0700970 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500971 ext4_group_t block_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972 int bit;
David Howells1d1fe1e2008-02-07 00:15:37 -0800973 struct buffer_head *bitmap_bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 struct inode *inode = NULL;
David Howells1d1fe1e2008-02-07 00:15:37 -0800975 long err = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976
977 /* Error cases - e2fsck has already cleaned up for us */
978 if (ino > max_ino) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400979 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700980 "bad orphan ino %lu! e2fsck was run?", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -0800981 goto error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 }
983
Mingming Cao617ba132006-10-11 01:20:53 -0700984 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
985 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400986 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700987 if (!bitmap_bh) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400988 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989 "inode bitmap error for orphan %lu", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -0800990 goto error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 }
992
993 /* Having the inode bit set should be a 100% indicator that this
994 * is a valid orphan (no e2fsck run on fs). Orphans also include
995 * inodes that were being truncated, so we can't check i_nlink==0.
996 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800997 if (!ext4_test_bit(bit, bitmap_bh->b_data))
998 goto bad_orphan;
999
1000 inode = ext4_iget(sb, ino);
1001 if (IS_ERR(inode))
1002 goto iget_failed;
1003
Duane Griffin91ef4ca2008-07-11 19:27:31 -04001004 /*
1005 * If the orphans has i_nlinks > 0 then it should be able to be
1006 * truncated, otherwise it won't be removed from the orphan list
1007 * during processing and an infinite loop will result.
1008 */
1009 if (inode->i_nlink && !ext4_can_truncate(inode))
1010 goto bad_orphan;
1011
David Howells1d1fe1e2008-02-07 00:15:37 -08001012 if (NEXT_ORPHAN(inode) > max_ino)
1013 goto bad_orphan;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014 brelse(bitmap_bh);
1015 return inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08001016
1017iget_failed:
1018 err = PTR_ERR(inode);
1019 inode = NULL;
1020bad_orphan:
Harvey Harrison46e665e2008-04-17 10:38:59 -04001021 ext4_warning(sb, __func__,
David Howells1d1fe1e2008-02-07 00:15:37 -08001022 "bad orphan inode %lu! e2fsck was run?", ino);
1023 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1024 bit, (unsigned long long)bitmap_bh->b_blocknr,
1025 ext4_test_bit(bit, bitmap_bh->b_data));
1026 printk(KERN_NOTICE "inode=%p\n", inode);
1027 if (inode) {
1028 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
1029 is_bad_inode(inode));
1030 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
1031 NEXT_ORPHAN(inode));
1032 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
Duane Griffin91ef4ca2008-07-11 19:27:31 -04001033 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
David Howells1d1fe1e2008-02-07 00:15:37 -08001034 /* Avoid freeing blocks if we got a bad deleted inode */
1035 if (inode->i_nlink == 0)
1036 inode->i_blocks = 0;
1037 iput(inode);
1038 }
1039 brelse(bitmap_bh);
1040error:
1041 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001042}
1043
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001044unsigned long ext4_count_free_inodes(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001045{
1046 unsigned long desc_count;
Mingming Cao617ba132006-10-11 01:20:53 -07001047 struct ext4_group_desc *gdp;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001048 ext4_group_t i;
Mingming Cao617ba132006-10-11 01:20:53 -07001049#ifdef EXT4FS_DEBUG
1050 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 unsigned long bitmap_count, x;
1052 struct buffer_head *bitmap_bh = NULL;
1053
Mingming Cao617ba132006-10-11 01:20:53 -07001054 es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 desc_count = 0;
1056 bitmap_count = 0;
1057 gdp = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001058 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001059 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001060 if (!gdp)
1061 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001062 desc_count += ext4_free_inodes_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 brelse(bitmap_bh);
Eric Sandeene29d1cd2008-08-02 21:21:02 -04001064 bitmap_bh = ext4_read_inode_bitmap(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001065 if (!bitmap_bh)
1066 continue;
1067
Mingming Cao617ba132006-10-11 01:20:53 -07001068 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
Eric Sandeenc549a952008-01-28 23:58:27 -05001069 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001070 i, ext4_free_inodes_count(sb, gdp), x);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 bitmap_count += x;
1072 }
1073 brelse(bitmap_bh);
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001074 printk(KERN_DEBUG "ext4_count_free_inodes: "
1075 "stored = %u, computed = %lu, %lu\n",
1076 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001077 return desc_count;
1078#else
1079 desc_count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001080 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001081 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001082 if (!gdp)
1083 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001084 desc_count += ext4_free_inodes_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001085 cond_resched();
1086 }
1087 return desc_count;
1088#endif
1089}
1090
1091/* Called at mount-time, super-block is locked */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001092unsigned long ext4_count_dirs(struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001093{
1094 unsigned long count = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001095 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096
Mingming Cao617ba132006-10-11 01:20:53 -07001097 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001098 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001099 if (!gdp)
1100 continue;
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001101 count += ext4_used_dirs_count(sb, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 }
1103 return count;
1104}