]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/overlayfs/export.c
ovl: hash non-indexed dir by upper inode for NFS export
[thirdparty/kernel/linux.git] / fs / overlayfs / export.c
CommitLineData
8ed5eec9
AG
1/*
2 * Overlayfs NFS export support.
3 *
4 * Amir Goldstein <amir73il@gmail.com>
5 *
6 * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved.
7 *
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.
11 */
12
13#include <linux/fs.h>
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"
21
b305e844
AG
22/*
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.
29 *
30 * The following table summarizes the different file handle encodings used for
31 * different overlay object types:
32 *
33 * Object type | Encoding
34 * --------------------------------
35 * Pure upper | U
36 * Non-indexed upper | U
05e1f118
AG
37 * Indexed upper | L (*)
38 * Non-upper | L (*)
b305e844
AG
39 *
40 * U = upper file handle
41 * L = lower file handle
05e1f118
AG
42 *
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.
b305e844
AG
46 */
47static bool ovl_should_encode_origin(struct dentry *dentry)
48{
05e1f118
AG
49 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
50
b305e844
AG
51 if (!ovl_dentry_lower(dentry))
52 return false;
53
05e1f118
AG
54 /*
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.
59 *
60 * TODO: copy up only the parent that is under redirected lower.
61 */
62 if (d_is_dir(dentry) && ofs->upper_mnt &&
63 OVL_E(dentry)->lowerstack[0].layer->idx > 1)
64 return false;
65
b305e844
AG
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)))
69 return false;
70
71 return true;
72}
73
05e1f118
AG
74static int ovl_encode_maybe_copy_up(struct dentry *dentry)
75{
76 int err;
77
78 if (ovl_dentry_upper(dentry))
79 return 0;
80
81 err = ovl_want_write(dentry);
82 if (err)
83 return err;
84
85 err = ovl_copy_up(dentry);
86
87 ovl_drop_write(dentry);
88 return err;
89}
90
8ed5eec9
AG
91static int ovl_d_to_fh(struct dentry *dentry, char *buf, int buflen)
92{
8ed5eec9
AG
93 struct dentry *origin = ovl_dentry_lower(dentry);
94 struct ovl_fh *fh = NULL;
95 int err;
96
05e1f118
AG
97 /*
98 * If we should not encode a lower dir file handle, copy up and encode
99 * an upper dir file handle.
100 */
101 if (!ovl_should_encode_origin(dentry)) {
102 err = ovl_encode_maybe_copy_up(dentry);
103 if (err)
104 goto fail;
105
8ed5eec9 106 origin = NULL;
05e1f118 107 }
8ed5eec9 108
03e1c584
AG
109 /* Encode an upper or origin file handle */
110 fh = ovl_encode_fh(origin ?: ovl_dentry_upper(dentry), !origin);
8ed5eec9
AG
111
112 err = -EOVERFLOW;
113 if (fh->len > buflen)
114 goto fail;
115
116 memcpy(buf, (char *)fh, fh->len);
117 err = fh->len;
118
119out:
120 kfree(fh);
121 return err;
122
123fail:
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,
126 fh ? fh->type : 0);
127 goto out;
128}
129
130static int ovl_dentry_to_fh(struct dentry *dentry, u32 *fid, int *max_len)
131{
132 int res, len = *max_len << 2;
133
134 res = ovl_d_to_fh(dentry, (char *)fid, len);
135 if (res <= 0)
136 return FILEID_INVALID;
137
138 len = res;
139
140 /* Round up to dwords */
141 *max_len = (len + 3) >> 2;
142 return OVL_FILEID;
143}
144
145static int ovl_encode_inode_fh(struct inode *inode, u32 *fid, int *max_len,
146 struct inode *parent)
147{
148 struct dentry *dentry;
149 int type;
150
151 /* TODO: encode connectable file handles */
152 if (parent)
153 return FILEID_INVALID;
154
155 dentry = d_find_any_alias(inode);
156 if (WARN_ON(!dentry))
157 return FILEID_INVALID;
158
159 type = ovl_dentry_to_fh(dentry, fid, max_len);
160
161 dput(dentry);
162 return type;
163}
164
8556a420 165/*
f71bd9cf 166 * Find or instantiate an overlay dentry from real dentries and index.
8556a420
AG
167 */
168static struct dentry *ovl_obtain_alias(struct super_block *sb,
f71bd9cf
AG
169 struct dentry *upper_alias,
170 struct ovl_path *lowerpath,
171 struct dentry *index)
8556a420 172{
f941866f 173 struct dentry *lower = lowerpath ? lowerpath->dentry : NULL;
f71bd9cf 174 struct dentry *upper = upper_alias ?: index;
8556a420 175 struct dentry *dentry;
f941866f 176 struct inode *inode;
8556a420 177 struct ovl_entry *oe;
8556a420 178
f71bd9cf
AG
179 /* We get overlay directory dentries with ovl_lookup_real() */
180 if (d_is_dir(upper ?: lower))
8556a420
AG
181 return ERR_PTR(-EIO);
182
f71bd9cf 183 inode = ovl_get_inode(sb, dget(upper), lower, index, !!lower);
8556a420
AG
184 if (IS_ERR(inode)) {
185 dput(upper);
186 return ERR_CAST(inode);
187 }
188
f71bd9cf
AG
189 if (index)
190 ovl_set_flag(OVL_INDEX, inode);
191
8556a420
AG
192 dentry = d_find_any_alias(inode);
193 if (!dentry) {
194 dentry = d_alloc_anon(inode->i_sb);
195 if (!dentry)
196 goto nomem;
f941866f 197 oe = ovl_alloc_entry(lower ? 1 : 0);
8556a420
AG
198 if (!oe)
199 goto nomem;
200
f941866f
AG
201 if (lower) {
202 oe->lowerstack->dentry = dget(lower);
203 oe->lowerstack->layer = lowerpath->layer;
204 }
8556a420 205 dentry->d_fsdata = oe;
f71bd9cf 206 if (upper_alias)
f941866f 207 ovl_dentry_set_upper_alias(dentry);
8556a420
AG
208 }
209
210 return d_instantiate_anon(dentry, inode);
211
212nomem:
213 iput(inode);
214 dput(dentry);
215 return ERR_PTR(-ENOMEM);
216}
217
98892516
AG
218/* Get the upper or lower dentry in stach whose on layer @idx */
219static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
220{
221 struct ovl_entry *oe = dentry->d_fsdata;
222 int i;
223
224 if (!idx)
225 return ovl_dentry_upper(dentry);
226
227 for (i = 0; i < oe->numlower; i++) {
228 if (oe->lowerstack[i].layer->idx == idx)
229 return oe->lowerstack[i].dentry;
230 }
231
232 return NULL;
233}
234
3985b70a
AG
235/*
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
239 * index for lookup.
240 */
241static struct dentry *ovl_lookup_real_one(struct dentry *connected,
242 struct dentry *real,
243 struct ovl_layer *layer)
244{
245 struct inode *dir = d_inode(connected);
246 struct dentry *this, *parent = NULL;
247 struct name_snapshot name;
248 int err;
249
3985b70a
AG
250 /*
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.
256 */
257 inode_lock_nested(dir, I_MUTEX_PARENT);
258 err = -ECHILD;
259 parent = dget_parent(real);
98892516 260 if (ovl_dentry_real_at(connected, layer->idx) != parent)
3985b70a
AG
261 goto fail;
262
263 /*
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.
268 */
269 take_dentry_name_snapshot(&name, real);
270 this = lookup_one_len(name.name, connected, strlen(name.name));
271 err = PTR_ERR(this);
272 if (IS_ERR(this)) {
273 goto fail;
274 } else if (!this || !this->d_inode) {
275 dput(this);
276 err = -ENOENT;
277 goto fail;
98892516 278 } else if (ovl_dentry_real_at(this, layer->idx) != real) {
3985b70a
AG
279 dput(this);
280 err = -ESTALE;
281 goto fail;
282 }
283
284out:
285 release_dentry_name_snapshot(&name);
286 dput(parent);
287 inode_unlock(dir);
288 return this;
289
290fail:
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);
293 this = ERR_PTR(err);
294 goto out;
295}
296
297/*
298 * Lookup a connected overlay dentry whose real dentry is @real.
299 * If @real is on upper layer, we lookup a child overlay dentry with the same
300 * path the real dentry. Otherwise, we need to consult index for lookup.
301 */
302static struct dentry *ovl_lookup_real(struct super_block *sb,
303 struct dentry *real,
304 struct ovl_layer *layer)
305{
306 struct dentry *connected;
307 int err = 0;
308
309 /* TODO: use index when looking up by lower real dentry */
3985b70a
AG
310
311 connected = dget(sb->s_root);
312 while (!err) {
313 struct dentry *next, *this;
314 struct dentry *parent = NULL;
98892516
AG
315 struct dentry *real_connected = ovl_dentry_real_at(connected,
316 layer->idx);
3985b70a
AG
317
318 if (real_connected == real)
319 break;
320
321 /* Find the topmost dentry not yet connected */
322 next = dget(real);
323 for (;;) {
324 parent = dget_parent(next);
325
326 if (parent == real_connected)
327 break;
328
329 /*
330 * If real has been moved out of 'real_connected',
331 * we will not find 'real_connected' and hit the layer
332 * root. In that case, we need to restart connecting.
333 * This game can go on forever in the worst case. We
334 * may want to consider taking s_vfs_rename_mutex if
335 * this happens more than once.
336 */
337 if (parent == layer->mnt->mnt_root) {
338 dput(connected);
339 connected = dget(sb->s_root);
340 break;
341 }
342
343 /*
344 * If real file has been moved out of the layer root
345 * directory, we will eventully hit the real fs root.
346 * This cannot happen by legit overlay rename, so we
347 * return error in that case.
348 */
349 if (parent == next) {
350 err = -EXDEV;
351 break;
352 }
353
354 dput(next);
355 next = parent;
356 }
357
358 if (!err) {
359 this = ovl_lookup_real_one(connected, next, layer);
360 if (IS_ERR(this))
361 err = PTR_ERR(this);
362
363 /*
364 * Lookup of child in overlay can fail when racing with
365 * overlay rename of child away from 'connected' parent.
366 * In this case, we need to restart the lookup from the
367 * top, because we cannot trust that 'real_connected' is
368 * still an ancestor of 'real'.
369 */
370 if (err == -ECHILD) {
371 this = dget(sb->s_root);
372 err = 0;
373 }
374 if (!err) {
375 dput(connected);
376 connected = this;
377 }
378 }
379
380 dput(parent);
381 dput(next);
382 }
383
384 if (err)
385 goto fail;
386
387 return connected;
388
389fail:
390 pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
391 real, layer->idx, connected, err);
392 dput(connected);
393 return ERR_PTR(err);
394}
395
396/*
f71bd9cf 397 * Get an overlay dentry from upper/lower real dentries and index.
3985b70a
AG
398 */
399static struct dentry *ovl_get_dentry(struct super_block *sb,
400 struct dentry *upper,
f71bd9cf
AG
401 struct ovl_path *lowerpath,
402 struct dentry *index)
3985b70a
AG
403{
404 struct ovl_fs *ofs = sb->s_fs_info;
405 struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
98892516 406 struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer;
f71bd9cf 407 struct dentry *real = upper ?: (index ?: lowerpath->dentry);
3985b70a 408
f941866f 409 /*
f71bd9cf
AG
410 * Obtain a disconnected overlay dentry from a non-dir real dentry
411 * and index.
f941866f 412 */
f71bd9cf
AG
413 if (!d_is_dir(real))
414 return ovl_obtain_alias(sb, upper, lowerpath, index);
f941866f 415
3985b70a 416 /* Removed empty directory? */
98892516 417 if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
3985b70a
AG
418 return ERR_PTR(-ENOENT);
419
420 /*
98892516
AG
421 * If real dentry is connected and hashed, get a connected overlay
422 * dentry whose real dentry is @real.
3985b70a 423 */
98892516 424 return ovl_lookup_real(sb, real, layer);
3985b70a
AG
425}
426
8556a420
AG
427static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
428 struct ovl_fh *fh)
429{
430 struct ovl_fs *ofs = sb->s_fs_info;
431 struct dentry *dentry;
432 struct dentry *upper;
433
434 if (!ofs->upper_mnt)
435 return ERR_PTR(-EACCES);
436
437 upper = ovl_decode_fh(fh, ofs->upper_mnt);
438 if (IS_ERR_OR_NULL(upper))
439 return upper;
440
f71bd9cf 441 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
8556a420
AG
442 dput(upper);
443
444 return dentry;
445}
446
f941866f
AG
447static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
448 struct ovl_fh *fh)
449{
450 struct ovl_fs *ofs = sb->s_fs_info;
451 struct ovl_path origin = { };
452 struct ovl_path *stack = &origin;
453 struct dentry *dentry = NULL;
f71bd9cf 454 struct dentry *index = NULL;
9436a1a3
AG
455 struct inode *inode = NULL;
456 bool is_deleted = false;
f941866f
AG
457 int err;
458
f71bd9cf
AG
459 /* First lookup indexed upper by fh */
460 if (ofs->indexdir) {
461 index = ovl_get_index_fh(ofs, fh);
462 err = PTR_ERR(index);
9436a1a3
AG
463 if (IS_ERR(index)) {
464 if (err != -ESTALE)
465 return ERR_PTR(err);
466
467 /* Found a whiteout index - treat as deleted inode */
468 is_deleted = true;
469 index = NULL;
470 }
f71bd9cf
AG
471 }
472
3b0bfc6e
AG
473 /* Then try to get upper dir by index */
474 if (index && d_is_dir(index)) {
475 struct dentry *upper = ovl_index_upper(ofs, index);
476
477 err = PTR_ERR(upper);
478 if (IS_ERR_OR_NULL(upper))
479 goto out_err;
480
481 dentry = ovl_get_dentry(sb, upper, NULL, NULL);
482 dput(upper);
483 goto out;
484 }
485
f71bd9cf 486 /* Then lookup origin by fh */
f941866f 487 err = ovl_check_origin_fh(ofs, fh, NULL, &stack);
f71bd9cf
AG
488 if (err) {
489 goto out_err;
490 } else if (index) {
491 err = ovl_verify_origin(index, origin.dentry, false);
492 if (err)
493 goto out_err;
9436a1a3
AG
494 } else if (is_deleted) {
495 /* Lookup deleted non-dir by origin inode */
496 if (!d_is_dir(origin.dentry))
497 inode = ovl_lookup_inode(sb, origin.dentry);
498 err = -ESTALE;
499 if (!inode || atomic_read(&inode->i_count) == 1)
500 goto out_err;
501
502 /* Deleted but still open? */
503 index = dget(ovl_i_dentry_upper(inode));
f71bd9cf 504 }
f941866f 505
f71bd9cf 506 dentry = ovl_get_dentry(sb, NULL, &origin, index);
f941866f 507
f71bd9cf
AG
508out:
509 dput(origin.dentry);
510 dput(index);
9436a1a3 511 iput(inode);
f941866f 512 return dentry;
f71bd9cf
AG
513
514out_err:
515 dentry = ERR_PTR(err);
516 goto out;
f941866f
AG
517}
518
8556a420
AG
519static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
520 int fh_len, int fh_type)
521{
522 struct dentry *dentry = NULL;
523 struct ovl_fh *fh = (struct ovl_fh *) fid;
524 int len = fh_len << 2;
525 unsigned int flags = 0;
526 int err;
527
528 err = -EINVAL;
529 if (fh_type != OVL_FILEID)
530 goto out_err;
531
532 err = ovl_check_fh_len(fh, len);
533 if (err)
534 goto out_err;
535
8556a420 536 flags = fh->flags;
f941866f
AG
537 dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
538 ovl_upper_fh_to_d(sb, fh) :
539 ovl_lower_fh_to_d(sb, fh);
8556a420
AG
540 err = PTR_ERR(dentry);
541 if (IS_ERR(dentry) && err != -ESTALE)
542 goto out_err;
543
544 return dentry;
545
546out_err:
547 pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
548 len, fh_type, flags, err);
549 return ERR_PTR(err);
550}
551
3985b70a
AG
552static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
553 int fh_len, int fh_type)
554{
555 pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
556 return ERR_PTR(-EACCES);
557}
558
559static int ovl_get_name(struct dentry *parent, char *name,
560 struct dentry *child)
561{
562 /*
563 * ovl_fh_to_dentry() returns connected dir overlay dentries and
564 * ovl_fh_to_parent() is not implemented, so we should not get here.
565 */
566 WARN_ON_ONCE(1);
567 return -EIO;
568}
569
570static struct dentry *ovl_get_parent(struct dentry *dentry)
571{
572 /*
573 * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
574 * should not get here.
575 */
576 WARN_ON_ONCE(1);
577 return ERR_PTR(-EIO);
578}
579
8ed5eec9
AG
580const struct export_operations ovl_export_operations = {
581 .encode_fh = ovl_encode_inode_fh,
8556a420 582 .fh_to_dentry = ovl_fh_to_dentry,
3985b70a
AG
583 .fh_to_parent = ovl_fh_to_parent,
584 .get_name = ovl_get_name,
585 .get_parent = ovl_get_parent,
8ed5eec9 586};