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 25fe2b7595SAndreas Gruenbacher #define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay." 2643d193f8SMiklos Szeredi 2743d193f8SMiklos Szeredi enum ovl_xattr { 2843d193f8SMiklos Szeredi OVL_XATTR_OPAQUE, 2943d193f8SMiklos Szeredi OVL_XATTR_REDIRECT, 3043d193f8SMiklos Szeredi OVL_XATTR_ORIGIN, 3143d193f8SMiklos Szeredi OVL_XATTR_IMPURE, 3243d193f8SMiklos Szeredi OVL_XATTR_NLINK, 3343d193f8SMiklos Szeredi OVL_XATTR_UPPER, 3443d193f8SMiklos Szeredi OVL_XATTR_METACOPY, 3543d193f8SMiklos Szeredi }; 363a1e819bSAmir Goldstein 37c62520a8SAmir Goldstein enum ovl_inode_flag { 38b79e05aaSAmir Goldstein /* Pure upper dir that may contain non pure upper entries */ 3913c72075SMiklos Szeredi OVL_IMPURE, 40b79e05aaSAmir Goldstein /* Non-merge dir that may contain whiteout entries */ 41b79e05aaSAmir Goldstein OVL_WHITEOUTS, 42359f392cSAmir Goldstein OVL_INDEX, 430c288874SVivek Goyal OVL_UPPERDATA, 44a00c2d59SVivek Goyal /* Inode number will remain constant over copy up. */ 45a00c2d59SVivek Goyal OVL_CONST_INO, 4613c72075SMiklos Szeredi }; 4713c72075SMiklos Szeredi 48c62520a8SAmir Goldstein enum ovl_entry_flag { 49c62520a8SAmir Goldstein OVL_E_UPPER_ALIAS, 50c62520a8SAmir Goldstein OVL_E_OPAQUE, 512ca3c148SAmir Goldstein OVL_E_CONNECTED, 52c62520a8SAmir Goldstein }; 53c62520a8SAmir Goldstein 54926e94d7SAmir Goldstein enum { 55926e94d7SAmir Goldstein OVL_XINO_OFF, 56926e94d7SAmir Goldstein OVL_XINO_AUTO, 57926e94d7SAmir Goldstein OVL_XINO_ON, 58926e94d7SAmir Goldstein }; 59926e94d7SAmir Goldstein 603a1e819bSAmir Goldstein /* 613a1e819bSAmir Goldstein * The tuple (fh,uuid) is a universal unique identifier for a copy up origin, 623a1e819bSAmir Goldstein * where: 633a1e819bSAmir Goldstein * origin.fh - exported file handle of the lower file 643a1e819bSAmir Goldstein * origin.uuid - uuid of the lower filesystem 653a1e819bSAmir Goldstein */ 663a1e819bSAmir Goldstein #define OVL_FH_VERSION 0 673a1e819bSAmir Goldstein #define OVL_FH_MAGIC 0xfb 683a1e819bSAmir Goldstein 693a1e819bSAmir Goldstein /* CPU byte order required for fid decoding: */ 703a1e819bSAmir Goldstein #define OVL_FH_FLAG_BIG_ENDIAN (1 << 0) 713a1e819bSAmir Goldstein #define OVL_FH_FLAG_ANY_ENDIAN (1 << 1) 7254fb347eSAmir Goldstein /* Is the real inode encoded in fid an upper inode? */ 7354fb347eSAmir Goldstein #define OVL_FH_FLAG_PATH_UPPER (1 << 2) 743a1e819bSAmir Goldstein 75961af647SAmir Goldstein #define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \ 76961af647SAmir Goldstein OVL_FH_FLAG_PATH_UPPER) 773a1e819bSAmir Goldstein 783a1e819bSAmir Goldstein #if defined(__LITTLE_ENDIAN) 793a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN 0 803a1e819bSAmir Goldstein #elif defined(__BIG_ENDIAN) 813a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN 823a1e819bSAmir Goldstein #else 833a1e819bSAmir Goldstein #error Endianness not defined 843a1e819bSAmir Goldstein #endif 853a1e819bSAmir Goldstein 86cbe7fba8SAmir Goldstein /* The type used to be returned by overlay exportfs for misaligned fid */ 87cbe7fba8SAmir Goldstein #define OVL_FILEID_V0 0xfb 88cbe7fba8SAmir Goldstein /* The type returned by overlay exportfs for 32bit aligned fid */ 89cbe7fba8SAmir Goldstein #define OVL_FILEID_V1 0xf8 908ed5eec9SAmir Goldstein 91cbe7fba8SAmir Goldstein /* On-disk format for "origin" file handle */ 92cbe7fba8SAmir Goldstein struct ovl_fb { 933a1e819bSAmir Goldstein u8 version; /* 0 */ 943a1e819bSAmir Goldstein u8 magic; /* 0xfb */ 953a1e819bSAmir Goldstein u8 len; /* size of this header + size of fid */ 963a1e819bSAmir Goldstein u8 flags; /* OVL_FH_FLAG_* */ 973a1e819bSAmir Goldstein u8 type; /* fid_type of fid */ 9801633fd2SChristoph Hellwig uuid_t uuid; /* uuid of filesystem */ 990efbe7c4SGustavo A. R. Silva u32 fid[]; /* file identifier should be 32bit aligned in-memory */ 1003a1e819bSAmir Goldstein } __packed; 101e9be9d5eSMiklos Szeredi 102cbe7fba8SAmir Goldstein /* In-memory and on-wire format for overlay file handle */ 103cbe7fba8SAmir Goldstein struct ovl_fh { 104cbe7fba8SAmir Goldstein u8 padding[3]; /* make sure fb.fid is 32bit aligned */ 105cbe7fba8SAmir Goldstein union { 106cbe7fba8SAmir Goldstein struct ovl_fb fb; 107cbe7fba8SAmir Goldstein u8 buf[0]; 108cbe7fba8SAmir Goldstein }; 109cbe7fba8SAmir Goldstein } __packed; 110cbe7fba8SAmir Goldstein 111cbe7fba8SAmir Goldstein #define OVL_FH_WIRE_OFFSET offsetof(struct ovl_fh, fb) 112cbe7fba8SAmir Goldstein #define OVL_FH_LEN(fh) (OVL_FH_WIRE_OFFSET + (fh)->fb.len) 113cbe7fba8SAmir Goldstein #define OVL_FH_FID_OFFSET (OVL_FH_WIRE_OFFSET + \ 114cbe7fba8SAmir Goldstein offsetof(struct ovl_fb, fid)) 115cbe7fba8SAmir Goldstein 11643d193f8SMiklos Szeredi extern const char *ovl_xattr_table[]; 11743d193f8SMiklos Szeredi static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox) 11843d193f8SMiklos Szeredi { 11943d193f8SMiklos Szeredi return ovl_xattr_table[ox]; 12043d193f8SMiklos Szeredi } 12143d193f8SMiklos Szeredi 122e9be9d5eSMiklos Szeredi static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry) 123e9be9d5eSMiklos Szeredi { 124e9be9d5eSMiklos Szeredi int err = vfs_rmdir(dir, dentry); 1256cf00764SAmir Goldstein 126e9be9d5eSMiklos Szeredi pr_debug("rmdir(%pd2) = %i\n", dentry, err); 127e9be9d5eSMiklos Szeredi return err; 128e9be9d5eSMiklos Szeredi } 129e9be9d5eSMiklos Szeredi 130e9be9d5eSMiklos Szeredi static inline int ovl_do_unlink(struct inode *dir, struct dentry *dentry) 131e9be9d5eSMiklos Szeredi { 132e9be9d5eSMiklos Szeredi int err = vfs_unlink(dir, dentry, NULL); 1336cf00764SAmir Goldstein 134e9be9d5eSMiklos Szeredi pr_debug("unlink(%pd2) = %i\n", dentry, err); 135e9be9d5eSMiklos Szeredi return err; 136e9be9d5eSMiklos Szeredi } 137e9be9d5eSMiklos Szeredi 138e9be9d5eSMiklos Szeredi static inline int ovl_do_link(struct dentry *old_dentry, struct inode *dir, 1396cf00764SAmir Goldstein struct dentry *new_dentry) 140e9be9d5eSMiklos Szeredi { 141e9be9d5eSMiklos Szeredi int err = vfs_link(old_dentry, dir, new_dentry, NULL); 1426cf00764SAmir Goldstein 1436cf00764SAmir Goldstein pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err); 144e9be9d5eSMiklos Szeredi return err; 145e9be9d5eSMiklos Szeredi } 146e9be9d5eSMiklos Szeredi 147e9be9d5eSMiklos Szeredi static inline int ovl_do_create(struct inode *dir, struct dentry *dentry, 1486cf00764SAmir Goldstein umode_t mode) 149e9be9d5eSMiklos Szeredi { 150e9be9d5eSMiklos Szeredi int err = vfs_create(dir, dentry, mode, true); 1516cf00764SAmir Goldstein 152e9be9d5eSMiklos Szeredi pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err); 153e9be9d5eSMiklos Szeredi return err; 154e9be9d5eSMiklos Szeredi } 155e9be9d5eSMiklos Szeredi 156e9be9d5eSMiklos Szeredi static inline int ovl_do_mkdir(struct inode *dir, struct dentry *dentry, 1576cf00764SAmir Goldstein umode_t mode) 158e9be9d5eSMiklos Szeredi { 159e9be9d5eSMiklos Szeredi int err = vfs_mkdir(dir, dentry, mode); 160e9be9d5eSMiklos Szeredi pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err); 161e9be9d5eSMiklos Szeredi return err; 162e9be9d5eSMiklos Szeredi } 163e9be9d5eSMiklos Szeredi 164e9be9d5eSMiklos Szeredi static inline int ovl_do_mknod(struct inode *dir, struct dentry *dentry, 1656cf00764SAmir Goldstein umode_t mode, dev_t dev) 166e9be9d5eSMiklos Szeredi { 167e9be9d5eSMiklos Szeredi int err = vfs_mknod(dir, dentry, mode, dev); 1686cf00764SAmir Goldstein 1696cf00764SAmir Goldstein pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err); 170e9be9d5eSMiklos Szeredi return err; 171e9be9d5eSMiklos Szeredi } 172e9be9d5eSMiklos Szeredi 173e9be9d5eSMiklos Szeredi static inline int ovl_do_symlink(struct inode *dir, struct dentry *dentry, 1746cf00764SAmir Goldstein const char *oldname) 175e9be9d5eSMiklos Szeredi { 176e9be9d5eSMiklos Szeredi int err = vfs_symlink(dir, dentry, oldname); 1776cf00764SAmir Goldstein 178e9be9d5eSMiklos Szeredi pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err); 179e9be9d5eSMiklos Szeredi return err; 180e9be9d5eSMiklos Szeredi } 181e9be9d5eSMiklos Szeredi 182610afc0bSMiklos Szeredi static inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry, 18343d193f8SMiklos Szeredi enum ovl_xattr ox, void *value, 184610afc0bSMiklos Szeredi size_t size) 185d5dc7486SMiklos Szeredi { 18643d193f8SMiklos Szeredi const char *name = ovl_xattr(ofs, ox); 187d5dc7486SMiklos Szeredi return vfs_getxattr(dentry, name, value, size); 188d5dc7486SMiklos Szeredi } 189d5dc7486SMiklos Szeredi 190610afc0bSMiklos Szeredi static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry, 19143d193f8SMiklos Szeredi enum ovl_xattr ox, const void *value, 192610afc0bSMiklos Szeredi size_t size) 193e9be9d5eSMiklos Szeredi { 19443d193f8SMiklos Szeredi const char *name = ovl_xattr(ofs, ox); 19526150ab5SMiklos Szeredi int err = vfs_setxattr(dentry, name, value, size, 0); 19626150ab5SMiklos Szeredi pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n", 19726150ab5SMiklos Szeredi dentry, name, min((int)size, 48), value, size, err); 198e9be9d5eSMiklos Szeredi return err; 199e9be9d5eSMiklos Szeredi } 200e9be9d5eSMiklos Szeredi 201610afc0bSMiklos Szeredi static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry, 20243d193f8SMiklos Szeredi enum ovl_xattr ox) 203e9be9d5eSMiklos Szeredi { 20443d193f8SMiklos Szeredi const char *name = ovl_xattr(ofs, ox); 205e9be9d5eSMiklos Szeredi int err = vfs_removexattr(dentry, name); 206e9be9d5eSMiklos Szeredi pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err); 207e9be9d5eSMiklos Szeredi return err; 208e9be9d5eSMiklos Szeredi } 209e9be9d5eSMiklos Szeredi 210e9be9d5eSMiklos Szeredi static inline int ovl_do_rename(struct inode *olddir, struct dentry *olddentry, 211e9be9d5eSMiklos Szeredi struct inode *newdir, struct dentry *newdentry, 212e9be9d5eSMiklos Szeredi unsigned int flags) 213e9be9d5eSMiklos Szeredi { 214e9be9d5eSMiklos Szeredi int err; 215e9be9d5eSMiklos Szeredi 2166cf00764SAmir Goldstein pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags); 217e9be9d5eSMiklos Szeredi err = vfs_rename(olddir, olddentry, newdir, newdentry, NULL, flags); 218e9be9d5eSMiklos Szeredi if (err) { 2192773bf00SMiklos Szeredi pr_debug("...rename(%pd2, %pd2, ...) = %i\n", 220e9be9d5eSMiklos Szeredi olddentry, newdentry, err); 221e9be9d5eSMiklos Szeredi } 222e9be9d5eSMiklos Szeredi return err; 223e9be9d5eSMiklos Szeredi } 224e9be9d5eSMiklos Szeredi 225e9be9d5eSMiklos Szeredi static inline int ovl_do_whiteout(struct inode *dir, struct dentry *dentry) 226e9be9d5eSMiklos Szeredi { 227e9be9d5eSMiklos Szeredi int err = vfs_whiteout(dir, dentry); 228e9be9d5eSMiklos Szeredi pr_debug("whiteout(%pd2) = %i\n", dentry, err); 229e9be9d5eSMiklos Szeredi return err; 230e9be9d5eSMiklos Szeredi } 231e9be9d5eSMiklos Szeredi 232e7f52429SAmir Goldstein static inline struct dentry *ovl_do_tmpfile(struct dentry *dentry, umode_t mode) 233e7f52429SAmir Goldstein { 234e7f52429SAmir Goldstein struct dentry *ret = vfs_tmpfile(dentry, mode, 0); 2357879cb43SVasyl Gomonovych int err = PTR_ERR_OR_ZERO(ret); 236e7f52429SAmir Goldstein 237e7f52429SAmir Goldstein pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err); 238e7f52429SAmir Goldstein return ret; 239e7f52429SAmir Goldstein } 240e7f52429SAmir Goldstein 2410c288874SVivek Goyal static inline bool ovl_open_flags_need_copy_up(int flags) 2420c288874SVivek Goyal { 2430c288874SVivek Goyal if (!flags) 2440c288874SVivek Goyal return false; 2450c288874SVivek Goyal 2460c288874SVivek Goyal return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)); 2470c288874SVivek Goyal } 2480c288874SVivek Goyal 249bbb1e54dSMiklos Szeredi /* util.c */ 250bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry); 251bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry); 252bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry); 253bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb); 254e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb); 25502bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb); 256f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb); 257f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb); 258bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower); 259bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry); 260f4288844SMiklos Szeredi void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry, 261f4288844SMiklos Szeredi unsigned int mask); 262bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry); 263e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry); 264e9be9d5eSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path); 265e9be9d5eSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path); 2664f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path); 267e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path); 268e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry); 269e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry); 270647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry); 27113464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry); 272e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry); 2731d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode); 27409d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode); 27509d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode); 2762664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode); 27709d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode); 2784823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode); 2794edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode); 2804edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache); 281c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry); 282c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry); 283c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry); 284e9be9d5eSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry); 285c412ce49SMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry); 2865cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry); 28755acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry); 28855acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry); 2890c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags); 2900c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags); 2910c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode); 2920c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode); 293a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb); 294a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry); 295a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect); 29609d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry); 297d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity); 298bbb1e54dSMiklos Szeredi u64 ovl_dentry_version_get(struct dentry *dentry); 299bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry); 300e9be9d5eSMiklos Szeredi struct file *ovl_path_open(struct path *path, int flags); 3010c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags); 30239d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry); 3030c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags); 304610afc0bSMiklos Szeredi bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry); 305610afc0bSMiklos Szeredi bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry, 30643d193f8SMiklos Szeredi enum ovl_xattr ox); 307f3a15685SAmir Goldstein int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, 30843d193f8SMiklos Szeredi enum ovl_xattr ox, const void *value, size_t size, 309f3a15685SAmir Goldstein int xerr); 310f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry); 31113c72075SMiklos Szeredi void ovl_set_flag(unsigned long flag, struct inode *inode); 3124edb83bbSMiklos Szeredi void ovl_clear_flag(unsigned long flag, struct inode *inode); 31313c72075SMiklos Szeredi bool ovl_test_flag(unsigned long flag, struct inode *inode); 314ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry); 315ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry); 316146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry); 31724b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry); 3180e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry); 3190e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry); 3205820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir); 321610afc0bSMiklos Szeredi int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry); 32267d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry); 323610afc0bSMiklos Szeredi char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry, 324610afc0bSMiklos Szeredi int padding); 325f3a15685SAmir Goldstein 326610afc0bSMiklos Szeredi static inline bool ovl_is_impuredir(struct super_block *sb, 327610afc0bSMiklos Szeredi struct dentry *dentry) 328f3a15685SAmir Goldstein { 329610afc0bSMiklos Szeredi return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_IMPURE); 330f3a15685SAmir Goldstein } 331f3a15685SAmir Goldstein 332926e94d7SAmir Goldstein /* 333926e94d7SAmir Goldstein * With xino=auto, we do best effort to keep all inodes on same st_dev and 334926e94d7SAmir Goldstein * d_ino consistent with st_ino. 335926e94d7SAmir Goldstein * With xino=on, we do the same effort but we warn if we failed. 336926e94d7SAmir Goldstein */ 337926e94d7SAmir Goldstein static inline bool ovl_xino_warn(struct super_block *sb) 338926e94d7SAmir Goldstein { 339926e94d7SAmir Goldstein return OVL_FS(sb)->config.xino == OVL_XINO_ON; 340926e94d7SAmir Goldstein } 341926e94d7SAmir Goldstein 3420f831ec8SAmir Goldstein /* All layers on same fs? */ 3430f831ec8SAmir Goldstein static inline bool ovl_same_fs(struct super_block *sb) 3440f831ec8SAmir Goldstein { 3450f831ec8SAmir Goldstein return OVL_FS(sb)->xino_mode == 0; 3460f831ec8SAmir Goldstein } 3470f831ec8SAmir Goldstein 3480f831ec8SAmir Goldstein /* All overlay inodes have same st_dev? */ 3490f831ec8SAmir Goldstein static inline bool ovl_same_dev(struct super_block *sb) 3500f831ec8SAmir Goldstein { 3510f831ec8SAmir Goldstein return OVL_FS(sb)->xino_mode >= 0; 3520f831ec8SAmir Goldstein } 3530f831ec8SAmir Goldstein 354e487d889SAmir Goldstein static inline unsigned int ovl_xino_bits(struct super_block *sb) 355e487d889SAmir Goldstein { 3560f831ec8SAmir Goldstein return ovl_same_dev(sb) ? OVL_FS(sb)->xino_mode : 0; 357e487d889SAmir Goldstein } 358e487d889SAmir Goldstein 359531d3040SAmir Goldstein static inline void ovl_inode_lock(struct inode *inode) 360531d3040SAmir Goldstein { 361531d3040SAmir Goldstein mutex_lock(&OVL_I(inode)->lock); 362531d3040SAmir Goldstein } 363531d3040SAmir Goldstein 364531d3040SAmir Goldstein static inline int ovl_inode_lock_interruptible(struct inode *inode) 3651e92e307SAmir Goldstein { 3661e92e307SAmir Goldstein return mutex_lock_interruptible(&OVL_I(inode)->lock); 3671e92e307SAmir Goldstein } 3681e92e307SAmir Goldstein 3691e92e307SAmir Goldstein static inline void ovl_inode_unlock(struct inode *inode) 3701e92e307SAmir Goldstein { 3711e92e307SAmir Goldstein mutex_unlock(&OVL_I(inode)->lock); 3721e92e307SAmir Goldstein } 3731e92e307SAmir Goldstein 374e9be9d5eSMiklos Szeredi 375bbb1e54dSMiklos Szeredi /* namei.c */ 376cbe7fba8SAmir Goldstein int ovl_check_fb_len(struct ovl_fb *fb, int fb_len); 377cbe7fba8SAmir Goldstein 378cbe7fba8SAmir Goldstein static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len) 379cbe7fba8SAmir Goldstein { 380522f6e6cSAmir Goldstein if (fh_len < sizeof(struct ovl_fh)) 381522f6e6cSAmir Goldstein return -EINVAL; 382522f6e6cSAmir Goldstein 383cbe7fba8SAmir Goldstein return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET); 384cbe7fba8SAmir Goldstein } 385cbe7fba8SAmir Goldstein 3868a22efa1SAmir Goldstein struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt, 3878a22efa1SAmir Goldstein bool connected); 3888a22efa1SAmir Goldstein int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected, 389f941866fSAmir Goldstein struct dentry *upperdentry, struct ovl_path **stackp); 390610afc0bSMiklos Szeredi int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry, 39143d193f8SMiklos Szeredi enum ovl_xattr ox, struct dentry *real, bool is_upper, 392610afc0bSMiklos Szeredi bool set); 3933b0bfc6eSAmir Goldstein struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index); 3941eff1a1dSAmir Goldstein int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index); 395359f392cSAmir Goldstein int ovl_get_index_name(struct dentry *origin, struct qstr *name); 39691ffe7beSAmir Goldstein struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh); 39706170154SAmir Goldstein struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper, 39806170154SAmir Goldstein struct dentry *origin, bool verify); 399bbb1e54dSMiklos Szeredi int ovl_path_next(int idx, struct dentry *dentry, struct path *path); 4001eff1a1dSAmir Goldstein struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 4011eff1a1dSAmir Goldstein unsigned int flags); 402bbb1e54dSMiklos Szeredi bool ovl_lower_positive(struct dentry *dentry); 403e9be9d5eSMiklos Szeredi 404610afc0bSMiklos Szeredi static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper, 40505122443SAmir Goldstein struct dentry *origin, bool set) 40605122443SAmir Goldstein { 407610afc0bSMiklos Szeredi return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin, 408610afc0bSMiklos Szeredi false, set); 40905122443SAmir Goldstein } 41005122443SAmir Goldstein 411610afc0bSMiklos Szeredi static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index, 412ad1d615cSAmir Goldstein struct dentry *upper, bool set) 413ad1d615cSAmir Goldstein { 414610afc0bSMiklos Szeredi return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set); 415ad1d615cSAmir Goldstein } 416ad1d615cSAmir Goldstein 417e9be9d5eSMiklos Szeredi /* readdir.c */ 418e9be9d5eSMiklos Szeredi extern const struct file_operations ovl_dir_operations; 419*61536bedSAmir Goldstein struct file *ovl_dir_real_file(const struct file *file, bool want_upper); 420e9be9d5eSMiklos Szeredi int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list); 421e9be9d5eSMiklos Szeredi void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list); 422e9be9d5eSMiklos Szeredi void ovl_cache_free(struct list_head *list); 4234edb83bbSMiklos Szeredi void ovl_dir_cache_free(struct inode *inode); 42445aebeafSVivek Goyal int ovl_check_d_type_supported(struct path *realpath); 4253011645bSAmir Goldstein int ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt, 426eea2fb48SMiklos Szeredi struct dentry *dentry, int level); 4271eff1a1dSAmir Goldstein int ovl_indexdir_cleanup(struct ovl_fs *ofs); 428e9be9d5eSMiklos Szeredi 429e9be9d5eSMiklos Szeredi /* inode.c */ 4305f8415d6SAmir Goldstein int ovl_set_nlink_upper(struct dentry *dentry); 4315f8415d6SAmir Goldstein int ovl_set_nlink_lower(struct dentry *dentry); 432610afc0bSMiklos Szeredi unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry, 433caf70cb2SAmir Goldstein struct dentry *upperdentry, 434caf70cb2SAmir Goldstein unsigned int fallback); 435e9be9d5eSMiklos Szeredi int ovl_setattr(struct dentry *dentry, struct iattr *attr); 4365b712091SMiklos Szeredi int ovl_getattr(const struct path *path, struct kstat *stat, 4375b712091SMiklos Szeredi u32 request_mask, unsigned int flags); 438e9be9d5eSMiklos Szeredi int ovl_permission(struct inode *inode, int mask); 4391d88f183SMiklos Szeredi int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, 4401d88f183SMiklos Szeredi const void *value, size_t size, int flags); 4411d88f183SMiklos Szeredi int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, 4420eb45fc3SAndreas Gruenbacher void *value, size_t size); 443e9be9d5eSMiklos Szeredi ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size); 44439a25b2bSVivek Goyal struct posix_acl *ovl_get_acl(struct inode *inode, int type); 44595582b00SDeepa Dinamani int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags); 446610afc0bSMiklos Szeredi bool ovl_is_private_xattr(struct super_block *sb, const char *name); 447e9be9d5eSMiklos Szeredi 448ac6a52ebSVivek Goyal struct ovl_inode_params { 44901b39dccSAmir Goldstein struct inode *newinode; 450ac6a52ebSVivek Goyal struct dentry *upperdentry; 451ac6a52ebSVivek Goyal struct ovl_path *lowerpath; 45274c6e384SMiklos Szeredi bool index; 453ac6a52ebSVivek Goyal unsigned int numlower; 4549cec54c8SVivek Goyal char *redirect; 4552664bd08SVivek Goyal struct dentry *lowerdata; 456ac6a52ebSVivek Goyal }; 45762c832edSAmir Goldstein void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, 45862c832edSAmir Goldstein unsigned long ino, int fsid); 459ca4c8a3aSMiklos Szeredi struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev); 4604b91c30aSAmir Goldstein struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, 4614b91c30aSAmir Goldstein bool is_upper); 462146d62e5SAmir Goldstein bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir); 463146d62e5SAmir Goldstein struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir); 464ac6a52ebSVivek Goyal struct inode *ovl_get_inode(struct super_block *sb, 465ac6a52ebSVivek Goyal struct ovl_inode_params *oip); 466e9be9d5eSMiklos Szeredi static inline void ovl_copyattr(struct inode *from, struct inode *to) 467e9be9d5eSMiklos Szeredi { 468e9be9d5eSMiklos Szeredi to->i_uid = from->i_uid; 469e9be9d5eSMiklos Szeredi to->i_gid = from->i_gid; 47007a2daabSVivek Goyal to->i_mode = from->i_mode; 471d719e8f2SMiklos Szeredi to->i_atime = from->i_atime; 472d719e8f2SMiklos Szeredi to->i_mtime = from->i_mtime; 473d719e8f2SMiklos Szeredi to->i_ctime = from->i_ctime; 47446e5d0a3SMiklos Szeredi i_size_write(to, i_size_read(from)); 475e9be9d5eSMiklos Szeredi } 476e9be9d5eSMiklos Szeredi 4774f357295SMiklos Szeredi static inline void ovl_copyflags(struct inode *from, struct inode *to) 4784f357295SMiklos Szeredi { 4794f357295SMiklos Szeredi unsigned int mask = S_SYNC | S_IMMUTABLE | S_APPEND | S_NOATIME; 4804f357295SMiklos Szeredi 4814f357295SMiklos Szeredi inode_set_flags(to, from->i_flags & mask, mask); 4824f357295SMiklos Szeredi } 4834f357295SMiklos Szeredi 484e9be9d5eSMiklos Szeredi /* dir.c */ 485e9be9d5eSMiklos Szeredi extern const struct inode_operations ovl_dir_inode_operations; 486c21c839bSChengguang Xu int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir, 487e7dd0e71SAmir Goldstein struct dentry *dentry); 488471ec5dcSAmir Goldstein struct ovl_cattr { 48932a3d848SAl Viro dev_t rdev; 49032a3d848SAl Viro umode_t mode; 49132a3d848SAl Viro const char *link; 492471ec5dcSAmir Goldstein struct dentry *hardlink; 49332a3d848SAl Viro }; 494471ec5dcSAmir Goldstein 495471ec5dcSAmir Goldstein #define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) }) 496471ec5dcSAmir Goldstein 49795a1c815SMiklos Szeredi struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry, 498471ec5dcSAmir Goldstein struct ovl_cattr *attr); 499415543d5SAmir Goldstein int ovl_cleanup(struct inode *dir, struct dentry *dentry); 500cad218abSAmir Goldstein struct dentry *ovl_lookup_temp(struct dentry *workdir); 501137ec526SAmir Goldstein struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr); 502e9be9d5eSMiklos Szeredi 503d1d04ef8SMiklos Szeredi /* file.c */ 504d1d04ef8SMiklos Szeredi extern const struct file_operations ovl_file_operations; 5052406a307SJiufei Xue int __init ovl_aio_request_cache_init(void); 5062406a307SJiufei Xue void ovl_aio_request_cache_destroy(void); 507*61536bedSAmir Goldstein long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 508*61536bedSAmir Goldstein long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 509d1d04ef8SMiklos Szeredi 510e9be9d5eSMiklos Szeredi /* copy_up.c */ 511e9be9d5eSMiklos Szeredi int ovl_copy_up(struct dentry *dentry); 512d1e6f6a9SVivek Goyal int ovl_copy_up_with_data(struct dentry *dentry); 5133428030dSAmir Goldstein int ovl_maybe_copy_up(struct dentry *dentry, int flags); 514610afc0bSMiklos Szeredi int ovl_copy_xattr(struct super_block *sb, struct dentry *old, 515610afc0bSMiklos Szeredi struct dentry *new); 516e9be9d5eSMiklos Szeredi int ovl_set_attr(struct dentry *upper, struct kstat *stat); 5175b2cccd3SAmir Goldstein struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper); 5189678e630SAmir Goldstein int ovl_set_origin(struct dentry *dentry, struct dentry *lower, 5199678e630SAmir Goldstein struct dentry *upper); 5208ed5eec9SAmir Goldstein 5218ed5eec9SAmir Goldstein /* export.c */ 5228ed5eec9SAmir Goldstein extern const struct export_operations ovl_export_operations; 523