ialloc.c 33 KB

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