eCryptfs: remove unused functions and kmem_cache
[safe/jmp/linux-2.6] / fs / ecryptfs / mmap.c
1 /**
2  * eCryptfs: Linux filesystem encryption layer
3  * This is where eCryptfs coordinates the symmetric encryption and
4  * decryption of the file data as it passes between the lower
5  * encrypted file and the upper decrypted file.
6  *
7  * Copyright (C) 1997-2003 Erez Zadok
8  * Copyright (C) 2001-2003 Stony Brook University
9  * Copyright (C) 2004-2007 International Business Machines Corp.
10  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  * 02111-1307, USA.
26  */
27
28 #include <linux/pagemap.h>
29 #include <linux/writeback.h>
30 #include <linux/page-flags.h>
31 #include <linux/mount.h>
32 #include <linux/file.h>
33 #include <linux/crypto.h>
34 #include <linux/scatterlist.h>
35 #include "ecryptfs_kernel.h"
36
37 struct kmem_cache *ecryptfs_lower_page_cache;
38
39 /**
40  * ecryptfs_get1page
41  *
42  * Get one page from cache or lower f/s, return error otherwise.
43  *
44  * Returns unlocked and up-to-date page (if ok), with increased
45  * refcnt.
46  */
47 struct page *ecryptfs_get1page(struct file *file, loff_t index)
48 {
49         struct dentry *dentry;
50         struct inode *inode;
51         struct address_space *mapping;
52
53         dentry = file->f_path.dentry;
54         inode = dentry->d_inode;
55         mapping = inode->i_mapping;
56         return read_mapping_page(mapping, index, (void *)file);
57 }
58
59 /**
60  * ecryptfs_writepage
61  * @page: Page that is locked before this call is made
62  *
63  * Returns zero on success; non-zero otherwise
64  */
65 static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
66 {
67         int rc;
68
69         rc = ecryptfs_encrypt_page(page);
70         if (rc) {
71                 ecryptfs_printk(KERN_WARNING, "Error encrypting "
72                                 "page (upper index [0x%.16x])\n", page->index);
73                 ClearPageUptodate(page);
74                 goto out;
75         }
76         SetPageUptodate(page);
77         unlock_page(page);
78 out:
79         return rc;
80 }
81
82 /**
83  *   Header Extent:
84  *     Octets 0-7:        Unencrypted file size (big-endian)
85  *     Octets 8-15:       eCryptfs special marker
86  *     Octets 16-19:      Flags
87  *      Octet 16:         File format version number (between 0 and 255)
88  *      Octets 17-18:     Reserved
89  *      Octet 19:         Bit 1 (lsb): Reserved
90  *                        Bit 2: Encrypted?
91  *                        Bits 3-8: Reserved
92  *     Octets 20-23:      Header extent size (big-endian)
93  *     Octets 24-25:      Number of header extents at front of file
94  *                        (big-endian)
95  *     Octet  26:         Begin RFC 2440 authentication token packet set
96  */
97 static void set_header_info(char *page_virt,
98                             struct ecryptfs_crypt_stat *crypt_stat)
99 {
100         size_t written;
101         int save_num_header_extents_at_front =
102                 crypt_stat->num_header_extents_at_front;
103
104         crypt_stat->num_header_extents_at_front = 1;
105         ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
106         crypt_stat->num_header_extents_at_front =
107                 save_num_header_extents_at_front;
108 }
109
110 /**
111  * ecryptfs_copy_up_encrypted_with_header
112  * @page: Sort of a ``virtual'' representation of the encrypted lower
113  *        file. The actual lower file does not have the metadata in
114  *        the header. This is locked.
115  * @crypt_stat: The eCryptfs inode's cryptographic context
116  *
117  * The ``view'' is the version of the file that userspace winds up
118  * seeing, with the header information inserted.
119  */
120 static int
121 ecryptfs_copy_up_encrypted_with_header(struct page *page,
122                                        struct ecryptfs_crypt_stat *crypt_stat)
123 {
124         loff_t extent_num_in_page = 0;
125         loff_t num_extents_per_page = (PAGE_CACHE_SIZE
126                                        / crypt_stat->extent_size);
127         int rc = 0;
128
129         while (extent_num_in_page < num_extents_per_page) {
130                 loff_t view_extent_num = ((((loff_t)page->index)
131                                            * num_extents_per_page)
132                                           + extent_num_in_page);
133
134                 if (view_extent_num < crypt_stat->num_header_extents_at_front) {
135                         /* This is a header extent */
136                         char *page_virt;
137
138                         page_virt = kmap_atomic(page, KM_USER0);
139                         memset(page_virt, 0, PAGE_CACHE_SIZE);
140                         /* TODO: Support more than one header extent */
141                         if (view_extent_num == 0) {
142                                 rc = ecryptfs_read_xattr_region(
143                                         page_virt, page->mapping->host);
144                                 set_header_info(page_virt, crypt_stat);
145                         }
146                         kunmap_atomic(page_virt, KM_USER0);
147                         flush_dcache_page(page);
148                         if (rc) {
149                                 ClearPageUptodate(page);
150                                 printk(KERN_ERR "%s: Error reading xattr "
151                                        "region; rc = [%d]\n", __FUNCTION__, rc);
152                                 goto out;
153                         }
154                         SetPageUptodate(page);
155                 } else {
156                         /* This is an encrypted data extent */
157                         loff_t lower_offset =
158                                 ((view_extent_num -
159                                   crypt_stat->num_header_extents_at_front)
160                                  * crypt_stat->extent_size);
161
162                         rc = ecryptfs_read_lower_page_segment(
163                                 page, (lower_offset >> PAGE_CACHE_SHIFT),
164                                 (lower_offset & ~PAGE_CACHE_MASK),
165                                 crypt_stat->extent_size, page->mapping->host);
166                         if (rc) {
167                                 printk(KERN_ERR "%s: Error attempting to read "
168                                        "extent at offset [%lld] in the lower "
169                                        "file; rc = [%d]\n", __FUNCTION__,
170                                        lower_offset, rc);
171                                 goto out;
172                         }
173                 }
174                 extent_num_in_page++;
175         }
176 out:
177         return rc;
178 }
179
180 /**
181  * ecryptfs_readpage
182  * @file: An eCryptfs file
183  * @page: Page from eCryptfs inode mapping into which to stick the read data
184  *
185  * Read in a page, decrypting if necessary.
186  *
187  * Returns zero on success; non-zero on error.
188  */
189 static int ecryptfs_readpage(struct file *file, struct page *page)
190 {
191         struct ecryptfs_crypt_stat *crypt_stat =
192                 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
193         int rc = 0;
194
195         if (!crypt_stat
196             || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
197             || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
198                 ecryptfs_printk(KERN_DEBUG,
199                                 "Passing through unencrypted page\n");
200                 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
201                                                       PAGE_CACHE_SIZE,
202                                                       page->mapping->host);
203         } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
204                 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
205                         rc = ecryptfs_copy_up_encrypted_with_header(page,
206                                                                     crypt_stat);
207                         if (rc) {
208                                 printk(KERN_ERR "%s: Error attempting to copy "
209                                        "the encrypted content from the lower "
210                                        "file whilst inserting the metadata "
211                                        "from the xattr into the header; rc = "
212                                        "[%d]\n", __FUNCTION__, rc);
213                                 goto out;
214                         }
215
216                 } else {
217                         rc = ecryptfs_read_lower_page_segment(
218                                 page, page->index, 0, PAGE_CACHE_SIZE,
219                                 page->mapping->host);
220                         if (rc) {
221                                 printk(KERN_ERR "Error reading page; rc = "
222                                        "[%d]\n", rc);
223                                 goto out;
224                         }
225                 }
226         } else {
227                 rc = ecryptfs_decrypt_page(page);
228                 if (rc) {
229                         ecryptfs_printk(KERN_ERR, "Error decrypting page; "
230                                         "rc = [%d]\n", rc);
231                         goto out;
232                 }
233         }
234 out:
235         ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
236                         page->index);
237         unlock_page(page);
238         return rc;
239 }
240
241 /**
242  * Called with lower inode mutex held.
243  */
244 static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
245 {
246         struct inode *inode = page->mapping->host;
247         int end_byte_in_page;
248
249         if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
250                 goto out;
251         end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
252         if (to > end_byte_in_page)
253                 end_byte_in_page = to;
254         zero_user_page(page, end_byte_in_page,
255                 PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
256 out:
257         return 0;
258 }
259
260 static int ecryptfs_prepare_write(struct file *file, struct page *page,
261                                   unsigned from, unsigned to)
262 {
263         int rc = 0;
264
265         if (from == 0 && to == PAGE_CACHE_SIZE)
266                 goto out;       /* If we are writing a full page, it will be
267                                    up to date. */
268         if (!PageUptodate(page))
269                 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
270                                                       PAGE_CACHE_SIZE,
271                                                       page->mapping->host);
272         if (page->index != 0) {
273                 loff_t end_of_prev_pg_pos =
274                         (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1);
275
276                 if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) {
277                         rc = ecryptfs_truncate(file->f_path.dentry,
278                                                end_of_prev_pg_pos);
279                         if (rc) {
280                                 printk(KERN_ERR "Error on attempt to "
281                                        "truncate to (higher) offset [%lld];"
282                                        " rc = [%d]\n", end_of_prev_pg_pos, rc);
283                                 goto out;
284                         }
285                 }
286                 if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host))
287                         zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
288         }
289 out:
290         return rc;
291 }
292
293 /**
294  * ecryptfs_write_inode_size_to_header
295  *
296  * Writes the lower file size to the first 8 bytes of the header.
297  *
298  * Returns zero on success; non-zero on error.
299  */
300 static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
301 {
302         u64 file_size;
303         char *file_size_virt;
304         int rc;
305
306         file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
307         if (!file_size_virt) {
308                 rc = -ENOMEM;
309                 goto out;
310         }
311         file_size = (u64)i_size_read(ecryptfs_inode);
312         file_size = cpu_to_be64(file_size);
313         memcpy(file_size_virt, &file_size, sizeof(u64));
314         rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
315                                   sizeof(u64));
316         kfree(file_size_virt);
317         if (rc)
318                 printk(KERN_ERR "%s: Error writing file size to header; "
319                        "rc = [%d]\n", __FUNCTION__, rc);
320 out:
321         return rc;
322 }
323
324 struct kmem_cache *ecryptfs_xattr_cache;
325
326 static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
327 {
328         ssize_t size;
329         void *xattr_virt;
330         struct dentry *lower_dentry =
331                 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
332         struct inode *lower_inode = lower_dentry->d_inode;
333         u64 file_size;
334         int rc;
335
336         if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
337                 printk(KERN_WARNING
338                        "No support for setting xattr in lower filesystem\n");
339                 rc = -ENOSYS;
340                 goto out;
341         }
342         xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
343         if (!xattr_virt) {
344                 printk(KERN_ERR "Out of memory whilst attempting to write "
345                        "inode size to xattr\n");
346                 rc = -ENOMEM;
347                 goto out;
348         }
349         mutex_lock(&lower_inode->i_mutex);
350         size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
351                                            xattr_virt, PAGE_CACHE_SIZE);
352         if (size < 0)
353                 size = 8;
354         file_size = (u64)i_size_read(ecryptfs_inode);
355         file_size = cpu_to_be64(file_size);
356         memcpy(xattr_virt, &file_size, sizeof(u64));
357         rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
358                                          xattr_virt, size, 0);
359         mutex_unlock(&lower_inode->i_mutex);
360         if (rc)
361                 printk(KERN_ERR "Error whilst attempting to write inode size "
362                        "to lower file xattr; rc = [%d]\n", rc);
363         kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
364 out:
365         return rc;
366 }
367
368 int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
369 {
370         struct ecryptfs_crypt_stat *crypt_stat;
371
372         crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
373         if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
374                 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
375         else
376                 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
377 }
378
379 /**
380  * ecryptfs_commit_write
381  * @file: The eCryptfs file object
382  * @page: The eCryptfs page
383  * @from: Ignored (we rotate the page IV on each write)
384  * @to: Ignored
385  *
386  * This is where we encrypt the data and pass the encrypted data to
387  * the lower filesystem.  In OpenPGP-compatible mode, we operate on
388  * entire underlying packets.
389  */
390 static int ecryptfs_commit_write(struct file *file, struct page *page,
391                                  unsigned from, unsigned to)
392 {
393         loff_t pos;
394         struct inode *ecryptfs_inode = page->mapping->host;
395         struct ecryptfs_crypt_stat *crypt_stat =
396                 &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
397         int rc;
398
399         if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
400                 ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
401                         "crypt_stat at memory location [%p]\n", crypt_stat);
402                 crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
403         } else
404                 ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
405         ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
406                         "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
407                         to);
408         /* Fills in zeros if 'to' goes beyond inode size */
409         rc = fill_zeros_to_end_of_page(page, to);
410         if (rc) {
411                 ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
412                                 "zeros in page with index = [0x%.16x]\n",
413                                 page->index);
414                 goto out;
415         }
416         rc = ecryptfs_encrypt_page(page);
417         if (rc) {
418                 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
419                                 "index [0x%.16x])\n", page->index);
420                 goto out;
421         }
422         pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
423         if (pos > i_size_read(ecryptfs_inode)) {
424                 i_size_write(ecryptfs_inode, pos);
425                 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
426                                 "[0x%.16x]\n", i_size_read(ecryptfs_inode));
427         }
428         rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
429         if (rc)
430                 printk(KERN_ERR "Error writing inode size to metadata; "
431                        "rc = [%d]\n", rc);
432 out:
433         return rc;
434 }
435
436 static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
437 {
438         int rc = 0;
439         struct inode *inode;
440         struct inode *lower_inode;
441
442         inode = (struct inode *)mapping->host;
443         lower_inode = ecryptfs_inode_to_lower(inode);
444         if (lower_inode->i_mapping->a_ops->bmap)
445                 rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
446                                                          block);
447         return rc;
448 }
449
450 struct address_space_operations ecryptfs_aops = {
451         .writepage = ecryptfs_writepage,
452         .readpage = ecryptfs_readpage,
453         .prepare_write = ecryptfs_prepare_write,
454         .commit_write = ecryptfs_commit_write,
455         .bmap = ecryptfs_bmap,
456 };