ialloc.c 38 KB

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