ialloc.c 36 KB

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