ialloc.c 29 KB

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