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 {
26 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;
86 p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
88 char *name_copy = (char *) (p + 1);
89 memcpy(name_copy, name, len);
90 name_copy[len] = '\0';
95 p->is_whiteout = false;
101 static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
102 const char *name, int len, u64 ino,
105 struct rb_node **newp = &rdd->root->rb_node;
106 struct rb_node *parent = NULL;
107 struct ovl_cache_entry *p;
111 struct ovl_cache_entry *tmp;
114 tmp = ovl_cache_entry_from_node(*newp);
115 cmp = strncmp(name, tmp->name, len);
117 newp = &tmp->node.rb_right;
118 else if (cmp < 0 || len < tmp->len)
119 newp = &tmp->node.rb_left;
124 p = ovl_cache_entry_new(name, len, ino, d_type);
128 list_add_tail(&p->l_node, rdd->list);
129 rb_link_node(&p->node, parent, newp);
130 rb_insert_color(&p->node, rdd->root);
135 static int ovl_fill_lower(struct ovl_readdir_data *rdd,
136 const char *name, int namelen,
137 loff_t offset, u64 ino, unsigned int d_type)
139 struct ovl_cache_entry *p;
141 p = ovl_cache_entry_find(rdd->root, name, namelen);
143 list_move_tail(&p->l_node, rdd->middle);
145 p = ovl_cache_entry_new(name, namelen, ino, d_type);
149 list_add_tail(&p->l_node, rdd->middle);
155 void ovl_cache_free(struct list_head *list)
157 struct ovl_cache_entry *p;
158 struct ovl_cache_entry *n;
160 list_for_each_entry_safe(p, n, list, l_node)
163 INIT_LIST_HEAD(list);
166 static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
168 struct ovl_dir_cache *cache = od->cache;
170 list_del(&od->cursor.l_node);
171 WARN_ON(cache->refcount <= 0);
173 if (!cache->refcount) {
174 if (ovl_dir_cache(dentry) == cache)
175 ovl_set_dir_cache(dentry, NULL);
177 ovl_cache_free(&cache->entries);
182 static int ovl_fill_merge(void *buf, const char *name, int namelen,
183 loff_t offset, u64 ino, unsigned int d_type)
185 struct ovl_readdir_data *rdd = buf;
189 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
191 return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
194 static inline int ovl_dir_read(struct path *realpath,
195 struct ovl_readdir_data *rdd)
197 struct file *realfile;
200 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
201 if (IS_ERR(realfile))
202 return PTR_ERR(realfile);
208 err = iterate_dir(realfile, &rdd->ctx);
211 } while (!err && rdd->count);
217 static void ovl_dir_reset(struct file *file)
219 struct ovl_dir_file *od = file->private_data;
220 struct ovl_dir_cache *cache = od->cache;
221 struct dentry *dentry = file->f_path.dentry;
222 enum ovl_path_type type = ovl_path_type(dentry);
224 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
225 ovl_cache_put(od, dentry);
228 WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
229 if (od->is_real && type == OVL_PATH_MERGE)
233 static int ovl_dir_mark_whiteouts(struct dentry *dir,
234 struct ovl_readdir_data *rdd)
236 struct ovl_cache_entry *p;
237 struct dentry *dentry;
238 const struct cred *old_cred;
239 struct cred *override_cred;
241 override_cred = prepare_creds();
242 if (!override_cred) {
243 ovl_cache_free(rdd->list);
248 * CAP_DAC_OVERRIDE for lookup
250 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
251 old_cred = override_creds(override_cred);
253 mutex_lock(&dir->d_inode->i_mutex);
254 list_for_each_entry(p, rdd->list, l_node) {
258 if (p->type != DT_CHR)
261 dentry = lookup_one_len(p->name, dir, p->len);
265 p->is_whiteout = ovl_is_whiteout(dentry);
268 mutex_unlock(&dir->d_inode->i_mutex);
270 revert_creds(old_cred);
271 put_cred(override_cred);
276 static inline int ovl_dir_read_merged(struct path *upperpath,
277 struct path *lowerpath,
278 struct list_head *list)
281 struct rb_root root = RB_ROOT;
282 struct list_head middle;
283 struct ovl_readdir_data rdd = {
284 .ctx.actor = ovl_fill_merge,
290 if (upperpath->dentry) {
291 err = ovl_dir_read(upperpath, &rdd);
295 if (lowerpath->dentry) {
296 err = ovl_dir_mark_whiteouts(upperpath->dentry, &rdd);
301 if (lowerpath->dentry) {
303 * Insert lowerpath entries before upperpath ones, this allows
304 * offsets to be reasonably constant
306 list_add(&middle, rdd.list);
307 rdd.middle = &middle;
309 err = ovl_dir_read(lowerpath, &rdd);
317 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
319 struct ovl_cache_entry *p;
322 list_for_each_entry(p, &od->cache->entries, l_node) {
329 list_move_tail(&od->cursor.l_node, &p->l_node);
332 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
335 struct path lowerpath;
336 struct path upperpath;
337 struct ovl_dir_cache *cache;
339 cache = ovl_dir_cache(dentry);
340 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
344 ovl_set_dir_cache(dentry, NULL);
346 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
348 return ERR_PTR(-ENOMEM);
351 INIT_LIST_HEAD(&cache->entries);
353 ovl_path_lower(dentry, &lowerpath);
354 ovl_path_upper(dentry, &upperpath);
356 res = ovl_dir_read_merged(&upperpath, &lowerpath, &cache->entries);
358 ovl_cache_free(&cache->entries);
363 cache->version = ovl_dentry_version_get(dentry);
364 ovl_set_dir_cache(dentry, cache);
369 static int ovl_iterate(struct file *file, struct dir_context *ctx)
371 struct ovl_dir_file *od = file->private_data;
372 struct dentry *dentry = file->f_path.dentry;
378 return iterate_dir(od->realfile, ctx);
381 struct ovl_dir_cache *cache;
383 cache = ovl_cache_get(dentry);
385 return PTR_ERR(cache);
388 ovl_seek_cursor(od, ctx->pos);
391 while (od->cursor.l_node.next != &od->cache->entries) {
392 struct ovl_cache_entry *p;
394 p = list_entry(od->cursor.l_node.next, struct ovl_cache_entry, l_node);
397 if (!p->is_whiteout) {
398 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
403 list_move(&od->cursor.l_node, &p->l_node);
408 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
411 struct ovl_dir_file *od = file->private_data;
413 mutex_lock(&file_inode(file)->i_mutex);
418 res = vfs_llseek(od->realfile, offset, origin);
419 file->f_pos = od->realfile->f_pos;
425 offset += file->f_pos;
435 if (offset != file->f_pos) {
436 file->f_pos = offset;
438 ovl_seek_cursor(od, offset);
443 mutex_unlock(&file_inode(file)->i_mutex);
448 static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
451 struct ovl_dir_file *od = file->private_data;
452 struct dentry *dentry = file->f_path.dentry;
453 struct file *realfile = od->realfile;
456 * Need to check if we started out being a lower dir, but got copied up
458 if (!od->is_upper && ovl_path_type(dentry) == OVL_PATH_MERGE) {
459 struct inode *inode = file_inode(file);
461 realfile = od->upperfile;
463 struct path upperpath;
465 ovl_path_upper(dentry, &upperpath);
466 realfile = ovl_path_open(&upperpath, O_RDONLY);
467 mutex_lock(&inode->i_mutex);
468 if (!od->upperfile) {
469 if (IS_ERR(realfile)) {
470 mutex_unlock(&inode->i_mutex);
471 return PTR_ERR(realfile);
473 od->upperfile = realfile;
475 /* somebody has beaten us to it */
476 if (!IS_ERR(realfile))
478 realfile = od->upperfile;
480 mutex_unlock(&inode->i_mutex);
484 return vfs_fsync_range(realfile, start, end, datasync);
487 static int ovl_dir_release(struct inode *inode, struct file *file)
489 struct ovl_dir_file *od = file->private_data;
492 mutex_lock(&inode->i_mutex);
493 ovl_cache_put(od, file->f_path.dentry);
494 mutex_unlock(&inode->i_mutex);
504 static int ovl_dir_open(struct inode *inode, struct file *file)
506 struct path realpath;
507 struct file *realfile;
508 struct ovl_dir_file *od;
509 enum ovl_path_type type;
511 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
515 type = ovl_path_real(file->f_path.dentry, &realpath);
516 realfile = ovl_path_open(&realpath, file->f_flags);
517 if (IS_ERR(realfile)) {
519 return PTR_ERR(realfile);
521 INIT_LIST_HEAD(&od->cursor.l_node);
522 od->realfile = realfile;
523 od->is_real = (type != OVL_PATH_MERGE);
524 od->is_upper = (type != OVL_PATH_LOWER);
525 file->private_data = od;
530 const struct file_operations ovl_dir_operations = {
531 .read = generic_read_dir,
532 .open = ovl_dir_open,
533 .iterate = ovl_iterate,
534 .llseek = ovl_dir_llseek,
535 .fsync = ovl_dir_fsync,
536 .release = ovl_dir_release,
539 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
542 struct path lowerpath;
543 struct path upperpath;
544 struct ovl_cache_entry *p;
546 ovl_path_upper(dentry, &upperpath);
547 ovl_path_lower(dentry, &lowerpath);
549 err = ovl_dir_read_merged(&upperpath, &lowerpath, list);
555 list_for_each_entry(p, list, l_node) {
559 if (p->name[0] == '.') {
562 if (p->len == 2 && p->name[1] == '.')
572 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
574 struct ovl_cache_entry *p;
576 mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_PARENT);
577 list_for_each_entry(p, list, l_node) {
578 struct dentry *dentry;
583 dentry = lookup_one_len(p->name, upper, p->len);
584 if (IS_ERR(dentry)) {
585 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
586 upper->d_name.name, p->len, p->name,
587 (int) PTR_ERR(dentry));
590 ovl_cleanup(upper->d_inode, dentry);
593 mutex_unlock(&upper->d_inode->i_mutex);