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