ialloc.c 34 KB

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