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 static dev_t
ovl_get_pseudo_dev(struct dentry
*dentry
)
21 struct ovl_entry
*oe
= dentry
->d_fsdata
;
23 return oe
->lowerstack
[0].layer
->pseudo_dev
;
26 int ovl_setattr(struct dentry
*dentry
, struct iattr
*attr
)
29 struct dentry
*upperdentry
;
30 const struct cred
*old_cred
;
33 * Check for permissions before trying to copy-up. This is redundant
34 * since it will be rechecked later by ->setattr() on upper dentry. But
35 * without this, copy-up can be triggered by just about anybody.
37 * We don't initialize inode->size, which just means that
38 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
39 * check for a swapfile (which this won't be anyway).
41 err
= setattr_prepare(dentry
, attr
);
45 err
= ovl_want_write(dentry
);
49 err
= ovl_copy_up(dentry
);
51 upperdentry
= ovl_dentry_upper(dentry
);
53 if (attr
->ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
54 attr
->ia_valid
&= ~ATTR_MODE
;
56 inode_lock(upperdentry
->d_inode
);
57 old_cred
= ovl_override_creds(dentry
->d_sb
);
58 err
= notify_change(upperdentry
, attr
, NULL
);
59 revert_creds(old_cred
);
61 ovl_copyattr(upperdentry
->d_inode
, dentry
->d_inode
);
62 inode_unlock(upperdentry
->d_inode
);
64 ovl_drop_write(dentry
);
69 int ovl_getattr(const struct path
*path
, struct kstat
*stat
,
70 u32 request_mask
, unsigned int flags
)
72 struct dentry
*dentry
= path
->dentry
;
73 enum ovl_path_type type
;
75 const struct cred
*old_cred
;
76 bool is_dir
= S_ISDIR(dentry
->d_inode
->i_mode
);
77 bool samefs
= ovl_same_sb(dentry
->d_sb
);
80 type
= ovl_path_real(dentry
, &realpath
);
81 old_cred
= ovl_override_creds(dentry
->d_sb
);
82 err
= vfs_getattr(&realpath
, stat
, request_mask
, flags
);
87 * For non-dir or same fs, we use st_ino of the copy up origin, if we
88 * know it. This guaranties constant st_dev/st_ino across copy up.
90 * If filesystem supports NFS export ops, this also guaranties
91 * persistent st_ino across mount cycle.
93 if (!is_dir
|| samefs
) {
94 if (OVL_TYPE_ORIGIN(type
)) {
95 struct kstat lowerstat
;
96 u32 lowermask
= STATX_INO
| (!is_dir
? STATX_NLINK
: 0);
98 ovl_path_lower(dentry
, &realpath
);
99 err
= vfs_getattr(&realpath
, &lowerstat
,
105 * Lower hardlinks may be broken on copy up to different
106 * upper files, so we cannot use the lower origin st_ino
107 * for those different files, even for the same fs case.
109 * Similarly, several redirected dirs can point to the
110 * same dir on a lower layer. With the "verify_lower"
111 * feature, we do not use the lower origin st_ino, if
112 * we haven't verified that this redirect is unique.
114 * With inodes index enabled, it is safe to use st_ino
115 * of an indexed origin. The index validates that the
116 * upper hardlink is not broken and that a redirected
117 * dir is the only redirect to that origin.
119 if (ovl_test_flag(OVL_INDEX
, d_inode(dentry
)) ||
120 (!ovl_verify_lower(dentry
->d_sb
) &&
121 (is_dir
|| lowerstat
.nlink
== 1))) {
122 stat
->ino
= lowerstat
.ino
;
123 stat
->dev
= ovl_get_pseudo_dev(dentry
);
128 * When all layers are on the same fs, all real inode
129 * number are unique, so we use the overlay st_dev,
130 * which is friendly to du -x.
132 stat
->dev
= dentry
->d_sb
->s_dev
;
133 } else if (!OVL_TYPE_UPPER(type
)) {
135 * For non-samefs setup, to make sure that st_dev/st_ino
136 * pair is unique across the system, we use a unique
137 * anonymous st_dev for lower layer inode.
139 stat
->dev
= ovl_get_pseudo_dev(dentry
);
143 * Always use the overlay st_dev for directories, so 'find
144 * -xdev' will scan the entire overlay mount and won't cross the
145 * overlay mount boundaries.
147 * If not all layers are on the same fs the pair {real st_ino;
148 * overlay st_dev} is not unique, so use the non persistent
149 * overlay st_ino for directories.
151 stat
->dev
= dentry
->d_sb
->s_dev
;
152 stat
->ino
= dentry
->d_inode
->i_ino
;
156 * It's probably not worth it to count subdirs to get the
157 * correct link count. nlink=1 seems to pacify 'find' and
160 if (is_dir
&& OVL_TYPE_MERGE(type
))
164 * Return the overlay inode nlinks for indexed upper inodes.
165 * Overlay inode nlink counts the union of the upper hardlinks
166 * and non-covered lower hardlinks. It does not include the upper
169 if (!is_dir
&& ovl_test_flag(OVL_INDEX
, d_inode(dentry
)))
170 stat
->nlink
= dentry
->d_inode
->i_nlink
;
173 revert_creds(old_cred
);
178 int ovl_permission(struct inode
*inode
, int mask
)
180 struct inode
*upperinode
= ovl_inode_upper(inode
);
181 struct inode
*realinode
= upperinode
?: ovl_inode_lower(inode
);
182 const struct cred
*old_cred
;
185 /* Careful in RCU walk mode */
187 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
192 * Check overlay inode with the creds of task and underlying inode
193 * with creds of mounter
195 err
= generic_permission(inode
, mask
);
199 old_cred
= ovl_override_creds(inode
->i_sb
);
201 !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
202 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
203 /* Make sure mounter can read file for copy up later */
206 err
= inode_permission(realinode
, mask
);
207 revert_creds(old_cred
);
212 static const char *ovl_get_link(struct dentry
*dentry
,
214 struct delayed_call
*done
)
216 const struct cred
*old_cred
;
220 return ERR_PTR(-ECHILD
);
222 old_cred
= ovl_override_creds(dentry
->d_sb
);
223 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
224 revert_creds(old_cred
);
228 bool ovl_is_private_xattr(const char *name
)
230 return strncmp(name
, OVL_XATTR_PREFIX
,
231 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
234 int ovl_xattr_set(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
235 const void *value
, size_t size
, int flags
)
238 struct dentry
*upperdentry
= ovl_i_dentry_upper(inode
);
239 struct dentry
*realdentry
= upperdentry
?: ovl_dentry_lower(dentry
);
240 const struct cred
*old_cred
;
242 err
= ovl_want_write(dentry
);
246 if (!value
&& !upperdentry
) {
247 err
= vfs_getxattr(realdentry
, name
, NULL
, 0);
253 err
= ovl_copy_up(dentry
);
257 realdentry
= ovl_dentry_upper(dentry
);
260 old_cred
= ovl_override_creds(dentry
->d_sb
);
262 err
= vfs_setxattr(realdentry
, name
, value
, size
, flags
);
264 WARN_ON(flags
!= XATTR_REPLACE
);
265 err
= vfs_removexattr(realdentry
, name
);
267 revert_creds(old_cred
);
270 ovl_drop_write(dentry
);
275 int ovl_xattr_get(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
276 void *value
, size_t size
)
279 const struct cred
*old_cred
;
280 struct dentry
*realdentry
=
281 ovl_i_dentry_upper(inode
) ?: ovl_dentry_lower(dentry
);
283 old_cred
= ovl_override_creds(dentry
->d_sb
);
284 res
= vfs_getxattr(realdentry
, name
, value
, size
);
285 revert_creds(old_cred
);
289 static bool ovl_can_list(const char *s
)
291 /* List all non-trusted xatts */
292 if (strncmp(s
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) != 0)
295 /* Never list trusted.overlay, list other trusted for superuser only */
296 return !ovl_is_private_xattr(s
) && capable(CAP_SYS_ADMIN
);
299 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
301 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
305 const struct cred
*old_cred
;
307 old_cred
= ovl_override_creds(dentry
->d_sb
);
308 res
= vfs_listxattr(realdentry
, list
, size
);
309 revert_creds(old_cred
);
310 if (res
<= 0 || size
== 0)
313 /* filter out private xattrs */
314 for (s
= list
, len
= res
; len
;) {
315 size_t slen
= strnlen(s
, len
) + 1;
317 /* underlying fs providing us with an broken xattr list? */
318 if (WARN_ON(slen
> len
))
322 if (!ovl_can_list(s
)) {
324 memmove(s
, s
+ slen
, len
);
333 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
335 struct inode
*realinode
= ovl_inode_real(inode
);
336 const struct cred
*old_cred
;
337 struct posix_acl
*acl
;
339 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
342 old_cred
= ovl_override_creds(inode
->i_sb
);
343 acl
= get_acl(realinode
, type
);
344 revert_creds(old_cred
);
349 static bool ovl_open_need_copy_up(struct dentry
*dentry
, int flags
)
351 /* Copy up of disconnected dentry does not set upper alias */
352 if (ovl_dentry_upper(dentry
) &&
353 (ovl_dentry_has_upper_alias(dentry
) ||
354 (dentry
->d_flags
& DCACHE_DISCONNECTED
)))
357 if (special_file(d_inode(dentry
)->i_mode
))
360 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
366 int ovl_open_maybe_copy_up(struct dentry
*dentry
, unsigned int file_flags
)
370 if (ovl_open_need_copy_up(dentry
, file_flags
)) {
371 err
= ovl_want_write(dentry
);
373 err
= ovl_copy_up_flags(dentry
, file_flags
);
374 ovl_drop_write(dentry
);
381 int ovl_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
383 struct dentry
*alias
;
384 struct path upperpath
;
386 if (!(flags
& S_ATIME
))
389 alias
= d_find_any_alias(inode
);
393 ovl_path_upper(alias
, &upperpath
);
394 if (upperpath
.dentry
) {
395 touch_atime(&upperpath
);
396 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
404 static const struct inode_operations ovl_file_inode_operations
= {
405 .setattr
= ovl_setattr
,
406 .permission
= ovl_permission
,
407 .getattr
= ovl_getattr
,
408 .listxattr
= ovl_listxattr
,
409 .get_acl
= ovl_get_acl
,
410 .update_time
= ovl_update_time
,
413 static const struct inode_operations ovl_symlink_inode_operations
= {
414 .setattr
= ovl_setattr
,
415 .get_link
= ovl_get_link
,
416 .getattr
= ovl_getattr
,
417 .listxattr
= ovl_listxattr
,
418 .update_time
= ovl_update_time
,
422 * It is possible to stack overlayfs instance on top of another
423 * overlayfs instance as lower layer. We need to annonate the
424 * stackable i_mutex locks according to stack level of the super
425 * block instance. An overlayfs instance can never be in stack
426 * depth 0 (there is always a real fs below it). An overlayfs
427 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
429 * For example, here is a snip from /proc/lockdep_chains after
430 * dir_iterate of nested overlayfs:
432 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
433 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
434 * [...] &type->i_mutex_dir_key (stack_depth=0)
436 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
438 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode
*inode
)
440 #ifdef CONFIG_LOCKDEP
441 static struct lock_class_key ovl_i_mutex_key
[OVL_MAX_NESTING
];
442 static struct lock_class_key ovl_i_mutex_dir_key
[OVL_MAX_NESTING
];
443 static struct lock_class_key ovl_i_lock_key
[OVL_MAX_NESTING
];
445 int depth
= inode
->i_sb
->s_stack_depth
- 1;
447 if (WARN_ON_ONCE(depth
< 0 || depth
>= OVL_MAX_NESTING
))
450 if (S_ISDIR(inode
->i_mode
))
451 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_dir_key
[depth
]);
453 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_key
[depth
]);
455 lockdep_set_class(&OVL_I(inode
)->lock
, &ovl_i_lock_key
[depth
]);
459 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
,
463 * When NFS export is enabled and d_ino is consistent with st_ino
464 * (samefs), set the same value to i_ino, because nfsd readdirplus
465 * compares d_ino values to i_ino values of child entries. When called
466 * from ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
467 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
469 if (inode
->i_sb
->s_export_op
&& ovl_same_sb(inode
->i_sb
))
472 inode
->i_ino
= get_next_ino();
473 inode
->i_mode
= mode
;
474 inode
->i_flags
|= S_NOCMTIME
;
475 #ifdef CONFIG_FS_POSIX_ACL
476 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
479 ovl_lockdep_annotate_inode_mutex_key(inode
);
481 switch (mode
& S_IFMT
) {
483 inode
->i_op
= &ovl_file_inode_operations
;
487 inode
->i_op
= &ovl_dir_inode_operations
;
488 inode
->i_fop
= &ovl_dir_operations
;
492 inode
->i_op
= &ovl_symlink_inode_operations
;
496 inode
->i_op
= &ovl_file_inode_operations
;
497 init_special_inode(inode
, mode
, rdev
);
503 * With inodes index enabled, an overlay inode nlink counts the union of upper
504 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
505 * upper inode, the following nlink modifying operations can happen:
507 * 1. Lower hardlink copy up
508 * 2. Upper hardlink created, unlinked or renamed over
509 * 3. Lower hardlink whiteout or renamed over
511 * For the first, copy up case, the union nlink does not change, whether the
512 * operation succeeds or fails, but the upper inode nlink may change.
513 * Therefore, before copy up, we store the union nlink value relative to the
514 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
516 * For the second, upper hardlink case, the union nlink should be incremented
517 * or decremented IFF the operation succeeds, aligned with nlink change of the
518 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
519 * value relative to the upper inode nlink in the index inode.
521 * For the last, lower cover up case, we simplify things by preceding the
522 * whiteout or cover up with copy up. This makes sure that there is an index
523 * upper inode where the nlink xattr can be stored before the copied up upper
526 #define OVL_NLINK_ADD_UPPER (1 << 0)
529 * On-disk format for indexed nlink:
531 * nlink relative to the upper inode - "U[+-]NUM"
532 * nlink relative to the lower inode - "L[+-]NUM"
535 static int ovl_set_nlink_common(struct dentry
*dentry
,
536 struct dentry
*realdentry
, const char *format
)
538 struct inode
*inode
= d_inode(dentry
);
539 struct inode
*realinode
= d_inode(realdentry
);
543 len
= snprintf(buf
, sizeof(buf
), format
,
544 (int) (inode
->i_nlink
- realinode
->i_nlink
));
546 if (WARN_ON(len
>= sizeof(buf
)))
549 return ovl_do_setxattr(ovl_dentry_upper(dentry
),
550 OVL_XATTR_NLINK
, buf
, len
, 0);
553 int ovl_set_nlink_upper(struct dentry
*dentry
)
555 return ovl_set_nlink_common(dentry
, ovl_dentry_upper(dentry
), "U%+i");
558 int ovl_set_nlink_lower(struct dentry
*dentry
)
560 return ovl_set_nlink_common(dentry
, ovl_dentry_lower(dentry
), "L%+i");
563 unsigned int ovl_get_nlink(struct dentry
*lowerdentry
,
564 struct dentry
*upperdentry
,
565 unsigned int fallback
)
572 if (!lowerdentry
|| !upperdentry
|| d_inode(lowerdentry
)->i_nlink
== 1)
575 err
= vfs_getxattr(upperdentry
, OVL_XATTR_NLINK
, &buf
, sizeof(buf
) - 1);
580 if ((buf
[0] != 'L' && buf
[0] != 'U') ||
581 (buf
[1] != '+' && buf
[1] != '-'))
584 err
= kstrtoint(buf
+ 1, 10, &nlink_diff
);
588 nlink
= d_inode(buf
[0] == 'L' ? lowerdentry
: upperdentry
)->i_nlink
;
597 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
602 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
, dev_t rdev
)
606 inode
= new_inode(sb
);
608 ovl_fill_inode(inode
, mode
, rdev
, 0);
613 static int ovl_inode_test(struct inode
*inode
, void *data
)
615 return inode
->i_private
== data
;
618 static int ovl_inode_set(struct inode
*inode
, void *data
)
620 inode
->i_private
= data
;
624 static bool ovl_verify_inode(struct inode
*inode
, struct dentry
*lowerdentry
,
625 struct dentry
*upperdentry
, bool strict
)
628 * For directories, @strict verify from lookup path performs consistency
629 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
630 * inode. Non @strict verify from NFS handle decode path passes NULL for
631 * 'unknown' lower/upper.
633 if (S_ISDIR(inode
->i_mode
) && strict
) {
634 /* Real lower dir moved to upper layer under us? */
635 if (!lowerdentry
&& ovl_inode_lower(inode
))
638 /* Lookup of an uncovered redirect origin? */
639 if (!upperdentry
&& ovl_inode_upper(inode
))
644 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
645 * This happens when finding a copied up overlay inode for a renamed
646 * or hardlinked overlay dentry and lower dentry cannot be followed
647 * by origin because lower fs does not support file handles.
649 if (lowerdentry
&& ovl_inode_lower(inode
) != d_inode(lowerdentry
))
653 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
654 * This happens when finding a lower alias for a copied up hard link.
656 if (upperdentry
&& ovl_inode_upper(inode
) != d_inode(upperdentry
))
662 struct inode
*ovl_lookup_inode(struct super_block
*sb
, struct dentry
*real
,
665 struct inode
*inode
, *key
= d_inode(real
);
667 inode
= ilookup5(sb
, (unsigned long) key
, ovl_inode_test
, key
);
671 if (!ovl_verify_inode(inode
, is_upper
? NULL
: real
,
672 is_upper
? real
: NULL
, false)) {
674 return ERR_PTR(-ESTALE
);
681 * Does overlay inode need to be hashed by lower inode?
683 static bool ovl_hash_bylower(struct super_block
*sb
, struct dentry
*upper
,
684 struct dentry
*lower
, struct dentry
*index
)
686 struct ovl_fs
*ofs
= sb
->s_fs_info
;
688 /* No, if pure upper */
692 /* Yes, if already indexed */
696 /* Yes, if won't be copied up */
700 /* No, if lower hardlink is or will be broken on copy up */
701 if ((upper
|| !ovl_indexdir(sb
)) &&
702 !d_is_dir(lower
) && d_inode(lower
)->i_nlink
> 1)
705 /* No, if non-indexed upper with NFS export */
706 if (sb
->s_export_op
&& upper
)
709 /* Otherwise, hash by lower inode for fsnotify */
713 struct inode
*ovl_get_inode(struct super_block
*sb
, struct dentry
*upperdentry
,
714 struct dentry
*lowerdentry
, struct dentry
*index
,
715 unsigned int numlower
)
717 struct inode
*realinode
= upperdentry
? d_inode(upperdentry
) : NULL
;
719 bool bylower
= ovl_hash_bylower(sb
, upperdentry
, lowerdentry
, index
);
721 unsigned long ino
= 0;
724 realinode
= d_inode(lowerdentry
);
727 * Copy up origin (lower) may exist for non-indexed upper, but we must
728 * not use lower as hash key if this is a broken hardlink.
730 is_dir
= S_ISDIR(realinode
->i_mode
);
731 if (upperdentry
|| bylower
) {
732 struct inode
*key
= d_inode(bylower
? lowerdentry
:
734 unsigned int nlink
= is_dir
? 1 : realinode
->i_nlink
;
736 inode
= iget5_locked(sb
, (unsigned long) key
,
737 ovl_inode_test
, ovl_inode_set
, key
);
740 if (!(inode
->i_state
& I_NEW
)) {
742 * Verify that the underlying files stored in the inode
743 * match those in the dentry.
745 if (!ovl_verify_inode(inode
, lowerdentry
, upperdentry
,
748 inode
= ERR_PTR(-ESTALE
);
756 /* Recalculate nlink for non-dir due to indexing */
758 nlink
= ovl_get_nlink(lowerdentry
, upperdentry
, nlink
);
759 set_nlink(inode
, nlink
);
762 /* Lower hardlink that will be broken on copy up */
763 inode
= new_inode(sb
);
767 ovl_fill_inode(inode
, realinode
->i_mode
, realinode
->i_rdev
, ino
);
768 ovl_inode_init(inode
, upperdentry
, lowerdentry
);
770 if (upperdentry
&& ovl_is_impuredir(upperdentry
))
771 ovl_set_flag(OVL_IMPURE
, inode
);
773 /* Check for non-merge dir that may have whiteouts */
775 if (((upperdentry
&& lowerdentry
) || numlower
> 1) ||
776 ovl_check_origin_xattr(upperdentry
?: lowerdentry
)) {
777 ovl_set_flag(OVL_WHITEOUTS
, inode
);
781 if (inode
->i_state
& I_NEW
)
782 unlock_new_inode(inode
);
787 inode
= ERR_PTR(-ENOMEM
);