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
);
82 unsigned int xinoshift
= 64 - xinobits
;
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
) {
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). The lowest xinobit is reserved for mapping
98 * the non-peresistent inode numbers range in case of overflow.
99 * This way all overlay inode numbers are unique and use the
102 if (likely(!(stat
->ino
>> xinoshift
))) {
103 stat
->ino
|= ((u64
)fsid
) << (xinoshift
+ 1);
104 stat
->dev
= dentry
->d_sb
->s_dev
;
106 } else if (ovl_xino_warn(dentry
->d_sb
)) {
107 pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
108 dentry
, stat
->ino
, xinobits
);
112 /* The inode could not be mapped to a unified st_ino address space */
113 if (S_ISDIR(dentry
->d_inode
->i_mode
)) {
115 * Always use the overlay st_dev for directories, so 'find
116 * -xdev' will scan the entire overlay mount and won't cross the
117 * overlay mount boundaries.
119 * If not all layers are on the same fs the pair {real st_ino;
120 * overlay st_dev} is not unique, so use the non persistent
121 * overlay st_ino for directories.
123 stat
->dev
= dentry
->d_sb
->s_dev
;
124 stat
->ino
= dentry
->d_inode
->i_ino
;
127 * For non-samefs setup, if we cannot map all layers st_ino
128 * to a unified address space, we need to make sure that st_dev
129 * is unique per underlying fs, so we use the unique anonymous
130 * bdev assigned to the underlying fs.
132 stat
->dev
= OVL_FS(dentry
->d_sb
)->fs
[fsid
].pseudo_dev
;
138 int ovl_getattr(const struct path
*path
, struct kstat
*stat
,
139 u32 request_mask
, unsigned int flags
)
141 struct dentry
*dentry
= path
->dentry
;
142 enum ovl_path_type type
;
143 struct path realpath
;
144 const struct cred
*old_cred
;
145 bool is_dir
= S_ISDIR(dentry
->d_inode
->i_mode
);
148 bool metacopy_blocks
= false;
150 metacopy_blocks
= ovl_is_metacopy_dentry(dentry
);
152 type
= ovl_path_real(dentry
, &realpath
);
153 old_cred
= ovl_override_creds(dentry
->d_sb
);
154 err
= vfs_getattr(&realpath
, stat
, request_mask
, flags
);
159 * For non-dir or same fs, we use st_ino of the copy up origin.
160 * This guaranties constant st_dev/st_ino across copy up.
161 * With xino feature and non-samefs, we use st_ino of the copy up
162 * origin masked with high bits that represent the layer id.
164 * If lower filesystem supports NFS file handles, this also guaranties
165 * persistent st_ino across mount cycle.
167 if (!is_dir
|| ovl_same_dev(dentry
->d_sb
)) {
168 if (!OVL_TYPE_UPPER(type
)) {
169 fsid
= ovl_layer_lower(dentry
)->fsid
;
170 } else if (OVL_TYPE_ORIGIN(type
)) {
171 struct kstat lowerstat
;
172 u32 lowermask
= STATX_INO
| STATX_BLOCKS
|
173 (!is_dir
? STATX_NLINK
: 0);
175 ovl_path_lower(dentry
, &realpath
);
176 err
= vfs_getattr(&realpath
, &lowerstat
,
182 * Lower hardlinks may be broken on copy up to different
183 * upper files, so we cannot use the lower origin st_ino
184 * for those different files, even for the same fs case.
186 * Similarly, several redirected dirs can point to the
187 * same dir on a lower layer. With the "verify_lower"
188 * feature, we do not use the lower origin st_ino, if
189 * we haven't verified that this redirect is unique.
191 * With inodes index enabled, it is safe to use st_ino
192 * of an indexed origin. The index validates that the
193 * upper hardlink is not broken and that a redirected
194 * dir is the only redirect to that origin.
196 if (ovl_test_flag(OVL_INDEX
, d_inode(dentry
)) ||
197 (!ovl_verify_lower(dentry
->d_sb
) &&
198 (is_dir
|| lowerstat
.nlink
== 1))) {
199 fsid
= ovl_layer_lower(dentry
)->fsid
;
200 stat
->ino
= lowerstat
.ino
;
204 * If we are querying a metacopy dentry and lower
205 * dentry is data dentry, then use the blocks we
206 * queried just now. We don't have to do additional
207 * vfs_getattr(). If lower itself is metacopy, then
208 * additional vfs_getattr() is unavoidable.
210 if (metacopy_blocks
&&
211 realpath
.dentry
== ovl_dentry_lowerdata(dentry
)) {
212 stat
->blocks
= lowerstat
.blocks
;
213 metacopy_blocks
= false;
217 if (metacopy_blocks
) {
219 * If lower is not same as lowerdata or if there was
220 * no origin on upper, we can end up here.
222 struct kstat lowerdatastat
;
223 u32 lowermask
= STATX_BLOCKS
;
225 ovl_path_lowerdata(dentry
, &realpath
);
226 err
= vfs_getattr(&realpath
, &lowerdatastat
,
230 stat
->blocks
= lowerdatastat
.blocks
;
234 err
= ovl_map_dev_ino(dentry
, stat
, fsid
);
239 * It's probably not worth it to count subdirs to get the
240 * correct link count. nlink=1 seems to pacify 'find' and
243 if (is_dir
&& OVL_TYPE_MERGE(type
))
247 * Return the overlay inode nlinks for indexed upper inodes.
248 * Overlay inode nlink counts the union of the upper hardlinks
249 * and non-covered lower hardlinks. It does not include the upper
252 if (!is_dir
&& ovl_test_flag(OVL_INDEX
, d_inode(dentry
)))
253 stat
->nlink
= dentry
->d_inode
->i_nlink
;
256 revert_creds(old_cred
);
261 int ovl_permission(struct inode
*inode
, int mask
)
263 struct inode
*upperinode
= ovl_inode_upper(inode
);
264 struct inode
*realinode
= upperinode
?: ovl_inode_lower(inode
);
265 const struct cred
*old_cred
;
268 /* Careful in RCU walk mode */
270 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
275 * Check overlay inode with the creds of task and underlying inode
276 * with creds of mounter
278 err
= generic_permission(inode
, mask
);
282 old_cred
= ovl_override_creds(inode
->i_sb
);
284 !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
285 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
286 /* Make sure mounter can read file for copy up later */
289 err
= inode_permission(realinode
, mask
);
290 revert_creds(old_cred
);
295 static const char *ovl_get_link(struct dentry
*dentry
,
297 struct delayed_call
*done
)
299 const struct cred
*old_cred
;
303 return ERR_PTR(-ECHILD
);
305 old_cred
= ovl_override_creds(dentry
->d_sb
);
306 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
307 revert_creds(old_cred
);
311 bool ovl_is_private_xattr(const char *name
)
313 return strncmp(name
, OVL_XATTR_PREFIX
,
314 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
317 int ovl_xattr_set(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
318 const void *value
, size_t size
, int flags
)
321 struct dentry
*upperdentry
= ovl_i_dentry_upper(inode
);
322 struct dentry
*realdentry
= upperdentry
?: ovl_dentry_lower(dentry
);
323 const struct cred
*old_cred
;
325 err
= ovl_want_write(dentry
);
329 if (!value
&& !upperdentry
) {
330 err
= vfs_getxattr(realdentry
, name
, NULL
, 0);
336 err
= ovl_copy_up(dentry
);
340 realdentry
= ovl_dentry_upper(dentry
);
343 old_cred
= ovl_override_creds(dentry
->d_sb
);
345 err
= vfs_setxattr(realdentry
, name
, value
, size
, flags
);
347 WARN_ON(flags
!= XATTR_REPLACE
);
348 err
= vfs_removexattr(realdentry
, name
);
350 revert_creds(old_cred
);
353 ovl_copyattr(d_inode(realdentry
), inode
);
356 ovl_drop_write(dentry
);
361 int ovl_xattr_get(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
362 void *value
, size_t size
)
365 const struct cred
*old_cred
;
366 struct dentry
*realdentry
=
367 ovl_i_dentry_upper(inode
) ?: ovl_dentry_lower(dentry
);
369 old_cred
= ovl_override_creds(dentry
->d_sb
);
370 res
= vfs_getxattr(realdentry
, name
, value
, size
);
371 revert_creds(old_cred
);
375 static bool ovl_can_list(const char *s
)
377 /* List all non-trusted xatts */
378 if (strncmp(s
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) != 0)
381 /* Never list trusted.overlay, list other trusted for superuser only */
382 return !ovl_is_private_xattr(s
) &&
383 ns_capable_noaudit(&init_user_ns
, CAP_SYS_ADMIN
);
386 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
388 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
392 const struct cred
*old_cred
;
394 old_cred
= ovl_override_creds(dentry
->d_sb
);
395 res
= vfs_listxattr(realdentry
, list
, size
);
396 revert_creds(old_cred
);
397 if (res
<= 0 || size
== 0)
400 /* filter out private xattrs */
401 for (s
= list
, len
= res
; len
;) {
402 size_t slen
= strnlen(s
, len
) + 1;
404 /* underlying fs providing us with an broken xattr list? */
405 if (WARN_ON(slen
> len
))
409 if (!ovl_can_list(s
)) {
411 memmove(s
, s
+ slen
, len
);
420 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
422 struct inode
*realinode
= ovl_inode_real(inode
);
423 const struct cred
*old_cred
;
424 struct posix_acl
*acl
;
426 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
429 old_cred
= ovl_override_creds(inode
->i_sb
);
430 acl
= get_acl(realinode
, type
);
431 revert_creds(old_cred
);
436 int ovl_update_time(struct inode
*inode
, struct timespec64
*ts
, int flags
)
438 if (flags
& S_ATIME
) {
439 struct ovl_fs
*ofs
= inode
->i_sb
->s_fs_info
;
440 struct path upperpath
= {
441 .mnt
= ofs
->upper_mnt
,
442 .dentry
= ovl_upperdentry_dereference(OVL_I(inode
)),
445 if (upperpath
.dentry
) {
446 touch_atime(&upperpath
);
447 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
453 static int ovl_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
457 struct inode
*realinode
= ovl_inode_real(inode
);
458 const struct cred
*old_cred
;
460 if (!realinode
->i_op
->fiemap
)
463 old_cred
= ovl_override_creds(inode
->i_sb
);
465 if (fieinfo
->fi_flags
& FIEMAP_FLAG_SYNC
)
466 filemap_write_and_wait(realinode
->i_mapping
);
468 err
= realinode
->i_op
->fiemap(realinode
, fieinfo
, start
, len
);
469 revert_creds(old_cred
);
474 static const struct inode_operations ovl_file_inode_operations
= {
475 .setattr
= ovl_setattr
,
476 .permission
= ovl_permission
,
477 .getattr
= ovl_getattr
,
478 .listxattr
= ovl_listxattr
,
479 .get_acl
= ovl_get_acl
,
480 .update_time
= ovl_update_time
,
481 .fiemap
= ovl_fiemap
,
484 static const struct inode_operations ovl_symlink_inode_operations
= {
485 .setattr
= ovl_setattr
,
486 .get_link
= ovl_get_link
,
487 .getattr
= ovl_getattr
,
488 .listxattr
= ovl_listxattr
,
489 .update_time
= ovl_update_time
,
492 static const struct inode_operations ovl_special_inode_operations
= {
493 .setattr
= ovl_setattr
,
494 .permission
= ovl_permission
,
495 .getattr
= ovl_getattr
,
496 .listxattr
= ovl_listxattr
,
497 .get_acl
= ovl_get_acl
,
498 .update_time
= ovl_update_time
,
501 static const struct address_space_operations ovl_aops
= {
502 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
503 .direct_IO
= noop_direct_IO
,
507 * It is possible to stack overlayfs instance on top of another
508 * overlayfs instance as lower layer. We need to annotate the
509 * stackable i_mutex locks according to stack level of the super
510 * block instance. An overlayfs instance can never be in stack
511 * depth 0 (there is always a real fs below it). An overlayfs
512 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
514 * For example, here is a snip from /proc/lockdep_chains after
515 * dir_iterate of nested overlayfs:
517 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
518 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
519 * [...] &type->i_mutex_dir_key (stack_depth=0)
521 * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
523 * This chain is valid:
524 * - inode->i_rwsem (inode_lock[2])
525 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
526 * - OVL_I(inode)->lock (ovl_inode_lock[2])
527 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
529 * And this chain is valid:
530 * - inode->i_rwsem (inode_lock[2])
531 * - OVL_I(inode)->lock (ovl_inode_lock[2])
532 * - lowerinode->i_rwsem (inode_lock[1])
533 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
535 * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
536 * held, because it is in reverse order of the non-nested case using the same
538 * - inode->i_rwsem (inode_lock[1])
539 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
540 * - OVL_I(inode)->lock (ovl_inode_lock[1])
542 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
544 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode
*inode
)
546 #ifdef CONFIG_LOCKDEP
547 static struct lock_class_key ovl_i_mutex_key
[OVL_MAX_NESTING
];
548 static struct lock_class_key ovl_i_mutex_dir_key
[OVL_MAX_NESTING
];
549 static struct lock_class_key ovl_i_lock_key
[OVL_MAX_NESTING
];
551 int depth
= inode
->i_sb
->s_stack_depth
- 1;
553 if (WARN_ON_ONCE(depth
< 0 || depth
>= OVL_MAX_NESTING
))
556 if (S_ISDIR(inode
->i_mode
))
557 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_dir_key
[depth
]);
559 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_key
[depth
]);
561 lockdep_set_class(&OVL_I(inode
)->lock
, &ovl_i_lock_key
[depth
]);
565 static void ovl_next_ino(struct inode
*inode
)
567 struct ovl_fs
*ofs
= inode
->i_sb
->s_fs_info
;
569 inode
->i_ino
= atomic_long_inc_return(&ofs
->last_ino
);
570 if (unlikely(!inode
->i_ino
))
571 inode
->i_ino
= atomic_long_inc_return(&ofs
->last_ino
);
574 static void ovl_map_ino(struct inode
*inode
, unsigned long ino
, int fsid
)
576 int xinobits
= ovl_xino_bits(inode
->i_sb
);
577 unsigned int xinoshift
= 64 - xinobits
;
580 * When d_ino is consistent with st_ino (samefs or i_ino has enough
581 * bits to encode layer), set the same value used for st_ino to i_ino,
582 * so inode number exposed via /proc/locks and a like will be
583 * consistent with d_ino and st_ino values. An i_ino value inconsistent
584 * with d_ino also causes nfsd readdirplus to fail.
587 if (ovl_same_fs(inode
->i_sb
)) {
589 } else if (xinobits
&& likely(!(ino
>> xinoshift
))) {
590 inode
->i_ino
|= (unsigned long)fsid
<< (xinoshift
+ 1);
595 * For directory inodes on non-samefs with xino disabled or xino
596 * overflow, we allocate a non-persistent inode number, to be used for
597 * resolving st_ino collisions in ovl_map_dev_ino().
599 * To avoid ino collision with legitimate xino values from upper
600 * layer (fsid 0), use the lowest xinobit to map the non
601 * persistent inode numbers to the unified st_ino address space.
603 if (S_ISDIR(inode
->i_mode
)) {
606 inode
->i_ino
&= ~0UL >> xinobits
;
607 inode
->i_ino
|= 1UL << xinoshift
;
612 void ovl_inode_init(struct inode
*inode
, struct ovl_inode_params
*oip
,
613 unsigned long ino
, int fsid
)
615 struct inode
*realinode
;
617 if (oip
->upperdentry
)
618 OVL_I(inode
)->__upperdentry
= oip
->upperdentry
;
619 if (oip
->lowerpath
&& oip
->lowerpath
->dentry
)
620 OVL_I(inode
)->lower
= igrab(d_inode(oip
->lowerpath
->dentry
));
622 OVL_I(inode
)->lowerdata
= igrab(d_inode(oip
->lowerdata
));
624 realinode
= ovl_inode_real(inode
);
625 ovl_copyattr(realinode
, inode
);
626 ovl_copyflags(realinode
, inode
);
627 ovl_map_ino(inode
, ino
, fsid
);
630 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
632 inode
->i_mode
= mode
;
633 inode
->i_flags
|= S_NOCMTIME
;
634 #ifdef CONFIG_FS_POSIX_ACL
635 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
638 ovl_lockdep_annotate_inode_mutex_key(inode
);
640 switch (mode
& S_IFMT
) {
642 inode
->i_op
= &ovl_file_inode_operations
;
643 inode
->i_fop
= &ovl_file_operations
;
644 inode
->i_mapping
->a_ops
= &ovl_aops
;
648 inode
->i_op
= &ovl_dir_inode_operations
;
649 inode
->i_fop
= &ovl_dir_operations
;
653 inode
->i_op
= &ovl_symlink_inode_operations
;
657 inode
->i_op
= &ovl_special_inode_operations
;
658 init_special_inode(inode
, mode
, rdev
);
664 * With inodes index enabled, an overlay inode nlink counts the union of upper
665 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
666 * upper inode, the following nlink modifying operations can happen:
668 * 1. Lower hardlink copy up
669 * 2. Upper hardlink created, unlinked or renamed over
670 * 3. Lower hardlink whiteout or renamed over
672 * For the first, copy up case, the union nlink does not change, whether the
673 * operation succeeds or fails, but the upper inode nlink may change.
674 * Therefore, before copy up, we store the union nlink value relative to the
675 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
677 * For the second, upper hardlink case, the union nlink should be incremented
678 * or decremented IFF the operation succeeds, aligned with nlink change of the
679 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
680 * value relative to the upper inode nlink in the index inode.
682 * For the last, lower cover up case, we simplify things by preceding the
683 * whiteout or cover up with copy up. This makes sure that there is an index
684 * upper inode where the nlink xattr can be stored before the copied up upper
687 #define OVL_NLINK_ADD_UPPER (1 << 0)
690 * On-disk format for indexed nlink:
692 * nlink relative to the upper inode - "U[+-]NUM"
693 * nlink relative to the lower inode - "L[+-]NUM"
696 static int ovl_set_nlink_common(struct dentry
*dentry
,
697 struct dentry
*realdentry
, const char *format
)
699 struct inode
*inode
= d_inode(dentry
);
700 struct inode
*realinode
= d_inode(realdentry
);
704 len
= snprintf(buf
, sizeof(buf
), format
,
705 (int) (inode
->i_nlink
- realinode
->i_nlink
));
707 if (WARN_ON(len
>= sizeof(buf
)))
710 return ovl_do_setxattr(ovl_dentry_upper(dentry
),
711 OVL_XATTR_NLINK
, buf
, len
, 0);
714 int ovl_set_nlink_upper(struct dentry
*dentry
)
716 return ovl_set_nlink_common(dentry
, ovl_dentry_upper(dentry
), "U%+i");
719 int ovl_set_nlink_lower(struct dentry
*dentry
)
721 return ovl_set_nlink_common(dentry
, ovl_dentry_lower(dentry
), "L%+i");
724 unsigned int ovl_get_nlink(struct dentry
*lowerdentry
,
725 struct dentry
*upperdentry
,
726 unsigned int fallback
)
733 if (!lowerdentry
|| !upperdentry
|| d_inode(lowerdentry
)->i_nlink
== 1)
736 err
= vfs_getxattr(upperdentry
, OVL_XATTR_NLINK
, &buf
, sizeof(buf
) - 1);
741 if ((buf
[0] != 'L' && buf
[0] != 'U') ||
742 (buf
[1] != '+' && buf
[1] != '-'))
745 err
= kstrtoint(buf
+ 1, 10, &nlink_diff
);
749 nlink
= d_inode(buf
[0] == 'L' ? lowerdentry
: upperdentry
)->i_nlink
;
758 pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
763 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
, dev_t rdev
)
767 inode
= new_inode(sb
);
769 ovl_fill_inode(inode
, mode
, rdev
);
774 static int ovl_inode_test(struct inode
*inode
, void *data
)
776 return inode
->i_private
== data
;
779 static int ovl_inode_set(struct inode
*inode
, void *data
)
781 inode
->i_private
= data
;
785 static bool ovl_verify_inode(struct inode
*inode
, struct dentry
*lowerdentry
,
786 struct dentry
*upperdentry
, bool strict
)
789 * For directories, @strict verify from lookup path performs consistency
790 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
791 * inode. Non @strict verify from NFS handle decode path passes NULL for
792 * 'unknown' lower/upper.
794 if (S_ISDIR(inode
->i_mode
) && strict
) {
795 /* Real lower dir moved to upper layer under us? */
796 if (!lowerdentry
&& ovl_inode_lower(inode
))
799 /* Lookup of an uncovered redirect origin? */
800 if (!upperdentry
&& ovl_inode_upper(inode
))
805 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
806 * This happens when finding a copied up overlay inode for a renamed
807 * or hardlinked overlay dentry and lower dentry cannot be followed
808 * by origin because lower fs does not support file handles.
810 if (lowerdentry
&& ovl_inode_lower(inode
) != d_inode(lowerdentry
))
814 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
815 * This happens when finding a lower alias for a copied up hard link.
817 if (upperdentry
&& ovl_inode_upper(inode
) != d_inode(upperdentry
))
823 struct inode
*ovl_lookup_inode(struct super_block
*sb
, struct dentry
*real
,
826 struct inode
*inode
, *key
= d_inode(real
);
828 inode
= ilookup5(sb
, (unsigned long) key
, ovl_inode_test
, key
);
832 if (!ovl_verify_inode(inode
, is_upper
? NULL
: real
,
833 is_upper
? real
: NULL
, false)) {
835 return ERR_PTR(-ESTALE
);
841 bool ovl_lookup_trap_inode(struct super_block
*sb
, struct dentry
*dir
)
843 struct inode
*key
= d_inode(dir
);
847 trap
= ilookup5(sb
, (unsigned long) key
, ovl_inode_test
, key
);
851 res
= IS_DEADDIR(trap
) && !ovl_inode_upper(trap
) &&
852 !ovl_inode_lower(trap
);
859 * Create an inode cache entry for layer root dir, that will intentionally
860 * fail ovl_verify_inode(), so any lookup that will find some layer root
863 struct inode
*ovl_get_trap_inode(struct super_block
*sb
, struct dentry
*dir
)
865 struct inode
*key
= d_inode(dir
);
869 return ERR_PTR(-ENOTDIR
);
871 trap
= iget5_locked(sb
, (unsigned long) key
, ovl_inode_test
,
874 return ERR_PTR(-ENOMEM
);
876 if (!(trap
->i_state
& I_NEW
)) {
877 /* Conflicting layer roots? */
879 return ERR_PTR(-ELOOP
);
882 trap
->i_mode
= S_IFDIR
;
883 trap
->i_flags
= S_DEAD
;
884 unlock_new_inode(trap
);
890 * Does overlay inode need to be hashed by lower inode?
892 static bool ovl_hash_bylower(struct super_block
*sb
, struct dentry
*upper
,
893 struct dentry
*lower
, struct dentry
*index
)
895 struct ovl_fs
*ofs
= sb
->s_fs_info
;
897 /* No, if pure upper */
901 /* Yes, if already indexed */
905 /* Yes, if won't be copied up */
909 /* No, if lower hardlink is or will be broken on copy up */
910 if ((upper
|| !ovl_indexdir(sb
)) &&
911 !d_is_dir(lower
) && d_inode(lower
)->i_nlink
> 1)
914 /* No, if non-indexed upper with NFS export */
915 if (sb
->s_export_op
&& upper
)
918 /* Otherwise, hash by lower inode for fsnotify */
922 static struct inode
*ovl_iget5(struct super_block
*sb
, struct inode
*newinode
,
925 return newinode
? inode_insert5(newinode
, (unsigned long) key
,
926 ovl_inode_test
, ovl_inode_set
, key
) :
927 iget5_locked(sb
, (unsigned long) key
,
928 ovl_inode_test
, ovl_inode_set
, key
);
931 struct inode
*ovl_get_inode(struct super_block
*sb
,
932 struct ovl_inode_params
*oip
)
934 struct dentry
*upperdentry
= oip
->upperdentry
;
935 struct ovl_path
*lowerpath
= oip
->lowerpath
;
936 struct inode
*realinode
= upperdentry
? d_inode(upperdentry
) : NULL
;
938 struct dentry
*lowerdentry
= lowerpath
? lowerpath
->dentry
: NULL
;
939 bool bylower
= ovl_hash_bylower(sb
, upperdentry
, lowerdentry
,
941 int fsid
= bylower
? lowerpath
->layer
->fsid
: 0;
942 bool is_dir
, metacopy
= false;
943 unsigned long ino
= 0;
944 int err
= oip
->newinode
? -EEXIST
: -ENOMEM
;
947 realinode
= d_inode(lowerdentry
);
950 * Copy up origin (lower) may exist for non-indexed upper, but we must
951 * not use lower as hash key if this is a broken hardlink.
953 is_dir
= S_ISDIR(realinode
->i_mode
);
954 if (upperdentry
|| bylower
) {
955 struct inode
*key
= d_inode(bylower
? lowerdentry
:
957 unsigned int nlink
= is_dir
? 1 : realinode
->i_nlink
;
959 inode
= ovl_iget5(sb
, oip
->newinode
, key
);
962 if (!(inode
->i_state
& I_NEW
)) {
964 * Verify that the underlying files stored in the inode
965 * match those in the dentry.
967 if (!ovl_verify_inode(inode
, lowerdentry
, upperdentry
,
975 kfree(oip
->redirect
);
979 /* Recalculate nlink for non-dir due to indexing */
981 nlink
= ovl_get_nlink(lowerdentry
, upperdentry
, nlink
);
982 set_nlink(inode
, nlink
);
985 /* Lower hardlink that will be broken on copy up */
986 inode
= new_inode(sb
);
991 ino
= realinode
->i_ino
;
992 fsid
= lowerpath
->layer
->fsid
;
994 ovl_fill_inode(inode
, realinode
->i_mode
, realinode
->i_rdev
);
995 ovl_inode_init(inode
, oip
, ino
, fsid
);
997 if (upperdentry
&& ovl_is_impuredir(upperdentry
))
998 ovl_set_flag(OVL_IMPURE
, inode
);
1001 ovl_set_flag(OVL_INDEX
, inode
);
1004 err
= ovl_check_metacopy_xattr(upperdentry
);
1009 ovl_set_flag(OVL_UPPERDATA
, inode
);
1012 OVL_I(inode
)->redirect
= oip
->redirect
;
1015 ovl_set_flag(OVL_CONST_INO
, inode
);
1017 /* Check for non-merge dir that may have whiteouts */
1019 if (((upperdentry
&& lowerdentry
) || oip
->numlower
> 1) ||
1020 ovl_check_origin_xattr(upperdentry
?: lowerdentry
)) {
1021 ovl_set_flag(OVL_WHITEOUTS
, inode
);
1025 if (inode
->i_state
& I_NEW
)
1026 unlock_new_inode(inode
);
1031 pr_warn_ratelimited("failed to get inode (%i)\n", err
);
1032 inode
= ERR_PTR(err
);