ialloc.c 38 KB

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