apfs: move extent code to separate files

The extent structures and functions may be needed in both inode.c and
xattr.c, so place them in files of their own.

Signed-off-by: Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
This commit is contained in:
Ernesto A. Fernández
2018-11-28 16:09:44 -03:00
parent e79efd50e0
commit 7132a98d7f
7 changed files with 184 additions and 150 deletions
+2 -2
View File
@@ -5,5 +5,5 @@
obj-$(CONFIG_APFS_FS) += apfs.o
apfs-y := btree.o dir.o file.o inode.o key.o message.o namei.o \
super.o symlink.o table.o unicode.o xattr.o
apfs-y := btree.o dir.o extents.o file.o inode.o key.o message.o \
namei.o super.o symlink.o table.o unicode.o xattr.o
-23
View File
@@ -94,29 +94,6 @@ struct apfs_node {
#define APFS_MAX_CKSUM_SIZE 8
/* File extent records */
#define APFS_FILE_EXTENT_LEN_MASK 0x00ffffffffffffffULL
#define APFS_FILE_EXTENT_FLAG_MASK 0xff00000000000000ULL
#define APFS_FILE_EXTENT_FLAG_SHIFT 56
/*
* Structure of a file extent record
*/
struct apfs_file_extent_val {
__le64 len_and_flags;
__le64 phys_block_num;
__le64 crypto_id;
} __packed;
/*
* Extent record data in memory
*/
struct apfs_file_extent {
u64 logical_addr;
u64 phys_block_num;
u64 len;
};
/*
* Inode and file operations
*/
+140
View File
@@ -0,0 +1,140 @@
// SPDX-License-Identifier: GPL-2.0
/*
* linux/fs/apfs/extents.c
*
* Copyright (C) 2018 Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
*/
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include "apfs.h"
#include "btree.h"
#include "extents.h"
#include "inode.h"
#include "key.h"
#include "message.h"
#include "super.h"
#include "table.h"
/**
* apfs_extent_read - Read the extent record that covers a block
* @inode: inode that owns the record
* @iblock: logical number of the wanted block
* @extent: Return parameter. The extent found.
*
* Finds and caches the extent record. On success, returns a pointer to the
* cache record; on failure, returns an error code.
*/
static int apfs_extent_read(struct inode *inode, sector_t iblock,
struct apfs_file_extent *extent)
{
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_inode_info *ai = APFS_I(inode);
struct apfs_key *key;
struct apfs_query *query;
struct apfs_file_extent_val *ext;
struct apfs_file_extent_key *ext_key;
struct apfs_file_extent *cache = &ai->i_cached_extent;
char *raw;
u64 iaddr = iblock << inode->i_blkbits;
u64 ext_len;
int ret = 0;
mutex_lock(&ai->i_extent_lock);
if (iaddr >= cache->logical_addr &&
iaddr < cache->logical_addr + cache->len) {
*extent = *cache;
mutex_unlock(&ai->i_extent_lock);
return 0;
}
mutex_unlock(&ai->i_extent_lock);
key = kmalloc(sizeof(*key), GFP_KERNEL);
if (!key)
return -ENOMEM;
/* We will search for the extent that covers iblock */
apfs_init_key(sb, APFS_TYPE_FILE_EXTENT, ai->i_extent_id,
NULL /* name */, 0 /* namelen */, iaddr, key);
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query) {
ret = -ENOMEM;
goto fail;
}
query->key = key;
query->flags = APFS_QUERY_CAT;
ret = apfs_btree_query(sb, &query);
if (ret)
goto done;
if (query->len != sizeof(*ext) || query->key_len != sizeof(*ext_key)) {
apfs_alert(sb, "bad extent record for inode 0x%llx",
(unsigned long long) inode->i_ino);
ret = -EFSCORRUPTED;
goto done;
}
raw = query->table->t_node.bh->b_data;
ext = (struct apfs_file_extent_val *)(raw + query->off);
ext_key = (struct apfs_file_extent_key *)(raw + query->key_off);
ext_len = le64_to_cpu(ext->len_and_flags) & APFS_FILE_EXTENT_LEN_MASK;
/* Extent length must be a multiple of the block size */
if (ext_len & (sb->s_blocksize - 1)) {
apfs_alert(sb, "bad extent length for inode 0x%llx",
(unsigned long long) inode->i_ino);
ret = -EFSCORRUPTED;
goto done;
}
extent->logical_addr = le64_to_cpu(ext_key->logical_addr);
extent->phys_block_num = le64_to_cpu(ext->phys_block_num);
extent->len = ext_len;
mutex_lock(&ai->i_extent_lock);
*cache = *extent;
mutex_unlock(&ai->i_extent_lock);
done:
apfs_free_query(sb, query);
fail:
kfree(key);
return ret;
}
int apfs_get_block(struct inode *inode, sector_t iblock,
struct buffer_head *bh_result, int create)
{
struct super_block *sb = inode->i_sb;
struct apfs_file_extent ext;
u64 blk_off, bno, map_len;
int ret;
ret = apfs_extent_read(inode, iblock, &ext);
if (ret)
return ret;
/* Find the block offset of iblock within the extent */
blk_off = iblock - (ext.logical_addr >> inode->i_blkbits);
/* Make sure we don't read past the extent boundaries */
map_len = ext.len - (blk_off << inode->i_blkbits);
if (bh_result->b_size > map_len)
bh_result->b_size = map_len;
/*
* Save the requested mapping length as map_bh() replaces it with
* the filesystem block size
*/
map_len = bh_result->b_size;
/* Extents representing holes have block number 0 */
if (ext.phys_block_num != 0) {
/* Find the block number of iblock within the disk */
bno = ext.phys_block_num + blk_off;
map_bh(bh_result, sb, bno);
}
bh_result->b_size = map_len;
return 0;
}
+39
View File
@@ -0,0 +1,39 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* linux/fs/apfs/extents.h
*
* Copyright (C) 2018 Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
*/
#ifndef _EXTENTS_H
#define _EXTENTS_H
#include <linux/types.h>
/* File extent records */
#define APFS_FILE_EXTENT_LEN_MASK 0x00ffffffffffffffULL
#define APFS_FILE_EXTENT_FLAG_MASK 0xff00000000000000ULL
#define APFS_FILE_EXTENT_FLAG_SHIFT 56
/*
* Structure of a file extent record
*/
struct apfs_file_extent_val {
__le64 len_and_flags;
__le64 phys_block_num;
__le64 crypto_id;
} __packed;
/*
* Extent record data in memory
*/
struct apfs_file_extent {
u64 logical_addr;
u64 phys_block_num;
u64 len;
};
extern int apfs_get_block(struct inode *inode, sector_t iblock,
struct buffer_head *bh_result, int create);
#endif /* _EXTENTS_H */
+1 -125
View File
@@ -12,6 +12,7 @@
#include "apfs.h"
#include "btree.h"
#include "dir.h"
#include "extents.h"
#include "inode.h"
#include "key.h"
#include "message.h"
@@ -19,131 +20,6 @@
#include "table.h"
#include "xattr.h"
/**
* apfs_extent_read - Read the extent record that covers a block
* @inode: inode that owns the record
* @iblock: logical number of the wanted block
* @extent: Return parameter. The extent found.
*
* Finds and caches the extent record. On success, returns a pointer to the
* cache record; on failure, returns an error code.
*/
static int apfs_extent_read(struct inode *inode, sector_t iblock,
struct apfs_file_extent *extent)
{
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_inode_info *ai = APFS_I(inode);
struct apfs_key *key;
struct apfs_query *query;
struct apfs_file_extent_val *ext;
struct apfs_file_extent_key *ext_key;
struct apfs_file_extent *cache = &ai->i_cached_extent;
char *raw;
u64 iaddr = iblock << inode->i_blkbits;
u64 ext_len;
int ret = 0;
mutex_lock(&ai->i_extent_lock);
if (iaddr >= cache->logical_addr &&
iaddr < cache->logical_addr + cache->len) {
*extent = *cache;
mutex_unlock(&ai->i_extent_lock);
return 0;
}
mutex_unlock(&ai->i_extent_lock);
key = kmalloc(sizeof(*key), GFP_KERNEL);
if (!key)
return -ENOMEM;
/* We will search for the extent that covers iblock */
apfs_init_key(sb, APFS_TYPE_FILE_EXTENT, ai->i_extent_id,
NULL /* name */, 0 /* namelen */, iaddr, key);
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query) {
ret = -ENOMEM;
goto fail;
}
query->key = key;
query->flags = APFS_QUERY_CAT;
ret = apfs_btree_query(sb, &query);
if (ret)
goto done;
if (query->len != sizeof(*ext) || query->key_len != sizeof(*ext_key)) {
apfs_alert(sb, "bad extent record for inode 0x%llx",
(unsigned long long) inode->i_ino);
ret = -EFSCORRUPTED;
goto done;
}
raw = query->table->t_node.bh->b_data;
ext = (struct apfs_file_extent_val *)(raw + query->off);
ext_key = (struct apfs_file_extent_key *)(raw + query->key_off);
ext_len = le64_to_cpu(ext->len_and_flags) & APFS_FILE_EXTENT_LEN_MASK;
/* Extent length must be a multiple of the block size */
if (ext_len & (sb->s_blocksize - 1)) {
apfs_alert(sb, "bad extent length for inode 0x%llx",
(unsigned long long) inode->i_ino);
ret = -EFSCORRUPTED;
goto done;
}
extent->logical_addr = le64_to_cpu(ext_key->logical_addr);
extent->phys_block_num = le64_to_cpu(ext->phys_block_num);
extent->len = ext_len;
mutex_lock(&ai->i_extent_lock);
*cache = *extent;
mutex_unlock(&ai->i_extent_lock);
done:
apfs_free_query(sb, query);
fail:
kfree(key);
return ret;
}
static int apfs_get_block(struct inode *inode, sector_t iblock,
struct buffer_head *bh_result, int create)
{
struct super_block *sb = inode->i_sb;
struct apfs_file_extent ext;
u64 blk_off, bno, map_len;
int ret;
ret = apfs_extent_read(inode, iblock, &ext);
if (ret)
return ret;
/* Find the block offset of iblock within the extent */
blk_off = iblock - (ext.logical_addr >> inode->i_blkbits);
/* Make sure we don't read past the extent boundaries */
map_len = ext.len - (blk_off << inode->i_blkbits);
if (bh_result->b_size > map_len)
bh_result->b_size = map_len;
/*
* Save the requested mapping length as map_bh() replaces it with
* the filesystem block size
*/
map_len = bh_result->b_size;
/* Extents representing holes have block number 0 */
if (ext.phys_block_num != 0) {
/* Find the block number of iblock within the disk */
bno = ext.phys_block_num + blk_off;
map_bh(bh_result, sb, bno);
}
bh_result->b_size = map_len;
return 0;
}
static int apfs_readpage(struct file *file, struct page *page)
{
return mpage_readpage(page, apfs_get_block);
+1
View File
@@ -10,6 +10,7 @@
#include <linux/fs.h>
#include <linux/types.h>
#include "extents.h"
/* Inode numbers for special inodes */
#define APFS_INVALID_INO_NUM 0
+1
View File
@@ -9,6 +9,7 @@
#include <linux/xattr.h>
#include "apfs.h"
#include "btree.h"
#include "extents.h"
#include "key.h"
#include "super.h"
#include "table.h"