ialloc.c 28 KB

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