blob: 1c1b6ea7bf7c8316d703d2daed516e5ec2cdf94e [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>
Linus Torvaldse58bc922017-03-03 11:55:57 -080015#include <linux/sched/signal.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010016#include "overlayfs.h"
17#include "ovl_entry.h"
18
19int 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
25void 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
31struct dentry *ovl_workdir(struct dentry *dentry)
32{
33 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
34 return ofs->workdir;
35}
36
37const 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
Amir Goldstein7bcd74b2017-03-22 08:42:21 -040044struct 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
Miklos Szeredibbb1e542016-12-16 11:02:56 +010051struct 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
62bool 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
69bool 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
77enum 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 /*
Amir Goldstein59548502017-04-23 23:12:34 +030086 * Non-dir dentry can hold lower dentry of its copy up origin.
Miklos Szeredibbb1e542016-12-16 11:02:56 +010087 */
Amir Goldstein59548502017-04-23 23:12:34 +030088 if (oe->numlower) {
89 type |= __OVL_PATH_ORIGIN;
90 if (d_is_dir(dentry))
91 type |= __OVL_PATH_MERGE;
92 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +010093 } else {
94 if (oe->numlower > 1)
95 type |= __OVL_PATH_MERGE;
96 }
97 return type;
98}
99
100void 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
109void ovl_path_lower(struct dentry *dentry, struct path *path)
110{
111 struct ovl_entry *oe = dentry->d_fsdata;
112
Kees Cook33006cd2017-03-29 14:02:19 -0700113 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100114}
115
116enum 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
128struct 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
135static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
136{
137 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
138}
139
140struct 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
147struct 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
159struct 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
166void 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
173bool ovl_dentry_is_opaque(struct dentry *dentry)
174{
175 struct ovl_entry *oe = dentry->d_fsdata;
176 return oe->opaque;
177}
178
179bool ovl_dentry_is_whiteout(struct dentry *dentry)
180{
181 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
182}
183
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100184void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100185{
186 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100187
188 oe->opaque = true;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100189}
190
Miklos Szeredia6c60652016-12-16 11:02:56 +0100191bool 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
198void 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
205const char *ovl_dentry_get_redirect(struct dentry *dentry)
206{
207 struct ovl_entry *oe = dentry->d_fsdata;
208
209 return oe->redirect;
210}
211
212void 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
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100220void 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
234void 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
240void 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
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) {
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
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}