]>
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 | */ | |
223 | DQUOT_INIT(inode); | |
617ba13b | 224 | ext4_xattr_delete_inode(handle, inode); |
ac27a0ec DK |
225 | DQUOT_FREE_INODE(inode); |
226 | DQUOT_DROP(inode); | |
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 | ||
ac27a0ec DK |
413 | /* |
414 | * Orlov's allocator for directories. | |
415 | * | |
416 | * We always try to spread first-level directories. | |
417 | * | |
418 | * If there are blockgroups with both free inodes and free blocks counts | |
419 | * not worse than average we return one with smallest directory count. | |
420 | * Otherwise we simply return a random group. | |
421 | * | |
422 | * For the rest rules look so: | |
423 | * | |
424 | * It's OK to put directory into a group unless | |
425 | * it has too many directories already (max_dirs) or | |
426 | * it has too few free inodes left (min_inodes) or | |
427 | * it has too few free blocks left (min_blocks) or | |
428 | * it's already running too large debt (max_debt). | |
1cc8dcf5 | 429 | * Parent's group is preferred, if it doesn't satisfy these |
ac27a0ec DK |
430 | * conditions we search cyclically through the rest. If none |
431 | * of the groups look good we just look for a group with more | |
432 | * free inodes than average (starting at parent's group). | |
433 | * | |
434 | * Debt is incremented each time we allocate a directory and decremented | |
435 | * when we allocate an inode, within 0--255. | |
436 | */ | |
437 | ||
438 | #define INODE_COST 64 | |
439 | #define BLOCK_COST 256 | |
440 | ||
2aa9fc4c AM |
441 | static int find_group_orlov(struct super_block *sb, struct inode *parent, |
442 | ext4_group_t *group) | |
ac27a0ec | 443 | { |
fd2d4291 | 444 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
617ba13b MC |
445 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
446 | struct ext4_super_block *es = sbi->s_es; | |
fd2d4291 | 447 | ext4_group_t ngroups = sbi->s_groups_count; |
617ba13b | 448 | int inodes_per_group = EXT4_INODES_PER_GROUP(sb); |
ac27a0ec | 449 | unsigned int freei, avefreei; |
617ba13b MC |
450 | ext4_fsblk_t freeb, avefreeb; |
451 | ext4_fsblk_t blocks_per_dir; | |
ac27a0ec DK |
452 | unsigned int ndirs; |
453 | int max_debt, max_dirs, min_inodes; | |
617ba13b | 454 | ext4_grpblk_t min_blocks; |
2aa9fc4c | 455 | ext4_group_t i; |
617ba13b | 456 | struct ext4_group_desc *desc; |
ac27a0ec DK |
457 | |
458 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); | |
459 | avefreei = freei / ngroups; | |
460 | freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); | |
3a5b2ecd | 461 | avefreeb = freeb; |
f4e5bc24 | 462 | do_div(avefreeb, ngroups); |
ac27a0ec DK |
463 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
464 | ||
465 | if ((parent == sb->s_root->d_inode) || | |
617ba13b | 466 | (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) { |
ac27a0ec | 467 | int best_ndir = inodes_per_group; |
2aa9fc4c AM |
468 | ext4_group_t grp; |
469 | int ret = -1; | |
ac27a0ec | 470 | |
2aa9fc4c AM |
471 | get_random_bytes(&grp, sizeof(grp)); |
472 | parent_group = (unsigned)grp % ngroups; | |
ac27a0ec | 473 | for (i = 0; i < ngroups; i++) { |
2aa9fc4c AM |
474 | grp = (parent_group + i) % ngroups; |
475 | desc = ext4_get_group_desc(sb, grp, NULL); | |
560671a0 | 476 | if (!desc || !ext4_free_inodes_count(sb, desc)) |
ac27a0ec | 477 | continue; |
560671a0 | 478 | if (ext4_used_dirs_count(sb, desc) >= best_ndir) |
ac27a0ec | 479 | continue; |
560671a0 | 480 | if (ext4_free_inodes_count(sb, desc) < avefreei) |
ac27a0ec | 481 | continue; |
560671a0 | 482 | if (ext4_free_blks_count(sb, desc) < avefreeb) |
ac27a0ec | 483 | continue; |
2aa9fc4c AM |
484 | *group = grp; |
485 | ret = 0; | |
560671a0 | 486 | best_ndir = ext4_used_dirs_count(sb, desc); |
ac27a0ec | 487 | } |
2aa9fc4c AM |
488 | if (ret == 0) |
489 | return ret; | |
ac27a0ec DK |
490 | goto fallback; |
491 | } | |
492 | ||
bd81d8ee | 493 | blocks_per_dir = ext4_blocks_count(es) - freeb; |
f4e5bc24 | 494 | do_div(blocks_per_dir, ndirs); |
ac27a0ec DK |
495 | |
496 | max_dirs = ndirs / ngroups + inodes_per_group / 16; | |
497 | min_inodes = avefreei - inodes_per_group / 4; | |
617ba13b | 498 | min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4; |
ac27a0ec | 499 | |
3a5b2ecd | 500 | max_debt = EXT4_BLOCKS_PER_GROUP(sb); |
f4e5bc24 | 501 | max_debt /= max_t(int, blocks_per_dir, BLOCK_COST); |
ac27a0ec DK |
502 | if (max_debt * INODE_COST > inodes_per_group) |
503 | max_debt = inodes_per_group / INODE_COST; | |
504 | if (max_debt > 255) | |
505 | max_debt = 255; | |
506 | if (max_debt == 0) | |
507 | max_debt = 1; | |
508 | ||
509 | for (i = 0; i < ngroups; i++) { | |
2aa9fc4c AM |
510 | *group = (parent_group + i) % ngroups; |
511 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 | 512 | if (!desc || !ext4_free_inodes_count(sb, desc)) |
ac27a0ec | 513 | continue; |
560671a0 | 514 | if (ext4_used_dirs_count(sb, desc) >= max_dirs) |
ac27a0ec | 515 | continue; |
560671a0 | 516 | if (ext4_free_inodes_count(sb, desc) < min_inodes) |
ac27a0ec | 517 | continue; |
560671a0 | 518 | if (ext4_free_blks_count(sb, desc) < min_blocks) |
ac27a0ec | 519 | continue; |
2aa9fc4c | 520 | return 0; |
ac27a0ec DK |
521 | } |
522 | ||
523 | fallback: | |
524 | for (i = 0; i < ngroups; i++) { | |
2aa9fc4c AM |
525 | *group = (parent_group + i) % ngroups; |
526 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 AK |
527 | if (desc && ext4_free_inodes_count(sb, desc) && |
528 | ext4_free_inodes_count(sb, desc) >= avefreei) | |
2aa9fc4c | 529 | return 0; |
ac27a0ec DK |
530 | } |
531 | ||
532 | if (avefreei) { | |
533 | /* | |
534 | * The free-inodes counter is approximate, and for really small | |
535 | * filesystems the above test can fail to find any blockgroups | |
536 | */ | |
537 | avefreei = 0; | |
538 | goto fallback; | |
539 | } | |
540 | ||
541 | return -1; | |
542 | } | |
543 | ||
2aa9fc4c AM |
544 | static int find_group_other(struct super_block *sb, struct inode *parent, |
545 | ext4_group_t *group) | |
ac27a0ec | 546 | { |
fd2d4291 AM |
547 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
548 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; | |
617ba13b | 549 | struct ext4_group_desc *desc; |
2aa9fc4c | 550 | ext4_group_t i; |
ac27a0ec DK |
551 | |
552 | /* | |
553 | * Try to place the inode in its parent directory | |
554 | */ | |
2aa9fc4c AM |
555 | *group = parent_group; |
556 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 AK |
557 | if (desc && ext4_free_inodes_count(sb, desc) && |
558 | ext4_free_blks_count(sb, desc)) | |
2aa9fc4c | 559 | return 0; |
ac27a0ec DK |
560 | |
561 | /* | |
562 | * We're going to place this inode in a different blockgroup from its | |
563 | * parent. We want to cause files in a common directory to all land in | |
564 | * the same blockgroup. But we want files which are in a different | |
565 | * directory which shares a blockgroup with our parent to land in a | |
566 | * different blockgroup. | |
567 | * | |
568 | * So add our directory's i_ino into the starting point for the hash. | |
569 | */ | |
2aa9fc4c | 570 | *group = (*group + parent->i_ino) % ngroups; |
ac27a0ec DK |
571 | |
572 | /* | |
573 | * Use a quadratic hash to find a group with a free inode and some free | |
574 | * blocks. | |
575 | */ | |
576 | for (i = 1; i < ngroups; i <<= 1) { | |
2aa9fc4c AM |
577 | *group += i; |
578 | if (*group >= ngroups) | |
579 | *group -= ngroups; | |
580 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 AK |
581 | if (desc && ext4_free_inodes_count(sb, desc) && |
582 | ext4_free_blks_count(sb, desc)) | |
2aa9fc4c | 583 | return 0; |
ac27a0ec DK |
584 | } |
585 | ||
586 | /* | |
587 | * That failed: try linear search for a free inode, even if that group | |
588 | * has no free blocks. | |
589 | */ | |
2aa9fc4c | 590 | *group = parent_group; |
ac27a0ec | 591 | for (i = 0; i < ngroups; i++) { |
2aa9fc4c AM |
592 | if (++*group >= ngroups) |
593 | *group = 0; | |
594 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 | 595 | if (desc && ext4_free_inodes_count(sb, desc)) |
2aa9fc4c | 596 | return 0; |
ac27a0ec DK |
597 | } |
598 | ||
599 | return -1; | |
600 | } | |
601 | ||
39341867 AK |
602 | /* |
603 | * claim the inode from the inode bitmap. If the group | |
604 | * is uninit we need to take the groups's sb_bgl_lock | |
605 | * and clear the uninit flag. The inode bitmap update | |
606 | * and group desc uninit flag clear should be done | |
607 | * after holding sb_bgl_lock so that ext4_read_inode_bitmap | |
608 | * doesn't race with the ext4_claim_inode | |
609 | */ | |
610 | static int ext4_claim_inode(struct super_block *sb, | |
611 | struct buffer_head *inode_bitmap_bh, | |
612 | unsigned long ino, ext4_group_t group, int mode) | |
613 | { | |
614 | int free = 0, retval = 0, count; | |
615 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
616 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL); | |
617 | ||
618 | spin_lock(sb_bgl_lock(sbi, group)); | |
619 | if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) { | |
620 | /* not a free inode */ | |
621 | retval = 1; | |
622 | goto err_ret; | |
623 | } | |
624 | ino++; | |
625 | if ((group == 0 && ino < EXT4_FIRST_INO(sb)) || | |
626 | ino > EXT4_INODES_PER_GROUP(sb)) { | |
627 | spin_unlock(sb_bgl_lock(sbi, group)); | |
628 | ext4_error(sb, __func__, | |
629 | "reserved inode or inode > inodes count - " | |
630 | "block_group = %u, inode=%lu", group, | |
631 | ino + group * EXT4_INODES_PER_GROUP(sb)); | |
632 | return 1; | |
633 | } | |
634 | /* If we didn't allocate from within the initialized part of the inode | |
635 | * table then we need to initialize up to this inode. */ | |
636 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { | |
637 | ||
638 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { | |
639 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); | |
640 | /* When marking the block group with | |
641 | * ~EXT4_BG_INODE_UNINIT we don't want to depend | |
642 | * on the value of bg_itable_unused even though | |
643 | * mke2fs could have initialized the same for us. | |
644 | * Instead we calculated the value below | |
645 | */ | |
646 | ||
647 | free = 0; | |
648 | } else { | |
649 | free = EXT4_INODES_PER_GROUP(sb) - | |
650 | ext4_itable_unused_count(sb, gdp); | |
651 | } | |
652 | ||
653 | /* | |
654 | * Check the relative inode number against the last used | |
655 | * relative inode number in this group. if it is greater | |
656 | * we need to update the bg_itable_unused count | |
657 | * | |
658 | */ | |
659 | if (ino > free) | |
660 | ext4_itable_unused_set(sb, gdp, | |
661 | (EXT4_INODES_PER_GROUP(sb) - ino)); | |
662 | } | |
663 | count = ext4_free_inodes_count(sb, gdp) - 1; | |
664 | ext4_free_inodes_set(sb, gdp, count); | |
665 | if (S_ISDIR(mode)) { | |
666 | count = ext4_used_dirs_count(sb, gdp) + 1; | |
667 | ext4_used_dirs_set(sb, gdp, count); | |
668 | } | |
669 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); | |
670 | err_ret: | |
671 | spin_unlock(sb_bgl_lock(sbi, group)); | |
672 | return retval; | |
673 | } | |
674 | ||
ac27a0ec DK |
675 | /* |
676 | * There are two policies for allocating an inode. If the new inode is | |
677 | * a directory, then a forward search is made for a block group with both | |
678 | * free space and a low directory-to-inode ratio; if that fails, then of | |
679 | * the groups with above-average free space, that group with the fewest | |
680 | * directories already is chosen. | |
681 | * | |
682 | * For other inodes, search forward from the parent directory's block | |
683 | * group to find a free inode. | |
684 | */ | |
af5bc92d | 685 | struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode) |
ac27a0ec DK |
686 | { |
687 | struct super_block *sb; | |
3300beda AK |
688 | struct buffer_head *inode_bitmap_bh = NULL; |
689 | struct buffer_head *group_desc_bh; | |
2aa9fc4c | 690 | ext4_group_t group = 0; |
ac27a0ec | 691 | unsigned long ino = 0; |
af5bc92d TT |
692 | struct inode *inode; |
693 | struct ext4_group_desc *gdp = NULL; | |
694 | struct ext4_super_block *es; | |
617ba13b MC |
695 | struct ext4_inode_info *ei; |
696 | struct ext4_sb_info *sbi; | |
39341867 | 697 | int ret2, err = 0; |
ac27a0ec | 698 | struct inode *ret; |
2aa9fc4c AM |
699 | ext4_group_t i; |
700 | int free = 0; | |
2842c3b5 | 701 | static int once = 1; |
772cb7c8 | 702 | ext4_group_t flex_group; |
ac27a0ec DK |
703 | |
704 | /* Cannot create files in a deleted directory */ | |
705 | if (!dir || !dir->i_nlink) | |
706 | return ERR_PTR(-EPERM); | |
707 | ||
708 | sb = dir->i_sb; | |
ba80b101 TT |
709 | trace_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id, |
710 | dir->i_ino, mode); | |
ac27a0ec DK |
711 | inode = new_inode(sb); |
712 | if (!inode) | |
713 | return ERR_PTR(-ENOMEM); | |
617ba13b | 714 | ei = EXT4_I(inode); |
ac27a0ec | 715 | |
617ba13b | 716 | sbi = EXT4_SB(sb); |
ac27a0ec | 717 | es = sbi->s_es; |
772cb7c8 JS |
718 | |
719 | if (sbi->s_log_groups_per_flex) { | |
720 | ret2 = find_group_flex(sb, dir, &group); | |
05bf9e83 TT |
721 | if (ret2 == -1) { |
722 | ret2 = find_group_other(sb, dir, &group); | |
2842c3b5 TT |
723 | if (ret2 == 0 && once) |
724 | once = 0; | |
05bf9e83 TT |
725 | printk(KERN_NOTICE "ext4: find_group_flex " |
726 | "failed, fallback succeeded dir %lu\n", | |
727 | dir->i_ino); | |
728 | } | |
772cb7c8 JS |
729 | goto got_group; |
730 | } | |
731 | ||
ac27a0ec | 732 | if (S_ISDIR(mode)) { |
af5bc92d | 733 | if (test_opt(sb, OLDALLOC)) |
2aa9fc4c | 734 | ret2 = find_group_dir(sb, dir, &group); |
ac27a0ec | 735 | else |
2aa9fc4c | 736 | ret2 = find_group_orlov(sb, dir, &group); |
ac27a0ec | 737 | } else |
2aa9fc4c | 738 | ret2 = find_group_other(sb, dir, &group); |
ac27a0ec | 739 | |
772cb7c8 | 740 | got_group: |
ac27a0ec | 741 | err = -ENOSPC; |
2aa9fc4c | 742 | if (ret2 == -1) |
ac27a0ec DK |
743 | goto out; |
744 | ||
745 | for (i = 0; i < sbi->s_groups_count; i++) { | |
746 | err = -EIO; | |
747 | ||
3300beda | 748 | gdp = ext4_get_group_desc(sb, group, &group_desc_bh); |
ac27a0ec DK |
749 | if (!gdp) |
750 | goto fail; | |
751 | ||
3300beda AK |
752 | brelse(inode_bitmap_bh); |
753 | inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); | |
754 | if (!inode_bitmap_bh) | |
ac27a0ec DK |
755 | goto fail; |
756 | ||
757 | ino = 0; | |
758 | ||
759 | repeat_in_this_group: | |
617ba13b | 760 | ino = ext4_find_next_zero_bit((unsigned long *) |
3300beda AK |
761 | inode_bitmap_bh->b_data, |
762 | EXT4_INODES_PER_GROUP(sb), ino); | |
763 | ||
617ba13b | 764 | if (ino < EXT4_INODES_PER_GROUP(sb)) { |
ac27a0ec | 765 | |
3300beda AK |
766 | BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); |
767 | err = ext4_journal_get_write_access(handle, | |
768 | inode_bitmap_bh); | |
ac27a0ec DK |
769 | if (err) |
770 | goto fail; | |
771 | ||
39341867 AK |
772 | BUFFER_TRACE(group_desc_bh, "get_write_access"); |
773 | err = ext4_journal_get_write_access(handle, | |
774 | group_desc_bh); | |
775 | if (err) | |
776 | goto fail; | |
777 | if (!ext4_claim_inode(sb, inode_bitmap_bh, | |
778 | ino, group, mode)) { | |
ac27a0ec | 779 | /* we won it */ |
3300beda | 780 | BUFFER_TRACE(inode_bitmap_bh, |
0390131b FM |
781 | "call ext4_handle_dirty_metadata"); |
782 | err = ext4_handle_dirty_metadata(handle, | |
3300beda AK |
783 | inode, |
784 | inode_bitmap_bh); | |
ac27a0ec DK |
785 | if (err) |
786 | goto fail; | |
39341867 AK |
787 | /* zero bit is inode number 1*/ |
788 | ino++; | |
ac27a0ec DK |
789 | goto got; |
790 | } | |
791 | /* we lost it */ | |
3300beda | 792 | ext4_handle_release_buffer(handle, inode_bitmap_bh); |
39341867 | 793 | ext4_handle_release_buffer(handle, group_desc_bh); |
ac27a0ec | 794 | |
617ba13b | 795 | if (++ino < EXT4_INODES_PER_GROUP(sb)) |
ac27a0ec DK |
796 | goto repeat_in_this_group; |
797 | } | |
798 | ||
799 | /* | |
800 | * This case is possible in concurrent environment. It is very | |
801 | * rare. We cannot repeat the find_group_xxx() call because | |
802 | * that will simply return the same blockgroup, because the | |
803 | * group descriptor metadata has not yet been updated. | |
804 | * So we just go onto the next blockgroup. | |
805 | */ | |
806 | if (++group == sbi->s_groups_count) | |
807 | group = 0; | |
808 | } | |
809 | err = -ENOSPC; | |
810 | goto out; | |
811 | ||
812 | got: | |
717d50e4 AD |
813 | /* We may have to initialize the block bitmap if it isn't already */ |
814 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && | |
815 | gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { | |
3300beda | 816 | struct buffer_head *block_bitmap_bh; |
717d50e4 | 817 | |
3300beda AK |
818 | block_bitmap_bh = ext4_read_block_bitmap(sb, group); |
819 | BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); | |
820 | err = ext4_journal_get_write_access(handle, block_bitmap_bh); | |
717d50e4 | 821 | if (err) { |
3300beda | 822 | brelse(block_bitmap_bh); |
717d50e4 AD |
823 | goto fail; |
824 | } | |
825 | ||
826 | free = 0; | |
827 | spin_lock(sb_bgl_lock(sbi, group)); | |
828 | /* recheck and clear flag under lock if we still need to */ | |
829 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { | |
717d50e4 | 830 | free = ext4_free_blocks_after_init(sb, group, gdp); |
3300beda | 831 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
560671a0 | 832 | ext4_free_blks_set(sb, gdp, free); |
23712a9c FB |
833 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, |
834 | gdp); | |
717d50e4 AD |
835 | } |
836 | spin_unlock(sb_bgl_lock(sbi, group)); | |
837 | ||
838 | /* Don't need to dirty bitmap block if we didn't change it */ | |
839 | if (free) { | |
3300beda | 840 | BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); |
0390131b | 841 | err = ext4_handle_dirty_metadata(handle, |
3300beda | 842 | NULL, block_bitmap_bh); |
717d50e4 AD |
843 | } |
844 | ||
3300beda | 845 | brelse(block_bitmap_bh); |
717d50e4 AD |
846 | if (err) |
847 | goto fail; | |
848 | } | |
3300beda AK |
849 | BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); |
850 | err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); | |
39341867 AK |
851 | if (err) |
852 | goto fail; | |
ac27a0ec DK |
853 | |
854 | percpu_counter_dec(&sbi->s_freeinodes_counter); | |
855 | if (S_ISDIR(mode)) | |
856 | percpu_counter_inc(&sbi->s_dirs_counter); | |
857 | sb->s_dirt = 1; | |
858 | ||
772cb7c8 JS |
859 | if (sbi->s_log_groups_per_flex) { |
860 | flex_group = ext4_flex_group(sbi, group); | |
861 | spin_lock(sb_bgl_lock(sbi, flex_group)); | |
862 | sbi->s_flex_groups[flex_group].free_inodes--; | |
863 | spin_unlock(sb_bgl_lock(sbi, flex_group)); | |
864 | } | |
865 | ||
4c9c544e | 866 | inode->i_uid = current_fsuid(); |
af5bc92d | 867 | if (test_opt(sb, GRPID)) |
ac27a0ec DK |
868 | inode->i_gid = dir->i_gid; |
869 | else if (dir->i_mode & S_ISGID) { | |
870 | inode->i_gid = dir->i_gid; | |
871 | if (S_ISDIR(mode)) | |
872 | mode |= S_ISGID; | |
873 | } else | |
4c9c544e | 874 | inode->i_gid = current_fsgid(); |
ac27a0ec DK |
875 | inode->i_mode = mode; |
876 | ||
717d50e4 | 877 | inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); |
ac27a0ec DK |
878 | /* This is the optimal IO size (for stat), not the fs block size */ |
879 | inode->i_blocks = 0; | |
ef7f3835 KS |
880 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
881 | ext4_current_time(inode); | |
ac27a0ec DK |
882 | |
883 | memset(ei->i_data, 0, sizeof(ei->i_data)); | |
884 | ei->i_dir_start_lookup = 0; | |
885 | ei->i_disksize = 0; | |
886 | ||
42bf0383 AK |
887 | /* |
888 | * Don't inherit extent flag from directory. We set extent flag on | |
889 | * newly created directory and file only if -o extent mount option is | |
890 | * specified | |
891 | */ | |
892 | ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL); | |
ac27a0ec | 893 | if (S_ISLNK(mode)) |
617ba13b | 894 | ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL); |
ac27a0ec DK |
895 | /* dirsync only applies to directories */ |
896 | if (!S_ISDIR(mode)) | |
617ba13b | 897 | ei->i_flags &= ~EXT4_DIRSYNC_FL; |
ac27a0ec | 898 | ei->i_file_acl = 0; |
ac27a0ec | 899 | ei->i_dtime = 0; |
ac27a0ec DK |
900 | ei->i_block_group = group; |
901 | ||
617ba13b | 902 | ext4_set_inode_flags(inode); |
ac27a0ec | 903 | if (IS_DIRSYNC(inode)) |
0390131b | 904 | ext4_handle_sync(handle); |
6b38e842 AV |
905 | if (insert_inode_locked(inode) < 0) { |
906 | err = -EINVAL; | |
907 | goto fail_drop; | |
908 | } | |
ac27a0ec DK |
909 | spin_lock(&sbi->s_next_gen_lock); |
910 | inode->i_generation = sbi->s_next_generation++; | |
911 | spin_unlock(&sbi->s_next_gen_lock); | |
912 | ||
617ba13b | 913 | ei->i_state = EXT4_STATE_NEW; |
ef7f3835 KS |
914 | |
915 | ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; | |
ac27a0ec DK |
916 | |
917 | ret = inode; | |
af5bc92d | 918 | if (DQUOT_ALLOC_INODE(inode)) { |
ac27a0ec DK |
919 | err = -EDQUOT; |
920 | goto fail_drop; | |
921 | } | |
922 | ||
617ba13b | 923 | err = ext4_init_acl(handle, inode, dir); |
ac27a0ec DK |
924 | if (err) |
925 | goto fail_free_drop; | |
926 | ||
af5bc92d | 927 | err = ext4_init_security(handle, inode, dir); |
ac27a0ec DK |
928 | if (err) |
929 | goto fail_free_drop; | |
930 | ||
83982b6f | 931 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
e4079a11 | 932 | /* set extent flag only for directory, file and normal symlink*/ |
e65187e6 | 933 | if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { |
42bf0383 AK |
934 | EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; |
935 | ext4_ext_tree_init(handle, inode); | |
42bf0383 | 936 | } |
a86c6181 | 937 | } |
ac27a0ec | 938 | |
8753e88f AK |
939 | err = ext4_mark_inode_dirty(handle, inode); |
940 | if (err) { | |
941 | ext4_std_error(sb, err); | |
942 | goto fail_free_drop; | |
943 | } | |
944 | ||
617ba13b | 945 | ext4_debug("allocating inode %lu\n", inode->i_ino); |
ba80b101 TT |
946 | trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d", |
947 | sb->s_id, inode->i_ino, dir->i_ino, mode); | |
ac27a0ec DK |
948 | goto really_out; |
949 | fail: | |
617ba13b | 950 | ext4_std_error(sb, err); |
ac27a0ec DK |
951 | out: |
952 | iput(inode); | |
953 | ret = ERR_PTR(err); | |
954 | really_out: | |
3300beda | 955 | brelse(inode_bitmap_bh); |
ac27a0ec DK |
956 | return ret; |
957 | ||
958 | fail_free_drop: | |
959 | DQUOT_FREE_INODE(inode); | |
960 | ||
961 | fail_drop: | |
962 | DQUOT_DROP(inode); | |
963 | inode->i_flags |= S_NOQUOTA; | |
964 | inode->i_nlink = 0; | |
6b38e842 | 965 | unlock_new_inode(inode); |
ac27a0ec | 966 | iput(inode); |
3300beda | 967 | brelse(inode_bitmap_bh); |
ac27a0ec DK |
968 | return ERR_PTR(err); |
969 | } | |
970 | ||
971 | /* Verify that we are loading a valid orphan from disk */ | |
617ba13b | 972 | struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) |
ac27a0ec | 973 | { |
617ba13b | 974 | unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); |
fd2d4291 | 975 | ext4_group_t block_group; |
ac27a0ec | 976 | int bit; |
1d1fe1ee | 977 | struct buffer_head *bitmap_bh; |
ac27a0ec | 978 | struct inode *inode = NULL; |
1d1fe1ee | 979 | long err = -EIO; |
ac27a0ec DK |
980 | |
981 | /* Error cases - e2fsck has already cleaned up for us */ | |
982 | if (ino > max_ino) { | |
46e665e9 | 983 | ext4_warning(sb, __func__, |
ac27a0ec | 984 | "bad orphan ino %lu! e2fsck was run?", ino); |
1d1fe1ee | 985 | goto error; |
ac27a0ec DK |
986 | } |
987 | ||
617ba13b MC |
988 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
989 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); | |
e29d1cde | 990 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
ac27a0ec | 991 | if (!bitmap_bh) { |
46e665e9 | 992 | ext4_warning(sb, __func__, |
ac27a0ec | 993 | "inode bitmap error for orphan %lu", ino); |
1d1fe1ee | 994 | goto error; |
ac27a0ec DK |
995 | } |
996 | ||
997 | /* Having the inode bit set should be a 100% indicator that this | |
998 | * is a valid orphan (no e2fsck run on fs). Orphans also include | |
999 | * inodes that were being truncated, so we can't check i_nlink==0. | |
1000 | */ | |
1d1fe1ee DH |
1001 | if (!ext4_test_bit(bit, bitmap_bh->b_data)) |
1002 | goto bad_orphan; | |
1003 | ||
1004 | inode = ext4_iget(sb, ino); | |
1005 | if (IS_ERR(inode)) | |
1006 | goto iget_failed; | |
1007 | ||
91ef4caf DG |
1008 | /* |
1009 | * If the orphans has i_nlinks > 0 then it should be able to be | |
1010 | * truncated, otherwise it won't be removed from the orphan list | |
1011 | * during processing and an infinite loop will result. | |
1012 | */ | |
1013 | if (inode->i_nlink && !ext4_can_truncate(inode)) | |
1014 | goto bad_orphan; | |
1015 | ||
1d1fe1ee DH |
1016 | if (NEXT_ORPHAN(inode) > max_ino) |
1017 | goto bad_orphan; | |
1018 | brelse(bitmap_bh); | |
1019 | return inode; | |
1020 | ||
1021 | iget_failed: | |
1022 | err = PTR_ERR(inode); | |
1023 | inode = NULL; | |
1024 | bad_orphan: | |
46e665e9 | 1025 | ext4_warning(sb, __func__, |
1d1fe1ee DH |
1026 | "bad orphan inode %lu! e2fsck was run?", ino); |
1027 | printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", | |
1028 | bit, (unsigned long long)bitmap_bh->b_blocknr, | |
1029 | ext4_test_bit(bit, bitmap_bh->b_data)); | |
1030 | printk(KERN_NOTICE "inode=%p\n", inode); | |
1031 | if (inode) { | |
1032 | printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", | |
1033 | is_bad_inode(inode)); | |
1034 | printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", | |
1035 | NEXT_ORPHAN(inode)); | |
1036 | printk(KERN_NOTICE "max_ino=%lu\n", max_ino); | |
91ef4caf | 1037 | printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink); |
ac27a0ec | 1038 | /* Avoid freeing blocks if we got a bad deleted inode */ |
1d1fe1ee | 1039 | if (inode->i_nlink == 0) |
ac27a0ec DK |
1040 | inode->i_blocks = 0; |
1041 | iput(inode); | |
ac27a0ec | 1042 | } |
ac27a0ec | 1043 | brelse(bitmap_bh); |
1d1fe1ee DH |
1044 | error: |
1045 | return ERR_PTR(err); | |
ac27a0ec DK |
1046 | } |
1047 | ||
af5bc92d | 1048 | unsigned long ext4_count_free_inodes(struct super_block *sb) |
ac27a0ec DK |
1049 | { |
1050 | unsigned long desc_count; | |
617ba13b | 1051 | struct ext4_group_desc *gdp; |
fd2d4291 | 1052 | ext4_group_t i; |
617ba13b MC |
1053 | #ifdef EXT4FS_DEBUG |
1054 | struct ext4_super_block *es; | |
ac27a0ec DK |
1055 | unsigned long bitmap_count, x; |
1056 | struct buffer_head *bitmap_bh = NULL; | |
1057 | ||
617ba13b | 1058 | es = EXT4_SB(sb)->s_es; |
ac27a0ec DK |
1059 | desc_count = 0; |
1060 | bitmap_count = 0; | |
1061 | gdp = NULL; | |
617ba13b | 1062 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
af5bc92d | 1063 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1064 | if (!gdp) |
1065 | continue; | |
560671a0 | 1066 | desc_count += ext4_free_inodes_count(sb, gdp); |
ac27a0ec | 1067 | brelse(bitmap_bh); |
e29d1cde | 1068 | bitmap_bh = ext4_read_inode_bitmap(sb, i); |
ac27a0ec DK |
1069 | if (!bitmap_bh) |
1070 | continue; | |
1071 | ||
617ba13b | 1072 | x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); |
c549a95d | 1073 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
560671a0 | 1074 | i, ext4_free_inodes_count(sb, gdp), x); |
ac27a0ec DK |
1075 | bitmap_count += x; |
1076 | } | |
1077 | brelse(bitmap_bh); | |
4776004f TT |
1078 | printk(KERN_DEBUG "ext4_count_free_inodes: " |
1079 | "stored = %u, computed = %lu, %lu\n", | |
1080 | le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); | |
ac27a0ec DK |
1081 | return desc_count; |
1082 | #else | |
1083 | desc_count = 0; | |
617ba13b | 1084 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
af5bc92d | 1085 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1086 | if (!gdp) |
1087 | continue; | |
560671a0 | 1088 | desc_count += ext4_free_inodes_count(sb, gdp); |
ac27a0ec DK |
1089 | cond_resched(); |
1090 | } | |
1091 | return desc_count; | |
1092 | #endif | |
1093 | } | |
1094 | ||
1095 | /* Called at mount-time, super-block is locked */ | |
af5bc92d | 1096 | unsigned long ext4_count_dirs(struct super_block * sb) |
ac27a0ec DK |
1097 | { |
1098 | unsigned long count = 0; | |
fd2d4291 | 1099 | ext4_group_t i; |
ac27a0ec | 1100 | |
617ba13b | 1101 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
af5bc92d | 1102 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1103 | if (!gdp) |
1104 | continue; | |
560671a0 | 1105 | count += ext4_used_dirs_count(sb, gdp); |
ac27a0ec DK |
1106 | } |
1107 | return count; | |
1108 | } |