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