ialloc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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",
  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. ext4_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. ext4_fsblk_t bitmap_blk;
  93. desc = ext4_get_group_desc(sb, block_group, NULL);
  94. if (!desc)
  95. return NULL;
  96. bitmap_blk = ext4_inode_bitmap(sb, desc);
  97. bh = sb_getblk(sb, bitmap_blk);
  98. if (unlikely(!bh)) {
  99. ext4_error(sb, __func__,
  100. "Cannot read inode bitmap - "
  101. "block_group = %lu, inode_bitmap = %llu",
  102. block_group, bitmap_blk);
  103. return NULL;
  104. }
  105. if (buffer_uptodate(bh) &&
  106. !(desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
  107. return bh;
  108. lock_buffer(bh);
  109. spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
  110. if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  111. ext4_init_inode_bitmap(sb, bh, block_group, desc);
  112. set_buffer_uptodate(bh);
  113. unlock_buffer(bh);
  114. spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
  115. return bh;
  116. }
  117. spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
  118. if (bh_submit_read(bh) < 0) {
  119. put_bh(bh);
  120. ext4_error(sb, __func__,
  121. "Cannot read inode bitmap - "
  122. "block_group = %lu, inode_bitmap = %llu",
  123. block_group, bitmap_blk);
  124. return NULL;
  125. }
  126. return bh;
  127. }
  128. /*
  129. * NOTE! When we get the inode, we're the only people
  130. * that have access to it, and as such there are no
  131. * race conditions we have to worry about. The inode
  132. * is not on the hash-lists, and it cannot be reached
  133. * through the filesystem because the directory entry
  134. * has been deleted earlier.
  135. *
  136. * HOWEVER: we must make sure that we get no aliases,
  137. * which means that we have to call "clear_inode()"
  138. * _before_ we mark the inode not in use in the inode
  139. * bitmaps. Otherwise a newly created file might use
  140. * the same inode number (not actually the same pointer
  141. * though), and then we'd have two inodes sharing the
  142. * same inode number and space on the harddisk.
  143. */
  144. void ext4_free_inode(handle_t *handle, struct inode *inode)
  145. {
  146. struct super_block *sb = inode->i_sb;
  147. int is_directory;
  148. unsigned long ino;
  149. struct buffer_head *bitmap_bh = NULL;
  150. struct buffer_head *bh2;
  151. ext4_group_t block_group;
  152. unsigned long bit;
  153. struct ext4_group_desc *gdp;
  154. struct ext4_super_block *es;
  155. struct ext4_sb_info *sbi;
  156. int fatal = 0, err;
  157. ext4_group_t flex_group;
  158. if (atomic_read(&inode->i_count) > 1) {
  159. printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
  160. atomic_read(&inode->i_count));
  161. return;
  162. }
  163. if (inode->i_nlink) {
  164. printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
  165. inode->i_nlink);
  166. return;
  167. }
  168. if (!sb) {
  169. printk(KERN_ERR "ext4_free_inode: inode on "
  170. "nonexistent device\n");
  171. return;
  172. }
  173. sbi = EXT4_SB(sb);
  174. ino = inode->i_ino;
  175. ext4_debug("freeing inode %lu\n", ino);
  176. /*
  177. * Note: we must free any quota before locking the superblock,
  178. * as writing the quota to disk may need the lock as well.
  179. */
  180. DQUOT_INIT(inode);
  181. ext4_xattr_delete_inode(handle, inode);
  182. DQUOT_FREE_INODE(inode);
  183. DQUOT_DROP(inode);
  184. is_directory = S_ISDIR(inode->i_mode);
  185. /* Do this BEFORE marking the inode not in use or returning an error */
  186. clear_inode(inode);
  187. es = EXT4_SB(sb)->s_es;
  188. if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  189. ext4_error(sb, "ext4_free_inode",
  190. "reserved or nonexistent inode %lu", ino);
  191. goto error_return;
  192. }
  193. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  194. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  195. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  196. if (!bitmap_bh)
  197. goto error_return;
  198. BUFFER_TRACE(bitmap_bh, "get_write_access");
  199. fatal = ext4_journal_get_write_access(handle, bitmap_bh);
  200. if (fatal)
  201. goto error_return;
  202. /* Ok, now we can actually update the inode bitmaps.. */
  203. if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  204. bit, bitmap_bh->b_data))
  205. ext4_error(sb, "ext4_free_inode",
  206. "bit already cleared for inode %lu", ino);
  207. else {
  208. gdp = ext4_get_group_desc(sb, block_group, &bh2);
  209. BUFFER_TRACE(bh2, "get_write_access");
  210. fatal = ext4_journal_get_write_access(handle, bh2);
  211. if (fatal) goto error_return;
  212. if (gdp) {
  213. spin_lock(sb_bgl_lock(sbi, block_group));
  214. le16_add_cpu(&gdp->bg_free_inodes_count, 1);
  215. if (is_directory)
  216. le16_add_cpu(&gdp->bg_used_dirs_count, -1);
  217. gdp->bg_checksum = ext4_group_desc_csum(sbi,
  218. block_group, gdp);
  219. spin_unlock(sb_bgl_lock(sbi, block_group));
  220. percpu_counter_inc(&sbi->s_freeinodes_counter);
  221. if (is_directory)
  222. percpu_counter_dec(&sbi->s_dirs_counter);
  223. if (sbi->s_log_groups_per_flex) {
  224. flex_group = ext4_flex_group(sbi, block_group);
  225. spin_lock(sb_bgl_lock(sbi, flex_group));
  226. sbi->s_flex_groups[flex_group].free_inodes++;
  227. spin_unlock(sb_bgl_lock(sbi, flex_group));
  228. }
  229. }
  230. BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
  231. err = ext4_handle_dirty_metadata(handle, NULL, bh2);
  232. if (!fatal) fatal = err;
  233. }
  234. BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
  235. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  236. if (!fatal)
  237. fatal = err;
  238. sb->s_dirt = 1;
  239. error_return:
  240. brelse(bitmap_bh);
  241. ext4_std_error(sb, fatal);
  242. }
  243. /*
  244. * There are two policies for allocating an inode. If the new inode is
  245. * a directory, then a forward search is made for a block group with both
  246. * free space and a low directory-to-inode ratio; if that fails, then of
  247. * the groups with above-average free space, that group with the fewest
  248. * directories already is chosen.
  249. *
  250. * For other inodes, search forward from the parent directory\'s block
  251. * group to find a free inode.
  252. */
  253. static int find_group_dir(struct super_block *sb, struct inode *parent,
  254. ext4_group_t *best_group)
  255. {
  256. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  257. unsigned int freei, avefreei;
  258. struct ext4_group_desc *desc, *best_desc = NULL;
  259. ext4_group_t group;
  260. int ret = -1;
  261. freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
  262. avefreei = freei / ngroups;
  263. for (group = 0; group < ngroups; group++) {
  264. desc = ext4_get_group_desc(sb, group, NULL);
  265. if (!desc || !desc->bg_free_inodes_count)
  266. continue;
  267. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  268. continue;
  269. if (!best_desc ||
  270. (le16_to_cpu(desc->bg_free_blocks_count) >
  271. le16_to_cpu(best_desc->bg_free_blocks_count))) {
  272. *best_group = group;
  273. best_desc = desc;
  274. ret = 0;
  275. }
  276. }
  277. return ret;
  278. }
  279. #define free_block_ratio 10
  280. static int find_group_flex(struct super_block *sb, struct inode *parent,
  281. ext4_group_t *best_group)
  282. {
  283. struct ext4_sb_info *sbi = EXT4_SB(sb);
  284. struct ext4_group_desc *desc;
  285. struct buffer_head *bh;
  286. struct flex_groups *flex_group = sbi->s_flex_groups;
  287. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  288. ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
  289. ext4_group_t ngroups = sbi->s_groups_count;
  290. int flex_size = ext4_flex_bg_size(sbi);
  291. ext4_group_t best_flex = parent_fbg_group;
  292. int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
  293. int flexbg_free_blocks;
  294. int flex_freeb_ratio;
  295. ext4_group_t n_fbg_groups;
  296. ext4_group_t i;
  297. n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
  298. sbi->s_log_groups_per_flex;
  299. find_close_to_parent:
  300. flexbg_free_blocks = flex_group[best_flex].free_blocks;
  301. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  302. if (flex_group[best_flex].free_inodes &&
  303. flex_freeb_ratio > free_block_ratio)
  304. goto found_flexbg;
  305. if (best_flex && best_flex == parent_fbg_group) {
  306. best_flex--;
  307. goto find_close_to_parent;
  308. }
  309. for (i = 0; i < n_fbg_groups; i++) {
  310. if (i == parent_fbg_group || i == parent_fbg_group - 1)
  311. continue;
  312. flexbg_free_blocks = flex_group[i].free_blocks;
  313. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  314. if (flex_freeb_ratio > free_block_ratio &&
  315. flex_group[i].free_inodes) {
  316. best_flex = i;
  317. goto found_flexbg;
  318. }
  319. if (flex_group[best_flex].free_inodes == 0 ||
  320. (flex_group[i].free_blocks >
  321. flex_group[best_flex].free_blocks &&
  322. flex_group[i].free_inodes))
  323. best_flex = i;
  324. }
  325. if (!flex_group[best_flex].free_inodes ||
  326. !flex_group[best_flex].free_blocks)
  327. return -1;
  328. found_flexbg:
  329. for (i = best_flex * flex_size; i < ngroups &&
  330. i < (best_flex + 1) * flex_size; i++) {
  331. desc = ext4_get_group_desc(sb, i, &bh);
  332. if (le16_to_cpu(desc->bg_free_inodes_count)) {
  333. *best_group = i;
  334. goto out;
  335. }
  336. }
  337. return -1;
  338. out:
  339. return 0;
  340. }
  341. /*
  342. * Orlov's allocator for directories.
  343. *
  344. * We always try to spread first-level directories.
  345. *
  346. * If there are blockgroups with both free inodes and free blocks counts
  347. * not worse than average we return one with smallest directory count.
  348. * Otherwise we simply return a random group.
  349. *
  350. * For the rest rules look so:
  351. *
  352. * It's OK to put directory into a group unless
  353. * it has too many directories already (max_dirs) or
  354. * it has too few free inodes left (min_inodes) or
  355. * it has too few free blocks left (min_blocks) or
  356. * it's already running too large debt (max_debt).
  357. * Parent's group is preferred, if it doesn't satisfy these
  358. * conditions we search cyclically through the rest. If none
  359. * of the groups look good we just look for a group with more
  360. * free inodes than average (starting at parent's group).
  361. *
  362. * Debt is incremented each time we allocate a directory and decremented
  363. * when we allocate an inode, within 0--255.
  364. */
  365. #define INODE_COST 64
  366. #define BLOCK_COST 256
  367. static int find_group_orlov(struct super_block *sb, struct inode *parent,
  368. ext4_group_t *group)
  369. {
  370. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  371. struct ext4_sb_info *sbi = EXT4_SB(sb);
  372. struct ext4_super_block *es = sbi->s_es;
  373. ext4_group_t ngroups = sbi->s_groups_count;
  374. int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
  375. unsigned int freei, avefreei;
  376. ext4_fsblk_t freeb, avefreeb;
  377. ext4_fsblk_t blocks_per_dir;
  378. unsigned int ndirs;
  379. int max_debt, max_dirs, min_inodes;
  380. ext4_grpblk_t min_blocks;
  381. ext4_group_t i;
  382. struct ext4_group_desc *desc;
  383. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  384. avefreei = freei / ngroups;
  385. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  386. avefreeb = freeb;
  387. do_div(avefreeb, ngroups);
  388. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  389. if ((parent == sb->s_root->d_inode) ||
  390. (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
  391. int best_ndir = inodes_per_group;
  392. ext4_group_t grp;
  393. int ret = -1;
  394. get_random_bytes(&grp, sizeof(grp));
  395. parent_group = (unsigned)grp % ngroups;
  396. for (i = 0; i < ngroups; i++) {
  397. grp = (parent_group + i) % ngroups;
  398. desc = ext4_get_group_desc(sb, grp, NULL);
  399. if (!desc || !desc->bg_free_inodes_count)
  400. continue;
  401. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  402. continue;
  403. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  404. continue;
  405. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  406. continue;
  407. *group = grp;
  408. ret = 0;
  409. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  410. }
  411. if (ret == 0)
  412. return ret;
  413. goto fallback;
  414. }
  415. blocks_per_dir = ext4_blocks_count(es) - freeb;
  416. do_div(blocks_per_dir, ndirs);
  417. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  418. min_inodes = avefreei - inodes_per_group / 4;
  419. min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
  420. max_debt = EXT4_BLOCKS_PER_GROUP(sb);
  421. max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
  422. if (max_debt * INODE_COST > inodes_per_group)
  423. max_debt = inodes_per_group / INODE_COST;
  424. if (max_debt > 255)
  425. max_debt = 255;
  426. if (max_debt == 0)
  427. max_debt = 1;
  428. for (i = 0; i < ngroups; i++) {
  429. *group = (parent_group + i) % ngroups;
  430. desc = ext4_get_group_desc(sb, *group, NULL);
  431. if (!desc || !desc->bg_free_inodes_count)
  432. continue;
  433. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  434. continue;
  435. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  436. continue;
  437. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  438. continue;
  439. return 0;
  440. }
  441. fallback:
  442. for (i = 0; i < ngroups; i++) {
  443. *group = (parent_group + i) % ngroups;
  444. desc = ext4_get_group_desc(sb, *group, NULL);
  445. if (desc && desc->bg_free_inodes_count &&
  446. le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  447. return 0;
  448. }
  449. if (avefreei) {
  450. /*
  451. * The free-inodes counter is approximate, and for really small
  452. * filesystems the above test can fail to find any blockgroups
  453. */
  454. avefreei = 0;
  455. goto fallback;
  456. }
  457. return -1;
  458. }
  459. static int find_group_other(struct super_block *sb, struct inode *parent,
  460. ext4_group_t *group)
  461. {
  462. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  463. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  464. struct ext4_group_desc *desc;
  465. ext4_group_t i;
  466. /*
  467. * Try to place the inode in its parent directory
  468. */
  469. *group = parent_group;
  470. desc = ext4_get_group_desc(sb, *group, NULL);
  471. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  472. le16_to_cpu(desc->bg_free_blocks_count))
  473. return 0;
  474. /*
  475. * We're going to place this inode in a different blockgroup from its
  476. * parent. We want to cause files in a common directory to all land in
  477. * the same blockgroup. But we want files which are in a different
  478. * directory which shares a blockgroup with our parent to land in a
  479. * different blockgroup.
  480. *
  481. * So add our directory's i_ino into the starting point for the hash.
  482. */
  483. *group = (*group + parent->i_ino) % ngroups;
  484. /*
  485. * Use a quadratic hash to find a group with a free inode and some free
  486. * blocks.
  487. */
  488. for (i = 1; i < ngroups; i <<= 1) {
  489. *group += i;
  490. if (*group >= ngroups)
  491. *group -= ngroups;
  492. desc = ext4_get_group_desc(sb, *group, NULL);
  493. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  494. le16_to_cpu(desc->bg_free_blocks_count))
  495. return 0;
  496. }
  497. /*
  498. * That failed: try linear search for a free inode, even if that group
  499. * has no free blocks.
  500. */
  501. *group = parent_group;
  502. for (i = 0; i < ngroups; i++) {
  503. if (++*group >= ngroups)
  504. *group = 0;
  505. desc = ext4_get_group_desc(sb, *group, NULL);
  506. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  507. return 0;
  508. }
  509. return -1;
  510. }
  511. /*
  512. * There are two policies for allocating an inode. If the new inode is
  513. * a directory, then a forward search is made for a block group with both
  514. * free space and a low directory-to-inode ratio; if that fails, then of
  515. * the groups with above-average free space, that group with the fewest
  516. * directories already is chosen.
  517. *
  518. * For other inodes, search forward from the parent directory's block
  519. * group to find a free inode.
  520. */
  521. struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
  522. {
  523. struct super_block *sb;
  524. struct buffer_head *bitmap_bh = NULL;
  525. struct buffer_head *bh2;
  526. ext4_group_t group = 0;
  527. unsigned long ino = 0;
  528. struct inode *inode;
  529. struct ext4_group_desc *gdp = NULL;
  530. struct ext4_super_block *es;
  531. struct ext4_inode_info *ei;
  532. struct ext4_sb_info *sbi;
  533. int ret2, err = 0;
  534. struct inode *ret;
  535. ext4_group_t i;
  536. int free = 0;
  537. ext4_group_t flex_group;
  538. /* Cannot create files in a deleted directory */
  539. if (!dir || !dir->i_nlink)
  540. return ERR_PTR(-EPERM);
  541. sb = dir->i_sb;
  542. inode = new_inode(sb);
  543. if (!inode)
  544. return ERR_PTR(-ENOMEM);
  545. ei = EXT4_I(inode);
  546. sbi = EXT4_SB(sb);
  547. es = sbi->s_es;
  548. if (sbi->s_log_groups_per_flex) {
  549. ret2 = find_group_flex(sb, dir, &group);
  550. goto got_group;
  551. }
  552. if (S_ISDIR(mode)) {
  553. if (test_opt(sb, OLDALLOC))
  554. ret2 = find_group_dir(sb, dir, &group);
  555. else
  556. ret2 = find_group_orlov(sb, dir, &group);
  557. } else
  558. ret2 = find_group_other(sb, dir, &group);
  559. got_group:
  560. err = -ENOSPC;
  561. if (ret2 == -1)
  562. goto out;
  563. for (i = 0; i < sbi->s_groups_count; i++) {
  564. err = -EIO;
  565. gdp = ext4_get_group_desc(sb, group, &bh2);
  566. if (!gdp)
  567. goto fail;
  568. brelse(bitmap_bh);
  569. bitmap_bh = ext4_read_inode_bitmap(sb, group);
  570. if (!bitmap_bh)
  571. goto fail;
  572. ino = 0;
  573. repeat_in_this_group:
  574. ino = ext4_find_next_zero_bit((unsigned long *)
  575. bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
  576. if (ino < EXT4_INODES_PER_GROUP(sb)) {
  577. BUFFER_TRACE(bitmap_bh, "get_write_access");
  578. err = ext4_journal_get_write_access(handle, bitmap_bh);
  579. if (err)
  580. goto fail;
  581. if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
  582. ino, bitmap_bh->b_data)) {
  583. /* we won it */
  584. BUFFER_TRACE(bitmap_bh,
  585. "call ext4_handle_dirty_metadata");
  586. err = ext4_handle_dirty_metadata(handle,
  587. inode,
  588. bitmap_bh);
  589. if (err)
  590. goto fail;
  591. goto got;
  592. }
  593. /* we lost it */
  594. ext4_handle_release_buffer(handle, bitmap_bh);
  595. if (++ino < EXT4_INODES_PER_GROUP(sb))
  596. goto repeat_in_this_group;
  597. }
  598. /*
  599. * This case is possible in concurrent environment. It is very
  600. * rare. We cannot repeat the find_group_xxx() call because
  601. * that will simply return the same blockgroup, because the
  602. * group descriptor metadata has not yet been updated.
  603. * So we just go onto the next blockgroup.
  604. */
  605. if (++group == sbi->s_groups_count)
  606. group = 0;
  607. }
  608. err = -ENOSPC;
  609. goto out;
  610. got:
  611. ino++;
  612. if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
  613. ino > EXT4_INODES_PER_GROUP(sb)) {
  614. ext4_error(sb, __func__,
  615. "reserved inode or inode > inodes count - "
  616. "block_group = %lu, inode=%lu", group,
  617. ino + group * EXT4_INODES_PER_GROUP(sb));
  618. err = -EIO;
  619. goto fail;
  620. }
  621. BUFFER_TRACE(bh2, "get_write_access");
  622. err = ext4_journal_get_write_access(handle, bh2);
  623. if (err) goto fail;
  624. /* We may have to initialize the block bitmap if it isn't already */
  625. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
  626. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  627. struct buffer_head *block_bh = ext4_read_block_bitmap(sb, group);
  628. BUFFER_TRACE(block_bh, "get block bitmap access");
  629. err = ext4_journal_get_write_access(handle, block_bh);
  630. if (err) {
  631. brelse(block_bh);
  632. goto fail;
  633. }
  634. free = 0;
  635. spin_lock(sb_bgl_lock(sbi, group));
  636. /* recheck and clear flag under lock if we still need to */
  637. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  638. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  639. free = ext4_free_blocks_after_init(sb, group, gdp);
  640. gdp->bg_free_blocks_count = cpu_to_le16(free);
  641. gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
  642. gdp);
  643. }
  644. spin_unlock(sb_bgl_lock(sbi, group));
  645. /* Don't need to dirty bitmap block if we didn't change it */
  646. if (free) {
  647. BUFFER_TRACE(block_bh, "dirty block bitmap");
  648. err = ext4_handle_dirty_metadata(handle,
  649. NULL, block_bh);
  650. }
  651. brelse(block_bh);
  652. if (err)
  653. goto fail;
  654. }
  655. spin_lock(sb_bgl_lock(sbi, group));
  656. /* If we didn't allocate from within the initialized part of the inode
  657. * table then we need to initialize up to this inode. */
  658. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  659. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  660. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  661. /* When marking the block group with
  662. * ~EXT4_BG_INODE_UNINIT we don't want to depend
  663. * on the value of bg_itable_unused even though
  664. * mke2fs could have initialized the same for us.
  665. * Instead we calculated the value below
  666. */
  667. free = 0;
  668. } else {
  669. free = EXT4_INODES_PER_GROUP(sb) -
  670. le16_to_cpu(gdp->bg_itable_unused);
  671. }
  672. /*
  673. * Check the relative inode number against the last used
  674. * relative inode number in this group. if it is greater
  675. * we need to update the bg_itable_unused count
  676. *
  677. */
  678. if (ino > free)
  679. gdp->bg_itable_unused =
  680. cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
  681. }
  682. le16_add_cpu(&gdp->bg_free_inodes_count, -1);
  683. if (S_ISDIR(mode)) {
  684. le16_add_cpu(&gdp->bg_used_dirs_count, 1);
  685. }
  686. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  687. spin_unlock(sb_bgl_lock(sbi, group));
  688. BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
  689. err = ext4_handle_dirty_metadata(handle, NULL, bh2);
  690. if (err) goto fail;
  691. percpu_counter_dec(&sbi->s_freeinodes_counter);
  692. if (S_ISDIR(mode))
  693. percpu_counter_inc(&sbi->s_dirs_counter);
  694. sb->s_dirt = 1;
  695. if (sbi->s_log_groups_per_flex) {
  696. flex_group = ext4_flex_group(sbi, group);
  697. spin_lock(sb_bgl_lock(sbi, flex_group));
  698. sbi->s_flex_groups[flex_group].free_inodes--;
  699. spin_unlock(sb_bgl_lock(sbi, flex_group));
  700. }
  701. inode->i_uid = current_fsuid();
  702. if (test_opt(sb, GRPID))
  703. inode->i_gid = dir->i_gid;
  704. else if (dir->i_mode & S_ISGID) {
  705. inode->i_gid = dir->i_gid;
  706. if (S_ISDIR(mode))
  707. mode |= S_ISGID;
  708. } else
  709. inode->i_gid = current_fsgid();
  710. inode->i_mode = mode;
  711. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  712. /* This is the optimal IO size (for stat), not the fs block size */
  713. inode->i_blocks = 0;
  714. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  715. ext4_current_time(inode);
  716. memset(ei->i_data, 0, sizeof(ei->i_data));
  717. ei->i_dir_start_lookup = 0;
  718. ei->i_disksize = 0;
  719. /*
  720. * Don't inherit extent flag from directory. We set extent flag on
  721. * newly created directory and file only if -o extent mount option is
  722. * specified
  723. */
  724. ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
  725. if (S_ISLNK(mode))
  726. ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
  727. /* dirsync only applies to directories */
  728. if (!S_ISDIR(mode))
  729. ei->i_flags &= ~EXT4_DIRSYNC_FL;
  730. ei->i_file_acl = 0;
  731. ei->i_dtime = 0;
  732. ei->i_block_group = group;
  733. ext4_set_inode_flags(inode);
  734. if (IS_DIRSYNC(inode))
  735. ext4_handle_sync(handle);
  736. if (insert_inode_locked(inode) < 0) {
  737. err = -EINVAL;
  738. goto fail_drop;
  739. }
  740. spin_lock(&sbi->s_next_gen_lock);
  741. inode->i_generation = sbi->s_next_generation++;
  742. spin_unlock(&sbi->s_next_gen_lock);
  743. ei->i_state = EXT4_STATE_NEW;
  744. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  745. ret = inode;
  746. if (DQUOT_ALLOC_INODE(inode)) {
  747. err = -EDQUOT;
  748. goto fail_drop;
  749. }
  750. err = ext4_init_acl(handle, inode, dir);
  751. if (err)
  752. goto fail_free_drop;
  753. err = ext4_init_security(handle, inode, dir);
  754. if (err)
  755. goto fail_free_drop;
  756. if (test_opt(sb, EXTENTS)) {
  757. /* set extent flag only for directory, file and normal symlink*/
  758. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  759. EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
  760. ext4_ext_tree_init(handle, inode);
  761. }
  762. }
  763. err = ext4_mark_inode_dirty(handle, inode);
  764. if (err) {
  765. ext4_std_error(sb, err);
  766. goto fail_free_drop;
  767. }
  768. ext4_debug("allocating inode %lu\n", inode->i_ino);
  769. goto really_out;
  770. fail:
  771. ext4_std_error(sb, err);
  772. out:
  773. iput(inode);
  774. ret = ERR_PTR(err);
  775. really_out:
  776. brelse(bitmap_bh);
  777. return ret;
  778. fail_free_drop:
  779. DQUOT_FREE_INODE(inode);
  780. fail_drop:
  781. DQUOT_DROP(inode);
  782. inode->i_flags |= S_NOQUOTA;
  783. inode->i_nlink = 0;
  784. unlock_new_inode(inode);
  785. iput(inode);
  786. brelse(bitmap_bh);
  787. return ERR_PTR(err);
  788. }
  789. /* Verify that we are loading a valid orphan from disk */
  790. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  791. {
  792. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  793. ext4_group_t block_group;
  794. int bit;
  795. struct buffer_head *bitmap_bh;
  796. struct inode *inode = NULL;
  797. long err = -EIO;
  798. /* Error cases - e2fsck has already cleaned up for us */
  799. if (ino > max_ino) {
  800. ext4_warning(sb, __func__,
  801. "bad orphan ino %lu! e2fsck was run?", ino);
  802. goto error;
  803. }
  804. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  805. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  806. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  807. if (!bitmap_bh) {
  808. ext4_warning(sb, __func__,
  809. "inode bitmap error for orphan %lu", ino);
  810. goto error;
  811. }
  812. /* Having the inode bit set should be a 100% indicator that this
  813. * is a valid orphan (no e2fsck run on fs). Orphans also include
  814. * inodes that were being truncated, so we can't check i_nlink==0.
  815. */
  816. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  817. goto bad_orphan;
  818. inode = ext4_iget(sb, ino);
  819. if (IS_ERR(inode))
  820. goto iget_failed;
  821. /*
  822. * If the orphans has i_nlinks > 0 then it should be able to be
  823. * truncated, otherwise it won't be removed from the orphan list
  824. * during processing and an infinite loop will result.
  825. */
  826. if (inode->i_nlink && !ext4_can_truncate(inode))
  827. goto bad_orphan;
  828. if (NEXT_ORPHAN(inode) > max_ino)
  829. goto bad_orphan;
  830. brelse(bitmap_bh);
  831. return inode;
  832. iget_failed:
  833. err = PTR_ERR(inode);
  834. inode = NULL;
  835. bad_orphan:
  836. ext4_warning(sb, __func__,
  837. "bad orphan inode %lu! e2fsck was run?", ino);
  838. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  839. bit, (unsigned long long)bitmap_bh->b_blocknr,
  840. ext4_test_bit(bit, bitmap_bh->b_data));
  841. printk(KERN_NOTICE "inode=%p\n", inode);
  842. if (inode) {
  843. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  844. is_bad_inode(inode));
  845. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  846. NEXT_ORPHAN(inode));
  847. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  848. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  849. /* Avoid freeing blocks if we got a bad deleted inode */
  850. if (inode->i_nlink == 0)
  851. inode->i_blocks = 0;
  852. iput(inode);
  853. }
  854. brelse(bitmap_bh);
  855. error:
  856. return ERR_PTR(err);
  857. }
  858. unsigned long ext4_count_free_inodes(struct super_block *sb)
  859. {
  860. unsigned long desc_count;
  861. struct ext4_group_desc *gdp;
  862. ext4_group_t i;
  863. #ifdef EXT4FS_DEBUG
  864. struct ext4_super_block *es;
  865. unsigned long bitmap_count, x;
  866. struct buffer_head *bitmap_bh = NULL;
  867. es = EXT4_SB(sb)->s_es;
  868. desc_count = 0;
  869. bitmap_count = 0;
  870. gdp = NULL;
  871. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  872. gdp = ext4_get_group_desc(sb, i, NULL);
  873. if (!gdp)
  874. continue;
  875. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  876. brelse(bitmap_bh);
  877. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  878. if (!bitmap_bh)
  879. continue;
  880. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  881. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  882. i, le16_to_cpu(gdp->bg_free_inodes_count), x);
  883. bitmap_count += x;
  884. }
  885. brelse(bitmap_bh);
  886. printk(KERN_DEBUG "ext4_count_free_inodes: "
  887. "stored = %u, computed = %lu, %lu\n",
  888. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  889. return desc_count;
  890. #else
  891. desc_count = 0;
  892. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  893. gdp = ext4_get_group_desc(sb, i, NULL);
  894. if (!gdp)
  895. continue;
  896. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  897. cond_resched();
  898. }
  899. return desc_count;
  900. #endif
  901. }
  902. /* Called at mount-time, super-block is locked */
  903. unsigned long ext4_count_dirs(struct super_block * sb)
  904. {
  905. unsigned long count = 0;
  906. ext4_group_t i;
  907. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  908. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  909. if (!gdp)
  910. continue;
  911. count += le16_to_cpu(gdp->bg_used_dirs_count);
  912. }
  913. return count;
  914. }