blob: 2bd4c264ccbefca9caccf9c3613149068c5d6135 [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 /*
86 * Non-dir dentry can hold lower dentry from previous
87 * location.
88 */
89 if (oe->numlower && d_is_dir(dentry))
90 type |= __OVL_PATH_MERGE;
91 } else {
92 if (oe->numlower > 1)
93 type |= __OVL_PATH_MERGE;
94 }
95 return type;
96}
97
98void ovl_path_upper(struct dentry *dentry, struct path *path)
99{
100 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
101 struct ovl_entry *oe = dentry->d_fsdata;
102
103 path->mnt = ofs->upper_mnt;
104 path->dentry = ovl_upperdentry_dereference(oe);
105}
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{
128 struct ovl_entry *oe = dentry->d_fsdata;
129
130 return ovl_upperdentry_dereference(oe);
131}
132
133static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
134{
135 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
136}
137
138struct dentry *ovl_dentry_lower(struct dentry *dentry)
139{
140 struct ovl_entry *oe = dentry->d_fsdata;
141
142 return __ovl_dentry_lower(oe);
143}
144
145struct dentry *ovl_dentry_real(struct dentry *dentry)
146{
147 struct ovl_entry *oe = dentry->d_fsdata;
148 struct dentry *realdentry;
149
150 realdentry = ovl_upperdentry_dereference(oe);
151 if (!realdentry)
152 realdentry = __ovl_dentry_lower(oe);
153
154 return realdentry;
155}
156
157struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
158{
159 struct ovl_entry *oe = dentry->d_fsdata;
160
161 return oe->cache;
162}
163
164void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
165{
166 struct ovl_entry *oe = dentry->d_fsdata;
167
168 oe->cache = cache;
169}
170
171bool ovl_dentry_is_opaque(struct dentry *dentry)
172{
173 struct ovl_entry *oe = dentry->d_fsdata;
174 return oe->opaque;
175}
176
177bool ovl_dentry_is_whiteout(struct dentry *dentry)
178{
179 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
180}
181
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100182void ovl_dentry_set_opaque(struct dentry *dentry)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100183{
184 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100185
186 oe->opaque = true;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100187}
188
Miklos Szeredia6c60652016-12-16 11:02:56 +0100189bool ovl_redirect_dir(struct super_block *sb)
190{
191 struct ovl_fs *ofs = sb->s_fs_info;
192
193 return ofs->config.redirect_dir;
194}
195
196void ovl_clear_redirect_dir(struct super_block *sb)
197{
198 struct ovl_fs *ofs = sb->s_fs_info;
199
200 ofs->config.redirect_dir = false;
201}
202
203const char *ovl_dentry_get_redirect(struct dentry *dentry)
204{
205 struct ovl_entry *oe = dentry->d_fsdata;
206
207 return oe->redirect;
208}
209
210void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
211{
212 struct ovl_entry *oe = dentry->d_fsdata;
213
214 kfree(oe->redirect);
215 oe->redirect = redirect;
216}
217
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100218void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
219{
220 struct ovl_entry *oe = dentry->d_fsdata;
221
222 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
223 WARN_ON(oe->__upperdentry);
224 /*
225 * Make sure upperdentry is consistent before making it visible to
226 * ovl_upperdentry_dereference().
227 */
228 smp_wmb();
229 oe->__upperdentry = upperdentry;
230}
231
232void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper)
233{
234 WRITE_ONCE(inode->i_private, (unsigned long) realinode |
235 (is_upper ? OVL_ISUPPER_MASK : 0));
236}
237
238void ovl_inode_update(struct inode *inode, struct inode *upperinode)
239{
240 WARN_ON(!upperinode);
241 WARN_ON(!inode_unhashed(inode));
242 WRITE_ONCE(inode->i_private,
243 (unsigned long) upperinode | OVL_ISUPPER_MASK);
244 if (!S_ISDIR(upperinode->i_mode))
245 __insert_inode_hash(inode, (unsigned long) upperinode);
246}
247
248void ovl_dentry_version_inc(struct dentry *dentry)
249{
250 struct ovl_entry *oe = dentry->d_fsdata;
251
252 WARN_ON(!inode_is_locked(dentry->d_inode));
253 oe->version++;
254}
255
256u64 ovl_dentry_version_get(struct dentry *dentry)
257{
258 struct ovl_entry *oe = dentry->d_fsdata;
259
260 WARN_ON(!inode_is_locked(dentry->d_inode));
261 return oe->version;
262}
263
264bool ovl_is_whiteout(struct dentry *dentry)
265{
266 struct inode *inode = dentry->d_inode;
267
268 return inode && IS_WHITEOUT(inode);
269}
270
271struct file *ovl_path_open(struct path *path, int flags)
272{
273 return dentry_open(path, flags | O_NOATIME, current_cred());
274}
Amir Goldstein39d3d602017-01-17 06:34:56 +0200275
276int ovl_copy_up_start(struct dentry *dentry)
277{
278 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
279 struct ovl_entry *oe = dentry->d_fsdata;
280 int err;
281
282 spin_lock(&ofs->copyup_wq.lock);
283 err = wait_event_interruptible_locked(ofs->copyup_wq, !oe->copying);
284 if (!err) {
285 if (oe->__upperdentry)
286 err = 1; /* Already copied up */
287 else
288 oe->copying = true;
289 }
290 spin_unlock(&ofs->copyup_wq.lock);
291
292 return err;
293}
294
295void ovl_copy_up_end(struct dentry *dentry)
296{
297 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
298 struct ovl_entry *oe = dentry->d_fsdata;
299
300 spin_lock(&ofs->copyup_wq.lock);
301 oe->copying = false;
302 wake_up_locked(&ofs->copyup_wq);
303 spin_unlock(&ofs->copyup_wq.lock);
304}