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