ialloc.c 34 KB

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