ialloc.c 21 KB

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