ialloc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 ([email protected])
  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 ([email protected]), 1993
  12. * Big-endian to little-endian byte-swapping/bitmaps by
  13. * David S. Miller ([email protected]), 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. struct ext2_group_desc *best_desc = NULL;
  249. int best_ndir = inodes_per_group;
  250. int best_group = -1;
  251. parent_group = prandom_u32_max(ngroups);
  252. for (i = 0; i < ngroups; i++) {
  253. group = (parent_group + i) % ngroups;
  254. desc = ext2_get_group_desc (sb, group, NULL);
  255. if (!desc || !desc->bg_free_inodes_count)
  256. continue;
  257. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  258. continue;
  259. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  260. continue;
  261. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  262. continue;
  263. best_group = group;
  264. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  265. best_desc = desc;
  266. }
  267. if (best_group >= 0) {
  268. desc = best_desc;
  269. group = best_group;
  270. goto found;
  271. }
  272. goto fallback;
  273. }
  274. if (ndirs == 0)
  275. ndirs = 1; /* percpu_counters are approximate... */
  276. blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
  277. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  278. min_inodes = avefreei - inodes_per_group / 4;
  279. min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
  280. max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
  281. if (max_debt * INODE_COST > inodes_per_group)
  282. max_debt = inodes_per_group / INODE_COST;
  283. if (max_debt > 255)
  284. max_debt = 255;
  285. if (max_debt == 0)
  286. max_debt = 1;
  287. for (i = 0; i < ngroups; i++) {
  288. group = (parent_group + i) % ngroups;
  289. desc = ext2_get_group_desc (sb, group, NULL);
  290. if (!desc || !desc->bg_free_inodes_count)
  291. continue;
  292. if (sbi->s_debts[group] >= max_debt)
  293. continue;
  294. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  295. continue;
  296. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  297. continue;
  298. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  299. continue;
  300. goto found;
  301. }
  302. fallback:
  303. for (i = 0; i < ngroups; i++) {
  304. group = (parent_group + i) % ngroups;
  305. desc = ext2_get_group_desc (sb, group, NULL);
  306. if (!desc || !desc->bg_free_inodes_count)
  307. continue;
  308. if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  309. goto found;
  310. }
  311. if (avefreei) {
  312. /*
  313. * The free-inodes counter is approximate, and for really small
  314. * filesystems the above test can fail to find any blockgroups
  315. */
  316. avefreei = 0;
  317. goto fallback;
  318. }
  319. return -1;
  320. found:
  321. return group;
  322. }
  323. static int find_group_other(struct super_block *sb, struct inode *parent)
  324. {
  325. int parent_group = EXT2_I(parent)->i_block_group;
  326. int ngroups = EXT2_SB(sb)->s_groups_count;
  327. struct ext2_group_desc *desc;
  328. int group, i;
  329. /*
  330. * Try to place the inode in its parent directory
  331. */
  332. group = parent_group;
  333. desc = ext2_get_group_desc (sb, group, NULL);
  334. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  335. le16_to_cpu(desc->bg_free_blocks_count))
  336. goto found;
  337. /*
  338. * We're going to place this inode in a different blockgroup from its
  339. * parent. We want to cause files in a common directory to all land in
  340. * the same blockgroup. But we want files which are in a different
  341. * directory which shares a blockgroup with our parent to land in a
  342. * different blockgroup.
  343. *
  344. * So add our directory's i_ino into the starting point for the hash.
  345. */
  346. group = (group + parent->i_ino) % ngroups;
  347. /*
  348. * Use a quadratic hash to find a group with a free inode and some
  349. * free blocks.
  350. */
  351. for (i = 1; i < ngroups; i <<= 1) {
  352. group += i;
  353. if (group >= ngroups)
  354. group -= ngroups;
  355. desc = ext2_get_group_desc (sb, group, NULL);
  356. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  357. le16_to_cpu(desc->bg_free_blocks_count))
  358. goto found;
  359. }
  360. /*
  361. * That failed: try linear search for a free inode, even if that group
  362. * has no free blocks.
  363. */
  364. group = parent_group;
  365. for (i = 0; i < ngroups; i++) {
  366. if (++group >= ngroups)
  367. group = 0;
  368. desc = ext2_get_group_desc (sb, group, NULL);
  369. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  370. goto found;
  371. }
  372. return -1;
  373. found:
  374. return group;
  375. }
  376. struct inode *ext2_new_inode(struct inode *dir, umode_t mode,
  377. const struct qstr *qstr)
  378. {
  379. struct super_block *sb;
  380. struct buffer_head *bitmap_bh = NULL;
  381. struct buffer_head *bh2;
  382. int group, i;
  383. ino_t ino = 0;
  384. struct inode * inode;
  385. struct ext2_group_desc *gdp;
  386. struct ext2_super_block *es;
  387. struct ext2_inode_info *ei;
  388. struct ext2_sb_info *sbi;
  389. int err;
  390. sb = dir->i_sb;
  391. inode = new_inode(sb);
  392. if (!inode)
  393. return ERR_PTR(-ENOMEM);
  394. ei = EXT2_I(inode);
  395. sbi = EXT2_SB(sb);
  396. es = sbi->s_es;
  397. if (S_ISDIR(mode)) {
  398. if (test_opt(sb, OLDALLOC))
  399. group = find_group_dir(sb, dir);
  400. else
  401. group = find_group_orlov(sb, dir);
  402. } else
  403. group = find_group_other(sb, dir);
  404. if (group == -1) {
  405. err = -ENOSPC;
  406. goto fail;
  407. }
  408. for (i = 0; i < sbi->s_groups_count; i++) {
  409. gdp = ext2_get_group_desc(sb, group, &bh2);
  410. if (!gdp) {
  411. if (++group == sbi->s_groups_count)
  412. group = 0;
  413. continue;
  414. }
  415. brelse(bitmap_bh);
  416. bitmap_bh = read_inode_bitmap(sb, group);
  417. if (!bitmap_bh) {
  418. err = -EIO;
  419. goto fail;
  420. }
  421. ino = 0;
  422. repeat_in_this_group:
  423. ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
  424. EXT2_INODES_PER_GROUP(sb), ino);
  425. if (ino >= EXT2_INODES_PER_GROUP(sb)) {
  426. /*
  427. * Rare race: find_group_xx() decided that there were
  428. * free inodes in this group, but by the time we tried
  429. * to allocate one, they're all gone. This can also
  430. * occur because the counters which find_group_orlov()
  431. * uses are approximate. So just go and search the
  432. * next block group.
  433. */
  434. if (++group == sbi->s_groups_count)
  435. group = 0;
  436. continue;
  437. }
  438. if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
  439. ino, bitmap_bh->b_data)) {
  440. /* we lost this inode */
  441. if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
  442. /* this group is exhausted, try next group */
  443. if (++group == sbi->s_groups_count)
  444. group = 0;
  445. continue;
  446. }
  447. /* try to find free inode in the same group */
  448. goto repeat_in_this_group;
  449. }
  450. goto got;
  451. }
  452. /*
  453. * Scanned all blockgroups.
  454. */
  455. brelse(bitmap_bh);
  456. err = -ENOSPC;
  457. goto fail;
  458. got:
  459. mark_buffer_dirty(bitmap_bh);
  460. if (sb->s_flags & SB_SYNCHRONOUS)
  461. sync_dirty_buffer(bitmap_bh);
  462. brelse(bitmap_bh);
  463. ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
  464. if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  465. ext2_error (sb, "ext2_new_inode",
  466. "reserved inode or inode > inodes count - "
  467. "block_group = %d,inode=%lu", group,
  468. (unsigned long) ino);
  469. err = -EIO;
  470. goto fail;
  471. }
  472. percpu_counter_dec(&sbi->s_freeinodes_counter);
  473. if (S_ISDIR(mode))
  474. percpu_counter_inc(&sbi->s_dirs_counter);
  475. spin_lock(sb_bgl_lock(sbi, group));
  476. le16_add_cpu(&gdp->bg_free_inodes_count, -1);
  477. if (S_ISDIR(mode)) {
  478. if (sbi->s_debts[group] < 255)
  479. sbi->s_debts[group]++;
  480. le16_add_cpu(&gdp->bg_used_dirs_count, 1);
  481. } else {
  482. if (sbi->s_debts[group])
  483. sbi->s_debts[group]--;
  484. }
  485. spin_unlock(sb_bgl_lock(sbi, group));
  486. mark_buffer_dirty(bh2);
  487. if (test_opt(sb, GRPID)) {
  488. inode->i_mode = mode;
  489. inode->i_uid = current_fsuid();
  490. inode->i_gid = dir->i_gid;
  491. } else
  492. inode_init_owner(&init_user_ns, inode, dir, mode);
  493. inode->i_ino = ino;
  494. inode->i_blocks = 0;
  495. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  496. memset(ei->i_data, 0, sizeof(ei->i_data));
  497. ei->i_flags =
  498. ext2_mask_flags(mode, EXT2_I(dir)->i_flags & EXT2_FL_INHERITED);
  499. ei->i_faddr = 0;
  500. ei->i_frag_no = 0;
  501. ei->i_frag_size = 0;
  502. ei->i_file_acl = 0;
  503. ei->i_dir_acl = 0;
  504. ei->i_dtime = 0;
  505. ei->i_block_alloc_info = NULL;
  506. ei->i_block_group = group;
  507. ei->i_dir_start_lookup = 0;
  508. ei->i_state = EXT2_STATE_NEW;
  509. ext2_set_inode_flags(inode);
  510. spin_lock(&sbi->s_next_gen_lock);
  511. inode->i_generation = sbi->s_next_generation++;
  512. spin_unlock(&sbi->s_next_gen_lock);
  513. if (insert_inode_locked(inode) < 0) {
  514. ext2_error(sb, "ext2_new_inode",
  515. "inode number already in use - inode=%lu",
  516. (unsigned long) ino);
  517. err = -EIO;
  518. goto fail;
  519. }
  520. err = dquot_initialize(inode);
  521. if (err)
  522. goto fail_drop;
  523. err = dquot_alloc_inode(inode);
  524. if (err)
  525. goto fail_drop;
  526. err = ext2_init_acl(inode, dir);
  527. if (err)
  528. goto fail_free_drop;
  529. err = ext2_init_security(inode, dir, qstr);
  530. if (err)
  531. goto fail_free_drop;
  532. mark_inode_dirty(inode);
  533. ext2_debug("allocating inode %lu\n", inode->i_ino);
  534. ext2_preread_inode(inode);
  535. return inode;
  536. fail_free_drop:
  537. dquot_free_inode(inode);
  538. fail_drop:
  539. dquot_drop(inode);
  540. inode->i_flags |= S_NOQUOTA;
  541. clear_nlink(inode);
  542. discard_new_inode(inode);
  543. return ERR_PTR(err);
  544. fail:
  545. make_bad_inode(inode);
  546. iput(inode);
  547. return ERR_PTR(err);
  548. }
  549. unsigned long ext2_count_free_inodes (struct super_block * sb)
  550. {
  551. struct ext2_group_desc *desc;
  552. unsigned long desc_count = 0;
  553. int i;
  554. #ifdef EXT2FS_DEBUG
  555. struct ext2_super_block *es;
  556. unsigned long bitmap_count = 0;
  557. struct buffer_head *bitmap_bh = NULL;
  558. es = EXT2_SB(sb)->s_es;
  559. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  560. unsigned x;
  561. desc = ext2_get_group_desc (sb, i, NULL);
  562. if (!desc)
  563. continue;
  564. desc_count += le16_to_cpu(desc->bg_free_inodes_count);
  565. brelse(bitmap_bh);
  566. bitmap_bh = read_inode_bitmap(sb, i);
  567. if (!bitmap_bh)
  568. continue;
  569. x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
  570. printk("group %d: stored = %d, counted = %u\n",
  571. i, le16_to_cpu(desc->bg_free_inodes_count), x);
  572. bitmap_count += x;
  573. }
  574. brelse(bitmap_bh);
  575. printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
  576. (unsigned long)
  577. percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
  578. desc_count, bitmap_count);
  579. return desc_count;
  580. #else
  581. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  582. desc = ext2_get_group_desc (sb, i, NULL);
  583. if (!desc)
  584. continue;
  585. desc_count += le16_to_cpu(desc->bg_free_inodes_count);
  586. }
  587. return desc_count;
  588. #endif
  589. }
  590. /* Called at mount-time, super-block is locked */
  591. unsigned long ext2_count_dirs (struct super_block * sb)
  592. {
  593. unsigned long count = 0;
  594. int i;
  595. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  596. struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
  597. if (!gdp)
  598. continue;
  599. count += le16_to_cpu(gdp->bg_used_dirs_count);
  600. }
  601. return count;
  602. }