ialloc.c 18 KB

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