ocfs2: ocfs2_write_begin_nolock() should handle len=0
[linux-2.6/mini2440.git] / fs / ecryptfs / read_write.c
bloba137c6ea2feec65181741ad1531bd35b6bfe0012
1 /**
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
20 * 02111-1307, USA.
23 #include <linux/fs.h>
24 #include <linux/pagemap.h>
25 #include "ecryptfs_kernel.h"
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
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;
44 mm_segment_t fs_save;
45 int rc = 0;
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;
51 fs_save = get_fs();
52 set_fs(get_ds());
53 octets_written = vfs_write(inode_info->lower_file, data, size,
54 &inode_info->lower_file->f_pos);
55 set_fs(fs_save);
56 if (octets_written < 0) {
57 printk(KERN_ERR "%s: octets_written = [%td]; "
58 "expected [%td]\n", __func__, octets_written, size);
59 rc = -EINVAL;
61 mutex_unlock(&inode_info->lower_file_mutex);
62 mark_inode_dirty_sync(ecryptfs_inode);
63 return rc;
66 /**
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
70 * lower file
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
74 * lower file
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)
86 char *virt;
87 loff_t offset;
88 int rc;
90 offset = ((((loff_t)page_for_lower->index) << PAGE_CACHE_SHIFT)
91 + offset_in_page);
92 virt = kmap(page_for_lower);
93 rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
94 kunmap(page_for_lower);
95 return rc;
98 /**
99 * ecryptfs_write
100 * @ecryptfs_file: The eCryptfs file into which to write
101 * @data: Virtual address where data to write is located
102 * @offset: Offset in the eCryptfs file at which to begin writing the
103 * data from @data
104 * @size: The number of bytes to write from @data
106 * Write an arbitrary amount of data to an arbitrary location in the
107 * eCryptfs inode page cache. This is done on a page-by-page, and then
108 * by an extent-by-extent, basis; individual extents are encrypted and
109 * written to the lower page cache (via VFS writes). This function
110 * takes care of all the address translation to locations in the lower
111 * filesystem; it also handles truncate events, writing out zeros
112 * where necessary.
114 * Returns zero on success; non-zero otherwise
116 int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
117 size_t size)
119 struct page *ecryptfs_page;
120 struct ecryptfs_crypt_stat *crypt_stat;
121 struct inode *ecryptfs_inode = ecryptfs_file->f_dentry->d_inode;
122 char *ecryptfs_page_virt;
123 loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
124 loff_t data_offset = 0;
125 loff_t pos;
126 int rc = 0;
128 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
130 * if we are writing beyond current size, then start pos
131 * at the current size - we'll fill in zeros from there.
133 if (offset > ecryptfs_file_size)
134 pos = ecryptfs_file_size;
135 else
136 pos = offset;
137 while (pos < (offset + size)) {
138 pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
139 size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
140 size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
141 size_t total_remaining_bytes = ((offset + size) - pos);
143 if (num_bytes > total_remaining_bytes)
144 num_bytes = total_remaining_bytes;
145 if (pos < offset) {
146 /* remaining zeros to write, up to destination offset */
147 size_t total_remaining_zeros = (offset - pos);
149 if (num_bytes > total_remaining_zeros)
150 num_bytes = total_remaining_zeros;
152 ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file,
153 ecryptfs_page_idx);
154 if (IS_ERR(ecryptfs_page)) {
155 rc = PTR_ERR(ecryptfs_page);
156 printk(KERN_ERR "%s: Error getting page at "
157 "index [%ld] from eCryptfs inode "
158 "mapping; rc = [%d]\n", __func__,
159 ecryptfs_page_idx, rc);
160 goto out;
162 ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
165 * pos: where we're now writing, offset: where the request was
166 * If current pos is before request, we are filling zeros
167 * If we are at or beyond request, we are writing the *data*
168 * If we're in a fresh page beyond eof, zero it in either case
170 if (pos < offset || !start_offset_in_page) {
171 /* We are extending past the previous end of the file.
172 * Fill in zero values to the end of the page */
173 memset(((char *)ecryptfs_page_virt
174 + start_offset_in_page), 0,
175 PAGE_CACHE_SIZE - start_offset_in_page);
178 /* pos >= offset, we are now writing the data request */
179 if (pos >= offset) {
180 memcpy(((char *)ecryptfs_page_virt
181 + start_offset_in_page),
182 (data + data_offset), num_bytes);
183 data_offset += num_bytes;
185 kunmap_atomic(ecryptfs_page_virt, KM_USER0);
186 flush_dcache_page(ecryptfs_page);
187 SetPageUptodate(ecryptfs_page);
188 unlock_page(ecryptfs_page);
189 if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
190 rc = ecryptfs_encrypt_page(ecryptfs_page);
191 else
192 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
193 ecryptfs_page,
194 start_offset_in_page,
195 data_offset);
196 page_cache_release(ecryptfs_page);
197 if (rc) {
198 printk(KERN_ERR "%s: Error encrypting "
199 "page; rc = [%d]\n", __func__, rc);
200 goto out;
202 pos += num_bytes;
204 if ((offset + size) > ecryptfs_file_size) {
205 i_size_write(ecryptfs_inode, (offset + size));
206 if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
207 rc = ecryptfs_write_inode_size_to_metadata(
208 ecryptfs_inode);
209 if (rc) {
210 printk(KERN_ERR "Problem with "
211 "ecryptfs_write_inode_size_to_metadata; "
212 "rc = [%d]\n", rc);
213 goto out;
217 out:
218 return rc;
222 * ecryptfs_read_lower
223 * @data: The read data is stored here by this function
224 * @offset: Byte offset in the lower file from which to read the data
225 * @size: Number of bytes to read from @offset of the lower file and
226 * store into @data
227 * @ecryptfs_inode: The eCryptfs inode
229 * Read @size bytes of data at byte offset @offset from the lower
230 * inode into memory location @data.
232 * Returns zero on success; non-zero on error
234 int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
235 struct inode *ecryptfs_inode)
237 struct ecryptfs_inode_info *inode_info =
238 ecryptfs_inode_to_private(ecryptfs_inode);
239 ssize_t octets_read;
240 mm_segment_t fs_save;
241 int rc = 0;
243 mutex_lock(&inode_info->lower_file_mutex);
244 BUG_ON(!inode_info->lower_file);
245 inode_info->lower_file->f_pos = offset;
246 fs_save = get_fs();
247 set_fs(get_ds());
248 octets_read = vfs_read(inode_info->lower_file, data, size,
249 &inode_info->lower_file->f_pos);
250 set_fs(fs_save);
251 if (octets_read < 0) {
252 printk(KERN_ERR "%s: octets_read = [%td]; "
253 "expected [%td]\n", __func__, octets_read, size);
254 rc = -EINVAL;
256 mutex_unlock(&inode_info->lower_file_mutex);
257 return rc;
261 * ecryptfs_read_lower_page_segment
262 * @page_for_ecryptfs: The page into which data for eCryptfs will be
263 * written
264 * @offset_in_page: Offset in @page_for_ecryptfs from which to start
265 * writing
266 * @size: The number of bytes to write into @page_for_ecryptfs
267 * @ecryptfs_inode: The eCryptfs inode
269 * Determines the byte offset in the file for the given page and
270 * offset within the page, maps the page, and makes the call to read
271 * the contents of @page_for_ecryptfs from the lower inode.
273 * Returns zero on success; non-zero otherwise
275 int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
276 pgoff_t page_index,
277 size_t offset_in_page, size_t size,
278 struct inode *ecryptfs_inode)
280 char *virt;
281 loff_t offset;
282 int rc;
284 offset = ((((loff_t)page_index) << PAGE_CACHE_SHIFT) + offset_in_page);
285 virt = kmap(page_for_ecryptfs);
286 rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
287 kunmap(page_for_ecryptfs);
288 flush_dcache_page(page_for_ecryptfs);
289 return rc;
292 #if 0
294 * ecryptfs_read
295 * @data: The virtual address into which to write the data read (and
296 * possibly decrypted) from the lower file
297 * @offset: The offset in the decrypted view of the file from which to
298 * read into @data
299 * @size: The number of bytes to read into @data
300 * @ecryptfs_file: The eCryptfs file from which to read
302 * Read an arbitrary amount of data from an arbitrary location in the
303 * eCryptfs page cache. This is done on an extent-by-extent basis;
304 * individual extents are decrypted and read from the lower page
305 * cache (via VFS reads). This function takes care of all the
306 * address translation to locations in the lower filesystem.
308 * Returns zero on success; non-zero otherwise
310 int ecryptfs_read(char *data, loff_t offset, size_t size,
311 struct file *ecryptfs_file)
313 struct page *ecryptfs_page;
314 char *ecryptfs_page_virt;
315 loff_t ecryptfs_file_size =
316 i_size_read(ecryptfs_file->f_dentry->d_inode);
317 loff_t data_offset = 0;
318 loff_t pos;
319 int rc = 0;
321 if ((offset + size) > ecryptfs_file_size) {
322 rc = -EINVAL;
323 printk(KERN_ERR "%s: Attempt to read data past the end of the "
324 "file; offset = [%lld]; size = [%td]; "
325 "ecryptfs_file_size = [%lld]\n",
326 __func__, offset, size, ecryptfs_file_size);
327 goto out;
329 pos = offset;
330 while (pos < (offset + size)) {
331 pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
332 size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
333 size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
334 size_t total_remaining_bytes = ((offset + size) - pos);
336 if (num_bytes > total_remaining_bytes)
337 num_bytes = total_remaining_bytes;
338 ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file,
339 ecryptfs_page_idx);
340 if (IS_ERR(ecryptfs_page)) {
341 rc = PTR_ERR(ecryptfs_page);
342 printk(KERN_ERR "%s: Error getting page at "
343 "index [%ld] from eCryptfs inode "
344 "mapping; rc = [%d]\n", __func__,
345 ecryptfs_page_idx, rc);
346 goto out;
348 ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
349 memcpy((data + data_offset),
350 ((char *)ecryptfs_page_virt + start_offset_in_page),
351 num_bytes);
352 kunmap_atomic(ecryptfs_page_virt, KM_USER0);
353 flush_dcache_page(ecryptfs_page);
354 SetPageUptodate(ecryptfs_page);
355 unlock_page(ecryptfs_page);
356 page_cache_release(ecryptfs_page);
357 pos += num_bytes;
358 data_offset += num_bytes;
360 out:
361 return rc;
363 #endif /* 0 */