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