📄 inode.c
字号:
/* * inode.c * * PURPOSE * Inode handling routines for the OSTA-UDF(tm) filesystem. * * 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-2004 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/mm.h>#include <linux/smp_lock.h>#include <linux/module.h>#include <linux/pagemap.h>#include <linux/buffer_head.h>#include <linux/writeback.h>#include <linux/slab.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 int udf_alloc_i_data(struct inode *inode, size_t size);static struct buffer_head *inode_getblk(struct inode *, sector_t, int *, long *, int *);static int8_t udf_insert_aext(struct inode *, struct extent_position, kernel_lb_addr, uint32_t);static void udf_split_extents(struct inode *, int *, int, int, kernel_long_ad[EXTENT_MERGE_SIZE], int *);static void udf_prealloc_extents(struct inode *, int, int, kernel_long_ad[EXTENT_MERGE_SIZE], int *);static void udf_merge_extents(struct inode *, kernel_long_ad[EXTENT_MERGE_SIZE], int *);static void udf_update_extents(struct inode *, kernel_long_ad[EXTENT_MERGE_SIZE], int, int, struct extent_position *);static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);/* * 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){ truncate_inode_pages(&inode->i_data, 0); if (is_bad_inode(inode)) goto no_delete; inode->i_size = 0; udf_truncate(inode); lock_kernel(); udf_update_inode(inode, IS_SYNC(inode)); udf_free_inode(inode); unlock_kernel(); return;no_delete: clear_inode(inode);}/* * If we are going to release inode from memory, we discard preallocation and * truncate last inode extent to proper length. We could use drop_inode() but * it's called under inode_lock and thus we cannot mark inode dirty there. We * use clear_inode() but we have to make sure to write inode as it's not written * automatically. */void udf_clear_inode(struct inode *inode){ if (!(inode->i_sb->s_flags & MS_RDONLY)) { lock_kernel(); /* Discard preallocation for directories, symlinks, etc. */ udf_discard_prealloc(inode); udf_truncate_tail_extent(inode); unlock_kernel(); write_inode_now(inode, 1); } kfree(UDF_I_DATA(inode)); UDF_I_DATA(inode) = NULL;}static int udf_writepage(struct page *page, struct writeback_control *wbc){ return block_write_full_page(page, udf_get_block, wbc);}static int udf_readpage(struct file *file, struct page *page){ return block_read_full_page(page, udf_get_block);}static int udf_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata){ *pagep = NULL; return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, udf_get_block);}static sector_t udf_bmap(struct address_space *mapping, sector_t block){ return generic_block_bmap(mapping, block, udf_get_block);}const struct address_space_operations udf_aops = { .readpage = udf_readpage, .writepage = udf_writepage, .sync_page = block_sync_page, .write_begin = udf_write_begin, .write_end = generic_write_end, .bmap = udf_bmap,};void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err){ struct page *page; char *kaddr; struct writeback_control udf_wbc = { .sync_mode = WB_SYNC_NONE, .nr_to_write = 1, }; /* 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) = ICBTAG_FLAG_AD_SHORT; else UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG; mark_inode_dirty(inode); return; } page = grab_cache_page(inode->i_mapping, 0); BUG_ON(!PageLocked(page)); if (!PageUptodate(page)) { kaddr = kmap(page); memset(kaddr + UDF_I_LENALLOC(inode), 0x00, PAGE_CACHE_SIZE - UDF_I_LENALLOC(inode)); memcpy(kaddr, UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), UDF_I_LENALLOC(inode)); flush_dcache_page(page); SetPageUptodate(page); kunmap(page); } memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0x00, 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) = ICBTAG_FLAG_AD_SHORT; else UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG; inode->i_data.a_ops->writepage(page, &udf_wbc); page_cache_release(page); mark_inode_dirty(inode);}struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block, int *err){ int newblock; struct buffer_head *dbh = NULL; kernel_lb_addr eloc; uint32_t elen; uint8_t alloctype; struct extent_position epos; 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 (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) alloctype = ICBTAG_FLAG_AD_SHORT; else alloctype = ICBTAG_FLAG_AD_LONG; if (!inode->i_size) { UDF_I_ALLOCTYPE(inode) = alloctype; 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; dbh = udf_tgetblk(inode->i_sb, newblock); if (!dbh) return NULL; lock_buffer(dbh); memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize); set_buffer_uptodate(dbh); 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 = NULL; dfibh.soffset = dfibh.eoffset = 0; dfibh.sbh = dfibh.ebh = dbh; while ((f_pos < size)) { UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB; sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL, NULL, NULL, NULL); if (!sfi) { brelse(dbh); return NULL; } UDF_I_ALLOCTYPE(inode) = alloctype; sfi->descTag.tagLocation = cpu_to_le32(*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 + le16_to_cpu(sfi->lengthOfImpUse))) { UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB; brelse(dbh); return NULL; } } mark_buffer_dirty_inode(dbh, inode); memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0, UDF_I_LENALLOC(inode)); UDF_I_LENALLOC(inode) = 0; eloc.logicalBlockNum = *block; eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum; elen = inode->i_size; UDF_I_LENEXTENTS(inode) = elen; epos.bh = NULL; epos.block = UDF_I_LOCATION(inode); epos.offset = udf_file_entry_alloc_offset(inode); udf_add_aext(inode, &epos, eloc, elen, 0); /* UniqueID stuff */ brelse(epos.bh); mark_inode_dirty(inode); return dbh;}static int udf_get_block(struct inode *inode, sector_t 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) map_bh(bh_result, inode->i_sb, phys); 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); BUG_ON(bh); if (err) goto abort; BUG_ON(!phys); if (new) set_buffer_new(bh_result); map_bh(bh_result, inode->i_sb, phys);abort: unlock_kernel(); return err;abort_negative: udf_warning(inode->i_sb, "udf_get_block", "block < 0"); goto abort;}static struct buffer_head *udf_getblk(struct inode *inode, long block, int create, int *err){ struct buffer_head *bh; 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)) { 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); set_buffer_uptodate(bh); unlock_buffer(bh); mark_buffer_dirty_inode(bh, inode); } return bh; } return NULL;}/* Extend the file by 'blocks' blocks, return the number of extents added */int udf_extend_file(struct inode *inode, struct extent_position *last_pos, kernel_long_ad * last_ext, sector_t blocks){ sector_t add; int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK); struct super_block *sb = inode->i_sb; kernel_lb_addr prealloc_loc = {}; int prealloc_len = 0; /* The previous extent is fake and we should not extend by anything * - there's nothing to do... */ if (!blocks && fake) return 0; /* Round the last extent up to a multiple of block size */ if (last_ext->extLength & (sb->s_blocksize - 1)) { last_ext->extLength = (last_ext->extLength & UDF_EXTENT_FLAG_MASK) | (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1)); UDF_I_LENEXTENTS(inode) = (UDF_I_LENEXTENTS(inode) + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1); } /* Last extent are just preallocated blocks? */ if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) == EXT_NOT_RECORDED_ALLOCATED) { /* Save the extent so that we can reattach it to the end */ prealloc_loc = last_ext->extLocation; prealloc_len = last_ext->extLength; /* Mark the extent as a hole */ last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | (last_ext->extLength & UDF_EXTENT_LENGTH_MASK); last_ext->extLocation.logicalBlockNum = 0; last_ext->extLocation.partitionReferenceNum = 0; } /* Can we merge with the previous extent? */ if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) == EXT_NOT_RECORDED_NOT_ALLOCATED) { add = ((1 << 30) - sb->s_blocksize - (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >> sb->s_blocksize_bits; if (add > blocks) add = blocks; blocks -= add; last_ext->extLength += add << sb->s_blocksize_bits; } if (fake) { udf_add_aext(inode, last_pos, last_ext->extLocation, last_ext->extLength, 1); count++; } else { udf_write_aext(inode, last_pos, last_ext->extLocation, last_ext->extLength, 1); } /* Managed to do everything necessary? */ if (!blocks) goto out; /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */ last_ext->extLocation.logicalBlockNum = 0; last_ext->extLocation.partitionReferenceNum = 0; add = (1 << (30-sb->s_blocksize_bits)) - 1; last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | (add << sb->s_blocksize_bits); /* Create enough extents to cover the whole hole */ while (blocks > add) { blocks -= add; if (udf_add_aext(inode, last_pos, last_ext->extLocation, last_ext->extLength, 1) == -1) return -1; count++; } if (blocks) { last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | (blocks << sb->s_blocksize_bits); if (udf_add_aext(inode, last_pos, last_ext->extLocation, last_ext->extLength, 1) == -1) return -1; count++; }out: /* Do we have some preallocated blocks saved? */ if (prealloc_len) { if (udf_add_aext(inode, last_pos, prealloc_loc, prealloc_len, 1) == -1) return -1; last_ext->extLocation = prealloc_loc; last_ext->extLength = prealloc_len; count++; } /* last_pos should point to the last written extent... */ if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT) last_pos->offset -= sizeof(short_ad); else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG) last_pos->offset -= sizeof(long_ad); else return -1; return count;}static struct buffer_head *inode_getblk(struct inode *inode, sector_t block, int *err, long *phys, int *new){ static sector_t last_block; struct buffer_head *result = NULL; kernel_long_ad laarr[EXTENT_MERGE_SIZE]; struct extent_position prev_epos, cur_epos, next_epos; int count = 0, startnum = 0, endnum = 0; uint32_t elen = 0, tmpelen; kernel_lb_addr eloc, tmpeloc; int c = 1; loff_t lbcount = 0, b_off = 0; uint32_t newblocknum, newblock; sector_t offset = 0; int8_t etype; int goal = 0, pgoal = UDF_I_LOCATION(inode).logicalBlockNum; int lastblock = 0; prev_epos.offset = udf_file_entry_alloc_offset(inode); prev_epos.block = UDF_I_LOCATION(inode); prev_epos.bh = NULL; cur_epos = next_epos = prev_epos; b_off = (loff_t)block << inode->i_sb->s_blocksize_bits; /* 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 (prev_epos.bh != cur_epos.bh) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -