--- fs/ext4/ext4.h | 3 ++ fs/ext4/inode.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -1838,6 +1838,9 @@ extern int ext4_page_mkwrite(struct vm_a extern qsize_t *ext4_get_reserved_space(struct inode *inode); extern void ext4_da_update_reserve_space(struct inode *inode, int used, int quota_claim); +extern int ext4_map_inode_page(struct inode *inode, struct page *page, + unsigned long *blocks, int created); + /* ioctl.c */ extern long ext4_ioctl(struct file *, unsigned int, unsigned long); extern long ext4_compat_ioctl(struct file *, unsigned int, unsigned long); --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -5968,3 +5968,65 @@ out_unlock: up_read(&inode->i_alloc_sem); return ret; } + +int ext4_map_inode_page(struct inode *inode, struct page *page, + unsigned long *blocks, int create) +{ + unsigned int blocksize, blocks_per_page; + unsigned long iblock; + struct ext4_map_blocks map; + void *handle; + int i, rc = 0, failed = 0, needed_blocks; + + blocksize = inode->i_sb->s_blocksize; + blocks_per_page = PAGE_SIZE >> inode->i_sb->s_blocksize_bits; + iblock = page->index * blocks_per_page; + + for (i = 0; i < blocks_per_page; i++, iblock++) { + blocks[i] = ext4_bmap(inode->i_mapping, iblock); + if (blocks[i] == 0) { + failed++; + } + } + + if (failed == 0 || create == 0) + return 0; + + needed_blocks = ext4_writepage_trans_blocks(inode); + handle = ext4_journal_start(inode, needed_blocks); + if (IS_ERR(handle)) + return PTR_ERR(handle); + + iblock = page->index * blocks_per_page; + for (i = 0; i < blocks_per_page; i++, iblock++) { + if (blocks[i] != 0) + continue; + + map.m_lblk = iblock; + map.m_len = 1; + map.m_flags = 0; + rc = ext4_ind_map_blocks(handle, inode, &map, + EXT4_GET_BLOCKS_CREATE); + if (rc < 0) { + printk(KERN_INFO "ext4_map_inode_page: error reading " + "block %ld\n", iblock); + goto out; + } else { + if (rc > 1) + WARN_ON(1); + rc = 0; + } + /* Unmap any metadata buffers from the block mapping, to avoid + * data corruption due to direct-write from Lustre being + * clobbered by a later flush of the blockdev metadata buffer.*/ + if (map.m_flags & EXT4_MAP_NEW) + unmap_underlying_metadata(inode->i_sb->s_bdev, + map.m_pblk); + blocks[i] = map.m_pblk; + } + +out: + ext4_journal_stop(handle); + return rc; +} +EXPORT_SYMBOL(ext4_map_inode_page);