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