ialloc.c 39 KB

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