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 {
24 struct list_head l_node;
26 struct ovl_cache_entry *next_maybe_whiteout;
31 struct ovl_dir_cache {
34 struct list_head entries;
37 struct ovl_readdir_data {
38 struct dir_context ctx;
41 struct list_head *list;
42 struct list_head middle;
43 struct ovl_cache_entry *first_maybe_whiteout;
46 bool d_type_supported;
52 struct ovl_dir_cache *cache;
53 struct list_head *cursor;
54 struct file *realfile;
55 struct file *upperfile;
58 static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
60 return container_of(n, struct ovl_cache_entry, node);
63 static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
64 const char *name, int len)
66 struct rb_node *node = root->rb_node;
70 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
72 cmp = strncmp(name, p->name, len);
74 node = p->node.rb_right;
75 else if (cmp < 0 || len < p->len)
76 node = p->node.rb_left;
84 static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
85 const char *name, int len,
86 u64 ino, unsigned int d_type)
88 struct ovl_cache_entry *p;
89 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
91 p = kmalloc(size, GFP_KERNEL);
95 memcpy(p->name, name, len);
100 p->is_whiteout = false;
102 if (d_type == DT_CHR) {
103 p->next_maybe_whiteout = rdd->first_maybe_whiteout;
104 rdd->first_maybe_whiteout = p;
109 static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
110 const char *name, int len, u64 ino,
113 struct rb_node **newp = &rdd->root.rb_node;
114 struct rb_node *parent = NULL;
115 struct ovl_cache_entry *p;
119 struct ovl_cache_entry *tmp;
122 tmp = ovl_cache_entry_from_node(*newp);
123 cmp = strncmp(name, tmp->name, len);
125 newp = &tmp->node.rb_right;
126 else if (cmp < 0 || len < tmp->len)
127 newp = &tmp->node.rb_left;
132 p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
136 list_add_tail(&p->l_node, rdd->list);
137 rb_link_node(&p->node, parent, newp);
138 rb_insert_color(&p->node, &rdd->root);
143 static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
144 const char *name, int namelen,
145 loff_t offset, u64 ino, unsigned int d_type)
147 struct ovl_cache_entry *p;
149 p = ovl_cache_entry_find(&rdd->root, name, namelen);
151 list_move_tail(&p->l_node, &rdd->middle);
153 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
157 list_add_tail(&p->l_node, &rdd->middle);
163 void ovl_cache_free(struct list_head *list)
165 struct ovl_cache_entry *p;
166 struct ovl_cache_entry *n;
168 list_for_each_entry_safe(p, n, list, l_node)
171 INIT_LIST_HEAD(list);
174 static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
176 struct ovl_dir_cache *cache = od->cache;
178 WARN_ON(cache->refcount <= 0);
180 if (!cache->refcount) {
181 if (ovl_dir_cache(dentry) == cache)
182 ovl_set_dir_cache(dentry, NULL);
184 ovl_cache_free(&cache->entries);
189 static int ovl_fill_merge(struct dir_context *ctx, const char *name,
190 int namelen, loff_t offset, u64 ino,
193 struct ovl_readdir_data *rdd =
194 container_of(ctx, struct ovl_readdir_data, ctx);
198 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
200 return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
203 static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
206 struct ovl_cache_entry *p;
207 struct dentry *dentry;
208 const struct cred *old_cred;
209 struct cred *override_cred;
211 override_cred = prepare_creds();
216 * CAP_DAC_OVERRIDE for lookup
218 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
219 old_cred = override_creds(override_cred);
221 inode_lock(dir->d_inode);
223 // XXX: err = mutex_lock_killable(&dir->d_inode->i_mutex);
225 while (rdd->first_maybe_whiteout) {
226 p = rdd->first_maybe_whiteout;
227 rdd->first_maybe_whiteout = p->next_maybe_whiteout;
228 dentry = lookup_one_len(p->name, dir, p->len);
229 if (!IS_ERR(dentry)) {
230 p->is_whiteout = ovl_is_whiteout(dentry);
234 inode_unlock(dir->d_inode);
236 revert_creds(old_cred);
237 put_cred(override_cred);
242 static inline int ovl_dir_read(struct path *realpath,
243 struct ovl_readdir_data *rdd)
245 struct file *realfile;
248 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
249 if (IS_ERR(realfile))
250 return PTR_ERR(realfile);
252 rdd->first_maybe_whiteout = NULL;
257 err = iterate_dir(realfile, &rdd->ctx);
260 } while (!err && rdd->count);
262 if (!err && rdd->first_maybe_whiteout)
263 err = ovl_check_whiteouts(realpath->dentry, rdd);
270 static void ovl_dir_reset(struct file *file)
272 struct ovl_dir_file *od = file->private_data;
273 struct ovl_dir_cache *cache = od->cache;
274 struct dentry *dentry = file->f_path.dentry;
275 enum ovl_path_type type = ovl_path_type(dentry);
277 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
278 ovl_cache_put(od, dentry);
282 WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type));
283 if (od->is_real && OVL_TYPE_MERGE(type))
287 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
290 struct path realpath;
291 struct ovl_readdir_data rdd = {
292 .ctx.actor = ovl_fill_merge,
299 for (idx = 0; idx != -1; idx = next) {
300 next = ovl_path_next(idx, dentry, &realpath);
303 err = ovl_dir_read(&realpath, &rdd);
308 * Insert lowest layer entries before upper ones, this
309 * allows offsets to be reasonably constant
311 list_add(&rdd.middle, rdd.list);
312 rdd.is_lowest = true;
313 err = ovl_dir_read(&realpath, &rdd);
314 list_del(&rdd.middle);
320 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
325 list_for_each(p, &od->cache->entries) {
330 /* Cursor is safe since the cache is stable */
334 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
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 res = ovl_dir_read_merged(dentry, &cache->entries);
355 ovl_cache_free(&cache->entries);
360 cache->version = ovl_dentry_version_get(dentry);
361 ovl_set_dir_cache(dentry, cache);
366 static int ovl_iterate(struct file *file, struct dir_context *ctx)
368 struct ovl_dir_file *od = file->private_data;
369 struct dentry *dentry = file->f_path.dentry;
370 struct ovl_cache_entry *p;
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 != &od->cache->entries) {
390 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
392 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
394 od->cursor = p->l_node.next;
400 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
403 struct ovl_dir_file *od = file->private_data;
405 inode_lock(file_inode(file));
410 res = vfs_llseek(od->realfile, offset, origin);
411 file->f_pos = od->realfile->f_pos;
417 offset += file->f_pos;
427 if (offset != file->f_pos) {
428 file->f_pos = offset;
430 ovl_seek_cursor(od, offset);
435 inode_unlock(file_inode(file));
440 static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
443 struct ovl_dir_file *od = file->private_data;
444 struct dentry *dentry = file->f_path.dentry;
445 struct file *realfile = od->realfile;
448 * Need to check if we started out being a lower dir, but got copied up
450 if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) {
451 struct inode *inode = file_inode(file);
453 realfile = lockless_dereference(od->upperfile);
455 struct path upperpath;
457 ovl_path_upper(dentry, &upperpath);
458 realfile = ovl_path_open(&upperpath, O_RDONLY);
459 smp_mb__before_spinlock();
461 if (!od->upperfile) {
462 if (IS_ERR(realfile)) {
464 return PTR_ERR(realfile);
466 od->upperfile = realfile;
468 /* somebody has beaten us to it */
469 if (!IS_ERR(realfile))
471 realfile = od->upperfile;
477 return vfs_fsync_range(realfile, start, end, datasync);
480 static int ovl_dir_release(struct inode *inode, struct file *file)
482 struct ovl_dir_file *od = file->private_data;
486 ovl_cache_put(od, file->f_path.dentry);
497 static int ovl_dir_open(struct inode *inode, struct file *file)
499 struct path realpath;
500 struct file *realfile;
501 struct ovl_dir_file *od;
502 enum ovl_path_type type;
504 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
508 type = ovl_path_real(file->f_path.dentry, &realpath);
509 realfile = ovl_path_open(&realpath, file->f_flags);
510 if (IS_ERR(realfile)) {
512 return PTR_ERR(realfile);
514 od->realfile = realfile;
515 od->is_real = !OVL_TYPE_MERGE(type);
516 od->is_upper = OVL_TYPE_UPPER(type);
517 file->private_data = od;
522 const struct file_operations ovl_dir_operations = {
523 .read = generic_read_dir,
524 .open = ovl_dir_open,
525 .iterate = ovl_iterate,
526 .llseek = ovl_dir_llseek,
527 .fsync = ovl_dir_fsync,
528 .release = ovl_dir_release,
531 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
534 struct ovl_cache_entry *p;
536 err = ovl_dir_read_merged(dentry, list);
542 list_for_each_entry(p, list, l_node) {
546 if (p->name[0] == '.') {
549 if (p->len == 2 && p->name[1] == '.')
559 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
561 struct ovl_cache_entry *p;
563 inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
564 list_for_each_entry(p, list, l_node) {
565 struct dentry *dentry;
570 dentry = lookup_one_len(p->name, upper, p->len);
571 if (IS_ERR(dentry)) {
572 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
573 upper->d_name.name, p->len, p->name,
574 (int) PTR_ERR(dentry));
578 ovl_cleanup(upper->d_inode, dentry);
581 inode_unlock(upper->d_inode);
584 static int ovl_check_d_type(struct dir_context *ctx, const char *name,
585 int namelen, loff_t offset, u64 ino,
588 struct ovl_readdir_data *rdd =
589 container_of(ctx, struct ovl_readdir_data, ctx);
591 /* Even if d_type is not supported, DT_DIR is returned for . and .. */
592 if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
595 if (d_type != DT_UNKNOWN)
596 rdd->d_type_supported = true;
602 * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
603 * if error is encountered.
605 int ovl_check_d_type_supported(struct path *realpath)
608 struct ovl_readdir_data rdd = {
609 .ctx.actor = ovl_check_d_type,
610 .d_type_supported = false,
613 err = ovl_dir_read(realpath, &rdd);
617 return rdd.d_type_supported;