ialloc.c 29 KB

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