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