ialloc.c 31 KB

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