ialloc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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(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 = ext4_get_encryption_info(dir);
  683. if (err)
  684. return ERR_PTR(err);
  685. if (ext4_encryption_info(dir) == NULL)
  686. return ERR_PTR(-EPERM);
  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. * Initalize 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. err = dquot_initialize(inode);
  715. if (err)
  716. goto out;
  717. if (!goal)
  718. goal = sbi->s_inode_goal;
  719. if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
  720. group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
  721. ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
  722. ret2 = 0;
  723. goto got_group;
  724. }
  725. if (S_ISDIR(mode))
  726. ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
  727. else
  728. ret2 = find_group_other(sb, dir, &group, mode);
  729. got_group:
  730. EXT4_I(dir)->i_last_alloc_group = group;
  731. err = -ENOSPC;
  732. if (ret2 == -1)
  733. goto out;
  734. /*
  735. * Normally we will only go through one pass of this loop,
  736. * unless we get unlucky and it turns out the group we selected
  737. * had its last inode grabbed by someone else.
  738. */
  739. for (i = 0; i < ngroups; i++, ino = 0) {
  740. err = -EIO;
  741. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  742. if (!gdp)
  743. goto out;
  744. /*
  745. * Check free inodes count before loading bitmap.
  746. */
  747. if (ext4_free_inodes_count(sb, gdp) == 0) {
  748. if (++group == ngroups)
  749. group = 0;
  750. continue;
  751. }
  752. grp = ext4_get_group_info(sb, group);
  753. /* Skip groups with already-known suspicious inode tables */
  754. if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
  755. if (++group == ngroups)
  756. group = 0;
  757. continue;
  758. }
  759. brelse(inode_bitmap_bh);
  760. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  761. /* Skip groups with suspicious inode tables */
  762. if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
  763. IS_ERR(inode_bitmap_bh)) {
  764. inode_bitmap_bh = NULL;
  765. if (++group == ngroups)
  766. group = 0;
  767. continue;
  768. }
  769. repeat_in_this_group:
  770. ino = ext4_find_next_zero_bit((unsigned long *)
  771. inode_bitmap_bh->b_data,
  772. EXT4_INODES_PER_GROUP(sb), ino);
  773. if (ino >= EXT4_INODES_PER_GROUP(sb))
  774. goto next_group;
  775. if (group == 0 && (ino+1) < EXT4_FIRST_INO(sb)) {
  776. ext4_error(sb, "reserved inode found cleared - "
  777. "inode=%lu", ino + 1);
  778. continue;
  779. }
  780. if ((EXT4_SB(sb)->s_journal == NULL) &&
  781. recently_deleted(sb, group, ino)) {
  782. ino++;
  783. goto next_inode;
  784. }
  785. if (!handle) {
  786. BUG_ON(nblocks <= 0);
  787. handle = __ext4_journal_start_sb(dir->i_sb, line_no,
  788. handle_type, nblocks,
  789. 0);
  790. if (IS_ERR(handle)) {
  791. err = PTR_ERR(handle);
  792. ext4_std_error(sb, err);
  793. goto out;
  794. }
  795. }
  796. BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
  797. err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
  798. if (err) {
  799. ext4_std_error(sb, err);
  800. goto out;
  801. }
  802. ext4_lock_group(sb, group);
  803. ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
  804. ext4_unlock_group(sb, group);
  805. ino++; /* the inode bitmap is zero-based */
  806. if (!ret2)
  807. goto got; /* we grabbed the inode! */
  808. next_inode:
  809. if (ino < EXT4_INODES_PER_GROUP(sb))
  810. goto repeat_in_this_group;
  811. next_group:
  812. if (++group == ngroups)
  813. group = 0;
  814. }
  815. err = -ENOSPC;
  816. goto out;
  817. got:
  818. BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
  819. err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
  820. if (err) {
  821. ext4_std_error(sb, err);
  822. goto out;
  823. }
  824. BUFFER_TRACE(group_desc_bh, "get_write_access");
  825. err = ext4_journal_get_write_access(handle, group_desc_bh);
  826. if (err) {
  827. ext4_std_error(sb, err);
  828. goto out;
  829. }
  830. /* We may have to initialize the block bitmap if it isn't already */
  831. if (ext4_has_group_desc_csum(sb) &&
  832. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  833. struct buffer_head *block_bitmap_bh;
  834. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  835. if (IS_ERR(block_bitmap_bh)) {
  836. err = PTR_ERR(block_bitmap_bh);
  837. goto out;
  838. }
  839. BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
  840. err = ext4_journal_get_write_access(handle, block_bitmap_bh);
  841. if (err) {
  842. brelse(block_bitmap_bh);
  843. ext4_std_error(sb, err);
  844. goto out;
  845. }
  846. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  847. err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
  848. /* recheck and clear flag under lock if we still need to */
  849. ext4_lock_group(sb, group);
  850. if (ext4_has_group_desc_csum(sb) &&
  851. (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
  852. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  853. ext4_free_group_clusters_set(sb, gdp,
  854. ext4_free_clusters_after_init(sb, group, gdp));
  855. ext4_block_bitmap_csum_set(sb, group, gdp,
  856. block_bitmap_bh);
  857. ext4_group_desc_csum_set(sb, group, gdp);
  858. }
  859. ext4_unlock_group(sb, group);
  860. brelse(block_bitmap_bh);
  861. if (err) {
  862. ext4_std_error(sb, err);
  863. goto out;
  864. }
  865. }
  866. /* Update the relevant bg descriptor fields */
  867. if (ext4_has_group_desc_csum(sb)) {
  868. int free;
  869. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  870. down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
  871. ext4_lock_group(sb, group); /* while we modify the bg desc */
  872. free = EXT4_INODES_PER_GROUP(sb) -
  873. ext4_itable_unused_count(sb, gdp);
  874. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  875. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  876. free = 0;
  877. }
  878. /*
  879. * Check the relative inode number against the last used
  880. * relative inode number in this group. if it is greater
  881. * we need to update the bg_itable_unused count
  882. */
  883. if (ino > free)
  884. ext4_itable_unused_set(sb, gdp,
  885. (EXT4_INODES_PER_GROUP(sb) - ino));
  886. up_read(&grp->alloc_sem);
  887. } else {
  888. ext4_lock_group(sb, group);
  889. }
  890. ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
  891. if (S_ISDIR(mode)) {
  892. ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
  893. if (sbi->s_log_groups_per_flex) {
  894. ext4_group_t f = ext4_flex_group(sbi, group);
  895. atomic_inc(&sbi->s_flex_groups[f].used_dirs);
  896. }
  897. }
  898. if (ext4_has_group_desc_csum(sb)) {
  899. ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
  900. EXT4_INODES_PER_GROUP(sb) / 8);
  901. ext4_group_desc_csum_set(sb, group, gdp);
  902. }
  903. ext4_unlock_group(sb, group);
  904. BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
  905. err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
  906. if (err) {
  907. ext4_std_error(sb, err);
  908. goto out;
  909. }
  910. percpu_counter_dec(&sbi->s_freeinodes_counter);
  911. if (S_ISDIR(mode))
  912. percpu_counter_inc(&sbi->s_dirs_counter);
  913. if (sbi->s_log_groups_per_flex) {
  914. flex_group = ext4_flex_group(sbi, group);
  915. atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
  916. }
  917. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  918. /* This is the optimal IO size (for stat), not the fs block size */
  919. inode->i_blocks = 0;
  920. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  921. ext4_current_time(inode);
  922. memset(ei->i_data, 0, sizeof(ei->i_data));
  923. ei->i_dir_start_lookup = 0;
  924. ei->i_disksize = 0;
  925. /* Don't inherit extent flag from directory, amongst others. */
  926. ei->i_flags =
  927. ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
  928. ei->i_file_acl = 0;
  929. ei->i_dtime = 0;
  930. ei->i_block_group = group;
  931. ei->i_last_alloc_group = ~0;
  932. ext4_set_inode_flags(inode);
  933. if (IS_DIRSYNC(inode))
  934. ext4_handle_sync(handle);
  935. if (insert_inode_locked(inode) < 0) {
  936. /*
  937. * Likely a bitmap corruption causing inode to be allocated
  938. * twice.
  939. */
  940. err = -EIO;
  941. ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
  942. inode->i_ino);
  943. goto out;
  944. }
  945. spin_lock(&sbi->s_next_gen_lock);
  946. inode->i_generation = sbi->s_next_generation++;
  947. spin_unlock(&sbi->s_next_gen_lock);
  948. /* Precompute checksum seed for inode metadata */
  949. if (ext4_has_metadata_csum(sb)) {
  950. __u32 csum;
  951. __le32 inum = cpu_to_le32(inode->i_ino);
  952. __le32 gen = cpu_to_le32(inode->i_generation);
  953. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  954. sizeof(inum));
  955. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  956. sizeof(gen));
  957. }
  958. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  959. ext4_set_inode_state(inode, EXT4_STATE_NEW);
  960. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  961. ei->i_inline_off = 0;
  962. if (ext4_has_feature_inline_data(sb))
  963. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  964. ret = inode;
  965. err = dquot_alloc_inode(inode);
  966. if (err)
  967. goto fail_drop;
  968. err = ext4_init_acl(handle, inode, dir);
  969. if (err)
  970. goto fail_free_drop;
  971. err = ext4_init_security(handle, inode, dir, qstr);
  972. if (err)
  973. goto fail_free_drop;
  974. if (ext4_has_feature_extents(sb)) {
  975. /* set extent flag only for directory, file and normal symlink*/
  976. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  977. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  978. ext4_ext_tree_init(handle, inode);
  979. }
  980. }
  981. if (ext4_handle_valid(handle)) {
  982. ei->i_sync_tid = handle->h_transaction->t_tid;
  983. ei->i_datasync_tid = handle->h_transaction->t_tid;
  984. }
  985. if (encrypt) {
  986. err = ext4_inherit_context(dir, inode);
  987. if (err)
  988. goto fail_free_drop;
  989. }
  990. err = ext4_mark_inode_dirty(handle, inode);
  991. if (err) {
  992. ext4_std_error(sb, err);
  993. goto fail_free_drop;
  994. }
  995. ext4_debug("allocating inode %lu\n", inode->i_ino);
  996. trace_ext4_allocate_inode(inode, dir, mode);
  997. brelse(inode_bitmap_bh);
  998. return ret;
  999. fail_free_drop:
  1000. dquot_free_inode(inode);
  1001. fail_drop:
  1002. clear_nlink(inode);
  1003. unlock_new_inode(inode);
  1004. out:
  1005. dquot_drop(inode);
  1006. inode->i_flags |= S_NOQUOTA;
  1007. iput(inode);
  1008. brelse(inode_bitmap_bh);
  1009. return ERR_PTR(err);
  1010. }
  1011. /* Verify that we are loading a valid orphan from disk */
  1012. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  1013. {
  1014. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  1015. ext4_group_t block_group;
  1016. int bit;
  1017. struct buffer_head *bitmap_bh = NULL;
  1018. struct inode *inode = NULL;
  1019. int err = -EFSCORRUPTED;
  1020. if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
  1021. goto bad_orphan;
  1022. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  1023. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  1024. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  1025. if (IS_ERR(bitmap_bh)) {
  1026. ext4_error(sb, "inode bitmap error %ld for orphan %lu",
  1027. ino, PTR_ERR(bitmap_bh));
  1028. return (struct inode *) bitmap_bh;
  1029. }
  1030. /* Having the inode bit set should be a 100% indicator that this
  1031. * is a valid orphan (no e2fsck run on fs). Orphans also include
  1032. * inodes that were being truncated, so we can't check i_nlink==0.
  1033. */
  1034. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  1035. goto bad_orphan;
  1036. inode = ext4_iget(sb, ino);
  1037. if (IS_ERR(inode)) {
  1038. err = PTR_ERR(inode);
  1039. ext4_error(sb, "couldn't read orphan inode %lu (err %d)",
  1040. ino, err);
  1041. return inode;
  1042. }
  1043. /*
  1044. * If the orphans has i_nlinks > 0 then it should be able to
  1045. * be truncated, otherwise it won't be removed from the orphan
  1046. * list during processing and an infinite loop will result.
  1047. * Similarly, it must not be a bad inode.
  1048. */
  1049. if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
  1050. is_bad_inode(inode))
  1051. goto bad_orphan;
  1052. if (NEXT_ORPHAN(inode) > max_ino)
  1053. goto bad_orphan;
  1054. brelse(bitmap_bh);
  1055. return inode;
  1056. bad_orphan:
  1057. ext4_error(sb, "bad orphan inode %lu", ino);
  1058. if (bitmap_bh)
  1059. printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  1060. bit, (unsigned long long)bitmap_bh->b_blocknr,
  1061. ext4_test_bit(bit, bitmap_bh->b_data));
  1062. if (inode) {
  1063. printk(KERN_ERR "is_bad_inode(inode)=%d\n",
  1064. is_bad_inode(inode));
  1065. printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
  1066. NEXT_ORPHAN(inode));
  1067. printk(KERN_ERR "max_ino=%lu\n", max_ino);
  1068. printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
  1069. /* Avoid freeing blocks if we got a bad deleted inode */
  1070. if (inode->i_nlink == 0)
  1071. inode->i_blocks = 0;
  1072. iput(inode);
  1073. }
  1074. brelse(bitmap_bh);
  1075. return ERR_PTR(err);
  1076. }
  1077. unsigned long ext4_count_free_inodes(struct super_block *sb)
  1078. {
  1079. unsigned long desc_count;
  1080. struct ext4_group_desc *gdp;
  1081. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1082. #ifdef EXT4FS_DEBUG
  1083. struct ext4_super_block *es;
  1084. unsigned long bitmap_count, x;
  1085. struct buffer_head *bitmap_bh = NULL;
  1086. es = EXT4_SB(sb)->s_es;
  1087. desc_count = 0;
  1088. bitmap_count = 0;
  1089. gdp = NULL;
  1090. for (i = 0; i < ngroups; i++) {
  1091. gdp = ext4_get_group_desc(sb, i, NULL);
  1092. if (!gdp)
  1093. continue;
  1094. desc_count += ext4_free_inodes_count(sb, gdp);
  1095. brelse(bitmap_bh);
  1096. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  1097. if (IS_ERR(bitmap_bh)) {
  1098. bitmap_bh = NULL;
  1099. continue;
  1100. }
  1101. x = ext4_count_free(bitmap_bh->b_data,
  1102. EXT4_INODES_PER_GROUP(sb) / 8);
  1103. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  1104. (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
  1105. bitmap_count += x;
  1106. }
  1107. brelse(bitmap_bh);
  1108. printk(KERN_DEBUG "ext4_count_free_inodes: "
  1109. "stored = %u, computed = %lu, %lu\n",
  1110. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  1111. return desc_count;
  1112. #else
  1113. desc_count = 0;
  1114. for (i = 0; i < ngroups; i++) {
  1115. gdp = ext4_get_group_desc(sb, i, NULL);
  1116. if (!gdp)
  1117. continue;
  1118. desc_count += ext4_free_inodes_count(sb, gdp);
  1119. cond_resched();
  1120. }
  1121. return desc_count;
  1122. #endif
  1123. }
  1124. /* Called at mount-time, super-block is locked */
  1125. unsigned long ext4_count_dirs(struct super_block * sb)
  1126. {
  1127. unsigned long count = 0;
  1128. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1129. for (i = 0; i < ngroups; i++) {
  1130. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1131. if (!gdp)
  1132. continue;
  1133. count += ext4_used_dirs_count(sb, gdp);
  1134. }
  1135. return count;
  1136. }
  1137. /*
  1138. * Zeroes not yet zeroed inode table - just write zeroes through the whole
  1139. * inode table. Must be called without any spinlock held. The only place
  1140. * where it is called from on active part of filesystem is ext4lazyinit
  1141. * thread, so we do not need any special locks, however we have to prevent
  1142. * inode allocation from the current group, so we take alloc_sem lock, to
  1143. * block ext4_new_inode() until we are finished.
  1144. */
  1145. int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
  1146. int barrier)
  1147. {
  1148. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  1149. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1150. struct ext4_group_desc *gdp = NULL;
  1151. struct buffer_head *group_desc_bh;
  1152. handle_t *handle;
  1153. ext4_fsblk_t blk;
  1154. int num, ret = 0, used_blks = 0;
  1155. /* This should not happen, but just to be sure check this */
  1156. if (sb->s_flags & MS_RDONLY) {
  1157. ret = 1;
  1158. goto out;
  1159. }
  1160. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  1161. if (!gdp)
  1162. goto out;
  1163. /*
  1164. * We do not need to lock this, because we are the only one
  1165. * handling this flag.
  1166. */
  1167. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
  1168. goto out;
  1169. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  1170. if (IS_ERR(handle)) {
  1171. ret = PTR_ERR(handle);
  1172. goto out;
  1173. }
  1174. down_write(&grp->alloc_sem);
  1175. /*
  1176. * If inode bitmap was already initialized there may be some
  1177. * used inodes so we need to skip blocks with used inodes in
  1178. * inode table.
  1179. */
  1180. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
  1181. used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
  1182. ext4_itable_unused_count(sb, gdp)),
  1183. sbi->s_inodes_per_block);
  1184. if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group) ||
  1185. ((group == 0) && ((EXT4_INODES_PER_GROUP(sb) -
  1186. ext4_itable_unused_count(sb, gdp)) <
  1187. EXT4_FIRST_INO(sb)))) {
  1188. ext4_error(sb, "Something is wrong with group %u: "
  1189. "used itable blocks: %d; "
  1190. "itable unused count: %u",
  1191. group, used_blks,
  1192. ext4_itable_unused_count(sb, gdp));
  1193. ret = 1;
  1194. goto err_out;
  1195. }
  1196. blk = ext4_inode_table(sb, gdp) + used_blks;
  1197. num = sbi->s_itb_per_group - used_blks;
  1198. BUFFER_TRACE(group_desc_bh, "get_write_access");
  1199. ret = ext4_journal_get_write_access(handle,
  1200. group_desc_bh);
  1201. if (ret)
  1202. goto err_out;
  1203. /*
  1204. * Skip zeroout if the inode table is full. But we set the ZEROED
  1205. * flag anyway, because obviously, when it is full it does not need
  1206. * further zeroing.
  1207. */
  1208. if (unlikely(num == 0))
  1209. goto skip_zeroout;
  1210. ext4_debug("going to zero out inode table in group %d\n",
  1211. group);
  1212. ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
  1213. if (ret < 0)
  1214. goto err_out;
  1215. if (barrier)
  1216. blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
  1217. skip_zeroout:
  1218. ext4_lock_group(sb, group);
  1219. gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
  1220. ext4_group_desc_csum_set(sb, group, gdp);
  1221. ext4_unlock_group(sb, group);
  1222. BUFFER_TRACE(group_desc_bh,
  1223. "call ext4_handle_dirty_metadata");
  1224. ret = ext4_handle_dirty_metadata(handle, NULL,
  1225. group_desc_bh);
  1226. err_out:
  1227. up_write(&grp->alloc_sem);
  1228. ext4_journal_stop(handle);
  1229. out:
  1230. return ret;
  1231. }