ialloc.c 33 KB

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