ialloc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/ialloc.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * BSD ufs-inspired inode and directory allocation by
  11. * Stephen Tweedie (sct@redhat.com), 1993
  12. * Big-endian to little-endian byte-swapping/bitmaps by
  13. * David S. Miller (davem@caip.rutgers.edu), 1995
  14. */
  15. #include <linux/time.h>
  16. #include <linux/fs.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 <linux/cred.h>
  25. #include <asm/byteorder.h>
  26. #include "ext4.h"
  27. #include "ext4_jbd2.h"
  28. #include "xattr.h"
  29. #include "acl.h"
  30. #include <trace/events/ext4.h>
  31. /*
  32. * ialloc.c contains the inodes allocation and deallocation routines
  33. */
  34. /*
  35. * The free inodes are managed by bitmaps. A file system contains several
  36. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  37. * block for inodes, N blocks for the inode table and data blocks.
  38. *
  39. * The file system contains group descriptors which are located after the
  40. * super block. Each descriptor contains the number of the bitmap block and
  41. * the free blocks count in the block.
  42. */
  43. /*
  44. * To avoid calling the atomic setbit hundreds or thousands of times, we only
  45. * need to use it within a single byte (to ensure we get endianness right).
  46. * We can use memset for the rest of the bitmap as there are no other users.
  47. */
  48. void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
  49. {
  50. int i;
  51. if (start_bit >= end_bit)
  52. return;
  53. ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
  54. for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
  55. ext4_set_bit(i, bitmap);
  56. if (i < end_bit)
  57. memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
  58. }
  59. /* Initializes an uninitialized inode bitmap */
  60. static int ext4_init_inode_bitmap(struct super_block *sb,
  61. struct buffer_head *bh,
  62. ext4_group_t block_group,
  63. struct ext4_group_desc *gdp)
  64. {
  65. struct ext4_group_info *grp;
  66. struct ext4_sb_info *sbi = EXT4_SB(sb);
  67. J_ASSERT_BH(bh, buffer_locked(bh));
  68. /* If checksum is bad mark all blocks and inodes use to prevent
  69. * allocation, essentially implementing a per-group read-only flag. */
  70. if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
  71. grp = ext4_get_group_info(sb, block_group);
  72. if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
  73. percpu_counter_sub(&sbi->s_freeclusters_counter,
  74. grp->bb_free);
  75. set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
  76. if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
  77. int count;
  78. count = ext4_free_inodes_count(sb, gdp);
  79. percpu_counter_sub(&sbi->s_freeinodes_counter,
  80. count);
  81. }
  82. set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
  83. return -EFSBADCRC;
  84. }
  85. memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
  86. ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
  87. bh->b_data);
  88. ext4_inode_bitmap_csum_set(sb, block_group, gdp, bh,
  89. EXT4_INODES_PER_GROUP(sb) / 8);
  90. ext4_group_desc_csum_set(sb, block_group, gdp);
  91. return 0;
  92. }
  93. void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate)
  94. {
  95. if (uptodate) {
  96. set_buffer_uptodate(bh);
  97. set_bitmap_uptodate(bh);
  98. }
  99. unlock_buffer(bh);
  100. put_bh(bh);
  101. }
  102. static int ext4_validate_inode_bitmap(struct super_block *sb,
  103. struct ext4_group_desc *desc,
  104. ext4_group_t block_group,
  105. struct buffer_head *bh)
  106. {
  107. ext4_fsblk_t blk;
  108. struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
  109. struct ext4_sb_info *sbi = EXT4_SB(sb);
  110. if (buffer_verified(bh))
  111. return 0;
  112. if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
  113. return -EFSCORRUPTED;
  114. ext4_lock_group(sb, block_group);
  115. blk = ext4_inode_bitmap(sb, desc);
  116. if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
  117. EXT4_INODES_PER_GROUP(sb) / 8)) {
  118. ext4_unlock_group(sb, block_group);
  119. ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
  120. "inode_bitmap = %llu", block_group, blk);
  121. grp = ext4_get_group_info(sb, block_group);
  122. if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
  123. int count;
  124. count = ext4_free_inodes_count(sb, desc);
  125. percpu_counter_sub(&sbi->s_freeinodes_counter,
  126. count);
  127. }
  128. set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
  129. return -EFSBADCRC;
  130. }
  131. set_buffer_verified(bh);
  132. ext4_unlock_group(sb, block_group);
  133. return 0;
  134. }
  135. /*
  136. * Read the inode allocation bitmap for a given block_group, reading
  137. * into the specified slot in the superblock's bitmap cache.
  138. *
  139. * Return buffer_head of bitmap on success or NULL.
  140. */
  141. static struct buffer_head *
  142. ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
  143. {
  144. struct ext4_group_desc *desc;
  145. struct buffer_head *bh = NULL;
  146. ext4_fsblk_t bitmap_blk;
  147. int err;
  148. desc = ext4_get_group_desc(sb, block_group, NULL);
  149. if (!desc)
  150. return ERR_PTR(-EFSCORRUPTED);
  151. bitmap_blk = ext4_inode_bitmap(sb, desc);
  152. bh = sb_getblk(sb, bitmap_blk);
  153. if (unlikely(!bh)) {
  154. ext4_error(sb, "Cannot read inode bitmap - "
  155. "block_group = %u, inode_bitmap = %llu",
  156. block_group, bitmap_blk);
  157. return ERR_PTR(-EIO);
  158. }
  159. if (bitmap_uptodate(bh))
  160. goto verify;
  161. lock_buffer(bh);
  162. if (bitmap_uptodate(bh)) {
  163. unlock_buffer(bh);
  164. goto verify;
  165. }
  166. ext4_lock_group(sb, block_group);
  167. if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  168. err = ext4_init_inode_bitmap(sb, bh, block_group, desc);
  169. set_bitmap_uptodate(bh);
  170. set_buffer_uptodate(bh);
  171. set_buffer_verified(bh);
  172. ext4_unlock_group(sb, block_group);
  173. unlock_buffer(bh);
  174. if (err) {
  175. ext4_error(sb, "Failed to init inode bitmap for group "
  176. "%u: %d", block_group, err);
  177. goto out;
  178. }
  179. return bh;
  180. }
  181. ext4_unlock_group(sb, block_group);
  182. if (buffer_uptodate(bh)) {
  183. /*
  184. * if not uninit if bh is uptodate,
  185. * bitmap is also uptodate
  186. */
  187. set_bitmap_uptodate(bh);
  188. unlock_buffer(bh);
  189. goto verify;
  190. }
  191. /*
  192. * submit the buffer_head for reading
  193. */
  194. trace_ext4_load_inode_bitmap(sb, block_group);
  195. bh->b_end_io = ext4_end_bitmap_read;
  196. get_bh(bh);
  197. submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
  198. wait_on_buffer(bh);
  199. if (!buffer_uptodate(bh)) {
  200. put_bh(bh);
  201. ext4_error(sb, "Cannot read inode bitmap - "
  202. "block_group = %u, inode_bitmap = %llu",
  203. block_group, bitmap_blk);
  204. return ERR_PTR(-EIO);
  205. }
  206. verify:
  207. err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
  208. if (err)
  209. goto out;
  210. return bh;
  211. out:
  212. put_bh(bh);
  213. return ERR_PTR(err);
  214. }
  215. /*
  216. * NOTE! When we get the inode, we're the only people
  217. * that have access to it, and as such there are no
  218. * race conditions we have to worry about. The inode
  219. * is not on the hash-lists, and it cannot be reached
  220. * through the filesystem because the directory entry
  221. * has been deleted earlier.
  222. *
  223. * HOWEVER: we must make sure that we get no aliases,
  224. * which means that we have to call "clear_inode()"
  225. * _before_ we mark the inode not in use in the inode
  226. * bitmaps. Otherwise a newly created file might use
  227. * the same inode number (not actually the same pointer
  228. * though), and then we'd have two inodes sharing the
  229. * same inode number and space on the harddisk.
  230. */
  231. void ext4_free_inode(handle_t *handle, struct inode *inode)
  232. {
  233. struct super_block *sb = inode->i_sb;
  234. int is_directory;
  235. unsigned long ino;
  236. struct buffer_head *bitmap_bh = NULL;
  237. struct buffer_head *bh2;
  238. ext4_group_t block_group;
  239. unsigned long bit;
  240. struct ext4_group_desc *gdp;
  241. struct ext4_super_block *es;
  242. struct ext4_sb_info *sbi;
  243. int fatal = 0, err, count, cleared;
  244. struct ext4_group_info *grp;
  245. if (!sb) {
  246. printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
  247. "nonexistent device\n", __func__, __LINE__);
  248. return;
  249. }
  250. if (atomic_read(&inode->i_count) > 1) {
  251. ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
  252. __func__, __LINE__, inode->i_ino,
  253. atomic_read(&inode->i_count));
  254. return;
  255. }
  256. if (inode->i_nlink) {
  257. ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
  258. __func__, __LINE__, inode->i_ino, inode->i_nlink);
  259. return;
  260. }
  261. sbi = EXT4_SB(sb);
  262. ino = inode->i_ino;
  263. ext4_debug("freeing inode %lu\n", ino);
  264. trace_ext4_free_inode(inode);
  265. /*
  266. * Note: we must free any quota before locking the superblock,
  267. * as writing the quota to disk may need the lock as well.
  268. */
  269. dquot_initialize(inode);
  270. dquot_free_inode(inode);
  271. dquot_drop(inode);
  272. is_directory = S_ISDIR(inode->i_mode);
  273. /* Do this BEFORE marking the inode not in use or returning an error */
  274. ext4_clear_inode(inode);
  275. es = sbi->s_es;
  276. if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  277. ext4_error(sb, "reserved or nonexistent inode %lu", ino);
  278. goto error_return;
  279. }
  280. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  281. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  282. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  283. /* Don't bother if the inode bitmap is corrupt. */
  284. grp = ext4_get_group_info(sb, block_group);
  285. if (IS_ERR(bitmap_bh)) {
  286. fatal = PTR_ERR(bitmap_bh);
  287. bitmap_bh = NULL;
  288. goto error_return;
  289. }
  290. if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
  291. fatal = -EFSCORRUPTED;
  292. goto error_return;
  293. }
  294. BUFFER_TRACE(bitmap_bh, "get_write_access");
  295. fatal = ext4_journal_get_write_access(handle, bitmap_bh);
  296. if (fatal)
  297. goto error_return;
  298. fatal = -ESRCH;
  299. gdp = ext4_get_group_desc(sb, block_group, &bh2);
  300. if (gdp) {
  301. BUFFER_TRACE(bh2, "get_write_access");
  302. fatal = ext4_journal_get_write_access(handle, bh2);
  303. }
  304. ext4_lock_group(sb, block_group);
  305. cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
  306. if (fatal || !cleared) {
  307. ext4_unlock_group(sb, block_group);
  308. goto out;
  309. }
  310. count = ext4_free_inodes_count(sb, gdp) + 1;
  311. ext4_free_inodes_set(sb, gdp, count);
  312. if (is_directory) {
  313. count = ext4_used_dirs_count(sb, gdp) - 1;
  314. ext4_used_dirs_set(sb, gdp, count);
  315. percpu_counter_dec(&sbi->s_dirs_counter);
  316. }
  317. ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
  318. EXT4_INODES_PER_GROUP(sb) / 8);
  319. ext4_group_desc_csum_set(sb, block_group, gdp);
  320. ext4_unlock_group(sb, block_group);
  321. percpu_counter_inc(&sbi->s_freeinodes_counter);
  322. if (sbi->s_log_groups_per_flex) {
  323. ext4_group_t f = ext4_flex_group(sbi, block_group);
  324. atomic_inc(&sbi->s_flex_groups[f].free_inodes);
  325. if (is_directory)
  326. atomic_dec(&sbi->s_flex_groups[f].used_dirs);
  327. }
  328. BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
  329. fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
  330. out:
  331. if (cleared) {
  332. BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
  333. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  334. if (!fatal)
  335. fatal = err;
  336. } else {
  337. ext4_error(sb, "bit already cleared for inode %lu", ino);
  338. if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
  339. int count;
  340. count = ext4_free_inodes_count(sb, gdp);
  341. percpu_counter_sub(&sbi->s_freeinodes_counter,
  342. count);
  343. }
  344. set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
  345. }
  346. error_return:
  347. brelse(bitmap_bh);
  348. ext4_std_error(sb, fatal);
  349. }
  350. struct orlov_stats {
  351. __u64 free_clusters;
  352. __u32 free_inodes;
  353. __u32 used_dirs;
  354. };
  355. /*
  356. * Helper function for Orlov's allocator; returns critical information
  357. * for a particular block group or flex_bg. If flex_size is 1, then g
  358. * is a block group number; otherwise it is flex_bg number.
  359. */
  360. static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
  361. int flex_size, struct orlov_stats *stats)
  362. {
  363. struct ext4_group_desc *desc;
  364. struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
  365. if (flex_size > 1) {
  366. stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
  367. stats->free_clusters = atomic64_read(&flex_group[g].free_clusters);
  368. stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
  369. return;
  370. }
  371. desc = ext4_get_group_desc(sb, g, NULL);
  372. if (desc) {
  373. stats->free_inodes = ext4_free_inodes_count(sb, desc);
  374. stats->free_clusters = ext4_free_group_clusters(sb, desc);
  375. stats->used_dirs = ext4_used_dirs_count(sb, desc);
  376. } else {
  377. stats->free_inodes = 0;
  378. stats->free_clusters = 0;
  379. stats->used_dirs = 0;
  380. }
  381. }
  382. /*
  383. * Orlov's allocator for directories.
  384. *
  385. * We always try to spread first-level directories.
  386. *
  387. * If there are blockgroups with both free inodes and free blocks counts
  388. * not worse than average we return one with smallest directory count.
  389. * Otherwise we simply return a random group.
  390. *
  391. * For the rest rules look so:
  392. *
  393. * It's OK to put directory into a group unless
  394. * it has too many directories already (max_dirs) or
  395. * it has too few free inodes left (min_inodes) or
  396. * it has too few free blocks left (min_blocks) or
  397. * Parent's group is preferred, if it doesn't satisfy these
  398. * conditions we search cyclically through the rest. If none
  399. * of the groups look good we just look for a group with more
  400. * free inodes than average (starting at parent's group).
  401. */
  402. static int find_group_orlov(struct super_block *sb, struct inode *parent,
  403. ext4_group_t *group, umode_t mode,
  404. const struct qstr *qstr)
  405. {
  406. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  407. struct ext4_sb_info *sbi = EXT4_SB(sb);
  408. ext4_group_t real_ngroups = ext4_get_groups_count(sb);
  409. int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
  410. unsigned int freei, avefreei, grp_free;
  411. ext4_fsblk_t freeb, avefreec;
  412. unsigned int ndirs;
  413. int max_dirs, min_inodes;
  414. ext4_grpblk_t min_clusters;
  415. ext4_group_t i, grp, g, ngroups;
  416. struct ext4_group_desc *desc;
  417. struct orlov_stats stats;
  418. int flex_size = ext4_flex_bg_size(sbi);
  419. struct dx_hash_info hinfo;
  420. ngroups = real_ngroups;
  421. if (flex_size > 1) {
  422. ngroups = (real_ngroups + flex_size - 1) >>
  423. sbi->s_log_groups_per_flex;
  424. parent_group >>= sbi->s_log_groups_per_flex;
  425. }
  426. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  427. avefreei = freei / ngroups;
  428. freeb = EXT4_C2B(sbi,
  429. percpu_counter_read_positive(&sbi->s_freeclusters_counter));
  430. avefreec = freeb;
  431. do_div(avefreec, ngroups);
  432. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  433. if (S_ISDIR(mode) &&
  434. ((parent == d_inode(sb->s_root)) ||
  435. (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
  436. int best_ndir = inodes_per_group;
  437. int ret = -1;
  438. if (qstr) {
  439. hinfo.hash_version = DX_HASH_HALF_MD4;
  440. hinfo.seed = sbi->s_hash_seed;
  441. ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
  442. grp = hinfo.hash;
  443. } else
  444. grp = prandom_u32();
  445. parent_group = (unsigned)grp % ngroups;
  446. for (i = 0; i < ngroups; i++) {
  447. g = (parent_group + i) % ngroups;
  448. get_orlov_stats(sb, g, flex_size, &stats);
  449. if (!stats.free_inodes)
  450. continue;
  451. if (stats.used_dirs >= best_ndir)
  452. continue;
  453. if (stats.free_inodes < avefreei)
  454. continue;
  455. if (stats.free_clusters < avefreec)
  456. continue;
  457. grp = g;
  458. ret = 0;
  459. best_ndir = stats.used_dirs;
  460. }
  461. if (ret)
  462. goto fallback;
  463. found_flex_bg:
  464. if (flex_size == 1) {
  465. *group = grp;
  466. return 0;
  467. }
  468. /*
  469. * We pack inodes at the beginning of the flexgroup's
  470. * inode tables. Block allocation decisions will do
  471. * something similar, although regular files will
  472. * start at 2nd block group of the flexgroup. See
  473. * ext4_ext_find_goal() and ext4_find_near().
  474. */
  475. grp *= flex_size;
  476. for (i = 0; i < flex_size; i++) {
  477. if (grp+i >= real_ngroups)
  478. break;
  479. desc = ext4_get_group_desc(sb, grp+i, NULL);
  480. if (desc && ext4_free_inodes_count(sb, desc)) {
  481. *group = grp+i;
  482. return 0;
  483. }
  484. }
  485. goto fallback;
  486. }
  487. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  488. min_inodes = avefreei - inodes_per_group*flex_size / 4;
  489. if (min_inodes < 1)
  490. min_inodes = 1;
  491. min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
  492. /*
  493. * Start looking in the flex group where we last allocated an
  494. * inode for this parent directory
  495. */
  496. if (EXT4_I(parent)->i_last_alloc_group != ~0) {
  497. parent_group = EXT4_I(parent)->i_last_alloc_group;
  498. if (flex_size > 1)
  499. parent_group >>= sbi->s_log_groups_per_flex;
  500. }
  501. for (i = 0; i < ngroups; i++) {
  502. grp = (parent_group + i) % ngroups;
  503. get_orlov_stats(sb, grp, flex_size, &stats);
  504. if (stats.used_dirs >= max_dirs)
  505. continue;
  506. if (stats.free_inodes < min_inodes)
  507. continue;
  508. if (stats.free_clusters < min_clusters)
  509. continue;
  510. goto found_flex_bg;
  511. }
  512. fallback:
  513. ngroups = real_ngroups;
  514. avefreei = freei / ngroups;
  515. fallback_retry:
  516. parent_group = EXT4_I(parent)->i_block_group;
  517. for (i = 0; i < ngroups; i++) {
  518. grp = (parent_group + i) % ngroups;
  519. desc = ext4_get_group_desc(sb, grp, NULL);
  520. if (desc) {
  521. grp_free = ext4_free_inodes_count(sb, desc);
  522. if (grp_free && grp_free >= avefreei) {
  523. *group = grp;
  524. return 0;
  525. }
  526. }
  527. }
  528. if (avefreei) {
  529. /*
  530. * The free-inodes counter is approximate, and for really small
  531. * filesystems the above test can fail to find any blockgroups
  532. */
  533. avefreei = 0;
  534. goto fallback_retry;
  535. }
  536. return -1;
  537. }
  538. static int find_group_other(struct super_block *sb, struct inode *parent,
  539. ext4_group_t *group, umode_t mode)
  540. {
  541. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  542. ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
  543. struct ext4_group_desc *desc;
  544. int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
  545. /*
  546. * Try to place the inode is the same flex group as its
  547. * parent. If we can't find space, use the Orlov algorithm to
  548. * find another flex group, and store that information in the
  549. * parent directory's inode information so that use that flex
  550. * group for future allocations.
  551. */
  552. if (flex_size > 1) {
  553. int retry = 0;
  554. try_again:
  555. parent_group &= ~(flex_size-1);
  556. last = parent_group + flex_size;
  557. if (last > ngroups)
  558. last = ngroups;
  559. for (i = parent_group; i < last; i++) {
  560. desc = ext4_get_group_desc(sb, i, NULL);
  561. if (desc && ext4_free_inodes_count(sb, desc)) {
  562. *group = i;
  563. return 0;
  564. }
  565. }
  566. if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
  567. retry = 1;
  568. parent_group = EXT4_I(parent)->i_last_alloc_group;
  569. goto try_again;
  570. }
  571. /*
  572. * If this didn't work, use the Orlov search algorithm
  573. * to find a new flex group; we pass in the mode to
  574. * avoid the topdir algorithms.
  575. */
  576. *group = parent_group + flex_size;
  577. if (*group > ngroups)
  578. *group = 0;
  579. return find_group_orlov(sb, parent, group, mode, NULL);
  580. }
  581. /*
  582. * Try to place the inode in its parent directory
  583. */
  584. *group = parent_group;
  585. desc = ext4_get_group_desc(sb, *group, NULL);
  586. if (desc && ext4_free_inodes_count(sb, desc) &&
  587. ext4_free_group_clusters(sb, desc))
  588. return 0;
  589. /*
  590. * We're going to place this inode in a different blockgroup from its
  591. * parent. We want to cause files in a common directory to all land in
  592. * the same blockgroup. But we want files which are in a different
  593. * directory which shares a blockgroup with our parent to land in a
  594. * different blockgroup.
  595. *
  596. * So add our directory's i_ino into the starting point for the hash.
  597. */
  598. *group = (*group + parent->i_ino) % ngroups;
  599. /*
  600. * Use a quadratic hash to find a group with a free inode and some free
  601. * blocks.
  602. */
  603. for (i = 1; i < ngroups; i <<= 1) {
  604. *group += i;
  605. if (*group >= ngroups)
  606. *group -= ngroups;
  607. desc = ext4_get_group_desc(sb, *group, NULL);
  608. if (desc && ext4_free_inodes_count(sb, desc) &&
  609. ext4_free_group_clusters(sb, desc))
  610. return 0;
  611. }
  612. /*
  613. * That failed: try linear search for a free inode, even if that group
  614. * has no free blocks.
  615. */
  616. *group = parent_group;
  617. for (i = 0; i < ngroups; i++) {
  618. if (++*group >= ngroups)
  619. *group = 0;
  620. desc = ext4_get_group_desc(sb, *group, NULL);
  621. if (desc && ext4_free_inodes_count(sb, desc))
  622. return 0;
  623. }
  624. return -1;
  625. }
  626. /*
  627. * In no journal mode, if an inode has recently been deleted, we want
  628. * to avoid reusing it until we're reasonably sure the inode table
  629. * block has been written back to disk. (Yes, these values are
  630. * somewhat arbitrary...)
  631. */
  632. #define RECENTCY_MIN 5
  633. #define RECENTCY_DIRTY 300
  634. static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
  635. {
  636. struct ext4_group_desc *gdp;
  637. struct ext4_inode *raw_inode;
  638. struct buffer_head *bh;
  639. int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
  640. int offset, ret = 0;
  641. int recentcy = RECENTCY_MIN;
  642. u32 dtime, now;
  643. gdp = ext4_get_group_desc(sb, group, NULL);
  644. if (unlikely(!gdp))
  645. return 0;
  646. bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
  647. (ino / inodes_per_block));
  648. if (!bh || !buffer_uptodate(bh))
  649. /*
  650. * If the block is not in the buffer cache, then it
  651. * must have been written out.
  652. */
  653. goto out;
  654. offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
  655. raw_inode = (struct ext4_inode *) (bh->b_data + offset);
  656. /* i_dtime is only 32 bits on disk, but we only care about relative
  657. * times in the range of a few minutes (i.e. long enough to sync a
  658. * recently-deleted inode to disk), so using the low 32 bits of the
  659. * clock (a 68 year range) is enough, see time_before32() */
  660. dtime = le32_to_cpu(raw_inode->i_dtime);
  661. now = ktime_get_real_seconds();
  662. if (buffer_dirty(bh))
  663. recentcy += RECENTCY_DIRTY;
  664. if (dtime && time_before32(dtime, now) &&
  665. time_before32(now, dtime + recentcy))
  666. ret = 1;
  667. out:
  668. brelse(bh);
  669. return ret;
  670. }
  671. static int find_inode_bit(struct super_block *sb, ext4_group_t group,
  672. struct buffer_head *bitmap, unsigned long *ino)
  673. {
  674. next:
  675. *ino = ext4_find_next_zero_bit((unsigned long *)
  676. bitmap->b_data,
  677. EXT4_INODES_PER_GROUP(sb), *ino);
  678. if (*ino >= EXT4_INODES_PER_GROUP(sb))
  679. return 0;
  680. if ((EXT4_SB(sb)->s_journal == NULL) &&
  681. recently_deleted(sb, group, *ino)) {
  682. *ino = *ino + 1;
  683. if (*ino < EXT4_INODES_PER_GROUP(sb))
  684. goto next;
  685. return 0;
  686. }
  687. return 1;
  688. }
  689. /*
  690. * There are two policies for allocating an inode. If the new inode is
  691. * a directory, then a forward search is made for a block group with both
  692. * free space and a low directory-to-inode ratio; if that fails, then of
  693. * the groups with above-average free space, that group with the fewest
  694. * directories already is chosen.
  695. *
  696. * For other inodes, search forward from the parent directory's block
  697. * group to find a free inode.
  698. */
  699. struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
  700. umode_t mode, const struct qstr *qstr,
  701. __u32 goal, uid_t *owner, __u32 i_flags,
  702. int handle_type, unsigned int line_no,
  703. int nblocks)
  704. {
  705. struct super_block *sb;
  706. struct buffer_head *inode_bitmap_bh = NULL;
  707. struct buffer_head *group_desc_bh;
  708. ext4_group_t ngroups, group = 0;
  709. unsigned long ino = 0;
  710. struct inode *inode;
  711. struct ext4_group_desc *gdp = NULL;
  712. struct ext4_inode_info *ei;
  713. struct ext4_sb_info *sbi;
  714. int ret2, err;
  715. struct inode *ret;
  716. ext4_group_t i;
  717. ext4_group_t flex_group;
  718. struct ext4_group_info *grp;
  719. int encrypt = 0;
  720. /* Cannot create files in a deleted directory */
  721. if (!dir || !dir->i_nlink)
  722. return ERR_PTR(-EPERM);
  723. sb = dir->i_sb;
  724. sbi = EXT4_SB(sb);
  725. if (unlikely(ext4_forced_shutdown(sbi)))
  726. return ERR_PTR(-EIO);
  727. if ((ext4_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
  728. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
  729. !(i_flags & EXT4_EA_INODE_FL)) {
  730. err = fscrypt_get_encryption_info(dir);
  731. if (err)
  732. return ERR_PTR(err);
  733. if (!fscrypt_has_encryption_key(dir))
  734. return ERR_PTR(-ENOKEY);
  735. encrypt = 1;
  736. }
  737. if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
  738. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  739. struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
  740. if (IS_ERR(p))
  741. return ERR_CAST(p);
  742. if (p) {
  743. int acl_size = p->a_count * sizeof(ext4_acl_entry);
  744. nblocks += (S_ISDIR(mode) ? 2 : 1) *
  745. __ext4_xattr_set_credits(sb, NULL /* inode */,
  746. NULL /* block_bh */, acl_size,
  747. true /* is_create */);
  748. posix_acl_release(p);
  749. }
  750. #endif
  751. #ifdef CONFIG_SECURITY
  752. {
  753. int num_security_xattrs = 1;
  754. #ifdef CONFIG_INTEGRITY
  755. num_security_xattrs++;
  756. #endif
  757. /*
  758. * We assume that security xattrs are never
  759. * more than 1k. In practice they are under
  760. * 128 bytes.
  761. */
  762. nblocks += num_security_xattrs *
  763. __ext4_xattr_set_credits(sb, NULL /* inode */,
  764. NULL /* block_bh */, 1024,
  765. true /* is_create */);
  766. }
  767. #endif
  768. if (encrypt)
  769. nblocks += __ext4_xattr_set_credits(sb,
  770. NULL /* inode */, NULL /* block_bh */,
  771. FSCRYPT_SET_CONTEXT_MAX_SIZE,
  772. true /* is_create */);
  773. }
  774. ngroups = ext4_get_groups_count(sb);
  775. trace_ext4_request_inode(dir, mode);
  776. inode = new_inode(sb);
  777. if (!inode)
  778. return ERR_PTR(-ENOMEM);
  779. ei = EXT4_I(inode);
  780. /*
  781. * Initialize owners and quota early so that we don't have to account
  782. * for quota initialization worst case in standard inode creating
  783. * transaction
  784. */
  785. if (owner) {
  786. inode->i_mode = mode;
  787. i_uid_write(inode, owner[0]);
  788. i_gid_write(inode, owner[1]);
  789. } else if (test_opt(sb, GRPID)) {
  790. inode->i_mode = mode;
  791. inode->i_uid = current_fsuid();
  792. inode->i_gid = dir->i_gid;
  793. } else
  794. inode_init_owner(inode, dir, mode);
  795. if (ext4_has_feature_project(sb) &&
  796. ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
  797. ei->i_projid = EXT4_I(dir)->i_projid;
  798. else
  799. ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
  800. err = dquot_initialize(inode);
  801. if (err)
  802. goto out;
  803. if (!goal)
  804. goal = sbi->s_inode_goal;
  805. if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
  806. group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
  807. ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
  808. ret2 = 0;
  809. goto got_group;
  810. }
  811. if (S_ISDIR(mode))
  812. ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
  813. else
  814. ret2 = find_group_other(sb, dir, &group, mode);
  815. got_group:
  816. EXT4_I(dir)->i_last_alloc_group = group;
  817. err = -ENOSPC;
  818. if (ret2 == -1)
  819. goto out;
  820. /*
  821. * Normally we will only go through one pass of this loop,
  822. * unless we get unlucky and it turns out the group we selected
  823. * had its last inode grabbed by someone else.
  824. */
  825. for (i = 0; i < ngroups; i++, ino = 0) {
  826. err = -EIO;
  827. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  828. if (!gdp)
  829. goto out;
  830. /*
  831. * Check free inodes count before loading bitmap.
  832. */
  833. if (ext4_free_inodes_count(sb, gdp) == 0)
  834. goto next_group;
  835. grp = ext4_get_group_info(sb, group);
  836. /* Skip groups with already-known suspicious inode tables */
  837. if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
  838. goto next_group;
  839. brelse(inode_bitmap_bh);
  840. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  841. /* Skip groups with suspicious inode tables */
  842. if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
  843. IS_ERR(inode_bitmap_bh)) {
  844. inode_bitmap_bh = NULL;
  845. goto next_group;
  846. }
  847. repeat_in_this_group:
  848. ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
  849. if (!ret2)
  850. goto next_group;
  851. if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
  852. ext4_error(sb, "reserved inode found cleared - "
  853. "inode=%lu", ino + 1);
  854. goto next_group;
  855. }
  856. if (!handle) {
  857. BUG_ON(nblocks <= 0);
  858. handle = __ext4_journal_start_sb(dir->i_sb, line_no,
  859. handle_type, nblocks,
  860. 0);
  861. if (IS_ERR(handle)) {
  862. err = PTR_ERR(handle);
  863. ext4_std_error(sb, err);
  864. goto out;
  865. }
  866. }
  867. BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
  868. err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
  869. if (err) {
  870. ext4_std_error(sb, err);
  871. goto out;
  872. }
  873. ext4_lock_group(sb, group);
  874. ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
  875. if (ret2) {
  876. /* Someone already took the bit. Repeat the search
  877. * with lock held.
  878. */
  879. ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
  880. if (ret2) {
  881. ext4_set_bit(ino, inode_bitmap_bh->b_data);
  882. ret2 = 0;
  883. } else {
  884. ret2 = 1; /* we didn't grab the inode */
  885. }
  886. }
  887. ext4_unlock_group(sb, group);
  888. ino++; /* the inode bitmap is zero-based */
  889. if (!ret2)
  890. goto got; /* we grabbed the inode! */
  891. if (ino < EXT4_INODES_PER_GROUP(sb))
  892. goto repeat_in_this_group;
  893. next_group:
  894. if (++group == ngroups)
  895. group = 0;
  896. }
  897. err = -ENOSPC;
  898. goto out;
  899. got:
  900. BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
  901. err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
  902. if (err) {
  903. ext4_std_error(sb, err);
  904. goto out;
  905. }
  906. BUFFER_TRACE(group_desc_bh, "get_write_access");
  907. err = ext4_journal_get_write_access(handle, group_desc_bh);
  908. if (err) {
  909. ext4_std_error(sb, err);
  910. goto out;
  911. }
  912. /* We may have to initialize the block bitmap if it isn't already */
  913. if (ext4_has_group_desc_csum(sb) &&
  914. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  915. struct buffer_head *block_bitmap_bh;
  916. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  917. if (IS_ERR(block_bitmap_bh)) {
  918. err = PTR_ERR(block_bitmap_bh);
  919. goto out;
  920. }
  921. BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
  922. err = ext4_journal_get_write_access(handle, block_bitmap_bh);
  923. if (err) {
  924. brelse(block_bitmap_bh);
  925. ext4_std_error(sb, err);
  926. goto out;
  927. }
  928. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  929. err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
  930. /* recheck and clear flag under lock if we still need to */
  931. ext4_lock_group(sb, group);
  932. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  933. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  934. ext4_free_group_clusters_set(sb, gdp,
  935. ext4_free_clusters_after_init(sb, group, gdp));
  936. ext4_block_bitmap_csum_set(sb, group, gdp,
  937. block_bitmap_bh);
  938. ext4_group_desc_csum_set(sb, group, gdp);
  939. }
  940. ext4_unlock_group(sb, group);
  941. brelse(block_bitmap_bh);
  942. if (err) {
  943. ext4_std_error(sb, err);
  944. goto out;
  945. }
  946. }
  947. /* Update the relevant bg descriptor fields */
  948. if (ext4_has_group_desc_csum(sb)) {
  949. int free;
  950. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  951. down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
  952. ext4_lock_group(sb, group); /* while we modify the bg desc */
  953. free = EXT4_INODES_PER_GROUP(sb) -
  954. ext4_itable_unused_count(sb, gdp);
  955. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  956. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  957. free = 0;
  958. }
  959. /*
  960. * Check the relative inode number against the last used
  961. * relative inode number in this group. if it is greater
  962. * we need to update the bg_itable_unused count
  963. */
  964. if (ino > free)
  965. ext4_itable_unused_set(sb, gdp,
  966. (EXT4_INODES_PER_GROUP(sb) - ino));
  967. up_read(&grp->alloc_sem);
  968. } else {
  969. ext4_lock_group(sb, group);
  970. }
  971. ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
  972. if (S_ISDIR(mode)) {
  973. ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
  974. if (sbi->s_log_groups_per_flex) {
  975. ext4_group_t f = ext4_flex_group(sbi, group);
  976. atomic_inc(&sbi->s_flex_groups[f].used_dirs);
  977. }
  978. }
  979. if (ext4_has_group_desc_csum(sb)) {
  980. ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
  981. EXT4_INODES_PER_GROUP(sb) / 8);
  982. ext4_group_desc_csum_set(sb, group, gdp);
  983. }
  984. ext4_unlock_group(sb, group);
  985. BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
  986. err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
  987. if (err) {
  988. ext4_std_error(sb, err);
  989. goto out;
  990. }
  991. percpu_counter_dec(&sbi->s_freeinodes_counter);
  992. if (S_ISDIR(mode))
  993. percpu_counter_inc(&sbi->s_dirs_counter);
  994. if (sbi->s_log_groups_per_flex) {
  995. flex_group = ext4_flex_group(sbi, group);
  996. atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
  997. }
  998. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  999. /* This is the optimal IO size (for stat), not the fs block size */
  1000. inode->i_blocks = 0;
  1001. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  1002. current_time(inode);
  1003. memset(ei->i_data, 0, sizeof(ei->i_data));
  1004. ei->i_dir_start_lookup = 0;
  1005. ei->i_disksize = 0;
  1006. /* Don't inherit extent flag from directory, amongst others. */
  1007. ei->i_flags =
  1008. ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
  1009. ei->i_flags |= i_flags;
  1010. ei->i_file_acl = 0;
  1011. ei->i_dtime = 0;
  1012. ei->i_block_group = group;
  1013. ei->i_last_alloc_group = ~0;
  1014. ext4_set_inode_flags(inode);
  1015. if (IS_DIRSYNC(inode))
  1016. ext4_handle_sync(handle);
  1017. if (insert_inode_locked(inode) < 0) {
  1018. /*
  1019. * Likely a bitmap corruption causing inode to be allocated
  1020. * twice.
  1021. */
  1022. err = -EIO;
  1023. ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
  1024. inode->i_ino);
  1025. goto out;
  1026. }
  1027. inode->i_generation = prandom_u32();
  1028. /* Precompute checksum seed for inode metadata */
  1029. if (ext4_has_metadata_csum(sb)) {
  1030. __u32 csum;
  1031. __le32 inum = cpu_to_le32(inode->i_ino);
  1032. __le32 gen = cpu_to_le32(inode->i_generation);
  1033. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  1034. sizeof(inum));
  1035. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  1036. sizeof(gen));
  1037. }
  1038. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  1039. ext4_set_inode_state(inode, EXT4_STATE_NEW);
  1040. ei->i_extra_isize = sbi->s_want_extra_isize;
  1041. ei->i_inline_off = 0;
  1042. if (ext4_has_feature_inline_data(sb))
  1043. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  1044. ret = inode;
  1045. err = dquot_alloc_inode(inode);
  1046. if (err)
  1047. goto fail_drop;
  1048. /*
  1049. * Since the encryption xattr will always be unique, create it first so
  1050. * that it's less likely to end up in an external xattr block and
  1051. * prevent its deduplication.
  1052. */
  1053. if (encrypt) {
  1054. err = fscrypt_inherit_context(dir, inode, handle, true);
  1055. if (err)
  1056. goto fail_free_drop;
  1057. }
  1058. if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
  1059. err = ext4_init_acl(handle, inode, dir);
  1060. if (err)
  1061. goto fail_free_drop;
  1062. err = ext4_init_security(handle, inode, dir, qstr);
  1063. if (err)
  1064. goto fail_free_drop;
  1065. }
  1066. if (ext4_has_feature_extents(sb)) {
  1067. /* set extent flag only for directory, file and normal symlink*/
  1068. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  1069. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  1070. ext4_ext_tree_init(handle, inode);
  1071. }
  1072. }
  1073. if (ext4_handle_valid(handle)) {
  1074. ei->i_sync_tid = handle->h_transaction->t_tid;
  1075. ei->i_datasync_tid = handle->h_transaction->t_tid;
  1076. }
  1077. err = ext4_mark_inode_dirty(handle, inode);
  1078. if (err) {
  1079. ext4_std_error(sb, err);
  1080. goto fail_free_drop;
  1081. }
  1082. ext4_debug("allocating inode %lu\n", inode->i_ino);
  1083. trace_ext4_allocate_inode(inode, dir, mode);
  1084. brelse(inode_bitmap_bh);
  1085. return ret;
  1086. fail_free_drop:
  1087. dquot_free_inode(inode);
  1088. fail_drop:
  1089. clear_nlink(inode);
  1090. unlock_new_inode(inode);
  1091. out:
  1092. dquot_drop(inode);
  1093. inode->i_flags |= S_NOQUOTA;
  1094. iput(inode);
  1095. brelse(inode_bitmap_bh);
  1096. return ERR_PTR(err);
  1097. }
  1098. /* Verify that we are loading a valid orphan from disk */
  1099. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  1100. {
  1101. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  1102. ext4_group_t block_group;
  1103. int bit;
  1104. struct buffer_head *bitmap_bh = NULL;
  1105. struct inode *inode = NULL;
  1106. int err = -EFSCORRUPTED;
  1107. if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
  1108. goto bad_orphan;
  1109. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  1110. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  1111. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  1112. if (IS_ERR(bitmap_bh)) {
  1113. ext4_error(sb, "inode bitmap error %ld for orphan %lu",
  1114. ino, PTR_ERR(bitmap_bh));
  1115. return (struct inode *) bitmap_bh;
  1116. }
  1117. /* Having the inode bit set should be a 100% indicator that this
  1118. * is a valid orphan (no e2fsck run on fs). Orphans also include
  1119. * inodes that were being truncated, so we can't check i_nlink==0.
  1120. */
  1121. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  1122. goto bad_orphan;
  1123. inode = ext4_iget(sb, ino);
  1124. if (IS_ERR(inode)) {
  1125. err = PTR_ERR(inode);
  1126. ext4_error(sb, "couldn't read orphan inode %lu (err %d)",
  1127. ino, err);
  1128. return inode;
  1129. }
  1130. /*
  1131. * If the orphans has i_nlinks > 0 then it should be able to
  1132. * be truncated, otherwise it won't be removed from the orphan
  1133. * list during processing and an infinite loop will result.
  1134. * Similarly, it must not be a bad inode.
  1135. */
  1136. if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
  1137. is_bad_inode(inode))
  1138. goto bad_orphan;
  1139. if (NEXT_ORPHAN(inode) > max_ino)
  1140. goto bad_orphan;
  1141. brelse(bitmap_bh);
  1142. return inode;
  1143. bad_orphan:
  1144. ext4_error(sb, "bad orphan inode %lu", ino);
  1145. if (bitmap_bh)
  1146. printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  1147. bit, (unsigned long long)bitmap_bh->b_blocknr,
  1148. ext4_test_bit(bit, bitmap_bh->b_data));
  1149. if (inode) {
  1150. printk(KERN_ERR "is_bad_inode(inode)=%d\n",
  1151. is_bad_inode(inode));
  1152. printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
  1153. NEXT_ORPHAN(inode));
  1154. printk(KERN_ERR "max_ino=%lu\n", max_ino);
  1155. printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
  1156. /* Avoid freeing blocks if we got a bad deleted inode */
  1157. if (inode->i_nlink == 0)
  1158. inode->i_blocks = 0;
  1159. iput(inode);
  1160. }
  1161. brelse(bitmap_bh);
  1162. return ERR_PTR(err);
  1163. }
  1164. unsigned long ext4_count_free_inodes(struct super_block *sb)
  1165. {
  1166. unsigned long desc_count;
  1167. struct ext4_group_desc *gdp;
  1168. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1169. #ifdef EXT4FS_DEBUG
  1170. struct ext4_super_block *es;
  1171. unsigned long bitmap_count, x;
  1172. struct buffer_head *bitmap_bh = NULL;
  1173. es = EXT4_SB(sb)->s_es;
  1174. desc_count = 0;
  1175. bitmap_count = 0;
  1176. gdp = NULL;
  1177. for (i = 0; i < ngroups; i++) {
  1178. gdp = ext4_get_group_desc(sb, i, NULL);
  1179. if (!gdp)
  1180. continue;
  1181. desc_count += ext4_free_inodes_count(sb, gdp);
  1182. brelse(bitmap_bh);
  1183. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  1184. if (IS_ERR(bitmap_bh)) {
  1185. bitmap_bh = NULL;
  1186. continue;
  1187. }
  1188. x = ext4_count_free(bitmap_bh->b_data,
  1189. EXT4_INODES_PER_GROUP(sb) / 8);
  1190. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  1191. (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
  1192. bitmap_count += x;
  1193. }
  1194. brelse(bitmap_bh);
  1195. printk(KERN_DEBUG "ext4_count_free_inodes: "
  1196. "stored = %u, computed = %lu, %lu\n",
  1197. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  1198. return desc_count;
  1199. #else
  1200. desc_count = 0;
  1201. for (i = 0; i < ngroups; i++) {
  1202. gdp = ext4_get_group_desc(sb, i, NULL);
  1203. if (!gdp)
  1204. continue;
  1205. desc_count += ext4_free_inodes_count(sb, gdp);
  1206. cond_resched();
  1207. }
  1208. return desc_count;
  1209. #endif
  1210. }
  1211. /* Called at mount-time, super-block is locked */
  1212. unsigned long ext4_count_dirs(struct super_block * sb)
  1213. {
  1214. unsigned long count = 0;
  1215. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1216. for (i = 0; i < ngroups; i++) {
  1217. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1218. if (!gdp)
  1219. continue;
  1220. count += ext4_used_dirs_count(sb, gdp);
  1221. }
  1222. return count;
  1223. }
  1224. /*
  1225. * Zeroes not yet zeroed inode table - just write zeroes through the whole
  1226. * inode table. Must be called without any spinlock held. The only place
  1227. * where it is called from on active part of filesystem is ext4lazyinit
  1228. * thread, so we do not need any special locks, however we have to prevent
  1229. * inode allocation from the current group, so we take alloc_sem lock, to
  1230. * block ext4_new_inode() until we are finished.
  1231. */
  1232. int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
  1233. int barrier)
  1234. {
  1235. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  1236. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1237. struct ext4_group_desc *gdp = NULL;
  1238. struct buffer_head *group_desc_bh;
  1239. handle_t *handle;
  1240. ext4_fsblk_t blk;
  1241. int num, ret = 0, used_blks = 0;
  1242. /* This should not happen, but just to be sure check this */
  1243. if (sb_rdonly(sb)) {
  1244. ret = 1;
  1245. goto out;
  1246. }
  1247. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  1248. if (!gdp)
  1249. goto out;
  1250. /*
  1251. * We do not need to lock this, because we are the only one
  1252. * handling this flag.
  1253. */
  1254. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
  1255. goto out;
  1256. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  1257. if (IS_ERR(handle)) {
  1258. ret = PTR_ERR(handle);
  1259. goto out;
  1260. }
  1261. down_write(&grp->alloc_sem);
  1262. /*
  1263. * If inode bitmap was already initialized there may be some
  1264. * used inodes so we need to skip blocks with used inodes in
  1265. * inode table.
  1266. */
  1267. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
  1268. used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
  1269. ext4_itable_unused_count(sb, gdp)),
  1270. sbi->s_inodes_per_block);
  1271. if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
  1272. ext4_error(sb, "Something is wrong with group %u: "
  1273. "used itable blocks: %d; "
  1274. "itable unused count: %u",
  1275. group, used_blks,
  1276. ext4_itable_unused_count(sb, gdp));
  1277. ret = 1;
  1278. goto err_out;
  1279. }
  1280. blk = ext4_inode_table(sb, gdp) + used_blks;
  1281. num = sbi->s_itb_per_group - used_blks;
  1282. BUFFER_TRACE(group_desc_bh, "get_write_access");
  1283. ret = ext4_journal_get_write_access(handle,
  1284. group_desc_bh);
  1285. if (ret)
  1286. goto err_out;
  1287. /*
  1288. * Skip zeroout if the inode table is full. But we set the ZEROED
  1289. * flag anyway, because obviously, when it is full it does not need
  1290. * further zeroing.
  1291. */
  1292. if (unlikely(num == 0))
  1293. goto skip_zeroout;
  1294. ext4_debug("going to zero out inode table in group %d\n",
  1295. group);
  1296. ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
  1297. if (ret < 0)
  1298. goto err_out;
  1299. if (barrier)
  1300. blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
  1301. skip_zeroout:
  1302. ext4_lock_group(sb, group);
  1303. gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
  1304. ext4_group_desc_csum_set(sb, group, gdp);
  1305. ext4_unlock_group(sb, group);
  1306. BUFFER_TRACE(group_desc_bh,
  1307. "call ext4_handle_dirty_metadata");
  1308. ret = ext4_handle_dirty_metadata(handle, NULL,
  1309. group_desc_bh);
  1310. err_out:
  1311. up_write(&grp->alloc_sem);
  1312. ext4_journal_stop(handle);
  1313. out:
  1314. return ret;
  1315. }