ialloc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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/stat.h>
  18. #include <linux/string.h>
  19. #include <linux/quotaops.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/random.h>
  22. #include <linux/bitops.h>
  23. #include <linux/blkdev.h>
  24. #include <asm/byteorder.h>
  25. #include "ext4.h"
  26. #include "ext4_jbd2.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, struct buffer_head *bh,
  60. ext4_group_t 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, __func__, "Checksum bad for group %lu\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, ext4_group_t 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. ext4_group_t 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. le16_add_cpu(&gdp->bg_free_inodes_count, 1);
  202. if (is_directory)
  203. le16_add_cpu(&gdp->bg_used_dirs_count, -1);
  204. gdp->bg_checksum = ext4_group_desc_csum(sbi,
  205. block_group, gdp);
  206. spin_unlock(sb_bgl_lock(sbi, block_group));
  207. percpu_counter_inc(&sbi->s_freeinodes_counter);
  208. if (is_directory)
  209. percpu_counter_dec(&sbi->s_dirs_counter);
  210. }
  211. BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
  212. err = ext4_journal_dirty_metadata(handle, bh2);
  213. if (!fatal) fatal = err;
  214. }
  215. BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
  216. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  217. if (!fatal)
  218. fatal = err;
  219. sb->s_dirt = 1;
  220. error_return:
  221. brelse(bitmap_bh);
  222. ext4_std_error(sb, fatal);
  223. }
  224. /*
  225. * There are two policies for allocating an inode. If the new inode is
  226. * a directory, then a forward search is made for a block group with both
  227. * free space and a low directory-to-inode ratio; if that fails, then of
  228. * the groups with above-average free space, that group with the fewest
  229. * directories already is chosen.
  230. *
  231. * For other inodes, search forward from the parent directory\'s block
  232. * group to find a free inode.
  233. */
  234. static int find_group_dir(struct super_block *sb, struct inode *parent,
  235. ext4_group_t *best_group)
  236. {
  237. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  238. unsigned int freei, avefreei;
  239. struct ext4_group_desc *desc, *best_desc = NULL;
  240. ext4_group_t group;
  241. int ret = -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. ret = 0;
  256. }
  257. }
  258. return ret;
  259. }
  260. /*
  261. * Orlov's allocator for directories.
  262. *
  263. * We always try to spread first-level directories.
  264. *
  265. * If there are blockgroups with both free inodes and free blocks counts
  266. * not worse than average we return one with smallest directory count.
  267. * Otherwise we simply return a random group.
  268. *
  269. * For the rest rules look so:
  270. *
  271. * It's OK to put directory into a group unless
  272. * it has too many directories already (max_dirs) or
  273. * it has too few free inodes left (min_inodes) or
  274. * it has too few free blocks left (min_blocks) or
  275. * it's already running too large debt (max_debt).
  276. * Parent's group is preferred, if it doesn't satisfy these
  277. * conditions we search cyclically through the rest. If none
  278. * of the groups look good we just look for a group with more
  279. * free inodes than average (starting at parent's group).
  280. *
  281. * Debt is incremented each time we allocate a directory and decremented
  282. * when we allocate an inode, within 0--255.
  283. */
  284. #define INODE_COST 64
  285. #define BLOCK_COST 256
  286. static int find_group_orlov(struct super_block *sb, struct inode *parent,
  287. ext4_group_t *group)
  288. {
  289. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  290. struct ext4_sb_info *sbi = EXT4_SB(sb);
  291. struct ext4_super_block *es = sbi->s_es;
  292. ext4_group_t ngroups = sbi->s_groups_count;
  293. int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
  294. unsigned int freei, avefreei;
  295. ext4_fsblk_t freeb, avefreeb;
  296. ext4_fsblk_t blocks_per_dir;
  297. unsigned int ndirs;
  298. int max_debt, max_dirs, min_inodes;
  299. ext4_grpblk_t min_blocks;
  300. ext4_group_t i;
  301. struct ext4_group_desc *desc;
  302. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  303. avefreei = freei / ngroups;
  304. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  305. avefreeb = freeb;
  306. do_div(avefreeb, ngroups);
  307. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  308. if ((parent == sb->s_root->d_inode) ||
  309. (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
  310. int best_ndir = inodes_per_group;
  311. ext4_group_t grp;
  312. int ret = -1;
  313. get_random_bytes(&grp, sizeof(grp));
  314. parent_group = (unsigned)grp % ngroups;
  315. for (i = 0; i < ngroups; i++) {
  316. grp = (parent_group + i) % ngroups;
  317. desc = ext4_get_group_desc(sb, grp, NULL);
  318. if (!desc || !desc->bg_free_inodes_count)
  319. continue;
  320. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  321. continue;
  322. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  323. continue;
  324. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  325. continue;
  326. *group = grp;
  327. ret = 0;
  328. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  329. }
  330. if (ret == 0)
  331. return ret;
  332. goto fallback;
  333. }
  334. blocks_per_dir = ext4_blocks_count(es) - freeb;
  335. do_div(blocks_per_dir, ndirs);
  336. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  337. min_inodes = avefreei - inodes_per_group / 4;
  338. min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
  339. max_debt = EXT4_BLOCKS_PER_GROUP(sb);
  340. max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
  341. if (max_debt * INODE_COST > inodes_per_group)
  342. max_debt = inodes_per_group / INODE_COST;
  343. if (max_debt > 255)
  344. max_debt = 255;
  345. if (max_debt == 0)
  346. max_debt = 1;
  347. for (i = 0; i < ngroups; i++) {
  348. *group = (parent_group + i) % ngroups;
  349. desc = ext4_get_group_desc(sb, *group, NULL);
  350. if (!desc || !desc->bg_free_inodes_count)
  351. continue;
  352. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  353. continue;
  354. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  355. continue;
  356. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  357. continue;
  358. return 0;
  359. }
  360. fallback:
  361. for (i = 0; i < ngroups; i++) {
  362. *group = (parent_group + i) % ngroups;
  363. desc = ext4_get_group_desc(sb, *group, NULL);
  364. if (desc && desc->bg_free_inodes_count &&
  365. le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  366. return 0;
  367. }
  368. if (avefreei) {
  369. /*
  370. * The free-inodes counter is approximate, and for really small
  371. * filesystems the above test can fail to find any blockgroups
  372. */
  373. avefreei = 0;
  374. goto fallback;
  375. }
  376. return -1;
  377. }
  378. static int find_group_other(struct super_block *sb, struct inode *parent,
  379. ext4_group_t *group)
  380. {
  381. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  382. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  383. struct ext4_group_desc *desc;
  384. ext4_group_t i;
  385. /*
  386. * Try to place the inode in its parent directory
  387. */
  388. *group = parent_group;
  389. desc = ext4_get_group_desc(sb, *group, NULL);
  390. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  391. le16_to_cpu(desc->bg_free_blocks_count))
  392. return 0;
  393. /*
  394. * We're going to place this inode in a different blockgroup from its
  395. * parent. We want to cause files in a common directory to all land in
  396. * the same blockgroup. But we want files which are in a different
  397. * directory which shares a blockgroup with our parent to land in a
  398. * different blockgroup.
  399. *
  400. * So add our directory's i_ino into the starting point for the hash.
  401. */
  402. *group = (*group + parent->i_ino) % ngroups;
  403. /*
  404. * Use a quadratic hash to find a group with a free inode and some free
  405. * blocks.
  406. */
  407. for (i = 1; i < ngroups; i <<= 1) {
  408. *group += i;
  409. if (*group >= ngroups)
  410. *group -= ngroups;
  411. desc = ext4_get_group_desc(sb, *group, NULL);
  412. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  413. le16_to_cpu(desc->bg_free_blocks_count))
  414. return 0;
  415. }
  416. /*
  417. * That failed: try linear search for a free inode, even if that group
  418. * has no free blocks.
  419. */
  420. *group = parent_group;
  421. for (i = 0; i < ngroups; i++) {
  422. if (++*group >= ngroups)
  423. *group = 0;
  424. desc = ext4_get_group_desc(sb, *group, NULL);
  425. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  426. return 0;
  427. }
  428. return -1;
  429. }
  430. /*
  431. * There are two policies for allocating an inode. If the new inode is
  432. * a directory, then a forward search is made for a block group with both
  433. * free space and a low directory-to-inode ratio; if that fails, then of
  434. * the groups with above-average free space, that group with the fewest
  435. * directories already is chosen.
  436. *
  437. * For other inodes, search forward from the parent directory's block
  438. * group to find a free inode.
  439. */
  440. struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
  441. {
  442. struct super_block *sb;
  443. struct buffer_head *bitmap_bh = NULL;
  444. struct buffer_head *bh2;
  445. ext4_group_t group = 0;
  446. unsigned long ino = 0;
  447. struct inode * inode;
  448. struct ext4_group_desc * gdp = NULL;
  449. struct ext4_super_block * es;
  450. struct ext4_inode_info *ei;
  451. struct ext4_sb_info *sbi;
  452. int ret2, err = 0;
  453. struct inode *ret;
  454. ext4_group_t i;
  455. int free = 0;
  456. /* Cannot create files in a deleted directory */
  457. if (!dir || !dir->i_nlink)
  458. return ERR_PTR(-EPERM);
  459. sb = dir->i_sb;
  460. inode = new_inode(sb);
  461. if (!inode)
  462. return ERR_PTR(-ENOMEM);
  463. ei = EXT4_I(inode);
  464. sbi = EXT4_SB(sb);
  465. es = sbi->s_es;
  466. if (S_ISDIR(mode)) {
  467. if (test_opt (sb, OLDALLOC))
  468. ret2 = find_group_dir(sb, dir, &group);
  469. else
  470. ret2 = find_group_orlov(sb, dir, &group);
  471. } else
  472. ret2 = find_group_other(sb, dir, &group);
  473. err = -ENOSPC;
  474. if (ret2 == -1)
  475. goto out;
  476. for (i = 0; i < sbi->s_groups_count; i++) {
  477. err = -EIO;
  478. gdp = ext4_get_group_desc(sb, group, &bh2);
  479. if (!gdp)
  480. goto fail;
  481. brelse(bitmap_bh);
  482. bitmap_bh = read_inode_bitmap(sb, group);
  483. if (!bitmap_bh)
  484. goto fail;
  485. ino = 0;
  486. repeat_in_this_group:
  487. ino = ext4_find_next_zero_bit((unsigned long *)
  488. bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
  489. if (ino < EXT4_INODES_PER_GROUP(sb)) {
  490. BUFFER_TRACE(bitmap_bh, "get_write_access");
  491. err = ext4_journal_get_write_access(handle, bitmap_bh);
  492. if (err)
  493. goto fail;
  494. if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
  495. ino, bitmap_bh->b_data)) {
  496. /* we won it */
  497. BUFFER_TRACE(bitmap_bh,
  498. "call ext4_journal_dirty_metadata");
  499. err = ext4_journal_dirty_metadata(handle,
  500. bitmap_bh);
  501. if (err)
  502. goto fail;
  503. goto got;
  504. }
  505. /* we lost it */
  506. jbd2_journal_release_buffer(handle, bitmap_bh);
  507. if (++ino < EXT4_INODES_PER_GROUP(sb))
  508. goto repeat_in_this_group;
  509. }
  510. /*
  511. * This case is possible in concurrent environment. It is very
  512. * rare. We cannot repeat the find_group_xxx() call because
  513. * that will simply return the same blockgroup, because the
  514. * group descriptor metadata has not yet been updated.
  515. * So we just go onto the next blockgroup.
  516. */
  517. if (++group == sbi->s_groups_count)
  518. group = 0;
  519. }
  520. err = -ENOSPC;
  521. goto out;
  522. got:
  523. ino++;
  524. if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
  525. ino > EXT4_INODES_PER_GROUP(sb)) {
  526. ext4_error(sb, __func__,
  527. "reserved inode or inode > inodes count - "
  528. "block_group = %lu, inode=%lu", group,
  529. ino + group * EXT4_INODES_PER_GROUP(sb));
  530. err = -EIO;
  531. goto fail;
  532. }
  533. BUFFER_TRACE(bh2, "get_write_access");
  534. err = ext4_journal_get_write_access(handle, bh2);
  535. if (err) goto fail;
  536. /* We may have to initialize the block bitmap if it isn't already */
  537. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
  538. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  539. struct buffer_head *block_bh = read_block_bitmap(sb, group);
  540. BUFFER_TRACE(block_bh, "get block bitmap access");
  541. err = ext4_journal_get_write_access(handle, block_bh);
  542. if (err) {
  543. brelse(block_bh);
  544. goto fail;
  545. }
  546. free = 0;
  547. spin_lock(sb_bgl_lock(sbi, group));
  548. /* recheck and clear flag under lock if we still need to */
  549. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  550. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  551. free = ext4_free_blocks_after_init(sb, group, gdp);
  552. gdp->bg_free_blocks_count = cpu_to_le16(free);
  553. }
  554. spin_unlock(sb_bgl_lock(sbi, group));
  555. /* Don't need to dirty bitmap block if we didn't change it */
  556. if (free) {
  557. BUFFER_TRACE(block_bh, "dirty block bitmap");
  558. err = ext4_journal_dirty_metadata(handle, block_bh);
  559. }
  560. brelse(block_bh);
  561. if (err)
  562. goto fail;
  563. }
  564. spin_lock(sb_bgl_lock(sbi, group));
  565. /* If we didn't allocate from within the initialized part of the inode
  566. * table then we need to initialize up to this inode. */
  567. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  568. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  569. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  570. /* When marking the block group with
  571. * ~EXT4_BG_INODE_UNINIT we don't want to depend
  572. * on the value of bg_itable_unsed even though
  573. * mke2fs could have initialized the same for us.
  574. * Instead we calculated the value below
  575. */
  576. free = 0;
  577. } else {
  578. free = EXT4_INODES_PER_GROUP(sb) -
  579. le16_to_cpu(gdp->bg_itable_unused);
  580. }
  581. /*
  582. * Check the relative inode number against the last used
  583. * relative inode number in this group. if it is greater
  584. * we need to update the bg_itable_unused count
  585. *
  586. */
  587. if (ino > free)
  588. gdp->bg_itable_unused =
  589. cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
  590. }
  591. le16_add_cpu(&gdp->bg_free_inodes_count, -1);
  592. if (S_ISDIR(mode)) {
  593. le16_add_cpu(&gdp->bg_used_dirs_count, 1);
  594. }
  595. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  596. spin_unlock(sb_bgl_lock(sbi, group));
  597. BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
  598. err = ext4_journal_dirty_metadata(handle, bh2);
  599. if (err) goto fail;
  600. percpu_counter_dec(&sbi->s_freeinodes_counter);
  601. if (S_ISDIR(mode))
  602. percpu_counter_inc(&sbi->s_dirs_counter);
  603. sb->s_dirt = 1;
  604. inode->i_uid = current->fsuid;
  605. if (test_opt (sb, GRPID))
  606. inode->i_gid = dir->i_gid;
  607. else if (dir->i_mode & S_ISGID) {
  608. inode->i_gid = dir->i_gid;
  609. if (S_ISDIR(mode))
  610. mode |= S_ISGID;
  611. } else
  612. inode->i_gid = current->fsgid;
  613. inode->i_mode = mode;
  614. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  615. /* This is the optimal IO size (for stat), not the fs block size */
  616. inode->i_blocks = 0;
  617. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  618. ext4_current_time(inode);
  619. memset(ei->i_data, 0, sizeof(ei->i_data));
  620. ei->i_dir_start_lookup = 0;
  621. ei->i_disksize = 0;
  622. /*
  623. * Don't inherit extent flag from directory. We set extent flag on
  624. * newly created directory and file only if -o extent mount option is
  625. * specified
  626. */
  627. ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
  628. if (S_ISLNK(mode))
  629. ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
  630. /* dirsync only applies to directories */
  631. if (!S_ISDIR(mode))
  632. ei->i_flags &= ~EXT4_DIRSYNC_FL;
  633. ei->i_file_acl = 0;
  634. ei->i_dtime = 0;
  635. ei->i_block_alloc_info = NULL;
  636. ei->i_block_group = group;
  637. ext4_set_inode_flags(inode);
  638. if (IS_DIRSYNC(inode))
  639. handle->h_sync = 1;
  640. insert_inode_hash(inode);
  641. spin_lock(&sbi->s_next_gen_lock);
  642. inode->i_generation = sbi->s_next_generation++;
  643. spin_unlock(&sbi->s_next_gen_lock);
  644. ei->i_state = EXT4_STATE_NEW;
  645. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  646. ret = inode;
  647. if(DQUOT_ALLOC_INODE(inode)) {
  648. err = -EDQUOT;
  649. goto fail_drop;
  650. }
  651. err = ext4_init_acl(handle, inode, dir);
  652. if (err)
  653. goto fail_free_drop;
  654. err = ext4_init_security(handle,inode, dir);
  655. if (err)
  656. goto fail_free_drop;
  657. if (test_opt(sb, EXTENTS)) {
  658. /* set extent flag only for diretory, file and normal symlink*/
  659. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  660. EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
  661. ext4_ext_tree_init(handle, inode);
  662. err = ext4_update_incompat_feature(handle, sb,
  663. EXT4_FEATURE_INCOMPAT_EXTENTS);
  664. if (err)
  665. goto fail_free_drop;
  666. }
  667. }
  668. err = ext4_mark_inode_dirty(handle, inode);
  669. if (err) {
  670. ext4_std_error(sb, err);
  671. goto fail_free_drop;
  672. }
  673. ext4_debug("allocating inode %lu\n", inode->i_ino);
  674. goto really_out;
  675. fail:
  676. ext4_std_error(sb, err);
  677. out:
  678. iput(inode);
  679. ret = ERR_PTR(err);
  680. really_out:
  681. brelse(bitmap_bh);
  682. return ret;
  683. fail_free_drop:
  684. DQUOT_FREE_INODE(inode);
  685. fail_drop:
  686. DQUOT_DROP(inode);
  687. inode->i_flags |= S_NOQUOTA;
  688. inode->i_nlink = 0;
  689. iput(inode);
  690. brelse(bitmap_bh);
  691. return ERR_PTR(err);
  692. }
  693. /* Verify that we are loading a valid orphan from disk */
  694. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  695. {
  696. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  697. ext4_group_t block_group;
  698. int bit;
  699. struct buffer_head *bitmap_bh;
  700. struct inode *inode = NULL;
  701. long err = -EIO;
  702. /* Error cases - e2fsck has already cleaned up for us */
  703. if (ino > max_ino) {
  704. ext4_warning(sb, __func__,
  705. "bad orphan ino %lu! e2fsck was run?", ino);
  706. goto error;
  707. }
  708. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  709. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  710. bitmap_bh = read_inode_bitmap(sb, block_group);
  711. if (!bitmap_bh) {
  712. ext4_warning(sb, __func__,
  713. "inode bitmap error for orphan %lu", ino);
  714. goto error;
  715. }
  716. /* Having the inode bit set should be a 100% indicator that this
  717. * is a valid orphan (no e2fsck run on fs). Orphans also include
  718. * inodes that were being truncated, so we can't check i_nlink==0.
  719. */
  720. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  721. goto bad_orphan;
  722. inode = ext4_iget(sb, ino);
  723. if (IS_ERR(inode))
  724. goto iget_failed;
  725. if (NEXT_ORPHAN(inode) > max_ino)
  726. goto bad_orphan;
  727. brelse(bitmap_bh);
  728. return inode;
  729. iget_failed:
  730. err = PTR_ERR(inode);
  731. inode = NULL;
  732. bad_orphan:
  733. ext4_warning(sb, __func__,
  734. "bad orphan inode %lu! e2fsck was run?", ino);
  735. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  736. bit, (unsigned long long)bitmap_bh->b_blocknr,
  737. ext4_test_bit(bit, bitmap_bh->b_data));
  738. printk(KERN_NOTICE "inode=%p\n", inode);
  739. if (inode) {
  740. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  741. is_bad_inode(inode));
  742. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  743. NEXT_ORPHAN(inode));
  744. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  745. /* Avoid freeing blocks if we got a bad deleted inode */
  746. if (inode->i_nlink == 0)
  747. inode->i_blocks = 0;
  748. iput(inode);
  749. }
  750. brelse(bitmap_bh);
  751. error:
  752. return ERR_PTR(err);
  753. }
  754. unsigned long ext4_count_free_inodes (struct super_block * sb)
  755. {
  756. unsigned long desc_count;
  757. struct ext4_group_desc *gdp;
  758. ext4_group_t i;
  759. #ifdef EXT4FS_DEBUG
  760. struct ext4_super_block *es;
  761. unsigned long bitmap_count, x;
  762. struct buffer_head *bitmap_bh = NULL;
  763. es = EXT4_SB(sb)->s_es;
  764. desc_count = 0;
  765. bitmap_count = 0;
  766. gdp = NULL;
  767. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  768. gdp = ext4_get_group_desc (sb, i, NULL);
  769. if (!gdp)
  770. continue;
  771. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  772. brelse(bitmap_bh);
  773. bitmap_bh = read_inode_bitmap(sb, i);
  774. if (!bitmap_bh)
  775. continue;
  776. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  777. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  778. i, le16_to_cpu(gdp->bg_free_inodes_count), x);
  779. bitmap_count += x;
  780. }
  781. brelse(bitmap_bh);
  782. printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
  783. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  784. return desc_count;
  785. #else
  786. desc_count = 0;
  787. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  788. gdp = ext4_get_group_desc (sb, i, NULL);
  789. if (!gdp)
  790. continue;
  791. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  792. cond_resched();
  793. }
  794. return desc_count;
  795. #endif
  796. }
  797. /* Called at mount-time, super-block is locked */
  798. unsigned long ext4_count_dirs (struct super_block * sb)
  799. {
  800. unsigned long count = 0;
  801. ext4_group_t i;
  802. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  803. struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
  804. if (!gdp)
  805. continue;
  806. count += le16_to_cpu(gdp->bg_used_dirs_count);
  807. }
  808. return count;
  809. }