blob: 255c0523148fd3dcbf6e1124ac5a2f5f1c28561a [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:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100589 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100590out:
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);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100607 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200608 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:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100659 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100660out:
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 Szeredi6ee8acf2017-11-09 10:23:28 +0100830static int ovl_get_upperpath(struct ovl_fs *ufs, struct path *upperpath)
831{
832 int err;
833
834 err = ovl_mount_dir(ufs->config.upperdir, upperpath);
835 if (err)
836 goto out;
837
838 /* Upper fs should not be r/o */
839 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
840 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
841 err = -EINVAL;
842 goto out;
843 }
844
845 err = ovl_check_namelen(upperpath, ufs, ufs->config.upperdir);
846 if (err)
847 goto out;
848
849 err = -EBUSY;
850 if (ovl_inuse_trylock(upperpath->dentry)) {
851 ufs->upperdir_locked = true;
852 } else if (ufs->config.index) {
853 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
854 goto out;
855 } else {
856 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
857 }
858 err = 0;
859out:
860 return err;
861}
862
Miklos Szeredi87ad4472017-11-09 10:23:28 +0100863static int ovl_get_workpath(struct ovl_fs *ufs, struct path *upperpath,
864 struct path *workpath)
865{
866 int err;
867
868 err = ovl_mount_dir(ufs->config.workdir, workpath);
869 if (err)
870 goto out;
871
872 err = -EINVAL;
873 if (upperpath->mnt != workpath->mnt) {
874 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
875 goto out;
876 }
877 if (!ovl_workdir_ok(workpath->dentry, upperpath->dentry)) {
878 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
879 goto out;
880 }
881
882 err = -EBUSY;
883 if (ovl_inuse_trylock(workpath->dentry)) {
884 ufs->workdir_locked = true;
885 } else if (ufs->config.index) {
886 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
887 goto out;
888 } else {
889 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
890 }
891
Miklos Szeredi95e6d412017-11-09 10:23:29 +0100892 ufs->workbasedir = dget(workpath->dentry);
Miklos Szeredi87ad4472017-11-09 10:23:28 +0100893 err = 0;
894out:
895 return err;
896}
897
Miklos Szeredi21a3b312017-11-09 10:23:28 +0100898static int ovl_get_upper(struct ovl_fs *ufs, struct path *upperpath)
899{
900 struct vfsmount *upper_mnt;
901
902 upper_mnt = clone_private_mount(upperpath);
903 if (IS_ERR(upper_mnt)) {
904 pr_err("overlayfs: failed to clone upperpath\n");
905 return PTR_ERR(upper_mnt);
906 }
907
908 /* Don't inherit atime flags */
909 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
910 ufs->upper_mnt = upper_mnt;
911
912 return 0;
913}
914
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100915static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ufs,
916 struct path *workpath)
917{
918 struct dentry *temp;
919 int err;
920
Miklos Szeredi95e6d412017-11-09 10:23:29 +0100921 ufs->workdir = ovl_workdir_create(sb, ufs, ufs->workbasedir,
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100922 OVL_WORKDIR_NAME, false);
923 if (!ufs->workdir)
924 return 0;
925
926 /*
927 * Upper should support d_type, else whiteouts are visible. Given
928 * workdir and upper are on same fs, we can do iterate_dir() on
929 * workdir. This check requires successful creation of workdir in
930 * previous step.
931 */
932 err = ovl_check_d_type_supported(workpath);
933 if (err < 0)
934 return err;
935
936 /*
937 * We allowed this configuration and don't want to break users over
938 * kernel upgrade. So warn instead of erroring out.
939 */
940 if (!err)
941 pr_warn("overlayfs: upper fs needs to support d_type.\n");
942
943 /* Check if upper/work fs supports O_TMPFILE */
944 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
945 ufs->tmpfile = !IS_ERR(temp);
946 if (ufs->tmpfile)
947 dput(temp);
948 else
949 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
950
951 /*
952 * Check if upper/work fs supports trusted.overlay.* xattr
953 */
954 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
955 if (err) {
956 ufs->noxattr = true;
957 pr_warn("overlayfs: upper fs does not support xattr.\n");
958 } else {
959 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
960 }
961
962 /* Check if upper/work fs supports file handles */
963 if (ufs->config.index &&
964 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
965 ufs->config.index = false;
966 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
967 }
968
969 return 0;
970}
971
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +0100972static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ufs,
973 struct ovl_entry *oe,
Miklos Szeredi95e6d412017-11-09 10:23:29 +0100974 struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +0100975{
976 int err;
977
978 /* Verify lower root is upper root origin */
979 err = ovl_verify_origin(upperpath->dentry,
980 oe->lowerstack[0].layer->mnt,
981 oe->lowerstack[0].dentry,
982 false, true);
983 if (err) {
984 pr_err("overlayfs: failed to verify upper root origin\n");
985 goto out;
986 }
987
Miklos Szeredi95e6d412017-11-09 10:23:29 +0100988 ufs->indexdir = ovl_workdir_create(sb, ufs, ufs->workbasedir,
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +0100989 OVL_INDEXDIR_NAME, true);
990 if (ufs->indexdir) {
991 /* Verify upper root is index dir origin */
992 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
993 upperpath->dentry, true, true);
994 if (err)
995 pr_err("overlayfs: failed to verify index dir origin\n");
996
997 /* Cleanup bad/stale/orphan index entries */
998 if (!err)
999 err = ovl_indexdir_cleanup(ufs->indexdir,
1000 ufs->upper_mnt,
1001 oe->lowerstack,
1002 oe->numlower);
1003 }
1004 if (err || !ufs->indexdir)
1005 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1006
1007out:
1008 return err;
1009}
1010
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001011static int ovl_get_lowerstack(struct super_block *sb, struct ovl_fs *ufs,
1012 struct path **stackp, unsigned int *stacklenp)
1013{
1014 int err;
1015 char *lowertmp, *lower;
1016 struct path *stack;
1017 unsigned int stacklen, numlower, i;
1018 bool remote = false;
1019
1020 err = -ENOMEM;
1021 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1022 if (!lowertmp)
1023 goto out;
1024
1025 err = -EINVAL;
1026 stacklen = ovl_split_lowerdirs(lowertmp);
1027 if (stacklen > OVL_MAX_STACK) {
1028 pr_err("overlayfs: too many lower directories, limit is %d\n",
1029 OVL_MAX_STACK);
1030 goto out;
1031 } else if (!ufs->config.upperdir && stacklen == 1) {
1032 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1033 goto out;
1034 }
1035
1036 err = -ENOMEM;
1037 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1038 if (!stack)
1039 goto out;
1040
1041 err = -EINVAL;
1042 lower = lowertmp;
1043 for (numlower = 0; numlower < stacklen; numlower++) {
1044 err = ovl_lower_dir(lower, &stack[numlower], ufs,
1045 &sb->s_stack_depth, &remote);
1046 if (err)
1047 goto out_free_stack;
1048
1049 lower = strchr(lower, '\0') + 1;
1050 }
1051
1052 err = -EINVAL;
1053 sb->s_stack_depth++;
1054 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1055 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1056 goto out_free_stack;
1057 }
1058
1059 *stackp = stack;
1060 *stacklenp = numlower;
1061
1062 if (remote)
1063 sb->s_d_op = &ovl_reval_dentry_operations;
1064 else
1065 sb->s_d_op = &ovl_dentry_operations;
1066
1067 err = 0;
1068
1069out:
1070 kfree(lowertmp);
1071 return err;
1072
1073out_free_stack:
1074 for (i = 0; i < numlower; i++)
1075 path_put(&stack[i]);
1076 kfree(stack);
1077 goto out;
1078}
1079
Miklos Szeredic0d91fb2017-11-09 10:23:28 +01001080static int ovl_get_lower_layers(struct ovl_fs *ufs, struct path *stack,
1081 unsigned int numlower)
1082{
1083 int err;
1084 unsigned int i;
1085
1086 err = -ENOMEM;
1087 ufs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1088 GFP_KERNEL);
1089 if (ufs->lower_layers == NULL)
1090 goto out;
1091 for (i = 0; i < numlower; i++) {
1092 struct vfsmount *mnt;
1093 dev_t dev;
1094
1095 err = get_anon_bdev(&dev);
1096 if (err) {
1097 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1098 goto out;
1099 }
1100
1101 mnt = clone_private_mount(&stack[i]);
1102 err = PTR_ERR(mnt);
1103 if (IS_ERR(mnt)) {
1104 pr_err("overlayfs: failed to clone lowerpath\n");
1105 free_anon_bdev(dev);
1106 goto out;
1107 }
1108 /*
1109 * Make lower layers R/O. That way fchmod/fchown on lower file
1110 * will fail instead of modifying lower fs.
1111 */
1112 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1113
1114 ufs->lower_layers[ufs->numlower].mnt = mnt;
1115 ufs->lower_layers[ufs->numlower].pseudo_dev = dev;
1116 ufs->numlower++;
1117
1118 /* Check if all lower layers are on same sb */
1119 if (i == 0)
1120 ufs->same_sb = mnt->mnt_sb;
1121 else if (ufs->same_sb != mnt->mnt_sb)
1122 ufs->same_sb = NULL;
1123 }
1124 err = 0;
1125out:
1126 return err;
1127}
1128
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001129static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1130{
Kees Cook33006cd2017-03-29 14:02:19 -07001131 struct path upperpath = { };
1132 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001133 struct dentry *root_dentry;
1134 struct ovl_entry *oe;
1135 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001136 struct path *stack = NULL;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001137 unsigned int numlower = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +01001138 unsigned int i;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001139 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001140 int err;
1141
Erez Zadokf45827e82014-10-24 00:14:38 +02001142 err = -ENOMEM;
1143 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1144 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001145 goto out;
1146
Miklos Szeredi688ea0e2016-12-16 11:02:57 +01001147 ufs->config.redirect_dir = ovl_redirect_dir_def;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001148 ufs->config.index = ovl_index_def;
Erez Zadokf45827e82014-10-24 00:14:38 +02001149 err = ovl_parse_opt((char *) data, &ufs->config);
1150 if (err)
1151 goto out_free_config;
1152
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001153 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001154 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001155 if (!silent)
1156 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001157 goto out_free_config;
1158 }
1159
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001160 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001161 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001162 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001163 if (!ufs->config.workdir) {
1164 pr_err("overlayfs: missing 'workdir'\n");
1165 goto out_free_config;
1166 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001167
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001168 err = ovl_get_upperpath(ufs, &upperpath);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001169 if (err)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001170 goto out_unlock_upperdentry;
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001171
Miklos Szeredi87ad4472017-11-09 10:23:28 +01001172 err = ovl_get_workpath(ufs, &upperpath, &workpath);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001173 if (err)
Miklos Szeredi87ad4472017-11-09 10:23:28 +01001174 goto out_unlock_workdentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001175
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001176 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +01001177 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001178 err = ovl_get_lowerstack(sb, ufs, &stack, &numlower);
1179 if (err)
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001180 goto out_unlock_workdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001181
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001182 if (ufs->config.upperdir) {
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001183 err = ovl_get_upper(ufs, &upperpath);
1184 if (err)
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001185 goto out_put_lowerpath;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001186
1187 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001188
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001189 err = ovl_get_workdir(sb, ufs, &workpath);
1190 if (err)
1191 goto out_put_workdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001192 }
1193
Miklos Szeredic0d91fb2017-11-09 10:23:28 +01001194 err = ovl_get_lower_layers(ufs, stack, numlower);
1195 if (err)
1196 goto out_put_lower_layers;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001197
hujianyang71cbad72015-01-15 13:20:57 +08001198 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1199 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001200 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001201 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1202 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001203
Chandan Rajendrab9343632017-07-24 01:57:54 -05001204 err = -ENOMEM;
1205 oe = ovl_alloc_entry(numlower);
1206 if (!oe)
1207 goto out_put_lower_layers;
1208
1209 for (i = 0; i < numlower; i++) {
1210 oe->lowerstack[i].dentry = stack[i].dentry;
1211 oe->lowerstack[i].layer = &(ufs->lower_layers[i]);
1212 }
1213
Amir Goldstein02bcd152017-06-21 15:28:36 +03001214 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001215 err = ovl_get_indexdir(sb, ufs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001216 if (err)
1217 goto out_put_indexdir;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001218 }
1219
1220 /* Show index=off/on in /proc/mounts for any of the reasons above */
1221 if (!ufs->indexdir)
1222 ufs->config.index = false;
1223
Amir Goldstein8fc646b2017-07-11 15:58:35 +03001224 err = -ENOMEM;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001225 ufs->creator_cred = cred = prepare_creds();
1226 if (!cred)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001227 goto out_put_indexdir;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001228
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001229 /* Never override disk quota limits or use reserved space */
1230 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1231
Vivek Goyal655042c2016-10-14 03:03:36 +02001232 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1233 sb->s_op = &ovl_super_operations;
1234 sb->s_xattr = ovl_xattr_handlers;
1235 sb->s_fs_info = ufs;
1236 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1237
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001238 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001239 if (!root_dentry)
Chandan Rajendrab9343632017-07-24 01:57:54 -05001240 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001241
1242 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001243 for (i = 0; i < numlower; i++)
1244 mntput(stack[i].mnt);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001245 kfree(stack);
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001246 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001247
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001248 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001249 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001250 if (ovl_is_impuredir(upperpath.dentry))
1251 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001252 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001253
1254 root_dentry->d_fsdata = oe;
1255
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001256 /* Root is always merge -> can have whiteouts */
1257 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001258 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1259 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001260
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001261 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001262
1263 return 0;
1264
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001265out_put_cred:
1266 put_cred(ufs->creator_cred);
Amir Goldstein02bcd152017-06-21 15:28:36 +03001267out_put_indexdir:
1268 dput(ufs->indexdir);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001269 kfree(oe);
1270out_put_lower_layers:
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +02001271 for (i = 0; i < ufs->numlower; i++) {
1272 if (ufs->lower_layers[i].mnt)
1273 free_anon_bdev(ufs->lower_layers[i].pseudo_dev);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001274 mntput(ufs->lower_layers[i].mnt);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +02001275 }
Chandan Rajendrab9343632017-07-24 01:57:54 -05001276 kfree(ufs->lower_layers);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001277out_put_workdir:
1278 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001279 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001280out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001281 for (i = 0; i < numlower; i++)
1282 path_put(&stack[i]);
1283 kfree(stack);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001284out_unlock_workdentry:
Amir Goldstein85fdee12017-09-29 10:21:21 +03001285 if (ufs->workdir_locked)
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001286 ovl_inuse_unlock(ufs->workbasedir);
1287 dput(ufs->workbasedir);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001288 path_put(&workpath);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001289out_unlock_upperdentry:
Amir Goldstein85fdee12017-09-29 10:21:21 +03001290 if (ufs->upperdir_locked)
1291 ovl_inuse_unlock(upperpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001292 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001293out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001294 kfree(ufs->config.lowerdir);
1295 kfree(ufs->config.upperdir);
1296 kfree(ufs->config.workdir);
1297 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001298out:
1299 return err;
1300}
1301
1302static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1303 const char *dev_name, void *raw_data)
1304{
1305 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1306}
1307
1308static struct file_system_type ovl_fs_type = {
1309 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001310 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001311 .mount = ovl_mount,
1312 .kill_sb = kill_anon_super,
1313};
Miklos Szeredief94b182014-11-20 16:39:59 +01001314MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001315
Amir Goldstein13cf1992017-06-12 09:54:40 +03001316static void ovl_inode_init_once(void *foo)
1317{
1318 struct ovl_inode *oi = foo;
1319
1320 inode_init_once(&oi->vfs_inode);
1321}
1322
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001323static int __init ovl_init(void)
1324{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001325 int err;
1326
1327 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1328 sizeof(struct ovl_inode), 0,
1329 (SLAB_RECLAIM_ACCOUNT|
1330 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1331 ovl_inode_init_once);
1332 if (ovl_inode_cachep == NULL)
1333 return -ENOMEM;
1334
1335 err = register_filesystem(&ovl_fs_type);
1336 if (err)
1337 kmem_cache_destroy(ovl_inode_cachep);
1338
1339 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001340}
1341
1342static void __exit ovl_exit(void)
1343{
1344 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001345
1346 /*
1347 * Make sure all delayed rcu free inodes are flushed before we
1348 * destroy cache.
1349 */
1350 rcu_barrier();
1351 kmem_cache_destroy(ovl_inode_cachep);
1352
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001353}
1354
1355module_init(ovl_init);
1356module_exit(ovl_exit);