blob: e23752d9a79f3d255345177db1b66950ae8e6607 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Michael Halcrow237fead2006-10-04 02:16:22 -07002/**
3 * eCryptfs: Linux filesystem encryption layer
4 *
5 * Copyright (C) 1997-2004 Erez Zadok
6 * Copyright (C) 2001-2004 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08007 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07008 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9 * Michael C. Thompsion <mcthomps@us.ibm.com>
Michael Halcrow237fead2006-10-04 02:16:22 -070010 */
11
12#include <linux/file.h>
13#include <linux/vmalloc.h>
14#include <linux/pagemap.h>
15#include <linux/dcache.h>
16#include <linux/namei.h>
17#include <linux/mount.h>
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -080018#include <linux/fs_stack.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Roberto Sassu48b512e2010-10-05 18:53:45 +020020#include <linux/xattr.h>
Harvey Harrison0a688ad2008-07-23 21:30:07 -070021#include <asm/unaligned.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070022#include "ecryptfs_kernel.h"
23
24static struct dentry *lock_parent(struct dentry *dentry)
25{
26 struct dentry *dir;
27
Miklos Szeredi8dc4e372008-05-12 14:02:04 -070028 dir = dget_parent(dentry);
Al Viro59551022016-01-22 15:40:57 -050029 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
Michael Halcrow237fead2006-10-04 02:16:22 -070030 return dir;
31}
32
Michael Halcrow237fead2006-10-04 02:16:22 -070033static void unlock_dir(struct dentry *dir)
34{
Al Viro59551022016-01-22 15:40:57 -050035 inode_unlock(d_inode(dir));
Michael Halcrow237fead2006-10-04 02:16:22 -070036 dput(dir);
37}
38
Tyler Hicksc4f79072011-05-23 21:18:20 -050039static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
40{
Himangi Saraogic4cf3ba2014-06-27 01:11:59 +053041 return ecryptfs_inode_to_lower(inode) == lower_inode;
Tyler Hicksc4f79072011-05-23 21:18:20 -050042}
43
Tyler Hicks5ccf9202011-05-24 02:16:51 -050044static int ecryptfs_inode_set(struct inode *inode, void *opaque)
Tyler Hicksc4f79072011-05-23 21:18:20 -050045{
Tyler Hicks5ccf9202011-05-24 02:16:51 -050046 struct inode *lower_inode = opaque;
47
48 ecryptfs_set_inode_lower(inode, lower_inode);
49 fsstack_copy_attr_all(inode, lower_inode);
50 /* i_size will be overwritten for encrypted regular files */
51 fsstack_copy_inode_size(inode, lower_inode);
52 inode->i_ino = lower_inode->i_ino;
Tyler Hicksc4f79072011-05-23 21:18:20 -050053 inode->i_mapping->a_ops = &ecryptfs_aops;
Tyler Hicks5ccf9202011-05-24 02:16:51 -050054
55 if (S_ISLNK(inode->i_mode))
56 inode->i_op = &ecryptfs_symlink_iops;
57 else if (S_ISDIR(inode->i_mode))
58 inode->i_op = &ecryptfs_dir_iops;
59 else
60 inode->i_op = &ecryptfs_main_iops;
61
62 if (S_ISDIR(inode->i_mode))
63 inode->i_fop = &ecryptfs_dir_fops;
64 else if (special_file(inode->i_mode))
65 init_special_inode(inode, inode->i_mode, inode->i_rdev);
66 else
67 inode->i_fop = &ecryptfs_main_fops;
68
Tyler Hicksc4f79072011-05-23 21:18:20 -050069 return 0;
70}
71
Tyler Hicks5ccf9202011-05-24 02:16:51 -050072static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
73 struct super_block *sb)
74{
75 struct inode *inode;
76
77 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
78 return ERR_PTR(-EXDEV);
79 if (!igrab(lower_inode))
80 return ERR_PTR(-ESTALE);
81 inode = iget5_locked(sb, (unsigned long)lower_inode,
82 ecryptfs_inode_test, ecryptfs_inode_set,
83 lower_inode);
84 if (!inode) {
85 iput(lower_inode);
86 return ERR_PTR(-EACCES);
87 }
88 if (!(inode->i_state & I_NEW))
89 iput(lower_inode);
90
91 return inode;
92}
93
Tyler Hicksc4f79072011-05-23 21:18:20 -050094struct inode *ecryptfs_get_inode(struct inode *lower_inode,
95 struct super_block *sb)
96{
Tyler Hicks5ccf9202011-05-24 02:16:51 -050097 struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
Tyler Hicksc4f79072011-05-23 21:18:20 -050098
Tyler Hicks5ccf9202011-05-24 02:16:51 -050099 if (!IS_ERR(inode) && (inode->i_state & I_NEW))
Tyler Hicksc4f79072011-05-23 21:18:20 -0500100 unlock_new_inode(inode);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500101
Tyler Hicksc4f79072011-05-23 21:18:20 -0500102 return inode;
Tyler Hicksc4f79072011-05-23 21:18:20 -0500103}
104
Tyler Hicksc4f79072011-05-23 21:18:20 -0500105/**
106 * ecryptfs_interpose
107 * @lower_dentry: Existing dentry in the lower filesystem
108 * @dentry: ecryptfs' dentry
109 * @sb: ecryptfs's super_block
Tyler Hicksc4f79072011-05-23 21:18:20 -0500110 *
111 * Interposes upper and lower dentries.
112 *
113 * Returns zero on success; non-zero otherwise
114 */
115static int ecryptfs_interpose(struct dentry *lower_dentry,
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500116 struct dentry *dentry, struct super_block *sb)
Tyler Hicksc4f79072011-05-23 21:18:20 -0500117{
David Howells2b0143b2015-03-17 22:25:59 +0000118 struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500119
Tyler Hicksc4f79072011-05-23 21:18:20 -0500120 if (IS_ERR(inode))
121 return PTR_ERR(inode);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500122 d_instantiate(dentry, inode);
123
Tyler Hicksc4f79072011-05-23 21:18:20 -0500124 return 0;
125}
126
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500127static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
128 struct inode *inode)
129{
130 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500131 struct dentry *lower_dir_dentry;
Al Virobcf0d9d2019-11-03 12:07:15 -0500132 struct inode *lower_dir_inode;
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500133 int rc;
134
Al Virobcf0d9d2019-11-03 12:07:15 -0500135 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
136 lower_dir_inode = d_inode(lower_dir_dentry);
137 inode_lock_nested(lower_dir_inode, I_MUTEX_PARENT);
138 dget(lower_dentry); // don't even try to make the lower negative
139 if (lower_dentry->d_parent != lower_dir_dentry)
140 rc = -EINVAL;
141 else if (d_unhashed(lower_dentry))
142 rc = -EINVAL;
143 else
144 rc = vfs_unlink(lower_dir_inode, lower_dentry, NULL);
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500145 if (rc) {
146 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
147 goto out_unlock;
148 }
149 fsstack_copy_attr_times(dir, lower_dir_inode);
150 set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
151 inode->i_ctime = dir->i_ctime;
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500152out_unlock:
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500153 dput(lower_dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500154 inode_unlock(lower_dir_inode);
155 if (!rc)
156 d_drop(dentry);
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500157 return rc;
158}
159
Michael Halcrow237fead2006-10-04 02:16:22 -0700160/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700161 * ecryptfs_do_create
162 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
163 * @ecryptfs_dentry: New file's dentry in ecryptfs
164 * @mode: The mode of the new file
Michael Halcrow237fead2006-10-04 02:16:22 -0700165 *
166 * Creates the underlying file and the eCryptfs inode which will link to
167 * it. It will also update the eCryptfs directory inode to mimic the
168 * stat of the lower directory inode.
169 *
Tyler Hicksb59db432011-11-21 17:31:02 -0600170 * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
Michael Halcrow237fead2006-10-04 02:16:22 -0700171 */
Tyler Hicksb59db432011-11-21 17:31:02 -0600172static struct inode *
Michael Halcrow237fead2006-10-04 02:16:22 -0700173ecryptfs_do_create(struct inode *directory_inode,
Al Viro175a4eb2011-07-26 03:30:54 -0400174 struct dentry *ecryptfs_dentry, umode_t mode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700175{
176 int rc;
177 struct dentry *lower_dentry;
178 struct dentry *lower_dir_dentry;
Tyler Hicksb59db432011-11-21 17:31:02 -0600179 struct inode *inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700180
181 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
182 lower_dir_dentry = lock_parent(lower_dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000183 rc = vfs_create(d_inode(lower_dir_dentry), lower_dentry, mode, true);
Michael Halcrow4981e082007-10-16 01:28:09 -0700184 if (rc) {
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800185 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700186 "rc = [%d]\n", __func__, rc);
Tyler Hicksb59db432011-11-21 17:31:02 -0600187 inode = ERR_PTR(rc);
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800188 goto out_lock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700189 }
David Howells2b0143b2015-03-17 22:25:59 +0000190 inode = __ecryptfs_get_inode(d_inode(lower_dentry),
Tyler Hicksb59db432011-11-21 17:31:02 -0600191 directory_inode->i_sb);
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500192 if (IS_ERR(inode)) {
David Howells2b0143b2015-03-17 22:25:59 +0000193 vfs_unlink(d_inode(lower_dir_dentry), lower_dentry, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700194 goto out_lock;
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500195 }
David Howells2b0143b2015-03-17 22:25:59 +0000196 fsstack_copy_attr_times(directory_inode, d_inode(lower_dir_dentry));
197 fsstack_copy_inode_size(directory_inode, d_inode(lower_dir_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700198out_lock:
199 unlock_dir(lower_dir_dentry);
Tyler Hicksb59db432011-11-21 17:31:02 -0600200 return inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700201}
202
203/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700204 * ecryptfs_initialize_file
205 *
206 * Cause the file to be changed from a basic empty file to an ecryptfs
207 * file with a header and first data page.
208 *
209 * Returns zero on success
210 */
Tyler Hickse3ccaa92012-06-20 23:50:59 -0700211int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
212 struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700213{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700214 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -0600215 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700216 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700217
Tyler Hicksb59db432011-11-21 17:31:02 -0600218 if (S_ISDIR(ecryptfs_inode->i_mode)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700219 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800220 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700221 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700222 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700223 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
Tyler Hicksb59db432011-11-21 17:31:02 -0600224 rc = ecryptfs_new_file_context(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700225 if (rc) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700226 ecryptfs_printk(KERN_ERR, "Error creating new file "
227 "context; rc = [%d]\n", rc);
228 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700229 }
Tyler Hicksb59db432011-11-21 17:31:02 -0600230 rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
Roberto Sassu27992892010-11-03 11:11:28 +0100231 if (rc) {
232 printk(KERN_ERR "%s: Error attempting to initialize "
Tyler Hicks332ab162011-04-14 15:35:11 -0500233 "the lower file for the dentry with name "
David Howells9e78d142013-12-10 15:26:48 +0000234 "[%pd]; rc = [%d]\n", __func__,
235 ecryptfs_dentry, rc);
Roberto Sassu27992892010-11-03 11:11:28 +0100236 goto out;
Michael Halcrow391b52f2008-07-23 21:30:08 -0700237 }
Tyler Hicksb59db432011-11-21 17:31:02 -0600238 rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500239 if (rc)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700240 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
Tyler Hicksb59db432011-11-21 17:31:02 -0600241 ecryptfs_put_lower_file(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700242out:
243 return rc;
244}
245
246/**
247 * ecryptfs_create
248 * @dir: The inode of the directory in which to create the file.
249 * @dentry: The eCryptfs dentry
250 * @mode: The mode of the new file.
Michael Halcrow237fead2006-10-04 02:16:22 -0700251 *
252 * Creates a new file.
253 *
254 * Returns zero on success; non-zero on error condition
255 */
256static int
257ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
Al Viroebfc3b42012-06-10 18:05:36 -0400258 umode_t mode, bool excl)
Michael Halcrow237fead2006-10-04 02:16:22 -0700259{
Tyler Hicksb59db432011-11-21 17:31:02 -0600260 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700261 int rc;
262
Tyler Hicksb59db432011-11-21 17:31:02 -0600263 ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
264 mode);
Viresh Kumara1c83682015-08-12 15:59:44 +0530265 if (IS_ERR(ecryptfs_inode)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700266 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
267 "lower filesystem\n");
Tyler Hicksb59db432011-11-21 17:31:02 -0600268 rc = PTR_ERR(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700269 goto out;
270 }
271 /* At this point, a file exists on "disk"; we need to make sure
272 * that this on disk file is prepared to be an ecryptfs file */
Tyler Hicksb59db432011-11-21 17:31:02 -0600273 rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
274 if (rc) {
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500275 ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
276 ecryptfs_inode);
Al Viro0e81ba22015-12-05 18:23:48 -0500277 iget_failed(ecryptfs_inode);
Tyler Hicksb59db432011-11-21 17:31:02 -0600278 goto out;
279 }
Al Viro1e2e5472018-05-04 08:23:01 -0400280 d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700281out:
282 return rc;
283}
284
Tyler Hicks778aeb42011-05-24 04:56:23 -0500285static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700286{
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500287 struct ecryptfs_crypt_stat *crypt_stat;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500288 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700289
Tyler Hicks778aeb42011-05-24 04:56:23 -0500290 rc = ecryptfs_get_lower_file(dentry, inode);
Roberto Sassu27992892010-11-03 11:11:28 +0100291 if (rc) {
292 printk(KERN_ERR "%s: Error attempting to initialize "
Tyler Hicks332ab162011-04-14 15:35:11 -0500293 "the lower file for the dentry with name "
David Howells9e78d142013-12-10 15:26:48 +0000294 "[%pd]; rc = [%d]\n", __func__,
295 dentry, rc);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500296 return rc;
Michael Halcrow391b52f2008-07-23 21:30:08 -0700297 }
Tyler Hicks778aeb42011-05-24 04:56:23 -0500298
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500299 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500300 /* TODO: lock for crypt_stat comparison */
301 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
Tyler Hicks778aeb42011-05-24 04:56:23 -0500302 ecryptfs_set_default_sizes(crypt_stat);
303
304 rc = ecryptfs_read_and_validate_header_region(inode);
305 ecryptfs_put_lower_file(inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700306 if (rc) {
Tyler Hicks778aeb42011-05-24 04:56:23 -0500307 rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
308 if (!rc)
309 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
Michael Halcrow237fead2006-10-04 02:16:22 -0700310 }
Tyler Hicks778aeb42011-05-24 04:56:23 -0500311
312 /* Must return 0 to allow non-eCryptfs files to be looked up, too */
313 return 0;
314}
315
316/**
317 * ecryptfs_lookup_interpose - Dentry interposition for a lookup
318 */
Al Virob1168a92016-03-28 00:30:35 -0400319static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
320 struct dentry *lower_dentry)
Tyler Hicks778aeb42011-05-24 04:56:23 -0500321{
Al Viro762c6962019-11-03 13:55:43 -0500322 struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
Al Viroe72b9dd2019-11-03 13:45:04 -0500323 struct inode *inode, *lower_inode;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500324 struct ecryptfs_dentry_info *dentry_info;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500325 int rc = 0;
326
Tyler Hicks778aeb42011-05-24 04:56:23 -0500327 dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500328 if (!dentry_info) {
Tyler Hicks778aeb42011-05-24 04:56:23 -0500329 dput(lower_dentry);
Al Virob1168a92016-03-28 00:30:35 -0400330 return ERR_PTR(-ENOMEM);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500331 }
Al Viro0b1d9012012-07-20 12:09:19 +0400332
Al Virob1168a92016-03-28 00:30:35 -0400333 fsstack_copy_attr_atime(d_inode(dentry->d_parent),
Al Viro762c6962019-11-03 13:55:43 -0500334 d_inode(path->dentry));
Al Viro84d08fa2013-07-05 18:59:33 +0400335 BUG_ON(!d_count(lower_dentry));
Al Viro0b1d9012012-07-20 12:09:19 +0400336
337 ecryptfs_set_dentry_private(dentry, dentry_info);
Al Viro762c6962019-11-03 13:55:43 -0500338 dentry_info->lower_path.mnt = mntget(path->mnt);
Al Viro92dd1232013-09-15 20:50:13 -0400339 dentry_info->lower_path.dentry = lower_dentry;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500340
Al Viroe72b9dd2019-11-03 13:45:04 -0500341 /*
342 * negative dentry can go positive under us here - its parent is not
343 * locked. That's OK and that could happen just as we return from
344 * ecryptfs_lookup() anyway. Just need to be careful and fetch
345 * ->d_inode only once - it's not stable here.
346 */
347 lower_inode = READ_ONCE(lower_dentry->d_inode);
348
349 if (!lower_inode) {
Tyler Hicks778aeb42011-05-24 04:56:23 -0500350 /* We want to add because we couldn't find in lower */
351 d_add(dentry, NULL);
Al Virob1168a92016-03-28 00:30:35 -0400352 return NULL;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500353 }
Al Virob1168a92016-03-28 00:30:35 -0400354 inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500355 if (IS_ERR(inode)) {
356 printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
357 __func__, PTR_ERR(inode));
Al Virob1168a92016-03-28 00:30:35 -0400358 return ERR_CAST(inode);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500359 }
360 if (S_ISREG(inode->i_mode)) {
361 rc = ecryptfs_i_size_read(dentry, inode);
362 if (rc) {
363 make_bad_inode(inode);
Al Virob1168a92016-03-28 00:30:35 -0400364 return ERR_PTR(rc);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500365 }
366 }
367
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500368 if (inode->i_state & I_NEW)
369 unlock_new_inode(inode);
Al Virob1168a92016-03-28 00:30:35 -0400370 return d_splice_alias(inode, dentry);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800371}
372
373/**
374 * ecryptfs_lookup
375 * @ecryptfs_dir_inode: The eCryptfs directory inode
376 * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
Al Viro89076bc2015-05-12 08:29:38 -0400377 * @flags: lookup flags
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800378 *
379 * Find a file on disk. If the file does not exist, then we'll add it to the
380 * dentry cache and continue on to read it from the disk.
381 */
382static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
383 struct dentry *ecryptfs_dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400384 unsigned int flags)
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800385{
386 char *encrypted_and_encoded_name = NULL;
Al Viro88ae4ab2016-03-28 00:43:29 -0400387 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800388 struct dentry *lower_dir_dentry, *lower_dentry;
Al Viro88ae4ab2016-03-28 00:43:29 -0400389 const char *name = ecryptfs_dentry->d_name.name;
390 size_t len = ecryptfs_dentry->d_name.len;
Al Virob1168a92016-03-28 00:30:35 -0400391 struct dentry *res;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800392 int rc = 0;
393
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800394 lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
Al Viro88ae4ab2016-03-28 00:43:29 -0400395
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500396 mount_crypt_stat = &ecryptfs_superblock_to_private(
397 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Guenter Roeckab13a922018-01-18 18:40:25 -0800398 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
Al Viro88ae4ab2016-03-28 00:43:29 -0400399 rc = ecryptfs_encrypt_and_encode_filename(
400 &encrypted_and_encoded_name, &len,
401 mount_crypt_stat, name, len);
402 if (rc) {
403 printk(KERN_ERR "%s: Error attempting to encrypt and encode "
404 "filename; rc = [%d]\n", __func__, rc);
405 return ERR_PTR(rc);
406 }
407 name = encrypted_and_encoded_name;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800408 }
Al Viro88ae4ab2016-03-28 00:43:29 -0400409
410 lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800411 if (IS_ERR(lower_dentry)) {
Tyler Hicks8787c7a2011-02-17 18:51:24 -0600412 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
Al Virob1168a92016-03-28 00:30:35 -0400413 "[%ld] on lower_dentry = [%s]\n", __func__,
414 PTR_ERR(lower_dentry),
Al Viro88ae4ab2016-03-28 00:43:29 -0400415 name);
Al Virob1168a92016-03-28 00:30:35 -0400416 res = ERR_CAST(lower_dentry);
Al Viro88ae4ab2016-03-28 00:43:29 -0400417 } else {
418 res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800419 }
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800420 kfree(encrypted_and_encoded_name);
Al Virob1168a92016-03-28 00:30:35 -0400421 return res;
Michael Halcrow237fead2006-10-04 02:16:22 -0700422}
423
424static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
425 struct dentry *new_dentry)
426{
427 struct dentry *lower_old_dentry;
428 struct dentry *lower_new_dentry;
429 struct dentry *lower_dir_dentry;
430 u64 file_size_save;
431 int rc;
432
David Howells2b0143b2015-03-17 22:25:59 +0000433 file_size_save = i_size_read(d_inode(old_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700434 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
435 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
436 dget(lower_old_dentry);
437 dget(lower_new_dentry);
438 lower_dir_dentry = lock_parent(lower_new_dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000439 rc = vfs_link(lower_old_dentry, d_inode(lower_dir_dentry),
J. Bruce Fields146a8592011-09-20 17:14:31 -0400440 lower_new_dentry, NULL);
David Howells2b0143b2015-03-17 22:25:59 +0000441 if (rc || d_really_is_negative(lower_new_dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700442 goto out_lock;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500443 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700444 if (rc)
445 goto out_lock;
David Howells2b0143b2015-03-17 22:25:59 +0000446 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
447 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
448 set_nlink(d_inode(old_dentry),
449 ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
450 i_size_write(d_inode(new_dentry), file_size_save);
Michael Halcrow237fead2006-10-04 02:16:22 -0700451out_lock:
452 unlock_dir(lower_dir_dentry);
453 dput(lower_new_dentry);
454 dput(lower_old_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700455 return rc;
456}
457
458static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
459{
David Howells2b0143b2015-03-17 22:25:59 +0000460 return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700461}
462
463static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
464 const char *symname)
465{
466 int rc;
467 struct dentry *lower_dentry;
468 struct dentry *lower_dir_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700469 char *encoded_symname;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800470 size_t encoded_symlen;
471 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700472
473 lower_dentry = ecryptfs_dentry_to_lower(dentry);
474 dget(lower_dentry);
475 lower_dir_dentry = lock_parent(lower_dentry);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800476 mount_crypt_stat = &ecryptfs_superblock_to_private(
477 dir->i_sb)->mount_crypt_stat;
478 rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
479 &encoded_symlen,
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800480 mount_crypt_stat, symname,
481 strlen(symname));
482 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700483 goto out_lock;
David Howells2b0143b2015-03-17 22:25:59 +0000484 rc = vfs_symlink(d_inode(lower_dir_dentry), lower_dentry,
Miklos Szeredidb2e7472008-06-24 16:50:16 +0200485 encoded_symname);
Michael Halcrow237fead2006-10-04 02:16:22 -0700486 kfree(encoded_symname);
David Howells2b0143b2015-03-17 22:25:59 +0000487 if (rc || d_really_is_negative(lower_dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700488 goto out_lock;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500489 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700490 if (rc)
491 goto out_lock;
David Howells2b0143b2015-03-17 22:25:59 +0000492 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
493 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700494out_lock:
495 unlock_dir(lower_dir_dentry);
496 dput(lower_dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000497 if (d_really_is_negative(dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700498 d_drop(dentry);
499 return rc;
500}
501
Al Viro18bb1db2011-07-26 01:41:39 -0400502static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700503{
504 int rc;
505 struct dentry *lower_dentry;
506 struct dentry *lower_dir_dentry;
507
508 lower_dentry = ecryptfs_dentry_to_lower(dentry);
509 lower_dir_dentry = lock_parent(lower_dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000510 rc = vfs_mkdir(d_inode(lower_dir_dentry), lower_dentry, mode);
511 if (rc || d_really_is_negative(lower_dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700512 goto out;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500513 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700514 if (rc)
515 goto out;
David Howells2b0143b2015-03-17 22:25:59 +0000516 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
517 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
518 set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
Michael Halcrow237fead2006-10-04 02:16:22 -0700519out:
520 unlock_dir(lower_dir_dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000521 if (d_really_is_negative(dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700522 d_drop(dentry);
523 return rc;
524}
525
526static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
527{
Michael Halcrow237fead2006-10-04 02:16:22 -0700528 struct dentry *lower_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700529 struct dentry *lower_dir_dentry;
Al Virobcf0d9d2019-11-03 12:07:15 -0500530 struct inode *lower_dir_inode;
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800531 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700532
533 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500534 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
535 lower_dir_inode = d_inode(lower_dir_dentry);
536
537 inode_lock_nested(lower_dir_inode, I_MUTEX_PARENT);
538 dget(lower_dentry); // don't even try to make the lower negative
539 if (lower_dentry->d_parent != lower_dir_dentry)
540 rc = -EINVAL;
541 else if (d_unhashed(lower_dentry))
542 rc = -EINVAL;
543 else
544 rc = vfs_rmdir(lower_dir_inode, lower_dentry);
545 if (!rc) {
David Howells2b0143b2015-03-17 22:25:59 +0000546 clear_nlink(d_inode(dentry));
Al Virobcf0d9d2019-11-03 12:07:15 -0500547 fsstack_copy_attr_times(dir, lower_dir_inode);
548 set_nlink(dir, lower_dir_inode->i_nlink);
549 }
550 dput(lower_dentry);
551 inode_unlock(lower_dir_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700552 if (!rc)
553 d_drop(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700554 return rc;
555}
556
557static int
Al Viro1a67aaf2011-07-26 01:52:52 -0400558ecryptfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Michael Halcrow237fead2006-10-04 02:16:22 -0700559{
560 int rc;
561 struct dentry *lower_dentry;
562 struct dentry *lower_dir_dentry;
563
564 lower_dentry = ecryptfs_dentry_to_lower(dentry);
565 lower_dir_dentry = lock_parent(lower_dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000566 rc = vfs_mknod(d_inode(lower_dir_dentry), lower_dentry, mode, dev);
567 if (rc || d_really_is_negative(lower_dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700568 goto out;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500569 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700570 if (rc)
571 goto out;
David Howells2b0143b2015-03-17 22:25:59 +0000572 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
573 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700574out:
575 unlock_dir(lower_dir_dentry);
David Howells2b0143b2015-03-17 22:25:59 +0000576 if (d_really_is_negative(dentry))
Michael Halcrow237fead2006-10-04 02:16:22 -0700577 d_drop(dentry);
578 return rc;
579}
580
581static int
582ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +0200583 struct inode *new_dir, struct dentry *new_dentry,
584 unsigned int flags)
Michael Halcrow237fead2006-10-04 02:16:22 -0700585{
586 int rc;
587 struct dentry *lower_old_dentry;
588 struct dentry *lower_new_dentry;
589 struct dentry *lower_old_dir_dentry;
590 struct dentry *lower_new_dir_dentry;
Al Virobcf0d9d2019-11-03 12:07:15 -0500591 struct dentry *trap;
Tyler Hicks8335eaf2012-09-13 12:00:56 -0700592 struct inode *target_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700593
Miklos Szeredi1cd66c92016-09-27 11:03:58 +0200594 if (flags)
595 return -EINVAL;
596
Al Virobcf0d9d2019-11-03 12:07:15 -0500597 lower_old_dir_dentry = ecryptfs_dentry_to_lower(old_dentry->d_parent);
598 lower_new_dir_dentry = ecryptfs_dentry_to_lower(new_dentry->d_parent);
599
Michael Halcrow237fead2006-10-04 02:16:22 -0700600 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
601 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500602
David Howells2b0143b2015-03-17 22:25:59 +0000603 target_inode = d_inode(new_dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500604
Erez Zadok0d132f72009-12-05 21:17:09 -0500605 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500606 dget(lower_new_dentry);
Al Viro74dd7c92018-10-09 23:32:41 -0400607 rc = -EINVAL;
608 if (lower_old_dentry->d_parent != lower_old_dir_dentry)
Erez Zadok0d132f72009-12-05 21:17:09 -0500609 goto out_lock;
Al Viro74dd7c92018-10-09 23:32:41 -0400610 if (lower_new_dentry->d_parent != lower_new_dir_dentry)
611 goto out_lock;
612 if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry))
613 goto out_lock;
614 /* source should not be ancestor of target */
615 if (trap == lower_old_dentry)
616 goto out_lock;
Erez Zadok0d132f72009-12-05 21:17:09 -0500617 /* target should not be ancestor of source */
618 if (trap == lower_new_dentry) {
619 rc = -ENOTEMPTY;
620 goto out_lock;
621 }
David Howells2b0143b2015-03-17 22:25:59 +0000622 rc = vfs_rename(d_inode(lower_old_dir_dentry), lower_old_dentry,
623 d_inode(lower_new_dir_dentry), lower_new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +0200624 NULL, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -0700625 if (rc)
626 goto out_lock;
Tyler Hicks8335eaf2012-09-13 12:00:56 -0700627 if (target_inode)
628 fsstack_copy_attr_all(target_inode,
629 ecryptfs_inode_to_lower(target_inode));
David Howells2b0143b2015-03-17 22:25:59 +0000630 fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700631 if (new_dir != old_dir)
David Howells2b0143b2015-03-17 22:25:59 +0000632 fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700633out_lock:
Michael Halcrow237fead2006-10-04 02:16:22 -0700634 dput(lower_new_dentry);
Al Virobcf0d9d2019-11-03 12:07:15 -0500635 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700636 return rc;
637}
638
Al Virob22e8fe2013-11-29 22:51:47 -0500639static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
Michael Halcrow237fead2006-10-04 02:16:22 -0700640{
Miklos Szeredi6c988f52016-12-09 16:45:03 +0100641 DEFINE_DELAYED_CALL(done);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500642 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
Miklos Szeredi6c988f52016-12-09 16:45:03 +0100643 const char *link;
Al Virob22e8fe2013-11-29 22:51:47 -0500644 char *buf;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800645 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700646
Miklos Szeredi6c988f52016-12-09 16:45:03 +0100647 link = vfs_get_link(lower_dentry, &done);
648 if (IS_ERR(link))
649 return ERR_CAST(link);
650
Al Virob22e8fe2013-11-29 22:51:47 -0500651 rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
Miklos Szeredi6c988f52016-12-09 16:45:03 +0100652 link, strlen(link));
653 do_delayed_call(&done);
654 if (rc)
655 return ERR_PTR(rc);
656
657 return buf;
Tyler Hicks3a60a162010-03-22 00:41:35 -0500658}
659
Al Viro6b255392015-11-17 10:20:54 -0500660static const char *ecryptfs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500661 struct inode *inode,
662 struct delayed_call *done)
Michael Halcrow237fead2006-10-04 02:16:22 -0700663{
Al Virob22e8fe2013-11-29 22:51:47 -0500664 size_t len;
Al Viro6b255392015-11-17 10:20:54 -0500665 char *buf;
666
667 if (!dentry)
668 return ERR_PTR(-ECHILD);
669
670 buf = ecryptfs_readlink_lower(dentry, &len);
Al Virob22e8fe2013-11-29 22:51:47 -0500671 if (IS_ERR(buf))
Al Viro680baac2015-05-02 13:32:22 -0400672 return buf;
David Howells2b0143b2015-03-17 22:25:59 +0000673 fsstack_copy_attr_atime(d_inode(dentry),
674 d_inode(ecryptfs_dentry_to_lower(dentry)));
Al Viro408bd622012-05-03 09:34:20 -0400675 buf[len] = '\0';
Al Virofceef392015-12-29 15:58:39 -0500676 set_delayed_call(done, kfree_link, buf);
677 return buf;
Michael Halcrow237fead2006-10-04 02:16:22 -0700678}
679
Michael Halcrow237fead2006-10-04 02:16:22 -0700680/**
681 * upper_size_to_lower_size
682 * @crypt_stat: Crypt_stat associated with file
683 * @upper_size: Size of the upper file
684 *
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800685 * Calculate the required size of the lower file based on the
Michael Halcrow237fead2006-10-04 02:16:22 -0700686 * specified size of the upper file. This calculation is based on the
687 * number of headers in the underlying file and the extent size.
688 *
689 * Returns Calculated size of the lower file.
690 */
691static loff_t
692upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
693 loff_t upper_size)
694{
695 loff_t lower_size;
696
Tyler Hicks157f1072010-02-11 07:10:38 -0600697 lower_size = ecryptfs_lower_header_size(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700698 if (upper_size != 0) {
699 loff_t num_extents;
700
701 num_extents = upper_size >> crypt_stat->extent_shift;
702 if (upper_size & ~crypt_stat->extent_mask)
703 num_extents++;
704 lower_size += (num_extents * crypt_stat->extent_size);
705 }
706 return lower_size;
707}
708
709/**
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500710 * truncate_upper
Michael Halcrow237fead2006-10-04 02:16:22 -0700711 * @dentry: The ecryptfs layer dentry
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500712 * @ia: Address of the ecryptfs inode's attributes
713 * @lower_ia: Address of the lower inode's attributes
Michael Halcrow237fead2006-10-04 02:16:22 -0700714 *
715 * Function to handle truncations modifying the size of the file. Note
716 * that the file sizes are interpolated. When expanding, we are simply
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500717 * writing strings of 0's out. When truncating, we truncate the upper
718 * inode and update the lower_ia according to the page index
719 * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
720 * the caller must use lower_ia in a call to notify_change() to perform
721 * the truncation of the lower inode.
Michael Halcrow237fead2006-10-04 02:16:22 -0700722 *
723 * Returns zero on success; non-zero otherwise
724 */
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500725static int truncate_upper(struct dentry *dentry, struct iattr *ia,
726 struct iattr *lower_ia)
Michael Halcrow237fead2006-10-04 02:16:22 -0700727{
728 int rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +0000729 struct inode *inode = d_inode(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700730 struct ecryptfs_crypt_stat *crypt_stat;
731 loff_t i_size = i_size_read(inode);
732 loff_t lower_size_before_truncate;
733 loff_t lower_size_after_truncate;
734
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500735 if (unlikely((ia->ia_size == i_size))) {
736 lower_ia->ia_valid &= ~ATTR_SIZE;
Tyler Hicks332ab162011-04-14 15:35:11 -0500737 return 0;
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500738 }
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500739 rc = ecryptfs_get_lower_file(dentry, inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500740 if (rc)
741 return rc;
David Howells2b0143b2015-03-17 22:25:59 +0000742 crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700743 /* Switch on growing or shrinking file */
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500744 if (ia->ia_size > i_size) {
Michael Halcrow2ed92552007-10-16 01:28:10 -0700745 char zero[] = { 0x00 };
Michael Halcrow240e2df2007-06-27 14:09:44 -0700746
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500747 lower_ia->ia_valid &= ~ATTR_SIZE;
Michael Halcrow2ed92552007-10-16 01:28:10 -0700748 /* Write a single 0 at the last position of the file;
749 * this triggers code that will fill in 0's throughout
750 * the intermediate portion of the previous end of the
751 * file and the new and of the file */
Al Viro48c1e442010-05-21 11:09:58 -0400752 rc = ecryptfs_write(inode, zero,
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500753 (ia->ia_size - 1), 1);
754 } else { /* ia->ia_size < i_size_read(inode) */
755 /* We're chopping off all the pages down to the page
756 * in which ia->ia_size is located. Fill in the end of
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300757 * that page from (ia->ia_size & ~PAGE_MASK) to
758 * PAGE_SIZE with zeros. */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300759 size_t num_zeros = (PAGE_SIZE
760 - (ia->ia_size & ~PAGE_MASK));
Michael Halcrow2ed92552007-10-16 01:28:10 -0700761
Tyler Hicks13a791b2009-04-13 15:29:27 -0500762 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200763 truncate_setsize(inode, ia->ia_size);
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500764 lower_ia->ia_size = ia->ia_size;
765 lower_ia->ia_valid |= ATTR_SIZE;
Al Viro48c1e442010-05-21 11:09:58 -0400766 goto out;
Tyler Hicks13a791b2009-04-13 15:29:27 -0500767 }
Michael Halcrow2ed92552007-10-16 01:28:10 -0700768 if (num_zeros) {
769 char *zeros_virt;
770
771 zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
772 if (!zeros_virt) {
773 rc = -ENOMEM;
Al Viro48c1e442010-05-21 11:09:58 -0400774 goto out;
Michael Halcrow2ed92552007-10-16 01:28:10 -0700775 }
Al Viro48c1e442010-05-21 11:09:58 -0400776 rc = ecryptfs_write(inode, zeros_virt,
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500777 ia->ia_size, num_zeros);
Michael Halcrow2ed92552007-10-16 01:28:10 -0700778 kfree(zeros_virt);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700779 if (rc) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700780 printk(KERN_ERR "Error attempting to zero out "
781 "the remainder of the end page on "
782 "reducing truncate; rc = [%d]\n", rc);
Al Viro48c1e442010-05-21 11:09:58 -0400783 goto out;
Michael Halcrow240e2df2007-06-27 14:09:44 -0700784 }
785 }
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200786 truncate_setsize(inode, ia->ia_size);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700787 rc = ecryptfs_write_inode_size_to_metadata(inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800788 if (rc) {
789 printk(KERN_ERR "Problem with "
790 "ecryptfs_write_inode_size_to_metadata; "
791 "rc = [%d]\n", rc);
Al Viro48c1e442010-05-21 11:09:58 -0400792 goto out;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800793 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700794 /* We are reducing the size of the ecryptfs file, and need to
795 * know if we need to reduce the size of the lower file. */
796 lower_size_before_truncate =
797 upper_size_to_lower_size(crypt_stat, i_size);
798 lower_size_after_truncate =
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500799 upper_size_to_lower_size(crypt_stat, ia->ia_size);
800 if (lower_size_after_truncate < lower_size_before_truncate) {
801 lower_ia->ia_size = lower_size_after_truncate;
802 lower_ia->ia_valid |= ATTR_SIZE;
803 } else
804 lower_ia->ia_valid &= ~ATTR_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700805 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700806out:
Tyler Hicks332ab162011-04-14 15:35:11 -0500807 ecryptfs_put_lower_file(inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700808 return rc;
809}
810
Tyler Hicksa261a032012-01-19 20:33:44 -0600811static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
812{
813 struct ecryptfs_crypt_stat *crypt_stat;
814 loff_t lower_oldsize, lower_newsize;
815
816 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
817 lower_oldsize = upper_size_to_lower_size(crypt_stat,
818 i_size_read(inode));
819 lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
820 if (lower_newsize > lower_oldsize) {
821 /*
822 * The eCryptfs inode and the new *lower* size are mixed here
823 * because we may not have the lower i_mutex held and/or it may
824 * not be appropriate to call inode_newsize_ok() with inodes
825 * from other filesystems.
826 */
827 return inode_newsize_ok(inode, lower_newsize);
828 }
829
830 return 0;
831}
832
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500833/**
834 * ecryptfs_truncate
835 * @dentry: The ecryptfs layer dentry
836 * @new_length: The length to expand the file to
837 *
838 * Simple function that handles the truncation of an eCryptfs inode and
839 * its corresponding lower inode.
840 *
841 * Returns zero on success; non-zero otherwise
842 */
843int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
844{
845 struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
846 struct iattr lower_ia = { .ia_valid = 0 };
847 int rc;
848
David Howells2b0143b2015-03-17 22:25:59 +0000849 rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
Tyler Hicksa261a032012-01-19 20:33:44 -0600850 if (rc)
851 return rc;
852
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500853 rc = truncate_upper(dentry, &ia, &lower_ia);
854 if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
855 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
856
Al Viro59551022016-01-22 15:40:57 -0500857 inode_lock(d_inode(lower_dentry));
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400858 rc = notify_change(lower_dentry, &lower_ia, NULL);
Al Viro59551022016-01-22 15:40:57 -0500859 inode_unlock(d_inode(lower_dentry));
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500860 }
861 return rc;
862}
863
Michael Halcrow237fead2006-10-04 02:16:22 -0700864static int
Al Viro10556cb2011-06-20 19:28:19 -0400865ecryptfs_permission(struct inode *inode, int mask)
Michael Halcrow237fead2006-10-04 02:16:22 -0700866{
Al Virof419a2e2008-07-22 00:07:17 -0400867 return inode_permission(ecryptfs_inode_to_lower(inode), mask);
Michael Halcrow237fead2006-10-04 02:16:22 -0700868}
869
870/**
871 * ecryptfs_setattr
872 * @dentry: dentry handle to the inode to modify
873 * @ia: Structure with flags of what to change and values
874 *
875 * Updates the metadata of an inode. If the update is to the size
876 * i.e. truncation, then ecryptfs_truncate will handle the size modification
877 * of both the ecryptfs inode and the lower inode.
878 *
879 * All other metadata changes will be passed right to the lower filesystem,
880 * and we will just update our inode to look like the lower.
881 */
882static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
883{
884 int rc = 0;
885 struct dentry *lower_dentry;
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500886 struct iattr lower_ia;
Michael Halcrow237fead2006-10-04 02:16:22 -0700887 struct inode *inode;
888 struct inode *lower_inode;
889 struct ecryptfs_crypt_stat *crypt_stat;
890
David Howells2b0143b2015-03-17 22:25:59 +0000891 crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
Herbert Xue81f3342016-04-16 15:01:09 +0800892 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
893 rc = ecryptfs_init_crypt_stat(crypt_stat);
894 if (rc)
895 return rc;
896 }
David Howells2b0143b2015-03-17 22:25:59 +0000897 inode = d_inode(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700898 lower_inode = ecryptfs_inode_to_lower(inode);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700899 lower_dentry = ecryptfs_dentry_to_lower(dentry);
900 mutex_lock(&crypt_stat->cs_mutex);
David Howellse36cb0b2015-01-29 12:02:35 +0000901 if (d_is_dir(dentry))
Michael Halcrowe10f2812007-06-27 14:09:44 -0700902 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
David Howellse36cb0b2015-01-29 12:02:35 +0000903 else if (d_is_reg(dentry)
Michael Halcrow64ee4802007-07-19 01:47:54 -0700904 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
905 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700906 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrowe10f2812007-06-27 14:09:44 -0700907
Michael Halcrowe10f2812007-06-27 14:09:44 -0700908 mount_crypt_stat = &ecryptfs_superblock_to_private(
909 dentry->d_sb)->mount_crypt_stat;
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500910 rc = ecryptfs_get_lower_file(dentry, inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500911 if (rc) {
912 mutex_unlock(&crypt_stat->cs_mutex);
913 goto out;
914 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700915 rc = ecryptfs_read_metadata(dentry);
Tyler Hicks332ab162011-04-14 15:35:11 -0500916 ecryptfs_put_lower_file(inode);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700917 if (rc) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700918 if (!(mount_crypt_stat->flags
919 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
920 rc = -EIO;
Michael Halcrow25bd8172008-02-06 01:38:35 -0800921 printk(KERN_WARNING "Either the lower file "
Michael Halcrowe10f2812007-06-27 14:09:44 -0700922 "is not in a valid eCryptfs format, "
Michael Halcrow25bd8172008-02-06 01:38:35 -0800923 "or the key could not be retrieved. "
924 "Plaintext passthrough mode is not "
Michael Halcrowe10f2812007-06-27 14:09:44 -0700925 "enabled; returning -EIO\n");
Michael Halcrowe10f2812007-06-27 14:09:44 -0700926 mutex_unlock(&crypt_stat->cs_mutex);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700927 goto out;
928 }
929 rc = 0;
Tyler Hicks3aeb86e2011-03-15 14:54:00 -0500930 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
931 | ECRYPTFS_ENCRYPTED);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700932 }
Michael Halcrowe10f2812007-06-27 14:09:44 -0700933 }
934 mutex_unlock(&crypt_stat->cs_mutex);
Tyler Hicksa261a032012-01-19 20:33:44 -0600935
Jan Kara31051c82016-05-26 16:55:18 +0200936 rc = setattr_prepare(dentry, ia);
Tyler Hicksa261a032012-01-19 20:33:44 -0600937 if (rc)
938 goto out;
939 if (ia->ia_valid & ATTR_SIZE) {
940 rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
941 if (rc)
942 goto out;
943 }
944
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500945 memcpy(&lower_ia, ia, sizeof(lower_ia));
946 if (ia->ia_valid & ATTR_FILE)
947 lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
Michael Halcrow237fead2006-10-04 02:16:22 -0700948 if (ia->ia_valid & ATTR_SIZE) {
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500949 rc = truncate_upper(dentry, ia, &lower_ia);
Michael Halcrow237fead2006-10-04 02:16:22 -0700950 if (rc < 0)
951 goto out;
952 }
Jeff Layton1ac564e2007-10-18 03:05:17 -0700953
954 /*
955 * mode change is for clearing setuid/setgid bits. Allow lower fs
956 * to interpret this in its own way.
957 */
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500958 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
959 lower_ia.ia_valid &= ~ATTR_MODE;
Jeff Layton1ac564e2007-10-18 03:05:17 -0700960
Al Viro59551022016-01-22 15:40:57 -0500961 inode_lock(d_inode(lower_dentry));
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400962 rc = notify_change(lower_dentry, &lower_ia, NULL);
Al Viro59551022016-01-22 15:40:57 -0500963 inode_unlock(d_inode(lower_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -0700964out:
Erez Zadok9afa2fb2009-12-02 19:51:54 -0500965 fsstack_copy_attr_all(inode, lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700966 return rc;
967}
968
David Howellsa528d352017-01-31 16:46:22 +0000969static int ecryptfs_getattr_link(const struct path *path, struct kstat *stat,
970 u32 request_mask, unsigned int flags)
Tyler Hicks3a60a162010-03-22 00:41:35 -0500971{
David Howellsa528d352017-01-31 16:46:22 +0000972 struct dentry *dentry = path->dentry;
Tyler Hicks3a60a162010-03-22 00:41:35 -0500973 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
974 int rc = 0;
975
976 mount_crypt_stat = &ecryptfs_superblock_to_private(
977 dentry->d_sb)->mount_crypt_stat;
David Howells2b0143b2015-03-17 22:25:59 +0000978 generic_fillattr(d_inode(dentry), stat);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500979 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
980 char *target;
981 size_t targetsiz;
982
Al Virob22e8fe2013-11-29 22:51:47 -0500983 target = ecryptfs_readlink_lower(dentry, &targetsiz);
984 if (!IS_ERR(target)) {
Tyler Hicks3a60a162010-03-22 00:41:35 -0500985 kfree(target);
986 stat->size = targetsiz;
Al Virob22e8fe2013-11-29 22:51:47 -0500987 } else {
988 rc = PTR_ERR(target);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500989 }
990 }
991 return rc;
992}
993
David Howellsa528d352017-01-31 16:46:22 +0000994static int ecryptfs_getattr(const struct path *path, struct kstat *stat,
995 u32 request_mask, unsigned int flags)
Tyler Hicksf8f484d2009-11-04 02:48:01 -0600996{
David Howellsa528d352017-01-31 16:46:22 +0000997 struct dentry *dentry = path->dentry;
Tyler Hicksf8f484d2009-11-04 02:48:01 -0600998 struct kstat lower_stat;
999 int rc;
1000
David Howellsa528d352017-01-31 16:46:22 +00001001 rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat,
1002 request_mask, flags);
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001003 if (!rc) {
David Howells2b0143b2015-03-17 22:25:59 +00001004 fsstack_copy_attr_all(d_inode(dentry),
1005 ecryptfs_inode_to_lower(d_inode(dentry)));
1006 generic_fillattr(d_inode(dentry), stat);
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001007 stat->blocks = lower_stat.blocks;
1008 }
1009 return rc;
1010}
1011
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001012int
Al Viro3767e252016-05-27 11:06:05 -04001013ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
1014 const char *name, const void *value,
Michael Halcrow237fead2006-10-04 02:16:22 -07001015 size_t size, int flags)
1016{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001017 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -07001018 struct dentry *lower_dentry;
1019
1020 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001021 if (!(d_inode(lower_dentry)->i_opflags & IOP_XATTR)) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001022 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001023 goto out;
1024 }
Roberto Sassu48b512e2010-10-05 18:53:45 +02001025 rc = vfs_setxattr(lower_dentry, name, value, size, flags);
Al Viro3767e252016-05-27 11:06:05 -04001026 if (!rc && inode)
1027 fsstack_copy_attr_all(inode, d_inode(lower_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -07001028out:
1029 return rc;
1030}
1031
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001032ssize_t
Al Viroce23e642016-04-11 00:48:00 -04001033ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
1034 const char *name, void *value, size_t size)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001035{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001036 int rc;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001037
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001038 if (!(lower_inode->i_opflags & IOP_XATTR)) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001039 rc = -EOPNOTSUPP;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001040 goto out;
1041 }
Al Viroce23e642016-04-11 00:48:00 -04001042 inode_lock(lower_inode);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001043 rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size);
Al Viroce23e642016-04-11 00:48:00 -04001044 inode_unlock(lower_inode);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001045out:
1046 return rc;
1047}
1048
Adrian Bunk7896b632008-02-06 01:38:32 -08001049static ssize_t
Al Viroce23e642016-04-11 00:48:00 -04001050ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
1051 const char *name, void *value, size_t size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001052{
Al Viroce23e642016-04-11 00:48:00 -04001053 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1054 ecryptfs_inode_to_lower(inode),
1055 name, value, size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001056}
1057
1058static ssize_t
1059ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1060{
1061 int rc = 0;
1062 struct dentry *lower_dentry;
1063
1064 lower_dentry = ecryptfs_dentry_to_lower(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001065 if (!d_inode(lower_dentry)->i_op->listxattr) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001066 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001067 goto out;
1068 }
Al Viro59551022016-01-22 15:40:57 -05001069 inode_lock(d_inode(lower_dentry));
David Howells2b0143b2015-03-17 22:25:59 +00001070 rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
Al Viro59551022016-01-22 15:40:57 -05001071 inode_unlock(d_inode(lower_dentry));
Michael Halcrow237fead2006-10-04 02:16:22 -07001072out:
1073 return rc;
1074}
1075
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001076static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
1077 const char *name)
Michael Halcrow237fead2006-10-04 02:16:22 -07001078{
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001079 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -07001080 struct dentry *lower_dentry;
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001081 struct inode *lower_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -07001082
1083 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001084 lower_inode = ecryptfs_inode_to_lower(inode);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001085 if (!(lower_inode->i_opflags & IOP_XATTR)) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001086 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001087 goto out;
1088 }
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001089 inode_lock(lower_inode);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001090 rc = __vfs_removexattr(lower_dentry, name);
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001091 inode_unlock(lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -07001092out:
1093 return rc;
1094}
1095
Arjan van de Ven754661f2007-02-12 00:55:38 -08001096const struct inode_operations ecryptfs_symlink_iops = {
Al Viro6b255392015-11-17 10:20:54 -05001097 .get_link = ecryptfs_get_link,
Michael Halcrow237fead2006-10-04 02:16:22 -07001098 .permission = ecryptfs_permission,
1099 .setattr = ecryptfs_setattr,
Tyler Hicks3a60a162010-03-22 00:41:35 -05001100 .getattr = ecryptfs_getattr_link,
Michael Halcrow237fead2006-10-04 02:16:22 -07001101 .listxattr = ecryptfs_listxattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001102};
1103
Arjan van de Ven754661f2007-02-12 00:55:38 -08001104const struct inode_operations ecryptfs_dir_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001105 .create = ecryptfs_create,
1106 .lookup = ecryptfs_lookup,
1107 .link = ecryptfs_link,
1108 .unlink = ecryptfs_unlink,
1109 .symlink = ecryptfs_symlink,
1110 .mkdir = ecryptfs_mkdir,
1111 .rmdir = ecryptfs_rmdir,
1112 .mknod = ecryptfs_mknod,
1113 .rename = ecryptfs_rename,
1114 .permission = ecryptfs_permission,
1115 .setattr = ecryptfs_setattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001116 .listxattr = ecryptfs_listxattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001117};
1118
Arjan van de Ven754661f2007-02-12 00:55:38 -08001119const struct inode_operations ecryptfs_main_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001120 .permission = ecryptfs_permission,
1121 .setattr = ecryptfs_setattr,
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001122 .getattr = ecryptfs_getattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001123 .listxattr = ecryptfs_listxattr,
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001124};
1125
1126static int ecryptfs_xattr_get(const struct xattr_handler *handler,
1127 struct dentry *dentry, struct inode *inode,
1128 const char *name, void *buffer, size_t size)
1129{
1130 return ecryptfs_getxattr(dentry, inode, name, buffer, size);
1131}
1132
1133static int ecryptfs_xattr_set(const struct xattr_handler *handler,
1134 struct dentry *dentry, struct inode *inode,
1135 const char *name, const void *value, size_t size,
1136 int flags)
1137{
1138 if (value)
1139 return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
1140 else {
1141 BUG_ON(flags != XATTR_REPLACE);
1142 return ecryptfs_removexattr(dentry, inode, name);
1143 }
1144}
1145
YueHaibingc0360612019-06-14 23:51:17 +08001146static const struct xattr_handler ecryptfs_xattr_handler = {
Andreas Gruenbacher4b899da2016-09-29 17:48:36 +02001147 .prefix = "", /* match anything */
1148 .get = ecryptfs_xattr_get,
1149 .set = ecryptfs_xattr_set,
1150};
1151
1152const struct xattr_handler *ecryptfs_xattr_handlers[] = {
1153 &ecryptfs_xattr_handler,
1154 NULL
Michael Halcrow237fead2006-10-04 02:16:22 -07001155};