ialloc.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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 (atomic_read(&inode->i_count) > 1) {
  232. ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
  233. __func__, __LINE__, inode->i_ino,
  234. atomic_read(&inode->i_count));
  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. bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
  626. (ino / inodes_per_block));
  627. if (!bh || !buffer_uptodate(bh))
  628. /*
  629. * If the block is not in the buffer cache, then it
  630. * must have been written out.
  631. */
  632. goto out;
  633. offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
  634. raw_inode = (struct ext4_inode *) (bh->b_data + offset);
  635. /* i_dtime is only 32 bits on disk, but we only care about relative
  636. * times in the range of a few minutes (i.e. long enough to sync a
  637. * recently-deleted inode to disk), so using the low 32 bits of the
  638. * clock (a 68 year range) is enough, see time_before32() */
  639. dtime = le32_to_cpu(raw_inode->i_dtime);
  640. now = ktime_get_real_seconds();
  641. if (buffer_dirty(bh))
  642. recentcy += RECENTCY_DIRTY;
  643. if (dtime && time_before32(dtime, now) &&
  644. time_before32(now, dtime + recentcy))
  645. ret = 1;
  646. out:
  647. brelse(bh);
  648. return ret;
  649. }
  650. static int find_inode_bit(struct super_block *sb, ext4_group_t group,
  651. struct buffer_head *bitmap, unsigned long *ino)
  652. {
  653. bool check_recently_deleted = EXT4_SB(sb)->s_journal == NULL;
  654. unsigned long recently_deleted_ino = EXT4_INODES_PER_GROUP(sb);
  655. next:
  656. *ino = ext4_find_next_zero_bit((unsigned long *)
  657. bitmap->b_data,
  658. EXT4_INODES_PER_GROUP(sb), *ino);
  659. if (*ino >= EXT4_INODES_PER_GROUP(sb))
  660. goto not_found;
  661. if (check_recently_deleted && recently_deleted(sb, group, *ino)) {
  662. recently_deleted_ino = *ino;
  663. *ino = *ino + 1;
  664. if (*ino < EXT4_INODES_PER_GROUP(sb))
  665. goto next;
  666. goto not_found;
  667. }
  668. return 1;
  669. not_found:
  670. if (recently_deleted_ino >= EXT4_INODES_PER_GROUP(sb))
  671. return 0;
  672. /*
  673. * Not reusing recently deleted inodes is mostly a preference. We don't
  674. * want to report ENOSPC or skew allocation patterns because of that.
  675. * So return even recently deleted inode if we could find better in the
  676. * given range.
  677. */
  678. *ino = recently_deleted_ino;
  679. return 1;
  680. }
  681. int ext4_mark_inode_used(struct super_block *sb, int ino)
  682. {
  683. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  684. struct buffer_head *inode_bitmap_bh = NULL, *group_desc_bh = NULL;
  685. struct ext4_group_desc *gdp;
  686. ext4_group_t group;
  687. int bit;
  688. int err;
  689. if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
  690. return -EFSCORRUPTED;
  691. group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  692. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  693. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  694. if (IS_ERR(inode_bitmap_bh))
  695. return PTR_ERR(inode_bitmap_bh);
  696. if (ext4_test_bit(bit, inode_bitmap_bh->b_data)) {
  697. err = 0;
  698. goto out;
  699. }
  700. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  701. if (!gdp) {
  702. err = -EINVAL;
  703. goto out;
  704. }
  705. ext4_set_bit(bit, inode_bitmap_bh->b_data);
  706. BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
  707. err = ext4_handle_dirty_metadata(NULL, NULL, inode_bitmap_bh);
  708. if (err) {
  709. ext4_std_error(sb, err);
  710. goto out;
  711. }
  712. err = sync_dirty_buffer(inode_bitmap_bh);
  713. if (err) {
  714. ext4_std_error(sb, err);
  715. goto out;
  716. }
  717. /* We may have to initialize the block bitmap if it isn't already */
  718. if (ext4_has_group_desc_csum(sb) &&
  719. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  720. struct buffer_head *block_bitmap_bh;
  721. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  722. if (IS_ERR(block_bitmap_bh)) {
  723. err = PTR_ERR(block_bitmap_bh);
  724. goto out;
  725. }
  726. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  727. err = ext4_handle_dirty_metadata(NULL, NULL, block_bitmap_bh);
  728. sync_dirty_buffer(block_bitmap_bh);
  729. /* recheck and clear flag under lock if we still need to */
  730. ext4_lock_group(sb, group);
  731. if (ext4_has_group_desc_csum(sb) &&
  732. (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
  733. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  734. ext4_free_group_clusters_set(sb, gdp,
  735. ext4_free_clusters_after_init(sb, group, gdp));
  736. ext4_block_bitmap_csum_set(sb, gdp, block_bitmap_bh);
  737. ext4_group_desc_csum_set(sb, group, gdp);
  738. }
  739. ext4_unlock_group(sb, group);
  740. brelse(block_bitmap_bh);
  741. if (err) {
  742. ext4_std_error(sb, err);
  743. goto out;
  744. }
  745. }
  746. /* Update the relevant bg descriptor fields */
  747. if (ext4_has_group_desc_csum(sb)) {
  748. int free;
  749. ext4_lock_group(sb, group); /* while we modify the bg desc */
  750. free = EXT4_INODES_PER_GROUP(sb) -
  751. ext4_itable_unused_count(sb, gdp);
  752. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  753. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  754. free = 0;
  755. }
  756. /*
  757. * Check the relative inode number against the last used
  758. * relative inode number in this group. if it is greater
  759. * we need to update the bg_itable_unused count
  760. */
  761. if (bit >= free)
  762. ext4_itable_unused_set(sb, gdp,
  763. (EXT4_INODES_PER_GROUP(sb) - bit - 1));
  764. } else {
  765. ext4_lock_group(sb, group);
  766. }
  767. ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
  768. if (ext4_has_group_desc_csum(sb)) {
  769. ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
  770. ext4_group_desc_csum_set(sb, group, gdp);
  771. }
  772. ext4_unlock_group(sb, group);
  773. err = ext4_handle_dirty_metadata(NULL, NULL, group_desc_bh);
  774. sync_dirty_buffer(group_desc_bh);
  775. out:
  776. brelse(inode_bitmap_bh);
  777. return err;
  778. }
  779. static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
  780. bool encrypt)
  781. {
  782. struct super_block *sb = dir->i_sb;
  783. int nblocks = 0;
  784. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  785. struct posix_acl *p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
  786. if (IS_ERR(p))
  787. return PTR_ERR(p);
  788. if (p) {
  789. int acl_size = p->a_count * sizeof(ext4_acl_entry);
  790. nblocks += (S_ISDIR(mode) ? 2 : 1) *
  791. __ext4_xattr_set_credits(sb, NULL /* inode */,
  792. NULL /* block_bh */, acl_size,
  793. true /* is_create */);
  794. posix_acl_release(p);
  795. }
  796. #endif
  797. #ifdef CONFIG_SECURITY
  798. {
  799. int num_security_xattrs = 1;
  800. #ifdef CONFIG_INTEGRITY
  801. num_security_xattrs++;
  802. #endif
  803. /*
  804. * We assume that security xattrs are never more than 1k.
  805. * In practice they are under 128 bytes.
  806. */
  807. nblocks += num_security_xattrs *
  808. __ext4_xattr_set_credits(sb, NULL /* inode */,
  809. NULL /* block_bh */, 1024,
  810. true /* is_create */);
  811. }
  812. #endif
  813. if (encrypt)
  814. nblocks += __ext4_xattr_set_credits(sb,
  815. NULL /* inode */,
  816. NULL /* block_bh */,
  817. FSCRYPT_SET_CONTEXT_MAX_SIZE,
  818. true /* is_create */);
  819. return nblocks;
  820. }
  821. /*
  822. * There are two policies for allocating an inode. If the new inode is
  823. * a directory, then a forward search is made for a block group with both
  824. * free space and a low directory-to-inode ratio; if that fails, then of
  825. * the groups with above-average free space, that group with the fewest
  826. * directories already is chosen.
  827. *
  828. * For other inodes, search forward from the parent directory's block
  829. * group to find a free inode.
  830. */
  831. struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
  832. handle_t *handle, struct inode *dir,
  833. umode_t mode, const struct qstr *qstr,
  834. __u32 goal, uid_t *owner, __u32 i_flags,
  835. int handle_type, unsigned int line_no,
  836. int nblocks)
  837. {
  838. struct super_block *sb;
  839. struct buffer_head *inode_bitmap_bh = NULL;
  840. struct buffer_head *group_desc_bh;
  841. ext4_group_t ngroups, group = 0;
  842. unsigned long ino = 0;
  843. struct inode *inode;
  844. struct ext4_group_desc *gdp = NULL;
  845. struct ext4_inode_info *ei;
  846. struct ext4_sb_info *sbi;
  847. int ret2, err;
  848. struct inode *ret;
  849. ext4_group_t i;
  850. ext4_group_t flex_group;
  851. struct ext4_group_info *grp = NULL;
  852. bool encrypt = false;
  853. /* Cannot create files in a deleted directory */
  854. if (!dir || !dir->i_nlink)
  855. return ERR_PTR(-EPERM);
  856. sb = dir->i_sb;
  857. sbi = EXT4_SB(sb);
  858. if (unlikely(ext4_forced_shutdown(sb)))
  859. return ERR_PTR(-EIO);
  860. ngroups = ext4_get_groups_count(sb);
  861. trace_ext4_request_inode(dir, mode);
  862. inode = new_inode(sb);
  863. if (!inode)
  864. return ERR_PTR(-ENOMEM);
  865. ei = EXT4_I(inode);
  866. /*
  867. * Initialize owners and quota early so that we don't have to account
  868. * for quota initialization worst case in standard inode creating
  869. * transaction
  870. */
  871. if (owner) {
  872. inode->i_mode = mode;
  873. i_uid_write(inode, owner[0]);
  874. i_gid_write(inode, owner[1]);
  875. } else if (test_opt(sb, GRPID)) {
  876. inode->i_mode = mode;
  877. inode_fsuid_set(inode, idmap);
  878. inode->i_gid = dir->i_gid;
  879. } else
  880. inode_init_owner(idmap, inode, dir, mode);
  881. if (ext4_has_feature_project(sb) &&
  882. ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
  883. ei->i_projid = EXT4_I(dir)->i_projid;
  884. else
  885. ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
  886. if (!(i_flags & EXT4_EA_INODE_FL)) {
  887. err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
  888. if (err)
  889. goto out;
  890. }
  891. err = dquot_initialize(inode);
  892. if (err)
  893. goto out;
  894. if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
  895. ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
  896. if (ret2 < 0) {
  897. err = ret2;
  898. goto out;
  899. }
  900. nblocks += ret2;
  901. }
  902. if (!goal)
  903. goal = sbi->s_inode_goal;
  904. if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
  905. group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
  906. ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
  907. ret2 = 0;
  908. goto got_group;
  909. }
  910. if (S_ISDIR(mode))
  911. ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
  912. else
  913. ret2 = find_group_other(sb, dir, &group, mode);
  914. got_group:
  915. EXT4_I(dir)->i_last_alloc_group = group;
  916. err = -ENOSPC;
  917. if (ret2 == -1)
  918. goto out;
  919. /*
  920. * Normally we will only go through one pass of this loop,
  921. * unless we get unlucky and it turns out the group we selected
  922. * had its last inode grabbed by someone else.
  923. */
  924. for (i = 0; i < ngroups; i++, ino = 0) {
  925. err = -EIO;
  926. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  927. if (!gdp)
  928. goto out;
  929. /*
  930. * Check free inodes count before loading bitmap.
  931. */
  932. if (ext4_free_inodes_count(sb, gdp) == 0)
  933. goto next_group;
  934. if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
  935. grp = ext4_get_group_info(sb, group);
  936. /*
  937. * Skip groups with already-known suspicious inode
  938. * tables
  939. */
  940. if (!grp || EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
  941. goto next_group;
  942. }
  943. brelse(inode_bitmap_bh);
  944. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  945. /* Skip groups with suspicious inode tables */
  946. if (IS_ERR(inode_bitmap_bh)) {
  947. inode_bitmap_bh = NULL;
  948. goto next_group;
  949. }
  950. if (!(sbi->s_mount_state & EXT4_FC_REPLAY) &&
  951. EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
  952. goto next_group;
  953. ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
  954. if (!ret2)
  955. goto next_group;
  956. if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
  957. ext4_error(sb, "reserved inode found cleared - "
  958. "inode=%lu", ino + 1);
  959. ext4_mark_group_bitmap_corrupted(sb, group,
  960. EXT4_GROUP_INFO_IBITMAP_CORRUPT);
  961. goto next_group;
  962. }
  963. if ((!(sbi->s_mount_state & EXT4_FC_REPLAY)) && !handle) {
  964. BUG_ON(nblocks <= 0);
  965. handle = __ext4_journal_start_sb(NULL, dir->i_sb,
  966. line_no, handle_type, nblocks, 0,
  967. ext4_trans_default_revoke_credits(sb));
  968. if (IS_ERR(handle)) {
  969. err = PTR_ERR(handle);
  970. ext4_std_error(sb, err);
  971. goto out;
  972. }
  973. }
  974. BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
  975. err = ext4_journal_get_write_access(handle, sb, inode_bitmap_bh,
  976. EXT4_JTR_NONE);
  977. if (err) {
  978. ext4_std_error(sb, err);
  979. goto out;
  980. }
  981. ext4_lock_group(sb, group);
  982. ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
  983. if (ret2) {
  984. /* Someone already took the bit. Repeat the search
  985. * with lock held.
  986. */
  987. ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
  988. if (ret2) {
  989. ext4_set_bit(ino, inode_bitmap_bh->b_data);
  990. ret2 = 0;
  991. } else {
  992. ret2 = 1; /* we didn't grab the inode */
  993. }
  994. }
  995. ext4_unlock_group(sb, group);
  996. ino++; /* the inode bitmap is zero-based */
  997. if (!ret2)
  998. goto got; /* we grabbed the inode! */
  999. next_group:
  1000. if (++group == ngroups)
  1001. group = 0;
  1002. }
  1003. err = -ENOSPC;
  1004. goto out;
  1005. got:
  1006. BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
  1007. err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
  1008. if (err) {
  1009. ext4_std_error(sb, err);
  1010. goto out;
  1011. }
  1012. BUFFER_TRACE(group_desc_bh, "get_write_access");
  1013. err = ext4_journal_get_write_access(handle, sb, group_desc_bh,
  1014. EXT4_JTR_NONE);
  1015. if (err) {
  1016. ext4_std_error(sb, err);
  1017. goto out;
  1018. }
  1019. /* We may have to initialize the block bitmap if it isn't already */
  1020. if (ext4_has_group_desc_csum(sb) &&
  1021. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  1022. struct buffer_head *block_bitmap_bh;
  1023. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  1024. if (IS_ERR(block_bitmap_bh)) {
  1025. err = PTR_ERR(block_bitmap_bh);
  1026. goto out;
  1027. }
  1028. BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
  1029. err = ext4_journal_get_write_access(handle, sb, block_bitmap_bh,
  1030. EXT4_JTR_NONE);
  1031. if (err) {
  1032. brelse(block_bitmap_bh);
  1033. ext4_std_error(sb, err);
  1034. goto out;
  1035. }
  1036. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  1037. err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
  1038. /* recheck and clear flag under lock if we still need to */
  1039. ext4_lock_group(sb, group);
  1040. if (ext4_has_group_desc_csum(sb) &&
  1041. (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
  1042. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  1043. ext4_free_group_clusters_set(sb, gdp,
  1044. ext4_free_clusters_after_init(sb, group, gdp));
  1045. ext4_block_bitmap_csum_set(sb, gdp, block_bitmap_bh);
  1046. ext4_group_desc_csum_set(sb, group, gdp);
  1047. }
  1048. ext4_unlock_group(sb, group);
  1049. brelse(block_bitmap_bh);
  1050. if (err) {
  1051. ext4_std_error(sb, err);
  1052. goto out;
  1053. }
  1054. }
  1055. /* Update the relevant bg descriptor fields */
  1056. if (ext4_has_group_desc_csum(sb)) {
  1057. int free;
  1058. struct ext4_group_info *grp = NULL;
  1059. if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
  1060. grp = ext4_get_group_info(sb, group);
  1061. if (!grp) {
  1062. err = -EFSCORRUPTED;
  1063. goto out;
  1064. }
  1065. down_read(&grp->alloc_sem); /*
  1066. * protect vs itable
  1067. * lazyinit
  1068. */
  1069. }
  1070. ext4_lock_group(sb, group); /* while we modify the bg desc */
  1071. free = EXT4_INODES_PER_GROUP(sb) -
  1072. ext4_itable_unused_count(sb, gdp);
  1073. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  1074. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  1075. free = 0;
  1076. }
  1077. /*
  1078. * Check the relative inode number against the last used
  1079. * relative inode number in this group. if it is greater
  1080. * we need to update the bg_itable_unused count
  1081. */
  1082. if (ino > free)
  1083. ext4_itable_unused_set(sb, gdp,
  1084. (EXT4_INODES_PER_GROUP(sb) - ino));
  1085. if (!(sbi->s_mount_state & EXT4_FC_REPLAY))
  1086. up_read(&grp->alloc_sem);
  1087. } else {
  1088. ext4_lock_group(sb, group);
  1089. }
  1090. ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
  1091. if (S_ISDIR(mode)) {
  1092. ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
  1093. if (sbi->s_log_groups_per_flex) {
  1094. ext4_group_t f = ext4_flex_group(sbi, group);
  1095. atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
  1096. f)->used_dirs);
  1097. }
  1098. }
  1099. if (ext4_has_group_desc_csum(sb)) {
  1100. ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh);
  1101. ext4_group_desc_csum_set(sb, group, gdp);
  1102. }
  1103. ext4_unlock_group(sb, group);
  1104. BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
  1105. err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
  1106. if (err) {
  1107. ext4_std_error(sb, err);
  1108. goto out;
  1109. }
  1110. percpu_counter_dec(&sbi->s_freeinodes_counter);
  1111. if (S_ISDIR(mode))
  1112. percpu_counter_inc(&sbi->s_dirs_counter);
  1113. if (sbi->s_log_groups_per_flex) {
  1114. flex_group = ext4_flex_group(sbi, group);
  1115. atomic_dec(&sbi_array_rcu_deref(sbi, s_flex_groups,
  1116. flex_group)->free_inodes);
  1117. }
  1118. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  1119. /* This is the optimal IO size (for stat), not the fs block size */
  1120. inode->i_blocks = 0;
  1121. simple_inode_init_ts(inode);
  1122. ei->i_crtime = inode_get_mtime(inode);
  1123. memset(ei->i_data, 0, sizeof(ei->i_data));
  1124. ei->i_dir_start_lookup = 0;
  1125. ei->i_disksize = 0;
  1126. /* Don't inherit extent flag from directory, amongst others. */
  1127. ei->i_flags =
  1128. ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
  1129. ei->i_flags |= i_flags;
  1130. ei->i_file_acl = 0;
  1131. ei->i_dtime = 0;
  1132. ei->i_block_group = group;
  1133. ei->i_last_alloc_group = ~0;
  1134. ext4_set_inode_flags(inode, true);
  1135. if (IS_DIRSYNC(inode))
  1136. ext4_handle_sync(handle);
  1137. if (insert_inode_locked(inode) < 0) {
  1138. /*
  1139. * Likely a bitmap corruption causing inode to be allocated
  1140. * twice.
  1141. */
  1142. err = -EIO;
  1143. ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
  1144. inode->i_ino);
  1145. ext4_mark_group_bitmap_corrupted(sb, group,
  1146. EXT4_GROUP_INFO_IBITMAP_CORRUPT);
  1147. goto out;
  1148. }
  1149. inode->i_generation = get_random_u32();
  1150. /* Precompute checksum seed for inode metadata */
  1151. if (ext4_has_metadata_csum(sb)) {
  1152. __u32 csum;
  1153. __le32 inum = cpu_to_le32(inode->i_ino);
  1154. __le32 gen = cpu_to_le32(inode->i_generation);
  1155. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  1156. sizeof(inum));
  1157. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  1158. sizeof(gen));
  1159. }
  1160. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  1161. ext4_set_inode_state(inode, EXT4_STATE_NEW);
  1162. ei->i_extra_isize = sbi->s_want_extra_isize;
  1163. ei->i_inline_off = 0;
  1164. if (ext4_has_feature_inline_data(sb) &&
  1165. (!(ei->i_flags & EXT4_DAX_FL) || S_ISDIR(mode)))
  1166. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  1167. ret = inode;
  1168. err = dquot_alloc_inode(inode);
  1169. if (err)
  1170. goto fail_drop;
  1171. /*
  1172. * Since the encryption xattr will always be unique, create it first so
  1173. * that it's less likely to end up in an external xattr block and
  1174. * prevent its deduplication.
  1175. */
  1176. if (encrypt) {
  1177. err = fscrypt_set_context(inode, handle);
  1178. if (err)
  1179. goto fail_free_drop;
  1180. }
  1181. if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
  1182. err = ext4_init_acl(handle, inode, dir);
  1183. if (err)
  1184. goto fail_free_drop;
  1185. err = ext4_init_security(handle, inode, dir, qstr);
  1186. if (err)
  1187. goto fail_free_drop;
  1188. }
  1189. if (ext4_has_feature_extents(sb)) {
  1190. /* set extent flag only for directory, file and normal symlink*/
  1191. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  1192. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  1193. ext4_ext_tree_init(handle, inode);
  1194. }
  1195. }
  1196. ext4_update_inode_fsync_trans(handle, inode, 1);
  1197. err = ext4_mark_inode_dirty(handle, inode);
  1198. if (err) {
  1199. ext4_std_error(sb, err);
  1200. goto fail_free_drop;
  1201. }
  1202. ext4_debug("allocating inode %lu\n", inode->i_ino);
  1203. trace_ext4_allocate_inode(inode, dir, mode);
  1204. brelse(inode_bitmap_bh);
  1205. return ret;
  1206. fail_free_drop:
  1207. dquot_free_inode(inode);
  1208. fail_drop:
  1209. clear_nlink(inode);
  1210. unlock_new_inode(inode);
  1211. out:
  1212. dquot_drop(inode);
  1213. inode->i_flags |= S_NOQUOTA;
  1214. iput(inode);
  1215. brelse(inode_bitmap_bh);
  1216. return ERR_PTR(err);
  1217. }
  1218. /* Verify that we are loading a valid orphan from disk */
  1219. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  1220. {
  1221. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  1222. ext4_group_t block_group;
  1223. int bit;
  1224. struct buffer_head *bitmap_bh = NULL;
  1225. struct inode *inode = NULL;
  1226. int err = -EFSCORRUPTED;
  1227. if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
  1228. goto bad_orphan;
  1229. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  1230. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  1231. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  1232. if (IS_ERR(bitmap_bh))
  1233. return ERR_CAST(bitmap_bh);
  1234. /* Having the inode bit set should be a 100% indicator that this
  1235. * is a valid orphan (no e2fsck run on fs). Orphans also include
  1236. * inodes that were being truncated, so we can't check i_nlink==0.
  1237. */
  1238. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  1239. goto bad_orphan;
  1240. inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
  1241. if (IS_ERR(inode)) {
  1242. err = PTR_ERR(inode);
  1243. ext4_error_err(sb, -err,
  1244. "couldn't read orphan inode %lu (err %d)",
  1245. ino, err);
  1246. brelse(bitmap_bh);
  1247. return inode;
  1248. }
  1249. /*
  1250. * If the orphans has i_nlinks > 0 then it should be able to
  1251. * be truncated, otherwise it won't be removed from the orphan
  1252. * list during processing and an infinite loop will result.
  1253. * Similarly, it must not be a bad inode.
  1254. */
  1255. if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
  1256. is_bad_inode(inode))
  1257. goto bad_orphan;
  1258. if (NEXT_ORPHAN(inode) > max_ino)
  1259. goto bad_orphan;
  1260. brelse(bitmap_bh);
  1261. return inode;
  1262. bad_orphan:
  1263. ext4_error(sb, "bad orphan inode %lu", ino);
  1264. if (bitmap_bh)
  1265. printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  1266. bit, (unsigned long long)bitmap_bh->b_blocknr,
  1267. ext4_test_bit(bit, bitmap_bh->b_data));
  1268. if (inode) {
  1269. printk(KERN_ERR "is_bad_inode(inode)=%d\n",
  1270. is_bad_inode(inode));
  1271. printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
  1272. NEXT_ORPHAN(inode));
  1273. printk(KERN_ERR "max_ino=%lu\n", max_ino);
  1274. printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
  1275. /* Avoid freeing blocks if we got a bad deleted inode */
  1276. if (inode->i_nlink == 0)
  1277. inode->i_blocks = 0;
  1278. iput(inode);
  1279. }
  1280. brelse(bitmap_bh);
  1281. return ERR_PTR(err);
  1282. }
  1283. unsigned long ext4_count_free_inodes(struct super_block *sb)
  1284. {
  1285. unsigned long desc_count;
  1286. struct ext4_group_desc *gdp;
  1287. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1288. #ifdef EXT4FS_DEBUG
  1289. struct ext4_super_block *es;
  1290. unsigned long bitmap_count, x;
  1291. struct buffer_head *bitmap_bh = NULL;
  1292. es = EXT4_SB(sb)->s_es;
  1293. desc_count = 0;
  1294. bitmap_count = 0;
  1295. gdp = NULL;
  1296. for (i = 0; i < ngroups; i++) {
  1297. gdp = ext4_get_group_desc(sb, i, NULL);
  1298. if (!gdp)
  1299. continue;
  1300. desc_count += ext4_free_inodes_count(sb, gdp);
  1301. brelse(bitmap_bh);
  1302. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  1303. if (IS_ERR(bitmap_bh)) {
  1304. bitmap_bh = NULL;
  1305. continue;
  1306. }
  1307. x = ext4_count_free(bitmap_bh->b_data,
  1308. EXT4_INODES_PER_GROUP(sb) / 8);
  1309. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  1310. (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
  1311. bitmap_count += x;
  1312. }
  1313. brelse(bitmap_bh);
  1314. printk(KERN_DEBUG "ext4_count_free_inodes: "
  1315. "stored = %u, computed = %lu, %lu\n",
  1316. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  1317. return desc_count;
  1318. #else
  1319. desc_count = 0;
  1320. for (i = 0; i < ngroups; i++) {
  1321. gdp = ext4_get_group_desc(sb, i, NULL);
  1322. if (!gdp)
  1323. continue;
  1324. desc_count += ext4_free_inodes_count(sb, gdp);
  1325. cond_resched();
  1326. }
  1327. return desc_count;
  1328. #endif
  1329. }
  1330. /* Called at mount-time, super-block is locked */
  1331. unsigned long ext4_count_dirs(struct super_block * sb)
  1332. {
  1333. unsigned long count = 0;
  1334. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1335. for (i = 0; i < ngroups; i++) {
  1336. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1337. if (!gdp)
  1338. continue;
  1339. count += ext4_used_dirs_count(sb, gdp);
  1340. }
  1341. return count;
  1342. }
  1343. /*
  1344. * Zeroes not yet zeroed inode table - just write zeroes through the whole
  1345. * inode table. Must be called without any spinlock held. The only place
  1346. * where it is called from on active part of filesystem is ext4lazyinit
  1347. * thread, so we do not need any special locks, however we have to prevent
  1348. * inode allocation from the current group, so we take alloc_sem lock, to
  1349. * block ext4_new_inode() until we are finished.
  1350. */
  1351. int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
  1352. int barrier)
  1353. {
  1354. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  1355. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1356. struct ext4_group_desc *gdp = NULL;
  1357. struct buffer_head *group_desc_bh;
  1358. handle_t *handle;
  1359. ext4_fsblk_t blk;
  1360. int num, ret = 0, used_blks = 0;
  1361. unsigned long used_inos = 0;
  1362. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  1363. if (!gdp || !grp)
  1364. goto out;
  1365. /*
  1366. * We do not need to lock this, because we are the only one
  1367. * handling this flag.
  1368. */
  1369. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
  1370. goto out;
  1371. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  1372. if (IS_ERR(handle)) {
  1373. ret = PTR_ERR(handle);
  1374. goto out;
  1375. }
  1376. down_write(&grp->alloc_sem);
  1377. /*
  1378. * If inode bitmap was already initialized there may be some
  1379. * used inodes so we need to skip blocks with used inodes in
  1380. * inode table.
  1381. */
  1382. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
  1383. used_inos = EXT4_INODES_PER_GROUP(sb) -
  1384. ext4_itable_unused_count(sb, gdp);
  1385. used_blks = DIV_ROUND_UP(used_inos, sbi->s_inodes_per_block);
  1386. /* Bogus inode unused count? */
  1387. if (used_blks < 0 || used_blks > sbi->s_itb_per_group) {
  1388. ext4_error(sb, "Something is wrong with group %u: "
  1389. "used itable blocks: %d; "
  1390. "itable unused count: %u",
  1391. group, used_blks,
  1392. ext4_itable_unused_count(sb, gdp));
  1393. ret = 1;
  1394. goto err_out;
  1395. }
  1396. used_inos += group * EXT4_INODES_PER_GROUP(sb);
  1397. /*
  1398. * Are there some uninitialized inodes in the inode table
  1399. * before the first normal inode?
  1400. */
  1401. if ((used_blks != sbi->s_itb_per_group) &&
  1402. (used_inos < EXT4_FIRST_INO(sb))) {
  1403. ext4_error(sb, "Something is wrong with group %u: "
  1404. "itable unused count: %u; "
  1405. "itables initialized count: %ld",
  1406. group, ext4_itable_unused_count(sb, gdp),
  1407. used_inos);
  1408. ret = 1;
  1409. goto err_out;
  1410. }
  1411. }
  1412. blk = ext4_inode_table(sb, gdp) + used_blks;
  1413. num = sbi->s_itb_per_group - used_blks;
  1414. BUFFER_TRACE(group_desc_bh, "get_write_access");
  1415. ret = ext4_journal_get_write_access(handle, sb, group_desc_bh,
  1416. EXT4_JTR_NONE);
  1417. if (ret)
  1418. goto err_out;
  1419. /*
  1420. * Skip zeroout if the inode table is full. But we set the ZEROED
  1421. * flag anyway, because obviously, when it is full it does not need
  1422. * further zeroing.
  1423. */
  1424. if (unlikely(num == 0))
  1425. goto skip_zeroout;
  1426. ext4_debug("going to zero out inode table in group %d\n",
  1427. group);
  1428. ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
  1429. if (ret < 0)
  1430. goto err_out;
  1431. if (barrier)
  1432. blkdev_issue_flush(sb->s_bdev);
  1433. skip_zeroout:
  1434. ext4_lock_group(sb, group);
  1435. gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
  1436. ext4_group_desc_csum_set(sb, group, gdp);
  1437. ext4_unlock_group(sb, group);
  1438. BUFFER_TRACE(group_desc_bh,
  1439. "call ext4_handle_dirty_metadata");
  1440. ret = ext4_handle_dirty_metadata(handle, NULL,
  1441. group_desc_bh);
  1442. err_out:
  1443. up_write(&grp->alloc_sem);
  1444. ext4_journal_stop(handle);
  1445. out:
  1446. return ret;
  1447. }