balloc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * linux/fs/ext2/balloc.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. * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
  10. * Big-endian to little-endian byte-swapping/bitmaps by
  11. * David S. Miller (davem@caip.rutgers.edu), 1995
  12. */
  13. #include "ext2.h"
  14. #include <linux/quotaops.h>
  15. #include <linux/sched.h>
  16. #include <linux/buffer_head.h>
  17. #include <linux/capability.h>
  18. /*
  19. * balloc.c contains the blocks allocation and deallocation routines
  20. */
  21. /*
  22. * The free blocks are managed by bitmaps. A file system contains several
  23. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  24. * block for inodes, N blocks for the inode table and data blocks.
  25. *
  26. * The file system contains group descriptors which are located after the
  27. * super block. Each descriptor contains the number of the bitmap block and
  28. * the free blocks count in the block. The descriptors are loaded in memory
  29. * when a file system is mounted (see ext2_fill_super).
  30. */
  31. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  32. struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
  33. unsigned int block_group,
  34. struct buffer_head ** bh)
  35. {
  36. unsigned long group_desc;
  37. unsigned long offset;
  38. struct ext2_group_desc * desc;
  39. struct ext2_sb_info *sbi = EXT2_SB(sb);
  40. if (block_group >= sbi->s_groups_count) {
  41. ext2_error (sb, "ext2_get_group_desc",
  42. "block_group >= groups_count - "
  43. "block_group = %d, groups_count = %lu",
  44. block_group, sbi->s_groups_count);
  45. return NULL;
  46. }
  47. group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb);
  48. offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1);
  49. if (!sbi->s_group_desc[group_desc]) {
  50. ext2_error (sb, "ext2_get_group_desc",
  51. "Group descriptor not loaded - "
  52. "block_group = %d, group_desc = %lu, desc = %lu",
  53. block_group, group_desc, offset);
  54. return NULL;
  55. }
  56. desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data;
  57. if (bh)
  58. *bh = sbi->s_group_desc[group_desc];
  59. return desc + offset;
  60. }
  61. static inline int
  62. block_in_use(unsigned long block, struct super_block *sb, unsigned char *map)
  63. {
  64. return ext2_test_bit ((block -
  65. le32_to_cpu(EXT2_SB(sb)->s_es->s_first_data_block)) %
  66. EXT2_BLOCKS_PER_GROUP(sb), map);
  67. }
  68. /*
  69. * Read the bitmap for a given block_group, reading into the specified
  70. * slot in the superblock's bitmap cache.
  71. *
  72. * Return buffer_head on success or NULL in case of failure.
  73. */
  74. static struct buffer_head *
  75. read_block_bitmap(struct super_block *sb, unsigned int block_group)
  76. {
  77. int i;
  78. struct ext2_group_desc * desc;
  79. struct buffer_head * bh = NULL;
  80. unsigned int bitmap_blk;
  81. desc = ext2_get_group_desc (sb, block_group, NULL);
  82. if (!desc)
  83. return NULL;
  84. bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
  85. bh = sb_bread(sb, bitmap_blk);
  86. if (!bh)
  87. ext2_error (sb, __FUNCTION__,
  88. "Cannot read block bitmap - "
  89. "block_group = %d, block_bitmap = %u",
  90. block_group, le32_to_cpu(desc->bg_block_bitmap));
  91. /* check whether block bitmap block number is set */
  92. if (!block_in_use(bitmap_blk, sb, bh->b_data)) {
  93. /* bad block bitmap */
  94. goto error_out;
  95. }
  96. /* check whether the inode bitmap block number is set */
  97. bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap);
  98. if (!block_in_use(bitmap_blk, sb, bh->b_data)) {
  99. /* bad block bitmap */
  100. goto error_out;
  101. }
  102. /* check whether the inode table block number is set */
  103. bitmap_blk = le32_to_cpu(desc->bg_inode_table);
  104. for (i = 0; i < EXT2_SB(sb)->s_itb_per_group; i++, bitmap_blk++) {
  105. if (!block_in_use(bitmap_blk, sb, bh->b_data)) {
  106. /* bad block bitmap */
  107. goto error_out;
  108. }
  109. }
  110. return bh;
  111. error_out:
  112. brelse(bh);
  113. ext2_error(sb, __FUNCTION__,
  114. "Invalid block bitmap - "
  115. "block_group = %d, block = %u",
  116. block_group, bitmap_blk);
  117. return NULL;
  118. }
  119. /*
  120. * Set sb->s_dirt here because the superblock was "logically" altered. We
  121. * need to recalculate its free blocks count and flush it out.
  122. */
  123. static int reserve_blocks(struct super_block *sb, int count)
  124. {
  125. struct ext2_sb_info *sbi = EXT2_SB(sb);
  126. struct ext2_super_block *es = sbi->s_es;
  127. unsigned free_blocks;
  128. unsigned root_blocks;
  129. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  130. root_blocks = le32_to_cpu(es->s_r_blocks_count);
  131. if (free_blocks < count)
  132. count = free_blocks;
  133. if (free_blocks < root_blocks + count && !capable(CAP_SYS_RESOURCE) &&
  134. sbi->s_resuid != current->fsuid &&
  135. (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
  136. /*
  137. * We are too close to reserve and we are not privileged.
  138. * Can we allocate anything at all?
  139. */
  140. if (free_blocks > root_blocks)
  141. count = free_blocks - root_blocks;
  142. else
  143. return 0;
  144. }
  145. percpu_counter_sub(&sbi->s_freeblocks_counter, count);
  146. sb->s_dirt = 1;
  147. return count;
  148. }
  149. static void release_blocks(struct super_block *sb, int count)
  150. {
  151. if (count) {
  152. struct ext2_sb_info *sbi = EXT2_SB(sb);
  153. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  154. sb->s_dirt = 1;
  155. }
  156. }
  157. static int group_reserve_blocks(struct ext2_sb_info *sbi, int group_no,
  158. struct ext2_group_desc *desc, struct buffer_head *bh, int count)
  159. {
  160. unsigned free_blocks;
  161. if (!desc->bg_free_blocks_count)
  162. return 0;
  163. spin_lock(sb_bgl_lock(sbi, group_no));
  164. free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
  165. if (free_blocks < count)
  166. count = free_blocks;
  167. desc->bg_free_blocks_count = cpu_to_le16(free_blocks - count);
  168. spin_unlock(sb_bgl_lock(sbi, group_no));
  169. mark_buffer_dirty(bh);
  170. return count;
  171. }
  172. static void group_release_blocks(struct super_block *sb, int group_no,
  173. struct ext2_group_desc *desc, struct buffer_head *bh, int count)
  174. {
  175. if (count) {
  176. struct ext2_sb_info *sbi = EXT2_SB(sb);
  177. unsigned free_blocks;
  178. spin_lock(sb_bgl_lock(sbi, group_no));
  179. free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
  180. desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);
  181. spin_unlock(sb_bgl_lock(sbi, group_no));
  182. sb->s_dirt = 1;
  183. mark_buffer_dirty(bh);
  184. }
  185. }
  186. /* Free given blocks, update quota and i_blocks field */
  187. void ext2_free_blocks (struct inode * inode, unsigned long block,
  188. unsigned long count)
  189. {
  190. struct buffer_head *bitmap_bh = NULL;
  191. struct buffer_head * bh2;
  192. unsigned long block_group;
  193. unsigned long bit;
  194. unsigned long i;
  195. unsigned long overflow;
  196. struct super_block * sb = inode->i_sb;
  197. struct ext2_sb_info * sbi = EXT2_SB(sb);
  198. struct ext2_group_desc * desc;
  199. struct ext2_super_block * es = sbi->s_es;
  200. unsigned freed = 0, group_freed;
  201. if (block < le32_to_cpu(es->s_first_data_block) ||
  202. block + count < block ||
  203. block + count > le32_to_cpu(es->s_blocks_count)) {
  204. ext2_error (sb, "ext2_free_blocks",
  205. "Freeing blocks not in datazone - "
  206. "block = %lu, count = %lu", block, count);
  207. goto error_return;
  208. }
  209. ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
  210. do_more:
  211. overflow = 0;
  212. block_group = (block - le32_to_cpu(es->s_first_data_block)) /
  213. EXT2_BLOCKS_PER_GROUP(sb);
  214. bit = (block - le32_to_cpu(es->s_first_data_block)) %
  215. EXT2_BLOCKS_PER_GROUP(sb);
  216. /*
  217. * Check to see if we are freeing blocks across a group
  218. * boundary.
  219. */
  220. if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
  221. overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);
  222. count -= overflow;
  223. }
  224. brelse(bitmap_bh);
  225. bitmap_bh = read_block_bitmap(sb, block_group);
  226. if (!bitmap_bh)
  227. goto error_return;
  228. desc = ext2_get_group_desc (sb, block_group, &bh2);
  229. if (!desc)
  230. goto error_return;
  231. if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
  232. in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
  233. in_range (block, le32_to_cpu(desc->bg_inode_table),
  234. sbi->s_itb_per_group) ||
  235. in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
  236. sbi->s_itb_per_group))
  237. ext2_error (sb, "ext2_free_blocks",
  238. "Freeing blocks in system zones - "
  239. "Block = %lu, count = %lu",
  240. block, count);
  241. for (i = 0, group_freed = 0; i < count; i++) {
  242. if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  243. bit + i, bitmap_bh->b_data)) {
  244. ext2_error(sb, __FUNCTION__,
  245. "bit already cleared for block %lu", block + i);
  246. } else {
  247. group_freed++;
  248. }
  249. }
  250. mark_buffer_dirty(bitmap_bh);
  251. if (sb->s_flags & MS_SYNCHRONOUS)
  252. sync_dirty_buffer(bitmap_bh);
  253. group_release_blocks(sb, block_group, desc, bh2, group_freed);
  254. freed += group_freed;
  255. if (overflow) {
  256. block += count;
  257. count = overflow;
  258. goto do_more;
  259. }
  260. error_return:
  261. brelse(bitmap_bh);
  262. release_blocks(sb, freed);
  263. DQUOT_FREE_BLOCK(inode, freed);
  264. }
  265. static int grab_block(spinlock_t *lock, char *map, unsigned size, int goal)
  266. {
  267. int k;
  268. char *p, *r;
  269. if (!ext2_test_bit(goal, map))
  270. goto got_it;
  271. repeat:
  272. if (goal) {
  273. /*
  274. * The goal was occupied; search forward for a free
  275. * block within the next XX blocks.
  276. *
  277. * end_goal is more or less random, but it has to be
  278. * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
  279. * next 64-bit boundary is simple..
  280. */
  281. k = (goal + 63) & ~63;
  282. goal = ext2_find_next_zero_bit(map, k, goal);
  283. if (goal < k)
  284. goto got_it;
  285. /*
  286. * Search in the remainder of the current group.
  287. */
  288. }
  289. p = map + (goal >> 3);
  290. r = memscan(p, 0, (size - goal + 7) >> 3);
  291. k = (r - map) << 3;
  292. if (k < size) {
  293. /*
  294. * We have succeeded in finding a free byte in the block
  295. * bitmap. Now search backwards to find the start of this
  296. * group of free blocks - won't take more than 7 iterations.
  297. */
  298. for (goal = k; goal && !ext2_test_bit (goal - 1, map); goal--)
  299. ;
  300. goto got_it;
  301. }
  302. k = ext2_find_next_zero_bit ((u32 *)map, size, goal);
  303. if (k < size) {
  304. goal = k;
  305. goto got_it;
  306. }
  307. return -1;
  308. got_it:
  309. if (ext2_set_bit_atomic(lock, goal, (void *) map))
  310. goto repeat;
  311. return goal;
  312. }
  313. /*
  314. * ext2_new_block uses a goal block to assist allocation. If the goal is
  315. * free, or there is a free block within 32 blocks of the goal, that block
  316. * is allocated. Otherwise a forward search is made for a free block; within
  317. * each block group the search first looks for an entire free byte in the block
  318. * bitmap, and then for any free bit if that fails.
  319. * This function also updates quota and i_blocks field.
  320. */
  321. int ext2_new_block(struct inode *inode, unsigned long goal,
  322. u32 *prealloc_count, u32 *prealloc_block, int *err)
  323. {
  324. struct buffer_head *bitmap_bh = NULL;
  325. struct buffer_head *gdp_bh; /* bh2 */
  326. struct ext2_group_desc *desc;
  327. int group_no; /* i */
  328. int ret_block; /* j */
  329. int group_idx; /* k */
  330. int target_block; /* tmp */
  331. int block = 0;
  332. struct super_block *sb = inode->i_sb;
  333. struct ext2_sb_info *sbi = EXT2_SB(sb);
  334. struct ext2_super_block *es = sbi->s_es;
  335. unsigned group_size = EXT2_BLOCKS_PER_GROUP(sb);
  336. unsigned prealloc_goal = es->s_prealloc_blocks;
  337. unsigned group_alloc = 0, es_alloc, dq_alloc;
  338. int nr_scanned_groups;
  339. if (!prealloc_goal--)
  340. prealloc_goal = EXT2_DEFAULT_PREALLOC_BLOCKS - 1;
  341. if (!prealloc_count || *prealloc_count)
  342. prealloc_goal = 0;
  343. if (DQUOT_ALLOC_BLOCK(inode, 1)) {
  344. *err = -EDQUOT;
  345. goto out;
  346. }
  347. while (prealloc_goal && DQUOT_PREALLOC_BLOCK(inode, prealloc_goal))
  348. prealloc_goal--;
  349. dq_alloc = prealloc_goal + 1;
  350. es_alloc = reserve_blocks(sb, dq_alloc);
  351. if (!es_alloc) {
  352. *err = -ENOSPC;
  353. goto out_dquot;
  354. }
  355. ext2_debug ("goal=%lu.\n", goal);
  356. if (goal < le32_to_cpu(es->s_first_data_block) ||
  357. goal >= le32_to_cpu(es->s_blocks_count))
  358. goal = le32_to_cpu(es->s_first_data_block);
  359. group_no = (goal - le32_to_cpu(es->s_first_data_block)) / group_size;
  360. desc = ext2_get_group_desc (sb, group_no, &gdp_bh);
  361. if (!desc) {
  362. /*
  363. * gdp_bh may still be uninitialised. But group_release_blocks
  364. * will not touch it because group_alloc is zero.
  365. */
  366. goto io_error;
  367. }
  368. group_alloc = group_reserve_blocks(sbi, group_no, desc,
  369. gdp_bh, es_alloc);
  370. if (group_alloc) {
  371. ret_block = ((goal - le32_to_cpu(es->s_first_data_block)) %
  372. group_size);
  373. brelse(bitmap_bh);
  374. bitmap_bh = read_block_bitmap(sb, group_no);
  375. if (!bitmap_bh)
  376. goto io_error;
  377. ext2_debug("goal is at %d:%d.\n", group_no, ret_block);
  378. ret_block = grab_block(sb_bgl_lock(sbi, group_no),
  379. bitmap_bh->b_data, group_size, ret_block);
  380. if (ret_block >= 0)
  381. goto got_block;
  382. group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
  383. group_alloc = 0;
  384. }
  385. ext2_debug ("Bit not found in block group %d.\n", group_no);
  386. /*
  387. * Now search the rest of the groups. We assume that
  388. * i and desc correctly point to the last group visited.
  389. */
  390. nr_scanned_groups = 0;
  391. retry:
  392. for (group_idx = 0; !group_alloc &&
  393. group_idx < sbi->s_groups_count; group_idx++) {
  394. group_no++;
  395. if (group_no >= sbi->s_groups_count)
  396. group_no = 0;
  397. desc = ext2_get_group_desc(sb, group_no, &gdp_bh);
  398. if (!desc)
  399. goto io_error;
  400. group_alloc = group_reserve_blocks(sbi, group_no, desc,
  401. gdp_bh, es_alloc);
  402. }
  403. if (!group_alloc) {
  404. *err = -ENOSPC;
  405. goto out_release;
  406. }
  407. brelse(bitmap_bh);
  408. bitmap_bh = read_block_bitmap(sb, group_no);
  409. if (!bitmap_bh)
  410. goto io_error;
  411. ret_block = grab_block(sb_bgl_lock(sbi, group_no), bitmap_bh->b_data,
  412. group_size, 0);
  413. if (ret_block < 0) {
  414. /*
  415. * If a free block counter is corrupted we can loop inifintely.
  416. * Detect that here.
  417. */
  418. nr_scanned_groups++;
  419. if (nr_scanned_groups > 2 * sbi->s_groups_count) {
  420. ext2_error(sb, "ext2_new_block",
  421. "corrupted free blocks counters");
  422. goto io_error;
  423. }
  424. /*
  425. * Someone else grabbed the last free block in this blockgroup
  426. * before us. Retry the scan.
  427. */
  428. group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
  429. group_alloc = 0;
  430. goto retry;
  431. }
  432. got_block:
  433. ext2_debug("using block group %d(%d)\n",
  434. group_no, desc->bg_free_blocks_count);
  435. target_block = ret_block + group_no * group_size +
  436. le32_to_cpu(es->s_first_data_block);
  437. if (target_block == le32_to_cpu(desc->bg_block_bitmap) ||
  438. target_block == le32_to_cpu(desc->bg_inode_bitmap) ||
  439. in_range(target_block, le32_to_cpu(desc->bg_inode_table),
  440. sbi->s_itb_per_group))
  441. ext2_error (sb, "ext2_new_block",
  442. "Allocating block in system zone - "
  443. "block = %u", target_block);
  444. if (target_block >= le32_to_cpu(es->s_blocks_count)) {
  445. ext2_error (sb, "ext2_new_block",
  446. "block(%d) >= blocks count(%d) - "
  447. "block_group = %d, es == %p ", ret_block,
  448. le32_to_cpu(es->s_blocks_count), group_no, es);
  449. goto io_error;
  450. }
  451. block = target_block;
  452. /* OK, we _had_ allocated something */
  453. ext2_debug("found bit %d\n", ret_block);
  454. dq_alloc--;
  455. es_alloc--;
  456. group_alloc--;
  457. /*
  458. * Do block preallocation now if required.
  459. */
  460. write_lock(&EXT2_I(inode)->i_meta_lock);
  461. if (group_alloc && !*prealloc_count) {
  462. unsigned n;
  463. for (n = 0; n < group_alloc && ++ret_block < group_size; n++) {
  464. if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group_no),
  465. ret_block,
  466. (void*) bitmap_bh->b_data))
  467. break;
  468. }
  469. *prealloc_block = block + 1;
  470. *prealloc_count = n;
  471. es_alloc -= n;
  472. dq_alloc -= n;
  473. group_alloc -= n;
  474. }
  475. write_unlock(&EXT2_I(inode)->i_meta_lock);
  476. mark_buffer_dirty(bitmap_bh);
  477. if (sb->s_flags & MS_SYNCHRONOUS)
  478. sync_dirty_buffer(bitmap_bh);
  479. ext2_debug ("allocating block %d. ", block);
  480. *err = 0;
  481. out_release:
  482. group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
  483. release_blocks(sb, es_alloc);
  484. out_dquot:
  485. DQUOT_FREE_BLOCK(inode, dq_alloc);
  486. out:
  487. brelse(bitmap_bh);
  488. return block;
  489. io_error:
  490. *err = -EIO;
  491. goto out_release;
  492. }
  493. #ifdef EXT2FS_DEBUG
  494. static const int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0};
  495. unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars)
  496. {
  497. unsigned int i;
  498. unsigned long sum = 0;
  499. if (!map)
  500. return (0);
  501. for (i = 0; i < numchars; i++)
  502. sum += nibblemap[map->b_data[i] & 0xf] +
  503. nibblemap[(map->b_data[i] >> 4) & 0xf];
  504. return (sum);
  505. }
  506. #endif /* EXT2FS_DEBUG */
  507. unsigned long ext2_count_free_blocks (struct super_block * sb)
  508. {
  509. struct ext2_group_desc * desc;
  510. unsigned long desc_count = 0;
  511. int i;
  512. #ifdef EXT2FS_DEBUG
  513. unsigned long bitmap_count, x;
  514. struct ext2_super_block *es;
  515. es = EXT2_SB(sb)->s_es;
  516. desc_count = 0;
  517. bitmap_count = 0;
  518. desc = NULL;
  519. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  520. struct buffer_head *bitmap_bh;
  521. desc = ext2_get_group_desc (sb, i, NULL);
  522. if (!desc)
  523. continue;
  524. desc_count += le16_to_cpu(desc->bg_free_blocks_count);
  525. bitmap_bh = read_block_bitmap(sb, i);
  526. if (!bitmap_bh)
  527. continue;
  528. x = ext2_count_free(bitmap_bh, sb->s_blocksize);
  529. printk ("group %d: stored = %d, counted = %lu\n",
  530. i, le16_to_cpu(desc->bg_free_blocks_count), x);
  531. bitmap_count += x;
  532. brelse(bitmap_bh);
  533. }
  534. printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
  535. (long)le32_to_cpu(es->s_free_blocks_count),
  536. desc_count, bitmap_count);
  537. return bitmap_count;
  538. #else
  539. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  540. desc = ext2_get_group_desc (sb, i, NULL);
  541. if (!desc)
  542. continue;
  543. desc_count += le16_to_cpu(desc->bg_free_blocks_count);
  544. }
  545. return desc_count;
  546. #endif
  547. }
  548. static inline int test_root(int a, int b)
  549. {
  550. int num = b;
  551. while (a > num)
  552. num *= b;
  553. return num == a;
  554. }
  555. static int ext2_group_sparse(int group)
  556. {
  557. if (group <= 1)
  558. return 1;
  559. return (test_root(group, 3) || test_root(group, 5) ||
  560. test_root(group, 7));
  561. }
  562. /**
  563. * ext2_bg_has_super - number of blocks used by the superblock in group
  564. * @sb: superblock for filesystem
  565. * @group: group number to check
  566. *
  567. * Return the number of blocks used by the superblock (primary or backup)
  568. * in this group. Currently this will be only 0 or 1.
  569. */
  570. int ext2_bg_has_super(struct super_block *sb, int group)
  571. {
  572. if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
  573. !ext2_group_sparse(group))
  574. return 0;
  575. return 1;
  576. }
  577. /**
  578. * ext2_bg_num_gdb - number of blocks used by the group table in group
  579. * @sb: superblock for filesystem
  580. * @group: group number to check
  581. *
  582. * Return the number of blocks used by the group descriptor table
  583. * (primary or backup) in this group. In the future there may be a
  584. * different number of descriptor blocks in each group.
  585. */
  586. unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)
  587. {
  588. if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
  589. !ext2_group_sparse(group))
  590. return 0;
  591. return EXT2_SB(sb)->s_gdb_count;
  592. }