ialloc.c 34 KB

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