ialloc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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 | REQ_META | REQ_PRIO, 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. __u64 free_clusters;
  294. __u32 free_inodes;
  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 = atomic64_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,
  579. umode_t mode, const struct qstr *qstr,
  580. __u32 goal, uid_t *owner, int handle_type,
  581. unsigned int line_no, int nblocks)
  582. {
  583. struct super_block *sb;
  584. struct buffer_head *inode_bitmap_bh = NULL;
  585. struct buffer_head *group_desc_bh;
  586. ext4_group_t ngroups, group = 0;
  587. unsigned long ino = 0;
  588. struct inode *inode;
  589. struct ext4_group_desc *gdp = NULL;
  590. struct ext4_inode_info *ei;
  591. struct ext4_sb_info *sbi;
  592. int ret2, err = 0;
  593. struct inode *ret;
  594. ext4_group_t i;
  595. ext4_group_t flex_group;
  596. /* Cannot create files in a deleted directory */
  597. if (!dir || !dir->i_nlink)
  598. return ERR_PTR(-EPERM);
  599. sb = dir->i_sb;
  600. ngroups = ext4_get_groups_count(sb);
  601. trace_ext4_request_inode(dir, mode);
  602. inode = new_inode(sb);
  603. if (!inode)
  604. return ERR_PTR(-ENOMEM);
  605. ei = EXT4_I(inode);
  606. sbi = EXT4_SB(sb);
  607. /*
  608. * Initalize owners and quota early so that we don't have to account
  609. * for quota initialization worst case in standard inode creating
  610. * transaction
  611. */
  612. if (owner) {
  613. inode->i_mode = mode;
  614. i_uid_write(inode, owner[0]);
  615. i_gid_write(inode, owner[1]);
  616. } else if (test_opt(sb, GRPID)) {
  617. inode->i_mode = mode;
  618. inode->i_uid = current_fsuid();
  619. inode->i_gid = dir->i_gid;
  620. } else
  621. inode_init_owner(inode, dir, mode);
  622. dquot_initialize(inode);
  623. if (!goal)
  624. goal = sbi->s_inode_goal;
  625. if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
  626. group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
  627. ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
  628. ret2 = 0;
  629. goto got_group;
  630. }
  631. if (S_ISDIR(mode))
  632. ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
  633. else
  634. ret2 = find_group_other(sb, dir, &group, mode);
  635. got_group:
  636. EXT4_I(dir)->i_last_alloc_group = group;
  637. err = -ENOSPC;
  638. if (ret2 == -1)
  639. goto out;
  640. /*
  641. * Normally we will only go through one pass of this loop,
  642. * unless we get unlucky and it turns out the group we selected
  643. * had its last inode grabbed by someone else.
  644. */
  645. for (i = 0; i < ngroups; i++, ino = 0) {
  646. err = -EIO;
  647. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  648. if (!gdp)
  649. goto out;
  650. /*
  651. * Check free inodes count before loading bitmap.
  652. */
  653. if (ext4_free_inodes_count(sb, gdp) == 0) {
  654. if (++group == ngroups)
  655. group = 0;
  656. continue;
  657. }
  658. brelse(inode_bitmap_bh);
  659. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  660. if (!inode_bitmap_bh)
  661. goto out;
  662. repeat_in_this_group:
  663. ino = ext4_find_next_zero_bit((unsigned long *)
  664. inode_bitmap_bh->b_data,
  665. EXT4_INODES_PER_GROUP(sb), ino);
  666. if (ino >= EXT4_INODES_PER_GROUP(sb)) {
  667. if (++group == ngroups)
  668. group = 0;
  669. continue;
  670. }
  671. if (group == 0 && (ino+1) < EXT4_FIRST_INO(sb)) {
  672. ext4_error(sb, "reserved inode found cleared - "
  673. "inode=%lu", ino + 1);
  674. continue;
  675. }
  676. if (!handle) {
  677. BUG_ON(nblocks <= 0);
  678. handle = __ext4_journal_start_sb(dir->i_sb, line_no,
  679. handle_type, nblocks);
  680. if (IS_ERR(handle)) {
  681. err = PTR_ERR(handle);
  682. ext4_std_error(sb, err);
  683. goto out;
  684. }
  685. }
  686. BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
  687. err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
  688. if (err) {
  689. ext4_std_error(sb, err);
  690. goto out;
  691. }
  692. ext4_lock_group(sb, group);
  693. ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
  694. ext4_unlock_group(sb, group);
  695. ino++; /* the inode bitmap is zero-based */
  696. if (!ret2)
  697. goto got; /* we grabbed the inode! */
  698. if (ino < EXT4_INODES_PER_GROUP(sb))
  699. goto repeat_in_this_group;
  700. }
  701. err = -ENOSPC;
  702. goto out;
  703. got:
  704. BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
  705. err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
  706. if (err) {
  707. ext4_std_error(sb, err);
  708. goto out;
  709. }
  710. /* We may have to initialize the block bitmap if it isn't already */
  711. if (ext4_has_group_desc_csum(sb) &&
  712. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  713. struct buffer_head *block_bitmap_bh;
  714. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  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. ext4_std_error(sb, err);
  720. goto out;
  721. }
  722. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  723. err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
  724. /* recheck and clear flag under lock if we still need to */
  725. ext4_lock_group(sb, group);
  726. if (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. ext4_std_error(sb, err);
  738. goto out;
  739. }
  740. }
  741. BUFFER_TRACE(group_desc_bh, "get_write_access");
  742. err = ext4_journal_get_write_access(handle, group_desc_bh);
  743. if (err) {
  744. ext4_std_error(sb, err);
  745. goto out;
  746. }
  747. /* Update the relevant bg descriptor fields */
  748. if (ext4_has_group_desc_csum(sb)) {
  749. int free;
  750. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  751. down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
  752. ext4_lock_group(sb, group); /* while we modify the bg desc */
  753. free = EXT4_INODES_PER_GROUP(sb) -
  754. ext4_itable_unused_count(sb, gdp);
  755. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  756. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  757. free = 0;
  758. }
  759. /*
  760. * Check the relative inode number against the last used
  761. * relative inode number in this group. if it is greater
  762. * we need to update the bg_itable_unused count
  763. */
  764. if (ino > free)
  765. ext4_itable_unused_set(sb, gdp,
  766. (EXT4_INODES_PER_GROUP(sb) - ino));
  767. up_read(&grp->alloc_sem);
  768. } else {
  769. ext4_lock_group(sb, group);
  770. }
  771. ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
  772. if (S_ISDIR(mode)) {
  773. ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
  774. if (sbi->s_log_groups_per_flex) {
  775. ext4_group_t f = ext4_flex_group(sbi, group);
  776. atomic_inc(&sbi->s_flex_groups[f].used_dirs);
  777. }
  778. }
  779. if (ext4_has_group_desc_csum(sb)) {
  780. ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
  781. EXT4_INODES_PER_GROUP(sb) / 8);
  782. ext4_group_desc_csum_set(sb, group, gdp);
  783. }
  784. ext4_unlock_group(sb, group);
  785. BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
  786. err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
  787. if (err) {
  788. ext4_std_error(sb, err);
  789. goto out;
  790. }
  791. percpu_counter_dec(&sbi->s_freeinodes_counter);
  792. if (S_ISDIR(mode))
  793. percpu_counter_inc(&sbi->s_dirs_counter);
  794. if (sbi->s_log_groups_per_flex) {
  795. flex_group = ext4_flex_group(sbi, group);
  796. atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
  797. }
  798. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  799. /* This is the optimal IO size (for stat), not the fs block size */
  800. inode->i_blocks = 0;
  801. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  802. ext4_current_time(inode);
  803. memset(ei->i_data, 0, sizeof(ei->i_data));
  804. ei->i_dir_start_lookup = 0;
  805. ei->i_disksize = 0;
  806. /* Don't inherit extent flag from directory, amongst others. */
  807. ei->i_flags =
  808. ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
  809. ei->i_file_acl = 0;
  810. ei->i_dtime = 0;
  811. ei->i_block_group = group;
  812. ei->i_last_alloc_group = ~0;
  813. ext4_set_inode_flags(inode);
  814. if (IS_DIRSYNC(inode))
  815. ext4_handle_sync(handle);
  816. if (insert_inode_locked(inode) < 0) {
  817. /*
  818. * Likely a bitmap corruption causing inode to be allocated
  819. * twice.
  820. */
  821. err = -EIO;
  822. ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
  823. inode->i_ino);
  824. goto out;
  825. }
  826. spin_lock(&sbi->s_next_gen_lock);
  827. inode->i_generation = sbi->s_next_generation++;
  828. spin_unlock(&sbi->s_next_gen_lock);
  829. /* Precompute checksum seed for inode metadata */
  830. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  831. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
  832. __u32 csum;
  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. ei->i_inline_off = 0;
  844. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_INLINE_DATA))
  845. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  846. ret = inode;
  847. err = dquot_alloc_inode(inode);
  848. if (err)
  849. goto fail_drop;
  850. err = ext4_init_acl(handle, inode, dir);
  851. if (err)
  852. goto fail_free_drop;
  853. err = ext4_init_security(handle, inode, dir, qstr);
  854. if (err)
  855. goto fail_free_drop;
  856. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  857. /* set extent flag only for directory, file and normal symlink*/
  858. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  859. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  860. ext4_ext_tree_init(handle, inode);
  861. }
  862. }
  863. if (ext4_handle_valid(handle)) {
  864. ei->i_sync_tid = handle->h_transaction->t_tid;
  865. ei->i_datasync_tid = handle->h_transaction->t_tid;
  866. }
  867. err = ext4_mark_inode_dirty(handle, inode);
  868. if (err) {
  869. ext4_std_error(sb, err);
  870. goto fail_free_drop;
  871. }
  872. ext4_debug("allocating inode %lu\n", inode->i_ino);
  873. trace_ext4_allocate_inode(inode, dir, mode);
  874. brelse(inode_bitmap_bh);
  875. return ret;
  876. fail_free_drop:
  877. dquot_free_inode(inode);
  878. fail_drop:
  879. clear_nlink(inode);
  880. unlock_new_inode(inode);
  881. out:
  882. dquot_drop(inode);
  883. inode->i_flags |= S_NOQUOTA;
  884. iput(inode);
  885. brelse(inode_bitmap_bh);
  886. return ERR_PTR(err);
  887. }
  888. /* Verify that we are loading a valid orphan from disk */
  889. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  890. {
  891. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  892. ext4_group_t block_group;
  893. int bit;
  894. struct buffer_head *bitmap_bh;
  895. struct inode *inode = NULL;
  896. long err = -EIO;
  897. /* Error cases - e2fsck has already cleaned up for us */
  898. if (ino > max_ino) {
  899. ext4_warning(sb, "bad orphan ino %lu! e2fsck was run?", ino);
  900. goto error;
  901. }
  902. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  903. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  904. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  905. if (!bitmap_bh) {
  906. ext4_warning(sb, "inode bitmap error for orphan %lu", ino);
  907. goto error;
  908. }
  909. /* Having the inode bit set should be a 100% indicator that this
  910. * is a valid orphan (no e2fsck run on fs). Orphans also include
  911. * inodes that were being truncated, so we can't check i_nlink==0.
  912. */
  913. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  914. goto bad_orphan;
  915. inode = ext4_iget(sb, ino);
  916. if (IS_ERR(inode))
  917. goto iget_failed;
  918. /*
  919. * If the orphans has i_nlinks > 0 then it should be able to be
  920. * truncated, otherwise it won't be removed from the orphan list
  921. * during processing and an infinite loop will result.
  922. */
  923. if (inode->i_nlink && !ext4_can_truncate(inode))
  924. goto bad_orphan;
  925. if (NEXT_ORPHAN(inode) > max_ino)
  926. goto bad_orphan;
  927. brelse(bitmap_bh);
  928. return inode;
  929. iget_failed:
  930. err = PTR_ERR(inode);
  931. inode = NULL;
  932. bad_orphan:
  933. ext4_warning(sb, "bad orphan inode %lu! e2fsck was run?", ino);
  934. printk(KERN_WARNING "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  935. bit, (unsigned long long)bitmap_bh->b_blocknr,
  936. ext4_test_bit(bit, bitmap_bh->b_data));
  937. printk(KERN_WARNING "inode=%p\n", inode);
  938. if (inode) {
  939. printk(KERN_WARNING "is_bad_inode(inode)=%d\n",
  940. is_bad_inode(inode));
  941. printk(KERN_WARNING "NEXT_ORPHAN(inode)=%u\n",
  942. NEXT_ORPHAN(inode));
  943. printk(KERN_WARNING "max_ino=%lu\n", max_ino);
  944. printk(KERN_WARNING "i_nlink=%u\n", inode->i_nlink);
  945. /* Avoid freeing blocks if we got a bad deleted inode */
  946. if (inode->i_nlink == 0)
  947. inode->i_blocks = 0;
  948. iput(inode);
  949. }
  950. brelse(bitmap_bh);
  951. error:
  952. return ERR_PTR(err);
  953. }
  954. unsigned long ext4_count_free_inodes(struct super_block *sb)
  955. {
  956. unsigned long desc_count;
  957. struct ext4_group_desc *gdp;
  958. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  959. #ifdef EXT4FS_DEBUG
  960. struct ext4_super_block *es;
  961. unsigned long bitmap_count, x;
  962. struct buffer_head *bitmap_bh = NULL;
  963. es = EXT4_SB(sb)->s_es;
  964. desc_count = 0;
  965. bitmap_count = 0;
  966. gdp = NULL;
  967. for (i = 0; i < ngroups; i++) {
  968. gdp = ext4_get_group_desc(sb, i, NULL);
  969. if (!gdp)
  970. continue;
  971. desc_count += ext4_free_inodes_count(sb, gdp);
  972. brelse(bitmap_bh);
  973. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  974. if (!bitmap_bh)
  975. continue;
  976. x = ext4_count_free(bitmap_bh->b_data,
  977. EXT4_INODES_PER_GROUP(sb) / 8);
  978. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  979. (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
  980. bitmap_count += x;
  981. }
  982. brelse(bitmap_bh);
  983. printk(KERN_DEBUG "ext4_count_free_inodes: "
  984. "stored = %u, computed = %lu, %lu\n",
  985. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  986. return desc_count;
  987. #else
  988. desc_count = 0;
  989. for (i = 0; i < ngroups; i++) {
  990. gdp = ext4_get_group_desc(sb, i, NULL);
  991. if (!gdp)
  992. continue;
  993. desc_count += ext4_free_inodes_count(sb, gdp);
  994. cond_resched();
  995. }
  996. return desc_count;
  997. #endif
  998. }
  999. /* Called at mount-time, super-block is locked */
  1000. unsigned long ext4_count_dirs(struct super_block * sb)
  1001. {
  1002. unsigned long count = 0;
  1003. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1004. for (i = 0; i < ngroups; i++) {
  1005. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1006. if (!gdp)
  1007. continue;
  1008. count += ext4_used_dirs_count(sb, gdp);
  1009. }
  1010. return count;
  1011. }
  1012. /*
  1013. * Zeroes not yet zeroed inode table - just write zeroes through the whole
  1014. * inode table. Must be called without any spinlock held. The only place
  1015. * where it is called from on active part of filesystem is ext4lazyinit
  1016. * thread, so we do not need any special locks, however we have to prevent
  1017. * inode allocation from the current group, so we take alloc_sem lock, to
  1018. * block ext4_new_inode() until we are finished.
  1019. */
  1020. int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
  1021. int barrier)
  1022. {
  1023. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  1024. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1025. struct ext4_group_desc *gdp = NULL;
  1026. struct buffer_head *group_desc_bh;
  1027. handle_t *handle;
  1028. ext4_fsblk_t blk;
  1029. int num, ret = 0, used_blks = 0;
  1030. /* This should not happen, but just to be sure check this */
  1031. if (sb->s_flags & MS_RDONLY) {
  1032. ret = 1;
  1033. goto out;
  1034. }
  1035. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  1036. if (!gdp)
  1037. goto out;
  1038. /*
  1039. * We do not need to lock this, because we are the only one
  1040. * handling this flag.
  1041. */
  1042. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
  1043. goto out;
  1044. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  1045. if (IS_ERR(handle)) {
  1046. ret = PTR_ERR(handle);
  1047. goto out;
  1048. }
  1049. down_write(&grp->alloc_sem);
  1050. /*
  1051. * If inode bitmap was already initialized there may be some
  1052. * used inodes so we need to skip blocks with used inodes in
  1053. * inode table.
  1054. */
  1055. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
  1056. used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
  1057. ext4_itable_unused_count(sb, gdp)),
  1058. sbi->s_inodes_per_block);
  1059. if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
  1060. ext4_error(sb, "Something is wrong with group %u: "
  1061. "used itable blocks: %d; "
  1062. "itable unused count: %u",
  1063. group, used_blks,
  1064. ext4_itable_unused_count(sb, gdp));
  1065. ret = 1;
  1066. goto err_out;
  1067. }
  1068. blk = ext4_inode_table(sb, gdp) + used_blks;
  1069. num = sbi->s_itb_per_group - used_blks;
  1070. BUFFER_TRACE(group_desc_bh, "get_write_access");
  1071. ret = ext4_journal_get_write_access(handle,
  1072. group_desc_bh);
  1073. if (ret)
  1074. goto err_out;
  1075. /*
  1076. * Skip zeroout if the inode table is full. But we set the ZEROED
  1077. * flag anyway, because obviously, when it is full it does not need
  1078. * further zeroing.
  1079. */
  1080. if (unlikely(num == 0))
  1081. goto skip_zeroout;
  1082. ext4_debug("going to zero out inode table in group %d\n",
  1083. group);
  1084. ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
  1085. if (ret < 0)
  1086. goto err_out;
  1087. if (barrier)
  1088. blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
  1089. skip_zeroout:
  1090. ext4_lock_group(sb, group);
  1091. gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
  1092. ext4_group_desc_csum_set(sb, group, gdp);
  1093. ext4_unlock_group(sb, group);
  1094. BUFFER_TRACE(group_desc_bh,
  1095. "call ext4_handle_dirty_metadata");
  1096. ret = ext4_handle_dirty_metadata(handle, NULL,
  1097. group_desc_bh);
  1098. err_out:
  1099. up_write(&grp->alloc_sem);
  1100. ext4_journal_stop(handle);
  1101. out:
  1102. return ret;
  1103. }