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
;
51 struct ovl_dir_cache
*cache
;
52 struct list_head
*cursor
;
53 struct file
*realfile
;
54 struct file
*upperfile
;
57 static struct ovl_cache_entry
*ovl_cache_entry_from_node(struct rb_node
*n
)
59 return container_of(n
, struct ovl_cache_entry
, node
);
62 static struct ovl_cache_entry
*ovl_cache_entry_find(struct rb_root
*root
,
63 const char *name
, int len
)
65 struct rb_node
*node
= root
->rb_node
;
69 struct ovl_cache_entry
*p
= ovl_cache_entry_from_node(node
);
71 cmp
= strncmp(name
, p
->name
, len
);
73 node
= p
->node
.rb_right
;
74 else if (cmp
< 0 || len
< p
->len
)
75 node
= p
->node
.rb_left
;
83 static struct ovl_cache_entry
*ovl_cache_entry_new(struct ovl_readdir_data
*rdd
,
84 const char *name
, int len
,
85 u64 ino
, unsigned int d_type
)
87 struct ovl_cache_entry
*p
;
88 size_t size
= offsetof(struct ovl_cache_entry
, name
[len
+ 1]);
90 p
= kmalloc(size
, GFP_KERNEL
);
94 memcpy(p
->name
, name
, len
);
99 p
->is_whiteout
= false;
101 if (d_type
== DT_CHR
) {
102 p
->next_maybe_whiteout
= rdd
->first_maybe_whiteout
;
103 rdd
->first_maybe_whiteout
= p
;
108 static int ovl_cache_entry_add_rb(struct ovl_readdir_data
*rdd
,
109 const char *name
, int len
, u64 ino
,
112 struct rb_node
**newp
= &rdd
->root
.rb_node
;
113 struct rb_node
*parent
= NULL
;
114 struct ovl_cache_entry
*p
;
118 struct ovl_cache_entry
*tmp
;
121 tmp
= ovl_cache_entry_from_node(*newp
);
122 cmp
= strncmp(name
, tmp
->name
, len
);
124 newp
= &tmp
->node
.rb_right
;
125 else if (cmp
< 0 || len
< tmp
->len
)
126 newp
= &tmp
->node
.rb_left
;
131 p
= ovl_cache_entry_new(rdd
, name
, len
, ino
, d_type
);
135 list_add_tail(&p
->l_node
, rdd
->list
);
136 rb_link_node(&p
->node
, parent
, newp
);
137 rb_insert_color(&p
->node
, &rdd
->root
);
142 static int ovl_fill_lower(struct ovl_readdir_data
*rdd
,
143 const char *name
, int namelen
,
144 loff_t offset
, u64 ino
, unsigned int d_type
)
146 struct ovl_cache_entry
*p
;
148 p
= ovl_cache_entry_find(&rdd
->root
, name
, namelen
);
150 list_move_tail(&p
->l_node
, &rdd
->middle
);
152 p
= ovl_cache_entry_new(rdd
, name
, namelen
, ino
, d_type
);
156 list_add_tail(&p
->l_node
, &rdd
->middle
);
162 void ovl_cache_free(struct list_head
*list
)
164 struct ovl_cache_entry
*p
;
165 struct ovl_cache_entry
*n
;
167 list_for_each_entry_safe(p
, n
, list
, l_node
)
170 INIT_LIST_HEAD(list
);
173 static void ovl_cache_put(struct ovl_dir_file
*od
, struct dentry
*dentry
)
175 struct ovl_dir_cache
*cache
= od
->cache
;
177 WARN_ON(cache
->refcount
<= 0);
179 if (!cache
->refcount
) {
180 if (ovl_dir_cache(dentry
) == cache
)
181 ovl_set_dir_cache(dentry
, NULL
);
183 ovl_cache_free(&cache
->entries
);
188 static int ovl_fill_merge(struct dir_context
*ctx
, const char *name
,
189 int namelen
, loff_t offset
, u64 ino
,
192 struct ovl_readdir_data
*rdd
=
193 container_of(ctx
, struct ovl_readdir_data
, ctx
);
197 return ovl_cache_entry_add_rb(rdd
, name
, namelen
, ino
, d_type
);
199 return ovl_fill_lower(rdd
, name
, namelen
, offset
, ino
, d_type
);
202 static int ovl_check_whiteouts(struct dentry
*dir
, struct ovl_readdir_data
*rdd
)
205 struct ovl_cache_entry
*p
;
206 struct dentry
*dentry
;
207 const struct cred
*old_cred
;
208 struct cred
*override_cred
;
210 override_cred
= prepare_creds();
215 * CAP_DAC_OVERRIDE for lookup
217 cap_raise(override_cred
->cap_effective
, CAP_DAC_OVERRIDE
);
218 old_cred
= override_creds(override_cred
);
220 err
= mutex_lock_killable(&dir
->d_inode
->i_mutex
);
222 while (rdd
->first_maybe_whiteout
) {
223 p
= rdd
->first_maybe_whiteout
;
224 rdd
->first_maybe_whiteout
= p
->next_maybe_whiteout
;
225 dentry
= lookup_one_len(p
->name
, dir
, p
->len
);
226 if (!IS_ERR(dentry
)) {
227 p
->is_whiteout
= ovl_is_whiteout(dentry
);
231 mutex_unlock(&dir
->d_inode
->i_mutex
);
233 revert_creds(old_cred
);
234 put_cred(override_cred
);
239 static inline int ovl_dir_read(struct path
*realpath
,
240 struct ovl_readdir_data
*rdd
)
242 struct file
*realfile
;
245 realfile
= ovl_path_open(realpath
, O_RDONLY
| O_DIRECTORY
);
246 if (IS_ERR(realfile
))
247 return PTR_ERR(realfile
);
249 rdd
->first_maybe_whiteout
= NULL
;
254 err
= iterate_dir(realfile
, &rdd
->ctx
);
257 } while (!err
&& rdd
->count
);
259 if (!err
&& rdd
->first_maybe_whiteout
)
260 err
= ovl_check_whiteouts(realpath
->dentry
, rdd
);
267 static void ovl_dir_reset(struct file
*file
)
269 struct ovl_dir_file
*od
= file
->private_data
;
270 struct ovl_dir_cache
*cache
= od
->cache
;
271 struct dentry
*dentry
= file
->f_path
.dentry
;
272 enum ovl_path_type type
= ovl_path_type(dentry
);
274 if (cache
&& ovl_dentry_version_get(dentry
) != cache
->version
) {
275 ovl_cache_put(od
, dentry
);
279 WARN_ON(!od
->is_real
&& !OVL_TYPE_MERGE(type
));
280 if (od
->is_real
&& OVL_TYPE_MERGE(type
))
284 static int ovl_dir_read_merged(struct dentry
*dentry
, struct list_head
*list
)
287 struct path realpath
;
288 struct ovl_readdir_data rdd
= {
289 .ctx
.actor
= ovl_fill_merge
,
296 for (idx
= 0; idx
!= -1; idx
= next
) {
297 next
= ovl_path_next(idx
, dentry
, &realpath
);
300 err
= ovl_dir_read(&realpath
, &rdd
);
305 * Insert lowest layer entries before upper ones, this
306 * allows offsets to be reasonably constant
308 list_add(&rdd
.middle
, rdd
.list
);
310 err
= ovl_dir_read(&realpath
, &rdd
);
311 list_del(&rdd
.middle
);
317 static void ovl_seek_cursor(struct ovl_dir_file
*od
, loff_t pos
)
322 list_for_each(p
, &od
->cache
->entries
) {
327 /* Cursor is safe since the cache is stable */
331 static struct ovl_dir_cache
*ovl_cache_get(struct dentry
*dentry
)
334 struct ovl_dir_cache
*cache
;
336 cache
= ovl_dir_cache(dentry
);
337 if (cache
&& ovl_dentry_version_get(dentry
) == cache
->version
) {
341 ovl_set_dir_cache(dentry
, NULL
);
343 cache
= kzalloc(sizeof(struct ovl_dir_cache
), GFP_KERNEL
);
345 return ERR_PTR(-ENOMEM
);
348 INIT_LIST_HEAD(&cache
->entries
);
350 res
= ovl_dir_read_merged(dentry
, &cache
->entries
);
352 ovl_cache_free(&cache
->entries
);
357 cache
->version
= ovl_dentry_version_get(dentry
);
358 ovl_set_dir_cache(dentry
, cache
);
363 static int ovl_iterate(struct file
*file
, struct dir_context
*ctx
)
365 struct ovl_dir_file
*od
= file
->private_data
;
366 struct dentry
*dentry
= file
->f_path
.dentry
;
367 struct ovl_cache_entry
*p
;
373 return iterate_dir(od
->realfile
, ctx
);
376 struct ovl_dir_cache
*cache
;
378 cache
= ovl_cache_get(dentry
);
380 return PTR_ERR(cache
);
383 ovl_seek_cursor(od
, ctx
->pos
);
386 while (od
->cursor
!= &od
->cache
->entries
) {
387 p
= list_entry(od
->cursor
, struct ovl_cache_entry
, l_node
);
389 if (!dir_emit(ctx
, p
->name
, p
->len
, p
->ino
, p
->type
))
391 od
->cursor
= p
->l_node
.next
;
397 static loff_t
ovl_dir_llseek(struct file
*file
, loff_t offset
, int origin
)
400 struct ovl_dir_file
*od
= file
->private_data
;
402 mutex_lock(&file_inode(file
)->i_mutex
);
407 res
= vfs_llseek(od
->realfile
, offset
, origin
);
408 file
->f_pos
= od
->realfile
->f_pos
;
414 offset
+= file
->f_pos
;
424 if (offset
!= file
->f_pos
) {
425 file
->f_pos
= offset
;
427 ovl_seek_cursor(od
, offset
);
432 mutex_unlock(&file_inode(file
)->i_mutex
);
437 static int ovl_dir_fsync(struct file
*file
, loff_t start
, loff_t end
,
440 struct ovl_dir_file
*od
= file
->private_data
;
441 struct dentry
*dentry
= file
->f_path
.dentry
;
442 struct file
*realfile
= od
->realfile
;
445 * Need to check if we started out being a lower dir, but got copied up
447 if (!od
->is_upper
&& OVL_TYPE_UPPER(ovl_path_type(dentry
))) {
448 struct inode
*inode
= file_inode(file
);
450 realfile
= lockless_dereference(od
->upperfile
);
452 struct path upperpath
;
454 ovl_path_upper(dentry
, &upperpath
);
455 realfile
= ovl_path_open(&upperpath
, O_RDONLY
);
456 smp_mb__before_spinlock();
457 mutex_lock(&inode
->i_mutex
);
458 if (!od
->upperfile
) {
459 if (IS_ERR(realfile
)) {
460 mutex_unlock(&inode
->i_mutex
);
461 return PTR_ERR(realfile
);
463 od
->upperfile
= realfile
;
465 /* somebody has beaten us to it */
466 if (!IS_ERR(realfile
))
468 realfile
= od
->upperfile
;
470 mutex_unlock(&inode
->i_mutex
);
474 return vfs_fsync_range(realfile
, start
, end
, datasync
);
477 static int ovl_dir_release(struct inode
*inode
, struct file
*file
)
479 struct ovl_dir_file
*od
= file
->private_data
;
482 mutex_lock(&inode
->i_mutex
);
483 ovl_cache_put(od
, file
->f_path
.dentry
);
484 mutex_unlock(&inode
->i_mutex
);
494 static int ovl_dir_open(struct inode
*inode
, struct file
*file
)
496 struct path realpath
;
497 struct file
*realfile
;
498 struct ovl_dir_file
*od
;
499 enum ovl_path_type type
;
501 od
= kzalloc(sizeof(struct ovl_dir_file
), GFP_KERNEL
);
505 type
= ovl_path_real(file
->f_path
.dentry
, &realpath
);
506 realfile
= ovl_path_open(&realpath
, file
->f_flags
);
507 if (IS_ERR(realfile
)) {
509 return PTR_ERR(realfile
);
511 od
->realfile
= realfile
;
512 od
->is_real
= !OVL_TYPE_MERGE(type
);
513 od
->is_upper
= OVL_TYPE_UPPER(type
);
514 file
->private_data
= od
;
519 const struct file_operations ovl_dir_operations
= {
520 .read
= generic_read_dir
,
521 .open
= ovl_dir_open
,
522 .iterate
= ovl_iterate
,
523 .llseek
= ovl_dir_llseek
,
524 .fsync
= ovl_dir_fsync
,
525 .release
= ovl_dir_release
,
528 int ovl_check_empty_dir(struct dentry
*dentry
, struct list_head
*list
)
531 struct ovl_cache_entry
*p
;
533 err
= ovl_dir_read_merged(dentry
, list
);
539 list_for_each_entry(p
, list
, l_node
) {
543 if (p
->name
[0] == '.') {
546 if (p
->len
== 2 && p
->name
[1] == '.')
556 void ovl_cleanup_whiteouts(struct dentry
*upper
, struct list_head
*list
)
558 struct ovl_cache_entry
*p
;
560 mutex_lock_nested(&upper
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
561 list_for_each_entry(p
, list
, l_node
) {
562 struct dentry
*dentry
;
567 dentry
= lookup_one_len(p
->name
, upper
, p
->len
);
568 if (IS_ERR(dentry
)) {
569 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
570 upper
->d_name
.name
, p
->len
, p
->name
,
571 (int) PTR_ERR(dentry
));
574 ovl_cleanup(upper
->d_inode
, dentry
);
577 mutex_unlock(&upper
->d_inode
->i_mutex
);