ialloc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:14k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/fs/ext2/ialloc.c
  3.  *
  4.  * Copyright (C) 1992, 1993, 1994, 1995
  5.  * Remy Card (card@masi.ibp.fr)
  6.  * Laboratoire MASI - Institut Blaise Pascal
  7.  * Universite Pierre et Marie Curie (Paris VI)
  8.  *
  9.  *  BSD ufs-inspired inode and directory allocation by 
  10.  *  Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
  11.  *  Big-endian to little-endian byte-swapping/bitmaps by
  12.  *        David S. Miller (davem@caip.rutgers.edu), 1995
  13.  */
  14. #include <linux/config.h>
  15. #include <linux/fs.h>
  16. #include <linux/ext2_fs.h>
  17. #include <linux/locks.h>
  18. #include <linux/quotaops.h>
  19. /*
  20.  * ialloc.c contains the inodes allocation and deallocation routines
  21.  */
  22. /*
  23.  * The free inodes are managed by bitmaps.  A file system contains several
  24.  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
  25.  * block for inodes, N blocks for the inode table and data blocks.
  26.  *
  27.  * The file system contains group descriptors which are located after the
  28.  * super block.  Each descriptor contains the number of the bitmap block and
  29.  * the free blocks count in the block.  The descriptors are loaded in memory
  30.  * when a file system is mounted (see ext2_read_super).
  31.  */
  32. /*
  33.  * Read the inode allocation bitmap for a given block_group, reading
  34.  * into the specified slot in the superblock's bitmap cache.
  35.  *
  36.  * Return buffer_head of bitmap on success or NULL.
  37.  */
  38. static struct buffer_head *read_inode_bitmap (struct super_block * sb,
  39.        unsigned long block_group)
  40. {
  41. struct ext2_group_desc *desc;
  42. struct buffer_head *bh = NULL;
  43. desc = ext2_get_group_desc(sb, block_group, NULL);
  44. if (!desc)
  45. goto error_out;
  46. bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
  47. if (!bh)
  48. ext2_error (sb, "read_inode_bitmap",
  49.     "Cannot read inode bitmap - "
  50.     "block_group = %lu, inode_bitmap = %lu",
  51.     block_group, (unsigned long) desc->bg_inode_bitmap);
  52. error_out:
  53. return bh;
  54. }
  55. /*
  56.  * load_inode_bitmap loads the inode bitmap for a blocks group
  57.  *
  58.  * It maintains a cache for the last bitmaps loaded.  This cache is managed
  59.  * with a LRU algorithm.
  60.  *
  61.  * Notes:
  62.  * 1/ There is one cache per mounted file system.
  63.  * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
  64.  *    this function reads the bitmap without maintaining a LRU cache.
  65.  * 
  66.  * Return the buffer_head of the bitmap or the ERR_PTR(error)
  67.  */
  68. static struct buffer_head *load_inode_bitmap (struct super_block * sb,
  69.       unsigned int block_group)
  70. {
  71. int i, slot = 0;
  72. struct ext2_sb_info *sbi = &sb->u.ext2_sb;
  73. struct buffer_head *bh = sbi->s_inode_bitmap[0];
  74. if (block_group >= sbi->s_groups_count)
  75. ext2_panic (sb, "load_inode_bitmap",
  76.     "block_group >= groups_count - "
  77.     "block_group = %d, groups_count = %lu",
  78.      block_group, sbi->s_groups_count);
  79. if (sbi->s_loaded_inode_bitmaps > 0 &&
  80.     sbi->s_inode_bitmap_number[0] == block_group && bh)
  81. goto found;
  82. if (sbi->s_groups_count <= EXT2_MAX_GROUP_LOADED) {
  83. slot = block_group;
  84. bh = sbi->s_inode_bitmap[slot];
  85. if (!bh)
  86. goto read_it;
  87. if (sbi->s_inode_bitmap_number[slot] == slot)
  88. goto found;
  89. ext2_panic (sb, "load_inode_bitmap",
  90.     "block_group != inode_bitmap_number");
  91. }
  92. bh = NULL;
  93. for (i = 0; i < sbi->s_loaded_inode_bitmaps &&
  94.     sbi->s_inode_bitmap_number[i] != block_group;
  95.      i++)
  96. ;
  97. if (i < sbi->s_loaded_inode_bitmaps)
  98. bh = sbi->s_inode_bitmap[i];
  99. else if (sbi->s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED)
  100. sbi->s_loaded_inode_bitmaps++;
  101. else
  102. brelse (sbi->s_inode_bitmap[--i]);
  103. while (i--) {
  104. sbi->s_inode_bitmap_number[i+1] = sbi->s_inode_bitmap_number[i];
  105. sbi->s_inode_bitmap[i+1] = sbi->s_inode_bitmap[i];
  106. }
  107. read_it:
  108. if (!bh)
  109. bh = read_inode_bitmap (sb, block_group);
  110. sbi->s_inode_bitmap_number[slot] = block_group;
  111. sbi->s_inode_bitmap[slot] = bh;
  112. if (!bh)
  113. return ERR_PTR(-EIO);
  114. found:
  115. return bh;
  116. }
  117. /*
  118.  * NOTE! When we get the inode, we're the only people
  119.  * that have access to it, and as such there are no
  120.  * race conditions we have to worry about. The inode
  121.  * is not on the hash-lists, and it cannot be reached
  122.  * through the filesystem because the directory entry
  123.  * has been deleted earlier.
  124.  *
  125.  * HOWEVER: we must make sure that we get no aliases,
  126.  * which means that we have to call "clear_inode()"
  127.  * _before_ we mark the inode not in use in the inode
  128.  * bitmaps. Otherwise a newly created file might use
  129.  * the same inode number (not actually the same pointer
  130.  * though), and then we'd have two inodes sharing the
  131.  * same inode number and space on the harddisk.
  132.  */
  133. void ext2_free_inode (struct inode * inode)
  134. {
  135. struct super_block * sb = inode->i_sb;
  136. int is_directory;
  137. unsigned long ino;
  138. struct buffer_head * bh;
  139. struct buffer_head * bh2;
  140. unsigned long block_group;
  141. unsigned long bit;
  142. struct ext2_group_desc * desc;
  143. struct ext2_super_block * es;
  144. ino = inode->i_ino;
  145. ext2_debug ("freeing inode %lun", ino);
  146. /*
  147.  * Note: we must free any quota before locking the superblock,
  148.  * as writing the quota to disk may need the lock as well.
  149.  */
  150. if (!is_bad_inode(inode)) {
  151. /* Quota is already initialized in iput() */
  152.      DQUOT_FREE_INODE(inode);
  153. DQUOT_DROP(inode);
  154. }
  155. lock_super (sb);
  156. es = sb->u.ext2_sb.s_es;
  157. is_directory = S_ISDIR(inode->i_mode);
  158. /* Do this BEFORE marking the inode not in use or returning an error */
  159. clear_inode (inode);
  160. if (ino < EXT2_FIRST_INO(sb) ||
  161.     ino > le32_to_cpu(es->s_inodes_count)) {
  162. ext2_error (sb, "ext2_free_inode",
  163.     "reserved or nonexistent inode %lu", ino);
  164. goto error_return;
  165. }
  166. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  167. bit = (ino - 1) % EXT2_INODES_PER_GROUP(sb);
  168. bh = load_inode_bitmap (sb, block_group);
  169. if (IS_ERR(bh))
  170. goto error_return;
  171. /* Ok, now we can actually update the inode bitmaps.. */
  172. if (!ext2_clear_bit (bit, bh->b_data))
  173. ext2_error (sb, "ext2_free_inode",
  174.       "bit already cleared for inode %lu", ino);
  175. else {
  176. desc = ext2_get_group_desc (sb, block_group, &bh2);
  177. if (desc) {
  178. desc->bg_free_inodes_count =
  179. cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
  180. if (is_directory)
  181. desc->bg_used_dirs_count =
  182. cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
  183. }
  184. mark_buffer_dirty(bh2);
  185. es->s_free_inodes_count =
  186. cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) + 1);
  187. mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
  188. }
  189. mark_buffer_dirty(bh);
  190. if (sb->s_flags & MS_SYNCHRONOUS) {
  191. ll_rw_block (WRITE, 1, &bh);
  192. wait_on_buffer (bh);
  193. }
  194. sb->s_dirt = 1;
  195. error_return:
  196. unlock_super (sb);
  197. }
  198. /*
  199.  * There are two policies for allocating an inode.  If the new inode is
  200.  * a directory, then a forward search is made for a block group with both
  201.  * free space and a low directory-to-inode ratio; if that fails, then of
  202.  * the groups with above-average free space, that group with the fewest
  203.  * directories already is chosen.
  204.  *
  205.  * For other inodes, search forward from the parent directory's block
  206.  * group to find a free inode.
  207.  */
  208. static int find_group_dir(struct super_block *sb, int parent_group)
  209. {
  210. struct ext2_super_block * es = sb->u.ext2_sb.s_es;
  211. int ngroups = sb->u.ext2_sb.s_groups_count;
  212. int avefreei = le32_to_cpu(es->s_free_inodes_count) / ngroups;
  213. struct ext2_group_desc *desc, *best_desc = NULL;
  214. struct buffer_head *bh, *best_bh = NULL;
  215. int group, best_group = -1;
  216. for (group = 0; group < ngroups; group++) {
  217. desc = ext2_get_group_desc (sb, group, &bh);
  218. if (!desc || !desc->bg_free_inodes_count)
  219. continue;
  220. if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
  221. continue;
  222. if (!best_desc || 
  223.     (le16_to_cpu(desc->bg_free_blocks_count) >
  224.      le16_to_cpu(best_desc->bg_free_blocks_count))) {
  225. best_group = group;
  226. best_desc = desc;
  227. best_bh = bh;
  228. }
  229. }
  230. if (!best_desc)
  231. return -1;
  232. best_desc->bg_free_inodes_count =
  233. cpu_to_le16(le16_to_cpu(best_desc->bg_free_inodes_count) - 1);
  234. best_desc->bg_used_dirs_count =
  235. cpu_to_le16(le16_to_cpu(best_desc->bg_used_dirs_count) + 1);
  236. mark_buffer_dirty(best_bh);
  237. return best_group;
  238. }
  239. static int find_group_other(struct super_block *sb, int parent_group)
  240. {
  241. int ngroups = sb->u.ext2_sb.s_groups_count;
  242. struct ext2_group_desc *desc;
  243. struct buffer_head *bh;
  244. int group, i;
  245. /*
  246.  * Try to place the inode in its parent directory
  247.  */
  248. group = parent_group;
  249. desc = ext2_get_group_desc (sb, group, &bh);
  250. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  251. goto found;
  252. /*
  253.  * Use a quadratic hash to find a group with a
  254.  * free inode
  255.  */
  256. for (i = 1; i < ngroups; i <<= 1) {
  257. group += i;
  258. if (group >= ngroups)
  259. group -= ngroups;
  260. desc = ext2_get_group_desc (sb, group, &bh);
  261. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  262. goto found;
  263. }
  264. /*
  265.  * That failed: try linear search for a free inode
  266.  */
  267. group = parent_group + 1;
  268. for (i = 2; i < ngroups; i++) {
  269. if (++group >= ngroups)
  270. group = 0;
  271. desc = ext2_get_group_desc (sb, group, &bh);
  272. if (desc && le16_to_cpu(desc->bg_free_inodes_count))
  273. goto found;
  274. }
  275. return -1;
  276. found:
  277. desc->bg_free_inodes_count =
  278. cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) - 1);
  279. mark_buffer_dirty(bh);
  280. return group;
  281. }
  282. struct inode * ext2_new_inode (const struct inode * dir, int mode)
  283. {
  284. struct super_block * sb;
  285. struct buffer_head * bh;
  286. struct buffer_head * bh2;
  287. int group, i;
  288. ino_t ino;
  289. struct inode * inode;
  290. struct ext2_group_desc * desc;
  291. struct ext2_super_block * es;
  292. int err;
  293. sb = dir->i_sb;
  294. inode = new_inode(sb);
  295. if (!inode)
  296. return ERR_PTR(-ENOMEM);
  297. lock_super (sb);
  298. es = sb->u.ext2_sb.s_es;
  299. repeat:
  300. if (S_ISDIR(mode))
  301. group = find_group_dir(sb, dir->u.ext2_i.i_block_group);
  302. else 
  303. group = find_group_other(sb, dir->u.ext2_i.i_block_group);
  304. err = -ENOSPC;
  305. if (group == -1)
  306. goto fail;
  307. err = -EIO;
  308. bh = load_inode_bitmap (sb, group);
  309. if (IS_ERR(bh))
  310. goto fail2;
  311. i = ext2_find_first_zero_bit ((unsigned long *) bh->b_data,
  312.       EXT2_INODES_PER_GROUP(sb));
  313. if (i >= EXT2_INODES_PER_GROUP(sb))
  314. goto bad_count;
  315. ext2_set_bit (i, bh->b_data);
  316. mark_buffer_dirty(bh);
  317. if (sb->s_flags & MS_SYNCHRONOUS) {
  318. ll_rw_block (WRITE, 1, &bh);
  319. wait_on_buffer (bh);
  320. }
  321. ino = group * EXT2_INODES_PER_GROUP(sb) + i + 1;
  322. if (ino < EXT2_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
  323. ext2_error (sb, "ext2_new_inode",
  324.     "reserved inode or inode > inodes count - "
  325.     "block_group = %d,inode=%ld", group, ino);
  326. err = -EIO;
  327. goto fail2;
  328. }
  329. es->s_free_inodes_count =
  330. cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) - 1);
  331. mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
  332. sb->s_dirt = 1;
  333. inode->i_uid = current->fsuid;
  334. if (test_opt (sb, GRPID))
  335. inode->i_gid = dir->i_gid;
  336. else if (dir->i_mode & S_ISGID) {
  337. inode->i_gid = dir->i_gid;
  338. if (S_ISDIR(mode))
  339. mode |= S_ISGID;
  340. } else
  341. inode->i_gid = current->fsgid;
  342. inode->i_mode = mode;
  343. inode->i_ino = ino;
  344. inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
  345. inode->i_blocks = 0;
  346. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  347. inode->u.ext2_i.i_new_inode = 1;
  348. inode->u.ext2_i.i_flags = dir->u.ext2_i.i_flags & ~EXT2_BTREE_FL;
  349. if (S_ISLNK(mode))
  350. inode->u.ext2_i.i_flags &= ~(EXT2_IMMUTABLE_FL|EXT2_APPEND_FL);
  351. inode->u.ext2_i.i_block_group = group;
  352. if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL)
  353. inode->i_flags |= S_SYNC;
  354. insert_inode_hash(inode);
  355. inode->i_generation = event++;
  356. mark_inode_dirty(inode);
  357. unlock_super (sb);
  358. if(DQUOT_ALLOC_INODE(inode)) {
  359. DQUOT_DROP(inode);
  360. inode->i_flags |= S_NOQUOTA;
  361. inode->i_nlink = 0;
  362. iput(inode);
  363. return ERR_PTR(-EDQUOT);
  364. }
  365. ext2_debug ("allocating inode %lun", inode->i_ino);
  366. return inode;
  367. fail2:
  368. desc = ext2_get_group_desc (sb, group, &bh2);
  369. desc->bg_free_inodes_count =
  370. cpu_to_le16(le16_to_cpu(desc->bg_free_inodes_count) + 1);
  371. if (S_ISDIR(mode))
  372. desc->bg_used_dirs_count =
  373. cpu_to_le16(le16_to_cpu(desc->bg_used_dirs_count) - 1);
  374. mark_buffer_dirty(bh2);
  375. fail:
  376. unlock_super(sb);
  377. make_bad_inode(inode);
  378. iput(inode);
  379. return ERR_PTR(err);
  380. bad_count:
  381. ext2_error (sb, "ext2_new_inode",
  382.     "Free inodes count corrupted in group %d",
  383.     group);
  384. /* Is it really ENOSPC? */
  385. err = -ENOSPC;
  386. if (sb->s_flags & MS_RDONLY)
  387. goto fail;
  388. desc = ext2_get_group_desc (sb, group, &bh2);
  389. desc->bg_free_inodes_count = 0;
  390. mark_buffer_dirty(bh2);
  391. goto repeat;
  392. }
  393. unsigned long ext2_count_free_inodes (struct super_block * sb)
  394. {
  395. #ifdef EXT2FS_DEBUG
  396. struct ext2_super_block * es;
  397. unsigned long desc_count = 0, bitmap_count = 0;
  398. int i;
  399. lock_super (sb);
  400. es = sb->u.ext2_sb.s_es;
  401. for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
  402. struct ext2_group_desc *desc = ext2_get_group_desc (sb, i, NULL);
  403. struct buffer_head *bh;
  404. unsigned x;
  405. if (!desc)
  406. continue;
  407. desc_count += le16_to_cpu(desc->bg_free_inodes_count);
  408. bh = load_inode_bitmap (sb, i);
  409. if (IS_ERR(bh))
  410. continue;
  411. x = ext2_count_free (bh, EXT2_INODES_PER_GROUP(sb) / 8);
  412. printk ("group %d: stored = %d, counted = %lun",
  413. i, le16_to_cpu(desc->bg_free_inodes_count), x);
  414. bitmap_count += x;
  415. }
  416. printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lun",
  417. le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
  418. unlock_super (sb);
  419. return desc_count;
  420. #else
  421. return le32_to_cpu(sb->u.ext2_sb.s_es->s_free_inodes_count);
  422. #endif
  423. }
  424. #ifdef CONFIG_EXT2_CHECK
  425. /* Called at mount-time, super-block is locked */
  426. void ext2_check_inodes_bitmap (struct super_block * sb)
  427. {
  428. struct ext2_super_block * es = sb->u.ext2_sb.s_es;
  429. unsigned long desc_count = 0, bitmap_count = 0;
  430. int i;
  431. for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
  432. struct ext2_group_desc *desc = ext2_get_group_desc(sb, i, NULL);
  433. struct buffer_head *bh;
  434. unsigned x;
  435. if (!desc)
  436. continue;
  437. desc_count += le16_to_cpu(desc->bg_free_inodes_count);
  438. bh = load_inode_bitmap (sb, i);
  439. if (IS_ERR(bh))
  440. continue;
  441. x = ext2_count_free (bh, EXT2_INODES_PER_GROUP(sb) / 8);
  442. if (le16_to_cpu(desc->bg_free_inodes_count) != x)
  443. ext2_error (sb, "ext2_check_inodes_bitmap",
  444.     "Wrong free inodes count in group %d, "
  445.     "stored = %d, counted = %lu", i,
  446.     le16_to_cpu(desc->bg_free_inodes_count), x);
  447. bitmap_count += x;
  448. }
  449. if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
  450. ext2_error (sb, "ext2_check_inodes_bitmap",
  451.     "Wrong free inodes count in super block, "
  452.     "stored = %lu, counted = %lu",
  453.     (unsigned long)le32_to_cpu(es->s_free_inodes_count),
  454.     bitmap_count);
  455. }
  456. #endif