]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - fs/overlayfs/super.c
vfs: merge .d_select_inode() into .d_real()
[mirror_ubuntu-hirsute-kernel.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,
308 const struct inode *inode,
309 unsigned int open_flags)
310 {
311 struct dentry *real;
312
313 if (d_is_dir(dentry)) {
314 if (!inode || inode == d_inode(dentry))
315 return dentry;
316 goto bug;
317 }
318
319 if (d_is_negative(dentry))
320 return dentry;
321
322 if (open_flags) {
323 int err = ovl_open_maybe_copy_up(dentry, open_flags);
324
325 if (err)
326 return ERR_PTR(err);
327 }
328
329 real = ovl_dentry_upper(dentry);
330 if (real && (!inode || inode == d_inode(real)))
331 return real;
332
333 real = ovl_dentry_lower(dentry);
334 if (!real)
335 goto bug;
336
337 if (!inode || inode == d_inode(real))
338 return real;
339
340 /* Handle recursion */
341 return d_real(real, inode, open_flags);
342 bug:
343 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
344 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
345 return dentry;
346 }
347
348 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
349 {
350 struct ovl_entry *oe = dentry->d_fsdata;
351 unsigned int i;
352 int ret = 1;
353
354 for (i = 0; i < oe->numlower; i++) {
355 struct dentry *d = oe->lowerstack[i].dentry;
356
357 if (d->d_flags & DCACHE_OP_REVALIDATE) {
358 ret = d->d_op->d_revalidate(d, flags);
359 if (ret < 0)
360 return ret;
361 if (!ret) {
362 if (!(flags & LOOKUP_RCU))
363 d_invalidate(d);
364 return -ESTALE;
365 }
366 }
367 }
368 return 1;
369 }
370
371 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
372 {
373 struct ovl_entry *oe = dentry->d_fsdata;
374 unsigned int i;
375 int ret = 1;
376
377 for (i = 0; i < oe->numlower; i++) {
378 struct dentry *d = oe->lowerstack[i].dentry;
379
380 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
381 ret = d->d_op->d_weak_revalidate(d, flags);
382 if (ret <= 0)
383 break;
384 }
385 }
386 return ret;
387 }
388
389 static const struct dentry_operations ovl_dentry_operations = {
390 .d_release = ovl_dentry_release,
391 .d_real = ovl_d_real,
392 };
393
394 static const struct dentry_operations ovl_reval_dentry_operations = {
395 .d_release = ovl_dentry_release,
396 .d_real = ovl_d_real,
397 .d_revalidate = ovl_dentry_revalidate,
398 .d_weak_revalidate = ovl_dentry_weak_revalidate,
399 };
400
401 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
402 {
403 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
404 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
405
406 if (oe)
407 oe->numlower = numlower;
408
409 return oe;
410 }
411
412 static bool ovl_dentry_remote(struct dentry *dentry)
413 {
414 return dentry->d_flags &
415 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
416 }
417
418 static bool ovl_dentry_weird(struct dentry *dentry)
419 {
420 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
421 DCACHE_MANAGE_TRANSIT |
422 DCACHE_OP_HASH |
423 DCACHE_OP_COMPARE);
424 }
425
426 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
427 struct qstr *name)
428 {
429 struct dentry *dentry;
430
431 dentry = lookup_hash(name, dir);
432
433 if (IS_ERR(dentry)) {
434 if (PTR_ERR(dentry) == -ENOENT)
435 dentry = NULL;
436 } else if (!dentry->d_inode) {
437 dput(dentry);
438 dentry = NULL;
439 } else if (ovl_dentry_weird(dentry)) {
440 dput(dentry);
441 /* Don't support traversing automounts and other weirdness */
442 dentry = ERR_PTR(-EREMOTE);
443 }
444 return dentry;
445 }
446
447 /*
448 * Returns next layer in stack starting from top.
449 * Returns -1 if this is the last layer.
450 */
451 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
452 {
453 struct ovl_entry *oe = dentry->d_fsdata;
454
455 BUG_ON(idx < 0);
456 if (idx == 0) {
457 ovl_path_upper(dentry, path);
458 if (path->dentry)
459 return oe->numlower ? 1 : -1;
460 idx++;
461 }
462 BUG_ON(idx > oe->numlower);
463 *path = oe->lowerstack[idx - 1];
464
465 return (idx < oe->numlower) ? idx + 1 : -1;
466 }
467
468 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
469 unsigned int flags)
470 {
471 struct ovl_entry *oe;
472 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
473 struct path *stack = NULL;
474 struct dentry *upperdir, *upperdentry = NULL;
475 unsigned int ctr = 0;
476 struct inode *inode = NULL;
477 bool upperopaque = false;
478 struct dentry *this, *prev = NULL;
479 unsigned int i;
480 int err;
481
482 upperdir = ovl_upperdentry_dereference(poe);
483 if (upperdir) {
484 this = ovl_lookup_real(upperdir, &dentry->d_name);
485 err = PTR_ERR(this);
486 if (IS_ERR(this))
487 goto out;
488
489 if (this) {
490 if (unlikely(ovl_dentry_remote(this))) {
491 dput(this);
492 err = -EREMOTE;
493 goto out;
494 }
495 if (ovl_is_whiteout(this)) {
496 dput(this);
497 this = NULL;
498 upperopaque = true;
499 } else if (poe->numlower && ovl_is_opaquedir(this)) {
500 upperopaque = true;
501 }
502 }
503 upperdentry = prev = this;
504 }
505
506 if (!upperopaque && poe->numlower) {
507 err = -ENOMEM;
508 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
509 if (!stack)
510 goto out_put_upper;
511 }
512
513 for (i = 0; !upperopaque && i < poe->numlower; i++) {
514 bool opaque = false;
515 struct path lowerpath = poe->lowerstack[i];
516
517 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
518 err = PTR_ERR(this);
519 if (IS_ERR(this)) {
520 /*
521 * If it's positive, then treat ENAMETOOLONG as ENOENT.
522 */
523 if (err == -ENAMETOOLONG && (upperdentry || ctr))
524 continue;
525 goto out_put;
526 }
527 if (!this)
528 continue;
529 if (ovl_is_whiteout(this)) {
530 dput(this);
531 break;
532 }
533 /*
534 * Only makes sense to check opaque dir if this is not the
535 * lowermost layer.
536 */
537 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
538 opaque = true;
539
540 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
541 !S_ISDIR(this->d_inode->i_mode))) {
542 /*
543 * FIXME: check for upper-opaqueness maybe better done
544 * in remove code.
545 */
546 if (prev == upperdentry)
547 upperopaque = true;
548 dput(this);
549 break;
550 }
551 /*
552 * If this is a non-directory then stop here.
553 */
554 if (!S_ISDIR(this->d_inode->i_mode))
555 opaque = true;
556
557 stack[ctr].dentry = this;
558 stack[ctr].mnt = lowerpath.mnt;
559 ctr++;
560 prev = this;
561 if (opaque)
562 break;
563 }
564
565 oe = ovl_alloc_entry(ctr);
566 err = -ENOMEM;
567 if (!oe)
568 goto out_put;
569
570 if (upperdentry || ctr) {
571 struct dentry *realdentry;
572
573 realdentry = upperdentry ? upperdentry : stack[0].dentry;
574
575 err = -ENOMEM;
576 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
577 oe);
578 if (!inode)
579 goto out_free_oe;
580 ovl_copyattr(realdentry->d_inode, inode);
581 }
582
583 oe->opaque = upperopaque;
584 oe->__upperdentry = upperdentry;
585 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
586 kfree(stack);
587 dentry->d_fsdata = oe;
588 d_add(dentry, inode);
589
590 return NULL;
591
592 out_free_oe:
593 kfree(oe);
594 out_put:
595 for (i = 0; i < ctr; i++)
596 dput(stack[i].dentry);
597 kfree(stack);
598 out_put_upper:
599 dput(upperdentry);
600 out:
601 return ERR_PTR(err);
602 }
603
604 struct file *ovl_path_open(struct path *path, int flags)
605 {
606 return dentry_open(path, flags, current_cred());
607 }
608
609 static void ovl_put_super(struct super_block *sb)
610 {
611 struct ovl_fs *ufs = sb->s_fs_info;
612 unsigned i;
613
614 dput(ufs->workdir);
615 mntput(ufs->upper_mnt);
616 for (i = 0; i < ufs->numlower; i++)
617 mntput(ufs->lower_mnt[i]);
618 kfree(ufs->lower_mnt);
619
620 kfree(ufs->config.lowerdir);
621 kfree(ufs->config.upperdir);
622 kfree(ufs->config.workdir);
623 put_cred(ufs->creator_cred);
624 kfree(ufs);
625 }
626
627 /**
628 * ovl_statfs
629 * @sb: The overlayfs super block
630 * @buf: The struct kstatfs to fill in with stats
631 *
632 * Get the filesystem statistics. As writes always target the upper layer
633 * filesystem pass the statfs to the upper filesystem (if it exists)
634 */
635 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
636 {
637 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
638 struct dentry *root_dentry = dentry->d_sb->s_root;
639 struct path path;
640 int err;
641
642 ovl_path_real(root_dentry, &path);
643
644 err = vfs_statfs(&path, buf);
645 if (!err) {
646 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
647 buf->f_type = OVERLAYFS_SUPER_MAGIC;
648 }
649
650 return err;
651 }
652
653 /**
654 * ovl_show_options
655 *
656 * Prints the mount options for a given superblock.
657 * Returns zero; does not fail.
658 */
659 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
660 {
661 struct super_block *sb = dentry->d_sb;
662 struct ovl_fs *ufs = sb->s_fs_info;
663
664 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
665 if (ufs->config.upperdir) {
666 seq_show_option(m, "upperdir", ufs->config.upperdir);
667 seq_show_option(m, "workdir", ufs->config.workdir);
668 }
669 if (ufs->config.default_permissions)
670 seq_puts(m, ",default_permissions");
671 return 0;
672 }
673
674 static int ovl_remount(struct super_block *sb, int *flags, char *data)
675 {
676 struct ovl_fs *ufs = sb->s_fs_info;
677
678 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
679 return -EROFS;
680
681 return 0;
682 }
683
684 static const struct super_operations ovl_super_operations = {
685 .put_super = ovl_put_super,
686 .statfs = ovl_statfs,
687 .show_options = ovl_show_options,
688 .remount_fs = ovl_remount,
689 };
690
691 enum {
692 OPT_LOWERDIR,
693 OPT_UPPERDIR,
694 OPT_WORKDIR,
695 OPT_DEFAULT_PERMISSIONS,
696 OPT_ERR,
697 };
698
699 static const match_table_t ovl_tokens = {
700 {OPT_LOWERDIR, "lowerdir=%s"},
701 {OPT_UPPERDIR, "upperdir=%s"},
702 {OPT_WORKDIR, "workdir=%s"},
703 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
704 {OPT_ERR, NULL}
705 };
706
707 static char *ovl_next_opt(char **s)
708 {
709 char *sbegin = *s;
710 char *p;
711
712 if (sbegin == NULL)
713 return NULL;
714
715 for (p = sbegin; *p; p++) {
716 if (*p == '\\') {
717 p++;
718 if (!*p)
719 break;
720 } else if (*p == ',') {
721 *p = '\0';
722 *s = p + 1;
723 return sbegin;
724 }
725 }
726 *s = NULL;
727 return sbegin;
728 }
729
730 static int ovl_parse_opt(char *opt, struct ovl_config *config)
731 {
732 char *p;
733
734 while ((p = ovl_next_opt(&opt)) != NULL) {
735 int token;
736 substring_t args[MAX_OPT_ARGS];
737
738 if (!*p)
739 continue;
740
741 token = match_token(p, ovl_tokens, args);
742 switch (token) {
743 case OPT_UPPERDIR:
744 kfree(config->upperdir);
745 config->upperdir = match_strdup(&args[0]);
746 if (!config->upperdir)
747 return -ENOMEM;
748 break;
749
750 case OPT_LOWERDIR:
751 kfree(config->lowerdir);
752 config->lowerdir = match_strdup(&args[0]);
753 if (!config->lowerdir)
754 return -ENOMEM;
755 break;
756
757 case OPT_WORKDIR:
758 kfree(config->workdir);
759 config->workdir = match_strdup(&args[0]);
760 if (!config->workdir)
761 return -ENOMEM;
762 break;
763
764 case OPT_DEFAULT_PERMISSIONS:
765 config->default_permissions = true;
766 break;
767
768 default:
769 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
770 return -EINVAL;
771 }
772 }
773
774 /* Workdir is useless in non-upper mount */
775 if (!config->upperdir && config->workdir) {
776 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
777 config->workdir);
778 kfree(config->workdir);
779 config->workdir = NULL;
780 }
781
782 return 0;
783 }
784
785 #define OVL_WORKDIR_NAME "work"
786
787 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
788 struct dentry *dentry)
789 {
790 struct inode *dir = dentry->d_inode;
791 struct dentry *work;
792 int err;
793 bool retried = false;
794
795 err = mnt_want_write(mnt);
796 if (err)
797 return ERR_PTR(err);
798
799 inode_lock_nested(dir, I_MUTEX_PARENT);
800 retry:
801 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
802 strlen(OVL_WORKDIR_NAME));
803
804 if (!IS_ERR(work)) {
805 struct kstat stat = {
806 .mode = S_IFDIR | 0,
807 };
808
809 if (work->d_inode) {
810 err = -EEXIST;
811 if (retried)
812 goto out_dput;
813
814 retried = true;
815 ovl_cleanup(dir, work);
816 dput(work);
817 goto retry;
818 }
819
820 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
821 if (err)
822 goto out_dput;
823 }
824 out_unlock:
825 inode_unlock(dir);
826 mnt_drop_write(mnt);
827
828 return work;
829
830 out_dput:
831 dput(work);
832 work = ERR_PTR(err);
833 goto out_unlock;
834 }
835
836 static void ovl_unescape(char *s)
837 {
838 char *d = s;
839
840 for (;; s++, d++) {
841 if (*s == '\\')
842 s++;
843 *d = *s;
844 if (!*s)
845 break;
846 }
847 }
848
849 static int ovl_mount_dir_noesc(const char *name, struct path *path)
850 {
851 int err = -EINVAL;
852
853 if (!*name) {
854 pr_err("overlayfs: empty lowerdir\n");
855 goto out;
856 }
857 err = kern_path(name, LOOKUP_FOLLOW, path);
858 if (err) {
859 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
860 goto out;
861 }
862 err = -EINVAL;
863 if (ovl_dentry_weird(path->dentry)) {
864 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
865 goto out_put;
866 }
867 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
868 pr_err("overlayfs: '%s' not a directory\n", name);
869 goto out_put;
870 }
871 return 0;
872
873 out_put:
874 path_put(path);
875 out:
876 return err;
877 }
878
879 static int ovl_mount_dir(const char *name, struct path *path)
880 {
881 int err = -ENOMEM;
882 char *tmp = kstrdup(name, GFP_KERNEL);
883
884 if (tmp) {
885 ovl_unescape(tmp);
886 err = ovl_mount_dir_noesc(tmp, path);
887
888 if (!err)
889 if (ovl_dentry_remote(path->dentry)) {
890 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
891 tmp);
892 path_put(path);
893 err = -EINVAL;
894 }
895 kfree(tmp);
896 }
897 return err;
898 }
899
900 static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
901 int *stack_depth, bool *remote)
902 {
903 int err;
904 struct kstatfs statfs;
905
906 err = ovl_mount_dir_noesc(name, path);
907 if (err)
908 goto out;
909
910 err = vfs_statfs(path, &statfs);
911 if (err) {
912 pr_err("overlayfs: statfs failed on '%s'\n", name);
913 goto out_put;
914 }
915 *namelen = max(*namelen, statfs.f_namelen);
916 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
917
918 if (ovl_dentry_remote(path->dentry))
919 *remote = true;
920
921 return 0;
922
923 out_put:
924 path_put(path);
925 out:
926 return err;
927 }
928
929 /* Workdir should not be subdir of upperdir and vice versa */
930 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
931 {
932 bool ok = false;
933
934 if (workdir != upperdir) {
935 ok = (lock_rename(workdir, upperdir) == NULL);
936 unlock_rename(workdir, upperdir);
937 }
938 return ok;
939 }
940
941 static unsigned int ovl_split_lowerdirs(char *str)
942 {
943 unsigned int ctr = 1;
944 char *s, *d;
945
946 for (s = d = str;; s++, d++) {
947 if (*s == '\\') {
948 s++;
949 } else if (*s == ':') {
950 *d = '\0';
951 ctr++;
952 continue;
953 }
954 *d = *s;
955 if (!*s)
956 break;
957 }
958 return ctr;
959 }
960
961 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
962 {
963 struct path upperpath = { NULL, NULL };
964 struct path workpath = { NULL, NULL };
965 struct dentry *root_dentry;
966 struct ovl_entry *oe;
967 struct ovl_fs *ufs;
968 struct path *stack = NULL;
969 char *lowertmp;
970 char *lower;
971 unsigned int numlower;
972 unsigned int stacklen = 0;
973 unsigned int i;
974 bool remote = false;
975 int err;
976
977 err = -ENOMEM;
978 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
979 if (!ufs)
980 goto out;
981
982 err = ovl_parse_opt((char *) data, &ufs->config);
983 if (err)
984 goto out_free_config;
985
986 err = -EINVAL;
987 if (!ufs->config.lowerdir) {
988 if (!silent)
989 pr_err("overlayfs: missing 'lowerdir'\n");
990 goto out_free_config;
991 }
992
993 sb->s_stack_depth = 0;
994 sb->s_maxbytes = MAX_LFS_FILESIZE;
995 if (ufs->config.upperdir) {
996 if (!ufs->config.workdir) {
997 pr_err("overlayfs: missing 'workdir'\n");
998 goto out_free_config;
999 }
1000
1001 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
1002 if (err)
1003 goto out_free_config;
1004
1005 /* Upper fs should not be r/o */
1006 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
1007 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1008 err = -EINVAL;
1009 goto out_put_upperpath;
1010 }
1011
1012 err = ovl_mount_dir(ufs->config.workdir, &workpath);
1013 if (err)
1014 goto out_put_upperpath;
1015
1016 err = -EINVAL;
1017 if (upperpath.mnt != workpath.mnt) {
1018 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1019 goto out_put_workpath;
1020 }
1021 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1022 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1023 goto out_put_workpath;
1024 }
1025 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
1026 }
1027 err = -ENOMEM;
1028 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1029 if (!lowertmp)
1030 goto out_put_workpath;
1031
1032 err = -EINVAL;
1033 stacklen = ovl_split_lowerdirs(lowertmp);
1034 if (stacklen > OVL_MAX_STACK) {
1035 pr_err("overlayfs: too many lower directries, limit is %d\n",
1036 OVL_MAX_STACK);
1037 goto out_free_lowertmp;
1038 } else if (!ufs->config.upperdir && stacklen == 1) {
1039 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1040 goto out_free_lowertmp;
1041 }
1042
1043 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1044 if (!stack)
1045 goto out_free_lowertmp;
1046
1047 lower = lowertmp;
1048 for (numlower = 0; numlower < stacklen; numlower++) {
1049 err = ovl_lower_dir(lower, &stack[numlower],
1050 &ufs->lower_namelen, &sb->s_stack_depth,
1051 &remote);
1052 if (err)
1053 goto out_put_lowerpath;
1054
1055 lower = strchr(lower, '\0') + 1;
1056 }
1057
1058 err = -EINVAL;
1059 sb->s_stack_depth++;
1060 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1061 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1062 goto out_put_lowerpath;
1063 }
1064
1065 if (ufs->config.upperdir) {
1066 ufs->upper_mnt = clone_private_mount(&upperpath);
1067 err = PTR_ERR(ufs->upper_mnt);
1068 if (IS_ERR(ufs->upper_mnt)) {
1069 pr_err("overlayfs: failed to clone upperpath\n");
1070 goto out_put_lowerpath;
1071 }
1072
1073 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1074 err = PTR_ERR(ufs->workdir);
1075 if (IS_ERR(ufs->workdir)) {
1076 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1077 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1078 sb->s_flags |= MS_RDONLY;
1079 ufs->workdir = NULL;
1080 }
1081
1082 /*
1083 * Upper should support d_type, else whiteouts are visible.
1084 * Given workdir and upper are on same fs, we can do
1085 * iterate_dir() on workdir. This check requires successful
1086 * creation of workdir in previous step.
1087 */
1088 if (ufs->workdir) {
1089 err = ovl_check_d_type_supported(&workpath);
1090 if (err < 0)
1091 goto out_put_workdir;
1092
1093 if (!err) {
1094 pr_err("overlayfs: upper fs needs to support d_type.\n");
1095 err = -EINVAL;
1096 goto out_put_workdir;
1097 }
1098 }
1099 }
1100
1101 err = -ENOMEM;
1102 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1103 if (ufs->lower_mnt == NULL)
1104 goto out_put_workdir;
1105 for (i = 0; i < numlower; i++) {
1106 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1107
1108 err = PTR_ERR(mnt);
1109 if (IS_ERR(mnt)) {
1110 pr_err("overlayfs: failed to clone lowerpath\n");
1111 goto out_put_lower_mnt;
1112 }
1113 /*
1114 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1115 * will fail instead of modifying lower fs.
1116 */
1117 mnt->mnt_flags |= MNT_READONLY;
1118
1119 ufs->lower_mnt[ufs->numlower] = mnt;
1120 ufs->numlower++;
1121 }
1122
1123 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1124 if (!ufs->upper_mnt)
1125 sb->s_flags |= MS_RDONLY;
1126
1127 if (remote)
1128 sb->s_d_op = &ovl_reval_dentry_operations;
1129 else
1130 sb->s_d_op = &ovl_dentry_operations;
1131
1132 ufs->creator_cred = prepare_creds();
1133 if (!ufs->creator_cred)
1134 goto out_put_lower_mnt;
1135
1136 err = -ENOMEM;
1137 oe = ovl_alloc_entry(numlower);
1138 if (!oe)
1139 goto out_put_cred;
1140
1141 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
1142 if (!root_dentry)
1143 goto out_free_oe;
1144
1145 mntput(upperpath.mnt);
1146 for (i = 0; i < numlower; i++)
1147 mntput(stack[i].mnt);
1148 path_put(&workpath);
1149 kfree(lowertmp);
1150
1151 oe->__upperdentry = upperpath.dentry;
1152 for (i = 0; i < numlower; i++) {
1153 oe->lowerstack[i].dentry = stack[i].dentry;
1154 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1155 }
1156 kfree(stack);
1157
1158 root_dentry->d_fsdata = oe;
1159
1160 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1161 root_dentry->d_inode);
1162
1163 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1164 sb->s_op = &ovl_super_operations;
1165 sb->s_root = root_dentry;
1166 sb->s_fs_info = ufs;
1167
1168 return 0;
1169
1170 out_free_oe:
1171 kfree(oe);
1172 out_put_cred:
1173 put_cred(ufs->creator_cred);
1174 out_put_lower_mnt:
1175 for (i = 0; i < ufs->numlower; i++)
1176 mntput(ufs->lower_mnt[i]);
1177 kfree(ufs->lower_mnt);
1178 out_put_workdir:
1179 dput(ufs->workdir);
1180 mntput(ufs->upper_mnt);
1181 out_put_lowerpath:
1182 for (i = 0; i < numlower; i++)
1183 path_put(&stack[i]);
1184 kfree(stack);
1185 out_free_lowertmp:
1186 kfree(lowertmp);
1187 out_put_workpath:
1188 path_put(&workpath);
1189 out_put_upperpath:
1190 path_put(&upperpath);
1191 out_free_config:
1192 kfree(ufs->config.lowerdir);
1193 kfree(ufs->config.upperdir);
1194 kfree(ufs->config.workdir);
1195 kfree(ufs);
1196 out:
1197 return err;
1198 }
1199
1200 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1201 const char *dev_name, void *raw_data)
1202 {
1203 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1204 }
1205
1206 static struct file_system_type ovl_fs_type = {
1207 .owner = THIS_MODULE,
1208 .name = "overlay",
1209 .mount = ovl_mount,
1210 .kill_sb = kill_anon_super,
1211 };
1212 MODULE_ALIAS_FS("overlay");
1213
1214 static int __init ovl_init(void)
1215 {
1216 return register_filesystem(&ovl_fs_type);
1217 }
1218
1219 static void __exit ovl_exit(void)
1220 {
1221 unregister_filesystem(&ovl_fs_type);
1222 }
1223
1224 module_init(ovl_init);
1225 module_exit(ovl_exit);