ialloc.c 25 KB

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