xref: /linux/fs/ecryptfs/read_write.c (revision 09cbfeaf1a5a67bfb3201e0c83c810cecb2efa5a)
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  *
3796a7b9c2STyler Hicks  * Returns bytes written on success; less than 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 {
42f61500e0STyler Hicks 	struct file *lower_file;
4396a7b9c2STyler Hicks 	ssize_t rc;
44da0102a1SMichael Halcrow 
45f61500e0STyler Hicks 	lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
46f61500e0STyler Hicks 	if (!lower_file)
47f61500e0STyler Hicks 		return -EIO;
487bb307e8SAl Viro 	rc = kernel_write(lower_file, data, size, offset);
49da0102a1SMichael Halcrow 	mark_inode_dirty_sync(ecryptfs_inode);
50da0102a1SMichael Halcrow 	return rc;
51da0102a1SMichael Halcrow }
52da0102a1SMichael Halcrow 
53da0102a1SMichael Halcrow /**
54da0102a1SMichael Halcrow  * ecryptfs_write_lower_page_segment
55da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
56da0102a1SMichael Halcrow  * @page_for_lower: The page containing the data to be written to the
57da0102a1SMichael Halcrow  *                  lower file
58da0102a1SMichael Halcrow  * @offset_in_page: The offset in the @page_for_lower from which to
59da0102a1SMichael Halcrow  *                  start writing the data
60da0102a1SMichael Halcrow  * @size: The amount of data from @page_for_lower to write to the
61da0102a1SMichael Halcrow  *        lower file
62da0102a1SMichael Halcrow  *
63da0102a1SMichael Halcrow  * Determines the byte offset in the file for the given page and
64da0102a1SMichael Halcrow  * offset within the page, maps the page, and makes the call to write
65da0102a1SMichael Halcrow  * the contents of @page_for_lower to the lower inode.
66da0102a1SMichael Halcrow  *
67da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
68da0102a1SMichael Halcrow  */
69da0102a1SMichael Halcrow int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
70da0102a1SMichael Halcrow 				      struct page *page_for_lower,
71da0102a1SMichael Halcrow 				      size_t offset_in_page, size_t size)
72da0102a1SMichael Halcrow {
73da0102a1SMichael Halcrow 	char *virt;
74da0102a1SMichael Halcrow 	loff_t offset;
75da0102a1SMichael Halcrow 	int rc;
76da0102a1SMichael Halcrow 
77*09cbfeafSKirill A. Shutemov 	offset = ((((loff_t)page_for_lower->index) << PAGE_SHIFT)
78d6a13c17SMichael Halcrow 		  + offset_in_page);
79da0102a1SMichael Halcrow 	virt = kmap(page_for_lower);
80da0102a1SMichael Halcrow 	rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
8196a7b9c2STyler Hicks 	if (rc > 0)
8296a7b9c2STyler Hicks 		rc = 0;
83da0102a1SMichael Halcrow 	kunmap(page_for_lower);
84da0102a1SMichael Halcrow 	return rc;
85da0102a1SMichael Halcrow }
86da0102a1SMichael Halcrow 
87da0102a1SMichael Halcrow /**
88da0102a1SMichael Halcrow  * ecryptfs_write
8948c1e44aSAl Viro  * @ecryptfs_inode: The eCryptfs file into which to write
90da0102a1SMichael Halcrow  * @data: Virtual address where data to write is located
91da0102a1SMichael Halcrow  * @offset: Offset in the eCryptfs file at which to begin writing the
92da0102a1SMichael Halcrow  *          data from @data
93da0102a1SMichael Halcrow  * @size: The number of bytes to write from @data
94da0102a1SMichael Halcrow  *
95da0102a1SMichael Halcrow  * Write an arbitrary amount of data to an arbitrary location in the
96da0102a1SMichael Halcrow  * eCryptfs inode page cache. This is done on a page-by-page, and then
97da0102a1SMichael Halcrow  * by an extent-by-extent, basis; individual extents are encrypted and
98da0102a1SMichael Halcrow  * written to the lower page cache (via VFS writes). This function
99da0102a1SMichael Halcrow  * takes care of all the address translation to locations in the lower
100da0102a1SMichael Halcrow  * filesystem; it also handles truncate events, writing out zeros
101da0102a1SMichael Halcrow  * where necessary.
102da0102a1SMichael Halcrow  *
103da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
104da0102a1SMichael Halcrow  */
10548c1e44aSAl Viro int ecryptfs_write(struct inode *ecryptfs_inode, char *data, loff_t offset,
106da0102a1SMichael Halcrow 		   size_t size)
107da0102a1SMichael Halcrow {
108da0102a1SMichael Halcrow 	struct page *ecryptfs_page;
10913a791b4STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
110da0102a1SMichael Halcrow 	char *ecryptfs_page_virt;
11113a791b4STyler Hicks 	loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
112da0102a1SMichael Halcrow 	loff_t data_offset = 0;
113da0102a1SMichael Halcrow 	loff_t pos;
114da0102a1SMichael Halcrow 	int rc = 0;
115da0102a1SMichael Halcrow 
11613a791b4STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1177a3f595cSEric Sandeen 	/*
1187a3f595cSEric Sandeen 	 * if we are writing beyond current size, then start pos
1197a3f595cSEric Sandeen 	 * at the current size - we'll fill in zeros from there.
1207a3f595cSEric Sandeen 	 */
121da0102a1SMichael Halcrow 	if (offset > ecryptfs_file_size)
122da0102a1SMichael Halcrow 		pos = ecryptfs_file_size;
123da0102a1SMichael Halcrow 	else
124da0102a1SMichael Halcrow 		pos = offset;
125da0102a1SMichael Halcrow 	while (pos < (offset + size)) {
126*09cbfeafSKirill A. Shutemov 		pgoff_t ecryptfs_page_idx = (pos >> PAGE_SHIFT);
127*09cbfeafSKirill A. Shutemov 		size_t start_offset_in_page = (pos & ~PAGE_MASK);
128*09cbfeafSKirill A. Shutemov 		size_t num_bytes = (PAGE_SIZE - start_offset_in_page);
129684a3ff7SLi Wang 		loff_t total_remaining_bytes = ((offset + size) - pos);
130da0102a1SMichael Halcrow 
1315e6f0d76STyler Hicks 		if (fatal_signal_pending(current)) {
1325e6f0d76STyler Hicks 			rc = -EINTR;
1335e6f0d76STyler Hicks 			break;
1345e6f0d76STyler Hicks 		}
1355e6f0d76STyler Hicks 
136da0102a1SMichael Halcrow 		if (num_bytes > total_remaining_bytes)
137da0102a1SMichael Halcrow 			num_bytes = total_remaining_bytes;
138da0102a1SMichael Halcrow 		if (pos < offset) {
1397a3f595cSEric Sandeen 			/* remaining zeros to write, up to destination offset */
140684a3ff7SLi Wang 			loff_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 		}
14502bd9799SAl Viro 		ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_inode,
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 "
15118d1dbf1SHarvey Harrison 			       "mapping; rc = [%d]\n", __func__,
152da0102a1SMichael Halcrow 			       ecryptfs_page_idx, rc);
153da0102a1SMichael Halcrow 			goto out;
154da0102a1SMichael Halcrow 		}
155465c9343SCong Wang 		ecryptfs_page_virt = kmap_atomic(ecryptfs_page);
1567a3f595cSEric Sandeen 
1577a3f595cSEric Sandeen 		/*
1587a3f595cSEric Sandeen 		 * pos: where we're now writing, offset: where the request was
1597a3f595cSEric Sandeen 		 * If current pos is before request, we are filling zeros
1607a3f595cSEric Sandeen 		 * If we are at or beyond request, we are writing the *data*
1617a3f595cSEric Sandeen 		 * If we're in a fresh page beyond eof, zero it in either case
1627a3f595cSEric Sandeen 		 */
1637a3f595cSEric Sandeen 		if (pos < offset || !start_offset_in_page) {
1647a3f595cSEric Sandeen 			/* We are extending past the previous end of the file.
1657a3f595cSEric Sandeen 			 * Fill in zero values to the end of the page */
1667a3f595cSEric Sandeen 			memset(((char *)ecryptfs_page_virt
1677a3f595cSEric Sandeen 				+ start_offset_in_page), 0,
168*09cbfeafSKirill A. Shutemov 				PAGE_SIZE - start_offset_in_page);
1697a3f595cSEric Sandeen 		}
1707a3f595cSEric Sandeen 
1717a3f595cSEric Sandeen 		/* pos >= offset, we are now writing the data request */
172da0102a1SMichael Halcrow 		if (pos >= offset) {
173da0102a1SMichael Halcrow 			memcpy(((char *)ecryptfs_page_virt
174da0102a1SMichael Halcrow 				+ start_offset_in_page),
175da0102a1SMichael Halcrow 			       (data + data_offset), num_bytes);
176da0102a1SMichael Halcrow 			data_offset += num_bytes;
177da0102a1SMichael Halcrow 		}
178465c9343SCong Wang 		kunmap_atomic(ecryptfs_page_virt);
179da0102a1SMichael Halcrow 		flush_dcache_page(ecryptfs_page);
18016a72c45SMichael Halcrow 		SetPageUptodate(ecryptfs_page);
18116a72c45SMichael Halcrow 		unlock_page(ecryptfs_page);
18213a791b4STyler Hicks 		if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
1830216f7f7SMichael Halcrow 			rc = ecryptfs_encrypt_page(ecryptfs_page);
18413a791b4STyler Hicks 		else
18513a791b4STyler Hicks 			rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
18613a791b4STyler Hicks 						ecryptfs_page,
18713a791b4STyler Hicks 						start_offset_in_page,
18813a791b4STyler Hicks 						data_offset);
189*09cbfeafSKirill A. Shutemov 		put_page(ecryptfs_page);
190da0102a1SMichael Halcrow 		if (rc) {
191da0102a1SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting "
19218d1dbf1SHarvey Harrison 			       "page; rc = [%d]\n", __func__, rc);
193da0102a1SMichael Halcrow 			goto out;
194da0102a1SMichael Halcrow 		}
195da0102a1SMichael Halcrow 		pos += num_bytes;
196da0102a1SMichael Halcrow 	}
1975e6f0d76STyler Hicks 	if (pos > ecryptfs_file_size) {
1985e6f0d76STyler Hicks 		i_size_write(ecryptfs_inode, pos);
19913a791b4STyler Hicks 		if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
2005e6f0d76STyler Hicks 			int rc2;
2015e6f0d76STyler Hicks 
2025e6f0d76STyler Hicks 			rc2 = ecryptfs_write_inode_size_to_metadata(
20313a791b4STyler Hicks 								ecryptfs_inode);
2045e6f0d76STyler Hicks 			if (rc2) {
205da0102a1SMichael Halcrow 				printk(KERN_ERR	"Problem with "
206da0102a1SMichael Halcrow 				       "ecryptfs_write_inode_size_to_metadata; "
2075e6f0d76STyler Hicks 				       "rc = [%d]\n", rc2);
2085e6f0d76STyler Hicks 				if (!rc)
2095e6f0d76STyler Hicks 					rc = rc2;
210da0102a1SMichael Halcrow 				goto out;
211da0102a1SMichael Halcrow 			}
212da0102a1SMichael Halcrow 		}
21313a791b4STyler Hicks 	}
214da0102a1SMichael Halcrow out:
215da0102a1SMichael Halcrow 	return rc;
216da0102a1SMichael Halcrow }
217da0102a1SMichael Halcrow 
218da0102a1SMichael Halcrow /**
219da0102a1SMichael Halcrow  * ecryptfs_read_lower
220da0102a1SMichael Halcrow  * @data: The read data is stored here by this function
221da0102a1SMichael Halcrow  * @offset: Byte offset in the lower file from which to read the data
222da0102a1SMichael Halcrow  * @size: Number of bytes to read from @offset of the lower file and
223da0102a1SMichael Halcrow  *        store into @data
224da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
225da0102a1SMichael Halcrow  *
226da0102a1SMichael Halcrow  * Read @size bytes of data at byte offset @offset from the lower
227da0102a1SMichael Halcrow  * inode into memory location @data.
228da0102a1SMichael Halcrow  *
22996a7b9c2STyler Hicks  * Returns bytes read on success; 0 on EOF; less than zero on error
230da0102a1SMichael Halcrow  */
231da0102a1SMichael Halcrow int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
232da0102a1SMichael Halcrow 			struct inode *ecryptfs_inode)
233da0102a1SMichael Halcrow {
234f61500e0STyler Hicks 	struct file *lower_file;
235f61500e0STyler Hicks 	lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
236f61500e0STyler Hicks 	if (!lower_file)
237f61500e0STyler Hicks 		return -EIO;
23839dfe6c6SAl Viro 	return kernel_read(lower_file, offset, data, size);
239da0102a1SMichael Halcrow }
240da0102a1SMichael Halcrow 
241da0102a1SMichael Halcrow /**
242da0102a1SMichael Halcrow  * ecryptfs_read_lower_page_segment
243da0102a1SMichael Halcrow  * @page_for_ecryptfs: The page into which data for eCryptfs will be
244da0102a1SMichael Halcrow  *                     written
245da0102a1SMichael Halcrow  * @offset_in_page: Offset in @page_for_ecryptfs from which to start
246da0102a1SMichael Halcrow  *                  writing
247da0102a1SMichael Halcrow  * @size: The number of bytes to write into @page_for_ecryptfs
248da0102a1SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
249da0102a1SMichael Halcrow  *
250da0102a1SMichael Halcrow  * Determines the byte offset in the file for the given page and
251da0102a1SMichael Halcrow  * offset within the page, maps the page, and makes the call to read
252da0102a1SMichael Halcrow  * the contents of @page_for_ecryptfs from the lower inode.
253da0102a1SMichael Halcrow  *
254da0102a1SMichael Halcrow  * Returns zero on success; non-zero otherwise
255da0102a1SMichael Halcrow  */
256da0102a1SMichael Halcrow int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
257da0102a1SMichael Halcrow 				     pgoff_t page_index,
258da0102a1SMichael Halcrow 				     size_t offset_in_page, size_t size,
259da0102a1SMichael Halcrow 				     struct inode *ecryptfs_inode)
260da0102a1SMichael Halcrow {
261da0102a1SMichael Halcrow 	char *virt;
262da0102a1SMichael Halcrow 	loff_t offset;
263da0102a1SMichael Halcrow 	int rc;
264da0102a1SMichael Halcrow 
265*09cbfeafSKirill A. Shutemov 	offset = ((((loff_t)page_index) << PAGE_SHIFT) + offset_in_page);
266da0102a1SMichael Halcrow 	virt = kmap(page_for_ecryptfs);
267da0102a1SMichael Halcrow 	rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
26896a7b9c2STyler Hicks 	if (rc > 0)
26996a7b9c2STyler Hicks 		rc = 0;
270da0102a1SMichael Halcrow 	kunmap(page_for_ecryptfs);
27116a72c45SMichael Halcrow 	flush_dcache_page(page_for_ecryptfs);
272da0102a1SMichael Halcrow 	return rc;
273da0102a1SMichael Halcrow }
274