Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
8ed5eec9 AG |
2 | /* |
3 | * Overlayfs NFS export support. | |
4 | * | |
5 | * Amir Goldstein <amir73il@gmail.com> | |
6 | * | |
7 | * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved. | |
8ed5eec9 AG |
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 | ||
2ca3c148 AG |
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) { | |
1bd0a3ae | 33 | pr_warn_ratelimited("failed to copy up on encode (%pd2, err=%i)\n", |
2ca3c148 AG |
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 */ | |
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) | |
5522c9c7 | 83 | return ovl_numlower(oe); |
2ca3c148 AG |
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 */ | |
5522c9c7 | 94 | return ovl_lowerstack(oe)->layer->idx; |
2ca3c148 AG |
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 | */ | |
105 | static int ovl_connect_layer(struct dentry *dentry) | |
106 | { | |
107 | struct dentry *next, *parent = NULL; | |
5522c9c7 | 108 | struct ovl_entry *oe = OVL_E(dentry); |
2ca3c148 AG |
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 | ||
5522c9c7 | 116 | origin_layer = ovl_lowerstack(oe)->layer->idx; |
2ca3c148 AG |
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 | ||
b305e844 AG |
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 | |
05e1f118 AG |
171 | * Indexed upper | L (*) |
172 | * Non-upper | L (*) | |
b305e844 AG |
173 | * |
174 | * U = upper file handle | |
175 | * L = lower file handle | |
05e1f118 | 176 | * |
16aac5ad | 177 | * (*) Decoding a connected overlay dir from real lower dentry is not always |
2ca3c148 AG |
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 | |
16aac5ad | 180 | * of a decodable file handle for non-upper dir. |
2ca3c148 AG |
181 | * |
182 | * Return 0 for upper file handle, > 0 for lower file handle or < 0 on error. | |
b305e844 | 183 | */ |
2ca3c148 | 184 | static int ovl_check_encode_origin(struct dentry *dentry) |
b305e844 | 185 | { |
f01d0889 | 186 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb); |
16aac5ad AG |
187 | bool decodable = ofs->config.nfs_export; |
188 | ||
189 | /* Lower file handle for non-upper non-decodable */ | |
190 | if (!ovl_dentry_upper(dentry) && !decodable) | |
c7242a45 | 191 | return 1; |
05e1f118 | 192 | |
2ca3c148 | 193 | /* Upper file handle for pure upper */ |
b305e844 | 194 | if (!ovl_dentry_lower(dentry)) |
2ca3c148 | 195 | return 0; |
b305e844 | 196 | |
05e1f118 | 197 | /* |
2ca3c148 AG |
198 | * Root is never indexed, so if there's an upper layer, encode upper for |
199 | * root. | |
05e1f118 | 200 | */ |
16aac5ad AG |
201 | if (dentry == dentry->d_sb->s_root) |
202 | return 0; | |
203 | ||
204 | /* | |
205 | * Upper decodable file handle for non-indexed upper. | |
206 | */ | |
207 | if (ovl_dentry_upper(dentry) && decodable && | |
b305e844 | 208 | !ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
05e1f118 AG |
209 | return 0; |
210 | ||
2ca3c148 AG |
211 | /* |
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. | |
216 | */ | |
16aac5ad | 217 | if (d_is_dir(dentry) && ovl_upper_mnt(ofs) && decodable) |
2ca3c148 | 218 | return ovl_connect_layer(dentry); |
05e1f118 | 219 | |
2ca3c148 AG |
220 | /* Lower file handle for indexed and non-upper dir/non-dir */ |
221 | return 1; | |
05e1f118 AG |
222 | } |
223 | ||
1cdb0cb6 PT |
224 | static int ovl_dentry_to_fid(struct ovl_fs *ofs, struct dentry *dentry, |
225 | u32 *fid, int buflen) | |
8ed5eec9 | 226 | { |
8ed5eec9 | 227 | struct ovl_fh *fh = NULL; |
2ca3c148 | 228 | int err, enc_lower; |
cbe7fba8 | 229 | int len; |
8ed5eec9 | 230 | |
05e1f118 | 231 | /* |
2ca3c148 AG |
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. | |
05e1f118 | 234 | */ |
2ca3c148 AG |
235 | err = enc_lower = ovl_check_encode_origin(dentry); |
236 | if (enc_lower < 0) | |
237 | goto fail; | |
8ed5eec9 | 238 | |
2ca3c148 | 239 | /* Encode an upper or lower file handle */ |
1cdb0cb6 | 240 | fh = ovl_encode_real_fh(ofs, enc_lower ? ovl_dentry_lower(dentry) : |
5b2cccd3 | 241 | ovl_dentry_upper(dentry), !enc_lower); |
9b6faee0 | 242 | if (IS_ERR(fh)) |
97f024b9 | 243 | return PTR_ERR(fh); |
8ed5eec9 | 244 | |
cbe7fba8 | 245 | len = OVL_FH_LEN(fh); |
144da23b LD |
246 | if (len <= buflen) |
247 | memcpy(fid, fh, len); | |
cbe7fba8 | 248 | err = len; |
8ed5eec9 AG |
249 | |
250 | out: | |
251 | kfree(fh); | |
252 | return err; | |
253 | ||
254 | fail: | |
144da23b LD |
255 | pr_warn_ratelimited("failed to encode file handle (%pd2, err=%i)\n", |
256 | dentry, err); | |
8ed5eec9 AG |
257 | goto out; |
258 | } | |
259 | ||
5b2cccd3 AG |
260 | static int ovl_encode_fh(struct inode *inode, u32 *fid, int *max_len, |
261 | struct inode *parent) | |
8ed5eec9 | 262 | { |
1cdb0cb6 | 263 | struct ovl_fs *ofs = OVL_FS(inode->i_sb); |
8ed5eec9 | 264 | struct dentry *dentry; |
144da23b | 265 | int bytes, buflen = *max_len << 2; |
8ed5eec9 AG |
266 | |
267 | /* TODO: encode connectable file handles */ | |
268 | if (parent) | |
269 | return FILEID_INVALID; | |
270 | ||
271 | dentry = d_find_any_alias(inode); | |
dd524b7f | 272 | if (!dentry) |
8ed5eec9 AG |
273 | return FILEID_INVALID; |
274 | ||
1cdb0cb6 | 275 | bytes = ovl_dentry_to_fid(ofs, dentry, fid, buflen); |
8ed5eec9 | 276 | dput(dentry); |
cbe7fba8 AG |
277 | if (bytes <= 0) |
278 | return FILEID_INVALID; | |
279 | ||
280 | *max_len = bytes >> 2; | |
144da23b LD |
281 | if (bytes > buflen) |
282 | return FILEID_INVALID; | |
cbe7fba8 AG |
283 | |
284 | return OVL_FILEID_V1; | |
8ed5eec9 AG |
285 | } |
286 | ||
8556a420 | 287 | /* |
f71bd9cf | 288 | * Find or instantiate an overlay dentry from real dentries and index. |
8556a420 AG |
289 | */ |
290 | static struct dentry *ovl_obtain_alias(struct super_block *sb, | |
f71bd9cf AG |
291 | struct dentry *upper_alias, |
292 | struct ovl_path *lowerpath, | |
293 | struct dentry *index) | |
8556a420 | 294 | { |
f941866f | 295 | struct dentry *lower = lowerpath ? lowerpath->dentry : NULL; |
f71bd9cf | 296 | struct dentry *upper = upper_alias ?: index; |
8556a420 | 297 | struct dentry *dentry; |
0af950f5 | 298 | struct inode *inode = NULL; |
8556a420 | 299 | struct ovl_entry *oe; |
ac6a52eb | 300 | struct ovl_inode_params oip = { |
ac6a52eb | 301 | .index = index, |
ac6a52eb | 302 | }; |
8556a420 | 303 | |
f71bd9cf AG |
304 | /* We get overlay directory dentries with ovl_lookup_real() */ |
305 | if (d_is_dir(upper ?: lower)) | |
8556a420 AG |
306 | return ERR_PTR(-EIO); |
307 | ||
0af950f5 AG |
308 | oe = ovl_alloc_entry(!!lower); |
309 | if (!oe) | |
310 | return ERR_PTR(-ENOMEM); | |
311 | ||
ac6a52eb | 312 | oip.upperdentry = dget(upper); |
0af950f5 AG |
313 | if (lower) { |
314 | ovl_lowerstack(oe)->dentry = dget(lower); | |
315 | ovl_lowerstack(oe)->layer = lowerpath->layer; | |
316 | } | |
317 | oip.oe = oe; | |
ac6a52eb | 318 | inode = ovl_get_inode(sb, &oip); |
8556a420 | 319 | if (IS_ERR(inode)) { |
0af950f5 | 320 | ovl_free_entry(oe); |
8556a420 AG |
321 | dput(upper); |
322 | return ERR_CAST(inode); | |
323 | } | |
324 | ||
9d3dfea3 VG |
325 | if (upper) |
326 | ovl_set_flag(OVL_UPPERDATA, inode); | |
327 | ||
8556a420 | 328 | dentry = d_find_any_alias(inode); |
504f3841 AV |
329 | if (dentry) |
330 | goto out_iput; | |
331 | ||
332 | dentry = d_alloc_anon(inode->i_sb); | |
333 | if (unlikely(!dentry)) | |
334 | goto nomem; | |
504f3841 | 335 | |
504f3841 AV |
336 | if (upper_alias) |
337 | ovl_dentry_set_upper_alias(dentry); | |
338 | ||
0af950f5 | 339 | ovl_dentry_init_reval(dentry, upper, OVL_I_E(inode)); |
8556a420 AG |
340 | |
341 | return d_instantiate_anon(dentry, inode); | |
342 | ||
343 | nomem: | |
8556a420 | 344 | dput(dentry); |
504f3841 AV |
345 | dentry = ERR_PTR(-ENOMEM); |
346 | out_iput: | |
347 | iput(inode); | |
348 | return dentry; | |
8556a420 AG |
349 | } |
350 | ||
cdf5c9d1 | 351 | /* Get the upper or lower dentry in stack whose on layer @idx */ |
98892516 AG |
352 | static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx) |
353 | { | |
a6ff2bc0 | 354 | struct ovl_entry *oe = OVL_E(dentry); |
5522c9c7 | 355 | struct ovl_path *lowerstack = ovl_lowerstack(oe); |
98892516 AG |
356 | int i; |
357 | ||
358 | if (!idx) | |
359 | return ovl_dentry_upper(dentry); | |
360 | ||
5522c9c7 AG |
361 | for (i = 0; i < ovl_numlower(oe); i++) { |
362 | if (lowerstack[i].layer->idx == idx) | |
363 | return lowerstack[i].dentry; | |
98892516 AG |
364 | } |
365 | ||
366 | return NULL; | |
367 | } | |
368 | ||
3985b70a AG |
369 | /* |
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 | |
373 | * index for lookup. | |
374 | */ | |
375 | static struct dentry *ovl_lookup_real_one(struct dentry *connected, | |
376 | struct dentry *real, | |
13464165 | 377 | const struct ovl_layer *layer) |
3985b70a AG |
378 | { |
379 | struct inode *dir = d_inode(connected); | |
380 | struct dentry *this, *parent = NULL; | |
381 | struct name_snapshot name; | |
382 | int err; | |
383 | ||
3985b70a AG |
384 | /* |
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. | |
390 | */ | |
391 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
392 | err = -ECHILD; | |
393 | parent = dget_parent(real); | |
98892516 | 394 | if (ovl_dentry_real_at(connected, layer->idx) != parent) |
3985b70a AG |
395 | goto fail; |
396 | ||
397 | /* | |
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. | |
402 | */ | |
403 | take_dentry_name_snapshot(&name, real); | |
ba9ea771 | 404 | /* |
4609e1f1 CB |
405 | * No idmap handling here: it's an internal lookup. Could skip |
406 | * permission checking altogether, but for now just use non-idmap | |
ba9ea771 CB |
407 | * transformed ids. |
408 | */ | |
230c6402 | 409 | this = lookup_one_len(name.name.name, connected, name.name.len); |
580c6104 | 410 | release_dentry_name_snapshot(&name); |
3985b70a AG |
411 | err = PTR_ERR(this); |
412 | if (IS_ERR(this)) { | |
413 | goto fail; | |
414 | } else if (!this || !this->d_inode) { | |
415 | dput(this); | |
416 | err = -ENOENT; | |
417 | goto fail; | |
98892516 | 418 | } else if (ovl_dentry_real_at(this, layer->idx) != real) { |
3985b70a AG |
419 | dput(this); |
420 | err = -ESTALE; | |
421 | goto fail; | |
422 | } | |
423 | ||
424 | out: | |
3985b70a AG |
425 | dput(parent); |
426 | inode_unlock(dir); | |
427 | return this; | |
428 | ||
429 | fail: | |
1bd0a3ae | 430 | pr_warn_ratelimited("failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", |
3985b70a AG |
431 | real, layer->idx, connected, err); |
432 | this = ERR_PTR(err); | |
433 | goto out; | |
434 | } | |
435 | ||
06170154 AG |
436 | static struct dentry *ovl_lookup_real(struct super_block *sb, |
437 | struct dentry *real, | |
13464165 | 438 | const struct ovl_layer *layer); |
06170154 | 439 | |
4b91c30a AG |
440 | /* |
441 | * Lookup an indexed or hashed overlay dentry by real inode. | |
442 | */ | |
443 | static struct dentry *ovl_lookup_real_inode(struct super_block *sb, | |
444 | struct dentry *real, | |
13464165 | 445 | const struct ovl_layer *layer) |
4b91c30a | 446 | { |
f01d0889 | 447 | struct ovl_fs *ofs = OVL_FS(sb); |
06170154 | 448 | struct dentry *index = NULL; |
4b91c30a AG |
449 | struct dentry *this = NULL; |
450 | struct inode *inode; | |
451 | ||
06170154 AG |
452 | /* |
453 | * Decoding upper dir from index is expensive, so first try to lookup | |
454 | * overlay dentry in inode/dcache. | |
455 | */ | |
4b91c30a AG |
456 | inode = ovl_lookup_inode(sb, real, !layer->idx); |
457 | if (IS_ERR(inode)) | |
458 | return ERR_CAST(inode); | |
459 | if (inode) { | |
460 | this = d_find_any_alias(inode); | |
461 | iput(inode); | |
462 | } | |
463 | ||
06170154 AG |
464 | /* |
465 | * For decoded lower dir file handle, lookup index by origin to check | |
466 | * if lower dir was copied up and and/or removed. | |
467 | */ | |
468 | if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) { | |
469 | index = ovl_lookup_index(ofs, NULL, real, false); | |
470 | if (IS_ERR(index)) | |
471 | return index; | |
472 | } | |
473 | ||
474 | /* Get connected upper overlay dir from index */ | |
475 | if (index) { | |
8ea28765 | 476 | struct dentry *upper = ovl_index_upper(ofs, index, true); |
06170154 AG |
477 | |
478 | dput(index); | |
479 | if (IS_ERR_OR_NULL(upper)) | |
480 | return upper; | |
481 | ||
482 | /* | |
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). | |
488 | */ | |
94375f9d | 489 | this = ovl_lookup_real(sb, upper, &ofs->layers[0]); |
06170154 AG |
490 | dput(upper); |
491 | } | |
492 | ||
7168179f AG |
493 | if (IS_ERR_OR_NULL(this)) |
494 | return this; | |
4b91c30a | 495 | |
124c2de2 | 496 | if (ovl_dentry_real_at(this, layer->idx) != real) { |
4b91c30a AG |
497 | dput(this); |
498 | this = ERR_PTR(-EIO); | |
499 | } | |
500 | ||
501 | return this; | |
502 | } | |
503 | ||
504 | /* | |
505 | * Lookup an indexed or hashed overlay dentry, whose real dentry is an | |
506 | * ancestor of @real. | |
507 | */ | |
508 | static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb, | |
509 | struct dentry *real, | |
13464165 | 510 | const struct ovl_layer *layer) |
4b91c30a AG |
511 | { |
512 | struct dentry *next, *parent = NULL; | |
513 | struct dentry *ancestor = ERR_PTR(-EIO); | |
514 | ||
515 | if (real == layer->mnt->mnt_root) | |
516 | return dget(sb->s_root); | |
517 | ||
518 | /* Find the topmost indexed or hashed ancestor */ | |
519 | next = dget(real); | |
520 | for (;;) { | |
521 | parent = dget_parent(next); | |
522 | ||
523 | /* | |
524 | * Lookup a matching overlay dentry in inode/dentry | |
525 | * cache or in index by real inode. | |
526 | */ | |
527 | ancestor = ovl_lookup_real_inode(sb, next, layer); | |
528 | if (ancestor) | |
529 | break; | |
530 | ||
531 | if (parent == layer->mnt->mnt_root) { | |
532 | ancestor = dget(sb->s_root); | |
533 | break; | |
534 | } | |
535 | ||
536 | /* | |
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. | |
540 | */ | |
541 | if (parent == next) { | |
542 | ancestor = ERR_PTR(-EXDEV); | |
543 | break; | |
544 | } | |
545 | ||
546 | dput(next); | |
547 | next = parent; | |
548 | } | |
549 | ||
550 | dput(parent); | |
551 | dput(next); | |
552 | ||
553 | return ancestor; | |
554 | } | |
555 | ||
3985b70a AG |
556 | /* |
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. | |
560 | */ | |
561 | static struct dentry *ovl_lookup_real(struct super_block *sb, | |
562 | struct dentry *real, | |
13464165 | 563 | const struct ovl_layer *layer) |
3985b70a AG |
564 | { |
565 | struct dentry *connected; | |
566 | int err = 0; | |
567 | ||
4b91c30a AG |
568 | connected = ovl_lookup_real_ancestor(sb, real, layer); |
569 | if (IS_ERR(connected)) | |
570 | return connected; | |
3985b70a | 571 | |
3985b70a AG |
572 | while (!err) { |
573 | struct dentry *next, *this; | |
574 | struct dentry *parent = NULL; | |
98892516 AG |
575 | struct dentry *real_connected = ovl_dentry_real_at(connected, |
576 | layer->idx); | |
3985b70a AG |
577 | |
578 | if (real_connected == real) | |
579 | break; | |
580 | ||
581 | /* Find the topmost dentry not yet connected */ | |
582 | next = dget(real); | |
583 | for (;;) { | |
584 | parent = dget_parent(next); | |
585 | ||
586 | if (parent == real_connected) | |
587 | break; | |
588 | ||
589 | /* | |
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. | |
596 | */ | |
597 | if (parent == layer->mnt->mnt_root) { | |
598 | dput(connected); | |
599 | connected = dget(sb->s_root); | |
600 | break; | |
601 | } | |
602 | ||
603 | /* | |
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. | |
608 | */ | |
609 | if (parent == next) { | |
610 | err = -EXDEV; | |
611 | break; | |
612 | } | |
613 | ||
614 | dput(next); | |
615 | next = parent; | |
616 | } | |
617 | ||
618 | if (!err) { | |
619 | this = ovl_lookup_real_one(connected, next, layer); | |
620 | if (IS_ERR(this)) | |
621 | err = PTR_ERR(this); | |
622 | ||
623 | /* | |
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 | |
4b91c30a AG |
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. | |
3985b70a AG |
632 | */ |
633 | if (err == -ECHILD) { | |
4b91c30a AG |
634 | this = ovl_lookup_real_ancestor(sb, real, |
635 | layer); | |
b5095f24 | 636 | err = PTR_ERR_OR_ZERO(this); |
3985b70a AG |
637 | } |
638 | if (!err) { | |
639 | dput(connected); | |
640 | connected = this; | |
641 | } | |
642 | } | |
643 | ||
644 | dput(parent); | |
645 | dput(next); | |
646 | } | |
647 | ||
648 | if (err) | |
649 | goto fail; | |
650 | ||
651 | return connected; | |
652 | ||
653 | fail: | |
1bd0a3ae | 654 | pr_warn_ratelimited("failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", |
3985b70a AG |
655 | real, layer->idx, connected, err); |
656 | dput(connected); | |
657 | return ERR_PTR(err); | |
658 | } | |
659 | ||
660 | /* | |
f71bd9cf | 661 | * Get an overlay dentry from upper/lower real dentries and index. |
3985b70a AG |
662 | */ |
663 | static struct dentry *ovl_get_dentry(struct super_block *sb, | |
664 | struct dentry *upper, | |
f71bd9cf AG |
665 | struct ovl_path *lowerpath, |
666 | struct dentry *index) | |
3985b70a | 667 | { |
f01d0889 | 668 | struct ovl_fs *ofs = OVL_FS(sb); |
13464165 | 669 | const struct ovl_layer *layer = upper ? &ofs->layers[0] : lowerpath->layer; |
f71bd9cf | 670 | struct dentry *real = upper ?: (index ?: lowerpath->dentry); |
3985b70a | 671 | |
f941866f | 672 | /* |
f71bd9cf AG |
673 | * Obtain a disconnected overlay dentry from a non-dir real dentry |
674 | * and index. | |
f941866f | 675 | */ |
f71bd9cf AG |
676 | if (!d_is_dir(real)) |
677 | return ovl_obtain_alias(sb, upper, lowerpath, index); | |
f941866f | 678 | |
3985b70a | 679 | /* Removed empty directory? */ |
98892516 | 680 | if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real)) |
3985b70a AG |
681 | return ERR_PTR(-ENOENT); |
682 | ||
683 | /* | |
98892516 AG |
684 | * If real dentry is connected and hashed, get a connected overlay |
685 | * dentry whose real dentry is @real. | |
3985b70a | 686 | */ |
98892516 | 687 | return ovl_lookup_real(sb, real, layer); |
3985b70a AG |
688 | } |
689 | ||
8556a420 AG |
690 | static struct dentry *ovl_upper_fh_to_d(struct super_block *sb, |
691 | struct ovl_fh *fh) | |
692 | { | |
f01d0889 | 693 | struct ovl_fs *ofs = OVL_FS(sb); |
8556a420 AG |
694 | struct dentry *dentry; |
695 | struct dentry *upper; | |
696 | ||
08f4c7c8 | 697 | if (!ovl_upper_mnt(ofs)) |
8556a420 AG |
698 | return ERR_PTR(-EACCES); |
699 | ||
1cdb0cb6 | 700 | upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), true); |
8556a420 AG |
701 | if (IS_ERR_OR_NULL(upper)) |
702 | return upper; | |
703 | ||
f71bd9cf | 704 | dentry = ovl_get_dentry(sb, upper, NULL, NULL); |
8556a420 AG |
705 | dput(upper); |
706 | ||
707 | return dentry; | |
708 | } | |
709 | ||
f941866f AG |
710 | static struct dentry *ovl_lower_fh_to_d(struct super_block *sb, |
711 | struct ovl_fh *fh) | |
712 | { | |
f01d0889 | 713 | struct ovl_fs *ofs = OVL_FS(sb); |
f941866f AG |
714 | struct ovl_path origin = { }; |
715 | struct ovl_path *stack = &origin; | |
716 | struct dentry *dentry = NULL; | |
f71bd9cf | 717 | struct dentry *index = NULL; |
8b58924a | 718 | struct inode *inode; |
f941866f AG |
719 | int err; |
720 | ||
8b58924a AG |
721 | /* First lookup overlay inode in inode cache by origin fh */ |
722 | err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack); | |
723 | if (err) | |
724 | return ERR_PTR(err); | |
725 | ||
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); | |
730 | if (IS_ERR(inode)) | |
731 | goto out_err; | |
732 | if (inode) { | |
733 | dentry = d_find_any_alias(inode); | |
734 | iput(inode); | |
735 | if (dentry) | |
736 | goto out; | |
737 | } | |
738 | } | |
739 | ||
740 | /* Then lookup indexed upper/whiteout by origin fh */ | |
f71bd9cf AG |
741 | if (ofs->indexdir) { |
742 | index = ovl_get_index_fh(ofs, fh); | |
743 | err = PTR_ERR(index); | |
9436a1a3 | 744 | if (IS_ERR(index)) { |
9436a1a3 | 745 | index = NULL; |
8b58924a | 746 | goto out_err; |
9436a1a3 | 747 | } |
f71bd9cf AG |
748 | } |
749 | ||
8b58924a | 750 | /* Then try to get a connected upper dir by index */ |
3b0bfc6e | 751 | if (index && d_is_dir(index)) { |
8ea28765 | 752 | struct dentry *upper = ovl_index_upper(ofs, index, true); |
3b0bfc6e AG |
753 | |
754 | err = PTR_ERR(upper); | |
755 | if (IS_ERR_OR_NULL(upper)) | |
756 | goto out_err; | |
757 | ||
758 | dentry = ovl_get_dentry(sb, upper, NULL, NULL); | |
759 | dput(upper); | |
760 | goto out; | |
761 | } | |
762 | ||
155b8a04 AG |
763 | /* Find origin.dentry again with ovl_acceptable() layer check */ |
764 | if (d_is_dir(origin.dentry)) { | |
8b58924a AG |
765 | dput(origin.dentry); |
766 | origin.dentry = NULL; | |
767 | err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack); | |
f71bd9cf AG |
768 | if (err) |
769 | goto out_err; | |
8b58924a AG |
770 | } |
771 | if (index) { | |
610afc0b | 772 | err = ovl_verify_origin(ofs, index, origin.dentry, false); |
8b58924a | 773 | if (err) |
9436a1a3 | 774 | goto out_err; |
f71bd9cf | 775 | } |
f941866f | 776 | |
155b8a04 | 777 | /* Get a connected non-upper dir or disconnected non-dir */ |
f71bd9cf | 778 | dentry = ovl_get_dentry(sb, NULL, &origin, index); |
f941866f | 779 | |
f71bd9cf AG |
780 | out: |
781 | dput(origin.dentry); | |
782 | dput(index); | |
f941866f | 783 | return dentry; |
f71bd9cf AG |
784 | |
785 | out_err: | |
786 | dentry = ERR_PTR(err); | |
787 | goto out; | |
f941866f AG |
788 | } |
789 | ||
cbe7fba8 AG |
790 | static struct ovl_fh *ovl_fid_to_fh(struct fid *fid, int buflen, int fh_type) |
791 | { | |
792 | struct ovl_fh *fh; | |
793 | ||
794 | /* If on-wire inner fid is aligned - nothing to do */ | |
795 | if (fh_type == OVL_FILEID_V1) | |
796 | return (struct ovl_fh *)fid; | |
797 | ||
798 | if (fh_type != OVL_FILEID_V0) | |
799 | return ERR_PTR(-EINVAL); | |
800 | ||
9aafc1b0 DC |
801 | if (buflen <= OVL_FH_WIRE_OFFSET) |
802 | return ERR_PTR(-EINVAL); | |
803 | ||
cbe7fba8 AG |
804 | fh = kzalloc(buflen, GFP_KERNEL); |
805 | if (!fh) | |
806 | return ERR_PTR(-ENOMEM); | |
807 | ||
808 | /* Copy unaligned inner fh into aligned buffer */ | |
cf8aa9bf | 809 | memcpy(fh->buf, fid, buflen - OVL_FH_WIRE_OFFSET); |
cbe7fba8 AG |
810 | return fh; |
811 | } | |
812 | ||
8556a420 AG |
813 | static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid, |
814 | int fh_len, int fh_type) | |
815 | { | |
816 | struct dentry *dentry = NULL; | |
cbe7fba8 | 817 | struct ovl_fh *fh = NULL; |
8556a420 AG |
818 | int len = fh_len << 2; |
819 | unsigned int flags = 0; | |
820 | int err; | |
821 | ||
cbe7fba8 AG |
822 | fh = ovl_fid_to_fh(fid, len, fh_type); |
823 | err = PTR_ERR(fh); | |
824 | if (IS_ERR(fh)) | |
8556a420 AG |
825 | goto out_err; |
826 | ||
827 | err = ovl_check_fh_len(fh, len); | |
828 | if (err) | |
829 | goto out_err; | |
830 | ||
cbe7fba8 | 831 | flags = fh->fb.flags; |
f941866f AG |
832 | dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ? |
833 | ovl_upper_fh_to_d(sb, fh) : | |
834 | ovl_lower_fh_to_d(sb, fh); | |
8556a420 AG |
835 | err = PTR_ERR(dentry); |
836 | if (IS_ERR(dentry) && err != -ESTALE) | |
837 | goto out_err; | |
838 | ||
cbe7fba8 AG |
839 | out: |
840 | /* We may have needed to re-align OVL_FILEID_V0 */ | |
841 | if (!IS_ERR_OR_NULL(fh) && fh != (void *)fid) | |
842 | kfree(fh); | |
843 | ||
8556a420 AG |
844 | return dentry; |
845 | ||
846 | out_err: | |
1bd0a3ae | 847 | pr_warn_ratelimited("failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n", |
cbe7fba8 AG |
848 | fh_len, fh_type, flags, err); |
849 | dentry = ERR_PTR(err); | |
850 | goto out; | |
8556a420 AG |
851 | } |
852 | ||
3985b70a AG |
853 | static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid, |
854 | int fh_len, int fh_type) | |
855 | { | |
1bd0a3ae | 856 | pr_warn_ratelimited("connectable file handles not supported; use 'no_subtree_check' exportfs option.\n"); |
3985b70a AG |
857 | return ERR_PTR(-EACCES); |
858 | } | |
859 | ||
860 | static int ovl_get_name(struct dentry *parent, char *name, | |
861 | struct dentry *child) | |
862 | { | |
863 | /* | |
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. | |
866 | */ | |
867 | WARN_ON_ONCE(1); | |
868 | return -EIO; | |
869 | } | |
870 | ||
871 | static struct dentry *ovl_get_parent(struct dentry *dentry) | |
872 | { | |
873 | /* | |
874 | * ovl_fh_to_dentry() returns connected dir overlay dentries, so we | |
875 | * should not get here. | |
876 | */ | |
877 | WARN_ON_ONCE(1); | |
878 | return ERR_PTR(-EIO); | |
879 | } | |
880 | ||
8ed5eec9 | 881 | const struct export_operations ovl_export_operations = { |
5b2cccd3 | 882 | .encode_fh = ovl_encode_fh, |
8556a420 | 883 | .fh_to_dentry = ovl_fh_to_dentry, |
3985b70a AG |
884 | .fh_to_parent = ovl_fh_to_parent, |
885 | .get_name = ovl_get_name, | |
886 | .get_parent = ovl_get_parent, | |
8ed5eec9 | 887 | }; |
16aac5ad AG |
888 | |
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, | |
892 | }; |