1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * eCryptfs: Linux filesystem encryption layer
4 *
5 * Copyright (C) 1997-2004 Erez Zadok
6 * Copyright (C) 2001-2004 Stony Brook University
7 * Copyright (C) 2004-2007 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9 * Michael C. Thompsion <mcthomps@us.ibm.com>
10 */
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>
18 #include <linux/fs_stack.h>
19 #include <linux/slab.h>
20 #include <linux/xattr.h>
21 #include <linux/posix_acl.h>
22 #include <linux/posix_acl_xattr.h>
23 #include <linux/fileattr.h>
24 #include <linux/unaligned.h>
25 #include "ecryptfs_kernel.h"
26
lock_parent(struct dentry * dentry,struct dentry ** lower_dentry,struct inode ** lower_dir)27 static int lock_parent(struct dentry *dentry,
28 struct dentry **lower_dentry,
29 struct inode **lower_dir)
30 {
31 struct dentry *lower_dir_dentry;
32
33 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
34 *lower_dir = d_inode(lower_dir_dentry);
35 *lower_dentry = ecryptfs_dentry_to_lower(dentry);
36
37 inode_lock_nested(*lower_dir, I_MUTEX_PARENT);
38 return (*lower_dentry)->d_parent == lower_dir_dentry ? 0 : -EINVAL;
39 }
40
ecryptfs_inode_test(struct inode * inode,void * lower_inode)41 static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
42 {
43 return ecryptfs_inode_to_lower(inode) == lower_inode;
44 }
45
ecryptfs_inode_set(struct inode * inode,void * opaque)46 static int ecryptfs_inode_set(struct inode *inode, void *opaque)
47 {
48 struct inode *lower_inode = opaque;
49
50 ecryptfs_set_inode_lower(inode, lower_inode);
51 fsstack_copy_attr_all(inode, lower_inode);
52 /* i_size will be overwritten for encrypted regular files */
53 fsstack_copy_inode_size(inode, lower_inode);
54 inode->i_ino = lower_inode->i_ino;
55 inode->i_mapping->a_ops = &ecryptfs_aops;
56
57 if (S_ISLNK(inode->i_mode))
58 inode->i_op = &ecryptfs_symlink_iops;
59 else if (S_ISDIR(inode->i_mode))
60 inode->i_op = &ecryptfs_dir_iops;
61 else
62 inode->i_op = &ecryptfs_main_iops;
63
64 if (S_ISDIR(inode->i_mode))
65 inode->i_fop = &ecryptfs_dir_fops;
66 else if (special_file(inode->i_mode))
67 init_special_inode(inode, inode->i_mode, inode->i_rdev);
68 else
69 inode->i_fop = &ecryptfs_main_fops;
70
71 return 0;
72 }
73
__ecryptfs_get_inode(struct inode * lower_inode,struct super_block * sb)74 static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
75 struct super_block *sb)
76 {
77 struct inode *inode;
78
79 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
80 return ERR_PTR(-EXDEV);
81
82 /* Reject dealing with casefold directories. */
83 if (IS_CASEFOLDED(lower_inode)) {
84 pr_err_ratelimited("%s: Can't handle casefolded directory.\n",
85 __func__);
86 return ERR_PTR(-EREMOTE);
87 }
88
89 if (!igrab(lower_inode))
90 return ERR_PTR(-ESTALE);
91 inode = iget5_locked(sb, (unsigned long)lower_inode,
92 ecryptfs_inode_test, ecryptfs_inode_set,
93 lower_inode);
94 if (!inode) {
95 iput(lower_inode);
96 return ERR_PTR(-EACCES);
97 }
98 if (!(inode->i_state & I_NEW))
99 iput(lower_inode);
100
101 return inode;
102 }
103
ecryptfs_get_inode(struct inode * lower_inode,struct super_block * sb)104 struct inode *ecryptfs_get_inode(struct inode *lower_inode,
105 struct super_block *sb)
106 {
107 struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
108
109 if (!IS_ERR(inode) && (inode->i_state & I_NEW))
110 unlock_new_inode(inode);
111
112 return inode;
113 }
114
115 /**
116 * ecryptfs_interpose
117 * @lower_dentry: Existing dentry in the lower filesystem
118 * @dentry: ecryptfs' dentry
119 * @sb: ecryptfs's super_block
120 *
121 * Interposes upper and lower dentries.
122 *
123 * Returns zero on success; non-zero otherwise
124 */
ecryptfs_interpose(struct dentry * lower_dentry,struct dentry * dentry,struct super_block * sb)125 static int ecryptfs_interpose(struct dentry *lower_dentry,
126 struct dentry *dentry, struct super_block *sb)
127 {
128 struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
129
130 if (IS_ERR(inode))
131 return PTR_ERR(inode);
132 d_instantiate(dentry, inode);
133
134 return 0;
135 }
136
ecryptfs_do_unlink(struct inode * dir,struct dentry * dentry,struct inode * inode)137 static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
138 struct inode *inode)
139 {
140 struct dentry *lower_dentry;
141 struct inode *lower_dir;
142 int rc;
143
144 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
145 dget(lower_dentry); // don't even try to make the lower negative
146 if (!rc) {
147 if (d_unhashed(lower_dentry))
148 rc = -EINVAL;
149 else
150 rc = vfs_unlink(&nop_mnt_idmap, lower_dir, lower_dentry,
151 NULL);
152 }
153 if (rc) {
154 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
155 goto out_unlock;
156 }
157 fsstack_copy_attr_times(dir, lower_dir);
158 set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
159 inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
160 out_unlock:
161 dput(lower_dentry);
162 inode_unlock(lower_dir);
163 if (!rc)
164 d_drop(dentry);
165 return rc;
166 }
167
168 /**
169 * ecryptfs_do_create
170 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
171 * @ecryptfs_dentry: New file's dentry in ecryptfs
172 * @mode: The mode of the new file
173 *
174 * Creates the underlying file and the eCryptfs inode which will link to
175 * it. It will also update the eCryptfs directory inode to mimic the
176 * stat of the lower directory inode.
177 *
178 * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
179 */
180 static struct inode *
ecryptfs_do_create(struct inode * directory_inode,struct dentry * ecryptfs_dentry,umode_t mode)181 ecryptfs_do_create(struct inode *directory_inode,
182 struct dentry *ecryptfs_dentry, umode_t mode)
183 {
184 int rc;
185 struct dentry *lower_dentry;
186 struct inode *lower_dir;
187 struct inode *inode;
188
189 rc = lock_parent(ecryptfs_dentry, &lower_dentry, &lower_dir);
190 if (!rc)
191 rc = vfs_create(&nop_mnt_idmap, lower_dir,
192 lower_dentry, mode, true);
193 if (rc) {
194 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
195 "rc = [%d]\n", __func__, rc);
196 inode = ERR_PTR(rc);
197 goto out_lock;
198 }
199 inode = __ecryptfs_get_inode(d_inode(lower_dentry),
200 directory_inode->i_sb);
201 if (IS_ERR(inode)) {
202 vfs_unlink(&nop_mnt_idmap, lower_dir, lower_dentry, NULL);
203 goto out_lock;
204 }
205 fsstack_copy_attr_times(directory_inode, lower_dir);
206 fsstack_copy_inode_size(directory_inode, lower_dir);
207 out_lock:
208 inode_unlock(lower_dir);
209 return inode;
210 }
211
212 /*
213 * ecryptfs_initialize_file
214 *
215 * Cause the file to be changed from a basic empty file to an ecryptfs
216 * file with a header and first data page.
217 *
218 * Returns zero on success
219 */
ecryptfs_initialize_file(struct dentry * ecryptfs_dentry,struct inode * ecryptfs_inode)220 int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
221 struct inode *ecryptfs_inode)
222 {
223 struct ecryptfs_crypt_stat *crypt_stat =
224 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
225 int rc = 0;
226
227 if (S_ISDIR(ecryptfs_inode->i_mode)) {
228 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
229 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
230 goto out;
231 }
232 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
233 rc = ecryptfs_new_file_context(ecryptfs_inode);
234 if (rc) {
235 ecryptfs_printk(KERN_ERR, "Error creating new file "
236 "context; rc = [%d]\n", rc);
237 goto out;
238 }
239 rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
240 if (rc) {
241 printk(KERN_ERR "%s: Error attempting to initialize "
242 "the lower file for the dentry with name "
243 "[%pd]; rc = [%d]\n", __func__,
244 ecryptfs_dentry, rc);
245 goto out;
246 }
247 rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
248 if (rc)
249 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
250 ecryptfs_put_lower_file(ecryptfs_inode);
251 out:
252 return rc;
253 }
254
255 /*
256 * ecryptfs_create
257 * @mode: The mode of the new file.
258 *
259 * Creates a new file.
260 *
261 * Returns zero on success; non-zero on error condition
262 */
263 static int
ecryptfs_create(struct mnt_idmap * idmap,struct inode * directory_inode,struct dentry * ecryptfs_dentry,umode_t mode,bool excl)264 ecryptfs_create(struct mnt_idmap *idmap,
265 struct inode *directory_inode, struct dentry *ecryptfs_dentry,
266 umode_t mode, bool excl)
267 {
268 struct inode *ecryptfs_inode;
269 int rc;
270
271 ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
272 mode);
273 if (IS_ERR(ecryptfs_inode)) {
274 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
275 "lower filesystem\n");
276 rc = PTR_ERR(ecryptfs_inode);
277 goto out;
278 }
279 /* At this point, a file exists on "disk"; we need to make sure
280 * that this on disk file is prepared to be an ecryptfs file */
281 rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
282 if (rc) {
283 ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
284 ecryptfs_inode);
285 iget_failed(ecryptfs_inode);
286 goto out;
287 }
288 d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
289 out:
290 return rc;
291 }
292
ecryptfs_i_size_read(struct dentry * dentry,struct inode * inode)293 static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
294 {
295 struct ecryptfs_crypt_stat *crypt_stat;
296 int rc;
297
298 rc = ecryptfs_get_lower_file(dentry, inode);
299 if (rc) {
300 printk(KERN_ERR "%s: Error attempting to initialize "
301 "the lower file for the dentry with name "
302 "[%pd]; rc = [%d]\n", __func__,
303 dentry, rc);
304 return rc;
305 }
306
307 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
308 /* TODO: lock for crypt_stat comparison */
309 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
310 ecryptfs_set_default_sizes(crypt_stat);
311
312 rc = ecryptfs_read_and_validate_header_region(inode);
313 ecryptfs_put_lower_file(inode);
314 if (rc) {
315 rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
316 if (!rc)
317 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
318 }
319
320 /* Must return 0 to allow non-eCryptfs files to be looked up, too */
321 return 0;
322 }
323
324 /*
325 * ecryptfs_lookup_interpose - Dentry interposition for a lookup
326 */
ecryptfs_lookup_interpose(struct dentry * dentry,struct dentry * lower_dentry)327 static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
328 struct dentry *lower_dentry)
329 {
330 const struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
331 struct inode *inode, *lower_inode;
332 struct ecryptfs_dentry_info *dentry_info;
333 int rc = 0;
334
335 dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
336 if (!dentry_info) {
337 dput(lower_dentry);
338 return ERR_PTR(-ENOMEM);
339 }
340
341 fsstack_copy_attr_atime(d_inode(dentry->d_parent),
342 d_inode(path->dentry));
343 BUG_ON(!d_count(lower_dentry));
344
345 ecryptfs_set_dentry_private(dentry, dentry_info);
346 dentry_info->lower_path.mnt = mntget(path->mnt);
347 dentry_info->lower_path.dentry = lower_dentry;
348
349 /*
350 * negative dentry can go positive under us here - its parent is not
351 * locked. That's OK and that could happen just as we return from
352 * ecryptfs_lookup() anyway. Just need to be careful and fetch
353 * ->d_inode only once - it's not stable here.
354 */
355 lower_inode = READ_ONCE(lower_dentry->d_inode);
356
357 if (!lower_inode) {
358 /* We want to add because we couldn't find in lower */
359 d_add(dentry, NULL);
360 return NULL;
361 }
362 inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
363 if (IS_ERR(inode)) {
364 printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
365 __func__, PTR_ERR(inode));
366 return ERR_CAST(inode);
367 }
368 if (S_ISREG(inode->i_mode)) {
369 rc = ecryptfs_i_size_read(dentry, inode);
370 if (rc) {
371 make_bad_inode(inode);
372 return ERR_PTR(rc);
373 }
374 }
375
376 if (inode->i_state & I_NEW)
377 unlock_new_inode(inode);
378 return d_splice_alias(inode, dentry);
379 }
380
381 /**
382 * ecryptfs_lookup
383 * @ecryptfs_dir_inode: The eCryptfs directory inode
384 * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
385 * @flags: lookup flags
386 *
387 * Find a file on disk. If the file does not exist, then we'll add it to the
388 * dentry cache and continue on to read it from the disk.
389 */
ecryptfs_lookup(struct inode * ecryptfs_dir_inode,struct dentry * ecryptfs_dentry,unsigned int flags)390 static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
391 struct dentry *ecryptfs_dentry,
392 unsigned int flags)
393 {
394 char *encrypted_and_encoded_name = NULL;
395 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
396 struct dentry *lower_dir_dentry, *lower_dentry;
397 struct qstr qname = QSTR_INIT(ecryptfs_dentry->d_name.name,
398 ecryptfs_dentry->d_name.len);
399 struct dentry *res;
400 int rc = 0;
401
402 lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
403
404 mount_crypt_stat = &ecryptfs_superblock_to_private(
405 ecryptfs_dentry->d_sb)->mount_crypt_stat;
406 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
407 size_t len = qname.len;
408 rc = ecryptfs_encrypt_and_encode_filename(
409 &encrypted_and_encoded_name, &len,
410 mount_crypt_stat, qname.name, len);
411 if (rc) {
412 printk(KERN_ERR "%s: Error attempting to encrypt and encode "
413 "filename; rc = [%d]\n", __func__, rc);
414 return ERR_PTR(rc);
415 }
416 qname.name = encrypted_and_encoded_name;
417 qname.len = len;
418 }
419
420 lower_dentry = lookup_noperm_unlocked(&qname, lower_dir_dentry);
421 if (IS_ERR(lower_dentry)) {
422 ecryptfs_printk(KERN_DEBUG, "%s: lookup_noperm() returned "
423 "[%ld] on lower_dentry = [%s]\n", __func__,
424 PTR_ERR(lower_dentry),
425 qname.name);
426 res = ERR_CAST(lower_dentry);
427 } else {
428 res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
429 }
430 kfree(encrypted_and_encoded_name);
431 return res;
432 }
433
ecryptfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)434 static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
435 struct dentry *new_dentry)
436 {
437 struct dentry *lower_old_dentry;
438 struct dentry *lower_new_dentry;
439 struct inode *lower_dir;
440 u64 file_size_save;
441 int rc;
442
443 file_size_save = i_size_read(d_inode(old_dentry));
444 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
445 rc = lock_parent(new_dentry, &lower_new_dentry, &lower_dir);
446 if (!rc)
447 rc = vfs_link(lower_old_dentry, &nop_mnt_idmap, lower_dir,
448 lower_new_dentry, NULL);
449 if (rc || d_really_is_negative(lower_new_dentry))
450 goto out_lock;
451 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
452 if (rc)
453 goto out_lock;
454 fsstack_copy_attr_times(dir, lower_dir);
455 fsstack_copy_inode_size(dir, lower_dir);
456 set_nlink(d_inode(old_dentry),
457 ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
458 i_size_write(d_inode(new_dentry), file_size_save);
459 out_lock:
460 inode_unlock(lower_dir);
461 return rc;
462 }
463
ecryptfs_unlink(struct inode * dir,struct dentry * dentry)464 static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
465 {
466 return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
467 }
468
ecryptfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)469 static int ecryptfs_symlink(struct mnt_idmap *idmap,
470 struct inode *dir, struct dentry *dentry,
471 const char *symname)
472 {
473 int rc;
474 struct dentry *lower_dentry;
475 struct inode *lower_dir;
476 char *encoded_symname;
477 size_t encoded_symlen;
478 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
479
480 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
481 if (rc)
482 goto out_lock;
483 mount_crypt_stat = &ecryptfs_superblock_to_private(
484 dir->i_sb)->mount_crypt_stat;
485 rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
486 &encoded_symlen,
487 mount_crypt_stat, symname,
488 strlen(symname));
489 if (rc)
490 goto out_lock;
491 rc = vfs_symlink(&nop_mnt_idmap, lower_dir, lower_dentry,
492 encoded_symname);
493 kfree(encoded_symname);
494 if (rc || d_really_is_negative(lower_dentry))
495 goto out_lock;
496 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
497 if (rc)
498 goto out_lock;
499 fsstack_copy_attr_times(dir, lower_dir);
500 fsstack_copy_inode_size(dir, lower_dir);
501 out_lock:
502 inode_unlock(lower_dir);
503 if (d_really_is_negative(dentry))
504 d_drop(dentry);
505 return rc;
506 }
507
ecryptfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)508 static struct dentry *ecryptfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
509 struct dentry *dentry, umode_t mode)
510 {
511 int rc;
512 struct dentry *lower_dentry;
513 struct inode *lower_dir;
514
515 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
516 if (rc)
517 goto out;
518
519 lower_dentry = vfs_mkdir(&nop_mnt_idmap, lower_dir,
520 lower_dentry, mode);
521 rc = PTR_ERR(lower_dentry);
522 if (IS_ERR(lower_dentry))
523 goto out;
524 rc = 0;
525 if (d_unhashed(lower_dentry))
526 goto out;
527 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
528 if (rc)
529 goto out;
530 fsstack_copy_attr_times(dir, lower_dir);
531 fsstack_copy_inode_size(dir, lower_dir);
532 set_nlink(dir, lower_dir->i_nlink);
533 out:
534 inode_unlock(lower_dir);
535 if (d_really_is_negative(dentry))
536 d_drop(dentry);
537 return ERR_PTR(rc);
538 }
539
ecryptfs_rmdir(struct inode * dir,struct dentry * dentry)540 static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
541 {
542 struct dentry *lower_dentry;
543 struct inode *lower_dir;
544 int rc;
545
546 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
547 dget(lower_dentry); // don't even try to make the lower negative
548 if (!rc) {
549 if (d_unhashed(lower_dentry))
550 rc = -EINVAL;
551 else
552 rc = vfs_rmdir(&nop_mnt_idmap, lower_dir, lower_dentry);
553 }
554 if (!rc) {
555 clear_nlink(d_inode(dentry));
556 fsstack_copy_attr_times(dir, lower_dir);
557 set_nlink(dir, lower_dir->i_nlink);
558 }
559 dput(lower_dentry);
560 inode_unlock(lower_dir);
561 if (!rc)
562 d_drop(dentry);
563 return rc;
564 }
565
566 static int
ecryptfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)567 ecryptfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
568 struct dentry *dentry, umode_t mode, dev_t dev)
569 {
570 int rc;
571 struct dentry *lower_dentry;
572 struct inode *lower_dir;
573
574 rc = lock_parent(dentry, &lower_dentry, &lower_dir);
575 if (!rc)
576 rc = vfs_mknod(&nop_mnt_idmap, lower_dir,
577 lower_dentry, mode, dev);
578 if (rc || d_really_is_negative(lower_dentry))
579 goto out;
580 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
581 if (rc)
582 goto out;
583 fsstack_copy_attr_times(dir, lower_dir);
584 fsstack_copy_inode_size(dir, lower_dir);
585 out:
586 inode_unlock(lower_dir);
587 if (d_really_is_negative(dentry))
588 d_drop(dentry);
589 return rc;
590 }
591
592 static int
ecryptfs_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)593 ecryptfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
594 struct dentry *old_dentry, struct inode *new_dir,
595 struct dentry *new_dentry, unsigned int flags)
596 {
597 int rc;
598 struct dentry *lower_old_dentry;
599 struct dentry *lower_new_dentry;
600 struct dentry *lower_old_dir_dentry;
601 struct dentry *lower_new_dir_dentry;
602 struct dentry *trap;
603 struct inode *target_inode;
604 struct renamedata rd = {};
605
606 if (flags)
607 return -EINVAL;
608
609 lower_old_dir_dentry = ecryptfs_dentry_to_lower(old_dentry->d_parent);
610 lower_new_dir_dentry = ecryptfs_dentry_to_lower(new_dentry->d_parent);
611
612 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
613 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
614
615 target_inode = d_inode(new_dentry);
616
617 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
618 if (IS_ERR(trap))
619 return PTR_ERR(trap);
620 dget(lower_new_dentry);
621 rc = -EINVAL;
622 if (lower_old_dentry->d_parent != lower_old_dir_dentry)
623 goto out_lock;
624 if (lower_new_dentry->d_parent != lower_new_dir_dentry)
625 goto out_lock;
626 if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry))
627 goto out_lock;
628 /* source should not be ancestor of target */
629 if (trap == lower_old_dentry)
630 goto out_lock;
631 /* target should not be ancestor of source */
632 if (trap == lower_new_dentry) {
633 rc = -ENOTEMPTY;
634 goto out_lock;
635 }
636
637 rd.old_mnt_idmap = &nop_mnt_idmap;
638 rd.old_parent = lower_old_dir_dentry;
639 rd.old_dentry = lower_old_dentry;
640 rd.new_mnt_idmap = &nop_mnt_idmap;
641 rd.new_parent = lower_new_dir_dentry;
642 rd.new_dentry = lower_new_dentry;
643 rc = vfs_rename(&rd);
644 if (rc)
645 goto out_lock;
646 if (target_inode)
647 fsstack_copy_attr_all(target_inode,
648 ecryptfs_inode_to_lower(target_inode));
649 fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
650 if (new_dir != old_dir)
651 fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
652 out_lock:
653 dput(lower_new_dentry);
654 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
655 return rc;
656 }
657
ecryptfs_readlink_lower(struct dentry * dentry,size_t * bufsiz)658 static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
659 {
660 DEFINE_DELAYED_CALL(done);
661 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
662 const char *link;
663 char *buf;
664 int rc;
665
666 link = vfs_get_link(lower_dentry, &done);
667 if (IS_ERR(link))
668 return ERR_CAST(link);
669
670 rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
671 link, strlen(link));
672 do_delayed_call(&done);
673 if (rc)
674 return ERR_PTR(rc);
675
676 return buf;
677 }
678
ecryptfs_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)679 static const char *ecryptfs_get_link(struct dentry *dentry,
680 struct inode *inode,
681 struct delayed_call *done)
682 {
683 size_t len;
684 char *buf;
685
686 if (!dentry)
687 return ERR_PTR(-ECHILD);
688
689 buf = ecryptfs_readlink_lower(dentry, &len);
690 if (IS_ERR(buf))
691 return buf;
692 fsstack_copy_attr_atime(d_inode(dentry),
693 d_inode(ecryptfs_dentry_to_lower(dentry)));
694 buf[len] = '\0';
695 set_delayed_call(done, kfree_link, buf);
696 return buf;
697 }
698
699 /**
700 * upper_size_to_lower_size
701 * @crypt_stat: Crypt_stat associated with file
702 * @upper_size: Size of the upper file
703 *
704 * Calculate the required size of the lower file based on the
705 * specified size of the upper file. This calculation is based on the
706 * number of headers in the underlying file and the extent size.
707 *
708 * Returns Calculated size of the lower file.
709 */
710 static loff_t
upper_size_to_lower_size(struct ecryptfs_crypt_stat * crypt_stat,loff_t upper_size)711 upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
712 loff_t upper_size)
713 {
714 loff_t lower_size;
715
716 lower_size = ecryptfs_lower_header_size(crypt_stat);
717 if (upper_size != 0) {
718 loff_t num_extents;
719
720 num_extents = upper_size >> crypt_stat->extent_shift;
721 if (upper_size & ~crypt_stat->extent_mask)
722 num_extents++;
723 lower_size += (num_extents * crypt_stat->extent_size);
724 }
725 return lower_size;
726 }
727
728 /**
729 * truncate_upper
730 * @dentry: The ecryptfs layer dentry
731 * @ia: Address of the ecryptfs inode's attributes
732 * @lower_ia: Address of the lower inode's attributes
733 *
734 * Function to handle truncations modifying the size of the file. Note
735 * that the file sizes are interpolated. When expanding, we are simply
736 * writing strings of 0's out. When truncating, we truncate the upper
737 * inode and update the lower_ia according to the page index
738 * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
739 * the caller must use lower_ia in a call to notify_change() to perform
740 * the truncation of the lower inode.
741 *
742 * Returns zero on success; non-zero otherwise
743 */
truncate_upper(struct dentry * dentry,struct iattr * ia,struct iattr * lower_ia)744 static int truncate_upper(struct dentry *dentry, struct iattr *ia,
745 struct iattr *lower_ia)
746 {
747 int rc = 0;
748 struct inode *inode = d_inode(dentry);
749 struct ecryptfs_crypt_stat *crypt_stat;
750 loff_t i_size = i_size_read(inode);
751 loff_t lower_size_before_truncate;
752 loff_t lower_size_after_truncate;
753
754 if (unlikely((ia->ia_size == i_size))) {
755 lower_ia->ia_valid &= ~ATTR_SIZE;
756 return 0;
757 }
758 rc = ecryptfs_get_lower_file(dentry, inode);
759 if (rc)
760 return rc;
761 crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
762 /* Switch on growing or shrinking file */
763 if (ia->ia_size > i_size) {
764 char zero[] = { 0x00 };
765
766 lower_ia->ia_valid &= ~ATTR_SIZE;
767 /* Write a single 0 at the last position of the file;
768 * this triggers code that will fill in 0's throughout
769 * the intermediate portion of the previous end of the
770 * file and the new and of the file */
771 rc = ecryptfs_write(inode, zero,
772 (ia->ia_size - 1), 1);
773 } else { /* ia->ia_size < i_size_read(inode) */
774 /* We're chopping off all the pages down to the page
775 * in which ia->ia_size is located. Fill in the end of
776 * that page from (ia->ia_size & ~PAGE_MASK) to
777 * PAGE_SIZE with zeros. */
778 size_t num_zeros = (PAGE_SIZE
779 - (ia->ia_size & ~PAGE_MASK));
780
781 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
782 truncate_setsize(inode, ia->ia_size);
783 lower_ia->ia_size = ia->ia_size;
784 lower_ia->ia_valid |= ATTR_SIZE;
785 goto out;
786 }
787 if (num_zeros) {
788 char *zeros_virt;
789
790 zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
791 if (!zeros_virt) {
792 rc = -ENOMEM;
793 goto out;
794 }
795 rc = ecryptfs_write(inode, zeros_virt,
796 ia->ia_size, num_zeros);
797 kfree(zeros_virt);
798 if (rc) {
799 printk(KERN_ERR "Error attempting to zero out "
800 "the remainder of the end page on "
801 "reducing truncate; rc = [%d]\n", rc);
802 goto out;
803 }
804 }
805 truncate_setsize(inode, ia->ia_size);
806 rc = ecryptfs_write_inode_size_to_metadata(inode);
807 if (rc) {
808 printk(KERN_ERR "Problem with "
809 "ecryptfs_write_inode_size_to_metadata; "
810 "rc = [%d]\n", rc);
811 goto out;
812 }
813 /* We are reducing the size of the ecryptfs file, and need to
814 * know if we need to reduce the size of the lower file. */
815 lower_size_before_truncate =
816 upper_size_to_lower_size(crypt_stat, i_size);
817 lower_size_after_truncate =
818 upper_size_to_lower_size(crypt_stat, ia->ia_size);
819 if (lower_size_after_truncate < lower_size_before_truncate) {
820 lower_ia->ia_size = lower_size_after_truncate;
821 lower_ia->ia_valid |= ATTR_SIZE;
822 } else
823 lower_ia->ia_valid &= ~ATTR_SIZE;
824 }
825 out:
826 ecryptfs_put_lower_file(inode);
827 return rc;
828 }
829
ecryptfs_inode_newsize_ok(struct inode * inode,loff_t offset)830 static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
831 {
832 struct ecryptfs_crypt_stat *crypt_stat;
833 loff_t lower_oldsize, lower_newsize;
834
835 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
836 lower_oldsize = upper_size_to_lower_size(crypt_stat,
837 i_size_read(inode));
838 lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
839 if (lower_newsize > lower_oldsize) {
840 /*
841 * The eCryptfs inode and the new *lower* size are mixed here
842 * because we may not have the lower i_mutex held and/or it may
843 * not be appropriate to call inode_newsize_ok() with inodes
844 * from other filesystems.
845 */
846 return inode_newsize_ok(inode, lower_newsize);
847 }
848
849 return 0;
850 }
851
852 /**
853 * ecryptfs_truncate
854 * @dentry: The ecryptfs layer dentry
855 * @new_length: The length to expand the file to
856 *
857 * Simple function that handles the truncation of an eCryptfs inode and
858 * its corresponding lower inode.
859 *
860 * Returns zero on success; non-zero otherwise
861 */
ecryptfs_truncate(struct dentry * dentry,loff_t new_length)862 int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
863 {
864 struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
865 struct iattr lower_ia = { .ia_valid = 0 };
866 int rc;
867
868 rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
869 if (rc)
870 return rc;
871
872 rc = truncate_upper(dentry, &ia, &lower_ia);
873 if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
874 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
875
876 inode_lock(d_inode(lower_dentry));
877 rc = notify_change(&nop_mnt_idmap, lower_dentry,
878 &lower_ia, NULL);
879 inode_unlock(d_inode(lower_dentry));
880 }
881 return rc;
882 }
883
884 static int
ecryptfs_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)885 ecryptfs_permission(struct mnt_idmap *idmap, struct inode *inode,
886 int mask)
887 {
888 return inode_permission(&nop_mnt_idmap,
889 ecryptfs_inode_to_lower(inode), mask);
890 }
891
892 /**
893 * ecryptfs_setattr
894 * @idmap: idmap of the target mount
895 * @dentry: dentry handle to the inode to modify
896 * @ia: Structure with flags of what to change and values
897 *
898 * Updates the metadata of an inode. If the update is to the size
899 * i.e. truncation, then ecryptfs_truncate will handle the size modification
900 * of both the ecryptfs inode and the lower inode.
901 *
902 * All other metadata changes will be passed right to the lower filesystem,
903 * and we will just update our inode to look like the lower.
904 */
ecryptfs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * ia)905 static int ecryptfs_setattr(struct mnt_idmap *idmap,
906 struct dentry *dentry, struct iattr *ia)
907 {
908 int rc = 0;
909 struct dentry *lower_dentry;
910 struct iattr lower_ia;
911 struct inode *inode;
912 struct inode *lower_inode;
913 struct ecryptfs_crypt_stat *crypt_stat;
914
915 crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
916 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
917 rc = ecryptfs_init_crypt_stat(crypt_stat);
918 if (rc)
919 return rc;
920 }
921 inode = d_inode(dentry);
922 lower_inode = ecryptfs_inode_to_lower(inode);
923 lower_dentry = ecryptfs_dentry_to_lower(dentry);
924 mutex_lock(&crypt_stat->cs_mutex);
925 if (d_is_dir(dentry))
926 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
927 else if (d_is_reg(dentry)
928 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
929 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
930 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
931
932 mount_crypt_stat = &ecryptfs_superblock_to_private(
933 dentry->d_sb)->mount_crypt_stat;
934 rc = ecryptfs_get_lower_file(dentry, inode);
935 if (rc) {
936 mutex_unlock(&crypt_stat->cs_mutex);
937 goto out;
938 }
939 rc = ecryptfs_read_metadata(dentry);
940 ecryptfs_put_lower_file(inode);
941 if (rc) {
942 if (!(mount_crypt_stat->flags
943 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
944 rc = -EIO;
945 printk(KERN_WARNING "Either the lower file "
946 "is not in a valid eCryptfs format, "
947 "or the key could not be retrieved. "
948 "Plaintext passthrough mode is not "
949 "enabled; returning -EIO\n");
950 mutex_unlock(&crypt_stat->cs_mutex);
951 goto out;
952 }
953 rc = 0;
954 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
955 | ECRYPTFS_ENCRYPTED);
956 }
957 }
958 mutex_unlock(&crypt_stat->cs_mutex);
959
960 rc = setattr_prepare(&nop_mnt_idmap, dentry, ia);
961 if (rc)
962 goto out;
963 if (ia->ia_valid & ATTR_SIZE) {
964 rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
965 if (rc)
966 goto out;
967 }
968
969 memcpy(&lower_ia, ia, sizeof(lower_ia));
970 if (ia->ia_valid & ATTR_FILE)
971 lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
972 if (ia->ia_valid & ATTR_SIZE) {
973 rc = truncate_upper(dentry, ia, &lower_ia);
974 if (rc < 0)
975 goto out;
976 }
977
978 /*
979 * mode change is for clearing setuid/setgid bits. Allow lower fs
980 * to interpret this in its own way.
981 */
982 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
983 lower_ia.ia_valid &= ~ATTR_MODE;
984
985 inode_lock(d_inode(lower_dentry));
986 rc = notify_change(&nop_mnt_idmap, lower_dentry, &lower_ia, NULL);
987 inode_unlock(d_inode(lower_dentry));
988 out:
989 fsstack_copy_attr_all(inode, lower_inode);
990 return rc;
991 }
992
ecryptfs_getattr_link(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)993 static int ecryptfs_getattr_link(struct mnt_idmap *idmap,
994 const struct path *path, struct kstat *stat,
995 u32 request_mask, unsigned int flags)
996 {
997 struct dentry *dentry = path->dentry;
998 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
999 int rc = 0;
1000
1001 mount_crypt_stat = &ecryptfs_superblock_to_private(
1002 dentry->d_sb)->mount_crypt_stat;
1003 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
1004 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
1005 char *target;
1006 size_t targetsiz;
1007
1008 target = ecryptfs_readlink_lower(dentry, &targetsiz);
1009 if (!IS_ERR(target)) {
1010 kfree(target);
1011 stat->size = targetsiz;
1012 } else {
1013 rc = PTR_ERR(target);
1014 }
1015 }
1016 return rc;
1017 }
1018
ecryptfs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)1019 static int ecryptfs_getattr(struct mnt_idmap *idmap,
1020 const struct path *path, struct kstat *stat,
1021 u32 request_mask, unsigned int flags)
1022 {
1023 struct dentry *dentry = path->dentry;
1024 struct kstat lower_stat;
1025 int rc;
1026
1027 rc = vfs_getattr_nosec(ecryptfs_dentry_to_lower_path(dentry),
1028 &lower_stat, request_mask, flags);
1029 if (!rc) {
1030 fsstack_copy_attr_all(d_inode(dentry),
1031 ecryptfs_inode_to_lower(d_inode(dentry)));
1032 generic_fillattr(&nop_mnt_idmap, request_mask,
1033 d_inode(dentry), stat);
1034 stat->blocks = lower_stat.blocks;
1035 }
1036 return rc;
1037 }
1038
1039 int
ecryptfs_setxattr(struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)1040 ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
1041 const char *name, const void *value,
1042 size_t size, int flags)
1043 {
1044 int rc;
1045 struct dentry *lower_dentry;
1046 struct inode *lower_inode;
1047
1048 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1049 lower_inode = d_inode(lower_dentry);
1050 if (!(lower_inode->i_opflags & IOP_XATTR)) {
1051 rc = -EOPNOTSUPP;
1052 goto out;
1053 }
1054 inode_lock(lower_inode);
1055 rc = __vfs_setxattr_locked(&nop_mnt_idmap, lower_dentry, name, value, size, flags, NULL);
1056 inode_unlock(lower_inode);
1057 if (!rc && inode)
1058 fsstack_copy_attr_all(inode, lower_inode);
1059 out:
1060 return rc;
1061 }
1062
1063 ssize_t
ecryptfs_getxattr_lower(struct dentry * lower_dentry,struct inode * lower_inode,const char * name,void * value,size_t size)1064 ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
1065 const char *name, void *value, size_t size)
1066 {
1067 int rc;
1068
1069 if (!(lower_inode->i_opflags & IOP_XATTR)) {
1070 rc = -EOPNOTSUPP;
1071 goto out;
1072 }
1073 inode_lock(lower_inode);
1074 rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size);
1075 inode_unlock(lower_inode);
1076 out:
1077 return rc;
1078 }
1079
1080 static ssize_t
ecryptfs_getxattr(struct dentry * dentry,struct inode * inode,const char * name,void * value,size_t size)1081 ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
1082 const char *name, void *value, size_t size)
1083 {
1084 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1085 ecryptfs_inode_to_lower(inode),
1086 name, value, size);
1087 }
1088
1089 static ssize_t
ecryptfs_listxattr(struct dentry * dentry,char * list,size_t size)1090 ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1091 {
1092 int rc = 0;
1093 struct dentry *lower_dentry;
1094
1095 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1096 if (!d_inode(lower_dentry)->i_op->listxattr) {
1097 rc = -EOPNOTSUPP;
1098 goto out;
1099 }
1100 inode_lock(d_inode(lower_dentry));
1101 rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
1102 inode_unlock(d_inode(lower_dentry));
1103 out:
1104 return rc;
1105 }
1106
ecryptfs_removexattr(struct dentry * dentry,struct inode * inode,const char * name)1107 static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
1108 const char *name)
1109 {
1110 int rc;
1111 struct dentry *lower_dentry;
1112 struct inode *lower_inode;
1113
1114 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1115 lower_inode = ecryptfs_inode_to_lower(inode);
1116 if (!(lower_inode->i_opflags & IOP_XATTR)) {
1117 rc = -EOPNOTSUPP;
1118 goto out;
1119 }
1120 inode_lock(lower_inode);
1121 rc = __vfs_removexattr(&nop_mnt_idmap, lower_dentry, name);
1122 inode_unlock(lower_inode);
1123 out:
1124 return rc;
1125 }
1126
ecryptfs_fileattr_get(struct dentry * dentry,struct file_kattr * fa)1127 static int ecryptfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
1128 {
1129 return vfs_fileattr_get(ecryptfs_dentry_to_lower(dentry), fa);
1130 }
1131
ecryptfs_fileattr_set(struct mnt_idmap * idmap,struct dentry * dentry,struct file_kattr * fa)1132 static int ecryptfs_fileattr_set(struct mnt_idmap *idmap,
1133 struct dentry *dentry, struct file_kattr *fa)
1134 {
1135 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
1136 int rc;
1137
1138 rc = vfs_fileattr_set(&nop_mnt_idmap, lower_dentry, fa);
1139 fsstack_copy_attr_all(d_inode(dentry), d_inode(lower_dentry));
1140
1141 return rc;
1142 }
1143
ecryptfs_get_acl(struct mnt_idmap * idmap,struct dentry * dentry,int type)1144 static struct posix_acl *ecryptfs_get_acl(struct mnt_idmap *idmap,
1145 struct dentry *dentry, int type)
1146 {
1147 return vfs_get_acl(idmap, ecryptfs_dentry_to_lower(dentry),
1148 posix_acl_xattr_name(type));
1149 }
1150
ecryptfs_set_acl(struct mnt_idmap * idmap,struct dentry * dentry,struct posix_acl * acl,int type)1151 static int ecryptfs_set_acl(struct mnt_idmap *idmap,
1152 struct dentry *dentry, struct posix_acl *acl,
1153 int type)
1154 {
1155 int rc;
1156 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
1157 struct inode *lower_inode = d_inode(lower_dentry);
1158
1159 rc = vfs_set_acl(&nop_mnt_idmap, lower_dentry,
1160 posix_acl_xattr_name(type), acl);
1161 if (!rc)
1162 fsstack_copy_attr_all(d_inode(dentry), lower_inode);
1163 return rc;
1164 }
1165
1166 const struct inode_operations ecryptfs_symlink_iops = {
1167 .get_link = ecryptfs_get_link,
1168 .permission = ecryptfs_permission,
1169 .setattr = ecryptfs_setattr,
1170 .getattr = ecryptfs_getattr_link,
1171 .listxattr = ecryptfs_listxattr,
1172 };
1173
1174 const struct inode_operations ecryptfs_dir_iops = {
1175 .create = ecryptfs_create,
1176 .lookup = ecryptfs_lookup,
1177 .link = ecryptfs_link,
1178 .unlink = ecryptfs_unlink,
1179 .symlink = ecryptfs_symlink,
1180 .mkdir = ecryptfs_mkdir,
1181 .rmdir = ecryptfs_rmdir,
1182 .mknod = ecryptfs_mknod,
1183 .rename = ecryptfs_rename,
1184 .permission = ecryptfs_permission,
1185 .setattr = ecryptfs_setattr,
1186 .listxattr = ecryptfs_listxattr,
1187 .fileattr_get = ecryptfs_fileattr_get,
1188 .fileattr_set = ecryptfs_fileattr_set,
1189 .get_acl = ecryptfs_get_acl,
1190 .set_acl = ecryptfs_set_acl,
1191 };
1192
1193 const struct inode_operations ecryptfs_main_iops = {
1194 .permission = ecryptfs_permission,
1195 .setattr = ecryptfs_setattr,
1196 .getattr = ecryptfs_getattr,
1197 .listxattr = ecryptfs_listxattr,
1198 .fileattr_get = ecryptfs_fileattr_get,
1199 .fileattr_set = ecryptfs_fileattr_set,
1200 .get_acl = ecryptfs_get_acl,
1201 .set_acl = ecryptfs_set_acl,
1202 };
1203
ecryptfs_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size)1204 static int ecryptfs_xattr_get(const struct xattr_handler *handler,
1205 struct dentry *dentry, struct inode *inode,
1206 const char *name, void *buffer, size_t size)
1207 {
1208 return ecryptfs_getxattr(dentry, inode, name, buffer, size);
1209 }
1210
ecryptfs_xattr_set(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)1211 static int ecryptfs_xattr_set(const struct xattr_handler *handler,
1212 struct mnt_idmap *idmap,
1213 struct dentry *dentry, struct inode *inode,
1214 const char *name, const void *value, size_t size,
1215 int flags)
1216 {
1217 if (value)
1218 return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
1219 else {
1220 BUG_ON(flags != XATTR_REPLACE);
1221 return ecryptfs_removexattr(dentry, inode, name);
1222 }
1223 }
1224
1225 static const struct xattr_handler ecryptfs_xattr_handler = {
1226 .prefix = "", /* match anything */
1227 .get = ecryptfs_xattr_get,
1228 .set = ecryptfs_xattr_set,
1229 };
1230
1231 const struct xattr_handler * const ecryptfs_xattr_handlers[] = {
1232 &ecryptfs_xattr_handler,
1233 NULL
1234 };
1235