ialloc.c 39 KB

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