1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * 4 * Copyright (C) 2011 Novell Inc. 5 * Copyright (C) 2016 Red Hat, Inc. 6 */ 7 8 struct ovl_config { 9 char *upperdir; 10 char *workdir; 11 bool default_permissions; 12 int redirect_mode; 13 int verity_mode; 14 bool index; 15 int uuid; 16 bool nfs_export; 17 int xino; 18 bool metacopy; 19 bool userxattr; 20 bool ovl_volatile; 21 }; 22 23 struct ovl_sb { 24 struct super_block *sb; 25 dev_t pseudo_dev; 26 /* Unusable (conflicting) uuid */ 27 bool bad_uuid; 28 /* Used as a lower layer (but maybe also as upper) */ 29 bool is_lower; 30 }; 31 32 struct ovl_layer { 33 /* ovl_free_fs() relies on @mnt being the first member! */ 34 struct vfsmount *mnt; 35 /* Trap in ovl inode cache */ 36 struct inode *trap; 37 struct ovl_sb *fs; 38 /* Index of this layer in fs root (upper idx == 0) */ 39 int idx; 40 /* One fsid per unique underlying sb (upper fsid == 0) */ 41 int fsid; 42 char *name; 43 }; 44 45 /* 46 * ovl_free_fs() relies on @mnt being the first member when unmounting 47 * the private mounts created for each layer. Let's check both the 48 * offset and type. 49 */ 50 static_assert(offsetof(struct ovl_layer, mnt) == 0); 51 static_assert(__same_type(typeof_member(struct ovl_layer, mnt), struct vfsmount *)); 52 53 struct ovl_path { 54 const struct ovl_layer *layer; 55 struct dentry *dentry; 56 }; 57 58 struct ovl_entry { 59 unsigned int __numlower; 60 struct ovl_path __lowerstack[]; 61 }; 62 63 /* private information held for overlayfs's superblock */ 64 struct ovl_fs { 65 unsigned int numlayer; 66 /* Number of unique fs among layers including upper fs */ 67 unsigned int numfs; 68 /* Number of data-only lower layers */ 69 unsigned int numdatalayer; 70 const struct ovl_layer *layers; 71 struct ovl_sb *fs; 72 /* workbasedir is the path at workdir= mount option */ 73 struct dentry *workbasedir; 74 /* workdir is the 'work' directory under workbasedir */ 75 struct dentry *workdir; 76 /* index directory listing overlay inodes by origin file handle */ 77 struct dentry *indexdir; 78 long namelen; 79 /* pathnames of lower and upper dirs, for show_options */ 80 struct ovl_config config; 81 /* creds of process who forced instantiation of super block */ 82 const struct cred *creator_cred; 83 bool tmpfile; 84 bool noxattr; 85 bool nofh; 86 /* Did we take the inuse lock? */ 87 bool upperdir_locked; 88 bool workdir_locked; 89 /* Traps in ovl inode cache */ 90 struct inode *workbasedir_trap; 91 struct inode *workdir_trap; 92 struct inode *indexdir_trap; 93 /* -1: disabled, 0: same fs, 1..32: number of unused ino bits */ 94 int xino_mode; 95 /* For allocation of non-persistent inode numbers */ 96 atomic_long_t last_ino; 97 /* Shared whiteout cache */ 98 struct dentry *whiteout; 99 bool no_shared_whiteout; 100 /* r/o snapshot of upperdir sb's only taken on volatile mounts */ 101 errseq_t errseq; 102 }; 103 104 /* Number of lower layers, not including data-only layers */ 105 static inline unsigned int ovl_numlowerlayer(struct ovl_fs *ofs) 106 { 107 return ofs->numlayer - ofs->numdatalayer - 1; 108 } 109 110 static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs) 111 { 112 return ofs->layers[0].mnt; 113 } 114 115 static inline struct mnt_idmap *ovl_upper_mnt_idmap(struct ovl_fs *ofs) 116 { 117 return mnt_idmap(ovl_upper_mnt(ofs)); 118 } 119 120 extern struct file_system_type ovl_fs_type; 121 122 static inline struct ovl_fs *OVL_FS(struct super_block *sb) 123 { 124 return (struct ovl_fs *)sb->s_fs_info; 125 } 126 127 static inline bool ovl_should_sync(struct ovl_fs *ofs) 128 { 129 return !ofs->config.ovl_volatile; 130 } 131 132 static inline unsigned int ovl_numlower(struct ovl_entry *oe) 133 { 134 return oe ? oe->__numlower : 0; 135 } 136 137 static inline struct ovl_path *ovl_lowerstack(struct ovl_entry *oe) 138 { 139 return ovl_numlower(oe) ? oe->__lowerstack : NULL; 140 } 141 142 static inline struct ovl_path *ovl_lowerpath(struct ovl_entry *oe) 143 { 144 return ovl_lowerstack(oe); 145 } 146 147 static inline struct ovl_path *ovl_lowerdata(struct ovl_entry *oe) 148 { 149 struct ovl_path *lowerstack = ovl_lowerstack(oe); 150 151 return lowerstack ? &lowerstack[oe->__numlower - 1] : NULL; 152 } 153 154 /* May return NULL if lazy lookup of lowerdata is needed */ 155 static inline struct dentry *ovl_lowerdata_dentry(struct ovl_entry *oe) 156 { 157 struct ovl_path *lowerdata = ovl_lowerdata(oe); 158 159 return lowerdata ? READ_ONCE(lowerdata->dentry) : NULL; 160 } 161 162 /* private information held for every overlayfs dentry */ 163 static inline unsigned long *OVL_E_FLAGS(struct dentry *dentry) 164 { 165 return (unsigned long *) &dentry->d_fsdata; 166 } 167 168 struct ovl_inode { 169 union { 170 struct ovl_dir_cache *cache; /* directory */ 171 const char *lowerdata_redirect; /* regular file */ 172 }; 173 const char *redirect; 174 u64 version; 175 unsigned long flags; 176 struct inode vfs_inode; 177 struct dentry *__upperdentry; 178 struct ovl_entry *oe; 179 180 /* synchronize copy up and more */ 181 struct mutex lock; 182 }; 183 184 static inline struct ovl_inode *OVL_I(struct inode *inode) 185 { 186 return container_of(inode, struct ovl_inode, vfs_inode); 187 } 188 189 static inline struct ovl_entry *OVL_I_E(struct inode *inode) 190 { 191 return inode ? OVL_I(inode)->oe : NULL; 192 } 193 194 static inline struct ovl_entry *OVL_E(struct dentry *dentry) 195 { 196 return OVL_I_E(d_inode(dentry)); 197 } 198 199 static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi) 200 { 201 return READ_ONCE(oi->__upperdentry); 202 } 203