35 #include <linux/slab.h>
36 #include <asm/unaligned.h>
72 "page (upper index [0x%.16lx])\n", page->
index);
73 ClearPageUptodate(page);
76 SetPageUptodate(page);
82 static void strip_xattr_flag(
char *page_virt,
122 ecryptfs_copy_up_encrypted_with_header(
struct page *page,
125 loff_t extent_num_in_page = 0;
130 while (extent_num_in_page < num_extents_per_page) {
131 loff_t view_extent_num = ((((loff_t)page->
index)
132 * num_extents_per_page)
133 + extent_num_in_page);
134 size_t num_header_extents_at_front =
137 if (view_extent_num < num_header_extents_at_front) {
144 if (view_extent_num == 0) {
148 page_virt, page->
mapping->host);
149 strip_xattr_flag(page_virt + 16, crypt_stat);
158 "region; rc = [%d]\n", __func__, rc);
163 loff_t lower_offset =
173 "extent at offset [%lld] in the lower "
174 "file; rc = [%d]\n", __func__,
179 extent_num_in_page++;
194 static int ecryptfs_readpage(
struct file *
file,
struct page *page)
197 &ecryptfs_inode_to_private(page->
mapping->host)->crypt_stat;
206 rc = ecryptfs_copy_up_encrypted_with_header(page,
210 "the encrypted content from the lower "
211 "file whilst inserting the metadata "
212 "from the xattr into the header; rc = "
213 "[%d]\n", __func__, rc);
237 ClearPageUptodate(page);
239 SetPageUptodate(page);
249 static int fill_zeros_to_end_of_page(
struct page *page,
unsigned int to)
252 int end_byte_in_page;
257 if (to > end_byte_in_page)
258 end_byte_in_page = to;
278 static int ecryptfs_write_begin(
struct file *file,
280 loff_t
pos,
unsigned len,
unsigned flags,
281 struct page **pagep,
void **fsdata)
285 loff_t prev_page_end_size;
293 prev_page_end_size = ((loff_t)index << PAGE_CACHE_SHIFT);
294 if (!PageUptodate(page)) {
296 &ecryptfs_inode_to_private(mapping->
host)->crypt_stat;
303 "lower page segment; rc = [%d]\n",
305 ClearPageUptodate(page);
308 SetPageUptodate(page);
311 rc = ecryptfs_copy_up_encrypted_with_header(
315 "to copy the encrypted content "
316 "from the lower file whilst "
317 "inserting the metadata from "
318 "the xattr into the header; rc "
319 "= [%d]\n", __func__, rc);
320 ClearPageUptodate(page);
323 SetPageUptodate(page);
332 ClearPageUptodate(page);
335 SetPageUptodate(page);
338 if (prev_page_end_size
339 >= i_size_read(page->
mapping->host)) {
345 "page at index [%ld]; "
347 __func__, page->
index, rc);
348 ClearPageUptodate(page);
352 SetPageUptodate(page);
358 if (prev_page_end_size > i_size_read(page->
mapping->host)) {
363 "truncate to (higher) offset [%lld];"
364 " rc = [%d]\n", __func__,
365 prev_page_end_size, rc);
372 if ((i_size_read(mapping->
host) == prev_page_end_size)
391 static int ecryptfs_write_inode_size_to_header(
struct inode *ecryptfs_inode)
393 char *file_size_virt;
397 if (!file_size_virt) {
404 kfree(file_size_virt);
407 "rc = [%d]\n", __func__, rc);
416 static int ecryptfs_write_inode_size_to_xattr(
struct inode *ecryptfs_inode)
420 struct dentry *lower_dentry =
421 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
422 struct inode *lower_inode = lower_dentry->
d_inode;
425 if (!lower_inode->
i_op->getxattr || !lower_inode->
i_op->setxattr) {
427 "No support for setting xattr in lower filesystem\n");
434 "inode size to xattr\n");
445 xattr_virt, size, 0);
449 "to lower file xattr; rc = [%d]\n", rc);
459 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
462 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
464 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
477 static int ecryptfs_write_end(
struct file *file,
479 loff_t pos,
unsigned len,
unsigned copied,
480 struct page *page,
void *fsdata)
484 unsigned to = from + copied;
485 struct inode *ecryptfs_inode = mapping->
host;
487 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
491 "(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
498 ecryptfs_inode_to_lower(ecryptfs_inode));
503 rc = fill_zeros_to_end_of_page(page, to);
506 "zeros in page with index = [0x%.16lx]\n", index);
512 "index [0x%.16lx])\n", index);
515 if (pos + copied > i_size_read(ecryptfs_inode)) {
516 i_size_write(ecryptfs_inode, pos + copied);
519 (
unsigned long long)i_size_read(ecryptfs_inode));
537 struct inode *lower_inode;
539 inode = (
struct inode *)mapping->
host;
540 lower_inode = ecryptfs_inode_to_lower(inode);
548 .writepage = ecryptfs_writepage,
549 .readpage = ecryptfs_readpage,
550 .write_begin = ecryptfs_write_begin,
551 .write_end = ecryptfs_write_end,
552 .bmap = ecryptfs_bmap,