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"
18 int ovl_setattr(struct dentry
*dentry
, struct iattr
*attr
)
21 struct dentry
*upperdentry
;
22 const struct cred
*old_cred
;
25 * Check for permissions before trying to copy-up. This is redundant
26 * since it will be rechecked later by ->setattr() on upper dentry. But
27 * without this, copy-up can be triggered by just about anybody.
29 * We don't initialize inode->size, which just means that
30 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
31 * check for a swapfile (which this won't be anyway).
33 err
= setattr_prepare(dentry
, attr
);
37 err
= ovl_want_write(dentry
);
41 err
= ovl_copy_up(dentry
);
43 upperdentry
= ovl_dentry_upper(dentry
);
45 if (attr
->ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
46 attr
->ia_valid
&= ~ATTR_MODE
;
48 inode_lock(upperdentry
->d_inode
);
49 old_cred
= ovl_override_creds(dentry
->d_sb
);
50 err
= notify_change(upperdentry
, attr
, NULL
);
51 revert_creds(old_cred
);
53 ovl_copyattr(upperdentry
->d_inode
, dentry
->d_inode
);
54 inode_unlock(upperdentry
->d_inode
);
56 ovl_drop_write(dentry
);
61 int ovl_getattr(const struct path
*path
, struct kstat
*stat
,
62 u32 request_mask
, unsigned int flags
)
64 struct dentry
*dentry
= path
->dentry
;
65 enum ovl_path_type type
;
67 const struct cred
*old_cred
;
68 bool is_dir
= S_ISDIR(dentry
->d_inode
->i_mode
);
71 type
= ovl_path_real(dentry
, &realpath
);
72 old_cred
= ovl_override_creds(dentry
->d_sb
);
73 err
= vfs_getattr(&realpath
, stat
, request_mask
, flags
);
78 * When all layers are on the same fs, all real inode number are
79 * unique, so we use the overlay st_dev, which is friendly to du -x.
81 * We also use st_ino of the copy up origin, if we know it.
82 * This guaranties constant st_dev/st_ino across copy up.
84 * If filesystem supports NFS export ops, this also guaranties
85 * persistent st_ino across mount cycle.
87 if (ovl_same_sb(dentry
->d_sb
)) {
88 if (OVL_TYPE_ORIGIN(type
)) {
89 struct kstat lowerstat
;
90 u32 lowermask
= STATX_INO
| (!is_dir
? STATX_NLINK
: 0);
92 ovl_path_lower(dentry
, &realpath
);
93 err
= vfs_getattr(&realpath
, &lowerstat
,
98 WARN_ON_ONCE(stat
->dev
!= lowerstat
.dev
);
100 * Lower hardlinks may be broken on copy up to different
101 * upper files, so we cannot use the lower origin st_ino
102 * for those different files, even for the same fs case.
103 * With inodes index enabled, it is safe to use st_ino
104 * of an indexed hardlinked origin. The index validates
105 * that the upper hardlink is not broken.
107 if (is_dir
|| lowerstat
.nlink
== 1 ||
108 ovl_test_flag(OVL_INDEX
, d_inode(dentry
)))
109 stat
->ino
= lowerstat
.ino
;
111 stat
->dev
= dentry
->d_sb
->s_dev
;
114 * If not all layers are on the same fs the pair {real st_ino;
115 * overlay st_dev} is not unique, so use the non persistent
118 * Always use the overlay st_dev for directories, so 'find
119 * -xdev' will scan the entire overlay mount and won't cross the
120 * overlay mount boundaries.
122 stat
->dev
= dentry
->d_sb
->s_dev
;
123 stat
->ino
= dentry
->d_inode
->i_ino
;
127 * It's probably not worth it to count subdirs to get the
128 * correct link count. nlink=1 seems to pacify 'find' and
131 if (is_dir
&& OVL_TYPE_MERGE(type
))
135 * Return the overlay inode nlinks for indexed upper inodes.
136 * Overlay inode nlink counts the union of the upper hardlinks
137 * and non-covered lower hardlinks. It does not include the upper
140 if (!is_dir
&& ovl_test_flag(OVL_INDEX
, d_inode(dentry
)))
141 stat
->nlink
= dentry
->d_inode
->i_nlink
;
144 revert_creds(old_cred
);
149 int ovl_permission(struct inode
*inode
, int mask
)
151 struct inode
*upperinode
= ovl_inode_upper(inode
);
152 struct inode
*realinode
= upperinode
?: ovl_inode_lower(inode
);
153 const struct cred
*old_cred
;
156 /* Careful in RCU walk mode */
158 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
163 * Check overlay inode with the creds of task and underlying inode
164 * with creds of mounter
166 err
= generic_permission(inode
, mask
);
170 old_cred
= ovl_override_creds(inode
->i_sb
);
172 !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
173 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
174 /* Make sure mounter can read file for copy up later */
177 err
= inode_permission(realinode
, mask
);
178 revert_creds(old_cred
);
183 static const char *ovl_get_link(struct dentry
*dentry
,
185 struct delayed_call
*done
)
187 const struct cred
*old_cred
;
191 return ERR_PTR(-ECHILD
);
193 old_cred
= ovl_override_creds(dentry
->d_sb
);
194 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
195 revert_creds(old_cred
);
199 bool ovl_is_private_xattr(const char *name
)
201 return strncmp(name
, OVL_XATTR_PREFIX
,
202 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
205 int ovl_xattr_set(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
206 const void *value
, size_t size
, int flags
)
209 struct dentry
*upperdentry
= ovl_i_dentry_upper(inode
);
210 struct dentry
*realdentry
= upperdentry
?: ovl_dentry_lower(dentry
);
211 const struct cred
*old_cred
;
213 err
= ovl_want_write(dentry
);
217 if (!value
&& !upperdentry
) {
218 err
= vfs_getxattr(realdentry
, name
, NULL
, 0);
224 err
= ovl_copy_up(dentry
);
228 realdentry
= ovl_dentry_upper(dentry
);
231 old_cred
= ovl_override_creds(dentry
->d_sb
);
233 err
= vfs_setxattr(realdentry
, name
, value
, size
, flags
);
235 WARN_ON(flags
!= XATTR_REPLACE
);
236 err
= vfs_removexattr(realdentry
, name
);
238 revert_creds(old_cred
);
241 ovl_drop_write(dentry
);
246 int ovl_xattr_get(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
247 void *value
, size_t size
)
250 const struct cred
*old_cred
;
251 struct dentry
*realdentry
=
252 ovl_i_dentry_upper(inode
) ?: ovl_dentry_lower(dentry
);
254 old_cred
= ovl_override_creds(dentry
->d_sb
);
255 res
= vfs_getxattr(realdentry
, name
, value
, size
);
256 revert_creds(old_cred
);
260 static bool ovl_can_list(const char *s
)
262 /* List all non-trusted xatts */
263 if (strncmp(s
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) != 0)
266 /* Never list trusted.overlay, list other trusted for superuser only */
267 return !ovl_is_private_xattr(s
) && capable(CAP_SYS_ADMIN
);
270 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
272 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
276 const struct cred
*old_cred
;
278 old_cred
= ovl_override_creds(dentry
->d_sb
);
279 res
= vfs_listxattr(realdentry
, list
, size
);
280 revert_creds(old_cred
);
281 if (res
<= 0 || size
== 0)
284 /* filter out private xattrs */
285 for (s
= list
, len
= res
; len
;) {
286 size_t slen
= strnlen(s
, len
) + 1;
288 /* underlying fs providing us with an broken xattr list? */
289 if (WARN_ON(slen
> len
))
293 if (!ovl_can_list(s
)) {
295 memmove(s
, s
+ slen
, len
);
304 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
306 struct inode
*realinode
= ovl_inode_real(inode
);
307 const struct cred
*old_cred
;
308 struct posix_acl
*acl
;
310 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
313 old_cred
= ovl_override_creds(inode
->i_sb
);
314 acl
= get_acl(realinode
, type
);
315 revert_creds(old_cred
);
320 static bool ovl_open_need_copy_up(struct dentry
*dentry
, int flags
)
322 if (ovl_dentry_upper(dentry
) &&
323 ovl_dentry_has_upper_alias(dentry
))
326 if (special_file(d_inode(dentry
)->i_mode
))
329 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
335 int ovl_open_maybe_copy_up(struct dentry
*dentry
, unsigned int file_flags
)
339 if (ovl_open_need_copy_up(dentry
, file_flags
)) {
340 err
= ovl_want_write(dentry
);
342 err
= ovl_copy_up_flags(dentry
, file_flags
);
343 ovl_drop_write(dentry
);
350 int ovl_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
352 struct dentry
*alias
;
353 struct path upperpath
;
355 if (!(flags
& S_ATIME
))
358 alias
= d_find_any_alias(inode
);
362 ovl_path_upper(alias
, &upperpath
);
363 if (upperpath
.dentry
) {
364 touch_atime(&upperpath
);
365 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
373 static const struct inode_operations ovl_file_inode_operations
= {
374 .setattr
= ovl_setattr
,
375 .permission
= ovl_permission
,
376 .getattr
= ovl_getattr
,
377 .listxattr
= ovl_listxattr
,
378 .get_acl
= ovl_get_acl
,
379 .update_time
= ovl_update_time
,
382 static const struct inode_operations ovl_symlink_inode_operations
= {
383 .setattr
= ovl_setattr
,
384 .get_link
= ovl_get_link
,
385 .getattr
= ovl_getattr
,
386 .listxattr
= ovl_listxattr
,
387 .update_time
= ovl_update_time
,
391 * It is possible to stack overlayfs instance on top of another
392 * overlayfs instance as lower layer. We need to annonate the
393 * stackable i_mutex locks according to stack level of the super
394 * block instance. An overlayfs instance can never be in stack
395 * depth 0 (there is always a real fs below it). An overlayfs
396 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
398 * For example, here is a snip from /proc/lockdep_chains after
399 * dir_iterate of nested overlayfs:
401 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
402 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
403 * [...] &type->i_mutex_dir_key (stack_depth=0)
405 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
407 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode
*inode
)
409 #ifdef CONFIG_LOCKDEP
410 static struct lock_class_key ovl_i_mutex_key
[OVL_MAX_NESTING
];
411 static struct lock_class_key ovl_i_mutex_dir_key
[OVL_MAX_NESTING
];
413 int depth
= inode
->i_sb
->s_stack_depth
- 1;
415 if (WARN_ON_ONCE(depth
< 0 || depth
>= OVL_MAX_NESTING
))
418 if (S_ISDIR(inode
->i_mode
))
419 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_dir_key
[depth
]);
421 lockdep_set_class(&inode
->i_rwsem
, &ovl_i_mutex_key
[depth
]);
425 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
427 inode
->i_ino
= get_next_ino();
428 inode
->i_mode
= mode
;
429 inode
->i_flags
|= S_NOCMTIME
;
430 #ifdef CONFIG_FS_POSIX_ACL
431 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
434 ovl_lockdep_annotate_inode_mutex_key(inode
);
436 switch (mode
& S_IFMT
) {
438 inode
->i_op
= &ovl_file_inode_operations
;
442 inode
->i_op
= &ovl_dir_inode_operations
;
443 inode
->i_fop
= &ovl_dir_operations
;
447 inode
->i_op
= &ovl_symlink_inode_operations
;
451 inode
->i_op
= &ovl_file_inode_operations
;
452 init_special_inode(inode
, mode
, rdev
);
458 * With inodes index enabled, an overlay inode nlink counts the union of upper
459 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
460 * upper inode, the following nlink modifying operations can happen:
462 * 1. Lower hardlink copy up
463 * 2. Upper hardlink created, unlinked or renamed over
464 * 3. Lower hardlink whiteout or renamed over
466 * For the first, copy up case, the union nlink does not change, whether the
467 * operation succeeds or fails, but the upper inode nlink may change.
468 * Therefore, before copy up, we store the union nlink value relative to the
469 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
471 * For the second, upper hardlink case, the union nlink should be incremented
472 * or decremented IFF the operation succeeds, aligned with nlink change of the
473 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
474 * value relative to the upper inode nlink in the index inode.
476 * For the last, lower cover up case, we simplify things by preceding the
477 * whiteout or cover up with copy up. This makes sure that there is an index
478 * upper inode where the nlink xattr can be stored before the copied up upper
481 #define OVL_NLINK_ADD_UPPER (1 << 0)
484 * On-disk format for indexed nlink:
486 * nlink relative to the upper inode - "U[+-]NUM"
487 * nlink relative to the lower inode - "L[+-]NUM"
490 static int ovl_set_nlink_common(struct dentry
*dentry
,
491 struct dentry
*realdentry
, const char *format
)
493 struct inode
*inode
= d_inode(dentry
);
494 struct inode
*realinode
= d_inode(realdentry
);
498 len
= snprintf(buf
, sizeof(buf
), format
,
499 (int) (inode
->i_nlink
- realinode
->i_nlink
));
501 return ovl_do_setxattr(ovl_dentry_upper(dentry
),
502 OVL_XATTR_NLINK
, buf
, len
, 0);
505 int ovl_set_nlink_upper(struct dentry
*dentry
)
507 return ovl_set_nlink_common(dentry
, ovl_dentry_upper(dentry
), "U%+i");
510 int ovl_set_nlink_lower(struct dentry
*dentry
)
512 return ovl_set_nlink_common(dentry
, ovl_dentry_lower(dentry
), "L%+i");
515 unsigned int ovl_get_nlink(struct dentry
*lowerdentry
,
516 struct dentry
*upperdentry
,
517 unsigned int fallback
)
524 if (!lowerdentry
|| !upperdentry
|| d_inode(lowerdentry
)->i_nlink
== 1)
527 err
= vfs_getxattr(upperdentry
, OVL_XATTR_NLINK
, &buf
, sizeof(buf
) - 1);
532 if ((buf
[0] != 'L' && buf
[0] != 'U') ||
533 (buf
[1] != '+' && buf
[1] != '-'))
536 err
= kstrtoint(buf
+ 1, 10, &nlink_diff
);
540 nlink
= d_inode(buf
[0] == 'L' ? lowerdentry
: upperdentry
)->i_nlink
;
549 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
554 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
, dev_t rdev
)
558 inode
= new_inode(sb
);
560 ovl_fill_inode(inode
, mode
, rdev
);
565 static int ovl_inode_test(struct inode
*inode
, void *data
)
567 return inode
->i_private
== data
;
570 static int ovl_inode_set(struct inode
*inode
, void *data
)
572 inode
->i_private
= data
;
576 static bool ovl_verify_inode(struct inode
*inode
, struct dentry
*lowerdentry
,
577 struct dentry
*upperdentry
)
580 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
581 * This happens when finding a copied up overlay inode for a renamed
582 * or hardlinked overlay dentry and lower dentry cannot be followed
583 * by origin because lower fs does not support file handles.
585 if (lowerdentry
&& ovl_inode_lower(inode
) != d_inode(lowerdentry
))
589 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
590 * This happens when finding a lower alias for a copied up hard link.
592 if (upperdentry
&& ovl_inode_upper(inode
) != d_inode(upperdentry
))
598 struct inode
*ovl_get_inode(struct dentry
*dentry
, struct dentry
*upperdentry
)
600 struct dentry
*lowerdentry
= ovl_dentry_lower(dentry
);
601 struct inode
*realinode
= upperdentry
? d_inode(upperdentry
) : NULL
;
605 realinode
= d_inode(lowerdentry
);
607 if (!S_ISDIR(realinode
->i_mode
) &&
608 (upperdentry
|| (lowerdentry
&& ovl_indexdir(dentry
->d_sb
)))) {
609 struct inode
*key
= d_inode(lowerdentry
?: upperdentry
);
612 inode
= iget5_locked(dentry
->d_sb
, (unsigned long) key
,
613 ovl_inode_test
, ovl_inode_set
, key
);
616 if (!(inode
->i_state
& I_NEW
)) {
618 * Verify that the underlying files stored in the inode
619 * match those in the dentry.
621 if (!ovl_verify_inode(inode
, lowerdentry
, upperdentry
)) {
623 inode
= ERR_PTR(-ESTALE
);
631 nlink
= ovl_get_nlink(lowerdentry
, upperdentry
,
633 set_nlink(inode
, nlink
);
635 inode
= new_inode(dentry
->d_sb
);
639 ovl_fill_inode(inode
, realinode
->i_mode
, realinode
->i_rdev
);
640 ovl_inode_init(inode
, upperdentry
, lowerdentry
);
642 if (upperdentry
&& ovl_is_impuredir(upperdentry
))
643 ovl_set_flag(OVL_IMPURE
, inode
);
645 if (inode
->i_state
& I_NEW
)
646 unlock_new_inode(inode
);
651 inode
= ERR_PTR(-ENOMEM
);