ialloc.c 34 KB

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