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