ialloc.c 39 KB

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