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, int fsid)
80 bool samefs = ovl_same_fs(dentry->d_sb);
81 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
85 * When all layers are on the same fs, all real inode
86 * number are unique, so we use the overlay st_dev,
87 * which is friendly to du -x.
89 stat->dev = dentry->d_sb->s_dev;
91 } else if (xinobits) {
92 unsigned int shift = 64 - xinobits;
94 * All inode numbers of underlying fs should not be using the
95 * high xinobits, so we use high xinobits to partition the
96 * overlay st_ino address space. The high bits holds the fsid
97 * (upper fsid is 0). This way overlay inode numbers are unique
98 * and all inodes use overlay st_dev. Inode numbers are also
99 * persistent for a given layer configuration.
101 if (stat->ino >> shift) {
102 pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
103 dentry, stat->ino, xinobits);
105 stat->ino |= ((u64)fsid) << shift;
106 stat->dev = dentry->d_sb->s_dev;
111 /* The inode could not be mapped to a unified st_ino address space */
112 if (S_ISDIR(dentry->d_inode->i_mode)) {
114 * Always use the overlay st_dev for directories, so 'find
115 * -xdev' will scan the entire overlay mount and won't cross the
116 * overlay mount boundaries.
118 * If not all layers are on the same fs the pair {real st_ino;
119 * overlay st_dev} is not unique, so use the non persistent
120 * overlay st_ino for directories.
122 stat->dev = dentry->d_sb->s_dev;
123 stat->ino = dentry->d_inode->i_ino;
126 * For non-samefs setup, if we cannot map all layers st_ino
127 * to a unified address space, we need to make sure that st_dev
128 * is unique per underlying fs, so we use the unique anonymous
129 * bdev assigned to the underlying fs.
131 stat->dev = OVL_FS(dentry->d_sb)->fs[fsid].pseudo_dev;
137 int ovl_getattr(const struct path *path, struct kstat *stat,
138 u32 request_mask, unsigned int flags)
140 struct dentry *dentry = path->dentry;
141 enum ovl_path_type type;
142 struct path realpath;
143 const struct cred *old_cred;
144 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
147 bool metacopy_blocks = false;
149 metacopy_blocks = ovl_is_metacopy_dentry(dentry);
151 type = ovl_path_real(dentry, &realpath);
152 old_cred = ovl_override_creds(dentry->d_sb);
153 err = vfs_getattr(&realpath, stat, request_mask, flags);
158 * For non-dir or same fs, we use st_ino of the copy up origin.
159 * This guaranties constant st_dev/st_ino across copy up.
160 * With xino feature and non-samefs, we use st_ino of the copy up
161 * origin masked with high bits that represent the layer id.
163 * If lower filesystem supports NFS file handles, this also guaranties
164 * persistent st_ino across mount cycle.
166 if (!is_dir || ovl_same_dev(dentry->d_sb)) {
167 if (!OVL_TYPE_UPPER(type)) {
168 fsid = ovl_layer_lower(dentry)->fsid;
169 } else if (OVL_TYPE_ORIGIN(type)) {
170 struct kstat lowerstat;
171 u32 lowermask = STATX_INO | STATX_BLOCKS |
172 (!is_dir ? STATX_NLINK : 0);
174 ovl_path_lower(dentry, &realpath);
175 err = vfs_getattr(&realpath, &lowerstat,
181 * Lower hardlinks may be broken on copy up to different
182 * upper files, so we cannot use the lower origin st_ino
183 * for those different files, even for the same fs case.
185 * Similarly, several redirected dirs can point to the
186 * same dir on a lower layer. With the "verify_lower"
187 * feature, we do not use the lower origin st_ino, if
188 * we haven't verified that this redirect is unique.
190 * With inodes index enabled, it is safe to use st_ino
191 * of an indexed origin. The index validates that the
192 * upper hardlink is not broken and that a redirected
193 * dir is the only redirect to that origin.
195 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
196 (!ovl_verify_lower(dentry->d_sb) &&
197 (is_dir || lowerstat.nlink == 1))) {
198 fsid = ovl_layer_lower(dentry)->fsid;
199 stat->ino = lowerstat.ino;
203 * If we are querying a metacopy dentry and lower
204 * dentry is data dentry, then use the blocks we
205 * queried just now. We don't have to do additional
206 * vfs_getattr(). If lower itself is metacopy, then
207 * additional vfs_getattr() is unavoidable.
209 if (metacopy_blocks &&
210 realpath.dentry == ovl_dentry_lowerdata(dentry)) {
211 stat->blocks = lowerstat.blocks;
212 metacopy_blocks = false;
216 if (metacopy_blocks) {
218 * If lower is not same as lowerdata or if there was
219 * no origin on upper, we can end up here.
221 struct kstat lowerdatastat;
222 u32 lowermask = STATX_BLOCKS;
224 ovl_path_lowerdata(dentry, &realpath);
225 err = vfs_getattr(&realpath, &lowerdatastat,
229 stat->blocks = lowerdatastat.blocks;
233 err = ovl_map_dev_ino(dentry, stat, fsid);
238 * It's probably not worth it to count subdirs to get the
239 * correct link count. nlink=1 seems to pacify 'find' and
242 if (is_dir && OVL_TYPE_MERGE(type))
246 * Return the overlay inode nlinks for indexed upper inodes.
247 * Overlay inode nlink counts the union of the upper hardlinks
248 * and non-covered lower hardlinks. It does not include the upper
251 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
252 stat->nlink = dentry->d_inode->i_nlink;
255 revert_creds(old_cred);
260 int ovl_permission(struct inode *inode, int mask)
262 struct inode *upperinode = ovl_inode_upper(inode);
263 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
264 const struct cred *old_cred;
267 /* Careful in RCU walk mode */
269 WARN_ON(!(mask & MAY_NOT_BLOCK));
274 * Check overlay inode with the creds of task and underlying inode
275 * with creds of mounter
277 err = generic_permission(inode, mask);
281 old_cred = ovl_override_creds(inode->i_sb);
283 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
284 mask &= ~(MAY_WRITE | MAY_APPEND);
285 /* Make sure mounter can read file for copy up later */
288 err = inode_permission(realinode, mask);
289 revert_creds(old_cred);
294 static const char *ovl_get_link(struct dentry *dentry,
296 struct delayed_call *done)
298 const struct cred *old_cred;
302 return ERR_PTR(-ECHILD);
304 old_cred = ovl_override_creds(dentry->d_sb);
305 p = vfs_get_link(ovl_dentry_real(dentry), done);
306 revert_creds(old_cred);
310 bool ovl_is_private_xattr(const char *name)
312 return strncmp(name, OVL_XATTR_PREFIX,
313 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
316 int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
317 const void *value, size_t size, int flags)
320 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
321 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
322 const struct cred *old_cred;
324 err = ovl_want_write(dentry);
328 if (!value && !upperdentry) {
329 err = vfs_getxattr(realdentry, name, NULL, 0);
335 err = ovl_copy_up(dentry);
339 realdentry = ovl_dentry_upper(dentry);
342 old_cred = ovl_override_creds(dentry->d_sb);
344 err = vfs_setxattr(realdentry, name, value, size, flags);
346 WARN_ON(flags != XATTR_REPLACE);
347 err = vfs_removexattr(realdentry, name);
349 revert_creds(old_cred);
352 ovl_copyattr(d_inode(realdentry), inode);
355 ovl_drop_write(dentry);
360 int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
361 void *value, size_t size)
364 const struct cred *old_cred;
365 struct dentry *realdentry =
366 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
368 old_cred = ovl_override_creds(dentry->d_sb);
369 res = vfs_getxattr(realdentry, name, value, size);
370 revert_creds(old_cred);
374 static bool ovl_can_list(const char *s)
376 /* List all non-trusted xatts */
377 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
380 /* Never list trusted.overlay, list other trusted for superuser only */
381 return !ovl_is_private_xattr(s) &&
382 ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
385 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
387 struct dentry *realdentry = ovl_dentry_real(dentry);
391 const struct cred *old_cred;
393 old_cred = ovl_override_creds(dentry->d_sb);
394 res = vfs_listxattr(realdentry, list, size);
395 revert_creds(old_cred);
396 if (res <= 0 || size == 0)
399 /* filter out private xattrs */
400 for (s = list, len = res; len;) {
401 size_t slen = strnlen(s, len) + 1;
403 /* underlying fs providing us with an broken xattr list? */
404 if (WARN_ON(slen > len))
408 if (!ovl_can_list(s)) {
410 memmove(s, s + slen, len);
419 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
421 struct inode *realinode = ovl_inode_real(inode);
422 const struct cred *old_cred;
423 struct posix_acl *acl;
425 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
428 old_cred = ovl_override_creds(inode->i_sb);
429 acl = get_acl(realinode, type);
430 revert_creds(old_cred);
435 int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
437 if (flags & S_ATIME) {
438 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
439 struct path upperpath = {
440 .mnt = ofs->upper_mnt,
441 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
444 if (upperpath.dentry) {
445 touch_atime(&upperpath);
446 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
452 static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
456 struct inode *realinode = ovl_inode_real(inode);
457 const struct cred *old_cred;
459 if (!realinode->i_op->fiemap)
462 old_cred = ovl_override_creds(inode->i_sb);
464 if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
465 filemap_write_and_wait(realinode->i_mapping);
467 err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
468 revert_creds(old_cred);
473 static const struct inode_operations ovl_file_inode_operations = {
474 .setattr = ovl_setattr,
475 .permission = ovl_permission,
476 .getattr = ovl_getattr,
477 .listxattr = ovl_listxattr,
478 .get_acl = ovl_get_acl,
479 .update_time = ovl_update_time,
480 .fiemap = ovl_fiemap,
483 static const struct inode_operations ovl_symlink_inode_operations = {
484 .setattr = ovl_setattr,
485 .get_link = ovl_get_link,
486 .getattr = ovl_getattr,
487 .listxattr = ovl_listxattr,
488 .update_time = ovl_update_time,
491 static const struct inode_operations ovl_special_inode_operations = {
492 .setattr = ovl_setattr,
493 .permission = ovl_permission,
494 .getattr = ovl_getattr,
495 .listxattr = ovl_listxattr,
496 .get_acl = ovl_get_acl,
497 .update_time = ovl_update_time,
500 static const struct address_space_operations ovl_aops = {
501 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
502 .direct_IO = noop_direct_IO,
506 * It is possible to stack overlayfs instance on top of another
507 * overlayfs instance as lower layer. We need to annonate the
508 * stackable i_mutex locks according to stack level of the super
509 * block instance. An overlayfs instance can never be in stack
510 * depth 0 (there is always a real fs below it). An overlayfs
511 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
513 * For example, here is a snip from /proc/lockdep_chains after
514 * dir_iterate of nested overlayfs:
516 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
517 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
518 * [...] &type->i_mutex_dir_key (stack_depth=0)
520 * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
522 * This chain is valid:
523 * - inode->i_rwsem (inode_lock[2])
524 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
525 * - OVL_I(inode)->lock (ovl_inode_lock[2])
526 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
528 * And this chain is valid:
529 * - inode->i_rwsem (inode_lock[2])
530 * - OVL_I(inode)->lock (ovl_inode_lock[2])
531 * - lowerinode->i_rwsem (inode_lock[1])
532 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
534 * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
535 * held, because it is in reverse order of the non-nested case using the same
537 * - inode->i_rwsem (inode_lock[1])
538 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
539 * - OVL_I(inode)->lock (ovl_inode_lock[1])
541 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
543 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
545 #ifdef CONFIG_LOCKDEP
546 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
547 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
548 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
550 int depth = inode->i_sb->s_stack_depth - 1;
552 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
555 if (S_ISDIR(inode->i_mode))
556 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
558 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
560 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
564 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
565 unsigned long ino, int fsid)
567 int xinobits = ovl_xino_bits(inode->i_sb);
570 * When d_ino is consistent with st_ino (samefs or i_ino has enough
571 * bits to encode layer), set the same value used for st_ino to i_ino,
572 * so inode number exposed via /proc/locks and a like will be
573 * consistent with d_ino and st_ino values. An i_ino value inconsistent
574 * with d_ino also causes nfsd readdirplus to fail. When called from
575 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
576 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
578 if (ovl_same_dev(inode->i_sb)) {
580 if (xinobits && fsid && !(ino >> (64 - xinobits)))
581 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
583 inode->i_ino = get_next_ino();
585 inode->i_mode = mode;
586 inode->i_flags |= S_NOCMTIME;
587 #ifdef CONFIG_FS_POSIX_ACL
588 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
591 ovl_lockdep_annotate_inode_mutex_key(inode);
593 switch (mode & S_IFMT) {
595 inode->i_op = &ovl_file_inode_operations;
596 inode->i_fop = &ovl_file_operations;
597 inode->i_mapping->a_ops = &ovl_aops;
601 inode->i_op = &ovl_dir_inode_operations;
602 inode->i_fop = &ovl_dir_operations;
606 inode->i_op = &ovl_symlink_inode_operations;
610 inode->i_op = &ovl_special_inode_operations;
611 init_special_inode(inode, mode, rdev);
617 * With inodes index enabled, an overlay inode nlink counts the union of upper
618 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
619 * upper inode, the following nlink modifying operations can happen:
621 * 1. Lower hardlink copy up
622 * 2. Upper hardlink created, unlinked or renamed over
623 * 3. Lower hardlink whiteout or renamed over
625 * For the first, copy up case, the union nlink does not change, whether the
626 * operation succeeds or fails, but the upper inode nlink may change.
627 * Therefore, before copy up, we store the union nlink value relative to the
628 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
630 * For the second, upper hardlink case, the union nlink should be incremented
631 * or decremented IFF the operation succeeds, aligned with nlink change of the
632 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
633 * value relative to the upper inode nlink in the index inode.
635 * For the last, lower cover up case, we simplify things by preceding the
636 * whiteout or cover up with copy up. This makes sure that there is an index
637 * upper inode where the nlink xattr can be stored before the copied up upper
640 #define OVL_NLINK_ADD_UPPER (1 << 0)
643 * On-disk format for indexed nlink:
645 * nlink relative to the upper inode - "U[+-]NUM"
646 * nlink relative to the lower inode - "L[+-]NUM"
649 static int ovl_set_nlink_common(struct dentry *dentry,
650 struct dentry *realdentry, const char *format)
652 struct inode *inode = d_inode(dentry);
653 struct inode *realinode = d_inode(realdentry);
657 len = snprintf(buf, sizeof(buf), format,
658 (int) (inode->i_nlink - realinode->i_nlink));
660 if (WARN_ON(len >= sizeof(buf)))
663 return ovl_do_setxattr(ovl_dentry_upper(dentry),
664 OVL_XATTR_NLINK, buf, len, 0);
667 int ovl_set_nlink_upper(struct dentry *dentry)
669 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
672 int ovl_set_nlink_lower(struct dentry *dentry)
674 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
677 unsigned int ovl_get_nlink(struct dentry *lowerdentry,
678 struct dentry *upperdentry,
679 unsigned int fallback)
686 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
689 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
694 if ((buf[0] != 'L' && buf[0] != 'U') ||
695 (buf[1] != '+' && buf[1] != '-'))
698 err = kstrtoint(buf + 1, 10, &nlink_diff);
702 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
711 pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
716 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
720 inode = new_inode(sb);
722 ovl_fill_inode(inode, mode, rdev, 0, 0);
727 static int ovl_inode_test(struct inode *inode, void *data)
729 return inode->i_private == data;
732 static int ovl_inode_set(struct inode *inode, void *data)
734 inode->i_private = data;
738 static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
739 struct dentry *upperdentry, bool strict)
742 * For directories, @strict verify from lookup path performs consistency
743 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
744 * inode. Non @strict verify from NFS handle decode path passes NULL for
745 * 'unknown' lower/upper.
747 if (S_ISDIR(inode->i_mode) && strict) {
748 /* Real lower dir moved to upper layer under us? */
749 if (!lowerdentry && ovl_inode_lower(inode))
752 /* Lookup of an uncovered redirect origin? */
753 if (!upperdentry && ovl_inode_upper(inode))
758 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
759 * This happens when finding a copied up overlay inode for a renamed
760 * or hardlinked overlay dentry and lower dentry cannot be followed
761 * by origin because lower fs does not support file handles.
763 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
767 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
768 * This happens when finding a lower alias for a copied up hard link.
770 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
776 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
779 struct inode *inode, *key = d_inode(real);
781 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
785 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
786 is_upper ? real : NULL, false)) {
788 return ERR_PTR(-ESTALE);
794 bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
796 struct inode *key = d_inode(dir);
800 trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
804 res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
805 !ovl_inode_lower(trap);
812 * Create an inode cache entry for layer root dir, that will intentionally
813 * fail ovl_verify_inode(), so any lookup that will find some layer root
816 struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
818 struct inode *key = d_inode(dir);
822 return ERR_PTR(-ENOTDIR);
824 trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
827 return ERR_PTR(-ENOMEM);
829 if (!(trap->i_state & I_NEW)) {
830 /* Conflicting layer roots? */
832 return ERR_PTR(-ELOOP);
835 trap->i_mode = S_IFDIR;
836 trap->i_flags = S_DEAD;
837 unlock_new_inode(trap);
843 * Does overlay inode need to be hashed by lower inode?
845 static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
846 struct dentry *lower, struct dentry *index)
848 struct ovl_fs *ofs = sb->s_fs_info;
850 /* No, if pure upper */
854 /* Yes, if already indexed */
858 /* Yes, if won't be copied up */
862 /* No, if lower hardlink is or will be broken on copy up */
863 if ((upper || !ovl_indexdir(sb)) &&
864 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
867 /* No, if non-indexed upper with NFS export */
868 if (sb->s_export_op && upper)
871 /* Otherwise, hash by lower inode for fsnotify */
875 static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
878 return newinode ? inode_insert5(newinode, (unsigned long) key,
879 ovl_inode_test, ovl_inode_set, key) :
880 iget5_locked(sb, (unsigned long) key,
881 ovl_inode_test, ovl_inode_set, key);
884 struct inode *ovl_get_inode(struct super_block *sb,
885 struct ovl_inode_params *oip)
887 struct dentry *upperdentry = oip->upperdentry;
888 struct ovl_path *lowerpath = oip->lowerpath;
889 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
891 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
892 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
894 int fsid = bylower ? lowerpath->layer->fsid : 0;
895 bool is_dir, metacopy = false;
896 unsigned long ino = 0;
897 int err = oip->newinode ? -EEXIST : -ENOMEM;
900 realinode = d_inode(lowerdentry);
903 * Copy up origin (lower) may exist for non-indexed upper, but we must
904 * not use lower as hash key if this is a broken hardlink.
906 is_dir = S_ISDIR(realinode->i_mode);
907 if (upperdentry || bylower) {
908 struct inode *key = d_inode(bylower ? lowerdentry :
910 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
912 inode = ovl_iget5(sb, oip->newinode, key);
915 if (!(inode->i_state & I_NEW)) {
917 * Verify that the underlying files stored in the inode
918 * match those in the dentry.
920 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
928 kfree(oip->redirect);
932 /* Recalculate nlink for non-dir due to indexing */
934 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
935 set_nlink(inode, nlink);
938 /* Lower hardlink that will be broken on copy up */
939 inode = new_inode(sb);
944 ino = realinode->i_ino;
945 fsid = lowerpath->layer->fsid;
947 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
948 ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata);
950 if (upperdentry && ovl_is_impuredir(upperdentry))
951 ovl_set_flag(OVL_IMPURE, inode);
954 ovl_set_flag(OVL_INDEX, inode);
957 err = ovl_check_metacopy_xattr(upperdentry);
962 ovl_set_flag(OVL_UPPERDATA, inode);
965 OVL_I(inode)->redirect = oip->redirect;
968 ovl_set_flag(OVL_CONST_INO, inode);
970 /* Check for non-merge dir that may have whiteouts */
972 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
973 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
974 ovl_set_flag(OVL_WHITEOUTS, inode);
978 if (inode->i_state & I_NEW)
979 unlock_new_inode(inode);
984 pr_warn_ratelimited("failed to get inode (%i)\n", err);
985 inode = ERR_PTR(err);