ialloc.c 19 KB

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