@@ -266,70 +266,115 @@ static void set_header_info(char *page_virt,
266266 save_num_header_extents_at_front ;
267267}
268268
269+ /**
270+ * ecryptfs_copy_up_encrypted_with_header
271+ * @page: Sort of a ``virtual'' representation of the encrypted lower
272+ * file. The actual lower file does not have the metadata in
273+ * the header. This is locked.
274+ * @crypt_stat: The eCryptfs inode's cryptographic context
275+ *
276+ * The ``view'' is the version of the file that userspace winds up
277+ * seeing, with the header information inserted.
278+ */
279+ static int
280+ ecryptfs_copy_up_encrypted_with_header (struct page * page ,
281+ struct ecryptfs_crypt_stat * crypt_stat )
282+ {
283+ loff_t extent_num_in_page = 0 ;
284+ loff_t num_extents_per_page = (PAGE_CACHE_SIZE
285+ / crypt_stat -> extent_size );
286+ int rc = 0 ;
287+
288+ while (extent_num_in_page < num_extents_per_page ) {
289+ loff_t view_extent_num = ((page -> index * num_extents_per_page )
290+ + extent_num_in_page );
291+
292+ if (view_extent_num < crypt_stat -> num_header_extents_at_front ) {
293+ /* This is a header extent */
294+ char * page_virt ;
295+
296+ page_virt = kmap_atomic (page , KM_USER0 );
297+ memset (page_virt , 0 , PAGE_CACHE_SIZE );
298+ /* TODO: Support more than one header extent */
299+ if (view_extent_num == 0 ) {
300+ rc = ecryptfs_read_xattr_region (
301+ page_virt , page -> mapping -> host );
302+ set_header_info (page_virt , crypt_stat );
303+ }
304+ kunmap_atomic (page_virt , KM_USER0 );
305+ flush_dcache_page (page );
306+ if (rc ) {
307+ ClearPageUptodate (page );
308+ printk (KERN_ERR "%s: Error reading xattr "
309+ "region; rc = [%d]\n" , __FUNCTION__ , rc );
310+ goto out ;
311+ }
312+ SetPageUptodate (page );
313+ } else {
314+ /* This is an encrypted data extent */
315+ loff_t lower_offset =
316+ ((view_extent_num -
317+ crypt_stat -> num_header_extents_at_front )
318+ * crypt_stat -> extent_size );
319+
320+ rc = ecryptfs_read_lower_page_segment (
321+ page , (lower_offset >> PAGE_CACHE_SHIFT ),
322+ (lower_offset & ~PAGE_CACHE_MASK ),
323+ crypt_stat -> extent_size , page -> mapping -> host );
324+ if (rc ) {
325+ printk (KERN_ERR "%s: Error attempting to read "
326+ "extent at offset [%lld] in the lower "
327+ "file; rc = [%d]\n" , __FUNCTION__ ,
328+ lower_offset , rc );
329+ goto out ;
330+ }
331+ }
332+ extent_num_in_page ++ ;
333+ }
334+ out :
335+ return rc ;
336+ }
337+
269338/**
270339 * ecryptfs_readpage
271- * @file: This is an ecryptfs file
272- * @page: ecryptfs associated page to stick the read data into
340+ * @file: An eCryptfs file
341+ * @page: Page from eCryptfs inode mapping into which to stick the read data
273342 *
274343 * Read in a page, decrypting if necessary.
275344 *
276345 * Returns zero on success; non-zero on error.
277346 */
278347static int ecryptfs_readpage (struct file * file , struct page * page )
279348{
349+ struct ecryptfs_crypt_stat * crypt_stat =
350+ & ecryptfs_inode_to_private (file -> f_path .dentry -> d_inode )-> crypt_stat ;
280351 int rc = 0 ;
281- struct ecryptfs_crypt_stat * crypt_stat ;
282352
283- BUG_ON (!(file && file -> f_path .dentry && file -> f_path .dentry -> d_inode ));
284- crypt_stat = & ecryptfs_inode_to_private (file -> f_path .dentry -> d_inode )
285- -> crypt_stat ;
286353 if (!crypt_stat
287354 || !(crypt_stat -> flags & ECRYPTFS_ENCRYPTED )
288355 || (crypt_stat -> flags & ECRYPTFS_NEW_FILE )) {
289356 ecryptfs_printk (KERN_DEBUG ,
290357 "Passing through unencrypted page\n" );
291- rc = ecryptfs_do_readpage (file , page , page -> index );
292- if (rc ) {
293- ecryptfs_printk (KERN_ERR , "Error reading page; rc = "
294- "[%d]\n" , rc );
295- goto out ;
296- }
358+ rc = ecryptfs_read_lower_page_segment (page , page -> index , 0 ,
359+ PAGE_CACHE_SIZE ,
360+ page -> mapping -> host );
297361 } else if (crypt_stat -> flags & ECRYPTFS_VIEW_AS_ENCRYPTED ) {
298362 if (crypt_stat -> flags & ECRYPTFS_METADATA_IN_XATTR ) {
299- int num_pages_in_header_region =
300- (crypt_stat -> extent_size
301- / PAGE_CACHE_SIZE );
302-
303- if (page -> index < num_pages_in_header_region ) {
304- char * page_virt ;
305-
306- page_virt = kmap_atomic (page , KM_USER0 );
307- memset (page_virt , 0 , PAGE_CACHE_SIZE );
308- if (page -> index == 0 ) {
309- rc = ecryptfs_read_xattr_region (
310- page_virt , page -> mapping -> host );
311- set_header_info (page_virt , crypt_stat );
312- }
313- kunmap_atomic (page_virt , KM_USER0 );
314- flush_dcache_page (page );
315- if (rc ) {
316- printk (KERN_ERR "Error reading xattr "
317- "region\n" );
318- goto out ;
319- }
320- } else {
321- rc = ecryptfs_do_readpage (
322- file , page ,
323- (page -> index
324- - num_pages_in_header_region ));
325- if (rc ) {
326- printk (KERN_ERR "Error reading page; "
327- "rc = [%d]\n" , rc );
328- goto out ;
329- }
363+ rc = ecryptfs_copy_up_encrypted_with_header (page ,
364+ crypt_stat );
365+ if (rc ) {
366+ printk (KERN_ERR "%s: Error attempting to copy "
367+ "the encrypted content from the lower "
368+ "file whilst inserting the metadata "
369+ "from the xattr into the header; rc = "
370+ "[%d]\n" , __FUNCTION__ , rc );
371+ goto out ;
330372 }
373+
331374 } else {
332- rc = ecryptfs_do_readpage (file , page , page -> index );
375+ rc = ecryptfs_read_lower_page_segment (
376+ page , page -> index , 0 , PAGE_CACHE_SIZE ,
377+ page -> mapping -> host );
333378 if (rc ) {
334379 printk (KERN_ERR "Error reading page; rc = "
335380 "[%d]\n" , rc );
@@ -344,10 +389,7 @@ static int ecryptfs_readpage(struct file *file, struct page *page)
344389 goto out ;
345390 }
346391 }
347- SetPageUptodate (page );
348392out :
349- if (rc )
350- ClearPageUptodate (page );
351393 ecryptfs_printk (KERN_DEBUG , "Unlocking page with index = [0x%.16x]\n" ,
352394 page -> index );
353395 unlock_page (page );
@@ -403,9 +445,12 @@ static int ecryptfs_prepare_write(struct file *file, struct page *page,
403445 goto out ; /* If we are writing a full page, it will be
404446 up to date. */
405447 if (!PageUptodate (page ))
406- rc = ecryptfs_do_readpage (file , page , page -> index );
448+ rc = ecryptfs_read_lower_page_segment (page , page -> index , 0 ,
449+ PAGE_CACHE_SIZE ,
450+ page -> mapping -> host );
407451 if (page -> index != 0 ) {
408- loff_t end_of_prev_pg_pos = page_offset (page ) - 1 ;
452+ loff_t end_of_prev_pg_pos =
453+ (((loff_t )page -> index << PAGE_CACHE_SHIFT ) - 1 );
409454
410455 if (end_of_prev_pg_pos > i_size_read (page -> mapping -> host )) {
411456 rc = ecryptfs_truncate (file -> f_path .dentry ,
@@ -633,18 +678,11 @@ static int ecryptfs_commit_write(struct file *file, struct page *page,
633678 unsigned from , unsigned to )
634679{
635680 loff_t pos ;
636- struct inode * inode ;
637- struct inode * lower_inode ;
638- struct file * lower_file ;
639- struct ecryptfs_crypt_stat * crypt_stat ;
681+ struct inode * ecryptfs_inode = page -> mapping -> host ;
682+ struct ecryptfs_crypt_stat * crypt_stat =
683+ & ecryptfs_inode_to_private (file -> f_path .dentry -> d_inode )-> crypt_stat ;
640684 int rc ;
641685
642- inode = page -> mapping -> host ;
643- lower_inode = ecryptfs_inode_to_lower (inode );
644- lower_file = ecryptfs_file_to_lower (file );
645- mutex_lock (& lower_inode -> i_mutex );
646- crypt_stat = & ecryptfs_inode_to_private (file -> f_path .dentry -> d_inode )
647- -> crypt_stat ;
648686 if (crypt_stat -> flags & ECRYPTFS_NEW_FILE ) {
649687 ecryptfs_printk (KERN_DEBUG , "ECRYPTFS_NEW_FILE flag set in "
650688 "crypt_stat at memory location [%p]\n" , crypt_stat );
@@ -654,6 +692,7 @@ static int ecryptfs_commit_write(struct file *file, struct page *page,
654692 ecryptfs_printk (KERN_DEBUG , "Calling fill_zeros_to_end_of_page"
655693 "(page w/ index = [0x%.16x], to = [%d])\n" , page -> index ,
656694 to );
695+ /* Fills in zeros if 'to' goes beyond inode size */
657696 rc = fill_zeros_to_end_of_page (page , to );
658697 if (rc ) {
659698 ecryptfs_printk (KERN_WARNING , "Error attempting to fill "
@@ -667,25 +706,17 @@ static int ecryptfs_commit_write(struct file *file, struct page *page,
667706 "index [0x%.16x])\n" , page -> index );
668707 goto out ;
669708 }
670- inode -> i_blocks = lower_inode -> i_blocks ;
671- pos = page_offset (page ) + to ;
672- if (pos > i_size_read (inode )) {
673- i_size_write (inode , pos );
709+ pos = (page -> index << PAGE_CACHE_SHIFT ) + to ;
710+ if (pos > i_size_read (ecryptfs_inode )) {
711+ i_size_write (ecryptfs_inode , pos );
674712 ecryptfs_printk (KERN_DEBUG , "Expanded file size to "
675- "[0x%.16x]\n" , i_size_read (inode ));
713+ "[0x%.16x]\n" , i_size_read (ecryptfs_inode ));
676714 }
677- rc = ecryptfs_write_inode_size_to_metadata (inode );
715+ rc = ecryptfs_write_inode_size_to_metadata (ecryptfs_inode );
678716 if (rc )
679717 printk (KERN_ERR "Error writing inode size to metadata; "
680718 "rc = [%d]\n" , rc );
681- lower_inode -> i_mtime = lower_inode -> i_ctime = CURRENT_TIME ;
682- mark_inode_dirty_sync (inode );
683719out :
684- if (rc < 0 )
685- ClearPageUptodate (page );
686- else
687- SetPageUptodate (page );
688- mutex_unlock (& lower_inode -> i_mutex );
689720 return rc ;
690721}
691722
@@ -751,34 +782,10 @@ static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
751782 return rc ;
752783}
753784
754- static void ecryptfs_sync_page (struct page * page )
755- {
756- struct inode * inode ;
757- struct inode * lower_inode ;
758- struct page * lower_page ;
759-
760- inode = page -> mapping -> host ;
761- lower_inode = ecryptfs_inode_to_lower (inode );
762- /* NOTE: Recently swapped with grab_cache_page(), since
763- * sync_page() just makes sure that pending I/O gets done. */
764- lower_page = find_lock_page (lower_inode -> i_mapping , page -> index );
765- if (!lower_page ) {
766- ecryptfs_printk (KERN_DEBUG , "find_lock_page failed\n" );
767- return ;
768- }
769- if (lower_page -> mapping -> a_ops -> sync_page )
770- lower_page -> mapping -> a_ops -> sync_page (lower_page );
771- ecryptfs_printk (KERN_DEBUG , "Unlocking page with index = [0x%.16x]\n" ,
772- lower_page -> index );
773- unlock_page (lower_page );
774- page_cache_release (lower_page );
775- }
776-
777785struct address_space_operations ecryptfs_aops = {
778786 .writepage = ecryptfs_writepage ,
779787 .readpage = ecryptfs_readpage ,
780788 .prepare_write = ecryptfs_prepare_write ,
781789 .commit_write = ecryptfs_commit_write ,
782790 .bmap = ecryptfs_bmap ,
783- .sync_page = ecryptfs_sync_page ,
784791};
0 commit comments