]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e9be9d5e MS |
2 | /* |
3 | * | |
4 | * Copyright (C) 2011 Novell Inc. | |
e9be9d5e MS |
5 | */ |
6 | ||
7 | #include <linux/fs.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/namei.h> | |
10 | #include <linux/file.h> | |
11 | #include <linux/xattr.h> | |
12 | #include <linux/rbtree.h> | |
13 | #include <linux/security.h> | |
14 | #include <linux/cred.h> | |
b5efccbe | 15 | #include <linux/ratelimit.h> |
e9be9d5e MS |
16 | #include "overlayfs.h" |
17 | ||
18 | struct ovl_cache_entry { | |
e9be9d5e MS |
19 | unsigned int len; |
20 | unsigned int type; | |
b5efccbe | 21 | u64 real_ino; |
e9be9d5e | 22 | u64 ino; |
e9be9d5e MS |
23 | struct list_head l_node; |
24 | struct rb_node node; | |
cdb67279 | 25 | struct ovl_cache_entry *next_maybe_whiteout; |
95e598e7 | 26 | bool is_upper; |
c2096537 | 27 | bool is_whiteout; |
68bf8611 | 28 | char name[]; |
e9be9d5e MS |
29 | }; |
30 | ||
31 | struct ovl_dir_cache { | |
32 | long refcount; | |
33 | u64 version; | |
34 | struct list_head entries; | |
4edb83bb | 35 | struct rb_root root; |
e9be9d5e MS |
36 | }; |
37 | ||
38 | struct ovl_readdir_data { | |
39 | struct dir_context ctx; | |
3fe6e52f | 40 | struct dentry *dentry; |
56656e96 | 41 | bool is_lowest; |
4edb83bb | 42 | struct rb_root *root; |
e9be9d5e | 43 | struct list_head *list; |
db6ec212 | 44 | struct list_head middle; |
cdb67279 | 45 | struct ovl_cache_entry *first_maybe_whiteout; |
e9be9d5e MS |
46 | int count; |
47 | int err; | |
b5efccbe | 48 | bool is_upper; |
45aebeaf | 49 | bool d_type_supported; |
e9be9d5e MS |
50 | }; |
51 | ||
52 | struct ovl_dir_file { | |
53 | bool is_real; | |
54 | bool is_upper; | |
55 | struct ovl_dir_cache *cache; | |
4330397e | 56 | struct list_head *cursor; |
e9be9d5e MS |
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 | { | |
4edb83bb MS |
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; | |
e9be9d5e MS |
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 | ||
b5efccbe AG |
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 | ||
adbf4f7e AG |
120 | /* Always recalc d_ino when remapping lower inode numbers */ |
121 | if (ovl_xino_bits(rdd->dentry->d_sb)) | |
122 | return true; | |
123 | ||
b5efccbe AG |
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 | ||
cdb67279 | 143 | static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd, |
3e01cee3 | 144 | const char *name, int len, |
e9be9d5e MS |
145 | u64 ino, unsigned int d_type) |
146 | { | |
147 | struct ovl_cache_entry *p; | |
68bf8611 | 148 | size_t size = offsetof(struct ovl_cache_entry, name[len + 1]); |
e9be9d5e | 149 | |
68bf8611 | 150 | p = kmalloc(size, GFP_KERNEL); |
3e01cee3 MS |
151 | if (!p) |
152 | return NULL; | |
153 | ||
154 | memcpy(p->name, name, len); | |
155 | p->name[len] = '\0'; | |
156 | p->len = len; | |
157 | p->type = d_type; | |
b5efccbe | 158 | p->real_ino = ino; |
3e01cee3 | 159 | p->ino = ino; |
b5efccbe AG |
160 | /* Defer setting d_ino for upper entry to ovl_iterate() */ |
161 | if (ovl_calc_d_ino(rdd, p)) | |
162 | p->ino = 0; | |
95e598e7 | 163 | p->is_upper = rdd->is_upper; |
3e01cee3 | 164 | p->is_whiteout = false; |
3e01cee3 MS |
165 | |
166 | if (d_type == DT_CHR) { | |
cdb67279 MS |
167 | p->next_maybe_whiteout = rdd->first_maybe_whiteout; |
168 | rdd->first_maybe_whiteout = p; | |
3e01cee3 | 169 | } |
e9be9d5e MS |
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 | { | |
4edb83bb | 177 | struct rb_node **newp = &rdd->root->rb_node; |
e9be9d5e MS |
178 | struct rb_node *parent = NULL; |
179 | struct ovl_cache_entry *p; | |
180 | ||
4edb83bb MS |
181 | if (ovl_cache_entry_find_link(name, len, &newp, &parent)) |
182 | return 0; | |
e9be9d5e | 183 | |
cdb67279 | 184 | p = ovl_cache_entry_new(rdd, name, len, ino, d_type); |
31e8ccea MS |
185 | if (p == NULL) { |
186 | rdd->err = -ENOMEM; | |
e9be9d5e | 187 | return -ENOMEM; |
31e8ccea | 188 | } |
e9be9d5e MS |
189 | |
190 | list_add_tail(&p->l_node, rdd->list); | |
191 | rb_link_node(&p->node, parent, newp); | |
4edb83bb | 192 | rb_insert_color(&p->node, rdd->root); |
e9be9d5e MS |
193 | |
194 | return 0; | |
195 | } | |
196 | ||
56656e96 MS |
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) | |
e9be9d5e MS |
200 | { |
201 | struct ovl_cache_entry *p; | |
202 | ||
4edb83bb | 203 | p = ovl_cache_entry_find(rdd->root, name, namelen); |
e9be9d5e | 204 | if (p) { |
db6ec212 | 205 | list_move_tail(&p->l_node, &rdd->middle); |
e9be9d5e | 206 | } else { |
cdb67279 | 207 | p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type); |
e9be9d5e MS |
208 | if (p == NULL) |
209 | rdd->err = -ENOMEM; | |
210 | else | |
db6ec212 | 211 | list_add_tail(&p->l_node, &rdd->middle); |
e9be9d5e MS |
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 | ||
4edb83bb MS |
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 | ||
e9be9d5e MS |
238 | static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry) |
239 | { | |
240 | struct ovl_dir_cache *cache = od->cache; | |
241 | ||
e9be9d5e MS |
242 | WARN_ON(cache->refcount <= 0); |
243 | cache->refcount--; | |
244 | if (!cache->refcount) { | |
4edb83bb MS |
245 | if (ovl_dir_cache(d_inode(dentry)) == cache) |
246 | ovl_set_dir_cache(d_inode(dentry), NULL); | |
e9be9d5e MS |
247 | |
248 | ovl_cache_free(&cache->entries); | |
249 | kfree(cache); | |
250 | } | |
251 | } | |
252 | ||
ac7576f4 MS |
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) | |
e9be9d5e | 256 | { |
ac7576f4 MS |
257 | struct ovl_readdir_data *rdd = |
258 | container_of(ctx, struct ovl_readdir_data, ctx); | |
e9be9d5e MS |
259 | |
260 | rdd->count++; | |
56656e96 | 261 | if (!rdd->is_lowest) |
e9be9d5e MS |
262 | return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type); |
263 | else | |
56656e96 | 264 | return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type); |
e9be9d5e MS |
265 | } |
266 | ||
cdb67279 MS |
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; | |
cdb67279 | 273 | |
3fe6e52f | 274 | old_cred = ovl_override_creds(rdd->dentry->d_sb); |
cdb67279 | 275 | |
00235411 | 276 | err = down_write_killable(&dir->d_inode->i_rwsem); |
cdb67279 MS |
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 | } | |
5955102c | 287 | inode_unlock(dir->d_inode); |
cdb67279 MS |
288 | } |
289 | revert_creds(old_cred); | |
cdb67279 MS |
290 | |
291 | return err; | |
292 | } | |
293 | ||
e9be9d5e MS |
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 | ||
dcdcca98 | 300 | realfile = ovl_path_open(realpath, O_RDONLY | O_LARGEFILE); |
e9be9d5e MS |
301 | if (IS_ERR(realfile)) |
302 | return PTR_ERR(realfile); | |
303 | ||
cdb67279 | 304 | rdd->first_maybe_whiteout = NULL; |
e9be9d5e MS |
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); | |
cdb67279 | 313 | |
eea2fb48 | 314 | if (!err && rdd->first_maybe_whiteout && rdd->dentry) |
cdb67279 MS |
315 | err = ovl_check_whiteouts(realpath->dentry, rdd); |
316 | ||
e9be9d5e MS |
317 | fput(realfile); |
318 | ||
319 | return err; | |
320 | } | |
321 | ||
b79e05aa AG |
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 | ||
e9be9d5e MS |
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; | |
b79e05aa | 339 | bool is_real; |
e9be9d5e MS |
340 | |
341 | if (cache && ovl_dentry_version_get(dentry) != cache->version) { | |
342 | ovl_cache_put(od, dentry); | |
343 | od->cache = NULL; | |
4330397e | 344 | od->cursor = NULL; |
e9be9d5e | 345 | } |
b79e05aa AG |
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; | |
e9be9d5e | 351 | od->is_real = false; |
b79e05aa | 352 | } |
e9be9d5e MS |
353 | } |
354 | ||
4edb83bb MS |
355 | static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list, |
356 | struct rb_root *root) | |
e9be9d5e MS |
357 | { |
358 | int err; | |
9d7459d8 | 359 | struct path realpath; |
e9be9d5e MS |
360 | struct ovl_readdir_data rdd = { |
361 | .ctx.actor = ovl_fill_merge, | |
3fe6e52f | 362 | .dentry = dentry, |
e9be9d5e | 363 | .list = list, |
4edb83bb | 364 | .root = root, |
56656e96 | 365 | .is_lowest = false, |
e9be9d5e | 366 | }; |
9d7459d8 | 367 | int idx, next; |
e9be9d5e | 368 | |
9d7459d8 MS |
369 | for (idx = 0; idx != -1; idx = next) { |
370 | next = ovl_path_next(idx, dentry, &realpath); | |
b5efccbe | 371 | rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry; |
c9f00fdb | 372 | |
9d7459d8 | 373 | if (next != -1) { |
9d7459d8 | 374 | err = ovl_dir_read(&realpath, &rdd); |
e9be9d5e | 375 | if (err) |
9d7459d8 MS |
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); | |
56656e96 | 383 | rdd.is_lowest = true; |
9d7459d8 MS |
384 | err = ovl_dir_read(&realpath, &rdd); |
385 | list_del(&rdd.middle); | |
e9be9d5e MS |
386 | } |
387 | } | |
e9be9d5e | 388 | return err; |
e9be9d5e MS |
389 | } |
390 | ||
391 | static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos) | |
392 | { | |
4330397e | 393 | struct list_head *p; |
e9be9d5e MS |
394 | loff_t off = 0; |
395 | ||
4330397e | 396 | list_for_each(p, &od->cache->entries) { |
e9be9d5e MS |
397 | if (off >= pos) |
398 | break; | |
399 | off++; | |
400 | } | |
4330397e | 401 | /* Cursor is safe since the cache is stable */ |
402 | od->cursor = p; | |
e9be9d5e MS |
403 | } |
404 | ||
405 | static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry) | |
406 | { | |
407 | int res; | |
e9be9d5e MS |
408 | struct ovl_dir_cache *cache; |
409 | ||
4edb83bb | 410 | cache = ovl_dir_cache(d_inode(dentry)); |
e9be9d5e | 411 | if (cache && ovl_dentry_version_get(dentry) == cache->version) { |
4edb83bb | 412 | WARN_ON(!cache->refcount); |
e9be9d5e MS |
413 | cache->refcount++; |
414 | return cache; | |
415 | } | |
4edb83bb | 416 | ovl_set_dir_cache(d_inode(dentry), NULL); |
e9be9d5e MS |
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); | |
4edb83bb | 424 | cache->root = RB_ROOT; |
e9be9d5e | 425 | |
4edb83bb | 426 | res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root); |
e9be9d5e MS |
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); | |
4edb83bb | 434 | ovl_set_dir_cache(d_inode(dentry), cache); |
e9be9d5e MS |
435 | |
436 | return cache; | |
437 | } | |
438 | ||
adbf4f7e AG |
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 | ||
b5efccbe AG |
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; | |
adbf4f7e | 469 | int xinobits = ovl_xino_bits(dir->d_sb); |
b5efccbe AG |
470 | int err = 0; |
471 | ||
adbf4f7e | 472 | if (!ovl_same_sb(dir->d_sb) && !xinobits) |
b5efccbe AG |
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 | ||
07fa4af9 AG |
507 | /* |
508 | * Directory inode is always on overlay st_dev. | |
509 | * Non-dir with ovl_same_dev() could be on pseudo st_dev in case | |
510 | * of xino bits overflow. | |
511 | */ | |
512 | WARN_ON_ONCE(S_ISDIR(stat.mode) && | |
513 | dir->d_sb->s_dev != stat.dev); | |
b5efccbe | 514 | ino = stat.ino; |
adbf4f7e AG |
515 | } else if (xinobits && !OVL_TYPE_UPPER(type)) { |
516 | ino = ovl_remap_lower_ino(ino, xinobits, | |
517 | ovl_layer_lower(this)->fsid, | |
518 | p->name, p->len); | |
b5efccbe AG |
519 | } |
520 | ||
521 | out: | |
522 | p->ino = ino; | |
523 | dput(this); | |
524 | return err; | |
525 | ||
526 | fail: | |
da2e6b7e | 527 | pr_warn_ratelimited("overlayfs: failed to look up (%s) for ino (%i)\n", |
b5efccbe AG |
528 | p->name, err); |
529 | goto out; | |
530 | } | |
531 | ||
4edb83bb MS |
532 | static int ovl_fill_plain(struct dir_context *ctx, const char *name, |
533 | int namelen, loff_t offset, u64 ino, | |
534 | unsigned int d_type) | |
535 | { | |
536 | struct ovl_cache_entry *p; | |
537 | struct ovl_readdir_data *rdd = | |
538 | container_of(ctx, struct ovl_readdir_data, ctx); | |
539 | ||
540 | rdd->count++; | |
541 | p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type); | |
542 | if (p == NULL) { | |
543 | rdd->err = -ENOMEM; | |
544 | return -ENOMEM; | |
545 | } | |
546 | list_add_tail(&p->l_node, rdd->list); | |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
551 | static int ovl_dir_read_impure(struct path *path, struct list_head *list, | |
552 | struct rb_root *root) | |
553 | { | |
554 | int err; | |
555 | struct path realpath; | |
556 | struct ovl_cache_entry *p, *n; | |
557 | struct ovl_readdir_data rdd = { | |
558 | .ctx.actor = ovl_fill_plain, | |
559 | .list = list, | |
560 | .root = root, | |
561 | }; | |
562 | ||
563 | INIT_LIST_HEAD(list); | |
564 | *root = RB_ROOT; | |
565 | ovl_path_upper(path->dentry, &realpath); | |
566 | ||
567 | err = ovl_dir_read(&realpath, &rdd); | |
568 | if (err) | |
569 | return err; | |
570 | ||
571 | list_for_each_entry_safe(p, n, list, l_node) { | |
572 | if (strcmp(p->name, ".") != 0 && | |
573 | strcmp(p->name, "..") != 0) { | |
574 | err = ovl_cache_update_ino(path, p); | |
575 | if (err) | |
576 | return err; | |
577 | } | |
578 | if (p->ino == p->real_ino) { | |
579 | list_del(&p->l_node); | |
580 | kfree(p); | |
581 | } else { | |
582 | struct rb_node **newp = &root->rb_node; | |
583 | struct rb_node *parent = NULL; | |
584 | ||
585 | if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len, | |
586 | &newp, &parent))) | |
587 | return -EIO; | |
588 | ||
589 | rb_link_node(&p->node, parent, newp); | |
590 | rb_insert_color(&p->node, root); | |
591 | } | |
592 | } | |
593 | return 0; | |
594 | } | |
595 | ||
596 | static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path) | |
597 | { | |
598 | int res; | |
599 | struct dentry *dentry = path->dentry; | |
600 | struct ovl_dir_cache *cache; | |
601 | ||
602 | cache = ovl_dir_cache(d_inode(dentry)); | |
603 | if (cache && ovl_dentry_version_get(dentry) == cache->version) | |
604 | return cache; | |
605 | ||
606 | /* Impure cache is not refcounted, free it here */ | |
607 | ovl_dir_cache_free(d_inode(dentry)); | |
608 | ovl_set_dir_cache(d_inode(dentry), NULL); | |
609 | ||
610 | cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL); | |
611 | if (!cache) | |
612 | return ERR_PTR(-ENOMEM); | |
613 | ||
614 | res = ovl_dir_read_impure(path, &cache->entries, &cache->root); | |
615 | if (res) { | |
616 | ovl_cache_free(&cache->entries); | |
617 | kfree(cache); | |
618 | return ERR_PTR(res); | |
619 | } | |
620 | if (list_empty(&cache->entries)) { | |
a5a927a7 AG |
621 | /* |
622 | * A good opportunity to get rid of an unneeded "impure" flag. | |
623 | * Removing the "impure" xattr is best effort. | |
624 | */ | |
625 | if (!ovl_want_write(dentry)) { | |
626 | ovl_do_removexattr(ovl_dentry_upper(dentry), | |
627 | OVL_XATTR_IMPURE); | |
628 | ovl_drop_write(dentry); | |
629 | } | |
4edb83bb MS |
630 | ovl_clear_flag(OVL_IMPURE, d_inode(dentry)); |
631 | kfree(cache); | |
632 | return NULL; | |
633 | } | |
634 | ||
635 | cache->version = ovl_dentry_version_get(dentry); | |
636 | ovl_set_dir_cache(d_inode(dentry), cache); | |
637 | ||
638 | return cache; | |
639 | } | |
640 | ||
641 | struct ovl_readdir_translate { | |
642 | struct dir_context *orig_ctx; | |
643 | struct ovl_dir_cache *cache; | |
644 | struct dir_context ctx; | |
645 | u64 parent_ino; | |
adbf4f7e AG |
646 | int fsid; |
647 | int xinobits; | |
4edb83bb MS |
648 | }; |
649 | ||
650 | static int ovl_fill_real(struct dir_context *ctx, const char *name, | |
651 | int namelen, loff_t offset, u64 ino, | |
652 | unsigned int d_type) | |
653 | { | |
654 | struct ovl_readdir_translate *rdt = | |
655 | container_of(ctx, struct ovl_readdir_translate, ctx); | |
656 | struct dir_context *orig_ctx = rdt->orig_ctx; | |
657 | ||
adbf4f7e | 658 | if (rdt->parent_ino && strcmp(name, "..") == 0) { |
4edb83bb | 659 | ino = rdt->parent_ino; |
adbf4f7e | 660 | } else if (rdt->cache) { |
4edb83bb MS |
661 | struct ovl_cache_entry *p; |
662 | ||
663 | p = ovl_cache_entry_find(&rdt->cache->root, name, namelen); | |
664 | if (p) | |
665 | ino = p->ino; | |
adbf4f7e AG |
666 | } else if (rdt->xinobits) { |
667 | ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid, | |
668 | name, namelen); | |
4edb83bb MS |
669 | } |
670 | ||
671 | return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type); | |
672 | } | |
673 | ||
67810693 AG |
674 | static bool ovl_is_impure_dir(struct file *file) |
675 | { | |
676 | struct ovl_dir_file *od = file->private_data; | |
677 | struct inode *dir = d_inode(file->f_path.dentry); | |
678 | ||
679 | /* | |
680 | * Only upper dir can be impure, but if we are in the middle of | |
681 | * iterating a lower real dir, dir could be copied up and marked | |
682 | * impure. We only want the impure cache if we started iterating | |
683 | * a real upper dir to begin with. | |
684 | */ | |
685 | return od->is_upper && ovl_test_flag(OVL_IMPURE, dir); | |
686 | ||
687 | } | |
688 | ||
4edb83bb MS |
689 | static int ovl_iterate_real(struct file *file, struct dir_context *ctx) |
690 | { | |
691 | int err; | |
692 | struct ovl_dir_file *od = file->private_data; | |
693 | struct dentry *dir = file->f_path.dentry; | |
adbf4f7e | 694 | struct ovl_layer *lower_layer = ovl_layer_lower(dir); |
4edb83bb MS |
695 | struct ovl_readdir_translate rdt = { |
696 | .ctx.actor = ovl_fill_real, | |
697 | .orig_ctx = ctx, | |
adbf4f7e | 698 | .xinobits = ovl_xino_bits(dir->d_sb), |
4edb83bb MS |
699 | }; |
700 | ||
adbf4f7e AG |
701 | if (rdt.xinobits && lower_layer) |
702 | rdt.fsid = lower_layer->fsid; | |
703 | ||
4edb83bb MS |
704 | if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) { |
705 | struct kstat stat; | |
706 | struct path statpath = file->f_path; | |
707 | ||
708 | statpath.dentry = dir->d_parent; | |
709 | err = vfs_getattr(&statpath, &stat, STATX_INO, 0); | |
710 | if (err) | |
711 | return err; | |
712 | ||
713 | WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev); | |
714 | rdt.parent_ino = stat.ino; | |
715 | } | |
716 | ||
67810693 | 717 | if (ovl_is_impure_dir(file)) { |
4edb83bb MS |
718 | rdt.cache = ovl_cache_get_impure(&file->f_path); |
719 | if (IS_ERR(rdt.cache)) | |
720 | return PTR_ERR(rdt.cache); | |
721 | } | |
722 | ||
b02a16e6 AG |
723 | err = iterate_dir(od->realfile, &rdt.ctx); |
724 | ctx->pos = rdt.ctx.pos; | |
725 | ||
726 | return err; | |
4edb83bb MS |
727 | } |
728 | ||
729 | ||
e9be9d5e MS |
730 | static int ovl_iterate(struct file *file, struct dir_context *ctx) |
731 | { | |
732 | struct ovl_dir_file *od = file->private_data; | |
733 | struct dentry *dentry = file->f_path.dentry; | |
4330397e | 734 | struct ovl_cache_entry *p; |
13edb02e | 735 | const struct cred *old_cred; |
b5efccbe | 736 | int err; |
e9be9d5e | 737 | |
13edb02e | 738 | old_cred = ovl_override_creds(dentry->d_sb); |
e9be9d5e MS |
739 | if (!ctx->pos) |
740 | ovl_dir_reset(file); | |
741 | ||
4edb83bb MS |
742 | if (od->is_real) { |
743 | /* | |
744 | * If parent is merge, then need to adjust d_ino for '..', if | |
745 | * dir is impure then need to adjust d_ino for copied up | |
746 | * entries. | |
747 | */ | |
adbf4f7e AG |
748 | if (ovl_xino_bits(dentry->d_sb) || |
749 | (ovl_same_sb(dentry->d_sb) && | |
67810693 | 750 | (ovl_is_impure_dir(file) || |
adbf4f7e | 751 | OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) { |
13edb02e MS |
752 | err = ovl_iterate_real(file, ctx); |
753 | } else { | |
754 | err = iterate_dir(od->realfile, ctx); | |
4edb83bb | 755 | } |
13edb02e | 756 | goto out; |
4edb83bb | 757 | } |
e9be9d5e MS |
758 | |
759 | if (!od->cache) { | |
760 | struct ovl_dir_cache *cache; | |
761 | ||
762 | cache = ovl_cache_get(dentry); | |
13edb02e | 763 | err = PTR_ERR(cache); |
e9be9d5e | 764 | if (IS_ERR(cache)) |
13edb02e | 765 | goto out; |
e9be9d5e MS |
766 | |
767 | od->cache = cache; | |
768 | ovl_seek_cursor(od, ctx->pos); | |
769 | } | |
770 | ||
4330397e | 771 | while (od->cursor != &od->cache->entries) { |
772 | p = list_entry(od->cursor, struct ovl_cache_entry, l_node); | |
b5efccbe AG |
773 | if (!p->is_whiteout) { |
774 | if (!p->ino) { | |
775 | err = ovl_cache_update_ino(&file->f_path, p); | |
776 | if (err) | |
13edb02e | 777 | goto out; |
b5efccbe | 778 | } |
4330397e | 779 | if (!dir_emit(ctx, p->name, p->len, p->ino, p->type)) |
780 | break; | |
b5efccbe | 781 | } |
4330397e | 782 | od->cursor = p->l_node.next; |
783 | ctx->pos++; | |
e9be9d5e | 784 | } |
13edb02e MS |
785 | err = 0; |
786 | out: | |
787 | revert_creds(old_cred); | |
788 | return err; | |
e9be9d5e MS |
789 | } |
790 | ||
791 | static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin) | |
792 | { | |
793 | loff_t res; | |
794 | struct ovl_dir_file *od = file->private_data; | |
795 | ||
5955102c | 796 | inode_lock(file_inode(file)); |
e9be9d5e MS |
797 | if (!file->f_pos) |
798 | ovl_dir_reset(file); | |
799 | ||
800 | if (od->is_real) { | |
801 | res = vfs_llseek(od->realfile, offset, origin); | |
802 | file->f_pos = od->realfile->f_pos; | |
803 | } else { | |
804 | res = -EINVAL; | |
805 | ||
806 | switch (origin) { | |
807 | case SEEK_CUR: | |
808 | offset += file->f_pos; | |
809 | break; | |
810 | case SEEK_SET: | |
811 | break; | |
812 | default: | |
813 | goto out_unlock; | |
814 | } | |
815 | if (offset < 0) | |
816 | goto out_unlock; | |
817 | ||
818 | if (offset != file->f_pos) { | |
819 | file->f_pos = offset; | |
820 | if (od->cache) | |
821 | ovl_seek_cursor(od, offset); | |
822 | } | |
823 | res = offset; | |
824 | } | |
825 | out_unlock: | |
5955102c | 826 | inode_unlock(file_inode(file)); |
e9be9d5e MS |
827 | |
828 | return res; | |
829 | } | |
830 | ||
dcdcca98 MS |
831 | static struct file *ovl_dir_open_realfile(struct file *file, |
832 | struct path *realpath) | |
833 | { | |
13edb02e MS |
834 | struct file *res; |
835 | const struct cred *old_cred; | |
836 | ||
837 | old_cred = ovl_override_creds(file_inode(file)->i_sb); | |
838 | res = ovl_path_open(realpath, O_RDONLY | (file->f_flags & O_LARGEFILE)); | |
839 | revert_creds(old_cred); | |
840 | ||
841 | return res; | |
dcdcca98 MS |
842 | } |
843 | ||
e9be9d5e MS |
844 | static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end, |
845 | int datasync) | |
846 | { | |
847 | struct ovl_dir_file *od = file->private_data; | |
848 | struct dentry *dentry = file->f_path.dentry; | |
849 | struct file *realfile = od->realfile; | |
850 | ||
d796e77f AG |
851 | /* Nothing to sync for lower */ |
852 | if (!OVL_TYPE_UPPER(ovl_path_type(dentry))) | |
853 | return 0; | |
854 | ||
e9be9d5e MS |
855 | /* |
856 | * Need to check if we started out being a lower dir, but got copied up | |
857 | */ | |
d796e77f | 858 | if (!od->is_upper) { |
e9be9d5e MS |
859 | struct inode *inode = file_inode(file); |
860 | ||
506458ef | 861 | realfile = READ_ONCE(od->upperfile); |
e9be9d5e MS |
862 | if (!realfile) { |
863 | struct path upperpath; | |
864 | ||
865 | ovl_path_upper(dentry, &upperpath); | |
dcdcca98 | 866 | realfile = ovl_dir_open_realfile(file, &upperpath); |
ff7a5fb0 | 867 | |
5955102c | 868 | inode_lock(inode); |
3d268c9b AV |
869 | if (!od->upperfile) { |
870 | if (IS_ERR(realfile)) { | |
5955102c | 871 | inode_unlock(inode); |
3d268c9b AV |
872 | return PTR_ERR(realfile); |
873 | } | |
ff7a5fb0 | 874 | smp_store_release(&od->upperfile, realfile); |
3d268c9b AV |
875 | } else { |
876 | /* somebody has beaten us to it */ | |
877 | if (!IS_ERR(realfile)) | |
878 | fput(realfile); | |
879 | realfile = od->upperfile; | |
e9be9d5e | 880 | } |
5955102c | 881 | inode_unlock(inode); |
e9be9d5e | 882 | } |
e9be9d5e MS |
883 | } |
884 | ||
885 | return vfs_fsync_range(realfile, start, end, datasync); | |
886 | } | |
887 | ||
888 | static int ovl_dir_release(struct inode *inode, struct file *file) | |
889 | { | |
890 | struct ovl_dir_file *od = file->private_data; | |
891 | ||
892 | if (od->cache) { | |
5955102c | 893 | inode_lock(inode); |
e9be9d5e | 894 | ovl_cache_put(od, file->f_path.dentry); |
5955102c | 895 | inode_unlock(inode); |
e9be9d5e MS |
896 | } |
897 | fput(od->realfile); | |
898 | if (od->upperfile) | |
899 | fput(od->upperfile); | |
900 | kfree(od); | |
901 | ||
902 | return 0; | |
903 | } | |
904 | ||
905 | static int ovl_dir_open(struct inode *inode, struct file *file) | |
906 | { | |
907 | struct path realpath; | |
908 | struct file *realfile; | |
909 | struct ovl_dir_file *od; | |
910 | enum ovl_path_type type; | |
911 | ||
912 | od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL); | |
913 | if (!od) | |
914 | return -ENOMEM; | |
915 | ||
916 | type = ovl_path_real(file->f_path.dentry, &realpath); | |
dcdcca98 | 917 | realfile = ovl_dir_open_realfile(file, &realpath); |
e9be9d5e MS |
918 | if (IS_ERR(realfile)) { |
919 | kfree(od); | |
920 | return PTR_ERR(realfile); | |
921 | } | |
e9be9d5e | 922 | od->realfile = realfile; |
b79e05aa | 923 | od->is_real = ovl_dir_is_real(file->f_path.dentry); |
1afaba1e | 924 | od->is_upper = OVL_TYPE_UPPER(type); |
e9be9d5e MS |
925 | file->private_data = od; |
926 | ||
927 | return 0; | |
928 | } | |
929 | ||
930 | const struct file_operations ovl_dir_operations = { | |
931 | .read = generic_read_dir, | |
932 | .open = ovl_dir_open, | |
933 | .iterate = ovl_iterate, | |
934 | .llseek = ovl_dir_llseek, | |
935 | .fsync = ovl_dir_fsync, | |
936 | .release = ovl_dir_release, | |
937 | }; | |
938 | ||
939 | int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list) | |
940 | { | |
941 | int err; | |
95e598e7 | 942 | struct ovl_cache_entry *p, *n; |
4edb83bb | 943 | struct rb_root root = RB_ROOT; |
6d0a8a90 | 944 | const struct cred *old_cred; |
e9be9d5e | 945 | |
6d0a8a90 | 946 | old_cred = ovl_override_creds(dentry->d_sb); |
4edb83bb | 947 | err = ovl_dir_read_merged(dentry, list, &root); |
6d0a8a90 | 948 | revert_creds(old_cred); |
e9be9d5e MS |
949 | if (err) |
950 | return err; | |
951 | ||
952 | err = 0; | |
953 | ||
95e598e7 | 954 | list_for_each_entry_safe(p, n, list, l_node) { |
955 | /* | |
956 | * Select whiteouts in upperdir, they should | |
957 | * be cleared when deleting this directory. | |
958 | */ | |
959 | if (p->is_whiteout) { | |
960 | if (p->is_upper) | |
961 | continue; | |
962 | goto del_entry; | |
963 | } | |
e9be9d5e MS |
964 | |
965 | if (p->name[0] == '.') { | |
966 | if (p->len == 1) | |
95e598e7 | 967 | goto del_entry; |
e9be9d5e | 968 | if (p->len == 2 && p->name[1] == '.') |
95e598e7 | 969 | goto del_entry; |
e9be9d5e MS |
970 | } |
971 | err = -ENOTEMPTY; | |
972 | break; | |
95e598e7 | 973 | |
974 | del_entry: | |
975 | list_del(&p->l_node); | |
976 | kfree(p); | |
e9be9d5e MS |
977 | } |
978 | ||
979 | return err; | |
980 | } | |
981 | ||
982 | void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list) | |
983 | { | |
984 | struct ovl_cache_entry *p; | |
985 | ||
5955102c | 986 | inode_lock_nested(upper->d_inode, I_MUTEX_CHILD); |
e9be9d5e MS |
987 | list_for_each_entry(p, list, l_node) { |
988 | struct dentry *dentry; | |
989 | ||
95e598e7 | 990 | if (WARN_ON(!p->is_whiteout || !p->is_upper)) |
e9be9d5e MS |
991 | continue; |
992 | ||
993 | dentry = lookup_one_len(p->name, upper, p->len); | |
994 | if (IS_ERR(dentry)) { | |
995 | pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n", | |
996 | upper->d_name.name, p->len, p->name, | |
997 | (int) PTR_ERR(dentry)); | |
998 | continue; | |
999 | } | |
84889d49 KK |
1000 | if (dentry->d_inode) |
1001 | ovl_cleanup(upper->d_inode, dentry); | |
e9be9d5e MS |
1002 | dput(dentry); |
1003 | } | |
5955102c | 1004 | inode_unlock(upper->d_inode); |
e9be9d5e | 1005 | } |
45aebeaf VG |
1006 | |
1007 | static int ovl_check_d_type(struct dir_context *ctx, const char *name, | |
1008 | int namelen, loff_t offset, u64 ino, | |
1009 | unsigned int d_type) | |
1010 | { | |
1011 | struct ovl_readdir_data *rdd = | |
1012 | container_of(ctx, struct ovl_readdir_data, ctx); | |
1013 | ||
1014 | /* Even if d_type is not supported, DT_DIR is returned for . and .. */ | |
1015 | if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen)) | |
1016 | return 0; | |
1017 | ||
1018 | if (d_type != DT_UNKNOWN) | |
1019 | rdd->d_type_supported = true; | |
1020 | ||
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | /* | |
1025 | * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values | |
1026 | * if error is encountered. | |
1027 | */ | |
1028 | int ovl_check_d_type_supported(struct path *realpath) | |
1029 | { | |
1030 | int err; | |
1031 | struct ovl_readdir_data rdd = { | |
1032 | .ctx.actor = ovl_check_d_type, | |
1033 | .d_type_supported = false, | |
1034 | }; | |
1035 | ||
1036 | err = ovl_dir_read(realpath, &rdd); | |
1037 | if (err) | |
1038 | return err; | |
1039 | ||
1040 | return rdd.d_type_supported; | |
1041 | } | |
eea2fb48 MS |
1042 | |
1043 | static void ovl_workdir_cleanup_recurse(struct path *path, int level) | |
1044 | { | |
1045 | int err; | |
1046 | struct inode *dir = path->dentry->d_inode; | |
1047 | LIST_HEAD(list); | |
4edb83bb | 1048 | struct rb_root root = RB_ROOT; |
eea2fb48 MS |
1049 | struct ovl_cache_entry *p; |
1050 | struct ovl_readdir_data rdd = { | |
1051 | .ctx.actor = ovl_fill_merge, | |
1052 | .dentry = NULL, | |
1053 | .list = &list, | |
4edb83bb | 1054 | .root = &root, |
eea2fb48 MS |
1055 | .is_lowest = false, |
1056 | }; | |
1057 | ||
1058 | err = ovl_dir_read(path, &rdd); | |
1059 | if (err) | |
1060 | goto out; | |
1061 | ||
1062 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
1063 | list_for_each_entry(p, &list, l_node) { | |
1064 | struct dentry *dentry; | |
1065 | ||
1066 | if (p->name[0] == '.') { | |
1067 | if (p->len == 1) | |
1068 | continue; | |
1069 | if (p->len == 2 && p->name[1] == '.') | |
1070 | continue; | |
1071 | } | |
1072 | dentry = lookup_one_len(p->name, path->dentry, p->len); | |
1073 | if (IS_ERR(dentry)) | |
1074 | continue; | |
1075 | if (dentry->d_inode) | |
1076 | ovl_workdir_cleanup(dir, path->mnt, dentry, level); | |
1077 | dput(dentry); | |
1078 | } | |
1079 | inode_unlock(dir); | |
1080 | out: | |
1081 | ovl_cache_free(&list); | |
1082 | } | |
1083 | ||
1084 | void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt, | |
1085 | struct dentry *dentry, int level) | |
1086 | { | |
1087 | int err; | |
1088 | ||
1089 | if (!d_is_dir(dentry) || level > 1) { | |
1090 | ovl_cleanup(dir, dentry); | |
1091 | return; | |
1092 | } | |
1093 | ||
1094 | err = ovl_do_rmdir(dir, dentry); | |
1095 | if (err) { | |
1096 | struct path path = { .mnt = mnt, .dentry = dentry }; | |
1097 | ||
1098 | inode_unlock(dir); | |
1099 | ovl_workdir_cleanup_recurse(&path, level + 1); | |
1100 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
1101 | ovl_cleanup(dir, dentry); | |
1102 | } | |
1103 | } | |
415543d5 | 1104 | |
1eff1a1d | 1105 | int ovl_indexdir_cleanup(struct ovl_fs *ofs) |
415543d5 AG |
1106 | { |
1107 | int err; | |
1eff1a1d | 1108 | struct dentry *indexdir = ofs->indexdir; |
dc7ab677 | 1109 | struct dentry *index = NULL; |
1eff1a1d AG |
1110 | struct inode *dir = indexdir->d_inode; |
1111 | struct path path = { .mnt = ofs->upper_mnt, .dentry = indexdir }; | |
415543d5 | 1112 | LIST_HEAD(list); |
4edb83bb | 1113 | struct rb_root root = RB_ROOT; |
415543d5 AG |
1114 | struct ovl_cache_entry *p; |
1115 | struct ovl_readdir_data rdd = { | |
1116 | .ctx.actor = ovl_fill_merge, | |
1117 | .dentry = NULL, | |
1118 | .list = &list, | |
4edb83bb | 1119 | .root = &root, |
415543d5 AG |
1120 | .is_lowest = false, |
1121 | }; | |
1122 | ||
1123 | err = ovl_dir_read(&path, &rdd); | |
1124 | if (err) | |
1125 | goto out; | |
1126 | ||
1127 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
1128 | list_for_each_entry(p, &list, l_node) { | |
415543d5 AG |
1129 | if (p->name[0] == '.') { |
1130 | if (p->len == 1) | |
1131 | continue; | |
1132 | if (p->len == 2 && p->name[1] == '.') | |
1133 | continue; | |
1134 | } | |
1eff1a1d | 1135 | index = lookup_one_len(p->name, indexdir, p->len); |
415543d5 AG |
1136 | if (IS_ERR(index)) { |
1137 | err = PTR_ERR(index); | |
dc7ab677 | 1138 | index = NULL; |
415543d5 AG |
1139 | break; |
1140 | } | |
1eff1a1d | 1141 | err = ovl_verify_index(ofs, index); |
24f0b172 AG |
1142 | if (!err) { |
1143 | goto next; | |
1144 | } else if (err == -ESTALE) { | |
1145 | /* Cleanup stale index entries */ | |
415543d5 | 1146 | err = ovl_cleanup(dir, index); |
24f0b172 AG |
1147 | } else if (err != -ENOENT) { |
1148 | /* | |
1149 | * Abort mount to avoid corrupting the index if | |
1150 | * an incompatible index entry was found or on out | |
1151 | * of memory. | |
1152 | */ | |
1153 | break; | |
1154 | } else if (ofs->config.nfs_export) { | |
1155 | /* | |
1156 | * Whiteout orphan index to block future open by | |
1157 | * handle after overlay nlink dropped to zero. | |
1158 | */ | |
1159 | err = ovl_cleanup_and_whiteout(indexdir, dir, index); | |
1160 | } else { | |
1161 | /* Cleanup orphan index entries */ | |
1162 | err = ovl_cleanup(dir, index); | |
1163 | } | |
1164 | ||
fa0096e3 AG |
1165 | if (err) |
1166 | break; | |
1167 | ||
24f0b172 | 1168 | next: |
415543d5 | 1169 | dput(index); |
dc7ab677 | 1170 | index = NULL; |
415543d5 | 1171 | } |
dc7ab677 | 1172 | dput(index); |
415543d5 AG |
1173 | inode_unlock(dir); |
1174 | out: | |
1175 | ovl_cache_free(&list); | |
1176 | if (err) | |
1177 | pr_err("overlayfs: failed index dir cleanup (%i)\n", err); | |
1178 | return err; | |
1179 | } |