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("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)
83 return ovl_numlower(oe);
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 ovl_lowerstack(oe)->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;
108 struct ovl_entry *oe = OVL_E(dentry);
112 if (WARN_ON(dentry == dentry->d_sb->s_root) ||
113 WARN_ON(!ovl_dentry_lower(dentry)))
116 origin_layer = ovl_lowerstack(oe)->layer->idx;
117 if (ovl_dentry_test_flag(OVL_E_CONNECTED, dentry))
120 /* Find the topmost origin layer connectable ancestor of @dentry */
123 parent = dget_parent(next);
124 if (WARN_ON(parent == next)) {
130 * If @parent is not origin layer connectable, then copy up
131 * @next which is origin layer connectable and we are done.
133 if (ovl_connectable_layer(parent) < origin_layer) {
134 err = ovl_encode_maybe_copy_up(next);
138 /* If @parent is connected or indexed we are done */
139 if (ovl_dentry_test_flag(OVL_E_CONNECTED, parent) ||
140 ovl_test_flag(OVL_INDEX, d_inode(parent)))
151 ovl_dentry_set_flag(OVL_E_CONNECTED, dentry);
153 return err ?: origin_layer;
157 * We only need to encode origin if there is a chance that the same object was
158 * encoded pre copy up and then we need to stay consistent with the same
159 * encoding also after copy up. If non-pure upper is not indexed, then it was
160 * copied up before NFS export was enabled. In that case we don't need to worry
161 * about staying consistent with pre copy up encoding and we encode an upper
162 * file handle. Overlay root dentry is a private case of non-indexed upper.
164 * The following table summarizes the different file handle encodings used for
165 * different overlay object types:
167 * Object type | Encoding
168 * --------------------------------
170 * Non-indexed upper | U
171 * Indexed upper | L (*)
174 * U = upper file handle
175 * L = lower file handle
177 * (*) Decoding a connected overlay dir from real lower dentry is not always
178 * possible when there are redirects in lower layers and non-indexed merge dirs.
179 * To mitigate those case, we may copy up the lower dir ancestor before encode
180 * of a decodable file handle for non-upper dir.
182 * Return 0 for upper file handle, > 0 for lower file handle or < 0 on error.
184 static int ovl_check_encode_origin(struct dentry *dentry)
186 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
187 bool decodable = ofs->config.nfs_export;
189 /* Lower file handle for non-upper non-decodable */
190 if (!ovl_dentry_upper(dentry) && !decodable)
193 /* Upper file handle for pure upper */
194 if (!ovl_dentry_lower(dentry))
198 * Root is never indexed, so if there's an upper layer, encode upper for
201 if (dentry == dentry->d_sb->s_root)
205 * Upper decodable file handle for non-indexed upper.
207 if (ovl_dentry_upper(dentry) && decodable &&
208 !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
212 * Decoding a merge dir, whose origin's ancestor is under a redirected
213 * lower dir or under a non-indexed upper is not always possible.
214 * ovl_connect_layer() will try to make origin's layer "connected" by
215 * copying up a "connectable" ancestor.
217 if (d_is_dir(dentry) && ovl_upper_mnt(ofs) && decodable)
218 return ovl_connect_layer(dentry);
220 /* Lower file handle for indexed and non-upper dir/non-dir */
224 static int ovl_dentry_to_fid(struct ovl_fs *ofs, struct dentry *dentry,
225 u32 *fid, int buflen)
227 struct ovl_fh *fh = NULL;
232 * Check if we should encode a lower or upper file handle and maybe
233 * copy up an ancestor to make lower file handle connectable.
235 err = enc_lower = ovl_check_encode_origin(dentry);
239 /* Encode an upper or lower file handle */
240 fh = ovl_encode_real_fh(ofs, enc_lower ? ovl_dentry_lower(dentry) :
241 ovl_dentry_upper(dentry), !enc_lower);
245 len = OVL_FH_LEN(fh);
247 memcpy(fid, fh, len);
255 pr_warn_ratelimited("failed to encode file handle (%pd2, err=%i)\n",
260 static int ovl_encode_fh(struct inode *inode, u32 *fid, int *max_len,
261 struct inode *parent)
263 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
264 struct dentry *dentry;
265 int bytes, buflen = *max_len << 2;
267 /* TODO: encode connectable file handles */
269 return FILEID_INVALID;
271 dentry = d_find_any_alias(inode);
273 return FILEID_INVALID;
275 bytes = ovl_dentry_to_fid(ofs, dentry, fid, buflen);
278 return FILEID_INVALID;
280 *max_len = bytes >> 2;
282 return FILEID_INVALID;
284 return OVL_FILEID_V1;
288 * Find or instantiate an overlay dentry from real dentries and index.
290 static struct dentry *ovl_obtain_alias(struct super_block *sb,
291 struct dentry *upper_alias,
292 struct ovl_path *lowerpath,
293 struct dentry *index)
295 struct dentry *lower = lowerpath ? lowerpath->dentry : NULL;
296 struct dentry *upper = upper_alias ?: index;
297 struct dentry *dentry;
298 struct inode *inode = NULL;
299 struct ovl_entry *oe;
300 struct ovl_inode_params oip = {
304 /* We get overlay directory dentries with ovl_lookup_real() */
305 if (d_is_dir(upper ?: lower))
306 return ERR_PTR(-EIO);
308 oe = ovl_alloc_entry(!!lower);
310 return ERR_PTR(-ENOMEM);
312 oip.upperdentry = dget(upper);
314 ovl_lowerstack(oe)->dentry = dget(lower);
315 ovl_lowerstack(oe)->layer = lowerpath->layer;
318 inode = ovl_get_inode(sb, &oip);
322 return ERR_CAST(inode);
326 ovl_set_flag(OVL_UPPERDATA, inode);
328 dentry = d_find_any_alias(inode);
332 dentry = d_alloc_anon(inode->i_sb);
333 if (unlikely(!dentry))
337 ovl_dentry_set_upper_alias(dentry);
339 ovl_dentry_init_reval(dentry, upper, OVL_I_E(inode));
341 return d_instantiate_anon(dentry, inode);
345 dentry = ERR_PTR(-ENOMEM);
351 /* Get the upper or lower dentry in stack whose on layer @idx */
352 static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
354 struct ovl_entry *oe = OVL_E(dentry);
355 struct ovl_path *lowerstack = ovl_lowerstack(oe);
359 return ovl_dentry_upper(dentry);
361 for (i = 0; i < ovl_numlower(oe); i++) {
362 if (lowerstack[i].layer->idx == idx)
363 return lowerstack[i].dentry;
370 * Lookup a child overlay dentry to get a connected overlay dentry whose real
371 * dentry is @real. If @real is on upper layer, we lookup a child overlay
372 * dentry with the same name as the real dentry. Otherwise, we need to consult
375 static struct dentry *ovl_lookup_real_one(struct dentry *connected,
377 const struct ovl_layer *layer)
379 struct inode *dir = d_inode(connected);
380 struct dentry *this, *parent = NULL;
381 struct name_snapshot name;
385 * Lookup child overlay dentry by real name. The dir mutex protects us
386 * from racing with overlay rename. If the overlay dentry that is above
387 * real has already been moved to a parent that is not under the
388 * connected overlay dir, we return -ECHILD and restart the lookup of
389 * connected real path from the top.
391 inode_lock_nested(dir, I_MUTEX_PARENT);
393 parent = dget_parent(real);
394 if (ovl_dentry_real_at(connected, layer->idx) != parent)
398 * We also need to take a snapshot of real dentry name to protect us
399 * from racing with underlying layer rename. In this case, we don't
400 * care about returning ESTALE, only from dereferencing a free name
401 * pointer because we hold no lock on the real dentry.
403 take_dentry_name_snapshot(&name, real);
405 * No idmap handling here: it's an internal lookup. Could skip
406 * permission checking altogether, but for now just use non-idmap
409 this = lookup_one_len(name.name.name, connected, name.name.len);
410 release_dentry_name_snapshot(&name);
414 } else if (!this || !this->d_inode) {
418 } else if (ovl_dentry_real_at(this, layer->idx) != real) {
430 pr_warn_ratelimited("failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
431 real, layer->idx, connected, err);
436 static struct dentry *ovl_lookup_real(struct super_block *sb,
438 const struct ovl_layer *layer);
441 * Lookup an indexed or hashed overlay dentry by real inode.
443 static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
445 const struct ovl_layer *layer)
447 struct ovl_fs *ofs = OVL_FS(sb);
448 struct dentry *index = NULL;
449 struct dentry *this = NULL;
453 * Decoding upper dir from index is expensive, so first try to lookup
454 * overlay dentry in inode/dcache.
456 inode = ovl_lookup_inode(sb, real, !layer->idx);
458 return ERR_CAST(inode);
460 this = d_find_any_alias(inode);
465 * For decoded lower dir file handle, lookup index by origin to check
466 * if lower dir was copied up and and/or removed.
468 if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) {
469 index = ovl_lookup_index(ofs, NULL, real, false);
474 /* Get connected upper overlay dir from index */
476 struct dentry *upper = ovl_index_upper(ofs, index, true);
479 if (IS_ERR_OR_NULL(upper))
483 * ovl_lookup_real() in lower layer may call recursively once to
484 * ovl_lookup_real() in upper layer. The first level call walks
485 * back lower parents to the topmost indexed parent. The second
486 * recursive call walks back from indexed upper to the topmost
487 * connected/hashed upper parent (or up to root).
489 this = ovl_lookup_real(sb, upper, &ofs->layers[0]);
493 if (IS_ERR_OR_NULL(this))
496 if (ovl_dentry_real_at(this, layer->idx) != real) {
498 this = ERR_PTR(-EIO);
505 * Lookup an indexed or hashed overlay dentry, whose real dentry is an
508 static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
510 const struct ovl_layer *layer)
512 struct dentry *next, *parent = NULL;
513 struct dentry *ancestor = ERR_PTR(-EIO);
515 if (real == layer->mnt->mnt_root)
516 return dget(sb->s_root);
518 /* Find the topmost indexed or hashed ancestor */
521 parent = dget_parent(next);
524 * Lookup a matching overlay dentry in inode/dentry
525 * cache or in index by real inode.
527 ancestor = ovl_lookup_real_inode(sb, next, layer);
531 if (parent == layer->mnt->mnt_root) {
532 ancestor = dget(sb->s_root);
537 * If @real has been moved out of the layer root directory,
538 * we will eventully hit the real fs root. This cannot happen
539 * by legit overlay rename, so we return error in that case.
541 if (parent == next) {
542 ancestor = ERR_PTR(-EXDEV);
557 * Lookup a connected overlay dentry whose real dentry is @real.
558 * If @real is on upper layer, we lookup a child overlay dentry with the same
559 * path the real dentry. Otherwise, we need to consult index for lookup.
561 static struct dentry *ovl_lookup_real(struct super_block *sb,
563 const struct ovl_layer *layer)
565 struct dentry *connected;
568 connected = ovl_lookup_real_ancestor(sb, real, layer);
569 if (IS_ERR(connected))
573 struct dentry *next, *this;
574 struct dentry *parent = NULL;
575 struct dentry *real_connected = ovl_dentry_real_at(connected,
578 if (real_connected == real)
581 /* Find the topmost dentry not yet connected */
584 parent = dget_parent(next);
586 if (parent == real_connected)
590 * If real has been moved out of 'real_connected',
591 * we will not find 'real_connected' and hit the layer
592 * root. In that case, we need to restart connecting.
593 * This game can go on forever in the worst case. We
594 * may want to consider taking s_vfs_rename_mutex if
595 * this happens more than once.
597 if (parent == layer->mnt->mnt_root) {
599 connected = dget(sb->s_root);
604 * If real file has been moved out of the layer root
605 * directory, we will eventully hit the real fs root.
606 * This cannot happen by legit overlay rename, so we
607 * return error in that case.
609 if (parent == next) {
619 this = ovl_lookup_real_one(connected, next, layer);
624 * Lookup of child in overlay can fail when racing with
625 * overlay rename of child away from 'connected' parent.
626 * In this case, we need to restart the lookup from the
627 * top, because we cannot trust that 'real_connected' is
628 * still an ancestor of 'real'. There is a good chance
629 * that the renamed overlay ancestor is now in cache, so
630 * ovl_lookup_real_ancestor() will find it and we can
631 * continue to connect exactly from where lookup failed.
633 if (err == -ECHILD) {
634 this = ovl_lookup_real_ancestor(sb, real,
636 err = PTR_ERR_OR_ZERO(this);
654 pr_warn_ratelimited("failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
655 real, layer->idx, connected, err);
661 * Get an overlay dentry from upper/lower real dentries and index.
663 static struct dentry *ovl_get_dentry(struct super_block *sb,
664 struct dentry *upper,
665 struct ovl_path *lowerpath,
666 struct dentry *index)
668 struct ovl_fs *ofs = OVL_FS(sb);
669 const struct ovl_layer *layer = upper ? &ofs->layers[0] : lowerpath->layer;
670 struct dentry *real = upper ?: (index ?: lowerpath->dentry);
673 * Obtain a disconnected overlay dentry from a non-dir real dentry
677 return ovl_obtain_alias(sb, upper, lowerpath, index);
679 /* Removed empty directory? */
680 if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
681 return ERR_PTR(-ENOENT);
684 * If real dentry is connected and hashed, get a connected overlay
685 * dentry whose real dentry is @real.
687 return ovl_lookup_real(sb, real, layer);
690 static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
693 struct ovl_fs *ofs = OVL_FS(sb);
694 struct dentry *dentry;
695 struct dentry *upper;
697 if (!ovl_upper_mnt(ofs))
698 return ERR_PTR(-EACCES);
700 upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), true);
701 if (IS_ERR_OR_NULL(upper))
704 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
710 static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
713 struct ovl_fs *ofs = OVL_FS(sb);
714 struct ovl_path origin = { };
715 struct ovl_path *stack = &origin;
716 struct dentry *dentry = NULL;
717 struct dentry *index = NULL;
721 /* First lookup overlay inode in inode cache by origin fh */
722 err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack);
726 if (!d_is_dir(origin.dentry) ||
727 !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
728 inode = ovl_lookup_inode(sb, origin.dentry, false);
729 err = PTR_ERR(inode);
733 dentry = d_find_any_alias(inode);
740 /* Then lookup indexed upper/whiteout by origin fh */
742 index = ovl_get_index_fh(ofs, fh);
743 err = PTR_ERR(index);
750 /* Then try to get a connected upper dir by index */
751 if (index && d_is_dir(index)) {
752 struct dentry *upper = ovl_index_upper(ofs, index, true);
754 err = PTR_ERR(upper);
755 if (IS_ERR_OR_NULL(upper))
758 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
763 /* Find origin.dentry again with ovl_acceptable() layer check */
764 if (d_is_dir(origin.dentry)) {
766 origin.dentry = NULL;
767 err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack);
772 err = ovl_verify_origin(ofs, index, origin.dentry, false);
777 /* Get a connected non-upper dir or disconnected non-dir */
778 dentry = ovl_get_dentry(sb, NULL, &origin, index);
786 dentry = ERR_PTR(err);
790 static struct ovl_fh *ovl_fid_to_fh(struct fid *fid, int buflen, int fh_type)
794 /* If on-wire inner fid is aligned - nothing to do */
795 if (fh_type == OVL_FILEID_V1)
796 return (struct ovl_fh *)fid;
798 if (fh_type != OVL_FILEID_V0)
799 return ERR_PTR(-EINVAL);
801 if (buflen <= OVL_FH_WIRE_OFFSET)
802 return ERR_PTR(-EINVAL);
804 fh = kzalloc(buflen, GFP_KERNEL);
806 return ERR_PTR(-ENOMEM);
808 /* Copy unaligned inner fh into aligned buffer */
809 memcpy(fh->buf, fid, buflen - OVL_FH_WIRE_OFFSET);
813 static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
814 int fh_len, int fh_type)
816 struct dentry *dentry = NULL;
817 struct ovl_fh *fh = NULL;
818 int len = fh_len << 2;
819 unsigned int flags = 0;
822 fh = ovl_fid_to_fh(fid, len, fh_type);
827 err = ovl_check_fh_len(fh, len);
831 flags = fh->fb.flags;
832 dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
833 ovl_upper_fh_to_d(sb, fh) :
834 ovl_lower_fh_to_d(sb, fh);
835 err = PTR_ERR(dentry);
836 if (IS_ERR(dentry) && err != -ESTALE)
840 /* We may have needed to re-align OVL_FILEID_V0 */
841 if (!IS_ERR_OR_NULL(fh) && fh != (void *)fid)
847 pr_warn_ratelimited("failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
848 fh_len, fh_type, flags, err);
849 dentry = ERR_PTR(err);
853 static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
854 int fh_len, int fh_type)
856 pr_warn_ratelimited("connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
857 return ERR_PTR(-EACCES);
860 static int ovl_get_name(struct dentry *parent, char *name,
861 struct dentry *child)
864 * ovl_fh_to_dentry() returns connected dir overlay dentries and
865 * ovl_fh_to_parent() is not implemented, so we should not get here.
871 static struct dentry *ovl_get_parent(struct dentry *dentry)
874 * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
875 * should not get here.
878 return ERR_PTR(-EIO);
881 const struct export_operations ovl_export_operations = {
882 .encode_fh = ovl_encode_fh,
883 .fh_to_dentry = ovl_fh_to_dentry,
884 .fh_to_parent = ovl_fh_to_parent,
885 .get_name = ovl_get_name,
886 .get_parent = ovl_get_parent,
889 /* encode_fh() encodes non-decodable file handles with nfs_export=off */
890 const struct export_operations ovl_export_fid_operations = {
891 .encode_fh = ovl_encode_fh,