blob: c61f37fd3f05e4d72061791d79600024c858df97 [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>
Mingming Cao617ba132006-10-11 01:20:53 -070018#include <linux/ext4_fs.h>
Mingming Caodab291a2006-10-11 01:21:01 -070019#include <linux/ext4_jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070020#include <linux/stat.h>
21#include <linux/string.h>
22#include <linux/quotaops.h>
23#include <linux/buffer_head.h>
24#include <linux/random.h>
25#include <linux/bitops.h>
Mingming Cao3a5b2ec2006-10-11 01:21:05 -070026#include <linux/blkdev.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070027#include <asm/byteorder.h>
28
29#include "xattr.h"
30#include "acl.h"
Andreas Dilger717d50e2007-10-16 18:38:25 -040031#include "group.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032
33/*
34 * ialloc.c contains the inodes allocation and deallocation routines
35 */
36
37/*
38 * The free inodes are managed by bitmaps. A file system contains several
39 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
40 * block for inodes, N blocks for the inode table and data blocks.
41 *
42 * The file system contains group descriptors which are located after the
43 * super block. Each descriptor contains the number of the bitmap block and
44 * the free blocks count in the block.
45 */
46
Andreas Dilger717d50e2007-10-16 18:38:25 -040047/*
48 * To avoid calling the atomic setbit hundreds or thousands of times, we only
49 * need to use it within a single byte (to ensure we get endianness right).
50 * We can use memset for the rest of the bitmap as there are no other users.
51 */
52void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
53{
54 int i;
55
56 if (start_bit >= end_bit)
57 return;
58
59 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
60 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
61 ext4_set_bit(i, bitmap);
62 if (i < end_bit)
63 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
64}
65
66/* Initializes an uninitialized inode bitmap */
67unsigned ext4_init_inode_bitmap(struct super_block *sb,
68 struct buffer_head *bh, int block_group,
69 struct ext4_group_desc *gdp)
70{
71 struct ext4_sb_info *sbi = EXT4_SB(sb);
72
73 J_ASSERT_BH(bh, buffer_locked(bh));
74
75 /* If checksum is bad mark all blocks and inodes use to prevent
76 * allocation, essentially implementing a per-group read-only flag. */
77 if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
78 ext4_error(sb, __FUNCTION__, "Checksum bad for group %u\n",
79 block_group);
80 gdp->bg_free_blocks_count = 0;
81 gdp->bg_free_inodes_count = 0;
82 gdp->bg_itable_unused = 0;
83 memset(bh->b_data, 0xff, sb->s_blocksize);
84 return 0;
85 }
86
87 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
88 mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
89 bh->b_data);
90
91 return EXT4_INODES_PER_GROUP(sb);
92}
Dave Kleikampac27a0e2006-10-11 01:20:50 -070093
94/*
95 * Read the inode allocation bitmap for a given block_group, reading
96 * into the specified slot in the superblock's bitmap cache.
97 *
98 * Return buffer_head of bitmap on success or NULL.
99 */
100static struct buffer_head *
101read_inode_bitmap(struct super_block * sb, unsigned long block_group)
102{
Mingming Cao617ba132006-10-11 01:20:53 -0700103 struct ext4_group_desc *desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700104 struct buffer_head *bh = NULL;
105
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)
108 goto error_out;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400109 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
110 bh = sb_getblk(sb, ext4_inode_bitmap(sb, desc));
111 if (!buffer_uptodate(bh)) {
112 lock_buffer(bh);
113 if (!buffer_uptodate(bh)) {
114 ext4_init_inode_bitmap(sb, bh, block_group,
115 desc);
116 set_buffer_uptodate(bh);
117 }
118 unlock_buffer(bh);
119 }
120 } else {
121 bh = sb_bread(sb, ext4_inode_bitmap(sb, desc));
122 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700123 if (!bh)
Mingming Cao617ba132006-10-11 01:20:53 -0700124 ext4_error(sb, "read_inode_bitmap",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700125 "Cannot read inode bitmap - "
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700126 "block_group = %lu, inode_bitmap = %llu",
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700127 block_group, ext4_inode_bitmap(sb, desc));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700128error_out:
129 return bh;
130}
131
132/*
133 * NOTE! When we get the inode, we're the only people
134 * that have access to it, and as such there are no
135 * race conditions we have to worry about. The inode
136 * is not on the hash-lists, and it cannot be reached
137 * through the filesystem because the directory entry
138 * has been deleted earlier.
139 *
140 * HOWEVER: we must make sure that we get no aliases,
141 * which means that we have to call "clear_inode()"
142 * _before_ we mark the inode not in use in the inode
143 * bitmaps. Otherwise a newly created file might use
144 * the same inode number (not actually the same pointer
145 * though), and then we'd have two inodes sharing the
146 * same inode number and space on the harddisk.
147 */
Mingming Cao617ba132006-10-11 01:20:53 -0700148void ext4_free_inode (handle_t *handle, struct inode * inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700149{
150 struct super_block * sb = inode->i_sb;
151 int is_directory;
152 unsigned long ino;
153 struct buffer_head *bitmap_bh = NULL;
154 struct buffer_head *bh2;
155 unsigned long block_group;
156 unsigned long bit;
Mingming Cao617ba132006-10-11 01:20:53 -0700157 struct ext4_group_desc * gdp;
158 struct ext4_super_block * es;
159 struct ext4_sb_info *sbi;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160 int fatal = 0, err;
161
162 if (atomic_read(&inode->i_count) > 1) {
Mingming Cao617ba132006-10-11 01:20:53 -0700163 printk ("ext4_free_inode: inode has count=%d\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164 atomic_read(&inode->i_count));
165 return;
166 }
167 if (inode->i_nlink) {
Mingming Cao617ba132006-10-11 01:20:53 -0700168 printk ("ext4_free_inode: inode has nlink=%d\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 inode->i_nlink);
170 return;
171 }
172 if (!sb) {
Mingming Cao617ba132006-10-11 01:20:53 -0700173 printk("ext4_free_inode: inode on nonexistent device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700174 return;
175 }
Mingming Cao617ba132006-10-11 01:20:53 -0700176 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177
178 ino = inode->i_ino;
Mingming Cao617ba132006-10-11 01:20:53 -0700179 ext4_debug ("freeing inode %lu\n", ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180
181 /*
182 * Note: we must free any quota before locking the superblock,
183 * as writing the quota to disk may need the lock as well.
184 */
185 DQUOT_INIT(inode);
Mingming Cao617ba132006-10-11 01:20:53 -0700186 ext4_xattr_delete_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187 DQUOT_FREE_INODE(inode);
188 DQUOT_DROP(inode);
189
190 is_directory = S_ISDIR(inode->i_mode);
191
192 /* Do this BEFORE marking the inode not in use or returning an error */
193 clear_inode (inode);
194
Mingming Cao617ba132006-10-11 01:20:53 -0700195 es = EXT4_SB(sb)->s_es;
196 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
197 ext4_error (sb, "ext4_free_inode",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198 "reserved or nonexistent inode %lu", ino);
199 goto error_return;
200 }
Mingming Cao617ba132006-10-11 01:20:53 -0700201 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
202 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203 bitmap_bh = read_inode_bitmap(sb, block_group);
204 if (!bitmap_bh)
205 goto error_return;
206
207 BUFFER_TRACE(bitmap_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700208 fatal = ext4_journal_get_write_access(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700209 if (fatal)
210 goto error_return;
211
212 /* Ok, now we can actually update the inode bitmaps.. */
Mingming Cao617ba132006-10-11 01:20:53 -0700213 if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700214 bit, bitmap_bh->b_data))
Mingming Cao617ba132006-10-11 01:20:53 -0700215 ext4_error (sb, "ext4_free_inode",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700216 "bit already cleared for inode %lu", ino);
217 else {
Mingming Cao617ba132006-10-11 01:20:53 -0700218 gdp = ext4_get_group_desc (sb, block_group, &bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219
220 BUFFER_TRACE(bh2, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700221 fatal = ext4_journal_get_write_access(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222 if (fatal) goto error_return;
223
224 if (gdp) {
225 spin_lock(sb_bgl_lock(sbi, block_group));
226 gdp->bg_free_inodes_count = cpu_to_le16(
227 le16_to_cpu(gdp->bg_free_inodes_count) + 1);
228 if (is_directory)
229 gdp->bg_used_dirs_count = cpu_to_le16(
230 le16_to_cpu(gdp->bg_used_dirs_count) - 1);
Andreas Dilger717d50e2007-10-16 18:38:25 -0400231 gdp->bg_checksum = ext4_group_desc_csum(sbi,
232 block_group, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233 spin_unlock(sb_bgl_lock(sbi, block_group));
234 percpu_counter_inc(&sbi->s_freeinodes_counter);
235 if (is_directory)
236 percpu_counter_dec(&sbi->s_dirs_counter);
237
238 }
Mingming Cao617ba132006-10-11 01:20:53 -0700239 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
240 err = ext4_journal_dirty_metadata(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241 if (!fatal) fatal = err;
242 }
Mingming Cao617ba132006-10-11 01:20:53 -0700243 BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
244 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245 if (!fatal)
246 fatal = err;
247 sb->s_dirt = 1;
248error_return:
249 brelse(bitmap_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700250 ext4_std_error(sb, fatal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251}
252
253/*
254 * There are two policies for allocating an inode. If the new inode is
255 * a directory, then a forward search is made for a block group with both
256 * free space and a low directory-to-inode ratio; if that fails, then of
257 * the groups with above-average free space, that group with the fewest
258 * directories already is chosen.
259 *
260 * For other inodes, search forward from the parent directory\'s block
261 * group to find a free inode.
262 */
263static int find_group_dir(struct super_block *sb, struct inode *parent)
264{
Mingming Cao617ba132006-10-11 01:20:53 -0700265 int ngroups = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 unsigned int freei, avefreei;
Mingming Cao617ba132006-10-11 01:20:53 -0700267 struct ext4_group_desc *desc, *best_desc = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 int group, best_group = -1;
269
Mingming Cao617ba132006-10-11 01:20:53 -0700270 freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700271 avefreei = freei / ngroups;
272
273 for (group = 0; group < ngroups; group++) {
Eric Sandeenef2fb672007-10-16 23:26:30 -0700274 desc = ext4_get_group_desc (sb, group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 if (!desc || !desc->bg_free_inodes_count)
276 continue;
277 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
278 continue;
279 if (!best_desc ||
280 (le16_to_cpu(desc->bg_free_blocks_count) >
281 le16_to_cpu(best_desc->bg_free_blocks_count))) {
282 best_group = group;
283 best_desc = desc;
284 }
285 }
286 return best_group;
287}
288
289/*
290 * Orlov's allocator for directories.
291 *
292 * We always try to spread first-level directories.
293 *
294 * If there are blockgroups with both free inodes and free blocks counts
295 * not worse than average we return one with smallest directory count.
296 * Otherwise we simply return a random group.
297 *
298 * For the rest rules look so:
299 *
300 * It's OK to put directory into a group unless
301 * it has too many directories already (max_dirs) or
302 * it has too few free inodes left (min_inodes) or
303 * it has too few free blocks left (min_blocks) or
304 * it's already running too large debt (max_debt).
305 * Parent's group is prefered, if it doesn't satisfy these
306 * conditions we search cyclically through the rest. If none
307 * of the groups look good we just look for a group with more
308 * free inodes than average (starting at parent's group).
309 *
310 * Debt is incremented each time we allocate a directory and decremented
311 * when we allocate an inode, within 0--255.
312 */
313
314#define INODE_COST 64
315#define BLOCK_COST 256
316
317static int find_group_orlov(struct super_block *sb, struct inode *parent)
318{
Mingming Cao617ba132006-10-11 01:20:53 -0700319 int parent_group = EXT4_I(parent)->i_block_group;
320 struct ext4_sb_info *sbi = EXT4_SB(sb);
321 struct ext4_super_block *es = sbi->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700322 int ngroups = sbi->s_groups_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700323 int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324 unsigned int freei, avefreei;
Mingming Cao617ba132006-10-11 01:20:53 -0700325 ext4_fsblk_t freeb, avefreeb;
326 ext4_fsblk_t blocks_per_dir;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700327 unsigned int ndirs;
328 int max_debt, max_dirs, min_inodes;
Mingming Cao617ba132006-10-11 01:20:53 -0700329 ext4_grpblk_t min_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700330 int group = -1, i;
Mingming Cao617ba132006-10-11 01:20:53 -0700331 struct ext4_group_desc *desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700332
333 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
334 avefreei = freei / ngroups;
335 freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700336 avefreeb = freeb;
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700337 do_div(avefreeb, ngroups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
339
340 if ((parent == sb->s_root->d_inode) ||
Mingming Cao617ba132006-10-11 01:20:53 -0700341 (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700342 int best_ndir = inodes_per_group;
343 int best_group = -1;
344
345 get_random_bytes(&group, sizeof(group));
346 parent_group = (unsigned)group % ngroups;
347 for (i = 0; i < ngroups; i++) {
348 group = (parent_group + i) % ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700349 desc = ext4_get_group_desc (sb, group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700350 if (!desc || !desc->bg_free_inodes_count)
351 continue;
352 if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
353 continue;
354 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
355 continue;
356 if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
357 continue;
358 best_group = group;
359 best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
360 }
361 if (best_group >= 0)
362 return best_group;
363 goto fallback;
364 }
365
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700366 blocks_per_dir = ext4_blocks_count(es) - freeb;
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700367 do_div(blocks_per_dir, ndirs);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700368
369 max_dirs = ndirs / ngroups + inodes_per_group / 16;
370 min_inodes = avefreei - inodes_per_group / 4;
Mingming Cao617ba132006-10-11 01:20:53 -0700371 min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700372
Mingming Cao3a5b2ec2006-10-11 01:21:05 -0700373 max_debt = EXT4_BLOCKS_PER_GROUP(sb);
Andrew Mortonf4e5bc22006-10-11 01:21:19 -0700374 max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375 if (max_debt * INODE_COST > inodes_per_group)
376 max_debt = inodes_per_group / INODE_COST;
377 if (max_debt > 255)
378 max_debt = 255;
379 if (max_debt == 0)
380 max_debt = 1;
381
382 for (i = 0; i < ngroups; i++) {
383 group = (parent_group + i) % ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700384 desc = ext4_get_group_desc (sb, group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700385 if (!desc || !desc->bg_free_inodes_count)
386 continue;
387 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
388 continue;
389 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
390 continue;
391 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
392 continue;
393 return group;
394 }
395
396fallback:
397 for (i = 0; i < ngroups; i++) {
398 group = (parent_group + i) % ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700399 desc = ext4_get_group_desc (sb, group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700400 if (!desc || !desc->bg_free_inodes_count)
401 continue;
402 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
403 return group;
404 }
405
406 if (avefreei) {
407 /*
408 * The free-inodes counter is approximate, and for really small
409 * filesystems the above test can fail to find any blockgroups
410 */
411 avefreei = 0;
412 goto fallback;
413 }
414
415 return -1;
416}
417
418static int find_group_other(struct super_block *sb, struct inode *parent)
419{
Mingming Cao617ba132006-10-11 01:20:53 -0700420 int parent_group = EXT4_I(parent)->i_block_group;
421 int ngroups = EXT4_SB(sb)->s_groups_count;
422 struct ext4_group_desc *desc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700423 int group, i;
424
425 /*
426 * Try to place the inode in its parent directory
427 */
428 group = parent_group;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700429 desc = ext4_get_group_desc (sb, group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700430 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
431 le16_to_cpu(desc->bg_free_blocks_count))
432 return group;
433
434 /*
435 * We're going to place this inode in a different blockgroup from its
436 * parent. We want to cause files in a common directory to all land in
437 * the same blockgroup. But we want files which are in a different
438 * directory which shares a blockgroup with our parent to land in a
439 * different blockgroup.
440 *
441 * So add our directory's i_ino into the starting point for the hash.
442 */
443 group = (group + parent->i_ino) % ngroups;
444
445 /*
446 * Use a quadratic hash to find a group with a free inode and some free
447 * blocks.
448 */
449 for (i = 1; i < ngroups; i <<= 1) {
450 group += i;
451 if (group >= ngroups)
452 group -= ngroups;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700453 desc = ext4_get_group_desc (sb, group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700454 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
455 le16_to_cpu(desc->bg_free_blocks_count))
456 return group;
457 }
458
459 /*
460 * That failed: try linear search for a free inode, even if that group
461 * has no free blocks.
462 */
463 group = parent_group;
464 for (i = 0; i < ngroups; i++) {
465 if (++group >= ngroups)
466 group = 0;
Eric Sandeenef2fb672007-10-16 23:26:30 -0700467 desc = ext4_get_group_desc (sb, group, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700468 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
469 return group;
470 }
471
472 return -1;
473}
474
475/*
476 * There are two policies for allocating an inode. If the new inode is
477 * a directory, then a forward search is made for a block group with both
478 * free space and a low directory-to-inode ratio; if that fails, then of
479 * the groups with above-average free space, that group with the fewest
480 * directories already is chosen.
481 *
482 * For other inodes, search forward from the parent directory's block
483 * group to find a free inode.
484 */
Mingming Cao617ba132006-10-11 01:20:53 -0700485struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486{
487 struct super_block *sb;
488 struct buffer_head *bitmap_bh = NULL;
489 struct buffer_head *bh2;
490 int group;
491 unsigned long ino = 0;
492 struct inode * inode;
Mingming Cao617ba132006-10-11 01:20:53 -0700493 struct ext4_group_desc * gdp = NULL;
494 struct ext4_super_block * es;
495 struct ext4_inode_info *ei;
496 struct ext4_sb_info *sbi;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700497 int err = 0;
498 struct inode *ret;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400499 int i, free = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500
501 /* Cannot create files in a deleted directory */
502 if (!dir || !dir->i_nlink)
503 return ERR_PTR(-EPERM);
504
505 sb = dir->i_sb;
506 inode = new_inode(sb);
507 if (!inode)
508 return ERR_PTR(-ENOMEM);
Mingming Cao617ba132006-10-11 01:20:53 -0700509 ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510
Mingming Cao617ba132006-10-11 01:20:53 -0700511 sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 es = sbi->s_es;
513 if (S_ISDIR(mode)) {
514 if (test_opt (sb, OLDALLOC))
515 group = find_group_dir(sb, dir);
516 else
517 group = find_group_orlov(sb, dir);
518 } else
519 group = find_group_other(sb, dir);
520
521 err = -ENOSPC;
522 if (group == -1)
523 goto out;
524
525 for (i = 0; i < sbi->s_groups_count; i++) {
526 err = -EIO;
527
Mingming Cao617ba132006-10-11 01:20:53 -0700528 gdp = ext4_get_group_desc(sb, group, &bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700529 if (!gdp)
530 goto fail;
531
532 brelse(bitmap_bh);
533 bitmap_bh = read_inode_bitmap(sb, group);
534 if (!bitmap_bh)
535 goto fail;
536
537 ino = 0;
538
539repeat_in_this_group:
Mingming Cao617ba132006-10-11 01:20:53 -0700540 ino = ext4_find_next_zero_bit((unsigned long *)
541 bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
542 if (ino < EXT4_INODES_PER_GROUP(sb)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543
544 BUFFER_TRACE(bitmap_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700545 err = ext4_journal_get_write_access(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546 if (err)
547 goto fail;
548
Mingming Cao617ba132006-10-11 01:20:53 -0700549 if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550 ino, bitmap_bh->b_data)) {
551 /* we won it */
552 BUFFER_TRACE(bitmap_bh,
Mingming Cao617ba132006-10-11 01:20:53 -0700553 "call ext4_journal_dirty_metadata");
554 err = ext4_journal_dirty_metadata(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555 bitmap_bh);
556 if (err)
557 goto fail;
558 goto got;
559 }
560 /* we lost it */
Mingming Caodab291a2006-10-11 01:21:01 -0700561 jbd2_journal_release_buffer(handle, bitmap_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562
Mingming Cao617ba132006-10-11 01:20:53 -0700563 if (++ino < EXT4_INODES_PER_GROUP(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564 goto repeat_in_this_group;
565 }
566
567 /*
568 * This case is possible in concurrent environment. It is very
569 * rare. We cannot repeat the find_group_xxx() call because
570 * that will simply return the same blockgroup, because the
571 * group descriptor metadata has not yet been updated.
572 * So we just go onto the next blockgroup.
573 */
574 if (++group == sbi->s_groups_count)
575 group = 0;
576 }
577 err = -ENOSPC;
578 goto out;
579
580got:
Andreas Dilger717d50e2007-10-16 18:38:25 -0400581 ino++;
582 if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
583 ino > EXT4_INODES_PER_GROUP(sb)) {
584 ext4_error(sb, __FUNCTION__,
585 "reserved inode or inode > inodes count - "
586 "block_group = %d, inode=%lu", group,
587 ino + group * EXT4_INODES_PER_GROUP(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 err = -EIO;
589 goto fail;
590 }
591
592 BUFFER_TRACE(bh2, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700593 err = ext4_journal_get_write_access(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700594 if (err) goto fail;
Andreas Dilger717d50e2007-10-16 18:38:25 -0400595
596 /* We may have to initialize the block bitmap if it isn't already */
597 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
598 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
599 struct buffer_head *block_bh = read_block_bitmap(sb, group);
600
601 BUFFER_TRACE(block_bh, "get block bitmap access");
602 err = ext4_journal_get_write_access(handle, block_bh);
603 if (err) {
604 brelse(block_bh);
605 goto fail;
606 }
607
608 free = 0;
609 spin_lock(sb_bgl_lock(sbi, group));
610 /* recheck and clear flag under lock if we still need to */
611 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
612 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
613 free = ext4_free_blocks_after_init(sb, group, gdp);
614 gdp->bg_free_blocks_count = cpu_to_le16(free);
615 }
616 spin_unlock(sb_bgl_lock(sbi, group));
617
618 /* Don't need to dirty bitmap block if we didn't change it */
619 if (free) {
620 BUFFER_TRACE(block_bh, "dirty block bitmap");
621 err = ext4_journal_dirty_metadata(handle, block_bh);
622 }
623
624 brelse(block_bh);
625 if (err)
626 goto fail;
627 }
628
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629 spin_lock(sb_bgl_lock(sbi, group));
Andreas Dilger717d50e2007-10-16 18:38:25 -0400630 /* If we didn't allocate from within the initialized part of the inode
631 * table then we need to initialize up to this inode. */
632 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
633 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
634 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
635
636 /* When marking the block group with
637 * ~EXT4_BG_INODE_UNINIT we don't want to depend
638 * on the value of bg_itable_unsed even though
639 * mke2fs could have initialized the same for us.
640 * Instead we calculated the value below
641 */
642
643 free = 0;
644 } else {
645 free = EXT4_INODES_PER_GROUP(sb) -
646 le16_to_cpu(gdp->bg_itable_unused);
647 }
648
649 /*
650 * Check the relative inode number against the last used
651 * relative inode number in this group. if it is greater
652 * we need to update the bg_itable_unused count
653 *
654 */
655 if (ino > free)
656 gdp->bg_itable_unused =
657 cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
658 }
659
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 gdp->bg_free_inodes_count =
661 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
662 if (S_ISDIR(mode)) {
663 gdp->bg_used_dirs_count =
664 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
665 }
Andreas Dilger717d50e2007-10-16 18:38:25 -0400666 gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667 spin_unlock(sb_bgl_lock(sbi, group));
Mingming Cao617ba132006-10-11 01:20:53 -0700668 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
669 err = ext4_journal_dirty_metadata(handle, bh2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670 if (err) goto fail;
671
672 percpu_counter_dec(&sbi->s_freeinodes_counter);
673 if (S_ISDIR(mode))
674 percpu_counter_inc(&sbi->s_dirs_counter);
675 sb->s_dirt = 1;
676
677 inode->i_uid = current->fsuid;
678 if (test_opt (sb, GRPID))
679 inode->i_gid = dir->i_gid;
680 else if (dir->i_mode & S_ISGID) {
681 inode->i_gid = dir->i_gid;
682 if (S_ISDIR(mode))
683 mode |= S_ISGID;
684 } else
685 inode->i_gid = current->fsgid;
686 inode->i_mode = mode;
687
Andreas Dilger717d50e2007-10-16 18:38:25 -0400688 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700689 /* This is the optimal IO size (for stat), not the fs block size */
690 inode->i_blocks = 0;
Kalpak Shahef7f3832007-07-18 09:15:20 -0400691 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
692 ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700693
694 memset(ei->i_data, 0, sizeof(ei->i_data));
695 ei->i_dir_start_lookup = 0;
696 ei->i_disksize = 0;
697
Mingming Cao617ba132006-10-11 01:20:53 -0700698 ei->i_flags = EXT4_I(dir)->i_flags & ~EXT4_INDEX_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699 if (S_ISLNK(mode))
Mingming Cao617ba132006-10-11 01:20:53 -0700700 ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700701 /* dirsync only applies to directories */
702 if (!S_ISDIR(mode))
Mingming Cao617ba132006-10-11 01:20:53 -0700703 ei->i_flags &= ~EXT4_DIRSYNC_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 ei->i_file_acl = 0;
705 ei->i_dir_acl = 0;
706 ei->i_dtime = 0;
707 ei->i_block_alloc_info = NULL;
708 ei->i_block_group = group;
709
Mingming Cao617ba132006-10-11 01:20:53 -0700710 ext4_set_inode_flags(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700711 if (IS_DIRSYNC(inode))
712 handle->h_sync = 1;
713 insert_inode_hash(inode);
714 spin_lock(&sbi->s_next_gen_lock);
715 inode->i_generation = sbi->s_next_generation++;
716 spin_unlock(&sbi->s_next_gen_lock);
717
Mingming Cao617ba132006-10-11 01:20:53 -0700718 ei->i_state = EXT4_STATE_NEW;
Kalpak Shahef7f3832007-07-18 09:15:20 -0400719
720 ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721
722 ret = inode;
723 if(DQUOT_ALLOC_INODE(inode)) {
724 err = -EDQUOT;
725 goto fail_drop;
726 }
727
Mingming Cao617ba132006-10-11 01:20:53 -0700728 err = ext4_init_acl(handle, inode, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729 if (err)
730 goto fail_free_drop;
731
Mingming Cao617ba132006-10-11 01:20:53 -0700732 err = ext4_init_security(handle,inode, dir);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 if (err)
734 goto fail_free_drop;
735
Mingming Cao617ba132006-10-11 01:20:53 -0700736 err = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -0700738 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 goto fail_free_drop;
740 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700741 if (test_opt(sb, EXTENTS)) {
742 EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
743 ext4_ext_tree_init(handle, inode);
744 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
745 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
746 if (err) goto fail;
747 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS);
748 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "call ext4_journal_dirty_metadata");
749 err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
750 }
751 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752
Mingming Cao617ba132006-10-11 01:20:53 -0700753 ext4_debug("allocating inode %lu\n", inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 goto really_out;
755fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700756 ext4_std_error(sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757out:
758 iput(inode);
759 ret = ERR_PTR(err);
760really_out:
761 brelse(bitmap_bh);
762 return ret;
763
764fail_free_drop:
765 DQUOT_FREE_INODE(inode);
766
767fail_drop:
768 DQUOT_DROP(inode);
769 inode->i_flags |= S_NOQUOTA;
770 inode->i_nlink = 0;
771 iput(inode);
772 brelse(bitmap_bh);
773 return ERR_PTR(err);
774}
775
776/* Verify that we are loading a valid orphan from disk */
Mingming Cao617ba132006-10-11 01:20:53 -0700777struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700778{
Mingming Cao617ba132006-10-11 01:20:53 -0700779 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780 unsigned long block_group;
781 int bit;
782 struct buffer_head *bitmap_bh = NULL;
783 struct inode *inode = NULL;
784
785 /* Error cases - e2fsck has already cleaned up for us */
786 if (ino > max_ino) {
Mingming Cao617ba132006-10-11 01:20:53 -0700787 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 "bad orphan ino %lu! e2fsck was run?", ino);
789 goto out;
790 }
791
Mingming Cao617ba132006-10-11 01:20:53 -0700792 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
793 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700794 bitmap_bh = read_inode_bitmap(sb, block_group);
795 if (!bitmap_bh) {
Mingming Cao617ba132006-10-11 01:20:53 -0700796 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700797 "inode bitmap error for orphan %lu", ino);
798 goto out;
799 }
800
801 /* Having the inode bit set should be a 100% indicator that this
802 * is a valid orphan (no e2fsck run on fs). Orphans also include
803 * inodes that were being truncated, so we can't check i_nlink==0.
804 */
Mingming Cao617ba132006-10-11 01:20:53 -0700805 if (!ext4_test_bit(bit, bitmap_bh->b_data) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 !(inode = iget(sb, ino)) || is_bad_inode(inode) ||
807 NEXT_ORPHAN(inode) > max_ino) {
Mingming Cao617ba132006-10-11 01:20:53 -0700808 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 "bad orphan inode %lu! e2fsck was run?", ino);
Mingming Cao617ba132006-10-11 01:20:53 -0700810 printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 bit, (unsigned long long)bitmap_bh->b_blocknr,
Mingming Cao617ba132006-10-11 01:20:53 -0700812 ext4_test_bit(bit, bitmap_bh->b_data));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 printk(KERN_NOTICE "inode=%p\n", inode);
814 if (inode) {
815 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
816 is_bad_inode(inode));
817 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
818 NEXT_ORPHAN(inode));
819 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
820 }
821 /* Avoid freeing blocks if we got a bad deleted inode */
822 if (inode && inode->i_nlink == 0)
823 inode->i_blocks = 0;
824 iput(inode);
825 inode = NULL;
826 }
827out:
828 brelse(bitmap_bh);
829 return inode;
830}
831
Mingming Cao617ba132006-10-11 01:20:53 -0700832unsigned long ext4_count_free_inodes (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700833{
834 unsigned long desc_count;
Mingming Cao617ba132006-10-11 01:20:53 -0700835 struct ext4_group_desc *gdp;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836 int i;
Mingming Cao617ba132006-10-11 01:20:53 -0700837#ifdef EXT4FS_DEBUG
838 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839 unsigned long bitmap_count, x;
840 struct buffer_head *bitmap_bh = NULL;
841
Mingming Cao617ba132006-10-11 01:20:53 -0700842 es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700843 desc_count = 0;
844 bitmap_count = 0;
845 gdp = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700846 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
847 gdp = ext4_get_group_desc (sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700848 if (!gdp)
849 continue;
850 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
851 brelse(bitmap_bh);
852 bitmap_bh = read_inode_bitmap(sb, i);
853 if (!bitmap_bh)
854 continue;
855
Mingming Cao617ba132006-10-11 01:20:53 -0700856 x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 printk("group %d: stored = %d, counted = %lu\n",
858 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
859 bitmap_count += x;
860 }
861 brelse(bitmap_bh);
Mingming Cao617ba132006-10-11 01:20:53 -0700862 printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
864 return desc_count;
865#else
866 desc_count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700867 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
868 gdp = ext4_get_group_desc (sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869 if (!gdp)
870 continue;
871 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
872 cond_resched();
873 }
874 return desc_count;
875#endif
876}
877
878/* Called at mount-time, super-block is locked */
Mingming Cao617ba132006-10-11 01:20:53 -0700879unsigned long ext4_count_dirs (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700880{
881 unsigned long count = 0;
882 int i;
883
Mingming Cao617ba132006-10-11 01:20:53 -0700884 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
885 struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700886 if (!gdp)
887 continue;
888 count += le16_to_cpu(gdp->bg_used_dirs_count);
889 }
890 return count;
891}
892