1 // SPDX-License-Identifier: GPL-2.0-only
3 * Overlayfs NFS export support.
5 * Amir Goldstein <amir73il@gmail.com>
7 * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved.
11 #include <linux/cred.h>
12 #include <linux/mount.h>
13 #include <linux/namei.h>
14 #include <linux/xattr.h>
15 #include <linux/exportfs.h>
16 #include <linux/ratelimit.h>
17 #include "overlayfs.h"
19 static int ovl_encode_maybe_copy_up(struct dentry *dentry)
23 if (ovl_dentry_upper(dentry))
26 err = ovl_want_write(dentry);
28 err = ovl_copy_up(dentry);
29 ovl_drop_write(dentry);
33 pr_warn_ratelimited("overlayfs: failed to copy up on encode (%pd2, err=%i)\n",
41 * Before encoding a non-upper directory file handle from real layer N, we need
42 * to check if it will be possible to reconnect an overlay dentry from the real
43 * lower decoded dentry. This is done by following the overlay ancestry up to a
44 * "layer N connected" ancestor and verifying that all parents along the way are
45 * "layer N connectable". If an ancestor that is NOT "layer N connectable" is
46 * found, we need to copy up an ancestor, which is "layer N connectable", thus
47 * making that ancestor "layer N connected". For example:
52 * The overlay dentry /a is NOT "layer 2 connectable", because if dir /a is
53 * copied up and renamed, upper dir /a will be indexed by lower dir /a from
54 * layer 1. The dir /a from layer 2 will never be indexed, so the algorithm (*)
55 * in ovl_lookup_real_ancestor() will not be able to lookup a connected overlay
56 * dentry from the connected lower dentry /a/b/c.
58 * To avoid this problem on decode time, we need to copy up an ancestor of
59 * /a/b/c, which is "layer 2 connectable", on encode time. That ancestor is
60 * /a/b. After copy up (and index) of /a/b, it will become "layer 2 connected"
61 * and when the time comes to decode the file handle from lower dentry /a/b/c,
62 * ovl_lookup_real_ancestor() will find the indexed ancestor /a/b and decoding
63 * a connected overlay dentry will be accomplished.
65 * (*) the algorithm in ovl_lookup_real_ancestor() can be improved to lookup an
66 * entry /a in the lower layers above layer N and find the indexed dir /a from
67 * layer 1. If that improvement is made, then the check for "layer N connected"
68 * will need to verify there are no redirects in lower layers above N. In the
69 * example above, /a will be "layer 2 connectable". However, if layer 2 dir /a
70 * is a target of a layer 1 redirect, then /a will NOT be "layer 2 connectable":
72 * layer 1: /A (redirect = /a)
76 /* Return the lowest layer for encoding a connectable file handle */
77 static int ovl_connectable_layer(struct dentry *dentry)
79 struct ovl_entry *oe = OVL_E(dentry);
81 /* We can get overlay root from root of any layer */
82 if (dentry == dentry->d_sb->s_root)
86 * If it's an unindexed merge dir, then it's not connectable with any
89 if (ovl_dentry_upper(dentry) &&
90 !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
93 /* We can get upper/overlay path from indexed/lower dentry */
94 return oe->lowerstack[0].layer->idx;
98 * @dentry is "connected" if all ancestors up to root or a "connected" ancestor
99 * have the same uppermost lower layer as the origin's layer. We may need to
100 * copy up a "connectable" ancestor to make it "connected". A "connected" dentry
101 * cannot become non "connected", so cache positive result in dentry flags.
103 * Return the connected origin layer or < 0 on error.
105 static int ovl_connect_layer(struct dentry *dentry)
107 struct dentry *next, *parent = NULL;
111 if (WARN_ON(dentry == dentry->d_sb->s_root) ||
112 WARN_ON(!ovl_dentry_lower(dentry)))
115 origin_layer = OVL_E(dentry)->lowerstack[0].layer->idx;
116 if (ovl_dentry_test_flag(OVL_E_CONNECTED, dentry))
119 /* Find the topmost origin layer connectable ancestor of @dentry */
122 parent = dget_parent(next);
123 if (WARN_ON(parent == next)) {
129 * If @parent is not origin layer connectable, then copy up
130 * @next which is origin layer connectable and we are done.
132 if (ovl_connectable_layer(parent) < origin_layer) {
133 err = ovl_encode_maybe_copy_up(next);
137 /* If @parent is connected or indexed we are done */
138 if (ovl_dentry_test_flag(OVL_E_CONNECTED, parent) ||
139 ovl_test_flag(OVL_INDEX, d_inode(parent)))
150 ovl_dentry_set_flag(OVL_E_CONNECTED, dentry);
152 return err ?: origin_layer;
156 * We only need to encode origin if there is a chance that the same object was
157 * encoded pre copy up and then we need to stay consistent with the same
158 * encoding also after copy up. If non-pure upper is not indexed, then it was
159 * copied up before NFS export was enabled. In that case we don't need to worry
160 * about staying consistent with pre copy up encoding and we encode an upper
161 * file handle. Overlay root dentry is a private case of non-indexed upper.
163 * The following table summarizes the different file handle encodings used for
164 * different overlay object types:
166 * Object type | Encoding
167 * --------------------------------
169 * Non-indexed upper | U
170 * Indexed upper | L (*)
173 * U = upper file handle
174 * L = lower file handle
176 * (*) Connecting an overlay dir from real lower dentry is not always
177 * possible when there are redirects in lower layers and non-indexed merge dirs.
178 * To mitigate those case, we may copy up the lower dir ancestor before encode
179 * a lower dir file handle.
181 * Return 0 for upper file handle, > 0 for lower file handle or < 0 on error.
183 static int ovl_check_encode_origin(struct dentry *dentry)
185 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
187 /* Upper file handle for pure upper */
188 if (!ovl_dentry_lower(dentry))
192 * Upper file handle for non-indexed upper.
194 * Root is never indexed, so if there's an upper layer, encode upper for
197 if (ovl_dentry_upper(dentry) &&
198 !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
202 * Decoding a merge dir, whose origin's ancestor is under a redirected
203 * lower dir or under a non-indexed upper is not always possible.
204 * ovl_connect_layer() will try to make origin's layer "connected" by
205 * copying up a "connectable" ancestor.
207 if (d_is_dir(dentry) && ofs->upper_mnt)
208 return ovl_connect_layer(dentry);
210 /* Lower file handle for indexed and non-upper dir/non-dir */
214 static int ovl_dentry_to_fid(struct dentry *dentry, u32 *fid, int buflen)
216 struct ovl_fh *fh = NULL;
221 * Check if we should encode a lower or upper file handle and maybe
222 * copy up an ancestor to make lower file handle connectable.
224 err = enc_lower = ovl_check_encode_origin(dentry);
228 /* Encode an upper or lower file handle */
229 fh = ovl_encode_real_fh(enc_lower ? ovl_dentry_lower(dentry) :
230 ovl_dentry_upper(dentry), !enc_lower);
235 len = OVL_FH_LEN(fh);
239 memcpy(fid, fh, len);
247 pr_warn_ratelimited("overlayfs: failed to encode file handle (%pd2, err=%i, buflen=%d, len=%d, type=%d)\n",
248 dentry, err, buflen, fh ? (int)fh->fb.len : 0,
249 fh ? fh->fb.type : 0);
253 static int ovl_encode_fh(struct inode *inode, u32 *fid, int *max_len,
254 struct inode *parent)
256 struct dentry *dentry;
257 int bytes = *max_len << 2;
259 /* TODO: encode connectable file handles */
261 return FILEID_INVALID;
263 dentry = d_find_any_alias(inode);
264 if (WARN_ON(!dentry))
265 return FILEID_INVALID;
267 bytes = ovl_dentry_to_fid(dentry, fid, bytes);
270 return FILEID_INVALID;
272 *max_len = bytes >> 2;
274 return OVL_FILEID_V1;
278 * Find or instantiate an overlay dentry from real dentries and index.
280 static struct dentry *ovl_obtain_alias(struct super_block *sb,
281 struct dentry *upper_alias,
282 struct ovl_path *lowerpath,
283 struct dentry *index)
285 struct dentry *lower = lowerpath ? lowerpath->dentry : NULL;
286 struct dentry *upper = upper_alias ?: index;
287 struct dentry *dentry;
289 struct ovl_entry *oe;
290 struct ovl_inode_params oip = {
291 .lowerpath = lowerpath,
296 /* We get overlay directory dentries with ovl_lookup_real() */
297 if (d_is_dir(upper ?: lower))
298 return ERR_PTR(-EIO);
300 oip.upperdentry = dget(upper);
301 inode = ovl_get_inode(sb, &oip);
304 return ERR_CAST(inode);
308 ovl_set_flag(OVL_UPPERDATA, inode);
310 dentry = d_find_any_alias(inode);
312 dentry = d_alloc_anon(inode->i_sb);
315 oe = ovl_alloc_entry(lower ? 1 : 0);
320 oe->lowerstack->dentry = dget(lower);
321 oe->lowerstack->layer = lowerpath->layer;
323 dentry->d_fsdata = oe;
325 ovl_dentry_set_upper_alias(dentry);
328 return d_instantiate_anon(dentry, inode);
333 return ERR_PTR(-ENOMEM);
336 /* Get the upper or lower dentry in stach whose on layer @idx */
337 static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
339 struct ovl_entry *oe = dentry->d_fsdata;
343 return ovl_dentry_upper(dentry);
345 for (i = 0; i < oe->numlower; i++) {
346 if (oe->lowerstack[i].layer->idx == idx)
347 return oe->lowerstack[i].dentry;
354 * Lookup a child overlay dentry to get a connected overlay dentry whose real
355 * dentry is @real. If @real is on upper layer, we lookup a child overlay
356 * dentry with the same name as the real dentry. Otherwise, we need to consult
359 static struct dentry *ovl_lookup_real_one(struct dentry *connected,
361 struct ovl_layer *layer)
363 struct inode *dir = d_inode(connected);
364 struct dentry *this, *parent = NULL;
365 struct name_snapshot name;
369 * Lookup child overlay dentry by real name. The dir mutex protects us
370 * from racing with overlay rename. If the overlay dentry that is above
371 * real has already been moved to a parent that is not under the
372 * connected overlay dir, we return -ECHILD and restart the lookup of
373 * connected real path from the top.
375 inode_lock_nested(dir, I_MUTEX_PARENT);
377 parent = dget_parent(real);
378 if (ovl_dentry_real_at(connected, layer->idx) != parent)
382 * We also need to take a snapshot of real dentry name to protect us
383 * from racing with underlying layer rename. In this case, we don't
384 * care about returning ESTALE, only from dereferencing a free name
385 * pointer because we hold no lock on the real dentry.
387 take_dentry_name_snapshot(&name, real);
388 this = lookup_one_len(name.name.name, connected, name.name.len);
392 } else if (!this || !this->d_inode) {
396 } else if (ovl_dentry_real_at(this, layer->idx) != real) {
403 release_dentry_name_snapshot(&name);
409 pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
410 real, layer->idx, connected, err);
415 static struct dentry *ovl_lookup_real(struct super_block *sb,
417 struct ovl_layer *layer);
420 * Lookup an indexed or hashed overlay dentry by real inode.
422 static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
424 struct ovl_layer *layer)
426 struct ovl_fs *ofs = sb->s_fs_info;
427 struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
428 struct dentry *index = NULL;
429 struct dentry *this = NULL;
433 * Decoding upper dir from index is expensive, so first try to lookup
434 * overlay dentry in inode/dcache.
436 inode = ovl_lookup_inode(sb, real, !layer->idx);
438 return ERR_CAST(inode);
440 this = d_find_any_alias(inode);
445 * For decoded lower dir file handle, lookup index by origin to check
446 * if lower dir was copied up and and/or removed.
448 if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) {
449 index = ovl_lookup_index(ofs, NULL, real, false);
454 /* Get connected upper overlay dir from index */
456 struct dentry *upper = ovl_index_upper(ofs, index);
459 if (IS_ERR_OR_NULL(upper))
463 * ovl_lookup_real() in lower layer may call recursively once to
464 * ovl_lookup_real() in upper layer. The first level call walks
465 * back lower parents to the topmost indexed parent. The second
466 * recursive call walks back from indexed upper to the topmost
467 * connected/hashed upper parent (or up to root).
469 this = ovl_lookup_real(sb, upper, &upper_layer);
473 if (IS_ERR_OR_NULL(this))
476 if (WARN_ON(ovl_dentry_real_at(this, layer->idx) != real)) {
478 this = ERR_PTR(-EIO);
485 * Lookup an indexed or hashed overlay dentry, whose real dentry is an
488 static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
490 struct ovl_layer *layer)
492 struct dentry *next, *parent = NULL;
493 struct dentry *ancestor = ERR_PTR(-EIO);
495 if (real == layer->mnt->mnt_root)
496 return dget(sb->s_root);
498 /* Find the topmost indexed or hashed ancestor */
501 parent = dget_parent(next);
504 * Lookup a matching overlay dentry in inode/dentry
505 * cache or in index by real inode.
507 ancestor = ovl_lookup_real_inode(sb, next, layer);
511 if (parent == layer->mnt->mnt_root) {
512 ancestor = dget(sb->s_root);
517 * If @real has been moved out of the layer root directory,
518 * we will eventully hit the real fs root. This cannot happen
519 * by legit overlay rename, so we return error in that case.
521 if (parent == next) {
522 ancestor = ERR_PTR(-EXDEV);
537 * Lookup a connected overlay dentry whose real dentry is @real.
538 * If @real is on upper layer, we lookup a child overlay dentry with the same
539 * path the real dentry. Otherwise, we need to consult index for lookup.
541 static struct dentry *ovl_lookup_real(struct super_block *sb,
543 struct ovl_layer *layer)
545 struct dentry *connected;
548 connected = ovl_lookup_real_ancestor(sb, real, layer);
549 if (IS_ERR(connected))
553 struct dentry *next, *this;
554 struct dentry *parent = NULL;
555 struct dentry *real_connected = ovl_dentry_real_at(connected,
558 if (real_connected == real)
561 /* Find the topmost dentry not yet connected */
564 parent = dget_parent(next);
566 if (parent == real_connected)
570 * If real has been moved out of 'real_connected',
571 * we will not find 'real_connected' and hit the layer
572 * root. In that case, we need to restart connecting.
573 * This game can go on forever in the worst case. We
574 * may want to consider taking s_vfs_rename_mutex if
575 * this happens more than once.
577 if (parent == layer->mnt->mnt_root) {
579 connected = dget(sb->s_root);
584 * If real file has been moved out of the layer root
585 * directory, we will eventully hit the real fs root.
586 * This cannot happen by legit overlay rename, so we
587 * return error in that case.
589 if (parent == next) {
599 this = ovl_lookup_real_one(connected, next, layer);
604 * Lookup of child in overlay can fail when racing with
605 * overlay rename of child away from 'connected' parent.
606 * In this case, we need to restart the lookup from the
607 * top, because we cannot trust that 'real_connected' is
608 * still an ancestor of 'real'. There is a good chance
609 * that the renamed overlay ancestor is now in cache, so
610 * ovl_lookup_real_ancestor() will find it and we can
611 * continue to connect exactly from where lookup failed.
613 if (err == -ECHILD) {
614 this = ovl_lookup_real_ancestor(sb, real,
616 err = PTR_ERR_OR_ZERO(this);
634 pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
635 real, layer->idx, connected, err);
641 * Get an overlay dentry from upper/lower real dentries and index.
643 static struct dentry *ovl_get_dentry(struct super_block *sb,
644 struct dentry *upper,
645 struct ovl_path *lowerpath,
646 struct dentry *index)
648 struct ovl_fs *ofs = sb->s_fs_info;
649 struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
650 struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer;
651 struct dentry *real = upper ?: (index ?: lowerpath->dentry);
654 * Obtain a disconnected overlay dentry from a non-dir real dentry
658 return ovl_obtain_alias(sb, upper, lowerpath, index);
660 /* Removed empty directory? */
661 if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
662 return ERR_PTR(-ENOENT);
665 * If real dentry is connected and hashed, get a connected overlay
666 * dentry whose real dentry is @real.
668 return ovl_lookup_real(sb, real, layer);
671 static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
674 struct ovl_fs *ofs = sb->s_fs_info;
675 struct dentry *dentry;
676 struct dentry *upper;
679 return ERR_PTR(-EACCES);
681 upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
682 if (IS_ERR_OR_NULL(upper))
685 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
691 static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
694 struct ovl_fs *ofs = sb->s_fs_info;
695 struct ovl_path origin = { };
696 struct ovl_path *stack = &origin;
697 struct dentry *dentry = NULL;
698 struct dentry *index = NULL;
702 /* First lookup overlay inode in inode cache by origin fh */
703 err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack);
707 if (!d_is_dir(origin.dentry) ||
708 !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
709 inode = ovl_lookup_inode(sb, origin.dentry, false);
710 err = PTR_ERR(inode);
714 dentry = d_find_any_alias(inode);
721 /* Then lookup indexed upper/whiteout by origin fh */
723 index = ovl_get_index_fh(ofs, fh);
724 err = PTR_ERR(index);
731 /* Then try to get a connected upper dir by index */
732 if (index && d_is_dir(index)) {
733 struct dentry *upper = ovl_index_upper(ofs, index);
735 err = PTR_ERR(upper);
736 if (IS_ERR_OR_NULL(upper))
739 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
744 /* Find origin.dentry again with ovl_acceptable() layer check */
745 if (d_is_dir(origin.dentry)) {
747 origin.dentry = NULL;
748 err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack);
753 err = ovl_verify_origin(index, origin.dentry, false);
758 /* Get a connected non-upper dir or disconnected non-dir */
759 dentry = ovl_get_dentry(sb, NULL, &origin, index);
767 dentry = ERR_PTR(err);
771 static struct ovl_fh *ovl_fid_to_fh(struct fid *fid, int buflen, int fh_type)
775 /* If on-wire inner fid is aligned - nothing to do */
776 if (fh_type == OVL_FILEID_V1)
777 return (struct ovl_fh *)fid;
779 if (fh_type != OVL_FILEID_V0)
780 return ERR_PTR(-EINVAL);
782 fh = kzalloc(buflen, GFP_KERNEL);
784 return ERR_PTR(-ENOMEM);
786 /* Copy unaligned inner fh into aligned buffer */
787 memcpy(&fh->fb, fid, buflen - OVL_FH_WIRE_OFFSET);
791 static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
792 int fh_len, int fh_type)
794 struct dentry *dentry = NULL;
795 struct ovl_fh *fh = NULL;
796 int len = fh_len << 2;
797 unsigned int flags = 0;
800 fh = ovl_fid_to_fh(fid, len, fh_type);
805 err = ovl_check_fh_len(fh, len);
809 flags = fh->fb.flags;
810 dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
811 ovl_upper_fh_to_d(sb, fh) :
812 ovl_lower_fh_to_d(sb, fh);
813 err = PTR_ERR(dentry);
814 if (IS_ERR(dentry) && err != -ESTALE)
818 /* We may have needed to re-align OVL_FILEID_V0 */
819 if (!IS_ERR_OR_NULL(fh) && fh != (void *)fid)
825 pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
826 fh_len, fh_type, flags, err);
827 dentry = ERR_PTR(err);
831 static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
832 int fh_len, int fh_type)
834 pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
835 return ERR_PTR(-EACCES);
838 static int ovl_get_name(struct dentry *parent, char *name,
839 struct dentry *child)
842 * ovl_fh_to_dentry() returns connected dir overlay dentries and
843 * ovl_fh_to_parent() is not implemented, so we should not get here.
849 static struct dentry *ovl_get_parent(struct dentry *dentry)
852 * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
853 * should not get here.
856 return ERR_PTR(-EIO);
859 const struct export_operations ovl_export_operations = {
860 .encode_fh = ovl_encode_fh,
861 .fh_to_dentry = ovl_fh_to_dentry,
862 .fh_to_parent = ovl_fh_to_parent,
863 .get_name = ovl_get_name,
864 .get_parent = ovl_get_parent,