ialloc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. le16_add_cpu(&gdp->bg_free_inodes_count, 1);
  148. if (is_directory)
  149. le16_add_cpu(&gdp->bg_used_dirs_count, -1);
  150. spin_unlock(sb_bgl_lock(sbi, block_group));
  151. percpu_counter_inc(&sbi->s_freeinodes_counter);
  152. if (is_directory)
  153. percpu_counter_dec(&sbi->s_dirs_counter);
  154. }
  155. BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  156. err = ext3_journal_dirty_metadata(handle, bh2);
  157. if (!fatal) fatal = err;
  158. }
  159. BUFFER_TRACE(bitmap_bh, "call ext3_journal_dirty_metadata");
  160. err = ext3_journal_dirty_metadata(handle, bitmap_bh);
  161. if (!fatal)
  162. fatal = err;
  163. sb->s_dirt = 1;
  164. error_return:
  165. brelse(bitmap_bh);
  166. ext3_std_error(sb, fatal);
  167. }
  168. /*
  169. * There are two policies for allocating an inode. If the new inode is
  170. * a directory, then a forward search is made for a block group with both
  171. * free space and a low directory-to-inode ratio; if that fails, then of
  172. * the groups with above-average free space, that group with the fewest
  173. * directories already is chosen.
  174. *
  175. * For other inodes, search forward from the parent directory\'s block
  176. * group to find a free inode.
  177. */
  178. static int find_group_dir(struct super_block *sb, struct inode *parent)
  179. {
  180. int ngroups = EXT3_SB(sb)->s_groups_count;
  181. unsigned int freei, avefreei;
  182. struct ext3_group_desc *desc, *best_desc = NULL;
  183. int group, best_group = -1;
  184. freei = percpu_counter_read_positive(&EXT3_SB(sb)->s_freeinodes_counter);
  185. avefreei = freei / ngroups;
  186. for (group = 0; group < ngroups; group++) {
  187. desc = ext3_get_group_desc (sb, group, NULL);
  188. if (!desc || !desc->bg_free_inodes_count)
  189. continue;
  190. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  191. continue;
  192. if (!best_desc ||
  193. (le16_to_cpu(desc->bg_free_blocks_count) >
  194. le16_to_cpu(best_desc->bg_free_blocks_count))) {
  195. best_group = group;
  196. best_desc = desc;
  197. }
  198. }
  199. return best_group;
  200. }
  201. /*
  202. * Orlov's allocator for directories.
  203. *
  204. * We always try to spread first-level directories.
  205. *
  206. * If there are blockgroups with both free inodes and free blocks counts
  207. * not worse than average we return one with smallest directory count.
  208. * Otherwise we simply return a random group.
  209. *
  210. * For the rest rules look so:
  211. *
  212. * It's OK to put directory into a group unless
  213. * it has too many directories already (max_dirs) or
  214. * it has too few free inodes left (min_inodes) or
  215. * it has too few free blocks left (min_blocks) or
  216. * it's already running too large debt (max_debt).
  217. * Parent's group is preferred, if it doesn't satisfy these
  218. * conditions we search cyclically through the rest. If none
  219. * of the groups look good we just look for a group with more
  220. * free inodes than average (starting at parent's group).
  221. *
  222. * Debt is incremented each time we allocate a directory and decremented
  223. * when we allocate an inode, within 0--255.
  224. */
  225. #define INODE_COST 64
  226. #define BLOCK_COST 256
  227. static int find_group_orlov(struct super_block *sb, struct inode *parent)
  228. {
  229. int parent_group = EXT3_I(parent)->i_block_group;
  230. struct ext3_sb_info *sbi = EXT3_SB(sb);
  231. struct ext3_super_block *es = sbi->s_es;
  232. int ngroups = sbi->s_groups_count;
  233. int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
  234. unsigned int freei, avefreei;
  235. ext3_fsblk_t freeb, avefreeb;
  236. ext3_fsblk_t blocks_per_dir;
  237. unsigned int ndirs;
  238. int max_debt, max_dirs, min_inodes;
  239. ext3_grpblk_t min_blocks;
  240. int group = -1, i;
  241. struct ext3_group_desc *desc;
  242. freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
  243. avefreei = freei / ngroups;
  244. freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  245. avefreeb = freeb / ngroups;
  246. ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
  247. if ((parent == sb->s_root->d_inode) ||
  248. (EXT3_I(parent)->i_flags & EXT3_TOPDIR_FL)) {
  249. int best_ndir = inodes_per_group;
  250. int best_group = -1;
  251. get_random_bytes(&group, sizeof(group));
  252. parent_group = (unsigned)group % ngroups;
  253. for (i = 0; i < ngroups; i++) {
  254. group = (parent_group + i) % ngroups;
  255. desc = ext3_get_group_desc (sb, group, NULL);
  256. if (!desc || !desc->bg_free_inodes_count)
  257. continue;
  258. if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
  259. continue;
  260. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  261. continue;
  262. if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
  263. continue;
  264. best_group = group;
  265. best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
  266. }
  267. if (best_group >= 0)
  268. return best_group;
  269. goto fallback;
  270. }
  271. blocks_per_dir = (le32_to_cpu(es->s_blocks_count) - freeb) / ndirs;
  272. max_dirs = ndirs / ngroups + inodes_per_group / 16;
  273. min_inodes = avefreei - inodes_per_group / 4;
  274. min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;
  275. max_debt = EXT3_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, (ext3_fsblk_t)BLOCK_COST);
  276. if (max_debt * INODE_COST > inodes_per_group)
  277. max_debt = inodes_per_group / INODE_COST;
  278. if (max_debt > 255)
  279. max_debt = 255;
  280. if (max_debt == 0)
  281. max_debt = 1;
  282. for (i = 0; i < ngroups; i++) {
  283. group = (parent_group + i) % ngroups;
  284. desc = ext3_get_group_desc (sb, group, NULL);
  285. if (!desc || !desc->bg_free_inodes_count)
  286. continue;
  287. if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
  288. continue;
  289. if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
  290. continue;
  291. if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
  292. continue;
  293. return group;
  294. }
  295. fallback:
  296. for (i = 0; i < ngroups; i++) {
  297. group = (parent_group + i) % ngroups;
  298. desc = ext3_get_group_desc (sb, group, NULL);
  299. if (!desc || !desc->bg_free_inodes_count)
  300. continue;
  301. if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
  302. return group;
  303. }
  304. if (avefreei) {
  305. /*
  306. * The free-inodes counter is approximate, and for really small
  307. * filesystems the above test can fail to find any blockgroups
  308. */
  309. avefreei = 0;
  310. goto fallback;
  311. }
  312. return -1;
  313. }
  314. static int find_group_other(struct super_block *sb, struct inode *parent)
  315. {
  316. int parent_group = EXT3_I(parent)->i_block_group;
  317. int ngroups = EXT3_SB(sb)->s_groups_count;
  318. struct ext3_group_desc *desc;
  319. int group, i;
  320. /*
  321. * Try to place the inode in its parent directory
  322. */
  323. group = parent_group;
  324. desc = ext3_get_group_desc (sb, group, NULL);
  325. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  326. le16_to_cpu(desc->bg_free_blocks_count))
  327. return group;
  328. /*
  329. * We're going to place this inode in a different blockgroup from its
  330. * parent. We want to cause files in a common directory to all land in
  331. * the same blockgroup. But we want files which are in a different
  332. * directory which shares a blockgroup with our parent to land in a
  333. * different blockgroup.
  334. *
  335. * So add our directory's i_ino into the starting point for the hash.
  336. */
  337. group = (group + parent->i_ino) % ngroups;
  338. /*
  339. * Use a quadratic hash to find a group with a free inode and some free
  340. * blocks.
  341. */
  342. for (i = 1; i < ngroups; i <<= 1) {
  343. group += i;
  344. if (group >= ngroups)
  345. group -= ngroups;
  346. desc = ext3_get_group_desc (sb, group, NULL);
  347. if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
  348. le16_to_cpu(desc->bg_free_blocks_count))
  349. return group;
  350. }
  351. /*
  352. * That failed: try linear search for a free inode, even if that group
  353. * has no free blocks.
  354. */
  355. group = parent_group;
  356. for (i = 0; i < ngroups; i++) {
  357. if (++group >= ngroups)
  358. group = 0;
  359. desc = ext3_get_group_desc (sb, group, NULL);
  360. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  361. return group;
  362. }
  363. return -1;
  364. }
  365. /*
  366. * There are two policies for allocating an inode. If the new inode is
  367. * a directory, then a forward search is made for a block group with both
  368. * free space and a low directory-to-inode ratio; if that fails, then of
  369. * the groups with above-average free space, that group with the fewest
  370. * directories already is chosen.
  371. *
  372. * For other inodes, search forward from the parent directory's block
  373. * group to find a free inode.
  374. */
  375. struct inode *ext3_new_inode(handle_t *handle, struct inode * dir, int mode)
  376. {
  377. struct super_block *sb;
  378. struct buffer_head *bitmap_bh = NULL;
  379. struct buffer_head *bh2;
  380. int group;
  381. unsigned long ino = 0;
  382. struct inode * inode;
  383. struct ext3_group_desc * gdp = NULL;
  384. struct ext3_super_block * es;
  385. struct ext3_inode_info *ei;
  386. struct ext3_sb_info *sbi;
  387. int err = 0;
  388. struct inode *ret;
  389. int i;
  390. /* Cannot create files in a deleted directory */
  391. if (!dir || !dir->i_nlink)
  392. return ERR_PTR(-EPERM);
  393. sb = dir->i_sb;
  394. inode = new_inode(sb);
  395. if (!inode)
  396. return ERR_PTR(-ENOMEM);
  397. ei = EXT3_I(inode);
  398. sbi = EXT3_SB(sb);
  399. es = sbi->s_es;
  400. if (S_ISDIR(mode)) {
  401. if (test_opt (sb, OLDALLOC))
  402. group = find_group_dir(sb, dir);
  403. else
  404. group = find_group_orlov(sb, dir);
  405. } else
  406. group = find_group_other(sb, dir);
  407. err = -ENOSPC;
  408. if (group == -1)
  409. goto out;
  410. for (i = 0; i < sbi->s_groups_count; i++) {
  411. err = -EIO;
  412. gdp = ext3_get_group_desc(sb, group, &bh2);
  413. if (!gdp)
  414. goto fail;
  415. brelse(bitmap_bh);
  416. bitmap_bh = read_inode_bitmap(sb, group);
  417. if (!bitmap_bh)
  418. goto fail;
  419. ino = 0;
  420. repeat_in_this_group:
  421. ino = ext3_find_next_zero_bit((unsigned long *)
  422. bitmap_bh->b_data, EXT3_INODES_PER_GROUP(sb), ino);
  423. if (ino < EXT3_INODES_PER_GROUP(sb)) {
  424. BUFFER_TRACE(bitmap_bh, "get_write_access");
  425. err = ext3_journal_get_write_access(handle, bitmap_bh);
  426. if (err)
  427. goto fail;
  428. if (!ext3_set_bit_atomic(sb_bgl_lock(sbi, group),
  429. ino, bitmap_bh->b_data)) {
  430. /* we won it */
  431. BUFFER_TRACE(bitmap_bh,
  432. "call ext3_journal_dirty_metadata");
  433. err = ext3_journal_dirty_metadata(handle,
  434. bitmap_bh);
  435. if (err)
  436. goto fail;
  437. goto got;
  438. }
  439. /* we lost it */
  440. journal_release_buffer(handle, bitmap_bh);
  441. if (++ino < EXT3_INODES_PER_GROUP(sb))
  442. goto repeat_in_this_group;
  443. }
  444. /*
  445. * This case is possible in concurrent environment. It is very
  446. * rare. We cannot repeat the find_group_xxx() call because
  447. * that will simply return the same blockgroup, because the
  448. * group descriptor metadata has not yet been updated.
  449. * So we just go onto the next blockgroup.
  450. */
  451. if (++group == sbi->s_groups_count)
  452. group = 0;
  453. }
  454. err = -ENOSPC;
  455. goto out;
  456. got:
  457. ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
  458. if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  459. ext3_error (sb, "ext3_new_inode",
  460. "reserved inode or inode > inodes count - "
  461. "block_group = %d, inode=%lu", group, ino);
  462. err = -EIO;
  463. goto fail;
  464. }
  465. BUFFER_TRACE(bh2, "get_write_access");
  466. err = ext3_journal_get_write_access(handle, bh2);
  467. if (err) goto fail;
  468. spin_lock(sb_bgl_lock(sbi, group));
  469. le16_add_cpu(&gdp->bg_free_inodes_count, -1);
  470. if (S_ISDIR(mode)) {
  471. le16_add_cpu(&gdp->bg_used_dirs_count, 1);
  472. }
  473. spin_unlock(sb_bgl_lock(sbi, group));
  474. BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
  475. err = ext3_journal_dirty_metadata(handle, bh2);
  476. if (err) goto fail;
  477. percpu_counter_dec(&sbi->s_freeinodes_counter);
  478. if (S_ISDIR(mode))
  479. percpu_counter_inc(&sbi->s_dirs_counter);
  480. sb->s_dirt = 1;
  481. inode->i_uid = current_fsuid();
  482. if (test_opt (sb, GRPID))
  483. inode->i_gid = dir->i_gid;
  484. else if (dir->i_mode & S_ISGID) {
  485. inode->i_gid = dir->i_gid;
  486. if (S_ISDIR(mode))
  487. mode |= S_ISGID;
  488. } else
  489. inode->i_gid = current_fsgid();
  490. inode->i_mode = mode;
  491. inode->i_ino = ino;
  492. /* This is the optimal IO size (for stat), not the fs block size */
  493. inode->i_blocks = 0;
  494. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  495. memset(ei->i_data, 0, sizeof(ei->i_data));
  496. ei->i_dir_start_lookup = 0;
  497. ei->i_disksize = 0;
  498. ei->i_flags = EXT3_I(dir)->i_flags & ~EXT3_INDEX_FL;
  499. if (S_ISLNK(mode))
  500. ei->i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
  501. /* dirsync only applies to directories */
  502. if (!S_ISDIR(mode))
  503. ei->i_flags &= ~EXT3_DIRSYNC_FL;
  504. #ifdef EXT3_FRAGMENTS
  505. ei->i_faddr = 0;
  506. ei->i_frag_no = 0;
  507. ei->i_frag_size = 0;
  508. #endif
  509. ei->i_file_acl = 0;
  510. ei->i_dir_acl = 0;
  511. ei->i_dtime = 0;
  512. ei->i_block_alloc_info = NULL;
  513. ei->i_block_group = group;
  514. ext3_set_inode_flags(inode);
  515. if (IS_DIRSYNC(inode))
  516. handle->h_sync = 1;
  517. insert_inode_hash(inode);
  518. spin_lock(&sbi->s_next_gen_lock);
  519. inode->i_generation = sbi->s_next_generation++;
  520. spin_unlock(&sbi->s_next_gen_lock);
  521. ei->i_state = EXT3_STATE_NEW;
  522. ei->i_extra_isize =
  523. (EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) ?
  524. sizeof(struct ext3_inode) - EXT3_GOOD_OLD_INODE_SIZE : 0;
  525. ret = inode;
  526. if(DQUOT_ALLOC_INODE(inode)) {
  527. err = -EDQUOT;
  528. goto fail_drop;
  529. }
  530. err = ext3_init_acl(handle, inode, dir);
  531. if (err)
  532. goto fail_free_drop;
  533. err = ext3_init_security(handle,inode, dir);
  534. if (err)
  535. goto fail_free_drop;
  536. err = ext3_mark_inode_dirty(handle, inode);
  537. if (err) {
  538. ext3_std_error(sb, err);
  539. goto fail_free_drop;
  540. }
  541. ext3_debug("allocating inode %lu\n", inode->i_ino);
  542. goto really_out;
  543. fail:
  544. ext3_std_error(sb, err);
  545. out:
  546. iput(inode);
  547. ret = ERR_PTR(err);
  548. really_out:
  549. brelse(bitmap_bh);
  550. return ret;
  551. fail_free_drop:
  552. DQUOT_FREE_INODE(inode);
  553. fail_drop:
  554. DQUOT_DROP(inode);
  555. inode->i_flags |= S_NOQUOTA;
  556. inode->i_nlink = 0;
  557. iput(inode);
  558. brelse(bitmap_bh);
  559. return ERR_PTR(err);
  560. }
  561. /* Verify that we are loading a valid orphan from disk */
  562. struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
  563. {
  564. unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
  565. unsigned long block_group;
  566. int bit;
  567. struct buffer_head *bitmap_bh;
  568. struct inode *inode = NULL;
  569. long err = -EIO;
  570. /* Error cases - e2fsck has already cleaned up for us */
  571. if (ino > max_ino) {
  572. ext3_warning(sb, __func__,
  573. "bad orphan ino %lu! e2fsck was run?", ino);
  574. goto error;
  575. }
  576. block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
  577. bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
  578. bitmap_bh = read_inode_bitmap(sb, block_group);
  579. if (!bitmap_bh) {
  580. ext3_warning(sb, __func__,
  581. "inode bitmap error for orphan %lu", ino);
  582. goto error;
  583. }
  584. /* Having the inode bit set should be a 100% indicator that this
  585. * is a valid orphan (no e2fsck run on fs). Orphans also include
  586. * inodes that were being truncated, so we can't check i_nlink==0.
  587. */
  588. if (!ext3_test_bit(bit, bitmap_bh->b_data))
  589. goto bad_orphan;
  590. inode = ext3_iget(sb, ino);
  591. if (IS_ERR(inode))
  592. goto iget_failed;
  593. /*
  594. * If the orphans has i_nlinks > 0 then it should be able to be
  595. * truncated, otherwise it won't be removed from the orphan list
  596. * during processing and an infinite loop will result.
  597. */
  598. if (inode->i_nlink && !ext3_can_truncate(inode))
  599. goto bad_orphan;
  600. if (NEXT_ORPHAN(inode) > max_ino)
  601. goto bad_orphan;
  602. brelse(bitmap_bh);
  603. return inode;
  604. iget_failed:
  605. err = PTR_ERR(inode);
  606. inode = NULL;
  607. bad_orphan:
  608. ext3_warning(sb, __func__,
  609. "bad orphan inode %lu! e2fsck was run?", ino);
  610. printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%llu) = %d\n",
  611. bit, (unsigned long long)bitmap_bh->b_blocknr,
  612. ext3_test_bit(bit, bitmap_bh->b_data));
  613. printk(KERN_NOTICE "inode=%p\n", inode);
  614. if (inode) {
  615. printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
  616. is_bad_inode(inode));
  617. printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
  618. NEXT_ORPHAN(inode));
  619. printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
  620. printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
  621. /* Avoid freeing blocks if we got a bad deleted inode */
  622. if (inode->i_nlink == 0)
  623. inode->i_blocks = 0;
  624. iput(inode);
  625. }
  626. brelse(bitmap_bh);
  627. error:
  628. return ERR_PTR(err);
  629. }
  630. unsigned long ext3_count_free_inodes (struct super_block * sb)
  631. {
  632. unsigned long desc_count;
  633. struct ext3_group_desc *gdp;
  634. int i;
  635. #ifdef EXT3FS_DEBUG
  636. struct ext3_super_block *es;
  637. unsigned long bitmap_count, x;
  638. struct buffer_head *bitmap_bh = NULL;
  639. es = EXT3_SB(sb)->s_es;
  640. desc_count = 0;
  641. bitmap_count = 0;
  642. gdp = NULL;
  643. for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
  644. gdp = ext3_get_group_desc (sb, i, NULL);
  645. if (!gdp)
  646. continue;
  647. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  648. brelse(bitmap_bh);
  649. bitmap_bh = read_inode_bitmap(sb, i);
  650. if (!bitmap_bh)
  651. continue;
  652. x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
  653. printk("group %d: stored = %d, counted = %lu\n",
  654. i, le16_to_cpu(gdp->bg_free_inodes_count), x);
  655. bitmap_count += x;
  656. }
  657. brelse(bitmap_bh);
  658. printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
  659. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  660. return desc_count;
  661. #else
  662. desc_count = 0;
  663. for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
  664. gdp = ext3_get_group_desc (sb, i, NULL);
  665. if (!gdp)
  666. continue;
  667. desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
  668. cond_resched();
  669. }
  670. return desc_count;
  671. #endif
  672. }
  673. /* Called at mount-time, super-block is locked */
  674. unsigned long ext3_count_dirs (struct super_block * sb)
  675. {
  676. unsigned long count = 0;
  677. int i;
  678. for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
  679. struct ext3_group_desc *gdp = ext3_get_group_desc (sb, i, NULL);
  680. if (!gdp)
  681. continue;
  682. count += le16_to_cpu(gdp->bg_used_dirs_count);
  683. }
  684. return count;
  685. }