ialloc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * linux/fs/ext3/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@redhat.com), 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/random.h>
  16. #include "ext3.h"
  17. #include "xattr.h"
  18. #include "acl.h"
  19. /*
  20. * ialloc.c contains the inodes allocation and deallocation routines
  21. */
  22. /*
  23. * The free inodes are managed by bitmaps. A file system contains several
  24. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  25. * block for inodes, N blocks for the inode table and data blocks.
  26. *
  27. * The file system contains group descriptors which are located after the
  28. * super block. Each descriptor contains the number of the bitmap block and
  29. * the free blocks count in the block.
  30. */
  31. /*
  32. * Read the inode allocation bitmap for a given block_group, reading
  33. * into the specified slot in the superblock's bitmap cache.
  34. *
  35. * Return buffer_head of bitmap on success or NULL.
  36. */
  37. static struct buffer_head *
  38. read_inode_bitmap(struct super_block * sb, unsigned long block_group)
  39. {
  40. struct ext3_group_desc *desc;
  41. struct buffer_head *bh = NULL;
  42. desc = ext3_get_group_desc(sb, block_group, NULL);
  43. if (!desc)
  44. goto error_out;
  45. bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
  46. if (!bh)
  47. ext3_error(sb, "read_inode_bitmap",
  48. "Cannot read inode bitmap - "
  49. "block_group = %lu, inode_bitmap = %u",
  50. block_group, le32_to_cpu(desc->bg_inode_bitmap));
  51. error_out:
  52. return bh;
  53. }
  54. /*
  55. * NOTE! When we get the inode, we're the only people
  56. * that have access to it, and as such there are no
  57. * race conditions we have to worry about. The inode
  58. * is not on the hash-lists, and it cannot be reached
  59. * through the filesystem because the directory entry
  60. * has been deleted earlier.
  61. *
  62. * HOWEVER: we must make sure that we get no aliases,
  63. * which means that we have to call "clear_inode()"
  64. * _before_ we mark the inode not in use in the inode
  65. * bitmaps. Otherwise a newly created file might use
  66. * the same inode number (not actually the same pointer
  67. * though), and then we'd have two inodes sharing the
  68. * same inode number and space on the harddisk.
  69. */
  70. void ext3_free_inode (handle_t *handle, struct inode * inode)
  71. {
  72. struct super_block * sb = inode->i_sb;
  73. int is_directory;
  74. unsigned long ino;
  75. struct buffer_head *bitmap_bh = NULL;
  76. struct buffer_head *bh2;
  77. unsigned long block_group;
  78. unsigned long bit;
  79. struct ext3_group_desc * gdp;
  80. struct ext3_super_block * es;
  81. struct ext3_sb_info *sbi;
  82. int fatal = 0, err;
  83. if (atomic_read(&inode->i_count) > 1) {
  84. printk ("ext3_free_inode: inode has count=%d\n",
  85. atomic_read(&inode->i_count));
  86. return;
  87. }
  88. if (inode->i_nlink) {
  89. printk ("ext3_free_inode: inode has nlink=%d\n",
  90. inode->i_nlink);
  91. return;
  92. }
  93. if (!sb) {
  94. printk("ext3_free_inode: inode on nonexistent device\n");
  95. return;
  96. }
  97. sbi = EXT3_SB(sb);
  98. ino = inode->i_ino;
  99. ext3_debug ("freeing inode %lu\n", ino);
  100. trace_ext3_free_inode(inode);
  101. is_directory = S_ISDIR(inode->i_mode);
  102. es = EXT3_SB(sb)->s_es;
  103. if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  104. ext3_error (sb, "ext3_free_inode",
  105. "reserved or nonexistent inode %lu", ino);
  106. goto error_return;
  107. }
  108. block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
  109. bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
  110. bitmap_bh = read_inode_bitmap(sb, block_group);
  111. if (!bitmap_bh)
  112. goto error_return;
  113. BUFFER_TRACE(bitmap_bh, "get_write_access");
  114. fatal = ext3_journal_get_write_access(handle, bitmap_bh);
  115. if (fatal)
  116. goto error_return;
  117. /* Ok, now we can actually update the inode bitmaps.. */
  118. if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  119. bit, bitmap_bh->b_data))
  120. ext3_error (sb, "ext3_free_inode",
  121. "bit already cleared for inode %lu", ino);
  122. else {
  123. gdp = ext3_get_group_desc (sb, block_group, &bh2);
  124. BUFFER_TRACE(bh2, "get_write_access");
  125. fatal = ext3_journal_get_write_access(handle, bh2);
  126. if (fatal) goto error_return;
  127. if (gdp) {
  128. spin_lock(sb_bgl_lock(sbi, block_group));
  129. le16_add_cpu(&gdp->bg_free_inodes_count, 1);
  130. if (is_directory)
  131. le16_add_cpu(&gdp->bg_used_dirs_count, -1);
  132. spin_unlock(sb_bgl_lock(sbi, block_group));
  133. percpu_counter_inc(&sbi->s_freeinodes_counter);
  134. if (is_directory)
  135. percpu_counter_dec(&sbi->s_dirs_counter);
  136. }
  137. BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  138. err = ext3_journal_dirty_metadata(handle, bh2);
  139. if (!fatal) fatal = err;
  140. }
  141. BUFFER_TRACE(bitmap_bh, "call ext3_journal_dirty_metadata");
  142. err = ext3_journal_dirty_metadata(handle, bitmap_bh);
  143. if (!fatal)
  144. fatal = err;
  145. error_return:
  146. brelse(bitmap_bh);
  147. ext3_std_error(sb, fatal);
  148. }
  149. /*
  150. * Orlov's allocator for directories.
  151. *
  152. * We always try to spread first-level directories.
  153. *
  154. * If there are blockgroups with both free inodes and free blocks counts
  155. * not worse than average we return one with smallest directory count.
  156. * Otherwise we simply return a random group.
  157. *
  158. * For the rest rules look so:
  159. *
  160. * It's OK to put directory into a group unless
  161. * it has too many directories already (max_dirs) or
  162. * it has too few free inodes left (min_inodes) or
  163. * it has too few free blocks left (min_blocks) or
  164. * it's already running too large debt (max_debt).
  165. * Parent's group is preferred, if it doesn't satisfy these
  166. * conditions we search cyclically through the rest. If none
  167. * of the groups look good we just look for a group with more
  168. * free inodes than average (starting at parent's group).
  169. *
  170. * Debt is incremented each time we allocate a directory and decremented
  171. * when we allocate an inode, within 0--255.
  172. */
  173. #define INODE_COST 64
  174. #define BLOCK_COST 256
  175. static int find_group_orlov(struct super_block *sb, struct inode *parent)
  176. {
  177. int parent_group = EXT3_I(parent)->i_block_group;
  178. struct ext3_sb_info *sbi = EXT3_SB(sb);
  179. struct ext3_super_block *es = sbi->s_es;
  180. int ngroups = sbi->s_groups_count;
  181. int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
  182. unsigned int freei, avefreei;
  183. ext3_fsblk_t freeb, avefreeb;
  184. ext3_fsblk_t blocks_per_dir;
  185. unsigned int ndirs;
  186. int max_debt, max_dirs, min_inodes;
  187. ext3_grpblk_t min_blocks;
  188. int group = -1, i;
  189. struct ext3_group_desc *desc;
  190. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  191. avefreei = freei / ngroups;
  192. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  193. avefreeb = freeb / ngroups;
  194. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  195. if ((parent == sb->s_root->d_inode) ||
  196. (EXT3_I(parent)->i_flags & EXT3_TOPDIR_FL)) {
  197. int best_ndir = inodes_per_group;
  198. int best_group = -1;
  199. get_random_bytes(&group, sizeof(group));
  200. parent_group = (unsigned)group % ngroups;
  201. for (i = 0; i < ngroups; i++) {
  202. group = (parent_group + i) % ngroups;
  203. desc = ext3_get_group_desc (sb, group, NULL);
  204. if (!desc || !desc->bg_free_inodes_count)
  205. continue;
  206. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  207. continue;
  208. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  209. continue;
  210. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  211. continue;
  212. best_group = group;
  213. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  214. }
  215. if (best_group >= 0)
  216. return best_group;
  217. goto fallback;
  218. }
  219. blocks_per_dir = (le32_to_cpu(es->s_blocks_count) - freeb) / ndirs;
  220. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  221. min_inodes = avefreei - inodes_per_group / 4;
  222. min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;
  223. max_debt = EXT3_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, (ext3_fsblk_t)BLOCK_COST);
  224. if (max_debt * INODE_COST > inodes_per_group)
  225. max_debt = inodes_per_group / INODE_COST;
  226. if (max_debt > 255)
  227. max_debt = 255;
  228. if (max_debt == 0)
  229. max_debt = 1;
  230. for (i = 0; i < ngroups; i++) {
  231. group = (parent_group + i) % ngroups;
  232. desc = ext3_get_group_desc (sb, group, NULL);
  233. if (!desc || !desc->bg_free_inodes_count)
  234. continue;
  235. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  236. continue;
  237. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  238. continue;
  239. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  240. continue;
  241. return group;
  242. }
  243. fallback:
  244. for (i = 0; i < ngroups; i++) {
  245. group = (parent_group + i) % ngroups;
  246. desc = ext3_get_group_desc (sb, group, NULL);
  247. if (!desc || !desc->bg_free_inodes_count)
  248. continue;
  249. if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  250. return group;
  251. }
  252. if (avefreei) {
  253. /*
  254. * The free-inodes counter is approximate, and for really small
  255. * filesystems the above test can fail to find any blockgroups
  256. */
  257. avefreei = 0;
  258. goto fallback;
  259. }
  260. return -1;
  261. }
  262. static int find_group_other(struct super_block *sb, struct inode *parent)
  263. {
  264. int parent_group = EXT3_I(parent)->i_block_group;
  265. int ngroups = EXT3_SB(sb)->s_groups_count;
  266. struct ext3_group_desc *desc;
  267. int group, i;
  268. /*
  269. * Try to place the inode in its parent directory
  270. */
  271. group = parent_group;
  272. desc = ext3_get_group_desc (sb, group, NULL);
  273. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  274. le16_to_cpu(desc->bg_free_blocks_count))
  275. return group;
  276. /*
  277. * We're going to place this inode in a different blockgroup from its
  278. * parent. We want to cause files in a common directory to all land in
  279. * the same blockgroup. But we want files which are in a different
  280. * directory which shares a blockgroup with our parent to land in a
  281. * different blockgroup.
  282. *
  283. * So add our directory's i_ino into the starting point for the hash.
  284. */
  285. group = (group + parent->i_ino) % ngroups;
  286. /*
  287. * Use a quadratic hash to find a group with a free inode and some free
  288. * blocks.
  289. */
  290. for (i = 1; i < ngroups; i <<= 1) {
  291. group += i;
  292. if (group >= ngroups)
  293. group -= ngroups;
  294. desc = ext3_get_group_desc (sb, group, NULL);
  295. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  296. le16_to_cpu(desc->bg_free_blocks_count))
  297. return group;
  298. }
  299. /*
  300. * That failed: try linear search for a free inode, even if that group
  301. * has no free blocks.
  302. */
  303. group = parent_group;
  304. for (i = 0; i < ngroups; i++) {
  305. if (++group >= ngroups)
  306. group = 0;
  307. desc = ext3_get_group_desc (sb, group, NULL);
  308. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  309. return group;
  310. }
  311. return -1;
  312. }
  313. /*
  314. * There are two policies for allocating an inode. If the new inode is
  315. * a directory, then a forward search is made for a block group with both
  316. * free space and a low directory-to-inode ratio; if that fails, then of
  317. * the groups with above-average free space, that group with the fewest
  318. * directories already is chosen.
  319. *
  320. * For other inodes, search forward from the parent directory's block
  321. * group to find a free inode.
  322. */
  323. struct inode *ext3_new_inode(handle_t *handle, struct inode * dir,
  324. const struct qstr *qstr, umode_t mode)
  325. {
  326. struct super_block *sb;
  327. struct buffer_head *bitmap_bh = NULL;
  328. struct buffer_head *bh2;
  329. int group;
  330. unsigned long ino = 0;
  331. struct inode * inode;
  332. struct ext3_group_desc * gdp = NULL;
  333. struct ext3_super_block * es;
  334. struct ext3_inode_info *ei;
  335. struct ext3_sb_info *sbi;
  336. int err = 0;
  337. struct inode *ret;
  338. int i;
  339. /* Cannot create files in a deleted directory */
  340. if (!dir || !dir->i_nlink)
  341. return ERR_PTR(-EPERM);
  342. sb = dir->i_sb;
  343. trace_ext3_request_inode(dir, mode);
  344. inode = new_inode(sb);
  345. if (!inode)
  346. return ERR_PTR(-ENOMEM);
  347. ei = EXT3_I(inode);
  348. sbi = EXT3_SB(sb);
  349. es = sbi->s_es;
  350. if (S_ISDIR(mode))
  351. group = find_group_orlov(sb, dir);
  352. else
  353. group = find_group_other(sb, dir);
  354. err = -ENOSPC;
  355. if (group == -1)
  356. goto out;
  357. for (i = 0; i < sbi->s_groups_count; i++) {
  358. err = -EIO;
  359. gdp = ext3_get_group_desc(sb, group, &bh2);
  360. if (!gdp)
  361. goto fail;
  362. brelse(bitmap_bh);
  363. bitmap_bh = read_inode_bitmap(sb, group);
  364. if (!bitmap_bh)
  365. goto fail;
  366. ino = 0;
  367. repeat_in_this_group:
  368. ino = ext3_find_next_zero_bit((unsigned long *)
  369. bitmap_bh->b_data, EXT3_INODES_PER_GROUP(sb), ino);
  370. if (ino < EXT3_INODES_PER_GROUP(sb)) {
  371. BUFFER_TRACE(bitmap_bh, "get_write_access");
  372. err = ext3_journal_get_write_access(handle, bitmap_bh);
  373. if (err)
  374. goto fail;
  375. if (!ext3_set_bit_atomic(sb_bgl_lock(sbi, group),
  376. ino, bitmap_bh->b_data)) {
  377. /* we won it */
  378. BUFFER_TRACE(bitmap_bh,
  379. "call ext3_journal_dirty_metadata");
  380. err = ext3_journal_dirty_metadata(handle,
  381. bitmap_bh);
  382. if (err)
  383. goto fail;
  384. goto got;
  385. }
  386. /* we lost it */
  387. journal_release_buffer(handle, bitmap_bh);
  388. if (++ino < EXT3_INODES_PER_GROUP(sb))
  389. goto repeat_in_this_group;
  390. }
  391. /*
  392. * This case is possible in concurrent environment. It is very
  393. * rare. We cannot repeat the find_group_xxx() call because
  394. * that will simply return the same blockgroup, because the
  395. * group descriptor metadata has not yet been updated.
  396. * So we just go onto the next blockgroup.
  397. */
  398. if (++group == sbi->s_groups_count)
  399. group = 0;
  400. }
  401. err = -ENOSPC;
  402. goto out;
  403. got:
  404. ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
  405. if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  406. ext3_error (sb, "ext3_new_inode",
  407. "reserved inode or inode > inodes count - "
  408. "block_group = %d, inode=%lu", group, ino);
  409. err = -EIO;
  410. goto fail;
  411. }
  412. BUFFER_TRACE(bh2, "get_write_access");
  413. err = ext3_journal_get_write_access(handle, bh2);
  414. if (err) goto fail;
  415. spin_lock(sb_bgl_lock(sbi, group));
  416. le16_add_cpu(&gdp->bg_free_inodes_count, -1);
  417. if (S_ISDIR(mode)) {
  418. le16_add_cpu(&gdp->bg_used_dirs_count, 1);
  419. }
  420. spin_unlock(sb_bgl_lock(sbi, group));
  421. BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  422. err = ext3_journal_dirty_metadata(handle, bh2);
  423. if (err) goto fail;
  424. percpu_counter_dec(&sbi->s_freeinodes_counter);
  425. if (S_ISDIR(mode))
  426. percpu_counter_inc(&sbi->s_dirs_counter);
  427. if (test_opt(sb, GRPID)) {
  428. inode->i_mode = mode;
  429. inode->i_uid = current_fsuid();
  430. inode->i_gid = dir->i_gid;
  431. } else
  432. inode_init_owner(inode, dir, mode);
  433. inode->i_ino = ino;
  434. /* This is the optimal IO size (for stat), not the fs block size */
  435. inode->i_blocks = 0;
  436. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  437. memset(ei->i_data, 0, sizeof(ei->i_data));
  438. ei->i_dir_start_lookup = 0;
  439. ei->i_disksize = 0;
  440. ei->i_flags =
  441. ext3_mask_flags(mode, EXT3_I(dir)->i_flags & EXT3_FL_INHERITED);
  442. #ifdef EXT3_FRAGMENTS
  443. ei->i_faddr = 0;
  444. ei->i_frag_no = 0;
  445. ei->i_frag_size = 0;
  446. #endif
  447. ei->i_file_acl = 0;
  448. ei->i_dir_acl = 0;
  449. ei->i_dtime = 0;
  450. ei->i_block_alloc_info = NULL;
  451. ei->i_block_group = group;
  452. ext3_set_inode_flags(inode);
  453. if (IS_DIRSYNC(inode))
  454. handle->h_sync = 1;
  455. if (insert_inode_locked(inode) < 0) {
  456. /*
  457. * Likely a bitmap corruption causing inode to be allocated
  458. * twice.
  459. */
  460. err = -EIO;
  461. goto fail;
  462. }
  463. spin_lock(&sbi->s_next_gen_lock);
  464. inode->i_generation = sbi->s_next_generation++;
  465. spin_unlock(&sbi->s_next_gen_lock);
  466. ei->i_state_flags = 0;
  467. ext3_set_inode_state(inode, EXT3_STATE_NEW);
  468. /* See comment in ext3_iget for explanation */
  469. if (ino >= EXT3_FIRST_INO(sb) + 1 &&
  470. EXT3_INODE_SIZE(sb) > EXT3_GOOD_OLD_INODE_SIZE) {
  471. ei->i_extra_isize =
  472. sizeof(struct ext3_inode) - EXT3_GOOD_OLD_INODE_SIZE;
  473. } else {
  474. ei->i_extra_isize = 0;
  475. }
  476. ret = inode;
  477. dquot_initialize(inode);
  478. err = dquot_alloc_inode(inode);
  479. if (err)
  480. goto fail_drop;
  481. err = ext3_init_acl(handle, inode, dir);
  482. if (err)
  483. goto fail_free_drop;
  484. err = ext3_init_security(handle, inode, dir, qstr);
  485. if (err)
  486. goto fail_free_drop;
  487. err = ext3_mark_inode_dirty(handle, inode);
  488. if (err) {
  489. ext3_std_error(sb, err);
  490. goto fail_free_drop;
  491. }
  492. ext3_debug("allocating inode %lu\n", inode->i_ino);
  493. trace_ext3_allocate_inode(inode, dir, mode);
  494. goto really_out;
  495. fail:
  496. ext3_std_error(sb, err);
  497. out:
  498. iput(inode);
  499. ret = ERR_PTR(err);
  500. really_out:
  501. brelse(bitmap_bh);
  502. return ret;
  503. fail_free_drop:
  504. dquot_free_inode(inode);
  505. fail_drop:
  506. dquot_drop(inode);
  507. inode->i_flags |= S_NOQUOTA;
  508. clear_nlink(inode);
  509. unlock_new_inode(inode);
  510. iput(inode);
  511. brelse(bitmap_bh);
  512. return ERR_PTR(err);
  513. }
  514. /* Verify that we are loading a valid orphan from disk */
  515. struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
  516. {
  517. unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
  518. unsigned long block_group;
  519. int bit;
  520. struct buffer_head *bitmap_bh;
  521. struct inode *inode = NULL;
  522. long err = -EIO;
  523. /* Error cases - e2fsck has already cleaned up for us */
  524. if (ino > max_ino) {
  525. ext3_warning(sb, __func__,
  526. "bad orphan ino %lu! e2fsck was run?", ino);
  527. goto error;
  528. }
  529. block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
  530. bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
  531. bitmap_bh = read_inode_bitmap(sb, block_group);
  532. if (!bitmap_bh) {
  533. ext3_warning(sb, __func__,
  534. "inode bitmap error for orphan %lu", ino);
  535. goto error;
  536. }
  537. /* Having the inode bit set should be a 100% indicator that this
  538. * is a valid orphan (no e2fsck run on fs). Orphans also include
  539. * inodes that were being truncated, so we can't check i_nlink==0.
  540. */
  541. if (!ext3_test_bit(bit, bitmap_bh->b_data))
  542. goto bad_orphan;
  543. inode = ext3_iget(sb, ino);
  544. if (IS_ERR(inode))
  545. goto iget_failed;
  546. /*
  547. * If the orphans has i_nlinks > 0 then it should be able to be
  548. * truncated, otherwise it won't be removed from the orphan list
  549. * during processing and an infinite loop will result.
  550. */
  551. if (inode->i_nlink && !ext3_can_truncate(inode))
  552. goto bad_orphan;
  553. if (NEXT_ORPHAN(inode) > max_ino)
  554. goto bad_orphan;
  555. brelse(bitmap_bh);
  556. return inode;
  557. iget_failed:
  558. err = PTR_ERR(inode);
  559. inode = NULL;
  560. bad_orphan:
  561. ext3_warning(sb, __func__,
  562. "bad orphan inode %lu! e2fsck was run?", ino);
  563. printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%llu) = %d\n",
  564. bit, (unsigned long long)bitmap_bh->b_blocknr,
  565. ext3_test_bit(bit, bitmap_bh->b_data));
  566. printk(KERN_NOTICE "inode=%p\n", inode);
  567. if (inode) {
  568. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  569. is_bad_inode(inode));
  570. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  571. NEXT_ORPHAN(inode));
  572. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  573. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  574. /* Avoid freeing blocks if we got a bad deleted inode */
  575. if (inode->i_nlink == 0)
  576. inode->i_blocks = 0;
  577. iput(inode);
  578. }
  579. brelse(bitmap_bh);
  580. error:
  581. return ERR_PTR(err);
  582. }
  583. unsigned long ext3_count_free_inodes (struct super_block * sb)
  584. {
  585. unsigned long desc_count;
  586. struct ext3_group_desc *gdp;
  587. int i;
  588. #ifdef EXT3FS_DEBUG
  589. struct ext3_super_block *es;
  590. unsigned long bitmap_count, x;
  591. struct buffer_head *bitmap_bh = NULL;
  592. es = EXT3_SB(sb)->s_es;
  593. desc_count = 0;
  594. bitmap_count = 0;
  595. gdp = NULL;
  596. for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
  597. gdp = ext3_get_group_desc (sb, i, NULL);
  598. if (!gdp)
  599. continue;
  600. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  601. brelse(bitmap_bh);
  602. bitmap_bh = read_inode_bitmap(sb, i);
  603. if (!bitmap_bh)
  604. continue;
  605. x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
  606. printk("group %d: stored = %d, counted = %lu\n",
  607. i, le16_to_cpu(gdp->bg_free_inodes_count), x);
  608. bitmap_count += x;
  609. }
  610. brelse(bitmap_bh);
  611. printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
  612. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  613. return desc_count;
  614. #else
  615. desc_count = 0;
  616. for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
  617. gdp = ext3_get_group_desc (sb, i, NULL);
  618. if (!gdp)
  619. continue;
  620. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  621. cond_resched();
  622. }
  623. return desc_count;
  624. #endif
  625. }
  626. /* Called at mount-time, super-block is locked */
  627. unsigned long ext3_count_dirs (struct super_block * sb)
  628. {
  629. unsigned long count = 0;
  630. int i;
  631. for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
  632. struct ext3_group_desc *gdp = ext3_get_group_desc (sb, i, NULL);
  633. if (!gdp)
  634. continue;
  635. count += le16_to_cpu(gdp->bg_used_dirs_count);
  636. }
  637. return count;
  638. }