ialloc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. /*
  30. * ialloc.c contains the inodes allocation and deallocation routines
  31. */
  32. /*
  33. * The free inodes are managed by bitmaps. A file system contains several
  34. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  35. * block for inodes, N blocks for the inode table and data blocks.
  36. *
  37. * The file system contains group descriptors which are located after the
  38. * super block. Each descriptor contains the number of the bitmap block and
  39. * the free blocks count in the block.
  40. */
  41. /*
  42. * To avoid calling the atomic setbit hundreds or thousands of times, we only
  43. * need to use it within a single byte (to ensure we get endianness right).
  44. * We can use memset for the rest of the bitmap as there are no other users.
  45. */
  46. void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
  47. {
  48. int i;
  49. if (start_bit >= end_bit)
  50. return;
  51. ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
  52. for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
  53. ext4_set_bit(i, bitmap);
  54. if (i < end_bit)
  55. memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
  56. }
  57. /* Initializes an uninitialized inode bitmap */
  58. unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
  59. ext4_group_t block_group,
  60. struct ext4_group_desc *gdp)
  61. {
  62. struct ext4_sb_info *sbi = EXT4_SB(sb);
  63. J_ASSERT_BH(bh, buffer_locked(bh));
  64. /* If checksum is bad mark all blocks and inodes use to prevent
  65. * allocation, essentially implementing a per-group read-only flag. */
  66. if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
  67. ext4_error(sb, __func__, "Checksum bad for group %u",
  68. 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, __func__,
  99. "Cannot read inode bitmap - "
  100. "block_group = %u, inode_bitmap = %llu",
  101. block_group, bitmap_blk);
  102. return NULL;
  103. }
  104. if (bitmap_uptodate(bh))
  105. return bh;
  106. lock_buffer(bh);
  107. if (bitmap_uptodate(bh)) {
  108. unlock_buffer(bh);
  109. return bh;
  110. }
  111. ext4_lock_group(sb, block_group);
  112. if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  113. ext4_init_inode_bitmap(sb, bh, block_group, desc);
  114. set_bitmap_uptodate(bh);
  115. set_buffer_uptodate(bh);
  116. ext4_unlock_group(sb, block_group);
  117. unlock_buffer(bh);
  118. return bh;
  119. }
  120. ext4_unlock_group(sb, block_group);
  121. if (buffer_uptodate(bh)) {
  122. /*
  123. * if not uninit if bh is uptodate,
  124. * bitmap is also uptodate
  125. */
  126. set_bitmap_uptodate(bh);
  127. unlock_buffer(bh);
  128. return bh;
  129. }
  130. /*
  131. * submit the buffer_head for read. We can
  132. * safely mark the bitmap as uptodate now.
  133. * We do it here so the bitmap uptodate bit
  134. * get set with buffer lock held.
  135. */
  136. set_bitmap_uptodate(bh);
  137. if (bh_submit_read(bh) < 0) {
  138. put_bh(bh);
  139. ext4_error(sb, __func__,
  140. "Cannot read inode bitmap - "
  141. "block_group = %u, inode_bitmap = %llu",
  142. block_group, bitmap_blk);
  143. return NULL;
  144. }
  145. return bh;
  146. }
  147. /*
  148. * NOTE! When we get the inode, we're the only people
  149. * that have access to it, and as such there are no
  150. * race conditions we have to worry about. The inode
  151. * is not on the hash-lists, and it cannot be reached
  152. * through the filesystem because the directory entry
  153. * has been deleted earlier.
  154. *
  155. * HOWEVER: we must make sure that we get no aliases,
  156. * which means that we have to call "clear_inode()"
  157. * _before_ we mark the inode not in use in the inode
  158. * bitmaps. Otherwise a newly created file might use
  159. * the same inode number (not actually the same pointer
  160. * though), and then we'd have two inodes sharing the
  161. * same inode number and space on the harddisk.
  162. */
  163. void ext4_free_inode(handle_t *handle, struct inode *inode)
  164. {
  165. struct super_block *sb = inode->i_sb;
  166. int is_directory;
  167. unsigned long ino;
  168. struct buffer_head *bitmap_bh = NULL;
  169. struct buffer_head *bh2;
  170. ext4_group_t block_group;
  171. unsigned long bit;
  172. struct ext4_group_desc *gdp;
  173. struct ext4_super_block *es;
  174. struct ext4_sb_info *sbi;
  175. int fatal = 0, err, count, cleared;
  176. if (atomic_read(&inode->i_count) > 1) {
  177. printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
  178. atomic_read(&inode->i_count));
  179. return;
  180. }
  181. if (inode->i_nlink) {
  182. printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
  183. inode->i_nlink);
  184. return;
  185. }
  186. if (!sb) {
  187. printk(KERN_ERR "ext4_free_inode: inode on "
  188. "nonexistent device\n");
  189. return;
  190. }
  191. sbi = EXT4_SB(sb);
  192. ino = inode->i_ino;
  193. ext4_debug("freeing inode %lu\n", ino);
  194. trace_mark(ext4_free_inode,
  195. "dev %s ino %lu mode %d uid %lu gid %lu bocks %llu",
  196. sb->s_id, inode->i_ino, inode->i_mode,
  197. (unsigned long) inode->i_uid, (unsigned long) inode->i_gid,
  198. (unsigned long long) inode->i_blocks);
  199. /*
  200. * Note: we must free any quota before locking the superblock,
  201. * as writing the quota to disk may need the lock as well.
  202. */
  203. vfs_dq_init(inode);
  204. ext4_xattr_delete_inode(handle, inode);
  205. vfs_dq_free_inode(inode);
  206. vfs_dq_drop(inode);
  207. is_directory = S_ISDIR(inode->i_mode);
  208. /* Do this BEFORE marking the inode not in use or returning an error */
  209. clear_inode(inode);
  210. es = EXT4_SB(sb)->s_es;
  211. if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  212. ext4_error(sb, "ext4_free_inode",
  213. "reserved or nonexistent inode %lu", ino);
  214. goto error_return;
  215. }
  216. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  217. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  218. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  219. if (!bitmap_bh)
  220. goto error_return;
  221. BUFFER_TRACE(bitmap_bh, "get_write_access");
  222. fatal = ext4_journal_get_write_access(handle, bitmap_bh);
  223. if (fatal)
  224. goto error_return;
  225. /* Ok, now we can actually update the inode bitmaps.. */
  226. cleared = ext4_clear_bit_atomic(ext4_group_lock_ptr(sb, block_group),
  227. bit, bitmap_bh->b_data);
  228. if (!cleared)
  229. ext4_error(sb, "ext4_free_inode",
  230. "bit already cleared for inode %lu", ino);
  231. else {
  232. gdp = ext4_get_group_desc(sb, block_group, &bh2);
  233. BUFFER_TRACE(bh2, "get_write_access");
  234. fatal = ext4_journal_get_write_access(handle, bh2);
  235. if (fatal) goto error_return;
  236. if (gdp) {
  237. ext4_lock_group(sb, block_group);
  238. count = ext4_free_inodes_count(sb, gdp) + 1;
  239. ext4_free_inodes_set(sb, gdp, count);
  240. if (is_directory) {
  241. count = ext4_used_dirs_count(sb, gdp) - 1;
  242. ext4_used_dirs_set(sb, gdp, count);
  243. if (sbi->s_log_groups_per_flex) {
  244. ext4_group_t f;
  245. f = ext4_flex_group(sbi, block_group);
  246. atomic_dec(&sbi->s_flex_groups[f].free_inodes);
  247. }
  248. }
  249. gdp->bg_checksum = ext4_group_desc_csum(sbi,
  250. block_group, gdp);
  251. ext4_unlock_group(sb, block_group);
  252. percpu_counter_inc(&sbi->s_freeinodes_counter);
  253. if (is_directory)
  254. percpu_counter_dec(&sbi->s_dirs_counter);
  255. if (sbi->s_log_groups_per_flex) {
  256. ext4_group_t f;
  257. f = ext4_flex_group(sbi, block_group);
  258. atomic_inc(&sbi->s_flex_groups[f].free_inodes);
  259. }
  260. }
  261. BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
  262. err = ext4_handle_dirty_metadata(handle, NULL, bh2);
  263. if (!fatal) fatal = err;
  264. }
  265. BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
  266. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  267. if (!fatal)
  268. fatal = err;
  269. sb->s_dirt = 1;
  270. error_return:
  271. brelse(bitmap_bh);
  272. ext4_std_error(sb, fatal);
  273. }
  274. /*
  275. * There are two policies for allocating an inode. If the new inode is
  276. * a directory, then a forward search is made for a block group with both
  277. * free space and a low directory-to-inode ratio; if that fails, then of
  278. * the groups with above-average free space, that group with the fewest
  279. * directories already is chosen.
  280. *
  281. * For other inodes, search forward from the parent directory\'s block
  282. * group to find a free inode.
  283. */
  284. static int find_group_dir(struct super_block *sb, struct inode *parent,
  285. ext4_group_t *best_group)
  286. {
  287. ext4_group_t ngroups = ext4_get_groups_count(sb);
  288. unsigned int freei, avefreei;
  289. struct ext4_group_desc *desc, *best_desc = NULL;
  290. ext4_group_t group;
  291. int ret = -1;
  292. freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
  293. avefreei = freei / ngroups;
  294. for (group = 0; group < ngroups; group++) {
  295. desc = ext4_get_group_desc(sb, group, NULL);
  296. if (!desc || !ext4_free_inodes_count(sb, desc))
  297. continue;
  298. if (ext4_free_inodes_count(sb, desc) < avefreei)
  299. continue;
  300. if (!best_desc ||
  301. (ext4_free_blks_count(sb, desc) >
  302. ext4_free_blks_count(sb, best_desc))) {
  303. *best_group = group;
  304. best_desc = desc;
  305. ret = 0;
  306. }
  307. }
  308. return ret;
  309. }
  310. #define free_block_ratio 10
  311. static int find_group_flex(struct super_block *sb, struct inode *parent,
  312. ext4_group_t *best_group)
  313. {
  314. struct ext4_sb_info *sbi = EXT4_SB(sb);
  315. struct ext4_group_desc *desc;
  316. struct flex_groups *flex_group = sbi->s_flex_groups;
  317. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  318. ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
  319. ext4_group_t ngroups = ext4_get_groups_count(sb);
  320. int flex_size = ext4_flex_bg_size(sbi);
  321. ext4_group_t best_flex = parent_fbg_group;
  322. int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
  323. int flexbg_free_blocks;
  324. int flex_freeb_ratio;
  325. ext4_group_t n_fbg_groups;
  326. ext4_group_t i;
  327. n_fbg_groups = (ngroups + flex_size - 1) >>
  328. sbi->s_log_groups_per_flex;
  329. find_close_to_parent:
  330. flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks);
  331. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  332. if (atomic_read(&flex_group[best_flex].free_inodes) &&
  333. flex_freeb_ratio > free_block_ratio)
  334. goto found_flexbg;
  335. if (best_flex && best_flex == parent_fbg_group) {
  336. best_flex--;
  337. goto find_close_to_parent;
  338. }
  339. for (i = 0; i < n_fbg_groups; i++) {
  340. if (i == parent_fbg_group || i == parent_fbg_group - 1)
  341. continue;
  342. flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks);
  343. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  344. if (flex_freeb_ratio > free_block_ratio &&
  345. (atomic_read(&flex_group[i].free_inodes))) {
  346. best_flex = i;
  347. goto found_flexbg;
  348. }
  349. if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) ||
  350. ((atomic_read(&flex_group[i].free_blocks) >
  351. atomic_read(&flex_group[best_flex].free_blocks)) &&
  352. atomic_read(&flex_group[i].free_inodes)))
  353. best_flex = i;
  354. }
  355. if (!atomic_read(&flex_group[best_flex].free_inodes) ||
  356. !atomic_read(&flex_group[best_flex].free_blocks))
  357. return -1;
  358. found_flexbg:
  359. for (i = best_flex * flex_size; i < ngroups &&
  360. i < (best_flex + 1) * flex_size; i++) {
  361. desc = ext4_get_group_desc(sb, i, NULL);
  362. if (ext4_free_inodes_count(sb, desc)) {
  363. *best_group = i;
  364. goto out;
  365. }
  366. }
  367. return -1;
  368. out:
  369. return 0;
  370. }
  371. struct orlov_stats {
  372. __u32 free_inodes;
  373. __u32 free_blocks;
  374. __u32 used_dirs;
  375. };
  376. /*
  377. * Helper function for Orlov's allocator; returns critical information
  378. * for a particular block group or flex_bg. If flex_size is 1, then g
  379. * is a block group number; otherwise it is flex_bg number.
  380. */
  381. void get_orlov_stats(struct super_block *sb, ext4_group_t g,
  382. int flex_size, struct orlov_stats *stats)
  383. {
  384. struct ext4_group_desc *desc;
  385. struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
  386. if (flex_size > 1) {
  387. stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
  388. stats->free_blocks = atomic_read(&flex_group[g].free_blocks);
  389. stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
  390. return;
  391. }
  392. desc = ext4_get_group_desc(sb, g, NULL);
  393. if (desc) {
  394. stats->free_inodes = ext4_free_inodes_count(sb, desc);
  395. stats->free_blocks = ext4_free_blks_count(sb, desc);
  396. stats->used_dirs = ext4_used_dirs_count(sb, desc);
  397. } else {
  398. stats->free_inodes = 0;
  399. stats->free_blocks = 0;
  400. stats->used_dirs = 0;
  401. }
  402. }
  403. /*
  404. * Orlov's allocator for directories.
  405. *
  406. * We always try to spread first-level directories.
  407. *
  408. * If there are blockgroups with both free inodes and free blocks counts
  409. * not worse than average we return one with smallest directory count.
  410. * Otherwise we simply return a random group.
  411. *
  412. * For the rest rules look so:
  413. *
  414. * It's OK to put directory into a group unless
  415. * it has too many directories already (max_dirs) or
  416. * it has too few free inodes left (min_inodes) or
  417. * it has too few free blocks left (min_blocks) or
  418. * Parent's group is preferred, if it doesn't satisfy these
  419. * conditions we search cyclically through the rest. If none
  420. * of the groups look good we just look for a group with more
  421. * free inodes than average (starting at parent's group).
  422. */
  423. static int find_group_orlov(struct super_block *sb, struct inode *parent,
  424. ext4_group_t *group, int mode)
  425. {
  426. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  427. struct ext4_sb_info *sbi = EXT4_SB(sb);
  428. ext4_group_t real_ngroups = ext4_get_groups_count(sb);
  429. int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
  430. unsigned int freei, avefreei;
  431. ext4_fsblk_t freeb, avefreeb;
  432. unsigned int ndirs;
  433. int max_dirs, min_inodes;
  434. ext4_grpblk_t min_blocks;
  435. ext4_group_t i, grp, g, ngroups;
  436. struct ext4_group_desc *desc;
  437. struct orlov_stats stats;
  438. int flex_size = ext4_flex_bg_size(sbi);
  439. ngroups = real_ngroups;
  440. if (flex_size > 1) {
  441. ngroups = (real_ngroups + flex_size - 1) >>
  442. sbi->s_log_groups_per_flex;
  443. parent_group >>= sbi->s_log_groups_per_flex;
  444. }
  445. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  446. avefreei = freei / ngroups;
  447. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  448. avefreeb = freeb;
  449. do_div(avefreeb, ngroups);
  450. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  451. if (S_ISDIR(mode) &&
  452. ((parent == sb->s_root->d_inode) ||
  453. (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL))) {
  454. int best_ndir = inodes_per_group;
  455. int ret = -1;
  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);
  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, __func__,
  662. "reserved inode or inode > inodes count - "
  663. "block_group = %u, inode=%lu", group,
  664. ino + group * EXT4_INODES_PER_GROUP(sb));
  665. return 1;
  666. }
  667. /* If we didn't allocate from within the initialized part of the inode
  668. * table then we need to initialize up to this inode. */
  669. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  670. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  671. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  672. /* When marking the block group with
  673. * ~EXT4_BG_INODE_UNINIT we don't want to depend
  674. * on the value of bg_itable_unused even though
  675. * mke2fs could have initialized the same for us.
  676. * Instead we calculated the value below
  677. */
  678. free = 0;
  679. } else {
  680. free = EXT4_INODES_PER_GROUP(sb) -
  681. ext4_itable_unused_count(sb, gdp);
  682. }
  683. /*
  684. * Check the relative inode number against the last used
  685. * relative inode number in this group. if it is greater
  686. * we need to update the bg_itable_unused count
  687. *
  688. */
  689. if (ino > free)
  690. ext4_itable_unused_set(sb, gdp,
  691. (EXT4_INODES_PER_GROUP(sb) - ino));
  692. }
  693. count = ext4_free_inodes_count(sb, gdp) - 1;
  694. ext4_free_inodes_set(sb, gdp, count);
  695. if (S_ISDIR(mode)) {
  696. count = ext4_used_dirs_count(sb, gdp) + 1;
  697. ext4_used_dirs_set(sb, gdp, count);
  698. if (sbi->s_log_groups_per_flex) {
  699. ext4_group_t f = ext4_flex_group(sbi, group);
  700. atomic_inc(&sbi->s_flex_groups[f].free_inodes);
  701. }
  702. }
  703. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  704. err_ret:
  705. ext4_unlock_group(sb, group);
  706. return retval;
  707. }
  708. /*
  709. * There are two policies for allocating an inode. If the new inode is
  710. * a directory, then a forward search is made for a block group with both
  711. * free space and a low directory-to-inode ratio; if that fails, then of
  712. * the groups with above-average free space, that group with the fewest
  713. * directories already is chosen.
  714. *
  715. * For other inodes, search forward from the parent directory's block
  716. * group to find a free inode.
  717. */
  718. struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
  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_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id,
  741. dir->i_ino, mode);
  742. inode = new_inode(sb);
  743. if (!inode)
  744. return ERR_PTR(-ENOMEM);
  745. ei = EXT4_I(inode);
  746. sbi = EXT4_SB(sb);
  747. if (sbi->s_log_groups_per_flex && test_opt(sb, OLDALLOC)) {
  748. ret2 = find_group_flex(sb, dir, &group);
  749. if (ret2 == -1) {
  750. ret2 = find_group_other(sb, dir, &group, mode);
  751. if (ret2 == 0 && once) {
  752. once = 0;
  753. printk(KERN_NOTICE "ext4: find_group_flex "
  754. "failed, fallback succeeded dir %lu\n",
  755. dir->i_ino);
  756. }
  757. }
  758. goto got_group;
  759. }
  760. if (S_ISDIR(mode)) {
  761. if (test_opt(sb, OLDALLOC))
  762. ret2 = find_group_dir(sb, dir, &group);
  763. else
  764. ret2 = find_group_orlov(sb, dir, &group, mode);
  765. } else
  766. ret2 = find_group_other(sb, dir, &group, mode);
  767. got_group:
  768. EXT4_I(dir)->i_last_alloc_group = group;
  769. err = -ENOSPC;
  770. if (ret2 == -1)
  771. goto out;
  772. for (i = 0; i < ngroups; i++) {
  773. err = -EIO;
  774. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  775. if (!gdp)
  776. goto fail;
  777. brelse(inode_bitmap_bh);
  778. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  779. if (!inode_bitmap_bh)
  780. goto fail;
  781. ino = 0;
  782. repeat_in_this_group:
  783. ino = ext4_find_next_zero_bit((unsigned long *)
  784. inode_bitmap_bh->b_data,
  785. EXT4_INODES_PER_GROUP(sb), ino);
  786. if (ino < EXT4_INODES_PER_GROUP(sb)) {
  787. BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
  788. err = ext4_journal_get_write_access(handle,
  789. inode_bitmap_bh);
  790. if (err)
  791. goto fail;
  792. BUFFER_TRACE(group_desc_bh, "get_write_access");
  793. err = ext4_journal_get_write_access(handle,
  794. group_desc_bh);
  795. if (err)
  796. goto fail;
  797. if (!ext4_claim_inode(sb, inode_bitmap_bh,
  798. ino, group, mode)) {
  799. /* we won it */
  800. BUFFER_TRACE(inode_bitmap_bh,
  801. "call ext4_handle_dirty_metadata");
  802. err = ext4_handle_dirty_metadata(handle,
  803. inode,
  804. inode_bitmap_bh);
  805. if (err)
  806. goto fail;
  807. /* zero bit is inode number 1*/
  808. ino++;
  809. goto got;
  810. }
  811. /* we lost it */
  812. ext4_handle_release_buffer(handle, inode_bitmap_bh);
  813. ext4_handle_release_buffer(handle, group_desc_bh);
  814. if (++ino < EXT4_INODES_PER_GROUP(sb))
  815. goto repeat_in_this_group;
  816. }
  817. /*
  818. * This case is possible in concurrent environment. It is very
  819. * rare. We cannot repeat the find_group_xxx() call because
  820. * that will simply return the same blockgroup, because the
  821. * group descriptor metadata has not yet been updated.
  822. * So we just go onto the next blockgroup.
  823. */
  824. if (++group == ngroups)
  825. group = 0;
  826. }
  827. err = -ENOSPC;
  828. goto out;
  829. got:
  830. /* We may have to initialize the block bitmap if it isn't already */
  831. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
  832. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  833. struct buffer_head *block_bitmap_bh;
  834. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  835. BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
  836. err = ext4_journal_get_write_access(handle, block_bitmap_bh);
  837. if (err) {
  838. brelse(block_bitmap_bh);
  839. goto fail;
  840. }
  841. free = 0;
  842. ext4_lock_group(sb, group);
  843. /* recheck and clear flag under lock if we still need to */
  844. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  845. free = ext4_free_blocks_after_init(sb, group, gdp);
  846. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  847. ext4_free_blks_set(sb, gdp, free);
  848. gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
  849. gdp);
  850. }
  851. ext4_unlock_group(sb, group);
  852. /* Don't need to dirty bitmap block if we didn't change it */
  853. if (free) {
  854. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  855. err = ext4_handle_dirty_metadata(handle,
  856. NULL, block_bitmap_bh);
  857. }
  858. brelse(block_bitmap_bh);
  859. if (err)
  860. goto fail;
  861. }
  862. BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
  863. err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
  864. if (err)
  865. goto fail;
  866. percpu_counter_dec(&sbi->s_freeinodes_counter);
  867. if (S_ISDIR(mode))
  868. percpu_counter_inc(&sbi->s_dirs_counter);
  869. sb->s_dirt = 1;
  870. if (sbi->s_log_groups_per_flex) {
  871. flex_group = ext4_flex_group(sbi, group);
  872. atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
  873. }
  874. inode->i_uid = current_fsuid();
  875. if (test_opt(sb, GRPID))
  876. inode->i_gid = dir->i_gid;
  877. else if (dir->i_mode & S_ISGID) {
  878. inode->i_gid = dir->i_gid;
  879. if (S_ISDIR(mode))
  880. mode |= S_ISGID;
  881. } else
  882. inode->i_gid = current_fsgid();
  883. inode->i_mode = mode;
  884. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  885. /* This is the optimal IO size (for stat), not the fs block size */
  886. inode->i_blocks = 0;
  887. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  888. ext4_current_time(inode);
  889. memset(ei->i_data, 0, sizeof(ei->i_data));
  890. ei->i_dir_start_lookup = 0;
  891. ei->i_disksize = 0;
  892. /*
  893. * Don't inherit extent flag from directory, amongst others. We set
  894. * extent flag on newly created directory and file only if -o extent
  895. * mount option is specified
  896. */
  897. ei->i_flags =
  898. ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
  899. ei->i_file_acl = 0;
  900. ei->i_dtime = 0;
  901. ei->i_block_group = group;
  902. ei->i_last_alloc_group = ~0;
  903. ext4_set_inode_flags(inode);
  904. if (IS_DIRSYNC(inode))
  905. ext4_handle_sync(handle);
  906. if (insert_inode_locked(inode) < 0) {
  907. err = -EINVAL;
  908. goto fail_drop;
  909. }
  910. spin_lock(&sbi->s_next_gen_lock);
  911. inode->i_generation = sbi->s_next_generation++;
  912. spin_unlock(&sbi->s_next_gen_lock);
  913. ei->i_state = EXT4_STATE_NEW;
  914. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  915. ret = inode;
  916. if (vfs_dq_alloc_inode(inode)) {
  917. err = -EDQUOT;
  918. goto fail_drop;
  919. }
  920. err = ext4_init_acl(handle, inode, dir);
  921. if (err)
  922. goto fail_free_drop;
  923. err = ext4_init_security(handle, inode, dir);
  924. if (err)
  925. goto fail_free_drop;
  926. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  927. /* set extent flag only for directory, file and normal symlink*/
  928. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  929. EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
  930. ext4_ext_tree_init(handle, inode);
  931. }
  932. }
  933. err = ext4_mark_inode_dirty(handle, inode);
  934. if (err) {
  935. ext4_std_error(sb, err);
  936. goto fail_free_drop;
  937. }
  938. ext4_debug("allocating inode %lu\n", inode->i_ino);
  939. trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d",
  940. sb->s_id, inode->i_ino, dir->i_ino, mode);
  941. goto really_out;
  942. fail:
  943. ext4_std_error(sb, err);
  944. out:
  945. iput(inode);
  946. ret = ERR_PTR(err);
  947. really_out:
  948. brelse(inode_bitmap_bh);
  949. return ret;
  950. fail_free_drop:
  951. vfs_dq_free_inode(inode);
  952. fail_drop:
  953. vfs_dq_drop(inode);
  954. inode->i_flags |= S_NOQUOTA;
  955. inode->i_nlink = 0;
  956. unlock_new_inode(inode);
  957. iput(inode);
  958. brelse(inode_bitmap_bh);
  959. return ERR_PTR(err);
  960. }
  961. /* Verify that we are loading a valid orphan from disk */
  962. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  963. {
  964. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  965. ext4_group_t block_group;
  966. int bit;
  967. struct buffer_head *bitmap_bh;
  968. struct inode *inode = NULL;
  969. long err = -EIO;
  970. /* Error cases - e2fsck has already cleaned up for us */
  971. if (ino > max_ino) {
  972. ext4_warning(sb, __func__,
  973. "bad orphan ino %lu! e2fsck was run?", ino);
  974. goto error;
  975. }
  976. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  977. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  978. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  979. if (!bitmap_bh) {
  980. ext4_warning(sb, __func__,
  981. "inode bitmap error for orphan %lu", ino);
  982. goto error;
  983. }
  984. /* Having the inode bit set should be a 100% indicator that this
  985. * is a valid orphan (no e2fsck run on fs). Orphans also include
  986. * inodes that were being truncated, so we can't check i_nlink==0.
  987. */
  988. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  989. goto bad_orphan;
  990. inode = ext4_iget(sb, ino);
  991. if (IS_ERR(inode))
  992. goto iget_failed;
  993. /*
  994. * If the orphans has i_nlinks > 0 then it should be able to be
  995. * truncated, otherwise it won't be removed from the orphan list
  996. * during processing and an infinite loop will result.
  997. */
  998. if (inode->i_nlink && !ext4_can_truncate(inode))
  999. goto bad_orphan;
  1000. if (NEXT_ORPHAN(inode) > max_ino)
  1001. goto bad_orphan;
  1002. brelse(bitmap_bh);
  1003. return inode;
  1004. iget_failed:
  1005. err = PTR_ERR(inode);
  1006. inode = NULL;
  1007. bad_orphan:
  1008. ext4_warning(sb, __func__,
  1009. "bad orphan inode %lu! e2fsck was run?", ino);
  1010. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  1011. bit, (unsigned long long)bitmap_bh->b_blocknr,
  1012. ext4_test_bit(bit, bitmap_bh->b_data));
  1013. printk(KERN_NOTICE "inode=%p\n", inode);
  1014. if (inode) {
  1015. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  1016. is_bad_inode(inode));
  1017. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  1018. NEXT_ORPHAN(inode));
  1019. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  1020. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  1021. /* Avoid freeing blocks if we got a bad deleted inode */
  1022. if (inode->i_nlink == 0)
  1023. inode->i_blocks = 0;
  1024. iput(inode);
  1025. }
  1026. brelse(bitmap_bh);
  1027. error:
  1028. return ERR_PTR(err);
  1029. }
  1030. unsigned long ext4_count_free_inodes(struct super_block *sb)
  1031. {
  1032. unsigned long desc_count;
  1033. struct ext4_group_desc *gdp;
  1034. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1035. #ifdef EXT4FS_DEBUG
  1036. struct ext4_super_block *es;
  1037. unsigned long bitmap_count, x;
  1038. struct buffer_head *bitmap_bh = NULL;
  1039. es = EXT4_SB(sb)->s_es;
  1040. desc_count = 0;
  1041. bitmap_count = 0;
  1042. gdp = NULL;
  1043. for (i = 0; i < ngroups; i++) {
  1044. gdp = ext4_get_group_desc(sb, i, NULL);
  1045. if (!gdp)
  1046. continue;
  1047. desc_count += ext4_free_inodes_count(sb, gdp);
  1048. brelse(bitmap_bh);
  1049. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  1050. if (!bitmap_bh)
  1051. continue;
  1052. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  1053. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  1054. i, ext4_free_inodes_count(sb, gdp), x);
  1055. bitmap_count += x;
  1056. }
  1057. brelse(bitmap_bh);
  1058. printk(KERN_DEBUG "ext4_count_free_inodes: "
  1059. "stored = %u, computed = %lu, %lu\n",
  1060. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  1061. return desc_count;
  1062. #else
  1063. desc_count = 0;
  1064. for (i = 0; i < ngroups; i++) {
  1065. gdp = ext4_get_group_desc(sb, i, NULL);
  1066. if (!gdp)
  1067. continue;
  1068. desc_count += ext4_free_inodes_count(sb, gdp);
  1069. cond_resched();
  1070. }
  1071. return desc_count;
  1072. #endif
  1073. }
  1074. /* Called at mount-time, super-block is locked */
  1075. unsigned long ext4_count_dirs(struct super_block * sb)
  1076. {
  1077. unsigned long count = 0;
  1078. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1079. for (i = 0; i < ngroups; i++) {
  1080. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1081. if (!gdp)
  1082. continue;
  1083. count += ext4_used_dirs_count(sb, gdp);
  1084. }
  1085. return count;
  1086. }