ialloc.c 37 KB

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