LCOV - code coverage report
Current view: top level - fs/overlayfs - readdir.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 64 640 10.0 %
Date: 2021-04-22 12:43:58 Functions: 7 37 18.9 %

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

Generated by: LCOV version 1.14