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