Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
bbb1e54d MS |
2 | /* |
3 | * Copyright (C) 2011 Novell Inc. | |
4 | * Copyright (C) 2016 Red Hat, Inc. | |
bbb1e54d MS |
5 | */ |
6 | ||
7 | #include <linux/fs.h> | |
8 | #include <linux/mount.h> | |
9 | #include <linux/slab.h> | |
5b825c3a | 10 | #include <linux/cred.h> |
bbb1e54d | 11 | #include <linux/xattr.h> |
02bcd157 AG |
12 | #include <linux/exportfs.h> |
13 | #include <linux/uuid.h> | |
caf70cb2 AG |
14 | #include <linux/namei.h> |
15 | #include <linux/ratelimit.h> | |
bbb1e54d | 16 | #include "overlayfs.h" |
bbb1e54d MS |
17 | |
18 | int ovl_want_write(struct dentry *dentry) | |
19 | { | |
20 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
08f4c7c8 | 21 | return mnt_want_write(ovl_upper_mnt(ofs)); |
bbb1e54d MS |
22 | } |
23 | ||
24 | void ovl_drop_write(struct dentry *dentry) | |
25 | { | |
26 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
08f4c7c8 | 27 | mnt_drop_write(ovl_upper_mnt(ofs)); |
bbb1e54d MS |
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 | ||
e487d889 AG |
43 | /* |
44 | * Check if underlying fs supports file handles and try to determine encoding | |
45 | * type, in order to deduce maximum inode number used by fs. | |
46 | * | |
47 | * Return 0 if file handles are not supported. | |
48 | * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding. | |
49 | * Return -1 if fs uses a non default encoding with unknown inode size. | |
50 | */ | |
51 | int ovl_can_decode_fh(struct super_block *sb) | |
02bcd157 | 52 | { |
9df085f3 | 53 | if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry) |
e487d889 AG |
54 | return 0; |
55 | ||
56 | return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN; | |
02bcd157 AG |
57 | } |
58 | ||
59 | struct dentry *ovl_indexdir(struct super_block *sb) | |
60 | { | |
61 | struct ovl_fs *ofs = sb->s_fs_info; | |
62 | ||
63 | return ofs->indexdir; | |
64 | } | |
65 | ||
f168f109 AG |
66 | /* Index all files on copy up. For now only enabled for NFS export */ |
67 | bool ovl_index_all(struct super_block *sb) | |
68 | { | |
69 | struct ovl_fs *ofs = sb->s_fs_info; | |
70 | ||
71 | return ofs->config.nfs_export && ofs->config.index; | |
72 | } | |
73 | ||
74 | /* Verify lower origin on lookup. For now only enabled for NFS export */ | |
75 | bool ovl_verify_lower(struct super_block *sb) | |
76 | { | |
77 | struct ovl_fs *ofs = sb->s_fs_info; | |
78 | ||
79 | return ofs->config.nfs_export && ofs->config.index; | |
80 | } | |
81 | ||
bbb1e54d MS |
82 | struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
83 | { | |
84 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); | |
85 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); | |
86 | ||
87 | if (oe) | |
88 | oe->numlower = numlower; | |
89 | ||
90 | return oe; | |
91 | } | |
92 | ||
93 | bool ovl_dentry_remote(struct dentry *dentry) | |
94 | { | |
95 | return dentry->d_flags & | |
7925dad8 | 96 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); |
bbb1e54d MS |
97 | } |
98 | ||
f4288844 MS |
99 | void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry, |
100 | unsigned int mask) | |
101 | { | |
102 | struct ovl_entry *oe = OVL_E(dentry); | |
103 | unsigned int i, flags = 0; | |
104 | ||
bccece1e MS |
105 | if (upperdentry) |
106 | flags |= upperdentry->d_flags; | |
f4288844 MS |
107 | for (i = 0; i < oe->numlower; i++) |
108 | flags |= oe->lowerstack[i].dentry->d_flags; | |
109 | ||
110 | spin_lock(&dentry->d_lock); | |
111 | dentry->d_flags &= ~mask; | |
112 | dentry->d_flags |= flags & mask; | |
113 | spin_unlock(&dentry->d_lock); | |
114 | } | |
115 | ||
bbb1e54d MS |
116 | bool ovl_dentry_weird(struct dentry *dentry) |
117 | { | |
118 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | | |
119 | DCACHE_MANAGE_TRANSIT | | |
120 | DCACHE_OP_HASH | | |
121 | DCACHE_OP_COMPARE); | |
122 | } | |
123 | ||
124 | enum ovl_path_type ovl_path_type(struct dentry *dentry) | |
125 | { | |
126 | struct ovl_entry *oe = dentry->d_fsdata; | |
127 | enum ovl_path_type type = 0; | |
128 | ||
09d8b586 | 129 | if (ovl_dentry_upper(dentry)) { |
bbb1e54d MS |
130 | type = __OVL_PATH_UPPER; |
131 | ||
132 | /* | |
59548503 | 133 | * Non-dir dentry can hold lower dentry of its copy up origin. |
bbb1e54d | 134 | */ |
59548503 | 135 | if (oe->numlower) { |
60124877 VG |
136 | if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry))) |
137 | type |= __OVL_PATH_ORIGIN; | |
0b17c28a VG |
138 | if (d_is_dir(dentry) || |
139 | !ovl_has_upperdata(d_inode(dentry))) | |
59548503 AG |
140 | type |= __OVL_PATH_MERGE; |
141 | } | |
bbb1e54d MS |
142 | } else { |
143 | if (oe->numlower > 1) | |
144 | type |= __OVL_PATH_MERGE; | |
145 | } | |
146 | return type; | |
147 | } | |
148 | ||
149 | void ovl_path_upper(struct dentry *dentry, struct path *path) | |
150 | { | |
151 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
bbb1e54d | 152 | |
08f4c7c8 | 153 | path->mnt = ovl_upper_mnt(ofs); |
09d8b586 | 154 | path->dentry = ovl_dentry_upper(dentry); |
bbb1e54d MS |
155 | } |
156 | ||
157 | void ovl_path_lower(struct dentry *dentry, struct path *path) | |
158 | { | |
159 | struct ovl_entry *oe = dentry->d_fsdata; | |
160 | ||
b9343632 CR |
161 | if (oe->numlower) { |
162 | path->mnt = oe->lowerstack[0].layer->mnt; | |
163 | path->dentry = oe->lowerstack[0].dentry; | |
164 | } else { | |
165 | *path = (struct path) { }; | |
166 | } | |
bbb1e54d MS |
167 | } |
168 | ||
4f93b426 VG |
169 | void ovl_path_lowerdata(struct dentry *dentry, struct path *path) |
170 | { | |
171 | struct ovl_entry *oe = dentry->d_fsdata; | |
172 | ||
173 | if (oe->numlower) { | |
174 | path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt; | |
175 | path->dentry = oe->lowerstack[oe->numlower - 1].dentry; | |
176 | } else { | |
177 | *path = (struct path) { }; | |
178 | } | |
179 | } | |
180 | ||
bbb1e54d MS |
181 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) |
182 | { | |
183 | enum ovl_path_type type = ovl_path_type(dentry); | |
184 | ||
185 | if (!OVL_TYPE_UPPER(type)) | |
186 | ovl_path_lower(dentry, path); | |
187 | else | |
188 | ovl_path_upper(dentry, path); | |
189 | ||
190 | return type; | |
191 | } | |
192 | ||
193 | struct dentry *ovl_dentry_upper(struct dentry *dentry) | |
194 | { | |
09d8b586 | 195 | return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); |
bbb1e54d MS |
196 | } |
197 | ||
198 | struct dentry *ovl_dentry_lower(struct dentry *dentry) | |
199 | { | |
200 | struct ovl_entry *oe = dentry->d_fsdata; | |
201 | ||
09d8b586 | 202 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; |
bbb1e54d MS |
203 | } |
204 | ||
13464165 | 205 | const struct ovl_layer *ovl_layer_lower(struct dentry *dentry) |
da309e8c AG |
206 | { |
207 | struct ovl_entry *oe = dentry->d_fsdata; | |
208 | ||
209 | return oe->numlower ? oe->lowerstack[0].layer : NULL; | |
210 | } | |
211 | ||
647d253f VG |
212 | /* |
213 | * ovl_dentry_lower() could return either a data dentry or metacopy dentry | |
214 | * dependig on what is stored in lowerstack[0]. At times we need to find | |
215 | * lower dentry which has data (and not metacopy dentry). This helper | |
216 | * returns the lower data dentry. | |
217 | */ | |
218 | struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) | |
219 | { | |
220 | struct ovl_entry *oe = dentry->d_fsdata; | |
221 | ||
222 | return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL; | |
223 | } | |
224 | ||
bbb1e54d MS |
225 | struct dentry *ovl_dentry_real(struct dentry *dentry) |
226 | { | |
09d8b586 | 227 | return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); |
bbb1e54d MS |
228 | } |
229 | ||
1d88f183 MS |
230 | struct dentry *ovl_i_dentry_upper(struct inode *inode) |
231 | { | |
232 | return ovl_upperdentry_dereference(OVL_I(inode)); | |
233 | } | |
234 | ||
09d8b586 | 235 | struct inode *ovl_inode_upper(struct inode *inode) |
25b7713a | 236 | { |
1d88f183 | 237 | struct dentry *upperdentry = ovl_i_dentry_upper(inode); |
25b7713a | 238 | |
09d8b586 MS |
239 | return upperdentry ? d_inode(upperdentry) : NULL; |
240 | } | |
25b7713a | 241 | |
09d8b586 MS |
242 | struct inode *ovl_inode_lower(struct inode *inode) |
243 | { | |
244 | return OVL_I(inode)->lower; | |
245 | } | |
25b7713a | 246 | |
09d8b586 MS |
247 | struct inode *ovl_inode_real(struct inode *inode) |
248 | { | |
249 | return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); | |
25b7713a MS |
250 | } |
251 | ||
2664bd08 VG |
252 | /* Return inode which contains lower data. Do not return metacopy */ |
253 | struct inode *ovl_inode_lowerdata(struct inode *inode) | |
254 | { | |
255 | if (WARN_ON(!S_ISREG(inode->i_mode))) | |
256 | return NULL; | |
257 | ||
258 | return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode); | |
259 | } | |
09d8b586 | 260 | |
4823d49c VG |
261 | /* Return real inode which contains data. Does not return metacopy inode */ |
262 | struct inode *ovl_inode_realdata(struct inode *inode) | |
263 | { | |
264 | struct inode *upperinode; | |
265 | ||
266 | upperinode = ovl_inode_upper(inode); | |
267 | if (upperinode && ovl_has_upperdata(inode)) | |
268 | return upperinode; | |
269 | ||
270 | return ovl_inode_lowerdata(inode); | |
271 | } | |
272 | ||
4edb83bb | 273 | struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) |
bbb1e54d | 274 | { |
4edb83bb | 275 | return OVL_I(inode)->cache; |
bbb1e54d MS |
276 | } |
277 | ||
4edb83bb | 278 | void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) |
bbb1e54d | 279 | { |
4edb83bb | 280 | OVL_I(inode)->cache = cache; |
bbb1e54d MS |
281 | } |
282 | ||
c62520a8 AG |
283 | void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) |
284 | { | |
285 | set_bit(flag, &OVL_E(dentry)->flags); | |
286 | } | |
287 | ||
288 | void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) | |
289 | { | |
290 | clear_bit(flag, &OVL_E(dentry)->flags); | |
291 | } | |
292 | ||
293 | bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) | |
294 | { | |
295 | return test_bit(flag, &OVL_E(dentry)->flags); | |
296 | } | |
297 | ||
bbb1e54d MS |
298 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
299 | { | |
c62520a8 | 300 | return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); |
bbb1e54d MS |
301 | } |
302 | ||
303 | bool ovl_dentry_is_whiteout(struct dentry *dentry) | |
304 | { | |
305 | return !dentry->d_inode && ovl_dentry_is_opaque(dentry); | |
306 | } | |
307 | ||
5cf5b477 | 308 | void ovl_dentry_set_opaque(struct dentry *dentry) |
bbb1e54d | 309 | { |
c62520a8 | 310 | ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); |
bbb1e54d MS |
311 | } |
312 | ||
55acc661 | 313 | /* |
aa3ff3c1 AG |
314 | * For hard links and decoded file handles, it's possible for ovl_dentry_upper() |
315 | * to return positive, while there's no actual upper alias for the inode. | |
316 | * Copy up code needs to know about the existence of the upper alias, so it | |
317 | * can't use ovl_dentry_upper(). | |
55acc661 MS |
318 | */ |
319 | bool ovl_dentry_has_upper_alias(struct dentry *dentry) | |
320 | { | |
c62520a8 | 321 | return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); |
55acc661 MS |
322 | } |
323 | ||
324 | void ovl_dentry_set_upper_alias(struct dentry *dentry) | |
325 | { | |
c62520a8 | 326 | ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); |
55acc661 MS |
327 | } |
328 | ||
0c288874 VG |
329 | static bool ovl_should_check_upperdata(struct inode *inode) |
330 | { | |
331 | if (!S_ISREG(inode->i_mode)) | |
332 | return false; | |
333 | ||
334 | if (!ovl_inode_lower(inode)) | |
335 | return false; | |
336 | ||
337 | return true; | |
338 | } | |
339 | ||
340 | bool ovl_has_upperdata(struct inode *inode) | |
341 | { | |
342 | if (!ovl_should_check_upperdata(inode)) | |
343 | return true; | |
344 | ||
345 | if (!ovl_test_flag(OVL_UPPERDATA, inode)) | |
346 | return false; | |
347 | /* | |
348 | * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of | |
349 | * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure | |
350 | * if setting of OVL_UPPERDATA is visible, then effects of writes | |
351 | * before that are visible too. | |
352 | */ | |
353 | smp_rmb(); | |
354 | return true; | |
355 | } | |
356 | ||
357 | void ovl_set_upperdata(struct inode *inode) | |
358 | { | |
359 | /* | |
360 | * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure | |
361 | * if OVL_UPPERDATA flag is visible, then effects of write operations | |
362 | * before it are visible as well. | |
363 | */ | |
364 | smp_wmb(); | |
365 | ovl_set_flag(OVL_UPPERDATA, inode); | |
366 | } | |
367 | ||
368 | /* Caller should hold ovl_inode->lock */ | |
369 | bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) | |
370 | { | |
371 | if (!ovl_open_flags_need_copy_up(flags)) | |
372 | return false; | |
373 | ||
374 | return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); | |
375 | } | |
376 | ||
377 | bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) | |
378 | { | |
379 | if (!ovl_open_flags_need_copy_up(flags)) | |
380 | return false; | |
381 | ||
382 | return !ovl_has_upperdata(d_inode(dentry)); | |
383 | } | |
384 | ||
a6c60655 MS |
385 | bool ovl_redirect_dir(struct super_block *sb) |
386 | { | |
387 | struct ovl_fs *ofs = sb->s_fs_info; | |
388 | ||
21a22878 | 389 | return ofs->config.redirect_dir && !ofs->noxattr; |
a6c60655 MS |
390 | } |
391 | ||
392 | const char *ovl_dentry_get_redirect(struct dentry *dentry) | |
393 | { | |
cf31c463 | 394 | return OVL_I(d_inode(dentry))->redirect; |
a6c60655 MS |
395 | } |
396 | ||
397 | void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) | |
398 | { | |
cf31c463 | 399 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
a6c60655 | 400 | |
cf31c463 MS |
401 | kfree(oi->redirect); |
402 | oi->redirect = redirect; | |
a6c60655 MS |
403 | } |
404 | ||
09d8b586 | 405 | void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) |
bbb1e54d | 406 | { |
09d8b586 | 407 | struct inode *upperinode = d_inode(upperdentry); |
e6d2ebdd | 408 | |
09d8b586 MS |
409 | WARN_ON(OVL_I(inode)->__upperdentry); |
410 | ||
25b7713a | 411 | /* |
09d8b586 | 412 | * Make sure upperdentry is consistent before making it visible |
25b7713a MS |
413 | */ |
414 | smp_wmb(); | |
09d8b586 | 415 | OVL_I(inode)->__upperdentry = upperdentry; |
31747eda | 416 | if (inode_unhashed(inode)) { |
25b7713a | 417 | inode->i_private = upperinode; |
bbb1e54d | 418 | __insert_inode_hash(inode, (unsigned long) upperinode); |
25b7713a | 419 | } |
bbb1e54d MS |
420 | } |
421 | ||
d9854c87 | 422 | static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity) |
bbb1e54d | 423 | { |
04a01ac7 | 424 | struct inode *inode = d_inode(dentry); |
bbb1e54d | 425 | |
04a01ac7 | 426 | WARN_ON(!inode_is_locked(inode)); |
4edb83bb MS |
427 | /* |
428 | * Version is used by readdir code to keep cache consistent. For merge | |
429 | * dirs all changes need to be noted. For non-merge dirs, cache only | |
430 | * contains impure (ones which have been copied up and have origins) | |
431 | * entries, so only need to note changes to impure entries. | |
432 | */ | |
433 | if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity) | |
434 | OVL_I(inode)->version++; | |
bbb1e54d MS |
435 | } |
436 | ||
d9854c87 MS |
437 | void ovl_dir_modified(struct dentry *dentry, bool impurity) |
438 | { | |
439 | /* Copy mtime/ctime */ | |
440 | ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry)); | |
441 | ||
442 | ovl_dentry_version_inc(dentry, impurity); | |
443 | } | |
444 | ||
bbb1e54d MS |
445 | u64 ovl_dentry_version_get(struct dentry *dentry) |
446 | { | |
04a01ac7 | 447 | struct inode *inode = d_inode(dentry); |
bbb1e54d | 448 | |
04a01ac7 MS |
449 | WARN_ON(!inode_is_locked(inode)); |
450 | return OVL_I(inode)->version; | |
bbb1e54d MS |
451 | } |
452 | ||
453 | bool ovl_is_whiteout(struct dentry *dentry) | |
454 | { | |
455 | struct inode *inode = dentry->d_inode; | |
456 | ||
457 | return inode && IS_WHITEOUT(inode); | |
458 | } | |
459 | ||
460 | struct file *ovl_path_open(struct path *path, int flags) | |
461 | { | |
56230d95 MS |
462 | struct inode *inode = d_inode(path->dentry); |
463 | int err, acc_mode; | |
464 | ||
465 | if (flags & ~(O_ACCMODE | O_LARGEFILE)) | |
466 | BUG(); | |
467 | ||
468 | switch (flags & O_ACCMODE) { | |
469 | case O_RDONLY: | |
470 | acc_mode = MAY_READ; | |
471 | break; | |
472 | case O_WRONLY: | |
473 | acc_mode = MAY_WRITE; | |
474 | break; | |
475 | default: | |
476 | BUG(); | |
477 | } | |
478 | ||
479 | err = inode_permission(inode, acc_mode | MAY_OPEN); | |
480 | if (err) | |
481 | return ERR_PTR(err); | |
482 | ||
483 | /* O_NOATIME is an optimization, don't fail if not permitted */ | |
484 | if (inode_owner_or_capable(inode)) | |
485 | flags |= O_NOATIME; | |
486 | ||
487 | return dentry_open(path, flags, current_cred()); | |
bbb1e54d | 488 | } |
39d3d60a | 489 | |
0c288874 VG |
490 | /* Caller should hold ovl_inode->lock */ |
491 | static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) | |
492 | { | |
493 | bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; | |
494 | ||
495 | if (ovl_dentry_upper(dentry) && | |
496 | (ovl_dentry_has_upper_alias(dentry) || disconnected) && | |
497 | !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) | |
498 | return true; | |
499 | ||
500 | return false; | |
501 | } | |
502 | ||
503 | bool ovl_already_copied_up(struct dentry *dentry, int flags) | |
2002df85 VG |
504 | { |
505 | bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; | |
506 | ||
507 | /* | |
508 | * Check if copy-up has happened as well as for upper alias (in | |
509 | * case of hard links) is there. | |
510 | * | |
511 | * Both checks are lockless: | |
512 | * - false negatives: will recheck under oi->lock | |
513 | * - false positives: | |
514 | * + ovl_dentry_upper() uses memory barriers to ensure the | |
515 | * upper dentry is up-to-date | |
516 | * + ovl_dentry_has_upper_alias() relies on locking of | |
517 | * upper parent i_rwsem to prevent reordering copy-up | |
518 | * with rename. | |
519 | */ | |
520 | if (ovl_dentry_upper(dentry) && | |
0c288874 VG |
521 | (ovl_dentry_has_upper_alias(dentry) || disconnected) && |
522 | !ovl_dentry_needs_data_copy_up(dentry, flags)) | |
2002df85 VG |
523 | return true; |
524 | ||
525 | return false; | |
526 | } | |
527 | ||
0c288874 | 528 | int ovl_copy_up_start(struct dentry *dentry, int flags) |
39d3d60a | 529 | { |
1e92e307 | 530 | struct inode *inode = d_inode(dentry); |
39d3d60a AG |
531 | int err; |
532 | ||
531d3040 | 533 | err = ovl_inode_lock_interruptible(inode); |
0c288874 | 534 | if (!err && ovl_already_copied_up_locked(dentry, flags)) { |
a015dafc | 535 | err = 1; /* Already copied up */ |
1e92e307 | 536 | ovl_inode_unlock(inode); |
39d3d60a | 537 | } |
39d3d60a AG |
538 | |
539 | return err; | |
540 | } | |
541 | ||
542 | void ovl_copy_up_end(struct dentry *dentry) | |
543 | { | |
1e92e307 | 544 | ovl_inode_unlock(d_inode(dentry)); |
39d3d60a | 545 | } |
82b749b2 | 546 | |
610afc0b | 547 | bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry) |
b79e05aa AG |
548 | { |
549 | int res; | |
550 | ||
610afc0b | 551 | res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0); |
b79e05aa AG |
552 | |
553 | /* Zero size value means "copied up but origin unknown" */ | |
554 | if (res >= 0) | |
555 | return true; | |
556 | ||
557 | return false; | |
558 | } | |
559 | ||
610afc0b | 560 | bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry, |
43d193f8 | 561 | enum ovl_xattr ox) |
f3a15685 AG |
562 | { |
563 | int res; | |
564 | char val; | |
565 | ||
566 | if (!d_is_dir(dentry)) | |
567 | return false; | |
568 | ||
43d193f8 | 569 | res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1); |
f3a15685 AG |
570 | if (res == 1 && val == 'y') |
571 | return true; | |
572 | ||
573 | return false; | |
574 | } | |
575 | ||
43d193f8 MS |
576 | #define OVL_XATTR_OPAQUE_POSTFIX "opaque" |
577 | #define OVL_XATTR_REDIRECT_POSTFIX "redirect" | |
578 | #define OVL_XATTR_ORIGIN_POSTFIX "origin" | |
579 | #define OVL_XATTR_IMPURE_POSTFIX "impure" | |
580 | #define OVL_XATTR_NLINK_POSTFIX "nlink" | |
581 | #define OVL_XATTR_UPPER_POSTFIX "upper" | |
582 | #define OVL_XATTR_METACOPY_POSTFIX "metacopy" | |
583 | ||
584 | #define OVL_XATTR_TAB_ENTRY(x) \ | |
585 | [x] = OVL_XATTR_PREFIX x ## _POSTFIX | |
586 | ||
587 | const char *ovl_xattr_table[] = { | |
588 | OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE), | |
589 | OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT), | |
590 | OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN), | |
591 | OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE), | |
592 | OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK), | |
593 | OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER), | |
594 | OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY), | |
595 | }; | |
596 | ||
82b749b2 | 597 | int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, |
43d193f8 | 598 | enum ovl_xattr ox, const void *value, size_t size, |
82b749b2 AG |
599 | int xerr) |
600 | { | |
601 | int err; | |
602 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
603 | ||
604 | if (ofs->noxattr) | |
605 | return xerr; | |
606 | ||
43d193f8 | 607 | err = ovl_do_setxattr(ofs, upperdentry, ox, value, size); |
82b749b2 AG |
608 | |
609 | if (err == -EOPNOTSUPP) { | |
43d193f8 | 610 | pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox)); |
82b749b2 AG |
611 | ofs->noxattr = true; |
612 | return xerr; | |
613 | } | |
614 | ||
615 | return err; | |
616 | } | |
f3a15685 AG |
617 | |
618 | int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) | |
619 | { | |
620 | int err; | |
f3a15685 | 621 | |
13c72075 | 622 | if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) |
f3a15685 AG |
623 | return 0; |
624 | ||
625 | /* | |
626 | * Do not fail when upper doesn't support xattrs. | |
627 | * Upper inodes won't have origin nor redirect xattr anyway. | |
628 | */ | |
629 | err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE, | |
630 | "y", 1, 0); | |
631 | if (!err) | |
13c72075 | 632 | ovl_set_flag(OVL_IMPURE, d_inode(dentry)); |
f3a15685 AG |
633 | |
634 | return err; | |
635 | } | |
13c72075 MS |
636 | |
637 | void ovl_set_flag(unsigned long flag, struct inode *inode) | |
638 | { | |
639 | set_bit(flag, &OVL_I(inode)->flags); | |
640 | } | |
641 | ||
4edb83bb MS |
642 | void ovl_clear_flag(unsigned long flag, struct inode *inode) |
643 | { | |
644 | clear_bit(flag, &OVL_I(inode)->flags); | |
645 | } | |
646 | ||
13c72075 MS |
647 | bool ovl_test_flag(unsigned long flag, struct inode *inode) |
648 | { | |
649 | return test_bit(flag, &OVL_I(inode)->flags); | |
650 | } | |
ad0af710 AG |
651 | |
652 | /** | |
653 | * Caller must hold a reference to inode to prevent it from being freed while | |
654 | * it is marked inuse. | |
655 | */ | |
656 | bool ovl_inuse_trylock(struct dentry *dentry) | |
657 | { | |
658 | struct inode *inode = d_inode(dentry); | |
659 | bool locked = false; | |
660 | ||
661 | spin_lock(&inode->i_lock); | |
662 | if (!(inode->i_state & I_OVL_INUSE)) { | |
663 | inode->i_state |= I_OVL_INUSE; | |
664 | locked = true; | |
665 | } | |
666 | spin_unlock(&inode->i_lock); | |
667 | ||
668 | return locked; | |
669 | } | |
670 | ||
671 | void ovl_inuse_unlock(struct dentry *dentry) | |
672 | { | |
673 | if (dentry) { | |
674 | struct inode *inode = d_inode(dentry); | |
675 | ||
676 | spin_lock(&inode->i_lock); | |
677 | WARN_ON(!(inode->i_state & I_OVL_INUSE)); | |
678 | inode->i_state &= ~I_OVL_INUSE; | |
679 | spin_unlock(&inode->i_lock); | |
680 | } | |
681 | } | |
5f8415d6 | 682 | |
146d62e5 AG |
683 | bool ovl_is_inuse(struct dentry *dentry) |
684 | { | |
685 | struct inode *inode = d_inode(dentry); | |
686 | bool inuse; | |
687 | ||
688 | spin_lock(&inode->i_lock); | |
689 | inuse = (inode->i_state & I_OVL_INUSE); | |
690 | spin_unlock(&inode->i_lock); | |
691 | ||
692 | return inuse; | |
693 | } | |
694 | ||
24b33ee1 AG |
695 | /* |
696 | * Does this overlay dentry need to be indexed on copy up? | |
697 | */ | |
698 | bool ovl_need_index(struct dentry *dentry) | |
699 | { | |
700 | struct dentry *lower = ovl_dentry_lower(dentry); | |
701 | ||
702 | if (!lower || !ovl_indexdir(dentry->d_sb)) | |
703 | return false; | |
704 | ||
fbd2d207 | 705 | /* Index all files for NFS export and consistency verification */ |
016b720f | 706 | if (ovl_index_all(dentry->d_sb)) |
fbd2d207 AG |
707 | return true; |
708 | ||
24b33ee1 AG |
709 | /* Index only lower hardlinks on copy up */ |
710 | if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) | |
711 | return true; | |
712 | ||
713 | return false; | |
714 | } | |
715 | ||
9f4ec904 | 716 | /* Caller must hold OVL_I(inode)->lock */ |
caf70cb2 AG |
717 | static void ovl_cleanup_index(struct dentry *dentry) |
718 | { | |
e7dd0e71 AG |
719 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); |
720 | struct inode *dir = indexdir->d_inode; | |
caf70cb2 AG |
721 | struct dentry *lowerdentry = ovl_dentry_lower(dentry); |
722 | struct dentry *upperdentry = ovl_dentry_upper(dentry); | |
723 | struct dentry *index = NULL; | |
724 | struct inode *inode; | |
63e13252 | 725 | struct qstr name = { }; |
caf70cb2 AG |
726 | int err; |
727 | ||
728 | err = ovl_get_index_name(lowerdentry, &name); | |
729 | if (err) | |
730 | goto fail; | |
731 | ||
732 | inode = d_inode(upperdentry); | |
89a17556 | 733 | if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { |
1bd0a3ae | 734 | pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", |
caf70cb2 AG |
735 | upperdentry, inode->i_ino, inode->i_nlink); |
736 | /* | |
737 | * We either have a bug with persistent union nlink or a lower | |
738 | * hardlink was added while overlay is mounted. Adding a lower | |
739 | * hardlink and then unlinking all overlay hardlinks would drop | |
740 | * overlay nlink to zero before all upper inodes are unlinked. | |
741 | * As a safety measure, when that situation is detected, set | |
742 | * the overlay nlink to the index inode nlink minus one for the | |
743 | * index entry itself. | |
744 | */ | |
745 | set_nlink(d_inode(dentry), inode->i_nlink - 1); | |
746 | ovl_set_nlink_upper(dentry); | |
747 | goto out; | |
748 | } | |
749 | ||
750 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
e7dd0e71 | 751 | index = lookup_one_len(name.name, indexdir, name.len); |
caf70cb2 | 752 | err = PTR_ERR(index); |
e7dd0e71 | 753 | if (IS_ERR(index)) { |
9f4ec904 | 754 | index = NULL; |
e7dd0e71 AG |
755 | } else if (ovl_index_all(dentry->d_sb)) { |
756 | /* Whiteout orphan index to block future open by handle */ | |
c21c839b CX |
757 | err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb), |
758 | dir, index); | |
e7dd0e71 AG |
759 | } else { |
760 | /* Cleanup orphan index entries */ | |
761 | err = ovl_cleanup(dir, index); | |
762 | } | |
9f4ec904 | 763 | |
caf70cb2 AG |
764 | inode_unlock(dir); |
765 | if (err) | |
766 | goto fail; | |
767 | ||
768 | out: | |
63e13252 | 769 | kfree(name.name); |
caf70cb2 AG |
770 | dput(index); |
771 | return; | |
772 | ||
773 | fail: | |
1bd0a3ae | 774 | pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err); |
caf70cb2 AG |
775 | goto out; |
776 | } | |
777 | ||
5f8415d6 AG |
778 | /* |
779 | * Operations that change overlay inode and upper inode nlink need to be | |
780 | * synchronized with copy up for persistent nlink accounting. | |
781 | */ | |
0e32992f | 782 | int ovl_nlink_start(struct dentry *dentry) |
5f8415d6 | 783 | { |
1e92e307 | 784 | struct inode *inode = d_inode(dentry); |
5f8415d6 AG |
785 | const struct cred *old_cred; |
786 | int err; | |
787 | ||
1e92e307 | 788 | if (WARN_ON(!inode)) |
0e32992f | 789 | return -ENOENT; |
5f8415d6 AG |
790 | |
791 | /* | |
792 | * With inodes index is enabled, we store the union overlay nlink | |
24b33ee1 | 793 | * in an xattr on the index inode. When whiting out an indexed lower, |
5f8415d6 AG |
794 | * we need to decrement the overlay persistent nlink, but before the |
795 | * first copy up, we have no upper index inode to store the xattr. | |
796 | * | |
24b33ee1 | 797 | * As a workaround, before whiteout/rename over an indexed lower, |
5f8415d6 AG |
798 | * copy up to create the upper index. Creating the upper index will |
799 | * initialize the overlay nlink, so it could be dropped if unlink | |
800 | * or rename succeeds. | |
801 | * | |
802 | * TODO: implement metadata only index copy up when called with | |
803 | * ovl_copy_up_flags(dentry, O_PATH). | |
804 | */ | |
24b33ee1 | 805 | if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { |
5f8415d6 AG |
806 | err = ovl_copy_up(dentry); |
807 | if (err) | |
808 | return err; | |
809 | } | |
810 | ||
531d3040 | 811 | err = ovl_inode_lock_interruptible(inode); |
5f8415d6 AG |
812 | if (err) |
813 | return err; | |
814 | ||
1e92e307 | 815 | if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) |
5f8415d6 AG |
816 | goto out; |
817 | ||
818 | old_cred = ovl_override_creds(dentry->d_sb); | |
819 | /* | |
820 | * The overlay inode nlink should be incremented/decremented IFF the | |
821 | * upper operation succeeds, along with nlink change of upper inode. | |
822 | * Therefore, before link/unlink/rename, we store the union nlink | |
823 | * value relative to the upper inode nlink in an upper inode xattr. | |
824 | */ | |
825 | err = ovl_set_nlink_upper(dentry); | |
826 | revert_creds(old_cred); | |
827 | ||
828 | out: | |
829 | if (err) | |
1e92e307 | 830 | ovl_inode_unlock(inode); |
5f8415d6 AG |
831 | |
832 | return err; | |
833 | } | |
834 | ||
0e32992f | 835 | void ovl_nlink_end(struct dentry *dentry) |
5f8415d6 | 836 | { |
1e92e307 AG |
837 | struct inode *inode = d_inode(dentry); |
838 | ||
839 | if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { | |
0e32992f | 840 | const struct cred *old_cred; |
caf70cb2 | 841 | |
0e32992f AG |
842 | old_cred = ovl_override_creds(dentry->d_sb); |
843 | ovl_cleanup_index(dentry); | |
844 | revert_creds(old_cred); | |
caf70cb2 | 845 | } |
0e32992f | 846 | |
1e92e307 | 847 | ovl_inode_unlock(inode); |
5f8415d6 | 848 | } |
5820dc08 AG |
849 | |
850 | int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) | |
851 | { | |
852 | /* Workdir should not be the same as upperdir */ | |
853 | if (workdir == upperdir) | |
854 | goto err; | |
855 | ||
856 | /* Workdir should not be subdir of upperdir and vice versa */ | |
857 | if (lock_rename(workdir, upperdir) != NULL) | |
858 | goto err_unlock; | |
859 | ||
860 | return 0; | |
861 | ||
862 | err_unlock: | |
863 | unlock_rename(workdir, upperdir); | |
864 | err: | |
1bd0a3ae | 865 | pr_err("failed to lock workdir+upperdir\n"); |
5820dc08 AG |
866 | return -EIO; |
867 | } | |
9d3dfea3 VG |
868 | |
869 | /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ | |
610afc0b | 870 | int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry) |
9d3dfea3 VG |
871 | { |
872 | int res; | |
873 | ||
874 | /* Only regular files can have metacopy xattr */ | |
875 | if (!S_ISREG(d_inode(dentry)->i_mode)) | |
876 | return 0; | |
877 | ||
610afc0b | 878 | res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0); |
9d3dfea3 VG |
879 | if (res < 0) { |
880 | if (res == -ENODATA || res == -EOPNOTSUPP) | |
881 | return 0; | |
882 | goto out; | |
883 | } | |
884 | ||
885 | return 1; | |
886 | out: | |
1bd0a3ae | 887 | pr_warn_ratelimited("failed to get metacopy (%i)\n", res); |
9d3dfea3 VG |
888 | return res; |
889 | } | |
67d756c2 VG |
890 | |
891 | bool ovl_is_metacopy_dentry(struct dentry *dentry) | |
892 | { | |
893 | struct ovl_entry *oe = dentry->d_fsdata; | |
894 | ||
895 | if (!d_is_reg(dentry)) | |
896 | return false; | |
897 | ||
898 | if (ovl_dentry_upper(dentry)) { | |
899 | if (!ovl_has_upperdata(d_inode(dentry))) | |
900 | return true; | |
901 | return false; | |
902 | } | |
903 | ||
904 | return (oe->numlower > 1); | |
905 | } | |
0a2d0d3f | 906 | |
610afc0b MS |
907 | char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry, |
908 | int padding) | |
993a0b2a VG |
909 | { |
910 | int res; | |
911 | char *s, *next, *buf = NULL; | |
912 | ||
610afc0b | 913 | res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0); |
92f0d6c9 | 914 | if (res == -ENODATA || res == -EOPNOTSUPP) |
993a0b2a | 915 | return NULL; |
0a2d0d3f | 916 | if (res < 0) |
92f0d6c9 MS |
917 | goto fail; |
918 | if (res == 0) | |
919 | goto invalid; | |
920 | ||
921 | buf = kzalloc(res + padding + 1, GFP_KERNEL); | |
922 | if (!buf) | |
923 | return ERR_PTR(-ENOMEM); | |
924 | ||
610afc0b | 925 | res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res); |
92f0d6c9 MS |
926 | if (res < 0) |
927 | goto fail; | |
0a2d0d3f VG |
928 | if (res == 0) |
929 | goto invalid; | |
930 | ||
931 | if (buf[0] == '/') { | |
932 | for (s = buf; *s++ == '/'; s = next) { | |
933 | next = strchrnul(s, '/'); | |
934 | if (s == next) | |
935 | goto invalid; | |
936 | } | |
937 | } else { | |
938 | if (strchr(buf, '/') != NULL) | |
939 | goto invalid; | |
940 | } | |
941 | ||
942 | return buf; | |
0a2d0d3f | 943 | invalid: |
1bd0a3ae | 944 | pr_warn_ratelimited("invalid redirect (%s)\n", buf); |
0a2d0d3f | 945 | res = -EINVAL; |
92f0d6c9 MS |
946 | goto err_free; |
947 | fail: | |
948 | pr_warn_ratelimited("failed to get redirect (%i)\n", res); | |
949 | err_free: | |
993a0b2a VG |
950 | kfree(buf); |
951 | return ERR_PTR(res); | |
0a2d0d3f | 952 | } |