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 "bitmap.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. /*
  111. * Note: we must free any quota before locking the superblock,
  112. * as writing the quota to disk may need the lock as well.
  113. */
  114. DQUOT_INIT(inode);
  115. ext3_xattr_delete_inode(handle, inode);
  116. DQUOT_FREE_INODE(inode);
  117. DQUOT_DROP(inode);
  118. is_directory = S_ISDIR(inode->i_mode);
  119. /* Do this BEFORE marking the inode not in use or returning an error */
  120. clear_inode (inode);
  121. es = EXT3_SB(sb)->s_es;
  122. if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  123. ext3_error (sb, "ext3_free_inode",
  124. "reserved or nonexistent inode %lu", ino);
  125. goto error_return;
  126. }
  127. block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
  128. bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
  129. bitmap_bh = read_inode_bitmap(sb, block_group);
  130. if (!bitmap_bh)
  131. goto error_return;
  132. BUFFER_TRACE(bitmap_bh, "get_write_access");
  133. fatal = ext3_journal_get_write_access(handle, bitmap_bh);
  134. if (fatal)
  135. goto error_return;
  136. /* Ok, now we can actually update the inode bitmaps.. */
  137. if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  138. bit, bitmap_bh->b_data))
  139. ext3_error (sb, "ext3_free_inode",
  140. "bit already cleared for inode %lu", ino);
  141. else {
  142. gdp = ext3_get_group_desc (sb, block_group, &bh2);
  143. BUFFER_TRACE(bh2, "get_write_access");
  144. fatal = ext3_journal_get_write_access(handle, bh2);
  145. if (fatal) goto error_return;
  146. if (gdp) {
  147. spin_lock(sb_bgl_lock(sbi, block_group));
  148. gdp->bg_free_inodes_count = cpu_to_le16(
  149. le16_to_cpu(gdp->bg_free_inodes_count) + 1);
  150. if (is_directory)
  151. gdp->bg_used_dirs_count = cpu_to_le16(
  152. le16_to_cpu(gdp->bg_used_dirs_count) - 1);
  153. spin_unlock(sb_bgl_lock(sbi, block_group));
  154. percpu_counter_inc(&sbi->s_freeinodes_counter);
  155. if (is_directory)
  156. percpu_counter_dec(&sbi->s_dirs_counter);
  157. }
  158. BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  159. err = ext3_journal_dirty_metadata(handle, bh2);
  160. if (!fatal) fatal = err;
  161. }
  162. BUFFER_TRACE(bitmap_bh, "call ext3_journal_dirty_metadata");
  163. err = ext3_journal_dirty_metadata(handle, bitmap_bh);
  164. if (!fatal)
  165. fatal = err;
  166. sb->s_dirt = 1;
  167. error_return:
  168. brelse(bitmap_bh);
  169. ext3_std_error(sb, fatal);
  170. }
  171. /*
  172. * There are two policies for allocating an inode. If the new inode is
  173. * a directory, then a forward search is made for a block group with both
  174. * free space and a low directory-to-inode ratio; if that fails, then of
  175. * the groups with above-average free space, that group with the fewest
  176. * directories already is chosen.
  177. *
  178. * For other inodes, search forward from the parent directory\'s block
  179. * group to find a free inode.
  180. */
  181. static int find_group_dir(struct super_block *sb, struct inode *parent)
  182. {
  183. int ngroups = EXT3_SB(sb)->s_groups_count;
  184. int freei, avefreei;
  185. struct ext3_group_desc *desc, *best_desc = NULL;
  186. struct buffer_head *bh;
  187. int group, best_group = -1;
  188. freei = percpu_counter_read_positive(&EXT3_SB(sb)->s_freeinodes_counter);
  189. avefreei = freei / ngroups;
  190. for (group = 0; group < ngroups; group++) {
  191. desc = ext3_get_group_desc (sb, group, &bh);
  192. if (!desc || !desc->bg_free_inodes_count)
  193. continue;
  194. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  195. continue;
  196. if (!best_desc ||
  197. (le16_to_cpu(desc->bg_free_blocks_count) >
  198. le16_to_cpu(best_desc->bg_free_blocks_count))) {
  199. best_group = group;
  200. best_desc = desc;
  201. }
  202. }
  203. return best_group;
  204. }
  205. /*
  206. * Orlov's allocator for directories.
  207. *
  208. * We always try to spread first-level directories.
  209. *
  210. * If there are blockgroups with both free inodes and free blocks counts
  211. * not worse than average we return one with smallest directory count.
  212. * Otherwise we simply return a random group.
  213. *
  214. * For the rest rules look so:
  215. *
  216. * It's OK to put directory into a group unless
  217. * it has too many directories already (max_dirs) or
  218. * it has too few free inodes left (min_inodes) or
  219. * it has too few free blocks left (min_blocks) or
  220. * it's already running too large debt (max_debt).
  221. * Parent's group is prefered, if it doesn't satisfy these
  222. * conditions we search cyclically through the rest. If none
  223. * of the groups look good we just look for a group with more
  224. * free inodes than average (starting at parent's group).
  225. *
  226. * Debt is incremented each time we allocate a directory and decremented
  227. * when we allocate an inode, within 0--255.
  228. */
  229. #define INODE_COST 64
  230. #define BLOCK_COST 256
  231. static int find_group_orlov(struct super_block *sb, struct inode *parent)
  232. {
  233. int parent_group = EXT3_I(parent)->i_block_group;
  234. struct ext3_sb_info *sbi = EXT3_SB(sb);
  235. struct ext3_super_block *es = sbi->s_es;
  236. int ngroups = sbi->s_groups_count;
  237. int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
  238. int freei, avefreei;
  239. int freeb, avefreeb;
  240. int blocks_per_dir, ndirs;
  241. int max_debt, max_dirs, min_blocks, min_inodes;
  242. int group = -1, i;
  243. struct ext3_group_desc *desc;
  244. struct buffer_head *bh;
  245. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  246. avefreei = freei / ngroups;
  247. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  248. avefreeb = freeb / ngroups;
  249. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  250. if ((parent == sb->s_root->d_inode) ||
  251. (EXT3_I(parent)->i_flags & EXT3_TOPDIR_FL)) {
  252. int best_ndir = inodes_per_group;
  253. int best_group = -1;
  254. get_random_bytes(&group, sizeof(group));
  255. parent_group = (unsigned)group % ngroups;
  256. for (i = 0; i < ngroups; i++) {
  257. group = (parent_group + i) % ngroups;
  258. desc = ext3_get_group_desc (sb, group, &bh);
  259. if (!desc || !desc->bg_free_inodes_count)
  260. continue;
  261. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  262. continue;
  263. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  264. continue;
  265. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  266. continue;
  267. best_group = group;
  268. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  269. }
  270. if (best_group >= 0)
  271. return best_group;
  272. goto fallback;
  273. }
  274. blocks_per_dir = (le32_to_cpu(es->s_blocks_count) - freeb) / ndirs;
  275. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  276. min_inodes = avefreei - inodes_per_group / 4;
  277. min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;
  278. max_debt = EXT3_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
  279. if (max_debt * INODE_COST > inodes_per_group)
  280. max_debt = inodes_per_group / INODE_COST;
  281. if (max_debt > 255)
  282. max_debt = 255;
  283. if (max_debt == 0)
  284. max_debt = 1;
  285. for (i = 0; i < ngroups; i++) {
  286. group = (parent_group + i) % ngroups;
  287. desc = ext3_get_group_desc (sb, group, &bh);
  288. if (!desc || !desc->bg_free_inodes_count)
  289. continue;
  290. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  291. continue;
  292. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  293. continue;
  294. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  295. continue;
  296. return group;
  297. }
  298. fallback:
  299. for (i = 0; i < ngroups; i++) {
  300. group = (parent_group + i) % ngroups;
  301. desc = ext3_get_group_desc (sb, group, &bh);
  302. if (!desc || !desc->bg_free_inodes_count)
  303. continue;
  304. if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  305. return group;
  306. }
  307. if (avefreei) {
  308. /*
  309. * The free-inodes counter is approximate, and for really small
  310. * filesystems the above test can fail to find any blockgroups
  311. */
  312. avefreei = 0;
  313. goto fallback;
  314. }
  315. return -1;
  316. }
  317. static int find_group_other(struct super_block *sb, struct inode *parent)
  318. {
  319. int parent_group = EXT3_I(parent)->i_block_group;
  320. int ngroups = EXT3_SB(sb)->s_groups_count;
  321. struct ext3_group_desc *desc;
  322. struct buffer_head *bh;
  323. int group, i;
  324. /*
  325. * Try to place the inode in its parent directory
  326. */
  327. group = parent_group;
  328. desc = ext3_get_group_desc (sb, group, &bh);
  329. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  330. le16_to_cpu(desc->bg_free_blocks_count))
  331. return group;
  332. /*
  333. * We're going to place this inode in a different blockgroup from its
  334. * parent. We want to cause files in a common directory to all land in
  335. * the same blockgroup. But we want files which are in a different
  336. * directory which shares a blockgroup with our parent to land in a
  337. * different blockgroup.
  338. *
  339. * So add our directory's i_ino into the starting point for the hash.
  340. */
  341. group = (group + parent->i_ino) % ngroups;
  342. /*
  343. * Use a quadratic hash to find a group with a free inode and some free
  344. * blocks.
  345. */
  346. for (i = 1; i < ngroups; i <<= 1) {
  347. group += i;
  348. if (group >= ngroups)
  349. group -= ngroups;
  350. desc = ext3_get_group_desc (sb, group, &bh);
  351. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  352. le16_to_cpu(desc->bg_free_blocks_count))
  353. return group;
  354. }
  355. /*
  356. * That failed: try linear search for a free inode, even if that group
  357. * has no free blocks.
  358. */
  359. group = parent_group;
  360. for (i = 0; i < ngroups; i++) {
  361. if (++group >= ngroups)
  362. group = 0;
  363. desc = ext3_get_group_desc (sb, group, &bh);
  364. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  365. return group;
  366. }
  367. return -1;
  368. }
  369. /*
  370. * There are two policies for allocating an inode. If the new inode is
  371. * a directory, then a forward search is made for a block group with both
  372. * free space and a low directory-to-inode ratio; if that fails, then of
  373. * the groups with above-average free space, that group with the fewest
  374. * directories already is chosen.
  375. *
  376. * For other inodes, search forward from the parent directory's block
  377. * group to find a free inode.
  378. */
  379. struct inode *ext3_new_inode(handle_t *handle, struct inode * dir, int mode)
  380. {
  381. struct super_block *sb;
  382. struct buffer_head *bitmap_bh = NULL;
  383. struct buffer_head *bh2;
  384. int group;
  385. unsigned long ino = 0;
  386. struct inode * inode;
  387. struct ext3_group_desc * gdp = NULL;
  388. struct ext3_super_block * es;
  389. struct ext3_inode_info *ei;
  390. struct ext3_sb_info *sbi;
  391. int err = 0;
  392. struct inode *ret;
  393. int i;
  394. /* Cannot create files in a deleted directory */
  395. if (!dir || !dir->i_nlink)
  396. return ERR_PTR(-EPERM);
  397. sb = dir->i_sb;
  398. inode = new_inode(sb);
  399. if (!inode)
  400. return ERR_PTR(-ENOMEM);
  401. ei = EXT3_I(inode);
  402. sbi = EXT3_SB(sb);
  403. es = sbi->s_es;
  404. if (S_ISDIR(mode)) {
  405. if (test_opt (sb, OLDALLOC))
  406. group = find_group_dir(sb, dir);
  407. else
  408. group = find_group_orlov(sb, dir);
  409. } else
  410. group = find_group_other(sb, dir);
  411. err = -ENOSPC;
  412. if (group == -1)
  413. goto out;
  414. for (i = 0; i < sbi->s_groups_count; i++) {
  415. err = -EIO;
  416. gdp = ext3_get_group_desc(sb, group, &bh2);
  417. if (!gdp)
  418. goto fail;
  419. brelse(bitmap_bh);
  420. bitmap_bh = read_inode_bitmap(sb, group);
  421. if (!bitmap_bh)
  422. goto fail;
  423. ino = 0;
  424. repeat_in_this_group:
  425. ino = ext3_find_next_zero_bit((unsigned long *)
  426. bitmap_bh->b_data, EXT3_INODES_PER_GROUP(sb), ino);
  427. if (ino < EXT3_INODES_PER_GROUP(sb)) {
  428. BUFFER_TRACE(bitmap_bh, "get_write_access");
  429. err = ext3_journal_get_write_access(handle, bitmap_bh);
  430. if (err)
  431. goto fail;
  432. if (!ext3_set_bit_atomic(sb_bgl_lock(sbi, group),
  433. ino, bitmap_bh->b_data)) {
  434. /* we won it */
  435. BUFFER_TRACE(bitmap_bh,
  436. "call ext3_journal_dirty_metadata");
  437. err = ext3_journal_dirty_metadata(handle,
  438. bitmap_bh);
  439. if (err)
  440. goto fail;
  441. goto got;
  442. }
  443. /* we lost it */
  444. journal_release_buffer(handle, bitmap_bh);
  445. if (++ino < EXT3_INODES_PER_GROUP(sb))
  446. goto repeat_in_this_group;
  447. }
  448. /*
  449. * This case is possible in concurrent environment. It is very
  450. * rare. We cannot repeat the find_group_xxx() call because
  451. * that will simply return the same blockgroup, because the
  452. * group descriptor metadata has not yet been updated.
  453. * So we just go onto the next blockgroup.
  454. */
  455. if (++group == sbi->s_groups_count)
  456. group = 0;
  457. }
  458. err = -ENOSPC;
  459. goto out;
  460. got:
  461. ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
  462. if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  463. ext3_error (sb, "ext3_new_inode",
  464. "reserved inode or inode > inodes count - "
  465. "block_group = %d, inode=%lu", group, ino);
  466. err = -EIO;
  467. goto fail;
  468. }
  469. BUFFER_TRACE(bh2, "get_write_access");
  470. err = ext3_journal_get_write_access(handle, bh2);
  471. if (err) goto fail;
  472. spin_lock(sb_bgl_lock(sbi, group));
  473. gdp->bg_free_inodes_count =
  474. cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
  475. if (S_ISDIR(mode)) {
  476. gdp->bg_used_dirs_count =
  477. cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
  478. }
  479. spin_unlock(sb_bgl_lock(sbi, group));
  480. BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  481. err = ext3_journal_dirty_metadata(handle, bh2);
  482. if (err) goto fail;
  483. percpu_counter_dec(&sbi->s_freeinodes_counter);
  484. if (S_ISDIR(mode))
  485. percpu_counter_inc(&sbi->s_dirs_counter);
  486. sb->s_dirt = 1;
  487. inode->i_uid = current->fsuid;
  488. if (test_opt (sb, GRPID))
  489. inode->i_gid = dir->i_gid;
  490. else if (dir->i_mode & S_ISGID) {
  491. inode->i_gid = dir->i_gid;
  492. if (S_ISDIR(mode))
  493. mode |= S_ISGID;
  494. } else
  495. inode->i_gid = current->fsgid;
  496. inode->i_mode = mode;
  497. inode->i_ino = ino;
  498. /* This is the optimal IO size (for stat), not the fs block size */
  499. inode->i_blksize = PAGE_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?\n", 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\n", 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?\n", 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. }