blob: 260b215852a37907fc0928da65b6d016411c1102 [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>
13#include <linux/xattr.h>
14#include "overlayfs.h"
15#include "ovl_entry.h"
16
17int ovl_want_write(struct dentry *dentry)
18{
19 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
20 return mnt_want_write(ofs->upper_mnt);
21}
22
23void ovl_drop_write(struct dentry *dentry)
24{
25 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
26 mnt_drop_write(ofs->upper_mnt);
27}
28
29struct dentry *ovl_workdir(struct dentry *dentry)
30{
31 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
32 return ofs->workdir;
33}
34
35const struct cred *ovl_override_creds(struct super_block *sb)
36{
37 struct ovl_fs *ofs = sb->s_fs_info;
38
39 return override_creds(ofs->creator_cred);
40}
41
42struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
43{
44 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
45 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
46
47 if (oe)
48 oe->numlower = numlower;
49
50 return oe;
51}
52
53bool ovl_dentry_remote(struct dentry *dentry)
54{
55 return dentry->d_flags &
56 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
57 DCACHE_OP_REAL);
58}
59
60bool ovl_dentry_weird(struct dentry *dentry)
61{
62 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
63 DCACHE_MANAGE_TRANSIT |
64 DCACHE_OP_HASH |
65 DCACHE_OP_COMPARE);
66}
67
68enum ovl_path_type ovl_path_type(struct dentry *dentry)
69{
70 struct ovl_entry *oe = dentry->d_fsdata;
71 enum ovl_path_type type = 0;
72
73 if (oe->__upperdentry) {
74 type = __OVL_PATH_UPPER;
75
76 /*
77 * Non-dir dentry can hold lower dentry from previous
78 * location.
79 */
80 if (oe->numlower && d_is_dir(dentry))
81 type |= __OVL_PATH_MERGE;
82 } else {
83 if (oe->numlower > 1)
84 type |= __OVL_PATH_MERGE;
85 }
86 return type;
87}
88
89void ovl_path_upper(struct dentry *dentry, struct path *path)
90{
91 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
92 struct ovl_entry *oe = dentry->d_fsdata;
93
94 path->mnt = ofs->upper_mnt;
95 path->dentry = ovl_upperdentry_dereference(oe);
96}
97
98void ovl_path_lower(struct dentry *dentry, struct path *path)
99{
100 struct ovl_entry *oe = dentry->d_fsdata;
101
102 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
103}
104
105enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
106{
107 enum ovl_path_type type = ovl_path_type(dentry);
108
109 if (!OVL_TYPE_UPPER(type))
110 ovl_path_lower(dentry, path);
111 else
112 ovl_path_upper(dentry, path);
113
114 return type;
115}
116
117struct dentry *ovl_dentry_upper(struct dentry *dentry)
118{
119 struct ovl_entry *oe = dentry->d_fsdata;
120
121 return ovl_upperdentry_dereference(oe);
122}
123
124static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
125{
126 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
127}
128
129struct dentry *ovl_dentry_lower(struct dentry *dentry)
130{
131 struct ovl_entry *oe = dentry->d_fsdata;
132
133 return __ovl_dentry_lower(oe);
134}
135
136struct dentry *ovl_dentry_real(struct dentry *dentry)
137{
138 struct ovl_entry *oe = dentry->d_fsdata;
139 struct dentry *realdentry;
140
141 realdentry = ovl_upperdentry_dereference(oe);
142 if (!realdentry)
143 realdentry = __ovl_dentry_lower(oe);
144
145 return realdentry;
146}
147
148struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
149{
150 struct ovl_entry *oe = dentry->d_fsdata;
151
152 return oe->cache;
153}
154
155void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
156{
157 struct ovl_entry *oe = dentry->d_fsdata;
158
159 oe->cache = cache;
160}
161
162bool ovl_dentry_is_opaque(struct dentry *dentry)
163{
164 struct ovl_entry *oe = dentry->d_fsdata;
165 return oe->opaque;
166}
167
168bool ovl_dentry_is_whiteout(struct dentry *dentry)
169{
170 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
171}
172
173void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
174{
175 struct ovl_entry *oe = dentry->d_fsdata;
176 oe->opaque = opaque;
177}
178
Miklos Szeredia6c60652016-12-16 11:02:56 +0100179bool ovl_redirect_dir(struct super_block *sb)
180{
181 struct ovl_fs *ofs = sb->s_fs_info;
182
183 return ofs->config.redirect_dir;
184}
185
186void ovl_clear_redirect_dir(struct super_block *sb)
187{
188 struct ovl_fs *ofs = sb->s_fs_info;
189
190 ofs->config.redirect_dir = false;
191}
192
193const char *ovl_dentry_get_redirect(struct dentry *dentry)
194{
195 struct ovl_entry *oe = dentry->d_fsdata;
196
197 return oe->redirect;
198}
199
200void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
201{
202 struct ovl_entry *oe = dentry->d_fsdata;
203
204 kfree(oe->redirect);
205 oe->redirect = redirect;
206}
207
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100208void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
209{
210 struct ovl_entry *oe = dentry->d_fsdata;
211
212 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
213 WARN_ON(oe->__upperdentry);
214 /*
215 * Make sure upperdentry is consistent before making it visible to
216 * ovl_upperdentry_dereference().
217 */
218 smp_wmb();
219 oe->__upperdentry = upperdentry;
220}
221
222void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper)
223{
224 WRITE_ONCE(inode->i_private, (unsigned long) realinode |
225 (is_upper ? OVL_ISUPPER_MASK : 0));
226}
227
228void ovl_inode_update(struct inode *inode, struct inode *upperinode)
229{
230 WARN_ON(!upperinode);
231 WARN_ON(!inode_unhashed(inode));
232 WRITE_ONCE(inode->i_private,
233 (unsigned long) upperinode | OVL_ISUPPER_MASK);
234 if (!S_ISDIR(upperinode->i_mode))
235 __insert_inode_hash(inode, (unsigned long) upperinode);
236}
237
238void ovl_dentry_version_inc(struct dentry *dentry)
239{
240 struct ovl_entry *oe = dentry->d_fsdata;
241
242 WARN_ON(!inode_is_locked(dentry->d_inode));
243 oe->version++;
244}
245
246u64 ovl_dentry_version_get(struct dentry *dentry)
247{
248 struct ovl_entry *oe = dentry->d_fsdata;
249
250 WARN_ON(!inode_is_locked(dentry->d_inode));
251 return oe->version;
252}
253
254bool ovl_is_whiteout(struct dentry *dentry)
255{
256 struct inode *inode = dentry->d_inode;
257
258 return inode && IS_WHITEOUT(inode);
259}
260
261struct file *ovl_path_open(struct path *path, int flags)
262{
263 return dentry_open(path, flags | O_NOATIME, current_cred());
264}