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.
108 * With inodes index enabled, it is safe to use st_ino
109 * of an indexed hardlinked origin. The index validates
110 * that the upper hardlink is not broken.
112 if (is_dir
|| lowerstat
.nlink
== 1 ||
113 ovl_test_flag(OVL_INDEX
, d_inode(dentry
)))
114 stat
->ino
= lowerstat
.ino
;
117 WARN_ON_ONCE(stat
->dev
!= lowerstat
.dev
);
119 stat
->dev
= ovl_get_pseudo_dev(dentry
);
123 * When all layers are on the same fs, all real inode
124 * number are unique, so we use the overlay st_dev,
125 * which is friendly to du -x.
127 stat
->dev
= dentry
->d_sb
->s_dev
;
128 } else if (!OVL_TYPE_UPPER(type
)) {
130 * For non-samefs setup, to make sure that st_dev/st_ino
131 * pair is unique across the system, we use a unique
132 * anonymous st_dev for lower layer inode.
134 stat
->dev
= ovl_get_pseudo_dev(dentry
);
138 * Always use the overlay st_dev for directories, so 'find
139 * -xdev' will scan the entire overlay mount and won't cross the
140 * overlay mount boundaries.
142 * If not all layers are on the same fs the pair {real st_ino;
143 * overlay st_dev} is not unique, so use the non persistent
144 * overlay st_ino for directories.
146 stat
->dev
= dentry
->d_sb
->s_dev
;
147 stat
->ino
= dentry
->d_inode
->i_ino
;
151 * It's probably not worth it to count subdirs to get the
152 * correct link count. nlink=1 seems to pacify 'find' and
155 if (is_dir
&& OVL_TYPE_MERGE(type
))
159 * Return the overlay inode nlinks for indexed upper inodes.
160 * Overlay inode nlink counts the union of the upper hardlinks
161 * and non-covered lower hardlinks. It does not include the upper
164 if (!is_dir
&& ovl_test_flag(OVL_INDEX
, d_inode(dentry
)))
165 stat
->nlink
= dentry
->d_inode
->i_nlink
;
168 revert_creds(old_cred
);
173 int ovl_permission(struct inode
*inode
, int mask
)
175 struct inode
*upperinode
= ovl_inode_upper(inode
);
176 struct inode
*realinode
= upperinode
?: ovl_inode_lower(inode
);
177 const struct cred
*old_cred
;
180 /* Careful in RCU walk mode */
182 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
187 * Check overlay inode with the creds of task and underlying inode
188 * with creds of mounter
190 err
= generic_permission(inode
, mask
);
194 old_cred
= ovl_override_creds(inode
->i_sb
);
196 !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
197 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
198 /* Make sure mounter can read file for copy up later */
201 err
= inode_permission(realinode
, mask
);
202 revert_creds(old_cred
);
207 static const char *ovl_get_link(struct dentry
*dentry
,
209 struct delayed_call
*done
)
211 const struct cred
*old_cred
;
215 return ERR_PTR(-ECHILD
);
217 old_cred
= ovl_override_creds(dentry
->d_sb
);
218 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
219 revert_creds(old_cred
);
223 bool ovl_is_private_xattr(const char *name
)
225 return strncmp(name
, OVL_XATTR_PREFIX
,
226 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
229 int ovl_xattr_set(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
230 const void *value
, size_t size
, int flags
)
233 struct dentry
*upperdentry
= ovl_i_dentry_upper(inode
);
234 struct dentry
*realdentry
= upperdentry
?: ovl_dentry_lower(dentry
);
235 const struct cred
*old_cred
;
237 err
= ovl_want_write(dentry
);
241 if (!value
&& !upperdentry
) {
242 err
= vfs_getxattr(realdentry
, name
, NULL
, 0);
248 err
= ovl_copy_up(dentry
);
252 realdentry
= ovl_dentry_upper(dentry
);
255 old_cred
= ovl_override_creds(dentry
->d_sb
);
257 err
= vfs_setxattr(realdentry
, name
, value
, size
, flags
);
259 WARN_ON(flags
!= XATTR_REPLACE
);
260 err
= vfs_removexattr(realdentry
, name
);
262 revert_creds(old_cred
);
265 ovl_drop_write(dentry
);
270 int ovl_xattr_get(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
271 void *value
, size_t size
)
274 const struct cred
*old_cred
;
275 struct dentry
*realdentry
=
276 ovl_i_dentry_upper(inode
) ?: ovl_dentry_lower(dentry
);
278 old_cred
= ovl_override_creds(dentry
->d_sb
);
279 res
= vfs_getxattr(realdentry
, name
, value
, size
);
280 revert_creds(old_cred
);
284 static bool ovl_can_list(const char *s
)
286 /* List all non-trusted xatts */
287 if (strncmp(s
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) != 0)
290 /* Never list trusted.overlay, list other trusted for superuser only */
291 return !ovl_is_private_xattr(s
) && capable(CAP_SYS_ADMIN
);
294 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
296 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
300 const struct cred
*old_cred
;
302 old_cred
= ovl_override_creds(dentry
->d_sb
);
303 res
= vfs_listxattr(realdentry
, list
, size
);
304 revert_creds(old_cred
);
305 if (res
<= 0 || size
== 0)
308 /* filter out private xattrs */
309 for (s
= list
, len
= res
; len
;) {
310 size_t slen
= strnlen(s
, len
) + 1;
312 /* underlying fs providing us with an broken xattr list? */
313 if (WARN_ON(slen
> len
))
317 if (!ovl_can_list(s
)) {
319 memmove(s
, s
+ slen
, len
);
328 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
330 struct inode
*realinode
= ovl_inode_real(inode
);
331 const struct cred
*old_cred
;
332 struct posix_acl
*acl
;
334 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
337 old_cred
= ovl_override_creds(inode
->i_sb
);
338 acl
= get_acl(realinode
, type
);
339 revert_creds(old_cred
);
344 static bool ovl_open_need_copy_up(struct dentry
*dentry
, int flags
)
346 if (ovl_dentry_upper(dentry
) &&
347 ovl_dentry_has_upper_alias(dentry
))
350 if (special_file(d_inode(dentry
)->i_mode
))
353 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
359 int ovl_open_maybe_copy_up(struct dentry
*dentry
, unsigned int file_flags
)
363 if (ovl_open_need_copy_up(dentry
, file_flags
)) {
364 err
= ovl_want_write(dentry
);
366 err
= ovl_copy_up_flags(dentry
, file_flags
);
367 ovl_drop_write(dentry
);
374 int ovl_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
376 struct dentry
*alias
;
377 struct path upperpath
;
379 if (!(flags
& S_ATIME
))
382 alias
= d_find_any_alias(inode
);
386 ovl_path_upper(alias
, &upperpath
);
387 if (upperpath
.dentry
) {
388 touch_atime(&upperpath
);
389 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
397 static const struct inode_operations ovl_file_inode_operations
= {
398 .setattr
= ovl_setattr
,
399 .permission
= ovl_permission
,
400 .getattr
= ovl_getattr
,
401 .listxattr
= ovl_listxattr
,
402 .get_acl
= ovl_get_acl
,
403 .update_time
= ovl_update_time
,
406 static const struct inode_operations ovl_symlink_inode_operations
= {
407 .setattr
= ovl_setattr
,
408 .get_link
= ovl_get_link
,
409 .getattr
= ovl_getattr
,
410 .listxattr
= ovl_listxattr
,
411 .update_time
= ovl_update_time
,
415 * It is possible to stack overlayfs instance on top of another
416 * overlayfs instance as lower layer. We need to annonate the
417 * stackable i_mutex locks according to stack level of the super
418 * block instance. An overlayfs instance can never be in stack
419 * depth 0 (there is always a real fs below it). An overlayfs
420 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
422 * For example, here is a snip from /proc/lockdep_chains after
423 * dir_iterate of nested overlayfs:
425 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
426 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
427 * [...] &type->i_mutex_dir_key (stack_depth=0)
429 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
431 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode
*inode
)
433 #ifdef CONFIG_LOCKDEP
434 static struct lock_class_key ovl_i_mutex_key
[OVL_MAX_NESTING
];
435 static struct lock_class_key ovl_i_mutex_dir_key
[OVL_MAX_NESTING
];
436 static struct lock_class_key ovl_i_lock_key
[OVL_MAX_NESTING
];
438 int depth
= inode
->i_sb
->s_stack_depth
- 1;
440 if (WARN_ON_ONCE(depth
< 0 || depth
>= OVL_MAX_NESTING
))
443 if (S_ISDIR(inode
->i_mode
))
444 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_dir_key
[depth
]);
446 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_key
[depth
]);
448 lockdep_set_class(&OVL_I(inode
)->lock
, &ovl_i_lock_key
[depth
]);
452 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
454 inode
->i_ino
= get_next_ino();
455 inode
->i_mode
= mode
;
456 inode
->i_flags
|= S_NOCMTIME
;
457 #ifdef CONFIG_FS_POSIX_ACL
458 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
461 ovl_lockdep_annotate_inode_mutex_key(inode
);
463 switch (mode
& S_IFMT
) {
465 inode
->i_op
= &ovl_file_inode_operations
;
469 inode
->i_op
= &ovl_dir_inode_operations
;
470 inode
->i_fop
= &ovl_dir_operations
;
474 inode
->i_op
= &ovl_symlink_inode_operations
;
478 inode
->i_op
= &ovl_file_inode_operations
;
479 init_special_inode(inode
, mode
, rdev
);
485 * With inodes index enabled, an overlay inode nlink counts the union of upper
486 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
487 * upper inode, the following nlink modifying operations can happen:
489 * 1. Lower hardlink copy up
490 * 2. Upper hardlink created, unlinked or renamed over
491 * 3. Lower hardlink whiteout or renamed over
493 * For the first, copy up case, the union nlink does not change, whether the
494 * operation succeeds or fails, but the upper inode nlink may change.
495 * Therefore, before copy up, we store the union nlink value relative to the
496 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
498 * For the second, upper hardlink case, the union nlink should be incremented
499 * or decremented IFF the operation succeeds, aligned with nlink change of the
500 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
501 * value relative to the upper inode nlink in the index inode.
503 * For the last, lower cover up case, we simplify things by preceding the
504 * whiteout or cover up with copy up. This makes sure that there is an index
505 * upper inode where the nlink xattr can be stored before the copied up upper
508 #define OVL_NLINK_ADD_UPPER (1 << 0)
511 * On-disk format for indexed nlink:
513 * nlink relative to the upper inode - "U[+-]NUM"
514 * nlink relative to the lower inode - "L[+-]NUM"
517 static int ovl_set_nlink_common(struct dentry
*dentry
,
518 struct dentry
*realdentry
, const char *format
)
520 struct inode
*inode
= d_inode(dentry
);
521 struct inode
*realinode
= d_inode(realdentry
);
525 len
= snprintf(buf
, sizeof(buf
), format
,
526 (int) (inode
->i_nlink
- realinode
->i_nlink
));
528 if (WARN_ON(len
>= sizeof(buf
)))
531 return ovl_do_setxattr(ovl_dentry_upper(dentry
),
532 OVL_XATTR_NLINK
, buf
, len
, 0);
535 int ovl_set_nlink_upper(struct dentry
*dentry
)
537 return ovl_set_nlink_common(dentry
, ovl_dentry_upper(dentry
), "U%+i");
540 int ovl_set_nlink_lower(struct dentry
*dentry
)
542 return ovl_set_nlink_common(dentry
, ovl_dentry_lower(dentry
), "L%+i");
545 unsigned int ovl_get_nlink(struct dentry
*lowerdentry
,
546 struct dentry
*upperdentry
,
547 unsigned int fallback
)
554 if (!lowerdentry
|| !upperdentry
|| d_inode(lowerdentry
)->i_nlink
== 1)
557 err
= vfs_getxattr(upperdentry
, OVL_XATTR_NLINK
, &buf
, sizeof(buf
) - 1);
562 if ((buf
[0] != 'L' && buf
[0] != 'U') ||
563 (buf
[1] != '+' && buf
[1] != '-'))
566 err
= kstrtoint(buf
+ 1, 10, &nlink_diff
);
570 nlink
= d_inode(buf
[0] == 'L' ? lowerdentry
: upperdentry
)->i_nlink
;
579 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
584 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
, dev_t rdev
)
588 inode
= new_inode(sb
);
590 ovl_fill_inode(inode
, mode
, rdev
);
595 static int ovl_inode_test(struct inode
*inode
, void *data
)
597 return inode
->i_private
== data
;
600 static int ovl_inode_set(struct inode
*inode
, void *data
)
602 inode
->i_private
= data
;
606 static bool ovl_verify_inode(struct inode
*inode
, struct dentry
*lowerdentry
,
607 struct dentry
*upperdentry
)
609 if (S_ISDIR(inode
->i_mode
)) {
610 /* Real lower dir moved to upper layer under us? */
611 if (!lowerdentry
&& ovl_inode_lower(inode
))
614 /* Lookup of an uncovered redirect origin? */
615 if (!upperdentry
&& ovl_inode_upper(inode
))
620 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
621 * This happens when finding a copied up overlay inode for a renamed
622 * or hardlinked overlay dentry and lower dentry cannot be followed
623 * by origin because lower fs does not support file handles.
625 if (lowerdentry
&& ovl_inode_lower(inode
) != d_inode(lowerdentry
))
629 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
630 * This happens when finding a lower alias for a copied up hard link.
632 if (upperdentry
&& ovl_inode_upper(inode
) != d_inode(upperdentry
))
638 struct inode
*ovl_get_inode(struct dentry
*dentry
, struct dentry
*upperdentry
,
639 struct dentry
*index
)
641 struct dentry
*lowerdentry
= ovl_dentry_lower(dentry
);
642 struct inode
*realinode
= upperdentry
? d_inode(upperdentry
) : NULL
;
644 /* Already indexed or could be indexed on copy up? */
645 bool indexed
= (index
|| (ovl_indexdir(dentry
->d_sb
) && !upperdentry
));
646 struct dentry
*origin
= indexed
? lowerdentry
: NULL
;
649 if (WARN_ON(upperdentry
&& indexed
&& !lowerdentry
))
650 return ERR_PTR(-EIO
);
653 realinode
= d_inode(lowerdentry
);
656 * Copy up origin (lower) may exist for non-indexed non-dir upper, but
657 * we must not use lower as hash key in that case.
658 * Hash non-dir that is or could be indexed by origin inode.
659 * Hash dir that is or could be merged by origin inode.
660 * Hash pure upper and non-indexed non-dir by upper inode.
662 is_dir
= S_ISDIR(realinode
->i_mode
);
664 origin
= lowerdentry
;
666 if (upperdentry
|| origin
) {
667 struct inode
*key
= d_inode(origin
?: upperdentry
);
668 unsigned int nlink
= is_dir
? 1 : realinode
->i_nlink
;
670 inode
= iget5_locked(dentry
->d_sb
, (unsigned long) key
,
671 ovl_inode_test
, ovl_inode_set
, key
);
674 if (!(inode
->i_state
& I_NEW
)) {
676 * Verify that the underlying files stored in the inode
677 * match those in the dentry.
679 if (!ovl_verify_inode(inode
, lowerdentry
, upperdentry
)) {
681 inode
= ERR_PTR(-ESTALE
);
689 /* Recalculate nlink for non-dir due to indexing */
691 nlink
= ovl_get_nlink(lowerdentry
, upperdentry
, nlink
);
692 set_nlink(inode
, nlink
);
694 inode
= new_inode(dentry
->d_sb
);
698 ovl_fill_inode(inode
, realinode
->i_mode
, realinode
->i_rdev
);
699 ovl_inode_init(inode
, upperdentry
, lowerdentry
);
701 if (upperdentry
&& ovl_is_impuredir(upperdentry
))
702 ovl_set_flag(OVL_IMPURE
, inode
);
704 /* Check for non-merge dir that may have whiteouts */
706 struct ovl_entry
*oe
= dentry
->d_fsdata
;
708 if (((upperdentry
&& lowerdentry
) || oe
->numlower
> 1) ||
709 ovl_check_origin_xattr(upperdentry
?: lowerdentry
)) {
710 ovl_set_flag(OVL_WHITEOUTS
, inode
);
714 if (inode
->i_state
& I_NEW
)
715 unlock_new_inode(inode
);
720 inode
= ERR_PTR(-ENOMEM
);