xref: /linux/fs/ecryptfs/read_write.c (revision 13a791b4e63eb0537a7f804a340d6527485983b4)
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]; "
5818d1dbf1SHarvey Harrison 		       "expected [%td]\n", __func__, 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;
120*13a791b4STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
121*13a791b4STyler Hicks 	struct inode *ecryptfs_inode = ecryptfs_file->f_dentry->d_inode;
122da0102a1SMichael Halcrow 	char *ecryptfs_page_virt;
123*13a791b4STyler Hicks 	loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
124da0102a1SMichael Halcrow 	loff_t data_offset = 0;
125da0102a1SMichael Halcrow 	loff_t pos;
126da0102a1SMichael Halcrow 	int rc = 0;
127da0102a1SMichael Halcrow 
128*13a791b4STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1297a3f595cSEric Sandeen 	/*
1307a3f595cSEric Sandeen 	 * if we are writing beyond current size, then start pos
1317a3f595cSEric Sandeen 	 * at the current size - we'll fill in zeros from there.
1327a3f595cSEric Sandeen 	 */
133da0102a1SMichael Halcrow 	if (offset > ecryptfs_file_size)
134da0102a1SMichael Halcrow 		pos = ecryptfs_file_size;
135da0102a1SMichael Halcrow 	else
136da0102a1SMichael Halcrow 		pos = offset;
137da0102a1SMichael Halcrow 	while (pos < (offset + size)) {
138da0102a1SMichael Halcrow 		pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
139da0102a1SMichael Halcrow 		size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
140da0102a1SMichael Halcrow 		size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
141da0102a1SMichael Halcrow 		size_t total_remaining_bytes = ((offset + size) - pos);
142da0102a1SMichael Halcrow 
143da0102a1SMichael Halcrow 		if (num_bytes > total_remaining_bytes)
144da0102a1SMichael Halcrow 			num_bytes = total_remaining_bytes;
145da0102a1SMichael Halcrow 		if (pos < offset) {
1467a3f595cSEric Sandeen 			/* remaining zeros to write, up to destination offset */
147da0102a1SMichael Halcrow 			size_t total_remaining_zeros = (offset - pos);
148da0102a1SMichael Halcrow 
149da0102a1SMichael Halcrow 			if (num_bytes > total_remaining_zeros)
150da0102a1SMichael Halcrow 				num_bytes = total_remaining_zeros;
151da0102a1SMichael Halcrow 		}
15216a72c45SMichael Halcrow 		ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file,
153da0102a1SMichael Halcrow 							 ecryptfs_page_idx);
154da0102a1SMichael Halcrow 		if (IS_ERR(ecryptfs_page)) {
155da0102a1SMichael Halcrow 			rc = PTR_ERR(ecryptfs_page);
156da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error getting page at "
157da0102a1SMichael Halcrow 			       "index [%ld] from eCryptfs inode "
15818d1dbf1SHarvey Harrison 			       "mapping; rc = [%d]\n", __func__,
159da0102a1SMichael Halcrow 			       ecryptfs_page_idx, rc);
160da0102a1SMichael Halcrow 			goto out;
161da0102a1SMichael Halcrow 		}
162da0102a1SMichael Halcrow 		ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
1637a3f595cSEric Sandeen 
1647a3f595cSEric Sandeen 		/*
1657a3f595cSEric Sandeen 		 * pos: where we're now writing, offset: where the request was
1667a3f595cSEric Sandeen 		 * If current pos is before request, we are filling zeros
1677a3f595cSEric Sandeen 		 * If we are at or beyond request, we are writing the *data*
1687a3f595cSEric Sandeen 		 * If we're in a fresh page beyond eof, zero it in either case
1697a3f595cSEric Sandeen 		 */
1707a3f595cSEric Sandeen 		if (pos < offset || !start_offset_in_page) {
1717a3f595cSEric Sandeen 			/* We are extending past the previous end of the file.
1727a3f595cSEric Sandeen 			 * Fill in zero values to the end of the page */
1737a3f595cSEric Sandeen 			memset(((char *)ecryptfs_page_virt
1747a3f595cSEric Sandeen 				+ start_offset_in_page), 0,
1757a3f595cSEric Sandeen 				PAGE_CACHE_SIZE - start_offset_in_page);
1767a3f595cSEric Sandeen 		}
1777a3f595cSEric Sandeen 
1787a3f595cSEric Sandeen 		/* pos >= offset, we are now writing the data request */
179da0102a1SMichael Halcrow 		if (pos >= offset) {
180da0102a1SMichael Halcrow 			memcpy(((char *)ecryptfs_page_virt
181da0102a1SMichael Halcrow 				+ start_offset_in_page),
182da0102a1SMichael Halcrow 			       (data + data_offset), num_bytes);
183da0102a1SMichael Halcrow 			data_offset += num_bytes;
184da0102a1SMichael Halcrow 		}
185da0102a1SMichael Halcrow 		kunmap_atomic(ecryptfs_page_virt, KM_USER0);
186da0102a1SMichael Halcrow 		flush_dcache_page(ecryptfs_page);
18716a72c45SMichael Halcrow 		SetPageUptodate(ecryptfs_page);
18816a72c45SMichael Halcrow 		unlock_page(ecryptfs_page);
189*13a791b4STyler Hicks 		if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
1900216f7f7SMichael Halcrow 			rc = ecryptfs_encrypt_page(ecryptfs_page);
191*13a791b4STyler Hicks 		else
192*13a791b4STyler Hicks 			rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
193*13a791b4STyler Hicks 						ecryptfs_page,
194*13a791b4STyler Hicks 						start_offset_in_page,
195*13a791b4STyler Hicks 						data_offset);
19616a72c45SMichael Halcrow 		page_cache_release(ecryptfs_page);
197da0102a1SMichael Halcrow 		if (rc) {
198da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting "
19918d1dbf1SHarvey Harrison 			       "page; rc = [%d]\n", __func__, rc);
200da0102a1SMichael Halcrow 			goto out;
201da0102a1SMichael Halcrow 		}
202da0102a1SMichael Halcrow 		pos += num_bytes;
203da0102a1SMichael Halcrow 	}
204da0102a1SMichael Halcrow 	if ((offset + size) > ecryptfs_file_size) {
205*13a791b4STyler Hicks 		i_size_write(ecryptfs_inode, (offset + size));
206*13a791b4STyler Hicks 		if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
2070216f7f7SMichael Halcrow 			rc = ecryptfs_write_inode_size_to_metadata(
208*13a791b4STyler Hicks 								ecryptfs_inode);
209da0102a1SMichael Halcrow 			if (rc) {
210da0102a1SMichael Halcrow 				printk(KERN_ERR	"Problem with "
211da0102a1SMichael Halcrow 				       "ecryptfs_write_inode_size_to_metadata; "
212da0102a1SMichael Halcrow 				       "rc = [%d]\n", rc);
213da0102a1SMichael Halcrow 				goto out;
214da0102a1SMichael Halcrow 			}
215da0102a1SMichael Halcrow 		}
216*13a791b4STyler Hicks 	}
217da0102a1SMichael Halcrow out:
218da0102a1SMichael Halcrow 	return rc;
219da0102a1SMichael Halcrow }
220da0102a1SMichael Halcrow 
221da0102a1SMichael Halcrow /**
222da0102a1SMichael Halcrow  * ecryptfs_read_lower
223da0102a1SMichael Halcrow  * @data: The read data is stored here by this function
224da0102a1SMichael Halcrow  * @offset: Byte offset in the lower file from which to read the data
225da0102a1SMichael Halcrow  * @size: Number of bytes to read from @offset of the lower file and
226da0102a1SMichael Halcrow  *        store into @data
227da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
228da0102a1SMichael Halcrow  *
229da0102a1SMichael Halcrow  * Read @size bytes of data at byte offset @offset from the lower
230da0102a1SMichael Halcrow  * inode into memory location @data.
231da0102a1SMichael Halcrow  *
232da0102a1SMichael Halcrow  * Returns zero on success; non-zero on error
233da0102a1SMichael Halcrow  */
234da0102a1SMichael Halcrow int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
235da0102a1SMichael Halcrow 			struct inode *ecryptfs_inode)
236da0102a1SMichael Halcrow {
237da0102a1SMichael Halcrow 	struct ecryptfs_inode_info *inode_info =
238da0102a1SMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode);
239da0102a1SMichael Halcrow 	ssize_t octets_read;
240da0102a1SMichael Halcrow 	mm_segment_t fs_save;
241da0102a1SMichael Halcrow 	int rc = 0;
242da0102a1SMichael Halcrow 
243da0102a1SMichael Halcrow 	mutex_lock(&inode_info->lower_file_mutex);
244da0102a1SMichael Halcrow 	BUG_ON(!inode_info->lower_file);
245da0102a1SMichael Halcrow 	inode_info->lower_file->f_pos = offset;
246da0102a1SMichael Halcrow 	fs_save = get_fs();
247da0102a1SMichael Halcrow 	set_fs(get_ds());
248da0102a1SMichael Halcrow 	octets_read = vfs_read(inode_info->lower_file, data, size,
249da0102a1SMichael Halcrow 			       &inode_info->lower_file->f_pos);
250da0102a1SMichael Halcrow 	set_fs(fs_save);
251da0102a1SMichael Halcrow 	if (octets_read < 0) {
252da0102a1SMichael Halcrow 		printk(KERN_ERR "%s: octets_read = [%td]; "
25318d1dbf1SHarvey Harrison 		       "expected [%td]\n", __func__, octets_read, size);
254da0102a1SMichael Halcrow 		rc = -EINVAL;
255da0102a1SMichael Halcrow 	}
256da0102a1SMichael Halcrow 	mutex_unlock(&inode_info->lower_file_mutex);
257da0102a1SMichael Halcrow 	return rc;
258da0102a1SMichael Halcrow }
259da0102a1SMichael Halcrow 
260da0102a1SMichael Halcrow /**
261da0102a1SMichael Halcrow  * ecryptfs_read_lower_page_segment
262da0102a1SMichael Halcrow  * @page_for_ecryptfs: The page into which data for eCryptfs will be
263da0102a1SMichael Halcrow  *                     written
264da0102a1SMichael Halcrow  * @offset_in_page: Offset in @page_for_ecryptfs from which to start
265da0102a1SMichael Halcrow  *                  writing
266da0102a1SMichael Halcrow  * @size: The number of bytes to write into @page_for_ecryptfs
267da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
268da0102a1SMichael Halcrow  *
269da0102a1SMichael Halcrow  * Determines the byte offset in the file for the given page and
270da0102a1SMichael Halcrow  * offset within the page, maps the page, and makes the call to read
271da0102a1SMichael Halcrow  * the contents of @page_for_ecryptfs from the lower inode.
272da0102a1SMichael Halcrow  *
273da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
274da0102a1SMichael Halcrow  */
275da0102a1SMichael Halcrow int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
276da0102a1SMichael Halcrow 				     pgoff_t page_index,
277da0102a1SMichael Halcrow 				     size_t offset_in_page, size_t size,
278da0102a1SMichael Halcrow 				     struct inode *ecryptfs_inode)
279da0102a1SMichael Halcrow {
280da0102a1SMichael Halcrow 	char *virt;
281da0102a1SMichael Halcrow 	loff_t offset;
282da0102a1SMichael Halcrow 	int rc;
283da0102a1SMichael Halcrow 
284d6a13c17SMichael Halcrow 	offset = ((((loff_t)page_index) << PAGE_CACHE_SHIFT) + offset_in_page);
285da0102a1SMichael Halcrow 	virt = kmap(page_for_ecryptfs);
286da0102a1SMichael Halcrow 	rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
287da0102a1SMichael Halcrow 	kunmap(page_for_ecryptfs);
28816a72c45SMichael Halcrow 	flush_dcache_page(page_for_ecryptfs);
289da0102a1SMichael Halcrow 	return rc;
290da0102a1SMichael Halcrow }
291da0102a1SMichael Halcrow 
2927896b631SAdrian Bunk #if 0
293da0102a1SMichael Halcrow /**
294da0102a1SMichael Halcrow  * ecryptfs_read
295da0102a1SMichael Halcrow  * @data: The virtual address into which to write the data read (and
296da0102a1SMichael Halcrow  *        possibly decrypted) from the lower file
297da0102a1SMichael Halcrow  * @offset: The offset in the decrypted view of the file from which to
298da0102a1SMichael Halcrow  *          read into @data
299da0102a1SMichael Halcrow  * @size: The number of bytes to read into @data
300da0102a1SMichael Halcrow  * @ecryptfs_file: The eCryptfs file from which to read
301da0102a1SMichael Halcrow  *
302da0102a1SMichael Halcrow  * Read an arbitrary amount of data from an arbitrary location in the
303da0102a1SMichael Halcrow  * eCryptfs page cache. This is done on an extent-by-extent basis;
304da0102a1SMichael Halcrow  * individual extents are decrypted and read from the lower page
305da0102a1SMichael Halcrow  * cache (via VFS reads). This function takes care of all the
306da0102a1SMichael Halcrow  * address translation to locations in the lower filesystem.
307da0102a1SMichael Halcrow  *
308da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
309da0102a1SMichael Halcrow  */
310da0102a1SMichael Halcrow int ecryptfs_read(char *data, loff_t offset, size_t size,
311da0102a1SMichael Halcrow 		  struct file *ecryptfs_file)
312da0102a1SMichael Halcrow {
313da0102a1SMichael Halcrow 	struct page *ecryptfs_page;
314da0102a1SMichael Halcrow 	char *ecryptfs_page_virt;
315d6a13c17SMichael Halcrow 	loff_t ecryptfs_file_size =
316d6a13c17SMichael Halcrow 		i_size_read(ecryptfs_file->f_dentry->d_inode);
317da0102a1SMichael Halcrow 	loff_t data_offset = 0;
318da0102a1SMichael Halcrow 	loff_t pos;
319da0102a1SMichael Halcrow 	int rc = 0;
320da0102a1SMichael Halcrow 
321da0102a1SMichael Halcrow 	if ((offset + size) > ecryptfs_file_size) {
322da0102a1SMichael Halcrow 		rc = -EINVAL;
323da0102a1SMichael Halcrow 		printk(KERN_ERR "%s: Attempt to read data past the end of the "
324da0102a1SMichael Halcrow 			"file; offset = [%lld]; size = [%td]; "
325da0102a1SMichael Halcrow 		       "ecryptfs_file_size = [%lld]\n",
32618d1dbf1SHarvey Harrison 		       __func__, offset, size, ecryptfs_file_size);
327da0102a1SMichael Halcrow 		goto out;
328da0102a1SMichael Halcrow 	}
329da0102a1SMichael Halcrow 	pos = offset;
330da0102a1SMichael Halcrow 	while (pos < (offset + size)) {
331da0102a1SMichael Halcrow 		pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
332da0102a1SMichael Halcrow 		size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
333da0102a1SMichael Halcrow 		size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
334da0102a1SMichael Halcrow 		size_t total_remaining_bytes = ((offset + size) - pos);
335da0102a1SMichael Halcrow 
336da0102a1SMichael Halcrow 		if (num_bytes > total_remaining_bytes)
337da0102a1SMichael Halcrow 			num_bytes = total_remaining_bytes;
33816a72c45SMichael Halcrow 		ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file,
339da0102a1SMichael Halcrow 							 ecryptfs_page_idx);
340da0102a1SMichael Halcrow 		if (IS_ERR(ecryptfs_page)) {
341da0102a1SMichael Halcrow 			rc = PTR_ERR(ecryptfs_page);
342da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error getting page at "
343da0102a1SMichael Halcrow 			       "index [%ld] from eCryptfs inode "
34418d1dbf1SHarvey Harrison 			       "mapping; rc = [%d]\n", __func__,
345da0102a1SMichael Halcrow 			       ecryptfs_page_idx, rc);
346da0102a1SMichael Halcrow 			goto out;
347da0102a1SMichael Halcrow 		}
348da0102a1SMichael Halcrow 		ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
349da0102a1SMichael Halcrow 		memcpy((data + data_offset),
350da0102a1SMichael Halcrow 		       ((char *)ecryptfs_page_virt + start_offset_in_page),
351da0102a1SMichael Halcrow 		       num_bytes);
352da0102a1SMichael Halcrow 		kunmap_atomic(ecryptfs_page_virt, KM_USER0);
35316a72c45SMichael Halcrow 		flush_dcache_page(ecryptfs_page);
35416a72c45SMichael Halcrow 		SetPageUptodate(ecryptfs_page);
35516a72c45SMichael Halcrow 		unlock_page(ecryptfs_page);
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 }
3637896b631SAdrian Bunk #endif  /*  0  */
364