ialloc.c 34 KB

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