blob: b368e2e102fa15e19fad75823e8056250543f5fd [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002/*
3 *
4 * Copyright (C) 2011 Novell Inc.
Miklos Szeredie9be9d52014-10-24 00:14:38 +02005 */
6
Ingo Molnar5b825c32017-02-02 17:54:15 +01007#include <uapi/linux/magic.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +02008#include <linux/fs.h>
9#include <linux/namei.h>
10#include <linux/xattr.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011#include <linux/mount.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020012#include <linux/parser.h>
13#include <linux/module.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020014#include <linux/statfs.h>
Erez Zadokf45827e82014-10-24 00:14:38 +020015#include <linux/seq_file.h>
Miklos Szeredid837a492016-07-29 12:05:24 +020016#include <linux/posix_acl_xattr.h>
Amir Goldsteine487d882017-11-07 13:55:04 +020017#include <linux/exportfs.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020018#include "overlayfs.h"
19
20MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
21MODULE_DESCRIPTION("Overlay filesystem");
22MODULE_LICENSE("GPL");
23
Miklos Szeredie9be9d52014-10-24 00:14:38 +020024
25struct ovl_dir_cache;
26
Miklos Szeredia78d9f02014-12-13 00:59:52 +010027#define OVL_MAX_STACK 500
28
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010029static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
30module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020031MODULE_PARM_DESC(redirect_dir,
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010032 "Default to on or off for the redirect_dir feature");
Miklos Szeredie9be9d52014-10-24 00:14:38 +020033
Miklos Szeredi438c84c2017-12-11 11:28:10 +010034static bool ovl_redirect_always_follow =
35 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
36module_param_named(redirect_always_follow, ovl_redirect_always_follow,
37 bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020038MODULE_PARM_DESC(redirect_always_follow,
Miklos Szeredi438c84c2017-12-11 11:28:10 +010039 "Follow redirects even if redirect_dir feature is turned off");
40
Amir Goldstein02bcd152017-06-21 15:28:36 +030041static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
42module_param_named(index, ovl_index_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020043MODULE_PARM_DESC(index,
Amir Goldstein02bcd152017-06-21 15:28:36 +030044 "Default to on or off for the inodes index feature");
45
Amir Goldsteinf168f102018-01-19 11:26:53 +020046static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
47module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020048MODULE_PARM_DESC(nfs_export,
Amir Goldsteinf168f102018-01-19 11:26:53 +020049 "Default to on or off for the NFS export feature");
50
Amir Goldstein795939a2018-03-29 09:08:18 +030051static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
52module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020053MODULE_PARM_DESC(xino_auto,
Amir Goldstein795939a2018-03-29 09:08:18 +030054 "Auto enable xino feature");
55
Miklos Szeredi4155c102017-11-10 09:39:15 +010056static void ovl_entry_stack_free(struct ovl_entry *oe)
57{
58 unsigned int i;
59
60 for (i = 0; i < oe->numlower; i++)
61 dput(oe->lowerstack[i].dentry);
62}
63
Vivek Goyald5791042018-05-11 11:49:27 -040064static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
65module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020066MODULE_PARM_DESC(metacopy,
Vivek Goyald5791042018-05-11 11:49:27 -040067 "Default to on or off for the metadata only copy up feature");
68
Miklos Szeredie9be9d52014-10-24 00:14:38 +020069static void ovl_dentry_release(struct dentry *dentry)
70{
71 struct ovl_entry *oe = dentry->d_fsdata;
72
73 if (oe) {
Miklos Szeredi4155c102017-11-10 09:39:15 +010074 ovl_entry_stack_free(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020075 kfree_rcu(oe, rcu);
76 }
77}
78
Miklos Szeredi2d902672016-06-30 08:53:27 +020079static struct dentry *ovl_d_real(struct dentry *dentry,
Miklos Szeredifb160432018-07-18 15:44:44 +020080 const struct inode *inode)
Miklos Szeredid101a122016-03-26 16:14:37 -040081{
82 struct dentry *real;
83
Miklos Szeredie8c985b2018-07-18 15:44:41 +020084 /* It's an overlay file */
85 if (inode && d_inode(dentry) == inode)
86 return dentry;
87
Miklos Szeredica4c8a32016-12-16 11:02:55 +010088 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040089 if (!inode || inode == d_inode(dentry))
90 return dentry;
91 goto bug;
92 }
93
94 real = ovl_dentry_upper(dentry);
Vivek Goyal2c3d7352018-05-11 11:49:31 -040095 if (real && (inode == d_inode(real)))
Miklos Szeredid101a122016-03-26 16:14:37 -040096 return real;
97
Vivek Goyal2c3d7352018-05-11 11:49:31 -040098 if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
99 return real;
100
101 real = ovl_dentry_lowerdata(dentry);
Miklos Szeredid101a122016-03-26 16:14:37 -0400102 if (!real)
103 goto bug;
104
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100105 /* Handle recursion */
Miklos Szeredifb160432018-07-18 15:44:44 +0200106 real = d_real(real, inode);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100107
Miklos Szeredid101a122016-03-26 16:14:37 -0400108 if (!inode || inode == d_inode(real))
109 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400110bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200111 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400112 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
113 return dentry;
114}
115
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200116static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
117{
118 struct ovl_entry *oe = dentry->d_fsdata;
119 unsigned int i;
120 int ret = 1;
121
122 for (i = 0; i < oe->numlower; i++) {
123 struct dentry *d = oe->lowerstack[i].dentry;
124
125 if (d->d_flags & DCACHE_OP_REVALIDATE) {
126 ret = d->d_op->d_revalidate(d, flags);
127 if (ret < 0)
128 return ret;
129 if (!ret) {
130 if (!(flags & LOOKUP_RCU))
131 d_invalidate(d);
132 return -ESTALE;
133 }
134 }
135 }
136 return 1;
137}
138
139static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
140{
141 struct ovl_entry *oe = dentry->d_fsdata;
142 unsigned int i;
143 int ret = 1;
144
145 for (i = 0; i < oe->numlower; i++) {
146 struct dentry *d = oe->lowerstack[i].dentry;
147
148 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
149 ret = d->d_op->d_weak_revalidate(d, flags);
150 if (ret <= 0)
151 break;
152 }
153 }
154 return ret;
155}
156
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200157static const struct dentry_operations ovl_dentry_operations = {
158 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400159 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200160};
161
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200162static const struct dentry_operations ovl_reval_dentry_operations = {
163 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400164 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200165 .d_revalidate = ovl_dentry_revalidate,
166 .d_weak_revalidate = ovl_dentry_weak_revalidate,
167};
168
Amir Goldstein13cf1992017-06-12 09:54:40 +0300169static struct kmem_cache *ovl_inode_cachep;
170
171static struct inode *ovl_alloc_inode(struct super_block *sb)
172{
173 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
174
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900175 if (!oi)
176 return NULL;
177
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200178 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200179 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200180 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200181 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200182 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200183 oi->lower = NULL;
Vivek Goyal2664bd02018-05-11 11:49:30 -0400184 oi->lowerdata = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300185 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200186
Amir Goldstein13cf1992017-06-12 09:54:40 +0300187 return &oi->vfs_inode;
188}
189
Al Viro0b269de2019-04-15 22:52:17 -0400190static void ovl_free_inode(struct inode *inode)
Amir Goldstein13cf1992017-06-12 09:54:40 +0300191{
Al Viro0b269de2019-04-15 22:52:17 -0400192 struct ovl_inode *oi = OVL_I(inode);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300193
Al Viro0b269de2019-04-15 22:52:17 -0400194 kfree(oi->redirect);
195 mutex_destroy(&oi->lock);
196 kmem_cache_free(ovl_inode_cachep, oi);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300197}
198
199static void ovl_destroy_inode(struct inode *inode)
200{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200201 struct ovl_inode *oi = OVL_I(inode);
202
203 dput(oi->__upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200204 iput(oi->lower);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400205 if (S_ISDIR(inode->i_mode))
206 ovl_dir_cache_free(inode);
207 else
208 iput(oi->lowerdata);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300209}
210
Miklos Szerediad204482017-11-10 09:39:16 +0100211static void ovl_free_fs(struct ovl_fs *ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200212{
Miklos Szeredidd662662014-12-13 00:59:43 +0100213 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200214
Amir Goldstein146d62e2019-04-18 17:42:08 +0300215 iput(ofs->indexdir_trap);
216 iput(ofs->workdir_trap);
217 iput(ofs->upperdir_trap);
Miklos Szerediad204482017-11-10 09:39:16 +0100218 dput(ofs->indexdir);
219 dput(ofs->workdir);
220 if (ofs->workdir_locked)
221 ovl_inuse_unlock(ofs->workbasedir);
222 dput(ofs->workbasedir);
223 if (ofs->upperdir_locked)
224 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
225 mntput(ofs->upper_mnt);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300226 for (i = 0; i < ofs->numlower; i++) {
227 iput(ofs->lower_layers[i].trap);
Miklos Szerediad204482017-11-10 09:39:16 +0100228 mntput(ofs->lower_layers[i].mnt);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300229 }
Amir Goldstein51486262018-03-28 20:22:41 +0300230 for (i = 0; i < ofs->numlowerfs; i++)
231 free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
Miklos Szerediad204482017-11-10 09:39:16 +0100232 kfree(ofs->lower_layers);
Amir Goldstein51486262018-03-28 20:22:41 +0300233 kfree(ofs->lower_fs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200234
Miklos Szerediad204482017-11-10 09:39:16 +0100235 kfree(ofs->config.lowerdir);
236 kfree(ofs->config.upperdir);
237 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100238 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100239 if (ofs->creator_cred)
240 put_cred(ofs->creator_cred);
241 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242}
243
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100244static void ovl_put_super(struct super_block *sb)
245{
246 struct ovl_fs *ofs = sb->s_fs_info;
247
248 ovl_free_fs(ofs);
249}
250
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800251/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200252static int ovl_sync_fs(struct super_block *sb, int wait)
253{
Miklos Szerediad204482017-11-10 09:39:16 +0100254 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200255 struct super_block *upper_sb;
256 int ret;
257
Miklos Szerediad204482017-11-10 09:39:16 +0100258 if (!ofs->upper_mnt)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200259 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800260
261 /*
262 * If this is a sync(2) call or an emergency sync, all the super blocks
263 * will be iterated, including upper_sb, so no need to do anything.
264 *
265 * If this is a syncfs(2) call, then we do need to call
266 * sync_filesystem() on upper_sb, but enough if we do it when being
267 * called with wait == 1.
268 */
269 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200270 return 0;
271
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800272 upper_sb = ofs->upper_mnt->mnt_sb;
273
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200274 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800275 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200276 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800277
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200278 return ret;
279}
280
Andy Whitcroftcc259632014-10-24 00:14:38 +0200281/**
282 * ovl_statfs
283 * @sb: The overlayfs super block
284 * @buf: The struct kstatfs to fill in with stats
285 *
286 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100287 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200288 */
289static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
290{
291 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
292 struct dentry *root_dentry = dentry->d_sb->s_root;
293 struct path path;
294 int err;
295
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100296 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200297
298 err = vfs_statfs(&path, buf);
299 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100300 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200301 buf->f_type = OVERLAYFS_SUPER_MAGIC;
302 }
303
304 return err;
305}
306
Amir Goldstein02bcd152017-06-21 15:28:36 +0300307/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100308static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300309{
Miklos Szerediad204482017-11-10 09:39:16 +0100310 return (!ofs->upper_mnt || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300311}
312
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100313static const char *ovl_redirect_mode_def(void)
314{
315 return ovl_redirect_dir_def ? "on" : "off";
316}
317
Amir Goldstein795939a2018-03-29 09:08:18 +0300318enum {
319 OVL_XINO_OFF,
320 OVL_XINO_AUTO,
321 OVL_XINO_ON,
322};
323
324static const char * const ovl_xino_str[] = {
325 "off",
326 "auto",
327 "on",
328};
329
330static inline int ovl_xino_def(void)
331{
332 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
333}
334
Erez Zadokf45827e82014-10-24 00:14:38 +0200335/**
336 * ovl_show_options
337 *
338 * Prints the mount options for a given superblock.
339 * Returns zero; does not fail.
340 */
341static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
342{
343 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100344 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200345
Miklos Szerediad204482017-11-10 09:39:16 +0100346 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
347 if (ofs->config.upperdir) {
348 seq_show_option(m, "upperdir", ofs->config.upperdir);
349 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100350 }
Miklos Szerediad204482017-11-10 09:39:16 +0100351 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200352 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100353 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
354 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100355 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100356 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Amir Goldsteinf168f102018-01-19 11:26:53 +0200357 if (ofs->config.nfs_export != ovl_nfs_export_def)
358 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
359 "on" : "off");
Amir Goldstein795939a2018-03-29 09:08:18 +0300360 if (ofs->config.xino != ovl_xino_def())
361 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
Vivek Goyald5791042018-05-11 11:49:27 -0400362 if (ofs->config.metacopy != ovl_metacopy_def)
363 seq_printf(m, ",metacopy=%s",
364 ofs->config.metacopy ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200365 return 0;
366}
367
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900368static int ovl_remount(struct super_block *sb, int *flags, char *data)
369{
Miklos Szerediad204482017-11-10 09:39:16 +0100370 struct ovl_fs *ofs = sb->s_fs_info;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900371
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800372 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900373 return -EROFS;
374
375 return 0;
376}
377
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200378static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300379 .alloc_inode = ovl_alloc_inode,
Al Viro0b269de2019-04-15 22:52:17 -0400380 .free_inode = ovl_free_inode,
Amir Goldstein13cf1992017-06-12 09:54:40 +0300381 .destroy_inode = ovl_destroy_inode,
382 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200383 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200384 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200385 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200386 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900387 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200388};
389
390enum {
391 OPT_LOWERDIR,
392 OPT_UPPERDIR,
393 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200394 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100395 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300396 OPT_INDEX_ON,
397 OPT_INDEX_OFF,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200398 OPT_NFS_EXPORT_ON,
399 OPT_NFS_EXPORT_OFF,
Amir Goldstein795939a2018-03-29 09:08:18 +0300400 OPT_XINO_ON,
401 OPT_XINO_OFF,
402 OPT_XINO_AUTO,
Vivek Goyald5791042018-05-11 11:49:27 -0400403 OPT_METACOPY_ON,
404 OPT_METACOPY_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200405 OPT_ERR,
406};
407
408static const match_table_t ovl_tokens = {
409 {OPT_LOWERDIR, "lowerdir=%s"},
410 {OPT_UPPERDIR, "upperdir=%s"},
411 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200412 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100413 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300414 {OPT_INDEX_ON, "index=on"},
415 {OPT_INDEX_OFF, "index=off"},
Amir Goldsteinf168f102018-01-19 11:26:53 +0200416 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
417 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
Amir Goldstein795939a2018-03-29 09:08:18 +0300418 {OPT_XINO_ON, "xino=on"},
419 {OPT_XINO_OFF, "xino=off"},
420 {OPT_XINO_AUTO, "xino=auto"},
Vivek Goyald5791042018-05-11 11:49:27 -0400421 {OPT_METACOPY_ON, "metacopy=on"},
422 {OPT_METACOPY_OFF, "metacopy=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200423 {OPT_ERR, NULL}
424};
425
Miklos Szeredi91c77942014-11-20 16:40:00 +0100426static char *ovl_next_opt(char **s)
427{
428 char *sbegin = *s;
429 char *p;
430
431 if (sbegin == NULL)
432 return NULL;
433
434 for (p = sbegin; *p; p++) {
435 if (*p == '\\') {
436 p++;
437 if (!*p)
438 break;
439 } else if (*p == ',') {
440 *p = '\0';
441 *s = p + 1;
442 return sbegin;
443 }
444 }
445 *s = NULL;
446 return sbegin;
447}
448
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100449static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
450{
451 if (strcmp(mode, "on") == 0) {
452 config->redirect_dir = true;
453 /*
454 * Does not make sense to have redirect creation without
455 * redirect following.
456 */
457 config->redirect_follow = true;
458 } else if (strcmp(mode, "follow") == 0) {
459 config->redirect_follow = true;
460 } else if (strcmp(mode, "off") == 0) {
461 if (ovl_redirect_always_follow)
462 config->redirect_follow = true;
463 } else if (strcmp(mode, "nofollow") != 0) {
464 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
465 mode);
466 return -EINVAL;
467 }
468
469 return 0;
470}
471
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200472static int ovl_parse_opt(char *opt, struct ovl_config *config)
473{
474 char *p;
Vivek Goyald5791042018-05-11 11:49:27 -0400475 int err;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100476 bool metacopy_opt = false, redirect_opt = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200477
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100478 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
479 if (!config->redirect_mode)
480 return -ENOMEM;
481
Miklos Szeredi91c77942014-11-20 16:40:00 +0100482 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200483 int token;
484 substring_t args[MAX_OPT_ARGS];
485
486 if (!*p)
487 continue;
488
489 token = match_token(p, ovl_tokens, args);
490 switch (token) {
491 case OPT_UPPERDIR:
492 kfree(config->upperdir);
493 config->upperdir = match_strdup(&args[0]);
494 if (!config->upperdir)
495 return -ENOMEM;
496 break;
497
498 case OPT_LOWERDIR:
499 kfree(config->lowerdir);
500 config->lowerdir = match_strdup(&args[0]);
501 if (!config->lowerdir)
502 return -ENOMEM;
503 break;
504
505 case OPT_WORKDIR:
506 kfree(config->workdir);
507 config->workdir = match_strdup(&args[0]);
508 if (!config->workdir)
509 return -ENOMEM;
510 break;
511
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200512 case OPT_DEFAULT_PERMISSIONS:
513 config->default_permissions = true;
514 break;
515
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100516 case OPT_REDIRECT_DIR:
517 kfree(config->redirect_mode);
518 config->redirect_mode = match_strdup(&args[0]);
519 if (!config->redirect_mode)
520 return -ENOMEM;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100521 redirect_opt = true;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100522 break;
523
Amir Goldstein02bcd152017-06-21 15:28:36 +0300524 case OPT_INDEX_ON:
525 config->index = true;
526 break;
527
528 case OPT_INDEX_OFF:
529 config->index = false;
530 break;
531
Amir Goldsteinf168f102018-01-19 11:26:53 +0200532 case OPT_NFS_EXPORT_ON:
533 config->nfs_export = true;
534 break;
535
536 case OPT_NFS_EXPORT_OFF:
537 config->nfs_export = false;
538 break;
539
Amir Goldstein795939a2018-03-29 09:08:18 +0300540 case OPT_XINO_ON:
541 config->xino = OVL_XINO_ON;
542 break;
543
544 case OPT_XINO_OFF:
545 config->xino = OVL_XINO_OFF;
546 break;
547
548 case OPT_XINO_AUTO:
549 config->xino = OVL_XINO_AUTO;
550 break;
551
Vivek Goyald5791042018-05-11 11:49:27 -0400552 case OPT_METACOPY_ON:
553 config->metacopy = true;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100554 metacopy_opt = true;
Vivek Goyald5791042018-05-11 11:49:27 -0400555 break;
556
557 case OPT_METACOPY_OFF:
558 config->metacopy = false;
559 break;
560
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200561 default:
hujianyangbead55e2015-01-15 13:17:36 +0800562 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200563 return -EINVAL;
564 }
565 }
hujianyang71cbad72015-01-15 13:20:57 +0800566
567 /* Workdir is useless in non-upper mount */
568 if (!config->upperdir && config->workdir) {
569 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
570 config->workdir);
571 kfree(config->workdir);
572 config->workdir = NULL;
573 }
574
Vivek Goyald5791042018-05-11 11:49:27 -0400575 err = ovl_parse_redirect_mode(config, config->redirect_mode);
576 if (err)
577 return err;
578
Miklos Szeredid47748e2018-11-01 21:31:39 +0100579 /*
580 * This is to make the logic below simpler. It doesn't make any other
581 * difference, since config->redirect_dir is only used for upper.
582 */
583 if (!config->upperdir && config->redirect_follow)
584 config->redirect_dir = true;
585
586 /* Resolve metacopy -> redirect_dir dependency */
587 if (config->metacopy && !config->redirect_dir) {
588 if (metacopy_opt && redirect_opt) {
589 pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n",
590 config->redirect_mode);
591 return -EINVAL;
592 }
593 if (redirect_opt) {
594 /*
595 * There was an explicit redirect_dir=... that resulted
596 * in this conflict.
597 */
598 pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n",
599 config->redirect_mode);
600 config->metacopy = false;
601 } else {
602 /* Automatically enable redirect otherwise. */
603 config->redirect_follow = config->redirect_dir = true;
604 }
Vivek Goyald5791042018-05-11 11:49:27 -0400605 }
606
607 return 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200608}
609
610#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300611#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200612
Miklos Szerediad204482017-11-10 09:39:16 +0100613static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300614 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200615{
Miklos Szerediad204482017-11-10 09:39:16 +0100616 struct inode *dir = ofs->workbasedir->d_inode;
617 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200618 struct dentry *work;
619 int err;
620 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300621 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200622
Al Viro59551022016-01-22 15:40:57 -0500623 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300624 locked = true;
625
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200626retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100627 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200628
629 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200630 struct iattr attr = {
631 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000632 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200633 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200634
635 if (work->d_inode) {
636 err = -EEXIST;
637 if (retried)
638 goto out_dput;
639
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300640 if (persist)
641 goto out_unlock;
642
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200643 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200644 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200645 dput(work);
646 goto retry;
647 }
648
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300649 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
650 err = PTR_ERR(work);
651 if (IS_ERR(work))
652 goto out_err;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200653
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200654 /*
655 * Try to remove POSIX ACL xattrs from workdir. We are good if:
656 *
657 * a) success (there was a POSIX ACL xattr and was removed)
658 * b) -ENODATA (there was no POSIX ACL xattr)
659 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
660 *
661 * There are various other error values that could effectively
662 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
663 * if the xattr name is too long), but the set of filesystems
664 * allowed as upper are limited to "normal" ones, where checking
665 * for the above two errors is sufficient.
666 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200667 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200668 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200669 goto out_dput;
670
671 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200672 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200673 goto out_dput;
674
675 /* Clear any inherited mode bits */
676 inode_lock(work->d_inode);
677 err = notify_change(work, &attr, NULL);
678 inode_unlock(work->d_inode);
679 if (err)
680 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300681 } else {
682 err = PTR_ERR(work);
683 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200684 }
685out_unlock:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300686 if (locked)
687 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688
689 return work;
690
691out_dput:
692 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300693out_err:
694 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100695 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300696 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200697 goto out_unlock;
698}
699
Miklos Szeredi91c77942014-11-20 16:40:00 +0100700static void ovl_unescape(char *s)
701{
702 char *d = s;
703
704 for (;; s++, d++) {
705 if (*s == '\\')
706 s++;
707 *d = *s;
708 if (!*s)
709 break;
710 }
711}
712
Miklos Szerediab508822014-12-13 00:59:49 +0100713static int ovl_mount_dir_noesc(const char *name, struct path *path)
714{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100715 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100716
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100717 if (!*name) {
718 pr_err("overlayfs: empty lowerdir\n");
719 goto out;
720 }
Miklos Szerediab508822014-12-13 00:59:49 +0100721 err = kern_path(name, LOOKUP_FOLLOW, path);
722 if (err) {
723 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
724 goto out;
725 }
726 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200727 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100728 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
729 goto out_put;
730 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100731 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100732 pr_err("overlayfs: '%s' not a directory\n", name);
733 goto out_put;
734 }
735 return 0;
736
737out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100738 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100739out:
740 return err;
741}
742
743static int ovl_mount_dir(const char *name, struct path *path)
744{
745 int err = -ENOMEM;
746 char *tmp = kstrdup(name, GFP_KERNEL);
747
748 if (tmp) {
749 ovl_unescape(tmp);
750 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200751
752 if (!err)
753 if (ovl_dentry_remote(path->dentry)) {
754 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
755 tmp);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100756 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200757 err = -EINVAL;
758 }
Miklos Szerediab508822014-12-13 00:59:49 +0100759 kfree(tmp);
760 }
761 return err;
762}
763
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100764static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
765 const char *name)
766{
767 struct kstatfs statfs;
768 int err = vfs_statfs(path, &statfs);
769
770 if (err)
771 pr_err("overlayfs: statfs failed on '%s'\n", name);
772 else
773 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
774
775 return err;
776}
777
778static int ovl_lower_dir(const char *name, struct path *path,
779 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100780{
Amir Goldsteine487d882017-11-07 13:55:04 +0200781 int fh_type;
Miklos Szerediab508822014-12-13 00:59:49 +0100782 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100783
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100784 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100785 if (err)
786 goto out;
787
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100788 err = ovl_check_namelen(path, ofs, name);
789 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100790 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100791
Miklos Szerediab508822014-12-13 00:59:49 +0100792 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
793
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200794 if (ovl_dentry_remote(path->dentry))
795 *remote = true;
796
Amir Goldstein02bcd152017-06-21 15:28:36 +0300797 /*
Amir Goldsteinf168f102018-01-19 11:26:53 +0200798 * The inodes index feature and NFS export need to encode and decode
799 * file handles, so they require that all layers support them.
Amir Goldstein02bcd152017-06-21 15:28:36 +0300800 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200801 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
Amir Goldsteinf168f102018-01-19 11:26:53 +0200802 if ((ofs->config.nfs_export ||
Amir Goldsteine487d882017-11-07 13:55:04 +0200803 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300804 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200805 ofs->config.nfs_export = false;
806 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
807 name);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300808 }
809
Amir Goldsteine487d882017-11-07 13:55:04 +0200810 /* Check if lower fs has 32bit inode numbers */
811 if (fh_type != FILEID_INO32_GEN)
812 ofs->xino_bits = 0;
813
Miklos Szerediab508822014-12-13 00:59:49 +0100814 return 0;
815
816out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100817 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100818out:
819 return err;
820}
821
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200822/* Workdir should not be subdir of upperdir and vice versa */
823static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
824{
825 bool ok = false;
826
827 if (workdir != upperdir) {
828 ok = (lock_rename(workdir, upperdir) == NULL);
829 unlock_rename(workdir, upperdir);
830 }
831 return ok;
832}
833
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100834static unsigned int ovl_split_lowerdirs(char *str)
835{
836 unsigned int ctr = 1;
837 char *s, *d;
838
839 for (s = d = str;; s++, d++) {
840 if (*s == '\\') {
841 s++;
842 } else if (*s == ':') {
843 *d = '\0';
844 ctr++;
845 continue;
846 }
847 *d = *s;
848 if (!*s)
849 break;
850 }
851 return ctr;
852}
853
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200854static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200855ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
856 struct dentry *dentry, struct inode *inode,
857 const char *name, void *buffer, size_t size)
858{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200859 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200860}
861
862static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200863ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
864 struct dentry *dentry, struct inode *inode,
865 const char *name, const void *value,
866 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200867{
868 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200869 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200870 struct posix_acl *acl = NULL;
871 int err;
872
873 /* Check that everything is OK before copy-up */
874 if (value) {
875 acl = posix_acl_from_xattr(&init_user_ns, value, size);
876 if (IS_ERR(acl))
877 return PTR_ERR(acl);
878 }
879 err = -EOPNOTSUPP;
880 if (!IS_POSIXACL(d_inode(workdir)))
881 goto out_acl_release;
882 if (!realinode->i_op->set_acl)
883 goto out_acl_release;
884 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
885 err = acl ? -EACCES : 0;
886 goto out_acl_release;
887 }
888 err = -EPERM;
889 if (!inode_owner_or_capable(inode))
890 goto out_acl_release;
891
892 posix_acl_release(acl);
893
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100894 /*
895 * Check if sgid bit needs to be cleared (actual setacl operation will
896 * be done with mounter's capabilities and so that won't do it for us).
897 */
898 if (unlikely(inode->i_mode & S_ISGID) &&
899 handler->flags == ACL_TYPE_ACCESS &&
900 !in_group_p(inode->i_gid) &&
901 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
902 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
903
904 err = ovl_setattr(dentry, &iattr);
905 if (err)
906 return err;
907 }
908
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200909 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200910 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200911 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200912
913 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200914
915out_acl_release:
916 posix_acl_release(acl);
917 return err;
918}
919
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200920static int ovl_own_xattr_get(const struct xattr_handler *handler,
921 struct dentry *dentry, struct inode *inode,
922 const char *name, void *buffer, size_t size)
923{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200924 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200925}
926
Miklos Szeredid837a492016-07-29 12:05:24 +0200927static int ovl_own_xattr_set(const struct xattr_handler *handler,
928 struct dentry *dentry, struct inode *inode,
929 const char *name, const void *value,
930 size_t size, int flags)
931{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200932 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200933}
934
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200935static int ovl_other_xattr_get(const struct xattr_handler *handler,
936 struct dentry *dentry, struct inode *inode,
937 const char *name, void *buffer, size_t size)
938{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200939 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200940}
941
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200942static int ovl_other_xattr_set(const struct xattr_handler *handler,
943 struct dentry *dentry, struct inode *inode,
944 const char *name, const void *value,
945 size_t size, int flags)
946{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200947 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200948}
949
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200950static const struct xattr_handler __maybe_unused
951ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200952 .name = XATTR_NAME_POSIX_ACL_ACCESS,
953 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200954 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200955 .set = ovl_posix_acl_xattr_set,
956};
957
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200958static const struct xattr_handler __maybe_unused
959ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200960 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
961 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200962 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200963 .set = ovl_posix_acl_xattr_set,
964};
965
966static const struct xattr_handler ovl_own_xattr_handler = {
967 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200968 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200969 .set = ovl_own_xattr_set,
970};
971
972static const struct xattr_handler ovl_other_xattr_handler = {
973 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200974 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200975 .set = ovl_other_xattr_set,
976};
977
978static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200979#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200980 &ovl_posix_acl_access_xattr_handler,
981 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200982#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200983 &ovl_own_xattr_handler,
984 &ovl_other_xattr_handler,
985 NULL
986};
987
Amir Goldstein146d62e2019-04-18 17:42:08 +0300988static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
989 struct inode **ptrap, const char *name)
990{
991 struct inode *trap;
992 int err;
993
994 trap = ovl_get_trap_inode(sb, dir);
Arnd Bergmann1dac6f5b2019-06-17 14:39:29 +0200995 err = PTR_ERR_OR_ZERO(trap);
996 if (err) {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300997 if (err == -ELOOP)
998 pr_err("overlayfs: conflicting %s path\n", name);
999 return err;
1000 }
1001
1002 *ptrap = trap;
1003 return 0;
1004}
1005
1006static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
1007 struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001008{
Miklos Szeredi50649752017-11-10 09:39:15 +01001009 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001010 int err;
1011
Miklos Szerediad204482017-11-10 09:39:16 +01001012 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001013 if (err)
1014 goto out;
1015
1016 /* Upper fs should not be r/o */
1017 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
1018 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1019 err = -EINVAL;
1020 goto out;
1021 }
1022
Miklos Szerediad204482017-11-10 09:39:16 +01001023 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001024 if (err)
1025 goto out;
1026
Amir Goldstein146d62e2019-04-18 17:42:08 +03001027 err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap,
1028 "upperdir");
1029 if (err)
1030 goto out;
1031
Miklos Szeredi50649752017-11-10 09:39:15 +01001032 upper_mnt = clone_private_mount(upperpath);
1033 err = PTR_ERR(upper_mnt);
1034 if (IS_ERR(upper_mnt)) {
1035 pr_err("overlayfs: failed to clone upperpath\n");
1036 goto out;
1037 }
1038
1039 /* Don't inherit atime flags */
1040 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szerediad204482017-11-10 09:39:16 +01001041 ofs->upper_mnt = upper_mnt;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001042
1043 err = -EBUSY;
1044 if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
1045 ofs->upperdir_locked = true;
1046 } else if (ofs->config.index) {
1047 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
1048 goto out;
1049 } else {
1050 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1051 }
1052
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001053 err = 0;
1054out:
1055 return err;
1056}
1057
Amir Goldstein146d62e2019-04-18 17:42:08 +03001058static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1059 struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001060{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001061 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001062 struct dentry *temp;
Amir Goldsteine487d882017-11-07 13:55:04 +02001063 int fh_type;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001064 int err;
1065
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001066 err = mnt_want_write(mnt);
1067 if (err)
1068 return err;
1069
Miklos Szerediad204482017-11-10 09:39:16 +01001070 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1071 if (!ofs->workdir)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001072 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001073
Amir Goldstein146d62e2019-04-18 17:42:08 +03001074 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1075 if (err)
1076 goto out;
1077
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001078 /*
1079 * Upper should support d_type, else whiteouts are visible. Given
1080 * workdir and upper are on same fs, we can do iterate_dir() on
1081 * workdir. This check requires successful creation of workdir in
1082 * previous step.
1083 */
1084 err = ovl_check_d_type_supported(workpath);
1085 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001086 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001087
1088 /*
1089 * We allowed this configuration and don't want to break users over
1090 * kernel upgrade. So warn instead of erroring out.
1091 */
1092 if (!err)
1093 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1094
1095 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +01001096 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1097 ofs->tmpfile = !IS_ERR(temp);
1098 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001099 dput(temp);
1100 else
1101 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1102
1103 /*
1104 * Check if upper/work fs supports trusted.overlay.* xattr
1105 */
Miklos Szerediad204482017-11-10 09:39:16 +01001106 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001107 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +01001108 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +03001109 ofs->config.index = false;
Vivek Goyald5791042018-05-11 11:49:27 -04001110 ofs->config.metacopy = false;
1111 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001112 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001113 } else {
Miklos Szerediad204482017-11-10 09:39:16 +01001114 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001115 }
1116
1117 /* Check if upper/work fs supports file handles */
Amir Goldsteine487d882017-11-07 13:55:04 +02001118 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1119 if (ofs->config.index && !fh_type) {
Miklos Szerediad204482017-11-10 09:39:16 +01001120 ofs->config.index = false;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001121 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1122 }
1123
Amir Goldsteine487d882017-11-07 13:55:04 +02001124 /* Check if upper fs has 32bit inode numbers */
1125 if (fh_type != FILEID_INO32_GEN)
1126 ofs->xino_bits = 0;
1127
Amir Goldsteinf168f102018-01-19 11:26:53 +02001128 /* NFS export of r/w mount depends on index */
1129 if (ofs->config.nfs_export && !ofs->config.index) {
1130 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1131 ofs->config.nfs_export = false;
1132 }
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001133out:
1134 mnt_drop_write(mnt);
1135 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001136}
1137
Amir Goldstein146d62e2019-04-18 17:42:08 +03001138static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1139 struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001140{
1141 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001142 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001143
Miklos Szerediad204482017-11-10 09:39:16 +01001144 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001145 if (err)
1146 goto out;
1147
1148 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001149 if (upperpath->mnt != workpath.mnt) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001150 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1151 goto out;
1152 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001153 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001154 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1155 goto out;
1156 }
1157
Miklos Szeredi8c257412018-09-10 11:43:29 +02001158 ofs->workbasedir = dget(workpath.dentry);
1159
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001160 err = -EBUSY;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001161 if (ovl_inuse_trylock(ofs->workbasedir)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001162 ofs->workdir_locked = true;
1163 } else if (ofs->config.index) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001164 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1165 goto out;
1166 } else {
1167 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1168 }
1169
Amir Goldstein146d62e2019-04-18 17:42:08 +03001170 err = ovl_make_workdir(sb, ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001171
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001172out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001173 path_put(&workpath);
1174
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001175 return err;
1176}
1177
Amir Goldstein146d62e2019-04-18 17:42:08 +03001178static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1179 struct ovl_entry *oe, struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001180{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001181 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001182 int err;
1183
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001184 err = mnt_want_write(mnt);
1185 if (err)
1186 return err;
1187
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001188 /* Verify lower root is upper root origin */
Amir Goldsteind9768072017-09-24 13:00:19 +03001189 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
Amir Goldstein05122442018-01-11 08:25:32 +02001190 true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001191 if (err) {
1192 pr_err("overlayfs: failed to verify upper root origin\n");
1193 goto out;
1194 }
1195
Miklos Szerediad204482017-11-10 09:39:16 +01001196 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1197 if (ofs->indexdir) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001198 err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1199 "indexdir");
1200 if (err)
1201 goto out;
1202
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001203 /*
1204 * Verify upper root is exclusively associated with index dir.
1205 * Older kernels stored upper fh in "trusted.overlay.origin"
1206 * xattr. If that xattr exists, verify that it is a match to
1207 * upper dir file handle. In any case, verify or set xattr
1208 * "trusted.overlay.upper" to indicate that index may have
1209 * directory entries.
1210 */
1211 if (ovl_check_origin_xattr(ofs->indexdir)) {
1212 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1213 upperpath->dentry, true, false);
1214 if (err)
1215 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1216 }
1217 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001218 if (err)
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001219 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001220
1221 /* Cleanup bad/stale/orphan index entries */
1222 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001223 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001224 }
Miklos Szerediad204482017-11-10 09:39:16 +01001225 if (err || !ofs->indexdir)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001226 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1227
1228out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001229 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001230 return err;
1231}
1232
Amir Goldstein9df085f2018-09-03 09:12:09 +03001233static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
Amir Goldstein51486262018-03-28 20:22:41 +03001234{
1235 unsigned int i;
Amir Goldstein9df085f2018-09-03 09:12:09 +03001236
1237 if (!ofs->config.nfs_export && !(ofs->config.index && ofs->upper_mnt))
1238 return true;
1239
1240 for (i = 0; i < ofs->numlowerfs; i++) {
1241 /*
1242 * We use uuid to associate an overlay lower file handle with a
1243 * lower layer, so we can accept lower fs with null uuid as long
1244 * as all lower layers with null uuid are on the same fs.
1245 */
1246 if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid))
1247 return false;
1248 }
1249 return true;
1250}
1251
1252/* Get a unique fsid for the layer */
1253static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1254{
1255 struct super_block *sb = path->mnt->mnt_sb;
1256 unsigned int i;
Amir Goldstein51486262018-03-28 20:22:41 +03001257 dev_t dev;
1258 int err;
1259
1260 /* fsid 0 is reserved for upper fs even with non upper overlay */
1261 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1262 return 0;
1263
1264 for (i = 0; i < ofs->numlowerfs; i++) {
1265 if (ofs->lower_fs[i].sb == sb)
1266 return i + 1;
1267 }
1268
Amir Goldstein9df085f2018-09-03 09:12:09 +03001269 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1270 ofs->config.index = false;
1271 ofs->config.nfs_export = false;
1272 pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1273 uuid_is_null(&sb->s_uuid) ? "null" : "conflicting",
1274 path->dentry);
1275 }
1276
Amir Goldstein51486262018-03-28 20:22:41 +03001277 err = get_anon_bdev(&dev);
1278 if (err) {
1279 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1280 return err;
1281 }
1282
1283 ofs->lower_fs[ofs->numlowerfs].sb = sb;
1284 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1285 ofs->numlowerfs++;
1286
1287 return ofs->numlowerfs;
1288}
1289
Amir Goldstein146d62e2019-04-18 17:42:08 +03001290static int ovl_get_lower_layers(struct super_block *sb, struct ovl_fs *ofs,
1291 struct path *stack, unsigned int numlower)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001292{
1293 int err;
1294 unsigned int i;
1295
1296 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001297 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001298 GFP_KERNEL);
Miklos Szerediad204482017-11-10 09:39:16 +01001299 if (ofs->lower_layers == NULL)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001300 goto out;
Amir Goldstein51486262018-03-28 20:22:41 +03001301
1302 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1303 GFP_KERNEL);
1304 if (ofs->lower_fs == NULL)
1305 goto out;
1306
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001307 for (i = 0; i < numlower; i++) {
1308 struct vfsmount *mnt;
Amir Goldstein146d62e2019-04-18 17:42:08 +03001309 struct inode *trap;
Amir Goldstein51486262018-03-28 20:22:41 +03001310 int fsid;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001311
Amir Goldstein9df085f2018-09-03 09:12:09 +03001312 err = fsid = ovl_get_fsid(ofs, &stack[i]);
Amir Goldstein51486262018-03-28 20:22:41 +03001313 if (err < 0)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001314 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001315
Amir Goldstein146d62e2019-04-18 17:42:08 +03001316 err = -EBUSY;
1317 if (ovl_is_inuse(stack[i].dentry)) {
1318 pr_err("overlayfs: lowerdir is in-use as upperdir/workdir\n");
1319 goto out;
1320 }
1321
1322 err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1323 if (err)
1324 goto out;
1325
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001326 mnt = clone_private_mount(&stack[i]);
1327 err = PTR_ERR(mnt);
1328 if (IS_ERR(mnt)) {
1329 pr_err("overlayfs: failed to clone lowerpath\n");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001330 iput(trap);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001331 goto out;
1332 }
Amir Goldstein51486262018-03-28 20:22:41 +03001333
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001334 /*
1335 * Make lower layers R/O. That way fchmod/fchown on lower file
1336 * will fail instead of modifying lower fs.
1337 */
1338 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1339
Amir Goldstein146d62e2019-04-18 17:42:08 +03001340 ofs->lower_layers[ofs->numlower].trap = trap;
Miklos Szerediad204482017-11-10 09:39:16 +01001341 ofs->lower_layers[ofs->numlower].mnt = mnt;
Amir Goldsteind583ed72017-11-08 19:23:36 +02001342 ofs->lower_layers[ofs->numlower].idx = i + 1;
Amir Goldstein51486262018-03-28 20:22:41 +03001343 ofs->lower_layers[ofs->numlower].fsid = fsid;
1344 if (fsid) {
1345 ofs->lower_layers[ofs->numlower].fs =
1346 &ofs->lower_fs[fsid - 1];
1347 }
Miklos Szerediad204482017-11-10 09:39:16 +01001348 ofs->numlower++;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001349 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001350
Amir Goldstein795939a2018-03-29 09:08:18 +03001351 /*
1352 * When all layers on same fs, overlay can use real inode numbers.
1353 * With mount option "xino=on", mounter declares that there are enough
1354 * free high bits in underlying fs to hold the unique fsid.
1355 * If overlayfs does encounter underlying inodes using the high xino
1356 * bits reserved for fsid, it emits a warning and uses the original
1357 * inode number.
1358 */
1359 if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
Amir Goldsteine487d882017-11-07 13:55:04 +02001360 ofs->xino_bits = 0;
Amir Goldstein795939a2018-03-29 09:08:18 +03001361 ofs->config.xino = OVL_XINO_OFF;
1362 } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1363 /*
1364 * This is a roundup of number of bits needed for numlowerfs+1
1365 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
1366 * upper fs even with non upper overlay.
1367 */
1368 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1369 ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1370 }
1371
1372 if (ofs->xino_bits) {
1373 pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1374 ofs->xino_bits);
1375 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001376
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001377 err = 0;
1378out:
1379 return err;
1380}
1381
Miklos Szeredi4155c102017-11-10 09:39:15 +01001382static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szerediad204482017-11-10 09:39:16 +01001383 struct ovl_fs *ofs)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001384{
1385 int err;
1386 char *lowertmp, *lower;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001387 struct path *stack = NULL;
1388 unsigned int stacklen, numlower = 0, i;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001389 bool remote = false;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001390 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001391
1392 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001393 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001394 if (!lowertmp)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001395 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001396
1397 err = -EINVAL;
1398 stacklen = ovl_split_lowerdirs(lowertmp);
1399 if (stacklen > OVL_MAX_STACK) {
1400 pr_err("overlayfs: too many lower directories, limit is %d\n",
1401 OVL_MAX_STACK);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001402 goto out_err;
Miklos Szerediad204482017-11-10 09:39:16 +01001403 } else if (!ofs->config.upperdir && stacklen == 1) {
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001404 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001405 goto out_err;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001406 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1407 ofs->config.redirect_follow) {
1408 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1409 ofs->config.nfs_export = false;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001410 }
1411
1412 err = -ENOMEM;
1413 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1414 if (!stack)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001415 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001416
1417 err = -EINVAL;
1418 lower = lowertmp;
1419 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szerediad204482017-11-10 09:39:16 +01001420 err = ovl_lower_dir(lower, &stack[numlower], ofs,
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001421 &sb->s_stack_depth, &remote);
1422 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001423 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001424
1425 lower = strchr(lower, '\0') + 1;
1426 }
1427
1428 err = -EINVAL;
1429 sb->s_stack_depth++;
1430 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1431 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001432 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001433 }
1434
Amir Goldstein146d62e2019-04-18 17:42:08 +03001435 err = ovl_get_lower_layers(sb, ofs, stack, numlower);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001436 if (err)
1437 goto out_err;
1438
1439 err = -ENOMEM;
1440 oe = ovl_alloc_entry(numlower);
1441 if (!oe)
1442 goto out_err;
1443
1444 for (i = 0; i < numlower; i++) {
1445 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Miklos Szerediad204482017-11-10 09:39:16 +01001446 oe->lowerstack[i].layer = &ofs->lower_layers[i];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001447 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001448
1449 if (remote)
1450 sb->s_d_op = &ovl_reval_dentry_operations;
1451 else
1452 sb->s_d_op = &ovl_dentry_operations;
1453
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001454out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001455 for (i = 0; i < numlower; i++)
1456 path_put(&stack[i]);
1457 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001458 kfree(lowertmp);
1459
1460 return oe;
1461
1462out_err:
1463 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001464 goto out;
1465}
1466
Amir Goldstein146d62e2019-04-18 17:42:08 +03001467/*
1468 * Check if this layer root is a descendant of:
1469 * - another layer of this overlayfs instance
1470 * - upper/work dir of any overlayfs instance
Amir Goldstein146d62e2019-04-18 17:42:08 +03001471 */
1472static int ovl_check_layer(struct super_block *sb, struct dentry *dentry,
1473 const char *name)
1474{
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001475 struct dentry *next = dentry, *parent;
Amir Goldstein146d62e2019-04-18 17:42:08 +03001476 int err = 0;
1477
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001478 if (!dentry)
Amir Goldstein146d62e2019-04-18 17:42:08 +03001479 return 0;
1480
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001481 parent = dget_parent(next);
1482
1483 /* Walk back ancestors to root (inclusive) looking for traps */
1484 while (!err && parent != next) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001485 if (ovl_is_inuse(parent)) {
1486 err = -EBUSY;
1487 pr_err("overlayfs: %s path overlapping in-use upperdir/workdir\n",
1488 name);
1489 } else if (ovl_lookup_trap_inode(sb, parent)) {
1490 err = -ELOOP;
1491 pr_err("overlayfs: overlapping %s path\n", name);
1492 }
Amir Goldstein146d62e2019-04-18 17:42:08 +03001493 next = parent;
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001494 parent = dget_parent(next);
1495 dput(next);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001496 }
1497
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001498 dput(parent);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001499
1500 return err;
1501}
1502
1503/*
1504 * Check if any of the layers or work dirs overlap.
1505 */
1506static int ovl_check_overlapping_layers(struct super_block *sb,
1507 struct ovl_fs *ofs)
1508{
1509 int i, err;
1510
1511 if (ofs->upper_mnt) {
1512 err = ovl_check_layer(sb, ofs->upper_mnt->mnt_root, "upperdir");
1513 if (err)
1514 return err;
1515
1516 /*
1517 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1518 * this instance and covers overlapping work and index dirs,
1519 * unless work or index dir have been moved since created inside
1520 * workbasedir. In that case, we already have their traps in
1521 * inode cache and we will catch that case on lookup.
1522 */
1523 err = ovl_check_layer(sb, ofs->workbasedir, "workdir");
1524 if (err)
1525 return err;
1526 }
1527
1528 for (i = 0; i < ofs->numlower; i++) {
1529 err = ovl_check_layer(sb, ofs->lower_layers[i].mnt->mnt_root,
1530 "lowerdir");
1531 if (err)
1532 return err;
1533 }
1534
1535 return 0;
1536}
1537
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001538static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1539{
Kees Cook33006cd2017-03-29 14:02:19 -07001540 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001541 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001542 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001543 struct ovl_fs *ofs;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001544 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001545 int err;
1546
Erez Zadokf45827e82014-10-24 00:14:38 +02001547 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001548 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1549 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001550 goto out;
1551
Miklos Szerediad204482017-11-10 09:39:16 +01001552 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001553 if (!cred)
1554 goto out_err;
1555
Miklos Szerediad204482017-11-10 09:39:16 +01001556 ofs->config.index = ovl_index_def;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001557 ofs->config.nfs_export = ovl_nfs_export_def;
Amir Goldstein795939a2018-03-29 09:08:18 +03001558 ofs->config.xino = ovl_xino_def();
Vivek Goyald5791042018-05-11 11:49:27 -04001559 ofs->config.metacopy = ovl_metacopy_def;
Miklos Szerediad204482017-11-10 09:39:16 +01001560 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001561 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001562 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001563
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001564 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001565 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001566 if (!silent)
1567 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001568 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001569 }
1570
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001571 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001572 sb->s_maxbytes = MAX_LFS_FILESIZE;
Amir Goldsteine487d882017-11-07 13:55:04 +02001573 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
Amir Goldstein795939a2018-03-29 09:08:18 +03001574 if (ofs->config.xino != OVL_XINO_OFF)
1575 ofs->xino_bits = BITS_PER_LONG - 32;
1576
Amir Goldstein146d62e2019-04-18 17:42:08 +03001577 /* alloc/destroy_inode needed for setting up traps in inode cache */
1578 sb->s_op = &ovl_super_operations;
1579
Miklos Szerediad204482017-11-10 09:39:16 +01001580 if (ofs->config.upperdir) {
1581 if (!ofs->config.workdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001582 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001583 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001584 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001585
Amir Goldstein146d62e2019-04-18 17:42:08 +03001586 err = ovl_get_upper(sb, ofs, &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001587 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001588 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001589
Amir Goldstein146d62e2019-04-18 17:42:08 +03001590 err = ovl_get_workdir(sb, ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001591 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001592 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001593
Miklos Szerediad204482017-11-10 09:39:16 +01001594 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001595 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001596
Miklos Szerediad204482017-11-10 09:39:16 +01001597 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1598 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001599
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001600 }
Miklos Szerediad204482017-11-10 09:39:16 +01001601 oe = ovl_get_lowerstack(sb, ofs);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001602 err = PTR_ERR(oe);
1603 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001604 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001605
hujianyang71cbad72015-01-15 13:20:57 +08001606 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szerediad204482017-11-10 09:39:16 +01001607 if (!ofs->upper_mnt)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001608 sb->s_flags |= SB_RDONLY;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001609
Miklos Szerediad204482017-11-10 09:39:16 +01001610 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001611 err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001612 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001613 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001614
Amir Goldstein972d0092017-09-19 12:14:18 +03001615 /* Force r/o mount with no index dir */
1616 if (!ofs->indexdir) {
1617 dput(ofs->workdir);
1618 ofs->workdir = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001619 sb->s_flags |= SB_RDONLY;
Amir Goldstein972d0092017-09-19 12:14:18 +03001620 }
1621
Amir Goldstein02bcd152017-06-21 15:28:36 +03001622 }
1623
Amir Goldstein146d62e2019-04-18 17:42:08 +03001624 err = ovl_check_overlapping_layers(sb, ofs);
1625 if (err)
1626 goto out_free_oe;
1627
Amir Goldstein972d0092017-09-19 12:14:18 +03001628 /* Show index=off in /proc/mounts for forced r/o mount */
Amir Goldsteinf168f102018-01-19 11:26:53 +02001629 if (!ofs->indexdir) {
Miklos Szerediad204482017-11-10 09:39:16 +01001630 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001631 if (ofs->upper_mnt && ofs->config.nfs_export) {
1632 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1633 ofs->config.nfs_export = false;
1634 }
1635 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001636
Vivek Goyald5791042018-05-11 11:49:27 -04001637 if (ofs->config.metacopy && ofs->config.nfs_export) {
1638 pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1639 ofs->config.nfs_export = false;
1640 }
1641
Amir Goldstein8383f172017-10-02 11:31:42 +03001642 if (ofs->config.nfs_export)
1643 sb->s_export_op = &ovl_export_operations;
1644
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001645 /* Never override disk quota limits or use reserved space */
1646 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1647
Vivek Goyal655042c2016-10-14 03:03:36 +02001648 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Vivek Goyal655042c2016-10-14 03:03:36 +02001649 sb->s_xattr = ovl_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01001650 sb->s_fs_info = ofs;
Miklos Szeredide2a4a52018-07-18 15:44:43 +02001651 sb->s_flags |= SB_POSIXACL;
Vivek Goyal655042c2016-10-14 03:03:36 +02001652
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001653 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001654 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001655 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001656 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001657
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001658 root_dentry->d_fsdata = oe;
1659
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001660 mntput(upperpath.mnt);
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001661 if (upperpath.dentry) {
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001662 ovl_dentry_set_upper_alias(root_dentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +02001663 if (ovl_is_impuredir(upperpath.dentry))
1664 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001665 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001666
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001667 /* Root is always merge -> can have whiteouts */
1668 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Amir Goldstein2ca3c142018-01-30 13:31:09 +02001669 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
Vivek Goyal0c288872018-05-11 11:49:28 -04001670 ovl_set_upperdata(d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001671 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
Vivek Goyal2664bd02018-05-11 11:49:30 -04001672 ovl_dentry_lower(root_dentry), NULL);
Miklos Szeredied06e062015-12-09 16:11:59 +01001673
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001674 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001675
1676 return 0;
1677
Miklos Szeredi4155c102017-11-10 09:39:15 +01001678out_free_oe:
1679 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001680 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001681out_err:
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001682 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01001683 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001684out:
1685 return err;
1686}
1687
1688static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1689 const char *dev_name, void *raw_data)
1690{
1691 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1692}
1693
1694static struct file_system_type ovl_fs_type = {
1695 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001696 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001697 .mount = ovl_mount,
1698 .kill_sb = kill_anon_super,
1699};
Miklos Szeredief94b182014-11-20 16:39:59 +01001700MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001701
Amir Goldstein13cf1992017-06-12 09:54:40 +03001702static void ovl_inode_init_once(void *foo)
1703{
1704 struct ovl_inode *oi = foo;
1705
1706 inode_init_once(&oi->vfs_inode);
1707}
1708
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001709static int __init ovl_init(void)
1710{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001711 int err;
1712
1713 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1714 sizeof(struct ovl_inode), 0,
1715 (SLAB_RECLAIM_ACCOUNT|
1716 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1717 ovl_inode_init_once);
1718 if (ovl_inode_cachep == NULL)
1719 return -ENOMEM;
1720
1721 err = register_filesystem(&ovl_fs_type);
1722 if (err)
1723 kmem_cache_destroy(ovl_inode_cachep);
1724
1725 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001726}
1727
1728static void __exit ovl_exit(void)
1729{
1730 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001731
1732 /*
1733 * Make sure all delayed rcu free inodes are flushed before we
1734 * destroy cache.
1735 */
1736 rcu_barrier();
1737 kmem_cache_destroy(ovl_inode_cachep);
1738
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001739}
1740
1741module_init(ovl_init);
1742module_exit(ovl_exit);