ialloc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. #include "group.h"
  30. /*
  31. * ialloc.c contains the inodes allocation and deallocation routines
  32. */
  33. /*
  34. * The free inodes are managed by bitmaps. A file system contains several
  35. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  36. * block for inodes, N blocks for the inode table and data blocks.
  37. *
  38. * The file system contains group descriptors which are located after the
  39. * super block. Each descriptor contains the number of the bitmap block and
  40. * the free blocks count in the block.
  41. */
  42. /*
  43. * To avoid calling the atomic setbit hundreds or thousands of times, we only
  44. * need to use it within a single byte (to ensure we get endianness right).
  45. * We can use memset for the rest of the bitmap as there are no other users.
  46. */
  47. void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
  48. {
  49. int i;
  50. if (start_bit >= end_bit)
  51. return;
  52. ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
  53. for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
  54. ext4_set_bit(i, bitmap);
  55. if (i < end_bit)
  56. memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
  57. }
  58. /* Initializes an uninitialized inode bitmap */
  59. unsigned ext4_init_inode_bitmap(struct super_block *sb,
  60. struct buffer_head *bh, int block_group,
  61. struct ext4_group_desc *gdp)
  62. {
  63. struct ext4_sb_info *sbi = EXT4_SB(sb);
  64. J_ASSERT_BH(bh, buffer_locked(bh));
  65. /* If checksum is bad mark all blocks and inodes use to prevent
  66. * allocation, essentially implementing a per-group read-only flag. */
  67. if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
  68. ext4_error(sb, __FUNCTION__, "Checksum bad for group %u\n",
  69. block_group);
  70. gdp->bg_free_blocks_count = 0;
  71. gdp->bg_free_inodes_count = 0;
  72. gdp->bg_itable_unused = 0;
  73. memset(bh->b_data, 0xff, sb->s_blocksize);
  74. return 0;
  75. }
  76. memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
  77. mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
  78. bh->b_data);
  79. return EXT4_INODES_PER_GROUP(sb);
  80. }
  81. /*
  82. * Read the inode allocation bitmap for a given block_group, reading
  83. * into the specified slot in the superblock's bitmap cache.
  84. *
  85. * Return buffer_head of bitmap on success or NULL.
  86. */
  87. static struct buffer_head *
  88. read_inode_bitmap(struct super_block * sb, unsigned long block_group)
  89. {
  90. struct ext4_group_desc *desc;
  91. struct buffer_head *bh = NULL;
  92. desc = ext4_get_group_desc(sb, block_group, NULL);
  93. if (!desc)
  94. goto error_out;
  95. if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  96. bh = sb_getblk(sb, ext4_inode_bitmap(sb, desc));
  97. if (!buffer_uptodate(bh)) {
  98. lock_buffer(bh);
  99. if (!buffer_uptodate(bh)) {
  100. ext4_init_inode_bitmap(sb, bh, block_group,
  101. desc);
  102. set_buffer_uptodate(bh);
  103. }
  104. unlock_buffer(bh);
  105. }
  106. } else {
  107. bh = sb_bread(sb, ext4_inode_bitmap(sb, desc));
  108. }
  109. if (!bh)
  110. ext4_error(sb, "read_inode_bitmap",
  111. "Cannot read inode bitmap - "
  112. "block_group = %lu, inode_bitmap = %llu",
  113. block_group, ext4_inode_bitmap(sb, desc));
  114. error_out:
  115. return bh;
  116. }
  117. /*
  118. * NOTE! When we get the inode, we're the only people
  119. * that have access to it, and as such there are no
  120. * race conditions we have to worry about. The inode
  121. * is not on the hash-lists, and it cannot be reached
  122. * through the filesystem because the directory entry
  123. * has been deleted earlier.
  124. *
  125. * HOWEVER: we must make sure that we get no aliases,
  126. * which means that we have to call "clear_inode()"
  127. * _before_ we mark the inode not in use in the inode
  128. * bitmaps. Otherwise a newly created file might use
  129. * the same inode number (not actually the same pointer
  130. * though), and then we'd have two inodes sharing the
  131. * same inode number and space on the harddisk.
  132. */
  133. void ext4_free_inode (handle_t *handle, struct inode * inode)
  134. {
  135. struct super_block * sb = inode->i_sb;
  136. int is_directory;
  137. unsigned long ino;
  138. struct buffer_head *bitmap_bh = NULL;
  139. struct buffer_head *bh2;
  140. unsigned long block_group;
  141. unsigned long bit;
  142. struct ext4_group_desc * gdp;
  143. struct ext4_super_block * es;
  144. struct ext4_sb_info *sbi;
  145. int fatal = 0, err;
  146. if (atomic_read(&inode->i_count) > 1) {
  147. printk ("ext4_free_inode: inode has count=%d\n",
  148. atomic_read(&inode->i_count));
  149. return;
  150. }
  151. if (inode->i_nlink) {
  152. printk ("ext4_free_inode: inode has nlink=%d\n",
  153. inode->i_nlink);
  154. return;
  155. }
  156. if (!sb) {
  157. printk("ext4_free_inode: inode on nonexistent device\n");
  158. return;
  159. }
  160. sbi = EXT4_SB(sb);
  161. ino = inode->i_ino;
  162. ext4_debug ("freeing inode %lu\n", ino);
  163. /*
  164. * Note: we must free any quota before locking the superblock,
  165. * as writing the quota to disk may need the lock as well.
  166. */
  167. DQUOT_INIT(inode);
  168. ext4_xattr_delete_inode(handle, inode);
  169. DQUOT_FREE_INODE(inode);
  170. DQUOT_DROP(inode);
  171. is_directory = S_ISDIR(inode->i_mode);
  172. /* Do this BEFORE marking the inode not in use or returning an error */
  173. clear_inode (inode);
  174. es = EXT4_SB(sb)->s_es;
  175. if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  176. ext4_error (sb, "ext4_free_inode",
  177. "reserved or nonexistent inode %lu", ino);
  178. goto error_return;
  179. }
  180. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  181. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  182. bitmap_bh = read_inode_bitmap(sb, block_group);
  183. if (!bitmap_bh)
  184. goto error_return;
  185. BUFFER_TRACE(bitmap_bh, "get_write_access");
  186. fatal = ext4_journal_get_write_access(handle, bitmap_bh);
  187. if (fatal)
  188. goto error_return;
  189. /* Ok, now we can actually update the inode bitmaps.. */
  190. if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  191. bit, bitmap_bh->b_data))
  192. ext4_error (sb, "ext4_free_inode",
  193. "bit already cleared for inode %lu", ino);
  194. else {
  195. gdp = ext4_get_group_desc (sb, block_group, &bh2);
  196. BUFFER_TRACE(bh2, "get_write_access");
  197. fatal = ext4_journal_get_write_access(handle, bh2);
  198. if (fatal) goto error_return;
  199. if (gdp) {
  200. spin_lock(sb_bgl_lock(sbi, block_group));
  201. gdp->bg_free_inodes_count = cpu_to_le16(
  202. le16_to_cpu(gdp->bg_free_inodes_count) + 1);
  203. if (is_directory)
  204. gdp->bg_used_dirs_count = cpu_to_le16(
  205. le16_to_cpu(gdp->bg_used_dirs_count) - 1);
  206. gdp->bg_checksum = ext4_group_desc_csum(sbi,
  207. block_group, gdp);
  208. spin_unlock(sb_bgl_lock(sbi, block_group));
  209. percpu_counter_inc(&sbi->s_freeinodes_counter);
  210. if (is_directory)
  211. percpu_counter_dec(&sbi->s_dirs_counter);
  212. }
  213. BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
  214. err = ext4_journal_dirty_metadata(handle, bh2);
  215. if (!fatal) fatal = err;
  216. }
  217. BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
  218. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  219. if (!fatal)
  220. fatal = err;
  221. sb->s_dirt = 1;
  222. error_return:
  223. brelse(bitmap_bh);
  224. ext4_std_error(sb, fatal);
  225. }
  226. /*
  227. * There are two policies for allocating an inode. If the new inode is
  228. * a directory, then a forward search is made for a block group with both
  229. * free space and a low directory-to-inode ratio; if that fails, then of
  230. * the groups with above-average free space, that group with the fewest
  231. * directories already is chosen.
  232. *
  233. * For other inodes, search forward from the parent directory\'s block
  234. * group to find a free inode.
  235. */
  236. static int find_group_dir(struct super_block *sb, struct inode *parent)
  237. {
  238. int ngroups = EXT4_SB(sb)->s_groups_count;
  239. unsigned int freei, avefreei;
  240. struct ext4_group_desc *desc, *best_desc = NULL;
  241. int group, best_group = -1;
  242. freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
  243. avefreei = freei / ngroups;
  244. for (group = 0; group < ngroups; group++) {
  245. desc = ext4_get_group_desc (sb, group, NULL);
  246. if (!desc || !desc->bg_free_inodes_count)
  247. continue;
  248. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  249. continue;
  250. if (!best_desc ||
  251. (le16_to_cpu(desc->bg_free_blocks_count) >
  252. le16_to_cpu(best_desc->bg_free_blocks_count))) {
  253. best_group = group;
  254. best_desc = desc;
  255. }
  256. }
  257. return best_group;
  258. }
  259. /*
  260. * Orlov's allocator for directories.
  261. *
  262. * We always try to spread first-level directories.
  263. *
  264. * If there are blockgroups with both free inodes and free blocks counts
  265. * not worse than average we return one with smallest directory count.
  266. * Otherwise we simply return a random group.
  267. *
  268. * For the rest rules look so:
  269. *
  270. * It's OK to put directory into a group unless
  271. * it has too many directories already (max_dirs) or
  272. * it has too few free inodes left (min_inodes) or
  273. * it has too few free blocks left (min_blocks) or
  274. * it's already running too large debt (max_debt).
  275. * Parent's group is prefered, if it doesn't satisfy these
  276. * conditions we search cyclically through the rest. If none
  277. * of the groups look good we just look for a group with more
  278. * free inodes than average (starting at parent's group).
  279. *
  280. * Debt is incremented each time we allocate a directory and decremented
  281. * when we allocate an inode, within 0--255.
  282. */
  283. #define INODE_COST 64
  284. #define BLOCK_COST 256
  285. static int find_group_orlov(struct super_block *sb, struct inode *parent)
  286. {
  287. int parent_group = EXT4_I(parent)->i_block_group;
  288. struct ext4_sb_info *sbi = EXT4_SB(sb);
  289. struct ext4_super_block *es = sbi->s_es;
  290. int ngroups = sbi->s_groups_count;
  291. int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
  292. unsigned int freei, avefreei;
  293. ext4_fsblk_t freeb, avefreeb;
  294. ext4_fsblk_t blocks_per_dir;
  295. unsigned int ndirs;
  296. int max_debt, max_dirs, min_inodes;
  297. ext4_grpblk_t min_blocks;
  298. int group = -1, i;
  299. struct ext4_group_desc *desc;
  300. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  301. avefreei = freei / ngroups;
  302. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  303. avefreeb = freeb;
  304. do_div(avefreeb, ngroups);
  305. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  306. if ((parent == sb->s_root->d_inode) ||
  307. (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
  308. int best_ndir = inodes_per_group;
  309. int best_group = -1;
  310. get_random_bytes(&group, sizeof(group));
  311. parent_group = (unsigned)group % ngroups;
  312. for (i = 0; i < ngroups; i++) {
  313. group = (parent_group + i) % ngroups;
  314. desc = ext4_get_group_desc (sb, group, NULL);
  315. if (!desc || !desc->bg_free_inodes_count)
  316. continue;
  317. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  318. continue;
  319. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  320. continue;
  321. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  322. continue;
  323. best_group = group;
  324. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  325. }
  326. if (best_group >= 0)
  327. return best_group;
  328. goto fallback;
  329. }
  330. blocks_per_dir = ext4_blocks_count(es) - freeb;
  331. do_div(blocks_per_dir, ndirs);
  332. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  333. min_inodes = avefreei - inodes_per_group / 4;
  334. min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
  335. max_debt = EXT4_BLOCKS_PER_GROUP(sb);
  336. max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
  337. if (max_debt * INODE_COST > inodes_per_group)
  338. max_debt = inodes_per_group / INODE_COST;
  339. if (max_debt > 255)
  340. max_debt = 255;
  341. if (max_debt == 0)
  342. max_debt = 1;
  343. for (i = 0; i < ngroups; i++) {
  344. group = (parent_group + i) % ngroups;
  345. desc = ext4_get_group_desc (sb, group, NULL);
  346. if (!desc || !desc->bg_free_inodes_count)
  347. continue;
  348. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  349. continue;
  350. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  351. continue;
  352. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  353. continue;
  354. return group;
  355. }
  356. fallback:
  357. for (i = 0; i < ngroups; i++) {
  358. group = (parent_group + i) % ngroups;
  359. desc = ext4_get_group_desc (sb, group, NULL);
  360. if (!desc || !desc->bg_free_inodes_count)
  361. continue;
  362. if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  363. return group;
  364. }
  365. if (avefreei) {
  366. /*
  367. * The free-inodes counter is approximate, and for really small
  368. * filesystems the above test can fail to find any blockgroups
  369. */
  370. avefreei = 0;
  371. goto fallback;
  372. }
  373. return -1;
  374. }
  375. static int find_group_other(struct super_block *sb, struct inode *parent)
  376. {
  377. int parent_group = EXT4_I(parent)->i_block_group;
  378. int ngroups = EXT4_SB(sb)->s_groups_count;
  379. struct ext4_group_desc *desc;
  380. int group, i;
  381. /*
  382. * Try to place the inode in its parent directory
  383. */
  384. group = parent_group;
  385. desc = ext4_get_group_desc (sb, group, NULL);
  386. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  387. le16_to_cpu(desc->bg_free_blocks_count))
  388. return group;
  389. /*
  390. * We're going to place this inode in a different blockgroup from its
  391. * parent. We want to cause files in a common directory to all land in
  392. * the same blockgroup. But we want files which are in a different
  393. * directory which shares a blockgroup with our parent to land in a
  394. * different blockgroup.
  395. *
  396. * So add our directory's i_ino into the starting point for the hash.
  397. */
  398. group = (group + parent->i_ino) % ngroups;
  399. /*
  400. * Use a quadratic hash to find a group with a free inode and some free
  401. * blocks.
  402. */
  403. for (i = 1; i < ngroups; i <<= 1) {
  404. group += i;
  405. if (group >= ngroups)
  406. group -= ngroups;
  407. desc = ext4_get_group_desc (sb, group, NULL);
  408. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  409. le16_to_cpu(desc->bg_free_blocks_count))
  410. return group;
  411. }
  412. /*
  413. * That failed: try linear search for a free inode, even if that group
  414. * has no free blocks.
  415. */
  416. group = parent_group;
  417. for (i = 0; i < ngroups; i++) {
  418. if (++group >= ngroups)
  419. group = 0;
  420. desc = ext4_get_group_desc (sb, group, NULL);
  421. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  422. return group;
  423. }
  424. return -1;
  425. }
  426. /*
  427. * There are two policies for allocating an inode. If the new inode is
  428. * a directory, then a forward search is made for a block group with both
  429. * free space and a low directory-to-inode ratio; if that fails, then of
  430. * the groups with above-average free space, that group with the fewest
  431. * directories already is chosen.
  432. *
  433. * For other inodes, search forward from the parent directory's block
  434. * group to find a free inode.
  435. */
  436. struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
  437. {
  438. struct super_block *sb;
  439. struct buffer_head *bitmap_bh = NULL;
  440. struct buffer_head *bh2;
  441. int group;
  442. unsigned long ino = 0;
  443. struct inode * inode;
  444. struct ext4_group_desc * gdp = NULL;
  445. struct ext4_super_block * es;
  446. struct ext4_inode_info *ei;
  447. struct ext4_sb_info *sbi;
  448. int err = 0;
  449. struct inode *ret;
  450. int i, free = 0;
  451. /* Cannot create files in a deleted directory */
  452. if (!dir || !dir->i_nlink)
  453. return ERR_PTR(-EPERM);
  454. sb = dir->i_sb;
  455. inode = new_inode(sb);
  456. if (!inode)
  457. return ERR_PTR(-ENOMEM);
  458. ei = EXT4_I(inode);
  459. sbi = EXT4_SB(sb);
  460. es = sbi->s_es;
  461. if (S_ISDIR(mode)) {
  462. if (test_opt (sb, OLDALLOC))
  463. group = find_group_dir(sb, dir);
  464. else
  465. group = find_group_orlov(sb, dir);
  466. } else
  467. group = find_group_other(sb, dir);
  468. err = -ENOSPC;
  469. if (group == -1)
  470. goto out;
  471. for (i = 0; i < sbi->s_groups_count; i++) {
  472. err = -EIO;
  473. gdp = ext4_get_group_desc(sb, group, &bh2);
  474. if (!gdp)
  475. goto fail;
  476. brelse(bitmap_bh);
  477. bitmap_bh = read_inode_bitmap(sb, group);
  478. if (!bitmap_bh)
  479. goto fail;
  480. ino = 0;
  481. repeat_in_this_group:
  482. ino = ext4_find_next_zero_bit((unsigned long *)
  483. bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
  484. if (ino < EXT4_INODES_PER_GROUP(sb)) {
  485. BUFFER_TRACE(bitmap_bh, "get_write_access");
  486. err = ext4_journal_get_write_access(handle, bitmap_bh);
  487. if (err)
  488. goto fail;
  489. if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
  490. ino, bitmap_bh->b_data)) {
  491. /* we won it */
  492. BUFFER_TRACE(bitmap_bh,
  493. "call ext4_journal_dirty_metadata");
  494. err = ext4_journal_dirty_metadata(handle,
  495. bitmap_bh);
  496. if (err)
  497. goto fail;
  498. goto got;
  499. }
  500. /* we lost it */
  501. jbd2_journal_release_buffer(handle, bitmap_bh);
  502. if (++ino < EXT4_INODES_PER_GROUP(sb))
  503. goto repeat_in_this_group;
  504. }
  505. /*
  506. * This case is possible in concurrent environment. It is very
  507. * rare. We cannot repeat the find_group_xxx() call because
  508. * that will simply return the same blockgroup, because the
  509. * group descriptor metadata has not yet been updated.
  510. * So we just go onto the next blockgroup.
  511. */
  512. if (++group == sbi->s_groups_count)
  513. group = 0;
  514. }
  515. err = -ENOSPC;
  516. goto out;
  517. got:
  518. ino++;
  519. if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
  520. ino > EXT4_INODES_PER_GROUP(sb)) {
  521. ext4_error(sb, __FUNCTION__,
  522. "reserved inode or inode > inodes count - "
  523. "block_group = %d, inode=%lu", group,
  524. ino + group * EXT4_INODES_PER_GROUP(sb));
  525. err = -EIO;
  526. goto fail;
  527. }
  528. BUFFER_TRACE(bh2, "get_write_access");
  529. err = ext4_journal_get_write_access(handle, bh2);
  530. if (err) goto fail;
  531. /* We may have to initialize the block bitmap if it isn't already */
  532. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
  533. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  534. struct buffer_head *block_bh = read_block_bitmap(sb, group);
  535. BUFFER_TRACE(block_bh, "get block bitmap access");
  536. err = ext4_journal_get_write_access(handle, block_bh);
  537. if (err) {
  538. brelse(block_bh);
  539. goto fail;
  540. }
  541. free = 0;
  542. spin_lock(sb_bgl_lock(sbi, group));
  543. /* recheck and clear flag under lock if we still need to */
  544. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  545. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  546. free = ext4_free_blocks_after_init(sb, group, gdp);
  547. gdp->bg_free_blocks_count = cpu_to_le16(free);
  548. }
  549. spin_unlock(sb_bgl_lock(sbi, group));
  550. /* Don't need to dirty bitmap block if we didn't change it */
  551. if (free) {
  552. BUFFER_TRACE(block_bh, "dirty block bitmap");
  553. err = ext4_journal_dirty_metadata(handle, block_bh);
  554. }
  555. brelse(block_bh);
  556. if (err)
  557. goto fail;
  558. }
  559. spin_lock(sb_bgl_lock(sbi, group));
  560. /* If we didn't allocate from within the initialized part of the inode
  561. * table then we need to initialize up to this inode. */
  562. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  563. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  564. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  565. /* When marking the block group with
  566. * ~EXT4_BG_INODE_UNINIT we don't want to depend
  567. * on the value of bg_itable_unsed even though
  568. * mke2fs could have initialized the same for us.
  569. * Instead we calculated the value below
  570. */
  571. free = 0;
  572. } else {
  573. free = EXT4_INODES_PER_GROUP(sb) -
  574. le16_to_cpu(gdp->bg_itable_unused);
  575. }
  576. /*
  577. * Check the relative inode number against the last used
  578. * relative inode number in this group. if it is greater
  579. * we need to update the bg_itable_unused count
  580. *
  581. */
  582. if (ino > free)
  583. gdp->bg_itable_unused =
  584. cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
  585. }
  586. gdp->bg_free_inodes_count =
  587. cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
  588. if (S_ISDIR(mode)) {
  589. gdp->bg_used_dirs_count =
  590. cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
  591. }
  592. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  593. spin_unlock(sb_bgl_lock(sbi, group));
  594. BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
  595. err = ext4_journal_dirty_metadata(handle, bh2);
  596. if (err) goto fail;
  597. percpu_counter_dec(&sbi->s_freeinodes_counter);
  598. if (S_ISDIR(mode))
  599. percpu_counter_inc(&sbi->s_dirs_counter);
  600. sb->s_dirt = 1;
  601. inode->i_uid = current->fsuid;
  602. if (test_opt (sb, GRPID))
  603. inode->i_gid = dir->i_gid;
  604. else if (dir->i_mode & S_ISGID) {
  605. inode->i_gid = dir->i_gid;
  606. if (S_ISDIR(mode))
  607. mode |= S_ISGID;
  608. } else
  609. inode->i_gid = current->fsgid;
  610. inode->i_mode = mode;
  611. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  612. /* This is the optimal IO size (for stat), not the fs block size */
  613. inode->i_blocks = 0;
  614. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  615. ext4_current_time(inode);
  616. memset(ei->i_data, 0, sizeof(ei->i_data));
  617. ei->i_dir_start_lookup = 0;
  618. ei->i_disksize = 0;
  619. ei->i_flags = EXT4_I(dir)->i_flags & ~EXT4_INDEX_FL;
  620. if (S_ISLNK(mode))
  621. ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
  622. /* dirsync only applies to directories */
  623. if (!S_ISDIR(mode))
  624. ei->i_flags &= ~EXT4_DIRSYNC_FL;
  625. ei->i_file_acl = 0;
  626. ei->i_dir_acl = 0;
  627. ei->i_dtime = 0;
  628. ei->i_block_alloc_info = NULL;
  629. ei->i_block_group = group;
  630. ext4_set_inode_flags(inode);
  631. if (IS_DIRSYNC(inode))
  632. handle->h_sync = 1;
  633. insert_inode_hash(inode);
  634. spin_lock(&sbi->s_next_gen_lock);
  635. inode->i_generation = sbi->s_next_generation++;
  636. spin_unlock(&sbi->s_next_gen_lock);
  637. ei->i_state = EXT4_STATE_NEW;
  638. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  639. ret = inode;
  640. if(DQUOT_ALLOC_INODE(inode)) {
  641. err = -EDQUOT;
  642. goto fail_drop;
  643. }
  644. err = ext4_init_acl(handle, inode, dir);
  645. if (err)
  646. goto fail_free_drop;
  647. err = ext4_init_security(handle,inode, dir);
  648. if (err)
  649. goto fail_free_drop;
  650. err = ext4_mark_inode_dirty(handle, inode);
  651. if (err) {
  652. ext4_std_error(sb, err);
  653. goto fail_free_drop;
  654. }
  655. if (test_opt(sb, EXTENTS)) {
  656. EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
  657. ext4_ext_tree_init(handle, inode);
  658. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  659. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  660. if (err) goto fail;
  661. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS);
  662. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "call ext4_journal_dirty_metadata");
  663. err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
  664. }
  665. }
  666. ext4_debug("allocating inode %lu\n", inode->i_ino);
  667. goto really_out;
  668. fail:
  669. ext4_std_error(sb, err);
  670. out:
  671. iput(inode);
  672. ret = ERR_PTR(err);
  673. really_out:
  674. brelse(bitmap_bh);
  675. return ret;
  676. fail_free_drop:
  677. DQUOT_FREE_INODE(inode);
  678. fail_drop:
  679. DQUOT_DROP(inode);
  680. inode->i_flags |= S_NOQUOTA;
  681. inode->i_nlink = 0;
  682. iput(inode);
  683. brelse(bitmap_bh);
  684. return ERR_PTR(err);
  685. }
  686. /* Verify that we are loading a valid orphan from disk */
  687. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  688. {
  689. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  690. unsigned long block_group;
  691. int bit;
  692. struct buffer_head *bitmap_bh = NULL;
  693. struct inode *inode = NULL;
  694. /* Error cases - e2fsck has already cleaned up for us */
  695. if (ino > max_ino) {
  696. ext4_warning(sb, __FUNCTION__,
  697. "bad orphan ino %lu! e2fsck was run?", ino);
  698. goto out;
  699. }
  700. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  701. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  702. bitmap_bh = read_inode_bitmap(sb, block_group);
  703. if (!bitmap_bh) {
  704. ext4_warning(sb, __FUNCTION__,
  705. "inode bitmap error for orphan %lu", ino);
  706. goto out;
  707. }
  708. /* Having the inode bit set should be a 100% indicator that this
  709. * is a valid orphan (no e2fsck run on fs). Orphans also include
  710. * inodes that were being truncated, so we can't check i_nlink==0.
  711. */
  712. if (!ext4_test_bit(bit, bitmap_bh->b_data) ||
  713. !(inode = iget(sb, ino)) || is_bad_inode(inode) ||
  714. NEXT_ORPHAN(inode) > max_ino) {
  715. ext4_warning(sb, __FUNCTION__,
  716. "bad orphan inode %lu! e2fsck was run?", ino);
  717. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  718. bit, (unsigned long long)bitmap_bh->b_blocknr,
  719. ext4_test_bit(bit, bitmap_bh->b_data));
  720. printk(KERN_NOTICE "inode=%p\n", inode);
  721. if (inode) {
  722. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  723. is_bad_inode(inode));
  724. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  725. NEXT_ORPHAN(inode));
  726. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  727. }
  728. /* Avoid freeing blocks if we got a bad deleted inode */
  729. if (inode && inode->i_nlink == 0)
  730. inode->i_blocks = 0;
  731. iput(inode);
  732. inode = NULL;
  733. }
  734. out:
  735. brelse(bitmap_bh);
  736. return inode;
  737. }
  738. unsigned long ext4_count_free_inodes (struct super_block * sb)
  739. {
  740. unsigned long desc_count;
  741. struct ext4_group_desc *gdp;
  742. int i;
  743. #ifdef EXT4FS_DEBUG
  744. struct ext4_super_block *es;
  745. unsigned long bitmap_count, x;
  746. struct buffer_head *bitmap_bh = NULL;
  747. es = EXT4_SB(sb)->s_es;
  748. desc_count = 0;
  749. bitmap_count = 0;
  750. gdp = NULL;
  751. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  752. gdp = ext4_get_group_desc (sb, i, NULL);
  753. if (!gdp)
  754. continue;
  755. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  756. brelse(bitmap_bh);
  757. bitmap_bh = read_inode_bitmap(sb, i);
  758. if (!bitmap_bh)
  759. continue;
  760. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  761. printk("group %d: stored = %d, counted = %lu\n",
  762. i, le16_to_cpu(gdp->bg_free_inodes_count), x);
  763. bitmap_count += x;
  764. }
  765. brelse(bitmap_bh);
  766. printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
  767. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  768. return desc_count;
  769. #else
  770. desc_count = 0;
  771. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  772. gdp = ext4_get_group_desc (sb, i, NULL);
  773. if (!gdp)
  774. continue;
  775. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  776. cond_resched();
  777. }
  778. return desc_count;
  779. #endif
  780. }
  781. /* Called at mount-time, super-block is locked */
  782. unsigned long ext4_count_dirs (struct super_block * sb)
  783. {
  784. unsigned long count = 0;
  785. int i;
  786. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  787. struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
  788. if (!gdp)
  789. continue;
  790. count += le16_to_cpu(gdp->bg_used_dirs_count);
  791. }
  792. return count;
  793. }