ialloc.c 26 KB

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