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> 10bf070890SAlexander Larsson #include <linux/fsverity.h> 1122f289ceSChristian Brauner #include <linux/namei.h> 120e641857SChristian Brauner #include <linux/posix_acl.h> 130e641857SChristian Brauner #include <linux/posix_acl_xattr.h> 14ee023c30SAmir Goldstein #include "ovl_entry.h" 15e9be9d5eSMiklos Szeredi 161bd0a3aeSlijiazi #undef pr_fmt 171bd0a3aeSlijiazi #define pr_fmt(fmt) "overlayfs: " fmt 181bd0a3aeSlijiazi 19e9be9d5eSMiklos Szeredi enum ovl_path_type { 2038e813dbSMiklos Szeredi __OVL_PATH_UPPER = (1 << 0), 2138e813dbSMiklos Szeredi __OVL_PATH_MERGE = (1 << 1), 2259548503SAmir Goldstein __OVL_PATH_ORIGIN = (1 << 2), 23e9be9d5eSMiklos Szeredi }; 24e9be9d5eSMiklos Szeredi 251afaba1eSMiklos Szeredi #define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER) 261afaba1eSMiklos Szeredi #define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE) 2759548503SAmir Goldstein #define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN) 28d837a49bSMiklos Szeredi 292d2f2d73SMiklos Szeredi #define OVL_XATTR_NAMESPACE "overlay." 302d2f2d73SMiklos Szeredi #define OVL_XATTR_TRUSTED_PREFIX XATTR_TRUSTED_PREFIX OVL_XATTR_NAMESPACE 312d2f2d73SMiklos Szeredi #define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE 3243d193f8SMiklos Szeredi 3343d193f8SMiklos Szeredi enum ovl_xattr { 3443d193f8SMiklos Szeredi OVL_XATTR_OPAQUE, 3543d193f8SMiklos Szeredi OVL_XATTR_REDIRECT, 3643d193f8SMiklos Szeredi OVL_XATTR_ORIGIN, 3743d193f8SMiklos Szeredi OVL_XATTR_IMPURE, 3843d193f8SMiklos Szeredi OVL_XATTR_NLINK, 3943d193f8SMiklos Szeredi OVL_XATTR_UPPER, 40d9544c1bSAmir Goldstein OVL_XATTR_UUID, 4143d193f8SMiklos Szeredi OVL_XATTR_METACOPY, 42096a218aSAmir Goldstein OVL_XATTR_PROTATTR, 4343d193f8SMiklos Szeredi }; 443a1e819bSAmir Goldstein 45c62520a8SAmir Goldstein enum ovl_inode_flag { 46b79e05aaSAmir Goldstein /* Pure upper dir that may contain non pure upper entries */ 4713c72075SMiklos Szeredi OVL_IMPURE, 48b79e05aaSAmir Goldstein /* Non-merge dir that may contain whiteout entries */ 49b79e05aaSAmir Goldstein OVL_WHITEOUTS, 50359f392cSAmir Goldstein OVL_INDEX, 510c288874SVivek Goyal OVL_UPPERDATA, 52a00c2d59SVivek Goyal /* Inode number will remain constant over copy up. */ 53a00c2d59SVivek Goyal OVL_CONST_INO, 54184996e9SAlexander Larsson OVL_HAS_DIGEST, 55184996e9SAlexander Larsson OVL_VERIFIED_DIGEST, 5613c72075SMiklos Szeredi }; 5713c72075SMiklos Szeredi 58c62520a8SAmir Goldstein enum ovl_entry_flag { 59c62520a8SAmir Goldstein OVL_E_UPPER_ALIAS, 60c62520a8SAmir Goldstein OVL_E_OPAQUE, 612ca3c148SAmir Goldstein OVL_E_CONNECTED, 62c62520a8SAmir Goldstein }; 63c62520a8SAmir Goldstein 64926e94d7SAmir Goldstein enum { 65af5f2396SAmir Goldstein OVL_REDIRECT_OFF, /* "off" mode is never used. In effect */ 66af5f2396SAmir Goldstein OVL_REDIRECT_FOLLOW, /* ...it translates to either "follow" */ 67af5f2396SAmir Goldstein OVL_REDIRECT_NOFOLLOW, /* ...or "nofollow". */ 68af5f2396SAmir Goldstein OVL_REDIRECT_ON, 69af5f2396SAmir Goldstein }; 70af5f2396SAmir Goldstein 71af5f2396SAmir Goldstein enum { 72b0504bfeSAmir Goldstein OVL_UUID_OFF, 73b0504bfeSAmir Goldstein OVL_UUID_NULL, 74*cbb44f09SAmir Goldstein OVL_UUID_AUTO, 75b0504bfeSAmir Goldstein OVL_UUID_ON, 76b0504bfeSAmir Goldstein }; 77b0504bfeSAmir Goldstein 78b0504bfeSAmir Goldstein enum { 79926e94d7SAmir Goldstein OVL_XINO_OFF, 80926e94d7SAmir Goldstein OVL_XINO_AUTO, 81926e94d7SAmir Goldstein OVL_XINO_ON, 82926e94d7SAmir Goldstein }; 83926e94d7SAmir Goldstein 84ae8cba40SAlexander Larsson enum { 85ae8cba40SAlexander Larsson OVL_VERITY_OFF, 86ae8cba40SAlexander Larsson OVL_VERITY_ON, 87ae8cba40SAlexander Larsson OVL_VERITY_REQUIRE, 88ae8cba40SAlexander Larsson }; 89ae8cba40SAlexander Larsson 903a1e819bSAmir Goldstein /* 913a1e819bSAmir Goldstein * The tuple (fh,uuid) is a universal unique identifier for a copy up origin, 923a1e819bSAmir Goldstein * where: 933a1e819bSAmir Goldstein * origin.fh - exported file handle of the lower file 943a1e819bSAmir Goldstein * origin.uuid - uuid of the lower filesystem 953a1e819bSAmir Goldstein */ 963a1e819bSAmir Goldstein #define OVL_FH_VERSION 0 973a1e819bSAmir Goldstein #define OVL_FH_MAGIC 0xfb 983a1e819bSAmir Goldstein 993a1e819bSAmir Goldstein /* CPU byte order required for fid decoding: */ 1003a1e819bSAmir Goldstein #define OVL_FH_FLAG_BIG_ENDIAN (1 << 0) 1013a1e819bSAmir Goldstein #define OVL_FH_FLAG_ANY_ENDIAN (1 << 1) 10254fb347eSAmir Goldstein /* Is the real inode encoded in fid an upper inode? */ 10354fb347eSAmir Goldstein #define OVL_FH_FLAG_PATH_UPPER (1 << 2) 1043a1e819bSAmir Goldstein 105961af647SAmir Goldstein #define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \ 106961af647SAmir Goldstein OVL_FH_FLAG_PATH_UPPER) 1073a1e819bSAmir Goldstein 1083a1e819bSAmir Goldstein #if defined(__LITTLE_ENDIAN) 1093a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN 0 1103a1e819bSAmir Goldstein #elif defined(__BIG_ENDIAN) 1113a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN 1123a1e819bSAmir Goldstein #else 1133a1e819bSAmir Goldstein #error Endianness not defined 1143a1e819bSAmir Goldstein #endif 1153a1e819bSAmir Goldstein 116cbe7fba8SAmir Goldstein /* The type used to be returned by overlay exportfs for misaligned fid */ 117cbe7fba8SAmir Goldstein #define OVL_FILEID_V0 0xfb 118cbe7fba8SAmir Goldstein /* The type returned by overlay exportfs for 32bit aligned fid */ 119cbe7fba8SAmir Goldstein #define OVL_FILEID_V1 0xf8 1208ed5eec9SAmir Goldstein 121cbe7fba8SAmir Goldstein /* On-disk format for "origin" file handle */ 122cbe7fba8SAmir Goldstein struct ovl_fb { 1233a1e819bSAmir Goldstein u8 version; /* 0 */ 1243a1e819bSAmir Goldstein u8 magic; /* 0xfb */ 1253a1e819bSAmir Goldstein u8 len; /* size of this header + size of fid */ 1263a1e819bSAmir Goldstein u8 flags; /* OVL_FH_FLAG_* */ 1273a1e819bSAmir Goldstein u8 type; /* fid_type of fid */ 12801633fd2SChristoph Hellwig uuid_t uuid; /* uuid of filesystem */ 1290efbe7c4SGustavo A. R. Silva u32 fid[]; /* file identifier should be 32bit aligned in-memory */ 1303a1e819bSAmir Goldstein } __packed; 131e9be9d5eSMiklos Szeredi 132cbe7fba8SAmir Goldstein /* In-memory and on-wire format for overlay file handle */ 133cbe7fba8SAmir Goldstein struct ovl_fh { 134cbe7fba8SAmir Goldstein u8 padding[3]; /* make sure fb.fid is 32bit aligned */ 135cbe7fba8SAmir Goldstein union { 136cbe7fba8SAmir Goldstein struct ovl_fb fb; 137cf8aa9bfSKees Cook DECLARE_FLEX_ARRAY(u8, buf); 138cbe7fba8SAmir Goldstein }; 139cbe7fba8SAmir Goldstein } __packed; 140cbe7fba8SAmir Goldstein 141cbe7fba8SAmir Goldstein #define OVL_FH_WIRE_OFFSET offsetof(struct ovl_fh, fb) 142cbe7fba8SAmir Goldstein #define OVL_FH_LEN(fh) (OVL_FH_WIRE_OFFSET + (fh)->fb.len) 143cbe7fba8SAmir Goldstein #define OVL_FH_FID_OFFSET (OVL_FH_WIRE_OFFSET + \ 144cbe7fba8SAmir Goldstein offsetof(struct ovl_fb, fid)) 145cbe7fba8SAmir Goldstein 146bf070890SAlexander Larsson /* On-disk format for "metacopy" xattr (if non-zero size) */ 147bf070890SAlexander Larsson struct ovl_metacopy { 148bf070890SAlexander Larsson u8 version; /* 0 */ 149bf070890SAlexander Larsson u8 len; /* size of this header + used digest bytes */ 150bf070890SAlexander Larsson u8 flags; 151bf070890SAlexander Larsson u8 digest_algo; /* FS_VERITY_HASH_ALG_* constant, 0 for no digest */ 152bf070890SAlexander Larsson u8 digest[FS_VERITY_MAX_DIGEST_SIZE]; /* Only the used part on disk */ 153bf070890SAlexander Larsson } __packed; 154bf070890SAlexander Larsson 155bf070890SAlexander Larsson #define OVL_METACOPY_MAX_SIZE (sizeof(struct ovl_metacopy)) 156bf070890SAlexander Larsson #define OVL_METACOPY_MIN_SIZE (OVL_METACOPY_MAX_SIZE - FS_VERITY_MAX_DIGEST_SIZE) 157bf070890SAlexander Larsson #define OVL_METACOPY_INIT { 0, OVL_METACOPY_MIN_SIZE } 158bf070890SAlexander Larsson 159bf070890SAlexander Larsson static inline int ovl_metadata_digest_size(const struct ovl_metacopy *metacopy) 160bf070890SAlexander Larsson { 161bf070890SAlexander Larsson if (metacopy->len < OVL_METACOPY_MIN_SIZE) 162bf070890SAlexander Larsson return 0; 163bf070890SAlexander Larsson return (int)metacopy->len - OVL_METACOPY_MIN_SIZE; 164bf070890SAlexander Larsson } 165bf070890SAlexander Larsson 1662d2f2d73SMiklos Szeredi extern const char *const ovl_xattr_table[][2]; 16743d193f8SMiklos Szeredi static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox) 16843d193f8SMiklos Szeredi { 1692d2f2d73SMiklos Szeredi return ovl_xattr_table[ox][ofs->config.userxattr]; 17043d193f8SMiklos Szeredi } 17143d193f8SMiklos Szeredi 172a15506eaSChristian Brauner /* 173a15506eaSChristian Brauner * When changing ownership of an upper object map the intended ownership 174a15506eaSChristian Brauner * according to the upper layer's idmapping. When an upper mount idmaps files 175a15506eaSChristian Brauner * that are stored on-disk as owned by id 1001 to id 1000 this means stat on 176a15506eaSChristian Brauner * this object will report it as being owned by id 1000 when calling stat via 177a15506eaSChristian Brauner * the upper mount. 178a15506eaSChristian Brauner * In order to change ownership of an object so stat reports id 1000 when 179a15506eaSChristian Brauner * called on an idmapped upper mount the value written to disk - i.e., the 180a15506eaSChristian Brauner * value stored in ia_*id - must 1001. The mount mapping helper will thus take 181a15506eaSChristian Brauner * care to map 1000 to 1001. 182a15506eaSChristian Brauner * The mnt idmapping helpers are nops if the upper layer isn't idmapped. 183a15506eaSChristian Brauner */ 184a15506eaSChristian Brauner static inline int ovl_do_notify_change(struct ovl_fs *ofs, 185a15506eaSChristian Brauner struct dentry *upperdentry, 186a15506eaSChristian Brauner struct iattr *attr) 187a15506eaSChristian Brauner { 188abf08576SChristian Brauner return notify_change(ovl_upper_mnt_idmap(ofs), upperdentry, attr, NULL); 189a15506eaSChristian Brauner } 190a15506eaSChristian Brauner 191576bb263SChristian Brauner static inline int ovl_do_rmdir(struct ovl_fs *ofs, 192576bb263SChristian Brauner struct inode *dir, struct dentry *dentry) 193e9be9d5eSMiklos Szeredi { 194abf08576SChristian Brauner int err = vfs_rmdir(ovl_upper_mnt_idmap(ofs), dir, dentry); 1956cf00764SAmir Goldstein 196e9be9d5eSMiklos Szeredi pr_debug("rmdir(%pd2) = %i\n", dentry, err); 197e9be9d5eSMiklos Szeredi return err; 198e9be9d5eSMiklos Szeredi } 199e9be9d5eSMiklos Szeredi 200576bb263SChristian Brauner static inline int ovl_do_unlink(struct ovl_fs *ofs, struct inode *dir, 201576bb263SChristian Brauner struct dentry *dentry) 202e9be9d5eSMiklos Szeredi { 203abf08576SChristian Brauner int err = vfs_unlink(ovl_upper_mnt_idmap(ofs), dir, dentry, NULL); 2046cf00764SAmir Goldstein 205e9be9d5eSMiklos Szeredi pr_debug("unlink(%pd2) = %i\n", dentry, err); 206e9be9d5eSMiklos Szeredi return err; 207e9be9d5eSMiklos Szeredi } 208e9be9d5eSMiklos Szeredi 209576bb263SChristian Brauner static inline int ovl_do_link(struct ovl_fs *ofs, struct dentry *old_dentry, 210576bb263SChristian Brauner struct inode *dir, struct dentry *new_dentry) 211e9be9d5eSMiklos Szeredi { 212abf08576SChristian Brauner int err = vfs_link(old_dentry, ovl_upper_mnt_idmap(ofs), dir, 213abf08576SChristian Brauner new_dentry, NULL); 2146cf00764SAmir Goldstein 2156cf00764SAmir Goldstein pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err); 216e9be9d5eSMiklos Szeredi return err; 217e9be9d5eSMiklos Szeredi } 218e9be9d5eSMiklos Szeredi 219576bb263SChristian Brauner static inline int ovl_do_create(struct ovl_fs *ofs, 220576bb263SChristian Brauner struct inode *dir, struct dentry *dentry, 2216cf00764SAmir Goldstein umode_t mode) 222e9be9d5eSMiklos Szeredi { 223abf08576SChristian Brauner int err = vfs_create(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, true); 2246cf00764SAmir Goldstein 225e9be9d5eSMiklos Szeredi pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err); 226e9be9d5eSMiklos Szeredi return err; 227e9be9d5eSMiklos Szeredi } 228e9be9d5eSMiklos Szeredi 229576bb263SChristian Brauner static inline int ovl_do_mkdir(struct ovl_fs *ofs, 230576bb263SChristian Brauner struct inode *dir, struct dentry *dentry, 2316cf00764SAmir Goldstein umode_t mode) 232e9be9d5eSMiklos Szeredi { 233abf08576SChristian Brauner int err = vfs_mkdir(ovl_upper_mnt_idmap(ofs), dir, dentry, mode); 234e9be9d5eSMiklos Szeredi pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err); 235e9be9d5eSMiklos Szeredi return err; 236e9be9d5eSMiklos Szeredi } 237e9be9d5eSMiklos Szeredi 238576bb263SChristian Brauner static inline int ovl_do_mknod(struct ovl_fs *ofs, 239576bb263SChristian Brauner struct inode *dir, struct dentry *dentry, 2406cf00764SAmir Goldstein umode_t mode, dev_t dev) 241e9be9d5eSMiklos Szeredi { 242abf08576SChristian Brauner int err = vfs_mknod(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, dev); 2436cf00764SAmir Goldstein 2446cf00764SAmir Goldstein pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err); 245e9be9d5eSMiklos Szeredi return err; 246e9be9d5eSMiklos Szeredi } 247e9be9d5eSMiklos Szeredi 248576bb263SChristian Brauner static inline int ovl_do_symlink(struct ovl_fs *ofs, 249576bb263SChristian Brauner struct inode *dir, struct dentry *dentry, 2506cf00764SAmir Goldstein const char *oldname) 251e9be9d5eSMiklos Szeredi { 252abf08576SChristian Brauner int err = vfs_symlink(ovl_upper_mnt_idmap(ofs), dir, dentry, oldname); 2536cf00764SAmir Goldstein 254e9be9d5eSMiklos Szeredi pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err); 255e9be9d5eSMiklos Szeredi return err; 256e9be9d5eSMiklos Szeredi } 257e9be9d5eSMiklos Szeredi 2582d343087SAl Viro static inline ssize_t ovl_do_getxattr(const struct path *path, const char *name, 259dad7017aSChristian Brauner void *value, size_t size) 260d5dc7486SMiklos Szeredi { 261dad7017aSChristian Brauner int err, len; 262dad7017aSChristian Brauner 263dad7017aSChristian Brauner WARN_ON(path->dentry->d_sb != path->mnt->mnt_sb); 264dad7017aSChristian Brauner 2654609e1f1SChristian Brauner err = vfs_getxattr(mnt_idmap(path->mnt), path->dentry, 266dad7017aSChristian Brauner name, value, size); 267dad7017aSChristian Brauner len = (value && err > 0) ? err : 0; 2685e717c6fSAmir Goldstein 2695e717c6fSAmir Goldstein pr_debug("getxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n", 270dad7017aSChristian Brauner path->dentry, name, min(len, 48), value, size, err); 2715e717c6fSAmir Goldstein return err; 272d5dc7486SMiklos Szeredi } 273d5dc7486SMiklos Szeredi 274dad7017aSChristian Brauner static inline ssize_t ovl_getxattr_upper(struct ovl_fs *ofs, 275dad7017aSChristian Brauner struct dentry *upperdentry, 276c914c0e2SAmir Goldstein enum ovl_xattr ox, void *value, 277610afc0bSMiklos Szeredi size_t size) 278e9be9d5eSMiklos Szeredi { 279dad7017aSChristian Brauner struct path upperpath = { 280dad7017aSChristian Brauner .dentry = upperdentry, 281dad7017aSChristian Brauner .mnt = ovl_upper_mnt(ofs), 282dad7017aSChristian Brauner }; 283dad7017aSChristian Brauner 284dad7017aSChristian Brauner return ovl_do_getxattr(&upperpath, ovl_xattr(ofs, ox), value, size); 285dad7017aSChristian Brauner } 286dad7017aSChristian Brauner 287dad7017aSChristian Brauner static inline ssize_t ovl_path_getxattr(struct ovl_fs *ofs, 2882d343087SAl Viro const struct path *path, 289dad7017aSChristian Brauner enum ovl_xattr ox, void *value, 290dad7017aSChristian Brauner size_t size) 291dad7017aSChristian Brauner { 292dad7017aSChristian Brauner return ovl_do_getxattr(path, ovl_xattr(ofs, ox), value, size); 293c914c0e2SAmir Goldstein } 294c914c0e2SAmir Goldstein 295c914c0e2SAmir Goldstein static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry, 296c914c0e2SAmir Goldstein const char *name, const void *value, 297c914c0e2SAmir Goldstein size_t size, int flags) 298c914c0e2SAmir Goldstein { 2994609e1f1SChristian Brauner int err = vfs_setxattr(ovl_upper_mnt_idmap(ofs), dentry, name, 3006344e669SChristian Brauner value, size, flags); 301c914c0e2SAmir Goldstein 302c914c0e2SAmir Goldstein pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, %d) = %i\n", 303c914c0e2SAmir Goldstein dentry, name, min((int)size, 48), value, size, flags, err); 304e9be9d5eSMiklos Szeredi return err; 305e9be9d5eSMiklos Szeredi } 306e9be9d5eSMiklos Szeredi 307c914c0e2SAmir Goldstein static inline int ovl_setxattr(struct ovl_fs *ofs, struct dentry *dentry, 308c914c0e2SAmir Goldstein enum ovl_xattr ox, const void *value, 309c914c0e2SAmir Goldstein size_t size) 310e9be9d5eSMiklos Szeredi { 311c914c0e2SAmir Goldstein return ovl_do_setxattr(ofs, dentry, ovl_xattr(ofs, ox), value, size, 0); 312c914c0e2SAmir Goldstein } 313c914c0e2SAmir Goldstein 314c914c0e2SAmir Goldstein static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry, 315c914c0e2SAmir Goldstein const char *name) 316c914c0e2SAmir Goldstein { 3174609e1f1SChristian Brauner int err = vfs_removexattr(ovl_upper_mnt_idmap(ofs), dentry, name); 318e9be9d5eSMiklos Szeredi pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err); 319e9be9d5eSMiklos Szeredi return err; 320e9be9d5eSMiklos Szeredi } 321e9be9d5eSMiklos Szeredi 322c914c0e2SAmir Goldstein static inline int ovl_removexattr(struct ovl_fs *ofs, struct dentry *dentry, 323c914c0e2SAmir Goldstein enum ovl_xattr ox) 324c914c0e2SAmir Goldstein { 325c914c0e2SAmir Goldstein return ovl_do_removexattr(ofs, dentry, ovl_xattr(ofs, ox)); 326c914c0e2SAmir Goldstein } 327c914c0e2SAmir Goldstein 3280e641857SChristian Brauner static inline int ovl_do_set_acl(struct ovl_fs *ofs, struct dentry *dentry, 3290e641857SChristian Brauner const char *acl_name, struct posix_acl *acl) 3300e641857SChristian Brauner { 33113e83a49SChristian Brauner return vfs_set_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name, acl); 3320e641857SChristian Brauner } 3330e641857SChristian Brauner 3340e641857SChristian Brauner static inline int ovl_do_remove_acl(struct ovl_fs *ofs, struct dentry *dentry, 3350e641857SChristian Brauner const char *acl_name) 3360e641857SChristian Brauner { 33713e83a49SChristian Brauner return vfs_remove_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name); 3380e641857SChristian Brauner } 3390e641857SChristian Brauner 340576bb263SChristian Brauner static inline int ovl_do_rename(struct ovl_fs *ofs, struct inode *olddir, 341576bb263SChristian Brauner struct dentry *olddentry, struct inode *newdir, 342576bb263SChristian Brauner struct dentry *newdentry, unsigned int flags) 343e9be9d5eSMiklos Szeredi { 344e9be9d5eSMiklos Szeredi int err; 3459fe61450SChristian Brauner struct renamedata rd = { 346abf08576SChristian Brauner .old_mnt_idmap = ovl_upper_mnt_idmap(ofs), 3479fe61450SChristian Brauner .old_dir = olddir, 3489fe61450SChristian Brauner .old_dentry = olddentry, 349abf08576SChristian Brauner .new_mnt_idmap = ovl_upper_mnt_idmap(ofs), 3509fe61450SChristian Brauner .new_dir = newdir, 3519fe61450SChristian Brauner .new_dentry = newdentry, 3529fe61450SChristian Brauner .flags = flags, 3539fe61450SChristian Brauner }; 354e9be9d5eSMiklos Szeredi 3556cf00764SAmir Goldstein pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags); 3569fe61450SChristian Brauner err = vfs_rename(&rd); 357e9be9d5eSMiklos Szeredi if (err) { 3582773bf00SMiklos Szeredi pr_debug("...rename(%pd2, %pd2, ...) = %i\n", 359e9be9d5eSMiklos Szeredi olddentry, newdentry, err); 360e9be9d5eSMiklos Szeredi } 361e9be9d5eSMiklos Szeredi return err; 362e9be9d5eSMiklos Szeredi } 363e9be9d5eSMiklos Szeredi 364576bb263SChristian Brauner static inline int ovl_do_whiteout(struct ovl_fs *ofs, 365576bb263SChristian Brauner struct inode *dir, struct dentry *dentry) 366e9be9d5eSMiklos Szeredi { 367abf08576SChristian Brauner int err = vfs_whiteout(ovl_upper_mnt_idmap(ofs), dir, dentry); 368e9be9d5eSMiklos Szeredi pr_debug("whiteout(%pd2) = %i\n", dentry, err); 369e9be9d5eSMiklos Szeredi return err; 370e9be9d5eSMiklos Szeredi } 371e9be9d5eSMiklos Szeredi 3722b1a7746SMiklos Szeredi static inline struct file *ovl_do_tmpfile(struct ovl_fs *ofs, 373576bb263SChristian Brauner struct dentry *dentry, umode_t mode) 374e7f52429SAmir Goldstein { 3752b1a7746SMiklos Szeredi struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = dentry }; 376d56e0ddbSAmir Goldstein struct file *file = kernel_tmpfile_open(ovl_upper_mnt_idmap(ofs), &path, 377d56e0ddbSAmir Goldstein mode, O_LARGEFILE | O_WRONLY, 378d56e0ddbSAmir Goldstein current_cred()); 3792b1a7746SMiklos Szeredi int err = PTR_ERR_OR_ZERO(file); 380e7f52429SAmir Goldstein 381e7f52429SAmir Goldstein pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err); 3822b1a7746SMiklos Szeredi return file; 383e7f52429SAmir Goldstein } 384e7f52429SAmir Goldstein 38522f289ceSChristian Brauner static inline struct dentry *ovl_lookup_upper(struct ovl_fs *ofs, 38622f289ceSChristian Brauner const char *name, 38722f289ceSChristian Brauner struct dentry *base, int len) 38822f289ceSChristian Brauner { 3894609e1f1SChristian Brauner return lookup_one(ovl_upper_mnt_idmap(ofs), name, base, len); 39022f289ceSChristian Brauner } 39122f289ceSChristian Brauner 3920c288874SVivek Goyal static inline bool ovl_open_flags_need_copy_up(int flags) 3930c288874SVivek Goyal { 3940c288874SVivek Goyal if (!flags) 3950c288874SVivek Goyal return false; 3960c288874SVivek Goyal 3970c288874SVivek Goyal return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)); 3980c288874SVivek Goyal } 3990c288874SVivek Goyal 400bbb1e54dSMiklos Szeredi /* util.c */ 401bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry); 402bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry); 403bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry); 404bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb); 405e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb); 40602bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb); 407f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb); 408f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb); 409163db0daSAmir Goldstein struct ovl_path *ovl_stack_alloc(unsigned int n); 410163db0daSAmir Goldstein void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n); 411163db0daSAmir Goldstein void ovl_stack_put(struct ovl_path *stack, unsigned int n); 412163db0daSAmir Goldstein void ovl_stack_free(struct ovl_path *stack, unsigned int n); 413bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower); 414163db0daSAmir Goldstein void ovl_free_entry(struct ovl_entry *oe); 415bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry); 416b07d5cc9SAmir Goldstein void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry); 4170af950f5SAmir Goldstein void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry, 4180af950f5SAmir Goldstein struct ovl_entry *oe); 419b07d5cc9SAmir Goldstein void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry, 4200af950f5SAmir Goldstein struct ovl_entry *oe, unsigned int mask); 421bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry); 422e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry); 423e9be9d5eSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path); 424e9be9d5eSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path); 4254f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path); 426b2dd05f1SZhihao Cheng struct inode *ovl_i_path_real(struct inode *inode, struct path *path); 427e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path); 4281248ea4bSAmir Goldstein enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path); 429e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry); 430e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry); 431647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry); 43242dd69aeSAmir Goldstein int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath); 433ffa5723cSAmir Goldstein const struct ovl_layer *ovl_i_layer_lower(struct inode *inode); 43413464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry); 435e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry); 4361d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode); 43709d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode); 43809d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode); 4392664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode); 44009d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode); 4414823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode); 4422b21da92SAmir Goldstein const char *ovl_lowerdata_redirect(struct inode *inode); 4434edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode); 4444edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache); 445c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry); 446c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry); 447c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry); 448e9be9d5eSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry); 449c412ce49SMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry); 4505cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry); 45155acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry); 45255acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry); 4530c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags); 4540c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags); 4550c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode); 4560c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode); 457a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry); 458a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect); 45909d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry); 460d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity); 4611fa9c5c5SMiklos Szeredi u64 ovl_inode_version_get(struct inode *inode); 462bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry); 4632d343087SAl Viro struct file *ovl_path_open(const struct path *path, int flags); 4640c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags); 46539d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry); 4660c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags); 4672d343087SAl Viro bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path, 46843d193f8SMiklos Szeredi enum ovl_xattr ox); 4692d343087SAl Viro bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path); 470d9544c1bSAmir Goldstein bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs, 471d9544c1bSAmir Goldstein const struct path *upperpath); 472dad7017aSChristian Brauner 473dad7017aSChristian Brauner static inline bool ovl_check_origin_xattr(struct ovl_fs *ofs, 474dad7017aSChristian Brauner struct dentry *upperdentry) 475dad7017aSChristian Brauner { 476dad7017aSChristian Brauner struct path upperpath = { 477dad7017aSChristian Brauner .dentry = upperdentry, 478dad7017aSChristian Brauner .mnt = ovl_upper_mnt(ofs), 479dad7017aSChristian Brauner }; 480dad7017aSChristian Brauner return ovl_path_check_origin_xattr(ofs, &upperpath); 481dad7017aSChristian Brauner } 482dad7017aSChristian Brauner 483a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry, 48443d193f8SMiklos Szeredi enum ovl_xattr ox, const void *value, size_t size, 485f3a15685SAmir Goldstein int xerr); 486f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry); 487ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry); 488ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry); 489146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry); 49024b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry); 4910e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry); 4920e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry); 4935820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir); 494bf070890SAlexander Larsson int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path, 495bf070890SAlexander Larsson struct ovl_metacopy *data); 496184996e9SAlexander Larsson int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, 497184996e9SAlexander Larsson struct ovl_metacopy *metacopy); 49867d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry); 4992d343087SAl Viro char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding); 5000c71faf5SAlexander Larsson int ovl_ensure_verity_loaded(struct path *path); 501184996e9SAlexander Larsson int ovl_get_verity_xattr(struct ovl_fs *ofs, const struct path *path, 502184996e9SAlexander Larsson u8 *digest_buf, int *buf_length); 503184996e9SAlexander Larsson int ovl_validate_verity(struct ovl_fs *ofs, 504184996e9SAlexander Larsson struct path *metapath, 505184996e9SAlexander Larsson struct path *datapath); 5060c71faf5SAlexander Larsson int ovl_get_verity_digest(struct ovl_fs *ofs, struct path *src, 5070c71faf5SAlexander Larsson struct ovl_metacopy *metacopy); 508335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs); 509f3a15685SAmir Goldstein 51065cd913eSAmir Goldstein static inline void ovl_set_flag(unsigned long flag, struct inode *inode) 51165cd913eSAmir Goldstein { 51265cd913eSAmir Goldstein set_bit(flag, &OVL_I(inode)->flags); 51365cd913eSAmir Goldstein } 51465cd913eSAmir Goldstein 51565cd913eSAmir Goldstein static inline void ovl_clear_flag(unsigned long flag, struct inode *inode) 51665cd913eSAmir Goldstein { 51765cd913eSAmir Goldstein clear_bit(flag, &OVL_I(inode)->flags); 51865cd913eSAmir Goldstein } 51965cd913eSAmir Goldstein 52065cd913eSAmir Goldstein static inline bool ovl_test_flag(unsigned long flag, struct inode *inode) 52165cd913eSAmir Goldstein { 52265cd913eSAmir Goldstein return test_bit(flag, &OVL_I(inode)->flags); 52365cd913eSAmir Goldstein } 52465cd913eSAmir Goldstein 525610afc0bSMiklos Szeredi static inline bool ovl_is_impuredir(struct super_block *sb, 526dad7017aSChristian Brauner struct dentry *upperdentry) 527f3a15685SAmir Goldstein { 528dad7017aSChristian Brauner struct ovl_fs *ofs = OVL_FS(sb); 529dad7017aSChristian Brauner struct path upperpath = { 530dad7017aSChristian Brauner .dentry = upperdentry, 531dad7017aSChristian Brauner .mnt = ovl_upper_mnt(ofs), 532dad7017aSChristian Brauner }; 533dad7017aSChristian Brauner 534dad7017aSChristian Brauner return ovl_path_check_dir_xattr(ofs, &upperpath, OVL_XATTR_IMPURE); 535f3a15685SAmir Goldstein } 536f3a15685SAmir Goldstein 537af5f2396SAmir Goldstein static inline bool ovl_redirect_follow(struct ovl_fs *ofs) 538af5f2396SAmir Goldstein { 539af5f2396SAmir Goldstein return ofs->config.redirect_mode != OVL_REDIRECT_NOFOLLOW; 540af5f2396SAmir Goldstein } 541af5f2396SAmir Goldstein 542af5f2396SAmir Goldstein static inline bool ovl_redirect_dir(struct ovl_fs *ofs) 543af5f2396SAmir Goldstein { 544af5f2396SAmir Goldstein return ofs->config.redirect_mode == OVL_REDIRECT_ON; 545af5f2396SAmir Goldstein } 546af5f2396SAmir Goldstein 547b0504bfeSAmir Goldstein static inline bool ovl_origin_uuid(struct ovl_fs *ofs) 548b0504bfeSAmir Goldstein { 549b0504bfeSAmir Goldstein return ofs->config.uuid != OVL_UUID_OFF; 550b0504bfeSAmir Goldstein } 551b0504bfeSAmir Goldstein 552b0504bfeSAmir Goldstein static inline bool ovl_has_fsid(struct ovl_fs *ofs) 553b0504bfeSAmir Goldstein { 554*cbb44f09SAmir Goldstein return ofs->config.uuid == OVL_UUID_ON || 555*cbb44f09SAmir Goldstein ofs->config.uuid == OVL_UUID_AUTO; 556b0504bfeSAmir Goldstein } 557b0504bfeSAmir Goldstein 558926e94d7SAmir Goldstein /* 559926e94d7SAmir Goldstein * With xino=auto, we do best effort to keep all inodes on same st_dev and 560926e94d7SAmir Goldstein * d_ino consistent with st_ino. 561926e94d7SAmir Goldstein * With xino=on, we do the same effort but we warn if we failed. 562926e94d7SAmir Goldstein */ 563dcb399deSAmir Goldstein static inline bool ovl_xino_warn(struct ovl_fs *ofs) 564926e94d7SAmir Goldstein { 565dcb399deSAmir Goldstein return ofs->config.xino == OVL_XINO_ON; 566926e94d7SAmir Goldstein } 567926e94d7SAmir Goldstein 568af5f2396SAmir Goldstein /* 569af5f2396SAmir Goldstein * To avoid regressions in existing setups with overlay lower offline changes, 570af5f2396SAmir Goldstein * we allow lower changes only if none of the new features are used. 571af5f2396SAmir Goldstein */ 572af5f2396SAmir Goldstein static inline bool ovl_allow_offline_changes(struct ovl_fs *ofs) 573af5f2396SAmir Goldstein { 574af5f2396SAmir Goldstein return (!ofs->config.index && !ofs->config.metacopy && 575af5f2396SAmir Goldstein !ovl_redirect_dir(ofs) && !ovl_xino_warn(ofs)); 5760f831ec8SAmir Goldstein } 577e487d889SAmir Goldstein 578e487d889SAmir Goldstein /* All layers on same fs? */ 579dcb399deSAmir Goldstein static inline bool ovl_same_fs(struct ovl_fs *ofs) 580bbb1e54dSMiklos Szeredi { 581dcb399deSAmir Goldstein return ofs->xino_mode == 0; 5828a22efa1SAmir Goldstein } 5838a22efa1SAmir Goldstein 5848a22efa1SAmir Goldstein /* All overlay inodes have same st_dev? */ 585dcb399deSAmir Goldstein static inline bool ovl_same_dev(struct ovl_fs *ofs) 5861e92e307SAmir Goldstein { 587dcb399deSAmir Goldstein return ofs->xino_mode >= 0; 5881e92e307SAmir Goldstein } 5891e92e307SAmir Goldstein 590dcb399deSAmir Goldstein static inline unsigned int ovl_xino_bits(struct ovl_fs *ofs) 5911e92e307SAmir Goldstein { 592dcb399deSAmir Goldstein return ovl_same_dev(ofs) ? ofs->xino_mode : 0; 593f941866fSAmir Goldstein } 59405122443SAmir Goldstein 595531d3040SAmir Goldstein static inline void ovl_inode_lock(struct inode *inode) 596531d3040SAmir Goldstein { 597531d3040SAmir Goldstein mutex_lock(&OVL_I(inode)->lock); 598531d3040SAmir Goldstein } 599531d3040SAmir Goldstein 600531d3040SAmir Goldstein static inline int ovl_inode_lock_interruptible(struct inode *inode) 6011e92e307SAmir Goldstein { 6021e92e307SAmir Goldstein return mutex_lock_interruptible(&OVL_I(inode)->lock); 6031e92e307SAmir Goldstein } 6041e92e307SAmir Goldstein 6051e92e307SAmir Goldstein static inline void ovl_inode_unlock(struct inode *inode) 6061e92e307SAmir Goldstein { 6071e92e307SAmir Goldstein mutex_unlock(&OVL_I(inode)->lock); 6081e92e307SAmir Goldstein } 6091e92e307SAmir Goldstein 610e9be9d5eSMiklos Szeredi 611bbb1e54dSMiklos Szeredi /* namei.c */ 612cbe7fba8SAmir Goldstein int ovl_check_fb_len(struct ovl_fb *fb, int fb_len); 613cbe7fba8SAmir Goldstein 614cbe7fba8SAmir Goldstein static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len) 615cbe7fba8SAmir Goldstein { 616522f6e6cSAmir Goldstein if (fh_len < sizeof(struct ovl_fh)) 617522f6e6cSAmir Goldstein return -EINVAL; 618522f6e6cSAmir Goldstein 619cbe7fba8SAmir Goldstein return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET); 620cbe7fba8SAmir Goldstein } 621cbe7fba8SAmir Goldstein 6221cdb0cb6SPavel Tikhomirov struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh, 6231cdb0cb6SPavel Tikhomirov struct vfsmount *mnt, bool connected); 62405122443SAmir Goldstein int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected, 6251eff1a1dSAmir Goldstein struct dentry *upperdentry, struct ovl_path **stackp); 626610afc0bSMiklos Szeredi int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry, 62743d193f8SMiklos Szeredi enum ovl_xattr ox, struct dentry *real, bool is_upper, 628610afc0bSMiklos Szeredi bool set); 6298ea28765SAmir Goldstein struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index, 6308ea28765SAmir Goldstein bool connected); 631415543d5SAmir Goldstein int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index); 6321cdb0cb6SPavel Tikhomirov int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin, 6331cdb0cb6SPavel Tikhomirov struct qstr *name); 63491ffe7beSAmir Goldstein struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh); 63506170154SAmir Goldstein struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper, 63606170154SAmir Goldstein struct dentry *origin, bool verify); 637bbb1e54dSMiklos Szeredi int ovl_path_next(int idx, struct dentry *dentry, struct path *path); 638184996e9SAlexander Larsson int ovl_verify_lowerdata(struct dentry *dentry); 6391eff1a1dSAmir Goldstein struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 6401eff1a1dSAmir Goldstein unsigned int flags); 641bbb1e54dSMiklos Szeredi bool ovl_lower_positive(struct dentry *dentry); 642e9be9d5eSMiklos Szeredi 643610afc0bSMiklos Szeredi static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper, 64405122443SAmir Goldstein struct dentry *origin, bool set) 64505122443SAmir Goldstein { 646610afc0bSMiklos Szeredi return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin, 647610afc0bSMiklos Szeredi false, set); 64805122443SAmir Goldstein } 64905122443SAmir Goldstein 650610afc0bSMiklos Szeredi static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index, 651ad1d615cSAmir Goldstein struct dentry *upper, bool set) 652ad1d615cSAmir Goldstein { 653610afc0bSMiklos Szeredi return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set); 654ad1d615cSAmir Goldstein } 655ad1d615cSAmir Goldstein 656e9be9d5eSMiklos Szeredi /* readdir.c */ 657e9be9d5eSMiklos Szeredi extern const struct file_operations ovl_dir_operations; 65861536bedSAmir Goldstein struct file *ovl_dir_real_file(const struct file *file, bool want_upper); 659e9be9d5eSMiklos Szeredi int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list); 660576bb263SChristian Brauner void ovl_cleanup_whiteouts(struct ovl_fs *ofs, struct dentry *upper, 661576bb263SChristian Brauner struct list_head *list); 662e9be9d5eSMiklos Szeredi void ovl_cache_free(struct list_head *list); 6634edb83bbSMiklos Szeredi void ovl_dir_cache_free(struct inode *inode); 6642d343087SAl Viro int ovl_check_d_type_supported(const struct path *realpath); 665576bb263SChristian Brauner int ovl_workdir_cleanup(struct ovl_fs *ofs, struct inode *dir, 666576bb263SChristian Brauner struct vfsmount *mnt, struct dentry *dentry, int level); 6671eff1a1dSAmir Goldstein int ovl_indexdir_cleanup(struct ovl_fs *ofs); 668e9be9d5eSMiklos Szeredi 66965cd913eSAmir Goldstein /* 67065cd913eSAmir Goldstein * Can we iterate real dir directly? 67165cd913eSAmir Goldstein * 67265cd913eSAmir Goldstein * Non-merge dir may contain whiteouts from a time it was a merge upper, before 67365cd913eSAmir Goldstein * lower dir was removed under it and possibly before it was rotated from upper 67465cd913eSAmir Goldstein * to lower layer. 67565cd913eSAmir Goldstein */ 6761fa9c5c5SMiklos Szeredi static inline bool ovl_dir_is_real(struct inode *dir) 67765cd913eSAmir Goldstein { 6781fa9c5c5SMiklos Szeredi return !ovl_test_flag(OVL_WHITEOUTS, dir); 67965cd913eSAmir Goldstein } 68065cd913eSAmir Goldstein 681e9be9d5eSMiklos Szeredi /* inode.c */ 6825f8415d6SAmir Goldstein int ovl_set_nlink_upper(struct dentry *dentry); 6835f8415d6SAmir Goldstein int ovl_set_nlink_lower(struct dentry *dentry); 684610afc0bSMiklos Szeredi unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry, 685caf70cb2SAmir Goldstein struct dentry *upperdentry, 686caf70cb2SAmir Goldstein unsigned int fallback); 687c1632a0fSChristian Brauner int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 688549c7297SChristian Brauner struct iattr *attr); 689b74d24f7SChristian Brauner int ovl_getattr(struct mnt_idmap *idmap, const struct path *path, 690549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int flags); 6914609e1f1SChristian Brauner int ovl_permission(struct mnt_idmap *idmap, struct inode *inode, 692549c7297SChristian Brauner int mask); 6931d88f183SMiklos Szeredi int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, 6941d88f183SMiklos Szeredi const void *value, size_t size, int flags); 6951d88f183SMiklos Szeredi int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, 6960eb45fc3SAndreas Gruenbacher void *value, size_t size); 697e9be9d5eSMiklos Szeredi ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size); 698ded53656SYang Xu 699ded53656SYang Xu #ifdef CONFIG_FS_POSIX_ACL 70077435322SChristian Brauner struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap, 7016c0a8bfbSChristian Brauner struct inode *inode, int type, 7026c0a8bfbSChristian Brauner bool rcu, bool noperm); 7036c0a8bfbSChristian Brauner static inline struct posix_acl *ovl_get_inode_acl(struct inode *inode, int type, 7046c0a8bfbSChristian Brauner bool rcu) 7056c0a8bfbSChristian Brauner { 70677435322SChristian Brauner return do_ovl_get_acl(&nop_mnt_idmap, inode, type, rcu, true); 7076c0a8bfbSChristian Brauner } 70877435322SChristian Brauner static inline struct posix_acl *ovl_get_acl(struct mnt_idmap *idmap, 7096c0a8bfbSChristian Brauner struct dentry *dentry, int type) 7106c0a8bfbSChristian Brauner { 71177435322SChristian Brauner return do_ovl_get_acl(idmap, d_inode(dentry), type, false, false); 7126c0a8bfbSChristian Brauner } 71313e83a49SChristian Brauner int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 7140e641857SChristian Brauner struct posix_acl *acl, int type); 71531acceb9SChristian Brauner struct posix_acl *ovl_get_acl_path(const struct path *path, 71631acceb9SChristian Brauner const char *acl_name, bool noperm); 717ded53656SYang Xu #else 7186c0a8bfbSChristian Brauner #define ovl_get_inode_acl NULL 719ded53656SYang Xu #define ovl_get_acl NULL 7200e641857SChristian Brauner #define ovl_set_acl NULL 72131acceb9SChristian Brauner static inline struct posix_acl *ovl_get_acl_path(const struct path *path, 72231acceb9SChristian Brauner const char *acl_name, 72331acceb9SChristian Brauner bool noperm) 72431acceb9SChristian Brauner { 72531acceb9SChristian Brauner return NULL; 72631acceb9SChristian Brauner } 727ded53656SYang Xu #endif 728ded53656SYang Xu 72995582b00SDeepa Dinamani int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags); 730610afc0bSMiklos Szeredi bool ovl_is_private_xattr(struct super_block *sb, const char *name); 731e9be9d5eSMiklos Szeredi 732ac6a52ebSVivek Goyal struct ovl_inode_params { 73301b39dccSAmir Goldstein struct inode *newinode; 734ac6a52ebSVivek Goyal struct dentry *upperdentry; 7350af950f5SAmir Goldstein struct ovl_entry *oe; 73674c6e384SMiklos Szeredi bool index; 7379cec54c8SVivek Goyal char *redirect; 7382b21da92SAmir Goldstein char *lowerdata_redirect; 739ac6a52ebSVivek Goyal }; 74062c832edSAmir Goldstein void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, 74162c832edSAmir Goldstein unsigned long ino, int fsid); 742ca4c8a3aSMiklos Szeredi struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev); 7434b91c30aSAmir Goldstein struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, 7444b91c30aSAmir Goldstein bool is_upper); 745146d62e5SAmir Goldstein bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir); 746146d62e5SAmir Goldstein struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir); 747ac6a52ebSVivek Goyal struct inode *ovl_get_inode(struct super_block *sb, 748ac6a52ebSVivek Goyal struct ovl_inode_params *oip); 7492878dffcSChristian Brauner void ovl_copyattr(struct inode *to); 750e9be9d5eSMiklos Szeredi 75172db8211SAmir Goldstein /* vfs inode flags copied from real to ovl inode */ 75272db8211SAmir Goldstein #define OVL_COPY_I_FLAGS_MASK (S_SYNC | S_NOATIME | S_APPEND | S_IMMUTABLE) 753096a218aSAmir Goldstein /* vfs inode flags read from overlay.protattr xattr to ovl inode */ 754096a218aSAmir Goldstein #define OVL_PROT_I_FLAGS_MASK (S_APPEND | S_IMMUTABLE) 75572db8211SAmir Goldstein 75672db8211SAmir Goldstein /* 75772db8211SAmir Goldstein * fileattr flags copied from lower to upper inode on copy up. 758096a218aSAmir Goldstein * We cannot copy up immutable/append-only flags, because that would prevent 759096a218aSAmir Goldstein * linking temp inode to upper dir, so we store them in xattr instead. 76072db8211SAmir Goldstein */ 76172db8211SAmir Goldstein #define OVL_COPY_FS_FLAGS_MASK (FS_SYNC_FL | FS_NOATIME_FL) 76272db8211SAmir Goldstein #define OVL_COPY_FSX_FLAGS_MASK (FS_XFLAG_SYNC | FS_XFLAG_NOATIME) 763096a218aSAmir Goldstein #define OVL_PROT_FS_FLAGS_MASK (FS_APPEND_FL | FS_IMMUTABLE_FL) 764096a218aSAmir Goldstein #define OVL_PROT_FSX_FLAGS_MASK (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE) 765096a218aSAmir Goldstein 766096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper); 767096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper, 768096a218aSAmir Goldstein struct fileattr *fa); 76972db8211SAmir Goldstein 7704f357295SMiklos Szeredi static inline void ovl_copyflags(struct inode *from, struct inode *to) 7714f357295SMiklos Szeredi { 77272db8211SAmir Goldstein unsigned int mask = OVL_COPY_I_FLAGS_MASK; 7734f357295SMiklos Szeredi 7744f357295SMiklos Szeredi inode_set_flags(to, from->i_flags & mask, mask); 7754f357295SMiklos Szeredi } 7764f357295SMiklos Szeredi 777e9be9d5eSMiklos Szeredi /* dir.c */ 778e9be9d5eSMiklos Szeredi extern const struct inode_operations ovl_dir_inode_operations; 779c21c839bSChengguang Xu int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir, 780e7dd0e71SAmir Goldstein struct dentry *dentry); 781471ec5dcSAmir Goldstein struct ovl_cattr { 78232a3d848SAl Viro dev_t rdev; 78332a3d848SAl Viro umode_t mode; 78432a3d848SAl Viro const char *link; 785471ec5dcSAmir Goldstein struct dentry *hardlink; 78632a3d848SAl Viro }; 787471ec5dcSAmir Goldstein 788471ec5dcSAmir Goldstein #define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) }) 789471ec5dcSAmir Goldstein 790576bb263SChristian Brauner int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir, 791576bb263SChristian Brauner struct dentry **newdentry, umode_t mode); 792576bb263SChristian Brauner struct dentry *ovl_create_real(struct ovl_fs *ofs, 793576bb263SChristian Brauner struct inode *dir, struct dentry *newdentry, 794471ec5dcSAmir Goldstein struct ovl_cattr *attr); 795576bb263SChristian Brauner int ovl_cleanup(struct ovl_fs *ofs, struct inode *dir, struct dentry *dentry); 796576bb263SChristian Brauner struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir); 797576bb263SChristian Brauner struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir, 798576bb263SChristian Brauner struct ovl_cattr *attr); 799e9be9d5eSMiklos Szeredi 800d1d04ef8SMiklos Szeredi /* file.c */ 801d1d04ef8SMiklos Szeredi extern const struct file_operations ovl_file_operations; 8022406a307SJiufei Xue int __init ovl_aio_request_cache_init(void); 8032406a307SJiufei Xue void ovl_aio_request_cache_destroy(void); 8042d343087SAl Viro int ovl_real_fileattr_get(const struct path *realpath, struct fileattr *fa); 8052d343087SAl Viro int ovl_real_fileattr_set(const struct path *realpath, struct fileattr *fa); 80666dbfabfSMiklos Szeredi int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa); 8078782a9aeSChristian Brauner int ovl_fileattr_set(struct mnt_idmap *idmap, 80866dbfabfSMiklos Szeredi struct dentry *dentry, struct fileattr *fa); 809d1d04ef8SMiklos Szeredi 810e9be9d5eSMiklos Szeredi /* copy_up.c */ 811e9be9d5eSMiklos Szeredi int ovl_copy_up(struct dentry *dentry); 812d1e6f6a9SVivek Goyal int ovl_copy_up_with_data(struct dentry *dentry); 8133428030dSAmir Goldstein int ovl_maybe_copy_up(struct dentry *dentry, int flags); 8142d343087SAl Viro int ovl_copy_xattr(struct super_block *sb, const struct path *path, struct dentry *new); 8155272eaf3SChristian Brauner int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upper, struct kstat *stat); 8161cdb0cb6SPavel Tikhomirov struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real, 8171cdb0cb6SPavel Tikhomirov bool is_upper); 818a0c236b1SAmir Goldstein int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower, 819a0c236b1SAmir Goldstein struct dentry *upper); 8208ed5eec9SAmir Goldstein 8218ed5eec9SAmir Goldstein /* export.c */ 8228ed5eec9SAmir Goldstein extern const struct export_operations ovl_export_operations; 82316aac5adSAmir Goldstein extern const struct export_operations ovl_export_fid_operations; 8247fb7998bSChristian Brauner 8257fb7998bSChristian Brauner /* super.c */ 8267fb7998bSChristian Brauner int ovl_fill_super(struct super_block *sb, struct fs_context *fc); 8277fb7998bSChristian Brauner 8287fb7998bSChristian Brauner /* Will this overlay be forced to mount/remount ro? */ 8297fb7998bSChristian Brauner static inline bool ovl_force_readonly(struct ovl_fs *ofs) 8307fb7998bSChristian Brauner { 8317fb7998bSChristian Brauner return (!ovl_upper_mnt(ofs) || !ofs->workdir); 8327fb7998bSChristian Brauner } 833