| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [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 |  | 
|  | 22 | static int ovl_encode_maybe_copy_up(struct dentry *dentry) | 
|  | 23 | { | 
|  | 24 | int err; | 
|  | 25 |  | 
|  | 26 | if (ovl_dentry_upper(dentry)) | 
|  | 27 | return 0; | 
|  | 28 |  | 
|  | 29 | err = ovl_want_write(dentry); | 
|  | 30 | if (!err) { | 
|  | 31 | err = ovl_copy_up(dentry); | 
|  | 32 | ovl_drop_write(dentry); | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | if (err) { | 
|  | 36 | pr_warn_ratelimited("overlayfs: failed to copy up on encode (%pd2, err=%i)\n", | 
|  | 37 | dentry, err); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | return err; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | /* | 
|  | 44 | * Before encoding a non-upper directory file handle from real layer N, we need | 
|  | 45 | * to check if it will be possible to reconnect an overlay dentry from the real | 
|  | 46 | * lower decoded dentry. This is done by following the overlay ancestry up to a | 
|  | 47 | * "layer N connected" ancestor and verifying that all parents along the way are | 
|  | 48 | * "layer N connectable". If an ancestor that is NOT "layer N connectable" is | 
|  | 49 | * found, we need to copy up an ancestor, which is "layer N connectable", thus | 
|  | 50 | * making that ancestor "layer N connected". For example: | 
|  | 51 | * | 
|  | 52 | * layer 1: /a | 
|  | 53 | * layer 2: /a/b/c | 
|  | 54 | * | 
|  | 55 | * The overlay dentry /a is NOT "layer 2 connectable", because if dir /a is | 
|  | 56 | * copied up and renamed, upper dir /a will be indexed by lower dir /a from | 
|  | 57 | * layer 1. The dir /a from layer 2 will never be indexed, so the algorithm (*) | 
|  | 58 | * in ovl_lookup_real_ancestor() will not be able to lookup a connected overlay | 
|  | 59 | * dentry from the connected lower dentry /a/b/c. | 
|  | 60 | * | 
|  | 61 | * To avoid this problem on decode time, we need to copy up an ancestor of | 
|  | 62 | * /a/b/c, which is "layer 2 connectable", on encode time. That ancestor is | 
|  | 63 | * /a/b. After copy up (and index) of /a/b, it will become "layer 2 connected" | 
|  | 64 | * and when the time comes to decode the file handle from lower dentry /a/b/c, | 
|  | 65 | * ovl_lookup_real_ancestor() will find the indexed ancestor /a/b and decoding | 
|  | 66 | * a connected overlay dentry will be accomplished. | 
|  | 67 | * | 
|  | 68 | * (*) the algorithm in ovl_lookup_real_ancestor() can be improved to lookup an | 
|  | 69 | * entry /a in the lower layers above layer N and find the indexed dir /a from | 
|  | 70 | * layer 1. If that improvement is made, then the check for "layer N connected" | 
|  | 71 | * will need to verify there are no redirects in lower layers above N. In the | 
|  | 72 | * example above, /a will be "layer 2 connectable". However, if layer 2 dir /a | 
|  | 73 | * is a target of a layer 1 redirect, then /a will NOT be "layer 2 connectable": | 
|  | 74 | * | 
|  | 75 | * layer 1: /A (redirect = /a) | 
|  | 76 | * layer 2: /a/b/c | 
|  | 77 | */ | 
|  | 78 |  | 
|  | 79 | /* Return the lowest layer for encoding a connectable file handle */ | 
|  | 80 | static int ovl_connectable_layer(struct dentry *dentry) | 
|  | 81 | { | 
|  | 82 | struct ovl_entry *oe = OVL_E(dentry); | 
|  | 83 |  | 
|  | 84 | /* We can get overlay root from root of any layer */ | 
|  | 85 | if (dentry == dentry->d_sb->s_root) | 
|  | 86 | return oe->numlower; | 
|  | 87 |  | 
|  | 88 | /* | 
|  | 89 | * If it's an unindexed merge dir, then it's not connectable with any | 
|  | 90 | * lower layer | 
|  | 91 | */ | 
|  | 92 | if (ovl_dentry_upper(dentry) && | 
|  | 93 | !ovl_test_flag(OVL_INDEX, d_inode(dentry))) | 
|  | 94 | return 0; | 
|  | 95 |  | 
|  | 96 | /* We can get upper/overlay path from indexed/lower dentry */ | 
|  | 97 | return oe->lowerstack[0].layer->idx; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | /* | 
|  | 101 | * @dentry is "connected" if all ancestors up to root or a "connected" ancestor | 
|  | 102 | * have the same uppermost lower layer as the origin's layer. We may need to | 
|  | 103 | * copy up a "connectable" ancestor to make it "connected". A "connected" dentry | 
|  | 104 | * cannot become non "connected", so cache positive result in dentry flags. | 
|  | 105 | * | 
|  | 106 | * Return the connected origin layer or < 0 on error. | 
|  | 107 | */ | 
|  | 108 | static int ovl_connect_layer(struct dentry *dentry) | 
|  | 109 | { | 
|  | 110 | struct dentry *next, *parent = NULL; | 
|  | 111 | int origin_layer; | 
|  | 112 | int err = 0; | 
|  | 113 |  | 
|  | 114 | if (WARN_ON(dentry == dentry->d_sb->s_root) || | 
|  | 115 | WARN_ON(!ovl_dentry_lower(dentry))) | 
|  | 116 | return -EIO; | 
|  | 117 |  | 
|  | 118 | origin_layer = OVL_E(dentry)->lowerstack[0].layer->idx; | 
|  | 119 | if (ovl_dentry_test_flag(OVL_E_CONNECTED, dentry)) | 
|  | 120 | return origin_layer; | 
|  | 121 |  | 
|  | 122 | /* Find the topmost origin layer connectable ancestor of @dentry */ | 
|  | 123 | next = dget(dentry); | 
|  | 124 | for (;;) { | 
|  | 125 | parent = dget_parent(next); | 
|  | 126 | if (WARN_ON(parent == next)) { | 
|  | 127 | err = -EIO; | 
|  | 128 | break; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | /* | 
|  | 132 | * If @parent is not origin layer connectable, then copy up | 
|  | 133 | * @next which is origin layer connectable and we are done. | 
|  | 134 | */ | 
|  | 135 | if (ovl_connectable_layer(parent) < origin_layer) { | 
|  | 136 | err = ovl_encode_maybe_copy_up(next); | 
|  | 137 | break; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | /* If @parent is connected or indexed we are done */ | 
|  | 141 | if (ovl_dentry_test_flag(OVL_E_CONNECTED, parent) || | 
|  | 142 | ovl_test_flag(OVL_INDEX, d_inode(parent))) | 
|  | 143 | break; | 
|  | 144 |  | 
|  | 145 | dput(next); | 
|  | 146 | next = parent; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | dput(parent); | 
|  | 150 | dput(next); | 
|  | 151 |  | 
|  | 152 | if (!err) | 
|  | 153 | ovl_dentry_set_flag(OVL_E_CONNECTED, dentry); | 
|  | 154 |  | 
|  | 155 | return err ?: origin_layer; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | /* | 
|  | 159 | * We only need to encode origin if there is a chance that the same object was | 
|  | 160 | * encoded pre copy up and then we need to stay consistent with the same | 
|  | 161 | * encoding also after copy up. If non-pure upper is not indexed, then it was | 
|  | 162 | * copied up before NFS export was enabled. In that case we don't need to worry | 
|  | 163 | * about staying consistent with pre copy up encoding and we encode an upper | 
|  | 164 | * file handle. Overlay root dentry is a private case of non-indexed upper. | 
|  | 165 | * | 
|  | 166 | * The following table summarizes the different file handle encodings used for | 
|  | 167 | * different overlay object types: | 
|  | 168 | * | 
|  | 169 | *  Object type		| Encoding | 
|  | 170 | * -------------------------------- | 
|  | 171 | *  Pure upper		| U | 
|  | 172 | *  Non-indexed upper	| U | 
|  | 173 | *  Indexed upper	| L (*) | 
|  | 174 | *  Non-upper		| L (*) | 
|  | 175 | * | 
|  | 176 | * U = upper file handle | 
|  | 177 | * L = lower file handle | 
|  | 178 | * | 
|  | 179 | * (*) Connecting an overlay dir from real lower dentry is not always | 
|  | 180 | * possible when there are redirects in lower layers and non-indexed merge dirs. | 
|  | 181 | * To mitigate those case, we may copy up the lower dir ancestor before encode | 
|  | 182 | * a lower dir file handle. | 
|  | 183 | * | 
|  | 184 | * Return 0 for upper file handle, > 0 for lower file handle or < 0 on error. | 
|  | 185 | */ | 
|  | 186 | static int ovl_check_encode_origin(struct dentry *dentry) | 
|  | 187 | { | 
|  | 188 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | 
|  | 189 |  | 
|  | 190 | /* Upper file handle for pure upper */ | 
|  | 191 | if (!ovl_dentry_lower(dentry)) | 
|  | 192 | return 0; | 
|  | 193 |  | 
|  | 194 | /* | 
|  | 195 | * Upper file handle for non-indexed upper. | 
|  | 196 | * | 
|  | 197 | * Root is never indexed, so if there's an upper layer, encode upper for | 
|  | 198 | * root. | 
|  | 199 | */ | 
|  | 200 | if (ovl_dentry_upper(dentry) && | 
|  | 201 | !ovl_test_flag(OVL_INDEX, d_inode(dentry))) | 
|  | 202 | return 0; | 
|  | 203 |  | 
|  | 204 | /* | 
|  | 205 | * Decoding a merge dir, whose origin's ancestor is under a redirected | 
|  | 206 | * lower dir or under a non-indexed upper is not always possible. | 
|  | 207 | * ovl_connect_layer() will try to make origin's layer "connected" by | 
|  | 208 | * copying up a "connectable" ancestor. | 
|  | 209 | */ | 
|  | 210 | if (d_is_dir(dentry) && ofs->upper_mnt) | 
|  | 211 | return ovl_connect_layer(dentry); | 
|  | 212 |  | 
|  | 213 | /* Lower file handle for indexed and non-upper dir/non-dir */ | 
|  | 214 | return 1; | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | static int ovl_d_to_fh(struct dentry *dentry, char *buf, int buflen) | 
|  | 218 | { | 
|  | 219 | struct ovl_fh *fh = NULL; | 
|  | 220 | int err, enc_lower; | 
|  | 221 |  | 
|  | 222 | /* | 
|  | 223 | * Check if we should encode a lower or upper file handle and maybe | 
|  | 224 | * copy up an ancestor to make lower file handle connectable. | 
|  | 225 | */ | 
|  | 226 | err = enc_lower = ovl_check_encode_origin(dentry); | 
|  | 227 | if (enc_lower < 0) | 
|  | 228 | goto fail; | 
|  | 229 |  | 
|  | 230 | /* Encode an upper or lower file handle */ | 
|  | 231 | fh = ovl_encode_real_fh(enc_lower ? ovl_dentry_lower(dentry) : | 
|  | 232 | ovl_dentry_upper(dentry), !enc_lower); | 
|  | 233 | if (IS_ERR(fh)) | 
|  | 234 | return PTR_ERR(fh); | 
|  | 235 |  | 
|  | 236 | err = -EOVERFLOW; | 
|  | 237 | if (fh->len > buflen) | 
|  | 238 | goto fail; | 
|  | 239 |  | 
|  | 240 | memcpy(buf, (char *)fh, fh->len); | 
|  | 241 | err = fh->len; | 
|  | 242 |  | 
|  | 243 | out: | 
|  | 244 | kfree(fh); | 
|  | 245 | return err; | 
|  | 246 |  | 
|  | 247 | fail: | 
|  | 248 | pr_warn_ratelimited("overlayfs: failed to encode file handle (%pd2, err=%i, buflen=%d, len=%d, type=%d)\n", | 
|  | 249 | dentry, err, buflen, fh ? (int)fh->len : 0, | 
|  | 250 | fh ? fh->type : 0); | 
|  | 251 | goto out; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | static int ovl_dentry_to_fh(struct dentry *dentry, u32 *fid, int *max_len) | 
|  | 255 | { | 
|  | 256 | int res, len = *max_len << 2; | 
|  | 257 |  | 
|  | 258 | res = ovl_d_to_fh(dentry, (char *)fid, len); | 
|  | 259 | if (res <= 0) | 
|  | 260 | return FILEID_INVALID; | 
|  | 261 |  | 
|  | 262 | len = res; | 
|  | 263 |  | 
|  | 264 | /* Round up to dwords */ | 
|  | 265 | *max_len = (len + 3) >> 2; | 
|  | 266 | return OVL_FILEID; | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | static int ovl_encode_fh(struct inode *inode, u32 *fid, int *max_len, | 
|  | 270 | struct inode *parent) | 
|  | 271 | { | 
|  | 272 | struct dentry *dentry; | 
|  | 273 | int type; | 
|  | 274 |  | 
|  | 275 | /* TODO: encode connectable file handles */ | 
|  | 276 | if (parent) | 
|  | 277 | return FILEID_INVALID; | 
|  | 278 |  | 
|  | 279 | dentry = d_find_any_alias(inode); | 
|  | 280 | if (WARN_ON(!dentry)) | 
|  | 281 | return FILEID_INVALID; | 
|  | 282 |  | 
|  | 283 | type = ovl_dentry_to_fh(dentry, fid, max_len); | 
|  | 284 |  | 
|  | 285 | dput(dentry); | 
|  | 286 | return type; | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | /* | 
|  | 290 | * Find or instantiate an overlay dentry from real dentries and index. | 
|  | 291 | */ | 
|  | 292 | static struct dentry *ovl_obtain_alias(struct super_block *sb, | 
|  | 293 | struct dentry *upper_alias, | 
|  | 294 | struct ovl_path *lowerpath, | 
|  | 295 | struct dentry *index) | 
|  | 296 | { | 
|  | 297 | struct dentry *lower = lowerpath ? lowerpath->dentry : NULL; | 
|  | 298 | struct dentry *upper = upper_alias ?: index; | 
|  | 299 | struct dentry *dentry; | 
|  | 300 | struct inode *inode; | 
|  | 301 | struct ovl_entry *oe; | 
|  | 302 | struct ovl_inode_params oip = { | 
|  | 303 | .lowerpath = lowerpath, | 
|  | 304 | .index = index, | 
|  | 305 | .numlower = !!lower | 
|  | 306 | }; | 
|  | 307 |  | 
|  | 308 | /* We get overlay directory dentries with ovl_lookup_real() */ | 
|  | 309 | if (d_is_dir(upper ?: lower)) | 
|  | 310 | return ERR_PTR(-EIO); | 
|  | 311 |  | 
|  | 312 | oip.upperdentry = dget(upper); | 
|  | 313 | inode = ovl_get_inode(sb, &oip); | 
|  | 314 | if (IS_ERR(inode)) { | 
|  | 315 | dput(upper); | 
|  | 316 | return ERR_CAST(inode); | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | if (upper) | 
|  | 320 | ovl_set_flag(OVL_UPPERDATA, inode); | 
|  | 321 |  | 
|  | 322 | dentry = d_find_any_alias(inode); | 
|  | 323 | if (!dentry) { | 
|  | 324 | dentry = d_alloc_anon(inode->i_sb); | 
|  | 325 | if (!dentry) | 
|  | 326 | goto nomem; | 
|  | 327 | oe = ovl_alloc_entry(lower ? 1 : 0); | 
|  | 328 | if (!oe) | 
|  | 329 | goto nomem; | 
|  | 330 |  | 
|  | 331 | if (lower) { | 
|  | 332 | oe->lowerstack->dentry = dget(lower); | 
|  | 333 | oe->lowerstack->layer = lowerpath->layer; | 
|  | 334 | } | 
|  | 335 | dentry->d_fsdata = oe; | 
|  | 336 | if (upper_alias) | 
|  | 337 | ovl_dentry_set_upper_alias(dentry); | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | return d_instantiate_anon(dentry, inode); | 
|  | 341 |  | 
|  | 342 | nomem: | 
|  | 343 | iput(inode); | 
|  | 344 | dput(dentry); | 
|  | 345 | return ERR_PTR(-ENOMEM); | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | /* Get the upper or lower dentry in stach whose on layer @idx */ | 
|  | 349 | static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx) | 
|  | 350 | { | 
|  | 351 | struct ovl_entry *oe = dentry->d_fsdata; | 
|  | 352 | int i; | 
|  | 353 |  | 
|  | 354 | if (!idx) | 
|  | 355 | return ovl_dentry_upper(dentry); | 
|  | 356 |  | 
|  | 357 | for (i = 0; i < oe->numlower; i++) { | 
|  | 358 | if (oe->lowerstack[i].layer->idx == idx) | 
|  | 359 | return oe->lowerstack[i].dentry; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | return NULL; | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | /* | 
|  | 366 | * Lookup a child overlay dentry to get a connected overlay dentry whose real | 
|  | 367 | * dentry is @real. If @real is on upper layer, we lookup a child overlay | 
|  | 368 | * dentry with the same name as the real dentry. Otherwise, we need to consult | 
|  | 369 | * index for lookup. | 
|  | 370 | */ | 
|  | 371 | static struct dentry *ovl_lookup_real_one(struct dentry *connected, | 
|  | 372 | struct dentry *real, | 
|  | 373 | struct ovl_layer *layer) | 
|  | 374 | { | 
|  | 375 | struct inode *dir = d_inode(connected); | 
|  | 376 | struct dentry *this, *parent = NULL; | 
|  | 377 | struct name_snapshot name; | 
|  | 378 | int err; | 
|  | 379 |  | 
|  | 380 | /* | 
|  | 381 | * Lookup child overlay dentry by real name. The dir mutex protects us | 
|  | 382 | * from racing with overlay rename. If the overlay dentry that is above | 
|  | 383 | * real has already been moved to a parent that is not under the | 
|  | 384 | * connected overlay dir, we return -ECHILD and restart the lookup of | 
|  | 385 | * connected real path from the top. | 
|  | 386 | */ | 
|  | 387 | inode_lock_nested(dir, I_MUTEX_PARENT); | 
|  | 388 | err = -ECHILD; | 
|  | 389 | parent = dget_parent(real); | 
|  | 390 | if (ovl_dentry_real_at(connected, layer->idx) != parent) | 
|  | 391 | goto fail; | 
|  | 392 |  | 
|  | 393 | /* | 
|  | 394 | * We also need to take a snapshot of real dentry name to protect us | 
|  | 395 | * from racing with underlying layer rename. In this case, we don't | 
|  | 396 | * care about returning ESTALE, only from dereferencing a free name | 
|  | 397 | * pointer because we hold no lock on the real dentry. | 
|  | 398 | */ | 
|  | 399 | take_dentry_name_snapshot(&name, real); | 
|  | 400 | this = lookup_one_len(name.name, connected, strlen(name.name)); | 
|  | 401 | err = PTR_ERR(this); | 
|  | 402 | if (IS_ERR(this)) { | 
|  | 403 | goto fail; | 
|  | 404 | } else if (!this || !this->d_inode) { | 
|  | 405 | dput(this); | 
|  | 406 | err = -ENOENT; | 
|  | 407 | goto fail; | 
|  | 408 | } else if (ovl_dentry_real_at(this, layer->idx) != real) { | 
|  | 409 | dput(this); | 
|  | 410 | err = -ESTALE; | 
|  | 411 | goto fail; | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | out: | 
|  | 415 | release_dentry_name_snapshot(&name); | 
|  | 416 | dput(parent); | 
|  | 417 | inode_unlock(dir); | 
|  | 418 | return this; | 
|  | 419 |  | 
|  | 420 | fail: | 
|  | 421 | pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", | 
|  | 422 | real, layer->idx, connected, err); | 
|  | 423 | this = ERR_PTR(err); | 
|  | 424 | goto out; | 
|  | 425 | } | 
|  | 426 |  | 
|  | 427 | static struct dentry *ovl_lookup_real(struct super_block *sb, | 
|  | 428 | struct dentry *real, | 
|  | 429 | struct ovl_layer *layer); | 
|  | 430 |  | 
|  | 431 | /* | 
|  | 432 | * Lookup an indexed or hashed overlay dentry by real inode. | 
|  | 433 | */ | 
|  | 434 | static struct dentry *ovl_lookup_real_inode(struct super_block *sb, | 
|  | 435 | struct dentry *real, | 
|  | 436 | struct ovl_layer *layer) | 
|  | 437 | { | 
|  | 438 | struct ovl_fs *ofs = sb->s_fs_info; | 
|  | 439 | struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt }; | 
|  | 440 | struct dentry *index = NULL; | 
|  | 441 | struct dentry *this = NULL; | 
|  | 442 | struct inode *inode; | 
|  | 443 |  | 
|  | 444 | /* | 
|  | 445 | * Decoding upper dir from index is expensive, so first try to lookup | 
|  | 446 | * overlay dentry in inode/dcache. | 
|  | 447 | */ | 
|  | 448 | inode = ovl_lookup_inode(sb, real, !layer->idx); | 
|  | 449 | if (IS_ERR(inode)) | 
|  | 450 | return ERR_CAST(inode); | 
|  | 451 | if (inode) { | 
|  | 452 | this = d_find_any_alias(inode); | 
|  | 453 | iput(inode); | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | /* | 
|  | 457 | * For decoded lower dir file handle, lookup index by origin to check | 
|  | 458 | * if lower dir was copied up and and/or removed. | 
|  | 459 | */ | 
|  | 460 | if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) { | 
|  | 461 | index = ovl_lookup_index(ofs, NULL, real, false); | 
|  | 462 | if (IS_ERR(index)) | 
|  | 463 | return index; | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | /* Get connected upper overlay dir from index */ | 
|  | 467 | if (index) { | 
|  | 468 | struct dentry *upper = ovl_index_upper(ofs, index); | 
|  | 469 |  | 
|  | 470 | dput(index); | 
|  | 471 | if (IS_ERR_OR_NULL(upper)) | 
|  | 472 | return upper; | 
|  | 473 |  | 
|  | 474 | /* | 
|  | 475 | * ovl_lookup_real() in lower layer may call recursively once to | 
|  | 476 | * ovl_lookup_real() in upper layer. The first level call walks | 
|  | 477 | * back lower parents to the topmost indexed parent. The second | 
|  | 478 | * recursive call walks back from indexed upper to the topmost | 
|  | 479 | * connected/hashed upper parent (or up to root). | 
|  | 480 | */ | 
|  | 481 | this = ovl_lookup_real(sb, upper, &upper_layer); | 
|  | 482 | dput(upper); | 
|  | 483 | } | 
|  | 484 |  | 
|  | 485 | if (IS_ERR_OR_NULL(this)) | 
|  | 486 | return this; | 
|  | 487 |  | 
|  | 488 | if (WARN_ON(ovl_dentry_real_at(this, layer->idx) != real)) { | 
|  | 489 | dput(this); | 
|  | 490 | this = ERR_PTR(-EIO); | 
|  | 491 | } | 
|  | 492 |  | 
|  | 493 | return this; | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | /* | 
|  | 497 | * Lookup an indexed or hashed overlay dentry, whose real dentry is an | 
|  | 498 | * ancestor of @real. | 
|  | 499 | */ | 
|  | 500 | static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb, | 
|  | 501 | struct dentry *real, | 
|  | 502 | struct ovl_layer *layer) | 
|  | 503 | { | 
|  | 504 | struct dentry *next, *parent = NULL; | 
|  | 505 | struct dentry *ancestor = ERR_PTR(-EIO); | 
|  | 506 |  | 
|  | 507 | if (real == layer->mnt->mnt_root) | 
|  | 508 | return dget(sb->s_root); | 
|  | 509 |  | 
|  | 510 | /* Find the topmost indexed or hashed ancestor */ | 
|  | 511 | next = dget(real); | 
|  | 512 | for (;;) { | 
|  | 513 | parent = dget_parent(next); | 
|  | 514 |  | 
|  | 515 | /* | 
|  | 516 | * Lookup a matching overlay dentry in inode/dentry | 
|  | 517 | * cache or in index by real inode. | 
|  | 518 | */ | 
|  | 519 | ancestor = ovl_lookup_real_inode(sb, next, layer); | 
|  | 520 | if (ancestor) | 
|  | 521 | break; | 
|  | 522 |  | 
|  | 523 | if (parent == layer->mnt->mnt_root) { | 
|  | 524 | ancestor = dget(sb->s_root); | 
|  | 525 | break; | 
|  | 526 | } | 
|  | 527 |  | 
|  | 528 | /* | 
|  | 529 | * If @real has been moved out of the layer root directory, | 
|  | 530 | * we will eventully hit the real fs root. This cannot happen | 
|  | 531 | * by legit overlay rename, so we return error in that case. | 
|  | 532 | */ | 
|  | 533 | if (parent == next) { | 
|  | 534 | ancestor = ERR_PTR(-EXDEV); | 
|  | 535 | break; | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | dput(next); | 
|  | 539 | next = parent; | 
|  | 540 | } | 
|  | 541 |  | 
|  | 542 | dput(parent); | 
|  | 543 | dput(next); | 
|  | 544 |  | 
|  | 545 | return ancestor; | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | /* | 
|  | 549 | * Lookup a connected overlay dentry whose real dentry is @real. | 
|  | 550 | * If @real is on upper layer, we lookup a child overlay dentry with the same | 
|  | 551 | * path the real dentry. Otherwise, we need to consult index for lookup. | 
|  | 552 | */ | 
|  | 553 | static struct dentry *ovl_lookup_real(struct super_block *sb, | 
|  | 554 | struct dentry *real, | 
|  | 555 | struct ovl_layer *layer) | 
|  | 556 | { | 
|  | 557 | struct dentry *connected; | 
|  | 558 | int err = 0; | 
|  | 559 |  | 
|  | 560 | connected = ovl_lookup_real_ancestor(sb, real, layer); | 
|  | 561 | if (IS_ERR(connected)) | 
|  | 562 | return connected; | 
|  | 563 |  | 
|  | 564 | while (!err) { | 
|  | 565 | struct dentry *next, *this; | 
|  | 566 | struct dentry *parent = NULL; | 
|  | 567 | struct dentry *real_connected = ovl_dentry_real_at(connected, | 
|  | 568 | layer->idx); | 
|  | 569 |  | 
|  | 570 | if (real_connected == real) | 
|  | 571 | break; | 
|  | 572 |  | 
|  | 573 | /* Find the topmost dentry not yet connected */ | 
|  | 574 | next = dget(real); | 
|  | 575 | for (;;) { | 
|  | 576 | parent = dget_parent(next); | 
|  | 577 |  | 
|  | 578 | if (parent == real_connected) | 
|  | 579 | break; | 
|  | 580 |  | 
|  | 581 | /* | 
|  | 582 | * If real has been moved out of 'real_connected', | 
|  | 583 | * we will not find 'real_connected' and hit the layer | 
|  | 584 | * root. In that case, we need to restart connecting. | 
|  | 585 | * This game can go on forever in the worst case. We | 
|  | 586 | * may want to consider taking s_vfs_rename_mutex if | 
|  | 587 | * this happens more than once. | 
|  | 588 | */ | 
|  | 589 | if (parent == layer->mnt->mnt_root) { | 
|  | 590 | dput(connected); | 
|  | 591 | connected = dget(sb->s_root); | 
|  | 592 | break; | 
|  | 593 | } | 
|  | 594 |  | 
|  | 595 | /* | 
|  | 596 | * If real file has been moved out of the layer root | 
|  | 597 | * directory, we will eventully hit the real fs root. | 
|  | 598 | * This cannot happen by legit overlay rename, so we | 
|  | 599 | * return error in that case. | 
|  | 600 | */ | 
|  | 601 | if (parent == next) { | 
|  | 602 | err = -EXDEV; | 
|  | 603 | break; | 
|  | 604 | } | 
|  | 605 |  | 
|  | 606 | dput(next); | 
|  | 607 | next = parent; | 
|  | 608 | } | 
|  | 609 |  | 
|  | 610 | if (!err) { | 
|  | 611 | this = ovl_lookup_real_one(connected, next, layer); | 
|  | 612 | if (IS_ERR(this)) | 
|  | 613 | err = PTR_ERR(this); | 
|  | 614 |  | 
|  | 615 | /* | 
|  | 616 | * Lookup of child in overlay can fail when racing with | 
|  | 617 | * overlay rename of child away from 'connected' parent. | 
|  | 618 | * In this case, we need to restart the lookup from the | 
|  | 619 | * top, because we cannot trust that 'real_connected' is | 
|  | 620 | * still an ancestor of 'real'. There is a good chance | 
|  | 621 | * that the renamed overlay ancestor is now in cache, so | 
|  | 622 | * ovl_lookup_real_ancestor() will find it and we can | 
|  | 623 | * continue to connect exactly from where lookup failed. | 
|  | 624 | */ | 
|  | 625 | if (err == -ECHILD) { | 
|  | 626 | this = ovl_lookup_real_ancestor(sb, real, | 
|  | 627 | layer); | 
|  | 628 | err = PTR_ERR_OR_ZERO(this); | 
|  | 629 | } | 
|  | 630 | if (!err) { | 
|  | 631 | dput(connected); | 
|  | 632 | connected = this; | 
|  | 633 | } | 
|  | 634 | } | 
|  | 635 |  | 
|  | 636 | dput(parent); | 
|  | 637 | dput(next); | 
|  | 638 | } | 
|  | 639 |  | 
|  | 640 | if (err) | 
|  | 641 | goto fail; | 
|  | 642 |  | 
|  | 643 | return connected; | 
|  | 644 |  | 
|  | 645 | fail: | 
|  | 646 | pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", | 
|  | 647 | real, layer->idx, connected, err); | 
|  | 648 | dput(connected); | 
|  | 649 | return ERR_PTR(err); | 
|  | 650 | } | 
|  | 651 |  | 
|  | 652 | /* | 
|  | 653 | * Get an overlay dentry from upper/lower real dentries and index. | 
|  | 654 | */ | 
|  | 655 | static struct dentry *ovl_get_dentry(struct super_block *sb, | 
|  | 656 | struct dentry *upper, | 
|  | 657 | struct ovl_path *lowerpath, | 
|  | 658 | struct dentry *index) | 
|  | 659 | { | 
|  | 660 | struct ovl_fs *ofs = sb->s_fs_info; | 
|  | 661 | struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt }; | 
|  | 662 | struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer; | 
|  | 663 | struct dentry *real = upper ?: (index ?: lowerpath->dentry); | 
|  | 664 |  | 
|  | 665 | /* | 
|  | 666 | * Obtain a disconnected overlay dentry from a non-dir real dentry | 
|  | 667 | * and index. | 
|  | 668 | */ | 
|  | 669 | if (!d_is_dir(real)) | 
|  | 670 | return ovl_obtain_alias(sb, upper, lowerpath, index); | 
|  | 671 |  | 
|  | 672 | /* Removed empty directory? */ | 
|  | 673 | if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real)) | 
|  | 674 | return ERR_PTR(-ENOENT); | 
|  | 675 |  | 
|  | 676 | /* | 
|  | 677 | * If real dentry is connected and hashed, get a connected overlay | 
|  | 678 | * dentry whose real dentry is @real. | 
|  | 679 | */ | 
|  | 680 | return ovl_lookup_real(sb, real, layer); | 
|  | 681 | } | 
|  | 682 |  | 
|  | 683 | static struct dentry *ovl_upper_fh_to_d(struct super_block *sb, | 
|  | 684 | struct ovl_fh *fh) | 
|  | 685 | { | 
|  | 686 | struct ovl_fs *ofs = sb->s_fs_info; | 
|  | 687 | struct dentry *dentry; | 
|  | 688 | struct dentry *upper; | 
|  | 689 |  | 
|  | 690 | if (!ofs->upper_mnt) | 
|  | 691 | return ERR_PTR(-EACCES); | 
|  | 692 |  | 
|  | 693 | upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true); | 
|  | 694 | if (IS_ERR_OR_NULL(upper)) | 
|  | 695 | return upper; | 
|  | 696 |  | 
|  | 697 | dentry = ovl_get_dentry(sb, upper, NULL, NULL); | 
|  | 698 | dput(upper); | 
|  | 699 |  | 
|  | 700 | return dentry; | 
|  | 701 | } | 
|  | 702 |  | 
|  | 703 | static struct dentry *ovl_lower_fh_to_d(struct super_block *sb, | 
|  | 704 | struct ovl_fh *fh) | 
|  | 705 | { | 
|  | 706 | struct ovl_fs *ofs = sb->s_fs_info; | 
|  | 707 | struct ovl_path origin = { }; | 
|  | 708 | struct ovl_path *stack = &origin; | 
|  | 709 | struct dentry *dentry = NULL; | 
|  | 710 | struct dentry *index = NULL; | 
|  | 711 | struct inode *inode; | 
|  | 712 | int err; | 
|  | 713 |  | 
|  | 714 | /* First lookup overlay inode in inode cache by origin fh */ | 
|  | 715 | err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack); | 
|  | 716 | if (err) | 
|  | 717 | return ERR_PTR(err); | 
|  | 718 |  | 
|  | 719 | if (!d_is_dir(origin.dentry) || | 
|  | 720 | !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) { | 
|  | 721 | inode = ovl_lookup_inode(sb, origin.dentry, false); | 
|  | 722 | err = PTR_ERR(inode); | 
|  | 723 | if (IS_ERR(inode)) | 
|  | 724 | goto out_err; | 
|  | 725 | if (inode) { | 
|  | 726 | dentry = d_find_any_alias(inode); | 
|  | 727 | iput(inode); | 
|  | 728 | if (dentry) | 
|  | 729 | goto out; | 
|  | 730 | } | 
|  | 731 | } | 
|  | 732 |  | 
|  | 733 | /* Then lookup indexed upper/whiteout by origin fh */ | 
|  | 734 | if (ofs->indexdir) { | 
|  | 735 | index = ovl_get_index_fh(ofs, fh); | 
|  | 736 | err = PTR_ERR(index); | 
|  | 737 | if (IS_ERR(index)) { | 
|  | 738 | index = NULL; | 
|  | 739 | goto out_err; | 
|  | 740 | } | 
|  | 741 | } | 
|  | 742 |  | 
|  | 743 | /* Then try to get a connected upper dir by index */ | 
|  | 744 | if (index && d_is_dir(index)) { | 
|  | 745 | struct dentry *upper = ovl_index_upper(ofs, index); | 
|  | 746 |  | 
|  | 747 | err = PTR_ERR(upper); | 
|  | 748 | if (IS_ERR_OR_NULL(upper)) | 
|  | 749 | goto out_err; | 
|  | 750 |  | 
|  | 751 | dentry = ovl_get_dentry(sb, upper, NULL, NULL); | 
|  | 752 | dput(upper); | 
|  | 753 | goto out; | 
|  | 754 | } | 
|  | 755 |  | 
|  | 756 | /* Find origin.dentry again with ovl_acceptable() layer check */ | 
|  | 757 | if (d_is_dir(origin.dentry)) { | 
|  | 758 | dput(origin.dentry); | 
|  | 759 | origin.dentry = NULL; | 
|  | 760 | err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack); | 
|  | 761 | if (err) | 
|  | 762 | goto out_err; | 
|  | 763 | } | 
|  | 764 | if (index) { | 
|  | 765 | err = ovl_verify_origin(index, origin.dentry, false); | 
|  | 766 | if (err) | 
|  | 767 | goto out_err; | 
|  | 768 | } | 
|  | 769 |  | 
|  | 770 | /* Get a connected non-upper dir or disconnected non-dir */ | 
|  | 771 | dentry = ovl_get_dentry(sb, NULL, &origin, index); | 
|  | 772 |  | 
|  | 773 | out: | 
|  | 774 | dput(origin.dentry); | 
|  | 775 | dput(index); | 
|  | 776 | return dentry; | 
|  | 777 |  | 
|  | 778 | out_err: | 
|  | 779 | dentry = ERR_PTR(err); | 
|  | 780 | goto out; | 
|  | 781 | } | 
|  | 782 |  | 
|  | 783 | static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid, | 
|  | 784 | int fh_len, int fh_type) | 
|  | 785 | { | 
|  | 786 | struct dentry *dentry = NULL; | 
|  | 787 | struct ovl_fh *fh = (struct ovl_fh *) fid; | 
|  | 788 | int len = fh_len << 2; | 
|  | 789 | unsigned int flags = 0; | 
|  | 790 | int err; | 
|  | 791 |  | 
|  | 792 | err = -EINVAL; | 
|  | 793 | if (fh_type != OVL_FILEID) | 
|  | 794 | goto out_err; | 
|  | 795 |  | 
|  | 796 | err = ovl_check_fh_len(fh, len); | 
|  | 797 | if (err) | 
|  | 798 | goto out_err; | 
|  | 799 |  | 
|  | 800 | flags = fh->flags; | 
|  | 801 | dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ? | 
|  | 802 | ovl_upper_fh_to_d(sb, fh) : | 
|  | 803 | ovl_lower_fh_to_d(sb, fh); | 
|  | 804 | err = PTR_ERR(dentry); | 
|  | 805 | if (IS_ERR(dentry) && err != -ESTALE) | 
|  | 806 | goto out_err; | 
|  | 807 |  | 
|  | 808 | return dentry; | 
|  | 809 |  | 
|  | 810 | out_err: | 
|  | 811 | pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n", | 
|  | 812 | len, fh_type, flags, err); | 
|  | 813 | return ERR_PTR(err); | 
|  | 814 | } | 
|  | 815 |  | 
|  | 816 | static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid, | 
|  | 817 | int fh_len, int fh_type) | 
|  | 818 | { | 
|  | 819 | pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n"); | 
|  | 820 | return ERR_PTR(-EACCES); | 
|  | 821 | } | 
|  | 822 |  | 
|  | 823 | static int ovl_get_name(struct dentry *parent, char *name, | 
|  | 824 | struct dentry *child) | 
|  | 825 | { | 
|  | 826 | /* | 
|  | 827 | * ovl_fh_to_dentry() returns connected dir overlay dentries and | 
|  | 828 | * ovl_fh_to_parent() is not implemented, so we should not get here. | 
|  | 829 | */ | 
|  | 830 | WARN_ON_ONCE(1); | 
|  | 831 | return -EIO; | 
|  | 832 | } | 
|  | 833 |  | 
|  | 834 | static struct dentry *ovl_get_parent(struct dentry *dentry) | 
|  | 835 | { | 
|  | 836 | /* | 
|  | 837 | * ovl_fh_to_dentry() returns connected dir overlay dentries, so we | 
|  | 838 | * should not get here. | 
|  | 839 | */ | 
|  | 840 | WARN_ON_ONCE(1); | 
|  | 841 | return ERR_PTR(-EIO); | 
|  | 842 | } | 
|  | 843 |  | 
|  | 844 | const struct export_operations ovl_export_operations = { | 
|  | 845 | .encode_fh	= ovl_encode_fh, | 
|  | 846 | .fh_to_dentry	= ovl_fh_to_dentry, | 
|  | 847 | .fh_to_parent	= ovl_fh_to_parent, | 
|  | 848 | .get_name	= ovl_get_name, | 
|  | 849 | .get_parent	= ovl_get_parent, | 
|  | 850 | }; |