ialloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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).
  164. * Parent's group is preferred, if it doesn't satisfy these
  165. * conditions we search cyclically through the rest. If none
  166. * of the groups look good we just look for a group with more
  167. * free inodes than average (starting at parent's group).
  168. *
  169. * Debt is incremented each time we allocate a directory and decremented
  170. * when we allocate an inode, within 0--255.
  171. */
  172. static int find_group_orlov(struct super_block *sb, struct inode *parent)
  173. {
  174. int parent_group = EXT3_I(parent)->i_block_group;
  175. struct ext3_sb_info *sbi = EXT3_SB(sb);
  176. int ngroups = sbi->s_groups_count;
  177. int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
  178. unsigned int freei, avefreei;
  179. ext3_fsblk_t freeb, avefreeb;
  180. unsigned int ndirs;
  181. int max_dirs, min_inodes;
  182. ext3_grpblk_t min_blocks;
  183. int group = -1, i;
  184. struct ext3_group_desc *desc;
  185. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  186. avefreei = freei / ngroups;
  187. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  188. avefreeb = freeb / ngroups;
  189. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  190. if ((parent == d_inode(sb->s_root)) ||
  191. (EXT3_I(parent)->i_flags & EXT3_TOPDIR_FL)) {
  192. int best_ndir = inodes_per_group;
  193. int best_group = -1;
  194. group = prandom_u32();
  195. parent_group = (unsigned)group % ngroups;
  196. for (i = 0; i < ngroups; i++) {
  197. group = (parent_group + i) % ngroups;
  198. desc = ext3_get_group_desc (sb, group, NULL);
  199. if (!desc || !desc->bg_free_inodes_count)
  200. continue;
  201. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  202. continue;
  203. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  204. continue;
  205. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  206. continue;
  207. best_group = group;
  208. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  209. }
  210. if (best_group >= 0)
  211. return best_group;
  212. goto fallback;
  213. }
  214. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  215. min_inodes = avefreei - inodes_per_group / 4;
  216. min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;
  217. for (i = 0; i < ngroups; i++) {
  218. group = (parent_group + i) % ngroups;
  219. desc = ext3_get_group_desc (sb, group, NULL);
  220. if (!desc || !desc->bg_free_inodes_count)
  221. continue;
  222. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  223. continue;
  224. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  225. continue;
  226. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  227. continue;
  228. return group;
  229. }
  230. fallback:
  231. for (i = 0; i < ngroups; i++) {
  232. group = (parent_group + i) % ngroups;
  233. desc = ext3_get_group_desc (sb, group, NULL);
  234. if (!desc || !desc->bg_free_inodes_count)
  235. continue;
  236. if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  237. return group;
  238. }
  239. if (avefreei) {
  240. /*
  241. * The free-inodes counter is approximate, and for really small
  242. * filesystems the above test can fail to find any blockgroups
  243. */
  244. avefreei = 0;
  245. goto fallback;
  246. }
  247. return -1;
  248. }
  249. static int find_group_other(struct super_block *sb, struct inode *parent)
  250. {
  251. int parent_group = EXT3_I(parent)->i_block_group;
  252. int ngroups = EXT3_SB(sb)->s_groups_count;
  253. struct ext3_group_desc *desc;
  254. int group, i;
  255. /*
  256. * Try to place the inode in its parent directory
  257. */
  258. group = parent_group;
  259. desc = ext3_get_group_desc (sb, group, NULL);
  260. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  261. le16_to_cpu(desc->bg_free_blocks_count))
  262. return group;
  263. /*
  264. * We're going to place this inode in a different blockgroup from its
  265. * parent. We want to cause files in a common directory to all land in
  266. * the same blockgroup. But we want files which are in a different
  267. * directory which shares a blockgroup with our parent to land in a
  268. * different blockgroup.
  269. *
  270. * So add our directory's i_ino into the starting point for the hash.
  271. */
  272. group = (group + parent->i_ino) % ngroups;
  273. /*
  274. * Use a quadratic hash to find a group with a free inode and some free
  275. * blocks.
  276. */
  277. for (i = 1; i < ngroups; i <<= 1) {
  278. group += i;
  279. if (group >= ngroups)
  280. group -= ngroups;
  281. desc = ext3_get_group_desc (sb, group, NULL);
  282. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  283. le16_to_cpu(desc->bg_free_blocks_count))
  284. return group;
  285. }
  286. /*
  287. * That failed: try linear search for a free inode, even if that group
  288. * has no free blocks.
  289. */
  290. group = parent_group;
  291. for (i = 0; i < ngroups; i++) {
  292. if (++group >= ngroups)
  293. group = 0;
  294. desc = ext3_get_group_desc (sb, group, NULL);
  295. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  296. return group;
  297. }
  298. return -1;
  299. }
  300. /*
  301. * There are two policies for allocating an inode. If the new inode is
  302. * a directory, then a forward search is made for a block group with both
  303. * free space and a low directory-to-inode ratio; if that fails, then of
  304. * the groups with above-average free space, that group with the fewest
  305. * directories already is chosen.
  306. *
  307. * For other inodes, search forward from the parent directory's block
  308. * group to find a free inode.
  309. */
  310. struct inode *ext3_new_inode(handle_t *handle, struct inode * dir,
  311. const struct qstr *qstr, umode_t mode)
  312. {
  313. struct super_block *sb;
  314. struct buffer_head *bitmap_bh = NULL;
  315. struct buffer_head *bh2;
  316. int group;
  317. unsigned long ino = 0;
  318. struct inode * inode;
  319. struct ext3_group_desc * gdp = NULL;
  320. struct ext3_super_block * es;
  321. struct ext3_inode_info *ei;
  322. struct ext3_sb_info *sbi;
  323. int err = 0;
  324. struct inode *ret;
  325. int i;
  326. /* Cannot create files in a deleted directory */
  327. if (!dir || !dir->i_nlink)
  328. return ERR_PTR(-EPERM);
  329. sb = dir->i_sb;
  330. trace_ext3_request_inode(dir, mode);
  331. inode = new_inode(sb);
  332. if (!inode)
  333. return ERR_PTR(-ENOMEM);
  334. ei = EXT3_I(inode);
  335. sbi = EXT3_SB(sb);
  336. es = sbi->s_es;
  337. if (S_ISDIR(mode))
  338. group = find_group_orlov(sb, dir);
  339. else
  340. group = find_group_other(sb, dir);
  341. err = -ENOSPC;
  342. if (group == -1)
  343. goto out;
  344. for (i = 0; i < sbi->s_groups_count; i++) {
  345. err = -EIO;
  346. gdp = ext3_get_group_desc(sb, group, &bh2);
  347. if (!gdp)
  348. goto fail;
  349. brelse(bitmap_bh);
  350. bitmap_bh = read_inode_bitmap(sb, group);
  351. if (!bitmap_bh)
  352. goto fail;
  353. ino = 0;
  354. repeat_in_this_group:
  355. ino = ext3_find_next_zero_bit((unsigned long *)
  356. bitmap_bh->b_data, EXT3_INODES_PER_GROUP(sb), ino);
  357. if (ino < EXT3_INODES_PER_GROUP(sb)) {
  358. BUFFER_TRACE(bitmap_bh, "get_write_access");
  359. err = ext3_journal_get_write_access(handle, bitmap_bh);
  360. if (err)
  361. goto fail;
  362. if (!ext3_set_bit_atomic(sb_bgl_lock(sbi, group),
  363. ino, bitmap_bh->b_data)) {
  364. /* we won it */
  365. BUFFER_TRACE(bitmap_bh,
  366. "call ext3_journal_dirty_metadata");
  367. err = ext3_journal_dirty_metadata(handle,
  368. bitmap_bh);
  369. if (err)
  370. goto fail;
  371. goto got;
  372. }
  373. /* we lost it */
  374. journal_release_buffer(handle, bitmap_bh);
  375. if (++ino < EXT3_INODES_PER_GROUP(sb))
  376. goto repeat_in_this_group;
  377. }
  378. /*
  379. * This case is possible in concurrent environment. It is very
  380. * rare. We cannot repeat the find_group_xxx() call because
  381. * that will simply return the same blockgroup, because the
  382. * group descriptor metadata has not yet been updated.
  383. * So we just go onto the next blockgroup.
  384. */
  385. if (++group == sbi->s_groups_count)
  386. group = 0;
  387. }
  388. err = -ENOSPC;
  389. goto out;
  390. got:
  391. ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
  392. if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  393. ext3_error (sb, "ext3_new_inode",
  394. "reserved inode or inode > inodes count - "
  395. "block_group = %d, inode=%lu", group, ino);
  396. err = -EIO;
  397. goto fail;
  398. }
  399. BUFFER_TRACE(bh2, "get_write_access");
  400. err = ext3_journal_get_write_access(handle, bh2);
  401. if (err) goto fail;
  402. spin_lock(sb_bgl_lock(sbi, group));
  403. le16_add_cpu(&gdp->bg_free_inodes_count, -1);
  404. if (S_ISDIR(mode)) {
  405. le16_add_cpu(&gdp->bg_used_dirs_count, 1);
  406. }
  407. spin_unlock(sb_bgl_lock(sbi, group));
  408. BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  409. err = ext3_journal_dirty_metadata(handle, bh2);
  410. if (err) goto fail;
  411. percpu_counter_dec(&sbi->s_freeinodes_counter);
  412. if (S_ISDIR(mode))
  413. percpu_counter_inc(&sbi->s_dirs_counter);
  414. if (test_opt(sb, GRPID)) {
  415. inode->i_mode = mode;
  416. inode->i_uid = current_fsuid();
  417. inode->i_gid = dir->i_gid;
  418. } else
  419. inode_init_owner(inode, dir, mode);
  420. inode->i_ino = ino;
  421. /* This is the optimal IO size (for stat), not the fs block size */
  422. inode->i_blocks = 0;
  423. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  424. memset(ei->i_data, 0, sizeof(ei->i_data));
  425. ei->i_dir_start_lookup = 0;
  426. ei->i_disksize = 0;
  427. ei->i_flags =
  428. ext3_mask_flags(mode, EXT3_I(dir)->i_flags & EXT3_FL_INHERITED);
  429. #ifdef EXT3_FRAGMENTS
  430. ei->i_faddr = 0;
  431. ei->i_frag_no = 0;
  432. ei->i_frag_size = 0;
  433. #endif
  434. ei->i_file_acl = 0;
  435. ei->i_dir_acl = 0;
  436. ei->i_dtime = 0;
  437. ei->i_block_alloc_info = NULL;
  438. ei->i_block_group = group;
  439. ext3_set_inode_flags(inode);
  440. if (IS_DIRSYNC(inode))
  441. handle->h_sync = 1;
  442. if (insert_inode_locked(inode) < 0) {
  443. /*
  444. * Likely a bitmap corruption causing inode to be allocated
  445. * twice.
  446. */
  447. err = -EIO;
  448. goto fail;
  449. }
  450. spin_lock(&sbi->s_next_gen_lock);
  451. inode->i_generation = sbi->s_next_generation++;
  452. spin_unlock(&sbi->s_next_gen_lock);
  453. ei->i_state_flags = 0;
  454. ext3_set_inode_state(inode, EXT3_STATE_NEW);
  455. /* See comment in ext3_iget for explanation */
  456. if (ino >= EXT3_FIRST_INO(sb) + 1 &&
  457. EXT3_INODE_SIZE(sb) > EXT3_GOOD_OLD_INODE_SIZE) {
  458. ei->i_extra_isize =
  459. sizeof(struct ext3_inode) - EXT3_GOOD_OLD_INODE_SIZE;
  460. } else {
  461. ei->i_extra_isize = 0;
  462. }
  463. ret = inode;
  464. dquot_initialize(inode);
  465. err = dquot_alloc_inode(inode);
  466. if (err)
  467. goto fail_drop;
  468. err = ext3_init_acl(handle, inode, dir);
  469. if (err)
  470. goto fail_free_drop;
  471. err = ext3_init_security(handle, inode, dir, qstr);
  472. if (err)
  473. goto fail_free_drop;
  474. err = ext3_mark_inode_dirty(handle, inode);
  475. if (err) {
  476. ext3_std_error(sb, err);
  477. goto fail_free_drop;
  478. }
  479. ext3_debug("allocating inode %lu\n", inode->i_ino);
  480. trace_ext3_allocate_inode(inode, dir, mode);
  481. goto really_out;
  482. fail:
  483. ext3_std_error(sb, err);
  484. out:
  485. iput(inode);
  486. ret = ERR_PTR(err);
  487. really_out:
  488. brelse(bitmap_bh);
  489. return ret;
  490. fail_free_drop:
  491. dquot_free_inode(inode);
  492. fail_drop:
  493. dquot_drop(inode);
  494. inode->i_flags |= S_NOQUOTA;
  495. clear_nlink(inode);
  496. unlock_new_inode(inode);
  497. iput(inode);
  498. brelse(bitmap_bh);
  499. return ERR_PTR(err);
  500. }
  501. /* Verify that we are loading a valid orphan from disk */
  502. struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
  503. {
  504. unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
  505. unsigned long block_group;
  506. int bit;
  507. struct buffer_head *bitmap_bh;
  508. struct inode *inode = NULL;
  509. long err = -EIO;
  510. /* Error cases - e2fsck has already cleaned up for us */
  511. if (ino > max_ino) {
  512. ext3_warning(sb, __func__,
  513. "bad orphan ino %lu! e2fsck was run?", ino);
  514. goto error;
  515. }
  516. block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
  517. bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
  518. bitmap_bh = read_inode_bitmap(sb, block_group);
  519. if (!bitmap_bh) {
  520. ext3_warning(sb, __func__,
  521. "inode bitmap error for orphan %lu", ino);
  522. goto error;
  523. }
  524. /* Having the inode bit set should be a 100% indicator that this
  525. * is a valid orphan (no e2fsck run on fs). Orphans also include
  526. * inodes that were being truncated, so we can't check i_nlink==0.
  527. */
  528. if (!ext3_test_bit(bit, bitmap_bh->b_data))
  529. goto bad_orphan;
  530. inode = ext3_iget(sb, ino);
  531. if (IS_ERR(inode))
  532. goto iget_failed;
  533. /*
  534. * If the orphans has i_nlinks > 0 then it should be able to be
  535. * truncated, otherwise it won't be removed from the orphan list
  536. * during processing and an infinite loop will result.
  537. */
  538. if (inode->i_nlink && !ext3_can_truncate(inode))
  539. goto bad_orphan;
  540. if (NEXT_ORPHAN(inode) > max_ino)
  541. goto bad_orphan;
  542. brelse(bitmap_bh);
  543. return inode;
  544. iget_failed:
  545. err = PTR_ERR(inode);
  546. inode = NULL;
  547. bad_orphan:
  548. ext3_warning(sb, __func__,
  549. "bad orphan inode %lu! e2fsck was run?", ino);
  550. printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%llu) = %d\n",
  551. bit, (unsigned long long)bitmap_bh->b_blocknr,
  552. ext3_test_bit(bit, bitmap_bh->b_data));
  553. printk(KERN_NOTICE "inode=%p\n", inode);
  554. if (inode) {
  555. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  556. is_bad_inode(inode));
  557. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  558. NEXT_ORPHAN(inode));
  559. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  560. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  561. /* Avoid freeing blocks if we got a bad deleted inode */
  562. if (inode->i_nlink == 0)
  563. inode->i_blocks = 0;
  564. iput(inode);
  565. }
  566. brelse(bitmap_bh);
  567. error:
  568. return ERR_PTR(err);
  569. }
  570. unsigned long ext3_count_free_inodes (struct super_block * sb)
  571. {
  572. unsigned long desc_count;
  573. struct ext3_group_desc *gdp;
  574. int i;
  575. #ifdef EXT3FS_DEBUG
  576. struct ext3_super_block *es;
  577. unsigned long bitmap_count, x;
  578. struct buffer_head *bitmap_bh = NULL;
  579. es = EXT3_SB(sb)->s_es;
  580. desc_count = 0;
  581. bitmap_count = 0;
  582. gdp = NULL;
  583. for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
  584. gdp = ext3_get_group_desc (sb, i, NULL);
  585. if (!gdp)
  586. continue;
  587. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  588. brelse(bitmap_bh);
  589. bitmap_bh = read_inode_bitmap(sb, i);
  590. if (!bitmap_bh)
  591. continue;
  592. x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
  593. printk("group %d: stored = %d, counted = %lu\n",
  594. i, le16_to_cpu(gdp->bg_free_inodes_count), x);
  595. bitmap_count += x;
  596. }
  597. brelse(bitmap_bh);
  598. printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
  599. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  600. return desc_count;
  601. #else
  602. desc_count = 0;
  603. for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
  604. gdp = ext3_get_group_desc (sb, i, NULL);
  605. if (!gdp)
  606. continue;
  607. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  608. cond_resched();
  609. }
  610. return desc_count;
  611. #endif
  612. }
  613. /* Called at mount-time, super-block is locked */
  614. unsigned long ext3_count_dirs (struct super_block * sb)
  615. {
  616. unsigned long count = 0;
  617. int i;
  618. for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
  619. struct ext3_group_desc *gdp = ext3_get_group_desc (sb, i, NULL);
  620. if (!gdp)
  621. continue;
  622. count += le16_to_cpu(gdp->bg_used_dirs_count);
  623. }
  624. return count;
  625. }