Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * eCryptfs: Linux filesystem encryption layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * This is where eCryptfs coordinates the symmetric encryption and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * decryption of the file data as it passes between the lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * encrypted file and the upper decrypted file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 1997-2003 Erez Zadok
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 2001-2003 Stony Brook University
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Copyright (C) 2004-2007 International Business Machines Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/page-flags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "ecryptfs_kernel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * ecryptfs_get_locked_page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * Get one page from cache or lower f/s, return error otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * Returns locked and up-to-date page (if ok), with increased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * refcnt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct page *page = read_mapping_page(inode->i_mapping, index, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (!IS_ERR(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * ecryptfs_writepage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * @page: Page that is locked before this call is made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * This is where we encrypt the data and pass the encrypted data to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * the lower filesystem.  In OpenPGP-compatible mode, we operate on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * entire underlying packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	rc = ecryptfs_encrypt_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		ecryptfs_printk(KERN_WARNING, "Error encrypting "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 				"page (upper index [0x%.16lx])\n", page->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void strip_xattr_flag(char *page_virt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			     struct ecryptfs_crypt_stat *crypt_stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		size_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 						&written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  *   Header Extent:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  *     Octets 0-7:        Unencrypted file size (big-endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  *     Octets 8-15:       eCryptfs special marker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  *     Octets 16-19:      Flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *      Octet 16:         File format version number (between 0 and 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  *      Octets 17-18:     Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  *      Octet 19:         Bit 1 (lsb): Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *                        Bit 2: Encrypted?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  *                        Bits 3-8: Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  *     Octets 20-23:      Header extent size (big-endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  *     Octets 24-25:      Number of header extents at front of file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  *                        (big-endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  *     Octet  26:         Begin RFC 2440 authentication token packet set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * ecryptfs_copy_up_encrypted_with_header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * @page: Sort of a ``virtual'' representation of the encrypted lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *        file. The actual lower file does not have the metadata in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  *        the header. This is locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * @crypt_stat: The eCryptfs inode's cryptographic context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * The ``view'' is the version of the file that userspace winds up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * seeing, with the header information inserted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ecryptfs_copy_up_encrypted_with_header(struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				       struct ecryptfs_crypt_stat *crypt_stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	loff_t extent_num_in_page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	loff_t num_extents_per_page = (PAGE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 				       / crypt_stat->extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	while (extent_num_in_page < num_extents_per_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		loff_t view_extent_num = ((((loff_t)page->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 					   * num_extents_per_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 					  + extent_num_in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		size_t num_header_extents_at_front =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			(crypt_stat->metadata_size / crypt_stat->extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		if (view_extent_num < num_header_extents_at_front) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			/* This is a header extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			char *page_virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			page_virt = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			memset(page_virt, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			/* TODO: Support more than one header extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			if (view_extent_num == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				size_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				rc = ecryptfs_read_xattr_region(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 					page_virt, page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 				strip_xattr_flag(page_virt + 16, crypt_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 				ecryptfs_write_header_metadata(page_virt + 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 							       crypt_stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 							       &written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			kunmap_atomic(page_virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				printk(KERN_ERR "%s: Error reading xattr "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 				       "region; rc = [%d]\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			/* This is an encrypted data extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			loff_t lower_offset =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				((view_extent_num * crypt_stat->extent_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				 - crypt_stat->metadata_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			rc = ecryptfs_read_lower_page_segment(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				page, (lower_offset >> PAGE_SHIFT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 				(lower_offset & ~PAGE_MASK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				crypt_stat->extent_size, page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				printk(KERN_ERR "%s: Error attempting to read "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 				       "extent at offset [%lld] in the lower "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				       "file; rc = [%d]\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				       lower_offset, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		extent_num_in_page++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * ecryptfs_readpage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  * @file: An eCryptfs file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * @page: Page from eCryptfs inode mapping into which to stick the read data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * Read in a page, decrypting if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * Returns zero on success; non-zero on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int ecryptfs_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	struct ecryptfs_crypt_stat *crypt_stat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		&ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 						      PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 						      page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			rc = ecryptfs_copy_up_encrypted_with_header(page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 								    crypt_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				printk(KERN_ERR "%s: Error attempting to copy "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				       "the encrypted content from the lower "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 				       "file whilst inserting the metadata "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 				       "from the xattr into the header; rc = "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				       "[%d]\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			rc = ecryptfs_read_lower_page_segment(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 				page, page->index, 0, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 				printk(KERN_ERR "Error reading page; rc = "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				       "[%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		rc = ecryptfs_decrypt_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			ecryptfs_printk(KERN_ERR, "Error decrypting page; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 					"rc = [%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			page->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * Called with lower inode mutex held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	int end_byte_in_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if ((i_size_read(inode) / PAGE_SIZE) != page->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	end_byte_in_page = i_size_read(inode) % PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (to > end_byte_in_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		end_byte_in_page = to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	zero_user_segment(page, end_byte_in_page, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * ecryptfs_write_begin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  * @file: The eCryptfs file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * @mapping: The eCryptfs object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  * @pos: The file offset at which to start writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * @len: Length of the write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * @flags: Various flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  * @pagep: Pointer to return the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * @fsdata: Pointer to return fs data (unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  * This function must zero any hole we create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int ecryptfs_write_begin(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	pgoff_t index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	loff_t prev_page_end_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	page = grab_cache_page_write_begin(mapping, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	*pagep = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	prev_page_end_size = ((loff_t)index << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		struct ecryptfs_crypt_stat *crypt_stat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			&ecryptfs_inode_to_private(mapping->host)->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			rc = ecryptfs_read_lower_page_segment(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 				page, index, 0, PAGE_SIZE, mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 				printk(KERN_ERR "%s: Error attempting to read "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 				       "lower page segment; rc = [%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 				       __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 				SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		} else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 				rc = ecryptfs_copy_up_encrypted_with_header(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 					page, crypt_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 				if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 					printk(KERN_ERR "%s: Error attempting "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 					       "to copy the encrypted content "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 					       "from the lower file whilst "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 					       "inserting the metadata from "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 					       "the xattr into the header; rc "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 					       "= [%d]\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 					ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 				SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				rc = ecryptfs_read_lower_page_segment(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 					page, index, 0, PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 					mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 				if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 					printk(KERN_ERR "%s: Error reading "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 					       "page; rc = [%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 					       __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 					ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			if (prev_page_end_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			    >= i_size_read(page->mapping->host)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 				zero_user(page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 				SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			} else if (len < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 				rc = ecryptfs_decrypt_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 					printk(KERN_ERR "%s: Error decrypting "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 					       "page at index [%ld]; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 					       "rc = [%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 					       __func__, page->index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 					ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 				SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	/* If creating a page or more of holes, zero them out via truncate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	 * Note, this will increase i_size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (index != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		if (prev_page_end_size > i_size_read(page->mapping->host)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			rc = ecryptfs_truncate(file->f_path.dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 					       prev_page_end_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				printk(KERN_ERR "%s: Error on attempt to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 				       "truncate to (higher) offset [%lld];"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 				       " rc = [%d]\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				       prev_page_end_size, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	/* Writing to a new page, and creating a small hole from start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 * of page?  Zero it out. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if ((i_size_read(mapping->host) == prev_page_end_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	    && (pos != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		zero_user(page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (unlikely(rc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		*pagep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  * ecryptfs_write_inode_size_to_header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  * Writes the lower file size to the first 8 bytes of the header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  * Returns zero on success; non-zero on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	char *file_size_virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (!file_size_virt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 				  sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	kfree(file_size_virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		printk(KERN_ERR "%s: Error writing file size to header; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		       "rc = [%d]\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct kmem_cache *ecryptfs_xattr_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	ssize_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	void *xattr_virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	struct dentry *lower_dentry =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	struct inode *lower_inode = d_inode(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		       "No support for setting xattr in lower filesystem\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		rc = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	if (!xattr_virt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	inode_lock(lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	size = __vfs_getxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			      xattr_virt, PAGE_SIZE, XATTR_NOSECURITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		size = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	rc = __vfs_setxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			    xattr_virt, size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	inode_unlock(lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		printk(KERN_ERR "Error whilst attempting to write inode size "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		       "to lower file xattr; rc = [%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	struct ecryptfs_crypt_stat *crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  * ecryptfs_write_end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  * @file: The eCryptfs file object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)  * @mapping: The eCryptfs object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)  * @pos: The file position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)  * @len: The length of the data (unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)  * @copied: The amount of data copied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)  * @page: The eCryptfs page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)  * @fsdata: The fsdata (unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static int ecryptfs_write_end(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	pgoff_t index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	unsigned from = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	unsigned to = from + copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	struct inode *ecryptfs_inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	struct ecryptfs_crypt_stat *crypt_stat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			"(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 						       to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			rc = copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			fsstack_copy_inode_size(ecryptfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 				ecryptfs_inode_to_lower(ecryptfs_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		if (copied < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	/* Fills in zeros if 'to' goes beyond inode size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	rc = fill_zeros_to_end_of_page(page, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			"zeros in page with index = [0x%.16lx]\n", index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	rc = ecryptfs_encrypt_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 				"index [0x%.16lx])\n", index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (pos + copied > i_size_read(ecryptfs_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		i_size_write(ecryptfs_inode, pos + copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			"[0x%.16llx]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 			(unsigned long long)i_size_read(ecryptfs_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		printk(KERN_ERR "Error writing inode size to metadata; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		       "rc = [%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		rc = copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	struct inode *lower_inode = ecryptfs_inode_to_lower(mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	int ret = bmap(lower_inode, &block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	return block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) const struct address_space_operations ecryptfs_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	.writepage = ecryptfs_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	.readpage = ecryptfs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	.write_begin = ecryptfs_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	.write_end = ecryptfs_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	.bmap = ecryptfs_bmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) };