ialloc.c 38 KB

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