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