ialloc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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 buffer_head *bh;
  317. struct flex_groups *flex_group = sbi->s_flex_groups;
  318. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  319. ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
  320. ext4_group_t ngroups = ext4_get_groups_count(sb);
  321. int flex_size = ext4_flex_bg_size(sbi);
  322. ext4_group_t best_flex = parent_fbg_group;
  323. int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
  324. int flexbg_free_blocks;
  325. int flex_freeb_ratio;
  326. ext4_group_t n_fbg_groups;
  327. ext4_group_t i;
  328. n_fbg_groups = (ngroups + flex_size - 1) >>
  329. sbi->s_log_groups_per_flex;
  330. find_close_to_parent:
  331. flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks);
  332. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  333. if (atomic_read(&flex_group[best_flex].free_inodes) &&
  334. flex_freeb_ratio > free_block_ratio)
  335. goto found_flexbg;
  336. if (best_flex && best_flex == parent_fbg_group) {
  337. best_flex--;
  338. goto find_close_to_parent;
  339. }
  340. for (i = 0; i < n_fbg_groups; i++) {
  341. if (i == parent_fbg_group || i == parent_fbg_group - 1)
  342. continue;
  343. flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks);
  344. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  345. if (flex_freeb_ratio > free_block_ratio &&
  346. (atomic_read(&flex_group[i].free_inodes))) {
  347. best_flex = i;
  348. goto found_flexbg;
  349. }
  350. if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) ||
  351. ((atomic_read(&flex_group[i].free_blocks) >
  352. atomic_read(&flex_group[best_flex].free_blocks)) &&
  353. atomic_read(&flex_group[i].free_inodes)))
  354. best_flex = i;
  355. }
  356. if (!atomic_read(&flex_group[best_flex].free_inodes) ||
  357. !atomic_read(&flex_group[best_flex].free_blocks))
  358. return -1;
  359. found_flexbg:
  360. for (i = best_flex * flex_size; i < ngroups &&
  361. i < (best_flex + 1) * flex_size; i++) {
  362. desc = ext4_get_group_desc(sb, i, &bh);
  363. if (ext4_free_inodes_count(sb, desc)) {
  364. *best_group = i;
  365. goto out;
  366. }
  367. }
  368. return -1;
  369. out:
  370. return 0;
  371. }
  372. struct orlov_stats {
  373. __u32 free_inodes;
  374. __u32 free_blocks;
  375. __u32 used_dirs;
  376. };
  377. /*
  378. * Helper function for Orlov's allocator; returns critical information
  379. * for a particular block group or flex_bg. If flex_size is 1, then g
  380. * is a block group number; otherwise it is flex_bg number.
  381. */
  382. void get_orlov_stats(struct super_block *sb, ext4_group_t g,
  383. int flex_size, struct orlov_stats *stats)
  384. {
  385. struct ext4_group_desc *desc;
  386. struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
  387. if (flex_size > 1) {
  388. stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
  389. stats->free_blocks = atomic_read(&flex_group[g].free_blocks);
  390. stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
  391. return;
  392. }
  393. desc = ext4_get_group_desc(sb, g, NULL);
  394. if (desc) {
  395. stats->free_inodes = ext4_free_inodes_count(sb, desc);
  396. stats->free_blocks = ext4_free_blks_count(sb, desc);
  397. stats->used_dirs = ext4_used_dirs_count(sb, desc);
  398. } else {
  399. stats->free_inodes = 0;
  400. stats->free_blocks = 0;
  401. stats->used_dirs = 0;
  402. }
  403. }
  404. /*
  405. * Orlov's allocator for directories.
  406. *
  407. * We always try to spread first-level directories.
  408. *
  409. * If there are blockgroups with both free inodes and free blocks counts
  410. * not worse than average we return one with smallest directory count.
  411. * Otherwise we simply return a random group.
  412. *
  413. * For the rest rules look so:
  414. *
  415. * It's OK to put directory into a group unless
  416. * it has too many directories already (max_dirs) or
  417. * it has too few free inodes left (min_inodes) or
  418. * it has too few free blocks left (min_blocks) or
  419. * Parent's group is preferred, if it doesn't satisfy these
  420. * conditions we search cyclically through the rest. If none
  421. * of the groups look good we just look for a group with more
  422. * free inodes than average (starting at parent's group).
  423. */
  424. static int find_group_orlov(struct super_block *sb, struct inode *parent,
  425. ext4_group_t *group, int mode)
  426. {
  427. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  428. struct ext4_sb_info *sbi = EXT4_SB(sb);
  429. ext4_group_t real_ngroups = ext4_get_groups_count(sb);
  430. int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
  431. unsigned int freei, avefreei;
  432. ext4_fsblk_t freeb, avefreeb;
  433. unsigned int ndirs;
  434. int max_dirs, min_inodes;
  435. ext4_grpblk_t min_blocks;
  436. ext4_group_t i, grp, g, ngroups;
  437. struct ext4_group_desc *desc;
  438. struct orlov_stats stats;
  439. int flex_size = ext4_flex_bg_size(sbi);
  440. ngroups = real_ngroups;
  441. if (flex_size > 1) {
  442. ngroups = (real_ngroups + flex_size - 1) >>
  443. sbi->s_log_groups_per_flex;
  444. parent_group >>= sbi->s_log_groups_per_flex;
  445. }
  446. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  447. avefreei = freei / ngroups;
  448. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  449. avefreeb = freeb;
  450. do_div(avefreeb, ngroups);
  451. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  452. if (S_ISDIR(mode) &&
  453. ((parent == sb->s_root->d_inode) ||
  454. (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL))) {
  455. int best_ndir = inodes_per_group;
  456. int ret = -1;
  457. get_random_bytes(&grp, sizeof(grp));
  458. parent_group = (unsigned)grp % ngroups;
  459. for (i = 0; i < ngroups; i++) {
  460. g = (parent_group + i) % ngroups;
  461. get_orlov_stats(sb, g, flex_size, &stats);
  462. if (!stats.free_inodes)
  463. continue;
  464. if (stats.used_dirs >= best_ndir)
  465. continue;
  466. if (stats.free_inodes < avefreei)
  467. continue;
  468. if (stats.free_blocks < avefreeb)
  469. continue;
  470. grp = g;
  471. ret = 0;
  472. best_ndir = stats.used_dirs;
  473. }
  474. if (ret)
  475. goto fallback;
  476. found_flex_bg:
  477. if (flex_size == 1) {
  478. *group = grp;
  479. return 0;
  480. }
  481. /*
  482. * We pack inodes at the beginning of the flexgroup's
  483. * inode tables. Block allocation decisions will do
  484. * something similar, although regular files will
  485. * start at 2nd block group of the flexgroup. See
  486. * ext4_ext_find_goal() and ext4_find_near().
  487. */
  488. grp *= flex_size;
  489. for (i = 0; i < flex_size; i++) {
  490. if (grp+i >= real_ngroups)
  491. break;
  492. desc = ext4_get_group_desc(sb, grp+i, NULL);
  493. if (desc && ext4_free_inodes_count(sb, desc)) {
  494. *group = grp+i;
  495. return 0;
  496. }
  497. }
  498. goto fallback;
  499. }
  500. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  501. min_inodes = avefreei - inodes_per_group*flex_size / 4;
  502. if (min_inodes < 1)
  503. min_inodes = 1;
  504. min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb)*flex_size / 4;
  505. /*
  506. * Start looking in the flex group where we last allocated an
  507. * inode for this parent directory
  508. */
  509. if (EXT4_I(parent)->i_last_alloc_group != ~0) {
  510. parent_group = EXT4_I(parent)->i_last_alloc_group;
  511. if (flex_size > 1)
  512. parent_group >>= sbi->s_log_groups_per_flex;
  513. }
  514. for (i = 0; i < ngroups; i++) {
  515. grp = (parent_group + i) % ngroups;
  516. get_orlov_stats(sb, grp, flex_size, &stats);
  517. if (stats.used_dirs >= max_dirs)
  518. continue;
  519. if (stats.free_inodes < min_inodes)
  520. continue;
  521. if (stats.free_blocks < min_blocks)
  522. continue;
  523. goto found_flex_bg;
  524. }
  525. fallback:
  526. ngroups = real_ngroups;
  527. avefreei = freei / ngroups;
  528. fallback_retry:
  529. parent_group = EXT4_I(parent)->i_block_group;
  530. for (i = 0; i < ngroups; i++) {
  531. grp = (parent_group + i) % ngroups;
  532. desc = ext4_get_group_desc(sb, grp, NULL);
  533. if (desc && ext4_free_inodes_count(sb, desc) &&
  534. ext4_free_inodes_count(sb, desc) >= avefreei) {
  535. *group = grp;
  536. return 0;
  537. }
  538. }
  539. if (avefreei) {
  540. /*
  541. * The free-inodes counter is approximate, and for really small
  542. * filesystems the above test can fail to find any blockgroups
  543. */
  544. avefreei = 0;
  545. goto fallback_retry;
  546. }
  547. return -1;
  548. }
  549. static int find_group_other(struct super_block *sb, struct inode *parent,
  550. ext4_group_t *group, int mode)
  551. {
  552. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  553. ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
  554. struct ext4_group_desc *desc;
  555. int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
  556. /*
  557. * Try to place the inode is the same flex group as its
  558. * parent. If we can't find space, use the Orlov algorithm to
  559. * find another flex group, and store that information in the
  560. * parent directory's inode information so that use that flex
  561. * group for future allocations.
  562. */
  563. if (flex_size > 1) {
  564. int retry = 0;
  565. try_again:
  566. parent_group &= ~(flex_size-1);
  567. last = parent_group + flex_size;
  568. if (last > ngroups)
  569. last = ngroups;
  570. for (i = parent_group; i < last; i++) {
  571. desc = ext4_get_group_desc(sb, i, NULL);
  572. if (desc && ext4_free_inodes_count(sb, desc)) {
  573. *group = i;
  574. return 0;
  575. }
  576. }
  577. if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
  578. retry = 1;
  579. parent_group = EXT4_I(parent)->i_last_alloc_group;
  580. goto try_again;
  581. }
  582. /*
  583. * If this didn't work, use the Orlov search algorithm
  584. * to find a new flex group; we pass in the mode to
  585. * avoid the topdir algorithms.
  586. */
  587. *group = parent_group + flex_size;
  588. if (*group > ngroups)
  589. *group = 0;
  590. return find_group_orlov(sb, parent, group, mode);
  591. }
  592. /*
  593. * Try to place the inode in its parent directory
  594. */
  595. *group = parent_group;
  596. desc = ext4_get_group_desc(sb, *group, NULL);
  597. if (desc && ext4_free_inodes_count(sb, desc) &&
  598. ext4_free_blks_count(sb, desc))
  599. return 0;
  600. /*
  601. * We're going to place this inode in a different blockgroup from its
  602. * parent. We want to cause files in a common directory to all land in
  603. * the same blockgroup. But we want files which are in a different
  604. * directory which shares a blockgroup with our parent to land in a
  605. * different blockgroup.
  606. *
  607. * So add our directory's i_ino into the starting point for the hash.
  608. */
  609. *group = (*group + parent->i_ino) % ngroups;
  610. /*
  611. * Use a quadratic hash to find a group with a free inode and some free
  612. * blocks.
  613. */
  614. for (i = 1; i < ngroups; i <<= 1) {
  615. *group += i;
  616. if (*group >= ngroups)
  617. *group -= ngroups;
  618. desc = ext4_get_group_desc(sb, *group, NULL);
  619. if (desc && ext4_free_inodes_count(sb, desc) &&
  620. ext4_free_blks_count(sb, desc))
  621. return 0;
  622. }
  623. /*
  624. * That failed: try linear search for a free inode, even if that group
  625. * has no free blocks.
  626. */
  627. *group = parent_group;
  628. for (i = 0; i < ngroups; i++) {
  629. if (++*group >= ngroups)
  630. *group = 0;
  631. desc = ext4_get_group_desc(sb, *group, NULL);
  632. if (desc && ext4_free_inodes_count(sb, desc))
  633. return 0;
  634. }
  635. return -1;
  636. }
  637. /*
  638. * claim the inode from the inode bitmap. If the group
  639. * is uninit we need to take the groups's ext4_group_lock
  640. * and clear the uninit flag. The inode bitmap update
  641. * and group desc uninit flag clear should be done
  642. * after holding ext4_group_lock so that ext4_read_inode_bitmap
  643. * doesn't race with the ext4_claim_inode
  644. */
  645. static int ext4_claim_inode(struct super_block *sb,
  646. struct buffer_head *inode_bitmap_bh,
  647. unsigned long ino, ext4_group_t group, int mode)
  648. {
  649. int free = 0, retval = 0, count;
  650. struct ext4_sb_info *sbi = EXT4_SB(sb);
  651. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
  652. ext4_lock_group(sb, group);
  653. if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
  654. /* not a free inode */
  655. retval = 1;
  656. goto err_ret;
  657. }
  658. ino++;
  659. if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
  660. ino > EXT4_INODES_PER_GROUP(sb)) {
  661. ext4_unlock_group(sb, group);
  662. ext4_error(sb, __func__,
  663. "reserved inode or inode > inodes count - "
  664. "block_group = %u, inode=%lu", group,
  665. ino + group * EXT4_INODES_PER_GROUP(sb));
  666. return 1;
  667. }
  668. /* If we didn't allocate from within the initialized part of the inode
  669. * table then we need to initialize up to this inode. */
  670. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  671. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  672. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  673. /* When marking the block group with
  674. * ~EXT4_BG_INODE_UNINIT we don't want to depend
  675. * on the value of bg_itable_unused even though
  676. * mke2fs could have initialized the same for us.
  677. * Instead we calculated the value below
  678. */
  679. free = 0;
  680. } else {
  681. free = EXT4_INODES_PER_GROUP(sb) -
  682. ext4_itable_unused_count(sb, gdp);
  683. }
  684. /*
  685. * Check the relative inode number against the last used
  686. * relative inode number in this group. if it is greater
  687. * we need to update the bg_itable_unused count
  688. *
  689. */
  690. if (ino > free)
  691. ext4_itable_unused_set(sb, gdp,
  692. (EXT4_INODES_PER_GROUP(sb) - ino));
  693. }
  694. count = ext4_free_inodes_count(sb, gdp) - 1;
  695. ext4_free_inodes_set(sb, gdp, count);
  696. if (S_ISDIR(mode)) {
  697. count = ext4_used_dirs_count(sb, gdp) + 1;
  698. ext4_used_dirs_set(sb, gdp, count);
  699. if (sbi->s_log_groups_per_flex) {
  700. ext4_group_t f = ext4_flex_group(sbi, group);
  701. atomic_inc(&sbi->s_flex_groups[f].free_inodes);
  702. }
  703. }
  704. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  705. err_ret:
  706. ext4_unlock_group(sb, group);
  707. return retval;
  708. }
  709. /*
  710. * There are two policies for allocating an inode. If the new inode is
  711. * a directory, then a forward search is made for a block group with both
  712. * free space and a low directory-to-inode ratio; if that fails, then of
  713. * the groups with above-average free space, that group with the fewest
  714. * directories already is chosen.
  715. *
  716. * For other inodes, search forward from the parent directory's block
  717. * group to find a free inode.
  718. */
  719. struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
  720. {
  721. struct super_block *sb;
  722. struct buffer_head *inode_bitmap_bh = NULL;
  723. struct buffer_head *group_desc_bh;
  724. ext4_group_t ngroups, group = 0;
  725. unsigned long ino = 0;
  726. struct inode *inode;
  727. struct ext4_group_desc *gdp = NULL;
  728. struct ext4_inode_info *ei;
  729. struct ext4_sb_info *sbi;
  730. int ret2, err = 0;
  731. struct inode *ret;
  732. ext4_group_t i;
  733. int free = 0;
  734. static int once = 1;
  735. ext4_group_t flex_group;
  736. /* Cannot create files in a deleted directory */
  737. if (!dir || !dir->i_nlink)
  738. return ERR_PTR(-EPERM);
  739. sb = dir->i_sb;
  740. ngroups = ext4_get_groups_count(sb);
  741. trace_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id,
  742. dir->i_ino, mode);
  743. inode = new_inode(sb);
  744. if (!inode)
  745. return ERR_PTR(-ENOMEM);
  746. ei = EXT4_I(inode);
  747. sbi = EXT4_SB(sb);
  748. if (sbi->s_log_groups_per_flex && test_opt(sb, OLDALLOC)) {
  749. ret2 = find_group_flex(sb, dir, &group);
  750. if (ret2 == -1) {
  751. ret2 = find_group_other(sb, dir, &group, mode);
  752. if (ret2 == 0 && once) {
  753. once = 0;
  754. printk(KERN_NOTICE "ext4: find_group_flex "
  755. "failed, fallback succeeded dir %lu\n",
  756. dir->i_ino);
  757. }
  758. }
  759. goto got_group;
  760. }
  761. if (S_ISDIR(mode)) {
  762. if (test_opt(sb, OLDALLOC))
  763. ret2 = find_group_dir(sb, dir, &group);
  764. else
  765. ret2 = find_group_orlov(sb, dir, &group, mode);
  766. } else
  767. ret2 = find_group_other(sb, dir, &group, mode);
  768. got_group:
  769. EXT4_I(dir)->i_last_alloc_group = group;
  770. err = -ENOSPC;
  771. if (ret2 == -1)
  772. goto out;
  773. for (i = 0; i < ngroups; i++) {
  774. err = -EIO;
  775. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  776. if (!gdp)
  777. goto fail;
  778. brelse(inode_bitmap_bh);
  779. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  780. if (!inode_bitmap_bh)
  781. goto fail;
  782. ino = 0;
  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. inode,
  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. sb->s_dirt = 1;
  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. inode->i_uid = current_fsuid();
  876. if (test_opt(sb, GRPID))
  877. inode->i_gid = dir->i_gid;
  878. else if (dir->i_mode & S_ISGID) {
  879. inode->i_gid = dir->i_gid;
  880. if (S_ISDIR(mode))
  881. mode |= S_ISGID;
  882. } else
  883. inode->i_gid = current_fsgid();
  884. inode->i_mode = mode;
  885. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  886. /* This is the optimal IO size (for stat), not the fs block size */
  887. inode->i_blocks = 0;
  888. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  889. ext4_current_time(inode);
  890. memset(ei->i_data, 0, sizeof(ei->i_data));
  891. ei->i_dir_start_lookup = 0;
  892. ei->i_disksize = 0;
  893. /*
  894. * Don't inherit extent flag from directory, amongst others. We set
  895. * extent flag on newly created directory and file only if -o extent
  896. * mount option is specified
  897. */
  898. ei->i_flags =
  899. ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
  900. ei->i_file_acl = 0;
  901. ei->i_dtime = 0;
  902. ei->i_block_group = group;
  903. ei->i_last_alloc_group = ~0;
  904. ext4_set_inode_flags(inode);
  905. if (IS_DIRSYNC(inode))
  906. ext4_handle_sync(handle);
  907. if (insert_inode_locked(inode) < 0) {
  908. err = -EINVAL;
  909. goto fail_drop;
  910. }
  911. spin_lock(&sbi->s_next_gen_lock);
  912. inode->i_generation = sbi->s_next_generation++;
  913. spin_unlock(&sbi->s_next_gen_lock);
  914. ei->i_state = EXT4_STATE_NEW;
  915. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  916. ret = inode;
  917. if (vfs_dq_alloc_inode(inode)) {
  918. err = -EDQUOT;
  919. goto fail_drop;
  920. }
  921. err = ext4_init_acl(handle, inode, dir);
  922. if (err)
  923. goto fail_free_drop;
  924. err = ext4_init_security(handle, inode, dir);
  925. if (err)
  926. goto fail_free_drop;
  927. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  928. /* set extent flag only for directory, file and normal symlink*/
  929. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  930. EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
  931. ext4_ext_tree_init(handle, inode);
  932. }
  933. }
  934. err = ext4_mark_inode_dirty(handle, inode);
  935. if (err) {
  936. ext4_std_error(sb, err);
  937. goto fail_free_drop;
  938. }
  939. ext4_debug("allocating inode %lu\n", inode->i_ino);
  940. trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d",
  941. sb->s_id, inode->i_ino, dir->i_ino, mode);
  942. goto really_out;
  943. fail:
  944. ext4_std_error(sb, err);
  945. out:
  946. iput(inode);
  947. ret = ERR_PTR(err);
  948. really_out:
  949. brelse(inode_bitmap_bh);
  950. return ret;
  951. fail_free_drop:
  952. vfs_dq_free_inode(inode);
  953. fail_drop:
  954. vfs_dq_drop(inode);
  955. inode->i_flags |= S_NOQUOTA;
  956. inode->i_nlink = 0;
  957. unlock_new_inode(inode);
  958. iput(inode);
  959. brelse(inode_bitmap_bh);
  960. return ERR_PTR(err);
  961. }
  962. /* Verify that we are loading a valid orphan from disk */
  963. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  964. {
  965. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  966. ext4_group_t block_group;
  967. int bit;
  968. struct buffer_head *bitmap_bh;
  969. struct inode *inode = NULL;
  970. long err = -EIO;
  971. /* Error cases - e2fsck has already cleaned up for us */
  972. if (ino > max_ino) {
  973. ext4_warning(sb, __func__,
  974. "bad orphan ino %lu! e2fsck was run?", ino);
  975. goto error;
  976. }
  977. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  978. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  979. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  980. if (!bitmap_bh) {
  981. ext4_warning(sb, __func__,
  982. "inode bitmap error for orphan %lu", ino);
  983. goto error;
  984. }
  985. /* Having the inode bit set should be a 100% indicator that this
  986. * is a valid orphan (no e2fsck run on fs). Orphans also include
  987. * inodes that were being truncated, so we can't check i_nlink==0.
  988. */
  989. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  990. goto bad_orphan;
  991. inode = ext4_iget(sb, ino);
  992. if (IS_ERR(inode))
  993. goto iget_failed;
  994. /*
  995. * If the orphans has i_nlinks > 0 then it should be able to be
  996. * truncated, otherwise it won't be removed from the orphan list
  997. * during processing and an infinite loop will result.
  998. */
  999. if (inode->i_nlink && !ext4_can_truncate(inode))
  1000. goto bad_orphan;
  1001. if (NEXT_ORPHAN(inode) > max_ino)
  1002. goto bad_orphan;
  1003. brelse(bitmap_bh);
  1004. return inode;
  1005. iget_failed:
  1006. err = PTR_ERR(inode);
  1007. inode = NULL;
  1008. bad_orphan:
  1009. ext4_warning(sb, __func__,
  1010. "bad orphan inode %lu! e2fsck was run?", ino);
  1011. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  1012. bit, (unsigned long long)bitmap_bh->b_blocknr,
  1013. ext4_test_bit(bit, bitmap_bh->b_data));
  1014. printk(KERN_NOTICE "inode=%p\n", inode);
  1015. if (inode) {
  1016. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  1017. is_bad_inode(inode));
  1018. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  1019. NEXT_ORPHAN(inode));
  1020. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  1021. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  1022. /* Avoid freeing blocks if we got a bad deleted inode */
  1023. if (inode->i_nlink == 0)
  1024. inode->i_blocks = 0;
  1025. iput(inode);
  1026. }
  1027. brelse(bitmap_bh);
  1028. error:
  1029. return ERR_PTR(err);
  1030. }
  1031. unsigned long ext4_count_free_inodes(struct super_block *sb)
  1032. {
  1033. unsigned long desc_count;
  1034. struct ext4_group_desc *gdp;
  1035. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1036. #ifdef EXT4FS_DEBUG
  1037. struct ext4_super_block *es;
  1038. unsigned long bitmap_count, x;
  1039. struct buffer_head *bitmap_bh = NULL;
  1040. es = EXT4_SB(sb)->s_es;
  1041. desc_count = 0;
  1042. bitmap_count = 0;
  1043. gdp = NULL;
  1044. for (i = 0; i < ngroups; i++) {
  1045. gdp = ext4_get_group_desc(sb, i, NULL);
  1046. if (!gdp)
  1047. continue;
  1048. desc_count += ext4_free_inodes_count(sb, gdp);
  1049. brelse(bitmap_bh);
  1050. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  1051. if (!bitmap_bh)
  1052. continue;
  1053. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  1054. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  1055. i, ext4_free_inodes_count(sb, gdp), x);
  1056. bitmap_count += x;
  1057. }
  1058. brelse(bitmap_bh);
  1059. printk(KERN_DEBUG "ext4_count_free_inodes: "
  1060. "stored = %u, computed = %lu, %lu\n",
  1061. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  1062. return desc_count;
  1063. #else
  1064. desc_count = 0;
  1065. for (i = 0; i < ngroups; i++) {
  1066. gdp = ext4_get_group_desc(sb, i, NULL);
  1067. if (!gdp)
  1068. continue;
  1069. desc_count += ext4_free_inodes_count(sb, gdp);
  1070. cond_resched();
  1071. }
  1072. return desc_count;
  1073. #endif
  1074. }
  1075. /* Called at mount-time, super-block is locked */
  1076. unsigned long ext4_count_dirs(struct super_block * sb)
  1077. {
  1078. unsigned long count = 0;
  1079. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1080. for (i = 0; i < ngroups; i++) {
  1081. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1082. if (!gdp)
  1083. continue;
  1084. count += ext4_used_dirs_count(sb, gdp);
  1085. }
  1086. return count;
  1087. }