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