blob: 2c9f48096ff03090348431ffe93ae6aa68fa5e39 [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"
21
22MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23MODULE_DESCRIPTION("Overlay filesystem");
24MODULE_LICENSE("GPL");
25
Miklos Szeredie9be9d52014-10-24 00:14:38 +020026
27struct ovl_dir_cache;
28
Miklos Szeredia78d9f02014-12-13 00:59:52 +010029#define OVL_MAX_STACK 500
30
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010031static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
32module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
33MODULE_PARM_DESC(ovl_redirect_dir_def,
34 "Default to on or off for the redirect_dir feature");
Miklos Szeredie9be9d52014-10-24 00:14:38 +020035
Amir Goldstein02bcd152017-06-21 15:28:36 +030036static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
37module_param_named(index, ovl_index_def, bool, 0644);
38MODULE_PARM_DESC(ovl_index_def,
39 "Default to on or off for the inodes index feature");
40
Miklos Szeredie9be9d52014-10-24 00:14:38 +020041static void ovl_dentry_release(struct dentry *dentry)
42{
43 struct ovl_entry *oe = dentry->d_fsdata;
44
45 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +010046 unsigned int i;
47
Miklos Szeredidd662662014-12-13 00:59:43 +010048 for (i = 0; i < oe->numlower; i++)
49 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020050 kfree_rcu(oe, rcu);
51 }
52}
53
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030054static int ovl_check_append_only(struct inode *inode, int flag)
55{
56 /*
57 * This test was moot in vfs may_open() because overlay inode does
58 * not have the S_APPEND flag, so re-check on real upper inode
59 */
60 if (IS_APPEND(inode)) {
61 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
62 return -EPERM;
63 if (flag & O_TRUNC)
64 return -EPERM;
65 }
66
67 return 0;
68}
69
Miklos Szeredi2d902672016-06-30 08:53:27 +020070static struct dentry *ovl_d_real(struct dentry *dentry,
71 const struct inode *inode,
Miklos Szeredi495e6422017-09-04 21:42:22 +020072 unsigned int open_flags, unsigned int flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040073{
74 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030075 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040076
Miklos Szeredicd913042017-09-05 12:53:11 +020077 if (flags & D_REAL_UPPER)
78 return ovl_dentry_upper(dentry);
79
Miklos Szeredica4c8a32016-12-16 11:02:55 +010080 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040081 if (!inode || inode == d_inode(dentry))
82 return dentry;
83 goto bug;
84 }
85
Miklos Szeredi2d902672016-06-30 08:53:27 +020086 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030087 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +020088 if (err)
89 return ERR_PTR(err);
90 }
91
Miklos Szeredid101a122016-03-26 16:14:37 -040092 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030093 if (real && (!inode || inode == d_inode(real))) {
94 if (!inode) {
95 err = ovl_check_append_only(d_inode(real), open_flags);
96 if (err)
97 return ERR_PTR(err);
98 }
Miklos Szeredid101a122016-03-26 16:14:37 -040099 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300100 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400101
102 real = ovl_dentry_lower(dentry);
103 if (!real)
104 goto bug;
105
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100106 /* Handle recursion */
Miklos Szeredi495e6422017-09-04 21:42:22 +0200107 real = d_real(real, inode, open_flags, 0);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100108
Miklos Szeredid101a122016-03-26 16:14:37 -0400109 if (!inode || inode == d_inode(real))
110 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400111bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200112 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400113 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
114 return dentry;
115}
116
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200117static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
118{
119 struct ovl_entry *oe = dentry->d_fsdata;
120 unsigned int i;
121 int ret = 1;
122
123 for (i = 0; i < oe->numlower; i++) {
124 struct dentry *d = oe->lowerstack[i].dentry;
125
126 if (d->d_flags & DCACHE_OP_REVALIDATE) {
127 ret = d->d_op->d_revalidate(d, flags);
128 if (ret < 0)
129 return ret;
130 if (!ret) {
131 if (!(flags & LOOKUP_RCU))
132 d_invalidate(d);
133 return -ESTALE;
134 }
135 }
136 }
137 return 1;
138}
139
140static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
141{
142 struct ovl_entry *oe = dentry->d_fsdata;
143 unsigned int i;
144 int ret = 1;
145
146 for (i = 0; i < oe->numlower; i++) {
147 struct dentry *d = oe->lowerstack[i].dentry;
148
149 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
150 ret = d->d_op->d_weak_revalidate(d, flags);
151 if (ret <= 0)
152 break;
153 }
154 }
155 return ret;
156}
157
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200158static const struct dentry_operations ovl_dentry_operations = {
159 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400160 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200161};
162
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200163static const struct dentry_operations ovl_reval_dentry_operations = {
164 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400165 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200166 .d_revalidate = ovl_dentry_revalidate,
167 .d_weak_revalidate = ovl_dentry_weak_revalidate,
168};
169
Amir Goldstein13cf1992017-06-12 09:54:40 +0300170static struct kmem_cache *ovl_inode_cachep;
171
172static struct inode *ovl_alloc_inode(struct super_block *sb)
173{
174 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
175
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900176 if (!oi)
177 return NULL;
178
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200179 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200180 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200181 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200182 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200183 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200184 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300185 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200186
Amir Goldstein13cf1992017-06-12 09:54:40 +0300187 return &oi->vfs_inode;
188}
189
190static void ovl_i_callback(struct rcu_head *head)
191{
192 struct inode *inode = container_of(head, struct inode, i_rcu);
193
194 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
195}
196
197static void ovl_destroy_inode(struct inode *inode)
198{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200199 struct ovl_inode *oi = OVL_I(inode);
200
201 dput(oi->__upperdentry);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200202 kfree(oi->redirect);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200203 ovl_dir_cache_free(inode);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300204 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200205
Amir Goldstein13cf1992017-06-12 09:54:40 +0300206 call_rcu(&inode->i_rcu, ovl_i_callback);
207}
208
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200209static void ovl_put_super(struct super_block *sb)
210{
211 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100212 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200213
Amir Goldstein02bcd152017-06-21 15:28:36 +0300214 dput(ufs->indexdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215 dput(ufs->workdir);
Amir Goldstein85fdee12017-09-29 10:21:21 +0300216 if (ufs->workdir_locked)
217 ovl_inuse_unlock(ufs->workbasedir);
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300218 dput(ufs->workbasedir);
Amir Goldstein85fdee12017-09-29 10:21:21 +0300219 if (ufs->upper_mnt && ufs->upperdir_locked)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300220 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200221 mntput(ufs->upper_mnt);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +0200222 for (i = 0; i < ufs->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500223 mntput(ufs->lower_layers[i].mnt);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +0200224 free_anon_bdev(ufs->lower_layers[i].pseudo_dev);
225 }
Chandan Rajendrab9343632017-07-24 01:57:54 -0500226 kfree(ufs->lower_layers);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200227
Erez Zadokf45827e82014-10-24 00:14:38 +0200228 kfree(ufs->config.lowerdir);
229 kfree(ufs->config.upperdir);
230 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200231 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200232 kfree(ufs);
233}
234
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200235static int ovl_sync_fs(struct super_block *sb, int wait)
236{
237 struct ovl_fs *ufs = sb->s_fs_info;
238 struct super_block *upper_sb;
239 int ret;
240
241 if (!ufs->upper_mnt)
242 return 0;
243 upper_sb = ufs->upper_mnt->mnt_sb;
244 if (!upper_sb->s_op->sync_fs)
245 return 0;
246
247 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
248 down_read(&upper_sb->s_umount);
249 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
250 up_read(&upper_sb->s_umount);
251 return ret;
252}
253
Andy Whitcroftcc259632014-10-24 00:14:38 +0200254/**
255 * ovl_statfs
256 * @sb: The overlayfs super block
257 * @buf: The struct kstatfs to fill in with stats
258 *
259 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100260 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200261 */
262static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
263{
264 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
265 struct dentry *root_dentry = dentry->d_sb->s_root;
266 struct path path;
267 int err;
268
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100269 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200270
271 err = vfs_statfs(&path, buf);
272 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100273 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200274 buf->f_type = OVERLAYFS_SUPER_MAGIC;
275 }
276
277 return err;
278}
279
Amir Goldstein02bcd152017-06-21 15:28:36 +0300280/* Will this overlay be forced to mount/remount ro? */
281static bool ovl_force_readonly(struct ovl_fs *ufs)
282{
283 return (!ufs->upper_mnt || !ufs->workdir);
284}
285
Erez Zadokf45827e82014-10-24 00:14:38 +0200286/**
287 * ovl_show_options
288 *
289 * Prints the mount options for a given superblock.
290 * Returns zero; does not fail.
291 */
292static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
293{
294 struct super_block *sb = dentry->d_sb;
295 struct ovl_fs *ufs = sb->s_fs_info;
296
Kees Cooka068acf2015-09-04 15:44:57 -0700297 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100298 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700299 seq_show_option(m, "upperdir", ufs->config.upperdir);
300 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100301 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200302 if (ufs->config.default_permissions)
303 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200304 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
305 seq_printf(m, ",redirect_dir=%s",
306 ufs->config.redirect_dir ? "on" : "off");
Amir Goldstein02bcd152017-06-21 15:28:36 +0300307 if (ufs->config.index != ovl_index_def)
308 seq_printf(m, ",index=%s",
309 ufs->config.index ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200310 return 0;
311}
312
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900313static int ovl_remount(struct super_block *sb, int *flags, char *data)
314{
315 struct ovl_fs *ufs = sb->s_fs_info;
316
Amir Goldstein02bcd152017-06-21 15:28:36 +0300317 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900318 return -EROFS;
319
320 return 0;
321}
322
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200323static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300324 .alloc_inode = ovl_alloc_inode,
325 .destroy_inode = ovl_destroy_inode,
326 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200327 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200328 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200329 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200330 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900331 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200332};
333
334enum {
335 OPT_LOWERDIR,
336 OPT_UPPERDIR,
337 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200338 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100339 OPT_REDIRECT_DIR_ON,
340 OPT_REDIRECT_DIR_OFF,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300341 OPT_INDEX_ON,
342 OPT_INDEX_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343 OPT_ERR,
344};
345
346static const match_table_t ovl_tokens = {
347 {OPT_LOWERDIR, "lowerdir=%s"},
348 {OPT_UPPERDIR, "upperdir=%s"},
349 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200350 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100351 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
352 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300353 {OPT_INDEX_ON, "index=on"},
354 {OPT_INDEX_OFF, "index=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200355 {OPT_ERR, NULL}
356};
357
Miklos Szeredi91c77942014-11-20 16:40:00 +0100358static char *ovl_next_opt(char **s)
359{
360 char *sbegin = *s;
361 char *p;
362
363 if (sbegin == NULL)
364 return NULL;
365
366 for (p = sbegin; *p; p++) {
367 if (*p == '\\') {
368 p++;
369 if (!*p)
370 break;
371 } else if (*p == ',') {
372 *p = '\0';
373 *s = p + 1;
374 return sbegin;
375 }
376 }
377 *s = NULL;
378 return sbegin;
379}
380
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200381static int ovl_parse_opt(char *opt, struct ovl_config *config)
382{
383 char *p;
384
Miklos Szeredi91c77942014-11-20 16:40:00 +0100385 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200386 int token;
387 substring_t args[MAX_OPT_ARGS];
388
389 if (!*p)
390 continue;
391
392 token = match_token(p, ovl_tokens, args);
393 switch (token) {
394 case OPT_UPPERDIR:
395 kfree(config->upperdir);
396 config->upperdir = match_strdup(&args[0]);
397 if (!config->upperdir)
398 return -ENOMEM;
399 break;
400
401 case OPT_LOWERDIR:
402 kfree(config->lowerdir);
403 config->lowerdir = match_strdup(&args[0]);
404 if (!config->lowerdir)
405 return -ENOMEM;
406 break;
407
408 case OPT_WORKDIR:
409 kfree(config->workdir);
410 config->workdir = match_strdup(&args[0]);
411 if (!config->workdir)
412 return -ENOMEM;
413 break;
414
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200415 case OPT_DEFAULT_PERMISSIONS:
416 config->default_permissions = true;
417 break;
418
Miklos Szeredia6c60652016-12-16 11:02:56 +0100419 case OPT_REDIRECT_DIR_ON:
420 config->redirect_dir = true;
421 break;
422
423 case OPT_REDIRECT_DIR_OFF:
424 config->redirect_dir = false;
425 break;
426
Amir Goldstein02bcd152017-06-21 15:28:36 +0300427 case OPT_INDEX_ON:
428 config->index = true;
429 break;
430
431 case OPT_INDEX_OFF:
432 config->index = false;
433 break;
434
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200435 default:
hujianyangbead55e2015-01-15 13:17:36 +0800436 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200437 return -EINVAL;
438 }
439 }
hujianyang71cbad72015-01-15 13:20:57 +0800440
441 /* Workdir is useless in non-upper mount */
442 if (!config->upperdir && config->workdir) {
443 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
444 config->workdir);
445 kfree(config->workdir);
446 config->workdir = NULL;
447 }
448
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200449 return 0;
450}
451
452#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300453#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200454
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300455static struct dentry *ovl_workdir_create(struct super_block *sb,
456 struct ovl_fs *ufs,
457 struct dentry *dentry,
458 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200459{
460 struct inode *dir = dentry->d_inode;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300461 struct vfsmount *mnt = ufs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200462 struct dentry *work;
463 int err;
464 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300465 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200466
467 err = mnt_want_write(mnt);
468 if (err)
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300469 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200470
Al Viro59551022016-01-22 15:40:57 -0500471 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300472 locked = true;
473
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200474retry:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300475 work = lookup_one_len(name, dentry, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476
477 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200478 struct iattr attr = {
479 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000480 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200481 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200482
483 if (work->d_inode) {
484 err = -EEXIST;
485 if (retried)
486 goto out_dput;
487
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300488 if (persist)
489 goto out_unlock;
490
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200491 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200492 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200493 dput(work);
494 goto retry;
495 }
496
Al Viro32a3d842016-12-04 17:33:17 +0000497 err = ovl_create_real(dir, work,
498 &(struct cattr){.mode = S_IFDIR | 0},
499 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200500 if (err)
501 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200502
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200503 /*
504 * Try to remove POSIX ACL xattrs from workdir. We are good if:
505 *
506 * a) success (there was a POSIX ACL xattr and was removed)
507 * b) -ENODATA (there was no POSIX ACL xattr)
508 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
509 *
510 * There are various other error values that could effectively
511 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
512 * if the xattr name is too long), but the set of filesystems
513 * allowed as upper are limited to "normal" ones, where checking
514 * for the above two errors is sufficient.
515 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200516 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200517 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200518 goto out_dput;
519
520 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200521 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200522 goto out_dput;
523
524 /* Clear any inherited mode bits */
525 inode_lock(work->d_inode);
526 err = notify_change(work, &attr, NULL);
527 inode_unlock(work->d_inode);
528 if (err)
529 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300530 } else {
531 err = PTR_ERR(work);
532 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200533 }
534out_unlock:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200535 mnt_drop_write(mnt);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300536 if (locked)
537 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200538
539 return work;
540
541out_dput:
542 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300543out_err:
544 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
545 ufs->config.workdir, name, -err);
546 sb->s_flags |= MS_RDONLY;
547 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200548 goto out_unlock;
549}
550
Miklos Szeredi91c77942014-11-20 16:40:00 +0100551static void ovl_unescape(char *s)
552{
553 char *d = s;
554
555 for (;; s++, d++) {
556 if (*s == '\\')
557 s++;
558 *d = *s;
559 if (!*s)
560 break;
561 }
562}
563
Miklos Szerediab508822014-12-13 00:59:49 +0100564static int ovl_mount_dir_noesc(const char *name, struct path *path)
565{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100566 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100567
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100568 if (!*name) {
569 pr_err("overlayfs: empty lowerdir\n");
570 goto out;
571 }
Miklos Szerediab508822014-12-13 00:59:49 +0100572 err = kern_path(name, LOOKUP_FOLLOW, path);
573 if (err) {
574 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
575 goto out;
576 }
577 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200578 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100579 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
580 goto out_put;
581 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100582 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100583 pr_err("overlayfs: '%s' not a directory\n", name);
584 goto out_put;
585 }
586 return 0;
587
588out_put:
589 path_put(path);
590out:
591 return err;
592}
593
594static int ovl_mount_dir(const char *name, struct path *path)
595{
596 int err = -ENOMEM;
597 char *tmp = kstrdup(name, GFP_KERNEL);
598
599 if (tmp) {
600 ovl_unescape(tmp);
601 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200602
603 if (!err)
604 if (ovl_dentry_remote(path->dentry)) {
605 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
606 tmp);
607 path_put(path);
608 err = -EINVAL;
609 }
Miklos Szerediab508822014-12-13 00:59:49 +0100610 kfree(tmp);
611 }
612 return err;
613}
614
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100615static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
616 const char *name)
617{
618 struct kstatfs statfs;
619 int err = vfs_statfs(path, &statfs);
620
621 if (err)
622 pr_err("overlayfs: statfs failed on '%s'\n", name);
623 else
624 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
625
626 return err;
627}
628
629static int ovl_lower_dir(const char *name, struct path *path,
630 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100631{
632 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100633
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100634 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100635 if (err)
636 goto out;
637
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100638 err = ovl_check_namelen(path, ofs, name);
639 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100640 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100641
Miklos Szerediab508822014-12-13 00:59:49 +0100642 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
643
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200644 if (ovl_dentry_remote(path->dentry))
645 *remote = true;
646
Amir Goldstein02bcd152017-06-21 15:28:36 +0300647 /*
648 * The inodes index feature needs to encode and decode file
649 * handles, so it requires that all layers support them.
650 */
651 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
652 ofs->config.index = false;
653 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
654 }
655
Miklos Szerediab508822014-12-13 00:59:49 +0100656 return 0;
657
658out_put:
659 path_put(path);
660out:
661 return err;
662}
663
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200664/* Workdir should not be subdir of upperdir and vice versa */
665static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
666{
667 bool ok = false;
668
669 if (workdir != upperdir) {
670 ok = (lock_rename(workdir, upperdir) == NULL);
671 unlock_rename(workdir, upperdir);
672 }
673 return ok;
674}
675
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100676static unsigned int ovl_split_lowerdirs(char *str)
677{
678 unsigned int ctr = 1;
679 char *s, *d;
680
681 for (s = d = str;; s++, d++) {
682 if (*s == '\\') {
683 s++;
684 } else if (*s == ':') {
685 *d = '\0';
686 ctr++;
687 continue;
688 }
689 *d = *s;
690 if (!*s)
691 break;
692 }
693 return ctr;
694}
695
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200696static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200697ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
698 struct dentry *dentry, struct inode *inode,
699 const char *name, void *buffer, size_t size)
700{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200701 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200702}
703
704static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200705ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
706 struct dentry *dentry, struct inode *inode,
707 const char *name, const void *value,
708 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200709{
710 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200711 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200712 struct posix_acl *acl = NULL;
713 int err;
714
715 /* Check that everything is OK before copy-up */
716 if (value) {
717 acl = posix_acl_from_xattr(&init_user_ns, value, size);
718 if (IS_ERR(acl))
719 return PTR_ERR(acl);
720 }
721 err = -EOPNOTSUPP;
722 if (!IS_POSIXACL(d_inode(workdir)))
723 goto out_acl_release;
724 if (!realinode->i_op->set_acl)
725 goto out_acl_release;
726 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
727 err = acl ? -EACCES : 0;
728 goto out_acl_release;
729 }
730 err = -EPERM;
731 if (!inode_owner_or_capable(inode))
732 goto out_acl_release;
733
734 posix_acl_release(acl);
735
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100736 /*
737 * Check if sgid bit needs to be cleared (actual setacl operation will
738 * be done with mounter's capabilities and so that won't do it for us).
739 */
740 if (unlikely(inode->i_mode & S_ISGID) &&
741 handler->flags == ACL_TYPE_ACCESS &&
742 !in_group_p(inode->i_gid) &&
743 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
744 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
745
746 err = ovl_setattr(dentry, &iattr);
747 if (err)
748 return err;
749 }
750
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200751 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200752 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200753 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200754
755 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200756
757out_acl_release:
758 posix_acl_release(acl);
759 return err;
760}
761
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200762static int ovl_own_xattr_get(const struct xattr_handler *handler,
763 struct dentry *dentry, struct inode *inode,
764 const char *name, void *buffer, size_t size)
765{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200766 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200767}
768
Miklos Szeredid837a492016-07-29 12:05:24 +0200769static int ovl_own_xattr_set(const struct xattr_handler *handler,
770 struct dentry *dentry, struct inode *inode,
771 const char *name, const void *value,
772 size_t size, int flags)
773{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200774 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200775}
776
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200777static int ovl_other_xattr_get(const struct xattr_handler *handler,
778 struct dentry *dentry, struct inode *inode,
779 const char *name, void *buffer, size_t size)
780{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200781 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200782}
783
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200784static int ovl_other_xattr_set(const struct xattr_handler *handler,
785 struct dentry *dentry, struct inode *inode,
786 const char *name, const void *value,
787 size_t size, int flags)
788{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200789 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200790}
791
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200792static const struct xattr_handler __maybe_unused
793ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200794 .name = XATTR_NAME_POSIX_ACL_ACCESS,
795 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200796 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200797 .set = ovl_posix_acl_xattr_set,
798};
799
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200800static const struct xattr_handler __maybe_unused
801ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200802 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
803 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200804 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200805 .set = ovl_posix_acl_xattr_set,
806};
807
808static const struct xattr_handler ovl_own_xattr_handler = {
809 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200810 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200811 .set = ovl_own_xattr_set,
812};
813
814static const struct xattr_handler ovl_other_xattr_handler = {
815 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200816 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200817 .set = ovl_other_xattr_set,
818};
819
820static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200821#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200822 &ovl_posix_acl_access_xattr_handler,
823 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200824#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200825 &ovl_own_xattr_handler,
826 &ovl_other_xattr_handler,
827 NULL
828};
829
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200830static int ovl_fill_super(struct super_block *sb, void *data, int silent)
831{
Kees Cook33006cd2017-03-29 14:02:19 -0700832 struct path upperpath = { };
833 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200834 struct dentry *root_dentry;
835 struct ovl_entry *oe;
836 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100837 struct path *stack = NULL;
838 char *lowertmp;
839 char *lower;
840 unsigned int numlower;
841 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100842 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200843 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300844 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200845 int err;
846
Erez Zadokf45827e82014-10-24 00:14:38 +0200847 err = -ENOMEM;
848 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
849 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200850 goto out;
851
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100852 ufs->config.redirect_dir = ovl_redirect_dir_def;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300853 ufs->config.index = ovl_index_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200854 err = ovl_parse_opt((char *) data, &ufs->config);
855 if (err)
856 goto out_free_config;
857
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200858 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100859 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300860 if (!silent)
861 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200862 goto out_free_config;
863 }
864
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100865 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100866 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100867 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100868 if (!ufs->config.workdir) {
869 pr_err("overlayfs: missing 'workdir'\n");
870 goto out_free_config;
871 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200872
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100873 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
874 if (err)
875 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100876
hujianyang71cbad72015-01-15 13:20:57 +0800877 /* Upper fs should not be r/o */
David Howellsbc98a422017-07-17 08:45:34 +0100878 if (sb_rdonly(upperpath.mnt->mnt_sb)) {
hujianyang71cbad72015-01-15 13:20:57 +0800879 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
880 err = -EINVAL;
881 goto out_put_upperpath;
882 }
883
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100884 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
885 if (err)
886 goto out_put_upperpath;
887
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300888 err = -EBUSY;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300889 if (ovl_inuse_trylock(upperpath.dentry)) {
890 ufs->upperdir_locked = true;
891 } else if (ufs->config.index) {
892 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300893 goto out_put_upperpath;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300894 } else {
895 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300896 }
897
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100898 err = ovl_mount_dir(ufs->config.workdir, &workpath);
899 if (err)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300900 goto out_unlock_upperdentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100901
hujianyang2f83fd82015-01-06 12:52:13 +0800902 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100903 if (upperpath.mnt != workpath.mnt) {
904 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
905 goto out_put_workpath;
906 }
907 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
908 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
909 goto out_put_workpath;
910 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300911
912 err = -EBUSY;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300913 if (ovl_inuse_trylock(workpath.dentry)) {
914 ufs->workdir_locked = true;
915 } else if (ufs->config.index) {
916 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300917 goto out_put_workpath;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300918 } else {
919 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300920 }
921
922 ufs->workbasedir = workpath.dentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100923 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100924 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100925 err = -ENOMEM;
926 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
927 if (!lowertmp)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300928 goto out_unlock_workdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200929
930 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100931 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800932 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100933 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800934 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100935 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800936 } else if (!ufs->config.upperdir && stacklen == 1) {
937 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
938 goto out_free_lowertmp;
939 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100940
Geliang Tang313684c2016-11-18 23:02:46 +0800941 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100942 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
943 if (!stack)
944 goto out_free_lowertmp;
945
Geliang Tang313684c2016-11-18 23:02:46 +0800946 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100947 lower = lowertmp;
948 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100949 err = ovl_lower_dir(lower, &stack[numlower], ufs,
950 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100951 if (err)
952 goto out_put_lowerpath;
953
954 lower = strchr(lower, '\0') + 1;
955 }
956
957 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100958 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200959 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
960 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100961 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200962 }
963
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100964 if (ufs->config.upperdir) {
965 ufs->upper_mnt = clone_private_mount(&upperpath);
966 err = PTR_ERR(ufs->upper_mnt);
967 if (IS_ERR(ufs->upper_mnt)) {
968 pr_err("overlayfs: failed to clone upperpath\n");
969 goto out_put_lowerpath;
970 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300971
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200972 /* Don't inherit atime flags */
973 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
974
975 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100976
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300977 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
978 OVL_WORKDIR_NAME, false);
Vivek Goyal45aebea2016-02-22 09:28:34 -0500979 /*
980 * Upper should support d_type, else whiteouts are visible.
981 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400982 * iterate_dir() on workdir. This check requires successful
983 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500984 */
Vivek Goyal21765192016-05-20 09:04:26 -0400985 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200986 struct dentry *temp;
987
Vivek Goyal21765192016-05-20 09:04:26 -0400988 err = ovl_check_d_type_supported(&workpath);
989 if (err < 0)
990 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500991
Vivek Goyale7c0b592016-07-01 10:02:44 -0400992 /*
993 * We allowed this configuration and don't want to
994 * break users over kernel upgrade. So warn instead
995 * of erroring out.
996 */
997 if (!err)
998 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200999
1000 /* Check if upper/work fs supports O_TMPFILE */
1001 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
1002 ufs->tmpfile = !IS_ERR(temp);
1003 if (ufs->tmpfile)
1004 dput(temp);
1005 else
1006 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +03001007
1008 /*
1009 * Check if upper/work fs supports trusted.overlay.*
1010 * xattr
1011 */
1012 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
1013 "0", 1, 0);
1014 if (err) {
1015 ufs->noxattr = true;
1016 pr_warn("overlayfs: upper fs does not support xattr.\n");
1017 } else {
1018 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1019 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001020
1021 /* Check if upper/work fs supports file handles */
1022 if (ufs->config.index &&
1023 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1024 ufs->config.index = false;
1025 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1026 }
Vivek Goyal45aebea2016-02-22 09:28:34 -05001027 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001028 }
1029
hujianyang2f83fd82015-01-06 12:52:13 +08001030 err = -ENOMEM;
Chandan Rajendrab9343632017-07-24 01:57:54 -05001031 ufs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1032 GFP_KERNEL);
1033 if (ufs->lower_layers == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001034 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001035 for (i = 0; i < numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -05001036 struct vfsmount *mnt;
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +02001037 dev_t dev;
1038
1039 err = get_anon_bdev(&dev);
1040 if (err) {
1041 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1042 goto out_put_lower_layers;
1043 }
Miklos Szeredidd662662014-12-13 00:59:43 +01001044
Chandan Rajendrab9343632017-07-24 01:57:54 -05001045 mnt = clone_private_mount(&stack[i]);
hujianyang2f83fd82015-01-06 12:52:13 +08001046 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001047 if (IS_ERR(mnt)) {
1048 pr_err("overlayfs: failed to clone lowerpath\n");
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +02001049 free_anon_bdev(dev);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001050 goto out_put_lower_layers;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001051 }
1052 /*
Chandan Rajendrab9343632017-07-24 01:57:54 -05001053 * Make lower layers R/O. That way fchmod/fchown on lower file
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001054 * will fail instead of modifying lower fs.
1055 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001056 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001057
Chandan Rajendrab9343632017-07-24 01:57:54 -05001058 ufs->lower_layers[ufs->numlower].mnt = mnt;
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +02001059 ufs->lower_layers[ufs->numlower].pseudo_dev = dev;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001060 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001061
1062 /* Check if all lower layers are on same sb */
1063 if (i == 0)
1064 ufs->same_sb = mnt->mnt_sb;
1065 else if (ufs->same_sb != mnt->mnt_sb)
1066 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001067 }
1068
hujianyang71cbad72015-01-15 13:20:57 +08001069 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1070 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001071 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001072 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1073 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001074
Chandan Rajendrab9343632017-07-24 01:57:54 -05001075 err = -ENOMEM;
1076 oe = ovl_alloc_entry(numlower);
1077 if (!oe)
1078 goto out_put_lower_layers;
1079
1080 for (i = 0; i < numlower; i++) {
1081 oe->lowerstack[i].dentry = stack[i].dentry;
1082 oe->lowerstack[i].layer = &(ufs->lower_layers[i]);
1083 }
1084
Amir Goldstein02bcd152017-06-21 15:28:36 +03001085 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001086 /* Verify lower root is upper root origin */
Chandan Rajendrab9343632017-07-24 01:57:54 -05001087 err = ovl_verify_origin(upperpath.dentry,
1088 oe->lowerstack[0].layer->mnt,
1089 oe->lowerstack[0].dentry,
1090 false, true);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001091 if (err) {
1092 pr_err("overlayfs: failed to verify upper root origin\n");
Chandan Rajendrab9343632017-07-24 01:57:54 -05001093 goto out_free_oe;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001094 }
1095
Amir Goldstein02bcd152017-06-21 15:28:36 +03001096 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1097 OVL_INDEXDIR_NAME, true);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001098 if (ufs->indexdir) {
1099 /* Verify upper root is index dir origin */
1100 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
1101 upperpath.dentry, true, true);
1102 if (err)
1103 pr_err("overlayfs: failed to verify index dir origin\n");
Amir Goldstein415543d2017-06-21 15:28:42 +03001104
Amir Goldsteincaf70cb2017-06-21 13:46:12 +03001105 /* Cleanup bad/stale/orphan index entries */
Amir Goldstein415543d2017-06-21 15:28:42 +03001106 if (!err)
1107 err = ovl_indexdir_cleanup(ufs->indexdir,
1108 ufs->upper_mnt,
Chandan Rajendrab9343632017-07-24 01:57:54 -05001109 oe->lowerstack,
1110 numlower);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001111 }
1112 if (err || !ufs->indexdir)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001113 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 +03001114 if (err)
1115 goto out_put_indexdir;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001116 }
1117
1118 /* Show index=off/on in /proc/mounts for any of the reasons above */
1119 if (!ufs->indexdir)
1120 ufs->config.index = false;
1121
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001122 if (remote)
1123 sb->s_d_op = &ovl_reval_dentry_operations;
1124 else
1125 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001126
Amir Goldstein8fc646b2017-07-11 15:58:35 +03001127 err = -ENOMEM;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001128 ufs->creator_cred = cred = prepare_creds();
1129 if (!cred)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001130 goto out_put_indexdir;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001131
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001132 /* Never override disk quota limits or use reserved space */
1133 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1134
Vivek Goyal655042c2016-10-14 03:03:36 +02001135 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1136 sb->s_op = &ovl_super_operations;
1137 sb->s_xattr = ovl_xattr_handlers;
1138 sb->s_fs_info = ufs;
1139 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1140
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001141 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001142 if (!root_dentry)
Chandan Rajendrab9343632017-07-24 01:57:54 -05001143 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001144
1145 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001146 for (i = 0; i < numlower; i++)
1147 mntput(stack[i].mnt);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001148 kfree(stack);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001149 mntput(workpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001150 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001151
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001152 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001153 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001154 if (ovl_is_impuredir(upperpath.dentry))
1155 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001156 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001157
1158 root_dentry->d_fsdata = oe;
1159
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001160 /* Root is always merge -> can have whiteouts */
1161 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001162 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1163 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001164
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001165 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001166
1167 return 0;
1168
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001169out_put_cred:
1170 put_cred(ufs->creator_cred);
Amir Goldstein02bcd152017-06-21 15:28:36 +03001171out_put_indexdir:
1172 dput(ufs->indexdir);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001173out_free_oe:
1174 kfree(oe);
1175out_put_lower_layers:
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +02001176 for (i = 0; i < ufs->numlower; i++) {
1177 if (ufs->lower_layers[i].mnt)
1178 free_anon_bdev(ufs->lower_layers[i].pseudo_dev);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001179 mntput(ufs->lower_layers[i].mnt);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +02001180 }
Chandan Rajendrab9343632017-07-24 01:57:54 -05001181 kfree(ufs->lower_layers);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001182out_put_workdir:
1183 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001184 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001185out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001186 for (i = 0; i < numlower; i++)
1187 path_put(&stack[i]);
1188 kfree(stack);
1189out_free_lowertmp:
1190 kfree(lowertmp);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001191out_unlock_workdentry:
Amir Goldstein85fdee12017-09-29 10:21:21 +03001192 if (ufs->workdir_locked)
1193 ovl_inuse_unlock(workpath.dentry);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001194out_put_workpath:
1195 path_put(&workpath);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001196out_unlock_upperdentry:
Amir Goldstein85fdee12017-09-29 10:21:21 +03001197 if (ufs->upperdir_locked)
1198 ovl_inuse_unlock(upperpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001199out_put_upperpath:
1200 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001201out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001202 kfree(ufs->config.lowerdir);
1203 kfree(ufs->config.upperdir);
1204 kfree(ufs->config.workdir);
1205 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001206out:
1207 return err;
1208}
1209
1210static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1211 const char *dev_name, void *raw_data)
1212{
1213 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1214}
1215
1216static struct file_system_type ovl_fs_type = {
1217 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001218 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001219 .mount = ovl_mount,
1220 .kill_sb = kill_anon_super,
1221};
Miklos Szeredief94b182014-11-20 16:39:59 +01001222MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001223
Amir Goldstein13cf1992017-06-12 09:54:40 +03001224static void ovl_inode_init_once(void *foo)
1225{
1226 struct ovl_inode *oi = foo;
1227
1228 inode_init_once(&oi->vfs_inode);
1229}
1230
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001231static int __init ovl_init(void)
1232{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001233 int err;
1234
1235 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1236 sizeof(struct ovl_inode), 0,
1237 (SLAB_RECLAIM_ACCOUNT|
1238 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1239 ovl_inode_init_once);
1240 if (ovl_inode_cachep == NULL)
1241 return -ENOMEM;
1242
1243 err = register_filesystem(&ovl_fs_type);
1244 if (err)
1245 kmem_cache_destroy(ovl_inode_cachep);
1246
1247 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001248}
1249
1250static void __exit ovl_exit(void)
1251{
1252 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001253
1254 /*
1255 * Make sure all delayed rcu free inodes are flushed before we
1256 * destroy cache.
1257 */
1258 rcu_barrier();
1259 kmem_cache_destroy(ovl_inode_cachep);
1260
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001261}
1262
1263module_init(ovl_init);
1264module_exit(ovl_exit);