ialloc.c 21 KB

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