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> |
3dcf5451 CH |
26 | #include "ext4.h" |
27 | #include "ext4_jbd2.h" | |
ac27a0ec DK |
28 | #include "xattr.h" |
29 | #include "acl.h" | |
717d50e4 | 30 | #include "group.h" |
ac27a0ec DK |
31 | |
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 | ||
717d50e4 AD |
46 | /* |
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 | */ | |
51 | void 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 */ | |
fd2d4291 AM |
66 | unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh, |
67 | ext4_group_t block_group, | |
717d50e4 AD |
68 | 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)) { | |
a9df9a49 | 77 | ext4_error(sb, __func__, "Checksum bad for group %u", |
717d50e4 | 78 | block_group); |
560671a0 AK |
79 | ext4_free_blks_set(sb, gdp, 0); |
80 | ext4_free_inodes_set(sb, gdp, 0); | |
81 | ext4_itable_unused_set(sb, gdp, 0); | |
717d50e4 AD |
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); | |
648f5879 | 87 | mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, |
717d50e4 AD |
88 | bh->b_data); |
89 | ||
90 | return EXT4_INODES_PER_GROUP(sb); | |
91 | } | |
ac27a0ec DK |
92 | |
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 | */ | |
99 | static struct buffer_head * | |
e29d1cde | 100 | ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) |
ac27a0ec | 101 | { |
617ba13b | 102 | struct ext4_group_desc *desc; |
ac27a0ec | 103 | struct buffer_head *bh = NULL; |
e29d1cde | 104 | ext4_fsblk_t bitmap_blk; |
ac27a0ec | 105 | |
617ba13b | 106 | desc = ext4_get_group_desc(sb, block_group, NULL); |
ac27a0ec | 107 | if (!desc) |
e29d1cde ES |
108 | 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__, | |
113 | "Cannot read inode bitmap - " | |
a9df9a49 | 114 | "block_group = %u, inode_bitmap = %llu", |
e29d1cde ES |
115 | block_group, bitmap_blk); |
116 | return NULL; | |
117 | } | |
2ccb5fb9 | 118 | if (bitmap_uptodate(bh)) |
e29d1cde ES |
119 | return bh; |
120 | ||
c806e68f | 121 | lock_buffer(bh); |
2ccb5fb9 AK |
122 | if (bitmap_uptodate(bh)) { |
123 | unlock_buffer(bh); | |
124 | return bh; | |
125 | } | |
b5f10eed | 126 | spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
717d50e4 | 127 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
e29d1cde | 128 | ext4_init_inode_bitmap(sb, bh, block_group, desc); |
2ccb5fb9 | 129 | set_bitmap_uptodate(bh); |
e29d1cde | 130 | set_buffer_uptodate(bh); |
b5f10eed | 131 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
3300beda | 132 | unlock_buffer(bh); |
e29d1cde | 133 | return bh; |
717d50e4 | 134 | } |
b5f10eed | 135 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
2ccb5fb9 AK |
136 | if (buffer_uptodate(bh)) { |
137 | /* | |
138 | * if not uninit if bh is uptodate, | |
139 | * bitmap is also uptodate | |
140 | */ | |
141 | set_bitmap_uptodate(bh); | |
142 | unlock_buffer(bh); | |
143 | return bh; | |
144 | } | |
145 | /* | |
146 | * submit the buffer_head for read. We can | |
147 | * safely mark the bitmap as uptodate now. | |
148 | * We do it here so the bitmap uptodate bit | |
149 | * get set with buffer lock held. | |
150 | */ | |
151 | set_bitmap_uptodate(bh); | |
e29d1cde ES |
152 | if (bh_submit_read(bh) < 0) { |
153 | put_bh(bh); | |
154 | ext4_error(sb, __func__, | |
ac27a0ec | 155 | "Cannot read inode bitmap - " |
a9df9a49 | 156 | "block_group = %u, inode_bitmap = %llu", |
e29d1cde ES |
157 | block_group, bitmap_blk); |
158 | return NULL; | |
159 | } | |
ac27a0ec DK |
160 | return bh; |
161 | } | |
162 | ||
163 | /* | |
164 | * NOTE! When we get the inode, we're the only people | |
165 | * that have access to it, and as such there are no | |
166 | * race conditions we have to worry about. The inode | |
167 | * is not on the hash-lists, and it cannot be reached | |
168 | * through the filesystem because the directory entry | |
169 | * has been deleted earlier. | |
170 | * | |
171 | * HOWEVER: we must make sure that we get no aliases, | |
172 | * which means that we have to call "clear_inode()" | |
173 | * _before_ we mark the inode not in use in the inode | |
174 | * bitmaps. Otherwise a newly created file might use | |
175 | * the same inode number (not actually the same pointer | |
176 | * though), and then we'd have two inodes sharing the | |
177 | * same inode number and space on the harddisk. | |
178 | */ | |
af5bc92d | 179 | void ext4_free_inode(handle_t *handle, struct inode *inode) |
ac27a0ec | 180 | { |
af5bc92d | 181 | struct super_block *sb = inode->i_sb; |
ac27a0ec DK |
182 | int is_directory; |
183 | unsigned long ino; | |
184 | struct buffer_head *bitmap_bh = NULL; | |
185 | struct buffer_head *bh2; | |
fd2d4291 | 186 | ext4_group_t block_group; |
ac27a0ec | 187 | unsigned long bit; |
af5bc92d TT |
188 | struct ext4_group_desc *gdp; |
189 | struct ext4_super_block *es; | |
617ba13b | 190 | struct ext4_sb_info *sbi; |
7ce9d5d1 | 191 | int fatal = 0, err, count, cleared; |
ac27a0ec DK |
192 | |
193 | if (atomic_read(&inode->i_count) > 1) { | |
4776004f TT |
194 | printk(KERN_ERR "ext4_free_inode: inode has count=%d\n", |
195 | atomic_read(&inode->i_count)); | |
ac27a0ec DK |
196 | return; |
197 | } | |
198 | if (inode->i_nlink) { | |
4776004f TT |
199 | printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n", |
200 | inode->i_nlink); | |
ac27a0ec DK |
201 | return; |
202 | } | |
203 | if (!sb) { | |
4776004f TT |
204 | printk(KERN_ERR "ext4_free_inode: inode on " |
205 | "nonexistent device\n"); | |
ac27a0ec DK |
206 | return; |
207 | } | |
617ba13b | 208 | sbi = EXT4_SB(sb); |
ac27a0ec DK |
209 | |
210 | ino = inode->i_ino; | |
af5bc92d | 211 | ext4_debug("freeing inode %lu\n", ino); |
ba80b101 TT |
212 | trace_mark(ext4_free_inode, |
213 | "dev %s ino %lu mode %d uid %lu gid %lu bocks %llu", | |
214 | sb->s_id, inode->i_ino, inode->i_mode, | |
215 | (unsigned long) inode->i_uid, (unsigned long) inode->i_gid, | |
216 | (unsigned long long) inode->i_blocks); | |
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 | */ | |
a269eb18 | 222 | vfs_dq_init(inode); |
617ba13b | 223 | ext4_xattr_delete_inode(handle, inode); |
a269eb18 JK |
224 | vfs_dq_free_inode(inode); |
225 | vfs_dq_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 */ | |
af5bc92d | 230 | 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)) { | |
af5bc92d TT |
234 | ext4_error(sb, "ext4_free_inode", |
235 | "reserved or nonexistent inode %lu", ino); | |
ac27a0ec DK |
236 | goto error_return; |
237 | } | |
617ba13b MC |
238 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
239 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); | |
e29d1cde | 240 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
ac27a0ec DK |
241 | if (!bitmap_bh) |
242 | goto error_return; | |
243 | ||
244 | BUFFER_TRACE(bitmap_bh, "get_write_access"); | |
617ba13b | 245 | fatal = ext4_journal_get_write_access(handle, bitmap_bh); |
ac27a0ec DK |
246 | if (fatal) |
247 | goto error_return; | |
248 | ||
249 | /* Ok, now we can actually update the inode bitmaps.. */ | |
7ce9d5d1 ES |
250 | spin_lock(sb_bgl_lock(sbi, block_group)); |
251 | cleared = ext4_clear_bit(bit, bitmap_bh->b_data); | |
252 | spin_unlock(sb_bgl_lock(sbi, block_group)); | |
253 | if (!cleared) | |
af5bc92d TT |
254 | ext4_error(sb, "ext4_free_inode", |
255 | "bit already cleared for inode %lu", ino); | |
ac27a0ec | 256 | else { |
af5bc92d | 257 | gdp = ext4_get_group_desc(sb, block_group, &bh2); |
ac27a0ec DK |
258 | |
259 | BUFFER_TRACE(bh2, "get_write_access"); | |
617ba13b | 260 | fatal = ext4_journal_get_write_access(handle, bh2); |
ac27a0ec DK |
261 | if (fatal) goto error_return; |
262 | ||
263 | if (gdp) { | |
264 | spin_lock(sb_bgl_lock(sbi, block_group)); | |
560671a0 AK |
265 | count = ext4_free_inodes_count(sb, gdp) + 1; |
266 | ext4_free_inodes_set(sb, gdp, count); | |
267 | if (is_directory) { | |
268 | count = ext4_used_dirs_count(sb, gdp) - 1; | |
269 | ext4_used_dirs_set(sb, gdp, count); | |
270 | } | |
717d50e4 AD |
271 | gdp->bg_checksum = ext4_group_desc_csum(sbi, |
272 | block_group, gdp); | |
ac27a0ec DK |
273 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
274 | percpu_counter_inc(&sbi->s_freeinodes_counter); | |
275 | if (is_directory) | |
276 | percpu_counter_dec(&sbi->s_dirs_counter); | |
277 | ||
772cb7c8 | 278 | if (sbi->s_log_groups_per_flex) { |
9f24e420 TT |
279 | ext4_group_t f; |
280 | ||
281 | f = ext4_flex_group(sbi, block_group); | |
282 | atomic_inc(&sbi->s_flex_groups[f].free_inodes); | |
772cb7c8 | 283 | } |
ac27a0ec | 284 | } |
0390131b FM |
285 | BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); |
286 | err = ext4_handle_dirty_metadata(handle, NULL, bh2); | |
ac27a0ec DK |
287 | if (!fatal) fatal = err; |
288 | } | |
0390131b FM |
289 | BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); |
290 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); | |
ac27a0ec DK |
291 | if (!fatal) |
292 | fatal = err; | |
293 | sb->s_dirt = 1; | |
294 | error_return: | |
295 | brelse(bitmap_bh); | |
617ba13b | 296 | ext4_std_error(sb, fatal); |
ac27a0ec DK |
297 | } |
298 | ||
299 | /* | |
300 | * There are two policies for allocating an inode. If the new inode is | |
301 | * a directory, then a forward search is made for a block group with both | |
302 | * free space and a low directory-to-inode ratio; if that fails, then of | |
303 | * the groups with above-average free space, that group with the fewest | |
304 | * directories already is chosen. | |
305 | * | |
306 | * For other inodes, search forward from the parent directory\'s block | |
307 | * group to find a free inode. | |
308 | */ | |
2aa9fc4c AM |
309 | static int find_group_dir(struct super_block *sb, struct inode *parent, |
310 | ext4_group_t *best_group) | |
ac27a0ec | 311 | { |
fd2d4291 | 312 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
ac27a0ec | 313 | unsigned int freei, avefreei; |
617ba13b | 314 | struct ext4_group_desc *desc, *best_desc = NULL; |
2aa9fc4c AM |
315 | ext4_group_t group; |
316 | int ret = -1; | |
ac27a0ec | 317 | |
617ba13b | 318 | freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter); |
ac27a0ec DK |
319 | avefreei = freei / ngroups; |
320 | ||
321 | for (group = 0; group < ngroups; group++) { | |
af5bc92d | 322 | desc = ext4_get_group_desc(sb, group, NULL); |
560671a0 | 323 | if (!desc || !ext4_free_inodes_count(sb, desc)) |
ac27a0ec | 324 | continue; |
560671a0 | 325 | if (ext4_free_inodes_count(sb, desc) < avefreei) |
ac27a0ec DK |
326 | continue; |
327 | if (!best_desc || | |
560671a0 AK |
328 | (ext4_free_blks_count(sb, desc) > |
329 | ext4_free_blks_count(sb, best_desc))) { | |
2aa9fc4c | 330 | *best_group = group; |
ac27a0ec | 331 | best_desc = desc; |
2aa9fc4c | 332 | ret = 0; |
ac27a0ec DK |
333 | } |
334 | } | |
2aa9fc4c | 335 | return ret; |
ac27a0ec DK |
336 | } |
337 | ||
772cb7c8 JS |
338 | #define free_block_ratio 10 |
339 | ||
340 | static int find_group_flex(struct super_block *sb, struct inode *parent, | |
341 | ext4_group_t *best_group) | |
342 | { | |
343 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
344 | struct ext4_group_desc *desc; | |
345 | struct buffer_head *bh; | |
346 | struct flex_groups *flex_group = sbi->s_flex_groups; | |
347 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; | |
348 | ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group); | |
349 | ext4_group_t ngroups = sbi->s_groups_count; | |
350 | int flex_size = ext4_flex_bg_size(sbi); | |
351 | ext4_group_t best_flex = parent_fbg_group; | |
352 | int blocks_per_flex = sbi->s_blocks_per_group * flex_size; | |
353 | int flexbg_free_blocks; | |
354 | int flex_freeb_ratio; | |
355 | ext4_group_t n_fbg_groups; | |
356 | ext4_group_t i; | |
357 | ||
358 | n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >> | |
359 | sbi->s_log_groups_per_flex; | |
360 | ||
361 | find_close_to_parent: | |
9f24e420 | 362 | flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks); |
772cb7c8 | 363 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; |
9f24e420 | 364 | if (atomic_read(&flex_group[best_flex].free_inodes) && |
772cb7c8 JS |
365 | flex_freeb_ratio > free_block_ratio) |
366 | goto found_flexbg; | |
367 | ||
368 | if (best_flex && best_flex == parent_fbg_group) { | |
369 | best_flex--; | |
370 | goto find_close_to_parent; | |
371 | } | |
372 | ||
373 | for (i = 0; i < n_fbg_groups; i++) { | |
374 | if (i == parent_fbg_group || i == parent_fbg_group - 1) | |
375 | continue; | |
376 | ||
9f24e420 | 377 | flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks); |
772cb7c8 JS |
378 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; |
379 | ||
380 | if (flex_freeb_ratio > free_block_ratio && | |
9f24e420 | 381 | (atomic_read(&flex_group[i].free_inodes))) { |
772cb7c8 JS |
382 | best_flex = i; |
383 | goto found_flexbg; | |
384 | } | |
385 | ||
9f24e420 TT |
386 | if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) || |
387 | ((atomic_read(&flex_group[i].free_blocks) > | |
388 | atomic_read(&flex_group[best_flex].free_blocks)) && | |
389 | atomic_read(&flex_group[i].free_inodes))) | |
772cb7c8 JS |
390 | best_flex = i; |
391 | } | |
392 | ||
9f24e420 TT |
393 | if (!atomic_read(&flex_group[best_flex].free_inodes) || |
394 | !atomic_read(&flex_group[best_flex].free_blocks)) | |
772cb7c8 JS |
395 | return -1; |
396 | ||
397 | found_flexbg: | |
398 | for (i = best_flex * flex_size; i < ngroups && | |
399 | i < (best_flex + 1) * flex_size; i++) { | |
400 | desc = ext4_get_group_desc(sb, i, &bh); | |
560671a0 | 401 | if (ext4_free_inodes_count(sb, desc)) { |
772cb7c8 JS |
402 | *best_group = i; |
403 | goto out; | |
404 | } | |
405 | } | |
406 | ||
407 | return -1; | |
408 | out: | |
409 | return 0; | |
410 | } | |
411 | ||
a4912123 TT |
412 | struct orlov_stats { |
413 | __u32 free_inodes; | |
414 | __u32 free_blocks; | |
415 | __u32 used_dirs; | |
416 | }; | |
417 | ||
418 | /* | |
419 | * Helper function for Orlov's allocator; returns critical information | |
420 | * for a particular block group or flex_bg. If flex_size is 1, then g | |
421 | * is a block group number; otherwise it is flex_bg number. | |
422 | */ | |
423 | void get_orlov_stats(struct super_block *sb, ext4_group_t g, | |
424 | int flex_size, struct orlov_stats *stats) | |
425 | { | |
426 | struct ext4_group_desc *desc; | |
427 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; | |
428 | int i; | |
429 | ||
430 | stats->free_inodes = 0; | |
431 | stats->free_blocks = 0; | |
432 | stats->used_dirs = 0; | |
433 | ||
434 | g *= flex_size; | |
435 | ||
436 | for (i = 0; i < flex_size; i++) { | |
437 | if (g >= ngroups) | |
438 | break; | |
439 | desc = ext4_get_group_desc(sb, g++, NULL); | |
440 | if (!desc) | |
441 | continue; | |
442 | ||
443 | stats->free_inodes += ext4_free_inodes_count(sb, desc); | |
444 | stats->free_blocks += ext4_free_blks_count(sb, desc); | |
445 | stats->used_dirs += ext4_used_dirs_count(sb, desc); | |
446 | } | |
447 | } | |
448 | ||
ac27a0ec DK |
449 | /* |
450 | * Orlov's allocator for directories. | |
451 | * | |
452 | * We always try to spread first-level directories. | |
453 | * | |
454 | * If there are blockgroups with both free inodes and free blocks counts | |
455 | * not worse than average we return one with smallest directory count. | |
456 | * Otherwise we simply return a random group. | |
457 | * | |
458 | * For the rest rules look so: | |
459 | * | |
460 | * It's OK to put directory into a group unless | |
461 | * it has too many directories already (max_dirs) or | |
462 | * it has too few free inodes left (min_inodes) or | |
463 | * it has too few free blocks left (min_blocks) or | |
1cc8dcf5 | 464 | * Parent's group is preferred, if it doesn't satisfy these |
ac27a0ec DK |
465 | * conditions we search cyclically through the rest. If none |
466 | * of the groups look good we just look for a group with more | |
467 | * free inodes than average (starting at parent's group). | |
ac27a0ec DK |
468 | */ |
469 | ||
2aa9fc4c | 470 | static int find_group_orlov(struct super_block *sb, struct inode *parent, |
a4912123 | 471 | ext4_group_t *group, int mode) |
ac27a0ec | 472 | { |
fd2d4291 | 473 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
617ba13b | 474 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
fd2d4291 | 475 | ext4_group_t ngroups = sbi->s_groups_count; |
617ba13b | 476 | int inodes_per_group = EXT4_INODES_PER_GROUP(sb); |
ac27a0ec | 477 | unsigned int freei, avefreei; |
617ba13b | 478 | ext4_fsblk_t freeb, avefreeb; |
ac27a0ec | 479 | unsigned int ndirs; |
a4912123 | 480 | int max_dirs, min_inodes; |
617ba13b | 481 | ext4_grpblk_t min_blocks; |
a4912123 | 482 | ext4_group_t i, grp, g; |
617ba13b | 483 | struct ext4_group_desc *desc; |
a4912123 TT |
484 | struct orlov_stats stats; |
485 | int flex_size = ext4_flex_bg_size(sbi); | |
486 | ||
487 | if (flex_size > 1) { | |
488 | ngroups = (ngroups + flex_size - 1) >> | |
489 | sbi->s_log_groups_per_flex; | |
490 | parent_group >>= sbi->s_log_groups_per_flex; | |
491 | } | |
ac27a0ec DK |
492 | |
493 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); | |
494 | avefreei = freei / ngroups; | |
495 | freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); | |
3a5b2ecd | 496 | avefreeb = freeb; |
f4e5bc24 | 497 | do_div(avefreeb, ngroups); |
ac27a0ec DK |
498 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
499 | ||
a4912123 TT |
500 | if (S_ISDIR(mode) && |
501 | ((parent == sb->s_root->d_inode) || | |
502 | (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL))) { | |
ac27a0ec | 503 | int best_ndir = inodes_per_group; |
2aa9fc4c | 504 | int ret = -1; |
ac27a0ec | 505 | |
2aa9fc4c AM |
506 | get_random_bytes(&grp, sizeof(grp)); |
507 | parent_group = (unsigned)grp % ngroups; | |
ac27a0ec | 508 | for (i = 0; i < ngroups; i++) { |
a4912123 TT |
509 | g = (parent_group + i) % ngroups; |
510 | get_orlov_stats(sb, g, flex_size, &stats); | |
511 | if (!stats.free_inodes) | |
ac27a0ec | 512 | continue; |
a4912123 | 513 | if (stats.used_dirs >= best_ndir) |
ac27a0ec | 514 | continue; |
a4912123 | 515 | if (stats.free_inodes < avefreei) |
ac27a0ec | 516 | continue; |
a4912123 | 517 | if (stats.free_blocks < avefreeb) |
ac27a0ec | 518 | continue; |
a4912123 | 519 | grp = g; |
2aa9fc4c | 520 | ret = 0; |
a4912123 TT |
521 | best_ndir = stats.used_dirs; |
522 | } | |
523 | if (ret) | |
524 | goto fallback; | |
525 | found_flex_bg: | |
526 | if (flex_size == 1) { | |
527 | *group = grp; | |
528 | return 0; | |
529 | } | |
530 | ||
531 | /* | |
532 | * We pack inodes at the beginning of the flexgroup's | |
533 | * inode tables. Block allocation decisions will do | |
534 | * something similar, although regular files will | |
535 | * start at 2nd block group of the flexgroup. See | |
536 | * ext4_ext_find_goal() and ext4_find_near(). | |
537 | */ | |
538 | grp *= flex_size; | |
539 | for (i = 0; i < flex_size; i++) { | |
540 | if (grp+i >= sbi->s_groups_count) | |
541 | break; | |
542 | desc = ext4_get_group_desc(sb, grp+i, NULL); | |
543 | if (desc && ext4_free_inodes_count(sb, desc)) { | |
544 | *group = grp+i; | |
545 | return 0; | |
546 | } | |
ac27a0ec | 547 | } |
ac27a0ec DK |
548 | goto fallback; |
549 | } | |
550 | ||
ac27a0ec | 551 | max_dirs = ndirs / ngroups + inodes_per_group / 16; |
a4912123 TT |
552 | min_inodes = avefreei - inodes_per_group*flex_size / 4; |
553 | if (min_inodes < 1) | |
554 | min_inodes = 1; | |
555 | min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb)*flex_size / 4; | |
556 | ||
557 | /* | |
558 | * Start looking in the flex group where we last allocated an | |
559 | * inode for this parent directory | |
560 | */ | |
561 | if (EXT4_I(parent)->i_last_alloc_group != ~0) { | |
562 | parent_group = EXT4_I(parent)->i_last_alloc_group; | |
563 | if (flex_size > 1) | |
564 | parent_group >>= sbi->s_log_groups_per_flex; | |
565 | } | |
ac27a0ec DK |
566 | |
567 | for (i = 0; i < ngroups; i++) { | |
a4912123 TT |
568 | grp = (parent_group + i) % ngroups; |
569 | get_orlov_stats(sb, grp, flex_size, &stats); | |
570 | if (stats.used_dirs >= max_dirs) | |
ac27a0ec | 571 | continue; |
a4912123 | 572 | if (stats.free_inodes < min_inodes) |
ac27a0ec | 573 | continue; |
a4912123 | 574 | if (stats.free_blocks < min_blocks) |
ac27a0ec | 575 | continue; |
a4912123 | 576 | goto found_flex_bg; |
ac27a0ec DK |
577 | } |
578 | ||
579 | fallback: | |
a4912123 TT |
580 | ngroups = sbi->s_groups_count; |
581 | avefreei = freei / ngroups; | |
582 | parent_group = EXT4_I(parent)->i_block_group; | |
ac27a0ec | 583 | for (i = 0; i < ngroups; i++) { |
a4912123 TT |
584 | grp = (parent_group + i) % ngroups; |
585 | desc = ext4_get_group_desc(sb, grp, NULL); | |
560671a0 | 586 | if (desc && ext4_free_inodes_count(sb, desc) && |
a4912123 TT |
587 | ext4_free_inodes_count(sb, desc) >= avefreei) { |
588 | *group = grp; | |
2aa9fc4c | 589 | return 0; |
a4912123 | 590 | } |
ac27a0ec DK |
591 | } |
592 | ||
593 | if (avefreei) { | |
594 | /* | |
595 | * The free-inodes counter is approximate, and for really small | |
596 | * filesystems the above test can fail to find any blockgroups | |
597 | */ | |
598 | avefreei = 0; | |
599 | goto fallback; | |
600 | } | |
601 | ||
602 | return -1; | |
603 | } | |
604 | ||
2aa9fc4c | 605 | static int find_group_other(struct super_block *sb, struct inode *parent, |
a4912123 | 606 | ext4_group_t *group, int mode) |
ac27a0ec | 607 | { |
fd2d4291 AM |
608 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
609 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; | |
617ba13b | 610 | struct ext4_group_desc *desc; |
a4912123 TT |
611 | ext4_group_t i, last; |
612 | int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); | |
613 | ||
614 | /* | |
615 | * Try to place the inode is the same flex group as its | |
616 | * parent. If we can't find space, use the Orlov algorithm to | |
617 | * find another flex group, and store that information in the | |
618 | * parent directory's inode information so that use that flex | |
619 | * group for future allocations. | |
620 | */ | |
621 | if (flex_size > 1) { | |
622 | int retry = 0; | |
623 | ||
624 | try_again: | |
625 | parent_group &= ~(flex_size-1); | |
626 | last = parent_group + flex_size; | |
627 | if (last > ngroups) | |
628 | last = ngroups; | |
629 | for (i = parent_group; i < last; i++) { | |
630 | desc = ext4_get_group_desc(sb, i, NULL); | |
631 | if (desc && ext4_free_inodes_count(sb, desc)) { | |
632 | *group = i; | |
633 | return 0; | |
634 | } | |
635 | } | |
636 | if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { | |
637 | retry = 1; | |
638 | parent_group = EXT4_I(parent)->i_last_alloc_group; | |
639 | goto try_again; | |
640 | } | |
641 | /* | |
642 | * If this didn't work, use the Orlov search algorithm | |
643 | * to find a new flex group; we pass in the mode to | |
644 | * avoid the topdir algorithms. | |
645 | */ | |
646 | *group = parent_group + flex_size; | |
647 | if (*group > ngroups) | |
648 | *group = 0; | |
649 | return find_group_orlov(sb, parent, group, mode); | |
650 | } | |
ac27a0ec DK |
651 | |
652 | /* | |
653 | * Try to place the inode in its parent directory | |
654 | */ | |
2aa9fc4c AM |
655 | *group = parent_group; |
656 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 AK |
657 | if (desc && ext4_free_inodes_count(sb, desc) && |
658 | ext4_free_blks_count(sb, desc)) | |
2aa9fc4c | 659 | return 0; |
ac27a0ec DK |
660 | |
661 | /* | |
662 | * We're going to place this inode in a different blockgroup from its | |
663 | * parent. We want to cause files in a common directory to all land in | |
664 | * the same blockgroup. But we want files which are in a different | |
665 | * directory which shares a blockgroup with our parent to land in a | |
666 | * different blockgroup. | |
667 | * | |
668 | * So add our directory's i_ino into the starting point for the hash. | |
669 | */ | |
2aa9fc4c | 670 | *group = (*group + parent->i_ino) % ngroups; |
ac27a0ec DK |
671 | |
672 | /* | |
673 | * Use a quadratic hash to find a group with a free inode and some free | |
674 | * blocks. | |
675 | */ | |
676 | for (i = 1; i < ngroups; i <<= 1) { | |
2aa9fc4c AM |
677 | *group += i; |
678 | if (*group >= ngroups) | |
679 | *group -= ngroups; | |
680 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 AK |
681 | if (desc && ext4_free_inodes_count(sb, desc) && |
682 | ext4_free_blks_count(sb, desc)) | |
2aa9fc4c | 683 | return 0; |
ac27a0ec DK |
684 | } |
685 | ||
686 | /* | |
687 | * That failed: try linear search for a free inode, even if that group | |
688 | * has no free blocks. | |
689 | */ | |
2aa9fc4c | 690 | *group = parent_group; |
ac27a0ec | 691 | for (i = 0; i < ngroups; i++) { |
2aa9fc4c AM |
692 | if (++*group >= ngroups) |
693 | *group = 0; | |
694 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 | 695 | if (desc && ext4_free_inodes_count(sb, desc)) |
2aa9fc4c | 696 | return 0; |
ac27a0ec DK |
697 | } |
698 | ||
699 | return -1; | |
700 | } | |
701 | ||
39341867 AK |
702 | /* |
703 | * claim the inode from the inode bitmap. If the group | |
704 | * is uninit we need to take the groups's sb_bgl_lock | |
705 | * and clear the uninit flag. The inode bitmap update | |
706 | * and group desc uninit flag clear should be done | |
707 | * after holding sb_bgl_lock so that ext4_read_inode_bitmap | |
708 | * doesn't race with the ext4_claim_inode | |
709 | */ | |
710 | static int ext4_claim_inode(struct super_block *sb, | |
711 | struct buffer_head *inode_bitmap_bh, | |
712 | unsigned long ino, ext4_group_t group, int mode) | |
713 | { | |
714 | int free = 0, retval = 0, count; | |
715 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
716 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL); | |
717 | ||
718 | spin_lock(sb_bgl_lock(sbi, group)); | |
719 | if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) { | |
720 | /* not a free inode */ | |
721 | retval = 1; | |
722 | goto err_ret; | |
723 | } | |
724 | ino++; | |
725 | if ((group == 0 && ino < EXT4_FIRST_INO(sb)) || | |
726 | ino > EXT4_INODES_PER_GROUP(sb)) { | |
727 | spin_unlock(sb_bgl_lock(sbi, group)); | |
728 | ext4_error(sb, __func__, | |
729 | "reserved inode or inode > inodes count - " | |
730 | "block_group = %u, inode=%lu", group, | |
731 | ino + group * EXT4_INODES_PER_GROUP(sb)); | |
732 | return 1; | |
733 | } | |
734 | /* 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 | ||
738 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { | |
739 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); | |
740 | /* When marking the block group with | |
741 | * ~EXT4_BG_INODE_UNINIT we don't want to depend | |
742 | * on the value of bg_itable_unused even though | |
743 | * 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 | ext4_itable_unused_count(sb, gdp); | |
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 | ext4_itable_unused_set(sb, gdp, | |
761 | (EXT4_INODES_PER_GROUP(sb) - ino)); | |
762 | } | |
763 | count = ext4_free_inodes_count(sb, gdp) - 1; | |
764 | ext4_free_inodes_set(sb, gdp, count); | |
765 | if (S_ISDIR(mode)) { | |
766 | count = ext4_used_dirs_count(sb, gdp) + 1; | |
767 | ext4_used_dirs_set(sb, gdp, count); | |
768 | } | |
769 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); | |
770 | err_ret: | |
771 | spin_unlock(sb_bgl_lock(sbi, group)); | |
772 | return retval; | |
773 | } | |
774 | ||
ac27a0ec DK |
775 | /* |
776 | * There are two policies for allocating an inode. If the new inode is | |
777 | * a directory, then a forward search is made for a block group with both | |
778 | * free space and a low directory-to-inode ratio; if that fails, then of | |
779 | * the groups with above-average free space, that group with the fewest | |
780 | * directories already is chosen. | |
781 | * | |
782 | * For other inodes, search forward from the parent directory's block | |
783 | * group to find a free inode. | |
784 | */ | |
af5bc92d | 785 | struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode) |
ac27a0ec DK |
786 | { |
787 | struct super_block *sb; | |
3300beda AK |
788 | struct buffer_head *inode_bitmap_bh = NULL; |
789 | struct buffer_head *group_desc_bh; | |
2aa9fc4c | 790 | ext4_group_t group = 0; |
ac27a0ec | 791 | unsigned long ino = 0; |
af5bc92d TT |
792 | struct inode *inode; |
793 | struct ext4_group_desc *gdp = NULL; | |
794 | struct ext4_super_block *es; | |
617ba13b MC |
795 | struct ext4_inode_info *ei; |
796 | struct ext4_sb_info *sbi; | |
39341867 | 797 | int ret2, err = 0; |
ac27a0ec | 798 | struct inode *ret; |
2aa9fc4c AM |
799 | ext4_group_t i; |
800 | int free = 0; | |
2842c3b5 | 801 | static int once = 1; |
772cb7c8 | 802 | ext4_group_t flex_group; |
ac27a0ec DK |
803 | |
804 | /* Cannot create files in a deleted directory */ | |
805 | if (!dir || !dir->i_nlink) | |
806 | return ERR_PTR(-EPERM); | |
807 | ||
808 | sb = dir->i_sb; | |
ba80b101 TT |
809 | trace_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id, |
810 | dir->i_ino, mode); | |
ac27a0ec DK |
811 | inode = new_inode(sb); |
812 | if (!inode) | |
813 | return ERR_PTR(-ENOMEM); | |
617ba13b | 814 | ei = EXT4_I(inode); |
ac27a0ec | 815 | |
617ba13b | 816 | sbi = EXT4_SB(sb); |
ac27a0ec | 817 | es = sbi->s_es; |
772cb7c8 | 818 | |
a4912123 | 819 | if (sbi->s_log_groups_per_flex && test_opt(sb, OLDALLOC)) { |
772cb7c8 | 820 | ret2 = find_group_flex(sb, dir, &group); |
05bf9e83 | 821 | if (ret2 == -1) { |
a4912123 | 822 | ret2 = find_group_other(sb, dir, &group, mode); |
2842c3b5 TT |
823 | if (ret2 == 0 && once) |
824 | once = 0; | |
05bf9e83 TT |
825 | printk(KERN_NOTICE "ext4: find_group_flex " |
826 | "failed, fallback succeeded dir %lu\n", | |
827 | dir->i_ino); | |
828 | } | |
772cb7c8 JS |
829 | goto got_group; |
830 | } | |
831 | ||
ac27a0ec | 832 | if (S_ISDIR(mode)) { |
af5bc92d | 833 | if (test_opt(sb, OLDALLOC)) |
2aa9fc4c | 834 | ret2 = find_group_dir(sb, dir, &group); |
ac27a0ec | 835 | else |
a4912123 | 836 | ret2 = find_group_orlov(sb, dir, &group, mode); |
ac27a0ec | 837 | } else |
a4912123 | 838 | ret2 = find_group_other(sb, dir, &group, mode); |
ac27a0ec | 839 | |
772cb7c8 | 840 | got_group: |
a4912123 | 841 | EXT4_I(dir)->i_last_alloc_group = group; |
ac27a0ec | 842 | err = -ENOSPC; |
2aa9fc4c | 843 | if (ret2 == -1) |
ac27a0ec DK |
844 | goto out; |
845 | ||
846 | for (i = 0; i < sbi->s_groups_count; i++) { | |
847 | err = -EIO; | |
848 | ||
3300beda | 849 | gdp = ext4_get_group_desc(sb, group, &group_desc_bh); |
ac27a0ec DK |
850 | if (!gdp) |
851 | goto fail; | |
852 | ||
3300beda AK |
853 | brelse(inode_bitmap_bh); |
854 | inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); | |
855 | if (!inode_bitmap_bh) | |
ac27a0ec DK |
856 | goto fail; |
857 | ||
858 | ino = 0; | |
859 | ||
860 | repeat_in_this_group: | |
617ba13b | 861 | ino = ext4_find_next_zero_bit((unsigned long *) |
3300beda AK |
862 | inode_bitmap_bh->b_data, |
863 | EXT4_INODES_PER_GROUP(sb), ino); | |
864 | ||
617ba13b | 865 | if (ino < EXT4_INODES_PER_GROUP(sb)) { |
ac27a0ec | 866 | |
3300beda AK |
867 | BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); |
868 | err = ext4_journal_get_write_access(handle, | |
869 | inode_bitmap_bh); | |
ac27a0ec DK |
870 | if (err) |
871 | goto fail; | |
872 | ||
39341867 AK |
873 | BUFFER_TRACE(group_desc_bh, "get_write_access"); |
874 | err = ext4_journal_get_write_access(handle, | |
875 | group_desc_bh); | |
876 | if (err) | |
877 | goto fail; | |
878 | if (!ext4_claim_inode(sb, inode_bitmap_bh, | |
879 | ino, group, mode)) { | |
ac27a0ec | 880 | /* we won it */ |
3300beda | 881 | BUFFER_TRACE(inode_bitmap_bh, |
0390131b FM |
882 | "call ext4_handle_dirty_metadata"); |
883 | err = ext4_handle_dirty_metadata(handle, | |
3300beda AK |
884 | inode, |
885 | inode_bitmap_bh); | |
ac27a0ec DK |
886 | if (err) |
887 | goto fail; | |
39341867 AK |
888 | /* zero bit is inode number 1*/ |
889 | ino++; | |
ac27a0ec DK |
890 | goto got; |
891 | } | |
892 | /* we lost it */ | |
3300beda | 893 | ext4_handle_release_buffer(handle, inode_bitmap_bh); |
39341867 | 894 | ext4_handle_release_buffer(handle, group_desc_bh); |
ac27a0ec | 895 | |
617ba13b | 896 | if (++ino < EXT4_INODES_PER_GROUP(sb)) |
ac27a0ec DK |
897 | goto repeat_in_this_group; |
898 | } | |
899 | ||
900 | /* | |
901 | * This case is possible in concurrent environment. It is very | |
902 | * rare. We cannot repeat the find_group_xxx() call because | |
903 | * that will simply return the same blockgroup, because the | |
904 | * group descriptor metadata has not yet been updated. | |
905 | * So we just go onto the next blockgroup. | |
906 | */ | |
907 | if (++group == sbi->s_groups_count) | |
908 | group = 0; | |
909 | } | |
910 | err = -ENOSPC; | |
911 | goto out; | |
912 | ||
913 | got: | |
717d50e4 AD |
914 | /* We may have to initialize the block bitmap if it isn't already */ |
915 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && | |
916 | gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { | |
3300beda | 917 | struct buffer_head *block_bitmap_bh; |
717d50e4 | 918 | |
3300beda AK |
919 | block_bitmap_bh = ext4_read_block_bitmap(sb, group); |
920 | BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); | |
921 | err = ext4_journal_get_write_access(handle, block_bitmap_bh); | |
717d50e4 | 922 | if (err) { |
3300beda | 923 | brelse(block_bitmap_bh); |
717d50e4 AD |
924 | goto fail; |
925 | } | |
926 | ||
927 | free = 0; | |
928 | spin_lock(sb_bgl_lock(sbi, group)); | |
929 | /* recheck and clear flag under lock if we still need to */ | |
930 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { | |
717d50e4 | 931 | free = ext4_free_blocks_after_init(sb, group, gdp); |
3300beda | 932 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
560671a0 | 933 | ext4_free_blks_set(sb, gdp, free); |
23712a9c FB |
934 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, |
935 | gdp); | |
717d50e4 AD |
936 | } |
937 | spin_unlock(sb_bgl_lock(sbi, group)); | |
938 | ||
939 | /* Don't need to dirty bitmap block if we didn't change it */ | |
940 | if (free) { | |
3300beda | 941 | BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); |
0390131b | 942 | err = ext4_handle_dirty_metadata(handle, |
3300beda | 943 | NULL, block_bitmap_bh); |
717d50e4 AD |
944 | } |
945 | ||
3300beda | 946 | brelse(block_bitmap_bh); |
717d50e4 AD |
947 | if (err) |
948 | goto fail; | |
949 | } | |
3300beda AK |
950 | BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); |
951 | err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); | |
39341867 AK |
952 | if (err) |
953 | goto fail; | |
ac27a0ec DK |
954 | |
955 | percpu_counter_dec(&sbi->s_freeinodes_counter); | |
956 | if (S_ISDIR(mode)) | |
957 | percpu_counter_inc(&sbi->s_dirs_counter); | |
958 | sb->s_dirt = 1; | |
959 | ||
772cb7c8 JS |
960 | if (sbi->s_log_groups_per_flex) { |
961 | flex_group = ext4_flex_group(sbi, group); | |
9f24e420 | 962 | atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes); |
772cb7c8 JS |
963 | } |
964 | ||
4c9c544e | 965 | inode->i_uid = current_fsuid(); |
af5bc92d | 966 | if (test_opt(sb, GRPID)) |
ac27a0ec DK |
967 | inode->i_gid = dir->i_gid; |
968 | else if (dir->i_mode & S_ISGID) { | |
969 | inode->i_gid = dir->i_gid; | |
970 | if (S_ISDIR(mode)) | |
971 | mode |= S_ISGID; | |
972 | } else | |
4c9c544e | 973 | inode->i_gid = current_fsgid(); |
ac27a0ec DK |
974 | inode->i_mode = mode; |
975 | ||
717d50e4 | 976 | inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); |
ac27a0ec DK |
977 | /* This is the optimal IO size (for stat), not the fs block size */ |
978 | inode->i_blocks = 0; | |
ef7f3835 KS |
979 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
980 | ext4_current_time(inode); | |
ac27a0ec DK |
981 | |
982 | memset(ei->i_data, 0, sizeof(ei->i_data)); | |
983 | ei->i_dir_start_lookup = 0; | |
984 | ei->i_disksize = 0; | |
985 | ||
42bf0383 | 986 | /* |
2dc6b0d4 DG |
987 | * Don't inherit extent flag from directory, amongst others. We set |
988 | * extent flag on newly created directory and file only if -o extent | |
989 | * mount option is specified | |
42bf0383 | 990 | */ |
2dc6b0d4 DG |
991 | ei->i_flags = |
992 | ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); | |
ac27a0ec | 993 | ei->i_file_acl = 0; |
ac27a0ec | 994 | ei->i_dtime = 0; |
ac27a0ec | 995 | ei->i_block_group = group; |
a4912123 | 996 | ei->i_last_alloc_group = ~0; |
ac27a0ec | 997 | |
617ba13b | 998 | ext4_set_inode_flags(inode); |
ac27a0ec | 999 | if (IS_DIRSYNC(inode)) |
0390131b | 1000 | ext4_handle_sync(handle); |
6b38e842 AV |
1001 | if (insert_inode_locked(inode) < 0) { |
1002 | err = -EINVAL; | |
1003 | goto fail_drop; | |
1004 | } | |
ac27a0ec DK |
1005 | spin_lock(&sbi->s_next_gen_lock); |
1006 | inode->i_generation = sbi->s_next_generation++; | |
1007 | spin_unlock(&sbi->s_next_gen_lock); | |
1008 | ||
617ba13b | 1009 | ei->i_state = EXT4_STATE_NEW; |
ef7f3835 KS |
1010 | |
1011 | ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; | |
ac27a0ec DK |
1012 | |
1013 | ret = inode; | |
a269eb18 | 1014 | if (vfs_dq_alloc_inode(inode)) { |
ac27a0ec DK |
1015 | err = -EDQUOT; |
1016 | goto fail_drop; | |
1017 | } | |
1018 | ||
617ba13b | 1019 | err = ext4_init_acl(handle, inode, dir); |
ac27a0ec DK |
1020 | if (err) |
1021 | goto fail_free_drop; | |
1022 | ||
af5bc92d | 1023 | err = ext4_init_security(handle, inode, dir); |
ac27a0ec DK |
1024 | if (err) |
1025 | goto fail_free_drop; | |
1026 | ||
83982b6f | 1027 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
e4079a11 | 1028 | /* set extent flag only for directory, file and normal symlink*/ |
e65187e6 | 1029 | if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { |
42bf0383 AK |
1030 | EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; |
1031 | ext4_ext_tree_init(handle, inode); | |
42bf0383 | 1032 | } |
a86c6181 | 1033 | } |
ac27a0ec | 1034 | |
8753e88f AK |
1035 | err = ext4_mark_inode_dirty(handle, inode); |
1036 | if (err) { | |
1037 | ext4_std_error(sb, err); | |
1038 | goto fail_free_drop; | |
1039 | } | |
1040 | ||
617ba13b | 1041 | ext4_debug("allocating inode %lu\n", inode->i_ino); |
ba80b101 TT |
1042 | trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d", |
1043 | sb->s_id, inode->i_ino, dir->i_ino, mode); | |
ac27a0ec DK |
1044 | goto really_out; |
1045 | fail: | |
617ba13b | 1046 | ext4_std_error(sb, err); |
ac27a0ec DK |
1047 | out: |
1048 | iput(inode); | |
1049 | ret = ERR_PTR(err); | |
1050 | really_out: | |
3300beda | 1051 | brelse(inode_bitmap_bh); |
ac27a0ec DK |
1052 | return ret; |
1053 | ||
1054 | fail_free_drop: | |
a269eb18 | 1055 | vfs_dq_free_inode(inode); |
ac27a0ec DK |
1056 | |
1057 | fail_drop: | |
a269eb18 | 1058 | vfs_dq_drop(inode); |
ac27a0ec DK |
1059 | inode->i_flags |= S_NOQUOTA; |
1060 | inode->i_nlink = 0; | |
6b38e842 | 1061 | unlock_new_inode(inode); |
ac27a0ec | 1062 | iput(inode); |
3300beda | 1063 | brelse(inode_bitmap_bh); |
ac27a0ec DK |
1064 | return ERR_PTR(err); |
1065 | } | |
1066 | ||
1067 | /* Verify that we are loading a valid orphan from disk */ | |
617ba13b | 1068 | struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) |
ac27a0ec | 1069 | { |
617ba13b | 1070 | unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); |
fd2d4291 | 1071 | ext4_group_t block_group; |
ac27a0ec | 1072 | int bit; |
1d1fe1ee | 1073 | struct buffer_head *bitmap_bh; |
ac27a0ec | 1074 | struct inode *inode = NULL; |
1d1fe1ee | 1075 | long err = -EIO; |
ac27a0ec DK |
1076 | |
1077 | /* Error cases - e2fsck has already cleaned up for us */ | |
1078 | if (ino > max_ino) { | |
46e665e9 | 1079 | ext4_warning(sb, __func__, |
ac27a0ec | 1080 | "bad orphan ino %lu! e2fsck was run?", ino); |
1d1fe1ee | 1081 | goto error; |
ac27a0ec DK |
1082 | } |
1083 | ||
617ba13b MC |
1084 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
1085 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); | |
e29d1cde | 1086 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
ac27a0ec | 1087 | if (!bitmap_bh) { |
46e665e9 | 1088 | ext4_warning(sb, __func__, |
ac27a0ec | 1089 | "inode bitmap error for orphan %lu", ino); |
1d1fe1ee | 1090 | goto error; |
ac27a0ec DK |
1091 | } |
1092 | ||
1093 | /* Having the inode bit set should be a 100% indicator that this | |
1094 | * is a valid orphan (no e2fsck run on fs). Orphans also include | |
1095 | * inodes that were being truncated, so we can't check i_nlink==0. | |
1096 | */ | |
1d1fe1ee DH |
1097 | if (!ext4_test_bit(bit, bitmap_bh->b_data)) |
1098 | goto bad_orphan; | |
1099 | ||
1100 | inode = ext4_iget(sb, ino); | |
1101 | if (IS_ERR(inode)) | |
1102 | goto iget_failed; | |
1103 | ||
91ef4caf DG |
1104 | /* |
1105 | * If the orphans has i_nlinks > 0 then it should be able to be | |
1106 | * truncated, otherwise it won't be removed from the orphan list | |
1107 | * during processing and an infinite loop will result. | |
1108 | */ | |
1109 | if (inode->i_nlink && !ext4_can_truncate(inode)) | |
1110 | goto bad_orphan; | |
1111 | ||
1d1fe1ee DH |
1112 | if (NEXT_ORPHAN(inode) > max_ino) |
1113 | goto bad_orphan; | |
1114 | brelse(bitmap_bh); | |
1115 | return inode; | |
1116 | ||
1117 | iget_failed: | |
1118 | err = PTR_ERR(inode); | |
1119 | inode = NULL; | |
1120 | bad_orphan: | |
46e665e9 | 1121 | ext4_warning(sb, __func__, |
1d1fe1ee DH |
1122 | "bad orphan inode %lu! e2fsck was run?", ino); |
1123 | printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", | |
1124 | bit, (unsigned long long)bitmap_bh->b_blocknr, | |
1125 | ext4_test_bit(bit, bitmap_bh->b_data)); | |
1126 | printk(KERN_NOTICE "inode=%p\n", inode); | |
1127 | if (inode) { | |
1128 | printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", | |
1129 | is_bad_inode(inode)); | |
1130 | printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", | |
1131 | NEXT_ORPHAN(inode)); | |
1132 | printk(KERN_NOTICE "max_ino=%lu\n", max_ino); | |
91ef4caf | 1133 | printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink); |
ac27a0ec | 1134 | /* Avoid freeing blocks if we got a bad deleted inode */ |
1d1fe1ee | 1135 | if (inode->i_nlink == 0) |
ac27a0ec DK |
1136 | inode->i_blocks = 0; |
1137 | iput(inode); | |
ac27a0ec | 1138 | } |
ac27a0ec | 1139 | brelse(bitmap_bh); |
1d1fe1ee DH |
1140 | error: |
1141 | return ERR_PTR(err); | |
ac27a0ec DK |
1142 | } |
1143 | ||
af5bc92d | 1144 | unsigned long ext4_count_free_inodes(struct super_block *sb) |
ac27a0ec DK |
1145 | { |
1146 | unsigned long desc_count; | |
617ba13b | 1147 | struct ext4_group_desc *gdp; |
fd2d4291 | 1148 | ext4_group_t i; |
617ba13b MC |
1149 | #ifdef EXT4FS_DEBUG |
1150 | struct ext4_super_block *es; | |
ac27a0ec DK |
1151 | unsigned long bitmap_count, x; |
1152 | struct buffer_head *bitmap_bh = NULL; | |
1153 | ||
617ba13b | 1154 | es = EXT4_SB(sb)->s_es; |
ac27a0ec DK |
1155 | desc_count = 0; |
1156 | bitmap_count = 0; | |
1157 | gdp = NULL; | |
617ba13b | 1158 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
af5bc92d | 1159 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1160 | if (!gdp) |
1161 | continue; | |
560671a0 | 1162 | desc_count += ext4_free_inodes_count(sb, gdp); |
ac27a0ec | 1163 | brelse(bitmap_bh); |
e29d1cde | 1164 | bitmap_bh = ext4_read_inode_bitmap(sb, i); |
ac27a0ec DK |
1165 | if (!bitmap_bh) |
1166 | continue; | |
1167 | ||
617ba13b | 1168 | x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); |
c549a95d | 1169 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
560671a0 | 1170 | i, ext4_free_inodes_count(sb, gdp), x); |
ac27a0ec DK |
1171 | bitmap_count += x; |
1172 | } | |
1173 | brelse(bitmap_bh); | |
4776004f TT |
1174 | printk(KERN_DEBUG "ext4_count_free_inodes: " |
1175 | "stored = %u, computed = %lu, %lu\n", | |
1176 | le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); | |
ac27a0ec DK |
1177 | return desc_count; |
1178 | #else | |
1179 | desc_count = 0; | |
617ba13b | 1180 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
af5bc92d | 1181 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1182 | if (!gdp) |
1183 | continue; | |
560671a0 | 1184 | desc_count += ext4_free_inodes_count(sb, gdp); |
ac27a0ec DK |
1185 | cond_resched(); |
1186 | } | |
1187 | return desc_count; | |
1188 | #endif | |
1189 | } | |
1190 | ||
1191 | /* Called at mount-time, super-block is locked */ | |
af5bc92d | 1192 | unsigned long ext4_count_dirs(struct super_block * sb) |
ac27a0ec DK |
1193 | { |
1194 | unsigned long count = 0; | |
fd2d4291 | 1195 | ext4_group_t i; |
ac27a0ec | 1196 | |
617ba13b | 1197 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
af5bc92d | 1198 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1199 | if (!gdp) |
1200 | continue; | |
560671a0 | 1201 | count += ext4_used_dirs_count(sb, gdp); |
ac27a0ec DK |
1202 | } |
1203 | return count; | |
1204 | } |