blob: 5e66a2feef0973e388536f428436bf2b264760c1 [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)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -040077 ext4_error(sb, __func__, "Checksum bad for group %lu\n",
Andreas Dilger717d50e2007-10-16 18:38:25 -040078 block_group);
79 gdp->bg_free_blocks_count = 0;
80 gdp->bg_free_inodes_count = 0;
81 gdp->bg_itable_unused = 0;
82 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);
87 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
88 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 - "
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700114 "block_group = %lu, inode_bitmap = %llu",
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400115 block_group, bitmap_blk);
116 return NULL;
117 }
118 if (bh_uptodate_or_lock(bh))
119 return bh;
120
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400121 spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400122 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
123 ext4_init_inode_bitmap(sb, bh, block_group, desc);
124 set_buffer_uptodate(bh);
125 unlock_buffer(bh);
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400126 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400127 return bh;
128 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400129 spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400130 if (bh_submit_read(bh) < 0) {
131 put_bh(bh);
132 ext4_error(sb, __func__,
133 "Cannot read inode bitmap - "
134 "block_group = %lu, inode_bitmap = %llu",
135 block_group, bitmap_blk);
136 return NULL;
137 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138 return bh;
139}
140
141/*
142 * NOTE! When we get the inode, we're the only people
143 * that have access to it, and as such there are no
144 * race conditions we have to worry about. The inode
145 * is not on the hash-lists, and it cannot be reached
146 * through the filesystem because the directory entry
147 * has been deleted earlier.
148 *
149 * HOWEVER: we must make sure that we get no aliases,
150 * which means that we have to call "clear_inode()"
151 * _before_ we mark the inode not in use in the inode
152 * bitmaps. Otherwise a newly created file might use
153 * the same inode number (not actually the same pointer
154 * though), and then we'd have two inodes sharing the
155 * same inode number and space on the harddisk.
156 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400157void ext4_free_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400159 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160 int is_directory;
161 unsigned long ino;
162 struct buffer_head *bitmap_bh = NULL;
163 struct buffer_head *bh2;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500164 ext4_group_t block_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165 unsigned long bit;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400166 struct ext4_group_desc *gdp;
167 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -0700168 struct ext4_sb_info *sbi;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 int fatal = 0, err;
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400170 ext4_group_t flex_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171
172 if (atomic_read(&inode->i_count) > 1) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400173 printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
174 atomic_read(&inode->i_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700175 return;
176 }
177 if (inode->i_nlink) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400178 printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
179 inode->i_nlink);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180 return;
181 }
182 if (!sb) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400183 printk(KERN_ERR "ext4_free_inode: inode on "
184 "nonexistent device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 return;
186 }
Mingming Cao617ba132006-10-11 01:20:53 -0700187 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700188
189 ino = inode->i_ino;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400190 ext4_debug("freeing inode %lu\n", ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700191
192 /*
193 * Note: we must free any quota before locking the superblock,
194 * as writing the quota to disk may need the lock as well.
195 */
196 DQUOT_INIT(inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700197 ext4_xattr_delete_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198 DQUOT_FREE_INODE(inode);
199 DQUOT_DROP(inode);
200
201 is_directory = S_ISDIR(inode->i_mode);
202
203 /* Do this BEFORE marking the inode not in use or returning an error */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400204 clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205
Mingming Cao617ba132006-10-11 01:20:53 -0700206 es = EXT4_SB(sb)->s_es;
207 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400208 ext4_error(sb, "ext4_free_inode",
209 "reserved or nonexistent inode %lu", ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 goto error_return;
211 }
Mingming Cao617ba132006-10-11 01:20:53 -0700212 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
213 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400214 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215 if (!bitmap_bh)
216 goto error_return;
217
218 BUFFER_TRACE(bitmap_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700219 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220 if (fatal)
221 goto error_return;
222
223 /* Ok, now we can actually update the inode bitmaps.. */
Mingming Cao617ba132006-10-11 01:20:53 -0700224 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 bit, bitmap_bh->b_data))
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400226 ext4_error(sb, "ext4_free_inode",
227 "bit already cleared for inode %lu", ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228 else {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400229 gdp = ext4_get_group_desc(sb, block_group, &bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700230
231 BUFFER_TRACE(bh2, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700232 fatal = ext4_journal_get_write_access(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233 if (fatal) goto error_return;
234
235 if (gdp) {
236 spin_lock(sb_bgl_lock(sbi, block_group));
Marcin Slusarze8546d02008-04-17 10:38:59 -0400237 le16_add_cpu(&gdp->bg_free_inodes_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700238 if (is_directory)
Marcin Slusarze8546d02008-04-17 10:38:59 -0400239 le16_add_cpu(&gdp->bg_used_dirs_count, -1);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400240 gdp->bg_checksum = ext4_group_desc_csum(sbi,
241 block_group, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 spin_unlock(sb_bgl_lock(sbi, block_group));
243 percpu_counter_inc(&sbi->s_freeinodes_counter);
244 if (is_directory)
245 percpu_counter_dec(&sbi->s_dirs_counter);
246
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400247 if (sbi->s_log_groups_per_flex) {
248 flex_group = ext4_flex_group(sbi, block_group);
249 spin_lock(sb_bgl_lock(sbi, flex_group));
250 sbi->s_flex_groups[flex_group].free_inodes++;
251 spin_unlock(sb_bgl_lock(sbi, flex_group));
252 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253 }
Mingming Cao617ba132006-10-11 01:20:53 -0700254 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
255 err = ext4_journal_dirty_metadata(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 if (!fatal) fatal = err;
257 }
Mingming Cao617ba132006-10-11 01:20:53 -0700258 BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
259 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260 if (!fatal)
261 fatal = err;
262 sb->s_dirt = 1;
263error_return:
264 brelse(bitmap_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700265 ext4_std_error(sb, fatal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266}
267
268/*
269 * There are two policies for allocating an inode. If the new inode is
270 * a directory, then a forward search is made for a block group with both
271 * free space and a low directory-to-inode ratio; if that fails, then of
272 * the groups with above-average free space, that group with the fewest
273 * directories already is chosen.
274 *
275 * For other inodes, search forward from the parent directory\'s block
276 * group to find a free inode.
277 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500278static int find_group_dir(struct super_block *sb, struct inode *parent,
279 ext4_group_t *best_group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700280{
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500281 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 unsigned int freei, avefreei;
Mingming Cao617ba132006-10-11 01:20:53 -0700283 struct ext4_group_desc *desc, *best_desc = NULL;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500284 ext4_group_t group;
285 int ret = -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286
Mingming Cao617ba132006-10-11 01:20:53 -0700287 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 avefreei = freei / ngroups;
289
290 for (group = 0; group < ngroups; group++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400291 desc = ext4_get_group_desc(sb, group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 if (!desc || !desc->bg_free_inodes_count)
293 continue;
294 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
295 continue;
296 if (!best_desc ||
297 (le16_to_cpu(desc->bg_free_blocks_count) >
298 le16_to_cpu(best_desc->bg_free_blocks_count))) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500299 *best_group = group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300 best_desc = desc;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500301 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302 }
303 }
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500304 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305}
306
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400307#define free_block_ratio 10
308
309static int find_group_flex(struct super_block *sb, struct inode *parent,
310 ext4_group_t *best_group)
311{
312 struct ext4_sb_info *sbi = EXT4_SB(sb);
313 struct ext4_group_desc *desc;
314 struct buffer_head *bh;
315 struct flex_groups *flex_group = sbi->s_flex_groups;
316 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
317 ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
318 ext4_group_t ngroups = sbi->s_groups_count;
319 int flex_size = ext4_flex_bg_size(sbi);
320 ext4_group_t best_flex = parent_fbg_group;
321 int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
322 int flexbg_free_blocks;
323 int flex_freeb_ratio;
324 ext4_group_t n_fbg_groups;
325 ext4_group_t i;
326
327 n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
328 sbi->s_log_groups_per_flex;
329
330find_close_to_parent:
331 flexbg_free_blocks = flex_group[best_flex].free_blocks;
332 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
333 if (flex_group[best_flex].free_inodes &&
334 flex_freeb_ratio > free_block_ratio)
335 goto found_flexbg;
336
337 if (best_flex && best_flex == parent_fbg_group) {
338 best_flex--;
339 goto find_close_to_parent;
340 }
341
342 for (i = 0; i < n_fbg_groups; i++) {
343 if (i == parent_fbg_group || i == parent_fbg_group - 1)
344 continue;
345
346 flexbg_free_blocks = flex_group[i].free_blocks;
347 flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
348
349 if (flex_freeb_ratio > free_block_ratio &&
350 flex_group[i].free_inodes) {
351 best_flex = i;
352 goto found_flexbg;
353 }
354
Eric Sandeenc0010772008-08-19 22:19:50 -0400355 if (flex_group[best_flex].free_inodes == 0 ||
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400356 (flex_group[i].free_blocks >
357 flex_group[best_flex].free_blocks &&
358 flex_group[i].free_inodes))
359 best_flex = i;
360 }
361
362 if (!flex_group[best_flex].free_inodes ||
363 !flex_group[best_flex].free_blocks)
364 return -1;
365
366found_flexbg:
367 for (i = best_flex * flex_size; i < ngroups &&
368 i < (best_flex + 1) * flex_size; i++) {
369 desc = ext4_get_group_desc(sb, i, &bh);
370 if (le16_to_cpu(desc->bg_free_inodes_count)) {
371 *best_group = i;
372 goto out;
373 }
374 }
375
376 return -1;
377out:
378 return 0;
379}
380
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700381/*
382 * Orlov's allocator for directories.
383 *
384 * We always try to spread first-level directories.
385 *
386 * If there are blockgroups with both free inodes and free blocks counts
387 * not worse than average we return one with smallest directory count.
388 * Otherwise we simply return a random group.
389 *
390 * For the rest rules look so:
391 *
392 * It's OK to put directory into a group unless
393 * it has too many directories already (max_dirs) or
394 * it has too few free inodes left (min_inodes) or
395 * it has too few free blocks left (min_blocks) or
396 * it's already running too large debt (max_debt).
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000397 * Parent's group is preferred, if it doesn't satisfy these
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700398 * conditions we search cyclically through the rest. If none
399 * of the groups look good we just look for a group with more
400 * free inodes than average (starting at parent's group).
401 *
402 * Debt is incremented each time we allocate a directory and decremented
403 * when we allocate an inode, within 0--255.
404 */
405
406#define INODE_COST 64
407#define BLOCK_COST 256
408
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500409static int find_group_orlov(struct super_block *sb, struct inode *parent,
410 ext4_group_t *group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700411{
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500412 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
Mingming Cao617ba132006-10-11 01:20:53 -0700413 struct ext4_sb_info *sbi = EXT4_SB(sb);
414 struct ext4_super_block *es = sbi->s_es;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500415 ext4_group_t ngroups = sbi->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700416 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417 unsigned int freei, avefreei;
Mingming Cao617ba132006-10-11 01:20:53 -0700418 ext4_fsblk_t freeb, avefreeb;
419 ext4_fsblk_t blocks_per_dir;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700420 unsigned int ndirs;
421 int max_debt, max_dirs, min_inodes;
Mingming Cao617ba132006-10-11 01:20:53 -0700422 ext4_grpblk_t min_blocks;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500423 ext4_group_t i;
Mingming Cao617ba132006-10-11 01:20:53 -0700424 struct ext4_group_desc *desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425
426 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
427 avefreei = freei / ngroups;
428 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700429 avefreeb = freeb;
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700430 do_div(avefreeb, ngroups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700431 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
432
433 if ((parent == sb->s_root->d_inode) ||
Mingming Cao617ba132006-10-11 01:20:53 -0700434 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435 int best_ndir = inodes_per_group;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500436 ext4_group_t grp;
437 int ret = -1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700438
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500439 get_random_bytes(&grp, sizeof(grp));
440 parent_group = (unsigned)grp % ngroups;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441 for (i = 0; i < ngroups; i++) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500442 grp = (parent_group + i) % ngroups;
443 desc = ext4_get_group_desc(sb, grp, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444 if (!desc || !desc->bg_free_inodes_count)
445 continue;
446 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
447 continue;
448 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
449 continue;
450 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
451 continue;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500452 *group = grp;
453 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700454 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
455 }
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500456 if (ret == 0)
457 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458 goto fallback;
459 }
460
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700461 blocks_per_dir = ext4_blocks_count(es) - freeb;
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700462 do_div(blocks_per_dir, ndirs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700463
464 max_dirs = ndirs / ngroups + inodes_per_group / 16;
465 min_inodes = avefreei - inodes_per_group / 4;
Mingming Cao617ba132006-10-11 01:20:53 -0700466 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700467
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700468 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700469 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700470 if (max_debt * INODE_COST > inodes_per_group)
471 max_debt = inodes_per_group / INODE_COST;
472 if (max_debt > 255)
473 max_debt = 255;
474 if (max_debt == 0)
475 max_debt = 1;
476
477 for (i = 0; i < ngroups; i++) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500478 *group = (parent_group + i) % ngroups;
479 desc = ext4_get_group_desc(sb, *group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700480 if (!desc || !desc->bg_free_inodes_count)
481 continue;
482 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
483 continue;
484 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
485 continue;
486 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
487 continue;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500488 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700489 }
490
491fallback:
492 for (i = 0; i < ngroups; i++) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500493 *group = (parent_group + i) % ngroups;
494 desc = ext4_get_group_desc(sb, *group, NULL);
495 if (desc && desc->bg_free_inodes_count &&
496 le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
497 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498 }
499
500 if (avefreei) {
501 /*
502 * The free-inodes counter is approximate, and for really small
503 * filesystems the above test can fail to find any blockgroups
504 */
505 avefreei = 0;
506 goto fallback;
507 }
508
509 return -1;
510}
511
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500512static int find_group_other(struct super_block *sb, struct inode *parent,
513 ext4_group_t *group)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514{
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500515 ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
516 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700517 struct ext4_group_desc *desc;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500518 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519
520 /*
521 * Try to place the inode in its parent directory
522 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500523 *group = parent_group;
524 desc = ext4_get_group_desc(sb, *group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
526 le16_to_cpu(desc->bg_free_blocks_count))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500527 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528
529 /*
530 * We're going to place this inode in a different blockgroup from its
531 * parent. We want to cause files in a common directory to all land in
532 * the same blockgroup. But we want files which are in a different
533 * directory which shares a blockgroup with our parent to land in a
534 * different blockgroup.
535 *
536 * So add our directory's i_ino into the starting point for the hash.
537 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500538 *group = (*group + parent->i_ino) % ngroups;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539
540 /*
541 * Use a quadratic hash to find a group with a free inode and some free
542 * blocks.
543 */
544 for (i = 1; i < ngroups; i <<= 1) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500545 *group += i;
546 if (*group >= ngroups)
547 *group -= ngroups;
548 desc = ext4_get_group_desc(sb, *group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
550 le16_to_cpu(desc->bg_free_blocks_count))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500551 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552 }
553
554 /*
555 * That failed: try linear search for a free inode, even if that group
556 * has no free blocks.
557 */
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500558 *group = parent_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559 for (i = 0; i < ngroups; i++) {
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500560 if (++*group >= ngroups)
561 *group = 0;
562 desc = ext4_get_group_desc(sb, *group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500564 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 }
566
567 return -1;
568}
569
570/*
571 * There are two policies for allocating an inode. If the new inode is
572 * a directory, then a forward search is made for a block group with both
573 * free space and a low directory-to-inode ratio; if that fails, then of
574 * the groups with above-average free space, that group with the fewest
575 * directories already is chosen.
576 *
577 * For other inodes, search forward from the parent directory's block
578 * group to find a free inode.
579 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400580struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581{
582 struct super_block *sb;
583 struct buffer_head *bitmap_bh = NULL;
584 struct buffer_head *bh2;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500585 ext4_group_t group = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700586 unsigned long ino = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400587 struct inode *inode;
588 struct ext4_group_desc *gdp = NULL;
589 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -0700590 struct ext4_inode_info *ei;
591 struct ext4_sb_info *sbi;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500592 int ret2, err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593 struct inode *ret;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500594 ext4_group_t i;
595 int free = 0;
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400596 ext4_group_t flex_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597
598 /* Cannot create files in a deleted directory */
599 if (!dir || !dir->i_nlink)
600 return ERR_PTR(-EPERM);
601
602 sb = dir->i_sb;
603 inode = new_inode(sb);
604 if (!inode)
605 return ERR_PTR(-ENOMEM);
Mingming Cao617ba132006-10-11 01:20:53 -0700606 ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607
Mingming Cao617ba132006-10-11 01:20:53 -0700608 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700609 es = sbi->s_es;
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400610
611 if (sbi->s_log_groups_per_flex) {
612 ret2 = find_group_flex(sb, dir, &group);
613 goto got_group;
614 }
615
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700616 if (S_ISDIR(mode)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400617 if (test_opt(sb, OLDALLOC))
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500618 ret2 = find_group_dir(sb, dir, &group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619 else
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500620 ret2 = find_group_orlov(sb, dir, &group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621 } else
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500622 ret2 = find_group_other(sb, dir, &group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400624got_group:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 err = -ENOSPC;
Avantika Mathur2aa9fc42008-01-28 23:58:27 -0500626 if (ret2 == -1)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700627 goto out;
628
629 for (i = 0; i < sbi->s_groups_count; i++) {
630 err = -EIO;
631
Mingming Cao617ba132006-10-11 01:20:53 -0700632 gdp = ext4_get_group_desc(sb, group, &bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700633 if (!gdp)
634 goto fail;
635
636 brelse(bitmap_bh);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400637 bitmap_bh = ext4_read_inode_bitmap(sb, group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638 if (!bitmap_bh)
639 goto fail;
640
641 ino = 0;
642
643repeat_in_this_group:
Mingming Cao617ba132006-10-11 01:20:53 -0700644 ino = ext4_find_next_zero_bit((unsigned long *)
645 bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
646 if (ino < EXT4_INODES_PER_GROUP(sb)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647
648 BUFFER_TRACE(bitmap_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700649 err = ext4_journal_get_write_access(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650 if (err)
651 goto fail;
652
Mingming Cao617ba132006-10-11 01:20:53 -0700653 if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700654 ino, bitmap_bh->b_data)) {
655 /* we won it */
656 BUFFER_TRACE(bitmap_bh,
Mingming Cao617ba132006-10-11 01:20:53 -0700657 "call ext4_journal_dirty_metadata");
658 err = ext4_journal_dirty_metadata(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659 bitmap_bh);
660 if (err)
661 goto fail;
662 goto got;
663 }
664 /* we lost it */
Mingming Caodab291a2006-10-11 01:21:01 -0700665 jbd2_journal_release_buffer(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700666
Mingming Cao617ba132006-10-11 01:20:53 -0700667 if (++ino < EXT4_INODES_PER_GROUP(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668 goto repeat_in_this_group;
669 }
670
671 /*
672 * This case is possible in concurrent environment. It is very
673 * rare. We cannot repeat the find_group_xxx() call because
674 * that will simply return the same blockgroup, because the
675 * group descriptor metadata has not yet been updated.
676 * So we just go onto the next blockgroup.
677 */
678 if (++group == sbi->s_groups_count)
679 group = 0;
680 }
681 err = -ENOSPC;
682 goto out;
683
684got:
Andreas Dilger717d50e2007-10-16 18:38:25 -0400685 ino++;
686 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
687 ino > EXT4_INODES_PER_GROUP(sb)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400688 ext4_error(sb, __func__,
Andreas Dilger717d50e2007-10-16 18:38:25 -0400689 "reserved inode or inode > inodes count - "
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500690 "block_group = %lu, inode=%lu", group,
Andreas Dilger717d50e2007-10-16 18:38:25 -0400691 ino + group * EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 err = -EIO;
693 goto fail;
694 }
695
696 BUFFER_TRACE(bh2, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700697 err = ext4_journal_get_write_access(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 if (err) goto fail;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400699
700 /* We may have to initialize the block bitmap if it isn't already */
701 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
702 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
Theodore Ts'o574ca172008-07-11 19:27:31 -0400703 struct buffer_head *block_bh = ext4_read_block_bitmap(sb, group);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400704
705 BUFFER_TRACE(block_bh, "get block bitmap access");
706 err = ext4_journal_get_write_access(handle, block_bh);
707 if (err) {
708 brelse(block_bh);
709 goto fail;
710 }
711
712 free = 0;
713 spin_lock(sb_bgl_lock(sbi, group));
714 /* recheck and clear flag under lock if we still need to */
715 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
716 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
717 free = ext4_free_blocks_after_init(sb, group, gdp);
718 gdp->bg_free_blocks_count = cpu_to_le16(free);
719 }
720 spin_unlock(sb_bgl_lock(sbi, group));
721
722 /* Don't need to dirty bitmap block if we didn't change it */
723 if (free) {
724 BUFFER_TRACE(block_bh, "dirty block bitmap");
725 err = ext4_journal_dirty_metadata(handle, block_bh);
726 }
727
728 brelse(block_bh);
729 if (err)
730 goto fail;
731 }
732
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 spin_lock(sb_bgl_lock(sbi, group));
Andreas Dilger717d50e2007-10-16 18:38:25 -0400734 /* If we didn't allocate from within the initialized part of the inode
735 * table then we need to initialize up to this inode. */
736 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
737 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
738 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
739
740 /* When marking the block group with
741 * ~EXT4_BG_INODE_UNINIT we don't want to depend
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400742 * on the value of bg_itable_unused even though
Andreas Dilger717d50e2007-10-16 18:38:25 -0400743 * mke2fs could have initialized the same for us.
744 * Instead we calculated the value below
745 */
746
747 free = 0;
748 } else {
749 free = EXT4_INODES_PER_GROUP(sb) -
750 le16_to_cpu(gdp->bg_itable_unused);
751 }
752
753 /*
754 * Check the relative inode number against the last used
755 * relative inode number in this group. if it is greater
756 * we need to update the bg_itable_unused count
757 *
758 */
759 if (ino > free)
760 gdp->bg_itable_unused =
761 cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
762 }
763
Marcin Slusarze8546d02008-04-17 10:38:59 -0400764 le16_add_cpu(&gdp->bg_free_inodes_count, -1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765 if (S_ISDIR(mode)) {
Marcin Slusarze8546d02008-04-17 10:38:59 -0400766 le16_add_cpu(&gdp->bg_used_dirs_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767 }
Andreas Dilger717d50e2007-10-16 18:38:25 -0400768 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769 spin_unlock(sb_bgl_lock(sbi, group));
Mingming Cao617ba132006-10-11 01:20:53 -0700770 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
771 err = ext4_journal_dirty_metadata(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 if (err) goto fail;
773
774 percpu_counter_dec(&sbi->s_freeinodes_counter);
775 if (S_ISDIR(mode))
776 percpu_counter_inc(&sbi->s_dirs_counter);
777 sb->s_dirt = 1;
778
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400779 if (sbi->s_log_groups_per_flex) {
780 flex_group = ext4_flex_group(sbi, group);
781 spin_lock(sb_bgl_lock(sbi, flex_group));
782 sbi->s_flex_groups[flex_group].free_inodes--;
783 spin_unlock(sb_bgl_lock(sbi, flex_group));
784 }
785
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 inode->i_uid = current->fsuid;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400787 if (test_opt(sb, GRPID))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 inode->i_gid = dir->i_gid;
789 else if (dir->i_mode & S_ISGID) {
790 inode->i_gid = dir->i_gid;
791 if (S_ISDIR(mode))
792 mode |= S_ISGID;
793 } else
794 inode->i_gid = current->fsgid;
795 inode->i_mode = mode;
796
Andreas Dilger717d50e2007-10-16 18:38:25 -0400797 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798 /* This is the optimal IO size (for stat), not the fs block size */
799 inode->i_blocks = 0;
Kalpak Shahef7f3832007-07-18 09:15:20 -0400800 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
801 ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700802
803 memset(ei->i_data, 0, sizeof(ei->i_data));
804 ei->i_dir_start_lookup = 0;
805 ei->i_disksize = 0;
806
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -0500807 /*
808 * Don't inherit extent flag from directory. We set extent flag on
809 * newly created directory and file only if -o extent mount option is
810 * specified
811 */
812 ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 if (S_ISLNK(mode))
Mingming Cao617ba132006-10-11 01:20:53 -0700814 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700815 /* dirsync only applies to directories */
816 if (!S_ISDIR(mode))
Mingming Cao617ba132006-10-11 01:20:53 -0700817 ei->i_flags &= ~EXT4_DIRSYNC_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700818 ei->i_file_acl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 ei->i_dtime = 0;
820 ei->i_block_alloc_info = NULL;
821 ei->i_block_group = group;
822
Mingming Cao617ba132006-10-11 01:20:53 -0700823 ext4_set_inode_flags(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 if (IS_DIRSYNC(inode))
825 handle->h_sync = 1;
826 insert_inode_hash(inode);
827 spin_lock(&sbi->s_next_gen_lock);
828 inode->i_generation = sbi->s_next_generation++;
829 spin_unlock(&sbi->s_next_gen_lock);
830
Mingming Cao617ba132006-10-11 01:20:53 -0700831 ei->i_state = EXT4_STATE_NEW;
Kalpak Shahef7f3832007-07-18 09:15:20 -0400832
833 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700834
835 ret = inode;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400836 if (DQUOT_ALLOC_INODE(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700837 err = -EDQUOT;
838 goto fail_drop;
839 }
840
Mingming Cao617ba132006-10-11 01:20:53 -0700841 err = ext4_init_acl(handle, inode, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 if (err)
843 goto fail_free_drop;
844
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400845 err = ext4_init_security(handle, inode, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846 if (err)
847 goto fail_free_drop;
848
Alex Tomasa86c6182006-10-11 01:21:03 -0700849 if (test_opt(sb, EXTENTS)) {
Eric Sandeene4079a12008-07-11 19:27:31 -0400850 /* set extent flag only for directory, file and normal symlink*/
Aneesh Kumar K.Ve65187e2008-04-29 08:11:12 -0400851 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -0500852 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
853 ext4_ext_tree_init(handle, inode);
Aneesh Kumar K.V42bf0382008-02-25 16:38:03 -0500854 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700855 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700856
Aneesh Kumar K.V8753e882008-04-29 22:00:36 -0400857 err = ext4_mark_inode_dirty(handle, inode);
858 if (err) {
859 ext4_std_error(sb, err);
860 goto fail_free_drop;
861 }
862
Mingming Cao617ba132006-10-11 01:20:53 -0700863 ext4_debug("allocating inode %lu\n", inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864 goto really_out;
865fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700866 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867out:
868 iput(inode);
869 ret = ERR_PTR(err);
870really_out:
871 brelse(bitmap_bh);
872 return ret;
873
874fail_free_drop:
875 DQUOT_FREE_INODE(inode);
876
877fail_drop:
878 DQUOT_DROP(inode);
879 inode->i_flags |= S_NOQUOTA;
880 inode->i_nlink = 0;
881 iput(inode);
882 brelse(bitmap_bh);
883 return ERR_PTR(err);
884}
885
886/* Verify that we are loading a valid orphan from disk */
Mingming Cao617ba132006-10-11 01:20:53 -0700887struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888{
Mingming Cao617ba132006-10-11 01:20:53 -0700889 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500890 ext4_group_t block_group;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 int bit;
David Howells1d1fe1e2008-02-07 00:15:37 -0800892 struct buffer_head *bitmap_bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700893 struct inode *inode = NULL;
David Howells1d1fe1e2008-02-07 00:15:37 -0800894 long err = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895
896 /* Error cases - e2fsck has already cleaned up for us */
897 if (ino > max_ino) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400898 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700899 "bad orphan ino %lu! e2fsck was run?", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -0800900 goto error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 }
902
Mingming Cao617ba132006-10-11 01:20:53 -0700903 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
904 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400905 bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906 if (!bitmap_bh) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400907 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 "inode bitmap error for orphan %lu", ino);
David Howells1d1fe1e2008-02-07 00:15:37 -0800909 goto error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 }
911
912 /* Having the inode bit set should be a 100% indicator that this
913 * is a valid orphan (no e2fsck run on fs). Orphans also include
914 * inodes that were being truncated, so we can't check i_nlink==0.
915 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800916 if (!ext4_test_bit(bit, bitmap_bh->b_data))
917 goto bad_orphan;
918
919 inode = ext4_iget(sb, ino);
920 if (IS_ERR(inode))
921 goto iget_failed;
922
Duane Griffin91ef4ca2008-07-11 19:27:31 -0400923 /*
924 * If the orphans has i_nlinks > 0 then it should be able to be
925 * truncated, otherwise it won't be removed from the orphan list
926 * during processing and an infinite loop will result.
927 */
928 if (inode->i_nlink && !ext4_can_truncate(inode))
929 goto bad_orphan;
930
David Howells1d1fe1e2008-02-07 00:15:37 -0800931 if (NEXT_ORPHAN(inode) > max_ino)
932 goto bad_orphan;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 brelse(bitmap_bh);
934 return inode;
David Howells1d1fe1e2008-02-07 00:15:37 -0800935
936iget_failed:
937 err = PTR_ERR(inode);
938 inode = NULL;
939bad_orphan:
Harvey Harrison46e665e2008-04-17 10:38:59 -0400940 ext4_warning(sb, __func__,
David Howells1d1fe1e2008-02-07 00:15:37 -0800941 "bad orphan inode %lu! e2fsck was run?", ino);
942 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
943 bit, (unsigned long long)bitmap_bh->b_blocknr,
944 ext4_test_bit(bit, bitmap_bh->b_data));
945 printk(KERN_NOTICE "inode=%p\n", inode);
946 if (inode) {
947 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
948 is_bad_inode(inode));
949 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
950 NEXT_ORPHAN(inode));
951 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
Duane Griffin91ef4ca2008-07-11 19:27:31 -0400952 printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
David Howells1d1fe1e2008-02-07 00:15:37 -0800953 /* Avoid freeing blocks if we got a bad deleted inode */
954 if (inode->i_nlink == 0)
955 inode->i_blocks = 0;
956 iput(inode);
957 }
958 brelse(bitmap_bh);
959error:
960 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961}
962
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400963unsigned long ext4_count_free_inodes(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964{
965 unsigned long desc_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700966 struct ext4_group_desc *gdp;
Avantika Mathurfd2d4292008-01-28 23:58:27 -0500967 ext4_group_t i;
Mingming Cao617ba132006-10-11 01:20:53 -0700968#ifdef EXT4FS_DEBUG
969 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970 unsigned long bitmap_count, x;
971 struct buffer_head *bitmap_bh = NULL;
972
Mingming Cao617ba132006-10-11 01:20:53 -0700973 es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 desc_count = 0;
975 bitmap_count = 0;
976 gdp = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700977 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400978 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700979 if (!gdp)
980 continue;
981 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
982 brelse(bitmap_bh);
Eric Sandeene29d1cd2008-08-02 21:21:02 -0400983 bitmap_bh = ext4_read_inode_bitmap(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 if (!bitmap_bh)
985 continue;
986
Mingming Cao617ba132006-10-11 01:20:53 -0700987 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
Eric Sandeenc549a952008-01-28 23:58:27 -0500988 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
990 bitmap_count += x;
991 }
992 brelse(bitmap_bh);
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400993 printk(KERN_DEBUG "ext4_count_free_inodes: "
994 "stored = %u, computed = %lu, %lu\n",
995 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 return desc_count;
997#else
998 desc_count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700999 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001000 gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001 if (!gdp)
1002 continue;
1003 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
1004 cond_resched();
1005 }
1006 return desc_count;
1007#endif
1008}
1009
1010/* Called at mount-time, super-block is locked */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001011unsigned long ext4_count_dirs(struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001012{
1013 unsigned long count = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001014 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015
Mingming Cao617ba132006-10-11 01:20:53 -07001016 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001017 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018 if (!gdp)
1019 continue;
1020 count += le16_to_cpu(gdp->bg_used_dirs_count);
1021 }
1022 return count;
1023}
1024