ialloc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * linux/fs/ext4/ialloc.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * BSD ufs-inspired inode and directory allocation by
  10. * Stephen Tweedie (sct@redhat.com), 1993
  11. * Big-endian to little-endian byte-swapping/bitmaps by
  12. * David S. Miller (davem@caip.rutgers.edu), 1995
  13. */
  14. #include <linux/time.h>
  15. #include <linux/fs.h>
  16. #include <linux/jbd2.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 <asm/byteorder.h>
  25. #include "ext4.h"
  26. #include "ext4_jbd2.h"
  27. #include "xattr.h"
  28. #include "acl.h"
  29. #include "group.h"
  30. /*
  31. * ialloc.c contains the inodes allocation and deallocation routines
  32. */
  33. /*
  34. * The free inodes are managed by bitmaps. A file system contains several
  35. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  36. * block for inodes, N blocks for the inode table and data blocks.
  37. *
  38. * The file system contains group descriptors which are located after the
  39. * super block. Each descriptor contains the number of the bitmap block and
  40. * the free blocks count in the block.
  41. */
  42. /*
  43. * To avoid calling the atomic setbit hundreds or thousands of times, we only
  44. * need to use it within a single byte (to ensure we get endianness right).
  45. * We can use memset for the rest of the bitmap as there are no other users.
  46. */
  47. void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
  48. {
  49. int i;
  50. if (start_bit >= end_bit)
  51. return;
  52. ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
  53. for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
  54. ext4_set_bit(i, bitmap);
  55. if (i < end_bit)
  56. memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
  57. }
  58. /* Initializes an uninitialized inode bitmap */
  59. unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
  60. ext4_group_t block_group,
  61. struct ext4_group_desc *gdp)
  62. {
  63. struct ext4_sb_info *sbi = EXT4_SB(sb);
  64. J_ASSERT_BH(bh, buffer_locked(bh));
  65. /* If checksum is bad mark all blocks and inodes use to prevent
  66. * allocation, essentially implementing a per-group read-only flag. */
  67. if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
  68. ext4_error(sb, __func__, "Checksum bad for group %lu\n",
  69. block_group);
  70. gdp->bg_free_blocks_count = 0;
  71. gdp->bg_free_inodes_count = 0;
  72. gdp->bg_itable_unused = 0;
  73. memset(bh->b_data, 0xff, sb->s_blocksize);
  74. return 0;
  75. }
  76. memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
  77. mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
  78. bh->b_data);
  79. return EXT4_INODES_PER_GROUP(sb);
  80. }
  81. /*
  82. * Read the inode allocation bitmap for a given block_group, reading
  83. * into the specified slot in the superblock's bitmap cache.
  84. *
  85. * Return buffer_head of bitmap on success or NULL.
  86. */
  87. static struct buffer_head *
  88. ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
  89. {
  90. struct ext4_group_desc *desc;
  91. struct buffer_head *bh = NULL;
  92. ext4_fsblk_t bitmap_blk;
  93. desc = ext4_get_group_desc(sb, block_group, NULL);
  94. if (!desc)
  95. return NULL;
  96. bitmap_blk = ext4_inode_bitmap(sb, desc);
  97. bh = sb_getblk(sb, bitmap_blk);
  98. if (unlikely(!bh)) {
  99. ext4_error(sb, __func__,
  100. "Cannot read inode bitmap - "
  101. "block_group = %lu, inode_bitmap = %llu",
  102. block_group, bitmap_blk);
  103. return NULL;
  104. }
  105. if (bh_uptodate_or_lock(bh))
  106. return bh;
  107. spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
  108. if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  109. ext4_init_inode_bitmap(sb, bh, block_group, desc);
  110. set_buffer_uptodate(bh);
  111. unlock_buffer(bh);
  112. spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
  113. return bh;
  114. }
  115. spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
  116. if (bh_submit_read(bh) < 0) {
  117. put_bh(bh);
  118. ext4_error(sb, __func__,
  119. "Cannot read inode bitmap - "
  120. "block_group = %lu, inode_bitmap = %llu",
  121. block_group, bitmap_blk);
  122. return NULL;
  123. }
  124. return bh;
  125. }
  126. /*
  127. * NOTE! When we get the inode, we're the only people
  128. * that have access to it, and as such there are no
  129. * race conditions we have to worry about. The inode
  130. * is not on the hash-lists, and it cannot be reached
  131. * through the filesystem because the directory entry
  132. * has been deleted earlier.
  133. *
  134. * HOWEVER: we must make sure that we get no aliases,
  135. * which means that we have to call "clear_inode()"
  136. * _before_ we mark the inode not in use in the inode
  137. * bitmaps. Otherwise a newly created file might use
  138. * the same inode number (not actually the same pointer
  139. * though), and then we'd have two inodes sharing the
  140. * same inode number and space on the harddisk.
  141. */
  142. void ext4_free_inode (handle_t *handle, struct inode * inode)
  143. {
  144. struct super_block * sb = inode->i_sb;
  145. int is_directory;
  146. unsigned long ino;
  147. struct buffer_head *bitmap_bh = NULL;
  148. struct buffer_head *bh2;
  149. ext4_group_t block_group;
  150. unsigned long bit;
  151. struct ext4_group_desc * gdp;
  152. struct ext4_super_block * es;
  153. struct ext4_sb_info *sbi;
  154. int fatal = 0, err;
  155. ext4_group_t flex_group;
  156. if (atomic_read(&inode->i_count) > 1) {
  157. printk ("ext4_free_inode: inode has count=%d\n",
  158. atomic_read(&inode->i_count));
  159. return;
  160. }
  161. if (inode->i_nlink) {
  162. printk ("ext4_free_inode: inode has nlink=%d\n",
  163. inode->i_nlink);
  164. return;
  165. }
  166. if (!sb) {
  167. printk("ext4_free_inode: inode on nonexistent device\n");
  168. return;
  169. }
  170. sbi = EXT4_SB(sb);
  171. ino = inode->i_ino;
  172. ext4_debug ("freeing inode %lu\n", ino);
  173. /*
  174. * Note: we must free any quota before locking the superblock,
  175. * as writing the quota to disk may need the lock as well.
  176. */
  177. DQUOT_INIT(inode);
  178. ext4_xattr_delete_inode(handle, inode);
  179. DQUOT_FREE_INODE(inode);
  180. DQUOT_DROP(inode);
  181. is_directory = S_ISDIR(inode->i_mode);
  182. /* Do this BEFORE marking the inode not in use or returning an error */
  183. clear_inode (inode);
  184. es = EXT4_SB(sb)->s_es;
  185. if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  186. ext4_error (sb, "ext4_free_inode",
  187. "reserved or nonexistent inode %lu", ino);
  188. goto error_return;
  189. }
  190. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  191. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  192. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  193. if (!bitmap_bh)
  194. goto error_return;
  195. BUFFER_TRACE(bitmap_bh, "get_write_access");
  196. fatal = ext4_journal_get_write_access(handle, bitmap_bh);
  197. if (fatal)
  198. goto error_return;
  199. /* Ok, now we can actually update the inode bitmaps.. */
  200. if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  201. bit, bitmap_bh->b_data))
  202. ext4_error (sb, "ext4_free_inode",
  203. "bit already cleared for inode %lu", ino);
  204. else {
  205. gdp = ext4_get_group_desc (sb, block_group, &bh2);
  206. BUFFER_TRACE(bh2, "get_write_access");
  207. fatal = ext4_journal_get_write_access(handle, bh2);
  208. if (fatal) goto error_return;
  209. if (gdp) {
  210. spin_lock(sb_bgl_lock(sbi, block_group));
  211. le16_add_cpu(&gdp->bg_free_inodes_count, 1);
  212. if (is_directory)
  213. le16_add_cpu(&gdp->bg_used_dirs_count, -1);
  214. gdp->bg_checksum = ext4_group_desc_csum(sbi,
  215. block_group, gdp);
  216. spin_unlock(sb_bgl_lock(sbi, block_group));
  217. percpu_counter_inc(&sbi->s_freeinodes_counter);
  218. if (is_directory)
  219. percpu_counter_dec(&sbi->s_dirs_counter);
  220. if (sbi->s_log_groups_per_flex) {
  221. flex_group = ext4_flex_group(sbi, block_group);
  222. spin_lock(sb_bgl_lock(sbi, flex_group));
  223. sbi->s_flex_groups[flex_group].free_inodes++;
  224. spin_unlock(sb_bgl_lock(sbi, flex_group));
  225. }
  226. }
  227. BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
  228. err = ext4_journal_dirty_metadata(handle, bh2);
  229. if (!fatal) fatal = err;
  230. }
  231. BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
  232. err = ext4_journal_dirty_metadata(handle, bitmap_bh);
  233. if (!fatal)
  234. fatal = err;
  235. sb->s_dirt = 1;
  236. error_return:
  237. brelse(bitmap_bh);
  238. ext4_std_error(sb, fatal);
  239. }
  240. /*
  241. * There are two policies for allocating an inode. If the new inode is
  242. * a directory, then a forward search is made for a block group with both
  243. * free space and a low directory-to-inode ratio; if that fails, then of
  244. * the groups with above-average free space, that group with the fewest
  245. * directories already is chosen.
  246. *
  247. * For other inodes, search forward from the parent directory\'s block
  248. * group to find a free inode.
  249. */
  250. static int find_group_dir(struct super_block *sb, struct inode *parent,
  251. ext4_group_t *best_group)
  252. {
  253. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  254. unsigned int freei, avefreei;
  255. struct ext4_group_desc *desc, *best_desc = NULL;
  256. ext4_group_t group;
  257. int ret = -1;
  258. freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
  259. avefreei = freei / ngroups;
  260. for (group = 0; group < ngroups; group++) {
  261. desc = ext4_get_group_desc (sb, group, NULL);
  262. if (!desc || !desc->bg_free_inodes_count)
  263. continue;
  264. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  265. continue;
  266. if (!best_desc ||
  267. (le16_to_cpu(desc->bg_free_blocks_count) >
  268. le16_to_cpu(best_desc->bg_free_blocks_count))) {
  269. *best_group = group;
  270. best_desc = desc;
  271. ret = 0;
  272. }
  273. }
  274. return ret;
  275. }
  276. #define free_block_ratio 10
  277. static int find_group_flex(struct super_block *sb, struct inode *parent,
  278. ext4_group_t *best_group)
  279. {
  280. struct ext4_sb_info *sbi = EXT4_SB(sb);
  281. struct ext4_group_desc *desc;
  282. struct buffer_head *bh;
  283. struct flex_groups *flex_group = sbi->s_flex_groups;
  284. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  285. ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
  286. ext4_group_t ngroups = sbi->s_groups_count;
  287. int flex_size = ext4_flex_bg_size(sbi);
  288. ext4_group_t best_flex = parent_fbg_group;
  289. int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
  290. int flexbg_free_blocks;
  291. int flex_freeb_ratio;
  292. ext4_group_t n_fbg_groups;
  293. ext4_group_t i;
  294. n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
  295. sbi->s_log_groups_per_flex;
  296. find_close_to_parent:
  297. flexbg_free_blocks = flex_group[best_flex].free_blocks;
  298. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  299. if (flex_group[best_flex].free_inodes &&
  300. flex_freeb_ratio > free_block_ratio)
  301. goto found_flexbg;
  302. if (best_flex && best_flex == parent_fbg_group) {
  303. best_flex--;
  304. goto find_close_to_parent;
  305. }
  306. for (i = 0; i < n_fbg_groups; i++) {
  307. if (i == parent_fbg_group || i == parent_fbg_group - 1)
  308. continue;
  309. flexbg_free_blocks = flex_group[i].free_blocks;
  310. flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
  311. if (flex_freeb_ratio > free_block_ratio &&
  312. flex_group[i].free_inodes) {
  313. best_flex = i;
  314. goto found_flexbg;
  315. }
  316. if (flex_group[best_flex].free_inodes == 0 ||
  317. (flex_group[i].free_blocks >
  318. flex_group[best_flex].free_blocks &&
  319. flex_group[i].free_inodes))
  320. best_flex = i;
  321. }
  322. if (!flex_group[best_flex].free_inodes ||
  323. !flex_group[best_flex].free_blocks)
  324. return -1;
  325. found_flexbg:
  326. for (i = best_flex * flex_size; i < ngroups &&
  327. i < (best_flex + 1) * flex_size; i++) {
  328. desc = ext4_get_group_desc(sb, i, &bh);
  329. if (le16_to_cpu(desc->bg_free_inodes_count)) {
  330. *best_group = i;
  331. goto out;
  332. }
  333. }
  334. return -1;
  335. out:
  336. return 0;
  337. }
  338. /*
  339. * Orlov's allocator for directories.
  340. *
  341. * We always try to spread first-level directories.
  342. *
  343. * If there are blockgroups with both free inodes and free blocks counts
  344. * not worse than average we return one with smallest directory count.
  345. * Otherwise we simply return a random group.
  346. *
  347. * For the rest rules look so:
  348. *
  349. * It's OK to put directory into a group unless
  350. * it has too many directories already (max_dirs) or
  351. * it has too few free inodes left (min_inodes) or
  352. * it has too few free blocks left (min_blocks) or
  353. * it's already running too large debt (max_debt).
  354. * Parent's group is preferred, if it doesn't satisfy these
  355. * conditions we search cyclically through the rest. If none
  356. * of the groups look good we just look for a group with more
  357. * free inodes than average (starting at parent's group).
  358. *
  359. * Debt is incremented each time we allocate a directory and decremented
  360. * when we allocate an inode, within 0--255.
  361. */
  362. #define INODE_COST 64
  363. #define BLOCK_COST 256
  364. static int find_group_orlov(struct super_block *sb, struct inode *parent,
  365. ext4_group_t *group)
  366. {
  367. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  368. struct ext4_sb_info *sbi = EXT4_SB(sb);
  369. struct ext4_super_block *es = sbi->s_es;
  370. ext4_group_t ngroups = sbi->s_groups_count;
  371. int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
  372. unsigned int freei, avefreei;
  373. ext4_fsblk_t freeb, avefreeb;
  374. ext4_fsblk_t blocks_per_dir;
  375. unsigned int ndirs;
  376. int max_debt, max_dirs, min_inodes;
  377. ext4_grpblk_t min_blocks;
  378. ext4_group_t i;
  379. struct ext4_group_desc *desc;
  380. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  381. avefreei = freei / ngroups;
  382. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  383. avefreeb = freeb;
  384. do_div(avefreeb, ngroups);
  385. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  386. if ((parent == sb->s_root->d_inode) ||
  387. (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
  388. int best_ndir = inodes_per_group;
  389. ext4_group_t grp;
  390. int ret = -1;
  391. get_random_bytes(&grp, sizeof(grp));
  392. parent_group = (unsigned)grp % ngroups;
  393. for (i = 0; i < ngroups; i++) {
  394. grp = (parent_group + i) % ngroups;
  395. desc = ext4_get_group_desc(sb, grp, NULL);
  396. if (!desc || !desc->bg_free_inodes_count)
  397. continue;
  398. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  399. continue;
  400. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  401. continue;
  402. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  403. continue;
  404. *group = grp;
  405. ret = 0;
  406. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  407. }
  408. if (ret == 0)
  409. return ret;
  410. goto fallback;
  411. }
  412. blocks_per_dir = ext4_blocks_count(es) - freeb;
  413. do_div(blocks_per_dir, ndirs);
  414. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  415. min_inodes = avefreei - inodes_per_group / 4;
  416. min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
  417. max_debt = EXT4_BLOCKS_PER_GROUP(sb);
  418. max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
  419. if (max_debt * INODE_COST > inodes_per_group)
  420. max_debt = inodes_per_group / INODE_COST;
  421. if (max_debt > 255)
  422. max_debt = 255;
  423. if (max_debt == 0)
  424. max_debt = 1;
  425. for (i = 0; i < ngroups; i++) {
  426. *group = (parent_group + i) % ngroups;
  427. desc = ext4_get_group_desc(sb, *group, NULL);
  428. if (!desc || !desc->bg_free_inodes_count)
  429. continue;
  430. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  431. continue;
  432. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  433. continue;
  434. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  435. continue;
  436. return 0;
  437. }
  438. fallback:
  439. for (i = 0; i < ngroups; i++) {
  440. *group = (parent_group + i) % ngroups;
  441. desc = ext4_get_group_desc(sb, *group, NULL);
  442. if (desc && desc->bg_free_inodes_count &&
  443. le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  444. return 0;
  445. }
  446. if (avefreei) {
  447. /*
  448. * The free-inodes counter is approximate, and for really small
  449. * filesystems the above test can fail to find any blockgroups
  450. */
  451. avefreei = 0;
  452. goto fallback;
  453. }
  454. return -1;
  455. }
  456. static int find_group_other(struct super_block *sb, struct inode *parent,
  457. ext4_group_t *group)
  458. {
  459. ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
  460. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  461. struct ext4_group_desc *desc;
  462. ext4_group_t i;
  463. /*
  464. * Try to place the inode in its parent directory
  465. */
  466. *group = parent_group;
  467. desc = ext4_get_group_desc(sb, *group, NULL);
  468. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  469. le16_to_cpu(desc->bg_free_blocks_count))
  470. return 0;
  471. /*
  472. * We're going to place this inode in a different blockgroup from its
  473. * parent. We want to cause files in a common directory to all land in
  474. * the same blockgroup. But we want files which are in a different
  475. * directory which shares a blockgroup with our parent to land in a
  476. * different blockgroup.
  477. *
  478. * So add our directory's i_ino into the starting point for the hash.
  479. */
  480. *group = (*group + parent->i_ino) % ngroups;
  481. /*
  482. * Use a quadratic hash to find a group with a free inode and some free
  483. * blocks.
  484. */
  485. for (i = 1; i < ngroups; i <<= 1) {
  486. *group += i;
  487. if (*group >= ngroups)
  488. *group -= ngroups;
  489. desc = ext4_get_group_desc(sb, *group, NULL);
  490. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  491. le16_to_cpu(desc->bg_free_blocks_count))
  492. return 0;
  493. }
  494. /*
  495. * That failed: try linear search for a free inode, even if that group
  496. * has no free blocks.
  497. */
  498. *group = parent_group;
  499. for (i = 0; i < ngroups; i++) {
  500. if (++*group >= ngroups)
  501. *group = 0;
  502. desc = ext4_get_group_desc(sb, *group, NULL);
  503. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  504. return 0;
  505. }
  506. return -1;
  507. }
  508. /*
  509. * There are two policies for allocating an inode. If the new inode is
  510. * a directory, then a forward search is made for a block group with both
  511. * free space and a low directory-to-inode ratio; if that fails, then of
  512. * the groups with above-average free space, that group with the fewest
  513. * directories already is chosen.
  514. *
  515. * For other inodes, search forward from the parent directory's block
  516. * group to find a free inode.
  517. */
  518. struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
  519. {
  520. struct super_block *sb;
  521. struct buffer_head *bitmap_bh = NULL;
  522. struct buffer_head *bh2;
  523. ext4_group_t group = 0;
  524. unsigned long ino = 0;
  525. struct inode * inode;
  526. struct ext4_group_desc * gdp = NULL;
  527. struct ext4_super_block * es;
  528. struct ext4_inode_info *ei;
  529. struct ext4_sb_info *sbi;
  530. int ret2, err = 0;
  531. struct inode *ret;
  532. ext4_group_t i;
  533. int free = 0;
  534. ext4_group_t flex_group;
  535. /* Cannot create files in a deleted directory */
  536. if (!dir || !dir->i_nlink)
  537. return ERR_PTR(-EPERM);
  538. sb = dir->i_sb;
  539. inode = new_inode(sb);
  540. if (!inode)
  541. return ERR_PTR(-ENOMEM);
  542. ei = EXT4_I(inode);
  543. sbi = EXT4_SB(sb);
  544. es = sbi->s_es;
  545. if (sbi->s_log_groups_per_flex) {
  546. ret2 = find_group_flex(sb, dir, &group);
  547. goto got_group;
  548. }
  549. if (S_ISDIR(mode)) {
  550. if (test_opt (sb, OLDALLOC))
  551. ret2 = find_group_dir(sb, dir, &group);
  552. else
  553. ret2 = find_group_orlov(sb, dir, &group);
  554. } else
  555. ret2 = find_group_other(sb, dir, &group);
  556. got_group:
  557. err = -ENOSPC;
  558. if (ret2 == -1)
  559. goto out;
  560. for (i = 0; i < sbi->s_groups_count; i++) {
  561. err = -EIO;
  562. gdp = ext4_get_group_desc(sb, group, &bh2);
  563. if (!gdp)
  564. goto fail;
  565. brelse(bitmap_bh);
  566. bitmap_bh = ext4_read_inode_bitmap(sb, group);
  567. if (!bitmap_bh)
  568. goto fail;
  569. ino = 0;
  570. repeat_in_this_group:
  571. ino = ext4_find_next_zero_bit((unsigned long *)
  572. bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
  573. if (ino < EXT4_INODES_PER_GROUP(sb)) {
  574. BUFFER_TRACE(bitmap_bh, "get_write_access");
  575. err = ext4_journal_get_write_access(handle, bitmap_bh);
  576. if (err)
  577. goto fail;
  578. if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
  579. ino, bitmap_bh->b_data)) {
  580. /* we won it */
  581. BUFFER_TRACE(bitmap_bh,
  582. "call ext4_journal_dirty_metadata");
  583. err = ext4_journal_dirty_metadata(handle,
  584. bitmap_bh);
  585. if (err)
  586. goto fail;
  587. goto got;
  588. }
  589. /* we lost it */
  590. jbd2_journal_release_buffer(handle, bitmap_bh);
  591. if (++ino < EXT4_INODES_PER_GROUP(sb))
  592. goto repeat_in_this_group;
  593. }
  594. /*
  595. * This case is possible in concurrent environment. It is very
  596. * rare. We cannot repeat the find_group_xxx() call because
  597. * that will simply return the same blockgroup, because the
  598. * group descriptor metadata has not yet been updated.
  599. * So we just go onto the next blockgroup.
  600. */
  601. if (++group == sbi->s_groups_count)
  602. group = 0;
  603. }
  604. err = -ENOSPC;
  605. goto out;
  606. got:
  607. ino++;
  608. if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
  609. ino > EXT4_INODES_PER_GROUP(sb)) {
  610. ext4_error(sb, __func__,
  611. "reserved inode or inode > inodes count - "
  612. "block_group = %lu, inode=%lu", group,
  613. ino + group * EXT4_INODES_PER_GROUP(sb));
  614. err = -EIO;
  615. goto fail;
  616. }
  617. BUFFER_TRACE(bh2, "get_write_access");
  618. err = ext4_journal_get_write_access(handle, bh2);
  619. if (err) goto fail;
  620. /* We may have to initialize the block bitmap if it isn't already */
  621. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
  622. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  623. struct buffer_head *block_bh = ext4_read_block_bitmap(sb, group);
  624. BUFFER_TRACE(block_bh, "get block bitmap access");
  625. err = ext4_journal_get_write_access(handle, block_bh);
  626. if (err) {
  627. brelse(block_bh);
  628. goto fail;
  629. }
  630. free = 0;
  631. spin_lock(sb_bgl_lock(sbi, group));
  632. /* recheck and clear flag under lock if we still need to */
  633. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  634. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  635. free = ext4_free_blocks_after_init(sb, group, gdp);
  636. gdp->bg_free_blocks_count = cpu_to_le16(free);
  637. }
  638. spin_unlock(sb_bgl_lock(sbi, group));
  639. /* Don't need to dirty bitmap block if we didn't change it */
  640. if (free) {
  641. BUFFER_TRACE(block_bh, "dirty block bitmap");
  642. err = ext4_journal_dirty_metadata(handle, block_bh);
  643. }
  644. brelse(block_bh);
  645. if (err)
  646. goto fail;
  647. }
  648. spin_lock(sb_bgl_lock(sbi, group));
  649. /* If we didn't allocate from within the initialized part of the inode
  650. * table then we need to initialize up to this inode. */
  651. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  652. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
  653. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
  654. /* When marking the block group with
  655. * ~EXT4_BG_INODE_UNINIT we don't want to depend
  656. * on the value of bg_itable_unused even though
  657. * mke2fs could have initialized the same for us.
  658. * Instead we calculated the value below
  659. */
  660. free = 0;
  661. } else {
  662. free = EXT4_INODES_PER_GROUP(sb) -
  663. le16_to_cpu(gdp->bg_itable_unused);
  664. }
  665. /*
  666. * Check the relative inode number against the last used
  667. * relative inode number in this group. if it is greater
  668. * we need to update the bg_itable_unused count
  669. *
  670. */
  671. if (ino > free)
  672. gdp->bg_itable_unused =
  673. cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
  674. }
  675. le16_add_cpu(&gdp->bg_free_inodes_count, -1);
  676. if (S_ISDIR(mode)) {
  677. le16_add_cpu(&gdp->bg_used_dirs_count, 1);
  678. }
  679. gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
  680. spin_unlock(sb_bgl_lock(sbi, group));
  681. BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
  682. err = ext4_journal_dirty_metadata(handle, bh2);
  683. if (err) goto fail;
  684. percpu_counter_dec(&sbi->s_freeinodes_counter);
  685. if (S_ISDIR(mode))
  686. percpu_counter_inc(&sbi->s_dirs_counter);
  687. sb->s_dirt = 1;
  688. if (sbi->s_log_groups_per_flex) {
  689. flex_group = ext4_flex_group(sbi, group);
  690. spin_lock(sb_bgl_lock(sbi, flex_group));
  691. sbi->s_flex_groups[flex_group].free_inodes--;
  692. spin_unlock(sb_bgl_lock(sbi, flex_group));
  693. }
  694. inode->i_uid = current->fsuid;
  695. if (test_opt (sb, GRPID))
  696. inode->i_gid = dir->i_gid;
  697. else if (dir->i_mode & S_ISGID) {
  698. inode->i_gid = dir->i_gid;
  699. if (S_ISDIR(mode))
  700. mode |= S_ISGID;
  701. } else
  702. inode->i_gid = current->fsgid;
  703. inode->i_mode = mode;
  704. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  705. /* This is the optimal IO size (for stat), not the fs block size */
  706. inode->i_blocks = 0;
  707. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  708. ext4_current_time(inode);
  709. memset(ei->i_data, 0, sizeof(ei->i_data));
  710. ei->i_dir_start_lookup = 0;
  711. ei->i_disksize = 0;
  712. /*
  713. * Don't inherit extent flag from directory. We set extent flag on
  714. * newly created directory and file only if -o extent mount option is
  715. * specified
  716. */
  717. ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
  718. if (S_ISLNK(mode))
  719. ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
  720. /* dirsync only applies to directories */
  721. if (!S_ISDIR(mode))
  722. ei->i_flags &= ~EXT4_DIRSYNC_FL;
  723. ei->i_file_acl = 0;
  724. ei->i_dtime = 0;
  725. ei->i_block_alloc_info = NULL;
  726. ei->i_block_group = group;
  727. ext4_set_inode_flags(inode);
  728. if (IS_DIRSYNC(inode))
  729. handle->h_sync = 1;
  730. insert_inode_hash(inode);
  731. spin_lock(&sbi->s_next_gen_lock);
  732. inode->i_generation = sbi->s_next_generation++;
  733. spin_unlock(&sbi->s_next_gen_lock);
  734. ei->i_state = EXT4_STATE_NEW;
  735. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  736. ret = inode;
  737. if(DQUOT_ALLOC_INODE(inode)) {
  738. err = -EDQUOT;
  739. goto fail_drop;
  740. }
  741. err = ext4_init_acl(handle, inode, dir);
  742. if (err)
  743. goto fail_free_drop;
  744. err = ext4_init_security(handle,inode, dir);
  745. if (err)
  746. goto fail_free_drop;
  747. if (test_opt(sb, EXTENTS)) {
  748. /* set extent flag only for directory, file and normal symlink*/
  749. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  750. EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
  751. ext4_ext_tree_init(handle, inode);
  752. }
  753. }
  754. err = ext4_mark_inode_dirty(handle, inode);
  755. if (err) {
  756. ext4_std_error(sb, err);
  757. goto fail_free_drop;
  758. }
  759. ext4_debug("allocating inode %lu\n", inode->i_ino);
  760. goto really_out;
  761. fail:
  762. ext4_std_error(sb, err);
  763. out:
  764. iput(inode);
  765. ret = ERR_PTR(err);
  766. really_out:
  767. brelse(bitmap_bh);
  768. return ret;
  769. fail_free_drop:
  770. DQUOT_FREE_INODE(inode);
  771. fail_drop:
  772. DQUOT_DROP(inode);
  773. inode->i_flags |= S_NOQUOTA;
  774. inode->i_nlink = 0;
  775. iput(inode);
  776. brelse(bitmap_bh);
  777. return ERR_PTR(err);
  778. }
  779. /* Verify that we are loading a valid orphan from disk */
  780. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  781. {
  782. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  783. ext4_group_t block_group;
  784. int bit;
  785. struct buffer_head *bitmap_bh;
  786. struct inode *inode = NULL;
  787. long err = -EIO;
  788. /* Error cases - e2fsck has already cleaned up for us */
  789. if (ino > max_ino) {
  790. ext4_warning(sb, __func__,
  791. "bad orphan ino %lu! e2fsck was run?", ino);
  792. goto error;
  793. }
  794. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  795. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  796. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  797. if (!bitmap_bh) {
  798. ext4_warning(sb, __func__,
  799. "inode bitmap error for orphan %lu", ino);
  800. goto error;
  801. }
  802. /* Having the inode bit set should be a 100% indicator that this
  803. * is a valid orphan (no e2fsck run on fs). Orphans also include
  804. * inodes that were being truncated, so we can't check i_nlink==0.
  805. */
  806. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  807. goto bad_orphan;
  808. inode = ext4_iget(sb, ino);
  809. if (IS_ERR(inode))
  810. goto iget_failed;
  811. /*
  812. * If the orphans has i_nlinks > 0 then it should be able to be
  813. * truncated, otherwise it won't be removed from the orphan list
  814. * during processing and an infinite loop will result.
  815. */
  816. if (inode->i_nlink && !ext4_can_truncate(inode))
  817. goto bad_orphan;
  818. if (NEXT_ORPHAN(inode) > max_ino)
  819. goto bad_orphan;
  820. brelse(bitmap_bh);
  821. return inode;
  822. iget_failed:
  823. err = PTR_ERR(inode);
  824. inode = NULL;
  825. bad_orphan:
  826. ext4_warning(sb, __func__,
  827. "bad orphan inode %lu! e2fsck was run?", ino);
  828. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  829. bit, (unsigned long long)bitmap_bh->b_blocknr,
  830. ext4_test_bit(bit, bitmap_bh->b_data));
  831. printk(KERN_NOTICE "inode=%p\n", inode);
  832. if (inode) {
  833. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  834. is_bad_inode(inode));
  835. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  836. NEXT_ORPHAN(inode));
  837. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  838. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  839. /* Avoid freeing blocks if we got a bad deleted inode */
  840. if (inode->i_nlink == 0)
  841. inode->i_blocks = 0;
  842. iput(inode);
  843. }
  844. brelse(bitmap_bh);
  845. error:
  846. return ERR_PTR(err);
  847. }
  848. unsigned long ext4_count_free_inodes (struct super_block * sb)
  849. {
  850. unsigned long desc_count;
  851. struct ext4_group_desc *gdp;
  852. ext4_group_t i;
  853. #ifdef EXT4FS_DEBUG
  854. struct ext4_super_block *es;
  855. unsigned long bitmap_count, x;
  856. struct buffer_head *bitmap_bh = NULL;
  857. es = EXT4_SB(sb)->s_es;
  858. desc_count = 0;
  859. bitmap_count = 0;
  860. gdp = NULL;
  861. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  862. gdp = ext4_get_group_desc (sb, i, NULL);
  863. if (!gdp)
  864. continue;
  865. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  866. brelse(bitmap_bh);
  867. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  868. if (!bitmap_bh)
  869. continue;
  870. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  871. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  872. i, le16_to_cpu(gdp->bg_free_inodes_count), x);
  873. bitmap_count += x;
  874. }
  875. brelse(bitmap_bh);
  876. printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
  877. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  878. return desc_count;
  879. #else
  880. desc_count = 0;
  881. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  882. gdp = ext4_get_group_desc (sb, i, NULL);
  883. if (!gdp)
  884. continue;
  885. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  886. cond_resched();
  887. }
  888. return desc_count;
  889. #endif
  890. }
  891. /* Called at mount-time, super-block is locked */
  892. unsigned long ext4_count_dirs (struct super_block * sb)
  893. {
  894. unsigned long count = 0;
  895. ext4_group_t i;
  896. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  897. struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
  898. if (!gdp)
  899. continue;
  900. count += le16_to_cpu(gdp->bg_used_dirs_count);
  901. }
  902. return count;
  903. }