blob: 0e84593af84e0df8014af1e468a504dd41b5639f [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
Miklos Szeredie9be9d52014-10-24 00:14:38 +020067static void ovl_dentry_release(struct dentry *dentry)
68{
69 struct ovl_entry *oe = dentry->d_fsdata;
70
71 if (oe) {
Miklos Szeredi4155c102017-11-10 09:39:15 +010072 ovl_entry_stack_free(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020073 kfree_rcu(oe, rcu);
74 }
75}
76
Miklos Szeredi2d902672016-06-30 08:53:27 +020077static struct dentry *ovl_d_real(struct dentry *dentry,
Miklos Szeredifb160432018-07-18 15:44:44 +020078 const struct inode *inode)
Miklos Szeredid101a122016-03-26 16:14:37 -040079{
80 struct dentry *real;
81
Miklos Szeredie8c985b2018-07-18 15:44:41 +020082 /* It's an overlay file */
83 if (inode && d_inode(dentry) == inode)
84 return dentry;
85
Miklos Szeredica4c8a32016-12-16 11:02:55 +010086 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040087 if (!inode || inode == d_inode(dentry))
88 return dentry;
89 goto bug;
90 }
91
92 real = ovl_dentry_upper(dentry);
Miklos Szeredifb160432018-07-18 15:44:44 +020093 if (real && (!inode || inode == d_inode(real)))
Miklos Szeredid101a122016-03-26 16:14:37 -040094 return real;
95
96 real = ovl_dentry_lower(dentry);
97 if (!real)
98 goto bug;
99
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100100 /* Handle recursion */
Miklos Szeredifb160432018-07-18 15:44:44 +0200101 real = d_real(real, inode);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100102
Miklos Szeredid101a122016-03-26 16:14:37 -0400103 if (!inode || inode == d_inode(real))
104 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400105bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200106 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400107 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
108 return dentry;
109}
110
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200111static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
112{
113 struct ovl_entry *oe = dentry->d_fsdata;
114 unsigned int i;
115 int ret = 1;
116
117 for (i = 0; i < oe->numlower; i++) {
118 struct dentry *d = oe->lowerstack[i].dentry;
119
120 if (d->d_flags & DCACHE_OP_REVALIDATE) {
121 ret = d->d_op->d_revalidate(d, flags);
122 if (ret < 0)
123 return ret;
124 if (!ret) {
125 if (!(flags & LOOKUP_RCU))
126 d_invalidate(d);
127 return -ESTALE;
128 }
129 }
130 }
131 return 1;
132}
133
134static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
135{
136 struct ovl_entry *oe = dentry->d_fsdata;
137 unsigned int i;
138 int ret = 1;
139
140 for (i = 0; i < oe->numlower; i++) {
141 struct dentry *d = oe->lowerstack[i].dentry;
142
143 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
144 ret = d->d_op->d_weak_revalidate(d, flags);
145 if (ret <= 0)
146 break;
147 }
148 }
149 return ret;
150}
151
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200152static const struct dentry_operations ovl_dentry_operations = {
153 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400154 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200155};
156
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200157static const struct dentry_operations ovl_reval_dentry_operations = {
158 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400159 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200160 .d_revalidate = ovl_dentry_revalidate,
161 .d_weak_revalidate = ovl_dentry_weak_revalidate,
162};
163
Amir Goldstein13cf1992017-06-12 09:54:40 +0300164static struct kmem_cache *ovl_inode_cachep;
165
166static struct inode *ovl_alloc_inode(struct super_block *sb)
167{
168 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
169
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900170 if (!oi)
171 return NULL;
172
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200173 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200174 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200175 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200176 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200177 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200178 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300179 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200180
Amir Goldstein13cf1992017-06-12 09:54:40 +0300181 return &oi->vfs_inode;
182}
183
184static void ovl_i_callback(struct rcu_head *head)
185{
186 struct inode *inode = container_of(head, struct inode, i_rcu);
187
188 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
189}
190
191static void ovl_destroy_inode(struct inode *inode)
192{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200193 struct ovl_inode *oi = OVL_I(inode);
194
195 dput(oi->__upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200196 iput(oi->lower);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200197 kfree(oi->redirect);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200198 ovl_dir_cache_free(inode);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300199 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200200
Amir Goldstein13cf1992017-06-12 09:54:40 +0300201 call_rcu(&inode->i_rcu, ovl_i_callback);
202}
203
Miklos Szerediad204482017-11-10 09:39:16 +0100204static void ovl_free_fs(struct ovl_fs *ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200205{
Miklos Szeredidd662662014-12-13 00:59:43 +0100206 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207
Miklos Szerediad204482017-11-10 09:39:16 +0100208 dput(ofs->indexdir);
209 dput(ofs->workdir);
210 if (ofs->workdir_locked)
211 ovl_inuse_unlock(ofs->workbasedir);
212 dput(ofs->workbasedir);
213 if (ofs->upperdir_locked)
214 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
215 mntput(ofs->upper_mnt);
Amir Goldstein51486262018-03-28 20:22:41 +0300216 for (i = 0; i < ofs->numlower; i++)
Miklos Szerediad204482017-11-10 09:39:16 +0100217 mntput(ofs->lower_layers[i].mnt);
Amir Goldstein51486262018-03-28 20:22:41 +0300218 for (i = 0; i < ofs->numlowerfs; i++)
219 free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
Miklos Szerediad204482017-11-10 09:39:16 +0100220 kfree(ofs->lower_layers);
Amir Goldstein51486262018-03-28 20:22:41 +0300221 kfree(ofs->lower_fs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200222
Miklos Szerediad204482017-11-10 09:39:16 +0100223 kfree(ofs->config.lowerdir);
224 kfree(ofs->config.upperdir);
225 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100226 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100227 if (ofs->creator_cred)
228 put_cred(ofs->creator_cred);
229 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200230}
231
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100232static void ovl_put_super(struct super_block *sb)
233{
234 struct ovl_fs *ofs = sb->s_fs_info;
235
236 ovl_free_fs(ofs);
237}
238
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800239/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200240static int ovl_sync_fs(struct super_block *sb, int wait)
241{
Miklos Szerediad204482017-11-10 09:39:16 +0100242 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200243 struct super_block *upper_sb;
244 int ret;
245
Miklos Szerediad204482017-11-10 09:39:16 +0100246 if (!ofs->upper_mnt)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200247 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800248
249 /*
250 * If this is a sync(2) call or an emergency sync, all the super blocks
251 * will be iterated, including upper_sb, so no need to do anything.
252 *
253 * If this is a syncfs(2) call, then we do need to call
254 * sync_filesystem() on upper_sb, but enough if we do it when being
255 * called with wait == 1.
256 */
257 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200258 return 0;
259
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800260 upper_sb = ofs->upper_mnt->mnt_sb;
261
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200262 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800263 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200264 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800265
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200266 return ret;
267}
268
Andy Whitcroftcc259632014-10-24 00:14:38 +0200269/**
270 * ovl_statfs
271 * @sb: The overlayfs super block
272 * @buf: The struct kstatfs to fill in with stats
273 *
274 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100275 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200276 */
277static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
278{
279 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
280 struct dentry *root_dentry = dentry->d_sb->s_root;
281 struct path path;
282 int err;
283
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100284 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200285
286 err = vfs_statfs(&path, buf);
287 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100288 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200289 buf->f_type = OVERLAYFS_SUPER_MAGIC;
290 }
291
292 return err;
293}
294
Amir Goldstein02bcd152017-06-21 15:28:36 +0300295/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100296static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300297{
Miklos Szerediad204482017-11-10 09:39:16 +0100298 return (!ofs->upper_mnt || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300299}
300
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100301static const char *ovl_redirect_mode_def(void)
302{
303 return ovl_redirect_dir_def ? "on" : "off";
304}
305
Amir Goldstein795939a2018-03-29 09:08:18 +0300306enum {
307 OVL_XINO_OFF,
308 OVL_XINO_AUTO,
309 OVL_XINO_ON,
310};
311
312static const char * const ovl_xino_str[] = {
313 "off",
314 "auto",
315 "on",
316};
317
318static inline int ovl_xino_def(void)
319{
320 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
321}
322
Erez Zadokf45827e82014-10-24 00:14:38 +0200323/**
324 * ovl_show_options
325 *
326 * Prints the mount options for a given superblock.
327 * Returns zero; does not fail.
328 */
329static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
330{
331 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100332 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200333
Miklos Szerediad204482017-11-10 09:39:16 +0100334 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
335 if (ofs->config.upperdir) {
336 seq_show_option(m, "upperdir", ofs->config.upperdir);
337 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100338 }
Miklos Szerediad204482017-11-10 09:39:16 +0100339 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200340 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100341 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
342 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100343 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100344 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Amir Goldsteinf168f102018-01-19 11:26:53 +0200345 if (ofs->config.nfs_export != ovl_nfs_export_def)
346 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
347 "on" : "off");
Amir Goldstein795939a2018-03-29 09:08:18 +0300348 if (ofs->config.xino != ovl_xino_def())
349 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
Erez Zadokf45827e82014-10-24 00:14:38 +0200350 return 0;
351}
352
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900353static int ovl_remount(struct super_block *sb, int *flags, char *data)
354{
Miklos Szerediad204482017-11-10 09:39:16 +0100355 struct ovl_fs *ofs = sb->s_fs_info;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900356
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800357 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900358 return -EROFS;
359
360 return 0;
361}
362
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200363static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300364 .alloc_inode = ovl_alloc_inode,
365 .destroy_inode = ovl_destroy_inode,
366 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200367 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200368 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200369 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200370 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900371 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200372};
373
374enum {
375 OPT_LOWERDIR,
376 OPT_UPPERDIR,
377 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200378 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100379 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300380 OPT_INDEX_ON,
381 OPT_INDEX_OFF,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200382 OPT_NFS_EXPORT_ON,
383 OPT_NFS_EXPORT_OFF,
Amir Goldstein795939a2018-03-29 09:08:18 +0300384 OPT_XINO_ON,
385 OPT_XINO_OFF,
386 OPT_XINO_AUTO,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200387 OPT_ERR,
388};
389
390static const match_table_t ovl_tokens = {
391 {OPT_LOWERDIR, "lowerdir=%s"},
392 {OPT_UPPERDIR, "upperdir=%s"},
393 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200394 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100395 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300396 {OPT_INDEX_ON, "index=on"},
397 {OPT_INDEX_OFF, "index=off"},
Amir Goldsteinf168f102018-01-19 11:26:53 +0200398 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
399 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
Amir Goldstein795939a2018-03-29 09:08:18 +0300400 {OPT_XINO_ON, "xino=on"},
401 {OPT_XINO_OFF, "xino=off"},
402 {OPT_XINO_AUTO, "xino=auto"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200403 {OPT_ERR, NULL}
404};
405
Miklos Szeredi91c77942014-11-20 16:40:00 +0100406static char *ovl_next_opt(char **s)
407{
408 char *sbegin = *s;
409 char *p;
410
411 if (sbegin == NULL)
412 return NULL;
413
414 for (p = sbegin; *p; p++) {
415 if (*p == '\\') {
416 p++;
417 if (!*p)
418 break;
419 } else if (*p == ',') {
420 *p = '\0';
421 *s = p + 1;
422 return sbegin;
423 }
424 }
425 *s = NULL;
426 return sbegin;
427}
428
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100429static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
430{
431 if (strcmp(mode, "on") == 0) {
432 config->redirect_dir = true;
433 /*
434 * Does not make sense to have redirect creation without
435 * redirect following.
436 */
437 config->redirect_follow = true;
438 } else if (strcmp(mode, "follow") == 0) {
439 config->redirect_follow = true;
440 } else if (strcmp(mode, "off") == 0) {
441 if (ovl_redirect_always_follow)
442 config->redirect_follow = true;
443 } else if (strcmp(mode, "nofollow") != 0) {
444 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
445 mode);
446 return -EINVAL;
447 }
448
449 return 0;
450}
451
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200452static int ovl_parse_opt(char *opt, struct ovl_config *config)
453{
454 char *p;
455
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100456 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
457 if (!config->redirect_mode)
458 return -ENOMEM;
459
Miklos Szeredi91c77942014-11-20 16:40:00 +0100460 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200461 int token;
462 substring_t args[MAX_OPT_ARGS];
463
464 if (!*p)
465 continue;
466
467 token = match_token(p, ovl_tokens, args);
468 switch (token) {
469 case OPT_UPPERDIR:
470 kfree(config->upperdir);
471 config->upperdir = match_strdup(&args[0]);
472 if (!config->upperdir)
473 return -ENOMEM;
474 break;
475
476 case OPT_LOWERDIR:
477 kfree(config->lowerdir);
478 config->lowerdir = match_strdup(&args[0]);
479 if (!config->lowerdir)
480 return -ENOMEM;
481 break;
482
483 case OPT_WORKDIR:
484 kfree(config->workdir);
485 config->workdir = match_strdup(&args[0]);
486 if (!config->workdir)
487 return -ENOMEM;
488 break;
489
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200490 case OPT_DEFAULT_PERMISSIONS:
491 config->default_permissions = true;
492 break;
493
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100494 case OPT_REDIRECT_DIR:
495 kfree(config->redirect_mode);
496 config->redirect_mode = match_strdup(&args[0]);
497 if (!config->redirect_mode)
498 return -ENOMEM;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100499 break;
500
Amir Goldstein02bcd152017-06-21 15:28:36 +0300501 case OPT_INDEX_ON:
502 config->index = true;
503 break;
504
505 case OPT_INDEX_OFF:
506 config->index = false;
507 break;
508
Amir Goldsteinf168f102018-01-19 11:26:53 +0200509 case OPT_NFS_EXPORT_ON:
510 config->nfs_export = true;
511 break;
512
513 case OPT_NFS_EXPORT_OFF:
514 config->nfs_export = false;
515 break;
516
Amir Goldstein795939a2018-03-29 09:08:18 +0300517 case OPT_XINO_ON:
518 config->xino = OVL_XINO_ON;
519 break;
520
521 case OPT_XINO_OFF:
522 config->xino = OVL_XINO_OFF;
523 break;
524
525 case OPT_XINO_AUTO:
526 config->xino = OVL_XINO_AUTO;
527 break;
528
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200529 default:
hujianyangbead55e2015-01-15 13:17:36 +0800530 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200531 return -EINVAL;
532 }
533 }
hujianyang71cbad72015-01-15 13:20:57 +0800534
535 /* Workdir is useless in non-upper mount */
536 if (!config->upperdir && config->workdir) {
537 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
538 config->workdir);
539 kfree(config->workdir);
540 config->workdir = NULL;
541 }
542
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100543 return ovl_parse_redirect_mode(config, config->redirect_mode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544}
545
546#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300547#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200548
Miklos Szerediad204482017-11-10 09:39:16 +0100549static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300550 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200551{
Miklos Szerediad204482017-11-10 09:39:16 +0100552 struct inode *dir = ofs->workbasedir->d_inode;
553 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200554 struct dentry *work;
555 int err;
556 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300557 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200558
Al Viro59551022016-01-22 15:40:57 -0500559 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300560 locked = true;
561
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200562retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100563 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200564
565 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200566 struct iattr attr = {
567 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000568 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200569 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200570
571 if (work->d_inode) {
572 err = -EEXIST;
573 if (retried)
574 goto out_dput;
575
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300576 if (persist)
577 goto out_unlock;
578
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200579 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200580 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200581 dput(work);
582 goto retry;
583 }
584
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300585 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
586 err = PTR_ERR(work);
587 if (IS_ERR(work))
588 goto out_err;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200589
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200590 /*
591 * Try to remove POSIX ACL xattrs from workdir. We are good if:
592 *
593 * a) success (there was a POSIX ACL xattr and was removed)
594 * b) -ENODATA (there was no POSIX ACL xattr)
595 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
596 *
597 * There are various other error values that could effectively
598 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
599 * if the xattr name is too long), but the set of filesystems
600 * allowed as upper are limited to "normal" ones, where checking
601 * for the above two errors is sufficient.
602 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200603 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200604 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200605 goto out_dput;
606
607 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200608 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200609 goto out_dput;
610
611 /* Clear any inherited mode bits */
612 inode_lock(work->d_inode);
613 err = notify_change(work, &attr, NULL);
614 inode_unlock(work->d_inode);
615 if (err)
616 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300617 } else {
618 err = PTR_ERR(work);
619 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200620 }
621out_unlock:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300622 if (locked)
623 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200624
625 return work;
626
627out_dput:
628 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300629out_err:
630 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100631 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300632 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200633 goto out_unlock;
634}
635
Miklos Szeredi91c77942014-11-20 16:40:00 +0100636static void ovl_unescape(char *s)
637{
638 char *d = s;
639
640 for (;; s++, d++) {
641 if (*s == '\\')
642 s++;
643 *d = *s;
644 if (!*s)
645 break;
646 }
647}
648
Miklos Szerediab508822014-12-13 00:59:49 +0100649static int ovl_mount_dir_noesc(const char *name, struct path *path)
650{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100651 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100652
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100653 if (!*name) {
654 pr_err("overlayfs: empty lowerdir\n");
655 goto out;
656 }
Miklos Szerediab508822014-12-13 00:59:49 +0100657 err = kern_path(name, LOOKUP_FOLLOW, path);
658 if (err) {
659 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
660 goto out;
661 }
662 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200663 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100664 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
665 goto out_put;
666 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100667 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100668 pr_err("overlayfs: '%s' not a directory\n", name);
669 goto out_put;
670 }
671 return 0;
672
673out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100674 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100675out:
676 return err;
677}
678
679static int ovl_mount_dir(const char *name, struct path *path)
680{
681 int err = -ENOMEM;
682 char *tmp = kstrdup(name, GFP_KERNEL);
683
684 if (tmp) {
685 ovl_unescape(tmp);
686 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200687
688 if (!err)
689 if (ovl_dentry_remote(path->dentry)) {
690 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
691 tmp);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100692 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200693 err = -EINVAL;
694 }
Miklos Szerediab508822014-12-13 00:59:49 +0100695 kfree(tmp);
696 }
697 return err;
698}
699
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100700static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
701 const char *name)
702{
703 struct kstatfs statfs;
704 int err = vfs_statfs(path, &statfs);
705
706 if (err)
707 pr_err("overlayfs: statfs failed on '%s'\n", name);
708 else
709 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
710
711 return err;
712}
713
714static int ovl_lower_dir(const char *name, struct path *path,
715 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100716{
Amir Goldsteine487d882017-11-07 13:55:04 +0200717 int fh_type;
Miklos Szerediab508822014-12-13 00:59:49 +0100718 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100719
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100720 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100721 if (err)
722 goto out;
723
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100724 err = ovl_check_namelen(path, ofs, name);
725 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100726 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100727
Miklos Szerediab508822014-12-13 00:59:49 +0100728 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
729
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200730 if (ovl_dentry_remote(path->dentry))
731 *remote = true;
732
Amir Goldstein02bcd152017-06-21 15:28:36 +0300733 /*
Amir Goldsteinf168f102018-01-19 11:26:53 +0200734 * The inodes index feature and NFS export need to encode and decode
735 * file handles, so they require that all layers support them.
Amir Goldstein02bcd152017-06-21 15:28:36 +0300736 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200737 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
Amir Goldsteinf168f102018-01-19 11:26:53 +0200738 if ((ofs->config.nfs_export ||
Amir Goldsteine487d882017-11-07 13:55:04 +0200739 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300740 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200741 ofs->config.nfs_export = false;
742 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
743 name);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300744 }
745
Amir Goldsteine487d882017-11-07 13:55:04 +0200746 /* Check if lower fs has 32bit inode numbers */
747 if (fh_type != FILEID_INO32_GEN)
748 ofs->xino_bits = 0;
749
Miklos Szerediab508822014-12-13 00:59:49 +0100750 return 0;
751
752out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100753 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100754out:
755 return err;
756}
757
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200758/* Workdir should not be subdir of upperdir and vice versa */
759static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
760{
761 bool ok = false;
762
763 if (workdir != upperdir) {
764 ok = (lock_rename(workdir, upperdir) == NULL);
765 unlock_rename(workdir, upperdir);
766 }
767 return ok;
768}
769
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100770static unsigned int ovl_split_lowerdirs(char *str)
771{
772 unsigned int ctr = 1;
773 char *s, *d;
774
775 for (s = d = str;; s++, d++) {
776 if (*s == '\\') {
777 s++;
778 } else if (*s == ':') {
779 *d = '\0';
780 ctr++;
781 continue;
782 }
783 *d = *s;
784 if (!*s)
785 break;
786 }
787 return ctr;
788}
789
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200790static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200791ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
792 struct dentry *dentry, struct inode *inode,
793 const char *name, void *buffer, size_t size)
794{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200795 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200796}
797
798static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200799ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
800 struct dentry *dentry, struct inode *inode,
801 const char *name, const void *value,
802 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200803{
804 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200805 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200806 struct posix_acl *acl = NULL;
807 int err;
808
809 /* Check that everything is OK before copy-up */
810 if (value) {
811 acl = posix_acl_from_xattr(&init_user_ns, value, size);
812 if (IS_ERR(acl))
813 return PTR_ERR(acl);
814 }
815 err = -EOPNOTSUPP;
816 if (!IS_POSIXACL(d_inode(workdir)))
817 goto out_acl_release;
818 if (!realinode->i_op->set_acl)
819 goto out_acl_release;
820 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
821 err = acl ? -EACCES : 0;
822 goto out_acl_release;
823 }
824 err = -EPERM;
825 if (!inode_owner_or_capable(inode))
826 goto out_acl_release;
827
828 posix_acl_release(acl);
829
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100830 /*
831 * Check if sgid bit needs to be cleared (actual setacl operation will
832 * be done with mounter's capabilities and so that won't do it for us).
833 */
834 if (unlikely(inode->i_mode & S_ISGID) &&
835 handler->flags == ACL_TYPE_ACCESS &&
836 !in_group_p(inode->i_gid) &&
837 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
838 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
839
840 err = ovl_setattr(dentry, &iattr);
841 if (err)
842 return err;
843 }
844
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200845 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200846 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200847 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200848
849 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200850
851out_acl_release:
852 posix_acl_release(acl);
853 return err;
854}
855
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200856static int ovl_own_xattr_get(const struct xattr_handler *handler,
857 struct dentry *dentry, struct inode *inode,
858 const char *name, void *buffer, size_t size)
859{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200860 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200861}
862
Miklos Szeredid837a492016-07-29 12:05:24 +0200863static int ovl_own_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)
867{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200868 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200869}
870
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200871static int ovl_other_xattr_get(const struct xattr_handler *handler,
872 struct dentry *dentry, struct inode *inode,
873 const char *name, void *buffer, size_t size)
874{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200875 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200876}
877
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200878static int ovl_other_xattr_set(const struct xattr_handler *handler,
879 struct dentry *dentry, struct inode *inode,
880 const char *name, const void *value,
881 size_t size, int flags)
882{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200883 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200884}
885
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200886static const struct xattr_handler __maybe_unused
887ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200888 .name = XATTR_NAME_POSIX_ACL_ACCESS,
889 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200890 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200891 .set = ovl_posix_acl_xattr_set,
892};
893
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200894static const struct xattr_handler __maybe_unused
895ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200896 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
897 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200898 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200899 .set = ovl_posix_acl_xattr_set,
900};
901
902static const struct xattr_handler ovl_own_xattr_handler = {
903 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200904 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200905 .set = ovl_own_xattr_set,
906};
907
908static const struct xattr_handler ovl_other_xattr_handler = {
909 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200910 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200911 .set = ovl_other_xattr_set,
912};
913
914static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200915#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200916 &ovl_posix_acl_access_xattr_handler,
917 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200918#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200919 &ovl_own_xattr_handler,
920 &ovl_other_xattr_handler,
921 NULL
922};
923
Miklos Szerediad204482017-11-10 09:39:16 +0100924static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100925{
Miklos Szeredi50649752017-11-10 09:39:15 +0100926 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100927 int err;
928
Miklos Szerediad204482017-11-10 09:39:16 +0100929 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100930 if (err)
931 goto out;
932
933 /* Upper fs should not be r/o */
934 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
935 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
936 err = -EINVAL;
937 goto out;
938 }
939
Miklos Szerediad204482017-11-10 09:39:16 +0100940 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100941 if (err)
942 goto out;
943
944 err = -EBUSY;
945 if (ovl_inuse_trylock(upperpath->dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +0100946 ofs->upperdir_locked = true;
947 } else if (ofs->config.index) {
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100948 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
949 goto out;
950 } else {
951 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
952 }
Miklos Szeredi50649752017-11-10 09:39:15 +0100953
954 upper_mnt = clone_private_mount(upperpath);
955 err = PTR_ERR(upper_mnt);
956 if (IS_ERR(upper_mnt)) {
957 pr_err("overlayfs: failed to clone upperpath\n");
958 goto out;
959 }
960
961 /* Don't inherit atime flags */
962 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szerediad204482017-11-10 09:39:16 +0100963 ofs->upper_mnt = upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100964 err = 0;
965out:
966 return err;
967}
968
Miklos Szerediad204482017-11-10 09:39:16 +0100969static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100970{
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200971 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100972 struct dentry *temp;
Amir Goldsteine487d882017-11-07 13:55:04 +0200973 int fh_type;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100974 int err;
975
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200976 err = mnt_want_write(mnt);
977 if (err)
978 return err;
979
Miklos Szerediad204482017-11-10 09:39:16 +0100980 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
981 if (!ofs->workdir)
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200982 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100983
984 /*
985 * Upper should support d_type, else whiteouts are visible. Given
986 * workdir and upper are on same fs, we can do iterate_dir() on
987 * workdir. This check requires successful creation of workdir in
988 * previous step.
989 */
990 err = ovl_check_d_type_supported(workpath);
991 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +0200992 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100993
994 /*
995 * We allowed this configuration and don't want to break users over
996 * kernel upgrade. So warn instead of erroring out.
997 */
998 if (!err)
999 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1000
1001 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +01001002 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1003 ofs->tmpfile = !IS_ERR(temp);
1004 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001005 dput(temp);
1006 else
1007 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1008
1009 /*
1010 * Check if upper/work fs supports trusted.overlay.* xattr
1011 */
Miklos Szerediad204482017-11-10 09:39:16 +01001012 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001013 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +01001014 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +03001015 ofs->config.index = false;
1016 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off.\n");
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001017 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001018 } else {
Miklos Szerediad204482017-11-10 09:39:16 +01001019 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001020 }
1021
1022 /* Check if upper/work fs supports file handles */
Amir Goldsteine487d882017-11-07 13:55:04 +02001023 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1024 if (ofs->config.index && !fh_type) {
Miklos Szerediad204482017-11-10 09:39:16 +01001025 ofs->config.index = false;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001026 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1027 }
1028
Amir Goldsteine487d882017-11-07 13:55:04 +02001029 /* Check if upper fs has 32bit inode numbers */
1030 if (fh_type != FILEID_INO32_GEN)
1031 ofs->xino_bits = 0;
1032
Amir Goldsteinf168f102018-01-19 11:26:53 +02001033 /* NFS export of r/w mount depends on index */
1034 if (ofs->config.nfs_export && !ofs->config.index) {
1035 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1036 ofs->config.nfs_export = false;
1037 }
1038
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001039out:
1040 mnt_drop_write(mnt);
1041 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001042}
1043
Miklos Szerediad204482017-11-10 09:39:16 +01001044static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001045{
1046 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001047 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001048
Miklos Szerediad204482017-11-10 09:39:16 +01001049 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001050 if (err)
1051 goto out;
1052
1053 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001054 if (upperpath->mnt != workpath.mnt) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001055 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1056 goto out;
1057 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001058 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001059 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1060 goto out;
1061 }
1062
1063 err = -EBUSY;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001064 if (ovl_inuse_trylock(workpath.dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001065 ofs->workdir_locked = true;
1066 } else if (ofs->config.index) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001067 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1068 goto out;
1069 } else {
1070 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1071 }
1072
Miklos Szerediad204482017-11-10 09:39:16 +01001073 ofs->workbasedir = dget(workpath.dentry);
1074 err = ovl_make_workdir(ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001075 if (err)
1076 goto out;
1077
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001078 err = 0;
1079out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001080 path_put(&workpath);
1081
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001082 return err;
1083}
1084
Miklos Szerediad204482017-11-10 09:39:16 +01001085static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001086 struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001087{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001088 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001089 int err;
1090
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001091 err = mnt_want_write(mnt);
1092 if (err)
1093 return err;
1094
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001095 /* Verify lower root is upper root origin */
Amir Goldsteind9768072017-09-24 13:00:19 +03001096 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
Amir Goldstein05122442018-01-11 08:25:32 +02001097 true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001098 if (err) {
1099 pr_err("overlayfs: failed to verify upper root origin\n");
1100 goto out;
1101 }
1102
Miklos Szerediad204482017-11-10 09:39:16 +01001103 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1104 if (ofs->indexdir) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001105 /*
1106 * Verify upper root is exclusively associated with index dir.
1107 * Older kernels stored upper fh in "trusted.overlay.origin"
1108 * xattr. If that xattr exists, verify that it is a match to
1109 * upper dir file handle. In any case, verify or set xattr
1110 * "trusted.overlay.upper" to indicate that index may have
1111 * directory entries.
1112 */
1113 if (ovl_check_origin_xattr(ofs->indexdir)) {
1114 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1115 upperpath->dentry, true, false);
1116 if (err)
1117 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1118 }
1119 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001120 if (err)
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001121 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001122
1123 /* Cleanup bad/stale/orphan index entries */
1124 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001125 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001126 }
Miklos Szerediad204482017-11-10 09:39:16 +01001127 if (err || !ofs->indexdir)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001128 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1129
1130out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001131 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001132 return err;
1133}
1134
Amir Goldstein51486262018-03-28 20:22:41 +03001135/* Get a unique fsid for the layer */
1136static int ovl_get_fsid(struct ovl_fs *ofs, struct super_block *sb)
1137{
1138 unsigned int i;
1139 dev_t dev;
1140 int err;
1141
1142 /* fsid 0 is reserved for upper fs even with non upper overlay */
1143 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1144 return 0;
1145
1146 for (i = 0; i < ofs->numlowerfs; i++) {
1147 if (ofs->lower_fs[i].sb == sb)
1148 return i + 1;
1149 }
1150
1151 err = get_anon_bdev(&dev);
1152 if (err) {
1153 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1154 return err;
1155 }
1156
1157 ofs->lower_fs[ofs->numlowerfs].sb = sb;
1158 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1159 ofs->numlowerfs++;
1160
1161 return ofs->numlowerfs;
1162}
1163
Miklos Szerediad204482017-11-10 09:39:16 +01001164static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack,
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001165 unsigned int numlower)
1166{
1167 int err;
1168 unsigned int i;
1169
1170 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001171 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001172 GFP_KERNEL);
Miklos Szerediad204482017-11-10 09:39:16 +01001173 if (ofs->lower_layers == NULL)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001174 goto out;
Amir Goldstein51486262018-03-28 20:22:41 +03001175
1176 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1177 GFP_KERNEL);
1178 if (ofs->lower_fs == NULL)
1179 goto out;
1180
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001181 for (i = 0; i < numlower; i++) {
1182 struct vfsmount *mnt;
Amir Goldstein51486262018-03-28 20:22:41 +03001183 int fsid;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001184
Amir Goldstein51486262018-03-28 20:22:41 +03001185 err = fsid = ovl_get_fsid(ofs, stack[i].mnt->mnt_sb);
1186 if (err < 0)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001187 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001188
1189 mnt = clone_private_mount(&stack[i]);
1190 err = PTR_ERR(mnt);
1191 if (IS_ERR(mnt)) {
1192 pr_err("overlayfs: failed to clone lowerpath\n");
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001193 goto out;
1194 }
Amir Goldstein51486262018-03-28 20:22:41 +03001195
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001196 /*
1197 * Make lower layers R/O. That way fchmod/fchown on lower file
1198 * will fail instead of modifying lower fs.
1199 */
1200 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1201
Miklos Szerediad204482017-11-10 09:39:16 +01001202 ofs->lower_layers[ofs->numlower].mnt = mnt;
Amir Goldsteind583ed72017-11-08 19:23:36 +02001203 ofs->lower_layers[ofs->numlower].idx = i + 1;
Amir Goldstein51486262018-03-28 20:22:41 +03001204 ofs->lower_layers[ofs->numlower].fsid = fsid;
1205 if (fsid) {
1206 ofs->lower_layers[ofs->numlower].fs =
1207 &ofs->lower_fs[fsid - 1];
1208 }
Miklos Szerediad204482017-11-10 09:39:16 +01001209 ofs->numlower++;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001210 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001211
Amir Goldstein795939a2018-03-29 09:08:18 +03001212 /*
1213 * When all layers on same fs, overlay can use real inode numbers.
1214 * With mount option "xino=on", mounter declares that there are enough
1215 * free high bits in underlying fs to hold the unique fsid.
1216 * If overlayfs does encounter underlying inodes using the high xino
1217 * bits reserved for fsid, it emits a warning and uses the original
1218 * inode number.
1219 */
1220 if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
Amir Goldsteine487d882017-11-07 13:55:04 +02001221 ofs->xino_bits = 0;
Amir Goldstein795939a2018-03-29 09:08:18 +03001222 ofs->config.xino = OVL_XINO_OFF;
1223 } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1224 /*
1225 * This is a roundup of number of bits needed for numlowerfs+1
1226 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
1227 * upper fs even with non upper overlay.
1228 */
1229 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1230 ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1231 }
1232
1233 if (ofs->xino_bits) {
1234 pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1235 ofs->xino_bits);
1236 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001237
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001238 err = 0;
1239out:
1240 return err;
1241}
1242
Miklos Szeredi4155c102017-11-10 09:39:15 +01001243static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szerediad204482017-11-10 09:39:16 +01001244 struct ovl_fs *ofs)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001245{
1246 int err;
1247 char *lowertmp, *lower;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001248 struct path *stack = NULL;
1249 unsigned int stacklen, numlower = 0, i;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001250 bool remote = false;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001251 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001252
1253 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001254 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001255 if (!lowertmp)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001256 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001257
1258 err = -EINVAL;
1259 stacklen = ovl_split_lowerdirs(lowertmp);
1260 if (stacklen > OVL_MAX_STACK) {
1261 pr_err("overlayfs: too many lower directories, limit is %d\n",
1262 OVL_MAX_STACK);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001263 goto out_err;
Miklos Szerediad204482017-11-10 09:39:16 +01001264 } else if (!ofs->config.upperdir && stacklen == 1) {
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001265 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001266 goto out_err;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001267 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1268 ofs->config.redirect_follow) {
1269 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1270 ofs->config.nfs_export = false;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001271 }
1272
1273 err = -ENOMEM;
1274 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1275 if (!stack)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001276 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001277
1278 err = -EINVAL;
1279 lower = lowertmp;
1280 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szerediad204482017-11-10 09:39:16 +01001281 err = ovl_lower_dir(lower, &stack[numlower], ofs,
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001282 &sb->s_stack_depth, &remote);
1283 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001284 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001285
1286 lower = strchr(lower, '\0') + 1;
1287 }
1288
1289 err = -EINVAL;
1290 sb->s_stack_depth++;
1291 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1292 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001293 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001294 }
1295
Miklos Szerediad204482017-11-10 09:39:16 +01001296 err = ovl_get_lower_layers(ofs, stack, numlower);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001297 if (err)
1298 goto out_err;
1299
1300 err = -ENOMEM;
1301 oe = ovl_alloc_entry(numlower);
1302 if (!oe)
1303 goto out_err;
1304
1305 for (i = 0; i < numlower; i++) {
1306 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Miklos Szerediad204482017-11-10 09:39:16 +01001307 oe->lowerstack[i].layer = &ofs->lower_layers[i];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001308 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001309
1310 if (remote)
1311 sb->s_d_op = &ovl_reval_dentry_operations;
1312 else
1313 sb->s_d_op = &ovl_dentry_operations;
1314
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001315out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001316 for (i = 0; i < numlower; i++)
1317 path_put(&stack[i]);
1318 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001319 kfree(lowertmp);
1320
1321 return oe;
1322
1323out_err:
1324 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001325 goto out;
1326}
1327
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001328static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1329{
Kees Cook33006cd2017-03-29 14:02:19 -07001330 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001331 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001332 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001333 struct ovl_fs *ofs;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001334 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001335 int err;
1336
Erez Zadokf45827e82014-10-24 00:14:38 +02001337 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001338 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1339 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001340 goto out;
1341
Miklos Szerediad204482017-11-10 09:39:16 +01001342 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001343 if (!cred)
1344 goto out_err;
1345
Miklos Szerediad204482017-11-10 09:39:16 +01001346 ofs->config.index = ovl_index_def;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001347 ofs->config.nfs_export = ovl_nfs_export_def;
Amir Goldstein795939a2018-03-29 09:08:18 +03001348 ofs->config.xino = ovl_xino_def();
Miklos Szerediad204482017-11-10 09:39:16 +01001349 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001350 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001351 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001352
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001353 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001354 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001355 if (!silent)
1356 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001357 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001358 }
1359
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001360 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001361 sb->s_maxbytes = MAX_LFS_FILESIZE;
Amir Goldsteine487d882017-11-07 13:55:04 +02001362 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
Amir Goldstein795939a2018-03-29 09:08:18 +03001363 if (ofs->config.xino != OVL_XINO_OFF)
1364 ofs->xino_bits = BITS_PER_LONG - 32;
1365
Miklos Szerediad204482017-11-10 09:39:16 +01001366 if (ofs->config.upperdir) {
1367 if (!ofs->config.workdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001368 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001369 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001370 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001371
Miklos Szerediad204482017-11-10 09:39:16 +01001372 err = ovl_get_upper(ofs, &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001373 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001374 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001375
Miklos Szerediad204482017-11-10 09:39:16 +01001376 err = ovl_get_workdir(ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001377 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001378 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001379
Miklos Szerediad204482017-11-10 09:39:16 +01001380 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001381 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001382
Miklos Szerediad204482017-11-10 09:39:16 +01001383 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1384 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001385
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001386 }
Miklos Szerediad204482017-11-10 09:39:16 +01001387 oe = ovl_get_lowerstack(sb, ofs);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001388 err = PTR_ERR(oe);
1389 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001390 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001391
hujianyang71cbad72015-01-15 13:20:57 +08001392 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szerediad204482017-11-10 09:39:16 +01001393 if (!ofs->upper_mnt)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001394 sb->s_flags |= SB_RDONLY;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001395
Miklos Szerediad204482017-11-10 09:39:16 +01001396 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1397 err = ovl_get_indexdir(ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001398 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001399 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001400
Amir Goldstein972d0092017-09-19 12:14:18 +03001401 /* Force r/o mount with no index dir */
1402 if (!ofs->indexdir) {
1403 dput(ofs->workdir);
1404 ofs->workdir = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001405 sb->s_flags |= SB_RDONLY;
Amir Goldstein972d0092017-09-19 12:14:18 +03001406 }
1407
Amir Goldstein02bcd152017-06-21 15:28:36 +03001408 }
1409
Amir Goldstein972d0092017-09-19 12:14:18 +03001410 /* Show index=off in /proc/mounts for forced r/o mount */
Amir Goldsteinf168f102018-01-19 11:26:53 +02001411 if (!ofs->indexdir) {
Miklos Szerediad204482017-11-10 09:39:16 +01001412 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001413 if (ofs->upper_mnt && ofs->config.nfs_export) {
1414 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1415 ofs->config.nfs_export = false;
1416 }
1417 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001418
Amir Goldstein8383f172017-10-02 11:31:42 +03001419 if (ofs->config.nfs_export)
1420 sb->s_export_op = &ovl_export_operations;
1421
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001422 /* Never override disk quota limits or use reserved space */
1423 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1424
Vivek Goyal655042c2016-10-14 03:03:36 +02001425 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1426 sb->s_op = &ovl_super_operations;
1427 sb->s_xattr = ovl_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01001428 sb->s_fs_info = ofs;
Miklos Szeredide2a4a52018-07-18 15:44:43 +02001429 sb->s_flags |= SB_POSIXACL;
Vivek Goyal655042c2016-10-14 03:03:36 +02001430
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001431 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001432 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001433 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001434 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001435
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001436 root_dentry->d_fsdata = oe;
1437
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001438 mntput(upperpath.mnt);
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001439 if (upperpath.dentry) {
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001440 ovl_dentry_set_upper_alias(root_dentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +02001441 if (ovl_is_impuredir(upperpath.dentry))
1442 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001443 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001444
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001445 /* Root is always merge -> can have whiteouts */
1446 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Amir Goldstein2ca3c142018-01-30 13:31:09 +02001447 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001448 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1449 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001450
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001451 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001452
1453 return 0;
1454
Miklos Szeredi4155c102017-11-10 09:39:15 +01001455out_free_oe:
1456 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001457 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001458out_err:
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001459 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01001460 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001461out:
1462 return err;
1463}
1464
1465static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1466 const char *dev_name, void *raw_data)
1467{
1468 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1469}
1470
1471static struct file_system_type ovl_fs_type = {
1472 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001473 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001474 .mount = ovl_mount,
1475 .kill_sb = kill_anon_super,
1476};
Miklos Szeredief94b182014-11-20 16:39:59 +01001477MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001478
Amir Goldstein13cf1992017-06-12 09:54:40 +03001479static void ovl_inode_init_once(void *foo)
1480{
1481 struct ovl_inode *oi = foo;
1482
1483 inode_init_once(&oi->vfs_inode);
1484}
1485
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001486static int __init ovl_init(void)
1487{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001488 int err;
1489
1490 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1491 sizeof(struct ovl_inode), 0,
1492 (SLAB_RECLAIM_ACCOUNT|
1493 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1494 ovl_inode_init_once);
1495 if (ovl_inode_cachep == NULL)
1496 return -ENOMEM;
1497
1498 err = register_filesystem(&ovl_fs_type);
1499 if (err)
1500 kmem_cache_destroy(ovl_inode_cachep);
1501
1502 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001503}
1504
1505static void __exit ovl_exit(void)
1506{
1507 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001508
1509 /*
1510 * Make sure all delayed rcu free inodes are flushed before we
1511 * destroy cache.
1512 */
1513 rcu_barrier();
1514 kmem_cache_destroy(ovl_inode_cachep);
1515
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001516}
1517
1518module_init(ovl_init);
1519module_exit(ovl_exit);