ialloc.c 34 KB

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