2008-07-21 04:31:56 +08:00
|
|
|
#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-06 10:25:51 +08: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 04:31:56 +08:00
|
|
|
|
|
|
|
static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
|
|
|
|
int connectable)
|
|
|
|
{
|
|
|
|
struct btrfs_fid *fid = (struct btrfs_fid *)fh;
|
|
|
|
struct inode *inode = dentry->d_inode;
|
|
|
|
int len = *max_len;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) ||
|
|
|
|
(connectable && len < BTRFS_FID_SIZE_CONNECTABLE))
|
|
|
|
return 255;
|
|
|
|
|
|
|
|
len = BTRFS_FID_SIZE_NON_CONNECTABLE;
|
|
|
|
type = FILEID_BTRFS_WITHOUT_PARENT;
|
|
|
|
|
2009-09-22 04:00:26 +08:00
|
|
|
fid->objectid = inode->i_ino;
|
2008-07-21 04:31:56 +08:00
|
|
|
fid->root_objectid = BTRFS_I(inode)->root->objectid;
|
|
|
|
fid->gen = inode->i_generation;
|
|
|
|
|
|
|
|
if (connectable && !S_ISDIR(inode->i_mode)) {
|
|
|
|
struct inode *parent;
|
|
|
|
u64 parent_root_id;
|
|
|
|
|
|
|
|
spin_lock(&dentry->d_lock);
|
|
|
|
|
|
|
|
parent = dentry->d_parent->d_inode;
|
|
|
|
fid->parent_objectid = BTRFS_I(parent)->location.objectid;
|
|
|
|
fid->parent_gen = parent->i_generation;
|
|
|
|
parent_root_id = BTRFS_I(parent)->root->objectid;
|
|
|
|
|
|
|
|
spin_unlock(&dentry->d_lock);
|
|
|
|
|
|
|
|
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-22 04:00:26 +08:00
|
|
|
u64 root_objectid, u32 generation,
|
|
|
|
int check_generation)
|
2008-07-21 04:31:56 +08:00
|
|
|
{
|
2009-09-22 04:00:26 +08:00
|
|
|
struct btrfs_fs_info *fs_info = btrfs_sb(sb)->fs_info;
|
2008-07-21 04:31:56 +08:00
|
|
|
struct btrfs_root *root;
|
2009-09-22 04:00:26 +08:00
|
|
|
struct dentry *dentry;
|
2008-07-21 04:31:56 +08:00
|
|
|
struct inode *inode;
|
|
|
|
struct btrfs_key key;
|
2009-09-22 04:00:26 +08:00
|
|
|
int index;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (objectid < BTRFS_FIRST_FREE_OBJECTID)
|
|
|
|
return ERR_PTR(-ESTALE);
|
2008-07-21 04:31:56 +08:00
|
|
|
|
2008-08-20 05:20:17 +08:00
|
|
|
key.objectid = root_objectid;
|
|
|
|
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
|
|
|
|
key.offset = (u64)-1;
|
|
|
|
|
2009-09-22 04:00:26 +08: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-20 05:20:17 +08:00
|
|
|
|
2008-07-21 04:31:56 +08:00
|
|
|
key.objectid = objectid;
|
|
|
|
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
|
|
|
|
key.offset = 0;
|
|
|
|
|
Btrfs: Mixed back reference (FORWARD ROLLING FORMAT CHANGE)
This commit introduces a new kind of back reference for btrfs metadata.
Once a filesystem has been mounted with this commit, IT WILL NO LONGER
BE MOUNTABLE BY OLDER KERNELS.
When a tree block in subvolume tree is cow'd, the reference counts of all
extents it points to are increased by one. At transaction commit time,
the old root of the subvolume is recorded in a "dead root" data structure,
and the btree it points to is later walked, dropping reference counts
and freeing any blocks where the reference count goes to 0.
The increments done during cow and decrements done after commit cancel out,
and the walk is a very expensive way to go about freeing the blocks that
are no longer referenced by the new btree root. This commit reduces the
transaction overhead by avoiding the need for dead root records.
When a non-shared tree block is cow'd, we free the old block at once, and the
new block inherits old block's references. When a tree block with reference
count > 1 is cow'd, we increase the reference counts of all extents
the new block points to by one, and decrease the old block's reference count by
one.
This dead tree avoidance code removes the need to modify the reference
counts of lower level extents when a non-shared tree block is cow'd.
But we still need to update back ref for all pointers in the block.
This is because the location of the block is recorded in the back ref
item.
We can solve this by introducing a new type of back ref. The new
back ref provides information about pointer's key, level and in which
tree the pointer lives. This information allow us to find the pointer
by searching the tree. The shortcoming of the new back ref is that it
only works for pointers in tree blocks referenced by their owner trees.
This is mostly a problem for snapshots, where resolving one of these
fuzzy back references would be O(number_of_snapshots) and quite slow.
The solution used here is to use the fuzzy back references in the common
case where a given tree block is only referenced by one root,
and use the full back references when multiple roots have a reference
on a given block.
This commit adds per subvolume red-black tree to keep trace of cached
inodes. The red-black tree helps the balancing code to find cached
inodes whose inode numbers within a given range.
This commit improves the balancing code by introducing several data
structures to keep the state of balancing. The most important one
is the back ref cache. It caches how the upper level tree blocks are
referenced. This greatly reduce the overhead of checking back ref.
The improved balancing code scales significantly better with a large
number of snapshots.
This is a very large commit and was written in a number of
pieces. But, they depend heavily on the disk format change and were
squashed together to make sure git bisect didn't end up in a
bad state wrt space balancing or the format change.
Signed-off-by: Yan Zheng <zheng.yan@oracle.com>
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-06-10 22:45:14 +08:00
|
|
|
inode = btrfs_iget(sb, &key, root);
|
2009-09-22 04:00:26 +08:00
|
|
|
if (IS_ERR(inode)) {
|
|
|
|
err = PTR_ERR(inode);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcu_read_unlock(&fs_info->subvol_srcu, index);
|
2008-07-21 04:31:56 +08:00
|
|
|
|
2009-09-22 04:00:26 +08:00
|
|
|
if (check_generation && generation != inode->i_generation) {
|
2008-07-21 04:31:56 +08:00
|
|
|
iput(inode);
|
|
|
|
return ERR_PTR(-ESTALE);
|
|
|
|
}
|
|
|
|
|
2009-09-22 04:00:26 +08:00
|
|
|
dentry = d_obtain_alias(inode);
|
|
|
|
if (!IS_ERR(dentry))
|
|
|
|
dentry->d_op = &btrfs_dentry_operations;
|
|
|
|
return dentry;
|
|
|
|
fail:
|
|
|
|
srcu_read_unlock(&fs_info->subvol_srcu, index);
|
|
|
|
return ERR_PTR(err);
|
2008-07-21 04:31:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-22 04:00:26 +08:00
|
|
|
return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
|
2008-07-21 04:31:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-22 04:00:26 +08:00
|
|
|
return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
|
2008-07-21 04:31:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct dentry *btrfs_get_parent(struct dentry *child)
|
|
|
|
{
|
|
|
|
struct inode *dir = child->d_inode;
|
2009-09-22 04:00:26 +08:00
|
|
|
static struct dentry *dentry;
|
2008-07-21 04:31:56 +08:00
|
|
|
struct btrfs_root *root = BTRFS_I(dir)->root;
|
|
|
|
struct btrfs_path *path;
|
|
|
|
struct extent_buffer *leaf;
|
2009-09-22 04:00:26 +08:00
|
|
|
struct btrfs_root_ref *ref;
|
|
|
|
struct btrfs_key key;
|
|
|
|
struct btrfs_key found_key;
|
2008-07-21 04:31:56 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
path = btrfs_alloc_path();
|
|
|
|
|
2009-09-22 04:00:26 +08:00
|
|
|
if (dir->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
|
|
|
|
key.objectid = root->root_key.objectid;
|
|
|
|
key.type = BTRFS_ROOT_BACKREF_KEY;
|
|
|
|
key.offset = (u64)-1;
|
|
|
|
root = root->fs_info->tree_root;
|
|
|
|
} else {
|
|
|
|
key.objectid = dir->i_ino;
|
|
|
|
key.type = BTRFS_INODE_REF_KEY;
|
|
|
|
key.offset = (u64)-1;
|
|
|
|
}
|
2008-07-21 04:31:56 +08:00
|
|
|
|
2008-08-19 05:50:22 +08:00
|
|
|
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
|
2009-09-22 04:00:26 +08:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
BUG_ON(ret == 0);
|
|
|
|
if (path->slots[0] == 0) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto fail;
|
2008-08-20 05:33:04 +08:00
|
|
|
}
|
2009-09-22 04:00:26 +08:00
|
|
|
|
|
|
|
path->slots[0]--;
|
2008-07-21 04:31:56 +08:00
|
|
|
leaf = path->nodes[0];
|
2009-09-22 04:00:26 +08: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 04:31:56 +08:00
|
|
|
}
|
|
|
|
|
2009-09-22 04:00:26 +08: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 04:31:56 +08:00
|
|
|
btrfs_free_path(path);
|
|
|
|
|
2009-09-22 04:00:26 +08: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-20 05:20:17 +08:00
|
|
|
|
2009-09-22 04:00:26 +08:00
|
|
|
key.type = BTRFS_INODE_ITEM_KEY;
|
2008-07-21 04:31:56 +08:00
|
|
|
key.offset = 0;
|
2009-09-22 04:00:26 +08:00
|
|
|
dentry = d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root));
|
|
|
|
if (!IS_ERR(dentry))
|
|
|
|
dentry->d_op = &btrfs_dentry_operations;
|
|
|
|
return dentry;
|
|
|
|
fail:
|
|
|
|
btrfs_free_path(path);
|
|
|
|
return ERR_PTR(ret);
|
2008-07-21 04:31:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|