blob: 7d97d30cad397eed320ee01e5a4dd3b5c921828d [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
Miklos Szeredi438c84c2017-12-11 11:28:10 +010036static bool ovl_redirect_always_follow =
37 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
38module_param_named(redirect_always_follow, ovl_redirect_always_follow,
39 bool, 0644);
40MODULE_PARM_DESC(ovl_redirect_always_follow,
41 "Follow redirects even if redirect_dir feature is turned off");
42
Amir Goldstein02bcd152017-06-21 15:28:36 +030043static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
44module_param_named(index, ovl_index_def, bool, 0644);
45MODULE_PARM_DESC(ovl_index_def,
46 "Default to on or off for the inodes index feature");
47
Amir Goldsteinf168f102018-01-19 11:26:53 +020048static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
49module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
50MODULE_PARM_DESC(ovl_nfs_export_def,
51 "Default to on or off for the NFS export feature");
52
Miklos Szeredi4155c102017-11-10 09:39:15 +010053static void ovl_entry_stack_free(struct ovl_entry *oe)
54{
55 unsigned int i;
56
57 for (i = 0; i < oe->numlower; i++)
58 dput(oe->lowerstack[i].dentry);
59}
60
Miklos Szeredie9be9d52014-10-24 00:14:38 +020061static void ovl_dentry_release(struct dentry *dentry)
62{
63 struct ovl_entry *oe = dentry->d_fsdata;
64
65 if (oe) {
Miklos Szeredi4155c102017-11-10 09:39:15 +010066 ovl_entry_stack_free(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020067 kfree_rcu(oe, rcu);
68 }
69}
70
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030071static int ovl_check_append_only(struct inode *inode, int flag)
72{
73 /*
74 * This test was moot in vfs may_open() because overlay inode does
75 * not have the S_APPEND flag, so re-check on real upper inode
76 */
77 if (IS_APPEND(inode)) {
78 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
79 return -EPERM;
80 if (flag & O_TRUNC)
81 return -EPERM;
82 }
83
84 return 0;
85}
86
Miklos Szeredi2d902672016-06-30 08:53:27 +020087static struct dentry *ovl_d_real(struct dentry *dentry,
88 const struct inode *inode,
Miklos Szeredi495e6422017-09-04 21:42:22 +020089 unsigned int open_flags, unsigned int flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040090{
91 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030092 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040093
Miklos Szeredicd913042017-09-05 12:53:11 +020094 if (flags & D_REAL_UPPER)
95 return ovl_dentry_upper(dentry);
96
Miklos Szeredica4c8a32016-12-16 11:02:55 +010097 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040098 if (!inode || inode == d_inode(dentry))
99 return dentry;
100 goto bug;
101 }
102
Miklos Szeredi2d902672016-06-30 08:53:27 +0200103 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300104 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200105 if (err)
106 return ERR_PTR(err);
107 }
108
Miklos Szeredid101a122016-03-26 16:14:37 -0400109 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300110 if (real && (!inode || inode == d_inode(real))) {
111 if (!inode) {
112 err = ovl_check_append_only(d_inode(real), open_flags);
113 if (err)
114 return ERR_PTR(err);
115 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400116 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300117 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400118
119 real = ovl_dentry_lower(dentry);
120 if (!real)
121 goto bug;
122
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100123 /* Handle recursion */
Miklos Szeredi495e6422017-09-04 21:42:22 +0200124 real = d_real(real, inode, open_flags, 0);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100125
Miklos Szeredid101a122016-03-26 16:14:37 -0400126 if (!inode || inode == d_inode(real))
127 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400128bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200129 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400130 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
131 return dentry;
132}
133
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200134static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
135{
136 struct ovl_entry *oe = dentry->d_fsdata;
137 unsigned int i;
138 int ret = 1;
139
140 for (i = 0; i < oe->numlower; i++) {
141 struct dentry *d = oe->lowerstack[i].dentry;
142
143 if (d->d_flags & DCACHE_OP_REVALIDATE) {
144 ret = d->d_op->d_revalidate(d, flags);
145 if (ret < 0)
146 return ret;
147 if (!ret) {
148 if (!(flags & LOOKUP_RCU))
149 d_invalidate(d);
150 return -ESTALE;
151 }
152 }
153 }
154 return 1;
155}
156
157static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
158{
159 struct ovl_entry *oe = dentry->d_fsdata;
160 unsigned int i;
161 int ret = 1;
162
163 for (i = 0; i < oe->numlower; i++) {
164 struct dentry *d = oe->lowerstack[i].dentry;
165
166 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
167 ret = d->d_op->d_weak_revalidate(d, flags);
168 if (ret <= 0)
169 break;
170 }
171 }
172 return ret;
173}
174
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200175static const struct dentry_operations ovl_dentry_operations = {
176 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400177 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200178};
179
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200180static const struct dentry_operations ovl_reval_dentry_operations = {
181 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400182 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200183 .d_revalidate = ovl_dentry_revalidate,
184 .d_weak_revalidate = ovl_dentry_weak_revalidate,
185};
186
Amir Goldstein13cf1992017-06-12 09:54:40 +0300187static struct kmem_cache *ovl_inode_cachep;
188
189static struct inode *ovl_alloc_inode(struct super_block *sb)
190{
191 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
192
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900193 if (!oi)
194 return NULL;
195
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200196 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200197 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200198 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200199 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200200 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200201 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300202 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200203
Amir Goldstein13cf1992017-06-12 09:54:40 +0300204 return &oi->vfs_inode;
205}
206
207static void ovl_i_callback(struct rcu_head *head)
208{
209 struct inode *inode = container_of(head, struct inode, i_rcu);
210
211 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
212}
213
214static void ovl_destroy_inode(struct inode *inode)
215{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200216 struct ovl_inode *oi = OVL_I(inode);
217
218 dput(oi->__upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200219 iput(oi->lower);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200220 kfree(oi->redirect);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200221 ovl_dir_cache_free(inode);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300222 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200223
Amir Goldstein13cf1992017-06-12 09:54:40 +0300224 call_rcu(&inode->i_rcu, ovl_i_callback);
225}
226
Miklos Szerediad204482017-11-10 09:39:16 +0100227static void ovl_free_fs(struct ovl_fs *ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200228{
Miklos Szeredidd662662014-12-13 00:59:43 +0100229 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200230
Miklos Szerediad204482017-11-10 09:39:16 +0100231 dput(ofs->indexdir);
232 dput(ofs->workdir);
233 if (ofs->workdir_locked)
234 ovl_inuse_unlock(ofs->workbasedir);
235 dput(ofs->workbasedir);
236 if (ofs->upperdir_locked)
237 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
238 mntput(ofs->upper_mnt);
Amir Goldstein51486262018-03-28 20:22:41 +0300239 for (i = 0; i < ofs->numlower; i++)
Miklos Szerediad204482017-11-10 09:39:16 +0100240 mntput(ofs->lower_layers[i].mnt);
Amir Goldstein51486262018-03-28 20:22:41 +0300241 for (i = 0; i < ofs->numlowerfs; i++)
242 free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
Miklos Szerediad204482017-11-10 09:39:16 +0100243 kfree(ofs->lower_layers);
Amir Goldstein51486262018-03-28 20:22:41 +0300244 kfree(ofs->lower_fs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200245
Miklos Szerediad204482017-11-10 09:39:16 +0100246 kfree(ofs->config.lowerdir);
247 kfree(ofs->config.upperdir);
248 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100249 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100250 if (ofs->creator_cred)
251 put_cred(ofs->creator_cred);
252 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200253}
254
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100255static void ovl_put_super(struct super_block *sb)
256{
257 struct ovl_fs *ofs = sb->s_fs_info;
258
259 ovl_free_fs(ofs);
260}
261
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800262/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200263static int ovl_sync_fs(struct super_block *sb, int wait)
264{
Miklos Szerediad204482017-11-10 09:39:16 +0100265 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200266 struct super_block *upper_sb;
267 int ret;
268
Miklos Szerediad204482017-11-10 09:39:16 +0100269 if (!ofs->upper_mnt)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200270 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800271
272 /*
273 * If this is a sync(2) call or an emergency sync, all the super blocks
274 * will be iterated, including upper_sb, so no need to do anything.
275 *
276 * If this is a syncfs(2) call, then we do need to call
277 * sync_filesystem() on upper_sb, but enough if we do it when being
278 * called with wait == 1.
279 */
280 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200281 return 0;
282
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800283 upper_sb = ofs->upper_mnt->mnt_sb;
284
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200285 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800286 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200287 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800288
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200289 return ret;
290}
291
Andy Whitcroftcc259632014-10-24 00:14:38 +0200292/**
293 * ovl_statfs
294 * @sb: The overlayfs super block
295 * @buf: The struct kstatfs to fill in with stats
296 *
297 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100298 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200299 */
300static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
301{
302 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
303 struct dentry *root_dentry = dentry->d_sb->s_root;
304 struct path path;
305 int err;
306
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100307 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200308
309 err = vfs_statfs(&path, buf);
310 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100311 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200312 buf->f_type = OVERLAYFS_SUPER_MAGIC;
313 }
314
315 return err;
316}
317
Amir Goldstein02bcd152017-06-21 15:28:36 +0300318/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100319static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300320{
Miklos Szerediad204482017-11-10 09:39:16 +0100321 return (!ofs->upper_mnt || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300322}
323
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100324static const char *ovl_redirect_mode_def(void)
325{
326 return ovl_redirect_dir_def ? "on" : "off";
327}
328
Erez Zadokf45827e82014-10-24 00:14:38 +0200329/**
330 * ovl_show_options
331 *
332 * Prints the mount options for a given superblock.
333 * Returns zero; does not fail.
334 */
335static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
336{
337 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100338 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200339
Miklos Szerediad204482017-11-10 09:39:16 +0100340 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
341 if (ofs->config.upperdir) {
342 seq_show_option(m, "upperdir", ofs->config.upperdir);
343 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100344 }
Miklos Szerediad204482017-11-10 09:39:16 +0100345 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200346 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100347 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
348 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100349 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100350 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Amir Goldsteinf168f102018-01-19 11:26:53 +0200351 if (ofs->config.nfs_export != ovl_nfs_export_def)
352 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
353 "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200354 return 0;
355}
356
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900357static int ovl_remount(struct super_block *sb, int *flags, char *data)
358{
Miklos Szerediad204482017-11-10 09:39:16 +0100359 struct ovl_fs *ofs = sb->s_fs_info;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900360
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800361 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900362 return -EROFS;
363
364 return 0;
365}
366
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200367static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300368 .alloc_inode = ovl_alloc_inode,
369 .destroy_inode = ovl_destroy_inode,
370 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200372 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200373 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200374 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900375 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200376};
377
378enum {
379 OPT_LOWERDIR,
380 OPT_UPPERDIR,
381 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200382 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100383 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300384 OPT_INDEX_ON,
385 OPT_INDEX_OFF,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200386 OPT_NFS_EXPORT_ON,
387 OPT_NFS_EXPORT_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200388 OPT_ERR,
389};
390
391static const match_table_t ovl_tokens = {
392 {OPT_LOWERDIR, "lowerdir=%s"},
393 {OPT_UPPERDIR, "upperdir=%s"},
394 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200395 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100396 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300397 {OPT_INDEX_ON, "index=on"},
398 {OPT_INDEX_OFF, "index=off"},
Amir Goldsteinf168f102018-01-19 11:26:53 +0200399 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
400 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200401 {OPT_ERR, NULL}
402};
403
Miklos Szeredi91c77942014-11-20 16:40:00 +0100404static char *ovl_next_opt(char **s)
405{
406 char *sbegin = *s;
407 char *p;
408
409 if (sbegin == NULL)
410 return NULL;
411
412 for (p = sbegin; *p; p++) {
413 if (*p == '\\') {
414 p++;
415 if (!*p)
416 break;
417 } else if (*p == ',') {
418 *p = '\0';
419 *s = p + 1;
420 return sbegin;
421 }
422 }
423 *s = NULL;
424 return sbegin;
425}
426
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100427static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
428{
429 if (strcmp(mode, "on") == 0) {
430 config->redirect_dir = true;
431 /*
432 * Does not make sense to have redirect creation without
433 * redirect following.
434 */
435 config->redirect_follow = true;
436 } else if (strcmp(mode, "follow") == 0) {
437 config->redirect_follow = true;
438 } else if (strcmp(mode, "off") == 0) {
439 if (ovl_redirect_always_follow)
440 config->redirect_follow = true;
441 } else if (strcmp(mode, "nofollow") != 0) {
442 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
443 mode);
444 return -EINVAL;
445 }
446
447 return 0;
448}
449
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200450static int ovl_parse_opt(char *opt, struct ovl_config *config)
451{
452 char *p;
453
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100454 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
455 if (!config->redirect_mode)
456 return -ENOMEM;
457
Miklos Szeredi91c77942014-11-20 16:40:00 +0100458 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200459 int token;
460 substring_t args[MAX_OPT_ARGS];
461
462 if (!*p)
463 continue;
464
465 token = match_token(p, ovl_tokens, args);
466 switch (token) {
467 case OPT_UPPERDIR:
468 kfree(config->upperdir);
469 config->upperdir = match_strdup(&args[0]);
470 if (!config->upperdir)
471 return -ENOMEM;
472 break;
473
474 case OPT_LOWERDIR:
475 kfree(config->lowerdir);
476 config->lowerdir = match_strdup(&args[0]);
477 if (!config->lowerdir)
478 return -ENOMEM;
479 break;
480
481 case OPT_WORKDIR:
482 kfree(config->workdir);
483 config->workdir = match_strdup(&args[0]);
484 if (!config->workdir)
485 return -ENOMEM;
486 break;
487
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200488 case OPT_DEFAULT_PERMISSIONS:
489 config->default_permissions = true;
490 break;
491
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100492 case OPT_REDIRECT_DIR:
493 kfree(config->redirect_mode);
494 config->redirect_mode = match_strdup(&args[0]);
495 if (!config->redirect_mode)
496 return -ENOMEM;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100497 break;
498
Amir Goldstein02bcd152017-06-21 15:28:36 +0300499 case OPT_INDEX_ON:
500 config->index = true;
501 break;
502
503 case OPT_INDEX_OFF:
504 config->index = false;
505 break;
506
Amir Goldsteinf168f102018-01-19 11:26:53 +0200507 case OPT_NFS_EXPORT_ON:
508 config->nfs_export = true;
509 break;
510
511 case OPT_NFS_EXPORT_OFF:
512 config->nfs_export = false;
513 break;
514
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200515 default:
hujianyangbead55e2015-01-15 13:17:36 +0800516 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200517 return -EINVAL;
518 }
519 }
hujianyang71cbad72015-01-15 13:20:57 +0800520
521 /* Workdir is useless in non-upper mount */
522 if (!config->upperdir && config->workdir) {
523 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
524 config->workdir);
525 kfree(config->workdir);
526 config->workdir = NULL;
527 }
528
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100529 return ovl_parse_redirect_mode(config, config->redirect_mode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200530}
531
532#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300533#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200534
Miklos Szerediad204482017-11-10 09:39:16 +0100535static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300536 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200537{
Miklos Szerediad204482017-11-10 09:39:16 +0100538 struct inode *dir = ofs->workbasedir->d_inode;
539 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200540 struct dentry *work;
541 int err;
542 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300543 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544
Al Viro59551022016-01-22 15:40:57 -0500545 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300546 locked = true;
547
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200548retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100549 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200550
551 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200552 struct iattr attr = {
553 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000554 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200555 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200556
557 if (work->d_inode) {
558 err = -EEXIST;
559 if (retried)
560 goto out_dput;
561
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300562 if (persist)
563 goto out_unlock;
564
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200565 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200566 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200567 dput(work);
568 goto retry;
569 }
570
Al Viro32a3d842016-12-04 17:33:17 +0000571 err = ovl_create_real(dir, work,
572 &(struct cattr){.mode = S_IFDIR | 0},
573 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200574 if (err)
575 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200576
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200577 /*
578 * Try to remove POSIX ACL xattrs from workdir. We are good if:
579 *
580 * a) success (there was a POSIX ACL xattr and was removed)
581 * b) -ENODATA (there was no POSIX ACL xattr)
582 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
583 *
584 * There are various other error values that could effectively
585 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
586 * if the xattr name is too long), but the set of filesystems
587 * allowed as upper are limited to "normal" ones, where checking
588 * for the above two errors is sufficient.
589 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200590 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200591 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200592 goto out_dput;
593
594 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200595 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200596 goto out_dput;
597
598 /* Clear any inherited mode bits */
599 inode_lock(work->d_inode);
600 err = notify_change(work, &attr, NULL);
601 inode_unlock(work->d_inode);
602 if (err)
603 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300604 } else {
605 err = PTR_ERR(work);
606 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200607 }
608out_unlock:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300609 if (locked)
610 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200611
612 return work;
613
614out_dput:
615 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300616out_err:
617 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100618 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300619 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200620 goto out_unlock;
621}
622
Miklos Szeredi91c77942014-11-20 16:40:00 +0100623static void ovl_unescape(char *s)
624{
625 char *d = s;
626
627 for (;; s++, d++) {
628 if (*s == '\\')
629 s++;
630 *d = *s;
631 if (!*s)
632 break;
633 }
634}
635
Miklos Szerediab508822014-12-13 00:59:49 +0100636static int ovl_mount_dir_noesc(const char *name, struct path *path)
637{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100638 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100639
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100640 if (!*name) {
641 pr_err("overlayfs: empty lowerdir\n");
642 goto out;
643 }
Miklos Szerediab508822014-12-13 00:59:49 +0100644 err = kern_path(name, LOOKUP_FOLLOW, path);
645 if (err) {
646 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
647 goto out;
648 }
649 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200650 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100651 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
652 goto out_put;
653 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100654 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100655 pr_err("overlayfs: '%s' not a directory\n", name);
656 goto out_put;
657 }
658 return 0;
659
660out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100661 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100662out:
663 return err;
664}
665
666static int ovl_mount_dir(const char *name, struct path *path)
667{
668 int err = -ENOMEM;
669 char *tmp = kstrdup(name, GFP_KERNEL);
670
671 if (tmp) {
672 ovl_unescape(tmp);
673 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200674
675 if (!err)
676 if (ovl_dentry_remote(path->dentry)) {
677 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
678 tmp);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100679 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200680 err = -EINVAL;
681 }
Miklos Szerediab508822014-12-13 00:59:49 +0100682 kfree(tmp);
683 }
684 return err;
685}
686
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100687static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
688 const char *name)
689{
690 struct kstatfs statfs;
691 int err = vfs_statfs(path, &statfs);
692
693 if (err)
694 pr_err("overlayfs: statfs failed on '%s'\n", name);
695 else
696 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
697
698 return err;
699}
700
701static int ovl_lower_dir(const char *name, struct path *path,
702 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100703{
704 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100705
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100706 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100707 if (err)
708 goto out;
709
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100710 err = ovl_check_namelen(path, ofs, name);
711 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100712 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100713
Miklos Szerediab508822014-12-13 00:59:49 +0100714 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
715
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200716 if (ovl_dentry_remote(path->dentry))
717 *remote = true;
718
Amir Goldstein02bcd152017-06-21 15:28:36 +0300719 /*
Amir Goldsteinf168f102018-01-19 11:26:53 +0200720 * The inodes index feature and NFS export need to encode and decode
721 * file handles, so they require that all layers support them.
Amir Goldstein02bcd152017-06-21 15:28:36 +0300722 */
Amir Goldsteinf168f102018-01-19 11:26:53 +0200723 if ((ofs->config.nfs_export ||
724 (ofs->config.index && ofs->config.upperdir)) &&
Amir Goldstein972d0092017-09-19 12:14:18 +0300725 !ovl_can_decode_fh(path->dentry->d_sb)) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300726 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200727 ofs->config.nfs_export = false;
728 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
729 name);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300730 }
731
Miklos Szerediab508822014-12-13 00:59:49 +0100732 return 0;
733
734out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100735 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100736out:
737 return err;
738}
739
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200740/* Workdir should not be subdir of upperdir and vice versa */
741static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
742{
743 bool ok = false;
744
745 if (workdir != upperdir) {
746 ok = (lock_rename(workdir, upperdir) == NULL);
747 unlock_rename(workdir, upperdir);
748 }
749 return ok;
750}
751
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100752static unsigned int ovl_split_lowerdirs(char *str)
753{
754 unsigned int ctr = 1;
755 char *s, *d;
756
757 for (s = d = str;; s++, d++) {
758 if (*s == '\\') {
759 s++;
760 } else if (*s == ':') {
761 *d = '\0';
762 ctr++;
763 continue;
764 }
765 *d = *s;
766 if (!*s)
767 break;
768 }
769 return ctr;
770}
771
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200772static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200773ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
774 struct dentry *dentry, struct inode *inode,
775 const char *name, void *buffer, size_t size)
776{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200777 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200778}
779
780static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200781ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
782 struct dentry *dentry, struct inode *inode,
783 const char *name, const void *value,
784 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200785{
786 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200787 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200788 struct posix_acl *acl = NULL;
789 int err;
790
791 /* Check that everything is OK before copy-up */
792 if (value) {
793 acl = posix_acl_from_xattr(&init_user_ns, value, size);
794 if (IS_ERR(acl))
795 return PTR_ERR(acl);
796 }
797 err = -EOPNOTSUPP;
798 if (!IS_POSIXACL(d_inode(workdir)))
799 goto out_acl_release;
800 if (!realinode->i_op->set_acl)
801 goto out_acl_release;
802 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
803 err = acl ? -EACCES : 0;
804 goto out_acl_release;
805 }
806 err = -EPERM;
807 if (!inode_owner_or_capable(inode))
808 goto out_acl_release;
809
810 posix_acl_release(acl);
811
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100812 /*
813 * Check if sgid bit needs to be cleared (actual setacl operation will
814 * be done with mounter's capabilities and so that won't do it for us).
815 */
816 if (unlikely(inode->i_mode & S_ISGID) &&
817 handler->flags == ACL_TYPE_ACCESS &&
818 !in_group_p(inode->i_gid) &&
819 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
820 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
821
822 err = ovl_setattr(dentry, &iattr);
823 if (err)
824 return err;
825 }
826
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200827 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200828 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200829 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200830
831 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200832
833out_acl_release:
834 posix_acl_release(acl);
835 return err;
836}
837
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200838static int ovl_own_xattr_get(const struct xattr_handler *handler,
839 struct dentry *dentry, struct inode *inode,
840 const char *name, void *buffer, size_t size)
841{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200842 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200843}
844
Miklos Szeredid837a492016-07-29 12:05:24 +0200845static int ovl_own_xattr_set(const struct xattr_handler *handler,
846 struct dentry *dentry, struct inode *inode,
847 const char *name, const void *value,
848 size_t size, int flags)
849{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200850 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200851}
852
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200853static int ovl_other_xattr_get(const struct xattr_handler *handler,
854 struct dentry *dentry, struct inode *inode,
855 const char *name, void *buffer, size_t size)
856{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200857 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200858}
859
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200860static int ovl_other_xattr_set(const struct xattr_handler *handler,
861 struct dentry *dentry, struct inode *inode,
862 const char *name, const void *value,
863 size_t size, int flags)
864{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200865 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200866}
867
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200868static const struct xattr_handler __maybe_unused
869ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200870 .name = XATTR_NAME_POSIX_ACL_ACCESS,
871 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200872 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200873 .set = ovl_posix_acl_xattr_set,
874};
875
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200876static const struct xattr_handler __maybe_unused
877ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200878 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
879 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200880 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200881 .set = ovl_posix_acl_xattr_set,
882};
883
884static const struct xattr_handler ovl_own_xattr_handler = {
885 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200886 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200887 .set = ovl_own_xattr_set,
888};
889
890static const struct xattr_handler ovl_other_xattr_handler = {
891 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200892 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200893 .set = ovl_other_xattr_set,
894};
895
896static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200897#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200898 &ovl_posix_acl_access_xattr_handler,
899 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200900#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200901 &ovl_own_xattr_handler,
902 &ovl_other_xattr_handler,
903 NULL
904};
905
Miklos Szerediad204482017-11-10 09:39:16 +0100906static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100907{
Miklos Szeredi50649752017-11-10 09:39:15 +0100908 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100909 int err;
910
Miklos Szerediad204482017-11-10 09:39:16 +0100911 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100912 if (err)
913 goto out;
914
915 /* Upper fs should not be r/o */
916 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
917 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
918 err = -EINVAL;
919 goto out;
920 }
921
Miklos Szerediad204482017-11-10 09:39:16 +0100922 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100923 if (err)
924 goto out;
925
926 err = -EBUSY;
927 if (ovl_inuse_trylock(upperpath->dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +0100928 ofs->upperdir_locked = true;
929 } else if (ofs->config.index) {
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100930 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
931 goto out;
932 } else {
933 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
934 }
Miklos Szeredi50649752017-11-10 09:39:15 +0100935
936 upper_mnt = clone_private_mount(upperpath);
937 err = PTR_ERR(upper_mnt);
938 if (IS_ERR(upper_mnt)) {
939 pr_err("overlayfs: failed to clone upperpath\n");
940 goto out;
941 }
942
943 /* Don't inherit atime flags */
944 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szerediad204482017-11-10 09:39:16 +0100945 ofs->upper_mnt = upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100946 err = 0;
947out:
948 return err;
949}
950
Miklos Szerediad204482017-11-10 09:39:16 +0100951static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100952{
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200953 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100954 struct dentry *temp;
955 int err;
956
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200957 err = mnt_want_write(mnt);
958 if (err)
959 return err;
960
Miklos Szerediad204482017-11-10 09:39:16 +0100961 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
962 if (!ofs->workdir)
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200963 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100964
965 /*
966 * Upper should support d_type, else whiteouts are visible. Given
967 * workdir and upper are on same fs, we can do iterate_dir() on
968 * workdir. This check requires successful creation of workdir in
969 * previous step.
970 */
971 err = ovl_check_d_type_supported(workpath);
972 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200973 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100974
975 /*
976 * We allowed this configuration and don't want to break users over
977 * kernel upgrade. So warn instead of erroring out.
978 */
979 if (!err)
980 pr_warn("overlayfs: upper fs needs to support d_type.\n");
981
982 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +0100983 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
984 ofs->tmpfile = !IS_ERR(temp);
985 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100986 dput(temp);
987 else
988 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
989
990 /*
991 * Check if upper/work fs supports trusted.overlay.* xattr
992 */
Miklos Szerediad204482017-11-10 09:39:16 +0100993 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100994 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +0100995 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +0300996 ofs->config.index = false;
997 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off.\n");
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200998 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100999 } else {
Miklos Szerediad204482017-11-10 09:39:16 +01001000 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001001 }
1002
1003 /* Check if upper/work fs supports file handles */
Miklos Szerediad204482017-11-10 09:39:16 +01001004 if (ofs->config.index &&
1005 !ovl_can_decode_fh(ofs->workdir->d_sb)) {
1006 ofs->config.index = false;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001007 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1008 }
1009
Amir Goldsteinf168f102018-01-19 11:26:53 +02001010 /* NFS export of r/w mount depends on index */
1011 if (ofs->config.nfs_export && !ofs->config.index) {
1012 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1013 ofs->config.nfs_export = false;
1014 }
1015
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001016out:
1017 mnt_drop_write(mnt);
1018 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001019}
1020
Miklos Szerediad204482017-11-10 09:39:16 +01001021static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001022{
1023 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001024 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001025
Miklos Szerediad204482017-11-10 09:39:16 +01001026 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001027 if (err)
1028 goto out;
1029
1030 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001031 if (upperpath->mnt != workpath.mnt) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001032 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1033 goto out;
1034 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001035 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001036 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1037 goto out;
1038 }
1039
1040 err = -EBUSY;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001041 if (ovl_inuse_trylock(workpath.dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001042 ofs->workdir_locked = true;
1043 } else if (ofs->config.index) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001044 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1045 goto out;
1046 } else {
1047 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1048 }
1049
Miklos Szerediad204482017-11-10 09:39:16 +01001050 ofs->workbasedir = dget(workpath.dentry);
1051 err = ovl_make_workdir(ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001052 if (err)
1053 goto out;
1054
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001055 err = 0;
1056out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001057 path_put(&workpath);
1058
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001059 return err;
1060}
1061
Miklos Szerediad204482017-11-10 09:39:16 +01001062static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001063 struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001064{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001065 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001066 int err;
1067
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001068 err = mnt_want_write(mnt);
1069 if (err)
1070 return err;
1071
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001072 /* Verify lower root is upper root origin */
Amir Goldsteind9768072017-09-24 13:00:19 +03001073 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
Amir Goldstein05122442018-01-11 08:25:32 +02001074 true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001075 if (err) {
1076 pr_err("overlayfs: failed to verify upper root origin\n");
1077 goto out;
1078 }
1079
Miklos Szerediad204482017-11-10 09:39:16 +01001080 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1081 if (ofs->indexdir) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001082 /*
1083 * Verify upper root is exclusively associated with index dir.
1084 * Older kernels stored upper fh in "trusted.overlay.origin"
1085 * xattr. If that xattr exists, verify that it is a match to
1086 * upper dir file handle. In any case, verify or set xattr
1087 * "trusted.overlay.upper" to indicate that index may have
1088 * directory entries.
1089 */
1090 if (ovl_check_origin_xattr(ofs->indexdir)) {
1091 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1092 upperpath->dentry, true, false);
1093 if (err)
1094 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1095 }
1096 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001097 if (err)
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001098 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001099
1100 /* Cleanup bad/stale/orphan index entries */
1101 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001102 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001103 }
Miklos Szerediad204482017-11-10 09:39:16 +01001104 if (err || !ofs->indexdir)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001105 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1106
1107out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001108 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001109 return err;
1110}
1111
Amir Goldstein51486262018-03-28 20:22:41 +03001112/* Get a unique fsid for the layer */
1113static int ovl_get_fsid(struct ovl_fs *ofs, struct super_block *sb)
1114{
1115 unsigned int i;
1116 dev_t dev;
1117 int err;
1118
1119 /* fsid 0 is reserved for upper fs even with non upper overlay */
1120 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1121 return 0;
1122
1123 for (i = 0; i < ofs->numlowerfs; i++) {
1124 if (ofs->lower_fs[i].sb == sb)
1125 return i + 1;
1126 }
1127
1128 err = get_anon_bdev(&dev);
1129 if (err) {
1130 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1131 return err;
1132 }
1133
1134 ofs->lower_fs[ofs->numlowerfs].sb = sb;
1135 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1136 ofs->numlowerfs++;
1137
1138 return ofs->numlowerfs;
1139}
1140
Miklos Szerediad204482017-11-10 09:39:16 +01001141static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack,
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001142 unsigned int numlower)
1143{
1144 int err;
1145 unsigned int i;
1146
1147 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001148 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001149 GFP_KERNEL);
Miklos Szerediad204482017-11-10 09:39:16 +01001150 if (ofs->lower_layers == NULL)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001151 goto out;
Amir Goldstein51486262018-03-28 20:22:41 +03001152
1153 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1154 GFP_KERNEL);
1155 if (ofs->lower_fs == NULL)
1156 goto out;
1157
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001158 for (i = 0; i < numlower; i++) {
1159 struct vfsmount *mnt;
Amir Goldstein51486262018-03-28 20:22:41 +03001160 int fsid;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001161
Amir Goldstein51486262018-03-28 20:22:41 +03001162 err = fsid = ovl_get_fsid(ofs, stack[i].mnt->mnt_sb);
1163 if (err < 0)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001164 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001165
1166 mnt = clone_private_mount(&stack[i]);
1167 err = PTR_ERR(mnt);
1168 if (IS_ERR(mnt)) {
1169 pr_err("overlayfs: failed to clone lowerpath\n");
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001170 goto out;
1171 }
Amir Goldstein51486262018-03-28 20:22:41 +03001172
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001173 /*
1174 * Make lower layers R/O. That way fchmod/fchown on lower file
1175 * will fail instead of modifying lower fs.
1176 */
1177 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1178
Miklos Szerediad204482017-11-10 09:39:16 +01001179 ofs->lower_layers[ofs->numlower].mnt = mnt;
Amir Goldsteind583ed72017-11-08 19:23:36 +02001180 ofs->lower_layers[ofs->numlower].idx = i + 1;
Amir Goldstein51486262018-03-28 20:22:41 +03001181 ofs->lower_layers[ofs->numlower].fsid = fsid;
1182 if (fsid) {
1183 ofs->lower_layers[ofs->numlower].fs =
1184 &ofs->lower_fs[fsid - 1];
1185 }
Miklos Szerediad204482017-11-10 09:39:16 +01001186 ofs->numlower++;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001187 }
1188 err = 0;
1189out:
1190 return err;
1191}
1192
Miklos Szeredi4155c102017-11-10 09:39:15 +01001193static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szerediad204482017-11-10 09:39:16 +01001194 struct ovl_fs *ofs)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001195{
1196 int err;
1197 char *lowertmp, *lower;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001198 struct path *stack = NULL;
1199 unsigned int stacklen, numlower = 0, i;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001200 bool remote = false;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001201 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001202
1203 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001204 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001205 if (!lowertmp)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001206 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001207
1208 err = -EINVAL;
1209 stacklen = ovl_split_lowerdirs(lowertmp);
1210 if (stacklen > OVL_MAX_STACK) {
1211 pr_err("overlayfs: too many lower directories, limit is %d\n",
1212 OVL_MAX_STACK);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001213 goto out_err;
Miklos Szerediad204482017-11-10 09:39:16 +01001214 } else if (!ofs->config.upperdir && stacklen == 1) {
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001215 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001216 goto out_err;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001217 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1218 ofs->config.redirect_follow) {
1219 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1220 ofs->config.nfs_export = false;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001221 }
1222
1223 err = -ENOMEM;
1224 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1225 if (!stack)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001226 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001227
1228 err = -EINVAL;
1229 lower = lowertmp;
1230 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szerediad204482017-11-10 09:39:16 +01001231 err = ovl_lower_dir(lower, &stack[numlower], ofs,
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001232 &sb->s_stack_depth, &remote);
1233 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001234 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001235
1236 lower = strchr(lower, '\0') + 1;
1237 }
1238
1239 err = -EINVAL;
1240 sb->s_stack_depth++;
1241 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1242 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001243 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001244 }
1245
Miklos Szerediad204482017-11-10 09:39:16 +01001246 err = ovl_get_lower_layers(ofs, stack, numlower);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001247 if (err)
1248 goto out_err;
1249
1250 err = -ENOMEM;
1251 oe = ovl_alloc_entry(numlower);
1252 if (!oe)
1253 goto out_err;
1254
1255 for (i = 0; i < numlower; i++) {
1256 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Miklos Szerediad204482017-11-10 09:39:16 +01001257 oe->lowerstack[i].layer = &ofs->lower_layers[i];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001258 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001259
1260 if (remote)
1261 sb->s_d_op = &ovl_reval_dentry_operations;
1262 else
1263 sb->s_d_op = &ovl_dentry_operations;
1264
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001265out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001266 for (i = 0; i < numlower; i++)
1267 path_put(&stack[i]);
1268 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001269 kfree(lowertmp);
1270
1271 return oe;
1272
1273out_err:
1274 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001275 goto out;
1276}
1277
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001278static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1279{
Kees Cook33006cd2017-03-29 14:02:19 -07001280 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001281 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001282 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001283 struct ovl_fs *ofs;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001284 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001285 int err;
1286
Erez Zadokf45827e82014-10-24 00:14:38 +02001287 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001288 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1289 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001290 goto out;
1291
Miklos Szerediad204482017-11-10 09:39:16 +01001292 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001293 if (!cred)
1294 goto out_err;
1295
Miklos Szerediad204482017-11-10 09:39:16 +01001296 ofs->config.index = ovl_index_def;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001297 ofs->config.nfs_export = ovl_nfs_export_def;
Miklos Szerediad204482017-11-10 09:39:16 +01001298 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001299 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001300 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001301
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001302 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001303 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001304 if (!silent)
1305 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001306 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001307 }
1308
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001309 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001310 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szerediad204482017-11-10 09:39:16 +01001311 if (ofs->config.upperdir) {
1312 if (!ofs->config.workdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001313 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001314 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001315 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001316
Miklos Szerediad204482017-11-10 09:39:16 +01001317 err = ovl_get_upper(ofs, &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001318 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001319 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001320
Miklos Szerediad204482017-11-10 09:39:16 +01001321 err = ovl_get_workdir(ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001322 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001323 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001324
Miklos Szerediad204482017-11-10 09:39:16 +01001325 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001326 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001327
Miklos Szerediad204482017-11-10 09:39:16 +01001328 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1329 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001330
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001331 }
Miklos Szerediad204482017-11-10 09:39:16 +01001332 oe = ovl_get_lowerstack(sb, ofs);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001333 err = PTR_ERR(oe);
1334 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001335 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001336
hujianyang71cbad72015-01-15 13:20:57 +08001337 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szerediad204482017-11-10 09:39:16 +01001338 if (!ofs->upper_mnt)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001339 sb->s_flags |= SB_RDONLY;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001340
Miklos Szerediad204482017-11-10 09:39:16 +01001341 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1342 err = ovl_get_indexdir(ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001343 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001344 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001345
Amir Goldstein972d0092017-09-19 12:14:18 +03001346 /* Force r/o mount with no index dir */
1347 if (!ofs->indexdir) {
1348 dput(ofs->workdir);
1349 ofs->workdir = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001350 sb->s_flags |= SB_RDONLY;
Amir Goldstein972d0092017-09-19 12:14:18 +03001351 }
1352
Amir Goldstein02bcd152017-06-21 15:28:36 +03001353 }
1354
Amir Goldstein972d0092017-09-19 12:14:18 +03001355 /* Show index=off in /proc/mounts for forced r/o mount */
Amir Goldsteinf168f102018-01-19 11:26:53 +02001356 if (!ofs->indexdir) {
Miklos Szerediad204482017-11-10 09:39:16 +01001357 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001358 if (ofs->upper_mnt && ofs->config.nfs_export) {
1359 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1360 ofs->config.nfs_export = false;
1361 }
1362 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001363
Amir Goldstein8383f172017-10-02 11:31:42 +03001364 if (ofs->config.nfs_export)
1365 sb->s_export_op = &ovl_export_operations;
1366
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001367 /* Never override disk quota limits or use reserved space */
1368 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1369
Vivek Goyal655042c2016-10-14 03:03:36 +02001370 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1371 sb->s_op = &ovl_super_operations;
1372 sb->s_xattr = ovl_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01001373 sb->s_fs_info = ofs;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001374 sb->s_flags |= SB_POSIXACL | SB_NOREMOTELOCK;
Vivek Goyal655042c2016-10-14 03:03:36 +02001375
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001376 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001377 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001378 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001379 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001380
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001381 root_dentry->d_fsdata = oe;
1382
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001383 mntput(upperpath.mnt);
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001384 if (upperpath.dentry) {
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001385 ovl_dentry_set_upper_alias(root_dentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +02001386 if (ovl_is_impuredir(upperpath.dentry))
1387 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001388 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001389
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001390 /* Root is always merge -> can have whiteouts */
1391 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Amir Goldstein2ca3c142018-01-30 13:31:09 +02001392 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001393 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1394 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001395
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001396 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001397
1398 return 0;
1399
Miklos Szeredi4155c102017-11-10 09:39:15 +01001400out_free_oe:
1401 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001402 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001403out_err:
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001404 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01001405 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001406out:
1407 return err;
1408}
1409
1410static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1411 const char *dev_name, void *raw_data)
1412{
1413 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1414}
1415
1416static struct file_system_type ovl_fs_type = {
1417 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001418 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001419 .mount = ovl_mount,
1420 .kill_sb = kill_anon_super,
1421};
Miklos Szeredief94b182014-11-20 16:39:59 +01001422MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001423
Amir Goldstein13cf1992017-06-12 09:54:40 +03001424static void ovl_inode_init_once(void *foo)
1425{
1426 struct ovl_inode *oi = foo;
1427
1428 inode_init_once(&oi->vfs_inode);
1429}
1430
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001431static int __init ovl_init(void)
1432{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001433 int err;
1434
1435 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1436 sizeof(struct ovl_inode), 0,
1437 (SLAB_RECLAIM_ACCOUNT|
1438 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1439 ovl_inode_init_once);
1440 if (ovl_inode_cachep == NULL)
1441 return -ENOMEM;
1442
1443 err = register_filesystem(&ovl_fs_type);
1444 if (err)
1445 kmem_cache_destroy(ovl_inode_cachep);
1446
1447 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001448}
1449
1450static void __exit ovl_exit(void)
1451{
1452 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001453
1454 /*
1455 * Make sure all delayed rcu free inodes are flushed before we
1456 * destroy cache.
1457 */
1458 rcu_barrier();
1459 kmem_cache_destroy(ovl_inode_cachep);
1460
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001461}
1462
1463module_init(ovl_init);
1464module_exit(ovl_exit);