blob: 746ea36f3171d5e5ef75bbf108ee3b2e004af93a [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>
Amir Goldsteine487d882017-11-07 13:55:04 +020020#include <linux/exportfs.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020021#include "overlayfs.h"
22
23MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24MODULE_DESCRIPTION("Overlay filesystem");
25MODULE_LICENSE("GPL");
26
Miklos Szeredie9be9d52014-10-24 00:14:38 +020027
28struct ovl_dir_cache;
29
Miklos Szeredia78d9f02014-12-13 00:59:52 +010030#define OVL_MAX_STACK 500
31
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010032static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
33module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
34MODULE_PARM_DESC(ovl_redirect_dir_def,
35 "Default to on or off for the redirect_dir feature");
Miklos Szeredie9be9d52014-10-24 00:14:38 +020036
Miklos Szeredi438c84c2017-12-11 11:28:10 +010037static bool ovl_redirect_always_follow =
38 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
39module_param_named(redirect_always_follow, ovl_redirect_always_follow,
40 bool, 0644);
41MODULE_PARM_DESC(ovl_redirect_always_follow,
42 "Follow redirects even if redirect_dir feature is turned off");
43
Amir Goldstein02bcd152017-06-21 15:28:36 +030044static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
45module_param_named(index, ovl_index_def, bool, 0644);
46MODULE_PARM_DESC(ovl_index_def,
47 "Default to on or off for the inodes index feature");
48
Amir Goldsteinf168f102018-01-19 11:26:53 +020049static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
50module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
51MODULE_PARM_DESC(ovl_nfs_export_def,
52 "Default to on or off for the NFS export feature");
53
Amir Goldstein795939a2018-03-29 09:08:18 +030054static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
55module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
56MODULE_PARM_DESC(ovl_xino_auto_def,
57 "Auto enable xino feature");
58
Miklos Szeredi4155c102017-11-10 09:39:15 +010059static void ovl_entry_stack_free(struct ovl_entry *oe)
60{
61 unsigned int i;
62
63 for (i = 0; i < oe->numlower; i++)
64 dput(oe->lowerstack[i].dentry);
65}
66
Vivek Goyald5791042018-05-11 11:49:27 -040067static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
68module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
69MODULE_PARM_DESC(ovl_metacopy_def,
70 "Default to on or off for the metadata only copy up feature");
71
Miklos Szeredie9be9d52014-10-24 00:14:38 +020072static void ovl_dentry_release(struct dentry *dentry)
73{
74 struct ovl_entry *oe = dentry->d_fsdata;
75
76 if (oe) {
Miklos Szeredi4155c102017-11-10 09:39:15 +010077 ovl_entry_stack_free(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020078 kfree_rcu(oe, rcu);
79 }
80}
81
Miklos Szeredi2d902672016-06-30 08:53:27 +020082static struct dentry *ovl_d_real(struct dentry *dentry,
Miklos Szeredifb160432018-07-18 15:44:44 +020083 const struct inode *inode)
Miklos Szeredid101a122016-03-26 16:14:37 -040084{
85 struct dentry *real;
86
Miklos Szeredie8c985b2018-07-18 15:44:41 +020087 /* It's an overlay file */
88 if (inode && d_inode(dentry) == inode)
89 return dentry;
90
Miklos Szeredica4c8a32016-12-16 11:02:55 +010091 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040092 if (!inode || inode == d_inode(dentry))
93 return dentry;
94 goto bug;
95 }
96
97 real = ovl_dentry_upper(dentry);
Vivek Goyal2c3d7352018-05-11 11:49:31 -040098 if (real && (inode == d_inode(real)))
Miklos Szeredid101a122016-03-26 16:14:37 -040099 return real;
100
Vivek Goyal2c3d7352018-05-11 11:49:31 -0400101 if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
102 return real;
103
104 real = ovl_dentry_lowerdata(dentry);
Miklos Szeredid101a122016-03-26 16:14:37 -0400105 if (!real)
106 goto bug;
107
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100108 /* Handle recursion */
Miklos Szeredifb160432018-07-18 15:44:44 +0200109 real = d_real(real, inode);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100110
Miklos Szeredid101a122016-03-26 16:14:37 -0400111 if (!inode || inode == d_inode(real))
112 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400113bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200114 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400115 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
116 return dentry;
117}
118
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200119static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
120{
121 struct ovl_entry *oe = dentry->d_fsdata;
122 unsigned int i;
123 int ret = 1;
124
125 for (i = 0; i < oe->numlower; i++) {
126 struct dentry *d = oe->lowerstack[i].dentry;
127
128 if (d->d_flags & DCACHE_OP_REVALIDATE) {
129 ret = d->d_op->d_revalidate(d, flags);
130 if (ret < 0)
131 return ret;
132 if (!ret) {
133 if (!(flags & LOOKUP_RCU))
134 d_invalidate(d);
135 return -ESTALE;
136 }
137 }
138 }
139 return 1;
140}
141
142static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
143{
144 struct ovl_entry *oe = dentry->d_fsdata;
145 unsigned int i;
146 int ret = 1;
147
148 for (i = 0; i < oe->numlower; i++) {
149 struct dentry *d = oe->lowerstack[i].dentry;
150
151 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
152 ret = d->d_op->d_weak_revalidate(d, flags);
153 if (ret <= 0)
154 break;
155 }
156 }
157 return ret;
158}
159
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200160static const struct dentry_operations ovl_dentry_operations = {
161 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400162 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200163};
164
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200165static const struct dentry_operations ovl_reval_dentry_operations = {
166 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400167 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200168 .d_revalidate = ovl_dentry_revalidate,
169 .d_weak_revalidate = ovl_dentry_weak_revalidate,
170};
171
Amir Goldstein13cf1992017-06-12 09:54:40 +0300172static struct kmem_cache *ovl_inode_cachep;
173
174static struct inode *ovl_alloc_inode(struct super_block *sb)
175{
176 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
177
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900178 if (!oi)
179 return NULL;
180
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200181 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200182 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200183 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200184 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200185 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200186 oi->lower = NULL;
Vivek Goyal2664bd02018-05-11 11:49:30 -0400187 oi->lowerdata = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300188 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200189
Amir Goldstein13cf1992017-06-12 09:54:40 +0300190 return &oi->vfs_inode;
191}
192
Al Viro0b269ded2019-04-15 22:52:17 -0400193static void ovl_free_inode(struct inode *inode)
Amir Goldstein13cf1992017-06-12 09:54:40 +0300194{
Al Viro0b269ded2019-04-15 22:52:17 -0400195 struct ovl_inode *oi = OVL_I(inode);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300196
Al Viro0b269ded2019-04-15 22:52:17 -0400197 kfree(oi->redirect);
198 mutex_destroy(&oi->lock);
199 kmem_cache_free(ovl_inode_cachep, oi);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300200}
201
202static void ovl_destroy_inode(struct inode *inode)
203{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200204 struct ovl_inode *oi = OVL_I(inode);
205
206 dput(oi->__upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200207 iput(oi->lower);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400208 if (S_ISDIR(inode->i_mode))
209 ovl_dir_cache_free(inode);
210 else
211 iput(oi->lowerdata);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300212}
213
Miklos Szerediad204482017-11-10 09:39:16 +0100214static void ovl_free_fs(struct ovl_fs *ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215{
Miklos Szeredidd662662014-12-13 00:59:43 +0100216 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200217
Amir Goldstein146d62e2019-04-18 17:42:08 +0300218 iput(ofs->indexdir_trap);
219 iput(ofs->workdir_trap);
220 iput(ofs->upperdir_trap);
Miklos Szerediad204482017-11-10 09:39:16 +0100221 dput(ofs->indexdir);
222 dput(ofs->workdir);
223 if (ofs->workdir_locked)
224 ovl_inuse_unlock(ofs->workbasedir);
225 dput(ofs->workbasedir);
226 if (ofs->upperdir_locked)
227 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
228 mntput(ofs->upper_mnt);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300229 for (i = 0; i < ofs->numlower; i++) {
230 iput(ofs->lower_layers[i].trap);
Miklos Szerediad204482017-11-10 09:39:16 +0100231 mntput(ofs->lower_layers[i].mnt);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300232 }
Amir Goldstein51486262018-03-28 20:22:41 +0300233 for (i = 0; i < ofs->numlowerfs; i++)
234 free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
Miklos Szerediad204482017-11-10 09:39:16 +0100235 kfree(ofs->lower_layers);
Amir Goldstein51486262018-03-28 20:22:41 +0300236 kfree(ofs->lower_fs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200237
Miklos Szerediad204482017-11-10 09:39:16 +0100238 kfree(ofs->config.lowerdir);
239 kfree(ofs->config.upperdir);
240 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100241 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100242 if (ofs->creator_cred)
243 put_cred(ofs->creator_cred);
244 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200245}
246
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100247static void ovl_put_super(struct super_block *sb)
248{
249 struct ovl_fs *ofs = sb->s_fs_info;
250
251 ovl_free_fs(ofs);
252}
253
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800254/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200255static int ovl_sync_fs(struct super_block *sb, int wait)
256{
Miklos Szerediad204482017-11-10 09:39:16 +0100257 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200258 struct super_block *upper_sb;
259 int ret;
260
Miklos Szerediad204482017-11-10 09:39:16 +0100261 if (!ofs->upper_mnt)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200262 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800263
264 /*
265 * If this is a sync(2) call or an emergency sync, all the super blocks
266 * will be iterated, including upper_sb, so no need to do anything.
267 *
268 * If this is a syncfs(2) call, then we do need to call
269 * sync_filesystem() on upper_sb, but enough if we do it when being
270 * called with wait == 1.
271 */
272 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200273 return 0;
274
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800275 upper_sb = ofs->upper_mnt->mnt_sb;
276
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200277 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800278 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200279 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800280
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200281 return ret;
282}
283
Andy Whitcroftcc259632014-10-24 00:14:38 +0200284/**
285 * ovl_statfs
286 * @sb: The overlayfs super block
287 * @buf: The struct kstatfs to fill in with stats
288 *
289 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100290 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200291 */
292static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
293{
294 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
295 struct dentry *root_dentry = dentry->d_sb->s_root;
296 struct path path;
297 int err;
298
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100299 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200300
301 err = vfs_statfs(&path, buf);
302 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100303 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200304 buf->f_type = OVERLAYFS_SUPER_MAGIC;
305 }
306
307 return err;
308}
309
Amir Goldstein02bcd152017-06-21 15:28:36 +0300310/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100311static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300312{
Miklos Szerediad204482017-11-10 09:39:16 +0100313 return (!ofs->upper_mnt || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300314}
315
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100316static const char *ovl_redirect_mode_def(void)
317{
318 return ovl_redirect_dir_def ? "on" : "off";
319}
320
Amir Goldstein795939a2018-03-29 09:08:18 +0300321enum {
322 OVL_XINO_OFF,
323 OVL_XINO_AUTO,
324 OVL_XINO_ON,
325};
326
327static const char * const ovl_xino_str[] = {
328 "off",
329 "auto",
330 "on",
331};
332
333static inline int ovl_xino_def(void)
334{
335 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
336}
337
Erez Zadokf45827e82014-10-24 00:14:38 +0200338/**
339 * ovl_show_options
340 *
341 * Prints the mount options for a given superblock.
342 * Returns zero; does not fail.
343 */
344static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
345{
346 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100347 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200348
Miklos Szerediad204482017-11-10 09:39:16 +0100349 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
350 if (ofs->config.upperdir) {
351 seq_show_option(m, "upperdir", ofs->config.upperdir);
352 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100353 }
Miklos Szerediad204482017-11-10 09:39:16 +0100354 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200355 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100356 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
357 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100358 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100359 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Amir Goldsteinf168f102018-01-19 11:26:53 +0200360 if (ofs->config.nfs_export != ovl_nfs_export_def)
361 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
362 "on" : "off");
Amir Goldstein795939a2018-03-29 09:08:18 +0300363 if (ofs->config.xino != ovl_xino_def())
364 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
Vivek Goyald5791042018-05-11 11:49:27 -0400365 if (ofs->config.metacopy != ovl_metacopy_def)
366 seq_printf(m, ",metacopy=%s",
367 ofs->config.metacopy ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200368 return 0;
369}
370
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900371static int ovl_remount(struct super_block *sb, int *flags, char *data)
372{
Miklos Szerediad204482017-11-10 09:39:16 +0100373 struct ovl_fs *ofs = sb->s_fs_info;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900374
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800375 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900376 return -EROFS;
377
378 return 0;
379}
380
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200381static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300382 .alloc_inode = ovl_alloc_inode,
Al Viro0b269ded2019-04-15 22:52:17 -0400383 .free_inode = ovl_free_inode,
Amir Goldstein13cf1992017-06-12 09:54:40 +0300384 .destroy_inode = ovl_destroy_inode,
385 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200386 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200387 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200388 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200389 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900390 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200391};
392
393enum {
394 OPT_LOWERDIR,
395 OPT_UPPERDIR,
396 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200397 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100398 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300399 OPT_INDEX_ON,
400 OPT_INDEX_OFF,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200401 OPT_NFS_EXPORT_ON,
402 OPT_NFS_EXPORT_OFF,
Amir Goldstein795939a2018-03-29 09:08:18 +0300403 OPT_XINO_ON,
404 OPT_XINO_OFF,
405 OPT_XINO_AUTO,
Vivek Goyald5791042018-05-11 11:49:27 -0400406 OPT_METACOPY_ON,
407 OPT_METACOPY_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408 OPT_ERR,
409};
410
411static const match_table_t ovl_tokens = {
412 {OPT_LOWERDIR, "lowerdir=%s"},
413 {OPT_UPPERDIR, "upperdir=%s"},
414 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200415 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100416 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300417 {OPT_INDEX_ON, "index=on"},
418 {OPT_INDEX_OFF, "index=off"},
Amir Goldsteinf168f102018-01-19 11:26:53 +0200419 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
420 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
Amir Goldstein795939a2018-03-29 09:08:18 +0300421 {OPT_XINO_ON, "xino=on"},
422 {OPT_XINO_OFF, "xino=off"},
423 {OPT_XINO_AUTO, "xino=auto"},
Vivek Goyald5791042018-05-11 11:49:27 -0400424 {OPT_METACOPY_ON, "metacopy=on"},
425 {OPT_METACOPY_OFF, "metacopy=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200426 {OPT_ERR, NULL}
427};
428
Miklos Szeredi91c77942014-11-20 16:40:00 +0100429static char *ovl_next_opt(char **s)
430{
431 char *sbegin = *s;
432 char *p;
433
434 if (sbegin == NULL)
435 return NULL;
436
437 for (p = sbegin; *p; p++) {
438 if (*p == '\\') {
439 p++;
440 if (!*p)
441 break;
442 } else if (*p == ',') {
443 *p = '\0';
444 *s = p + 1;
445 return sbegin;
446 }
447 }
448 *s = NULL;
449 return sbegin;
450}
451
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100452static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
453{
454 if (strcmp(mode, "on") == 0) {
455 config->redirect_dir = true;
456 /*
457 * Does not make sense to have redirect creation without
458 * redirect following.
459 */
460 config->redirect_follow = true;
461 } else if (strcmp(mode, "follow") == 0) {
462 config->redirect_follow = true;
463 } else if (strcmp(mode, "off") == 0) {
464 if (ovl_redirect_always_follow)
465 config->redirect_follow = true;
466 } else if (strcmp(mode, "nofollow") != 0) {
467 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
468 mode);
469 return -EINVAL;
470 }
471
472 return 0;
473}
474
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200475static int ovl_parse_opt(char *opt, struct ovl_config *config)
476{
477 char *p;
Vivek Goyald5791042018-05-11 11:49:27 -0400478 int err;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100479 bool metacopy_opt = false, redirect_opt = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100481 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
482 if (!config->redirect_mode)
483 return -ENOMEM;
484
Miklos Szeredi91c77942014-11-20 16:40:00 +0100485 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200486 int token;
487 substring_t args[MAX_OPT_ARGS];
488
489 if (!*p)
490 continue;
491
492 token = match_token(p, ovl_tokens, args);
493 switch (token) {
494 case OPT_UPPERDIR:
495 kfree(config->upperdir);
496 config->upperdir = match_strdup(&args[0]);
497 if (!config->upperdir)
498 return -ENOMEM;
499 break;
500
501 case OPT_LOWERDIR:
502 kfree(config->lowerdir);
503 config->lowerdir = match_strdup(&args[0]);
504 if (!config->lowerdir)
505 return -ENOMEM;
506 break;
507
508 case OPT_WORKDIR:
509 kfree(config->workdir);
510 config->workdir = match_strdup(&args[0]);
511 if (!config->workdir)
512 return -ENOMEM;
513 break;
514
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200515 case OPT_DEFAULT_PERMISSIONS:
516 config->default_permissions = true;
517 break;
518
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100519 case OPT_REDIRECT_DIR:
520 kfree(config->redirect_mode);
521 config->redirect_mode = match_strdup(&args[0]);
522 if (!config->redirect_mode)
523 return -ENOMEM;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100524 redirect_opt = true;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100525 break;
526
Amir Goldstein02bcd152017-06-21 15:28:36 +0300527 case OPT_INDEX_ON:
528 config->index = true;
529 break;
530
531 case OPT_INDEX_OFF:
532 config->index = false;
533 break;
534
Amir Goldsteinf168f102018-01-19 11:26:53 +0200535 case OPT_NFS_EXPORT_ON:
536 config->nfs_export = true;
537 break;
538
539 case OPT_NFS_EXPORT_OFF:
540 config->nfs_export = false;
541 break;
542
Amir Goldstein795939a2018-03-29 09:08:18 +0300543 case OPT_XINO_ON:
544 config->xino = OVL_XINO_ON;
545 break;
546
547 case OPT_XINO_OFF:
548 config->xino = OVL_XINO_OFF;
549 break;
550
551 case OPT_XINO_AUTO:
552 config->xino = OVL_XINO_AUTO;
553 break;
554
Vivek Goyald5791042018-05-11 11:49:27 -0400555 case OPT_METACOPY_ON:
556 config->metacopy = true;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100557 metacopy_opt = true;
Vivek Goyald5791042018-05-11 11:49:27 -0400558 break;
559
560 case OPT_METACOPY_OFF:
561 config->metacopy = false;
562 break;
563
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200564 default:
hujianyangbead55e2015-01-15 13:17:36 +0800565 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200566 return -EINVAL;
567 }
568 }
hujianyang71cbad72015-01-15 13:20:57 +0800569
570 /* Workdir is useless in non-upper mount */
571 if (!config->upperdir && config->workdir) {
572 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
573 config->workdir);
574 kfree(config->workdir);
575 config->workdir = NULL;
576 }
577
Vivek Goyald5791042018-05-11 11:49:27 -0400578 err = ovl_parse_redirect_mode(config, config->redirect_mode);
579 if (err)
580 return err;
581
Miklos Szeredid47748e2018-11-01 21:31:39 +0100582 /*
583 * This is to make the logic below simpler. It doesn't make any other
584 * difference, since config->redirect_dir is only used for upper.
585 */
586 if (!config->upperdir && config->redirect_follow)
587 config->redirect_dir = true;
588
589 /* Resolve metacopy -> redirect_dir dependency */
590 if (config->metacopy && !config->redirect_dir) {
591 if (metacopy_opt && redirect_opt) {
592 pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n",
593 config->redirect_mode);
594 return -EINVAL;
595 }
596 if (redirect_opt) {
597 /*
598 * There was an explicit redirect_dir=... that resulted
599 * in this conflict.
600 */
601 pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n",
602 config->redirect_mode);
603 config->metacopy = false;
604 } else {
605 /* Automatically enable redirect otherwise. */
606 config->redirect_follow = config->redirect_dir = true;
607 }
Vivek Goyald5791042018-05-11 11:49:27 -0400608 }
609
610 return 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200611}
612
613#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300614#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200615
Miklos Szerediad204482017-11-10 09:39:16 +0100616static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300617 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200618{
Miklos Szerediad204482017-11-10 09:39:16 +0100619 struct inode *dir = ofs->workbasedir->d_inode;
620 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200621 struct dentry *work;
622 int err;
623 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300624 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200625
Al Viro59551022016-01-22 15:40:57 -0500626 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300627 locked = true;
628
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200629retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100630 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200631
632 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200633 struct iattr attr = {
634 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000635 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200636 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200637
638 if (work->d_inode) {
639 err = -EEXIST;
640 if (retried)
641 goto out_dput;
642
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300643 if (persist)
644 goto out_unlock;
645
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200646 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200647 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200648 dput(work);
649 goto retry;
650 }
651
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300652 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
653 err = PTR_ERR(work);
654 if (IS_ERR(work))
655 goto out_err;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200656
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200657 /*
658 * Try to remove POSIX ACL xattrs from workdir. We are good if:
659 *
660 * a) success (there was a POSIX ACL xattr and was removed)
661 * b) -ENODATA (there was no POSIX ACL xattr)
662 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
663 *
664 * There are various other error values that could effectively
665 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
666 * if the xattr name is too long), but the set of filesystems
667 * allowed as upper are limited to "normal" ones, where checking
668 * for the above two errors is sufficient.
669 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200670 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200671 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200672 goto out_dput;
673
674 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200675 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200676 goto out_dput;
677
678 /* Clear any inherited mode bits */
679 inode_lock(work->d_inode);
680 err = notify_change(work, &attr, NULL);
681 inode_unlock(work->d_inode);
682 if (err)
683 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300684 } else {
685 err = PTR_ERR(work);
686 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200687 }
688out_unlock:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300689 if (locked)
690 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200691
692 return work;
693
694out_dput:
695 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300696out_err:
697 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100698 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300699 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200700 goto out_unlock;
701}
702
Miklos Szeredi91c77942014-11-20 16:40:00 +0100703static void ovl_unescape(char *s)
704{
705 char *d = s;
706
707 for (;; s++, d++) {
708 if (*s == '\\')
709 s++;
710 *d = *s;
711 if (!*s)
712 break;
713 }
714}
715
Miklos Szerediab508822014-12-13 00:59:49 +0100716static int ovl_mount_dir_noesc(const char *name, struct path *path)
717{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100718 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100719
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100720 if (!*name) {
721 pr_err("overlayfs: empty lowerdir\n");
722 goto out;
723 }
Miklos Szerediab508822014-12-13 00:59:49 +0100724 err = kern_path(name, LOOKUP_FOLLOW, path);
725 if (err) {
726 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
727 goto out;
728 }
729 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200730 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100731 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
732 goto out_put;
733 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100734 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100735 pr_err("overlayfs: '%s' not a directory\n", name);
736 goto out_put;
737 }
738 return 0;
739
740out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100741 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100742out:
743 return err;
744}
745
746static int ovl_mount_dir(const char *name, struct path *path)
747{
748 int err = -ENOMEM;
749 char *tmp = kstrdup(name, GFP_KERNEL);
750
751 if (tmp) {
752 ovl_unescape(tmp);
753 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200754
755 if (!err)
756 if (ovl_dentry_remote(path->dentry)) {
757 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
758 tmp);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100759 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200760 err = -EINVAL;
761 }
Miklos Szerediab508822014-12-13 00:59:49 +0100762 kfree(tmp);
763 }
764 return err;
765}
766
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100767static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
768 const char *name)
769{
770 struct kstatfs statfs;
771 int err = vfs_statfs(path, &statfs);
772
773 if (err)
774 pr_err("overlayfs: statfs failed on '%s'\n", name);
775 else
776 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
777
778 return err;
779}
780
781static int ovl_lower_dir(const char *name, struct path *path,
782 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100783{
Amir Goldsteine487d882017-11-07 13:55:04 +0200784 int fh_type;
Miklos Szerediab508822014-12-13 00:59:49 +0100785 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100786
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100787 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100788 if (err)
789 goto out;
790
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100791 err = ovl_check_namelen(path, ofs, name);
792 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100793 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100794
Miklos Szerediab508822014-12-13 00:59:49 +0100795 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
796
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200797 if (ovl_dentry_remote(path->dentry))
798 *remote = true;
799
Amir Goldstein02bcd152017-06-21 15:28:36 +0300800 /*
Amir Goldsteinf168f102018-01-19 11:26:53 +0200801 * The inodes index feature and NFS export need to encode and decode
802 * file handles, so they require that all layers support them.
Amir Goldstein02bcd152017-06-21 15:28:36 +0300803 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200804 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
Amir Goldsteinf168f102018-01-19 11:26:53 +0200805 if ((ofs->config.nfs_export ||
Amir Goldsteine487d882017-11-07 13:55:04 +0200806 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300807 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200808 ofs->config.nfs_export = false;
809 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
810 name);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300811 }
812
Amir Goldsteine487d882017-11-07 13:55:04 +0200813 /* Check if lower fs has 32bit inode numbers */
814 if (fh_type != FILEID_INO32_GEN)
815 ofs->xino_bits = 0;
816
Miklos Szerediab508822014-12-13 00:59:49 +0100817 return 0;
818
819out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100820 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100821out:
822 return err;
823}
824
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200825/* Workdir should not be subdir of upperdir and vice versa */
826static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
827{
828 bool ok = false;
829
830 if (workdir != upperdir) {
831 ok = (lock_rename(workdir, upperdir) == NULL);
832 unlock_rename(workdir, upperdir);
833 }
834 return ok;
835}
836
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100837static unsigned int ovl_split_lowerdirs(char *str)
838{
839 unsigned int ctr = 1;
840 char *s, *d;
841
842 for (s = d = str;; s++, d++) {
843 if (*s == '\\') {
844 s++;
845 } else if (*s == ':') {
846 *d = '\0';
847 ctr++;
848 continue;
849 }
850 *d = *s;
851 if (!*s)
852 break;
853 }
854 return ctr;
855}
856
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200857static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200858ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
859 struct dentry *dentry, struct inode *inode,
860 const char *name, void *buffer, size_t size)
861{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200862 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200863}
864
865static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200866ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
867 struct dentry *dentry, struct inode *inode,
868 const char *name, const void *value,
869 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200870{
871 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200872 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200873 struct posix_acl *acl = NULL;
874 int err;
875
876 /* Check that everything is OK before copy-up */
877 if (value) {
878 acl = posix_acl_from_xattr(&init_user_ns, value, size);
879 if (IS_ERR(acl))
880 return PTR_ERR(acl);
881 }
882 err = -EOPNOTSUPP;
883 if (!IS_POSIXACL(d_inode(workdir)))
884 goto out_acl_release;
885 if (!realinode->i_op->set_acl)
886 goto out_acl_release;
887 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
888 err = acl ? -EACCES : 0;
889 goto out_acl_release;
890 }
891 err = -EPERM;
892 if (!inode_owner_or_capable(inode))
893 goto out_acl_release;
894
895 posix_acl_release(acl);
896
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100897 /*
898 * Check if sgid bit needs to be cleared (actual setacl operation will
899 * be done with mounter's capabilities and so that won't do it for us).
900 */
901 if (unlikely(inode->i_mode & S_ISGID) &&
902 handler->flags == ACL_TYPE_ACCESS &&
903 !in_group_p(inode->i_gid) &&
904 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
905 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
906
907 err = ovl_setattr(dentry, &iattr);
908 if (err)
909 return err;
910 }
911
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200912 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200913 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200914 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200915
916 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200917
918out_acl_release:
919 posix_acl_release(acl);
920 return err;
921}
922
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200923static int ovl_own_xattr_get(const struct xattr_handler *handler,
924 struct dentry *dentry, struct inode *inode,
925 const char *name, void *buffer, size_t size)
926{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200927 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200928}
929
Miklos Szeredid837a492016-07-29 12:05:24 +0200930static int ovl_own_xattr_set(const struct xattr_handler *handler,
931 struct dentry *dentry, struct inode *inode,
932 const char *name, const void *value,
933 size_t size, int flags)
934{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200935 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200936}
937
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200938static int ovl_other_xattr_get(const struct xattr_handler *handler,
939 struct dentry *dentry, struct inode *inode,
940 const char *name, void *buffer, size_t size)
941{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200942 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200943}
944
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200945static int ovl_other_xattr_set(const struct xattr_handler *handler,
946 struct dentry *dentry, struct inode *inode,
947 const char *name, const void *value,
948 size_t size, int flags)
949{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200950 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200951}
952
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200953static const struct xattr_handler __maybe_unused
954ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200955 .name = XATTR_NAME_POSIX_ACL_ACCESS,
956 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200957 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200958 .set = ovl_posix_acl_xattr_set,
959};
960
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200961static const struct xattr_handler __maybe_unused
962ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200963 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
964 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200965 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200966 .set = ovl_posix_acl_xattr_set,
967};
968
969static const struct xattr_handler ovl_own_xattr_handler = {
970 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200971 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200972 .set = ovl_own_xattr_set,
973};
974
975static const struct xattr_handler ovl_other_xattr_handler = {
976 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200977 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200978 .set = ovl_other_xattr_set,
979};
980
981static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200982#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200983 &ovl_posix_acl_access_xattr_handler,
984 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200985#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200986 &ovl_own_xattr_handler,
987 &ovl_other_xattr_handler,
988 NULL
989};
990
Amir Goldstein146d62e2019-04-18 17:42:08 +0300991static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
992 struct inode **ptrap, const char *name)
993{
994 struct inode *trap;
995 int err;
996
997 trap = ovl_get_trap_inode(sb, dir);
998 err = PTR_ERR(trap);
999 if (IS_ERR(trap)) {
1000 if (err == -ELOOP)
1001 pr_err("overlayfs: conflicting %s path\n", name);
1002 return err;
1003 }
1004
1005 *ptrap = trap;
1006 return 0;
1007}
1008
1009static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
1010 struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001011{
Miklos Szeredi50649752017-11-10 09:39:15 +01001012 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001013 int err;
1014
Miklos Szerediad204482017-11-10 09:39:16 +01001015 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001016 if (err)
1017 goto out;
1018
1019 /* Upper fs should not be r/o */
1020 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
1021 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1022 err = -EINVAL;
1023 goto out;
1024 }
1025
Miklos Szerediad204482017-11-10 09:39:16 +01001026 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001027 if (err)
1028 goto out;
1029
Amir Goldstein146d62e2019-04-18 17:42:08 +03001030 err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap,
1031 "upperdir");
1032 if (err)
1033 goto out;
1034
Miklos Szeredi50649752017-11-10 09:39:15 +01001035 upper_mnt = clone_private_mount(upperpath);
1036 err = PTR_ERR(upper_mnt);
1037 if (IS_ERR(upper_mnt)) {
1038 pr_err("overlayfs: failed to clone upperpath\n");
1039 goto out;
1040 }
1041
1042 /* Don't inherit atime flags */
1043 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szerediad204482017-11-10 09:39:16 +01001044 ofs->upper_mnt = upper_mnt;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001045
1046 err = -EBUSY;
1047 if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
1048 ofs->upperdir_locked = true;
1049 } else if (ofs->config.index) {
1050 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
1051 goto out;
1052 } else {
1053 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1054 }
1055
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001056 err = 0;
1057out:
1058 return err;
1059}
1060
Amir Goldstein146d62e2019-04-18 17:42:08 +03001061static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1062 struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001063{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001064 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001065 struct dentry *temp;
Amir Goldsteine487d882017-11-07 13:55:04 +02001066 int fh_type;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001067 int err;
1068
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001069 err = mnt_want_write(mnt);
1070 if (err)
1071 return err;
1072
Miklos Szerediad204482017-11-10 09:39:16 +01001073 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1074 if (!ofs->workdir)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001075 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001076
Amir Goldstein146d62e2019-04-18 17:42:08 +03001077 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1078 if (err)
1079 goto out;
1080
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001081 /*
1082 * Upper should support d_type, else whiteouts are visible. Given
1083 * workdir and upper are on same fs, we can do iterate_dir() on
1084 * workdir. This check requires successful creation of workdir in
1085 * previous step.
1086 */
1087 err = ovl_check_d_type_supported(workpath);
1088 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001089 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001090
1091 /*
1092 * We allowed this configuration and don't want to break users over
1093 * kernel upgrade. So warn instead of erroring out.
1094 */
1095 if (!err)
1096 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1097
1098 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +01001099 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1100 ofs->tmpfile = !IS_ERR(temp);
1101 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001102 dput(temp);
1103 else
1104 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1105
1106 /*
1107 * Check if upper/work fs supports trusted.overlay.* xattr
1108 */
Miklos Szerediad204482017-11-10 09:39:16 +01001109 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001110 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +01001111 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +03001112 ofs->config.index = false;
Vivek Goyald5791042018-05-11 11:49:27 -04001113 ofs->config.metacopy = false;
1114 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 +02001115 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001116 } else {
Miklos Szerediad204482017-11-10 09:39:16 +01001117 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001118 }
1119
1120 /* Check if upper/work fs supports file handles */
Amir Goldsteine487d882017-11-07 13:55:04 +02001121 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1122 if (ofs->config.index && !fh_type) {
Miklos Szerediad204482017-11-10 09:39:16 +01001123 ofs->config.index = false;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001124 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1125 }
1126
Amir Goldsteine487d882017-11-07 13:55:04 +02001127 /* Check if upper fs has 32bit inode numbers */
1128 if (fh_type != FILEID_INO32_GEN)
1129 ofs->xino_bits = 0;
1130
Amir Goldsteinf168f102018-01-19 11:26:53 +02001131 /* NFS export of r/w mount depends on index */
1132 if (ofs->config.nfs_export && !ofs->config.index) {
1133 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1134 ofs->config.nfs_export = false;
1135 }
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001136out:
1137 mnt_drop_write(mnt);
1138 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001139}
1140
Amir Goldstein146d62e2019-04-18 17:42:08 +03001141static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1142 struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001143{
1144 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001145 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001146
Miklos Szerediad204482017-11-10 09:39:16 +01001147 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001148 if (err)
1149 goto out;
1150
1151 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001152 if (upperpath->mnt != workpath.mnt) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001153 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1154 goto out;
1155 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001156 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001157 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1158 goto out;
1159 }
1160
Miklos Szeredi8c257412018-09-10 11:43:29 +02001161 ofs->workbasedir = dget(workpath.dentry);
1162
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001163 err = -EBUSY;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001164 if (ovl_inuse_trylock(ofs->workbasedir)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001165 ofs->workdir_locked = true;
1166 } else if (ofs->config.index) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001167 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1168 goto out;
1169 } else {
1170 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1171 }
1172
Amir Goldstein146d62e2019-04-18 17:42:08 +03001173 err = ovl_make_workdir(sb, ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001174
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001175out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001176 path_put(&workpath);
1177
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001178 return err;
1179}
1180
Amir Goldstein146d62e2019-04-18 17:42:08 +03001181static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1182 struct ovl_entry *oe, struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001183{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001184 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001185 int err;
1186
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001187 err = mnt_want_write(mnt);
1188 if (err)
1189 return err;
1190
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001191 /* Verify lower root is upper root origin */
Amir Goldsteind9768072017-09-24 13:00:19 +03001192 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
Amir Goldstein05122442018-01-11 08:25:32 +02001193 true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001194 if (err) {
1195 pr_err("overlayfs: failed to verify upper root origin\n");
1196 goto out;
1197 }
1198
Miklos Szerediad204482017-11-10 09:39:16 +01001199 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1200 if (ofs->indexdir) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001201 err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1202 "indexdir");
1203 if (err)
1204 goto out;
1205
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001206 /*
1207 * Verify upper root is exclusively associated with index dir.
1208 * Older kernels stored upper fh in "trusted.overlay.origin"
1209 * xattr. If that xattr exists, verify that it is a match to
1210 * upper dir file handle. In any case, verify or set xattr
1211 * "trusted.overlay.upper" to indicate that index may have
1212 * directory entries.
1213 */
1214 if (ovl_check_origin_xattr(ofs->indexdir)) {
1215 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1216 upperpath->dentry, true, false);
1217 if (err)
1218 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1219 }
1220 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001221 if (err)
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001222 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001223
1224 /* Cleanup bad/stale/orphan index entries */
1225 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001226 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001227 }
Miklos Szerediad204482017-11-10 09:39:16 +01001228 if (err || !ofs->indexdir)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001229 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1230
1231out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001232 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001233 return err;
1234}
1235
Amir Goldstein9df085f2018-09-03 09:12:09 +03001236static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
Amir Goldstein51486262018-03-28 20:22:41 +03001237{
1238 unsigned int i;
Amir Goldstein9df085f2018-09-03 09:12:09 +03001239
1240 if (!ofs->config.nfs_export && !(ofs->config.index && ofs->upper_mnt))
1241 return true;
1242
1243 for (i = 0; i < ofs->numlowerfs; i++) {
1244 /*
1245 * We use uuid to associate an overlay lower file handle with a
1246 * lower layer, so we can accept lower fs with null uuid as long
1247 * as all lower layers with null uuid are on the same fs.
1248 */
1249 if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid))
1250 return false;
1251 }
1252 return true;
1253}
1254
1255/* Get a unique fsid for the layer */
1256static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1257{
1258 struct super_block *sb = path->mnt->mnt_sb;
1259 unsigned int i;
Amir Goldstein51486262018-03-28 20:22:41 +03001260 dev_t dev;
1261 int err;
1262
1263 /* fsid 0 is reserved for upper fs even with non upper overlay */
1264 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1265 return 0;
1266
1267 for (i = 0; i < ofs->numlowerfs; i++) {
1268 if (ofs->lower_fs[i].sb == sb)
1269 return i + 1;
1270 }
1271
Amir Goldstein9df085f2018-09-03 09:12:09 +03001272 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1273 ofs->config.index = false;
1274 ofs->config.nfs_export = false;
1275 pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1276 uuid_is_null(&sb->s_uuid) ? "null" : "conflicting",
1277 path->dentry);
1278 }
1279
Amir Goldstein51486262018-03-28 20:22:41 +03001280 err = get_anon_bdev(&dev);
1281 if (err) {
1282 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1283 return err;
1284 }
1285
1286 ofs->lower_fs[ofs->numlowerfs].sb = sb;
1287 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1288 ofs->numlowerfs++;
1289
1290 return ofs->numlowerfs;
1291}
1292
Amir Goldstein146d62e2019-04-18 17:42:08 +03001293static int ovl_get_lower_layers(struct super_block *sb, struct ovl_fs *ofs,
1294 struct path *stack, unsigned int numlower)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001295{
1296 int err;
1297 unsigned int i;
1298
1299 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001300 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001301 GFP_KERNEL);
Miklos Szerediad204482017-11-10 09:39:16 +01001302 if (ofs->lower_layers == NULL)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001303 goto out;
Amir Goldstein51486262018-03-28 20:22:41 +03001304
1305 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1306 GFP_KERNEL);
1307 if (ofs->lower_fs == NULL)
1308 goto out;
1309
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001310 for (i = 0; i < numlower; i++) {
1311 struct vfsmount *mnt;
Amir Goldstein146d62e2019-04-18 17:42:08 +03001312 struct inode *trap;
Amir Goldstein51486262018-03-28 20:22:41 +03001313 int fsid;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001314
Amir Goldstein9df085f2018-09-03 09:12:09 +03001315 err = fsid = ovl_get_fsid(ofs, &stack[i]);
Amir Goldstein51486262018-03-28 20:22:41 +03001316 if (err < 0)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001317 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001318
Amir Goldstein146d62e2019-04-18 17:42:08 +03001319 err = -EBUSY;
1320 if (ovl_is_inuse(stack[i].dentry)) {
1321 pr_err("overlayfs: lowerdir is in-use as upperdir/workdir\n");
1322 goto out;
1323 }
1324
1325 err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1326 if (err)
1327 goto out;
1328
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001329 mnt = clone_private_mount(&stack[i]);
1330 err = PTR_ERR(mnt);
1331 if (IS_ERR(mnt)) {
1332 pr_err("overlayfs: failed to clone lowerpath\n");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001333 iput(trap);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001334 goto out;
1335 }
Amir Goldstein51486262018-03-28 20:22:41 +03001336
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001337 /*
1338 * Make lower layers R/O. That way fchmod/fchown on lower file
1339 * will fail instead of modifying lower fs.
1340 */
1341 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1342
Amir Goldstein146d62e2019-04-18 17:42:08 +03001343 ofs->lower_layers[ofs->numlower].trap = trap;
Miklos Szerediad204482017-11-10 09:39:16 +01001344 ofs->lower_layers[ofs->numlower].mnt = mnt;
Amir Goldsteind583ed72017-11-08 19:23:36 +02001345 ofs->lower_layers[ofs->numlower].idx = i + 1;
Amir Goldstein51486262018-03-28 20:22:41 +03001346 ofs->lower_layers[ofs->numlower].fsid = fsid;
1347 if (fsid) {
1348 ofs->lower_layers[ofs->numlower].fs =
1349 &ofs->lower_fs[fsid - 1];
1350 }
Miklos Szerediad204482017-11-10 09:39:16 +01001351 ofs->numlower++;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001352 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001353
Amir Goldstein795939a2018-03-29 09:08:18 +03001354 /*
1355 * When all layers on same fs, overlay can use real inode numbers.
1356 * With mount option "xino=on", mounter declares that there are enough
1357 * free high bits in underlying fs to hold the unique fsid.
1358 * If overlayfs does encounter underlying inodes using the high xino
1359 * bits reserved for fsid, it emits a warning and uses the original
1360 * inode number.
1361 */
1362 if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
Amir Goldsteine487d882017-11-07 13:55:04 +02001363 ofs->xino_bits = 0;
Amir Goldstein795939a2018-03-29 09:08:18 +03001364 ofs->config.xino = OVL_XINO_OFF;
1365 } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1366 /*
1367 * This is a roundup of number of bits needed for numlowerfs+1
1368 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
1369 * upper fs even with non upper overlay.
1370 */
1371 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1372 ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1373 }
1374
1375 if (ofs->xino_bits) {
1376 pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1377 ofs->xino_bits);
1378 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001379
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001380 err = 0;
1381out:
1382 return err;
1383}
1384
Miklos Szeredi4155c102017-11-10 09:39:15 +01001385static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szerediad204482017-11-10 09:39:16 +01001386 struct ovl_fs *ofs)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001387{
1388 int err;
1389 char *lowertmp, *lower;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001390 struct path *stack = NULL;
1391 unsigned int stacklen, numlower = 0, i;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001392 bool remote = false;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001393 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001394
1395 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001396 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001397 if (!lowertmp)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001398 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001399
1400 err = -EINVAL;
1401 stacklen = ovl_split_lowerdirs(lowertmp);
1402 if (stacklen > OVL_MAX_STACK) {
1403 pr_err("overlayfs: too many lower directories, limit is %d\n",
1404 OVL_MAX_STACK);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001405 goto out_err;
Miklos Szerediad204482017-11-10 09:39:16 +01001406 } else if (!ofs->config.upperdir && stacklen == 1) {
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001407 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001408 goto out_err;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001409 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1410 ofs->config.redirect_follow) {
1411 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1412 ofs->config.nfs_export = false;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001413 }
1414
1415 err = -ENOMEM;
1416 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1417 if (!stack)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001418 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001419
1420 err = -EINVAL;
1421 lower = lowertmp;
1422 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szerediad204482017-11-10 09:39:16 +01001423 err = ovl_lower_dir(lower, &stack[numlower], ofs,
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001424 &sb->s_stack_depth, &remote);
1425 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001426 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001427
1428 lower = strchr(lower, '\0') + 1;
1429 }
1430
1431 err = -EINVAL;
1432 sb->s_stack_depth++;
1433 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1434 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001435 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001436 }
1437
Amir Goldstein146d62e2019-04-18 17:42:08 +03001438 err = ovl_get_lower_layers(sb, ofs, stack, numlower);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001439 if (err)
1440 goto out_err;
1441
1442 err = -ENOMEM;
1443 oe = ovl_alloc_entry(numlower);
1444 if (!oe)
1445 goto out_err;
1446
1447 for (i = 0; i < numlower; i++) {
1448 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Miklos Szerediad204482017-11-10 09:39:16 +01001449 oe->lowerstack[i].layer = &ofs->lower_layers[i];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001450 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001451
1452 if (remote)
1453 sb->s_d_op = &ovl_reval_dentry_operations;
1454 else
1455 sb->s_d_op = &ovl_dentry_operations;
1456
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001457out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001458 for (i = 0; i < numlower; i++)
1459 path_put(&stack[i]);
1460 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001461 kfree(lowertmp);
1462
1463 return oe;
1464
1465out_err:
1466 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001467 goto out;
1468}
1469
Amir Goldstein146d62e2019-04-18 17:42:08 +03001470/*
1471 * Check if this layer root is a descendant of:
1472 * - another layer of this overlayfs instance
1473 * - upper/work dir of any overlayfs instance
1474 * - a disconnected dentry (detached root)
1475 */
1476static int ovl_check_layer(struct super_block *sb, struct dentry *dentry,
1477 const char *name)
1478{
1479 struct dentry *next, *parent;
1480 bool is_root = false;
1481 int err = 0;
1482
1483 if (!dentry || dentry == dentry->d_sb->s_root)
1484 return 0;
1485
1486 next = dget(dentry);
1487 /* Walk back ancestors to fs root (inclusive) looking for traps */
1488 do {
1489 parent = dget_parent(next);
1490 is_root = (parent == next);
1491 if (ovl_is_inuse(parent)) {
1492 err = -EBUSY;
1493 pr_err("overlayfs: %s path overlapping in-use upperdir/workdir\n",
1494 name);
1495 } else if (ovl_lookup_trap_inode(sb, parent)) {
1496 err = -ELOOP;
1497 pr_err("overlayfs: overlapping %s path\n", name);
1498 }
1499 dput(next);
1500 next = parent;
1501 } while (!err && !is_root);
1502
1503 /* Did we really walk to fs root or found a detached root? */
1504 if (!err && next != dentry->d_sb->s_root) {
1505 err = -ESTALE;
1506 pr_err("overlayfs: disconnected %s path\n", name);
1507 }
1508
1509 dput(next);
1510
1511 return err;
1512}
1513
1514/*
1515 * Check if any of the layers or work dirs overlap.
1516 */
1517static int ovl_check_overlapping_layers(struct super_block *sb,
1518 struct ovl_fs *ofs)
1519{
1520 int i, err;
1521
1522 if (ofs->upper_mnt) {
1523 err = ovl_check_layer(sb, ofs->upper_mnt->mnt_root, "upperdir");
1524 if (err)
1525 return err;
1526
1527 /*
1528 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1529 * this instance and covers overlapping work and index dirs,
1530 * unless work or index dir have been moved since created inside
1531 * workbasedir. In that case, we already have their traps in
1532 * inode cache and we will catch that case on lookup.
1533 */
1534 err = ovl_check_layer(sb, ofs->workbasedir, "workdir");
1535 if (err)
1536 return err;
1537 }
1538
1539 for (i = 0; i < ofs->numlower; i++) {
1540 err = ovl_check_layer(sb, ofs->lower_layers[i].mnt->mnt_root,
1541 "lowerdir");
1542 if (err)
1543 return err;
1544 }
1545
1546 return 0;
1547}
1548
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001549static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1550{
Kees Cook33006cd2017-03-29 14:02:19 -07001551 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001552 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001553 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001554 struct ovl_fs *ofs;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001555 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001556 int err;
1557
Erez Zadokf45827e82014-10-24 00:14:38 +02001558 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001559 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1560 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001561 goto out;
1562
Miklos Szerediad204482017-11-10 09:39:16 +01001563 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001564 if (!cred)
1565 goto out_err;
1566
Miklos Szerediad204482017-11-10 09:39:16 +01001567 ofs->config.index = ovl_index_def;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001568 ofs->config.nfs_export = ovl_nfs_export_def;
Amir Goldstein795939a2018-03-29 09:08:18 +03001569 ofs->config.xino = ovl_xino_def();
Vivek Goyald5791042018-05-11 11:49:27 -04001570 ofs->config.metacopy = ovl_metacopy_def;
Miklos Szerediad204482017-11-10 09:39:16 +01001571 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001572 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001573 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001574
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001575 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001576 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001577 if (!silent)
1578 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001579 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001580 }
1581
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001582 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001583 sb->s_maxbytes = MAX_LFS_FILESIZE;
Amir Goldsteine487d882017-11-07 13:55:04 +02001584 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
Amir Goldstein795939a2018-03-29 09:08:18 +03001585 if (ofs->config.xino != OVL_XINO_OFF)
1586 ofs->xino_bits = BITS_PER_LONG - 32;
1587
Amir Goldstein146d62e2019-04-18 17:42:08 +03001588 /* alloc/destroy_inode needed for setting up traps in inode cache */
1589 sb->s_op = &ovl_super_operations;
1590
Miklos Szerediad204482017-11-10 09:39:16 +01001591 if (ofs->config.upperdir) {
1592 if (!ofs->config.workdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001593 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001594 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001595 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001596
Amir Goldstein146d62e2019-04-18 17:42:08 +03001597 err = ovl_get_upper(sb, ofs, &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001598 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001599 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001600
Amir Goldstein146d62e2019-04-18 17:42:08 +03001601 err = ovl_get_workdir(sb, ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001602 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001603 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001604
Miklos Szerediad204482017-11-10 09:39:16 +01001605 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001606 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001607
Miklos Szerediad204482017-11-10 09:39:16 +01001608 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1609 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001610
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001611 }
Miklos Szerediad204482017-11-10 09:39:16 +01001612 oe = ovl_get_lowerstack(sb, ofs);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001613 err = PTR_ERR(oe);
1614 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001615 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001616
hujianyang71cbad72015-01-15 13:20:57 +08001617 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szerediad204482017-11-10 09:39:16 +01001618 if (!ofs->upper_mnt)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001619 sb->s_flags |= SB_RDONLY;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001620
Miklos Szerediad204482017-11-10 09:39:16 +01001621 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001622 err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001623 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001624 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001625
Amir Goldstein972d0092017-09-19 12:14:18 +03001626 /* Force r/o mount with no index dir */
1627 if (!ofs->indexdir) {
1628 dput(ofs->workdir);
1629 ofs->workdir = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001630 sb->s_flags |= SB_RDONLY;
Amir Goldstein972d0092017-09-19 12:14:18 +03001631 }
1632
Amir Goldstein02bcd152017-06-21 15:28:36 +03001633 }
1634
Amir Goldstein146d62e2019-04-18 17:42:08 +03001635 err = ovl_check_overlapping_layers(sb, ofs);
1636 if (err)
1637 goto out_free_oe;
1638
Amir Goldstein972d0092017-09-19 12:14:18 +03001639 /* Show index=off in /proc/mounts for forced r/o mount */
Amir Goldsteinf168f102018-01-19 11:26:53 +02001640 if (!ofs->indexdir) {
Miklos Szerediad204482017-11-10 09:39:16 +01001641 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001642 if (ofs->upper_mnt && ofs->config.nfs_export) {
1643 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1644 ofs->config.nfs_export = false;
1645 }
1646 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001647
Vivek Goyald5791042018-05-11 11:49:27 -04001648 if (ofs->config.metacopy && ofs->config.nfs_export) {
1649 pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1650 ofs->config.nfs_export = false;
1651 }
1652
Amir Goldstein8383f172017-10-02 11:31:42 +03001653 if (ofs->config.nfs_export)
1654 sb->s_export_op = &ovl_export_operations;
1655
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001656 /* Never override disk quota limits or use reserved space */
1657 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1658
Vivek Goyal655042c2016-10-14 03:03:36 +02001659 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Vivek Goyal655042c2016-10-14 03:03:36 +02001660 sb->s_xattr = ovl_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01001661 sb->s_fs_info = ofs;
Miklos Szeredide2a4a52018-07-18 15:44:43 +02001662 sb->s_flags |= SB_POSIXACL;
Vivek Goyal655042c2016-10-14 03:03:36 +02001663
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001664 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001665 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001666 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001667 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001668
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001669 root_dentry->d_fsdata = oe;
1670
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001671 mntput(upperpath.mnt);
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001672 if (upperpath.dentry) {
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001673 ovl_dentry_set_upper_alias(root_dentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +02001674 if (ovl_is_impuredir(upperpath.dentry))
1675 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001676 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001677
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001678 /* Root is always merge -> can have whiteouts */
1679 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Amir Goldstein2ca3c142018-01-30 13:31:09 +02001680 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
Vivek Goyal0c288872018-05-11 11:49:28 -04001681 ovl_set_upperdata(d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001682 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
Vivek Goyal2664bd02018-05-11 11:49:30 -04001683 ovl_dentry_lower(root_dentry), NULL);
Miklos Szeredied06e062015-12-09 16:11:59 +01001684
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001685 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001686
1687 return 0;
1688
Miklos Szeredi4155c102017-11-10 09:39:15 +01001689out_free_oe:
1690 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001691 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001692out_err:
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001693 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01001694 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001695out:
1696 return err;
1697}
1698
1699static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1700 const char *dev_name, void *raw_data)
1701{
1702 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1703}
1704
1705static struct file_system_type ovl_fs_type = {
1706 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001707 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001708 .mount = ovl_mount,
1709 .kill_sb = kill_anon_super,
1710};
Miklos Szeredief94b182014-11-20 16:39:59 +01001711MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001712
Amir Goldstein13cf1992017-06-12 09:54:40 +03001713static void ovl_inode_init_once(void *foo)
1714{
1715 struct ovl_inode *oi = foo;
1716
1717 inode_init_once(&oi->vfs_inode);
1718}
1719
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001720static int __init ovl_init(void)
1721{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001722 int err;
1723
1724 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1725 sizeof(struct ovl_inode), 0,
1726 (SLAB_RECLAIM_ACCOUNT|
1727 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1728 ovl_inode_init_once);
1729 if (ovl_inode_cachep == NULL)
1730 return -ENOMEM;
1731
1732 err = register_filesystem(&ovl_fs_type);
1733 if (err)
1734 kmem_cache_destroy(ovl_inode_cachep);
1735
1736 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001737}
1738
1739static void __exit ovl_exit(void)
1740{
1741 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001742
1743 /*
1744 * Make sure all delayed rcu free inodes are flushed before we
1745 * destroy cache.
1746 */
1747 rcu_barrier();
1748 kmem_cache_destroy(ovl_inode_cachep);
1749
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001750}
1751
1752module_init(ovl_init);
1753module_exit(ovl_exit);