3 * Copyright (C) 2011 Novell Inc.
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.
11 #include <linux/slab.h>
12 #include <linux/cred.h>
13 #include <linux/xattr.h>
14 #include <linux/posix_acl.h>
15 #include <linux/ratelimit.h>
16 #include "overlayfs.h"
19 int ovl_setattr(struct dentry
*dentry
, struct iattr
*attr
)
22 bool full_copy_up
= false;
23 struct dentry
*upperdentry
;
24 const struct cred
*old_cred
;
26 err
= setattr_prepare(dentry
, attr
);
30 err
= ovl_want_write(dentry
);
34 if (attr
->ia_valid
& ATTR_SIZE
) {
35 struct inode
*realinode
= d_inode(ovl_dentry_real(dentry
));
38 if (atomic_read(&realinode
->i_writecount
) < 0)
41 /* Truncate should trigger data copy up as well */
46 err
= ovl_copy_up(dentry
);
48 err
= ovl_copy_up_with_data(dentry
);
50 struct inode
*winode
= NULL
;
52 upperdentry
= ovl_dentry_upper(dentry
);
54 if (attr
->ia_valid
& ATTR_SIZE
) {
55 winode
= d_inode(upperdentry
);
56 err
= get_write_access(winode
);
61 if (attr
->ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
62 attr
->ia_valid
&= ~ATTR_MODE
;
64 inode_lock(upperdentry
->d_inode
);
65 old_cred
= ovl_override_creds(dentry
->d_sb
);
66 err
= notify_change(upperdentry
, attr
, NULL
);
67 revert_creds(old_cred
);
69 ovl_copyattr(upperdentry
->d_inode
, dentry
->d_inode
);
70 inode_unlock(upperdentry
->d_inode
);
73 put_write_access(winode
);
76 ovl_drop_write(dentry
);
81 static int ovl_map_dev_ino(struct dentry
*dentry
, struct kstat
*stat
,
82 struct ovl_layer
*lower_layer
)
84 bool samefs
= ovl_same_sb(dentry
->d_sb
);
85 unsigned int xinobits
= ovl_xino_bits(dentry
->d_sb
);
89 * When all layers are on the same fs, all real inode
90 * number are unique, so we use the overlay st_dev,
91 * which is friendly to du -x.
93 stat
->dev
= dentry
->d_sb
->s_dev
;
95 } else if (xinobits
) {
96 unsigned int shift
= 64 - xinobits
;
98 * All inode numbers of underlying fs should not be using the
99 * high xinobits, so we use high xinobits to partition the
100 * overlay st_ino address space. The high bits holds the fsid
101 * (upper fsid is 0). This way overlay inode numbers are unique
102 * and all inodes use overlay st_dev. Inode numbers are also
103 * persistent for a given layer configuration.
105 if (stat
->ino
>> shift
) {
106 pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
107 dentry
, stat
->ino
, xinobits
);
110 stat
->ino
|= ((u64
)lower_layer
->fsid
) << shift
;
112 stat
->dev
= dentry
->d_sb
->s_dev
;
117 /* The inode could not be mapped to a unified st_ino address space */
118 if (S_ISDIR(dentry
->d_inode
->i_mode
)) {
120 * Always use the overlay st_dev for directories, so 'find
121 * -xdev' will scan the entire overlay mount and won't cross the
122 * overlay mount boundaries.
124 * If not all layers are on the same fs the pair {real st_ino;
125 * overlay st_dev} is not unique, so use the non persistent
126 * overlay st_ino for directories.
128 stat
->dev
= dentry
->d_sb
->s_dev
;
129 stat
->ino
= dentry
->d_inode
->i_ino
;
130 } else if (lower_layer
&& lower_layer
->fsid
) {
132 * For non-samefs setup, if we cannot map all layers st_ino
133 * to a unified address space, we need to make sure that st_dev
134 * is unique per lower fs. Upper layer uses real st_dev and
135 * lower layers use the unique anonymous bdev assigned to the
138 stat
->dev
= lower_layer
->fs
->pseudo_dev
;
144 int ovl_getattr(const struct path
*path
, struct kstat
*stat
,
145 u32 request_mask
, unsigned int flags
)
147 struct dentry
*dentry
= path
->dentry
;
148 enum ovl_path_type type
;
149 struct path realpath
;
150 const struct cred
*old_cred
;
151 bool is_dir
= S_ISDIR(dentry
->d_inode
->i_mode
);
152 bool samefs
= ovl_same_sb(dentry
->d_sb
);
153 struct ovl_layer
*lower_layer
= NULL
;
155 bool metacopy_blocks
= false;
157 metacopy_blocks
= ovl_is_metacopy_dentry(dentry
);
159 type
= ovl_path_real(dentry
, &realpath
);
160 old_cred
= ovl_override_creds(dentry
->d_sb
);
161 err
= vfs_getattr(&realpath
, stat
, request_mask
, flags
);
166 * For non-dir or same fs, we use st_ino of the copy up origin.
167 * This guaranties constant st_dev/st_ino across copy up.
168 * With xino feature and non-samefs, we use st_ino of the copy up
169 * origin masked with high bits that represent the layer id.
171 * If lower filesystem supports NFS file handles, this also guaranties
172 * persistent st_ino across mount cycle.
174 if (!is_dir
|| samefs
|| ovl_xino_bits(dentry
->d_sb
)) {
175 if (!OVL_TYPE_UPPER(type
)) {
176 lower_layer
= ovl_layer_lower(dentry
);
177 } else if (OVL_TYPE_ORIGIN(type
)) {
178 struct kstat lowerstat
;
179 u32 lowermask
= STATX_INO
| STATX_BLOCKS
|
180 (!is_dir
? STATX_NLINK
: 0);
182 ovl_path_lower(dentry
, &realpath
);
183 err
= vfs_getattr(&realpath
, &lowerstat
,
189 * Lower hardlinks may be broken on copy up to different
190 * upper files, so we cannot use the lower origin st_ino
191 * for those different files, even for the same fs case.
193 * Similarly, several redirected dirs can point to the
194 * same dir on a lower layer. With the "verify_lower"
195 * feature, we do not use the lower origin st_ino, if
196 * we haven't verified that this redirect is unique.
198 * With inodes index enabled, it is safe to use st_ino
199 * of an indexed origin. The index validates that the
200 * upper hardlink is not broken and that a redirected
201 * dir is the only redirect to that origin.
203 if (ovl_test_flag(OVL_INDEX
, d_inode(dentry
)) ||
204 (!ovl_verify_lower(dentry
->d_sb
) &&
205 (is_dir
|| lowerstat
.nlink
== 1))) {
206 stat
->ino
= lowerstat
.ino
;
207 lower_layer
= ovl_layer_lower(dentry
);
211 * If we are querying a metacopy dentry and lower
212 * dentry is data dentry, then use the blocks we
213 * queried just now. We don't have to do additional
214 * vfs_getattr(). If lower itself is metacopy, then
215 * additional vfs_getattr() is unavoidable.
217 if (metacopy_blocks
&&
218 realpath
.dentry
== ovl_dentry_lowerdata(dentry
)) {
219 stat
->blocks
= lowerstat
.blocks
;
220 metacopy_blocks
= false;
224 if (metacopy_blocks
) {
226 * If lower is not same as lowerdata or if there was
227 * no origin on upper, we can end up here.
229 struct kstat lowerdatastat
;
230 u32 lowermask
= STATX_BLOCKS
;
232 ovl_path_lowerdata(dentry
, &realpath
);
233 err
= vfs_getattr(&realpath
, &lowerdatastat
,
237 stat
->blocks
= lowerdatastat
.blocks
;
241 err
= ovl_map_dev_ino(dentry
, stat
, lower_layer
);
246 * It's probably not worth it to count subdirs to get the
247 * correct link count. nlink=1 seems to pacify 'find' and
250 if (is_dir
&& OVL_TYPE_MERGE(type
))
254 * Return the overlay inode nlinks for indexed upper inodes.
255 * Overlay inode nlink counts the union of the upper hardlinks
256 * and non-covered lower hardlinks. It does not include the upper
259 if (!is_dir
&& ovl_test_flag(OVL_INDEX
, d_inode(dentry
)))
260 stat
->nlink
= dentry
->d_inode
->i_nlink
;
263 revert_creds(old_cred
);
268 int ovl_permission(struct inode
*inode
, int mask
)
270 struct inode
*upperinode
= ovl_inode_upper(inode
);
271 struct inode
*realinode
= upperinode
?: ovl_inode_lower(inode
);
272 const struct cred
*old_cred
;
275 /* Careful in RCU walk mode */
277 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
282 * Check overlay inode with the creds of task and underlying inode
283 * with creds of mounter
285 err
= generic_permission(inode
, mask
);
289 old_cred
= ovl_override_creds(inode
->i_sb
);
291 !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
292 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
293 /* Make sure mounter can read file for copy up later */
296 err
= inode_permission(realinode
, mask
);
297 revert_creds(old_cred
);
302 static const char *ovl_get_link(struct dentry
*dentry
,
304 struct delayed_call
*done
)
306 const struct cred
*old_cred
;
310 return ERR_PTR(-ECHILD
);
312 old_cred
= ovl_override_creds(dentry
->d_sb
);
313 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
314 revert_creds(old_cred
);
318 bool ovl_is_private_xattr(const char *name
)
320 return strncmp(name
, OVL_XATTR_PREFIX
,
321 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
324 int ovl_xattr_set(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
325 const void *value
, size_t size
, int flags
)
328 struct dentry
*upperdentry
= ovl_i_dentry_upper(inode
);
329 struct dentry
*realdentry
= upperdentry
?: ovl_dentry_lower(dentry
);
330 const struct cred
*old_cred
;
332 err
= ovl_want_write(dentry
);
336 if (!value
&& !upperdentry
) {
337 err
= vfs_getxattr(realdentry
, name
, NULL
, 0);
343 err
= ovl_copy_up(dentry
);
347 realdentry
= ovl_dentry_upper(dentry
);
350 old_cred
= ovl_override_creds(dentry
->d_sb
);
352 err
= vfs_setxattr(realdentry
, name
, value
, size
, flags
);
354 WARN_ON(flags
!= XATTR_REPLACE
);
355 err
= vfs_removexattr(realdentry
, name
);
357 revert_creds(old_cred
);
360 ovl_copyattr(d_inode(realdentry
), inode
);
363 ovl_drop_write(dentry
);
368 int ovl_xattr_get(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
369 void *value
, size_t size
)
372 const struct cred
*old_cred
;
373 struct dentry
*realdentry
=
374 ovl_i_dentry_upper(inode
) ?: ovl_dentry_lower(dentry
);
376 old_cred
= ovl_override_creds(dentry
->d_sb
);
377 res
= vfs_getxattr(realdentry
, name
, value
, size
);
378 revert_creds(old_cred
);
382 static bool ovl_can_list(const char *s
)
384 /* List all non-trusted xatts */
385 if (strncmp(s
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) != 0)
388 /* Never list trusted.overlay, list other trusted for superuser only */
389 return !ovl_is_private_xattr(s
) && capable(CAP_SYS_ADMIN
);
392 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
394 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
398 const struct cred
*old_cred
;
400 old_cred
= ovl_override_creds(dentry
->d_sb
);
401 res
= vfs_listxattr(realdentry
, list
, size
);
402 revert_creds(old_cred
);
403 if (res
<= 0 || size
== 0)
406 /* filter out private xattrs */
407 for (s
= list
, len
= res
; len
;) {
408 size_t slen
= strnlen(s
, len
) + 1;
410 /* underlying fs providing us with an broken xattr list? */
411 if (WARN_ON(slen
> len
))
415 if (!ovl_can_list(s
)) {
417 memmove(s
, s
+ slen
, len
);
426 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
428 struct inode
*realinode
= ovl_inode_real(inode
);
429 const struct cred
*old_cred
;
430 struct posix_acl
*acl
;
432 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
435 old_cred
= ovl_override_creds(inode
->i_sb
);
436 acl
= get_acl(realinode
, type
);
437 revert_creds(old_cred
);
442 int ovl_update_time(struct inode
*inode
, struct timespec64
*ts
, int flags
)
444 if (flags
& S_ATIME
) {
445 struct ovl_fs
*ofs
= inode
->i_sb
->s_fs_info
;
446 struct path upperpath
= {
447 .mnt
= ofs
->upper_mnt
,
448 .dentry
= ovl_upperdentry_dereference(OVL_I(inode
)),
451 if (upperpath
.dentry
) {
452 touch_atime(&upperpath
);
453 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
459 static int ovl_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
463 struct inode
*realinode
= ovl_inode_real(inode
);
464 const struct cred
*old_cred
;
466 if (!realinode
->i_op
->fiemap
)
469 old_cred
= ovl_override_creds(inode
->i_sb
);
471 if (fieinfo
->fi_flags
& FIEMAP_FLAG_SYNC
)
472 filemap_write_and_wait(realinode
->i_mapping
);
474 err
= realinode
->i_op
->fiemap(realinode
, fieinfo
, start
, len
);
475 revert_creds(old_cred
);
480 static const struct inode_operations ovl_file_inode_operations
= {
481 .setattr
= ovl_setattr
,
482 .permission
= ovl_permission
,
483 .getattr
= ovl_getattr
,
484 .listxattr
= ovl_listxattr
,
485 .get_acl
= ovl_get_acl
,
486 .update_time
= ovl_update_time
,
487 .fiemap
= ovl_fiemap
,
490 static const struct inode_operations ovl_symlink_inode_operations
= {
491 .setattr
= ovl_setattr
,
492 .get_link
= ovl_get_link
,
493 .getattr
= ovl_getattr
,
494 .listxattr
= ovl_listxattr
,
495 .update_time
= ovl_update_time
,
498 static const struct inode_operations ovl_special_inode_operations
= {
499 .setattr
= ovl_setattr
,
500 .permission
= ovl_permission
,
501 .getattr
= ovl_getattr
,
502 .listxattr
= ovl_listxattr
,
503 .get_acl
= ovl_get_acl
,
504 .update_time
= ovl_update_time
,
507 static const struct address_space_operations ovl_aops
= {
508 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
509 .direct_IO
= noop_direct_IO
,
513 * It is possible to stack overlayfs instance on top of another
514 * overlayfs instance as lower layer. We need to annonate the
515 * stackable i_mutex locks according to stack level of the super
516 * block instance. An overlayfs instance can never be in stack
517 * depth 0 (there is always a real fs below it). An overlayfs
518 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
520 * For example, here is a snip from /proc/lockdep_chains after
521 * dir_iterate of nested overlayfs:
523 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
524 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
525 * [...] &type->i_mutex_dir_key (stack_depth=0)
527 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
529 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode
*inode
)
531 #ifdef CONFIG_LOCKDEP
532 static struct lock_class_key ovl_i_mutex_key
[OVL_MAX_NESTING
];
533 static struct lock_class_key ovl_i_mutex_dir_key
[OVL_MAX_NESTING
];
534 static struct lock_class_key ovl_i_lock_key
[OVL_MAX_NESTING
];
536 int depth
= inode
->i_sb
->s_stack_depth
- 1;
538 if (WARN_ON_ONCE(depth
< 0 || depth
>= OVL_MAX_NESTING
))
541 if (S_ISDIR(inode
->i_mode
))
542 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_dir_key
[depth
]);
544 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_key
[depth
]);
546 lockdep_set_class(&OVL_I(inode
)->lock
, &ovl_i_lock_key
[depth
]);
550 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
,
551 unsigned long ino
, int fsid
)
553 int xinobits
= ovl_xino_bits(inode
->i_sb
);
556 * When d_ino is consistent with st_ino (samefs or i_ino has enough
557 * bits to encode layer), set the same value used for st_ino to i_ino,
558 * so inode number exposed via /proc/locks and a like will be
559 * consistent with d_ino and st_ino values. An i_ino value inconsistent
560 * with d_ino also causes nfsd readdirplus to fail. When called from
561 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
562 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
564 if (ovl_same_sb(inode
->i_sb
) || xinobits
) {
566 if (xinobits
&& fsid
&& !(ino
>> (64 - xinobits
)))
567 inode
->i_ino
|= (unsigned long)fsid
<< (64 - xinobits
);
569 inode
->i_ino
= get_next_ino();
571 inode
->i_mode
= mode
;
572 inode
->i_flags
|= S_NOCMTIME
;
573 #ifdef CONFIG_FS_POSIX_ACL
574 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
577 ovl_lockdep_annotate_inode_mutex_key(inode
);
579 switch (mode
& S_IFMT
) {
581 inode
->i_op
= &ovl_file_inode_operations
;
582 inode
->i_fop
= &ovl_file_operations
;
583 inode
->i_mapping
->a_ops
= &ovl_aops
;
587 inode
->i_op
= &ovl_dir_inode_operations
;
588 inode
->i_fop
= &ovl_dir_operations
;
592 inode
->i_op
= &ovl_symlink_inode_operations
;
596 inode
->i_op
= &ovl_special_inode_operations
;
597 init_special_inode(inode
, mode
, rdev
);
603 * With inodes index enabled, an overlay inode nlink counts the union of upper
604 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
605 * upper inode, the following nlink modifying operations can happen:
607 * 1. Lower hardlink copy up
608 * 2. Upper hardlink created, unlinked or renamed over
609 * 3. Lower hardlink whiteout or renamed over
611 * For the first, copy up case, the union nlink does not change, whether the
612 * operation succeeds or fails, but the upper inode nlink may change.
613 * Therefore, before copy up, we store the union nlink value relative to the
614 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
616 * For the second, upper hardlink case, the union nlink should be incremented
617 * or decremented IFF the operation succeeds, aligned with nlink change of the
618 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
619 * value relative to the upper inode nlink in the index inode.
621 * For the last, lower cover up case, we simplify things by preceding the
622 * whiteout or cover up with copy up. This makes sure that there is an index
623 * upper inode where the nlink xattr can be stored before the copied up upper
626 #define OVL_NLINK_ADD_UPPER (1 << 0)
629 * On-disk format for indexed nlink:
631 * nlink relative to the upper inode - "U[+-]NUM"
632 * nlink relative to the lower inode - "L[+-]NUM"
635 static int ovl_set_nlink_common(struct dentry
*dentry
,
636 struct dentry
*realdentry
, const char *format
)
638 struct inode
*inode
= d_inode(dentry
);
639 struct inode
*realinode
= d_inode(realdentry
);
643 len
= snprintf(buf
, sizeof(buf
), format
,
644 (int) (inode
->i_nlink
- realinode
->i_nlink
));
646 if (WARN_ON(len
>= sizeof(buf
)))
649 return ovl_do_setxattr(ovl_dentry_upper(dentry
),
650 OVL_XATTR_NLINK
, buf
, len
, 0);
653 int ovl_set_nlink_upper(struct dentry
*dentry
)
655 return ovl_set_nlink_common(dentry
, ovl_dentry_upper(dentry
), "U%+i");
658 int ovl_set_nlink_lower(struct dentry
*dentry
)
660 return ovl_set_nlink_common(dentry
, ovl_dentry_lower(dentry
), "L%+i");
663 unsigned int ovl_get_nlink(struct dentry
*lowerdentry
,
664 struct dentry
*upperdentry
,
665 unsigned int fallback
)
672 if (!lowerdentry
|| !upperdentry
|| d_inode(lowerdentry
)->i_nlink
== 1)
675 err
= vfs_getxattr(upperdentry
, OVL_XATTR_NLINK
, &buf
, sizeof(buf
) - 1);
680 if ((buf
[0] != 'L' && buf
[0] != 'U') ||
681 (buf
[1] != '+' && buf
[1] != '-'))
684 err
= kstrtoint(buf
+ 1, 10, &nlink_diff
);
688 nlink
= d_inode(buf
[0] == 'L' ? lowerdentry
: upperdentry
)->i_nlink
;
697 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
702 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
, dev_t rdev
)
706 inode
= new_inode(sb
);
708 ovl_fill_inode(inode
, mode
, rdev
, 0, 0);
713 static int ovl_inode_test(struct inode
*inode
, void *data
)
715 return inode
->i_private
== data
;
718 static int ovl_inode_set(struct inode
*inode
, void *data
)
720 inode
->i_private
= data
;
724 static bool ovl_verify_inode(struct inode
*inode
, struct dentry
*lowerdentry
,
725 struct dentry
*upperdentry
, bool strict
)
728 * For directories, @strict verify from lookup path performs consistency
729 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
730 * inode. Non @strict verify from NFS handle decode path passes NULL for
731 * 'unknown' lower/upper.
733 if (S_ISDIR(inode
->i_mode
) && strict
) {
734 /* Real lower dir moved to upper layer under us? */
735 if (!lowerdentry
&& ovl_inode_lower(inode
))
738 /* Lookup of an uncovered redirect origin? */
739 if (!upperdentry
&& ovl_inode_upper(inode
))
744 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
745 * This happens when finding a copied up overlay inode for a renamed
746 * or hardlinked overlay dentry and lower dentry cannot be followed
747 * by origin because lower fs does not support file handles.
749 if (lowerdentry
&& ovl_inode_lower(inode
) != d_inode(lowerdentry
))
753 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
754 * This happens when finding a lower alias for a copied up hard link.
756 if (upperdentry
&& ovl_inode_upper(inode
) != d_inode(upperdentry
))
762 struct inode
*ovl_lookup_inode(struct super_block
*sb
, struct dentry
*real
,
765 struct inode
*inode
, *key
= d_inode(real
);
767 inode
= ilookup5(sb
, (unsigned long) key
, ovl_inode_test
, key
);
771 if (!ovl_verify_inode(inode
, is_upper
? NULL
: real
,
772 is_upper
? real
: NULL
, false)) {
774 return ERR_PTR(-ESTALE
);
780 bool ovl_lookup_trap_inode(struct super_block
*sb
, struct dentry
*dir
)
782 struct inode
*key
= d_inode(dir
);
786 trap
= ilookup5(sb
, (unsigned long) key
, ovl_inode_test
, key
);
790 res
= IS_DEADDIR(trap
) && !ovl_inode_upper(trap
) &&
791 !ovl_inode_lower(trap
);
798 * Create an inode cache entry for layer root dir, that will intentionally
799 * fail ovl_verify_inode(), so any lookup that will find some layer root
802 struct inode
*ovl_get_trap_inode(struct super_block
*sb
, struct dentry
*dir
)
804 struct inode
*key
= d_inode(dir
);
808 return ERR_PTR(-ENOTDIR
);
810 trap
= iget5_locked(sb
, (unsigned long) key
, ovl_inode_test
,
813 return ERR_PTR(-ENOMEM
);
815 if (!(trap
->i_state
& I_NEW
)) {
816 /* Conflicting layer roots? */
818 return ERR_PTR(-ELOOP
);
821 trap
->i_mode
= S_IFDIR
;
822 trap
->i_flags
= S_DEAD
;
823 unlock_new_inode(trap
);
829 * Does overlay inode need to be hashed by lower inode?
831 static bool ovl_hash_bylower(struct super_block
*sb
, struct dentry
*upper
,
832 struct dentry
*lower
, struct dentry
*index
)
834 struct ovl_fs
*ofs
= sb
->s_fs_info
;
836 /* No, if pure upper */
840 /* Yes, if already indexed */
844 /* Yes, if won't be copied up */
848 /* No, if lower hardlink is or will be broken on copy up */
849 if ((upper
|| !ovl_indexdir(sb
)) &&
850 !d_is_dir(lower
) && d_inode(lower
)->i_nlink
> 1)
853 /* No, if non-indexed upper with NFS export */
854 if (sb
->s_export_op
&& upper
)
857 /* Otherwise, hash by lower inode for fsnotify */
861 static struct inode
*ovl_iget5(struct super_block
*sb
, struct inode
*newinode
,
864 return newinode
? inode_insert5(newinode
, (unsigned long) key
,
865 ovl_inode_test
, ovl_inode_set
, key
) :
866 iget5_locked(sb
, (unsigned long) key
,
867 ovl_inode_test
, ovl_inode_set
, key
);
870 struct inode
*ovl_get_inode(struct super_block
*sb
,
871 struct ovl_inode_params
*oip
)
873 struct dentry
*upperdentry
= oip
->upperdentry
;
874 struct ovl_path
*lowerpath
= oip
->lowerpath
;
875 struct inode
*realinode
= upperdentry
? d_inode(upperdentry
) : NULL
;
877 struct dentry
*lowerdentry
= lowerpath
? lowerpath
->dentry
: NULL
;
878 bool bylower
= ovl_hash_bylower(sb
, upperdentry
, lowerdentry
,
880 int fsid
= bylower
? oip
->lowerpath
->layer
->fsid
: 0;
881 bool is_dir
, metacopy
= false;
882 unsigned long ino
= 0;
883 int err
= oip
->newinode
? -EEXIST
: -ENOMEM
;
886 realinode
= d_inode(lowerdentry
);
889 * Copy up origin (lower) may exist for non-indexed upper, but we must
890 * not use lower as hash key if this is a broken hardlink.
892 is_dir
= S_ISDIR(realinode
->i_mode
);
893 if (upperdentry
|| bylower
) {
894 struct inode
*key
= d_inode(bylower
? lowerdentry
:
896 unsigned int nlink
= is_dir
? 1 : realinode
->i_nlink
;
898 inode
= ovl_iget5(sb
, oip
->newinode
, key
);
901 if (!(inode
->i_state
& I_NEW
)) {
903 * Verify that the underlying files stored in the inode
904 * match those in the dentry.
906 if (!ovl_verify_inode(inode
, lowerdentry
, upperdentry
,
914 kfree(oip
->redirect
);
918 /* Recalculate nlink for non-dir due to indexing */
920 nlink
= ovl_get_nlink(lowerdentry
, upperdentry
, nlink
);
921 set_nlink(inode
, nlink
);
924 /* Lower hardlink that will be broken on copy up */
925 inode
= new_inode(sb
);
931 ovl_fill_inode(inode
, realinode
->i_mode
, realinode
->i_rdev
, ino
, fsid
);
932 ovl_inode_init(inode
, upperdentry
, lowerdentry
, oip
->lowerdata
);
934 if (upperdentry
&& ovl_is_impuredir(upperdentry
))
935 ovl_set_flag(OVL_IMPURE
, inode
);
938 ovl_set_flag(OVL_INDEX
, inode
);
941 err
= ovl_check_metacopy_xattr(upperdentry
);
946 ovl_set_flag(OVL_UPPERDATA
, inode
);
949 OVL_I(inode
)->redirect
= oip
->redirect
;
952 ovl_set_flag(OVL_CONST_INO
, inode
);
954 /* Check for non-merge dir that may have whiteouts */
956 if (((upperdentry
&& lowerdentry
) || oip
->numlower
> 1) ||
957 ovl_check_origin_xattr(upperdentry
?: lowerdentry
)) {
958 ovl_set_flag(OVL_WHITEOUTS
, inode
);
962 if (inode
->i_state
& I_NEW
)
963 unlock_new_inode(inode
);
968 pr_warn_ratelimited("overlayfs: failed to get inode (%i)\n", err
);
969 inode
= ERR_PTR(err
);