• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2011 Novell Inc.
5  */
6 
7 #include <uapi/linux/magic.h>
8 #include <linux/fs.h>
9 #include <linux/namei.h>
10 #include <linux/xattr.h>
11 #include <linux/mount.h>
12 #include <linux/parser.h>
13 #include <linux/module.h>
14 #include <linux/statfs.h>
15 #include <linux/seq_file.h>
16 #include <linux/posix_acl_xattr.h>
17 #include <linux/exportfs.h>
18 #include "overlayfs.h"
19 
20 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
21 MODULE_DESCRIPTION("Overlay filesystem");
22 MODULE_LICENSE("GPL");
23 
24 
25 struct ovl_dir_cache;
26 
27 #define OVL_MAX_STACK 500
28 
29 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
30 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
31 MODULE_PARM_DESC(redirect_dir,
32 		 "Default to on or off for the redirect_dir feature");
33 
34 static bool ovl_redirect_always_follow =
35 	IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
36 module_param_named(redirect_always_follow, ovl_redirect_always_follow,
37 		   bool, 0644);
38 MODULE_PARM_DESC(redirect_always_follow,
39 		 "Follow redirects even if redirect_dir feature is turned off");
40 
41 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
42 module_param_named(index, ovl_index_def, bool, 0644);
43 MODULE_PARM_DESC(index,
44 		 "Default to on or off for the inodes index feature");
45 
46 static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
47 module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
48 MODULE_PARM_DESC(nfs_export,
49 		 "Default to on or off for the NFS export feature");
50 
51 static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
52 module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
53 MODULE_PARM_DESC(xino_auto,
54 		 "Auto enable xino feature");
55 
ovl_entry_stack_free(struct ovl_entry * oe)56 static void ovl_entry_stack_free(struct ovl_entry *oe)
57 {
58 	unsigned int i;
59 
60 	for (i = 0; i < oe->numlower; i++)
61 		dput(oe->lowerstack[i].dentry);
62 }
63 
64 static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
65 module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
66 MODULE_PARM_DESC(metacopy,
67 		 "Default to on or off for the metadata only copy up feature");
68 
ovl_dentry_release(struct dentry * dentry)69 static void ovl_dentry_release(struct dentry *dentry)
70 {
71 	struct ovl_entry *oe = dentry->d_fsdata;
72 
73 	if (oe) {
74 		ovl_entry_stack_free(oe);
75 		kfree_rcu(oe, rcu);
76 	}
77 }
78 
ovl_d_real(struct dentry * dentry,const struct inode * inode)79 static struct dentry *ovl_d_real(struct dentry *dentry,
80 				 const struct inode *inode)
81 {
82 	struct dentry *real = NULL, *lower;
83 
84 	/* It's an overlay file */
85 	if (inode && d_inode(dentry) == inode)
86 		return dentry;
87 
88 	if (!d_is_reg(dentry)) {
89 		if (!inode || inode == d_inode(dentry))
90 			return dentry;
91 		goto bug;
92 	}
93 
94 	real = ovl_dentry_upper(dentry);
95 	if (real && (inode == d_inode(real)))
96 		return real;
97 
98 	if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
99 		return real;
100 
101 	lower = ovl_dentry_lowerdata(dentry);
102 	if (!lower)
103 		goto bug;
104 	real = lower;
105 
106 	/* Handle recursion */
107 	real = d_real(real, inode);
108 
109 	if (!inode || inode == d_inode(real))
110 		return real;
111 bug:
112 	WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n",
113 	     __func__, dentry, inode ? inode->i_sb->s_id : "NULL",
114 	     inode ? inode->i_ino : 0, real,
115 	     real && d_inode(real) ? d_inode(real)->i_ino : 0);
116 	return dentry;
117 }
118 
ovl_revalidate_real(struct dentry * d,unsigned int flags,bool weak)119 static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
120 {
121 	int ret = 1;
122 
123 	if (weak) {
124 		if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
125 			ret =  d->d_op->d_weak_revalidate(d, flags);
126 	} else if (d->d_flags & DCACHE_OP_REVALIDATE) {
127 		ret = d->d_op->d_revalidate(d, flags);
128 		if (!ret) {
129 			if (!(flags & LOOKUP_RCU))
130 				d_invalidate(d);
131 			ret = -ESTALE;
132 		}
133 	}
134 	return ret;
135 }
136 
ovl_dentry_revalidate_common(struct dentry * dentry,unsigned int flags,bool weak)137 static int ovl_dentry_revalidate_common(struct dentry *dentry,
138 					unsigned int flags, bool weak)
139 {
140 	struct ovl_entry *oe = dentry->d_fsdata;
141 	struct dentry *upper;
142 	unsigned int i;
143 	int ret = 1;
144 
145 	upper = ovl_dentry_upper(dentry);
146 	if (upper)
147 		ret = ovl_revalidate_real(upper, flags, weak);
148 
149 	for (i = 0; ret > 0 && i < oe->numlower; i++) {
150 		ret = ovl_revalidate_real(oe->lowerstack[i].dentry, flags,
151 					  weak);
152 	}
153 	return ret;
154 }
155 
ovl_dentry_revalidate(struct dentry * dentry,unsigned int flags)156 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
157 {
158 	return ovl_dentry_revalidate_common(dentry, flags, false);
159 }
160 
ovl_dentry_weak_revalidate(struct dentry * dentry,unsigned int flags)161 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
162 {
163 	return ovl_dentry_revalidate_common(dentry, flags, true);
164 }
165 
166 static const struct dentry_operations ovl_dentry_operations = {
167 	.d_release = ovl_dentry_release,
168 	.d_real = ovl_d_real,
169 	.d_revalidate = ovl_dentry_revalidate,
170 	.d_weak_revalidate = ovl_dentry_weak_revalidate,
171 };
172 
173 static struct kmem_cache *ovl_inode_cachep;
174 
ovl_alloc_inode(struct super_block * sb)175 static struct inode *ovl_alloc_inode(struct super_block *sb)
176 {
177 	struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
178 
179 	if (!oi)
180 		return NULL;
181 
182 	oi->cache = NULL;
183 	oi->redirect = NULL;
184 	oi->version = 0;
185 	oi->flags = 0;
186 	oi->__upperdentry = NULL;
187 	oi->lower = NULL;
188 	oi->lowerdata = NULL;
189 	mutex_init(&oi->lock);
190 
191 	return &oi->vfs_inode;
192 }
193 
ovl_free_inode(struct inode * inode)194 static void ovl_free_inode(struct inode *inode)
195 {
196 	struct ovl_inode *oi = OVL_I(inode);
197 
198 	kfree(oi->redirect);
199 	mutex_destroy(&oi->lock);
200 	kmem_cache_free(ovl_inode_cachep, oi);
201 }
202 
ovl_destroy_inode(struct inode * inode)203 static void ovl_destroy_inode(struct inode *inode)
204 {
205 	struct ovl_inode *oi = OVL_I(inode);
206 
207 	dput(oi->__upperdentry);
208 	iput(oi->lower);
209 	if (S_ISDIR(inode->i_mode))
210 		ovl_dir_cache_free(inode);
211 	else
212 		iput(oi->lowerdata);
213 }
214 
ovl_free_fs(struct ovl_fs * ofs)215 static void ovl_free_fs(struct ovl_fs *ofs)
216 {
217 	struct vfsmount **mounts;
218 	unsigned i;
219 
220 	iput(ofs->workbasedir_trap);
221 	iput(ofs->indexdir_trap);
222 	iput(ofs->workdir_trap);
223 	dput(ofs->whiteout);
224 	dput(ofs->indexdir);
225 	dput(ofs->workdir);
226 	if (ofs->workdir_locked)
227 		ovl_inuse_unlock(ofs->workbasedir);
228 	dput(ofs->workbasedir);
229 	if (ofs->upperdir_locked)
230 		ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root);
231 
232 	/* Hack!  Reuse ofs->layers as a vfsmount array before freeing it */
233 	mounts = (struct vfsmount **) ofs->layers;
234 	for (i = 0; i < ofs->numlayer; i++) {
235 		iput(ofs->layers[i].trap);
236 		mounts[i] = ofs->layers[i].mnt;
237 	}
238 	kern_unmount_array(mounts, ofs->numlayer);
239 	kfree(ofs->layers);
240 	for (i = 0; i < ofs->numfs; i++)
241 		free_anon_bdev(ofs->fs[i].pseudo_dev);
242 	kfree(ofs->fs);
243 
244 	kfree(ofs->config.lowerdir);
245 	kfree(ofs->config.upperdir);
246 	kfree(ofs->config.workdir);
247 	kfree(ofs->config.redirect_mode);
248 	if (ofs->creator_cred)
249 		put_cred(ofs->creator_cred);
250 	kfree(ofs);
251 }
252 
ovl_put_super(struct super_block * sb)253 static void ovl_put_super(struct super_block *sb)
254 {
255 	struct ovl_fs *ofs = sb->s_fs_info;
256 
257 	ovl_free_fs(ofs);
258 }
259 
260 /* Sync real dirty inodes in upper filesystem (if it exists) */
ovl_sync_fs(struct super_block * sb,int wait)261 static int ovl_sync_fs(struct super_block *sb, int wait)
262 {
263 	struct ovl_fs *ofs = sb->s_fs_info;
264 	struct super_block *upper_sb;
265 	int ret;
266 
267 	ret = ovl_sync_status(ofs);
268 	/*
269 	 * We have to always set the err, because the return value isn't
270 	 * checked in syncfs, and instead indirectly return an error via
271 	 * the sb's writeback errseq, which VFS inspects after this call.
272 	 */
273 	if (ret < 0) {
274 		errseq_set(&sb->s_wb_err, -EIO);
275 		return -EIO;
276 	}
277 
278 	if (!ret)
279 		return ret;
280 
281 	/*
282 	 * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
283 	 * All the super blocks will be iterated, including upper_sb.
284 	 *
285 	 * If this is a syncfs(2) call, then we do need to call
286 	 * sync_filesystem() on upper_sb, but enough if we do it when being
287 	 * called with wait == 1.
288 	 */
289 	if (!wait)
290 		return 0;
291 
292 	upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
293 
294 	down_read(&upper_sb->s_umount);
295 	ret = sync_filesystem(upper_sb);
296 	up_read(&upper_sb->s_umount);
297 
298 	return ret;
299 }
300 
301 /**
302  * ovl_statfs
303  * @sb: The overlayfs super block
304  * @buf: The struct kstatfs to fill in with stats
305  *
306  * Get the filesystem statistics.  As writes always target the upper layer
307  * filesystem pass the statfs to the upper filesystem (if it exists)
308  */
ovl_statfs(struct dentry * dentry,struct kstatfs * buf)309 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
310 {
311 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
312 	struct dentry *root_dentry = dentry->d_sb->s_root;
313 	struct path path;
314 	int err;
315 
316 	ovl_path_real(root_dentry, &path);
317 
318 	err = vfs_statfs(&path, buf);
319 	if (!err) {
320 		buf->f_namelen = ofs->namelen;
321 		buf->f_type = OVERLAYFS_SUPER_MAGIC;
322 	}
323 
324 	return err;
325 }
326 
327 /* Will this overlay be forced to mount/remount ro? */
ovl_force_readonly(struct ovl_fs * ofs)328 static bool ovl_force_readonly(struct ovl_fs *ofs)
329 {
330 	return (!ovl_upper_mnt(ofs) || !ofs->workdir);
331 }
332 
ovl_redirect_mode_def(void)333 static const char *ovl_redirect_mode_def(void)
334 {
335 	return ovl_redirect_dir_def ? "on" : "off";
336 }
337 
338 static const char * const ovl_xino_str[] = {
339 	"off",
340 	"auto",
341 	"on",
342 };
343 
ovl_xino_def(void)344 static inline int ovl_xino_def(void)
345 {
346 	return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
347 }
348 
349 /**
350  * ovl_show_options
351  *
352  * Prints the mount options for a given superblock.
353  * Returns zero; does not fail.
354  */
ovl_show_options(struct seq_file * m,struct dentry * dentry)355 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
356 {
357 	struct super_block *sb = dentry->d_sb;
358 	struct ovl_fs *ofs = sb->s_fs_info;
359 
360 	seq_show_option(m, "lowerdir", ofs->config.lowerdir);
361 	if (ofs->config.upperdir) {
362 		seq_show_option(m, "upperdir", ofs->config.upperdir);
363 		seq_show_option(m, "workdir", ofs->config.workdir);
364 	}
365 	if (ofs->config.default_permissions)
366 		seq_puts(m, ",default_permissions");
367 	if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
368 		seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
369 	if (ofs->config.index != ovl_index_def)
370 		seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
371 	if (ofs->config.nfs_export != ovl_nfs_export_def)
372 		seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
373 						"on" : "off");
374 	if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(sb))
375 		seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
376 	if (ofs->config.metacopy != ovl_metacopy_def)
377 		seq_printf(m, ",metacopy=%s",
378 			   ofs->config.metacopy ? "on" : "off");
379 	if (ofs->config.ovl_volatile)
380 		seq_puts(m, ",volatile");
381 	return 0;
382 }
383 
ovl_remount(struct super_block * sb,int * flags,char * data)384 static int ovl_remount(struct super_block *sb, int *flags, char *data)
385 {
386 	struct ovl_fs *ofs = sb->s_fs_info;
387 	struct super_block *upper_sb;
388 	int ret = 0;
389 
390 	if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
391 		return -EROFS;
392 
393 	if (*flags & SB_RDONLY && !sb_rdonly(sb)) {
394 		upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
395 		if (ovl_should_sync(ofs)) {
396 			down_read(&upper_sb->s_umount);
397 			ret = sync_filesystem(upper_sb);
398 			up_read(&upper_sb->s_umount);
399 		}
400 	}
401 
402 	return ret;
403 }
404 
405 static const struct super_operations ovl_super_operations = {
406 	.alloc_inode	= ovl_alloc_inode,
407 	.free_inode	= ovl_free_inode,
408 	.destroy_inode	= ovl_destroy_inode,
409 	.drop_inode	= generic_delete_inode,
410 	.put_super	= ovl_put_super,
411 	.sync_fs	= ovl_sync_fs,
412 	.statfs		= ovl_statfs,
413 	.show_options	= ovl_show_options,
414 	.remount_fs	= ovl_remount,
415 };
416 
417 enum {
418 	OPT_LOWERDIR,
419 	OPT_UPPERDIR,
420 	OPT_WORKDIR,
421 	OPT_DEFAULT_PERMISSIONS,
422 	OPT_REDIRECT_DIR,
423 	OPT_INDEX_ON,
424 	OPT_INDEX_OFF,
425 	OPT_NFS_EXPORT_ON,
426 	OPT_NFS_EXPORT_OFF,
427 	OPT_XINO_ON,
428 	OPT_XINO_OFF,
429 	OPT_XINO_AUTO,
430 	OPT_METACOPY_ON,
431 	OPT_METACOPY_OFF,
432 	OPT_VOLATILE,
433 	OPT_ERR,
434 };
435 
436 static const match_table_t ovl_tokens = {
437 	{OPT_LOWERDIR,			"lowerdir=%s"},
438 	{OPT_UPPERDIR,			"upperdir=%s"},
439 	{OPT_WORKDIR,			"workdir=%s"},
440 	{OPT_DEFAULT_PERMISSIONS,	"default_permissions"},
441 	{OPT_REDIRECT_DIR,		"redirect_dir=%s"},
442 	{OPT_INDEX_ON,			"index=on"},
443 	{OPT_INDEX_OFF,			"index=off"},
444 	{OPT_NFS_EXPORT_ON,		"nfs_export=on"},
445 	{OPT_NFS_EXPORT_OFF,		"nfs_export=off"},
446 	{OPT_XINO_ON,			"xino=on"},
447 	{OPT_XINO_OFF,			"xino=off"},
448 	{OPT_XINO_AUTO,			"xino=auto"},
449 	{OPT_METACOPY_ON,		"metacopy=on"},
450 	{OPT_METACOPY_OFF,		"metacopy=off"},
451 	{OPT_VOLATILE,			"volatile"},
452 	{OPT_ERR,			NULL}
453 };
454 
ovl_next_opt(char ** s)455 static char *ovl_next_opt(char **s)
456 {
457 	char *sbegin = *s;
458 	char *p;
459 
460 	if (sbegin == NULL)
461 		return NULL;
462 
463 	for (p = sbegin; *p; p++) {
464 		if (*p == '\\') {
465 			p++;
466 			if (!*p)
467 				break;
468 		} else if (*p == ',') {
469 			*p = '\0';
470 			*s = p + 1;
471 			return sbegin;
472 		}
473 	}
474 	*s = NULL;
475 	return sbegin;
476 }
477 
ovl_parse_redirect_mode(struct ovl_config * config,const char * mode)478 static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
479 {
480 	if (strcmp(mode, "on") == 0) {
481 		config->redirect_dir = true;
482 		/*
483 		 * Does not make sense to have redirect creation without
484 		 * redirect following.
485 		 */
486 		config->redirect_follow = true;
487 	} else if (strcmp(mode, "follow") == 0) {
488 		config->redirect_follow = true;
489 	} else if (strcmp(mode, "off") == 0) {
490 		if (ovl_redirect_always_follow)
491 			config->redirect_follow = true;
492 	} else if (strcmp(mode, "nofollow") != 0) {
493 		pr_err("bad mount option \"redirect_dir=%s\"\n",
494 		       mode);
495 		return -EINVAL;
496 	}
497 
498 	return 0;
499 }
500 
ovl_parse_opt(char * opt,struct ovl_config * config)501 static int ovl_parse_opt(char *opt, struct ovl_config *config)
502 {
503 	char *p;
504 	int err;
505 	bool metacopy_opt = false, redirect_opt = false;
506 	bool nfs_export_opt = false, index_opt = false;
507 
508 	config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
509 	if (!config->redirect_mode)
510 		return -ENOMEM;
511 
512 	while ((p = ovl_next_opt(&opt)) != NULL) {
513 		int token;
514 		substring_t args[MAX_OPT_ARGS];
515 
516 		if (!*p)
517 			continue;
518 
519 		token = match_token(p, ovl_tokens, args);
520 		switch (token) {
521 		case OPT_UPPERDIR:
522 			kfree(config->upperdir);
523 			config->upperdir = match_strdup(&args[0]);
524 			if (!config->upperdir)
525 				return -ENOMEM;
526 			break;
527 
528 		case OPT_LOWERDIR:
529 			kfree(config->lowerdir);
530 			config->lowerdir = match_strdup(&args[0]);
531 			if (!config->lowerdir)
532 				return -ENOMEM;
533 			break;
534 
535 		case OPT_WORKDIR:
536 			kfree(config->workdir);
537 			config->workdir = match_strdup(&args[0]);
538 			if (!config->workdir)
539 				return -ENOMEM;
540 			break;
541 
542 		case OPT_DEFAULT_PERMISSIONS:
543 			config->default_permissions = true;
544 			break;
545 
546 		case OPT_REDIRECT_DIR:
547 			kfree(config->redirect_mode);
548 			config->redirect_mode = match_strdup(&args[0]);
549 			if (!config->redirect_mode)
550 				return -ENOMEM;
551 			redirect_opt = true;
552 			break;
553 
554 		case OPT_INDEX_ON:
555 			config->index = true;
556 			index_opt = true;
557 			break;
558 
559 		case OPT_INDEX_OFF:
560 			config->index = false;
561 			index_opt = true;
562 			break;
563 
564 		case OPT_NFS_EXPORT_ON:
565 			config->nfs_export = true;
566 			nfs_export_opt = true;
567 			break;
568 
569 		case OPT_NFS_EXPORT_OFF:
570 			config->nfs_export = false;
571 			nfs_export_opt = true;
572 			break;
573 
574 		case OPT_XINO_ON:
575 			config->xino = OVL_XINO_ON;
576 			break;
577 
578 		case OPT_XINO_OFF:
579 			config->xino = OVL_XINO_OFF;
580 			break;
581 
582 		case OPT_XINO_AUTO:
583 			config->xino = OVL_XINO_AUTO;
584 			break;
585 
586 		case OPT_METACOPY_ON:
587 			config->metacopy = true;
588 			metacopy_opt = true;
589 			break;
590 
591 		case OPT_METACOPY_OFF:
592 			config->metacopy = false;
593 			metacopy_opt = true;
594 			break;
595 
596 		case OPT_VOLATILE:
597 			config->ovl_volatile = true;
598 			break;
599 
600 		default:
601 			pr_err("unrecognized mount option \"%s\" or missing value\n",
602 					p);
603 			return -EINVAL;
604 		}
605 	}
606 
607 	/* Workdir/index are useless in non-upper mount */
608 	if (!config->upperdir) {
609 		if (config->workdir) {
610 			pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
611 				config->workdir);
612 			kfree(config->workdir);
613 			config->workdir = NULL;
614 		}
615 		if (config->index && index_opt) {
616 			pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
617 			index_opt = false;
618 		}
619 		config->index = false;
620 	}
621 
622 	if (!config->upperdir && config->ovl_volatile) {
623 		pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
624 		config->ovl_volatile = false;
625 	}
626 
627 	err = ovl_parse_redirect_mode(config, config->redirect_mode);
628 	if (err)
629 		return err;
630 
631 	/*
632 	 * This is to make the logic below simpler.  It doesn't make any other
633 	 * difference, since config->redirect_dir is only used for upper.
634 	 */
635 	if (!config->upperdir && config->redirect_follow)
636 		config->redirect_dir = true;
637 
638 	/* Resolve metacopy -> redirect_dir dependency */
639 	if (config->metacopy && !config->redirect_dir) {
640 		if (metacopy_opt && redirect_opt) {
641 			pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
642 			       config->redirect_mode);
643 			return -EINVAL;
644 		}
645 		if (redirect_opt) {
646 			/*
647 			 * There was an explicit redirect_dir=... that resulted
648 			 * in this conflict.
649 			 */
650 			pr_info("disabling metacopy due to redirect_dir=%s\n",
651 				config->redirect_mode);
652 			config->metacopy = false;
653 		} else {
654 			/* Automatically enable redirect otherwise. */
655 			config->redirect_follow = config->redirect_dir = true;
656 		}
657 	}
658 
659 	/* Resolve nfs_export -> index dependency */
660 	if (config->nfs_export && !config->index) {
661 		if (!config->upperdir && config->redirect_follow) {
662 			pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
663 			config->nfs_export = false;
664 		} else if (nfs_export_opt && index_opt) {
665 			pr_err("conflicting options: nfs_export=on,index=off\n");
666 			return -EINVAL;
667 		} else if (index_opt) {
668 			/*
669 			 * There was an explicit index=off that resulted
670 			 * in this conflict.
671 			 */
672 			pr_info("disabling nfs_export due to index=off\n");
673 			config->nfs_export = false;
674 		} else {
675 			/* Automatically enable index otherwise. */
676 			config->index = true;
677 		}
678 	}
679 
680 	/* Resolve nfs_export -> !metacopy dependency */
681 	if (config->nfs_export && config->metacopy) {
682 		if (nfs_export_opt && metacopy_opt) {
683 			pr_err("conflicting options: nfs_export=on,metacopy=on\n");
684 			return -EINVAL;
685 		}
686 		if (metacopy_opt) {
687 			/*
688 			 * There was an explicit metacopy=on that resulted
689 			 * in this conflict.
690 			 */
691 			pr_info("disabling nfs_export due to metacopy=on\n");
692 			config->nfs_export = false;
693 		} else {
694 			/*
695 			 * There was an explicit nfs_export=on that resulted
696 			 * in this conflict.
697 			 */
698 			pr_info("disabling metacopy due to nfs_export=on\n");
699 			config->metacopy = false;
700 		}
701 	}
702 
703 	return 0;
704 }
705 
706 #define OVL_WORKDIR_NAME "work"
707 #define OVL_INDEXDIR_NAME "index"
708 
ovl_workdir_create(struct ovl_fs * ofs,const char * name,bool persist)709 static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
710 					 const char *name, bool persist)
711 {
712 	struct inode *dir =  ofs->workbasedir->d_inode;
713 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
714 	struct dentry *work;
715 	int err;
716 	bool retried = false;
717 
718 	inode_lock_nested(dir, I_MUTEX_PARENT);
719 retry:
720 	work = lookup_one_len(name, ofs->workbasedir, strlen(name));
721 
722 	if (!IS_ERR(work)) {
723 		struct iattr attr = {
724 			.ia_valid = ATTR_MODE,
725 			.ia_mode = S_IFDIR | 0,
726 		};
727 
728 		if (work->d_inode) {
729 			err = -EEXIST;
730 			if (retried)
731 				goto out_dput;
732 
733 			if (persist)
734 				goto out_unlock;
735 
736 			retried = true;
737 			err = ovl_workdir_cleanup(dir, mnt, work, 0);
738 			dput(work);
739 			if (err == -EINVAL) {
740 				work = ERR_PTR(err);
741 				goto out_unlock;
742 			}
743 			goto retry;
744 		}
745 
746 		err = ovl_mkdir_real(dir, &work, attr.ia_mode);
747 		if (err)
748 			goto out_dput;
749 
750 		/* Weird filesystem returning with hashed negative (kernfs)? */
751 		err = -EINVAL;
752 		if (d_really_is_negative(work))
753 			goto out_dput;
754 
755 		/*
756 		 * Try to remove POSIX ACL xattrs from workdir.  We are good if:
757 		 *
758 		 * a) success (there was a POSIX ACL xattr and was removed)
759 		 * b) -ENODATA (there was no POSIX ACL xattr)
760 		 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
761 		 *
762 		 * There are various other error values that could effectively
763 		 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
764 		 * if the xattr name is too long), but the set of filesystems
765 		 * allowed as upper are limited to "normal" ones, where checking
766 		 * for the above two errors is sufficient.
767 		 */
768 		err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
769 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
770 			goto out_dput;
771 
772 		err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
773 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
774 			goto out_dput;
775 
776 		/* Clear any inherited mode bits */
777 		inode_lock(work->d_inode);
778 		err = notify_change(work, &attr, NULL);
779 		inode_unlock(work->d_inode);
780 		if (err)
781 			goto out_dput;
782 	} else {
783 		err = PTR_ERR(work);
784 		goto out_err;
785 	}
786 out_unlock:
787 	inode_unlock(dir);
788 	return work;
789 
790 out_dput:
791 	dput(work);
792 out_err:
793 	pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
794 		ofs->config.workdir, name, -err);
795 	work = NULL;
796 	goto out_unlock;
797 }
798 
ovl_unescape(char * s)799 static void ovl_unescape(char *s)
800 {
801 	char *d = s;
802 
803 	for (;; s++, d++) {
804 		if (*s == '\\')
805 			s++;
806 		*d = *s;
807 		if (!*s)
808 			break;
809 	}
810 }
811 
ovl_mount_dir_noesc(const char * name,struct path * path)812 static int ovl_mount_dir_noesc(const char *name, struct path *path)
813 {
814 	int err = -EINVAL;
815 
816 	if (!*name) {
817 		pr_err("empty lowerdir\n");
818 		goto out;
819 	}
820 	err = kern_path(name, LOOKUP_FOLLOW, path);
821 	if (err) {
822 		pr_err("failed to resolve '%s': %i\n", name, err);
823 		goto out;
824 	}
825 	err = -EINVAL;
826 	if (ovl_dentry_weird(path->dentry)) {
827 		pr_err("filesystem on '%s' not supported\n", name);
828 		goto out_put;
829 	}
830 	if (!d_is_dir(path->dentry)) {
831 		pr_err("'%s' not a directory\n", name);
832 		goto out_put;
833 	}
834 	return 0;
835 
836 out_put:
837 	path_put_init(path);
838 out:
839 	return err;
840 }
841 
ovl_mount_dir(const char * name,struct path * path)842 static int ovl_mount_dir(const char *name, struct path *path)
843 {
844 	int err = -ENOMEM;
845 	char *tmp = kstrdup(name, GFP_KERNEL);
846 
847 	if (tmp) {
848 		ovl_unescape(tmp);
849 		err = ovl_mount_dir_noesc(tmp, path);
850 
851 		if (!err && path->dentry->d_flags & DCACHE_OP_REAL) {
852 			pr_err("filesystem on '%s' not supported as upperdir\n",
853 			       tmp);
854 			path_put_init(path);
855 			err = -EINVAL;
856 		}
857 		kfree(tmp);
858 	}
859 	return err;
860 }
861 
ovl_check_namelen(struct path * path,struct ovl_fs * ofs,const char * name)862 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
863 			     const char *name)
864 {
865 	struct kstatfs statfs;
866 	int err = vfs_statfs(path, &statfs);
867 
868 	if (err)
869 		pr_err("statfs failed on '%s'\n", name);
870 	else
871 		ofs->namelen = max(ofs->namelen, statfs.f_namelen);
872 
873 	return err;
874 }
875 
ovl_lower_dir(const char * name,struct path * path,struct ovl_fs * ofs,int * stack_depth)876 static int ovl_lower_dir(const char *name, struct path *path,
877 			 struct ovl_fs *ofs, int *stack_depth)
878 {
879 	int fh_type;
880 	int err;
881 
882 	err = ovl_mount_dir_noesc(name, path);
883 	if (err)
884 		return err;
885 
886 	err = ovl_check_namelen(path, ofs, name);
887 	if (err)
888 		return err;
889 
890 	*stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
891 
892 	/*
893 	 * The inodes index feature and NFS export need to encode and decode
894 	 * file handles, so they require that all layers support them.
895 	 */
896 	fh_type = ovl_can_decode_fh(path->dentry->d_sb);
897 	if ((ofs->config.nfs_export ||
898 	     (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
899 		ofs->config.index = false;
900 		ofs->config.nfs_export = false;
901 		pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
902 			name);
903 	}
904 
905 	/* Check if lower fs has 32bit inode numbers */
906 	if (fh_type != FILEID_INO32_GEN)
907 		ofs->xino_mode = -1;
908 
909 	return 0;
910 }
911 
912 /* Workdir should not be subdir of upperdir and vice versa */
ovl_workdir_ok(struct dentry * workdir,struct dentry * upperdir)913 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
914 {
915 	bool ok = false;
916 
917 	if (workdir != upperdir) {
918 		ok = (lock_rename(workdir, upperdir) == NULL);
919 		unlock_rename(workdir, upperdir);
920 	}
921 	return ok;
922 }
923 
ovl_split_lowerdirs(char * str)924 static unsigned int ovl_split_lowerdirs(char *str)
925 {
926 	unsigned int ctr = 1;
927 	char *s, *d;
928 
929 	for (s = d = str;; s++, d++) {
930 		if (*s == '\\') {
931 			s++;
932 		} else if (*s == ':') {
933 			*d = '\0';
934 			ctr++;
935 			continue;
936 		}
937 		*d = *s;
938 		if (!*s)
939 			break;
940 	}
941 	return ctr;
942 }
943 
944 static int __maybe_unused
ovl_posix_acl_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size)945 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
946 			struct dentry *dentry, struct inode *inode,
947 			const char *name, void *buffer, size_t size)
948 {
949 	return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
950 }
951 
952 static int __maybe_unused
ovl_posix_acl_xattr_set(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)953 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
954 			struct dentry *dentry, struct inode *inode,
955 			const char *name, const void *value,
956 			size_t size, int flags)
957 {
958 	struct dentry *workdir = ovl_workdir(dentry);
959 	struct inode *realinode = ovl_inode_real(inode);
960 	struct posix_acl *acl = NULL;
961 	int err;
962 
963 	/* Check that everything is OK before copy-up */
964 	if (value) {
965 		acl = posix_acl_from_xattr(&init_user_ns, value, size);
966 		if (IS_ERR(acl))
967 			return PTR_ERR(acl);
968 	}
969 	err = -EOPNOTSUPP;
970 	if (!IS_POSIXACL(d_inode(workdir)))
971 		goto out_acl_release;
972 	if (!realinode->i_op->set_acl)
973 		goto out_acl_release;
974 	if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
975 		err = acl ? -EACCES : 0;
976 		goto out_acl_release;
977 	}
978 	err = -EPERM;
979 	if (!inode_owner_or_capable(inode))
980 		goto out_acl_release;
981 
982 	posix_acl_release(acl);
983 
984 	/*
985 	 * Check if sgid bit needs to be cleared (actual setacl operation will
986 	 * be done with mounter's capabilities and so that won't do it for us).
987 	 */
988 	if (unlikely(inode->i_mode & S_ISGID) &&
989 	    handler->flags == ACL_TYPE_ACCESS &&
990 	    !in_group_p(inode->i_gid) &&
991 	    !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
992 		struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
993 
994 		err = ovl_setattr(dentry, &iattr);
995 		if (err)
996 			return err;
997 	}
998 
999 	err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
1000 	if (!err)
1001 		ovl_copyattr(ovl_inode_real(inode), inode);
1002 
1003 	return err;
1004 
1005 out_acl_release:
1006 	posix_acl_release(acl);
1007 	return err;
1008 }
1009 
ovl_own_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size)1010 static int ovl_own_xattr_get(const struct xattr_handler *handler,
1011 			     struct dentry *dentry, struct inode *inode,
1012 			     const char *name, void *buffer, size_t size)
1013 {
1014 	return -EOPNOTSUPP;
1015 }
1016 
ovl_own_xattr_set(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)1017 static int ovl_own_xattr_set(const struct xattr_handler *handler,
1018 			     struct dentry *dentry, struct inode *inode,
1019 			     const char *name, const void *value,
1020 			     size_t size, int flags)
1021 {
1022 	return -EOPNOTSUPP;
1023 }
1024 
ovl_other_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size)1025 static int ovl_other_xattr_get(const struct xattr_handler *handler,
1026 			       struct dentry *dentry, struct inode *inode,
1027 			       const char *name, void *buffer, size_t size)
1028 {
1029 	return ovl_xattr_get(dentry, inode, name, buffer, size);
1030 }
1031 
ovl_other_xattr_set(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)1032 static int ovl_other_xattr_set(const struct xattr_handler *handler,
1033 			       struct dentry *dentry, struct inode *inode,
1034 			       const char *name, const void *value,
1035 			       size_t size, int flags)
1036 {
1037 	return ovl_xattr_set(dentry, inode, name, value, size, flags);
1038 }
1039 
1040 static const struct xattr_handler __maybe_unused
1041 ovl_posix_acl_access_xattr_handler = {
1042 	.name = XATTR_NAME_POSIX_ACL_ACCESS,
1043 	.flags = ACL_TYPE_ACCESS,
1044 	.get = ovl_posix_acl_xattr_get,
1045 	.set = ovl_posix_acl_xattr_set,
1046 };
1047 
1048 static const struct xattr_handler __maybe_unused
1049 ovl_posix_acl_default_xattr_handler = {
1050 	.name = XATTR_NAME_POSIX_ACL_DEFAULT,
1051 	.flags = ACL_TYPE_DEFAULT,
1052 	.get = ovl_posix_acl_xattr_get,
1053 	.set = ovl_posix_acl_xattr_set,
1054 };
1055 
1056 static const struct xattr_handler ovl_own_xattr_handler = {
1057 	.prefix	= OVL_XATTR_PREFIX,
1058 	.get = ovl_own_xattr_get,
1059 	.set = ovl_own_xattr_set,
1060 };
1061 
1062 static const struct xattr_handler ovl_other_xattr_handler = {
1063 	.prefix	= "", /* catch all */
1064 	.get = ovl_other_xattr_get,
1065 	.set = ovl_other_xattr_set,
1066 };
1067 
1068 static const struct xattr_handler *ovl_xattr_handlers[] = {
1069 #ifdef CONFIG_FS_POSIX_ACL
1070 	&ovl_posix_acl_access_xattr_handler,
1071 	&ovl_posix_acl_default_xattr_handler,
1072 #endif
1073 	&ovl_own_xattr_handler,
1074 	&ovl_other_xattr_handler,
1075 	NULL
1076 };
1077 
ovl_setup_trap(struct super_block * sb,struct dentry * dir,struct inode ** ptrap,const char * name)1078 static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
1079 			  struct inode **ptrap, const char *name)
1080 {
1081 	struct inode *trap;
1082 	int err;
1083 
1084 	trap = ovl_get_trap_inode(sb, dir);
1085 	err = PTR_ERR_OR_ZERO(trap);
1086 	if (err) {
1087 		if (err == -ELOOP)
1088 			pr_err("conflicting %s path\n", name);
1089 		return err;
1090 	}
1091 
1092 	*ptrap = trap;
1093 	return 0;
1094 }
1095 
1096 /*
1097  * Determine how we treat concurrent use of upperdir/workdir based on the
1098  * index feature. This is papering over mount leaks of container runtimes,
1099  * for example, an old overlay mount is leaked and now its upperdir is
1100  * attempted to be used as a lower layer in a new overlay mount.
1101  */
ovl_report_in_use(struct ovl_fs * ofs,const char * name)1102 static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
1103 {
1104 	if (ofs->config.index) {
1105 		pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
1106 		       name);
1107 		return -EBUSY;
1108 	} else {
1109 		pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
1110 			name);
1111 		return 0;
1112 	}
1113 }
1114 
ovl_get_upper(struct super_block * sb,struct ovl_fs * ofs,struct ovl_layer * upper_layer,struct path * upperpath)1115 static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
1116 			 struct ovl_layer *upper_layer, struct path *upperpath)
1117 {
1118 	struct vfsmount *upper_mnt;
1119 	int err;
1120 
1121 	err = ovl_mount_dir(ofs->config.upperdir, upperpath);
1122 	if (err)
1123 		goto out;
1124 
1125 	/* Upper fs should not be r/o */
1126 	if (sb_rdonly(upperpath->mnt->mnt_sb)) {
1127 		pr_err("upper fs is r/o, try multi-lower layers mount\n");
1128 		err = -EINVAL;
1129 		goto out;
1130 	}
1131 
1132 	err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
1133 	if (err)
1134 		goto out;
1135 
1136 	err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
1137 			     "upperdir");
1138 	if (err)
1139 		goto out;
1140 
1141 	upper_mnt = clone_private_mount(upperpath);
1142 	err = PTR_ERR(upper_mnt);
1143 	if (IS_ERR(upper_mnt)) {
1144 		pr_err("failed to clone upperpath\n");
1145 		goto out;
1146 	}
1147 
1148 	/* Don't inherit atime flags */
1149 	upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1150 	upper_layer->mnt = upper_mnt;
1151 	upper_layer->idx = 0;
1152 	upper_layer->fsid = 0;
1153 
1154 	/*
1155 	 * Inherit SB_NOSEC flag from upperdir.
1156 	 *
1157 	 * This optimization changes behavior when a security related attribute
1158 	 * (suid/sgid/security.*) is changed on an underlying layer.  This is
1159 	 * okay because we don't yet have guarantees in that case, but it will
1160 	 * need careful treatment once we want to honour changes to underlying
1161 	 * filesystems.
1162 	 */
1163 	if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
1164 		sb->s_flags |= SB_NOSEC;
1165 
1166 	if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
1167 		ofs->upperdir_locked = true;
1168 	} else {
1169 		err = ovl_report_in_use(ofs, "upperdir");
1170 		if (err)
1171 			goto out;
1172 	}
1173 
1174 	err = 0;
1175 out:
1176 	return err;
1177 }
1178 
1179 /*
1180  * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
1181  * negative values if error is encountered.
1182  */
ovl_check_rename_whiteout(struct dentry * workdir)1183 static int ovl_check_rename_whiteout(struct dentry *workdir)
1184 {
1185 	struct inode *dir = d_inode(workdir);
1186 	struct dentry *temp;
1187 	struct dentry *dest;
1188 	struct dentry *whiteout;
1189 	struct name_snapshot name;
1190 	int err;
1191 
1192 	inode_lock_nested(dir, I_MUTEX_PARENT);
1193 
1194 	temp = ovl_create_temp(workdir, OVL_CATTR(S_IFREG | 0));
1195 	err = PTR_ERR(temp);
1196 	if (IS_ERR(temp))
1197 		goto out_unlock;
1198 
1199 	dest = ovl_lookup_temp(workdir);
1200 	err = PTR_ERR(dest);
1201 	if (IS_ERR(dest)) {
1202 		dput(temp);
1203 		goto out_unlock;
1204 	}
1205 
1206 	/* Name is inline and stable - using snapshot as a copy helper */
1207 	take_dentry_name_snapshot(&name, temp);
1208 	err = ovl_do_rename(dir, temp, dir, dest, RENAME_WHITEOUT);
1209 	if (err) {
1210 		if (err == -EINVAL)
1211 			err = 0;
1212 		goto cleanup_temp;
1213 	}
1214 
1215 	whiteout = lookup_one_len(name.name.name, workdir, name.name.len);
1216 	err = PTR_ERR(whiteout);
1217 	if (IS_ERR(whiteout))
1218 		goto cleanup_temp;
1219 
1220 	err = ovl_is_whiteout(whiteout);
1221 
1222 	/* Best effort cleanup of whiteout and temp file */
1223 	if (err)
1224 		ovl_cleanup(dir, whiteout);
1225 	dput(whiteout);
1226 
1227 cleanup_temp:
1228 	ovl_cleanup(dir, temp);
1229 	release_dentry_name_snapshot(&name);
1230 	dput(temp);
1231 	dput(dest);
1232 
1233 out_unlock:
1234 	inode_unlock(dir);
1235 
1236 	return err;
1237 }
1238 
ovl_lookup_or_create(struct dentry * parent,const char * name,umode_t mode)1239 static struct dentry *ovl_lookup_or_create(struct dentry *parent,
1240 					   const char *name, umode_t mode)
1241 {
1242 	size_t len = strlen(name);
1243 	struct dentry *child;
1244 
1245 	inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
1246 	child = lookup_one_len(name, parent, len);
1247 	if (!IS_ERR(child) && !child->d_inode)
1248 		child = ovl_create_real(parent->d_inode, child,
1249 					OVL_CATTR(mode));
1250 	inode_unlock(parent->d_inode);
1251 	dput(parent);
1252 
1253 	return child;
1254 }
1255 
1256 /*
1257  * Creates $workdir/work/incompat/volatile/dirty file if it is not already
1258  * present.
1259  */
ovl_create_volatile_dirty(struct ovl_fs * ofs)1260 static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
1261 {
1262 	unsigned int ctr;
1263 	struct dentry *d = dget(ofs->workbasedir);
1264 	static const char *const volatile_path[] = {
1265 		OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
1266 	};
1267 	const char *const *name = volatile_path;
1268 
1269 	for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
1270 		d = ovl_lookup_or_create(d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
1271 		if (IS_ERR(d))
1272 			return PTR_ERR(d);
1273 	}
1274 	dput(d);
1275 	return 0;
1276 }
1277 
ovl_make_workdir(struct super_block * sb,struct ovl_fs * ofs,struct path * workpath)1278 static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1279 			    struct path *workpath)
1280 {
1281 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
1282 	struct dentry *temp, *workdir;
1283 	bool rename_whiteout;
1284 	bool d_type;
1285 	int fh_type;
1286 	int err;
1287 
1288 	err = mnt_want_write(mnt);
1289 	if (err)
1290 		return err;
1291 
1292 	workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1293 	err = PTR_ERR(workdir);
1294 	if (IS_ERR_OR_NULL(workdir))
1295 		goto out;
1296 
1297 	ofs->workdir = workdir;
1298 
1299 	err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1300 	if (err)
1301 		goto out;
1302 
1303 	/*
1304 	 * Upper should support d_type, else whiteouts are visible.  Given
1305 	 * workdir and upper are on same fs, we can do iterate_dir() on
1306 	 * workdir. This check requires successful creation of workdir in
1307 	 * previous step.
1308 	 */
1309 	err = ovl_check_d_type_supported(workpath);
1310 	if (err < 0)
1311 		goto out;
1312 
1313 	d_type = err;
1314 	if (!d_type)
1315 		pr_warn("upper fs needs to support d_type.\n");
1316 
1317 	/* Check if upper/work fs supports O_TMPFILE */
1318 	temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1319 	ofs->tmpfile = !IS_ERR(temp);
1320 	if (ofs->tmpfile)
1321 		dput(temp);
1322 	else
1323 		pr_warn("upper fs does not support tmpfile.\n");
1324 
1325 
1326 	/* Check if upper/work fs supports RENAME_WHITEOUT */
1327 	err = ovl_check_rename_whiteout(ofs->workdir);
1328 	if (err < 0)
1329 		goto out;
1330 
1331 	rename_whiteout = err;
1332 	if (!rename_whiteout)
1333 		pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
1334 
1335 	/*
1336 	 * Check if upper/work fs supports trusted.overlay.* xattr
1337 	 */
1338 	err = ovl_do_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
1339 	if (err) {
1340 		ofs->noxattr = true;
1341 		ofs->config.index = false;
1342 		ofs->config.metacopy = false;
1343 		pr_warn("upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
1344 		err = 0;
1345 	} else {
1346 		ovl_do_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
1347 	}
1348 
1349 	/*
1350 	 * We allowed sub-optimal upper fs configuration and don't want to break
1351 	 * users over kernel upgrade, but we never allowed remote upper fs, so
1352 	 * we can enforce strict requirements for remote upper fs.
1353 	 */
1354 	if (ovl_dentry_remote(ofs->workdir) &&
1355 	    (!d_type || !rename_whiteout || ofs->noxattr)) {
1356 		pr_err("upper fs missing required features.\n");
1357 		err = -EINVAL;
1358 		goto out;
1359 	}
1360 
1361 	/*
1362 	 * For volatile mount, create a incompat/volatile/dirty file to keep
1363 	 * track of it.
1364 	 */
1365 	if (ofs->config.ovl_volatile) {
1366 		err = ovl_create_volatile_dirty(ofs);
1367 		if (err < 0) {
1368 			pr_err("Failed to create volatile/dirty file.\n");
1369 			goto out;
1370 		}
1371 	}
1372 
1373 	/* Check if upper/work fs supports file handles */
1374 	fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1375 	if (ofs->config.index && !fh_type) {
1376 		ofs->config.index = false;
1377 		pr_warn("upper fs does not support file handles, falling back to index=off.\n");
1378 	}
1379 
1380 	/* Check if upper fs has 32bit inode numbers */
1381 	if (fh_type != FILEID_INO32_GEN)
1382 		ofs->xino_mode = -1;
1383 
1384 	/* NFS export of r/w mount depends on index */
1385 	if (ofs->config.nfs_export && !ofs->config.index) {
1386 		pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1387 		ofs->config.nfs_export = false;
1388 	}
1389 out:
1390 	mnt_drop_write(mnt);
1391 	return err;
1392 }
1393 
ovl_get_workdir(struct super_block * sb,struct ovl_fs * ofs,struct path * upperpath)1394 static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1395 			   struct path *upperpath)
1396 {
1397 	int err;
1398 	struct path workpath = { };
1399 
1400 	err = ovl_mount_dir(ofs->config.workdir, &workpath);
1401 	if (err)
1402 		goto out;
1403 
1404 	err = -EINVAL;
1405 	if (upperpath->mnt != workpath.mnt) {
1406 		pr_err("workdir and upperdir must reside under the same mount\n");
1407 		goto out;
1408 	}
1409 	if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
1410 		pr_err("workdir and upperdir must be separate subtrees\n");
1411 		goto out;
1412 	}
1413 
1414 	ofs->workbasedir = dget(workpath.dentry);
1415 
1416 	if (ovl_inuse_trylock(ofs->workbasedir)) {
1417 		ofs->workdir_locked = true;
1418 	} else {
1419 		err = ovl_report_in_use(ofs, "workdir");
1420 		if (err)
1421 			goto out;
1422 	}
1423 
1424 	err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
1425 			     "workdir");
1426 	if (err)
1427 		goto out;
1428 
1429 	err = ovl_make_workdir(sb, ofs, &workpath);
1430 
1431 out:
1432 	path_put(&workpath);
1433 
1434 	return err;
1435 }
1436 
ovl_get_indexdir(struct super_block * sb,struct ovl_fs * ofs,struct ovl_entry * oe,struct path * upperpath)1437 static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1438 			    struct ovl_entry *oe, struct path *upperpath)
1439 {
1440 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
1441 	struct dentry *indexdir;
1442 	int err;
1443 
1444 	err = mnt_want_write(mnt);
1445 	if (err)
1446 		return err;
1447 
1448 	/* Verify lower root is upper root origin */
1449 	err = ovl_verify_origin(ofs, upperpath->dentry,
1450 				oe->lowerstack[0].dentry, true);
1451 	if (err) {
1452 		pr_err("failed to verify upper root origin\n");
1453 		goto out;
1454 	}
1455 
1456 	/* index dir will act also as workdir */
1457 	iput(ofs->workdir_trap);
1458 	ofs->workdir_trap = NULL;
1459 	dput(ofs->workdir);
1460 	ofs->workdir = NULL;
1461 	indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1462 	if (IS_ERR(indexdir)) {
1463 		err = PTR_ERR(indexdir);
1464 	} else if (indexdir) {
1465 		ofs->indexdir = indexdir;
1466 		ofs->workdir = dget(indexdir);
1467 
1468 		err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1469 				     "indexdir");
1470 		if (err)
1471 			goto out;
1472 
1473 		/*
1474 		 * Verify upper root is exclusively associated with index dir.
1475 		 * Older kernels stored upper fh in "trusted.overlay.origin"
1476 		 * xattr. If that xattr exists, verify that it is a match to
1477 		 * upper dir file handle. In any case, verify or set xattr
1478 		 * "trusted.overlay.upper" to indicate that index may have
1479 		 * directory entries.
1480 		 */
1481 		if (ovl_check_origin_xattr(ofs, ofs->indexdir)) {
1482 			err = ovl_verify_set_fh(ofs, ofs->indexdir,
1483 						OVL_XATTR_ORIGIN,
1484 						upperpath->dentry, true, false);
1485 			if (err)
1486 				pr_err("failed to verify index dir 'origin' xattr\n");
1487 		}
1488 		err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry,
1489 				       true);
1490 		if (err)
1491 			pr_err("failed to verify index dir 'upper' xattr\n");
1492 
1493 		/* Cleanup bad/stale/orphan index entries */
1494 		if (!err)
1495 			err = ovl_indexdir_cleanup(ofs);
1496 	}
1497 	if (err || !ofs->indexdir)
1498 		pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1499 
1500 out:
1501 	mnt_drop_write(mnt);
1502 	return err;
1503 }
1504 
ovl_lower_uuid_ok(struct ovl_fs * ofs,const uuid_t * uuid)1505 static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
1506 {
1507 	unsigned int i;
1508 
1509 	if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
1510 		return true;
1511 
1512 	/*
1513 	 * We allow using single lower with null uuid for index and nfs_export
1514 	 * for example to support those features with single lower squashfs.
1515 	 * To avoid regressions in setups of overlay with re-formatted lower
1516 	 * squashfs, do not allow decoding origin with lower null uuid unless
1517 	 * user opted-in to one of the new features that require following the
1518 	 * lower inode of non-dir upper.
1519 	 */
1520 	if (!ofs->config.index && !ofs->config.metacopy && !ofs->config.xino &&
1521 	    uuid_is_null(uuid))
1522 		return false;
1523 
1524 	for (i = 0; i < ofs->numfs; i++) {
1525 		/*
1526 		 * We use uuid to associate an overlay lower file handle with a
1527 		 * lower layer, so we can accept lower fs with null uuid as long
1528 		 * as all lower layers with null uuid are on the same fs.
1529 		 * if we detect multiple lower fs with the same uuid, we
1530 		 * disable lower file handle decoding on all of them.
1531 		 */
1532 		if (ofs->fs[i].is_lower &&
1533 		    uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
1534 			ofs->fs[i].bad_uuid = true;
1535 			return false;
1536 		}
1537 	}
1538 	return true;
1539 }
1540 
1541 /* Get a unique fsid for the layer */
ovl_get_fsid(struct ovl_fs * ofs,const struct path * path)1542 static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1543 {
1544 	struct super_block *sb = path->mnt->mnt_sb;
1545 	unsigned int i;
1546 	dev_t dev;
1547 	int err;
1548 	bool bad_uuid = false;
1549 
1550 	for (i = 0; i < ofs->numfs; i++) {
1551 		if (ofs->fs[i].sb == sb)
1552 			return i;
1553 	}
1554 
1555 	if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1556 		bad_uuid = true;
1557 		if (ofs->config.index || ofs->config.nfs_export) {
1558 			ofs->config.index = false;
1559 			ofs->config.nfs_export = false;
1560 			pr_warn("%s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1561 				uuid_is_null(&sb->s_uuid) ? "null" :
1562 							    "conflicting",
1563 				path->dentry);
1564 		}
1565 	}
1566 
1567 	err = get_anon_bdev(&dev);
1568 	if (err) {
1569 		pr_err("failed to get anonymous bdev for lowerpath\n");
1570 		return err;
1571 	}
1572 
1573 	ofs->fs[ofs->numfs].sb = sb;
1574 	ofs->fs[ofs->numfs].pseudo_dev = dev;
1575 	ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
1576 
1577 	return ofs->numfs++;
1578 }
1579 
ovl_get_layers(struct super_block * sb,struct ovl_fs * ofs,struct path * stack,unsigned int numlower,struct ovl_layer * layers)1580 static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
1581 			  struct path *stack, unsigned int numlower,
1582 			  struct ovl_layer *layers)
1583 {
1584 	int err;
1585 	unsigned int i;
1586 
1587 	err = -ENOMEM;
1588 	ofs->fs = kcalloc(numlower + 1, sizeof(struct ovl_sb), GFP_KERNEL);
1589 	if (ofs->fs == NULL)
1590 		goto out;
1591 
1592 	/* idx/fsid 0 are reserved for upper fs even with lower only overlay */
1593 	ofs->numfs++;
1594 
1595 	/*
1596 	 * All lower layers that share the same fs as upper layer, use the same
1597 	 * pseudo_dev as upper layer.  Allocate fs[0].pseudo_dev even for lower
1598 	 * only overlay to simplify ovl_fs_free().
1599 	 * is_lower will be set if upper fs is shared with a lower layer.
1600 	 */
1601 	err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
1602 	if (err) {
1603 		pr_err("failed to get anonymous bdev for upper fs\n");
1604 		goto out;
1605 	}
1606 
1607 	if (ovl_upper_mnt(ofs)) {
1608 		ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
1609 		ofs->fs[0].is_lower = false;
1610 	}
1611 
1612 	for (i = 0; i < numlower; i++) {
1613 		struct vfsmount *mnt;
1614 		struct inode *trap;
1615 		int fsid;
1616 
1617 		err = fsid = ovl_get_fsid(ofs, &stack[i]);
1618 		if (err < 0)
1619 			goto out;
1620 
1621 		/*
1622 		 * Check if lower root conflicts with this overlay layers before
1623 		 * checking if it is in-use as upperdir/workdir of "another"
1624 		 * mount, because we do not bother to check in ovl_is_inuse() if
1625 		 * the upperdir/workdir is in fact in-use by our
1626 		 * upperdir/workdir.
1627 		 */
1628 		err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1629 		if (err)
1630 			goto out;
1631 
1632 		if (ovl_is_inuse(stack[i].dentry)) {
1633 			err = ovl_report_in_use(ofs, "lowerdir");
1634 			if (err) {
1635 				iput(trap);
1636 				goto out;
1637 			}
1638 		}
1639 
1640 		mnt = clone_private_mount(&stack[i]);
1641 		err = PTR_ERR(mnt);
1642 		if (IS_ERR(mnt)) {
1643 			pr_err("failed to clone lowerpath\n");
1644 			iput(trap);
1645 			goto out;
1646 		}
1647 
1648 		/*
1649 		 * Make lower layers R/O.  That way fchmod/fchown on lower file
1650 		 * will fail instead of modifying lower fs.
1651 		 */
1652 		mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1653 
1654 		layers[ofs->numlayer].trap = trap;
1655 		layers[ofs->numlayer].mnt = mnt;
1656 		layers[ofs->numlayer].idx = ofs->numlayer;
1657 		layers[ofs->numlayer].fsid = fsid;
1658 		layers[ofs->numlayer].fs = &ofs->fs[fsid];
1659 		ofs->numlayer++;
1660 		ofs->fs[fsid].is_lower = true;
1661 	}
1662 
1663 	/*
1664 	 * When all layers on same fs, overlay can use real inode numbers.
1665 	 * With mount option "xino=<on|auto>", mounter declares that there are
1666 	 * enough free high bits in underlying fs to hold the unique fsid.
1667 	 * If overlayfs does encounter underlying inodes using the high xino
1668 	 * bits reserved for fsid, it emits a warning and uses the original
1669 	 * inode number or a non persistent inode number allocated from a
1670 	 * dedicated range.
1671 	 */
1672 	if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
1673 		if (ofs->config.xino == OVL_XINO_ON)
1674 			pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
1675 		ofs->xino_mode = 0;
1676 	} else if (ofs->config.xino == OVL_XINO_OFF) {
1677 		ofs->xino_mode = -1;
1678 	} else if (ofs->xino_mode < 0) {
1679 		/*
1680 		 * This is a roundup of number of bits needed for encoding
1681 		 * fsid, where fsid 0 is reserved for upper fs (even with
1682 		 * lower only overlay) +1 extra bit is reserved for the non
1683 		 * persistent inode number range that is used for resolving
1684 		 * xino lower bits overflow.
1685 		 */
1686 		BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
1687 		ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
1688 	}
1689 
1690 	if (ofs->xino_mode > 0) {
1691 		pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
1692 			ofs->xino_mode);
1693 	}
1694 
1695 	err = 0;
1696 out:
1697 	return err;
1698 }
1699 
ovl_get_lowerstack(struct super_block * sb,const char * lower,unsigned int numlower,struct ovl_fs * ofs,struct ovl_layer * layers)1700 static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1701 				const char *lower, unsigned int numlower,
1702 				struct ovl_fs *ofs, struct ovl_layer *layers)
1703 {
1704 	int err;
1705 	struct path *stack = NULL;
1706 	unsigned int i;
1707 	struct ovl_entry *oe;
1708 
1709 	if (!ofs->config.upperdir && numlower == 1) {
1710 		pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
1711 		return ERR_PTR(-EINVAL);
1712 	}
1713 
1714 	stack = kcalloc(numlower, sizeof(struct path), GFP_KERNEL);
1715 	if (!stack)
1716 		return ERR_PTR(-ENOMEM);
1717 
1718 	err = -EINVAL;
1719 	for (i = 0; i < numlower; i++) {
1720 		err = ovl_lower_dir(lower, &stack[i], ofs, &sb->s_stack_depth);
1721 		if (err)
1722 			goto out_err;
1723 
1724 		lower = strchr(lower, '\0') + 1;
1725 	}
1726 
1727 	err = -EINVAL;
1728 	sb->s_stack_depth++;
1729 	if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1730 		pr_err("maximum fs stacking depth exceeded\n");
1731 		goto out_err;
1732 	}
1733 
1734 	err = ovl_get_layers(sb, ofs, stack, numlower, layers);
1735 	if (err)
1736 		goto out_err;
1737 
1738 	err = -ENOMEM;
1739 	oe = ovl_alloc_entry(numlower);
1740 	if (!oe)
1741 		goto out_err;
1742 
1743 	for (i = 0; i < numlower; i++) {
1744 		oe->lowerstack[i].dentry = dget(stack[i].dentry);
1745 		oe->lowerstack[i].layer = &ofs->layers[i+1];
1746 	}
1747 
1748 out:
1749 	for (i = 0; i < numlower; i++)
1750 		path_put(&stack[i]);
1751 	kfree(stack);
1752 
1753 	return oe;
1754 
1755 out_err:
1756 	oe = ERR_PTR(err);
1757 	goto out;
1758 }
1759 
1760 /*
1761  * Check if this layer root is a descendant of:
1762  * - another layer of this overlayfs instance
1763  * - upper/work dir of any overlayfs instance
1764  */
ovl_check_layer(struct super_block * sb,struct ovl_fs * ofs,struct dentry * dentry,const char * name,bool is_lower)1765 static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
1766 			   struct dentry *dentry, const char *name,
1767 			   bool is_lower)
1768 {
1769 	struct dentry *next = dentry, *parent;
1770 	int err = 0;
1771 
1772 	if (!dentry)
1773 		return 0;
1774 
1775 	parent = dget_parent(next);
1776 
1777 	/* Walk back ancestors to root (inclusive) looking for traps */
1778 	while (!err && parent != next) {
1779 		if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
1780 			err = -ELOOP;
1781 			pr_err("overlapping %s path\n", name);
1782 		} else if (ovl_is_inuse(parent)) {
1783 			err = ovl_report_in_use(ofs, name);
1784 		}
1785 		next = parent;
1786 		parent = dget_parent(next);
1787 		dput(next);
1788 	}
1789 
1790 	dput(parent);
1791 
1792 	return err;
1793 }
1794 
1795 /*
1796  * Check if any of the layers or work dirs overlap.
1797  */
ovl_check_overlapping_layers(struct super_block * sb,struct ovl_fs * ofs)1798 static int ovl_check_overlapping_layers(struct super_block *sb,
1799 					struct ovl_fs *ofs)
1800 {
1801 	int i, err;
1802 
1803 	if (ovl_upper_mnt(ofs)) {
1804 		err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
1805 				      "upperdir", false);
1806 		if (err)
1807 			return err;
1808 
1809 		/*
1810 		 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1811 		 * this instance and covers overlapping work and index dirs,
1812 		 * unless work or index dir have been moved since created inside
1813 		 * workbasedir.  In that case, we already have their traps in
1814 		 * inode cache and we will catch that case on lookup.
1815 		 */
1816 		err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
1817 				      false);
1818 		if (err)
1819 			return err;
1820 	}
1821 
1822 	for (i = 1; i < ofs->numlayer; i++) {
1823 		err = ovl_check_layer(sb, ofs,
1824 				      ofs->layers[i].mnt->mnt_root,
1825 				      "lowerdir", true);
1826 		if (err)
1827 			return err;
1828 	}
1829 
1830 	return 0;
1831 }
1832 
ovl_get_root(struct super_block * sb,struct dentry * upperdentry,struct ovl_entry * oe)1833 static struct dentry *ovl_get_root(struct super_block *sb,
1834 				   struct dentry *upperdentry,
1835 				   struct ovl_entry *oe)
1836 {
1837 	struct dentry *root;
1838 	struct ovl_path *lowerpath = &oe->lowerstack[0];
1839 	unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
1840 	int fsid = lowerpath->layer->fsid;
1841 	struct ovl_inode_params oip = {
1842 		.upperdentry = upperdentry,
1843 		.lowerpath = lowerpath,
1844 	};
1845 
1846 	root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1847 	if (!root)
1848 		return NULL;
1849 
1850 	root->d_fsdata = oe;
1851 
1852 	if (upperdentry) {
1853 		/* Root inode uses upper st_ino/i_ino */
1854 		ino = d_inode(upperdentry)->i_ino;
1855 		fsid = 0;
1856 		ovl_dentry_set_upper_alias(root);
1857 		if (ovl_is_impuredir(sb, upperdentry))
1858 			ovl_set_flag(OVL_IMPURE, d_inode(root));
1859 	}
1860 
1861 	/* Root is always merge -> can have whiteouts */
1862 	ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
1863 	ovl_dentry_set_flag(OVL_E_CONNECTED, root);
1864 	ovl_set_upperdata(d_inode(root));
1865 	ovl_inode_init(d_inode(root), &oip, ino, fsid);
1866 	ovl_dentry_update_reval(root, upperdentry, DCACHE_OP_WEAK_REVALIDATE);
1867 
1868 	return root;
1869 }
1870 
ovl_fill_super(struct super_block * sb,void * data,int silent)1871 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1872 {
1873 	struct path upperpath = { };
1874 	struct dentry *root_dentry;
1875 	struct ovl_entry *oe;
1876 	struct ovl_fs *ofs;
1877 	struct ovl_layer *layers;
1878 	struct cred *cred;
1879 	char *splitlower = NULL;
1880 	unsigned int numlower;
1881 	int err;
1882 
1883 	sb->s_d_op = &ovl_dentry_operations;
1884 
1885 	err = -ENOMEM;
1886 	ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1887 	if (!ofs)
1888 		goto out;
1889 
1890 	ofs->creator_cred = cred = prepare_creds();
1891 	if (!cred)
1892 		goto out_err;
1893 
1894 	/* Is there a reason anyone would want not to share whiteouts? */
1895 	ofs->share_whiteout = true;
1896 
1897 	ofs->config.index = ovl_index_def;
1898 	ofs->config.nfs_export = ovl_nfs_export_def;
1899 	ofs->config.xino = ovl_xino_def();
1900 	ofs->config.metacopy = ovl_metacopy_def;
1901 	err = ovl_parse_opt((char *) data, &ofs->config);
1902 	if (err)
1903 		goto out_err;
1904 
1905 	err = -EINVAL;
1906 	if (!ofs->config.lowerdir) {
1907 		if (!silent)
1908 			pr_err("missing 'lowerdir'\n");
1909 		goto out_err;
1910 	}
1911 
1912 	err = -ENOMEM;
1913 	splitlower = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1914 	if (!splitlower)
1915 		goto out_err;
1916 
1917 	numlower = ovl_split_lowerdirs(splitlower);
1918 	if (numlower > OVL_MAX_STACK) {
1919 		pr_err("too many lower directories, limit is %d\n",
1920 		       OVL_MAX_STACK);
1921 		goto out_err;
1922 	}
1923 
1924 	layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL);
1925 	if (!layers)
1926 		goto out_err;
1927 
1928 	ofs->layers = layers;
1929 	/* Layer 0 is reserved for upper even if there's no upper */
1930 	ofs->numlayer = 1;
1931 
1932 	sb->s_stack_depth = 0;
1933 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1934 	atomic_long_set(&ofs->last_ino, 1);
1935 	/* Assume underlaying fs uses 32bit inodes unless proven otherwise */
1936 	if (ofs->config.xino != OVL_XINO_OFF) {
1937 		ofs->xino_mode = BITS_PER_LONG - 32;
1938 		if (!ofs->xino_mode) {
1939 			pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
1940 			ofs->config.xino = OVL_XINO_OFF;
1941 		}
1942 	}
1943 
1944 	/* alloc/destroy_inode needed for setting up traps in inode cache */
1945 	sb->s_op = &ovl_super_operations;
1946 
1947 	if (ofs->config.upperdir) {
1948 		struct super_block *upper_sb;
1949 
1950 		if (!ofs->config.workdir) {
1951 			pr_err("missing 'workdir'\n");
1952 			goto out_err;
1953 		}
1954 
1955 		err = ovl_get_upper(sb, ofs, &layers[0], &upperpath);
1956 		if (err)
1957 			goto out_err;
1958 
1959 		upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
1960 		if (!ovl_should_sync(ofs)) {
1961 			ofs->errseq = errseq_sample(&upper_sb->s_wb_err);
1962 			if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) {
1963 				err = -EIO;
1964 				pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n");
1965 				goto out_err;
1966 			}
1967 		}
1968 
1969 		err = ovl_get_workdir(sb, ofs, &upperpath);
1970 		if (err)
1971 			goto out_err;
1972 
1973 		if (!ofs->workdir)
1974 			sb->s_flags |= SB_RDONLY;
1975 
1976 		sb->s_stack_depth = upper_sb->s_stack_depth;
1977 		sb->s_time_gran = upper_sb->s_time_gran;
1978 	}
1979 	oe = ovl_get_lowerstack(sb, splitlower, numlower, ofs, layers);
1980 	err = PTR_ERR(oe);
1981 	if (IS_ERR(oe))
1982 		goto out_err;
1983 
1984 	/* If the upper fs is nonexistent, we mark overlayfs r/o too */
1985 	if (!ovl_upper_mnt(ofs))
1986 		sb->s_flags |= SB_RDONLY;
1987 
1988 	if (!ovl_force_readonly(ofs) && ofs->config.index) {
1989 		err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
1990 		if (err)
1991 			goto out_free_oe;
1992 
1993 		/* Force r/o mount with no index dir */
1994 		if (!ofs->indexdir)
1995 			sb->s_flags |= SB_RDONLY;
1996 	}
1997 
1998 	err = ovl_check_overlapping_layers(sb, ofs);
1999 	if (err)
2000 		goto out_free_oe;
2001 
2002 	/* Show index=off in /proc/mounts for forced r/o mount */
2003 	if (!ofs->indexdir) {
2004 		ofs->config.index = false;
2005 		if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
2006 			pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
2007 			ofs->config.nfs_export = false;
2008 		}
2009 	}
2010 
2011 	if (ofs->config.metacopy && ofs->config.nfs_export) {
2012 		pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
2013 		ofs->config.nfs_export = false;
2014 	}
2015 
2016 	if (ofs->config.nfs_export)
2017 		sb->s_export_op = &ovl_export_operations;
2018 
2019 	/* Never override disk quota limits or use reserved space */
2020 	cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
2021 
2022 	sb->s_magic = OVERLAYFS_SUPER_MAGIC;
2023 	sb->s_xattr = ovl_xattr_handlers;
2024 	sb->s_fs_info = ofs;
2025 	sb->s_flags |= SB_POSIXACL;
2026 	sb->s_iflags |= SB_I_SKIP_SYNC;
2027 
2028 	err = -ENOMEM;
2029 	root_dentry = ovl_get_root(sb, upperpath.dentry, oe);
2030 	if (!root_dentry)
2031 		goto out_free_oe;
2032 
2033 	mntput(upperpath.mnt);
2034 	kfree(splitlower);
2035 
2036 	sb->s_root = root_dentry;
2037 
2038 	return 0;
2039 
2040 out_free_oe:
2041 	ovl_entry_stack_free(oe);
2042 	kfree(oe);
2043 out_err:
2044 	kfree(splitlower);
2045 	path_put(&upperpath);
2046 	ovl_free_fs(ofs);
2047 out:
2048 	return err;
2049 }
2050 
ovl_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * raw_data)2051 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
2052 				const char *dev_name, void *raw_data)
2053 {
2054 	return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
2055 }
2056 
2057 static struct file_system_type ovl_fs_type = {
2058 	.owner		= THIS_MODULE,
2059 	.name		= "overlay",
2060 	.mount		= ovl_mount,
2061 	.kill_sb	= kill_anon_super,
2062 };
2063 MODULE_ALIAS_FS("overlay");
2064 
ovl_inode_init_once(void * foo)2065 static void ovl_inode_init_once(void *foo)
2066 {
2067 	struct ovl_inode *oi = foo;
2068 
2069 	inode_init_once(&oi->vfs_inode);
2070 }
2071 
ovl_init(void)2072 static int __init ovl_init(void)
2073 {
2074 	int err;
2075 
2076 	ovl_inode_cachep = kmem_cache_create("ovl_inode",
2077 					     sizeof(struct ovl_inode), 0,
2078 					     (SLAB_RECLAIM_ACCOUNT|
2079 					      SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2080 					     ovl_inode_init_once);
2081 	if (ovl_inode_cachep == NULL)
2082 		return -ENOMEM;
2083 
2084 	err = ovl_aio_request_cache_init();
2085 	if (!err) {
2086 		err = register_filesystem(&ovl_fs_type);
2087 		if (!err)
2088 			return 0;
2089 
2090 		ovl_aio_request_cache_destroy();
2091 	}
2092 	kmem_cache_destroy(ovl_inode_cachep);
2093 
2094 	return err;
2095 }
2096 
ovl_exit(void)2097 static void __exit ovl_exit(void)
2098 {
2099 	unregister_filesystem(&ovl_fs_type);
2100 
2101 	/*
2102 	 * Make sure all delayed rcu free inodes are flushed before we
2103 	 * destroy cache.
2104 	 */
2105 	rcu_barrier();
2106 	kmem_cache_destroy(ovl_inode_cachep);
2107 	ovl_aio_request_cache_destroy();
2108 }
2109 
2110 module_init(ovl_init);
2111 module_exit(ovl_exit);
2112