Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszer...
[deliverable/linux.git] / fs / overlayfs / readdir.c
1 /*
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
20 struct ovl_cache_entry {
21 unsigned int len;
22 unsigned int type;
23 u64 ino;
24 struct list_head l_node;
25 struct rb_node node;
26 struct ovl_cache_entry *next_maybe_whiteout;
27 bool is_whiteout;
28 char name[];
29 };
30
31 struct ovl_dir_cache {
32 long refcount;
33 u64 version;
34 struct list_head entries;
35 };
36
37 struct ovl_readdir_data {
38 struct dir_context ctx;
39 struct dentry *dentry;
40 bool is_lowest;
41 struct rb_root root;
42 struct list_head *list;
43 struct list_head middle;
44 struct ovl_cache_entry *first_maybe_whiteout;
45 int count;
46 int err;
47 bool d_type_supported;
48 };
49
50 struct ovl_dir_file {
51 bool is_real;
52 bool is_upper;
53 struct ovl_dir_cache *cache;
54 struct list_head *cursor;
55 struct file *realfile;
56 struct file *upperfile;
57 };
58
59 static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
60 {
61 return container_of(n, struct ovl_cache_entry, node);
62 }
63
64 static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
65 const char *name, int len)
66 {
67 struct rb_node *node = root->rb_node;
68 int cmp;
69
70 while (node) {
71 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
72
73 cmp = strncmp(name, p->name, len);
74 if (cmp > 0)
75 node = p->node.rb_right;
76 else if (cmp < 0 || len < p->len)
77 node = p->node.rb_left;
78 else
79 return p;
80 }
81
82 return NULL;
83 }
84
85 static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
86 const char *name, int len,
87 u64 ino, unsigned int d_type)
88 {
89 struct ovl_cache_entry *p;
90 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
91
92 p = kmalloc(size, GFP_KERNEL);
93 if (!p)
94 return NULL;
95
96 memcpy(p->name, name, len);
97 p->name[len] = '\0';
98 p->len = len;
99 p->type = d_type;
100 p->ino = ino;
101 p->is_whiteout = false;
102
103 if (d_type == DT_CHR) {
104 p->next_maybe_whiteout = rdd->first_maybe_whiteout;
105 rdd->first_maybe_whiteout = p;
106 }
107 return p;
108 }
109
110 static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
111 const char *name, int len, u64 ino,
112 unsigned int d_type)
113 {
114 struct rb_node **newp = &rdd->root.rb_node;
115 struct rb_node *parent = NULL;
116 struct ovl_cache_entry *p;
117
118 while (*newp) {
119 int cmp;
120 struct ovl_cache_entry *tmp;
121
122 parent = *newp;
123 tmp = ovl_cache_entry_from_node(*newp);
124 cmp = strncmp(name, tmp->name, len);
125 if (cmp > 0)
126 newp = &tmp->node.rb_right;
127 else if (cmp < 0 || len < tmp->len)
128 newp = &tmp->node.rb_left;
129 else
130 return 0;
131 }
132
133 p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
134 if (p == NULL)
135 return -ENOMEM;
136
137 list_add_tail(&p->l_node, rdd->list);
138 rb_link_node(&p->node, parent, newp);
139 rb_insert_color(&p->node, &rdd->root);
140
141 return 0;
142 }
143
144 static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
145 const char *name, int namelen,
146 loff_t offset, u64 ino, unsigned int d_type)
147 {
148 struct ovl_cache_entry *p;
149
150 p = ovl_cache_entry_find(&rdd->root, name, namelen);
151 if (p) {
152 list_move_tail(&p->l_node, &rdd->middle);
153 } else {
154 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
155 if (p == NULL)
156 rdd->err = -ENOMEM;
157 else
158 list_add_tail(&p->l_node, &rdd->middle);
159 }
160
161 return rdd->err;
162 }
163
164 void ovl_cache_free(struct list_head *list)
165 {
166 struct ovl_cache_entry *p;
167 struct ovl_cache_entry *n;
168
169 list_for_each_entry_safe(p, n, list, l_node)
170 kfree(p);
171
172 INIT_LIST_HEAD(list);
173 }
174
175 static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
176 {
177 struct ovl_dir_cache *cache = od->cache;
178
179 WARN_ON(cache->refcount <= 0);
180 cache->refcount--;
181 if (!cache->refcount) {
182 if (ovl_dir_cache(dentry) == cache)
183 ovl_set_dir_cache(dentry, NULL);
184
185 ovl_cache_free(&cache->entries);
186 kfree(cache);
187 }
188 }
189
190 static int ovl_fill_merge(struct dir_context *ctx, const char *name,
191 int namelen, loff_t offset, u64 ino,
192 unsigned int d_type)
193 {
194 struct ovl_readdir_data *rdd =
195 container_of(ctx, struct ovl_readdir_data, ctx);
196
197 rdd->count++;
198 if (!rdd->is_lowest)
199 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
200 else
201 return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
202 }
203
204 static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
205 {
206 int err;
207 struct ovl_cache_entry *p;
208 struct dentry *dentry;
209 const struct cred *old_cred;
210
211 old_cred = ovl_override_creds(rdd->dentry->d_sb);
212
213 inode_lock(dir->d_inode);
214 err = 0;
215 // XXX: err = mutex_lock_killable(&dir->d_inode->i_mutex);
216 if (!err) {
217 while (rdd->first_maybe_whiteout) {
218 p = rdd->first_maybe_whiteout;
219 rdd->first_maybe_whiteout = p->next_maybe_whiteout;
220 dentry = lookup_one_len(p->name, dir, p->len);
221 if (!IS_ERR(dentry)) {
222 p->is_whiteout = ovl_is_whiteout(dentry);
223 dput(dentry);
224 }
225 }
226 inode_unlock(dir->d_inode);
227 }
228 revert_creds(old_cred);
229
230 return err;
231 }
232
233 static inline int ovl_dir_read(struct path *realpath,
234 struct ovl_readdir_data *rdd)
235 {
236 struct file *realfile;
237 int err;
238
239 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
240 if (IS_ERR(realfile))
241 return PTR_ERR(realfile);
242
243 rdd->first_maybe_whiteout = NULL;
244 rdd->ctx.pos = 0;
245 do {
246 rdd->count = 0;
247 rdd->err = 0;
248 err = iterate_dir(realfile, &rdd->ctx);
249 if (err >= 0)
250 err = rdd->err;
251 } while (!err && rdd->count);
252
253 if (!err && rdd->first_maybe_whiteout)
254 err = ovl_check_whiteouts(realpath->dentry, rdd);
255
256 fput(realfile);
257
258 return err;
259 }
260
261 static void ovl_dir_reset(struct file *file)
262 {
263 struct ovl_dir_file *od = file->private_data;
264 struct ovl_dir_cache *cache = od->cache;
265 struct dentry *dentry = file->f_path.dentry;
266 enum ovl_path_type type = ovl_path_type(dentry);
267
268 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
269 ovl_cache_put(od, dentry);
270 od->cache = NULL;
271 od->cursor = NULL;
272 }
273 WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type));
274 if (od->is_real && OVL_TYPE_MERGE(type))
275 od->is_real = false;
276 }
277
278 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
279 {
280 int err;
281 struct path realpath;
282 struct ovl_readdir_data rdd = {
283 .ctx.actor = ovl_fill_merge,
284 .dentry = dentry,
285 .list = list,
286 .root = RB_ROOT,
287 .is_lowest = false,
288 };
289 int idx, next;
290
291 for (idx = 0; idx != -1; idx = next) {
292 next = ovl_path_next(idx, dentry, &realpath);
293
294 if (next != -1) {
295 err = ovl_dir_read(&realpath, &rdd);
296 if (err)
297 break;
298 } else {
299 /*
300 * Insert lowest layer entries before upper ones, this
301 * allows offsets to be reasonably constant
302 */
303 list_add(&rdd.middle, rdd.list);
304 rdd.is_lowest = true;
305 err = ovl_dir_read(&realpath, &rdd);
306 list_del(&rdd.middle);
307 }
308 }
309 return err;
310 }
311
312 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
313 {
314 struct list_head *p;
315 loff_t off = 0;
316
317 list_for_each(p, &od->cache->entries) {
318 if (off >= pos)
319 break;
320 off++;
321 }
322 /* Cursor is safe since the cache is stable */
323 od->cursor = p;
324 }
325
326 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
327 {
328 int res;
329 struct ovl_dir_cache *cache;
330
331 cache = ovl_dir_cache(dentry);
332 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
333 cache->refcount++;
334 return cache;
335 }
336 ovl_set_dir_cache(dentry, NULL);
337
338 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
339 if (!cache)
340 return ERR_PTR(-ENOMEM);
341
342 cache->refcount = 1;
343 INIT_LIST_HEAD(&cache->entries);
344
345 res = ovl_dir_read_merged(dentry, &cache->entries);
346 if (res) {
347 ovl_cache_free(&cache->entries);
348 kfree(cache);
349 return ERR_PTR(res);
350 }
351
352 cache->version = ovl_dentry_version_get(dentry);
353 ovl_set_dir_cache(dentry, cache);
354
355 return cache;
356 }
357
358 static int ovl_iterate(struct file *file, struct dir_context *ctx)
359 {
360 struct ovl_dir_file *od = file->private_data;
361 struct dentry *dentry = file->f_path.dentry;
362 struct ovl_cache_entry *p;
363
364 if (!ctx->pos)
365 ovl_dir_reset(file);
366
367 if (od->is_real)
368 return iterate_dir(od->realfile, ctx);
369
370 if (!od->cache) {
371 struct ovl_dir_cache *cache;
372
373 cache = ovl_cache_get(dentry);
374 if (IS_ERR(cache))
375 return PTR_ERR(cache);
376
377 od->cache = cache;
378 ovl_seek_cursor(od, ctx->pos);
379 }
380
381 while (od->cursor != &od->cache->entries) {
382 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
383 if (!p->is_whiteout)
384 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
385 break;
386 od->cursor = p->l_node.next;
387 ctx->pos++;
388 }
389 return 0;
390 }
391
392 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
393 {
394 loff_t res;
395 struct ovl_dir_file *od = file->private_data;
396
397 inode_lock(file_inode(file));
398 if (!file->f_pos)
399 ovl_dir_reset(file);
400
401 if (od->is_real) {
402 res = vfs_llseek(od->realfile, offset, origin);
403 file->f_pos = od->realfile->f_pos;
404 } else {
405 res = -EINVAL;
406
407 switch (origin) {
408 case SEEK_CUR:
409 offset += file->f_pos;
410 break;
411 case SEEK_SET:
412 break;
413 default:
414 goto out_unlock;
415 }
416 if (offset < 0)
417 goto out_unlock;
418
419 if (offset != file->f_pos) {
420 file->f_pos = offset;
421 if (od->cache)
422 ovl_seek_cursor(od, offset);
423 }
424 res = offset;
425 }
426 out_unlock:
427 inode_unlock(file_inode(file));
428
429 return res;
430 }
431
432 static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
433 int datasync)
434 {
435 struct ovl_dir_file *od = file->private_data;
436 struct dentry *dentry = file->f_path.dentry;
437 struct file *realfile = od->realfile;
438
439 /*
440 * Need to check if we started out being a lower dir, but got copied up
441 */
442 if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) {
443 struct inode *inode = file_inode(file);
444
445 realfile = lockless_dereference(od->upperfile);
446 if (!realfile) {
447 struct path upperpath;
448
449 ovl_path_upper(dentry, &upperpath);
450 realfile = ovl_path_open(&upperpath, O_RDONLY);
451 smp_mb__before_spinlock();
452 inode_lock(inode);
453 if (!od->upperfile) {
454 if (IS_ERR(realfile)) {
455 inode_unlock(inode);
456 return PTR_ERR(realfile);
457 }
458 od->upperfile = realfile;
459 } else {
460 /* somebody has beaten us to it */
461 if (!IS_ERR(realfile))
462 fput(realfile);
463 realfile = od->upperfile;
464 }
465 inode_unlock(inode);
466 }
467 }
468
469 return vfs_fsync_range(realfile, start, end, datasync);
470 }
471
472 static int ovl_dir_release(struct inode *inode, struct file *file)
473 {
474 struct ovl_dir_file *od = file->private_data;
475
476 if (od->cache) {
477 inode_lock(inode);
478 ovl_cache_put(od, file->f_path.dentry);
479 inode_unlock(inode);
480 }
481 fput(od->realfile);
482 if (od->upperfile)
483 fput(od->upperfile);
484 kfree(od);
485
486 return 0;
487 }
488
489 static int ovl_dir_open(struct inode *inode, struct file *file)
490 {
491 struct path realpath;
492 struct file *realfile;
493 struct ovl_dir_file *od;
494 enum ovl_path_type type;
495
496 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
497 if (!od)
498 return -ENOMEM;
499
500 type = ovl_path_real(file->f_path.dentry, &realpath);
501 realfile = ovl_path_open(&realpath, file->f_flags);
502 if (IS_ERR(realfile)) {
503 kfree(od);
504 return PTR_ERR(realfile);
505 }
506 od->realfile = realfile;
507 od->is_real = !OVL_TYPE_MERGE(type);
508 od->is_upper = OVL_TYPE_UPPER(type);
509 file->private_data = od;
510
511 return 0;
512 }
513
514 const struct file_operations ovl_dir_operations = {
515 .read = generic_read_dir,
516 .open = ovl_dir_open,
517 .iterate = ovl_iterate,
518 .llseek = ovl_dir_llseek,
519 .fsync = ovl_dir_fsync,
520 .release = ovl_dir_release,
521 };
522
523 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
524 {
525 int err;
526 struct ovl_cache_entry *p;
527
528 err = ovl_dir_read_merged(dentry, list);
529 if (err)
530 return err;
531
532 err = 0;
533
534 list_for_each_entry(p, list, l_node) {
535 if (p->is_whiteout)
536 continue;
537
538 if (p->name[0] == '.') {
539 if (p->len == 1)
540 continue;
541 if (p->len == 2 && p->name[1] == '.')
542 continue;
543 }
544 err = -ENOTEMPTY;
545 break;
546 }
547
548 return err;
549 }
550
551 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
552 {
553 struct ovl_cache_entry *p;
554
555 inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
556 list_for_each_entry(p, list, l_node) {
557 struct dentry *dentry;
558
559 if (!p->is_whiteout)
560 continue;
561
562 dentry = lookup_one_len(p->name, upper, p->len);
563 if (IS_ERR(dentry)) {
564 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
565 upper->d_name.name, p->len, p->name,
566 (int) PTR_ERR(dentry));
567 continue;
568 }
569 if (dentry->d_inode)
570 ovl_cleanup(upper->d_inode, dentry);
571 dput(dentry);
572 }
573 inode_unlock(upper->d_inode);
574 }
575
576 static int ovl_check_d_type(struct dir_context *ctx, const char *name,
577 int namelen, loff_t offset, u64 ino,
578 unsigned int d_type)
579 {
580 struct ovl_readdir_data *rdd =
581 container_of(ctx, struct ovl_readdir_data, ctx);
582
583 /* Even if d_type is not supported, DT_DIR is returned for . and .. */
584 if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
585 return 0;
586
587 if (d_type != DT_UNKNOWN)
588 rdd->d_type_supported = true;
589
590 return 0;
591 }
592
593 /*
594 * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
595 * if error is encountered.
596 */
597 int ovl_check_d_type_supported(struct path *realpath)
598 {
599 int err;
600 struct ovl_readdir_data rdd = {
601 .ctx.actor = ovl_check_d_type,
602 .d_type_supported = false,
603 };
604
605 err = ovl_dir_read(realpath, &rdd);
606 if (err)
607 return err;
608
609 return rdd.d_type_supported;
610 }
This page took 0.060676 seconds and 5 git commands to generate.