ialloc.c 18 KB

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