ialloc.c 30 KB

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