Blame view
fs/ecryptfs/read_write.c
11.7 KB
da0102a10 eCryptfs: read_wr... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
/** * eCryptfs: Linux filesystem encryption layer * * Copyright (C) 2007 International Business Machines Corp. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ #include <linux/fs.h> #include <linux/pagemap.h> #include "ecryptfs_kernel.h" /** * ecryptfs_write_lower * @ecryptfs_inode: The eCryptfs inode * @data: Data to write * @offset: Byte offset in the lower file to which to write the data * @size: Number of bytes from @data to write at @offset in the lower * file * * Write data to the lower file. * * Returns zero on success; non-zero on error */ int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data, loff_t offset, size_t size) { struct ecryptfs_inode_info *inode_info; ssize_t octets_written; mm_segment_t fs_save; int rc = 0; inode_info = ecryptfs_inode_to_private(ecryptfs_inode); mutex_lock(&inode_info->lower_file_mutex); BUG_ON(!inode_info->lower_file); inode_info->lower_file->f_pos = offset; fs_save = get_fs(); set_fs(get_ds()); octets_written = vfs_write(inode_info->lower_file, data, size, &inode_info->lower_file->f_pos); set_fs(fs_save); if (octets_written < 0) { printk(KERN_ERR "%s: octets_written = [%td]; " |
18d1dbf1d ecryptfs: replace... |
58 59 |
"expected [%td] ", __func__, octets_written, size); |
da0102a10 eCryptfs: read_wr... |
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
rc = -EINVAL; } mutex_unlock(&inode_info->lower_file_mutex); mark_inode_dirty_sync(ecryptfs_inode); return rc; } /** * ecryptfs_write_lower_page_segment * @ecryptfs_inode: The eCryptfs inode * @page_for_lower: The page containing the data to be written to the * lower file * @offset_in_page: The offset in the @page_for_lower from which to * start writing the data * @size: The amount of data from @page_for_lower to write to the * lower file * * Determines the byte offset in the file for the given page and * offset within the page, maps the page, and makes the call to write * the contents of @page_for_lower to the lower inode. * * Returns zero on success; non-zero otherwise */ int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode, struct page *page_for_lower, size_t offset_in_page, size_t size) { char *virt; loff_t offset; int rc; |
8a146a2b0 eCryptfs: cast pa... |
90 |
offset = ((((loff_t)page_for_lower->index) << PAGE_CACHE_SHIFT) |
d6a13c171 eCryptfs: fix dat... |
91 |
+ offset_in_page); |
da0102a10 eCryptfs: read_wr... |
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
virt = kmap(page_for_lower); rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size); kunmap(page_for_lower); return rc; } /** * ecryptfs_write * @ecryptfs_file: The eCryptfs file into which to write * @data: Virtual address where data to write is located * @offset: Offset in the eCryptfs file at which to begin writing the * data from @data * @size: The number of bytes to write from @data * * Write an arbitrary amount of data to an arbitrary location in the * eCryptfs inode page cache. This is done on a page-by-page, and then * by an extent-by-extent, basis; individual extents are encrypted and * written to the lower page cache (via VFS writes). This function * takes care of all the address translation to locations in the lower * filesystem; it also handles truncate events, writing out zeros * where necessary. * * Returns zero on success; non-zero otherwise */ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset, size_t size) { struct page *ecryptfs_page; |
13a791b4e eCryptfs: Fix dat... |
120 121 |
struct ecryptfs_crypt_stat *crypt_stat; struct inode *ecryptfs_inode = ecryptfs_file->f_dentry->d_inode; |
da0102a10 eCryptfs: read_wr... |
122 |
char *ecryptfs_page_virt; |
13a791b4e eCryptfs: Fix dat... |
123 |
loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode); |
da0102a10 eCryptfs: read_wr... |
124 125 126 |
loff_t data_offset = 0; loff_t pos; int rc = 0; |
13a791b4e eCryptfs: Fix dat... |
127 |
crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; |
7a3f595cc ecryptfs: fix fsx... |
128 129 130 131 |
/* * if we are writing beyond current size, then start pos * at the current size - we'll fill in zeros from there. */ |
da0102a10 eCryptfs: read_wr... |
132 133 134 135 136 137 138 139 140 141 142 143 144 |
if (offset > ecryptfs_file_size) pos = ecryptfs_file_size; else pos = offset; while (pos < (offset + size)) { pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT); size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK); size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page); size_t total_remaining_bytes = ((offset + size) - pos); if (num_bytes > total_remaining_bytes) num_bytes = total_remaining_bytes; if (pos < offset) { |
7a3f595cc ecryptfs: fix fsx... |
145 |
/* remaining zeros to write, up to destination offset */ |
da0102a10 eCryptfs: read_wr... |
146 147 148 149 150 |
size_t total_remaining_zeros = (offset - pos); if (num_bytes > total_remaining_zeros) num_bytes = total_remaining_zeros; } |
16a72c455 ecryptfs: clean u... |
151 152 |
ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file, ecryptfs_page_idx); |
da0102a10 eCryptfs: read_wr... |
153 154 155 156 |
if (IS_ERR(ecryptfs_page)) { rc = PTR_ERR(ecryptfs_page); printk(KERN_ERR "%s: Error getting page at " "index [%ld] from eCryptfs inode " |
18d1dbf1d ecryptfs: replace... |
157 158 |
"mapping; rc = [%d] ", __func__, |
da0102a10 eCryptfs: read_wr... |
159 160 161 |
ecryptfs_page_idx, rc); goto out; } |
da0102a10 eCryptfs: read_wr... |
162 |
ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0); |
7a3f595cc ecryptfs: fix fsx... |
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
/* * pos: where we're now writing, offset: where the request was * If current pos is before request, we are filling zeros * If we are at or beyond request, we are writing the *data* * If we're in a fresh page beyond eof, zero it in either case */ if (pos < offset || !start_offset_in_page) { /* We are extending past the previous end of the file. * Fill in zero values to the end of the page */ memset(((char *)ecryptfs_page_virt + start_offset_in_page), 0, PAGE_CACHE_SIZE - start_offset_in_page); } /* pos >= offset, we are now writing the data request */ |
da0102a10 eCryptfs: read_wr... |
179 180 181 182 183 |
if (pos >= offset) { memcpy(((char *)ecryptfs_page_virt + start_offset_in_page), (data + data_offset), num_bytes); data_offset += num_bytes; |
da0102a10 eCryptfs: read_wr... |
184 185 186 |
} kunmap_atomic(ecryptfs_page_virt, KM_USER0); flush_dcache_page(ecryptfs_page); |
16a72c455 ecryptfs: clean u... |
187 188 |
SetPageUptodate(ecryptfs_page); unlock_page(ecryptfs_page); |
13a791b4e eCryptfs: Fix dat... |
189 190 191 192 193 194 195 |
if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) rc = ecryptfs_encrypt_page(ecryptfs_page); else rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, ecryptfs_page, start_offset_in_page, data_offset); |
16a72c455 ecryptfs: clean u... |
196 |
page_cache_release(ecryptfs_page); |
da0102a10 eCryptfs: read_wr... |
197 198 |
if (rc) { printk(KERN_ERR "%s: Error encrypting " |
18d1dbf1d ecryptfs: replace... |
199 200 |
"page; rc = [%d] ", __func__, rc); |
da0102a10 eCryptfs: read_wr... |
201 202 |
goto out; } |
da0102a10 eCryptfs: read_wr... |
203 204 205 |
pos += num_bytes; } if ((offset + size) > ecryptfs_file_size) { |
13a791b4e eCryptfs: Fix dat... |
206 207 208 209 210 211 212 213 214 215 216 |
i_size_write(ecryptfs_inode, (offset + size)); if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) { rc = ecryptfs_write_inode_size_to_metadata( ecryptfs_inode); if (rc) { printk(KERN_ERR "Problem with " "ecryptfs_write_inode_size_to_metadata; " "rc = [%d] ", rc); goto out; } |
da0102a10 eCryptfs: read_wr... |
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
} } out: return rc; } /** * ecryptfs_read_lower * @data: The read data is stored here by this function * @offset: Byte offset in the lower file from which to read the data * @size: Number of bytes to read from @offset of the lower file and * store into @data * @ecryptfs_inode: The eCryptfs inode * * Read @size bytes of data at byte offset @offset from the lower * inode into memory location @data. * * Returns zero on success; non-zero on error */ int ecryptfs_read_lower(char *data, loff_t offset, size_t size, struct inode *ecryptfs_inode) { struct ecryptfs_inode_info *inode_info = ecryptfs_inode_to_private(ecryptfs_inode); ssize_t octets_read; mm_segment_t fs_save; |
da0102a10 eCryptfs: read_wr... |
243 244 245 246 247 248 249 250 251 252 253 254 |
int rc = 0; mutex_lock(&inode_info->lower_file_mutex); BUG_ON(!inode_info->lower_file); inode_info->lower_file->f_pos = offset; fs_save = get_fs(); set_fs(get_ds()); octets_read = vfs_read(inode_info->lower_file, data, size, &inode_info->lower_file->f_pos); set_fs(fs_save); if (octets_read < 0) { printk(KERN_ERR "%s: octets_read = [%td]; " |
18d1dbf1d ecryptfs: replace... |
255 256 |
"expected [%td] ", __func__, octets_read, size); |
da0102a10 eCryptfs: read_wr... |
257 258 259 |
rc = -EINVAL; } mutex_unlock(&inode_info->lower_file_mutex); |
da0102a10 eCryptfs: read_wr... |
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
return rc; } /** * ecryptfs_read_lower_page_segment * @page_for_ecryptfs: The page into which data for eCryptfs will be * written * @offset_in_page: Offset in @page_for_ecryptfs from which to start * writing * @size: The number of bytes to write into @page_for_ecryptfs * @ecryptfs_inode: The eCryptfs inode * * Determines the byte offset in the file for the given page and * offset within the page, maps the page, and makes the call to read * the contents of @page_for_ecryptfs from the lower inode. * * Returns zero on success; non-zero otherwise */ int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs, pgoff_t page_index, size_t offset_in_page, size_t size, struct inode *ecryptfs_inode) { char *virt; loff_t offset; int rc; |
d6a13c171 eCryptfs: fix dat... |
286 |
offset = ((((loff_t)page_index) << PAGE_CACHE_SHIFT) + offset_in_page); |
da0102a10 eCryptfs: read_wr... |
287 288 289 |
virt = kmap(page_for_ecryptfs); rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode); kunmap(page_for_ecryptfs); |
16a72c455 ecryptfs: clean u... |
290 |
flush_dcache_page(page_for_ecryptfs); |
da0102a10 eCryptfs: read_wr... |
291 292 |
return rc; } |
7896b6318 fs/ecryptfs/: pos... |
293 |
#if 0 |
da0102a10 eCryptfs: read_wr... |
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
/** * ecryptfs_read * @data: The virtual address into which to write the data read (and * possibly decrypted) from the lower file * @offset: The offset in the decrypted view of the file from which to * read into @data * @size: The number of bytes to read into @data * @ecryptfs_file: The eCryptfs file from which to read * * Read an arbitrary amount of data from an arbitrary location in the * eCryptfs page cache. This is done on an extent-by-extent basis; * individual extents are decrypted and read from the lower page * cache (via VFS reads). This function takes care of all the * address translation to locations in the lower filesystem. * * Returns zero on success; non-zero otherwise */ int ecryptfs_read(char *data, loff_t offset, size_t size, struct file *ecryptfs_file) { struct page *ecryptfs_page; char *ecryptfs_page_virt; |
d6a13c171 eCryptfs: fix dat... |
316 317 |
loff_t ecryptfs_file_size = i_size_read(ecryptfs_file->f_dentry->d_inode); |
da0102a10 eCryptfs: read_wr... |
318 319 320 321 322 323 324 325 326 327 |
loff_t data_offset = 0; loff_t pos; int rc = 0; if ((offset + size) > ecryptfs_file_size) { rc = -EINVAL; printk(KERN_ERR "%s: Attempt to read data past the end of the " "file; offset = [%lld]; size = [%td]; " "ecryptfs_file_size = [%lld] ", |
18d1dbf1d ecryptfs: replace... |
328 |
__func__, offset, size, ecryptfs_file_size); |
da0102a10 eCryptfs: read_wr... |
329 330 331 332 333 334 335 336 337 338 339 |
goto out; } pos = offset; while (pos < (offset + size)) { pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT); size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK); size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page); size_t total_remaining_bytes = ((offset + size) - pos); if (num_bytes > total_remaining_bytes) num_bytes = total_remaining_bytes; |
16a72c455 ecryptfs: clean u... |
340 341 |
ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_file, ecryptfs_page_idx); |
da0102a10 eCryptfs: read_wr... |
342 343 344 345 |
if (IS_ERR(ecryptfs_page)) { rc = PTR_ERR(ecryptfs_page); printk(KERN_ERR "%s: Error getting page at " "index [%ld] from eCryptfs inode " |
18d1dbf1d ecryptfs: replace... |
346 347 |
"mapping; rc = [%d] ", __func__, |
da0102a10 eCryptfs: read_wr... |
348 349 350 |
ecryptfs_page_idx, rc); goto out; } |
da0102a10 eCryptfs: read_wr... |
351 352 353 354 355 |
ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0); memcpy((data + data_offset), ((char *)ecryptfs_page_virt + start_offset_in_page), num_bytes); kunmap_atomic(ecryptfs_page_virt, KM_USER0); |
16a72c455 ecryptfs: clean u... |
356 357 358 |
flush_dcache_page(ecryptfs_page); SetPageUptodate(ecryptfs_page); unlock_page(ecryptfs_page); |
da0102a10 eCryptfs: read_wr... |
359 360 361 362 363 364 365 |
page_cache_release(ecryptfs_page); pos += num_bytes; data_offset += num_bytes; } out: return rc; } |
7896b6318 fs/ecryptfs/: pos... |
366 |
#endif /* 0 */ |