ialloc.c 33 KB

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