313f773652fff87f2355bc02e09175a953b1acbb
[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 static void ovl_inode_init(struct inode *inode, struct inode *realinode,
149 bool is_upper)
150 {
151 WRITE_ONCE(inode->i_private, (unsigned long) realinode |
152 (is_upper ? OVL_ISUPPER_MASK : 0));
153 }
154
155 struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
156 bool is_upper)
157 {
158 if (is_upper) {
159 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
160
161 return ofs->upper_mnt;
162 } else {
163 return oe->numlower ? oe->lowerstack[0].mnt : NULL;
164 }
165 }
166
167 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
168 {
169 struct ovl_entry *oe = dentry->d_fsdata;
170
171 return oe->cache;
172 }
173
174 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
175 {
176 struct ovl_entry *oe = dentry->d_fsdata;
177
178 oe->cache = cache;
179 }
180
181 void ovl_path_lower(struct dentry *dentry, struct path *path)
182 {
183 struct ovl_entry *oe = dentry->d_fsdata;
184
185 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
186 }
187
188 int ovl_want_write(struct dentry *dentry)
189 {
190 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
191 return mnt_want_write(ofs->upper_mnt);
192 }
193
194 void ovl_drop_write(struct dentry *dentry)
195 {
196 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
197 mnt_drop_write(ofs->upper_mnt);
198 }
199
200 struct dentry *ovl_workdir(struct dentry *dentry)
201 {
202 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
203 return ofs->workdir;
204 }
205
206 bool ovl_dentry_is_opaque(struct dentry *dentry)
207 {
208 struct ovl_entry *oe = dentry->d_fsdata;
209 return oe->opaque;
210 }
211
212 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
213 {
214 struct ovl_entry *oe = dentry->d_fsdata;
215 oe->opaque = opaque;
216 }
217
218 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
219 {
220 struct ovl_entry *oe = dentry->d_fsdata;
221
222 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
223 WARN_ON(oe->__upperdentry);
224 /*
225 * Make sure upperdentry is consistent before making it visible to
226 * ovl_upperdentry_dereference().
227 */
228 smp_wmb();
229 oe->__upperdentry = upperdentry;
230 }
231
232 void ovl_inode_update(struct inode *inode, struct inode *upperinode)
233 {
234 WARN_ON(!upperinode);
235 WRITE_ONCE(inode->i_private,
236 (unsigned long) upperinode | OVL_ISUPPER_MASK);
237 }
238
239 void ovl_dentry_version_inc(struct dentry *dentry)
240 {
241 struct ovl_entry *oe = dentry->d_fsdata;
242
243 WARN_ON(!inode_is_locked(dentry->d_inode));
244 oe->version++;
245 }
246
247 u64 ovl_dentry_version_get(struct dentry *dentry)
248 {
249 struct ovl_entry *oe = dentry->d_fsdata;
250
251 WARN_ON(!inode_is_locked(dentry->d_inode));
252 return oe->version;
253 }
254
255 bool ovl_is_whiteout(struct dentry *dentry)
256 {
257 struct inode *inode = dentry->d_inode;
258
259 return inode && IS_WHITEOUT(inode);
260 }
261
262 const struct cred *ovl_override_creds(struct super_block *sb)
263 {
264 struct ovl_fs *ofs = sb->s_fs_info;
265
266 return override_creds(ofs->creator_cred);
267 }
268
269 static bool ovl_is_opaquedir(struct dentry *dentry)
270 {
271 int res;
272 char val;
273 struct inode *inode = dentry->d_inode;
274
275 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
276 return false;
277
278 res = inode->i_op->getxattr(dentry, inode, OVL_XATTR_OPAQUE, &val, 1);
279 if (res == 1 && val == 'y')
280 return true;
281
282 return false;
283 }
284
285 static void ovl_dentry_release(struct dentry *dentry)
286 {
287 struct ovl_entry *oe = dentry->d_fsdata;
288
289 if (oe) {
290 unsigned int i;
291
292 dput(oe->__upperdentry);
293 for (i = 0; i < oe->numlower; i++)
294 dput(oe->lowerstack[i].dentry);
295 kfree_rcu(oe, rcu);
296 }
297 }
298
299 static struct dentry *ovl_d_real(struct dentry *dentry,
300 const struct inode *inode,
301 unsigned int open_flags)
302 {
303 struct dentry *real;
304
305 if (d_is_dir(dentry)) {
306 if (!inode || inode == d_inode(dentry))
307 return dentry;
308 goto bug;
309 }
310
311 if (d_is_negative(dentry))
312 return dentry;
313
314 if (open_flags) {
315 int err = ovl_open_maybe_copy_up(dentry, open_flags);
316
317 if (err)
318 return ERR_PTR(err);
319 }
320
321 real = ovl_dentry_upper(dentry);
322 if (real && (!inode || inode == d_inode(real)))
323 return real;
324
325 real = ovl_dentry_lower(dentry);
326 if (!real)
327 goto bug;
328
329 if (!inode || inode == d_inode(real))
330 return real;
331
332 /* Handle recursion */
333 return d_real(real, inode, open_flags);
334 bug:
335 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
336 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
337 return dentry;
338 }
339
340 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
341 {
342 struct ovl_entry *oe = dentry->d_fsdata;
343 unsigned int i;
344 int ret = 1;
345
346 for (i = 0; i < oe->numlower; i++) {
347 struct dentry *d = oe->lowerstack[i].dentry;
348
349 if (d->d_flags & DCACHE_OP_REVALIDATE) {
350 ret = d->d_op->d_revalidate(d, flags);
351 if (ret < 0)
352 return ret;
353 if (!ret) {
354 if (!(flags & LOOKUP_RCU))
355 d_invalidate(d);
356 return -ESTALE;
357 }
358 }
359 }
360 return 1;
361 }
362
363 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
364 {
365 struct ovl_entry *oe = dentry->d_fsdata;
366 unsigned int i;
367 int ret = 1;
368
369 for (i = 0; i < oe->numlower; i++) {
370 struct dentry *d = oe->lowerstack[i].dentry;
371
372 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
373 ret = d->d_op->d_weak_revalidate(d, flags);
374 if (ret <= 0)
375 break;
376 }
377 }
378 return ret;
379 }
380
381 static const struct dentry_operations ovl_dentry_operations = {
382 .d_release = ovl_dentry_release,
383 .d_real = ovl_d_real,
384 };
385
386 static const struct dentry_operations ovl_reval_dentry_operations = {
387 .d_release = ovl_dentry_release,
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 super_block *ovl_sb,
419 struct dentry *dir,
420 struct qstr *name)
421 {
422 const struct cred *old_cred;
423 struct dentry *dentry;
424
425 old_cred = ovl_override_creds(ovl_sb);
426 dentry = lookup_one_len_unlocked(name->name, dir, name->len);
427 revert_creds(old_cred);
428
429 if (IS_ERR(dentry)) {
430 if (PTR_ERR(dentry) == -ENOENT)
431 dentry = NULL;
432 } else if (!dentry->d_inode) {
433 dput(dentry);
434 dentry = NULL;
435 } else if (ovl_dentry_weird(dentry)) {
436 dput(dentry);
437 /* Don't support traversing automounts and other weirdness */
438 dentry = ERR_PTR(-EREMOTE);
439 }
440 return dentry;
441 }
442
443 /*
444 * Returns next layer in stack starting from top.
445 * Returns -1 if this is the last layer.
446 */
447 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
448 {
449 struct ovl_entry *oe = dentry->d_fsdata;
450
451 BUG_ON(idx < 0);
452 if (idx == 0) {
453 ovl_path_upper(dentry, path);
454 if (path->dentry)
455 return oe->numlower ? 1 : -1;
456 idx++;
457 }
458 BUG_ON(idx > oe->numlower);
459 *path = oe->lowerstack[idx - 1];
460
461 return (idx < oe->numlower) ? idx + 1 : -1;
462 }
463
464 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
465 unsigned int flags)
466 {
467 struct ovl_entry *oe;
468 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
469 struct path *stack = NULL;
470 struct dentry *upperdir, *upperdentry = NULL;
471 unsigned int ctr = 0;
472 struct inode *inode = NULL;
473 bool upperopaque = false;
474 struct dentry *this, *prev = NULL;
475 unsigned int i;
476 int err;
477
478 upperdir = ovl_upperdentry_dereference(poe);
479 if (upperdir) {
480 this = ovl_lookup_real(dentry->d_sb, upperdir, &dentry->d_name);
481 err = PTR_ERR(this);
482 if (IS_ERR(this))
483 goto out;
484
485 if (this) {
486 if (unlikely(ovl_dentry_remote(this))) {
487 dput(this);
488 err = -EREMOTE;
489 goto out;
490 }
491 if (ovl_is_whiteout(this)) {
492 dput(this);
493 this = NULL;
494 upperopaque = true;
495 } else if (poe->numlower && ovl_is_opaquedir(this)) {
496 upperopaque = true;
497 }
498 }
499 upperdentry = prev = this;
500 }
501
502 if (!upperopaque && poe->numlower) {
503 err = -ENOMEM;
504 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
505 if (!stack)
506 goto out_put_upper;
507 }
508
509 for (i = 0; !upperopaque && i < poe->numlower; i++) {
510 bool opaque = false;
511 struct path lowerpath = poe->lowerstack[i];
512
513 this = ovl_lookup_real(dentry->d_sb,
514 lowerpath.dentry, &dentry->d_name);
515 err = PTR_ERR(this);
516 if (IS_ERR(this)) {
517 /*
518 * If it's positive, then treat ENAMETOOLONG as ENOENT.
519 */
520 if (err == -ENAMETOOLONG && (upperdentry || ctr))
521 continue;
522 goto out_put;
523 }
524 if (!this)
525 continue;
526 if (ovl_is_whiteout(this)) {
527 dput(this);
528 break;
529 }
530 /*
531 * Only makes sense to check opaque dir if this is not the
532 * lowermost layer.
533 */
534 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
535 opaque = true;
536
537 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
538 !S_ISDIR(this->d_inode->i_mode))) {
539 /*
540 * FIXME: check for upper-opaqueness maybe better done
541 * in remove code.
542 */
543 if (prev == upperdentry)
544 upperopaque = true;
545 dput(this);
546 break;
547 }
548 /*
549 * If this is a non-directory then stop here.
550 */
551 if (!S_ISDIR(this->d_inode->i_mode))
552 opaque = true;
553
554 stack[ctr].dentry = this;
555 stack[ctr].mnt = lowerpath.mnt;
556 ctr++;
557 prev = this;
558 if (opaque)
559 break;
560 }
561
562 oe = ovl_alloc_entry(ctr);
563 err = -ENOMEM;
564 if (!oe)
565 goto out_put;
566
567 if (upperdentry || ctr) {
568 struct dentry *realdentry;
569 struct inode *realinode;
570
571 realdentry = upperdentry ? upperdentry : stack[0].dentry;
572 realinode = d_inode(realdentry);
573
574 err = -ENOMEM;
575 inode = ovl_new_inode(dentry->d_sb, realinode->i_mode);
576 if (!inode)
577 goto out_free_oe;
578 ovl_inode_init(inode, realinode, !!upperdentry);
579 ovl_copyattr(realdentry->d_inode, inode);
580 }
581
582 oe->opaque = upperopaque;
583 oe->__upperdentry = upperdentry;
584 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
585 kfree(stack);
586 dentry->d_fsdata = oe;
587 d_add(dentry, inode);
588
589 return NULL;
590
591 out_free_oe:
592 kfree(oe);
593 out_put:
594 for (i = 0; i < ctr; i++)
595 dput(stack[i].dentry);
596 kfree(stack);
597 out_put_upper:
598 dput(upperdentry);
599 out:
600 return ERR_PTR(err);
601 }
602
603 struct file *ovl_path_open(struct path *path, int flags)
604 {
605 return dentry_open(path, flags | O_NOATIME, current_cred());
606 }
607
608 static void ovl_put_super(struct super_block *sb)
609 {
610 struct ovl_fs *ufs = sb->s_fs_info;
611 unsigned i;
612
613 dput(ufs->workdir);
614 mntput(ufs->upper_mnt);
615 for (i = 0; i < ufs->numlower; i++)
616 mntput(ufs->lower_mnt[i]);
617 kfree(ufs->lower_mnt);
618
619 kfree(ufs->config.lowerdir);
620 kfree(ufs->config.upperdir);
621 kfree(ufs->config.workdir);
622 put_cred(ufs->creator_cred);
623 kfree(ufs);
624 }
625
626 /**
627 * ovl_statfs
628 * @sb: The overlayfs super block
629 * @buf: The struct kstatfs to fill in with stats
630 *
631 * Get the filesystem statistics. As writes always target the upper layer
632 * filesystem pass the statfs to the upper filesystem (if it exists)
633 */
634 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
635 {
636 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
637 struct dentry *root_dentry = dentry->d_sb->s_root;
638 struct path path;
639 int err;
640
641 ovl_path_real(root_dentry, &path);
642
643 err = vfs_statfs(&path, buf);
644 if (!err) {
645 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
646 buf->f_type = OVERLAYFS_SUPER_MAGIC;
647 }
648
649 return err;
650 }
651
652 /**
653 * ovl_show_options
654 *
655 * Prints the mount options for a given superblock.
656 * Returns zero; does not fail.
657 */
658 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
659 {
660 struct super_block *sb = dentry->d_sb;
661 struct ovl_fs *ufs = sb->s_fs_info;
662
663 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
664 if (ufs->config.upperdir) {
665 seq_show_option(m, "upperdir", ufs->config.upperdir);
666 seq_show_option(m, "workdir", ufs->config.workdir);
667 }
668 if (ufs->config.default_permissions)
669 seq_puts(m, ",default_permissions");
670 return 0;
671 }
672
673 static int ovl_remount(struct super_block *sb, int *flags, char *data)
674 {
675 struct ovl_fs *ufs = sb->s_fs_info;
676
677 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
678 return -EROFS;
679
680 return 0;
681 }
682
683 static const struct super_operations ovl_super_operations = {
684 .put_super = ovl_put_super,
685 .statfs = ovl_statfs,
686 .show_options = ovl_show_options,
687 .remount_fs = ovl_remount,
688 .drop_inode = generic_delete_inode,
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 inode *realinode;
967 struct ovl_entry *oe;
968 struct ovl_fs *ufs;
969 struct path *stack = NULL;
970 char *lowertmp;
971 char *lower;
972 unsigned int numlower;
973 unsigned int stacklen = 0;
974 unsigned int i;
975 bool remote = false;
976 int err;
977
978 err = -ENOMEM;
979 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
980 if (!ufs)
981 goto out;
982
983 err = ovl_parse_opt((char *) data, &ufs->config);
984 if (err)
985 goto out_free_config;
986
987 err = -EINVAL;
988 if (!ufs->config.lowerdir) {
989 if (!silent)
990 pr_err("overlayfs: missing 'lowerdir'\n");
991 goto out_free_config;
992 }
993
994 sb->s_stack_depth = 0;
995 sb->s_maxbytes = MAX_LFS_FILESIZE;
996 if (ufs->config.upperdir) {
997 if (!ufs->config.workdir) {
998 pr_err("overlayfs: missing 'workdir'\n");
999 goto out_free_config;
1000 }
1001
1002 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
1003 if (err)
1004 goto out_free_config;
1005
1006 /* Upper fs should not be r/o */
1007 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
1008 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1009 err = -EINVAL;
1010 goto out_put_upperpath;
1011 }
1012
1013 err = ovl_mount_dir(ufs->config.workdir, &workpath);
1014 if (err)
1015 goto out_put_upperpath;
1016
1017 err = -EINVAL;
1018 if (upperpath.mnt != workpath.mnt) {
1019 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1020 goto out_put_workpath;
1021 }
1022 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1023 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1024 goto out_put_workpath;
1025 }
1026 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
1027 }
1028 err = -ENOMEM;
1029 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1030 if (!lowertmp)
1031 goto out_put_workpath;
1032
1033 err = -EINVAL;
1034 stacklen = ovl_split_lowerdirs(lowertmp);
1035 if (stacklen > OVL_MAX_STACK) {
1036 pr_err("overlayfs: too many lower directries, limit is %d\n",
1037 OVL_MAX_STACK);
1038 goto out_free_lowertmp;
1039 } else if (!ufs->config.upperdir && stacklen == 1) {
1040 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1041 goto out_free_lowertmp;
1042 }
1043
1044 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1045 if (!stack)
1046 goto out_free_lowertmp;
1047
1048 lower = lowertmp;
1049 for (numlower = 0; numlower < stacklen; numlower++) {
1050 err = ovl_lower_dir(lower, &stack[numlower],
1051 &ufs->lower_namelen, &sb->s_stack_depth,
1052 &remote);
1053 if (err)
1054 goto out_put_lowerpath;
1055
1056 lower = strchr(lower, '\0') + 1;
1057 }
1058
1059 err = -EINVAL;
1060 sb->s_stack_depth++;
1061 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1062 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1063 goto out_put_lowerpath;
1064 }
1065
1066 if (ufs->config.upperdir) {
1067 ufs->upper_mnt = clone_private_mount(&upperpath);
1068 err = PTR_ERR(ufs->upper_mnt);
1069 if (IS_ERR(ufs->upper_mnt)) {
1070 pr_err("overlayfs: failed to clone upperpath\n");
1071 goto out_put_lowerpath;
1072 }
1073 /* Don't inherit atime flags */
1074 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1075
1076 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
1077
1078 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1079 err = PTR_ERR(ufs->workdir);
1080 if (IS_ERR(ufs->workdir)) {
1081 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1082 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1083 sb->s_flags |= MS_RDONLY;
1084 ufs->workdir = NULL;
1085 }
1086
1087 /*
1088 * Upper should support d_type, else whiteouts are visible.
1089 * Given workdir and upper are on same fs, we can do
1090 * iterate_dir() on workdir. This check requires successful
1091 * creation of workdir in previous step.
1092 */
1093 if (ufs->workdir) {
1094 err = ovl_check_d_type_supported(&workpath);
1095 if (err < 0)
1096 goto out_put_workdir;
1097
1098 /*
1099 * We allowed this configuration and don't want to
1100 * break users over kernel upgrade. So warn instead
1101 * of erroring out.
1102 */
1103 if (!err)
1104 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1105 }
1106 }
1107
1108 err = -ENOMEM;
1109 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1110 if (ufs->lower_mnt == NULL)
1111 goto out_put_workdir;
1112 for (i = 0; i < numlower; i++) {
1113 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1114
1115 err = PTR_ERR(mnt);
1116 if (IS_ERR(mnt)) {
1117 pr_err("overlayfs: failed to clone lowerpath\n");
1118 goto out_put_lower_mnt;
1119 }
1120 /*
1121 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1122 * will fail instead of modifying lower fs.
1123 */
1124 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1125
1126 ufs->lower_mnt[ufs->numlower] = mnt;
1127 ufs->numlower++;
1128 }
1129
1130 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1131 if (!ufs->upper_mnt)
1132 sb->s_flags |= MS_RDONLY;
1133
1134 if (remote)
1135 sb->s_d_op = &ovl_reval_dentry_operations;
1136 else
1137 sb->s_d_op = &ovl_dentry_operations;
1138
1139 ufs->creator_cred = prepare_creds();
1140 if (!ufs->creator_cred)
1141 goto out_put_lower_mnt;
1142
1143 err = -ENOMEM;
1144 oe = ovl_alloc_entry(numlower);
1145 if (!oe)
1146 goto out_put_cred;
1147
1148 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR));
1149 if (!root_dentry)
1150 goto out_free_oe;
1151
1152 mntput(upperpath.mnt);
1153 for (i = 0; i < numlower; i++)
1154 mntput(stack[i].mnt);
1155 path_put(&workpath);
1156 kfree(lowertmp);
1157
1158 oe->__upperdentry = upperpath.dentry;
1159 for (i = 0; i < numlower; i++) {
1160 oe->lowerstack[i].dentry = stack[i].dentry;
1161 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1162 }
1163 kfree(stack);
1164
1165 root_dentry->d_fsdata = oe;
1166
1167 realinode = d_inode(ovl_dentry_real(root_dentry));
1168 ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry);
1169 ovl_copyattr(realinode, d_inode(root_dentry));
1170
1171 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1172 sb->s_op = &ovl_super_operations;
1173 sb->s_root = root_dentry;
1174 sb->s_fs_info = ufs;
1175 sb->s_flags |= MS_POSIXACL;
1176
1177 return 0;
1178
1179 out_free_oe:
1180 kfree(oe);
1181 out_put_cred:
1182 put_cred(ufs->creator_cred);
1183 out_put_lower_mnt:
1184 for (i = 0; i < ufs->numlower; i++)
1185 mntput(ufs->lower_mnt[i]);
1186 kfree(ufs->lower_mnt);
1187 out_put_workdir:
1188 dput(ufs->workdir);
1189 mntput(ufs->upper_mnt);
1190 out_put_lowerpath:
1191 for (i = 0; i < numlower; i++)
1192 path_put(&stack[i]);
1193 kfree(stack);
1194 out_free_lowertmp:
1195 kfree(lowertmp);
1196 out_put_workpath:
1197 path_put(&workpath);
1198 out_put_upperpath:
1199 path_put(&upperpath);
1200 out_free_config:
1201 kfree(ufs->config.lowerdir);
1202 kfree(ufs->config.upperdir);
1203 kfree(ufs->config.workdir);
1204 kfree(ufs);
1205 out:
1206 return err;
1207 }
1208
1209 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1210 const char *dev_name, void *raw_data)
1211 {
1212 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1213 }
1214
1215 static struct file_system_type ovl_fs_type = {
1216 .owner = THIS_MODULE,
1217 .name = "overlay",
1218 .mount = ovl_mount,
1219 .kill_sb = kill_anon_super,
1220 };
1221 MODULE_ALIAS_FS("overlay");
1222
1223 static int __init ovl_init(void)
1224 {
1225 return register_filesystem(&ovl_fs_type);
1226 }
1227
1228 static void __exit ovl_exit(void)
1229 {
1230 unregister_filesystem(&ovl_fs_type);
1231 }
1232
1233 module_init(ovl_init);
1234 module_exit(ovl_exit);
This page took 0.05379 seconds and 4 git commands to generate.