blob: 1d538be87fa05861f828e22a58a51bbcba5eb914 [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);
239 for (i = 0; i < ofs->numlower; i++) {
240 mntput(ofs->lower_layers[i].mnt);
241 free_anon_bdev(ofs->lower_layers[i].pseudo_dev);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +0200242 }
Miklos Szerediad204482017-11-10 09:39:16 +0100243 kfree(ofs->lower_layers);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200244
Miklos Szerediad204482017-11-10 09:39:16 +0100245 kfree(ofs->config.lowerdir);
246 kfree(ofs->config.upperdir);
247 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100248 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100249 if (ofs->creator_cred)
250 put_cred(ofs->creator_cred);
251 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200252}
253
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100254static void ovl_put_super(struct super_block *sb)
255{
256 struct ovl_fs *ofs = sb->s_fs_info;
257
258 ovl_free_fs(ofs);
259}
260
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800261/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200262static int ovl_sync_fs(struct super_block *sb, int wait)
263{
Miklos Szerediad204482017-11-10 09:39:16 +0100264 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200265 struct super_block *upper_sb;
266 int ret;
267
Miklos Szerediad204482017-11-10 09:39:16 +0100268 if (!ofs->upper_mnt)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200269 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800270
271 /*
272 * If this is a sync(2) call or an emergency sync, all the super blocks
273 * will be iterated, including upper_sb, so no need to do anything.
274 *
275 * If this is a syncfs(2) call, then we do need to call
276 * sync_filesystem() on upper_sb, but enough if we do it when being
277 * called with wait == 1.
278 */
279 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200280 return 0;
281
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800282 upper_sb = ofs->upper_mnt->mnt_sb;
283
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200284 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800285 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200286 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800287
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200288 return ret;
289}
290
Andy Whitcroftcc259632014-10-24 00:14:38 +0200291/**
292 * ovl_statfs
293 * @sb: The overlayfs super block
294 * @buf: The struct kstatfs to fill in with stats
295 *
296 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100297 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200298 */
299static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
300{
301 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
302 struct dentry *root_dentry = dentry->d_sb->s_root;
303 struct path path;
304 int err;
305
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100306 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200307
308 err = vfs_statfs(&path, buf);
309 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100310 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200311 buf->f_type = OVERLAYFS_SUPER_MAGIC;
312 }
313
314 return err;
315}
316
Amir Goldstein02bcd152017-06-21 15:28:36 +0300317/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100318static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300319{
Miklos Szerediad204482017-11-10 09:39:16 +0100320 return (!ofs->upper_mnt || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300321}
322
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100323static const char *ovl_redirect_mode_def(void)
324{
325 return ovl_redirect_dir_def ? "on" : "off";
326}
327
Erez Zadokf45827e82014-10-24 00:14:38 +0200328/**
329 * ovl_show_options
330 *
331 * Prints the mount options for a given superblock.
332 * Returns zero; does not fail.
333 */
334static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
335{
336 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100337 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200338
Miklos Szerediad204482017-11-10 09:39:16 +0100339 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
340 if (ofs->config.upperdir) {
341 seq_show_option(m, "upperdir", ofs->config.upperdir);
342 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100343 }
Miklos Szerediad204482017-11-10 09:39:16 +0100344 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200345 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100346 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
347 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100348 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100349 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Amir Goldsteinf168f102018-01-19 11:26:53 +0200350 if (ofs->config.nfs_export != ovl_nfs_export_def)
351 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
352 "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200353 return 0;
354}
355
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900356static int ovl_remount(struct super_block *sb, int *flags, char *data)
357{
Miklos Szerediad204482017-11-10 09:39:16 +0100358 struct ovl_fs *ofs = sb->s_fs_info;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900359
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800360 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900361 return -EROFS;
362
363 return 0;
364}
365
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200366static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300367 .alloc_inode = ovl_alloc_inode,
368 .destroy_inode = ovl_destroy_inode,
369 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200370 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200371 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200372 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200373 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900374 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200375};
376
377enum {
378 OPT_LOWERDIR,
379 OPT_UPPERDIR,
380 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200381 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100382 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300383 OPT_INDEX_ON,
384 OPT_INDEX_OFF,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200385 OPT_NFS_EXPORT_ON,
386 OPT_NFS_EXPORT_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200387 OPT_ERR,
388};
389
390static const match_table_t ovl_tokens = {
391 {OPT_LOWERDIR, "lowerdir=%s"},
392 {OPT_UPPERDIR, "upperdir=%s"},
393 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200394 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100395 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300396 {OPT_INDEX_ON, "index=on"},
397 {OPT_INDEX_OFF, "index=off"},
Amir Goldsteinf168f102018-01-19 11:26:53 +0200398 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
399 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200400 {OPT_ERR, NULL}
401};
402
Miklos Szeredi91c77942014-11-20 16:40:00 +0100403static char *ovl_next_opt(char **s)
404{
405 char *sbegin = *s;
406 char *p;
407
408 if (sbegin == NULL)
409 return NULL;
410
411 for (p = sbegin; *p; p++) {
412 if (*p == '\\') {
413 p++;
414 if (!*p)
415 break;
416 } else if (*p == ',') {
417 *p = '\0';
418 *s = p + 1;
419 return sbegin;
420 }
421 }
422 *s = NULL;
423 return sbegin;
424}
425
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100426static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
427{
428 if (strcmp(mode, "on") == 0) {
429 config->redirect_dir = true;
430 /*
431 * Does not make sense to have redirect creation without
432 * redirect following.
433 */
434 config->redirect_follow = true;
435 } else if (strcmp(mode, "follow") == 0) {
436 config->redirect_follow = true;
437 } else if (strcmp(mode, "off") == 0) {
438 if (ovl_redirect_always_follow)
439 config->redirect_follow = true;
440 } else if (strcmp(mode, "nofollow") != 0) {
441 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
442 mode);
443 return -EINVAL;
444 }
445
446 return 0;
447}
448
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200449static int ovl_parse_opt(char *opt, struct ovl_config *config)
450{
451 char *p;
452
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100453 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
454 if (!config->redirect_mode)
455 return -ENOMEM;
456
Miklos Szeredi91c77942014-11-20 16:40:00 +0100457 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200458 int token;
459 substring_t args[MAX_OPT_ARGS];
460
461 if (!*p)
462 continue;
463
464 token = match_token(p, ovl_tokens, args);
465 switch (token) {
466 case OPT_UPPERDIR:
467 kfree(config->upperdir);
468 config->upperdir = match_strdup(&args[0]);
469 if (!config->upperdir)
470 return -ENOMEM;
471 break;
472
473 case OPT_LOWERDIR:
474 kfree(config->lowerdir);
475 config->lowerdir = match_strdup(&args[0]);
476 if (!config->lowerdir)
477 return -ENOMEM;
478 break;
479
480 case OPT_WORKDIR:
481 kfree(config->workdir);
482 config->workdir = match_strdup(&args[0]);
483 if (!config->workdir)
484 return -ENOMEM;
485 break;
486
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200487 case OPT_DEFAULT_PERMISSIONS:
488 config->default_permissions = true;
489 break;
490
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100491 case OPT_REDIRECT_DIR:
492 kfree(config->redirect_mode);
493 config->redirect_mode = match_strdup(&args[0]);
494 if (!config->redirect_mode)
495 return -ENOMEM;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100496 break;
497
Amir Goldstein02bcd152017-06-21 15:28:36 +0300498 case OPT_INDEX_ON:
499 config->index = true;
500 break;
501
502 case OPT_INDEX_OFF:
503 config->index = false;
504 break;
505
Amir Goldsteinf168f102018-01-19 11:26:53 +0200506 case OPT_NFS_EXPORT_ON:
507 config->nfs_export = true;
508 break;
509
510 case OPT_NFS_EXPORT_OFF:
511 config->nfs_export = false;
512 break;
513
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200514 default:
hujianyangbead55e2015-01-15 13:17:36 +0800515 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200516 return -EINVAL;
517 }
518 }
hujianyang71cbad72015-01-15 13:20:57 +0800519
520 /* Workdir is useless in non-upper mount */
521 if (!config->upperdir && config->workdir) {
522 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
523 config->workdir);
524 kfree(config->workdir);
525 config->workdir = NULL;
526 }
527
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100528 return ovl_parse_redirect_mode(config, config->redirect_mode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200529}
530
531#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300532#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200533
Miklos Szerediad204482017-11-10 09:39:16 +0100534static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300535 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200536{
Miklos Szerediad204482017-11-10 09:39:16 +0100537 struct inode *dir = ofs->workbasedir->d_inode;
538 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200539 struct dentry *work;
540 int err;
541 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300542 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200543
Al Viro59551022016-01-22 15:40:57 -0500544 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300545 locked = true;
546
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200547retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100548 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200549
550 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200551 struct iattr attr = {
552 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000553 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200554 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200555
556 if (work->d_inode) {
557 err = -EEXIST;
558 if (retried)
559 goto out_dput;
560
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300561 if (persist)
562 goto out_unlock;
563
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200564 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200565 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200566 dput(work);
567 goto retry;
568 }
569
Al Viro32a3d842016-12-04 17:33:17 +0000570 err = ovl_create_real(dir, work,
571 &(struct cattr){.mode = S_IFDIR | 0},
572 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200573 if (err)
574 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200575
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200576 /*
577 * Try to remove POSIX ACL xattrs from workdir. We are good if:
578 *
579 * a) success (there was a POSIX ACL xattr and was removed)
580 * b) -ENODATA (there was no POSIX ACL xattr)
581 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
582 *
583 * There are various other error values that could effectively
584 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
585 * if the xattr name is too long), but the set of filesystems
586 * allowed as upper are limited to "normal" ones, where checking
587 * for the above two errors is sufficient.
588 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200589 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200590 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200591 goto out_dput;
592
593 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200594 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200595 goto out_dput;
596
597 /* Clear any inherited mode bits */
598 inode_lock(work->d_inode);
599 err = notify_change(work, &attr, NULL);
600 inode_unlock(work->d_inode);
601 if (err)
602 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300603 } else {
604 err = PTR_ERR(work);
605 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200606 }
607out_unlock:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300608 if (locked)
609 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200610
611 return work;
612
613out_dput:
614 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300615out_err:
616 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100617 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300618 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200619 goto out_unlock;
620}
621
Miklos Szeredi91c77942014-11-20 16:40:00 +0100622static void ovl_unescape(char *s)
623{
624 char *d = s;
625
626 for (;; s++, d++) {
627 if (*s == '\\')
628 s++;
629 *d = *s;
630 if (!*s)
631 break;
632 }
633}
634
Miklos Szerediab508822014-12-13 00:59:49 +0100635static int ovl_mount_dir_noesc(const char *name, struct path *path)
636{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100637 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100638
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100639 if (!*name) {
640 pr_err("overlayfs: empty lowerdir\n");
641 goto out;
642 }
Miklos Szerediab508822014-12-13 00:59:49 +0100643 err = kern_path(name, LOOKUP_FOLLOW, path);
644 if (err) {
645 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
646 goto out;
647 }
648 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200649 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100650 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
651 goto out_put;
652 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100653 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100654 pr_err("overlayfs: '%s' not a directory\n", name);
655 goto out_put;
656 }
657 return 0;
658
659out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100660 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100661out:
662 return err;
663}
664
665static int ovl_mount_dir(const char *name, struct path *path)
666{
667 int err = -ENOMEM;
668 char *tmp = kstrdup(name, GFP_KERNEL);
669
670 if (tmp) {
671 ovl_unescape(tmp);
672 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200673
674 if (!err)
675 if (ovl_dentry_remote(path->dentry)) {
676 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
677 tmp);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100678 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200679 err = -EINVAL;
680 }
Miklos Szerediab508822014-12-13 00:59:49 +0100681 kfree(tmp);
682 }
683 return err;
684}
685
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100686static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
687 const char *name)
688{
689 struct kstatfs statfs;
690 int err = vfs_statfs(path, &statfs);
691
692 if (err)
693 pr_err("overlayfs: statfs failed on '%s'\n", name);
694 else
695 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
696
697 return err;
698}
699
700static int ovl_lower_dir(const char *name, struct path *path,
701 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100702{
703 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100704
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100705 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100706 if (err)
707 goto out;
708
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100709 err = ovl_check_namelen(path, ofs, name);
710 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100711 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100712
Miklos Szerediab508822014-12-13 00:59:49 +0100713 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
714
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200715 if (ovl_dentry_remote(path->dentry))
716 *remote = true;
717
Amir Goldstein02bcd152017-06-21 15:28:36 +0300718 /*
Amir Goldsteinf168f102018-01-19 11:26:53 +0200719 * The inodes index feature and NFS export need to encode and decode
720 * file handles, so they require that all layers support them.
Amir Goldstein02bcd152017-06-21 15:28:36 +0300721 */
Amir Goldsteinf168f102018-01-19 11:26:53 +0200722 if ((ofs->config.nfs_export ||
723 (ofs->config.index && ofs->config.upperdir)) &&
Amir Goldstein972d0092017-09-19 12:14:18 +0300724 !ovl_can_decode_fh(path->dentry->d_sb)) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300725 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200726 ofs->config.nfs_export = false;
727 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
728 name);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300729 }
730
Miklos Szerediab508822014-12-13 00:59:49 +0100731 return 0;
732
733out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100734 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100735out:
736 return err;
737}
738
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200739/* Workdir should not be subdir of upperdir and vice versa */
740static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
741{
742 bool ok = false;
743
744 if (workdir != upperdir) {
745 ok = (lock_rename(workdir, upperdir) == NULL);
746 unlock_rename(workdir, upperdir);
747 }
748 return ok;
749}
750
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100751static unsigned int ovl_split_lowerdirs(char *str)
752{
753 unsigned int ctr = 1;
754 char *s, *d;
755
756 for (s = d = str;; s++, d++) {
757 if (*s == '\\') {
758 s++;
759 } else if (*s == ':') {
760 *d = '\0';
761 ctr++;
762 continue;
763 }
764 *d = *s;
765 if (!*s)
766 break;
767 }
768 return ctr;
769}
770
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200771static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200772ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
773 struct dentry *dentry, struct inode *inode,
774 const char *name, void *buffer, size_t size)
775{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200776 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200777}
778
779static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200780ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
781 struct dentry *dentry, struct inode *inode,
782 const char *name, const void *value,
783 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200784{
785 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200786 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200787 struct posix_acl *acl = NULL;
788 int err;
789
790 /* Check that everything is OK before copy-up */
791 if (value) {
792 acl = posix_acl_from_xattr(&init_user_ns, value, size);
793 if (IS_ERR(acl))
794 return PTR_ERR(acl);
795 }
796 err = -EOPNOTSUPP;
797 if (!IS_POSIXACL(d_inode(workdir)))
798 goto out_acl_release;
799 if (!realinode->i_op->set_acl)
800 goto out_acl_release;
801 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
802 err = acl ? -EACCES : 0;
803 goto out_acl_release;
804 }
805 err = -EPERM;
806 if (!inode_owner_or_capable(inode))
807 goto out_acl_release;
808
809 posix_acl_release(acl);
810
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100811 /*
812 * Check if sgid bit needs to be cleared (actual setacl operation will
813 * be done with mounter's capabilities and so that won't do it for us).
814 */
815 if (unlikely(inode->i_mode & S_ISGID) &&
816 handler->flags == ACL_TYPE_ACCESS &&
817 !in_group_p(inode->i_gid) &&
818 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
819 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
820
821 err = ovl_setattr(dentry, &iattr);
822 if (err)
823 return err;
824 }
825
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200826 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200827 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200828 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200829
830 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200831
832out_acl_release:
833 posix_acl_release(acl);
834 return err;
835}
836
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200837static int ovl_own_xattr_get(const struct xattr_handler *handler,
838 struct dentry *dentry, struct inode *inode,
839 const char *name, void *buffer, size_t size)
840{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200841 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200842}
843
Miklos Szeredid837a492016-07-29 12:05:24 +0200844static int ovl_own_xattr_set(const struct xattr_handler *handler,
845 struct dentry *dentry, struct inode *inode,
846 const char *name, const void *value,
847 size_t size, int flags)
848{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200849 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200850}
851
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200852static int ovl_other_xattr_get(const struct xattr_handler *handler,
853 struct dentry *dentry, struct inode *inode,
854 const char *name, void *buffer, size_t size)
855{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200856 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200857}
858
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200859static int ovl_other_xattr_set(const struct xattr_handler *handler,
860 struct dentry *dentry, struct inode *inode,
861 const char *name, const void *value,
862 size_t size, int flags)
863{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200864 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200865}
866
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200867static const struct xattr_handler __maybe_unused
868ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200869 .name = XATTR_NAME_POSIX_ACL_ACCESS,
870 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200871 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200872 .set = ovl_posix_acl_xattr_set,
873};
874
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200875static const struct xattr_handler __maybe_unused
876ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200877 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
878 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200879 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200880 .set = ovl_posix_acl_xattr_set,
881};
882
883static const struct xattr_handler ovl_own_xattr_handler = {
884 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200885 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200886 .set = ovl_own_xattr_set,
887};
888
889static const struct xattr_handler ovl_other_xattr_handler = {
890 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200891 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200892 .set = ovl_other_xattr_set,
893};
894
895static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200896#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200897 &ovl_posix_acl_access_xattr_handler,
898 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200899#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200900 &ovl_own_xattr_handler,
901 &ovl_other_xattr_handler,
902 NULL
903};
904
Miklos Szerediad204482017-11-10 09:39:16 +0100905static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100906{
Miklos Szeredi50649752017-11-10 09:39:15 +0100907 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100908 int err;
909
Miklos Szerediad204482017-11-10 09:39:16 +0100910 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100911 if (err)
912 goto out;
913
914 /* Upper fs should not be r/o */
915 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
916 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
917 err = -EINVAL;
918 goto out;
919 }
920
Miklos Szerediad204482017-11-10 09:39:16 +0100921 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100922 if (err)
923 goto out;
924
925 err = -EBUSY;
926 if (ovl_inuse_trylock(upperpath->dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +0100927 ofs->upperdir_locked = true;
928 } else if (ofs->config.index) {
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100929 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
930 goto out;
931 } else {
932 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
933 }
Miklos Szeredi50649752017-11-10 09:39:15 +0100934
935 upper_mnt = clone_private_mount(upperpath);
936 err = PTR_ERR(upper_mnt);
937 if (IS_ERR(upper_mnt)) {
938 pr_err("overlayfs: failed to clone upperpath\n");
939 goto out;
940 }
941
942 /* Don't inherit atime flags */
943 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szerediad204482017-11-10 09:39:16 +0100944 ofs->upper_mnt = upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100945 err = 0;
946out:
947 return err;
948}
949
Miklos Szerediad204482017-11-10 09:39:16 +0100950static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100951{
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200952 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100953 struct dentry *temp;
954 int err;
955
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200956 err = mnt_want_write(mnt);
957 if (err)
958 return err;
959
Miklos Szerediad204482017-11-10 09:39:16 +0100960 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
961 if (!ofs->workdir)
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200962 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100963
964 /*
965 * Upper should support d_type, else whiteouts are visible. Given
966 * workdir and upper are on same fs, we can do iterate_dir() on
967 * workdir. This check requires successful creation of workdir in
968 * previous step.
969 */
970 err = ovl_check_d_type_supported(workpath);
971 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200972 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100973
974 /*
975 * We allowed this configuration and don't want to break users over
976 * kernel upgrade. So warn instead of erroring out.
977 */
978 if (!err)
979 pr_warn("overlayfs: upper fs needs to support d_type.\n");
980
981 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +0100982 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
983 ofs->tmpfile = !IS_ERR(temp);
984 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100985 dput(temp);
986 else
987 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
988
989 /*
990 * Check if upper/work fs supports trusted.overlay.* xattr
991 */
Miklos Szerediad204482017-11-10 09:39:16 +0100992 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100993 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +0100994 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +0300995 ofs->config.index = false;
996 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off.\n");
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200997 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100998 } else {
Miklos Szerediad204482017-11-10 09:39:16 +0100999 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001000 }
1001
1002 /* Check if upper/work fs supports file handles */
Miklos Szerediad204482017-11-10 09:39:16 +01001003 if (ofs->config.index &&
1004 !ovl_can_decode_fh(ofs->workdir->d_sb)) {
1005 ofs->config.index = false;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001006 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1007 }
1008
Amir Goldsteinf168f102018-01-19 11:26:53 +02001009 /* NFS export of r/w mount depends on index */
1010 if (ofs->config.nfs_export && !ofs->config.index) {
1011 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1012 ofs->config.nfs_export = false;
1013 }
1014
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001015out:
1016 mnt_drop_write(mnt);
1017 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001018}
1019
Miklos Szerediad204482017-11-10 09:39:16 +01001020static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001021{
1022 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001023 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001024
Miklos Szerediad204482017-11-10 09:39:16 +01001025 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001026 if (err)
1027 goto out;
1028
1029 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001030 if (upperpath->mnt != workpath.mnt) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001031 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1032 goto out;
1033 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001034 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001035 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1036 goto out;
1037 }
1038
1039 err = -EBUSY;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001040 if (ovl_inuse_trylock(workpath.dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001041 ofs->workdir_locked = true;
1042 } else if (ofs->config.index) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001043 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1044 goto out;
1045 } else {
1046 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1047 }
1048
Miklos Szerediad204482017-11-10 09:39:16 +01001049 ofs->workbasedir = dget(workpath.dentry);
1050 err = ovl_make_workdir(ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001051 if (err)
1052 goto out;
1053
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001054 err = 0;
1055out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001056 path_put(&workpath);
1057
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001058 return err;
1059}
1060
Miklos Szerediad204482017-11-10 09:39:16 +01001061static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001062 struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001063{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001064 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001065 int err;
1066
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001067 err = mnt_want_write(mnt);
1068 if (err)
1069 return err;
1070
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001071 /* Verify lower root is upper root origin */
Amir Goldsteind9768072017-09-24 13:00:19 +03001072 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
Amir Goldstein05122442018-01-11 08:25:32 +02001073 true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001074 if (err) {
1075 pr_err("overlayfs: failed to verify upper root origin\n");
1076 goto out;
1077 }
1078
Miklos Szerediad204482017-11-10 09:39:16 +01001079 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1080 if (ofs->indexdir) {
Amir Goldstein05122442018-01-11 08:25:32 +02001081 /* Verify upper root is exclusively associated with index dir */
1082 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1083 upperpath->dentry, true, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001084 if (err)
1085 pr_err("overlayfs: failed to verify index dir origin\n");
1086
1087 /* Cleanup bad/stale/orphan index entries */
1088 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001089 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001090 }
Miklos Szerediad204482017-11-10 09:39:16 +01001091 if (err || !ofs->indexdir)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001092 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1093
1094out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001095 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001096 return err;
1097}
1098
Miklos Szerediad204482017-11-10 09:39:16 +01001099static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack,
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001100 unsigned int numlower)
1101{
1102 int err;
1103 unsigned int i;
1104
1105 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001106 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001107 GFP_KERNEL);
Miklos Szerediad204482017-11-10 09:39:16 +01001108 if (ofs->lower_layers == NULL)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001109 goto out;
1110 for (i = 0; i < numlower; i++) {
1111 struct vfsmount *mnt;
1112 dev_t dev;
1113
1114 err = get_anon_bdev(&dev);
1115 if (err) {
1116 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1117 goto out;
1118 }
1119
1120 mnt = clone_private_mount(&stack[i]);
1121 err = PTR_ERR(mnt);
1122 if (IS_ERR(mnt)) {
1123 pr_err("overlayfs: failed to clone lowerpath\n");
1124 free_anon_bdev(dev);
1125 goto out;
1126 }
1127 /*
1128 * Make lower layers R/O. That way fchmod/fchown on lower file
1129 * will fail instead of modifying lower fs.
1130 */
1131 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1132
Miklos Szerediad204482017-11-10 09:39:16 +01001133 ofs->lower_layers[ofs->numlower].mnt = mnt;
1134 ofs->lower_layers[ofs->numlower].pseudo_dev = dev;
Amir Goldsteind583ed72017-11-08 19:23:36 +02001135 ofs->lower_layers[ofs->numlower].idx = i + 1;
Miklos Szerediad204482017-11-10 09:39:16 +01001136 ofs->numlower++;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001137
1138 /* Check if all lower layers are on same sb */
1139 if (i == 0)
Miklos Szerediad204482017-11-10 09:39:16 +01001140 ofs->same_sb = mnt->mnt_sb;
1141 else if (ofs->same_sb != mnt->mnt_sb)
1142 ofs->same_sb = NULL;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001143 }
1144 err = 0;
1145out:
1146 return err;
1147}
1148
Miklos Szeredi4155c102017-11-10 09:39:15 +01001149static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szerediad204482017-11-10 09:39:16 +01001150 struct ovl_fs *ofs)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001151{
1152 int err;
1153 char *lowertmp, *lower;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001154 struct path *stack = NULL;
1155 unsigned int stacklen, numlower = 0, i;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001156 bool remote = false;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001157 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001158
1159 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001160 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001161 if (!lowertmp)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001162 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001163
1164 err = -EINVAL;
1165 stacklen = ovl_split_lowerdirs(lowertmp);
1166 if (stacklen > OVL_MAX_STACK) {
1167 pr_err("overlayfs: too many lower directories, limit is %d\n",
1168 OVL_MAX_STACK);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001169 goto out_err;
Miklos Szerediad204482017-11-10 09:39:16 +01001170 } else if (!ofs->config.upperdir && stacklen == 1) {
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001171 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001172 goto out_err;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001173 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1174 ofs->config.redirect_follow) {
1175 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1176 ofs->config.nfs_export = false;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001177 }
1178
1179 err = -ENOMEM;
1180 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1181 if (!stack)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001182 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001183
1184 err = -EINVAL;
1185 lower = lowertmp;
1186 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szerediad204482017-11-10 09:39:16 +01001187 err = ovl_lower_dir(lower, &stack[numlower], ofs,
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001188 &sb->s_stack_depth, &remote);
1189 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001190 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001191
1192 lower = strchr(lower, '\0') + 1;
1193 }
1194
1195 err = -EINVAL;
1196 sb->s_stack_depth++;
1197 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1198 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001199 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001200 }
1201
Miklos Szerediad204482017-11-10 09:39:16 +01001202 err = ovl_get_lower_layers(ofs, stack, numlower);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001203 if (err)
1204 goto out_err;
1205
1206 err = -ENOMEM;
1207 oe = ovl_alloc_entry(numlower);
1208 if (!oe)
1209 goto out_err;
1210
1211 for (i = 0; i < numlower; i++) {
1212 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Miklos Szerediad204482017-11-10 09:39:16 +01001213 oe->lowerstack[i].layer = &ofs->lower_layers[i];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001214 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001215
1216 if (remote)
1217 sb->s_d_op = &ovl_reval_dentry_operations;
1218 else
1219 sb->s_d_op = &ovl_dentry_operations;
1220
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001221out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001222 for (i = 0; i < numlower; i++)
1223 path_put(&stack[i]);
1224 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001225 kfree(lowertmp);
1226
1227 return oe;
1228
1229out_err:
1230 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001231 goto out;
1232}
1233
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001234static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1235{
Kees Cook33006cd2017-03-29 14:02:19 -07001236 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001237 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001238 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001239 struct ovl_fs *ofs;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001240 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001241 int err;
1242
Erez Zadokf45827e82014-10-24 00:14:38 +02001243 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001244 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1245 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001246 goto out;
1247
Miklos Szerediad204482017-11-10 09:39:16 +01001248 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001249 if (!cred)
1250 goto out_err;
1251
Miklos Szerediad204482017-11-10 09:39:16 +01001252 ofs->config.index = ovl_index_def;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001253 ofs->config.nfs_export = ovl_nfs_export_def;
Miklos Szerediad204482017-11-10 09:39:16 +01001254 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001255 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001256 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001257
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001258 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001259 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001260 if (!silent)
1261 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001262 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001263 }
1264
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001265 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001266 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szerediad204482017-11-10 09:39:16 +01001267 if (ofs->config.upperdir) {
1268 if (!ofs->config.workdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001269 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001270 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001271 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001272
Miklos Szerediad204482017-11-10 09:39:16 +01001273 err = ovl_get_upper(ofs, &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001274 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001275 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001276
Miklos Szerediad204482017-11-10 09:39:16 +01001277 err = ovl_get_workdir(ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001278 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001279 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001280
Miklos Szerediad204482017-11-10 09:39:16 +01001281 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001282 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001283
Miklos Szerediad204482017-11-10 09:39:16 +01001284 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1285 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001286
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001287 }
Miklos Szerediad204482017-11-10 09:39:16 +01001288 oe = ovl_get_lowerstack(sb, ofs);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001289 err = PTR_ERR(oe);
1290 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001291 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001292
hujianyang71cbad72015-01-15 13:20:57 +08001293 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szerediad204482017-11-10 09:39:16 +01001294 if (!ofs->upper_mnt)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001295 sb->s_flags |= SB_RDONLY;
Miklos Szerediad204482017-11-10 09:39:16 +01001296 else if (ofs->upper_mnt->mnt_sb != ofs->same_sb)
1297 ofs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001298
Miklos Szerediad204482017-11-10 09:39:16 +01001299 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1300 err = ovl_get_indexdir(ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001301 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001302 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001303
Amir Goldstein972d0092017-09-19 12:14:18 +03001304 /* Force r/o mount with no index dir */
1305 if (!ofs->indexdir) {
1306 dput(ofs->workdir);
1307 ofs->workdir = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001308 sb->s_flags |= SB_RDONLY;
Amir Goldstein972d0092017-09-19 12:14:18 +03001309 }
1310
Amir Goldstein02bcd152017-06-21 15:28:36 +03001311 }
1312
Amir Goldstein972d0092017-09-19 12:14:18 +03001313 /* Show index=off in /proc/mounts for forced r/o mount */
Amir Goldsteinf168f102018-01-19 11:26:53 +02001314 if (!ofs->indexdir) {
Miklos Szerediad204482017-11-10 09:39:16 +01001315 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001316 if (ofs->upper_mnt && ofs->config.nfs_export) {
1317 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1318 ofs->config.nfs_export = false;
1319 }
1320 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001321
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001322 /* Never override disk quota limits or use reserved space */
1323 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1324
Vivek Goyal655042c2016-10-14 03:03:36 +02001325 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1326 sb->s_op = &ovl_super_operations;
1327 sb->s_xattr = ovl_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01001328 sb->s_fs_info = ofs;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001329 sb->s_flags |= SB_POSIXACL | SB_NOREMOTELOCK;
Vivek Goyal655042c2016-10-14 03:03:36 +02001330
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001331 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001332 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001333 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001334 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001335
1336 mntput(upperpath.mnt);
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001337 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001338 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001339 if (ovl_is_impuredir(upperpath.dentry))
1340 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001341 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001342
1343 root_dentry->d_fsdata = oe;
1344
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001345 /* Root is always merge -> can have whiteouts */
1346 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001347 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1348 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001349
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001350 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001351
1352 return 0;
1353
Miklos Szeredi4155c102017-11-10 09:39:15 +01001354out_free_oe:
1355 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001356 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001357out_err:
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001358 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01001359 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001360out:
1361 return err;
1362}
1363
1364static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1365 const char *dev_name, void *raw_data)
1366{
1367 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1368}
1369
1370static struct file_system_type ovl_fs_type = {
1371 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001372 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001373 .mount = ovl_mount,
1374 .kill_sb = kill_anon_super,
1375};
Miklos Szeredief94b182014-11-20 16:39:59 +01001376MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001377
Amir Goldstein13cf1992017-06-12 09:54:40 +03001378static void ovl_inode_init_once(void *foo)
1379{
1380 struct ovl_inode *oi = foo;
1381
1382 inode_init_once(&oi->vfs_inode);
1383}
1384
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001385static int __init ovl_init(void)
1386{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001387 int err;
1388
1389 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1390 sizeof(struct ovl_inode), 0,
1391 (SLAB_RECLAIM_ACCOUNT|
1392 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1393 ovl_inode_init_once);
1394 if (ovl_inode_cachep == NULL)
1395 return -ENOMEM;
1396
1397 err = register_filesystem(&ovl_fs_type);
1398 if (err)
1399 kmem_cache_destroy(ovl_inode_cachep);
1400
1401 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001402}
1403
1404static void __exit ovl_exit(void)
1405{
1406 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001407
1408 /*
1409 * Make sure all delayed rcu free inodes are flushed before we
1410 * destroy cache.
1411 */
1412 rcu_barrier();
1413 kmem_cache_destroy(ovl_inode_cachep);
1414
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001415}
1416
1417module_init(ovl_init);
1418module_exit(ovl_exit);