Commit 2ed92554 authored by Michael Halcrow's avatar Michael Halcrow Committed by Linus Torvalds

eCryptfs: make open, truncate, and setattr use persistent file

Rather than open a new lower file for every eCryptfs file that is opened,
truncated, or setattr'd, instead use the existing lower persistent file for
the eCryptfs inode.  Change truncate to use read_write.c functions.  Change
ecryptfs_getxattr() to use the common ecryptfs_getxattr_lower() function.
Signed-off-by: default avatarMichael Halcrow <mhalcrow@us.ibm.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent d7cdc5fe
...@@ -1673,7 +1673,7 @@ static int ecryptfs_read_headers_virt(char *page_virt, ...@@ -1673,7 +1673,7 @@ static int ecryptfs_read_headers_virt(char *page_virt,
/** /**
* ecryptfs_read_xattr_region * ecryptfs_read_xattr_region
* @page_virt: The vitual address into which to read the xattr data * @page_virt: The vitual address into which to read the xattr data
* @ecryptfs_dentry: The eCryptfs dentry * @ecryptfs_inode: The eCryptfs inode
* *
* Attempts to read the crypto metadata from the extended attribute * Attempts to read the crypto metadata from the extended attribute
* region of the lower file. * region of the lower file.
......
...@@ -187,11 +187,7 @@ static int ecryptfs_open(struct inode *inode, struct file *file) ...@@ -187,11 +187,7 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
/* Private value of ecryptfs_dentry allocated in /* Private value of ecryptfs_dentry allocated in
* ecryptfs_lookup() */ * ecryptfs_lookup() */
struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
struct inode *lower_inode = NULL;
struct file *lower_file = NULL;
struct vfsmount *lower_mnt;
struct ecryptfs_file_info *file_info; struct ecryptfs_file_info *file_info;
int lower_flags;
mount_crypt_stat = &ecryptfs_superblock_to_private( mount_crypt_stat = &ecryptfs_superblock_to_private(
ecryptfs_dentry->d_sb)->mount_crypt_stat; ecryptfs_dentry->d_sb)->mount_crypt_stat;
...@@ -219,26 +215,12 @@ static int ecryptfs_open(struct inode *inode, struct file *file) ...@@ -219,26 +215,12 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) { if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n"); ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
/* Policy code enabled in future release */ /* Policy code enabled in future release */
crypt_stat->flags |= ECRYPTFS_POLICY_APPLIED; crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
crypt_stat->flags |= ECRYPTFS_ENCRYPTED; | ECRYPTFS_ENCRYPTED);
} }
mutex_unlock(&crypt_stat->cs_mutex); mutex_unlock(&crypt_stat->cs_mutex);
lower_flags = file->f_flags; ecryptfs_set_file_lower(
if ((lower_flags & O_ACCMODE) == O_WRONLY) file, ecryptfs_inode_to_private(inode)->lower_file);
lower_flags = (lower_flags & O_ACCMODE) | O_RDWR;
if (file->f_flags & O_APPEND)
lower_flags &= ~O_APPEND;
lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
/* Corresponding fput() in ecryptfs_release() */
rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
lower_flags);
if (rc) {
ecryptfs_printk(KERN_ERR, "Error opening lower file\n");
goto out_puts;
}
ecryptfs_set_file_lower(file, lower_file);
/* Isn't this check the same as the one in lookup? */
lower_inode = lower_dentry->d_inode;
if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) { if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
ecryptfs_printk(KERN_DEBUG, "This is a directory\n"); ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
...@@ -260,7 +242,7 @@ static int ecryptfs_open(struct inode *inode, struct file *file) ...@@ -260,7 +242,7 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
"and plaintext passthrough mode is not " "and plaintext passthrough mode is not "
"enabled; returning -EIO\n"); "enabled; returning -EIO\n");
mutex_unlock(&crypt_stat->cs_mutex); mutex_unlock(&crypt_stat->cs_mutex);
goto out_puts; goto out_free;
} }
rc = 0; rc = 0;
crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
...@@ -272,11 +254,8 @@ static int ecryptfs_open(struct inode *inode, struct file *file) ...@@ -272,11 +254,8 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = [0x%.16x] " ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = [0x%.16x] "
"size: [0x%.16x]\n", inode, inode->i_ino, "size: [0x%.16x]\n", inode, inode->i_ino,
i_size_read(inode)); i_size_read(inode));
ecryptfs_set_file_lower(file, lower_file);
goto out; goto out;
out_puts: out_free:
mntput(lower_mnt);
dput(lower_dentry);
kmem_cache_free(ecryptfs_file_info_cache, kmem_cache_free(ecryptfs_file_info_cache,
ecryptfs_file_to_private(file)); ecryptfs_file_to_private(file));
out: out:
...@@ -296,20 +275,9 @@ static int ecryptfs_flush(struct file *file, fl_owner_t td) ...@@ -296,20 +275,9 @@ static int ecryptfs_flush(struct file *file, fl_owner_t td)
static int ecryptfs_release(struct inode *inode, struct file *file) static int ecryptfs_release(struct inode *inode, struct file *file)
{ {
struct file *lower_file = ecryptfs_file_to_lower(file); kmem_cache_free(ecryptfs_file_info_cache,
struct ecryptfs_file_info *file_info = ecryptfs_file_to_private(file); ecryptfs_file_to_private(file));
struct inode *lower_inode = ecryptfs_inode_to_lower(inode); return 0;
int rc;
rc = ecryptfs_close_lower_file(lower_file);
if (rc) {
printk(KERN_ERR "Error closing lower_file\n");
goto out;
}
inode->i_blocks = lower_inode->i_blocks;
kmem_cache_free(ecryptfs_file_info_cache, file_info);
out:
return rc;
} }
static int static int
......
...@@ -739,8 +739,7 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length) ...@@ -739,8 +739,7 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
int rc = 0; int rc = 0;
struct inode *inode = dentry->d_inode; struct inode *inode = dentry->d_inode;
struct dentry *lower_dentry; struct dentry *lower_dentry;
struct vfsmount *lower_mnt; struct file fake_ecryptfs_file;
struct file fake_ecryptfs_file, *lower_file = NULL;
struct ecryptfs_crypt_stat *crypt_stat; struct ecryptfs_crypt_stat *crypt_stat;
loff_t i_size = i_size_read(inode); loff_t i_size = i_size_read(inode);
loff_t lower_size_before_truncate; loff_t lower_size_before_truncate;
...@@ -763,51 +762,43 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length) ...@@ -763,51 +762,43 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
goto out; goto out;
} }
lower_dentry = ecryptfs_dentry_to_lower(dentry); lower_dentry = ecryptfs_dentry_to_lower(dentry);
/* This dget & mntget is released through fput at out_fput: */ ecryptfs_set_file_lower(
lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry); &fake_ecryptfs_file,
rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt, ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
O_RDWR);
if (rc) {
ecryptfs_printk(KERN_ERR,
"Error opening dentry; rc = [%i]\n", rc);
goto out_free;
}
ecryptfs_set_file_lower(&fake_ecryptfs_file, lower_file);
/* Switch on growing or shrinking file */ /* Switch on growing or shrinking file */
if (new_length > i_size) { if (new_length > i_size) {
rc = ecryptfs_fill_zeros(&fake_ecryptfs_file, new_length); char zero[] = { 0x00 };
if (rc) {
ecryptfs_printk(KERN_ERR, /* Write a single 0 at the last position of the file;
"Problem with fill_zeros\n"); * this triggers code that will fill in 0's throughout
goto out_fput; * the intermediate portion of the previous end of the
} * file and the new and of the file */
i_size_write(inode, new_length); rc = ecryptfs_write(&fake_ecryptfs_file, zero,
rc = ecryptfs_write_inode_size_to_metadata(inode); (new_length - 1), 1);
if (rc) {
printk(KERN_ERR "Problem with "
"ecryptfs_write_inode_size_to_metadata; "
"rc = [%d]\n", rc);
goto out_fput;
}
} else { /* new_length < i_size_read(inode) */ } else { /* new_length < i_size_read(inode) */
pgoff_t index = 0; /* We're chopping off all the pages down do the page
int end_pos_in_page = -1; * in which new_length is located. Fill in the end of
* that page from (new_length & ~PAGE_CACHE_MASK) to
if (new_length != 0) { * PAGE_CACHE_SIZE with zeros. */
index = ((new_length - 1) >> PAGE_CACHE_SHIFT); size_t num_zeros = (PAGE_CACHE_SIZE
end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK); - (new_length & ~PAGE_CACHE_MASK));
}
if (end_pos_in_page != (PAGE_CACHE_SIZE - 1)) { if (num_zeros) {
rc = ecryptfs_write_zeros(&fake_ecryptfs_file, char *zeros_virt;
index,
(end_pos_in_page + 1), zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
((PAGE_CACHE_SIZE - 1) if (!zeros_virt) {
- end_pos_in_page)); rc = -ENOMEM;
goto out_free;
}
rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt,
new_length, num_zeros);
kfree(zeros_virt);
if (rc) { if (rc) {
printk(KERN_ERR "Error attempting to zero out " printk(KERN_ERR "Error attempting to zero out "
"the remainder of the end page on " "the remainder of the end page on "
"reducing truncate; rc = [%d]\n", rc); "reducing truncate; rc = [%d]\n", rc);
goto out_fput; goto out_free;
} }
} }
vmtruncate(inode, new_length); vmtruncate(inode, new_length);
...@@ -816,7 +807,7 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length) ...@@ -816,7 +807,7 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
printk(KERN_ERR "Problem with " printk(KERN_ERR "Problem with "
"ecryptfs_write_inode_size_to_metadata; " "ecryptfs_write_inode_size_to_metadata; "
"rc = [%d]\n", rc); "rc = [%d]\n", rc);
goto out_fput; goto out_free;
} }
/* We are reducing the size of the ecryptfs file, and need to /* We are reducing the size of the ecryptfs file, and need to
* know if we need to reduce the size of the lower file. */ * know if we need to reduce the size of the lower file. */
...@@ -828,14 +819,6 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length) ...@@ -828,14 +819,6 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
vmtruncate(lower_dentry->d_inode, vmtruncate(lower_dentry->d_inode,
lower_size_after_truncate); lower_size_after_truncate);
} }
/* Update the access times */
lower_dentry->d_inode->i_mtime = lower_dentry->d_inode->i_ctime
= CURRENT_TIME;
mark_inode_dirty_sync(inode);
out_fput:
rc = ecryptfs_close_lower_file(lower_file);
if (rc)
printk(KERN_ERR "Error closing lower_file\n");
out_free: out_free:
if (ecryptfs_file_to_private(&fake_ecryptfs_file)) if (ecryptfs_file_to_private(&fake_ecryptfs_file))
kmem_cache_free(ecryptfs_file_info_cache, kmem_cache_free(ecryptfs_file_info_cache,
...@@ -895,21 +878,8 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia) ...@@ -895,21 +878,8 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
else if (S_ISREG(dentry->d_inode->i_mode) else if (S_ISREG(dentry->d_inode->i_mode)
&& (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED) && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
|| !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) { || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
struct vfsmount *lower_mnt;
struct file *lower_file = NULL;
struct ecryptfs_mount_crypt_stat *mount_crypt_stat; struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
int lower_flags;
lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
lower_flags = O_RDONLY;
rc = ecryptfs_open_lower_file(&lower_file, lower_dentry,
lower_mnt, lower_flags);
if (rc) {
printk(KERN_ERR
"Error opening lower file; rc = [%d]\n", rc);
mutex_unlock(&crypt_stat->cs_mutex);
goto out;
}
mount_crypt_stat = &ecryptfs_superblock_to_private( mount_crypt_stat = &ecryptfs_superblock_to_private(
dentry->d_sb)->mount_crypt_stat; dentry->d_sb)->mount_crypt_stat;
rc = ecryptfs_read_metadata(dentry); rc = ecryptfs_read_metadata(dentry);
...@@ -923,16 +893,13 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia) ...@@ -923,16 +893,13 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
"enabled; returning -EIO\n"); "enabled; returning -EIO\n");
mutex_unlock(&crypt_stat->cs_mutex); mutex_unlock(&crypt_stat->cs_mutex);
fput(lower_file);
goto out; goto out;
} }
rc = 0; rc = 0;
crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
mutex_unlock(&crypt_stat->cs_mutex); mutex_unlock(&crypt_stat->cs_mutex);
fput(lower_file);
goto out; goto out;
} }
fput(lower_file);
} }
mutex_unlock(&crypt_stat->cs_mutex); mutex_unlock(&crypt_stat->cs_mutex);
if (ia->ia_valid & ATTR_SIZE) { if (ia->ia_valid & ATTR_SIZE) {
...@@ -995,20 +962,8 @@ ssize_t ...@@ -995,20 +962,8 @@ ssize_t
ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value, ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
size_t size) size_t size)
{ {
int rc = 0; return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
struct dentry *lower_dentry; value, size);
lower_dentry = ecryptfs_dentry_to_lower(dentry);
if (!lower_dentry->d_inode->i_op->getxattr) {
rc = -ENOSYS;
goto out;
}
mutex_lock(&lower_dentry->d_inode->i_mutex);
rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
size);
mutex_unlock(&lower_dentry->d_inode->i_mutex);
out:
return rc;
} }
static ssize_t static ssize_t
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment