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
;
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
;
51 struct ovl_dir_cache
*cache
;
52 struct ovl_cache_entry 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 dentry
*dir
,
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;
100 p
->is_cursor
= false;
102 if (d_type
== DT_CHR
) {
103 struct dentry
*dentry
;
104 const struct cred
*old_cred
;
105 struct cred
*override_cred
;
107 override_cred
= prepare_creds();
108 if (!override_cred
) {
114 * CAP_DAC_OVERRIDE for lookup
116 cap_raise(override_cred
->cap_effective
, CAP_DAC_OVERRIDE
);
117 old_cred
= override_creds(override_cred
);
119 dentry
= lookup_one_len(name
, dir
, len
);
120 if (!IS_ERR(dentry
)) {
121 p
->is_whiteout
= ovl_is_whiteout(dentry
);
124 revert_creds(old_cred
);
125 put_cred(override_cred
);
130 static int ovl_cache_entry_add_rb(struct ovl_readdir_data
*rdd
,
131 const char *name
, int len
, u64 ino
,
134 struct rb_node
**newp
= &rdd
->root
.rb_node
;
135 struct rb_node
*parent
= NULL
;
136 struct ovl_cache_entry
*p
;
140 struct ovl_cache_entry
*tmp
;
143 tmp
= ovl_cache_entry_from_node(*newp
);
144 cmp
= strncmp(name
, tmp
->name
, len
);
146 newp
= &tmp
->node
.rb_right
;
147 else if (cmp
< 0 || len
< tmp
->len
)
148 newp
= &tmp
->node
.rb_left
;
153 p
= ovl_cache_entry_new(rdd
->dir
, name
, len
, ino
, d_type
);
157 list_add_tail(&p
->l_node
, rdd
->list
);
158 rb_link_node(&p
->node
, parent
, newp
);
159 rb_insert_color(&p
->node
, &rdd
->root
);
164 static int ovl_fill_lower(struct ovl_readdir_data
*rdd
,
165 const char *name
, int namelen
,
166 loff_t offset
, u64 ino
, unsigned int d_type
)
168 struct ovl_cache_entry
*p
;
170 p
= ovl_cache_entry_find(&rdd
->root
, name
, namelen
);
172 list_move_tail(&p
->l_node
, &rdd
->middle
);
174 p
= ovl_cache_entry_new(rdd
->dir
, name
, namelen
, ino
, d_type
);
178 list_add_tail(&p
->l_node
, &rdd
->middle
);
184 void ovl_cache_free(struct list_head
*list
)
186 struct ovl_cache_entry
*p
;
187 struct ovl_cache_entry
*n
;
189 list_for_each_entry_safe(p
, n
, list
, l_node
)
192 INIT_LIST_HEAD(list
);
195 static void ovl_cache_put(struct ovl_dir_file
*od
, struct dentry
*dentry
)
197 struct ovl_dir_cache
*cache
= od
->cache
;
199 list_del_init(&od
->cursor
.l_node
);
200 WARN_ON(cache
->refcount
<= 0);
202 if (!cache
->refcount
) {
203 if (ovl_dir_cache(dentry
) == cache
)
204 ovl_set_dir_cache(dentry
, NULL
);
206 ovl_cache_free(&cache
->entries
);
211 static int ovl_fill_merge(void *buf
, const char *name
, int namelen
,
212 loff_t offset
, u64 ino
, unsigned int d_type
)
214 struct ovl_readdir_data
*rdd
= buf
;
218 return ovl_cache_entry_add_rb(rdd
, name
, namelen
, ino
, d_type
);
220 return ovl_fill_lower(rdd
, name
, namelen
, offset
, ino
, d_type
);
223 static inline int ovl_dir_read(struct path
*realpath
,
224 struct ovl_readdir_data
*rdd
)
226 struct file
*realfile
;
229 realfile
= ovl_path_open(realpath
, O_RDONLY
| O_DIRECTORY
);
230 if (IS_ERR(realfile
))
231 return PTR_ERR(realfile
);
233 rdd
->dir
= realpath
->dentry
;
238 err
= iterate_dir(realfile
, &rdd
->ctx
);
241 } while (!err
&& rdd
->count
);
247 static void ovl_dir_reset(struct file
*file
)
249 struct ovl_dir_file
*od
= file
->private_data
;
250 struct ovl_dir_cache
*cache
= od
->cache
;
251 struct dentry
*dentry
= file
->f_path
.dentry
;
252 enum ovl_path_type type
= ovl_path_type(dentry
);
254 if (cache
&& ovl_dentry_version_get(dentry
) != cache
->version
) {
255 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
)
298 struct ovl_cache_entry
*p
;
301 list_for_each_entry(p
, &od
->cache
->entries
, l_node
) {
308 list_move_tail(&od
->cursor
.l_node
, &p
->l_node
);
311 static struct ovl_dir_cache
*ovl_cache_get(struct dentry
*dentry
)
314 struct ovl_dir_cache
*cache
;
316 cache
= ovl_dir_cache(dentry
);
317 if (cache
&& ovl_dentry_version_get(dentry
) == cache
->version
) {
321 ovl_set_dir_cache(dentry
, NULL
);
323 cache
= kzalloc(sizeof(struct ovl_dir_cache
), GFP_KERNEL
);
325 return ERR_PTR(-ENOMEM
);
328 INIT_LIST_HEAD(&cache
->entries
);
330 res
= ovl_dir_read_merged(dentry
, &cache
->entries
);
332 ovl_cache_free(&cache
->entries
);
337 cache
->version
= ovl_dentry_version_get(dentry
);
338 ovl_set_dir_cache(dentry
, cache
);
343 static int ovl_iterate(struct file
*file
, struct dir_context
*ctx
)
345 struct ovl_dir_file
*od
= file
->private_data
;
346 struct dentry
*dentry
= file
->f_path
.dentry
;
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
.l_node
.next
!= &od
->cache
->entries
) {
366 struct ovl_cache_entry
*p
;
368 p
= list_entry(od
->cursor
.l_node
.next
, struct ovl_cache_entry
, l_node
);
371 if (!p
->is_whiteout
) {
372 if (!dir_emit(ctx
, p
->name
, p
->len
, p
->ino
, p
->type
))
377 list_move(&od
->cursor
.l_node
, &p
->l_node
);
382 static loff_t
ovl_dir_llseek(struct file
*file
, loff_t offset
, int origin
)
385 struct ovl_dir_file
*od
= file
->private_data
;
387 mutex_lock(&file_inode(file
)->i_mutex
);
392 res
= vfs_llseek(od
->realfile
, offset
, origin
);
393 file
->f_pos
= od
->realfile
->f_pos
;
399 offset
+= file
->f_pos
;
409 if (offset
!= file
->f_pos
) {
410 file
->f_pos
= offset
;
412 ovl_seek_cursor(od
, offset
);
417 mutex_unlock(&file_inode(file
)->i_mutex
);
422 static int ovl_dir_fsync(struct file
*file
, loff_t start
, loff_t end
,
425 struct ovl_dir_file
*od
= file
->private_data
;
426 struct dentry
*dentry
= file
->f_path
.dentry
;
427 struct file
*realfile
= od
->realfile
;
430 * Need to check if we started out being a lower dir, but got copied up
432 if (!od
->is_upper
&& OVL_TYPE_UPPER(ovl_path_type(dentry
))) {
433 struct inode
*inode
= file_inode(file
);
435 realfile
= lockless_dereference(od
->upperfile
);
437 struct path upperpath
;
439 ovl_path_upper(dentry
, &upperpath
);
440 realfile
= ovl_path_open(&upperpath
, O_RDONLY
);
441 smp_mb__before_spinlock();
442 mutex_lock(&inode
->i_mutex
);
443 if (!od
->upperfile
) {
444 if (IS_ERR(realfile
)) {
445 mutex_unlock(&inode
->i_mutex
);
446 return PTR_ERR(realfile
);
448 od
->upperfile
= realfile
;
450 /* somebody has beaten us to it */
451 if (!IS_ERR(realfile
))
453 realfile
= od
->upperfile
;
455 mutex_unlock(&inode
->i_mutex
);
459 return vfs_fsync_range(realfile
, start
, end
, datasync
);
462 static int ovl_dir_release(struct inode
*inode
, struct file
*file
)
464 struct ovl_dir_file
*od
= file
->private_data
;
467 mutex_lock(&inode
->i_mutex
);
468 ovl_cache_put(od
, file
->f_path
.dentry
);
469 mutex_unlock(&inode
->i_mutex
);
479 static int ovl_dir_open(struct inode
*inode
, struct file
*file
)
481 struct path realpath
;
482 struct file
*realfile
;
483 struct ovl_dir_file
*od
;
484 enum ovl_path_type type
;
486 od
= kzalloc(sizeof(struct ovl_dir_file
), GFP_KERNEL
);
490 type
= ovl_path_real(file
->f_path
.dentry
, &realpath
);
491 realfile
= ovl_path_open(&realpath
, file
->f_flags
);
492 if (IS_ERR(realfile
)) {
494 return PTR_ERR(realfile
);
496 INIT_LIST_HEAD(&od
->cursor
.l_node
);
497 od
->realfile
= realfile
;
498 od
->is_real
= !OVL_TYPE_MERGE(type
);
499 od
->is_upper
= OVL_TYPE_UPPER(type
);
500 od
->cursor
.is_cursor
= true;
501 file
->private_data
= od
;
506 const struct file_operations ovl_dir_operations
= {
507 .read
= generic_read_dir
,
508 .open
= ovl_dir_open
,
509 .iterate
= ovl_iterate
,
510 .llseek
= ovl_dir_llseek
,
511 .fsync
= ovl_dir_fsync
,
512 .release
= ovl_dir_release
,
515 int ovl_check_empty_dir(struct dentry
*dentry
, struct list_head
*list
)
518 struct ovl_cache_entry
*p
;
520 err
= ovl_dir_read_merged(dentry
, list
);
526 list_for_each_entry(p
, list
, l_node
) {
530 if (p
->name
[0] == '.') {
533 if (p
->len
== 2 && p
->name
[1] == '.')
543 void ovl_cleanup_whiteouts(struct dentry
*upper
, struct list_head
*list
)
545 struct ovl_cache_entry
*p
;
547 mutex_lock_nested(&upper
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
548 list_for_each_entry(p
, list
, l_node
) {
549 struct dentry
*dentry
;
554 dentry
= lookup_one_len(p
->name
, upper
, p
->len
);
555 if (IS_ERR(dentry
)) {
556 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
557 upper
->d_name
.name
, p
->len
, p
->name
,
558 (int) PTR_ERR(dentry
));
561 ovl_cleanup(upper
->d_inode
, dentry
);
564 mutex_unlock(&upper
->d_inode
->i_mutex
);