xref: /linux/fs/ecryptfs/read_write.c (revision d6a13c17164fccab8aa96ca435ddacbf428335ca)
1da0102a1SMichael Halcrow /**
2da0102a1SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3da0102a1SMichael Halcrow  *
4da0102a1SMichael Halcrow  * Copyright (C) 2007 International Business Machines Corp.
5da0102a1SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
6da0102a1SMichael Halcrow  *
7da0102a1SMichael Halcrow  * This program is free software; you can redistribute it and/or
8da0102a1SMichael Halcrow  * modify it under the terms of the GNU General Public License as
9da0102a1SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
10da0102a1SMichael Halcrow  * License, or (at your option) any later version.
11da0102a1SMichael Halcrow  *
12da0102a1SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
13da0102a1SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
14da0102a1SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15da0102a1SMichael Halcrow  * General Public License for more details.
16da0102a1SMichael Halcrow  *
17da0102a1SMichael Halcrow  * You should have received a copy of the GNU General Public License
18da0102a1SMichael Halcrow  * along with this program; if not, write to the Free Software
19da0102a1SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20da0102a1SMichael Halcrow  * 02111-1307, USA.
21da0102a1SMichael Halcrow  */
22da0102a1SMichael Halcrow 
23da0102a1SMichael Halcrow #include <linux/fs.h>
24da0102a1SMichael Halcrow #include <linux/pagemap.h>
25da0102a1SMichael Halcrow #include "ecryptfs_kernel.h"
26da0102a1SMichael Halcrow 
27da0102a1SMichael Halcrow /**
28da0102a1SMichael Halcrow  * ecryptfs_write_lower
29da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
30da0102a1SMichael Halcrow  * @data: Data to write
31da0102a1SMichael Halcrow  * @offset: Byte offset in the lower file to which to write the data
32da0102a1SMichael Halcrow  * @size: Number of bytes from @data to write at @offset in the lower
33da0102a1SMichael Halcrow  *        file
34da0102a1SMichael Halcrow  *
35da0102a1SMichael Halcrow  * Write data to the lower file.
36da0102a1SMichael Halcrow  *
37da0102a1SMichael Halcrow  * Returns zero on success; non-zero on error
38da0102a1SMichael Halcrow  */
39da0102a1SMichael Halcrow int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
40da0102a1SMichael Halcrow 			 loff_t offset, size_t size)
41da0102a1SMichael Halcrow {
42da0102a1SMichael Halcrow 	struct ecryptfs_inode_info *inode_info;
43da0102a1SMichael Halcrow 	ssize_t octets_written;
44da0102a1SMichael Halcrow 	mm_segment_t fs_save;
45da0102a1SMichael Halcrow 	int rc = 0;
46da0102a1SMichael Halcrow 
47da0102a1SMichael Halcrow 	inode_info = ecryptfs_inode_to_private(ecryptfs_inode);
48da0102a1SMichael Halcrow 	mutex_lock(&inode_info->lower_file_mutex);
49da0102a1SMichael Halcrow 	BUG_ON(!inode_info->lower_file);
50da0102a1SMichael Halcrow 	inode_info->lower_file->f_pos = offset;
51da0102a1SMichael Halcrow 	fs_save = get_fs();
52da0102a1SMichael Halcrow 	set_fs(get_ds());
53da0102a1SMichael Halcrow 	octets_written = vfs_write(inode_info->lower_file, data, size,
54da0102a1SMichael Halcrow 				   &inode_info->lower_file->f_pos);
55da0102a1SMichael Halcrow 	set_fs(fs_save);
56da0102a1SMichael Halcrow 	if (octets_written < 0) {
57da0102a1SMichael Halcrow 		printk(KERN_ERR "%s: octets_written = [%td]; "
58da0102a1SMichael Halcrow 		       "expected [%td]\n", __FUNCTION__, octets_written, size);
59da0102a1SMichael Halcrow 		rc = -EINVAL;
60da0102a1SMichael Halcrow 	}
61da0102a1SMichael Halcrow 	mutex_unlock(&inode_info->lower_file_mutex);
62da0102a1SMichael Halcrow 	mark_inode_dirty_sync(ecryptfs_inode);
63da0102a1SMichael Halcrow 	return rc;
64da0102a1SMichael Halcrow }
65da0102a1SMichael Halcrow 
66da0102a1SMichael Halcrow /**
67da0102a1SMichael Halcrow  * ecryptfs_write_lower_page_segment
68da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
69da0102a1SMichael Halcrow  * @page_for_lower: The page containing the data to be written to the
70da0102a1SMichael Halcrow  *                  lower file
71da0102a1SMichael Halcrow  * @offset_in_page: The offset in the @page_for_lower from which to
72da0102a1SMichael Halcrow  *                  start writing the data
73da0102a1SMichael Halcrow  * @size: The amount of data from @page_for_lower to write to the
74da0102a1SMichael Halcrow  *        lower file
75da0102a1SMichael Halcrow  *
76da0102a1SMichael Halcrow  * Determines the byte offset in the file for the given page and
77da0102a1SMichael Halcrow  * offset within the page, maps the page, and makes the call to write
78da0102a1SMichael Halcrow  * the contents of @page_for_lower to the lower inode.
79da0102a1SMichael Halcrow  *
80da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
81da0102a1SMichael Halcrow  */
82da0102a1SMichael Halcrow int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
83da0102a1SMichael Halcrow 				      struct page *page_for_lower,
84da0102a1SMichael Halcrow 				      size_t offset_in_page, size_t size)
85da0102a1SMichael Halcrow {
86da0102a1SMichael Halcrow 	char *virt;
87da0102a1SMichael Halcrow 	loff_t offset;
88da0102a1SMichael Halcrow 	int rc;
89da0102a1SMichael Halcrow 
90*d6a13c17SMichael Halcrow 	offset = ((((off_t)page_for_lower->index) << PAGE_CACHE_SHIFT)
91*d6a13c17SMichael Halcrow 		  + offset_in_page);
92da0102a1SMichael Halcrow 	virt = kmap(page_for_lower);
93da0102a1SMichael Halcrow 	rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
94da0102a1SMichael Halcrow 	kunmap(page_for_lower);
95da0102a1SMichael Halcrow 	return rc;
96da0102a1SMichael Halcrow }
97da0102a1SMichael Halcrow 
98da0102a1SMichael Halcrow /**
99da0102a1SMichael Halcrow  * ecryptfs_write
100da0102a1SMichael Halcrow  * @ecryptfs_file: The eCryptfs file into which to write
101da0102a1SMichael Halcrow  * @data: Virtual address where data to write is located
102da0102a1SMichael Halcrow  * @offset: Offset in the eCryptfs file at which to begin writing the
103da0102a1SMichael Halcrow  *          data from @data
104da0102a1SMichael Halcrow  * @size: The number of bytes to write from @data
105da0102a1SMichael Halcrow  *
106da0102a1SMichael Halcrow  * Write an arbitrary amount of data to an arbitrary location in the
107da0102a1SMichael Halcrow  * eCryptfs inode page cache. This is done on a page-by-page, and then
108da0102a1SMichael Halcrow  * by an extent-by-extent, basis; individual extents are encrypted and
109da0102a1SMichael Halcrow  * written to the lower page cache (via VFS writes). This function
110da0102a1SMichael Halcrow  * takes care of all the address translation to locations in the lower
111da0102a1SMichael Halcrow  * filesystem; it also handles truncate events, writing out zeros
112da0102a1SMichael Halcrow  * where necessary.
113da0102a1SMichael Halcrow  *
114da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
115da0102a1SMichael Halcrow  */
116da0102a1SMichael Halcrow int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
117da0102a1SMichael Halcrow 		   size_t size)
118da0102a1SMichael Halcrow {
119da0102a1SMichael Halcrow 	struct page *ecryptfs_page;
120da0102a1SMichael Halcrow 	char *ecryptfs_page_virt;
121*d6a13c17SMichael Halcrow 	loff_t ecryptfs_file_size =
122*d6a13c17SMichael Halcrow 		i_size_read(ecryptfs_file->f_dentry->d_inode);
123da0102a1SMichael Halcrow 	loff_t data_offset = 0;
124da0102a1SMichael Halcrow 	loff_t pos;
125da0102a1SMichael Halcrow 	int rc = 0;
126da0102a1SMichael Halcrow 
127da0102a1SMichael Halcrow 	if (offset > ecryptfs_file_size)
128da0102a1SMichael Halcrow 		pos = ecryptfs_file_size;
129da0102a1SMichael Halcrow 	else
130da0102a1SMichael Halcrow 		pos = offset;
131da0102a1SMichael Halcrow 	while (pos < (offset + size)) {
132da0102a1SMichael Halcrow 		pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
133da0102a1SMichael Halcrow 		size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
134da0102a1SMichael Halcrow 		size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
135da0102a1SMichael Halcrow 		size_t total_remaining_bytes = ((offset + size) - pos);
136da0102a1SMichael Halcrow 
137da0102a1SMichael Halcrow 		if (num_bytes > total_remaining_bytes)
138da0102a1SMichael Halcrow 			num_bytes = total_remaining_bytes;
139da0102a1SMichael Halcrow 		if (pos < offset) {
140da0102a1SMichael Halcrow 			size_t total_remaining_zeros = (offset - pos);
141da0102a1SMichael Halcrow 
142da0102a1SMichael Halcrow 			if (num_bytes > total_remaining_zeros)
143da0102a1SMichael Halcrow 				num_bytes = total_remaining_zeros;
144da0102a1SMichael Halcrow 		}
145da0102a1SMichael Halcrow 		ecryptfs_page = ecryptfs_get1page(ecryptfs_file,
146da0102a1SMichael Halcrow 						  ecryptfs_page_idx);
147da0102a1SMichael Halcrow 		if (IS_ERR(ecryptfs_page)) {
148da0102a1SMichael Halcrow 			rc = PTR_ERR(ecryptfs_page);
149da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error getting page at "
150da0102a1SMichael Halcrow 			       "index [%ld] from eCryptfs inode "
151da0102a1SMichael Halcrow 			       "mapping; rc = [%d]\n", __FUNCTION__,
152da0102a1SMichael Halcrow 			       ecryptfs_page_idx, rc);
153da0102a1SMichael Halcrow 			goto out;
154da0102a1SMichael Halcrow 		}
155da0102a1SMichael Halcrow 		if (start_offset_in_page) {
156da0102a1SMichael Halcrow 			/* Read in the page from the lower
157da0102a1SMichael Halcrow 			 * into the eCryptfs inode page cache,
158da0102a1SMichael Halcrow 			 * decrypting */
1590216f7f7SMichael Halcrow 			rc = ecryptfs_decrypt_page(ecryptfs_page);
1600216f7f7SMichael Halcrow 			if (rc) {
161da0102a1SMichael Halcrow 				printk(KERN_ERR "%s: Error decrypting "
162da0102a1SMichael Halcrow 				       "page; rc = [%d]\n",
163da0102a1SMichael Halcrow 				       __FUNCTION__, rc);
164da0102a1SMichael Halcrow 				page_cache_release(ecryptfs_page);
165da0102a1SMichael Halcrow 				goto out;
166da0102a1SMichael Halcrow 			}
167da0102a1SMichael Halcrow 		}
168da0102a1SMichael Halcrow 		ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
169da0102a1SMichael Halcrow 		if (pos >= offset) {
170da0102a1SMichael Halcrow 			memcpy(((char *)ecryptfs_page_virt
171da0102a1SMichael Halcrow 				+ start_offset_in_page),
172da0102a1SMichael Halcrow 			       (data + data_offset), num_bytes);
173da0102a1SMichael Halcrow 			data_offset += num_bytes;
174da0102a1SMichael Halcrow 		} else {
175da0102a1SMichael Halcrow 			/* We are extending past the previous end of the file.
176da0102a1SMichael Halcrow 			 * Fill in zero values up to the start of where we
177da0102a1SMichael Halcrow 			 * will be writing data. */
178da0102a1SMichael Halcrow 			memset(((char *)ecryptfs_page_virt
179da0102a1SMichael Halcrow 				+ start_offset_in_page), 0, num_bytes);
180da0102a1SMichael Halcrow 		}
181da0102a1SMichael Halcrow 		kunmap_atomic(ecryptfs_page_virt, KM_USER0);
182da0102a1SMichael Halcrow 		flush_dcache_page(ecryptfs_page);
1830216f7f7SMichael Halcrow 		rc = ecryptfs_encrypt_page(ecryptfs_page);
184da0102a1SMichael Halcrow 		if (rc) {
185da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting "
186da0102a1SMichael Halcrow 			       "page; rc = [%d]\n", __FUNCTION__, rc);
187da0102a1SMichael Halcrow 			page_cache_release(ecryptfs_page);
188da0102a1SMichael Halcrow 			goto out;
189da0102a1SMichael Halcrow 		}
190da0102a1SMichael Halcrow 		page_cache_release(ecryptfs_page);
191da0102a1SMichael Halcrow 		pos += num_bytes;
192da0102a1SMichael Halcrow 	}
193da0102a1SMichael Halcrow 	if ((offset + size) > ecryptfs_file_size) {
194da0102a1SMichael Halcrow 		i_size_write(ecryptfs_file->f_dentry->d_inode, (offset + size));
1950216f7f7SMichael Halcrow 		rc = ecryptfs_write_inode_size_to_metadata(
1960216f7f7SMichael Halcrow 			ecryptfs_file->f_dentry->d_inode);
197da0102a1SMichael Halcrow 		if (rc) {
198da0102a1SMichael Halcrow 			printk(KERN_ERR	"Problem with "
199da0102a1SMichael Halcrow 			       "ecryptfs_write_inode_size_to_metadata; "
200da0102a1SMichael Halcrow 			       "rc = [%d]\n", rc);
201da0102a1SMichael Halcrow 			goto out;
202da0102a1SMichael Halcrow 		}
203da0102a1SMichael Halcrow 	}
204da0102a1SMichael Halcrow out:
205da0102a1SMichael Halcrow 	return rc;
206da0102a1SMichael Halcrow }
207da0102a1SMichael Halcrow 
208da0102a1SMichael Halcrow /**
209da0102a1SMichael Halcrow  * ecryptfs_read_lower
210da0102a1SMichael Halcrow  * @data: The read data is stored here by this function
211da0102a1SMichael Halcrow  * @offset: Byte offset in the lower file from which to read the data
212da0102a1SMichael Halcrow  * @size: Number of bytes to read from @offset of the lower file and
213da0102a1SMichael Halcrow  *        store into @data
214da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
215da0102a1SMichael Halcrow  *
216da0102a1SMichael Halcrow  * Read @size bytes of data at byte offset @offset from the lower
217da0102a1SMichael Halcrow  * inode into memory location @data.
218da0102a1SMichael Halcrow  *
219da0102a1SMichael Halcrow  * Returns zero on success; non-zero on error
220da0102a1SMichael Halcrow  */
221da0102a1SMichael Halcrow int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
222da0102a1SMichael Halcrow 			struct inode *ecryptfs_inode)
223da0102a1SMichael Halcrow {
224da0102a1SMichael Halcrow 	struct ecryptfs_inode_info *inode_info =
225da0102a1SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode);
226da0102a1SMichael Halcrow 	ssize_t octets_read;
227da0102a1SMichael Halcrow 	mm_segment_t fs_save;
228da0102a1SMichael Halcrow 	size_t i;
229da0102a1SMichael Halcrow 	int rc = 0;
230da0102a1SMichael Halcrow 
231da0102a1SMichael Halcrow 	mutex_lock(&inode_info->lower_file_mutex);
232da0102a1SMichael Halcrow 	BUG_ON(!inode_info->lower_file);
233da0102a1SMichael Halcrow 	inode_info->lower_file->f_pos = offset;
234da0102a1SMichael Halcrow 	fs_save = get_fs();
235da0102a1SMichael Halcrow 	set_fs(get_ds());
236da0102a1SMichael Halcrow 	octets_read = vfs_read(inode_info->lower_file, data, size,
237da0102a1SMichael Halcrow 			       &inode_info->lower_file->f_pos);
238da0102a1SMichael Halcrow 	set_fs(fs_save);
239da0102a1SMichael Halcrow 	if (octets_read < 0) {
240da0102a1SMichael Halcrow 		printk(KERN_ERR "%s: octets_read = [%td]; "
241da0102a1SMichael Halcrow 		       "expected [%td]\n", __FUNCTION__, octets_read, size);
242da0102a1SMichael Halcrow 		rc = -EINVAL;
243da0102a1SMichael Halcrow 	}
244da0102a1SMichael Halcrow 	mutex_unlock(&inode_info->lower_file_mutex);
245da0102a1SMichael Halcrow 	for (i = 0; i < size; i += PAGE_CACHE_SIZE) {
246da0102a1SMichael Halcrow 		struct page *data_page;
247da0102a1SMichael Halcrow 
248da0102a1SMichael Halcrow 		data_page = virt_to_page(data + i);
249da0102a1SMichael Halcrow 		flush_dcache_page(data_page);
250da0102a1SMichael Halcrow 		if (rc)
251da0102a1SMichael Halcrow 			ClearPageUptodate(data_page);
252da0102a1SMichael Halcrow 		else
253da0102a1SMichael Halcrow 			SetPageUptodate(data_page);
254da0102a1SMichael Halcrow 	}
255da0102a1SMichael Halcrow 	return rc;
256da0102a1SMichael Halcrow }
257da0102a1SMichael Halcrow 
258da0102a1SMichael Halcrow /**
259da0102a1SMichael Halcrow  * ecryptfs_read_lower_page_segment
260da0102a1SMichael Halcrow  * @page_for_ecryptfs: The page into which data for eCryptfs will be
261da0102a1SMichael Halcrow  *                     written
262da0102a1SMichael Halcrow  * @offset_in_page: Offset in @page_for_ecryptfs from which to start
263da0102a1SMichael Halcrow  *                  writing
264da0102a1SMichael Halcrow  * @size: The number of bytes to write into @page_for_ecryptfs
265da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
266da0102a1SMichael Halcrow  *
267da0102a1SMichael Halcrow  * Determines the byte offset in the file for the given page and
268da0102a1SMichael Halcrow  * offset within the page, maps the page, and makes the call to read
269da0102a1SMichael Halcrow  * the contents of @page_for_ecryptfs from the lower inode.
270da0102a1SMichael Halcrow  *
271da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
272da0102a1SMichael Halcrow  */
273da0102a1SMichael Halcrow int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
274da0102a1SMichael Halcrow 				     pgoff_t page_index,
275da0102a1SMichael Halcrow 				     size_t offset_in_page, size_t size,
276da0102a1SMichael Halcrow 				     struct inode *ecryptfs_inode)
277da0102a1SMichael Halcrow {
278da0102a1SMichael Halcrow 	char *virt;
279da0102a1SMichael Halcrow 	loff_t offset;
280da0102a1SMichael Halcrow 	int rc;
281da0102a1SMichael Halcrow 
282*d6a13c17SMichael Halcrow 	offset = ((((loff_t)page_index) << PAGE_CACHE_SHIFT) + offset_in_page);
283da0102a1SMichael Halcrow 	virt = kmap(page_for_ecryptfs);
284da0102a1SMichael Halcrow 	rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
285da0102a1SMichael Halcrow 	kunmap(page_for_ecryptfs);
286da0102a1SMichael Halcrow 	return rc;
287da0102a1SMichael Halcrow }
288da0102a1SMichael Halcrow 
289da0102a1SMichael Halcrow /**
290da0102a1SMichael Halcrow  * ecryptfs_read
291da0102a1SMichael Halcrow  * @data: The virtual address into which to write the data read (and
292da0102a1SMichael Halcrow  *        possibly decrypted) from the lower file
293da0102a1SMichael Halcrow  * @offset: The offset in the decrypted view of the file from which to
294da0102a1SMichael Halcrow  *          read into @data
295da0102a1SMichael Halcrow  * @size: The number of bytes to read into @data
296da0102a1SMichael Halcrow  * @ecryptfs_file: The eCryptfs file from which to read
297da0102a1SMichael Halcrow  *
298da0102a1SMichael Halcrow  * Read an arbitrary amount of data from an arbitrary location in the
299da0102a1SMichael Halcrow  * eCryptfs page cache. This is done on an extent-by-extent basis;
300da0102a1SMichael Halcrow  * individual extents are decrypted and read from the lower page
301da0102a1SMichael Halcrow  * cache (via VFS reads). This function takes care of all the
302da0102a1SMichael Halcrow  * address translation to locations in the lower filesystem.
303da0102a1SMichael Halcrow  *
304da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
305da0102a1SMichael Halcrow  */
306da0102a1SMichael Halcrow int ecryptfs_read(char *data, loff_t offset, size_t size,
307da0102a1SMichael Halcrow 		  struct file *ecryptfs_file)
308da0102a1SMichael Halcrow {
309da0102a1SMichael Halcrow 	struct page *ecryptfs_page;
310da0102a1SMichael Halcrow 	char *ecryptfs_page_virt;
311*d6a13c17SMichael Halcrow 	loff_t ecryptfs_file_size =
312*d6a13c17SMichael Halcrow 		i_size_read(ecryptfs_file->f_dentry->d_inode);
313da0102a1SMichael Halcrow 	loff_t data_offset = 0;
314da0102a1SMichael Halcrow 	loff_t pos;
315da0102a1SMichael Halcrow 	int rc = 0;
316da0102a1SMichael Halcrow 
317da0102a1SMichael Halcrow 	if ((offset + size) > ecryptfs_file_size) {
318da0102a1SMichael Halcrow 		rc = -EINVAL;
319da0102a1SMichael Halcrow 		printk(KERN_ERR "%s: Attempt to read data past the end of the "
320da0102a1SMichael Halcrow 			"file; offset = [%lld]; size = [%td]; "
321da0102a1SMichael Halcrow 		       "ecryptfs_file_size = [%lld]\n",
322da0102a1SMichael Halcrow 		       __FUNCTION__, offset, size, ecryptfs_file_size);
323da0102a1SMichael Halcrow 		goto out;
324da0102a1SMichael Halcrow 	}
325da0102a1SMichael Halcrow 	pos = offset;
326da0102a1SMichael Halcrow 	while (pos < (offset + size)) {
327da0102a1SMichael Halcrow 		pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
328da0102a1SMichael Halcrow 		size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
329da0102a1SMichael Halcrow 		size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
330da0102a1SMichael Halcrow 		size_t total_remaining_bytes = ((offset + size) - pos);
331da0102a1SMichael Halcrow 
332da0102a1SMichael Halcrow 		if (num_bytes > total_remaining_bytes)
333da0102a1SMichael Halcrow 			num_bytes = total_remaining_bytes;
334da0102a1SMichael Halcrow 		ecryptfs_page = ecryptfs_get1page(ecryptfs_file,
335da0102a1SMichael Halcrow 						  ecryptfs_page_idx);
336da0102a1SMichael Halcrow 		if (IS_ERR(ecryptfs_page)) {
337da0102a1SMichael Halcrow 			rc = PTR_ERR(ecryptfs_page);
338da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error getting page at "
339da0102a1SMichael Halcrow 			       "index [%ld] from eCryptfs inode "
340da0102a1SMichael Halcrow 			       "mapping; rc = [%d]\n", __FUNCTION__,
341da0102a1SMichael Halcrow 			       ecryptfs_page_idx, rc);
342da0102a1SMichael Halcrow 			goto out;
343da0102a1SMichael Halcrow 		}
3440216f7f7SMichael Halcrow 		rc = ecryptfs_decrypt_page(ecryptfs_page);
345da0102a1SMichael Halcrow 		if (rc) {
346da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error decrypting "
347da0102a1SMichael Halcrow 			       "page; rc = [%d]\n", __FUNCTION__, rc);
348da0102a1SMichael Halcrow 			page_cache_release(ecryptfs_page);
349da0102a1SMichael Halcrow 			goto out;
350da0102a1SMichael Halcrow 		}
351da0102a1SMichael Halcrow 		ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
352da0102a1SMichael Halcrow 		memcpy((data + data_offset),
353da0102a1SMichael Halcrow 		       ((char *)ecryptfs_page_virt + start_offset_in_page),
354da0102a1SMichael Halcrow 		       num_bytes);
355da0102a1SMichael Halcrow 		kunmap_atomic(ecryptfs_page_virt, KM_USER0);
356da0102a1SMichael Halcrow 		page_cache_release(ecryptfs_page);
357da0102a1SMichael Halcrow 		pos += num_bytes;
358da0102a1SMichael Halcrow 		data_offset += num_bytes;
359da0102a1SMichael Halcrow 	}
360da0102a1SMichael Halcrow out:
361da0102a1SMichael Halcrow 	return rc;
362da0102a1SMichael Halcrow }
363