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