ialloc.c 21 KB

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