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