blob: d944ab46b69e97944adda45e80ce4eb57ee9d600 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002/*
3 *
4 * Copyright (C) 2011 Novell Inc.
Miklos Szeredie9be9d52014-10-24 00:14:38 +02005 */
6
Ingo Molnar5b825c32017-02-02 17:54:15 +01007#include <uapi/linux/magic.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +02008#include <linux/fs.h>
9#include <linux/namei.h>
10#include <linux/xattr.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011#include <linux/mount.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020012#include <linux/parser.h>
13#include <linux/module.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020014#include <linux/statfs.h>
Erez Zadokf45827e82014-10-24 00:14:38 +020015#include <linux/seq_file.h>
Miklos Szeredid837a492016-07-29 12:05:24 +020016#include <linux/posix_acl_xattr.h>
Amir Goldsteine487d882017-11-07 13:55:04 +020017#include <linux/exportfs.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020018#include "overlayfs.h"
19
20MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
21MODULE_DESCRIPTION("Overlay filesystem");
22MODULE_LICENSE("GPL");
23
Miklos Szeredie9be9d52014-10-24 00:14:38 +020024
25struct ovl_dir_cache;
26
Miklos Szeredia78d9f02014-12-13 00:59:52 +010027#define OVL_MAX_STACK 500
28
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010029static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
30module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020031MODULE_PARM_DESC(redirect_dir,
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010032 "Default to on or off for the redirect_dir feature");
Miklos Szeredie9be9d52014-10-24 00:14:38 +020033
Miklos Szeredi438c84c2017-12-11 11:28:10 +010034static bool ovl_redirect_always_follow =
35 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
36module_param_named(redirect_always_follow, ovl_redirect_always_follow,
37 bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020038MODULE_PARM_DESC(redirect_always_follow,
Miklos Szeredi438c84c2017-12-11 11:28:10 +010039 "Follow redirects even if redirect_dir feature is turned off");
40
Amir Goldstein02bcd152017-06-21 15:28:36 +030041static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
42module_param_named(index, ovl_index_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020043MODULE_PARM_DESC(index,
Amir Goldstein02bcd152017-06-21 15:28:36 +030044 "Default to on or off for the inodes index feature");
45
Amir Goldsteinf168f102018-01-19 11:26:53 +020046static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
47module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020048MODULE_PARM_DESC(nfs_export,
Amir Goldsteinf168f102018-01-19 11:26:53 +020049 "Default to on or off for the NFS export feature");
50
Amir Goldstein795939a2018-03-29 09:08:18 +030051static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
52module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020053MODULE_PARM_DESC(xino_auto,
Amir Goldstein795939a2018-03-29 09:08:18 +030054 "Auto enable xino feature");
55
Miklos Szeredi4155c102017-11-10 09:39:15 +010056static void ovl_entry_stack_free(struct ovl_entry *oe)
57{
58 unsigned int i;
59
60 for (i = 0; i < oe->numlower; i++)
61 dput(oe->lowerstack[i].dentry);
62}
63
Vivek Goyald5791042018-05-11 11:49:27 -040064static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
65module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
Nicolas Schier253e7482019-06-17 09:39:00 +020066MODULE_PARM_DESC(metacopy,
Vivek Goyald5791042018-05-11 11:49:27 -040067 "Default to on or off for the metadata only copy up feature");
68
Miklos Szeredie9be9d52014-10-24 00:14:38 +020069static void ovl_dentry_release(struct dentry *dentry)
70{
71 struct ovl_entry *oe = dentry->d_fsdata;
72
73 if (oe) {
Miklos Szeredi4155c102017-11-10 09:39:15 +010074 ovl_entry_stack_free(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020075 kfree_rcu(oe, rcu);
76 }
77}
78
Miklos Szeredi2d902672016-06-30 08:53:27 +020079static struct dentry *ovl_d_real(struct dentry *dentry,
Miklos Szeredifb160432018-07-18 15:44:44 +020080 const struct inode *inode)
Miklos Szeredid101a122016-03-26 16:14:37 -040081{
82 struct dentry *real;
83
Miklos Szeredie8c985b2018-07-18 15:44:41 +020084 /* It's an overlay file */
85 if (inode && d_inode(dentry) == inode)
86 return dentry;
87
Miklos Szeredica4c8a32016-12-16 11:02:55 +010088 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040089 if (!inode || inode == d_inode(dentry))
90 return dentry;
91 goto bug;
92 }
93
94 real = ovl_dentry_upper(dentry);
Vivek Goyal2c3d7352018-05-11 11:49:31 -040095 if (real && (inode == d_inode(real)))
Miklos Szeredid101a122016-03-26 16:14:37 -040096 return real;
97
Vivek Goyal2c3d7352018-05-11 11:49:31 -040098 if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
99 return real;
100
101 real = ovl_dentry_lowerdata(dentry);
Miklos Szeredid101a122016-03-26 16:14:37 -0400102 if (!real)
103 goto bug;
104
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100105 /* Handle recursion */
Miklos Szeredifb160432018-07-18 15:44:44 +0200106 real = d_real(real, inode);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100107
Miklos Szeredid101a122016-03-26 16:14:37 -0400108 if (!inode || inode == d_inode(real))
109 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400110bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200111 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400112 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
113 return dentry;
114}
115
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100116static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
117{
118 int ret = 1;
119
120 if (weak) {
121 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
122 ret = d->d_op->d_weak_revalidate(d, flags);
123 } else if (d->d_flags & DCACHE_OP_REVALIDATE) {
124 ret = d->d_op->d_revalidate(d, flags);
125 if (!ret) {
126 if (!(flags & LOOKUP_RCU))
127 d_invalidate(d);
128 ret = -ESTALE;
129 }
130 }
131 return ret;
132}
133
134static int ovl_dentry_revalidate_common(struct dentry *dentry,
135 unsigned int flags, bool weak)
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200136{
137 struct ovl_entry *oe = dentry->d_fsdata;
138 unsigned int i;
139 int ret = 1;
140
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100141 for (i = 0; ret > 0 && i < oe->numlower; i++) {
142 ret = ovl_revalidate_real(oe->lowerstack[i].dentry, flags,
143 weak);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200144 }
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100145 return ret;
146}
147
148static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
149{
150 return ovl_dentry_revalidate_common(dentry, flags, false);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200151}
152
153static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
154{
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100155 return ovl_dentry_revalidate_common(dentry, flags, true);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200156}
157
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200158static const struct dentry_operations ovl_dentry_operations = {
159 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400160 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200161 .d_revalidate = ovl_dentry_revalidate,
162 .d_weak_revalidate = ovl_dentry_weak_revalidate,
163};
164
Amir Goldstein13cf1992017-06-12 09:54:40 +0300165static struct kmem_cache *ovl_inode_cachep;
166
167static struct inode *ovl_alloc_inode(struct super_block *sb)
168{
169 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
170
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900171 if (!oi)
172 return NULL;
173
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200174 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200175 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200176 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200177 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200178 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200179 oi->lower = NULL;
Vivek Goyal2664bd02018-05-11 11:49:30 -0400180 oi->lowerdata = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300181 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200182
Amir Goldstein13cf1992017-06-12 09:54:40 +0300183 return &oi->vfs_inode;
184}
185
Al Viro0b269de2019-04-15 22:52:17 -0400186static void ovl_free_inode(struct inode *inode)
Amir Goldstein13cf1992017-06-12 09:54:40 +0300187{
Al Viro0b269de2019-04-15 22:52:17 -0400188 struct ovl_inode *oi = OVL_I(inode);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300189
Al Viro0b269de2019-04-15 22:52:17 -0400190 kfree(oi->redirect);
191 mutex_destroy(&oi->lock);
192 kmem_cache_free(ovl_inode_cachep, oi);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300193}
194
195static void ovl_destroy_inode(struct inode *inode)
196{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200197 struct ovl_inode *oi = OVL_I(inode);
198
199 dput(oi->__upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200200 iput(oi->lower);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400201 if (S_ISDIR(inode->i_mode))
202 ovl_dir_cache_free(inode);
203 else
204 iput(oi->lowerdata);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300205}
206
Miklos Szerediad204482017-11-10 09:39:16 +0100207static void ovl_free_fs(struct ovl_fs *ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200208{
Miklos Szeredidd662662014-12-13 00:59:43 +0100209 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210
Amir Goldstein0be0bfd2019-07-12 15:24:34 +0300211 iput(ofs->workbasedir_trap);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300212 iput(ofs->indexdir_trap);
213 iput(ofs->workdir_trap);
214 iput(ofs->upperdir_trap);
Miklos Szerediad204482017-11-10 09:39:16 +0100215 dput(ofs->indexdir);
216 dput(ofs->workdir);
217 if (ofs->workdir_locked)
218 ovl_inuse_unlock(ofs->workbasedir);
219 dput(ofs->workbasedir);
220 if (ofs->upperdir_locked)
221 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
222 mntput(ofs->upper_mnt);
Amir Goldstein94375f92019-11-15 14:12:40 +0200223 for (i = 1; i < ofs->numlayer; i++) {
224 iput(ofs->layers[i].trap);
225 mntput(ofs->layers[i].mnt);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300226 }
Amir Goldstein94375f92019-11-15 14:12:40 +0200227 kfree(ofs->layers);
Amir Goldsteinb7bf9902020-01-14 22:17:25 +0200228 for (i = 0; i < ofs->numfs; i++)
Amir Goldstein07f1e592020-01-14 21:59:22 +0200229 free_anon_bdev(ofs->fs[i].pseudo_dev);
230 kfree(ofs->fs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200231
Miklos Szerediad204482017-11-10 09:39:16 +0100232 kfree(ofs->config.lowerdir);
233 kfree(ofs->config.upperdir);
234 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100235 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100236 if (ofs->creator_cred)
237 put_cred(ofs->creator_cred);
238 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200239}
240
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100241static void ovl_put_super(struct super_block *sb)
242{
243 struct ovl_fs *ofs = sb->s_fs_info;
244
245 ovl_free_fs(ofs);
246}
247
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800248/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200249static int ovl_sync_fs(struct super_block *sb, int wait)
250{
Miklos Szerediad204482017-11-10 09:39:16 +0100251 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200252 struct super_block *upper_sb;
253 int ret;
254
Miklos Szerediad204482017-11-10 09:39:16 +0100255 if (!ofs->upper_mnt)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200256 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800257
258 /*
259 * If this is a sync(2) call or an emergency sync, all the super blocks
260 * will be iterated, including upper_sb, so no need to do anything.
261 *
262 * If this is a syncfs(2) call, then we do need to call
263 * sync_filesystem() on upper_sb, but enough if we do it when being
264 * called with wait == 1.
265 */
266 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200267 return 0;
268
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800269 upper_sb = ofs->upper_mnt->mnt_sb;
270
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200271 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800272 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200273 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800274
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200275 return ret;
276}
277
Andy Whitcroftcc259632014-10-24 00:14:38 +0200278/**
279 * ovl_statfs
280 * @sb: The overlayfs super block
281 * @buf: The struct kstatfs to fill in with stats
282 *
283 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100284 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200285 */
286static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
287{
288 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
289 struct dentry *root_dentry = dentry->d_sb->s_root;
290 struct path path;
291 int err;
292
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100293 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200294
295 err = vfs_statfs(&path, buf);
296 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100297 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200298 buf->f_type = OVERLAYFS_SUPER_MAGIC;
299 }
300
301 return err;
302}
303
Amir Goldstein02bcd152017-06-21 15:28:36 +0300304/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100305static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300306{
Miklos Szerediad204482017-11-10 09:39:16 +0100307 return (!ofs->upper_mnt || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300308}
309
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100310static const char *ovl_redirect_mode_def(void)
311{
312 return ovl_redirect_dir_def ? "on" : "off";
313}
314
Amir Goldstein795939a2018-03-29 09:08:18 +0300315enum {
316 OVL_XINO_OFF,
317 OVL_XINO_AUTO,
318 OVL_XINO_ON,
319};
320
321static const char * const ovl_xino_str[] = {
322 "off",
323 "auto",
324 "on",
325};
326
327static inline int ovl_xino_def(void)
328{
329 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
330}
331
Erez Zadokf45827e82014-10-24 00:14:38 +0200332/**
333 * ovl_show_options
334 *
335 * Prints the mount options for a given superblock.
336 * Returns zero; does not fail.
337 */
338static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
339{
340 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100341 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200342
Miklos Szerediad204482017-11-10 09:39:16 +0100343 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
344 if (ofs->config.upperdir) {
345 seq_show_option(m, "upperdir", ofs->config.upperdir);
346 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100347 }
Miklos Szerediad204482017-11-10 09:39:16 +0100348 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200349 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100350 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
351 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100352 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100353 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Amir Goldsteinf168f102018-01-19 11:26:53 +0200354 if (ofs->config.nfs_export != ovl_nfs_export_def)
355 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
356 "on" : "off");
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200357 if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(sb))
Amir Goldstein795939a2018-03-29 09:08:18 +0300358 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
Vivek Goyald5791042018-05-11 11:49:27 -0400359 if (ofs->config.metacopy != ovl_metacopy_def)
360 seq_printf(m, ",metacopy=%s",
361 ofs->config.metacopy ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200362 return 0;
363}
364
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900365static int ovl_remount(struct super_block *sb, int *flags, char *data)
366{
Miklos Szerediad204482017-11-10 09:39:16 +0100367 struct ovl_fs *ofs = sb->s_fs_info;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900368
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800369 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900370 return -EROFS;
371
372 return 0;
373}
374
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200375static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300376 .alloc_inode = ovl_alloc_inode,
Al Viro0b269de2019-04-15 22:52:17 -0400377 .free_inode = ovl_free_inode,
Amir Goldstein13cf1992017-06-12 09:54:40 +0300378 .destroy_inode = ovl_destroy_inode,
379 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200380 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200381 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200382 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200383 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900384 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200385};
386
387enum {
388 OPT_LOWERDIR,
389 OPT_UPPERDIR,
390 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200391 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100392 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300393 OPT_INDEX_ON,
394 OPT_INDEX_OFF,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200395 OPT_NFS_EXPORT_ON,
396 OPT_NFS_EXPORT_OFF,
Amir Goldstein795939a2018-03-29 09:08:18 +0300397 OPT_XINO_ON,
398 OPT_XINO_OFF,
399 OPT_XINO_AUTO,
Vivek Goyald5791042018-05-11 11:49:27 -0400400 OPT_METACOPY_ON,
401 OPT_METACOPY_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200402 OPT_ERR,
403};
404
405static const match_table_t ovl_tokens = {
406 {OPT_LOWERDIR, "lowerdir=%s"},
407 {OPT_UPPERDIR, "upperdir=%s"},
408 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200409 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100410 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300411 {OPT_INDEX_ON, "index=on"},
412 {OPT_INDEX_OFF, "index=off"},
Amir Goldsteinf168f102018-01-19 11:26:53 +0200413 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
414 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
Amir Goldstein795939a2018-03-29 09:08:18 +0300415 {OPT_XINO_ON, "xino=on"},
416 {OPT_XINO_OFF, "xino=off"},
417 {OPT_XINO_AUTO, "xino=auto"},
Vivek Goyald5791042018-05-11 11:49:27 -0400418 {OPT_METACOPY_ON, "metacopy=on"},
419 {OPT_METACOPY_OFF, "metacopy=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200420 {OPT_ERR, NULL}
421};
422
Miklos Szeredi91c77942014-11-20 16:40:00 +0100423static char *ovl_next_opt(char **s)
424{
425 char *sbegin = *s;
426 char *p;
427
428 if (sbegin == NULL)
429 return NULL;
430
431 for (p = sbegin; *p; p++) {
432 if (*p == '\\') {
433 p++;
434 if (!*p)
435 break;
436 } else if (*p == ',') {
437 *p = '\0';
438 *s = p + 1;
439 return sbegin;
440 }
441 }
442 *s = NULL;
443 return sbegin;
444}
445
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100446static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
447{
448 if (strcmp(mode, "on") == 0) {
449 config->redirect_dir = true;
450 /*
451 * Does not make sense to have redirect creation without
452 * redirect following.
453 */
454 config->redirect_follow = true;
455 } else if (strcmp(mode, "follow") == 0) {
456 config->redirect_follow = true;
457 } else if (strcmp(mode, "off") == 0) {
458 if (ovl_redirect_always_follow)
459 config->redirect_follow = true;
460 } else if (strcmp(mode, "nofollow") != 0) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800461 pr_err("bad mount option \"redirect_dir=%s\"\n",
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100462 mode);
463 return -EINVAL;
464 }
465
466 return 0;
467}
468
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200469static int ovl_parse_opt(char *opt, struct ovl_config *config)
470{
471 char *p;
Vivek Goyald5791042018-05-11 11:49:27 -0400472 int err;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100473 bool metacopy_opt = false, redirect_opt = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200474
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100475 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
476 if (!config->redirect_mode)
477 return -ENOMEM;
478
Miklos Szeredi91c77942014-11-20 16:40:00 +0100479 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480 int token;
481 substring_t args[MAX_OPT_ARGS];
482
483 if (!*p)
484 continue;
485
486 token = match_token(p, ovl_tokens, args);
487 switch (token) {
488 case OPT_UPPERDIR:
489 kfree(config->upperdir);
490 config->upperdir = match_strdup(&args[0]);
491 if (!config->upperdir)
492 return -ENOMEM;
493 break;
494
495 case OPT_LOWERDIR:
496 kfree(config->lowerdir);
497 config->lowerdir = match_strdup(&args[0]);
498 if (!config->lowerdir)
499 return -ENOMEM;
500 break;
501
502 case OPT_WORKDIR:
503 kfree(config->workdir);
504 config->workdir = match_strdup(&args[0]);
505 if (!config->workdir)
506 return -ENOMEM;
507 break;
508
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200509 case OPT_DEFAULT_PERMISSIONS:
510 config->default_permissions = true;
511 break;
512
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100513 case OPT_REDIRECT_DIR:
514 kfree(config->redirect_mode);
515 config->redirect_mode = match_strdup(&args[0]);
516 if (!config->redirect_mode)
517 return -ENOMEM;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100518 redirect_opt = true;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100519 break;
520
Amir Goldstein02bcd152017-06-21 15:28:36 +0300521 case OPT_INDEX_ON:
522 config->index = true;
523 break;
524
525 case OPT_INDEX_OFF:
526 config->index = false;
527 break;
528
Amir Goldsteinf168f102018-01-19 11:26:53 +0200529 case OPT_NFS_EXPORT_ON:
530 config->nfs_export = true;
531 break;
532
533 case OPT_NFS_EXPORT_OFF:
534 config->nfs_export = false;
535 break;
536
Amir Goldstein795939a2018-03-29 09:08:18 +0300537 case OPT_XINO_ON:
538 config->xino = OVL_XINO_ON;
539 break;
540
541 case OPT_XINO_OFF:
542 config->xino = OVL_XINO_OFF;
543 break;
544
545 case OPT_XINO_AUTO:
546 config->xino = OVL_XINO_AUTO;
547 break;
548
Vivek Goyald5791042018-05-11 11:49:27 -0400549 case OPT_METACOPY_ON:
550 config->metacopy = true;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100551 metacopy_opt = true;
Vivek Goyald5791042018-05-11 11:49:27 -0400552 break;
553
554 case OPT_METACOPY_OFF:
555 config->metacopy = false;
556 break;
557
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200558 default:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800559 pr_err("unrecognized mount option \"%s\" or missing value\n",
560 p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200561 return -EINVAL;
562 }
563 }
hujianyang71cbad72015-01-15 13:20:57 +0800564
565 /* Workdir is useless in non-upper mount */
566 if (!config->upperdir && config->workdir) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800567 pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
hujianyang71cbad72015-01-15 13:20:57 +0800568 config->workdir);
569 kfree(config->workdir);
570 config->workdir = NULL;
571 }
572
Vivek Goyald5791042018-05-11 11:49:27 -0400573 err = ovl_parse_redirect_mode(config, config->redirect_mode);
574 if (err)
575 return err;
576
Miklos Szeredid47748e2018-11-01 21:31:39 +0100577 /*
578 * This is to make the logic below simpler. It doesn't make any other
579 * difference, since config->redirect_dir is only used for upper.
580 */
581 if (!config->upperdir && config->redirect_follow)
582 config->redirect_dir = true;
583
584 /* Resolve metacopy -> redirect_dir dependency */
585 if (config->metacopy && !config->redirect_dir) {
586 if (metacopy_opt && redirect_opt) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800587 pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
Miklos Szeredid47748e2018-11-01 21:31:39 +0100588 config->redirect_mode);
589 return -EINVAL;
590 }
591 if (redirect_opt) {
592 /*
593 * There was an explicit redirect_dir=... that resulted
594 * in this conflict.
595 */
lijiazi1bd0a3a2019-12-16 19:12:32 +0800596 pr_info("disabling metacopy due to redirect_dir=%s\n",
Miklos Szeredid47748e2018-11-01 21:31:39 +0100597 config->redirect_mode);
598 config->metacopy = false;
599 } else {
600 /* Automatically enable redirect otherwise. */
601 config->redirect_follow = config->redirect_dir = true;
602 }
Vivek Goyald5791042018-05-11 11:49:27 -0400603 }
604
605 return 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200606}
607
608#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300609#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200610
Miklos Szerediad204482017-11-10 09:39:16 +0100611static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300612 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200613{
Miklos Szerediad204482017-11-10 09:39:16 +0100614 struct inode *dir = ofs->workbasedir->d_inode;
615 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200616 struct dentry *work;
617 int err;
618 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300619 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200620
Al Viro59551022016-01-22 15:40:57 -0500621 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300622 locked = true;
623
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200624retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100625 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200626
627 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200628 struct iattr attr = {
629 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000630 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200631 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200632
633 if (work->d_inode) {
634 err = -EEXIST;
635 if (retried)
636 goto out_dput;
637
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300638 if (persist)
639 goto out_unlock;
640
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200641 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200642 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200643 dput(work);
644 goto retry;
645 }
646
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300647 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
648 err = PTR_ERR(work);
649 if (IS_ERR(work))
650 goto out_err;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200651
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200652 /*
653 * Try to remove POSIX ACL xattrs from workdir. We are good if:
654 *
655 * a) success (there was a POSIX ACL xattr and was removed)
656 * b) -ENODATA (there was no POSIX ACL xattr)
657 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
658 *
659 * There are various other error values that could effectively
660 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
661 * if the xattr name is too long), but the set of filesystems
662 * allowed as upper are limited to "normal" ones, where checking
663 * for the above two errors is sufficient.
664 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200665 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200666 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200667 goto out_dput;
668
669 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200670 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200671 goto out_dput;
672
673 /* Clear any inherited mode bits */
674 inode_lock(work->d_inode);
675 err = notify_change(work, &attr, NULL);
676 inode_unlock(work->d_inode);
677 if (err)
678 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300679 } else {
680 err = PTR_ERR(work);
681 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200682 }
683out_unlock:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300684 if (locked)
685 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200686
687 return work;
688
689out_dput:
690 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300691out_err:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800692 pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100693 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300694 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200695 goto out_unlock;
696}
697
Miklos Szeredi91c77942014-11-20 16:40:00 +0100698static void ovl_unescape(char *s)
699{
700 char *d = s;
701
702 for (;; s++, d++) {
703 if (*s == '\\')
704 s++;
705 *d = *s;
706 if (!*s)
707 break;
708 }
709}
710
Miklos Szerediab508822014-12-13 00:59:49 +0100711static int ovl_mount_dir_noesc(const char *name, struct path *path)
712{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100713 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100714
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100715 if (!*name) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800716 pr_err("empty lowerdir\n");
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100717 goto out;
718 }
Miklos Szerediab508822014-12-13 00:59:49 +0100719 err = kern_path(name, LOOKUP_FOLLOW, path);
720 if (err) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800721 pr_err("failed to resolve '%s': %i\n", name, err);
Miklos Szerediab508822014-12-13 00:59:49 +0100722 goto out;
723 }
724 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200725 if (ovl_dentry_weird(path->dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800726 pr_err("filesystem on '%s' not supported\n", name);
Miklos Szerediab508822014-12-13 00:59:49 +0100727 goto out_put;
728 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100729 if (!d_is_dir(path->dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800730 pr_err("'%s' not a directory\n", name);
Miklos Szerediab508822014-12-13 00:59:49 +0100731 goto out_put;
732 }
733 return 0;
734
735out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100736 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100737out:
738 return err;
739}
740
741static int ovl_mount_dir(const char *name, struct path *path)
742{
743 int err = -ENOMEM;
744 char *tmp = kstrdup(name, GFP_KERNEL);
745
746 if (tmp) {
747 ovl_unescape(tmp);
748 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200749
Miklos Szeredi7925dad2020-03-17 15:04:22 +0100750 if (!err && (ovl_dentry_remote(path->dentry) ||
751 path->dentry->d_flags & DCACHE_OP_REAL)) {
752 pr_err("filesystem on '%s' not supported as upperdir\n",
753 tmp);
754 path_put_init(path);
755 err = -EINVAL;
756 }
Miklos Szerediab508822014-12-13 00:59:49 +0100757 kfree(tmp);
758 }
759 return err;
760}
761
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100762static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
763 const char *name)
764{
765 struct kstatfs statfs;
766 int err = vfs_statfs(path, &statfs);
767
768 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +0800769 pr_err("statfs failed on '%s'\n", name);
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100770 else
771 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
772
773 return err;
774}
775
776static int ovl_lower_dir(const char *name, struct path *path,
Miklos Szeredif4288842020-03-17 15:04:22 +0100777 struct ovl_fs *ofs, int *stack_depth)
Miklos Szerediab508822014-12-13 00:59:49 +0100778{
Amir Goldsteine487d882017-11-07 13:55:04 +0200779 int fh_type;
Miklos Szerediab508822014-12-13 00:59:49 +0100780 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100781
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100782 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100783 if (err)
784 goto out;
785
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100786 err = ovl_check_namelen(path, ofs, name);
787 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100788 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100789
Miklos Szerediab508822014-12-13 00:59:49 +0100790 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
791
Amir Goldstein02bcd152017-06-21 15:28:36 +0300792 /*
Amir Goldsteinf168f102018-01-19 11:26:53 +0200793 * The inodes index feature and NFS export need to encode and decode
794 * file handles, so they require that all layers support them.
Amir Goldstein02bcd152017-06-21 15:28:36 +0300795 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200796 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
Amir Goldsteinf168f102018-01-19 11:26:53 +0200797 if ((ofs->config.nfs_export ||
Amir Goldsteine487d882017-11-07 13:55:04 +0200798 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300799 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200800 ofs->config.nfs_export = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +0800801 pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
Amir Goldsteinf168f102018-01-19 11:26:53 +0200802 name);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300803 }
804
Amir Goldsteine487d882017-11-07 13:55:04 +0200805 /* Check if lower fs has 32bit inode numbers */
806 if (fh_type != FILEID_INO32_GEN)
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200807 ofs->xino_mode = -1;
Amir Goldsteine487d882017-11-07 13:55:04 +0200808
Miklos Szerediab508822014-12-13 00:59:49 +0100809 return 0;
810
811out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100812 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100813out:
814 return err;
815}
816
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200817/* Workdir should not be subdir of upperdir and vice versa */
818static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
819{
820 bool ok = false;
821
822 if (workdir != upperdir) {
823 ok = (lock_rename(workdir, upperdir) == NULL);
824 unlock_rename(workdir, upperdir);
825 }
826 return ok;
827}
828
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100829static unsigned int ovl_split_lowerdirs(char *str)
830{
831 unsigned int ctr = 1;
832 char *s, *d;
833
834 for (s = d = str;; s++, d++) {
835 if (*s == '\\') {
836 s++;
837 } else if (*s == ':') {
838 *d = '\0';
839 ctr++;
840 continue;
841 }
842 *d = *s;
843 if (!*s)
844 break;
845 }
846 return ctr;
847}
848
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200849static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200850ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
851 struct dentry *dentry, struct inode *inode,
852 const char *name, void *buffer, size_t size)
853{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200854 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200855}
856
857static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200858ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
859 struct dentry *dentry, struct inode *inode,
860 const char *name, const void *value,
861 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200862{
863 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200864 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200865 struct posix_acl *acl = NULL;
866 int err;
867
868 /* Check that everything is OK before copy-up */
869 if (value) {
870 acl = posix_acl_from_xattr(&init_user_ns, value, size);
871 if (IS_ERR(acl))
872 return PTR_ERR(acl);
873 }
874 err = -EOPNOTSUPP;
875 if (!IS_POSIXACL(d_inode(workdir)))
876 goto out_acl_release;
877 if (!realinode->i_op->set_acl)
878 goto out_acl_release;
879 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
880 err = acl ? -EACCES : 0;
881 goto out_acl_release;
882 }
883 err = -EPERM;
884 if (!inode_owner_or_capable(inode))
885 goto out_acl_release;
886
887 posix_acl_release(acl);
888
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100889 /*
890 * Check if sgid bit needs to be cleared (actual setacl operation will
891 * be done with mounter's capabilities and so that won't do it for us).
892 */
893 if (unlikely(inode->i_mode & S_ISGID) &&
894 handler->flags == ACL_TYPE_ACCESS &&
895 !in_group_p(inode->i_gid) &&
896 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
897 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
898
899 err = ovl_setattr(dentry, &iattr);
900 if (err)
901 return err;
902 }
903
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200904 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200905 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200906 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200907
908 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200909
910out_acl_release:
911 posix_acl_release(acl);
912 return err;
913}
914
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200915static int ovl_own_xattr_get(const struct xattr_handler *handler,
916 struct dentry *dentry, struct inode *inode,
917 const char *name, void *buffer, size_t size)
918{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200919 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200920}
921
Miklos Szeredid837a492016-07-29 12:05:24 +0200922static int ovl_own_xattr_set(const struct xattr_handler *handler,
923 struct dentry *dentry, struct inode *inode,
924 const char *name, const void *value,
925 size_t size, int flags)
926{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200927 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200928}
929
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200930static int ovl_other_xattr_get(const struct xattr_handler *handler,
931 struct dentry *dentry, struct inode *inode,
932 const char *name, void *buffer, size_t size)
933{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200934 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200935}
936
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200937static int ovl_other_xattr_set(const struct xattr_handler *handler,
938 struct dentry *dentry, struct inode *inode,
939 const char *name, const void *value,
940 size_t size, int flags)
941{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200942 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200943}
944
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200945static const struct xattr_handler __maybe_unused
946ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200947 .name = XATTR_NAME_POSIX_ACL_ACCESS,
948 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200949 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200950 .set = ovl_posix_acl_xattr_set,
951};
952
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200953static const struct xattr_handler __maybe_unused
954ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200955 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
956 .flags = ACL_TYPE_DEFAULT,
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
961static const struct xattr_handler ovl_own_xattr_handler = {
962 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200963 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200964 .set = ovl_own_xattr_set,
965};
966
967static const struct xattr_handler ovl_other_xattr_handler = {
968 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200969 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200970 .set = ovl_other_xattr_set,
971};
972
973static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200974#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200975 &ovl_posix_acl_access_xattr_handler,
976 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200977#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200978 &ovl_own_xattr_handler,
979 &ovl_other_xattr_handler,
980 NULL
981};
982
Amir Goldstein146d62e2019-04-18 17:42:08 +0300983static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
984 struct inode **ptrap, const char *name)
985{
986 struct inode *trap;
987 int err;
988
989 trap = ovl_get_trap_inode(sb, dir);
Arnd Bergmann1dac6f5b2019-06-17 14:39:29 +0200990 err = PTR_ERR_OR_ZERO(trap);
991 if (err) {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300992 if (err == -ELOOP)
lijiazi1bd0a3a2019-12-16 19:12:32 +0800993 pr_err("conflicting %s path\n", name);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300994 return err;
995 }
996
997 *ptrap = trap;
998 return 0;
999}
1000
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001001/*
1002 * Determine how we treat concurrent use of upperdir/workdir based on the
1003 * index feature. This is papering over mount leaks of container runtimes,
1004 * for example, an old overlay mount is leaked and now its upperdir is
1005 * attempted to be used as a lower layer in a new overlay mount.
1006 */
1007static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
1008{
1009 if (ofs->config.index) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001010 pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001011 name);
1012 return -EBUSY;
1013 } else {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001014 pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001015 name);
1016 return 0;
1017 }
1018}
1019
Amir Goldstein146d62e2019-04-18 17:42:08 +03001020static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
1021 struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001022{
Miklos Szeredi50649752017-11-10 09:39:15 +01001023 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001024 int err;
1025
Miklos Szerediad204482017-11-10 09:39:16 +01001026 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001027 if (err)
1028 goto out;
1029
1030 /* Upper fs should not be r/o */
1031 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001032 pr_err("upper fs is r/o, try multi-lower layers mount\n");
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001033 err = -EINVAL;
1034 goto out;
1035 }
1036
Miklos Szerediad204482017-11-10 09:39:16 +01001037 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001038 if (err)
1039 goto out;
1040
Amir Goldstein146d62e2019-04-18 17:42:08 +03001041 err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap,
1042 "upperdir");
1043 if (err)
1044 goto out;
1045
Miklos Szeredi50649752017-11-10 09:39:15 +01001046 upper_mnt = clone_private_mount(upperpath);
1047 err = PTR_ERR(upper_mnt);
1048 if (IS_ERR(upper_mnt)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001049 pr_err("failed to clone upperpath\n");
Miklos Szeredi50649752017-11-10 09:39:15 +01001050 goto out;
1051 }
1052
1053 /* Don't inherit atime flags */
1054 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szerediad204482017-11-10 09:39:16 +01001055 ofs->upper_mnt = upper_mnt;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001056
Miklos Szeredi8c257412018-09-10 11:43:29 +02001057 if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
1058 ofs->upperdir_locked = true;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001059 } else {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001060 err = ovl_report_in_use(ofs, "upperdir");
1061 if (err)
1062 goto out;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001063 }
1064
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001065 err = 0;
1066out:
1067 return err;
1068}
1069
Amir Goldstein146d62e2019-04-18 17:42:08 +03001070static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1071 struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001072{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001073 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001074 struct dentry *temp;
Amir Goldsteine487d882017-11-07 13:55:04 +02001075 int fh_type;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001076 int err;
1077
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001078 err = mnt_want_write(mnt);
1079 if (err)
1080 return err;
1081
Miklos Szerediad204482017-11-10 09:39:16 +01001082 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1083 if (!ofs->workdir)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001084 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001085
Amir Goldstein146d62e2019-04-18 17:42:08 +03001086 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1087 if (err)
1088 goto out;
1089
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001090 /*
1091 * Upper should support d_type, else whiteouts are visible. Given
1092 * workdir and upper are on same fs, we can do iterate_dir() on
1093 * workdir. This check requires successful creation of workdir in
1094 * previous step.
1095 */
1096 err = ovl_check_d_type_supported(workpath);
1097 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001098 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001099
1100 /*
1101 * We allowed this configuration and don't want to break users over
1102 * kernel upgrade. So warn instead of erroring out.
1103 */
1104 if (!err)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001105 pr_warn("upper fs needs to support d_type.\n");
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001106
1107 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +01001108 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1109 ofs->tmpfile = !IS_ERR(temp);
1110 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001111 dput(temp);
1112 else
lijiazi1bd0a3a2019-12-16 19:12:32 +08001113 pr_warn("upper fs does not support tmpfile.\n");
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001114
1115 /*
1116 * Check if upper/work fs supports trusted.overlay.* xattr
1117 */
Miklos Szerediad204482017-11-10 09:39:16 +01001118 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001119 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +01001120 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +03001121 ofs->config.index = false;
Vivek Goyald5791042018-05-11 11:49:27 -04001122 ofs->config.metacopy = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001123 pr_warn("upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001124 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001125 } else {
Miklos Szerediad204482017-11-10 09:39:16 +01001126 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001127 }
1128
1129 /* Check if upper/work fs supports file handles */
Amir Goldsteine487d882017-11-07 13:55:04 +02001130 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1131 if (ofs->config.index && !fh_type) {
Miklos Szerediad204482017-11-10 09:39:16 +01001132 ofs->config.index = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001133 pr_warn("upper fs does not support file handles, falling back to index=off.\n");
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001134 }
1135
Amir Goldsteine487d882017-11-07 13:55:04 +02001136 /* Check if upper fs has 32bit inode numbers */
1137 if (fh_type != FILEID_INO32_GEN)
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001138 ofs->xino_mode = -1;
Amir Goldsteine487d882017-11-07 13:55:04 +02001139
Amir Goldsteinf168f102018-01-19 11:26:53 +02001140 /* NFS export of r/w mount depends on index */
1141 if (ofs->config.nfs_export && !ofs->config.index) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001142 pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
Amir Goldsteinf168f102018-01-19 11:26:53 +02001143 ofs->config.nfs_export = false;
1144 }
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001145out:
1146 mnt_drop_write(mnt);
1147 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001148}
1149
Amir Goldstein146d62e2019-04-18 17:42:08 +03001150static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1151 struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001152{
1153 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001154 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001155
Miklos Szerediad204482017-11-10 09:39:16 +01001156 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001157 if (err)
1158 goto out;
1159
1160 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001161 if (upperpath->mnt != workpath.mnt) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001162 pr_err("workdir and upperdir must reside under the same mount\n");
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001163 goto out;
1164 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001165 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001166 pr_err("workdir and upperdir must be separate subtrees\n");
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001167 goto out;
1168 }
1169
Miklos Szeredi8c257412018-09-10 11:43:29 +02001170 ofs->workbasedir = dget(workpath.dentry);
1171
Miklos Szeredi8c257412018-09-10 11:43:29 +02001172 if (ovl_inuse_trylock(ofs->workbasedir)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001173 ofs->workdir_locked = true;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001174 } else {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001175 err = ovl_report_in_use(ofs, "workdir");
1176 if (err)
1177 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001178 }
1179
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001180 err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
1181 "workdir");
1182 if (err)
1183 goto out;
1184
Amir Goldstein146d62e2019-04-18 17:42:08 +03001185 err = ovl_make_workdir(sb, ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001186
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001187out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001188 path_put(&workpath);
1189
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001190 return err;
1191}
1192
Amir Goldstein146d62e2019-04-18 17:42:08 +03001193static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1194 struct ovl_entry *oe, struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001195{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001196 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001197 int err;
1198
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001199 err = mnt_want_write(mnt);
1200 if (err)
1201 return err;
1202
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001203 /* Verify lower root is upper root origin */
Amir Goldsteind9768072017-09-24 13:00:19 +03001204 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
Amir Goldstein05122442018-01-11 08:25:32 +02001205 true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001206 if (err) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001207 pr_err("failed to verify upper root origin\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001208 goto out;
1209 }
1210
Miklos Szerediad204482017-11-10 09:39:16 +01001211 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1212 if (ofs->indexdir) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001213 err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1214 "indexdir");
1215 if (err)
1216 goto out;
1217
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001218 /*
1219 * Verify upper root is exclusively associated with index dir.
1220 * Older kernels stored upper fh in "trusted.overlay.origin"
1221 * xattr. If that xattr exists, verify that it is a match to
1222 * upper dir file handle. In any case, verify or set xattr
1223 * "trusted.overlay.upper" to indicate that index may have
1224 * directory entries.
1225 */
1226 if (ovl_check_origin_xattr(ofs->indexdir)) {
1227 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1228 upperpath->dentry, true, false);
1229 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001230 pr_err("failed to verify index dir 'origin' xattr\n");
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001231 }
1232 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001233 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001234 pr_err("failed to verify index dir 'upper' xattr\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001235
1236 /* Cleanup bad/stale/orphan index entries */
1237 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001238 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001239 }
Miklos Szerediad204482017-11-10 09:39:16 +01001240 if (err || !ofs->indexdir)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001241 pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001242
1243out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001244 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001245 return err;
1246}
1247
Amir Goldstein9df085f2018-09-03 09:12:09 +03001248static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
Amir Goldstein51486262018-03-28 20:22:41 +03001249{
1250 unsigned int i;
Amir Goldstein9df085f2018-09-03 09:12:09 +03001251
Amir Goldstein7e63c872019-11-14 22:28:41 +02001252 if (!ofs->config.nfs_export && !ofs->upper_mnt)
Amir Goldstein9df085f2018-09-03 09:12:09 +03001253 return true;
1254
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001255 for (i = 0; i < ofs->numfs; i++) {
Amir Goldstein9df085f2018-09-03 09:12:09 +03001256 /*
1257 * We use uuid to associate an overlay lower file handle with a
1258 * lower layer, so we can accept lower fs with null uuid as long
1259 * as all lower layers with null uuid are on the same fs.
Amir Goldstein7e63c872019-11-14 22:28:41 +02001260 * if we detect multiple lower fs with the same uuid, we
1261 * disable lower file handle decoding on all of them.
Amir Goldstein9df085f2018-09-03 09:12:09 +03001262 */
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001263 if (ofs->fs[i].is_lower &&
1264 uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
Amir Goldstein07f1e592020-01-14 21:59:22 +02001265 ofs->fs[i].bad_uuid = true;
Amir Goldstein9df085f2018-09-03 09:12:09 +03001266 return false;
Amir Goldstein7e63c872019-11-14 22:28:41 +02001267 }
Amir Goldstein9df085f2018-09-03 09:12:09 +03001268 }
1269 return true;
1270}
1271
1272/* Get a unique fsid for the layer */
1273static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1274{
1275 struct super_block *sb = path->mnt->mnt_sb;
1276 unsigned int i;
Amir Goldstein51486262018-03-28 20:22:41 +03001277 dev_t dev;
1278 int err;
Amir Goldstein7e63c872019-11-14 22:28:41 +02001279 bool bad_uuid = false;
Amir Goldstein51486262018-03-28 20:22:41 +03001280
Amir Goldstein07f1e592020-01-14 21:59:22 +02001281 for (i = 0; i < ofs->numfs; i++) {
1282 if (ofs->fs[i].sb == sb)
1283 return i;
Amir Goldstein51486262018-03-28 20:22:41 +03001284 }
1285
Amir Goldstein9df085f2018-09-03 09:12:09 +03001286 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
Amir Goldstein7e63c872019-11-14 22:28:41 +02001287 bad_uuid = true;
1288 if (ofs->config.index || ofs->config.nfs_export) {
1289 ofs->config.index = false;
1290 ofs->config.nfs_export = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001291 pr_warn("%s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
Amir Goldstein7e63c872019-11-14 22:28:41 +02001292 uuid_is_null(&sb->s_uuid) ? "null" :
1293 "conflicting",
1294 path->dentry);
1295 }
Amir Goldstein9df085f2018-09-03 09:12:09 +03001296 }
1297
Amir Goldstein51486262018-03-28 20:22:41 +03001298 err = get_anon_bdev(&dev);
1299 if (err) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001300 pr_err("failed to get anonymous bdev for lowerpath\n");
Amir Goldstein51486262018-03-28 20:22:41 +03001301 return err;
1302 }
1303
Amir Goldstein07f1e592020-01-14 21:59:22 +02001304 ofs->fs[ofs->numfs].sb = sb;
1305 ofs->fs[ofs->numfs].pseudo_dev = dev;
1306 ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
Amir Goldstein51486262018-03-28 20:22:41 +03001307
Amir Goldstein07f1e592020-01-14 21:59:22 +02001308 return ofs->numfs++;
Amir Goldstein51486262018-03-28 20:22:41 +03001309}
1310
Amir Goldstein94375f92019-11-15 14:12:40 +02001311static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
1312 struct path *stack, unsigned int numlower)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001313{
1314 int err;
1315 unsigned int i;
Miklos Szeredi13464162020-01-24 09:46:45 +01001316 struct ovl_layer *layers;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001317
1318 err = -ENOMEM;
Miklos Szeredi13464162020-01-24 09:46:45 +01001319 layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL);
1320 if (!layers)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001321 goto out;
Miklos Szeredi13464162020-01-24 09:46:45 +01001322 ofs->layers = layers;
Amir Goldstein51486262018-03-28 20:22:41 +03001323
Amir Goldstein07f1e592020-01-14 21:59:22 +02001324 ofs->fs = kcalloc(numlower + 1, sizeof(struct ovl_sb), GFP_KERNEL);
1325 if (ofs->fs == NULL)
Amir Goldstein51486262018-03-28 20:22:41 +03001326 goto out;
1327
Amir Goldstein07f1e592020-01-14 21:59:22 +02001328 /* idx/fsid 0 are reserved for upper fs even with lower only overlay */
1329 ofs->numfs++;
1330
Miklos Szeredi13464162020-01-24 09:46:45 +01001331 layers[0].mnt = ofs->upper_mnt;
1332 layers[0].idx = 0;
1333 layers[0].fsid = 0;
Amir Goldstein94375f92019-11-15 14:12:40 +02001334 ofs->numlayer = 1;
1335
Amir Goldstein07f1e592020-01-14 21:59:22 +02001336 /*
Amir Goldsteinb7bf9902020-01-14 22:17:25 +02001337 * All lower layers that share the same fs as upper layer, use the same
1338 * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower
1339 * only overlay to simplify ovl_fs_free().
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001340 * is_lower will be set if upper fs is shared with a lower layer.
Amir Goldstein07f1e592020-01-14 21:59:22 +02001341 */
Amir Goldsteinb7bf9902020-01-14 22:17:25 +02001342 err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
1343 if (err) {
1344 pr_err("failed to get anonymous bdev for upper fs\n");
1345 goto out;
1346 }
1347
Amir Goldstein07f1e592020-01-14 21:59:22 +02001348 if (ofs->upper_mnt) {
1349 ofs->fs[0].sb = ofs->upper_mnt->mnt_sb;
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001350 ofs->fs[0].is_lower = false;
Amir Goldstein07f1e592020-01-14 21:59:22 +02001351 }
1352
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001353 for (i = 0; i < numlower; i++) {
1354 struct vfsmount *mnt;
Amir Goldstein146d62e2019-04-18 17:42:08 +03001355 struct inode *trap;
Amir Goldstein51486262018-03-28 20:22:41 +03001356 int fsid;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001357
Amir Goldstein9df085f2018-09-03 09:12:09 +03001358 err = fsid = ovl_get_fsid(ofs, &stack[i]);
Amir Goldstein51486262018-03-28 20:22:41 +03001359 if (err < 0)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001360 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001361
Amir Goldstein146d62e2019-04-18 17:42:08 +03001362 err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1363 if (err)
1364 goto out;
1365
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001366 if (ovl_is_inuse(stack[i].dentry)) {
1367 err = ovl_report_in_use(ofs, "lowerdir");
1368 if (err)
1369 goto out;
1370 }
1371
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001372 mnt = clone_private_mount(&stack[i]);
1373 err = PTR_ERR(mnt);
1374 if (IS_ERR(mnt)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001375 pr_err("failed to clone lowerpath\n");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001376 iput(trap);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001377 goto out;
1378 }
Amir Goldstein51486262018-03-28 20:22:41 +03001379
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001380 /*
1381 * Make lower layers R/O. That way fchmod/fchown on lower file
1382 * will fail instead of modifying lower fs.
1383 */
1384 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1385
Miklos Szeredi13464162020-01-24 09:46:45 +01001386 layers[ofs->numlayer].trap = trap;
1387 layers[ofs->numlayer].mnt = mnt;
1388 layers[ofs->numlayer].idx = ofs->numlayer;
1389 layers[ofs->numlayer].fsid = fsid;
1390 layers[ofs->numlayer].fs = &ofs->fs[fsid];
Amir Goldstein94375f92019-11-15 14:12:40 +02001391 ofs->numlayer++;
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001392 ofs->fs[fsid].is_lower = true;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001393 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001394
Amir Goldstein795939a2018-03-29 09:08:18 +03001395 /*
1396 * When all layers on same fs, overlay can use real inode numbers.
1397 * With mount option "xino=on", mounter declares that there are enough
1398 * free high bits in underlying fs to hold the unique fsid.
1399 * If overlayfs does encounter underlying inodes using the high xino
1400 * bits reserved for fsid, it emits a warning and uses the original
1401 * inode number.
1402 */
Amir Goldstein07f1e592020-01-14 21:59:22 +02001403 if (ofs->numfs - !ofs->upper_mnt == 1) {
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001404 if (ofs->config.xino == OVL_XINO_ON)
1405 pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
1406 ofs->xino_mode = 0;
Amir Goldstein53afcd32020-02-21 16:34:42 +02001407 } else if (ofs->config.xino == OVL_XINO_OFF) {
1408 ofs->xino_mode = -1;
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001409 } else if (ofs->config.xino == OVL_XINO_ON && ofs->xino_mode < 0) {
Amir Goldstein795939a2018-03-29 09:08:18 +03001410 /*
Amir Goldstein07f1e592020-01-14 21:59:22 +02001411 * This is a roundup of number of bits needed for encoding
1412 * fsid, where fsid 0 is reserved for upper fs even with
1413 * lower only overlay.
Amir Goldstein795939a2018-03-29 09:08:18 +03001414 */
1415 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
Amir Goldstein07f1e592020-01-14 21:59:22 +02001416 ofs->xino_mode = ilog2(ofs->numfs - 1) + 1;
Amir Goldstein795939a2018-03-29 09:08:18 +03001417 }
1418
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001419 if (ofs->xino_mode > 0) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001420 pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001421 ofs->xino_mode);
Amir Goldstein795939a2018-03-29 09:08:18 +03001422 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001423
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001424 err = 0;
1425out:
1426 return err;
1427}
1428
Miklos Szeredi4155c102017-11-10 09:39:15 +01001429static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szerediad204482017-11-10 09:39:16 +01001430 struct ovl_fs *ofs)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001431{
1432 int err;
1433 char *lowertmp, *lower;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001434 struct path *stack = NULL;
1435 unsigned int stacklen, numlower = 0, i;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001436 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001437
1438 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001439 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001440 if (!lowertmp)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001441 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001442
1443 err = -EINVAL;
1444 stacklen = ovl_split_lowerdirs(lowertmp);
1445 if (stacklen > OVL_MAX_STACK) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001446 pr_err("too many lower directories, limit is %d\n",
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001447 OVL_MAX_STACK);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001448 goto out_err;
Miklos Szerediad204482017-11-10 09:39:16 +01001449 } else if (!ofs->config.upperdir && stacklen == 1) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001450 pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001451 goto out_err;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001452 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1453 ofs->config.redirect_follow) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001454 pr_warn("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
Amir Goldsteinf168f102018-01-19 11:26:53 +02001455 ofs->config.nfs_export = false;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001456 }
1457
1458 err = -ENOMEM;
1459 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1460 if (!stack)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001461 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001462
1463 err = -EINVAL;
1464 lower = lowertmp;
1465 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szerediad204482017-11-10 09:39:16 +01001466 err = ovl_lower_dir(lower, &stack[numlower], ofs,
Miklos Szeredif4288842020-03-17 15:04:22 +01001467 &sb->s_stack_depth);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001468 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001469 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001470
1471 lower = strchr(lower, '\0') + 1;
1472 }
1473
1474 err = -EINVAL;
1475 sb->s_stack_depth++;
1476 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001477 pr_err("maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001478 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001479 }
1480
Amir Goldstein94375f92019-11-15 14:12:40 +02001481 err = ovl_get_layers(sb, ofs, stack, numlower);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001482 if (err)
1483 goto out_err;
1484
1485 err = -ENOMEM;
1486 oe = ovl_alloc_entry(numlower);
1487 if (!oe)
1488 goto out_err;
1489
1490 for (i = 0; i < numlower; i++) {
1491 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Amir Goldstein94375f92019-11-15 14:12:40 +02001492 oe->lowerstack[i].layer = &ofs->layers[i+1];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001493 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001494
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001495out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001496 for (i = 0; i < numlower; i++)
1497 path_put(&stack[i]);
1498 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001499 kfree(lowertmp);
1500
1501 return oe;
1502
1503out_err:
1504 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001505 goto out;
1506}
1507
Amir Goldstein146d62e2019-04-18 17:42:08 +03001508/*
1509 * Check if this layer root is a descendant of:
1510 * - another layer of this overlayfs instance
1511 * - upper/work dir of any overlayfs instance
Amir Goldstein146d62e2019-04-18 17:42:08 +03001512 */
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001513static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
1514 struct dentry *dentry, const char *name)
Amir Goldstein146d62e2019-04-18 17:42:08 +03001515{
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001516 struct dentry *next = dentry, *parent;
Amir Goldstein146d62e2019-04-18 17:42:08 +03001517 int err = 0;
1518
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001519 if (!dentry)
Amir Goldstein146d62e2019-04-18 17:42:08 +03001520 return 0;
1521
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001522 parent = dget_parent(next);
1523
1524 /* Walk back ancestors to root (inclusive) looking for traps */
1525 while (!err && parent != next) {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001526 if (ovl_lookup_trap_inode(sb, parent)) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001527 err = -ELOOP;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001528 pr_err("overlapping %s path\n", name);
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001529 } else if (ovl_is_inuse(parent)) {
1530 err = ovl_report_in_use(ofs, name);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001531 }
Amir Goldstein146d62e2019-04-18 17:42:08 +03001532 next = parent;
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001533 parent = dget_parent(next);
1534 dput(next);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001535 }
1536
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001537 dput(parent);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001538
1539 return err;
1540}
1541
1542/*
1543 * Check if any of the layers or work dirs overlap.
1544 */
1545static int ovl_check_overlapping_layers(struct super_block *sb,
1546 struct ovl_fs *ofs)
1547{
1548 int i, err;
1549
1550 if (ofs->upper_mnt) {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001551 err = ovl_check_layer(sb, ofs, ofs->upper_mnt->mnt_root,
1552 "upperdir");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001553 if (err)
1554 return err;
1555
1556 /*
1557 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1558 * this instance and covers overlapping work and index dirs,
1559 * unless work or index dir have been moved since created inside
1560 * workbasedir. In that case, we already have their traps in
1561 * inode cache and we will catch that case on lookup.
1562 */
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001563 err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001564 if (err)
1565 return err;
1566 }
1567
Amir Goldstein94375f92019-11-15 14:12:40 +02001568 for (i = 1; i < ofs->numlayer; i++) {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001569 err = ovl_check_layer(sb, ofs,
Amir Goldstein94375f92019-11-15 14:12:40 +02001570 ofs->layers[i].mnt->mnt_root,
Amir Goldstein146d62e2019-04-18 17:42:08 +03001571 "lowerdir");
1572 if (err)
1573 return err;
1574 }
1575
1576 return 0;
1577}
1578
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001579static struct dentry *ovl_get_root(struct super_block *sb,
1580 struct dentry *upperdentry,
1581 struct ovl_entry *oe)
1582{
1583 struct dentry *root;
Amir Goldstein62c832e2019-11-19 15:31:46 +02001584 struct ovl_path *lowerpath = &oe->lowerstack[0];
1585 unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
1586 int fsid = lowerpath->layer->fsid;
1587 struct ovl_inode_params oip = {
1588 .upperdentry = upperdentry,
1589 .lowerpath = lowerpath,
1590 };
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001591
1592 root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1593 if (!root)
1594 return NULL;
1595
1596 root->d_fsdata = oe;
1597
1598 if (upperdentry) {
Amir Goldstein62c832e2019-11-19 15:31:46 +02001599 /* Root inode uses upper st_ino/i_ino */
1600 ino = d_inode(upperdentry)->i_ino;
1601 fsid = 0;
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001602 ovl_dentry_set_upper_alias(root);
1603 if (ovl_is_impuredir(upperdentry))
1604 ovl_set_flag(OVL_IMPURE, d_inode(root));
1605 }
1606
1607 /* Root is always merge -> can have whiteouts */
1608 ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
1609 ovl_dentry_set_flag(OVL_E_CONNECTED, root);
1610 ovl_set_upperdata(d_inode(root));
Amir Goldstein62c832e2019-11-19 15:31:46 +02001611 ovl_inode_init(d_inode(root), &oip, ino, fsid);
Miklos Szeredif4288842020-03-17 15:04:22 +01001612 ovl_dentry_update_reval(root, upperdentry, DCACHE_OP_WEAK_REVALIDATE);
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001613
1614 return root;
1615}
1616
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001617static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1618{
Kees Cook33006cd2017-03-29 14:02:19 -07001619 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001620 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001621 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001622 struct ovl_fs *ofs;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001623 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001624 int err;
1625
Miklos Szeredif4288842020-03-17 15:04:22 +01001626 sb->s_d_op = &ovl_dentry_operations;
1627
Erez Zadokf45827e82014-10-24 00:14:38 +02001628 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001629 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1630 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001631 goto out;
1632
Miklos Szerediad204482017-11-10 09:39:16 +01001633 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001634 if (!cred)
1635 goto out_err;
1636
Miklos Szerediad204482017-11-10 09:39:16 +01001637 ofs->config.index = ovl_index_def;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001638 ofs->config.nfs_export = ovl_nfs_export_def;
Amir Goldstein795939a2018-03-29 09:08:18 +03001639 ofs->config.xino = ovl_xino_def();
Vivek Goyald5791042018-05-11 11:49:27 -04001640 ofs->config.metacopy = ovl_metacopy_def;
Miklos Szerediad204482017-11-10 09:39:16 +01001641 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001642 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001643 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001644
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001645 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001646 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001647 if (!silent)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001648 pr_err("missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001649 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001650 }
1651
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001652 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001653 sb->s_maxbytes = MAX_LFS_FILESIZE;
Amir Goldsteine487d882017-11-07 13:55:04 +02001654 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
Amir Goldstein53afcd32020-02-21 16:34:42 +02001655 if (ofs->config.xino != OVL_XINO_OFF) {
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001656 ofs->xino_mode = BITS_PER_LONG - 32;
Amir Goldstein53afcd32020-02-21 16:34:42 +02001657 if (!ofs->xino_mode) {
1658 pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
1659 ofs->config.xino = OVL_XINO_OFF;
1660 }
1661 }
Amir Goldstein795939a2018-03-29 09:08:18 +03001662
Amir Goldstein146d62e2019-04-18 17:42:08 +03001663 /* alloc/destroy_inode needed for setting up traps in inode cache */
1664 sb->s_op = &ovl_super_operations;
1665
Miklos Szerediad204482017-11-10 09:39:16 +01001666 if (ofs->config.upperdir) {
1667 if (!ofs->config.workdir) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001668 pr_err("missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001669 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001670 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001671
Amir Goldstein146d62e2019-04-18 17:42:08 +03001672 err = ovl_get_upper(sb, ofs, &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001673 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001674 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001675
Amir Goldstein146d62e2019-04-18 17:42:08 +03001676 err = ovl_get_workdir(sb, ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001677 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001678 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001679
Miklos Szerediad204482017-11-10 09:39:16 +01001680 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001681 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001682
Miklos Szerediad204482017-11-10 09:39:16 +01001683 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1684 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001685
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001686 }
Miklos Szerediad204482017-11-10 09:39:16 +01001687 oe = ovl_get_lowerstack(sb, ofs);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001688 err = PTR_ERR(oe);
1689 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001690 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001691
hujianyang71cbad72015-01-15 13:20:57 +08001692 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szerediad204482017-11-10 09:39:16 +01001693 if (!ofs->upper_mnt)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001694 sb->s_flags |= SB_RDONLY;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001695
Miklos Szerediad204482017-11-10 09:39:16 +01001696 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001697 err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001698 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001699 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001700
Amir Goldstein972d0092017-09-19 12:14:18 +03001701 /* Force r/o mount with no index dir */
1702 if (!ofs->indexdir) {
1703 dput(ofs->workdir);
1704 ofs->workdir = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001705 sb->s_flags |= SB_RDONLY;
Amir Goldstein972d0092017-09-19 12:14:18 +03001706 }
1707
Amir Goldstein02bcd152017-06-21 15:28:36 +03001708 }
1709
Amir Goldstein146d62e2019-04-18 17:42:08 +03001710 err = ovl_check_overlapping_layers(sb, ofs);
1711 if (err)
1712 goto out_free_oe;
1713
Amir Goldstein972d0092017-09-19 12:14:18 +03001714 /* Show index=off in /proc/mounts for forced r/o mount */
Amir Goldsteinf168f102018-01-19 11:26:53 +02001715 if (!ofs->indexdir) {
Miklos Szerediad204482017-11-10 09:39:16 +01001716 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001717 if (ofs->upper_mnt && ofs->config.nfs_export) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001718 pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
Amir Goldsteinf168f102018-01-19 11:26:53 +02001719 ofs->config.nfs_export = false;
1720 }
1721 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001722
Vivek Goyald5791042018-05-11 11:49:27 -04001723 if (ofs->config.metacopy && ofs->config.nfs_export) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001724 pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
Vivek Goyald5791042018-05-11 11:49:27 -04001725 ofs->config.nfs_export = false;
1726 }
1727
Amir Goldstein8383f172017-10-02 11:31:42 +03001728 if (ofs->config.nfs_export)
1729 sb->s_export_op = &ovl_export_operations;
1730
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001731 /* Never override disk quota limits or use reserved space */
1732 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1733
Vivek Goyal655042c2016-10-14 03:03:36 +02001734 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Vivek Goyal655042c2016-10-14 03:03:36 +02001735 sb->s_xattr = ovl_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01001736 sb->s_fs_info = ofs;
Miklos Szeredide2a4a52018-07-18 15:44:43 +02001737 sb->s_flags |= SB_POSIXACL;
Vivek Goyal655042c2016-10-14 03:03:36 +02001738
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001739 err = -ENOMEM;
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001740 root_dentry = ovl_get_root(sb, upperpath.dentry, oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001741 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001742 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001743
1744 mntput(upperpath.mnt);
Miklos Szeredied06e062015-12-09 16:11:59 +01001745
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001746 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001747
1748 return 0;
1749
Miklos Szeredi4155c102017-11-10 09:39:15 +01001750out_free_oe:
1751 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001752 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001753out_err:
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001754 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01001755 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001756out:
1757 return err;
1758}
1759
1760static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1761 const char *dev_name, void *raw_data)
1762{
1763 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1764}
1765
1766static struct file_system_type ovl_fs_type = {
1767 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001768 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001769 .mount = ovl_mount,
1770 .kill_sb = kill_anon_super,
1771};
Miklos Szeredief94b182014-11-20 16:39:59 +01001772MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001773
Amir Goldstein13cf1992017-06-12 09:54:40 +03001774static void ovl_inode_init_once(void *foo)
1775{
1776 struct ovl_inode *oi = foo;
1777
1778 inode_init_once(&oi->vfs_inode);
1779}
1780
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001781static int __init ovl_init(void)
1782{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001783 int err;
1784
1785 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1786 sizeof(struct ovl_inode), 0,
1787 (SLAB_RECLAIM_ACCOUNT|
1788 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1789 ovl_inode_init_once);
1790 if (ovl_inode_cachep == NULL)
1791 return -ENOMEM;
1792
Jiufei Xue2406a302019-11-20 17:45:26 +08001793 err = ovl_aio_request_cache_init();
1794 if (!err) {
1795 err = register_filesystem(&ovl_fs_type);
1796 if (!err)
1797 return 0;
1798
1799 ovl_aio_request_cache_destroy();
1800 }
1801 kmem_cache_destroy(ovl_inode_cachep);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001802
1803 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001804}
1805
1806static void __exit ovl_exit(void)
1807{
1808 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001809
1810 /*
1811 * Make sure all delayed rcu free inodes are flushed before we
1812 * destroy cache.
1813 */
1814 rcu_barrier();
1815 kmem_cache_destroy(ovl_inode_cachep);
Jiufei Xue2406a302019-11-20 17:45:26 +08001816 ovl_aio_request_cache_destroy();
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001817}
1818
1819module_init(ovl_init);
1820module_exit(ovl_exit);