ialloc.c 18 KB

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