ialloc.c 37 KB

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