Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2011 Novell Inc. |
| 4 | * |
| 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. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/slab.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 12 | #include <linux/cred.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 13 | #include <linux/xattr.h> |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 14 | #include <linux/posix_acl.h> |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 15 | #include <linux/ratelimit.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 16 | #include "overlayfs.h" |
| 17 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 18 | int ovl_setattr(struct dentry *dentry, struct iattr *attr) |
| 19 | { |
| 20 | int err; |
| 21 | struct dentry *upperdentry; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 22 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 23 | |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 24 | /* |
| 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. |
| 28 | * |
| 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). |
| 32 | */ |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 33 | err = setattr_prepare(dentry, attr); |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 34 | if (err) |
| 35 | return err; |
| 36 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 37 | err = ovl_want_write(dentry); |
| 38 | if (err) |
| 39 | goto out; |
| 40 | |
Miklos Szeredi | acff81e | 2015-12-04 19:18:48 +0100 | [diff] [blame] | 41 | err = ovl_copy_up(dentry); |
| 42 | if (!err) { |
| 43 | upperdentry = ovl_dentry_upper(dentry); |
| 44 | |
Miklos Szeredi | b99c2d9 | 2016-07-04 16:49:48 +0200 | [diff] [blame] | 45 | if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 46 | attr->ia_valid &= ~ATTR_MODE; |
| 47 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 48 | inode_lock(upperdentry->d_inode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 49 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 50 | err = notify_change(upperdentry, attr, NULL); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 51 | revert_creds(old_cred); |
Konstantin Khlebnikov | b81de06 | 2016-01-31 16:21:29 +0300 | [diff] [blame] | 52 | if (!err) |
| 53 | ovl_copyattr(upperdentry->d_inode, dentry->d_inode); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 54 | inode_unlock(upperdentry->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 55 | } |
| 56 | ovl_drop_write(dentry); |
| 57 | out: |
| 58 | return err; |
| 59 | } |
| 60 | |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 61 | int ovl_getattr(const struct path *path, struct kstat *stat, |
| 62 | u32 request_mask, unsigned int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 63 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 64 | struct dentry *dentry = path->dentry; |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 65 | enum ovl_path_type type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 66 | struct path realpath; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 67 | const struct cred *old_cred; |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 68 | bool is_dir = S_ISDIR(dentry->d_inode->i_mode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 69 | int err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 70 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 71 | type = ovl_path_real(dentry, &realpath); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 72 | old_cred = ovl_override_creds(dentry->d_sb); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 73 | err = vfs_getattr(&realpath, stat, request_mask, flags); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 74 | if (err) |
| 75 | goto out; |
| 76 | |
| 77 | /* |
| 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. |
| 80 | * |
| 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. |
| 83 | * |
| 84 | * If filesystem supports NFS export ops, this also guaranties |
| 85 | * persistent st_ino across mount cycle. |
| 86 | */ |
| 87 | if (ovl_same_sb(dentry->d_sb)) { |
| 88 | if (OVL_TYPE_ORIGIN(type)) { |
| 89 | struct kstat lowerstat; |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 90 | u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 91 | |
| 92 | ovl_path_lower(dentry, &realpath); |
| 93 | err = vfs_getattr(&realpath, &lowerstat, |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 94 | lowermask, flags); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 95 | if (err) |
| 96 | goto out; |
| 97 | |
| 98 | WARN_ON_ONCE(stat->dev != lowerstat.dev); |
| 99 | /* |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 100 | * Lower hardlinks may be broken on copy up to different |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 101 | * upper files, so we cannot use the lower origin st_ino |
| 102 | * for those different files, even for the same fs case. |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 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. |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 106 | */ |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 107 | if (is_dir || lowerstat.nlink == 1 || |
| 108 | ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 109 | stat->ino = lowerstat.ino; |
| 110 | } |
| 111 | stat->dev = dentry->d_sb->s_dev; |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 112 | } else if (is_dir) { |
| 113 | /* |
| 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 |
| 116 | * overlay st_ino. |
| 117 | * |
| 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. |
| 121 | */ |
| 122 | stat->dev = dentry->d_sb->s_dev; |
| 123 | stat->ino = dentry->d_inode->i_ino; |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 124 | } |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * It's probably not worth it to count subdirs to get the |
| 128 | * correct link count. nlink=1 seems to pacify 'find' and |
| 129 | * other utilities. |
| 130 | */ |
| 131 | if (is_dir && OVL_TYPE_MERGE(type)) |
| 132 | stat->nlink = 1; |
| 133 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 134 | /* |
| 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 |
| 138 | * index hardlink. |
| 139 | */ |
| 140 | if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
| 141 | stat->nlink = dentry->d_inode->i_nlink; |
| 142 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 143 | out: |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 144 | revert_creds(old_cred); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 145 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 146 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int ovl_permission(struct inode *inode, int mask) |
| 150 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 151 | struct inode *upperinode = ovl_inode_upper(inode); |
| 152 | struct inode *realinode = upperinode ?: ovl_inode_lower(inode); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 153 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 154 | int err; |
| 155 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 156 | /* Careful in RCU walk mode */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 157 | if (!realinode) { |
| 158 | WARN_ON(!(mask & MAY_NOT_BLOCK)); |
Miklos Szeredi | a999d7e | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 159 | return -ECHILD; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 162 | /* |
| 163 | * Check overlay inode with the creds of task and underlying inode |
| 164 | * with creds of mounter |
| 165 | */ |
| 166 | err = generic_permission(inode, mask); |
| 167 | if (err) |
| 168 | return err; |
| 169 | |
| 170 | old_cred = ovl_override_creds(inode->i_sb); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 171 | if (!upperinode && |
| 172 | !special_file(realinode->i_mode) && mask & MAY_WRITE) { |
Vivek Goyal | 754f8cb | 2016-07-01 16:34:29 -0400 | [diff] [blame] | 173 | mask &= ~(MAY_WRITE | MAY_APPEND); |
Vivek Goyal | 500cac3 | 2016-07-13 11:00:14 -0400 | [diff] [blame] | 174 | /* Make sure mounter can read file for copy up later */ |
| 175 | mask |= MAY_READ; |
| 176 | } |
Miklos Szeredi | 9c630eb | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 177 | err = inode_permission(realinode, mask); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 178 | revert_creds(old_cred); |
| 179 | |
| 180 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 183 | static const char *ovl_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 184 | struct inode *inode, |
| 185 | struct delayed_call *done) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 186 | { |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 187 | const struct cred *old_cred; |
| 188 | const char *p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 189 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 190 | if (!dentry) |
| 191 | return ERR_PTR(-ECHILD); |
| 192 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 193 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 194 | p = vfs_get_link(ovl_dentry_real(dentry), done); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 195 | revert_creds(old_cred); |
| 196 | return p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Miklos Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 199 | bool ovl_is_private_xattr(const char *name) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 200 | { |
Andreas Gruenbacher | fe2b759 | 2016-08-22 17:59:22 +0200 | [diff] [blame] | 201 | return strncmp(name, OVL_XATTR_PREFIX, |
| 202 | sizeof(OVL_XATTR_PREFIX) - 1) == 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 205 | int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, |
| 206 | const void *value, size_t size, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 207 | { |
| 208 | int err; |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 209 | struct dentry *upperdentry = ovl_i_dentry_upper(inode); |
| 210 | struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 211 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 212 | |
| 213 | err = ovl_want_write(dentry); |
| 214 | if (err) |
| 215 | goto out; |
| 216 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 217 | if (!value && !upperdentry) { |
| 218 | err = vfs_getxattr(realdentry, name, NULL, 0); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 219 | if (err < 0) |
| 220 | goto out_drop_write; |
| 221 | } |
| 222 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 223 | if (!upperdentry) { |
| 224 | err = ovl_copy_up(dentry); |
| 225 | if (err) |
| 226 | goto out_drop_write; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 227 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 228 | realdentry = ovl_dentry_upper(dentry); |
| 229 | } |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 230 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 231 | old_cred = ovl_override_creds(dentry->d_sb); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 232 | if (value) |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 233 | err = vfs_setxattr(realdentry, name, value, size, flags); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 234 | else { |
| 235 | WARN_ON(flags != XATTR_REPLACE); |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 236 | err = vfs_removexattr(realdentry, name); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 237 | } |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 238 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 239 | |
| 240 | out_drop_write: |
| 241 | ovl_drop_write(dentry); |
| 242 | out: |
| 243 | return err; |
| 244 | } |
| 245 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 246 | int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 247 | void *value, size_t size) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 248 | { |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 249 | ssize_t res; |
| 250 | const struct cred *old_cred; |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 251 | struct dentry *realdentry = |
| 252 | ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry); |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 253 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 254 | old_cred = ovl_override_creds(dentry->d_sb); |
| 255 | res = vfs_getxattr(realdentry, name, value, size); |
| 256 | revert_creds(old_cred); |
| 257 | return res; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Miklos Szeredi | a082c6f | 2017-05-29 15:15:27 +0200 | [diff] [blame] | 260 | static bool ovl_can_list(const char *s) |
| 261 | { |
| 262 | /* List all non-trusted xatts */ |
| 263 | if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) |
| 264 | return true; |
| 265 | |
| 266 | /* Never list trusted.overlay, list other trusted for superuser only */ |
| 267 | return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN); |
| 268 | } |
| 269 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 270 | ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) |
| 271 | { |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 272 | struct dentry *realdentry = ovl_dentry_real(dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 273 | ssize_t res; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 274 | size_t len; |
| 275 | char *s; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 276 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 277 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 278 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 279 | res = vfs_listxattr(realdentry, list, size); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 280 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 281 | if (res <= 0 || size == 0) |
| 282 | return res; |
| 283 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 284 | /* filter out private xattrs */ |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 285 | for (s = list, len = res; len;) { |
| 286 | size_t slen = strnlen(s, len) + 1; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 287 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 288 | /* underlying fs providing us with an broken xattr list? */ |
| 289 | if (WARN_ON(slen > len)) |
| 290 | return -EIO; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 291 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 292 | len -= slen; |
Miklos Szeredi | a082c6f | 2017-05-29 15:15:27 +0200 | [diff] [blame] | 293 | if (!ovl_can_list(s)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 294 | res -= slen; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 295 | memmove(s, s + slen, len); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 296 | } else { |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 297 | s += slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | return res; |
| 302 | } |
| 303 | |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 304 | struct posix_acl *ovl_get_acl(struct inode *inode, int type) |
| 305 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 306 | struct inode *realinode = ovl_inode_real(inode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 307 | const struct cred *old_cred; |
| 308 | struct posix_acl *acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 309 | |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 310 | if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 311 | return NULL; |
| 312 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 313 | old_cred = ovl_override_creds(inode->i_sb); |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 314 | acl = get_acl(realinode, type); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 315 | revert_creds(old_cred); |
| 316 | |
| 317 | return acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 320 | static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 321 | { |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 322 | if (ovl_dentry_upper(dentry) && |
| 323 | ovl_dentry_has_upper_alias(dentry)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 324 | return false; |
| 325 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 326 | if (special_file(d_inode(dentry)->i_mode)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 327 | return false; |
| 328 | |
| 329 | if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC)) |
| 330 | return false; |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 335 | int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 336 | { |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 337 | int err = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 338 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 339 | if (ovl_open_need_copy_up(dentry, file_flags)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 340 | err = ovl_want_write(dentry); |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 341 | if (!err) { |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 342 | err = ovl_copy_up_flags(dentry, file_flags); |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 343 | ovl_drop_write(dentry); |
| 344 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 347 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 350 | int ovl_update_time(struct inode *inode, struct timespec *ts, int flags) |
| 351 | { |
| 352 | struct dentry *alias; |
| 353 | struct path upperpath; |
| 354 | |
| 355 | if (!(flags & S_ATIME)) |
| 356 | return 0; |
| 357 | |
| 358 | alias = d_find_any_alias(inode); |
| 359 | if (!alias) |
| 360 | return 0; |
| 361 | |
| 362 | ovl_path_upper(alias, &upperpath); |
| 363 | if (upperpath.dentry) { |
| 364 | touch_atime(&upperpath); |
| 365 | inode->i_atime = d_inode(upperpath.dentry)->i_atime; |
| 366 | } |
| 367 | |
| 368 | dput(alias); |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 373 | static const struct inode_operations ovl_file_inode_operations = { |
| 374 | .setattr = ovl_setattr, |
| 375 | .permission = ovl_permission, |
| 376 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 377 | .listxattr = ovl_listxattr, |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 378 | .get_acl = ovl_get_acl, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 379 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | static const struct inode_operations ovl_symlink_inode_operations = { |
| 383 | .setattr = ovl_setattr, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 384 | .get_link = ovl_get_link, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 385 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 386 | .listxattr = ovl_listxattr, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 387 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 388 | }; |
| 389 | |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 390 | /* |
| 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]. |
| 397 | * |
| 398 | * For example, here is a snip from /proc/lockdep_chains after |
| 399 | * dir_iterate of nested overlayfs: |
| 400 | * |
| 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) |
| 404 | */ |
| 405 | #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH |
| 406 | |
| 407 | static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) |
| 408 | { |
| 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]; |
| 412 | |
| 413 | int depth = inode->i_sb->s_stack_depth - 1; |
| 414 | |
| 415 | if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) |
| 416 | depth = 0; |
| 417 | |
| 418 | if (S_ISDIR(inode->i_mode)) |
| 419 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); |
| 420 | else |
| 421 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); |
| 422 | #endif |
| 423 | } |
| 424 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 425 | static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 426 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 427 | inode->i_ino = get_next_ino(); |
| 428 | inode->i_mode = mode; |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 429 | inode->i_flags |= S_NOCMTIME; |
Miklos Szeredi | 2a3a2a3 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 430 | #ifdef CONFIG_FS_POSIX_ACL |
| 431 | inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; |
| 432 | #endif |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 433 | |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 434 | ovl_lockdep_annotate_inode_mutex_key(inode); |
| 435 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 436 | switch (mode & S_IFMT) { |
| 437 | case S_IFREG: |
| 438 | inode->i_op = &ovl_file_inode_operations; |
| 439 | break; |
| 440 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 441 | case S_IFDIR: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 442 | inode->i_op = &ovl_dir_inode_operations; |
| 443 | inode->i_fop = &ovl_dir_operations; |
| 444 | break; |
| 445 | |
| 446 | case S_IFLNK: |
| 447 | inode->i_op = &ovl_symlink_inode_operations; |
| 448 | break; |
| 449 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 450 | default: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 451 | inode->i_op = &ovl_file_inode_operations; |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 452 | init_special_inode(inode, mode, rdev); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 453 | break; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 454 | } |
| 455 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 456 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 457 | /* |
| 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: |
| 461 | * |
| 462 | * 1. Lower hardlink copy up |
| 463 | * 2. Upper hardlink created, unlinked or renamed over |
| 464 | * 3. Lower hardlink whiteout or renamed over |
| 465 | * |
| 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. |
| 470 | * |
| 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. |
| 475 | * |
| 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 |
| 479 | * entry is unlink. |
| 480 | */ |
| 481 | #define OVL_NLINK_ADD_UPPER (1 << 0) |
| 482 | |
| 483 | /* |
| 484 | * On-disk format for indexed nlink: |
| 485 | * |
| 486 | * nlink relative to the upper inode - "U[+-]NUM" |
| 487 | * nlink relative to the lower inode - "L[+-]NUM" |
| 488 | */ |
| 489 | |
| 490 | static int ovl_set_nlink_common(struct dentry *dentry, |
| 491 | struct dentry *realdentry, const char *format) |
| 492 | { |
| 493 | struct inode *inode = d_inode(dentry); |
| 494 | struct inode *realinode = d_inode(realdentry); |
| 495 | char buf[13]; |
| 496 | int len; |
| 497 | |
| 498 | len = snprintf(buf, sizeof(buf), format, |
| 499 | (int) (inode->i_nlink - realinode->i_nlink)); |
| 500 | |
| 501 | return ovl_do_setxattr(ovl_dentry_upper(dentry), |
| 502 | OVL_XATTR_NLINK, buf, len, 0); |
| 503 | } |
| 504 | |
| 505 | int ovl_set_nlink_upper(struct dentry *dentry) |
| 506 | { |
| 507 | return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); |
| 508 | } |
| 509 | |
| 510 | int ovl_set_nlink_lower(struct dentry *dentry) |
| 511 | { |
| 512 | return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); |
| 513 | } |
| 514 | |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 515 | unsigned int ovl_get_nlink(struct dentry *lowerdentry, |
| 516 | struct dentry *upperdentry, |
| 517 | unsigned int fallback) |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 518 | { |
| 519 | int nlink_diff; |
| 520 | int nlink; |
| 521 | char buf[13]; |
| 522 | int err; |
| 523 | |
| 524 | if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) |
| 525 | return fallback; |
| 526 | |
| 527 | err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1); |
| 528 | if (err < 0) |
| 529 | goto fail; |
| 530 | |
| 531 | buf[err] = '\0'; |
| 532 | if ((buf[0] != 'L' && buf[0] != 'U') || |
| 533 | (buf[1] != '+' && buf[1] != '-')) |
| 534 | goto fail; |
| 535 | |
| 536 | err = kstrtoint(buf + 1, 10, &nlink_diff); |
| 537 | if (err < 0) |
| 538 | goto fail; |
| 539 | |
| 540 | nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; |
| 541 | nlink += nlink_diff; |
| 542 | |
| 543 | if (nlink <= 0) |
| 544 | goto fail; |
| 545 | |
| 546 | return nlink; |
| 547 | |
| 548 | fail: |
| 549 | pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n", |
| 550 | upperdentry, err); |
| 551 | return fallback; |
| 552 | } |
| 553 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 554 | struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev) |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 555 | { |
| 556 | struct inode *inode; |
| 557 | |
| 558 | inode = new_inode(sb); |
| 559 | if (inode) |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 560 | ovl_fill_inode(inode, mode, rdev); |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 561 | |
| 562 | return inode; |
| 563 | } |
| 564 | |
| 565 | static int ovl_inode_test(struct inode *inode, void *data) |
| 566 | { |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 567 | return inode->i_private == data; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static int ovl_inode_set(struct inode *inode, void *data) |
| 571 | { |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 572 | inode->i_private = data; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 576 | static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, |
| 577 | struct dentry *upperdentry) |
| 578 | { |
| 579 | struct inode *lowerinode = lowerdentry ? d_inode(lowerdentry) : NULL; |
| 580 | |
| 581 | /* Lower (origin) inode must match, even if NULL */ |
| 582 | if (ovl_inode_lower(inode) != lowerinode) |
| 583 | return false; |
| 584 | |
| 585 | /* |
| 586 | * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. |
| 587 | * This happens when finding a lower alias for a copied up hard link. |
| 588 | */ |
| 589 | if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) |
| 590 | return false; |
| 591 | |
| 592 | return true; |
| 593 | } |
| 594 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 595 | struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry) |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 596 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 597 | struct dentry *lowerdentry = ovl_dentry_lower(dentry); |
| 598 | struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 599 | struct inode *inode; |
| 600 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 601 | if (!realinode) |
| 602 | realinode = d_inode(lowerdentry); |
| 603 | |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 604 | if (!S_ISDIR(realinode->i_mode) && |
| 605 | (upperdentry || (lowerdentry && ovl_indexdir(dentry->d_sb)))) { |
| 606 | struct inode *key = d_inode(lowerdentry ?: upperdentry); |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 607 | unsigned int nlink; |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 608 | |
| 609 | inode = iget5_locked(dentry->d_sb, (unsigned long) key, |
| 610 | ovl_inode_test, ovl_inode_set, key); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 611 | if (!inode) |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 612 | goto out_nomem; |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 613 | if (!(inode->i_state & I_NEW)) { |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 614 | /* |
| 615 | * Verify that the underlying files stored in the inode |
| 616 | * match those in the dentry. |
| 617 | */ |
| 618 | if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) { |
| 619 | iput(inode); |
| 620 | inode = ERR_PTR(-ESTALE); |
| 621 | goto out; |
| 622 | } |
| 623 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 624 | dput(upperdentry); |
| 625 | goto out; |
| 626 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 627 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 628 | nlink = ovl_get_nlink(lowerdentry, upperdentry, |
| 629 | realinode->i_nlink); |
| 630 | set_nlink(inode, nlink); |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 631 | } else { |
| 632 | inode = new_inode(dentry->d_sb); |
| 633 | if (!inode) |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 634 | goto out_nomem; |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 635 | } |
| 636 | ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 637 | ovl_inode_init(inode, upperdentry, lowerdentry); |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 638 | |
| 639 | if (upperdentry && ovl_is_impuredir(upperdentry)) |
| 640 | ovl_set_flag(OVL_IMPURE, inode); |
| 641 | |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 642 | if (inode->i_state & I_NEW) |
| 643 | unlock_new_inode(inode); |
| 644 | out: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 645 | return inode; |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 646 | |
| 647 | out_nomem: |
| 648 | inode = ERR_PTR(-ENOMEM); |
| 649 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 650 | } |