2 * Overlayfs NFS export support.
4 * Amir Goldstein <amir73il@gmail.com>
6 * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
14 #include <linux/cred.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/xattr.h>
18 #include <linux/exportfs.h>
19 #include <linux/ratelimit.h>
20 #include "overlayfs.h"
23 * We only need to encode origin if there is a chance that the same object was
24 * encoded pre copy up and then we need to stay consistent with the same
25 * encoding also after copy up. If non-pure upper is not indexed, then it was
26 * copied up before NFS export was enabled. In that case we don't need to worry
27 * about staying consistent with pre copy up encoding and we encode an upper
28 * file handle. Overlay root dentry is a private case of non-indexed upper.
30 * The following table summarizes the different file handle encodings used for
31 * different overlay object types:
33 * Object type | Encoding
34 * --------------------------------
36 * Non-indexed upper | U
37 * Indexed upper | L (*)
40 * U = upper file handle
41 * L = lower file handle
43 * (*) Connecting an overlay dir from real lower dentry is not always
44 * possible when there are redirects in lower layers. To mitigate this case,
45 * we copy up the lower dir first and then encode an upper dir file handle.
47 static bool ovl_should_encode_origin(struct dentry *dentry)
49 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
51 if (!ovl_dentry_lower(dentry))
55 * Decoding a merge dir, whose origin's parent is under a redirected
56 * lower dir is not always possible. As a simple aproximation, we do
57 * not encode lower dir file handles when overlay has multiple lower
58 * layers and origin is below the topmost lower layer.
60 * TODO: copy up only the parent that is under redirected lower.
62 if (d_is_dir(dentry) && ofs->upper_mnt &&
63 OVL_E(dentry)->lowerstack[0].layer->idx > 1)
66 /* Decoding a non-indexed upper from origin is not implemented */
67 if (ovl_dentry_upper(dentry) &&
68 !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
74 static int ovl_encode_maybe_copy_up(struct dentry *dentry)
78 if (ovl_dentry_upper(dentry))
81 err = ovl_want_write(dentry);
85 err = ovl_copy_up(dentry);
87 ovl_drop_write(dentry);
91 static int ovl_d_to_fh(struct dentry *dentry, char *buf, int buflen)
93 struct dentry *origin = ovl_dentry_lower(dentry);
94 struct ovl_fh *fh = NULL;
98 * If we should not encode a lower dir file handle, copy up and encode
99 * an upper dir file handle.
101 if (!ovl_should_encode_origin(dentry)) {
102 err = ovl_encode_maybe_copy_up(dentry);
109 /* Encode an upper or origin file handle */
110 fh = ovl_encode_fh(origin ?: ovl_dentry_upper(dentry), !origin);
113 if (fh->len > buflen)
116 memcpy(buf, (char *)fh, fh->len);
124 pr_warn_ratelimited("overlayfs: failed to encode file handle (%pd2, err=%i, buflen=%d, len=%d, type=%d)\n",
125 dentry, err, buflen, fh ? (int)fh->len : 0,
130 static int ovl_dentry_to_fh(struct dentry *dentry, u32 *fid, int *max_len)
132 int res, len = *max_len << 2;
134 res = ovl_d_to_fh(dentry, (char *)fid, len);
136 return FILEID_INVALID;
140 /* Round up to dwords */
141 *max_len = (len + 3) >> 2;
145 static int ovl_encode_inode_fh(struct inode *inode, u32 *fid, int *max_len,
146 struct inode *parent)
148 struct dentry *dentry;
151 /* TODO: encode connectable file handles */
153 return FILEID_INVALID;
155 dentry = d_find_any_alias(inode);
156 if (WARN_ON(!dentry))
157 return FILEID_INVALID;
159 type = ovl_dentry_to_fh(dentry, fid, max_len);
166 * Find or instantiate an overlay dentry from real dentries and index.
168 static struct dentry *ovl_obtain_alias(struct super_block *sb,
169 struct dentry *upper_alias,
170 struct ovl_path *lowerpath,
171 struct dentry *index)
173 struct dentry *lower = lowerpath ? lowerpath->dentry : NULL;
174 struct dentry *upper = upper_alias ?: index;
175 struct dentry *dentry;
177 struct ovl_entry *oe;
179 /* We get overlay directory dentries with ovl_lookup_real() */
180 if (d_is_dir(upper ?: lower))
181 return ERR_PTR(-EIO);
183 inode = ovl_get_inode(sb, dget(upper), lower, index, !!lower);
186 return ERR_CAST(inode);
190 ovl_set_flag(OVL_INDEX, inode);
192 dentry = d_find_any_alias(inode);
194 dentry = d_alloc_anon(inode->i_sb);
197 oe = ovl_alloc_entry(lower ? 1 : 0);
202 oe->lowerstack->dentry = dget(lower);
203 oe->lowerstack->layer = lowerpath->layer;
205 dentry->d_fsdata = oe;
207 ovl_dentry_set_upper_alias(dentry);
210 return d_instantiate_anon(dentry, inode);
215 return ERR_PTR(-ENOMEM);
218 /* Get the upper or lower dentry in stach whose on layer @idx */
219 static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
221 struct ovl_entry *oe = dentry->d_fsdata;
225 return ovl_dentry_upper(dentry);
227 for (i = 0; i < oe->numlower; i++) {
228 if (oe->lowerstack[i].layer->idx == idx)
229 return oe->lowerstack[i].dentry;
236 * Lookup a child overlay dentry to get a connected overlay dentry whose real
237 * dentry is @real. If @real is on upper layer, we lookup a child overlay
238 * dentry with the same name as the real dentry. Otherwise, we need to consult
241 static struct dentry *ovl_lookup_real_one(struct dentry *connected,
243 struct ovl_layer *layer)
245 struct inode *dir = d_inode(connected);
246 struct dentry *this, *parent = NULL;
247 struct name_snapshot name;
251 * Lookup child overlay dentry by real name. The dir mutex protects us
252 * from racing with overlay rename. If the overlay dentry that is above
253 * real has already been moved to a parent that is not under the
254 * connected overlay dir, we return -ECHILD and restart the lookup of
255 * connected real path from the top.
257 inode_lock_nested(dir, I_MUTEX_PARENT);
259 parent = dget_parent(real);
260 if (ovl_dentry_real_at(connected, layer->idx) != parent)
264 * We also need to take a snapshot of real dentry name to protect us
265 * from racing with underlying layer rename. In this case, we don't
266 * care about returning ESTALE, only from dereferencing a free name
267 * pointer because we hold no lock on the real dentry.
269 take_dentry_name_snapshot(&name, real);
270 this = lookup_one_len(name.name, connected, strlen(name.name));
274 } else if (!this || !this->d_inode) {
278 } else if (ovl_dentry_real_at(this, layer->idx) != real) {
285 release_dentry_name_snapshot(&name);
291 pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
292 real, layer->idx, connected, err);
298 * Lookup an indexed or hashed overlay dentry by real inode.
300 static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
302 struct ovl_layer *layer)
304 struct dentry *this = NULL;
307 inode = ovl_lookup_inode(sb, real, !layer->idx);
309 return ERR_CAST(inode);
311 this = d_find_any_alias(inode);
315 /* TODO: use index when looking up by origin inode */
319 if (WARN_ON(ovl_dentry_real_at(this, layer->idx) != real)) {
321 this = ERR_PTR(-EIO);
328 * Lookup an indexed or hashed overlay dentry, whose real dentry is an
331 static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
333 struct ovl_layer *layer)
335 struct dentry *next, *parent = NULL;
336 struct dentry *ancestor = ERR_PTR(-EIO);
338 if (real == layer->mnt->mnt_root)
339 return dget(sb->s_root);
341 /* Find the topmost indexed or hashed ancestor */
344 parent = dget_parent(next);
347 * Lookup a matching overlay dentry in inode/dentry
348 * cache or in index by real inode.
350 ancestor = ovl_lookup_real_inode(sb, next, layer);
354 if (parent == layer->mnt->mnt_root) {
355 ancestor = dget(sb->s_root);
360 * If @real has been moved out of the layer root directory,
361 * we will eventully hit the real fs root. This cannot happen
362 * by legit overlay rename, so we return error in that case.
364 if (parent == next) {
365 ancestor = ERR_PTR(-EXDEV);
380 * Lookup a connected overlay dentry whose real dentry is @real.
381 * If @real is on upper layer, we lookup a child overlay dentry with the same
382 * path the real dentry. Otherwise, we need to consult index for lookup.
384 static struct dentry *ovl_lookup_real(struct super_block *sb,
386 struct ovl_layer *layer)
388 struct dentry *connected;
391 connected = ovl_lookup_real_ancestor(sb, real, layer);
392 if (IS_ERR(connected))
396 struct dentry *next, *this;
397 struct dentry *parent = NULL;
398 struct dentry *real_connected = ovl_dentry_real_at(connected,
401 if (real_connected == real)
404 /* Find the topmost dentry not yet connected */
407 parent = dget_parent(next);
409 if (parent == real_connected)
413 * If real has been moved out of 'real_connected',
414 * we will not find 'real_connected' and hit the layer
415 * root. In that case, we need to restart connecting.
416 * This game can go on forever in the worst case. We
417 * may want to consider taking s_vfs_rename_mutex if
418 * this happens more than once.
420 if (parent == layer->mnt->mnt_root) {
422 connected = dget(sb->s_root);
427 * If real file has been moved out of the layer root
428 * directory, we will eventully hit the real fs root.
429 * This cannot happen by legit overlay rename, so we
430 * return error in that case.
432 if (parent == next) {
442 this = ovl_lookup_real_one(connected, next, layer);
447 * Lookup of child in overlay can fail when racing with
448 * overlay rename of child away from 'connected' parent.
449 * In this case, we need to restart the lookup from the
450 * top, because we cannot trust that 'real_connected' is
451 * still an ancestor of 'real'. There is a good chance
452 * that the renamed overlay ancestor is now in cache, so
453 * ovl_lookup_real_ancestor() will find it and we can
454 * continue to connect exactly from where lookup failed.
456 if (err == -ECHILD) {
457 this = ovl_lookup_real_ancestor(sb, real,
459 err = IS_ERR(this) ? PTR_ERR(this) : 0;
477 pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
478 real, layer->idx, connected, err);
484 * Get an overlay dentry from upper/lower real dentries and index.
486 static struct dentry *ovl_get_dentry(struct super_block *sb,
487 struct dentry *upper,
488 struct ovl_path *lowerpath,
489 struct dentry *index)
491 struct ovl_fs *ofs = sb->s_fs_info;
492 struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
493 struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer;
494 struct dentry *real = upper ?: (index ?: lowerpath->dentry);
497 * Obtain a disconnected overlay dentry from a non-dir real dentry
501 return ovl_obtain_alias(sb, upper, lowerpath, index);
503 /* Removed empty directory? */
504 if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
505 return ERR_PTR(-ENOENT);
508 * If real dentry is connected and hashed, get a connected overlay
509 * dentry whose real dentry is @real.
511 return ovl_lookup_real(sb, real, layer);
514 static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
517 struct ovl_fs *ofs = sb->s_fs_info;
518 struct dentry *dentry;
519 struct dentry *upper;
522 return ERR_PTR(-EACCES);
524 upper = ovl_decode_fh(fh, ofs->upper_mnt);
525 if (IS_ERR_OR_NULL(upper))
528 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
534 static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
537 struct ovl_fs *ofs = sb->s_fs_info;
538 struct ovl_path origin = { };
539 struct ovl_path *stack = &origin;
540 struct dentry *dentry = NULL;
541 struct dentry *index = NULL;
542 struct inode *inode = NULL;
543 bool is_deleted = false;
546 /* First lookup indexed upper by fh */
548 index = ovl_get_index_fh(ofs, fh);
549 err = PTR_ERR(index);
554 /* Found a whiteout index - treat as deleted inode */
560 /* Then try to get upper dir by index */
561 if (index && d_is_dir(index)) {
562 struct dentry *upper = ovl_index_upper(ofs, index);
564 err = PTR_ERR(upper);
565 if (IS_ERR_OR_NULL(upper))
568 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
573 /* Then lookup origin by fh */
574 err = ovl_check_origin_fh(ofs, fh, NULL, &stack);
578 err = ovl_verify_origin(index, origin.dentry, false);
581 } else if (is_deleted) {
582 /* Lookup deleted non-dir by origin inode */
583 if (!d_is_dir(origin.dentry))
584 inode = ovl_lookup_inode(sb, origin.dentry, false);
586 if (!inode || atomic_read(&inode->i_count) == 1)
589 /* Deleted but still open? */
590 index = dget(ovl_i_dentry_upper(inode));
593 dentry = ovl_get_dentry(sb, NULL, &origin, index);
602 dentry = ERR_PTR(err);
606 static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
607 int fh_len, int fh_type)
609 struct dentry *dentry = NULL;
610 struct ovl_fh *fh = (struct ovl_fh *) fid;
611 int len = fh_len << 2;
612 unsigned int flags = 0;
616 if (fh_type != OVL_FILEID)
619 err = ovl_check_fh_len(fh, len);
624 dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
625 ovl_upper_fh_to_d(sb, fh) :
626 ovl_lower_fh_to_d(sb, fh);
627 err = PTR_ERR(dentry);
628 if (IS_ERR(dentry) && err != -ESTALE)
634 pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
635 len, fh_type, flags, err);
639 static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
640 int fh_len, int fh_type)
642 pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
643 return ERR_PTR(-EACCES);
646 static int ovl_get_name(struct dentry *parent, char *name,
647 struct dentry *child)
650 * ovl_fh_to_dentry() returns connected dir overlay dentries and
651 * ovl_fh_to_parent() is not implemented, so we should not get here.
657 static struct dentry *ovl_get_parent(struct dentry *dentry)
660 * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
661 * should not get here.
664 return ERR_PTR(-EIO);
667 const struct export_operations ovl_export_operations = {
668 .encode_fh = ovl_encode_inode_fh,
669 .fh_to_dentry = ovl_fh_to_dentry,
670 .fh_to_parent = ovl_fh_to_parent,
671 .get_name = ovl_get_name,
672 .get_parent = ovl_get_parent,