ialloc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * linux/fs/ext2/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@dcs.ed.ac.uk), 1993
  11. * Big-endian to little-endian byte-swapping/bitmaps by
  12. * David S. Miller (davem@caip.rutgers.edu), 1995
  13. */
  14. #include <linux/config.h>
  15. #include <linux/quotaops.h>
  16. #include <linux/sched.h>
  17. #include <linux/backing-dev.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/random.h>
  20. #include "ext2.h"
  21. #include "xattr.h"
  22. #include "acl.h"
  23. /*
  24. * ialloc.c contains the inodes allocation and deallocation routines
  25. */
  26. /*
  27. * The free inodes are managed by bitmaps. A file system contains several
  28. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  29. * block for inodes, N blocks for the inode table and data blocks.
  30. *
  31. * The file system contains group descriptors which are located after the
  32. * super block. Each descriptor contains the number of the bitmap block and
  33. * the free blocks count in the block.
  34. */
  35. /*
  36. * Read the inode allocation bitmap for a given block_group, reading
  37. * into the specified slot in the superblock's bitmap cache.
  38. *
  39. * Return buffer_head of bitmap on success or NULL.
  40. */
  41. static struct buffer_head *
  42. read_inode_bitmap(struct super_block * sb, unsigned long block_group)
  43. {
  44. struct ext2_group_desc *desc;
  45. struct buffer_head *bh = NULL;
  46. desc = ext2_get_group_desc(sb, block_group, NULL);
  47. if (!desc)
  48. goto error_out;
  49. bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
  50. if (!bh)
  51. ext2_error(sb, "read_inode_bitmap",
  52. "Cannot read inode bitmap - "
  53. "block_group = %lu, inode_bitmap = %u",
  54. block_group, le32_to_cpu(desc->bg_inode_bitmap));
  55. error_out:
  56. return bh;
  57. }
  58. static void ext2_release_inode(struct super_block *sb, int group, int dir)
  59. {
  60. struct ext2_group_desc * desc;
  61. struct buffer_head *bh;
  62. desc = ext2_get_group_desc(sb, group, &bh);
  63. if (!desc) {
  64. ext2_error(sb, "ext2_release_inode",
  65. "can't get descriptor for group %d", group);
  66. return;
  67. }
  68. spin_lock(sb_bgl_lock(EXT2_SB(sb), group));
  69. desc->bg_free_inodes_count =
  70. cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
  71. if (dir)
  72. desc->bg_used_dirs_count =
  73. cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
  74. spin_unlock(sb_bgl_lock(EXT2_SB(sb), group));
  75. if (dir)
  76. percpu_counter_dec(&EXT2_SB(sb)->s_dirs_counter);
  77. sb->s_dirt = 1;
  78. mark_buffer_dirty(bh);
  79. }
  80. /*
  81. * NOTE! When we get the inode, we're the only people
  82. * that have access to it, and as such there are no
  83. * race conditions we have to worry about. The inode
  84. * is not on the hash-lists, and it cannot be reached
  85. * through the filesystem because the directory entry
  86. * has been deleted earlier.
  87. *
  88. * HOWEVER: we must make sure that we get no aliases,
  89. * which means that we have to call "clear_inode()"
  90. * _before_ we mark the inode not in use in the inode
  91. * bitmaps. Otherwise a newly created file might use
  92. * the same inode number (not actually the same pointer
  93. * though), and then we'd have two inodes sharing the
  94. * same inode number and space on the harddisk.
  95. */
  96. void ext2_free_inode (struct inode * inode)
  97. {
  98. struct super_block * sb = inode->i_sb;
  99. int is_directory;
  100. unsigned long ino;
  101. struct buffer_head *bitmap_bh = NULL;
  102. unsigned long block_group;
  103. unsigned long bit;
  104. struct ext2_super_block * es;
  105. ino = inode->i_ino;
  106. ext2_debug ("freeing inode %lu\n", ino);
  107. /*
  108. * Note: we must free any quota before locking the superblock,
  109. * as writing the quota to disk may need the lock as well.
  110. */
  111. if (!is_bad_inode(inode)) {
  112. /* Quota is already initialized in iput() */
  113. ext2_xattr_delete_inode(inode);
  114. DQUOT_FREE_INODE(inode);
  115. DQUOT_DROP(inode);
  116. }
  117. es = EXT2_SB(sb)->s_es;
  118. is_directory = S_ISDIR(inode->i_mode);
  119. /* Do this BEFORE marking the inode not in use or returning an error */
  120. clear_inode (inode);
  121. if (ino < EXT2_FIRST_INO(sb) ||
  122. ino > le32_to_cpu(es->s_inodes_count)) {
  123. ext2_error (sb, "ext2_free_inode",
  124. "reserved or nonexistent inode %lu", ino);
  125. goto error_return;
  126. }
  127. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  128. bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
  129. brelse(bitmap_bh);
  130. bitmap_bh = read_inode_bitmap(sb, block_group);
  131. if (!bitmap_bh)
  132. goto error_return;
  133. /* Ok, now we can actually update the inode bitmaps.. */
  134. if (!ext2_clear_bit_atomic(sb_bgl_lock(EXT2_SB(sb), block_group),
  135. bit, (void *) bitmap_bh->b_data))
  136. ext2_error (sb, "ext2_free_inode",
  137. "bit already cleared for inode %lu", ino);
  138. else
  139. ext2_release_inode(sb, block_group, is_directory);
  140. mark_buffer_dirty(bitmap_bh);
  141. if (sb->s_flags & MS_SYNCHRONOUS)
  142. sync_dirty_buffer(bitmap_bh);
  143. error_return:
  144. brelse(bitmap_bh);
  145. }
  146. /*
  147. * We perform asynchronous prereading of the new inode's inode block when
  148. * we create the inode, in the expectation that the inode will be written
  149. * back soon. There are two reasons:
  150. *
  151. * - When creating a large number of files, the async prereads will be
  152. * nicely merged into large reads
  153. * - When writing out a large number of inodes, we don't need to keep on
  154. * stalling the writes while we read the inode block.
  155. *
  156. * FIXME: ext2_get_group_desc() needs to be simplified.
  157. */
  158. static void ext2_preread_inode(struct inode *inode)
  159. {
  160. unsigned long block_group;
  161. unsigned long offset;
  162. unsigned long block;
  163. struct buffer_head *bh;
  164. struct ext2_group_desc * gdp;
  165. struct backing_dev_info *bdi;
  166. bdi = inode->i_mapping->backing_dev_info;
  167. if (bdi_read_congested(bdi))
  168. return;
  169. if (bdi_write_congested(bdi))
  170. return;
  171. block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  172. gdp = ext2_get_group_desc(inode->i_sb, block_group, &bh);
  173. if (gdp == NULL)
  174. return;
  175. /*
  176. * Figure out the offset within the block group inode table
  177. */
  178. offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
  179. EXT2_INODE_SIZE(inode->i_sb);
  180. block = le32_to_cpu(gdp->bg_inode_table) +
  181. (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
  182. sb_breadahead(inode->i_sb, block);
  183. }
  184. /*
  185. * There are two policies for allocating an inode. If the new inode is
  186. * a directory, then a forward search is made for a block group with both
  187. * free space and a low directory-to-inode ratio; if that fails, then of
  188. * the groups with above-average free space, that group with the fewest
  189. * directories already is chosen.
  190. *
  191. * For other inodes, search forward from the parent directory\'s block
  192. * group to find a free inode.
  193. */
  194. static int find_group_dir(struct super_block *sb, struct inode *parent)
  195. {
  196. int ngroups = EXT2_SB(sb)->s_groups_count;
  197. int avefreei = ext2_count_free_inodes(sb) / ngroups;
  198. struct ext2_group_desc *desc, *best_desc = NULL;
  199. struct buffer_head *bh, *best_bh = NULL;
  200. int group, best_group = -1;
  201. for (group = 0; group < ngroups; group++) {
  202. desc = ext2_get_group_desc (sb, group, &bh);
  203. if (!desc || !desc->bg_free_inodes_count)
  204. continue;
  205. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  206. continue;
  207. if (!best_desc ||
  208. (le16_to_cpu(desc->bg_free_blocks_count) >
  209. le16_to_cpu(best_desc->bg_free_blocks_count))) {
  210. best_group = group;
  211. best_desc = desc;
  212. best_bh = bh;
  213. }
  214. }
  215. if (!best_desc)
  216. return -1;
  217. return best_group;
  218. }
  219. /*
  220. * Orlov's allocator for directories.
  221. *
  222. * We always try to spread first-level directories.
  223. *
  224. * If there are blockgroups with both free inodes and free blocks counts
  225. * not worse than average we return one with smallest directory count.
  226. * Otherwise we simply return a random group.
  227. *
  228. * For the rest rules look so:
  229. *
  230. * It's OK to put directory into a group unless
  231. * it has too many directories already (max_dirs) or
  232. * it has too few free inodes left (min_inodes) or
  233. * it has too few free blocks left (min_blocks) or
  234. * it's already running too large debt (max_debt).
  235. * Parent's group is prefered, if it doesn't satisfy these
  236. * conditions we search cyclically through the rest. If none
  237. * of the groups look good we just look for a group with more
  238. * free inodes than average (starting at parent's group).
  239. *
  240. * Debt is incremented each time we allocate a directory and decremented
  241. * when we allocate an inode, within 0--255.
  242. */
  243. #define INODE_COST 64
  244. #define BLOCK_COST 256
  245. static int find_group_orlov(struct super_block *sb, struct inode *parent)
  246. {
  247. int parent_group = EXT2_I(parent)->i_block_group;
  248. struct ext2_sb_info *sbi = EXT2_SB(sb);
  249. struct ext2_super_block *es = sbi->s_es;
  250. int ngroups = sbi->s_groups_count;
  251. int inodes_per_group = EXT2_INODES_PER_GROUP(sb);
  252. int freei;
  253. int avefreei;
  254. int free_blocks;
  255. int avefreeb;
  256. int blocks_per_dir;
  257. int ndirs;
  258. int max_debt, max_dirs, min_blocks, min_inodes;
  259. int group = -1, i;
  260. struct ext2_group_desc *desc;
  261. struct buffer_head *bh;
  262. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  263. avefreei = freei / ngroups;
  264. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  265. avefreeb = free_blocks / ngroups;
  266. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  267. if ((parent == sb->s_root->d_inode) ||
  268. (EXT2_I(parent)->i_flags & EXT2_TOPDIR_FL)) {
  269. struct ext2_group_desc *best_desc = NULL;
  270. struct buffer_head *best_bh = NULL;
  271. int best_ndir = inodes_per_group;
  272. int best_group = -1;
  273. get_random_bytes(&group, sizeof(group));
  274. parent_group = (unsigned)group % ngroups;
  275. for (i = 0; i < ngroups; i++) {
  276. group = (parent_group + i) % ngroups;
  277. desc = ext2_get_group_desc (sb, group, &bh);
  278. if (!desc || !desc->bg_free_inodes_count)
  279. continue;
  280. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  281. continue;
  282. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  283. continue;
  284. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  285. continue;
  286. best_group = group;
  287. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  288. best_desc = desc;
  289. best_bh = bh;
  290. }
  291. if (best_group >= 0) {
  292. desc = best_desc;
  293. bh = best_bh;
  294. group = best_group;
  295. goto found;
  296. }
  297. goto fallback;
  298. }
  299. if (ndirs == 0)
  300. ndirs = 1; /* percpu_counters are approximate... */
  301. blocks_per_dir = (le32_to_cpu(es->s_blocks_count)-free_blocks) / ndirs;
  302. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  303. min_inodes = avefreei - inodes_per_group / 4;
  304. min_blocks = avefreeb - EXT2_BLOCKS_PER_GROUP(sb) / 4;
  305. max_debt = EXT2_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
  306. if (max_debt * INODE_COST > inodes_per_group)
  307. max_debt = inodes_per_group / INODE_COST;
  308. if (max_debt > 255)
  309. max_debt = 255;
  310. if (max_debt == 0)
  311. max_debt = 1;
  312. for (i = 0; i < ngroups; i++) {
  313. group = (parent_group + i) % ngroups;
  314. desc = ext2_get_group_desc (sb, group, &bh);
  315. if (!desc || !desc->bg_free_inodes_count)
  316. continue;
  317. if (sbi->s_debts[group] >= max_debt)
  318. continue;
  319. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  320. continue;
  321. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  322. continue;
  323. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  324. continue;
  325. goto found;
  326. }
  327. fallback:
  328. for (i = 0; i < ngroups; i++) {
  329. group = (parent_group + i) % ngroups;
  330. desc = ext2_get_group_desc (sb, group, &bh);
  331. if (!desc || !desc->bg_free_inodes_count)
  332. continue;
  333. if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  334. goto found;
  335. }
  336. if (avefreei) {
  337. /*
  338. * The free-inodes counter is approximate, and for really small
  339. * filesystems the above test can fail to find any blockgroups
  340. */
  341. avefreei = 0;
  342. goto fallback;
  343. }
  344. return -1;
  345. found:
  346. return group;
  347. }
  348. static int find_group_other(struct super_block *sb, struct inode *parent)
  349. {
  350. int parent_group = EXT2_I(parent)->i_block_group;
  351. int ngroups = EXT2_SB(sb)->s_groups_count;
  352. struct ext2_group_desc *desc;
  353. struct buffer_head *bh;
  354. int group, i;
  355. /*
  356. * Try to place the inode in its parent directory
  357. */
  358. group = parent_group;
  359. desc = ext2_get_group_desc (sb, group, &bh);
  360. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  361. le16_to_cpu(desc->bg_free_blocks_count))
  362. goto found;
  363. /*
  364. * We're going to place this inode in a different blockgroup from its
  365. * parent. We want to cause files in a common directory to all land in
  366. * the same blockgroup. But we want files which are in a different
  367. * directory which shares a blockgroup with our parent to land in a
  368. * different blockgroup.
  369. *
  370. * So add our directory's i_ino into the starting point for the hash.
  371. */
  372. group = (group + parent->i_ino) % ngroups;
  373. /*
  374. * Use a quadratic hash to find a group with a free inode and some
  375. * free blocks.
  376. */
  377. for (i = 1; i < ngroups; i <<= 1) {
  378. group += i;
  379. if (group >= ngroups)
  380. group -= ngroups;
  381. desc = ext2_get_group_desc (sb, group, &bh);
  382. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  383. le16_to_cpu(desc->bg_free_blocks_count))
  384. goto found;
  385. }
  386. /*
  387. * That failed: try linear search for a free inode, even if that group
  388. * has no free blocks.
  389. */
  390. group = parent_group;
  391. for (i = 0; i < ngroups; i++) {
  392. if (++group >= ngroups)
  393. group = 0;
  394. desc = ext2_get_group_desc (sb, group, &bh);
  395. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  396. goto found;
  397. }
  398. return -1;
  399. found:
  400. return group;
  401. }
  402. struct inode *ext2_new_inode(struct inode *dir, int mode)
  403. {
  404. struct super_block *sb;
  405. struct buffer_head *bitmap_bh = NULL;
  406. struct buffer_head *bh2;
  407. int group, i;
  408. ino_t ino = 0;
  409. struct inode * inode;
  410. struct ext2_group_desc *gdp;
  411. struct ext2_super_block *es;
  412. struct ext2_inode_info *ei;
  413. struct ext2_sb_info *sbi;
  414. int err;
  415. sb = dir->i_sb;
  416. inode = new_inode(sb);
  417. if (!inode)
  418. return ERR_PTR(-ENOMEM);
  419. ei = EXT2_I(inode);
  420. sbi = EXT2_SB(sb);
  421. es = sbi->s_es;
  422. if (S_ISDIR(mode)) {
  423. if (test_opt(sb, OLDALLOC))
  424. group = find_group_dir(sb, dir);
  425. else
  426. group = find_group_orlov(sb, dir);
  427. } else
  428. group = find_group_other(sb, dir);
  429. if (group == -1) {
  430. err = -ENOSPC;
  431. goto fail;
  432. }
  433. for (i = 0; i < sbi->s_groups_count; i++) {
  434. gdp = ext2_get_group_desc(sb, group, &bh2);
  435. brelse(bitmap_bh);
  436. bitmap_bh = read_inode_bitmap(sb, group);
  437. if (!bitmap_bh) {
  438. err = -EIO;
  439. goto fail;
  440. }
  441. ino = 0;
  442. repeat_in_this_group:
  443. ino = ext2_find_next_zero_bit((unsigned long *)bitmap_bh->b_data,
  444. EXT2_INODES_PER_GROUP(sb), ino);
  445. if (ino >= EXT2_INODES_PER_GROUP(sb)) {
  446. /*
  447. * Rare race: find_group_xx() decided that there were
  448. * free inodes in this group, but by the time we tried
  449. * to allocate one, they're all gone. This can also
  450. * occur because the counters which find_group_orlov()
  451. * uses are approximate. So just go and search the
  452. * next block group.
  453. */
  454. if (++group == sbi->s_groups_count)
  455. group = 0;
  456. continue;
  457. }
  458. if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group),
  459. ino, bitmap_bh->b_data)) {
  460. /* we lost this inode */
  461. if (++ino >= EXT2_INODES_PER_GROUP(sb)) {
  462. /* this group is exhausted, try next group */
  463. if (++group == sbi->s_groups_count)
  464. group = 0;
  465. continue;
  466. }
  467. /* try to find free inode in the same group */
  468. goto repeat_in_this_group;
  469. }
  470. goto got;
  471. }
  472. /*
  473. * Scanned all blockgroups.
  474. */
  475. err = -ENOSPC;
  476. goto fail;
  477. got:
  478. mark_buffer_dirty(bitmap_bh);
  479. if (sb->s_flags & MS_SYNCHRONOUS)
  480. sync_dirty_buffer(bitmap_bh);
  481. brelse(bitmap_bh);
  482. ino += group * EXT2_INODES_PER_GROUP(sb) + 1;
  483. if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  484. ext2_error (sb, "ext2_new_inode",
  485. "reserved inode or inode > inodes count - "
  486. "block_group = %d,inode=%lu", group,
  487. (unsigned long) ino);
  488. err = -EIO;
  489. goto fail;
  490. }
  491. percpu_counter_mod(&sbi->s_freeinodes_counter, -1);
  492. if (S_ISDIR(mode))
  493. percpu_counter_inc(&sbi->s_dirs_counter);
  494. spin_lock(sb_bgl_lock(sbi, group));
  495. gdp->bg_free_inodes_count =
  496. cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
  497. if (S_ISDIR(mode)) {
  498. if (sbi->s_debts[group] < 255)
  499. sbi->s_debts[group]++;
  500. gdp->bg_used_dirs_count =
  501. cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
  502. } else {
  503. if (sbi->s_debts[group])
  504. sbi->s_debts[group]--;
  505. }
  506. spin_unlock(sb_bgl_lock(sbi, group));
  507. sb->s_dirt = 1;
  508. mark_buffer_dirty(bh2);
  509. inode->i_uid = current->fsuid;
  510. if (test_opt (sb, GRPID))
  511. inode->i_gid = dir->i_gid;
  512. else if (dir->i_mode & S_ISGID) {
  513. inode->i_gid = dir->i_gid;
  514. if (S_ISDIR(mode))
  515. mode |= S_ISGID;
  516. } else
  517. inode->i_gid = current->fsgid;
  518. inode->i_mode = mode;
  519. inode->i_ino = ino;
  520. inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
  521. inode->i_blocks = 0;
  522. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  523. memset(ei->i_data, 0, sizeof(ei->i_data));
  524. ei->i_flags = EXT2_I(dir)->i_flags & ~EXT2_BTREE_FL;
  525. if (S_ISLNK(mode))
  526. ei->i_flags &= ~(EXT2_IMMUTABLE_FL|EXT2_APPEND_FL);
  527. /* dirsync is only applied to directories */
  528. if (!S_ISDIR(mode))
  529. ei->i_flags &= ~EXT2_DIRSYNC_FL;
  530. ei->i_faddr = 0;
  531. ei->i_frag_no = 0;
  532. ei->i_frag_size = 0;
  533. ei->i_file_acl = 0;
  534. ei->i_dir_acl = 0;
  535. ei->i_dtime = 0;
  536. ei->i_block_group = group;
  537. ei->i_next_alloc_block = 0;
  538. ei->i_next_alloc_goal = 0;
  539. ei->i_prealloc_block = 0;
  540. ei->i_prealloc_count = 0;
  541. ei->i_dir_start_lookup = 0;
  542. ei->i_state = EXT2_STATE_NEW;
  543. ext2_set_inode_flags(inode);
  544. spin_lock(&sbi->s_next_gen_lock);
  545. inode->i_generation = sbi->s_next_generation++;
  546. spin_unlock(&sbi->s_next_gen_lock);
  547. insert_inode_hash(inode);
  548. if (DQUOT_ALLOC_INODE(inode)) {
  549. DQUOT_DROP(inode);
  550. err = -ENOSPC;
  551. goto fail2;
  552. }
  553. err = ext2_init_acl(inode, dir);
  554. if (err) {
  555. DQUOT_FREE_INODE(inode);
  556. DQUOT_DROP(inode);
  557. goto fail2;
  558. }
  559. err = ext2_init_security(inode,dir);
  560. if (err) {
  561. DQUOT_FREE_INODE(inode);
  562. goto fail2;
  563. }
  564. mark_inode_dirty(inode);
  565. ext2_debug("allocating inode %lu\n", inode->i_ino);
  566. ext2_preread_inode(inode);
  567. return inode;
  568. fail2:
  569. inode->i_flags |= S_NOQUOTA;
  570. inode->i_nlink = 0;
  571. iput(inode);
  572. return ERR_PTR(err);
  573. fail:
  574. make_bad_inode(inode);
  575. iput(inode);
  576. return ERR_PTR(err);
  577. }
  578. unsigned long ext2_count_free_inodes (struct super_block * sb)
  579. {
  580. struct ext2_group_desc *desc;
  581. unsigned long desc_count = 0;
  582. int i;
  583. #ifdef EXT2FS_DEBUG
  584. struct ext2_super_block *es;
  585. unsigned long bitmap_count = 0;
  586. struct buffer_head *bitmap_bh = NULL;
  587. lock_super (sb);
  588. es = EXT2_SB(sb)->s_es;
  589. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  590. unsigned x;
  591. desc = ext2_get_group_desc (sb, i, NULL);
  592. if (!desc)
  593. continue;
  594. desc_count += le16_to_cpu(desc->bg_free_inodes_count);
  595. brelse(bitmap_bh);
  596. bitmap_bh = read_inode_bitmap(sb, i);
  597. if (!bitmap_bh)
  598. continue;
  599. x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
  600. printk("group %d: stored = %d, counted = %u\n",
  601. i, le16_to_cpu(desc->bg_free_inodes_count), x);
  602. bitmap_count += x;
  603. }
  604. brelse(bitmap_bh);
  605. printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
  606. percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter),
  607. desc_count, bitmap_count);
  608. unlock_super(sb);
  609. return desc_count;
  610. #else
  611. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  612. desc = ext2_get_group_desc (sb, i, NULL);
  613. if (!desc)
  614. continue;
  615. desc_count += le16_to_cpu(desc->bg_free_inodes_count);
  616. }
  617. return desc_count;
  618. #endif
  619. }
  620. /* Called at mount-time, super-block is locked */
  621. unsigned long ext2_count_dirs (struct super_block * sb)
  622. {
  623. unsigned long count = 0;
  624. int i;
  625. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  626. struct ext2_group_desc *gdp = ext2_get_group_desc (sb, i, NULL);
  627. if (!gdp)
  628. continue;
  629. count += le16_to_cpu(gdp->bg_used_dirs_count);
  630. }
  631. return count;
  632. }
  633. #ifdef CONFIG_EXT2_CHECK
  634. /* Called at mount-time, super-block is locked */
  635. void ext2_check_inodes_bitmap (struct super_block * sb)
  636. {
  637. struct ext2_super_block * es = EXT2_SB(sb)->s_es;
  638. unsigned long desc_count = 0, bitmap_count = 0;
  639. struct buffer_head *bitmap_bh = NULL;
  640. int i;
  641. for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
  642. struct ext2_group_desc *desc;
  643. unsigned x;
  644. desc = ext2_get_group_desc(sb, i, NULL);
  645. if (!desc)
  646. continue;
  647. desc_count += le16_to_cpu(desc->bg_free_inodes_count);
  648. brelse(bitmap_bh);
  649. bitmap_bh = read_inode_bitmap(sb, i);
  650. if (!bitmap_bh)
  651. continue;
  652. x = ext2_count_free(bitmap_bh, EXT2_INODES_PER_GROUP(sb) / 8);
  653. if (le16_to_cpu(desc->bg_free_inodes_count) != x)
  654. ext2_error (sb, "ext2_check_inodes_bitmap",
  655. "Wrong free inodes count in group %d, "
  656. "stored = %d, counted = %lu", i,
  657. le16_to_cpu(desc->bg_free_inodes_count), x);
  658. bitmap_count += x;
  659. }
  660. brelse(bitmap_bh);
  661. if (percpu_counter_read(&EXT2_SB(sb)->s_freeinodes_counter) !=
  662. bitmap_count)
  663. ext2_error(sb, "ext2_check_inodes_bitmap",
  664. "Wrong free inodes count in super block, "
  665. "stored = %lu, counted = %lu",
  666. (unsigned long)le32_to_cpu(es->s_free_inodes_count),
  667. bitmap_count);
  668. }
  669. #endif