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 | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 218 | /* |
| 219 | * Lookup a child overlay dentry to get a connected overlay dentry whose real |
| 220 | * dentry is @real. If @real is on upper layer, we lookup a child overlay |
| 221 | * dentry with the same name as the real dentry. Otherwise, we need to consult |
| 222 | * index for lookup. |
| 223 | */ |
| 224 | static struct dentry *ovl_lookup_real_one(struct dentry *connected, |
| 225 | struct dentry *real, |
| 226 | struct ovl_layer *layer) |
| 227 | { |
| 228 | struct inode *dir = d_inode(connected); |
| 229 | struct dentry *this, *parent = NULL; |
| 230 | struct name_snapshot name; |
| 231 | int err; |
| 232 | |
| 233 | /* TODO: lookup by lower real dentry */ |
| 234 | if (layer->idx) |
| 235 | return ERR_PTR(-EACCES); |
| 236 | |
| 237 | /* |
| 238 | * Lookup child overlay dentry by real name. The dir mutex protects us |
| 239 | * from racing with overlay rename. If the overlay dentry that is above |
| 240 | * real has already been moved to a parent that is not under the |
| 241 | * connected overlay dir, we return -ECHILD and restart the lookup of |
| 242 | * connected real path from the top. |
| 243 | */ |
| 244 | inode_lock_nested(dir, I_MUTEX_PARENT); |
| 245 | err = -ECHILD; |
| 246 | parent = dget_parent(real); |
| 247 | if (ovl_dentry_upper(connected) != parent) |
| 248 | goto fail; |
| 249 | |
| 250 | /* |
| 251 | * We also need to take a snapshot of real dentry name to protect us |
| 252 | * from racing with underlying layer rename. In this case, we don't |
| 253 | * care about returning ESTALE, only from dereferencing a free name |
| 254 | * pointer because we hold no lock on the real dentry. |
| 255 | */ |
| 256 | take_dentry_name_snapshot(&name, real); |
| 257 | this = lookup_one_len(name.name, connected, strlen(name.name)); |
| 258 | err = PTR_ERR(this); |
| 259 | if (IS_ERR(this)) { |
| 260 | goto fail; |
| 261 | } else if (!this || !this->d_inode) { |
| 262 | dput(this); |
| 263 | err = -ENOENT; |
| 264 | goto fail; |
| 265 | } else if (ovl_dentry_upper(this) != real) { |
| 266 | dput(this); |
| 267 | err = -ESTALE; |
| 268 | goto fail; |
| 269 | } |
| 270 | |
| 271 | out: |
| 272 | release_dentry_name_snapshot(&name); |
| 273 | dput(parent); |
| 274 | inode_unlock(dir); |
| 275 | return this; |
| 276 | |
| 277 | fail: |
| 278 | pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", |
| 279 | real, layer->idx, connected, err); |
| 280 | this = ERR_PTR(err); |
| 281 | goto out; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Lookup a connected overlay dentry whose real dentry is @real. |
| 286 | * If @real is on upper layer, we lookup a child overlay dentry with the same |
| 287 | * path the real dentry. Otherwise, we need to consult index for lookup. |
| 288 | */ |
| 289 | static struct dentry *ovl_lookup_real(struct super_block *sb, |
| 290 | struct dentry *real, |
| 291 | struct ovl_layer *layer) |
| 292 | { |
| 293 | struct dentry *connected; |
| 294 | int err = 0; |
| 295 | |
| 296 | /* TODO: use index when looking up by lower real dentry */ |
| 297 | if (layer->idx) |
| 298 | return ERR_PTR(-EACCES); |
| 299 | |
| 300 | connected = dget(sb->s_root); |
| 301 | while (!err) { |
| 302 | struct dentry *next, *this; |
| 303 | struct dentry *parent = NULL; |
| 304 | struct dentry *real_connected = ovl_dentry_upper(connected); |
| 305 | |
| 306 | if (real_connected == real) |
| 307 | break; |
| 308 | |
| 309 | /* Find the topmost dentry not yet connected */ |
| 310 | next = dget(real); |
| 311 | for (;;) { |
| 312 | parent = dget_parent(next); |
| 313 | |
| 314 | if (parent == real_connected) |
| 315 | break; |
| 316 | |
| 317 | /* |
| 318 | * If real has been moved out of 'real_connected', |
| 319 | * we will not find 'real_connected' and hit the layer |
| 320 | * root. In that case, we need to restart connecting. |
| 321 | * This game can go on forever in the worst case. We |
| 322 | * may want to consider taking s_vfs_rename_mutex if |
| 323 | * this happens more than once. |
| 324 | */ |
| 325 | if (parent == layer->mnt->mnt_root) { |
| 326 | dput(connected); |
| 327 | connected = dget(sb->s_root); |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * If real file has been moved out of the layer root |
| 333 | * directory, we will eventully hit the real fs root. |
| 334 | * This cannot happen by legit overlay rename, so we |
| 335 | * return error in that case. |
| 336 | */ |
| 337 | if (parent == next) { |
| 338 | err = -EXDEV; |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | dput(next); |
| 343 | next = parent; |
| 344 | } |
| 345 | |
| 346 | if (!err) { |
| 347 | this = ovl_lookup_real_one(connected, next, layer); |
| 348 | if (IS_ERR(this)) |
| 349 | err = PTR_ERR(this); |
| 350 | |
| 351 | /* |
| 352 | * Lookup of child in overlay can fail when racing with |
| 353 | * overlay rename of child away from 'connected' parent. |
| 354 | * In this case, we need to restart the lookup from the |
| 355 | * top, because we cannot trust that 'real_connected' is |
| 356 | * still an ancestor of 'real'. |
| 357 | */ |
| 358 | if (err == -ECHILD) { |
| 359 | this = dget(sb->s_root); |
| 360 | err = 0; |
| 361 | } |
| 362 | if (!err) { |
| 363 | dput(connected); |
| 364 | connected = this; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | dput(parent); |
| 369 | dput(next); |
| 370 | } |
| 371 | |
| 372 | if (err) |
| 373 | goto fail; |
| 374 | |
| 375 | return connected; |
| 376 | |
| 377 | fail: |
| 378 | pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", |
| 379 | real, layer->idx, connected, err); |
| 380 | dput(connected); |
| 381 | return ERR_PTR(err); |
| 382 | } |
| 383 | |
| 384 | /* |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 385 | * Get an overlay dentry from upper/lower real dentries and index. |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 386 | */ |
| 387 | static struct dentry *ovl_get_dentry(struct super_block *sb, |
| 388 | struct dentry *upper, |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 389 | struct ovl_path *lowerpath, |
| 390 | struct dentry *index) |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 391 | { |
| 392 | struct ovl_fs *ofs = sb->s_fs_info; |
| 393 | struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt }; |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 394 | struct dentry *real = upper ?: (index ?: lowerpath->dentry); |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 395 | |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 396 | /* |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 397 | * Obtain a disconnected overlay dentry from a non-dir real dentry |
| 398 | * and index. |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 399 | */ |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 400 | if (!d_is_dir(real)) |
| 401 | return ovl_obtain_alias(sb, upper, lowerpath, index); |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 402 | |
| 403 | /* TODO: lookup connected dir from real lower dir */ |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 404 | if (!upper) |
| 405 | return ERR_PTR(-EACCES); |
| 406 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 407 | /* Removed empty directory? */ |
| 408 | if ((upper->d_flags & DCACHE_DISCONNECTED) || d_unhashed(upper)) |
| 409 | return ERR_PTR(-ENOENT); |
| 410 | |
| 411 | /* |
| 412 | * If real upper dentry is connected and hashed, get a connected |
| 413 | * overlay dentry with the same path as the real upper dentry. |
| 414 | */ |
| 415 | return ovl_lookup_real(sb, upper, &upper_layer); |
| 416 | } |
| 417 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 418 | static struct dentry *ovl_upper_fh_to_d(struct super_block *sb, |
| 419 | struct ovl_fh *fh) |
| 420 | { |
| 421 | struct ovl_fs *ofs = sb->s_fs_info; |
| 422 | struct dentry *dentry; |
| 423 | struct dentry *upper; |
| 424 | |
| 425 | if (!ofs->upper_mnt) |
| 426 | return ERR_PTR(-EACCES); |
| 427 | |
| 428 | upper = ovl_decode_fh(fh, ofs->upper_mnt); |
| 429 | if (IS_ERR_OR_NULL(upper)) |
| 430 | return upper; |
| 431 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 432 | dentry = ovl_get_dentry(sb, upper, NULL, NULL); |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 433 | dput(upper); |
| 434 | |
| 435 | return dentry; |
| 436 | } |
| 437 | |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 438 | static struct dentry *ovl_lower_fh_to_d(struct super_block *sb, |
| 439 | struct ovl_fh *fh) |
| 440 | { |
| 441 | struct ovl_fs *ofs = sb->s_fs_info; |
| 442 | struct ovl_path origin = { }; |
| 443 | struct ovl_path *stack = &origin; |
| 444 | struct dentry *dentry = NULL; |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 445 | struct dentry *index = NULL; |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame^] | 446 | struct inode *inode = NULL; |
| 447 | bool is_deleted = false; |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 448 | int err; |
| 449 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 450 | /* First lookup indexed upper by fh */ |
| 451 | if (ofs->indexdir) { |
| 452 | index = ovl_get_index_fh(ofs, fh); |
| 453 | err = PTR_ERR(index); |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame^] | 454 | if (IS_ERR(index)) { |
| 455 | if (err != -ESTALE) |
| 456 | return ERR_PTR(err); |
| 457 | |
| 458 | /* Found a whiteout index - treat as deleted inode */ |
| 459 | is_deleted = true; |
| 460 | index = NULL; |
| 461 | } |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* Then lookup origin by fh */ |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 465 | err = ovl_check_origin_fh(ofs, fh, NULL, &stack); |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 466 | if (err) { |
| 467 | goto out_err; |
| 468 | } else if (index) { |
| 469 | err = ovl_verify_origin(index, origin.dentry, false); |
| 470 | if (err) |
| 471 | goto out_err; |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame^] | 472 | } else if (is_deleted) { |
| 473 | /* Lookup deleted non-dir by origin inode */ |
| 474 | if (!d_is_dir(origin.dentry)) |
| 475 | inode = ovl_lookup_inode(sb, origin.dentry); |
| 476 | err = -ESTALE; |
| 477 | if (!inode || atomic_read(&inode->i_count) == 1) |
| 478 | goto out_err; |
| 479 | |
| 480 | /* Deleted but still open? */ |
| 481 | index = dget(ovl_i_dentry_upper(inode)); |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 482 | } |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 483 | |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 484 | dentry = ovl_get_dentry(sb, NULL, &origin, index); |
| 485 | |
| 486 | out: |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 487 | dput(origin.dentry); |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 488 | dput(index); |
Amir Goldstein | 9436a1a | 2017-12-24 18:28:04 +0200 | [diff] [blame^] | 489 | iput(inode); |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 490 | return dentry; |
Amir Goldstein | f71bd9c | 2018-01-19 21:36:20 +0200 | [diff] [blame] | 491 | |
| 492 | out_err: |
| 493 | dentry = ERR_PTR(err); |
| 494 | goto out; |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 497 | static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 498 | int fh_len, int fh_type) |
| 499 | { |
| 500 | struct dentry *dentry = NULL; |
| 501 | struct ovl_fh *fh = (struct ovl_fh *) fid; |
| 502 | int len = fh_len << 2; |
| 503 | unsigned int flags = 0; |
| 504 | int err; |
| 505 | |
| 506 | err = -EINVAL; |
| 507 | if (fh_type != OVL_FILEID) |
| 508 | goto out_err; |
| 509 | |
| 510 | err = ovl_check_fh_len(fh, len); |
| 511 | if (err) |
| 512 | goto out_err; |
| 513 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 514 | flags = fh->flags; |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 515 | dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ? |
| 516 | ovl_upper_fh_to_d(sb, fh) : |
| 517 | ovl_lower_fh_to_d(sb, fh); |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 518 | err = PTR_ERR(dentry); |
| 519 | if (IS_ERR(dentry) && err != -ESTALE) |
| 520 | goto out_err; |
| 521 | |
| 522 | return dentry; |
| 523 | |
| 524 | out_err: |
| 525 | pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n", |
| 526 | len, fh_type, flags, err); |
| 527 | return ERR_PTR(err); |
| 528 | } |
| 529 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 530 | static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 531 | int fh_len, int fh_type) |
| 532 | { |
| 533 | pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n"); |
| 534 | return ERR_PTR(-EACCES); |
| 535 | } |
| 536 | |
| 537 | static int ovl_get_name(struct dentry *parent, char *name, |
| 538 | struct dentry *child) |
| 539 | { |
| 540 | /* |
| 541 | * ovl_fh_to_dentry() returns connected dir overlay dentries and |
| 542 | * ovl_fh_to_parent() is not implemented, so we should not get here. |
| 543 | */ |
| 544 | WARN_ON_ONCE(1); |
| 545 | return -EIO; |
| 546 | } |
| 547 | |
| 548 | static struct dentry *ovl_get_parent(struct dentry *dentry) |
| 549 | { |
| 550 | /* |
| 551 | * ovl_fh_to_dentry() returns connected dir overlay dentries, so we |
| 552 | * should not get here. |
| 553 | */ |
| 554 | WARN_ON_ONCE(1); |
| 555 | return ERR_PTR(-EIO); |
| 556 | } |
| 557 | |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 558 | const struct export_operations ovl_export_operations = { |
| 559 | .encode_fh = ovl_encode_inode_fh, |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 560 | .fh_to_dentry = ovl_fh_to_dentry, |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 561 | .fh_to_parent = ovl_fh_to_parent, |
| 562 | .get_name = ovl_get_name, |
| 563 | .get_parent = ovl_get_parent, |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 564 | }; |