]>
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)) { | |
46e665e9 | 77 | ext4_error(sb, __func__, "Checksum bad for group %lu\n", |
717d50e4 AD |
78 | block_group); |
79 | gdp->bg_free_blocks_count = 0; | |
80 | gdp->bg_free_inodes_count = 0; | |
81 | gdp->bg_itable_unused = 0; | |
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); | |
87 | mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb), | |
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 - " | |
114 | "block_group = %lu, inode_bitmap = %llu", | |
115 | block_group, bitmap_blk); | |
116 | return NULL; | |
117 | } | |
118 | if (bh_uptodate_or_lock(bh)) | |
119 | return bh; | |
120 | ||
b5f10eed | 121 | spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
717d50e4 | 122 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
e29d1cde ES |
123 | ext4_init_inode_bitmap(sb, bh, block_group, desc); |
124 | set_buffer_uptodate(bh); | |
125 | unlock_buffer(bh); | |
b5f10eed | 126 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
e29d1cde | 127 | return bh; |
717d50e4 | 128 | } |
b5f10eed | 129 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
e29d1cde ES |
130 | if (bh_submit_read(bh) < 0) { |
131 | put_bh(bh); | |
132 | ext4_error(sb, __func__, | |
ac27a0ec | 133 | "Cannot read inode bitmap - " |
bd81d8ee | 134 | "block_group = %lu, inode_bitmap = %llu", |
e29d1cde ES |
135 | block_group, bitmap_blk); |
136 | return NULL; | |
137 | } | |
ac27a0ec DK |
138 | return bh; |
139 | } | |
140 | ||
141 | /* | |
142 | * NOTE! When we get the inode, we're the only people | |
143 | * that have access to it, and as such there are no | |
144 | * race conditions we have to worry about. The inode | |
145 | * is not on the hash-lists, and it cannot be reached | |
146 | * through the filesystem because the directory entry | |
147 | * has been deleted earlier. | |
148 | * | |
149 | * HOWEVER: we must make sure that we get no aliases, | |
150 | * which means that we have to call "clear_inode()" | |
151 | * _before_ we mark the inode not in use in the inode | |
152 | * bitmaps. Otherwise a newly created file might use | |
153 | * the same inode number (not actually the same pointer | |
154 | * though), and then we'd have two inodes sharing the | |
155 | * same inode number and space on the harddisk. | |
156 | */ | |
617ba13b | 157 | void ext4_free_inode (handle_t *handle, struct inode * inode) |
ac27a0ec DK |
158 | { |
159 | struct super_block * sb = inode->i_sb; | |
160 | int is_directory; | |
161 | unsigned long ino; | |
162 | struct buffer_head *bitmap_bh = NULL; | |
163 | struct buffer_head *bh2; | |
fd2d4291 | 164 | ext4_group_t block_group; |
ac27a0ec | 165 | unsigned long bit; |
617ba13b MC |
166 | struct ext4_group_desc * gdp; |
167 | struct ext4_super_block * es; | |
168 | struct ext4_sb_info *sbi; | |
ac27a0ec | 169 | int fatal = 0, err; |
772cb7c8 | 170 | ext4_group_t flex_group; |
ac27a0ec DK |
171 | |
172 | if (atomic_read(&inode->i_count) > 1) { | |
617ba13b | 173 | printk ("ext4_free_inode: inode has count=%d\n", |
ac27a0ec DK |
174 | atomic_read(&inode->i_count)); |
175 | return; | |
176 | } | |
177 | if (inode->i_nlink) { | |
617ba13b | 178 | printk ("ext4_free_inode: inode has nlink=%d\n", |
ac27a0ec DK |
179 | inode->i_nlink); |
180 | return; | |
181 | } | |
182 | if (!sb) { | |
617ba13b | 183 | printk("ext4_free_inode: inode on nonexistent device\n"); |
ac27a0ec DK |
184 | return; |
185 | } | |
617ba13b | 186 | sbi = EXT4_SB(sb); |
ac27a0ec DK |
187 | |
188 | ino = inode->i_ino; | |
617ba13b | 189 | ext4_debug ("freeing inode %lu\n", ino); |
ac27a0ec DK |
190 | |
191 | /* | |
192 | * Note: we must free any quota before locking the superblock, | |
193 | * as writing the quota to disk may need the lock as well. | |
194 | */ | |
195 | DQUOT_INIT(inode); | |
617ba13b | 196 | ext4_xattr_delete_inode(handle, inode); |
ac27a0ec DK |
197 | DQUOT_FREE_INODE(inode); |
198 | DQUOT_DROP(inode); | |
199 | ||
200 | is_directory = S_ISDIR(inode->i_mode); | |
201 | ||
202 | /* Do this BEFORE marking the inode not in use or returning an error */ | |
203 | clear_inode (inode); | |
204 | ||
617ba13b MC |
205 | es = EXT4_SB(sb)->s_es; |
206 | if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { | |
207 | ext4_error (sb, "ext4_free_inode", | |
ac27a0ec DK |
208 | "reserved or nonexistent inode %lu", ino); |
209 | goto error_return; | |
210 | } | |
617ba13b MC |
211 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
212 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); | |
e29d1cde | 213 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
ac27a0ec DK |
214 | if (!bitmap_bh) |
215 | goto error_return; | |
216 | ||
217 | BUFFER_TRACE(bitmap_bh, "get_write_access"); | |
617ba13b | 218 | fatal = ext4_journal_get_write_access(handle, bitmap_bh); |
ac27a0ec DK |
219 | if (fatal) |
220 | goto error_return; | |
221 | ||
222 | /* Ok, now we can actually update the inode bitmaps.. */ | |
617ba13b | 223 | if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), |
ac27a0ec | 224 | bit, bitmap_bh->b_data)) |
617ba13b | 225 | ext4_error (sb, "ext4_free_inode", |
ac27a0ec DK |
226 | "bit already cleared for inode %lu", ino); |
227 | else { | |
617ba13b | 228 | gdp = ext4_get_group_desc (sb, block_group, &bh2); |
ac27a0ec DK |
229 | |
230 | BUFFER_TRACE(bh2, "get_write_access"); | |
617ba13b | 231 | fatal = ext4_journal_get_write_access(handle, bh2); |
ac27a0ec DK |
232 | if (fatal) goto error_return; |
233 | ||
234 | if (gdp) { | |
235 | spin_lock(sb_bgl_lock(sbi, block_group)); | |
e8546d06 | 236 | le16_add_cpu(&gdp->bg_free_inodes_count, 1); |
ac27a0ec | 237 | if (is_directory) |
e8546d06 | 238 | le16_add_cpu(&gdp->bg_used_dirs_count, -1); |
717d50e4 AD |
239 | gdp->bg_checksum = ext4_group_desc_csum(sbi, |
240 | block_group, gdp); | |
ac27a0ec DK |
241 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
242 | percpu_counter_inc(&sbi->s_freeinodes_counter); | |
243 | if (is_directory) | |
244 | percpu_counter_dec(&sbi->s_dirs_counter); | |
245 | ||
772cb7c8 JS |
246 | if (sbi->s_log_groups_per_flex) { |
247 | flex_group = ext4_flex_group(sbi, block_group); | |
248 | spin_lock(sb_bgl_lock(sbi, flex_group)); | |
249 | sbi->s_flex_groups[flex_group].free_inodes++; | |
250 | spin_unlock(sb_bgl_lock(sbi, flex_group)); | |
251 | } | |
ac27a0ec | 252 | } |
617ba13b MC |
253 | BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata"); |
254 | err = ext4_journal_dirty_metadata(handle, bh2); | |
ac27a0ec DK |
255 | if (!fatal) fatal = err; |
256 | } | |
617ba13b MC |
257 | BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata"); |
258 | err = ext4_journal_dirty_metadata(handle, bitmap_bh); | |
ac27a0ec DK |
259 | if (!fatal) |
260 | fatal = err; | |
261 | sb->s_dirt = 1; | |
262 | error_return: | |
263 | brelse(bitmap_bh); | |
617ba13b | 264 | ext4_std_error(sb, fatal); |
ac27a0ec DK |
265 | } |
266 | ||
267 | /* | |
268 | * There are two policies for allocating an inode. If the new inode is | |
269 | * a directory, then a forward search is made for a block group with both | |
270 | * free space and a low directory-to-inode ratio; if that fails, then of | |
271 | * the groups with above-average free space, that group with the fewest | |
272 | * directories already is chosen. | |
273 | * | |
274 | * For other inodes, search forward from the parent directory\'s block | |
275 | * group to find a free inode. | |
276 | */ | |
2aa9fc4c AM |
277 | static int find_group_dir(struct super_block *sb, struct inode *parent, |
278 | ext4_group_t *best_group) | |
ac27a0ec | 279 | { |
fd2d4291 | 280 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
ac27a0ec | 281 | unsigned int freei, avefreei; |
617ba13b | 282 | struct ext4_group_desc *desc, *best_desc = NULL; |
2aa9fc4c AM |
283 | ext4_group_t group; |
284 | int ret = -1; | |
ac27a0ec | 285 | |
617ba13b | 286 | freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter); |
ac27a0ec DK |
287 | avefreei = freei / ngroups; |
288 | ||
289 | for (group = 0; group < ngroups; group++) { | |
ef2fb679 | 290 | desc = ext4_get_group_desc (sb, group, NULL); |
ac27a0ec DK |
291 | if (!desc || !desc->bg_free_inodes_count) |
292 | continue; | |
293 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) | |
294 | continue; | |
295 | if (!best_desc || | |
296 | (le16_to_cpu(desc->bg_free_blocks_count) > | |
297 | le16_to_cpu(best_desc->bg_free_blocks_count))) { | |
2aa9fc4c | 298 | *best_group = group; |
ac27a0ec | 299 | best_desc = desc; |
2aa9fc4c | 300 | ret = 0; |
ac27a0ec DK |
301 | } |
302 | } | |
2aa9fc4c | 303 | return ret; |
ac27a0ec DK |
304 | } |
305 | ||
772cb7c8 JS |
306 | #define free_block_ratio 10 |
307 | ||
308 | static int find_group_flex(struct super_block *sb, struct inode *parent, | |
309 | ext4_group_t *best_group) | |
310 | { | |
311 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
312 | struct ext4_group_desc *desc; | |
313 | struct buffer_head *bh; | |
314 | struct flex_groups *flex_group = sbi->s_flex_groups; | |
315 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; | |
316 | ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group); | |
317 | ext4_group_t ngroups = sbi->s_groups_count; | |
318 | int flex_size = ext4_flex_bg_size(sbi); | |
319 | ext4_group_t best_flex = parent_fbg_group; | |
320 | int blocks_per_flex = sbi->s_blocks_per_group * flex_size; | |
321 | int flexbg_free_blocks; | |
322 | int flex_freeb_ratio; | |
323 | ext4_group_t n_fbg_groups; | |
324 | ext4_group_t i; | |
325 | ||
326 | n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >> | |
327 | sbi->s_log_groups_per_flex; | |
328 | ||
329 | find_close_to_parent: | |
330 | flexbg_free_blocks = flex_group[best_flex].free_blocks; | |
331 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; | |
332 | if (flex_group[best_flex].free_inodes && | |
333 | flex_freeb_ratio > free_block_ratio) | |
334 | goto found_flexbg; | |
335 | ||
336 | if (best_flex && best_flex == parent_fbg_group) { | |
337 | best_flex--; | |
338 | goto find_close_to_parent; | |
339 | } | |
340 | ||
341 | for (i = 0; i < n_fbg_groups; i++) { | |
342 | if (i == parent_fbg_group || i == parent_fbg_group - 1) | |
343 | continue; | |
344 | ||
345 | flexbg_free_blocks = flex_group[i].free_blocks; | |
346 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; | |
347 | ||
348 | if (flex_freeb_ratio > free_block_ratio && | |
349 | flex_group[i].free_inodes) { | |
350 | best_flex = i; | |
351 | goto found_flexbg; | |
352 | } | |
353 | ||
c001077f | 354 | if (flex_group[best_flex].free_inodes == 0 || |
772cb7c8 JS |
355 | (flex_group[i].free_blocks > |
356 | flex_group[best_flex].free_blocks && | |
357 | flex_group[i].free_inodes)) | |
358 | best_flex = i; | |
359 | } | |
360 | ||
361 | if (!flex_group[best_flex].free_inodes || | |
362 | !flex_group[best_flex].free_blocks) | |
363 | return -1; | |
364 | ||
365 | found_flexbg: | |
366 | for (i = best_flex * flex_size; i < ngroups && | |
367 | i < (best_flex + 1) * flex_size; i++) { | |
368 | desc = ext4_get_group_desc(sb, i, &bh); | |
369 | if (le16_to_cpu(desc->bg_free_inodes_count)) { | |
370 | *best_group = i; | |
371 | goto out; | |
372 | } | |
373 | } | |
374 | ||
375 | return -1; | |
376 | out: | |
377 | return 0; | |
378 | } | |
379 | ||
ac27a0ec DK |
380 | /* |
381 | * Orlov's allocator for directories. | |
382 | * | |
383 | * We always try to spread first-level directories. | |
384 | * | |
385 | * If there are blockgroups with both free inodes and free blocks counts | |
386 | * not worse than average we return one with smallest directory count. | |
387 | * Otherwise we simply return a random group. | |
388 | * | |
389 | * For the rest rules look so: | |
390 | * | |
391 | * It's OK to put directory into a group unless | |
392 | * it has too many directories already (max_dirs) or | |
393 | * it has too few free inodes left (min_inodes) or | |
394 | * it has too few free blocks left (min_blocks) or | |
395 | * it's already running too large debt (max_debt). | |
1cc8dcf5 | 396 | * Parent's group is preferred, if it doesn't satisfy these |
ac27a0ec DK |
397 | * conditions we search cyclically through the rest. If none |
398 | * of the groups look good we just look for a group with more | |
399 | * free inodes than average (starting at parent's group). | |
400 | * | |
401 | * Debt is incremented each time we allocate a directory and decremented | |
402 | * when we allocate an inode, within 0--255. | |
403 | */ | |
404 | ||
405 | #define INODE_COST 64 | |
406 | #define BLOCK_COST 256 | |
407 | ||
2aa9fc4c AM |
408 | static int find_group_orlov(struct super_block *sb, struct inode *parent, |
409 | ext4_group_t *group) | |
ac27a0ec | 410 | { |
fd2d4291 | 411 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
617ba13b MC |
412 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
413 | struct ext4_super_block *es = sbi->s_es; | |
fd2d4291 | 414 | ext4_group_t ngroups = sbi->s_groups_count; |
617ba13b | 415 | int inodes_per_group = EXT4_INODES_PER_GROUP(sb); |
ac27a0ec | 416 | unsigned int freei, avefreei; |
617ba13b MC |
417 | ext4_fsblk_t freeb, avefreeb; |
418 | ext4_fsblk_t blocks_per_dir; | |
ac27a0ec DK |
419 | unsigned int ndirs; |
420 | int max_debt, max_dirs, min_inodes; | |
617ba13b | 421 | ext4_grpblk_t min_blocks; |
2aa9fc4c | 422 | ext4_group_t i; |
617ba13b | 423 | struct ext4_group_desc *desc; |
ac27a0ec DK |
424 | |
425 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); | |
426 | avefreei = freei / ngroups; | |
427 | freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); | |
3a5b2ecd | 428 | avefreeb = freeb; |
f4e5bc24 | 429 | do_div(avefreeb, ngroups); |
ac27a0ec DK |
430 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
431 | ||
432 | if ((parent == sb->s_root->d_inode) || | |
617ba13b | 433 | (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) { |
ac27a0ec | 434 | int best_ndir = inodes_per_group; |
2aa9fc4c AM |
435 | ext4_group_t grp; |
436 | int ret = -1; | |
ac27a0ec | 437 | |
2aa9fc4c AM |
438 | get_random_bytes(&grp, sizeof(grp)); |
439 | parent_group = (unsigned)grp % ngroups; | |
ac27a0ec | 440 | for (i = 0; i < ngroups; i++) { |
2aa9fc4c AM |
441 | grp = (parent_group + i) % ngroups; |
442 | desc = ext4_get_group_desc(sb, grp, NULL); | |
ac27a0ec DK |
443 | if (!desc || !desc->bg_free_inodes_count) |
444 | continue; | |
445 | if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir) | |
446 | continue; | |
447 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) | |
448 | continue; | |
449 | if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb) | |
450 | continue; | |
2aa9fc4c AM |
451 | *group = grp; |
452 | ret = 0; | |
ac27a0ec DK |
453 | best_ndir = le16_to_cpu(desc->bg_used_dirs_count); |
454 | } | |
2aa9fc4c AM |
455 | if (ret == 0) |
456 | return ret; | |
ac27a0ec DK |
457 | goto fallback; |
458 | } | |
459 | ||
bd81d8ee | 460 | blocks_per_dir = ext4_blocks_count(es) - freeb; |
f4e5bc24 | 461 | do_div(blocks_per_dir, ndirs); |
ac27a0ec DK |
462 | |
463 | max_dirs = ndirs / ngroups + inodes_per_group / 16; | |
464 | min_inodes = avefreei - inodes_per_group / 4; | |
617ba13b | 465 | min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4; |
ac27a0ec | 466 | |
3a5b2ecd | 467 | max_debt = EXT4_BLOCKS_PER_GROUP(sb); |
f4e5bc24 | 468 | max_debt /= max_t(int, blocks_per_dir, BLOCK_COST); |
ac27a0ec DK |
469 | if (max_debt * INODE_COST > inodes_per_group) |
470 | max_debt = inodes_per_group / INODE_COST; | |
471 | if (max_debt > 255) | |
472 | max_debt = 255; | |
473 | if (max_debt == 0) | |
474 | max_debt = 1; | |
475 | ||
476 | for (i = 0; i < ngroups; i++) { | |
2aa9fc4c AM |
477 | *group = (parent_group + i) % ngroups; |
478 | desc = ext4_get_group_desc(sb, *group, NULL); | |
ac27a0ec DK |
479 | if (!desc || !desc->bg_free_inodes_count) |
480 | continue; | |
481 | if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs) | |
482 | continue; | |
483 | if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes) | |
484 | continue; | |
485 | if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks) | |
486 | continue; | |
2aa9fc4c | 487 | return 0; |
ac27a0ec DK |
488 | } |
489 | ||
490 | fallback: | |
491 | for (i = 0; i < ngroups; i++) { | |
2aa9fc4c AM |
492 | *group = (parent_group + i) % ngroups; |
493 | desc = ext4_get_group_desc(sb, *group, NULL); | |
494 | if (desc && desc->bg_free_inodes_count && | |
495 | le16_to_cpu(desc->bg_free_inodes_count) >= avefreei) | |
496 | return 0; | |
ac27a0ec DK |
497 | } |
498 | ||
499 | if (avefreei) { | |
500 | /* | |
501 | * The free-inodes counter is approximate, and for really small | |
502 | * filesystems the above test can fail to find any blockgroups | |
503 | */ | |
504 | avefreei = 0; | |
505 | goto fallback; | |
506 | } | |
507 | ||
508 | return -1; | |
509 | } | |
510 | ||
2aa9fc4c AM |
511 | static int find_group_other(struct super_block *sb, struct inode *parent, |
512 | ext4_group_t *group) | |
ac27a0ec | 513 | { |
fd2d4291 AM |
514 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
515 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; | |
617ba13b | 516 | struct ext4_group_desc *desc; |
2aa9fc4c | 517 | ext4_group_t i; |
ac27a0ec DK |
518 | |
519 | /* | |
520 | * Try to place the inode in its parent directory | |
521 | */ | |
2aa9fc4c AM |
522 | *group = parent_group; |
523 | desc = ext4_get_group_desc(sb, *group, NULL); | |
ac27a0ec DK |
524 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
525 | le16_to_cpu(desc->bg_free_blocks_count)) | |
2aa9fc4c | 526 | return 0; |
ac27a0ec DK |
527 | |
528 | /* | |
529 | * We're going to place this inode in a different blockgroup from its | |
530 | * parent. We want to cause files in a common directory to all land in | |
531 | * the same blockgroup. But we want files which are in a different | |
532 | * directory which shares a blockgroup with our parent to land in a | |
533 | * different blockgroup. | |
534 | * | |
535 | * So add our directory's i_ino into the starting point for the hash. | |
536 | */ | |
2aa9fc4c | 537 | *group = (*group + parent->i_ino) % ngroups; |
ac27a0ec DK |
538 | |
539 | /* | |
540 | * Use a quadratic hash to find a group with a free inode and some free | |
541 | * blocks. | |
542 | */ | |
543 | for (i = 1; i < ngroups; i <<= 1) { | |
2aa9fc4c AM |
544 | *group += i; |
545 | if (*group >= ngroups) | |
546 | *group -= ngroups; | |
547 | desc = ext4_get_group_desc(sb, *group, NULL); | |
ac27a0ec DK |
548 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
549 | le16_to_cpu(desc->bg_free_blocks_count)) | |
2aa9fc4c | 550 | return 0; |
ac27a0ec DK |
551 | } |
552 | ||
553 | /* | |
554 | * That failed: try linear search for a free inode, even if that group | |
555 | * has no free blocks. | |
556 | */ | |
2aa9fc4c | 557 | *group = parent_group; |
ac27a0ec | 558 | for (i = 0; i < ngroups; i++) { |
2aa9fc4c AM |
559 | if (++*group >= ngroups) |
560 | *group = 0; | |
561 | desc = ext4_get_group_desc(sb, *group, NULL); | |
ac27a0ec | 562 | if (desc && le16_to_cpu(desc->bg_free_inodes_count)) |
2aa9fc4c | 563 | return 0; |
ac27a0ec DK |
564 | } |
565 | ||
566 | return -1; | |
567 | } | |
568 | ||
569 | /* | |
570 | * There are two policies for allocating an inode. If the new inode is | |
571 | * a directory, then a forward search is made for a block group with both | |
572 | * free space and a low directory-to-inode ratio; if that fails, then of | |
573 | * the groups with above-average free space, that group with the fewest | |
574 | * directories already is chosen. | |
575 | * | |
576 | * For other inodes, search forward from the parent directory's block | |
577 | * group to find a free inode. | |
578 | */ | |
617ba13b | 579 | struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode) |
ac27a0ec DK |
580 | { |
581 | struct super_block *sb; | |
582 | struct buffer_head *bitmap_bh = NULL; | |
583 | struct buffer_head *bh2; | |
2aa9fc4c | 584 | ext4_group_t group = 0; |
ac27a0ec DK |
585 | unsigned long ino = 0; |
586 | struct inode * inode; | |
617ba13b MC |
587 | struct ext4_group_desc * gdp = NULL; |
588 | struct ext4_super_block * es; | |
589 | struct ext4_inode_info *ei; | |
590 | struct ext4_sb_info *sbi; | |
2aa9fc4c | 591 | int ret2, err = 0; |
ac27a0ec | 592 | struct inode *ret; |
2aa9fc4c AM |
593 | ext4_group_t i; |
594 | int free = 0; | |
772cb7c8 | 595 | ext4_group_t flex_group; |
ac27a0ec DK |
596 | |
597 | /* Cannot create files in a deleted directory */ | |
598 | if (!dir || !dir->i_nlink) | |
599 | return ERR_PTR(-EPERM); | |
600 | ||
601 | sb = dir->i_sb; | |
602 | inode = new_inode(sb); | |
603 | if (!inode) | |
604 | return ERR_PTR(-ENOMEM); | |
617ba13b | 605 | ei = EXT4_I(inode); |
ac27a0ec | 606 | |
617ba13b | 607 | sbi = EXT4_SB(sb); |
ac27a0ec | 608 | es = sbi->s_es; |
772cb7c8 JS |
609 | |
610 | if (sbi->s_log_groups_per_flex) { | |
611 | ret2 = find_group_flex(sb, dir, &group); | |
612 | goto got_group; | |
613 | } | |
614 | ||
ac27a0ec DK |
615 | if (S_ISDIR(mode)) { |
616 | if (test_opt (sb, OLDALLOC)) | |
2aa9fc4c | 617 | ret2 = find_group_dir(sb, dir, &group); |
ac27a0ec | 618 | else |
2aa9fc4c | 619 | ret2 = find_group_orlov(sb, dir, &group); |
ac27a0ec | 620 | } else |
2aa9fc4c | 621 | ret2 = find_group_other(sb, dir, &group); |
ac27a0ec | 622 | |
772cb7c8 | 623 | got_group: |
ac27a0ec | 624 | err = -ENOSPC; |
2aa9fc4c | 625 | if (ret2 == -1) |
ac27a0ec DK |
626 | goto out; |
627 | ||
628 | for (i = 0; i < sbi->s_groups_count; i++) { | |
629 | err = -EIO; | |
630 | ||
617ba13b | 631 | gdp = ext4_get_group_desc(sb, group, &bh2); |
ac27a0ec DK |
632 | if (!gdp) |
633 | goto fail; | |
634 | ||
635 | brelse(bitmap_bh); | |
e29d1cde | 636 | bitmap_bh = ext4_read_inode_bitmap(sb, group); |
ac27a0ec DK |
637 | if (!bitmap_bh) |
638 | goto fail; | |
639 | ||
640 | ino = 0; | |
641 | ||
642 | repeat_in_this_group: | |
617ba13b MC |
643 | ino = ext4_find_next_zero_bit((unsigned long *) |
644 | bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino); | |
645 | if (ino < EXT4_INODES_PER_GROUP(sb)) { | |
ac27a0ec DK |
646 | |
647 | BUFFER_TRACE(bitmap_bh, "get_write_access"); | |
617ba13b | 648 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
ac27a0ec DK |
649 | if (err) |
650 | goto fail; | |
651 | ||
617ba13b | 652 | if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group), |
ac27a0ec DK |
653 | ino, bitmap_bh->b_data)) { |
654 | /* we won it */ | |
655 | BUFFER_TRACE(bitmap_bh, | |
617ba13b MC |
656 | "call ext4_journal_dirty_metadata"); |
657 | err = ext4_journal_dirty_metadata(handle, | |
ac27a0ec DK |
658 | bitmap_bh); |
659 | if (err) | |
660 | goto fail; | |
661 | goto got; | |
662 | } | |
663 | /* we lost it */ | |
dab291af | 664 | jbd2_journal_release_buffer(handle, bitmap_bh); |
ac27a0ec | 665 | |
617ba13b | 666 | if (++ino < EXT4_INODES_PER_GROUP(sb)) |
ac27a0ec DK |
667 | goto repeat_in_this_group; |
668 | } | |
669 | ||
670 | /* | |
671 | * This case is possible in concurrent environment. It is very | |
672 | * rare. We cannot repeat the find_group_xxx() call because | |
673 | * that will simply return the same blockgroup, because the | |
674 | * group descriptor metadata has not yet been updated. | |
675 | * So we just go onto the next blockgroup. | |
676 | */ | |
677 | if (++group == sbi->s_groups_count) | |
678 | group = 0; | |
679 | } | |
680 | err = -ENOSPC; | |
681 | goto out; | |
682 | ||
683 | got: | |
717d50e4 AD |
684 | ino++; |
685 | if ((group == 0 && ino < EXT4_FIRST_INO(sb)) || | |
686 | ino > EXT4_INODES_PER_GROUP(sb)) { | |
46e665e9 | 687 | ext4_error(sb, __func__, |
717d50e4 | 688 | "reserved inode or inode > inodes count - " |
fd2d4291 | 689 | "block_group = %lu, inode=%lu", group, |
717d50e4 | 690 | ino + group * EXT4_INODES_PER_GROUP(sb)); |
ac27a0ec DK |
691 | err = -EIO; |
692 | goto fail; | |
693 | } | |
694 | ||
695 | BUFFER_TRACE(bh2, "get_write_access"); | |
617ba13b | 696 | err = ext4_journal_get_write_access(handle, bh2); |
ac27a0ec | 697 | if (err) goto fail; |
717d50e4 AD |
698 | |
699 | /* We may have to initialize the block bitmap if it isn't already */ | |
700 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && | |
701 | gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { | |
574ca174 | 702 | struct buffer_head *block_bh = ext4_read_block_bitmap(sb, group); |
717d50e4 AD |
703 | |
704 | BUFFER_TRACE(block_bh, "get block bitmap access"); | |
705 | err = ext4_journal_get_write_access(handle, block_bh); | |
706 | if (err) { | |
707 | brelse(block_bh); | |
708 | goto fail; | |
709 | } | |
710 | ||
711 | free = 0; | |
712 | spin_lock(sb_bgl_lock(sbi, group)); | |
713 | /* recheck and clear flag under lock if we still need to */ | |
714 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { | |
715 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); | |
716 | free = ext4_free_blocks_after_init(sb, group, gdp); | |
717 | gdp->bg_free_blocks_count = cpu_to_le16(free); | |
718 | } | |
719 | spin_unlock(sb_bgl_lock(sbi, group)); | |
720 | ||
721 | /* Don't need to dirty bitmap block if we didn't change it */ | |
722 | if (free) { | |
723 | BUFFER_TRACE(block_bh, "dirty block bitmap"); | |
724 | err = ext4_journal_dirty_metadata(handle, block_bh); | |
725 | } | |
726 | ||
727 | brelse(block_bh); | |
728 | if (err) | |
729 | goto fail; | |
730 | } | |
731 | ||
ac27a0ec | 732 | spin_lock(sb_bgl_lock(sbi, group)); |
717d50e4 AD |
733 | /* If we didn't allocate from within the initialized part of the inode |
734 | * table then we need to initialize up to this inode. */ | |
735 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { | |
736 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { | |
737 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); | |
738 | ||
739 | /* When marking the block group with | |
740 | * ~EXT4_BG_INODE_UNINIT we don't want to depend | |
b5f10eed | 741 | * on the value of bg_itable_unused even though |
717d50e4 AD |
742 | * mke2fs could have initialized the same for us. |
743 | * Instead we calculated the value below | |
744 | */ | |
745 | ||
746 | free = 0; | |
747 | } else { | |
748 | free = EXT4_INODES_PER_GROUP(sb) - | |
749 | le16_to_cpu(gdp->bg_itable_unused); | |
750 | } | |
751 | ||
752 | /* | |
753 | * Check the relative inode number against the last used | |
754 | * relative inode number in this group. if it is greater | |
755 | * we need to update the bg_itable_unused count | |
756 | * | |
757 | */ | |
758 | if (ino > free) | |
759 | gdp->bg_itable_unused = | |
760 | cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino); | |
761 | } | |
762 | ||
e8546d06 | 763 | le16_add_cpu(&gdp->bg_free_inodes_count, -1); |
ac27a0ec | 764 | if (S_ISDIR(mode)) { |
e8546d06 | 765 | le16_add_cpu(&gdp->bg_used_dirs_count, 1); |
ac27a0ec | 766 | } |
717d50e4 | 767 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); |
ac27a0ec | 768 | spin_unlock(sb_bgl_lock(sbi, group)); |
617ba13b MC |
769 | BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata"); |
770 | err = ext4_journal_dirty_metadata(handle, bh2); | |
ac27a0ec DK |
771 | if (err) goto fail; |
772 | ||
773 | percpu_counter_dec(&sbi->s_freeinodes_counter); | |
774 | if (S_ISDIR(mode)) | |
775 | percpu_counter_inc(&sbi->s_dirs_counter); | |
776 | sb->s_dirt = 1; | |
777 | ||
772cb7c8 JS |
778 | if (sbi->s_log_groups_per_flex) { |
779 | flex_group = ext4_flex_group(sbi, group); | |
780 | spin_lock(sb_bgl_lock(sbi, flex_group)); | |
781 | sbi->s_flex_groups[flex_group].free_inodes--; | |
782 | spin_unlock(sb_bgl_lock(sbi, flex_group)); | |
783 | } | |
784 | ||
ac27a0ec DK |
785 | inode->i_uid = current->fsuid; |
786 | if (test_opt (sb, GRPID)) | |
787 | inode->i_gid = dir->i_gid; | |
788 | else if (dir->i_mode & S_ISGID) { | |
789 | inode->i_gid = dir->i_gid; | |
790 | if (S_ISDIR(mode)) | |
791 | mode |= S_ISGID; | |
792 | } else | |
793 | inode->i_gid = current->fsgid; | |
794 | inode->i_mode = mode; | |
795 | ||
717d50e4 | 796 | inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); |
ac27a0ec DK |
797 | /* This is the optimal IO size (for stat), not the fs block size */ |
798 | inode->i_blocks = 0; | |
ef7f3835 KS |
799 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
800 | ext4_current_time(inode); | |
ac27a0ec DK |
801 | |
802 | memset(ei->i_data, 0, sizeof(ei->i_data)); | |
803 | ei->i_dir_start_lookup = 0; | |
804 | ei->i_disksize = 0; | |
805 | ||
42bf0383 AK |
806 | /* |
807 | * Don't inherit extent flag from directory. We set extent flag on | |
808 | * newly created directory and file only if -o extent mount option is | |
809 | * specified | |
810 | */ | |
811 | ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL); | |
ac27a0ec | 812 | if (S_ISLNK(mode)) |
617ba13b | 813 | ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL); |
ac27a0ec DK |
814 | /* dirsync only applies to directories */ |
815 | if (!S_ISDIR(mode)) | |
617ba13b | 816 | ei->i_flags &= ~EXT4_DIRSYNC_FL; |
ac27a0ec | 817 | ei->i_file_acl = 0; |
ac27a0ec DK |
818 | ei->i_dtime = 0; |
819 | ei->i_block_alloc_info = NULL; | |
820 | ei->i_block_group = group; | |
821 | ||
617ba13b | 822 | ext4_set_inode_flags(inode); |
ac27a0ec DK |
823 | if (IS_DIRSYNC(inode)) |
824 | handle->h_sync = 1; | |
825 | insert_inode_hash(inode); | |
826 | spin_lock(&sbi->s_next_gen_lock); | |
827 | inode->i_generation = sbi->s_next_generation++; | |
828 | spin_unlock(&sbi->s_next_gen_lock); | |
829 | ||
617ba13b | 830 | ei->i_state = EXT4_STATE_NEW; |
ef7f3835 KS |
831 | |
832 | ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; | |
ac27a0ec DK |
833 | |
834 | ret = inode; | |
835 | if(DQUOT_ALLOC_INODE(inode)) { | |
836 | err = -EDQUOT; | |
837 | goto fail_drop; | |
838 | } | |
839 | ||
617ba13b | 840 | err = ext4_init_acl(handle, inode, dir); |
ac27a0ec DK |
841 | if (err) |
842 | goto fail_free_drop; | |
843 | ||
617ba13b | 844 | err = ext4_init_security(handle,inode, dir); |
ac27a0ec DK |
845 | if (err) |
846 | goto fail_free_drop; | |
847 | ||
a86c6181 | 848 | if (test_opt(sb, EXTENTS)) { |
e4079a11 | 849 | /* set extent flag only for directory, file and normal symlink*/ |
e65187e6 | 850 | if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { |
42bf0383 AK |
851 | EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; |
852 | ext4_ext_tree_init(handle, inode); | |
42bf0383 | 853 | } |
a86c6181 | 854 | } |
ac27a0ec | 855 | |
8753e88f AK |
856 | err = ext4_mark_inode_dirty(handle, inode); |
857 | if (err) { | |
858 | ext4_std_error(sb, err); | |
859 | goto fail_free_drop; | |
860 | } | |
861 | ||
617ba13b | 862 | ext4_debug("allocating inode %lu\n", inode->i_ino); |
ac27a0ec DK |
863 | goto really_out; |
864 | fail: | |
617ba13b | 865 | ext4_std_error(sb, err); |
ac27a0ec DK |
866 | out: |
867 | iput(inode); | |
868 | ret = ERR_PTR(err); | |
869 | really_out: | |
870 | brelse(bitmap_bh); | |
871 | return ret; | |
872 | ||
873 | fail_free_drop: | |
874 | DQUOT_FREE_INODE(inode); | |
875 | ||
876 | fail_drop: | |
877 | DQUOT_DROP(inode); | |
878 | inode->i_flags |= S_NOQUOTA; | |
879 | inode->i_nlink = 0; | |
880 | iput(inode); | |
881 | brelse(bitmap_bh); | |
882 | return ERR_PTR(err); | |
883 | } | |
884 | ||
885 | /* Verify that we are loading a valid orphan from disk */ | |
617ba13b | 886 | struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) |
ac27a0ec | 887 | { |
617ba13b | 888 | unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); |
fd2d4291 | 889 | ext4_group_t block_group; |
ac27a0ec | 890 | int bit; |
1d1fe1ee | 891 | struct buffer_head *bitmap_bh; |
ac27a0ec | 892 | struct inode *inode = NULL; |
1d1fe1ee | 893 | long err = -EIO; |
ac27a0ec DK |
894 | |
895 | /* Error cases - e2fsck has already cleaned up for us */ | |
896 | if (ino > max_ino) { | |
46e665e9 | 897 | ext4_warning(sb, __func__, |
ac27a0ec | 898 | "bad orphan ino %lu! e2fsck was run?", ino); |
1d1fe1ee | 899 | goto error; |
ac27a0ec DK |
900 | } |
901 | ||
617ba13b MC |
902 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
903 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); | |
e29d1cde | 904 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
ac27a0ec | 905 | if (!bitmap_bh) { |
46e665e9 | 906 | ext4_warning(sb, __func__, |
ac27a0ec | 907 | "inode bitmap error for orphan %lu", ino); |
1d1fe1ee | 908 | goto error; |
ac27a0ec DK |
909 | } |
910 | ||
911 | /* Having the inode bit set should be a 100% indicator that this | |
912 | * is a valid orphan (no e2fsck run on fs). Orphans also include | |
913 | * inodes that were being truncated, so we can't check i_nlink==0. | |
914 | */ | |
1d1fe1ee DH |
915 | if (!ext4_test_bit(bit, bitmap_bh->b_data)) |
916 | goto bad_orphan; | |
917 | ||
918 | inode = ext4_iget(sb, ino); | |
919 | if (IS_ERR(inode)) | |
920 | goto iget_failed; | |
921 | ||
91ef4caf DG |
922 | /* |
923 | * If the orphans has i_nlinks > 0 then it should be able to be | |
924 | * truncated, otherwise it won't be removed from the orphan list | |
925 | * during processing and an infinite loop will result. | |
926 | */ | |
927 | if (inode->i_nlink && !ext4_can_truncate(inode)) | |
928 | goto bad_orphan; | |
929 | ||
1d1fe1ee DH |
930 | if (NEXT_ORPHAN(inode) > max_ino) |
931 | goto bad_orphan; | |
932 | brelse(bitmap_bh); | |
933 | return inode; | |
934 | ||
935 | iget_failed: | |
936 | err = PTR_ERR(inode); | |
937 | inode = NULL; | |
938 | bad_orphan: | |
46e665e9 | 939 | ext4_warning(sb, __func__, |
1d1fe1ee DH |
940 | "bad orphan inode %lu! e2fsck was run?", ino); |
941 | printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", | |
942 | bit, (unsigned long long)bitmap_bh->b_blocknr, | |
943 | ext4_test_bit(bit, bitmap_bh->b_data)); | |
944 | printk(KERN_NOTICE "inode=%p\n", inode); | |
945 | if (inode) { | |
946 | printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", | |
947 | is_bad_inode(inode)); | |
948 | printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", | |
949 | NEXT_ORPHAN(inode)); | |
950 | printk(KERN_NOTICE "max_ino=%lu\n", max_ino); | |
91ef4caf | 951 | printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink); |
ac27a0ec | 952 | /* Avoid freeing blocks if we got a bad deleted inode */ |
1d1fe1ee | 953 | if (inode->i_nlink == 0) |
ac27a0ec DK |
954 | inode->i_blocks = 0; |
955 | iput(inode); | |
ac27a0ec | 956 | } |
ac27a0ec | 957 | brelse(bitmap_bh); |
1d1fe1ee DH |
958 | error: |
959 | return ERR_PTR(err); | |
ac27a0ec DK |
960 | } |
961 | ||
617ba13b | 962 | unsigned long ext4_count_free_inodes (struct super_block * sb) |
ac27a0ec DK |
963 | { |
964 | unsigned long desc_count; | |
617ba13b | 965 | struct ext4_group_desc *gdp; |
fd2d4291 | 966 | ext4_group_t i; |
617ba13b MC |
967 | #ifdef EXT4FS_DEBUG |
968 | struct ext4_super_block *es; | |
ac27a0ec DK |
969 | unsigned long bitmap_count, x; |
970 | struct buffer_head *bitmap_bh = NULL; | |
971 | ||
617ba13b | 972 | es = EXT4_SB(sb)->s_es; |
ac27a0ec DK |
973 | desc_count = 0; |
974 | bitmap_count = 0; | |
975 | gdp = NULL; | |
617ba13b MC |
976 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
977 | gdp = ext4_get_group_desc (sb, i, NULL); | |
ac27a0ec DK |
978 | if (!gdp) |
979 | continue; | |
980 | desc_count += le16_to_cpu(gdp->bg_free_inodes_count); | |
981 | brelse(bitmap_bh); | |
e29d1cde | 982 | bitmap_bh = ext4_read_inode_bitmap(sb, i); |
ac27a0ec DK |
983 | if (!bitmap_bh) |
984 | continue; | |
985 | ||
617ba13b | 986 | x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); |
c549a95d | 987 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
ac27a0ec DK |
988 | i, le16_to_cpu(gdp->bg_free_inodes_count), x); |
989 | bitmap_count += x; | |
990 | } | |
991 | brelse(bitmap_bh); | |
617ba13b | 992 | printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n", |
ac27a0ec DK |
993 | le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); |
994 | return desc_count; | |
995 | #else | |
996 | desc_count = 0; | |
617ba13b MC |
997 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
998 | gdp = ext4_get_group_desc (sb, i, NULL); | |
ac27a0ec DK |
999 | if (!gdp) |
1000 | continue; | |
1001 | desc_count += le16_to_cpu(gdp->bg_free_inodes_count); | |
1002 | cond_resched(); | |
1003 | } | |
1004 | return desc_count; | |
1005 | #endif | |
1006 | } | |
1007 | ||
1008 | /* Called at mount-time, super-block is locked */ | |
617ba13b | 1009 | unsigned long ext4_count_dirs (struct super_block * sb) |
ac27a0ec DK |
1010 | { |
1011 | unsigned long count = 0; | |
fd2d4291 | 1012 | ext4_group_t i; |
ac27a0ec | 1013 | |
617ba13b MC |
1014 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
1015 | struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL); | |
ac27a0ec DK |
1016 | if (!gdp) |
1017 | continue; | |
1018 | count += le16_to_cpu(gdp->bg_used_dirs_count); | |
1019 | } | |
1020 | return count; | |
1021 | } | |
1022 |