blob: c166c1d76890b69e2bd4cf9f4ba6707d80bd6bf9 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <uapi/linux/magic.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011#include <linux/fs.h>
12#include <linux/namei.h>
13#include <linux/xattr.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020014#include <linux/mount.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020015#include <linux/parser.h>
16#include <linux/module.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020017#include <linux/statfs.h>
Erez Zadokf45827e82014-10-24 00:14:38 +020018#include <linux/seq_file.h>
Miklos Szeredid837a492016-07-29 12:05:24 +020019#include <linux/posix_acl_xattr.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include "overlayfs.h"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010021#include "ovl_entry.h"
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022
23MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24MODULE_DESCRIPTION("Overlay filesystem");
25MODULE_LICENSE("GPL");
26
Miklos Szeredie9be9d52014-10-24 00:14:38 +020027
28struct ovl_dir_cache;
29
Miklos Szeredia78d9f02014-12-13 00:59:52 +010030#define OVL_MAX_STACK 500
31
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010032static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
33module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
34MODULE_PARM_DESC(ovl_redirect_dir_def,
35 "Default to on or off for the redirect_dir feature");
Miklos Szeredie9be9d52014-10-24 00:14:38 +020036
37static void ovl_dentry_release(struct dentry *dentry)
38{
39 struct ovl_entry *oe = dentry->d_fsdata;
40
41 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +010042 unsigned int i;
43
Miklos Szeredie9be9d52014-10-24 00:14:38 +020044 dput(oe->__upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +010045 kfree(oe->redirect);
Miklos Szeredidd662662014-12-13 00:59:43 +010046 for (i = 0; i < oe->numlower; i++)
47 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020048 kfree_rcu(oe, rcu);
49 }
50}
51
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030052static int ovl_check_append_only(struct inode *inode, int flag)
53{
54 /*
55 * This test was moot in vfs may_open() because overlay inode does
56 * not have the S_APPEND flag, so re-check on real upper inode
57 */
58 if (IS_APPEND(inode)) {
59 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
60 return -EPERM;
61 if (flag & O_TRUNC)
62 return -EPERM;
63 }
64
65 return 0;
66}
67
Miklos Szeredi2d902672016-06-30 08:53:27 +020068static struct dentry *ovl_d_real(struct dentry *dentry,
69 const struct inode *inode,
70 unsigned int open_flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040071{
72 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030073 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040074
Miklos Szeredica4c8a32016-12-16 11:02:55 +010075 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040076 if (!inode || inode == d_inode(dentry))
77 return dentry;
78 goto bug;
79 }
80
Miklos Szeredi2d902672016-06-30 08:53:27 +020081 if (d_is_negative(dentry))
82 return dentry;
83
84 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030085 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +020086 if (err)
87 return ERR_PTR(err);
88 }
89
Miklos Szeredid101a122016-03-26 16:14:37 -040090 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030091 if (real && (!inode || inode == d_inode(real))) {
92 if (!inode) {
93 err = ovl_check_append_only(d_inode(real), open_flags);
94 if (err)
95 return ERR_PTR(err);
96 }
Miklos Szeredid101a122016-03-26 16:14:37 -040097 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030098 }
Miklos Szeredid101a122016-03-26 16:14:37 -040099
100 real = ovl_dentry_lower(dentry);
101 if (!real)
102 goto bug;
103
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100104 /* Handle recursion */
105 real = d_real(real, inode, open_flags);
106
Miklos Szeredid101a122016-03-26 16:14:37 -0400107 if (!inode || inode == d_inode(real))
108 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400109bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200110 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400111 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
112 return dentry;
113}
114
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200115static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
116{
117 struct ovl_entry *oe = dentry->d_fsdata;
118 unsigned int i;
119 int ret = 1;
120
121 for (i = 0; i < oe->numlower; i++) {
122 struct dentry *d = oe->lowerstack[i].dentry;
123
124 if (d->d_flags & DCACHE_OP_REVALIDATE) {
125 ret = d->d_op->d_revalidate(d, flags);
126 if (ret < 0)
127 return ret;
128 if (!ret) {
129 if (!(flags & LOOKUP_RCU))
130 d_invalidate(d);
131 return -ESTALE;
132 }
133 }
134 }
135 return 1;
136}
137
138static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
139{
140 struct ovl_entry *oe = dentry->d_fsdata;
141 unsigned int i;
142 int ret = 1;
143
144 for (i = 0; i < oe->numlower; i++) {
145 struct dentry *d = oe->lowerstack[i].dentry;
146
147 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
148 ret = d->d_op->d_weak_revalidate(d, flags);
149 if (ret <= 0)
150 break;
151 }
152 }
153 return ret;
154}
155
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200156static const struct dentry_operations ovl_dentry_operations = {
157 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400158 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200159};
160
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200161static const struct dentry_operations ovl_reval_dentry_operations = {
162 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400163 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200164 .d_revalidate = ovl_dentry_revalidate,
165 .d_weak_revalidate = ovl_dentry_weak_revalidate,
166};
167
Amir Goldstein13cf1992017-06-12 09:54:40 +0300168static struct kmem_cache *ovl_inode_cachep;
169
170static struct inode *ovl_alloc_inode(struct super_block *sb)
171{
172 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
173
Miklos Szeredi25b77132017-07-04 22:03:16 +0200174 oi->upper = NULL;
175 oi->lower = NULL;
176
Amir Goldstein13cf1992017-06-12 09:54:40 +0300177 return &oi->vfs_inode;
178}
179
180static void ovl_i_callback(struct rcu_head *head)
181{
182 struct inode *inode = container_of(head, struct inode, i_rcu);
183
184 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
185}
186
187static void ovl_destroy_inode(struct inode *inode)
188{
189 call_rcu(&inode->i_rcu, ovl_i_callback);
190}
191
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200192static void ovl_put_super(struct super_block *sb)
193{
194 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100195 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196
197 dput(ufs->workdir);
198 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100199 for (i = 0; i < ufs->numlower; i++)
200 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300201 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200202
Erez Zadokf45827e82014-10-24 00:14:38 +0200203 kfree(ufs->config.lowerdir);
204 kfree(ufs->config.upperdir);
205 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200206 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207 kfree(ufs);
208}
209
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200210static int ovl_sync_fs(struct super_block *sb, int wait)
211{
212 struct ovl_fs *ufs = sb->s_fs_info;
213 struct super_block *upper_sb;
214 int ret;
215
216 if (!ufs->upper_mnt)
217 return 0;
218 upper_sb = ufs->upper_mnt->mnt_sb;
219 if (!upper_sb->s_op->sync_fs)
220 return 0;
221
222 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
223 down_read(&upper_sb->s_umount);
224 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
225 up_read(&upper_sb->s_umount);
226 return ret;
227}
228
Andy Whitcroftcc259632014-10-24 00:14:38 +0200229/**
230 * ovl_statfs
231 * @sb: The overlayfs super block
232 * @buf: The struct kstatfs to fill in with stats
233 *
234 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100235 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200236 */
237static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
238{
239 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
240 struct dentry *root_dentry = dentry->d_sb->s_root;
241 struct path path;
242 int err;
243
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100244 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200245
246 err = vfs_statfs(&path, buf);
247 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100248 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200249 buf->f_type = OVERLAYFS_SUPER_MAGIC;
250 }
251
252 return err;
253}
254
Erez Zadokf45827e82014-10-24 00:14:38 +0200255/**
256 * ovl_show_options
257 *
258 * Prints the mount options for a given superblock.
259 * Returns zero; does not fail.
260 */
261static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
262{
263 struct super_block *sb = dentry->d_sb;
264 struct ovl_fs *ufs = sb->s_fs_info;
265
Kees Cooka068acf2015-09-04 15:44:57 -0700266 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100267 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700268 seq_show_option(m, "upperdir", ufs->config.upperdir);
269 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100270 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200271 if (ufs->config.default_permissions)
272 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200273 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
274 seq_printf(m, ",redirect_dir=%s",
275 ufs->config.redirect_dir ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200276 return 0;
277}
278
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900279static int ovl_remount(struct super_block *sb, int *flags, char *data)
280{
281 struct ovl_fs *ufs = sb->s_fs_info;
282
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200283 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900284 return -EROFS;
285
286 return 0;
287}
288
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200289static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300290 .alloc_inode = ovl_alloc_inode,
291 .destroy_inode = ovl_destroy_inode,
292 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200293 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200294 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200295 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200296 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900297 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200298};
299
300enum {
301 OPT_LOWERDIR,
302 OPT_UPPERDIR,
303 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200304 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100305 OPT_REDIRECT_DIR_ON,
306 OPT_REDIRECT_DIR_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200307 OPT_ERR,
308};
309
310static const match_table_t ovl_tokens = {
311 {OPT_LOWERDIR, "lowerdir=%s"},
312 {OPT_UPPERDIR, "upperdir=%s"},
313 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200314 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100315 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
316 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200317 {OPT_ERR, NULL}
318};
319
Miklos Szeredi91c77942014-11-20 16:40:00 +0100320static char *ovl_next_opt(char **s)
321{
322 char *sbegin = *s;
323 char *p;
324
325 if (sbegin == NULL)
326 return NULL;
327
328 for (p = sbegin; *p; p++) {
329 if (*p == '\\') {
330 p++;
331 if (!*p)
332 break;
333 } else if (*p == ',') {
334 *p = '\0';
335 *s = p + 1;
336 return sbegin;
337 }
338 }
339 *s = NULL;
340 return sbegin;
341}
342
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343static int ovl_parse_opt(char *opt, struct ovl_config *config)
344{
345 char *p;
346
Miklos Szeredi91c77942014-11-20 16:40:00 +0100347 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200348 int token;
349 substring_t args[MAX_OPT_ARGS];
350
351 if (!*p)
352 continue;
353
354 token = match_token(p, ovl_tokens, args);
355 switch (token) {
356 case OPT_UPPERDIR:
357 kfree(config->upperdir);
358 config->upperdir = match_strdup(&args[0]);
359 if (!config->upperdir)
360 return -ENOMEM;
361 break;
362
363 case OPT_LOWERDIR:
364 kfree(config->lowerdir);
365 config->lowerdir = match_strdup(&args[0]);
366 if (!config->lowerdir)
367 return -ENOMEM;
368 break;
369
370 case OPT_WORKDIR:
371 kfree(config->workdir);
372 config->workdir = match_strdup(&args[0]);
373 if (!config->workdir)
374 return -ENOMEM;
375 break;
376
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200377 case OPT_DEFAULT_PERMISSIONS:
378 config->default_permissions = true;
379 break;
380
Miklos Szeredia6c60652016-12-16 11:02:56 +0100381 case OPT_REDIRECT_DIR_ON:
382 config->redirect_dir = true;
383 break;
384
385 case OPT_REDIRECT_DIR_OFF:
386 config->redirect_dir = false;
387 break;
388
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200389 default:
hujianyangbead55e2015-01-15 13:17:36 +0800390 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200391 return -EINVAL;
392 }
393 }
hujianyang71cbad72015-01-15 13:20:57 +0800394
395 /* Workdir is useless in non-upper mount */
396 if (!config->upperdir && config->workdir) {
397 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
398 config->workdir);
399 kfree(config->workdir);
400 config->workdir = NULL;
401 }
402
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200403 return 0;
404}
405
406#define OVL_WORKDIR_NAME "work"
407
408static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
409 struct dentry *dentry)
410{
411 struct inode *dir = dentry->d_inode;
412 struct dentry *work;
413 int err;
414 bool retried = false;
415
416 err = mnt_want_write(mnt);
417 if (err)
418 return ERR_PTR(err);
419
Al Viro59551022016-01-22 15:40:57 -0500420 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200421retry:
422 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
423 strlen(OVL_WORKDIR_NAME));
424
425 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200426 struct iattr attr = {
427 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000428 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200429 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200430
431 if (work->d_inode) {
432 err = -EEXIST;
433 if (retried)
434 goto out_dput;
435
436 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200437 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200438 dput(work);
439 goto retry;
440 }
441
Al Viro32a3d842016-12-04 17:33:17 +0000442 err = ovl_create_real(dir, work,
443 &(struct cattr){.mode = S_IFDIR | 0},
444 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445 if (err)
446 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200447
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200448 /*
449 * Try to remove POSIX ACL xattrs from workdir. We are good if:
450 *
451 * a) success (there was a POSIX ACL xattr and was removed)
452 * b) -ENODATA (there was no POSIX ACL xattr)
453 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
454 *
455 * There are various other error values that could effectively
456 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
457 * if the xattr name is too long), but the set of filesystems
458 * allowed as upper are limited to "normal" ones, where checking
459 * for the above two errors is sufficient.
460 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200461 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200462 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200463 goto out_dput;
464
465 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200466 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200467 goto out_dput;
468
469 /* Clear any inherited mode bits */
470 inode_lock(work->d_inode);
471 err = notify_change(work, &attr, NULL);
472 inode_unlock(work->d_inode);
473 if (err)
474 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200475 }
476out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500477 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200478 mnt_drop_write(mnt);
479
480 return work;
481
482out_dput:
483 dput(work);
484 work = ERR_PTR(err);
485 goto out_unlock;
486}
487
Miklos Szeredi91c77942014-11-20 16:40:00 +0100488static void ovl_unescape(char *s)
489{
490 char *d = s;
491
492 for (;; s++, d++) {
493 if (*s == '\\')
494 s++;
495 *d = *s;
496 if (!*s)
497 break;
498 }
499}
500
Miklos Szerediab508822014-12-13 00:59:49 +0100501static int ovl_mount_dir_noesc(const char *name, struct path *path)
502{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100503 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100504
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100505 if (!*name) {
506 pr_err("overlayfs: empty lowerdir\n");
507 goto out;
508 }
Miklos Szerediab508822014-12-13 00:59:49 +0100509 err = kern_path(name, LOOKUP_FOLLOW, path);
510 if (err) {
511 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
512 goto out;
513 }
514 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200515 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100516 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
517 goto out_put;
518 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100519 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100520 pr_err("overlayfs: '%s' not a directory\n", name);
521 goto out_put;
522 }
523 return 0;
524
525out_put:
526 path_put(path);
527out:
528 return err;
529}
530
531static int ovl_mount_dir(const char *name, struct path *path)
532{
533 int err = -ENOMEM;
534 char *tmp = kstrdup(name, GFP_KERNEL);
535
536 if (tmp) {
537 ovl_unescape(tmp);
538 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200539
540 if (!err)
541 if (ovl_dentry_remote(path->dentry)) {
542 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
543 tmp);
544 path_put(path);
545 err = -EINVAL;
546 }
Miklos Szerediab508822014-12-13 00:59:49 +0100547 kfree(tmp);
548 }
549 return err;
550}
551
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100552static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
553 const char *name)
554{
555 struct kstatfs statfs;
556 int err = vfs_statfs(path, &statfs);
557
558 if (err)
559 pr_err("overlayfs: statfs failed on '%s'\n", name);
560 else
561 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
562
563 return err;
564}
565
566static int ovl_lower_dir(const char *name, struct path *path,
567 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100568{
569 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100570
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100571 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100572 if (err)
573 goto out;
574
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100575 err = ovl_check_namelen(path, ofs, name);
576 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100577 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100578
Miklos Szerediab508822014-12-13 00:59:49 +0100579 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
580
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200581 if (ovl_dentry_remote(path->dentry))
582 *remote = true;
583
Miklos Szerediab508822014-12-13 00:59:49 +0100584 return 0;
585
586out_put:
587 path_put(path);
588out:
589 return err;
590}
591
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200592/* Workdir should not be subdir of upperdir and vice versa */
593static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
594{
595 bool ok = false;
596
597 if (workdir != upperdir) {
598 ok = (lock_rename(workdir, upperdir) == NULL);
599 unlock_rename(workdir, upperdir);
600 }
601 return ok;
602}
603
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100604static unsigned int ovl_split_lowerdirs(char *str)
605{
606 unsigned int ctr = 1;
607 char *s, *d;
608
609 for (s = d = str;; s++, d++) {
610 if (*s == '\\') {
611 s++;
612 } else if (*s == ':') {
613 *d = '\0';
614 ctr++;
615 continue;
616 }
617 *d = *s;
618 if (!*s)
619 break;
620 }
621 return ctr;
622}
623
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200624static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200625ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
626 struct dentry *dentry, struct inode *inode,
627 const char *name, void *buffer, size_t size)
628{
629 return ovl_xattr_get(dentry, handler->name, buffer, size);
630}
631
632static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200633ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
634 struct dentry *dentry, struct inode *inode,
635 const char *name, const void *value,
636 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200637{
638 struct dentry *workdir = ovl_workdir(dentry);
639 struct inode *realinode = ovl_inode_real(inode, NULL);
640 struct posix_acl *acl = NULL;
641 int err;
642
643 /* Check that everything is OK before copy-up */
644 if (value) {
645 acl = posix_acl_from_xattr(&init_user_ns, value, size);
646 if (IS_ERR(acl))
647 return PTR_ERR(acl);
648 }
649 err = -EOPNOTSUPP;
650 if (!IS_POSIXACL(d_inode(workdir)))
651 goto out_acl_release;
652 if (!realinode->i_op->set_acl)
653 goto out_acl_release;
654 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
655 err = acl ? -EACCES : 0;
656 goto out_acl_release;
657 }
658 err = -EPERM;
659 if (!inode_owner_or_capable(inode))
660 goto out_acl_release;
661
662 posix_acl_release(acl);
663
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100664 /*
665 * Check if sgid bit needs to be cleared (actual setacl operation will
666 * be done with mounter's capabilities and so that won't do it for us).
667 */
668 if (unlikely(inode->i_mode & S_ISGID) &&
669 handler->flags == ACL_TYPE_ACCESS &&
670 !in_group_p(inode->i_gid) &&
671 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
672 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
673
674 err = ovl_setattr(dentry, &iattr);
675 if (err)
676 return err;
677 }
678
Miklos Szeredice315132016-09-01 11:12:00 +0200679 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
680 if (!err)
681 ovl_copyattr(ovl_inode_real(inode, NULL), inode);
682
683 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200684
685out_acl_release:
686 posix_acl_release(acl);
687 return err;
688}
689
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200690static int ovl_own_xattr_get(const struct xattr_handler *handler,
691 struct dentry *dentry, struct inode *inode,
692 const char *name, void *buffer, size_t size)
693{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200694 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200695}
696
Miklos Szeredid837a492016-07-29 12:05:24 +0200697static int ovl_own_xattr_set(const struct xattr_handler *handler,
698 struct dentry *dentry, struct inode *inode,
699 const char *name, const void *value,
700 size_t size, int flags)
701{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200702 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200703}
704
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200705static int ovl_other_xattr_get(const struct xattr_handler *handler,
706 struct dentry *dentry, struct inode *inode,
707 const char *name, void *buffer, size_t size)
708{
709 return ovl_xattr_get(dentry, name, buffer, size);
710}
711
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200712static int ovl_other_xattr_set(const struct xattr_handler *handler,
713 struct dentry *dentry, struct inode *inode,
714 const char *name, const void *value,
715 size_t size, int flags)
716{
717 return ovl_xattr_set(dentry, name, value, size, flags);
718}
719
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200720static const struct xattr_handler __maybe_unused
721ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200722 .name = XATTR_NAME_POSIX_ACL_ACCESS,
723 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200724 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200725 .set = ovl_posix_acl_xattr_set,
726};
727
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200728static const struct xattr_handler __maybe_unused
729ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200730 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
731 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200732 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200733 .set = ovl_posix_acl_xattr_set,
734};
735
736static const struct xattr_handler ovl_own_xattr_handler = {
737 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200738 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200739 .set = ovl_own_xattr_set,
740};
741
742static const struct xattr_handler ovl_other_xattr_handler = {
743 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200744 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200745 .set = ovl_other_xattr_set,
746};
747
748static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200749#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200750 &ovl_posix_acl_access_xattr_handler,
751 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200752#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200753 &ovl_own_xattr_handler,
754 &ovl_other_xattr_handler,
755 NULL
756};
757
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200758static int ovl_fill_super(struct super_block *sb, void *data, int silent)
759{
Kees Cook33006cd2017-03-29 14:02:19 -0700760 struct path upperpath = { };
761 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200762 struct dentry *root_dentry;
763 struct ovl_entry *oe;
764 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100765 struct path *stack = NULL;
766 char *lowertmp;
767 char *lower;
768 unsigned int numlower;
769 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100770 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200771 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300772 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200773 int err;
774
Erez Zadokf45827e82014-10-24 00:14:38 +0200775 err = -ENOMEM;
776 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
777 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200778 goto out;
779
Amir Goldstein39d3d602017-01-17 06:34:56 +0200780 init_waitqueue_head(&ufs->copyup_wq);
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100781 ufs->config.redirect_dir = ovl_redirect_dir_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200782 err = ovl_parse_opt((char *) data, &ufs->config);
783 if (err)
784 goto out_free_config;
785
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200786 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100787 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300788 if (!silent)
789 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200790 goto out_free_config;
791 }
792
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100793 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100794 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100795 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100796 if (!ufs->config.workdir) {
797 pr_err("overlayfs: missing 'workdir'\n");
798 goto out_free_config;
799 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200800
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100801 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
802 if (err)
803 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100804
hujianyang71cbad72015-01-15 13:20:57 +0800805 /* Upper fs should not be r/o */
806 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
807 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
808 err = -EINVAL;
809 goto out_put_upperpath;
810 }
811
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100812 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
813 if (err)
814 goto out_put_upperpath;
815
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100816 err = ovl_mount_dir(ufs->config.workdir, &workpath);
817 if (err)
818 goto out_put_upperpath;
819
hujianyang2f83fd82015-01-06 12:52:13 +0800820 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100821 if (upperpath.mnt != workpath.mnt) {
822 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
823 goto out_put_workpath;
824 }
825 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
826 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
827 goto out_put_workpath;
828 }
829 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100830 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100831 err = -ENOMEM;
832 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
833 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100834 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200835
836 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100837 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800838 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100839 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800840 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100841 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800842 } else if (!ufs->config.upperdir && stacklen == 1) {
843 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
844 goto out_free_lowertmp;
845 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100846
Geliang Tang313684c2016-11-18 23:02:46 +0800847 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100848 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
849 if (!stack)
850 goto out_free_lowertmp;
851
Geliang Tang313684c2016-11-18 23:02:46 +0800852 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100853 lower = lowertmp;
854 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100855 err = ovl_lower_dir(lower, &stack[numlower], ufs,
856 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100857 if (err)
858 goto out_put_lowerpath;
859
860 lower = strchr(lower, '\0') + 1;
861 }
862
863 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100864 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200865 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
866 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100867 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200868 }
869
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100870 if (ufs->config.upperdir) {
871 ufs->upper_mnt = clone_private_mount(&upperpath);
872 err = PTR_ERR(ufs->upper_mnt);
873 if (IS_ERR(ufs->upper_mnt)) {
874 pr_err("overlayfs: failed to clone upperpath\n");
875 goto out_put_lowerpath;
876 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200877 /* Don't inherit atime flags */
878 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
879
880 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100881
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100882 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
883 err = PTR_ERR(ufs->workdir);
884 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200885 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
886 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
887 sb->s_flags |= MS_RDONLY;
888 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100889 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500890
891 /*
892 * Upper should support d_type, else whiteouts are visible.
893 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400894 * iterate_dir() on workdir. This check requires successful
895 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500896 */
Vivek Goyal21765192016-05-20 09:04:26 -0400897 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200898 struct dentry *temp;
899
Vivek Goyal21765192016-05-20 09:04:26 -0400900 err = ovl_check_d_type_supported(&workpath);
901 if (err < 0)
902 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500903
Vivek Goyale7c0b592016-07-01 10:02:44 -0400904 /*
905 * We allowed this configuration and don't want to
906 * break users over kernel upgrade. So warn instead
907 * of erroring out.
908 */
909 if (!err)
910 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200911
912 /* Check if upper/work fs supports O_TMPFILE */
913 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
914 ufs->tmpfile = !IS_ERR(temp);
915 if (ufs->tmpfile)
916 dput(temp);
917 else
918 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +0300919
920 /*
921 * Check if upper/work fs supports trusted.overlay.*
922 * xattr
923 */
924 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
925 "0", 1, 0);
926 if (err) {
927 ufs->noxattr = true;
928 pr_warn("overlayfs: upper fs does not support xattr.\n");
929 } else {
930 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
931 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500932 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200933 }
934
hujianyang2f83fd82015-01-06 12:52:13 +0800935 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100936 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +0100937 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100938 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100939 for (i = 0; i < numlower; i++) {
940 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +0100941
hujianyang2f83fd82015-01-06 12:52:13 +0800942 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100943 if (IS_ERR(mnt)) {
944 pr_err("overlayfs: failed to clone lowerpath\n");
945 goto out_put_lower_mnt;
946 }
947 /*
948 * Make lower_mnt R/O. That way fchmod/fchown on lower file
949 * will fail instead of modifying lower fs.
950 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200951 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100952
953 ufs->lower_mnt[ufs->numlower] = mnt;
954 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400955
956 /* Check if all lower layers are on same sb */
957 if (i == 0)
958 ufs->same_sb = mnt->mnt_sb;
959 else if (ufs->same_sb != mnt->mnt_sb)
960 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200961 }
962
hujianyang71cbad72015-01-15 13:20:57 +0800963 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
964 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200965 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400966 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
967 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200968
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200969 if (remote)
970 sb->s_d_op = &ovl_reval_dentry_operations;
971 else
972 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200973
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300974 ufs->creator_cred = cred = prepare_creds();
975 if (!cred)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200976 goto out_put_lower_mnt;
977
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300978 /* Never override disk quota limits or use reserved space */
979 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
980
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200981 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100982 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100983 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200984 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200985
Vivek Goyal655042c2016-10-14 03:03:36 +0200986 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
987 sb->s_op = &ovl_super_operations;
988 sb->s_xattr = ovl_xattr_handlers;
989 sb->s_fs_info = ufs;
990 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
991
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100992 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200993 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100994 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200995
996 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100997 for (i = 0; i < numlower; i++)
998 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200999 path_put(&workpath);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001000 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001001
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001002 if (upperpath.dentry) {
1003 oe->__upperdentry = upperpath.dentry;
1004 oe->impure = ovl_is_impuredir(upperpath.dentry);
1005 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001006 for (i = 0; i < numlower; i++) {
1007 oe->lowerstack[i].dentry = stack[i].dentry;
1008 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1009 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001010 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001011
1012 root_dentry->d_fsdata = oe;
1013
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001014 ovl_inode_init(d_inode(root_dentry), root_dentry);
Miklos Szeredied06e062015-12-09 16:11:59 +01001015
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001016 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001017
1018 return 0;
1019
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001020out_free_oe:
1021 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001022out_put_cred:
1023 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001024out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001025 for (i = 0; i < ufs->numlower; i++)
1026 mntput(ufs->lower_mnt[i]);
1027 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001028out_put_workdir:
1029 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001030 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001031out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001032 for (i = 0; i < numlower; i++)
1033 path_put(&stack[i]);
1034 kfree(stack);
1035out_free_lowertmp:
1036 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001037out_put_workpath:
1038 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001039out_put_upperpath:
1040 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001041out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001042 kfree(ufs->config.lowerdir);
1043 kfree(ufs->config.upperdir);
1044 kfree(ufs->config.workdir);
1045 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001046out:
1047 return err;
1048}
1049
1050static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1051 const char *dev_name, void *raw_data)
1052{
1053 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1054}
1055
1056static struct file_system_type ovl_fs_type = {
1057 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001058 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001059 .mount = ovl_mount,
1060 .kill_sb = kill_anon_super,
1061};
Miklos Szeredief94b182014-11-20 16:39:59 +01001062MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001063
Amir Goldstein13cf1992017-06-12 09:54:40 +03001064static void ovl_inode_init_once(void *foo)
1065{
1066 struct ovl_inode *oi = foo;
1067
1068 inode_init_once(&oi->vfs_inode);
1069}
1070
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001071static int __init ovl_init(void)
1072{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001073 int err;
1074
1075 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1076 sizeof(struct ovl_inode), 0,
1077 (SLAB_RECLAIM_ACCOUNT|
1078 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1079 ovl_inode_init_once);
1080 if (ovl_inode_cachep == NULL)
1081 return -ENOMEM;
1082
1083 err = register_filesystem(&ovl_fs_type);
1084 if (err)
1085 kmem_cache_destroy(ovl_inode_cachep);
1086
1087 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001088}
1089
1090static void __exit ovl_exit(void)
1091{
1092 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001093
1094 /*
1095 * Make sure all delayed rcu free inodes are flushed before we
1096 * destroy cache.
1097 */
1098 rcu_barrier();
1099 kmem_cache_destroy(ovl_inode_cachep);
1100
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001101}
1102
1103module_init(ovl_init);
1104module_exit(ovl_exit);