ext4 crypto: add symlink encryption

Signed-off-by: Uday Savagaonkar <savagaon@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
这个提交包含在:
Theodore Ts'o
2015-04-16 01:55:00 -04:00
父节点 4461471107
当前提交 f348c25232
修改 5 个文件,包含 184 行新增23 行删除

查看文件

@@ -3193,16 +3193,24 @@ static int ext4_symlink(struct inode *dir,
{
handle_t *handle;
struct inode *inode;
int l, err, retries = 0;
int err, len = strlen(symname);
int credits;
bool encryption_required;
struct ext4_str disk_link;
struct ext4_encrypted_symlink_data *sd = NULL;
l = strlen(symname)+1;
if (l > dir->i_sb->s_blocksize)
disk_link.len = len + 1;
disk_link.name = (char *) symname;
encryption_required = ext4_encrypted_inode(dir);
if (encryption_required)
disk_link.len = encrypted_symlink_data_len(len) + 1;
if (disk_link.len > dir->i_sb->s_blocksize)
return -ENAMETOOLONG;
dquot_initialize(dir);
if (l > EXT4_N_BLOCKS * 4) {
if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
/*
* For non-fast symlinks, we just allocate inode and put it on
* orphan list in the first transaction => we need bitmap,
@@ -3221,16 +3229,49 @@ static int ext4_symlink(struct inode *dir,
credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
}
retry:
inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
&dentry->d_name, 0, NULL,
EXT4_HT_DIR, credits);
handle = ext4_journal_current_handle();
err = PTR_ERR(inode);
if (IS_ERR(inode))
goto out_stop;
if (IS_ERR(inode)) {
if (handle)
ext4_journal_stop(handle);
return PTR_ERR(inode);
}
if (l > EXT4_N_BLOCKS * 4) {
if (encryption_required) {
struct ext4_fname_crypto_ctx *ctx = NULL;
struct qstr istr;
struct ext4_str ostr;
sd = kzalloc(disk_link.len, GFP_NOFS);
if (!sd) {
err = -ENOMEM;
goto err_drop_inode;
}
err = ext4_inherit_context(dir, inode);
if (err)
goto err_drop_inode;
ctx = ext4_get_fname_crypto_ctx(inode,
inode->i_sb->s_blocksize);
if (IS_ERR_OR_NULL(ctx)) {
/* We just set the policy, so ctx should not be NULL */
err = (ctx == NULL) ? -EIO : PTR_ERR(ctx);
goto err_drop_inode;
}
istr.name = (const unsigned char *) symname;
istr.len = len;
ostr.name = sd->encrypted_path;
err = ext4_fname_usr_to_disk(ctx, &istr, &ostr);
ext4_put_fname_crypto_ctx(&ctx);
if (err < 0)
goto err_drop_inode;
sd->len = cpu_to_le16(ostr.len);
disk_link.name = (char *) sd;
}
if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
inode->i_op = &ext4_symlink_inode_operations;
ext4_set_aops(inode);
/*
@@ -3246,9 +3287,10 @@ retry:
drop_nlink(inode);
err = ext4_orphan_add(handle, inode);
ext4_journal_stop(handle);
handle = NULL;
if (err)
goto err_drop_inode;
err = __page_symlink(inode, symname, l, 1);
err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
if (err)
goto err_drop_inode;
/*
@@ -3260,34 +3302,37 @@ retry:
EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
if (IS_ERR(handle)) {
err = PTR_ERR(handle);
handle = NULL;
goto err_drop_inode;
}
set_nlink(inode, 1);
err = ext4_orphan_del(handle, inode);
if (err) {
ext4_journal_stop(handle);
clear_nlink(inode);
if (err)
goto err_drop_inode;
}
} else {
/* clear the extent format for fast symlink */
ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
inode->i_op = &ext4_fast_symlink_inode_operations;
memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
inode->i_size = l-1;
inode->i_op = encryption_required ?
&ext4_symlink_inode_operations :
&ext4_fast_symlink_inode_operations;
memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
disk_link.len);
inode->i_size = disk_link.len - 1;
}
EXT4_I(inode)->i_disksize = inode->i_size;
err = ext4_add_nondir(handle, dentry, inode);
if (!err && IS_DIRSYNC(dir))
ext4_handle_sync(handle);
out_stop:
if (handle)
ext4_journal_stop(handle);
if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
goto retry;
kfree(sd);
return err;
err_drop_inode:
if (handle)
ext4_journal_stop(handle);
kfree(sd);
clear_nlink(inode);
unlock_new_inode(inode);
iput(inode);
return err;