3 * Copyright (C) 2011 Novell Inc.
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.
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"
21 struct ovl_cache_entry {
26 struct list_head l_node;
28 struct ovl_cache_entry *next_maybe_whiteout;
33 struct ovl_dir_cache {
36 struct list_head entries;
40 struct ovl_readdir_data {
41 struct dir_context ctx;
42 struct dentry *dentry;
45 struct list_head *list;
46 struct list_head middle;
47 struct ovl_cache_entry *first_maybe_whiteout;
51 bool d_type_supported;
57 struct ovl_dir_cache *cache;
58 struct list_head *cursor;
59 struct file *realfile;
60 struct file *upperfile;
63 static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
65 return rb_entry(n, struct ovl_cache_entry, node);
68 static bool ovl_cache_entry_find_link(const char *name, int len,
69 struct rb_node ***link,
70 struct rb_node **parent)
73 struct rb_node **newp = *link;
75 while (!found && *newp) {
77 struct ovl_cache_entry *tmp;
80 tmp = ovl_cache_entry_from_node(*newp);
81 cmp = strncmp(name, tmp->name, len);
83 newp = &tmp->node.rb_right;
84 else if (cmp < 0 || len < tmp->len)
85 newp = &tmp->node.rb_left;
94 static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
95 const char *name, int len)
97 struct rb_node *node = root->rb_node;
101 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
103 cmp = strncmp(name, p->name, len);
105 node = p->node.rb_right;
106 else if (cmp < 0 || len < p->len)
107 node = p->node.rb_left;
115 static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd,
116 struct ovl_cache_entry *p)
118 /* Don't care if not doing ovl_iter() */
122 /* Always recalc d_ino for parent */
123 if (strcmp(p->name, "..") == 0)
126 /* If this is lower, then native d_ino will do */
131 * Recalc d_ino for '.' and for all entries if dir is impure (contains
134 if ((p->name[0] == '.' && p->len == 1) ||
135 ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
141 static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
142 const char *name, int len,
143 u64 ino, unsigned int d_type)
145 struct ovl_cache_entry *p;
146 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
148 p = kmalloc(size, GFP_KERNEL);
152 memcpy(p->name, name, len);
158 /* Defer setting d_ino for upper entry to ovl_iterate() */
159 if (ovl_calc_d_ino(rdd, p))
161 p->is_whiteout = false;
163 if (d_type == DT_CHR) {
164 p->next_maybe_whiteout = rdd->first_maybe_whiteout;
165 rdd->first_maybe_whiteout = p;
170 static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
171 const char *name, int len, u64 ino,
174 struct rb_node **newp = &rdd->root->rb_node;
175 struct rb_node *parent = NULL;
176 struct ovl_cache_entry *p;
178 if (ovl_cache_entry_find_link(name, len, &newp, &parent))
181 p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
187 list_add_tail(&p->l_node, rdd->list);
188 rb_link_node(&p->node, parent, newp);
189 rb_insert_color(&p->node, rdd->root);
194 static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
195 const char *name, int namelen,
196 loff_t offset, u64 ino, unsigned int d_type)
198 struct ovl_cache_entry *p;
200 p = ovl_cache_entry_find(rdd->root, name, namelen);
202 list_move_tail(&p->l_node, &rdd->middle);
204 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
208 list_add_tail(&p->l_node, &rdd->middle);
214 void ovl_cache_free(struct list_head *list)
216 struct ovl_cache_entry *p;
217 struct ovl_cache_entry *n;
219 list_for_each_entry_safe(p, n, list, l_node)
222 INIT_LIST_HEAD(list);
225 void ovl_dir_cache_free(struct inode *inode)
227 struct ovl_dir_cache *cache = ovl_dir_cache(inode);
230 ovl_cache_free(&cache->entries);
235 static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
237 struct ovl_dir_cache *cache = od->cache;
239 WARN_ON(cache->refcount <= 0);
241 if (!cache->refcount) {
242 if (ovl_dir_cache(d_inode(dentry)) == cache)
243 ovl_set_dir_cache(d_inode(dentry), NULL);
245 ovl_cache_free(&cache->entries);
250 static int ovl_fill_merge(struct dir_context *ctx, const char *name,
251 int namelen, loff_t offset, u64 ino,
254 struct ovl_readdir_data *rdd =
255 container_of(ctx, struct ovl_readdir_data, ctx);
259 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
261 return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
264 static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
267 struct ovl_cache_entry *p;
268 struct dentry *dentry;
269 const struct cred *old_cred;
271 old_cred = ovl_override_creds(rdd->dentry->d_sb);
273 err = down_write_killable(&dir->d_inode->i_rwsem);
275 while (rdd->first_maybe_whiteout) {
276 p = rdd->first_maybe_whiteout;
277 rdd->first_maybe_whiteout = p->next_maybe_whiteout;
278 dentry = lookup_one_len(p->name, dir, p->len);
279 if (!IS_ERR(dentry)) {
280 p->is_whiteout = ovl_is_whiteout(dentry);
284 inode_unlock(dir->d_inode);
286 revert_creds(old_cred);
291 static inline int ovl_dir_read(struct path *realpath,
292 struct ovl_readdir_data *rdd)
294 struct file *realfile;
297 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
298 if (IS_ERR(realfile))
299 return PTR_ERR(realfile);
301 rdd->first_maybe_whiteout = NULL;
306 err = iterate_dir(realfile, &rdd->ctx);
309 } while (!err && rdd->count);
311 if (!err && rdd->first_maybe_whiteout && rdd->dentry)
312 err = ovl_check_whiteouts(realpath->dentry, rdd);
319 static void ovl_dir_reset(struct file *file)
321 struct ovl_dir_file *od = file->private_data;
322 struct ovl_dir_cache *cache = od->cache;
323 struct dentry *dentry = file->f_path.dentry;
324 enum ovl_path_type type = ovl_path_type(dentry);
326 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
327 ovl_cache_put(od, dentry);
331 WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type));
332 if (od->is_real && OVL_TYPE_MERGE(type))
336 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list,
337 struct rb_root *root)
340 struct path realpath;
341 struct ovl_readdir_data rdd = {
342 .ctx.actor = ovl_fill_merge,
350 for (idx = 0; idx != -1; idx = next) {
351 next = ovl_path_next(idx, dentry, &realpath);
352 rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry;
355 err = ovl_dir_read(&realpath, &rdd);
360 * Insert lowest layer entries before upper ones, this
361 * allows offsets to be reasonably constant
363 list_add(&rdd.middle, rdd.list);
364 rdd.is_lowest = true;
365 err = ovl_dir_read(&realpath, &rdd);
366 list_del(&rdd.middle);
372 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
377 list_for_each(p, &od->cache->entries) {
382 /* Cursor is safe since the cache is stable */
386 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
389 struct ovl_dir_cache *cache;
391 cache = ovl_dir_cache(d_inode(dentry));
392 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
393 WARN_ON(!cache->refcount);
397 ovl_set_dir_cache(d_inode(dentry), NULL);
399 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
401 return ERR_PTR(-ENOMEM);
404 INIT_LIST_HEAD(&cache->entries);
405 cache->root = RB_ROOT;
407 res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root);
409 ovl_cache_free(&cache->entries);
414 cache->version = ovl_dentry_version_get(dentry);
415 ovl_set_dir_cache(d_inode(dentry), cache);
421 * Set d_ino for upper entries. Non-upper entries should always report
422 * the uppermost real inode ino and should not call this function.
424 * When not all layer are on same fs, report real ino also for upper.
426 * When all layers are on the same fs, and upper has a reference to
427 * copy up origin, call vfs_getattr() on the overlay entry to make
428 * sure that d_ino will be consistent with st_ino from stat(2).
430 static int ovl_cache_update_ino(struct path *path, struct ovl_cache_entry *p)
433 struct dentry *dir = path->dentry;
434 struct dentry *this = NULL;
435 enum ovl_path_type type;
436 u64 ino = p->real_ino;
439 if (!ovl_same_sb(dir->d_sb))
442 if (p->name[0] == '.') {
447 if (p->len == 2 && p->name[1] == '.') {
448 /* we shall not be moved */
449 this = dget(dir->d_parent);
453 this = lookup_one_len(p->name, dir, p->len);
454 if (IS_ERR_OR_NULL(this) || !this->d_inode) {
464 type = ovl_path_type(this);
465 if (OVL_TYPE_ORIGIN(type)) {
467 struct path statpath = *path;
469 statpath.dentry = this;
470 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
474 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
484 pr_warn_ratelimited("overlay: failed to look up (%s) for ino (%i)\n",
489 static int ovl_fill_plain(struct dir_context *ctx, const char *name,
490 int namelen, loff_t offset, u64 ino,
493 struct ovl_cache_entry *p;
494 struct ovl_readdir_data *rdd =
495 container_of(ctx, struct ovl_readdir_data, ctx);
498 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
503 list_add_tail(&p->l_node, rdd->list);
508 static int ovl_dir_read_impure(struct path *path, struct list_head *list,
509 struct rb_root *root)
512 struct path realpath;
513 struct ovl_cache_entry *p, *n;
514 struct ovl_readdir_data rdd = {
515 .ctx.actor = ovl_fill_plain,
520 INIT_LIST_HEAD(list);
522 ovl_path_upper(path->dentry, &realpath);
524 err = ovl_dir_read(&realpath, &rdd);
528 list_for_each_entry_safe(p, n, list, l_node) {
529 if (strcmp(p->name, ".") != 0 &&
530 strcmp(p->name, "..") != 0) {
531 err = ovl_cache_update_ino(path, p);
535 if (p->ino == p->real_ino) {
536 list_del(&p->l_node);
539 struct rb_node **newp = &root->rb_node;
540 struct rb_node *parent = NULL;
542 if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
546 rb_link_node(&p->node, parent, newp);
547 rb_insert_color(&p->node, root);
553 static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path)
556 struct dentry *dentry = path->dentry;
557 struct ovl_dir_cache *cache;
559 cache = ovl_dir_cache(d_inode(dentry));
560 if (cache && ovl_dentry_version_get(dentry) == cache->version)
563 /* Impure cache is not refcounted, free it here */
564 ovl_dir_cache_free(d_inode(dentry));
565 ovl_set_dir_cache(d_inode(dentry), NULL);
567 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
569 return ERR_PTR(-ENOMEM);
571 res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
573 ovl_cache_free(&cache->entries);
577 if (list_empty(&cache->entries)) {
578 /* Good oportunity to get rid of an unnecessary "impure" flag */
579 ovl_do_removexattr(ovl_dentry_upper(dentry), OVL_XATTR_IMPURE);
580 ovl_clear_flag(OVL_IMPURE, d_inode(dentry));
585 cache->version = ovl_dentry_version_get(dentry);
586 ovl_set_dir_cache(d_inode(dentry), cache);
591 struct ovl_readdir_translate {
592 struct dir_context *orig_ctx;
593 struct ovl_dir_cache *cache;
594 struct dir_context ctx;
598 static int ovl_fill_real(struct dir_context *ctx, const char *name,
599 int namelen, loff_t offset, u64 ino,
602 struct ovl_readdir_translate *rdt =
603 container_of(ctx, struct ovl_readdir_translate, ctx);
604 struct dir_context *orig_ctx = rdt->orig_ctx;
606 if (rdt->parent_ino && strcmp(name, "..") == 0)
607 ino = rdt->parent_ino;
608 else if (rdt->cache) {
609 struct ovl_cache_entry *p;
611 p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
616 return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
619 static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
622 struct ovl_dir_file *od = file->private_data;
623 struct dentry *dir = file->f_path.dentry;
624 struct ovl_readdir_translate rdt = {
625 .ctx.actor = ovl_fill_real,
629 if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
631 struct path statpath = file->f_path;
633 statpath.dentry = dir->d_parent;
634 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
638 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
639 rdt.parent_ino = stat.ino;
642 if (ovl_test_flag(OVL_IMPURE, d_inode(dir))) {
643 rdt.cache = ovl_cache_get_impure(&file->f_path);
644 if (IS_ERR(rdt.cache))
645 return PTR_ERR(rdt.cache);
648 return iterate_dir(od->realfile, &rdt.ctx);
652 static int ovl_iterate(struct file *file, struct dir_context *ctx)
654 struct ovl_dir_file *od = file->private_data;
655 struct dentry *dentry = file->f_path.dentry;
656 struct ovl_cache_entry *p;
664 * If parent is merge, then need to adjust d_ino for '..', if
665 * dir is impure then need to adjust d_ino for copied up
668 if (ovl_same_sb(dentry->d_sb) &&
669 (ovl_test_flag(OVL_IMPURE, d_inode(dentry)) ||
670 OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent)))) {
671 return ovl_iterate_real(file, ctx);
673 return iterate_dir(od->realfile, ctx);
677 struct ovl_dir_cache *cache;
679 cache = ovl_cache_get(dentry);
681 return PTR_ERR(cache);
684 ovl_seek_cursor(od, ctx->pos);
687 while (od->cursor != &od->cache->entries) {
688 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
689 if (!p->is_whiteout) {
691 err = ovl_cache_update_ino(&file->f_path, p);
695 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
698 od->cursor = p->l_node.next;
704 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
707 struct ovl_dir_file *od = file->private_data;
709 inode_lock(file_inode(file));
714 res = vfs_llseek(od->realfile, offset, origin);
715 file->f_pos = od->realfile->f_pos;
721 offset += file->f_pos;
731 if (offset != file->f_pos) {
732 file->f_pos = offset;
734 ovl_seek_cursor(od, offset);
739 inode_unlock(file_inode(file));
744 static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
747 struct ovl_dir_file *od = file->private_data;
748 struct dentry *dentry = file->f_path.dentry;
749 struct file *realfile = od->realfile;
752 * Need to check if we started out being a lower dir, but got copied up
754 if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) {
755 struct inode *inode = file_inode(file);
757 realfile = lockless_dereference(od->upperfile);
759 struct path upperpath;
761 ovl_path_upper(dentry, &upperpath);
762 realfile = ovl_path_open(&upperpath, O_RDONLY);
765 if (!od->upperfile) {
766 if (IS_ERR(realfile)) {
768 return PTR_ERR(realfile);
770 smp_store_release(&od->upperfile, realfile);
772 /* somebody has beaten us to it */
773 if (!IS_ERR(realfile))
775 realfile = od->upperfile;
781 return vfs_fsync_range(realfile, start, end, datasync);
784 static int ovl_dir_release(struct inode *inode, struct file *file)
786 struct ovl_dir_file *od = file->private_data;
790 ovl_cache_put(od, file->f_path.dentry);
801 static int ovl_dir_open(struct inode *inode, struct file *file)
803 struct path realpath;
804 struct file *realfile;
805 struct ovl_dir_file *od;
806 enum ovl_path_type type;
808 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
812 type = ovl_path_real(file->f_path.dentry, &realpath);
813 realfile = ovl_path_open(&realpath, file->f_flags);
814 if (IS_ERR(realfile)) {
816 return PTR_ERR(realfile);
818 od->realfile = realfile;
819 od->is_real = !OVL_TYPE_MERGE(type);
820 od->is_upper = OVL_TYPE_UPPER(type);
821 file->private_data = od;
826 const struct file_operations ovl_dir_operations = {
827 .read = generic_read_dir,
828 .open = ovl_dir_open,
829 .iterate = ovl_iterate,
830 .llseek = ovl_dir_llseek,
831 .fsync = ovl_dir_fsync,
832 .release = ovl_dir_release,
835 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
838 struct ovl_cache_entry *p;
839 struct rb_root root = RB_ROOT;
841 err = ovl_dir_read_merged(dentry, list, &root);
847 list_for_each_entry(p, list, l_node) {
851 if (p->name[0] == '.') {
854 if (p->len == 2 && p->name[1] == '.')
864 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
866 struct ovl_cache_entry *p;
868 inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
869 list_for_each_entry(p, list, l_node) {
870 struct dentry *dentry;
875 dentry = lookup_one_len(p->name, upper, p->len);
876 if (IS_ERR(dentry)) {
877 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
878 upper->d_name.name, p->len, p->name,
879 (int) PTR_ERR(dentry));
883 ovl_cleanup(upper->d_inode, dentry);
886 inode_unlock(upper->d_inode);
889 static int ovl_check_d_type(struct dir_context *ctx, const char *name,
890 int namelen, loff_t offset, u64 ino,
893 struct ovl_readdir_data *rdd =
894 container_of(ctx, struct ovl_readdir_data, ctx);
896 /* Even if d_type is not supported, DT_DIR is returned for . and .. */
897 if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
900 if (d_type != DT_UNKNOWN)
901 rdd->d_type_supported = true;
907 * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
908 * if error is encountered.
910 int ovl_check_d_type_supported(struct path *realpath)
913 struct ovl_readdir_data rdd = {
914 .ctx.actor = ovl_check_d_type,
915 .d_type_supported = false,
918 err = ovl_dir_read(realpath, &rdd);
922 return rdd.d_type_supported;
925 static void ovl_workdir_cleanup_recurse(struct path *path, int level)
928 struct inode *dir = path->dentry->d_inode;
930 struct rb_root root = RB_ROOT;
931 struct ovl_cache_entry *p;
932 struct ovl_readdir_data rdd = {
933 .ctx.actor = ovl_fill_merge,
940 err = ovl_dir_read(path, &rdd);
944 inode_lock_nested(dir, I_MUTEX_PARENT);
945 list_for_each_entry(p, &list, l_node) {
946 struct dentry *dentry;
948 if (p->name[0] == '.') {
951 if (p->len == 2 && p->name[1] == '.')
954 dentry = lookup_one_len(p->name, path->dentry, p->len);
958 ovl_workdir_cleanup(dir, path->mnt, dentry, level);
963 ovl_cache_free(&list);
966 void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
967 struct dentry *dentry, int level)
971 if (!d_is_dir(dentry) || level > 1) {
972 ovl_cleanup(dir, dentry);
976 err = ovl_do_rmdir(dir, dentry);
978 struct path path = { .mnt = mnt, .dentry = dentry };
981 ovl_workdir_cleanup_recurse(&path, level + 1);
982 inode_lock_nested(dir, I_MUTEX_PARENT);
983 ovl_cleanup(dir, dentry);
987 int ovl_indexdir_cleanup(struct dentry *dentry, struct vfsmount *mnt,
988 struct path *lowerstack, unsigned int numlower)
991 struct dentry *index = NULL;
992 struct inode *dir = dentry->d_inode;
993 struct path path = { .mnt = mnt, .dentry = dentry };
995 struct rb_root root = RB_ROOT;
996 struct ovl_cache_entry *p;
997 struct ovl_readdir_data rdd = {
998 .ctx.actor = ovl_fill_merge,
1005 err = ovl_dir_read(&path, &rdd);
1009 inode_lock_nested(dir, I_MUTEX_PARENT);
1010 list_for_each_entry(p, &list, l_node) {
1011 if (p->name[0] == '.') {
1014 if (p->len == 2 && p->name[1] == '.')
1017 index = lookup_one_len(p->name, dentry, p->len);
1018 if (IS_ERR(index)) {
1019 err = PTR_ERR(index);
1023 err = ovl_verify_index(index, lowerstack, numlower);
1024 /* Cleanup stale and orphan index entries */
1025 if (err && (err == -ESTALE || err == -ENOENT))
1026 err = ovl_cleanup(dir, index);
1036 ovl_cache_free(&list);
1038 pr_err("overlayfs: failed index dir cleanup (%i)\n", err);