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;
25 err = setattr_prepare(dentry, attr);
29 err = ovl_want_write(dentry);
33 if (attr->ia_valid & ATTR_SIZE) {
34 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
37 if (atomic_read(&realinode->i_writecount) < 0)
41 err = ovl_copy_up(dentry);
43 struct inode *winode = NULL;
45 upperdentry = ovl_dentry_upper(dentry);
47 if (attr->ia_valid & ATTR_SIZE) {
48 winode = d_inode(upperdentry);
49 err = get_write_access(winode);
54 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
55 attr->ia_valid &= ~ATTR_MODE;
57 inode_lock(upperdentry->d_inode);
58 old_cred = ovl_override_creds(dentry->d_sb);
59 err = notify_change(upperdentry, attr, NULL);
60 revert_creds(old_cred);
62 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
63 inode_unlock(upperdentry->d_inode);
66 put_write_access(winode);
69 ovl_drop_write(dentry);
74 static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat,
75 struct ovl_layer *lower_layer)
77 bool samefs = ovl_same_sb(dentry->d_sb);
78 unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
82 * When all layers are on the same fs, all real inode
83 * number are unique, so we use the overlay st_dev,
84 * which is friendly to du -x.
86 stat->dev = dentry->d_sb->s_dev;
88 } else if (xinobits) {
89 unsigned int shift = 64 - xinobits;
91 * All inode numbers of underlying fs should not be using the
92 * high xinobits, so we use high xinobits to partition the
93 * overlay st_ino address space. The high bits holds the fsid
94 * (upper fsid is 0). This way overlay inode numbers are unique
95 * and all inodes use overlay st_dev. Inode numbers are also
96 * persistent for a given layer configuration.
98 if (stat->ino >> shift) {
99 pr_warn_ratelimited("overlayfs: inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
100 dentry, stat->ino, xinobits);
103 stat->ino |= ((u64)lower_layer->fsid) << shift;
105 stat->dev = dentry->d_sb->s_dev;
110 /* The inode could not be mapped to a unified st_ino address space */
111 if (S_ISDIR(dentry->d_inode->i_mode)) {
113 * Always use the overlay st_dev for directories, so 'find
114 * -xdev' will scan the entire overlay mount and won't cross the
115 * overlay mount boundaries.
117 * If not all layers are on the same fs the pair {real st_ino;
118 * overlay st_dev} is not unique, so use the non persistent
119 * overlay st_ino for directories.
121 stat->dev = dentry->d_sb->s_dev;
122 stat->ino = dentry->d_inode->i_ino;
123 } else if (lower_layer && lower_layer->fsid) {
125 * For non-samefs setup, if we cannot map all layers st_ino
126 * to a unified address space, we need to make sure that st_dev
127 * is unique per lower fs. Upper layer uses real st_dev and
128 * lower layers use the unique anonymous bdev assigned to the
131 stat->dev = lower_layer->fs->pseudo_dev;
137 int ovl_getattr(const struct path *path, struct kstat *stat,
138 u32 request_mask, unsigned int flags)
140 struct dentry *dentry = path->dentry;
141 enum ovl_path_type type;
142 struct path realpath;
143 const struct cred *old_cred;
144 bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
145 bool samefs = ovl_same_sb(dentry->d_sb);
146 struct ovl_layer *lower_layer = NULL;
149 type = ovl_path_real(dentry, &realpath);
150 old_cred = ovl_override_creds(dentry->d_sb);
151 err = vfs_getattr(&realpath, stat, request_mask, flags);
156 * For non-dir or same fs, we use st_ino of the copy up origin.
157 * This guaranties constant st_dev/st_ino across copy up.
158 * With xino feature and non-samefs, we use st_ino of the copy up
159 * origin masked with high bits that represent the layer id.
161 * If lower filesystem supports NFS file handles, this also guaranties
162 * persistent st_ino across mount cycle.
164 if (!is_dir || samefs || ovl_xino_bits(dentry->d_sb)) {
165 if (!OVL_TYPE_UPPER(type)) {
166 lower_layer = ovl_layer_lower(dentry);
167 } else if (OVL_TYPE_ORIGIN(type)) {
168 struct kstat lowerstat;
169 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
171 ovl_path_lower(dentry, &realpath);
172 err = vfs_getattr(&realpath, &lowerstat,
178 * Lower hardlinks may be broken on copy up to different
179 * upper files, so we cannot use the lower origin st_ino
180 * for those different files, even for the same fs case.
182 * Similarly, several redirected dirs can point to the
183 * same dir on a lower layer. With the "verify_lower"
184 * feature, we do not use the lower origin st_ino, if
185 * we haven't verified that this redirect is unique.
187 * With inodes index enabled, it is safe to use st_ino
188 * of an indexed origin. The index validates that the
189 * upper hardlink is not broken and that a redirected
190 * dir is the only redirect to that origin.
192 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
193 (!ovl_verify_lower(dentry->d_sb) &&
194 (is_dir || lowerstat.nlink == 1))) {
195 stat->ino = lowerstat.ino;
196 lower_layer = ovl_layer_lower(dentry);
201 err = ovl_map_dev_ino(dentry, stat, lower_layer);
206 * It's probably not worth it to count subdirs to get the
207 * correct link count. nlink=1 seems to pacify 'find' and
210 if (is_dir && OVL_TYPE_MERGE(type))
214 * Return the overlay inode nlinks for indexed upper inodes.
215 * Overlay inode nlink counts the union of the upper hardlinks
216 * and non-covered lower hardlinks. It does not include the upper
219 if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
220 stat->nlink = dentry->d_inode->i_nlink;
223 revert_creds(old_cred);
228 int ovl_permission(struct inode *inode, int mask)
230 struct inode *upperinode = ovl_inode_upper(inode);
231 struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
232 const struct cred *old_cred;
235 /* Careful in RCU walk mode */
237 WARN_ON(!(mask & MAY_NOT_BLOCK));
242 * Check overlay inode with the creds of task and underlying inode
243 * with creds of mounter
245 err = generic_permission(inode, mask);
249 old_cred = ovl_override_creds(inode->i_sb);
251 !special_file(realinode->i_mode) && mask & MAY_WRITE) {
252 mask &= ~(MAY_WRITE | MAY_APPEND);
253 /* Make sure mounter can read file for copy up later */
256 err = inode_permission(realinode, mask);
257 revert_creds(old_cred);
262 static const char *ovl_get_link(struct dentry *dentry,
264 struct delayed_call *done)
266 const struct cred *old_cred;
270 return ERR_PTR(-ECHILD);
272 old_cred = ovl_override_creds(dentry->d_sb);
273 p = vfs_get_link(ovl_dentry_real(dentry), done);
274 revert_creds(old_cred);
278 bool ovl_is_private_xattr(const char *name)
280 return strncmp(name, OVL_XATTR_PREFIX,
281 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
284 int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
285 const void *value, size_t size, int flags)
288 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
289 struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
290 const struct cred *old_cred;
292 err = ovl_want_write(dentry);
296 if (!value && !upperdentry) {
297 err = vfs_getxattr(realdentry, name, NULL, 0);
303 err = ovl_copy_up(dentry);
307 realdentry = ovl_dentry_upper(dentry);
310 old_cred = ovl_override_creds(dentry->d_sb);
312 err = vfs_setxattr(realdentry, name, value, size, flags);
314 WARN_ON(flags != XATTR_REPLACE);
315 err = vfs_removexattr(realdentry, name);
317 revert_creds(old_cred);
320 ovl_copyattr(d_inode(realdentry), inode);
323 ovl_drop_write(dentry);
328 int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
329 void *value, size_t size)
332 const struct cred *old_cred;
333 struct dentry *realdentry =
334 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
336 old_cred = ovl_override_creds(dentry->d_sb);
337 res = vfs_getxattr(realdentry, name, value, size);
338 revert_creds(old_cred);
342 static bool ovl_can_list(const char *s)
344 /* List all non-trusted xatts */
345 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
348 /* Never list trusted.overlay, list other trusted for superuser only */
349 return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
352 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
354 struct dentry *realdentry = ovl_dentry_real(dentry);
358 const struct cred *old_cred;
360 old_cred = ovl_override_creds(dentry->d_sb);
361 res = vfs_listxattr(realdentry, list, size);
362 revert_creds(old_cred);
363 if (res <= 0 || size == 0)
366 /* filter out private xattrs */
367 for (s = list, len = res; len;) {
368 size_t slen = strnlen(s, len) + 1;
370 /* underlying fs providing us with an broken xattr list? */
371 if (WARN_ON(slen > len))
375 if (!ovl_can_list(s)) {
377 memmove(s, s + slen, len);
386 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
388 struct inode *realinode = ovl_inode_real(inode);
389 const struct cred *old_cred;
390 struct posix_acl *acl;
392 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
395 old_cred = ovl_override_creds(inode->i_sb);
396 acl = get_acl(realinode, type);
397 revert_creds(old_cred);
402 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
404 /* Copy up of disconnected dentry does not set upper alias */
405 if (ovl_dentry_upper(dentry) &&
406 (ovl_dentry_has_upper_alias(dentry) ||
407 (dentry->d_flags & DCACHE_DISCONNECTED)))
410 if (special_file(d_inode(dentry)->i_mode))
413 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
419 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
423 if (ovl_open_need_copy_up(dentry, file_flags)) {
424 err = ovl_want_write(dentry);
426 err = ovl_copy_up_flags(dentry, file_flags);
427 ovl_drop_write(dentry);
434 int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
436 if (flags & S_ATIME) {
437 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
438 struct path upperpath = {
439 .mnt = ofs->upper_mnt,
440 .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
443 if (upperpath.dentry) {
444 touch_atime(&upperpath);
445 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
451 static const struct inode_operations ovl_file_inode_operations = {
452 .setattr = ovl_setattr,
453 .permission = ovl_permission,
454 .getattr = ovl_getattr,
455 .listxattr = ovl_listxattr,
456 .get_acl = ovl_get_acl,
457 .update_time = ovl_update_time,
460 static const struct inode_operations ovl_symlink_inode_operations = {
461 .setattr = ovl_setattr,
462 .get_link = ovl_get_link,
463 .getattr = ovl_getattr,
464 .listxattr = ovl_listxattr,
465 .update_time = ovl_update_time,
469 * It is possible to stack overlayfs instance on top of another
470 * overlayfs instance as lower layer. We need to annonate the
471 * stackable i_mutex locks according to stack level of the super
472 * block instance. An overlayfs instance can never be in stack
473 * depth 0 (there is always a real fs below it). An overlayfs
474 * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
476 * For example, here is a snip from /proc/lockdep_chains after
477 * dir_iterate of nested overlayfs:
479 * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
480 * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
481 * [...] &type->i_mutex_dir_key (stack_depth=0)
483 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
485 static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
487 #ifdef CONFIG_LOCKDEP
488 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
489 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
490 static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
492 int depth = inode->i_sb->s_stack_depth - 1;
494 if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
497 if (S_ISDIR(inode->i_mode))
498 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
500 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
502 lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
506 static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev,
507 unsigned long ino, int fsid)
509 int xinobits = ovl_xino_bits(inode->i_sb);
512 * When NFS export is enabled and d_ino is consistent with st_ino
513 * (samefs or i_ino has enough bits to encode layer), set the same
514 * value used for d_ino to i_ino, because nfsd readdirplus compares
515 * d_ino values to i_ino values of child entries. When called from
516 * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real
517 * upper inode i_ino on ovl_inode_init() or ovl_inode_update().
519 if (inode->i_sb->s_export_op &&
520 (ovl_same_sb(inode->i_sb) || xinobits)) {
522 if (xinobits && fsid && !(ino >> (64 - xinobits)))
523 inode->i_ino |= (unsigned long)fsid << (64 - xinobits);
525 inode->i_ino = get_next_ino();
527 inode->i_mode = mode;
528 inode->i_flags |= S_NOCMTIME;
529 #ifdef CONFIG_FS_POSIX_ACL
530 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
533 ovl_lockdep_annotate_inode_mutex_key(inode);
535 switch (mode & S_IFMT) {
537 inode->i_op = &ovl_file_inode_operations;
538 inode->i_fop = &ovl_file_operations;
542 inode->i_op = &ovl_dir_inode_operations;
543 inode->i_fop = &ovl_dir_operations;
547 inode->i_op = &ovl_symlink_inode_operations;
551 inode->i_op = &ovl_file_inode_operations;
552 init_special_inode(inode, mode, rdev);
558 * With inodes index enabled, an overlay inode nlink counts the union of upper
559 * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
560 * upper inode, the following nlink modifying operations can happen:
562 * 1. Lower hardlink copy up
563 * 2. Upper hardlink created, unlinked or renamed over
564 * 3. Lower hardlink whiteout or renamed over
566 * For the first, copy up case, the union nlink does not change, whether the
567 * operation succeeds or fails, but the upper inode nlink may change.
568 * Therefore, before copy up, we store the union nlink value relative to the
569 * lower inode nlink in the index inode xattr trusted.overlay.nlink.
571 * For the second, upper hardlink case, the union nlink should be incremented
572 * or decremented IFF the operation succeeds, aligned with nlink change of the
573 * upper inode. Therefore, before link/unlink/rename, we store the union nlink
574 * value relative to the upper inode nlink in the index inode.
576 * For the last, lower cover up case, we simplify things by preceding the
577 * whiteout or cover up with copy up. This makes sure that there is an index
578 * upper inode where the nlink xattr can be stored before the copied up upper
581 #define OVL_NLINK_ADD_UPPER (1 << 0)
584 * On-disk format for indexed nlink:
586 * nlink relative to the upper inode - "U[+-]NUM"
587 * nlink relative to the lower inode - "L[+-]NUM"
590 static int ovl_set_nlink_common(struct dentry *dentry,
591 struct dentry *realdentry, const char *format)
593 struct inode *inode = d_inode(dentry);
594 struct inode *realinode = d_inode(realdentry);
598 len = snprintf(buf, sizeof(buf), format,
599 (int) (inode->i_nlink - realinode->i_nlink));
601 if (WARN_ON(len >= sizeof(buf)))
604 return ovl_do_setxattr(ovl_dentry_upper(dentry),
605 OVL_XATTR_NLINK, buf, len, 0);
608 int ovl_set_nlink_upper(struct dentry *dentry)
610 return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
613 int ovl_set_nlink_lower(struct dentry *dentry)
615 return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
618 unsigned int ovl_get_nlink(struct dentry *lowerdentry,
619 struct dentry *upperdentry,
620 unsigned int fallback)
627 if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
630 err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
635 if ((buf[0] != 'L' && buf[0] != 'U') ||
636 (buf[1] != '+' && buf[1] != '-'))
639 err = kstrtoint(buf + 1, 10, &nlink_diff);
643 nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
652 pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
657 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
661 inode = new_inode(sb);
663 ovl_fill_inode(inode, mode, rdev, 0, 0);
668 static int ovl_inode_test(struct inode *inode, void *data)
670 return inode->i_private == data;
673 static int ovl_inode_set(struct inode *inode, void *data)
675 inode->i_private = data;
679 static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
680 struct dentry *upperdentry, bool strict)
683 * For directories, @strict verify from lookup path performs consistency
684 * checks, so NULL lower/upper in dentry must match NULL lower/upper in
685 * inode. Non @strict verify from NFS handle decode path passes NULL for
686 * 'unknown' lower/upper.
688 if (S_ISDIR(inode->i_mode) && strict) {
689 /* Real lower dir moved to upper layer under us? */
690 if (!lowerdentry && ovl_inode_lower(inode))
693 /* Lookup of an uncovered redirect origin? */
694 if (!upperdentry && ovl_inode_upper(inode))
699 * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
700 * This happens when finding a copied up overlay inode for a renamed
701 * or hardlinked overlay dentry and lower dentry cannot be followed
702 * by origin because lower fs does not support file handles.
704 if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
708 * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
709 * This happens when finding a lower alias for a copied up hard link.
711 if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
717 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
720 struct inode *inode, *key = d_inode(real);
722 inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
726 if (!ovl_verify_inode(inode, is_upper ? NULL : real,
727 is_upper ? real : NULL, false)) {
729 return ERR_PTR(-ESTALE);
736 * Does overlay inode need to be hashed by lower inode?
738 static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
739 struct dentry *lower, struct dentry *index)
741 struct ovl_fs *ofs = sb->s_fs_info;
743 /* No, if pure upper */
747 /* Yes, if already indexed */
751 /* Yes, if won't be copied up */
755 /* No, if lower hardlink is or will be broken on copy up */
756 if ((upper || !ovl_indexdir(sb)) &&
757 !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
760 /* No, if non-indexed upper with NFS export */
761 if (sb->s_export_op && upper)
764 /* Otherwise, hash by lower inode for fsnotify */
768 static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
771 return newinode ? inode_insert5(newinode, (unsigned long) key,
772 ovl_inode_test, ovl_inode_set, key) :
773 iget5_locked(sb, (unsigned long) key,
774 ovl_inode_test, ovl_inode_set, key);
777 struct inode *ovl_get_inode(struct super_block *sb,
778 struct ovl_inode_params *oip)
780 struct dentry *upperdentry = oip->upperdentry;
781 struct ovl_path *lowerpath = oip->lowerpath;
782 struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
784 struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
785 bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
787 int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
789 unsigned long ino = 0;
792 realinode = d_inode(lowerdentry);
795 * Copy up origin (lower) may exist for non-indexed upper, but we must
796 * not use lower as hash key if this is a broken hardlink.
798 is_dir = S_ISDIR(realinode->i_mode);
799 if (upperdentry || bylower) {
800 struct inode *key = d_inode(bylower ? lowerdentry :
802 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
804 inode = ovl_iget5(sb, oip->newinode, key);
807 if (!(inode->i_state & I_NEW)) {
809 * Verify that the underlying files stored in the inode
810 * match those in the dentry.
812 if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
815 inode = ERR_PTR(-ESTALE);
823 /* Recalculate nlink for non-dir due to indexing */
825 nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink);
826 set_nlink(inode, nlink);
829 /* Lower hardlink that will be broken on copy up */
830 inode = new_inode(sb);
834 ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid);
835 ovl_inode_init(inode, upperdentry, lowerdentry);
837 if (upperdentry && ovl_is_impuredir(upperdentry))
838 ovl_set_flag(OVL_IMPURE, inode);
841 ovl_set_flag(OVL_INDEX, inode);
843 /* Check for non-merge dir that may have whiteouts */
845 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
846 ovl_check_origin_xattr(upperdentry ?: lowerdentry)) {
847 ovl_set_flag(OVL_WHITEOUTS, inode);
851 if (inode->i_state & I_NEW)
852 unlock_new_inode(inode);
857 inode = ERR_PTR(-ENOMEM);