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