?? inode.c
字號:
/* * inode.c * * PURPOSE * Inode handling routines for the OSTA-UDF(tm) filesystem. * * CONTACTS * E-mail regarding any portion of the Linux UDF file system should be * directed to the development team mailing list (run by majordomo): * linux_udf@hpesjro.fc.hp.com * * COPYRIGHT * This file is distributed under the terms of the GNU General Public * License (GPL). Copies of the GPL can be obtained from: * ftp://prep.ai.mit.edu/pub/gnu/GPL * Each contributing author retains all rights to their own work. * * (C) 1998 Dave Boynton * (C) 1998-2000 Ben Fennema * (C) 1999-2000 Stelias Computing Inc * * HISTORY * * 10/04/98 dgb Added rudimentary directory functions * 10/07/98 Fully working udf_block_map! It works! * 11/25/98 bmap altered to better support extents * 12/06/98 blf partition support in udf_iget, udf_block_map and udf_read_inode * 12/12/98 rewrote udf_block_map to handle next extents and descs across * block boundaries (which is not actually allowed) * 12/20/98 added support for strategy 4096 * 03/07/99 rewrote udf_block_map (again) * New funcs, inode_bmap, udf_next_aext * 04/19/99 Support for writing device EA's for major/minor # */#include "udfdecl.h"#include <linux/locks.h>#include <linux/mm.h>#include <linux/smp_lock.h>#include <linux/module.h>#include "udf_i.h"#include "udf_sb.h"MODULE_AUTHOR("Ben Fennema");MODULE_DESCRIPTION("Universal Disk Format Filesystem");MODULE_LICENSE("GPL");#define EXTENT_MERGE_SIZE 5static mode_t udf_convert_permissions(struct FileEntry *);static int udf_update_inode(struct inode *, int);static void udf_fill_inode(struct inode *, struct buffer_head *);static struct buffer_head *inode_getblk(struct inode *, long, int *, long *, int *);static void udf_split_extents(struct inode *, int *, int, int, long_ad [EXTENT_MERGE_SIZE], int *);static void udf_prealloc_extents(struct inode *, int, int, long_ad [EXTENT_MERGE_SIZE], int *);static void udf_merge_extents(struct inode *, long_ad [EXTENT_MERGE_SIZE], int *);static void udf_update_extents(struct inode *, long_ad [EXTENT_MERGE_SIZE], int, int, lb_addr, Uint32, struct buffer_head **);static int udf_get_block(struct inode *, long, struct buffer_head *, int);/* * udf_put_inode * * PURPOSE * * DESCRIPTION * This routine is called whenever the kernel no longer needs the inode. * * HISTORY * July 1, 1997 - Andrew E. Mileski * Written, tested, and released. * * Called at each iput() */void udf_put_inode(struct inode * inode){ if (!(inode->i_sb->s_flags & MS_RDONLY)) { lock_kernel(); udf_discard_prealloc(inode); /* write the root inode on put, if dirty */ if (!inode->i_sb->s_root && inode->i_state & I_DIRTY) udf_update_inode(inode, IS_SYNC(inode)); unlock_kernel(); }}/* * udf_delete_inode * * PURPOSE * Clean-up before the specified inode is destroyed. * * DESCRIPTION * This routine is called when the kernel destroys an inode structure * ie. when iput() finds i_count == 0. * * HISTORY * July 1, 1997 - Andrew E. Mileski * Written, tested, and released. * * Called at the last iput() if i_nlink is zero. */void udf_delete_inode(struct inode * inode){ lock_kernel(); if (is_bad_inode(inode)) goto no_delete; inode->i_size = 0; udf_truncate(inode); udf_update_inode(inode, IS_SYNC(inode)); udf_free_inode(inode); unlock_kernel(); return;no_delete: unlock_kernel(); clear_inode(inode);}void udf_discard_prealloc(struct inode * inode){ if (inode->i_size && inode->i_size != UDF_I_LENEXTENTS(inode) && UDF_I_ALLOCTYPE(inode) != ICB_FLAG_AD_IN_ICB) { udf_truncate_extents(inode); }}static int udf_writepage(struct page *page){ return block_write_full_page(page, udf_get_block);}static int udf_readpage(struct file *file, struct page *page){ return block_read_full_page(page, udf_get_block);}static int udf_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to){ return block_prepare_write(page, from, to, udf_get_block);}static int udf_bmap(struct address_space *mapping, long block){ return generic_block_bmap(mapping,block,udf_get_block);}struct address_space_operations udf_aops = { readpage: udf_readpage, writepage: udf_writepage, sync_page: block_sync_page, prepare_write: udf_prepare_write, commit_write: generic_commit_write, bmap: udf_bmap,};void udf_expand_file_adinicb(struct inode * inode, int newsize, int * err){ struct buffer_head *bh = NULL; struct page *page; char *kaddr; int block; /* from now on we have normal address_space methods */ inode->i_data.a_ops = &udf_aops; if (!UDF_I_LENALLOC(inode)) { if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_SHORT; else UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_LONG; mark_inode_dirty(inode); return; } block = udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0); bh = udf_tread(inode->i_sb, block); if (!bh) return; page = grab_cache_page(inode->i_mapping, 0); if (!PageLocked(page)) PAGE_BUG(page); if (!Page_Uptodate(page)) { kaddr = kmap(page); memset(kaddr + UDF_I_LENALLOC(inode), 0x00, PAGE_CACHE_SIZE - UDF_I_LENALLOC(inode)); memcpy(kaddr, bh->b_data + udf_file_entry_alloc_offset(inode), UDF_I_LENALLOC(inode)); flush_dcache_page(page); SetPageUptodate(page); kunmap(page); } memset(bh->b_data + udf_file_entry_alloc_offset(inode), 0, UDF_I_LENALLOC(inode)); UDF_I_LENALLOC(inode) = 0; if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_SHORT; else UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_LONG; mark_buffer_dirty_inode(bh, inode); udf_release_data(bh); inode->i_data.a_ops->writepage(page); page_cache_release(page); mark_inode_dirty(inode); inode->i_version ++;}struct buffer_head * udf_expand_dir_adinicb(struct inode *inode, int *block, int *err){ int newblock; struct buffer_head *sbh = NULL, *dbh = NULL; lb_addr bloc, eloc; Uint32 elen, extoffset; struct udf_fileident_bh sfibh, dfibh; loff_t f_pos = udf_ext0_offset(inode) >> 2; int size = (udf_ext0_offset(inode) + inode->i_size) >> 2; struct FileIdentDesc cfi, *sfi, *dfi; if (!inode->i_size) { if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_SHORT; else UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_LONG; mark_inode_dirty(inode); return NULL; } /* alloc block, and copy data to it */ *block = udf_new_block(inode->i_sb, inode, UDF_I_LOCATION(inode).partitionReferenceNum, UDF_I_LOCATION(inode).logicalBlockNum, err); if (!(*block)) return NULL; newblock = udf_get_pblock(inode->i_sb, *block, UDF_I_LOCATION(inode).partitionReferenceNum, 0); if (!newblock) return NULL; sbh = udf_tread(inode->i_sb, inode->i_ino); if (!sbh) return NULL; dbh = udf_tgetblk(inode->i_sb, newblock); if (!dbh) return NULL; lock_buffer(dbh); memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize); mark_buffer_uptodate(dbh, 1); unlock_buffer(dbh); mark_buffer_dirty_inode(dbh, inode); sfibh.soffset = sfibh.eoffset = (f_pos & ((inode->i_sb->s_blocksize - 1) >> 2)) << 2; sfibh.sbh = sfibh.ebh = sbh; dfibh.soffset = dfibh.eoffset = 0; dfibh.sbh = dfibh.ebh = dbh; while ( (f_pos < size) ) { sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL, NULL, NULL, NULL, NULL, NULL); if (!sfi) { udf_release_data(sbh); udf_release_data(dbh); return NULL; } sfi->descTag.tagLocation = *block; dfibh.soffset = dfibh.eoffset; dfibh.eoffset += (sfibh.eoffset - sfibh.soffset); dfi = (struct FileIdentDesc *)(dbh->b_data + dfibh.soffset); if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse, sfi->fileIdent + sfi->lengthOfImpUse)) { udf_release_data(sbh); udf_release_data(dbh); return NULL; } } mark_buffer_dirty_inode(dbh, inode); memset(sbh->b_data + udf_file_entry_alloc_offset(inode), 0, UDF_I_LENALLOC(inode)); UDF_I_LENALLOC(inode) = 0; if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_SHORT; else UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_LONG; bloc = UDF_I_LOCATION(inode); eloc.logicalBlockNum = *block; eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum; elen = inode->i_size; UDF_I_LENEXTENTS(inode) = elen; extoffset = udf_file_entry_alloc_offset(inode); udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &sbh, 0); /* UniqueID stuff */ mark_buffer_dirty(sbh); udf_release_data(sbh); mark_inode_dirty(inode); inode->i_version ++; return dbh;}static int udf_get_block(struct inode *inode, long block, struct buffer_head *bh_result, int create){ int err, new; struct buffer_head *bh; unsigned long phys; if (!create) { phys = udf_block_map(inode, block); if (phys) { bh_result->b_dev = inode->i_dev; bh_result->b_blocknr = phys; bh_result->b_state |= (1UL << BH_Mapped); } return 0; } err = -EIO; new = 0; bh = NULL; lock_kernel(); if (block < 0) goto abort_negative; if (block == UDF_I_NEXT_ALLOC_BLOCK(inode) + 1) { UDF_I_NEXT_ALLOC_BLOCK(inode) ++; UDF_I_NEXT_ALLOC_GOAL(inode) ++; } err = 0; bh = inode_getblk(inode, block, &err, &phys, &new); if (bh) BUG(); if (err) goto abort; if (!phys) BUG(); bh_result->b_dev = inode->i_dev; bh_result->b_blocknr = phys; bh_result->b_state |= (1UL << BH_Mapped); if (new) bh_result->b_state |= (1UL << BH_New);abort: unlock_kernel(); return err;abort_negative: udf_warning(inode->i_sb, "udf_get_block", "block < 0"); goto abort;}struct buffer_head * udf_getblk(struct inode * inode, long block, int create, int * err){ struct buffer_head dummy; dummy.b_state = 0; dummy.b_blocknr = -1000; *err = udf_get_block(inode, block, &dummy, create); if (!*err && buffer_mapped(&dummy)) { struct buffer_head *bh; bh = sb_getblk(inode->i_sb, dummy.b_blocknr); if (buffer_new(&dummy)) { lock_buffer(bh); memset(bh->b_data, 0x00, inode->i_sb->s_blocksize); mark_buffer_uptodate(bh, 1); unlock_buffer(bh); mark_buffer_dirty_inode(bh, inode); } return bh; } return NULL;}static struct buffer_head * inode_getblk(struct inode * inode, long block, int *err, long *phys, int *new){ struct buffer_head *pbh = NULL, *cbh = NULL, *nbh = NULL, *result = NULL; long_ad laarr[EXTENT_MERGE_SIZE]; Uint32 pextoffset = 0, cextoffset = 0, nextoffset = 0; int count = 0, startnum = 0, endnum = 0; Uint32 elen = 0; lb_addr eloc, pbloc, cbloc, nbloc; int c = 1; Uint64 lbcount = 0, b_off = 0; Uint32 newblocknum, newblock, offset = 0; Sint8 etype; int goal = 0, pgoal = UDF_I_LOCATION(inode).logicalBlockNum; char lastblock = 0; pextoffset = cextoffset = nextoffset = udf_file_entry_alloc_offset(inode); b_off = (Uint64)block << inode->i_sb->s_blocksize_bits; pbloc = cbloc = nbloc = UDF_I_LOCATION(inode); /* find the extent which contains the block we are looking for. alternate between laarr[0] and laarr[1] for locations of the current extent, and the previous extent */ do { if (pbh != cbh) { udf_release_data(pbh); atomic_inc(&cbh->b_count); pbh = cbh; } if (cbh != nbh) { udf_release_data(cbh); atomic_inc(&nbh->b_count); cbh = nbh; } lbcount += elen; pbloc = cbloc; cbloc = nbloc; pextoffset = cextoffset; cextoffset = nextoffset; if ((etype = udf_next_aext(inode, &nbloc, &nextoffset, &eloc, &elen, &nbh, 1)) == -1) break; c = !c; laarr[c].extLength = (etype << 30) | elen; laarr[c].extLocation = eloc; if (etype != EXTENT_NOT_RECORDED_NOT_ALLOCATED) pgoal = eloc.logicalBlockNum + ((elen + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits); count ++; } while (lbcount + elen <= b_off); b_off -= lbcount; offset = b_off >> inode->i_sb->s_blocksize_bits; /* if the extent is allocated and recorded, return the block if the extent is not a multiple of the blocksize, round up */ if (etype == EXTENT_RECORDED_ALLOCATED) { if (elen & (inode->i_sb->s_blocksize - 1)) { elen = (EXTENT_RECORDED_ALLOCATED << 30) | ((elen + inode->i_sb->s_blocksize - 1) & ~(inode->i_sb->s_blocksize - 1)); etype = udf_write_aext(inode, nbloc, &cextoffset, eloc, elen, nbh, 1); } udf_release_data(pbh); udf_release_data(cbh); udf_release_data(nbh); newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset); *phys = newblock; return NULL; } if (etype == -1) { endnum = startnum = ((count > 1) ? 1 : count); if (laarr[c].extLength & (inode->i_sb->s_blocksize - 1)) { laarr[c].extLength = (laarr[c].extLength & UDF_EXTENT_FLAG_MASK) | (((laarr[c].extLength & UDF_EXTENT_LENGTH_MASK) + inode->i_sb->s_blocksize - 1) & ~(inode->i_sb->s_blocksize - 1)); UDF_I_LENEXTENTS(inode) = (UDF_I_LENEXTENTS(inode) + inode->i_sb->s_blocksize - 1) & ~(inode->i_sb->s_blocksize - 1); } c = !c; laarr[c].extLength = (EXTENT_NOT_RECORDED_NOT_ALLOCATED << 30) | ((offset + 1) << inode->i_sb->s_blocksize_bits); memset(&laarr[c].extLocation, 0x00, sizeof(lb_addr)); count ++; endnum ++; lastblock = 1; } else endnum = startnum = ((count > 2) ? 2 : count); /* if the current extent is in position 0, swap it with the previous */ if (!c && count != 1) { laarr[2] = laarr[0]; laarr[0] = laarr[1]; laarr[1] = laarr[2]; c = 1; } /* if the current block is located in a extent, read the next extent */ if (etype != -1) { if ((etype = udf_next_aext(inode, &nbloc, &nextoffset, &eloc, &elen, &nbh, 0)) != -1) { laarr[c+1].extLength = (etype << 30) | elen; laarr[c+1].extLocation = eloc; count ++; startnum ++; endnum ++; } else lastblock = 1; } udf_release_data(nbh); if (!pbh) pbh = cbh; else udf_release_data(cbh); /* if the current extent is not recorded but allocated, get the block in the extent corresponding to the requested block */ if ((laarr[c].extLength >> 30) == EXTENT_NOT_RECORDED_ALLOCATED) newblocknum = laarr[c].extLocation.logicalBlockNum + offset; else /* otherwise, allocate a new block */ { if (UDF_I_NEXT_ALLOC_BLOCK(inode) == block) goal = UDF_I_NEXT_ALLOC_GOAL(inode); if (!goal) { if (!(goal = pgoal)) goal = UDF_I_LOCATION(inode).logicalBlockNum + 1; } if (!(newblocknum = udf_new_block(inode->i_sb, inode, UDF_I_LOCATION(inode).partitionReferenceNum, goal, err)))
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -