aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ecryptfs/mmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ecryptfs/mmap.c')
-rw-r--r--fs/ecryptfs/mmap.c703
1 files changed, 161 insertions, 542 deletions
diff --git a/fs/ecryptfs/mmap.c b/fs/ecryptfs/mmap.c
index e4ab7bc14efe..16a7a555f392 100644
--- a/fs/ecryptfs/mmap.c
+++ b/fs/ecryptfs/mmap.c
@@ -37,130 +37,27 @@
37struct kmem_cache *ecryptfs_lower_page_cache; 37struct kmem_cache *ecryptfs_lower_page_cache;
38 38
39/** 39/**
40 * ecryptfs_get1page 40 * ecryptfs_get_locked_page
41 * 41 *
42 * Get one page from cache or lower f/s, return error otherwise. 42 * Get one page from cache or lower f/s, return error otherwise.
43 * 43 *
44 * Returns unlocked and up-to-date page (if ok), with increased 44 * Returns locked and up-to-date page (if ok), with increased
45 * refcnt. 45 * refcnt.
46 */ 46 */
47static struct page *ecryptfs_get1page(struct file *file, int index) 47struct page *ecryptfs_get_locked_page(struct file *file, loff_t index)
48{ 48{
49 struct dentry *dentry; 49 struct dentry *dentry;
50 struct inode *inode; 50 struct inode *inode;
51 struct address_space *mapping; 51 struct address_space *mapping;
52 struct page *page;
52 53
53 dentry = file->f_path.dentry; 54 dentry = file->f_path.dentry;
54 inode = dentry->d_inode; 55 inode = dentry->d_inode;
55 mapping = inode->i_mapping; 56 mapping = inode->i_mapping;
56 return read_mapping_page(mapping, index, (void *)file); 57 page = read_mapping_page(mapping, index, (void *)file);
57} 58 if (!IS_ERR(page))
58 59 lock_page(page);
59/** 60 return page;
60 * ecryptfs_fill_zeros
61 * @file: The ecryptfs file
62 * @new_length: The new length of the data in the underlying file;
63 * everything between the prior end of the file and the
64 * new end of the file will be filled with zero's.
65 * new_length must be greater than current length
66 *
67 * Function for handling lseek-ing past the end of the file.
68 *
69 * This function does not support shrinking, only growing a file.
70 *
71 * Returns zero on success; non-zero otherwise.
72 */
73int ecryptfs_fill_zeros(struct file *file, loff_t new_length)
74{
75 int rc = 0;
76 struct dentry *dentry = file->f_path.dentry;
77 struct inode *inode = dentry->d_inode;
78 pgoff_t old_end_page_index = 0;
79 pgoff_t index = old_end_page_index;
80 int old_end_pos_in_page = -1;
81 pgoff_t new_end_page_index;
82 int new_end_pos_in_page;
83 loff_t cur_length = i_size_read(inode);
84
85 if (cur_length != 0) {
86 index = old_end_page_index =
87 ((cur_length - 1) >> PAGE_CACHE_SHIFT);
88 old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK);
89 }
90 new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT);
91 new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK);
92 ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; "
93 "old_end_pos_in_page = [%d]; "
94 "new_end_page_index = [0x%.16x]; "
95 "new_end_pos_in_page = [%d]\n",
96 old_end_page_index, old_end_pos_in_page,
97 new_end_page_index, new_end_pos_in_page);
98 if (old_end_page_index == new_end_page_index) {
99 /* Start and end are in the same page; we just need to
100 * set a portion of the existing page to zero's */
101 rc = ecryptfs_write_zeros(file, index,
102 (old_end_pos_in_page + 1),
103 (new_end_pos_in_page
104 - old_end_pos_in_page));
105 if (rc)
106 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros("
107 "file=[%p], "
108 "index=[0x%.16x], "
109 "old_end_pos_in_page=[d], "
110 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
111 "=[%d]"
112 ")=[d]) returned [%d]\n", file, index,
113 old_end_pos_in_page,
114 new_end_pos_in_page,
115 (PAGE_CACHE_SIZE - new_end_pos_in_page),
116 rc);
117 goto out;
118 }
119 /* Fill the remainder of the previous last page with zeros */
120 rc = ecryptfs_write_zeros(file, index, (old_end_pos_in_page + 1),
121 ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page));
122 if (rc) {
123 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file=[%p], "
124 "index=[0x%.16x], old_end_pos_in_page=[d], "
125 "(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) "
126 "returned [%d]\n", file, index,
127 old_end_pos_in_page,
128 (PAGE_CACHE_SIZE - old_end_pos_in_page), rc);
129 goto out;
130 }
131 index++;
132 while (index < new_end_page_index) {
133 /* Fill all intermediate pages with zeros */
134 rc = ecryptfs_write_zeros(file, index, 0, PAGE_CACHE_SIZE);
135 if (rc) {
136 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros("
137 "file=[%p], "
138 "index=[0x%.16x], "
139 "old_end_pos_in_page=[d], "
140 "(PAGE_CACHE_SIZE - new_end_pos_in_page"
141 "=[%d]"
142 ")=[d]) returned [%d]\n", file, index,
143 old_end_pos_in_page,
144 new_end_pos_in_page,
145 (PAGE_CACHE_SIZE - new_end_pos_in_page),
146 rc);
147 goto out;
148 }
149 index++;
150 }
151 /* Fill the portion at the beginning of the last new page with
152 * zero's */
153 rc = ecryptfs_write_zeros(file, index, 0, (new_end_pos_in_page + 1));
154 if (rc) {
155 ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file="
156 "[%p], index=[0x%.16x], 0, "
157 "new_end_pos_in_page=[%d]"
158 "returned [%d]\n", file, index,
159 new_end_pos_in_page, rc);
160 goto out;
161 }
162out:
163 return rc;
164} 61}
165 62
166/** 63/**
@@ -171,13 +68,9 @@ out:
171 */ 68 */
172static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) 69static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
173{ 70{
174 struct ecryptfs_page_crypt_context ctx;
175 int rc; 71 int rc;
176 72
177 ctx.page = page; 73 rc = ecryptfs_encrypt_page(page);
178 ctx.mode = ECRYPTFS_WRITEPAGE_MODE;
179 ctx.param.wbc = wbc;
180 rc = ecryptfs_encrypt_page(&ctx);
181 if (rc) { 74 if (rc) {
182 ecryptfs_printk(KERN_WARNING, "Error encrypting " 75 ecryptfs_printk(KERN_WARNING, "Error encrypting "
183 "page (upper index [0x%.16x])\n", page->index); 76 "page (upper index [0x%.16x])\n", page->index);
@@ -191,58 +84,6 @@ out:
191} 84}
192 85
193/** 86/**
194 * Reads the data from the lower file file at index lower_page_index
195 * and copies that data into page.
196 *
197 * @param page Page to fill
198 * @param lower_page_index Index of the page in the lower file to get
199 */
200int ecryptfs_do_readpage(struct file *file, struct page *page,
201 pgoff_t lower_page_index)
202{
203 int rc;
204 struct dentry *dentry;
205 struct file *lower_file;
206 struct dentry *lower_dentry;
207 struct inode *inode;
208 struct inode *lower_inode;
209 char *page_data;
210 struct page *lower_page = NULL;
211 char *lower_page_data;
212 const struct address_space_operations *lower_a_ops;
213
214 dentry = file->f_path.dentry;
215 lower_file = ecryptfs_file_to_lower(file);
216 lower_dentry = ecryptfs_dentry_to_lower(dentry);
217 inode = dentry->d_inode;
218 lower_inode = ecryptfs_inode_to_lower(inode);
219 lower_a_ops = lower_inode->i_mapping->a_ops;
220 lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
221 (filler_t *)lower_a_ops->readpage,
222 (void *)lower_file);
223 if (IS_ERR(lower_page)) {
224 rc = PTR_ERR(lower_page);
225 lower_page = NULL;
226 ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
227 goto out;
228 }
229 page_data = kmap_atomic(page, KM_USER0);
230 lower_page_data = kmap_atomic(lower_page, KM_USER1);
231 memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
232 kunmap_atomic(lower_page_data, KM_USER1);
233 kunmap_atomic(page_data, KM_USER0);
234 flush_dcache_page(page);
235 rc = 0;
236out:
237 if (likely(lower_page))
238 page_cache_release(lower_page);
239 if (rc == 0)
240 SetPageUptodate(page);
241 else
242 ClearPageUptodate(page);
243 return rc;
244}
245/**
246 * Header Extent: 87 * Header Extent:
247 * Octets 0-7: Unencrypted file size (big-endian) 88 * Octets 0-7: Unencrypted file size (big-endian)
248 * Octets 8-15: eCryptfs special marker 89 * Octets 8-15: eCryptfs special marker
@@ -271,9 +112,77 @@ static void set_header_info(char *page_virt,
271} 112}
272 113
273/** 114/**
115 * ecryptfs_copy_up_encrypted_with_header
116 * @page: Sort of a ``virtual'' representation of the encrypted lower
117 * file. The actual lower file does not have the metadata in
118 * the header. This is locked.
119 * @crypt_stat: The eCryptfs inode's cryptographic context
120 *
121 * The ``view'' is the version of the file that userspace winds up
122 * seeing, with the header information inserted.
123 */
124static int
125ecryptfs_copy_up_encrypted_with_header(struct page *page,
126 struct ecryptfs_crypt_stat *crypt_stat)
127{
128 loff_t extent_num_in_page = 0;
129 loff_t num_extents_per_page = (PAGE_CACHE_SIZE
130 / crypt_stat->extent_size);
131 int rc = 0;
132
133 while (extent_num_in_page < num_extents_per_page) {
134 loff_t view_extent_num = ((((loff_t)page->index)
135 * num_extents_per_page)
136 + extent_num_in_page);
137
138 if (view_extent_num < crypt_stat->num_header_extents_at_front) {
139 /* This is a header extent */
140 char *page_virt;
141
142 page_virt = kmap_atomic(page, KM_USER0);
143 memset(page_virt, 0, PAGE_CACHE_SIZE);
144 /* TODO: Support more than one header extent */
145 if (view_extent_num == 0) {
146 rc = ecryptfs_read_xattr_region(
147 page_virt, page->mapping->host);
148 set_header_info(page_virt, crypt_stat);
149 }
150 kunmap_atomic(page_virt, KM_USER0);
151 flush_dcache_page(page);
152 if (rc) {
153 printk(KERN_ERR "%s: Error reading xattr "
154 "region; rc = [%d]\n", __FUNCTION__, rc);
155 goto out;
156 }
157 } else {
158 /* This is an encrypted data extent */
159 loff_t lower_offset =
160 ((view_extent_num -
161 crypt_stat->num_header_extents_at_front)
162 * crypt_stat->extent_size);
163
164 rc = ecryptfs_read_lower_page_segment(
165 page, (lower_offset >> PAGE_CACHE_SHIFT),
166 (lower_offset & ~PAGE_CACHE_MASK),
167 crypt_stat->extent_size, page->mapping->host);
168 if (rc) {
169 printk(KERN_ERR "%s: Error attempting to read "
170 "extent at offset [%lld] in the lower "
171 "file; rc = [%d]\n", __FUNCTION__,
172 lower_offset, rc);
173 goto out;
174 }
175 }
176 extent_num_in_page++;
177 }
178out:
179 return rc;
180}
181
182/**
274 * ecryptfs_readpage 183 * ecryptfs_readpage
275 * @file: This is an ecryptfs file 184 * @file: An eCryptfs file
276 * @page: ecryptfs associated page to stick the read data into 185 * @page: Page from eCryptfs inode mapping into which to stick the read data
277 * 186 *
278 * Read in a page, decrypting if necessary. 187 * Read in a page, decrypting if necessary.
279 * 188 *
@@ -281,59 +190,35 @@ static void set_header_info(char *page_virt,
281 */ 190 */
282static int ecryptfs_readpage(struct file *file, struct page *page) 191static int ecryptfs_readpage(struct file *file, struct page *page)
283{ 192{
193 struct ecryptfs_crypt_stat *crypt_stat =
194 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
284 int rc = 0; 195 int rc = 0;
285 struct ecryptfs_crypt_stat *crypt_stat;
286 196
287 BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode));
288 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
289 ->crypt_stat;
290 if (!crypt_stat 197 if (!crypt_stat
291 || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED) 198 || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
292 || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { 199 || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
293 ecryptfs_printk(KERN_DEBUG, 200 ecryptfs_printk(KERN_DEBUG,
294 "Passing through unencrypted page\n"); 201 "Passing through unencrypted page\n");
295 rc = ecryptfs_do_readpage(file, page, page->index); 202 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
296 if (rc) { 203 PAGE_CACHE_SIZE,
297 ecryptfs_printk(KERN_ERR, "Error reading page; rc = " 204 page->mapping->host);
298 "[%d]\n", rc);
299 goto out;
300 }
301 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { 205 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
302 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 206 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
303 int num_pages_in_header_region = 207 rc = ecryptfs_copy_up_encrypted_with_header(page,
304 (crypt_stat->header_extent_size 208 crypt_stat);
305 / PAGE_CACHE_SIZE); 209 if (rc) {
306 210 printk(KERN_ERR "%s: Error attempting to copy "
307 if (page->index < num_pages_in_header_region) { 211 "the encrypted content from the lower "
308 char *page_virt; 212 "file whilst inserting the metadata "
309 213 "from the xattr into the header; rc = "
310 page_virt = kmap_atomic(page, KM_USER0); 214 "[%d]\n", __FUNCTION__, rc);
311 memset(page_virt, 0, PAGE_CACHE_SIZE); 215 goto out;
312 if (page->index == 0) {
313 rc = ecryptfs_read_xattr_region(
314 page_virt, file->f_path.dentry);
315 set_header_info(page_virt, crypt_stat);
316 }
317 kunmap_atomic(page_virt, KM_USER0);
318 flush_dcache_page(page);
319 if (rc) {
320 printk(KERN_ERR "Error reading xattr "
321 "region\n");
322 goto out;
323 }
324 } else {
325 rc = ecryptfs_do_readpage(
326 file, page,
327 (page->index
328 - num_pages_in_header_region));
329 if (rc) {
330 printk(KERN_ERR "Error reading page; "
331 "rc = [%d]\n", rc);
332 goto out;
333 }
334 } 216 }
217
335 } else { 218 } else {
336 rc = ecryptfs_do_readpage(file, page, page->index); 219 rc = ecryptfs_read_lower_page_segment(
220 page, page->index, 0, PAGE_CACHE_SIZE,
221 page->mapping->host);
337 if (rc) { 222 if (rc) {
338 printk(KERN_ERR "Error reading page; rc = " 223 printk(KERN_ERR "Error reading page; rc = "
339 "[%d]\n", rc); 224 "[%d]\n", rc);
@@ -341,17 +226,18 @@ static int ecryptfs_readpage(struct file *file, struct page *page)
341 } 226 }
342 } 227 }
343 } else { 228 } else {
344 rc = ecryptfs_decrypt_page(file, page); 229 rc = ecryptfs_decrypt_page(page);
345 if (rc) { 230 if (rc) {
346 ecryptfs_printk(KERN_ERR, "Error decrypting page; " 231 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
347 "rc = [%d]\n", rc); 232 "rc = [%d]\n", rc);
348 goto out; 233 goto out;
349 } 234 }
350 } 235 }
351 SetPageUptodate(page);
352out: 236out:
353 if (rc) 237 if (rc)
354 ClearPageUptodate(page); 238 ClearPageUptodate(page);
239 else
240 SetPageUptodate(page);
355 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", 241 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
356 page->index); 242 page->index);
357 unlock_page(page); 243 unlock_page(page);
@@ -377,27 +263,6 @@ out:
377 return 0; 263 return 0;
378} 264}
379 265
380/**
381 * eCryptfs does not currently support holes. When writing after a
382 * seek past the end of the file, eCryptfs fills in 0's through to the
383 * current location. The code to fill in the 0's to all the
384 * intermediate pages calls ecryptfs_prepare_write_no_truncate().
385 */
386static int
387ecryptfs_prepare_write_no_truncate(struct file *file, struct page *page,
388 unsigned from, unsigned to)
389{
390 int rc = 0;
391
392 if (from == 0 && to == PAGE_CACHE_SIZE)
393 goto out; /* If we are writing a full page, it will be
394 up to date. */
395 if (!PageUptodate(page))
396 rc = ecryptfs_do_readpage(file, page, page->index);
397out:
398 return rc;
399}
400
401static int ecryptfs_prepare_write(struct file *file, struct page *page, 266static int ecryptfs_prepare_write(struct file *file, struct page *page,
402 unsigned from, unsigned to) 267 unsigned from, unsigned to)
403{ 268{
@@ -406,10 +271,21 @@ static int ecryptfs_prepare_write(struct file *file, struct page *page,
406 if (from == 0 && to == PAGE_CACHE_SIZE) 271 if (from == 0 && to == PAGE_CACHE_SIZE)
407 goto out; /* If we are writing a full page, it will be 272 goto out; /* If we are writing a full page, it will be
408 up to date. */ 273 up to date. */
409 if (!PageUptodate(page)) 274 if (!PageUptodate(page)) {
410 rc = ecryptfs_do_readpage(file, page, page->index); 275 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
276 PAGE_CACHE_SIZE,
277 page->mapping->host);
278 if (rc) {
279 printk(KERN_ERR "%s: Error attemping to read lower "
280 "page segment; rc = [%d]\n", __FUNCTION__, rc);
281 ClearPageUptodate(page);
282 goto out;
283 } else
284 SetPageUptodate(page);
285 }
411 if (page->index != 0) { 286 if (page->index != 0) {
412 loff_t end_of_prev_pg_pos = page_offset(page) - 1; 287 loff_t end_of_prev_pg_pos =
288 (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1);
413 289
414 if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) { 290 if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) {
415 rc = ecryptfs_truncate(file->f_path.dentry, 291 rc = ecryptfs_truncate(file->f_path.dentry,
@@ -428,32 +304,6 @@ out:
428 return rc; 304 return rc;
429} 305}
430 306
431int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
432 struct inode *lower_inode,
433 struct writeback_control *wbc)
434{
435 int rc = 0;
436
437 rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
438 if (rc) {
439 ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
440 "rc = [%d]\n", rc);
441 goto out;
442 }
443 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
444 page_cache_release(lower_page);
445out:
446 return rc;
447}
448
449static
450void ecryptfs_release_lower_page(struct page *lower_page, int page_locked)
451{
452 if (page_locked)
453 unlock_page(lower_page);
454 page_cache_release(lower_page);
455}
456
457/** 307/**
458 * ecryptfs_write_inode_size_to_header 308 * ecryptfs_write_inode_size_to_header
459 * 309 *
@@ -461,67 +311,48 @@ void ecryptfs_release_lower_page(struct page *lower_page, int page_locked)
461 * 311 *
462 * Returns zero on success; non-zero on error. 312 * Returns zero on success; non-zero on error.
463 */ 313 */
464static int ecryptfs_write_inode_size_to_header(struct file *lower_file, 314static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
465 struct inode *lower_inode,
466 struct inode *inode)
467{ 315{
468 int rc = 0;
469 struct page *header_page;
470 char *header_virt;
471 const struct address_space_operations *lower_a_ops;
472 u64 file_size; 316 u64 file_size;
317 char *file_size_virt;
318 int rc;
473 319
474retry: 320 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
475 header_page = grab_cache_page(lower_inode->i_mapping, 0); 321 if (!file_size_virt) {
476 if (!header_page) { 322 rc = -ENOMEM;
477 ecryptfs_printk(KERN_ERR, "grab_cache_page for "
478 "lower_page_index 0 failed\n");
479 rc = -EINVAL;
480 goto out;
481 }
482 lower_a_ops = lower_inode->i_mapping->a_ops;
483 rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
484 if (rc) {
485 if (rc == AOP_TRUNCATED_PAGE) {
486 ecryptfs_release_lower_page(header_page, 0);
487 goto retry;
488 } else
489 ecryptfs_release_lower_page(header_page, 1);
490 goto out; 323 goto out;
491 } 324 }
492 file_size = (u64)i_size_read(inode); 325 file_size = (u64)i_size_read(ecryptfs_inode);
493 ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
494 file_size = cpu_to_be64(file_size); 326 file_size = cpu_to_be64(file_size);
495 header_virt = kmap_atomic(header_page, KM_USER0); 327 memcpy(file_size_virt, &file_size, sizeof(u64));
496 memcpy(header_virt, &file_size, sizeof(u64)); 328 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
497 kunmap_atomic(header_virt, KM_USER0); 329 sizeof(u64));
498 flush_dcache_page(header_page); 330 kfree(file_size_virt);
499 rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8); 331 if (rc)
500 if (rc < 0) 332 printk(KERN_ERR "%s: Error writing file size to header; "
501 ecryptfs_printk(KERN_ERR, "Error commiting header page " 333 "rc = [%d]\n", __FUNCTION__, rc);
502 "write\n");
503 if (rc == AOP_TRUNCATED_PAGE) {
504 ecryptfs_release_lower_page(header_page, 0);
505 goto retry;
506 } else
507 ecryptfs_release_lower_page(header_page, 1);
508 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
509 mark_inode_dirty_sync(inode);
510out: 334out:
511 return rc; 335 return rc;
512} 336}
513 337
514static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode, 338struct kmem_cache *ecryptfs_xattr_cache;
515 struct inode *inode, 339
516 struct dentry *ecryptfs_dentry, 340static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
517 int lower_i_mutex_held)
518{ 341{
519 ssize_t size; 342 ssize_t size;
520 void *xattr_virt; 343 void *xattr_virt;
521 struct dentry *lower_dentry; 344 struct dentry *lower_dentry =
345 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
346 struct inode *lower_inode = lower_dentry->d_inode;
522 u64 file_size; 347 u64 file_size;
523 int rc; 348 int rc;
524 349
350 if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
351 printk(KERN_WARNING
352 "No support for setting xattr in lower filesystem\n");
353 rc = -ENOSYS;
354 goto out;
355 }
525 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); 356 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
526 if (!xattr_virt) { 357 if (!xattr_virt) {
527 printk(KERN_ERR "Out of memory whilst attempting to write " 358 printk(KERN_ERR "Out of memory whilst attempting to write "
@@ -529,35 +360,17 @@ static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
529 rc = -ENOMEM; 360 rc = -ENOMEM;
530 goto out; 361 goto out;
531 } 362 }
532 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); 363 mutex_lock(&lower_inode->i_mutex);
533 if (!lower_dentry->d_inode->i_op->getxattr || 364 size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
534 !lower_dentry->d_inode->i_op->setxattr) { 365 xattr_virt, PAGE_CACHE_SIZE);
535 printk(KERN_WARNING
536 "No support for setting xattr in lower filesystem\n");
537 rc = -ENOSYS;
538 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
539 goto out;
540 }
541 if (!lower_i_mutex_held)
542 mutex_lock(&lower_dentry->d_inode->i_mutex);
543 size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
544 ECRYPTFS_XATTR_NAME,
545 xattr_virt,
546 PAGE_CACHE_SIZE);
547 if (!lower_i_mutex_held)
548 mutex_unlock(&lower_dentry->d_inode->i_mutex);
549 if (size < 0) 366 if (size < 0)
550 size = 8; 367 size = 8;
551 file_size = (u64)i_size_read(inode); 368 file_size = (u64)i_size_read(ecryptfs_inode);
552 file_size = cpu_to_be64(file_size); 369 file_size = cpu_to_be64(file_size);
553 memcpy(xattr_virt, &file_size, sizeof(u64)); 370 memcpy(xattr_virt, &file_size, sizeof(u64));
554 if (!lower_i_mutex_held) 371 rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
555 mutex_lock(&lower_dentry->d_inode->i_mutex); 372 xattr_virt, size, 0);
556 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, 373 mutex_unlock(&lower_inode->i_mutex);
557 ECRYPTFS_XATTR_NAME,
558 xattr_virt, size, 0);
559 if (!lower_i_mutex_held)
560 mutex_unlock(&lower_dentry->d_inode->i_mutex);
561 if (rc) 374 if (rc)
562 printk(KERN_ERR "Error whilst attempting to write inode size " 375 printk(KERN_ERR "Error whilst attempting to write inode size "
563 "to lower file xattr; rc = [%d]\n", rc); 376 "to lower file xattr; rc = [%d]\n", rc);
@@ -566,122 +379,18 @@ out:
566 return rc; 379 return rc;
567} 380}
568 381
569int 382int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
570ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
571 struct inode *lower_inode,
572 struct inode *inode,
573 struct dentry *ecryptfs_dentry,
574 int lower_i_mutex_held)
575{ 383{
576 struct ecryptfs_crypt_stat *crypt_stat; 384 struct ecryptfs_crypt_stat *crypt_stat;
577 385
578 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; 386 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
579 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 387 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
580 return ecryptfs_write_inode_size_to_xattr(lower_inode, inode, 388 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
581 ecryptfs_dentry,
582 lower_i_mutex_held);
583 else 389 else
584 return ecryptfs_write_inode_size_to_header(lower_file, 390 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
585 lower_inode,
586 inode);
587}
588
589int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
590 struct file *lower_file,
591 unsigned long lower_page_index, int byte_offset,
592 int region_bytes)
593{
594 int rc = 0;
595
596retry:
597 *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index);
598 if (!(*lower_page)) {
599 rc = -EINVAL;
600 ecryptfs_printk(KERN_ERR, "Error attempting to grab "
601 "lower page with index [0x%.16x]\n",
602 lower_page_index);
603 goto out;
604 }
605 rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
606 (*lower_page),
607 byte_offset,
608 region_bytes);
609 if (rc) {
610 if (rc == AOP_TRUNCATED_PAGE) {
611 ecryptfs_release_lower_page(*lower_page, 0);
612 goto retry;
613 } else {
614 ecryptfs_printk(KERN_ERR, "prepare_write for "
615 "lower_page_index = [0x%.16x] failed; rc = "
616 "[%d]\n", lower_page_index, rc);
617 ecryptfs_release_lower_page(*lower_page, 1);
618 (*lower_page) = NULL;
619 }
620 }
621out:
622 return rc;
623}
624
625/**
626 * ecryptfs_commit_lower_page
627 *
628 * Returns zero on success; non-zero on error
629 */
630int
631ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
632 struct file *lower_file, int byte_offset,
633 int region_size)
634{
635 int page_locked = 1;
636 int rc = 0;
637
638 rc = lower_inode->i_mapping->a_ops->commit_write(
639 lower_file, lower_page, byte_offset, region_size);
640 if (rc == AOP_TRUNCATED_PAGE)
641 page_locked = 0;
642 if (rc < 0) {
643 ecryptfs_printk(KERN_ERR,
644 "Error committing write; rc = [%d]\n", rc);
645 } else
646 rc = 0;
647 ecryptfs_release_lower_page(lower_page, page_locked);
648 return rc;
649} 391}
650 392
651/** 393/**
652 * ecryptfs_copy_page_to_lower
653 *
654 * Used for plaintext pass-through; no page index interpolation
655 * required.
656 */
657int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
658 struct file *lower_file)
659{
660 int rc = 0;
661 struct page *lower_page;
662
663 rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
664 page->index, 0, PAGE_CACHE_SIZE);
665 if (rc) {
666 ecryptfs_printk(KERN_ERR, "Error attempting to get page "
667 "at index [0x%.16x]\n", page->index);
668 goto out;
669 }
670 /* TODO: aops */
671 memcpy((char *)page_address(lower_page), page_address(page),
672 PAGE_CACHE_SIZE);
673 rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
674 0, PAGE_CACHE_SIZE);
675 if (rc)
676 ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
677 "at index [0x%.16x]\n", page->index);
678out:
679 return rc;
680}
681
682struct kmem_cache *ecryptfs_xattr_cache;
683
684/**
685 * ecryptfs_commit_write 394 * ecryptfs_commit_write
686 * @file: The eCryptfs file object 395 * @file: The eCryptfs file object
687 * @page: The eCryptfs page 396 * @page: The eCryptfs page
@@ -695,20 +404,12 @@ struct kmem_cache *ecryptfs_xattr_cache;
695static int ecryptfs_commit_write(struct file *file, struct page *page, 404static int ecryptfs_commit_write(struct file *file, struct page *page,
696 unsigned from, unsigned to) 405 unsigned from, unsigned to)
697{ 406{
698 struct ecryptfs_page_crypt_context ctx;
699 loff_t pos; 407 loff_t pos;
700 struct inode *inode; 408 struct inode *ecryptfs_inode = page->mapping->host;
701 struct inode *lower_inode; 409 struct ecryptfs_crypt_stat *crypt_stat =
702 struct file *lower_file; 410 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
703 struct ecryptfs_crypt_stat *crypt_stat;
704 int rc; 411 int rc;
705 412
706 inode = page->mapping->host;
707 lower_inode = ecryptfs_inode_to_lower(inode);
708 lower_file = ecryptfs_file_to_lower(file);
709 mutex_lock(&lower_inode->i_mutex);
710 crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
711 ->crypt_stat;
712 if (crypt_stat->flags & ECRYPTFS_NEW_FILE) { 413 if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
713 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " 414 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
714 "crypt_stat at memory location [%p]\n", crypt_stat); 415 "crypt_stat at memory location [%p]\n", crypt_stat);
@@ -718,6 +419,7 @@ static int ecryptfs_commit_write(struct file *file, struct page *page,
718 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" 419 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
719 "(page w/ index = [0x%.16x], to = [%d])\n", page->index, 420 "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
720 to); 421 to);
422 /* Fills in zeros if 'to' goes beyond inode size */
721 rc = fill_zeros_to_end_of_page(page, to); 423 rc = fill_zeros_to_end_of_page(page, to);
722 if (rc) { 424 if (rc) {
723 ecryptfs_printk(KERN_WARNING, "Error attempting to fill " 425 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
@@ -725,82 +427,22 @@ static int ecryptfs_commit_write(struct file *file, struct page *page,
725 page->index); 427 page->index);
726 goto out; 428 goto out;
727 } 429 }
728 ctx.page = page; 430 rc = ecryptfs_encrypt_page(page);
729 ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
730 ctx.param.lower_file = lower_file;
731 rc = ecryptfs_encrypt_page(&ctx);
732 if (rc) { 431 if (rc) {
733 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " 432 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
734 "index [0x%.16x])\n", page->index); 433 "index [0x%.16x])\n", page->index);
735 goto out; 434 goto out;
736 } 435 }
737 inode->i_blocks = lower_inode->i_blocks; 436 pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
738 pos = page_offset(page) + to; 437 if (pos > i_size_read(ecryptfs_inode)) {
739 if (pos > i_size_read(inode)) { 438 i_size_write(ecryptfs_inode, pos);
740 i_size_write(inode, pos);
741 ecryptfs_printk(KERN_DEBUG, "Expanded file size to " 439 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
742 "[0x%.16x]\n", i_size_read(inode)); 440 "[0x%.16x]\n", i_size_read(ecryptfs_inode));
743 } 441 }
744 rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode, 442 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
745 inode, file->f_dentry,
746 ECRYPTFS_LOWER_I_MUTEX_HELD);
747 if (rc) 443 if (rc)
748 printk(KERN_ERR "Error writing inode size to metadata; " 444 printk(KERN_ERR "Error writing inode size to metadata; "
749 "rc = [%d]\n", rc); 445 "rc = [%d]\n", rc);
750 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
751 mark_inode_dirty_sync(inode);
752out:
753 if (rc < 0)
754 ClearPageUptodate(page);
755 else
756 SetPageUptodate(page);
757 mutex_unlock(&lower_inode->i_mutex);
758 return rc;
759}
760
761/**
762 * ecryptfs_write_zeros
763 * @file: The ecryptfs file
764 * @index: The index in which we are writing
765 * @start: The position after the last block of data
766 * @num_zeros: The number of zeros to write
767 *
768 * Write a specified number of zero's to a page.
769 *
770 * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
771 */
772int
773ecryptfs_write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
774{
775 int rc = 0;
776 struct page *tmp_page;
777
778 tmp_page = ecryptfs_get1page(file, index);
779 if (IS_ERR(tmp_page)) {
780 ecryptfs_printk(KERN_ERR, "Error getting page at index "
781 "[0x%.16x]\n", index);
782 rc = PTR_ERR(tmp_page);
783 goto out;
784 }
785 if ((rc = ecryptfs_prepare_write_no_truncate(file, tmp_page, start,
786 (start + num_zeros)))) {
787 ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
788 "to page at index [0x%.16x]\n",
789 index);
790 page_cache_release(tmp_page);
791 goto out;
792 }
793 zero_user_page(tmp_page, start, num_zeros, KM_USER0);
794 rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
795 if (rc < 0) {
796 ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
797 "to remainder of page at index [0x%.16x]\n",
798 index);
799 page_cache_release(tmp_page);
800 goto out;
801 }
802 rc = 0;
803 page_cache_release(tmp_page);
804out: 446out:
805 return rc; 447 return rc;
806} 448}
@@ -819,33 +461,10 @@ static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
819 return rc; 461 return rc;
820} 462}
821 463
822static void ecryptfs_sync_page(struct page *page)
823{
824 struct inode *inode;
825 struct inode *lower_inode;
826 struct page *lower_page;
827
828 inode = page->mapping->host;
829 lower_inode = ecryptfs_inode_to_lower(inode);
830 /* NOTE: Recently swapped with grab_cache_page(), since
831 * sync_page() just makes sure that pending I/O gets done. */
832 lower_page = find_lock_page(lower_inode->i_mapping, page->index);
833 if (!lower_page) {
834 ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n");
835 return;
836 }
837 lower_page->mapping->a_ops->sync_page(lower_page);
838 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
839 lower_page->index);
840 unlock_page(lower_page);
841 page_cache_release(lower_page);
842}
843
844struct address_space_operations ecryptfs_aops = { 464struct address_space_operations ecryptfs_aops = {
845 .writepage = ecryptfs_writepage, 465 .writepage = ecryptfs_writepage,
846 .readpage = ecryptfs_readpage, 466 .readpage = ecryptfs_readpage,
847 .prepare_write = ecryptfs_prepare_write, 467 .prepare_write = ecryptfs_prepare_write,
848 .commit_write = ecryptfs_commit_write, 468 .commit_write = ecryptfs_commit_write,
849 .bmap = ecryptfs_bmap, 469 .bmap = ecryptfs_bmap,
850 .sync_page = ecryptfs_sync_page,
851}; 470};