Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/ext2/ialloc.c | |
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@dcs.ed.ac.uk), 1993 | |
12 | * Big-endian to little-endian byte-swapping/bitmaps by | |
13 | * David S. Miller (davem@caip.rutgers.edu), 1995 | |
14 | */ | |
15 | ||
1da177e4 LT |
16 | #include <linux/quotaops.h> |
17 | #include <linux/sched.h> | |
18 | #include <linux/backing-dev.h> | |
19 | #include <linux/buffer_head.h> | |
20 | #include <linux/random.h> | |
21 | #include "ext2.h" | |
22 | #include "xattr.h" | |
23 | #include "acl.h" | |
24 | ||
25 | /* | |
26 | * ialloc.c contains the inodes allocation and deallocation routines | |
27 | */ | |
28 | ||
29 | /* | |
30 | * The free inodes are managed by bitmaps. A file system contains several | |
31 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap | |
32 | * block for inodes, N blocks for the inode table and data blocks. | |
33 | * | |
34 | * The file system contains group descriptors which are located after the | |
35 | * super block. Each descriptor contains the number of the bitmap block and | |
36 | * the free blocks count in the block. | |
37 | */ | |
38 | ||
39 | ||
40 | /* | |
41 | * Read the inode allocation bitmap for a given block_group, reading | |
42 | * into the specified slot in the superblock's bitmap cache. | |
43 | * | |
44 | * Return buffer_head of bitmap on success or NULL. | |
45 | */ | |
46 | static struct buffer_head * | |
47 | read_inode_bitmap(struct super_block * sb, unsigned long block_group) | |
48 | { | |
49 | struct ext2_group_desc *desc; | |
50 | struct buffer_head *bh = NULL; | |
51 | ||
52 | desc = ext2_get_group_desc(sb, block_group, NULL); | |
53 | if (!desc) | |
54 | goto error_out; | |
55 | ||
56 | bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap)); | |
57 | if (!bh) | |
58 | ext2_error(sb, "read_inode_bitmap", | |
59 | "Cannot read inode bitmap - " | |
60 | "block_group = %lu, inode_bitmap = %u", | |
61 | block_group, le32_to_cpu(desc->bg_inode_bitmap)); | |
62 | error_out: | |
63 | return bh; | |
64 | } | |
65 | ||
66 | static void ext2_release_inode(struct super_block *sb, int group, int dir) | |
67 | { | |
68 | struct ext2_group_desc * desc; | |
69 | struct buffer_head *bh; | |
70 | ||
71 | desc = ext2_get_group_desc(sb, group, &bh); | |
72 | if (!desc) { | |
73 | ext2_error(sb, "ext2_release_inode", | |
74 | "can't get descriptor for group %d", group); | |
75 | return; | |
76 | } | |
77 | ||
78 | spin_lock(sb_bgl_lock(EXT2_SB(sb), group)); | |
fba4d399 | 79 | le16_add_cpu(&desc->bg_free_inodes_count, 1); |
1da177e4 | 80 | if (dir) |
fba4d399 | 81 | le16_add_cpu(&desc->bg_used_dirs_count, -1); |
1da177e4 LT |
82 | spin_unlock(sb_bgl_lock(EXT2_SB(sb), group)); |
83 | if (dir) | |
84 | percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter); | |
1da177e4 LT |
85 | mark_buffer_dirty(bh); |
86 | } | |
87 | ||
88 | /* | |
89 | * NOTE! When we get the inode, we're the only people | |
90 | * that have access to it, and as such there are no | |
91 | * race conditions we have to worry about. The inode | |
92 | * is not on the hash-lists, and it cannot be reached | |
93 | * through the filesystem because the directory entry | |
94 | * has been deleted earlier. | |
95 | * | |
96 | * HOWEVER: we must make sure that we get no aliases, | |
97 | * which means that we have to call "clear_inode()" | |
98 | * _before_ we mark the inode not in use in the inode | |
99 | * bitmaps. Otherwise a newly created file might use | |
100 | * the same inode number (not actually the same pointer | |
101 | * though), and then we'd have two inodes sharing the | |
102 | * same inode number and space on the harddisk. | |
103 | */ | |
104 | void ext2_free_inode (struct inode * inode) | |
105 | { | |
106 | struct super_block * sb = inode->i_sb; | |
107 | int is_directory; | |
108 | unsigned long ino; | |
524e4a1d | 109 | struct buffer_head *bitmap_bh; |
1da177e4 LT |
110 | unsigned long block_group; |
111 | unsigned long bit; | |
112 | struct ext2_super_block * es; | |
113 | ||
114 | ino = inode->i_ino; | |
115 | ext2_debug ("freeing inode %lu\n", ino); | |
116 | ||
117 | /* | |
118 | * Note: we must free any quota before locking the superblock, | |
119 | * as writing the quota to disk may need the lock as well. | |
120 | */ | |
72edc4d0 | 121 | /* Quota is already initialized in iput() */ |
72edc4d0 AV |
122 | dquot_free_inode(inode); |
123 | dquot_drop(inode); | |
1da177e4 LT |
124 | |
125 | es = EXT2_SB(sb)->s_es; | |
126 | is_directory = S_ISDIR(inode->i_mode); | |
127 | ||
1da177e4 LT |
128 | if (ino < EXT2_FIRST_INO(sb) || |
129 | ino > le32_to_cpu(es->s_inodes_count)) { | |
130 | ext2_error (sb, "ext2_free_inode", | |
131 | "reserved or nonexistent inode %lu", ino); | |
524e4a1d | 132 | return; |
1da177e4 LT |
133 | } |
134 | block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb); | |
135 | bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb); | |
1da177e4 LT |
136 | bitmap_bh = read_inode_bitmap(sb, block_group); |
137 | if (!bitmap_bh) | |
524e4a1d | 138 | return; |
1da177e4 LT |
139 | |
140 | /* Ok, now we can actually update the inode bitmaps.. */ | |
141 | if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group), | |
142 | bit, (void *) bitmap_bh->b_data)) | |
143 | ext2_error (sb, "ext2_free_inode", | |
144 | "bit already cleared for inode %lu", ino); | |
145 | else | |
146 | ext2_release_inode(sb, block_group, is_directory); | |
147 | mark_buffer_dirty(bitmap_bh); | |
1751e8a6 | 148 | if (sb->s_flags & SB_SYNCHRONOUS) |
1da177e4 | 149 | sync_dirty_buffer(bitmap_bh); |
524e4a1d | 150 | |
1da177e4 LT |
151 | brelse(bitmap_bh); |
152 | } | |
153 | ||
154 | /* | |
155 | * We perform asynchronous prereading of the new inode's inode block when | |
156 | * we create the inode, in the expectation that the inode will be written | |
157 | * back soon. There are two reasons: | |
158 | * | |
159 | * - When creating a large number of files, the async prereads will be | |
160 | * nicely merged into large reads | |
161 | * - When writing out a large number of inodes, we don't need to keep on | |
162 | * stalling the writes while we read the inode block. | |
163 | * | |
164 | * FIXME: ext2_get_group_desc() needs to be simplified. | |
165 | */ | |
166 | static void ext2_preread_inode(struct inode *inode) | |
167 | { | |
168 | unsigned long block_group; | |
169 | unsigned long offset; | |
170 | unsigned long block; | |
1da177e4 LT |
171 | struct ext2_group_desc * gdp; |
172 | struct backing_dev_info *bdi; | |
173 | ||
de1414a6 | 174 | bdi = inode_to_bdi(inode); |
7f58351a | 175 | if (bdi_rw_congested(bdi)) |
1da177e4 LT |
176 | return; |
177 | ||
178 | block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb); | |
ef2fb679 | 179 | gdp = ext2_get_group_desc(inode->i_sb, block_group, NULL); |
1da177e4 LT |
180 | if (gdp == NULL) |
181 | return; | |
182 | ||
183 | /* | |
184 | * Figure out the offset within the block group inode table | |
185 | */ | |
186 | offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) * | |
187 | EXT2_INODE_SIZE(inode->i_sb); | |
188 | block = le32_to_cpu(gdp->bg_inode_table) + | |
189 | (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb)); | |
190 | sb_breadahead(inode->i_sb, block); | |
191 | } | |
192 | ||
193 | /* | |
194 | * There are two policies for allocating an inode. If the new inode is | |
195 | * a directory, then a forward search is made for a block group with both | |
196 | * free space and a low directory-to-inode ratio; if that fails, then of | |
197 | * the groups with above-average free space, that group with the fewest | |
198 | * directories already is chosen. | |
199 | * | |
200 | * For other inodes, search forward from the parent directory\'s block | |
201 | * group to find a free inode. | |
202 | */ | |
203 | static int find_group_dir(struct super_block *sb, struct inode *parent) | |
204 | { | |
205 | int ngroups = EXT2_SB(sb)->s_groups_count; | |
206 | int avefreei = ext2_count_free_inodes(sb) / ngroups; | |
207 | struct ext2_group_desc *desc, *best_desc = NULL; | |
1da177e4 LT |
208 | int group, best_group = -1; |
209 | ||
210 | for (group = 0; group < ngroups; group++) { | |
ef2fb679 | 211 | desc = ext2_get_group_desc (sb, group, NULL); |
1da177e4 LT |
212 | if (!desc || !desc->bg_free_inodes_count) |
213 | continue; | |
214 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) | |
215 | continue; | |
216 | if (!best_desc || | |
217 | (le16_to_cpu(desc->bg_free_blocks_count) > | |
218 | le16_to_cpu(best_desc->bg_free_blocks_count))) { | |
219 | best_group = group; | |
220 | best_desc = desc; | |
1da177e4 LT |
221 | } |
222 | } | |
1da177e4 LT |
223 | |
224 | return best_group; | |
225 | } | |
226 | ||
227 | /* | |
228 | * Orlov's allocator for directories. | |
229 | * | |
230 | * We always try to spread first-level directories. | |
231 | * | |
232 | * If there are blockgroups with both free inodes and free blocks counts | |
233 | * not worse than average we return one with smallest directory count. | |
234 | * Otherwise we simply return a random group. | |
235 | * | |
236 | * For the rest rules look so: | |
237 | * | |
238 | * It's OK to put directory into a group unless | |
239 | * it has too many directories already (max_dirs) or | |
240 | * it has too few free inodes left (min_inodes) or | |
241 | * it has too few free blocks left (min_blocks) or | |
242 | * it's already running too large debt (max_debt). | |
1cc8dcf5 | 243 | * Parent's group is preferred, if it doesn't satisfy these |
1da177e4 LT |
244 | * conditions we search cyclically through the rest. If none |
245 | * of the groups look good we just look for a group with more | |
246 | * free inodes than average (starting at parent's group). | |
247 | * | |
248 | * Debt is incremented each time we allocate a directory and decremented | |
249 | * when we allocate an inode, within 0--255. | |
250 | */ | |
251 | ||
252 | #define INODE_COST 64 | |
253 | #define BLOCK_COST 256 | |
254 | ||
255 | static int find_group_orlov(struct super_block *sb, struct inode *parent) | |
256 | { | |
257 | int parent_group = EXT2_I(parent)->i_block_group; | |
258 | struct ext2_sb_info *sbi = EXT2_SB(sb); | |
259 | struct ext2_super_block *es = sbi->s_es; | |
260 | int ngroups = sbi->s_groups_count; | |
261 | int inodes_per_group = EXT2_INODES_PER_GROUP(sb); | |
262 | int freei; | |
263 | int avefreei; | |
264 | int free_blocks; | |
265 | int avefreeb; | |
266 | int blocks_per_dir; | |
267 | int ndirs; | |
268 | int max_debt, max_dirs, min_blocks, min_inodes; | |
269 | int group = -1, i; | |
270 | struct ext2_group_desc *desc; | |
1da177e4 LT |
271 | |
272 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); | |
273 | avefreei = freei / ngroups; | |
274 | free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); | |
275 | avefreeb = free_blocks / ngroups; | |
276 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); | |
277 | ||
2b0143b5 | 278 | if ((parent == d_inode(sb->s_root)) || |
1da177e4 LT |
279 | (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) { |
280 | struct ext2_group_desc *best_desc = NULL; | |
1da177e4 LT |
281 | int best_ndir = inodes_per_group; |
282 | int best_group = -1; | |
283 | ||
e878167a | 284 | group = prandom_u32(); |
1da177e4 LT |
285 | parent_group = (unsigned)group % ngroups; |
286 | for (i = 0; i < ngroups; i++) { | |
287 | group = (parent_group + i) % ngroups; | |
ef2fb679 | 288 | desc = ext2_get_group_desc (sb, group, NULL); |
1da177e4 LT |
289 | if (!desc || !desc->bg_free_inodes_count) |
290 | continue; | |
291 | if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir) | |
292 | continue; | |
293 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) | |
294 | continue; | |
295 | if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb) | |
296 | continue; | |
297 | best_group = group; | |
298 | best_ndir = le16_to_cpu(desc->bg_used_dirs_count); | |
299 | best_desc = desc; | |
1da177e4 LT |
300 | } |
301 | if (best_group >= 0) { | |
302 | desc = best_desc; | |
1da177e4 LT |
303 | group = best_group; |
304 | goto found; | |
305 | } | |
306 | goto fallback; | |
307 | } | |
308 | ||
309 | if (ndirs == 0) | |
310 | ndirs = 1; /* percpu_counters are approximate... */ | |
311 | ||
312 | blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs; | |
313 | ||
314 | max_dirs = ndirs / ngroups + inodes_per_group / 16; | |
315 | min_inodes = avefreei - inodes_per_group / 4; | |
316 | min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4; | |
317 | ||
318 | max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST); | |
319 | if (max_debt * INODE_COST > inodes_per_group) | |
320 | max_debt = inodes_per_group / INODE_COST; | |
321 | if (max_debt > 255) | |
322 | max_debt = 255; | |
323 | if (max_debt == 0) | |
324 | max_debt = 1; | |
325 | ||
326 | for (i = 0; i < ngroups; i++) { | |
327 | group = (parent_group + i) % ngroups; | |
ef2fb679 | 328 | desc = ext2_get_group_desc (sb, group, NULL); |
1da177e4 LT |
329 | if (!desc || !desc->bg_free_inodes_count) |
330 | continue; | |
331 | if (sbi->s_debts[group] >= max_debt) | |
332 | continue; | |
333 | if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs) | |
334 | continue; | |
335 | if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes) | |
336 | continue; | |
337 | if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks) | |
338 | continue; | |
339 | goto found; | |
340 | } | |
341 | ||
342 | fallback: | |
343 | for (i = 0; i < ngroups; i++) { | |
344 | group = (parent_group + i) % ngroups; | |
ef2fb679 | 345 | desc = ext2_get_group_desc (sb, group, NULL); |
1da177e4 LT |
346 | if (!desc || !desc->bg_free_inodes_count) |
347 | continue; | |
348 | if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei) | |
349 | goto found; | |
350 | } | |
351 | ||
352 | if (avefreei) { | |
353 | /* | |
354 | * The free-inodes counter is approximate, and for really small | |
355 | * filesystems the above test can fail to find any blockgroups | |
356 | */ | |
357 | avefreei = 0; | |
358 | goto fallback; | |
359 | } | |
360 | ||
361 | return -1; | |
362 | ||
363 | found: | |
364 | return group; | |
365 | } | |
366 | ||
367 | static int find_group_other(struct super_block *sb, struct inode *parent) | |
368 | { | |
369 | int parent_group = EXT2_I(parent)->i_block_group; | |
370 | int ngroups = EXT2_SB(sb)->s_groups_count; | |
371 | struct ext2_group_desc *desc; | |
1da177e4 LT |
372 | int group, i; |
373 | ||
374 | /* | |
375 | * Try to place the inode in its parent directory | |
376 | */ | |
377 | group = parent_group; | |
ef2fb679 | 378 | desc = ext2_get_group_desc (sb, group, NULL); |
1da177e4 LT |
379 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
380 | le16_to_cpu(desc->bg_free_blocks_count)) | |
381 | goto found; | |
382 | ||
383 | /* | |
384 | * We're going to place this inode in a different blockgroup from its | |
385 | * parent. We want to cause files in a common directory to all land in | |
386 | * the same blockgroup. But we want files which are in a different | |
387 | * directory which shares a blockgroup with our parent to land in a | |
388 | * different blockgroup. | |
389 | * | |
390 | * So add our directory's i_ino into the starting point for the hash. | |
391 | */ | |
392 | group = (group + parent->i_ino) % ngroups; | |
393 | ||
394 | /* | |
395 | * Use a quadratic hash to find a group with a free inode and some | |
396 | * free blocks. | |
397 | */ | |
398 | for (i = 1; i < ngroups; i <<= 1) { | |
399 | group += i; | |
400 | if (group >= ngroups) | |
401 | group -= ngroups; | |
ef2fb679 | 402 | desc = ext2_get_group_desc (sb, group, NULL); |
1da177e4 LT |
403 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
404 | le16_to_cpu(desc->bg_free_blocks_count)) | |
405 | goto found; | |
406 | } | |
407 | ||
408 | /* | |
409 | * That failed: try linear search for a free inode, even if that group | |
410 | * has no free blocks. | |
411 | */ | |
412 | group = parent_group; | |
413 | for (i = 0; i < ngroups; i++) { | |
414 | if (++group >= ngroups) | |
415 | group = 0; | |
ef2fb679 | 416 | desc = ext2_get_group_desc (sb, group, NULL); |
1da177e4 LT |
417 | if (desc && le16_to_cpu(desc->bg_free_inodes_count)) |
418 | goto found; | |
419 | } | |
420 | ||
421 | return -1; | |
422 | ||
423 | found: | |
424 | return group; | |
425 | } | |
426 | ||
3ea40bc9 | 427 | struct inode *ext2_new_inode(struct inode *dir, umode_t mode, |
2a7dba39 | 428 | const struct qstr *qstr) |
1da177e4 LT |
429 | { |
430 | struct super_block *sb; | |
431 | struct buffer_head *bitmap_bh = NULL; | |
432 | struct buffer_head *bh2; | |
433 | int group, i; | |
434 | ino_t ino = 0; | |
435 | struct inode * inode; | |
436 | struct ext2_group_desc *gdp; | |
437 | struct ext2_super_block *es; | |
438 | struct ext2_inode_info *ei; | |
439 | struct ext2_sb_info *sbi; | |
440 | int err; | |
441 | ||
442 | sb = dir->i_sb; | |
443 | inode = new_inode(sb); | |
444 | if (!inode) | |
445 | return ERR_PTR(-ENOMEM); | |
446 | ||
447 | ei = EXT2_I(inode); | |
448 | sbi = EXT2_SB(sb); | |
449 | es = sbi->s_es; | |
450 | if (S_ISDIR(mode)) { | |
451 | if (test_opt(sb, OLDALLOC)) | |
452 | group = find_group_dir(sb, dir); | |
453 | else | |
454 | group = find_group_orlov(sb, dir); | |
455 | } else | |
456 | group = find_group_other(sb, dir); | |
457 | ||
458 | if (group == -1) { | |
459 | err = -ENOSPC; | |
460 | goto fail; | |
461 | } | |
462 | ||
463 | for (i = 0; i < sbi->s_groups_count; i++) { | |
464 | gdp = ext2_get_group_desc(sb, group, &bh2); | |
f7a1c358 JK |
465 | if (!gdp) { |
466 | if (++group == sbi->s_groups_count) | |
467 | group = 0; | |
468 | continue; | |
469 | } | |
1da177e4 LT |
470 | brelse(bitmap_bh); |
471 | bitmap_bh = read_inode_bitmap(sb, group); | |
472 | if (!bitmap_bh) { | |
473 | err = -EIO; | |
474 | goto fail; | |
475 | } | |
476 | ino = 0; | |
477 | ||
478 | repeat_in_this_group: | |
479 | ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data, | |
480 | EXT2_INODES_PER_GROUP(sb), ino); | |
481 | if (ino >= EXT2_INODES_PER_GROUP(sb)) { | |
482 | /* | |
483 | * Rare race: find_group_xx() decided that there were | |
484 | * free inodes in this group, but by the time we tried | |
485 | * to allocate one, they're all gone. This can also | |
486 | * occur because the counters which find_group_orlov() | |
487 | * uses are approximate. So just go and search the | |
488 | * next block group. | |
489 | */ | |
490 | if (++group == sbi->s_groups_count) | |
491 | group = 0; | |
492 | continue; | |
493 | } | |
494 | if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group), | |
495 | ino, bitmap_bh->b_data)) { | |
496 | /* we lost this inode */ | |
497 | if (++ino >= EXT2_INODES_PER_GROUP(sb)) { | |
498 | /* this group is exhausted, try next group */ | |
499 | if (++group == sbi->s_groups_count) | |
500 | group = 0; | |
501 | continue; | |
502 | } | |
503 | /* try to find free inode in the same group */ | |
504 | goto repeat_in_this_group; | |
505 | } | |
506 | goto got; | |
507 | } | |
508 | ||
509 | /* | |
510 | * Scanned all blockgroups. | |
511 | */ | |
dc1f7380 | 512 | brelse(bitmap_bh); |
1da177e4 LT |
513 | err = -ENOSPC; |
514 | goto fail; | |
515 | got: | |
516 | mark_buffer_dirty(bitmap_bh); | |
1751e8a6 | 517 | if (sb->s_flags & SB_SYNCHRONOUS) |
1da177e4 LT |
518 | sync_dirty_buffer(bitmap_bh); |
519 | brelse(bitmap_bh); | |
520 | ||
521 | ino += group * EXT2_INODES_PER_GROUP(sb) + 1; | |
522 | if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { | |
523 | ext2_error (sb, "ext2_new_inode", | |
524 | "reserved inode or inode > inodes count - " | |
525 | "block_group = %d,inode=%lu", group, | |
526 | (unsigned long) ino); | |
527 | err = -EIO; | |
528 | goto fail; | |
529 | } | |
530 | ||
aa0dff2d | 531 | percpu_counter_add(&sbi->s_freeinodes_counter, -1); |
1da177e4 LT |
532 | if (S_ISDIR(mode)) |
533 | percpu_counter_inc(&sbi->s_dirs_counter); | |
534 | ||
535 | spin_lock(sb_bgl_lock(sbi, group)); | |
fba4d399 | 536 | le16_add_cpu(&gdp->bg_free_inodes_count, -1); |
1da177e4 LT |
537 | if (S_ISDIR(mode)) { |
538 | if (sbi->s_debts[group] < 255) | |
539 | sbi->s_debts[group]++; | |
fba4d399 | 540 | le16_add_cpu(&gdp->bg_used_dirs_count, 1); |
1da177e4 LT |
541 | } else { |
542 | if (sbi->s_debts[group]) | |
543 | sbi->s_debts[group]--; | |
544 | } | |
545 | spin_unlock(sb_bgl_lock(sbi, group)); | |
546 | ||
1da177e4 | 547 | mark_buffer_dirty(bh2); |
ffba102d DM |
548 | if (test_opt(sb, GRPID)) { |
549 | inode->i_mode = mode; | |
550 | inode->i_uid = current_fsuid(); | |
1da177e4 | 551 | inode->i_gid = dir->i_gid; |
1da177e4 | 552 | } else |
ffba102d | 553 | inode_init_owner(inode, dir, mode); |
1da177e4 LT |
554 | |
555 | inode->i_ino = ino; | |
1da177e4 | 556 | inode->i_blocks = 0; |
02027d42 | 557 | inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
1da177e4 | 558 | memset(ei->i_data, 0, sizeof(ei->i_data)); |
ef8b6461 DG |
559 | ei->i_flags = |
560 | ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED); | |
1da177e4 LT |
561 | ei->i_faddr = 0; |
562 | ei->i_frag_no = 0; | |
563 | ei->i_frag_size = 0; | |
564 | ei->i_file_acl = 0; | |
565 | ei->i_dir_acl = 0; | |
566 | ei->i_dtime = 0; | |
a686cd89 | 567 | ei->i_block_alloc_info = NULL; |
1da177e4 | 568 | ei->i_block_group = group; |
1da177e4 LT |
569 | ei->i_dir_start_lookup = 0; |
570 | ei->i_state = EXT2_STATE_NEW; | |
571 | ext2_set_inode_flags(inode); | |
572 | spin_lock(&sbi->s_next_gen_lock); | |
573 | inode->i_generation = sbi->s_next_generation++; | |
574 | spin_unlock(&sbi->s_next_gen_lock); | |
41080b5a | 575 | if (insert_inode_locked(inode) < 0) { |
ef6919c2 JK |
576 | ext2_error(sb, "ext2_new_inode", |
577 | "inode number already in use - inode=%lu", | |
578 | (unsigned long) ino); | |
579 | err = -EIO; | |
580 | goto fail; | |
41080b5a | 581 | } |
1da177e4 | 582 | |
c2edb305 JK |
583 | err = dquot_initialize(inode); |
584 | if (err) | |
585 | goto fail_drop; | |
586 | ||
63936dda CH |
587 | err = dquot_alloc_inode(inode); |
588 | if (err) | |
9ed6c2fb | 589 | goto fail_drop; |
9ed6c2fb | 590 | |
1da177e4 | 591 | err = ext2_init_acl(inode, dir); |
9ed6c2fb CS |
592 | if (err) |
593 | goto fail_free_drop; | |
594 | ||
2a7dba39 | 595 | err = ext2_init_security(inode, dir, qstr); |
9ed6c2fb CS |
596 | if (err) |
597 | goto fail_free_drop; | |
598 | ||
1da177e4 LT |
599 | mark_inode_dirty(inode); |
600 | ext2_debug("allocating inode %lu\n", inode->i_ino); | |
601 | ext2_preread_inode(inode); | |
602 | return inode; | |
603 | ||
9ed6c2fb | 604 | fail_free_drop: |
63936dda | 605 | dquot_free_inode(inode); |
9ed6c2fb CS |
606 | |
607 | fail_drop: | |
9f754758 | 608 | dquot_drop(inode); |
1da177e4 | 609 | inode->i_flags |= S_NOQUOTA; |
6d6b77f1 | 610 | clear_nlink(inode); |
2e5afe54 | 611 | discard_new_inode(inode); |
1da177e4 LT |
612 | return ERR_PTR(err); |
613 | ||
614 | fail: | |
615 | make_bad_inode(inode); | |
616 | iput(inode); | |
617 | return ERR_PTR(err); | |
618 | } | |
619 | ||
620 | unsigned long ext2_count_free_inodes (struct super_block * sb) | |
621 | { | |
622 | struct ext2_group_desc *desc; | |
623 | unsigned long desc_count = 0; | |
624 | int i; | |
625 | ||
626 | #ifdef EXT2FS_DEBUG | |
627 | struct ext2_super_block *es; | |
628 | unsigned long bitmap_count = 0; | |
629 | struct buffer_head *bitmap_bh = NULL; | |
630 | ||
1da177e4 LT |
631 | es = EXT2_SB(sb)->s_es; |
632 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { | |
633 | unsigned x; | |
634 | ||
635 | desc = ext2_get_group_desc (sb, i, NULL); | |
636 | if (!desc) | |
637 | continue; | |
638 | desc_count += le16_to_cpu(desc->bg_free_inodes_count); | |
639 | brelse(bitmap_bh); | |
640 | bitmap_bh = read_inode_bitmap(sb, i); | |
641 | if (!bitmap_bh) | |
642 | continue; | |
643 | ||
644 | x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8); | |
645 | printk("group %d: stored = %d, counted = %u\n", | |
646 | i, le16_to_cpu(desc->bg_free_inodes_count), x); | |
647 | bitmap_count += x; | |
648 | } | |
649 | brelse(bitmap_bh); | |
650 | printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n", | |
ecd0afa3 | 651 | (unsigned long) |
1da177e4 LT |
652 | percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter), |
653 | desc_count, bitmap_count); | |
1da177e4 LT |
654 | return desc_count; |
655 | #else | |
656 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { | |
657 | desc = ext2_get_group_desc (sb, i, NULL); | |
658 | if (!desc) | |
659 | continue; | |
660 | desc_count += le16_to_cpu(desc->bg_free_inodes_count); | |
661 | } | |
662 | return desc_count; | |
663 | #endif | |
664 | } | |
665 | ||
666 | /* Called at mount-time, super-block is locked */ | |
667 | unsigned long ext2_count_dirs (struct super_block * sb) | |
668 | { | |
669 | unsigned long count = 0; | |
670 | int i; | |
671 | ||
672 | for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) { | |
673 | struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL); | |
674 | if (!gdp) | |
675 | continue; | |
676 | count += le16_to_cpu(gdp->bg_used_dirs_count); | |
677 | } | |
678 | return count; | |
679 | } | |
680 |