inode.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 817 行 · 第 1/2 页
C
817 行
/* * linux/fs/ufs/inode.c * * Copyright (C) 1998 * Daniel Pirkl <daniel.pirkl@email.cz> * Charles University, Faculty of Mathematics and Physics * * from * * 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 * Big-endian to little-endian byte-swapping/bitmaps by * David S. Miller (davem@caip.rutgers.edu), 1995 */#include <asm/uaccess.h>#include <asm/system.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/ufs_fs.h>#include <linux/time.h>#include <linux/stat.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/smp_lock.h>#include <linux/buffer_head.h>#include "swab.h"#include "util.h"#undef UFS_INODE_DEBUG#undef UFS_INODE_DEBUG_MORE#ifdef UFS_INODE_DEBUG#define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;#else#define UFSD(x)#endifstatic int ufs_block_to_path(struct inode *inode, sector_t i_block, sector_t offsets[4]){ struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi; int ptrs = uspi->s_apb; int ptrs_bits = uspi->s_apbshift; const long direct_blocks = UFS_NDADDR, indirect_blocks = ptrs, double_blocks = (1 << (ptrs_bits * 2)); int n = 0; UFSD(("ptrs=uspi->s_apb = %d,double_blocks=%d \n",ptrs,double_blocks)); if (i_block < 0) { ufs_warning(inode->i_sb, "ufs_block_to_path", "block < 0"); } else if (i_block < direct_blocks) { offsets[n++] = i_block; } else if ((i_block -= direct_blocks) < indirect_blocks) { offsets[n++] = UFS_IND_BLOCK; offsets[n++] = i_block; } else if ((i_block -= indirect_blocks) < double_blocks) { offsets[n++] = UFS_DIND_BLOCK; offsets[n++] = i_block >> ptrs_bits; offsets[n++] = i_block & (ptrs - 1); } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { offsets[n++] = UFS_TIND_BLOCK; offsets[n++] = i_block >> (ptrs_bits * 2); offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); offsets[n++] = i_block & (ptrs - 1); } else { ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big"); } return n;}/* * Returns the location of the fragment from * the begining of the filesystem. */u64 ufs_frag_map(struct inode *inode, sector_t frag){ struct ufs_inode_info *ufsi = UFS_I(inode); struct super_block *sb = inode->i_sb; struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift; int shift = uspi->s_apbshift-uspi->s_fpbshift; sector_t offsets[4], *p; int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets); u64 ret = 0L; __fs32 block; __fs64 u2_block = 0L; unsigned flags = UFS_SB(sb)->s_flags; u64 temp = 0L; UFSD((": frag = %lu depth = %d\n",frag,depth)); UFSD((": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",uspi->s_fpbshift,uspi->s_apbmask,mask)); if (depth == 0) return 0; p = offsets; lock_kernel(); if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) goto ufs2; block = ufsi->i_u1.i_data[*p++]; if (!block) goto out; while (--depth) { struct buffer_head *bh; sector_t n = *p++; bh = sb_bread(sb, uspi->s_sbbase + fs32_to_cpu(sb, block)+(n>>shift)); if (!bh) goto out; block = ((__fs32 *) bh->b_data)[n & mask]; brelse (bh); if (!block) goto out; } ret = (u64) (uspi->s_sbbase + fs32_to_cpu(sb, block) + (frag & uspi->s_fpbmask)); goto out;ufs2: u2_block = ufsi->i_u1.u2_i_data[*p++]; if (!u2_block) goto out; while (--depth) { struct buffer_head *bh; sector_t n = *p++; temp = (u64)(uspi->s_sbbase) + fs64_to_cpu(sb, u2_block); bh = sb_bread(sb, temp +(u64) (n>>shift)); if (!bh) goto out; u2_block = ((__fs64 *)bh->b_data)[n & mask]; brelse(bh); if (!u2_block) goto out; } temp = (u64)uspi->s_sbbase + fs64_to_cpu(sb, u2_block); ret = temp + (u64) (frag & uspi->s_fpbmask);out: unlock_kernel(); return ret;}static struct buffer_head * ufs_inode_getfrag (struct inode *inode, unsigned int fragment, unsigned int new_fragment, unsigned int required, int *err, int metadata, long *phys, int *new){ struct ufs_inode_info *ufsi = UFS_I(inode); struct super_block * sb; struct ufs_sb_private_info * uspi; struct buffer_head * result; unsigned block, blockoff, lastfrag, lastblock, lastblockoff; unsigned tmp, goal; __fs32 * p, * p2; unsigned flags = 0; UFSD(("ENTER, ino %lu, fragment %u, new_fragment %u, required %u\n", inode->i_ino, fragment, new_fragment, required)) sb = inode->i_sb; uspi = UFS_SB(sb)->s_uspi; flags = UFS_SB(sb)->s_flags; /* TODO : to be done for write support if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) goto ufs2; */ block = ufs_fragstoblks (fragment); blockoff = ufs_fragnum (fragment); p = ufsi->i_u1.i_data + block; goal = 0;repeat: tmp = fs32_to_cpu(sb, *p); lastfrag = ufsi->i_lastfrag; if (tmp && fragment < lastfrag) { if (metadata) { result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff); if (tmp == fs32_to_cpu(sb, *p)) { UFSD(("EXIT, result %u\n", tmp + blockoff)) return result; } brelse (result); goto repeat; } else { *phys = tmp; return NULL; } } lastblock = ufs_fragstoblks (lastfrag); lastblockoff = ufs_fragnum (lastfrag); /* * We will extend file into new block beyond last allocated block */ if (lastblock < block) { /* * We must reallocate last allocated block */ if (lastblockoff) { p2 = ufsi->i_u1.i_data + lastblock; tmp = ufs_new_fragments (inode, p2, lastfrag, fs32_to_cpu(sb, *p2), uspi->s_fpb - lastblockoff, err); if (!tmp) { if (lastfrag != ufsi->i_lastfrag) goto repeat; else return NULL; } lastfrag = ufsi->i_lastfrag; } goal = fs32_to_cpu(sb, ufsi->i_u1.i_data[lastblock]) + uspi->s_fpb; tmp = ufs_new_fragments (inode, p, fragment - blockoff, goal, required + blockoff, err); } /* * We will extend last allocated block */ else if (lastblock == block) { tmp = ufs_new_fragments (inode, p, fragment - (blockoff - lastblockoff), fs32_to_cpu(sb, *p), required + (blockoff - lastblockoff), err); } /* * We will allocate new block before last allocated block */ else /* (lastblock > block) */ { if (lastblock && (tmp = fs32_to_cpu(sb, ufsi->i_u1.i_data[lastblock-1]))) goal = tmp + uspi->s_fpb; tmp = ufs_new_fragments (inode, p, fragment - blockoff, goal, uspi->s_fpb, err); } if (!tmp) { if ((!blockoff && *p) || (blockoff && lastfrag != ufsi->i_lastfrag)) goto repeat; *err = -ENOSPC; return NULL; } /* The nullification of framgents done in ufs/balloc.c is * something I don't have the stomache to move into here right * now. -DaveM */ if (metadata) { result = sb_getblk(inode->i_sb, tmp + blockoff); } else { *phys = tmp; result = NULL; *err = 0; *new = 1; } inode->i_ctime = CURRENT_TIME; if (IS_SYNC(inode)) ufs_sync_inode (inode); mark_inode_dirty(inode); UFSD(("EXIT, result %u\n", tmp + blockoff)) return result; /* This part : To be implemented .... Required only for writing, not required for READ-ONLY.ufs2: u2_block = ufs_fragstoblks(fragment); u2_blockoff = ufs_fragnum(fragment); p = ufsi->i_u1.u2_i_data + block; goal = 0;repeat2: tmp = fs32_to_cpu(sb, *p); lastfrag = ufsi->i_lastfrag; */}static struct buffer_head * ufs_block_getfrag (struct inode *inode, struct buffer_head *bh, unsigned int fragment, unsigned int new_fragment, unsigned int blocksize, int * err, int metadata, long *phys, int *new){ struct super_block * sb; struct ufs_sb_private_info * uspi; struct buffer_head * result; unsigned tmp, goal, block, blockoff; __fs32 * p; sb = inode->i_sb; uspi = UFS_SB(sb)->s_uspi; block = ufs_fragstoblks (fragment); blockoff = ufs_fragnum (fragment); UFSD(("ENTER, ino %lu, fragment %u, new_fragment %u\n", inode->i_ino, fragment, new_fragment)) result = NULL; if (!bh) goto out; if (!buffer_uptodate(bh)) { ll_rw_block (READ, 1, &bh); wait_on_buffer (bh); if (!buffer_uptodate(bh)) goto out; } p = (__fs32 *) bh->b_data + block;repeat: tmp = fs32_to_cpu(sb, *p); if (tmp) { if (metadata) { result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff); if (tmp == fs32_to_cpu(sb, *p)) goto out; brelse (result); goto repeat; } else { *phys = tmp; goto out; } } if (block && (tmp = fs32_to_cpu(sb, ((__fs32*)bh->b_data)[block-1]) + uspi->s_fpb)) goal = tmp + uspi->s_fpb; else goal = bh->b_blocknr + uspi->s_fpb; tmp = ufs_new_fragments (inode, p, ufs_blknum(new_fragment), goal, uspi->s_fpb, err); if (!tmp) { if (fs32_to_cpu(sb, *p)) goto repeat; goto out; } /* The nullification of framgents done in ufs/balloc.c is * something I don't have the stomache to move into here right * now. -DaveM */ if (metadata) { result = sb_getblk(sb, tmp + blockoff); } else { *phys = tmp; *new = 1; } mark_buffer_dirty(bh); if (IS_SYNC(inode)) sync_dirty_buffer(bh); inode->i_ctime = CURRENT_TIME; mark_inode_dirty(inode);out: brelse (bh); UFSD(("EXIT, result %u\n", tmp + blockoff)) return result;}/* * This function gets the block which contains the fragment. */static int ufs_getfrag_block (struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create){ struct super_block * sb = inode->i_sb; struct ufs_sb_private_info * uspi = UFS_SB(sb)->s_uspi; struct buffer_head * bh; int ret, err, new; unsigned long ptr,phys; u64 phys64 = 0; if (!create) { phys64 = ufs_frag_map(inode, fragment); UFSD(("phys64 = %lu \n",phys64)); if (phys64) map_bh(bh_result, sb, phys64); return 0; } /* This code entered only while writing ....? */ err = -EIO; new = 0; ret = 0; bh = NULL; lock_kernel(); UFSD(("ENTER, ino %lu, fragment %u\n", inode->i_ino, fragment)) if (fragment < 0) goto abort_negative; if (fragment > ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb) << uspi->s_fpbshift))
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?