]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/overlayfs/util.c
ovl: copy up of disconnected dentries
[mirror_ubuntu-hirsute-kernel.git] / fs / overlayfs / util.c
CommitLineData
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 14#include <linux/xattr.h>
02bcd157
AG
15#include <linux/exportfs.h>
16#include <linux/uuid.h>
caf70cb2
AG
17#include <linux/namei.h>
18#include <linux/ratelimit.h>
bbb1e54d 19#include "overlayfs.h"
bbb1e54d
MS
20
21int ovl_want_write(struct dentry *dentry)
22{
23 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
24 return mnt_want_write(ofs->upper_mnt);
25}
26
27void ovl_drop_write(struct dentry *dentry)
28{
29 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
30 mnt_drop_write(ofs->upper_mnt);
31}
32
33struct dentry *ovl_workdir(struct dentry *dentry)
34{
35 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
36 return ofs->workdir;
37}
38
39const struct cred *ovl_override_creds(struct super_block *sb)
40{
41 struct ovl_fs *ofs = sb->s_fs_info;
42
43 return override_creds(ofs->creator_cred);
44}
45
7bcd74b9
AG
46struct super_block *ovl_same_sb(struct super_block *sb)
47{
48 struct ovl_fs *ofs = sb->s_fs_info;
49
50 return ofs->same_sb;
51}
52
02bcd157
AG
53bool ovl_can_decode_fh(struct super_block *sb)
54{
55 return (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
56 !uuid_is_null(&sb->s_uuid));
57}
58
59struct 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 */
67bool 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 */
75bool 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
82struct 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
93bool ovl_dentry_remote(struct dentry *dentry)
94{
95 return dentry->d_flags &
96 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
97 DCACHE_OP_REAL);
98}
99
100bool ovl_dentry_weird(struct dentry *dentry)
101{
102 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
103 DCACHE_MANAGE_TRANSIT |
104 DCACHE_OP_HASH |
105 DCACHE_OP_COMPARE);
106}
107
108enum ovl_path_type ovl_path_type(struct dentry *dentry)
109{
110 struct ovl_entry *oe = dentry->d_fsdata;
111 enum ovl_path_type type = 0;
112
09d8b586 113 if (ovl_dentry_upper(dentry)) {
bbb1e54d
MS
114 type = __OVL_PATH_UPPER;
115
116 /*
59548503 117 * Non-dir dentry can hold lower dentry of its copy up origin.
bbb1e54d 118 */
59548503
AG
119 if (oe->numlower) {
120 type |= __OVL_PATH_ORIGIN;
121 if (d_is_dir(dentry))
122 type |= __OVL_PATH_MERGE;
123 }
bbb1e54d
MS
124 } else {
125 if (oe->numlower > 1)
126 type |= __OVL_PATH_MERGE;
127 }
128 return type;
129}
130
131void ovl_path_upper(struct dentry *dentry, struct path *path)
132{
133 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
bbb1e54d
MS
134
135 path->mnt = ofs->upper_mnt;
09d8b586 136 path->dentry = ovl_dentry_upper(dentry);
bbb1e54d
MS
137}
138
139void ovl_path_lower(struct dentry *dentry, struct path *path)
140{
141 struct ovl_entry *oe = dentry->d_fsdata;
142
b9343632
CR
143 if (oe->numlower) {
144 path->mnt = oe->lowerstack[0].layer->mnt;
145 path->dentry = oe->lowerstack[0].dentry;
146 } else {
147 *path = (struct path) { };
148 }
bbb1e54d
MS
149}
150
151enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
152{
153 enum ovl_path_type type = ovl_path_type(dentry);
154
155 if (!OVL_TYPE_UPPER(type))
156 ovl_path_lower(dentry, path);
157 else
158 ovl_path_upper(dentry, path);
159
160 return type;
161}
162
163struct dentry *ovl_dentry_upper(struct dentry *dentry)
164{
09d8b586 165 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
bbb1e54d
MS
166}
167
168struct dentry *ovl_dentry_lower(struct dentry *dentry)
169{
170 struct ovl_entry *oe = dentry->d_fsdata;
171
09d8b586 172 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
bbb1e54d
MS
173}
174
175struct dentry *ovl_dentry_real(struct dentry *dentry)
176{
09d8b586 177 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
bbb1e54d
MS
178}
179
1d88f183
MS
180struct dentry *ovl_i_dentry_upper(struct inode *inode)
181{
182 return ovl_upperdentry_dereference(OVL_I(inode));
183}
184
09d8b586 185struct inode *ovl_inode_upper(struct inode *inode)
25b7713a 186{
1d88f183 187 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
25b7713a 188
09d8b586
MS
189 return upperdentry ? d_inode(upperdentry) : NULL;
190}
25b7713a 191
09d8b586
MS
192struct inode *ovl_inode_lower(struct inode *inode)
193{
194 return OVL_I(inode)->lower;
195}
25b7713a 196
09d8b586
MS
197struct inode *ovl_inode_real(struct inode *inode)
198{
199 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
25b7713a
MS
200}
201
09d8b586 202
4edb83bb 203struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
bbb1e54d 204{
4edb83bb 205 return OVL_I(inode)->cache;
bbb1e54d
MS
206}
207
4edb83bb 208void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
bbb1e54d 209{
4edb83bb 210 OVL_I(inode)->cache = cache;
bbb1e54d
MS
211}
212
213bool ovl_dentry_is_opaque(struct dentry *dentry)
214{
215 struct ovl_entry *oe = dentry->d_fsdata;
216 return oe->opaque;
217}
218
219bool ovl_dentry_is_whiteout(struct dentry *dentry)
220{
221 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
222}
223
5cf5b477 224void ovl_dentry_set_opaque(struct dentry *dentry)
bbb1e54d
MS
225{
226 struct ovl_entry *oe = dentry->d_fsdata;
5cf5b477
MS
227
228 oe->opaque = true;
bbb1e54d
MS
229}
230
55acc661 231/*
aa3ff3c1
AG
232 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
233 * to return positive, while there's no actual upper alias for the inode.
234 * Copy up code needs to know about the existence of the upper alias, so it
235 * can't use ovl_dentry_upper().
55acc661
MS
236 */
237bool ovl_dentry_has_upper_alias(struct dentry *dentry)
238{
239 struct ovl_entry *oe = dentry->d_fsdata;
240
241 return oe->has_upper;
242}
243
244void ovl_dentry_set_upper_alias(struct dentry *dentry)
245{
246 struct ovl_entry *oe = dentry->d_fsdata;
247
248 oe->has_upper = true;
249}
250
a6c60655
MS
251bool ovl_redirect_dir(struct super_block *sb)
252{
253 struct ovl_fs *ofs = sb->s_fs_info;
254
21a22878 255 return ofs->config.redirect_dir && !ofs->noxattr;
a6c60655
MS
256}
257
258const char *ovl_dentry_get_redirect(struct dentry *dentry)
259{
cf31c463 260 return OVL_I(d_inode(dentry))->redirect;
a6c60655
MS
261}
262
263void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
264{
cf31c463 265 struct ovl_inode *oi = OVL_I(d_inode(dentry));
a6c60655 266
cf31c463
MS
267 kfree(oi->redirect);
268 oi->redirect = redirect;
a6c60655
MS
269}
270
09d8b586
MS
271void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
272 struct dentry *lowerdentry)
bbb1e54d 273{
09d8b586
MS
274 if (upperdentry)
275 OVL_I(inode)->__upperdentry = upperdentry;
276 if (lowerdentry)
31747eda 277 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
bbb1e54d 278
09d8b586 279 ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
bbb1e54d
MS
280}
281
09d8b586 282void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
bbb1e54d 283{
09d8b586 284 struct inode *upperinode = d_inode(upperdentry);
e6d2ebdd 285
09d8b586
MS
286 WARN_ON(OVL_I(inode)->__upperdentry);
287
25b7713a 288 /*
09d8b586 289 * Make sure upperdentry is consistent before making it visible
25b7713a
MS
290 */
291 smp_wmb();
09d8b586 292 OVL_I(inode)->__upperdentry = upperdentry;
31747eda 293 if (inode_unhashed(inode)) {
25b7713a 294 inode->i_private = upperinode;
bbb1e54d 295 __insert_inode_hash(inode, (unsigned long) upperinode);
25b7713a 296 }
bbb1e54d
MS
297}
298
4edb83bb 299void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
bbb1e54d 300{
04a01ac7 301 struct inode *inode = d_inode(dentry);
bbb1e54d 302
04a01ac7 303 WARN_ON(!inode_is_locked(inode));
4edb83bb
MS
304 /*
305 * Version is used by readdir code to keep cache consistent. For merge
306 * dirs all changes need to be noted. For non-merge dirs, cache only
307 * contains impure (ones which have been copied up and have origins)
308 * entries, so only need to note changes to impure entries.
309 */
310 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
311 OVL_I(inode)->version++;
bbb1e54d
MS
312}
313
314u64 ovl_dentry_version_get(struct dentry *dentry)
315{
04a01ac7 316 struct inode *inode = d_inode(dentry);
bbb1e54d 317
04a01ac7
MS
318 WARN_ON(!inode_is_locked(inode));
319 return OVL_I(inode)->version;
bbb1e54d
MS
320}
321
322bool ovl_is_whiteout(struct dentry *dentry)
323{
324 struct inode *inode = dentry->d_inode;
325
326 return inode && IS_WHITEOUT(inode);
327}
328
329struct file *ovl_path_open(struct path *path, int flags)
330{
331 return dentry_open(path, flags | O_NOATIME, current_cred());
332}
39d3d60a
AG
333
334int ovl_copy_up_start(struct dentry *dentry)
335{
a015dafc 336 struct ovl_inode *oi = OVL_I(d_inode(dentry));
39d3d60a
AG
337 int err;
338
a015dafc 339 err = mutex_lock_interruptible(&oi->lock);
59be0971 340 if (!err && ovl_dentry_has_upper_alias(dentry)) {
a015dafc
AG
341 err = 1; /* Already copied up */
342 mutex_unlock(&oi->lock);
39d3d60a 343 }
39d3d60a
AG
344
345 return err;
346}
347
348void ovl_copy_up_end(struct dentry *dentry)
349{
a015dafc 350 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
39d3d60a 351}
82b749b2 352
b79e05aa
AG
353bool ovl_check_origin_xattr(struct dentry *dentry)
354{
355 int res;
356
357 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
358
359 /* Zero size value means "copied up but origin unknown" */
360 if (res >= 0)
361 return true;
362
363 return false;
364}
365
f3a15685
AG
366bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
367{
368 int res;
369 char val;
370
371 if (!d_is_dir(dentry))
372 return false;
373
374 res = vfs_getxattr(dentry, name, &val, 1);
375 if (res == 1 && val == 'y')
376 return true;
377
378 return false;
379}
380
82b749b2
AG
381int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
382 const char *name, const void *value, size_t size,
383 int xerr)
384{
385 int err;
386 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
387
388 if (ofs->noxattr)
389 return xerr;
390
391 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
392
393 if (err == -EOPNOTSUPP) {
394 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
395 ofs->noxattr = true;
396 return xerr;
397 }
398
399 return err;
400}
f3a15685
AG
401
402int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
403{
404 int err;
f3a15685 405
13c72075 406 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
f3a15685
AG
407 return 0;
408
409 /*
410 * Do not fail when upper doesn't support xattrs.
411 * Upper inodes won't have origin nor redirect xattr anyway.
412 */
413 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
414 "y", 1, 0);
415 if (!err)
13c72075 416 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
f3a15685
AG
417
418 return err;
419}
13c72075
MS
420
421void ovl_set_flag(unsigned long flag, struct inode *inode)
422{
423 set_bit(flag, &OVL_I(inode)->flags);
424}
425
4edb83bb
MS
426void ovl_clear_flag(unsigned long flag, struct inode *inode)
427{
428 clear_bit(flag, &OVL_I(inode)->flags);
429}
430
13c72075
MS
431bool ovl_test_flag(unsigned long flag, struct inode *inode)
432{
433 return test_bit(flag, &OVL_I(inode)->flags);
434}
ad0af710
AG
435
436/**
437 * Caller must hold a reference to inode to prevent it from being freed while
438 * it is marked inuse.
439 */
440bool ovl_inuse_trylock(struct dentry *dentry)
441{
442 struct inode *inode = d_inode(dentry);
443 bool locked = false;
444
445 spin_lock(&inode->i_lock);
446 if (!(inode->i_state & I_OVL_INUSE)) {
447 inode->i_state |= I_OVL_INUSE;
448 locked = true;
449 }
450 spin_unlock(&inode->i_lock);
451
452 return locked;
453}
454
455void ovl_inuse_unlock(struct dentry *dentry)
456{
457 if (dentry) {
458 struct inode *inode = d_inode(dentry);
459
460 spin_lock(&inode->i_lock);
461 WARN_ON(!(inode->i_state & I_OVL_INUSE));
462 inode->i_state &= ~I_OVL_INUSE;
463 spin_unlock(&inode->i_lock);
464 }
465}
5f8415d6 466
24b33ee1
AG
467/*
468 * Does this overlay dentry need to be indexed on copy up?
469 */
470bool ovl_need_index(struct dentry *dentry)
471{
472 struct dentry *lower = ovl_dentry_lower(dentry);
473
474 if (!lower || !ovl_indexdir(dentry->d_sb))
475 return false;
476
fbd2d207 477 /* Index all files for NFS export and consistency verification */
016b720f 478 if (ovl_index_all(dentry->d_sb))
fbd2d207
AG
479 return true;
480
24b33ee1
AG
481 /* Index only lower hardlinks on copy up */
482 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
483 return true;
484
485 return false;
486}
487
9f4ec904 488/* Caller must hold OVL_I(inode)->lock */
caf70cb2
AG
489static void ovl_cleanup_index(struct dentry *dentry)
490{
e7dd0e71
AG
491 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
492 struct inode *dir = indexdir->d_inode;
caf70cb2
AG
493 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
494 struct dentry *upperdentry = ovl_dentry_upper(dentry);
495 struct dentry *index = NULL;
496 struct inode *inode;
497 struct qstr name;
498 int err;
499
500 err = ovl_get_index_name(lowerdentry, &name);
501 if (err)
502 goto fail;
503
504 inode = d_inode(upperdentry);
89a17556 505 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
caf70cb2
AG
506 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
507 upperdentry, inode->i_ino, inode->i_nlink);
508 /*
509 * We either have a bug with persistent union nlink or a lower
510 * hardlink was added while overlay is mounted. Adding a lower
511 * hardlink and then unlinking all overlay hardlinks would drop
512 * overlay nlink to zero before all upper inodes are unlinked.
513 * As a safety measure, when that situation is detected, set
514 * the overlay nlink to the index inode nlink minus one for the
515 * index entry itself.
516 */
517 set_nlink(d_inode(dentry), inode->i_nlink - 1);
518 ovl_set_nlink_upper(dentry);
519 goto out;
520 }
521
522 inode_lock_nested(dir, I_MUTEX_PARENT);
e7dd0e71 523 index = lookup_one_len(name.name, indexdir, name.len);
caf70cb2 524 err = PTR_ERR(index);
e7dd0e71 525 if (IS_ERR(index)) {
9f4ec904 526 index = NULL;
e7dd0e71
AG
527 } else if (ovl_index_all(dentry->d_sb)) {
528 /* Whiteout orphan index to block future open by handle */
529 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
530 } else {
531 /* Cleanup orphan index entries */
532 err = ovl_cleanup(dir, index);
533 }
9f4ec904 534
caf70cb2
AG
535 inode_unlock(dir);
536 if (err)
537 goto fail;
538
539out:
540 dput(index);
541 return;
542
543fail:
544 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
545 goto out;
546}
547
5f8415d6
AG
548/*
549 * Operations that change overlay inode and upper inode nlink need to be
550 * synchronized with copy up for persistent nlink accounting.
551 */
552int ovl_nlink_start(struct dentry *dentry, bool *locked)
553{
554 struct ovl_inode *oi = OVL_I(d_inode(dentry));
555 const struct cred *old_cred;
556 int err;
557
89a17556 558 if (!d_inode(dentry))
5f8415d6
AG
559 return 0;
560
561 /*
562 * With inodes index is enabled, we store the union overlay nlink
24b33ee1 563 * in an xattr on the index inode. When whiting out an indexed lower,
5f8415d6
AG
564 * we need to decrement the overlay persistent nlink, but before the
565 * first copy up, we have no upper index inode to store the xattr.
566 *
24b33ee1 567 * As a workaround, before whiteout/rename over an indexed lower,
5f8415d6
AG
568 * copy up to create the upper index. Creating the upper index will
569 * initialize the overlay nlink, so it could be dropped if unlink
570 * or rename succeeds.
571 *
572 * TODO: implement metadata only index copy up when called with
573 * ovl_copy_up_flags(dentry, O_PATH).
574 */
24b33ee1 575 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
5f8415d6
AG
576 err = ovl_copy_up(dentry);
577 if (err)
578 return err;
579 }
580
581 err = mutex_lock_interruptible(&oi->lock);
582 if (err)
583 return err;
584
89a17556 585 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
5f8415d6
AG
586 goto out;
587
588 old_cred = ovl_override_creds(dentry->d_sb);
589 /*
590 * The overlay inode nlink should be incremented/decremented IFF the
591 * upper operation succeeds, along with nlink change of upper inode.
592 * Therefore, before link/unlink/rename, we store the union nlink
593 * value relative to the upper inode nlink in an upper inode xattr.
594 */
595 err = ovl_set_nlink_upper(dentry);
596 revert_creds(old_cred);
597
598out:
599 if (err)
600 mutex_unlock(&oi->lock);
601 else
602 *locked = true;
603
604 return err;
605}
606
607void ovl_nlink_end(struct dentry *dentry, bool locked)
608{
caf70cb2
AG
609 if (locked) {
610 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
611 d_inode(dentry)->i_nlink == 0) {
612 const struct cred *old_cred;
613
614 old_cred = ovl_override_creds(dentry->d_sb);
615 ovl_cleanup_index(dentry);
616 revert_creds(old_cred);
617 }
618
5f8415d6 619 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
caf70cb2 620 }
5f8415d6 621}
5820dc08
AG
622
623int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
624{
625 /* Workdir should not be the same as upperdir */
626 if (workdir == upperdir)
627 goto err;
628
629 /* Workdir should not be subdir of upperdir and vice versa */
630 if (lock_rename(workdir, upperdir) != NULL)
631 goto err_unlock;
632
633 return 0;
634
635err_unlock:
636 unlock_rename(workdir, upperdir);
637err:
638 pr_err("overlayfs: failed to lock workdir+upperdir\n");
639 return -EIO;
640}