1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 Novell Inc.
8 #include <linux/slab.h>
9 #include <linux/cred.h>
10 #include <linux/xattr.h>
11 #include <linux/posix_acl.h>
12 #include <linux/ratelimit.h>
13 #include "overlayfs.h"
16 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
19 bool full_copy_up = false;
20 struct dentry *upperdentry;
21 const struct cred *old_cred;
23 err = setattr_prepare(dentry, attr);
27 err = ovl_want_write(dentry);
31 if (attr->ia_valid & ATTR_SIZE) {
32 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
35 if (atomic_read(&realinode->i_writecount) < 0)
38 /* Truncate should trigger data copy up as well */
43 err = ovl_copy_up(dentry);
45 err = ovl_copy_up_with_data(dentry);
47 struct inode *winode = NULL;
49 upperdentry = ovl_dentry_upper(dentry);
51 if (attr->ia_valid & ATTR_SIZE) {
52 winode = d_inode(upperdentry);
53 err = get_write_access(winode);
58 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
59 attr->ia_valid &= ~ATTR_MODE;
61 inode_lock(upperdentry->d_inode);
62 old_cred = ovl_override_creds(dentry->d_sb);
63 err = notify_change(upperdentry, attr, NULL);
64 revert_creds(old_cred);
66 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
67 inode_unlock(upperdentry->d_inode);
70 put_write_access(winode);
73 ovl_drop_write(dentry);
78 static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat,
79 struct ovl_layer *lower_layer)
81 bool samefs = ovl_same_sb(dentry->d_sb);
82 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
86 * When all layers are on the same fs, all real inode
87 * number are unique, so we use the overlay st_dev,
88 * which is friendly to du -x.
90 stat->dev = dentry->d_sb->s_dev;
92 } else if (xinobits) {
93 unsigned int shift = 64 - xinobits;
95 * All inode numbers of underlying fs should not be using the
96 * high xinobits, so we use high xinobits to partition the
97 * overlay st_ino address space. The high bits holds the fsid
98 * (upper fsid is 0). This way overlay inode numbers are unique
99 * and all inodes use overlay st_dev. Inode numbers are also
100 * persistent for a given layer configuration.
102 if (stat->ino >> shift) {
103 pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
104 dentry, stat->ino, xinobits);
107 stat->ino |= ((u64)lower_layer->fsid) << shift;
109 stat->dev = dentry->d_sb->s_dev;
114 /* The inode could not be mapped to a unified st_ino address space */
115 if (S_ISDIR(dentry->d_inode->i_mode)) {
117 * Always use the overlay st_dev for directories, so 'find
118 * -xdev' will scan the entire overlay mount and won't cross the
119 * overlay mount boundaries.
121 * If not all layers are on the same fs the pair {real st_ino;
122 * overlay st_dev} is not unique, so use the non persistent
123 * overlay st_ino for directories.
125 stat->dev = dentry->d_sb->s_dev;
126 stat->ino = dentry->d_inode->i_ino;
127 } else if (lower_layer && lower_layer->fsid) {
129 * For non-samefs setup, if we cannot map all layers st_ino
130 * to a unified address space, we need to make sure that st_dev
131 * is unique per lower fs. Upper layer uses real st_dev and
132 * lower layers use the unique anonymous bdev assigned to the
135 stat->dev = lower_layer->fs->pseudo_dev;
141 int ovl_getattr(const struct path *path, struct kstat *stat,
142 u32 request_mask, unsigned int flags)
144 struct dentry *dentry = path->dentry;
145 enum ovl_path_type type;
146 struct path realpath;
147 const struct cred *old_cred;
148 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
149 bool samefs = ovl_same_sb(dentry->d_sb);
150 struct ovl_layer *lower_layer = NULL;
152 bool metacopy_blocks = false;
154 metacopy_blocks = ovl_is_metacopy_dentry(dentry);
156 type = ovl_path_real(dentry, &realpath);
157 old_cred = ovl_override_creds(dentry->d_sb);
158 err = vfs_getattr(&realpath, stat, request_mask, flags);
163 * For non-dir or same fs, we use st_ino of the copy up origin.
164 * This guaranties constant st_dev/st_ino across copy up.
165 * With xino feature and non-samefs, we use st_ino of the copy up
166 * origin masked with high bits that represent the layer id.
168 * If lower filesystem supports NFS file handles, this also guaranties
169 * persistent st_ino across mount cycle.
171 if (!is_dir || samefs || ovl_xino_bits(dentry->d_sb)) {
172 if (!OVL_TYPE_UPPER(type)) {
173 lower_layer = ovl_layer_lower(dentry);
174 } else if (OVL_TYPE_ORIGIN(type)) {
175 struct kstat lowerstat;
176 u32 lowermask = STATX_INO | STATX_BLOCKS |
177 (!is_dir ? STATX_NLINK : 0);
179 ovl_path_lower(dentry, &realpath);
180 err = vfs_getattr(&realpath, &lowerstat,
186 * Lower hardlinks may be broken on copy up to different
187 * upper files, so we cannot use the lower origin st_ino
188 * for those different files, even for the same fs case.
190 * Similarly, several redirected dirs can point to the
191 * same dir on a lower layer. With the "verify_lower"
192 * feature, we do not use the lower origin st_ino, if
193 * we haven't verified that this redirect is unique.
195 * With inodes index enabled, it is safe to use st_ino
196 * of an indexed origin. The index validates that the
197 * upper hardlink is not broken and that a redirected
198 * dir is the only redirect to that origin.
200 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
201 (!ovl_verify_lower(dentry->d_sb) &&
202 (is_dir || lowerstat.nlink == 1))) {
203 lower_layer = ovl_layer_lower(dentry);
205 * Cannot use origin st_dev;st_ino because
206 * origin inode content may differ from overlay
209 if (samefs || lower_layer->fsid)
210 stat->ino = lowerstat.ino;
214 * If we are querying a metacopy dentry and lower
215 * dentry is data dentry, then use the blocks we
216 * queried just now. We don't have to do additional
217 * vfs_getattr(). If lower itself is metacopy, then
218 * additional vfs_getattr() is unavoidable.
220 if (metacopy_blocks &&
221 realpath.dentry == ovl_dentry_lowerdata(dentry)) {
222 stat->blocks = lowerstat.blocks;
223 metacopy_blocks = false;
227 if (metacopy_blocks) {
229 * If lower is not same as lowerdata or if there was
230 * no origin on upper, we can end up here.
232 struct kstat lowerdatastat;
233 u32 lowermask = STATX_BLOCKS;
235 ovl_path_lowerdata(dentry, &realpath);
236 err = vfs_getattr(&realpath, &lowerdatastat,
240 stat->blocks = lowerdatastat.blocks;
244 err = ovl_map_dev_ino(dentry, stat, lower_layer);
249 * It's probably not worth it to count subdirs to get the
250 * correct link count. nlink=1 seems to pacify 'find' and
253 if (is_dir && OVL_TYPE_MERGE(type))
257 * Return the overlay inode nlinks for indexed upper inodes.
258 * Overlay inode nlink counts the union of the upper hardlinks
259 * and non-covered lower hardlinks. It does not include the upper
262 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
263 stat->nlink = dentry->d_inode->i_nlink;
266 revert_creds(old_cred);
271 int ovl_permission(struct inode *inode, int mask)
273 struct inode *upperinode = ovl_inode_upper(inode);
274 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
275 const struct cred *old_cred;
278 /* Careful in RCU walk mode */
280 WARN_ON(!(mask & MAY_NOT_BLOCK));
285 * Check overlay inode with the creds of task and underlying inode
286 * with creds of mounter
288 err = generic_permission(inode, mask);
292 old_cred = ovl_override_creds(inode->i_sb);
294 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
295 mask &= ~(MAY_WRITE | MAY_APPEND);
296 /* Make sure mounter can read file for copy up later */
299 err = inode_permission(realinode, mask);
300 revert_creds(old_cred);
305 static const char *ovl_get_link(struct dentry *dentry,
307 struct delayed_call *done)
309 const struct cred *old_cred;
313 return ERR_PTR(-ECHILD);
315 old_cred = ovl_override_creds(dentry->d_sb);
316 p = vfs_get_link(ovl_dentry_real(dentry), done);
317 revert_creds(old_cred);
321 bool ovl_is_private_xattr(const char *name)
323 return strncmp(name, OVL_XATTR_PREFIX,
324 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
327 int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
328 const void *value, size_t size, int flags)
331 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
332 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
333 const struct cred *old_cred;
335 err = ovl_want_write(dentry);
339 if (!value && !upperdentry) {
340 old_cred = ovl_override_creds(dentry->d_sb);
341 err = vfs_getxattr(realdentry, name, NULL, 0);
342 revert_creds(old_cred);
348 err = ovl_copy_up(dentry);
352 realdentry = ovl_dentry_upper(dentry);
355 old_cred = ovl_override_creds(dentry->d_sb);
357 err = vfs_setxattr(realdentry, name, value, size, flags);
359 WARN_ON(flags != XATTR_REPLACE);
360 err = vfs_removexattr(realdentry, name);
362 revert_creds(old_cred);
365 ovl_copyattr(d_inode(realdentry), inode);
368 ovl_drop_write(dentry);
373 int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
374 void *value, size_t size)
377 const struct cred *old_cred;
378 struct dentry *realdentry =
379 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
381 old_cred = ovl_override_creds(dentry->d_sb);
382 res = vfs_getxattr(realdentry, name, value, size);
383 revert_creds(old_cred);
387 static bool ovl_can_list(const char *s)
389 /* List all non-trusted xatts */
390 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
393 /* Never list trusted.overlay, list other trusted for superuser only */
394 return !ovl_is_private_xattr(s) &&
395 ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
398 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
400 struct dentry *realdentry = ovl_dentry_real(dentry);
404 const struct cred *old_cred;
406 old_cred = ovl_override_creds(dentry->d_sb);
407 res = vfs_listxattr(realdentry, list, size);
408 revert_creds(old_cred);
409 if (res <= 0 || size == 0)
412 /* filter out private xattrs */
413 for (s = list, len = res; len;) {
414 size_t slen = strnlen(s, len) + 1;
416 /* underlying fs providing us with an broken xattr list? */
417 if (WARN_ON(slen > len))
421 if (!ovl_can_list(s)) {
423 memmove(s, s + slen, len);
432 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
434 struct inode *realinode = ovl_inode_real(inode);
435 const struct cred *old_cred;
436 struct posix_acl *acl;
438 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
441 old_cred = ovl_override_creds(inode->i_sb);
442 acl = get_acl(realinode, type);
443 revert_creds(old_cred);
448 int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
450 if (flags & S_ATIME) {
451 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
452 struct path upperpath = {
453 .mnt = ofs->upper_mnt,
454 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
457 if (upperpath.dentry) {
458 touch_atime(&upperpath);
459 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
465 static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
469 struct inode *realinode = ovl_inode_real(inode);
470 const struct cred *old_cred;
472 if (!realinode->i_op->fiemap)
475 old_cred = ovl_override_creds(inode->i_sb);
477 if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
478 filemap_write_and_wait(realinode->i_mapping);
480 err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
481 revert_creds(old_cred);
486 static const struct inode_operations ovl_file_inode_operations = {
487 .setattr = ovl_setattr,
488 .permission = ovl_permission,
489 .getattr = ovl_getattr,
490 .listxattr = ovl_listxattr,
491 .get_acl = ovl_get_acl,
492 .update_time = ovl_update_time,
493 .fiemap = ovl_fiemap,
496 static const struct inode_operations ovl_symlink_inode_operations = {
497 .setattr = ovl_setattr,
498 .get_link = ovl_get_link,
499 .getattr = ovl_getattr,
500 .listxattr = ovl_listxattr,
501 .update_time = ovl_update_time,
504 static const struct inode_operations ovl_special_inode_operations = {
505 .setattr = ovl_setattr,
506 .permission = ovl_permission,
507 .getattr = ovl_getattr,
508 .listxattr = ovl_listxattr,
509 .get_acl = ovl_get_acl,
510 .update_time = ovl_update_time,
513 static const struct address_space_operations ovl_aops = {
514 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
515 .direct_IO = noop_direct_IO,
519 * It is possible to stack overlayfs instance on top of another
520 * overlayfs instance as lower layer. We need to annonate the
521 * stackable i_mutex locks according to stack level of the super
522 * block instance. An overlayfs instance can never be in stack
523 * depth 0 (there is always a real fs below it). An overlayfs
524 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
526 * For example, here is a snip from /proc/lockdep_chains after
527 * dir_iterate of nested overlayfs:
529 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
530 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
531 * [...] &type->i_mutex_dir_key (stack_depth=0)
533 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
535 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
537 #ifdef CONFIG_LOCKDEP
538 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
539 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
540 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
542 int depth = inode->i_sb->s_stack_depth - 1;
544 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
547 if (S_ISDIR(inode->i_mode))
548 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
550 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
552 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
556 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
557 unsigned long ino, int fsid)
559 int xinobits = ovl_xino_bits(inode->i_sb);
562 * When d_ino is consistent with st_ino (samefs or i_ino has enough
563 * bits to encode layer), set the same value used for st_ino to i_ino,
564 * so inode number exposed via /proc/locks and a like will be
565 * consistent with d_ino and st_ino values. An i_ino value inconsistent
566 * with d_ino also causes nfsd readdirplus to fail. When called from
567 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
568 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
570 if (ovl_same_sb(inode->i_sb) || xinobits) {
572 if (xinobits && fsid && !(ino >> (64 - xinobits)))
573 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
575 inode->i_ino = get_next_ino();
577 inode->i_mode = mode;
578 inode->i_flags |= S_NOCMTIME;
579 #ifdef CONFIG_FS_POSIX_ACL
580 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
583 ovl_lockdep_annotate_inode_mutex_key(inode);
585 switch (mode & S_IFMT) {
587 inode->i_op = &ovl_file_inode_operations;
588 inode->i_fop = &ovl_file_operations;
589 inode->i_mapping->a_ops = &ovl_aops;
593 inode->i_op = &ovl_dir_inode_operations;
594 inode->i_fop = &ovl_dir_operations;
598 inode->i_op = &ovl_symlink_inode_operations;
602 inode->i_op = &ovl_special_inode_operations;
603 init_special_inode(inode, mode, rdev);
609 * With inodes index enabled, an overlay inode nlink counts the union of upper
610 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
611 * upper inode, the following nlink modifying operations can happen:
613 * 1. Lower hardlink copy up
614 * 2. Upper hardlink created, unlinked or renamed over
615 * 3. Lower hardlink whiteout or renamed over
617 * For the first, copy up case, the union nlink does not change, whether the
618 * operation succeeds or fails, but the upper inode nlink may change.
619 * Therefore, before copy up, we store the union nlink value relative to the
620 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
622 * For the second, upper hardlink case, the union nlink should be incremented
623 * or decremented IFF the operation succeeds, aligned with nlink change of the
624 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
625 * value relative to the upper inode nlink in the index inode.
627 * For the last, lower cover up case, we simplify things by preceding the
628 * whiteout or cover up with copy up. This makes sure that there is an index
629 * upper inode where the nlink xattr can be stored before the copied up upper
632 #define OVL_NLINK_ADD_UPPER (1 << 0)
635 * On-disk format for indexed nlink:
637 * nlink relative to the upper inode - "U[+-]NUM"
638 * nlink relative to the lower inode - "L[+-]NUM"
641 static int ovl_set_nlink_common(struct dentry *dentry,
642 struct dentry *realdentry, const char *format)
644 struct inode *inode = d_inode(dentry);
645 struct inode *realinode = d_inode(realdentry);
649 len = snprintf(buf, sizeof(buf), format,
650 (int) (inode->i_nlink - realinode->i_nlink));
652 if (WARN_ON(len >= sizeof(buf)))
655 return ovl_do_setxattr(ovl_dentry_upper(dentry),
656 OVL_XATTR_NLINK, buf, len, 0);
659 int ovl_set_nlink_upper(struct dentry *dentry)
661 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
664 int ovl_set_nlink_lower(struct dentry *dentry)
666 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
669 unsigned int ovl_get_nlink(struct dentry *lowerdentry,
670 struct dentry *upperdentry,
671 unsigned int fallback)
678 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
681 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
686 if ((buf[0] != 'L' && buf[0] != 'U') ||
687 (buf[1] != '+' && buf[1] != '-'))
690 err = kstrtoint(buf + 1, 10, &nlink_diff);
694 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
703 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
708 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
712 inode = new_inode(sb);
714 ovl_fill_inode(inode, mode, rdev, 0, 0);
719 static int ovl_inode_test(struct inode *inode, void *data)
721 return inode->i_private == data;
724 static int ovl_inode_set(struct inode *inode, void *data)
726 inode->i_private = data;
730 static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
731 struct dentry *upperdentry, bool strict)
734 * For directories, @strict verify from lookup path performs consistency
735 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
736 * inode. Non @strict verify from NFS handle decode path passes NULL for
737 * 'unknown' lower/upper.
739 if (S_ISDIR(inode->i_mode) && strict) {
740 /* Real lower dir moved to upper layer under us? */
741 if (!lowerdentry && ovl_inode_lower(inode))
744 /* Lookup of an uncovered redirect origin? */
745 if (!upperdentry && ovl_inode_upper(inode))
750 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
751 * This happens when finding a copied up overlay inode for a renamed
752 * or hardlinked overlay dentry and lower dentry cannot be followed
753 * by origin because lower fs does not support file handles.
755 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
759 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
760 * This happens when finding a lower alias for a copied up hard link.
762 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
768 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
771 struct inode *inode, *key = d_inode(real);
773 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
777 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
778 is_upper ? real : NULL, false)) {
780 return ERR_PTR(-ESTALE);
786 bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
788 struct inode *key = d_inode(dir);
792 trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
796 res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
797 !ovl_inode_lower(trap);
804 * Create an inode cache entry for layer root dir, that will intentionally
805 * fail ovl_verify_inode(), so any lookup that will find some layer root
808 struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
810 struct inode *key = d_inode(dir);
814 return ERR_PTR(-ENOTDIR);
816 trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
819 return ERR_PTR(-ENOMEM);
821 if (!(trap->i_state & I_NEW)) {
822 /* Conflicting layer roots? */
824 return ERR_PTR(-ELOOP);
827 trap->i_mode = S_IFDIR;
828 trap->i_flags = S_DEAD;
829 unlock_new_inode(trap);
835 * Does overlay inode need to be hashed by lower inode?
837 static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
838 struct dentry *lower, struct dentry *index)
840 struct ovl_fs *ofs = sb->s_fs_info;
842 /* No, if pure upper */
846 /* Yes, if already indexed */
850 /* Yes, if won't be copied up */
854 /* No, if lower hardlink is or will be broken on copy up */
855 if ((upper || !ovl_indexdir(sb)) &&
856 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
859 /* No, if non-indexed upper with NFS export */
860 if (sb->s_export_op && upper)
863 /* Otherwise, hash by lower inode for fsnotify */
867 static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
870 return newinode ? inode_insert5(newinode, (unsigned long) key,
871 ovl_inode_test, ovl_inode_set, key) :
872 iget5_locked(sb, (unsigned long) key,
873 ovl_inode_test, ovl_inode_set, key);
876 struct inode *ovl_get_inode(struct super_block *sb,
877 struct ovl_inode_params *oip)
879 struct dentry *upperdentry = oip->upperdentry;
880 struct ovl_path *lowerpath = oip->lowerpath;
881 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
883 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
884 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
886 int fsid = bylower ? lowerpath->layer->fsid : 0;
887 bool is_dir, metacopy = false;
888 unsigned long ino = 0;
889 int err = oip->newinode ? -EEXIST : -ENOMEM;
892 realinode = d_inode(lowerdentry);
895 * Copy up origin (lower) may exist for non-indexed upper, but we must
896 * not use lower as hash key if this is a broken hardlink.
898 is_dir = S_ISDIR(realinode->i_mode);
899 if (upperdentry || bylower) {
900 struct inode *key = d_inode(bylower ? lowerdentry :
902 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
904 inode = ovl_iget5(sb, oip->newinode, key);
907 if (!(inode->i_state & I_NEW)) {
909 * Verify that the underlying files stored in the inode
910 * match those in the dentry.
912 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
920 kfree(oip->redirect);
924 /* Recalculate nlink for non-dir due to indexing */
926 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
927 set_nlink(inode, nlink);
930 /* Lower hardlink that will be broken on copy up */
931 inode = new_inode(sb);
936 ino = realinode->i_ino;
937 fsid = lowerpath->layer->fsid;
939 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
940 ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
942 if (upperdentry && ovl_is_impuredir(upperdentry))
943 ovl_set_flag(OVL_IMPURE, inode);
946 ovl_set_flag(OVL_INDEX, inode);
949 err = ovl_check_metacopy_xattr(upperdentry);
954 ovl_set_flag(OVL_UPPERDATA, inode);
957 OVL_I(inode)->redirect = oip->redirect;
960 ovl_set_flag(OVL_CONST_INO, inode);
962 /* Check for non-merge dir that may have whiteouts */
964 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
965 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
966 ovl_set_flag(OVL_WHITEOUTS, inode);
970 if (inode->i_state & I_NEW)
971 unlock_new_inode(inode);
976 pr_warn_ratelimited("overlayfs: failed to get inode (%i)\n", err);
977 inode = ERR_PTR(err);