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 *lowerdir; 10bbb1e54dSMiklos Szeredi char *upperdir; 11bbb1e54dSMiklos Szeredi char *workdir; 12bbb1e54dSMiklos Szeredi bool default_permissions; 13a6c60655SMiklos Szeredi bool redirect_dir; 14438c84c2SMiklos Szeredi bool redirect_follow; 15438c84c2SMiklos Szeredi const char *redirect_mode; 1602bcd157SAmir Goldstein bool index; 175830fb6bSPavel Tikhomirov bool uuid; 18f168f109SAmir Goldstein bool nfs_export; 19795939a9SAmir Goldstein int xino; 20d5791044SVivek Goyal bool metacopy; 212d2f2d73SMiklos Szeredi bool userxattr; 22c86243b0SVivek Goyal bool ovl_volatile; 23bbb1e54dSMiklos Szeredi }; 24bbb1e54dSMiklos Szeredi 255148626bSAmir Goldstein struct ovl_sb { 265148626bSAmir Goldstein struct super_block *sb; 275148626bSAmir Goldstein dev_t pseudo_dev; 287e63c87fSAmir Goldstein /* Unusable (conflicting) uuid */ 297e63c87fSAmir Goldstein bool bad_uuid; 301b81ddddSAmir Goldstein /* Used as a lower layer (but maybe also as upper) */ 311b81ddddSAmir Goldstein bool is_lower; 325148626bSAmir Goldstein }; 335148626bSAmir Goldstein 34b9343632SChandan Rajendra struct ovl_layer { 35*f723edb8SChristian Brauner /* ovl_free_fs() relies on @mnt being the first member! */ 36b9343632SChandan Rajendra struct vfsmount *mnt; 37146d62e5SAmir Goldstein /* Trap in ovl inode cache */ 38146d62e5SAmir Goldstein struct inode *trap; 395148626bSAmir Goldstein struct ovl_sb *fs; 405148626bSAmir Goldstein /* Index of this layer in fs root (upper idx == 0) */ 41d583ed7dSAmir Goldstein int idx; 425148626bSAmir Goldstein /* One fsid per unique underlying sb (upper fsid == 0) */ 435148626bSAmir Goldstein int fsid; 44b9343632SChandan Rajendra }; 45b9343632SChandan Rajendra 46*f723edb8SChristian Brauner /* 47*f723edb8SChristian Brauner * ovl_free_fs() relies on @mnt being the first member when unmounting 48*f723edb8SChristian Brauner * the private mounts created for each layer. Let's check both the 49*f723edb8SChristian Brauner * offset and type. 50*f723edb8SChristian Brauner */ 51*f723edb8SChristian Brauner static_assert(offsetof(struct ovl_layer, mnt) == 0); 52*f723edb8SChristian Brauner static_assert(__same_type(typeof_member(struct ovl_layer, mnt), struct vfsmount *)); 53*f723edb8SChristian Brauner 54b9343632SChandan Rajendra struct ovl_path { 5513464165SMiklos Szeredi const struct ovl_layer *layer; 56b9343632SChandan Rajendra struct dentry *dentry; 57b9343632SChandan Rajendra }; 58b9343632SChandan Rajendra 590af950f5SAmir Goldstein struct ovl_entry { 600af950f5SAmir Goldstein unsigned int __numlower; 610af950f5SAmir Goldstein struct ovl_path __lowerstack[]; 620af950f5SAmir Goldstein }; 630af950f5SAmir Goldstein 64bbb1e54dSMiklos Szeredi /* private information held for overlayfs's superblock */ 65bbb1e54dSMiklos Szeredi struct ovl_fs { 6694375f9dSAmir Goldstein unsigned int numlayer; 6707f1e596SAmir Goldstein /* Number of unique fs among layers including upper fs */ 6807f1e596SAmir Goldstein unsigned int numfs; 6937ebf056SAmir Goldstein /* Number of data-only lower layers */ 7037ebf056SAmir Goldstein unsigned int numdatalayer; 7113464165SMiklos Szeredi const struct ovl_layer *layers; 7207f1e596SAmir Goldstein struct ovl_sb *fs; 732cac0c00SAmir Goldstein /* workbasedir is the path at workdir= mount option */ 742cac0c00SAmir Goldstein struct dentry *workbasedir; 752cac0c00SAmir Goldstein /* workdir is the 'work' directory under workbasedir */ 76bbb1e54dSMiklos Szeredi struct dentry *workdir; 7702bcd157SAmir Goldstein /* index directory listing overlay inodes by origin file handle */ 7802bcd157SAmir Goldstein struct dentry *indexdir; 796b2d5fe4SMiklos Szeredi long namelen; 80bbb1e54dSMiklos Szeredi /* pathnames of lower and upper dirs, for show_options */ 81bbb1e54dSMiklos Szeredi struct ovl_config config; 82bbb1e54dSMiklos Szeredi /* creds of process who forced instantiation of super block */ 83bbb1e54dSMiklos Szeredi const struct cred *creator_cred; 84e7f52429SAmir Goldstein bool tmpfile; 8582b749b2SAmir Goldstein bool noxattr; 8685fdee1eSAmir Goldstein /* Did we take the inuse lock? */ 8785fdee1eSAmir Goldstein bool upperdir_locked; 8885fdee1eSAmir Goldstein bool workdir_locked; 89c21c839bSChengguang Xu bool share_whiteout; 90146d62e5SAmir Goldstein /* Traps in ovl inode cache */ 910be0bfd2SAmir Goldstein struct inode *workbasedir_trap; 92146d62e5SAmir Goldstein struct inode *workdir_trap; 93146d62e5SAmir Goldstein struct inode *indexdir_trap; 940f831ec8SAmir Goldstein /* -1: disabled, 0: same fs, 1..32: number of unused ino bits */ 950f831ec8SAmir Goldstein int xino_mode; 964d314f78SAmir Goldstein /* For allocation of non-persistent inode numbers */ 974d314f78SAmir Goldstein atomic_long_t last_ino; 98c21c839bSChengguang Xu /* Whiteout dentry cache */ 99c21c839bSChengguang Xu struct dentry *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 10537ebf056SAmir Goldstein /* Number of lower layers, not including data-only layers */ 10637ebf056SAmir Goldstein static inline unsigned int ovl_numlowerlayer(struct ovl_fs *ofs) 10737ebf056SAmir Goldstein { 10837ebf056SAmir Goldstein return ofs->numlayer - ofs->numdatalayer - 1; 10937ebf056SAmir Goldstein } 11037ebf056SAmir Goldstein 11108f4c7c8SMiklos Szeredi static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs) 11208f4c7c8SMiklos Szeredi { 113b8e42a65SMiklos Szeredi return ofs->layers[0].mnt; 11408f4c7c8SMiklos Szeredi } 11508f4c7c8SMiklos Szeredi 116abf08576SChristian Brauner static inline struct mnt_idmap *ovl_upper_mnt_idmap(struct ovl_fs *ofs) 117abf08576SChristian Brauner { 118abf08576SChristian Brauner return mnt_idmap(ovl_upper_mnt(ofs)); 119abf08576SChristian Brauner } 120abf08576SChristian Brauner 1210f831ec8SAmir Goldstein static inline struct ovl_fs *OVL_FS(struct super_block *sb) 1220f831ec8SAmir Goldstein { 1230f831ec8SAmir Goldstein return (struct ovl_fs *)sb->s_fs_info; 1240f831ec8SAmir Goldstein } 1250f831ec8SAmir Goldstein 126c86243b0SVivek Goyal static inline bool ovl_should_sync(struct ovl_fs *ofs) 127c86243b0SVivek Goyal { 128c86243b0SVivek Goyal return !ofs->config.ovl_volatile; 129c86243b0SVivek Goyal } 130c86243b0SVivek Goyal 1315522c9c7SAmir Goldstein static inline unsigned int ovl_numlower(struct ovl_entry *oe) 1325522c9c7SAmir Goldstein { 1335522c9c7SAmir Goldstein return oe ? oe->__numlower : 0; 1345522c9c7SAmir Goldstein } 1355522c9c7SAmir Goldstein 1365522c9c7SAmir Goldstein static inline struct ovl_path *ovl_lowerstack(struct ovl_entry *oe) 1375522c9c7SAmir Goldstein { 1385522c9c7SAmir Goldstein return ovl_numlower(oe) ? oe->__lowerstack : NULL; 1395522c9c7SAmir Goldstein } 1405522c9c7SAmir Goldstein 141ac900ed4SAmir Goldstein static inline struct ovl_path *ovl_lowerpath(struct ovl_entry *oe) 142ac900ed4SAmir Goldstein { 143ac900ed4SAmir Goldstein return ovl_lowerstack(oe); 144ac900ed4SAmir Goldstein } 145ac900ed4SAmir Goldstein 146ab1eb5ffSAmir Goldstein static inline struct ovl_path *ovl_lowerdata(struct ovl_entry *oe) 147ab1eb5ffSAmir Goldstein { 148ab1eb5ffSAmir Goldstein struct ovl_path *lowerstack = ovl_lowerstack(oe); 149ab1eb5ffSAmir Goldstein 150ab1eb5ffSAmir Goldstein return lowerstack ? &lowerstack[oe->__numlower - 1] : NULL; 151ab1eb5ffSAmir Goldstein } 152ab1eb5ffSAmir Goldstein 1532b21da92SAmir Goldstein /* May return NULL if lazy lookup of lowerdata is needed */ 154ab1eb5ffSAmir Goldstein static inline struct dentry *ovl_lowerdata_dentry(struct ovl_entry *oe) 155ab1eb5ffSAmir Goldstein { 156ab1eb5ffSAmir Goldstein struct ovl_path *lowerdata = ovl_lowerdata(oe); 157ab1eb5ffSAmir Goldstein 15842dd69aeSAmir Goldstein return lowerdata ? READ_ONCE(lowerdata->dentry) : NULL; 159ab1eb5ffSAmir Goldstein } 160ab1eb5ffSAmir Goldstein 1610af950f5SAmir Goldstein /* private information held for every overlayfs dentry */ 162a6ff2bc0SAmir Goldstein static inline unsigned long *OVL_E_FLAGS(struct dentry *dentry) 163a6ff2bc0SAmir Goldstein { 1640af950f5SAmir Goldstein return (unsigned long *) &dentry->d_fsdata; 165a6ff2bc0SAmir Goldstein } 166a6ff2bc0SAmir Goldstein 16713cf199dSAmir Goldstein struct ovl_inode { 1682664bd08SVivek Goyal union { 1692664bd08SVivek Goyal struct ovl_dir_cache *cache; /* directory */ 1702b21da92SAmir Goldstein const char *lowerdata_redirect; /* regular file */ 1712664bd08SVivek Goyal }; 172cf31c463SMiklos Szeredi const char *redirect; 17304a01ac7SMiklos Szeredi u64 version; 17413c72075SMiklos Szeredi unsigned long flags; 17513cf199dSAmir Goldstein struct inode vfs_inode; 17609d8b586SMiklos Szeredi struct dentry *__upperdentry; 1770af950f5SAmir Goldstein struct ovl_entry *oe; 178a015dafcSAmir Goldstein 179a015dafcSAmir Goldstein /* synchronize copy up and more */ 180a015dafcSAmir Goldstein struct mutex lock; 18113cf199dSAmir Goldstein }; 18213cf199dSAmir Goldstein 18313cf199dSAmir Goldstein static inline struct ovl_inode *OVL_I(struct inode *inode) 18413cf199dSAmir Goldstein { 18513cf199dSAmir Goldstein return container_of(inode, struct ovl_inode, vfs_inode); 18613cf199dSAmir Goldstein } 18709d8b586SMiklos Szeredi 1880af950f5SAmir Goldstein static inline struct ovl_entry *OVL_I_E(struct inode *inode) 1890af950f5SAmir Goldstein { 1900af950f5SAmir Goldstein return inode ? OVL_I(inode)->oe : NULL; 1910af950f5SAmir Goldstein } 1920af950f5SAmir Goldstein 1930af950f5SAmir Goldstein static inline struct ovl_entry *OVL_E(struct dentry *dentry) 1940af950f5SAmir Goldstein { 1950af950f5SAmir Goldstein return OVL_I_E(d_inode(dentry)); 1960af950f5SAmir Goldstein } 1970af950f5SAmir Goldstein 19809d8b586SMiklos Szeredi static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi) 19909d8b586SMiklos Szeredi { 200506458efSWill Deacon return READ_ONCE(oi->__upperdentry); 20109d8b586SMiklos Szeredi } 202