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