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