blob: e6b1d80952b91d7dda9a3689fe018cf215ec5a11 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
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 Halcrowdd2a3b72007-02-12 00:53:46 -08009 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -070010 * 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 Halcrow237fead2006-10-04 02:16:22 -070033#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Harvey Harrison0a688ad2008-07-23 21:30:07 -070035#include <asm/unaligned.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070036#include "ecryptfs_kernel.h"
37
Michael Halcrow237fead2006-10-04 02:16:22 -070038/**
Michael Halcrow16a72c42007-10-16 01:28:14 -070039 * ecryptfs_get_locked_page
Michael Halcrow237fead2006-10-04 02:16:22 -070040 *
41 * Get one page from cache or lower f/s, return error otherwise.
42 *
Michael Halcrow16a72c42007-10-16 01:28:14 -070043 * Returns locked and up-to-date page (if ok), with increased
Michael Halcrow237fead2006-10-04 02:16:22 -070044 * refcnt.
45 */
Al Viro02bd9792010-05-21 11:02:14 -040046struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index)
Michael Halcrow237fead2006-10-04 02:16:22 -070047{
Al Viro02bd9792010-05-21 11:02:14 -040048 struct page *page = read_mapping_page(inode->i_mapping, index, NULL);
Michael Halcrow16a72c42007-10-16 01:28:14 -070049 if (!IS_ERR(page))
50 lock_page(page);
51 return page;
Michael Halcrow237fead2006-10-04 02:16:22 -070052}
53
Michael Halcrow237fead2006-10-04 02:16:22 -070054/**
Michael Halcrow237fead2006-10-04 02:16:22 -070055 * ecryptfs_writepage
56 * @page: Page that is locked before this call is made
57 *
58 * Returns zero on success; non-zero otherwise
Li Wang1589cb12012-01-25 15:40:31 +080059 *
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 Halcrow237fead2006-10-04 02:16:22 -070063 */
64static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
65{
Michael Halcrow237fead2006-10-04 02:16:22 -070066 int rc;
67
Michael Halcrow0216f7f2007-10-16 01:28:08 -070068 rc = ecryptfs_encrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -070069 if (rc) {
70 ecryptfs_printk(KERN_WARNING, "Error encrypting "
Joe Perches888d57b2010-11-10 15:46:16 -080071 "page (upper index [0x%.16lx])\n", page->index);
Michael Halcrow237fead2006-10-04 02:16:22 -070072 ClearPageUptodate(page);
73 goto out;
74 }
75 SetPageUptodate(page);
Michael Halcrow237fead2006-10-04 02:16:22 -070076out:
Thieu Le57db4e82011-03-08 16:26:03 -080077 unlock_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -070078 return rc;
79}
80
Tyler Hicksf4e60e62010-02-11 00:02:32 -060081static 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 Halcrow237fead2006-10-04 02:16:22 -070094/**
Michael Halcrowe77a56d2007-02-12 00:53:47 -080095 * 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 Halcrow237fead2006-10-04 02:16:22 -0700109
110/**
Michael Halcrowbf12be12007-10-16 01:28:11 -0700111 * 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 */
120static int
121ecryptfs_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. Shutemov09cbfea2016-04-01 15:29:47 +0300125 loff_t num_extents_per_page = (PAGE_SIZE
Michael Halcrowbf12be12007-10-16 01:28:11 -0700126 / crypt_stat->extent_size);
127 int rc = 0;
128
129 while (extent_num_in_page < num_extents_per_page) {
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700130 loff_t view_extent_num = ((((loff_t)page->index)
131 * num_extents_per_page)
Michael Halcrowbf12be12007-10-16 01:28:11 -0700132 + extent_num_in_page);
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800133 size_t num_header_extents_at_front =
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600134 (crypt_stat->metadata_size / crypt_stat->extent_size);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700135
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800136 if (view_extent_num < num_header_extents_at_front) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700137 /* This is a header extent */
138 char *page_virt;
139
Cong Wang465c9342012-02-10 13:39:50 +0800140 page_virt = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300141 memset(page_virt, 0, PAGE_SIZE);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700142 /* TODO: Support more than one header extent */
143 if (view_extent_num == 0) {
Tyler Hicks157f1072010-02-11 07:10:38 -0600144 size_t written;
145
Michael Halcrowbf12be12007-10-16 01:28:11 -0700146 rc = ecryptfs_read_xattr_region(
147 page_virt, page->mapping->host);
Tyler Hicksf4e60e62010-02-11 00:02:32 -0600148 strip_xattr_flag(page_virt + 16, crypt_stat);
Tyler Hicks157f1072010-02-11 07:10:38 -0600149 ecryptfs_write_header_metadata(page_virt + 20,
150 crypt_stat,
151 &written);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700152 }
Cong Wang465c9342012-02-10 13:39:50 +0800153 kunmap_atomic(page_virt);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700154 flush_dcache_page(page);
155 if (rc) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700156 printk(KERN_ERR "%s: Error reading xattr "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700157 "region; rc = [%d]\n", __func__, rc);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700158 goto out;
159 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700160 } else {
161 /* This is an encrypted data extent */
162 loff_t lower_offset =
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800163 ((view_extent_num * crypt_stat->extent_size)
Tyler Hicksfa3ef1c2010-02-11 05:09:14 -0600164 - crypt_stat->metadata_size);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700165
166 rc = ecryptfs_read_lower_page_segment(
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300167 page, (lower_offset >> PAGE_SHIFT),
168 (lower_offset & ~PAGE_MASK),
Michael Halcrowbf12be12007-10-16 01:28:11 -0700169 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 Harrison18d1dbf2008-04-29 00:59:48 -0700173 "file; rc = [%d]\n", __func__,
Michael Halcrowbf12be12007-10-16 01:28:11 -0700174 lower_offset, rc);
175 goto out;
176 }
177 }
178 extent_num_in_page++;
179 }
180out:
181 return rc;
182}
183
184/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700185 * ecryptfs_readpage
Michael Halcrowbf12be12007-10-16 01:28:11 -0700186 * @file: An eCryptfs file
187 * @page: Page from eCryptfs inode mapping into which to stick the read data
Michael Halcrow237fead2006-10-04 02:16:22 -0700188 *
189 * Read in a page, decrypting if necessary.
190 *
191 * Returns zero on success; non-zero on error.
192 */
193static int ecryptfs_readpage(struct file *file, struct page *page)
194{
Michael Halcrowbf12be12007-10-16 01:28:11 -0700195 struct ecryptfs_crypt_stat *crypt_stat =
Al Virobef5bc22010-05-21 10:56:12 -0400196 &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700197 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700198
Tyler Hicksfed88592011-02-23 00:54:20 -0600199 if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700200 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300201 PAGE_SIZE,
Michael Halcrowbf12be12007-10-16 01:28:11 -0700202 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800203 } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
204 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700205 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 Harrison18d1dbf2008-04-29 00:59:48 -0700212 "[%d]\n", __func__, rc);
Michael Halcrowbf12be12007-10-16 01:28:11 -0700213 goto out;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800214 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700215
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800216 } else {
Michael Halcrowbf12be12007-10-16 01:28:11 -0700217 rc = ecryptfs_read_lower_page_segment(
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300218 page, page->index, 0, PAGE_SIZE,
Michael Halcrowbf12be12007-10-16 01:28:11 -0700219 page->mapping->host);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800220 if (rc) {
221 printk(KERN_ERR "Error reading page; rc = "
222 "[%d]\n", rc);
223 goto out;
224 }
225 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700226 } else {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700227 rc = ecryptfs_decrypt_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700228 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700229 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
230 "rc = [%d]\n", rc);
231 goto out;
232 }
233 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700234out:
Michael Halcrow16a72c42007-10-16 01:28:14 -0700235 if (rc)
236 ClearPageUptodate(page);
237 else
238 SetPageUptodate(page);
Joe Perches888d57b2010-11-10 15:46:16 -0800239 ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n",
Michael Halcrow237fead2006-10-04 02:16:22 -0700240 page->index);
241 unlock_page(page);
242 return rc;
243}
244
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800245/**
246 * Called with lower inode mutex held.
247 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700248static 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 Halcrow237fead2006-10-04 02:16:22 -0700252
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300253 if ((i_size_read(inode) / PAGE_SIZE) != page->index)
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800254 goto out;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300255 end_byte_in_page = i_size_read(inode) % PAGE_SIZE;
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800256 if (to > end_byte_in_page)
257 end_byte_in_page = to;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300258 zero_user_segment(page, end_byte_in_page, PAGE_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700259out:
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800260 return 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700261}
262
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800263/**
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700264 * ecryptfs_write_begin
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800265 * @file: The eCryptfs file
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700266 * @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 Halcrowe4465fd2008-03-04 14:29:24 -0800272 *
273 * This function must zero any hole we create
274 *
275 * Returns zero on success; non-zero otherwise
276 */
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700277static 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 Halcrow53a27312007-05-23 13:58:15 -0700281{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300282 pgoff_t index = pos >> PAGE_SHIFT;
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700283 struct page *page;
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800284 loff_t prev_page_end_size;
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800285 int rc = 0;
Michael Halcrow53a27312007-05-23 13:58:15 -0700286
Nick Piggin54566b22009-01-04 12:00:53 -0800287 page = grab_cache_page_write_begin(mapping, index, flags);
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700288 if (!page)
289 return -ENOMEM;
290 *pagep = page;
291
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300292 prev_page_end_size = ((loff_t)index << PAGE_SHIFT);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700293 if (!PageUptodate(page)) {
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800294 struct ecryptfs_crypt_stat *crypt_stat =
Al Virobef5bc22010-05-21 10:56:12 -0400295 &ecryptfs_inode_to_private(mapping->host)->crypt_stat;
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800296
Tyler Hicksfed88592011-02-23 00:54:20 -0600297 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800298 rc = ecryptfs_read_lower_page_segment(
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300299 page, index, 0, PAGE_SIZE, mapping->host);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800300 if (rc) {
Masanari Iida971bd8f2015-05-20 23:54:02 +0900301 printk(KERN_ERR "%s: Error attempting to read "
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800302 "lower page segment; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700303 __func__, rc);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800304 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 Harrison18d1dbf2008-04-29 00:59:48 -0700318 "= [%d]\n", __func__, rc);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800319 ClearPageUptodate(page);
320 goto out;
321 }
322 SetPageUptodate(page);
323 } else {
324 rc = ecryptfs_read_lower_page_segment(
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300325 page, index, 0, PAGE_SIZE,
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700326 mapping->host);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800327 if (rc) {
328 printk(KERN_ERR "%s: Error reading "
329 "page; rc = [%d]\n",
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700330 __func__, rc);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800331 ClearPageUptodate(page);
332 goto out;
333 }
334 SetPageUptodate(page);
335 }
336 } else {
Frank Swiderski24562482010-11-15 10:43:22 -0800337 if (prev_page_end_size
338 >= i_size_read(page->mapping->host)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300339 zero_user(page, 0, PAGE_SIZE);
Li Wange4bc6522012-10-30 19:52:40 +0800340 SetPageUptodate(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300341 } else if (len < PAGE_SIZE) {
Frank Swiderski24562482010-11-15 10:43:22 -0800342 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 Wange4bc6522012-10-30 19:52:40 +0800351 SetPageUptodate(page);
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800352 }
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800353 }
Michael Halcrow16a72c42007-10-16 01:28:14 -0700354 }
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800355 /* If creating a page or more of holes, zero them out via truncate.
356 * Note, this will increase i_size. */
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700357 if (index != 0) {
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800358 if (prev_page_end_size > i_size_read(page->mapping->host)) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700359 rc = ecryptfs_truncate(file->f_path.dentry,
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800360 prev_page_end_size);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700361 if (rc) {
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800362 printk(KERN_ERR "%s: Error on attempt to "
Michael Halcrow240e2df2007-06-27 14:09:44 -0700363 "truncate to (higher) offset [%lld];"
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700364 " rc = [%d]\n", __func__,
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800365 prev_page_end_size, rc);
Michael Halcrow240e2df2007-06-27 14:09:44 -0700366 goto out;
367 }
Michael Halcrow53a27312007-05-23 13:58:15 -0700368 }
Eric Sandeen7a3f5952007-12-17 16:20:10 -0800369 }
Michael Halcrowe4465fd2008-03-04 14:29:24 -0800370 /* Writing to a new page, and creating a small hole from start
371 * of page? Zero it out. */
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700372 if ((i_size_read(mapping->host) == prev_page_end_size)
373 && (pos != 0))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300374 zero_user(page, 0, PAGE_SIZE);
Michael Halcrow53a27312007-05-23 13:58:15 -0700375out:
Tyler Hicks50f198a2011-03-09 11:49:13 -0600376 if (unlikely(rc)) {
377 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300378 put_page(page);
Tyler Hicks50f198a2011-03-09 11:49:13 -0600379 *pagep = NULL;
380 }
Michael Halcrow53a27312007-05-23 13:58:15 -0700381 return rc;
382}
383
Michael Halcrow237fead2006-10-04 02:16:22 -0700384/**
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 Halcrow0216f7f2007-10-16 01:28:08 -0700391static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700392{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700393 char *file_size_virt;
394 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700395
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700396 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
397 if (!file_size_virt) {
398 rc = -ENOMEM;
Michael Halcrow237fead2006-10-04 02:16:22 -0700399 goto out;
400 }
Harvey Harrison0a688ad2008-07-23 21:30:07 -0700401 put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700402 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
403 sizeof(u64));
404 kfree(file_size_virt);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -0500405 if (rc < 0)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700406 printk(KERN_ERR "%s: Error writing file size to header; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700407 "rc = [%d]\n", __func__, rc);
Tyler Hicks96a7b9c2009-09-16 19:04:20 -0500408 else
409 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700410out:
411 return rc;
412}
413
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700414struct kmem_cache *ecryptfs_xattr_cache;
415
416static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800417{
418 ssize_t size;
419 void *xattr_virt;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700420 struct dentry *lower_dentry =
Al Virob5830432014-10-31 01:22:04 -0400421 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000422 struct inode *lower_inode = d_inode(lower_dentry);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800423 int rc;
424
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700425 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 Halcrowdd2a3b72007-02-12 00:53:46 -0800431 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 Viro59551022016-01-22 15:40:57 -0500438 inode_lock(lower_inode);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700439 size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300440 xattr_virt, PAGE_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800441 if (size < 0)
442 size = 8;
Harvey Harrison0a688ad2008-07-23 21:30:07 -0700443 put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700444 rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
445 xattr_virt, size, 0);
Al Viro59551022016-01-22 15:40:57 -0500446 inode_unlock(lower_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800447 if (rc)
448 printk(KERN_ERR "Error whilst attempting to write inode size "
449 "to lower file xattr; rc = [%d]\n", rc);
450 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
451out:
452 return rc;
453}
454
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700455int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800456{
457 struct ecryptfs_crypt_stat *crypt_stat;
458
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700459 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Tyler Hicks13a791b2009-04-13 15:29:27 -0500460 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800461 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700462 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800463 else
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700464 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800465}
466
Michael Halcrow237fead2006-10-04 02:16:22 -0700467/**
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700468 * ecryptfs_write_end
Michael Halcrow237fead2006-10-04 02:16:22 -0700469 * @file: The eCryptfs file object
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700470 * @mapping: The eCryptfs object
471 * @pos: The file position
472 * @len: The length of the data (unused)
473 * @copied: The amount of data copied
Michael Halcrow237fead2006-10-04 02:16:22 -0700474 * @page: The eCryptfs page
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700475 * @fsdata: The fsdata (unused)
Michael Halcrow237fead2006-10-04 02:16:22 -0700476 */
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700477static int ecryptfs_write_end(struct file *file,
478 struct address_space *mapping,
479 loff_t pos, unsigned len, unsigned copied,
480 struct page *page, void *fsdata)
Michael Halcrow237fead2006-10-04 02:16:22 -0700481{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300482 pgoff_t index = pos >> PAGE_SHIFT;
483 unsigned from = pos & (PAGE_SIZE - 1);
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700484 unsigned to = from + copied;
485 struct inode *ecryptfs_inode = mapping->host;
Michael Halcrowbf12be12007-10-16 01:28:11 -0700486 struct ecryptfs_crypt_stat *crypt_stat =
Al Virobef5bc22010-05-21 10:56:12 -0400487 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700488 int rc;
489
Michael Halcrow237fead2006-10-04 02:16:22 -0700490 ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
Joe Perches888d57b2010-11-10 15:46:16 -0800491 "(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
Tyler Hicks13a791b2009-04-13 15:29:27 -0500492 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
493 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
494 to);
495 if (!rc) {
496 rc = copied;
497 fsstack_copy_inode_size(ecryptfs_inode,
498 ecryptfs_inode_to_lower(ecryptfs_inode));
499 }
500 goto out;
501 }
Li Wange4bc6522012-10-30 19:52:40 +0800502 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300503 if (copied < PAGE_SIZE) {
Li Wange4bc6522012-10-30 19:52:40 +0800504 rc = 0;
505 goto out;
506 }
507 SetPageUptodate(page);
508 }
Michael Halcrowbf12be12007-10-16 01:28:11 -0700509 /* Fills in zeros if 'to' goes beyond inode size */
Michael Halcrow237fead2006-10-04 02:16:22 -0700510 rc = fill_zeros_to_end_of_page(page, to);
511 if (rc) {
512 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
Joe Perches888d57b2010-11-10 15:46:16 -0800513 "zeros in page with index = [0x%.16lx]\n", index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700514 goto out;
515 }
Tyler Hicks821f7492012-07-03 16:50:57 -0700516 rc = ecryptfs_encrypt_page(page);
517 if (rc) {
518 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
519 "index [0x%.16lx])\n", index);
520 goto out;
521 }
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700522 if (pos + copied > i_size_read(ecryptfs_inode)) {
523 i_size_write(ecryptfs_inode, pos + copied);
Michael Halcrow237fead2006-10-04 02:16:22 -0700524 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
Joe Perches888d57b2010-11-10 15:46:16 -0800525 "[0x%.16llx]\n",
526 (unsigned long long)i_size_read(ecryptfs_inode));
Michael Halcrow237fead2006-10-04 02:16:22 -0700527 }
Tyler Hicks821f7492012-07-03 16:50:57 -0700528 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
529 if (rc)
530 printk(KERN_ERR "Error writing inode size to metadata; "
531 "rc = [%d]\n", rc);
532 else
533 rc = copied;
Michael Halcrow237fead2006-10-04 02:16:22 -0700534out:
Tyler Hicks821f7492012-07-03 16:50:57 -0700535 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300536 put_page(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700537 return rc;
538}
539
Michael Halcrow237fead2006-10-04 02:16:22 -0700540static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
541{
542 int rc = 0;
543 struct inode *inode;
544 struct inode *lower_inode;
545
546 inode = (struct inode *)mapping->host;
547 lower_inode = ecryptfs_inode_to_lower(inode);
548 if (lower_inode->i_mapping->a_ops->bmap)
549 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
550 block);
551 return rc;
552}
553
Alexey Dobriyan7f094102009-09-21 17:01:10 -0700554const struct address_space_operations ecryptfs_aops = {
Michael Halcrow237fead2006-10-04 02:16:22 -0700555 .writepage = ecryptfs_writepage,
556 .readpage = ecryptfs_readpage,
Badari Pulavarty807b7eb2008-10-15 22:02:50 -0700557 .write_begin = ecryptfs_write_begin,
558 .write_end = ecryptfs_write_end,
Michael Halcrow237fead2006-10-04 02:16:22 -0700559 .bmap = ecryptfs_bmap,
Michael Halcrow237fead2006-10-04 02:16:22 -0700560};