ialloc.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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. int encrypt = 0;
  666. /* Cannot create files in a deleted directory */
  667. if (!dir || !dir->i_nlink)
  668. return ERR_PTR(-EPERM);
  669. if ((ext4_encrypted_inode(dir) ||
  670. DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) &&
  671. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
  672. err = ext4_get_encryption_info(dir);
  673. if (err)
  674. return ERR_PTR(err);
  675. if (ext4_encryption_info(dir) == NULL)
  676. return ERR_PTR(-EPERM);
  677. if (!handle)
  678. nblocks += EXT4_DATA_TRANS_BLOCKS(dir->i_sb);
  679. encrypt = 1;
  680. }
  681. sb = dir->i_sb;
  682. ngroups = ext4_get_groups_count(sb);
  683. trace_ext4_request_inode(dir, mode);
  684. inode = new_inode(sb);
  685. if (!inode)
  686. return ERR_PTR(-ENOMEM);
  687. ei = EXT4_I(inode);
  688. sbi = EXT4_SB(sb);
  689. /*
  690. * Initalize owners and quota early so that we don't have to account
  691. * for quota initialization worst case in standard inode creating
  692. * transaction
  693. */
  694. if (owner) {
  695. inode->i_mode = mode;
  696. i_uid_write(inode, owner[0]);
  697. i_gid_write(inode, owner[1]);
  698. } else if (test_opt(sb, GRPID)) {
  699. inode->i_mode = mode;
  700. inode->i_uid = current_fsuid();
  701. inode->i_gid = dir->i_gid;
  702. } else
  703. inode_init_owner(inode, dir, mode);
  704. dquot_initialize(inode);
  705. if (!goal)
  706. goal = sbi->s_inode_goal;
  707. if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
  708. group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
  709. ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
  710. ret2 = 0;
  711. goto got_group;
  712. }
  713. if (S_ISDIR(mode))
  714. ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
  715. else
  716. ret2 = find_group_other(sb, dir, &group, mode);
  717. got_group:
  718. EXT4_I(dir)->i_last_alloc_group = group;
  719. err = -ENOSPC;
  720. if (ret2 == -1)
  721. goto out;
  722. /*
  723. * Normally we will only go through one pass of this loop,
  724. * unless we get unlucky and it turns out the group we selected
  725. * had its last inode grabbed by someone else.
  726. */
  727. for (i = 0; i < ngroups; i++, ino = 0) {
  728. err = -EIO;
  729. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  730. if (!gdp)
  731. goto out;
  732. /*
  733. * Check free inodes count before loading bitmap.
  734. */
  735. if (ext4_free_inodes_count(sb, gdp) == 0) {
  736. if (++group == ngroups)
  737. group = 0;
  738. continue;
  739. }
  740. grp = ext4_get_group_info(sb, group);
  741. /* Skip groups with already-known suspicious inode tables */
  742. if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
  743. if (++group == ngroups)
  744. group = 0;
  745. continue;
  746. }
  747. brelse(inode_bitmap_bh);
  748. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  749. /* Skip groups with suspicious inode tables */
  750. if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) || !inode_bitmap_bh) {
  751. if (++group == ngroups)
  752. group = 0;
  753. continue;
  754. }
  755. repeat_in_this_group:
  756. ino = ext4_find_next_zero_bit((unsigned long *)
  757. inode_bitmap_bh->b_data,
  758. EXT4_INODES_PER_GROUP(sb), ino);
  759. if (ino >= EXT4_INODES_PER_GROUP(sb))
  760. goto next_group;
  761. if (group == 0 && (ino+1) < EXT4_FIRST_INO(sb)) {
  762. ext4_error(sb, "reserved inode found cleared - "
  763. "inode=%lu", ino + 1);
  764. continue;
  765. }
  766. if ((EXT4_SB(sb)->s_journal == NULL) &&
  767. recently_deleted(sb, group, ino)) {
  768. ino++;
  769. goto next_inode;
  770. }
  771. if (!handle) {
  772. BUG_ON(nblocks <= 0);
  773. handle = __ext4_journal_start_sb(dir->i_sb, line_no,
  774. handle_type, nblocks,
  775. 0);
  776. if (IS_ERR(handle)) {
  777. err = PTR_ERR(handle);
  778. ext4_std_error(sb, err);
  779. goto out;
  780. }
  781. }
  782. BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
  783. err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
  784. if (err) {
  785. ext4_std_error(sb, err);
  786. goto out;
  787. }
  788. ext4_lock_group(sb, group);
  789. ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
  790. ext4_unlock_group(sb, group);
  791. ino++; /* the inode bitmap is zero-based */
  792. if (!ret2)
  793. goto got; /* we grabbed the inode! */
  794. next_inode:
  795. if (ino < EXT4_INODES_PER_GROUP(sb))
  796. goto repeat_in_this_group;
  797. next_group:
  798. if (++group == ngroups)
  799. group = 0;
  800. }
  801. err = -ENOSPC;
  802. goto out;
  803. got:
  804. BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
  805. err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
  806. if (err) {
  807. ext4_std_error(sb, err);
  808. goto out;
  809. }
  810. BUFFER_TRACE(group_desc_bh, "get_write_access");
  811. err = ext4_journal_get_write_access(handle, group_desc_bh);
  812. if (err) {
  813. ext4_std_error(sb, err);
  814. goto out;
  815. }
  816. /* We may have to initialize the block bitmap if it isn't already */
  817. if (ext4_has_group_desc_csum(sb) &&
  818. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  819. struct buffer_head *block_bitmap_bh;
  820. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  821. if (!block_bitmap_bh) {
  822. err = -EIO;
  823. goto out;
  824. }
  825. BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
  826. err = ext4_journal_get_write_access(handle, block_bitmap_bh);
  827. if (err) {
  828. brelse(block_bitmap_bh);
  829. ext4_std_error(sb, err);
  830. goto out;
  831. }
  832. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  833. err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
  834. /* recheck and clear flag under lock if we still need to */
  835. ext4_lock_group(sb, group);
  836. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  837. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  838. ext4_free_group_clusters_set(sb, gdp,
  839. ext4_free_clusters_after_init(sb, group, gdp));
  840. ext4_block_bitmap_csum_set(sb, group, gdp,
  841. block_bitmap_bh);
  842. ext4_group_desc_csum_set(sb, group, gdp);
  843. }
  844. ext4_unlock_group(sb, group);
  845. brelse(block_bitmap_bh);
  846. if (err) {
  847. ext4_std_error(sb, err);
  848. goto out;
  849. }
  850. }
  851. /* Update the relevant bg descriptor fields */
  852. if (ext4_has_group_desc_csum(sb)) {
  853. int free;
  854. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  855. down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
  856. ext4_lock_group(sb, group); /* while we modify the bg desc */
  857. free = EXT4_INODES_PER_GROUP(sb) -
  858. ext4_itable_unused_count(sb, gdp);
  859. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  860. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  861. free = 0;
  862. }
  863. /*
  864. * Check the relative inode number against the last used
  865. * relative inode number in this group. if it is greater
  866. * we need to update the bg_itable_unused count
  867. */
  868. if (ino > free)
  869. ext4_itable_unused_set(sb, gdp,
  870. (EXT4_INODES_PER_GROUP(sb) - ino));
  871. up_read(&grp->alloc_sem);
  872. } else {
  873. ext4_lock_group(sb, group);
  874. }
  875. ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
  876. if (S_ISDIR(mode)) {
  877. ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
  878. if (sbi->s_log_groups_per_flex) {
  879. ext4_group_t f = ext4_flex_group(sbi, group);
  880. atomic_inc(&sbi->s_flex_groups[f].used_dirs);
  881. }
  882. }
  883. if (ext4_has_group_desc_csum(sb)) {
  884. ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
  885. EXT4_INODES_PER_GROUP(sb) / 8);
  886. ext4_group_desc_csum_set(sb, group, gdp);
  887. }
  888. ext4_unlock_group(sb, group);
  889. BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
  890. err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
  891. if (err) {
  892. ext4_std_error(sb, err);
  893. goto out;
  894. }
  895. percpu_counter_dec(&sbi->s_freeinodes_counter);
  896. if (S_ISDIR(mode))
  897. percpu_counter_inc(&sbi->s_dirs_counter);
  898. if (sbi->s_log_groups_per_flex) {
  899. flex_group = ext4_flex_group(sbi, group);
  900. atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
  901. }
  902. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  903. /* This is the optimal IO size (for stat), not the fs block size */
  904. inode->i_blocks = 0;
  905. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  906. ext4_current_time(inode);
  907. memset(ei->i_data, 0, sizeof(ei->i_data));
  908. ei->i_dir_start_lookup = 0;
  909. ei->i_disksize = 0;
  910. /* Don't inherit extent flag from directory, amongst others. */
  911. ei->i_flags =
  912. ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
  913. ei->i_file_acl = 0;
  914. ei->i_dtime = 0;
  915. ei->i_block_group = group;
  916. ei->i_last_alloc_group = ~0;
  917. ext4_set_inode_flags(inode);
  918. if (IS_DIRSYNC(inode))
  919. ext4_handle_sync(handle);
  920. if (insert_inode_locked(inode) < 0) {
  921. /*
  922. * Likely a bitmap corruption causing inode to be allocated
  923. * twice.
  924. */
  925. err = -EIO;
  926. ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
  927. inode->i_ino);
  928. goto out;
  929. }
  930. spin_lock(&sbi->s_next_gen_lock);
  931. inode->i_generation = sbi->s_next_generation++;
  932. spin_unlock(&sbi->s_next_gen_lock);
  933. /* Precompute checksum seed for inode metadata */
  934. if (ext4_has_metadata_csum(sb)) {
  935. __u32 csum;
  936. __le32 inum = cpu_to_le32(inode->i_ino);
  937. __le32 gen = cpu_to_le32(inode->i_generation);
  938. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  939. sizeof(inum));
  940. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  941. sizeof(gen));
  942. }
  943. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  944. ext4_set_inode_state(inode, EXT4_STATE_NEW);
  945. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  946. ei->i_inline_off = 0;
  947. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_INLINE_DATA))
  948. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  949. ret = inode;
  950. err = dquot_alloc_inode(inode);
  951. if (err)
  952. goto fail_drop;
  953. err = ext4_init_acl(handle, inode, dir);
  954. if (err)
  955. goto fail_free_drop;
  956. err = ext4_init_security(handle, inode, dir, qstr);
  957. if (err)
  958. goto fail_free_drop;
  959. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  960. /* set extent flag only for directory, file and normal symlink*/
  961. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  962. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  963. ext4_ext_tree_init(handle, inode);
  964. }
  965. }
  966. if (ext4_handle_valid(handle)) {
  967. ei->i_sync_tid = handle->h_transaction->t_tid;
  968. ei->i_datasync_tid = handle->h_transaction->t_tid;
  969. }
  970. if (encrypt) {
  971. err = ext4_inherit_context(dir, inode);
  972. if (err)
  973. goto fail_free_drop;
  974. }
  975. err = ext4_mark_inode_dirty(handle, inode);
  976. if (err) {
  977. ext4_std_error(sb, err);
  978. goto fail_free_drop;
  979. }
  980. ext4_debug("allocating inode %lu\n", inode->i_ino);
  981. trace_ext4_allocate_inode(inode, dir, mode);
  982. brelse(inode_bitmap_bh);
  983. return ret;
  984. fail_free_drop:
  985. dquot_free_inode(inode);
  986. fail_drop:
  987. clear_nlink(inode);
  988. unlock_new_inode(inode);
  989. out:
  990. dquot_drop(inode);
  991. inode->i_flags |= S_NOQUOTA;
  992. iput(inode);
  993. brelse(inode_bitmap_bh);
  994. return ERR_PTR(err);
  995. }
  996. /* Verify that we are loading a valid orphan from disk */
  997. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  998. {
  999. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  1000. ext4_group_t block_group;
  1001. int bit;
  1002. struct buffer_head *bitmap_bh;
  1003. struct inode *inode = NULL;
  1004. long err = -EIO;
  1005. /* Error cases - e2fsck has already cleaned up for us */
  1006. if (ino > max_ino) {
  1007. ext4_warning(sb, "bad orphan ino %lu! e2fsck was run?", ino);
  1008. goto error;
  1009. }
  1010. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  1011. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  1012. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  1013. if (!bitmap_bh) {
  1014. ext4_warning(sb, "inode bitmap error for orphan %lu", ino);
  1015. goto error;
  1016. }
  1017. /* Having the inode bit set should be a 100% indicator that this
  1018. * is a valid orphan (no e2fsck run on fs). Orphans also include
  1019. * inodes that were being truncated, so we can't check i_nlink==0.
  1020. */
  1021. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  1022. goto bad_orphan;
  1023. inode = ext4_iget(sb, ino);
  1024. if (IS_ERR(inode))
  1025. goto iget_failed;
  1026. /*
  1027. * If the orphans has i_nlinks > 0 then it should be able to be
  1028. * truncated, otherwise it won't be removed from the orphan list
  1029. * during processing and an infinite loop will result.
  1030. */
  1031. if (inode->i_nlink && !ext4_can_truncate(inode))
  1032. goto bad_orphan;
  1033. if (NEXT_ORPHAN(inode) > max_ino)
  1034. goto bad_orphan;
  1035. brelse(bitmap_bh);
  1036. return inode;
  1037. iget_failed:
  1038. err = PTR_ERR(inode);
  1039. inode = NULL;
  1040. bad_orphan:
  1041. ext4_warning(sb, "bad orphan inode %lu! e2fsck was run?", ino);
  1042. printk(KERN_WARNING "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  1043. bit, (unsigned long long)bitmap_bh->b_blocknr,
  1044. ext4_test_bit(bit, bitmap_bh->b_data));
  1045. printk(KERN_WARNING "inode=%p\n", inode);
  1046. if (inode) {
  1047. printk(KERN_WARNING "is_bad_inode(inode)=%d\n",
  1048. is_bad_inode(inode));
  1049. printk(KERN_WARNING "NEXT_ORPHAN(inode)=%u\n",
  1050. NEXT_ORPHAN(inode));
  1051. printk(KERN_WARNING "max_ino=%lu\n", max_ino);
  1052. printk(KERN_WARNING "i_nlink=%u\n", inode->i_nlink);
  1053. /* Avoid freeing blocks if we got a bad deleted inode */
  1054. if (inode->i_nlink == 0)
  1055. inode->i_blocks = 0;
  1056. iput(inode);
  1057. }
  1058. brelse(bitmap_bh);
  1059. error:
  1060. return ERR_PTR(err);
  1061. }
  1062. unsigned long ext4_count_free_inodes(struct super_block *sb)
  1063. {
  1064. unsigned long desc_count;
  1065. struct ext4_group_desc *gdp;
  1066. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1067. #ifdef EXT4FS_DEBUG
  1068. struct ext4_super_block *es;
  1069. unsigned long bitmap_count, x;
  1070. struct buffer_head *bitmap_bh = NULL;
  1071. es = EXT4_SB(sb)->s_es;
  1072. desc_count = 0;
  1073. bitmap_count = 0;
  1074. gdp = NULL;
  1075. for (i = 0; i < ngroups; i++) {
  1076. gdp = ext4_get_group_desc(sb, i, NULL);
  1077. if (!gdp)
  1078. continue;
  1079. desc_count += ext4_free_inodes_count(sb, gdp);
  1080. brelse(bitmap_bh);
  1081. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  1082. if (!bitmap_bh)
  1083. continue;
  1084. x = ext4_count_free(bitmap_bh->b_data,
  1085. EXT4_INODES_PER_GROUP(sb) / 8);
  1086. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  1087. (unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
  1088. bitmap_count += x;
  1089. }
  1090. brelse(bitmap_bh);
  1091. printk(KERN_DEBUG "ext4_count_free_inodes: "
  1092. "stored = %u, computed = %lu, %lu\n",
  1093. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  1094. return desc_count;
  1095. #else
  1096. desc_count = 0;
  1097. for (i = 0; i < ngroups; i++) {
  1098. gdp = ext4_get_group_desc(sb, i, NULL);
  1099. if (!gdp)
  1100. continue;
  1101. desc_count += ext4_free_inodes_count(sb, gdp);
  1102. cond_resched();
  1103. }
  1104. return desc_count;
  1105. #endif
  1106. }
  1107. /* Called at mount-time, super-block is locked */
  1108. unsigned long ext4_count_dirs(struct super_block * sb)
  1109. {
  1110. unsigned long count = 0;
  1111. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  1112. for (i = 0; i < ngroups; i++) {
  1113. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1114. if (!gdp)
  1115. continue;
  1116. count += ext4_used_dirs_count(sb, gdp);
  1117. }
  1118. return count;
  1119. }
  1120. /*
  1121. * Zeroes not yet zeroed inode table - just write zeroes through the whole
  1122. * inode table. Must be called without any spinlock held. The only place
  1123. * where it is called from on active part of filesystem is ext4lazyinit
  1124. * thread, so we do not need any special locks, however we have to prevent
  1125. * inode allocation from the current group, so we take alloc_sem lock, to
  1126. * block ext4_new_inode() until we are finished.
  1127. */
  1128. int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
  1129. int barrier)
  1130. {
  1131. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  1132. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1133. struct ext4_group_desc *gdp = NULL;
  1134. struct buffer_head *group_desc_bh;
  1135. handle_t *handle;
  1136. ext4_fsblk_t blk;
  1137. int num, ret = 0, used_blks = 0;
  1138. /* This should not happen, but just to be sure check this */
  1139. if (sb->s_flags & MS_RDONLY) {
  1140. ret = 1;
  1141. goto out;
  1142. }
  1143. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  1144. if (!gdp)
  1145. goto out;
  1146. /*
  1147. * We do not need to lock this, because we are the only one
  1148. * handling this flag.
  1149. */
  1150. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
  1151. goto out;
  1152. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  1153. if (IS_ERR(handle)) {
  1154. ret = PTR_ERR(handle);
  1155. goto out;
  1156. }
  1157. down_write(&grp->alloc_sem);
  1158. /*
  1159. * If inode bitmap was already initialized there may be some
  1160. * used inodes so we need to skip blocks with used inodes in
  1161. * inode table.
  1162. */
  1163. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
  1164. used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
  1165. ext4_itable_unused_count(sb, gdp)),
  1166. sbi->s_inodes_per_block);
  1167. if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
  1168. ext4_error(sb, "Something is wrong with group %u: "
  1169. "used itable blocks: %d; "
  1170. "itable unused count: %u",
  1171. group, used_blks,
  1172. ext4_itable_unused_count(sb, gdp));
  1173. ret = 1;
  1174. goto err_out;
  1175. }
  1176. blk = ext4_inode_table(sb, gdp) + used_blks;
  1177. num = sbi->s_itb_per_group - used_blks;
  1178. BUFFER_TRACE(group_desc_bh, "get_write_access");
  1179. ret = ext4_journal_get_write_access(handle,
  1180. group_desc_bh);
  1181. if (ret)
  1182. goto err_out;
  1183. /*
  1184. * Skip zeroout if the inode table is full. But we set the ZEROED
  1185. * flag anyway, because obviously, when it is full it does not need
  1186. * further zeroing.
  1187. */
  1188. if (unlikely(num == 0))
  1189. goto skip_zeroout;
  1190. ext4_debug("going to zero out inode table in group %d\n",
  1191. group);
  1192. ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
  1193. if (ret < 0)
  1194. goto err_out;
  1195. if (barrier)
  1196. blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
  1197. skip_zeroout:
  1198. ext4_lock_group(sb, group);
  1199. gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
  1200. ext4_group_desc_csum_set(sb, group, gdp);
  1201. ext4_unlock_group(sb, group);
  1202. BUFFER_TRACE(group_desc_bh,
  1203. "call ext4_handle_dirty_metadata");
  1204. ret = ext4_handle_dirty_metadata(handle, NULL,
  1205. group_desc_bh);
  1206. err_out:
  1207. up_write(&grp->alloc_sem);
  1208. ext4_journal_stop(handle);
  1209. out:
  1210. return ret;
  1211. }