Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Copyright (C) 2011 Novell Inc. |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/fs.h> |
| 8 | #include <linux/slab.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 9 | #include <linux/cred.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 10 | #include <linux/xattr.h> |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 11 | #include <linux/posix_acl.h> |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 12 | #include <linux/ratelimit.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 13 | #include "overlayfs.h" |
| 14 | |
Chandan Rajendra | ba1e563 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 15 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 16 | int ovl_setattr(struct dentry *dentry, struct iattr *attr) |
| 17 | { |
| 18 | int err; |
Vivek Goyal | 997336f | 2018-05-11 11:49:33 -0400 | [diff] [blame] | 19 | bool full_copy_up = false; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 20 | struct dentry *upperdentry; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 21 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 22 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 23 | err = setattr_prepare(dentry, attr); |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 24 | if (err) |
| 25 | return err; |
| 26 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 27 | err = ovl_want_write(dentry); |
| 28 | if (err) |
| 29 | goto out; |
| 30 | |
Miklos Szeredi | 5812160 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 31 | if (attr->ia_valid & ATTR_SIZE) { |
| 32 | struct inode *realinode = d_inode(ovl_dentry_real(dentry)); |
| 33 | |
| 34 | err = -ETXTBSY; |
| 35 | if (atomic_read(&realinode->i_writecount) < 0) |
| 36 | goto out_drop_write; |
Vivek Goyal | 997336f | 2018-05-11 11:49:33 -0400 | [diff] [blame] | 37 | |
| 38 | /* Truncate should trigger data copy up as well */ |
| 39 | full_copy_up = true; |
Miklos Szeredi | 5812160 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Vivek Goyal | 997336f | 2018-05-11 11:49:33 -0400 | [diff] [blame] | 42 | if (!full_copy_up) |
| 43 | err = ovl_copy_up(dentry); |
| 44 | else |
| 45 | err = ovl_copy_up_with_data(dentry); |
Miklos Szeredi | acff81e | 2015-12-04 19:18:48 +0100 | [diff] [blame] | 46 | if (!err) { |
Miklos Szeredi | 5812160 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 47 | struct inode *winode = NULL; |
| 48 | |
Miklos Szeredi | acff81e | 2015-12-04 19:18:48 +0100 | [diff] [blame] | 49 | upperdentry = ovl_dentry_upper(dentry); |
| 50 | |
Miklos Szeredi | 5812160 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 51 | if (attr->ia_valid & ATTR_SIZE) { |
| 52 | winode = d_inode(upperdentry); |
| 53 | err = get_write_access(winode); |
| 54 | if (err) |
| 55 | goto out_drop_write; |
| 56 | } |
| 57 | |
Miklos Szeredi | b99c2d9 | 2016-07-04 16:49:48 +0200 | [diff] [blame] | 58 | if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 59 | attr->ia_valid &= ~ATTR_MODE; |
| 60 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 61 | inode_lock(upperdentry->d_inode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 62 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 63 | err = notify_change(upperdentry, attr, NULL); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 64 | revert_creds(old_cred); |
Konstantin Khlebnikov | b81de06 | 2016-01-31 16:21:29 +0300 | [diff] [blame] | 65 | if (!err) |
| 66 | ovl_copyattr(upperdentry->d_inode, dentry->d_inode); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 67 | inode_unlock(upperdentry->d_inode); |
Miklos Szeredi | 5812160 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 68 | |
| 69 | if (winode) |
| 70 | put_write_access(winode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 71 | } |
Miklos Szeredi | 5812160 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 72 | out_drop_write: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 73 | ovl_drop_write(dentry); |
| 74 | out: |
| 75 | return err; |
| 76 | } |
| 77 | |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 78 | static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, |
| 79 | struct ovl_layer *lower_layer) |
| 80 | { |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame^] | 81 | bool samefs = ovl_same_fs(dentry->d_sb); |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 82 | unsigned int xinobits = ovl_xino_bits(dentry->d_sb); |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 83 | |
| 84 | if (samefs) { |
| 85 | /* |
| 86 | * When all layers are on the same fs, all real inode |
| 87 | * number are unique, so we use the overlay st_dev, |
| 88 | * which is friendly to du -x. |
| 89 | */ |
| 90 | stat->dev = dentry->d_sb->s_dev; |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 91 | return 0; |
| 92 | } else if (xinobits) { |
| 93 | unsigned int shift = 64 - xinobits; |
| 94 | /* |
| 95 | * All inode numbers of underlying fs should not be using the |
| 96 | * high xinobits, so we use high xinobits to partition the |
| 97 | * overlay st_ino address space. The high bits holds the fsid |
| 98 | * (upper fsid is 0). This way overlay inode numbers are unique |
| 99 | * and all inodes use overlay st_dev. Inode numbers are also |
| 100 | * persistent for a given layer configuration. |
| 101 | */ |
| 102 | if (stat->ino >> shift) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 103 | pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n", |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 104 | dentry, stat->ino, xinobits); |
| 105 | } else { |
| 106 | if (lower_layer) |
| 107 | stat->ino |= ((u64)lower_layer->fsid) << shift; |
| 108 | |
| 109 | stat->dev = dentry->d_sb->s_dev; |
| 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* The inode could not be mapped to a unified st_ino address space */ |
| 115 | if (S_ISDIR(dentry->d_inode->i_mode)) { |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 116 | /* |
| 117 | * Always use the overlay st_dev for directories, so 'find |
| 118 | * -xdev' will scan the entire overlay mount and won't cross the |
| 119 | * overlay mount boundaries. |
| 120 | * |
| 121 | * If not all layers are on the same fs the pair {real st_ino; |
| 122 | * overlay st_dev} is not unique, so use the non persistent |
| 123 | * overlay st_ino for directories. |
| 124 | */ |
| 125 | stat->dev = dentry->d_sb->s_dev; |
| 126 | stat->ino = dentry->d_inode->i_ino; |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 127 | } else if (lower_layer && lower_layer->fsid) { |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 128 | /* |
| 129 | * For non-samefs setup, if we cannot map all layers st_ino |
| 130 | * to a unified address space, we need to make sure that st_dev |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 131 | * is unique per lower fs. Upper layer uses real st_dev and |
| 132 | * lower layers use the unique anonymous bdev assigned to the |
| 133 | * lower fs. |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 134 | */ |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 135 | stat->dev = lower_layer->fs->pseudo_dev; |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 141 | int ovl_getattr(const struct path *path, struct kstat *stat, |
| 142 | u32 request_mask, unsigned int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 143 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 144 | struct dentry *dentry = path->dentry; |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 145 | enum ovl_path_type type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 146 | struct path realpath; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 147 | const struct cred *old_cred; |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 148 | bool is_dir = S_ISDIR(dentry->d_inode->i_mode); |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame^] | 149 | bool samefs = ovl_same_fs(dentry->d_sb); |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 150 | struct ovl_layer *lower_layer = NULL; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 151 | int err; |
Vivek Goyal | 67d756c2 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 152 | bool metacopy_blocks = false; |
| 153 | |
| 154 | metacopy_blocks = ovl_is_metacopy_dentry(dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 155 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 156 | type = ovl_path_real(dentry, &realpath); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 157 | old_cred = ovl_override_creds(dentry->d_sb); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 158 | err = vfs_getattr(&realpath, stat, request_mask, flags); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 159 | if (err) |
| 160 | goto out; |
| 161 | |
| 162 | /* |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 163 | * For non-dir or same fs, we use st_ino of the copy up origin. |
| 164 | * This guaranties constant st_dev/st_ino across copy up. |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 165 | * With xino feature and non-samefs, we use st_ino of the copy up |
| 166 | * origin masked with high bits that represent the layer id. |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 167 | * |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 168 | * If lower filesystem supports NFS file handles, this also guaranties |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 169 | * persistent st_ino across mount cycle. |
| 170 | */ |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame^] | 171 | if (!is_dir || ovl_same_dev(dentry->d_sb)) { |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 172 | if (!OVL_TYPE_UPPER(type)) { |
| 173 | lower_layer = ovl_layer_lower(dentry); |
| 174 | } else if (OVL_TYPE_ORIGIN(type)) { |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 175 | struct kstat lowerstat; |
Vivek Goyal | 67d756c2 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 176 | u32 lowermask = STATX_INO | STATX_BLOCKS | |
| 177 | (!is_dir ? STATX_NLINK : 0); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 178 | |
| 179 | ovl_path_lower(dentry, &realpath); |
| 180 | err = vfs_getattr(&realpath, &lowerstat, |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 181 | lowermask, flags); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 182 | if (err) |
| 183 | goto out; |
| 184 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 185 | /* |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 186 | * Lower hardlinks may be broken on copy up to different |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 187 | * upper files, so we cannot use the lower origin st_ino |
| 188 | * for those different files, even for the same fs case. |
Amir Goldstein | 86eaa13 | 2017-11-21 13:55:51 +0200 | [diff] [blame] | 189 | * |
| 190 | * Similarly, several redirected dirs can point to the |
| 191 | * same dir on a lower layer. With the "verify_lower" |
| 192 | * feature, we do not use the lower origin st_ino, if |
| 193 | * we haven't verified that this redirect is unique. |
| 194 | * |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 195 | * With inodes index enabled, it is safe to use st_ino |
Amir Goldstein | 86eaa13 | 2017-11-21 13:55:51 +0200 | [diff] [blame] | 196 | * of an indexed origin. The index validates that the |
| 197 | * upper hardlink is not broken and that a redirected |
| 198 | * dir is the only redirect to that origin. |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 199 | */ |
Amir Goldstein | 86eaa13 | 2017-11-21 13:55:51 +0200 | [diff] [blame] | 200 | if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) || |
| 201 | (!ovl_verify_lower(dentry->d_sb) && |
Amir Goldstein | 9f99e50 | 2018-04-11 20:09:29 +0300 | [diff] [blame] | 202 | (is_dir || lowerstat.nlink == 1))) { |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 203 | lower_layer = ovl_layer_lower(dentry); |
Amir Goldstein | 9c6d8f1 | 2019-11-17 17:43:44 +0200 | [diff] [blame] | 204 | /* |
| 205 | * Cannot use origin st_dev;st_ino because |
| 206 | * origin inode content may differ from overlay |
| 207 | * inode content. |
| 208 | */ |
| 209 | if (samefs || lower_layer->fsid) |
| 210 | stat->ino = lowerstat.ino; |
Amir Goldstein | 9f99e50 | 2018-04-11 20:09:29 +0300 | [diff] [blame] | 211 | } |
Vivek Goyal | 67d756c2 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * If we are querying a metacopy dentry and lower |
| 215 | * dentry is data dentry, then use the blocks we |
| 216 | * queried just now. We don't have to do additional |
| 217 | * vfs_getattr(). If lower itself is metacopy, then |
| 218 | * additional vfs_getattr() is unavoidable. |
| 219 | */ |
| 220 | if (metacopy_blocks && |
| 221 | realpath.dentry == ovl_dentry_lowerdata(dentry)) { |
| 222 | stat->blocks = lowerstat.blocks; |
| 223 | metacopy_blocks = false; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (metacopy_blocks) { |
| 228 | /* |
| 229 | * If lower is not same as lowerdata or if there was |
| 230 | * no origin on upper, we can end up here. |
| 231 | */ |
| 232 | struct kstat lowerdatastat; |
| 233 | u32 lowermask = STATX_BLOCKS; |
| 234 | |
| 235 | ovl_path_lowerdata(dentry, &realpath); |
| 236 | err = vfs_getattr(&realpath, &lowerdatastat, |
| 237 | lowermask, flags); |
| 238 | if (err) |
| 239 | goto out; |
| 240 | stat->blocks = lowerdatastat.blocks; |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 241 | } |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 242 | } |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 243 | |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 244 | err = ovl_map_dev_ino(dentry, stat, lower_layer); |
| 245 | if (err) |
| 246 | goto out; |
| 247 | |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame] | 248 | /* |
| 249 | * It's probably not worth it to count subdirs to get the |
| 250 | * correct link count. nlink=1 seems to pacify 'find' and |
| 251 | * other utilities. |
| 252 | */ |
| 253 | if (is_dir && OVL_TYPE_MERGE(type)) |
| 254 | stat->nlink = 1; |
| 255 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 256 | /* |
| 257 | * Return the overlay inode nlinks for indexed upper inodes. |
| 258 | * Overlay inode nlink counts the union of the upper hardlinks |
| 259 | * and non-covered lower hardlinks. It does not include the upper |
| 260 | * index hardlink. |
| 261 | */ |
| 262 | if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
| 263 | stat->nlink = dentry->d_inode->i_nlink; |
| 264 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 265 | out: |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 266 | revert_creds(old_cred); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 267 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 268 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | int ovl_permission(struct inode *inode, int mask) |
| 272 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 273 | struct inode *upperinode = ovl_inode_upper(inode); |
| 274 | struct inode *realinode = upperinode ?: ovl_inode_lower(inode); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 275 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 276 | int err; |
| 277 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 278 | /* Careful in RCU walk mode */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 279 | if (!realinode) { |
| 280 | WARN_ON(!(mask & MAY_NOT_BLOCK)); |
Miklos Szeredi | a999d7e | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 281 | return -ECHILD; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 284 | /* |
| 285 | * Check overlay inode with the creds of task and underlying inode |
| 286 | * with creds of mounter |
| 287 | */ |
| 288 | err = generic_permission(inode, mask); |
| 289 | if (err) |
| 290 | return err; |
| 291 | |
Miklos Szeredi | ec7ba11 | 2018-12-04 11:31:30 +0100 | [diff] [blame] | 292 | old_cred = ovl_override_creds(inode->i_sb); |
| 293 | if (!upperinode && |
| 294 | !special_file(realinode->i_mode) && mask & MAY_WRITE) { |
Vivek Goyal | 754f8cb | 2016-07-01 16:34:29 -0400 | [diff] [blame] | 295 | mask &= ~(MAY_WRITE | MAY_APPEND); |
Miklos Szeredi | ec7ba11 | 2018-12-04 11:31:30 +0100 | [diff] [blame] | 296 | /* Make sure mounter can read file for copy up later */ |
Vivek Goyal | 500cac3 | 2016-07-13 11:00:14 -0400 | [diff] [blame] | 297 | mask |= MAY_READ; |
| 298 | } |
Miklos Szeredi | 9c630eb | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 299 | err = inode_permission(realinode, mask); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 300 | revert_creds(old_cred); |
| 301 | |
| 302 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 305 | static const char *ovl_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 306 | struct inode *inode, |
| 307 | struct delayed_call *done) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 308 | { |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 309 | const struct cred *old_cred; |
| 310 | const char *p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 311 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 312 | if (!dentry) |
| 313 | return ERR_PTR(-ECHILD); |
| 314 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 315 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 316 | p = vfs_get_link(ovl_dentry_real(dentry), done); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 317 | revert_creds(old_cred); |
| 318 | return p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Miklos Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 321 | bool ovl_is_private_xattr(const char *name) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 322 | { |
Andreas Gruenbacher | fe2b759 | 2016-08-22 17:59:22 +0200 | [diff] [blame] | 323 | return strncmp(name, OVL_XATTR_PREFIX, |
| 324 | sizeof(OVL_XATTR_PREFIX) - 1) == 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 327 | int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, |
| 328 | const void *value, size_t size, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 329 | { |
| 330 | int err; |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 331 | struct dentry *upperdentry = ovl_i_dentry_upper(inode); |
| 332 | struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 333 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 334 | |
| 335 | err = ovl_want_write(dentry); |
| 336 | if (err) |
| 337 | goto out; |
| 338 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 339 | if (!value && !upperdentry) { |
| 340 | err = vfs_getxattr(realdentry, name, NULL, 0); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 341 | if (err < 0) |
| 342 | goto out_drop_write; |
| 343 | } |
| 344 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 345 | if (!upperdentry) { |
| 346 | err = ovl_copy_up(dentry); |
| 347 | if (err) |
| 348 | goto out_drop_write; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 349 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 350 | realdentry = ovl_dentry_upper(dentry); |
| 351 | } |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 352 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 353 | old_cred = ovl_override_creds(dentry->d_sb); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 354 | if (value) |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 355 | err = vfs_setxattr(realdentry, name, value, size, flags); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 356 | else { |
| 357 | WARN_ON(flags != XATTR_REPLACE); |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 358 | err = vfs_removexattr(realdentry, name); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 359 | } |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 360 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 361 | |
Miklos Szeredi | d9854c8 | 2018-07-18 15:44:40 +0200 | [diff] [blame] | 362 | /* copy c/mtime */ |
| 363 | ovl_copyattr(d_inode(realdentry), inode); |
| 364 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 365 | out_drop_write: |
| 366 | ovl_drop_write(dentry); |
| 367 | out: |
| 368 | return err; |
| 369 | } |
| 370 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 371 | 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] | 372 | void *value, size_t size) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 373 | { |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 374 | ssize_t res; |
| 375 | const struct cred *old_cred; |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 376 | struct dentry *realdentry = |
| 377 | ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry); |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 378 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 379 | old_cred = ovl_override_creds(dentry->d_sb); |
| 380 | res = vfs_getxattr(realdentry, name, value, size); |
| 381 | revert_creds(old_cred); |
| 382 | return res; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Miklos Szeredi | a082c6f | 2017-05-29 15:15:27 +0200 | [diff] [blame] | 385 | static bool ovl_can_list(const char *s) |
| 386 | { |
| 387 | /* List all non-trusted xatts */ |
| 388 | if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) |
| 389 | return true; |
| 390 | |
| 391 | /* Never list trusted.overlay, list other trusted for superuser only */ |
Mark Salyzyn | 5c2e9f3 | 2019-08-29 11:30:14 -0700 | [diff] [blame] | 392 | return !ovl_is_private_xattr(s) && |
| 393 | ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN); |
Miklos Szeredi | a082c6f | 2017-05-29 15:15:27 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 396 | ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) |
| 397 | { |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 398 | struct dentry *realdentry = ovl_dentry_real(dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 399 | ssize_t res; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 400 | size_t len; |
| 401 | char *s; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 402 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 403 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 404 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 405 | res = vfs_listxattr(realdentry, list, size); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 406 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 407 | if (res <= 0 || size == 0) |
| 408 | return res; |
| 409 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 410 | /* filter out private xattrs */ |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 411 | for (s = list, len = res; len;) { |
| 412 | size_t slen = strnlen(s, len) + 1; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 413 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 414 | /* underlying fs providing us with an broken xattr list? */ |
| 415 | if (WARN_ON(slen > len)) |
| 416 | return -EIO; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 417 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 418 | len -= slen; |
Miklos Szeredi | a082c6f | 2017-05-29 15:15:27 +0200 | [diff] [blame] | 419 | if (!ovl_can_list(s)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 420 | res -= slen; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 421 | memmove(s, s + slen, len); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 422 | } else { |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 423 | s += slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | return res; |
| 428 | } |
| 429 | |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 430 | struct posix_acl *ovl_get_acl(struct inode *inode, int type) |
| 431 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 432 | struct inode *realinode = ovl_inode_real(inode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 433 | const struct cred *old_cred; |
| 434 | struct posix_acl *acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 435 | |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 436 | if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 437 | return NULL; |
| 438 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 439 | old_cred = ovl_override_creds(inode->i_sb); |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 440 | acl = get_acl(realinode, type); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 441 | revert_creds(old_cred); |
| 442 | |
| 443 | return acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 444 | } |
| 445 | |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 446 | int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags) |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 447 | { |
Miklos Szeredi | 8f35cf5 | 2018-04-12 12:04:50 +0200 | [diff] [blame] | 448 | if (flags & S_ATIME) { |
| 449 | struct ovl_fs *ofs = inode->i_sb->s_fs_info; |
| 450 | struct path upperpath = { |
| 451 | .mnt = ofs->upper_mnt, |
| 452 | .dentry = ovl_upperdentry_dereference(OVL_I(inode)), |
| 453 | }; |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 454 | |
Miklos Szeredi | 8f35cf5 | 2018-04-12 12:04:50 +0200 | [diff] [blame] | 455 | if (upperpath.dentry) { |
| 456 | touch_atime(&upperpath); |
| 457 | inode->i_atime = d_inode(upperpath.dentry)->i_atime; |
| 458 | } |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 459 | } |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
Miklos Szeredi | 9e142c4 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 463 | static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 464 | u64 start, u64 len) |
| 465 | { |
| 466 | int err; |
| 467 | struct inode *realinode = ovl_inode_real(inode); |
| 468 | const struct cred *old_cred; |
| 469 | |
| 470 | if (!realinode->i_op->fiemap) |
| 471 | return -EOPNOTSUPP; |
| 472 | |
| 473 | old_cred = ovl_override_creds(inode->i_sb); |
Amir Goldstein | 80d3481 | 2018-08-27 15:55:59 +0300 | [diff] [blame] | 474 | |
| 475 | if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) |
| 476 | filemap_write_and_wait(realinode->i_mapping); |
| 477 | |
Miklos Szeredi | 9e142c4 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 478 | err = realinode->i_op->fiemap(realinode, fieinfo, start, len); |
| 479 | revert_creds(old_cred); |
| 480 | |
| 481 | return err; |
| 482 | } |
| 483 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 484 | static const struct inode_operations ovl_file_inode_operations = { |
| 485 | .setattr = ovl_setattr, |
| 486 | .permission = ovl_permission, |
| 487 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 488 | .listxattr = ovl_listxattr, |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 489 | .get_acl = ovl_get_acl, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 490 | .update_time = ovl_update_time, |
Miklos Szeredi | 9e142c4 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 491 | .fiemap = ovl_fiemap, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | static const struct inode_operations ovl_symlink_inode_operations = { |
| 495 | .setattr = ovl_setattr, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 496 | .get_link = ovl_get_link, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 497 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 498 | .listxattr = ovl_listxattr, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 499 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 500 | }; |
| 501 | |
Miklos Szeredi | 9e142c4 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 502 | static const struct inode_operations ovl_special_inode_operations = { |
| 503 | .setattr = ovl_setattr, |
| 504 | .permission = ovl_permission, |
| 505 | .getattr = ovl_getattr, |
| 506 | .listxattr = ovl_listxattr, |
| 507 | .get_acl = ovl_get_acl, |
| 508 | .update_time = ovl_update_time, |
| 509 | }; |
| 510 | |
Wei Yongjun | 69383c5 | 2018-09-25 14:57:42 +0000 | [diff] [blame] | 511 | static const struct address_space_operations ovl_aops = { |
Amir Goldstein | 5b910bd | 2018-08-27 15:56:00 +0300 | [diff] [blame] | 512 | /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */ |
| 513 | .direct_IO = noop_direct_IO, |
| 514 | }; |
| 515 | |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 516 | /* |
| 517 | * It is possible to stack overlayfs instance on top of another |
| 518 | * overlayfs instance as lower layer. We need to annonate the |
| 519 | * stackable i_mutex locks according to stack level of the super |
| 520 | * block instance. An overlayfs instance can never be in stack |
| 521 | * depth 0 (there is always a real fs below it). An overlayfs |
| 522 | * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth]. |
| 523 | * |
| 524 | * For example, here is a snip from /proc/lockdep_chains after |
| 525 | * dir_iterate of nested overlayfs: |
| 526 | * |
| 527 | * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) |
| 528 | * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) |
| 529 | * [...] &type->i_mutex_dir_key (stack_depth=0) |
Amir Goldstein | b1f9d38 | 2019-12-21 11:42:29 +0200 | [diff] [blame] | 530 | * |
| 531 | * Locking order w.r.t ovl_want_write() is important for nested overlayfs. |
| 532 | * |
| 533 | * This chain is valid: |
| 534 | * - inode->i_rwsem (inode_lock[2]) |
| 535 | * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) |
| 536 | * - OVL_I(inode)->lock (ovl_inode_lock[2]) |
| 537 | * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) |
| 538 | * |
| 539 | * And this chain is valid: |
| 540 | * - inode->i_rwsem (inode_lock[2]) |
| 541 | * - OVL_I(inode)->lock (ovl_inode_lock[2]) |
| 542 | * - lowerinode->i_rwsem (inode_lock[1]) |
| 543 | * - OVL_I(lowerinode)->lock (ovl_inode_lock[1]) |
| 544 | * |
| 545 | * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is |
| 546 | * held, because it is in reverse order of the non-nested case using the same |
| 547 | * upper fs: |
| 548 | * - inode->i_rwsem (inode_lock[1]) |
| 549 | * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0]) |
| 550 | * - OVL_I(inode)->lock (ovl_inode_lock[1]) |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 551 | */ |
| 552 | #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH |
| 553 | |
| 554 | static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) |
| 555 | { |
| 556 | #ifdef CONFIG_LOCKDEP |
| 557 | static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; |
| 558 | static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; |
Amir Goldstein | 4eae06d | 2017-10-27 15:44:08 +0300 | [diff] [blame] | 559 | static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING]; |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 560 | |
| 561 | int depth = inode->i_sb->s_stack_depth - 1; |
| 562 | |
| 563 | if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) |
| 564 | depth = 0; |
| 565 | |
| 566 | if (S_ISDIR(inode->i_mode)) |
| 567 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); |
| 568 | else |
| 569 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); |
Amir Goldstein | 4eae06d | 2017-10-27 15:44:08 +0300 | [diff] [blame] | 570 | |
| 571 | lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]); |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 572 | #endif |
| 573 | } |
| 574 | |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 575 | static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev, |
Amir Goldstein | 12574a9 | 2018-03-16 10:39:37 +0200 | [diff] [blame] | 576 | unsigned long ino, int fsid) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 577 | { |
Amir Goldstein | 12574a9 | 2018-03-16 10:39:37 +0200 | [diff] [blame] | 578 | int xinobits = ovl_xino_bits(inode->i_sb); |
| 579 | |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 580 | /* |
Amir Goldstein | 6dde1e4 | 2019-06-09 19:03:44 +0300 | [diff] [blame] | 581 | * When d_ino is consistent with st_ino (samefs or i_ino has enough |
| 582 | * bits to encode layer), set the same value used for st_ino to i_ino, |
| 583 | * so inode number exposed via /proc/locks and a like will be |
| 584 | * consistent with d_ino and st_ino values. An i_ino value inconsistent |
| 585 | * with d_ino also causes nfsd readdirplus to fail. When called from |
Amir Goldstein | 12574a9 | 2018-03-16 10:39:37 +0200 | [diff] [blame] | 586 | * ovl_new_inode(), ino arg is 0, so i_ino will be updated to real |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 587 | * upper inode i_ino on ovl_inode_init() or ovl_inode_update(). |
| 588 | */ |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame^] | 589 | if (ovl_same_dev(inode->i_sb)) { |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 590 | inode->i_ino = ino; |
Amir Goldstein | 12574a9 | 2018-03-16 10:39:37 +0200 | [diff] [blame] | 591 | if (xinobits && fsid && !(ino >> (64 - xinobits))) |
| 592 | inode->i_ino |= (unsigned long)fsid << (64 - xinobits); |
| 593 | } else { |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 594 | inode->i_ino = get_next_ino(); |
Amir Goldstein | 12574a9 | 2018-03-16 10:39:37 +0200 | [diff] [blame] | 595 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 596 | inode->i_mode = mode; |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 597 | inode->i_flags |= S_NOCMTIME; |
Miklos Szeredi | 2a3a2a3 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 598 | #ifdef CONFIG_FS_POSIX_ACL |
| 599 | inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; |
| 600 | #endif |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 601 | |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 602 | ovl_lockdep_annotate_inode_mutex_key(inode); |
| 603 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 604 | switch (mode & S_IFMT) { |
| 605 | case S_IFREG: |
| 606 | inode->i_op = &ovl_file_inode_operations; |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 607 | inode->i_fop = &ovl_file_operations; |
Amir Goldstein | 5b910bd | 2018-08-27 15:56:00 +0300 | [diff] [blame] | 608 | inode->i_mapping->a_ops = &ovl_aops; |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 609 | break; |
| 610 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 611 | case S_IFDIR: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 612 | inode->i_op = &ovl_dir_inode_operations; |
| 613 | inode->i_fop = &ovl_dir_operations; |
| 614 | break; |
| 615 | |
| 616 | case S_IFLNK: |
| 617 | inode->i_op = &ovl_symlink_inode_operations; |
| 618 | break; |
| 619 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 620 | default: |
Miklos Szeredi | 9e142c4 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 621 | inode->i_op = &ovl_special_inode_operations; |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 622 | init_special_inode(inode, mode, rdev); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 623 | break; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 624 | } |
| 625 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 626 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 627 | /* |
| 628 | * With inodes index enabled, an overlay inode nlink counts the union of upper |
| 629 | * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure |
| 630 | * upper inode, the following nlink modifying operations can happen: |
| 631 | * |
| 632 | * 1. Lower hardlink copy up |
| 633 | * 2. Upper hardlink created, unlinked or renamed over |
| 634 | * 3. Lower hardlink whiteout or renamed over |
| 635 | * |
| 636 | * For the first, copy up case, the union nlink does not change, whether the |
| 637 | * operation succeeds or fails, but the upper inode nlink may change. |
| 638 | * Therefore, before copy up, we store the union nlink value relative to the |
| 639 | * lower inode nlink in the index inode xattr trusted.overlay.nlink. |
| 640 | * |
| 641 | * For the second, upper hardlink case, the union nlink should be incremented |
| 642 | * or decremented IFF the operation succeeds, aligned with nlink change of the |
| 643 | * upper inode. Therefore, before link/unlink/rename, we store the union nlink |
| 644 | * value relative to the upper inode nlink in the index inode. |
| 645 | * |
| 646 | * For the last, lower cover up case, we simplify things by preceding the |
| 647 | * whiteout or cover up with copy up. This makes sure that there is an index |
| 648 | * upper inode where the nlink xattr can be stored before the copied up upper |
| 649 | * entry is unlink. |
| 650 | */ |
| 651 | #define OVL_NLINK_ADD_UPPER (1 << 0) |
| 652 | |
| 653 | /* |
| 654 | * On-disk format for indexed nlink: |
| 655 | * |
| 656 | * nlink relative to the upper inode - "U[+-]NUM" |
| 657 | * nlink relative to the lower inode - "L[+-]NUM" |
| 658 | */ |
| 659 | |
| 660 | static int ovl_set_nlink_common(struct dentry *dentry, |
| 661 | struct dentry *realdentry, const char *format) |
| 662 | { |
| 663 | struct inode *inode = d_inode(dentry); |
| 664 | struct inode *realinode = d_inode(realdentry); |
| 665 | char buf[13]; |
| 666 | int len; |
| 667 | |
| 668 | len = snprintf(buf, sizeof(buf), format, |
| 669 | (int) (inode->i_nlink - realinode->i_nlink)); |
| 670 | |
Miklos Szeredi | 6787341 | 2017-07-27 21:54:05 +0200 | [diff] [blame] | 671 | if (WARN_ON(len >= sizeof(buf))) |
| 672 | return -EIO; |
| 673 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 674 | return ovl_do_setxattr(ovl_dentry_upper(dentry), |
| 675 | OVL_XATTR_NLINK, buf, len, 0); |
| 676 | } |
| 677 | |
| 678 | int ovl_set_nlink_upper(struct dentry *dentry) |
| 679 | { |
| 680 | return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); |
| 681 | } |
| 682 | |
| 683 | int ovl_set_nlink_lower(struct dentry *dentry) |
| 684 | { |
| 685 | return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); |
| 686 | } |
| 687 | |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 688 | unsigned int ovl_get_nlink(struct dentry *lowerdentry, |
| 689 | struct dentry *upperdentry, |
| 690 | unsigned int fallback) |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 691 | { |
| 692 | int nlink_diff; |
| 693 | int nlink; |
| 694 | char buf[13]; |
| 695 | int err; |
| 696 | |
| 697 | if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) |
| 698 | return fallback; |
| 699 | |
| 700 | err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1); |
| 701 | if (err < 0) |
| 702 | goto fail; |
| 703 | |
| 704 | buf[err] = '\0'; |
| 705 | if ((buf[0] != 'L' && buf[0] != 'U') || |
| 706 | (buf[1] != '+' && buf[1] != '-')) |
| 707 | goto fail; |
| 708 | |
| 709 | err = kstrtoint(buf + 1, 10, &nlink_diff); |
| 710 | if (err < 0) |
| 711 | goto fail; |
| 712 | |
| 713 | nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; |
| 714 | nlink += nlink_diff; |
| 715 | |
| 716 | if (nlink <= 0) |
| 717 | goto fail; |
| 718 | |
| 719 | return nlink; |
| 720 | |
| 721 | fail: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 722 | pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n", |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 723 | upperdentry, err); |
| 724 | return fallback; |
| 725 | } |
| 726 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 727 | 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] | 728 | { |
| 729 | struct inode *inode; |
| 730 | |
| 731 | inode = new_inode(sb); |
| 732 | if (inode) |
Amir Goldstein | 12574a9 | 2018-03-16 10:39:37 +0200 | [diff] [blame] | 733 | ovl_fill_inode(inode, mode, rdev, 0, 0); |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 734 | |
| 735 | return inode; |
| 736 | } |
| 737 | |
| 738 | static int ovl_inode_test(struct inode *inode, void *data) |
| 739 | { |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 740 | return inode->i_private == data; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | static int ovl_inode_set(struct inode *inode, void *data) |
| 744 | { |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 745 | inode->i_private = data; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 746 | return 0; |
| 747 | } |
| 748 | |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 749 | static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 750 | struct dentry *upperdentry, bool strict) |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 751 | { |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 752 | /* |
| 753 | * For directories, @strict verify from lookup path performs consistency |
| 754 | * checks, so NULL lower/upper in dentry must match NULL lower/upper in |
| 755 | * inode. Non @strict verify from NFS handle decode path passes NULL for |
| 756 | * 'unknown' lower/upper. |
| 757 | */ |
| 758 | if (S_ISDIR(inode->i_mode) && strict) { |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 759 | /* Real lower dir moved to upper layer under us? */ |
| 760 | if (!lowerdentry && ovl_inode_lower(inode)) |
| 761 | return false; |
| 762 | |
| 763 | /* Lookup of an uncovered redirect origin? */ |
| 764 | if (!upperdentry && ovl_inode_upper(inode)) |
| 765 | return false; |
| 766 | } |
| 767 | |
Amir Goldstein | 939ae4e | 2017-09-11 16:30:15 +0300 | [diff] [blame] | 768 | /* |
| 769 | * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL. |
| 770 | * This happens when finding a copied up overlay inode for a renamed |
| 771 | * or hardlinked overlay dentry and lower dentry cannot be followed |
| 772 | * by origin because lower fs does not support file handles. |
| 773 | */ |
| 774 | if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry)) |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 775 | return false; |
| 776 | |
| 777 | /* |
| 778 | * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. |
| 779 | * This happens when finding a lower alias for a copied up hard link. |
| 780 | */ |
| 781 | if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) |
| 782 | return false; |
| 783 | |
| 784 | return true; |
| 785 | } |
| 786 | |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 787 | struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, |
| 788 | bool is_upper) |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 789 | { |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 790 | struct inode *inode, *key = d_inode(real); |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 791 | |
| 792 | inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); |
| 793 | if (!inode) |
| 794 | return NULL; |
| 795 | |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 796 | if (!ovl_verify_inode(inode, is_upper ? NULL : real, |
| 797 | is_upper ? real : NULL, false)) { |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 798 | iput(inode); |
| 799 | return ERR_PTR(-ESTALE); |
| 800 | } |
| 801 | |
| 802 | return inode; |
| 803 | } |
| 804 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 805 | bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir) |
| 806 | { |
| 807 | struct inode *key = d_inode(dir); |
| 808 | struct inode *trap; |
| 809 | bool res; |
| 810 | |
| 811 | trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key); |
| 812 | if (!trap) |
| 813 | return false; |
| 814 | |
| 815 | res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) && |
| 816 | !ovl_inode_lower(trap); |
| 817 | |
| 818 | iput(trap); |
| 819 | return res; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Create an inode cache entry for layer root dir, that will intentionally |
| 824 | * fail ovl_verify_inode(), so any lookup that will find some layer root |
| 825 | * will fail. |
| 826 | */ |
| 827 | struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir) |
| 828 | { |
| 829 | struct inode *key = d_inode(dir); |
| 830 | struct inode *trap; |
| 831 | |
| 832 | if (!d_is_dir(dir)) |
| 833 | return ERR_PTR(-ENOTDIR); |
| 834 | |
| 835 | trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test, |
| 836 | ovl_inode_set, key); |
| 837 | if (!trap) |
| 838 | return ERR_PTR(-ENOMEM); |
| 839 | |
| 840 | if (!(trap->i_state & I_NEW)) { |
| 841 | /* Conflicting layer roots? */ |
| 842 | iput(trap); |
| 843 | return ERR_PTR(-ELOOP); |
| 844 | } |
| 845 | |
| 846 | trap->i_mode = S_IFDIR; |
| 847 | trap->i_flags = S_DEAD; |
| 848 | unlock_new_inode(trap); |
| 849 | |
| 850 | return trap; |
| 851 | } |
| 852 | |
Amir Goldstein | 764baba | 2018-02-04 15:35:09 +0200 | [diff] [blame] | 853 | /* |
| 854 | * Does overlay inode need to be hashed by lower inode? |
| 855 | */ |
| 856 | static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper, |
| 857 | struct dentry *lower, struct dentry *index) |
| 858 | { |
| 859 | struct ovl_fs *ofs = sb->s_fs_info; |
| 860 | |
| 861 | /* No, if pure upper */ |
| 862 | if (!lower) |
| 863 | return false; |
| 864 | |
| 865 | /* Yes, if already indexed */ |
| 866 | if (index) |
| 867 | return true; |
| 868 | |
| 869 | /* Yes, if won't be copied up */ |
| 870 | if (!ofs->upper_mnt) |
| 871 | return true; |
| 872 | |
| 873 | /* No, if lower hardlink is or will be broken on copy up */ |
| 874 | if ((upper || !ovl_indexdir(sb)) && |
| 875 | !d_is_dir(lower) && d_inode(lower)->i_nlink > 1) |
| 876 | return false; |
| 877 | |
| 878 | /* No, if non-indexed upper with NFS export */ |
| 879 | if (sb->s_export_op && upper) |
| 880 | return false; |
| 881 | |
| 882 | /* Otherwise, hash by lower inode for fsnotify */ |
| 883 | return true; |
| 884 | } |
| 885 | |
Amir Goldstein | 01b39dc | 2018-05-11 11:15:15 +0300 | [diff] [blame] | 886 | static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode, |
| 887 | struct inode *key) |
| 888 | { |
| 889 | return newinode ? inode_insert5(newinode, (unsigned long) key, |
| 890 | ovl_inode_test, ovl_inode_set, key) : |
| 891 | iget5_locked(sb, (unsigned long) key, |
| 892 | ovl_inode_test, ovl_inode_set, key); |
| 893 | } |
| 894 | |
Vivek Goyal | ac6a52e | 2018-05-08 09:27:21 -0400 | [diff] [blame] | 895 | struct inode *ovl_get_inode(struct super_block *sb, |
| 896 | struct ovl_inode_params *oip) |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 897 | { |
Vivek Goyal | ac6a52e | 2018-05-08 09:27:21 -0400 | [diff] [blame] | 898 | struct dentry *upperdentry = oip->upperdentry; |
| 899 | struct ovl_path *lowerpath = oip->lowerpath; |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 900 | struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 901 | struct inode *inode; |
Amir Goldstein | 12574a9 | 2018-03-16 10:39:37 +0200 | [diff] [blame] | 902 | struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL; |
Vivek Goyal | ac6a52e | 2018-05-08 09:27:21 -0400 | [diff] [blame] | 903 | bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, |
| 904 | oip->index); |
| 905 | int fsid = bylower ? oip->lowerpath->layer->fsid : 0; |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 906 | bool is_dir, metacopy = false; |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 907 | unsigned long ino = 0; |
Amir Goldstein | acf3062 | 2019-03-28 17:38:29 +0200 | [diff] [blame] | 908 | int err = oip->newinode ? -EEXIST : -ENOMEM; |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 909 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 910 | if (!realinode) |
| 911 | realinode = d_inode(lowerdentry); |
| 912 | |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 913 | /* |
Amir Goldstein | 764baba | 2018-02-04 15:35:09 +0200 | [diff] [blame] | 914 | * Copy up origin (lower) may exist for non-indexed upper, but we must |
| 915 | * not use lower as hash key if this is a broken hardlink. |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 916 | */ |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 917 | is_dir = S_ISDIR(realinode->i_mode); |
Amir Goldstein | 764baba | 2018-02-04 15:35:09 +0200 | [diff] [blame] | 918 | if (upperdentry || bylower) { |
| 919 | struct inode *key = d_inode(bylower ? lowerdentry : |
| 920 | upperdentry); |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 921 | unsigned int nlink = is_dir ? 1 : realinode->i_nlink; |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 922 | |
Amir Goldstein | 01b39dc | 2018-05-11 11:15:15 +0300 | [diff] [blame] | 923 | inode = ovl_iget5(sb, oip->newinode, key); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 924 | if (!inode) |
Vivek Goyal | 027065b | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 925 | goto out_err; |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 926 | if (!(inode->i_state & I_NEW)) { |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 927 | /* |
| 928 | * Verify that the underlying files stored in the inode |
| 929 | * match those in the dentry. |
| 930 | */ |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 931 | if (!ovl_verify_inode(inode, lowerdentry, upperdentry, |
| 932 | true)) { |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 933 | iput(inode); |
Vivek Goyal | 027065b | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 934 | err = -ESTALE; |
| 935 | goto out_err; |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 936 | } |
| 937 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 938 | dput(upperdentry); |
Vivek Goyal | 9cec54c | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 939 | kfree(oip->redirect); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 940 | goto out; |
| 941 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 942 | |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 943 | /* Recalculate nlink for non-dir due to indexing */ |
| 944 | if (!is_dir) |
| 945 | nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink); |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 946 | set_nlink(inode, nlink); |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 947 | ino = key->i_ino; |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 948 | } else { |
Amir Goldstein | 764baba | 2018-02-04 15:35:09 +0200 | [diff] [blame] | 949 | /* Lower hardlink that will be broken on copy up */ |
Amir Goldstein | 0aceb53 | 2017-12-12 23:43:16 +0200 | [diff] [blame] | 950 | inode = new_inode(sb); |
Vivek Goyal | 027065b | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 951 | if (!inode) { |
| 952 | err = -ENOMEM; |
| 953 | goto out_err; |
| 954 | } |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 955 | } |
Amir Goldstein | 12574a9 | 2018-03-16 10:39:37 +0200 | [diff] [blame] | 956 | ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev, ino, fsid); |
Vivek Goyal | 2664bd0 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 957 | ovl_inode_init(inode, upperdentry, lowerdentry, oip->lowerdata); |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 958 | |
| 959 | if (upperdentry && ovl_is_impuredir(upperdentry)) |
| 960 | ovl_set_flag(OVL_IMPURE, inode); |
| 961 | |
Vivek Goyal | ac6a52e | 2018-05-08 09:27:21 -0400 | [diff] [blame] | 962 | if (oip->index) |
Vivek Goyal | 0471a9c | 2018-03-20 16:35:40 -0400 | [diff] [blame] | 963 | ovl_set_flag(OVL_INDEX, inode); |
| 964 | |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 965 | if (upperdentry) { |
| 966 | err = ovl_check_metacopy_xattr(upperdentry); |
| 967 | if (err < 0) |
| 968 | goto out_err; |
| 969 | metacopy = err; |
| 970 | if (!metacopy) |
| 971 | ovl_set_flag(OVL_UPPERDATA, inode); |
| 972 | } |
| 973 | |
Vivek Goyal | 9cec54c | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 974 | OVL_I(inode)->redirect = oip->redirect; |
| 975 | |
Vivek Goyal | a00c2d5 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 976 | if (bylower) |
| 977 | ovl_set_flag(OVL_CONST_INO, inode); |
| 978 | |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 979 | /* Check for non-merge dir that may have whiteouts */ |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 980 | if (is_dir) { |
Vivek Goyal | ac6a52e | 2018-05-08 09:27:21 -0400 | [diff] [blame] | 981 | if (((upperdentry && lowerdentry) || oip->numlower > 1) || |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 982 | ovl_check_origin_xattr(upperdentry ?: lowerdentry)) { |
| 983 | ovl_set_flag(OVL_WHITEOUTS, inode); |
| 984 | } |
| 985 | } |
| 986 | |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 987 | if (inode->i_state & I_NEW) |
| 988 | unlock_new_inode(inode); |
| 989 | out: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 990 | return inode; |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 991 | |
Vivek Goyal | 027065b | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 992 | out_err: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 993 | pr_warn_ratelimited("failed to get inode (%i)\n", err); |
Vivek Goyal | 027065b | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 994 | inode = ERR_PTR(err); |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 995 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 996 | } |