Merge branch 'address-masking'
[linux-block.git] / fs / overlayfs / readdir.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
e9be9d5e
MS
2/*
3 *
4 * Copyright (C) 2011 Novell Inc.
e9be9d5e
MS
5 */
6
7#include <linux/fs.h>
8#include <linux/slab.h>
9#include <linux/namei.h>
10#include <linux/file.h>
11#include <linux/xattr.h>
12#include <linux/rbtree.h>
13#include <linux/security.h>
14#include <linux/cred.h>
b5efccbe 15#include <linux/ratelimit.h>
e9be9d5e
MS
16#include "overlayfs.h"
17
18struct ovl_cache_entry {
e9be9d5e
MS
19 unsigned int len;
20 unsigned int type;
b5efccbe 21 u64 real_ino;
e9be9d5e 22 u64 ino;
e9be9d5e
MS
23 struct list_head l_node;
24 struct rb_node node;
cdb67279 25 struct ovl_cache_entry *next_maybe_whiteout;
95e598e7 26 bool is_upper;
c2096537 27 bool is_whiteout;
bc8df7a3 28 bool check_xwhiteout;
68bf8611 29 char name[];
e9be9d5e
MS
30};
31
32struct ovl_dir_cache {
33 long refcount;
34 u64 version;
35 struct list_head entries;
4edb83bb 36 struct rb_root root;
e9be9d5e
MS
37};
38
39struct ovl_readdir_data {
40 struct dir_context ctx;
3fe6e52f 41 struct dentry *dentry;
56656e96 42 bool is_lowest;
4edb83bb 43 struct rb_root *root;
e9be9d5e 44 struct list_head *list;
db6ec212 45 struct list_head middle;
cdb67279 46 struct ovl_cache_entry *first_maybe_whiteout;
e9be9d5e
MS
47 int count;
48 int err;
b5efccbe 49 bool is_upper;
45aebeaf 50 bool d_type_supported;
bc8df7a3 51 bool in_xwhiteouts_dir;
e9be9d5e
MS
52};
53
54struct ovl_dir_file {
55 bool is_real;
56 bool is_upper;
57 struct ovl_dir_cache *cache;
4330397e 58 struct list_head *cursor;
e9be9d5e
MS
59 struct file *realfile;
60 struct file *upperfile;
61};
62
63static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
64{
4edb83bb
MS
65 return rb_entry(n, struct ovl_cache_entry, node);
66}
67
68static bool ovl_cache_entry_find_link(const char *name, int len,
69 struct rb_node ***link,
70 struct rb_node **parent)
71{
72 bool found = false;
73 struct rb_node **newp = *link;
74
75 while (!found && *newp) {
76 int cmp;
77 struct ovl_cache_entry *tmp;
78
79 *parent = *newp;
80 tmp = ovl_cache_entry_from_node(*newp);
81 cmp = strncmp(name, tmp->name, len);
82 if (cmp > 0)
83 newp = &tmp->node.rb_right;
84 else if (cmp < 0 || len < tmp->len)
85 newp = &tmp->node.rb_left;
86 else
87 found = true;
88 }
89 *link = newp;
90
91 return found;
e9be9d5e
MS
92}
93
94static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
95 const char *name, int len)
96{
97 struct rb_node *node = root->rb_node;
98 int cmp;
99
100 while (node) {
101 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
102
103 cmp = strncmp(name, p->name, len);
104 if (cmp > 0)
105 node = p->node.rb_right;
106 else if (cmp < 0 || len < p->len)
107 node = p->node.rb_left;
108 else
109 return p;
110 }
111
112 return NULL;
113}
114
b5efccbe
AG
115static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd,
116 struct ovl_cache_entry *p)
117{
118 /* Don't care if not doing ovl_iter() */
119 if (!rdd->dentry)
120 return false;
121
adbf4f7e 122 /* Always recalc d_ino when remapping lower inode numbers */
dcb399de 123 if (ovl_xino_bits(OVL_FS(rdd->dentry->d_sb)))
adbf4f7e
AG
124 return true;
125
b5efccbe
AG
126 /* Always recalc d_ino for parent */
127 if (strcmp(p->name, "..") == 0)
128 return true;
129
130 /* If this is lower, then native d_ino will do */
131 if (!rdd->is_upper)
132 return false;
133
134 /*
135 * Recalc d_ino for '.' and for all entries if dir is impure (contains
136 * copied up entries)
137 */
138 if ((p->name[0] == '.' && p->len == 1) ||
139 ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
140 return true;
141
142 return false;
143}
144
cdb67279 145static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
3e01cee3 146 const char *name, int len,
e9be9d5e
MS
147 u64 ino, unsigned int d_type)
148{
149 struct ovl_cache_entry *p;
68bf8611 150 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
e9be9d5e 151
68bf8611 152 p = kmalloc(size, GFP_KERNEL);
3e01cee3
MS
153 if (!p)
154 return NULL;
155
156 memcpy(p->name, name, len);
157 p->name[len] = '\0';
158 p->len = len;
159 p->type = d_type;
b5efccbe 160 p->real_ino = ino;
3e01cee3 161 p->ino = ino;
b5efccbe
AG
162 /* Defer setting d_ino for upper entry to ovl_iterate() */
163 if (ovl_calc_d_ino(rdd, p))
164 p->ino = 0;
95e598e7 165 p->is_upper = rdd->is_upper;
3e01cee3 166 p->is_whiteout = false;
bc8df7a3
AL
167 /* Defer check for overlay.whiteout to ovl_iterate() */
168 p->check_xwhiteout = rdd->in_xwhiteouts_dir && d_type == DT_REG;
3e01cee3
MS
169
170 if (d_type == DT_CHR) {
cdb67279
MS
171 p->next_maybe_whiteout = rdd->first_maybe_whiteout;
172 rdd->first_maybe_whiteout = p;
3e01cee3 173 }
e9be9d5e
MS
174 return p;
175}
176
25885a35 177static bool ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
e9be9d5e
MS
178 const char *name, int len, u64 ino,
179 unsigned int d_type)
180{
4edb83bb 181 struct rb_node **newp = &rdd->root->rb_node;
e9be9d5e
MS
182 struct rb_node *parent = NULL;
183 struct ovl_cache_entry *p;
184
4edb83bb 185 if (ovl_cache_entry_find_link(name, len, &newp, &parent))
25885a35 186 return true;
e9be9d5e 187
cdb67279 188 p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
31e8ccea
MS
189 if (p == NULL) {
190 rdd->err = -ENOMEM;
25885a35 191 return false;
31e8ccea 192 }
e9be9d5e
MS
193
194 list_add_tail(&p->l_node, rdd->list);
195 rb_link_node(&p->node, parent, newp);
4edb83bb 196 rb_insert_color(&p->node, rdd->root);
e9be9d5e 197
25885a35 198 return true;
e9be9d5e
MS
199}
200
25885a35 201static bool ovl_fill_lowest(struct ovl_readdir_data *rdd,
56656e96
MS
202 const char *name, int namelen,
203 loff_t offset, u64 ino, unsigned int d_type)
e9be9d5e
MS
204{
205 struct ovl_cache_entry *p;
206
4edb83bb 207 p = ovl_cache_entry_find(rdd->root, name, namelen);
e9be9d5e 208 if (p) {
db6ec212 209 list_move_tail(&p->l_node, &rdd->middle);
e9be9d5e 210 } else {
cdb67279 211 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
e9be9d5e
MS
212 if (p == NULL)
213 rdd->err = -ENOMEM;
214 else
db6ec212 215 list_add_tail(&p->l_node, &rdd->middle);
e9be9d5e
MS
216 }
217
25885a35 218 return rdd->err == 0;
e9be9d5e
MS
219}
220
221void ovl_cache_free(struct list_head *list)
222{
223 struct ovl_cache_entry *p;
224 struct ovl_cache_entry *n;
225
226 list_for_each_entry_safe(p, n, list, l_node)
227 kfree(p);
228
229 INIT_LIST_HEAD(list);
230}
231
4edb83bb
MS
232void ovl_dir_cache_free(struct inode *inode)
233{
234 struct ovl_dir_cache *cache = ovl_dir_cache(inode);
235
236 if (cache) {
237 ovl_cache_free(&cache->entries);
238 kfree(cache);
239 }
240}
241
1fa9c5c5 242static void ovl_cache_put(struct ovl_dir_file *od, struct inode *inode)
e9be9d5e
MS
243{
244 struct ovl_dir_cache *cache = od->cache;
245
e9be9d5e
MS
246 WARN_ON(cache->refcount <= 0);
247 cache->refcount--;
248 if (!cache->refcount) {
1fa9c5c5
MS
249 if (ovl_dir_cache(inode) == cache)
250 ovl_set_dir_cache(inode, NULL);
e9be9d5e
MS
251
252 ovl_cache_free(&cache->entries);
253 kfree(cache);
254 }
255}
256
25885a35 257static bool ovl_fill_merge(struct dir_context *ctx, const char *name,
ac7576f4
MS
258 int namelen, loff_t offset, u64 ino,
259 unsigned int d_type)
e9be9d5e 260{
ac7576f4
MS
261 struct ovl_readdir_data *rdd =
262 container_of(ctx, struct ovl_readdir_data, ctx);
e9be9d5e
MS
263
264 rdd->count++;
56656e96 265 if (!rdd->is_lowest)
e9be9d5e
MS
266 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
267 else
56656e96 268 return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
e9be9d5e
MS
269}
270
2d343087 271static int ovl_check_whiteouts(const struct path *path, struct ovl_readdir_data *rdd)
cdb67279
MS
272{
273 int err;
274 struct ovl_cache_entry *p;
ba9ea771 275 struct dentry *dentry, *dir = path->dentry;
cdb67279 276 const struct cred *old_cred;
cdb67279 277
3fe6e52f 278 old_cred = ovl_override_creds(rdd->dentry->d_sb);
cdb67279 279
00235411 280 err = down_write_killable(&dir->d_inode->i_rwsem);
cdb67279
MS
281 if (!err) {
282 while (rdd->first_maybe_whiteout) {
283 p = rdd->first_maybe_whiteout;
284 rdd->first_maybe_whiteout = p->next_maybe_whiteout;
4609e1f1 285 dentry = lookup_one(mnt_idmap(path->mnt), p->name, dir, p->len);
cdb67279
MS
286 if (!IS_ERR(dentry)) {
287 p->is_whiteout = ovl_is_whiteout(dentry);
288 dput(dentry);
289 }
290 }
5955102c 291 inode_unlock(dir->d_inode);
cdb67279
MS
292 }
293 revert_creds(old_cred);
cdb67279
MS
294
295 return err;
296}
297
2d343087 298static inline int ovl_dir_read(const struct path *realpath,
e9be9d5e
MS
299 struct ovl_readdir_data *rdd)
300{
301 struct file *realfile;
302 int err;
303
130fdbc3 304 realfile = ovl_path_open(realpath, O_RDONLY | O_LARGEFILE);
e9be9d5e
MS
305 if (IS_ERR(realfile))
306 return PTR_ERR(realfile);
307
cdb67279 308 rdd->first_maybe_whiteout = NULL;
e9be9d5e
MS
309 rdd->ctx.pos = 0;
310 do {
311 rdd->count = 0;
312 rdd->err = 0;
313 err = iterate_dir(realfile, &rdd->ctx);
314 if (err >= 0)
315 err = rdd->err;
316 } while (!err && rdd->count);
cdb67279 317
eea2fb48 318 if (!err && rdd->first_maybe_whiteout && rdd->dentry)
ba9ea771 319 err = ovl_check_whiteouts(realpath, rdd);
cdb67279 320
e9be9d5e
MS
321 fput(realfile);
322
323 return err;
324}
325
326static void ovl_dir_reset(struct file *file)
327{
328 struct ovl_dir_file *od = file->private_data;
329 struct ovl_dir_cache *cache = od->cache;
1fa9c5c5 330 struct inode *inode = file_inode(file);
b79e05aa 331 bool is_real;
e9be9d5e 332
1fa9c5c5
MS
333 if (cache && ovl_inode_version_get(inode) != cache->version) {
334 ovl_cache_put(od, inode);
e9be9d5e 335 od->cache = NULL;
4330397e 336 od->cursor = NULL;
e9be9d5e 337 }
1fa9c5c5 338 is_real = ovl_dir_is_real(inode);
b79e05aa
AG
339 if (od->is_real != is_real) {
340 /* is_real can only become false when dir is copied up */
341 if (WARN_ON(is_real))
342 return;
e9be9d5e 343 od->is_real = false;
b79e05aa 344 }
e9be9d5e
MS
345}
346
4edb83bb
MS
347static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list,
348 struct rb_root *root)
e9be9d5e
MS
349{
350 int err;
9d7459d8 351 struct path realpath;
e9be9d5e
MS
352 struct ovl_readdir_data rdd = {
353 .ctx.actor = ovl_fill_merge,
3fe6e52f 354 .dentry = dentry,
e9be9d5e 355 .list = list,
4edb83bb 356 .root = root,
56656e96 357 .is_lowest = false,
e9be9d5e 358 };
9d7459d8 359 int idx, next;
420332b9 360 const struct ovl_layer *layer;
e9be9d5e 361
9d7459d8 362 for (idx = 0; idx != -1; idx = next) {
420332b9 363 next = ovl_path_next(idx, dentry, &realpath, &layer);
b5efccbe 364 rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry;
420332b9
AG
365 rdd.in_xwhiteouts_dir = layer->has_xwhiteouts &&
366 ovl_dentry_has_xwhiteouts(dentry);
c9f00fdb 367
9d7459d8 368 if (next != -1) {
9d7459d8 369 err = ovl_dir_read(&realpath, &rdd);
e9be9d5e 370 if (err)
9d7459d8
MS
371 break;
372 } else {
373 /*
374 * Insert lowest layer entries before upper ones, this
375 * allows offsets to be reasonably constant
376 */
377 list_add(&rdd.middle, rdd.list);
56656e96 378 rdd.is_lowest = true;
9d7459d8
MS
379 err = ovl_dir_read(&realpath, &rdd);
380 list_del(&rdd.middle);
e9be9d5e
MS
381 }
382 }
e9be9d5e 383 return err;
e9be9d5e
MS
384}
385
386static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
387{
4330397e 388 struct list_head *p;
e9be9d5e
MS
389 loff_t off = 0;
390
4330397e 391 list_for_each(p, &od->cache->entries) {
e9be9d5e
MS
392 if (off >= pos)
393 break;
394 off++;
395 }
4330397e 396 /* Cursor is safe since the cache is stable */
397 od->cursor = p;
e9be9d5e
MS
398}
399
400static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
401{
402 int res;
e9be9d5e 403 struct ovl_dir_cache *cache;
1fa9c5c5 404 struct inode *inode = d_inode(dentry);
e9be9d5e 405
1fa9c5c5
MS
406 cache = ovl_dir_cache(inode);
407 if (cache && ovl_inode_version_get(inode) == cache->version) {
4edb83bb 408 WARN_ON(!cache->refcount);
e9be9d5e
MS
409 cache->refcount++;
410 return cache;
411 }
4edb83bb 412 ovl_set_dir_cache(d_inode(dentry), NULL);
e9be9d5e
MS
413
414 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
415 if (!cache)
416 return ERR_PTR(-ENOMEM);
417
418 cache->refcount = 1;
419 INIT_LIST_HEAD(&cache->entries);
4edb83bb 420 cache->root = RB_ROOT;
e9be9d5e 421
4edb83bb 422 res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root);
e9be9d5e
MS
423 if (res) {
424 ovl_cache_free(&cache->entries);
425 kfree(cache);
426 return ERR_PTR(res);
427 }
428
1fa9c5c5
MS
429 cache->version = ovl_inode_version_get(inode);
430 ovl_set_dir_cache(inode, cache);
e9be9d5e
MS
431
432 return cache;
433}
434
adbf4f7e
AG
435/* Map inode number to lower fs unique range */
436static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid,
926e94d7 437 const char *name, int namelen, bool warn)
adbf4f7e 438{
dfe51d47
AG
439 unsigned int xinoshift = 64 - xinobits;
440
441 if (unlikely(ino >> xinoshift)) {
926e94d7
AG
442 if (warn) {
443 pr_warn_ratelimited("d_ino too big (%.*s, ino=%llu, xinobits=%d)\n",
444 namelen, name, ino, xinobits);
445 }
adbf4f7e
AG
446 return ino;
447 }
448
dfe51d47
AG
449 /*
450 * The lowest xinobit is reserved for mapping the non-peresistent inode
451 * numbers range, but this range is only exposed via st_ino, not here.
452 */
453 return ino | ((u64)fsid) << (xinoshift + 1);
adbf4f7e
AG
454}
455
b5efccbe 456/*
bc8df7a3 457 * Set d_ino for upper entries if needed. Non-upper entries should always report
b5efccbe
AG
458 * the uppermost real inode ino and should not call this function.
459 *
460 * When not all layer are on same fs, report real ino also for upper.
461 *
462 * When all layers are on the same fs, and upper has a reference to
463 * copy up origin, call vfs_getattr() on the overlay entry to make
464 * sure that d_ino will be consistent with st_ino from stat(2).
bc8df7a3
AL
465 *
466 * Also checks the overlay.whiteout xattr by doing a full lookup which will return
467 * negative in this case.
b5efccbe 468 */
bc8df7a3 469static int ovl_cache_update(const struct path *path, struct ovl_cache_entry *p, bool update_ino)
b5efccbe
AG
470
471{
472 struct dentry *dir = path->dentry;
dcb399de 473 struct ovl_fs *ofs = OVL_FS(dir->d_sb);
b5efccbe
AG
474 struct dentry *this = NULL;
475 enum ovl_path_type type;
476 u64 ino = p->real_ino;
dcb399de 477 int xinobits = ovl_xino_bits(ofs);
b5efccbe
AG
478 int err = 0;
479
bc8df7a3 480 if (!ovl_same_dev(ofs) && !p->check_xwhiteout)
b5efccbe
AG
481 goto out;
482
483 if (p->name[0] == '.') {
484 if (p->len == 1) {
485 this = dget(dir);
486 goto get;
487 }
488 if (p->len == 2 && p->name[1] == '.') {
489 /* we shall not be moved */
490 this = dget(dir->d_parent);
491 goto get;
492 }
493 }
bc8df7a3 494 /* This checks also for xwhiteouts */
4609e1f1 495 this = lookup_one(mnt_idmap(path->mnt), p->name, dir, p->len);
b5efccbe 496 if (IS_ERR_OR_NULL(this) || !this->d_inode) {
9011c279
AG
497 /* Mark a stale entry */
498 p->is_whiteout = true;
b5efccbe
AG
499 if (IS_ERR(this)) {
500 err = PTR_ERR(this);
501 this = NULL;
502 goto fail;
503 }
504 goto out;
505 }
506
507get:
bc8df7a3
AL
508 if (!ovl_same_dev(ofs) || !update_ino)
509 goto out;
510
b5efccbe
AG
511 type = ovl_path_type(this);
512 if (OVL_TYPE_ORIGIN(type)) {
513 struct kstat stat;
514 struct path statpath = *path;
515
516 statpath.dentry = this;
517 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
518 if (err)
519 goto fail;
520
4c37e71b
AG
521 /*
522 * Directory inode is always on overlay st_dev.
523 * Non-dir with ovl_same_dev() could be on pseudo st_dev in case
524 * of xino bits overflow.
525 */
526 WARN_ON_ONCE(S_ISDIR(stat.mode) &&
527 dir->d_sb->s_dev != stat.dev);
b5efccbe 528 ino = stat.ino;
adbf4f7e
AG
529 } else if (xinobits && !OVL_TYPE_UPPER(type)) {
530 ino = ovl_remap_lower_ino(ino, xinobits,
531 ovl_layer_lower(this)->fsid,
926e94d7 532 p->name, p->len,
dcb399de 533 ovl_xino_warn(ofs));
b5efccbe
AG
534 }
535
536out:
537 p->ino = ino;
538 dput(this);
539 return err;
540
541fail:
1bd0a3ae 542 pr_warn_ratelimited("failed to look up (%s) for ino (%i)\n",
b5efccbe
AG
543 p->name, err);
544 goto out;
545}
546
25885a35 547static bool ovl_fill_plain(struct dir_context *ctx, const char *name,
4edb83bb
MS
548 int namelen, loff_t offset, u64 ino,
549 unsigned int d_type)
550{
551 struct ovl_cache_entry *p;
552 struct ovl_readdir_data *rdd =
553 container_of(ctx, struct ovl_readdir_data, ctx);
554
555 rdd->count++;
556 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
557 if (p == NULL) {
558 rdd->err = -ENOMEM;
25885a35 559 return false;
4edb83bb
MS
560 }
561 list_add_tail(&p->l_node, rdd->list);
562
25885a35 563 return true;
4edb83bb
MS
564}
565
2d343087 566static int ovl_dir_read_impure(const struct path *path, struct list_head *list,
4edb83bb
MS
567 struct rb_root *root)
568{
569 int err;
570 struct path realpath;
571 struct ovl_cache_entry *p, *n;
572 struct ovl_readdir_data rdd = {
573 .ctx.actor = ovl_fill_plain,
574 .list = list,
575 .root = root,
576 };
577
578 INIT_LIST_HEAD(list);
579 *root = RB_ROOT;
580 ovl_path_upper(path->dentry, &realpath);
581
582 err = ovl_dir_read(&realpath, &rdd);
583 if (err)
584 return err;
585
586 list_for_each_entry_safe(p, n, list, l_node) {
587 if (strcmp(p->name, ".") != 0 &&
588 strcmp(p->name, "..") != 0) {
bc8df7a3 589 err = ovl_cache_update(path, p, true);
4edb83bb
MS
590 if (err)
591 return err;
592 }
593 if (p->ino == p->real_ino) {
594 list_del(&p->l_node);
595 kfree(p);
596 } else {
597 struct rb_node **newp = &root->rb_node;
598 struct rb_node *parent = NULL;
599
600 if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
601 &newp, &parent)))
602 return -EIO;
603
604 rb_link_node(&p->node, parent, newp);
605 rb_insert_color(&p->node, root);
606 }
607 }
608 return 0;
609}
610
2d343087 611static struct ovl_dir_cache *ovl_cache_get_impure(const struct path *path)
4edb83bb
MS
612{
613 int res;
614 struct dentry *dentry = path->dentry;
1fa9c5c5 615 struct inode *inode = d_inode(dentry);
610afc0b 616 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
4edb83bb
MS
617 struct ovl_dir_cache *cache;
618
1fa9c5c5
MS
619 cache = ovl_dir_cache(inode);
620 if (cache && ovl_inode_version_get(inode) == cache->version)
4edb83bb
MS
621 return cache;
622
623 /* Impure cache is not refcounted, free it here */
1fa9c5c5
MS
624 ovl_dir_cache_free(inode);
625 ovl_set_dir_cache(inode, NULL);
4edb83bb
MS
626
627 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
628 if (!cache)
629 return ERR_PTR(-ENOMEM);
630
631 res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
632 if (res) {
633 ovl_cache_free(&cache->entries);
634 kfree(cache);
635 return ERR_PTR(res);
636 }
637 if (list_empty(&cache->entries)) {
a5a927a7
AG
638 /*
639 * A good opportunity to get rid of an unneeded "impure" flag.
640 * Removing the "impure" xattr is best effort.
641 */
642 if (!ovl_want_write(dentry)) {
c914c0e2
AG
643 ovl_removexattr(ofs, ovl_dentry_upper(dentry),
644 OVL_XATTR_IMPURE);
a5a927a7
AG
645 ovl_drop_write(dentry);
646 }
1fa9c5c5 647 ovl_clear_flag(OVL_IMPURE, inode);
4edb83bb
MS
648 kfree(cache);
649 return NULL;
650 }
651
1fa9c5c5
MS
652 cache->version = ovl_inode_version_get(inode);
653 ovl_set_dir_cache(inode, cache);
4edb83bb
MS
654
655 return cache;
656}
657
658struct ovl_readdir_translate {
659 struct dir_context *orig_ctx;
660 struct ovl_dir_cache *cache;
661 struct dir_context ctx;
662 u64 parent_ino;
adbf4f7e
AG
663 int fsid;
664 int xinobits;
926e94d7 665 bool xinowarn;
4edb83bb
MS
666};
667
25885a35 668static bool ovl_fill_real(struct dir_context *ctx, const char *name,
4edb83bb
MS
669 int namelen, loff_t offset, u64 ino,
670 unsigned int d_type)
671{
672 struct ovl_readdir_translate *rdt =
673 container_of(ctx, struct ovl_readdir_translate, ctx);
674 struct dir_context *orig_ctx = rdt->orig_ctx;
675
adbf4f7e 676 if (rdt->parent_ino && strcmp(name, "..") == 0) {
4edb83bb 677 ino = rdt->parent_ino;
adbf4f7e 678 } else if (rdt->cache) {
4edb83bb
MS
679 struct ovl_cache_entry *p;
680
681 p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
682 if (p)
683 ino = p->ino;
adbf4f7e
AG
684 } else if (rdt->xinobits) {
685 ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid,
926e94d7 686 name, namelen, rdt->xinowarn);
4edb83bb
MS
687 }
688
689 return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
690}
691
67810693
AG
692static bool ovl_is_impure_dir(struct file *file)
693{
694 struct ovl_dir_file *od = file->private_data;
1fa9c5c5 695 struct inode *dir = file_inode(file);
67810693
AG
696
697 /*
698 * Only upper dir can be impure, but if we are in the middle of
699 * iterating a lower real dir, dir could be copied up and marked
700 * impure. We only want the impure cache if we started iterating
701 * a real upper dir to begin with.
702 */
703 return od->is_upper && ovl_test_flag(OVL_IMPURE, dir);
704
705}
706
4edb83bb
MS
707static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
708{
709 int err;
710 struct ovl_dir_file *od = file->private_data;
711 struct dentry *dir = file->f_path.dentry;
dcb399de 712 struct ovl_fs *ofs = OVL_FS(dir->d_sb);
13464165 713 const struct ovl_layer *lower_layer = ovl_layer_lower(dir);
4edb83bb
MS
714 struct ovl_readdir_translate rdt = {
715 .ctx.actor = ovl_fill_real,
716 .orig_ctx = ctx,
dcb399de
AG
717 .xinobits = ovl_xino_bits(ofs),
718 .xinowarn = ovl_xino_warn(ofs),
4edb83bb
MS
719 };
720
adbf4f7e
AG
721 if (rdt.xinobits && lower_layer)
722 rdt.fsid = lower_layer->fsid;
723
4edb83bb
MS
724 if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
725 struct kstat stat;
726 struct path statpath = file->f_path;
727
728 statpath.dentry = dir->d_parent;
729 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
730 if (err)
731 return err;
732
733 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
734 rdt.parent_ino = stat.ino;
735 }
736
67810693 737 if (ovl_is_impure_dir(file)) {
4edb83bb
MS
738 rdt.cache = ovl_cache_get_impure(&file->f_path);
739 if (IS_ERR(rdt.cache))
740 return PTR_ERR(rdt.cache);
741 }
742
b02a16e6
AG
743 err = iterate_dir(od->realfile, &rdt.ctx);
744 ctx->pos = rdt.ctx.pos;
745
746 return err;
4edb83bb
MS
747}
748
749
e9be9d5e
MS
750static int ovl_iterate(struct file *file, struct dir_context *ctx)
751{
752 struct ovl_dir_file *od = file->private_data;
753 struct dentry *dentry = file->f_path.dentry;
dcb399de 754 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
4330397e 755 struct ovl_cache_entry *p;
48bd024b 756 const struct cred *old_cred;
b5efccbe 757 int err;
e9be9d5e 758
48bd024b 759 old_cred = ovl_override_creds(dentry->d_sb);
e9be9d5e
MS
760 if (!ctx->pos)
761 ovl_dir_reset(file);
762
4edb83bb
MS
763 if (od->is_real) {
764 /*
765 * If parent is merge, then need to adjust d_ino for '..', if
766 * dir is impure then need to adjust d_ino for copied up
767 * entries.
768 */
dcb399de
AG
769 if (ovl_xino_bits(ofs) ||
770 (ovl_same_fs(ofs) &&
67810693 771 (ovl_is_impure_dir(file) ||
adbf4f7e 772 OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) {
48bd024b
MS
773 err = ovl_iterate_real(file, ctx);
774 } else {
775 err = iterate_dir(od->realfile, ctx);
4edb83bb 776 }
48bd024b 777 goto out;
4edb83bb 778 }
e9be9d5e
MS
779
780 if (!od->cache) {
781 struct ovl_dir_cache *cache;
782
783 cache = ovl_cache_get(dentry);
48bd024b 784 err = PTR_ERR(cache);
e9be9d5e 785 if (IS_ERR(cache))
48bd024b 786 goto out;
e9be9d5e
MS
787
788 od->cache = cache;
789 ovl_seek_cursor(od, ctx->pos);
790 }
791
4330397e 792 while (od->cursor != &od->cache->entries) {
793 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
b5efccbe 794 if (!p->is_whiteout) {
bc8df7a3
AL
795 if (!p->ino || p->check_xwhiteout) {
796 err = ovl_cache_update(&file->f_path, p, !p->ino);
b5efccbe 797 if (err)
48bd024b 798 goto out;
b5efccbe 799 }
9011c279 800 }
bc8df7a3 801 /* ovl_cache_update() sets is_whiteout on stale entry */
9011c279 802 if (!p->is_whiteout) {
4330397e 803 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
804 break;
b5efccbe 805 }
4330397e 806 od->cursor = p->l_node.next;
807 ctx->pos++;
e9be9d5e 808 }
48bd024b
MS
809 err = 0;
810out:
811 revert_creds(old_cred);
812 return err;
e9be9d5e
MS
813}
814
815static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
816{
817 loff_t res;
818 struct ovl_dir_file *od = file->private_data;
819
5955102c 820 inode_lock(file_inode(file));
e9be9d5e
MS
821 if (!file->f_pos)
822 ovl_dir_reset(file);
823
824 if (od->is_real) {
825 res = vfs_llseek(od->realfile, offset, origin);
826 file->f_pos = od->realfile->f_pos;
827 } else {
828 res = -EINVAL;
829
830 switch (origin) {
831 case SEEK_CUR:
832 offset += file->f_pos;
833 break;
834 case SEEK_SET:
835 break;
836 default:
837 goto out_unlock;
838 }
839 if (offset < 0)
840 goto out_unlock;
841
842 if (offset != file->f_pos) {
843 file->f_pos = offset;
844 if (od->cache)
845 ovl_seek_cursor(od, offset);
846 }
847 res = offset;
848 }
849out_unlock:
5955102c 850 inode_unlock(file_inode(file));
e9be9d5e
MS
851
852 return res;
853}
854
61536bed 855static struct file *ovl_dir_open_realfile(const struct file *file,
2d343087 856 const struct path *realpath)
130fdbc3 857{
48bd024b
MS
858 struct file *res;
859 const struct cred *old_cred;
860
861 old_cred = ovl_override_creds(file_inode(file)->i_sb);
862 res = ovl_path_open(realpath, O_RDONLY | (file->f_flags & O_LARGEFILE));
863 revert_creds(old_cred);
864
865 return res;
130fdbc3
MS
866}
867
61536bed
AG
868/*
869 * Like ovl_real_fdget(), returns upperfile if dir was copied up since open.
870 * Unlike ovl_real_fdget(), this caches upperfile in file->private_data.
871 *
872 * TODO: use same abstract type for file->private_data of dir and file so
873 * upperfile could also be cached for files as well.
874 */
875struct file *ovl_dir_real_file(const struct file *file, bool want_upper)
e9be9d5e 876{
61536bed 877
e9be9d5e
MS
878 struct ovl_dir_file *od = file->private_data;
879 struct dentry *dentry = file->f_path.dentry;
b854cc65 880 struct file *old, *realfile = od->realfile;
e9be9d5e 881
d796e77f 882 if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
61536bed 883 return want_upper ? NULL : realfile;
c86243b0 884
e9be9d5e
MS
885 /*
886 * Need to check if we started out being a lower dir, but got copied up
887 */
d796e77f 888 if (!od->is_upper) {
506458ef 889 realfile = READ_ONCE(od->upperfile);
e9be9d5e
MS
890 if (!realfile) {
891 struct path upperpath;
892
893 ovl_path_upper(dentry, &upperpath);
130fdbc3 894 realfile = ovl_dir_open_realfile(file, &upperpath);
b854cc65
MS
895 if (IS_ERR(realfile))
896 return realfile;
ff7a5fb0 897
b854cc65
MS
898 old = cmpxchg_release(&od->upperfile, NULL, realfile);
899 if (old) {
900 fput(realfile);
901 realfile = old;
e9be9d5e 902 }
e9be9d5e 903 }
e9be9d5e
MS
904 }
905
61536bed
AG
906 return realfile;
907}
908
909static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
910 int datasync)
911{
912 struct file *realfile;
913 int err;
914
1fa9c5c5 915 err = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
335d3fc5
SD
916 if (err <= 0)
917 return err;
61536bed
AG
918
919 realfile = ovl_dir_real_file(file, true);
920 err = PTR_ERR_OR_ZERO(realfile);
921
922 /* Nothing to sync for lower */
923 if (!realfile || err)
924 return err;
925
e9be9d5e
MS
926 return vfs_fsync_range(realfile, start, end, datasync);
927}
928
929static int ovl_dir_release(struct inode *inode, struct file *file)
930{
931 struct ovl_dir_file *od = file->private_data;
932
933 if (od->cache) {
5955102c 934 inode_lock(inode);
1fa9c5c5 935 ovl_cache_put(od, inode);
5955102c 936 inode_unlock(inode);
e9be9d5e
MS
937 }
938 fput(od->realfile);
939 if (od->upperfile)
940 fput(od->upperfile);
941 kfree(od);
942
943 return 0;
944}
945
946static int ovl_dir_open(struct inode *inode, struct file *file)
947{
948 struct path realpath;
949 struct file *realfile;
950 struct ovl_dir_file *od;
951 enum ovl_path_type type;
952
953 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
954 if (!od)
955 return -ENOMEM;
956
957 type = ovl_path_real(file->f_path.dentry, &realpath);
130fdbc3 958 realfile = ovl_dir_open_realfile(file, &realpath);
e9be9d5e
MS
959 if (IS_ERR(realfile)) {
960 kfree(od);
961 return PTR_ERR(realfile);
962 }
e9be9d5e 963 od->realfile = realfile;
1fa9c5c5 964 od->is_real = ovl_dir_is_real(inode);
1afaba1e 965 od->is_upper = OVL_TYPE_UPPER(type);
e9be9d5e
MS
966 file->private_data = od;
967
968 return 0;
969}
970
3e327154 971WRAP_DIR_ITER(ovl_iterate) // FIXME!
e9be9d5e
MS
972const struct file_operations ovl_dir_operations = {
973 .read = generic_read_dir,
974 .open = ovl_dir_open,
3e327154 975 .iterate_shared = shared_ovl_iterate,
e9be9d5e
MS
976 .llseek = ovl_dir_llseek,
977 .fsync = ovl_dir_fsync,
978 .release = ovl_dir_release,
979};
980
981int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
982{
983 int err;
95e598e7 984 struct ovl_cache_entry *p, *n;
4edb83bb 985 struct rb_root root = RB_ROOT;
6d0a8a90 986 const struct cred *old_cred;
e9be9d5e 987
6d0a8a90 988 old_cred = ovl_override_creds(dentry->d_sb);
4edb83bb 989 err = ovl_dir_read_merged(dentry, list, &root);
6d0a8a90 990 revert_creds(old_cred);
e9be9d5e
MS
991 if (err)
992 return err;
993
994 err = 0;
995
95e598e7 996 list_for_each_entry_safe(p, n, list, l_node) {
997 /*
998 * Select whiteouts in upperdir, they should
999 * be cleared when deleting this directory.
1000 */
1001 if (p->is_whiteout) {
1002 if (p->is_upper)
1003 continue;
1004 goto del_entry;
1005 }
e9be9d5e
MS
1006
1007 if (p->name[0] == '.') {
1008 if (p->len == 1)
95e598e7 1009 goto del_entry;
e9be9d5e 1010 if (p->len == 2 && p->name[1] == '.')
95e598e7 1011 goto del_entry;
e9be9d5e
MS
1012 }
1013 err = -ENOTEMPTY;
1014 break;
95e598e7 1015
1016del_entry:
1017 list_del(&p->l_node);
1018 kfree(p);
e9be9d5e
MS
1019 }
1020
1021 return err;
1022}
1023
576bb263
CB
1024void ovl_cleanup_whiteouts(struct ovl_fs *ofs, struct dentry *upper,
1025 struct list_head *list)
e9be9d5e
MS
1026{
1027 struct ovl_cache_entry *p;
1028
5955102c 1029 inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
e9be9d5e
MS
1030 list_for_each_entry(p, list, l_node) {
1031 struct dentry *dentry;
1032
95e598e7 1033 if (WARN_ON(!p->is_whiteout || !p->is_upper))
e9be9d5e
MS
1034 continue;
1035
22f289ce 1036 dentry = ovl_lookup_upper(ofs, p->name, upper, p->len);
e9be9d5e 1037 if (IS_ERR(dentry)) {
1bd0a3ae 1038 pr_err("lookup '%s/%.*s' failed (%i)\n",
e9be9d5e
MS
1039 upper->d_name.name, p->len, p->name,
1040 (int) PTR_ERR(dentry));
1041 continue;
1042 }
84889d49 1043 if (dentry->d_inode)
576bb263 1044 ovl_cleanup(ofs, upper->d_inode, dentry);
e9be9d5e
MS
1045 dput(dentry);
1046 }
5955102c 1047 inode_unlock(upper->d_inode);
e9be9d5e 1048}
45aebeaf 1049
25885a35 1050static bool ovl_check_d_type(struct dir_context *ctx, const char *name,
45aebeaf
VG
1051 int namelen, loff_t offset, u64 ino,
1052 unsigned int d_type)
1053{
1054 struct ovl_readdir_data *rdd =
1055 container_of(ctx, struct ovl_readdir_data, ctx);
1056
1057 /* Even if d_type is not supported, DT_DIR is returned for . and .. */
1058 if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
25885a35 1059 return true;
45aebeaf
VG
1060
1061 if (d_type != DT_UNKNOWN)
1062 rdd->d_type_supported = true;
1063
25885a35 1064 return true;
45aebeaf
VG
1065}
1066
1067/*
1068 * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
1069 * if error is encountered.
1070 */
2d343087 1071int ovl_check_d_type_supported(const struct path *realpath)
45aebeaf
VG
1072{
1073 int err;
1074 struct ovl_readdir_data rdd = {
1075 .ctx.actor = ovl_check_d_type,
1076 .d_type_supported = false,
1077 };
1078
1079 err = ovl_dir_read(realpath, &rdd);
1080 if (err)
1081 return err;
1082
1083 return rdd.d_type_supported;
1084}
eea2fb48 1085
235ce9ed
AG
1086#define OVL_INCOMPATDIR_NAME "incompat"
1087
2d343087 1088static int ovl_workdir_cleanup_recurse(struct ovl_fs *ofs, const struct path *path,
576bb263 1089 int level)
eea2fb48
MS
1090{
1091 int err;
1092 struct inode *dir = path->dentry->d_inode;
1093 LIST_HEAD(list);
1094 struct ovl_cache_entry *p;
1095 struct ovl_readdir_data rdd = {
af4dcb6d 1096 .ctx.actor = ovl_fill_plain,
eea2fb48 1097 .list = &list,
eea2fb48 1098 };
235ce9ed
AG
1099 bool incompat = false;
1100
1101 /*
1102 * The "work/incompat" directory is treated specially - if it is not
1103 * empty, instead of printing a generic error and mounting read-only,
1104 * we will error about incompat features and fail the mount.
1105 *
1106 * When called from ovl_indexdir_cleanup(), path->dentry->d_name.name
1107 * starts with '#'.
1108 */
1109 if (level == 2 &&
1110 !strcmp(path->dentry->d_name.name, OVL_INCOMPATDIR_NAME))
1111 incompat = true;
eea2fb48
MS
1112
1113 err = ovl_dir_read(path, &rdd);
1114 if (err)
1115 goto out;
1116
1117 inode_lock_nested(dir, I_MUTEX_PARENT);
1118 list_for_each_entry(p, &list, l_node) {
1119 struct dentry *dentry;
1120
1121 if (p->name[0] == '.') {
1122 if (p->len == 1)
1123 continue;
1124 if (p->len == 2 && p->name[1] == '.')
1125 continue;
235ce9ed
AG
1126 } else if (incompat) {
1127 pr_err("overlay with incompat feature '%s' cannot be mounted\n",
1128 p->name);
1129 err = -EINVAL;
1130 break;
eea2fb48 1131 }
22f289ce 1132 dentry = ovl_lookup_upper(ofs, p->name, path->dentry, p->len);
eea2fb48
MS
1133 if (IS_ERR(dentry))
1134 continue;
1135 if (dentry->d_inode)
576bb263 1136 err = ovl_workdir_cleanup(ofs, dir, path->mnt, dentry, level);
eea2fb48 1137 dput(dentry);
235ce9ed
AG
1138 if (err)
1139 break;
eea2fb48
MS
1140 }
1141 inode_unlock(dir);
1142out:
1143 ovl_cache_free(&list);
235ce9ed 1144 return err;
eea2fb48
MS
1145}
1146
576bb263
CB
1147int ovl_workdir_cleanup(struct ovl_fs *ofs, struct inode *dir,
1148 struct vfsmount *mnt, struct dentry *dentry, int level)
eea2fb48
MS
1149{
1150 int err;
1151
1152 if (!d_is_dir(dentry) || level > 1) {
576bb263 1153 return ovl_cleanup(ofs, dir, dentry);
eea2fb48
MS
1154 }
1155
576bb263 1156 err = ovl_do_rmdir(ofs, dir, dentry);
eea2fb48
MS
1157 if (err) {
1158 struct path path = { .mnt = mnt, .dentry = dentry };
1159
1160 inode_unlock(dir);
576bb263 1161 err = ovl_workdir_cleanup_recurse(ofs, &path, level + 1);
eea2fb48 1162 inode_lock_nested(dir, I_MUTEX_PARENT);
235ce9ed 1163 if (!err)
576bb263 1164 err = ovl_cleanup(ofs, dir, dentry);
eea2fb48 1165 }
3011645b
AG
1166
1167 return err;
eea2fb48 1168}
415543d5 1169
1eff1a1d 1170int ovl_indexdir_cleanup(struct ovl_fs *ofs)
415543d5
AG
1171{
1172 int err;
02d70090 1173 struct dentry *indexdir = ofs->workdir;
dc7ab677 1174 struct dentry *index = NULL;
1eff1a1d 1175 struct inode *dir = indexdir->d_inode;
08f4c7c8 1176 struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = indexdir };
415543d5
AG
1177 LIST_HEAD(list);
1178 struct ovl_cache_entry *p;
1179 struct ovl_readdir_data rdd = {
af4dcb6d 1180 .ctx.actor = ovl_fill_plain,
415543d5 1181 .list = &list,
415543d5
AG
1182 };
1183
1184 err = ovl_dir_read(&path, &rdd);
1185 if (err)
1186 goto out;
1187
1188 inode_lock_nested(dir, I_MUTEX_PARENT);
1189 list_for_each_entry(p, &list, l_node) {
415543d5
AG
1190 if (p->name[0] == '.') {
1191 if (p->len == 1)
1192 continue;
1193 if (p->len == 2 && p->name[1] == '.')
1194 continue;
1195 }
22f289ce 1196 index = ovl_lookup_upper(ofs, p->name, indexdir, p->len);
415543d5
AG
1197 if (IS_ERR(index)) {
1198 err = PTR_ERR(index);
dc7ab677 1199 index = NULL;
415543d5
AG
1200 break;
1201 }
3011645b
AG
1202 /* Cleanup leftover from index create/cleanup attempt */
1203 if (index->d_name.name[0] == '#') {
576bb263 1204 err = ovl_workdir_cleanup(ofs, dir, path.mnt, index, 1);
3011645b
AG
1205 if (err)
1206 break;
1207 goto next;
1208 }
1eff1a1d 1209 err = ovl_verify_index(ofs, index);
24f0b172
AG
1210 if (!err) {
1211 goto next;
1212 } else if (err == -ESTALE) {
1213 /* Cleanup stale index entries */
576bb263 1214 err = ovl_cleanup(ofs, dir, index);
24f0b172
AG
1215 } else if (err != -ENOENT) {
1216 /*
1217 * Abort mount to avoid corrupting the index if
1218 * an incompatible index entry was found or on out
1219 * of memory.
1220 */
1221 break;
1222 } else if (ofs->config.nfs_export) {
1223 /*
1224 * Whiteout orphan index to block future open by
1225 * handle after overlay nlink dropped to zero.
1226 */
c21c839b 1227 err = ovl_cleanup_and_whiteout(ofs, dir, index);
24f0b172
AG
1228 } else {
1229 /* Cleanup orphan index entries */
576bb263 1230 err = ovl_cleanup(ofs, dir, index);
24f0b172
AG
1231 }
1232
fa0096e3
AG
1233 if (err)
1234 break;
1235
24f0b172 1236next:
415543d5 1237 dput(index);
dc7ab677 1238 index = NULL;
415543d5 1239 }
dc7ab677 1240 dput(index);
415543d5
AG
1241 inode_unlock(dir);
1242out:
1243 ovl_cache_free(&list);
1244 if (err)
1bd0a3ae 1245 pr_err("failed index dir cleanup (%i)\n", err);
415543d5
AG
1246 return err;
1247}