Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/ialloc.c |
ac27a0ec DK |
3 | * |
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> | |
dab291af | 17 | #include <linux/jbd2.h> |
ac27a0ec DK |
18 | #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> | |
3a5b2ecd | 24 | #include <linux/blkdev.h> |
ac27a0ec | 25 | #include <asm/byteorder.h> |
9bffad1e | 26 | |
3dcf5451 CH |
27 | #include "ext4.h" |
28 | #include "ext4_jbd2.h" | |
ac27a0ec DK |
29 | #include "xattr.h" |
30 | #include "acl.h" | |
31 | ||
9bffad1e TT |
32 | #include <trace/events/ext4.h> |
33 | ||
ac27a0ec DK |
34 | /* |
35 | * ialloc.c contains the inodes allocation and deallocation routines | |
36 | */ | |
37 | ||
38 | /* | |
39 | * The free inodes are managed by bitmaps. A file system contains several | |
40 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap | |
41 | * block for inodes, N blocks for the inode table and data blocks. | |
42 | * | |
43 | * The file system contains group descriptors which are located after the | |
44 | * super block. Each descriptor contains the number of the bitmap block and | |
45 | * the free blocks count in the block. | |
46 | */ | |
47 | ||
717d50e4 AD |
48 | /* |
49 | * To avoid calling the atomic setbit hundreds or thousands of times, we only | |
50 | * need to use it within a single byte (to ensure we get endianness right). | |
51 | * We can use memset for the rest of the bitmap as there are no other users. | |
52 | */ | |
61d08673 | 53 | void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap) |
717d50e4 AD |
54 | { |
55 | int i; | |
56 | ||
57 | if (start_bit >= end_bit) | |
58 | return; | |
59 | ||
60 | ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); | |
61 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) | |
62 | ext4_set_bit(i, bitmap); | |
63 | if (i < end_bit) | |
64 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); | |
65 | } | |
66 | ||
67 | /* Initializes an uninitialized inode bitmap */ | |
1f109d5a TT |
68 | static unsigned ext4_init_inode_bitmap(struct super_block *sb, |
69 | struct buffer_head *bh, | |
70 | ext4_group_t block_group, | |
71 | struct ext4_group_desc *gdp) | |
717d50e4 AD |
72 | { |
73 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
74 | ||
75 | J_ASSERT_BH(bh, buffer_locked(bh)); | |
76 | ||
77 | /* If checksum is bad mark all blocks and inodes use to prevent | |
78 | * allocation, essentially implementing a per-group read-only flag. */ | |
79 | if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { | |
12062ddd | 80 | ext4_error(sb, "Checksum bad for group %u", block_group); |
021b65bb | 81 | ext4_free_group_clusters_set(sb, gdp, 0); |
560671a0 AK |
82 | ext4_free_inodes_set(sb, gdp, 0); |
83 | ext4_itable_unused_set(sb, gdp, 0); | |
717d50e4 AD |
84 | memset(bh->b_data, 0xff, sb->s_blocksize); |
85 | return 0; | |
86 | } | |
87 | ||
88 | memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); | |
61d08673 | 89 | ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, |
717d50e4 AD |
90 | bh->b_data); |
91 | ||
92 | return EXT4_INODES_PER_GROUP(sb); | |
93 | } | |
ac27a0ec DK |
94 | |
95 | /* | |
96 | * Read the inode allocation bitmap for a given block_group, reading | |
97 | * into the specified slot in the superblock's bitmap cache. | |
98 | * | |
99 | * Return buffer_head of bitmap on success or NULL. | |
100 | */ | |
101 | static struct buffer_head * | |
e29d1cde | 102 | ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) |
ac27a0ec | 103 | { |
617ba13b | 104 | struct ext4_group_desc *desc; |
ac27a0ec | 105 | struct buffer_head *bh = NULL; |
e29d1cde | 106 | ext4_fsblk_t bitmap_blk; |
ac27a0ec | 107 | |
617ba13b | 108 | desc = ext4_get_group_desc(sb, block_group, NULL); |
ac27a0ec | 109 | if (!desc) |
e29d1cde | 110 | return NULL; |
bfff6873 | 111 | |
e29d1cde ES |
112 | bitmap_blk = ext4_inode_bitmap(sb, desc); |
113 | bh = sb_getblk(sb, bitmap_blk); | |
114 | if (unlikely(!bh)) { | |
12062ddd | 115 | ext4_error(sb, "Cannot read inode bitmap - " |
a9df9a49 | 116 | "block_group = %u, inode_bitmap = %llu", |
e29d1cde ES |
117 | block_group, bitmap_blk); |
118 | return NULL; | |
119 | } | |
2ccb5fb9 | 120 | if (bitmap_uptodate(bh)) |
e29d1cde ES |
121 | return bh; |
122 | ||
c806e68f | 123 | lock_buffer(bh); |
2ccb5fb9 AK |
124 | if (bitmap_uptodate(bh)) { |
125 | unlock_buffer(bh); | |
126 | return bh; | |
127 | } | |
bfff6873 | 128 | |
955ce5f5 | 129 | ext4_lock_group(sb, block_group); |
717d50e4 | 130 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
e29d1cde | 131 | ext4_init_inode_bitmap(sb, bh, block_group, desc); |
2ccb5fb9 | 132 | set_bitmap_uptodate(bh); |
e29d1cde | 133 | set_buffer_uptodate(bh); |
955ce5f5 | 134 | ext4_unlock_group(sb, block_group); |
3300beda | 135 | unlock_buffer(bh); |
e29d1cde | 136 | return bh; |
717d50e4 | 137 | } |
955ce5f5 | 138 | ext4_unlock_group(sb, block_group); |
bfff6873 | 139 | |
2ccb5fb9 AK |
140 | if (buffer_uptodate(bh)) { |
141 | /* | |
142 | * if not uninit if bh is uptodate, | |
143 | * bitmap is also uptodate | |
144 | */ | |
145 | set_bitmap_uptodate(bh); | |
146 | unlock_buffer(bh); | |
147 | return bh; | |
148 | } | |
149 | /* | |
150 | * submit the buffer_head for read. We can | |
151 | * safely mark the bitmap as uptodate now. | |
152 | * We do it here so the bitmap uptodate bit | |
153 | * get set with buffer lock held. | |
154 | */ | |
0562e0ba | 155 | trace_ext4_load_inode_bitmap(sb, block_group); |
2ccb5fb9 | 156 | set_bitmap_uptodate(bh); |
e29d1cde ES |
157 | if (bh_submit_read(bh) < 0) { |
158 | put_bh(bh); | |
12062ddd | 159 | ext4_error(sb, "Cannot read inode bitmap - " |
a9df9a49 | 160 | "block_group = %u, inode_bitmap = %llu", |
e29d1cde ES |
161 | block_group, bitmap_blk); |
162 | return NULL; | |
163 | } | |
ac27a0ec DK |
164 | return bh; |
165 | } | |
166 | ||
167 | /* | |
168 | * NOTE! When we get the inode, we're the only people | |
169 | * that have access to it, and as such there are no | |
170 | * race conditions we have to worry about. The inode | |
171 | * is not on the hash-lists, and it cannot be reached | |
172 | * through the filesystem because the directory entry | |
173 | * has been deleted earlier. | |
174 | * | |
175 | * HOWEVER: we must make sure that we get no aliases, | |
176 | * which means that we have to call "clear_inode()" | |
177 | * _before_ we mark the inode not in use in the inode | |
178 | * bitmaps. Otherwise a newly created file might use | |
179 | * the same inode number (not actually the same pointer | |
180 | * though), and then we'd have two inodes sharing the | |
181 | * same inode number and space on the harddisk. | |
182 | */ | |
af5bc92d | 183 | void ext4_free_inode(handle_t *handle, struct inode *inode) |
ac27a0ec | 184 | { |
af5bc92d | 185 | struct super_block *sb = inode->i_sb; |
ac27a0ec DK |
186 | int is_directory; |
187 | unsigned long ino; | |
188 | struct buffer_head *bitmap_bh = NULL; | |
189 | struct buffer_head *bh2; | |
fd2d4291 | 190 | ext4_group_t block_group; |
ac27a0ec | 191 | unsigned long bit; |
af5bc92d TT |
192 | struct ext4_group_desc *gdp; |
193 | struct ext4_super_block *es; | |
617ba13b | 194 | struct ext4_sb_info *sbi; |
7ce9d5d1 | 195 | int fatal = 0, err, count, cleared; |
ac27a0ec DK |
196 | |
197 | if (atomic_read(&inode->i_count) > 1) { | |
4776004f TT |
198 | printk(KERN_ERR "ext4_free_inode: inode has count=%d\n", |
199 | atomic_read(&inode->i_count)); | |
ac27a0ec DK |
200 | return; |
201 | } | |
202 | if (inode->i_nlink) { | |
4776004f TT |
203 | printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n", |
204 | inode->i_nlink); | |
ac27a0ec DK |
205 | return; |
206 | } | |
207 | if (!sb) { | |
4776004f TT |
208 | printk(KERN_ERR "ext4_free_inode: inode on " |
209 | "nonexistent device\n"); | |
ac27a0ec DK |
210 | return; |
211 | } | |
617ba13b | 212 | sbi = EXT4_SB(sb); |
ac27a0ec DK |
213 | |
214 | ino = inode->i_ino; | |
af5bc92d | 215 | ext4_debug("freeing inode %lu\n", ino); |
9bffad1e | 216 | trace_ext4_free_inode(inode); |
ac27a0ec DK |
217 | |
218 | /* | |
219 | * Note: we must free any quota before locking the superblock, | |
220 | * as writing the quota to disk may need the lock as well. | |
221 | */ | |
871a2931 | 222 | dquot_initialize(inode); |
617ba13b | 223 | ext4_xattr_delete_inode(handle, inode); |
63936dda | 224 | dquot_free_inode(inode); |
9f754758 | 225 | dquot_drop(inode); |
ac27a0ec DK |
226 | |
227 | is_directory = S_ISDIR(inode->i_mode); | |
228 | ||
229 | /* Do this BEFORE marking the inode not in use or returning an error */ | |
0930fcc1 | 230 | ext4_clear_inode(inode); |
ac27a0ec | 231 | |
617ba13b MC |
232 | es = EXT4_SB(sb)->s_es; |
233 | if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { | |
12062ddd | 234 | ext4_error(sb, "reserved or nonexistent inode %lu", ino); |
ac27a0ec DK |
235 | goto error_return; |
236 | } | |
617ba13b MC |
237 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
238 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); | |
e29d1cde | 239 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
ac27a0ec DK |
240 | if (!bitmap_bh) |
241 | goto error_return; | |
242 | ||
243 | BUFFER_TRACE(bitmap_bh, "get_write_access"); | |
617ba13b | 244 | fatal = ext4_journal_get_write_access(handle, bitmap_bh); |
ac27a0ec DK |
245 | if (fatal) |
246 | goto error_return; | |
247 | ||
d17413c0 DM |
248 | fatal = -ESRCH; |
249 | gdp = ext4_get_group_desc(sb, block_group, &bh2); | |
250 | if (gdp) { | |
ac27a0ec | 251 | BUFFER_TRACE(bh2, "get_write_access"); |
617ba13b | 252 | fatal = ext4_journal_get_write_access(handle, bh2); |
d17413c0 DM |
253 | } |
254 | ext4_lock_group(sb, block_group); | |
597d508c | 255 | cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data); |
d17413c0 DM |
256 | if (fatal || !cleared) { |
257 | ext4_unlock_group(sb, block_group); | |
258 | goto out; | |
259 | } | |
7d39db14 | 260 | |
d17413c0 DM |
261 | count = ext4_free_inodes_count(sb, gdp) + 1; |
262 | ext4_free_inodes_set(sb, gdp, count); | |
263 | if (is_directory) { | |
264 | count = ext4_used_dirs_count(sb, gdp) - 1; | |
265 | ext4_used_dirs_set(sb, gdp, count); | |
266 | percpu_counter_dec(&sbi->s_dirs_counter); | |
ac27a0ec | 267 | } |
d17413c0 DM |
268 | gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp); |
269 | ext4_unlock_group(sb, block_group); | |
ac27a0ec | 270 | |
d17413c0 DM |
271 | percpu_counter_inc(&sbi->s_freeinodes_counter); |
272 | if (sbi->s_log_groups_per_flex) { | |
273 | ext4_group_t f = ext4_flex_group(sbi, block_group); | |
9f24e420 | 274 | |
d17413c0 DM |
275 | atomic_inc(&sbi->s_flex_groups[f].free_inodes); |
276 | if (is_directory) | |
277 | atomic_dec(&sbi->s_flex_groups[f].used_dirs); | |
ac27a0ec | 278 | } |
d17413c0 DM |
279 | BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); |
280 | fatal = ext4_handle_dirty_metadata(handle, NULL, bh2); | |
281 | out: | |
282 | if (cleared) { | |
283 | BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); | |
284 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); | |
285 | if (!fatal) | |
286 | fatal = err; | |
a0375156 | 287 | ext4_mark_super_dirty(sb); |
d17413c0 DM |
288 | } else |
289 | ext4_error(sb, "bit already cleared for inode %lu", ino); | |
290 | ||
ac27a0ec DK |
291 | error_return: |
292 | brelse(bitmap_bh); | |
617ba13b | 293 | ext4_std_error(sb, fatal); |
ac27a0ec DK |
294 | } |
295 | ||
a4912123 TT |
296 | struct orlov_stats { |
297 | __u32 free_inodes; | |
24aaa8ef | 298 | __u32 free_clusters; |
a4912123 TT |
299 | __u32 used_dirs; |
300 | }; | |
301 | ||
302 | /* | |
303 | * Helper function for Orlov's allocator; returns critical information | |
304 | * for a particular block group or flex_bg. If flex_size is 1, then g | |
305 | * is a block group number; otherwise it is flex_bg number. | |
306 | */ | |
1f109d5a TT |
307 | static void get_orlov_stats(struct super_block *sb, ext4_group_t g, |
308 | int flex_size, struct orlov_stats *stats) | |
a4912123 TT |
309 | { |
310 | struct ext4_group_desc *desc; | |
7d39db14 | 311 | struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups; |
a4912123 | 312 | |
7d39db14 TT |
313 | if (flex_size > 1) { |
314 | stats->free_inodes = atomic_read(&flex_group[g].free_inodes); | |
24aaa8ef | 315 | stats->free_clusters = atomic_read(&flex_group[g].free_clusters); |
7d39db14 TT |
316 | stats->used_dirs = atomic_read(&flex_group[g].used_dirs); |
317 | return; | |
318 | } | |
a4912123 | 319 | |
7d39db14 TT |
320 | desc = ext4_get_group_desc(sb, g, NULL); |
321 | if (desc) { | |
322 | stats->free_inodes = ext4_free_inodes_count(sb, desc); | |
021b65bb | 323 | stats->free_clusters = ext4_free_group_clusters(sb, desc); |
7d39db14 TT |
324 | stats->used_dirs = ext4_used_dirs_count(sb, desc); |
325 | } else { | |
326 | stats->free_inodes = 0; | |
24aaa8ef | 327 | stats->free_clusters = 0; |
7d39db14 | 328 | stats->used_dirs = 0; |
a4912123 TT |
329 | } |
330 | } | |
331 | ||
ac27a0ec DK |
332 | /* |
333 | * Orlov's allocator for directories. | |
334 | * | |
335 | * We always try to spread first-level directories. | |
336 | * | |
337 | * If there are blockgroups with both free inodes and free blocks counts | |
338 | * not worse than average we return one with smallest directory count. | |
339 | * Otherwise we simply return a random group. | |
340 | * | |
341 | * For the rest rules look so: | |
342 | * | |
343 | * It's OK to put directory into a group unless | |
344 | * it has too many directories already (max_dirs) or | |
345 | * it has too few free inodes left (min_inodes) or | |
346 | * it has too few free blocks left (min_blocks) or | |
1cc8dcf5 | 347 | * Parent's group is preferred, if it doesn't satisfy these |
ac27a0ec DK |
348 | * conditions we search cyclically through the rest. If none |
349 | * of the groups look good we just look for a group with more | |
350 | * free inodes than average (starting at parent's group). | |
ac27a0ec DK |
351 | */ |
352 | ||
2aa9fc4c | 353 | static int find_group_orlov(struct super_block *sb, struct inode *parent, |
dcca3fec | 354 | ext4_group_t *group, umode_t mode, |
f157a4aa | 355 | const struct qstr *qstr) |
ac27a0ec | 356 | { |
fd2d4291 | 357 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
617ba13b | 358 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
8df9675f | 359 | ext4_group_t real_ngroups = ext4_get_groups_count(sb); |
617ba13b | 360 | int inodes_per_group = EXT4_INODES_PER_GROUP(sb); |
14c83c9f | 361 | unsigned int freei, avefreei, grp_free; |
24aaa8ef | 362 | ext4_fsblk_t freeb, avefreec; |
ac27a0ec | 363 | unsigned int ndirs; |
a4912123 | 364 | int max_dirs, min_inodes; |
24aaa8ef | 365 | ext4_grpblk_t min_clusters; |
8df9675f | 366 | ext4_group_t i, grp, g, ngroups; |
617ba13b | 367 | struct ext4_group_desc *desc; |
a4912123 TT |
368 | struct orlov_stats stats; |
369 | int flex_size = ext4_flex_bg_size(sbi); | |
f157a4aa | 370 | struct dx_hash_info hinfo; |
a4912123 | 371 | |
8df9675f | 372 | ngroups = real_ngroups; |
a4912123 | 373 | if (flex_size > 1) { |
8df9675f | 374 | ngroups = (real_ngroups + flex_size - 1) >> |
a4912123 TT |
375 | sbi->s_log_groups_per_flex; |
376 | parent_group >>= sbi->s_log_groups_per_flex; | |
377 | } | |
ac27a0ec DK |
378 | |
379 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); | |
380 | avefreei = freei / ngroups; | |
57042651 TT |
381 | freeb = EXT4_C2B(sbi, |
382 | percpu_counter_read_positive(&sbi->s_freeclusters_counter)); | |
24aaa8ef TT |
383 | avefreec = freeb; |
384 | do_div(avefreec, ngroups); | |
ac27a0ec DK |
385 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
386 | ||
a4912123 TT |
387 | if (S_ISDIR(mode) && |
388 | ((parent == sb->s_root->d_inode) || | |
12e9b892 | 389 | (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) { |
ac27a0ec | 390 | int best_ndir = inodes_per_group; |
2aa9fc4c | 391 | int ret = -1; |
ac27a0ec | 392 | |
f157a4aa TT |
393 | if (qstr) { |
394 | hinfo.hash_version = DX_HASH_HALF_MD4; | |
395 | hinfo.seed = sbi->s_hash_seed; | |
396 | ext4fs_dirhash(qstr->name, qstr->len, &hinfo); | |
397 | grp = hinfo.hash; | |
398 | } else | |
399 | get_random_bytes(&grp, sizeof(grp)); | |
2aa9fc4c | 400 | parent_group = (unsigned)grp % ngroups; |
ac27a0ec | 401 | for (i = 0; i < ngroups; i++) { |
a4912123 TT |
402 | g = (parent_group + i) % ngroups; |
403 | get_orlov_stats(sb, g, flex_size, &stats); | |
404 | if (!stats.free_inodes) | |
ac27a0ec | 405 | continue; |
a4912123 | 406 | if (stats.used_dirs >= best_ndir) |
ac27a0ec | 407 | continue; |
a4912123 | 408 | if (stats.free_inodes < avefreei) |
ac27a0ec | 409 | continue; |
24aaa8ef | 410 | if (stats.free_clusters < avefreec) |
ac27a0ec | 411 | continue; |
a4912123 | 412 | grp = g; |
2aa9fc4c | 413 | ret = 0; |
a4912123 TT |
414 | best_ndir = stats.used_dirs; |
415 | } | |
416 | if (ret) | |
417 | goto fallback; | |
418 | found_flex_bg: | |
419 | if (flex_size == 1) { | |
420 | *group = grp; | |
421 | return 0; | |
422 | } | |
423 | ||
424 | /* | |
425 | * We pack inodes at the beginning of the flexgroup's | |
426 | * inode tables. Block allocation decisions will do | |
427 | * something similar, although regular files will | |
428 | * start at 2nd block group of the flexgroup. See | |
429 | * ext4_ext_find_goal() and ext4_find_near(). | |
430 | */ | |
431 | grp *= flex_size; | |
432 | for (i = 0; i < flex_size; i++) { | |
8df9675f | 433 | if (grp+i >= real_ngroups) |
a4912123 TT |
434 | break; |
435 | desc = ext4_get_group_desc(sb, grp+i, NULL); | |
436 | if (desc && ext4_free_inodes_count(sb, desc)) { | |
437 | *group = grp+i; | |
438 | return 0; | |
439 | } | |
ac27a0ec | 440 | } |
ac27a0ec DK |
441 | goto fallback; |
442 | } | |
443 | ||
ac27a0ec | 444 | max_dirs = ndirs / ngroups + inodes_per_group / 16; |
a4912123 TT |
445 | min_inodes = avefreei - inodes_per_group*flex_size / 4; |
446 | if (min_inodes < 1) | |
447 | min_inodes = 1; | |
24aaa8ef | 448 | min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4; |
a4912123 TT |
449 | |
450 | /* | |
451 | * Start looking in the flex group where we last allocated an | |
452 | * inode for this parent directory | |
453 | */ | |
454 | if (EXT4_I(parent)->i_last_alloc_group != ~0) { | |
455 | parent_group = EXT4_I(parent)->i_last_alloc_group; | |
456 | if (flex_size > 1) | |
457 | parent_group >>= sbi->s_log_groups_per_flex; | |
458 | } | |
ac27a0ec DK |
459 | |
460 | for (i = 0; i < ngroups; i++) { | |
a4912123 TT |
461 | grp = (parent_group + i) % ngroups; |
462 | get_orlov_stats(sb, grp, flex_size, &stats); | |
463 | if (stats.used_dirs >= max_dirs) | |
ac27a0ec | 464 | continue; |
a4912123 | 465 | if (stats.free_inodes < min_inodes) |
ac27a0ec | 466 | continue; |
24aaa8ef | 467 | if (stats.free_clusters < min_clusters) |
ac27a0ec | 468 | continue; |
a4912123 | 469 | goto found_flex_bg; |
ac27a0ec DK |
470 | } |
471 | ||
472 | fallback: | |
8df9675f | 473 | ngroups = real_ngroups; |
a4912123 | 474 | avefreei = freei / ngroups; |
b5451f7b | 475 | fallback_retry: |
a4912123 | 476 | parent_group = EXT4_I(parent)->i_block_group; |
ac27a0ec | 477 | for (i = 0; i < ngroups; i++) { |
a4912123 TT |
478 | grp = (parent_group + i) % ngroups; |
479 | desc = ext4_get_group_desc(sb, grp, NULL); | |
14c83c9f TT |
480 | grp_free = ext4_free_inodes_count(sb, desc); |
481 | if (desc && grp_free && grp_free >= avefreei) { | |
a4912123 | 482 | *group = grp; |
2aa9fc4c | 483 | return 0; |
a4912123 | 484 | } |
ac27a0ec DK |
485 | } |
486 | ||
487 | if (avefreei) { | |
488 | /* | |
489 | * The free-inodes counter is approximate, and for really small | |
490 | * filesystems the above test can fail to find any blockgroups | |
491 | */ | |
492 | avefreei = 0; | |
b5451f7b | 493 | goto fallback_retry; |
ac27a0ec DK |
494 | } |
495 | ||
496 | return -1; | |
497 | } | |
498 | ||
2aa9fc4c | 499 | static int find_group_other(struct super_block *sb, struct inode *parent, |
dcca3fec | 500 | ext4_group_t *group, umode_t mode) |
ac27a0ec | 501 | { |
fd2d4291 | 502 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
8df9675f | 503 | ext4_group_t i, last, ngroups = ext4_get_groups_count(sb); |
617ba13b | 504 | struct ext4_group_desc *desc; |
a4912123 TT |
505 | int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); |
506 | ||
507 | /* | |
508 | * Try to place the inode is the same flex group as its | |
509 | * parent. If we can't find space, use the Orlov algorithm to | |
510 | * find another flex group, and store that information in the | |
511 | * parent directory's inode information so that use that flex | |
512 | * group for future allocations. | |
513 | */ | |
514 | if (flex_size > 1) { | |
515 | int retry = 0; | |
516 | ||
517 | try_again: | |
518 | parent_group &= ~(flex_size-1); | |
519 | last = parent_group + flex_size; | |
520 | if (last > ngroups) | |
521 | last = ngroups; | |
522 | for (i = parent_group; i < last; i++) { | |
523 | desc = ext4_get_group_desc(sb, i, NULL); | |
524 | if (desc && ext4_free_inodes_count(sb, desc)) { | |
525 | *group = i; | |
526 | return 0; | |
527 | } | |
528 | } | |
529 | if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { | |
530 | retry = 1; | |
531 | parent_group = EXT4_I(parent)->i_last_alloc_group; | |
532 | goto try_again; | |
533 | } | |
534 | /* | |
535 | * If this didn't work, use the Orlov search algorithm | |
536 | * to find a new flex group; we pass in the mode to | |
537 | * avoid the topdir algorithms. | |
538 | */ | |
539 | *group = parent_group + flex_size; | |
540 | if (*group > ngroups) | |
541 | *group = 0; | |
7dc57615 | 542 | return find_group_orlov(sb, parent, group, mode, NULL); |
a4912123 | 543 | } |
ac27a0ec DK |
544 | |
545 | /* | |
546 | * Try to place the inode in its parent directory | |
547 | */ | |
2aa9fc4c AM |
548 | *group = parent_group; |
549 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 | 550 | if (desc && ext4_free_inodes_count(sb, desc) && |
021b65bb | 551 | ext4_free_group_clusters(sb, desc)) |
2aa9fc4c | 552 | return 0; |
ac27a0ec DK |
553 | |
554 | /* | |
555 | * We're going to place this inode in a different blockgroup from its | |
556 | * parent. We want to cause files in a common directory to all land in | |
557 | * the same blockgroup. But we want files which are in a different | |
558 | * directory which shares a blockgroup with our parent to land in a | |
559 | * different blockgroup. | |
560 | * | |
561 | * So add our directory's i_ino into the starting point for the hash. | |
562 | */ | |
2aa9fc4c | 563 | *group = (*group + parent->i_ino) % ngroups; |
ac27a0ec DK |
564 | |
565 | /* | |
566 | * Use a quadratic hash to find a group with a free inode and some free | |
567 | * blocks. | |
568 | */ | |
569 | for (i = 1; i < ngroups; i <<= 1) { | |
2aa9fc4c AM |
570 | *group += i; |
571 | if (*group >= ngroups) | |
572 | *group -= ngroups; | |
573 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 | 574 | if (desc && ext4_free_inodes_count(sb, desc) && |
021b65bb | 575 | ext4_free_group_clusters(sb, desc)) |
2aa9fc4c | 576 | return 0; |
ac27a0ec DK |
577 | } |
578 | ||
579 | /* | |
580 | * That failed: try linear search for a free inode, even if that group | |
581 | * has no free blocks. | |
582 | */ | |
2aa9fc4c | 583 | *group = parent_group; |
ac27a0ec | 584 | for (i = 0; i < ngroups; i++) { |
2aa9fc4c AM |
585 | if (++*group >= ngroups) |
586 | *group = 0; | |
587 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 | 588 | if (desc && ext4_free_inodes_count(sb, desc)) |
2aa9fc4c | 589 | return 0; |
ac27a0ec DK |
590 | } |
591 | ||
592 | return -1; | |
593 | } | |
594 | ||
595 | /* | |
596 | * There are two policies for allocating an inode. If the new inode is | |
597 | * a directory, then a forward search is made for a block group with both | |
598 | * free space and a low directory-to-inode ratio; if that fails, then of | |
599 | * the groups with above-average free space, that group with the fewest | |
600 | * directories already is chosen. | |
601 | * | |
602 | * For other inodes, search forward from the parent directory's block | |
603 | * group to find a free inode. | |
604 | */ | |
dcca3fec | 605 | struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, umode_t mode, |
5cb81dab | 606 | const struct qstr *qstr, __u32 goal, uid_t *owner) |
ac27a0ec DK |
607 | { |
608 | struct super_block *sb; | |
3300beda AK |
609 | struct buffer_head *inode_bitmap_bh = NULL; |
610 | struct buffer_head *group_desc_bh; | |
8df9675f | 611 | ext4_group_t ngroups, group = 0; |
ac27a0ec | 612 | unsigned long ino = 0; |
af5bc92d TT |
613 | struct inode *inode; |
614 | struct ext4_group_desc *gdp = NULL; | |
617ba13b MC |
615 | struct ext4_inode_info *ei; |
616 | struct ext4_sb_info *sbi; | |
39341867 | 617 | int ret2, err = 0; |
ac27a0ec | 618 | struct inode *ret; |
2aa9fc4c | 619 | ext4_group_t i; |
772cb7c8 | 620 | ext4_group_t flex_group; |
ac27a0ec DK |
621 | |
622 | /* Cannot create files in a deleted directory */ | |
623 | if (!dir || !dir->i_nlink) | |
624 | return ERR_PTR(-EPERM); | |
625 | ||
626 | sb = dir->i_sb; | |
8df9675f | 627 | ngroups = ext4_get_groups_count(sb); |
9bffad1e | 628 | trace_ext4_request_inode(dir, mode); |
ac27a0ec DK |
629 | inode = new_inode(sb); |
630 | if (!inode) | |
631 | return ERR_PTR(-ENOMEM); | |
617ba13b | 632 | ei = EXT4_I(inode); |
617ba13b | 633 | sbi = EXT4_SB(sb); |
772cb7c8 | 634 | |
11013911 AD |
635 | if (!goal) |
636 | goal = sbi->s_inode_goal; | |
637 | ||
e6462869 | 638 | if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) { |
11013911 AD |
639 | group = (goal - 1) / EXT4_INODES_PER_GROUP(sb); |
640 | ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb); | |
641 | ret2 = 0; | |
642 | goto got_group; | |
643 | } | |
644 | ||
4113c4ca LC |
645 | if (S_ISDIR(mode)) |
646 | ret2 = find_group_orlov(sb, dir, &group, mode, qstr); | |
647 | else | |
a4912123 | 648 | ret2 = find_group_other(sb, dir, &group, mode); |
ac27a0ec | 649 | |
772cb7c8 | 650 | got_group: |
a4912123 | 651 | EXT4_I(dir)->i_last_alloc_group = group; |
ac27a0ec | 652 | err = -ENOSPC; |
2aa9fc4c | 653 | if (ret2 == -1) |
ac27a0ec DK |
654 | goto out; |
655 | ||
119c0d44 TT |
656 | /* |
657 | * Normally we will only go through one pass of this loop, | |
658 | * unless we get unlucky and it turns out the group we selected | |
659 | * had its last inode grabbed by someone else. | |
660 | */ | |
11013911 | 661 | for (i = 0; i < ngroups; i++, ino = 0) { |
ac27a0ec DK |
662 | err = -EIO; |
663 | ||
3300beda | 664 | gdp = ext4_get_group_desc(sb, group, &group_desc_bh); |
ac27a0ec DK |
665 | if (!gdp) |
666 | goto fail; | |
667 | ||
3300beda AK |
668 | brelse(inode_bitmap_bh); |
669 | inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); | |
670 | if (!inode_bitmap_bh) | |
ac27a0ec DK |
671 | goto fail; |
672 | ||
ac27a0ec | 673 | repeat_in_this_group: |
617ba13b | 674 | ino = ext4_find_next_zero_bit((unsigned long *) |
3300beda AK |
675 | inode_bitmap_bh->b_data, |
676 | EXT4_INODES_PER_GROUP(sb), ino); | |
119c0d44 TT |
677 | if (ino >= EXT4_INODES_PER_GROUP(sb)) { |
678 | if (++group == ngroups) | |
679 | group = 0; | |
680 | continue; | |
ac27a0ec | 681 | } |
119c0d44 TT |
682 | if (group == 0 && (ino+1) < EXT4_FIRST_INO(sb)) { |
683 | ext4_error(sb, "reserved inode found cleared - " | |
684 | "inode=%lu", ino + 1); | |
685 | continue; | |
686 | } | |
687 | ext4_lock_group(sb, group); | |
688 | ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data); | |
689 | ext4_unlock_group(sb, group); | |
690 | ino++; /* the inode bitmap is zero-based */ | |
691 | if (!ret2) | |
692 | goto got; /* we grabbed the inode! */ | |
693 | if (ino < EXT4_INODES_PER_GROUP(sb)) | |
694 | goto repeat_in_this_group; | |
ac27a0ec DK |
695 | } |
696 | err = -ENOSPC; | |
697 | goto out; | |
698 | ||
699 | got: | |
717d50e4 AD |
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)) { | |
3300beda | 703 | struct buffer_head *block_bitmap_bh; |
717d50e4 | 704 | |
3300beda AK |
705 | block_bitmap_bh = ext4_read_block_bitmap(sb, group); |
706 | BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); | |
707 | err = ext4_journal_get_write_access(handle, block_bitmap_bh); | |
717d50e4 | 708 | if (err) { |
3300beda | 709 | brelse(block_bitmap_bh); |
717d50e4 AD |
710 | goto fail; |
711 | } | |
712 | ||
fd034a84 TT |
713 | BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); |
714 | err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh); | |
715 | brelse(block_bitmap_bh); | |
716 | ||
717d50e4 | 717 | /* recheck and clear flag under lock if we still need to */ |
fd034a84 | 718 | ext4_lock_group(sb, group); |
717d50e4 | 719 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
3300beda | 720 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
021b65bb | 721 | ext4_free_group_clusters_set(sb, gdp, |
cff1dfd7 | 722 | ext4_free_clusters_after_init(sb, group, gdp)); |
23712a9c FB |
723 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, |
724 | gdp); | |
717d50e4 | 725 | } |
955ce5f5 | 726 | ext4_unlock_group(sb, group); |
717d50e4 | 727 | |
717d50e4 AD |
728 | if (err) |
729 | goto fail; | |
730 | } | |
119c0d44 TT |
731 | |
732 | BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); | |
733 | err = ext4_journal_get_write_access(handle, inode_bitmap_bh); | |
734 | if (err) | |
735 | goto fail; | |
736 | ||
737 | BUFFER_TRACE(group_desc_bh, "get_write_access"); | |
738 | err = ext4_journal_get_write_access(handle, group_desc_bh); | |
739 | if (err) | |
740 | goto fail; | |
741 | ||
742 | /* Update the relevant bg descriptor fields */ | |
743 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { | |
744 | int free; | |
745 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); | |
746 | ||
747 | down_read(&grp->alloc_sem); /* protect vs itable lazyinit */ | |
748 | ext4_lock_group(sb, group); /* while we modify the bg desc */ | |
749 | free = EXT4_INODES_PER_GROUP(sb) - | |
750 | ext4_itable_unused_count(sb, gdp); | |
751 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { | |
752 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); | |
753 | free = 0; | |
754 | } | |
755 | /* | |
756 | * Check the relative inode number against the last used | |
757 | * relative inode number in this group. if it is greater | |
758 | * we need to update the bg_itable_unused count | |
759 | */ | |
760 | if (ino > free) | |
761 | ext4_itable_unused_set(sb, gdp, | |
762 | (EXT4_INODES_PER_GROUP(sb) - ino)); | |
763 | up_read(&grp->alloc_sem); | |
764 | } | |
765 | ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1); | |
766 | if (S_ISDIR(mode)) { | |
767 | ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1); | |
768 | if (sbi->s_log_groups_per_flex) { | |
769 | ext4_group_t f = ext4_flex_group(sbi, group); | |
770 | ||
771 | atomic_inc(&sbi->s_flex_groups[f].used_dirs); | |
772 | } | |
773 | } | |
774 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { | |
775 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); | |
776 | ext4_unlock_group(sb, group); | |
777 | } | |
778 | ||
779 | BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata"); | |
780 | err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh); | |
781 | if (err) | |
782 | goto fail; | |
783 | ||
3300beda AK |
784 | BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); |
785 | err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); | |
39341867 AK |
786 | if (err) |
787 | goto fail; | |
ac27a0ec DK |
788 | |
789 | percpu_counter_dec(&sbi->s_freeinodes_counter); | |
790 | if (S_ISDIR(mode)) | |
791 | percpu_counter_inc(&sbi->s_dirs_counter); | |
a0375156 | 792 | ext4_mark_super_dirty(sb); |
ac27a0ec | 793 | |
772cb7c8 JS |
794 | if (sbi->s_log_groups_per_flex) { |
795 | flex_group = ext4_flex_group(sbi, group); | |
9f24e420 | 796 | atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes); |
772cb7c8 | 797 | } |
5cb81dab DM |
798 | if (owner) { |
799 | inode->i_mode = mode; | |
800 | inode->i_uid = owner[0]; | |
801 | inode->i_gid = owner[1]; | |
802 | } else if (test_opt(sb, GRPID)) { | |
b10b8520 DM |
803 | inode->i_mode = mode; |
804 | inode->i_uid = current_fsuid(); | |
ac27a0ec | 805 | inode->i_gid = dir->i_gid; |
ac27a0ec | 806 | } else |
b10b8520 | 807 | inode_init_owner(inode, dir, mode); |
ac27a0ec | 808 | |
717d50e4 | 809 | inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); |
ac27a0ec DK |
810 | /* This is the optimal IO size (for stat), not the fs block size */ |
811 | inode->i_blocks = 0; | |
ef7f3835 KS |
812 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
813 | ext4_current_time(inode); | |
ac27a0ec DK |
814 | |
815 | memset(ei->i_data, 0, sizeof(ei->i_data)); | |
816 | ei->i_dir_start_lookup = 0; | |
817 | ei->i_disksize = 0; | |
818 | ||
4af83508 | 819 | /* Don't inherit extent flag from directory, amongst others. */ |
2dc6b0d4 DG |
820 | ei->i_flags = |
821 | ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); | |
ac27a0ec | 822 | ei->i_file_acl = 0; |
ac27a0ec | 823 | ei->i_dtime = 0; |
ac27a0ec | 824 | ei->i_block_group = group; |
a4912123 | 825 | ei->i_last_alloc_group = ~0; |
ac27a0ec | 826 | |
617ba13b | 827 | ext4_set_inode_flags(inode); |
ac27a0ec | 828 | if (IS_DIRSYNC(inode)) |
0390131b | 829 | ext4_handle_sync(handle); |
6b38e842 | 830 | if (insert_inode_locked(inode) < 0) { |
acd6ad83 JK |
831 | /* |
832 | * Likely a bitmap corruption causing inode to be allocated | |
833 | * twice. | |
834 | */ | |
835 | err = -EIO; | |
836 | goto fail; | |
6b38e842 | 837 | } |
ac27a0ec DK |
838 | spin_lock(&sbi->s_next_gen_lock); |
839 | inode->i_generation = sbi->s_next_generation++; | |
840 | spin_unlock(&sbi->s_next_gen_lock); | |
841 | ||
353eb83c | 842 | ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ |
19f5fb7a | 843 | ext4_set_inode_state(inode, EXT4_STATE_NEW); |
ef7f3835 KS |
844 | |
845 | ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; | |
ac27a0ec DK |
846 | |
847 | ret = inode; | |
871a2931 | 848 | dquot_initialize(inode); |
63936dda CH |
849 | err = dquot_alloc_inode(inode); |
850 | if (err) | |
ac27a0ec | 851 | goto fail_drop; |
ac27a0ec | 852 | |
617ba13b | 853 | err = ext4_init_acl(handle, inode, dir); |
ac27a0ec DK |
854 | if (err) |
855 | goto fail_free_drop; | |
856 | ||
2a7dba39 | 857 | err = ext4_init_security(handle, inode, dir, qstr); |
ac27a0ec DK |
858 | if (err) |
859 | goto fail_free_drop; | |
860 | ||
83982b6f | 861 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
e4079a11 | 862 | /* set extent flag only for directory, file and normal symlink*/ |
e65187e6 | 863 | if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { |
12e9b892 | 864 | ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); |
42bf0383 | 865 | ext4_ext_tree_init(handle, inode); |
42bf0383 | 866 | } |
a86c6181 | 867 | } |
ac27a0ec | 868 | |
688f869c TT |
869 | if (ext4_handle_valid(handle)) { |
870 | ei->i_sync_tid = handle->h_transaction->t_tid; | |
871 | ei->i_datasync_tid = handle->h_transaction->t_tid; | |
872 | } | |
873 | ||
8753e88f AK |
874 | err = ext4_mark_inode_dirty(handle, inode); |
875 | if (err) { | |
876 | ext4_std_error(sb, err); | |
877 | goto fail_free_drop; | |
878 | } | |
879 | ||
617ba13b | 880 | ext4_debug("allocating inode %lu\n", inode->i_ino); |
9bffad1e | 881 | trace_ext4_allocate_inode(inode, dir, mode); |
ac27a0ec DK |
882 | goto really_out; |
883 | fail: | |
617ba13b | 884 | ext4_std_error(sb, err); |
ac27a0ec DK |
885 | out: |
886 | iput(inode); | |
887 | ret = ERR_PTR(err); | |
888 | really_out: | |
3300beda | 889 | brelse(inode_bitmap_bh); |
ac27a0ec DK |
890 | return ret; |
891 | ||
892 | fail_free_drop: | |
63936dda | 893 | dquot_free_inode(inode); |
ac27a0ec DK |
894 | |
895 | fail_drop: | |
9f754758 | 896 | dquot_drop(inode); |
ac27a0ec | 897 | inode->i_flags |= S_NOQUOTA; |
6d6b77f1 | 898 | clear_nlink(inode); |
6b38e842 | 899 | unlock_new_inode(inode); |
ac27a0ec | 900 | iput(inode); |
3300beda | 901 | brelse(inode_bitmap_bh); |
ac27a0ec DK |
902 | return ERR_PTR(err); |
903 | } | |
904 | ||
905 | /* Verify that we are loading a valid orphan from disk */ | |
617ba13b | 906 | struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) |
ac27a0ec | 907 | { |
617ba13b | 908 | unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); |
fd2d4291 | 909 | ext4_group_t block_group; |
ac27a0ec | 910 | int bit; |
1d1fe1ee | 911 | struct buffer_head *bitmap_bh; |
ac27a0ec | 912 | struct inode *inode = NULL; |
1d1fe1ee | 913 | long err = -EIO; |
ac27a0ec DK |
914 | |
915 | /* Error cases - e2fsck has already cleaned up for us */ | |
916 | if (ino > max_ino) { | |
12062ddd | 917 | ext4_warning(sb, "bad orphan ino %lu! e2fsck was run?", ino); |
1d1fe1ee | 918 | goto error; |
ac27a0ec DK |
919 | } |
920 | ||
617ba13b MC |
921 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
922 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); | |
e29d1cde | 923 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
ac27a0ec | 924 | if (!bitmap_bh) { |
12062ddd | 925 | ext4_warning(sb, "inode bitmap error for orphan %lu", ino); |
1d1fe1ee | 926 | goto error; |
ac27a0ec DK |
927 | } |
928 | ||
929 | /* Having the inode bit set should be a 100% indicator that this | |
930 | * is a valid orphan (no e2fsck run on fs). Orphans also include | |
931 | * inodes that were being truncated, so we can't check i_nlink==0. | |
932 | */ | |
1d1fe1ee DH |
933 | if (!ext4_test_bit(bit, bitmap_bh->b_data)) |
934 | goto bad_orphan; | |
935 | ||
936 | inode = ext4_iget(sb, ino); | |
937 | if (IS_ERR(inode)) | |
938 | goto iget_failed; | |
939 | ||
91ef4caf DG |
940 | /* |
941 | * If the orphans has i_nlinks > 0 then it should be able to be | |
942 | * truncated, otherwise it won't be removed from the orphan list | |
943 | * during processing and an infinite loop will result. | |
944 | */ | |
945 | if (inode->i_nlink && !ext4_can_truncate(inode)) | |
946 | goto bad_orphan; | |
947 | ||
1d1fe1ee DH |
948 | if (NEXT_ORPHAN(inode) > max_ino) |
949 | goto bad_orphan; | |
950 | brelse(bitmap_bh); | |
951 | return inode; | |
952 | ||
953 | iget_failed: | |
954 | err = PTR_ERR(inode); | |
955 | inode = NULL; | |
956 | bad_orphan: | |
12062ddd | 957 | ext4_warning(sb, "bad orphan inode %lu! e2fsck was run?", ino); |
1d1fe1ee DH |
958 | printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", |
959 | bit, (unsigned long long)bitmap_bh->b_blocknr, | |
960 | ext4_test_bit(bit, bitmap_bh->b_data)); | |
961 | printk(KERN_NOTICE "inode=%p\n", inode); | |
962 | if (inode) { | |
963 | printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", | |
964 | is_bad_inode(inode)); | |
965 | printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", | |
966 | NEXT_ORPHAN(inode)); | |
967 | printk(KERN_NOTICE "max_ino=%lu\n", max_ino); | |
91ef4caf | 968 | printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink); |
ac27a0ec | 969 | /* Avoid freeing blocks if we got a bad deleted inode */ |
1d1fe1ee | 970 | if (inode->i_nlink == 0) |
ac27a0ec DK |
971 | inode->i_blocks = 0; |
972 | iput(inode); | |
ac27a0ec | 973 | } |
ac27a0ec | 974 | brelse(bitmap_bh); |
1d1fe1ee DH |
975 | error: |
976 | return ERR_PTR(err); | |
ac27a0ec DK |
977 | } |
978 | ||
af5bc92d | 979 | unsigned long ext4_count_free_inodes(struct super_block *sb) |
ac27a0ec DK |
980 | { |
981 | unsigned long desc_count; | |
617ba13b | 982 | struct ext4_group_desc *gdp; |
8df9675f | 983 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
617ba13b MC |
984 | #ifdef EXT4FS_DEBUG |
985 | struct ext4_super_block *es; | |
ac27a0ec DK |
986 | unsigned long bitmap_count, x; |
987 | struct buffer_head *bitmap_bh = NULL; | |
988 | ||
617ba13b | 989 | es = EXT4_SB(sb)->s_es; |
ac27a0ec DK |
990 | desc_count = 0; |
991 | bitmap_count = 0; | |
992 | gdp = NULL; | |
8df9675f | 993 | for (i = 0; i < ngroups; i++) { |
af5bc92d | 994 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
995 | if (!gdp) |
996 | continue; | |
560671a0 | 997 | desc_count += ext4_free_inodes_count(sb, gdp); |
ac27a0ec | 998 | brelse(bitmap_bh); |
e29d1cde | 999 | bitmap_bh = ext4_read_inode_bitmap(sb, i); |
ac27a0ec DK |
1000 | if (!bitmap_bh) |
1001 | continue; | |
1002 | ||
617ba13b | 1003 | x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); |
c549a95d | 1004 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
785b4b3a | 1005 | (unsigned long) i, ext4_free_inodes_count(sb, gdp), x); |
ac27a0ec DK |
1006 | bitmap_count += x; |
1007 | } | |
1008 | brelse(bitmap_bh); | |
4776004f TT |
1009 | printk(KERN_DEBUG "ext4_count_free_inodes: " |
1010 | "stored = %u, computed = %lu, %lu\n", | |
1011 | le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); | |
ac27a0ec DK |
1012 | return desc_count; |
1013 | #else | |
1014 | desc_count = 0; | |
8df9675f | 1015 | for (i = 0; i < ngroups; i++) { |
af5bc92d | 1016 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1017 | if (!gdp) |
1018 | continue; | |
560671a0 | 1019 | desc_count += ext4_free_inodes_count(sb, gdp); |
ac27a0ec DK |
1020 | cond_resched(); |
1021 | } | |
1022 | return desc_count; | |
1023 | #endif | |
1024 | } | |
1025 | ||
1026 | /* Called at mount-time, super-block is locked */ | |
af5bc92d | 1027 | unsigned long ext4_count_dirs(struct super_block * sb) |
ac27a0ec DK |
1028 | { |
1029 | unsigned long count = 0; | |
8df9675f | 1030 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
ac27a0ec | 1031 | |
8df9675f | 1032 | for (i = 0; i < ngroups; i++) { |
af5bc92d | 1033 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1034 | if (!gdp) |
1035 | continue; | |
560671a0 | 1036 | count += ext4_used_dirs_count(sb, gdp); |
ac27a0ec DK |
1037 | } |
1038 | return count; | |
1039 | } | |
bfff6873 LC |
1040 | |
1041 | /* | |
1042 | * Zeroes not yet zeroed inode table - just write zeroes through the whole | |
1043 | * inode table. Must be called without any spinlock held. The only place | |
1044 | * where it is called from on active part of filesystem is ext4lazyinit | |
1045 | * thread, so we do not need any special locks, however we have to prevent | |
1046 | * inode allocation from the current group, so we take alloc_sem lock, to | |
119c0d44 | 1047 | * block ext4_new_inode() until we are finished. |
bfff6873 | 1048 | */ |
e0cbee3e | 1049 | int ext4_init_inode_table(struct super_block *sb, ext4_group_t group, |
bfff6873 LC |
1050 | int barrier) |
1051 | { | |
1052 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); | |
1053 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
1054 | struct ext4_group_desc *gdp = NULL; | |
1055 | struct buffer_head *group_desc_bh; | |
1056 | handle_t *handle; | |
1057 | ext4_fsblk_t blk; | |
1058 | int num, ret = 0, used_blks = 0; | |
bfff6873 LC |
1059 | |
1060 | /* This should not happen, but just to be sure check this */ | |
1061 | if (sb->s_flags & MS_RDONLY) { | |
1062 | ret = 1; | |
1063 | goto out; | |
1064 | } | |
1065 | ||
1066 | gdp = ext4_get_group_desc(sb, group, &group_desc_bh); | |
1067 | if (!gdp) | |
1068 | goto out; | |
1069 | ||
1070 | /* | |
1071 | * We do not need to lock this, because we are the only one | |
1072 | * handling this flag. | |
1073 | */ | |
1074 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)) | |
1075 | goto out; | |
1076 | ||
1077 | handle = ext4_journal_start_sb(sb, 1); | |
1078 | if (IS_ERR(handle)) { | |
1079 | ret = PTR_ERR(handle); | |
1080 | goto out; | |
1081 | } | |
1082 | ||
1083 | down_write(&grp->alloc_sem); | |
1084 | /* | |
1085 | * If inode bitmap was already initialized there may be some | |
1086 | * used inodes so we need to skip blocks with used inodes in | |
1087 | * inode table. | |
1088 | */ | |
1089 | if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) | |
1090 | used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) - | |
1091 | ext4_itable_unused_count(sb, gdp)), | |
1092 | sbi->s_inodes_per_block); | |
1093 | ||
857ac889 LC |
1094 | if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) { |
1095 | ext4_error(sb, "Something is wrong with group %u\n" | |
1096 | "Used itable blocks: %d" | |
1097 | "itable unused count: %u\n", | |
1098 | group, used_blks, | |
1099 | ext4_itable_unused_count(sb, gdp)); | |
1100 | ret = 1; | |
33853a0d | 1101 | goto err_out; |
857ac889 LC |
1102 | } |
1103 | ||
bfff6873 LC |
1104 | blk = ext4_inode_table(sb, gdp) + used_blks; |
1105 | num = sbi->s_itb_per_group - used_blks; | |
1106 | ||
1107 | BUFFER_TRACE(group_desc_bh, "get_write_access"); | |
1108 | ret = ext4_journal_get_write_access(handle, | |
1109 | group_desc_bh); | |
1110 | if (ret) | |
1111 | goto err_out; | |
1112 | ||
bfff6873 LC |
1113 | /* |
1114 | * Skip zeroout if the inode table is full. But we set the ZEROED | |
1115 | * flag anyway, because obviously, when it is full it does not need | |
1116 | * further zeroing. | |
1117 | */ | |
1118 | if (unlikely(num == 0)) | |
1119 | goto skip_zeroout; | |
1120 | ||
1121 | ext4_debug("going to zero out inode table in group %d\n", | |
1122 | group); | |
a107e5a3 | 1123 | ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS); |
bfff6873 LC |
1124 | if (ret < 0) |
1125 | goto err_out; | |
a107e5a3 TT |
1126 | if (barrier) |
1127 | blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL); | |
bfff6873 LC |
1128 | |
1129 | skip_zeroout: | |
1130 | ext4_lock_group(sb, group); | |
1131 | gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED); | |
1132 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); | |
1133 | ext4_unlock_group(sb, group); | |
1134 | ||
1135 | BUFFER_TRACE(group_desc_bh, | |
1136 | "call ext4_handle_dirty_metadata"); | |
1137 | ret = ext4_handle_dirty_metadata(handle, NULL, | |
1138 | group_desc_bh); | |
1139 | ||
1140 | err_out: | |
1141 | up_write(&grp->alloc_sem); | |
1142 | ext4_journal_stop(handle); | |
1143 | out: | |
1144 | return ret; | |
1145 | } |