ialloc.c 34 KB

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