ialloc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. if (ret2 == -1) {
  649. ret2 = find_group_other(sb, dir, &group);
  650. if (ret2 == 0 && printk_ratelimit())
  651. printk(KERN_NOTICE "ext4: find_group_flex "
  652. "failed, fallback succeeded dir %lu\n",
  653. dir->i_ino);
  654. }
  655. goto got_group;
  656. }
  657. if (S_ISDIR(mode)) {
  658. if (test_opt(sb, OLDALLOC))
  659. ret2 = find_group_dir(sb, dir, &group);
  660. else
  661. ret2 = find_group_orlov(sb, dir, &group);
  662. } else
  663. ret2 = find_group_other(sb, dir, &group);
  664. got_group:
  665. err = -ENOSPC;
  666. if (ret2 == -1)
  667. goto out;
  668. for (i = 0; i < sbi->s_groups_count; i++) {
  669. err = -EIO;
  670. gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
  671. if (!gdp)
  672. goto fail;
  673. brelse(inode_bitmap_bh);
  674. inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
  675. if (!inode_bitmap_bh)
  676. goto fail;
  677. ino = 0;
  678. repeat_in_this_group:
  679. ino = ext4_find_next_zero_bit((unsigned long *)
  680. inode_bitmap_bh->b_data,
  681. EXT4_INODES_PER_GROUP(sb), ino);
  682. if (ino < EXT4_INODES_PER_GROUP(sb)) {
  683. BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
  684. err = ext4_journal_get_write_access(handle,
  685. inode_bitmap_bh);
  686. if (err)
  687. goto fail;
  688. BUFFER_TRACE(group_desc_bh, "get_write_access");
  689. err = ext4_journal_get_write_access(handle,
  690. group_desc_bh);
  691. if (err)
  692. goto fail;
  693. if (!ext4_claim_inode(sb, inode_bitmap_bh,
  694. ino, group, mode)) {
  695. /* we won it */
  696. BUFFER_TRACE(inode_bitmap_bh,
  697. "call ext4_handle_dirty_metadata");
  698. err = ext4_handle_dirty_metadata(handle,
  699. inode,
  700. inode_bitmap_bh);
  701. if (err)
  702. goto fail;
  703. /* zero bit is inode number 1*/
  704. ino++;
  705. goto got;
  706. }
  707. /* we lost it */
  708. ext4_handle_release_buffer(handle, inode_bitmap_bh);
  709. ext4_handle_release_buffer(handle, group_desc_bh);
  710. if (++ino < EXT4_INODES_PER_GROUP(sb))
  711. goto repeat_in_this_group;
  712. }
  713. /*
  714. * This case is possible in concurrent environment. It is very
  715. * rare. We cannot repeat the find_group_xxx() call because
  716. * that will simply return the same blockgroup, because the
  717. * group descriptor metadata has not yet been updated.
  718. * So we just go onto the next blockgroup.
  719. */
  720. if (++group == sbi->s_groups_count)
  721. group = 0;
  722. }
  723. err = -ENOSPC;
  724. goto out;
  725. got:
  726. /* We may have to initialize the block bitmap if it isn't already */
  727. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
  728. gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  729. struct buffer_head *block_bitmap_bh;
  730. block_bitmap_bh = ext4_read_block_bitmap(sb, group);
  731. BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
  732. err = ext4_journal_get_write_access(handle, block_bitmap_bh);
  733. if (err) {
  734. brelse(block_bitmap_bh);
  735. goto fail;
  736. }
  737. free = 0;
  738. spin_lock(sb_bgl_lock(sbi, group));
  739. /* recheck and clear flag under lock if we still need to */
  740. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  741. free = ext4_free_blocks_after_init(sb, group, gdp);
  742. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  743. ext4_free_blks_set(sb, gdp, free);
  744. gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
  745. gdp);
  746. }
  747. spin_unlock(sb_bgl_lock(sbi, group));
  748. /* Don't need to dirty bitmap block if we didn't change it */
  749. if (free) {
  750. BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
  751. err = ext4_handle_dirty_metadata(handle,
  752. NULL, block_bitmap_bh);
  753. }
  754. brelse(block_bitmap_bh);
  755. if (err)
  756. goto fail;
  757. }
  758. BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
  759. err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
  760. if (err)
  761. goto fail;
  762. percpu_counter_dec(&sbi->s_freeinodes_counter);
  763. if (S_ISDIR(mode))
  764. percpu_counter_inc(&sbi->s_dirs_counter);
  765. sb->s_dirt = 1;
  766. if (sbi->s_log_groups_per_flex) {
  767. flex_group = ext4_flex_group(sbi, group);
  768. spin_lock(sb_bgl_lock(sbi, flex_group));
  769. sbi->s_flex_groups[flex_group].free_inodes--;
  770. spin_unlock(sb_bgl_lock(sbi, flex_group));
  771. }
  772. inode->i_uid = current_fsuid();
  773. if (test_opt(sb, GRPID))
  774. inode->i_gid = dir->i_gid;
  775. else if (dir->i_mode & S_ISGID) {
  776. inode->i_gid = dir->i_gid;
  777. if (S_ISDIR(mode))
  778. mode |= S_ISGID;
  779. } else
  780. inode->i_gid = current_fsgid();
  781. inode->i_mode = mode;
  782. inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
  783. /* This is the optimal IO size (for stat), not the fs block size */
  784. inode->i_blocks = 0;
  785. inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
  786. ext4_current_time(inode);
  787. memset(ei->i_data, 0, sizeof(ei->i_data));
  788. ei->i_dir_start_lookup = 0;
  789. ei->i_disksize = 0;
  790. /*
  791. * Don't inherit extent flag from directory. We set extent flag on
  792. * newly created directory and file only if -o extent mount option is
  793. * specified
  794. */
  795. ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
  796. if (S_ISLNK(mode))
  797. ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
  798. /* dirsync only applies to directories */
  799. if (!S_ISDIR(mode))
  800. ei->i_flags &= ~EXT4_DIRSYNC_FL;
  801. ei->i_file_acl = 0;
  802. ei->i_dtime = 0;
  803. ei->i_block_group = group;
  804. ext4_set_inode_flags(inode);
  805. if (IS_DIRSYNC(inode))
  806. ext4_handle_sync(handle);
  807. if (insert_inode_locked(inode) < 0) {
  808. err = -EINVAL;
  809. goto fail_drop;
  810. }
  811. spin_lock(&sbi->s_next_gen_lock);
  812. inode->i_generation = sbi->s_next_generation++;
  813. spin_unlock(&sbi->s_next_gen_lock);
  814. ei->i_state = EXT4_STATE_NEW;
  815. ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
  816. ret = inode;
  817. if (DQUOT_ALLOC_INODE(inode)) {
  818. err = -EDQUOT;
  819. goto fail_drop;
  820. }
  821. err = ext4_init_acl(handle, inode, dir);
  822. if (err)
  823. goto fail_free_drop;
  824. err = ext4_init_security(handle, inode, dir);
  825. if (err)
  826. goto fail_free_drop;
  827. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  828. /* set extent flag only for directory, file and normal symlink*/
  829. if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
  830. EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
  831. ext4_ext_tree_init(handle, inode);
  832. }
  833. }
  834. err = ext4_mark_inode_dirty(handle, inode);
  835. if (err) {
  836. ext4_std_error(sb, err);
  837. goto fail_free_drop;
  838. }
  839. ext4_debug("allocating inode %lu\n", inode->i_ino);
  840. trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d",
  841. sb->s_id, inode->i_ino, dir->i_ino, mode);
  842. goto really_out;
  843. fail:
  844. ext4_std_error(sb, err);
  845. out:
  846. iput(inode);
  847. ret = ERR_PTR(err);
  848. really_out:
  849. brelse(inode_bitmap_bh);
  850. return ret;
  851. fail_free_drop:
  852. DQUOT_FREE_INODE(inode);
  853. fail_drop:
  854. DQUOT_DROP(inode);
  855. inode->i_flags |= S_NOQUOTA;
  856. inode->i_nlink = 0;
  857. unlock_new_inode(inode);
  858. iput(inode);
  859. brelse(inode_bitmap_bh);
  860. return ERR_PTR(err);
  861. }
  862. /* Verify that we are loading a valid orphan from disk */
  863. struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
  864. {
  865. unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
  866. ext4_group_t block_group;
  867. int bit;
  868. struct buffer_head *bitmap_bh;
  869. struct inode *inode = NULL;
  870. long err = -EIO;
  871. /* Error cases - e2fsck has already cleaned up for us */
  872. if (ino > max_ino) {
  873. ext4_warning(sb, __func__,
  874. "bad orphan ino %lu! e2fsck was run?", ino);
  875. goto error;
  876. }
  877. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  878. bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
  879. bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
  880. if (!bitmap_bh) {
  881. ext4_warning(sb, __func__,
  882. "inode bitmap error for orphan %lu", ino);
  883. goto error;
  884. }
  885. /* Having the inode bit set should be a 100% indicator that this
  886. * is a valid orphan (no e2fsck run on fs). Orphans also include
  887. * inodes that were being truncated, so we can't check i_nlink==0.
  888. */
  889. if (!ext4_test_bit(bit, bitmap_bh->b_data))
  890. goto bad_orphan;
  891. inode = ext4_iget(sb, ino);
  892. if (IS_ERR(inode))
  893. goto iget_failed;
  894. /*
  895. * If the orphans has i_nlinks > 0 then it should be able to be
  896. * truncated, otherwise it won't be removed from the orphan list
  897. * during processing and an infinite loop will result.
  898. */
  899. if (inode->i_nlink && !ext4_can_truncate(inode))
  900. goto bad_orphan;
  901. if (NEXT_ORPHAN(inode) > max_ino)
  902. goto bad_orphan;
  903. brelse(bitmap_bh);
  904. return inode;
  905. iget_failed:
  906. err = PTR_ERR(inode);
  907. inode = NULL;
  908. bad_orphan:
  909. ext4_warning(sb, __func__,
  910. "bad orphan inode %lu! e2fsck was run?", ino);
  911. printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
  912. bit, (unsigned long long)bitmap_bh->b_blocknr,
  913. ext4_test_bit(bit, bitmap_bh->b_data));
  914. printk(KERN_NOTICE "inode=%p\n", inode);
  915. if (inode) {
  916. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  917. is_bad_inode(inode));
  918. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  919. NEXT_ORPHAN(inode));
  920. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  921. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  922. /* Avoid freeing blocks if we got a bad deleted inode */
  923. if (inode->i_nlink == 0)
  924. inode->i_blocks = 0;
  925. iput(inode);
  926. }
  927. brelse(bitmap_bh);
  928. error:
  929. return ERR_PTR(err);
  930. }
  931. unsigned long ext4_count_free_inodes(struct super_block *sb)
  932. {
  933. unsigned long desc_count;
  934. struct ext4_group_desc *gdp;
  935. ext4_group_t i;
  936. #ifdef EXT4FS_DEBUG
  937. struct ext4_super_block *es;
  938. unsigned long bitmap_count, x;
  939. struct buffer_head *bitmap_bh = NULL;
  940. es = EXT4_SB(sb)->s_es;
  941. desc_count = 0;
  942. bitmap_count = 0;
  943. gdp = NULL;
  944. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  945. gdp = ext4_get_group_desc(sb, i, NULL);
  946. if (!gdp)
  947. continue;
  948. desc_count += ext4_free_inodes_count(sb, gdp);
  949. brelse(bitmap_bh);
  950. bitmap_bh = ext4_read_inode_bitmap(sb, i);
  951. if (!bitmap_bh)
  952. continue;
  953. x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
  954. printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
  955. i, ext4_free_inodes_count(sb, gdp), x);
  956. bitmap_count += x;
  957. }
  958. brelse(bitmap_bh);
  959. printk(KERN_DEBUG "ext4_count_free_inodes: "
  960. "stored = %u, computed = %lu, %lu\n",
  961. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  962. return desc_count;
  963. #else
  964. desc_count = 0;
  965. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  966. gdp = ext4_get_group_desc(sb, i, NULL);
  967. if (!gdp)
  968. continue;
  969. desc_count += ext4_free_inodes_count(sb, gdp);
  970. cond_resched();
  971. }
  972. return desc_count;
  973. #endif
  974. }
  975. /* Called at mount-time, super-block is locked */
  976. unsigned long ext4_count_dirs(struct super_block * sb)
  977. {
  978. unsigned long count = 0;
  979. ext4_group_t i;
  980. for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
  981. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  982. if (!gdp)
  983. continue;
  984. count += ext4_used_dirs_count(sb, gdp);
  985. }
  986. return count;
  987. }