ialloc.c 40 KB

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