Files
OrangePi_CM5/fs/btrfs/export.c
T

310 lines
7.6 KiB
C
Raw Normal View History

2008-07-21 02:01:56 +05:30
#include <linux/fs.h>
#include <linux/types.h>
#include "ctree.h"
#include "disk-io.h"
#include "btrfs_inode.h"
#include "print-tree.h"
#include "export.h"
#include "compat.h"
2009-01-05 21:25:51 -05:00
#define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
parent_objectid) / 4)
#define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \
parent_root_objectid) / 4)
#define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4)
2008-07-21 02:01:56 +05:30
2012-04-02 14:34:06 -04:00
static int btrfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
struct inode *parent)
2008-07-21 02:01:56 +05:30
{
struct btrfs_fid *fid = (struct btrfs_fid *)fh;
int len = *max_len;
int type;
2012-04-02 14:34:06 -04:00
if (parent && (len < BTRFS_FID_SIZE_CONNECTABLE)) {
*max_len = BTRFS_FID_SIZE_CONNECTABLE;
2008-07-21 02:01:56 +05:30
return 255;
} else if (len < BTRFS_FID_SIZE_NON_CONNECTABLE) {
*max_len = BTRFS_FID_SIZE_NON_CONNECTABLE;
return 255;
}
2008-07-21 02:01:56 +05:30
len = BTRFS_FID_SIZE_NON_CONNECTABLE;
type = FILEID_BTRFS_WITHOUT_PARENT;
2011-04-20 10:31:50 +08:00
fid->objectid = btrfs_ino(inode);
2008-07-21 02:01:56 +05:30
fid->root_objectid = BTRFS_I(inode)->root->objectid;
fid->gen = inode->i_generation;
2012-04-02 14:34:06 -04:00
if (parent) {
2008-07-21 02:01:56 +05:30
u64 parent_root_id;
fid->parent_objectid = BTRFS_I(parent)->location.objectid;
fid->parent_gen = parent->i_generation;
parent_root_id = BTRFS_I(parent)->root->objectid;
if (parent_root_id != fid->root_objectid) {
fid->parent_root_objectid = parent_root_id;
len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
type = FILEID_BTRFS_WITH_PARENT_ROOT;
} else {
len = BTRFS_FID_SIZE_CONNECTABLE;
type = FILEID_BTRFS_WITH_PARENT;
}
}
*max_len = len;
return type;
}
static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
2009-09-21 16:00:26 -04:00
u64 root_objectid, u32 generation,
int check_generation)
2008-07-21 02:01:56 +05:30
{
struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2008-07-21 02:01:56 +05:30
struct btrfs_root *root;
struct inode *inode;
struct btrfs_key key;
2009-09-21 16:00:26 -04:00
int index;
int err = 0;
if (objectid < BTRFS_FIRST_FREE_OBJECTID)
return ERR_PTR(-ESTALE);
2008-07-21 02:01:56 +05:30
2008-08-19 22:20:17 +01:00
key.objectid = root_objectid;
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
key.offset = (u64)-1;
2009-09-21 16:00:26 -04:00
index = srcu_read_lock(&fs_info->subvol_srcu);
root = btrfs_read_fs_root_no_name(fs_info, &key);
if (IS_ERR(root)) {
err = PTR_ERR(root);
goto fail;
}
if (btrfs_root_refs(&root->root_item) == 0) {
err = -ENOENT;
goto fail;
}
2008-08-19 22:20:17 +01:00
2008-07-21 02:01:56 +05:30
key.objectid = objectid;
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
key.offset = 0;
2009-12-04 17:38:27 +00:00
inode = btrfs_iget(sb, &key, root, NULL);
2009-09-21 16:00:26 -04:00
if (IS_ERR(inode)) {
err = PTR_ERR(inode);
goto fail;
}
srcu_read_unlock(&fs_info->subvol_srcu, index);
2008-07-21 02:01:56 +05:30
2009-09-21 16:00:26 -04:00
if (check_generation && generation != inode->i_generation) {
2008-07-21 02:01:56 +05:30
iput(inode);
return ERR_PTR(-ESTALE);
}
2010-12-20 10:56:06 -05:00
return d_obtain_alias(inode);
2009-09-21 16:00:26 -04:00
fail:
srcu_read_unlock(&fs_info->subvol_srcu, index);
return ERR_PTR(err);
2008-07-21 02:01:56 +05:30
}
static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
int fh_len, int fh_type)
{
struct btrfs_fid *fid = (struct btrfs_fid *) fh;
u64 objectid, root_objectid;
u32 generation;
if (fh_type == FILEID_BTRFS_WITH_PARENT) {
if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
return NULL;
root_objectid = fid->root_objectid;
} else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
return NULL;
root_objectid = fid->parent_root_objectid;
} else
return NULL;
objectid = fid->parent_objectid;
generation = fid->parent_gen;
2009-09-21 16:00:26 -04:00
return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
2008-07-21 02:01:56 +05:30
}
static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
int fh_len, int fh_type)
{
struct btrfs_fid *fid = (struct btrfs_fid *) fh;
u64 objectid, root_objectid;
u32 generation;
if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
(fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
(fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
return NULL;
objectid = fid->objectid;
root_objectid = fid->root_objectid;
generation = fid->gen;
2009-09-21 16:00:26 -04:00
return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
2008-07-21 02:01:56 +05:30
}
static struct dentry *btrfs_get_parent(struct dentry *child)
{
struct inode *dir = child->d_inode;
struct btrfs_root *root = BTRFS_I(dir)->root;
struct btrfs_path *path;
struct extent_buffer *leaf;
2009-09-21 16:00:26 -04:00
struct btrfs_root_ref *ref;
struct btrfs_key key;
struct btrfs_key found_key;
2008-07-21 02:01:56 +05:30
int ret;
path = btrfs_alloc_path();
if (!path)
return ERR_PTR(-ENOMEM);
2008-07-21 02:01:56 +05:30
2011-04-20 10:31:50 +08:00
if (btrfs_ino(dir) == BTRFS_FIRST_FREE_OBJECTID) {
2009-09-21 16:00:26 -04:00
key.objectid = root->root_key.objectid;
key.type = BTRFS_ROOT_BACKREF_KEY;
key.offset = (u64)-1;
root = root->fs_info->tree_root;
} else {
2011-04-20 10:31:50 +08:00
key.objectid = btrfs_ino(dir);
2009-09-21 16:00:26 -04:00
key.type = BTRFS_INODE_REF_KEY;
key.offset = (u64)-1;
}
2008-07-21 02:01:56 +05:30
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2009-09-21 16:00:26 -04:00
if (ret < 0)
goto fail;
BUG_ON(ret == 0); /* Key with offset of -1 found */
2009-09-21 16:00:26 -04:00
if (path->slots[0] == 0) {
ret = -ENOENT;
goto fail;
}
2009-09-21 16:00:26 -04:00
path->slots[0]--;
2008-07-21 02:01:56 +05:30
leaf = path->nodes[0];
2009-09-21 16:00:26 -04:00
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
if (found_key.objectid != key.objectid || found_key.type != key.type) {
ret = -ENOENT;
goto fail;
2008-07-21 02:01:56 +05:30
}
2009-09-21 16:00:26 -04:00
if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
ref = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_root_ref);
key.objectid = btrfs_root_ref_dirid(leaf, ref);
} else {
key.objectid = found_key.offset;
}
2008-07-21 02:01:56 +05:30
btrfs_free_path(path);
2009-09-21 16:00:26 -04:00
if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
return btrfs_get_dentry(root->fs_info->sb, key.objectid,
found_key.offset, 0, 0);
}
2008-08-19 22:20:17 +01:00
2009-09-21 16:00:26 -04:00
key.type = BTRFS_INODE_ITEM_KEY;
2008-07-21 02:01:56 +05:30
key.offset = 0;
2010-12-20 10:56:06 -05:00
return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
2009-09-21 16:00:26 -04:00
fail:
btrfs_free_path(path);
return ERR_PTR(ret);
2008-07-21 02:01:56 +05:30
}
2010-11-17 18:54:54 +00:00
static int btrfs_get_name(struct dentry *parent, char *name,
struct dentry *child)
{
struct inode *inode = child->d_inode;
struct inode *dir = parent->d_inode;
struct btrfs_path *path;
struct btrfs_root *root = BTRFS_I(dir)->root;
struct btrfs_inode_ref *iref;
struct btrfs_root_ref *rref;
struct extent_buffer *leaf;
unsigned long name_ptr;
struct btrfs_key key;
int name_len;
int ret;
2011-04-20 10:31:50 +08:00
u64 ino;
2010-11-17 18:54:54 +00:00
if (!dir || !inode)
return -EINVAL;
if (!S_ISDIR(dir->i_mode))
return -EINVAL;
2011-04-20 10:31:50 +08:00
ino = btrfs_ino(inode);
2010-11-17 18:54:54 +00:00
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
path->leave_spinning = 1;
2011-04-20 10:31:50 +08:00
if (ino == BTRFS_FIRST_FREE_OBJECTID) {
2010-11-17 18:54:54 +00:00
key.objectid = BTRFS_I(inode)->root->root_key.objectid;
key.type = BTRFS_ROOT_BACKREF_KEY;
key.offset = (u64)-1;
root = root->fs_info->tree_root;
} else {
2011-04-20 10:31:50 +08:00
key.objectid = ino;
key.offset = btrfs_ino(dir);
2010-11-17 18:54:54 +00:00
key.type = BTRFS_INODE_REF_KEY;
}
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0) {
btrfs_free_path(path);
return ret;
} else if (ret > 0) {
2011-04-20 10:31:50 +08:00
if (ino == BTRFS_FIRST_FREE_OBJECTID) {
2010-11-17 18:54:54 +00:00
path->slots[0]--;
} else {
btrfs_free_path(path);
return -ENOENT;
}
}
leaf = path->nodes[0];
2011-04-20 10:31:50 +08:00
if (ino == BTRFS_FIRST_FREE_OBJECTID) {
rref = btrfs_item_ptr(leaf, path->slots[0],
2010-11-17 18:54:54 +00:00
struct btrfs_root_ref);
2011-04-20 10:31:50 +08:00
name_ptr = (unsigned long)(rref + 1);
name_len = btrfs_root_ref_name_len(leaf, rref);
2010-11-17 18:54:54 +00:00
} else {
iref = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_inode_ref);
name_ptr = (unsigned long)(iref + 1);
name_len = btrfs_inode_ref_name_len(leaf, iref);
}
read_extent_buffer(leaf, name, name_ptr, name_len);
btrfs_free_path(path);
/*
* have to add the null termination to make sure that reconnect_path
* gets the right len for strlen
*/
name[name_len] = '\0';
return 0;
}
2008-07-21 02:01:56 +05:30
const struct export_operations btrfs_export_ops = {
.encode_fh = btrfs_encode_fh,
.fh_to_dentry = btrfs_fh_to_dentry,
.fh_to_parent = btrfs_fh_to_parent,
.get_parent = btrfs_get_parent,
2010-11-17 18:54:54 +00:00
.get_name = btrfs_get_name,
2008-07-21 02:01:56 +05:30
};