blob: fc7b1447435bb86ea29c419f8f7c4c136013f38b [file] [log] [blame]
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001/*
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>
Ingo Molnar5b825c32017-02-02 17:54:15 +010013#include <linux/cred.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010014#include <linux/xattr.h>
15#include "overlayfs.h"
16#include "ovl_entry.h"
17
18int ovl_want_write(struct dentry *dentry)
19{
20 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
21 return mnt_want_write(ofs->upper_mnt);
22}
23
24void ovl_drop_write(struct dentry *dentry)
25{
26 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
27 mnt_drop_write(ofs->upper_mnt);
28}
29
30struct dentry *ovl_workdir(struct dentry *dentry)
31{
32 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
33 return ofs->workdir;
34}
35
36const struct cred *ovl_override_creds(struct super_block *sb)
37{
38 struct ovl_fs *ofs = sb->s_fs_info;
39
40 return override_creds(ofs->creator_cred);
41}
42
Amir Goldstein7bcd74b2017-03-22 08:42:21 -040043struct super_block *ovl_same_sb(struct super_block *sb)
44{
45 struct ovl_fs *ofs = sb->s_fs_info;
46
47 return ofs->same_sb;
48}
49
Miklos Szeredibbb1e542016-12-16 11:02:56 +010050struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
51{
52 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
53 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
54
55 if (oe)
56 oe->numlower = numlower;
57
58 return oe;
59}
60
61bool ovl_dentry_remote(struct dentry *dentry)
62{
63 return dentry->d_flags &
64 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
65 DCACHE_OP_REAL);
66}
67
68bool ovl_dentry_weird(struct dentry *dentry)
69{
70 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
71 DCACHE_MANAGE_TRANSIT |
72 DCACHE_OP_HASH |
73 DCACHE_OP_COMPARE);
74}
75
76enum ovl_path_type ovl_path_type(struct dentry *dentry)
77{
78 struct ovl_entry *oe = dentry->d_fsdata;
79 enum ovl_path_type type = 0;
80
81 if (oe->__upperdentry) {
82 type = __OVL_PATH_UPPER;
83
84 /*
Amir Goldstein59548502017-04-23 23:12:34 +030085 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +010086 */
Amir Goldstein59548502017-04-23 23:12:34 +030087 if (oe->numlower) {
88 type |= __OVL_PATH_ORIGIN;
89 if (d_is_dir(dentry))
90 type |= __OVL_PATH_MERGE;
91 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +010092 } else {
93 if (oe->numlower > 1)
94 type |= __OVL_PATH_MERGE;
95 }
96 return type;
97}
98
99void ovl_path_upper(struct dentry *dentry, struct path *path)
100{
101 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
102 struct ovl_entry *oe = dentry->d_fsdata;
103
104 path->mnt = ofs->upper_mnt;
105 path->dentry = ovl_upperdentry_dereference(oe);
106}
107
108void ovl_path_lower(struct dentry *dentry, struct path *path)
109{
110 struct ovl_entry *oe = dentry->d_fsdata;
111
Kees Cook33006cd2017-03-29 14:02:19 -0700112 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100113}
114
115enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
116{
117 enum ovl_path_type type = ovl_path_type(dentry);
118
119 if (!OVL_TYPE_UPPER(type))
120 ovl_path_lower(dentry, path);
121 else
122 ovl_path_upper(dentry, path);
123
124 return type;
125}
126
127struct dentry *ovl_dentry_upper(struct dentry *dentry)
128{
129 struct ovl_entry *oe = dentry->d_fsdata;
130
131 return ovl_upperdentry_dereference(oe);
132}
133
134static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
135{
136 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
137}
138
139struct dentry *ovl_dentry_lower(struct dentry *dentry)
140{
141 struct ovl_entry *oe = dentry->d_fsdata;
142
143 return __ovl_dentry_lower(oe);
144}
145
146struct dentry *ovl_dentry_real(struct dentry *dentry)
147{
148 struct ovl_entry *oe = dentry->d_fsdata;
149 struct dentry *realdentry;
150
151 realdentry = ovl_upperdentry_dereference(oe);
152 if (!realdentry)
153 realdentry = __ovl_dentry_lower(oe);
154
155 return realdentry;
156}
157
Miklos Szeredi25b77132017-07-04 22:03:16 +0200158struct inode *ovl_inode_real(struct inode *inode, bool *is_upper)
159{
160 struct inode *realinode = lockless_dereference(OVL_I(inode)->upper);
161 bool isup = false;
162
163 if (!realinode)
164 realinode = OVL_I(inode)->lower;
165 else
166 isup = true;
167
168 if (is_upper)
169 *is_upper = isup;
170
171 return realinode;
172}
173
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100174struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
175{
176 struct ovl_entry *oe = dentry->d_fsdata;
177
178 return oe->cache;
179}
180
181void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
182{
183 struct ovl_entry *oe = dentry->d_fsdata;
184
185 oe->cache = cache;
186}
187
188bool ovl_dentry_is_opaque(struct dentry *dentry)
189{
190 struct ovl_entry *oe = dentry->d_fsdata;
191 return oe->opaque;
192}
193
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300194bool ovl_dentry_is_impure(struct dentry *dentry)
195{
196 struct ovl_entry *oe = dentry->d_fsdata;
197
198 return oe->impure;
199}
200
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100201bool ovl_dentry_is_whiteout(struct dentry *dentry)
202{
203 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
204}
205
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100206void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100207{
208 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100209
210 oe->opaque = true;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100211}
212
Miklos Szeredia6c60652016-12-16 11:02:56 +0100213bool ovl_redirect_dir(struct super_block *sb)
214{
215 struct ovl_fs *ofs = sb->s_fs_info;
216
Amir Goldstein21a22872017-05-17 00:12:41 +0300217 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100218}
219
220const char *ovl_dentry_get_redirect(struct dentry *dentry)
221{
222 struct ovl_entry *oe = dentry->d_fsdata;
223
224 return oe->redirect;
225}
226
227void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
228{
229 struct ovl_entry *oe = dentry->d_fsdata;
230
231 kfree(oe->redirect);
232 oe->redirect = redirect;
233}
234
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100235void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
236{
237 struct ovl_entry *oe = dentry->d_fsdata;
238
239 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
240 WARN_ON(oe->__upperdentry);
241 /*
242 * Make sure upperdentry is consistent before making it visible to
243 * ovl_upperdentry_dereference().
244 */
245 smp_wmb();
246 oe->__upperdentry = upperdentry;
247}
248
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200249void ovl_inode_init(struct inode *inode, struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100250{
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200251 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200252
Miklos Szeredi25b77132017-07-04 22:03:16 +0200253 if (ovl_dentry_upper(dentry))
254 OVL_I(inode)->upper = realinode;
255 else
256 OVL_I(inode)->lower = realinode;
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200257
258 ovl_copyattr(realinode, inode);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100259}
260
261void ovl_inode_update(struct inode *inode, struct inode *upperinode)
262{
263 WARN_ON(!upperinode);
264 WARN_ON(!inode_unhashed(inode));
Miklos Szeredi25b77132017-07-04 22:03:16 +0200265 /*
266 * Make sure upperinode is consistent before making it visible to
267 * ovl_inode_real();
268 */
269 smp_wmb();
270 OVL_I(inode)->upper = upperinode;
271 if (!S_ISDIR(upperinode->i_mode)) {
272 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100273 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200274 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100275}
276
277void ovl_dentry_version_inc(struct dentry *dentry)
278{
279 struct ovl_entry *oe = dentry->d_fsdata;
280
281 WARN_ON(!inode_is_locked(dentry->d_inode));
282 oe->version++;
283}
284
285u64 ovl_dentry_version_get(struct dentry *dentry)
286{
287 struct ovl_entry *oe = dentry->d_fsdata;
288
289 WARN_ON(!inode_is_locked(dentry->d_inode));
290 return oe->version;
291}
292
293bool ovl_is_whiteout(struct dentry *dentry)
294{
295 struct inode *inode = dentry->d_inode;
296
297 return inode && IS_WHITEOUT(inode);
298}
299
300struct file *ovl_path_open(struct path *path, int flags)
301{
302 return dentry_open(path, flags | O_NOATIME, current_cred());
303}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200304
305int ovl_copy_up_start(struct dentry *dentry)
306{
307 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
308 struct ovl_entry *oe = dentry->d_fsdata;
309 int err;
310
311 spin_lock(&ofs->copyup_wq.lock);
312 err = wait_event_interruptible_locked(ofs->copyup_wq, !oe->copying);
313 if (!err) {
314 if (oe->__upperdentry)
315 err = 1; /* Already copied up */
316 else
317 oe->copying = true;
318 }
319 spin_unlock(&ofs->copyup_wq.lock);
320
321 return err;
322}
323
324void ovl_copy_up_end(struct dentry *dentry)
325{
326 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
327 struct ovl_entry *oe = dentry->d_fsdata;
328
329 spin_lock(&ofs->copyup_wq.lock);
330 oe->copying = false;
331 wake_up_locked(&ofs->copyup_wq);
332 spin_unlock(&ofs->copyup_wq.lock);
333}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300334
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300335bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
336{
337 int res;
338 char val;
339
340 if (!d_is_dir(dentry))
341 return false;
342
343 res = vfs_getxattr(dentry, name, &val, 1);
344 if (res == 1 && val == 'y')
345 return true;
346
347 return false;
348}
349
Amir Goldstein82b749b2017-05-17 00:12:40 +0300350int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
351 const char *name, const void *value, size_t size,
352 int xerr)
353{
354 int err;
355 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
356
357 if (ofs->noxattr)
358 return xerr;
359
360 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
361
362 if (err == -EOPNOTSUPP) {
363 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
364 ofs->noxattr = true;
365 return xerr;
366 }
367
368 return err;
369}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300370
371int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
372{
373 int err;
374 struct ovl_entry *oe = dentry->d_fsdata;
375
376 if (oe->impure)
377 return 0;
378
379 /*
380 * Do not fail when upper doesn't support xattrs.
381 * Upper inodes won't have origin nor redirect xattr anyway.
382 */
383 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
384 "y", 1, 0);
385 if (!err)
386 oe->impure = true;
387
388 return err;
389}