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