xref: /linux/fs/ecryptfs/read_write.c (revision 7a3f595cc8298df14a7c71b0d876bafd8e9e1cbf)
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 
908a146a2bSMichael Halcrow 	offset = ((((loff_t)page_for_lower->index) << PAGE_CACHE_SHIFT)
91d6a13c17SMichael 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;
121d6a13c17SMichael Halcrow 	loff_t ecryptfs_file_size =
122d6a13c17SMichael 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 
127*7a3f595cSEric Sandeen 	/*
128*7a3f595cSEric Sandeen 	 * if we are writing beyond current size, then start pos
129*7a3f595cSEric Sandeen 	 * at the current size - we'll fill in zeros from there.
130*7a3f595cSEric Sandeen 	 */
131da0102a1SMichael Halcrow 	if (offset > ecryptfs_file_size)
132da0102a1SMichael Halcrow 		pos = ecryptfs_file_size;
133da0102a1SMichael Halcrow 	else
134da0102a1SMichael Halcrow 		pos = offset;
135da0102a1SMichael Halcrow 	while (pos < (offset + size)) {
136da0102a1SMichael Halcrow 		pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
137da0102a1SMichael Halcrow 		size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
138da0102a1SMichael Halcrow 		size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
139da0102a1SMichael Halcrow 		size_t total_remaining_bytes = ((offset + size) - pos);
140da0102a1SMichael Halcrow 
141da0102a1SMichael Halcrow 		if (num_bytes > total_remaining_bytes)
142da0102a1SMichael Halcrow 			num_bytes = total_remaining_bytes;
143da0102a1SMichael Halcrow 		if (pos < offset) {
144*7a3f595cSEric Sandeen 			/* remaining zeros to write, up to destination offset */
145da0102a1SMichael Halcrow 			size_t total_remaining_zeros = (offset - pos);
146da0102a1SMichael Halcrow 
147da0102a1SMichael Halcrow 			if (num_bytes > total_remaining_zeros)
148da0102a1SMichael Halcrow 				num_bytes = total_remaining_zeros;
149da0102a1SMichael Halcrow 		}
15016a72c45SMichael Halcrow 		ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file,
151da0102a1SMichael Halcrow 							 ecryptfs_page_idx);
152da0102a1SMichael Halcrow 		if (IS_ERR(ecryptfs_page)) {
153da0102a1SMichael Halcrow 			rc = PTR_ERR(ecryptfs_page);
154da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error getting page at "
155da0102a1SMichael Halcrow 			       "index [%ld] from eCryptfs inode "
156da0102a1SMichael Halcrow 			       "mapping; rc = [%d]\n", __FUNCTION__,
157da0102a1SMichael Halcrow 			       ecryptfs_page_idx, rc);
158da0102a1SMichael Halcrow 			goto out;
159da0102a1SMichael Halcrow 		}
160da0102a1SMichael Halcrow 		if (start_offset_in_page) {
161da0102a1SMichael Halcrow 			/* Read in the page from the lower
162da0102a1SMichael Halcrow 			 * into the eCryptfs inode page cache,
163da0102a1SMichael Halcrow 			 * decrypting */
1640216f7f7SMichael Halcrow 			rc = ecryptfs_decrypt_page(ecryptfs_page);
1650216f7f7SMichael Halcrow 			if (rc) {
166da0102a1SMichael Halcrow 				printk(KERN_ERR "%s: Error decrypting "
167da0102a1SMichael Halcrow 				       "page; rc = [%d]\n",
168da0102a1SMichael Halcrow 				       __FUNCTION__, rc);
16916a72c45SMichael Halcrow 				ClearPageUptodate(ecryptfs_page);
170da0102a1SMichael Halcrow 				page_cache_release(ecryptfs_page);
171da0102a1SMichael Halcrow 				goto out;
172da0102a1SMichael Halcrow 			}
173da0102a1SMichael Halcrow 		}
174da0102a1SMichael Halcrow 		ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
175*7a3f595cSEric Sandeen 
176*7a3f595cSEric Sandeen 		/*
177*7a3f595cSEric Sandeen 		 * pos: where we're now writing, offset: where the request was
178*7a3f595cSEric Sandeen 		 * If current pos is before request, we are filling zeros
179*7a3f595cSEric Sandeen 		 * If we are at or beyond request, we are writing the *data*
180*7a3f595cSEric Sandeen 		 * If we're in a fresh page beyond eof, zero it in either case
181*7a3f595cSEric Sandeen 		 */
182*7a3f595cSEric Sandeen 		if (pos < offset || !start_offset_in_page) {
183*7a3f595cSEric Sandeen 			/* We are extending past the previous end of the file.
184*7a3f595cSEric Sandeen 			 * Fill in zero values to the end of the page */
185*7a3f595cSEric Sandeen 			memset(((char *)ecryptfs_page_virt
186*7a3f595cSEric Sandeen 				+ start_offset_in_page), 0,
187*7a3f595cSEric Sandeen 				PAGE_CACHE_SIZE - start_offset_in_page);
188*7a3f595cSEric Sandeen 		}
189*7a3f595cSEric Sandeen 
190*7a3f595cSEric Sandeen 		/* pos >= offset, we are now writing the data request */
191da0102a1SMichael Halcrow 		if (pos >= offset) {
192da0102a1SMichael Halcrow 			memcpy(((char *)ecryptfs_page_virt
193da0102a1SMichael Halcrow 				+ start_offset_in_page),
194da0102a1SMichael Halcrow 			       (data + data_offset), num_bytes);
195da0102a1SMichael Halcrow 			data_offset += num_bytes;
196da0102a1SMichael Halcrow 		}
197da0102a1SMichael Halcrow 		kunmap_atomic(ecryptfs_page_virt, KM_USER0);
198da0102a1SMichael Halcrow 		flush_dcache_page(ecryptfs_page);
19916a72c45SMichael Halcrow 		SetPageUptodate(ecryptfs_page);
20016a72c45SMichael Halcrow 		unlock_page(ecryptfs_page);
2010216f7f7SMichael Halcrow 		rc = ecryptfs_encrypt_page(ecryptfs_page);
20216a72c45SMichael Halcrow 		page_cache_release(ecryptfs_page);
203da0102a1SMichael Halcrow 		if (rc) {
204da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting "
205da0102a1SMichael Halcrow 			       "page; rc = [%d]\n", __FUNCTION__, rc);
206da0102a1SMichael Halcrow 			goto out;
207da0102a1SMichael Halcrow 		}
208da0102a1SMichael Halcrow 		pos += num_bytes;
209da0102a1SMichael Halcrow 	}
210da0102a1SMichael Halcrow 	if ((offset + size) > ecryptfs_file_size) {
211da0102a1SMichael Halcrow 		i_size_write(ecryptfs_file->f_dentry->d_inode, (offset + size));
2120216f7f7SMichael Halcrow 		rc = ecryptfs_write_inode_size_to_metadata(
2130216f7f7SMichael Halcrow 			ecryptfs_file->f_dentry->d_inode);
214da0102a1SMichael Halcrow 		if (rc) {
215da0102a1SMichael Halcrow 			printk(KERN_ERR	"Problem with "
216da0102a1SMichael Halcrow 			       "ecryptfs_write_inode_size_to_metadata; "
217da0102a1SMichael Halcrow 			       "rc = [%d]\n", rc);
218da0102a1SMichael Halcrow 			goto out;
219da0102a1SMichael Halcrow 		}
220da0102a1SMichael Halcrow 	}
221da0102a1SMichael Halcrow out:
222da0102a1SMichael Halcrow 	return rc;
223da0102a1SMichael Halcrow }
224da0102a1SMichael Halcrow 
225da0102a1SMichael Halcrow /**
226da0102a1SMichael Halcrow  * ecryptfs_read_lower
227da0102a1SMichael Halcrow  * @data: The read data is stored here by this function
228da0102a1SMichael Halcrow  * @offset: Byte offset in the lower file from which to read the data
229da0102a1SMichael Halcrow  * @size: Number of bytes to read from @offset of the lower file and
230da0102a1SMichael Halcrow  *        store into @data
231da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
232da0102a1SMichael Halcrow  *
233da0102a1SMichael Halcrow  * Read @size bytes of data at byte offset @offset from the lower
234da0102a1SMichael Halcrow  * inode into memory location @data.
235da0102a1SMichael Halcrow  *
236da0102a1SMichael Halcrow  * Returns zero on success; non-zero on error
237da0102a1SMichael Halcrow  */
238da0102a1SMichael Halcrow int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
239da0102a1SMichael Halcrow 			struct inode *ecryptfs_inode)
240da0102a1SMichael Halcrow {
241da0102a1SMichael Halcrow 	struct ecryptfs_inode_info *inode_info =
242da0102a1SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode);
243da0102a1SMichael Halcrow 	ssize_t octets_read;
244da0102a1SMichael Halcrow 	mm_segment_t fs_save;
245da0102a1SMichael Halcrow 	int rc = 0;
246da0102a1SMichael Halcrow 
247da0102a1SMichael Halcrow 	mutex_lock(&inode_info->lower_file_mutex);
248da0102a1SMichael Halcrow 	BUG_ON(!inode_info->lower_file);
249da0102a1SMichael Halcrow 	inode_info->lower_file->f_pos = offset;
250da0102a1SMichael Halcrow 	fs_save = get_fs();
251da0102a1SMichael Halcrow 	set_fs(get_ds());
252da0102a1SMichael Halcrow 	octets_read = vfs_read(inode_info->lower_file, data, size,
253da0102a1SMichael Halcrow 			       &inode_info->lower_file->f_pos);
254da0102a1SMichael Halcrow 	set_fs(fs_save);
255da0102a1SMichael Halcrow 	if (octets_read < 0) {
256da0102a1SMichael Halcrow 		printk(KERN_ERR "%s: octets_read = [%td]; "
257da0102a1SMichael Halcrow 		       "expected [%td]\n", __FUNCTION__, octets_read, size);
258da0102a1SMichael Halcrow 		rc = -EINVAL;
259da0102a1SMichael Halcrow 	}
260da0102a1SMichael Halcrow 	mutex_unlock(&inode_info->lower_file_mutex);
261da0102a1SMichael Halcrow 	return rc;
262da0102a1SMichael Halcrow }
263da0102a1SMichael Halcrow 
264da0102a1SMichael Halcrow /**
265da0102a1SMichael Halcrow  * ecryptfs_read_lower_page_segment
266da0102a1SMichael Halcrow  * @page_for_ecryptfs: The page into which data for eCryptfs will be
267da0102a1SMichael Halcrow  *                     written
268da0102a1SMichael Halcrow  * @offset_in_page: Offset in @page_for_ecryptfs from which to start
269da0102a1SMichael Halcrow  *                  writing
270da0102a1SMichael Halcrow  * @size: The number of bytes to write into @page_for_ecryptfs
271da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
272da0102a1SMichael Halcrow  *
273da0102a1SMichael Halcrow  * Determines the byte offset in the file for the given page and
274da0102a1SMichael Halcrow  * offset within the page, maps the page, and makes the call to read
275da0102a1SMichael Halcrow  * the contents of @page_for_ecryptfs from the lower inode.
276da0102a1SMichael Halcrow  *
277da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
278da0102a1SMichael Halcrow  */
279da0102a1SMichael Halcrow int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
280da0102a1SMichael Halcrow 				     pgoff_t page_index,
281da0102a1SMichael Halcrow 				     size_t offset_in_page, size_t size,
282da0102a1SMichael Halcrow 				     struct inode *ecryptfs_inode)
283da0102a1SMichael Halcrow {
284da0102a1SMichael Halcrow 	char *virt;
285da0102a1SMichael Halcrow 	loff_t offset;
286da0102a1SMichael Halcrow 	int rc;
287da0102a1SMichael Halcrow 
288d6a13c17SMichael Halcrow 	offset = ((((loff_t)page_index) << PAGE_CACHE_SHIFT) + offset_in_page);
289da0102a1SMichael Halcrow 	virt = kmap(page_for_ecryptfs);
290da0102a1SMichael Halcrow 	rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
291da0102a1SMichael Halcrow 	kunmap(page_for_ecryptfs);
29216a72c45SMichael Halcrow 	flush_dcache_page(page_for_ecryptfs);
293da0102a1SMichael Halcrow 	return rc;
294da0102a1SMichael Halcrow }
295da0102a1SMichael Halcrow 
296da0102a1SMichael Halcrow /**
297da0102a1SMichael Halcrow  * ecryptfs_read
298da0102a1SMichael Halcrow  * @data: The virtual address into which to write the data read (and
299da0102a1SMichael Halcrow  *        possibly decrypted) from the lower file
300da0102a1SMichael Halcrow  * @offset: The offset in the decrypted view of the file from which to
301da0102a1SMichael Halcrow  *          read into @data
302da0102a1SMichael Halcrow  * @size: The number of bytes to read into @data
303da0102a1SMichael Halcrow  * @ecryptfs_file: The eCryptfs file from which to read
304da0102a1SMichael Halcrow  *
305da0102a1SMichael Halcrow  * Read an arbitrary amount of data from an arbitrary location in the
306da0102a1SMichael Halcrow  * eCryptfs page cache. This is done on an extent-by-extent basis;
307da0102a1SMichael Halcrow  * individual extents are decrypted and read from the lower page
308da0102a1SMichael Halcrow  * cache (via VFS reads). This function takes care of all the
309da0102a1SMichael Halcrow  * address translation to locations in the lower filesystem.
310da0102a1SMichael Halcrow  *
311da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
312da0102a1SMichael Halcrow  */
313da0102a1SMichael Halcrow int ecryptfs_read(char *data, loff_t offset, size_t size,
314da0102a1SMichael Halcrow 		  struct file *ecryptfs_file)
315da0102a1SMichael Halcrow {
316da0102a1SMichael Halcrow 	struct page *ecryptfs_page;
317da0102a1SMichael Halcrow 	char *ecryptfs_page_virt;
318d6a13c17SMichael Halcrow 	loff_t ecryptfs_file_size =
319d6a13c17SMichael Halcrow 		i_size_read(ecryptfs_file->f_dentry->d_inode);
320da0102a1SMichael Halcrow 	loff_t data_offset = 0;
321da0102a1SMichael Halcrow 	loff_t pos;
322da0102a1SMichael Halcrow 	int rc = 0;
323da0102a1SMichael Halcrow 
324da0102a1SMichael Halcrow 	if ((offset + size) > ecryptfs_file_size) {
325da0102a1SMichael Halcrow 		rc = -EINVAL;
326da0102a1SMichael Halcrow 		printk(KERN_ERR "%s: Attempt to read data past the end of the "
327da0102a1SMichael Halcrow 			"file; offset = [%lld]; size = [%td]; "
328da0102a1SMichael Halcrow 		       "ecryptfs_file_size = [%lld]\n",
329da0102a1SMichael Halcrow 		       __FUNCTION__, offset, size, ecryptfs_file_size);
330da0102a1SMichael Halcrow 		goto out;
331da0102a1SMichael Halcrow 	}
332da0102a1SMichael Halcrow 	pos = offset;
333da0102a1SMichael Halcrow 	while (pos < (offset + size)) {
334da0102a1SMichael Halcrow 		pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
335da0102a1SMichael Halcrow 		size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
336da0102a1SMichael Halcrow 		size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
337da0102a1SMichael Halcrow 		size_t total_remaining_bytes = ((offset + size) - pos);
338da0102a1SMichael Halcrow 
339da0102a1SMichael Halcrow 		if (num_bytes > total_remaining_bytes)
340da0102a1SMichael Halcrow 			num_bytes = total_remaining_bytes;
34116a72c45SMichael Halcrow 		ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file,
342da0102a1SMichael Halcrow 							 ecryptfs_page_idx);
343da0102a1SMichael Halcrow 		if (IS_ERR(ecryptfs_page)) {
344da0102a1SMichael Halcrow 			rc = PTR_ERR(ecryptfs_page);
345da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error getting page at "
346da0102a1SMichael Halcrow 			       "index [%ld] from eCryptfs inode "
347da0102a1SMichael Halcrow 			       "mapping; rc = [%d]\n", __FUNCTION__,
348da0102a1SMichael Halcrow 			       ecryptfs_page_idx, rc);
349da0102a1SMichael Halcrow 			goto out;
350da0102a1SMichael Halcrow 		}
3510216f7f7SMichael Halcrow 		rc = ecryptfs_decrypt_page(ecryptfs_page);
352da0102a1SMichael Halcrow 		if (rc) {
353da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error decrypting "
354da0102a1SMichael Halcrow 			       "page; rc = [%d]\n", __FUNCTION__, rc);
35516a72c45SMichael Halcrow 			ClearPageUptodate(ecryptfs_page);
356da0102a1SMichael Halcrow 			page_cache_release(ecryptfs_page);
357da0102a1SMichael Halcrow 			goto out;
358da0102a1SMichael Halcrow 		}
359da0102a1SMichael Halcrow 		ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
360da0102a1SMichael Halcrow 		memcpy((data + data_offset),
361da0102a1SMichael Halcrow 		       ((char *)ecryptfs_page_virt + start_offset_in_page),
362da0102a1SMichael Halcrow 		       num_bytes);
363da0102a1SMichael Halcrow 		kunmap_atomic(ecryptfs_page_virt, KM_USER0);
36416a72c45SMichael Halcrow 		flush_dcache_page(ecryptfs_page);
36516a72c45SMichael Halcrow 		SetPageUptodate(ecryptfs_page);
36616a72c45SMichael Halcrow 		unlock_page(ecryptfs_page);
367da0102a1SMichael Halcrow 		page_cache_release(ecryptfs_page);
368da0102a1SMichael Halcrow 		pos += num_bytes;
369da0102a1SMichael Halcrow 		data_offset += num_bytes;
370da0102a1SMichael Halcrow 	}
371da0102a1SMichael Halcrow out:
372da0102a1SMichael Halcrow 	return rc;
373da0102a1SMichael Halcrow }
374