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