blob: 4345c3f83fde01536ad040d95e4fdafd1696abf1 [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
Miklos Szeredi4155c102017-11-10 09:39:15 +010048static void ovl_entry_stack_free(struct ovl_entry *oe)
49{
50 unsigned int i;
51
52 for (i = 0; i < oe->numlower; i++)
53 dput(oe->lowerstack[i].dentry);
54}
55
Miklos Szeredie9be9d52014-10-24 00:14:38 +020056static void ovl_dentry_release(struct dentry *dentry)
57{
58 struct ovl_entry *oe = dentry->d_fsdata;
59
60 if (oe) {
Miklos Szeredi4155c102017-11-10 09:39:15 +010061 ovl_entry_stack_free(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020062 kfree_rcu(oe, rcu);
63 }
64}
65
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030066static int ovl_check_append_only(struct inode *inode, int flag)
67{
68 /*
69 * This test was moot in vfs may_open() because overlay inode does
70 * not have the S_APPEND flag, so re-check on real upper inode
71 */
72 if (IS_APPEND(inode)) {
73 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
74 return -EPERM;
75 if (flag & O_TRUNC)
76 return -EPERM;
77 }
78
79 return 0;
80}
81
Miklos Szeredi2d902672016-06-30 08:53:27 +020082static struct dentry *ovl_d_real(struct dentry *dentry,
83 const struct inode *inode,
Miklos Szeredi495e6422017-09-04 21:42:22 +020084 unsigned int open_flags, unsigned int flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040085{
86 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030087 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040088
Miklos Szeredicd913042017-09-05 12:53:11 +020089 if (flags & D_REAL_UPPER)
90 return ovl_dentry_upper(dentry);
91
Miklos Szeredica4c8a32016-12-16 11:02:55 +010092 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040093 if (!inode || inode == d_inode(dentry))
94 return dentry;
95 goto bug;
96 }
97
Miklos Szeredi2d902672016-06-30 08:53:27 +020098 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030099 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +0200100 if (err)
101 return ERR_PTR(err);
102 }
103
Miklos Szeredid101a122016-03-26 16:14:37 -0400104 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300105 if (real && (!inode || inode == d_inode(real))) {
106 if (!inode) {
107 err = ovl_check_append_only(d_inode(real), open_flags);
108 if (err)
109 return ERR_PTR(err);
110 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400111 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300112 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400113
114 real = ovl_dentry_lower(dentry);
115 if (!real)
116 goto bug;
117
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100118 /* Handle recursion */
Miklos Szeredi495e6422017-09-04 21:42:22 +0200119 real = d_real(real, inode, open_flags, 0);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100120
Miklos Szeredid101a122016-03-26 16:14:37 -0400121 if (!inode || inode == d_inode(real))
122 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400123bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200124 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400125 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
126 return dentry;
127}
128
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200129static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
130{
131 struct ovl_entry *oe = dentry->d_fsdata;
132 unsigned int i;
133 int ret = 1;
134
135 for (i = 0; i < oe->numlower; i++) {
136 struct dentry *d = oe->lowerstack[i].dentry;
137
138 if (d->d_flags & DCACHE_OP_REVALIDATE) {
139 ret = d->d_op->d_revalidate(d, flags);
140 if (ret < 0)
141 return ret;
142 if (!ret) {
143 if (!(flags & LOOKUP_RCU))
144 d_invalidate(d);
145 return -ESTALE;
146 }
147 }
148 }
149 return 1;
150}
151
152static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
153{
154 struct ovl_entry *oe = dentry->d_fsdata;
155 unsigned int i;
156 int ret = 1;
157
158 for (i = 0; i < oe->numlower; i++) {
159 struct dentry *d = oe->lowerstack[i].dentry;
160
161 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
162 ret = d->d_op->d_weak_revalidate(d, flags);
163 if (ret <= 0)
164 break;
165 }
166 }
167 return ret;
168}
169
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200170static const struct dentry_operations ovl_dentry_operations = {
171 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400172 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200173};
174
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200175static const struct dentry_operations ovl_reval_dentry_operations = {
176 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400177 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200178 .d_revalidate = ovl_dentry_revalidate,
179 .d_weak_revalidate = ovl_dentry_weak_revalidate,
180};
181
Amir Goldstein13cf1992017-06-12 09:54:40 +0300182static struct kmem_cache *ovl_inode_cachep;
183
184static struct inode *ovl_alloc_inode(struct super_block *sb)
185{
186 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
187
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900188 if (!oi)
189 return NULL;
190
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200191 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200192 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200193 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200194 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200195 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200196 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300197 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200198
Amir Goldstein13cf1992017-06-12 09:54:40 +0300199 return &oi->vfs_inode;
200}
201
202static void ovl_i_callback(struct rcu_head *head)
203{
204 struct inode *inode = container_of(head, struct inode, i_rcu);
205
206 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
207}
208
209static void ovl_destroy_inode(struct inode *inode)
210{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200211 struct ovl_inode *oi = OVL_I(inode);
212
213 dput(oi->__upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200214 iput(oi->lower);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200215 kfree(oi->redirect);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200216 ovl_dir_cache_free(inode);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300217 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200218
Amir Goldstein13cf1992017-06-12 09:54:40 +0300219 call_rcu(&inode->i_rcu, ovl_i_callback);
220}
221
Miklos Szerediad204482017-11-10 09:39:16 +0100222static void ovl_free_fs(struct ovl_fs *ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200223{
Miklos Szeredidd662662014-12-13 00:59:43 +0100224 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200225
Miklos Szerediad204482017-11-10 09:39:16 +0100226 dput(ofs->indexdir);
227 dput(ofs->workdir);
228 if (ofs->workdir_locked)
229 ovl_inuse_unlock(ofs->workbasedir);
230 dput(ofs->workbasedir);
231 if (ofs->upperdir_locked)
232 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
233 mntput(ofs->upper_mnt);
234 for (i = 0; i < ofs->numlower; i++) {
235 mntput(ofs->lower_layers[i].mnt);
236 free_anon_bdev(ofs->lower_layers[i].pseudo_dev);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +0200237 }
Miklos Szerediad204482017-11-10 09:39:16 +0100238 kfree(ofs->lower_layers);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200239
Miklos Szerediad204482017-11-10 09:39:16 +0100240 kfree(ofs->config.lowerdir);
241 kfree(ofs->config.upperdir);
242 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100243 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100244 if (ofs->creator_cred)
245 put_cred(ofs->creator_cred);
246 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200247}
248
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100249static void ovl_put_super(struct super_block *sb)
250{
251 struct ovl_fs *ofs = sb->s_fs_info;
252
253 ovl_free_fs(ofs);
254}
255
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800256/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200257static int ovl_sync_fs(struct super_block *sb, int wait)
258{
Miklos Szerediad204482017-11-10 09:39:16 +0100259 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200260 struct super_block *upper_sb;
261 int ret;
262
Miklos Szerediad204482017-11-10 09:39:16 +0100263 if (!ofs->upper_mnt)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200264 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800265
266 /*
267 * If this is a sync(2) call or an emergency sync, all the super blocks
268 * will be iterated, including upper_sb, so no need to do anything.
269 *
270 * If this is a syncfs(2) call, then we do need to call
271 * sync_filesystem() on upper_sb, but enough if we do it when being
272 * called with wait == 1.
273 */
274 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200275 return 0;
276
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800277 upper_sb = ofs->upper_mnt->mnt_sb;
278
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200279 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800280 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200281 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800282
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200283 return ret;
284}
285
Andy Whitcroftcc259632014-10-24 00:14:38 +0200286/**
287 * ovl_statfs
288 * @sb: The overlayfs super block
289 * @buf: The struct kstatfs to fill in with stats
290 *
291 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100292 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200293 */
294static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
295{
296 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
297 struct dentry *root_dentry = dentry->d_sb->s_root;
298 struct path path;
299 int err;
300
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100301 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200302
303 err = vfs_statfs(&path, buf);
304 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100305 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200306 buf->f_type = OVERLAYFS_SUPER_MAGIC;
307 }
308
309 return err;
310}
311
Amir Goldstein02bcd152017-06-21 15:28:36 +0300312/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100313static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300314{
Miklos Szerediad204482017-11-10 09:39:16 +0100315 return (!ofs->upper_mnt || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300316}
317
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100318static const char *ovl_redirect_mode_def(void)
319{
320 return ovl_redirect_dir_def ? "on" : "off";
321}
322
Erez Zadokf45827e82014-10-24 00:14:38 +0200323/**
324 * ovl_show_options
325 *
326 * Prints the mount options for a given superblock.
327 * Returns zero; does not fail.
328 */
329static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
330{
331 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100332 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200333
Miklos Szerediad204482017-11-10 09:39:16 +0100334 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
335 if (ofs->config.upperdir) {
336 seq_show_option(m, "upperdir", ofs->config.upperdir);
337 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100338 }
Miklos Szerediad204482017-11-10 09:39:16 +0100339 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200340 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100341 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
342 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100343 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100344 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200345 return 0;
346}
347
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900348static int ovl_remount(struct super_block *sb, int *flags, char *data)
349{
Miklos Szerediad204482017-11-10 09:39:16 +0100350 struct ovl_fs *ofs = sb->s_fs_info;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900351
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800352 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900353 return -EROFS;
354
355 return 0;
356}
357
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200358static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300359 .alloc_inode = ovl_alloc_inode,
360 .destroy_inode = ovl_destroy_inode,
361 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200363 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200364 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200365 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900366 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200367};
368
369enum {
370 OPT_LOWERDIR,
371 OPT_UPPERDIR,
372 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200373 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100374 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300375 OPT_INDEX_ON,
376 OPT_INDEX_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200377 OPT_ERR,
378};
379
380static const match_table_t ovl_tokens = {
381 {OPT_LOWERDIR, "lowerdir=%s"},
382 {OPT_UPPERDIR, "upperdir=%s"},
383 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200384 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100385 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300386 {OPT_INDEX_ON, "index=on"},
387 {OPT_INDEX_OFF, "index=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200388 {OPT_ERR, NULL}
389};
390
Miklos Szeredi91c77942014-11-20 16:40:00 +0100391static char *ovl_next_opt(char **s)
392{
393 char *sbegin = *s;
394 char *p;
395
396 if (sbegin == NULL)
397 return NULL;
398
399 for (p = sbegin; *p; p++) {
400 if (*p == '\\') {
401 p++;
402 if (!*p)
403 break;
404 } else if (*p == ',') {
405 *p = '\0';
406 *s = p + 1;
407 return sbegin;
408 }
409 }
410 *s = NULL;
411 return sbegin;
412}
413
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100414static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
415{
416 if (strcmp(mode, "on") == 0) {
417 config->redirect_dir = true;
418 /*
419 * Does not make sense to have redirect creation without
420 * redirect following.
421 */
422 config->redirect_follow = true;
423 } else if (strcmp(mode, "follow") == 0) {
424 config->redirect_follow = true;
425 } else if (strcmp(mode, "off") == 0) {
426 if (ovl_redirect_always_follow)
427 config->redirect_follow = true;
428 } else if (strcmp(mode, "nofollow") != 0) {
429 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
430 mode);
431 return -EINVAL;
432 }
433
434 return 0;
435}
436
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200437static int ovl_parse_opt(char *opt, struct ovl_config *config)
438{
439 char *p;
440
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100441 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
442 if (!config->redirect_mode)
443 return -ENOMEM;
444
Miklos Szeredi91c77942014-11-20 16:40:00 +0100445 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200446 int token;
447 substring_t args[MAX_OPT_ARGS];
448
449 if (!*p)
450 continue;
451
452 token = match_token(p, ovl_tokens, args);
453 switch (token) {
454 case OPT_UPPERDIR:
455 kfree(config->upperdir);
456 config->upperdir = match_strdup(&args[0]);
457 if (!config->upperdir)
458 return -ENOMEM;
459 break;
460
461 case OPT_LOWERDIR:
462 kfree(config->lowerdir);
463 config->lowerdir = match_strdup(&args[0]);
464 if (!config->lowerdir)
465 return -ENOMEM;
466 break;
467
468 case OPT_WORKDIR:
469 kfree(config->workdir);
470 config->workdir = match_strdup(&args[0]);
471 if (!config->workdir)
472 return -ENOMEM;
473 break;
474
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200475 case OPT_DEFAULT_PERMISSIONS:
476 config->default_permissions = true;
477 break;
478
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100479 case OPT_REDIRECT_DIR:
480 kfree(config->redirect_mode);
481 config->redirect_mode = match_strdup(&args[0]);
482 if (!config->redirect_mode)
483 return -ENOMEM;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100484 break;
485
Amir Goldstein02bcd152017-06-21 15:28:36 +0300486 case OPT_INDEX_ON:
487 config->index = true;
488 break;
489
490 case OPT_INDEX_OFF:
491 config->index = false;
492 break;
493
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200494 default:
hujianyangbead55e2015-01-15 13:17:36 +0800495 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200496 return -EINVAL;
497 }
498 }
hujianyang71cbad72015-01-15 13:20:57 +0800499
500 /* Workdir is useless in non-upper mount */
501 if (!config->upperdir && config->workdir) {
502 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
503 config->workdir);
504 kfree(config->workdir);
505 config->workdir = NULL;
506 }
507
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100508 return ovl_parse_redirect_mode(config, config->redirect_mode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200509}
510
511#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300512#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200513
Miklos Szerediad204482017-11-10 09:39:16 +0100514static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300515 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200516{
Miklos Szerediad204482017-11-10 09:39:16 +0100517 struct inode *dir = ofs->workbasedir->d_inode;
518 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200519 struct dentry *work;
520 int err;
521 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300522 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200523
Al Viro59551022016-01-22 15:40:57 -0500524 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300525 locked = true;
526
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200527retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100528 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200529
530 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200531 struct iattr attr = {
532 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000533 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200534 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200535
536 if (work->d_inode) {
537 err = -EEXIST;
538 if (retried)
539 goto out_dput;
540
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300541 if (persist)
542 goto out_unlock;
543
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200545 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200546 dput(work);
547 goto retry;
548 }
549
Al Viro32a3d842016-12-04 17:33:17 +0000550 err = ovl_create_real(dir, work,
551 &(struct cattr){.mode = S_IFDIR | 0},
552 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200553 if (err)
554 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200555
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200556 /*
557 * Try to remove POSIX ACL xattrs from workdir. We are good if:
558 *
559 * a) success (there was a POSIX ACL xattr and was removed)
560 * b) -ENODATA (there was no POSIX ACL xattr)
561 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
562 *
563 * There are various other error values that could effectively
564 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
565 * if the xattr name is too long), but the set of filesystems
566 * allowed as upper are limited to "normal" ones, where checking
567 * for the above two errors is sufficient.
568 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200569 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200570 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200571 goto out_dput;
572
573 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200574 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200575 goto out_dput;
576
577 /* Clear any inherited mode bits */
578 inode_lock(work->d_inode);
579 err = notify_change(work, &attr, NULL);
580 inode_unlock(work->d_inode);
581 if (err)
582 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300583 } else {
584 err = PTR_ERR(work);
585 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200586 }
587out_unlock:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300588 if (locked)
589 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200590
591 return work;
592
593out_dput:
594 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300595out_err:
596 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100597 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300598 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200599 goto out_unlock;
600}
601
Miklos Szeredi91c77942014-11-20 16:40:00 +0100602static void ovl_unescape(char *s)
603{
604 char *d = s;
605
606 for (;; s++, d++) {
607 if (*s == '\\')
608 s++;
609 *d = *s;
610 if (!*s)
611 break;
612 }
613}
614
Miklos Szerediab508822014-12-13 00:59:49 +0100615static int ovl_mount_dir_noesc(const char *name, struct path *path)
616{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100617 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100618
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100619 if (!*name) {
620 pr_err("overlayfs: empty lowerdir\n");
621 goto out;
622 }
Miklos Szerediab508822014-12-13 00:59:49 +0100623 err = kern_path(name, LOOKUP_FOLLOW, path);
624 if (err) {
625 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
626 goto out;
627 }
628 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200629 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100630 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
631 goto out_put;
632 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100633 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100634 pr_err("overlayfs: '%s' not a directory\n", name);
635 goto out_put;
636 }
637 return 0;
638
639out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100640 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100641out:
642 return err;
643}
644
645static int ovl_mount_dir(const char *name, struct path *path)
646{
647 int err = -ENOMEM;
648 char *tmp = kstrdup(name, GFP_KERNEL);
649
650 if (tmp) {
651 ovl_unescape(tmp);
652 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200653
654 if (!err)
655 if (ovl_dentry_remote(path->dentry)) {
656 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
657 tmp);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100658 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200659 err = -EINVAL;
660 }
Miklos Szerediab508822014-12-13 00:59:49 +0100661 kfree(tmp);
662 }
663 return err;
664}
665
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100666static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
667 const char *name)
668{
669 struct kstatfs statfs;
670 int err = vfs_statfs(path, &statfs);
671
672 if (err)
673 pr_err("overlayfs: statfs failed on '%s'\n", name);
674 else
675 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
676
677 return err;
678}
679
680static int ovl_lower_dir(const char *name, struct path *path,
681 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100682{
683 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100684
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100685 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100686 if (err)
687 goto out;
688
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100689 err = ovl_check_namelen(path, ofs, name);
690 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100691 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100692
Miklos Szerediab508822014-12-13 00:59:49 +0100693 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
694
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200695 if (ovl_dentry_remote(path->dentry))
696 *remote = true;
697
Amir Goldstein02bcd152017-06-21 15:28:36 +0300698 /*
699 * The inodes index feature needs to encode and decode file
700 * handles, so it requires that all layers support them.
701 */
Amir Goldstein972d0092017-09-19 12:14:18 +0300702 if (ofs->config.index && ofs->config.upperdir &&
703 !ovl_can_decode_fh(path->dentry->d_sb)) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300704 ofs->config.index = false;
705 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
706 }
707
Miklos Szerediab508822014-12-13 00:59:49 +0100708 return 0;
709
710out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100711 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100712out:
713 return err;
714}
715
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200716/* Workdir should not be subdir of upperdir and vice versa */
717static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
718{
719 bool ok = false;
720
721 if (workdir != upperdir) {
722 ok = (lock_rename(workdir, upperdir) == NULL);
723 unlock_rename(workdir, upperdir);
724 }
725 return ok;
726}
727
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100728static unsigned int ovl_split_lowerdirs(char *str)
729{
730 unsigned int ctr = 1;
731 char *s, *d;
732
733 for (s = d = str;; s++, d++) {
734 if (*s == '\\') {
735 s++;
736 } else if (*s == ':') {
737 *d = '\0';
738 ctr++;
739 continue;
740 }
741 *d = *s;
742 if (!*s)
743 break;
744 }
745 return ctr;
746}
747
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200748static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200749ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
750 struct dentry *dentry, struct inode *inode,
751 const char *name, void *buffer, size_t size)
752{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200753 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200754}
755
756static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200757ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
758 struct dentry *dentry, struct inode *inode,
759 const char *name, const void *value,
760 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200761{
762 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200763 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200764 struct posix_acl *acl = NULL;
765 int err;
766
767 /* Check that everything is OK before copy-up */
768 if (value) {
769 acl = posix_acl_from_xattr(&init_user_ns, value, size);
770 if (IS_ERR(acl))
771 return PTR_ERR(acl);
772 }
773 err = -EOPNOTSUPP;
774 if (!IS_POSIXACL(d_inode(workdir)))
775 goto out_acl_release;
776 if (!realinode->i_op->set_acl)
777 goto out_acl_release;
778 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
779 err = acl ? -EACCES : 0;
780 goto out_acl_release;
781 }
782 err = -EPERM;
783 if (!inode_owner_or_capable(inode))
784 goto out_acl_release;
785
786 posix_acl_release(acl);
787
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100788 /*
789 * Check if sgid bit needs to be cleared (actual setacl operation will
790 * be done with mounter's capabilities and so that won't do it for us).
791 */
792 if (unlikely(inode->i_mode & S_ISGID) &&
793 handler->flags == ACL_TYPE_ACCESS &&
794 !in_group_p(inode->i_gid) &&
795 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
796 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
797
798 err = ovl_setattr(dentry, &iattr);
799 if (err)
800 return err;
801 }
802
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200803 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200804 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200805 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200806
807 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200808
809out_acl_release:
810 posix_acl_release(acl);
811 return err;
812}
813
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200814static int ovl_own_xattr_get(const struct xattr_handler *handler,
815 struct dentry *dentry, struct inode *inode,
816 const char *name, void *buffer, size_t size)
817{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200818 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200819}
820
Miklos Szeredid837a492016-07-29 12:05:24 +0200821static int ovl_own_xattr_set(const struct xattr_handler *handler,
822 struct dentry *dentry, struct inode *inode,
823 const char *name, const void *value,
824 size_t size, int flags)
825{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200826 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200827}
828
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200829static int ovl_other_xattr_get(const struct xattr_handler *handler,
830 struct dentry *dentry, struct inode *inode,
831 const char *name, void *buffer, size_t size)
832{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200833 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200834}
835
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200836static int ovl_other_xattr_set(const struct xattr_handler *handler,
837 struct dentry *dentry, struct inode *inode,
838 const char *name, const void *value,
839 size_t size, int flags)
840{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200841 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200842}
843
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200844static const struct xattr_handler __maybe_unused
845ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200846 .name = XATTR_NAME_POSIX_ACL_ACCESS,
847 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200848 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200849 .set = ovl_posix_acl_xattr_set,
850};
851
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200852static const struct xattr_handler __maybe_unused
853ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200854 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
855 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200856 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200857 .set = ovl_posix_acl_xattr_set,
858};
859
860static const struct xattr_handler ovl_own_xattr_handler = {
861 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200862 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200863 .set = ovl_own_xattr_set,
864};
865
866static const struct xattr_handler ovl_other_xattr_handler = {
867 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200868 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200869 .set = ovl_other_xattr_set,
870};
871
872static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200873#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200874 &ovl_posix_acl_access_xattr_handler,
875 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200876#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200877 &ovl_own_xattr_handler,
878 &ovl_other_xattr_handler,
879 NULL
880};
881
Miklos Szerediad204482017-11-10 09:39:16 +0100882static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100883{
Miklos Szeredi50649752017-11-10 09:39:15 +0100884 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100885 int err;
886
Miklos Szerediad204482017-11-10 09:39:16 +0100887 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100888 if (err)
889 goto out;
890
891 /* Upper fs should not be r/o */
892 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
893 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
894 err = -EINVAL;
895 goto out;
896 }
897
Miklos Szerediad204482017-11-10 09:39:16 +0100898 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100899 if (err)
900 goto out;
901
902 err = -EBUSY;
903 if (ovl_inuse_trylock(upperpath->dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +0100904 ofs->upperdir_locked = true;
905 } else if (ofs->config.index) {
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100906 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
907 goto out;
908 } else {
909 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
910 }
Miklos Szeredi50649752017-11-10 09:39:15 +0100911
912 upper_mnt = clone_private_mount(upperpath);
913 err = PTR_ERR(upper_mnt);
914 if (IS_ERR(upper_mnt)) {
915 pr_err("overlayfs: failed to clone upperpath\n");
916 goto out;
917 }
918
919 /* Don't inherit atime flags */
920 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szerediad204482017-11-10 09:39:16 +0100921 ofs->upper_mnt = upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100922 err = 0;
923out:
924 return err;
925}
926
Miklos Szerediad204482017-11-10 09:39:16 +0100927static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100928{
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200929 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100930 struct dentry *temp;
931 int err;
932
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200933 err = mnt_want_write(mnt);
934 if (err)
935 return err;
936
Miklos Szerediad204482017-11-10 09:39:16 +0100937 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
938 if (!ofs->workdir)
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200939 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100940
941 /*
942 * Upper should support d_type, else whiteouts are visible. Given
943 * workdir and upper are on same fs, we can do iterate_dir() on
944 * workdir. This check requires successful creation of workdir in
945 * previous step.
946 */
947 err = ovl_check_d_type_supported(workpath);
948 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200949 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100950
951 /*
952 * We allowed this configuration and don't want to break users over
953 * kernel upgrade. So warn instead of erroring out.
954 */
955 if (!err)
956 pr_warn("overlayfs: upper fs needs to support d_type.\n");
957
958 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +0100959 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
960 ofs->tmpfile = !IS_ERR(temp);
961 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100962 dput(temp);
963 else
964 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
965
966 /*
967 * Check if upper/work fs supports trusted.overlay.* xattr
968 */
Miklos Szerediad204482017-11-10 09:39:16 +0100969 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100970 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +0100971 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +0300972 ofs->config.index = false;
973 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off.\n");
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200974 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100975 } else {
Miklos Szerediad204482017-11-10 09:39:16 +0100976 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100977 }
978
979 /* Check if upper/work fs supports file handles */
Miklos Szerediad204482017-11-10 09:39:16 +0100980 if (ofs->config.index &&
981 !ovl_can_decode_fh(ofs->workdir->d_sb)) {
982 ofs->config.index = false;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100983 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
984 }
985
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200986out:
987 mnt_drop_write(mnt);
988 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100989}
990
Miklos Szerediad204482017-11-10 09:39:16 +0100991static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100992{
993 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +0100994 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100995
Miklos Szerediad204482017-11-10 09:39:16 +0100996 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100997 if (err)
998 goto out;
999
1000 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001001 if (upperpath->mnt != workpath.mnt) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001002 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1003 goto out;
1004 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001005 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001006 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1007 goto out;
1008 }
1009
1010 err = -EBUSY;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001011 if (ovl_inuse_trylock(workpath.dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001012 ofs->workdir_locked = true;
1013 } else if (ofs->config.index) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001014 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1015 goto out;
1016 } else {
1017 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1018 }
1019
Miklos Szerediad204482017-11-10 09:39:16 +01001020 ofs->workbasedir = dget(workpath.dentry);
1021 err = ovl_make_workdir(ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001022 if (err)
1023 goto out;
1024
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001025 err = 0;
1026out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001027 path_put(&workpath);
1028
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001029 return err;
1030}
1031
Miklos Szerediad204482017-11-10 09:39:16 +01001032static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001033 struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001034{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001035 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001036 int err;
1037
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001038 err = mnt_want_write(mnt);
1039 if (err)
1040 return err;
1041
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001042 /* Verify lower root is upper root origin */
Amir Goldsteind9768072017-09-24 13:00:19 +03001043 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001044 false, true);
1045 if (err) {
1046 pr_err("overlayfs: failed to verify upper root origin\n");
1047 goto out;
1048 }
1049
Miklos Szerediad204482017-11-10 09:39:16 +01001050 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1051 if (ofs->indexdir) {
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001052 /* Verify upper root is index dir origin */
Amir Goldsteind9768072017-09-24 13:00:19 +03001053 err = ovl_verify_origin(ofs->indexdir, upperpath->dentry,
1054 true, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001055 if (err)
1056 pr_err("overlayfs: failed to verify index dir origin\n");
1057
1058 /* Cleanup bad/stale/orphan index entries */
1059 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001060 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001061 }
Miklos Szerediad204482017-11-10 09:39:16 +01001062 if (err || !ofs->indexdir)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001063 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1064
1065out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001066 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001067 return err;
1068}
1069
Miklos Szerediad204482017-11-10 09:39:16 +01001070static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack,
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001071 unsigned int numlower)
1072{
1073 int err;
1074 unsigned int i;
1075
1076 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001077 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001078 GFP_KERNEL);
Miklos Szerediad204482017-11-10 09:39:16 +01001079 if (ofs->lower_layers == NULL)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001080 goto out;
1081 for (i = 0; i < numlower; i++) {
1082 struct vfsmount *mnt;
1083 dev_t dev;
1084
1085 err = get_anon_bdev(&dev);
1086 if (err) {
1087 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1088 goto out;
1089 }
1090
1091 mnt = clone_private_mount(&stack[i]);
1092 err = PTR_ERR(mnt);
1093 if (IS_ERR(mnt)) {
1094 pr_err("overlayfs: failed to clone lowerpath\n");
1095 free_anon_bdev(dev);
1096 goto out;
1097 }
1098 /*
1099 * Make lower layers R/O. That way fchmod/fchown on lower file
1100 * will fail instead of modifying lower fs.
1101 */
1102 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1103
Miklos Szerediad204482017-11-10 09:39:16 +01001104 ofs->lower_layers[ofs->numlower].mnt = mnt;
1105 ofs->lower_layers[ofs->numlower].pseudo_dev = dev;
Amir Goldsteind583ed72017-11-08 19:23:36 +02001106 ofs->lower_layers[ofs->numlower].idx = i + 1;
Miklos Szerediad204482017-11-10 09:39:16 +01001107 ofs->numlower++;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001108
1109 /* Check if all lower layers are on same sb */
1110 if (i == 0)
Miklos Szerediad204482017-11-10 09:39:16 +01001111 ofs->same_sb = mnt->mnt_sb;
1112 else if (ofs->same_sb != mnt->mnt_sb)
1113 ofs->same_sb = NULL;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001114 }
1115 err = 0;
1116out:
1117 return err;
1118}
1119
Miklos Szeredi4155c102017-11-10 09:39:15 +01001120static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szerediad204482017-11-10 09:39:16 +01001121 struct ovl_fs *ofs)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001122{
1123 int err;
1124 char *lowertmp, *lower;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001125 struct path *stack = NULL;
1126 unsigned int stacklen, numlower = 0, i;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001127 bool remote = false;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001128 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001129
1130 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001131 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001132 if (!lowertmp)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001133 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001134
1135 err = -EINVAL;
1136 stacklen = ovl_split_lowerdirs(lowertmp);
1137 if (stacklen > OVL_MAX_STACK) {
1138 pr_err("overlayfs: too many lower directories, limit is %d\n",
1139 OVL_MAX_STACK);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001140 goto out_err;
Miklos Szerediad204482017-11-10 09:39:16 +01001141 } else if (!ofs->config.upperdir && stacklen == 1) {
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001142 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001143 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001144 }
1145
1146 err = -ENOMEM;
1147 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1148 if (!stack)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001149 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001150
1151 err = -EINVAL;
1152 lower = lowertmp;
1153 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szerediad204482017-11-10 09:39:16 +01001154 err = ovl_lower_dir(lower, &stack[numlower], ofs,
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001155 &sb->s_stack_depth, &remote);
1156 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001157 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001158
1159 lower = strchr(lower, '\0') + 1;
1160 }
1161
1162 err = -EINVAL;
1163 sb->s_stack_depth++;
1164 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1165 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001166 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001167 }
1168
Miklos Szerediad204482017-11-10 09:39:16 +01001169 err = ovl_get_lower_layers(ofs, stack, numlower);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001170 if (err)
1171 goto out_err;
1172
1173 err = -ENOMEM;
1174 oe = ovl_alloc_entry(numlower);
1175 if (!oe)
1176 goto out_err;
1177
1178 for (i = 0; i < numlower; i++) {
1179 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Miklos Szerediad204482017-11-10 09:39:16 +01001180 oe->lowerstack[i].layer = &ofs->lower_layers[i];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001181 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001182
1183 if (remote)
1184 sb->s_d_op = &ovl_reval_dentry_operations;
1185 else
1186 sb->s_d_op = &ovl_dentry_operations;
1187
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001188out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001189 for (i = 0; i < numlower; i++)
1190 path_put(&stack[i]);
1191 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001192 kfree(lowertmp);
1193
1194 return oe;
1195
1196out_err:
1197 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001198 goto out;
1199}
1200
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001201static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1202{
Kees Cook33006cd2017-03-29 14:02:19 -07001203 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001204 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001205 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001206 struct ovl_fs *ofs;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001207 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001208 int err;
1209
Erez Zadokf45827e82014-10-24 00:14:38 +02001210 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001211 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1212 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001213 goto out;
1214
Miklos Szerediad204482017-11-10 09:39:16 +01001215 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001216 if (!cred)
1217 goto out_err;
1218
Miklos Szerediad204482017-11-10 09:39:16 +01001219 ofs->config.index = ovl_index_def;
1220 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001221 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001222 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001223
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001224 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001225 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001226 if (!silent)
1227 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001228 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001229 }
1230
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001231 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001232 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szerediad204482017-11-10 09:39:16 +01001233 if (ofs->config.upperdir) {
1234 if (!ofs->config.workdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001235 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001236 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001237 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001238
Miklos Szerediad204482017-11-10 09:39:16 +01001239 err = ovl_get_upper(ofs, &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001240 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001241 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001242
Miklos Szerediad204482017-11-10 09:39:16 +01001243 err = ovl_get_workdir(ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001244 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001245 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001246
Miklos Szerediad204482017-11-10 09:39:16 +01001247 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001248 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001249
Miklos Szerediad204482017-11-10 09:39:16 +01001250 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1251 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001252
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001253 }
Miklos Szerediad204482017-11-10 09:39:16 +01001254 oe = ovl_get_lowerstack(sb, ofs);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001255 err = PTR_ERR(oe);
1256 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001257 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001258
hujianyang71cbad72015-01-15 13:20:57 +08001259 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szerediad204482017-11-10 09:39:16 +01001260 if (!ofs->upper_mnt)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001261 sb->s_flags |= SB_RDONLY;
Miklos Szerediad204482017-11-10 09:39:16 +01001262 else if (ofs->upper_mnt->mnt_sb != ofs->same_sb)
1263 ofs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001264
Miklos Szerediad204482017-11-10 09:39:16 +01001265 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1266 err = ovl_get_indexdir(ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001267 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001268 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001269
Amir Goldstein972d0092017-09-19 12:14:18 +03001270 /* Force r/o mount with no index dir */
1271 if (!ofs->indexdir) {
1272 dput(ofs->workdir);
1273 ofs->workdir = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001274 sb->s_flags |= SB_RDONLY;
Amir Goldstein972d0092017-09-19 12:14:18 +03001275 }
1276
Amir Goldstein02bcd152017-06-21 15:28:36 +03001277 }
1278
Amir Goldstein972d0092017-09-19 12:14:18 +03001279 /* Show index=off in /proc/mounts for forced r/o mount */
Miklos Szerediad204482017-11-10 09:39:16 +01001280 if (!ofs->indexdir)
1281 ofs->config.index = false;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001282
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001283 /* Never override disk quota limits or use reserved space */
1284 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1285
Vivek Goyal655042c2016-10-14 03:03:36 +02001286 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1287 sb->s_op = &ovl_super_operations;
1288 sb->s_xattr = ovl_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01001289 sb->s_fs_info = ofs;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001290 sb->s_flags |= SB_POSIXACL | SB_NOREMOTELOCK;
Vivek Goyal655042c2016-10-14 03:03:36 +02001291
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001292 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001293 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001294 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001295 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001296
1297 mntput(upperpath.mnt);
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001298 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001299 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001300 if (ovl_is_impuredir(upperpath.dentry))
1301 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001302 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001303
1304 root_dentry->d_fsdata = oe;
1305
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001306 /* Root is always merge -> can have whiteouts */
1307 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001308 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1309 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001310
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001311 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001312
1313 return 0;
1314
Miklos Szeredi4155c102017-11-10 09:39:15 +01001315out_free_oe:
1316 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001317 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001318out_err:
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001319 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01001320 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001321out:
1322 return err;
1323}
1324
1325static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1326 const char *dev_name, void *raw_data)
1327{
1328 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1329}
1330
1331static struct file_system_type ovl_fs_type = {
1332 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001333 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001334 .mount = ovl_mount,
1335 .kill_sb = kill_anon_super,
1336};
Miklos Szeredief94b182014-11-20 16:39:59 +01001337MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001338
Amir Goldstein13cf1992017-06-12 09:54:40 +03001339static void ovl_inode_init_once(void *foo)
1340{
1341 struct ovl_inode *oi = foo;
1342
1343 inode_init_once(&oi->vfs_inode);
1344}
1345
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001346static int __init ovl_init(void)
1347{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001348 int err;
1349
1350 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1351 sizeof(struct ovl_inode), 0,
1352 (SLAB_RECLAIM_ACCOUNT|
1353 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1354 ovl_inode_init_once);
1355 if (ovl_inode_cachep == NULL)
1356 return -ENOMEM;
1357
1358 err = register_filesystem(&ovl_fs_type);
1359 if (err)
1360 kmem_cache_destroy(ovl_inode_cachep);
1361
1362 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001363}
1364
1365static void __exit ovl_exit(void)
1366{
1367 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001368
1369 /*
1370 * Make sure all delayed rcu free inodes are flushed before we
1371 * destroy cache.
1372 */
1373 rcu_barrier();
1374 kmem_cache_destroy(ovl_inode_cachep);
1375
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001376}
1377
1378module_init(ovl_init);
1379module_exit(ovl_exit);