ialloc.c 34 KB

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