Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
ac27a0ec | 2 | /* |
617ba13b | 3 | * linux/fs/ext4/ialloc.c |
ac27a0ec DK |
4 | * |
5 | * Copyright (C) 1992, 1993, 1994, 1995 | |
6 | * Remy Card (card@masi.ibp.fr) | |
7 | * Laboratoire MASI - Institut Blaise Pascal | |
8 | * Universite Pierre et Marie Curie (Paris VI) | |
9 | * | |
10 | * BSD ufs-inspired inode and directory allocation by | |
11 | * Stephen Tweedie (sct@redhat.com), 1993 | |
12 | * Big-endian to little-endian byte-swapping/bitmaps by | |
13 | * David S. Miller (davem@caip.rutgers.edu), 1995 | |
14 | */ | |
15 | ||
16 | #include <linux/time.h> | |
17 | #include <linux/fs.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> |
5b825c3a IM |
25 | #include <linux/cred.h> |
26 | ||
ac27a0ec | 27 | #include <asm/byteorder.h> |
9bffad1e | 28 | |
3dcf5451 CH |
29 | #include "ext4.h" |
30 | #include "ext4_jbd2.h" | |
ac27a0ec DK |
31 | #include "xattr.h" |
32 | #include "acl.h" | |
33 | ||
9bffad1e TT |
34 | #include <trace/events/ext4.h> |
35 | ||
ac27a0ec DK |
36 | /* |
37 | * ialloc.c contains the inodes allocation and deallocation routines | |
38 | */ | |
39 | ||
40 | /* | |
41 | * The free inodes are managed by bitmaps. A file system contains several | |
42 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap | |
43 | * block for inodes, N blocks for the inode table and data blocks. | |
44 | * | |
45 | * The file system contains group descriptors which are located after the | |
46 | * super block. Each descriptor contains the number of the bitmap block and | |
47 | * the free blocks count in the block. | |
48 | */ | |
49 | ||
717d50e4 AD |
50 | /* |
51 | * To avoid calling the atomic setbit hundreds or thousands of times, we only | |
52 | * need to use it within a single byte (to ensure we get endianness right). | |
53 | * We can use memset for the rest of the bitmap as there are no other users. | |
54 | */ | |
61d08673 | 55 | void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap) |
717d50e4 AD |
56 | { |
57 | int i; | |
58 | ||
59 | if (start_bit >= end_bit) | |
60 | return; | |
61 | ||
62 | ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); | |
63 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) | |
64 | ext4_set_bit(i, bitmap); | |
65 | if (i < end_bit) | |
66 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); | |
67 | } | |
68 | ||
69 | /* Initializes an uninitialized inode bitmap */ | |
9008a58e | 70 | static int ext4_init_inode_bitmap(struct super_block *sb, |
1f109d5a TT |
71 | struct buffer_head *bh, |
72 | ext4_group_t block_group, | |
73 | struct ext4_group_desc *gdp) | |
717d50e4 | 74 | { |
bdfb6ff4 | 75 | struct ext4_group_info *grp; |
e43bb4e6 | 76 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
717d50e4 AD |
77 | J_ASSERT_BH(bh, buffer_locked(bh)); |
78 | ||
79 | /* If checksum is bad mark all blocks and inodes use to prevent | |
80 | * allocation, essentially implementing a per-group read-only flag. */ | |
feb0ab32 | 81 | if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) { |
bdfb6ff4 | 82 | grp = ext4_get_group_info(sb, block_group); |
e43bb4e6 NJ |
83 | if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp)) |
84 | percpu_counter_sub(&sbi->s_freeclusters_counter, | |
85 | grp->bb_free); | |
bdfb6ff4 | 86 | set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state); |
e43bb4e6 NJ |
87 | if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { |
88 | int count; | |
89 | count = ext4_free_inodes_count(sb, gdp); | |
90 | percpu_counter_sub(&sbi->s_freeinodes_counter, | |
91 | count); | |
92 | } | |
bdfb6ff4 | 93 | set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); |
9008a58e | 94 | return -EFSBADCRC; |
717d50e4 AD |
95 | } |
96 | ||
97 | memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); | |
61d08673 | 98 | ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, |
717d50e4 | 99 | bh->b_data); |
41a246d1 DW |
100 | ext4_inode_bitmap_csum_set(sb, block_group, gdp, bh, |
101 | EXT4_INODES_PER_GROUP(sb) / 8); | |
feb0ab32 | 102 | ext4_group_desc_csum_set(sb, block_group, gdp); |
717d50e4 | 103 | |
9008a58e | 104 | return 0; |
717d50e4 | 105 | } |
ac27a0ec | 106 | |
813e5727 TT |
107 | void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate) |
108 | { | |
109 | if (uptodate) { | |
110 | set_buffer_uptodate(bh); | |
111 | set_bitmap_uptodate(bh); | |
112 | } | |
113 | unlock_buffer(bh); | |
114 | put_bh(bh); | |
115 | } | |
116 | ||
9008a58e DW |
117 | static int ext4_validate_inode_bitmap(struct super_block *sb, |
118 | struct ext4_group_desc *desc, | |
119 | ext4_group_t block_group, | |
120 | struct buffer_head *bh) | |
121 | { | |
122 | ext4_fsblk_t blk; | |
123 | struct ext4_group_info *grp = ext4_get_group_info(sb, block_group); | |
124 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
125 | ||
126 | if (buffer_verified(bh)) | |
127 | return 0; | |
128 | if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) | |
129 | return -EFSCORRUPTED; | |
130 | ||
131 | ext4_lock_group(sb, block_group); | |
132 | blk = ext4_inode_bitmap(sb, desc); | |
133 | if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh, | |
134 | EXT4_INODES_PER_GROUP(sb) / 8)) { | |
135 | ext4_unlock_group(sb, block_group); | |
136 | ext4_error(sb, "Corrupt inode bitmap - block_group = %u, " | |
137 | "inode_bitmap = %llu", block_group, blk); | |
138 | grp = ext4_get_group_info(sb, block_group); | |
139 | if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { | |
140 | int count; | |
141 | count = ext4_free_inodes_count(sb, desc); | |
142 | percpu_counter_sub(&sbi->s_freeinodes_counter, | |
143 | count); | |
144 | } | |
145 | set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); | |
146 | return -EFSBADCRC; | |
147 | } | |
148 | set_buffer_verified(bh); | |
149 | ext4_unlock_group(sb, block_group); | |
150 | return 0; | |
151 | } | |
152 | ||
ac27a0ec DK |
153 | /* |
154 | * Read the inode allocation bitmap for a given block_group, reading | |
155 | * into the specified slot in the superblock's bitmap cache. | |
156 | * | |
157 | * Return buffer_head of bitmap on success or NULL. | |
158 | */ | |
159 | static struct buffer_head * | |
e29d1cde | 160 | ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) |
ac27a0ec | 161 | { |
617ba13b | 162 | struct ext4_group_desc *desc; |
ac27a0ec | 163 | struct buffer_head *bh = NULL; |
e29d1cde | 164 | ext4_fsblk_t bitmap_blk; |
9008a58e | 165 | int err; |
ac27a0ec | 166 | |
617ba13b | 167 | desc = ext4_get_group_desc(sb, block_group, NULL); |
ac27a0ec | 168 | if (!desc) |
9008a58e | 169 | return ERR_PTR(-EFSCORRUPTED); |
bfff6873 | 170 | |
e29d1cde ES |
171 | bitmap_blk = ext4_inode_bitmap(sb, desc); |
172 | bh = sb_getblk(sb, bitmap_blk); | |
173 | if (unlikely(!bh)) { | |
12062ddd | 174 | ext4_error(sb, "Cannot read inode bitmap - " |
a9df9a49 | 175 | "block_group = %u, inode_bitmap = %llu", |
e29d1cde | 176 | block_group, bitmap_blk); |
9008a58e | 177 | return ERR_PTR(-EIO); |
e29d1cde | 178 | } |
2ccb5fb9 | 179 | if (bitmap_uptodate(bh)) |
41a246d1 | 180 | goto verify; |
e29d1cde | 181 | |
c806e68f | 182 | lock_buffer(bh); |
2ccb5fb9 AK |
183 | if (bitmap_uptodate(bh)) { |
184 | unlock_buffer(bh); | |
41a246d1 | 185 | goto verify; |
2ccb5fb9 | 186 | } |
bfff6873 | 187 | |
955ce5f5 | 188 | ext4_lock_group(sb, block_group); |
717d50e4 | 189 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
9008a58e | 190 | err = ext4_init_inode_bitmap(sb, bh, block_group, desc); |
2ccb5fb9 | 191 | set_bitmap_uptodate(bh); |
e29d1cde | 192 | set_buffer_uptodate(bh); |
41a246d1 | 193 | set_buffer_verified(bh); |
955ce5f5 | 194 | ext4_unlock_group(sb, block_group); |
3300beda | 195 | unlock_buffer(bh); |
05145bd7 JK |
196 | if (err) { |
197 | ext4_error(sb, "Failed to init inode bitmap for group " | |
198 | "%u: %d", block_group, err); | |
9008a58e | 199 | goto out; |
05145bd7 | 200 | } |
e29d1cde | 201 | return bh; |
717d50e4 | 202 | } |
955ce5f5 | 203 | ext4_unlock_group(sb, block_group); |
bfff6873 | 204 | |
2ccb5fb9 AK |
205 | if (buffer_uptodate(bh)) { |
206 | /* | |
207 | * if not uninit if bh is uptodate, | |
208 | * bitmap is also uptodate | |
209 | */ | |
210 | set_bitmap_uptodate(bh); | |
211 | unlock_buffer(bh); | |
41a246d1 | 212 | goto verify; |
2ccb5fb9 AK |
213 | } |
214 | /* | |
813e5727 | 215 | * submit the buffer_head for reading |
2ccb5fb9 | 216 | */ |
0562e0ba | 217 | trace_ext4_load_inode_bitmap(sb, block_group); |
813e5727 TT |
218 | bh->b_end_io = ext4_end_bitmap_read; |
219 | get_bh(bh); | |
2a222ca9 | 220 | submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh); |
813e5727 TT |
221 | wait_on_buffer(bh); |
222 | if (!buffer_uptodate(bh)) { | |
e29d1cde | 223 | put_bh(bh); |
12062ddd | 224 | ext4_error(sb, "Cannot read inode bitmap - " |
813e5727 TT |
225 | "block_group = %u, inode_bitmap = %llu", |
226 | block_group, bitmap_blk); | |
9008a58e | 227 | return ERR_PTR(-EIO); |
e29d1cde | 228 | } |
41a246d1 DW |
229 | |
230 | verify: | |
9008a58e DW |
231 | err = ext4_validate_inode_bitmap(sb, desc, block_group, bh); |
232 | if (err) | |
233 | goto out; | |
ac27a0ec | 234 | return bh; |
9008a58e DW |
235 | out: |
236 | put_bh(bh); | |
237 | return ERR_PTR(err); | |
ac27a0ec DK |
238 | } |
239 | ||
240 | /* | |
241 | * NOTE! When we get the inode, we're the only people | |
242 | * that have access to it, and as such there are no | |
243 | * race conditions we have to worry about. The inode | |
244 | * is not on the hash-lists, and it cannot be reached | |
245 | * through the filesystem because the directory entry | |
246 | * has been deleted earlier. | |
247 | * | |
248 | * HOWEVER: we must make sure that we get no aliases, | |
249 | * which means that we have to call "clear_inode()" | |
250 | * _before_ we mark the inode not in use in the inode | |
251 | * bitmaps. Otherwise a newly created file might use | |
252 | * the same inode number (not actually the same pointer | |
253 | * though), and then we'd have two inodes sharing the | |
254 | * same inode number and space on the harddisk. | |
255 | */ | |
af5bc92d | 256 | void ext4_free_inode(handle_t *handle, struct inode *inode) |
ac27a0ec | 257 | { |
af5bc92d | 258 | struct super_block *sb = inode->i_sb; |
ac27a0ec DK |
259 | int is_directory; |
260 | unsigned long ino; | |
261 | struct buffer_head *bitmap_bh = NULL; | |
262 | struct buffer_head *bh2; | |
fd2d4291 | 263 | ext4_group_t block_group; |
ac27a0ec | 264 | unsigned long bit; |
af5bc92d TT |
265 | struct ext4_group_desc *gdp; |
266 | struct ext4_super_block *es; | |
617ba13b | 267 | struct ext4_sb_info *sbi; |
7ce9d5d1 | 268 | int fatal = 0, err, count, cleared; |
87a39389 | 269 | struct ext4_group_info *grp; |
ac27a0ec | 270 | |
92b97816 TT |
271 | if (!sb) { |
272 | printk(KERN_ERR "EXT4-fs: %s:%d: inode on " | |
273 | "nonexistent device\n", __func__, __LINE__); | |
ac27a0ec DK |
274 | return; |
275 | } | |
92b97816 TT |
276 | if (atomic_read(&inode->i_count) > 1) { |
277 | ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d", | |
278 | __func__, __LINE__, inode->i_ino, | |
279 | atomic_read(&inode->i_count)); | |
ac27a0ec DK |
280 | return; |
281 | } | |
92b97816 TT |
282 | if (inode->i_nlink) { |
283 | ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n", | |
284 | __func__, __LINE__, inode->i_ino, inode->i_nlink); | |
ac27a0ec DK |
285 | return; |
286 | } | |
617ba13b | 287 | sbi = EXT4_SB(sb); |
ac27a0ec DK |
288 | |
289 | ino = inode->i_ino; | |
af5bc92d | 290 | ext4_debug("freeing inode %lu\n", ino); |
9bffad1e | 291 | trace_ext4_free_inode(inode); |
ac27a0ec DK |
292 | |
293 | /* | |
294 | * Note: we must free any quota before locking the superblock, | |
295 | * as writing the quota to disk may need the lock as well. | |
296 | */ | |
871a2931 | 297 | dquot_initialize(inode); |
63936dda | 298 | dquot_free_inode(inode); |
9f754758 | 299 | dquot_drop(inode); |
ac27a0ec DK |
300 | |
301 | is_directory = S_ISDIR(inode->i_mode); | |
302 | ||
303 | /* Do this BEFORE marking the inode not in use or returning an error */ | |
0930fcc1 | 304 | ext4_clear_inode(inode); |
ac27a0ec | 305 | |
617ba13b MC |
306 | es = EXT4_SB(sb)->s_es; |
307 | if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { | |
12062ddd | 308 | ext4_error(sb, "reserved or nonexistent inode %lu", ino); |
ac27a0ec DK |
309 | goto error_return; |
310 | } | |
617ba13b MC |
311 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
312 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); | |
e29d1cde | 313 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
87a39389 DW |
314 | /* Don't bother if the inode bitmap is corrupt. */ |
315 | grp = ext4_get_group_info(sb, block_group); | |
9008a58e DW |
316 | if (IS_ERR(bitmap_bh)) { |
317 | fatal = PTR_ERR(bitmap_bh); | |
318 | bitmap_bh = NULL; | |
319 | goto error_return; | |
320 | } | |
321 | if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) { | |
322 | fatal = -EFSCORRUPTED; | |
ac27a0ec | 323 | goto error_return; |
9008a58e | 324 | } |
ac27a0ec DK |
325 | |
326 | BUFFER_TRACE(bitmap_bh, "get_write_access"); | |
617ba13b | 327 | fatal = ext4_journal_get_write_access(handle, bitmap_bh); |
ac27a0ec DK |
328 | if (fatal) |
329 | goto error_return; | |
330 | ||
d17413c0 DM |
331 | fatal = -ESRCH; |
332 | gdp = ext4_get_group_desc(sb, block_group, &bh2); | |
333 | if (gdp) { | |
ac27a0ec | 334 | BUFFER_TRACE(bh2, "get_write_access"); |
617ba13b | 335 | fatal = ext4_journal_get_write_access(handle, bh2); |
d17413c0 DM |
336 | } |
337 | ext4_lock_group(sb, block_group); | |
597d508c | 338 | cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data); |
d17413c0 DM |
339 | if (fatal || !cleared) { |
340 | ext4_unlock_group(sb, block_group); | |
341 | goto out; | |
342 | } | |
7d39db14 | 343 | |
d17413c0 DM |
344 | count = ext4_free_inodes_count(sb, gdp) + 1; |
345 | ext4_free_inodes_set(sb, gdp, count); | |
346 | if (is_directory) { | |
347 | count = ext4_used_dirs_count(sb, gdp) - 1; | |
348 | ext4_used_dirs_set(sb, gdp, count); | |
349 | percpu_counter_dec(&sbi->s_dirs_counter); | |
ac27a0ec | 350 | } |
41a246d1 DW |
351 | ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh, |
352 | EXT4_INODES_PER_GROUP(sb) / 8); | |
feb0ab32 | 353 | ext4_group_desc_csum_set(sb, block_group, gdp); |
d17413c0 | 354 | ext4_unlock_group(sb, block_group); |
ac27a0ec | 355 | |
d17413c0 DM |
356 | percpu_counter_inc(&sbi->s_freeinodes_counter); |
357 | if (sbi->s_log_groups_per_flex) { | |
358 | ext4_group_t f = ext4_flex_group(sbi, block_group); | |
9f24e420 | 359 | |
d17413c0 DM |
360 | atomic_inc(&sbi->s_flex_groups[f].free_inodes); |
361 | if (is_directory) | |
362 | atomic_dec(&sbi->s_flex_groups[f].used_dirs); | |
ac27a0ec | 363 | } |
d17413c0 DM |
364 | BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); |
365 | fatal = ext4_handle_dirty_metadata(handle, NULL, bh2); | |
366 | out: | |
367 | if (cleared) { | |
368 | BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); | |
369 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); | |
370 | if (!fatal) | |
371 | fatal = err; | |
87a39389 | 372 | } else { |
d17413c0 | 373 | ext4_error(sb, "bit already cleared for inode %lu", ino); |
bf40c926 | 374 | if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { |
e43bb4e6 NJ |
375 | int count; |
376 | count = ext4_free_inodes_count(sb, gdp); | |
377 | percpu_counter_sub(&sbi->s_freeinodes_counter, | |
378 | count); | |
379 | } | |
87a39389 DW |
380 | set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); |
381 | } | |
d17413c0 | 382 | |
ac27a0ec DK |
383 | error_return: |
384 | brelse(bitmap_bh); | |
617ba13b | 385 | ext4_std_error(sb, fatal); |
ac27a0ec DK |
386 | } |
387 | ||
a4912123 | 388 | struct orlov_stats { |
90ba983f | 389 | __u64 free_clusters; |
a4912123 | 390 | __u32 free_inodes; |
a4912123 TT |
391 | __u32 used_dirs; |
392 | }; | |
393 | ||
394 | /* | |
395 | * Helper function for Orlov's allocator; returns critical information | |
396 | * for a particular block group or flex_bg. If flex_size is 1, then g | |
397 | * is a block group number; otherwise it is flex_bg number. | |
398 | */ | |
1f109d5a TT |
399 | static void get_orlov_stats(struct super_block *sb, ext4_group_t g, |
400 | int flex_size, struct orlov_stats *stats) | |
a4912123 TT |
401 | { |
402 | struct ext4_group_desc *desc; | |
7d39db14 | 403 | struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups; |
a4912123 | 404 | |
7d39db14 TT |
405 | if (flex_size > 1) { |
406 | stats->free_inodes = atomic_read(&flex_group[g].free_inodes); | |
90ba983f | 407 | stats->free_clusters = atomic64_read(&flex_group[g].free_clusters); |
7d39db14 TT |
408 | stats->used_dirs = atomic_read(&flex_group[g].used_dirs); |
409 | return; | |
410 | } | |
a4912123 | 411 | |
7d39db14 TT |
412 | desc = ext4_get_group_desc(sb, g, NULL); |
413 | if (desc) { | |
414 | stats->free_inodes = ext4_free_inodes_count(sb, desc); | |
021b65bb | 415 | stats->free_clusters = ext4_free_group_clusters(sb, desc); |
7d39db14 TT |
416 | stats->used_dirs = ext4_used_dirs_count(sb, desc); |
417 | } else { | |
418 | stats->free_inodes = 0; | |
24aaa8ef | 419 | stats->free_clusters = 0; |
7d39db14 | 420 | stats->used_dirs = 0; |
a4912123 TT |
421 | } |
422 | } | |
423 | ||
ac27a0ec DK |
424 | /* |
425 | * Orlov's allocator for directories. | |
426 | * | |
427 | * We always try to spread first-level directories. | |
428 | * | |
429 | * If there are blockgroups with both free inodes and free blocks counts | |
430 | * not worse than average we return one with smallest directory count. | |
431 | * Otherwise we simply return a random group. | |
432 | * | |
433 | * For the rest rules look so: | |
434 | * | |
435 | * It's OK to put directory into a group unless | |
436 | * it has too many directories already (max_dirs) or | |
437 | * it has too few free inodes left (min_inodes) or | |
438 | * it has too few free blocks left (min_blocks) or | |
1cc8dcf5 | 439 | * Parent's group is preferred, if it doesn't satisfy these |
ac27a0ec DK |
440 | * conditions we search cyclically through the rest. If none |
441 | * of the groups look good we just look for a group with more | |
442 | * free inodes than average (starting at parent's group). | |
ac27a0ec DK |
443 | */ |
444 | ||
2aa9fc4c | 445 | static int find_group_orlov(struct super_block *sb, struct inode *parent, |
dcca3fec | 446 | ext4_group_t *group, umode_t mode, |
f157a4aa | 447 | const struct qstr *qstr) |
ac27a0ec | 448 | { |
fd2d4291 | 449 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
617ba13b | 450 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
8df9675f | 451 | ext4_group_t real_ngroups = ext4_get_groups_count(sb); |
617ba13b | 452 | int inodes_per_group = EXT4_INODES_PER_GROUP(sb); |
14c83c9f | 453 | unsigned int freei, avefreei, grp_free; |
24aaa8ef | 454 | ext4_fsblk_t freeb, avefreec; |
ac27a0ec | 455 | unsigned int ndirs; |
a4912123 | 456 | int max_dirs, min_inodes; |
24aaa8ef | 457 | ext4_grpblk_t min_clusters; |
8df9675f | 458 | ext4_group_t i, grp, g, ngroups; |
617ba13b | 459 | struct ext4_group_desc *desc; |
a4912123 TT |
460 | struct orlov_stats stats; |
461 | int flex_size = ext4_flex_bg_size(sbi); | |
f157a4aa | 462 | struct dx_hash_info hinfo; |
a4912123 | 463 | |
8df9675f | 464 | ngroups = real_ngroups; |
a4912123 | 465 | if (flex_size > 1) { |
8df9675f | 466 | ngroups = (real_ngroups + flex_size - 1) >> |
a4912123 TT |
467 | sbi->s_log_groups_per_flex; |
468 | parent_group >>= sbi->s_log_groups_per_flex; | |
469 | } | |
ac27a0ec DK |
470 | |
471 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); | |
472 | avefreei = freei / ngroups; | |
57042651 TT |
473 | freeb = EXT4_C2B(sbi, |
474 | percpu_counter_read_positive(&sbi->s_freeclusters_counter)); | |
24aaa8ef TT |
475 | avefreec = freeb; |
476 | do_div(avefreec, ngroups); | |
ac27a0ec DK |
477 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
478 | ||
a4912123 | 479 | if (S_ISDIR(mode) && |
2b0143b5 | 480 | ((parent == d_inode(sb->s_root)) || |
12e9b892 | 481 | (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) { |
ac27a0ec | 482 | int best_ndir = inodes_per_group; |
2aa9fc4c | 483 | int ret = -1; |
ac27a0ec | 484 | |
f157a4aa TT |
485 | if (qstr) { |
486 | hinfo.hash_version = DX_HASH_HALF_MD4; | |
487 | hinfo.seed = sbi->s_hash_seed; | |
488 | ext4fs_dirhash(qstr->name, qstr->len, &hinfo); | |
489 | grp = hinfo.hash; | |
490 | } else | |
dd1f723b | 491 | grp = prandom_u32(); |
2aa9fc4c | 492 | parent_group = (unsigned)grp % ngroups; |
ac27a0ec | 493 | for (i = 0; i < ngroups; i++) { |
a4912123 TT |
494 | g = (parent_group + i) % ngroups; |
495 | get_orlov_stats(sb, g, flex_size, &stats); | |
496 | if (!stats.free_inodes) | |
ac27a0ec | 497 | continue; |
a4912123 | 498 | if (stats.used_dirs >= best_ndir) |
ac27a0ec | 499 | continue; |
a4912123 | 500 | if (stats.free_inodes < avefreei) |
ac27a0ec | 501 | continue; |
24aaa8ef | 502 | if (stats.free_clusters < avefreec) |
ac27a0ec | 503 | continue; |
a4912123 | 504 | grp = g; |
2aa9fc4c | 505 | ret = 0; |
a4912123 TT |
506 | best_ndir = stats.used_dirs; |
507 | } | |
508 | if (ret) | |
509 | goto fallback; | |
510 | found_flex_bg: | |
511 | if (flex_size == 1) { | |
512 | *group = grp; | |
513 | return 0; | |
514 | } | |
515 | ||
516 | /* | |
517 | * We pack inodes at the beginning of the flexgroup's | |
518 | * inode tables. Block allocation decisions will do | |
519 | * something similar, although regular files will | |
520 | * start at 2nd block group of the flexgroup. See | |
521 | * ext4_ext_find_goal() and ext4_find_near(). | |
522 | */ | |
523 | grp *= flex_size; | |
524 | for (i = 0; i < flex_size; i++) { | |
8df9675f | 525 | if (grp+i >= real_ngroups) |
a4912123 TT |
526 | break; |
527 | desc = ext4_get_group_desc(sb, grp+i, NULL); | |
528 | if (desc && ext4_free_inodes_count(sb, desc)) { | |
529 | *group = grp+i; | |
530 | return 0; | |
531 | } | |
ac27a0ec | 532 | } |
ac27a0ec DK |
533 | goto fallback; |
534 | } | |
535 | ||
ac27a0ec | 536 | max_dirs = ndirs / ngroups + inodes_per_group / 16; |
a4912123 TT |
537 | min_inodes = avefreei - inodes_per_group*flex_size / 4; |
538 | if (min_inodes < 1) | |
539 | min_inodes = 1; | |
24aaa8ef | 540 | min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4; |
a4912123 TT |
541 | |
542 | /* | |
543 | * Start looking in the flex group where we last allocated an | |
544 | * inode for this parent directory | |
545 | */ | |
546 | if (EXT4_I(parent)->i_last_alloc_group != ~0) { | |
547 | parent_group = EXT4_I(parent)->i_last_alloc_group; | |
548 | if (flex_size > 1) | |
549 | parent_group >>= sbi->s_log_groups_per_flex; | |
550 | } | |
ac27a0ec DK |
551 | |
552 | for (i = 0; i < ngroups; i++) { | |
a4912123 TT |
553 | grp = (parent_group + i) % ngroups; |
554 | get_orlov_stats(sb, grp, flex_size, &stats); | |
555 | if (stats.used_dirs >= max_dirs) | |
ac27a0ec | 556 | continue; |
a4912123 | 557 | if (stats.free_inodes < min_inodes) |
ac27a0ec | 558 | continue; |
24aaa8ef | 559 | if (stats.free_clusters < min_clusters) |
ac27a0ec | 560 | continue; |
a4912123 | 561 | goto found_flex_bg; |
ac27a0ec DK |
562 | } |
563 | ||
564 | fallback: | |
8df9675f | 565 | ngroups = real_ngroups; |
a4912123 | 566 | avefreei = freei / ngroups; |
b5451f7b | 567 | fallback_retry: |
a4912123 | 568 | parent_group = EXT4_I(parent)->i_block_group; |
ac27a0ec | 569 | for (i = 0; i < ngroups; i++) { |
a4912123 TT |
570 | grp = (parent_group + i) % ngroups; |
571 | desc = ext4_get_group_desc(sb, grp, NULL); | |
bb3d132a DC |
572 | if (desc) { |
573 | grp_free = ext4_free_inodes_count(sb, desc); | |
574 | if (grp_free && grp_free >= avefreei) { | |
575 | *group = grp; | |
576 | return 0; | |
577 | } | |
a4912123 | 578 | } |
ac27a0ec DK |
579 | } |
580 | ||
581 | if (avefreei) { | |
582 | /* | |
583 | * The free-inodes counter is approximate, and for really small | |
584 | * filesystems the above test can fail to find any blockgroups | |
585 | */ | |
586 | avefreei = 0; | |
b5451f7b | 587 | goto fallback_retry; |
ac27a0ec DK |
588 | } |
589 | ||
590 | return -1; | |
591 | } | |
592 | ||
2aa9fc4c | 593 | static int find_group_other(struct super_block *sb, struct inode *parent, |
dcca3fec | 594 | ext4_group_t *group, umode_t mode) |
ac27a0ec | 595 | { |
fd2d4291 | 596 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
8df9675f | 597 | ext4_group_t i, last, ngroups = ext4_get_groups_count(sb); |
617ba13b | 598 | struct ext4_group_desc *desc; |
a4912123 TT |
599 | int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); |
600 | ||
601 | /* | |
602 | * Try to place the inode is the same flex group as its | |
603 | * parent. If we can't find space, use the Orlov algorithm to | |
604 | * find another flex group, and store that information in the | |
605 | * parent directory's inode information so that use that flex | |
606 | * group for future allocations. | |
607 | */ | |
608 | if (flex_size > 1) { | |
609 | int retry = 0; | |
610 | ||
611 | try_again: | |
612 | parent_group &= ~(flex_size-1); | |
613 | last = parent_group + flex_size; | |
614 | if (last > ngroups) | |
615 | last = ngroups; | |
616 | for (i = parent_group; i < last; i++) { | |
617 | desc = ext4_get_group_desc(sb, i, NULL); | |
618 | if (desc && ext4_free_inodes_count(sb, desc)) { | |
619 | *group = i; | |
620 | return 0; | |
621 | } | |
622 | } | |
623 | if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { | |
624 | retry = 1; | |
625 | parent_group = EXT4_I(parent)->i_last_alloc_group; | |
626 | goto try_again; | |
627 | } | |
628 | /* | |
629 | * If this didn't work, use the Orlov search algorithm | |
630 | * to find a new flex group; we pass in the mode to | |
631 | * avoid the topdir algorithms. | |
632 | */ | |
633 | *group = parent_group + flex_size; | |
634 | if (*group > ngroups) | |
635 | *group = 0; | |
7dc57615 | 636 | return find_group_orlov(sb, parent, group, mode, NULL); |
a4912123 | 637 | } |
ac27a0ec DK |
638 | |
639 | /* | |
640 | * Try to place the inode in its parent directory | |
641 | */ | |
2aa9fc4c AM |
642 | *group = parent_group; |
643 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 | 644 | if (desc && ext4_free_inodes_count(sb, desc) && |
021b65bb | 645 | ext4_free_group_clusters(sb, desc)) |
2aa9fc4c | 646 | return 0; |
ac27a0ec DK |
647 | |
648 | /* | |
649 | * We're going to place this inode in a different blockgroup from its | |
650 | * parent. We want to cause files in a common directory to all land in | |
651 | * the same blockgroup. But we want files which are in a different | |
652 | * directory which shares a blockgroup with our parent to land in a | |
653 | * different blockgroup. | |
654 | * | |
655 | * So add our directory's i_ino into the starting point for the hash. | |
656 | */ | |
2aa9fc4c | 657 | *group = (*group + parent->i_ino) % ngroups; |
ac27a0ec DK |
658 | |
659 | /* | |
660 | * Use a quadratic hash to find a group with a free inode and some free | |
661 | * blocks. | |
662 | */ | |
663 | for (i = 1; i < ngroups; i <<= 1) { | |
2aa9fc4c AM |
664 | *group += i; |
665 | if (*group >= ngroups) | |
666 | *group -= ngroups; | |
667 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 | 668 | if (desc && ext4_free_inodes_count(sb, desc) && |
021b65bb | 669 | ext4_free_group_clusters(sb, desc)) |
2aa9fc4c | 670 | return 0; |
ac27a0ec DK |
671 | } |
672 | ||
673 | /* | |
674 | * That failed: try linear search for a free inode, even if that group | |
675 | * has no free blocks. | |
676 | */ | |
2aa9fc4c | 677 | *group = parent_group; |
ac27a0ec | 678 | for (i = 0; i < ngroups; i++) { |
2aa9fc4c AM |
679 | if (++*group >= ngroups) |
680 | *group = 0; | |
681 | desc = ext4_get_group_desc(sb, *group, NULL); | |
560671a0 | 682 | if (desc && ext4_free_inodes_count(sb, desc)) |
2aa9fc4c | 683 | return 0; |
ac27a0ec DK |
684 | } |
685 | ||
686 | return -1; | |
687 | } | |
688 | ||
19883bd9 TT |
689 | /* |
690 | * In no journal mode, if an inode has recently been deleted, we want | |
691 | * to avoid reusing it until we're reasonably sure the inode table | |
692 | * block has been written back to disk. (Yes, these values are | |
693 | * somewhat arbitrary...) | |
694 | */ | |
695 | #define RECENTCY_MIN 5 | |
b5f51573 | 696 | #define RECENTCY_DIRTY 300 |
19883bd9 TT |
697 | |
698 | static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino) | |
699 | { | |
700 | struct ext4_group_desc *gdp; | |
701 | struct ext4_inode *raw_inode; | |
702 | struct buffer_head *bh; | |
b5f51573 AD |
703 | int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; |
704 | int offset, ret = 0; | |
705 | int recentcy = RECENTCY_MIN; | |
706 | u32 dtime, now; | |
19883bd9 TT |
707 | |
708 | gdp = ext4_get_group_desc(sb, group, NULL); | |
709 | if (unlikely(!gdp)) | |
710 | return 0; | |
711 | ||
4f9d956d | 712 | bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) + |
19883bd9 | 713 | (ino / inodes_per_block)); |
4f9d956d | 714 | if (!bh || !buffer_uptodate(bh)) |
19883bd9 TT |
715 | /* |
716 | * If the block is not in the buffer cache, then it | |
717 | * must have been written out. | |
718 | */ | |
719 | goto out; | |
720 | ||
721 | offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb); | |
722 | raw_inode = (struct ext4_inode *) (bh->b_data + offset); | |
b5f51573 AD |
723 | |
724 | /* i_dtime is only 32 bits on disk, but we only care about relative | |
725 | * times in the range of a few minutes (i.e. long enough to sync a | |
726 | * recently-deleted inode to disk), so using the low 32 bits of the | |
727 | * clock (a 68 year range) is enough, see time_before32() */ | |
19883bd9 | 728 | dtime = le32_to_cpu(raw_inode->i_dtime); |
b5f51573 | 729 | now = ktime_get_real_seconds(); |
19883bd9 TT |
730 | if (buffer_dirty(bh)) |
731 | recentcy += RECENTCY_DIRTY; | |
732 | ||
b5f51573 AD |
733 | if (dtime && time_before32(dtime, now) && |
734 | time_before32(now, dtime + recentcy)) | |
19883bd9 TT |
735 | ret = 1; |
736 | out: | |
737 | brelse(bh); | |
738 | return ret; | |
739 | } | |
740 | ||
901ed070 WS |
741 | static int find_inode_bit(struct super_block *sb, ext4_group_t group, |
742 | struct buffer_head *bitmap, unsigned long *ino) | |
743 | { | |
744 | next: | |
745 | *ino = ext4_find_next_zero_bit((unsigned long *) | |
746 | bitmap->b_data, | |
747 | EXT4_INODES_PER_GROUP(sb), *ino); | |
748 | if (*ino >= EXT4_INODES_PER_GROUP(sb)) | |
749 | return 0; | |
750 | ||
751 | if ((EXT4_SB(sb)->s_journal == NULL) && | |
752 | recently_deleted(sb, group, *ino)) { | |
753 | *ino = *ino + 1; | |
754 | if (*ino < EXT4_INODES_PER_GROUP(sb)) | |
755 | goto next; | |
756 | return 0; | |
757 | } | |
758 | ||
759 | return 1; | |
760 | } | |
761 | ||
ac27a0ec DK |
762 | /* |
763 | * There are two policies for allocating an inode. If the new inode is | |
764 | * a directory, then a forward search is made for a block group with both | |
765 | * free space and a low directory-to-inode ratio; if that fails, then of | |
766 | * the groups with above-average free space, that group with the fewest | |
767 | * directories already is chosen. | |
768 | * | |
769 | * For other inodes, search forward from the parent directory's block | |
770 | * group to find a free inode. | |
771 | */ | |
1139575a TT |
772 | struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir, |
773 | umode_t mode, const struct qstr *qstr, | |
1b917ed8 TE |
774 | __u32 goal, uid_t *owner, __u32 i_flags, |
775 | int handle_type, unsigned int line_no, | |
776 | int nblocks) | |
ac27a0ec DK |
777 | { |
778 | struct super_block *sb; | |
3300beda AK |
779 | struct buffer_head *inode_bitmap_bh = NULL; |
780 | struct buffer_head *group_desc_bh; | |
8df9675f | 781 | ext4_group_t ngroups, group = 0; |
ac27a0ec | 782 | unsigned long ino = 0; |
af5bc92d TT |
783 | struct inode *inode; |
784 | struct ext4_group_desc *gdp = NULL; | |
617ba13b MC |
785 | struct ext4_inode_info *ei; |
786 | struct ext4_sb_info *sbi; | |
a7cdadee | 787 | int ret2, err; |
ac27a0ec | 788 | struct inode *ret; |
2aa9fc4c | 789 | ext4_group_t i; |
772cb7c8 | 790 | ext4_group_t flex_group; |
87a39389 | 791 | struct ext4_group_info *grp; |
e709e9df | 792 | int encrypt = 0; |
ac27a0ec DK |
793 | |
794 | /* Cannot create files in a deleted directory */ | |
795 | if (!dir || !dir->i_nlink) | |
796 | return ERR_PTR(-EPERM); | |
797 | ||
af65207c TE |
798 | sb = dir->i_sb; |
799 | sbi = EXT4_SB(sb); | |
800 | ||
801 | if (unlikely(ext4_forced_shutdown(sbi))) | |
0db1ff22 TT |
802 | return ERR_PTR(-EIO); |
803 | ||
af65207c | 804 | if ((ext4_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) && |
ad47f953 TE |
805 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) && |
806 | !(i_flags & EXT4_EA_INODE_FL)) { | |
a7550b30 | 807 | err = fscrypt_get_encryption_info(dir); |
e709e9df TT |
808 | if (err) |
809 | return ERR_PTR(err); | |
a7550b30 | 810 | if (!fscrypt_has_encryption_key(dir)) |
54475f53 | 811 | return ERR_PTR(-ENOKEY); |
e709e9df TT |
812 | encrypt = 1; |
813 | } | |
814 | ||
af65207c TE |
815 | if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) { |
816 | #ifdef CONFIG_EXT4_FS_POSIX_ACL | |
817 | struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT); | |
818 | ||
996fc447 TT |
819 | if (IS_ERR(p)) |
820 | return ERR_CAST(p); | |
af65207c TE |
821 | if (p) { |
822 | int acl_size = p->a_count * sizeof(ext4_acl_entry); | |
823 | ||
824 | nblocks += (S_ISDIR(mode) ? 2 : 1) * | |
825 | __ext4_xattr_set_credits(sb, NULL /* inode */, | |
826 | NULL /* block_bh */, acl_size, | |
827 | true /* is_create */); | |
828 | posix_acl_release(p); | |
829 | } | |
830 | #endif | |
831 | ||
832 | #ifdef CONFIG_SECURITY | |
833 | { | |
834 | int num_security_xattrs = 1; | |
835 | ||
836 | #ifdef CONFIG_INTEGRITY | |
837 | num_security_xattrs++; | |
838 | #endif | |
839 | /* | |
840 | * We assume that security xattrs are never | |
841 | * more than 1k. In practice they are under | |
842 | * 128 bytes. | |
843 | */ | |
844 | nblocks += num_security_xattrs * | |
845 | __ext4_xattr_set_credits(sb, NULL /* inode */, | |
846 | NULL /* block_bh */, 1024, | |
847 | true /* is_create */); | |
848 | } | |
849 | #endif | |
850 | if (encrypt) | |
851 | nblocks += __ext4_xattr_set_credits(sb, | |
852 | NULL /* inode */, NULL /* block_bh */, | |
853 | FSCRYPT_SET_CONTEXT_MAX_SIZE, | |
854 | true /* is_create */); | |
855 | } | |
856 | ||
8df9675f | 857 | ngroups = ext4_get_groups_count(sb); |
9bffad1e | 858 | trace_ext4_request_inode(dir, mode); |
ac27a0ec DK |
859 | inode = new_inode(sb); |
860 | if (!inode) | |
861 | return ERR_PTR(-ENOMEM); | |
617ba13b | 862 | ei = EXT4_I(inode); |
772cb7c8 | 863 | |
eb9cc7e1 | 864 | /* |
b8a07463 | 865 | * Initialize owners and quota early so that we don't have to account |
eb9cc7e1 JK |
866 | * for quota initialization worst case in standard inode creating |
867 | * transaction | |
868 | */ | |
869 | if (owner) { | |
870 | inode->i_mode = mode; | |
871 | i_uid_write(inode, owner[0]); | |
872 | i_gid_write(inode, owner[1]); | |
873 | } else if (test_opt(sb, GRPID)) { | |
874 | inode->i_mode = mode; | |
875 | inode->i_uid = current_fsuid(); | |
876 | inode->i_gid = dir->i_gid; | |
877 | } else | |
878 | inode_init_owner(inode, dir, mode); | |
040cb378 | 879 | |
0b7b7779 | 880 | if (ext4_has_feature_project(sb) && |
040cb378 LX |
881 | ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) |
882 | ei->i_projid = EXT4_I(dir)->i_projid; | |
883 | else | |
884 | ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID); | |
885 | ||
a7cdadee JK |
886 | err = dquot_initialize(inode); |
887 | if (err) | |
888 | goto out; | |
eb9cc7e1 | 889 | |
11013911 AD |
890 | if (!goal) |
891 | goal = sbi->s_inode_goal; | |
892 | ||
e6462869 | 893 | if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) { |
11013911 AD |
894 | group = (goal - 1) / EXT4_INODES_PER_GROUP(sb); |
895 | ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb); | |
896 | ret2 = 0; | |
897 | goto got_group; | |
898 | } | |
899 | ||
4113c4ca LC |
900 | if (S_ISDIR(mode)) |
901 | ret2 = find_group_orlov(sb, dir, &group, mode, qstr); | |
902 | else | |
a4912123 | 903 | ret2 = find_group_other(sb, dir, &group, mode); |
ac27a0ec | 904 | |
772cb7c8 | 905 | got_group: |
a4912123 | 906 | EXT4_I(dir)->i_last_alloc_group = group; |
ac27a0ec | 907 | err = -ENOSPC; |
2aa9fc4c | 908 | if (ret2 == -1) |
ac27a0ec DK |
909 | goto out; |
910 | ||
119c0d44 TT |
911 | /* |
912 | * Normally we will only go through one pass of this loop, | |
913 | * unless we get unlucky and it turns out the group we selected | |
914 | * had its last inode grabbed by someone else. | |
915 | */ | |
11013911 | 916 | for (i = 0; i < ngroups; i++, ino = 0) { |
ac27a0ec DK |
917 | err = -EIO; |
918 | ||
3300beda | 919 | gdp = ext4_get_group_desc(sb, group, &group_desc_bh); |
ac27a0ec | 920 | if (!gdp) |
eb9cc7e1 | 921 | goto out; |
ac27a0ec | 922 | |
f2a09af6 YY |
923 | /* |
924 | * Check free inodes count before loading bitmap. | |
925 | */ | |
2fe435d8 WS |
926 | if (ext4_free_inodes_count(sb, gdp) == 0) |
927 | goto next_group; | |
f2a09af6 | 928 | |
87a39389 DW |
929 | grp = ext4_get_group_info(sb, group); |
930 | /* Skip groups with already-known suspicious inode tables */ | |
2fe435d8 WS |
931 | if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) |
932 | goto next_group; | |
87a39389 | 933 | |
3300beda AK |
934 | brelse(inode_bitmap_bh); |
935 | inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); | |
87a39389 | 936 | /* Skip groups with suspicious inode tables */ |
9008a58e DW |
937 | if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) || |
938 | IS_ERR(inode_bitmap_bh)) { | |
939 | inode_bitmap_bh = NULL; | |
2fe435d8 | 940 | goto next_group; |
87a39389 | 941 | } |
ac27a0ec | 942 | |
ac27a0ec | 943 | repeat_in_this_group: |
901ed070 WS |
944 | ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); |
945 | if (!ret2) | |
a34eb503 | 946 | goto next_group; |
901ed070 WS |
947 | |
948 | if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) { | |
119c0d44 TT |
949 | ext4_error(sb, "reserved inode found cleared - " |
950 | "inode=%lu", ino + 1); | |
2fe435d8 | 951 | goto next_group; |
119c0d44 | 952 | } |
901ed070 | 953 | |
1139575a TT |
954 | if (!handle) { |
955 | BUG_ON(nblocks <= 0); | |
956 | handle = __ext4_journal_start_sb(dir->i_sb, line_no, | |
5fe2fe89 JK |
957 | handle_type, nblocks, |
958 | 0); | |
1139575a TT |
959 | if (IS_ERR(handle)) { |
960 | err = PTR_ERR(handle); | |
eb9cc7e1 JK |
961 | ext4_std_error(sb, err); |
962 | goto out; | |
1139575a TT |
963 | } |
964 | } | |
ffb5387e ES |
965 | BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); |
966 | err = ext4_journal_get_write_access(handle, inode_bitmap_bh); | |
eb9cc7e1 JK |
967 | if (err) { |
968 | ext4_std_error(sb, err); | |
969 | goto out; | |
970 | } | |
119c0d44 TT |
971 | ext4_lock_group(sb, group); |
972 | ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data); | |
901ed070 WS |
973 | if (ret2) { |
974 | /* Someone already took the bit. Repeat the search | |
975 | * with lock held. | |
976 | */ | |
977 | ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); | |
978 | if (ret2) { | |
979 | ext4_set_bit(ino, inode_bitmap_bh->b_data); | |
980 | ret2 = 0; | |
981 | } else { | |
982 | ret2 = 1; /* we didn't grab the inode */ | |
983 | } | |
984 | } | |
119c0d44 TT |
985 | ext4_unlock_group(sb, group); |
986 | ino++; /* the inode bitmap is zero-based */ | |
987 | if (!ret2) | |
988 | goto got; /* we grabbed the inode! */ | |
901ed070 | 989 | |
119c0d44 TT |
990 | if (ino < EXT4_INODES_PER_GROUP(sb)) |
991 | goto repeat_in_this_group; | |
a34eb503 TT |
992 | next_group: |
993 | if (++group == ngroups) | |
994 | group = 0; | |
ac27a0ec DK |
995 | } |
996 | err = -ENOSPC; | |
997 | goto out; | |
998 | ||
999 | got: | |
ffb5387e ES |
1000 | BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata"); |
1001 | err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh); | |
eb9cc7e1 JK |
1002 | if (err) { |
1003 | ext4_std_error(sb, err); | |
1004 | goto out; | |
1005 | } | |
ffb5387e | 1006 | |
61c219f5 TT |
1007 | BUFFER_TRACE(group_desc_bh, "get_write_access"); |
1008 | err = ext4_journal_get_write_access(handle, group_desc_bh); | |
1009 | if (err) { | |
1010 | ext4_std_error(sb, err); | |
1011 | goto out; | |
1012 | } | |
1013 | ||
717d50e4 | 1014 | /* We may have to initialize the block bitmap if it isn't already */ |
feb0ab32 | 1015 | if (ext4_has_group_desc_csum(sb) && |
717d50e4 | 1016 | gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
3300beda | 1017 | struct buffer_head *block_bitmap_bh; |
717d50e4 | 1018 | |
3300beda | 1019 | block_bitmap_bh = ext4_read_block_bitmap(sb, group); |
9008a58e DW |
1020 | if (IS_ERR(block_bitmap_bh)) { |
1021 | err = PTR_ERR(block_bitmap_bh); | |
599a9b77 JK |
1022 | goto out; |
1023 | } | |
3300beda AK |
1024 | BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); |
1025 | err = ext4_journal_get_write_access(handle, block_bitmap_bh); | |
717d50e4 | 1026 | if (err) { |
3300beda | 1027 | brelse(block_bitmap_bh); |
eb9cc7e1 JK |
1028 | ext4_std_error(sb, err); |
1029 | goto out; | |
717d50e4 AD |
1030 | } |
1031 | ||
fd034a84 TT |
1032 | BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); |
1033 | err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh); | |
fd034a84 | 1034 | |
717d50e4 | 1035 | /* recheck and clear flag under lock if we still need to */ |
fd034a84 | 1036 | ext4_lock_group(sb, group); |
717d50e4 | 1037 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
3300beda | 1038 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
021b65bb | 1039 | ext4_free_group_clusters_set(sb, gdp, |
cff1dfd7 | 1040 | ext4_free_clusters_after_init(sb, group, gdp)); |
fa77dcfa | 1041 | ext4_block_bitmap_csum_set(sb, group, gdp, |
79f1ba49 | 1042 | block_bitmap_bh); |
feb0ab32 | 1043 | ext4_group_desc_csum_set(sb, group, gdp); |
717d50e4 | 1044 | } |
955ce5f5 | 1045 | ext4_unlock_group(sb, group); |
aeb1e5d6 | 1046 | brelse(block_bitmap_bh); |
717d50e4 | 1047 | |
eb9cc7e1 JK |
1048 | if (err) { |
1049 | ext4_std_error(sb, err); | |
1050 | goto out; | |
1051 | } | |
717d50e4 | 1052 | } |
119c0d44 | 1053 | |
119c0d44 | 1054 | /* Update the relevant bg descriptor fields */ |
41a246d1 | 1055 | if (ext4_has_group_desc_csum(sb)) { |
119c0d44 TT |
1056 | int free; |
1057 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); | |
1058 | ||
1059 | down_read(&grp->alloc_sem); /* protect vs itable lazyinit */ | |
1060 | ext4_lock_group(sb, group); /* while we modify the bg desc */ | |
1061 | free = EXT4_INODES_PER_GROUP(sb) - | |
1062 | ext4_itable_unused_count(sb, gdp); | |
1063 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { | |
1064 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); | |
1065 | free = 0; | |
1066 | } | |
1067 | /* | |
1068 | * Check the relative inode number against the last used | |
1069 | * relative inode number in this group. if it is greater | |
1070 | * we need to update the bg_itable_unused count | |
1071 | */ | |
1072 | if (ino > free) | |
1073 | ext4_itable_unused_set(sb, gdp, | |
1074 | (EXT4_INODES_PER_GROUP(sb) - ino)); | |
1075 | up_read(&grp->alloc_sem); | |
6f2e9f0e TM |
1076 | } else { |
1077 | ext4_lock_group(sb, group); | |
119c0d44 | 1078 | } |
6f2e9f0e | 1079 | |
119c0d44 TT |
1080 | ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1); |
1081 | if (S_ISDIR(mode)) { | |
1082 | ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1); | |
1083 | if (sbi->s_log_groups_per_flex) { | |
1084 | ext4_group_t f = ext4_flex_group(sbi, group); | |
1085 | ||
1086 | atomic_inc(&sbi->s_flex_groups[f].used_dirs); | |
1087 | } | |
1088 | } | |
41a246d1 DW |
1089 | if (ext4_has_group_desc_csum(sb)) { |
1090 | ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh, | |
1091 | EXT4_INODES_PER_GROUP(sb) / 8); | |
feb0ab32 | 1092 | ext4_group_desc_csum_set(sb, group, gdp); |
119c0d44 | 1093 | } |
6f2e9f0e | 1094 | ext4_unlock_group(sb, group); |
119c0d44 | 1095 | |
3300beda AK |
1096 | BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); |
1097 | err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); | |
eb9cc7e1 JK |
1098 | if (err) { |
1099 | ext4_std_error(sb, err); | |
1100 | goto out; | |
1101 | } | |
ac27a0ec DK |
1102 | |
1103 | percpu_counter_dec(&sbi->s_freeinodes_counter); | |
1104 | if (S_ISDIR(mode)) | |
1105 | percpu_counter_inc(&sbi->s_dirs_counter); | |
ac27a0ec | 1106 | |
772cb7c8 JS |
1107 | if (sbi->s_log_groups_per_flex) { |
1108 | flex_group = ext4_flex_group(sbi, group); | |
9f24e420 | 1109 | atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes); |
772cb7c8 | 1110 | } |
ac27a0ec | 1111 | |
717d50e4 | 1112 | inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); |
ac27a0ec DK |
1113 | /* This is the optimal IO size (for stat), not the fs block size */ |
1114 | inode->i_blocks = 0; | |
ef7f3835 | 1115 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
eeca7ea1 | 1116 | current_time(inode); |
ac27a0ec DK |
1117 | |
1118 | memset(ei->i_data, 0, sizeof(ei->i_data)); | |
1119 | ei->i_dir_start_lookup = 0; | |
1120 | ei->i_disksize = 0; | |
1121 | ||
4af83508 | 1122 | /* Don't inherit extent flag from directory, amongst others. */ |
2dc6b0d4 DG |
1123 | ei->i_flags = |
1124 | ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); | |
1b917ed8 | 1125 | ei->i_flags |= i_flags; |
ac27a0ec | 1126 | ei->i_file_acl = 0; |
ac27a0ec | 1127 | ei->i_dtime = 0; |
ac27a0ec | 1128 | ei->i_block_group = group; |
a4912123 | 1129 | ei->i_last_alloc_group = ~0; |
ac27a0ec | 1130 | |
617ba13b | 1131 | ext4_set_inode_flags(inode); |
ac27a0ec | 1132 | if (IS_DIRSYNC(inode)) |
0390131b | 1133 | ext4_handle_sync(handle); |
6b38e842 | 1134 | if (insert_inode_locked(inode) < 0) { |
acd6ad83 JK |
1135 | /* |
1136 | * Likely a bitmap corruption causing inode to be allocated | |
1137 | * twice. | |
1138 | */ | |
1139 | err = -EIO; | |
eb9cc7e1 JK |
1140 | ext4_error(sb, "failed to insert inode %lu: doubly allocated?", |
1141 | inode->i_ino); | |
1142 | goto out; | |
6b38e842 | 1143 | } |
23253068 | 1144 | inode->i_generation = prandom_u32(); |
ac27a0ec | 1145 | |
814525f4 | 1146 | /* Precompute checksum seed for inode metadata */ |
9aa5d32b | 1147 | if (ext4_has_metadata_csum(sb)) { |
814525f4 | 1148 | __u32 csum; |
814525f4 DW |
1149 | __le32 inum = cpu_to_le32(inode->i_ino); |
1150 | __le32 gen = cpu_to_le32(inode->i_generation); | |
1151 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, | |
1152 | sizeof(inum)); | |
1153 | ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, | |
1154 | sizeof(gen)); | |
1155 | } | |
1156 | ||
353eb83c | 1157 | ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ |
19f5fb7a | 1158 | ext4_set_inode_state(inode, EXT4_STATE_NEW); |
ef7f3835 KS |
1159 | |
1160 | ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; | |
f08225d1 | 1161 | ei->i_inline_off = 0; |
e2b911c5 | 1162 | if (ext4_has_feature_inline_data(sb)) |
f08225d1 | 1163 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
ac27a0ec | 1164 | ret = inode; |
63936dda CH |
1165 | err = dquot_alloc_inode(inode); |
1166 | if (err) | |
ac27a0ec | 1167 | goto fail_drop; |
ac27a0ec | 1168 | |
aa1dca3b EB |
1169 | /* |
1170 | * Since the encryption xattr will always be unique, create it first so | |
1171 | * that it's less likely to end up in an external xattr block and | |
1172 | * prevent its deduplication. | |
1173 | */ | |
1174 | if (encrypt) { | |
1175 | err = fscrypt_inherit_context(dir, inode, handle, true); | |
1176 | if (err) | |
1177 | goto fail_free_drop; | |
1178 | } | |
1179 | ||
1b917ed8 TE |
1180 | if (!(ei->i_flags & EXT4_EA_INODE_FL)) { |
1181 | err = ext4_init_acl(handle, inode, dir); | |
1182 | if (err) | |
1183 | goto fail_free_drop; | |
ac27a0ec | 1184 | |
ad47f953 TE |
1185 | err = ext4_init_security(handle, inode, dir, qstr); |
1186 | if (err) | |
1187 | goto fail_free_drop; | |
1188 | } | |
ac27a0ec | 1189 | |
e2b911c5 | 1190 | if (ext4_has_feature_extents(sb)) { |
e4079a11 | 1191 | /* set extent flag only for directory, file and normal symlink*/ |
e65187e6 | 1192 | if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { |
12e9b892 | 1193 | ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); |
42bf0383 | 1194 | ext4_ext_tree_init(handle, inode); |
42bf0383 | 1195 | } |
a86c6181 | 1196 | } |
ac27a0ec | 1197 | |
688f869c TT |
1198 | if (ext4_handle_valid(handle)) { |
1199 | ei->i_sync_tid = handle->h_transaction->t_tid; | |
1200 | ei->i_datasync_tid = handle->h_transaction->t_tid; | |
1201 | } | |
1202 | ||
8753e88f AK |
1203 | err = ext4_mark_inode_dirty(handle, inode); |
1204 | if (err) { | |
1205 | ext4_std_error(sb, err); | |
1206 | goto fail_free_drop; | |
1207 | } | |
1208 | ||
617ba13b | 1209 | ext4_debug("allocating inode %lu\n", inode->i_ino); |
9bffad1e | 1210 | trace_ext4_allocate_inode(inode, dir, mode); |
3300beda | 1211 | brelse(inode_bitmap_bh); |
ac27a0ec DK |
1212 | return ret; |
1213 | ||
1214 | fail_free_drop: | |
63936dda | 1215 | dquot_free_inode(inode); |
ac27a0ec | 1216 | fail_drop: |
6d6b77f1 | 1217 | clear_nlink(inode); |
6b38e842 | 1218 | unlock_new_inode(inode); |
eb9cc7e1 JK |
1219 | out: |
1220 | dquot_drop(inode); | |
1221 | inode->i_flags |= S_NOQUOTA; | |
ac27a0ec | 1222 | iput(inode); |
3300beda | 1223 | brelse(inode_bitmap_bh); |
ac27a0ec DK |
1224 | return ERR_PTR(err); |
1225 | } | |
1226 | ||
1227 | /* Verify that we are loading a valid orphan from disk */ | |
617ba13b | 1228 | struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) |
ac27a0ec | 1229 | { |
617ba13b | 1230 | unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); |
fd2d4291 | 1231 | ext4_group_t block_group; |
ac27a0ec | 1232 | int bit; |
7827a7f6 | 1233 | struct buffer_head *bitmap_bh = NULL; |
ac27a0ec | 1234 | struct inode *inode = NULL; |
7827a7f6 | 1235 | int err = -EFSCORRUPTED; |
ac27a0ec | 1236 | |
7827a7f6 TT |
1237 | if (ino < EXT4_FIRST_INO(sb) || ino > max_ino) |
1238 | goto bad_orphan; | |
ac27a0ec | 1239 | |
617ba13b MC |
1240 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
1241 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); | |
e29d1cde | 1242 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
9008a58e | 1243 | if (IS_ERR(bitmap_bh)) { |
7827a7f6 TT |
1244 | ext4_error(sb, "inode bitmap error %ld for orphan %lu", |
1245 | ino, PTR_ERR(bitmap_bh)); | |
1246 | return (struct inode *) bitmap_bh; | |
ac27a0ec DK |
1247 | } |
1248 | ||
1249 | /* Having the inode bit set should be a 100% indicator that this | |
1250 | * is a valid orphan (no e2fsck run on fs). Orphans also include | |
1251 | * inodes that were being truncated, so we can't check i_nlink==0. | |
1252 | */ | |
1d1fe1ee DH |
1253 | if (!ext4_test_bit(bit, bitmap_bh->b_data)) |
1254 | goto bad_orphan; | |
1255 | ||
1256 | inode = ext4_iget(sb, ino); | |
7827a7f6 TT |
1257 | if (IS_ERR(inode)) { |
1258 | err = PTR_ERR(inode); | |
1259 | ext4_error(sb, "couldn't read orphan inode %lu (err %d)", | |
1260 | ino, err); | |
1261 | return inode; | |
1262 | } | |
1d1fe1ee | 1263 | |
91ef4caf | 1264 | /* |
c9eb13a9 TT |
1265 | * If the orphans has i_nlinks > 0 then it should be able to |
1266 | * be truncated, otherwise it won't be removed from the orphan | |
1267 | * list during processing and an infinite loop will result. | |
1268 | * Similarly, it must not be a bad inode. | |
91ef4caf | 1269 | */ |
c9eb13a9 TT |
1270 | if ((inode->i_nlink && !ext4_can_truncate(inode)) || |
1271 | is_bad_inode(inode)) | |
91ef4caf DG |
1272 | goto bad_orphan; |
1273 | ||
1d1fe1ee DH |
1274 | if (NEXT_ORPHAN(inode) > max_ino) |
1275 | goto bad_orphan; | |
1276 | brelse(bitmap_bh); | |
1277 | return inode; | |
1278 | ||
1d1fe1ee | 1279 | bad_orphan: |
7827a7f6 TT |
1280 | ext4_error(sb, "bad orphan inode %lu", ino); |
1281 | if (bitmap_bh) | |
1282 | printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n", | |
1283 | bit, (unsigned long long)bitmap_bh->b_blocknr, | |
1284 | ext4_test_bit(bit, bitmap_bh->b_data)); | |
1d1fe1ee | 1285 | if (inode) { |
7827a7f6 | 1286 | printk(KERN_ERR "is_bad_inode(inode)=%d\n", |
1d1fe1ee | 1287 | is_bad_inode(inode)); |
7827a7f6 | 1288 | printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n", |
1d1fe1ee | 1289 | NEXT_ORPHAN(inode)); |
7827a7f6 TT |
1290 | printk(KERN_ERR "max_ino=%lu\n", max_ino); |
1291 | printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink); | |
ac27a0ec | 1292 | /* Avoid freeing blocks if we got a bad deleted inode */ |
1d1fe1ee | 1293 | if (inode->i_nlink == 0) |
ac27a0ec DK |
1294 | inode->i_blocks = 0; |
1295 | iput(inode); | |
ac27a0ec | 1296 | } |
ac27a0ec | 1297 | brelse(bitmap_bh); |
1d1fe1ee | 1298 | return ERR_PTR(err); |
ac27a0ec DK |
1299 | } |
1300 | ||
af5bc92d | 1301 | unsigned long ext4_count_free_inodes(struct super_block *sb) |
ac27a0ec DK |
1302 | { |
1303 | unsigned long desc_count; | |
617ba13b | 1304 | struct ext4_group_desc *gdp; |
8df9675f | 1305 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
617ba13b MC |
1306 | #ifdef EXT4FS_DEBUG |
1307 | struct ext4_super_block *es; | |
ac27a0ec DK |
1308 | unsigned long bitmap_count, x; |
1309 | struct buffer_head *bitmap_bh = NULL; | |
1310 | ||
617ba13b | 1311 | es = EXT4_SB(sb)->s_es; |
ac27a0ec DK |
1312 | desc_count = 0; |
1313 | bitmap_count = 0; | |
1314 | gdp = NULL; | |
8df9675f | 1315 | for (i = 0; i < ngroups; i++) { |
af5bc92d | 1316 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1317 | if (!gdp) |
1318 | continue; | |
560671a0 | 1319 | desc_count += ext4_free_inodes_count(sb, gdp); |
ac27a0ec | 1320 | brelse(bitmap_bh); |
e29d1cde | 1321 | bitmap_bh = ext4_read_inode_bitmap(sb, i); |
9008a58e DW |
1322 | if (IS_ERR(bitmap_bh)) { |
1323 | bitmap_bh = NULL; | |
ac27a0ec | 1324 | continue; |
9008a58e | 1325 | } |
ac27a0ec | 1326 | |
f6fb99ca TT |
1327 | x = ext4_count_free(bitmap_bh->b_data, |
1328 | EXT4_INODES_PER_GROUP(sb) / 8); | |
c549a95d | 1329 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
785b4b3a | 1330 | (unsigned long) i, ext4_free_inodes_count(sb, gdp), x); |
ac27a0ec DK |
1331 | bitmap_count += x; |
1332 | } | |
1333 | brelse(bitmap_bh); | |
4776004f TT |
1334 | printk(KERN_DEBUG "ext4_count_free_inodes: " |
1335 | "stored = %u, computed = %lu, %lu\n", | |
1336 | le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); | |
ac27a0ec DK |
1337 | return desc_count; |
1338 | #else | |
1339 | desc_count = 0; | |
8df9675f | 1340 | for (i = 0; i < ngroups; i++) { |
af5bc92d | 1341 | gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1342 | if (!gdp) |
1343 | continue; | |
560671a0 | 1344 | desc_count += ext4_free_inodes_count(sb, gdp); |
ac27a0ec DK |
1345 | cond_resched(); |
1346 | } | |
1347 | return desc_count; | |
1348 | #endif | |
1349 | } | |
1350 | ||
1351 | /* Called at mount-time, super-block is locked */ | |
af5bc92d | 1352 | unsigned long ext4_count_dirs(struct super_block * sb) |
ac27a0ec DK |
1353 | { |
1354 | unsigned long count = 0; | |
8df9675f | 1355 | ext4_group_t i, ngroups = ext4_get_groups_count(sb); |
ac27a0ec | 1356 | |
8df9675f | 1357 | for (i = 0; i < ngroups; i++) { |
af5bc92d | 1358 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); |
ac27a0ec DK |
1359 | if (!gdp) |
1360 | continue; | |
560671a0 | 1361 | count += ext4_used_dirs_count(sb, gdp); |
ac27a0ec DK |
1362 | } |
1363 | return count; | |
1364 | } | |
bfff6873 LC |
1365 | |
1366 | /* | |
1367 | * Zeroes not yet zeroed inode table - just write zeroes through the whole | |
1368 | * inode table. Must be called without any spinlock held. The only place | |
1369 | * where it is called from on active part of filesystem is ext4lazyinit | |
1370 | * thread, so we do not need any special locks, however we have to prevent | |
1371 | * inode allocation from the current group, so we take alloc_sem lock, to | |
119c0d44 | 1372 | * block ext4_new_inode() until we are finished. |
bfff6873 | 1373 | */ |
e0cbee3e | 1374 | int ext4_init_inode_table(struct super_block *sb, ext4_group_t group, |
bfff6873 LC |
1375 | int barrier) |
1376 | { | |
1377 | struct ext4_group_info *grp = ext4_get_group_info(sb, group); | |
1378 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
1379 | struct ext4_group_desc *gdp = NULL; | |
1380 | struct buffer_head *group_desc_bh; | |
1381 | handle_t *handle; | |
1382 | ext4_fsblk_t blk; | |
1383 | int num, ret = 0, used_blks = 0; | |
bfff6873 LC |
1384 | |
1385 | /* This should not happen, but just to be sure check this */ | |
bc98a42c | 1386 | if (sb_rdonly(sb)) { |
bfff6873 LC |
1387 | ret = 1; |
1388 | goto out; | |
1389 | } | |
1390 | ||
1391 | gdp = ext4_get_group_desc(sb, group, &group_desc_bh); | |
1392 | if (!gdp) | |
1393 | goto out; | |
1394 | ||
1395 | /* | |
1396 | * We do not need to lock this, because we are the only one | |
1397 | * handling this flag. | |
1398 | */ | |
1399 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)) | |
1400 | goto out; | |
1401 | ||
9924a92a | 1402 | handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); |
bfff6873 LC |
1403 | if (IS_ERR(handle)) { |
1404 | ret = PTR_ERR(handle); | |
1405 | goto out; | |
1406 | } | |
1407 | ||
1408 | down_write(&grp->alloc_sem); | |
1409 | /* | |
1410 | * If inode bitmap was already initialized there may be some | |
1411 | * used inodes so we need to skip blocks with used inodes in | |
1412 | * inode table. | |
1413 | */ | |
1414 | if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) | |
1415 | used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) - | |
1416 | ext4_itable_unused_count(sb, gdp)), | |
1417 | sbi->s_inodes_per_block); | |
1418 | ||
857ac889 | 1419 | if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) { |
1084f252 TT |
1420 | ext4_error(sb, "Something is wrong with group %u: " |
1421 | "used itable blocks: %d; " | |
1422 | "itable unused count: %u", | |
857ac889 LC |
1423 | group, used_blks, |
1424 | ext4_itable_unused_count(sb, gdp)); | |
1425 | ret = 1; | |
33853a0d | 1426 | goto err_out; |
857ac889 LC |
1427 | } |
1428 | ||
bfff6873 LC |
1429 | blk = ext4_inode_table(sb, gdp) + used_blks; |
1430 | num = sbi->s_itb_per_group - used_blks; | |
1431 | ||
1432 | BUFFER_TRACE(group_desc_bh, "get_write_access"); | |
1433 | ret = ext4_journal_get_write_access(handle, | |
1434 | group_desc_bh); | |
1435 | if (ret) | |
1436 | goto err_out; | |
1437 | ||
bfff6873 LC |
1438 | /* |
1439 | * Skip zeroout if the inode table is full. But we set the ZEROED | |
1440 | * flag anyway, because obviously, when it is full it does not need | |
1441 | * further zeroing. | |
1442 | */ | |
1443 | if (unlikely(num == 0)) | |
1444 | goto skip_zeroout; | |
1445 | ||
1446 | ext4_debug("going to zero out inode table in group %d\n", | |
1447 | group); | |
a107e5a3 | 1448 | ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS); |
bfff6873 LC |
1449 | if (ret < 0) |
1450 | goto err_out; | |
a107e5a3 TT |
1451 | if (barrier) |
1452 | blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL); | |
bfff6873 LC |
1453 | |
1454 | skip_zeroout: | |
1455 | ext4_lock_group(sb, group); | |
1456 | gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED); | |
feb0ab32 | 1457 | ext4_group_desc_csum_set(sb, group, gdp); |
bfff6873 LC |
1458 | ext4_unlock_group(sb, group); |
1459 | ||
1460 | BUFFER_TRACE(group_desc_bh, | |
1461 | "call ext4_handle_dirty_metadata"); | |
1462 | ret = ext4_handle_dirty_metadata(handle, NULL, | |
1463 | group_desc_bh); | |
1464 | ||
1465 | err_out: | |
1466 | up_write(&grp->alloc_sem); | |
1467 | ext4_journal_stop(handle); | |
1468 | out: | |
1469 | return ret; | |
1470 | } |