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 "overlayfs.h"
20 struct ovl_cache_entry {
25 struct list_head l_node;
30 struct ovl_dir_cache {
33 struct list_head entries;
36 struct ovl_readdir_data {
37 struct dir_context ctx;
40 struct list_head *list;
41 struct list_head *middle;
49 struct ovl_dir_cache *cache;
50 struct ovl_cache_entry cursor;
51 struct file *realfile;
52 struct file *upperfile;
55 static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
57 return container_of(n, struct ovl_cache_entry, node);
60 static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
61 const char *name, int len)
63 struct rb_node *node = root->rb_node;
67 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
69 cmp = strncmp(name, p->name, len);
71 node = p->node.rb_right;
72 else if (cmp < 0 || len < p->len)
73 node = p->node.rb_left;
81 static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
82 u64 ino, unsigned int d_type)
84 struct ovl_cache_entry *p;
85 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
87 p = kmalloc(size, GFP_KERNEL);
89 memcpy(p->name, name, len);
94 p->is_whiteout = false;
100 static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
101 const char *name, int len, u64 ino,
104 struct rb_node **newp = &rdd->root.rb_node;
105 struct rb_node *parent = NULL;
106 struct ovl_cache_entry *p;
110 struct ovl_cache_entry *tmp;
113 tmp = ovl_cache_entry_from_node(*newp);
114 cmp = strncmp(name, tmp->name, len);
116 newp = &tmp->node.rb_right;
117 else if (cmp < 0 || len < tmp->len)
118 newp = &tmp->node.rb_left;
123 p = ovl_cache_entry_new(name, len, ino, d_type);
127 list_add_tail(&p->l_node, rdd->list);
128 rb_link_node(&p->node, parent, newp);
129 rb_insert_color(&p->node, &rdd->root);
134 static int ovl_fill_lower(struct ovl_readdir_data *rdd,
135 const char *name, int namelen,
136 loff_t offset, u64 ino, unsigned int d_type)
138 struct ovl_cache_entry *p;
140 p = ovl_cache_entry_find(&rdd->root, name, namelen);
142 list_move_tail(&p->l_node, rdd->middle);
144 p = ovl_cache_entry_new(name, namelen, ino, d_type);
148 list_add_tail(&p->l_node, rdd->middle);
154 void ovl_cache_free(struct list_head *list)
156 struct ovl_cache_entry *p;
157 struct ovl_cache_entry *n;
159 list_for_each_entry_safe(p, n, list, l_node)
162 INIT_LIST_HEAD(list);
165 static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
167 struct ovl_dir_cache *cache = od->cache;
169 list_del(&od->cursor.l_node);
170 WARN_ON(cache->refcount <= 0);
172 if (!cache->refcount) {
173 if (ovl_dir_cache(dentry) == cache)
174 ovl_set_dir_cache(dentry, NULL);
176 ovl_cache_free(&cache->entries);
181 static int ovl_fill_merge(void *buf, const char *name, int namelen,
182 loff_t offset, u64 ino, unsigned int d_type)
184 struct ovl_readdir_data *rdd = buf;
188 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
190 return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
193 static inline int ovl_dir_read(struct path *realpath,
194 struct ovl_readdir_data *rdd)
196 struct file *realfile;
199 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
200 if (IS_ERR(realfile))
201 return PTR_ERR(realfile);
207 err = iterate_dir(realfile, &rdd->ctx);
210 } while (!err && rdd->count);
216 static void ovl_dir_reset(struct file *file)
218 struct ovl_dir_file *od = file->private_data;
219 struct ovl_dir_cache *cache = od->cache;
220 struct dentry *dentry = file->f_path.dentry;
221 enum ovl_path_type type = ovl_path_type(dentry);
223 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
224 ovl_cache_put(od, dentry);
227 WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
228 if (od->is_real && type == OVL_PATH_MERGE)
232 static int ovl_dir_mark_whiteouts(struct dentry *dir,
233 struct ovl_readdir_data *rdd)
235 struct ovl_cache_entry *p;
236 struct dentry *dentry;
237 const struct cred *old_cred;
238 struct cred *override_cred;
240 override_cred = prepare_creds();
241 if (!override_cred) {
242 ovl_cache_free(rdd->list);
247 * CAP_DAC_OVERRIDE for lookup
249 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
250 old_cred = override_creds(override_cred);
252 mutex_lock(&dir->d_inode->i_mutex);
253 list_for_each_entry(p, rdd->list, l_node) {
257 if (p->type != DT_CHR)
260 dentry = lookup_one_len(p->name, dir, p->len);
264 p->is_whiteout = ovl_is_whiteout(dentry);
267 mutex_unlock(&dir->d_inode->i_mutex);
269 revert_creds(old_cred);
270 put_cred(override_cred);
275 static inline int ovl_dir_read_merged(struct path *upperpath,
276 struct path *lowerpath,
277 struct list_head *list)
280 struct list_head middle;
281 struct ovl_readdir_data rdd = {
282 .ctx.actor = ovl_fill_merge,
288 if (upperpath->dentry) {
289 err = ovl_dir_read(upperpath, &rdd);
293 if (lowerpath->dentry) {
294 err = ovl_dir_mark_whiteouts(upperpath->dentry, &rdd);
299 if (lowerpath->dentry) {
301 * Insert lowerpath entries before upperpath ones, this allows
302 * offsets to be reasonably constant
304 list_add(&middle, rdd.list);
305 rdd.middle = &middle;
307 err = ovl_dir_read(lowerpath, &rdd);
315 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
317 struct ovl_cache_entry *p;
320 list_for_each_entry(p, &od->cache->entries, l_node) {
327 list_move_tail(&od->cursor.l_node, &p->l_node);
330 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
333 struct path lowerpath;
334 struct path upperpath;
335 struct ovl_dir_cache *cache;
337 cache = ovl_dir_cache(dentry);
338 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
342 ovl_set_dir_cache(dentry, NULL);
344 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
346 return ERR_PTR(-ENOMEM);
349 INIT_LIST_HEAD(&cache->entries);
351 ovl_path_lower(dentry, &lowerpath);
352 ovl_path_upper(dentry, &upperpath);
354 res = ovl_dir_read_merged(&upperpath, &lowerpath, &cache->entries);
356 ovl_cache_free(&cache->entries);
361 cache->version = ovl_dentry_version_get(dentry);
362 ovl_set_dir_cache(dentry, cache);
367 static int ovl_iterate(struct file *file, struct dir_context *ctx)
369 struct ovl_dir_file *od = file->private_data;
370 struct dentry *dentry = file->f_path.dentry;
376 return iterate_dir(od->realfile, ctx);
379 struct ovl_dir_cache *cache;
381 cache = ovl_cache_get(dentry);
383 return PTR_ERR(cache);
386 ovl_seek_cursor(od, ctx->pos);
389 while (od->cursor.l_node.next != &od->cache->entries) {
390 struct ovl_cache_entry *p;
392 p = list_entry(od->cursor.l_node.next, struct ovl_cache_entry, l_node);
395 if (!p->is_whiteout) {
396 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
401 list_move(&od->cursor.l_node, &p->l_node);
406 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
409 struct ovl_dir_file *od = file->private_data;
411 mutex_lock(&file_inode(file)->i_mutex);
416 res = vfs_llseek(od->realfile, offset, origin);
417 file->f_pos = od->realfile->f_pos;
423 offset += file->f_pos;
433 if (offset != file->f_pos) {
434 file->f_pos = offset;
436 ovl_seek_cursor(od, offset);
441 mutex_unlock(&file_inode(file)->i_mutex);
446 static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
449 struct ovl_dir_file *od = file->private_data;
450 struct dentry *dentry = file->f_path.dentry;
451 struct file *realfile = od->realfile;
454 * Need to check if we started out being a lower dir, but got copied up
456 if (!od->is_upper && ovl_path_type(dentry) == OVL_PATH_MERGE) {
457 struct inode *inode = file_inode(file);
459 realfile = od->upperfile;
461 struct path upperpath;
463 ovl_path_upper(dentry, &upperpath);
464 realfile = ovl_path_open(&upperpath, O_RDONLY);
465 mutex_lock(&inode->i_mutex);
466 if (!od->upperfile) {
467 if (IS_ERR(realfile)) {
468 mutex_unlock(&inode->i_mutex);
469 return PTR_ERR(realfile);
471 od->upperfile = realfile;
473 /* somebody has beaten us to it */
474 if (!IS_ERR(realfile))
476 realfile = od->upperfile;
478 mutex_unlock(&inode->i_mutex);
482 return vfs_fsync_range(realfile, start, end, datasync);
485 static int ovl_dir_release(struct inode *inode, struct file *file)
487 struct ovl_dir_file *od = file->private_data;
490 mutex_lock(&inode->i_mutex);
491 ovl_cache_put(od, file->f_path.dentry);
492 mutex_unlock(&inode->i_mutex);
502 static int ovl_dir_open(struct inode *inode, struct file *file)
504 struct path realpath;
505 struct file *realfile;
506 struct ovl_dir_file *od;
507 enum ovl_path_type type;
509 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
513 type = ovl_path_real(file->f_path.dentry, &realpath);
514 realfile = ovl_path_open(&realpath, file->f_flags);
515 if (IS_ERR(realfile)) {
517 return PTR_ERR(realfile);
519 INIT_LIST_HEAD(&od->cursor.l_node);
520 od->realfile = realfile;
521 od->is_real = (type != OVL_PATH_MERGE);
522 od->is_upper = (type != OVL_PATH_LOWER);
523 file->private_data = od;
528 const struct file_operations ovl_dir_operations = {
529 .read = generic_read_dir,
530 .open = ovl_dir_open,
531 .iterate = ovl_iterate,
532 .llseek = ovl_dir_llseek,
533 .fsync = ovl_dir_fsync,
534 .release = ovl_dir_release,
537 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
540 struct path lowerpath;
541 struct path upperpath;
542 struct ovl_cache_entry *p;
544 ovl_path_upper(dentry, &upperpath);
545 ovl_path_lower(dentry, &lowerpath);
547 err = ovl_dir_read_merged(&upperpath, &lowerpath, list);
553 list_for_each_entry(p, list, l_node) {
557 if (p->name[0] == '.') {
560 if (p->len == 2 && p->name[1] == '.')
570 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
572 struct ovl_cache_entry *p;
574 mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_PARENT);
575 list_for_each_entry(p, list, l_node) {
576 struct dentry *dentry;
581 dentry = lookup_one_len(p->name, upper, p->len);
582 if (IS_ERR(dentry)) {
583 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
584 upper->d_name.name, p->len, p->name,
585 (int) PTR_ERR(dentry));
588 ovl_cleanup(upper->d_inode, dentry);
591 mutex_unlock(&upper->d_inode->i_mutex);