Commit | Line | Data |
---|---|---|
237fead6 MH |
1 | /** |
2 | * eCryptfs: Linux filesystem encryption layer | |
3 | * This is where eCryptfs coordinates the symmetric encryption and | |
4 | * decryption of the file data as it passes between the lower | |
5 | * encrypted file and the upper decrypted file. | |
6 | * | |
7 | * Copyright (C) 1997-2003 Erez Zadok | |
8 | * Copyright (C) 2001-2003 Stony Brook University | |
dd2a3b7a | 9 | * Copyright (C) 2004-2007 International Business Machines Corp. |
237fead6 MH |
10 | * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; if not, write to the Free Software | |
24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
25 | * 02111-1307, USA. | |
26 | */ | |
27 | ||
28 | #include <linux/pagemap.h> | |
29 | #include <linux/writeback.h> | |
30 | #include <linux/page-flags.h> | |
31 | #include <linux/mount.h> | |
32 | #include <linux/file.h> | |
33 | #include <linux/crypto.h> | |
34 | #include <linux/scatterlist.h> | |
35 | #include "ecryptfs_kernel.h" | |
36 | ||
37 | struct kmem_cache *ecryptfs_lower_page_cache; | |
38 | ||
39 | /** | |
40 | * ecryptfs_get1page | |
41 | * | |
42 | * Get one page from cache or lower f/s, return error otherwise. | |
43 | * | |
44 | * Returns unlocked and up-to-date page (if ok), with increased | |
45 | * refcnt. | |
46 | */ | |
da0102a1 | 47 | struct page *ecryptfs_get1page(struct file *file, loff_t index) |
237fead6 | 48 | { |
237fead6 MH |
49 | struct dentry *dentry; |
50 | struct inode *inode; | |
51 | struct address_space *mapping; | |
52 | ||
bd243a4b | 53 | dentry = file->f_path.dentry; |
237fead6 MH |
54 | inode = dentry->d_inode; |
55 | mapping = inode->i_mapping; | |
6fe6900e | 56 | return read_mapping_page(mapping, index, (void *)file); |
237fead6 MH |
57 | } |
58 | ||
237fead6 MH |
59 | /** |
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 | */ | |
73 | int ecryptfs_fill_zeros(struct file *file, loff_t new_length) | |
74 | { | |
75 | int rc = 0; | |
bd243a4b | 76 | struct dentry *dentry = file->f_path.dentry; |
237fead6 MH |
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 */ | |
240e2df5 MH |
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)); | |
237fead6 | 105 | if (rc) |
240e2df5 MH |
106 | ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(" |
107 | "file=[%p], " | |
237fead6 MH |
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 */ | |
240e2df5 | 120 | rc = ecryptfs_write_zeros(file, index, (old_end_pos_in_page + 1), |
237fead6 MH |
121 | ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page)); |
122 | if (rc) { | |
240e2df5 | 123 | ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file=[%p], " |
237fead6 MH |
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 */ | |
240e2df5 | 134 | rc = ecryptfs_write_zeros(file, index, 0, PAGE_CACHE_SIZE); |
237fead6 | 135 | if (rc) { |
240e2df5 MH |
136 | ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(" |
137 | "file=[%p], " | |
237fead6 MH |
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 */ | |
240e2df5 | 153 | rc = ecryptfs_write_zeros(file, index, 0, (new_end_pos_in_page + 1)); |
237fead6 | 154 | if (rc) { |
240e2df5 | 155 | ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file=" |
237fead6 MH |
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 | } | |
162 | out: | |
163 | return rc; | |
164 | } | |
165 | ||
166 | /** | |
167 | * ecryptfs_writepage | |
168 | * @page: Page that is locked before this call is made | |
169 | * | |
170 | * Returns zero on success; non-zero otherwise | |
171 | */ | |
172 | static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) | |
173 | { | |
237fead6 MH |
174 | int rc; |
175 | ||
0216f7f7 | 176 | rc = ecryptfs_encrypt_page(page); |
237fead6 MH |
177 | if (rc) { |
178 | ecryptfs_printk(KERN_WARNING, "Error encrypting " | |
179 | "page (upper index [0x%.16x])\n", page->index); | |
180 | ClearPageUptodate(page); | |
181 | goto out; | |
182 | } | |
183 | SetPageUptodate(page); | |
184 | unlock_page(page); | |
185 | out: | |
186 | return rc; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Reads the data from the lower file file at index lower_page_index | |
191 | * and copies that data into page. | |
192 | * | |
193 | * @param page Page to fill | |
194 | * @param lower_page_index Index of the page in the lower file to get | |
195 | */ | |
196 | int ecryptfs_do_readpage(struct file *file, struct page *page, | |
197 | pgoff_t lower_page_index) | |
198 | { | |
199 | int rc; | |
200 | struct dentry *dentry; | |
201 | struct file *lower_file; | |
202 | struct dentry *lower_dentry; | |
203 | struct inode *inode; | |
204 | struct inode *lower_inode; | |
205 | char *page_data; | |
206 | struct page *lower_page = NULL; | |
207 | char *lower_page_data; | |
208 | const struct address_space_operations *lower_a_ops; | |
209 | ||
bd243a4b | 210 | dentry = file->f_path.dentry; |
237fead6 MH |
211 | lower_file = ecryptfs_file_to_lower(file); |
212 | lower_dentry = ecryptfs_dentry_to_lower(dentry); | |
213 | inode = dentry->d_inode; | |
214 | lower_inode = ecryptfs_inode_to_lower(inode); | |
215 | lower_a_ops = lower_inode->i_mapping->a_ops; | |
216 | lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index, | |
217 | (filler_t *)lower_a_ops->readpage, | |
218 | (void *)lower_file); | |
219 | if (IS_ERR(lower_page)) { | |
220 | rc = PTR_ERR(lower_page); | |
221 | lower_page = NULL; | |
222 | ecryptfs_printk(KERN_ERR, "Error reading from page cache\n"); | |
223 | goto out; | |
224 | } | |
9d8b8ce5 MH |
225 | page_data = kmap_atomic(page, KM_USER0); |
226 | lower_page_data = kmap_atomic(lower_page, KM_USER1); | |
237fead6 | 227 | memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE); |
9d8b8ce5 MH |
228 | kunmap_atomic(lower_page_data, KM_USER1); |
229 | kunmap_atomic(page_data, KM_USER0); | |
0a9ac382 | 230 | flush_dcache_page(page); |
237fead6 MH |
231 | rc = 0; |
232 | out: | |
233 | if (likely(lower_page)) | |
234 | page_cache_release(lower_page); | |
235 | if (rc == 0) | |
236 | SetPageUptodate(page); | |
237 | else | |
238 | ClearPageUptodate(page); | |
239 | return rc; | |
240 | } | |
e77a56dd MH |
241 | /** |
242 | * Header Extent: | |
243 | * Octets 0-7: Unencrypted file size (big-endian) | |
244 | * Octets 8-15: eCryptfs special marker | |
245 | * Octets 16-19: Flags | |
246 | * Octet 16: File format version number (between 0 and 255) | |
247 | * Octets 17-18: Reserved | |
248 | * Octet 19: Bit 1 (lsb): Reserved | |
249 | * Bit 2: Encrypted? | |
250 | * Bits 3-8: Reserved | |
251 | * Octets 20-23: Header extent size (big-endian) | |
252 | * Octets 24-25: Number of header extents at front of file | |
253 | * (big-endian) | |
254 | * Octet 26: Begin RFC 2440 authentication token packet set | |
255 | */ | |
256 | static void set_header_info(char *page_virt, | |
257 | struct ecryptfs_crypt_stat *crypt_stat) | |
258 | { | |
259 | size_t written; | |
260 | int save_num_header_extents_at_front = | |
261 | crypt_stat->num_header_extents_at_front; | |
262 | ||
263 | crypt_stat->num_header_extents_at_front = 1; | |
264 | ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written); | |
265 | crypt_stat->num_header_extents_at_front = | |
266 | save_num_header_extents_at_front; | |
267 | } | |
237fead6 MH |
268 | |
269 | /** | |
270 | * ecryptfs_readpage | |
271 | * @file: This is an ecryptfs file | |
272 | * @page: ecryptfs associated page to stick the read data into | |
273 | * | |
274 | * Read in a page, decrypting if necessary. | |
275 | * | |
276 | * Returns zero on success; non-zero on error. | |
277 | */ | |
278 | static int ecryptfs_readpage(struct file *file, struct page *page) | |
279 | { | |
280 | int rc = 0; | |
281 | struct ecryptfs_crypt_stat *crypt_stat; | |
282 | ||
bd243a4b JJS |
283 | BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode)); |
284 | crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode) | |
285 | ->crypt_stat; | |
237fead6 | 286 | if (!crypt_stat |
e2bd99ec MH |
287 | || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED) |
288 | || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { | |
237fead6 MH |
289 | ecryptfs_printk(KERN_DEBUG, |
290 | "Passing through unencrypted page\n"); | |
291 | rc = ecryptfs_do_readpage(file, page, page->index); | |
292 | if (rc) { | |
293 | ecryptfs_printk(KERN_ERR, "Error reading page; rc = " | |
294 | "[%d]\n", rc); | |
295 | goto out; | |
296 | } | |
e77a56dd MH |
297 | } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { |
298 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { | |
299 | int num_pages_in_header_region = | |
45eaab79 | 300 | (crypt_stat->extent_size |
e77a56dd MH |
301 | / PAGE_CACHE_SIZE); |
302 | ||
303 | if (page->index < num_pages_in_header_region) { | |
304 | char *page_virt; | |
305 | ||
9d8b8ce5 | 306 | page_virt = kmap_atomic(page, KM_USER0); |
e77a56dd MH |
307 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
308 | if (page->index == 0) { | |
309 | rc = ecryptfs_read_xattr_region( | |
d7cdc5fe | 310 | page_virt, page->mapping->host); |
e77a56dd MH |
311 | set_header_info(page_virt, crypt_stat); |
312 | } | |
9d8b8ce5 | 313 | kunmap_atomic(page_virt, KM_USER0); |
0a9ac382 | 314 | flush_dcache_page(page); |
e77a56dd MH |
315 | if (rc) { |
316 | printk(KERN_ERR "Error reading xattr " | |
317 | "region\n"); | |
318 | goto out; | |
319 | } | |
320 | } else { | |
321 | rc = ecryptfs_do_readpage( | |
322 | file, page, | |
323 | (page->index | |
324 | - num_pages_in_header_region)); | |
325 | if (rc) { | |
326 | printk(KERN_ERR "Error reading page; " | |
327 | "rc = [%d]\n", rc); | |
328 | goto out; | |
329 | } | |
330 | } | |
331 | } else { | |
332 | rc = ecryptfs_do_readpage(file, page, page->index); | |
333 | if (rc) { | |
334 | printk(KERN_ERR "Error reading page; rc = " | |
335 | "[%d]\n", rc); | |
336 | goto out; | |
337 | } | |
338 | } | |
237fead6 | 339 | } else { |
0216f7f7 | 340 | rc = ecryptfs_decrypt_page(page); |
237fead6 | 341 | if (rc) { |
237fead6 MH |
342 | ecryptfs_printk(KERN_ERR, "Error decrypting page; " |
343 | "rc = [%d]\n", rc); | |
344 | goto out; | |
345 | } | |
346 | } | |
347 | SetPageUptodate(page); | |
348 | out: | |
349 | if (rc) | |
350 | ClearPageUptodate(page); | |
351 | ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", | |
352 | page->index); | |
353 | unlock_page(page); | |
354 | return rc; | |
355 | } | |
356 | ||
dd2a3b7a MH |
357 | /** |
358 | * Called with lower inode mutex held. | |
359 | */ | |
237fead6 MH |
360 | static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) |
361 | { | |
362 | struct inode *inode = page->mapping->host; | |
363 | int end_byte_in_page; | |
237fead6 | 364 | |
9d8b8ce5 MH |
365 | if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index) |
366 | goto out; | |
367 | end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; | |
368 | if (to > end_byte_in_page) | |
369 | end_byte_in_page = to; | |
c9f2875b ND |
370 | zero_user_page(page, end_byte_in_page, |
371 | PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0); | |
237fead6 | 372 | out: |
9d8b8ce5 | 373 | return 0; |
237fead6 MH |
374 | } |
375 | ||
53a2731f MH |
376 | /** |
377 | * eCryptfs does not currently support holes. When writing after a | |
378 | * seek past the end of the file, eCryptfs fills in 0's through to the | |
379 | * current location. The code to fill in the 0's to all the | |
380 | * intermediate pages calls ecryptfs_prepare_write_no_truncate(). | |
381 | */ | |
382 | static int | |
383 | ecryptfs_prepare_write_no_truncate(struct file *file, struct page *page, | |
384 | unsigned from, unsigned to) | |
385 | { | |
386 | int rc = 0; | |
387 | ||
388 | if (from == 0 && to == PAGE_CACHE_SIZE) | |
389 | goto out; /* If we are writing a full page, it will be | |
390 | up to date. */ | |
391 | if (!PageUptodate(page)) | |
392 | rc = ecryptfs_do_readpage(file, page, page->index); | |
393 | out: | |
394 | return rc; | |
395 | } | |
396 | ||
237fead6 MH |
397 | static int ecryptfs_prepare_write(struct file *file, struct page *page, |
398 | unsigned from, unsigned to) | |
399 | { | |
400 | int rc = 0; | |
401 | ||
237fead6 MH |
402 | if (from == 0 && to == PAGE_CACHE_SIZE) |
403 | goto out; /* If we are writing a full page, it will be | |
404 | up to date. */ | |
405 | if (!PageUptodate(page)) | |
406 | rc = ecryptfs_do_readpage(file, page, page->index); | |
240e2df5 | 407 | if (page->index != 0) { |
18336338 | 408 | loff_t end_of_prev_pg_pos = page_offset(page) - 1; |
240e2df5 MH |
409 | |
410 | if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) { | |
411 | rc = ecryptfs_truncate(file->f_path.dentry, | |
412 | end_of_prev_pg_pos); | |
413 | if (rc) { | |
414 | printk(KERN_ERR "Error on attempt to " | |
415 | "truncate to (higher) offset [%lld];" | |
416 | " rc = [%d]\n", end_of_prev_pg_pos, rc); | |
417 | goto out; | |
418 | } | |
53a2731f | 419 | } |
d4c5cdb3 MH |
420 | if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host)) |
421 | zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0); | |
53a2731f | 422 | } |
237fead6 MH |
423 | out: |
424 | return rc; | |
425 | } | |
426 | ||
237fead6 MH |
427 | int ecryptfs_writepage_and_release_lower_page(struct page *lower_page, |
428 | struct inode *lower_inode, | |
429 | struct writeback_control *wbc) | |
430 | { | |
431 | int rc = 0; | |
432 | ||
433 | rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc); | |
434 | if (rc) { | |
435 | ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); " | |
436 | "rc = [%d]\n", rc); | |
437 | goto out; | |
438 | } | |
439 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; | |
440 | page_cache_release(lower_page); | |
441 | out: | |
442 | return rc; | |
443 | } | |
444 | ||
55144768 | 445 | static void ecryptfs_release_lower_page(struct page *lower_page) |
237fead6 | 446 | { |
55144768 | 447 | unlock_page(lower_page); |
237fead6 MH |
448 | page_cache_release(lower_page); |
449 | } | |
450 | ||
451 | /** | |
452 | * ecryptfs_write_inode_size_to_header | |
453 | * | |
454 | * Writes the lower file size to the first 8 bytes of the header. | |
455 | * | |
456 | * Returns zero on success; non-zero on error. | |
457 | */ | |
0216f7f7 | 458 | static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode) |
237fead6 | 459 | { |
237fead6 | 460 | u64 file_size; |
0216f7f7 MH |
461 | char *file_size_virt; |
462 | int rc; | |
237fead6 | 463 | |
0216f7f7 MH |
464 | file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL); |
465 | if (!file_size_virt) { | |
466 | rc = -ENOMEM; | |
ae73fc09 MH |
467 | goto out; |
468 | } | |
0216f7f7 | 469 | file_size = (u64)i_size_read(ecryptfs_inode); |
237fead6 | 470 | file_size = cpu_to_be64(file_size); |
0216f7f7 MH |
471 | memcpy(file_size_virt, &file_size, sizeof(u64)); |
472 | rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0, | |
473 | sizeof(u64)); | |
474 | kfree(file_size_virt); | |
475 | if (rc) | |
476 | printk(KERN_ERR "%s: Error writing file size to header; " | |
477 | "rc = [%d]\n", __FUNCTION__, rc); | |
237fead6 MH |
478 | out: |
479 | return rc; | |
480 | } | |
481 | ||
0216f7f7 MH |
482 | struct kmem_cache *ecryptfs_xattr_cache; |
483 | ||
484 | static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode) | |
dd2a3b7a MH |
485 | { |
486 | ssize_t size; | |
487 | void *xattr_virt; | |
0216f7f7 MH |
488 | struct dentry *lower_dentry = |
489 | ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry; | |
490 | struct inode *lower_inode = lower_dentry->d_inode; | |
dd2a3b7a MH |
491 | u64 file_size; |
492 | int rc; | |
493 | ||
0216f7f7 MH |
494 | if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) { |
495 | printk(KERN_WARNING | |
496 | "No support for setting xattr in lower filesystem\n"); | |
497 | rc = -ENOSYS; | |
498 | goto out; | |
499 | } | |
dd2a3b7a MH |
500 | xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); |
501 | if (!xattr_virt) { | |
502 | printk(KERN_ERR "Out of memory whilst attempting to write " | |
503 | "inode size to xattr\n"); | |
504 | rc = -ENOMEM; | |
505 | goto out; | |
506 | } | |
0216f7f7 MH |
507 | mutex_lock(&lower_inode->i_mutex); |
508 | size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME, | |
509 | xattr_virt, PAGE_CACHE_SIZE); | |
dd2a3b7a MH |
510 | if (size < 0) |
511 | size = 8; | |
0216f7f7 | 512 | file_size = (u64)i_size_read(ecryptfs_inode); |
dd2a3b7a MH |
513 | file_size = cpu_to_be64(file_size); |
514 | memcpy(xattr_virt, &file_size, sizeof(u64)); | |
0216f7f7 MH |
515 | rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME, |
516 | xattr_virt, size, 0); | |
517 | mutex_unlock(&lower_inode->i_mutex); | |
dd2a3b7a MH |
518 | if (rc) |
519 | printk(KERN_ERR "Error whilst attempting to write inode size " | |
520 | "to lower file xattr; rc = [%d]\n", rc); | |
521 | kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); | |
522 | out: | |
523 | return rc; | |
524 | } | |
525 | ||
0216f7f7 | 526 | int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode) |
dd2a3b7a MH |
527 | { |
528 | struct ecryptfs_crypt_stat *crypt_stat; | |
529 | ||
0216f7f7 | 530 | crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
dd2a3b7a | 531 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
0216f7f7 | 532 | return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode); |
dd2a3b7a | 533 | else |
0216f7f7 | 534 | return ecryptfs_write_inode_size_to_header(ecryptfs_inode); |
dd2a3b7a MH |
535 | } |
536 | ||
237fead6 MH |
537 | int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode, |
538 | struct file *lower_file, | |
539 | unsigned long lower_page_index, int byte_offset, | |
540 | int region_bytes) | |
541 | { | |
542 | int rc = 0; | |
543 | ||
9d8b8ce5 MH |
544 | *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index); |
545 | if (!(*lower_page)) { | |
546 | rc = -EINVAL; | |
547 | ecryptfs_printk(KERN_ERR, "Error attempting to grab " | |
237fead6 MH |
548 | "lower page with index [0x%.16x]\n", |
549 | lower_page_index); | |
550 | goto out; | |
551 | } | |
552 | rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file, | |
553 | (*lower_page), | |
554 | byte_offset, | |
555 | region_bytes); | |
556 | if (rc) { | |
55144768 NP |
557 | ecryptfs_printk(KERN_ERR, "prepare_write for " |
558 | "lower_page_index = [0x%.16x] failed; rc = " | |
559 | "[%d]\n", lower_page_index, rc); | |
560 | ecryptfs_release_lower_page(*lower_page); | |
561 | (*lower_page) = NULL; | |
237fead6 | 562 | } |
a8fa74ab | 563 | out: |
237fead6 MH |
564 | return rc; |
565 | } | |
566 | ||
567 | /** | |
568 | * ecryptfs_commit_lower_page | |
569 | * | |
570 | * Returns zero on success; non-zero on error | |
571 | */ | |
572 | int | |
573 | ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode, | |
574 | struct file *lower_file, int byte_offset, | |
575 | int region_size) | |
576 | { | |
577 | int rc = 0; | |
578 | ||
579 | rc = lower_inode->i_mapping->a_ops->commit_write( | |
580 | lower_file, lower_page, byte_offset, region_size); | |
581 | if (rc < 0) { | |
582 | ecryptfs_printk(KERN_ERR, | |
583 | "Error committing write; rc = [%d]\n", rc); | |
584 | } else | |
585 | rc = 0; | |
55144768 | 586 | ecryptfs_release_lower_page(lower_page); |
237fead6 MH |
587 | return rc; |
588 | } | |
589 | ||
590 | /** | |
591 | * ecryptfs_copy_page_to_lower | |
592 | * | |
593 | * Used for plaintext pass-through; no page index interpolation | |
594 | * required. | |
595 | */ | |
596 | int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode, | |
597 | struct file *lower_file) | |
598 | { | |
599 | int rc = 0; | |
600 | struct page *lower_page; | |
601 | ||
602 | rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file, | |
603 | page->index, 0, PAGE_CACHE_SIZE); | |
604 | if (rc) { | |
605 | ecryptfs_printk(KERN_ERR, "Error attempting to get page " | |
606 | "at index [0x%.16x]\n", page->index); | |
607 | goto out; | |
608 | } | |
609 | /* TODO: aops */ | |
610 | memcpy((char *)page_address(lower_page), page_address(page), | |
611 | PAGE_CACHE_SIZE); | |
612 | rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file, | |
613 | 0, PAGE_CACHE_SIZE); | |
614 | if (rc) | |
615 | ecryptfs_printk(KERN_ERR, "Error attempting to commit page " | |
616 | "at index [0x%.16x]\n", page->index); | |
617 | out: | |
618 | return rc; | |
619 | } | |
620 | ||
237fead6 MH |
621 | /** |
622 | * ecryptfs_commit_write | |
623 | * @file: The eCryptfs file object | |
624 | * @page: The eCryptfs page | |
625 | * @from: Ignored (we rotate the page IV on each write) | |
626 | * @to: Ignored | |
627 | * | |
628 | * This is where we encrypt the data and pass the encrypted data to | |
629 | * the lower filesystem. In OpenPGP-compatible mode, we operate on | |
630 | * entire underlying packets. | |
631 | */ | |
632 | static int ecryptfs_commit_write(struct file *file, struct page *page, | |
633 | unsigned from, unsigned to) | |
634 | { | |
237fead6 MH |
635 | loff_t pos; |
636 | struct inode *inode; | |
637 | struct inode *lower_inode; | |
638 | struct file *lower_file; | |
639 | struct ecryptfs_crypt_stat *crypt_stat; | |
640 | int rc; | |
641 | ||
642 | inode = page->mapping->host; | |
643 | lower_inode = ecryptfs_inode_to_lower(inode); | |
644 | lower_file = ecryptfs_file_to_lower(file); | |
645 | mutex_lock(&lower_inode->i_mutex); | |
bd243a4b JJS |
646 | crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode) |
647 | ->crypt_stat; | |
e2bd99ec | 648 | if (crypt_stat->flags & ECRYPTFS_NEW_FILE) { |
237fead6 MH |
649 | ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " |
650 | "crypt_stat at memory location [%p]\n", crypt_stat); | |
e2bd99ec | 651 | crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE); |
237fead6 MH |
652 | } else |
653 | ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); | |
654 | ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" | |
655 | "(page w/ index = [0x%.16x], to = [%d])\n", page->index, | |
656 | to); | |
657 | rc = fill_zeros_to_end_of_page(page, to); | |
658 | if (rc) { | |
659 | ecryptfs_printk(KERN_WARNING, "Error attempting to fill " | |
660 | "zeros in page with index = [0x%.16x]\n", | |
661 | page->index); | |
662 | goto out; | |
663 | } | |
0216f7f7 | 664 | rc = ecryptfs_encrypt_page(page); |
237fead6 MH |
665 | if (rc) { |
666 | ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " | |
667 | "index [0x%.16x])\n", page->index); | |
668 | goto out; | |
669 | } | |
237fead6 | 670 | inode->i_blocks = lower_inode->i_blocks; |
18336338 | 671 | pos = page_offset(page) + to; |
237fead6 MH |
672 | if (pos > i_size_read(inode)) { |
673 | i_size_write(inode, pos); | |
674 | ecryptfs_printk(KERN_DEBUG, "Expanded file size to " | |
675 | "[0x%.16x]\n", i_size_read(inode)); | |
676 | } | |
0216f7f7 | 677 | rc = ecryptfs_write_inode_size_to_metadata(inode); |
dd2a3b7a MH |
678 | if (rc) |
679 | printk(KERN_ERR "Error writing inode size to metadata; " | |
680 | "rc = [%d]\n", rc); | |
237fead6 MH |
681 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; |
682 | mark_inode_dirty_sync(inode); | |
683 | out: | |
237fead6 MH |
684 | if (rc < 0) |
685 | ClearPageUptodate(page); | |
686 | else | |
687 | SetPageUptodate(page); | |
688 | mutex_unlock(&lower_inode->i_mutex); | |
689 | return rc; | |
690 | } | |
691 | ||
692 | /** | |
240e2df5 | 693 | * ecryptfs_write_zeros |
237fead6 MH |
694 | * @file: The ecryptfs file |
695 | * @index: The index in which we are writing | |
696 | * @start: The position after the last block of data | |
697 | * @num_zeros: The number of zeros to write | |
698 | * | |
699 | * Write a specified number of zero's to a page. | |
700 | * | |
701 | * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE | |
702 | */ | |
240e2df5 MH |
703 | int |
704 | ecryptfs_write_zeros(struct file *file, pgoff_t index, int start, int num_zeros) | |
237fead6 MH |
705 | { |
706 | int rc = 0; | |
707 | struct page *tmp_page; | |
708 | ||
709 | tmp_page = ecryptfs_get1page(file, index); | |
710 | if (IS_ERR(tmp_page)) { | |
711 | ecryptfs_printk(KERN_ERR, "Error getting page at index " | |
712 | "[0x%.16x]\n", index); | |
713 | rc = PTR_ERR(tmp_page); | |
714 | goto out; | |
715 | } | |
5dda6992 MH |
716 | rc = ecryptfs_prepare_write_no_truncate(file, tmp_page, start, |
717 | (start + num_zeros)); | |
718 | if (rc) { | |
237fead6 | 719 | ecryptfs_printk(KERN_ERR, "Error preparing to write zero's " |
53a2731f | 720 | "to page at index [0x%.16x]\n", |
237fead6 | 721 | index); |
237fead6 MH |
722 | page_cache_release(tmp_page); |
723 | goto out; | |
724 | } | |
c9f2875b | 725 | zero_user_page(tmp_page, start, num_zeros, KM_USER0); |
237fead6 MH |
726 | rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros); |
727 | if (rc < 0) { | |
728 | ecryptfs_printk(KERN_ERR, "Error attempting to write zero's " | |
729 | "to remainder of page at index [0x%.16x]\n", | |
730 | index); | |
237fead6 MH |
731 | page_cache_release(tmp_page); |
732 | goto out; | |
733 | } | |
734 | rc = 0; | |
237fead6 MH |
735 | page_cache_release(tmp_page); |
736 | out: | |
737 | return rc; | |
738 | } | |
739 | ||
740 | static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) | |
741 | { | |
742 | int rc = 0; | |
743 | struct inode *inode; | |
744 | struct inode *lower_inode; | |
745 | ||
746 | inode = (struct inode *)mapping->host; | |
747 | lower_inode = ecryptfs_inode_to_lower(inode); | |
748 | if (lower_inode->i_mapping->a_ops->bmap) | |
749 | rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, | |
750 | block); | |
751 | return rc; | |
752 | } | |
753 | ||
754 | static void ecryptfs_sync_page(struct page *page) | |
755 | { | |
756 | struct inode *inode; | |
757 | struct inode *lower_inode; | |
758 | struct page *lower_page; | |
759 | ||
760 | inode = page->mapping->host; | |
761 | lower_inode = ecryptfs_inode_to_lower(inode); | |
762 | /* NOTE: Recently swapped with grab_cache_page(), since | |
763 | * sync_page() just makes sure that pending I/O gets done. */ | |
764 | lower_page = find_lock_page(lower_inode->i_mapping, page->index); | |
765 | if (!lower_page) { | |
766 | ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n"); | |
767 | return; | |
768 | } | |
2aeb3db1 RK |
769 | if (lower_page->mapping->a_ops->sync_page) |
770 | lower_page->mapping->a_ops->sync_page(lower_page); | |
237fead6 MH |
771 | ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", |
772 | lower_page->index); | |
773 | unlock_page(lower_page); | |
774 | page_cache_release(lower_page); | |
775 | } | |
776 | ||
777 | struct address_space_operations ecryptfs_aops = { | |
778 | .writepage = ecryptfs_writepage, | |
779 | .readpage = ecryptfs_readpage, | |
780 | .prepare_write = ecryptfs_prepare_write, | |
781 | .commit_write = ecryptfs_commit_write, | |
782 | .bmap = ecryptfs_bmap, | |
783 | .sync_page = ecryptfs_sync_page, | |
784 | }; |