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;
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;
50 struct ovl_dir_cache *cache;
51 struct list_head *cursor;
52 struct file *realfile;
53 struct file *upperfile;
56 static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
58 return container_of(n, struct ovl_cache_entry, node);
61 static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
62 const char *name, int len)
64 struct rb_node *node = root->rb_node;
68 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
70 cmp = strncmp(name, p->name, len);
72 node = p->node.rb_right;
73 else if (cmp < 0 || len < p->len)
74 node = p->node.rb_left;
82 static struct ovl_cache_entry *ovl_cache_entry_new(struct dentry *dir,
83 const char *name, int len,
84 u64 ino, unsigned int d_type)
86 struct ovl_cache_entry *p;
87 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
89 p = kmalloc(size, GFP_KERNEL);
93 memcpy(p->name, name, len);
98 p->is_whiteout = false;
100 if (d_type == DT_CHR) {
101 struct dentry *dentry;
102 const struct cred *old_cred;
103 struct cred *override_cred;
105 override_cred = prepare_creds();
106 if (!override_cred) {
112 * CAP_DAC_OVERRIDE for lookup
114 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
115 old_cred = override_creds(override_cred);
117 dentry = lookup_one_len(name, dir, len);
118 if (!IS_ERR(dentry)) {
119 p->is_whiteout = ovl_is_whiteout(dentry);
122 revert_creds(old_cred);
123 put_cred(override_cred);
128 static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
129 const char *name, int len, u64 ino,
132 struct rb_node **newp = &rdd->root.rb_node;
133 struct rb_node *parent = NULL;
134 struct ovl_cache_entry *p;
138 struct ovl_cache_entry *tmp;
141 tmp = ovl_cache_entry_from_node(*newp);
142 cmp = strncmp(name, tmp->name, len);
144 newp = &tmp->node.rb_right;
145 else if (cmp < 0 || len < tmp->len)
146 newp = &tmp->node.rb_left;
151 p = ovl_cache_entry_new(rdd->dir, name, len, ino, d_type);
155 list_add_tail(&p->l_node, rdd->list);
156 rb_link_node(&p->node, parent, newp);
157 rb_insert_color(&p->node, &rdd->root);
162 static int ovl_fill_lower(struct ovl_readdir_data *rdd,
163 const char *name, int namelen,
164 loff_t offset, u64 ino, unsigned int d_type)
166 struct ovl_cache_entry *p;
168 p = ovl_cache_entry_find(&rdd->root, name, namelen);
170 list_move_tail(&p->l_node, &rdd->middle);
172 p = ovl_cache_entry_new(rdd->dir, name, namelen, ino, d_type);
176 list_add_tail(&p->l_node, &rdd->middle);
182 void ovl_cache_free(struct list_head *list)
184 struct ovl_cache_entry *p;
185 struct ovl_cache_entry *n;
187 list_for_each_entry_safe(p, n, list, l_node)
190 INIT_LIST_HEAD(list);
193 static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
195 struct ovl_dir_cache *cache = od->cache;
197 WARN_ON(cache->refcount <= 0);
199 if (!cache->refcount) {
200 if (ovl_dir_cache(dentry) == cache)
201 ovl_set_dir_cache(dentry, NULL);
203 ovl_cache_free(&cache->entries);
208 static int ovl_fill_merge(struct dir_context *ctx, const char *name,
209 int namelen, loff_t offset, u64 ino,
212 struct ovl_readdir_data *rdd =
213 container_of(ctx, struct ovl_readdir_data, ctx);
217 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
219 return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
222 static inline int ovl_dir_read(struct path *realpath,
223 struct ovl_readdir_data *rdd)
225 struct file *realfile;
228 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
229 if (IS_ERR(realfile))
230 return PTR_ERR(realfile);
232 rdd->dir = realpath->dentry;
237 err = iterate_dir(realfile, &rdd->ctx);
240 } while (!err && rdd->count);
246 static void ovl_dir_reset(struct file *file)
248 struct ovl_dir_file *od = file->private_data;
249 struct ovl_dir_cache *cache = od->cache;
250 struct dentry *dentry = file->f_path.dentry;
251 enum ovl_path_type type = ovl_path_type(dentry);
253 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
254 ovl_cache_put(od, dentry);
258 WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type));
259 if (od->is_real && OVL_TYPE_MERGE(type))
263 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
266 struct path realpath;
267 struct ovl_readdir_data rdd = {
268 .ctx.actor = ovl_fill_merge,
275 for (idx = 0; idx != -1; idx = next) {
276 next = ovl_path_next(idx, dentry, &realpath);
279 err = ovl_dir_read(&realpath, &rdd);
284 * Insert lowest layer entries before upper ones, this
285 * allows offsets to be reasonably constant
287 list_add(&rdd.middle, rdd.list);
289 err = ovl_dir_read(&realpath, &rdd);
290 list_del(&rdd.middle);
296 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
301 list_for_each(p, &od->cache->entries) {
306 /* Cursor is safe since the cache is stable */
310 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
313 struct ovl_dir_cache *cache;
315 cache = ovl_dir_cache(dentry);
316 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
320 ovl_set_dir_cache(dentry, NULL);
322 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
324 return ERR_PTR(-ENOMEM);
327 INIT_LIST_HEAD(&cache->entries);
329 res = ovl_dir_read_merged(dentry, &cache->entries);
331 ovl_cache_free(&cache->entries);
336 cache->version = ovl_dentry_version_get(dentry);
337 ovl_set_dir_cache(dentry, cache);
342 static int ovl_iterate(struct file *file, struct dir_context *ctx)
344 struct ovl_dir_file *od = file->private_data;
345 struct dentry *dentry = file->f_path.dentry;
346 struct ovl_cache_entry *p;
352 return iterate_dir(od->realfile, ctx);
355 struct ovl_dir_cache *cache;
357 cache = ovl_cache_get(dentry);
359 return PTR_ERR(cache);
362 ovl_seek_cursor(od, ctx->pos);
365 while (od->cursor != &od->cache->entries) {
366 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
368 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
370 od->cursor = p->l_node.next;
376 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
379 struct ovl_dir_file *od = file->private_data;
381 mutex_lock(&file_inode(file)->i_mutex);
386 res = vfs_llseek(od->realfile, offset, origin);
387 file->f_pos = od->realfile->f_pos;
393 offset += file->f_pos;
403 if (offset != file->f_pos) {
404 file->f_pos = offset;
406 ovl_seek_cursor(od, offset);
411 mutex_unlock(&file_inode(file)->i_mutex);
416 static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
419 struct ovl_dir_file *od = file->private_data;
420 struct dentry *dentry = file->f_path.dentry;
421 struct file *realfile = od->realfile;
424 * Need to check if we started out being a lower dir, but got copied up
426 if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) {
427 struct inode *inode = file_inode(file);
429 realfile = lockless_dereference(od->upperfile);
431 struct path upperpath;
433 ovl_path_upper(dentry, &upperpath);
434 realfile = ovl_path_open(&upperpath, O_RDONLY);
435 smp_mb__before_spinlock();
436 mutex_lock(&inode->i_mutex);
437 if (!od->upperfile) {
438 if (IS_ERR(realfile)) {
439 mutex_unlock(&inode->i_mutex);
440 return PTR_ERR(realfile);
442 od->upperfile = realfile;
444 /* somebody has beaten us to it */
445 if (!IS_ERR(realfile))
447 realfile = od->upperfile;
449 mutex_unlock(&inode->i_mutex);
453 return vfs_fsync_range(realfile, start, end, datasync);
456 static int ovl_dir_release(struct inode *inode, struct file *file)
458 struct ovl_dir_file *od = file->private_data;
461 mutex_lock(&inode->i_mutex);
462 ovl_cache_put(od, file->f_path.dentry);
463 mutex_unlock(&inode->i_mutex);
473 static int ovl_dir_open(struct inode *inode, struct file *file)
475 struct path realpath;
476 struct file *realfile;
477 struct ovl_dir_file *od;
478 enum ovl_path_type type;
480 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
484 type = ovl_path_real(file->f_path.dentry, &realpath);
485 realfile = ovl_path_open(&realpath, file->f_flags);
486 if (IS_ERR(realfile)) {
488 return PTR_ERR(realfile);
490 od->realfile = realfile;
491 od->is_real = !OVL_TYPE_MERGE(type);
492 od->is_upper = OVL_TYPE_UPPER(type);
493 file->private_data = od;
498 const struct file_operations ovl_dir_operations = {
499 .read = generic_read_dir,
500 .open = ovl_dir_open,
501 .iterate = ovl_iterate,
502 .llseek = ovl_dir_llseek,
503 .fsync = ovl_dir_fsync,
504 .release = ovl_dir_release,
507 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
510 struct ovl_cache_entry *p;
512 err = ovl_dir_read_merged(dentry, list);
518 list_for_each_entry(p, list, l_node) {
522 if (p->name[0] == '.') {
525 if (p->len == 2 && p->name[1] == '.')
535 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
537 struct ovl_cache_entry *p;
539 mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_CHILD);
540 list_for_each_entry(p, list, l_node) {
541 struct dentry *dentry;
546 dentry = lookup_one_len(p->name, upper, p->len);
547 if (IS_ERR(dentry)) {
548 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
549 upper->d_name.name, p->len, p->name,
550 (int) PTR_ERR(dentry));
553 ovl_cleanup(upper->d_inode, dentry);
556 mutex_unlock(&upper->d_inode->i_mutex);