1/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 2007 International Business Machines Corp.
5 *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include "ecryptfs_kernel.h"
26
27/**
28 * ecryptfs_write_lower
29 * @ecryptfs_inode: The eCryptfs inode
30 * @data: Data to write
31 * @offset: Byte offset in the lower file to which to write the data
32 * @size: Number of bytes from @data to write at @offset in the lower
33 *        file
34 *
35 * Write data to the lower file.
36 *
37 * Returns bytes written on success; less than zero on error
38 */
39int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
40			 loff_t offset, size_t size)
41{
42	struct file *lower_file;
43	ssize_t rc;
44
45	lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
46	if (!lower_file)
47		return -EIO;
48	rc = kernel_write(lower_file, data, size, offset);
49	mark_inode_dirty_sync(ecryptfs_inode);
50	return rc;
51}
52
53/**
54 * ecryptfs_write_lower_page_segment
55 * @ecryptfs_inode: The eCryptfs inode
56 * @page_for_lower: The page containing the data to be written to the
57 *                  lower file
58 * @offset_in_page: The offset in the @page_for_lower from which to
59 *                  start writing the data
60 * @size: The amount of data from @page_for_lower to write to the
61 *        lower file
62 *
63 * Determines the byte offset in the file for the given page and
64 * offset within the page, maps the page, and makes the call to write
65 * the contents of @page_for_lower to the lower inode.
66 *
67 * Returns zero on success; non-zero otherwise
68 */
69int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
70				      struct page *page_for_lower,
71				      size_t offset_in_page, size_t size)
72{
73	char *virt;
74	loff_t offset;
75	int rc;
76
77	offset = ((((loff_t)page_for_lower->index) << PAGE_CACHE_SHIFT)
78		  + offset_in_page);
79	virt = kmap(page_for_lower);
80	rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
81	if (rc > 0)
82		rc = 0;
83	kunmap(page_for_lower);
84	return rc;
85}
86
87/**
88 * ecryptfs_write
89 * @ecryptfs_inode: The eCryptfs file into which to write
90 * @data: Virtual address where data to write is located
91 * @offset: Offset in the eCryptfs file at which to begin writing the
92 *          data from @data
93 * @size: The number of bytes to write from @data
94 *
95 * Write an arbitrary amount of data to an arbitrary location in the
96 * eCryptfs inode page cache. This is done on a page-by-page, and then
97 * by an extent-by-extent, basis; individual extents are encrypted and
98 * written to the lower page cache (via VFS writes). This function
99 * takes care of all the address translation to locations in the lower
100 * filesystem; it also handles truncate events, writing out zeros
101 * where necessary.
102 *
103 * Returns zero on success; non-zero otherwise
104 */
105int ecryptfs_write(struct inode *ecryptfs_inode, char *data, loff_t offset,
106		   size_t size)
107{
108	struct page *ecryptfs_page;
109	struct ecryptfs_crypt_stat *crypt_stat;
110	char *ecryptfs_page_virt;
111	loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
112	loff_t data_offset = 0;
113	loff_t pos;
114	int rc = 0;
115
116	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
117	/*
118	 * if we are writing beyond current size, then start pos
119	 * at the current size - we'll fill in zeros from there.
120	 */
121	if (offset > ecryptfs_file_size)
122		pos = ecryptfs_file_size;
123	else
124		pos = offset;
125	while (pos < (offset + size)) {
126		pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
127		size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
128		size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
129		loff_t total_remaining_bytes = ((offset + size) - pos);
130
131		if (fatal_signal_pending(current)) {
132			rc = -EINTR;
133			break;
134		}
135
136		if (num_bytes > total_remaining_bytes)
137			num_bytes = total_remaining_bytes;
138		if (pos < offset) {
139			/* remaining zeros to write, up to destination offset */
140			loff_t total_remaining_zeros = (offset - pos);
141
142			if (num_bytes > total_remaining_zeros)
143				num_bytes = total_remaining_zeros;
144		}
145		ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_inode,
146							 ecryptfs_page_idx);
147		if (IS_ERR(ecryptfs_page)) {
148			rc = PTR_ERR(ecryptfs_page);
149			printk(KERN_ERR "%s: Error getting page at "
150			       "index [%ld] from eCryptfs inode "
151			       "mapping; rc = [%d]\n", __func__,
152			       ecryptfs_page_idx, rc);
153			goto out;
154		}
155		ecryptfs_page_virt = kmap_atomic(ecryptfs_page);
156
157		/*
158		 * pos: where we're now writing, offset: where the request was
159		 * If current pos is before request, we are filling zeros
160		 * If we are at or beyond request, we are writing the *data*
161		 * If we're in a fresh page beyond eof, zero it in either case
162		 */
163		if (pos < offset || !start_offset_in_page) {
164			/* We are extending past the previous end of the file.
165			 * Fill in zero values to the end of the page */
166			memset(((char *)ecryptfs_page_virt
167				+ start_offset_in_page), 0,
168				PAGE_CACHE_SIZE - start_offset_in_page);
169		}
170
171		/* pos >= offset, we are now writing the data request */
172		if (pos >= offset) {
173			memcpy(((char *)ecryptfs_page_virt
174				+ start_offset_in_page),
175			       (data + data_offset), num_bytes);
176			data_offset += num_bytes;
177		}
178		kunmap_atomic(ecryptfs_page_virt);
179		flush_dcache_page(ecryptfs_page);
180		SetPageUptodate(ecryptfs_page);
181		unlock_page(ecryptfs_page);
182		if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
183			rc = ecryptfs_encrypt_page(ecryptfs_page);
184		else
185			rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
186						ecryptfs_page,
187						start_offset_in_page,
188						data_offset);
189		page_cache_release(ecryptfs_page);
190		if (rc) {
191			printk(KERN_ERR "%s: Error encrypting "
192			       "page; rc = [%d]\n", __func__, rc);
193			goto out;
194		}
195		pos += num_bytes;
196	}
197	if (pos > ecryptfs_file_size) {
198		i_size_write(ecryptfs_inode, pos);
199		if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
200			int rc2;
201
202			rc2 = ecryptfs_write_inode_size_to_metadata(
203								ecryptfs_inode);
204			if (rc2) {
205				printk(KERN_ERR	"Problem with "
206				       "ecryptfs_write_inode_size_to_metadata; "
207				       "rc = [%d]\n", rc2);
208				if (!rc)
209					rc = rc2;
210				goto out;
211			}
212		}
213	}
214out:
215	return rc;
216}
217
218/**
219 * ecryptfs_read_lower
220 * @data: The read data is stored here by this function
221 * @offset: Byte offset in the lower file from which to read the data
222 * @size: Number of bytes to read from @offset of the lower file and
223 *        store into @data
224 * @ecryptfs_inode: The eCryptfs inode
225 *
226 * Read @size bytes of data at byte offset @offset from the lower
227 * inode into memory location @data.
228 *
229 * Returns bytes read on success; 0 on EOF; less than zero on error
230 */
231int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
232			struct inode *ecryptfs_inode)
233{
234	struct file *lower_file;
235	lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
236	if (!lower_file)
237		return -EIO;
238	return kernel_read(lower_file, offset, data, size);
239}
240
241/**
242 * ecryptfs_read_lower_page_segment
243 * @page_for_ecryptfs: The page into which data for eCryptfs will be
244 *                     written
245 * @offset_in_page: Offset in @page_for_ecryptfs from which to start
246 *                  writing
247 * @size: The number of bytes to write into @page_for_ecryptfs
248 * @ecryptfs_inode: The eCryptfs inode
249 *
250 * Determines the byte offset in the file for the given page and
251 * offset within the page, maps the page, and makes the call to read
252 * the contents of @page_for_ecryptfs from the lower inode.
253 *
254 * Returns zero on success; non-zero otherwise
255 */
256int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
257				     pgoff_t page_index,
258				     size_t offset_in_page, size_t size,
259				     struct inode *ecryptfs_inode)
260{
261	char *virt;
262	loff_t offset;
263	int rc;
264
265	offset = ((((loff_t)page_index) << PAGE_CACHE_SHIFT) + offset_in_page);
266	virt = kmap(page_for_ecryptfs);
267	rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
268	if (rc > 0)
269		rc = 0;
270	kunmap(page_for_ecryptfs);
271	flush_dcache_page(page_for_ecryptfs);
272	return rc;
273}
274