ialloc.c 33 KB

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