2 * eCryptfs: Linux filesystem encryption layer
4 * Copyright (C) 2007 International Business Machines Corp.
5 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
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.
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.
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
24 #include <linux/pagemap.h>
25 #include "ecryptfs_kernel.h"
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
35 * Write data to the lower file.
37 * Returns zero on success; non-zero on error
39 int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
40 loff_t offset, size_t size)
42 struct ecryptfs_inode_info *inode_info;
43 ssize_t octets_written;
47 inode_info = ecryptfs_inode_to_private(ecryptfs_inode);
48 mutex_lock(&inode_info->lower_file_mutex);
49 BUG_ON(!inode_info->lower_file);
50 inode_info->lower_file->f_pos = offset;
53 octets_written = vfs_write(inode_info->lower_file, data, size,
54 &inode_info->lower_file->f_pos);
56 if (octets_written < 0) {
57 printk(KERN_ERR "%s: octets_written = [%td]; "
58 "expected [%td]\n", __FUNCTION__, octets_written, size);
61 mutex_unlock(&inode_info->lower_file_mutex);
62 mark_inode_dirty_sync(ecryptfs_inode);
67 * ecryptfs_write_lower_page_segment
68 * @ecryptfs_inode: The eCryptfs inode
69 * @page_for_lower: The page containing the data to be written to the
71 * @offset_in_page: The offset in the @page_for_lower from which to
72 * start writing the data
73 * @size: The amount of data from @page_for_lower to write to the
76 * Determines the byte offset in the file for the given page and
77 * offset within the page, maps the page, and makes the call to write
78 * the contents of @page_for_lower to the lower inode.
80 * Returns zero on success; non-zero otherwise
82 int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
83 struct page *page_for_lower,
84 size_t offset_in_page, size_t size)
90 offset = (page_for_lower->index << PAGE_CACHE_SHIFT) + offset_in_page;
91 virt = kmap(page_for_lower);
92 rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
93 kunmap(page_for_lower);
99 * @ecryptfs_file: The eCryptfs file into which to write
100 * @data: Virtual address where data to write is located
101 * @offset: Offset in the eCryptfs file at which to begin writing the
103 * @size: The number of bytes to write from @data
105 * Write an arbitrary amount of data to an arbitrary location in the
106 * eCryptfs inode page cache. This is done on a page-by-page, and then
107 * by an extent-by-extent, basis; individual extents are encrypted and
108 * written to the lower page cache (via VFS writes). This function
109 * takes care of all the address translation to locations in the lower
110 * filesystem; it also handles truncate events, writing out zeros
113 * Returns zero on success; non-zero otherwise
115 int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
118 struct page *ecryptfs_page;
119 char *ecryptfs_page_virt;
120 u64 ecryptfs_file_size = i_size_read(ecryptfs_file->f_dentry->d_inode);
121 loff_t data_offset = 0;
125 if (offset > ecryptfs_file_size)
126 pos = ecryptfs_file_size;
129 while (pos < (offset + size)) {
130 pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
131 size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
132 size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
133 size_t total_remaining_bytes = ((offset + size) - pos);
135 if (num_bytes > total_remaining_bytes)
136 num_bytes = total_remaining_bytes;
138 size_t total_remaining_zeros = (offset - pos);
140 if (num_bytes > total_remaining_zeros)
141 num_bytes = total_remaining_zeros;
143 ecryptfs_page = ecryptfs_get1page(ecryptfs_file,
145 if (IS_ERR(ecryptfs_page)) {
146 rc = PTR_ERR(ecryptfs_page);
147 printk(KERN_ERR "%s: Error getting page at "
148 "index [%ld] from eCryptfs inode "
149 "mapping; rc = [%d]\n", __FUNCTION__,
150 ecryptfs_page_idx, rc);
153 if (start_offset_in_page) {
154 /* Read in the page from the lower
155 * into the eCryptfs inode page cache,
157 if ((rc = ecryptfs_decrypt_page(NULL, /* placeholder for git-bisect */
159 printk(KERN_ERR "%s: Error decrypting "
162 page_cache_release(ecryptfs_page);
166 ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
168 memcpy(((char *)ecryptfs_page_virt
169 + start_offset_in_page),
170 (data + data_offset), num_bytes);
171 data_offset += num_bytes;
173 /* We are extending past the previous end of the file.
174 * Fill in zero values up to the start of where we
175 * will be writing data. */
176 memset(((char *)ecryptfs_page_virt
177 + start_offset_in_page), 0, num_bytes);
179 kunmap_atomic(ecryptfs_page_virt, KM_USER0);
180 flush_dcache_page(ecryptfs_page);
181 rc = ecryptfs_encrypt_page(NULL /* placeholder for git-bisect */);
183 printk(KERN_ERR "%s: Error encrypting "
184 "page; rc = [%d]\n", __FUNCTION__, rc);
185 page_cache_release(ecryptfs_page);
188 page_cache_release(ecryptfs_page);
191 if ((offset + size) > ecryptfs_file_size) {
192 i_size_write(ecryptfs_file->f_dentry->d_inode, (offset + size));
193 rc = ecryptfs_write_inode_size_to_metadata(NULL, NULL, NULL,
194 NULL, 0); /* placeholders for git-bisect */
196 printk(KERN_ERR "Problem with "
197 "ecryptfs_write_inode_size_to_metadata; "
207 * ecryptfs_read_lower
208 * @data: The read data is stored here by this function
209 * @offset: Byte offset in the lower file from which to read the data
210 * @size: Number of bytes to read from @offset of the lower file and
212 * @ecryptfs_inode: The eCryptfs inode
214 * Read @size bytes of data at byte offset @offset from the lower
215 * inode into memory location @data.
217 * Returns zero on success; non-zero on error
219 int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
220 struct inode *ecryptfs_inode)
222 struct ecryptfs_inode_info *inode_info =
223 ecryptfs_inode_to_private(ecryptfs_inode);
225 mm_segment_t fs_save;
229 mutex_lock(&inode_info->lower_file_mutex);
230 BUG_ON(!inode_info->lower_file);
231 inode_info->lower_file->f_pos = offset;
234 octets_read = vfs_read(inode_info->lower_file, data, size,
235 &inode_info->lower_file->f_pos);
237 if (octets_read < 0) {
238 printk(KERN_ERR "%s: octets_read = [%td]; "
239 "expected [%td]\n", __FUNCTION__, octets_read, size);
242 mutex_unlock(&inode_info->lower_file_mutex);
243 for (i = 0; i < size; i += PAGE_CACHE_SIZE) {
244 struct page *data_page;
246 data_page = virt_to_page(data + i);
247 flush_dcache_page(data_page);
249 ClearPageUptodate(data_page);
251 SetPageUptodate(data_page);
257 * ecryptfs_read_lower_page_segment
258 * @page_for_ecryptfs: The page into which data for eCryptfs will be
260 * @offset_in_page: Offset in @page_for_ecryptfs from which to start
262 * @size: The number of bytes to write into @page_for_ecryptfs
263 * @ecryptfs_inode: The eCryptfs inode
265 * Determines the byte offset in the file for the given page and
266 * offset within the page, maps the page, and makes the call to read
267 * the contents of @page_for_ecryptfs from the lower inode.
269 * Returns zero on success; non-zero otherwise
271 int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
273 size_t offset_in_page, size_t size,
274 struct inode *ecryptfs_inode)
280 offset = ((page_index << PAGE_CACHE_SHIFT) + offset_in_page);
281 virt = kmap(page_for_ecryptfs);
282 rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
283 kunmap(page_for_ecryptfs);
289 * @data: The virtual address into which to write the data read (and
290 * possibly decrypted) from the lower file
291 * @offset: The offset in the decrypted view of the file from which to
293 * @size: The number of bytes to read into @data
294 * @ecryptfs_file: The eCryptfs file from which to read
296 * Read an arbitrary amount of data from an arbitrary location in the
297 * eCryptfs page cache. This is done on an extent-by-extent basis;
298 * individual extents are decrypted and read from the lower page
299 * cache (via VFS reads). This function takes care of all the
300 * address translation to locations in the lower filesystem.
302 * Returns zero on success; non-zero otherwise
304 int ecryptfs_read(char *data, loff_t offset, size_t size,
305 struct file *ecryptfs_file)
307 struct page *ecryptfs_page;
308 char *ecryptfs_page_virt;
309 u64 ecryptfs_file_size = i_size_read(ecryptfs_file->f_dentry->d_inode);
310 loff_t data_offset = 0;
314 if ((offset + size) > ecryptfs_file_size) {
316 printk(KERN_ERR "%s: Attempt to read data past the end of the "
317 "file; offset = [%lld]; size = [%td]; "
318 "ecryptfs_file_size = [%lld]\n",
319 __FUNCTION__, offset, size, ecryptfs_file_size);
323 while (pos < (offset + size)) {
324 pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
325 size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
326 size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
327 size_t total_remaining_bytes = ((offset + size) - pos);
329 if (num_bytes > total_remaining_bytes)
330 num_bytes = total_remaining_bytes;
331 ecryptfs_page = ecryptfs_get1page(ecryptfs_file,
333 if (IS_ERR(ecryptfs_page)) {
334 rc = PTR_ERR(ecryptfs_page);
335 printk(KERN_ERR "%s: Error getting page at "
336 "index [%ld] from eCryptfs inode "
337 "mapping; rc = [%d]\n", __FUNCTION__,
338 ecryptfs_page_idx, rc);
341 rc = ecryptfs_decrypt_page(NULL /* placeholder for git-bisect */, ecryptfs_page);
343 printk(KERN_ERR "%s: Error decrypting "
344 "page; rc = [%d]\n", __FUNCTION__, rc);
345 page_cache_release(ecryptfs_page);
348 ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
349 memcpy((data + data_offset),
350 ((char *)ecryptfs_page_virt + start_offset_in_page),
352 kunmap_atomic(ecryptfs_page_virt, KM_USER0);
353 page_cache_release(ecryptfs_page);
355 data_offset += num_bytes;