Commit | Line | Data |
---|---|---|
e9be9d5e MS |
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 { | |
e9be9d5e MS |
21 | unsigned int len; |
22 | unsigned int type; | |
23 | u64 ino; | |
e9be9d5e MS |
24 | struct list_head l_node; |
25 | struct rb_node node; | |
c2096537 MS |
26 | bool is_whiteout; |
27 | bool is_cursor; | |
68bf8611 | 28 | char name[]; |
e9be9d5e MS |
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 | bool is_merge; | |
49be4fb9 | 40 | struct rb_root root; |
e9be9d5e | 41 | struct list_head *list; |
db6ec212 | 42 | struct list_head middle; |
49c21e1c | 43 | struct dentry *dir; |
e9be9d5e MS |
44 | int count; |
45 | int err; | |
46 | }; | |
47 | ||
48 | struct ovl_dir_file { | |
49 | bool is_real; | |
50 | bool is_upper; | |
51 | struct ovl_dir_cache *cache; | |
52 | struct ovl_cache_entry cursor; | |
53 | struct file *realfile; | |
54 | struct file *upperfile; | |
55 | }; | |
56 | ||
57 | static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n) | |
58 | { | |
59 | return container_of(n, struct ovl_cache_entry, node); | |
60 | } | |
61 | ||
62 | static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root, | |
63 | const char *name, int len) | |
64 | { | |
65 | struct rb_node *node = root->rb_node; | |
66 | int cmp; | |
67 | ||
68 | while (node) { | |
69 | struct ovl_cache_entry *p = ovl_cache_entry_from_node(node); | |
70 | ||
71 | cmp = strncmp(name, p->name, len); | |
72 | if (cmp > 0) | |
73 | node = p->node.rb_right; | |
74 | else if (cmp < 0 || len < p->len) | |
75 | node = p->node.rb_left; | |
76 | else | |
77 | return p; | |
78 | } | |
79 | ||
80 | return NULL; | |
81 | } | |
82 | ||
83 | static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len, | |
84 | u64 ino, unsigned int d_type) | |
85 | { | |
86 | struct ovl_cache_entry *p; | |
68bf8611 | 87 | size_t size = offsetof(struct ovl_cache_entry, name[len + 1]); |
e9be9d5e | 88 | |
68bf8611 | 89 | p = kmalloc(size, GFP_KERNEL); |
e9be9d5e | 90 | if (p) { |
68bf8611 AV |
91 | memcpy(p->name, name, len); |
92 | p->name[len] = '\0'; | |
e9be9d5e MS |
93 | p->len = len; |
94 | p->type = d_type; | |
95 | p->ino = ino; | |
96 | p->is_whiteout = false; | |
9f2f7d4c | 97 | p->is_cursor = false; |
e9be9d5e MS |
98 | } |
99 | ||
100 | return p; | |
101 | } | |
102 | ||
103 | static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd, | |
104 | const char *name, int len, u64 ino, | |
105 | unsigned int d_type) | |
106 | { | |
49be4fb9 | 107 | struct rb_node **newp = &rdd->root.rb_node; |
e9be9d5e MS |
108 | struct rb_node *parent = NULL; |
109 | struct ovl_cache_entry *p; | |
110 | ||
111 | while (*newp) { | |
112 | int cmp; | |
113 | struct ovl_cache_entry *tmp; | |
114 | ||
115 | parent = *newp; | |
116 | tmp = ovl_cache_entry_from_node(*newp); | |
117 | cmp = strncmp(name, tmp->name, len); | |
118 | if (cmp > 0) | |
119 | newp = &tmp->node.rb_right; | |
120 | else if (cmp < 0 || len < tmp->len) | |
121 | newp = &tmp->node.rb_left; | |
122 | else | |
123 | return 0; | |
124 | } | |
125 | ||
126 | p = ovl_cache_entry_new(name, len, ino, d_type); | |
127 | if (p == NULL) | |
128 | return -ENOMEM; | |
129 | ||
49c21e1c MS |
130 | if (d_type == DT_CHR) { |
131 | struct dentry *dentry; | |
132 | const struct cred *old_cred; | |
133 | struct cred *override_cred; | |
134 | ||
135 | override_cred = prepare_creds(); | |
136 | if (!override_cred) { | |
137 | kfree(p); | |
138 | return -ENOMEM; | |
139 | } | |
140 | ||
141 | /* | |
142 | * CAP_DAC_OVERRIDE for lookup | |
143 | */ | |
144 | cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE); | |
145 | old_cred = override_creds(override_cred); | |
146 | ||
147 | dentry = lookup_one_len(name, rdd->dir, len); | |
148 | if (!IS_ERR(dentry)) { | |
149 | p->is_whiteout = ovl_is_whiteout(dentry); | |
150 | dput(dentry); | |
151 | } | |
152 | revert_creds(old_cred); | |
153 | put_cred(override_cred); | |
154 | } | |
155 | ||
e9be9d5e MS |
156 | list_add_tail(&p->l_node, rdd->list); |
157 | rb_link_node(&p->node, parent, newp); | |
49be4fb9 | 158 | rb_insert_color(&p->node, &rdd->root); |
e9be9d5e MS |
159 | |
160 | return 0; | |
161 | } | |
162 | ||
163 | static int ovl_fill_lower(struct ovl_readdir_data *rdd, | |
164 | const char *name, int namelen, | |
165 | loff_t offset, u64 ino, unsigned int d_type) | |
166 | { | |
167 | struct ovl_cache_entry *p; | |
168 | ||
49be4fb9 | 169 | p = ovl_cache_entry_find(&rdd->root, name, namelen); |
e9be9d5e | 170 | if (p) { |
db6ec212 | 171 | list_move_tail(&p->l_node, &rdd->middle); |
e9be9d5e MS |
172 | } else { |
173 | p = ovl_cache_entry_new(name, namelen, ino, d_type); | |
174 | if (p == NULL) | |
175 | rdd->err = -ENOMEM; | |
176 | else | |
db6ec212 | 177 | list_add_tail(&p->l_node, &rdd->middle); |
e9be9d5e MS |
178 | } |
179 | ||
180 | return rdd->err; | |
181 | } | |
182 | ||
183 | void ovl_cache_free(struct list_head *list) | |
184 | { | |
185 | struct ovl_cache_entry *p; | |
186 | struct ovl_cache_entry *n; | |
187 | ||
188 | list_for_each_entry_safe(p, n, list, l_node) | |
189 | kfree(p); | |
190 | ||
191 | INIT_LIST_HEAD(list); | |
192 | } | |
193 | ||
194 | static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry) | |
195 | { | |
196 | struct ovl_dir_cache *cache = od->cache; | |
197 | ||
3f822c62 | 198 | list_del_init(&od->cursor.l_node); |
e9be9d5e MS |
199 | WARN_ON(cache->refcount <= 0); |
200 | cache->refcount--; | |
201 | if (!cache->refcount) { | |
202 | if (ovl_dir_cache(dentry) == cache) | |
203 | ovl_set_dir_cache(dentry, NULL); | |
204 | ||
205 | ovl_cache_free(&cache->entries); | |
206 | kfree(cache); | |
207 | } | |
208 | } | |
209 | ||
210 | static int ovl_fill_merge(void *buf, const char *name, int namelen, | |
211 | loff_t offset, u64 ino, unsigned int d_type) | |
212 | { | |
213 | struct ovl_readdir_data *rdd = buf; | |
214 | ||
215 | rdd->count++; | |
216 | if (!rdd->is_merge) | |
217 | return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type); | |
218 | else | |
219 | return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type); | |
220 | } | |
221 | ||
222 | static inline int ovl_dir_read(struct path *realpath, | |
223 | struct ovl_readdir_data *rdd) | |
224 | { | |
225 | struct file *realfile; | |
226 | int err; | |
227 | ||
228 | realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY); | |
229 | if (IS_ERR(realfile)) | |
230 | return PTR_ERR(realfile); | |
231 | ||
232 | rdd->ctx.pos = 0; | |
233 | do { | |
234 | rdd->count = 0; | |
235 | rdd->err = 0; | |
236 | err = iterate_dir(realfile, &rdd->ctx); | |
237 | if (err >= 0) | |
238 | err = rdd->err; | |
239 | } while (!err && rdd->count); | |
240 | fput(realfile); | |
241 | ||
242 | return err; | |
243 | } | |
244 | ||
245 | static void ovl_dir_reset(struct file *file) | |
246 | { | |
247 | struct ovl_dir_file *od = file->private_data; | |
248 | struct ovl_dir_cache *cache = od->cache; | |
249 | struct dentry *dentry = file->f_path.dentry; | |
250 | enum ovl_path_type type = ovl_path_type(dentry); | |
251 | ||
252 | if (cache && ovl_dentry_version_get(dentry) != cache->version) { | |
253 | ovl_cache_put(od, dentry); | |
254 | od->cache = NULL; | |
255 | } | |
1afaba1e MS |
256 | WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type)); |
257 | if (od->is_real && OVL_TYPE_MERGE(type)) | |
e9be9d5e MS |
258 | od->is_real = false; |
259 | } | |
260 | ||
c9f00fdb | 261 | static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list) |
e9be9d5e MS |
262 | { |
263 | int err; | |
c9f00fdb MS |
264 | struct path lowerpath; |
265 | struct path upperpath; | |
e9be9d5e MS |
266 | struct ovl_readdir_data rdd = { |
267 | .ctx.actor = ovl_fill_merge, | |
268 | .list = list, | |
49be4fb9 | 269 | .root = RB_ROOT, |
e9be9d5e MS |
270 | .is_merge = false, |
271 | }; | |
272 | ||
c9f00fdb MS |
273 | ovl_path_lower(dentry, &lowerpath); |
274 | ovl_path_upper(dentry, &upperpath); | |
275 | ||
276 | if (upperpath.dentry) { | |
49c21e1c | 277 | rdd.dir = upperpath.dentry; |
c9f00fdb | 278 | err = ovl_dir_read(&upperpath, &rdd); |
e9be9d5e MS |
279 | if (err) |
280 | goto out; | |
e9be9d5e | 281 | } |
c9f00fdb | 282 | if (lowerpath.dentry) { |
e9be9d5e MS |
283 | /* |
284 | * Insert lowerpath entries before upperpath ones, this allows | |
285 | * offsets to be reasonably constant | |
286 | */ | |
db6ec212 | 287 | list_add(&rdd.middle, rdd.list); |
e9be9d5e | 288 | rdd.is_merge = true; |
c9f00fdb | 289 | err = ovl_dir_read(&lowerpath, &rdd); |
db6ec212 | 290 | list_del(&rdd.middle); |
e9be9d5e MS |
291 | } |
292 | out: | |
293 | return err; | |
e9be9d5e MS |
294 | } |
295 | ||
296 | static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos) | |
297 | { | |
298 | struct ovl_cache_entry *p; | |
299 | loff_t off = 0; | |
300 | ||
301 | list_for_each_entry(p, &od->cache->entries, l_node) { | |
c2096537 | 302 | if (p->is_cursor) |
e9be9d5e MS |
303 | continue; |
304 | if (off >= pos) | |
305 | break; | |
306 | off++; | |
307 | } | |
308 | list_move_tail(&od->cursor.l_node, &p->l_node); | |
309 | } | |
310 | ||
311 | static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry) | |
312 | { | |
313 | int res; | |
e9be9d5e MS |
314 | struct ovl_dir_cache *cache; |
315 | ||
316 | cache = ovl_dir_cache(dentry); | |
317 | if (cache && ovl_dentry_version_get(dentry) == cache->version) { | |
318 | cache->refcount++; | |
319 | return cache; | |
320 | } | |
321 | ovl_set_dir_cache(dentry, NULL); | |
322 | ||
323 | cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL); | |
324 | if (!cache) | |
325 | return ERR_PTR(-ENOMEM); | |
326 | ||
327 | cache->refcount = 1; | |
328 | INIT_LIST_HEAD(&cache->entries); | |
329 | ||
c9f00fdb | 330 | res = ovl_dir_read_merged(dentry, &cache->entries); |
e9be9d5e MS |
331 | if (res) { |
332 | ovl_cache_free(&cache->entries); | |
333 | kfree(cache); | |
334 | return ERR_PTR(res); | |
335 | } | |
336 | ||
337 | cache->version = ovl_dentry_version_get(dentry); | |
338 | ovl_set_dir_cache(dentry, cache); | |
339 | ||
340 | return cache; | |
341 | } | |
342 | ||
343 | static int ovl_iterate(struct file *file, struct dir_context *ctx) | |
344 | { | |
345 | struct ovl_dir_file *od = file->private_data; | |
346 | struct dentry *dentry = file->f_path.dentry; | |
347 | ||
348 | if (!ctx->pos) | |
349 | ovl_dir_reset(file); | |
350 | ||
351 | if (od->is_real) | |
352 | return iterate_dir(od->realfile, ctx); | |
353 | ||
354 | if (!od->cache) { | |
355 | struct ovl_dir_cache *cache; | |
356 | ||
357 | cache = ovl_cache_get(dentry); | |
358 | if (IS_ERR(cache)) | |
359 | return PTR_ERR(cache); | |
360 | ||
361 | od->cache = cache; | |
362 | ovl_seek_cursor(od, ctx->pos); | |
363 | } | |
364 | ||
365 | while (od->cursor.l_node.next != &od->cache->entries) { | |
366 | struct ovl_cache_entry *p; | |
367 | ||
368 | p = list_entry(od->cursor.l_node.next, struct ovl_cache_entry, l_node); | |
369 | /* Skip cursors */ | |
c2096537 | 370 | if (!p->is_cursor) { |
e9be9d5e MS |
371 | if (!p->is_whiteout) { |
372 | if (!dir_emit(ctx, p->name, p->len, p->ino, p->type)) | |
373 | break; | |
374 | } | |
375 | ctx->pos++; | |
376 | } | |
377 | list_move(&od->cursor.l_node, &p->l_node); | |
378 | } | |
379 | return 0; | |
380 | } | |
381 | ||
382 | static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin) | |
383 | { | |
384 | loff_t res; | |
385 | struct ovl_dir_file *od = file->private_data; | |
386 | ||
387 | mutex_lock(&file_inode(file)->i_mutex); | |
388 | if (!file->f_pos) | |
389 | ovl_dir_reset(file); | |
390 | ||
391 | if (od->is_real) { | |
392 | res = vfs_llseek(od->realfile, offset, origin); | |
393 | file->f_pos = od->realfile->f_pos; | |
394 | } else { | |
395 | res = -EINVAL; | |
396 | ||
397 | switch (origin) { | |
398 | case SEEK_CUR: | |
399 | offset += file->f_pos; | |
400 | break; | |
401 | case SEEK_SET: | |
402 | break; | |
403 | default: | |
404 | goto out_unlock; | |
405 | } | |
406 | if (offset < 0) | |
407 | goto out_unlock; | |
408 | ||
409 | if (offset != file->f_pos) { | |
410 | file->f_pos = offset; | |
411 | if (od->cache) | |
412 | ovl_seek_cursor(od, offset); | |
413 | } | |
414 | res = offset; | |
415 | } | |
416 | out_unlock: | |
417 | mutex_unlock(&file_inode(file)->i_mutex); | |
418 | ||
419 | return res; | |
420 | } | |
421 | ||
422 | static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end, | |
423 | int datasync) | |
424 | { | |
425 | struct ovl_dir_file *od = file->private_data; | |
426 | struct dentry *dentry = file->f_path.dentry; | |
427 | struct file *realfile = od->realfile; | |
428 | ||
429 | /* | |
430 | * Need to check if we started out being a lower dir, but got copied up | |
431 | */ | |
1afaba1e | 432 | if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) { |
e9be9d5e MS |
433 | struct inode *inode = file_inode(file); |
434 | ||
7676895f | 435 | realfile = lockless_dereference(od->upperfile); |
e9be9d5e MS |
436 | if (!realfile) { |
437 | struct path upperpath; | |
438 | ||
439 | ovl_path_upper(dentry, &upperpath); | |
440 | realfile = ovl_path_open(&upperpath, O_RDONLY); | |
d45f00ae | 441 | smp_mb__before_spinlock(); |
3d268c9b AV |
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); | |
447 | } | |
448 | od->upperfile = realfile; | |
449 | } else { | |
450 | /* somebody has beaten us to it */ | |
451 | if (!IS_ERR(realfile)) | |
452 | fput(realfile); | |
453 | realfile = od->upperfile; | |
e9be9d5e | 454 | } |
3d268c9b | 455 | mutex_unlock(&inode->i_mutex); |
e9be9d5e | 456 | } |
e9be9d5e MS |
457 | } |
458 | ||
459 | return vfs_fsync_range(realfile, start, end, datasync); | |
460 | } | |
461 | ||
462 | static int ovl_dir_release(struct inode *inode, struct file *file) | |
463 | { | |
464 | struct ovl_dir_file *od = file->private_data; | |
465 | ||
466 | if (od->cache) { | |
467 | mutex_lock(&inode->i_mutex); | |
468 | ovl_cache_put(od, file->f_path.dentry); | |
469 | mutex_unlock(&inode->i_mutex); | |
470 | } | |
471 | fput(od->realfile); | |
472 | if (od->upperfile) | |
473 | fput(od->upperfile); | |
474 | kfree(od); | |
475 | ||
476 | return 0; | |
477 | } | |
478 | ||
479 | static int ovl_dir_open(struct inode *inode, struct file *file) | |
480 | { | |
481 | struct path realpath; | |
482 | struct file *realfile; | |
483 | struct ovl_dir_file *od; | |
484 | enum ovl_path_type type; | |
485 | ||
486 | od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL); | |
487 | if (!od) | |
488 | return -ENOMEM; | |
489 | ||
490 | type = ovl_path_real(file->f_path.dentry, &realpath); | |
491 | realfile = ovl_path_open(&realpath, file->f_flags); | |
492 | if (IS_ERR(realfile)) { | |
493 | kfree(od); | |
494 | return PTR_ERR(realfile); | |
495 | } | |
496 | INIT_LIST_HEAD(&od->cursor.l_node); | |
497 | od->realfile = realfile; | |
1afaba1e MS |
498 | od->is_real = !OVL_TYPE_MERGE(type); |
499 | od->is_upper = OVL_TYPE_UPPER(type); | |
c2096537 | 500 | od->cursor.is_cursor = true; |
e9be9d5e MS |
501 | file->private_data = od; |
502 | ||
503 | return 0; | |
504 | } | |
505 | ||
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, | |
513 | }; | |
514 | ||
515 | int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list) | |
516 | { | |
517 | int err; | |
e9be9d5e MS |
518 | struct ovl_cache_entry *p; |
519 | ||
c9f00fdb | 520 | err = ovl_dir_read_merged(dentry, list); |
e9be9d5e MS |
521 | if (err) |
522 | return err; | |
523 | ||
524 | err = 0; | |
525 | ||
526 | list_for_each_entry(p, list, l_node) { | |
527 | if (p->is_whiteout) | |
528 | continue; | |
529 | ||
530 | if (p->name[0] == '.') { | |
531 | if (p->len == 1) | |
532 | continue; | |
533 | if (p->len == 2 && p->name[1] == '.') | |
534 | continue; | |
535 | } | |
536 | err = -ENOTEMPTY; | |
537 | break; | |
538 | } | |
539 | ||
540 | return err; | |
541 | } | |
542 | ||
543 | void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list) | |
544 | { | |
545 | struct ovl_cache_entry *p; | |
546 | ||
d1b72cc6 | 547 | mutex_lock_nested(&upper->d_inode->i_mutex, I_MUTEX_CHILD); |
e9be9d5e MS |
548 | list_for_each_entry(p, list, l_node) { |
549 | struct dentry *dentry; | |
550 | ||
551 | if (!p->is_whiteout) | |
552 | continue; | |
553 | ||
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)); | |
559 | continue; | |
560 | } | |
561 | ovl_cleanup(upper->d_inode, dentry); | |
562 | dput(dentry); | |
563 | } | |
564 | mutex_unlock(&upper->d_inode->i_mutex); | |
565 | } |