ialloc.c 21 KB

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