blob: be0670f1addfc32c6f22989fc8966b6e63e96826 [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
Miklos Szeredi09d8b582017-07-04 22:03:16 +020081 if (ovl_dentry_upper(dentry)) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +010082 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;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100102
103 path->mnt = ofs->upper_mnt;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200104 path->dentry = ovl_dentry_upper(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100105}
106
107void ovl_path_lower(struct dentry *dentry, struct path *path)
108{
109 struct ovl_entry *oe = dentry->d_fsdata;
110
Kees Cook33006cd2017-03-29 14:02:19 -0700111 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100112}
113
114enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
115{
116 enum ovl_path_type type = ovl_path_type(dentry);
117
118 if (!OVL_TYPE_UPPER(type))
119 ovl_path_lower(dentry, path);
120 else
121 ovl_path_upper(dentry, path);
122
123 return type;
124}
125
126struct dentry *ovl_dentry_upper(struct dentry *dentry)
127{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200128 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100129}
130
131struct dentry *ovl_dentry_lower(struct dentry *dentry)
132{
133 struct ovl_entry *oe = dentry->d_fsdata;
134
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200135 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100136}
137
138struct dentry *ovl_dentry_real(struct dentry *dentry)
139{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200140 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100141}
142
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200143struct inode *ovl_inode_upper(struct inode *inode)
Miklos Szeredi25b77132017-07-04 22:03:16 +0200144{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200145 struct dentry *upperdentry = ovl_upperdentry_dereference(OVL_I(inode));
Miklos Szeredi25b77132017-07-04 22:03:16 +0200146
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200147 return upperdentry ? d_inode(upperdentry) : NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200148}
149
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200150struct inode *ovl_inode_lower(struct inode *inode)
151{
152 return OVL_I(inode)->lower;
153}
154
155struct inode *ovl_inode_real(struct inode *inode)
156{
157 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
158}
159
160
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100161struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
162{
163 struct ovl_entry *oe = dentry->d_fsdata;
164
165 return oe->cache;
166}
167
168void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
169{
170 struct ovl_entry *oe = dentry->d_fsdata;
171
172 oe->cache = cache;
173}
174
175bool ovl_dentry_is_opaque(struct dentry *dentry)
176{
177 struct ovl_entry *oe = dentry->d_fsdata;
178 return oe->opaque;
179}
180
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300181bool ovl_dentry_is_impure(struct dentry *dentry)
182{
183 struct ovl_entry *oe = dentry->d_fsdata;
184
185 return oe->impure;
186}
187
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100188bool ovl_dentry_is_whiteout(struct dentry *dentry)
189{
190 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
191}
192
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100193void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100194{
195 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100196
197 oe->opaque = true;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100198}
199
Miklos Szeredia6c60652016-12-16 11:02:56 +0100200bool ovl_redirect_dir(struct super_block *sb)
201{
202 struct ovl_fs *ofs = sb->s_fs_info;
203
Amir Goldstein21a22872017-05-17 00:12:41 +0300204 return ofs->config.redirect_dir && !ofs->noxattr;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100205}
206
207const char *ovl_dentry_get_redirect(struct dentry *dentry)
208{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200209 return OVL_I(d_inode(dentry))->redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100210}
211
212void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
213{
Miklos Szeredicf31c462017-07-04 22:03:16 +0200214 struct ovl_inode *oi = OVL_I(d_inode(dentry));
Miklos Szeredia6c60652016-12-16 11:02:56 +0100215
Miklos Szeredicf31c462017-07-04 22:03:16 +0200216 kfree(oi->redirect);
217 oi->redirect = redirect;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100218}
219
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200220void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
221 struct dentry *lowerdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100222{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200223 if (upperdentry)
224 OVL_I(inode)->__upperdentry = upperdentry;
225 if (lowerdentry)
226 OVL_I(inode)->lower = d_inode(lowerdentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100227
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200228 ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100229}
230
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200231void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100232{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200233 struct inode *upperinode = d_inode(upperdentry);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200234
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100235 WARN_ON(!inode_unhashed(inode));
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200236 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
237 WARN_ON(OVL_I(inode)->__upperdentry);
238
Miklos Szeredi25b77132017-07-04 22:03:16 +0200239 /*
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200240 * Make sure upperdentry is consistent before making it visible
Miklos Szeredi25b77132017-07-04 22:03:16 +0200241 */
242 smp_wmb();
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200243 OVL_I(inode)->__upperdentry = upperdentry;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200244 if (!S_ISDIR(upperinode->i_mode)) {
245 inode->i_private = upperinode;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100246 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200247 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100248}
249
250void 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
258u64 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
266bool ovl_is_whiteout(struct dentry *dentry)
267{
268 struct inode *inode = dentry->d_inode;
269
270 return inode && IS_WHITEOUT(inode);
271}
272
273struct file *ovl_path_open(struct path *path, int flags)
274{
275 return dentry_open(path, flags | O_NOATIME, current_cred());
276}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200277
278int 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) {
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200287 if (ovl_dentry_upper(dentry))
Amir Goldstein39d3d602017-01-17 06:34:56 +0200288 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
297void 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}
Amir Goldstein82b749b2017-05-17 00:12:40 +0300307
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300308bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
309{
310 int res;
311 char val;
312
313 if (!d_is_dir(dentry))
314 return false;
315
316 res = vfs_getxattr(dentry, name, &val, 1);
317 if (res == 1 && val == 'y')
318 return true;
319
320 return false;
321}
322
Amir Goldstein82b749b2017-05-17 00:12:40 +0300323int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
324 const char *name, const void *value, size_t size,
325 int xerr)
326{
327 int err;
328 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
329
330 if (ofs->noxattr)
331 return xerr;
332
333 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
334
335 if (err == -EOPNOTSUPP) {
336 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
337 ofs->noxattr = true;
338 return xerr;
339 }
340
341 return err;
342}
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300343
344int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
345{
346 int err;
347 struct ovl_entry *oe = dentry->d_fsdata;
348
349 if (oe->impure)
350 return 0;
351
352 /*
353 * Do not fail when upper doesn't support xattrs.
354 * Upper inodes won't have origin nor redirect xattr anyway.
355 */
356 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
357 "y", 1, 0);
358 if (!err)
359 oe->impure = true;
360
361 return err;
362}