]>
Commit | Line | Data |
---|---|---|
bbb1e54d MS |
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> | |
5b825c3a | 13 | #include <linux/cred.h> |
bbb1e54d MS |
14 | #include <linux/xattr.h> |
15 | #include "overlayfs.h" | |
16 | #include "ovl_entry.h" | |
17 | ||
18 | int 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 | ||
24 | void 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 | ||
30 | struct dentry *ovl_workdir(struct dentry *dentry) | |
31 | { | |
32 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
33 | return ofs->workdir; | |
34 | } | |
35 | ||
36 | const 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 | ||
7bcd74b9 AG |
43 | struct 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 | ||
bbb1e54d MS |
50 | struct 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 | ||
61 | bool 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 | ||
68 | bool 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 | ||
76 | enum 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 | ||
81 | if (oe->__upperdentry) { | |
82 | type = __OVL_PATH_UPPER; | |
83 | ||
84 | /* | |
59548503 | 85 | * Non-dir dentry can hold lower dentry of its copy up origin. |
bbb1e54d | 86 | */ |
59548503 AG |
87 | if (oe->numlower) { |
88 | type |= __OVL_PATH_ORIGIN; | |
89 | if (d_is_dir(dentry)) | |
90 | type |= __OVL_PATH_MERGE; | |
91 | } | |
bbb1e54d MS |
92 | } else { |
93 | if (oe->numlower > 1) | |
94 | type |= __OVL_PATH_MERGE; | |
95 | } | |
96 | return type; | |
97 | } | |
98 | ||
99 | void ovl_path_upper(struct dentry *dentry, struct path *path) | |
100 | { | |
101 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
102 | struct ovl_entry *oe = dentry->d_fsdata; | |
103 | ||
104 | path->mnt = ofs->upper_mnt; | |
105 | path->dentry = ovl_upperdentry_dereference(oe); | |
106 | } | |
107 | ||
108 | void ovl_path_lower(struct dentry *dentry, struct path *path) | |
109 | { | |
110 | struct ovl_entry *oe = dentry->d_fsdata; | |
111 | ||
33006cdf | 112 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { }; |
bbb1e54d MS |
113 | } |
114 | ||
115 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) | |
116 | { | |
117 | enum ovl_path_type type = ovl_path_type(dentry); | |
118 | ||
119 | if (!OVL_TYPE_UPPER(type)) | |
120 | ovl_path_lower(dentry, path); | |
121 | else | |
122 | ovl_path_upper(dentry, path); | |
123 | ||
124 | return type; | |
125 | } | |
126 | ||
127 | struct dentry *ovl_dentry_upper(struct dentry *dentry) | |
128 | { | |
129 | struct ovl_entry *oe = dentry->d_fsdata; | |
130 | ||
131 | return ovl_upperdentry_dereference(oe); | |
132 | } | |
133 | ||
134 | static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) | |
135 | { | |
136 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; | |
137 | } | |
138 | ||
139 | struct dentry *ovl_dentry_lower(struct dentry *dentry) | |
140 | { | |
141 | struct ovl_entry *oe = dentry->d_fsdata; | |
142 | ||
143 | return __ovl_dentry_lower(oe); | |
144 | } | |
145 | ||
146 | struct dentry *ovl_dentry_real(struct dentry *dentry) | |
147 | { | |
148 | struct ovl_entry *oe = dentry->d_fsdata; | |
149 | struct dentry *realdentry; | |
150 | ||
151 | realdentry = ovl_upperdentry_dereference(oe); | |
152 | if (!realdentry) | |
153 | realdentry = __ovl_dentry_lower(oe); | |
154 | ||
155 | return realdentry; | |
156 | } | |
157 | ||
158 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) | |
159 | { | |
160 | struct ovl_entry *oe = dentry->d_fsdata; | |
161 | ||
162 | return oe->cache; | |
163 | } | |
164 | ||
165 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) | |
166 | { | |
167 | struct ovl_entry *oe = dentry->d_fsdata; | |
168 | ||
169 | oe->cache = cache; | |
170 | } | |
171 | ||
172 | bool ovl_dentry_is_opaque(struct dentry *dentry) | |
173 | { | |
174 | struct ovl_entry *oe = dentry->d_fsdata; | |
175 | return oe->opaque; | |
176 | } | |
177 | ||
178 | bool ovl_dentry_is_whiteout(struct dentry *dentry) | |
179 | { | |
180 | return !dentry->d_inode && ovl_dentry_is_opaque(dentry); | |
181 | } | |
182 | ||
5cf5b477 | 183 | void ovl_dentry_set_opaque(struct dentry *dentry) |
bbb1e54d MS |
184 | { |
185 | struct ovl_entry *oe = dentry->d_fsdata; | |
5cf5b477 MS |
186 | |
187 | oe->opaque = true; | |
bbb1e54d MS |
188 | } |
189 | ||
a6c60655 MS |
190 | bool ovl_redirect_dir(struct super_block *sb) |
191 | { | |
192 | struct ovl_fs *ofs = sb->s_fs_info; | |
193 | ||
21a22878 | 194 | return ofs->config.redirect_dir && !ofs->noxattr; |
a6c60655 MS |
195 | } |
196 | ||
197 | const char *ovl_dentry_get_redirect(struct dentry *dentry) | |
198 | { | |
199 | struct ovl_entry *oe = dentry->d_fsdata; | |
200 | ||
201 | return oe->redirect; | |
202 | } | |
203 | ||
204 | void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) | |
205 | { | |
206 | struct ovl_entry *oe = dentry->d_fsdata; | |
207 | ||
208 | kfree(oe->redirect); | |
209 | oe->redirect = redirect; | |
210 | } | |
211 | ||
bbb1e54d MS |
212 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) |
213 | { | |
214 | struct ovl_entry *oe = dentry->d_fsdata; | |
215 | ||
216 | WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode)); | |
217 | WARN_ON(oe->__upperdentry); | |
218 | /* | |
219 | * Make sure upperdentry is consistent before making it visible to | |
220 | * ovl_upperdentry_dereference(). | |
221 | */ | |
222 | smp_wmb(); | |
223 | oe->__upperdentry = upperdentry; | |
224 | } | |
225 | ||
226 | void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper) | |
227 | { | |
228 | WRITE_ONCE(inode->i_private, (unsigned long) realinode | | |
229 | (is_upper ? OVL_ISUPPER_MASK : 0)); | |
230 | } | |
231 | ||
232 | void ovl_inode_update(struct inode *inode, struct inode *upperinode) | |
233 | { | |
234 | WARN_ON(!upperinode); | |
235 | WARN_ON(!inode_unhashed(inode)); | |
236 | WRITE_ONCE(inode->i_private, | |
237 | (unsigned long) upperinode | OVL_ISUPPER_MASK); | |
238 | if (!S_ISDIR(upperinode->i_mode)) | |
239 | __insert_inode_hash(inode, (unsigned long) upperinode); | |
240 | } | |
241 | ||
242 | void ovl_dentry_version_inc(struct dentry *dentry) | |
243 | { | |
244 | struct ovl_entry *oe = dentry->d_fsdata; | |
245 | ||
246 | WARN_ON(!inode_is_locked(dentry->d_inode)); | |
247 | oe->version++; | |
248 | } | |
249 | ||
250 | u64 ovl_dentry_version_get(struct dentry *dentry) | |
251 | { | |
252 | struct ovl_entry *oe = dentry->d_fsdata; | |
253 | ||
254 | WARN_ON(!inode_is_locked(dentry->d_inode)); | |
255 | return oe->version; | |
256 | } | |
257 | ||
258 | bool ovl_is_whiteout(struct dentry *dentry) | |
259 | { | |
260 | struct inode *inode = dentry->d_inode; | |
261 | ||
262 | return inode && IS_WHITEOUT(inode); | |
263 | } | |
264 | ||
265 | struct file *ovl_path_open(struct path *path, int flags) | |
266 | { | |
267 | return dentry_open(path, flags | O_NOATIME, current_cred()); | |
268 | } | |
39d3d60a AG |
269 | |
270 | int ovl_copy_up_start(struct dentry *dentry) | |
271 | { | |
272 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
273 | struct ovl_entry *oe = dentry->d_fsdata; | |
274 | int err; | |
275 | ||
276 | spin_lock(&ofs->copyup_wq.lock); | |
277 | err = wait_event_interruptible_locked(ofs->copyup_wq, !oe->copying); | |
278 | if (!err) { | |
279 | if (oe->__upperdentry) | |
280 | err = 1; /* Already copied up */ | |
281 | else | |
282 | oe->copying = true; | |
283 | } | |
284 | spin_unlock(&ofs->copyup_wq.lock); | |
285 | ||
286 | return err; | |
287 | } | |
288 | ||
289 | void ovl_copy_up_end(struct dentry *dentry) | |
290 | { | |
291 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
292 | struct ovl_entry *oe = dentry->d_fsdata; | |
293 | ||
294 | spin_lock(&ofs->copyup_wq.lock); | |
295 | oe->copying = false; | |
296 | wake_up_locked(&ofs->copyup_wq); | |
297 | spin_unlock(&ofs->copyup_wq.lock); | |
298 | } | |
82b749b2 AG |
299 | |
300 | int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, | |
301 | const char *name, const void *value, size_t size, | |
302 | int xerr) | |
303 | { | |
304 | int err; | |
305 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
306 | ||
307 | if (ofs->noxattr) | |
308 | return xerr; | |
309 | ||
310 | err = ovl_do_setxattr(upperdentry, name, value, size, 0); | |
311 | ||
312 | if (err == -EOPNOTSUPP) { | |
313 | pr_warn("overlayfs: cannot set %s xattr on upper\n", name); | |
314 | ofs->noxattr = true; | |
315 | return xerr; | |
316 | } | |
317 | ||
318 | return err; | |
319 | } |