1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 2bbb1e54dSMiklos Szeredi /* 3bbb1e54dSMiklos Szeredi * 4bbb1e54dSMiklos Szeredi * Copyright (C) 2011 Novell Inc. 5bbb1e54dSMiklos Szeredi * Copyright (C) 2016 Red Hat, Inc. 6bbb1e54dSMiklos Szeredi */ 7bbb1e54dSMiklos Szeredi 8bbb1e54dSMiklos Szeredi struct ovl_config { 9bbb1e54dSMiklos Szeredi char *upperdir; 10bbb1e54dSMiklos Szeredi char *workdir; 11bbb1e54dSMiklos Szeredi bool default_permissions; 12af5f2396SAmir Goldstein int redirect_mode; 13ae8cba40SAlexander Larsson int verity_mode; 1402bcd157SAmir Goldstein bool index; 15b0504bfeSAmir Goldstein int uuid; 16f168f109SAmir Goldstein bool nfs_export; 17795939a9SAmir Goldstein int xino; 18d5791044SVivek Goyal bool metacopy; 192d2f2d73SMiklos Szeredi bool userxattr; 20c86243b0SVivek Goyal bool ovl_volatile; 21bbb1e54dSMiklos Szeredi }; 22bbb1e54dSMiklos Szeredi 235148626bSAmir Goldstein struct ovl_sb { 245148626bSAmir Goldstein struct super_block *sb; 255148626bSAmir Goldstein dev_t pseudo_dev; 267e63c87fSAmir Goldstein /* Unusable (conflicting) uuid */ 277e63c87fSAmir Goldstein bool bad_uuid; 281b81ddddSAmir Goldstein /* Used as a lower layer (but maybe also as upper) */ 291b81ddddSAmir Goldstein bool is_lower; 305148626bSAmir Goldstein }; 315148626bSAmir Goldstein 32b9343632SChandan Rajendra struct ovl_layer { 33f723edb8SChristian Brauner /* ovl_free_fs() relies on @mnt being the first member! */ 34b9343632SChandan Rajendra struct vfsmount *mnt; 35146d62e5SAmir Goldstein /* Trap in ovl inode cache */ 36146d62e5SAmir Goldstein struct inode *trap; 375148626bSAmir Goldstein struct ovl_sb *fs; 385148626bSAmir Goldstein /* Index of this layer in fs root (upper idx == 0) */ 39d583ed7dSAmir Goldstein int idx; 405148626bSAmir Goldstein /* One fsid per unique underlying sb (upper fsid == 0) */ 415148626bSAmir Goldstein int fsid; 42b36a5780SChristian Brauner char *name; 43b9343632SChandan Rajendra }; 44b9343632SChandan Rajendra 45f723edb8SChristian Brauner /* 46f723edb8SChristian Brauner * ovl_free_fs() relies on @mnt being the first member when unmounting 47f723edb8SChristian Brauner * the private mounts created for each layer. Let's check both the 48f723edb8SChristian Brauner * offset and type. 49f723edb8SChristian Brauner */ 50f723edb8SChristian Brauner static_assert(offsetof(struct ovl_layer, mnt) == 0); 51f723edb8SChristian Brauner static_assert(__same_type(typeof_member(struct ovl_layer, mnt), struct vfsmount *)); 52f723edb8SChristian Brauner 53b9343632SChandan Rajendra struct ovl_path { 5413464165SMiklos Szeredi const struct ovl_layer *layer; 55b9343632SChandan Rajendra struct dentry *dentry; 56b9343632SChandan Rajendra }; 57b9343632SChandan Rajendra 580af950f5SAmir Goldstein struct ovl_entry { 590af950f5SAmir Goldstein unsigned int __numlower; 600af950f5SAmir Goldstein struct ovl_path __lowerstack[]; 610af950f5SAmir Goldstein }; 620af950f5SAmir Goldstein 63bbb1e54dSMiklos Szeredi /* private information held for overlayfs's superblock */ 64bbb1e54dSMiklos Szeredi struct ovl_fs { 6594375f9dSAmir Goldstein unsigned int numlayer; 6607f1e596SAmir Goldstein /* Number of unique fs among layers including upper fs */ 6707f1e596SAmir Goldstein unsigned int numfs; 6837ebf056SAmir Goldstein /* Number of data-only lower layers */ 6937ebf056SAmir Goldstein unsigned int numdatalayer; 7013464165SMiklos Szeredi const struct ovl_layer *layers; 7107f1e596SAmir Goldstein struct ovl_sb *fs; 722cac0c00SAmir Goldstein /* workbasedir is the path at workdir= mount option */ 732cac0c00SAmir Goldstein struct dentry *workbasedir; 742cac0c00SAmir Goldstein /* workdir is the 'work' directory under workbasedir */ 75bbb1e54dSMiklos Szeredi struct dentry *workdir; 7602bcd157SAmir Goldstein /* index directory listing overlay inodes by origin file handle */ 7702bcd157SAmir Goldstein struct dentry *indexdir; 786b2d5fe4SMiklos Szeredi long namelen; 79bbb1e54dSMiklos Szeredi /* pathnames of lower and upper dirs, for show_options */ 80bbb1e54dSMiklos Szeredi struct ovl_config config; 81bbb1e54dSMiklos Szeredi /* creds of process who forced instantiation of super block */ 82bbb1e54dSMiklos Szeredi const struct cred *creator_cred; 83e7f52429SAmir Goldstein bool tmpfile; 8482b749b2SAmir Goldstein bool noxattr; 8516aac5adSAmir Goldstein bool nofh; 8685fdee1eSAmir Goldstein /* Did we take the inuse lock? */ 8785fdee1eSAmir Goldstein bool upperdir_locked; 8885fdee1eSAmir Goldstein bool workdir_locked; 89146d62e5SAmir Goldstein /* Traps in ovl inode cache */ 900be0bfd2SAmir Goldstein struct inode *workbasedir_trap; 91146d62e5SAmir Goldstein struct inode *workdir_trap; 92146d62e5SAmir Goldstein struct inode *indexdir_trap; 930f831ec8SAmir Goldstein /* -1: disabled, 0: same fs, 1..32: number of unused ino bits */ 940f831ec8SAmir Goldstein int xino_mode; 954d314f78SAmir Goldstein /* For allocation of non-persistent inode numbers */ 964d314f78SAmir Goldstein atomic_long_t last_ino; 97e4599d4bSAmir Goldstein /* Shared whiteout cache */ 98c21c839bSChengguang Xu struct dentry *whiteout; 99e4599d4bSAmir Goldstein bool no_shared_whiteout; 100335d3fc5SSargun Dhillon /* r/o snapshot of upperdir sb's only taken on volatile mounts */ 101335d3fc5SSargun Dhillon errseq_t errseq; 102bbb1e54dSMiklos Szeredi }; 103bbb1e54dSMiklos Szeredi 10437ebf056SAmir Goldstein /* Number of lower layers, not including data-only layers */ 10537ebf056SAmir Goldstein static inline unsigned int ovl_numlowerlayer(struct ovl_fs *ofs) 10637ebf056SAmir Goldstein { 10737ebf056SAmir Goldstein return ofs->numlayer - ofs->numdatalayer - 1; 10837ebf056SAmir Goldstein } 10937ebf056SAmir Goldstein 11008f4c7c8SMiklos Szeredi static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs) 11108f4c7c8SMiklos Szeredi { 112b8e42a65SMiklos Szeredi return ofs->layers[0].mnt; 11308f4c7c8SMiklos Szeredi } 11408f4c7c8SMiklos Szeredi 115abf08576SChristian Brauner static inline struct mnt_idmap *ovl_upper_mnt_idmap(struct ovl_fs *ofs) 116abf08576SChristian Brauner { 117abf08576SChristian Brauner return mnt_idmap(ovl_upper_mnt(ofs)); 118abf08576SChristian Brauner } 119abf08576SChristian Brauner 120*f01d0889SAndrea Righi extern struct file_system_type ovl_fs_type; 121*f01d0889SAndrea Righi 1220f831ec8SAmir Goldstein static inline struct ovl_fs *OVL_FS(struct super_block *sb) 1230f831ec8SAmir Goldstein { 1240f831ec8SAmir Goldstein return (struct ovl_fs *)sb->s_fs_info; 1250f831ec8SAmir Goldstein } 1260f831ec8SAmir Goldstein 127c86243b0SVivek Goyal static inline bool ovl_should_sync(struct ovl_fs *ofs) 128c86243b0SVivek Goyal { 129c86243b0SVivek Goyal return !ofs->config.ovl_volatile; 130c86243b0SVivek Goyal } 131c86243b0SVivek Goyal 1325522c9c7SAmir Goldstein static inline unsigned int ovl_numlower(struct ovl_entry *oe) 1335522c9c7SAmir Goldstein { 1345522c9c7SAmir Goldstein return oe ? oe->__numlower : 0; 1355522c9c7SAmir Goldstein } 1365522c9c7SAmir Goldstein 1375522c9c7SAmir Goldstein static inline struct ovl_path *ovl_lowerstack(struct ovl_entry *oe) 1385522c9c7SAmir Goldstein { 1395522c9c7SAmir Goldstein return ovl_numlower(oe) ? oe->__lowerstack : NULL; 1405522c9c7SAmir Goldstein } 1415522c9c7SAmir Goldstein 142ac900ed4SAmir Goldstein static inline struct ovl_path *ovl_lowerpath(struct ovl_entry *oe) 143ac900ed4SAmir Goldstein { 144ac900ed4SAmir Goldstein return ovl_lowerstack(oe); 145ac900ed4SAmir Goldstein } 146ac900ed4SAmir Goldstein 147ab1eb5ffSAmir Goldstein static inline struct ovl_path *ovl_lowerdata(struct ovl_entry *oe) 148ab1eb5ffSAmir Goldstein { 149ab1eb5ffSAmir Goldstein struct ovl_path *lowerstack = ovl_lowerstack(oe); 150ab1eb5ffSAmir Goldstein 151ab1eb5ffSAmir Goldstein return lowerstack ? &lowerstack[oe->__numlower - 1] : NULL; 152ab1eb5ffSAmir Goldstein } 153ab1eb5ffSAmir Goldstein 1542b21da92SAmir Goldstein /* May return NULL if lazy lookup of lowerdata is needed */ 155ab1eb5ffSAmir Goldstein static inline struct dentry *ovl_lowerdata_dentry(struct ovl_entry *oe) 156ab1eb5ffSAmir Goldstein { 157ab1eb5ffSAmir Goldstein struct ovl_path *lowerdata = ovl_lowerdata(oe); 158ab1eb5ffSAmir Goldstein 15942dd69aeSAmir Goldstein return lowerdata ? READ_ONCE(lowerdata->dentry) : NULL; 160ab1eb5ffSAmir Goldstein } 161ab1eb5ffSAmir Goldstein 1620af950f5SAmir Goldstein /* private information held for every overlayfs dentry */ 163a6ff2bc0SAmir Goldstein static inline unsigned long *OVL_E_FLAGS(struct dentry *dentry) 164a6ff2bc0SAmir Goldstein { 1650af950f5SAmir Goldstein return (unsigned long *) &dentry->d_fsdata; 166a6ff2bc0SAmir Goldstein } 167a6ff2bc0SAmir Goldstein 16813cf199dSAmir Goldstein struct ovl_inode { 1692664bd08SVivek Goyal union { 1702664bd08SVivek Goyal struct ovl_dir_cache *cache; /* directory */ 1712b21da92SAmir Goldstein const char *lowerdata_redirect; /* regular file */ 1722664bd08SVivek Goyal }; 173cf31c463SMiklos Szeredi const char *redirect; 17404a01ac7SMiklos Szeredi u64 version; 17513c72075SMiklos Szeredi unsigned long flags; 17613cf199dSAmir Goldstein struct inode vfs_inode; 17709d8b586SMiklos Szeredi struct dentry *__upperdentry; 1780af950f5SAmir Goldstein struct ovl_entry *oe; 179a015dafcSAmir Goldstein 180a015dafcSAmir Goldstein /* synchronize copy up and more */ 181a015dafcSAmir Goldstein struct mutex lock; 18213cf199dSAmir Goldstein }; 18313cf199dSAmir Goldstein 18413cf199dSAmir Goldstein static inline struct ovl_inode *OVL_I(struct inode *inode) 18513cf199dSAmir Goldstein { 18613cf199dSAmir Goldstein return container_of(inode, struct ovl_inode, vfs_inode); 18713cf199dSAmir Goldstein } 18809d8b586SMiklos Szeredi 1890af950f5SAmir Goldstein static inline struct ovl_entry *OVL_I_E(struct inode *inode) 1900af950f5SAmir Goldstein { 1910af950f5SAmir Goldstein return inode ? OVL_I(inode)->oe : NULL; 1920af950f5SAmir Goldstein } 1930af950f5SAmir Goldstein 1940af950f5SAmir Goldstein static inline struct ovl_entry *OVL_E(struct dentry *dentry) 1950af950f5SAmir Goldstein { 1960af950f5SAmir Goldstein return OVL_I_E(d_inode(dentry)); 1970af950f5SAmir Goldstein } 1980af950f5SAmir Goldstein 19909d8b586SMiklos Szeredi static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi) 20009d8b586SMiklos Szeredi { 201506458efSWill Deacon return READ_ONCE(oi->__upperdentry); 20209d8b586SMiklos Szeredi } 203