ialloc.c 18 KB

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