blob: a10fff49194b9a75228a29b1d09a5435d904e906 [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);
Miklos Szeredidd662662014-12-13 00:59:43 +0100222 for (i = 0; i < ufs->numlower; i++)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500223 mntput(ufs->lower_layers[i].mnt);
224 kfree(ufs->lower_layers);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200225
Erez Zadokf45827e82014-10-24 00:14:38 +0200226 kfree(ufs->config.lowerdir);
227 kfree(ufs->config.upperdir);
228 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200229 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200230 kfree(ufs);
231}
232
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200233static int ovl_sync_fs(struct super_block *sb, int wait)
234{
235 struct ovl_fs *ufs = sb->s_fs_info;
236 struct super_block *upper_sb;
237 int ret;
238
239 if (!ufs->upper_mnt)
240 return 0;
241 upper_sb = ufs->upper_mnt->mnt_sb;
242 if (!upper_sb->s_op->sync_fs)
243 return 0;
244
245 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
246 down_read(&upper_sb->s_umount);
247 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
248 up_read(&upper_sb->s_umount);
249 return ret;
250}
251
Andy Whitcroftcc259632014-10-24 00:14:38 +0200252/**
253 * ovl_statfs
254 * @sb: The overlayfs super block
255 * @buf: The struct kstatfs to fill in with stats
256 *
257 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100258 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200259 */
260static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
261{
262 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
263 struct dentry *root_dentry = dentry->d_sb->s_root;
264 struct path path;
265 int err;
266
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100267 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200268
269 err = vfs_statfs(&path, buf);
270 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100271 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200272 buf->f_type = OVERLAYFS_SUPER_MAGIC;
273 }
274
275 return err;
276}
277
Amir Goldstein02bcd152017-06-21 15:28:36 +0300278/* Will this overlay be forced to mount/remount ro? */
279static bool ovl_force_readonly(struct ovl_fs *ufs)
280{
281 return (!ufs->upper_mnt || !ufs->workdir);
282}
283
Erez Zadokf45827e82014-10-24 00:14:38 +0200284/**
285 * ovl_show_options
286 *
287 * Prints the mount options for a given superblock.
288 * Returns zero; does not fail.
289 */
290static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
291{
292 struct super_block *sb = dentry->d_sb;
293 struct ovl_fs *ufs = sb->s_fs_info;
294
Kees Cooka068acf2015-09-04 15:44:57 -0700295 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100296 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700297 seq_show_option(m, "upperdir", ufs->config.upperdir);
298 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100299 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200300 if (ufs->config.default_permissions)
301 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200302 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
303 seq_printf(m, ",redirect_dir=%s",
304 ufs->config.redirect_dir ? "on" : "off");
Amir Goldstein02bcd152017-06-21 15:28:36 +0300305 if (ufs->config.index != ovl_index_def)
306 seq_printf(m, ",index=%s",
307 ufs->config.index ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200308 return 0;
309}
310
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900311static int ovl_remount(struct super_block *sb, int *flags, char *data)
312{
313 struct ovl_fs *ufs = sb->s_fs_info;
314
Amir Goldstein02bcd152017-06-21 15:28:36 +0300315 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900316 return -EROFS;
317
318 return 0;
319}
320
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200321static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300322 .alloc_inode = ovl_alloc_inode,
323 .destroy_inode = ovl_destroy_inode,
324 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200326 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200327 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200328 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900329 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330};
331
332enum {
333 OPT_LOWERDIR,
334 OPT_UPPERDIR,
335 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200336 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100337 OPT_REDIRECT_DIR_ON,
338 OPT_REDIRECT_DIR_OFF,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300339 OPT_INDEX_ON,
340 OPT_INDEX_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200341 OPT_ERR,
342};
343
344static const match_table_t ovl_tokens = {
345 {OPT_LOWERDIR, "lowerdir=%s"},
346 {OPT_UPPERDIR, "upperdir=%s"},
347 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200348 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100349 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
350 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300351 {OPT_INDEX_ON, "index=on"},
352 {OPT_INDEX_OFF, "index=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200353 {OPT_ERR, NULL}
354};
355
Miklos Szeredi91c77942014-11-20 16:40:00 +0100356static char *ovl_next_opt(char **s)
357{
358 char *sbegin = *s;
359 char *p;
360
361 if (sbegin == NULL)
362 return NULL;
363
364 for (p = sbegin; *p; p++) {
365 if (*p == '\\') {
366 p++;
367 if (!*p)
368 break;
369 } else if (*p == ',') {
370 *p = '\0';
371 *s = p + 1;
372 return sbegin;
373 }
374 }
375 *s = NULL;
376 return sbegin;
377}
378
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200379static int ovl_parse_opt(char *opt, struct ovl_config *config)
380{
381 char *p;
382
Miklos Szeredi91c77942014-11-20 16:40:00 +0100383 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200384 int token;
385 substring_t args[MAX_OPT_ARGS];
386
387 if (!*p)
388 continue;
389
390 token = match_token(p, ovl_tokens, args);
391 switch (token) {
392 case OPT_UPPERDIR:
393 kfree(config->upperdir);
394 config->upperdir = match_strdup(&args[0]);
395 if (!config->upperdir)
396 return -ENOMEM;
397 break;
398
399 case OPT_LOWERDIR:
400 kfree(config->lowerdir);
401 config->lowerdir = match_strdup(&args[0]);
402 if (!config->lowerdir)
403 return -ENOMEM;
404 break;
405
406 case OPT_WORKDIR:
407 kfree(config->workdir);
408 config->workdir = match_strdup(&args[0]);
409 if (!config->workdir)
410 return -ENOMEM;
411 break;
412
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200413 case OPT_DEFAULT_PERMISSIONS:
414 config->default_permissions = true;
415 break;
416
Miklos Szeredia6c60652016-12-16 11:02:56 +0100417 case OPT_REDIRECT_DIR_ON:
418 config->redirect_dir = true;
419 break;
420
421 case OPT_REDIRECT_DIR_OFF:
422 config->redirect_dir = false;
423 break;
424
Amir Goldstein02bcd152017-06-21 15:28:36 +0300425 case OPT_INDEX_ON:
426 config->index = true;
427 break;
428
429 case OPT_INDEX_OFF:
430 config->index = false;
431 break;
432
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200433 default:
hujianyangbead55e2015-01-15 13:17:36 +0800434 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200435 return -EINVAL;
436 }
437 }
hujianyang71cbad72015-01-15 13:20:57 +0800438
439 /* Workdir is useless in non-upper mount */
440 if (!config->upperdir && config->workdir) {
441 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
442 config->workdir);
443 kfree(config->workdir);
444 config->workdir = NULL;
445 }
446
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200447 return 0;
448}
449
450#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300451#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200452
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300453static struct dentry *ovl_workdir_create(struct super_block *sb,
454 struct ovl_fs *ufs,
455 struct dentry *dentry,
456 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200457{
458 struct inode *dir = dentry->d_inode;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300459 struct vfsmount *mnt = ufs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460 struct dentry *work;
461 int err;
462 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300463 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200464
465 err = mnt_want_write(mnt);
466 if (err)
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300467 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200468
Al Viro59551022016-01-22 15:40:57 -0500469 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300470 locked = true;
471
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200472retry:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300473 work = lookup_one_len(name, dentry, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200474
475 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200476 struct iattr attr = {
477 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000478 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200479 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480
481 if (work->d_inode) {
482 err = -EEXIST;
483 if (retried)
484 goto out_dput;
485
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300486 if (persist)
487 goto out_unlock;
488
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200489 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200490 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200491 dput(work);
492 goto retry;
493 }
494
Al Viro32a3d842016-12-04 17:33:17 +0000495 err = ovl_create_real(dir, work,
496 &(struct cattr){.mode = S_IFDIR | 0},
497 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200498 if (err)
499 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200500
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200501 /*
502 * Try to remove POSIX ACL xattrs from workdir. We are good if:
503 *
504 * a) success (there was a POSIX ACL xattr and was removed)
505 * b) -ENODATA (there was no POSIX ACL xattr)
506 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
507 *
508 * There are various other error values that could effectively
509 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
510 * if the xattr name is too long), but the set of filesystems
511 * allowed as upper are limited to "normal" ones, where checking
512 * for the above two errors is sufficient.
513 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200514 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
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 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200519 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200520 goto out_dput;
521
522 /* Clear any inherited mode bits */
523 inode_lock(work->d_inode);
524 err = notify_change(work, &attr, NULL);
525 inode_unlock(work->d_inode);
526 if (err)
527 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300528 } else {
529 err = PTR_ERR(work);
530 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200531 }
532out_unlock:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200533 mnt_drop_write(mnt);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300534 if (locked)
535 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200536
537 return work;
538
539out_dput:
540 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300541out_err:
542 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
543 ufs->config.workdir, name, -err);
544 sb->s_flags |= MS_RDONLY;
545 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200546 goto out_unlock;
547}
548
Miklos Szeredi91c77942014-11-20 16:40:00 +0100549static void ovl_unescape(char *s)
550{
551 char *d = s;
552
553 for (;; s++, d++) {
554 if (*s == '\\')
555 s++;
556 *d = *s;
557 if (!*s)
558 break;
559 }
560}
561
Miklos Szerediab508822014-12-13 00:59:49 +0100562static int ovl_mount_dir_noesc(const char *name, struct path *path)
563{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100564 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100565
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100566 if (!*name) {
567 pr_err("overlayfs: empty lowerdir\n");
568 goto out;
569 }
Miklos Szerediab508822014-12-13 00:59:49 +0100570 err = kern_path(name, LOOKUP_FOLLOW, path);
571 if (err) {
572 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
573 goto out;
574 }
575 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200576 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100577 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
578 goto out_put;
579 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100580 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100581 pr_err("overlayfs: '%s' not a directory\n", name);
582 goto out_put;
583 }
584 return 0;
585
586out_put:
587 path_put(path);
588out:
589 return err;
590}
591
592static int ovl_mount_dir(const char *name, struct path *path)
593{
594 int err = -ENOMEM;
595 char *tmp = kstrdup(name, GFP_KERNEL);
596
597 if (tmp) {
598 ovl_unescape(tmp);
599 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200600
601 if (!err)
602 if (ovl_dentry_remote(path->dentry)) {
603 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
604 tmp);
605 path_put(path);
606 err = -EINVAL;
607 }
Miklos Szerediab508822014-12-13 00:59:49 +0100608 kfree(tmp);
609 }
610 return err;
611}
612
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100613static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
614 const char *name)
615{
616 struct kstatfs statfs;
617 int err = vfs_statfs(path, &statfs);
618
619 if (err)
620 pr_err("overlayfs: statfs failed on '%s'\n", name);
621 else
622 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
623
624 return err;
625}
626
627static int ovl_lower_dir(const char *name, struct path *path,
628 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100629{
630 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100631
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100632 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100633 if (err)
634 goto out;
635
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100636 err = ovl_check_namelen(path, ofs, name);
637 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100638 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100639
Miklos Szerediab508822014-12-13 00:59:49 +0100640 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
641
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200642 if (ovl_dentry_remote(path->dentry))
643 *remote = true;
644
Amir Goldstein02bcd152017-06-21 15:28:36 +0300645 /*
646 * The inodes index feature needs to encode and decode file
647 * handles, so it requires that all layers support them.
648 */
649 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
650 ofs->config.index = false;
651 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
652 }
653
Miklos Szerediab508822014-12-13 00:59:49 +0100654 return 0;
655
656out_put:
657 path_put(path);
658out:
659 return err;
660}
661
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200662/* Workdir should not be subdir of upperdir and vice versa */
663static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
664{
665 bool ok = false;
666
667 if (workdir != upperdir) {
668 ok = (lock_rename(workdir, upperdir) == NULL);
669 unlock_rename(workdir, upperdir);
670 }
671 return ok;
672}
673
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100674static unsigned int ovl_split_lowerdirs(char *str)
675{
676 unsigned int ctr = 1;
677 char *s, *d;
678
679 for (s = d = str;; s++, d++) {
680 if (*s == '\\') {
681 s++;
682 } else if (*s == ':') {
683 *d = '\0';
684 ctr++;
685 continue;
686 }
687 *d = *s;
688 if (!*s)
689 break;
690 }
691 return ctr;
692}
693
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200694static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200695ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
696 struct dentry *dentry, struct inode *inode,
697 const char *name, void *buffer, size_t size)
698{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200699 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200700}
701
702static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200703ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
704 struct dentry *dentry, struct inode *inode,
705 const char *name, const void *value,
706 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200707{
708 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200709 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200710 struct posix_acl *acl = NULL;
711 int err;
712
713 /* Check that everything is OK before copy-up */
714 if (value) {
715 acl = posix_acl_from_xattr(&init_user_ns, value, size);
716 if (IS_ERR(acl))
717 return PTR_ERR(acl);
718 }
719 err = -EOPNOTSUPP;
720 if (!IS_POSIXACL(d_inode(workdir)))
721 goto out_acl_release;
722 if (!realinode->i_op->set_acl)
723 goto out_acl_release;
724 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
725 err = acl ? -EACCES : 0;
726 goto out_acl_release;
727 }
728 err = -EPERM;
729 if (!inode_owner_or_capable(inode))
730 goto out_acl_release;
731
732 posix_acl_release(acl);
733
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100734 /*
735 * Check if sgid bit needs to be cleared (actual setacl operation will
736 * be done with mounter's capabilities and so that won't do it for us).
737 */
738 if (unlikely(inode->i_mode & S_ISGID) &&
739 handler->flags == ACL_TYPE_ACCESS &&
740 !in_group_p(inode->i_gid) &&
741 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
742 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
743
744 err = ovl_setattr(dentry, &iattr);
745 if (err)
746 return err;
747 }
748
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200749 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200750 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200751 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200752
753 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200754
755out_acl_release:
756 posix_acl_release(acl);
757 return err;
758}
759
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200760static int ovl_own_xattr_get(const struct xattr_handler *handler,
761 struct dentry *dentry, struct inode *inode,
762 const char *name, void *buffer, size_t size)
763{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200764 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200765}
766
Miklos Szeredid837a492016-07-29 12:05:24 +0200767static int ovl_own_xattr_set(const struct xattr_handler *handler,
768 struct dentry *dentry, struct inode *inode,
769 const char *name, const void *value,
770 size_t size, int flags)
771{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200772 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200773}
774
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200775static int ovl_other_xattr_get(const struct xattr_handler *handler,
776 struct dentry *dentry, struct inode *inode,
777 const char *name, void *buffer, size_t size)
778{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200779 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200780}
781
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200782static int ovl_other_xattr_set(const struct xattr_handler *handler,
783 struct dentry *dentry, struct inode *inode,
784 const char *name, const void *value,
785 size_t size, int flags)
786{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200787 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200788}
789
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200790static const struct xattr_handler __maybe_unused
791ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200792 .name = XATTR_NAME_POSIX_ACL_ACCESS,
793 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200794 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200795 .set = ovl_posix_acl_xattr_set,
796};
797
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200798static const struct xattr_handler __maybe_unused
799ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200800 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
801 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200802 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200803 .set = ovl_posix_acl_xattr_set,
804};
805
806static const struct xattr_handler ovl_own_xattr_handler = {
807 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200808 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200809 .set = ovl_own_xattr_set,
810};
811
812static const struct xattr_handler ovl_other_xattr_handler = {
813 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200814 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200815 .set = ovl_other_xattr_set,
816};
817
818static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200819#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200820 &ovl_posix_acl_access_xattr_handler,
821 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200822#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200823 &ovl_own_xattr_handler,
824 &ovl_other_xattr_handler,
825 NULL
826};
827
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200828static int ovl_fill_super(struct super_block *sb, void *data, int silent)
829{
Kees Cook33006cd2017-03-29 14:02:19 -0700830 struct path upperpath = { };
831 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200832 struct dentry *root_dentry;
833 struct ovl_entry *oe;
834 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100835 struct path *stack = NULL;
836 char *lowertmp;
837 char *lower;
838 unsigned int numlower;
839 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100840 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200841 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300842 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200843 int err;
844
Erez Zadokf45827e82014-10-24 00:14:38 +0200845 err = -ENOMEM;
846 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
847 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200848 goto out;
849
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100850 ufs->config.redirect_dir = ovl_redirect_dir_def;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300851 ufs->config.index = ovl_index_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200852 err = ovl_parse_opt((char *) data, &ufs->config);
853 if (err)
854 goto out_free_config;
855
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200856 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100857 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300858 if (!silent)
859 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200860 goto out_free_config;
861 }
862
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100863 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100864 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100865 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100866 if (!ufs->config.workdir) {
867 pr_err("overlayfs: missing 'workdir'\n");
868 goto out_free_config;
869 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200870
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100871 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
872 if (err)
873 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100874
hujianyang71cbad72015-01-15 13:20:57 +0800875 /* Upper fs should not be r/o */
David Howellsbc98a422017-07-17 08:45:34 +0100876 if (sb_rdonly(upperpath.mnt->mnt_sb)) {
hujianyang71cbad72015-01-15 13:20:57 +0800877 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
878 err = -EINVAL;
879 goto out_put_upperpath;
880 }
881
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100882 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
883 if (err)
884 goto out_put_upperpath;
885
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300886 err = -EBUSY;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300887 if (ovl_inuse_trylock(upperpath.dentry)) {
888 ufs->upperdir_locked = true;
889 } else if (ufs->config.index) {
890 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 +0300891 goto out_put_upperpath;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300892 } else {
893 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 +0300894 }
895
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100896 err = ovl_mount_dir(ufs->config.workdir, &workpath);
897 if (err)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300898 goto out_unlock_upperdentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100899
hujianyang2f83fd82015-01-06 12:52:13 +0800900 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100901 if (upperpath.mnt != workpath.mnt) {
902 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
903 goto out_put_workpath;
904 }
905 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
906 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
907 goto out_put_workpath;
908 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300909
910 err = -EBUSY;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300911 if (ovl_inuse_trylock(workpath.dentry)) {
912 ufs->workdir_locked = true;
913 } else if (ufs->config.index) {
914 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 +0300915 goto out_put_workpath;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300916 } else {
917 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 +0300918 }
919
920 ufs->workbasedir = workpath.dentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100921 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100922 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100923 err = -ENOMEM;
924 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
925 if (!lowertmp)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300926 goto out_unlock_workdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200927
928 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100929 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800930 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100931 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800932 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100933 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800934 } else if (!ufs->config.upperdir && stacklen == 1) {
935 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
936 goto out_free_lowertmp;
937 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100938
Geliang Tang313684c2016-11-18 23:02:46 +0800939 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100940 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
941 if (!stack)
942 goto out_free_lowertmp;
943
Geliang Tang313684c2016-11-18 23:02:46 +0800944 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100945 lower = lowertmp;
946 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100947 err = ovl_lower_dir(lower, &stack[numlower], ufs,
948 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100949 if (err)
950 goto out_put_lowerpath;
951
952 lower = strchr(lower, '\0') + 1;
953 }
954
955 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100956 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200957 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
958 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100959 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200960 }
961
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100962 if (ufs->config.upperdir) {
963 ufs->upper_mnt = clone_private_mount(&upperpath);
964 err = PTR_ERR(ufs->upper_mnt);
965 if (IS_ERR(ufs->upper_mnt)) {
966 pr_err("overlayfs: failed to clone upperpath\n");
967 goto out_put_lowerpath;
968 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300969
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200970 /* Don't inherit atime flags */
971 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
972
973 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100974
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300975 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
976 OVL_WORKDIR_NAME, false);
Vivek Goyal45aebea2016-02-22 09:28:34 -0500977 /*
978 * Upper should support d_type, else whiteouts are visible.
979 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400980 * iterate_dir() on workdir. This check requires successful
981 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500982 */
Vivek Goyal21765192016-05-20 09:04:26 -0400983 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200984 struct dentry *temp;
985
Vivek Goyal21765192016-05-20 09:04:26 -0400986 err = ovl_check_d_type_supported(&workpath);
987 if (err < 0)
988 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500989
Vivek Goyale7c0b592016-07-01 10:02:44 -0400990 /*
991 * We allowed this configuration and don't want to
992 * break users over kernel upgrade. So warn instead
993 * of erroring out.
994 */
995 if (!err)
996 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200997
998 /* Check if upper/work fs supports O_TMPFILE */
999 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
1000 ufs->tmpfile = !IS_ERR(temp);
1001 if (ufs->tmpfile)
1002 dput(temp);
1003 else
1004 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +03001005
1006 /*
1007 * Check if upper/work fs supports trusted.overlay.*
1008 * xattr
1009 */
1010 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
1011 "0", 1, 0);
1012 if (err) {
1013 ufs->noxattr = true;
1014 pr_warn("overlayfs: upper fs does not support xattr.\n");
1015 } else {
1016 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1017 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001018
1019 /* Check if upper/work fs supports file handles */
1020 if (ufs->config.index &&
1021 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1022 ufs->config.index = false;
1023 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1024 }
Vivek Goyal45aebea2016-02-22 09:28:34 -05001025 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001026 }
1027
hujianyang2f83fd82015-01-06 12:52:13 +08001028 err = -ENOMEM;
Chandan Rajendrab9343632017-07-24 01:57:54 -05001029 ufs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1030 GFP_KERNEL);
1031 if (ufs->lower_layers == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001032 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001033 for (i = 0; i < numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -05001034 struct vfsmount *mnt;
Miklos Szeredidd662662014-12-13 00:59:43 +01001035
Chandan Rajendrab9343632017-07-24 01:57:54 -05001036 mnt = clone_private_mount(&stack[i]);
hujianyang2f83fd82015-01-06 12:52:13 +08001037 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001038 if (IS_ERR(mnt)) {
1039 pr_err("overlayfs: failed to clone lowerpath\n");
Chandan Rajendrab9343632017-07-24 01:57:54 -05001040 goto out_put_lower_layers;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001041 }
1042 /*
Chandan Rajendrab9343632017-07-24 01:57:54 -05001043 * Make lower layers R/O. That way fchmod/fchown on lower file
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001044 * will fail instead of modifying lower fs.
1045 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001046 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001047
Chandan Rajendrab9343632017-07-24 01:57:54 -05001048 ufs->lower_layers[ufs->numlower].mnt = mnt;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001049 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001050
1051 /* Check if all lower layers are on same sb */
1052 if (i == 0)
1053 ufs->same_sb = mnt->mnt_sb;
1054 else if (ufs->same_sb != mnt->mnt_sb)
1055 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001056 }
1057
hujianyang71cbad72015-01-15 13:20:57 +08001058 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1059 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001060 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001061 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1062 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001063
Chandan Rajendrab9343632017-07-24 01:57:54 -05001064 err = -ENOMEM;
1065 oe = ovl_alloc_entry(numlower);
1066 if (!oe)
1067 goto out_put_lower_layers;
1068
1069 for (i = 0; i < numlower; i++) {
1070 oe->lowerstack[i].dentry = stack[i].dentry;
1071 oe->lowerstack[i].layer = &(ufs->lower_layers[i]);
1072 }
1073
Amir Goldstein02bcd152017-06-21 15:28:36 +03001074 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001075 /* Verify lower root is upper root origin */
Chandan Rajendrab9343632017-07-24 01:57:54 -05001076 err = ovl_verify_origin(upperpath.dentry,
1077 oe->lowerstack[0].layer->mnt,
1078 oe->lowerstack[0].dentry,
1079 false, true);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001080 if (err) {
1081 pr_err("overlayfs: failed to verify upper root origin\n");
Chandan Rajendrab9343632017-07-24 01:57:54 -05001082 goto out_free_oe;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001083 }
1084
Amir Goldstein02bcd152017-06-21 15:28:36 +03001085 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1086 OVL_INDEXDIR_NAME, true);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001087 if (ufs->indexdir) {
1088 /* Verify upper root is index dir origin */
1089 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
1090 upperpath.dentry, true, true);
1091 if (err)
1092 pr_err("overlayfs: failed to verify index dir origin\n");
Amir Goldstein415543d2017-06-21 15:28:42 +03001093
Amir Goldsteincaf70cb2017-06-21 13:46:12 +03001094 /* Cleanup bad/stale/orphan index entries */
Amir Goldstein415543d2017-06-21 15:28:42 +03001095 if (!err)
1096 err = ovl_indexdir_cleanup(ufs->indexdir,
1097 ufs->upper_mnt,
Chandan Rajendrab9343632017-07-24 01:57:54 -05001098 oe->lowerstack,
1099 numlower);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001100 }
1101 if (err || !ufs->indexdir)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001102 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 +03001103 if (err)
1104 goto out_put_indexdir;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001105 }
1106
1107 /* Show index=off/on in /proc/mounts for any of the reasons above */
1108 if (!ufs->indexdir)
1109 ufs->config.index = false;
1110
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001111 if (remote)
1112 sb->s_d_op = &ovl_reval_dentry_operations;
1113 else
1114 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001115
Amir Goldstein8fc646b2017-07-11 15:58:35 +03001116 err = -ENOMEM;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001117 ufs->creator_cred = cred = prepare_creds();
1118 if (!cred)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001119 goto out_put_indexdir;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001120
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001121 /* Never override disk quota limits or use reserved space */
1122 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1123
Vivek Goyal655042c2016-10-14 03:03:36 +02001124 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1125 sb->s_op = &ovl_super_operations;
1126 sb->s_xattr = ovl_xattr_handlers;
1127 sb->s_fs_info = ufs;
1128 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1129
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001130 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001131 if (!root_dentry)
Chandan Rajendrab9343632017-07-24 01:57:54 -05001132 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001133
1134 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001135 for (i = 0; i < numlower; i++)
1136 mntput(stack[i].mnt);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001137 kfree(stack);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001138 mntput(workpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001139 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001140
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001141 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001142 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001143 if (ovl_is_impuredir(upperpath.dentry))
1144 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001145 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001146
1147 root_dentry->d_fsdata = oe;
1148
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001149 /* Root is always merge -> can have whiteouts */
1150 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001151 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1152 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001153
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001154 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001155
1156 return 0;
1157
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001158out_put_cred:
1159 put_cred(ufs->creator_cred);
Amir Goldstein02bcd152017-06-21 15:28:36 +03001160out_put_indexdir:
1161 dput(ufs->indexdir);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001162out_free_oe:
1163 kfree(oe);
1164out_put_lower_layers:
Miklos Szeredidd662662014-12-13 00:59:43 +01001165 for (i = 0; i < ufs->numlower; i++)
Chandan Rajendrab9343632017-07-24 01:57:54 -05001166 mntput(ufs->lower_layers[i].mnt);
1167 kfree(ufs->lower_layers);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001168out_put_workdir:
1169 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001170 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001171out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001172 for (i = 0; i < numlower; i++)
1173 path_put(&stack[i]);
1174 kfree(stack);
1175out_free_lowertmp:
1176 kfree(lowertmp);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001177out_unlock_workdentry:
Amir Goldstein85fdee12017-09-29 10:21:21 +03001178 if (ufs->workdir_locked)
1179 ovl_inuse_unlock(workpath.dentry);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001180out_put_workpath:
1181 path_put(&workpath);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001182out_unlock_upperdentry:
Amir Goldstein85fdee12017-09-29 10:21:21 +03001183 if (ufs->upperdir_locked)
1184 ovl_inuse_unlock(upperpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001185out_put_upperpath:
1186 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001187out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001188 kfree(ufs->config.lowerdir);
1189 kfree(ufs->config.upperdir);
1190 kfree(ufs->config.workdir);
1191 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001192out:
1193 return err;
1194}
1195
1196static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1197 const char *dev_name, void *raw_data)
1198{
1199 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1200}
1201
1202static struct file_system_type ovl_fs_type = {
1203 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001204 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001205 .mount = ovl_mount,
1206 .kill_sb = kill_anon_super,
1207};
Miklos Szeredief94b182014-11-20 16:39:59 +01001208MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001209
Amir Goldstein13cf1992017-06-12 09:54:40 +03001210static void ovl_inode_init_once(void *foo)
1211{
1212 struct ovl_inode *oi = foo;
1213
1214 inode_init_once(&oi->vfs_inode);
1215}
1216
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001217static int __init ovl_init(void)
1218{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001219 int err;
1220
1221 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1222 sizeof(struct ovl_inode), 0,
1223 (SLAB_RECLAIM_ACCOUNT|
1224 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1225 ovl_inode_init_once);
1226 if (ovl_inode_cachep == NULL)
1227 return -ENOMEM;
1228
1229 err = register_filesystem(&ovl_fs_type);
1230 if (err)
1231 kmem_cache_destroy(ovl_inode_cachep);
1232
1233 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001234}
1235
1236static void __exit ovl_exit(void)
1237{
1238 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001239
1240 /*
1241 * Make sure all delayed rcu free inodes are flushed before we
1242 * destroy cache.
1243 */
1244 rcu_barrier();
1245 kmem_cache_destroy(ovl_inode_cachep);
1246
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001247}
1248
1249module_init(ovl_init);
1250module_exit(ovl_exit);