blob: bfdcff0f31689892582b612d319d57d993442d47 [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);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300201 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200202
Amir Goldstein13cf1992017-06-12 09:54:40 +0300203 call_rcu(&inode->i_rcu, ovl_i_callback);
204}
205
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200206static void ovl_put_super(struct super_block *sb)
207{
208 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100209 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210
Amir Goldstein02bcd152017-06-21 15:28:36 +0300211 dput(ufs->indexdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200212 dput(ufs->workdir);
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300213 ovl_inuse_unlock(ufs->workbasedir);
214 dput(ufs->workbasedir);
215 if (ufs->upper_mnt)
216 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200217 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100218 for (i = 0; i < ufs->numlower; i++)
219 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300220 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200221
Erez Zadokf45827e82014-10-24 00:14:38 +0200222 kfree(ufs->config.lowerdir);
223 kfree(ufs->config.upperdir);
224 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200225 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200226 kfree(ufs);
227}
228
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200229static int ovl_sync_fs(struct super_block *sb, int wait)
230{
231 struct ovl_fs *ufs = sb->s_fs_info;
232 struct super_block *upper_sb;
233 int ret;
234
235 if (!ufs->upper_mnt)
236 return 0;
237 upper_sb = ufs->upper_mnt->mnt_sb;
238 if (!upper_sb->s_op->sync_fs)
239 return 0;
240
241 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
242 down_read(&upper_sb->s_umount);
243 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
244 up_read(&upper_sb->s_umount);
245 return ret;
246}
247
Andy Whitcroftcc259632014-10-24 00:14:38 +0200248/**
249 * ovl_statfs
250 * @sb: The overlayfs super block
251 * @buf: The struct kstatfs to fill in with stats
252 *
253 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100254 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200255 */
256static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
257{
258 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
259 struct dentry *root_dentry = dentry->d_sb->s_root;
260 struct path path;
261 int err;
262
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100263 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200264
265 err = vfs_statfs(&path, buf);
266 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100267 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200268 buf->f_type = OVERLAYFS_SUPER_MAGIC;
269 }
270
271 return err;
272}
273
Amir Goldstein02bcd152017-06-21 15:28:36 +0300274/* Will this overlay be forced to mount/remount ro? */
275static bool ovl_force_readonly(struct ovl_fs *ufs)
276{
277 return (!ufs->upper_mnt || !ufs->workdir);
278}
279
Erez Zadokf45827e82014-10-24 00:14:38 +0200280/**
281 * ovl_show_options
282 *
283 * Prints the mount options for a given superblock.
284 * Returns zero; does not fail.
285 */
286static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
287{
288 struct super_block *sb = dentry->d_sb;
289 struct ovl_fs *ufs = sb->s_fs_info;
290
Kees Cooka068acf2015-09-04 15:44:57 -0700291 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100292 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700293 seq_show_option(m, "upperdir", ufs->config.upperdir);
294 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100295 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200296 if (ufs->config.default_permissions)
297 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200298 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
299 seq_printf(m, ",redirect_dir=%s",
300 ufs->config.redirect_dir ? "on" : "off");
Amir Goldstein02bcd152017-06-21 15:28:36 +0300301 if (ufs->config.index != ovl_index_def)
302 seq_printf(m, ",index=%s",
303 ufs->config.index ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200304 return 0;
305}
306
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900307static int ovl_remount(struct super_block *sb, int *flags, char *data)
308{
309 struct ovl_fs *ufs = sb->s_fs_info;
310
Amir Goldstein02bcd152017-06-21 15:28:36 +0300311 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900312 return -EROFS;
313
314 return 0;
315}
316
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200317static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300318 .alloc_inode = ovl_alloc_inode,
319 .destroy_inode = ovl_destroy_inode,
320 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200321 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200322 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200323 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200324 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900325 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200326};
327
328enum {
329 OPT_LOWERDIR,
330 OPT_UPPERDIR,
331 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200332 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100333 OPT_REDIRECT_DIR_ON,
334 OPT_REDIRECT_DIR_OFF,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300335 OPT_INDEX_ON,
336 OPT_INDEX_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200337 OPT_ERR,
338};
339
340static const match_table_t ovl_tokens = {
341 {OPT_LOWERDIR, "lowerdir=%s"},
342 {OPT_UPPERDIR, "upperdir=%s"},
343 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200344 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100345 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
346 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300347 {OPT_INDEX_ON, "index=on"},
348 {OPT_INDEX_OFF, "index=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200349 {OPT_ERR, NULL}
350};
351
Miklos Szeredi91c77942014-11-20 16:40:00 +0100352static char *ovl_next_opt(char **s)
353{
354 char *sbegin = *s;
355 char *p;
356
357 if (sbegin == NULL)
358 return NULL;
359
360 for (p = sbegin; *p; p++) {
361 if (*p == '\\') {
362 p++;
363 if (!*p)
364 break;
365 } else if (*p == ',') {
366 *p = '\0';
367 *s = p + 1;
368 return sbegin;
369 }
370 }
371 *s = NULL;
372 return sbegin;
373}
374
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200375static int ovl_parse_opt(char *opt, struct ovl_config *config)
376{
377 char *p;
378
Miklos Szeredi91c77942014-11-20 16:40:00 +0100379 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200380 int token;
381 substring_t args[MAX_OPT_ARGS];
382
383 if (!*p)
384 continue;
385
386 token = match_token(p, ovl_tokens, args);
387 switch (token) {
388 case OPT_UPPERDIR:
389 kfree(config->upperdir);
390 config->upperdir = match_strdup(&args[0]);
391 if (!config->upperdir)
392 return -ENOMEM;
393 break;
394
395 case OPT_LOWERDIR:
396 kfree(config->lowerdir);
397 config->lowerdir = match_strdup(&args[0]);
398 if (!config->lowerdir)
399 return -ENOMEM;
400 break;
401
402 case OPT_WORKDIR:
403 kfree(config->workdir);
404 config->workdir = match_strdup(&args[0]);
405 if (!config->workdir)
406 return -ENOMEM;
407 break;
408
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200409 case OPT_DEFAULT_PERMISSIONS:
410 config->default_permissions = true;
411 break;
412
Miklos Szeredia6c60652016-12-16 11:02:56 +0100413 case OPT_REDIRECT_DIR_ON:
414 config->redirect_dir = true;
415 break;
416
417 case OPT_REDIRECT_DIR_OFF:
418 config->redirect_dir = false;
419 break;
420
Amir Goldstein02bcd152017-06-21 15:28:36 +0300421 case OPT_INDEX_ON:
422 config->index = true;
423 break;
424
425 case OPT_INDEX_OFF:
426 config->index = false;
427 break;
428
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200429 default:
hujianyangbead55e2015-01-15 13:17:36 +0800430 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200431 return -EINVAL;
432 }
433 }
hujianyang71cbad72015-01-15 13:20:57 +0800434
435 /* Workdir is useless in non-upper mount */
436 if (!config->upperdir && config->workdir) {
437 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
438 config->workdir);
439 kfree(config->workdir);
440 config->workdir = NULL;
441 }
442
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200443 return 0;
444}
445
446#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300447#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200448
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300449static struct dentry *ovl_workdir_create(struct super_block *sb,
450 struct ovl_fs *ufs,
451 struct dentry *dentry,
452 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200453{
454 struct inode *dir = dentry->d_inode;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300455 struct vfsmount *mnt = ufs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200456 struct dentry *work;
457 int err;
458 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300459 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460
461 err = mnt_want_write(mnt);
462 if (err)
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300463 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200464
Al Viro59551022016-01-22 15:40:57 -0500465 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300466 locked = true;
467
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200468retry:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300469 work = lookup_one_len(name, dentry, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200470
471 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200472 struct iattr attr = {
473 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000474 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200475 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476
477 if (work->d_inode) {
478 err = -EEXIST;
479 if (retried)
480 goto out_dput;
481
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300482 if (persist)
483 goto out_unlock;
484
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200485 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200486 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200487 dput(work);
488 goto retry;
489 }
490
Al Viro32a3d842016-12-04 17:33:17 +0000491 err = ovl_create_real(dir, work,
492 &(struct cattr){.mode = S_IFDIR | 0},
493 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200494 if (err)
495 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200496
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200497 /*
498 * Try to remove POSIX ACL xattrs from workdir. We are good if:
499 *
500 * a) success (there was a POSIX ACL xattr and was removed)
501 * b) -ENODATA (there was no POSIX ACL xattr)
502 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
503 *
504 * There are various other error values that could effectively
505 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
506 * if the xattr name is too long), but the set of filesystems
507 * allowed as upper are limited to "normal" ones, where checking
508 * for the above two errors is sufficient.
509 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200510 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200511 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200512 goto out_dput;
513
514 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200515 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200516 goto out_dput;
517
518 /* Clear any inherited mode bits */
519 inode_lock(work->d_inode);
520 err = notify_change(work, &attr, NULL);
521 inode_unlock(work->d_inode);
522 if (err)
523 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300524 } else {
525 err = PTR_ERR(work);
526 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200527 }
528out_unlock:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200529 mnt_drop_write(mnt);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300530 if (locked)
531 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200532
533 return work;
534
535out_dput:
536 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300537out_err:
538 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
539 ufs->config.workdir, name, -err);
540 sb->s_flags |= MS_RDONLY;
541 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200542 goto out_unlock;
543}
544
Miklos Szeredi91c77942014-11-20 16:40:00 +0100545static void ovl_unescape(char *s)
546{
547 char *d = s;
548
549 for (;; s++, d++) {
550 if (*s == '\\')
551 s++;
552 *d = *s;
553 if (!*s)
554 break;
555 }
556}
557
Miklos Szerediab508822014-12-13 00:59:49 +0100558static int ovl_mount_dir_noesc(const char *name, struct path *path)
559{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100560 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100561
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100562 if (!*name) {
563 pr_err("overlayfs: empty lowerdir\n");
564 goto out;
565 }
Miklos Szerediab508822014-12-13 00:59:49 +0100566 err = kern_path(name, LOOKUP_FOLLOW, path);
567 if (err) {
568 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
569 goto out;
570 }
571 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200572 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100573 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
574 goto out_put;
575 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100576 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100577 pr_err("overlayfs: '%s' not a directory\n", name);
578 goto out_put;
579 }
580 return 0;
581
582out_put:
583 path_put(path);
584out:
585 return err;
586}
587
588static int ovl_mount_dir(const char *name, struct path *path)
589{
590 int err = -ENOMEM;
591 char *tmp = kstrdup(name, GFP_KERNEL);
592
593 if (tmp) {
594 ovl_unescape(tmp);
595 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200596
597 if (!err)
598 if (ovl_dentry_remote(path->dentry)) {
599 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
600 tmp);
601 path_put(path);
602 err = -EINVAL;
603 }
Miklos Szerediab508822014-12-13 00:59:49 +0100604 kfree(tmp);
605 }
606 return err;
607}
608
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100609static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
610 const char *name)
611{
612 struct kstatfs statfs;
613 int err = vfs_statfs(path, &statfs);
614
615 if (err)
616 pr_err("overlayfs: statfs failed on '%s'\n", name);
617 else
618 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
619
620 return err;
621}
622
623static int ovl_lower_dir(const char *name, struct path *path,
624 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100625{
626 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100627
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100628 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100629 if (err)
630 goto out;
631
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100632 err = ovl_check_namelen(path, ofs, name);
633 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100634 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100635
Miklos Szerediab508822014-12-13 00:59:49 +0100636 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
637
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200638 if (ovl_dentry_remote(path->dentry))
639 *remote = true;
640
Amir Goldstein02bcd152017-06-21 15:28:36 +0300641 /*
642 * The inodes index feature needs to encode and decode file
643 * handles, so it requires that all layers support them.
644 */
645 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
646 ofs->config.index = false;
647 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
648 }
649
Miklos Szerediab508822014-12-13 00:59:49 +0100650 return 0;
651
652out_put:
653 path_put(path);
654out:
655 return err;
656}
657
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200658/* Workdir should not be subdir of upperdir and vice versa */
659static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
660{
661 bool ok = false;
662
663 if (workdir != upperdir) {
664 ok = (lock_rename(workdir, upperdir) == NULL);
665 unlock_rename(workdir, upperdir);
666 }
667 return ok;
668}
669
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100670static unsigned int ovl_split_lowerdirs(char *str)
671{
672 unsigned int ctr = 1;
673 char *s, *d;
674
675 for (s = d = str;; s++, d++) {
676 if (*s == '\\') {
677 s++;
678 } else if (*s == ':') {
679 *d = '\0';
680 ctr++;
681 continue;
682 }
683 *d = *s;
684 if (!*s)
685 break;
686 }
687 return ctr;
688}
689
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200690static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200691ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
692 struct dentry *dentry, struct inode *inode,
693 const char *name, void *buffer, size_t size)
694{
695 return ovl_xattr_get(dentry, handler->name, buffer, size);
696}
697
698static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200699ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
700 struct dentry *dentry, struct inode *inode,
701 const char *name, const void *value,
702 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200703{
704 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200705 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200706 struct posix_acl *acl = NULL;
707 int err;
708
709 /* Check that everything is OK before copy-up */
710 if (value) {
711 acl = posix_acl_from_xattr(&init_user_ns, value, size);
712 if (IS_ERR(acl))
713 return PTR_ERR(acl);
714 }
715 err = -EOPNOTSUPP;
716 if (!IS_POSIXACL(d_inode(workdir)))
717 goto out_acl_release;
718 if (!realinode->i_op->set_acl)
719 goto out_acl_release;
720 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
721 err = acl ? -EACCES : 0;
722 goto out_acl_release;
723 }
724 err = -EPERM;
725 if (!inode_owner_or_capable(inode))
726 goto out_acl_release;
727
728 posix_acl_release(acl);
729
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100730 /*
731 * Check if sgid bit needs to be cleared (actual setacl operation will
732 * be done with mounter's capabilities and so that won't do it for us).
733 */
734 if (unlikely(inode->i_mode & S_ISGID) &&
735 handler->flags == ACL_TYPE_ACCESS &&
736 !in_group_p(inode->i_gid) &&
737 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
738 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
739
740 err = ovl_setattr(dentry, &iattr);
741 if (err)
742 return err;
743 }
744
Miklos Szeredice315132016-09-01 11:12:00 +0200745 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
746 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200747 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200748
749 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200750
751out_acl_release:
752 posix_acl_release(acl);
753 return err;
754}
755
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200756static int ovl_own_xattr_get(const struct xattr_handler *handler,
757 struct dentry *dentry, struct inode *inode,
758 const char *name, void *buffer, size_t size)
759{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200760 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200761}
762
Miklos Szeredid837a492016-07-29 12:05:24 +0200763static int ovl_own_xattr_set(const struct xattr_handler *handler,
764 struct dentry *dentry, struct inode *inode,
765 const char *name, const void *value,
766 size_t size, int flags)
767{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200768 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200769}
770
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200771static int ovl_other_xattr_get(const struct xattr_handler *handler,
772 struct dentry *dentry, struct inode *inode,
773 const char *name, void *buffer, size_t size)
774{
775 return ovl_xattr_get(dentry, name, buffer, size);
776}
777
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200778static int ovl_other_xattr_set(const struct xattr_handler *handler,
779 struct dentry *dentry, struct inode *inode,
780 const char *name, const void *value,
781 size_t size, int flags)
782{
783 return ovl_xattr_set(dentry, name, value, size, flags);
784}
785
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200786static const struct xattr_handler __maybe_unused
787ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200788 .name = XATTR_NAME_POSIX_ACL_ACCESS,
789 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200790 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200791 .set = ovl_posix_acl_xattr_set,
792};
793
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200794static const struct xattr_handler __maybe_unused
795ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200796 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
797 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200798 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200799 .set = ovl_posix_acl_xattr_set,
800};
801
802static const struct xattr_handler ovl_own_xattr_handler = {
803 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200804 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200805 .set = ovl_own_xattr_set,
806};
807
808static const struct xattr_handler ovl_other_xattr_handler = {
809 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200810 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200811 .set = ovl_other_xattr_set,
812};
813
814static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200815#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200816 &ovl_posix_acl_access_xattr_handler,
817 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200818#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200819 &ovl_own_xattr_handler,
820 &ovl_other_xattr_handler,
821 NULL
822};
823
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200824static int ovl_fill_super(struct super_block *sb, void *data, int silent)
825{
Kees Cook33006cd2017-03-29 14:02:19 -0700826 struct path upperpath = { };
827 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200828 struct dentry *root_dentry;
829 struct ovl_entry *oe;
830 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100831 struct path *stack = NULL;
832 char *lowertmp;
833 char *lower;
834 unsigned int numlower;
835 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100836 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200837 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300838 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200839 int err;
840
Erez Zadokf45827e82014-10-24 00:14:38 +0200841 err = -ENOMEM;
842 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
843 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200844 goto out;
845
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100846 ufs->config.redirect_dir = ovl_redirect_dir_def;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300847 ufs->config.index = ovl_index_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200848 err = ovl_parse_opt((char *) data, &ufs->config);
849 if (err)
850 goto out_free_config;
851
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200852 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100853 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300854 if (!silent)
855 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200856 goto out_free_config;
857 }
858
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100859 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100860 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100861 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100862 if (!ufs->config.workdir) {
863 pr_err("overlayfs: missing 'workdir'\n");
864 goto out_free_config;
865 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200866
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100867 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
868 if (err)
869 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100870
hujianyang71cbad72015-01-15 13:20:57 +0800871 /* Upper fs should not be r/o */
872 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
873 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
874 err = -EINVAL;
875 goto out_put_upperpath;
876 }
877
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100878 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
879 if (err)
880 goto out_put_upperpath;
881
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300882 err = -EBUSY;
883 if (!ovl_inuse_trylock(upperpath.dentry)) {
884 pr_err("overlayfs: upperdir is in-use by another mount\n");
885 goto out_put_upperpath;
886 }
887
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100888 err = ovl_mount_dir(ufs->config.workdir, &workpath);
889 if (err)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300890 goto out_unlock_upperdentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100891
hujianyang2f83fd82015-01-06 12:52:13 +0800892 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100893 if (upperpath.mnt != workpath.mnt) {
894 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
895 goto out_put_workpath;
896 }
897 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
898 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
899 goto out_put_workpath;
900 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300901
902 err = -EBUSY;
903 if (!ovl_inuse_trylock(workpath.dentry)) {
904 pr_err("overlayfs: workdir is in-use by another mount\n");
905 goto out_put_workpath;
906 }
907
908 ufs->workbasedir = workpath.dentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100909 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100910 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100911 err = -ENOMEM;
912 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
913 if (!lowertmp)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300914 goto out_unlock_workdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200915
916 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100917 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800918 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100919 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800920 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100921 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800922 } else if (!ufs->config.upperdir && stacklen == 1) {
923 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
924 goto out_free_lowertmp;
925 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100926
Geliang Tang313684c2016-11-18 23:02:46 +0800927 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100928 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
929 if (!stack)
930 goto out_free_lowertmp;
931
Geliang Tang313684c2016-11-18 23:02:46 +0800932 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100933 lower = lowertmp;
934 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100935 err = ovl_lower_dir(lower, &stack[numlower], ufs,
936 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100937 if (err)
938 goto out_put_lowerpath;
939
940 lower = strchr(lower, '\0') + 1;
941 }
942
943 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100944 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200945 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
946 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100947 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200948 }
949
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100950 if (ufs->config.upperdir) {
951 ufs->upper_mnt = clone_private_mount(&upperpath);
952 err = PTR_ERR(ufs->upper_mnt);
953 if (IS_ERR(ufs->upper_mnt)) {
954 pr_err("overlayfs: failed to clone upperpath\n");
955 goto out_put_lowerpath;
956 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300957
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200958 /* Don't inherit atime flags */
959 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
960
961 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100962
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300963 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
964 OVL_WORKDIR_NAME, false);
Vivek Goyal45aebea2016-02-22 09:28:34 -0500965 /*
966 * Upper should support d_type, else whiteouts are visible.
967 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400968 * iterate_dir() on workdir. This check requires successful
969 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500970 */
Vivek Goyal21765192016-05-20 09:04:26 -0400971 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200972 struct dentry *temp;
973
Vivek Goyal21765192016-05-20 09:04:26 -0400974 err = ovl_check_d_type_supported(&workpath);
975 if (err < 0)
976 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500977
Vivek Goyale7c0b592016-07-01 10:02:44 -0400978 /*
979 * We allowed this configuration and don't want to
980 * break users over kernel upgrade. So warn instead
981 * of erroring out.
982 */
983 if (!err)
984 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200985
986 /* Check if upper/work fs supports O_TMPFILE */
987 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
988 ufs->tmpfile = !IS_ERR(temp);
989 if (ufs->tmpfile)
990 dput(temp);
991 else
992 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +0300993
994 /*
995 * Check if upper/work fs supports trusted.overlay.*
996 * xattr
997 */
998 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
999 "0", 1, 0);
1000 if (err) {
1001 ufs->noxattr = true;
1002 pr_warn("overlayfs: upper fs does not support xattr.\n");
1003 } else {
1004 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1005 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001006
1007 /* Check if upper/work fs supports file handles */
1008 if (ufs->config.index &&
1009 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1010 ufs->config.index = false;
1011 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1012 }
Vivek Goyal45aebea2016-02-22 09:28:34 -05001013 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001014 }
1015
hujianyang2f83fd82015-01-06 12:52:13 +08001016 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001017 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001018 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001019 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001020 for (i = 0; i < numlower; i++) {
1021 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001022
hujianyang2f83fd82015-01-06 12:52:13 +08001023 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001024 if (IS_ERR(mnt)) {
1025 pr_err("overlayfs: failed to clone lowerpath\n");
1026 goto out_put_lower_mnt;
1027 }
1028 /*
1029 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1030 * will fail instead of modifying lower fs.
1031 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001032 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001033
1034 ufs->lower_mnt[ufs->numlower] = mnt;
1035 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001036
1037 /* Check if all lower layers are on same sb */
1038 if (i == 0)
1039 ufs->same_sb = mnt->mnt_sb;
1040 else if (ufs->same_sb != mnt->mnt_sb)
1041 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001042 }
1043
hujianyang71cbad72015-01-15 13:20:57 +08001044 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1045 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001046 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001047 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1048 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001049
Amir Goldstein02bcd152017-06-21 15:28:36 +03001050 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001051 /* Verify lower root is upper root origin */
1052 err = ovl_verify_origin(upperpath.dentry, ufs->lower_mnt[0],
1053 stack[0].dentry, true);
1054 if (err) {
1055 pr_err("overlayfs: failed to verify upper root origin\n");
1056 goto out_put_lower_mnt;
1057 }
1058
Amir Goldstein02bcd152017-06-21 15:28:36 +03001059 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1060 OVL_INDEXDIR_NAME, true);
1061 err = PTR_ERR(ufs->indexdir);
1062 if (IS_ERR(ufs->indexdir))
1063 goto out_put_lower_mnt;
1064
1065 if (!ufs->indexdir)
1066 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1067 }
1068
1069 /* Show index=off/on in /proc/mounts for any of the reasons above */
1070 if (!ufs->indexdir)
1071 ufs->config.index = false;
1072
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001073 if (remote)
1074 sb->s_d_op = &ovl_reval_dentry_operations;
1075 else
1076 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001077
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001078 ufs->creator_cred = cred = prepare_creds();
1079 if (!cred)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001080 goto out_put_indexdir;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001081
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001082 /* Never override disk quota limits or use reserved space */
1083 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1084
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001085 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001086 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001087 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001088 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001089
Vivek Goyal655042c2016-10-14 03:03:36 +02001090 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1091 sb->s_op = &ovl_super_operations;
1092 sb->s_xattr = ovl_xattr_handlers;
1093 sb->s_fs_info = ufs;
1094 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1095
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001096 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001097 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001098 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001099
1100 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001101 for (i = 0; i < numlower; i++)
1102 mntput(stack[i].mnt);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001103 mntput(workpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001104 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001105
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001106 if (upperpath.dentry) {
Miklos Szeredi13c72072017-07-04 22:03:16 +02001107 if (ovl_is_impuredir(upperpath.dentry))
1108 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001109 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001110 for (i = 0; i < numlower; i++) {
1111 oe->lowerstack[i].dentry = stack[i].dentry;
1112 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1113 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001114 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001115
1116 root_dentry->d_fsdata = oe;
1117
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001118 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1119 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001120
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001121 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001122
1123 return 0;
1124
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001125out_free_oe:
1126 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001127out_put_cred:
1128 put_cred(ufs->creator_cred);
Amir Goldstein02bcd152017-06-21 15:28:36 +03001129out_put_indexdir:
1130 dput(ufs->indexdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001131out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001132 for (i = 0; i < ufs->numlower; i++)
1133 mntput(ufs->lower_mnt[i]);
1134 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001135out_put_workdir:
1136 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001137 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001138out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001139 for (i = 0; i < numlower; i++)
1140 path_put(&stack[i]);
1141 kfree(stack);
1142out_free_lowertmp:
1143 kfree(lowertmp);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001144out_unlock_workdentry:
1145 ovl_inuse_unlock(workpath.dentry);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001146out_put_workpath:
1147 path_put(&workpath);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001148out_unlock_upperdentry:
1149 ovl_inuse_unlock(upperpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001150out_put_upperpath:
1151 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001152out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001153 kfree(ufs->config.lowerdir);
1154 kfree(ufs->config.upperdir);
1155 kfree(ufs->config.workdir);
1156 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001157out:
1158 return err;
1159}
1160
1161static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1162 const char *dev_name, void *raw_data)
1163{
1164 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1165}
1166
1167static struct file_system_type ovl_fs_type = {
1168 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001169 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001170 .mount = ovl_mount,
1171 .kill_sb = kill_anon_super,
1172};
Miklos Szeredief94b182014-11-20 16:39:59 +01001173MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001174
Amir Goldstein13cf1992017-06-12 09:54:40 +03001175static void ovl_inode_init_once(void *foo)
1176{
1177 struct ovl_inode *oi = foo;
1178
1179 inode_init_once(&oi->vfs_inode);
1180}
1181
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001182static int __init ovl_init(void)
1183{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001184 int err;
1185
1186 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1187 sizeof(struct ovl_inode), 0,
1188 (SLAB_RECLAIM_ACCOUNT|
1189 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1190 ovl_inode_init_once);
1191 if (ovl_inode_cachep == NULL)
1192 return -ENOMEM;
1193
1194 err = register_filesystem(&ovl_fs_type);
1195 if (err)
1196 kmem_cache_destroy(ovl_inode_cachep);
1197
1198 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001199}
1200
1201static void __exit ovl_exit(void)
1202{
1203 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001204
1205 /*
1206 * Make sure all delayed rcu free inodes are flushed before we
1207 * destroy cache.
1208 */
1209 rcu_barrier();
1210 kmem_cache_destroy(ovl_inode_cachep);
1211
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001212}
1213
1214module_init(ovl_init);
1215module_exit(ovl_exit);