| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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 | 
| Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 9 | * Copyright (C) 2004-2007 International Business Machines Corp. | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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 | */ | 
|  | 47 | static struct page *ecryptfs_get1page(struct file *file, int index) | 
|  | 48 | { | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 49 | struct dentry *dentry; | 
|  | 50 | struct inode *inode; | 
|  | 51 | struct address_space *mapping; | 
|  | 52 |  | 
| Josef "Jeff" Sipek | bd243a4 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 53 | dentry = file->f_path.dentry; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 54 | inode = dentry->d_inode; | 
|  | 55 | mapping = inode->i_mapping; | 
| Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 56 | return read_mapping_page(mapping, index, (void *)file); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
|  | 59 | static | 
|  | 60 | int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros); | 
|  | 61 |  | 
|  | 62 | /** | 
|  | 63 | * ecryptfs_fill_zeros | 
|  | 64 | * @file: The ecryptfs file | 
|  | 65 | * @new_length: The new length of the data in the underlying file; | 
|  | 66 | *              everything between the prior end of the file and the | 
|  | 67 | *              new end of the file will be filled with zero's. | 
|  | 68 | *              new_length must be greater than  current length | 
|  | 69 | * | 
|  | 70 | * Function for handling lseek-ing past the end of the file. | 
|  | 71 | * | 
|  | 72 | * This function does not support shrinking, only growing a file. | 
|  | 73 | * | 
|  | 74 | * Returns zero on success; non-zero otherwise. | 
|  | 75 | */ | 
|  | 76 | int ecryptfs_fill_zeros(struct file *file, loff_t new_length) | 
|  | 77 | { | 
|  | 78 | int rc = 0; | 
| Josef "Jeff" Sipek | bd243a4 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 79 | struct dentry *dentry = file->f_path.dentry; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 80 | struct inode *inode = dentry->d_inode; | 
|  | 81 | pgoff_t old_end_page_index = 0; | 
|  | 82 | pgoff_t index = old_end_page_index; | 
|  | 83 | int old_end_pos_in_page = -1; | 
|  | 84 | pgoff_t new_end_page_index; | 
|  | 85 | int new_end_pos_in_page; | 
|  | 86 | loff_t cur_length = i_size_read(inode); | 
|  | 87 |  | 
|  | 88 | if (cur_length != 0) { | 
|  | 89 | index = old_end_page_index = | 
|  | 90 | ((cur_length - 1) >> PAGE_CACHE_SHIFT); | 
|  | 91 | old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK); | 
|  | 92 | } | 
|  | 93 | new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT); | 
|  | 94 | new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK); | 
|  | 95 | ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; " | 
|  | 96 | "old_end_pos_in_page = [%d]; " | 
|  | 97 | "new_end_page_index = [0x%.16x]; " | 
|  | 98 | "new_end_pos_in_page = [%d]\n", | 
|  | 99 | old_end_page_index, old_end_pos_in_page, | 
|  | 100 | new_end_page_index, new_end_pos_in_page); | 
|  | 101 | if (old_end_page_index == new_end_page_index) { | 
|  | 102 | /* Start and end are in the same page; we just need to | 
|  | 103 | * set a portion of the existing page to zero's */ | 
|  | 104 | rc = write_zeros(file, index, (old_end_pos_in_page + 1), | 
|  | 105 | (new_end_pos_in_page - old_end_pos_in_page)); | 
|  | 106 | if (rc) | 
|  | 107 | ecryptfs_printk(KERN_ERR, "write_zeros(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 = 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, "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 = write_zeros(file, index, 0, PAGE_CACHE_SIZE); | 
|  | 135 | if (rc) { | 
|  | 136 | ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], " | 
|  | 137 | "index=[0x%.16x], " | 
|  | 138 | "old_end_pos_in_page=[d], " | 
|  | 139 | "(PAGE_CACHE_SIZE - new_end_pos_in_page" | 
|  | 140 | "=[%d]" | 
|  | 141 | ")=[d]) returned [%d]\n", file, index, | 
|  | 142 | old_end_pos_in_page, | 
|  | 143 | new_end_pos_in_page, | 
|  | 144 | (PAGE_CACHE_SIZE - new_end_pos_in_page), | 
|  | 145 | rc); | 
|  | 146 | goto out; | 
|  | 147 | } | 
|  | 148 | index++; | 
|  | 149 | } | 
|  | 150 | /* Fill the portion at the beginning of the last new page with | 
|  | 151 | * zero's */ | 
|  | 152 | rc = write_zeros(file, index, 0, (new_end_pos_in_page + 1)); | 
|  | 153 | if (rc) { | 
|  | 154 | ecryptfs_printk(KERN_ERR, "write_zeros(file=" | 
|  | 155 | "[%p], index=[0x%.16x], 0, " | 
|  | 156 | "new_end_pos_in_page=[%d]" | 
|  | 157 | "returned [%d]\n", file, index, | 
|  | 158 | new_end_pos_in_page, rc); | 
|  | 159 | goto out; | 
|  | 160 | } | 
|  | 161 | out: | 
|  | 162 | return rc; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | /** | 
|  | 166 | * ecryptfs_writepage | 
|  | 167 | * @page: Page that is locked before this call is made | 
|  | 168 | * | 
|  | 169 | * Returns zero on success; non-zero otherwise | 
|  | 170 | */ | 
|  | 171 | static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) | 
|  | 172 | { | 
|  | 173 | struct ecryptfs_page_crypt_context ctx; | 
|  | 174 | int rc; | 
|  | 175 |  | 
|  | 176 | ctx.page = page; | 
|  | 177 | ctx.mode = ECRYPTFS_WRITEPAGE_MODE; | 
|  | 178 | ctx.param.wbc = wbc; | 
|  | 179 | rc = ecryptfs_encrypt_page(&ctx); | 
|  | 180 | if (rc) { | 
|  | 181 | ecryptfs_printk(KERN_WARNING, "Error encrypting " | 
|  | 182 | "page (upper index [0x%.16x])\n", page->index); | 
|  | 183 | ClearPageUptodate(page); | 
|  | 184 | goto out; | 
|  | 185 | } | 
|  | 186 | SetPageUptodate(page); | 
|  | 187 | unlock_page(page); | 
|  | 188 | out: | 
|  | 189 | return rc; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | /** | 
|  | 193 | * Reads the data from the lower file file at index lower_page_index | 
|  | 194 | * and copies that data into page. | 
|  | 195 | * | 
|  | 196 | * @param page	Page to fill | 
|  | 197 | * @param lower_page_index Index of the page in the lower file to get | 
|  | 198 | */ | 
|  | 199 | int ecryptfs_do_readpage(struct file *file, struct page *page, | 
|  | 200 | pgoff_t lower_page_index) | 
|  | 201 | { | 
|  | 202 | int rc; | 
|  | 203 | struct dentry *dentry; | 
|  | 204 | struct file *lower_file; | 
|  | 205 | struct dentry *lower_dentry; | 
|  | 206 | struct inode *inode; | 
|  | 207 | struct inode *lower_inode; | 
|  | 208 | char *page_data; | 
|  | 209 | struct page *lower_page = NULL; | 
|  | 210 | char *lower_page_data; | 
|  | 211 | const struct address_space_operations *lower_a_ops; | 
|  | 212 |  | 
| Josef "Jeff" Sipek | bd243a4 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 213 | dentry = file->f_path.dentry; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 214 | lower_file = ecryptfs_file_to_lower(file); | 
|  | 215 | lower_dentry = ecryptfs_dentry_to_lower(dentry); | 
|  | 216 | inode = dentry->d_inode; | 
|  | 217 | lower_inode = ecryptfs_inode_to_lower(inode); | 
|  | 218 | lower_a_ops = lower_inode->i_mapping->a_ops; | 
|  | 219 | lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index, | 
|  | 220 | (filler_t *)lower_a_ops->readpage, | 
|  | 221 | (void *)lower_file); | 
|  | 222 | if (IS_ERR(lower_page)) { | 
|  | 223 | rc = PTR_ERR(lower_page); | 
|  | 224 | lower_page = NULL; | 
|  | 225 | ecryptfs_printk(KERN_ERR, "Error reading from page cache\n"); | 
|  | 226 | goto out; | 
|  | 227 | } | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 228 | page_data = kmap_atomic(page, KM_USER0); | 
|  | 229 | lower_page_data = kmap_atomic(lower_page, KM_USER1); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 230 | memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE); | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 231 | kunmap_atomic(lower_page_data, KM_USER1); | 
|  | 232 | kunmap_atomic(page_data, KM_USER0); | 
| Michael Halcrow | 0a9ac38 | 2007-02-12 00:53:50 -0800 | [diff] [blame] | 233 | flush_dcache_page(page); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 234 | rc = 0; | 
|  | 235 | out: | 
|  | 236 | if (likely(lower_page)) | 
|  | 237 | page_cache_release(lower_page); | 
|  | 238 | if (rc == 0) | 
|  | 239 | SetPageUptodate(page); | 
|  | 240 | else | 
|  | 241 | ClearPageUptodate(page); | 
|  | 242 | return rc; | 
|  | 243 | } | 
| Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 244 | /** | 
|  | 245 | *   Header Extent: | 
|  | 246 | *     Octets 0-7:        Unencrypted file size (big-endian) | 
|  | 247 | *     Octets 8-15:       eCryptfs special marker | 
|  | 248 | *     Octets 16-19:      Flags | 
|  | 249 | *      Octet 16:         File format version number (between 0 and 255) | 
|  | 250 | *      Octets 17-18:     Reserved | 
|  | 251 | *      Octet 19:         Bit 1 (lsb): Reserved | 
|  | 252 | *                        Bit 2: Encrypted? | 
|  | 253 | *                        Bits 3-8: Reserved | 
|  | 254 | *     Octets 20-23:      Header extent size (big-endian) | 
|  | 255 | *     Octets 24-25:      Number of header extents at front of file | 
|  | 256 | *                        (big-endian) | 
|  | 257 | *     Octet  26:         Begin RFC 2440 authentication token packet set | 
|  | 258 | */ | 
|  | 259 | static void set_header_info(char *page_virt, | 
|  | 260 | struct ecryptfs_crypt_stat *crypt_stat) | 
|  | 261 | { | 
|  | 262 | size_t written; | 
|  | 263 | int save_num_header_extents_at_front = | 
|  | 264 | crypt_stat->num_header_extents_at_front; | 
|  | 265 |  | 
|  | 266 | crypt_stat->num_header_extents_at_front = 1; | 
|  | 267 | ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written); | 
|  | 268 | crypt_stat->num_header_extents_at_front = | 
|  | 269 | save_num_header_extents_at_front; | 
|  | 270 | } | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 271 |  | 
|  | 272 | /** | 
|  | 273 | * ecryptfs_readpage | 
|  | 274 | * @file: This is an ecryptfs file | 
|  | 275 | * @page: ecryptfs associated page to stick the read data into | 
|  | 276 | * | 
|  | 277 | * Read in a page, decrypting if necessary. | 
|  | 278 | * | 
|  | 279 | * Returns zero on success; non-zero on error. | 
|  | 280 | */ | 
|  | 281 | static int ecryptfs_readpage(struct file *file, struct page *page) | 
|  | 282 | { | 
|  | 283 | int rc = 0; | 
|  | 284 | struct ecryptfs_crypt_stat *crypt_stat; | 
|  | 285 |  | 
| Josef "Jeff" Sipek | bd243a4 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 286 | BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode)); | 
|  | 287 | crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode) | 
|  | 288 | ->crypt_stat; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 289 | if (!crypt_stat | 
| Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 290 | || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED) | 
|  | 291 | || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 292 | ecryptfs_printk(KERN_DEBUG, | 
|  | 293 | "Passing through unencrypted page\n"); | 
|  | 294 | rc = ecryptfs_do_readpage(file, page, page->index); | 
|  | 295 | if (rc) { | 
|  | 296 | ecryptfs_printk(KERN_ERR, "Error reading page; rc = " | 
|  | 297 | "[%d]\n", rc); | 
|  | 298 | goto out; | 
|  | 299 | } | 
| Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 300 | } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { | 
|  | 301 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { | 
|  | 302 | int num_pages_in_header_region = | 
|  | 303 | (crypt_stat->header_extent_size | 
|  | 304 | / PAGE_CACHE_SIZE); | 
|  | 305 |  | 
|  | 306 | if (page->index < num_pages_in_header_region) { | 
|  | 307 | char *page_virt; | 
|  | 308 |  | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 309 | page_virt = kmap_atomic(page, KM_USER0); | 
| Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 310 | memset(page_virt, 0, PAGE_CACHE_SIZE); | 
|  | 311 | if (page->index == 0) { | 
|  | 312 | rc = ecryptfs_read_xattr_region( | 
|  | 313 | page_virt, file->f_path.dentry); | 
|  | 314 | set_header_info(page_virt, crypt_stat); | 
|  | 315 | } | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 316 | kunmap_atomic(page_virt, KM_USER0); | 
| Michael Halcrow | 0a9ac38 | 2007-02-12 00:53:50 -0800 | [diff] [blame] | 317 | flush_dcache_page(page); | 
| Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 318 | if (rc) { | 
|  | 319 | printk(KERN_ERR "Error reading xattr " | 
|  | 320 | "region\n"); | 
|  | 321 | goto out; | 
|  | 322 | } | 
|  | 323 | } else { | 
|  | 324 | rc = ecryptfs_do_readpage( | 
|  | 325 | file, page, | 
|  | 326 | (page->index | 
|  | 327 | - num_pages_in_header_region)); | 
|  | 328 | if (rc) { | 
|  | 329 | printk(KERN_ERR "Error reading page; " | 
|  | 330 | "rc = [%d]\n", rc); | 
|  | 331 | goto out; | 
|  | 332 | } | 
|  | 333 | } | 
|  | 334 | } else { | 
|  | 335 | rc = ecryptfs_do_readpage(file, page, page->index); | 
|  | 336 | if (rc) { | 
|  | 337 | printk(KERN_ERR "Error reading page; rc = " | 
|  | 338 | "[%d]\n", rc); | 
|  | 339 | goto out; | 
|  | 340 | } | 
|  | 341 | } | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 342 | } else { | 
|  | 343 | rc = ecryptfs_decrypt_page(file, page); | 
|  | 344 | if (rc) { | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 345 | ecryptfs_printk(KERN_ERR, "Error decrypting page; " | 
|  | 346 | "rc = [%d]\n", rc); | 
|  | 347 | goto out; | 
|  | 348 | } | 
|  | 349 | } | 
|  | 350 | SetPageUptodate(page); | 
|  | 351 | out: | 
|  | 352 | if (rc) | 
|  | 353 | ClearPageUptodate(page); | 
|  | 354 | ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", | 
|  | 355 | page->index); | 
|  | 356 | unlock_page(page); | 
|  | 357 | return rc; | 
|  | 358 | } | 
|  | 359 |  | 
| Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 360 | /** | 
|  | 361 | * Called with lower inode mutex held. | 
|  | 362 | */ | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 363 | static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) | 
|  | 364 | { | 
|  | 365 | struct inode *inode = page->mapping->host; | 
|  | 366 | int end_byte_in_page; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 367 |  | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 368 | if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index) | 
|  | 369 | goto out; | 
|  | 370 | end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; | 
|  | 371 | if (to > end_byte_in_page) | 
|  | 372 | end_byte_in_page = to; | 
| Nate Diller | c9f2875 | 2007-05-16 22:11:17 -0700 | [diff] [blame] | 373 | zero_user_page(page, end_byte_in_page, | 
|  | 374 | PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 375 | out: | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 376 | return 0; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 377 | } | 
|  | 378 |  | 
|  | 379 | static int ecryptfs_prepare_write(struct file *file, struct page *page, | 
|  | 380 | unsigned from, unsigned to) | 
|  | 381 | { | 
|  | 382 | int rc = 0; | 
|  | 383 |  | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 384 | if (from == 0 && to == PAGE_CACHE_SIZE) | 
|  | 385 | goto out;	/* If we are writing a full page, it will be | 
|  | 386 | up to date. */ | 
|  | 387 | if (!PageUptodate(page)) | 
|  | 388 | rc = ecryptfs_do_readpage(file, page, page->index); | 
|  | 389 | out: | 
|  | 390 | return rc; | 
|  | 391 | } | 
|  | 392 |  | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 393 | int ecryptfs_writepage_and_release_lower_page(struct page *lower_page, | 
|  | 394 | struct inode *lower_inode, | 
|  | 395 | struct writeback_control *wbc) | 
|  | 396 | { | 
|  | 397 | int rc = 0; | 
|  | 398 |  | 
|  | 399 | rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc); | 
|  | 400 | if (rc) { | 
|  | 401 | ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); " | 
|  | 402 | "rc = [%d]\n", rc); | 
|  | 403 | goto out; | 
|  | 404 | } | 
|  | 405 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; | 
|  | 406 | page_cache_release(lower_page); | 
|  | 407 | out: | 
|  | 408 | return rc; | 
|  | 409 | } | 
|  | 410 |  | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 411 | static | 
|  | 412 | void ecryptfs_release_lower_page(struct page *lower_page, int page_locked) | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 413 | { | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 414 | if (page_locked) | 
|  | 415 | unlock_page(lower_page); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 416 | page_cache_release(lower_page); | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | /** | 
|  | 420 | * ecryptfs_write_inode_size_to_header | 
|  | 421 | * | 
|  | 422 | * Writes the lower file size to the first 8 bytes of the header. | 
|  | 423 | * | 
|  | 424 | * Returns zero on success; non-zero on error. | 
|  | 425 | */ | 
| Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 426 | static int ecryptfs_write_inode_size_to_header(struct file *lower_file, | 
|  | 427 | struct inode *lower_inode, | 
|  | 428 | struct inode *inode) | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 429 | { | 
|  | 430 | int rc = 0; | 
|  | 431 | struct page *header_page; | 
|  | 432 | char *header_virt; | 
|  | 433 | const struct address_space_operations *lower_a_ops; | 
|  | 434 | u64 file_size; | 
|  | 435 |  | 
| Dmitriy Monakhov | a8fa74a | 2007-03-05 00:30:47 -0800 | [diff] [blame] | 436 | retry: | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 437 | header_page = grab_cache_page(lower_inode->i_mapping, 0); | 
|  | 438 | if (!header_page) { | 
|  | 439 | ecryptfs_printk(KERN_ERR, "grab_cache_page for " | 
|  | 440 | "lower_page_index 0 failed\n"); | 
|  | 441 | rc = -EINVAL; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 442 | goto out; | 
|  | 443 | } | 
|  | 444 | lower_a_ops = lower_inode->i_mapping->a_ops; | 
|  | 445 | rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8); | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 446 | if (rc) { | 
| Dmitriy Monakhov | a8fa74a | 2007-03-05 00:30:47 -0800 | [diff] [blame] | 447 | if (rc == AOP_TRUNCATED_PAGE) { | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 448 | ecryptfs_release_lower_page(header_page, 0); | 
| Dmitriy Monakhov | a8fa74a | 2007-03-05 00:30:47 -0800 | [diff] [blame] | 449 | goto retry; | 
|  | 450 | } else | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 451 | ecryptfs_release_lower_page(header_page, 1); | 
|  | 452 | goto out; | 
|  | 453 | } | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 454 | file_size = (u64)i_size_read(inode); | 
|  | 455 | ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size); | 
|  | 456 | file_size = cpu_to_be64(file_size); | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 457 | header_virt = kmap_atomic(header_page, KM_USER0); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 458 | memcpy(header_virt, &file_size, sizeof(u64)); | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 459 | kunmap_atomic(header_virt, KM_USER0); | 
| Michael Halcrow | 0a9ac38 | 2007-02-12 00:53:50 -0800 | [diff] [blame] | 460 | flush_dcache_page(header_page); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 461 | rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8); | 
|  | 462 | if (rc < 0) | 
|  | 463 | ecryptfs_printk(KERN_ERR, "Error commiting header page " | 
|  | 464 | "write\n"); | 
| Dmitriy Monakhov | a8fa74a | 2007-03-05 00:30:47 -0800 | [diff] [blame] | 465 | if (rc == AOP_TRUNCATED_PAGE) { | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 466 | ecryptfs_release_lower_page(header_page, 0); | 
| Dmitriy Monakhov | a8fa74a | 2007-03-05 00:30:47 -0800 | [diff] [blame] | 467 | goto retry; | 
|  | 468 | } else | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 469 | ecryptfs_release_lower_page(header_page, 1); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 470 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; | 
|  | 471 | mark_inode_dirty_sync(inode); | 
|  | 472 | out: | 
|  | 473 | return rc; | 
|  | 474 | } | 
|  | 475 |  | 
| Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 476 | static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode, | 
|  | 477 | struct inode *inode, | 
|  | 478 | struct dentry *ecryptfs_dentry, | 
|  | 479 | int lower_i_mutex_held) | 
|  | 480 | { | 
|  | 481 | ssize_t size; | 
|  | 482 | void *xattr_virt; | 
|  | 483 | struct dentry *lower_dentry; | 
|  | 484 | u64 file_size; | 
|  | 485 | int rc; | 
|  | 486 |  | 
|  | 487 | xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); | 
|  | 488 | if (!xattr_virt) { | 
|  | 489 | printk(KERN_ERR "Out of memory whilst attempting to write " | 
|  | 490 | "inode size to xattr\n"); | 
|  | 491 | rc = -ENOMEM; | 
|  | 492 | goto out; | 
|  | 493 | } | 
|  | 494 | lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); | 
| Dmitriy Monakhov | ad5f119 | 2007-03-05 00:30:12 -0800 | [diff] [blame] | 495 | if (!lower_dentry->d_inode->i_op->getxattr || | 
|  | 496 | !lower_dentry->d_inode->i_op->setxattr) { | 
| Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 497 | printk(KERN_WARNING | 
|  | 498 | "No support for setting xattr in lower filesystem\n"); | 
|  | 499 | rc = -ENOSYS; | 
|  | 500 | kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); | 
|  | 501 | goto out; | 
|  | 502 | } | 
|  | 503 | if (!lower_i_mutex_held) | 
|  | 504 | mutex_lock(&lower_dentry->d_inode->i_mutex); | 
|  | 505 | size = lower_dentry->d_inode->i_op->getxattr(lower_dentry, | 
|  | 506 | ECRYPTFS_XATTR_NAME, | 
|  | 507 | xattr_virt, | 
|  | 508 | PAGE_CACHE_SIZE); | 
|  | 509 | if (!lower_i_mutex_held) | 
|  | 510 | mutex_unlock(&lower_dentry->d_inode->i_mutex); | 
|  | 511 | if (size < 0) | 
|  | 512 | size = 8; | 
|  | 513 | file_size = (u64)i_size_read(inode); | 
|  | 514 | file_size = cpu_to_be64(file_size); | 
|  | 515 | memcpy(xattr_virt, &file_size, sizeof(u64)); | 
|  | 516 | if (!lower_i_mutex_held) | 
|  | 517 | mutex_lock(&lower_dentry->d_inode->i_mutex); | 
|  | 518 | rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, | 
|  | 519 | ECRYPTFS_XATTR_NAME, | 
|  | 520 | xattr_virt, size, 0); | 
|  | 521 | if (!lower_i_mutex_held) | 
|  | 522 | mutex_unlock(&lower_dentry->d_inode->i_mutex); | 
|  | 523 | if (rc) | 
|  | 524 | printk(KERN_ERR "Error whilst attempting to write inode size " | 
|  | 525 | "to lower file xattr; rc = [%d]\n", rc); | 
|  | 526 | kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); | 
|  | 527 | out: | 
|  | 528 | return rc; | 
|  | 529 | } | 
|  | 530 |  | 
|  | 531 | int | 
|  | 532 | ecryptfs_write_inode_size_to_metadata(struct file *lower_file, | 
|  | 533 | struct inode *lower_inode, | 
|  | 534 | struct inode *inode, | 
|  | 535 | struct dentry *ecryptfs_dentry, | 
|  | 536 | int lower_i_mutex_held) | 
|  | 537 | { | 
|  | 538 | struct ecryptfs_crypt_stat *crypt_stat; | 
|  | 539 |  | 
|  | 540 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; | 
|  | 541 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) | 
|  | 542 | return ecryptfs_write_inode_size_to_xattr(lower_inode, inode, | 
|  | 543 | ecryptfs_dentry, | 
|  | 544 | lower_i_mutex_held); | 
|  | 545 | else | 
|  | 546 | return ecryptfs_write_inode_size_to_header(lower_file, | 
|  | 547 | lower_inode, | 
|  | 548 | inode); | 
|  | 549 | } | 
|  | 550 |  | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 551 | int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode, | 
|  | 552 | struct file *lower_file, | 
|  | 553 | unsigned long lower_page_index, int byte_offset, | 
|  | 554 | int region_bytes) | 
|  | 555 | { | 
|  | 556 | int rc = 0; | 
|  | 557 |  | 
| Dmitriy Monakhov | a8fa74a | 2007-03-05 00:30:47 -0800 | [diff] [blame] | 558 | retry: | 
| Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 559 | *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index); | 
|  | 560 | if (!(*lower_page)) { | 
|  | 561 | rc = -EINVAL; | 
|  | 562 | ecryptfs_printk(KERN_ERR, "Error attempting to grab " | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 563 | "lower page with index [0x%.16x]\n", | 
|  | 564 | lower_page_index); | 
|  | 565 | goto out; | 
|  | 566 | } | 
|  | 567 | rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file, | 
|  | 568 | (*lower_page), | 
|  | 569 | byte_offset, | 
|  | 570 | region_bytes); | 
|  | 571 | if (rc) { | 
| Dmitriy Monakhov | a8fa74a | 2007-03-05 00:30:47 -0800 | [diff] [blame] | 572 | if (rc == AOP_TRUNCATED_PAGE) { | 
|  | 573 | ecryptfs_release_lower_page(*lower_page, 0); | 
|  | 574 | goto retry; | 
|  | 575 | } else { | 
|  | 576 | ecryptfs_printk(KERN_ERR, "prepare_write for " | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 577 | "lower_page_index = [0x%.16x] failed; rc = " | 
|  | 578 | "[%d]\n", lower_page_index, rc); | 
| Dmitriy Monakhov | a8fa74a | 2007-03-05 00:30:47 -0800 | [diff] [blame] | 579 | ecryptfs_release_lower_page(*lower_page, 1); | 
|  | 580 | (*lower_page) = NULL; | 
|  | 581 | } | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 582 | } | 
|  | 583 | out: | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 584 | return rc; | 
|  | 585 | } | 
|  | 586 |  | 
|  | 587 | /** | 
|  | 588 | * ecryptfs_commit_lower_page | 
|  | 589 | * | 
|  | 590 | * Returns zero on success; non-zero on error | 
|  | 591 | */ | 
|  | 592 | int | 
|  | 593 | ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode, | 
|  | 594 | struct file *lower_file, int byte_offset, | 
|  | 595 | int region_size) | 
|  | 596 | { | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 597 | int page_locked = 1; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 598 | int rc = 0; | 
|  | 599 |  | 
|  | 600 | rc = lower_inode->i_mapping->a_ops->commit_write( | 
|  | 601 | lower_file, lower_page, byte_offset, region_size); | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 602 | if (rc == AOP_TRUNCATED_PAGE) | 
|  | 603 | page_locked = 0; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 604 | if (rc < 0) { | 
|  | 605 | ecryptfs_printk(KERN_ERR, | 
|  | 606 | "Error committing write; rc = [%d]\n", rc); | 
|  | 607 | } else | 
|  | 608 | rc = 0; | 
| Michael Halcrow | ae73fc0 | 2007-02-28 20:12:16 -0800 | [diff] [blame] | 609 | ecryptfs_release_lower_page(lower_page, page_locked); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 610 | return rc; | 
|  | 611 | } | 
|  | 612 |  | 
|  | 613 | /** | 
|  | 614 | * ecryptfs_copy_page_to_lower | 
|  | 615 | * | 
|  | 616 | * Used for plaintext pass-through; no page index interpolation | 
|  | 617 | * required. | 
|  | 618 | */ | 
|  | 619 | int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode, | 
|  | 620 | struct file *lower_file) | 
|  | 621 | { | 
|  | 622 | int rc = 0; | 
|  | 623 | struct page *lower_page; | 
|  | 624 |  | 
|  | 625 | rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file, | 
|  | 626 | page->index, 0, PAGE_CACHE_SIZE); | 
|  | 627 | if (rc) { | 
|  | 628 | ecryptfs_printk(KERN_ERR, "Error attempting to get page " | 
|  | 629 | "at index [0x%.16x]\n", page->index); | 
|  | 630 | goto out; | 
|  | 631 | } | 
|  | 632 | /* TODO: aops */ | 
|  | 633 | memcpy((char *)page_address(lower_page), page_address(page), | 
|  | 634 | PAGE_CACHE_SIZE); | 
|  | 635 | rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file, | 
|  | 636 | 0, PAGE_CACHE_SIZE); | 
|  | 637 | if (rc) | 
|  | 638 | ecryptfs_printk(KERN_ERR, "Error attempting to commit page " | 
|  | 639 | "at index [0x%.16x]\n", page->index); | 
|  | 640 | out: | 
|  | 641 | return rc; | 
|  | 642 | } | 
|  | 643 |  | 
| Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 644 | struct kmem_cache *ecryptfs_xattr_cache; | 
|  | 645 |  | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 646 | /** | 
|  | 647 | * ecryptfs_commit_write | 
|  | 648 | * @file: The eCryptfs file object | 
|  | 649 | * @page: The eCryptfs page | 
|  | 650 | * @from: Ignored (we rotate the page IV on each write) | 
|  | 651 | * @to: Ignored | 
|  | 652 | * | 
|  | 653 | * This is where we encrypt the data and pass the encrypted data to | 
|  | 654 | * the lower filesystem.  In OpenPGP-compatible mode, we operate on | 
|  | 655 | * entire underlying packets. | 
|  | 656 | */ | 
|  | 657 | static int ecryptfs_commit_write(struct file *file, struct page *page, | 
|  | 658 | unsigned from, unsigned to) | 
|  | 659 | { | 
|  | 660 | struct ecryptfs_page_crypt_context ctx; | 
|  | 661 | loff_t pos; | 
|  | 662 | struct inode *inode; | 
|  | 663 | struct inode *lower_inode; | 
|  | 664 | struct file *lower_file; | 
|  | 665 | struct ecryptfs_crypt_stat *crypt_stat; | 
|  | 666 | int rc; | 
|  | 667 |  | 
|  | 668 | inode = page->mapping->host; | 
|  | 669 | lower_inode = ecryptfs_inode_to_lower(inode); | 
|  | 670 | lower_file = ecryptfs_file_to_lower(file); | 
|  | 671 | mutex_lock(&lower_inode->i_mutex); | 
| Josef "Jeff" Sipek | bd243a4 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 672 | crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode) | 
|  | 673 | ->crypt_stat; | 
| Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 674 | if (crypt_stat->flags & ECRYPTFS_NEW_FILE) { | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 675 | ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " | 
|  | 676 | "crypt_stat at memory location [%p]\n", crypt_stat); | 
| Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 677 | crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 678 | } else | 
|  | 679 | ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); | 
|  | 680 | ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" | 
|  | 681 | "(page w/ index = [0x%.16x], to = [%d])\n", page->index, | 
|  | 682 | to); | 
|  | 683 | rc = fill_zeros_to_end_of_page(page, to); | 
|  | 684 | if (rc) { | 
|  | 685 | ecryptfs_printk(KERN_WARNING, "Error attempting to fill " | 
|  | 686 | "zeros in page with index = [0x%.16x]\n", | 
|  | 687 | page->index); | 
|  | 688 | goto out; | 
|  | 689 | } | 
|  | 690 | ctx.page = page; | 
|  | 691 | ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE; | 
|  | 692 | ctx.param.lower_file = lower_file; | 
|  | 693 | rc = ecryptfs_encrypt_page(&ctx); | 
|  | 694 | if (rc) { | 
|  | 695 | ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " | 
|  | 696 | "index [0x%.16x])\n", page->index); | 
|  | 697 | goto out; | 
|  | 698 | } | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 699 | inode->i_blocks = lower_inode->i_blocks; | 
|  | 700 | pos = (page->index << PAGE_CACHE_SHIFT) + to; | 
|  | 701 | if (pos > i_size_read(inode)) { | 
|  | 702 | i_size_write(inode, pos); | 
|  | 703 | ecryptfs_printk(KERN_DEBUG, "Expanded file size to " | 
|  | 704 | "[0x%.16x]\n", i_size_read(inode)); | 
|  | 705 | } | 
| Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 706 | rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode, | 
|  | 707 | inode, file->f_dentry, | 
|  | 708 | ECRYPTFS_LOWER_I_MUTEX_HELD); | 
|  | 709 | if (rc) | 
|  | 710 | printk(KERN_ERR "Error writing inode size to metadata; " | 
|  | 711 | "rc = [%d]\n", rc); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 712 | lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; | 
|  | 713 | mark_inode_dirty_sync(inode); | 
|  | 714 | out: | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 715 | if (rc < 0) | 
|  | 716 | ClearPageUptodate(page); | 
|  | 717 | else | 
|  | 718 | SetPageUptodate(page); | 
|  | 719 | mutex_unlock(&lower_inode->i_mutex); | 
|  | 720 | return rc; | 
|  | 721 | } | 
|  | 722 |  | 
|  | 723 | /** | 
|  | 724 | * write_zeros | 
|  | 725 | * @file: The ecryptfs file | 
|  | 726 | * @index: The index in which we are writing | 
|  | 727 | * @start: The position after the last block of data | 
|  | 728 | * @num_zeros: The number of zeros to write | 
|  | 729 | * | 
|  | 730 | * Write a specified number of zero's to a page. | 
|  | 731 | * | 
|  | 732 | * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE | 
|  | 733 | */ | 
|  | 734 | static | 
|  | 735 | int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros) | 
|  | 736 | { | 
|  | 737 | int rc = 0; | 
|  | 738 | struct page *tmp_page; | 
|  | 739 |  | 
|  | 740 | tmp_page = ecryptfs_get1page(file, index); | 
|  | 741 | if (IS_ERR(tmp_page)) { | 
|  | 742 | ecryptfs_printk(KERN_ERR, "Error getting page at index " | 
|  | 743 | "[0x%.16x]\n", index); | 
|  | 744 | rc = PTR_ERR(tmp_page); | 
|  | 745 | goto out; | 
|  | 746 | } | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 747 | rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros); | 
|  | 748 | if (rc) { | 
|  | 749 | ecryptfs_printk(KERN_ERR, "Error preparing to write zero's " | 
|  | 750 | "to remainder of page at index [0x%.16x]\n", | 
|  | 751 | index); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 752 | page_cache_release(tmp_page); | 
|  | 753 | goto out; | 
|  | 754 | } | 
| Nate Diller | c9f2875 | 2007-05-16 22:11:17 -0700 | [diff] [blame] | 755 | zero_user_page(tmp_page, start, num_zeros, KM_USER0); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 756 | rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros); | 
|  | 757 | if (rc < 0) { | 
|  | 758 | ecryptfs_printk(KERN_ERR, "Error attempting to write zero's " | 
|  | 759 | "to remainder of page at index [0x%.16x]\n", | 
|  | 760 | index); | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 761 | page_cache_release(tmp_page); | 
|  | 762 | goto out; | 
|  | 763 | } | 
|  | 764 | rc = 0; | 
| Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 765 | page_cache_release(tmp_page); | 
|  | 766 | out: | 
|  | 767 | return rc; | 
|  | 768 | } | 
|  | 769 |  | 
|  | 770 | static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) | 
|  | 771 | { | 
|  | 772 | int rc = 0; | 
|  | 773 | struct inode *inode; | 
|  | 774 | struct inode *lower_inode; | 
|  | 775 |  | 
|  | 776 | inode = (struct inode *)mapping->host; | 
|  | 777 | lower_inode = ecryptfs_inode_to_lower(inode); | 
|  | 778 | if (lower_inode->i_mapping->a_ops->bmap) | 
|  | 779 | rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, | 
|  | 780 | block); | 
|  | 781 | return rc; | 
|  | 782 | } | 
|  | 783 |  | 
|  | 784 | static void ecryptfs_sync_page(struct page *page) | 
|  | 785 | { | 
|  | 786 | struct inode *inode; | 
|  | 787 | struct inode *lower_inode; | 
|  | 788 | struct page *lower_page; | 
|  | 789 |  | 
|  | 790 | inode = page->mapping->host; | 
|  | 791 | lower_inode = ecryptfs_inode_to_lower(inode); | 
|  | 792 | /* NOTE: Recently swapped with grab_cache_page(), since | 
|  | 793 | * sync_page() just makes sure that pending I/O gets done. */ | 
|  | 794 | lower_page = find_lock_page(lower_inode->i_mapping, page->index); | 
|  | 795 | if (!lower_page) { | 
|  | 796 | ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n"); | 
|  | 797 | return; | 
|  | 798 | } | 
|  | 799 | lower_page->mapping->a_ops->sync_page(lower_page); | 
|  | 800 | ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", | 
|  | 801 | lower_page->index); | 
|  | 802 | unlock_page(lower_page); | 
|  | 803 | page_cache_release(lower_page); | 
|  | 804 | } | 
|  | 805 |  | 
|  | 806 | struct address_space_operations ecryptfs_aops = { | 
|  | 807 | .writepage = ecryptfs_writepage, | 
|  | 808 | .readpage = ecryptfs_readpage, | 
|  | 809 | .prepare_write = ecryptfs_prepare_write, | 
|  | 810 | .commit_write = ecryptfs_commit_write, | 
|  | 811 | .bmap = ecryptfs_bmap, | 
|  | 812 | .sync_page = ecryptfs_sync_page, | 
|  | 813 | }; |