blob: 40d49390feb996c41e3b5150df172c58b2d728bc [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
Amir Goldstein02bcd152017-06-21 15:28:36 +030037static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
38module_param_named(index, ovl_index_def, bool, 0644);
39MODULE_PARM_DESC(ovl_index_def,
40 "Default to on or off for the inodes index feature");
41
Miklos Szeredie9be9d52014-10-24 00:14:38 +020042static void ovl_dentry_release(struct dentry *dentry)
43{
44 struct ovl_entry *oe = dentry->d_fsdata;
45
46 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +010047 unsigned int i;
48
Miklos Szeredidd662662014-12-13 00:59:43 +010049 for (i = 0; i < oe->numlower; i++)
50 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020051 kfree_rcu(oe, rcu);
52 }
53}
54
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030055static int ovl_check_append_only(struct inode *inode, int flag)
56{
57 /*
58 * This test was moot in vfs may_open() because overlay inode does
59 * not have the S_APPEND flag, so re-check on real upper inode
60 */
61 if (IS_APPEND(inode)) {
62 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
63 return -EPERM;
64 if (flag & O_TRUNC)
65 return -EPERM;
66 }
67
68 return 0;
69}
70
Miklos Szeredi2d902672016-06-30 08:53:27 +020071static struct dentry *ovl_d_real(struct dentry *dentry,
72 const struct inode *inode,
73 unsigned int open_flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040074{
75 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030076 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040077
Miklos Szeredica4c8a32016-12-16 11:02:55 +010078 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040079 if (!inode || inode == d_inode(dentry))
80 return dentry;
81 goto bug;
82 }
83
Miklos Szeredi2d902672016-06-30 08:53:27 +020084 if (d_is_negative(dentry))
85 return dentry;
86
87 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030088 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +020089 if (err)
90 return ERR_PTR(err);
91 }
92
Miklos Szeredid101a122016-03-26 16:14:37 -040093 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030094 if (real && (!inode || inode == d_inode(real))) {
95 if (!inode) {
96 err = ovl_check_append_only(d_inode(real), open_flags);
97 if (err)
98 return ERR_PTR(err);
99 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400100 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300101 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400102
103 real = ovl_dentry_lower(dentry);
104 if (!real)
105 goto bug;
106
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100107 /* Handle recursion */
108 real = d_real(real, inode, open_flags);
109
Miklos Szeredid101a122016-03-26 16:14:37 -0400110 if (!inode || inode == d_inode(real))
111 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400112bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200113 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400114 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
115 return dentry;
116}
117
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200118static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
119{
120 struct ovl_entry *oe = dentry->d_fsdata;
121 unsigned int i;
122 int ret = 1;
123
124 for (i = 0; i < oe->numlower; i++) {
125 struct dentry *d = oe->lowerstack[i].dentry;
126
127 if (d->d_flags & DCACHE_OP_REVALIDATE) {
128 ret = d->d_op->d_revalidate(d, flags);
129 if (ret < 0)
130 return ret;
131 if (!ret) {
132 if (!(flags & LOOKUP_RCU))
133 d_invalidate(d);
134 return -ESTALE;
135 }
136 }
137 }
138 return 1;
139}
140
141static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
142{
143 struct ovl_entry *oe = dentry->d_fsdata;
144 unsigned int i;
145 int ret = 1;
146
147 for (i = 0; i < oe->numlower; i++) {
148 struct dentry *d = oe->lowerstack[i].dentry;
149
150 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
151 ret = d->d_op->d_weak_revalidate(d, flags);
152 if (ret <= 0)
153 break;
154 }
155 }
156 return ret;
157}
158
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200159static const struct dentry_operations ovl_dentry_operations = {
160 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400161 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200162};
163
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200164static const struct dentry_operations ovl_reval_dentry_operations = {
165 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400166 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200167 .d_revalidate = ovl_dentry_revalidate,
168 .d_weak_revalidate = ovl_dentry_weak_revalidate,
169};
170
Amir Goldstein13cf1992017-06-12 09:54:40 +0300171static struct kmem_cache *ovl_inode_cachep;
172
173static struct inode *ovl_alloc_inode(struct super_block *sb)
174{
175 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
176
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200177 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200178 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200179 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200180 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200181 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200182 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300183 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200184
Amir Goldstein13cf1992017-06-12 09:54:40 +0300185 return &oi->vfs_inode;
186}
187
188static void ovl_i_callback(struct rcu_head *head)
189{
190 struct inode *inode = container_of(head, struct inode, i_rcu);
191
192 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
193}
194
195static void ovl_destroy_inode(struct inode *inode)
196{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200197 struct ovl_inode *oi = OVL_I(inode);
198
199 dput(oi->__upperdentry);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200200 kfree(oi->redirect);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200201 ovl_dir_cache_free(inode);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300202 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200203
Amir Goldstein13cf1992017-06-12 09:54:40 +0300204 call_rcu(&inode->i_rcu, ovl_i_callback);
205}
206
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207static void ovl_put_super(struct super_block *sb)
208{
209 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100210 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200211
Amir Goldstein02bcd152017-06-21 15:28:36 +0300212 dput(ufs->indexdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200213 dput(ufs->workdir);
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300214 ovl_inuse_unlock(ufs->workbasedir);
215 dput(ufs->workbasedir);
216 if (ufs->upper_mnt)
217 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200218 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100219 for (i = 0; i < ufs->numlower; i++)
220 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300221 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200222
Erez Zadokf45827e82014-10-24 00:14:38 +0200223 kfree(ufs->config.lowerdir);
224 kfree(ufs->config.upperdir);
225 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200226 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200227 kfree(ufs);
228}
229
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200230static int ovl_sync_fs(struct super_block *sb, int wait)
231{
232 struct ovl_fs *ufs = sb->s_fs_info;
233 struct super_block *upper_sb;
234 int ret;
235
236 if (!ufs->upper_mnt)
237 return 0;
238 upper_sb = ufs->upper_mnt->mnt_sb;
239 if (!upper_sb->s_op->sync_fs)
240 return 0;
241
242 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
243 down_read(&upper_sb->s_umount);
244 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
245 up_read(&upper_sb->s_umount);
246 return ret;
247}
248
Andy Whitcroftcc259632014-10-24 00:14:38 +0200249/**
250 * ovl_statfs
251 * @sb: The overlayfs super block
252 * @buf: The struct kstatfs to fill in with stats
253 *
254 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100255 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200256 */
257static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
258{
259 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
260 struct dentry *root_dentry = dentry->d_sb->s_root;
261 struct path path;
262 int err;
263
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100264 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200265
266 err = vfs_statfs(&path, buf);
267 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100268 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200269 buf->f_type = OVERLAYFS_SUPER_MAGIC;
270 }
271
272 return err;
273}
274
Amir Goldstein02bcd152017-06-21 15:28:36 +0300275/* Will this overlay be forced to mount/remount ro? */
276static bool ovl_force_readonly(struct ovl_fs *ufs)
277{
278 return (!ufs->upper_mnt || !ufs->workdir);
279}
280
Erez Zadokf45827e82014-10-24 00:14:38 +0200281/**
282 * ovl_show_options
283 *
284 * Prints the mount options for a given superblock.
285 * Returns zero; does not fail.
286 */
287static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
288{
289 struct super_block *sb = dentry->d_sb;
290 struct ovl_fs *ufs = sb->s_fs_info;
291
Kees Cooka068acf2015-09-04 15:44:57 -0700292 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100293 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700294 seq_show_option(m, "upperdir", ufs->config.upperdir);
295 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100296 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200297 if (ufs->config.default_permissions)
298 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200299 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
300 seq_printf(m, ",redirect_dir=%s",
301 ufs->config.redirect_dir ? "on" : "off");
Amir Goldstein02bcd152017-06-21 15:28:36 +0300302 if (ufs->config.index != ovl_index_def)
303 seq_printf(m, ",index=%s",
304 ufs->config.index ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200305 return 0;
306}
307
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900308static int ovl_remount(struct super_block *sb, int *flags, char *data)
309{
310 struct ovl_fs *ufs = sb->s_fs_info;
311
Amir Goldstein02bcd152017-06-21 15:28:36 +0300312 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900313 return -EROFS;
314
315 return 0;
316}
317
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200318static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300319 .alloc_inode = ovl_alloc_inode,
320 .destroy_inode = ovl_destroy_inode,
321 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200323 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200324 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200325 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900326 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200327};
328
329enum {
330 OPT_LOWERDIR,
331 OPT_UPPERDIR,
332 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200333 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100334 OPT_REDIRECT_DIR_ON,
335 OPT_REDIRECT_DIR_OFF,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300336 OPT_INDEX_ON,
337 OPT_INDEX_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338 OPT_ERR,
339};
340
341static const match_table_t ovl_tokens = {
342 {OPT_LOWERDIR, "lowerdir=%s"},
343 {OPT_UPPERDIR, "upperdir=%s"},
344 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200345 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100346 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
347 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300348 {OPT_INDEX_ON, "index=on"},
349 {OPT_INDEX_OFF, "index=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350 {OPT_ERR, NULL}
351};
352
Miklos Szeredi91c77942014-11-20 16:40:00 +0100353static char *ovl_next_opt(char **s)
354{
355 char *sbegin = *s;
356 char *p;
357
358 if (sbegin == NULL)
359 return NULL;
360
361 for (p = sbegin; *p; p++) {
362 if (*p == '\\') {
363 p++;
364 if (!*p)
365 break;
366 } else if (*p == ',') {
367 *p = '\0';
368 *s = p + 1;
369 return sbegin;
370 }
371 }
372 *s = NULL;
373 return sbegin;
374}
375
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200376static int ovl_parse_opt(char *opt, struct ovl_config *config)
377{
378 char *p;
379
Miklos Szeredi91c77942014-11-20 16:40:00 +0100380 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200381 int token;
382 substring_t args[MAX_OPT_ARGS];
383
384 if (!*p)
385 continue;
386
387 token = match_token(p, ovl_tokens, args);
388 switch (token) {
389 case OPT_UPPERDIR:
390 kfree(config->upperdir);
391 config->upperdir = match_strdup(&args[0]);
392 if (!config->upperdir)
393 return -ENOMEM;
394 break;
395
396 case OPT_LOWERDIR:
397 kfree(config->lowerdir);
398 config->lowerdir = match_strdup(&args[0]);
399 if (!config->lowerdir)
400 return -ENOMEM;
401 break;
402
403 case OPT_WORKDIR:
404 kfree(config->workdir);
405 config->workdir = match_strdup(&args[0]);
406 if (!config->workdir)
407 return -ENOMEM;
408 break;
409
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200410 case OPT_DEFAULT_PERMISSIONS:
411 config->default_permissions = true;
412 break;
413
Miklos Szeredia6c60652016-12-16 11:02:56 +0100414 case OPT_REDIRECT_DIR_ON:
415 config->redirect_dir = true;
416 break;
417
418 case OPT_REDIRECT_DIR_OFF:
419 config->redirect_dir = false;
420 break;
421
Amir Goldstein02bcd152017-06-21 15:28:36 +0300422 case OPT_INDEX_ON:
423 config->index = true;
424 break;
425
426 case OPT_INDEX_OFF:
427 config->index = false;
428 break;
429
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200430 default:
hujianyangbead55e2015-01-15 13:17:36 +0800431 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200432 return -EINVAL;
433 }
434 }
hujianyang71cbad72015-01-15 13:20:57 +0800435
436 /* Workdir is useless in non-upper mount */
437 if (!config->upperdir && config->workdir) {
438 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
439 config->workdir);
440 kfree(config->workdir);
441 config->workdir = NULL;
442 }
443
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200444 return 0;
445}
446
447#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300448#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200449
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300450static struct dentry *ovl_workdir_create(struct super_block *sb,
451 struct ovl_fs *ufs,
452 struct dentry *dentry,
453 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200454{
455 struct inode *dir = dentry->d_inode;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300456 struct vfsmount *mnt = ufs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200457 struct dentry *work;
458 int err;
459 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300460 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200461
462 err = mnt_want_write(mnt);
463 if (err)
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300464 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200465
Al Viro59551022016-01-22 15:40:57 -0500466 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300467 locked = true;
468
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200469retry:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300470 work = lookup_one_len(name, dentry, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200471
472 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200473 struct iattr attr = {
474 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000475 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200476 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200477
478 if (work->d_inode) {
479 err = -EEXIST;
480 if (retried)
481 goto out_dput;
482
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300483 if (persist)
484 goto out_unlock;
485
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200486 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200487 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200488 dput(work);
489 goto retry;
490 }
491
Al Viro32a3d842016-12-04 17:33:17 +0000492 err = ovl_create_real(dir, work,
493 &(struct cattr){.mode = S_IFDIR | 0},
494 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200495 if (err)
496 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200497
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200498 /*
499 * Try to remove POSIX ACL xattrs from workdir. We are good if:
500 *
501 * a) success (there was a POSIX ACL xattr and was removed)
502 * b) -ENODATA (there was no POSIX ACL xattr)
503 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
504 *
505 * There are various other error values that could effectively
506 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
507 * if the xattr name is too long), but the set of filesystems
508 * allowed as upper are limited to "normal" ones, where checking
509 * for the above two errors is sufficient.
510 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200511 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200512 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200513 goto out_dput;
514
515 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200516 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200517 goto out_dput;
518
519 /* Clear any inherited mode bits */
520 inode_lock(work->d_inode);
521 err = notify_change(work, &attr, NULL);
522 inode_unlock(work->d_inode);
523 if (err)
524 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300525 } else {
526 err = PTR_ERR(work);
527 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200528 }
529out_unlock:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200530 mnt_drop_write(mnt);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300531 if (locked)
532 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200533
534 return work;
535
536out_dput:
537 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300538out_err:
539 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
540 ufs->config.workdir, name, -err);
541 sb->s_flags |= MS_RDONLY;
542 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200543 goto out_unlock;
544}
545
Miklos Szeredi91c77942014-11-20 16:40:00 +0100546static void ovl_unescape(char *s)
547{
548 char *d = s;
549
550 for (;; s++, d++) {
551 if (*s == '\\')
552 s++;
553 *d = *s;
554 if (!*s)
555 break;
556 }
557}
558
Miklos Szerediab508822014-12-13 00:59:49 +0100559static int ovl_mount_dir_noesc(const char *name, struct path *path)
560{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100561 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100562
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100563 if (!*name) {
564 pr_err("overlayfs: empty lowerdir\n");
565 goto out;
566 }
Miklos Szerediab508822014-12-13 00:59:49 +0100567 err = kern_path(name, LOOKUP_FOLLOW, path);
568 if (err) {
569 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
570 goto out;
571 }
572 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200573 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100574 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
575 goto out_put;
576 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100577 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100578 pr_err("overlayfs: '%s' not a directory\n", name);
579 goto out_put;
580 }
581 return 0;
582
583out_put:
584 path_put(path);
585out:
586 return err;
587}
588
589static int ovl_mount_dir(const char *name, struct path *path)
590{
591 int err = -ENOMEM;
592 char *tmp = kstrdup(name, GFP_KERNEL);
593
594 if (tmp) {
595 ovl_unescape(tmp);
596 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200597
598 if (!err)
599 if (ovl_dentry_remote(path->dentry)) {
600 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
601 tmp);
602 path_put(path);
603 err = -EINVAL;
604 }
Miklos Szerediab508822014-12-13 00:59:49 +0100605 kfree(tmp);
606 }
607 return err;
608}
609
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100610static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
611 const char *name)
612{
613 struct kstatfs statfs;
614 int err = vfs_statfs(path, &statfs);
615
616 if (err)
617 pr_err("overlayfs: statfs failed on '%s'\n", name);
618 else
619 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
620
621 return err;
622}
623
624static int ovl_lower_dir(const char *name, struct path *path,
625 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100626{
627 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100628
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100629 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100630 if (err)
631 goto out;
632
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100633 err = ovl_check_namelen(path, ofs, name);
634 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100635 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100636
Miklos Szerediab508822014-12-13 00:59:49 +0100637 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
638
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200639 if (ovl_dentry_remote(path->dentry))
640 *remote = true;
641
Amir Goldstein02bcd152017-06-21 15:28:36 +0300642 /*
643 * The inodes index feature needs to encode and decode file
644 * handles, so it requires that all layers support them.
645 */
646 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
647 ofs->config.index = false;
648 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
649 }
650
Miklos Szerediab508822014-12-13 00:59:49 +0100651 return 0;
652
653out_put:
654 path_put(path);
655out:
656 return err;
657}
658
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200659/* Workdir should not be subdir of upperdir and vice versa */
660static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
661{
662 bool ok = false;
663
664 if (workdir != upperdir) {
665 ok = (lock_rename(workdir, upperdir) == NULL);
666 unlock_rename(workdir, upperdir);
667 }
668 return ok;
669}
670
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100671static unsigned int ovl_split_lowerdirs(char *str)
672{
673 unsigned int ctr = 1;
674 char *s, *d;
675
676 for (s = d = str;; s++, d++) {
677 if (*s == '\\') {
678 s++;
679 } else if (*s == ':') {
680 *d = '\0';
681 ctr++;
682 continue;
683 }
684 *d = *s;
685 if (!*s)
686 break;
687 }
688 return ctr;
689}
690
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200691static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200692ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
693 struct dentry *dentry, struct inode *inode,
694 const char *name, void *buffer, size_t size)
695{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200696 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200697}
698
699static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200700ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
701 struct dentry *dentry, struct inode *inode,
702 const char *name, const void *value,
703 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200704{
705 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200706 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200707 struct posix_acl *acl = NULL;
708 int err;
709
710 /* Check that everything is OK before copy-up */
711 if (value) {
712 acl = posix_acl_from_xattr(&init_user_ns, value, size);
713 if (IS_ERR(acl))
714 return PTR_ERR(acl);
715 }
716 err = -EOPNOTSUPP;
717 if (!IS_POSIXACL(d_inode(workdir)))
718 goto out_acl_release;
719 if (!realinode->i_op->set_acl)
720 goto out_acl_release;
721 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
722 err = acl ? -EACCES : 0;
723 goto out_acl_release;
724 }
725 err = -EPERM;
726 if (!inode_owner_or_capable(inode))
727 goto out_acl_release;
728
729 posix_acl_release(acl);
730
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100731 /*
732 * Check if sgid bit needs to be cleared (actual setacl operation will
733 * be done with mounter's capabilities and so that won't do it for us).
734 */
735 if (unlikely(inode->i_mode & S_ISGID) &&
736 handler->flags == ACL_TYPE_ACCESS &&
737 !in_group_p(inode->i_gid) &&
738 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
739 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
740
741 err = ovl_setattr(dentry, &iattr);
742 if (err)
743 return err;
744 }
745
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200746 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200747 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200748 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200749
750 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200751
752out_acl_release:
753 posix_acl_release(acl);
754 return err;
755}
756
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200757static int ovl_own_xattr_get(const struct xattr_handler *handler,
758 struct dentry *dentry, struct inode *inode,
759 const char *name, void *buffer, size_t size)
760{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200761 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200762}
763
Miklos Szeredid837a492016-07-29 12:05:24 +0200764static int ovl_own_xattr_set(const struct xattr_handler *handler,
765 struct dentry *dentry, struct inode *inode,
766 const char *name, const void *value,
767 size_t size, int flags)
768{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200769 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200770}
771
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200772static int ovl_other_xattr_get(const struct xattr_handler *handler,
773 struct dentry *dentry, struct inode *inode,
774 const char *name, void *buffer, size_t size)
775{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200776 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200777}
778
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200779static int ovl_other_xattr_set(const struct xattr_handler *handler,
780 struct dentry *dentry, struct inode *inode,
781 const char *name, const void *value,
782 size_t size, int flags)
783{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200784 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200785}
786
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200787static const struct xattr_handler __maybe_unused
788ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200789 .name = XATTR_NAME_POSIX_ACL_ACCESS,
790 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200791 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200792 .set = ovl_posix_acl_xattr_set,
793};
794
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200795static const struct xattr_handler __maybe_unused
796ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200797 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
798 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200799 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200800 .set = ovl_posix_acl_xattr_set,
801};
802
803static const struct xattr_handler ovl_own_xattr_handler = {
804 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200805 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200806 .set = ovl_own_xattr_set,
807};
808
809static const struct xattr_handler ovl_other_xattr_handler = {
810 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200811 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200812 .set = ovl_other_xattr_set,
813};
814
815static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200816#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200817 &ovl_posix_acl_access_xattr_handler,
818 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200819#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200820 &ovl_own_xattr_handler,
821 &ovl_other_xattr_handler,
822 NULL
823};
824
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200825static int ovl_fill_super(struct super_block *sb, void *data, int silent)
826{
Kees Cook33006cd2017-03-29 14:02:19 -0700827 struct path upperpath = { };
828 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200829 struct dentry *root_dentry;
830 struct ovl_entry *oe;
831 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100832 struct path *stack = NULL;
833 char *lowertmp;
834 char *lower;
835 unsigned int numlower;
836 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100837 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200838 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300839 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200840 int err;
841
Erez Zadokf45827e82014-10-24 00:14:38 +0200842 err = -ENOMEM;
843 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
844 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200845 goto out;
846
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100847 ufs->config.redirect_dir = ovl_redirect_dir_def;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300848 ufs->config.index = ovl_index_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200849 err = ovl_parse_opt((char *) data, &ufs->config);
850 if (err)
851 goto out_free_config;
852
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200853 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100854 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300855 if (!silent)
856 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200857 goto out_free_config;
858 }
859
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100860 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100861 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100862 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100863 if (!ufs->config.workdir) {
864 pr_err("overlayfs: missing 'workdir'\n");
865 goto out_free_config;
866 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200867
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100868 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
869 if (err)
870 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100871
hujianyang71cbad72015-01-15 13:20:57 +0800872 /* Upper fs should not be r/o */
873 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
874 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
875 err = -EINVAL;
876 goto out_put_upperpath;
877 }
878
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100879 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
880 if (err)
881 goto out_put_upperpath;
882
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300883 err = -EBUSY;
884 if (!ovl_inuse_trylock(upperpath.dentry)) {
885 pr_err("overlayfs: upperdir is in-use by another mount\n");
886 goto out_put_upperpath;
887 }
888
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100889 err = ovl_mount_dir(ufs->config.workdir, &workpath);
890 if (err)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300891 goto out_unlock_upperdentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100892
hujianyang2f83fd82015-01-06 12:52:13 +0800893 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100894 if (upperpath.mnt != workpath.mnt) {
895 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
896 goto out_put_workpath;
897 }
898 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
899 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
900 goto out_put_workpath;
901 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300902
903 err = -EBUSY;
904 if (!ovl_inuse_trylock(workpath.dentry)) {
905 pr_err("overlayfs: workdir is in-use by another mount\n");
906 goto out_put_workpath;
907 }
908
909 ufs->workbasedir = workpath.dentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100910 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100911 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100912 err = -ENOMEM;
913 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
914 if (!lowertmp)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300915 goto out_unlock_workdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200916
917 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100918 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800919 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100920 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800921 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100922 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800923 } else if (!ufs->config.upperdir && stacklen == 1) {
924 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
925 goto out_free_lowertmp;
926 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100927
Geliang Tang313684c2016-11-18 23:02:46 +0800928 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100929 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
930 if (!stack)
931 goto out_free_lowertmp;
932
Geliang Tang313684c2016-11-18 23:02:46 +0800933 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100934 lower = lowertmp;
935 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100936 err = ovl_lower_dir(lower, &stack[numlower], ufs,
937 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100938 if (err)
939 goto out_put_lowerpath;
940
941 lower = strchr(lower, '\0') + 1;
942 }
943
944 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100945 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200946 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
947 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100948 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200949 }
950
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100951 if (ufs->config.upperdir) {
952 ufs->upper_mnt = clone_private_mount(&upperpath);
953 err = PTR_ERR(ufs->upper_mnt);
954 if (IS_ERR(ufs->upper_mnt)) {
955 pr_err("overlayfs: failed to clone upperpath\n");
956 goto out_put_lowerpath;
957 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300958
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200959 /* Don't inherit atime flags */
960 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
961
962 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100963
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300964 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
965 OVL_WORKDIR_NAME, false);
Vivek Goyal45aebea2016-02-22 09:28:34 -0500966 /*
967 * Upper should support d_type, else whiteouts are visible.
968 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400969 * iterate_dir() on workdir. This check requires successful
970 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500971 */
Vivek Goyal21765192016-05-20 09:04:26 -0400972 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200973 struct dentry *temp;
974
Vivek Goyal21765192016-05-20 09:04:26 -0400975 err = ovl_check_d_type_supported(&workpath);
976 if (err < 0)
977 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500978
Vivek Goyale7c0b592016-07-01 10:02:44 -0400979 /*
980 * We allowed this configuration and don't want to
981 * break users over kernel upgrade. So warn instead
982 * of erroring out.
983 */
984 if (!err)
985 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200986
987 /* Check if upper/work fs supports O_TMPFILE */
988 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
989 ufs->tmpfile = !IS_ERR(temp);
990 if (ufs->tmpfile)
991 dput(temp);
992 else
993 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +0300994
995 /*
996 * Check if upper/work fs supports trusted.overlay.*
997 * xattr
998 */
999 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
1000 "0", 1, 0);
1001 if (err) {
1002 ufs->noxattr = true;
1003 pr_warn("overlayfs: upper fs does not support xattr.\n");
1004 } else {
1005 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1006 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001007
1008 /* Check if upper/work fs supports file handles */
1009 if (ufs->config.index &&
1010 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1011 ufs->config.index = false;
1012 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1013 }
Vivek Goyal45aebea2016-02-22 09:28:34 -05001014 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001015 }
1016
hujianyang2f83fd82015-01-06 12:52:13 +08001017 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001018 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001019 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001020 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001021 for (i = 0; i < numlower; i++) {
1022 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001023
hujianyang2f83fd82015-01-06 12:52:13 +08001024 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001025 if (IS_ERR(mnt)) {
1026 pr_err("overlayfs: failed to clone lowerpath\n");
1027 goto out_put_lower_mnt;
1028 }
1029 /*
1030 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1031 * will fail instead of modifying lower fs.
1032 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001033 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001034
1035 ufs->lower_mnt[ufs->numlower] = mnt;
1036 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001037
1038 /* Check if all lower layers are on same sb */
1039 if (i == 0)
1040 ufs->same_sb = mnt->mnt_sb;
1041 else if (ufs->same_sb != mnt->mnt_sb)
1042 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001043 }
1044
hujianyang71cbad72015-01-15 13:20:57 +08001045 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1046 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001047 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001048 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1049 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001050
Amir Goldstein02bcd152017-06-21 15:28:36 +03001051 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001052 /* Verify lower root is upper root origin */
1053 err = ovl_verify_origin(upperpath.dentry, ufs->lower_mnt[0],
Amir Goldstein54fb3472017-06-21 15:28:38 +03001054 stack[0].dentry, false, true);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001055 if (err) {
1056 pr_err("overlayfs: failed to verify upper root origin\n");
1057 goto out_put_lower_mnt;
1058 }
1059
Amir Goldstein02bcd152017-06-21 15:28:36 +03001060 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1061 OVL_INDEXDIR_NAME, true);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001062 if (ufs->indexdir) {
1063 /* Verify upper root is index dir origin */
1064 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
1065 upperpath.dentry, true, true);
1066 if (err)
1067 pr_err("overlayfs: failed to verify index dir origin\n");
Amir Goldstein415543d2017-06-21 15:28:42 +03001068
Amir Goldsteincaf70cb2017-06-21 13:46:12 +03001069 /* Cleanup bad/stale/orphan index entries */
Amir Goldstein415543d2017-06-21 15:28:42 +03001070 if (!err)
1071 err = ovl_indexdir_cleanup(ufs->indexdir,
1072 ufs->upper_mnt,
1073 stack, numlower);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001074 }
1075 if (err || !ufs->indexdir)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001076 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
Amir Goldstein54fb3472017-06-21 15:28:38 +03001077 if (err)
1078 goto out_put_indexdir;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001079 }
1080
1081 /* Show index=off/on in /proc/mounts for any of the reasons above */
1082 if (!ufs->indexdir)
1083 ufs->config.index = false;
1084
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001085 if (remote)
1086 sb->s_d_op = &ovl_reval_dentry_operations;
1087 else
1088 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001089
Amir Goldstein8fc646b2017-07-11 15:58:35 +03001090 err = -ENOMEM;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001091 ufs->creator_cred = cred = prepare_creds();
1092 if (!cred)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001093 goto out_put_indexdir;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001094
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001095 /* Never override disk quota limits or use reserved space */
1096 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1097
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001098 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001099 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001100 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001101 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001102
Vivek Goyal655042c2016-10-14 03:03:36 +02001103 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1104 sb->s_op = &ovl_super_operations;
1105 sb->s_xattr = ovl_xattr_handlers;
1106 sb->s_fs_info = ufs;
1107 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1108
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001109 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001110 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001111 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001112
1113 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001114 for (i = 0; i < numlower; i++)
1115 mntput(stack[i].mnt);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001116 mntput(workpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001117 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001118
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001119 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001120 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001121 if (ovl_is_impuredir(upperpath.dentry))
1122 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001123 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001124 for (i = 0; i < numlower; i++) {
1125 oe->lowerstack[i].dentry = stack[i].dentry;
1126 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1127 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001128 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001129
1130 root_dentry->d_fsdata = oe;
1131
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001132 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1133 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001134
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001135 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001136
1137 return 0;
1138
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001139out_free_oe:
1140 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001141out_put_cred:
1142 put_cred(ufs->creator_cred);
Amir Goldstein02bcd152017-06-21 15:28:36 +03001143out_put_indexdir:
1144 dput(ufs->indexdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001145out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001146 for (i = 0; i < ufs->numlower; i++)
1147 mntput(ufs->lower_mnt[i]);
1148 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001149out_put_workdir:
1150 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001151 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001152out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001153 for (i = 0; i < numlower; i++)
1154 path_put(&stack[i]);
1155 kfree(stack);
1156out_free_lowertmp:
1157 kfree(lowertmp);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001158out_unlock_workdentry:
1159 ovl_inuse_unlock(workpath.dentry);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001160out_put_workpath:
1161 path_put(&workpath);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001162out_unlock_upperdentry:
1163 ovl_inuse_unlock(upperpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001164out_put_upperpath:
1165 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001166out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001167 kfree(ufs->config.lowerdir);
1168 kfree(ufs->config.upperdir);
1169 kfree(ufs->config.workdir);
1170 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001171out:
1172 return err;
1173}
1174
1175static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1176 const char *dev_name, void *raw_data)
1177{
1178 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1179}
1180
1181static struct file_system_type ovl_fs_type = {
1182 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001183 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001184 .mount = ovl_mount,
1185 .kill_sb = kill_anon_super,
1186};
Miklos Szeredief94b182014-11-20 16:39:59 +01001187MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001188
Amir Goldstein13cf1992017-06-12 09:54:40 +03001189static void ovl_inode_init_once(void *foo)
1190{
1191 struct ovl_inode *oi = foo;
1192
1193 inode_init_once(&oi->vfs_inode);
1194}
1195
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001196static int __init ovl_init(void)
1197{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001198 int err;
1199
1200 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1201 sizeof(struct ovl_inode), 0,
1202 (SLAB_RECLAIM_ACCOUNT|
1203 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1204 ovl_inode_init_once);
1205 if (ovl_inode_cachep == NULL)
1206 return -ENOMEM;
1207
1208 err = register_filesystem(&ovl_fs_type);
1209 if (err)
1210 kmem_cache_destroy(ovl_inode_cachep);
1211
1212 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001213}
1214
1215static void __exit ovl_exit(void)
1216{
1217 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001218
1219 /*
1220 * Make sure all delayed rcu free inodes are flushed before we
1221 * destroy cache.
1222 */
1223 rcu_barrier();
1224 kmem_cache_destroy(ovl_inode_cachep);
1225
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001226}
1227
1228module_init(ovl_init);
1229module_exit(ovl_exit);