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