]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/overlayfs/util.c
Merge tag 'sti-dt-for-v4.10-rc' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-zesty-kernel.git] / fs / overlayfs / util.c
1 /*
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
17 int 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
23 void 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
29 struct dentry *ovl_workdir(struct dentry *dentry)
30 {
31 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
32 return ofs->workdir;
33 }
34
35 const 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
42 struct 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
53 bool 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
60 bool 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
68 enum 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
89 void 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
98 void 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
105 enum 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
117 struct 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
124 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
125 {
126 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
127 }
128
129 struct 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
136 struct 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
148 struct 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
155 void 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
162 bool ovl_dentry_is_opaque(struct dentry *dentry)
163 {
164 struct ovl_entry *oe = dentry->d_fsdata;
165 return oe->opaque;
166 }
167
168 bool ovl_dentry_is_whiteout(struct dentry *dentry)
169 {
170 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
171 }
172
173 void ovl_dentry_set_opaque(struct dentry *dentry)
174 {
175 struct ovl_entry *oe = dentry->d_fsdata;
176
177 oe->opaque = true;
178 }
179
180 bool ovl_redirect_dir(struct super_block *sb)
181 {
182 struct ovl_fs *ofs = sb->s_fs_info;
183
184 return ofs->config.redirect_dir;
185 }
186
187 void ovl_clear_redirect_dir(struct super_block *sb)
188 {
189 struct ovl_fs *ofs = sb->s_fs_info;
190
191 ofs->config.redirect_dir = false;
192 }
193
194 const char *ovl_dentry_get_redirect(struct dentry *dentry)
195 {
196 struct ovl_entry *oe = dentry->d_fsdata;
197
198 return oe->redirect;
199 }
200
201 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
202 {
203 struct ovl_entry *oe = dentry->d_fsdata;
204
205 kfree(oe->redirect);
206 oe->redirect = redirect;
207 }
208
209 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
210 {
211 struct ovl_entry *oe = dentry->d_fsdata;
212
213 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
214 WARN_ON(oe->__upperdentry);
215 /*
216 * Make sure upperdentry is consistent before making it visible to
217 * ovl_upperdentry_dereference().
218 */
219 smp_wmb();
220 oe->__upperdentry = upperdentry;
221 }
222
223 void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper)
224 {
225 WRITE_ONCE(inode->i_private, (unsigned long) realinode |
226 (is_upper ? OVL_ISUPPER_MASK : 0));
227 }
228
229 void ovl_inode_update(struct inode *inode, struct inode *upperinode)
230 {
231 WARN_ON(!upperinode);
232 WARN_ON(!inode_unhashed(inode));
233 WRITE_ONCE(inode->i_private,
234 (unsigned long) upperinode | OVL_ISUPPER_MASK);
235 if (!S_ISDIR(upperinode->i_mode))
236 __insert_inode_hash(inode, (unsigned long) upperinode);
237 }
238
239 void ovl_dentry_version_inc(struct dentry *dentry)
240 {
241 struct ovl_entry *oe = dentry->d_fsdata;
242
243 WARN_ON(!inode_is_locked(dentry->d_inode));
244 oe->version++;
245 }
246
247 u64 ovl_dentry_version_get(struct dentry *dentry)
248 {
249 struct ovl_entry *oe = dentry->d_fsdata;
250
251 WARN_ON(!inode_is_locked(dentry->d_inode));
252 return oe->version;
253 }
254
255 bool ovl_is_whiteout(struct dentry *dentry)
256 {
257 struct inode *inode = dentry->d_inode;
258
259 return inode && IS_WHITEOUT(inode);
260 }
261
262 struct file *ovl_path_open(struct path *path, int flags)
263 {
264 return dentry_open(path, flags | O_NOATIME, current_cred());
265 }