ialloc.c 38 KB

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