blob: b0d539af1fadc5a308e327fa3b805002b075e1f5 [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 Szeredidd662662014-12-13 00:59:43 +010044 for (i = 0; i < oe->numlower; i++)
45 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020046 kfree_rcu(oe, rcu);
47 }
48}
49
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030050static int ovl_check_append_only(struct inode *inode, int flag)
51{
52 /*
53 * This test was moot in vfs may_open() because overlay inode does
54 * not have the S_APPEND flag, so re-check on real upper inode
55 */
56 if (IS_APPEND(inode)) {
57 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
58 return -EPERM;
59 if (flag & O_TRUNC)
60 return -EPERM;
61 }
62
63 return 0;
64}
65
Miklos Szeredi2d902672016-06-30 08:53:27 +020066static struct dentry *ovl_d_real(struct dentry *dentry,
67 const struct inode *inode,
68 unsigned int open_flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040069{
70 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030071 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040072
Miklos Szeredica4c8a32016-12-16 11:02:55 +010073 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040074 if (!inode || inode == d_inode(dentry))
75 return dentry;
76 goto bug;
77 }
78
Miklos Szeredi2d902672016-06-30 08:53:27 +020079 if (d_is_negative(dentry))
80 return dentry;
81
82 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030083 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +020084 if (err)
85 return ERR_PTR(err);
86 }
87
Miklos Szeredid101a122016-03-26 16:14:37 -040088 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030089 if (real && (!inode || inode == d_inode(real))) {
90 if (!inode) {
91 err = ovl_check_append_only(d_inode(real), open_flags);
92 if (err)
93 return ERR_PTR(err);
94 }
Miklos Szeredid101a122016-03-26 16:14:37 -040095 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030096 }
Miklos Szeredid101a122016-03-26 16:14:37 -040097
98 real = ovl_dentry_lower(dentry);
99 if (!real)
100 goto bug;
101
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100102 /* Handle recursion */
103 real = d_real(real, inode, open_flags);
104
Miklos Szeredid101a122016-03-26 16:14:37 -0400105 if (!inode || inode == d_inode(real))
106 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400107bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200108 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400109 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
110 return dentry;
111}
112
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200113static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
114{
115 struct ovl_entry *oe = dentry->d_fsdata;
116 unsigned int i;
117 int ret = 1;
118
119 for (i = 0; i < oe->numlower; i++) {
120 struct dentry *d = oe->lowerstack[i].dentry;
121
122 if (d->d_flags & DCACHE_OP_REVALIDATE) {
123 ret = d->d_op->d_revalidate(d, flags);
124 if (ret < 0)
125 return ret;
126 if (!ret) {
127 if (!(flags & LOOKUP_RCU))
128 d_invalidate(d);
129 return -ESTALE;
130 }
131 }
132 }
133 return 1;
134}
135
136static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
137{
138 struct ovl_entry *oe = dentry->d_fsdata;
139 unsigned int i;
140 int ret = 1;
141
142 for (i = 0; i < oe->numlower; i++) {
143 struct dentry *d = oe->lowerstack[i].dentry;
144
145 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
146 ret = d->d_op->d_weak_revalidate(d, flags);
147 if (ret <= 0)
148 break;
149 }
150 }
151 return ret;
152}
153
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200154static const struct dentry_operations ovl_dentry_operations = {
155 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400156 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200157};
158
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200159static const struct dentry_operations ovl_reval_dentry_operations = {
160 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400161 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200162 .d_revalidate = ovl_dentry_revalidate,
163 .d_weak_revalidate = ovl_dentry_weak_revalidate,
164};
165
Amir Goldstein13cf1992017-06-12 09:54:40 +0300166static struct kmem_cache *ovl_inode_cachep;
167
168static struct inode *ovl_alloc_inode(struct super_block *sb)
169{
170 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
171
Miklos Szeredicf31c462017-07-04 22:03:16 +0200172 oi->redirect = NULL;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200173 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200174 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200175 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300176 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200177
Amir Goldstein13cf1992017-06-12 09:54:40 +0300178 return &oi->vfs_inode;
179}
180
181static void ovl_i_callback(struct rcu_head *head)
182{
183 struct inode *inode = container_of(head, struct inode, i_rcu);
184
185 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
186}
187
188static void ovl_destroy_inode(struct inode *inode)
189{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200190 struct ovl_inode *oi = OVL_I(inode);
191
192 dput(oi->__upperdentry);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200193 kfree(oi->redirect);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300194 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200195
Amir Goldstein13cf1992017-06-12 09:54:40 +0300196 call_rcu(&inode->i_rcu, ovl_i_callback);
197}
198
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199static void ovl_put_super(struct super_block *sb)
200{
201 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100202 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200203
204 dput(ufs->workdir);
205 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100206 for (i = 0; i < ufs->numlower; i++)
207 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300208 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200209
Erez Zadokf45827e82014-10-24 00:14:38 +0200210 kfree(ufs->config.lowerdir);
211 kfree(ufs->config.upperdir);
212 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200213 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200214 kfree(ufs);
215}
216
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200217static int ovl_sync_fs(struct super_block *sb, int wait)
218{
219 struct ovl_fs *ufs = sb->s_fs_info;
220 struct super_block *upper_sb;
221 int ret;
222
223 if (!ufs->upper_mnt)
224 return 0;
225 upper_sb = ufs->upper_mnt->mnt_sb;
226 if (!upper_sb->s_op->sync_fs)
227 return 0;
228
229 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
230 down_read(&upper_sb->s_umount);
231 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
232 up_read(&upper_sb->s_umount);
233 return ret;
234}
235
Andy Whitcroftcc259632014-10-24 00:14:38 +0200236/**
237 * ovl_statfs
238 * @sb: The overlayfs super block
239 * @buf: The struct kstatfs to fill in with stats
240 *
241 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100242 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200243 */
244static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
245{
246 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
247 struct dentry *root_dentry = dentry->d_sb->s_root;
248 struct path path;
249 int err;
250
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100251 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200252
253 err = vfs_statfs(&path, buf);
254 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100255 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200256 buf->f_type = OVERLAYFS_SUPER_MAGIC;
257 }
258
259 return err;
260}
261
Erez Zadokf45827e82014-10-24 00:14:38 +0200262/**
263 * ovl_show_options
264 *
265 * Prints the mount options for a given superblock.
266 * Returns zero; does not fail.
267 */
268static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
269{
270 struct super_block *sb = dentry->d_sb;
271 struct ovl_fs *ufs = sb->s_fs_info;
272
Kees Cooka068acf2015-09-04 15:44:57 -0700273 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100274 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700275 seq_show_option(m, "upperdir", ufs->config.upperdir);
276 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100277 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200278 if (ufs->config.default_permissions)
279 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200280 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
281 seq_printf(m, ",redirect_dir=%s",
282 ufs->config.redirect_dir ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200283 return 0;
284}
285
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900286static int ovl_remount(struct super_block *sb, int *flags, char *data)
287{
288 struct ovl_fs *ufs = sb->s_fs_info;
289
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200290 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900291 return -EROFS;
292
293 return 0;
294}
295
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200296static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300297 .alloc_inode = ovl_alloc_inode,
298 .destroy_inode = ovl_destroy_inode,
299 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200300 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200301 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200302 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200303 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900304 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305};
306
307enum {
308 OPT_LOWERDIR,
309 OPT_UPPERDIR,
310 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200311 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100312 OPT_REDIRECT_DIR_ON,
313 OPT_REDIRECT_DIR_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200314 OPT_ERR,
315};
316
317static const match_table_t ovl_tokens = {
318 {OPT_LOWERDIR, "lowerdir=%s"},
319 {OPT_UPPERDIR, "upperdir=%s"},
320 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200321 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100322 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
323 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200324 {OPT_ERR, NULL}
325};
326
Miklos Szeredi91c77942014-11-20 16:40:00 +0100327static char *ovl_next_opt(char **s)
328{
329 char *sbegin = *s;
330 char *p;
331
332 if (sbegin == NULL)
333 return NULL;
334
335 for (p = sbegin; *p; p++) {
336 if (*p == '\\') {
337 p++;
338 if (!*p)
339 break;
340 } else if (*p == ',') {
341 *p = '\0';
342 *s = p + 1;
343 return sbegin;
344 }
345 }
346 *s = NULL;
347 return sbegin;
348}
349
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350static int ovl_parse_opt(char *opt, struct ovl_config *config)
351{
352 char *p;
353
Miklos Szeredi91c77942014-11-20 16:40:00 +0100354 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200355 int token;
356 substring_t args[MAX_OPT_ARGS];
357
358 if (!*p)
359 continue;
360
361 token = match_token(p, ovl_tokens, args);
362 switch (token) {
363 case OPT_UPPERDIR:
364 kfree(config->upperdir);
365 config->upperdir = match_strdup(&args[0]);
366 if (!config->upperdir)
367 return -ENOMEM;
368 break;
369
370 case OPT_LOWERDIR:
371 kfree(config->lowerdir);
372 config->lowerdir = match_strdup(&args[0]);
373 if (!config->lowerdir)
374 return -ENOMEM;
375 break;
376
377 case OPT_WORKDIR:
378 kfree(config->workdir);
379 config->workdir = match_strdup(&args[0]);
380 if (!config->workdir)
381 return -ENOMEM;
382 break;
383
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200384 case OPT_DEFAULT_PERMISSIONS:
385 config->default_permissions = true;
386 break;
387
Miklos Szeredia6c60652016-12-16 11:02:56 +0100388 case OPT_REDIRECT_DIR_ON:
389 config->redirect_dir = true;
390 break;
391
392 case OPT_REDIRECT_DIR_OFF:
393 config->redirect_dir = false;
394 break;
395
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200396 default:
hujianyangbead55e2015-01-15 13:17:36 +0800397 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200398 return -EINVAL;
399 }
400 }
hujianyang71cbad72015-01-15 13:20:57 +0800401
402 /* Workdir is useless in non-upper mount */
403 if (!config->upperdir && config->workdir) {
404 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
405 config->workdir);
406 kfree(config->workdir);
407 config->workdir = NULL;
408 }
409
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200410 return 0;
411}
412
413#define OVL_WORKDIR_NAME "work"
414
415static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
416 struct dentry *dentry)
417{
418 struct inode *dir = dentry->d_inode;
419 struct dentry *work;
420 int err;
421 bool retried = false;
422
423 err = mnt_want_write(mnt);
424 if (err)
425 return ERR_PTR(err);
426
Al Viro59551022016-01-22 15:40:57 -0500427 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200428retry:
429 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
430 strlen(OVL_WORKDIR_NAME));
431
432 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200433 struct iattr attr = {
434 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000435 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200436 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200437
438 if (work->d_inode) {
439 err = -EEXIST;
440 if (retried)
441 goto out_dput;
442
443 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200444 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445 dput(work);
446 goto retry;
447 }
448
Al Viro32a3d842016-12-04 17:33:17 +0000449 err = ovl_create_real(dir, work,
450 &(struct cattr){.mode = S_IFDIR | 0},
451 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200452 if (err)
453 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200454
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200455 /*
456 * Try to remove POSIX ACL xattrs from workdir. We are good if:
457 *
458 * a) success (there was a POSIX ACL xattr and was removed)
459 * b) -ENODATA (there was no POSIX ACL xattr)
460 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
461 *
462 * There are various other error values that could effectively
463 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
464 * if the xattr name is too long), but the set of filesystems
465 * allowed as upper are limited to "normal" ones, where checking
466 * for the above two errors is sufficient.
467 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200468 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200469 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200470 goto out_dput;
471
472 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200473 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200474 goto out_dput;
475
476 /* Clear any inherited mode bits */
477 inode_lock(work->d_inode);
478 err = notify_change(work, &attr, NULL);
479 inode_unlock(work->d_inode);
480 if (err)
481 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200482 }
483out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500484 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200485 mnt_drop_write(mnt);
486
487 return work;
488
489out_dput:
490 dput(work);
491 work = ERR_PTR(err);
492 goto out_unlock;
493}
494
Miklos Szeredi91c77942014-11-20 16:40:00 +0100495static void ovl_unescape(char *s)
496{
497 char *d = s;
498
499 for (;; s++, d++) {
500 if (*s == '\\')
501 s++;
502 *d = *s;
503 if (!*s)
504 break;
505 }
506}
507
Miklos Szerediab508822014-12-13 00:59:49 +0100508static int ovl_mount_dir_noesc(const char *name, struct path *path)
509{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100510 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100511
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100512 if (!*name) {
513 pr_err("overlayfs: empty lowerdir\n");
514 goto out;
515 }
Miklos Szerediab508822014-12-13 00:59:49 +0100516 err = kern_path(name, LOOKUP_FOLLOW, path);
517 if (err) {
518 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
519 goto out;
520 }
521 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200522 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100523 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
524 goto out_put;
525 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100526 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100527 pr_err("overlayfs: '%s' not a directory\n", name);
528 goto out_put;
529 }
530 return 0;
531
532out_put:
533 path_put(path);
534out:
535 return err;
536}
537
538static int ovl_mount_dir(const char *name, struct path *path)
539{
540 int err = -ENOMEM;
541 char *tmp = kstrdup(name, GFP_KERNEL);
542
543 if (tmp) {
544 ovl_unescape(tmp);
545 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200546
547 if (!err)
548 if (ovl_dentry_remote(path->dentry)) {
549 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
550 tmp);
551 path_put(path);
552 err = -EINVAL;
553 }
Miklos Szerediab508822014-12-13 00:59:49 +0100554 kfree(tmp);
555 }
556 return err;
557}
558
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100559static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
560 const char *name)
561{
562 struct kstatfs statfs;
563 int err = vfs_statfs(path, &statfs);
564
565 if (err)
566 pr_err("overlayfs: statfs failed on '%s'\n", name);
567 else
568 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
569
570 return err;
571}
572
573static int ovl_lower_dir(const char *name, struct path *path,
574 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100575{
576 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100577
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100578 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100579 if (err)
580 goto out;
581
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100582 err = ovl_check_namelen(path, ofs, name);
583 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100584 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100585
Miklos Szerediab508822014-12-13 00:59:49 +0100586 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
587
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200588 if (ovl_dentry_remote(path->dentry))
589 *remote = true;
590
Miklos Szerediab508822014-12-13 00:59:49 +0100591 return 0;
592
593out_put:
594 path_put(path);
595out:
596 return err;
597}
598
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200599/* Workdir should not be subdir of upperdir and vice versa */
600static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
601{
602 bool ok = false;
603
604 if (workdir != upperdir) {
605 ok = (lock_rename(workdir, upperdir) == NULL);
606 unlock_rename(workdir, upperdir);
607 }
608 return ok;
609}
610
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100611static unsigned int ovl_split_lowerdirs(char *str)
612{
613 unsigned int ctr = 1;
614 char *s, *d;
615
616 for (s = d = str;; s++, d++) {
617 if (*s == '\\') {
618 s++;
619 } else if (*s == ':') {
620 *d = '\0';
621 ctr++;
622 continue;
623 }
624 *d = *s;
625 if (!*s)
626 break;
627 }
628 return ctr;
629}
630
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200631static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200632ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
633 struct dentry *dentry, struct inode *inode,
634 const char *name, void *buffer, size_t size)
635{
636 return ovl_xattr_get(dentry, handler->name, buffer, size);
637}
638
639static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200640ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
641 struct dentry *dentry, struct inode *inode,
642 const char *name, const void *value,
643 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200644{
645 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200646 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200647 struct posix_acl *acl = NULL;
648 int err;
649
650 /* Check that everything is OK before copy-up */
651 if (value) {
652 acl = posix_acl_from_xattr(&init_user_ns, value, size);
653 if (IS_ERR(acl))
654 return PTR_ERR(acl);
655 }
656 err = -EOPNOTSUPP;
657 if (!IS_POSIXACL(d_inode(workdir)))
658 goto out_acl_release;
659 if (!realinode->i_op->set_acl)
660 goto out_acl_release;
661 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
662 err = acl ? -EACCES : 0;
663 goto out_acl_release;
664 }
665 err = -EPERM;
666 if (!inode_owner_or_capable(inode))
667 goto out_acl_release;
668
669 posix_acl_release(acl);
670
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100671 /*
672 * Check if sgid bit needs to be cleared (actual setacl operation will
673 * be done with mounter's capabilities and so that won't do it for us).
674 */
675 if (unlikely(inode->i_mode & S_ISGID) &&
676 handler->flags == ACL_TYPE_ACCESS &&
677 !in_group_p(inode->i_gid) &&
678 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
679 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
680
681 err = ovl_setattr(dentry, &iattr);
682 if (err)
683 return err;
684 }
685
Miklos Szeredice315132016-09-01 11:12:00 +0200686 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
687 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200688 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200689
690 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200691
692out_acl_release:
693 posix_acl_release(acl);
694 return err;
695}
696
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200697static int ovl_own_xattr_get(const struct xattr_handler *handler,
698 struct dentry *dentry, struct inode *inode,
699 const char *name, void *buffer, size_t size)
700{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200701 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200702}
703
Miklos Szeredid837a492016-07-29 12:05:24 +0200704static int ovl_own_xattr_set(const struct xattr_handler *handler,
705 struct dentry *dentry, struct inode *inode,
706 const char *name, const void *value,
707 size_t size, int flags)
708{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200709 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200710}
711
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200712static int ovl_other_xattr_get(const struct xattr_handler *handler,
713 struct dentry *dentry, struct inode *inode,
714 const char *name, void *buffer, size_t size)
715{
716 return ovl_xattr_get(dentry, name, buffer, size);
717}
718
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200719static int ovl_other_xattr_set(const struct xattr_handler *handler,
720 struct dentry *dentry, struct inode *inode,
721 const char *name, const void *value,
722 size_t size, int flags)
723{
724 return ovl_xattr_set(dentry, name, value, size, flags);
725}
726
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200727static const struct xattr_handler __maybe_unused
728ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200729 .name = XATTR_NAME_POSIX_ACL_ACCESS,
730 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200731 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200732 .set = ovl_posix_acl_xattr_set,
733};
734
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200735static const struct xattr_handler __maybe_unused
736ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200737 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
738 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200739 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200740 .set = ovl_posix_acl_xattr_set,
741};
742
743static const struct xattr_handler ovl_own_xattr_handler = {
744 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200745 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200746 .set = ovl_own_xattr_set,
747};
748
749static const struct xattr_handler ovl_other_xattr_handler = {
750 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200751 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200752 .set = ovl_other_xattr_set,
753};
754
755static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200756#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200757 &ovl_posix_acl_access_xattr_handler,
758 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200759#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200760 &ovl_own_xattr_handler,
761 &ovl_other_xattr_handler,
762 NULL
763};
764
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200765static int ovl_fill_super(struct super_block *sb, void *data, int silent)
766{
Kees Cook33006cd2017-03-29 14:02:19 -0700767 struct path upperpath = { };
768 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200769 struct dentry *root_dentry;
770 struct ovl_entry *oe;
771 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100772 struct path *stack = NULL;
773 char *lowertmp;
774 char *lower;
775 unsigned int numlower;
776 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100777 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200778 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300779 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200780 int err;
781
Erez Zadokf45827e82014-10-24 00:14:38 +0200782 err = -ENOMEM;
783 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
784 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200785 goto out;
786
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100787 ufs->config.redirect_dir = ovl_redirect_dir_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200788 err = ovl_parse_opt((char *) data, &ufs->config);
789 if (err)
790 goto out_free_config;
791
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200792 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100793 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300794 if (!silent)
795 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200796 goto out_free_config;
797 }
798
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100799 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100800 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100801 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100802 if (!ufs->config.workdir) {
803 pr_err("overlayfs: missing 'workdir'\n");
804 goto out_free_config;
805 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200806
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100807 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
808 if (err)
809 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100810
hujianyang71cbad72015-01-15 13:20:57 +0800811 /* Upper fs should not be r/o */
812 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
813 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
814 err = -EINVAL;
815 goto out_put_upperpath;
816 }
817
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100818 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
819 if (err)
820 goto out_put_upperpath;
821
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100822 err = ovl_mount_dir(ufs->config.workdir, &workpath);
823 if (err)
824 goto out_put_upperpath;
825
hujianyang2f83fd82015-01-06 12:52:13 +0800826 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100827 if (upperpath.mnt != workpath.mnt) {
828 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
829 goto out_put_workpath;
830 }
831 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
832 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
833 goto out_put_workpath;
834 }
835 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100836 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100837 err = -ENOMEM;
838 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
839 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100840 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200841
842 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100843 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800844 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100845 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800846 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100847 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800848 } else if (!ufs->config.upperdir && stacklen == 1) {
849 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
850 goto out_free_lowertmp;
851 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100852
Geliang Tang313684c2016-11-18 23:02:46 +0800853 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100854 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
855 if (!stack)
856 goto out_free_lowertmp;
857
Geliang Tang313684c2016-11-18 23:02:46 +0800858 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100859 lower = lowertmp;
860 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100861 err = ovl_lower_dir(lower, &stack[numlower], ufs,
862 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100863 if (err)
864 goto out_put_lowerpath;
865
866 lower = strchr(lower, '\0') + 1;
867 }
868
869 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100870 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200871 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
872 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100873 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200874 }
875
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100876 if (ufs->config.upperdir) {
877 ufs->upper_mnt = clone_private_mount(&upperpath);
878 err = PTR_ERR(ufs->upper_mnt);
879 if (IS_ERR(ufs->upper_mnt)) {
880 pr_err("overlayfs: failed to clone upperpath\n");
881 goto out_put_lowerpath;
882 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200883 /* Don't inherit atime flags */
884 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
885
886 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100887
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100888 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
889 err = PTR_ERR(ufs->workdir);
890 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200891 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
892 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
893 sb->s_flags |= MS_RDONLY;
894 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100895 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500896
897 /*
898 * Upper should support d_type, else whiteouts are visible.
899 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400900 * iterate_dir() on workdir. This check requires successful
901 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500902 */
Vivek Goyal21765192016-05-20 09:04:26 -0400903 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200904 struct dentry *temp;
905
Vivek Goyal21765192016-05-20 09:04:26 -0400906 err = ovl_check_d_type_supported(&workpath);
907 if (err < 0)
908 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500909
Vivek Goyale7c0b592016-07-01 10:02:44 -0400910 /*
911 * We allowed this configuration and don't want to
912 * break users over kernel upgrade. So warn instead
913 * of erroring out.
914 */
915 if (!err)
916 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200917
918 /* Check if upper/work fs supports O_TMPFILE */
919 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
920 ufs->tmpfile = !IS_ERR(temp);
921 if (ufs->tmpfile)
922 dput(temp);
923 else
924 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +0300925
926 /*
927 * Check if upper/work fs supports trusted.overlay.*
928 * xattr
929 */
930 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
931 "0", 1, 0);
932 if (err) {
933 ufs->noxattr = true;
934 pr_warn("overlayfs: upper fs does not support xattr.\n");
935 } else {
936 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
937 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500938 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200939 }
940
hujianyang2f83fd82015-01-06 12:52:13 +0800941 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100942 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +0100943 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100944 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100945 for (i = 0; i < numlower; i++) {
946 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +0100947
hujianyang2f83fd82015-01-06 12:52:13 +0800948 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100949 if (IS_ERR(mnt)) {
950 pr_err("overlayfs: failed to clone lowerpath\n");
951 goto out_put_lower_mnt;
952 }
953 /*
954 * Make lower_mnt R/O. That way fchmod/fchown on lower file
955 * will fail instead of modifying lower fs.
956 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200957 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100958
959 ufs->lower_mnt[ufs->numlower] = mnt;
960 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400961
962 /* Check if all lower layers are on same sb */
963 if (i == 0)
964 ufs->same_sb = mnt->mnt_sb;
965 else if (ufs->same_sb != mnt->mnt_sb)
966 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200967 }
968
hujianyang71cbad72015-01-15 13:20:57 +0800969 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
970 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200971 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400972 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
973 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200974
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200975 if (remote)
976 sb->s_d_op = &ovl_reval_dentry_operations;
977 else
978 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200979
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300980 ufs->creator_cred = cred = prepare_creds();
981 if (!cred)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200982 goto out_put_lower_mnt;
983
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300984 /* Never override disk quota limits or use reserved space */
985 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
986
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200987 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100988 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100989 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200990 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200991
Vivek Goyal655042c2016-10-14 03:03:36 +0200992 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
993 sb->s_op = &ovl_super_operations;
994 sb->s_xattr = ovl_xattr_handlers;
995 sb->s_fs_info = ufs;
996 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
997
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100998 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200999 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001000 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001001
1002 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001003 for (i = 0; i < numlower; i++)
1004 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001005 path_put(&workpath);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001006 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001007
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001008 if (upperpath.dentry) {
Miklos Szeredi13c72072017-07-04 22:03:16 +02001009 if (ovl_is_impuredir(upperpath.dentry))
1010 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001011 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001012 for (i = 0; i < numlower; i++) {
1013 oe->lowerstack[i].dentry = stack[i].dentry;
1014 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1015 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001016 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001017
1018 root_dentry->d_fsdata = oe;
1019
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001020 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1021 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001022
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001023 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001024
1025 return 0;
1026
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001027out_free_oe:
1028 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001029out_put_cred:
1030 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001031out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001032 for (i = 0; i < ufs->numlower; i++)
1033 mntput(ufs->lower_mnt[i]);
1034 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001035out_put_workdir:
1036 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001037 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001038out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001039 for (i = 0; i < numlower; i++)
1040 path_put(&stack[i]);
1041 kfree(stack);
1042out_free_lowertmp:
1043 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001044out_put_workpath:
1045 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001046out_put_upperpath:
1047 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001048out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001049 kfree(ufs->config.lowerdir);
1050 kfree(ufs->config.upperdir);
1051 kfree(ufs->config.workdir);
1052 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001053out:
1054 return err;
1055}
1056
1057static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1058 const char *dev_name, void *raw_data)
1059{
1060 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1061}
1062
1063static struct file_system_type ovl_fs_type = {
1064 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001065 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001066 .mount = ovl_mount,
1067 .kill_sb = kill_anon_super,
1068};
Miklos Szeredief94b182014-11-20 16:39:59 +01001069MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001070
Amir Goldstein13cf1992017-06-12 09:54:40 +03001071static void ovl_inode_init_once(void *foo)
1072{
1073 struct ovl_inode *oi = foo;
1074
1075 inode_init_once(&oi->vfs_inode);
1076}
1077
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001078static int __init ovl_init(void)
1079{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001080 int err;
1081
1082 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1083 sizeof(struct ovl_inode), 0,
1084 (SLAB_RECLAIM_ACCOUNT|
1085 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1086 ovl_inode_init_once);
1087 if (ovl_inode_cachep == NULL)
1088 return -ENOMEM;
1089
1090 err = register_filesystem(&ovl_fs_type);
1091 if (err)
1092 kmem_cache_destroy(ovl_inode_cachep);
1093
1094 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001095}
1096
1097static void __exit ovl_exit(void)
1098{
1099 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001100
1101 /*
1102 * Make sure all delayed rcu free inodes are flushed before we
1103 * destroy cache.
1104 */
1105 rcu_barrier();
1106 kmem_cache_destroy(ovl_inode_cachep);
1107
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001108}
1109
1110module_init(ovl_init);
1111module_exit(ovl_exit);