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