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