ialloc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * linux/fs/ext4/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/jbd2.h>
  17. #include <linux/ext4_fs.h>
  18. #include <linux/ext4_jbd2.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 <linux/blkdev.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 ext4_group_desc *desc;
  51. struct buffer_head *bh = NULL;
  52. desc = ext4_get_group_desc(sb, block_group, NULL);
  53. if (!desc)
  54. goto error_out;
  55. bh = sb_bread(sb, ext4_inode_bitmap(sb, desc));
  56. if (!bh)
  57. ext4_error(sb, "read_inode_bitmap",
  58. "Cannot read inode bitmap - "
  59. "block_group = %lu, inode_bitmap = %llu",
  60. block_group, ext4_inode_bitmap(sb, desc));
  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 ext4_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 ext4_group_desc * gdp;
  90. struct ext4_super_block * es;
  91. struct ext4_sb_info *sbi;
  92. int fatal = 0, err;
  93. if (atomic_read(&inode->i_count) > 1) {
  94. printk ("ext4_free_inode: inode has count=%d\n",
  95. atomic_read(&inode->i_count));
  96. return;
  97. }
  98. if (inode->i_nlink) {
  99. printk ("ext4_free_inode: inode has nlink=%d\n",
  100. inode->i_nlink);
  101. return;
  102. }
  103. if (!sb) {
  104. printk("ext4_free_inode: inode on nonexistent device\n");
  105. return;
  106. }
  107. sbi = EXT4_SB(sb);
  108. ino = inode->i_ino;
  109. ext4_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. ext4_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 = EXT4_SB(sb)->s_es;
  122. if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  123. ext4_error (sb, "ext4_free_inode",
  124. "reserved or nonexistent inode %lu", ino);
  125. goto error_return;
  126. }
  127. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  128. bit = (ino - 1) % EXT4_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 = ext4_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 (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  138. bit, bitmap_bh->b_data))
  139. ext4_error (sb, "ext4_free_inode",
  140. "bit already cleared for inode %lu", ino);
  141. else {
  142. gdp = ext4_get_group_desc (sb, block_group, &bh2);
  143. BUFFER_TRACE(bh2, "get_write_access");
  144. fatal = ext4_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 ext4_journal_dirty_metadata");
  159. err = ext4_journal_dirty_metadata(handle, bh2);
  160. if (!fatal) fatal = err;
  161. }
  162. BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
  163. err = ext4_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. ext4_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 = EXT4_SB(sb)->s_groups_count;
  184. unsigned int freei, avefreei;
  185. struct ext4_group_desc *desc, *best_desc = NULL;
  186. struct buffer_head *bh;
  187. int group, best_group = -1;
  188. freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
  189. avefreei = freei / ngroups;
  190. for (group = 0; group < ngroups; group++) {
  191. desc = ext4_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 = EXT4_I(parent)->i_block_group;
  234. struct ext4_sb_info *sbi = EXT4_SB(sb);
  235. struct ext4_super_block *es = sbi->s_es;
  236. int ngroups = sbi->s_groups_count;
  237. int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
  238. unsigned int freei, avefreei;
  239. ext4_fsblk_t freeb, avefreeb;
  240. ext4_fsblk_t blocks_per_dir;
  241. unsigned int ndirs;
  242. int max_debt, max_dirs, min_inodes;
  243. ext4_grpblk_t min_blocks;
  244. int group = -1, i;
  245. struct ext4_group_desc *desc;
  246. struct buffer_head *bh;
  247. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  248. avefreei = freei / ngroups;
  249. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  250. avefreeb = freeb;
  251. do_div(avefreeb, ngroups);
  252. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  253. if ((parent == sb->s_root->d_inode) ||
  254. (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
  255. int best_ndir = inodes_per_group;
  256. int best_group = -1;
  257. get_random_bytes(&group, sizeof(group));
  258. parent_group = (unsigned)group % ngroups;
  259. for (i = 0; i < ngroups; i++) {
  260. group = (parent_group + i) % ngroups;
  261. desc = ext4_get_group_desc (sb, group, &bh);
  262. if (!desc || !desc->bg_free_inodes_count)
  263. continue;
  264. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  265. continue;
  266. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  267. continue;
  268. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  269. continue;
  270. best_group = group;
  271. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  272. }
  273. if (best_group >= 0)
  274. return best_group;
  275. goto fallback;
  276. }
  277. blocks_per_dir = ext4_blocks_count(es) - freeb;
  278. do_div(blocks_per_dir, ndirs);
  279. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  280. min_inodes = avefreei - inodes_per_group / 4;
  281. min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
  282. max_debt = EXT4_BLOCKS_PER_GROUP(sb);
  283. max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
  284. if (max_debt * INODE_COST > inodes_per_group)
  285. max_debt = inodes_per_group / INODE_COST;
  286. if (max_debt > 255)
  287. max_debt = 255;
  288. if (max_debt == 0)
  289. max_debt = 1;
  290. for (i = 0; i < ngroups; i++) {
  291. group = (parent_group + i) % ngroups;
  292. desc = ext4_get_group_desc (sb, group, &bh);
  293. if (!desc || !desc->bg_free_inodes_count)
  294. continue;
  295. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  296. continue;
  297. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  298. continue;
  299. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  300. continue;
  301. return group;
  302. }
  303. fallback:
  304. for (i = 0; i < ngroups; i++) {
  305. group = (parent_group + i) % ngroups;
  306. desc = ext4_get_group_desc (sb, group, &bh);
  307. if (!desc || !desc->bg_free_inodes_count)
  308. continue;
  309. if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  310. return group;
  311. }
  312. if (avefreei) {
  313. /*
  314. * The free-inodes counter is approximate, and for really small
  315. * filesystems the above test can fail to find any blockgroups
  316. */
  317. avefreei = 0;
  318. goto fallback;
  319. }
  320. return -1;
  321. }
  322. static int find_group_other(struct super_block *sb, struct inode *parent)
  323. {
  324. int parent_group = EXT4_I(parent)->i_block_group;
  325. int ngroups = EXT4_SB(sb)->s_groups_count;
  326. struct ext4_group_desc *desc;
  327. struct buffer_head *bh;
  328. int group, i;
  329. /*
  330. * Try to place the inode in its parent directory
  331. */
  332. group = parent_group;
  333. desc = ext4_get_group_desc (sb, group, &bh);
  334. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  335. le16_to_cpu(desc->bg_free_blocks_count))
  336. return group;
  337. /*
  338. * We're going to place this inode in a different blockgroup from its
  339. * parent. We want to cause files in a common directory to all land in
  340. * the same blockgroup. But we want files which are in a different
  341. * directory which shares a blockgroup with our parent to land in a
  342. * different blockgroup.
  343. *
  344. * So add our directory's i_ino into the starting point for the hash.
  345. */
  346. group = (group + parent->i_ino) % ngroups;
  347. /*
  348. * Use a quadratic hash to find a group with a free inode and some free
  349. * blocks.
  350. */
  351. for (i = 1; i < ngroups; i <<= 1) {
  352. group += i;
  353. if (group >= ngroups)
  354. group -= ngroups;
  355. desc = ext4_get_group_desc (sb, group, &bh);
  356. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  357. le16_to_cpu(desc->bg_free_blocks_count))
  358. return group;
  359. }
  360. /*
  361. * That failed: try linear search for a free inode, even if that group
  362. * has no free blocks.
  363. */
  364. group = parent_group;
  365. for (i = 0; i < ngroups; i++) {
  366. if (++group >= ngroups)
  367. group = 0;
  368. desc = ext4_get_group_desc (sb, group, &bh);
  369. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  370. return group;
  371. }
  372. return -1;
  373. }
  374. /*
  375. * There are two policies for allocating an inode. If the new inode is
  376. * a directory, then a forward search is made for a block group with both
  377. * free space and a low directory-to-inode ratio; if that fails, then of
  378. * the groups with above-average free space, that group with the fewest
  379. * directories already is chosen.
  380. *
  381. * For other inodes, search forward from the parent directory's block
  382. * group to find a free inode.
  383. */
  384. struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
  385. {
  386. struct super_block *sb;
  387. struct buffer_head *bitmap_bh = NULL;
  388. struct buffer_head *bh2;
  389. int group;
  390. unsigned long ino = 0;
  391. struct inode * inode;
  392. struct ext4_group_desc * gdp = NULL;
  393. struct ext4_super_block * es;
  394. struct ext4_inode_info *ei;
  395. struct ext4_sb_info *sbi;
  396. int err = 0;
  397. struct inode *ret;
  398. int i;
  399. /* Cannot create files in a deleted directory */
  400. if (!dir || !dir->i_nlink)
  401. return ERR_PTR(-EPERM);
  402. sb = dir->i_sb;
  403. inode = new_inode(sb);
  404. if (!inode)
  405. return ERR_PTR(-ENOMEM);
  406. ei = EXT4_I(inode);
  407. sbi = EXT4_SB(sb);
  408. es = sbi->s_es;
  409. if (S_ISDIR(mode)) {
  410. if (test_opt (sb, OLDALLOC))
  411. group = find_group_dir(sb, dir);
  412. else
  413. group = find_group_orlov(sb, dir);
  414. } else
  415. group = find_group_other(sb, dir);
  416. err = -ENOSPC;
  417. if (group == -1)
  418. goto out;
  419. for (i = 0; i < sbi->s_groups_count; i++) {
  420. err = -EIO;
  421. gdp = ext4_get_group_desc(sb, group, &bh2);
  422. if (!gdp)
  423. goto fail;
  424. brelse(bitmap_bh);
  425. bitmap_bh = read_inode_bitmap(sb, group);
  426. if (!bitmap_bh)
  427. goto fail;
  428. ino = 0;
  429. repeat_in_this_group:
  430. ino = ext4_find_next_zero_bit((unsigned long *)
  431. bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
  432. if (ino < EXT4_INODES_PER_GROUP(sb)) {
  433. BUFFER_TRACE(bitmap_bh, "get_write_access");
  434. err = ext4_journal_get_write_access(handle, bitmap_bh);
  435. if (err)
  436. goto fail;
  437. if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
  438. ino, bitmap_bh->b_data)) {
  439. /* we won it */
  440. BUFFER_TRACE(bitmap_bh,
  441. "call ext4_journal_dirty_metadata");
  442. err = ext4_journal_dirty_metadata(handle,
  443. bitmap_bh);
  444. if (err)
  445. goto fail;
  446. goto got;
  447. }
  448. /* we lost it */
  449. jbd2_journal_release_buffer(handle, bitmap_bh);
  450. if (++ino < EXT4_INODES_PER_GROUP(sb))
  451. goto repeat_in_this_group;
  452. }
  453. /*
  454. * This case is possible in concurrent environment. It is very
  455. * rare. We cannot repeat the find_group_xxx() call because
  456. * that will simply return the same blockgroup, because the
  457. * group descriptor metadata has not yet been updated.
  458. * So we just go onto the next blockgroup.
  459. */
  460. if (++group == sbi->s_groups_count)
  461. group = 0;
  462. }
  463. err = -ENOSPC;
  464. goto out;
  465. got:
  466. ino += group * EXT4_INODES_PER_GROUP(sb) + 1;
  467. if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  468. ext4_error (sb, "ext4_new_inode",
  469. "reserved inode or inode > inodes count - "
  470. "block_group = %d, inode=%lu", group, ino);
  471. err = -EIO;
  472. goto fail;
  473. }
  474. BUFFER_TRACE(bh2, "get_write_access");
  475. err = ext4_journal_get_write_access(handle, bh2);
  476. if (err) goto fail;
  477. spin_lock(sb_bgl_lock(sbi, group));
  478. gdp->bg_free_inodes_count =
  479. cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
  480. if (S_ISDIR(mode)) {
  481. gdp->bg_used_dirs_count =
  482. cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
  483. }
  484. spin_unlock(sb_bgl_lock(sbi, group));
  485. BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
  486. err = ext4_journal_dirty_metadata(handle, bh2);
  487. if (err) goto fail;
  488. percpu_counter_dec(&sbi->s_freeinodes_counter);
  489. if (S_ISDIR(mode))
  490. percpu_counter_inc(&sbi->s_dirs_counter);
  491. sb->s_dirt = 1;
  492. inode->i_uid = current->fsuid;
  493. if (test_opt (sb, GRPID))
  494. inode->i_gid = dir->i_gid;
  495. else if (dir->i_mode & S_ISGID) {
  496. inode->i_gid = dir->i_gid;
  497. if (S_ISDIR(mode))
  498. mode |= S_ISGID;
  499. } else
  500. inode->i_gid = current->fsgid;
  501. inode->i_mode = mode;
  502. inode->i_ino = ino;
  503. /* This is the optimal IO size (for stat), not the fs block size */
  504. inode->i_blocks = 0;
  505. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  506. ext4_current_time(inode);
  507. memset(ei->i_data, 0, sizeof(ei->i_data));
  508. ei->i_dir_start_lookup = 0;
  509. ei->i_disksize = 0;
  510. ei->i_flags = EXT4_I(dir)->i_flags & ~EXT4_INDEX_FL;
  511. if (S_ISLNK(mode))
  512. ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
  513. /* dirsync only applies to directories */
  514. if (!S_ISDIR(mode))
  515. ei->i_flags &= ~EXT4_DIRSYNC_FL;
  516. #ifdef EXT4_FRAGMENTS
  517. ei->i_faddr = 0;
  518. ei->i_frag_no = 0;
  519. ei->i_frag_size = 0;
  520. #endif
  521. ei->i_file_acl = 0;
  522. ei->i_dir_acl = 0;
  523. ei->i_dtime = 0;
  524. ei->i_block_alloc_info = NULL;
  525. ei->i_block_group = group;
  526. ext4_set_inode_flags(inode);
  527. if (IS_DIRSYNC(inode))
  528. handle->h_sync = 1;
  529. insert_inode_hash(inode);
  530. spin_lock(&sbi->s_next_gen_lock);
  531. inode->i_generation = sbi->s_next_generation++;
  532. spin_unlock(&sbi->s_next_gen_lock);
  533. ei->i_state = EXT4_STATE_NEW;
  534. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  535. ret = inode;
  536. if(DQUOT_ALLOC_INODE(inode)) {
  537. err = -EDQUOT;
  538. goto fail_drop;
  539. }
  540. err = ext4_init_acl(handle, inode, dir);
  541. if (err)
  542. goto fail_free_drop;
  543. err = ext4_init_security(handle,inode, dir);
  544. if (err)
  545. goto fail_free_drop;
  546. err = ext4_mark_inode_dirty(handle, inode);
  547. if (err) {
  548. ext4_std_error(sb, err);
  549. goto fail_free_drop;
  550. }
  551. if (test_opt(sb, EXTENTS)) {
  552. EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
  553. ext4_ext_tree_init(handle, inode);
  554. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  555. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  556. if (err) goto fail;
  557. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS);
  558. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "call ext4_journal_dirty_metadata");
  559. err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
  560. }
  561. }
  562. ext4_debug("allocating inode %lu\n", inode->i_ino);
  563. goto really_out;
  564. fail:
  565. ext4_std_error(sb, err);
  566. out:
  567. iput(inode);
  568. ret = ERR_PTR(err);
  569. really_out:
  570. brelse(bitmap_bh);
  571. return ret;
  572. fail_free_drop:
  573. DQUOT_FREE_INODE(inode);
  574. fail_drop:
  575. DQUOT_DROP(inode);
  576. inode->i_flags |= S_NOQUOTA;
  577. inode->i_nlink = 0;
  578. iput(inode);
  579. brelse(bitmap_bh);
  580. return ERR_PTR(err);
  581. }
  582. /* Verify that we are loading a valid orphan from disk */
  583. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  584. {
  585. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  586. unsigned long block_group;
  587. int bit;
  588. struct buffer_head *bitmap_bh = NULL;
  589. struct inode *inode = NULL;
  590. /* Error cases - e2fsck has already cleaned up for us */
  591. if (ino > max_ino) {
  592. ext4_warning(sb, __FUNCTION__,
  593. "bad orphan ino %lu! e2fsck was run?", ino);
  594. goto out;
  595. }
  596. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  597. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  598. bitmap_bh = read_inode_bitmap(sb, block_group);
  599. if (!bitmap_bh) {
  600. ext4_warning(sb, __FUNCTION__,
  601. "inode bitmap error for orphan %lu", ino);
  602. goto out;
  603. }
  604. /* Having the inode bit set should be a 100% indicator that this
  605. * is a valid orphan (no e2fsck run on fs). Orphans also include
  606. * inodes that were being truncated, so we can't check i_nlink==0.
  607. */
  608. if (!ext4_test_bit(bit, bitmap_bh->b_data) ||
  609. !(inode = iget(sb, ino)) || is_bad_inode(inode) ||
  610. NEXT_ORPHAN(inode) > max_ino) {
  611. ext4_warning(sb, __FUNCTION__,
  612. "bad orphan inode %lu! e2fsck was run?", ino);
  613. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  614. bit, (unsigned long long)bitmap_bh->b_blocknr,
  615. ext4_test_bit(bit, bitmap_bh->b_data));
  616. printk(KERN_NOTICE "inode=%p\n", inode);
  617. if (inode) {
  618. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  619. is_bad_inode(inode));
  620. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  621. NEXT_ORPHAN(inode));
  622. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  623. }
  624. /* Avoid freeing blocks if we got a bad deleted inode */
  625. if (inode && inode->i_nlink == 0)
  626. inode->i_blocks = 0;
  627. iput(inode);
  628. inode = NULL;
  629. }
  630. out:
  631. brelse(bitmap_bh);
  632. return inode;
  633. }
  634. unsigned long ext4_count_free_inodes (struct super_block * sb)
  635. {
  636. unsigned long desc_count;
  637. struct ext4_group_desc *gdp;
  638. int i;
  639. #ifdef EXT4FS_DEBUG
  640. struct ext4_super_block *es;
  641. unsigned long bitmap_count, x;
  642. struct buffer_head *bitmap_bh = NULL;
  643. es = EXT4_SB(sb)->s_es;
  644. desc_count = 0;
  645. bitmap_count = 0;
  646. gdp = NULL;
  647. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  648. gdp = ext4_get_group_desc (sb, i, NULL);
  649. if (!gdp)
  650. continue;
  651. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  652. brelse(bitmap_bh);
  653. bitmap_bh = read_inode_bitmap(sb, i);
  654. if (!bitmap_bh)
  655. continue;
  656. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  657. printk("group %d: stored = %d, counted = %lu\n",
  658. i, le16_to_cpu(gdp->bg_free_inodes_count), x);
  659. bitmap_count += x;
  660. }
  661. brelse(bitmap_bh);
  662. printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
  663. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  664. return desc_count;
  665. #else
  666. desc_count = 0;
  667. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  668. gdp = ext4_get_group_desc (sb, i, NULL);
  669. if (!gdp)
  670. continue;
  671. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  672. cond_resched();
  673. }
  674. return desc_count;
  675. #endif
  676. }
  677. /* Called at mount-time, super-block is locked */
  678. unsigned long ext4_count_dirs (struct super_block * sb)
  679. {
  680. unsigned long count = 0;
  681. int i;
  682. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  683. struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
  684. if (!gdp)
  685. continue;
  686. count += le16_to_cpu(gdp->bg_used_dirs_count);
  687. }
  688. return count;
  689. }