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