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