]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/overlayfs/util.c
ovl: set the ORIGIN type flag
[karo-tx-linux.git] / fs / overlayfs / util.c
1 /*
2  * Copyright (C) 2011 Novell Inc.
3  * Copyright (C) 2016 Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9
10 #include <linux/fs.h>
11 #include <linux/mount.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
14 #include <linux/xattr.h>
15 #include <linux/sched/signal.h>
16 #include "overlayfs.h"
17 #include "ovl_entry.h"
18
19 int ovl_want_write(struct dentry *dentry)
20 {
21         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
22         return mnt_want_write(ofs->upper_mnt);
23 }
24
25 void ovl_drop_write(struct dentry *dentry)
26 {
27         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
28         mnt_drop_write(ofs->upper_mnt);
29 }
30
31 struct dentry *ovl_workdir(struct dentry *dentry)
32 {
33         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
34         return ofs->workdir;
35 }
36
37 const struct cred *ovl_override_creds(struct super_block *sb)
38 {
39         struct ovl_fs *ofs = sb->s_fs_info;
40
41         return override_creds(ofs->creator_cred);
42 }
43
44 struct super_block *ovl_same_sb(struct super_block *sb)
45 {
46         struct ovl_fs *ofs = sb->s_fs_info;
47
48         return ofs->same_sb;
49 }
50
51 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
52 {
53         size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
54         struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
55
56         if (oe)
57                 oe->numlower = numlower;
58
59         return oe;
60 }
61
62 bool ovl_dentry_remote(struct dentry *dentry)
63 {
64         return dentry->d_flags &
65                 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
66                  DCACHE_OP_REAL);
67 }
68
69 bool ovl_dentry_weird(struct dentry *dentry)
70 {
71         return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
72                                   DCACHE_MANAGE_TRANSIT |
73                                   DCACHE_OP_HASH |
74                                   DCACHE_OP_COMPARE);
75 }
76
77 enum ovl_path_type ovl_path_type(struct dentry *dentry)
78 {
79         struct ovl_entry *oe = dentry->d_fsdata;
80         enum ovl_path_type type = 0;
81
82         if (oe->__upperdentry) {
83                 type = __OVL_PATH_UPPER;
84
85                 /*
86                  * Non-dir dentry can hold lower dentry of its copy up origin.
87                  */
88                 if (oe->numlower) {
89                         type |= __OVL_PATH_ORIGIN;
90                         if (d_is_dir(dentry))
91                                 type |= __OVL_PATH_MERGE;
92                 }
93         } else {
94                 if (oe->numlower > 1)
95                         type |= __OVL_PATH_MERGE;
96         }
97         return type;
98 }
99
100 void ovl_path_upper(struct dentry *dentry, struct path *path)
101 {
102         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
103         struct ovl_entry *oe = dentry->d_fsdata;
104
105         path->mnt = ofs->upper_mnt;
106         path->dentry = ovl_upperdentry_dereference(oe);
107 }
108
109 void ovl_path_lower(struct dentry *dentry, struct path *path)
110 {
111         struct ovl_entry *oe = dentry->d_fsdata;
112
113         *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
114 }
115
116 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
117 {
118         enum ovl_path_type type = ovl_path_type(dentry);
119
120         if (!OVL_TYPE_UPPER(type))
121                 ovl_path_lower(dentry, path);
122         else
123                 ovl_path_upper(dentry, path);
124
125         return type;
126 }
127
128 struct dentry *ovl_dentry_upper(struct dentry *dentry)
129 {
130         struct ovl_entry *oe = dentry->d_fsdata;
131
132         return ovl_upperdentry_dereference(oe);
133 }
134
135 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
136 {
137         return oe->numlower ? oe->lowerstack[0].dentry : NULL;
138 }
139
140 struct dentry *ovl_dentry_lower(struct dentry *dentry)
141 {
142         struct ovl_entry *oe = dentry->d_fsdata;
143
144         return __ovl_dentry_lower(oe);
145 }
146
147 struct dentry *ovl_dentry_real(struct dentry *dentry)
148 {
149         struct ovl_entry *oe = dentry->d_fsdata;
150         struct dentry *realdentry;
151
152         realdentry = ovl_upperdentry_dereference(oe);
153         if (!realdentry)
154                 realdentry = __ovl_dentry_lower(oe);
155
156         return realdentry;
157 }
158
159 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
160 {
161         struct ovl_entry *oe = dentry->d_fsdata;
162
163         return oe->cache;
164 }
165
166 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
167 {
168         struct ovl_entry *oe = dentry->d_fsdata;
169
170         oe->cache = cache;
171 }
172
173 bool ovl_dentry_is_opaque(struct dentry *dentry)
174 {
175         struct ovl_entry *oe = dentry->d_fsdata;
176         return oe->opaque;
177 }
178
179 bool ovl_dentry_is_whiteout(struct dentry *dentry)
180 {
181         return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
182 }
183
184 void ovl_dentry_set_opaque(struct dentry *dentry)
185 {
186         struct ovl_entry *oe = dentry->d_fsdata;
187
188         oe->opaque = true;
189 }
190
191 bool ovl_redirect_dir(struct super_block *sb)
192 {
193         struct ovl_fs *ofs = sb->s_fs_info;
194
195         return ofs->config.redirect_dir;
196 }
197
198 void ovl_clear_redirect_dir(struct super_block *sb)
199 {
200         struct ovl_fs *ofs = sb->s_fs_info;
201
202         ofs->config.redirect_dir = false;
203 }
204
205 const char *ovl_dentry_get_redirect(struct dentry *dentry)
206 {
207         struct ovl_entry *oe = dentry->d_fsdata;
208
209         return oe->redirect;
210 }
211
212 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
213 {
214         struct ovl_entry *oe = dentry->d_fsdata;
215
216         kfree(oe->redirect);
217         oe->redirect = redirect;
218 }
219
220 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
221 {
222         struct ovl_entry *oe = dentry->d_fsdata;
223
224         WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
225         WARN_ON(oe->__upperdentry);
226         /*
227          * Make sure upperdentry is consistent before making it visible to
228          * ovl_upperdentry_dereference().
229          */
230         smp_wmb();
231         oe->__upperdentry = upperdentry;
232 }
233
234 void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper)
235 {
236         WRITE_ONCE(inode->i_private, (unsigned long) realinode |
237                    (is_upper ? OVL_ISUPPER_MASK : 0));
238 }
239
240 void ovl_inode_update(struct inode *inode, struct inode *upperinode)
241 {
242         WARN_ON(!upperinode);
243         WARN_ON(!inode_unhashed(inode));
244         WRITE_ONCE(inode->i_private,
245                    (unsigned long) upperinode | OVL_ISUPPER_MASK);
246         if (!S_ISDIR(upperinode->i_mode))
247                 __insert_inode_hash(inode, (unsigned long) upperinode);
248 }
249
250 void ovl_dentry_version_inc(struct dentry *dentry)
251 {
252         struct ovl_entry *oe = dentry->d_fsdata;
253
254         WARN_ON(!inode_is_locked(dentry->d_inode));
255         oe->version++;
256 }
257
258 u64 ovl_dentry_version_get(struct dentry *dentry)
259 {
260         struct ovl_entry *oe = dentry->d_fsdata;
261
262         WARN_ON(!inode_is_locked(dentry->d_inode));
263         return oe->version;
264 }
265
266 bool ovl_is_whiteout(struct dentry *dentry)
267 {
268         struct inode *inode = dentry->d_inode;
269
270         return inode && IS_WHITEOUT(inode);
271 }
272
273 struct file *ovl_path_open(struct path *path, int flags)
274 {
275         return dentry_open(path, flags | O_NOATIME, current_cred());
276 }
277
278 int ovl_copy_up_start(struct dentry *dentry)
279 {
280         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
281         struct ovl_entry *oe = dentry->d_fsdata;
282         int err;
283
284         spin_lock(&ofs->copyup_wq.lock);
285         err = wait_event_interruptible_locked(ofs->copyup_wq, !oe->copying);
286         if (!err) {
287                 if (oe->__upperdentry)
288                         err = 1; /* Already copied up */
289                 else
290                         oe->copying = true;
291         }
292         spin_unlock(&ofs->copyup_wq.lock);
293
294         return err;
295 }
296
297 void ovl_copy_up_end(struct dentry *dentry)
298 {
299         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
300         struct ovl_entry *oe = dentry->d_fsdata;
301
302         spin_lock(&ofs->copyup_wq.lock);
303         oe->copying = false;
304         wake_up_locked(&ofs->copyup_wq);
305         spin_unlock(&ofs->copyup_wq.lock);
306 }