Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Overlayfs NFS export support. |
| 3 | * |
| 4 | * Amir Goldstein <amir73il@gmail.com> |
| 5 | * |
| 6 | * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published by |
| 10 | * the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/cred.h> |
| 15 | #include <linux/mount.h> |
| 16 | #include <linux/namei.h> |
| 17 | #include <linux/xattr.h> |
| 18 | #include <linux/exportfs.h> |
| 19 | #include <linux/ratelimit.h> |
| 20 | #include "overlayfs.h" |
| 21 | |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 22 | /* |
| 23 | * We only need to encode origin if there is a chance that the same object was |
| 24 | * encoded pre copy up and then we need to stay consistent with the same |
| 25 | * encoding also after copy up. If non-pure upper is not indexed, then it was |
| 26 | * copied up before NFS export was enabled. In that case we don't need to worry |
| 27 | * about staying consistent with pre copy up encoding and we encode an upper |
| 28 | * file handle. Overlay root dentry is a private case of non-indexed upper. |
| 29 | * |
| 30 | * The following table summarizes the different file handle encodings used for |
| 31 | * different overlay object types: |
| 32 | * |
| 33 | * Object type | Encoding |
| 34 | * -------------------------------- |
| 35 | * Pure upper | U |
| 36 | * Non-indexed upper | U |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame] | 37 | * Indexed upper | L (*) |
| 38 | * Non-upper | L (*) |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 39 | * |
| 40 | * U = upper file handle |
| 41 | * L = lower file handle |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame] | 42 | * |
| 43 | * (*) Connecting an overlay dir from real lower dentry is not always |
| 44 | * possible when there are redirects in lower layers. To mitigate this case, |
| 45 | * we copy up the lower dir first and then encode an upper dir file handle. |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 46 | */ |
| 47 | static bool ovl_should_encode_origin(struct dentry *dentry) |
| 48 | { |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame] | 49 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 50 | |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 51 | if (!ovl_dentry_lower(dentry)) |
| 52 | return false; |
| 53 | |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame] | 54 | /* |
| 55 | * Decoding a merge dir, whose origin's parent is under a redirected |
| 56 | * lower dir is not always possible. As a simple aproximation, we do |
| 57 | * not encode lower dir file handles when overlay has multiple lower |
| 58 | * layers and origin is below the topmost lower layer. |
| 59 | * |
| 60 | * TODO: copy up only the parent that is under redirected lower. |
| 61 | */ |
| 62 | if (d_is_dir(dentry) && ofs->upper_mnt && |
| 63 | OVL_E(dentry)->lowerstack[0].layer->idx > 1) |
| 64 | return false; |
| 65 | |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 66 | /* Decoding a non-indexed upper from origin is not implemented */ |
| 67 | if (ovl_dentry_upper(dentry) && |
| 68 | !ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
| 69 | return false; |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame] | 74 | static int ovl_encode_maybe_copy_up(struct dentry *dentry) |
| 75 | { |
| 76 | int err; |
| 77 | |
| 78 | if (ovl_dentry_upper(dentry)) |
| 79 | return 0; |
| 80 | |
| 81 | err = ovl_want_write(dentry); |
| 82 | if (err) |
| 83 | return err; |
| 84 | |
| 85 | err = ovl_copy_up(dentry); |
| 86 | |
| 87 | ovl_drop_write(dentry); |
| 88 | return err; |
| 89 | } |
| 90 | |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 91 | static int ovl_d_to_fh(struct dentry *dentry, char *buf, int buflen) |
| 92 | { |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 93 | struct dentry *origin = ovl_dentry_lower(dentry); |
| 94 | struct ovl_fh *fh = NULL; |
| 95 | int err; |
| 96 | |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame] | 97 | /* |
| 98 | * If we should not encode a lower dir file handle, copy up and encode |
| 99 | * an upper dir file handle. |
| 100 | */ |
| 101 | if (!ovl_should_encode_origin(dentry)) { |
| 102 | err = ovl_encode_maybe_copy_up(dentry); |
| 103 | if (err) |
| 104 | goto fail; |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 105 | |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame] | 106 | origin = NULL; |
| 107 | } |
| 108 | |
Amir Goldstein | 03e1c58 | 2017-12-28 19:35:21 +0200 | [diff] [blame] | 109 | /* Encode an upper or origin file handle */ |
| 110 | fh = ovl_encode_fh(origin ?: ovl_dentry_upper(dentry), !origin); |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 111 | |
| 112 | err = -EOVERFLOW; |
| 113 | if (fh->len > buflen) |
| 114 | goto fail; |
| 115 | |
| 116 | memcpy(buf, (char *)fh, fh->len); |
| 117 | err = fh->len; |
| 118 | |
| 119 | out: |
| 120 | kfree(fh); |
| 121 | return err; |
| 122 | |
| 123 | fail: |
| 124 | pr_warn_ratelimited("overlayfs: failed to encode file handle (%pd2, err=%i, buflen=%d, len=%d, type=%d)\n", |
| 125 | dentry, err, buflen, fh ? (int)fh->len : 0, |
| 126 | fh ? fh->type : 0); |
| 127 | goto out; |
| 128 | } |
| 129 | |
| 130 | static int ovl_dentry_to_fh(struct dentry *dentry, u32 *fid, int *max_len) |
| 131 | { |
| 132 | int res, len = *max_len << 2; |
| 133 | |
| 134 | res = ovl_d_to_fh(dentry, (char *)fid, len); |
| 135 | if (res <= 0) |
| 136 | return FILEID_INVALID; |
| 137 | |
| 138 | len = res; |
| 139 | |
| 140 | /* Round up to dwords */ |
| 141 | *max_len = (len + 3) >> 2; |
| 142 | return OVL_FILEID; |
| 143 | } |
| 144 | |
| 145 | static int ovl_encode_inode_fh(struct inode *inode, u32 *fid, int *max_len, |
| 146 | struct inode *parent) |
| 147 | { |
| 148 | struct dentry *dentry; |
| 149 | int type; |
| 150 | |
| 151 | /* TODO: encode connectable file handles */ |
| 152 | if (parent) |
| 153 | return FILEID_INVALID; |
| 154 | |
| 155 | dentry = d_find_any_alias(inode); |
| 156 | if (WARN_ON(!dentry)) |
| 157 | return FILEID_INVALID; |
| 158 | |
| 159 | type = ovl_dentry_to_fh(dentry, fid, max_len); |
| 160 | |
| 161 | dput(dentry); |
| 162 | return type; |
| 163 | } |
| 164 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 165 | /* |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 166 | * Find or instantiate an overlay dentry from real dentries and index. |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 167 | */ |
| 168 | static struct dentry *ovl_obtain_alias(struct super_block *sb, |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 169 | struct dentry *upper_alias, |
| 170 | struct ovl_path *lowerpath, |
| 171 | struct dentry *index) |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 172 | { |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 173 | struct dentry *lower = lowerpath ? lowerpath->dentry : NULL; |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 174 | struct dentry *upper = upper_alias ?: index; |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 175 | struct dentry *dentry; |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 176 | struct inode *inode; |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 177 | struct ovl_entry *oe; |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 178 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 179 | /* We get overlay directory dentries with ovl_lookup_real() */ |
| 180 | if (d_is_dir(upper ?: lower)) |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 181 | return ERR_PTR(-EIO); |
| 182 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 183 | inode = ovl_get_inode(sb, dget(upper), lower, index, !!lower); |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 184 | if (IS_ERR(inode)) { |
| 185 | dput(upper); |
| 186 | return ERR_CAST(inode); |
| 187 | } |
| 188 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 189 | if (index) |
| 190 | ovl_set_flag(OVL_INDEX, inode); |
| 191 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 192 | dentry = d_find_any_alias(inode); |
| 193 | if (!dentry) { |
| 194 | dentry = d_alloc_anon(inode->i_sb); |
| 195 | if (!dentry) |
| 196 | goto nomem; |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 197 | oe = ovl_alloc_entry(lower ? 1 : 0); |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 198 | if (!oe) |
| 199 | goto nomem; |
| 200 | |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 201 | if (lower) { |
| 202 | oe->lowerstack->dentry = dget(lower); |
| 203 | oe->lowerstack->layer = lowerpath->layer; |
| 204 | } |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 205 | dentry->d_fsdata = oe; |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 206 | if (upper_alias) |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 207 | ovl_dentry_set_upper_alias(dentry); |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | return d_instantiate_anon(dentry, inode); |
| 211 | |
| 212 | nomem: |
| 213 | iput(inode); |
| 214 | dput(dentry); |
| 215 | return ERR_PTR(-ENOMEM); |
| 216 | } |
| 217 | |
Amir Goldstein | 9889251 | 2018-01-17 22:32:44 +0200 | [diff] [blame] | 218 | /* Get the upper or lower dentry in stach whose on layer @idx */ |
| 219 | static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx) |
| 220 | { |
| 221 | struct ovl_entry *oe = dentry->d_fsdata; |
| 222 | int i; |
| 223 | |
| 224 | if (!idx) |
| 225 | return ovl_dentry_upper(dentry); |
| 226 | |
| 227 | for (i = 0; i < oe->numlower; i++) { |
| 228 | if (oe->lowerstack[i].layer->idx == idx) |
| 229 | return oe->lowerstack[i].dentry; |
| 230 | } |
| 231 | |
| 232 | return NULL; |
| 233 | } |
| 234 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 235 | /* |
| 236 | * Lookup a child overlay dentry to get a connected overlay dentry whose real |
| 237 | * dentry is @real. If @real is on upper layer, we lookup a child overlay |
| 238 | * dentry with the same name as the real dentry. Otherwise, we need to consult |
| 239 | * index for lookup. |
| 240 | */ |
| 241 | static struct dentry *ovl_lookup_real_one(struct dentry *connected, |
| 242 | struct dentry *real, |
| 243 | struct ovl_layer *layer) |
| 244 | { |
| 245 | struct inode *dir = d_inode(connected); |
| 246 | struct dentry *this, *parent = NULL; |
| 247 | struct name_snapshot name; |
| 248 | int err; |
| 249 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 250 | /* |
| 251 | * Lookup child overlay dentry by real name. The dir mutex protects us |
| 252 | * from racing with overlay rename. If the overlay dentry that is above |
| 253 | * real has already been moved to a parent that is not under the |
| 254 | * connected overlay dir, we return -ECHILD and restart the lookup of |
| 255 | * connected real path from the top. |
| 256 | */ |
| 257 | inode_lock_nested(dir, I_MUTEX_PARENT); |
| 258 | err = -ECHILD; |
| 259 | parent = dget_parent(real); |
Amir Goldstein | 9889251 | 2018-01-17 22:32:44 +0200 | [diff] [blame] | 260 | if (ovl_dentry_real_at(connected, layer->idx) != parent) |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 261 | goto fail; |
| 262 | |
| 263 | /* |
| 264 | * We also need to take a snapshot of real dentry name to protect us |
| 265 | * from racing with underlying layer rename. In this case, we don't |
| 266 | * care about returning ESTALE, only from dereferencing a free name |
| 267 | * pointer because we hold no lock on the real dentry. |
| 268 | */ |
| 269 | take_dentry_name_snapshot(&name, real); |
| 270 | this = lookup_one_len(name.name, connected, strlen(name.name)); |
| 271 | err = PTR_ERR(this); |
| 272 | if (IS_ERR(this)) { |
| 273 | goto fail; |
| 274 | } else if (!this || !this->d_inode) { |
| 275 | dput(this); |
| 276 | err = -ENOENT; |
| 277 | goto fail; |
Amir Goldstein | 9889251 | 2018-01-17 22:32:44 +0200 | [diff] [blame] | 278 | } else if (ovl_dentry_real_at(this, layer->idx) != real) { |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 279 | dput(this); |
| 280 | err = -ESTALE; |
| 281 | goto fail; |
| 282 | } |
| 283 | |
| 284 | out: |
| 285 | release_dentry_name_snapshot(&name); |
| 286 | dput(parent); |
| 287 | inode_unlock(dir); |
| 288 | return this; |
| 289 | |
| 290 | fail: |
| 291 | pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", |
| 292 | real, layer->idx, connected, err); |
| 293 | this = ERR_PTR(err); |
| 294 | goto out; |
| 295 | } |
| 296 | |
Amir Goldstein | 0617015 | 2018-01-17 14:40:27 +0200 | [diff] [blame^] | 297 | static struct dentry *ovl_lookup_real(struct super_block *sb, |
| 298 | struct dentry *real, |
| 299 | struct ovl_layer *layer); |
| 300 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 301 | /* |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 302 | * Lookup an indexed or hashed overlay dentry by real inode. |
| 303 | */ |
| 304 | static struct dentry *ovl_lookup_real_inode(struct super_block *sb, |
| 305 | struct dentry *real, |
| 306 | struct ovl_layer *layer) |
| 307 | { |
Amir Goldstein | 0617015 | 2018-01-17 14:40:27 +0200 | [diff] [blame^] | 308 | struct ovl_fs *ofs = sb->s_fs_info; |
| 309 | struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt }; |
| 310 | struct dentry *index = NULL; |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 311 | struct dentry *this = NULL; |
| 312 | struct inode *inode; |
| 313 | |
Amir Goldstein | 0617015 | 2018-01-17 14:40:27 +0200 | [diff] [blame^] | 314 | /* |
| 315 | * Decoding upper dir from index is expensive, so first try to lookup |
| 316 | * overlay dentry in inode/dcache. |
| 317 | */ |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 318 | inode = ovl_lookup_inode(sb, real, !layer->idx); |
| 319 | if (IS_ERR(inode)) |
| 320 | return ERR_CAST(inode); |
| 321 | if (inode) { |
| 322 | this = d_find_any_alias(inode); |
| 323 | iput(inode); |
| 324 | } |
| 325 | |
Amir Goldstein | 0617015 | 2018-01-17 14:40:27 +0200 | [diff] [blame^] | 326 | /* |
| 327 | * For decoded lower dir file handle, lookup index by origin to check |
| 328 | * if lower dir was copied up and and/or removed. |
| 329 | */ |
| 330 | if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) { |
| 331 | index = ovl_lookup_index(ofs, NULL, real, false); |
| 332 | if (IS_ERR(index)) |
| 333 | return index; |
| 334 | } |
| 335 | |
| 336 | /* Get connected upper overlay dir from index */ |
| 337 | if (index) { |
| 338 | struct dentry *upper = ovl_index_upper(ofs, index); |
| 339 | |
| 340 | dput(index); |
| 341 | if (IS_ERR_OR_NULL(upper)) |
| 342 | return upper; |
| 343 | |
| 344 | /* |
| 345 | * ovl_lookup_real() in lower layer may call recursively once to |
| 346 | * ovl_lookup_real() in upper layer. The first level call walks |
| 347 | * back lower parents to the topmost indexed parent. The second |
| 348 | * recursive call walks back from indexed upper to the topmost |
| 349 | * connected/hashed upper parent (or up to root). |
| 350 | */ |
| 351 | this = ovl_lookup_real(sb, upper, &upper_layer); |
| 352 | dput(upper); |
| 353 | } |
| 354 | |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 355 | if (!this) |
| 356 | return NULL; |
| 357 | |
| 358 | if (WARN_ON(ovl_dentry_real_at(this, layer->idx) != real)) { |
| 359 | dput(this); |
| 360 | this = ERR_PTR(-EIO); |
| 361 | } |
| 362 | |
| 363 | return this; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Lookup an indexed or hashed overlay dentry, whose real dentry is an |
| 368 | * ancestor of @real. |
| 369 | */ |
| 370 | static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb, |
| 371 | struct dentry *real, |
| 372 | struct ovl_layer *layer) |
| 373 | { |
| 374 | struct dentry *next, *parent = NULL; |
| 375 | struct dentry *ancestor = ERR_PTR(-EIO); |
| 376 | |
| 377 | if (real == layer->mnt->mnt_root) |
| 378 | return dget(sb->s_root); |
| 379 | |
| 380 | /* Find the topmost indexed or hashed ancestor */ |
| 381 | next = dget(real); |
| 382 | for (;;) { |
| 383 | parent = dget_parent(next); |
| 384 | |
| 385 | /* |
| 386 | * Lookup a matching overlay dentry in inode/dentry |
| 387 | * cache or in index by real inode. |
| 388 | */ |
| 389 | ancestor = ovl_lookup_real_inode(sb, next, layer); |
| 390 | if (ancestor) |
| 391 | break; |
| 392 | |
| 393 | if (parent == layer->mnt->mnt_root) { |
| 394 | ancestor = dget(sb->s_root); |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * If @real has been moved out of the layer root directory, |
| 400 | * we will eventully hit the real fs root. This cannot happen |
| 401 | * by legit overlay rename, so we return error in that case. |
| 402 | */ |
| 403 | if (parent == next) { |
| 404 | ancestor = ERR_PTR(-EXDEV); |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | dput(next); |
| 409 | next = parent; |
| 410 | } |
| 411 | |
| 412 | dput(parent); |
| 413 | dput(next); |
| 414 | |
| 415 | return ancestor; |
| 416 | } |
| 417 | |
| 418 | /* |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 419 | * Lookup a connected overlay dentry whose real dentry is @real. |
| 420 | * If @real is on upper layer, we lookup a child overlay dentry with the same |
| 421 | * path the real dentry. Otherwise, we need to consult index for lookup. |
| 422 | */ |
| 423 | static struct dentry *ovl_lookup_real(struct super_block *sb, |
| 424 | struct dentry *real, |
| 425 | struct ovl_layer *layer) |
| 426 | { |
| 427 | struct dentry *connected; |
| 428 | int err = 0; |
| 429 | |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 430 | connected = ovl_lookup_real_ancestor(sb, real, layer); |
| 431 | if (IS_ERR(connected)) |
| 432 | return connected; |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 433 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 434 | while (!err) { |
| 435 | struct dentry *next, *this; |
| 436 | struct dentry *parent = NULL; |
Amir Goldstein | 9889251 | 2018-01-17 22:32:44 +0200 | [diff] [blame] | 437 | struct dentry *real_connected = ovl_dentry_real_at(connected, |
| 438 | layer->idx); |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 439 | |
| 440 | if (real_connected == real) |
| 441 | break; |
| 442 | |
| 443 | /* Find the topmost dentry not yet connected */ |
| 444 | next = dget(real); |
| 445 | for (;;) { |
| 446 | parent = dget_parent(next); |
| 447 | |
| 448 | if (parent == real_connected) |
| 449 | break; |
| 450 | |
| 451 | /* |
| 452 | * If real has been moved out of 'real_connected', |
| 453 | * we will not find 'real_connected' and hit the layer |
| 454 | * root. In that case, we need to restart connecting. |
| 455 | * This game can go on forever in the worst case. We |
| 456 | * may want to consider taking s_vfs_rename_mutex if |
| 457 | * this happens more than once. |
| 458 | */ |
| 459 | if (parent == layer->mnt->mnt_root) { |
| 460 | dput(connected); |
| 461 | connected = dget(sb->s_root); |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * If real file has been moved out of the layer root |
| 467 | * directory, we will eventully hit the real fs root. |
| 468 | * This cannot happen by legit overlay rename, so we |
| 469 | * return error in that case. |
| 470 | */ |
| 471 | if (parent == next) { |
| 472 | err = -EXDEV; |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | dput(next); |
| 477 | next = parent; |
| 478 | } |
| 479 | |
| 480 | if (!err) { |
| 481 | this = ovl_lookup_real_one(connected, next, layer); |
| 482 | if (IS_ERR(this)) |
| 483 | err = PTR_ERR(this); |
| 484 | |
| 485 | /* |
| 486 | * Lookup of child in overlay can fail when racing with |
| 487 | * overlay rename of child away from 'connected' parent. |
| 488 | * In this case, we need to restart the lookup from the |
| 489 | * top, because we cannot trust that 'real_connected' is |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 490 | * still an ancestor of 'real'. There is a good chance |
| 491 | * that the renamed overlay ancestor is now in cache, so |
| 492 | * ovl_lookup_real_ancestor() will find it and we can |
| 493 | * continue to connect exactly from where lookup failed. |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 494 | */ |
| 495 | if (err == -ECHILD) { |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 496 | this = ovl_lookup_real_ancestor(sb, real, |
| 497 | layer); |
| 498 | err = IS_ERR(this) ? PTR_ERR(this) : 0; |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 499 | } |
| 500 | if (!err) { |
| 501 | dput(connected); |
| 502 | connected = this; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | dput(parent); |
| 507 | dput(next); |
| 508 | } |
| 509 | |
| 510 | if (err) |
| 511 | goto fail; |
| 512 | |
| 513 | return connected; |
| 514 | |
| 515 | fail: |
| 516 | pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", |
| 517 | real, layer->idx, connected, err); |
| 518 | dput(connected); |
| 519 | return ERR_PTR(err); |
| 520 | } |
| 521 | |
| 522 | /* |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 523 | * Get an overlay dentry from upper/lower real dentries and index. |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 524 | */ |
| 525 | static struct dentry *ovl_get_dentry(struct super_block *sb, |
| 526 | struct dentry *upper, |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 527 | struct ovl_path *lowerpath, |
| 528 | struct dentry *index) |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 529 | { |
| 530 | struct ovl_fs *ofs = sb->s_fs_info; |
| 531 | struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt }; |
Amir Goldstein | 9889251 | 2018-01-17 22:32:44 +0200 | [diff] [blame] | 532 | struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer; |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 533 | struct dentry *real = upper ?: (index ?: lowerpath->dentry); |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 534 | |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 535 | /* |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 536 | * Obtain a disconnected overlay dentry from a non-dir real dentry |
| 537 | * and index. |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 538 | */ |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 539 | if (!d_is_dir(real)) |
| 540 | return ovl_obtain_alias(sb, upper, lowerpath, index); |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 541 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 542 | /* Removed empty directory? */ |
Amir Goldstein | 9889251 | 2018-01-17 22:32:44 +0200 | [diff] [blame] | 543 | if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real)) |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 544 | return ERR_PTR(-ENOENT); |
| 545 | |
| 546 | /* |
Amir Goldstein | 9889251 | 2018-01-17 22:32:44 +0200 | [diff] [blame] | 547 | * If real dentry is connected and hashed, get a connected overlay |
| 548 | * dentry whose real dentry is @real. |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 549 | */ |
Amir Goldstein | 9889251 | 2018-01-17 22:32:44 +0200 | [diff] [blame] | 550 | return ovl_lookup_real(sb, real, layer); |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 553 | static struct dentry *ovl_upper_fh_to_d(struct super_block *sb, |
| 554 | struct ovl_fh *fh) |
| 555 | { |
| 556 | struct ovl_fs *ofs = sb->s_fs_info; |
| 557 | struct dentry *dentry; |
| 558 | struct dentry *upper; |
| 559 | |
| 560 | if (!ofs->upper_mnt) |
| 561 | return ERR_PTR(-EACCES); |
| 562 | |
| 563 | upper = ovl_decode_fh(fh, ofs->upper_mnt); |
| 564 | if (IS_ERR_OR_NULL(upper)) |
| 565 | return upper; |
| 566 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 567 | dentry = ovl_get_dentry(sb, upper, NULL, NULL); |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 568 | dput(upper); |
| 569 | |
| 570 | return dentry; |
| 571 | } |
| 572 | |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 573 | static struct dentry *ovl_lower_fh_to_d(struct super_block *sb, |
| 574 | struct ovl_fh *fh) |
| 575 | { |
| 576 | struct ovl_fs *ofs = sb->s_fs_info; |
| 577 | struct ovl_path origin = { }; |
| 578 | struct ovl_path *stack = &origin; |
| 579 | struct dentry *dentry = NULL; |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 580 | struct dentry *index = NULL; |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 581 | struct inode *inode = NULL; |
| 582 | bool is_deleted = false; |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 583 | int err; |
| 584 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 585 | /* First lookup indexed upper by fh */ |
| 586 | if (ofs->indexdir) { |
| 587 | index = ovl_get_index_fh(ofs, fh); |
| 588 | err = PTR_ERR(index); |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 589 | if (IS_ERR(index)) { |
| 590 | if (err != -ESTALE) |
| 591 | return ERR_PTR(err); |
| 592 | |
| 593 | /* Found a whiteout index - treat as deleted inode */ |
| 594 | is_deleted = true; |
| 595 | index = NULL; |
| 596 | } |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 597 | } |
| 598 | |
Amir Goldstein | 3b0bfc6 | 2017-12-24 18:42:16 +0200 | [diff] [blame] | 599 | /* Then try to get upper dir by index */ |
| 600 | if (index && d_is_dir(index)) { |
| 601 | struct dentry *upper = ovl_index_upper(ofs, index); |
| 602 | |
| 603 | err = PTR_ERR(upper); |
| 604 | if (IS_ERR_OR_NULL(upper)) |
| 605 | goto out_err; |
| 606 | |
| 607 | dentry = ovl_get_dentry(sb, upper, NULL, NULL); |
| 608 | dput(upper); |
| 609 | goto out; |
| 610 | } |
| 611 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 612 | /* Then lookup origin by fh */ |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 613 | err = ovl_check_origin_fh(ofs, fh, NULL, &stack); |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 614 | if (err) { |
| 615 | goto out_err; |
| 616 | } else if (index) { |
| 617 | err = ovl_verify_origin(index, origin.dentry, false); |
| 618 | if (err) |
| 619 | goto out_err; |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 620 | } else if (is_deleted) { |
| 621 | /* Lookup deleted non-dir by origin inode */ |
| 622 | if (!d_is_dir(origin.dentry)) |
Amir Goldstein | 4b91c30 | 2018-01-18 16:39:13 +0200 | [diff] [blame] | 623 | inode = ovl_lookup_inode(sb, origin.dentry, false); |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 624 | err = -ESTALE; |
| 625 | if (!inode || atomic_read(&inode->i_count) == 1) |
| 626 | goto out_err; |
| 627 | |
| 628 | /* Deleted but still open? */ |
| 629 | index = dget(ovl_i_dentry_upper(inode)); |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 630 | } |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 631 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 632 | dentry = ovl_get_dentry(sb, NULL, &origin, index); |
| 633 | |
| 634 | out: |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 635 | dput(origin.dentry); |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 636 | dput(index); |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame] | 637 | iput(inode); |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 638 | return dentry; |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 639 | |
| 640 | out_err: |
| 641 | dentry = ERR_PTR(err); |
| 642 | goto out; |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 643 | } |
| 644 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 645 | static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 646 | int fh_len, int fh_type) |
| 647 | { |
| 648 | struct dentry *dentry = NULL; |
| 649 | struct ovl_fh *fh = (struct ovl_fh *) fid; |
| 650 | int len = fh_len << 2; |
| 651 | unsigned int flags = 0; |
| 652 | int err; |
| 653 | |
| 654 | err = -EINVAL; |
| 655 | if (fh_type != OVL_FILEID) |
| 656 | goto out_err; |
| 657 | |
| 658 | err = ovl_check_fh_len(fh, len); |
| 659 | if (err) |
| 660 | goto out_err; |
| 661 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 662 | flags = fh->flags; |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 663 | dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ? |
| 664 | ovl_upper_fh_to_d(sb, fh) : |
| 665 | ovl_lower_fh_to_d(sb, fh); |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 666 | err = PTR_ERR(dentry); |
| 667 | if (IS_ERR(dentry) && err != -ESTALE) |
| 668 | goto out_err; |
| 669 | |
| 670 | return dentry; |
| 671 | |
| 672 | out_err: |
| 673 | pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n", |
| 674 | len, fh_type, flags, err); |
| 675 | return ERR_PTR(err); |
| 676 | } |
| 677 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 678 | static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 679 | int fh_len, int fh_type) |
| 680 | { |
| 681 | pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n"); |
| 682 | return ERR_PTR(-EACCES); |
| 683 | } |
| 684 | |
| 685 | static int ovl_get_name(struct dentry *parent, char *name, |
| 686 | struct dentry *child) |
| 687 | { |
| 688 | /* |
| 689 | * ovl_fh_to_dentry() returns connected dir overlay dentries and |
| 690 | * ovl_fh_to_parent() is not implemented, so we should not get here. |
| 691 | */ |
| 692 | WARN_ON_ONCE(1); |
| 693 | return -EIO; |
| 694 | } |
| 695 | |
| 696 | static struct dentry *ovl_get_parent(struct dentry *dentry) |
| 697 | { |
| 698 | /* |
| 699 | * ovl_fh_to_dentry() returns connected dir overlay dentries, so we |
| 700 | * should not get here. |
| 701 | */ |
| 702 | WARN_ON_ONCE(1); |
| 703 | return ERR_PTR(-EIO); |
| 704 | } |
| 705 | |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 706 | const struct export_operations ovl_export_operations = { |
| 707 | .encode_fh = ovl_encode_inode_fh, |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 708 | .fh_to_dentry = ovl_fh_to_dentry, |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 709 | .fh_to_parent = ovl_fh_to_parent, |
| 710 | .get_name = ovl_get_name, |
| 711 | .get_parent = ovl_get_parent, |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 712 | }; |