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 | /** |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 40 | * ecryptfs_get_locked_page |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 41 | * |
| 42 | * Get one page from cache or lower f/s, return error otherwise. |
| 43 | * |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 44 | * Returns locked and up-to-date page (if ok), with increased |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 45 | * refcnt. |
| 46 | */ |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 47 | struct page *ecryptfs_get_locked_page(struct file *file, loff_t index) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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; |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 52 | struct page *page; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 53 | |
Josef "Jeff" Sipek | bd243a4 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 54 | dentry = file->f_path.dentry; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 55 | inode = dentry->d_inode; |
| 56 | mapping = inode->i_mapping; |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 57 | page = read_mapping_page(mapping, index, (void *)file); |
| 58 | if (!IS_ERR(page)) |
| 59 | lock_page(page); |
| 60 | return page; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 63 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 64 | * ecryptfs_writepage |
| 65 | * @page: Page that is locked before this call is made |
| 66 | * |
| 67 | * Returns zero on success; non-zero otherwise |
| 68 | */ |
| 69 | static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) |
| 70 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 71 | int rc; |
| 72 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 73 | rc = ecryptfs_encrypt_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 74 | if (rc) { |
| 75 | ecryptfs_printk(KERN_WARNING, "Error encrypting " |
| 76 | "page (upper index [0x%.16x])\n", page->index); |
| 77 | ClearPageUptodate(page); |
| 78 | goto out; |
| 79 | } |
| 80 | SetPageUptodate(page); |
| 81 | unlock_page(page); |
| 82 | out: |
| 83 | return rc; |
| 84 | } |
| 85 | |
| 86 | /** |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 87 | * Header Extent: |
| 88 | * Octets 0-7: Unencrypted file size (big-endian) |
| 89 | * Octets 8-15: eCryptfs special marker |
| 90 | * Octets 16-19: Flags |
| 91 | * Octet 16: File format version number (between 0 and 255) |
| 92 | * Octets 17-18: Reserved |
| 93 | * Octet 19: Bit 1 (lsb): Reserved |
| 94 | * Bit 2: Encrypted? |
| 95 | * Bits 3-8: Reserved |
| 96 | * Octets 20-23: Header extent size (big-endian) |
| 97 | * Octets 24-25: Number of header extents at front of file |
| 98 | * (big-endian) |
| 99 | * Octet 26: Begin RFC 2440 authentication token packet set |
| 100 | */ |
| 101 | static void set_header_info(char *page_virt, |
| 102 | struct ecryptfs_crypt_stat *crypt_stat) |
| 103 | { |
| 104 | size_t written; |
| 105 | int save_num_header_extents_at_front = |
| 106 | crypt_stat->num_header_extents_at_front; |
| 107 | |
| 108 | crypt_stat->num_header_extents_at_front = 1; |
| 109 | ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written); |
| 110 | crypt_stat->num_header_extents_at_front = |
| 111 | save_num_header_extents_at_front; |
| 112 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 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 | */ |
| 124 | static int |
| 125 | ecryptfs_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) { |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 134 | loff_t view_extent_num = ((((loff_t)page->index) |
| 135 | * num_extents_per_page) |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 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) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 153 | printk(KERN_ERR "%s: Error reading xattr " |
| 154 | "region; rc = [%d]\n", __FUNCTION__, rc); |
| 155 | goto out; |
| 156 | } |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 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 | } |
| 178 | out: |
| 179 | return rc; |
| 180 | } |
| 181 | |
| 182 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 183 | * ecryptfs_readpage |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 184 | * @file: An eCryptfs file |
| 185 | * @page: Page from eCryptfs inode mapping into which to stick the read data |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 186 | * |
| 187 | * Read in a page, decrypting if necessary. |
| 188 | * |
| 189 | * Returns zero on success; non-zero on error. |
| 190 | */ |
| 191 | static int ecryptfs_readpage(struct file *file, struct page *page) |
| 192 | { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 193 | struct ecryptfs_crypt_stat *crypt_stat = |
| 194 | &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 195 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 196 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 197 | if (!crypt_stat |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 198 | || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED) |
| 199 | || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 200 | ecryptfs_printk(KERN_DEBUG, |
| 201 | "Passing through unencrypted page\n"); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 202 | rc = ecryptfs_read_lower_page_segment(page, page->index, 0, |
| 203 | PAGE_CACHE_SIZE, |
| 204 | page->mapping->host); |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 205 | } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { |
| 206 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 207 | rc = ecryptfs_copy_up_encrypted_with_header(page, |
| 208 | crypt_stat); |
| 209 | if (rc) { |
| 210 | printk(KERN_ERR "%s: Error attempting to copy " |
| 211 | "the encrypted content from the lower " |
| 212 | "file whilst inserting the metadata " |
| 213 | "from the xattr into the header; rc = " |
| 214 | "[%d]\n", __FUNCTION__, rc); |
| 215 | goto out; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 216 | } |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 217 | |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 218 | } else { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 219 | rc = ecryptfs_read_lower_page_segment( |
| 220 | page, page->index, 0, PAGE_CACHE_SIZE, |
| 221 | page->mapping->host); |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 222 | if (rc) { |
| 223 | printk(KERN_ERR "Error reading page; rc = " |
| 224 | "[%d]\n", rc); |
| 225 | goto out; |
| 226 | } |
| 227 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 228 | } else { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 229 | rc = ecryptfs_decrypt_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 230 | if (rc) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 231 | ecryptfs_printk(KERN_ERR, "Error decrypting page; " |
| 232 | "rc = [%d]\n", rc); |
| 233 | goto out; |
| 234 | } |
| 235 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 236 | out: |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 237 | if (rc) |
| 238 | ClearPageUptodate(page); |
| 239 | else |
| 240 | SetPageUptodate(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 241 | ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", |
| 242 | page->index); |
| 243 | unlock_page(page); |
| 244 | return rc; |
| 245 | } |
| 246 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 247 | /** |
| 248 | * Called with lower inode mutex held. |
| 249 | */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 250 | static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) |
| 251 | { |
| 252 | struct inode *inode = page->mapping->host; |
| 253 | int end_byte_in_page; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 254 | |
Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 255 | if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index) |
| 256 | goto out; |
| 257 | end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; |
| 258 | if (to > end_byte_in_page) |
| 259 | end_byte_in_page = to; |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 260 | zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 261 | out: |
Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 262 | return 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 265 | /* This function must zero any hole we create */ |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 266 | static int ecryptfs_prepare_write(struct file *file, struct page *page, |
| 267 | unsigned from, unsigned to) |
| 268 | { |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 269 | int rc = 0; |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 270 | loff_t prev_page_end_size; |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 271 | |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 272 | if (!PageUptodate(page)) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 273 | rc = ecryptfs_read_lower_page_segment(page, page->index, 0, |
| 274 | PAGE_CACHE_SIZE, |
| 275 | page->mapping->host); |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 276 | if (rc) { |
| 277 | printk(KERN_ERR "%s: Error attemping to read lower " |
| 278 | "page segment; rc = [%d]\n", __FUNCTION__, rc); |
| 279 | ClearPageUptodate(page); |
| 280 | goto out; |
| 281 | } else |
| 282 | SetPageUptodate(page); |
| 283 | } |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 284 | |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 285 | prev_page_end_size = ((loff_t)page->index << PAGE_CACHE_SHIFT); |
| 286 | |
| 287 | /* |
| 288 | * If creating a page or more of holes, zero them out via truncate. |
| 289 | * Note, this will increase i_size. |
| 290 | */ |
| 291 | if (page->index != 0) { |
| 292 | if (prev_page_end_size > i_size_read(page->mapping->host)) { |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 293 | rc = ecryptfs_truncate(file->f_path.dentry, |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 294 | prev_page_end_size); |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 295 | if (rc) { |
| 296 | printk(KERN_ERR "Error on attempt to " |
| 297 | "truncate to (higher) offset [%lld];" |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 298 | " rc = [%d]\n", prev_page_end_size, rc); |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 299 | goto out; |
| 300 | } |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 301 | } |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 302 | } |
| 303 | /* |
| 304 | * Writing to a new page, and creating a small hole from start of page? |
| 305 | * Zero it out. |
| 306 | */ |
| 307 | if ((i_size_read(page->mapping->host) == prev_page_end_size) && |
| 308 | (from != 0)) { |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 309 | zero_user(page, 0, PAGE_CACHE_SIZE); |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 310 | } |
| 311 | out: |
| 312 | return rc; |
| 313 | } |
| 314 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 315 | /** |
| 316 | * ecryptfs_write_inode_size_to_header |
| 317 | * |
| 318 | * Writes the lower file size to the first 8 bytes of the header. |
| 319 | * |
| 320 | * Returns zero on success; non-zero on error. |
| 321 | */ |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 322 | static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 323 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 324 | u64 file_size; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 325 | char *file_size_virt; |
| 326 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 327 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 328 | file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL); |
| 329 | if (!file_size_virt) { |
| 330 | rc = -ENOMEM; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 331 | goto out; |
| 332 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 333 | file_size = (u64)i_size_read(ecryptfs_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 334 | file_size = cpu_to_be64(file_size); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 335 | memcpy(file_size_virt, &file_size, sizeof(u64)); |
| 336 | rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0, |
| 337 | sizeof(u64)); |
| 338 | kfree(file_size_virt); |
| 339 | if (rc) |
| 340 | printk(KERN_ERR "%s: Error writing file size to header; " |
| 341 | "rc = [%d]\n", __FUNCTION__, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 342 | out: |
| 343 | return rc; |
| 344 | } |
| 345 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 346 | struct kmem_cache *ecryptfs_xattr_cache; |
| 347 | |
| 348 | static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 349 | { |
| 350 | ssize_t size; |
| 351 | void *xattr_virt; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 352 | struct dentry *lower_dentry = |
| 353 | ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry; |
| 354 | struct inode *lower_inode = lower_dentry->d_inode; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 355 | u64 file_size; |
| 356 | int rc; |
| 357 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 358 | if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) { |
| 359 | printk(KERN_WARNING |
| 360 | "No support for setting xattr in lower filesystem\n"); |
| 361 | rc = -ENOSYS; |
| 362 | goto out; |
| 363 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 364 | xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); |
| 365 | if (!xattr_virt) { |
| 366 | printk(KERN_ERR "Out of memory whilst attempting to write " |
| 367 | "inode size to xattr\n"); |
| 368 | rc = -ENOMEM; |
| 369 | goto out; |
| 370 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 371 | mutex_lock(&lower_inode->i_mutex); |
| 372 | size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME, |
| 373 | xattr_virt, PAGE_CACHE_SIZE); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 374 | if (size < 0) |
| 375 | size = 8; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 376 | file_size = (u64)i_size_read(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 377 | file_size = cpu_to_be64(file_size); |
| 378 | memcpy(xattr_virt, &file_size, sizeof(u64)); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 379 | rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME, |
| 380 | xattr_virt, size, 0); |
| 381 | mutex_unlock(&lower_inode->i_mutex); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 382 | if (rc) |
| 383 | printk(KERN_ERR "Error whilst attempting to write inode size " |
| 384 | "to lower file xattr; rc = [%d]\n", rc); |
| 385 | kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); |
| 386 | out: |
| 387 | return rc; |
| 388 | } |
| 389 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 390 | int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 391 | { |
| 392 | struct ecryptfs_crypt_stat *crypt_stat; |
| 393 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 394 | crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 395 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 396 | return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 397 | else |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 398 | return ecryptfs_write_inode_size_to_header(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 401 | /** |
| 402 | * ecryptfs_commit_write |
| 403 | * @file: The eCryptfs file object |
| 404 | * @page: The eCryptfs page |
| 405 | * @from: Ignored (we rotate the page IV on each write) |
| 406 | * @to: Ignored |
| 407 | * |
| 408 | * This is where we encrypt the data and pass the encrypted data to |
| 409 | * the lower filesystem. In OpenPGP-compatible mode, we operate on |
| 410 | * entire underlying packets. |
| 411 | */ |
| 412 | static int ecryptfs_commit_write(struct file *file, struct page *page, |
| 413 | unsigned from, unsigned to) |
| 414 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 415 | loff_t pos; |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 416 | struct inode *ecryptfs_inode = page->mapping->host; |
| 417 | struct ecryptfs_crypt_stat *crypt_stat = |
| 418 | &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 419 | int rc; |
| 420 | |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 421 | if (crypt_stat->flags & ECRYPTFS_NEW_FILE) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 422 | ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " |
| 423 | "crypt_stat at memory location [%p]\n", crypt_stat); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 424 | crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 425 | } else |
| 426 | ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); |
| 427 | ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" |
| 428 | "(page w/ index = [0x%.16x], to = [%d])\n", page->index, |
| 429 | to); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 430 | /* Fills in zeros if 'to' goes beyond inode size */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 431 | rc = fill_zeros_to_end_of_page(page, to); |
| 432 | if (rc) { |
| 433 | ecryptfs_printk(KERN_WARNING, "Error attempting to fill " |
| 434 | "zeros in page with index = [0x%.16x]\n", |
| 435 | page->index); |
| 436 | goto out; |
| 437 | } |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 438 | rc = ecryptfs_encrypt_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 439 | if (rc) { |
| 440 | ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " |
| 441 | "index [0x%.16x])\n", page->index); |
| 442 | goto out; |
| 443 | } |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 444 | pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to; |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 445 | if (pos > i_size_read(ecryptfs_inode)) { |
| 446 | i_size_write(ecryptfs_inode, pos); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 447 | ecryptfs_printk(KERN_DEBUG, "Expanded file size to " |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 448 | "[0x%.16x]\n", i_size_read(ecryptfs_inode)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 449 | } |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 450 | rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 451 | if (rc) |
| 452 | printk(KERN_ERR "Error writing inode size to metadata; " |
| 453 | "rc = [%d]\n", rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 454 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 455 | return rc; |
| 456 | } |
| 457 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 458 | static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) |
| 459 | { |
| 460 | int rc = 0; |
| 461 | struct inode *inode; |
| 462 | struct inode *lower_inode; |
| 463 | |
| 464 | inode = (struct inode *)mapping->host; |
| 465 | lower_inode = ecryptfs_inode_to_lower(inode); |
| 466 | if (lower_inode->i_mapping->a_ops->bmap) |
| 467 | rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, |
| 468 | block); |
| 469 | return rc; |
| 470 | } |
| 471 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 472 | struct address_space_operations ecryptfs_aops = { |
| 473 | .writepage = ecryptfs_writepage, |
| 474 | .readpage = ecryptfs_readpage, |
| 475 | .prepare_write = ecryptfs_prepare_write, |
| 476 | .commit_write = ecryptfs_commit_write, |
| 477 | .bmap = ecryptfs_bmap, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 478 | }; |