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> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 33 | #include <linux/scatterlist.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Harvey Harrison | 0a688ad | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 35 | #include <asm/unaligned.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 36 | #include "ecryptfs_kernel.h" |
| 37 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 38 | /** |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 39 | * ecryptfs_get_locked_page |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 40 | * |
| 41 | * Get one page from cache or lower f/s, return error otherwise. |
| 42 | * |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 43 | * Returns locked and up-to-date page (if ok), with increased |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 44 | * refcnt. |
| 45 | */ |
Al Viro | 02bd979 | 2010-05-21 11:02:14 -0400 | [diff] [blame] | 46 | struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 47 | { |
Al Viro | 02bd979 | 2010-05-21 11:02:14 -0400 | [diff] [blame] | 48 | struct page *page = read_mapping_page(inode->i_mapping, index, NULL); |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 49 | if (!IS_ERR(page)) |
| 50 | lock_page(page); |
| 51 | return page; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 54 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 55 | * ecryptfs_writepage |
| 56 | * @page: Page that is locked before this call is made |
| 57 | * |
| 58 | * Returns zero on success; non-zero otherwise |
Li Wang | 1589cb1 | 2012-01-25 15:40:31 +0800 | [diff] [blame] | 59 | * |
| 60 | * This is where we encrypt the data and pass the encrypted data to |
| 61 | * the lower filesystem. In OpenPGP-compatible mode, we operate on |
| 62 | * entire underlying packets. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 63 | */ |
| 64 | static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) |
| 65 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 66 | int rc; |
| 67 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 68 | rc = ecryptfs_encrypt_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 69 | if (rc) { |
| 70 | ecryptfs_printk(KERN_WARNING, "Error encrypting " |
Joe Perches | 888d57b | 2010-11-10 15:46:16 -0800 | [diff] [blame] | 71 | "page (upper index [0x%.16lx])\n", page->index); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 72 | ClearPageUptodate(page); |
| 73 | goto out; |
| 74 | } |
| 75 | SetPageUptodate(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 76 | out: |
Thieu Le | 57db4e8 | 2011-03-08 16:26:03 -0800 | [diff] [blame] | 77 | unlock_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 78 | return rc; |
| 79 | } |
| 80 | |
Tyler Hicks | f4e60e6 | 2010-02-11 00:02:32 -0600 | [diff] [blame] | 81 | static void strip_xattr_flag(char *page_virt, |
| 82 | struct ecryptfs_crypt_stat *crypt_stat) |
| 83 | { |
| 84 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { |
| 85 | size_t written; |
| 86 | |
| 87 | crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR; |
| 88 | ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat, |
| 89 | &written); |
| 90 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; |
| 91 | } |
| 92 | } |
| 93 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 94 | /** |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 95 | * Header Extent: |
| 96 | * Octets 0-7: Unencrypted file size (big-endian) |
| 97 | * Octets 8-15: eCryptfs special marker |
| 98 | * Octets 16-19: Flags |
| 99 | * Octet 16: File format version number (between 0 and 255) |
| 100 | * Octets 17-18: Reserved |
| 101 | * Octet 19: Bit 1 (lsb): Reserved |
| 102 | * Bit 2: Encrypted? |
| 103 | * Bits 3-8: Reserved |
| 104 | * Octets 20-23: Header extent size (big-endian) |
| 105 | * Octets 24-25: Number of header extents at front of file |
| 106 | * (big-endian) |
| 107 | * Octet 26: Begin RFC 2440 authentication token packet set |
| 108 | */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 111 | * ecryptfs_copy_up_encrypted_with_header |
| 112 | * @page: Sort of a ``virtual'' representation of the encrypted lower |
| 113 | * file. The actual lower file does not have the metadata in |
| 114 | * the header. This is locked. |
| 115 | * @crypt_stat: The eCryptfs inode's cryptographic context |
| 116 | * |
| 117 | * The ``view'' is the version of the file that userspace winds up |
| 118 | * seeing, with the header information inserted. |
| 119 | */ |
| 120 | static int |
| 121 | ecryptfs_copy_up_encrypted_with_header(struct page *page, |
| 122 | struct ecryptfs_crypt_stat *crypt_stat) |
| 123 | { |
| 124 | loff_t extent_num_in_page = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 125 | loff_t num_extents_per_page = (PAGE_SIZE |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 126 | / crypt_stat->extent_size); |
| 127 | int rc = 0; |
| 128 | |
| 129 | while (extent_num_in_page < num_extents_per_page) { |
Michael Halcrow | d6a13c1 | 2007-10-16 01:28:12 -0700 | [diff] [blame] | 130 | loff_t view_extent_num = ((((loff_t)page->index) |
| 131 | * num_extents_per_page) |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 132 | + extent_num_in_page); |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 133 | size_t num_header_extents_at_front = |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 134 | (crypt_stat->metadata_size / crypt_stat->extent_size); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 135 | |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 136 | if (view_extent_num < num_header_extents_at_front) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 137 | /* This is a header extent */ |
| 138 | char *page_virt; |
| 139 | |
Cong Wang | 465c934 | 2012-02-10 13:39:50 +0800 | [diff] [blame] | 140 | page_virt = kmap_atomic(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 141 | memset(page_virt, 0, PAGE_SIZE); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 142 | /* TODO: Support more than one header extent */ |
| 143 | if (view_extent_num == 0) { |
Tyler Hicks | 157f107 | 2010-02-11 07:10:38 -0600 | [diff] [blame] | 144 | size_t written; |
| 145 | |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 146 | rc = ecryptfs_read_xattr_region( |
| 147 | page_virt, page->mapping->host); |
Tyler Hicks | f4e60e6 | 2010-02-11 00:02:32 -0600 | [diff] [blame] | 148 | strip_xattr_flag(page_virt + 16, crypt_stat); |
Tyler Hicks | 157f107 | 2010-02-11 07:10:38 -0600 | [diff] [blame] | 149 | ecryptfs_write_header_metadata(page_virt + 20, |
| 150 | crypt_stat, |
| 151 | &written); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 152 | } |
Cong Wang | 465c934 | 2012-02-10 13:39:50 +0800 | [diff] [blame] | 153 | kunmap_atomic(page_virt); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 154 | flush_dcache_page(page); |
| 155 | if (rc) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 156 | printk(KERN_ERR "%s: Error reading xattr " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 157 | "region; rc = [%d]\n", __func__, rc); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 158 | goto out; |
| 159 | } |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 160 | } else { |
| 161 | /* This is an encrypted data extent */ |
| 162 | loff_t lower_offset = |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 163 | ((view_extent_num * crypt_stat->extent_size) |
Tyler Hicks | fa3ef1c | 2010-02-11 05:09:14 -0600 | [diff] [blame] | 164 | - crypt_stat->metadata_size); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 165 | |
| 166 | rc = ecryptfs_read_lower_page_segment( |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 167 | page, (lower_offset >> PAGE_SHIFT), |
| 168 | (lower_offset & ~PAGE_MASK), |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 169 | crypt_stat->extent_size, page->mapping->host); |
| 170 | if (rc) { |
| 171 | printk(KERN_ERR "%s: Error attempting to read " |
| 172 | "extent at offset [%lld] in the lower " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 173 | "file; rc = [%d]\n", __func__, |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 174 | lower_offset, rc); |
| 175 | goto out; |
| 176 | } |
| 177 | } |
| 178 | extent_num_in_page++; |
| 179 | } |
| 180 | out: |
| 181 | return rc; |
| 182 | } |
| 183 | |
| 184 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 185 | * ecryptfs_readpage |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 186 | * @file: An eCryptfs file |
| 187 | * @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] | 188 | * |
| 189 | * Read in a page, decrypting if necessary. |
| 190 | * |
| 191 | * Returns zero on success; non-zero on error. |
| 192 | */ |
| 193 | static int ecryptfs_readpage(struct file *file, struct page *page) |
| 194 | { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 195 | struct ecryptfs_crypt_stat *crypt_stat = |
Al Viro | bef5bc2 | 2010-05-21 10:56:12 -0400 | [diff] [blame] | 196 | &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 197 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 198 | |
Tyler Hicks | fed8859 | 2011-02-23 00:54:20 -0600 | [diff] [blame] | 199 | if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 200 | rc = ecryptfs_read_lower_page_segment(page, page->index, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 201 | PAGE_SIZE, |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 202 | page->mapping->host); |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 203 | } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { |
| 204 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 205 | rc = ecryptfs_copy_up_encrypted_with_header(page, |
| 206 | crypt_stat); |
| 207 | if (rc) { |
| 208 | printk(KERN_ERR "%s: Error attempting to copy " |
| 209 | "the encrypted content from the lower " |
| 210 | "file whilst inserting the metadata " |
| 211 | "from the xattr into the header; rc = " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 212 | "[%d]\n", __func__, rc); |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 213 | goto out; |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 214 | } |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 215 | |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 216 | } else { |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 217 | rc = ecryptfs_read_lower_page_segment( |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 218 | page, page->index, 0, PAGE_SIZE, |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 219 | page->mapping->host); |
Michael Halcrow | e77a56d | 2007-02-12 00:53:47 -0800 | [diff] [blame] | 220 | if (rc) { |
| 221 | printk(KERN_ERR "Error reading page; rc = " |
| 222 | "[%d]\n", rc); |
| 223 | goto out; |
| 224 | } |
| 225 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 226 | } else { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 227 | rc = ecryptfs_decrypt_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 228 | if (rc) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 229 | ecryptfs_printk(KERN_ERR, "Error decrypting page; " |
| 230 | "rc = [%d]\n", rc); |
| 231 | goto out; |
| 232 | } |
| 233 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 234 | out: |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 235 | if (rc) |
| 236 | ClearPageUptodate(page); |
| 237 | else |
| 238 | SetPageUptodate(page); |
Joe Perches | 888d57b | 2010-11-10 15:46:16 -0800 | [diff] [blame] | 239 | ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n", |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 240 | page->index); |
| 241 | unlock_page(page); |
| 242 | return rc; |
| 243 | } |
| 244 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 245 | /** |
| 246 | * Called with lower inode mutex held. |
| 247 | */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 248 | static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) |
| 249 | { |
| 250 | struct inode *inode = page->mapping->host; |
| 251 | int end_byte_in_page; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 252 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 253 | if ((i_size_read(inode) / PAGE_SIZE) != page->index) |
Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 254 | goto out; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 255 | end_byte_in_page = i_size_read(inode) % PAGE_SIZE; |
Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 256 | if (to > end_byte_in_page) |
| 257 | end_byte_in_page = to; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 258 | zero_user_segment(page, end_byte_in_page, PAGE_SIZE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 259 | out: |
Michael Halcrow | 9d8b8ce | 2007-02-12 00:53:48 -0800 | [diff] [blame] | 260 | return 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 263 | /** |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 264 | * ecryptfs_write_begin |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 265 | * @file: The eCryptfs file |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 266 | * @mapping: The eCryptfs object |
| 267 | * @pos: The file offset at which to start writing |
| 268 | * @len: Length of the write |
| 269 | * @flags: Various flags |
| 270 | * @pagep: Pointer to return the page |
| 271 | * @fsdata: Pointer to return fs data (unused) |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 272 | * |
| 273 | * This function must zero any hole we create |
| 274 | * |
| 275 | * Returns zero on success; non-zero otherwise |
| 276 | */ |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 277 | static int ecryptfs_write_begin(struct file *file, |
| 278 | struct address_space *mapping, |
| 279 | loff_t pos, unsigned len, unsigned flags, |
| 280 | struct page **pagep, void **fsdata) |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 281 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 282 | pgoff_t index = pos >> PAGE_SHIFT; |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 283 | struct page *page; |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 284 | loff_t prev_page_end_size; |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 285 | int rc = 0; |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 286 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 287 | page = grab_cache_page_write_begin(mapping, index, flags); |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 288 | if (!page) |
| 289 | return -ENOMEM; |
| 290 | *pagep = page; |
| 291 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 292 | prev_page_end_size = ((loff_t)index << PAGE_SHIFT); |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 293 | if (!PageUptodate(page)) { |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 294 | struct ecryptfs_crypt_stat *crypt_stat = |
Al Viro | bef5bc2 | 2010-05-21 10:56:12 -0400 | [diff] [blame] | 295 | &ecryptfs_inode_to_private(mapping->host)->crypt_stat; |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 296 | |
Tyler Hicks | fed8859 | 2011-02-23 00:54:20 -0600 | [diff] [blame] | 297 | if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 298 | rc = ecryptfs_read_lower_page_segment( |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 299 | page, index, 0, PAGE_SIZE, mapping->host); |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 300 | if (rc) { |
Masanari Iida | 971bd8f | 2015-05-20 23:54:02 +0900 | [diff] [blame] | 301 | printk(KERN_ERR "%s: Error attempting to read " |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 302 | "lower page segment; rc = [%d]\n", |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 303 | __func__, rc); |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 304 | ClearPageUptodate(page); |
| 305 | goto out; |
| 306 | } else |
| 307 | SetPageUptodate(page); |
| 308 | } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { |
| 309 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { |
| 310 | rc = ecryptfs_copy_up_encrypted_with_header( |
| 311 | page, crypt_stat); |
| 312 | if (rc) { |
| 313 | printk(KERN_ERR "%s: Error attempting " |
| 314 | "to copy the encrypted content " |
| 315 | "from the lower file whilst " |
| 316 | "inserting the metadata from " |
| 317 | "the xattr into the header; rc " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 318 | "= [%d]\n", __func__, rc); |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 319 | ClearPageUptodate(page); |
| 320 | goto out; |
| 321 | } |
| 322 | SetPageUptodate(page); |
| 323 | } else { |
| 324 | rc = ecryptfs_read_lower_page_segment( |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 325 | page, index, 0, PAGE_SIZE, |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 326 | mapping->host); |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 327 | if (rc) { |
| 328 | printk(KERN_ERR "%s: Error reading " |
| 329 | "page; rc = [%d]\n", |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 330 | __func__, rc); |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 331 | ClearPageUptodate(page); |
| 332 | goto out; |
| 333 | } |
| 334 | SetPageUptodate(page); |
| 335 | } |
| 336 | } else { |
Frank Swiderski | 2456248 | 2010-11-15 10:43:22 -0800 | [diff] [blame] | 337 | if (prev_page_end_size |
| 338 | >= i_size_read(page->mapping->host)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 339 | zero_user(page, 0, PAGE_SIZE); |
Li Wang | e4bc652 | 2012-10-30 19:52:40 +0800 | [diff] [blame] | 340 | SetPageUptodate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 341 | } else if (len < PAGE_SIZE) { |
Frank Swiderski | 2456248 | 2010-11-15 10:43:22 -0800 | [diff] [blame] | 342 | rc = ecryptfs_decrypt_page(page); |
| 343 | if (rc) { |
| 344 | printk(KERN_ERR "%s: Error decrypting " |
| 345 | "page at index [%ld]; " |
| 346 | "rc = [%d]\n", |
| 347 | __func__, page->index, rc); |
| 348 | ClearPageUptodate(page); |
| 349 | goto out; |
| 350 | } |
Li Wang | e4bc652 | 2012-10-30 19:52:40 +0800 | [diff] [blame] | 351 | SetPageUptodate(page); |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 352 | } |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 353 | } |
Michael Halcrow | 16a72c4 | 2007-10-16 01:28:14 -0700 | [diff] [blame] | 354 | } |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 355 | /* If creating a page or more of holes, zero them out via truncate. |
| 356 | * Note, this will increase i_size. */ |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 357 | if (index != 0) { |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 358 | if (prev_page_end_size > i_size_read(page->mapping->host)) { |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 359 | rc = ecryptfs_truncate(file->f_path.dentry, |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 360 | prev_page_end_size); |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 361 | if (rc) { |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 362 | printk(KERN_ERR "%s: Error on attempt to " |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 363 | "truncate to (higher) offset [%lld];" |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 364 | " rc = [%d]\n", __func__, |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 365 | prev_page_end_size, rc); |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 366 | goto out; |
| 367 | } |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 368 | } |
Eric Sandeen | 7a3f595 | 2007-12-17 16:20:10 -0800 | [diff] [blame] | 369 | } |
Michael Halcrow | e4465fd | 2008-03-04 14:29:24 -0800 | [diff] [blame] | 370 | /* Writing to a new page, and creating a small hole from start |
| 371 | * of page? Zero it out. */ |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 372 | if ((i_size_read(mapping->host) == prev_page_end_size) |
| 373 | && (pos != 0)) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 374 | zero_user(page, 0, PAGE_SIZE); |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 375 | out: |
Tyler Hicks | 50f198a | 2011-03-09 11:49:13 -0600 | [diff] [blame] | 376 | if (unlikely(rc)) { |
| 377 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 378 | put_page(page); |
Tyler Hicks | 50f198a | 2011-03-09 11:49:13 -0600 | [diff] [blame] | 379 | *pagep = NULL; |
| 380 | } |
Michael Halcrow | 53a2731 | 2007-05-23 13:58:15 -0700 | [diff] [blame] | 381 | return rc; |
| 382 | } |
| 383 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 384 | /** |
| 385 | * ecryptfs_write_inode_size_to_header |
| 386 | * |
| 387 | * Writes the lower file size to the first 8 bytes of the header. |
| 388 | * |
| 389 | * Returns zero on success; non-zero on error. |
| 390 | */ |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 391 | static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 392 | { |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 393 | char *file_size_virt; |
| 394 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 395 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 396 | file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL); |
| 397 | if (!file_size_virt) { |
| 398 | rc = -ENOMEM; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 399 | goto out; |
| 400 | } |
Harvey Harrison | 0a688ad | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 401 | put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 402 | rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0, |
| 403 | sizeof(u64)); |
| 404 | kfree(file_size_virt); |
Tyler Hicks | 96a7b9c | 2009-09-16 19:04:20 -0500 | [diff] [blame] | 405 | if (rc < 0) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 406 | printk(KERN_ERR "%s: Error writing file size to header; " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 407 | "rc = [%d]\n", __func__, rc); |
Tyler Hicks | 96a7b9c | 2009-09-16 19:04:20 -0500 | [diff] [blame] | 408 | else |
| 409 | rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 410 | out: |
| 411 | return rc; |
| 412 | } |
| 413 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 414 | struct kmem_cache *ecryptfs_xattr_cache; |
| 415 | |
| 416 | static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 417 | { |
| 418 | ssize_t size; |
| 419 | void *xattr_virt; |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 420 | struct dentry *lower_dentry = |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 421 | ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 422 | struct inode *lower_inode = d_inode(lower_dentry); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 423 | int rc; |
| 424 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 425 | if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) { |
| 426 | printk(KERN_WARNING |
| 427 | "No support for setting xattr in lower filesystem\n"); |
| 428 | rc = -ENOSYS; |
| 429 | goto out; |
| 430 | } |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 431 | xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); |
| 432 | if (!xattr_virt) { |
| 433 | printk(KERN_ERR "Out of memory whilst attempting to write " |
| 434 | "inode size to xattr\n"); |
| 435 | rc = -ENOMEM; |
| 436 | goto out; |
| 437 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 438 | inode_lock(lower_inode); |
Al Viro | ce23e64 | 2016-04-11 00:48:00 -0400 | [diff] [blame] | 439 | size = lower_inode->i_op->getxattr(lower_dentry, lower_inode, |
| 440 | ECRYPTFS_XATTR_NAME, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 441 | xattr_virt, PAGE_SIZE); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 442 | if (size < 0) |
| 443 | size = 8; |
Harvey Harrison | 0a688ad | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 444 | put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 445 | rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME, |
| 446 | xattr_virt, size, 0); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 447 | inode_unlock(lower_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 448 | if (rc) |
| 449 | printk(KERN_ERR "Error whilst attempting to write inode size " |
| 450 | "to lower file xattr; rc = [%d]\n", rc); |
| 451 | kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); |
| 452 | out: |
| 453 | return rc; |
| 454 | } |
| 455 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 456 | int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode) |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 457 | { |
| 458 | struct ecryptfs_crypt_stat *crypt_stat; |
| 459 | |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 460 | crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
Tyler Hicks | 13a791b | 2009-04-13 15:29:27 -0500 | [diff] [blame] | 461 | BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 462 | if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 463 | return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 464 | else |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 465 | return ecryptfs_write_inode_size_to_header(ecryptfs_inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 468 | /** |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 469 | * ecryptfs_write_end |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 470 | * @file: The eCryptfs file object |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 471 | * @mapping: The eCryptfs object |
| 472 | * @pos: The file position |
| 473 | * @len: The length of the data (unused) |
| 474 | * @copied: The amount of data copied |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 475 | * @page: The eCryptfs page |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 476 | * @fsdata: The fsdata (unused) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 477 | */ |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 478 | static int ecryptfs_write_end(struct file *file, |
| 479 | struct address_space *mapping, |
| 480 | loff_t pos, unsigned len, unsigned copied, |
| 481 | struct page *page, void *fsdata) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 482 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 483 | pgoff_t index = pos >> PAGE_SHIFT; |
| 484 | unsigned from = pos & (PAGE_SIZE - 1); |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 485 | unsigned to = from + copied; |
| 486 | struct inode *ecryptfs_inode = mapping->host; |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 487 | struct ecryptfs_crypt_stat *crypt_stat = |
Al Viro | bef5bc2 | 2010-05-21 10:56:12 -0400 | [diff] [blame] | 488 | &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 489 | int rc; |
| 490 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 491 | ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" |
Joe Perches | 888d57b | 2010-11-10 15:46:16 -0800 | [diff] [blame] | 492 | "(page w/ index = [0x%.16lx], to = [%d])\n", index, to); |
Tyler Hicks | 13a791b | 2009-04-13 15:29:27 -0500 | [diff] [blame] | 493 | if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
| 494 | rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0, |
| 495 | to); |
| 496 | if (!rc) { |
| 497 | rc = copied; |
| 498 | fsstack_copy_inode_size(ecryptfs_inode, |
| 499 | ecryptfs_inode_to_lower(ecryptfs_inode)); |
| 500 | } |
| 501 | goto out; |
| 502 | } |
Li Wang | e4bc652 | 2012-10-30 19:52:40 +0800 | [diff] [blame] | 503 | if (!PageUptodate(page)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 504 | if (copied < PAGE_SIZE) { |
Li Wang | e4bc652 | 2012-10-30 19:52:40 +0800 | [diff] [blame] | 505 | rc = 0; |
| 506 | goto out; |
| 507 | } |
| 508 | SetPageUptodate(page); |
| 509 | } |
Michael Halcrow | bf12be1 | 2007-10-16 01:28:11 -0700 | [diff] [blame] | 510 | /* Fills in zeros if 'to' goes beyond inode size */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 511 | rc = fill_zeros_to_end_of_page(page, to); |
| 512 | if (rc) { |
| 513 | ecryptfs_printk(KERN_WARNING, "Error attempting to fill " |
Joe Perches | 888d57b | 2010-11-10 15:46:16 -0800 | [diff] [blame] | 514 | "zeros in page with index = [0x%.16lx]\n", index); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 515 | goto out; |
| 516 | } |
Tyler Hicks | 821f749 | 2012-07-03 16:50:57 -0700 | [diff] [blame] | 517 | rc = ecryptfs_encrypt_page(page); |
| 518 | if (rc) { |
| 519 | ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " |
| 520 | "index [0x%.16lx])\n", index); |
| 521 | goto out; |
| 522 | } |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 523 | if (pos + copied > i_size_read(ecryptfs_inode)) { |
| 524 | i_size_write(ecryptfs_inode, pos + copied); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 525 | ecryptfs_printk(KERN_DEBUG, "Expanded file size to " |
Joe Perches | 888d57b | 2010-11-10 15:46:16 -0800 | [diff] [blame] | 526 | "[0x%.16llx]\n", |
| 527 | (unsigned long long)i_size_read(ecryptfs_inode)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 528 | } |
Tyler Hicks | 821f749 | 2012-07-03 16:50:57 -0700 | [diff] [blame] | 529 | rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); |
| 530 | if (rc) |
| 531 | printk(KERN_ERR "Error writing inode size to metadata; " |
| 532 | "rc = [%d]\n", rc); |
| 533 | else |
| 534 | rc = copied; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 535 | out: |
Tyler Hicks | 821f749 | 2012-07-03 16:50:57 -0700 | [diff] [blame] | 536 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 537 | put_page(page); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 538 | return rc; |
| 539 | } |
| 540 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 541 | static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) |
| 542 | { |
| 543 | int rc = 0; |
| 544 | struct inode *inode; |
| 545 | struct inode *lower_inode; |
| 546 | |
| 547 | inode = (struct inode *)mapping->host; |
| 548 | lower_inode = ecryptfs_inode_to_lower(inode); |
| 549 | if (lower_inode->i_mapping->a_ops->bmap) |
| 550 | rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, |
| 551 | block); |
| 552 | return rc; |
| 553 | } |
| 554 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 555 | const struct address_space_operations ecryptfs_aops = { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 556 | .writepage = ecryptfs_writepage, |
| 557 | .readpage = ecryptfs_readpage, |
Badari Pulavarty | 807b7eb | 2008-10-15 22:02:50 -0700 | [diff] [blame] | 558 | .write_begin = ecryptfs_write_begin, |
| 559 | .write_end = ecryptfs_write_end, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 560 | .bmap = ecryptfs_bmap, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 561 | }; |