ialloc.c 45 KB

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