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