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