1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/pagemap.h>
15#include <linux/writeback.h>
16#include <linux/page-flags.h>
17#include <linux/mount.h>
18#include <linux/file.h>
19#include <linux/scatterlist.h>
20#include <linux/slab.h>
21#include <linux/xattr.h>
22#include <asm/unaligned.h>
23#include "ecryptfs_kernel.h"
24
25
26
27
28
29
30
31
32
33struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index)
34{
35 struct page *page = read_mapping_page(inode->i_mapping, index, NULL);
36 if (!IS_ERR(page))
37 lock_page(page);
38 return page;
39}
40
41
42
43
44
45
46
47
48
49
50
51static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
52{
53 int rc;
54
55 rc = ecryptfs_encrypt_page(page);
56 if (rc) {
57 ecryptfs_printk(KERN_WARNING, "Error encrypting "
58 "page (upper index [0x%.16lx])\n", page->index);
59 ClearPageUptodate(page);
60 goto out;
61 }
62 SetPageUptodate(page);
63out:
64 unlock_page(page);
65 return rc;
66}
67
68static void strip_xattr_flag(char *page_virt,
69 struct ecryptfs_crypt_stat *crypt_stat)
70{
71 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
72 size_t written;
73
74 crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR;
75 ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat,
76 &written);
77 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
78 }
79}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107static int
108ecryptfs_copy_up_encrypted_with_header(struct page *page,
109 struct ecryptfs_crypt_stat *crypt_stat)
110{
111 loff_t extent_num_in_page = 0;
112 loff_t num_extents_per_page = (PAGE_SIZE
113 / crypt_stat->extent_size);
114 int rc = 0;
115
116 while (extent_num_in_page < num_extents_per_page) {
117 loff_t view_extent_num = ((((loff_t)page->index)
118 * num_extents_per_page)
119 + extent_num_in_page);
120 size_t num_header_extents_at_front =
121 (crypt_stat->metadata_size / crypt_stat->extent_size);
122
123 if (view_extent_num < num_header_extents_at_front) {
124
125 char *page_virt;
126
127 page_virt = kmap_atomic(page);
128 memset(page_virt, 0, PAGE_SIZE);
129
130 if (view_extent_num == 0) {
131 size_t written;
132
133 rc = ecryptfs_read_xattr_region(
134 page_virt, page->mapping->host);
135 strip_xattr_flag(page_virt + 16, crypt_stat);
136 ecryptfs_write_header_metadata(page_virt + 20,
137 crypt_stat,
138 &written);
139 }
140 kunmap_atomic(page_virt);
141 flush_dcache_page(page);
142 if (rc) {
143 printk(KERN_ERR "%s: Error reading xattr "
144 "region; rc = [%d]\n", __func__, rc);
145 goto out;
146 }
147 } else {
148
149 loff_t lower_offset =
150 ((view_extent_num * crypt_stat->extent_size)
151 - crypt_stat->metadata_size);
152
153 rc = ecryptfs_read_lower_page_segment(
154 page, (lower_offset >> PAGE_SHIFT),
155 (lower_offset & ~PAGE_MASK),
156 crypt_stat->extent_size, page->mapping->host);
157 if (rc) {
158 printk(KERN_ERR "%s: Error attempting to read "
159 "extent at offset [%lld] in the lower "
160 "file; rc = [%d]\n", __func__,
161 lower_offset, rc);
162 goto out;
163 }
164 }
165 extent_num_in_page++;
166 }
167out:
168 return rc;
169}
170
171
172
173
174
175
176
177
178
179
180static int ecryptfs_readpage(struct file *file, struct page *page)
181{
182 struct ecryptfs_crypt_stat *crypt_stat =
183 &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
184 int rc = 0;
185
186 if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
187 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
188 PAGE_SIZE,
189 page->mapping->host);
190 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
191 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
192 rc = ecryptfs_copy_up_encrypted_with_header(page,
193 crypt_stat);
194 if (rc) {
195 printk(KERN_ERR "%s: Error attempting to copy "
196 "the encrypted content from the lower "
197 "file whilst inserting the metadata "
198 "from the xattr into the header; rc = "
199 "[%d]\n", __func__, rc);
200 goto out;
201 }
202
203 } else {
204 rc = ecryptfs_read_lower_page_segment(
205 page, page->index, 0, PAGE_SIZE,
206 page->mapping->host);
207 if (rc) {
208 printk(KERN_ERR "Error reading page; rc = "
209 "[%d]\n", rc);
210 goto out;
211 }
212 }
213 } else {
214 rc = ecryptfs_decrypt_page(page);
215 if (rc) {
216 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
217 "rc = [%d]\n", rc);
218 goto out;
219 }
220 }
221out:
222 if (rc)
223 ClearPageUptodate(page);
224 else
225 SetPageUptodate(page);
226 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n",
227 page->index);
228 unlock_page(page);
229 return rc;
230}
231
232
233
234
235static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
236{
237 struct inode *inode = page->mapping->host;
238 int end_byte_in_page;
239
240 if ((i_size_read(inode) / PAGE_SIZE) != page->index)
241 goto out;
242 end_byte_in_page = i_size_read(inode) % PAGE_SIZE;
243 if (to > end_byte_in_page)
244 end_byte_in_page = to;
245 zero_user_segment(page, end_byte_in_page, PAGE_SIZE);
246out:
247 return 0;
248}
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264static int ecryptfs_write_begin(struct file *file,
265 struct address_space *mapping,
266 loff_t pos, unsigned len, unsigned flags,
267 struct page **pagep, void **fsdata)
268{
269 pgoff_t index = pos >> PAGE_SHIFT;
270 struct page *page;
271 loff_t prev_page_end_size;
272 int rc = 0;
273
274 page = grab_cache_page_write_begin(mapping, index, flags);
275 if (!page)
276 return -ENOMEM;
277 *pagep = page;
278
279 prev_page_end_size = ((loff_t)index << PAGE_SHIFT);
280 if (!PageUptodate(page)) {
281 struct ecryptfs_crypt_stat *crypt_stat =
282 &ecryptfs_inode_to_private(mapping->host)->crypt_stat;
283
284 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
285 rc = ecryptfs_read_lower_page_segment(
286 page, index, 0, PAGE_SIZE, mapping->host);
287 if (rc) {
288 printk(KERN_ERR "%s: Error attempting to read "
289 "lower page segment; rc = [%d]\n",
290 __func__, rc);
291 ClearPageUptodate(page);
292 goto out;
293 } else
294 SetPageUptodate(page);
295 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
296 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
297 rc = ecryptfs_copy_up_encrypted_with_header(
298 page, crypt_stat);
299 if (rc) {
300 printk(KERN_ERR "%s: Error attempting "
301 "to copy the encrypted content "
302 "from the lower file whilst "
303 "inserting the metadata from "
304 "the xattr into the header; rc "
305 "= [%d]\n", __func__, rc);
306 ClearPageUptodate(page);
307 goto out;
308 }
309 SetPageUptodate(page);
310 } else {
311 rc = ecryptfs_read_lower_page_segment(
312 page, index, 0, PAGE_SIZE,
313 mapping->host);
314 if (rc) {
315 printk(KERN_ERR "%s: Error reading "
316 "page; rc = [%d]\n",
317 __func__, rc);
318 ClearPageUptodate(page);
319 goto out;
320 }
321 SetPageUptodate(page);
322 }
323 } else {
324 if (prev_page_end_size
325 >= i_size_read(page->mapping->host)) {
326 zero_user(page, 0, PAGE_SIZE);
327 SetPageUptodate(page);
328 } else if (len < PAGE_SIZE) {
329 rc = ecryptfs_decrypt_page(page);
330 if (rc) {
331 printk(KERN_ERR "%s: Error decrypting "
332 "page at index [%ld]; "
333 "rc = [%d]\n",
334 __func__, page->index, rc);
335 ClearPageUptodate(page);
336 goto out;
337 }
338 SetPageUptodate(page);
339 }
340 }
341 }
342
343
344 if (index != 0) {
345 if (prev_page_end_size > i_size_read(page->mapping->host)) {
346 rc = ecryptfs_truncate(file->f_path.dentry,
347 prev_page_end_size);
348 if (rc) {
349 printk(KERN_ERR "%s: Error on attempt to "
350 "truncate to (higher) offset [%lld];"
351 " rc = [%d]\n", __func__,
352 prev_page_end_size, rc);
353 goto out;
354 }
355 }
356 }
357
358
359 if ((i_size_read(mapping->host) == prev_page_end_size)
360 && (pos != 0))
361 zero_user(page, 0, PAGE_SIZE);
362out:
363 if (unlikely(rc)) {
364 unlock_page(page);
365 put_page(page);
366 *pagep = NULL;
367 }
368 return rc;
369}
370
371
372
373
374
375
376
377
378static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
379{
380 char *file_size_virt;
381 int rc;
382
383 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
384 if (!file_size_virt) {
385 rc = -ENOMEM;
386 goto out;
387 }
388 put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
389 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
390 sizeof(u64));
391 kfree(file_size_virt);
392 if (rc < 0)
393 printk(KERN_ERR "%s: Error writing file size to header; "
394 "rc = [%d]\n", __func__, rc);
395 else
396 rc = 0;
397out:
398 return rc;
399}
400
401struct kmem_cache *ecryptfs_xattr_cache;
402
403static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
404{
405 ssize_t size;
406 void *xattr_virt;
407 struct dentry *lower_dentry =
408 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
409 struct inode *lower_inode = d_inode(lower_dentry);
410 int rc;
411
412 if (!(lower_inode->i_opflags & IOP_XATTR)) {
413 printk(KERN_WARNING
414 "No support for setting xattr in lower filesystem\n");
415 rc = -ENOSYS;
416 goto out;
417 }
418 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
419 if (!xattr_virt) {
420 rc = -ENOMEM;
421 goto out;
422 }
423 inode_lock(lower_inode);
424 size = __vfs_getxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
425 xattr_virt, PAGE_SIZE);
426 if (size < 0)
427 size = 8;
428 put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
429 rc = __vfs_setxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
430 xattr_virt, size, 0);
431 inode_unlock(lower_inode);
432 if (rc)
433 printk(KERN_ERR "Error whilst attempting to write inode size "
434 "to lower file xattr; rc = [%d]\n", rc);
435 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
436out:
437 return rc;
438}
439
440int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
441{
442 struct ecryptfs_crypt_stat *crypt_stat;
443
444 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
445 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
446 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
447 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
448 else
449 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
450}
451
452
453
454
455
456
457
458
459
460
461
462static int ecryptfs_write_end(struct file *file,
463 struct address_space *mapping,
464 loff_t pos, unsigned len, unsigned copied,
465 struct page *page, void *fsdata)
466{
467 pgoff_t index = pos >> PAGE_SHIFT;
468 unsigned from = pos & (PAGE_SIZE - 1);
469 unsigned to = from + copied;
470 struct inode *ecryptfs_inode = mapping->host;
471 struct ecryptfs_crypt_stat *crypt_stat =
472 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
473 int rc;
474
475 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
476 "(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
477 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
478 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
479 to);
480 if (!rc) {
481 rc = copied;
482 fsstack_copy_inode_size(ecryptfs_inode,
483 ecryptfs_inode_to_lower(ecryptfs_inode));
484 }
485 goto out;
486 }
487 if (!PageUptodate(page)) {
488 if (copied < PAGE_SIZE) {
489 rc = 0;
490 goto out;
491 }
492 SetPageUptodate(page);
493 }
494
495 rc = fill_zeros_to_end_of_page(page, to);
496 if (rc) {
497 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
498 "zeros in page with index = [0x%.16lx]\n", index);
499 goto out;
500 }
501 rc = ecryptfs_encrypt_page(page);
502 if (rc) {
503 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
504 "index [0x%.16lx])\n", index);
505 goto out;
506 }
507 if (pos + copied > i_size_read(ecryptfs_inode)) {
508 i_size_write(ecryptfs_inode, pos + copied);
509 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
510 "[0x%.16llx]\n",
511 (unsigned long long)i_size_read(ecryptfs_inode));
512 }
513 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
514 if (rc)
515 printk(KERN_ERR "Error writing inode size to metadata; "
516 "rc = [%d]\n", rc);
517 else
518 rc = copied;
519out:
520 unlock_page(page);
521 put_page(page);
522 return rc;
523}
524
525static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
526{
527 int rc = 0;
528 struct inode *inode;
529 struct inode *lower_inode;
530
531 inode = (struct inode *)mapping->host;
532 lower_inode = ecryptfs_inode_to_lower(inode);
533 if (lower_inode->i_mapping->a_ops->bmap)
534 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
535 block);
536 return rc;
537}
538
539const struct address_space_operations ecryptfs_aops = {
540 .writepage = ecryptfs_writepage,
541 .readpage = ecryptfs_readpage,
542 .write_begin = ecryptfs_write_begin,
543 .write_end = ecryptfs_write_end,
544 .bmap = ecryptfs_bmap,
545};
546