Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszer...
[deliverable/linux.git] / fs / overlayfs / super.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/namei.h>
12 #include <linux/pagemap.h>
13 #include <linux/xattr.h>
14 #include <linux/security.h>
15 #include <linux/mount.h>
16 #include <linux/slab.h>
17 #include <linux/parser.h>
18 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/statfs.h>
22 #include <linux/seq_file.h>
23 #include "overlayfs.h"
24
25 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
26 MODULE_DESCRIPTION("Overlay filesystem");
27 MODULE_LICENSE("GPL");
28
29 struct ovl_config {
30 char *lowerdir;
31 char *upperdir;
32 char *workdir;
33 bool default_permissions;
34 };
35
36 /* private information held for overlayfs's superblock */
37 struct ovl_fs {
38 struct vfsmount *upper_mnt;
39 unsigned numlower;
40 struct vfsmount **lower_mnt;
41 struct dentry *workdir;
42 long lower_namelen;
43 /* pathnames of lower and upper dirs, for show_options */
44 struct ovl_config config;
45 /* creds of process who forced instantiation of super block */
46 const struct cred *creator_cred;
47 };
48
49 struct ovl_dir_cache;
50
51 /* private information held for every overlayfs dentry */
52 struct ovl_entry {
53 struct dentry *__upperdentry;
54 struct ovl_dir_cache *cache;
55 union {
56 struct {
57 u64 version;
58 bool opaque;
59 };
60 struct rcu_head rcu;
61 };
62 unsigned numlower;
63 struct path lowerstack[];
64 };
65
66 #define OVL_MAX_STACK 500
67
68 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
69 {
70 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
71 }
72
73 enum ovl_path_type ovl_path_type(struct dentry *dentry)
74 {
75 struct ovl_entry *oe = dentry->d_fsdata;
76 enum ovl_path_type type = 0;
77
78 if (oe->__upperdentry) {
79 type = __OVL_PATH_UPPER;
80
81 /*
82 * Non-dir dentry can hold lower dentry from previous
83 * location. Its purity depends only on opaque flag.
84 */
85 if (oe->numlower && S_ISDIR(dentry->d_inode->i_mode))
86 type |= __OVL_PATH_MERGE;
87 else if (!oe->opaque)
88 type |= __OVL_PATH_PURE;
89 } else {
90 if (oe->numlower > 1)
91 type |= __OVL_PATH_MERGE;
92 }
93 return type;
94 }
95
96 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
97 {
98 return lockless_dereference(oe->__upperdentry);
99 }
100
101 void ovl_path_upper(struct dentry *dentry, struct path *path)
102 {
103 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
104 struct ovl_entry *oe = dentry->d_fsdata;
105
106 path->mnt = ofs->upper_mnt;
107 path->dentry = ovl_upperdentry_dereference(oe);
108 }
109
110 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
111 {
112 enum ovl_path_type type = ovl_path_type(dentry);
113
114 if (!OVL_TYPE_UPPER(type))
115 ovl_path_lower(dentry, path);
116 else
117 ovl_path_upper(dentry, path);
118
119 return type;
120 }
121
122 struct dentry *ovl_dentry_upper(struct dentry *dentry)
123 {
124 struct ovl_entry *oe = dentry->d_fsdata;
125
126 return ovl_upperdentry_dereference(oe);
127 }
128
129 struct dentry *ovl_dentry_lower(struct dentry *dentry)
130 {
131 struct ovl_entry *oe = dentry->d_fsdata;
132
133 return __ovl_dentry_lower(oe);
134 }
135
136 struct dentry *ovl_dentry_real(struct dentry *dentry)
137 {
138 struct ovl_entry *oe = dentry->d_fsdata;
139 struct dentry *realdentry;
140
141 realdentry = ovl_upperdentry_dereference(oe);
142 if (!realdentry)
143 realdentry = __ovl_dentry_lower(oe);
144
145 return realdentry;
146 }
147
148 struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
149 {
150 struct dentry *realdentry;
151
152 realdentry = ovl_upperdentry_dereference(oe);
153 if (realdentry) {
154 *is_upper = true;
155 } else {
156 realdentry = __ovl_dentry_lower(oe);
157 *is_upper = false;
158 }
159 return realdentry;
160 }
161
162 struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
163 bool is_upper)
164 {
165 if (is_upper) {
166 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
167
168 return ofs->upper_mnt;
169 } else {
170 return oe->numlower ? oe->lowerstack[0].mnt : NULL;
171 }
172 }
173
174 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
175 {
176 struct ovl_entry *oe = dentry->d_fsdata;
177
178 return oe->cache;
179 }
180
181 bool ovl_is_default_permissions(struct inode *inode)
182 {
183 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
184
185 return ofs->config.default_permissions;
186 }
187
188 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
189 {
190 struct ovl_entry *oe = dentry->d_fsdata;
191
192 oe->cache = cache;
193 }
194
195 void ovl_path_lower(struct dentry *dentry, struct path *path)
196 {
197 struct ovl_entry *oe = dentry->d_fsdata;
198
199 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
200 }
201
202 int ovl_want_write(struct dentry *dentry)
203 {
204 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
205 return mnt_want_write(ofs->upper_mnt);
206 }
207
208 void ovl_drop_write(struct dentry *dentry)
209 {
210 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
211 mnt_drop_write(ofs->upper_mnt);
212 }
213
214 struct dentry *ovl_workdir(struct dentry *dentry)
215 {
216 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
217 return ofs->workdir;
218 }
219
220 bool ovl_dentry_is_opaque(struct dentry *dentry)
221 {
222 struct ovl_entry *oe = dentry->d_fsdata;
223 return oe->opaque;
224 }
225
226 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
227 {
228 struct ovl_entry *oe = dentry->d_fsdata;
229 oe->opaque = opaque;
230 }
231
232 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
233 {
234 struct ovl_entry *oe = dentry->d_fsdata;
235
236 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
237 WARN_ON(oe->__upperdentry);
238 BUG_ON(!upperdentry->d_inode);
239 /*
240 * Make sure upperdentry is consistent before making it visible to
241 * ovl_upperdentry_dereference().
242 */
243 smp_wmb();
244 oe->__upperdentry = upperdentry;
245 }
246
247 void ovl_dentry_version_inc(struct dentry *dentry)
248 {
249 struct ovl_entry *oe = dentry->d_fsdata;
250
251 WARN_ON(!inode_is_locked(dentry->d_inode));
252 oe->version++;
253 }
254
255 u64 ovl_dentry_version_get(struct dentry *dentry)
256 {
257 struct ovl_entry *oe = dentry->d_fsdata;
258
259 WARN_ON(!inode_is_locked(dentry->d_inode));
260 return oe->version;
261 }
262
263 bool ovl_is_whiteout(struct dentry *dentry)
264 {
265 struct inode *inode = dentry->d_inode;
266
267 return inode && IS_WHITEOUT(inode);
268 }
269
270 const struct cred *ovl_override_creds(struct super_block *sb)
271 {
272 struct ovl_fs *ofs = sb->s_fs_info;
273
274 return override_creds(ofs->creator_cred);
275 }
276
277 static bool ovl_is_opaquedir(struct dentry *dentry)
278 {
279 int res;
280 char val;
281 struct inode *inode = dentry->d_inode;
282
283 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
284 return false;
285
286 res = inode->i_op->getxattr(dentry, inode, OVL_XATTR_OPAQUE, &val, 1);
287 if (res == 1 && val == 'y')
288 return true;
289
290 return false;
291 }
292
293 static void ovl_dentry_release(struct dentry *dentry)
294 {
295 struct ovl_entry *oe = dentry->d_fsdata;
296
297 if (oe) {
298 unsigned int i;
299
300 dput(oe->__upperdentry);
301 for (i = 0; i < oe->numlower; i++)
302 dput(oe->lowerstack[i].dentry);
303 kfree_rcu(oe, rcu);
304 }
305 }
306
307 static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode)
308 {
309 struct dentry *real;
310
311 if (d_is_dir(dentry)) {
312 if (!inode || inode == d_inode(dentry))
313 return dentry;
314 goto bug;
315 }
316
317 real = ovl_dentry_upper(dentry);
318 if (real && (!inode || inode == d_inode(real)))
319 return real;
320
321 real = ovl_dentry_lower(dentry);
322 if (!real)
323 goto bug;
324
325 if (!inode || inode == d_inode(real))
326 return real;
327
328 /* Handle recursion */
329 if (real->d_flags & DCACHE_OP_REAL)
330 return real->d_op->d_real(real, inode);
331
332 bug:
333 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
334 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
335 return dentry;
336 }
337
338 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
339 {
340 struct ovl_entry *oe = dentry->d_fsdata;
341 unsigned int i;
342 int ret = 1;
343
344 for (i = 0; i < oe->numlower; i++) {
345 struct dentry *d = oe->lowerstack[i].dentry;
346
347 if (d->d_flags & DCACHE_OP_REVALIDATE) {
348 ret = d->d_op->d_revalidate(d, flags);
349 if (ret < 0)
350 return ret;
351 if (!ret) {
352 if (!(flags & LOOKUP_RCU))
353 d_invalidate(d);
354 return -ESTALE;
355 }
356 }
357 }
358 return 1;
359 }
360
361 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
362 {
363 struct ovl_entry *oe = dentry->d_fsdata;
364 unsigned int i;
365 int ret = 1;
366
367 for (i = 0; i < oe->numlower; i++) {
368 struct dentry *d = oe->lowerstack[i].dentry;
369
370 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
371 ret = d->d_op->d_weak_revalidate(d, flags);
372 if (ret <= 0)
373 break;
374 }
375 }
376 return ret;
377 }
378
379 static const struct dentry_operations ovl_dentry_operations = {
380 .d_release = ovl_dentry_release,
381 .d_select_inode = ovl_d_select_inode,
382 .d_real = ovl_d_real,
383 };
384
385 static const struct dentry_operations ovl_reval_dentry_operations = {
386 .d_release = ovl_dentry_release,
387 .d_select_inode = ovl_d_select_inode,
388 .d_real = ovl_d_real,
389 .d_revalidate = ovl_dentry_revalidate,
390 .d_weak_revalidate = ovl_dentry_weak_revalidate,
391 };
392
393 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
394 {
395 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
396 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
397
398 if (oe)
399 oe->numlower = numlower;
400
401 return oe;
402 }
403
404 static bool ovl_dentry_remote(struct dentry *dentry)
405 {
406 return dentry->d_flags &
407 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
408 }
409
410 static bool ovl_dentry_weird(struct dentry *dentry)
411 {
412 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
413 DCACHE_MANAGE_TRANSIT |
414 DCACHE_OP_HASH |
415 DCACHE_OP_COMPARE);
416 }
417
418 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
419 struct qstr *name)
420 {
421 struct dentry *dentry;
422
423 dentry = lookup_hash(name, dir);
424
425 if (IS_ERR(dentry)) {
426 if (PTR_ERR(dentry) == -ENOENT)
427 dentry = NULL;
428 } else if (!dentry->d_inode) {
429 dput(dentry);
430 dentry = NULL;
431 } else if (ovl_dentry_weird(dentry)) {
432 dput(dentry);
433 /* Don't support traversing automounts and other weirdness */
434 dentry = ERR_PTR(-EREMOTE);
435 }
436 return dentry;
437 }
438
439 /*
440 * Returns next layer in stack starting from top.
441 * Returns -1 if this is the last layer.
442 */
443 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
444 {
445 struct ovl_entry *oe = dentry->d_fsdata;
446
447 BUG_ON(idx < 0);
448 if (idx == 0) {
449 ovl_path_upper(dentry, path);
450 if (path->dentry)
451 return oe->numlower ? 1 : -1;
452 idx++;
453 }
454 BUG_ON(idx > oe->numlower);
455 *path = oe->lowerstack[idx - 1];
456
457 return (idx < oe->numlower) ? idx + 1 : -1;
458 }
459
460 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
461 unsigned int flags)
462 {
463 struct ovl_entry *oe;
464 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
465 struct path *stack = NULL;
466 struct dentry *upperdir, *upperdentry = NULL;
467 unsigned int ctr = 0;
468 struct inode *inode = NULL;
469 bool upperopaque = false;
470 struct dentry *this, *prev = NULL;
471 unsigned int i;
472 int err;
473
474 upperdir = ovl_upperdentry_dereference(poe);
475 if (upperdir) {
476 this = ovl_lookup_real(upperdir, &dentry->d_name);
477 err = PTR_ERR(this);
478 if (IS_ERR(this))
479 goto out;
480
481 if (this) {
482 if (unlikely(ovl_dentry_remote(this))) {
483 dput(this);
484 err = -EREMOTE;
485 goto out;
486 }
487 if (ovl_is_whiteout(this)) {
488 dput(this);
489 this = NULL;
490 upperopaque = true;
491 } else if (poe->numlower && ovl_is_opaquedir(this)) {
492 upperopaque = true;
493 }
494 }
495 upperdentry = prev = this;
496 }
497
498 if (!upperopaque && poe->numlower) {
499 err = -ENOMEM;
500 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
501 if (!stack)
502 goto out_put_upper;
503 }
504
505 for (i = 0; !upperopaque && i < poe->numlower; i++) {
506 bool opaque = false;
507 struct path lowerpath = poe->lowerstack[i];
508
509 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
510 err = PTR_ERR(this);
511 if (IS_ERR(this)) {
512 /*
513 * If it's positive, then treat ENAMETOOLONG as ENOENT.
514 */
515 if (err == -ENAMETOOLONG && (upperdentry || ctr))
516 continue;
517 goto out_put;
518 }
519 if (!this)
520 continue;
521 if (ovl_is_whiteout(this)) {
522 dput(this);
523 break;
524 }
525 /*
526 * Only makes sense to check opaque dir if this is not the
527 * lowermost layer.
528 */
529 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
530 opaque = true;
531
532 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
533 !S_ISDIR(this->d_inode->i_mode))) {
534 /*
535 * FIXME: check for upper-opaqueness maybe better done
536 * in remove code.
537 */
538 if (prev == upperdentry)
539 upperopaque = true;
540 dput(this);
541 break;
542 }
543 /*
544 * If this is a non-directory then stop here.
545 */
546 if (!S_ISDIR(this->d_inode->i_mode))
547 opaque = true;
548
549 stack[ctr].dentry = this;
550 stack[ctr].mnt = lowerpath.mnt;
551 ctr++;
552 prev = this;
553 if (opaque)
554 break;
555 }
556
557 oe = ovl_alloc_entry(ctr);
558 err = -ENOMEM;
559 if (!oe)
560 goto out_put;
561
562 if (upperdentry || ctr) {
563 struct dentry *realdentry;
564
565 realdentry = upperdentry ? upperdentry : stack[0].dentry;
566
567 err = -ENOMEM;
568 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
569 oe);
570 if (!inode)
571 goto out_free_oe;
572 ovl_copyattr(realdentry->d_inode, inode);
573 }
574
575 oe->opaque = upperopaque;
576 oe->__upperdentry = upperdentry;
577 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
578 kfree(stack);
579 dentry->d_fsdata = oe;
580 d_add(dentry, inode);
581
582 return NULL;
583
584 out_free_oe:
585 kfree(oe);
586 out_put:
587 for (i = 0; i < ctr; i++)
588 dput(stack[i].dentry);
589 kfree(stack);
590 out_put_upper:
591 dput(upperdentry);
592 out:
593 return ERR_PTR(err);
594 }
595
596 struct file *ovl_path_open(struct path *path, int flags)
597 {
598 return dentry_open(path, flags, current_cred());
599 }
600
601 static void ovl_put_super(struct super_block *sb)
602 {
603 struct ovl_fs *ufs = sb->s_fs_info;
604 unsigned i;
605
606 dput(ufs->workdir);
607 mntput(ufs->upper_mnt);
608 for (i = 0; i < ufs->numlower; i++)
609 mntput(ufs->lower_mnt[i]);
610 kfree(ufs->lower_mnt);
611
612 kfree(ufs->config.lowerdir);
613 kfree(ufs->config.upperdir);
614 kfree(ufs->config.workdir);
615 put_cred(ufs->creator_cred);
616 kfree(ufs);
617 }
618
619 /**
620 * ovl_statfs
621 * @sb: The overlayfs super block
622 * @buf: The struct kstatfs to fill in with stats
623 *
624 * Get the filesystem statistics. As writes always target the upper layer
625 * filesystem pass the statfs to the upper filesystem (if it exists)
626 */
627 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
628 {
629 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
630 struct dentry *root_dentry = dentry->d_sb->s_root;
631 struct path path;
632 int err;
633
634 ovl_path_real(root_dentry, &path);
635
636 err = vfs_statfs(&path, buf);
637 if (!err) {
638 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
639 buf->f_type = OVERLAYFS_SUPER_MAGIC;
640 }
641
642 return err;
643 }
644
645 /**
646 * ovl_show_options
647 *
648 * Prints the mount options for a given superblock.
649 * Returns zero; does not fail.
650 */
651 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
652 {
653 struct super_block *sb = dentry->d_sb;
654 struct ovl_fs *ufs = sb->s_fs_info;
655
656 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
657 if (ufs->config.upperdir) {
658 seq_show_option(m, "upperdir", ufs->config.upperdir);
659 seq_show_option(m, "workdir", ufs->config.workdir);
660 }
661 if (ufs->config.default_permissions)
662 seq_puts(m, ",default_permissions");
663 return 0;
664 }
665
666 static int ovl_remount(struct super_block *sb, int *flags, char *data)
667 {
668 struct ovl_fs *ufs = sb->s_fs_info;
669
670 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
671 return -EROFS;
672
673 return 0;
674 }
675
676 static const struct super_operations ovl_super_operations = {
677 .put_super = ovl_put_super,
678 .statfs = ovl_statfs,
679 .show_options = ovl_show_options,
680 .remount_fs = ovl_remount,
681 };
682
683 enum {
684 OPT_LOWERDIR,
685 OPT_UPPERDIR,
686 OPT_WORKDIR,
687 OPT_DEFAULT_PERMISSIONS,
688 OPT_ERR,
689 };
690
691 static const match_table_t ovl_tokens = {
692 {OPT_LOWERDIR, "lowerdir=%s"},
693 {OPT_UPPERDIR, "upperdir=%s"},
694 {OPT_WORKDIR, "workdir=%s"},
695 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
696 {OPT_ERR, NULL}
697 };
698
699 static char *ovl_next_opt(char **s)
700 {
701 char *sbegin = *s;
702 char *p;
703
704 if (sbegin == NULL)
705 return NULL;
706
707 for (p = sbegin; *p; p++) {
708 if (*p == '\\') {
709 p++;
710 if (!*p)
711 break;
712 } else if (*p == ',') {
713 *p = '\0';
714 *s = p + 1;
715 return sbegin;
716 }
717 }
718 *s = NULL;
719 return sbegin;
720 }
721
722 static int ovl_parse_opt(char *opt, struct ovl_config *config)
723 {
724 char *p;
725
726 while ((p = ovl_next_opt(&opt)) != NULL) {
727 int token;
728 substring_t args[MAX_OPT_ARGS];
729
730 if (!*p)
731 continue;
732
733 token = match_token(p, ovl_tokens, args);
734 switch (token) {
735 case OPT_UPPERDIR:
736 kfree(config->upperdir);
737 config->upperdir = match_strdup(&args[0]);
738 if (!config->upperdir)
739 return -ENOMEM;
740 break;
741
742 case OPT_LOWERDIR:
743 kfree(config->lowerdir);
744 config->lowerdir = match_strdup(&args[0]);
745 if (!config->lowerdir)
746 return -ENOMEM;
747 break;
748
749 case OPT_WORKDIR:
750 kfree(config->workdir);
751 config->workdir = match_strdup(&args[0]);
752 if (!config->workdir)
753 return -ENOMEM;
754 break;
755
756 case OPT_DEFAULT_PERMISSIONS:
757 config->default_permissions = true;
758 break;
759
760 default:
761 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
762 return -EINVAL;
763 }
764 }
765
766 /* Workdir is useless in non-upper mount */
767 if (!config->upperdir && config->workdir) {
768 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
769 config->workdir);
770 kfree(config->workdir);
771 config->workdir = NULL;
772 }
773
774 return 0;
775 }
776
777 #define OVL_WORKDIR_NAME "work"
778
779 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
780 struct dentry *dentry)
781 {
782 struct inode *dir = dentry->d_inode;
783 struct dentry *work;
784 int err;
785 bool retried = false;
786
787 err = mnt_want_write(mnt);
788 if (err)
789 return ERR_PTR(err);
790
791 inode_lock_nested(dir, I_MUTEX_PARENT);
792 retry:
793 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
794 strlen(OVL_WORKDIR_NAME));
795
796 if (!IS_ERR(work)) {
797 struct kstat stat = {
798 .mode = S_IFDIR | 0,
799 };
800
801 if (work->d_inode) {
802 err = -EEXIST;
803 if (retried)
804 goto out_dput;
805
806 retried = true;
807 ovl_cleanup(dir, work);
808 dput(work);
809 goto retry;
810 }
811
812 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
813 if (err)
814 goto out_dput;
815 }
816 out_unlock:
817 inode_unlock(dir);
818 mnt_drop_write(mnt);
819
820 return work;
821
822 out_dput:
823 dput(work);
824 work = ERR_PTR(err);
825 goto out_unlock;
826 }
827
828 static void ovl_unescape(char *s)
829 {
830 char *d = s;
831
832 for (;; s++, d++) {
833 if (*s == '\\')
834 s++;
835 *d = *s;
836 if (!*s)
837 break;
838 }
839 }
840
841 static int ovl_mount_dir_noesc(const char *name, struct path *path)
842 {
843 int err = -EINVAL;
844
845 if (!*name) {
846 pr_err("overlayfs: empty lowerdir\n");
847 goto out;
848 }
849 err = kern_path(name, LOOKUP_FOLLOW, path);
850 if (err) {
851 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
852 goto out;
853 }
854 err = -EINVAL;
855 if (ovl_dentry_weird(path->dentry)) {
856 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
857 goto out_put;
858 }
859 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
860 pr_err("overlayfs: '%s' not a directory\n", name);
861 goto out_put;
862 }
863 return 0;
864
865 out_put:
866 path_put(path);
867 out:
868 return err;
869 }
870
871 static int ovl_mount_dir(const char *name, struct path *path)
872 {
873 int err = -ENOMEM;
874 char *tmp = kstrdup(name, GFP_KERNEL);
875
876 if (tmp) {
877 ovl_unescape(tmp);
878 err = ovl_mount_dir_noesc(tmp, path);
879
880 if (!err)
881 if (ovl_dentry_remote(path->dentry)) {
882 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
883 tmp);
884 path_put(path);
885 err = -EINVAL;
886 }
887 kfree(tmp);
888 }
889 return err;
890 }
891
892 static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
893 int *stack_depth, bool *remote)
894 {
895 int err;
896 struct kstatfs statfs;
897
898 err = ovl_mount_dir_noesc(name, path);
899 if (err)
900 goto out;
901
902 err = vfs_statfs(path, &statfs);
903 if (err) {
904 pr_err("overlayfs: statfs failed on '%s'\n", name);
905 goto out_put;
906 }
907 *namelen = max(*namelen, statfs.f_namelen);
908 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
909
910 if (ovl_dentry_remote(path->dentry))
911 *remote = true;
912
913 return 0;
914
915 out_put:
916 path_put(path);
917 out:
918 return err;
919 }
920
921 /* Workdir should not be subdir of upperdir and vice versa */
922 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
923 {
924 bool ok = false;
925
926 if (workdir != upperdir) {
927 ok = (lock_rename(workdir, upperdir) == NULL);
928 unlock_rename(workdir, upperdir);
929 }
930 return ok;
931 }
932
933 static unsigned int ovl_split_lowerdirs(char *str)
934 {
935 unsigned int ctr = 1;
936 char *s, *d;
937
938 for (s = d = str;; s++, d++) {
939 if (*s == '\\') {
940 s++;
941 } else if (*s == ':') {
942 *d = '\0';
943 ctr++;
944 continue;
945 }
946 *d = *s;
947 if (!*s)
948 break;
949 }
950 return ctr;
951 }
952
953 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
954 {
955 struct path upperpath = { NULL, NULL };
956 struct path workpath = { NULL, NULL };
957 struct dentry *root_dentry;
958 struct ovl_entry *oe;
959 struct ovl_fs *ufs;
960 struct path *stack = NULL;
961 char *lowertmp;
962 char *lower;
963 unsigned int numlower;
964 unsigned int stacklen = 0;
965 unsigned int i;
966 bool remote = false;
967 int err;
968
969 err = -ENOMEM;
970 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
971 if (!ufs)
972 goto out;
973
974 err = ovl_parse_opt((char *) data, &ufs->config);
975 if (err)
976 goto out_free_config;
977
978 err = -EINVAL;
979 if (!ufs->config.lowerdir) {
980 if (!silent)
981 pr_err("overlayfs: missing 'lowerdir'\n");
982 goto out_free_config;
983 }
984
985 sb->s_stack_depth = 0;
986 sb->s_maxbytes = MAX_LFS_FILESIZE;
987 if (ufs->config.upperdir) {
988 if (!ufs->config.workdir) {
989 pr_err("overlayfs: missing 'workdir'\n");
990 goto out_free_config;
991 }
992
993 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
994 if (err)
995 goto out_free_config;
996
997 /* Upper fs should not be r/o */
998 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
999 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1000 err = -EINVAL;
1001 goto out_put_upperpath;
1002 }
1003
1004 err = ovl_mount_dir(ufs->config.workdir, &workpath);
1005 if (err)
1006 goto out_put_upperpath;
1007
1008 err = -EINVAL;
1009 if (upperpath.mnt != workpath.mnt) {
1010 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1011 goto out_put_workpath;
1012 }
1013 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1014 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1015 goto out_put_workpath;
1016 }
1017 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
1018 }
1019 err = -ENOMEM;
1020 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1021 if (!lowertmp)
1022 goto out_put_workpath;
1023
1024 err = -EINVAL;
1025 stacklen = ovl_split_lowerdirs(lowertmp);
1026 if (stacklen > OVL_MAX_STACK) {
1027 pr_err("overlayfs: too many lower directries, limit is %d\n",
1028 OVL_MAX_STACK);
1029 goto out_free_lowertmp;
1030 } else if (!ufs->config.upperdir && stacklen == 1) {
1031 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1032 goto out_free_lowertmp;
1033 }
1034
1035 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1036 if (!stack)
1037 goto out_free_lowertmp;
1038
1039 lower = lowertmp;
1040 for (numlower = 0; numlower < stacklen; numlower++) {
1041 err = ovl_lower_dir(lower, &stack[numlower],
1042 &ufs->lower_namelen, &sb->s_stack_depth,
1043 &remote);
1044 if (err)
1045 goto out_put_lowerpath;
1046
1047 lower = strchr(lower, '\0') + 1;
1048 }
1049
1050 err = -EINVAL;
1051 sb->s_stack_depth++;
1052 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1053 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1054 goto out_put_lowerpath;
1055 }
1056
1057 if (ufs->config.upperdir) {
1058 ufs->upper_mnt = clone_private_mount(&upperpath);
1059 err = PTR_ERR(ufs->upper_mnt);
1060 if (IS_ERR(ufs->upper_mnt)) {
1061 pr_err("overlayfs: failed to clone upperpath\n");
1062 goto out_put_lowerpath;
1063 }
1064
1065 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1066 err = PTR_ERR(ufs->workdir);
1067 if (IS_ERR(ufs->workdir)) {
1068 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1069 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1070 sb->s_flags |= MS_RDONLY;
1071 ufs->workdir = NULL;
1072 }
1073
1074 /*
1075 * Upper should support d_type, else whiteouts are visible.
1076 * Given workdir and upper are on same fs, we can do
1077 * iterate_dir() on workdir. This check requires successful
1078 * creation of workdir in previous step.
1079 */
1080 if (ufs->workdir) {
1081 err = ovl_check_d_type_supported(&workpath);
1082 if (err < 0)
1083 goto out_put_workdir;
1084
1085 if (!err) {
1086 pr_err("overlayfs: upper fs needs to support d_type.\n");
1087 err = -EINVAL;
1088 goto out_put_workdir;
1089 }
1090 }
1091 }
1092
1093 err = -ENOMEM;
1094 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1095 if (ufs->lower_mnt == NULL)
1096 goto out_put_workdir;
1097 for (i = 0; i < numlower; i++) {
1098 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1099
1100 err = PTR_ERR(mnt);
1101 if (IS_ERR(mnt)) {
1102 pr_err("overlayfs: failed to clone lowerpath\n");
1103 goto out_put_lower_mnt;
1104 }
1105 /*
1106 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1107 * will fail instead of modifying lower fs.
1108 */
1109 mnt->mnt_flags |= MNT_READONLY;
1110
1111 ufs->lower_mnt[ufs->numlower] = mnt;
1112 ufs->numlower++;
1113 }
1114
1115 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1116 if (!ufs->upper_mnt)
1117 sb->s_flags |= MS_RDONLY;
1118
1119 if (remote)
1120 sb->s_d_op = &ovl_reval_dentry_operations;
1121 else
1122 sb->s_d_op = &ovl_dentry_operations;
1123
1124 ufs->creator_cred = prepare_creds();
1125 if (!ufs->creator_cred)
1126 goto out_put_lower_mnt;
1127
1128 err = -ENOMEM;
1129 oe = ovl_alloc_entry(numlower);
1130 if (!oe)
1131 goto out_put_cred;
1132
1133 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
1134 if (!root_dentry)
1135 goto out_free_oe;
1136
1137 mntput(upperpath.mnt);
1138 for (i = 0; i < numlower; i++)
1139 mntput(stack[i].mnt);
1140 path_put(&workpath);
1141 kfree(lowertmp);
1142
1143 oe->__upperdentry = upperpath.dentry;
1144 for (i = 0; i < numlower; i++) {
1145 oe->lowerstack[i].dentry = stack[i].dentry;
1146 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1147 }
1148 kfree(stack);
1149
1150 root_dentry->d_fsdata = oe;
1151
1152 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1153 root_dentry->d_inode);
1154
1155 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1156 sb->s_op = &ovl_super_operations;
1157 sb->s_root = root_dentry;
1158 sb->s_fs_info = ufs;
1159
1160 return 0;
1161
1162 out_free_oe:
1163 kfree(oe);
1164 out_put_cred:
1165 put_cred(ufs->creator_cred);
1166 out_put_lower_mnt:
1167 for (i = 0; i < ufs->numlower; i++)
1168 mntput(ufs->lower_mnt[i]);
1169 kfree(ufs->lower_mnt);
1170 out_put_workdir:
1171 dput(ufs->workdir);
1172 mntput(ufs->upper_mnt);
1173 out_put_lowerpath:
1174 for (i = 0; i < numlower; i++)
1175 path_put(&stack[i]);
1176 kfree(stack);
1177 out_free_lowertmp:
1178 kfree(lowertmp);
1179 out_put_workpath:
1180 path_put(&workpath);
1181 out_put_upperpath:
1182 path_put(&upperpath);
1183 out_free_config:
1184 kfree(ufs->config.lowerdir);
1185 kfree(ufs->config.upperdir);
1186 kfree(ufs->config.workdir);
1187 kfree(ufs);
1188 out:
1189 return err;
1190 }
1191
1192 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1193 const char *dev_name, void *raw_data)
1194 {
1195 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1196 }
1197
1198 static struct file_system_type ovl_fs_type = {
1199 .owner = THIS_MODULE,
1200 .name = "overlay",
1201 .mount = ovl_mount,
1202 .kill_sb = kill_anon_super,
1203 };
1204 MODULE_ALIAS_FS("overlay");
1205
1206 static int __init ovl_init(void)
1207 {
1208 return register_filesystem(&ovl_fs_type);
1209 }
1210
1211 static void __exit ovl_exit(void)
1212 {
1213 unregister_filesystem(&ovl_fs_type);
1214 }
1215
1216 module_init(ovl_init);
1217 module_exit(ovl_exit);
This page took 0.083165 seconds and 5 git commands to generate.