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 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
22 struct dentry *upperdentry;
23 const struct cred *old_cred;
26 * Check for permissions before trying to copy-up. This is redundant
27 * since it will be rechecked later by ->setattr() on upper dentry. But
28 * without this, copy-up can be triggered by just about anybody.
30 * We don't initialize inode->size, which just means that
31 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
32 * check for a swapfile (which this won't be anyway).
34 err = setattr_prepare(dentry, attr);
38 err = ovl_want_write(dentry);
42 err = ovl_copy_up(dentry);
44 upperdentry = ovl_dentry_upper(dentry);
46 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
47 attr->ia_valid &= ~ATTR_MODE;
49 inode_lock(upperdentry->d_inode);
50 old_cred = ovl_override_creds(dentry->d_sb);
51 err = notify_change(upperdentry, attr, NULL);
52 revert_creds(old_cred);
54 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
55 inode_unlock(upperdentry->d_inode);
57 ovl_drop_write(dentry);
62 static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat,
63 struct ovl_layer *lower_layer)
65 bool samefs = ovl_same_sb(dentry->d_sb);
66 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
70 * When all layers are on the same fs, all real inode
71 * number are unique, so we use the overlay st_dev,
72 * which is friendly to du -x.
74 stat->dev = dentry->d_sb->s_dev;
76 } else if (xinobits) {
77 unsigned int shift = 64 - xinobits;
79 * All inode numbers of underlying fs should not be using the
80 * high xinobits, so we use high xinobits to partition the
81 * overlay st_ino address space. The high bits holds the fsid
82 * (upper fsid is 0). This way overlay inode numbers are unique
83 * and all inodes use overlay st_dev. Inode numbers are also
84 * persistent for a given layer configuration.
86 if (stat->ino >> shift) {
87 pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
88 dentry, stat->ino, xinobits);
91 stat->ino |= ((u64)lower_layer->fsid) << shift;
93 stat->dev = dentry->d_sb->s_dev;
98 /* The inode could not be mapped to a unified st_ino address space */
99 if (S_ISDIR(dentry->d_inode->i_mode)) {
101 * Always use the overlay st_dev for directories, so 'find
102 * -xdev' will scan the entire overlay mount and won't cross the
103 * overlay mount boundaries.
105 * If not all layers are on the same fs the pair {real st_ino;
106 * overlay st_dev} is not unique, so use the non persistent
107 * overlay st_ino for directories.
109 stat->dev = dentry->d_sb->s_dev;
110 stat->ino = dentry->d_inode->i_ino;
111 } else if (lower_layer && lower_layer->fsid) {
113 * For non-samefs setup, if we cannot map all layers st_ino
114 * to a unified address space, we need to make sure that st_dev
115 * is unique per lower fs. Upper layer uses real st_dev and
116 * lower layers use the unique anonymous bdev assigned to the
119 stat->dev = lower_layer->fs->pseudo_dev;
125 int ovl_getattr(const struct path *path, struct kstat *stat,
126 u32 request_mask, unsigned int flags)
128 struct dentry *dentry = path->dentry;
129 enum ovl_path_type type;
130 struct path realpath;
131 const struct cred *old_cred;
132 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
133 bool samefs = ovl_same_sb(dentry->d_sb);
134 struct ovl_layer *lower_layer = NULL;
137 type = ovl_path_real(dentry, &realpath);
138 old_cred = ovl_override_creds(dentry->d_sb);
139 err = vfs_getattr(&realpath, stat, request_mask, flags);
144 * For non-dir or same fs, we use st_ino of the copy up origin.
145 * This guaranties constant st_dev/st_ino across copy up.
146 * With xino feature and non-samefs, we use st_ino of the copy up
147 * origin masked with high bits that represent the layer id.
149 * If lower filesystem supports NFS file handles, this also guaranties
150 * persistent st_ino across mount cycle.
152 if (!is_dir || samefs || ovl_xino_bits(dentry->d_sb)) {
153 if (!OVL_TYPE_UPPER(type)) {
154 lower_layer = ovl_layer_lower(dentry);
155 } else if (OVL_TYPE_ORIGIN(type)) {
156 struct kstat lowerstat;
157 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
159 ovl_path_lower(dentry, &realpath);
160 err = vfs_getattr(&realpath, &lowerstat,
166 * Lower hardlinks may be broken on copy up to different
167 * upper files, so we cannot use the lower origin st_ino
168 * for those different files, even for the same fs case.
170 * Similarly, several redirected dirs can point to the
171 * same dir on a lower layer. With the "verify_lower"
172 * feature, we do not use the lower origin st_ino, if
173 * we haven't verified that this redirect is unique.
175 * With inodes index enabled, it is safe to use st_ino
176 * of an indexed origin. The index validates that the
177 * upper hardlink is not broken and that a redirected
178 * dir is the only redirect to that origin.
180 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
181 (!ovl_verify_lower(dentry->d_sb) &&
182 (is_dir || lowerstat.nlink == 1))) {
183 stat->ino = lowerstat.ino;
184 lower_layer = ovl_layer_lower(dentry);
189 err = ovl_map_dev_ino(dentry, stat, lower_layer);
194 * It's probably not worth it to count subdirs to get the
195 * correct link count. nlink=1 seems to pacify 'find' and
198 if (is_dir && OVL_TYPE_MERGE(type))
202 * Return the overlay inode nlinks for indexed upper inodes.
203 * Overlay inode nlink counts the union of the upper hardlinks
204 * and non-covered lower hardlinks. It does not include the upper
207 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
208 stat->nlink = dentry->d_inode->i_nlink;
211 revert_creds(old_cred);
216 int ovl_permission(struct inode *inode, int mask)
218 struct inode *upperinode = ovl_inode_upper(inode);
219 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
220 const struct cred *old_cred;
223 /* Careful in RCU walk mode */
225 WARN_ON(!(mask & MAY_NOT_BLOCK));
230 * Check overlay inode with the creds of task and underlying inode
231 * with creds of mounter
233 err = generic_permission(inode, mask);
237 old_cred = ovl_override_creds(inode->i_sb);
239 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
240 mask &= ~(MAY_WRITE | MAY_APPEND);
241 /* Make sure mounter can read file for copy up later */
244 err = inode_permission(realinode, mask);
245 revert_creds(old_cred);
250 static const char *ovl_get_link(struct dentry *dentry,
252 struct delayed_call *done)
254 const struct cred *old_cred;
258 return ERR_PTR(-ECHILD);
260 old_cred = ovl_override_creds(dentry->d_sb);
261 p = vfs_get_link(ovl_dentry_real(dentry), done);
262 revert_creds(old_cred);
266 bool ovl_is_private_xattr(const char *name)
268 return strncmp(name, OVL_XATTR_PREFIX,
269 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
272 int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
273 const void *value, size_t size, int flags)
276 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
277 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
278 const struct cred *old_cred;
280 err = ovl_want_write(dentry);
284 if (!value && !upperdentry) {
285 err = vfs_getxattr(realdentry, name, NULL, 0);
291 err = ovl_copy_up(dentry);
295 realdentry = ovl_dentry_upper(dentry);
298 old_cred = ovl_override_creds(dentry->d_sb);
300 err = vfs_setxattr(realdentry, name, value, size, flags);
302 WARN_ON(flags != XATTR_REPLACE);
303 err = vfs_removexattr(realdentry, name);
305 revert_creds(old_cred);
308 ovl_drop_write(dentry);
313 int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
314 void *value, size_t size)
317 const struct cred *old_cred;
318 struct dentry *realdentry =
319 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
321 old_cred = ovl_override_creds(dentry->d_sb);
322 res = vfs_getxattr(realdentry, name, value, size);
323 revert_creds(old_cred);
327 static bool ovl_can_list(const char *s)
329 /* List all non-trusted xatts */
330 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
333 /* Never list trusted.overlay, list other trusted for superuser only */
334 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
337 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
339 struct dentry *realdentry = ovl_dentry_real(dentry);
343 const struct cred *old_cred;
345 old_cred = ovl_override_creds(dentry->d_sb);
346 res = vfs_listxattr(realdentry, list, size);
347 revert_creds(old_cred);
348 if (res <= 0 || size == 0)
351 /* filter out private xattrs */
352 for (s = list, len = res; len;) {
353 size_t slen = strnlen(s, len) + 1;
355 /* underlying fs providing us with an broken xattr list? */
356 if (WARN_ON(slen > len))
360 if (!ovl_can_list(s)) {
362 memmove(s, s + slen, len);
371 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
373 struct inode *realinode = ovl_inode_real(inode);
374 const struct cred *old_cred;
375 struct posix_acl *acl;
377 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
380 old_cred = ovl_override_creds(inode->i_sb);
381 acl = get_acl(realinode, type);
382 revert_creds(old_cred);
387 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
389 /* Copy up of disconnected dentry does not set upper alias */
390 if (ovl_dentry_upper(dentry) &&
391 (ovl_dentry_has_upper_alias(dentry) ||
392 (dentry->d_flags & DCACHE_DISCONNECTED)))
395 if (special_file(d_inode(dentry)->i_mode))
398 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
404 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
408 if (ovl_open_need_copy_up(dentry, file_flags)) {
409 err = ovl_want_write(dentry);
411 err = ovl_copy_up_flags(dentry, file_flags);
412 ovl_drop_write(dentry);
419 int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
421 if (flags & S_ATIME) {
422 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
423 struct path upperpath = {
424 .mnt = ofs->upper_mnt,
425 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
428 if (upperpath.dentry) {
429 touch_atime(&upperpath);
430 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
436 static const struct inode_operations ovl_file_inode_operations = {
437 .setattr = ovl_setattr,
438 .permission = ovl_permission,
439 .getattr = ovl_getattr,
440 .listxattr = ovl_listxattr,
441 .get_acl = ovl_get_acl,
442 .update_time = ovl_update_time,
445 static const struct inode_operations ovl_symlink_inode_operations = {
446 .setattr = ovl_setattr,
447 .get_link = ovl_get_link,
448 .getattr = ovl_getattr,
449 .listxattr = ovl_listxattr,
450 .update_time = ovl_update_time,
454 * It is possible to stack overlayfs instance on top of another
455 * overlayfs instance as lower layer. We need to annonate the
456 * stackable i_mutex locks according to stack level of the super
457 * block instance. An overlayfs instance can never be in stack
458 * depth 0 (there is always a real fs below it). An overlayfs
459 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
461 * For example, here is a snip from /proc/lockdep_chains after
462 * dir_iterate of nested overlayfs:
464 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
465 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
466 * [...] &type->i_mutex_dir_key (stack_depth=0)
468 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
470 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
472 #ifdef CONFIG_LOCKDEP
473 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
474 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
475 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
477 int depth = inode->i_sb->s_stack_depth - 1;
479 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
482 if (S_ISDIR(inode->i_mode))
483 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
485 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
487 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
491 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
492 unsigned long ino, int fsid)
494 int xinobits = ovl_xino_bits(inode->i_sb);
497 * When NFS export is enabled and d_ino is consistent with st_ino
498 * (samefs or i_ino has enough bits to encode layer), set the same
499 * value used for d_ino to i_ino, because nfsd readdirplus compares
500 * d_ino values to i_ino values of child entries. When called from
501 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
502 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
504 if (inode->i_sb->s_export_op &&
505 (ovl_same_sb(inode->i_sb) || xinobits)) {
507 if (xinobits && fsid && !(ino >> (64 - xinobits)))
508 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
510 inode->i_ino = get_next_ino();
512 inode->i_mode = mode;
513 inode->i_flags |= S_NOCMTIME;
514 #ifdef CONFIG_FS_POSIX_ACL
515 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
518 ovl_lockdep_annotate_inode_mutex_key(inode);
520 switch (mode & S_IFMT) {
522 inode->i_op = &ovl_file_inode_operations;
526 inode->i_op = &ovl_dir_inode_operations;
527 inode->i_fop = &ovl_dir_operations;
531 inode->i_op = &ovl_symlink_inode_operations;
535 inode->i_op = &ovl_file_inode_operations;
536 init_special_inode(inode, mode, rdev);
542 * With inodes index enabled, an overlay inode nlink counts the union of upper
543 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
544 * upper inode, the following nlink modifying operations can happen:
546 * 1. Lower hardlink copy up
547 * 2. Upper hardlink created, unlinked or renamed over
548 * 3. Lower hardlink whiteout or renamed over
550 * For the first, copy up case, the union nlink does not change, whether the
551 * operation succeeds or fails, but the upper inode nlink may change.
552 * Therefore, before copy up, we store the union nlink value relative to the
553 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
555 * For the second, upper hardlink case, the union nlink should be incremented
556 * or decremented IFF the operation succeeds, aligned with nlink change of the
557 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
558 * value relative to the upper inode nlink in the index inode.
560 * For the last, lower cover up case, we simplify things by preceding the
561 * whiteout or cover up with copy up. This makes sure that there is an index
562 * upper inode where the nlink xattr can be stored before the copied up upper
565 #define OVL_NLINK_ADD_UPPER (1 << 0)
568 * On-disk format for indexed nlink:
570 * nlink relative to the upper inode - "U[+-]NUM"
571 * nlink relative to the lower inode - "L[+-]NUM"
574 static int ovl_set_nlink_common(struct dentry *dentry,
575 struct dentry *realdentry, const char *format)
577 struct inode *inode = d_inode(dentry);
578 struct inode *realinode = d_inode(realdentry);
582 len = snprintf(buf, sizeof(buf), format,
583 (int) (inode->i_nlink - realinode->i_nlink));
585 if (WARN_ON(len >= sizeof(buf)))
588 return ovl_do_setxattr(ovl_dentry_upper(dentry),
589 OVL_XATTR_NLINK, buf, len, 0);
592 int ovl_set_nlink_upper(struct dentry *dentry)
594 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
597 int ovl_set_nlink_lower(struct dentry *dentry)
599 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
602 unsigned int ovl_get_nlink(struct dentry *lowerdentry,
603 struct dentry *upperdentry,
604 unsigned int fallback)
611 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
614 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
619 if ((buf[0] != 'L' && buf[0] != 'U') ||
620 (buf[1] != '+' && buf[1] != '-'))
623 err = kstrtoint(buf + 1, 10, &nlink_diff);
627 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
636 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
641 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
645 inode = new_inode(sb);
647 ovl_fill_inode(inode, mode, rdev, 0, 0);
652 static int ovl_inode_test(struct inode *inode, void *data)
654 return inode->i_private == data;
657 static int ovl_inode_set(struct inode *inode, void *data)
659 inode->i_private = data;
663 static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
664 struct dentry *upperdentry, bool strict)
667 * For directories, @strict verify from lookup path performs consistency
668 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
669 * inode. Non @strict verify from NFS handle decode path passes NULL for
670 * 'unknown' lower/upper.
672 if (S_ISDIR(inode->i_mode) && strict) {
673 /* Real lower dir moved to upper layer under us? */
674 if (!lowerdentry && ovl_inode_lower(inode))
677 /* Lookup of an uncovered redirect origin? */
678 if (!upperdentry && ovl_inode_upper(inode))
683 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
684 * This happens when finding a copied up overlay inode for a renamed
685 * or hardlinked overlay dentry and lower dentry cannot be followed
686 * by origin because lower fs does not support file handles.
688 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
692 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
693 * This happens when finding a lower alias for a copied up hard link.
695 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
701 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
704 struct inode *inode, *key = d_inode(real);
706 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
710 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
711 is_upper ? real : NULL, false)) {
713 return ERR_PTR(-ESTALE);
720 * Does overlay inode need to be hashed by lower inode?
722 static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
723 struct dentry *lower, struct dentry *index)
725 struct ovl_fs *ofs = sb->s_fs_info;
727 /* No, if pure upper */
731 /* Yes, if already indexed */
735 /* Yes, if won't be copied up */
739 /* No, if lower hardlink is or will be broken on copy up */
740 if ((upper || !ovl_indexdir(sb)) &&
741 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
744 /* No, if non-indexed upper with NFS export */
745 if (sb->s_export_op && upper)
748 /* Otherwise, hash by lower inode for fsnotify */
752 static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
755 return newinode ? inode_insert5(newinode, (unsigned long) key,
756 ovl_inode_test, ovl_inode_set, key) :
757 iget5_locked(sb, (unsigned long) key,
758 ovl_inode_test, ovl_inode_set, key);
761 struct inode *ovl_get_inode(struct super_block *sb,
762 struct ovl_inode_params *oip)
764 struct dentry *upperdentry = oip->upperdentry;
765 struct ovl_path *lowerpath = oip->lowerpath;
766 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
768 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
769 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
771 int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
773 unsigned long ino = 0;
776 realinode = d_inode(lowerdentry);
779 * Copy up origin (lower) may exist for non-indexed upper, but we must
780 * not use lower as hash key if this is a broken hardlink.
782 is_dir = S_ISDIR(realinode->i_mode);
783 if (upperdentry || bylower) {
784 struct inode *key = d_inode(bylower ? lowerdentry :
786 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
788 inode = ovl_iget5(sb, oip->newinode, key);
791 if (!(inode->i_state & I_NEW)) {
793 * Verify that the underlying files stored in the inode
794 * match those in the dentry.
796 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
799 inode = ERR_PTR(-ESTALE);
807 /* Recalculate nlink for non-dir due to indexing */
809 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
810 set_nlink(inode, nlink);
813 /* Lower hardlink that will be broken on copy up */
814 inode = new_inode(sb);
818 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
819 ovl_inode_init(inode, upperdentry, lowerdentry);
821 if (upperdentry && ovl_is_impuredir(upperdentry))
822 ovl_set_flag(OVL_IMPURE, inode);
825 ovl_set_flag(OVL_INDEX, inode);
827 /* Check for non-merge dir that may have whiteouts */
829 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
830 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
831 ovl_set_flag(OVL_WHITEOUTS, inode);
835 if (inode->i_state & I_NEW)
836 unlock_new_inode(inode);
841 inode = ERR_PTR(-ENOMEM);