blob: 8c8ce9d87ba35cbe02a1607f3f2565e0f2bafc56 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
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.
8 */
9
10#include <linux/fs.h>
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"
19
20struct ovl_cache_entry {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020021 unsigned int len;
22 unsigned int type;
23 u64 ino;
24 bool is_whiteout;
25 struct list_head l_node;
26 struct rb_node node;
Al Viro68bf8612014-10-23 22:58:56 -040027 char name[];
Miklos Szeredie9be9d52014-10-24 00:14:38 +020028};
29
30struct ovl_dir_cache {
31 long refcount;
32 u64 version;
33 struct list_head entries;
34};
35
36struct ovl_readdir_data {
37 struct dir_context ctx;
38 bool is_merge;
Al Viro49be4fb2014-10-23 23:00:53 -040039 struct rb_root root;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020040 struct list_head *list;
Al Virodb6ec212014-10-23 23:03:03 -040041 struct list_head middle;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020042 int count;
43 int err;
44};
45
46struct ovl_dir_file {
47 bool is_real;
48 bool is_upper;
49 struct ovl_dir_cache *cache;
50 struct ovl_cache_entry cursor;
51 struct file *realfile;
52 struct file *upperfile;
53};
54
55static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
56{
57 return container_of(n, struct ovl_cache_entry, node);
58}
59
60static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
61 const char *name, int len)
62{
63 struct rb_node *node = root->rb_node;
64 int cmp;
65
66 while (node) {
67 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
68
69 cmp = strncmp(name, p->name, len);
70 if (cmp > 0)
71 node = p->node.rb_right;
72 else if (cmp < 0 || len < p->len)
73 node = p->node.rb_left;
74 else
75 return p;
76 }
77
78 return NULL;
79}
80
81static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
82 u64 ino, unsigned int d_type)
83{
84 struct ovl_cache_entry *p;
Al Viro68bf8612014-10-23 22:58:56 -040085 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020086
Al Viro68bf8612014-10-23 22:58:56 -040087 p = kmalloc(size, GFP_KERNEL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020088 if (p) {
Al Viro68bf8612014-10-23 22:58:56 -040089 memcpy(p->name, name, len);
90 p->name[len] = '\0';
Miklos Szeredie9be9d52014-10-24 00:14:38 +020091 p->len = len;
92 p->type = d_type;
93 p->ino = ino;
94 p->is_whiteout = false;
95 }
96
97 return p;
98}
99
100static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
101 const char *name, int len, u64 ino,
102 unsigned int d_type)
103{
Al Viro49be4fb2014-10-23 23:00:53 -0400104 struct rb_node **newp = &rdd->root.rb_node;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200105 struct rb_node *parent = NULL;
106 struct ovl_cache_entry *p;
107
108 while (*newp) {
109 int cmp;
110 struct ovl_cache_entry *tmp;
111
112 parent = *newp;
113 tmp = ovl_cache_entry_from_node(*newp);
114 cmp = strncmp(name, tmp->name, len);
115 if (cmp > 0)
116 newp = &tmp->node.rb_right;
117 else if (cmp < 0 || len < tmp->len)
118 newp = &tmp->node.rb_left;
119 else
120 return 0;
121 }
122
123 p = ovl_cache_entry_new(name, len, ino, d_type);
124 if (p == NULL)
125 return -ENOMEM;
126
127 list_add_tail(&p->l_node, rdd->list);
128 rb_link_node(&p->node, parent, newp);
Al Viro49be4fb2014-10-23 23:00:53 -0400129 rb_insert_color(&p->node, &rdd->root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200130
131 return 0;
132}
133
134static int ovl_fill_lower(struct ovl_readdir_data *rdd,
135 const char *name, int namelen,
136 loff_t offset, u64 ino, unsigned int d_type)
137{
138 struct ovl_cache_entry *p;
139
Al Viro49be4fb2014-10-23 23:00:53 -0400140 p = ovl_cache_entry_find(&rdd->root, name, namelen);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200141 if (p) {
Al Virodb6ec212014-10-23 23:03:03 -0400142 list_move_tail(&p->l_node, &rdd->middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200143 } else {
144 p = ovl_cache_entry_new(name, namelen, ino, d_type);
145 if (p == NULL)
146 rdd->err = -ENOMEM;
147 else
Al Virodb6ec212014-10-23 23:03:03 -0400148 list_add_tail(&p->l_node, &rdd->middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149 }
150
151 return rdd->err;
152}
153
154void ovl_cache_free(struct list_head *list)
155{
156 struct ovl_cache_entry *p;
157 struct ovl_cache_entry *n;
158
159 list_for_each_entry_safe(p, n, list, l_node)
160 kfree(p);
161
162 INIT_LIST_HEAD(list);
163}
164
165static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
166{
167 struct ovl_dir_cache *cache = od->cache;
168
169 list_del(&od->cursor.l_node);
170 WARN_ON(cache->refcount <= 0);
171 cache->refcount--;
172 if (!cache->refcount) {
173 if (ovl_dir_cache(dentry) == cache)
174 ovl_set_dir_cache(dentry, NULL);
175
176 ovl_cache_free(&cache->entries);
177 kfree(cache);
178 }
179}
180
181static int ovl_fill_merge(void *buf, const char *name, int namelen,
182 loff_t offset, u64 ino, unsigned int d_type)
183{
184 struct ovl_readdir_data *rdd = buf;
185
186 rdd->count++;
187 if (!rdd->is_merge)
188 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
189 else
190 return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
191}
192
193static inline int ovl_dir_read(struct path *realpath,
194 struct ovl_readdir_data *rdd)
195{
196 struct file *realfile;
197 int err;
198
199 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
200 if (IS_ERR(realfile))
201 return PTR_ERR(realfile);
202
203 rdd->ctx.pos = 0;
204 do {
205 rdd->count = 0;
206 rdd->err = 0;
207 err = iterate_dir(realfile, &rdd->ctx);
208 if (err >= 0)
209 err = rdd->err;
210 } while (!err && rdd->count);
211 fput(realfile);
212
213 return err;
214}
215
216static void ovl_dir_reset(struct file *file)
217{
218 struct ovl_dir_file *od = file->private_data;
219 struct ovl_dir_cache *cache = od->cache;
220 struct dentry *dentry = file->f_path.dentry;
221 enum ovl_path_type type = ovl_path_type(dentry);
222
223 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
224 ovl_cache_put(od, dentry);
225 od->cache = NULL;
226 }
227 WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
228 if (od->is_real && type == OVL_PATH_MERGE)
229 od->is_real = false;
230}
231
232static int ovl_dir_mark_whiteouts(struct dentry *dir,
233 struct ovl_readdir_data *rdd)
234{
235 struct ovl_cache_entry *p;
236 struct dentry *dentry;
237 const struct cred *old_cred;
238 struct cred *override_cred;
239
240 override_cred = prepare_creds();
241 if (!override_cred) {
242 ovl_cache_free(rdd->list);
243 return -ENOMEM;
244 }
245
246 /*
247 * CAP_DAC_OVERRIDE for lookup
248 */
249 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
250 old_cred = override_creds(override_cred);
251
252 mutex_lock(&dir->d_inode->i_mutex);
253 list_for_each_entry(p, rdd->list, l_node) {
254 if (!p->name)
255 continue;
256
257 if (p->type != DT_CHR)
258 continue;
259
260 dentry = lookup_one_len(p->name, dir, p->len);
261 if (IS_ERR(dentry))
262 continue;
263
264 p->is_whiteout = ovl_is_whiteout(dentry);
265 dput(dentry);
266 }
267 mutex_unlock(&dir->d_inode->i_mutex);
268
269 revert_creds(old_cred);
270 put_cred(override_cred);
271
272 return 0;
273}
274
275static inline int ovl_dir_read_merged(struct path *upperpath,
276 struct path *lowerpath,
277 struct list_head *list)
278{
279 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200280 struct ovl_readdir_data rdd = {
281 .ctx.actor = ovl_fill_merge,
282 .list = list,
Al Viro49be4fb2014-10-23 23:00:53 -0400283 .root = RB_ROOT,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200284 .is_merge = false,
285 };
286
287 if (upperpath->dentry) {
288 err = ovl_dir_read(upperpath, &rdd);
289 if (err)
290 goto out;
291
292 if (lowerpath->dentry) {
293 err = ovl_dir_mark_whiteouts(upperpath->dentry, &rdd);
294 if (err)
295 goto out;
296 }
297 }
298 if (lowerpath->dentry) {
299 /*
300 * Insert lowerpath entries before upperpath ones, this allows
301 * offsets to be reasonably constant
302 */
Al Virodb6ec212014-10-23 23:03:03 -0400303 list_add(&rdd.middle, rdd.list);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200304 rdd.is_merge = true;
305 err = ovl_dir_read(lowerpath, &rdd);
Al Virodb6ec212014-10-23 23:03:03 -0400306 list_del(&rdd.middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200307 }
308out:
309 return err;
310
311}
312
313static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
314{
315 struct ovl_cache_entry *p;
316 loff_t off = 0;
317
318 list_for_each_entry(p, &od->cache->entries, l_node) {
319 if (!p->name)
320 continue;
321 if (off >= pos)
322 break;
323 off++;
324 }
325 list_move_tail(&od->cursor.l_node, &p->l_node);
326}
327
328static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
329{
330 int res;
331 struct path lowerpath;
332 struct path upperpath;
333 struct ovl_dir_cache *cache;
334
335 cache = ovl_dir_cache(dentry);
336 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
337 cache->refcount++;
338 return cache;
339 }
340 ovl_set_dir_cache(dentry, NULL);
341
342 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
343 if (!cache)
344 return ERR_PTR(-ENOMEM);
345
346 cache->refcount = 1;
347 INIT_LIST_HEAD(&cache->entries);
348
349 ovl_path_lower(dentry, &lowerpath);
350 ovl_path_upper(dentry, &upperpath);
351
352 res = ovl_dir_read_merged(&upperpath, &lowerpath, &cache->entries);
353 if (res) {
354 ovl_cache_free(&cache->entries);
355 kfree(cache);
356 return ERR_PTR(res);
357 }
358
359 cache->version = ovl_dentry_version_get(dentry);
360 ovl_set_dir_cache(dentry, cache);
361
362 return cache;
363}
364
365static int ovl_iterate(struct file *file, struct dir_context *ctx)
366{
367 struct ovl_dir_file *od = file->private_data;
368 struct dentry *dentry = file->f_path.dentry;
369
370 if (!ctx->pos)
371 ovl_dir_reset(file);
372
373 if (od->is_real)
374 return iterate_dir(od->realfile, ctx);
375
376 if (!od->cache) {
377 struct ovl_dir_cache *cache;
378
379 cache = ovl_cache_get(dentry);
380 if (IS_ERR(cache))
381 return PTR_ERR(cache);
382
383 od->cache = cache;
384 ovl_seek_cursor(od, ctx->pos);
385 }
386
387 while (od->cursor.l_node.next != &od->cache->entries) {
388 struct ovl_cache_entry *p;
389
390 p = list_entry(od->cursor.l_node.next, struct ovl_cache_entry, l_node);
391 /* Skip cursors */
392 if (p->name) {
393 if (!p->is_whiteout) {
394 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
395 break;
396 }
397 ctx->pos++;
398 }
399 list_move(&od->cursor.l_node, &p->l_node);
400 }
401 return 0;
402}
403
404static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
405{
406 loff_t res;
407 struct ovl_dir_file *od = file->private_data;
408
409 mutex_lock(&file_inode(file)->i_mutex);
410 if (!file->f_pos)
411 ovl_dir_reset(file);
412
413 if (od->is_real) {
414 res = vfs_llseek(od->realfile, offset, origin);
415 file->f_pos = od->realfile->f_pos;
416 } else {
417 res = -EINVAL;
418
419 switch (origin) {
420 case SEEK_CUR:
421 offset += file->f_pos;
422 break;
423 case SEEK_SET:
424 break;
425 default:
426 goto out_unlock;
427 }
428 if (offset < 0)
429 goto out_unlock;
430
431 if (offset != file->f_pos) {
432 file->f_pos = offset;
433 if (od->cache)
434 ovl_seek_cursor(od, offset);
435 }
436 res = offset;
437 }
438out_unlock:
439 mutex_unlock(&file_inode(file)->i_mutex);
440
441 return res;
442}
443
444static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
445 int datasync)
446{
447 struct ovl_dir_file *od = file->private_data;
448 struct dentry *dentry = file->f_path.dentry;
449 struct file *realfile = od->realfile;
450
451 /*
452 * Need to check if we started out being a lower dir, but got copied up
453 */
454 if (!od->is_upper && ovl_path_type(dentry) == OVL_PATH_MERGE) {
455 struct inode *inode = file_inode(file);
456
Al Virod45f00a2014-10-28 18:27:28 -0400457 realfile =lockless_dereference(od->upperfile);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200458 if (!realfile) {
459 struct path upperpath;
460
461 ovl_path_upper(dentry, &upperpath);
462 realfile = ovl_path_open(&upperpath, O_RDONLY);
Al Virod45f00a2014-10-28 18:27:28 -0400463 smp_mb__before_spinlock();
Al Viro3d268c92014-10-23 22:56:05 -0400464 mutex_lock(&inode->i_mutex);
465 if (!od->upperfile) {
466 if (IS_ERR(realfile)) {
467 mutex_unlock(&inode->i_mutex);
468 return PTR_ERR(realfile);
469 }
470 od->upperfile = realfile;
471 } else {
472 /* somebody has beaten us to it */
473 if (!IS_ERR(realfile))
474 fput(realfile);
475 realfile = od->upperfile;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476 }
Al Viro3d268c92014-10-23 22:56:05 -0400477 mutex_unlock(&inode->i_mutex);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200478 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200479 }
480
481 return vfs_fsync_range(realfile, start, end, datasync);
482}
483
484static int ovl_dir_release(struct inode *inode, struct file *file)
485{
486 struct ovl_dir_file *od = file->private_data;
487
488 if (od->cache) {
489 mutex_lock(&inode->i_mutex);
490 ovl_cache_put(od, file->f_path.dentry);
491 mutex_unlock(&inode->i_mutex);
492 }
493 fput(od->realfile);
494 if (od->upperfile)
495 fput(od->upperfile);
496 kfree(od);
497
498 return 0;
499}
500
501static int ovl_dir_open(struct inode *inode, struct file *file)
502{
503 struct path realpath;
504 struct file *realfile;
505 struct ovl_dir_file *od;
506 enum ovl_path_type type;
507
508 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
509 if (!od)
510 return -ENOMEM;
511
512 type = ovl_path_real(file->f_path.dentry, &realpath);
513 realfile = ovl_path_open(&realpath, file->f_flags);
514 if (IS_ERR(realfile)) {
515 kfree(od);
516 return PTR_ERR(realfile);
517 }
518 INIT_LIST_HEAD(&od->cursor.l_node);
519 od->realfile = realfile;
520 od->is_real = (type != OVL_PATH_MERGE);
521 od->is_upper = (type != OVL_PATH_LOWER);
522 file->private_data = od;
523
524 return 0;
525}
526
527const struct file_operations ovl_dir_operations = {
528 .read = generic_read_dir,
529 .open = ovl_dir_open,
530 .iterate = ovl_iterate,
531 .llseek = ovl_dir_llseek,
532 .fsync = ovl_dir_fsync,
533 .release = ovl_dir_release,
534};
535
536int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
537{
538 int err;
539 struct path lowerpath;
540 struct path upperpath;
541 struct ovl_cache_entry *p;
542
543 ovl_path_upper(dentry, &upperpath);
544 ovl_path_lower(dentry, &lowerpath);
545
546 err = ovl_dir_read_merged(&upperpath, &lowerpath, list);
547 if (err)
548 return err;
549
550 err = 0;
551
552 list_for_each_entry(p, list, l_node) {
553 if (p->is_whiteout)
554 continue;
555
556 if (p->name[0] == '.') {
557 if (p->len == 1)
558 continue;
559 if (p->len == 2 && p->name[1] == '.')
560 continue;
561 }
562 err = -ENOTEMPTY;
563 break;
564 }
565
566 return err;
567}
568
569void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
570{
571 struct ovl_cache_entry *p;
572
573 mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_PARENT);
574 list_for_each_entry(p, list, l_node) {
575 struct dentry *dentry;
576
577 if (!p->is_whiteout)
578 continue;
579
580 dentry = lookup_one_len(p->name, upper, p->len);
581 if (IS_ERR(dentry)) {
582 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
583 upper->d_name.name, p->len, p->name,
584 (int) PTR_ERR(dentry));
585 continue;
586 }
587 ovl_cleanup(upper->d_inode, dentry);
588 dput(dentry);
589 }
590 mutex_unlock(&upper->d_inode->i_mutex);
591}