📄 inode.c
字号:
/* * linux/fs/ext2/inode.c * * Copyright (C) 1992, 1993, 1994, 1995 * Remy Card (card@masi.ibp.fr) * Laboratoire MASI - Institut Blaise Pascal * Universite Pierre et Marie Curie (Paris VI) * * from * * linux/fs/minix/inode.c * * Copyright (C) 1991, 1992 Linus Torvalds * * Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 */#include <asm/segment.h>#include <asm/system.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/ext2_fs.h>#include <linux/sched.h>#include <linux/stat.h>#include <linux/string.h>#include <linux/locks.h>#include <linux/mm.h>static int ext2_update_inode(struct inode * inode, int do_sync);void ext2_put_inode (struct inode * inode){ ext2_discard_prealloc (inode); if (inode->i_nlink || inode->i_ino == EXT2_ACL_IDX_INO || inode->i_ino == EXT2_ACL_DATA_INO) return; inode->u.ext2_i.i_dtime = CURRENT_TIME; inode->i_dirt = 1; ext2_update_inode(inode, IS_SYNC(inode)); inode->i_size = 0; if (inode->i_blocks) ext2_truncate (inode); ext2_free_inode (inode);}#define inode_bmap(inode, nr) ((inode)->u.ext2_i.i_data[(nr)])static inline int block_bmap (struct buffer_head * bh, int nr){ int tmp; if (!bh) return 0; tmp = ((u32 *) bh->b_data)[nr]; brelse (bh); return tmp;}/* * ext2_discard_prealloc and ext2_alloc_block are atomic wrt. the * superblock in the same manner as are ext2_free_blocks and * ext2_new_block. We just wait on the super rather than locking it * here, since ext2_new_block will do the necessary locking and we * can't block until then. */void ext2_discard_prealloc (struct inode * inode){#ifdef EXT2_PREALLOCATE unsigned short total; if (inode->u.ext2_i.i_prealloc_count) { total = inode->u.ext2_i.i_prealloc_count; inode->u.ext2_i.i_prealloc_count = 0; ext2_free_blocks (inode, inode->u.ext2_i.i_prealloc_block, total); }#endif}static int ext2_alloc_block (struct inode * inode, unsigned long goal, int * err){#ifdef EXT2FS_DEBUG static unsigned long alloc_hits = 0, alloc_attempts = 0;#endif unsigned long result; struct buffer_head * bh; wait_on_super (inode->i_sb);#ifdef EXT2_PREALLOCATE if (inode->u.ext2_i.i_prealloc_count && (goal == inode->u.ext2_i.i_prealloc_block || goal + 1 == inode->u.ext2_i.i_prealloc_block)) { result = inode->u.ext2_i.i_prealloc_block++; inode->u.ext2_i.i_prealloc_count--; ext2_debug ("preallocation hit (%lu/%lu).\n", ++alloc_hits, ++alloc_attempts); /* It doesn't matter if we block in getblk() since we have already atomically allocated the block, and are only clearing it now. */ if (!(bh = getblk (inode->i_sb->s_dev, result, inode->i_sb->s_blocksize))) { ext2_error (inode->i_sb, "ext2_alloc_block", "cannot get block %lu", result); return 0; } memset(bh->b_data, 0, inode->i_sb->s_blocksize); mark_buffer_uptodate(bh, 1); mark_buffer_dirty(bh, 1); brelse (bh); } else { ext2_discard_prealloc (inode); ext2_debug ("preallocation miss (%lu/%lu).\n", alloc_hits, ++alloc_attempts); if (S_ISREG(inode->i_mode)) result = ext2_new_block (inode, goal, &inode->u.ext2_i.i_prealloc_count, &inode->u.ext2_i.i_prealloc_block, err); else result = ext2_new_block (inode, goal, 0, 0, err); }#else result = ext2_new_block (inode, goal, 0, 0, err);#endif return result;}int ext2_bmap (struct inode * inode, int block){ int i; int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb); int addr_per_block_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb); if (block < 0) { ext2_warning (inode->i_sb, "ext2_bmap", "block < 0"); return 0; } if (block >= EXT2_NDIR_BLOCKS + addr_per_block + (1 << (addr_per_block_bits * 2)) + ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) { ext2_warning (inode->i_sb, "ext2_bmap", "block > big"); return 0; } if (block < EXT2_NDIR_BLOCKS) return inode_bmap (inode, block); block -= EXT2_NDIR_BLOCKS; if (block < addr_per_block) { i = inode_bmap (inode, EXT2_IND_BLOCK); if (!i) return 0; return block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize), block); } block -= addr_per_block; if (block < (1 << (addr_per_block_bits * 2))) { i = inode_bmap (inode, EXT2_DIND_BLOCK); if (!i) return 0; i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize), block >> addr_per_block_bits); if (!i) return 0; return block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize), block & (addr_per_block - 1)); } block -= (1 << (addr_per_block_bits * 2)); i = inode_bmap (inode, EXT2_TIND_BLOCK); if (!i) return 0; i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize), block >> (addr_per_block_bits * 2)); if (!i) return 0; i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize), (block >> addr_per_block_bits) & (addr_per_block - 1)); if (!i) return 0; return block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize), block & (addr_per_block - 1));}static struct buffer_head * inode_getblk (struct inode * inode, int nr, int create, int new_block, int * err){ u32 * p; int tmp, goal = 0; struct buffer_head * result; int blocks = inode->i_sb->s_blocksize / 512; p = inode->u.ext2_i.i_data + nr;repeat: tmp = *p; if (tmp) { result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize); if (tmp == *p) return result; brelse (result); goto repeat; } if (!create || new_block >= (current->rlim[RLIMIT_FSIZE].rlim_cur >> EXT2_BLOCK_SIZE_BITS(inode->i_sb))) { *err = -EFBIG; return NULL; } if (inode->u.ext2_i.i_next_alloc_block == new_block) goal = inode->u.ext2_i.i_next_alloc_goal; ext2_debug ("hint = %d,", goal); if (!goal) { for (tmp = nr - 1; tmp >= 0; tmp--) { if (inode->u.ext2_i.i_data[tmp]) { goal = inode->u.ext2_i.i_data[tmp]; break; } } if (!goal) goal = (inode->u.ext2_i.i_block_group * EXT2_BLOCKS_PER_GROUP(inode->i_sb)) + inode->i_sb->u.ext2_sb.s_es->s_first_data_block; } ext2_debug ("goal = %d.\n", goal); tmp = ext2_alloc_block (inode, goal, err); if (!tmp) return NULL; result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize); if (*p) { ext2_free_blocks (inode, tmp, 1); brelse (result); goto repeat; } *p = tmp; inode->u.ext2_i.i_next_alloc_block = new_block; inode->u.ext2_i.i_next_alloc_goal = tmp; inode->i_ctime = CURRENT_TIME; inode->i_blocks += blocks; if (IS_SYNC(inode) || inode->u.ext2_i.i_osync) ext2_sync_inode (inode); else inode->i_dirt = 1; return result;}static struct buffer_head * block_getblk (struct inode * inode, struct buffer_head * bh, int nr, int create, int blocksize, int new_block, int * err){ int tmp, goal = 0; u32 * p; struct buffer_head * result; int blocks = inode->i_sb->s_blocksize / 512; if (!bh) return NULL; if (!buffer_uptodate(bh)) { ll_rw_block (READ, 1, &bh); wait_on_buffer (bh); if (!buffer_uptodate(bh)) { brelse (bh); return NULL; } } p = (u32 *) bh->b_data + nr;repeat: tmp = *p; if (tmp) { result = getblk (bh->b_dev, tmp, blocksize); if (tmp == *p) { brelse (bh); return result; } brelse (result); goto repeat; } if (!create || new_block >= (current->rlim[RLIMIT_FSIZE].rlim_cur >> EXT2_BLOCK_SIZE_BITS(inode->i_sb))) { brelse (bh); *err = -EFBIG; return NULL; } if (inode->u.ext2_i.i_next_alloc_block == new_block) goal = inode->u.ext2_i.i_next_alloc_goal; if (!goal) { for (tmp = nr - 1; tmp >= 0; tmp--) { if (((u32 *) bh->b_data)[tmp]) { goal = ((u32 *)bh->b_data)[tmp]; break; } } if (!goal) goal = bh->b_blocknr; } tmp = ext2_alloc_block (inode, goal, err); if (!tmp) { brelse (bh); return NULL; } result = getblk (bh->b_dev, tmp, blocksize); if (*p) { ext2_free_blocks (inode, tmp, 1); brelse (result); goto repeat; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -