ialloc.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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. if (desc) {
  464. grp_free = ext4_free_inodes_count(sb, desc);
  465. if (grp_free && grp_free >= avefreei) {
  466. *group = grp;
  467. return 0;
  468. }
  469. }
  470. }
  471. if (avefreei) {
  472. /*
  473. * The free-inodes counter is approximate, and for really small
  474. * filesystems the above test can fail to find any blockgroups
  475. */
  476. avefreei = 0;
  477. goto fallback_retry;
  478. }
  479. return -1;
  480. }
  481. static int find_group_other(struct super_block *sb, struct inode *parent,
  482. ext4_group_t *group, umode_t mode)
  483. {
  484. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  485. ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
  486. struct ext4_group_desc *desc;
  487. int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
  488. /*
  489. * Try to place the inode is the same flex group as its
  490. * parent. If we can't find space, use the Orlov algorithm to
  491. * find another flex group, and store that information in the
  492. * parent directory's inode information so that use that flex
  493. * group for future allocations.
  494. */
  495. if (flex_size > 1) {
  496. int retry = 0;
  497. try_again:
  498. parent_group &= ~(flex_size-1);
  499. last = parent_group + flex_size;
  500. if (last > ngroups)
  501. last = ngroups;
  502. for (i = parent_group; i < last; i++) {
  503. desc = ext4_get_group_desc(sb, i, NULL);
  504. if (desc && ext4_free_inodes_count(sb, desc)) {
  505. *group = i;
  506. return 0;
  507. }
  508. }
  509. if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
  510. retry = 1;
  511. parent_group = EXT4_I(parent)->i_last_alloc_group;
  512. goto try_again;
  513. }
  514. /*
  515. * If this didn't work, use the Orlov search algorithm
  516. * to find a new flex group; we pass in the mode to
  517. * avoid the topdir algorithms.
  518. */
  519. *group = parent_group + flex_size;
  520. if (*group > ngroups)
  521. *group = 0;
  522. return find_group_orlov(sb, parent, group, mode, NULL);
  523. }
  524. /*
  525. * Try to place the inode in its parent directory
  526. */
  527. *group = parent_group;
  528. desc = ext4_get_group_desc(sb, *group, NULL);
  529. if (desc && ext4_free_inodes_count(sb, desc) &&
  530. ext4_free_group_clusters(sb, desc))
  531. return 0;
  532. /*
  533. * We're going to place this inode in a different blockgroup from its
  534. * parent. We want to cause files in a common directory to all land in
  535. * the same blockgroup. But we want files which are in a different
  536. * directory which shares a blockgroup with our parent to land in a
  537. * different blockgroup.
  538. *
  539. * So add our directory's i_ino into the starting point for the hash.
  540. */
  541. *group = (*group + parent->i_ino) % ngroups;
  542. /*
  543. * Use a quadratic hash to find a group with a free inode and some free
  544. * blocks.
  545. */
  546. for (i = 1; i < ngroups; i <<= 1) {
  547. *group += i;
  548. if (*group >= ngroups)
  549. *group -= ngroups;
  550. desc = ext4_get_group_desc(sb, *group, NULL);
  551. if (desc && ext4_free_inodes_count(sb, desc) &&
  552. ext4_free_group_clusters(sb, desc))
  553. return 0;
  554. }
  555. /*
  556. * That failed: try linear search for a free inode, even if that group
  557. * has no free blocks.
  558. */
  559. *group = parent_group;
  560. for (i = 0; i < ngroups; i++) {
  561. if (++*group >= ngroups)
  562. *group = 0;
  563. desc = ext4_get_group_desc(sb, *group, NULL);
  564. if (desc && ext4_free_inodes_count(sb, desc))
  565. return 0;
  566. }
  567. return -1;
  568. }
  569. /*
  570. * There are two policies for allocating an inode. If the new inode is
  571. * a directory, then a forward search is made for a block group with both
  572. * free space and a low directory-to-inode ratio; if that fails, then of
  573. * the groups with above-average free space, that group with the fewest
  574. * directories already is chosen.
  575. *
  576. * For other inodes, search forward from the parent directory's block
  577. * group to find a free inode.
  578. */
  579. struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, umode_t mode,
  580. const struct qstr *qstr, __u32 goal, uid_t *owner)
  581. {
  582. struct super_block *sb;
  583. struct buffer_head *inode_bitmap_bh = NULL;
  584. struct buffer_head *group_desc_bh;
  585. ext4_group_t ngroups, group = 0;
  586. unsigned long ino = 0;
  587. struct inode *inode;
  588. struct ext4_group_desc *gdp = NULL;
  589. struct ext4_inode_info *ei;
  590. struct ext4_sb_info *sbi;
  591. int ret2, err = 0;
  592. struct inode *ret;
  593. ext4_group_t i;
  594. ext4_group_t flex_group;
  595. /* Cannot create files in a deleted directory */
  596. if (!dir || !dir->i_nlink)
  597. return ERR_PTR(-EPERM);
  598. sb = dir->i_sb;
  599. ngroups = ext4_get_groups_count(sb);
  600. trace_ext4_request_inode(dir, mode);
  601. inode = new_inode(sb);
  602. if (!inode)
  603. return ERR_PTR(-ENOMEM);
  604. ei = EXT4_I(inode);
  605. sbi = EXT4_SB(sb);
  606. if (!goal)
  607. goal = sbi->s_inode_goal;
  608. if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
  609. group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
  610. ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
  611. ret2 = 0;
  612. goto got_group;
  613. }
  614. if (S_ISDIR(mode))
  615. ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
  616. else
  617. ret2 = find_group_other(sb, dir, &group, mode);
  618. got_group:
  619. EXT4_I(dir)->i_last_alloc_group = group;
  620. err = -ENOSPC;
  621. if (ret2 == -1)
  622. goto out;
  623. /*
  624. * Normally we will only go through one pass of this loop,
  625. * unless we get unlucky and it turns out the group we selected
  626. * had its last inode grabbed by someone else.
  627. */
  628. for (i = 0; i < ngroups; i++, ino = 0) {
  629. err = -EIO;
  630. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  631. if (!gdp)
  632. goto fail;
  633. brelse(inode_bitmap_bh);
  634. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  635. if (!inode_bitmap_bh)
  636. goto fail;
  637. repeat_in_this_group:
  638. ino = ext4_find_next_zero_bit((unsigned long *)
  639. inode_bitmap_bh->b_data,
  640. EXT4_INODES_PER_GROUP(sb), ino);
  641. if (ino >= EXT4_INODES_PER_GROUP(sb)) {
  642. if (++group == ngroups)
  643. group = 0;
  644. continue;
  645. }
  646. if (group == 0 && (ino+1) < EXT4_FIRST_INO(sb)) {
  647. ext4_error(sb, "reserved inode found cleared - "
  648. "inode=%lu", ino + 1);
  649. continue;
  650. }
  651. ext4_lock_group(sb, group);
  652. ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
  653. ext4_unlock_group(sb, group);
  654. ino++; /* the inode bitmap is zero-based */
  655. if (!ret2)
  656. goto got; /* we grabbed the inode! */
  657. if (ino < EXT4_INODES_PER_GROUP(sb))
  658. goto repeat_in_this_group;
  659. }
  660. err = -ENOSPC;
  661. goto out;
  662. got:
  663. /* We may have to initialize the block bitmap if it isn't already */
  664. if (ext4_has_group_desc_csum(sb) &&
  665. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  666. struct buffer_head *block_bitmap_bh;
  667. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  668. BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
  669. err = ext4_journal_get_write_access(handle, block_bitmap_bh);
  670. if (err) {
  671. brelse(block_bitmap_bh);
  672. goto fail;
  673. }
  674. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  675. err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
  676. brelse(block_bitmap_bh);
  677. /* recheck and clear flag under lock if we still need to */
  678. ext4_lock_group(sb, group);
  679. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  680. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  681. ext4_free_group_clusters_set(sb, gdp,
  682. ext4_free_clusters_after_init(sb, group, gdp));
  683. ext4_block_bitmap_csum_set(sb, group, gdp,
  684. block_bitmap_bh,
  685. EXT4_BLOCKS_PER_GROUP(sb) /
  686. 8);
  687. ext4_group_desc_csum_set(sb, group, gdp);
  688. }
  689. ext4_unlock_group(sb, group);
  690. if (err)
  691. goto fail;
  692. }
  693. BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
  694. err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
  695. if (err)
  696. goto fail;
  697. BUFFER_TRACE(group_desc_bh, "get_write_access");
  698. err = ext4_journal_get_write_access(handle, group_desc_bh);
  699. if (err)
  700. goto fail;
  701. /* Update the relevant bg descriptor fields */
  702. if (ext4_has_group_desc_csum(sb)) {
  703. int free;
  704. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  705. down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
  706. ext4_lock_group(sb, group); /* while we modify the bg desc */
  707. free = EXT4_INODES_PER_GROUP(sb) -
  708. ext4_itable_unused_count(sb, gdp);
  709. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  710. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  711. free = 0;
  712. }
  713. /*
  714. * Check the relative inode number against the last used
  715. * relative inode number in this group. if it is greater
  716. * we need to update the bg_itable_unused count
  717. */
  718. if (ino > free)
  719. ext4_itable_unused_set(sb, gdp,
  720. (EXT4_INODES_PER_GROUP(sb) - ino));
  721. up_read(&grp->alloc_sem);
  722. } else {
  723. ext4_lock_group(sb, group);
  724. }
  725. ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
  726. if (S_ISDIR(mode)) {
  727. ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
  728. if (sbi->s_log_groups_per_flex) {
  729. ext4_group_t f = ext4_flex_group(sbi, group);
  730. atomic_inc(&sbi->s_flex_groups[f].used_dirs);
  731. }
  732. }
  733. if (ext4_has_group_desc_csum(sb)) {
  734. ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
  735. EXT4_INODES_PER_GROUP(sb) / 8);
  736. ext4_group_desc_csum_set(sb, group, gdp);
  737. }
  738. ext4_unlock_group(sb, group);
  739. BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
  740. err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
  741. if (err)
  742. goto fail;
  743. BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
  744. err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
  745. if (err)
  746. goto fail;
  747. percpu_counter_dec(&sbi->s_freeinodes_counter);
  748. if (S_ISDIR(mode))
  749. percpu_counter_inc(&sbi->s_dirs_counter);
  750. ext4_mark_super_dirty(sb);
  751. if (sbi->s_log_groups_per_flex) {
  752. flex_group = ext4_flex_group(sbi, group);
  753. atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
  754. }
  755. if (owner) {
  756. inode->i_mode = mode;
  757. i_uid_write(inode, owner[0]);
  758. i_gid_write(inode, owner[1]);
  759. } else if (test_opt(sb, GRPID)) {
  760. inode->i_mode = mode;
  761. inode->i_uid = current_fsuid();
  762. inode->i_gid = dir->i_gid;
  763. } else
  764. inode_init_owner(inode, dir, mode);
  765. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  766. /* This is the optimal IO size (for stat), not the fs block size */
  767. inode->i_blocks = 0;
  768. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  769. ext4_current_time(inode);
  770. memset(ei->i_data, 0, sizeof(ei->i_data));
  771. ei->i_dir_start_lookup = 0;
  772. ei->i_disksize = 0;
  773. /* Don't inherit extent flag from directory, amongst others. */
  774. ei->i_flags =
  775. ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
  776. ei->i_file_acl = 0;
  777. ei->i_dtime = 0;
  778. ei->i_block_group = group;
  779. ei->i_last_alloc_group = ~0;
  780. ext4_set_inode_flags(inode);
  781. if (IS_DIRSYNC(inode))
  782. ext4_handle_sync(handle);
  783. if (insert_inode_locked(inode) < 0) {
  784. /*
  785. * Likely a bitmap corruption causing inode to be allocated
  786. * twice.
  787. */
  788. err = -EIO;
  789. goto fail;
  790. }
  791. spin_lock(&sbi->s_next_gen_lock);
  792. inode->i_generation = sbi->s_next_generation++;
  793. spin_unlock(&sbi->s_next_gen_lock);
  794. /* Precompute checksum seed for inode metadata */
  795. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  796. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
  797. __u32 csum;
  798. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  799. __le32 inum = cpu_to_le32(inode->i_ino);
  800. __le32 gen = cpu_to_le32(inode->i_generation);
  801. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  802. sizeof(inum));
  803. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  804. sizeof(gen));
  805. }
  806. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  807. ext4_set_inode_state(inode, EXT4_STATE_NEW);
  808. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  809. ret = inode;
  810. dquot_initialize(inode);
  811. err = dquot_alloc_inode(inode);
  812. if (err)
  813. goto fail_drop;
  814. err = ext4_init_acl(handle, inode, dir);
  815. if (err)
  816. goto fail_free_drop;
  817. err = ext4_init_security(handle, inode, dir, qstr);
  818. if (err)
  819. goto fail_free_drop;
  820. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  821. /* set extent flag only for directory, file and normal symlink*/
  822. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  823. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  824. ext4_ext_tree_init(handle, inode);
  825. }
  826. }
  827. if (ext4_handle_valid(handle)) {
  828. ei->i_sync_tid = handle->h_transaction->t_tid;
  829. ei->i_datasync_tid = handle->h_transaction->t_tid;
  830. }
  831. err = ext4_mark_inode_dirty(handle, inode);
  832. if (err) {
  833. ext4_std_error(sb, err);
  834. goto fail_free_drop;
  835. }
  836. ext4_debug("allocating inode %lu\n", inode->i_ino);
  837. trace_ext4_allocate_inode(inode, dir, mode);
  838. goto really_out;
  839. fail:
  840. ext4_std_error(sb, err);
  841. out:
  842. iput(inode);
  843. ret = ERR_PTR(err);
  844. really_out:
  845. brelse(inode_bitmap_bh);
  846. return ret;
  847. fail_free_drop:
  848. dquot_free_inode(inode);
  849. fail_drop:
  850. dquot_drop(inode);
  851. inode->i_flags |= S_NOQUOTA;
  852. clear_nlink(inode);
  853. unlock_new_inode(inode);
  854. iput(inode);
  855. brelse(inode_bitmap_bh);
  856. return ERR_PTR(err);
  857. }
  858. /* Verify that we are loading a valid orphan from disk */
  859. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  860. {
  861. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  862. ext4_group_t block_group;
  863. int bit;
  864. struct buffer_head *bitmap_bh;
  865. struct inode *inode = NULL;
  866. long err = -EIO;
  867. /* Error cases - e2fsck has already cleaned up for us */
  868. if (ino > max_ino) {
  869. ext4_warning(sb, "bad orphan ino %lu! e2fsck was run?", ino);
  870. goto error;
  871. }
  872. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  873. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  874. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  875. if (!bitmap_bh) {
  876. ext4_warning(sb, "inode bitmap error for orphan %lu", ino);
  877. goto error;
  878. }
  879. /* Having the inode bit set should be a 100% indicator that this
  880. * is a valid orphan (no e2fsck run on fs). Orphans also include
  881. * inodes that were being truncated, so we can't check i_nlink==0.
  882. */
  883. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  884. goto bad_orphan;
  885. inode = ext4_iget(sb, ino);
  886. if (IS_ERR(inode))
  887. goto iget_failed;
  888. /*
  889. * If the orphans has i_nlinks > 0 then it should be able to be
  890. * truncated, otherwise it won't be removed from the orphan list
  891. * during processing and an infinite loop will result.
  892. */
  893. if (inode->i_nlink && !ext4_can_truncate(inode))
  894. goto bad_orphan;
  895. if (NEXT_ORPHAN(inode) > max_ino)
  896. goto bad_orphan;
  897. brelse(bitmap_bh);
  898. return inode;
  899. iget_failed:
  900. err = PTR_ERR(inode);
  901. inode = NULL;
  902. bad_orphan:
  903. ext4_warning(sb, "bad orphan inode %lu! e2fsck was run?", ino);
  904. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  905. bit, (unsigned long long)bitmap_bh->b_blocknr,
  906. ext4_test_bit(bit, bitmap_bh->b_data));
  907. printk(KERN_NOTICE "inode=%p\n", inode);
  908. if (inode) {
  909. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  910. is_bad_inode(inode));
  911. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  912. NEXT_ORPHAN(inode));
  913. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  914. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  915. /* Avoid freeing blocks if we got a bad deleted inode */
  916. if (inode->i_nlink == 0)
  917. inode->i_blocks = 0;
  918. iput(inode);
  919. }
  920. brelse(bitmap_bh);
  921. error:
  922. return ERR_PTR(err);
  923. }
  924. unsigned long ext4_count_free_inodes(struct super_block *sb)
  925. {
  926. unsigned long desc_count;
  927. struct ext4_group_desc *gdp;
  928. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  929. #ifdef EXT4FS_DEBUG
  930. struct ext4_super_block *es;
  931. unsigned long bitmap_count, x;
  932. struct buffer_head *bitmap_bh = NULL;
  933. es = EXT4_SB(sb)->s_es;
  934. desc_count = 0;
  935. bitmap_count = 0;
  936. gdp = NULL;
  937. for (i = 0; i < ngroups; i++) {
  938. gdp = ext4_get_group_desc(sb, i, NULL);
  939. if (!gdp)
  940. continue;
  941. desc_count += ext4_free_inodes_count(sb, gdp);
  942. brelse(bitmap_bh);
  943. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  944. if (!bitmap_bh)
  945. continue;
  946. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  947. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  948. (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
  949. bitmap_count += x;
  950. }
  951. brelse(bitmap_bh);
  952. printk(KERN_DEBUG "ext4_count_free_inodes: "
  953. "stored = %u, computed = %lu, %lu\n",
  954. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  955. return desc_count;
  956. #else
  957. desc_count = 0;
  958. for (i = 0; i < ngroups; i++) {
  959. gdp = ext4_get_group_desc(sb, i, NULL);
  960. if (!gdp)
  961. continue;
  962. desc_count += ext4_free_inodes_count(sb, gdp);
  963. cond_resched();
  964. }
  965. return desc_count;
  966. #endif
  967. }
  968. /* Called at mount-time, super-block is locked */
  969. unsigned long ext4_count_dirs(struct super_block * sb)
  970. {
  971. unsigned long count = 0;
  972. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  973. for (i = 0; i < ngroups; i++) {
  974. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  975. if (!gdp)
  976. continue;
  977. count += ext4_used_dirs_count(sb, gdp);
  978. }
  979. return count;
  980. }
  981. /*
  982. * Zeroes not yet zeroed inode table - just write zeroes through the whole
  983. * inode table. Must be called without any spinlock held. The only place
  984. * where it is called from on active part of filesystem is ext4lazyinit
  985. * thread, so we do not need any special locks, however we have to prevent
  986. * inode allocation from the current group, so we take alloc_sem lock, to
  987. * block ext4_new_inode() until we are finished.
  988. */
  989. int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
  990. int barrier)
  991. {
  992. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  993. struct ext4_sb_info *sbi = EXT4_SB(sb);
  994. struct ext4_group_desc *gdp = NULL;
  995. struct buffer_head *group_desc_bh;
  996. handle_t *handle;
  997. ext4_fsblk_t blk;
  998. int num, ret = 0, used_blks = 0;
  999. /* This should not happen, but just to be sure check this */
  1000. if (sb->s_flags & MS_RDONLY) {
  1001. ret = 1;
  1002. goto out;
  1003. }
  1004. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  1005. if (!gdp)
  1006. goto out;
  1007. /*
  1008. * We do not need to lock this, because we are the only one
  1009. * handling this flag.
  1010. */
  1011. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
  1012. goto out;
  1013. handle = ext4_journal_start_sb(sb, 1);
  1014. if (IS_ERR(handle)) {
  1015. ret = PTR_ERR(handle);
  1016. goto out;
  1017. }
  1018. down_write(&grp->alloc_sem);
  1019. /*
  1020. * If inode bitmap was already initialized there may be some
  1021. * used inodes so we need to skip blocks with used inodes in
  1022. * inode table.
  1023. */
  1024. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
  1025. used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
  1026. ext4_itable_unused_count(sb, gdp)),
  1027. sbi->s_inodes_per_block);
  1028. if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
  1029. ext4_error(sb, "Something is wrong with group %u: "
  1030. "used itable blocks: %d; "
  1031. "itable unused count: %u",
  1032. group, used_blks,
  1033. ext4_itable_unused_count(sb, gdp));
  1034. ret = 1;
  1035. goto err_out;
  1036. }
  1037. blk = ext4_inode_table(sb, gdp) + used_blks;
  1038. num = sbi->s_itb_per_group - used_blks;
  1039. BUFFER_TRACE(group_desc_bh, "get_write_access");
  1040. ret = ext4_journal_get_write_access(handle,
  1041. group_desc_bh);
  1042. if (ret)
  1043. goto err_out;
  1044. /*
  1045. * Skip zeroout if the inode table is full. But we set the ZEROED
  1046. * flag anyway, because obviously, when it is full it does not need
  1047. * further zeroing.
  1048. */
  1049. if (unlikely(num == 0))
  1050. goto skip_zeroout;
  1051. ext4_debug("going to zero out inode table in group %d\n",
  1052. group);
  1053. ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
  1054. if (ret < 0)
  1055. goto err_out;
  1056. if (barrier)
  1057. blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
  1058. skip_zeroout:
  1059. ext4_lock_group(sb, group);
  1060. gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
  1061. ext4_group_desc_csum_set(sb, group, gdp);
  1062. ext4_unlock_group(sb, group);
  1063. BUFFER_TRACE(group_desc_bh,
  1064. "call ext4_handle_dirty_metadata");
  1065. ret = ext4_handle_dirty_metadata(handle, NULL,
  1066. group_desc_bh);
  1067. err_out:
  1068. up_write(&grp->alloc_sem);
  1069. ext4_journal_stop(handle);
  1070. out:
  1071. return ret;
  1072. }