1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 2e9be9d5eSMiklos Szeredi /* 3e9be9d5eSMiklos Szeredi * 4e9be9d5eSMiklos Szeredi * Copyright (C) 2011 Novell Inc. 5e9be9d5eSMiklos Szeredi */ 6e9be9d5eSMiklos Szeredi 7e9be9d5eSMiklos Szeredi #include <linux/kernel.h> 83a1e819bSAmir Goldstein #include <linux/uuid.h> 946e5d0a3SMiklos Szeredi #include <linux/fs.h> 10ee023c30SAmir Goldstein #include "ovl_entry.h" 11e9be9d5eSMiklos Szeredi 121bd0a3aeSlijiazi #undef pr_fmt 131bd0a3aeSlijiazi #define pr_fmt(fmt) "overlayfs: " fmt 141bd0a3aeSlijiazi 15e9be9d5eSMiklos Szeredi enum ovl_path_type { 1638e813dbSMiklos Szeredi __OVL_PATH_UPPER = (1 << 0), 1738e813dbSMiklos Szeredi __OVL_PATH_MERGE = (1 << 1), 1859548503SAmir Goldstein __OVL_PATH_ORIGIN = (1 << 2), 19e9be9d5eSMiklos Szeredi }; 20e9be9d5eSMiklos Szeredi 211afaba1eSMiklos Szeredi #define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER) 221afaba1eSMiklos Szeredi #define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE) 2359548503SAmir Goldstein #define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN) 24d837a49bSMiklos Szeredi 252d2f2d73SMiklos Szeredi #define OVL_XATTR_NAMESPACE "overlay." 262d2f2d73SMiklos Szeredi #define OVL_XATTR_TRUSTED_PREFIX XATTR_TRUSTED_PREFIX OVL_XATTR_NAMESPACE 272d2f2d73SMiklos Szeredi #define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE 2843d193f8SMiklos Szeredi 2943d193f8SMiklos Szeredi enum ovl_xattr { 3043d193f8SMiklos Szeredi OVL_XATTR_OPAQUE, 3143d193f8SMiklos Szeredi OVL_XATTR_REDIRECT, 3243d193f8SMiklos Szeredi OVL_XATTR_ORIGIN, 3343d193f8SMiklos Szeredi OVL_XATTR_IMPURE, 3443d193f8SMiklos Szeredi OVL_XATTR_NLINK, 3543d193f8SMiklos Szeredi OVL_XATTR_UPPER, 3643d193f8SMiklos Szeredi OVL_XATTR_METACOPY, 3743d193f8SMiklos Szeredi }; 383a1e819bSAmir Goldstein 39c62520a8SAmir Goldstein enum ovl_inode_flag { 40b79e05aaSAmir Goldstein /* Pure upper dir that may contain non pure upper entries */ 4113c72075SMiklos Szeredi OVL_IMPURE, 42b79e05aaSAmir Goldstein /* Non-merge dir that may contain whiteout entries */ 43b79e05aaSAmir Goldstein OVL_WHITEOUTS, 44359f392cSAmir Goldstein OVL_INDEX, 450c288874SVivek Goyal OVL_UPPERDATA, 46a00c2d59SVivek Goyal /* Inode number will remain constant over copy up. */ 47a00c2d59SVivek Goyal OVL_CONST_INO, 4813c72075SMiklos Szeredi }; 4913c72075SMiklos Szeredi 50c62520a8SAmir Goldstein enum ovl_entry_flag { 51c62520a8SAmir Goldstein OVL_E_UPPER_ALIAS, 52c62520a8SAmir Goldstein OVL_E_OPAQUE, 532ca3c148SAmir Goldstein OVL_E_CONNECTED, 54c62520a8SAmir Goldstein }; 55c62520a8SAmir Goldstein 56926e94d7SAmir Goldstein enum { 57926e94d7SAmir Goldstein OVL_XINO_OFF, 58926e94d7SAmir Goldstein OVL_XINO_AUTO, 59926e94d7SAmir Goldstein OVL_XINO_ON, 60926e94d7SAmir Goldstein }; 61926e94d7SAmir Goldstein 623a1e819bSAmir Goldstein /* 633a1e819bSAmir Goldstein * The tuple (fh,uuid) is a universal unique identifier for a copy up origin, 643a1e819bSAmir Goldstein * where: 653a1e819bSAmir Goldstein * origin.fh - exported file handle of the lower file 663a1e819bSAmir Goldstein * origin.uuid - uuid of the lower filesystem 673a1e819bSAmir Goldstein */ 683a1e819bSAmir Goldstein #define OVL_FH_VERSION 0 693a1e819bSAmir Goldstein #define OVL_FH_MAGIC 0xfb 703a1e819bSAmir Goldstein 713a1e819bSAmir Goldstein /* CPU byte order required for fid decoding: */ 723a1e819bSAmir Goldstein #define OVL_FH_FLAG_BIG_ENDIAN (1 << 0) 733a1e819bSAmir Goldstein #define OVL_FH_FLAG_ANY_ENDIAN (1 << 1) 7454fb347eSAmir Goldstein /* Is the real inode encoded in fid an upper inode? */ 7554fb347eSAmir Goldstein #define OVL_FH_FLAG_PATH_UPPER (1 << 2) 763a1e819bSAmir Goldstein 77961af647SAmir Goldstein #define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \ 78961af647SAmir Goldstein OVL_FH_FLAG_PATH_UPPER) 793a1e819bSAmir Goldstein 803a1e819bSAmir Goldstein #if defined(__LITTLE_ENDIAN) 813a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN 0 823a1e819bSAmir Goldstein #elif defined(__BIG_ENDIAN) 833a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN 843a1e819bSAmir Goldstein #else 853a1e819bSAmir Goldstein #error Endianness not defined 863a1e819bSAmir Goldstein #endif 873a1e819bSAmir Goldstein 88cbe7fba8SAmir Goldstein /* The type used to be returned by overlay exportfs for misaligned fid */ 89cbe7fba8SAmir Goldstein #define OVL_FILEID_V0 0xfb 90cbe7fba8SAmir Goldstein /* The type returned by overlay exportfs for 32bit aligned fid */ 91cbe7fba8SAmir Goldstein #define OVL_FILEID_V1 0xf8 928ed5eec9SAmir Goldstein 93cbe7fba8SAmir Goldstein /* On-disk format for "origin" file handle */ 94cbe7fba8SAmir Goldstein struct ovl_fb { 953a1e819bSAmir Goldstein u8 version; /* 0 */ 963a1e819bSAmir Goldstein u8 magic; /* 0xfb */ 973a1e819bSAmir Goldstein u8 len; /* size of this header + size of fid */ 983a1e819bSAmir Goldstein u8 flags; /* OVL_FH_FLAG_* */ 993a1e819bSAmir Goldstein u8 type; /* fid_type of fid */ 10001633fd2SChristoph Hellwig uuid_t uuid; /* uuid of filesystem */ 1010efbe7c4SGustavo A. R. Silva u32 fid[]; /* file identifier should be 32bit aligned in-memory */ 1023a1e819bSAmir Goldstein } __packed; 103e9be9d5eSMiklos Szeredi 104cbe7fba8SAmir Goldstein /* In-memory and on-wire format for overlay file handle */ 105cbe7fba8SAmir Goldstein struct ovl_fh { 106cbe7fba8SAmir Goldstein u8 padding[3]; /* make sure fb.fid is 32bit aligned */ 107cbe7fba8SAmir Goldstein union { 108cbe7fba8SAmir Goldstein struct ovl_fb fb; 109cbe7fba8SAmir Goldstein u8 buf[0]; 110cbe7fba8SAmir Goldstein }; 111cbe7fba8SAmir Goldstein } __packed; 112cbe7fba8SAmir Goldstein 113cbe7fba8SAmir Goldstein #define OVL_FH_WIRE_OFFSET offsetof(struct ovl_fh, fb) 114cbe7fba8SAmir Goldstein #define OVL_FH_LEN(fh) (OVL_FH_WIRE_OFFSET + (fh)->fb.len) 115cbe7fba8SAmir Goldstein #define OVL_FH_FID_OFFSET (OVL_FH_WIRE_OFFSET + \ 116cbe7fba8SAmir Goldstein offsetof(struct ovl_fb, fid)) 117cbe7fba8SAmir Goldstein 1182d2f2d73SMiklos Szeredi extern const char *const ovl_xattr_table[][2]; 11943d193f8SMiklos Szeredi static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox) 12043d193f8SMiklos Szeredi { 1212d2f2d73SMiklos Szeredi return ovl_xattr_table[ox][ofs->config.userxattr]; 12243d193f8SMiklos Szeredi } 12343d193f8SMiklos Szeredi 124e9be9d5eSMiklos Szeredi static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry) 125e9be9d5eSMiklos Szeredi { 1266521f891SChristian Brauner int err = vfs_rmdir(&init_user_ns, dir, dentry); 1276cf00764SAmir Goldstein 128e9be9d5eSMiklos Szeredi pr_debug("rmdir(%pd2) = %i\n", dentry, err); 129e9be9d5eSMiklos Szeredi return err; 130e9be9d5eSMiklos Szeredi } 131e9be9d5eSMiklos Szeredi 132e9be9d5eSMiklos Szeredi static inline int ovl_do_unlink(struct inode *dir, struct dentry *dentry) 133e9be9d5eSMiklos Szeredi { 1346521f891SChristian Brauner int err = vfs_unlink(&init_user_ns, dir, dentry, NULL); 1356cf00764SAmir Goldstein 136e9be9d5eSMiklos Szeredi pr_debug("unlink(%pd2) = %i\n", dentry, err); 137e9be9d5eSMiklos Szeredi return err; 138e9be9d5eSMiklos Szeredi } 139e9be9d5eSMiklos Szeredi 140e9be9d5eSMiklos Szeredi static inline int ovl_do_link(struct dentry *old_dentry, struct inode *dir, 1416cf00764SAmir Goldstein struct dentry *new_dentry) 142e9be9d5eSMiklos Szeredi { 1436521f891SChristian Brauner int err = vfs_link(old_dentry, &init_user_ns, dir, new_dentry, NULL); 1446cf00764SAmir Goldstein 1456cf00764SAmir Goldstein pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err); 146e9be9d5eSMiklos Szeredi return err; 147e9be9d5eSMiklos Szeredi } 148e9be9d5eSMiklos Szeredi 149e9be9d5eSMiklos Szeredi static inline int ovl_do_create(struct inode *dir, struct dentry *dentry, 1506cf00764SAmir Goldstein umode_t mode) 151e9be9d5eSMiklos Szeredi { 1526521f891SChristian Brauner int err = vfs_create(&init_user_ns, dir, dentry, mode, true); 1536cf00764SAmir Goldstein 154e9be9d5eSMiklos Szeredi pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err); 155e9be9d5eSMiklos Szeredi return err; 156e9be9d5eSMiklos Szeredi } 157e9be9d5eSMiklos Szeredi 158e9be9d5eSMiklos Szeredi static inline int ovl_do_mkdir(struct inode *dir, struct dentry *dentry, 1596cf00764SAmir Goldstein umode_t mode) 160e9be9d5eSMiklos Szeredi { 1616521f891SChristian Brauner int err = vfs_mkdir(&init_user_ns, dir, dentry, mode); 162e9be9d5eSMiklos Szeredi pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err); 163e9be9d5eSMiklos Szeredi return err; 164e9be9d5eSMiklos Szeredi } 165e9be9d5eSMiklos Szeredi 166e9be9d5eSMiklos Szeredi static inline int ovl_do_mknod(struct inode *dir, struct dentry *dentry, 1676cf00764SAmir Goldstein umode_t mode, dev_t dev) 168e9be9d5eSMiklos Szeredi { 1696521f891SChristian Brauner int err = vfs_mknod(&init_user_ns, dir, dentry, mode, dev); 1706cf00764SAmir Goldstein 1716cf00764SAmir Goldstein pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err); 172e9be9d5eSMiklos Szeredi return err; 173e9be9d5eSMiklos Szeredi } 174e9be9d5eSMiklos Szeredi 175e9be9d5eSMiklos Szeredi static inline int ovl_do_symlink(struct inode *dir, struct dentry *dentry, 1766cf00764SAmir Goldstein const char *oldname) 177e9be9d5eSMiklos Szeredi { 1786521f891SChristian Brauner int err = vfs_symlink(&init_user_ns, dir, dentry, oldname); 1796cf00764SAmir Goldstein 180e9be9d5eSMiklos Szeredi pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err); 181e9be9d5eSMiklos Szeredi return err; 182e9be9d5eSMiklos Szeredi } 183e9be9d5eSMiklos Szeredi 184610afc0bSMiklos Szeredi static inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry, 18543d193f8SMiklos Szeredi enum ovl_xattr ox, void *value, 186610afc0bSMiklos Szeredi size_t size) 187d5dc7486SMiklos Szeredi { 18843d193f8SMiklos Szeredi const char *name = ovl_xattr(ofs, ox); 189c7c7a1a1STycho Andersen return vfs_getxattr(&init_user_ns, dentry, name, value, size); 190d5dc7486SMiklos Szeredi } 191d5dc7486SMiklos Szeredi 192610afc0bSMiklos Szeredi static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry, 19343d193f8SMiklos Szeredi enum ovl_xattr ox, const void *value, 194610afc0bSMiklos Szeredi size_t size) 195e9be9d5eSMiklos Szeredi { 19643d193f8SMiklos Szeredi const char *name = ovl_xattr(ofs, ox); 197c7c7a1a1STycho Andersen int err = vfs_setxattr(&init_user_ns, dentry, name, value, size, 0); 19826150ab5SMiklos Szeredi pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n", 19926150ab5SMiklos Szeredi dentry, name, min((int)size, 48), value, size, err); 200e9be9d5eSMiklos Szeredi return err; 201e9be9d5eSMiklos Szeredi } 202e9be9d5eSMiklos Szeredi 203610afc0bSMiklos Szeredi static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry, 20443d193f8SMiklos Szeredi enum ovl_xattr ox) 205e9be9d5eSMiklos Szeredi { 20643d193f8SMiklos Szeredi const char *name = ovl_xattr(ofs, ox); 207c7c7a1a1STycho Andersen int err = vfs_removexattr(&init_user_ns, dentry, name); 208e9be9d5eSMiklos Szeredi pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err); 209e9be9d5eSMiklos Szeredi return err; 210e9be9d5eSMiklos Szeredi } 211e9be9d5eSMiklos Szeredi 212e9be9d5eSMiklos Szeredi static inline int ovl_do_rename(struct inode *olddir, struct dentry *olddentry, 213e9be9d5eSMiklos Szeredi struct inode *newdir, struct dentry *newdentry, 214e9be9d5eSMiklos Szeredi unsigned int flags) 215e9be9d5eSMiklos Szeredi { 216e9be9d5eSMiklos Szeredi int err; 2179fe61450SChristian Brauner struct renamedata rd = { 2186521f891SChristian Brauner .old_mnt_userns = &init_user_ns, 2199fe61450SChristian Brauner .old_dir = olddir, 2209fe61450SChristian Brauner .old_dentry = olddentry, 2216521f891SChristian Brauner .new_mnt_userns = &init_user_ns, 2229fe61450SChristian Brauner .new_dir = newdir, 2239fe61450SChristian Brauner .new_dentry = newdentry, 2249fe61450SChristian Brauner .flags = flags, 2259fe61450SChristian Brauner }; 226e9be9d5eSMiklos Szeredi 2276cf00764SAmir Goldstein pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags); 2289fe61450SChristian Brauner err = vfs_rename(&rd); 229e9be9d5eSMiklos Szeredi if (err) { 2302773bf00SMiklos Szeredi pr_debug("...rename(%pd2, %pd2, ...) = %i\n", 231e9be9d5eSMiklos Szeredi olddentry, newdentry, err); 232e9be9d5eSMiklos Szeredi } 233e9be9d5eSMiklos Szeredi return err; 234e9be9d5eSMiklos Szeredi } 235e9be9d5eSMiklos Szeredi 236e9be9d5eSMiklos Szeredi static inline int ovl_do_whiteout(struct inode *dir, struct dentry *dentry) 237e9be9d5eSMiklos Szeredi { 2386521f891SChristian Brauner int err = vfs_whiteout(&init_user_ns, dir, dentry); 239e9be9d5eSMiklos Szeredi pr_debug("whiteout(%pd2) = %i\n", dentry, err); 240e9be9d5eSMiklos Szeredi return err; 241e9be9d5eSMiklos Szeredi } 242e9be9d5eSMiklos Szeredi 243e7f52429SAmir Goldstein static inline struct dentry *ovl_do_tmpfile(struct dentry *dentry, umode_t mode) 244e7f52429SAmir Goldstein { 2456521f891SChristian Brauner struct dentry *ret = vfs_tmpfile(&init_user_ns, dentry, mode, 0); 2467879cb43SVasyl Gomonovych int err = PTR_ERR_OR_ZERO(ret); 247e7f52429SAmir Goldstein 248e7f52429SAmir Goldstein pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err); 249e7f52429SAmir Goldstein return ret; 250e7f52429SAmir Goldstein } 251e7f52429SAmir Goldstein 2520c288874SVivek Goyal static inline bool ovl_open_flags_need_copy_up(int flags) 2530c288874SVivek Goyal { 2540c288874SVivek Goyal if (!flags) 2550c288874SVivek Goyal return false; 2560c288874SVivek Goyal 2570c288874SVivek Goyal return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)); 2580c288874SVivek Goyal } 2590c288874SVivek Goyal 260bbb1e54dSMiklos Szeredi /* util.c */ 261bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry); 262bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry); 263bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry); 264bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb); 265e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb); 26602bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb); 267f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb); 268f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb); 269bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower); 270bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry); 271f4288844SMiklos Szeredi void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry, 272f4288844SMiklos Szeredi unsigned int mask); 273bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry); 274e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry); 275e9be9d5eSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path); 276e9be9d5eSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path); 2774f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path); 278e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path); 279e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry); 280e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry); 281647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry); 28213464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry); 283e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry); 2841d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode); 28509d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode); 28609d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode); 2872664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode); 28809d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode); 2894823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode); 2904edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode); 2914edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache); 292c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry); 293c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry); 294c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry); 295e9be9d5eSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry); 296c412ce49SMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry); 2975cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry); 29855acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry); 29955acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry); 3000c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags); 3010c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags); 3020c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode); 3030c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode); 304a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb); 305a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry); 306a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect); 30709d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry); 308d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity); 309bbb1e54dSMiklos Szeredi u64 ovl_dentry_version_get(struct dentry *dentry); 310bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry); 311e9be9d5eSMiklos Szeredi struct file *ovl_path_open(struct path *path, int flags); 3120c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags); 31339d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry); 3140c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags); 315610afc0bSMiklos Szeredi bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry); 316610afc0bSMiklos Szeredi bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry, 31743d193f8SMiklos Szeredi enum ovl_xattr ox); 318f3a15685SAmir Goldstein int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, 31943d193f8SMiklos Szeredi enum ovl_xattr ox, const void *value, size_t size, 320f3a15685SAmir Goldstein int xerr); 321f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry); 322ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry); 323ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry); 324146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry); 32524b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry); 3260e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry); 3270e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry); 3285820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir); 329610afc0bSMiklos Szeredi int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry); 33067d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry); 331610afc0bSMiklos Szeredi char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry, 332610afc0bSMiklos Szeredi int padding); 333335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs); 334f3a15685SAmir Goldstein 335*65cd913eSAmir Goldstein static inline void ovl_set_flag(unsigned long flag, struct inode *inode) 336*65cd913eSAmir Goldstein { 337*65cd913eSAmir Goldstein set_bit(flag, &OVL_I(inode)->flags); 338*65cd913eSAmir Goldstein } 339*65cd913eSAmir Goldstein 340*65cd913eSAmir Goldstein static inline void ovl_clear_flag(unsigned long flag, struct inode *inode) 341*65cd913eSAmir Goldstein { 342*65cd913eSAmir Goldstein clear_bit(flag, &OVL_I(inode)->flags); 343*65cd913eSAmir Goldstein } 344*65cd913eSAmir Goldstein 345*65cd913eSAmir Goldstein static inline bool ovl_test_flag(unsigned long flag, struct inode *inode) 346*65cd913eSAmir Goldstein { 347*65cd913eSAmir Goldstein return test_bit(flag, &OVL_I(inode)->flags); 348*65cd913eSAmir Goldstein } 349*65cd913eSAmir Goldstein 350610afc0bSMiklos Szeredi static inline bool ovl_is_impuredir(struct super_block *sb, 351610afc0bSMiklos Szeredi struct dentry *dentry) 352f3a15685SAmir Goldstein { 353610afc0bSMiklos Szeredi return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_IMPURE); 354f3a15685SAmir Goldstein } 355f3a15685SAmir Goldstein 356926e94d7SAmir Goldstein /* 357926e94d7SAmir Goldstein * With xino=auto, we do best effort to keep all inodes on same st_dev and 358926e94d7SAmir Goldstein * d_ino consistent with st_ino. 359926e94d7SAmir Goldstein * With xino=on, we do the same effort but we warn if we failed. 360926e94d7SAmir Goldstein */ 361926e94d7SAmir Goldstein static inline bool ovl_xino_warn(struct super_block *sb) 362926e94d7SAmir Goldstein { 363926e94d7SAmir Goldstein return OVL_FS(sb)->config.xino == OVL_XINO_ON; 364926e94d7SAmir Goldstein } 365926e94d7SAmir Goldstein 3660f831ec8SAmir Goldstein /* All layers on same fs? */ 3670f831ec8SAmir Goldstein static inline bool ovl_same_fs(struct super_block *sb) 3680f831ec8SAmir Goldstein { 3690f831ec8SAmir Goldstein return OVL_FS(sb)->xino_mode == 0; 3700f831ec8SAmir Goldstein } 3710f831ec8SAmir Goldstein 3720f831ec8SAmir Goldstein /* All overlay inodes have same st_dev? */ 3730f831ec8SAmir Goldstein static inline bool ovl_same_dev(struct super_block *sb) 3740f831ec8SAmir Goldstein { 3750f831ec8SAmir Goldstein return OVL_FS(sb)->xino_mode >= 0; 3760f831ec8SAmir Goldstein } 3770f831ec8SAmir Goldstein 378e487d889SAmir Goldstein static inline unsigned int ovl_xino_bits(struct super_block *sb) 379e487d889SAmir Goldstein { 3800f831ec8SAmir Goldstein return ovl_same_dev(sb) ? OVL_FS(sb)->xino_mode : 0; 381e487d889SAmir Goldstein } 382e487d889SAmir Goldstein 383531d3040SAmir Goldstein static inline void ovl_inode_lock(struct inode *inode) 384531d3040SAmir Goldstein { 385531d3040SAmir Goldstein mutex_lock(&OVL_I(inode)->lock); 386531d3040SAmir Goldstein } 387531d3040SAmir Goldstein 388531d3040SAmir Goldstein static inline int ovl_inode_lock_interruptible(struct inode *inode) 3891e92e307SAmir Goldstein { 3901e92e307SAmir Goldstein return mutex_lock_interruptible(&OVL_I(inode)->lock); 3911e92e307SAmir Goldstein } 3921e92e307SAmir Goldstein 3931e92e307SAmir Goldstein static inline void ovl_inode_unlock(struct inode *inode) 3941e92e307SAmir Goldstein { 3951e92e307SAmir Goldstein mutex_unlock(&OVL_I(inode)->lock); 3961e92e307SAmir Goldstein } 3971e92e307SAmir Goldstein 398e9be9d5eSMiklos Szeredi 399bbb1e54dSMiklos Szeredi /* namei.c */ 400cbe7fba8SAmir Goldstein int ovl_check_fb_len(struct ovl_fb *fb, int fb_len); 401cbe7fba8SAmir Goldstein 402cbe7fba8SAmir Goldstein static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len) 403cbe7fba8SAmir Goldstein { 404522f6e6cSAmir Goldstein if (fh_len < sizeof(struct ovl_fh)) 405522f6e6cSAmir Goldstein return -EINVAL; 406522f6e6cSAmir Goldstein 407cbe7fba8SAmir Goldstein return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET); 408cbe7fba8SAmir Goldstein } 409cbe7fba8SAmir Goldstein 4101cdb0cb6SPavel Tikhomirov struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh, 4111cdb0cb6SPavel Tikhomirov struct vfsmount *mnt, bool connected); 4128a22efa1SAmir Goldstein int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected, 413f941866fSAmir Goldstein struct dentry *upperdentry, struct ovl_path **stackp); 414610afc0bSMiklos Szeredi int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry, 41543d193f8SMiklos Szeredi enum ovl_xattr ox, struct dentry *real, bool is_upper, 416610afc0bSMiklos Szeredi bool set); 4173b0bfc6eSAmir Goldstein struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index); 4181eff1a1dSAmir Goldstein int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index); 4191cdb0cb6SPavel Tikhomirov int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin, 4201cdb0cb6SPavel Tikhomirov struct qstr *name); 42191ffe7beSAmir Goldstein struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh); 42206170154SAmir Goldstein struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper, 42306170154SAmir Goldstein struct dentry *origin, bool verify); 424bbb1e54dSMiklos Szeredi int ovl_path_next(int idx, struct dentry *dentry, struct path *path); 4251eff1a1dSAmir Goldstein struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 4261eff1a1dSAmir Goldstein unsigned int flags); 427bbb1e54dSMiklos Szeredi bool ovl_lower_positive(struct dentry *dentry); 428e9be9d5eSMiklos Szeredi 429610afc0bSMiklos Szeredi static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper, 43005122443SAmir Goldstein struct dentry *origin, bool set) 43105122443SAmir Goldstein { 432610afc0bSMiklos Szeredi return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin, 433610afc0bSMiklos Szeredi false, set); 43405122443SAmir Goldstein } 43505122443SAmir Goldstein 436610afc0bSMiklos Szeredi static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index, 437ad1d615cSAmir Goldstein struct dentry *upper, bool set) 438ad1d615cSAmir Goldstein { 439610afc0bSMiklos Szeredi return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set); 440ad1d615cSAmir Goldstein } 441ad1d615cSAmir Goldstein 442e9be9d5eSMiklos Szeredi /* readdir.c */ 443e9be9d5eSMiklos Szeredi extern const struct file_operations ovl_dir_operations; 44461536bedSAmir Goldstein struct file *ovl_dir_real_file(const struct file *file, bool want_upper); 445e9be9d5eSMiklos Szeredi int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list); 446e9be9d5eSMiklos Szeredi void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list); 447e9be9d5eSMiklos Szeredi void ovl_cache_free(struct list_head *list); 4484edb83bbSMiklos Szeredi void ovl_dir_cache_free(struct inode *inode); 44945aebeafSVivek Goyal int ovl_check_d_type_supported(struct path *realpath); 4503011645bSAmir Goldstein int ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt, 451eea2fb48SMiklos Szeredi struct dentry *dentry, int level); 4521eff1a1dSAmir Goldstein int ovl_indexdir_cleanup(struct ovl_fs *ofs); 453e9be9d5eSMiklos Szeredi 454*65cd913eSAmir Goldstein /* 455*65cd913eSAmir Goldstein * Can we iterate real dir directly? 456*65cd913eSAmir Goldstein * 457*65cd913eSAmir Goldstein * Non-merge dir may contain whiteouts from a time it was a merge upper, before 458*65cd913eSAmir Goldstein * lower dir was removed under it and possibly before it was rotated from upper 459*65cd913eSAmir Goldstein * to lower layer. 460*65cd913eSAmir Goldstein */ 461*65cd913eSAmir Goldstein static inline bool ovl_dir_is_real(struct dentry *dir) 462*65cd913eSAmir Goldstein { 463*65cd913eSAmir Goldstein return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir)); 464*65cd913eSAmir Goldstein } 465*65cd913eSAmir Goldstein 466e9be9d5eSMiklos Szeredi /* inode.c */ 4675f8415d6SAmir Goldstein int ovl_set_nlink_upper(struct dentry *dentry); 4685f8415d6SAmir Goldstein int ovl_set_nlink_lower(struct dentry *dentry); 469610afc0bSMiklos Szeredi unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry, 470caf70cb2SAmir Goldstein struct dentry *upperdentry, 471caf70cb2SAmir Goldstein unsigned int fallback); 472549c7297SChristian Brauner int ovl_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, 473549c7297SChristian Brauner struct iattr *attr); 474549c7297SChristian Brauner int ovl_getattr(struct user_namespace *mnt_userns, const struct path *path, 475549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int flags); 476549c7297SChristian Brauner int ovl_permission(struct user_namespace *mnt_userns, struct inode *inode, 477549c7297SChristian Brauner int mask); 4781d88f183SMiklos Szeredi int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, 4791d88f183SMiklos Szeredi const void *value, size_t size, int flags); 4801d88f183SMiklos Szeredi int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, 4810eb45fc3SAndreas Gruenbacher void *value, size_t size); 482e9be9d5eSMiklos Szeredi ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size); 48339a25b2bSVivek Goyal struct posix_acl *ovl_get_acl(struct inode *inode, int type); 48495582b00SDeepa Dinamani int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags); 485610afc0bSMiklos Szeredi bool ovl_is_private_xattr(struct super_block *sb, const char *name); 486e9be9d5eSMiklos Szeredi 487ac6a52ebSVivek Goyal struct ovl_inode_params { 48801b39dccSAmir Goldstein struct inode *newinode; 489ac6a52ebSVivek Goyal struct dentry *upperdentry; 490ac6a52ebSVivek Goyal struct ovl_path *lowerpath; 49174c6e384SMiklos Szeredi bool index; 492ac6a52ebSVivek Goyal unsigned int numlower; 4939cec54c8SVivek Goyal char *redirect; 4942664bd08SVivek Goyal struct dentry *lowerdata; 495ac6a52ebSVivek Goyal }; 49662c832edSAmir Goldstein void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, 49762c832edSAmir Goldstein unsigned long ino, int fsid); 498ca4c8a3aSMiklos Szeredi struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev); 4994b91c30aSAmir Goldstein struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, 5004b91c30aSAmir Goldstein bool is_upper); 501146d62e5SAmir Goldstein bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir); 502146d62e5SAmir Goldstein struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir); 503ac6a52ebSVivek Goyal struct inode *ovl_get_inode(struct super_block *sb, 504ac6a52ebSVivek Goyal struct ovl_inode_params *oip); 505e9be9d5eSMiklos Szeredi static inline void ovl_copyattr(struct inode *from, struct inode *to) 506e9be9d5eSMiklos Szeredi { 507e9be9d5eSMiklos Szeredi to->i_uid = from->i_uid; 508e9be9d5eSMiklos Szeredi to->i_gid = from->i_gid; 50907a2daabSVivek Goyal to->i_mode = from->i_mode; 510d719e8f2SMiklos Szeredi to->i_atime = from->i_atime; 511d719e8f2SMiklos Szeredi to->i_mtime = from->i_mtime; 512d719e8f2SMiklos Szeredi to->i_ctime = from->i_ctime; 51346e5d0a3SMiklos Szeredi i_size_write(to, i_size_read(from)); 514e9be9d5eSMiklos Szeredi } 515e9be9d5eSMiklos Szeredi 5164f357295SMiklos Szeredi static inline void ovl_copyflags(struct inode *from, struct inode *to) 5174f357295SMiklos Szeredi { 5184f357295SMiklos Szeredi unsigned int mask = S_SYNC | S_IMMUTABLE | S_APPEND | S_NOATIME; 5194f357295SMiklos Szeredi 5204f357295SMiklos Szeredi inode_set_flags(to, from->i_flags & mask, mask); 5214f357295SMiklos Szeredi } 5224f357295SMiklos Szeredi 523e9be9d5eSMiklos Szeredi /* dir.c */ 524e9be9d5eSMiklos Szeredi extern const struct inode_operations ovl_dir_inode_operations; 525c21c839bSChengguang Xu int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir, 526e7dd0e71SAmir Goldstein struct dentry *dentry); 527471ec5dcSAmir Goldstein struct ovl_cattr { 52832a3d848SAl Viro dev_t rdev; 52932a3d848SAl Viro umode_t mode; 53032a3d848SAl Viro const char *link; 531471ec5dcSAmir Goldstein struct dentry *hardlink; 53232a3d848SAl Viro }; 533471ec5dcSAmir Goldstein 534471ec5dcSAmir Goldstein #define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) }) 535471ec5dcSAmir Goldstein 53695a1c815SMiklos Szeredi struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry, 537471ec5dcSAmir Goldstein struct ovl_cattr *attr); 538415543d5SAmir Goldstein int ovl_cleanup(struct inode *dir, struct dentry *dentry); 539cad218abSAmir Goldstein struct dentry *ovl_lookup_temp(struct dentry *workdir); 540137ec526SAmir Goldstein struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr); 541e9be9d5eSMiklos Szeredi 542d1d04ef8SMiklos Szeredi /* file.c */ 543d1d04ef8SMiklos Szeredi extern const struct file_operations ovl_file_operations; 5442406a307SJiufei Xue int __init ovl_aio_request_cache_init(void); 5452406a307SJiufei Xue void ovl_aio_request_cache_destroy(void); 54661536bedSAmir Goldstein long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 54761536bedSAmir Goldstein long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 548d1d04ef8SMiklos Szeredi 549e9be9d5eSMiklos Szeredi /* copy_up.c */ 550e9be9d5eSMiklos Szeredi int ovl_copy_up(struct dentry *dentry); 551d1e6f6a9SVivek Goyal int ovl_copy_up_with_data(struct dentry *dentry); 5523428030dSAmir Goldstein int ovl_maybe_copy_up(struct dentry *dentry, int flags); 553610afc0bSMiklos Szeredi int ovl_copy_xattr(struct super_block *sb, struct dentry *old, 554610afc0bSMiklos Szeredi struct dentry *new); 555e9be9d5eSMiklos Szeredi int ovl_set_attr(struct dentry *upper, struct kstat *stat); 5561cdb0cb6SPavel Tikhomirov struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real, 5571cdb0cb6SPavel Tikhomirov bool is_upper); 5581cdb0cb6SPavel Tikhomirov int ovl_set_origin(struct ovl_fs *ofs, struct dentry *dentry, 5591cdb0cb6SPavel Tikhomirov struct dentry *lower, struct dentry *upper); 5608ed5eec9SAmir Goldstein 5618ed5eec9SAmir Goldstein /* export.c */ 5628ed5eec9SAmir Goldstein extern const struct export_operations ovl_export_operations; 563