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