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