1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Overlayfs NFS export support.
4 *
5 * Amir Goldstein <amir73il@gmail.com>
6 *
7 * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved.
8 */
9
10 #include <linux/fs.h>
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"
18
ovl_encode_maybe_copy_up(struct dentry * dentry)19 static int ovl_encode_maybe_copy_up(struct dentry *dentry)
20 {
21 int err;
22
23 if (ovl_dentry_upper(dentry))
24 return 0;
25
26 err = ovl_want_write(dentry);
27 if (!err) {
28 err = ovl_copy_up(dentry);
29 ovl_drop_write(dentry);
30 }
31
32 if (err) {
33 pr_warn_ratelimited("failed to copy up on encode (%pd2, err=%i)\n",
34 dentry, err);
35 }
36
37 return err;
38 }
39
40 /*
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:
48 *
49 * layer 1: /a
50 * layer 2: /a/b/c
51 *
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.
57 *
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.
64 *
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":
71 *
72 * layer 1: /A (redirect = /a)
73 * layer 2: /a/b/c
74 */
75
76 /* Return the lowest layer for encoding a connectable file handle */
ovl_connectable_layer(struct dentry * dentry)77 static int ovl_connectable_layer(struct dentry *dentry)
78 {
79 struct ovl_entry *oe = OVL_E(dentry);
80
81 /* We can get overlay root from root of any layer */
82 if (dentry == dentry->d_sb->s_root)
83 return ovl_numlower(oe);
84
85 /*
86 * If it's an unindexed merge dir, then it's not connectable with any
87 * lower layer
88 */
89 if (ovl_dentry_upper(dentry) &&
90 !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
91 return 0;
92
93 /* We can get upper/overlay path from indexed/lower dentry */
94 return ovl_lowerstack(oe)->layer->idx;
95 }
96
97 /*
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.
102 *
103 * Return the connected origin layer or < 0 on error.
104 */
ovl_connect_layer(struct dentry * dentry)105 static int ovl_connect_layer(struct dentry *dentry)
106 {
107 struct dentry *next, *parent = NULL;
108 struct ovl_entry *oe = OVL_E(dentry);
109 int origin_layer;
110 int err = 0;
111
112 if (WARN_ON(dentry == dentry->d_sb->s_root) ||
113 WARN_ON(!ovl_dentry_lower(dentry)))
114 return -EIO;
115
116 origin_layer = ovl_lowerstack(oe)->layer->idx;
117 if (ovl_dentry_test_flag(OVL_E_CONNECTED, dentry))
118 return origin_layer;
119
120 /* Find the topmost origin layer connectable ancestor of @dentry */
121 next = dget(dentry);
122 for (;;) {
123 parent = dget_parent(next);
124 if (WARN_ON(parent == next)) {
125 err = -EIO;
126 break;
127 }
128
129 /*
130 * If @parent is not origin layer connectable, then copy up
131 * @next which is origin layer connectable and we are done.
132 */
133 if (ovl_connectable_layer(parent) < origin_layer) {
134 err = ovl_encode_maybe_copy_up(next);
135 break;
136 }
137
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)))
141 break;
142
143 dput(next);
144 next = parent;
145 }
146
147 dput(parent);
148 dput(next);
149
150 if (!err)
151 ovl_dentry_set_flag(OVL_E_CONNECTED, dentry);
152
153 return err ?: origin_layer;
154 }
155
156 /*
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.
163 *
164 * The following table summarizes the different file handle encodings used for
165 * different overlay object types:
166 *
167 * Object type | Encoding
168 * --------------------------------
169 * Pure upper | U
170 * Non-indexed upper | U
171 * Indexed upper | L (*)
172 * Non-upper | L (*)
173 *
174 * U = upper file handle
175 * L = lower file handle
176 *
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.
181 *
182 * Return 0 for upper file handle, > 0 for lower file handle or < 0 on error.
183 */
ovl_check_encode_origin(struct inode * inode)184 static int ovl_check_encode_origin(struct inode *inode)
185 {
186 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
187 bool decodable = ofs->config.nfs_export;
188 struct dentry *dentry;
189 int err;
190
191 /* No upper layer? */
192 if (!ovl_upper_mnt(ofs))
193 return 1;
194
195 /* Lower file handle for non-upper non-decodable */
196 if (!ovl_inode_upper(inode) && !decodable)
197 return 1;
198
199 /* Upper file handle for pure upper */
200 if (!ovl_inode_lower(inode))
201 return 0;
202
203 /*
204 * Root is never indexed, so if there's an upper layer, encode upper for
205 * root.
206 */
207 if (inode == d_inode(inode->i_sb->s_root))
208 return 0;
209
210 /*
211 * Upper decodable file handle for non-indexed upper.
212 */
213 if (ovl_inode_upper(inode) && decodable &&
214 !ovl_test_flag(OVL_INDEX, inode))
215 return 0;
216
217 /*
218 * Decoding a merge dir, whose origin's ancestor is under a redirected
219 * lower dir or under a non-indexed upper is not always possible.
220 * ovl_connect_layer() will try to make origin's layer "connected" by
221 * copying up a "connectable" ancestor.
222 */
223 if (!decodable || !S_ISDIR(inode->i_mode))
224 return 1;
225
226 dentry = d_find_any_alias(inode);
227 if (!dentry)
228 return -ENOENT;
229
230 err = ovl_connect_layer(dentry);
231 dput(dentry);
232 if (err < 0)
233 return err;
234
235 /* Lower file handle for indexed and non-upper dir/non-dir */
236 return 1;
237 }
238
ovl_dentry_to_fid(struct ovl_fs * ofs,struct inode * inode,u32 * fid,int buflen)239 static int ovl_dentry_to_fid(struct ovl_fs *ofs, struct inode *inode,
240 u32 *fid, int buflen)
241 {
242 struct ovl_fh *fh = NULL;
243 int err, enc_lower;
244 int len;
245
246 /*
247 * Check if we should encode a lower or upper file handle and maybe
248 * copy up an ancestor to make lower file handle connectable.
249 */
250 err = enc_lower = ovl_check_encode_origin(inode);
251 if (enc_lower < 0)
252 goto fail;
253
254 /* Encode an upper or lower file handle */
255 fh = ovl_encode_real_fh(ofs, enc_lower ? ovl_inode_lower(inode) :
256 ovl_inode_upper(inode), !enc_lower);
257 if (IS_ERR(fh))
258 return PTR_ERR(fh);
259
260 len = OVL_FH_LEN(fh);
261 if (len <= buflen)
262 memcpy(fid, fh, len);
263 err = len;
264
265 out:
266 kfree(fh);
267 return err;
268
269 fail:
270 pr_warn_ratelimited("failed to encode file handle (ino=%lu, err=%i)\n",
271 inode->i_ino, err);
272 goto out;
273 }
274
ovl_encode_fh(struct inode * inode,u32 * fid,int * max_len,struct inode * parent)275 static int ovl_encode_fh(struct inode *inode, u32 *fid, int *max_len,
276 struct inode *parent)
277 {
278 struct ovl_fs *ofs = OVL_FS(inode->i_sb);
279 int bytes, buflen = *max_len << 2;
280
281 /* TODO: encode connectable file handles */
282 if (parent)
283 return FILEID_INVALID;
284
285 bytes = ovl_dentry_to_fid(ofs, inode, fid, buflen);
286 if (bytes <= 0)
287 return FILEID_INVALID;
288
289 *max_len = bytes >> 2;
290 if (bytes > buflen)
291 return FILEID_INVALID;
292
293 return OVL_FILEID_V1;
294 }
295
296 /*
297 * Find or instantiate an overlay dentry from real dentries and index.
298 */
ovl_obtain_alias(struct super_block * sb,struct dentry * upper_alias,struct ovl_path * lowerpath,struct dentry * index)299 static struct dentry *ovl_obtain_alias(struct super_block *sb,
300 struct dentry *upper_alias,
301 struct ovl_path *lowerpath,
302 struct dentry *index)
303 {
304 struct dentry *lower = lowerpath ? lowerpath->dentry : NULL;
305 struct dentry *upper = upper_alias ?: index;
306 struct dentry *dentry;
307 struct inode *inode = NULL;
308 struct ovl_entry *oe;
309 struct ovl_inode_params oip = {
310 .index = index,
311 };
312
313 /* We get overlay directory dentries with ovl_lookup_real() */
314 if (d_is_dir(upper ?: lower))
315 return ERR_PTR(-EIO);
316
317 oe = ovl_alloc_entry(!!lower);
318 if (!oe)
319 return ERR_PTR(-ENOMEM);
320
321 oip.upperdentry = dget(upper);
322 if (lower) {
323 ovl_lowerstack(oe)->dentry = dget(lower);
324 ovl_lowerstack(oe)->layer = lowerpath->layer;
325 }
326 oip.oe = oe;
327 inode = ovl_get_inode(sb, &oip);
328 if (IS_ERR(inode)) {
329 ovl_free_entry(oe);
330 dput(upper);
331 return ERR_CAST(inode);
332 }
333
334 if (upper)
335 ovl_set_flag(OVL_UPPERDATA, inode);
336
337 dentry = d_find_any_alias(inode);
338 if (dentry)
339 goto out_iput;
340
341 dentry = d_alloc_anon(inode->i_sb);
342 if (unlikely(!dentry))
343 goto nomem;
344
345 if (upper_alias)
346 ovl_dentry_set_upper_alias(dentry);
347
348 ovl_dentry_init_reval(dentry, upper, OVL_I_E(inode));
349
350 return d_instantiate_anon(dentry, inode);
351
352 nomem:
353 dput(dentry);
354 dentry = ERR_PTR(-ENOMEM);
355 out_iput:
356 iput(inode);
357 return dentry;
358 }
359
360 /* Get the upper or lower dentry in stack whose on layer @idx */
ovl_dentry_real_at(struct dentry * dentry,int idx)361 static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
362 {
363 struct ovl_entry *oe = OVL_E(dentry);
364 struct ovl_path *lowerstack = ovl_lowerstack(oe);
365 int i;
366
367 if (!idx)
368 return ovl_dentry_upper(dentry);
369
370 for (i = 0; i < ovl_numlower(oe); i++) {
371 if (lowerstack[i].layer->idx == idx)
372 return lowerstack[i].dentry;
373 }
374
375 return NULL;
376 }
377
378 /*
379 * Lookup a child overlay dentry to get a connected overlay dentry whose real
380 * dentry is @real. If @real is on upper layer, we lookup a child overlay
381 * dentry with the same name as the real dentry. Otherwise, we need to consult
382 * index for lookup.
383 */
ovl_lookup_real_one(struct dentry * connected,struct dentry * real,const struct ovl_layer * layer)384 static struct dentry *ovl_lookup_real_one(struct dentry *connected,
385 struct dentry *real,
386 const struct ovl_layer *layer)
387 {
388 struct inode *dir = d_inode(connected);
389 struct dentry *this, *parent = NULL;
390 struct name_snapshot name;
391 int err;
392
393 /*
394 * Lookup child overlay dentry by real name. The dir mutex protects us
395 * from racing with overlay rename. If the overlay dentry that is above
396 * real has already been moved to a parent that is not under the
397 * connected overlay dir, we return -ECHILD and restart the lookup of
398 * connected real path from the top.
399 */
400 inode_lock_nested(dir, I_MUTEX_PARENT);
401 err = -ECHILD;
402 parent = dget_parent(real);
403 if (ovl_dentry_real_at(connected, layer->idx) != parent)
404 goto fail;
405
406 /*
407 * We also need to take a snapshot of real dentry name to protect us
408 * from racing with underlying layer rename. In this case, we don't
409 * care about returning ESTALE, only from dereferencing a free name
410 * pointer because we hold no lock on the real dentry.
411 */
412 take_dentry_name_snapshot(&name, real);
413 /*
414 * No idmap handling here: it's an internal lookup. Could skip
415 * permission checking altogether, but for now just use non-idmap
416 * transformed ids.
417 */
418 this = lookup_one_len(name.name.name, connected, name.name.len);
419 release_dentry_name_snapshot(&name);
420 err = PTR_ERR(this);
421 if (IS_ERR(this)) {
422 goto fail;
423 } else if (!this || !this->d_inode) {
424 dput(this);
425 err = -ENOENT;
426 goto fail;
427 } else if (ovl_dentry_real_at(this, layer->idx) != real) {
428 dput(this);
429 err = -ESTALE;
430 goto fail;
431 }
432
433 out:
434 dput(parent);
435 inode_unlock(dir);
436 return this;
437
438 fail:
439 pr_warn_ratelimited("failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
440 real, layer->idx, connected, err);
441 this = ERR_PTR(err);
442 goto out;
443 }
444
445 static struct dentry *ovl_lookup_real(struct super_block *sb,
446 struct dentry *real,
447 const struct ovl_layer *layer);
448
449 /*
450 * Lookup an indexed or hashed overlay dentry by real inode.
451 */
ovl_lookup_real_inode(struct super_block * sb,struct dentry * real,const struct ovl_layer * layer)452 static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
453 struct dentry *real,
454 const struct ovl_layer *layer)
455 {
456 struct ovl_fs *ofs = OVL_FS(sb);
457 struct dentry *index = NULL;
458 struct dentry *this = NULL;
459 struct inode *inode;
460
461 /*
462 * Decoding upper dir from index is expensive, so first try to lookup
463 * overlay dentry in inode/dcache.
464 */
465 inode = ovl_lookup_inode(sb, real, !layer->idx);
466 if (IS_ERR(inode))
467 return ERR_CAST(inode);
468 if (inode) {
469 this = d_find_any_alias(inode);
470 iput(inode);
471 }
472
473 /*
474 * For decoded lower dir file handle, lookup index by origin to check
475 * if lower dir was copied up and and/or removed.
476 */
477 if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) {
478 index = ovl_lookup_index(ofs, NULL, real, false);
479 if (IS_ERR(index))
480 return index;
481 }
482
483 /* Get connected upper overlay dir from index */
484 if (index) {
485 struct dentry *upper = ovl_index_upper(ofs, index, true);
486
487 dput(index);
488 if (IS_ERR_OR_NULL(upper))
489 return upper;
490
491 /*
492 * ovl_lookup_real() in lower layer may call recursively once to
493 * ovl_lookup_real() in upper layer. The first level call walks
494 * back lower parents to the topmost indexed parent. The second
495 * recursive call walks back from indexed upper to the topmost
496 * connected/hashed upper parent (or up to root).
497 */
498 this = ovl_lookup_real(sb, upper, &ofs->layers[0]);
499 dput(upper);
500 }
501
502 if (IS_ERR_OR_NULL(this))
503 return this;
504
505 if (ovl_dentry_real_at(this, layer->idx) != real) {
506 dput(this);
507 this = ERR_PTR(-EIO);
508 }
509
510 return this;
511 }
512
513 /*
514 * Lookup an indexed or hashed overlay dentry, whose real dentry is an
515 * ancestor of @real.
516 */
ovl_lookup_real_ancestor(struct super_block * sb,struct dentry * real,const struct ovl_layer * layer)517 static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
518 struct dentry *real,
519 const struct ovl_layer *layer)
520 {
521 struct dentry *next, *parent = NULL;
522 struct dentry *ancestor = ERR_PTR(-EIO);
523
524 if (real == layer->mnt->mnt_root)
525 return dget(sb->s_root);
526
527 /* Find the topmost indexed or hashed ancestor */
528 next = dget(real);
529 for (;;) {
530 parent = dget_parent(next);
531
532 /*
533 * Lookup a matching overlay dentry in inode/dentry
534 * cache or in index by real inode.
535 */
536 ancestor = ovl_lookup_real_inode(sb, next, layer);
537 if (ancestor)
538 break;
539
540 if (parent == layer->mnt->mnt_root) {
541 ancestor = dget(sb->s_root);
542 break;
543 }
544
545 /*
546 * If @real has been moved out of the layer root directory,
547 * we will eventully hit the real fs root. This cannot happen
548 * by legit overlay rename, so we return error in that case.
549 */
550 if (parent == next) {
551 ancestor = ERR_PTR(-EXDEV);
552 break;
553 }
554
555 dput(next);
556 next = parent;
557 }
558
559 dput(parent);
560 dput(next);
561
562 return ancestor;
563 }
564
565 /*
566 * Lookup a connected overlay dentry whose real dentry is @real.
567 * If @real is on upper layer, we lookup a child overlay dentry with the same
568 * path the real dentry. Otherwise, we need to consult index for lookup.
569 */
ovl_lookup_real(struct super_block * sb,struct dentry * real,const struct ovl_layer * layer)570 static struct dentry *ovl_lookup_real(struct super_block *sb,
571 struct dentry *real,
572 const struct ovl_layer *layer)
573 {
574 struct dentry *connected;
575 int err = 0;
576
577 connected = ovl_lookup_real_ancestor(sb, real, layer);
578 if (IS_ERR(connected))
579 return connected;
580
581 while (!err) {
582 struct dentry *next, *this;
583 struct dentry *parent = NULL;
584 struct dentry *real_connected = ovl_dentry_real_at(connected,
585 layer->idx);
586
587 if (real_connected == real)
588 break;
589
590 /* Find the topmost dentry not yet connected */
591 next = dget(real);
592 for (;;) {
593 parent = dget_parent(next);
594
595 if (parent == real_connected)
596 break;
597
598 /*
599 * If real has been moved out of 'real_connected',
600 * we will not find 'real_connected' and hit the layer
601 * root. In that case, we need to restart connecting.
602 * This game can go on forever in the worst case. We
603 * may want to consider taking s_vfs_rename_mutex if
604 * this happens more than once.
605 */
606 if (parent == layer->mnt->mnt_root) {
607 dput(connected);
608 connected = dget(sb->s_root);
609 break;
610 }
611
612 /*
613 * If real file has been moved out of the layer root
614 * directory, we will eventully hit the real fs root.
615 * This cannot happen by legit overlay rename, so we
616 * return error in that case.
617 */
618 if (parent == next) {
619 err = -EXDEV;
620 break;
621 }
622
623 dput(next);
624 next = parent;
625 }
626
627 if (!err) {
628 this = ovl_lookup_real_one(connected, next, layer);
629 if (IS_ERR(this))
630 err = PTR_ERR(this);
631
632 /*
633 * Lookup of child in overlay can fail when racing with
634 * overlay rename of child away from 'connected' parent.
635 * In this case, we need to restart the lookup from the
636 * top, because we cannot trust that 'real_connected' is
637 * still an ancestor of 'real'. There is a good chance
638 * that the renamed overlay ancestor is now in cache, so
639 * ovl_lookup_real_ancestor() will find it and we can
640 * continue to connect exactly from where lookup failed.
641 */
642 if (err == -ECHILD) {
643 this = ovl_lookup_real_ancestor(sb, real,
644 layer);
645 err = PTR_ERR_OR_ZERO(this);
646 }
647 if (!err) {
648 dput(connected);
649 connected = this;
650 }
651 }
652
653 dput(parent);
654 dput(next);
655 }
656
657 if (err)
658 goto fail;
659
660 return connected;
661
662 fail:
663 pr_warn_ratelimited("failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
664 real, layer->idx, connected, err);
665 dput(connected);
666 return ERR_PTR(err);
667 }
668
669 /*
670 * Get an overlay dentry from upper/lower real dentries and index.
671 */
ovl_get_dentry(struct super_block * sb,struct dentry * upper,struct ovl_path * lowerpath,struct dentry * index)672 static struct dentry *ovl_get_dentry(struct super_block *sb,
673 struct dentry *upper,
674 struct ovl_path *lowerpath,
675 struct dentry *index)
676 {
677 struct ovl_fs *ofs = OVL_FS(sb);
678 const struct ovl_layer *layer = upper ? &ofs->layers[0] : lowerpath->layer;
679 struct dentry *real = upper ?: (index ?: lowerpath->dentry);
680
681 /*
682 * Obtain a disconnected overlay dentry from a non-dir real dentry
683 * and index.
684 */
685 if (!d_is_dir(real))
686 return ovl_obtain_alias(sb, upper, lowerpath, index);
687
688 /* Removed empty directory? */
689 if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
690 return ERR_PTR(-ENOENT);
691
692 /*
693 * If real dentry is connected and hashed, get a connected overlay
694 * dentry whose real dentry is @real.
695 */
696 return ovl_lookup_real(sb, real, layer);
697 }
698
ovl_upper_fh_to_d(struct super_block * sb,struct ovl_fh * fh)699 static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
700 struct ovl_fh *fh)
701 {
702 struct ovl_fs *ofs = OVL_FS(sb);
703 struct dentry *dentry;
704 struct dentry *upper;
705
706 if (!ovl_upper_mnt(ofs))
707 return ERR_PTR(-EACCES);
708
709 upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), true);
710 if (IS_ERR_OR_NULL(upper))
711 return upper;
712
713 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
714 dput(upper);
715
716 return dentry;
717 }
718
ovl_lower_fh_to_d(struct super_block * sb,struct ovl_fh * fh)719 static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
720 struct ovl_fh *fh)
721 {
722 struct ovl_fs *ofs = OVL_FS(sb);
723 struct ovl_path origin = { };
724 struct ovl_path *stack = &origin;
725 struct dentry *dentry = NULL;
726 struct dentry *index = NULL;
727 struct inode *inode;
728 int err;
729
730 /* First lookup overlay inode in inode cache by origin fh */
731 err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack);
732 if (err)
733 return ERR_PTR(err);
734
735 if (!d_is_dir(origin.dentry) ||
736 !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
737 inode = ovl_lookup_inode(sb, origin.dentry, false);
738 err = PTR_ERR(inode);
739 if (IS_ERR(inode))
740 goto out_err;
741 if (inode) {
742 dentry = d_find_any_alias(inode);
743 iput(inode);
744 if (dentry)
745 goto out;
746 }
747 }
748
749 /* Then lookup indexed upper/whiteout by origin fh */
750 if (ofs->indexdir) {
751 index = ovl_get_index_fh(ofs, fh);
752 err = PTR_ERR(index);
753 if (IS_ERR(index)) {
754 index = NULL;
755 goto out_err;
756 }
757 }
758
759 /* Then try to get a connected upper dir by index */
760 if (index && d_is_dir(index)) {
761 struct dentry *upper = ovl_index_upper(ofs, index, true);
762
763 err = PTR_ERR(upper);
764 if (IS_ERR_OR_NULL(upper))
765 goto out_err;
766
767 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
768 dput(upper);
769 goto out;
770 }
771
772 /* Find origin.dentry again with ovl_acceptable() layer check */
773 if (d_is_dir(origin.dentry)) {
774 dput(origin.dentry);
775 origin.dentry = NULL;
776 err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack);
777 if (err)
778 goto out_err;
779 }
780 if (index) {
781 err = ovl_verify_origin(ofs, index, origin.dentry, false);
782 if (err)
783 goto out_err;
784 }
785
786 /* Get a connected non-upper dir or disconnected non-dir */
787 dentry = ovl_get_dentry(sb, NULL, &origin, index);
788
789 out:
790 dput(origin.dentry);
791 dput(index);
792 return dentry;
793
794 out_err:
795 dentry = ERR_PTR(err);
796 goto out;
797 }
798
ovl_fid_to_fh(struct fid * fid,int buflen,int fh_type)799 static struct ovl_fh *ovl_fid_to_fh(struct fid *fid, int buflen, int fh_type)
800 {
801 struct ovl_fh *fh;
802
803 /* If on-wire inner fid is aligned - nothing to do */
804 if (fh_type == OVL_FILEID_V1)
805 return (struct ovl_fh *)fid;
806
807 if (fh_type != OVL_FILEID_V0)
808 return ERR_PTR(-EINVAL);
809
810 if (buflen <= OVL_FH_WIRE_OFFSET)
811 return ERR_PTR(-EINVAL);
812
813 fh = kzalloc(buflen, GFP_KERNEL);
814 if (!fh)
815 return ERR_PTR(-ENOMEM);
816
817 /* Copy unaligned inner fh into aligned buffer */
818 memcpy(fh->buf, fid, buflen - OVL_FH_WIRE_OFFSET);
819 return fh;
820 }
821
ovl_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)822 static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
823 int fh_len, int fh_type)
824 {
825 struct dentry *dentry = NULL;
826 struct ovl_fh *fh = NULL;
827 int len = fh_len << 2;
828 unsigned int flags = 0;
829 int err;
830
831 fh = ovl_fid_to_fh(fid, len, fh_type);
832 err = PTR_ERR(fh);
833 if (IS_ERR(fh))
834 goto out_err;
835
836 err = ovl_check_fh_len(fh, len);
837 if (err)
838 goto out_err;
839
840 flags = fh->fb.flags;
841 dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
842 ovl_upper_fh_to_d(sb, fh) :
843 ovl_lower_fh_to_d(sb, fh);
844 err = PTR_ERR(dentry);
845 if (IS_ERR(dentry) && err != -ESTALE)
846 goto out_err;
847
848 out:
849 /* We may have needed to re-align OVL_FILEID_V0 */
850 if (!IS_ERR_OR_NULL(fh) && fh != (void *)fid)
851 kfree(fh);
852
853 return dentry;
854
855 out_err:
856 pr_warn_ratelimited("failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
857 fh_len, fh_type, flags, err);
858 dentry = ERR_PTR(err);
859 goto out;
860 }
861
ovl_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)862 static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
863 int fh_len, int fh_type)
864 {
865 pr_warn_ratelimited("connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
866 return ERR_PTR(-EACCES);
867 }
868
ovl_get_name(struct dentry * parent,char * name,struct dentry * child)869 static int ovl_get_name(struct dentry *parent, char *name,
870 struct dentry *child)
871 {
872 /*
873 * ovl_fh_to_dentry() returns connected dir overlay dentries and
874 * ovl_fh_to_parent() is not implemented, so we should not get here.
875 */
876 WARN_ON_ONCE(1);
877 return -EIO;
878 }
879
ovl_get_parent(struct dentry * dentry)880 static struct dentry *ovl_get_parent(struct dentry *dentry)
881 {
882 /*
883 * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
884 * should not get here.
885 */
886 WARN_ON_ONCE(1);
887 return ERR_PTR(-EIO);
888 }
889
890 const struct export_operations ovl_export_operations = {
891 .encode_fh = ovl_encode_fh,
892 .fh_to_dentry = ovl_fh_to_dentry,
893 .fh_to_parent = ovl_fh_to_parent,
894 .get_name = ovl_get_name,
895 .get_parent = ovl_get_parent,
896 };
897
898 /* encode_fh() encodes non-decodable file handles with nfs_export=off */
899 const struct export_operations ovl_export_fid_operations = {
900 .encode_fh = ovl_encode_fh,
901 };
902