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