ialloc.c 20 KB

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