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 22b305e844SAmir Goldstein /* 23b305e844SAmir Goldstein * We only need to encode origin if there is a chance that the same object was 24b305e844SAmir Goldstein * encoded pre copy up and then we need to stay consistent with the same 25b305e844SAmir Goldstein * encoding also after copy up. If non-pure upper is not indexed, then it was 26b305e844SAmir Goldstein * copied up before NFS export was enabled. In that case we don't need to worry 27b305e844SAmir Goldstein * about staying consistent with pre copy up encoding and we encode an upper 28b305e844SAmir Goldstein * file handle. Overlay root dentry is a private case of non-indexed upper. 29b305e844SAmir Goldstein * 30b305e844SAmir Goldstein * The following table summarizes the different file handle encodings used for 31b305e844SAmir Goldstein * different overlay object types: 32b305e844SAmir Goldstein * 33b305e844SAmir Goldstein * Object type | Encoding 34b305e844SAmir Goldstein * -------------------------------- 35b305e844SAmir Goldstein * Pure upper | U 36b305e844SAmir Goldstein * Non-indexed upper | U 3705e1f118SAmir Goldstein * Indexed upper | L (*) 3805e1f118SAmir Goldstein * Non-upper | L (*) 39b305e844SAmir Goldstein * 40b305e844SAmir Goldstein * U = upper file handle 41b305e844SAmir Goldstein * L = lower file handle 4205e1f118SAmir Goldstein * 4305e1f118SAmir Goldstein * (*) Connecting an overlay dir from real lower dentry is not always 4405e1f118SAmir Goldstein * possible when there are redirects in lower layers. To mitigate this case, 4505e1f118SAmir Goldstein * we copy up the lower dir first and then encode an upper dir file handle. 46b305e844SAmir Goldstein */ 47b305e844SAmir Goldstein static bool ovl_should_encode_origin(struct dentry *dentry) 48b305e844SAmir Goldstein { 4905e1f118SAmir Goldstein struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 5005e1f118SAmir Goldstein 51b305e844SAmir Goldstein if (!ovl_dentry_lower(dentry)) 52b305e844SAmir Goldstein return false; 53b305e844SAmir Goldstein 5405e1f118SAmir Goldstein /* 5505e1f118SAmir Goldstein * Decoding a merge dir, whose origin's parent is under a redirected 5605e1f118SAmir Goldstein * lower dir is not always possible. As a simple aproximation, we do 5705e1f118SAmir Goldstein * not encode lower dir file handles when overlay has multiple lower 5805e1f118SAmir Goldstein * layers and origin is below the topmost lower layer. 5905e1f118SAmir Goldstein * 6005e1f118SAmir Goldstein * TODO: copy up only the parent that is under redirected lower. 6105e1f118SAmir Goldstein */ 6205e1f118SAmir Goldstein if (d_is_dir(dentry) && ofs->upper_mnt && 6305e1f118SAmir Goldstein OVL_E(dentry)->lowerstack[0].layer->idx > 1) 6405e1f118SAmir Goldstein return false; 6505e1f118SAmir Goldstein 66b305e844SAmir Goldstein /* Decoding a non-indexed upper from origin is not implemented */ 67b305e844SAmir Goldstein if (ovl_dentry_upper(dentry) && 68b305e844SAmir Goldstein !ovl_test_flag(OVL_INDEX, d_inode(dentry))) 69b305e844SAmir Goldstein return false; 70b305e844SAmir Goldstein 71b305e844SAmir Goldstein return true; 72b305e844SAmir Goldstein } 73b305e844SAmir Goldstein 7405e1f118SAmir Goldstein static int ovl_encode_maybe_copy_up(struct dentry *dentry) 7505e1f118SAmir Goldstein { 7605e1f118SAmir Goldstein int err; 7705e1f118SAmir Goldstein 7805e1f118SAmir Goldstein if (ovl_dentry_upper(dentry)) 7905e1f118SAmir Goldstein return 0; 8005e1f118SAmir Goldstein 8105e1f118SAmir Goldstein err = ovl_want_write(dentry); 8205e1f118SAmir Goldstein if (err) 8305e1f118SAmir Goldstein return err; 8405e1f118SAmir Goldstein 8505e1f118SAmir Goldstein err = ovl_copy_up(dentry); 8605e1f118SAmir Goldstein 8705e1f118SAmir Goldstein ovl_drop_write(dentry); 8805e1f118SAmir Goldstein return err; 8905e1f118SAmir Goldstein } 9005e1f118SAmir Goldstein 918ed5eec9SAmir Goldstein static int ovl_d_to_fh(struct dentry *dentry, char *buf, int buflen) 928ed5eec9SAmir Goldstein { 938ed5eec9SAmir Goldstein struct dentry *origin = ovl_dentry_lower(dentry); 948ed5eec9SAmir Goldstein struct ovl_fh *fh = NULL; 958ed5eec9SAmir Goldstein int err; 968ed5eec9SAmir Goldstein 9705e1f118SAmir Goldstein /* 9805e1f118SAmir Goldstein * If we should not encode a lower dir file handle, copy up and encode 9905e1f118SAmir Goldstein * an upper dir file handle. 10005e1f118SAmir Goldstein */ 10105e1f118SAmir Goldstein if (!ovl_should_encode_origin(dentry)) { 10205e1f118SAmir Goldstein err = ovl_encode_maybe_copy_up(dentry); 10305e1f118SAmir Goldstein if (err) 10405e1f118SAmir Goldstein goto fail; 1058ed5eec9SAmir Goldstein 10605e1f118SAmir Goldstein origin = NULL; 10705e1f118SAmir Goldstein } 10805e1f118SAmir Goldstein 10903e1c584SAmir Goldstein /* Encode an upper or origin file handle */ 11003e1c584SAmir Goldstein fh = ovl_encode_fh(origin ?: ovl_dentry_upper(dentry), !origin); 1118ed5eec9SAmir Goldstein 1128ed5eec9SAmir Goldstein err = -EOVERFLOW; 1138ed5eec9SAmir Goldstein if (fh->len > buflen) 1148ed5eec9SAmir Goldstein goto fail; 1158ed5eec9SAmir Goldstein 1168ed5eec9SAmir Goldstein memcpy(buf, (char *)fh, fh->len); 1178ed5eec9SAmir Goldstein err = fh->len; 1188ed5eec9SAmir Goldstein 1198ed5eec9SAmir Goldstein out: 1208ed5eec9SAmir Goldstein kfree(fh); 1218ed5eec9SAmir Goldstein return err; 1228ed5eec9SAmir Goldstein 1238ed5eec9SAmir Goldstein fail: 1248ed5eec9SAmir Goldstein pr_warn_ratelimited("overlayfs: failed to encode file handle (%pd2, err=%i, buflen=%d, len=%d, type=%d)\n", 1258ed5eec9SAmir Goldstein dentry, err, buflen, fh ? (int)fh->len : 0, 1268ed5eec9SAmir Goldstein fh ? fh->type : 0); 1278ed5eec9SAmir Goldstein goto out; 1288ed5eec9SAmir Goldstein } 1298ed5eec9SAmir Goldstein 1308ed5eec9SAmir Goldstein static int ovl_dentry_to_fh(struct dentry *dentry, u32 *fid, int *max_len) 1318ed5eec9SAmir Goldstein { 1328ed5eec9SAmir Goldstein int res, len = *max_len << 2; 1338ed5eec9SAmir Goldstein 1348ed5eec9SAmir Goldstein res = ovl_d_to_fh(dentry, (char *)fid, len); 1358ed5eec9SAmir Goldstein if (res <= 0) 1368ed5eec9SAmir Goldstein return FILEID_INVALID; 1378ed5eec9SAmir Goldstein 1388ed5eec9SAmir Goldstein len = res; 1398ed5eec9SAmir Goldstein 1408ed5eec9SAmir Goldstein /* Round up to dwords */ 1418ed5eec9SAmir Goldstein *max_len = (len + 3) >> 2; 1428ed5eec9SAmir Goldstein return OVL_FILEID; 1438ed5eec9SAmir Goldstein } 1448ed5eec9SAmir Goldstein 1458ed5eec9SAmir Goldstein static int ovl_encode_inode_fh(struct inode *inode, u32 *fid, int *max_len, 1468ed5eec9SAmir Goldstein struct inode *parent) 1478ed5eec9SAmir Goldstein { 1488ed5eec9SAmir Goldstein struct dentry *dentry; 1498ed5eec9SAmir Goldstein int type; 1508ed5eec9SAmir Goldstein 1518ed5eec9SAmir Goldstein /* TODO: encode connectable file handles */ 1528ed5eec9SAmir Goldstein if (parent) 1538ed5eec9SAmir Goldstein return FILEID_INVALID; 1548ed5eec9SAmir Goldstein 1558ed5eec9SAmir Goldstein dentry = d_find_any_alias(inode); 1568ed5eec9SAmir Goldstein if (WARN_ON(!dentry)) 1578ed5eec9SAmir Goldstein return FILEID_INVALID; 1588ed5eec9SAmir Goldstein 1598ed5eec9SAmir Goldstein type = ovl_dentry_to_fh(dentry, fid, max_len); 1608ed5eec9SAmir Goldstein 1618ed5eec9SAmir Goldstein dput(dentry); 1628ed5eec9SAmir Goldstein return type; 1638ed5eec9SAmir Goldstein } 1648ed5eec9SAmir Goldstein 1658556a420SAmir Goldstein /* 166f71bd9cfSAmir Goldstein * Find or instantiate an overlay dentry from real dentries and index. 1678556a420SAmir Goldstein */ 1688556a420SAmir Goldstein static struct dentry *ovl_obtain_alias(struct super_block *sb, 169f71bd9cfSAmir Goldstein struct dentry *upper_alias, 170f71bd9cfSAmir Goldstein struct ovl_path *lowerpath, 171f71bd9cfSAmir Goldstein struct dentry *index) 1728556a420SAmir Goldstein { 173f941866fSAmir Goldstein struct dentry *lower = lowerpath ? lowerpath->dentry : NULL; 174f71bd9cfSAmir Goldstein struct dentry *upper = upper_alias ?: index; 1758556a420SAmir Goldstein struct dentry *dentry; 176f941866fSAmir Goldstein struct inode *inode; 1778556a420SAmir Goldstein struct ovl_entry *oe; 1788556a420SAmir Goldstein 179f71bd9cfSAmir Goldstein /* We get overlay directory dentries with ovl_lookup_real() */ 180f71bd9cfSAmir Goldstein if (d_is_dir(upper ?: lower)) 1818556a420SAmir Goldstein return ERR_PTR(-EIO); 1828556a420SAmir Goldstein 183f71bd9cfSAmir Goldstein inode = ovl_get_inode(sb, dget(upper), lower, index, !!lower); 1848556a420SAmir Goldstein if (IS_ERR(inode)) { 1858556a420SAmir Goldstein dput(upper); 1868556a420SAmir Goldstein return ERR_CAST(inode); 1878556a420SAmir Goldstein } 1888556a420SAmir Goldstein 189f71bd9cfSAmir Goldstein if (index) 190f71bd9cfSAmir Goldstein ovl_set_flag(OVL_INDEX, inode); 191f71bd9cfSAmir Goldstein 1928556a420SAmir Goldstein dentry = d_find_any_alias(inode); 1938556a420SAmir Goldstein if (!dentry) { 1948556a420SAmir Goldstein dentry = d_alloc_anon(inode->i_sb); 1958556a420SAmir Goldstein if (!dentry) 1968556a420SAmir Goldstein goto nomem; 197f941866fSAmir Goldstein oe = ovl_alloc_entry(lower ? 1 : 0); 1988556a420SAmir Goldstein if (!oe) 1998556a420SAmir Goldstein goto nomem; 2008556a420SAmir Goldstein 201f941866fSAmir Goldstein if (lower) { 202f941866fSAmir Goldstein oe->lowerstack->dentry = dget(lower); 203f941866fSAmir Goldstein oe->lowerstack->layer = lowerpath->layer; 204f941866fSAmir Goldstein } 2058556a420SAmir Goldstein dentry->d_fsdata = oe; 206f71bd9cfSAmir Goldstein if (upper_alias) 2078556a420SAmir Goldstein ovl_dentry_set_upper_alias(dentry); 2088556a420SAmir Goldstein } 2098556a420SAmir Goldstein 2108556a420SAmir Goldstein return d_instantiate_anon(dentry, inode); 2118556a420SAmir Goldstein 2128556a420SAmir Goldstein nomem: 2138556a420SAmir Goldstein iput(inode); 2148556a420SAmir Goldstein dput(dentry); 2158556a420SAmir Goldstein return ERR_PTR(-ENOMEM); 2168556a420SAmir Goldstein } 2178556a420SAmir Goldstein 21898892516SAmir Goldstein /* Get the upper or lower dentry in stach whose on layer @idx */ 21998892516SAmir Goldstein static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx) 22098892516SAmir Goldstein { 22198892516SAmir Goldstein struct ovl_entry *oe = dentry->d_fsdata; 22298892516SAmir Goldstein int i; 22398892516SAmir Goldstein 22498892516SAmir Goldstein if (!idx) 22598892516SAmir Goldstein return ovl_dentry_upper(dentry); 22698892516SAmir Goldstein 22798892516SAmir Goldstein for (i = 0; i < oe->numlower; i++) { 22898892516SAmir Goldstein if (oe->lowerstack[i].layer->idx == idx) 22998892516SAmir Goldstein return oe->lowerstack[i].dentry; 23098892516SAmir Goldstein } 23198892516SAmir Goldstein 23298892516SAmir Goldstein return NULL; 23398892516SAmir Goldstein } 23498892516SAmir Goldstein 2353985b70aSAmir Goldstein /* 2363985b70aSAmir Goldstein * Lookup a child overlay dentry to get a connected overlay dentry whose real 2373985b70aSAmir Goldstein * dentry is @real. If @real is on upper layer, we lookup a child overlay 2383985b70aSAmir Goldstein * dentry with the same name as the real dentry. Otherwise, we need to consult 2393985b70aSAmir Goldstein * index for lookup. 2403985b70aSAmir Goldstein */ 2413985b70aSAmir Goldstein static struct dentry *ovl_lookup_real_one(struct dentry *connected, 2423985b70aSAmir Goldstein struct dentry *real, 2433985b70aSAmir Goldstein struct ovl_layer *layer) 2443985b70aSAmir Goldstein { 2453985b70aSAmir Goldstein struct inode *dir = d_inode(connected); 2463985b70aSAmir Goldstein struct dentry *this, *parent = NULL; 2473985b70aSAmir Goldstein struct name_snapshot name; 2483985b70aSAmir Goldstein int err; 2493985b70aSAmir Goldstein 2503985b70aSAmir Goldstein /* 2513985b70aSAmir Goldstein * Lookup child overlay dentry by real name. The dir mutex protects us 2523985b70aSAmir Goldstein * from racing with overlay rename. If the overlay dentry that is above 2533985b70aSAmir Goldstein * real has already been moved to a parent that is not under the 2543985b70aSAmir Goldstein * connected overlay dir, we return -ECHILD and restart the lookup of 2553985b70aSAmir Goldstein * connected real path from the top. 2563985b70aSAmir Goldstein */ 2573985b70aSAmir Goldstein inode_lock_nested(dir, I_MUTEX_PARENT); 2583985b70aSAmir Goldstein err = -ECHILD; 2593985b70aSAmir Goldstein parent = dget_parent(real); 26098892516SAmir Goldstein if (ovl_dentry_real_at(connected, layer->idx) != parent) 2613985b70aSAmir Goldstein goto fail; 2623985b70aSAmir Goldstein 2633985b70aSAmir Goldstein /* 2643985b70aSAmir Goldstein * We also need to take a snapshot of real dentry name to protect us 2653985b70aSAmir Goldstein * from racing with underlying layer rename. In this case, we don't 2663985b70aSAmir Goldstein * care about returning ESTALE, only from dereferencing a free name 2673985b70aSAmir Goldstein * pointer because we hold no lock on the real dentry. 2683985b70aSAmir Goldstein */ 2693985b70aSAmir Goldstein take_dentry_name_snapshot(&name, real); 2703985b70aSAmir Goldstein this = lookup_one_len(name.name, connected, strlen(name.name)); 2713985b70aSAmir Goldstein err = PTR_ERR(this); 2723985b70aSAmir Goldstein if (IS_ERR(this)) { 2733985b70aSAmir Goldstein goto fail; 2743985b70aSAmir Goldstein } else if (!this || !this->d_inode) { 2753985b70aSAmir Goldstein dput(this); 2763985b70aSAmir Goldstein err = -ENOENT; 2773985b70aSAmir Goldstein goto fail; 27898892516SAmir Goldstein } else if (ovl_dentry_real_at(this, layer->idx) != real) { 2793985b70aSAmir Goldstein dput(this); 2803985b70aSAmir Goldstein err = -ESTALE; 2813985b70aSAmir Goldstein goto fail; 2823985b70aSAmir Goldstein } 2833985b70aSAmir Goldstein 2843985b70aSAmir Goldstein out: 2853985b70aSAmir Goldstein release_dentry_name_snapshot(&name); 2863985b70aSAmir Goldstein dput(parent); 2873985b70aSAmir Goldstein inode_unlock(dir); 2883985b70aSAmir Goldstein return this; 2893985b70aSAmir Goldstein 2903985b70aSAmir Goldstein fail: 2913985b70aSAmir Goldstein pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", 2923985b70aSAmir Goldstein real, layer->idx, connected, err); 2933985b70aSAmir Goldstein this = ERR_PTR(err); 2943985b70aSAmir Goldstein goto out; 2953985b70aSAmir Goldstein } 2963985b70aSAmir Goldstein 2973985b70aSAmir Goldstein /* 2984b91c30aSAmir Goldstein * Lookup an indexed or hashed overlay dentry by real inode. 2994b91c30aSAmir Goldstein */ 3004b91c30aSAmir Goldstein static struct dentry *ovl_lookup_real_inode(struct super_block *sb, 3014b91c30aSAmir Goldstein struct dentry *real, 3024b91c30aSAmir Goldstein struct ovl_layer *layer) 3034b91c30aSAmir Goldstein { 3044b91c30aSAmir Goldstein struct dentry *this = NULL; 3054b91c30aSAmir Goldstein struct inode *inode; 3064b91c30aSAmir Goldstein 3074b91c30aSAmir Goldstein inode = ovl_lookup_inode(sb, real, !layer->idx); 3084b91c30aSAmir Goldstein if (IS_ERR(inode)) 3094b91c30aSAmir Goldstein return ERR_CAST(inode); 3104b91c30aSAmir Goldstein if (inode) { 3114b91c30aSAmir Goldstein this = d_find_any_alias(inode); 3124b91c30aSAmir Goldstein iput(inode); 3134b91c30aSAmir Goldstein } 3144b91c30aSAmir Goldstein 3154b91c30aSAmir Goldstein /* TODO: use index when looking up by origin inode */ 3164b91c30aSAmir Goldstein if (!this) 3174b91c30aSAmir Goldstein return NULL; 3184b91c30aSAmir Goldstein 3194b91c30aSAmir Goldstein if (WARN_ON(ovl_dentry_real_at(this, layer->idx) != real)) { 3204b91c30aSAmir Goldstein dput(this); 3214b91c30aSAmir Goldstein this = ERR_PTR(-EIO); 3224b91c30aSAmir Goldstein } 3234b91c30aSAmir Goldstein 3244b91c30aSAmir Goldstein return this; 3254b91c30aSAmir Goldstein } 3264b91c30aSAmir Goldstein 3274b91c30aSAmir Goldstein /* 3284b91c30aSAmir Goldstein * Lookup an indexed or hashed overlay dentry, whose real dentry is an 3294b91c30aSAmir Goldstein * ancestor of @real. 3304b91c30aSAmir Goldstein */ 3314b91c30aSAmir Goldstein static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb, 3324b91c30aSAmir Goldstein struct dentry *real, 3334b91c30aSAmir Goldstein struct ovl_layer *layer) 3344b91c30aSAmir Goldstein { 3354b91c30aSAmir Goldstein struct dentry *next, *parent = NULL; 3364b91c30aSAmir Goldstein struct dentry *ancestor = ERR_PTR(-EIO); 3374b91c30aSAmir Goldstein 3384b91c30aSAmir Goldstein if (real == layer->mnt->mnt_root) 3394b91c30aSAmir Goldstein return dget(sb->s_root); 3404b91c30aSAmir Goldstein 3414b91c30aSAmir Goldstein /* Find the topmost indexed or hashed ancestor */ 3424b91c30aSAmir Goldstein next = dget(real); 3434b91c30aSAmir Goldstein for (;;) { 3444b91c30aSAmir Goldstein parent = dget_parent(next); 3454b91c30aSAmir Goldstein 3464b91c30aSAmir Goldstein /* 3474b91c30aSAmir Goldstein * Lookup a matching overlay dentry in inode/dentry 3484b91c30aSAmir Goldstein * cache or in index by real inode. 3494b91c30aSAmir Goldstein */ 3504b91c30aSAmir Goldstein ancestor = ovl_lookup_real_inode(sb, next, layer); 3514b91c30aSAmir Goldstein if (ancestor) 3524b91c30aSAmir Goldstein break; 3534b91c30aSAmir Goldstein 3544b91c30aSAmir Goldstein if (parent == layer->mnt->mnt_root) { 3554b91c30aSAmir Goldstein ancestor = dget(sb->s_root); 3564b91c30aSAmir Goldstein break; 3574b91c30aSAmir Goldstein } 3584b91c30aSAmir Goldstein 3594b91c30aSAmir Goldstein /* 3604b91c30aSAmir Goldstein * If @real has been moved out of the layer root directory, 3614b91c30aSAmir Goldstein * we will eventully hit the real fs root. This cannot happen 3624b91c30aSAmir Goldstein * by legit overlay rename, so we return error in that case. 3634b91c30aSAmir Goldstein */ 3644b91c30aSAmir Goldstein if (parent == next) { 3654b91c30aSAmir Goldstein ancestor = ERR_PTR(-EXDEV); 3664b91c30aSAmir Goldstein break; 3674b91c30aSAmir Goldstein } 3684b91c30aSAmir Goldstein 3694b91c30aSAmir Goldstein dput(next); 3704b91c30aSAmir Goldstein next = parent; 3714b91c30aSAmir Goldstein } 3724b91c30aSAmir Goldstein 3734b91c30aSAmir Goldstein dput(parent); 3744b91c30aSAmir Goldstein dput(next); 3754b91c30aSAmir Goldstein 3764b91c30aSAmir Goldstein return ancestor; 3774b91c30aSAmir Goldstein } 3784b91c30aSAmir Goldstein 3794b91c30aSAmir Goldstein /* 3803985b70aSAmir Goldstein * Lookup a connected overlay dentry whose real dentry is @real. 3813985b70aSAmir Goldstein * If @real is on upper layer, we lookup a child overlay dentry with the same 3823985b70aSAmir Goldstein * path the real dentry. Otherwise, we need to consult index for lookup. 3833985b70aSAmir Goldstein */ 3843985b70aSAmir Goldstein static struct dentry *ovl_lookup_real(struct super_block *sb, 3853985b70aSAmir Goldstein struct dentry *real, 3863985b70aSAmir Goldstein struct ovl_layer *layer) 3873985b70aSAmir Goldstein { 3883985b70aSAmir Goldstein struct dentry *connected; 3893985b70aSAmir Goldstein int err = 0; 3903985b70aSAmir Goldstein 3914b91c30aSAmir Goldstein connected = ovl_lookup_real_ancestor(sb, real, layer); 3924b91c30aSAmir Goldstein if (IS_ERR(connected)) 3934b91c30aSAmir Goldstein return connected; 3943985b70aSAmir Goldstein 3953985b70aSAmir Goldstein while (!err) { 3963985b70aSAmir Goldstein struct dentry *next, *this; 3973985b70aSAmir Goldstein struct dentry *parent = NULL; 39898892516SAmir Goldstein struct dentry *real_connected = ovl_dentry_real_at(connected, 39998892516SAmir Goldstein layer->idx); 4003985b70aSAmir Goldstein 4013985b70aSAmir Goldstein if (real_connected == real) 4023985b70aSAmir Goldstein break; 4033985b70aSAmir Goldstein 4043985b70aSAmir Goldstein /* Find the topmost dentry not yet connected */ 4053985b70aSAmir Goldstein next = dget(real); 4063985b70aSAmir Goldstein for (;;) { 4073985b70aSAmir Goldstein parent = dget_parent(next); 4083985b70aSAmir Goldstein 4093985b70aSAmir Goldstein if (parent == real_connected) 4103985b70aSAmir Goldstein break; 4113985b70aSAmir Goldstein 4123985b70aSAmir Goldstein /* 4133985b70aSAmir Goldstein * If real has been moved out of 'real_connected', 4143985b70aSAmir Goldstein * we will not find 'real_connected' and hit the layer 4153985b70aSAmir Goldstein * root. In that case, we need to restart connecting. 4163985b70aSAmir Goldstein * This game can go on forever in the worst case. We 4173985b70aSAmir Goldstein * may want to consider taking s_vfs_rename_mutex if 4183985b70aSAmir Goldstein * this happens more than once. 4193985b70aSAmir Goldstein */ 4203985b70aSAmir Goldstein if (parent == layer->mnt->mnt_root) { 4213985b70aSAmir Goldstein dput(connected); 4223985b70aSAmir Goldstein connected = dget(sb->s_root); 4233985b70aSAmir Goldstein break; 4243985b70aSAmir Goldstein } 4253985b70aSAmir Goldstein 4263985b70aSAmir Goldstein /* 4273985b70aSAmir Goldstein * If real file has been moved out of the layer root 4283985b70aSAmir Goldstein * directory, we will eventully hit the real fs root. 4293985b70aSAmir Goldstein * This cannot happen by legit overlay rename, so we 4303985b70aSAmir Goldstein * return error in that case. 4313985b70aSAmir Goldstein */ 4323985b70aSAmir Goldstein if (parent == next) { 4333985b70aSAmir Goldstein err = -EXDEV; 4343985b70aSAmir Goldstein break; 4353985b70aSAmir Goldstein } 4363985b70aSAmir Goldstein 4373985b70aSAmir Goldstein dput(next); 4383985b70aSAmir Goldstein next = parent; 4393985b70aSAmir Goldstein } 4403985b70aSAmir Goldstein 4413985b70aSAmir Goldstein if (!err) { 4423985b70aSAmir Goldstein this = ovl_lookup_real_one(connected, next, layer); 4433985b70aSAmir Goldstein if (IS_ERR(this)) 4443985b70aSAmir Goldstein err = PTR_ERR(this); 4453985b70aSAmir Goldstein 4463985b70aSAmir Goldstein /* 4473985b70aSAmir Goldstein * Lookup of child in overlay can fail when racing with 4483985b70aSAmir Goldstein * overlay rename of child away from 'connected' parent. 4493985b70aSAmir Goldstein * In this case, we need to restart the lookup from the 4503985b70aSAmir Goldstein * top, because we cannot trust that 'real_connected' is 4514b91c30aSAmir Goldstein * still an ancestor of 'real'. There is a good chance 4524b91c30aSAmir Goldstein * that the renamed overlay ancestor is now in cache, so 4534b91c30aSAmir Goldstein * ovl_lookup_real_ancestor() will find it and we can 4544b91c30aSAmir Goldstein * continue to connect exactly from where lookup failed. 4553985b70aSAmir Goldstein */ 4563985b70aSAmir Goldstein if (err == -ECHILD) { 4574b91c30aSAmir Goldstein this = ovl_lookup_real_ancestor(sb, real, 4584b91c30aSAmir Goldstein layer); 4594b91c30aSAmir Goldstein err = IS_ERR(this) ? PTR_ERR(this) : 0; 4603985b70aSAmir Goldstein } 4613985b70aSAmir Goldstein if (!err) { 4623985b70aSAmir Goldstein dput(connected); 4633985b70aSAmir Goldstein connected = this; 4643985b70aSAmir Goldstein } 4653985b70aSAmir Goldstein } 4663985b70aSAmir Goldstein 4673985b70aSAmir Goldstein dput(parent); 4683985b70aSAmir Goldstein dput(next); 4693985b70aSAmir Goldstein } 4703985b70aSAmir Goldstein 4713985b70aSAmir Goldstein if (err) 4723985b70aSAmir Goldstein goto fail; 4733985b70aSAmir Goldstein 4743985b70aSAmir Goldstein return connected; 4753985b70aSAmir Goldstein 4763985b70aSAmir Goldstein fail: 4773985b70aSAmir Goldstein pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", 4783985b70aSAmir Goldstein real, layer->idx, connected, err); 4793985b70aSAmir Goldstein dput(connected); 4803985b70aSAmir Goldstein return ERR_PTR(err); 4813985b70aSAmir Goldstein } 4823985b70aSAmir Goldstein 4833985b70aSAmir Goldstein /* 484f71bd9cfSAmir Goldstein * Get an overlay dentry from upper/lower real dentries and index. 4853985b70aSAmir Goldstein */ 4863985b70aSAmir Goldstein static struct dentry *ovl_get_dentry(struct super_block *sb, 4873985b70aSAmir Goldstein struct dentry *upper, 488f71bd9cfSAmir Goldstein struct ovl_path *lowerpath, 489f71bd9cfSAmir Goldstein struct dentry *index) 4903985b70aSAmir Goldstein { 4913985b70aSAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 4923985b70aSAmir Goldstein struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt }; 49398892516SAmir Goldstein struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer; 494f71bd9cfSAmir Goldstein struct dentry *real = upper ?: (index ?: lowerpath->dentry); 4953985b70aSAmir Goldstein 496f941866fSAmir Goldstein /* 497f71bd9cfSAmir Goldstein * Obtain a disconnected overlay dentry from a non-dir real dentry 498f71bd9cfSAmir Goldstein * and index. 499f941866fSAmir Goldstein */ 500f71bd9cfSAmir Goldstein if (!d_is_dir(real)) 501f71bd9cfSAmir Goldstein return ovl_obtain_alias(sb, upper, lowerpath, index); 502f941866fSAmir Goldstein 5033985b70aSAmir Goldstein /* Removed empty directory? */ 50498892516SAmir Goldstein if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real)) 5053985b70aSAmir Goldstein return ERR_PTR(-ENOENT); 5063985b70aSAmir Goldstein 5073985b70aSAmir Goldstein /* 50898892516SAmir Goldstein * If real dentry is connected and hashed, get a connected overlay 50998892516SAmir Goldstein * dentry whose real dentry is @real. 5103985b70aSAmir Goldstein */ 51198892516SAmir Goldstein return ovl_lookup_real(sb, real, layer); 5123985b70aSAmir Goldstein } 5133985b70aSAmir Goldstein 5148556a420SAmir Goldstein static struct dentry *ovl_upper_fh_to_d(struct super_block *sb, 5158556a420SAmir Goldstein struct ovl_fh *fh) 5168556a420SAmir Goldstein { 5178556a420SAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 5188556a420SAmir Goldstein struct dentry *dentry; 5198556a420SAmir Goldstein struct dentry *upper; 5208556a420SAmir Goldstein 5218556a420SAmir Goldstein if (!ofs->upper_mnt) 5228556a420SAmir Goldstein return ERR_PTR(-EACCES); 5238556a420SAmir Goldstein 5248556a420SAmir Goldstein upper = ovl_decode_fh(fh, ofs->upper_mnt); 5258556a420SAmir Goldstein if (IS_ERR_OR_NULL(upper)) 5268556a420SAmir Goldstein return upper; 5278556a420SAmir Goldstein 528f71bd9cfSAmir Goldstein dentry = ovl_get_dentry(sb, upper, NULL, NULL); 5298556a420SAmir Goldstein dput(upper); 5308556a420SAmir Goldstein 5318556a420SAmir Goldstein return dentry; 5328556a420SAmir Goldstein } 5338556a420SAmir Goldstein 534f941866fSAmir Goldstein static struct dentry *ovl_lower_fh_to_d(struct super_block *sb, 535f941866fSAmir Goldstein struct ovl_fh *fh) 536f941866fSAmir Goldstein { 537f941866fSAmir Goldstein struct ovl_fs *ofs = sb->s_fs_info; 538f941866fSAmir Goldstein struct ovl_path origin = { }; 539f941866fSAmir Goldstein struct ovl_path *stack = &origin; 540f941866fSAmir Goldstein struct dentry *dentry = NULL; 541f71bd9cfSAmir Goldstein struct dentry *index = NULL; 5429436a1a3SAmir Goldstein struct inode *inode = NULL; 5439436a1a3SAmir Goldstein bool is_deleted = false; 544f941866fSAmir Goldstein int err; 545f941866fSAmir Goldstein 546f71bd9cfSAmir Goldstein /* First lookup indexed upper by fh */ 547f71bd9cfSAmir Goldstein if (ofs->indexdir) { 548f71bd9cfSAmir Goldstein index = ovl_get_index_fh(ofs, fh); 549f71bd9cfSAmir Goldstein err = PTR_ERR(index); 5509436a1a3SAmir Goldstein if (IS_ERR(index)) { 5519436a1a3SAmir Goldstein if (err != -ESTALE) 552f941866fSAmir Goldstein return ERR_PTR(err); 5539436a1a3SAmir Goldstein 5549436a1a3SAmir Goldstein /* Found a whiteout index - treat as deleted inode */ 5559436a1a3SAmir Goldstein is_deleted = true; 5569436a1a3SAmir Goldstein index = NULL; 5579436a1a3SAmir Goldstein } 558f71bd9cfSAmir Goldstein } 559f941866fSAmir Goldstein 5603b0bfc6eSAmir Goldstein /* Then try to get upper dir by index */ 5613b0bfc6eSAmir Goldstein if (index && d_is_dir(index)) { 5623b0bfc6eSAmir Goldstein struct dentry *upper = ovl_index_upper(ofs, index); 5633b0bfc6eSAmir Goldstein 5643b0bfc6eSAmir Goldstein err = PTR_ERR(upper); 5653b0bfc6eSAmir Goldstein if (IS_ERR_OR_NULL(upper)) 5663b0bfc6eSAmir Goldstein goto out_err; 5673b0bfc6eSAmir Goldstein 5683b0bfc6eSAmir Goldstein dentry = ovl_get_dentry(sb, upper, NULL, NULL); 5693b0bfc6eSAmir Goldstein dput(upper); 5703b0bfc6eSAmir Goldstein goto out; 5713b0bfc6eSAmir Goldstein } 5723b0bfc6eSAmir Goldstein 573f71bd9cfSAmir Goldstein /* Then lookup origin by fh */ 574f71bd9cfSAmir Goldstein err = ovl_check_origin_fh(ofs, fh, NULL, &stack); 575f71bd9cfSAmir Goldstein if (err) { 576f71bd9cfSAmir Goldstein goto out_err; 577f71bd9cfSAmir Goldstein } else if (index) { 578f71bd9cfSAmir Goldstein err = ovl_verify_origin(index, origin.dentry, false); 579f71bd9cfSAmir Goldstein if (err) 580f71bd9cfSAmir Goldstein goto out_err; 5819436a1a3SAmir Goldstein } else if (is_deleted) { 5829436a1a3SAmir Goldstein /* Lookup deleted non-dir by origin inode */ 5839436a1a3SAmir Goldstein if (!d_is_dir(origin.dentry)) 5844b91c30aSAmir Goldstein inode = ovl_lookup_inode(sb, origin.dentry, false); 5859436a1a3SAmir Goldstein err = -ESTALE; 5869436a1a3SAmir Goldstein if (!inode || atomic_read(&inode->i_count) == 1) 5879436a1a3SAmir Goldstein goto out_err; 5889436a1a3SAmir Goldstein 5899436a1a3SAmir Goldstein /* Deleted but still open? */ 5909436a1a3SAmir Goldstein index = dget(ovl_i_dentry_upper(inode)); 591f71bd9cfSAmir Goldstein } 592f71bd9cfSAmir Goldstein 593f71bd9cfSAmir Goldstein dentry = ovl_get_dentry(sb, NULL, &origin, index); 594f71bd9cfSAmir Goldstein 595f71bd9cfSAmir Goldstein out: 596f941866fSAmir Goldstein dput(origin.dentry); 597f71bd9cfSAmir Goldstein dput(index); 5989436a1a3SAmir Goldstein iput(inode); 599f941866fSAmir Goldstein return dentry; 600f71bd9cfSAmir Goldstein 601f71bd9cfSAmir Goldstein out_err: 602f71bd9cfSAmir Goldstein dentry = ERR_PTR(err); 603f71bd9cfSAmir Goldstein goto out; 604f941866fSAmir Goldstein } 605f941866fSAmir Goldstein 6068556a420SAmir Goldstein static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid, 6078556a420SAmir Goldstein int fh_len, int fh_type) 6088556a420SAmir Goldstein { 6098556a420SAmir Goldstein struct dentry *dentry = NULL; 6108556a420SAmir Goldstein struct ovl_fh *fh = (struct ovl_fh *) fid; 6118556a420SAmir Goldstein int len = fh_len << 2; 6128556a420SAmir Goldstein unsigned int flags = 0; 6138556a420SAmir Goldstein int err; 6148556a420SAmir Goldstein 6158556a420SAmir Goldstein err = -EINVAL; 6168556a420SAmir Goldstein if (fh_type != OVL_FILEID) 6178556a420SAmir Goldstein goto out_err; 6188556a420SAmir Goldstein 6198556a420SAmir Goldstein err = ovl_check_fh_len(fh, len); 6208556a420SAmir Goldstein if (err) 6218556a420SAmir Goldstein goto out_err; 6228556a420SAmir Goldstein 6238556a420SAmir Goldstein flags = fh->flags; 624f941866fSAmir Goldstein dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ? 625f941866fSAmir Goldstein ovl_upper_fh_to_d(sb, fh) : 626f941866fSAmir Goldstein ovl_lower_fh_to_d(sb, fh); 6278556a420SAmir Goldstein err = PTR_ERR(dentry); 6288556a420SAmir Goldstein if (IS_ERR(dentry) && err != -ESTALE) 6298556a420SAmir Goldstein goto out_err; 6308556a420SAmir Goldstein 6318556a420SAmir Goldstein return dentry; 6328556a420SAmir Goldstein 6338556a420SAmir Goldstein out_err: 6348556a420SAmir Goldstein pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n", 6358556a420SAmir Goldstein len, fh_type, flags, err); 6368556a420SAmir Goldstein return ERR_PTR(err); 6378556a420SAmir Goldstein } 6388556a420SAmir Goldstein 6393985b70aSAmir Goldstein static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid, 6403985b70aSAmir Goldstein int fh_len, int fh_type) 6413985b70aSAmir Goldstein { 6423985b70aSAmir Goldstein pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n"); 6433985b70aSAmir Goldstein return ERR_PTR(-EACCES); 6443985b70aSAmir Goldstein } 6453985b70aSAmir Goldstein 6463985b70aSAmir Goldstein static int ovl_get_name(struct dentry *parent, char *name, 6473985b70aSAmir Goldstein struct dentry *child) 6483985b70aSAmir Goldstein { 6493985b70aSAmir Goldstein /* 6503985b70aSAmir Goldstein * ovl_fh_to_dentry() returns connected dir overlay dentries and 6513985b70aSAmir Goldstein * ovl_fh_to_parent() is not implemented, so we should not get here. 6523985b70aSAmir Goldstein */ 6533985b70aSAmir Goldstein WARN_ON_ONCE(1); 6543985b70aSAmir Goldstein return -EIO; 6553985b70aSAmir Goldstein } 6563985b70aSAmir Goldstein 6573985b70aSAmir Goldstein static struct dentry *ovl_get_parent(struct dentry *dentry) 6583985b70aSAmir Goldstein { 6593985b70aSAmir Goldstein /* 6603985b70aSAmir Goldstein * ovl_fh_to_dentry() returns connected dir overlay dentries, so we 6613985b70aSAmir Goldstein * should not get here. 6623985b70aSAmir Goldstein */ 6633985b70aSAmir Goldstein WARN_ON_ONCE(1); 6643985b70aSAmir Goldstein return ERR_PTR(-EIO); 6653985b70aSAmir Goldstein } 6663985b70aSAmir Goldstein 6678ed5eec9SAmir Goldstein const struct export_operations ovl_export_operations = { 6688ed5eec9SAmir Goldstein .encode_fh = ovl_encode_inode_fh, 6698556a420SAmir Goldstein .fh_to_dentry = ovl_fh_to_dentry, 6703985b70aSAmir Goldstein .fh_to_parent = ovl_fh_to_parent, 6713985b70aSAmir Goldstein .get_name = ovl_get_name, 6723985b70aSAmir Goldstein .get_parent = ovl_get_parent, 6738ed5eec9SAmir Goldstein }; 674