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