* Boston, MA 021110-1307, USA.
*/
-#include <linux/module.h>
#include <linux/buffer_head.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
static struct inode_operations btrfs_dir_inode_operations;
static struct inode_operations btrfs_symlink_inode_operations;
static struct inode_operations btrfs_dir_ro_inode_operations;
+static struct inode_operations btrfs_special_inode_operations;
static struct inode_operations btrfs_file_inode_operations;
static struct address_space_operations btrfs_aops;
static struct address_space_operations btrfs_symlink_aops;
struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_key location;
u64 alloc_group_block;
+ u32 rdev;
int ret;
path = btrfs_alloc_path();
inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
inode->i_blocks = btrfs_inode_nblocks(inode_item);
inode->i_generation = btrfs_inode_generation(inode_item);
+ inode->i_rdev = 0;
+ rdev = btrfs_inode_rdev(inode_item);
alloc_group_block = btrfs_inode_block_group(inode_item);
BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
alloc_group_block);
mutex_unlock(&root->fs_info->fs_mutex);
switch (inode->i_mode & S_IFMT) {
-#if 0
- default:
- init_special_inode(inode, inode->i_mode,
- btrfs_inode_rdev(inode_item));
- break;
-#endif
case S_IFREG:
inode->i_mapping->a_ops = &btrfs_aops;
inode->i_fop = &btrfs_file_operations;
inode->i_op = &btrfs_symlink_inode_operations;
inode->i_mapping->a_ops = &btrfs_symlink_aops;
break;
+ default:
+ init_special_inode(inode, inode->i_mode, rdev);
+ break;
}
return;
btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
btrfs_set_inode_nblocks(item, inode->i_blocks);
btrfs_set_inode_generation(item, inode->i_generation);
+ btrfs_set_inode_rdev(item, inode->i_rdev);
btrfs_set_inode_block_group(item,
BTRFS_I(inode)->block_group->key.objectid);
}
struct btrfs_dir_item *di;
path = btrfs_alloc_path();
- BUG_ON(!path);
+ if (!path) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
name, name_len, -1);
if (IS_ERR(di)) {
}
objectid = btrfs_disk_key_objectid(&di->location);
ret = btrfs_delete_one_dir_name(trans, root, path, di);
- BUG_ON(ret);
+ if (ret)
+ goto err;
btrfs_release_path(root, path);
di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
goto err;
}
ret = btrfs_delete_one_dir_name(trans, root, path, di);
- BUG_ON(ret);
dentry->d_inode->i_ctime = dir->i_ctime;
err:
btrfs_free_path(path);
if (!ret) {
dir->i_size -= name_len * 2;
+ dir->i_mtime = dir->i_ctime = CURRENT_TIME;
btrfs_update_inode(trans, root, dir);
drop_nlink(dentry->d_inode);
- btrfs_update_inode(trans, root, dentry->d_inode);
+ ret = btrfs_update_inode(trans, root, dentry->d_inode);
dir->i_sb->s_dirt = 1;
}
return ret;
BUG_ON(!path);
ret = btrfs_lookup_inode(trans, root, path,
&BTRFS_I(inode)->location, -1);
- BUG_ON(ret);
- ret = btrfs_del_item(trans, root, path);
- BUG_ON(ret);
+ if (ret > 0)
+ ret = -ENOENT;
+ if (!ret)
+ ret = btrfs_del_item(trans, root, path);
btrfs_free_path(path);
return ret;
}
extent_num_blocks);
inode->i_blocks -= (orig_num_blocks -
extent_num_blocks) << 3;
- mark_buffer_dirty(path->nodes[0]);
+ btrfs_mark_buffer_dirty(path->nodes[0]);
} else {
extent_start =
btrfs_file_extent_disk_blocknr(fi);
}
if (del_item) {
ret = btrfs_del_item(trans, root, path);
- BUG_ON(ret);
+ if (ret)
+ goto error;
} else {
break;
}
page->index << PAGE_CACHE_SHIFT,
(page->index + 1) << PAGE_CACHE_SHIFT,
&alloc_hint);
- BUG_ON(ret);
+ if (ret)
+ goto out;
ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
alloc_hint, (u64)-1, &ins, 1);
- BUG_ON(ret);
+ if (ret)
+ goto out;
ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
page->index << PAGE_CACHE_SHIFT,
ins.objectid, 1, 1);
- BUG_ON(ret);
+ if (ret)
+ goto out;
SetPageChecked(page);
kaddr = kmap(page);
memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
flush_dcache_page(page);
- btrfs_csum_file_block(trans, root, inode->i_ino,
+ ret = btrfs_csum_file_block(trans, root, inode->i_ino,
page->index << PAGE_CACHE_SHIFT,
kaddr, PAGE_CACHE_SIZE);
kunmap(page);
btrfs_set_trans_block_group(trans, inode);
err = btrfs_insert_file_extent(trans, root, inode->i_ino,
pos, 0, 0, hole_size);
- BUG_ON(err);
btrfs_end_transaction(trans, root);
mutex_unlock(&root->fs_info->fs_mutex);
+ if (err)
+ return err;
}
out:
err = inode_setattr(inode, attr);
trans = btrfs_start_transaction(root, 1);
btrfs_set_trans_block_group(trans, inode);
ret = btrfs_truncate_in_trans(trans, root, inode);
- BUG_ON(ret);
- btrfs_free_inode(trans, root, inode);
+ if (ret)
+ goto no_delete_lock;
+ ret = btrfs_free_inode(trans, root, inode);
+ if (ret)
+ goto no_delete_lock;
btrfs_end_transaction(trans, root);
mutex_unlock(&root->fs_info->fs_mutex);
btrfs_btree_balance_dirty(root);
return;
+
+no_delete_lock:
+ btrfs_end_transaction(trans, root);
+ mutex_unlock(&root->fs_info->fs_mutex);
+ btrfs_btree_balance_dirty(root);
no_delete:
clear_inode(inode);
}
}
/*
- * This is somewhat expense, updating the tree every time the
+ * This is somewhat expensive, updating the tree every time the
* inode changes. But, it is most likely to find the inode in cache.
* FIXME, needs more benchmarking...there are no reasons other than performance
* to keep or drop this code.
btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
- BUG_ON(ret);
-
+ if (ret)
+ return ERR_PTR(ret);
insert_inode_hash(inode);
return inode;
}
int ret;
struct btrfs_key key;
struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
+ struct inode *parent_inode;
key.objectid = inode->i_ino;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
dentry->d_parent->d_inode->i_ino,
&key, btrfs_inode_type(inode));
if (ret == 0) {
- dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
+ parent_inode = dentry->d_parent->d_inode;
+ parent_inode->i_size += dentry->d_name.len * 2;
+ parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
ret = btrfs_update_inode(trans, root,
dentry->d_parent->d_inode);
}
return err;
}
+static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
+ int mode, dev_t rdev)
+{
+ struct btrfs_trans_handle *trans;
+ struct btrfs_root *root = BTRFS_I(dir)->root;
+ struct inode *inode;
+ int err;
+ int drop_inode = 0;
+ u64 objectid;
+
+ if (!new_valid_dev(rdev))
+ return -EINVAL;
+
+ mutex_lock(&root->fs_info->fs_mutex);
+ trans = btrfs_start_transaction(root, 1);
+ btrfs_set_trans_block_group(trans, dir);
+
+ err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
+ if (err) {
+ err = -ENOSPC;
+ goto out_unlock;
+ }
+
+ inode = btrfs_new_inode(trans, root, objectid,
+ BTRFS_I(dir)->block_group, mode);
+ err = PTR_ERR(inode);
+ if (IS_ERR(inode))
+ goto out_unlock;
+
+ btrfs_set_trans_block_group(trans, inode);
+ err = btrfs_add_nondir(trans, dentry, inode);
+ if (err)
+ drop_inode = 1;
+ else {
+ inode->i_op = &btrfs_special_inode_operations;
+ init_special_inode(inode, inode->i_mode, rdev);
+ }
+ dir->i_sb->s_dirt = 1;
+ btrfs_update_inode_block_group(trans, inode);
+ btrfs_update_inode_block_group(trans, dir);
+out_unlock:
+ btrfs_end_transaction(trans, root);
+ mutex_unlock(&root->fs_info->fs_mutex);
+
+ if (drop_inode) {
+ inode_dec_link_count(inode);
+ iput(inode);
+ }
+ btrfs_btree_balance_dirty(root);
+ return err;
+}
+
static int btrfs_create(struct inode *dir, struct dentry *dentry,
int mode, struct nameidata *nd)
{
drop_inode = 1;
dir->i_sb->s_dirt = 1;
btrfs_update_inode_block_group(trans, dir);
- btrfs_update_inode(trans, root, inode);
+ err = btrfs_update_inode(trans, root, inode);
+ if (err)
+ drop_inode = 1;
btrfs_end_transaction(trans, root);
mutex_unlock(&root->fs_info->fs_mutex);
ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1, alloc_hint, (u64)-1,
&ins, 1);
- BUG_ON(ret);
+ if (ret) {
+ err = ret;
+ goto out;
+ }
ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
iblock << inode->i_blkbits,
ins.objectid, ins.offset,
ins.offset);
- BUG_ON(ret);
+ if (ret) {
+ err = ret;
+ goto out;
+ }
btrfs_map_bh_to_logical(root, result, ins.objectid);
}
out:
- if (trans)
- err = btrfs_end_transaction(trans, root);
+ if (trans) {
+ ret = btrfs_end_transaction(trans, root);
+ if (!err)
+ err = ret;
+ }
btrfs_free_path(path);
return err;
}
return err;
}
-int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
- struct buffer_head *result, int create)
+static int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
+ struct buffer_head *result, int create)
{
int ret;
struct btrfs_root *root = BTRFS_I(inode)->root;
/* a csum that isn't present is a preallocated region. */
if (ret == -ENOENT || ret == -EFBIG)
ret = 0;
- result->b_private = 0;
+ result->b_private = NULL;
goto out;
}
memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE);
trans = btrfs_start_transaction(root, 1);
btrfs_set_trans_block_group(trans, inode);
kaddr = kmap(page);
- ret = btrfs_csum_file_block(trans, root, inode->i_ino,
+ btrfs_csum_file_block(trans, root, inode->i_ino,
off, kaddr + bh_offset(bh),
bh->b_size);
kunmap(page);
- BUG_ON(ret);
ret = btrfs_end_transaction(trans, root);
BUG_ON(ret);
mutex_unlock(&root->fs_info->fs_mutex);
/* FIXME, add redo link to tree so we don't leak on crash */
ret = btrfs_truncate_in_trans(trans, root, inode);
- BUG_ON(ret);
btrfs_update_inode(trans, root, inode);
ret = btrfs_end_transaction(trans, root);
BUG_ON(ret);
struct inode *inode;
struct inode *dir;
int ret;
+ int err;
u64 objectid;
u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
BUG_ON(!trans);
subvol = btrfs_alloc_free_block(trans, root, 0);
- if (subvol == NULL)
- return -ENOSPC;
+ if (IS_ERR(subvol))
+ return PTR_ERR(subvol);
leaf = btrfs_buffer_leaf(subvol);
btrfs_set_header_nritems(&leaf->header, 0);
btrfs_set_header_level(&leaf->header, 0);
btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
sizeof(leaf->header.fsid));
- mark_buffer_dirty(subvol);
+ btrfs_mark_buffer_dirty(subvol);
inode_item = &root_item.inode;
memset(inode_item, 0, sizeof(*inode_item));
btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
btrfs_set_root_refs(&root_item, 1);
+ memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
+ root_item.drop_level = 0;
brelse(subvol);
subvol = NULL;
ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
0, &objectid);
- BUG_ON(ret);
+ if (ret)
+ goto fail;
btrfs_set_root_dirid(&root_item, new_dirid);
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
&root_item);
- BUG_ON(ret);
+ if (ret)
+ goto fail;
/*
* insert the directory item
ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
name, namelen, dir->i_ino, &key,
BTRFS_FT_DIR);
- BUG_ON(ret);
+ if (ret)
+ goto fail;
ret = btrfs_commit_transaction(trans, root);
- BUG_ON(ret);
+ if (ret)
+ goto fail_commit;
new_root = btrfs_read_fs_root(root->fs_info, &key);
BUG_ON(!new_root);
inode = btrfs_new_inode(trans, new_root, new_dirid,
BTRFS_I(dir)->block_group, S_IFDIR | 0700);
+ if (IS_ERR(inode))
+ goto fail;
inode->i_op = &btrfs_dir_inode_operations;
inode->i_fop = &btrfs_dir_file_operations;
new_root->inode = inode;
ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
- BUG_ON(ret);
+ if (ret)
+ goto fail;
inode->i_nlink = 1;
inode->i_size = 6;
ret = btrfs_update_inode(trans, new_root, inode);
- BUG_ON(ret);
-
- ret = btrfs_commit_transaction(trans, new_root);
- BUG_ON(ret);
-
+ if (ret)
+ goto fail;
+fail:
+ err = btrfs_commit_transaction(trans, root);
+ if (err && !ret)
+ ret = err;
+fail_commit:
mutex_unlock(&root->fs_info->fs_mutex);
btrfs_btree_balance_dirty(root);
- return 0;
+ return ret;
}
static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
struct btrfs_key key;
struct btrfs_root_item new_root_item;
int ret;
+ int err;
u64 objectid;
if (!root->ref_cows)
BUG_ON(!trans);
ret = btrfs_update_inode(trans, root, root->inode);
- BUG_ON(ret);
+ if (ret)
+ goto fail;
ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
0, &objectid);
- BUG_ON(ret);
+ if (ret)
+ goto fail;
memcpy(&new_root_item, &root->root_item,
sizeof(new_root_item));
ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
&new_root_item);
- BUG_ON(ret);
+ if (ret)
+ goto fail;
/*
* insert the directory item
root->fs_info->sb->s_root->d_inode->i_ino,
&key, BTRFS_FT_DIR);
- BUG_ON(ret);
+ if (ret)
+ goto fail;
ret = btrfs_inc_root_ref(trans, root);
- BUG_ON(ret);
+ if (ret)
+ goto fail;
- ret = btrfs_commit_transaction(trans, root);
- BUG_ON(ret);
+fail:
+ err = btrfs_commit_transaction(trans, root);
+ if (err && !ret)
+ ret = err;
mutex_unlock(&root->fs_info->fs_mutex);
btrfs_btree_balance_dirty(root);
- return 0;
+ return ret;
}
int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
btrfs_free_path(path);
if (di && !IS_ERR(di))
return -EEXIST;
+ if (IS_ERR(di))
+ return PTR_ERR(di);
if (root == root->fs_info->tree_root)
ret = create_subvol(root, vol_args.name, namelen);
else
ret = create_snapshot(root, vol_args.name, namelen);
- WARN_ON(ret);
break;
default:
return -ENOTTY;
old_parent_oid = btrfs_disk_key_objectid(&di->location);
ret = btrfs_del_item(trans, root, path);
if (ret) {
- ret = -EIO;
goto out_fail;
}
btrfs_release_path(root, path);
}
ret = btrfs_del_item(trans, root, path);
if (ret) {
- ret = -EIO;
goto out_fail;
}
btrfs_release_path(root, path);
clear_nlink(new_inode);
else
drop_nlink(new_inode);
- btrfs_update_inode(trans, root, new_inode);
+ ret = btrfs_update_inode(trans, root, new_inode);
+ if (ret)
+ goto out_fail;
}
ret = btrfs_add_link(trans, new_dentry, old_inode);
if (ret)
datasize = btrfs_file_extent_calc_inline_size(name_len);
err = btrfs_insert_empty_item(trans, root, path, &key,
datasize);
- BUG_ON(err);
+ if (err) {
+ drop_inode = 1;
+ goto out_unlock;
+ }
ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
path->slots[0], struct btrfs_file_extent_item);
btrfs_set_file_extent_generation(ei, trans->transid);
ptr = btrfs_file_extent_inline_start(ei);
btrfs_memcpy(root, path->nodes[0]->b_data,
ptr, symname, name_len);
- mark_buffer_dirty(path->nodes[0]);
+ btrfs_mark_buffer_dirty(path->nodes[0]);
btrfs_free_path(path);
inode->i_op = &btrfs_symlink_inode_operations;
inode->i_mapping->a_ops = &btrfs_symlink_aops;
inode->i_size = name_len - 1;
- btrfs_update_inode(trans, root, inode);
- err = 0;
+ err = btrfs_update_inode(trans, root, inode);
+ if (err)
+ drop_inode = 1;
out_unlock:
btrfs_end_transaction(trans, root);
mutex_unlock(&root->fs_info->fs_mutex);
-
if (drop_inode) {
inode_dec_link_count(inode);
iput(inode);
.rename = btrfs_rename,
.symlink = btrfs_symlink,
.setattr = btrfs_setattr,
+ .mknod = btrfs_mknod,
};
static struct inode_operations btrfs_dir_ro_inode_operations = {
.setattr = btrfs_setattr,
};
+static struct inode_operations btrfs_special_inode_operations = {
+ .getattr = btrfs_getattr,
+ .setattr = btrfs_setattr,
+};
+
static struct inode_operations btrfs_symlink_inode_operations = {
.readlink = generic_readlink,
.follow_link = page_follow_link_light,