ialloc.c 40 KB

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