]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/overlayfs/readdir.c
overlayfs: make ovl_cache_entry->name an array instead of pointer
[karo-tx-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         bool is_whiteout;
25         struct list_head l_node;
26         struct rb_node node;
27         char name[];
28 };
29
30 struct ovl_dir_cache {
31         long refcount;
32         u64 version;
33         struct list_head entries;
34 };
35
36 struct ovl_readdir_data {
37         struct dir_context ctx;
38         bool is_merge;
39         struct rb_root *root;
40         struct list_head *list;
41         struct list_head *middle;
42         int count;
43         int err;
44 };
45
46 struct 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
55 static 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
60 static 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
81 static 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;
85         size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
86
87         p = kmalloc(size, GFP_KERNEL);
88         if (p) {
89                 memcpy(p->name, name, len);
90                 p->name[len] = '\0';
91                 p->len = len;
92                 p->type = d_type;
93                 p->ino = ino;
94                 p->is_whiteout = false;
95         }
96
97         return p;
98 }
99
100 static 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 {
104         struct rb_node **newp = &rdd->root->rb_node;
105         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);
129         rb_insert_color(&p->node, rdd->root);
130
131         return 0;
132 }
133
134 static 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
140         p = ovl_cache_entry_find(rdd->root, name, namelen);
141         if (p) {
142                 list_move_tail(&p->l_node, rdd->middle);
143         } else {
144                 p = ovl_cache_entry_new(name, namelen, ino, d_type);
145                 if (p == NULL)
146                         rdd->err = -ENOMEM;
147                 else
148                         list_add_tail(&p->l_node, rdd->middle);
149         }
150
151         return rdd->err;
152 }
153
154 void 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
165 static 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
181 static 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
193 static 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
216 static 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
232 static 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
275 static inline int ovl_dir_read_merged(struct path *upperpath,
276                                       struct path *lowerpath,
277                                       struct list_head *list)
278 {
279         int err;
280         struct rb_root root = RB_ROOT;
281         struct list_head middle;
282         struct ovl_readdir_data rdd = {
283                 .ctx.actor = ovl_fill_merge,
284                 .list = list,
285                 .root = &root,
286                 .is_merge = false,
287         };
288
289         if (upperpath->dentry) {
290                 err = ovl_dir_read(upperpath, &rdd);
291                 if (err)
292                         goto out;
293
294                 if (lowerpath->dentry) {
295                         err = ovl_dir_mark_whiteouts(upperpath->dentry, &rdd);
296                         if (err)
297                                 goto out;
298                 }
299         }
300         if (lowerpath->dentry) {
301                 /*
302                  * Insert lowerpath entries before upperpath ones, this allows
303                  * offsets to be reasonably constant
304                  */
305                 list_add(&middle, rdd.list);
306                 rdd.middle = &middle;
307                 rdd.is_merge = true;
308                 err = ovl_dir_read(lowerpath, &rdd);
309                 list_del(&middle);
310         }
311 out:
312         return err;
313
314 }
315
316 static 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) {
322                 if (!p->name)
323                         continue;
324                 if (off >= pos)
325                         break;
326                 off++;
327         }
328         list_move_tail(&od->cursor.l_node, &p->l_node);
329 }
330
331 static 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
368 static 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 */
395                 if (p->name) {
396                         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
407 static 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         }
441 out_unlock:
442         mutex_unlock(&file_inode(file)->i_mutex);
443
444         return res;
445 }
446
447 static 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
460                 realfile = od->upperfile;
461                 if (!realfile) {
462                         struct path upperpath;
463
464                         ovl_path_upper(dentry, &upperpath);
465                         realfile = ovl_path_open(&upperpath, O_RDONLY);
466                         mutex_lock(&inode->i_mutex);
467                         if (!od->upperfile) {
468                                 if (IS_ERR(realfile)) {
469                                         mutex_unlock(&inode->i_mutex);
470                                         return PTR_ERR(realfile);
471                                 }
472                                 od->upperfile = realfile;
473                         } else {
474                                 /* somebody has beaten us to it */
475                                 if (!IS_ERR(realfile))
476                                         fput(realfile);
477                                 realfile = od->upperfile;
478                         }
479                         mutex_unlock(&inode->i_mutex);
480                 }
481         }
482
483         return vfs_fsync_range(realfile, start, end, datasync);
484 }
485
486 static int ovl_dir_release(struct inode *inode, struct file *file)
487 {
488         struct ovl_dir_file *od = file->private_data;
489
490         if (od->cache) {
491                 mutex_lock(&inode->i_mutex);
492                 ovl_cache_put(od, file->f_path.dentry);
493                 mutex_unlock(&inode->i_mutex);
494         }
495         fput(od->realfile);
496         if (od->upperfile)
497                 fput(od->upperfile);
498         kfree(od);
499
500         return 0;
501 }
502
503 static int ovl_dir_open(struct inode *inode, struct file *file)
504 {
505         struct path realpath;
506         struct file *realfile;
507         struct ovl_dir_file *od;
508         enum ovl_path_type type;
509
510         od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
511         if (!od)
512                 return -ENOMEM;
513
514         type = ovl_path_real(file->f_path.dentry, &realpath);
515         realfile = ovl_path_open(&realpath, file->f_flags);
516         if (IS_ERR(realfile)) {
517                 kfree(od);
518                 return PTR_ERR(realfile);
519         }
520         INIT_LIST_HEAD(&od->cursor.l_node);
521         od->realfile = realfile;
522         od->is_real = (type != OVL_PATH_MERGE);
523         od->is_upper = (type != OVL_PATH_LOWER);
524         file->private_data = od;
525
526         return 0;
527 }
528
529 const struct file_operations ovl_dir_operations = {
530         .read           = generic_read_dir,
531         .open           = ovl_dir_open,
532         .iterate        = ovl_iterate,
533         .llseek         = ovl_dir_llseek,
534         .fsync          = ovl_dir_fsync,
535         .release        = ovl_dir_release,
536 };
537
538 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
539 {
540         int err;
541         struct path lowerpath;
542         struct path upperpath;
543         struct ovl_cache_entry *p;
544
545         ovl_path_upper(dentry, &upperpath);
546         ovl_path_lower(dentry, &lowerpath);
547
548         err = ovl_dir_read_merged(&upperpath, &lowerpath, list);
549         if (err)
550                 return err;
551
552         err = 0;
553
554         list_for_each_entry(p, list, l_node) {
555                 if (p->is_whiteout)
556                         continue;
557
558                 if (p->name[0] == '.') {
559                         if (p->len == 1)
560                                 continue;
561                         if (p->len == 2 && p->name[1] == '.')
562                                 continue;
563                 }
564                 err = -ENOTEMPTY;
565                 break;
566         }
567
568         return err;
569 }
570
571 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
572 {
573         struct ovl_cache_entry *p;
574
575         mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_PARENT);
576         list_for_each_entry(p, list, l_node) {
577                 struct dentry *dentry;
578
579                 if (!p->is_whiteout)
580                         continue;
581
582                 dentry = lookup_one_len(p->name, upper, p->len);
583                 if (IS_ERR(dentry)) {
584                         pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
585                                upper->d_name.name, p->len, p->name,
586                                (int) PTR_ERR(dentry));
587                         continue;
588                 }
589                 ovl_cleanup(upper->d_inode, dentry);
590                 dput(dentry);
591         }
592         mutex_unlock(&upper->d_inode->i_mutex);
593 }