18ed5eec9SAmir Goldstein /* 28ed5eec9SAmir Goldstein * Overlayfs NFS export support. 38ed5eec9SAmir Goldstein * 48ed5eec9SAmir Goldstein * Amir Goldstein <amir73il@gmail.com> 58ed5eec9SAmir Goldstein * 68ed5eec9SAmir Goldstein * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved. 78ed5eec9SAmir Goldstein * 88ed5eec9SAmir Goldstein * This program is free software; you can redistribute it and/or modify it 98ed5eec9SAmir Goldstein * under the terms of the GNU General Public License version 2 as published by 108ed5eec9SAmir Goldstein * the Free Software Foundation. 118ed5eec9SAmir Goldstein */ 128ed5eec9SAmir Goldstein 138ed5eec9SAmir Goldstein #include <linux/fs.h> 148ed5eec9SAmir Goldstein #include <linux/cred.h> 158ed5eec9SAmir Goldstein #include <linux/mount.h> 168ed5eec9SAmir Goldstein #include <linux/namei.h> 178ed5eec9SAmir Goldstein #include <linux/xattr.h> 188ed5eec9SAmir Goldstein #include <linux/exportfs.h> 198ed5eec9SAmir Goldstein #include <linux/ratelimit.h> 208ed5eec9SAmir Goldstein #include "overlayfs.h" 218ed5eec9SAmir Goldstein 222ca3c148SAmir Goldstein static int ovl_encode_maybe_copy_up(struct dentry *dentry) 232ca3c148SAmir Goldstein { 242ca3c148SAmir Goldstein int err; 252ca3c148SAmir Goldstein 262ca3c148SAmir Goldstein if (ovl_dentry_upper(dentry)) 272ca3c148SAmir Goldstein return 0; 282ca3c148SAmir Goldstein 292ca3c148SAmir Goldstein err = ovl_want_write(dentry); 302ca3c148SAmir Goldstein if (!err) { 312ca3c148SAmir Goldstein err = ovl_copy_up(dentry); 322ca3c148SAmir Goldstein ovl_drop_write(dentry); 332ca3c148SAmir Goldstein } 342ca3c148SAmir Goldstein 352ca3c148SAmir Goldstein if (err) { 362ca3c148SAmir Goldstein pr_warn_ratelimited("overlayfs: failed to copy up on encode (%pd2, err=%i)\n", 372ca3c148SAmir Goldstein dentry, err); 382ca3c148SAmir Goldstein } 392ca3c148SAmir Goldstein 402ca3c148SAmir Goldstein return err; 412ca3c148SAmir Goldstein } 422ca3c148SAmir Goldstein 432ca3c148SAmir Goldstein /* 442ca3c148SAmir Goldstein * Before encoding a non-upper directory file handle from real layer N, we need 452ca3c148SAmir Goldstein * to check if it will be possible to reconnect an overlay dentry from the real 462ca3c148SAmir Goldstein * lower decoded dentry. This is done by following the overlay ancestry up to a 472ca3c148SAmir Goldstein * "layer N connected" ancestor and verifying that all parents along the way are 482ca3c148SAmir Goldstein * "layer N connectable". If an ancestor that is NOT "layer N connectable" is 492ca3c148SAmir Goldstein * found, we need to copy up an ancestor, which is "layer N connectable", thus 502ca3c148SAmir Goldstein * making that ancestor "layer N connected". For example: 512ca3c148SAmir Goldstein * 522ca3c148SAmir Goldstein * layer 1: /a 532ca3c148SAmir Goldstein * layer 2: /a/b/c 542ca3c148SAmir Goldstein * 552ca3c148SAmir Goldstein * The overlay dentry /a is NOT "layer 2 connectable", because if dir /a is 562ca3c148SAmir Goldstein * copied up and renamed, upper dir /a will be indexed by lower dir /a from 572ca3c148SAmir Goldstein * layer 1. The dir /a from layer 2 will never be indexed, so the algorithm (*) 582ca3c148SAmir Goldstein * in ovl_lookup_real_ancestor() will not be able to lookup a connected overlay 592ca3c148SAmir Goldstein * dentry from the connected lower dentry /a/b/c. 602ca3c148SAmir Goldstein * 612ca3c148SAmir Goldstein * To avoid this problem on decode time, we need to copy up an ancestor of 622ca3c148SAmir Goldstein * /a/b/c, which is "layer 2 connectable", on encode time. That ancestor is 632ca3c148SAmir Goldstein * /a/b. After copy up (and index) of /a/b, it will become "layer 2 connected" 642ca3c148SAmir Goldstein * and when the time comes to decode the file handle from lower dentry /a/b/c, 652ca3c148SAmir Goldstein * ovl_lookup_real_ancestor() will find the indexed ancestor /a/b and decoding 662ca3c148SAmir Goldstein * a connected overlay dentry will be accomplished. 672ca3c148SAmir Goldstein * 682ca3c148SAmir Goldstein * (*) the algorithm in ovl_lookup_real_ancestor() can be improved to lookup an 692ca3c148SAmir Goldstein * entry /a in the lower layers above layer N and find the indexed dir /a from 702ca3c148SAmir Goldstein * layer 1. If that improvement is made, then the check for "layer N connected" 712ca3c148SAmir Goldstein * will need to verify there are no redirects in lower layers above N. In the 722ca3c148SAmir Goldstein * example above, /a will be "layer 2 connectable". However, if layer 2 dir /a 732ca3c148SAmir Goldstein * is a target of a layer 1 redirect, then /a will NOT be "layer 2 connectable": 742ca3c148SAmir Goldstein * 752ca3c148SAmir Goldstein * layer 1: /A (redirect = /a) 762ca3c148SAmir Goldstein * layer 2: /a/b/c 772ca3c148SAmir Goldstein */ 782ca3c148SAmir Goldstein 792ca3c148SAmir Goldstein /* Return the lowest layer for encoding a connectable file handle */ 802ca3c148SAmir Goldstein static int ovl_connectable_layer(struct dentry *dentry) 812ca3c148SAmir Goldstein { 822ca3c148SAmir Goldstein struct ovl_entry *oe = OVL_E(dentry); 832ca3c148SAmir Goldstein 842ca3c148SAmir Goldstein /* We can get overlay root from root of any layer */ 852ca3c148SAmir Goldstein if (dentry == dentry->d_sb->s_root) 862ca3c148SAmir Goldstein return oe->numlower; 872ca3c148SAmir Goldstein 882ca3c148SAmir Goldstein /* 892ca3c148SAmir Goldstein * If it's an unindexed merge dir, then it's not connectable with any 902ca3c148SAmir Goldstein * lower layer 912ca3c148SAmir Goldstein */ 922ca3c148SAmir Goldstein if (ovl_dentry_upper(dentry) && 932ca3c148SAmir Goldstein !ovl_test_flag(OVL_INDEX, d_inode(dentry))) 942ca3c148SAmir Goldstein return 0; 952ca3c148SAmir Goldstein 962ca3c148SAmir Goldstein /* We can get upper/overlay path from indexed/lower dentry */ 972ca3c148SAmir Goldstein return oe->lowerstack[0].layer->idx; 982ca3c148SAmir Goldstein } 992ca3c148SAmir Goldstein 1002ca3c148SAmir Goldstein /* 1012ca3c148SAmir Goldstein * @dentry is "connected" if all ancestors up to root or a "connected" ancestor 1022ca3c148SAmir Goldstein * have the same uppermost lower layer as the origin's layer. We may need to 1032ca3c148SAmir Goldstein * copy up a "connectable" ancestor to make it "connected". A "connected" dentry 1042ca3c148SAmir Goldstein * cannot become non "connected", so cache positive result in dentry flags. 1052ca3c148SAmir Goldstein * 1062ca3c148SAmir Goldstein * Return the connected origin layer or < 0 on error. 1072ca3c148SAmir Goldstein */ 1082ca3c148SAmir Goldstein static int ovl_connect_layer(struct dentry *dentry) 1092ca3c148SAmir Goldstein { 1102ca3c148SAmir Goldstein struct dentry *next, *parent = NULL; 1112ca3c148SAmir Goldstein int origin_layer; 1122ca3c148SAmir Goldstein int err = 0; 1132ca3c148SAmir Goldstein 1142ca3c148SAmir Goldstein if (WARN_ON(dentry == dentry->d_sb->s_root) || 1152ca3c148SAmir Goldstein WARN_ON(!ovl_dentry_lower(dentry))) 1162ca3c148SAmir Goldstein return -EIO; 1172ca3c148SAmir Goldstein 1182ca3c148SAmir Goldstein origin_layer = OVL_E(dentry)->lowerstack[0].layer->idx; 1192ca3c148SAmir Goldstein if (ovl_dentry_test_flag(OVL_E_CONNECTED, dentry)) 1202ca3c148SAmir Goldstein return origin_layer; 1212ca3c148SAmir Goldstein 1222ca3c148SAmir Goldstein /* Find the topmost origin layer connectable ancestor of @dentry */ 1232ca3c148SAmir Goldstein next = dget(dentry); 1242ca3c148SAmir Goldstein for (;;) { 1252ca3c148SAmir Goldstein parent = dget_parent(next); 1262ca3c148SAmir Goldstein if (WARN_ON(parent == next)) { 1272ca3c148SAmir Goldstein err = -EIO; 1282ca3c148SAmir Goldstein break; 1292ca3c148SAmir Goldstein } 1302ca3c148SAmir Goldstein 1312ca3c148SAmir Goldstein /* 1322ca3c148SAmir Goldstein * If @parent is not origin layer connectable, then copy up 1332ca3c148SAmir Goldstein * @next which is origin layer connectable and we are done. 1342ca3c148SAmir Goldstein */ 1352ca3c148SAmir Goldstein if (ovl_connectable_layer(parent) < origin_layer) { 1362ca3c148SAmir Goldstein err = ovl_encode_maybe_copy_up(next); 1372ca3c148SAmir Goldstein break; 1382ca3c148SAmir Goldstein } 1392ca3c148SAmir Goldstein 1402ca3c148SAmir Goldstein /* If @parent is connected or indexed we are done */ 1412ca3c148SAmir Goldstein if (ovl_dentry_test_flag(OVL_E_CONNECTED, parent) || 1422ca3c148SAmir Goldstein ovl_test_flag(OVL_INDEX, d_inode(parent))) 1432ca3c148SAmir Goldstein break; 1442ca3c148SAmir Goldstein 1452ca3c148SAmir Goldstein dput(next); 1462ca3c148SAmir Goldstein next = parent; 1472ca3c148SAmir Goldstein } 1482ca3c148SAmir Goldstein 1492ca3c148SAmir Goldstein dput(parent); 1502ca3c148SAmir Goldstein dput(next); 1512ca3c148SAmir Goldstein 1522ca3c148SAmir Goldstein if (!err) 1532ca3c148SAmir Goldstein ovl_dentry_set_flag(OVL_E_CONNECTED, dentry); 1542ca3c148SAmir Goldstein 1552ca3c148SAmir Goldstein return err ?: origin_layer; 1562ca3c148SAmir Goldstein } 1572ca3c148SAmir Goldstein 158b305e844SAmir Goldstein /* 159b305e844SAmir Goldstein * We only need to encode origin if there is a chance that the same object was 160b305e844SAmir Goldstein * encoded pre copy up and then we need to stay consistent with the same 161b305e844SAmir Goldstein * encoding also after copy up. If non-pure upper is not indexed, then it was 162b305e844SAmir Goldstein * copied up before NFS export was enabled. In that case we don't need to worry 163b305e844SAmir Goldstein * about staying consistent with pre copy up encoding and we encode an upper 164b305e844SAmir Goldstein * file handle. Overlay root dentry is a private case of non-indexed upper. 165b305e844SAmir Goldstein * 166b305e844SAmir Goldstein * The following table summarizes the different file handle encodings used for 167b305e844SAmir Goldstein * different overlay object types: 168b305e844SAmir Goldstein * 169b305e844SAmir Goldstein * Object type | Encoding 170b305e844SAmir Goldstein * -------------------------------- 171b305e844SAmir Goldstein * Pure upper | U 172b305e844SAmir Goldstein * Non-indexed upper | U 17305e1f118SAmir Goldstein * Indexed upper | L (*) 17405e1f118SAmir Goldstein * Non-upper | L (*) 175b305e844SAmir Goldstein * 176b305e844SAmir Goldstein * U = upper file handle 177b305e844SAmir Goldstein * L = lower file handle 17805e1f118SAmir Goldstein * 17905e1f118SAmir Goldstein * (*) Connecting an overlay dir from real lower dentry is not always 1802ca3c148SAmir Goldstein * possible when there are redirects in lower layers and non-indexed merge dirs. 1812ca3c148SAmir Goldstein * To mitigate those case, we may copy up the lower dir ancestor before encode 1822ca3c148SAmir Goldstein * a lower dir file handle. 1832ca3c148SAmir Goldstein * 1842ca3c148SAmir Goldstein * Return 0 for upper file handle, > 0 for lower file handle or < 0 on error. 185b305e844SAmir Goldstein */ 1862ca3c148SAmir Goldstein static int ovl_check_encode_origin(struct dentry *dentry) 187b305e844SAmir Goldstein { 18805e1f118SAmir Goldstein struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 18905e1f118SAmir Goldstein 1902ca3c148SAmir Goldstein /* Upper file handle for pure upper */ 191b305e844SAmir Goldstein if (!ovl_dentry_lower(dentry)) 19205e1f118SAmir Goldstein return 0; 19305e1f118SAmir Goldstein 1942ca3c148SAmir Goldstein /* 1952ca3c148SAmir Goldstein * Upper file handle for non-indexed upper. 1962ca3c148SAmir Goldstein * 1972ca3c148SAmir Goldstein * Root is never indexed, so if there's an upper layer, encode upper for 1982ca3c148SAmir Goldstein * root. 1992ca3c148SAmir Goldstein */ 2002ca3c148SAmir Goldstein if (ovl_dentry_upper(dentry) && 2012ca3c148SAmir Goldstein !ovl_test_flag(OVL_INDEX, d_inode(dentry))) 2022ca3c148SAmir Goldstein return 0; 20305e1f118SAmir Goldstein 2042ca3c148SAmir Goldstein /* 2052ca3c148SAmir Goldstein * Decoding a merge dir, whose origin's ancestor is under a redirected 2062ca3c148SAmir Goldstein * lower dir or under a non-indexed upper is not always possible. 2072ca3c148SAmir Goldstein * ovl_connect_layer() will try to make origin's layer "connected" by 2082ca3c148SAmir Goldstein * copying up a "connectable" ancestor. 2092ca3c148SAmir Goldstein */ 2102ca3c148SAmir Goldstein if (d_is_dir(dentry) && ofs->upper_mnt) 2112ca3c148SAmir Goldstein return ovl_connect_layer(dentry); 21205e1f118SAmir Goldstein 2132ca3c148SAmir Goldstein /* Lower file handle for indexed and non-upper dir/non-dir */ 2142ca3c148SAmir Goldstein return 1; 21505e1f118SAmir Goldstein } 21605e1f118SAmir Goldstein 2178ed5eec9SAmir Goldstein static int ovl_d_to_fh(struct dentry *dentry, char *buf, int buflen) 2188ed5eec9SAmir Goldstein { 2198ed5eec9SAmir Goldstein struct ovl_fh *fh = NULL; 2202ca3c148SAmir Goldstein int err, enc_lower; 2218ed5eec9SAmir Goldstein 22205e1f118SAmir Goldstein /* 2232ca3c148SAmir Goldstein * Check if we should encode a lower or upper file handle and maybe 2242ca3c148SAmir Goldstein * copy up an ancestor to make lower file handle connectable. 22505e1f118SAmir Goldstein */ 2262ca3c148SAmir Goldstein err = enc_lower = ovl_check_encode_origin(dentry); 2272ca3c148SAmir Goldstein if (enc_lower < 0) 22805e1f118SAmir Goldstein goto fail; 2298ed5eec9SAmir Goldstein 2302ca3c148SAmir Goldstein /* Encode an upper or lower file handle */ 2312ca3c148SAmir Goldstein fh = ovl_encode_fh(enc_lower ? ovl_dentry_lower(dentry) : 2322ca3c148SAmir Goldstein ovl_dentry_upper(dentry), !enc_lower); 2339b6faee0SAmir Goldstein err = PTR_ERR(fh); 2349b6faee0SAmir Goldstein if (IS_ERR(fh)) 2359b6faee0SAmir Goldstein goto fail; 2368ed5eec9SAmir Goldstein 2378ed5eec9SAmir Goldstein err = -EOVERFLOW; 2388ed5eec9SAmir Goldstein if (fh->len > buflen) 2398ed5eec9SAmir Goldstein goto fail; 2408ed5eec9SAmir Goldstein 2418ed5eec9SAmir Goldstein memcpy(buf, (char *)fh, fh->len); 2428ed5eec9SAmir Goldstein err = fh->len; 2438ed5eec9SAmir Goldstein 2448ed5eec9SAmir Goldstein out: 2458ed5eec9SAmir Goldstein kfree(fh); 2468ed5eec9SAmir Goldstein return err; 2478ed5eec9SAmir Goldstein 2488ed5eec9SAmir Goldstein fail: 2498ed5eec9SAmir Goldstein pr_warn_ratelimited("overlayfs: failed to encode file handle (%pd2, err=%i, buflen=%d, len=%d, type=%d)\n", 2508ed5eec9SAmir Goldstein dentry, err, buflen, fh ? (int)fh->len : 0, 2518ed5eec9SAmir Goldstein fh ? fh->type : 0); 2528ed5eec9SAmir Goldstein goto out; 2538ed5eec9SAmir Goldstein } 2548ed5eec9SAmir Goldstein 2558ed5eec9SAmir Goldstein static int ovl_dentry_to_fh(struct dentry *dentry, u32 *fid, int *max_len) 2568ed5eec9SAmir Goldstein { 2578ed5eec9SAmir Goldstein int res, len = *max_len << 2; 2588ed5eec9SAmir Goldstein 2598ed5eec9SAmir Goldstein res = ovl_d_to_fh(dentry, (char *)fid, len); 2608ed5eec9SAmir Goldstein if (res <= 0) 2618ed5eec9SAmir Goldstein return FILEID_INVALID; 2628ed5eec9SAmir Goldstein 2638ed5eec9SAmir Goldstein len = res; 2648ed5eec9SAmir Goldstein 2658ed5eec9SAmir Goldstein /* Round up to dwords */ 2668ed5eec9SAmir Goldstein *max_len = (len + 3) >> 2; 2678ed5eec9SAmir Goldstein return OVL_FILEID; 2688ed5eec9SAmir Goldstein } 2698ed5eec9SAmir Goldstein 2708ed5eec9SAmir Goldstein static int ovl_encode_inode_fh(struct inode *inode, u32 *fid, int *max_len, 2718ed5eec9SAmir Goldstein struct inode *parent) 2728ed5eec9SAmir Goldstein { 2738ed5eec9SAmir Goldstein struct dentry *dentry; 2748ed5eec9SAmir Goldstein int type; 2758ed5eec9SAmir Goldstein 2768ed5eec9SAmir Goldstein /* TODO: encode connectable file handles */ 2778ed5eec9SAmir Goldstein if (parent) 2788ed5eec9SAmir Goldstein return FILEID_INVALID; 2798ed5eec9SAmir Goldstein 2808ed5eec9SAmir Goldstein dentry = d_find_any_alias(inode); 2818ed5eec9SAmir Goldstein if (WARN_ON(!dentry)) 2828ed5eec9SAmir Goldstein return FILEID_INVALID; 2838ed5eec9SAmir Goldstein 2848ed5eec9SAmir Goldstein type = ovl_dentry_to_fh(dentry, fid, max_len); 2858ed5eec9SAmir Goldstein 2868ed5eec9SAmir Goldstein dput(dentry); 2878ed5eec9SAmir Goldstein return type; 2888ed5eec9SAmir Goldstein } 2898ed5eec9SAmir Goldstein 2908556a420SAmir Goldstein /* 291f71bd9cfSAmir Goldstein * Find or instantiate an overlay dentry from real dentries and index. 2928556a420SAmir Goldstein */ 2938556a420SAmir Goldstein static struct dentry *ovl_obtain_alias(struct super_block *sb, 294f71bd9cfSAmir Goldstein struct dentry *upper_alias, 295f71bd9cfSAmir Goldstein struct ovl_path *lowerpath, 296f71bd9cfSAmir Goldstein struct dentry *index) 2978556a420SAmir Goldstein { 298f941866fSAmir Goldstein struct dentry *lower = lowerpath ? lowerpath->dentry : NULL; 299f71bd9cfSAmir Goldstein struct dentry *upper = upper_alias ?: index; 3008556a420SAmir Goldstein struct dentry *dentry; 301f941866fSAmir Goldstein struct inode *inode; 3028556a420SAmir Goldstein struct ovl_entry *oe; 3038556a420SAmir Goldstein 304f71bd9cfSAmir Goldstein /* We get overlay directory dentries with ovl_lookup_real() */ 305f71bd9cfSAmir Goldstein if (d_is_dir(upper ?: lower)) 3068556a420SAmir Goldstein return ERR_PTR(-EIO); 3078556a420SAmir Goldstein 308f71bd9cfSAmir Goldstein inode = ovl_get_inode(sb, dget(upper), lower, index, !!lower); 3098556a420SAmir Goldstein if (IS_ERR(inode)) { 3108556a420SAmir Goldstein dput(upper); 3118556a420SAmir Goldstein return ERR_CAST(inode); 3128556a420SAmir Goldstein } 3138556a420SAmir Goldstein 314f71bd9cfSAmir Goldstein if (index) 315f71bd9cfSAmir Goldstein ovl_set_flag(OVL_INDEX, inode); 316f71bd9cfSAmir Goldstein 3178556a420SAmir Goldstein dentry = d_find_any_alias(inode); 3188556a420SAmir Goldstein if (!dentry) { 3198556a420SAmir Goldstein dentry = d_alloc_anon(inode->i_sb); 3208556a420SAmir Goldstein if (!dentry) 3218556a420SAmir Goldstein goto nomem; 322f941866fSAmir Goldstein oe = ovl_alloc_entry(lower ? 1 : 0); 3238556a420SAmir Goldstein if (!oe) 3248556a420SAmir Goldstein goto nomem; 3258556a420SAmir Goldstein 326f941866fSAmir Goldstein if (lower) { 327f941866fSAmir Goldstein oe->lowerstack->dentry = dget(lower); 328f941866fSAmir Goldstein oe->lowerstack->layer = lowerpath->layer; 329f941866fSAmir Goldstein } 3308556a420SAmir Goldstein dentry->d_fsdata = oe; 331f71bd9cfSAmir Goldstein if (upper_alias) 3328556a420SAmir Goldstein ovl_dentry_set_upper_alias(dentry); 3338556a420SAmir Goldstein } 3348556a420SAmir Goldstein 3358556a420SAmir Goldstein return d_instantiate_anon(dentry, inode); 3368556a420SAmir Goldstein 3378556a420SAmir Goldstein nomem: 3388556a420SAmir Goldstein iput(inode); 3398556a420SAmir Goldstein dput(dentry); 3408556a420SAmir Goldstein return ERR_PTR(-ENOMEM); 3418556a420SAmir Goldstein } 3428556a420SAmir Goldstein 34398892516SAmir Goldstein /* Get the upper or lower dentry in stach whose on layer @idx */ 34498892516SAmir Goldstein static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx) 34598892516SAmir Goldstein { 34698892516SAmir Goldstein struct ovl_entry *oe = dentry->d_fsdata; 34798892516SAmir Goldstein int i; 34898892516SAmir Goldstein 34998892516SAmir Goldstein if (!idx) 35098892516SAmir Goldstein return ovl_dentry_upper(dentry); 35198892516SAmir Goldstein 35298892516SAmir Goldstein for (i = 0; i < oe->numlower; i++) { 35398892516SAmir Goldstein if (oe->lowerstack[i].layer->idx == idx) 35498892516SAmir Goldstein return oe->lowerstack[i].dentry; 35598892516SAmir Goldstein } 35698892516SAmir Goldstein 35798892516SAmir Goldstein return NULL; 35898892516SAmir Goldstein } 35998892516SAmir Goldstein 3603985b70aSAmir Goldstein /* 3613985b70aSAmir Goldstein * Lookup a child overlay dentry to get a connected overlay dentry whose real 3623985b70aSAmir Goldstein * dentry is @real. If @real is on upper layer, we lookup a child overlay 3633985b70aSAmir Goldstein * dentry with the same name as the real dentry. Otherwise, we need to consult 3643985b70aSAmir Goldstein * index for lookup. 3653985b70aSAmir Goldstein */ 3663985b70aSAmir Goldstein static struct dentry *ovl_lookup_real_one(struct dentry *connected, 3673985b70aSAmir Goldstein struct dentry *real, 3683985b70aSAmir Goldstein struct ovl_layer *layer) 3693985b70aSAmir Goldstein { 3703985b70aSAmir Goldstein struct inode *dir = d_inode(connected); 3713985b70aSAmir Goldstein struct dentry *this, *parent = NULL; 3723985b70aSAmir Goldstein struct name_snapshot name; 3733985b70aSAmir Goldstein int err; 3743985b70aSAmir Goldstein 3753985b70aSAmir Goldstein /* 3763985b70aSAmir Goldstein * Lookup child overlay dentry by real name. The dir mutex protects us 3773985b70aSAmir Goldstein * from racing with overlay rename. If the overlay dentry that is above 3783985b70aSAmir Goldstein * real has already been moved to a parent that is not under the 3793985b70aSAmir Goldstein * connected overlay dir, we return -ECHILD and restart the lookup of 3803985b70aSAmir Goldstein * connected real path from the top. 3813985b70aSAmir Goldstein */ 3823985b70aSAmir Goldstein inode_lock_nested(dir, I_MUTEX_PARENT); 3833985b70aSAmir Goldstein err = -ECHILD; 3843985b70aSAmir Goldstein parent = dget_parent(real); 38598892516SAmir Goldstein if (ovl_dentry_real_at(connected, layer->idx) != parent) 3863985b70aSAmir Goldstein goto fail; 3873985b70aSAmir Goldstein 3883985b70aSAmir Goldstein /* 3893985b70aSAmir Goldstein * We also need to take a snapshot of real dentry name to protect us 3903985b70aSAmir Goldstein * from racing with underlying layer rename. In this case, we don't 3913985b70aSAmir Goldstein * care about returning ESTALE, only from dereferencing a free name 3923985b70aSAmir Goldstein * pointer because we hold no lock on the real dentry. 3933985b70aSAmir Goldstein */ 3943985b70aSAmir Goldstein take_dentry_name_snapshot(&name, real); 3953985b70aSAmir Goldstein this = lookup_one_len(name.name, connected, strlen(name.name)); 3963985b70aSAmir Goldstein err = PTR_ERR(this); 3973985b70aSAmir Goldstein if (IS_ERR(this)) { 3983985b70aSAmir Goldstein goto fail; 3993985b70aSAmir Goldstein } else if (!this || !this->d_inode) { 4003985b70aSAmir Goldstein dput(this); 4013985b70aSAmir Goldstein err = -ENOENT; 4023985b70aSAmir Goldstein goto fail; 40398892516SAmir Goldstein } else if (ovl_dentry_real_at(this, layer->idx) != real) { 4043985b70aSAmir Goldstein dput(this); 4053985b70aSAmir Goldstein err = -ESTALE; 4063985b70aSAmir Goldstein goto fail; 4073985b70aSAmir Goldstein } 4083985b70aSAmir Goldstein 4093985b70aSAmir Goldstein out: 4103985b70aSAmir Goldstein release_dentry_name_snapshot(&name); 4113985b70aSAmir Goldstein dput(parent); 4123985b70aSAmir Goldstein inode_unlock(dir); 4133985b70aSAmir Goldstein return this; 4143985b70aSAmir Goldstein 4153985b70aSAmir Goldstein fail: 4163985b70aSAmir Goldstein pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", 4173985b70aSAmir Goldstein real, layer->idx, connected, err); 4183985b70aSAmir Goldstein this = ERR_PTR(err); 4193985b70aSAmir Goldstein goto out; 4203985b70aSAmir Goldstein } 4213985b70aSAmir Goldstein 42206170154SAmir Goldstein static struct dentry *ovl_lookup_real(struct super_block *sb, 42306170154SAmir Goldstein struct dentry *real, 42406170154SAmir Goldstein struct ovl_layer *layer); 42506170154SAmir Goldstein 4263985b70aSAmir Goldstein /* 4274b91c30aSAmir Goldstein * Lookup an indexed or hashed overlay dentry by real inode. 4284b91c30aSAmir Goldstein */ 4294b91c30aSAmir Goldstein static struct dentry *ovl_lookup_real_inode(struct super_block *sb, 4304b91c30aSAmir Goldstein struct dentry *real, 4314b91c30aSAmir Goldstein struct ovl_layer *layer) 4324b91c30aSAmir Goldstein { 43306170154SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 43406170154SAmir Goldstein struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt }; 43506170154SAmir Goldstein struct dentry *index = NULL; 4364b91c30aSAmir Goldstein struct dentry *this = NULL; 4374b91c30aSAmir Goldstein struct inode *inode; 4384b91c30aSAmir Goldstein 43906170154SAmir Goldstein /* 44006170154SAmir Goldstein * Decoding upper dir from index is expensive, so first try to lookup 44106170154SAmir Goldstein * overlay dentry in inode/dcache. 44206170154SAmir Goldstein */ 4434b91c30aSAmir Goldstein inode = ovl_lookup_inode(sb, real, !layer->idx); 4444b91c30aSAmir Goldstein if (IS_ERR(inode)) 4454b91c30aSAmir Goldstein return ERR_CAST(inode); 4464b91c30aSAmir Goldstein if (inode) { 4474b91c30aSAmir Goldstein this = d_find_any_alias(inode); 4484b91c30aSAmir Goldstein iput(inode); 4494b91c30aSAmir Goldstein } 4504b91c30aSAmir Goldstein 45106170154SAmir Goldstein /* 45206170154SAmir Goldstein * For decoded lower dir file handle, lookup index by origin to check 45306170154SAmir Goldstein * if lower dir was copied up and and/or removed. 45406170154SAmir Goldstein */ 45506170154SAmir Goldstein if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) { 45606170154SAmir Goldstein index = ovl_lookup_index(ofs, NULL, real, false); 45706170154SAmir Goldstein if (IS_ERR(index)) 45806170154SAmir Goldstein return index; 45906170154SAmir Goldstein } 46006170154SAmir Goldstein 46106170154SAmir Goldstein /* Get connected upper overlay dir from index */ 46206170154SAmir Goldstein if (index) { 46306170154SAmir Goldstein struct dentry *upper = ovl_index_upper(ofs, index); 46406170154SAmir Goldstein 46506170154SAmir Goldstein dput(index); 46606170154SAmir Goldstein if (IS_ERR_OR_NULL(upper)) 46706170154SAmir Goldstein return upper; 46806170154SAmir Goldstein 46906170154SAmir Goldstein /* 47006170154SAmir Goldstein * ovl_lookup_real() in lower layer may call recursively once to 47106170154SAmir Goldstein * ovl_lookup_real() in upper layer. The first level call walks 47206170154SAmir Goldstein * back lower parents to the topmost indexed parent. The second 47306170154SAmir Goldstein * recursive call walks back from indexed upper to the topmost 47406170154SAmir Goldstein * connected/hashed upper parent (or up to root). 47506170154SAmir Goldstein */ 47606170154SAmir Goldstein this = ovl_lookup_real(sb, upper, &upper_layer); 47706170154SAmir Goldstein dput(upper); 47806170154SAmir Goldstein } 47906170154SAmir Goldstein 4807168179fSAmir Goldstein if (IS_ERR_OR_NULL(this)) 4817168179fSAmir Goldstein return this; 4824b91c30aSAmir Goldstein 4834b91c30aSAmir Goldstein if (WARN_ON(ovl_dentry_real_at(this, layer->idx) != real)) { 4844b91c30aSAmir Goldstein dput(this); 4854b91c30aSAmir Goldstein this = ERR_PTR(-EIO); 4864b91c30aSAmir Goldstein } 4874b91c30aSAmir Goldstein 4884b91c30aSAmir Goldstein return this; 4894b91c30aSAmir Goldstein } 4904b91c30aSAmir Goldstein 4914b91c30aSAmir Goldstein /* 4924b91c30aSAmir Goldstein * Lookup an indexed or hashed overlay dentry, whose real dentry is an 4934b91c30aSAmir Goldstein * ancestor of @real. 4944b91c30aSAmir Goldstein */ 4954b91c30aSAmir Goldstein static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb, 4964b91c30aSAmir Goldstein struct dentry *real, 4974b91c30aSAmir Goldstein struct ovl_layer *layer) 4984b91c30aSAmir Goldstein { 4994b91c30aSAmir Goldstein struct dentry *next, *parent = NULL; 5004b91c30aSAmir Goldstein struct dentry *ancestor = ERR_PTR(-EIO); 5014b91c30aSAmir Goldstein 5024b91c30aSAmir Goldstein if (real == layer->mnt->mnt_root) 5034b91c30aSAmir Goldstein return dget(sb->s_root); 5044b91c30aSAmir Goldstein 5054b91c30aSAmir Goldstein /* Find the topmost indexed or hashed ancestor */ 5064b91c30aSAmir Goldstein next = dget(real); 5074b91c30aSAmir Goldstein for (;;) { 5084b91c30aSAmir Goldstein parent = dget_parent(next); 5094b91c30aSAmir Goldstein 5104b91c30aSAmir Goldstein /* 5114b91c30aSAmir Goldstein * Lookup a matching overlay dentry in inode/dentry 5124b91c30aSAmir Goldstein * cache or in index by real inode. 5134b91c30aSAmir Goldstein */ 5144b91c30aSAmir Goldstein ancestor = ovl_lookup_real_inode(sb, next, layer); 5154b91c30aSAmir Goldstein if (ancestor) 5164b91c30aSAmir Goldstein break; 5174b91c30aSAmir Goldstein 5184b91c30aSAmir Goldstein if (parent == layer->mnt->mnt_root) { 5194b91c30aSAmir Goldstein ancestor = dget(sb->s_root); 5204b91c30aSAmir Goldstein break; 5214b91c30aSAmir Goldstein } 5224b91c30aSAmir Goldstein 5234b91c30aSAmir Goldstein /* 5244b91c30aSAmir Goldstein * If @real has been moved out of the layer root directory, 5254b91c30aSAmir Goldstein * we will eventully hit the real fs root. This cannot happen 5264b91c30aSAmir Goldstein * by legit overlay rename, so we return error in that case. 5274b91c30aSAmir Goldstein */ 5284b91c30aSAmir Goldstein if (parent == next) { 5294b91c30aSAmir Goldstein ancestor = ERR_PTR(-EXDEV); 5304b91c30aSAmir Goldstein break; 5314b91c30aSAmir Goldstein } 5324b91c30aSAmir Goldstein 5334b91c30aSAmir Goldstein dput(next); 5344b91c30aSAmir Goldstein next = parent; 5354b91c30aSAmir Goldstein } 5364b91c30aSAmir Goldstein 5374b91c30aSAmir Goldstein dput(parent); 5384b91c30aSAmir Goldstein dput(next); 5394b91c30aSAmir Goldstein 5404b91c30aSAmir Goldstein return ancestor; 5414b91c30aSAmir Goldstein } 5424b91c30aSAmir Goldstein 5434b91c30aSAmir Goldstein /* 5443985b70aSAmir Goldstein * Lookup a connected overlay dentry whose real dentry is @real. 5453985b70aSAmir Goldstein * If @real is on upper layer, we lookup a child overlay dentry with the same 5463985b70aSAmir Goldstein * path the real dentry. Otherwise, we need to consult index for lookup. 5473985b70aSAmir Goldstein */ 5483985b70aSAmir Goldstein static struct dentry *ovl_lookup_real(struct super_block *sb, 5493985b70aSAmir Goldstein struct dentry *real, 5503985b70aSAmir Goldstein struct ovl_layer *layer) 5513985b70aSAmir Goldstein { 5523985b70aSAmir Goldstein struct dentry *connected; 5533985b70aSAmir Goldstein int err = 0; 5543985b70aSAmir Goldstein 5554b91c30aSAmir Goldstein connected = ovl_lookup_real_ancestor(sb, real, layer); 5564b91c30aSAmir Goldstein if (IS_ERR(connected)) 5574b91c30aSAmir Goldstein return connected; 5583985b70aSAmir Goldstein 5593985b70aSAmir Goldstein while (!err) { 5603985b70aSAmir Goldstein struct dentry *next, *this; 5613985b70aSAmir Goldstein struct dentry *parent = NULL; 56298892516SAmir Goldstein struct dentry *real_connected = ovl_dentry_real_at(connected, 56398892516SAmir Goldstein layer->idx); 5643985b70aSAmir Goldstein 5653985b70aSAmir Goldstein if (real_connected == real) 5663985b70aSAmir Goldstein break; 5673985b70aSAmir Goldstein 5683985b70aSAmir Goldstein /* Find the topmost dentry not yet connected */ 5693985b70aSAmir Goldstein next = dget(real); 5703985b70aSAmir Goldstein for (;;) { 5713985b70aSAmir Goldstein parent = dget_parent(next); 5723985b70aSAmir Goldstein 5733985b70aSAmir Goldstein if (parent == real_connected) 5743985b70aSAmir Goldstein break; 5753985b70aSAmir Goldstein 5763985b70aSAmir Goldstein /* 5773985b70aSAmir Goldstein * If real has been moved out of 'real_connected', 5783985b70aSAmir Goldstein * we will not find 'real_connected' and hit the layer 5793985b70aSAmir Goldstein * root. In that case, we need to restart connecting. 5803985b70aSAmir Goldstein * This game can go on forever in the worst case. We 5813985b70aSAmir Goldstein * may want to consider taking s_vfs_rename_mutex if 5823985b70aSAmir Goldstein * this happens more than once. 5833985b70aSAmir Goldstein */ 5843985b70aSAmir Goldstein if (parent == layer->mnt->mnt_root) { 5853985b70aSAmir Goldstein dput(connected); 5863985b70aSAmir Goldstein connected = dget(sb->s_root); 5873985b70aSAmir Goldstein break; 5883985b70aSAmir Goldstein } 5893985b70aSAmir Goldstein 5903985b70aSAmir Goldstein /* 5913985b70aSAmir Goldstein * If real file has been moved out of the layer root 5923985b70aSAmir Goldstein * directory, we will eventully hit the real fs root. 5933985b70aSAmir Goldstein * This cannot happen by legit overlay rename, so we 5943985b70aSAmir Goldstein * return error in that case. 5953985b70aSAmir Goldstein */ 5963985b70aSAmir Goldstein if (parent == next) { 5973985b70aSAmir Goldstein err = -EXDEV; 5983985b70aSAmir Goldstein break; 5993985b70aSAmir Goldstein } 6003985b70aSAmir Goldstein 6013985b70aSAmir Goldstein dput(next); 6023985b70aSAmir Goldstein next = parent; 6033985b70aSAmir Goldstein } 6043985b70aSAmir Goldstein 6053985b70aSAmir Goldstein if (!err) { 6063985b70aSAmir Goldstein this = ovl_lookup_real_one(connected, next, layer); 6073985b70aSAmir Goldstein if (IS_ERR(this)) 6083985b70aSAmir Goldstein err = PTR_ERR(this); 6093985b70aSAmir Goldstein 6103985b70aSAmir Goldstein /* 6113985b70aSAmir Goldstein * Lookup of child in overlay can fail when racing with 6123985b70aSAmir Goldstein * overlay rename of child away from 'connected' parent. 6133985b70aSAmir Goldstein * In this case, we need to restart the lookup from the 6143985b70aSAmir Goldstein * top, because we cannot trust that 'real_connected' is 6154b91c30aSAmir Goldstein * still an ancestor of 'real'. There is a good chance 6164b91c30aSAmir Goldstein * that the renamed overlay ancestor is now in cache, so 6174b91c30aSAmir Goldstein * ovl_lookup_real_ancestor() will find it and we can 6184b91c30aSAmir Goldstein * continue to connect exactly from where lookup failed. 6193985b70aSAmir Goldstein */ 6203985b70aSAmir Goldstein if (err == -ECHILD) { 6214b91c30aSAmir Goldstein this = ovl_lookup_real_ancestor(sb, real, 6224b91c30aSAmir Goldstein layer); 623b5095f24SFengguang Wu err = PTR_ERR_OR_ZERO(this); 6243985b70aSAmir Goldstein } 6253985b70aSAmir Goldstein if (!err) { 6263985b70aSAmir Goldstein dput(connected); 6273985b70aSAmir Goldstein connected = this; 6283985b70aSAmir Goldstein } 6293985b70aSAmir Goldstein } 6303985b70aSAmir Goldstein 6313985b70aSAmir Goldstein dput(parent); 6323985b70aSAmir Goldstein dput(next); 6333985b70aSAmir Goldstein } 6343985b70aSAmir Goldstein 6353985b70aSAmir Goldstein if (err) 6363985b70aSAmir Goldstein goto fail; 6373985b70aSAmir Goldstein 6383985b70aSAmir Goldstein return connected; 6393985b70aSAmir Goldstein 6403985b70aSAmir Goldstein fail: 6413985b70aSAmir Goldstein pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", 6423985b70aSAmir Goldstein real, layer->idx, connected, err); 6433985b70aSAmir Goldstein dput(connected); 6443985b70aSAmir Goldstein return ERR_PTR(err); 6453985b70aSAmir Goldstein } 6463985b70aSAmir Goldstein 6473985b70aSAmir Goldstein /* 648f71bd9cfSAmir Goldstein * Get an overlay dentry from upper/lower real dentries and index. 6493985b70aSAmir Goldstein */ 6503985b70aSAmir Goldstein static struct dentry *ovl_get_dentry(struct super_block *sb, 6513985b70aSAmir Goldstein struct dentry *upper, 652f71bd9cfSAmir Goldstein struct ovl_path *lowerpath, 653f71bd9cfSAmir Goldstein struct dentry *index) 6543985b70aSAmir Goldstein { 6553985b70aSAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 6563985b70aSAmir Goldstein struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt }; 65798892516SAmir Goldstein struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer; 658f71bd9cfSAmir Goldstein struct dentry *real = upper ?: (index ?: lowerpath->dentry); 6593985b70aSAmir Goldstein 660f941866fSAmir Goldstein /* 661f71bd9cfSAmir Goldstein * Obtain a disconnected overlay dentry from a non-dir real dentry 662f71bd9cfSAmir Goldstein * and index. 663f941866fSAmir Goldstein */ 664f71bd9cfSAmir Goldstein if (!d_is_dir(real)) 665f71bd9cfSAmir Goldstein return ovl_obtain_alias(sb, upper, lowerpath, index); 666f941866fSAmir Goldstein 6673985b70aSAmir Goldstein /* Removed empty directory? */ 66898892516SAmir Goldstein if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real)) 6693985b70aSAmir Goldstein return ERR_PTR(-ENOENT); 6703985b70aSAmir Goldstein 6713985b70aSAmir Goldstein /* 67298892516SAmir Goldstein * If real dentry is connected and hashed, get a connected overlay 67398892516SAmir Goldstein * dentry whose real dentry is @real. 6743985b70aSAmir Goldstein */ 67598892516SAmir Goldstein return ovl_lookup_real(sb, real, layer); 6763985b70aSAmir Goldstein } 6773985b70aSAmir Goldstein 6788556a420SAmir Goldstein static struct dentry *ovl_upper_fh_to_d(struct super_block *sb, 6798556a420SAmir Goldstein struct ovl_fh *fh) 6808556a420SAmir Goldstein { 6818556a420SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 6828556a420SAmir Goldstein struct dentry *dentry; 6838556a420SAmir Goldstein struct dentry *upper; 6848556a420SAmir Goldstein 6858556a420SAmir Goldstein if (!ofs->upper_mnt) 6868556a420SAmir Goldstein return ERR_PTR(-EACCES); 6878556a420SAmir Goldstein 6888556a420SAmir Goldstein upper = ovl_decode_fh(fh, ofs->upper_mnt); 6898556a420SAmir Goldstein if (IS_ERR_OR_NULL(upper)) 6908556a420SAmir Goldstein return upper; 6918556a420SAmir Goldstein 692f71bd9cfSAmir Goldstein dentry = ovl_get_dentry(sb, upper, NULL, NULL); 6938556a420SAmir Goldstein dput(upper); 6948556a420SAmir Goldstein 6958556a420SAmir Goldstein return dentry; 6968556a420SAmir Goldstein } 6978556a420SAmir Goldstein 698f941866fSAmir Goldstein static struct dentry *ovl_lower_fh_to_d(struct super_block *sb, 699f941866fSAmir Goldstein struct ovl_fh *fh) 700f941866fSAmir Goldstein { 701f941866fSAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 702f941866fSAmir Goldstein struct ovl_path origin = { }; 703f941866fSAmir Goldstein struct ovl_path *stack = &origin; 704f941866fSAmir Goldstein struct dentry *dentry = NULL; 705f71bd9cfSAmir Goldstein struct dentry *index = NULL; 7069436a1a3SAmir Goldstein struct inode *inode = NULL; 7079436a1a3SAmir Goldstein bool is_deleted = false; 708f941866fSAmir Goldstein int err; 709f941866fSAmir Goldstein 710f71bd9cfSAmir Goldstein /* First lookup indexed upper by fh */ 711f71bd9cfSAmir Goldstein if (ofs->indexdir) { 712f71bd9cfSAmir Goldstein index = ovl_get_index_fh(ofs, fh); 713f71bd9cfSAmir Goldstein err = PTR_ERR(index); 7149436a1a3SAmir Goldstein if (IS_ERR(index)) { 7159436a1a3SAmir Goldstein if (err != -ESTALE) 716f941866fSAmir Goldstein return ERR_PTR(err); 7179436a1a3SAmir Goldstein 7189436a1a3SAmir Goldstein /* Found a whiteout index - treat as deleted inode */ 7199436a1a3SAmir Goldstein is_deleted = true; 7209436a1a3SAmir Goldstein index = NULL; 7219436a1a3SAmir Goldstein } 722f71bd9cfSAmir Goldstein } 723f941866fSAmir Goldstein 7243b0bfc6eSAmir Goldstein /* Then try to get upper dir by index */ 7253b0bfc6eSAmir Goldstein if (index && d_is_dir(index)) { 7263b0bfc6eSAmir Goldstein struct dentry *upper = ovl_index_upper(ofs, index); 7273b0bfc6eSAmir Goldstein 7283b0bfc6eSAmir Goldstein err = PTR_ERR(upper); 7293b0bfc6eSAmir Goldstein if (IS_ERR_OR_NULL(upper)) 7303b0bfc6eSAmir Goldstein goto out_err; 7313b0bfc6eSAmir Goldstein 7323b0bfc6eSAmir Goldstein dentry = ovl_get_dentry(sb, upper, NULL, NULL); 7333b0bfc6eSAmir Goldstein dput(upper); 7343b0bfc6eSAmir Goldstein goto out; 7353b0bfc6eSAmir Goldstein } 7363b0bfc6eSAmir Goldstein 737f71bd9cfSAmir Goldstein /* Then lookup origin by fh */ 738f71bd9cfSAmir Goldstein err = ovl_check_origin_fh(ofs, fh, NULL, &stack); 739f71bd9cfSAmir Goldstein if (err) { 740f71bd9cfSAmir Goldstein goto out_err; 741f71bd9cfSAmir Goldstein } else if (index) { 742f71bd9cfSAmir Goldstein err = ovl_verify_origin(index, origin.dentry, false); 743f71bd9cfSAmir Goldstein if (err) 744f71bd9cfSAmir Goldstein goto out_err; 7459436a1a3SAmir Goldstein } else if (is_deleted) { 7469436a1a3SAmir Goldstein /* Lookup deleted non-dir by origin inode */ 7479436a1a3SAmir Goldstein if (!d_is_dir(origin.dentry)) 7484b91c30aSAmir Goldstein inode = ovl_lookup_inode(sb, origin.dentry, false); 7499436a1a3SAmir Goldstein err = -ESTALE; 7509436a1a3SAmir Goldstein if (!inode || atomic_read(&inode->i_count) == 1) 7519436a1a3SAmir Goldstein goto out_err; 7529436a1a3SAmir Goldstein 7539436a1a3SAmir Goldstein /* Deleted but still open? */ 7549436a1a3SAmir Goldstein index = dget(ovl_i_dentry_upper(inode)); 755f71bd9cfSAmir Goldstein } 756f71bd9cfSAmir Goldstein 757f71bd9cfSAmir Goldstein dentry = ovl_get_dentry(sb, NULL, &origin, index); 758f71bd9cfSAmir Goldstein 759f71bd9cfSAmir Goldstein out: 760f941866fSAmir Goldstein dput(origin.dentry); 761f71bd9cfSAmir Goldstein dput(index); 7629436a1a3SAmir Goldstein iput(inode); 763f941866fSAmir Goldstein return dentry; 764f71bd9cfSAmir Goldstein 765f71bd9cfSAmir Goldstein out_err: 766f71bd9cfSAmir Goldstein dentry = ERR_PTR(err); 767f71bd9cfSAmir Goldstein goto out; 768f941866fSAmir Goldstein } 769f941866fSAmir Goldstein 7708556a420SAmir Goldstein static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid, 7718556a420SAmir Goldstein int fh_len, int fh_type) 7728556a420SAmir Goldstein { 7738556a420SAmir Goldstein struct dentry *dentry = NULL; 7748556a420SAmir Goldstein struct ovl_fh *fh = (struct ovl_fh *) fid; 7758556a420SAmir Goldstein int len = fh_len << 2; 7768556a420SAmir Goldstein unsigned int flags = 0; 7778556a420SAmir Goldstein int err; 7788556a420SAmir Goldstein 7798556a420SAmir Goldstein err = -EINVAL; 7808556a420SAmir Goldstein if (fh_type != OVL_FILEID) 7818556a420SAmir Goldstein goto out_err; 7828556a420SAmir Goldstein 7838556a420SAmir Goldstein err = ovl_check_fh_len(fh, len); 7848556a420SAmir Goldstein if (err) 7858556a420SAmir Goldstein goto out_err; 7868556a420SAmir Goldstein 7878556a420SAmir Goldstein flags = fh->flags; 788f941866fSAmir Goldstein dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ? 789f941866fSAmir Goldstein ovl_upper_fh_to_d(sb, fh) : 790f941866fSAmir Goldstein ovl_lower_fh_to_d(sb, fh); 7918556a420SAmir Goldstein err = PTR_ERR(dentry); 7928556a420SAmir Goldstein if (IS_ERR(dentry) && err != -ESTALE) 7938556a420SAmir Goldstein goto out_err; 7948556a420SAmir Goldstein 7958556a420SAmir Goldstein return dentry; 7968556a420SAmir Goldstein 7978556a420SAmir Goldstein out_err: 7988556a420SAmir Goldstein pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n", 7998556a420SAmir Goldstein len, fh_type, flags, err); 8008556a420SAmir Goldstein return ERR_PTR(err); 8018556a420SAmir Goldstein } 8028556a420SAmir Goldstein 8033985b70aSAmir Goldstein static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid, 8043985b70aSAmir Goldstein int fh_len, int fh_type) 8053985b70aSAmir Goldstein { 8063985b70aSAmir Goldstein pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n"); 8073985b70aSAmir Goldstein return ERR_PTR(-EACCES); 8083985b70aSAmir Goldstein } 8093985b70aSAmir Goldstein 8103985b70aSAmir Goldstein static int ovl_get_name(struct dentry *parent, char *name, 8113985b70aSAmir Goldstein struct dentry *child) 8123985b70aSAmir Goldstein { 8133985b70aSAmir Goldstein /* 8143985b70aSAmir Goldstein * ovl_fh_to_dentry() returns connected dir overlay dentries and 8153985b70aSAmir Goldstein * ovl_fh_to_parent() is not implemented, so we should not get here. 8163985b70aSAmir Goldstein */ 8173985b70aSAmir Goldstein WARN_ON_ONCE(1); 8183985b70aSAmir Goldstein return -EIO; 8193985b70aSAmir Goldstein } 8203985b70aSAmir Goldstein 8213985b70aSAmir Goldstein static struct dentry *ovl_get_parent(struct dentry *dentry) 8223985b70aSAmir Goldstein { 8233985b70aSAmir Goldstein /* 8243985b70aSAmir Goldstein * ovl_fh_to_dentry() returns connected dir overlay dentries, so we 8253985b70aSAmir Goldstein * should not get here. 8263985b70aSAmir Goldstein */ 8273985b70aSAmir Goldstein WARN_ON_ONCE(1); 8283985b70aSAmir Goldstein return ERR_PTR(-EIO); 8293985b70aSAmir Goldstein } 8303985b70aSAmir Goldstein 8318ed5eec9SAmir Goldstein const struct export_operations ovl_export_operations = { 8328ed5eec9SAmir Goldstein .encode_fh = ovl_encode_inode_fh, 8338556a420SAmir Goldstein .fh_to_dentry = ovl_fh_to_dentry, 8343985b70aSAmir Goldstein .fh_to_parent = ovl_fh_to_parent, 8353985b70aSAmir Goldstein .get_name = ovl_get_name, 8363985b70aSAmir Goldstein .get_parent = ovl_get_parent, 8378ed5eec9SAmir Goldstein }; 838