1 #include <linux/btrfs.h>
2 #include <linux/capability.h>
3 #include <linux/cred.h>
4 #include <linux/mount.h>
5 #include <linux/fdtable.h>
6 #include <linux/file.h>
8 #include <linux/namei.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/magic.h>
12 #include <linux/parser.h>
13 #include <linux/security.h>
14 #include <linux/seq_file.h>
15 #include <linux/statfs.h>
16 #include <linux/slab.h>
17 #include <linux/user_namespace.h>
18 #include <linux/uidgid.h>
19 #include <linux/xattr.h>
20 #include <linux/posix_acl.h>
21 #include <linux/posix_acl_xattr.h>
22 #include <linux/uio.h>
24 struct shiftfs_super_info
{
26 struct user_namespace
*userns
;
27 /* creds of process who created the super block */
28 const struct cred
*creator_cred
;
30 unsigned int passthrough
;
31 struct shiftfs_super_info
*info_mark
;
34 struct shiftfs_file_info
{
36 struct file
*realfile
;
39 struct kmem_cache
*shiftfs_file_info_cache
;
41 static void shiftfs_fill_inode(struct inode
*inode
, unsigned long ino
,
42 umode_t mode
, dev_t dev
, struct dentry
*dentry
);
44 #define SHIFTFS_PASSTHROUGH_NONE 0
45 #define SHIFTFS_PASSTHROUGH_STAT 1
46 #define SHIFTFS_PASSTHROUGH_IOCTL 2
47 #define SHIFTFS_PASSTHROUGH_ALL \
48 (SHIFTFS_PASSTHROUGH_STAT | SHIFTFS_PASSTHROUGH_IOCTL)
50 static inline bool shiftfs_passthrough_ioctls(struct shiftfs_super_info
*info
)
52 if (!(info
->passthrough
& SHIFTFS_PASSTHROUGH_IOCTL
))
55 if (info
->info_mark
&&
56 !(info
->info_mark
->passthrough
& SHIFTFS_PASSTHROUGH_IOCTL
))
62 static inline bool shiftfs_passthrough_statfs(struct shiftfs_super_info
*info
)
64 if (!(info
->passthrough
& SHIFTFS_PASSTHROUGH_STAT
))
67 if (info
->info_mark
&&
68 !(info
->info_mark
->passthrough
& SHIFTFS_PASSTHROUGH_STAT
))
80 /* global filesystem options */
81 static const match_table_t tokens
= {
83 { OPT_PASSTHROUGH
, "passthrough=%u" },
87 static const struct cred
*shiftfs_override_creds(const struct super_block
*sb
)
89 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
91 return override_creds(sbinfo
->creator_cred
);
94 static inline void shiftfs_revert_object_creds(const struct cred
*oldcred
,
97 revert_creds(oldcred
);
101 static int shiftfs_override_object_creds(const struct super_block
*sb
,
102 const struct cred
**oldcred
,
103 struct cred
**newcred
,
104 struct dentry
*dentry
, umode_t mode
,
107 kuid_t fsuid
= current_fsuid();
108 kgid_t fsgid
= current_fsgid();
110 *oldcred
= shiftfs_override_creds(sb
);
112 *newcred
= prepare_creds();
114 revert_creds(*oldcred
);
118 (*newcred
)->fsuid
= KUIDT_INIT(from_kuid(sb
->s_user_ns
, fsuid
));
119 (*newcred
)->fsgid
= KGIDT_INIT(from_kgid(sb
->s_user_ns
, fsgid
));
122 int err
= security_dentry_create_files_as(dentry
, mode
,
126 shiftfs_revert_object_creds(*oldcred
, *newcred
);
131 put_cred(override_creds(*newcred
));
135 static kuid_t
shift_kuid(struct user_namespace
*from
, struct user_namespace
*to
,
138 uid_t uid
= from_kuid(from
, kuid
);
139 return make_kuid(to
, uid
);
142 static kgid_t
shift_kgid(struct user_namespace
*from
, struct user_namespace
*to
,
145 gid_t gid
= from_kgid(from
, kgid
);
146 return make_kgid(to
, gid
);
149 static void shiftfs_copyattr(struct inode
*from
, struct inode
*to
)
151 struct user_namespace
*from_ns
= from
->i_sb
->s_user_ns
;
152 struct user_namespace
*to_ns
= to
->i_sb
->s_user_ns
;
154 to
->i_uid
= shift_kuid(from_ns
, to_ns
, from
->i_uid
);
155 to
->i_gid
= shift_kgid(from_ns
, to_ns
, from
->i_gid
);
156 to
->i_mode
= from
->i_mode
;
157 to
->i_atime
= from
->i_atime
;
158 to
->i_mtime
= from
->i_mtime
;
159 to
->i_ctime
= from
->i_ctime
;
160 i_size_write(to
, i_size_read(from
));
163 static void shiftfs_copyflags(struct inode
*from
, struct inode
*to
)
165 unsigned int mask
= S_SYNC
| S_IMMUTABLE
| S_APPEND
| S_NOATIME
;
167 inode_set_flags(to
, from
->i_flags
& mask
, mask
);
170 static void shiftfs_file_accessed(struct file
*file
)
172 struct inode
*upperi
, *loweri
;
174 if (file
->f_flags
& O_NOATIME
)
177 upperi
= file_inode(file
);
178 loweri
= upperi
->i_private
;
183 upperi
->i_mtime
= loweri
->i_mtime
;
184 upperi
->i_ctime
= loweri
->i_ctime
;
186 touch_atime(&file
->f_path
);
189 static int shiftfs_parse_mount_options(struct shiftfs_super_info
*sbinfo
,
193 substring_t args
[MAX_OPT_ARGS
];
195 sbinfo
->mark
= false;
196 sbinfo
->passthrough
= 0;
198 while ((p
= strsep(&options
, ",")) != NULL
) {
199 int err
, intarg
, token
;
204 token
= match_token(p
, tokens
, args
);
209 case OPT_PASSTHROUGH
:
210 err
= match_int(&args
[0], &intarg
);
214 if (intarg
& ~SHIFTFS_PASSTHROUGH_ALL
)
217 sbinfo
->passthrough
= intarg
;
227 static void shiftfs_d_release(struct dentry
*dentry
)
229 struct dentry
*lowerd
= dentry
->d_fsdata
;
235 static struct dentry
*shiftfs_d_real(struct dentry
*dentry
,
236 const struct inode
*inode
)
238 struct dentry
*lowerd
= dentry
->d_fsdata
;
240 if (inode
&& d_inode(dentry
) == inode
)
243 lowerd
= d_real(lowerd
, inode
);
244 if (lowerd
&& (!inode
|| inode
== d_inode(lowerd
)))
247 WARN(1, "shiftfs_d_real(%pd4, %s:%lu): real dentry not found\n", dentry
,
248 inode
? inode
->i_sb
->s_id
: "NULL", inode
? inode
->i_ino
: 0);
252 static int shiftfs_d_weak_revalidate(struct dentry
*dentry
, unsigned int flags
)
255 struct dentry
*lowerd
= dentry
->d_fsdata
;
257 if (d_is_negative(lowerd
) != d_is_negative(dentry
))
260 if ((lowerd
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
))
261 err
= lowerd
->d_op
->d_weak_revalidate(lowerd
, flags
);
263 if (d_really_is_positive(dentry
)) {
264 struct inode
*inode
= d_inode(dentry
);
265 struct inode
*loweri
= d_inode(lowerd
);
267 shiftfs_copyattr(loweri
, inode
);
275 static int shiftfs_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
278 struct dentry
*lowerd
= dentry
->d_fsdata
;
280 if (d_unhashed(lowerd
) ||
281 ((d_is_negative(lowerd
) != d_is_negative(dentry
))))
284 if (flags
& LOOKUP_RCU
)
287 if ((lowerd
->d_flags
& DCACHE_OP_REVALIDATE
))
288 err
= lowerd
->d_op
->d_revalidate(lowerd
, flags
);
290 if (d_really_is_positive(dentry
)) {
291 struct inode
*inode
= d_inode(dentry
);
292 struct inode
*loweri
= d_inode(lowerd
);
294 shiftfs_copyattr(loweri
, inode
);
302 static const struct dentry_operations shiftfs_dentry_ops
= {
303 .d_release
= shiftfs_d_release
,
304 .d_real
= shiftfs_d_real
,
305 .d_revalidate
= shiftfs_d_revalidate
,
306 .d_weak_revalidate
= shiftfs_d_weak_revalidate
,
309 static const char *shiftfs_get_link(struct dentry
*dentry
, struct inode
*inode
,
310 struct delayed_call
*done
)
313 const struct cred
*oldcred
;
314 struct dentry
*lowerd
;
316 /* RCU lookup not supported */
318 return ERR_PTR(-ECHILD
);
320 lowerd
= dentry
->d_fsdata
;
321 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
322 p
= vfs_get_link(lowerd
, done
);
323 revert_creds(oldcred
);
328 static int shiftfs_setxattr(struct dentry
*dentry
, struct inode
*inode
,
329 const char *name
, const void *value
,
330 size_t size
, int flags
)
332 struct dentry
*lowerd
= dentry
->d_fsdata
;
334 const struct cred
*oldcred
;
336 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
337 err
= vfs_setxattr(lowerd
, name
, value
, size
, flags
);
338 revert_creds(oldcred
);
340 shiftfs_copyattr(lowerd
->d_inode
, inode
);
345 static int shiftfs_xattr_get(const struct xattr_handler
*handler
,
346 struct dentry
*dentry
, struct inode
*inode
,
347 const char *name
, void *value
, size_t size
)
349 struct dentry
*lowerd
= dentry
->d_fsdata
;
351 const struct cred
*oldcred
;
353 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
354 err
= vfs_getxattr(lowerd
, name
, value
, size
);
355 revert_creds(oldcred
);
360 static ssize_t
shiftfs_listxattr(struct dentry
*dentry
, char *list
,
363 struct dentry
*lowerd
= dentry
->d_fsdata
;
365 const struct cred
*oldcred
;
367 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
368 err
= vfs_listxattr(lowerd
, list
, size
);
369 revert_creds(oldcred
);
374 static int shiftfs_removexattr(struct dentry
*dentry
, const char *name
)
376 struct dentry
*lowerd
= dentry
->d_fsdata
;
378 const struct cred
*oldcred
;
380 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
381 err
= vfs_removexattr(lowerd
, name
);
382 revert_creds(oldcred
);
385 shiftfs_copyattr(lowerd
->d_inode
, d_inode(dentry
));
390 static int shiftfs_xattr_set(const struct xattr_handler
*handler
,
391 struct dentry
*dentry
, struct inode
*inode
,
392 const char *name
, const void *value
, size_t size
,
396 return shiftfs_removexattr(dentry
, name
);
397 return shiftfs_setxattr(dentry
, inode
, name
, value
, size
, flags
);
400 static int shiftfs_inode_test(struct inode
*inode
, void *data
)
402 return inode
->i_private
== data
;
405 static int shiftfs_inode_set(struct inode
*inode
, void *data
)
407 inode
->i_private
= data
;
411 static int shiftfs_create_object(struct inode
*diri
, struct dentry
*dentry
,
412 umode_t mode
, const char *symlink
,
413 struct dentry
*hardlink
, bool excl
)
416 const struct cred
*oldcred
;
417 struct cred
*newcred
;
418 void *loweri_iop_ptr
= NULL
;
419 umode_t modei
= mode
;
420 struct super_block
*dir_sb
= diri
->i_sb
;
421 struct dentry
*lowerd_new
= dentry
->d_fsdata
;
422 struct inode
*inode
= NULL
, *loweri_dir
= diri
->i_private
;
423 const struct inode_operations
*loweri_dir_iop
= loweri_dir
->i_op
;
424 struct dentry
*lowerd_link
= NULL
;
427 loweri_iop_ptr
= loweri_dir_iop
->link
;
429 switch (mode
& S_IFMT
) {
431 loweri_iop_ptr
= loweri_dir_iop
->mkdir
;
434 loweri_iop_ptr
= loweri_dir_iop
->create
;
437 loweri_iop_ptr
= loweri_dir_iop
->symlink
;
442 loweri_iop_ptr
= loweri_dir_iop
->mknod
;
446 if (!loweri_iop_ptr
) {
451 inode_lock_nested(loweri_dir
, I_MUTEX_PARENT
);
454 inode
= new_inode(dir_sb
);
461 * new_inode() will have added the new inode to the super
462 * block's list of inodes. Further below we will call
463 * inode_insert5() Which would perform the same operation again
464 * thereby corrupting the list. To avoid this raise I_CREATING
465 * in i_state which will cause inode_insert5() to skip this
466 * step. I_CREATING will be cleared by d_instantiate_new()
469 spin_lock(&inode
->i_lock
);
470 inode
->i_state
|= I_CREATING
;
471 spin_unlock(&inode
->i_lock
);
473 inode_init_owner(inode
, diri
, mode
);
474 modei
= inode
->i_mode
;
477 err
= shiftfs_override_object_creds(dentry
->d_sb
, &oldcred
, &newcred
,
478 dentry
, modei
, hardlink
!= NULL
);
483 lowerd_link
= hardlink
->d_fsdata
;
484 err
= vfs_link(lowerd_link
, loweri_dir
, lowerd_new
, NULL
);
486 switch (modei
& S_IFMT
) {
488 err
= vfs_mkdir(loweri_dir
, lowerd_new
, modei
);
491 err
= vfs_create(loweri_dir
, lowerd_new
, modei
, excl
);
494 err
= vfs_symlink(loweri_dir
, lowerd_new
, symlink
);
499 err
= vfs_mknod(loweri_dir
, lowerd_new
, modei
, 0);
507 shiftfs_revert_object_creds(oldcred
, newcred
);
509 if (!err
&& WARN_ON(!lowerd_new
->d_inode
))
515 inode
= d_inode(hardlink
);
518 /* copy up times from lower inode */
519 shiftfs_copyattr(d_inode(lowerd_link
), inode
);
520 set_nlink(d_inode(hardlink
), d_inode(lowerd_link
)->i_nlink
);
521 d_instantiate(dentry
, inode
);
523 struct inode
*inode_tmp
;
524 struct inode
*loweri_new
= d_inode(lowerd_new
);
526 inode_tmp
= inode_insert5(inode
, (unsigned long)loweri_new
,
527 shiftfs_inode_test
, shiftfs_inode_set
,
529 if (unlikely(inode_tmp
!= inode
)) {
530 pr_err_ratelimited("shiftfs: newly created inode found in cache\n");
537 shiftfs_fill_inode(inode
, loweri_new
->i_ino
, loweri_new
->i_mode
,
539 d_instantiate_new(dentry
, inode
);
542 shiftfs_copyattr(loweri_dir
, diri
);
543 if (loweri_iop_ptr
== loweri_dir_iop
->mkdir
)
544 set_nlink(diri
, loweri_dir
->i_nlink
);
550 inode_unlock(loweri_dir
);
555 static int shiftfs_create(struct inode
*dir
, struct dentry
*dentry
,
556 umode_t mode
, bool excl
)
560 return shiftfs_create_object(dir
, dentry
, mode
, NULL
, NULL
, excl
);
563 static int shiftfs_mkdir(struct inode
*dir
, struct dentry
*dentry
,
568 return shiftfs_create_object(dir
, dentry
, mode
, NULL
, NULL
, false);
571 static int shiftfs_link(struct dentry
*hardlink
, struct inode
*dir
,
572 struct dentry
*dentry
)
574 return shiftfs_create_object(dir
, dentry
, 0, NULL
, hardlink
, false);
577 static int shiftfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
580 if (!S_ISFIFO(mode
) && !S_ISSOCK(mode
))
583 return shiftfs_create_object(dir
, dentry
, mode
, NULL
, NULL
, false);
586 static int shiftfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
589 return shiftfs_create_object(dir
, dentry
, S_IFLNK
, symlink
, NULL
, false);
592 static int shiftfs_rm(struct inode
*dir
, struct dentry
*dentry
, bool rmdir
)
594 struct dentry
*lowerd
= dentry
->d_fsdata
;
595 struct inode
*loweri
= dir
->i_private
;
597 const struct cred
*oldcred
;
599 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
600 inode_lock_nested(loweri
, I_MUTEX_PARENT
);
602 err
= vfs_rmdir(loweri
, lowerd
);
604 err
= vfs_unlink(loweri
, lowerd
, NULL
);
605 inode_unlock(loweri
);
606 revert_creds(oldcred
);
608 shiftfs_copyattr(loweri
, dir
);
609 set_nlink(d_inode(dentry
), loweri
->i_nlink
);
613 set_nlink(dir
, loweri
->i_nlink
);
618 static int shiftfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
620 return shiftfs_rm(dir
, dentry
, false);
623 static int shiftfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
625 return shiftfs_rm(dir
, dentry
, true);
628 static int shiftfs_rename(struct inode
*olddir
, struct dentry
*old
,
629 struct inode
*newdir
, struct dentry
*new,
632 struct dentry
*lowerd_dir_old
= old
->d_parent
->d_fsdata
,
633 *lowerd_dir_new
= new->d_parent
->d_fsdata
,
634 *lowerd_old
= old
->d_fsdata
, *lowerd_new
= new->d_fsdata
,
636 struct inode
*loweri_dir_old
= lowerd_dir_old
->d_inode
,
637 *loweri_dir_new
= lowerd_dir_new
->d_inode
;
639 const struct cred
*oldcred
;
641 trapd
= lock_rename(lowerd_dir_new
, lowerd_dir_old
);
643 if (trapd
== lowerd_old
|| trapd
== lowerd_new
)
646 oldcred
= shiftfs_override_creds(old
->d_sb
);
647 err
= vfs_rename(loweri_dir_old
, lowerd_old
, loweri_dir_new
, lowerd_new
,
649 revert_creds(oldcred
);
651 shiftfs_copyattr(loweri_dir_old
, olddir
);
652 shiftfs_copyattr(loweri_dir_new
, newdir
);
655 unlock_rename(lowerd_dir_new
, lowerd_dir_old
);
660 static struct dentry
*shiftfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
665 const struct cred
*oldcred
;
666 struct dentry
*lowerd
= dentry
->d_parent
->d_fsdata
;
667 struct inode
*inode
= NULL
, *loweri
= lowerd
->d_inode
;
670 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
671 new = lookup_one_len(dentry
->d_name
.name
, lowerd
, dentry
->d_name
.len
);
672 revert_creds(oldcred
);
673 inode_unlock(loweri
);
678 dentry
->d_fsdata
= new;
684 inode
= iget5_locked(dentry
->d_sb
, (unsigned long)newi
,
685 shiftfs_inode_test
, shiftfs_inode_set
, newi
);
688 return ERR_PTR(-ENOMEM
);
690 if (inode
->i_state
& I_NEW
) {
692 * inode->i_private set by shiftfs_inode_set(), but we still
693 * need to take a reference
696 shiftfs_fill_inode(inode
, newi
->i_ino
, newi
->i_mode
, 0, new);
697 unlock_new_inode(inode
);
701 return d_splice_alias(inode
, dentry
);
704 static int shiftfs_permission(struct inode
*inode
, int mask
)
707 const struct cred
*oldcred
;
708 struct inode
*loweri
= inode
->i_private
;
711 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
715 err
= generic_permission(inode
, mask
);
719 oldcred
= shiftfs_override_creds(inode
->i_sb
);
720 err
= inode_permission(loweri
, mask
);
721 revert_creds(oldcred
);
726 static int shiftfs_fiemap(struct inode
*inode
,
727 struct fiemap_extent_info
*fieinfo
, u64 start
,
731 const struct cred
*oldcred
;
732 struct inode
*loweri
= inode
->i_private
;
734 if (!loweri
->i_op
->fiemap
)
737 oldcred
= shiftfs_override_creds(inode
->i_sb
);
738 if (fieinfo
->fi_flags
& FIEMAP_FLAG_SYNC
)
739 filemap_write_and_wait(loweri
->i_mapping
);
740 err
= loweri
->i_op
->fiemap(loweri
, fieinfo
, start
, len
);
741 revert_creds(oldcred
);
746 static int shiftfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
750 const struct cred
*oldcred
;
751 struct dentry
*lowerd
= dentry
->d_fsdata
;
752 struct inode
*loweri
= dir
->i_private
;
754 if (!loweri
->i_op
->tmpfile
)
757 oldcred
= shiftfs_override_creds(dir
->i_sb
);
758 err
= loweri
->i_op
->tmpfile(loweri
, lowerd
, mode
);
759 revert_creds(oldcred
);
764 static int shiftfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
766 struct dentry
*lowerd
= dentry
->d_fsdata
;
767 struct inode
*loweri
= lowerd
->d_inode
;
768 struct iattr newattr
;
769 const struct cred
*oldcred
;
770 struct super_block
*sb
= dentry
->d_sb
;
773 err
= setattr_prepare(dentry
, attr
);
778 newattr
.ia_uid
= KUIDT_INIT(from_kuid(sb
->s_user_ns
, attr
->ia_uid
));
779 newattr
.ia_gid
= KGIDT_INIT(from_kgid(sb
->s_user_ns
, attr
->ia_gid
));
782 * mode change is for clearing setuid/setgid bits. Allow lower fs
783 * to interpret this in its own way.
785 if (newattr
.ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
786 newattr
.ia_valid
&= ~ATTR_MODE
;
789 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
790 err
= notify_change(lowerd
, &newattr
, NULL
);
791 revert_creds(oldcred
);
792 inode_unlock(loweri
);
794 shiftfs_copyattr(loweri
, d_inode(dentry
));
799 static int shiftfs_getattr(const struct path
*path
, struct kstat
*stat
,
800 u32 request_mask
, unsigned int query_flags
)
802 struct inode
*inode
= path
->dentry
->d_inode
;
803 struct dentry
*lowerd
= path
->dentry
->d_fsdata
;
804 struct inode
*loweri
= lowerd
->d_inode
;
805 struct shiftfs_super_info
*info
= path
->dentry
->d_sb
->s_fs_info
;
806 struct path newpath
= { .mnt
= info
->mnt
, .dentry
= lowerd
};
807 struct user_namespace
*from_ns
= loweri
->i_sb
->s_user_ns
;
808 struct user_namespace
*to_ns
= inode
->i_sb
->s_user_ns
;
809 const struct cred
*oldcred
;
812 oldcred
= shiftfs_override_creds(inode
->i_sb
);
813 err
= vfs_getattr(&newpath
, stat
, request_mask
, query_flags
);
814 revert_creds(oldcred
);
819 /* transform the underlying id */
820 stat
->uid
= shift_kuid(from_ns
, to_ns
, stat
->uid
);
821 stat
->gid
= shift_kgid(from_ns
, to_ns
, stat
->gid
);
825 #ifdef CONFIG_SHIFT_FS_POSIX_ACL
828 shift_acl_ids(struct user_namespace
*from
, struct user_namespace
*to
,
829 struct posix_acl
*acl
)
833 for (i
= 0; i
< acl
->a_count
; i
++) {
834 struct posix_acl_entry
*e
= &acl
->a_entries
[i
];
837 e
->e_uid
= shift_kuid(from
, to
, e
->e_uid
);
838 if (!uid_valid(e
->e_uid
))
842 e
->e_gid
= shift_kgid(from
, to
, e
->e_gid
);
843 if (!gid_valid(e
->e_gid
))
852 shift_acl_xattr_ids(struct user_namespace
*from
, struct user_namespace
*to
,
853 void *value
, size_t size
)
855 struct posix_acl_xattr_header
*header
= value
;
856 struct posix_acl_xattr_entry
*entry
= (void *)(header
+ 1), *end
;
863 if (size
< sizeof(struct posix_acl_xattr_header
))
865 if (header
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
868 count
= posix_acl_xattr_count(size
);
874 for (end
= entry
+ count
; entry
!= end
; entry
++) {
875 switch(le16_to_cpu(entry
->e_tag
)) {
877 kuid
= make_kuid(&init_user_ns
, le32_to_cpu(entry
->e_id
));
878 kuid
= shift_kuid(from
, to
, kuid
);
879 entry
->e_id
= cpu_to_le32(from_kuid(&init_user_ns
, kuid
));
882 kgid
= make_kgid(&init_user_ns
, le32_to_cpu(entry
->e_id
));
883 kgid
= shift_kgid(from
, to
, kgid
);
884 entry
->e_id
= cpu_to_le32(from_kgid(&init_user_ns
, kgid
));
892 static struct posix_acl
*shiftfs_get_acl(struct inode
*inode
, int type
)
894 struct inode
*loweri
= inode
->i_private
;
895 const struct cred
*oldcred
;
896 struct posix_acl
*lower_acl
, *acl
= NULL
;
897 struct user_namespace
*from_ns
= loweri
->i_sb
->s_user_ns
;
898 struct user_namespace
*to_ns
= inode
->i_sb
->s_user_ns
;
902 if (!IS_POSIXACL(loweri
))
905 oldcred
= shiftfs_override_creds(inode
->i_sb
);
906 lower_acl
= get_acl(loweri
, type
);
907 revert_creds(oldcred
);
909 if (lower_acl
&& !IS_ERR(lower_acl
)) {
910 /* XXX: export posix_acl_clone? */
911 size
= sizeof(struct posix_acl
) +
912 lower_acl
->a_count
* sizeof(struct posix_acl_entry
);
913 acl
= kmemdup(lower_acl
, size
, GFP_KERNEL
);
914 posix_acl_release(lower_acl
);
917 return ERR_PTR(-ENOMEM
);
919 refcount_set(&acl
->a_refcount
, 1);
921 err
= shift_acl_ids(from_ns
, to_ns
, acl
);
932 shiftfs_posix_acl_xattr_get(const struct xattr_handler
*handler
,
933 struct dentry
*dentry
, struct inode
*inode
,
934 const char *name
, void *buffer
, size_t size
)
936 struct inode
*loweri
= inode
->i_private
;
939 ret
= shiftfs_xattr_get(NULL
, dentry
, inode
, handler
->name
,
945 shift_acl_xattr_ids(loweri
->i_sb
->s_user_ns
, inode
->i_sb
->s_user_ns
,
947 inode_unlock(loweri
);
952 shiftfs_posix_acl_xattr_set(const struct xattr_handler
*handler
,
953 struct dentry
*dentry
, struct inode
*inode
,
954 const char *name
, const void *value
,
955 size_t size
, int flags
)
957 struct inode
*loweri
= inode
->i_private
;
960 if (!IS_POSIXACL(loweri
) || !loweri
->i_op
->set_acl
)
962 if (handler
->flags
== ACL_TYPE_DEFAULT
&& !S_ISDIR(inode
->i_mode
))
963 return value
? -EACCES
: 0;
964 if (!inode_owner_or_capable(inode
))
968 shift_acl_xattr_ids(inode
->i_sb
->s_user_ns
,
969 loweri
->i_sb
->s_user_ns
,
970 (void *)value
, size
);
971 err
= shiftfs_setxattr(dentry
, inode
, handler
->name
, value
,
974 err
= shiftfs_removexattr(dentry
, handler
->name
);
978 shiftfs_copyattr(loweri
, inode
);
983 static const struct xattr_handler
984 shiftfs_posix_acl_access_xattr_handler
= {
985 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
986 .flags
= ACL_TYPE_ACCESS
,
987 .get
= shiftfs_posix_acl_xattr_get
,
988 .set
= shiftfs_posix_acl_xattr_set
,
991 static const struct xattr_handler
992 shiftfs_posix_acl_default_xattr_handler
= {
993 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
994 .flags
= ACL_TYPE_DEFAULT
,
995 .get
= shiftfs_posix_acl_xattr_get
,
996 .set
= shiftfs_posix_acl_xattr_set
,
999 #else /* !CONFIG_SHIFT_FS_POSIX_ACL */
1001 #define shiftfs_get_acl NULL
1003 #endif /* CONFIG_SHIFT_FS_POSIX_ACL */
1005 static const struct inode_operations shiftfs_dir_inode_operations
= {
1006 .lookup
= shiftfs_lookup
,
1007 .mkdir
= shiftfs_mkdir
,
1008 .symlink
= shiftfs_symlink
,
1009 .unlink
= shiftfs_unlink
,
1010 .rmdir
= shiftfs_rmdir
,
1011 .rename
= shiftfs_rename
,
1012 .link
= shiftfs_link
,
1013 .setattr
= shiftfs_setattr
,
1014 .create
= shiftfs_create
,
1015 .mknod
= shiftfs_mknod
,
1016 .permission
= shiftfs_permission
,
1017 .getattr
= shiftfs_getattr
,
1018 .listxattr
= shiftfs_listxattr
,
1019 .get_acl
= shiftfs_get_acl
,
1022 static const struct inode_operations shiftfs_file_inode_operations
= {
1023 .fiemap
= shiftfs_fiemap
,
1024 .getattr
= shiftfs_getattr
,
1025 .get_acl
= shiftfs_get_acl
,
1026 .listxattr
= shiftfs_listxattr
,
1027 .permission
= shiftfs_permission
,
1028 .setattr
= shiftfs_setattr
,
1029 .tmpfile
= shiftfs_tmpfile
,
1032 static const struct inode_operations shiftfs_special_inode_operations
= {
1033 .getattr
= shiftfs_getattr
,
1034 .get_acl
= shiftfs_get_acl
,
1035 .listxattr
= shiftfs_listxattr
,
1036 .permission
= shiftfs_permission
,
1037 .setattr
= shiftfs_setattr
,
1040 static const struct inode_operations shiftfs_symlink_inode_operations
= {
1041 .getattr
= shiftfs_getattr
,
1042 .get_link
= shiftfs_get_link
,
1043 .listxattr
= shiftfs_listxattr
,
1044 .setattr
= shiftfs_setattr
,
1047 static struct file
*shiftfs_open_realfile(const struct file
*file
,
1048 struct path
*realpath
)
1050 struct file
*lowerf
;
1051 const struct cred
*oldcred
;
1052 struct inode
*inode
= file_inode(file
);
1053 struct inode
*loweri
= realpath
->dentry
->d_inode
;
1054 struct shiftfs_super_info
*info
= inode
->i_sb
->s_fs_info
;
1056 oldcred
= shiftfs_override_creds(inode
->i_sb
);
1057 /* XXX: open_with_fake_path() not gauranteed to stay around, if
1058 * removed use dentry_open() */
1059 lowerf
= open_with_fake_path(realpath
, file
->f_flags
, loweri
, info
->creator_cred
);
1060 revert_creds(oldcred
);
1065 #define SHIFTFS_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
1067 static int shiftfs_change_flags(struct file
*file
, unsigned int flags
)
1069 struct inode
*inode
= file_inode(file
);
1072 /* if some flag changed that cannot be changed then something's amiss */
1073 if (WARN_ON((file
->f_flags
^ flags
) & ~SHIFTFS_SETFL_MASK
))
1076 flags
&= SHIFTFS_SETFL_MASK
;
1078 if (((flags
^ file
->f_flags
) & O_APPEND
) && IS_APPEND(inode
))
1081 if (flags
& O_DIRECT
) {
1082 if (!file
->f_mapping
->a_ops
||
1083 !file
->f_mapping
->a_ops
->direct_IO
)
1087 if (file
->f_op
->check_flags
) {
1088 err
= file
->f_op
->check_flags(flags
);
1093 spin_lock(&file
->f_lock
);
1094 file
->f_flags
= (file
->f_flags
& ~SHIFTFS_SETFL_MASK
) | flags
;
1095 spin_unlock(&file
->f_lock
);
1100 static int shiftfs_real_fdget(const struct file
*file
, struct fd
*lowerfd
)
1102 struct shiftfs_file_info
*file_info
= file
->private_data
;
1103 struct file
*realfile
= file_info
->realfile
;
1106 lowerfd
->file
= realfile
;
1108 /* Did the flags change since open? */
1109 if (unlikely(file
->f_flags
& ~lowerfd
->file
->f_flags
))
1110 return shiftfs_change_flags(lowerfd
->file
, file
->f_flags
);
1115 static int shiftfs_open(struct inode
*inode
, struct file
*file
)
1117 struct shiftfs_super_info
*ssi
= inode
->i_sb
->s_fs_info
;
1118 struct shiftfs_file_info
*file_info
;
1119 struct file
*realfile
;
1120 struct path
*realpath
;
1122 file_info
= kmem_cache_zalloc(shiftfs_file_info_cache
, GFP_KERNEL
);
1126 realpath
= &file_info
->realpath
;
1127 realpath
->mnt
= ssi
->mnt
;
1128 realpath
->dentry
= file
->f_path
.dentry
->d_fsdata
;
1130 realfile
= shiftfs_open_realfile(file
, realpath
);
1131 if (IS_ERR(realfile
)) {
1132 kmem_cache_free(shiftfs_file_info_cache
, file_info
);
1133 return PTR_ERR(realfile
);
1136 file
->private_data
= file_info
;
1137 file_info
->realfile
= realfile
;
1141 static int shiftfs_release(struct inode
*inode
, struct file
*file
)
1143 struct shiftfs_file_info
*file_info
= file
->private_data
;
1146 if (file_info
->realfile
)
1147 fput(file_info
->realfile
);
1149 kmem_cache_free(shiftfs_file_info_cache
, file_info
);
1155 static loff_t
shiftfs_llseek(struct file
*file
, loff_t offset
, int whence
)
1157 struct inode
*realinode
= file_inode(file
)->i_private
;
1159 return generic_file_llseek_size(file
, offset
, whence
,
1160 realinode
->i_sb
->s_maxbytes
,
1161 i_size_read(realinode
));
1164 /* XXX: Need to figure out what to to about atime updates, maybe other
1165 * timestamps too ... ref. ovl_file_accessed() */
1167 static rwf_t
shiftfs_iocb_to_rwf(struct kiocb
*iocb
)
1169 int ifl
= iocb
->ki_flags
;
1172 if (ifl
& IOCB_NOWAIT
)
1173 flags
|= RWF_NOWAIT
;
1174 if (ifl
& IOCB_HIPRI
)
1176 if (ifl
& IOCB_DSYNC
)
1178 if (ifl
& IOCB_SYNC
)
1184 static ssize_t
shiftfs_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
1186 struct file
*file
= iocb
->ki_filp
;
1188 const struct cred
*oldcred
;
1191 if (!iov_iter_count(iter
))
1194 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1198 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1199 ret
= vfs_iter_read(lowerfd
.file
, iter
, &iocb
->ki_pos
,
1200 shiftfs_iocb_to_rwf(iocb
));
1201 revert_creds(oldcred
);
1203 shiftfs_file_accessed(file
);
1209 static ssize_t
shiftfs_write_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
1211 struct file
*file
= iocb
->ki_filp
;
1212 struct inode
*inode
= file_inode(file
);
1214 const struct cred
*oldcred
;
1217 if (!iov_iter_count(iter
))
1222 shiftfs_copyattr(inode
->i_private
, inode
);
1223 ret
= file_remove_privs(file
);
1227 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1231 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1232 file_start_write(lowerfd
.file
);
1233 ret
= vfs_iter_write(lowerfd
.file
, iter
, &iocb
->ki_pos
,
1234 shiftfs_iocb_to_rwf(iocb
));
1235 file_end_write(lowerfd
.file
);
1236 revert_creds(oldcred
);
1239 shiftfs_copyattr(inode
->i_private
, inode
);
1244 inode_unlock(inode
);
1248 static int shiftfs_fsync(struct file
*file
, loff_t start
, loff_t end
,
1252 const struct cred
*oldcred
;
1255 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1259 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1260 ret
= vfs_fsync_range(lowerfd
.file
, start
, end
, datasync
);
1261 revert_creds(oldcred
);
1267 static int shiftfs_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1269 struct shiftfs_file_info
*file_info
= file
->private_data
;
1270 struct file
*realfile
= file_info
->realfile
;
1271 const struct cred
*oldcred
;
1274 if (!realfile
->f_op
->mmap
)
1277 if (WARN_ON(file
!= vma
->vm_file
))
1280 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1281 vma
->vm_file
= get_file(realfile
);
1282 ret
= call_mmap(vma
->vm_file
, vma
);
1283 revert_creds(oldcred
);
1285 shiftfs_file_accessed(file
);
1288 fput(realfile
); /* Drop refcount from new vm_file value */
1290 fput(file
); /* Drop refcount from previous vm_file value */
1295 static long shiftfs_fallocate(struct file
*file
, int mode
, loff_t offset
,
1298 struct inode
*inode
= file_inode(file
);
1299 struct inode
*loweri
= inode
->i_private
;
1301 const struct cred
*oldcred
;
1304 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1308 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1309 ret
= vfs_fallocate(lowerfd
.file
, mode
, offset
, len
);
1310 revert_creds(oldcred
);
1313 shiftfs_copyattr(loweri
, inode
);
1319 static int shiftfs_fadvise(struct file
*file
, loff_t offset
, loff_t len
,
1323 const struct cred
*oldcred
;
1326 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1330 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1331 ret
= vfs_fadvise(lowerfd
.file
, offset
, len
, advice
);
1332 revert_creds(oldcred
);
1338 static int shiftfs_override_ioctl_creds(const struct super_block
*sb
,
1339 const struct cred
**oldcred
,
1340 struct cred
**newcred
)
1342 kuid_t fsuid
= current_fsuid();
1343 kgid_t fsgid
= current_fsgid();
1345 *oldcred
= shiftfs_override_creds(sb
);
1347 *newcred
= prepare_creds();
1349 revert_creds(*oldcred
);
1353 (*newcred
)->fsuid
= KUIDT_INIT(from_kuid(sb
->s_user_ns
, fsuid
));
1354 (*newcred
)->fsgid
= KGIDT_INIT(from_kgid(sb
->s_user_ns
, fsgid
));
1356 /* clear all caps to prevent bypassing capable() checks */
1357 cap_clear((*newcred
)->cap_bset
);
1358 cap_clear((*newcred
)->cap_effective
);
1359 cap_clear((*newcred
)->cap_inheritable
);
1360 cap_clear((*newcred
)->cap_permitted
);
1362 put_cred(override_creds(*newcred
));
1366 static inline void shiftfs_revert_ioctl_creds(const struct cred
*oldcred
,
1367 struct cred
*newcred
)
1369 return shiftfs_revert_object_creds(oldcred
, newcred
);
1372 static inline bool is_btrfs_snap_ioctl(int cmd
)
1374 if ((cmd
== BTRFS_IOC_SNAP_CREATE
) || (cmd
== BTRFS_IOC_SNAP_CREATE_V2
))
1380 static int shiftfs_btrfs_ioctl_fd_restore(int cmd
, struct fd lfd
, int fd
,
1382 struct btrfs_ioctl_vol_args
*v1
,
1383 struct btrfs_ioctl_vol_args_v2
*v2
)
1387 if (!is_btrfs_snap_ioctl(cmd
))
1390 if (cmd
== BTRFS_IOC_SNAP_CREATE
)
1391 ret
= copy_to_user(arg
, v1
, sizeof(*v1
));
1393 ret
= copy_to_user(arg
, v2
, sizeof(*v2
));
1396 __close_fd(current
->files
, fd
);
1403 static int shiftfs_btrfs_ioctl_fd_replace(int cmd
, void __user
*arg
,
1404 struct btrfs_ioctl_vol_args
**b1
,
1405 struct btrfs_ioctl_vol_args_v2
**b2
,
1411 struct btrfs_ioctl_vol_args
*v1
= NULL
;
1412 struct btrfs_ioctl_vol_args_v2
*v2
= NULL
;
1414 if (!is_btrfs_snap_ioctl(cmd
))
1417 if (cmd
== BTRFS_IOC_SNAP_CREATE
) {
1418 v1
= memdup_user(arg
, sizeof(*v1
));
1424 v2
= memdup_user(arg
, sizeof(*v2
));
1435 ret
= shiftfs_real_fdget(src
.file
, lfd
);
1440 *newfd
= get_unused_fd_flags(lfd
->file
->f_flags
);
1446 fd_install(*newfd
, lfd
->file
);
1448 if (cmd
== BTRFS_IOC_SNAP_CREATE
) {
1450 ret
= copy_to_user(arg
, v1
, sizeof(*v1
));
1454 ret
= copy_to_user(arg
, v2
, sizeof(*v2
));
1459 shiftfs_btrfs_ioctl_fd_restore(cmd
, *lfd
, *newfd
, arg
, v1
, v2
);
1464 static long shiftfs_real_ioctl(struct file
*file
, unsigned int cmd
,
1468 struct cred
*newcred
;
1469 const struct cred
*oldcred
;
1471 long err
= 0, ret
= 0;
1472 void __user
*argp
= (void __user
*)arg
;
1473 struct fd btrfs_lfd
= {};
1474 struct super_block
*sb
= file
->f_path
.dentry
->d_sb
;
1475 struct btrfs_ioctl_vol_args
*btrfs_v1
= NULL
;
1476 struct btrfs_ioctl_vol_args_v2
*btrfs_v2
= NULL
;
1478 ret
= shiftfs_btrfs_ioctl_fd_replace(cmd
, argp
, &btrfs_v1
, &btrfs_v2
,
1479 &btrfs_lfd
, &newfd
);
1483 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1487 ret
= shiftfs_override_ioctl_creds(sb
, &oldcred
, &newcred
);
1491 ret
= vfs_ioctl(lowerfd
.file
, cmd
, arg
);
1493 shiftfs_revert_ioctl_creds(oldcred
, newcred
);
1495 shiftfs_copyattr(file_inode(lowerfd
.file
), file_inode(file
));
1496 shiftfs_copyflags(file_inode(lowerfd
.file
), file_inode(file
));
1502 err
= shiftfs_btrfs_ioctl_fd_restore(cmd
, btrfs_lfd
, newfd
, argp
,
1503 btrfs_v1
, btrfs_v2
);
1510 static bool in_ioctl_whitelist(int flag
)
1513 case BTRFS_IOC_SNAP_CREATE
:
1515 case BTRFS_IOC_SNAP_CREATE_V2
:
1517 case BTRFS_IOC_SUBVOL_CREATE
:
1519 case BTRFS_IOC_SUBVOL_CREATE_V2
:
1521 case BTRFS_IOC_SNAP_DESTROY
:
1528 static long shiftfs_ioctl(struct file
*file
, unsigned int cmd
,
1532 case FS_IOC_GETVERSION
:
1534 case FS_IOC_GETFLAGS
:
1536 case FS_IOC_SETFLAGS
:
1539 if (!in_ioctl_whitelist(cmd
) ||
1540 !shiftfs_passthrough_ioctls(file
->f_path
.dentry
->d_sb
->s_fs_info
))
1544 return shiftfs_real_ioctl(file
, cmd
, arg
);
1547 static long shiftfs_compat_ioctl(struct file
*file
, unsigned int cmd
,
1551 case FS_IOC32_GETVERSION
:
1553 case FS_IOC32_GETFLAGS
:
1555 case FS_IOC32_SETFLAGS
:
1558 if (!in_ioctl_whitelist(cmd
) ||
1559 !shiftfs_passthrough_ioctls(file
->f_path
.dentry
->d_sb
->s_fs_info
))
1560 return -ENOIOCTLCMD
;
1563 return shiftfs_real_ioctl(file
, cmd
, arg
);
1566 enum shiftfs_copyop
{
1572 static ssize_t
shiftfs_copyfile(struct file
*file_in
, loff_t pos_in
,
1573 struct file
*file_out
, loff_t pos_out
, u64 len
,
1574 unsigned int flags
, enum shiftfs_copyop op
)
1577 struct fd real_in
, real_out
;
1578 const struct cred
*oldcred
;
1579 struct inode
*inode_out
= file_inode(file_out
);
1580 struct inode
*loweri
= inode_out
->i_private
;
1582 ret
= shiftfs_real_fdget(file_out
, &real_out
);
1586 ret
= shiftfs_real_fdget(file_in
, &real_in
);
1592 oldcred
= shiftfs_override_creds(inode_out
->i_sb
);
1595 ret
= vfs_copy_file_range(real_in
.file
, pos_in
, real_out
.file
,
1596 pos_out
, len
, flags
);
1600 ret
= vfs_clone_file_range(real_in
.file
, pos_in
, real_out
.file
,
1601 pos_out
, len
, flags
);
1604 case SHIFTFS_DEDUPE
:
1605 ret
= vfs_dedupe_file_range_one(real_in
.file
, pos_in
,
1606 real_out
.file
, pos_out
, len
,
1610 revert_creds(oldcred
);
1613 shiftfs_copyattr(loweri
, inode_out
);
1621 static ssize_t
shiftfs_copy_file_range(struct file
*file_in
, loff_t pos_in
,
1622 struct file
*file_out
, loff_t pos_out
,
1623 size_t len
, unsigned int flags
)
1625 return shiftfs_copyfile(file_in
, pos_in
, file_out
, pos_out
, len
, flags
,
1629 static loff_t
shiftfs_remap_file_range(struct file
*file_in
, loff_t pos_in
,
1630 struct file
*file_out
, loff_t pos_out
,
1631 loff_t len
, unsigned int remap_flags
)
1633 enum shiftfs_copyop op
;
1635 if (remap_flags
& ~(REMAP_FILE_DEDUP
| REMAP_FILE_ADVISORY
))
1638 if (remap_flags
& REMAP_FILE_DEDUP
)
1639 op
= SHIFTFS_DEDUPE
;
1643 return shiftfs_copyfile(file_in
, pos_in
, file_out
, pos_out
, len
,
1647 static int shiftfs_iterate_shared(struct file
*file
, struct dir_context
*ctx
)
1649 const struct cred
*oldcred
;
1651 struct shiftfs_file_info
*file_info
= file
->private_data
;
1652 struct file
*realfile
= file_info
->realfile
;
1654 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1655 err
= iterate_dir(realfile
, ctx
);
1656 revert_creds(oldcred
);
1661 const struct file_operations shiftfs_file_operations
= {
1662 .open
= shiftfs_open
,
1663 .release
= shiftfs_release
,
1664 .llseek
= shiftfs_llseek
,
1665 .read_iter
= shiftfs_read_iter
,
1666 .write_iter
= shiftfs_write_iter
,
1667 .fsync
= shiftfs_fsync
,
1668 .mmap
= shiftfs_mmap
,
1669 .fallocate
= shiftfs_fallocate
,
1670 .fadvise
= shiftfs_fadvise
,
1671 .unlocked_ioctl
= shiftfs_ioctl
,
1672 .compat_ioctl
= shiftfs_compat_ioctl
,
1673 .copy_file_range
= shiftfs_copy_file_range
,
1674 .remap_file_range
= shiftfs_remap_file_range
,
1677 const struct file_operations shiftfs_dir_operations
= {
1678 .compat_ioctl
= shiftfs_compat_ioctl
,
1679 .fsync
= shiftfs_fsync
,
1680 .iterate_shared
= shiftfs_iterate_shared
,
1681 .llseek
= shiftfs_llseek
,
1682 .open
= shiftfs_open
,
1683 .read
= generic_read_dir
,
1684 .release
= shiftfs_release
,
1685 .unlocked_ioctl
= shiftfs_ioctl
,
1688 static const struct address_space_operations shiftfs_aops
= {
1689 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
1690 .direct_IO
= noop_direct_IO
,
1693 static void shiftfs_fill_inode(struct inode
*inode
, unsigned long ino
,
1694 umode_t mode
, dev_t dev
, struct dentry
*dentry
)
1696 struct inode
*loweri
;
1699 inode
->i_flags
|= S_NOCMTIME
;
1702 inode
->i_mode
= mode
;
1703 switch (mode
& S_IFMT
) {
1705 inode
->i_op
= &shiftfs_dir_inode_operations
;
1706 inode
->i_fop
= &shiftfs_dir_operations
;
1709 inode
->i_op
= &shiftfs_symlink_inode_operations
;
1712 inode
->i_op
= &shiftfs_file_inode_operations
;
1713 inode
->i_fop
= &shiftfs_file_operations
;
1714 inode
->i_mapping
->a_ops
= &shiftfs_aops
;
1717 inode
->i_op
= &shiftfs_special_inode_operations
;
1718 init_special_inode(inode
, mode
, dev
);
1725 loweri
= dentry
->d_inode
;
1726 if (!loweri
->i_op
->get_link
)
1727 inode
->i_opflags
|= IOP_NOFOLLOW
;
1729 shiftfs_copyattr(loweri
, inode
);
1730 shiftfs_copyflags(loweri
, inode
);
1731 set_nlink(inode
, loweri
->i_nlink
);
1734 static int shiftfs_show_options(struct seq_file
*m
, struct dentry
*dentry
)
1736 struct super_block
*sb
= dentry
->d_sb
;
1737 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1740 seq_show_option(m
, "mark", NULL
);
1742 if (sbinfo
->passthrough
)
1743 seq_printf(m
, ",passthrough=%u", sbinfo
->passthrough
);
1748 static int shiftfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
1750 struct super_block
*sb
= dentry
->d_sb
;
1751 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1752 struct dentry
*root
= sb
->s_root
;
1753 struct dentry
*realroot
= root
->d_fsdata
;
1754 struct path realpath
= { .mnt
= sbinfo
->mnt
, .dentry
= realroot
};
1757 err
= vfs_statfs(&realpath
, buf
);
1761 if (!shiftfs_passthrough_statfs(sbinfo
))
1762 buf
->f_type
= sb
->s_magic
;
1767 static void shiftfs_evict_inode(struct inode
*inode
)
1769 struct inode
*loweri
= inode
->i_private
;
1777 static void shiftfs_put_super(struct super_block
*sb
)
1779 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1782 mntput(sbinfo
->mnt
);
1783 put_cred(sbinfo
->creator_cred
);
1788 static const struct xattr_handler shiftfs_xattr_handler
= {
1790 .get
= shiftfs_xattr_get
,
1791 .set
= shiftfs_xattr_set
,
1794 const struct xattr_handler
*shiftfs_xattr_handlers
[] = {
1795 #ifdef CONFIG_SHIFT_FS_POSIX_ACL
1796 &shiftfs_posix_acl_access_xattr_handler
,
1797 &shiftfs_posix_acl_default_xattr_handler
,
1799 &shiftfs_xattr_handler
,
1803 static inline bool passthrough_is_subset(int old_flags
, int new_flags
)
1805 if ((new_flags
& old_flags
) != new_flags
)
1811 static int shiftfs_remount(struct super_block
*sb
, int *flags
, char *data
)
1814 struct shiftfs_super_info
new = {};
1815 struct shiftfs_super_info
*info
= sb
->s_fs_info
;
1817 err
= shiftfs_parse_mount_options(&new, data
);
1821 /* Mark mount option cannot be changed. */
1822 if (info
->mark
|| (info
->mark
!= new.mark
))
1825 if (info
->passthrough
!= new.passthrough
) {
1826 /* Don't allow exceeding passthrough options of mark mount. */
1827 if (!passthrough_is_subset(info
->info_mark
->passthrough
,
1831 info
->passthrough
= new.passthrough
;
1837 static const struct super_operations shiftfs_super_ops
= {
1838 .put_super
= shiftfs_put_super
,
1839 .show_options
= shiftfs_show_options
,
1840 .statfs
= shiftfs_statfs
,
1841 .remount_fs
= shiftfs_remount
,
1842 .evict_inode
= shiftfs_evict_inode
,
1845 struct shiftfs_data
{
1850 static int shiftfs_fill_super(struct super_block
*sb
, void *raw_data
,
1854 struct path path
= {};
1855 struct shiftfs_super_info
*sbinfo_mp
;
1857 struct inode
*inode
= NULL
;
1858 struct dentry
*dentry
= NULL
;
1859 struct shiftfs_data
*data
= raw_data
;
1860 struct shiftfs_super_info
*sbinfo
= NULL
;
1865 sb
->s_fs_info
= kzalloc(sizeof(*sbinfo
), GFP_KERNEL
);
1868 sbinfo
= sb
->s_fs_info
;
1870 err
= shiftfs_parse_mount_options(sbinfo
, data
->data
);
1874 /* to mount a mark, must be userns admin */
1875 if (!sbinfo
->mark
&& !ns_capable(current_user_ns(), CAP_SYS_ADMIN
))
1878 name
= kstrdup(data
->path
, GFP_KERNEL
);
1882 err
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
1886 if (!S_ISDIR(path
.dentry
->d_inode
->i_mode
)) {
1892 struct super_block
*lower_sb
= path
.mnt
->mnt_sb
;
1894 /* to mark a mount point, must root wrt lower s_user_ns */
1895 if (!ns_capable(lower_sb
->s_user_ns
, CAP_SYS_ADMIN
)) {
1901 * this part is visible unshifted, so make sure no
1902 * executables that could be used to give suid
1905 sb
->s_iflags
= SB_I_NOEXEC
;
1908 * Handle nesting of shiftfs mounts by referring this mark
1909 * mount back to the original mark mount. This is more
1910 * efficient and alleviates concerns about stack depth.
1912 if (lower_sb
->s_magic
== SHIFTFS_MAGIC
) {
1913 sbinfo_mp
= lower_sb
->s_fs_info
;
1915 /* Doesn't make sense to mark a mark mount */
1916 if (sbinfo_mp
->mark
) {
1921 if (!passthrough_is_subset(sbinfo_mp
->passthrough
,
1922 sbinfo
->passthrough
)) {
1927 sbinfo
->mnt
= mntget(sbinfo_mp
->mnt
);
1928 dentry
= dget(path
.dentry
->d_fsdata
);
1930 sbinfo
->mnt
= mntget(path
.mnt
);
1931 dentry
= dget(path
.dentry
);
1934 sbinfo
->creator_cred
= prepare_creds();
1935 if (!sbinfo
->creator_cred
) {
1941 * This leg executes if we're admin capable in the namespace,
1942 * so be very careful.
1945 if (path
.dentry
->d_sb
->s_magic
!= SHIFTFS_MAGIC
)
1948 sbinfo_mp
= path
.dentry
->d_sb
->s_fs_info
;
1949 if (!sbinfo_mp
->mark
)
1952 if (!passthrough_is_subset(sbinfo_mp
->passthrough
,
1953 sbinfo
->passthrough
))
1956 sbinfo
->mnt
= mntget(sbinfo_mp
->mnt
);
1957 sbinfo
->creator_cred
= get_cred(sbinfo_mp
->creator_cred
);
1958 dentry
= dget(path
.dentry
->d_fsdata
);
1959 sbinfo
->info_mark
= sbinfo_mp
;
1962 sb
->s_stack_depth
= dentry
->d_sb
->s_stack_depth
+ 1;
1963 if (sb
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
1964 printk(KERN_ERR
"shiftfs: maximum stacking depth exceeded\n");
1969 inode
= new_inode(sb
);
1974 shiftfs_fill_inode(inode
, dentry
->d_inode
->i_ino
, S_IFDIR
, 0, dentry
);
1976 ihold(dentry
->d_inode
);
1977 inode
->i_private
= dentry
->d_inode
;
1979 sb
->s_magic
= SHIFTFS_MAGIC
;
1980 sb
->s_op
= &shiftfs_super_ops
;
1981 sb
->s_xattr
= shiftfs_xattr_handlers
;
1982 sb
->s_d_op
= &shiftfs_dentry_ops
;
1983 sb
->s_flags
|= SB_POSIXACL
;
1984 sb
->s_root
= d_make_root(inode
);
1990 sb
->s_root
->d_fsdata
= dentry
;
1991 sbinfo
->userns
= get_user_ns(dentry
->d_sb
->s_user_ns
);
1992 shiftfs_copyattr(dentry
->d_inode
, sb
->s_root
->d_inode
);
2008 static struct dentry
*shiftfs_mount(struct file_system_type
*fs_type
,
2009 int flags
, const char *dev_name
, void *data
)
2011 struct shiftfs_data d
= { data
, dev_name
};
2013 return mount_nodev(fs_type
, flags
, &d
, shiftfs_fill_super
);
2016 static struct file_system_type shiftfs_type
= {
2017 .owner
= THIS_MODULE
,
2019 .mount
= shiftfs_mount
,
2020 .kill_sb
= kill_anon_super
,
2021 .fs_flags
= FS_USERNS_MOUNT
,
2024 static int __init
shiftfs_init(void)
2026 shiftfs_file_info_cache
= kmem_cache_create(
2027 "shiftfs_file_info_cache", sizeof(struct shiftfs_file_info
), 0,
2028 SLAB_HWCACHE_ALIGN
| SLAB_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
2029 if (!shiftfs_file_info_cache
)
2032 return register_filesystem(&shiftfs_type
);
2035 static void __exit
shiftfs_exit(void)
2037 unregister_filesystem(&shiftfs_type
);
2038 kmem_cache_destroy(shiftfs_file_info_cache
);
2041 MODULE_ALIAS_FS("shiftfs");
2042 MODULE_AUTHOR("James Bottomley");
2043 MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
2044 MODULE_AUTHOR("Christian Brauner <christian.brauner@ubuntu.com>");
2045 MODULE_DESCRIPTION("id shifting filesystem");
2046 MODULE_LICENSE("GPL v2");
2047 module_init(shiftfs_init
)
2048 module_exit(shiftfs_exit
)