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
;
62 * We might have to translate ovl file into real file object
63 * once use cases emerge. For now, simply don't let underlying
64 * filesystem rely on attr->ia_file
66 attr
->ia_valid
&= ~ATTR_FILE
;
69 * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN
70 * set. Overlayfs does not pass O_TRUNC flag to underlying
71 * filesystem during open -> do not pass ATTR_OPEN. This
72 * disables optimization in fuse which assumes open(O_TRUNC)
73 * already set file size to 0. But we never passed O_TRUNC to
74 * fuse. So by clearing ATTR_OPEN, fuse will be forced to send
75 * setattr request to server.
77 attr
->ia_valid
&= ~ATTR_OPEN
;
79 inode_lock(upperdentry
->d_inode
);
80 old_cred
= ovl_override_creds(dentry
->d_sb
);
81 err
= notify_change(upperdentry
, attr
, NULL
);
82 revert_creds(old_cred
);
84 ovl_copyattr(upperdentry
->d_inode
, dentry
->d_inode
);
85 inode_unlock(upperdentry
->d_inode
);
88 put_write_access(winode
);
91 ovl_drop_write(dentry
);
96 static int ovl_map_dev_ino(struct dentry
*dentry
, struct kstat
*stat
, int fsid
)
98 bool samefs
= ovl_same_fs(dentry
->d_sb
);
99 unsigned int xinobits
= ovl_xino_bits(dentry
->d_sb
);
100 unsigned int xinoshift
= 64 - xinobits
;
104 * When all layers are on the same fs, all real inode
105 * number are unique, so we use the overlay st_dev,
106 * which is friendly to du -x.
108 stat
->dev
= dentry
->d_sb
->s_dev
;
110 } else if (xinobits
) {
112 * All inode numbers of underlying fs should not be using the
113 * high xinobits, so we use high xinobits to partition the
114 * overlay st_ino address space. The high bits holds the fsid
115 * (upper fsid is 0). The lowest xinobit is reserved for mapping
116 * the non-peresistent inode numbers range in case of overflow.
117 * This way all overlay inode numbers are unique and use the
120 if (likely(!(stat
->ino
>> xinoshift
))) {
121 stat
->ino
|= ((u64
)fsid
) << (xinoshift
+ 1);
122 stat
->dev
= dentry
->d_sb
->s_dev
;
124 } else if (ovl_xino_warn(dentry
->d_sb
)) {
125 pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
126 dentry
, stat
->ino
, xinobits
);
130 /* The inode could not be mapped to a unified st_ino address space */
131 if (S_ISDIR(dentry
->d_inode
->i_mode
)) {
133 * Always use the overlay st_dev for directories, so 'find
134 * -xdev' will scan the entire overlay mount and won't cross the
135 * overlay mount boundaries.
137 * If not all layers are on the same fs the pair {real st_ino;
138 * overlay st_dev} is not unique, so use the non persistent
139 * overlay st_ino for directories.
141 stat
->dev
= dentry
->d_sb
->s_dev
;
142 stat
->ino
= dentry
->d_inode
->i_ino
;
145 * For non-samefs setup, if we cannot map all layers st_ino
146 * to a unified address space, we need to make sure that st_dev
147 * is unique per underlying fs, so we use the unique anonymous
148 * bdev assigned to the underlying fs.
150 stat
->dev
= OVL_FS(dentry
->d_sb
)->fs
[fsid
].pseudo_dev
;
156 int ovl_getattr(const struct path
*path
, struct kstat
*stat
,
157 u32 request_mask
, unsigned int flags
)
159 struct dentry
*dentry
= path
->dentry
;
160 enum ovl_path_type type
;
161 struct path realpath
;
162 const struct cred
*old_cred
;
163 bool is_dir
= S_ISDIR(dentry
->d_inode
->i_mode
);
166 bool metacopy_blocks
= false;
168 metacopy_blocks
= ovl_is_metacopy_dentry(dentry
);
170 type
= ovl_path_real(dentry
, &realpath
);
171 old_cred
= ovl_override_creds(dentry
->d_sb
);
172 err
= vfs_getattr(&realpath
, stat
, request_mask
, flags
);
177 * For non-dir or same fs, we use st_ino of the copy up origin.
178 * This guaranties constant st_dev/st_ino across copy up.
179 * With xino feature and non-samefs, we use st_ino of the copy up
180 * origin masked with high bits that represent the layer id.
182 * If lower filesystem supports NFS file handles, this also guaranties
183 * persistent st_ino across mount cycle.
185 if (!is_dir
|| ovl_same_dev(dentry
->d_sb
)) {
186 if (!OVL_TYPE_UPPER(type
)) {
187 fsid
= ovl_layer_lower(dentry
)->fsid
;
188 } else if (OVL_TYPE_ORIGIN(type
)) {
189 struct kstat lowerstat
;
190 u32 lowermask
= STATX_INO
| STATX_BLOCKS
|
191 (!is_dir
? STATX_NLINK
: 0);
193 ovl_path_lower(dentry
, &realpath
);
194 err
= vfs_getattr(&realpath
, &lowerstat
,
200 * Lower hardlinks may be broken on copy up to different
201 * upper files, so we cannot use the lower origin st_ino
202 * for those different files, even for the same fs case.
204 * Similarly, several redirected dirs can point to the
205 * same dir on a lower layer. With the "verify_lower"
206 * feature, we do not use the lower origin st_ino, if
207 * we haven't verified that this redirect is unique.
209 * With inodes index enabled, it is safe to use st_ino
210 * of an indexed origin. The index validates that the
211 * upper hardlink is not broken and that a redirected
212 * dir is the only redirect to that origin.
214 if (ovl_test_flag(OVL_INDEX
, d_inode(dentry
)) ||
215 (!ovl_verify_lower(dentry
->d_sb
) &&
216 (is_dir
|| lowerstat
.nlink
== 1))) {
217 fsid
= ovl_layer_lower(dentry
)->fsid
;
218 stat
->ino
= lowerstat
.ino
;
222 * If we are querying a metacopy dentry and lower
223 * dentry is data dentry, then use the blocks we
224 * queried just now. We don't have to do additional
225 * vfs_getattr(). If lower itself is metacopy, then
226 * additional vfs_getattr() is unavoidable.
228 if (metacopy_blocks
&&
229 realpath
.dentry
== ovl_dentry_lowerdata(dentry
)) {
230 stat
->blocks
= lowerstat
.blocks
;
231 metacopy_blocks
= false;
235 if (metacopy_blocks
) {
237 * If lower is not same as lowerdata or if there was
238 * no origin on upper, we can end up here.
240 struct kstat lowerdatastat
;
241 u32 lowermask
= STATX_BLOCKS
;
243 ovl_path_lowerdata(dentry
, &realpath
);
244 err
= vfs_getattr(&realpath
, &lowerdatastat
,
248 stat
->blocks
= lowerdatastat
.blocks
;
252 err
= ovl_map_dev_ino(dentry
, stat
, fsid
);
257 * It's probably not worth it to count subdirs to get the
258 * correct link count. nlink=1 seems to pacify 'find' and
261 if (is_dir
&& OVL_TYPE_MERGE(type
))
265 * Return the overlay inode nlinks for indexed upper inodes.
266 * Overlay inode nlink counts the union of the upper hardlinks
267 * and non-covered lower hardlinks. It does not include the upper
270 if (!is_dir
&& ovl_test_flag(OVL_INDEX
, d_inode(dentry
)))
271 stat
->nlink
= dentry
->d_inode
->i_nlink
;
274 revert_creds(old_cred
);
279 int ovl_permission(struct inode
*inode
, int mask
)
281 struct inode
*upperinode
= ovl_inode_upper(inode
);
282 struct inode
*realinode
= upperinode
?: ovl_inode_lower(inode
);
283 const struct cred
*old_cred
;
286 /* Careful in RCU walk mode */
288 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
293 * Check overlay inode with the creds of task and underlying inode
294 * with creds of mounter
296 err
= generic_permission(inode
, mask
);
300 old_cred
= ovl_override_creds(inode
->i_sb
);
302 !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
303 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
304 /* Make sure mounter can read file for copy up later */
307 err
= inode_permission(realinode
, mask
);
308 revert_creds(old_cred
);
313 static const char *ovl_get_link(struct dentry
*dentry
,
315 struct delayed_call
*done
)
317 const struct cred
*old_cred
;
321 return ERR_PTR(-ECHILD
);
323 old_cred
= ovl_override_creds(dentry
->d_sb
);
324 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
325 revert_creds(old_cred
);
329 bool ovl_is_private_xattr(const char *name
)
331 return strncmp(name
, OVL_XATTR_PREFIX
,
332 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
335 int ovl_xattr_set(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
336 const void *value
, size_t size
, int flags
)
339 struct dentry
*upperdentry
= ovl_i_dentry_upper(inode
);
340 struct dentry
*realdentry
= upperdentry
?: ovl_dentry_lower(dentry
);
341 const struct cred
*old_cred
;
343 err
= ovl_want_write(dentry
);
347 if (!value
&& !upperdentry
) {
348 err
= vfs_getxattr(realdentry
, name
, NULL
, 0);
354 err
= ovl_copy_up(dentry
);
358 realdentry
= ovl_dentry_upper(dentry
);
361 old_cred
= ovl_override_creds(dentry
->d_sb
);
363 err
= vfs_setxattr(realdentry
, name
, value
, size
, flags
);
365 WARN_ON(flags
!= XATTR_REPLACE
);
366 err
= vfs_removexattr(realdentry
, name
);
368 revert_creds(old_cred
);
371 ovl_copyattr(d_inode(realdentry
), inode
);
374 ovl_drop_write(dentry
);
379 int ovl_xattr_get(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
380 void *value
, size_t size
)
383 const struct cred
*old_cred
;
384 struct dentry
*realdentry
=
385 ovl_i_dentry_upper(inode
) ?: ovl_dentry_lower(dentry
);
387 old_cred
= ovl_override_creds(dentry
->d_sb
);
388 res
= vfs_getxattr(realdentry
, name
, value
, size
);
389 revert_creds(old_cred
);
393 static bool ovl_can_list(const char *s
)
395 /* List all non-trusted xatts */
396 if (strncmp(s
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) != 0)
399 /* Never list trusted.overlay, list other trusted for superuser only */
400 return !ovl_is_private_xattr(s
) &&
401 ns_capable_noaudit(&init_user_ns
, CAP_SYS_ADMIN
);
404 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
406 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
410 const struct cred
*old_cred
;
412 old_cred
= ovl_override_creds(dentry
->d_sb
);
413 res
= vfs_listxattr(realdentry
, list
, size
);
414 revert_creds(old_cred
);
415 if (res
<= 0 || size
== 0)
418 /* filter out private xattrs */
419 for (s
= list
, len
= res
; len
;) {
420 size_t slen
= strnlen(s
, len
) + 1;
422 /* underlying fs providing us with an broken xattr list? */
423 if (WARN_ON(slen
> len
))
427 if (!ovl_can_list(s
)) {
429 memmove(s
, s
+ slen
, len
);
438 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
440 struct inode
*realinode
= ovl_inode_real(inode
);
441 const struct cred
*old_cred
;
442 struct posix_acl
*acl
;
444 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
447 old_cred
= ovl_override_creds(inode
->i_sb
);
448 acl
= get_acl(realinode
, type
);
449 revert_creds(old_cred
);
454 int ovl_update_time(struct inode
*inode
, struct timespec64
*ts
, int flags
)
456 if (flags
& S_ATIME
) {
457 struct ovl_fs
*ofs
= inode
->i_sb
->s_fs_info
;
458 struct path upperpath
= {
459 .mnt
= ofs
->upper_mnt
,
460 .dentry
= ovl_upperdentry_dereference(OVL_I(inode
)),
463 if (upperpath
.dentry
) {
464 touch_atime(&upperpath
);
465 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
471 static int ovl_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
475 struct inode
*realinode
= ovl_inode_real(inode
);
476 const struct cred
*old_cred
;
478 if (!realinode
->i_op
->fiemap
)
481 old_cred
= ovl_override_creds(inode
->i_sb
);
483 if (fieinfo
->fi_flags
& FIEMAP_FLAG_SYNC
)
484 filemap_write_and_wait(realinode
->i_mapping
);
486 err
= realinode
->i_op
->fiemap(realinode
, fieinfo
, start
, len
);
487 revert_creds(old_cred
);
492 static const struct inode_operations ovl_file_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
,
499 .fiemap
= ovl_fiemap
,
502 static const struct inode_operations ovl_symlink_inode_operations
= {
503 .setattr
= ovl_setattr
,
504 .get_link
= ovl_get_link
,
505 .getattr
= ovl_getattr
,
506 .listxattr
= ovl_listxattr
,
507 .update_time
= ovl_update_time
,
510 static const struct inode_operations ovl_special_inode_operations
= {
511 .setattr
= ovl_setattr
,
512 .permission
= ovl_permission
,
513 .getattr
= ovl_getattr
,
514 .listxattr
= ovl_listxattr
,
515 .get_acl
= ovl_get_acl
,
516 .update_time
= ovl_update_time
,
519 static const struct address_space_operations ovl_aops
= {
520 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
521 .direct_IO
= noop_direct_IO
,
525 * It is possible to stack overlayfs instance on top of another
526 * overlayfs instance as lower layer. We need to annotate the
527 * stackable i_mutex locks according to stack level of the super
528 * block instance. An overlayfs instance can never be in stack
529 * depth 0 (there is always a real fs below it). An overlayfs
530 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
532 * For example, here is a snip from /proc/lockdep_chains after
533 * dir_iterate of nested overlayfs:
535 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
536 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
537 * [...] &type->i_mutex_dir_key (stack_depth=0)
539 * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
541 * This chain is valid:
542 * - inode->i_rwsem (inode_lock[2])
543 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
544 * - OVL_I(inode)->lock (ovl_inode_lock[2])
545 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
547 * And this chain is valid:
548 * - inode->i_rwsem (inode_lock[2])
549 * - OVL_I(inode)->lock (ovl_inode_lock[2])
550 * - lowerinode->i_rwsem (inode_lock[1])
551 * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
553 * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
554 * held, because it is in reverse order of the non-nested case using the same
556 * - inode->i_rwsem (inode_lock[1])
557 * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
558 * - OVL_I(inode)->lock (ovl_inode_lock[1])
560 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
562 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode
*inode
)
564 #ifdef CONFIG_LOCKDEP
565 static struct lock_class_key ovl_i_mutex_key
[OVL_MAX_NESTING
];
566 static struct lock_class_key ovl_i_mutex_dir_key
[OVL_MAX_NESTING
];
567 static struct lock_class_key ovl_i_lock_key
[OVL_MAX_NESTING
];
569 int depth
= inode
->i_sb
->s_stack_depth
- 1;
571 if (WARN_ON_ONCE(depth
< 0 || depth
>= OVL_MAX_NESTING
))
574 if (S_ISDIR(inode
->i_mode
))
575 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_dir_key
[depth
]);
577 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_key
[depth
]);
579 lockdep_set_class(&OVL_I(inode
)->lock
, &ovl_i_lock_key
[depth
]);
583 static void ovl_next_ino(struct inode
*inode
)
585 struct ovl_fs
*ofs
= inode
->i_sb
->s_fs_info
;
587 inode
->i_ino
= atomic_long_inc_return(&ofs
->last_ino
);
588 if (unlikely(!inode
->i_ino
))
589 inode
->i_ino
= atomic_long_inc_return(&ofs
->last_ino
);
592 static void ovl_map_ino(struct inode
*inode
, unsigned long ino
, int fsid
)
594 int xinobits
= ovl_xino_bits(inode
->i_sb
);
595 unsigned int xinoshift
= 64 - xinobits
;
598 * When d_ino is consistent with st_ino (samefs or i_ino has enough
599 * bits to encode layer), set the same value used for st_ino to i_ino,
600 * so inode number exposed via /proc/locks and a like will be
601 * consistent with d_ino and st_ino values. An i_ino value inconsistent
602 * with d_ino also causes nfsd readdirplus to fail.
605 if (ovl_same_fs(inode
->i_sb
)) {
607 } else if (xinobits
&& likely(!(ino
>> xinoshift
))) {
608 inode
->i_ino
|= (unsigned long)fsid
<< (xinoshift
+ 1);
613 * For directory inodes on non-samefs with xino disabled or xino
614 * overflow, we allocate a non-persistent inode number, to be used for
615 * resolving st_ino collisions in ovl_map_dev_ino().
617 * To avoid ino collision with legitimate xino values from upper
618 * layer (fsid 0), use the lowest xinobit to map the non
619 * persistent inode numbers to the unified st_ino address space.
621 if (S_ISDIR(inode
->i_mode
)) {
624 inode
->i_ino
&= ~0UL >> xinobits
;
625 inode
->i_ino
|= 1UL << xinoshift
;
630 void ovl_inode_init(struct inode
*inode
, struct ovl_inode_params
*oip
,
631 unsigned long ino
, int fsid
)
633 struct inode
*realinode
;
635 if (oip
->upperdentry
)
636 OVL_I(inode
)->__upperdentry
= oip
->upperdentry
;
637 if (oip
->lowerpath
&& oip
->lowerpath
->dentry
)
638 OVL_I(inode
)->lower
= igrab(d_inode(oip
->lowerpath
->dentry
));
640 OVL_I(inode
)->lowerdata
= igrab(d_inode(oip
->lowerdata
));
642 realinode
= ovl_inode_real(inode
);
643 ovl_copyattr(realinode
, inode
);
644 ovl_copyflags(realinode
, inode
);
645 ovl_map_ino(inode
, ino
, fsid
);
648 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
650 inode
->i_mode
= mode
;
651 inode
->i_flags
|= S_NOCMTIME
;
652 #ifdef CONFIG_FS_POSIX_ACL
653 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
656 ovl_lockdep_annotate_inode_mutex_key(inode
);
658 switch (mode
& S_IFMT
) {
660 inode
->i_op
= &ovl_file_inode_operations
;
661 inode
->i_fop
= &ovl_file_operations
;
662 inode
->i_mapping
->a_ops
= &ovl_aops
;
666 inode
->i_op
= &ovl_dir_inode_operations
;
667 inode
->i_fop
= &ovl_dir_operations
;
671 inode
->i_op
= &ovl_symlink_inode_operations
;
675 inode
->i_op
= &ovl_special_inode_operations
;
676 init_special_inode(inode
, mode
, rdev
);
682 * With inodes index enabled, an overlay inode nlink counts the union of upper
683 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
684 * upper inode, the following nlink modifying operations can happen:
686 * 1. Lower hardlink copy up
687 * 2. Upper hardlink created, unlinked or renamed over
688 * 3. Lower hardlink whiteout or renamed over
690 * For the first, copy up case, the union nlink does not change, whether the
691 * operation succeeds or fails, but the upper inode nlink may change.
692 * Therefore, before copy up, we store the union nlink value relative to the
693 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
695 * For the second, upper hardlink case, the union nlink should be incremented
696 * or decremented IFF the operation succeeds, aligned with nlink change of the
697 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
698 * value relative to the upper inode nlink in the index inode.
700 * For the last, lower cover up case, we simplify things by preceding the
701 * whiteout or cover up with copy up. This makes sure that there is an index
702 * upper inode where the nlink xattr can be stored before the copied up upper
705 #define OVL_NLINK_ADD_UPPER (1 << 0)
708 * On-disk format for indexed nlink:
710 * nlink relative to the upper inode - "U[+-]NUM"
711 * nlink relative to the lower inode - "L[+-]NUM"
714 static int ovl_set_nlink_common(struct dentry
*dentry
,
715 struct dentry
*realdentry
, const char *format
)
717 struct inode
*inode
= d_inode(dentry
);
718 struct inode
*realinode
= d_inode(realdentry
);
722 len
= snprintf(buf
, sizeof(buf
), format
,
723 (int) (inode
->i_nlink
- realinode
->i_nlink
));
725 if (WARN_ON(len
>= sizeof(buf
)))
728 return ovl_do_setxattr(ovl_dentry_upper(dentry
),
729 OVL_XATTR_NLINK
, buf
, len
, 0);
732 int ovl_set_nlink_upper(struct dentry
*dentry
)
734 return ovl_set_nlink_common(dentry
, ovl_dentry_upper(dentry
), "U%+i");
737 int ovl_set_nlink_lower(struct dentry
*dentry
)
739 return ovl_set_nlink_common(dentry
, ovl_dentry_lower(dentry
), "L%+i");
742 unsigned int ovl_get_nlink(struct dentry
*lowerdentry
,
743 struct dentry
*upperdentry
,
744 unsigned int fallback
)
751 if (!lowerdentry
|| !upperdentry
|| d_inode(lowerdentry
)->i_nlink
== 1)
754 err
= vfs_getxattr(upperdentry
, OVL_XATTR_NLINK
, &buf
, sizeof(buf
) - 1);
759 if ((buf
[0] != 'L' && buf
[0] != 'U') ||
760 (buf
[1] != '+' && buf
[1] != '-'))
763 err
= kstrtoint(buf
+ 1, 10, &nlink_diff
);
767 nlink
= d_inode(buf
[0] == 'L' ? lowerdentry
: upperdentry
)->i_nlink
;
776 pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
781 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
, dev_t rdev
)
785 inode
= new_inode(sb
);
787 ovl_fill_inode(inode
, mode
, rdev
);
792 static int ovl_inode_test(struct inode
*inode
, void *data
)
794 return inode
->i_private
== data
;
797 static int ovl_inode_set(struct inode
*inode
, void *data
)
799 inode
->i_private
= data
;
803 static bool ovl_verify_inode(struct inode
*inode
, struct dentry
*lowerdentry
,
804 struct dentry
*upperdentry
, bool strict
)
807 * For directories, @strict verify from lookup path performs consistency
808 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
809 * inode. Non @strict verify from NFS handle decode path passes NULL for
810 * 'unknown' lower/upper.
812 if (S_ISDIR(inode
->i_mode
) && strict
) {
813 /* Real lower dir moved to upper layer under us? */
814 if (!lowerdentry
&& ovl_inode_lower(inode
))
817 /* Lookup of an uncovered redirect origin? */
818 if (!upperdentry
&& ovl_inode_upper(inode
))
823 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
824 * This happens when finding a copied up overlay inode for a renamed
825 * or hardlinked overlay dentry and lower dentry cannot be followed
826 * by origin because lower fs does not support file handles.
828 if (lowerdentry
&& ovl_inode_lower(inode
) != d_inode(lowerdentry
))
832 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
833 * This happens when finding a lower alias for a copied up hard link.
835 if (upperdentry
&& ovl_inode_upper(inode
) != d_inode(upperdentry
))
841 struct inode
*ovl_lookup_inode(struct super_block
*sb
, struct dentry
*real
,
844 struct inode
*inode
, *key
= d_inode(real
);
846 inode
= ilookup5(sb
, (unsigned long) key
, ovl_inode_test
, key
);
850 if (!ovl_verify_inode(inode
, is_upper
? NULL
: real
,
851 is_upper
? real
: NULL
, false)) {
853 return ERR_PTR(-ESTALE
);
859 bool ovl_lookup_trap_inode(struct super_block
*sb
, struct dentry
*dir
)
861 struct inode
*key
= d_inode(dir
);
865 trap
= ilookup5(sb
, (unsigned long) key
, ovl_inode_test
, key
);
869 res
= IS_DEADDIR(trap
) && !ovl_inode_upper(trap
) &&
870 !ovl_inode_lower(trap
);
877 * Create an inode cache entry for layer root dir, that will intentionally
878 * fail ovl_verify_inode(), so any lookup that will find some layer root
881 struct inode
*ovl_get_trap_inode(struct super_block
*sb
, struct dentry
*dir
)
883 struct inode
*key
= d_inode(dir
);
887 return ERR_PTR(-ENOTDIR
);
889 trap
= iget5_locked(sb
, (unsigned long) key
, ovl_inode_test
,
892 return ERR_PTR(-ENOMEM
);
894 if (!(trap
->i_state
& I_NEW
)) {
895 /* Conflicting layer roots? */
897 return ERR_PTR(-ELOOP
);
900 trap
->i_mode
= S_IFDIR
;
901 trap
->i_flags
= S_DEAD
;
902 unlock_new_inode(trap
);
908 * Does overlay inode need to be hashed by lower inode?
910 static bool ovl_hash_bylower(struct super_block
*sb
, struct dentry
*upper
,
911 struct dentry
*lower
, struct dentry
*index
)
913 struct ovl_fs
*ofs
= sb
->s_fs_info
;
915 /* No, if pure upper */
919 /* Yes, if already indexed */
923 /* Yes, if won't be copied up */
927 /* No, if lower hardlink is or will be broken on copy up */
928 if ((upper
|| !ovl_indexdir(sb
)) &&
929 !d_is_dir(lower
) && d_inode(lower
)->i_nlink
> 1)
932 /* No, if non-indexed upper with NFS export */
933 if (sb
->s_export_op
&& upper
)
936 /* Otherwise, hash by lower inode for fsnotify */
940 static struct inode
*ovl_iget5(struct super_block
*sb
, struct inode
*newinode
,
943 return newinode
? inode_insert5(newinode
, (unsigned long) key
,
944 ovl_inode_test
, ovl_inode_set
, key
) :
945 iget5_locked(sb
, (unsigned long) key
,
946 ovl_inode_test
, ovl_inode_set
, key
);
949 struct inode
*ovl_get_inode(struct super_block
*sb
,
950 struct ovl_inode_params
*oip
)
952 struct dentry
*upperdentry
= oip
->upperdentry
;
953 struct ovl_path
*lowerpath
= oip
->lowerpath
;
954 struct inode
*realinode
= upperdentry
? d_inode(upperdentry
) : NULL
;
956 struct dentry
*lowerdentry
= lowerpath
? lowerpath
->dentry
: NULL
;
957 bool bylower
= ovl_hash_bylower(sb
, upperdentry
, lowerdentry
,
959 int fsid
= bylower
? lowerpath
->layer
->fsid
: 0;
960 bool is_dir
, metacopy
= false;
961 unsigned long ino
= 0;
962 int err
= oip
->newinode
? -EEXIST
: -ENOMEM
;
965 realinode
= d_inode(lowerdentry
);
968 * Copy up origin (lower) may exist for non-indexed upper, but we must
969 * not use lower as hash key if this is a broken hardlink.
971 is_dir
= S_ISDIR(realinode
->i_mode
);
972 if (upperdentry
|| bylower
) {
973 struct inode
*key
= d_inode(bylower
? lowerdentry
:
975 unsigned int nlink
= is_dir
? 1 : realinode
->i_nlink
;
977 inode
= ovl_iget5(sb
, oip
->newinode
, key
);
980 if (!(inode
->i_state
& I_NEW
)) {
982 * Verify that the underlying files stored in the inode
983 * match those in the dentry.
985 if (!ovl_verify_inode(inode
, lowerdentry
, upperdentry
,
993 kfree(oip
->redirect
);
997 /* Recalculate nlink for non-dir due to indexing */
999 nlink
= ovl_get_nlink(lowerdentry
, upperdentry
, nlink
);
1000 set_nlink(inode
, nlink
);
1003 /* Lower hardlink that will be broken on copy up */
1004 inode
= new_inode(sb
);
1009 ino
= realinode
->i_ino
;
1010 fsid
= lowerpath
->layer
->fsid
;
1012 ovl_fill_inode(inode
, realinode
->i_mode
, realinode
->i_rdev
);
1013 ovl_inode_init(inode
, oip
, ino
, fsid
);
1015 if (upperdentry
&& ovl_is_impuredir(upperdentry
))
1016 ovl_set_flag(OVL_IMPURE
, inode
);
1019 ovl_set_flag(OVL_INDEX
, inode
);
1022 err
= ovl_check_metacopy_xattr(upperdentry
);
1027 ovl_set_flag(OVL_UPPERDATA
, inode
);
1030 OVL_I(inode
)->redirect
= oip
->redirect
;
1033 ovl_set_flag(OVL_CONST_INO
, inode
);
1035 /* Check for non-merge dir that may have whiteouts */
1037 if (((upperdentry
&& lowerdentry
) || oip
->numlower
> 1) ||
1038 ovl_check_origin_xattr(upperdentry
?: lowerdentry
)) {
1039 ovl_set_flag(OVL_WHITEOUTS
, inode
);
1043 if (inode
->i_state
& I_NEW
)
1044 unlock_new_inode(inode
);
1049 pr_warn_ratelimited("failed to get inode (%i)\n", err
);
1050 inode
= ERR_PTR(err
);