Commit 57db4e8d authored by Thieu Le's avatar Thieu Le Committed by Tyler Hicks

ecryptfs: modify write path to encrypt page in writepage

Change the write path to encrypt the data only when the page is written to
disk in ecryptfs_writepage. Previously, ecryptfs encrypts the page in
ecryptfs_write_end which means that if there are multiple write requests to
the same page, ecryptfs ends up re-encrypting that page over and over again.
This patch minimizes the number of encryptions needed.
Signed-off-by: default avatarThieu Le <thieule@chromium.org>
[tyhicks: Changed NULL .drop_inode sop pointer to generic_drop_inode]
Signed-off-by: default avatarTyler Hicks <tyhicks@linux.vnet.ibm.com>
parent fed8859b
...@@ -296,7 +296,6 @@ struct ecryptfs_inode_info { ...@@ -296,7 +296,6 @@ struct ecryptfs_inode_info {
struct inode vfs_inode; struct inode vfs_inode;
struct inode *wii_inode; struct inode *wii_inode;
struct file *lower_file; struct file *lower_file;
struct mutex lower_file_mutex;
struct ecryptfs_crypt_stat crypt_stat; struct ecryptfs_crypt_stat crypt_stat;
}; };
......
...@@ -273,7 +273,14 @@ static int ecryptfs_release(struct inode *inode, struct file *file) ...@@ -273,7 +273,14 @@ static int ecryptfs_release(struct inode *inode, struct file *file)
static int static int
ecryptfs_fsync(struct file *file, int datasync) ecryptfs_fsync(struct file *file, int datasync)
{ {
return vfs_fsync(ecryptfs_file_to_lower(file), datasync); int rc = 0;
rc = generic_file_fsync(file, datasync);
if (rc)
goto out;
rc = vfs_fsync(ecryptfs_file_to_lower(file), datasync);
out:
return rc;
} }
static int ecryptfs_fasync(int fd, struct file *file, int flag) static int ecryptfs_fasync(int fd, struct file *file, int flag)
......
...@@ -122,7 +122,6 @@ int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry) ...@@ -122,7 +122,6 @@ int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
ecryptfs_inode_to_private(ecryptfs_dentry->d_inode); ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
int rc = 0; int rc = 0;
mutex_lock(&inode_info->lower_file_mutex);
if (!inode_info->lower_file) { if (!inode_info->lower_file) {
struct dentry *lower_dentry; struct dentry *lower_dentry;
struct vfsmount *lower_mnt = struct vfsmount *lower_mnt =
...@@ -138,7 +137,6 @@ int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry) ...@@ -138,7 +137,6 @@ int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
inode_info->lower_file = NULL; inode_info->lower_file = NULL;
} }
} }
mutex_unlock(&inode_info->lower_file_mutex);
return rc; return rc;
} }
......
...@@ -62,6 +62,18 @@ static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) ...@@ -62,6 +62,18 @@ static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
{ {
int rc; int rc;
/*
* Refuse to write the page out if we are called from reclaim context
* since our writepage() path may potentially allocate memory when
* calling into the lower fs vfs_write() which may in turn invoke
* us again.
*/
if (current->flags & PF_MEMALLOC) {
redirty_page_for_writepage(wbc, page);
rc = 0;
goto out;
}
rc = ecryptfs_encrypt_page(page); rc = ecryptfs_encrypt_page(page);
if (rc) { if (rc) {
ecryptfs_printk(KERN_WARNING, "Error encrypting " ecryptfs_printk(KERN_WARNING, "Error encrypting "
...@@ -70,8 +82,8 @@ static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) ...@@ -70,8 +82,8 @@ static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
goto out; goto out;
} }
SetPageUptodate(page); SetPageUptodate(page);
unlock_page(page);
out: out:
unlock_page(page);
return rc; return rc;
} }
...@@ -481,6 +493,7 @@ static int ecryptfs_write_end(struct file *file, ...@@ -481,6 +493,7 @@ static int ecryptfs_write_end(struct file *file,
struct ecryptfs_crypt_stat *crypt_stat = struct ecryptfs_crypt_stat *crypt_stat =
&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
int rc; int rc;
int need_unlock_page = 1;
ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
"(page w/ index = [0x%.16lx], to = [%d])\n", index, to); "(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
...@@ -501,25 +514,25 @@ static int ecryptfs_write_end(struct file *file, ...@@ -501,25 +514,25 @@ static int ecryptfs_write_end(struct file *file,
"zeros in page with index = [0x%.16lx]\n", index); "zeros in page with index = [0x%.16lx]\n", index);
goto out; goto out;
} }
rc = ecryptfs_encrypt_page(page); set_page_dirty(page);
if (rc) { unlock_page(page);
ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " need_unlock_page = 0;
"index [0x%.16lx])\n", index);
goto out;
}
if (pos + copied > i_size_read(ecryptfs_inode)) { if (pos + copied > i_size_read(ecryptfs_inode)) {
i_size_write(ecryptfs_inode, pos + copied); i_size_write(ecryptfs_inode, pos + copied);
ecryptfs_printk(KERN_DEBUG, "Expanded file size to " ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
"[0x%.16llx]\n", "[0x%.16llx]\n",
(unsigned long long)i_size_read(ecryptfs_inode)); (unsigned long long)i_size_read(ecryptfs_inode));
} balance_dirty_pages_ratelimited(mapping);
rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
if (rc) if (rc) {
printk(KERN_ERR "Error writing inode size to metadata; " printk(KERN_ERR "Error writing inode size to metadata; "
"rc = [%d]\n", rc); "rc = [%d]\n", rc);
else goto out;
}
}
rc = copied; rc = copied;
out: out:
if (need_unlock_page)
unlock_page(page); unlock_page(page);
page_cache_release(page); page_cache_release(page);
return rc; return rc;
......
...@@ -44,15 +44,11 @@ int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data, ...@@ -44,15 +44,11 @@ int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
ssize_t rc; ssize_t rc;
inode_info = ecryptfs_inode_to_private(ecryptfs_inode); inode_info = ecryptfs_inode_to_private(ecryptfs_inode);
mutex_lock(&inode_info->lower_file_mutex);
BUG_ON(!inode_info->lower_file); BUG_ON(!inode_info->lower_file);
inode_info->lower_file->f_pos = offset;
fs_save = get_fs(); fs_save = get_fs();
set_fs(get_ds()); set_fs(get_ds());
rc = vfs_write(inode_info->lower_file, data, size, rc = vfs_write(inode_info->lower_file, data, size, &offset);
&inode_info->lower_file->f_pos);
set_fs(fs_save); set_fs(fs_save);
mutex_unlock(&inode_info->lower_file_mutex);
mark_inode_dirty_sync(ecryptfs_inode); mark_inode_dirty_sync(ecryptfs_inode);
return rc; return rc;
} }
...@@ -234,15 +230,11 @@ int ecryptfs_read_lower(char *data, loff_t offset, size_t size, ...@@ -234,15 +230,11 @@ int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
mm_segment_t fs_save; mm_segment_t fs_save;
ssize_t rc; ssize_t rc;
mutex_lock(&inode_info->lower_file_mutex);
BUG_ON(!inode_info->lower_file); BUG_ON(!inode_info->lower_file);
inode_info->lower_file->f_pos = offset;
fs_save = get_fs(); fs_save = get_fs();
set_fs(get_ds()); set_fs(get_ds());
rc = vfs_read(inode_info->lower_file, data, size, rc = vfs_read(inode_info->lower_file, data, size, &offset);
&inode_info->lower_file->f_pos);
set_fs(fs_save); set_fs(fs_save);
mutex_unlock(&inode_info->lower_file_mutex);
return rc; return rc;
} }
......
...@@ -55,7 +55,6 @@ static struct inode *ecryptfs_alloc_inode(struct super_block *sb) ...@@ -55,7 +55,6 @@ static struct inode *ecryptfs_alloc_inode(struct super_block *sb)
if (unlikely(!inode_info)) if (unlikely(!inode_info))
goto out; goto out;
ecryptfs_init_crypt_stat(&inode_info->crypt_stat); ecryptfs_init_crypt_stat(&inode_info->crypt_stat);
mutex_init(&inode_info->lower_file_mutex);
inode_info->lower_file = NULL; inode_info->lower_file = NULL;
inode = &inode_info->vfs_inode; inode = &inode_info->vfs_inode;
out: out:
...@@ -198,7 +197,7 @@ static int ecryptfs_show_options(struct seq_file *m, struct vfsmount *mnt) ...@@ -198,7 +197,7 @@ static int ecryptfs_show_options(struct seq_file *m, struct vfsmount *mnt)
const struct super_operations ecryptfs_sops = { const struct super_operations ecryptfs_sops = {
.alloc_inode = ecryptfs_alloc_inode, .alloc_inode = ecryptfs_alloc_inode,
.destroy_inode = ecryptfs_destroy_inode, .destroy_inode = ecryptfs_destroy_inode,
.drop_inode = generic_delete_inode, .drop_inode = generic_drop_inode,
.statfs = ecryptfs_statfs, .statfs = ecryptfs_statfs,
.remount_fs = NULL, .remount_fs = NULL,
.evict_inode = ecryptfs_evict_inode, .evict_inode = ecryptfs_evict_inode,
......
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