blob: 4717244e7d7a4e44a1ef2b0c04d78cac38a0f743 [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;
Miklos Szeredibccece12020-03-17 15:04:22 +0100138 struct dentry *upper;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200139 unsigned int i;
140 int ret = 1;
141
Miklos Szeredibccece12020-03-17 15:04:22 +0100142 upper = ovl_dentry_upper(dentry);
143 if (upper)
144 ret = ovl_revalidate_real(upper, flags, weak);
145
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100146 for (i = 0; ret > 0 && i < oe->numlower; i++) {
147 ret = ovl_revalidate_real(oe->lowerstack[i].dentry, flags,
148 weak);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200149 }
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100150 return ret;
151}
152
153static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
154{
155 return ovl_dentry_revalidate_common(dentry, flags, false);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200156}
157
158static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
159{
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100160 return ovl_dentry_revalidate_common(dentry, flags, true);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200161}
162
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200163static const struct dentry_operations ovl_dentry_operations = {
164 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400165 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200166 .d_revalidate = ovl_dentry_revalidate,
167 .d_weak_revalidate = ovl_dentry_weak_revalidate,
168};
169
Amir Goldstein13cf1992017-06-12 09:54:40 +0300170static struct kmem_cache *ovl_inode_cachep;
171
172static struct inode *ovl_alloc_inode(struct super_block *sb)
173{
174 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
175
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900176 if (!oi)
177 return NULL;
178
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200179 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200180 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200181 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200182 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200183 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200184 oi->lower = NULL;
Vivek Goyal2664bd02018-05-11 11:49:30 -0400185 oi->lowerdata = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300186 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200187
Amir Goldstein13cf1992017-06-12 09:54:40 +0300188 return &oi->vfs_inode;
189}
190
Al Viro0b269de2019-04-15 22:52:17 -0400191static void ovl_free_inode(struct inode *inode)
Amir Goldstein13cf1992017-06-12 09:54:40 +0300192{
Al Viro0b269de2019-04-15 22:52:17 -0400193 struct ovl_inode *oi = OVL_I(inode);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300194
Al Viro0b269de2019-04-15 22:52:17 -0400195 kfree(oi->redirect);
196 mutex_destroy(&oi->lock);
197 kmem_cache_free(ovl_inode_cachep, oi);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300198}
199
200static void ovl_destroy_inode(struct inode *inode)
201{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200202 struct ovl_inode *oi = OVL_I(inode);
203
204 dput(oi->__upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200205 iput(oi->lower);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400206 if (S_ISDIR(inode->i_mode))
207 ovl_dir_cache_free(inode);
208 else
209 iput(oi->lowerdata);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300210}
211
Miklos Szerediad204482017-11-10 09:39:16 +0100212static void ovl_free_fs(struct ovl_fs *ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200213{
Miklos Szeredidf820f82020-06-04 10:48:19 +0200214 struct vfsmount **mounts;
Miklos Szeredidd662662014-12-13 00:59:43 +0100215 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200216
Amir Goldstein0be0bfd2019-07-12 15:24:34 +0300217 iput(ofs->workbasedir_trap);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300218 iput(ofs->indexdir_trap);
219 iput(ofs->workdir_trap);
Chengguang Xuc21c8392020-04-24 10:55:17 +0800220 dput(ofs->whiteout);
Miklos Szerediad204482017-11-10 09:39:16 +0100221 dput(ofs->indexdir);
222 dput(ofs->workdir);
223 if (ofs->workdir_locked)
224 ovl_inuse_unlock(ofs->workbasedir);
225 dput(ofs->workbasedir);
226 if (ofs->upperdir_locked)
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200227 ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root);
Miklos Szeredidf820f82020-06-04 10:48:19 +0200228
229 /* Hack! Reuse ofs->layers as a vfsmount array before freeing it */
230 mounts = (struct vfsmount **) ofs->layers;
Miklos Szeredib8e42a62020-06-04 10:48:19 +0200231 for (i = 0; i < ofs->numlayer; i++) {
Amir Goldstein94375f92019-11-15 14:12:40 +0200232 iput(ofs->layers[i].trap);
Miklos Szeredidf820f82020-06-04 10:48:19 +0200233 mounts[i] = ofs->layers[i].mnt;
Amir Goldstein146d62e2019-04-18 17:42:08 +0300234 }
Miklos Szeredidf820f82020-06-04 10:48:19 +0200235 kern_unmount_array(mounts, ofs->numlayer);
Amir Goldstein94375f92019-11-15 14:12:40 +0200236 kfree(ofs->layers);
Amir Goldsteinb7bf9902020-01-14 22:17:25 +0200237 for (i = 0; i < ofs->numfs; i++)
Amir Goldstein07f1e592020-01-14 21:59:22 +0200238 free_anon_bdev(ofs->fs[i].pseudo_dev);
239 kfree(ofs->fs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200240
Miklos Szerediad204482017-11-10 09:39:16 +0100241 kfree(ofs->config.lowerdir);
242 kfree(ofs->config.upperdir);
243 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100244 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100245 if (ofs->creator_cred)
246 put_cred(ofs->creator_cred);
247 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200248}
249
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100250static void ovl_put_super(struct super_block *sb)
251{
252 struct ovl_fs *ofs = sb->s_fs_info;
253
254 ovl_free_fs(ofs);
255}
256
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800257/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200258static int ovl_sync_fs(struct super_block *sb, int wait)
259{
Miklos Szerediad204482017-11-10 09:39:16 +0100260 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200261 struct super_block *upper_sb;
262 int ret;
263
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200264 if (!ovl_upper_mnt(ofs))
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200265 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800266
Vivek Goyalc86243b02020-08-31 14:15:29 -0400267 if (!ovl_should_sync(ofs))
268 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800269 /*
Konstantin Khlebnikov32b1924b22020-04-09 11:29:47 +0300270 * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
271 * All the super blocks will be iterated, including upper_sb.
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800272 *
273 * If this is a syncfs(2) call, then we do need to call
274 * sync_filesystem() on upper_sb, but enough if we do it when being
275 * called with wait == 1.
276 */
277 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200278 return 0;
279
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200280 upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800281
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200282 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800283 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200284 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800285
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200286 return ret;
287}
288
Andy Whitcroftcc259632014-10-24 00:14:38 +0200289/**
290 * ovl_statfs
291 * @sb: The overlayfs super block
292 * @buf: The struct kstatfs to fill in with stats
293 *
294 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100295 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200296 */
297static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
298{
299 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
300 struct dentry *root_dentry = dentry->d_sb->s_root;
301 struct path path;
302 int err;
303
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100304 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200305
306 err = vfs_statfs(&path, buf);
307 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100308 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200309 buf->f_type = OVERLAYFS_SUPER_MAGIC;
310 }
311
312 return err;
313}
314
Amir Goldstein02bcd152017-06-21 15:28:36 +0300315/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100316static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300317{
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200318 return (!ovl_upper_mnt(ofs) || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300319}
320
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100321static const char *ovl_redirect_mode_def(void)
322{
323 return ovl_redirect_dir_def ? "on" : "off";
324}
325
Amir Goldstein795939a2018-03-29 09:08:18 +0300326static const char * const ovl_xino_str[] = {
327 "off",
328 "auto",
329 "on",
330};
331
332static inline int ovl_xino_def(void)
333{
334 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
335}
336
Erez Zadokf45827e82014-10-24 00:14:38 +0200337/**
338 * ovl_show_options
339 *
340 * Prints the mount options for a given superblock.
341 * Returns zero; does not fail.
342 */
343static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
344{
345 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100346 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200347
Miklos Szerediad204482017-11-10 09:39:16 +0100348 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
349 if (ofs->config.upperdir) {
350 seq_show_option(m, "upperdir", ofs->config.upperdir);
351 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100352 }
Miklos Szerediad204482017-11-10 09:39:16 +0100353 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200354 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100355 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
356 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100357 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100358 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300359 if (!ofs->config.uuid)
360 seq_puts(m, ",uuid=off");
Amir Goldsteinf168f102018-01-19 11:26:53 +0200361 if (ofs->config.nfs_export != ovl_nfs_export_def)
362 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
363 "on" : "off");
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200364 if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(sb))
Amir Goldstein795939a2018-03-29 09:08:18 +0300365 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
Vivek Goyald5791042018-05-11 11:49:27 -0400366 if (ofs->config.metacopy != ovl_metacopy_def)
367 seq_printf(m, ",metacopy=%s",
368 ofs->config.metacopy ? "on" : "off");
Vivek Goyalc86243b02020-08-31 14:15:29 -0400369 if (ofs->config.ovl_volatile)
370 seq_puts(m, ",volatile");
Erez Zadokf45827e82014-10-24 00:14:38 +0200371 return 0;
372}
373
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900374static int ovl_remount(struct super_block *sb, int *flags, char *data)
375{
Miklos Szerediad204482017-11-10 09:39:16 +0100376 struct ovl_fs *ofs = sb->s_fs_info;
Chengguang Xu399c1092020-04-22 12:28:43 +0800377 struct super_block *upper_sb;
378 int ret = 0;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900379
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800380 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900381 return -EROFS;
382
Chengguang Xu399c1092020-04-22 12:28:43 +0800383 if (*flags & SB_RDONLY && !sb_rdonly(sb)) {
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200384 upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
Vivek Goyalc86243b02020-08-31 14:15:29 -0400385 if (ovl_should_sync(ofs)) {
386 down_read(&upper_sb->s_umount);
387 ret = sync_filesystem(upper_sb);
388 up_read(&upper_sb->s_umount);
389 }
Chengguang Xu399c1092020-04-22 12:28:43 +0800390 }
391
392 return ret;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900393}
394
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200395static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300396 .alloc_inode = ovl_alloc_inode,
Al Viro0b269de2019-04-15 22:52:17 -0400397 .free_inode = ovl_free_inode,
Amir Goldstein13cf1992017-06-12 09:54:40 +0300398 .destroy_inode = ovl_destroy_inode,
399 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200400 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200401 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200402 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200403 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900404 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200405};
406
407enum {
408 OPT_LOWERDIR,
409 OPT_UPPERDIR,
410 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200411 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100412 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300413 OPT_INDEX_ON,
414 OPT_INDEX_OFF,
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300415 OPT_UUID_ON,
416 OPT_UUID_OFF,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200417 OPT_NFS_EXPORT_ON,
418 OPT_NFS_EXPORT_OFF,
Amir Goldstein795939a2018-03-29 09:08:18 +0300419 OPT_XINO_ON,
420 OPT_XINO_OFF,
421 OPT_XINO_AUTO,
Vivek Goyald5791042018-05-11 11:49:27 -0400422 OPT_METACOPY_ON,
423 OPT_METACOPY_OFF,
Vivek Goyalc86243b02020-08-31 14:15:29 -0400424 OPT_VOLATILE,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200425 OPT_ERR,
426};
427
428static const match_table_t ovl_tokens = {
429 {OPT_LOWERDIR, "lowerdir=%s"},
430 {OPT_UPPERDIR, "upperdir=%s"},
431 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200432 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100433 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300434 {OPT_INDEX_ON, "index=on"},
435 {OPT_INDEX_OFF, "index=off"},
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300436 {OPT_UUID_ON, "uuid=on"},
437 {OPT_UUID_OFF, "uuid=off"},
Amir Goldsteinf168f102018-01-19 11:26:53 +0200438 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
439 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
Amir Goldstein795939a2018-03-29 09:08:18 +0300440 {OPT_XINO_ON, "xino=on"},
441 {OPT_XINO_OFF, "xino=off"},
442 {OPT_XINO_AUTO, "xino=auto"},
Vivek Goyald5791042018-05-11 11:49:27 -0400443 {OPT_METACOPY_ON, "metacopy=on"},
444 {OPT_METACOPY_OFF, "metacopy=off"},
Vivek Goyalc86243b02020-08-31 14:15:29 -0400445 {OPT_VOLATILE, "volatile"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200446 {OPT_ERR, NULL}
447};
448
Miklos Szeredi91c77942014-11-20 16:40:00 +0100449static char *ovl_next_opt(char **s)
450{
451 char *sbegin = *s;
452 char *p;
453
454 if (sbegin == NULL)
455 return NULL;
456
457 for (p = sbegin; *p; p++) {
458 if (*p == '\\') {
459 p++;
460 if (!*p)
461 break;
462 } else if (*p == ',') {
463 *p = '\0';
464 *s = p + 1;
465 return sbegin;
466 }
467 }
468 *s = NULL;
469 return sbegin;
470}
471
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100472static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
473{
474 if (strcmp(mode, "on") == 0) {
475 config->redirect_dir = true;
476 /*
477 * Does not make sense to have redirect creation without
478 * redirect following.
479 */
480 config->redirect_follow = true;
481 } else if (strcmp(mode, "follow") == 0) {
482 config->redirect_follow = true;
483 } else if (strcmp(mode, "off") == 0) {
484 if (ovl_redirect_always_follow)
485 config->redirect_follow = true;
486 } else if (strcmp(mode, "nofollow") != 0) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800487 pr_err("bad mount option \"redirect_dir=%s\"\n",
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100488 mode);
489 return -EINVAL;
490 }
491
492 return 0;
493}
494
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200495static int ovl_parse_opt(char *opt, struct ovl_config *config)
496{
497 char *p;
Vivek Goyald5791042018-05-11 11:49:27 -0400498 int err;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100499 bool metacopy_opt = false, redirect_opt = false;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300500 bool nfs_export_opt = false, index_opt = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200501
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100502 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
503 if (!config->redirect_mode)
504 return -ENOMEM;
505
Miklos Szeredi91c77942014-11-20 16:40:00 +0100506 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200507 int token;
508 substring_t args[MAX_OPT_ARGS];
509
510 if (!*p)
511 continue;
512
513 token = match_token(p, ovl_tokens, args);
514 switch (token) {
515 case OPT_UPPERDIR:
516 kfree(config->upperdir);
517 config->upperdir = match_strdup(&args[0]);
518 if (!config->upperdir)
519 return -ENOMEM;
520 break;
521
522 case OPT_LOWERDIR:
523 kfree(config->lowerdir);
524 config->lowerdir = match_strdup(&args[0]);
525 if (!config->lowerdir)
526 return -ENOMEM;
527 break;
528
529 case OPT_WORKDIR:
530 kfree(config->workdir);
531 config->workdir = match_strdup(&args[0]);
532 if (!config->workdir)
533 return -ENOMEM;
534 break;
535
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200536 case OPT_DEFAULT_PERMISSIONS:
537 config->default_permissions = true;
538 break;
539
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100540 case OPT_REDIRECT_DIR:
541 kfree(config->redirect_mode);
542 config->redirect_mode = match_strdup(&args[0]);
543 if (!config->redirect_mode)
544 return -ENOMEM;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100545 redirect_opt = true;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100546 break;
547
Amir Goldstein02bcd152017-06-21 15:28:36 +0300548 case OPT_INDEX_ON:
549 config->index = true;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300550 index_opt = true;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300551 break;
552
553 case OPT_INDEX_OFF:
554 config->index = false;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300555 index_opt = true;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300556 break;
557
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300558 case OPT_UUID_ON:
559 config->uuid = true;
560 break;
561
562 case OPT_UUID_OFF:
563 config->uuid = false;
564 break;
565
Amir Goldsteinf168f102018-01-19 11:26:53 +0200566 case OPT_NFS_EXPORT_ON:
567 config->nfs_export = true;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300568 nfs_export_opt = true;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200569 break;
570
571 case OPT_NFS_EXPORT_OFF:
572 config->nfs_export = false;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300573 nfs_export_opt = true;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200574 break;
575
Amir Goldstein795939a2018-03-29 09:08:18 +0300576 case OPT_XINO_ON:
577 config->xino = OVL_XINO_ON;
578 break;
579
580 case OPT_XINO_OFF:
581 config->xino = OVL_XINO_OFF;
582 break;
583
584 case OPT_XINO_AUTO:
585 config->xino = OVL_XINO_AUTO;
586 break;
587
Vivek Goyald5791042018-05-11 11:49:27 -0400588 case OPT_METACOPY_ON:
589 config->metacopy = true;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100590 metacopy_opt = true;
Vivek Goyald5791042018-05-11 11:49:27 -0400591 break;
592
593 case OPT_METACOPY_OFF:
594 config->metacopy = false;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300595 metacopy_opt = true;
Vivek Goyald5791042018-05-11 11:49:27 -0400596 break;
597
Vivek Goyalc86243b02020-08-31 14:15:29 -0400598 case OPT_VOLATILE:
599 config->ovl_volatile = true;
600 break;
601
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200602 default:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800603 pr_err("unrecognized mount option \"%s\" or missing value\n",
604 p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200605 return -EINVAL;
606 }
607 }
hujianyang71cbad72015-01-15 13:20:57 +0800608
Amir Goldsteinf0e1266e2020-07-13 17:19:44 +0300609 /* Workdir/index are useless in non-upper mount */
610 if (!config->upperdir) {
611 if (config->workdir) {
612 pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
613 config->workdir);
614 kfree(config->workdir);
615 config->workdir = NULL;
616 }
617 if (config->index && index_opt) {
618 pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
619 index_opt = false;
620 }
621 config->index = false;
hujianyang71cbad72015-01-15 13:20:57 +0800622 }
623
Vivek Goyalc86243b02020-08-31 14:15:29 -0400624 if (!config->upperdir && config->ovl_volatile) {
625 pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
626 config->ovl_volatile = false;
627 }
628
Vivek Goyald5791042018-05-11 11:49:27 -0400629 err = ovl_parse_redirect_mode(config, config->redirect_mode);
630 if (err)
631 return err;
632
Miklos Szeredid47748e2018-11-01 21:31:39 +0100633 /*
634 * This is to make the logic below simpler. It doesn't make any other
635 * difference, since config->redirect_dir is only used for upper.
636 */
637 if (!config->upperdir && config->redirect_follow)
638 config->redirect_dir = true;
639
640 /* Resolve metacopy -> redirect_dir dependency */
641 if (config->metacopy && !config->redirect_dir) {
642 if (metacopy_opt && redirect_opt) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800643 pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
Miklos Szeredid47748e2018-11-01 21:31:39 +0100644 config->redirect_mode);
645 return -EINVAL;
646 }
647 if (redirect_opt) {
648 /*
649 * There was an explicit redirect_dir=... that resulted
650 * in this conflict.
651 */
lijiazi1bd0a3a2019-12-16 19:12:32 +0800652 pr_info("disabling metacopy due to redirect_dir=%s\n",
Miklos Szeredid47748e2018-11-01 21:31:39 +0100653 config->redirect_mode);
654 config->metacopy = false;
655 } else {
656 /* Automatically enable redirect otherwise. */
657 config->redirect_follow = config->redirect_dir = true;
658 }
Vivek Goyald5791042018-05-11 11:49:27 -0400659 }
660
Amir Goldsteinb0def882020-04-09 18:58:34 +0300661 /* Resolve nfs_export -> index dependency */
662 if (config->nfs_export && !config->index) {
Amir Goldsteinf0e1266e2020-07-13 17:19:44 +0300663 if (!config->upperdir && config->redirect_follow) {
664 pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
665 config->nfs_export = false;
666 } else if (nfs_export_opt && index_opt) {
Amir Goldsteinb0def882020-04-09 18:58:34 +0300667 pr_err("conflicting options: nfs_export=on,index=off\n");
668 return -EINVAL;
Amir Goldsteinf0e1266e2020-07-13 17:19:44 +0300669 } else if (index_opt) {
Amir Goldsteinb0def882020-04-09 18:58:34 +0300670 /*
671 * There was an explicit index=off that resulted
672 * in this conflict.
673 */
674 pr_info("disabling nfs_export due to index=off\n");
675 config->nfs_export = false;
676 } else {
677 /* Automatically enable index otherwise. */
678 config->index = true;
679 }
680 }
681
682 /* Resolve nfs_export -> !metacopy dependency */
683 if (config->nfs_export && config->metacopy) {
684 if (nfs_export_opt && metacopy_opt) {
685 pr_err("conflicting options: nfs_export=on,metacopy=on\n");
686 return -EINVAL;
687 }
688 if (metacopy_opt) {
689 /*
690 * There was an explicit metacopy=on that resulted
691 * in this conflict.
692 */
693 pr_info("disabling nfs_export due to metacopy=on\n");
694 config->nfs_export = false;
695 } else {
696 /*
697 * There was an explicit nfs_export=on that resulted
698 * in this conflict.
699 */
700 pr_info("disabling metacopy due to nfs_export=on\n");
701 config->metacopy = false;
702 }
703 }
704
Vivek Goyald5791042018-05-11 11:49:27 -0400705 return 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200706}
707
708#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300709#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200710
Miklos Szerediad204482017-11-10 09:39:16 +0100711static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300712 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200713{
Miklos Szerediad204482017-11-10 09:39:16 +0100714 struct inode *dir = ofs->workbasedir->d_inode;
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200715 struct vfsmount *mnt = ovl_upper_mnt(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200716 struct dentry *work;
717 int err;
718 bool retried = false;
719
Al Viro59551022016-01-22 15:40:57 -0500720 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200721retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100722 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200723
724 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200725 struct iattr attr = {
726 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000727 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200728 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200729
730 if (work->d_inode) {
731 err = -EEXIST;
732 if (retried)
733 goto out_dput;
734
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300735 if (persist)
736 goto out_unlock;
737
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200738 retried = true;
Amir Goldstein235ce9e2020-08-30 23:28:03 +0300739 err = ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200740 dput(work);
Amir Goldstein235ce9e2020-08-30 23:28:03 +0300741 if (err == -EINVAL) {
742 work = ERR_PTR(err);
743 goto out_unlock;
744 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200745 goto retry;
746 }
747
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300748 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
749 err = PTR_ERR(work);
750 if (IS_ERR(work))
751 goto out_err;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200752
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200753 /*
754 * Try to remove POSIX ACL xattrs from workdir. We are good if:
755 *
756 * a) success (there was a POSIX ACL xattr and was removed)
757 * b) -ENODATA (there was no POSIX ACL xattr)
758 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
759 *
760 * There are various other error values that could effectively
761 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
762 * if the xattr name is too long), but the set of filesystems
763 * allowed as upper are limited to "normal" ones, where checking
764 * for the above two errors is sufficient.
765 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200766 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200767 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200768 goto out_dput;
769
770 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200771 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200772 goto out_dput;
773
774 /* Clear any inherited mode bits */
775 inode_lock(work->d_inode);
776 err = notify_change(work, &attr, NULL);
777 inode_unlock(work->d_inode);
778 if (err)
779 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300780 } else {
781 err = PTR_ERR(work);
782 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200783 }
784out_unlock:
youngjun2068cf72020-06-07 02:04:06 -0700785 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200786 return work;
787
788out_dput:
789 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300790out_err:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800791 pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100792 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300793 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200794 goto out_unlock;
795}
796
Miklos Szeredi91c77942014-11-20 16:40:00 +0100797static void ovl_unescape(char *s)
798{
799 char *d = s;
800
801 for (;; s++, d++) {
802 if (*s == '\\')
803 s++;
804 *d = *s;
805 if (!*s)
806 break;
807 }
808}
809
Miklos Szerediab508822014-12-13 00:59:49 +0100810static int ovl_mount_dir_noesc(const char *name, struct path *path)
811{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100812 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100813
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100814 if (!*name) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800815 pr_err("empty lowerdir\n");
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100816 goto out;
817 }
Miklos Szerediab508822014-12-13 00:59:49 +0100818 err = kern_path(name, LOOKUP_FOLLOW, path);
819 if (err) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800820 pr_err("failed to resolve '%s': %i\n", name, err);
Miklos Szerediab508822014-12-13 00:59:49 +0100821 goto out;
822 }
823 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200824 if (ovl_dentry_weird(path->dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800825 pr_err("filesystem on '%s' not supported\n", name);
Miklos Szerediab508822014-12-13 00:59:49 +0100826 goto out_put;
827 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100828 if (!d_is_dir(path->dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800829 pr_err("'%s' not a directory\n", name);
Miklos Szerediab508822014-12-13 00:59:49 +0100830 goto out_put;
831 }
832 return 0;
833
834out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100835 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100836out:
837 return err;
838}
839
840static int ovl_mount_dir(const char *name, struct path *path)
841{
842 int err = -ENOMEM;
843 char *tmp = kstrdup(name, GFP_KERNEL);
844
845 if (tmp) {
846 ovl_unescape(tmp);
847 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200848
Miklos Szeredibccece12020-03-17 15:04:22 +0100849 if (!err && path->dentry->d_flags & DCACHE_OP_REAL) {
Miklos Szeredi7925dad2020-03-17 15:04:22 +0100850 pr_err("filesystem on '%s' not supported as upperdir\n",
851 tmp);
852 path_put_init(path);
853 err = -EINVAL;
854 }
Miklos Szerediab508822014-12-13 00:59:49 +0100855 kfree(tmp);
856 }
857 return err;
858}
859
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100860static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
861 const char *name)
862{
863 struct kstatfs statfs;
864 int err = vfs_statfs(path, &statfs);
865
866 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +0800867 pr_err("statfs failed on '%s'\n", name);
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100868 else
869 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
870
871 return err;
872}
873
874static int ovl_lower_dir(const char *name, struct path *path,
Miklos Szeredif4288842020-03-17 15:04:22 +0100875 struct ovl_fs *ofs, int *stack_depth)
Miklos Szerediab508822014-12-13 00:59:49 +0100876{
Amir Goldsteine487d882017-11-07 13:55:04 +0200877 int fh_type;
Miklos Szerediab508822014-12-13 00:59:49 +0100878 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100879
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100880 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100881 if (err)
Miklos Szeredib8e42a62020-06-04 10:48:19 +0200882 return err;
Miklos Szerediab508822014-12-13 00:59:49 +0100883
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100884 err = ovl_check_namelen(path, ofs, name);
885 if (err)
Miklos Szeredib8e42a62020-06-04 10:48:19 +0200886 return err;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100887
Miklos Szerediab508822014-12-13 00:59:49 +0100888 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
889
Amir Goldstein02bcd152017-06-21 15:28:36 +0300890 /*
Amir Goldsteinf168f102018-01-19 11:26:53 +0200891 * The inodes index feature and NFS export need to encode and decode
892 * file handles, so they require that all layers support them.
Amir Goldstein02bcd152017-06-21 15:28:36 +0300893 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200894 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
Amir Goldsteinf168f102018-01-19 11:26:53 +0200895 if ((ofs->config.nfs_export ||
Amir Goldsteine487d882017-11-07 13:55:04 +0200896 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300897 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200898 ofs->config.nfs_export = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +0800899 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 +0200900 name);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300901 }
902
Amir Goldsteine487d882017-11-07 13:55:04 +0200903 /* Check if lower fs has 32bit inode numbers */
904 if (fh_type != FILEID_INO32_GEN)
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200905 ofs->xino_mode = -1;
Amir Goldsteine487d882017-11-07 13:55:04 +0200906
Miklos Szerediab508822014-12-13 00:59:49 +0100907 return 0;
Miklos Szerediab508822014-12-13 00:59:49 +0100908}
909
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200910/* Workdir should not be subdir of upperdir and vice versa */
911static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
912{
913 bool ok = false;
914
915 if (workdir != upperdir) {
916 ok = (lock_rename(workdir, upperdir) == NULL);
917 unlock_rename(workdir, upperdir);
918 }
919 return ok;
920}
921
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100922static unsigned int ovl_split_lowerdirs(char *str)
923{
924 unsigned int ctr = 1;
925 char *s, *d;
926
927 for (s = d = str;; s++, d++) {
928 if (*s == '\\') {
929 s++;
930 } else if (*s == ':') {
931 *d = '\0';
932 ctr++;
933 continue;
934 }
935 *d = *s;
936 if (!*s)
937 break;
938 }
939 return ctr;
940}
941
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200942static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200943ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
944 struct dentry *dentry, struct inode *inode,
945 const char *name, void *buffer, size_t size)
946{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200947 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200948}
949
950static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200951ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
952 struct dentry *dentry, struct inode *inode,
953 const char *name, const void *value,
954 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200955{
956 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200957 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200958 struct posix_acl *acl = NULL;
959 int err;
960
961 /* Check that everything is OK before copy-up */
962 if (value) {
963 acl = posix_acl_from_xattr(&init_user_ns, value, size);
964 if (IS_ERR(acl))
965 return PTR_ERR(acl);
966 }
967 err = -EOPNOTSUPP;
968 if (!IS_POSIXACL(d_inode(workdir)))
969 goto out_acl_release;
970 if (!realinode->i_op->set_acl)
971 goto out_acl_release;
972 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
973 err = acl ? -EACCES : 0;
974 goto out_acl_release;
975 }
976 err = -EPERM;
977 if (!inode_owner_or_capable(inode))
978 goto out_acl_release;
979
980 posix_acl_release(acl);
981
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100982 /*
983 * Check if sgid bit needs to be cleared (actual setacl operation will
984 * be done with mounter's capabilities and so that won't do it for us).
985 */
986 if (unlikely(inode->i_mode & S_ISGID) &&
987 handler->flags == ACL_TYPE_ACCESS &&
988 !in_group_p(inode->i_gid) &&
989 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
990 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
991
992 err = ovl_setattr(dentry, &iattr);
993 if (err)
994 return err;
995 }
996
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200997 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200998 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200999 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +02001000
1001 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +02001002
1003out_acl_release:
1004 posix_acl_release(acl);
1005 return err;
1006}
1007
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001008static int ovl_own_xattr_get(const struct xattr_handler *handler,
1009 struct dentry *dentry, struct inode *inode,
1010 const char *name, void *buffer, size_t size)
1011{
Amir Goldstein48fab5d2016-11-16 11:22:39 +02001012 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001013}
1014
Miklos Szeredid837a492016-07-29 12:05:24 +02001015static int ovl_own_xattr_set(const struct xattr_handler *handler,
1016 struct dentry *dentry, struct inode *inode,
1017 const char *name, const void *value,
1018 size_t size, int flags)
1019{
Amir Goldstein48fab5d2016-11-16 11:22:39 +02001020 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +02001021}
1022
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001023static int ovl_other_xattr_get(const struct xattr_handler *handler,
1024 struct dentry *dentry, struct inode *inode,
1025 const char *name, void *buffer, size_t size)
1026{
Miklos Szeredi1d88f182017-07-20 11:08:21 +02001027 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001028}
1029
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +02001030static int ovl_other_xattr_set(const struct xattr_handler *handler,
1031 struct dentry *dentry, struct inode *inode,
1032 const char *name, const void *value,
1033 size_t size, int flags)
1034{
Miklos Szeredi1d88f182017-07-20 11:08:21 +02001035 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +02001036}
1037
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +02001038static const struct xattr_handler __maybe_unused
1039ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +02001040 .name = XATTR_NAME_POSIX_ACL_ACCESS,
1041 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001042 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +02001043 .set = ovl_posix_acl_xattr_set,
1044};
1045
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +02001046static const struct xattr_handler __maybe_unused
1047ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +02001048 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
1049 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001050 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +02001051 .set = ovl_posix_acl_xattr_set,
1052};
1053
1054static const struct xattr_handler ovl_own_xattr_handler = {
1055 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001056 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +02001057 .set = ovl_own_xattr_set,
1058};
1059
1060static const struct xattr_handler ovl_other_xattr_handler = {
1061 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001062 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +02001063 .set = ovl_other_xattr_set,
1064};
1065
1066static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +02001067#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +02001068 &ovl_posix_acl_access_xattr_handler,
1069 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +02001070#endif
Miklos Szeredid837a492016-07-29 12:05:24 +02001071 &ovl_own_xattr_handler,
1072 &ovl_other_xattr_handler,
1073 NULL
1074};
1075
Amir Goldstein146d62e2019-04-18 17:42:08 +03001076static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
1077 struct inode **ptrap, const char *name)
1078{
1079 struct inode *trap;
1080 int err;
1081
1082 trap = ovl_get_trap_inode(sb, dir);
Arnd Bergmann1dac6f5b2019-06-17 14:39:29 +02001083 err = PTR_ERR_OR_ZERO(trap);
1084 if (err) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001085 if (err == -ELOOP)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001086 pr_err("conflicting %s path\n", name);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001087 return err;
1088 }
1089
1090 *ptrap = trap;
1091 return 0;
1092}
1093
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001094/*
1095 * Determine how we treat concurrent use of upperdir/workdir based on the
1096 * index feature. This is papering over mount leaks of container runtimes,
1097 * for example, an old overlay mount is leaked and now its upperdir is
1098 * attempted to be used as a lower layer in a new overlay mount.
1099 */
1100static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
1101{
1102 if (ofs->config.index) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001103 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 +03001104 name);
1105 return -EBUSY;
1106 } else {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001107 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 +03001108 name);
1109 return 0;
1110 }
1111}
1112
Amir Goldstein146d62e2019-04-18 17:42:08 +03001113static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001114 struct ovl_layer *upper_layer, struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001115{
Miklos Szeredi50649752017-11-10 09:39:15 +01001116 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001117 int err;
1118
Miklos Szerediad204482017-11-10 09:39:16 +01001119 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001120 if (err)
1121 goto out;
1122
1123 /* Upper fs should not be r/o */
1124 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001125 pr_err("upper fs is r/o, try multi-lower layers mount\n");
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001126 err = -EINVAL;
1127 goto out;
1128 }
1129
Miklos Szerediad204482017-11-10 09:39:16 +01001130 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001131 if (err)
1132 goto out;
1133
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001134 err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
Amir Goldstein146d62e2019-04-18 17:42:08 +03001135 "upperdir");
1136 if (err)
1137 goto out;
1138
Miklos Szeredi50649752017-11-10 09:39:15 +01001139 upper_mnt = clone_private_mount(upperpath);
1140 err = PTR_ERR(upper_mnt);
1141 if (IS_ERR(upper_mnt)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001142 pr_err("failed to clone upperpath\n");
Miklos Szeredi50649752017-11-10 09:39:15 +01001143 goto out;
1144 }
1145
1146 /* Don't inherit atime flags */
1147 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001148 upper_layer->mnt = upper_mnt;
1149 upper_layer->idx = 0;
1150 upper_layer->fsid = 0;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001151
Jeffle Xu654255f2020-04-23 19:06:55 +08001152 /*
1153 * Inherit SB_NOSEC flag from upperdir.
1154 *
1155 * This optimization changes behavior when a security related attribute
1156 * (suid/sgid/security.*) is changed on an underlying layer. This is
1157 * okay because we don't yet have guarantees in that case, but it will
1158 * need careful treatment once we want to honour changes to underlying
1159 * filesystems.
1160 */
1161 if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
1162 sb->s_flags |= SB_NOSEC;
1163
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001164 if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
Miklos Szeredi8c257412018-09-10 11:43:29 +02001165 ofs->upperdir_locked = true;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001166 } else {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001167 err = ovl_report_in_use(ofs, "upperdir");
1168 if (err)
1169 goto out;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001170 }
1171
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001172 err = 0;
1173out:
1174 return err;
1175}
1176
Amir Goldsteincad218a2020-02-20 09:00:19 +02001177/*
1178 * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
1179 * negative values if error is encountered.
1180 */
1181static int ovl_check_rename_whiteout(struct dentry *workdir)
1182{
1183 struct inode *dir = d_inode(workdir);
1184 struct dentry *temp;
1185 struct dentry *dest;
1186 struct dentry *whiteout;
1187 struct name_snapshot name;
1188 int err;
1189
1190 inode_lock_nested(dir, I_MUTEX_PARENT);
1191
1192 temp = ovl_create_temp(workdir, OVL_CATTR(S_IFREG | 0));
1193 err = PTR_ERR(temp);
1194 if (IS_ERR(temp))
1195 goto out_unlock;
1196
1197 dest = ovl_lookup_temp(workdir);
1198 err = PTR_ERR(dest);
1199 if (IS_ERR(dest)) {
1200 dput(temp);
1201 goto out_unlock;
1202 }
1203
1204 /* Name is inline and stable - using snapshot as a copy helper */
1205 take_dentry_name_snapshot(&name, temp);
1206 err = ovl_do_rename(dir, temp, dir, dest, RENAME_WHITEOUT);
1207 if (err) {
1208 if (err == -EINVAL)
1209 err = 0;
1210 goto cleanup_temp;
1211 }
1212
1213 whiteout = lookup_one_len(name.name.name, workdir, name.name.len);
1214 err = PTR_ERR(whiteout);
1215 if (IS_ERR(whiteout))
1216 goto cleanup_temp;
1217
1218 err = ovl_is_whiteout(whiteout);
1219
1220 /* Best effort cleanup of whiteout and temp file */
1221 if (err)
1222 ovl_cleanup(dir, whiteout);
1223 dput(whiteout);
1224
1225cleanup_temp:
1226 ovl_cleanup(dir, temp);
1227 release_dentry_name_snapshot(&name);
1228 dput(temp);
1229 dput(dest);
1230
1231out_unlock:
1232 inode_unlock(dir);
1233
1234 return err;
1235}
1236
Vivek Goyalc86243b02020-08-31 14:15:29 -04001237static struct dentry *ovl_lookup_or_create(struct dentry *parent,
1238 const char *name, umode_t mode)
1239{
1240 size_t len = strlen(name);
1241 struct dentry *child;
1242
1243 inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
1244 child = lookup_one_len(name, parent, len);
1245 if (!IS_ERR(child) && !child->d_inode)
1246 child = ovl_create_real(parent->d_inode, child,
1247 OVL_CATTR(mode));
1248 inode_unlock(parent->d_inode);
1249 dput(parent);
1250
1251 return child;
1252}
1253
1254/*
1255 * Creates $workdir/work/incompat/volatile/dirty file if it is not already
1256 * present.
1257 */
1258static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
1259{
1260 unsigned int ctr;
1261 struct dentry *d = dget(ofs->workbasedir);
1262 static const char *const volatile_path[] = {
1263 OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
1264 };
1265 const char *const *name = volatile_path;
1266
1267 for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
1268 d = ovl_lookup_or_create(d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
1269 if (IS_ERR(d))
1270 return PTR_ERR(d);
1271 }
1272 dput(d);
1273 return 0;
1274}
1275
Amir Goldstein146d62e2019-04-18 17:42:08 +03001276static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1277 struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001278{
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001279 struct vfsmount *mnt = ovl_upper_mnt(ofs);
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001280 struct dentry *temp, *workdir;
Amir Goldsteind80172c2020-02-20 09:03:08 +02001281 bool rename_whiteout;
1282 bool d_type;
Amir Goldsteine487d882017-11-07 13:55:04 +02001283 int fh_type;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001284 int err;
1285
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001286 err = mnt_want_write(mnt);
1287 if (err)
1288 return err;
1289
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001290 workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1291 err = PTR_ERR(workdir);
1292 if (IS_ERR_OR_NULL(workdir))
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001293 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001294
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001295 ofs->workdir = workdir;
1296
Amir Goldstein146d62e2019-04-18 17:42:08 +03001297 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1298 if (err)
1299 goto out;
1300
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001301 /*
1302 * Upper should support d_type, else whiteouts are visible. Given
1303 * workdir and upper are on same fs, we can do iterate_dir() on
1304 * workdir. This check requires successful creation of workdir in
1305 * previous step.
1306 */
1307 err = ovl_check_d_type_supported(workpath);
1308 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001309 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001310
Amir Goldsteind80172c2020-02-20 09:03:08 +02001311 d_type = err;
1312 if (!d_type)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001313 pr_warn("upper fs needs to support d_type.\n");
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001314
1315 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +01001316 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1317 ofs->tmpfile = !IS_ERR(temp);
1318 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001319 dput(temp);
1320 else
lijiazi1bd0a3a2019-12-16 19:12:32 +08001321 pr_warn("upper fs does not support tmpfile.\n");
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001322
Amir Goldsteincad218a2020-02-20 09:00:19 +02001323
1324 /* Check if upper/work fs supports RENAME_WHITEOUT */
1325 err = ovl_check_rename_whiteout(ofs->workdir);
1326 if (err < 0)
1327 goto out;
1328
Amir Goldsteind80172c2020-02-20 09:03:08 +02001329 rename_whiteout = err;
1330 if (!rename_whiteout)
Amir Goldsteincad218a2020-02-20 09:00:19 +02001331 pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
1332
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001333 /*
1334 * Check if upper/work fs supports trusted.overlay.* xattr
1335 */
Miklos Szeredi610afc02020-09-02 10:58:49 +02001336 err = ovl_do_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001337 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +01001338 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +03001339 ofs->config.index = false;
Vivek Goyald5791042018-05-11 11:49:27 -04001340 ofs->config.metacopy = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001341 pr_warn("upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001342 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001343 } else {
Miklos Szeredi610afc02020-09-02 10:58:49 +02001344 ovl_do_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001345 }
1346
Amir Goldsteind80172c2020-02-20 09:03:08 +02001347 /*
1348 * We allowed sub-optimal upper fs configuration and don't want to break
1349 * users over kernel upgrade, but we never allowed remote upper fs, so
1350 * we can enforce strict requirements for remote upper fs.
1351 */
1352 if (ovl_dentry_remote(ofs->workdir) &&
1353 (!d_type || !rename_whiteout || ofs->noxattr)) {
1354 pr_err("upper fs missing required features.\n");
1355 err = -EINVAL;
1356 goto out;
1357 }
1358
Vivek Goyalc86243b02020-08-31 14:15:29 -04001359 /*
1360 * For volatile mount, create a incompat/volatile/dirty file to keep
1361 * track of it.
1362 */
1363 if (ofs->config.ovl_volatile) {
1364 err = ovl_create_volatile_dirty(ofs);
1365 if (err < 0) {
1366 pr_err("Failed to create volatile/dirty file.\n");
1367 goto out;
1368 }
1369 }
1370
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001371 /* Check if upper/work fs supports file handles */
Amir Goldsteine487d882017-11-07 13:55:04 +02001372 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1373 if (ofs->config.index && !fh_type) {
Miklos Szerediad204482017-11-10 09:39:16 +01001374 ofs->config.index = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001375 pr_warn("upper fs does not support file handles, falling back to index=off.\n");
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001376 }
1377
Amir Goldsteine487d882017-11-07 13:55:04 +02001378 /* Check if upper fs has 32bit inode numbers */
1379 if (fh_type != FILEID_INO32_GEN)
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001380 ofs->xino_mode = -1;
Amir Goldsteine487d882017-11-07 13:55:04 +02001381
Amir Goldsteinf168f102018-01-19 11:26:53 +02001382 /* NFS export of r/w mount depends on index */
1383 if (ofs->config.nfs_export && !ofs->config.index) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001384 pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
Amir Goldsteinf168f102018-01-19 11:26:53 +02001385 ofs->config.nfs_export = false;
1386 }
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001387out:
1388 mnt_drop_write(mnt);
1389 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001390}
1391
Amir Goldstein146d62e2019-04-18 17:42:08 +03001392static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1393 struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001394{
1395 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001396 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001397
Miklos Szerediad204482017-11-10 09:39:16 +01001398 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001399 if (err)
1400 goto out;
1401
1402 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001403 if (upperpath->mnt != workpath.mnt) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001404 pr_err("workdir and upperdir must reside under the same mount\n");
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001405 goto out;
1406 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001407 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001408 pr_err("workdir and upperdir must be separate subtrees\n");
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001409 goto out;
1410 }
1411
Miklos Szeredi8c257412018-09-10 11:43:29 +02001412 ofs->workbasedir = dget(workpath.dentry);
1413
Miklos Szeredi8c257412018-09-10 11:43:29 +02001414 if (ovl_inuse_trylock(ofs->workbasedir)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001415 ofs->workdir_locked = true;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001416 } else {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001417 err = ovl_report_in_use(ofs, "workdir");
1418 if (err)
1419 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001420 }
1421
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001422 err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
1423 "workdir");
1424 if (err)
1425 goto out;
1426
Amir Goldstein146d62e2019-04-18 17:42:08 +03001427 err = ovl_make_workdir(sb, ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001428
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001429out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001430 path_put(&workpath);
1431
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001432 return err;
1433}
1434
Amir Goldstein146d62e2019-04-18 17:42:08 +03001435static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1436 struct ovl_entry *oe, struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001437{
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001438 struct vfsmount *mnt = ovl_upper_mnt(ofs);
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001439 struct dentry *indexdir;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001440 int err;
1441
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001442 err = mnt_want_write(mnt);
1443 if (err)
1444 return err;
1445
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001446 /* Verify lower root is upper root origin */
Miklos Szeredi610afc02020-09-02 10:58:49 +02001447 err = ovl_verify_origin(ofs, upperpath->dentry,
1448 oe->lowerstack[0].dentry, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001449 if (err) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001450 pr_err("failed to verify upper root origin\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001451 goto out;
1452 }
1453
Amir Goldstein470c1562020-07-13 17:19:43 +03001454 /* index dir will act also as workdir */
1455 iput(ofs->workdir_trap);
1456 ofs->workdir_trap = NULL;
1457 dput(ofs->workdir);
1458 ofs->workdir = NULL;
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001459 indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1460 if (IS_ERR(indexdir)) {
1461 err = PTR_ERR(indexdir);
1462 } else if (indexdir) {
1463 ofs->indexdir = indexdir;
1464 ofs->workdir = dget(indexdir);
Amir Goldstein20396362020-06-21 09:37:59 +03001465
Amir Goldstein146d62e2019-04-18 17:42:08 +03001466 err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1467 "indexdir");
1468 if (err)
1469 goto out;
1470
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001471 /*
1472 * Verify upper root is exclusively associated with index dir.
1473 * Older kernels stored upper fh in "trusted.overlay.origin"
1474 * xattr. If that xattr exists, verify that it is a match to
1475 * upper dir file handle. In any case, verify or set xattr
1476 * "trusted.overlay.upper" to indicate that index may have
1477 * directory entries.
1478 */
Miklos Szeredi610afc02020-09-02 10:58:49 +02001479 if (ovl_check_origin_xattr(ofs, ofs->indexdir)) {
1480 err = ovl_verify_set_fh(ofs, ofs->indexdir,
1481 OVL_XATTR_ORIGIN,
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001482 upperpath->dentry, true, false);
1483 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001484 pr_err("failed to verify index dir 'origin' xattr\n");
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001485 }
Miklos Szeredi610afc02020-09-02 10:58:49 +02001486 err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry,
1487 true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001488 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001489 pr_err("failed to verify index dir 'upper' xattr\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001490
1491 /* Cleanup bad/stale/orphan index entries */
1492 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001493 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001494 }
Miklos Szerediad204482017-11-10 09:39:16 +01001495 if (err || !ofs->indexdir)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001496 pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001497
1498out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001499 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001500 return err;
1501}
1502
Amir Goldstein9df085f2018-09-03 09:12:09 +03001503static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
Amir Goldstein51486262018-03-28 20:22:41 +03001504{
1505 unsigned int i;
Amir Goldstein9df085f2018-09-03 09:12:09 +03001506
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001507 if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
Amir Goldstein9df085f2018-09-03 09:12:09 +03001508 return true;
1509
Amir Goldsteina888db32020-07-08 16:16:13 +03001510 /*
1511 * We allow using single lower with null uuid for index and nfs_export
1512 * for example to support those features with single lower squashfs.
1513 * To avoid regressions in setups of overlay with re-formatted lower
1514 * squashfs, do not allow decoding origin with lower null uuid unless
1515 * user opted-in to one of the new features that require following the
1516 * lower inode of non-dir upper.
1517 */
1518 if (!ofs->config.index && !ofs->config.metacopy && !ofs->config.xino &&
1519 uuid_is_null(uuid))
1520 return false;
1521
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001522 for (i = 0; i < ofs->numfs; i++) {
Amir Goldstein9df085f2018-09-03 09:12:09 +03001523 /*
1524 * We use uuid to associate an overlay lower file handle with a
1525 * lower layer, so we can accept lower fs with null uuid as long
1526 * as all lower layers with null uuid are on the same fs.
Amir Goldstein7e63c872019-11-14 22:28:41 +02001527 * if we detect multiple lower fs with the same uuid, we
1528 * disable lower file handle decoding on all of them.
Amir Goldstein9df085f2018-09-03 09:12:09 +03001529 */
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001530 if (ofs->fs[i].is_lower &&
1531 uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
Amir Goldstein07f1e592020-01-14 21:59:22 +02001532 ofs->fs[i].bad_uuid = true;
Amir Goldstein9df085f2018-09-03 09:12:09 +03001533 return false;
Amir Goldstein7e63c872019-11-14 22:28:41 +02001534 }
Amir Goldstein9df085f2018-09-03 09:12:09 +03001535 }
1536 return true;
1537}
1538
1539/* Get a unique fsid for the layer */
1540static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1541{
1542 struct super_block *sb = path->mnt->mnt_sb;
1543 unsigned int i;
Amir Goldstein51486262018-03-28 20:22:41 +03001544 dev_t dev;
1545 int err;
Amir Goldstein7e63c872019-11-14 22:28:41 +02001546 bool bad_uuid = false;
Amir Goldstein51486262018-03-28 20:22:41 +03001547
Amir Goldstein07f1e592020-01-14 21:59:22 +02001548 for (i = 0; i < ofs->numfs; i++) {
1549 if (ofs->fs[i].sb == sb)
1550 return i;
Amir Goldstein51486262018-03-28 20:22:41 +03001551 }
1552
Amir Goldstein9df085f2018-09-03 09:12:09 +03001553 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
Amir Goldstein7e63c872019-11-14 22:28:41 +02001554 bad_uuid = true;
1555 if (ofs->config.index || ofs->config.nfs_export) {
1556 ofs->config.index = false;
1557 ofs->config.nfs_export = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001558 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 +02001559 uuid_is_null(&sb->s_uuid) ? "null" :
1560 "conflicting",
1561 path->dentry);
1562 }
Amir Goldstein9df085f2018-09-03 09:12:09 +03001563 }
1564
Amir Goldstein51486262018-03-28 20:22:41 +03001565 err = get_anon_bdev(&dev);
1566 if (err) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001567 pr_err("failed to get anonymous bdev for lowerpath\n");
Amir Goldstein51486262018-03-28 20:22:41 +03001568 return err;
1569 }
1570
Amir Goldstein07f1e592020-01-14 21:59:22 +02001571 ofs->fs[ofs->numfs].sb = sb;
1572 ofs->fs[ofs->numfs].pseudo_dev = dev;
1573 ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
Amir Goldstein51486262018-03-28 20:22:41 +03001574
Amir Goldstein07f1e592020-01-14 21:59:22 +02001575 return ofs->numfs++;
Amir Goldstein51486262018-03-28 20:22:41 +03001576}
1577
Amir Goldstein94375f92019-11-15 14:12:40 +02001578static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001579 struct path *stack, unsigned int numlower,
1580 struct ovl_layer *layers)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001581{
1582 int err;
1583 unsigned int i;
1584
1585 err = -ENOMEM;
Amir Goldstein07f1e592020-01-14 21:59:22 +02001586 ofs->fs = kcalloc(numlower + 1, sizeof(struct ovl_sb), GFP_KERNEL);
1587 if (ofs->fs == NULL)
Amir Goldstein51486262018-03-28 20:22:41 +03001588 goto out;
1589
Amir Goldstein07f1e592020-01-14 21:59:22 +02001590 /* idx/fsid 0 are reserved for upper fs even with lower only overlay */
1591 ofs->numfs++;
1592
Amir Goldstein07f1e592020-01-14 21:59:22 +02001593 /*
Amir Goldsteinb7bf9902020-01-14 22:17:25 +02001594 * All lower layers that share the same fs as upper layer, use the same
1595 * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower
1596 * only overlay to simplify ovl_fs_free().
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001597 * is_lower will be set if upper fs is shared with a lower layer.
Amir Goldstein07f1e592020-01-14 21:59:22 +02001598 */
Amir Goldsteinb7bf9902020-01-14 22:17:25 +02001599 err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
1600 if (err) {
1601 pr_err("failed to get anonymous bdev for upper fs\n");
1602 goto out;
1603 }
1604
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001605 if (ovl_upper_mnt(ofs)) {
1606 ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001607 ofs->fs[0].is_lower = false;
Amir Goldstein07f1e592020-01-14 21:59:22 +02001608 }
1609
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001610 for (i = 0; i < numlower; i++) {
1611 struct vfsmount *mnt;
Amir Goldstein146d62e2019-04-18 17:42:08 +03001612 struct inode *trap;
Amir Goldstein51486262018-03-28 20:22:41 +03001613 int fsid;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001614
Amir Goldstein9df085f2018-09-03 09:12:09 +03001615 err = fsid = ovl_get_fsid(ofs, &stack[i]);
Amir Goldstein51486262018-03-28 20:22:41 +03001616 if (err < 0)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001617 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001618
youngjun24f14002020-06-16 17:30:43 +09001619 /*
1620 * Check if lower root conflicts with this overlay layers before
1621 * checking if it is in-use as upperdir/workdir of "another"
1622 * mount, because we do not bother to check in ovl_is_inuse() if
1623 * the upperdir/workdir is in fact in-use by our
1624 * upperdir/workdir.
1625 */
Amir Goldstein146d62e2019-04-18 17:42:08 +03001626 err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1627 if (err)
1628 goto out;
1629
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001630 if (ovl_is_inuse(stack[i].dentry)) {
1631 err = ovl_report_in_use(ofs, "lowerdir");
youngjun24f14002020-06-16 17:30:43 +09001632 if (err) {
1633 iput(trap);
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001634 goto out;
youngjun24f14002020-06-16 17:30:43 +09001635 }
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001636 }
1637
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001638 mnt = clone_private_mount(&stack[i]);
1639 err = PTR_ERR(mnt);
1640 if (IS_ERR(mnt)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001641 pr_err("failed to clone lowerpath\n");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001642 iput(trap);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001643 goto out;
1644 }
Amir Goldstein51486262018-03-28 20:22:41 +03001645
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001646 /*
1647 * Make lower layers R/O. That way fchmod/fchown on lower file
1648 * will fail instead of modifying lower fs.
1649 */
1650 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1651
Miklos Szeredi13464162020-01-24 09:46:45 +01001652 layers[ofs->numlayer].trap = trap;
1653 layers[ofs->numlayer].mnt = mnt;
1654 layers[ofs->numlayer].idx = ofs->numlayer;
1655 layers[ofs->numlayer].fsid = fsid;
1656 layers[ofs->numlayer].fs = &ofs->fs[fsid];
Amir Goldstein94375f92019-11-15 14:12:40 +02001657 ofs->numlayer++;
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001658 ofs->fs[fsid].is_lower = true;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001659 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001660
Amir Goldstein795939a2018-03-29 09:08:18 +03001661 /*
1662 * When all layers on same fs, overlay can use real inode numbers.
Amir Goldstein926e94d2020-02-21 16:34:45 +02001663 * With mount option "xino=<on|auto>", mounter declares that there are
1664 * enough free high bits in underlying fs to hold the unique fsid.
Amir Goldstein795939a2018-03-29 09:08:18 +03001665 * If overlayfs does encounter underlying inodes using the high xino
1666 * bits reserved for fsid, it emits a warning and uses the original
Amir Goldsteindfe51d42020-02-21 16:34:44 +02001667 * inode number or a non persistent inode number allocated from a
1668 * dedicated range.
Amir Goldstein795939a2018-03-29 09:08:18 +03001669 */
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001670 if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001671 if (ofs->config.xino == OVL_XINO_ON)
1672 pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
1673 ofs->xino_mode = 0;
Amir Goldstein53afcd32020-02-21 16:34:42 +02001674 } else if (ofs->config.xino == OVL_XINO_OFF) {
1675 ofs->xino_mode = -1;
Amir Goldstein926e94d2020-02-21 16:34:45 +02001676 } else if (ofs->xino_mode < 0) {
Amir Goldstein795939a2018-03-29 09:08:18 +03001677 /*
Amir Goldstein07f1e592020-01-14 21:59:22 +02001678 * This is a roundup of number of bits needed for encoding
Amir Goldsteindfe51d42020-02-21 16:34:44 +02001679 * fsid, where fsid 0 is reserved for upper fs (even with
1680 * lower only overlay) +1 extra bit is reserved for the non
1681 * persistent inode number range that is used for resolving
1682 * xino lower bits overflow.
Amir Goldstein795939a2018-03-29 09:08:18 +03001683 */
Amir Goldsteindfe51d42020-02-21 16:34:44 +02001684 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
1685 ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
Amir Goldstein795939a2018-03-29 09:08:18 +03001686 }
1687
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001688 if (ofs->xino_mode > 0) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001689 pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001690 ofs->xino_mode);
Amir Goldstein795939a2018-03-29 09:08:18 +03001691 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001692
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001693 err = 0;
1694out:
1695 return err;
1696}
1697
Miklos Szeredi4155c102017-11-10 09:39:15 +01001698static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001699 const char *lower, unsigned int numlower,
1700 struct ovl_fs *ofs, struct ovl_layer *layers)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001701{
1702 int err;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001703 struct path *stack = NULL;
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001704 unsigned int i;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001705 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001706
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001707 if (!ofs->config.upperdir && numlower == 1) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001708 pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001709 return ERR_PTR(-EINVAL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001710 }
1711
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001712 stack = kcalloc(numlower, sizeof(struct path), GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001713 if (!stack)
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001714 return ERR_PTR(-ENOMEM);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001715
1716 err = -EINVAL;
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001717 for (i = 0; i < numlower; i++) {
1718 err = ovl_lower_dir(lower, &stack[i], ofs, &sb->s_stack_depth);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001719 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001720 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001721
1722 lower = strchr(lower, '\0') + 1;
1723 }
1724
1725 err = -EINVAL;
1726 sb->s_stack_depth++;
1727 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001728 pr_err("maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001729 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001730 }
1731
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001732 err = ovl_get_layers(sb, ofs, stack, numlower, layers);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001733 if (err)
1734 goto out_err;
1735
1736 err = -ENOMEM;
1737 oe = ovl_alloc_entry(numlower);
1738 if (!oe)
1739 goto out_err;
1740
1741 for (i = 0; i < numlower; i++) {
1742 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Amir Goldstein94375f92019-11-15 14:12:40 +02001743 oe->lowerstack[i].layer = &ofs->layers[i+1];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001744 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001745
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001746out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001747 for (i = 0; i < numlower; i++)
1748 path_put(&stack[i]);
1749 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001750
1751 return oe;
1752
1753out_err:
1754 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001755 goto out;
1756}
1757
Amir Goldstein146d62e2019-04-18 17:42:08 +03001758/*
1759 * Check if this layer root is a descendant of:
1760 * - another layer of this overlayfs instance
1761 * - upper/work dir of any overlayfs instance
Amir Goldstein146d62e2019-04-18 17:42:08 +03001762 */
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001763static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
1764 struct dentry *dentry, const char *name)
Amir Goldstein146d62e2019-04-18 17:42:08 +03001765{
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001766 struct dentry *next = dentry, *parent;
Amir Goldstein146d62e2019-04-18 17:42:08 +03001767 int err = 0;
1768
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001769 if (!dentry)
Amir Goldstein146d62e2019-04-18 17:42:08 +03001770 return 0;
1771
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001772 parent = dget_parent(next);
1773
1774 /* Walk back ancestors to root (inclusive) looking for traps */
1775 while (!err && parent != next) {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001776 if (ovl_lookup_trap_inode(sb, parent)) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001777 err = -ELOOP;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001778 pr_err("overlapping %s path\n", name);
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001779 } else if (ovl_is_inuse(parent)) {
1780 err = ovl_report_in_use(ofs, name);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001781 }
Amir Goldstein146d62e2019-04-18 17:42:08 +03001782 next = parent;
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001783 parent = dget_parent(next);
1784 dput(next);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001785 }
1786
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001787 dput(parent);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001788
1789 return err;
1790}
1791
1792/*
1793 * Check if any of the layers or work dirs overlap.
1794 */
1795static int ovl_check_overlapping_layers(struct super_block *sb,
1796 struct ovl_fs *ofs)
1797{
1798 int i, err;
1799
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001800 if (ovl_upper_mnt(ofs)) {
1801 err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001802 "upperdir");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001803 if (err)
1804 return err;
1805
1806 /*
1807 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1808 * this instance and covers overlapping work and index dirs,
1809 * unless work or index dir have been moved since created inside
1810 * workbasedir. In that case, we already have their traps in
1811 * inode cache and we will catch that case on lookup.
1812 */
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001813 err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001814 if (err)
1815 return err;
1816 }
1817
Amir Goldstein94375f92019-11-15 14:12:40 +02001818 for (i = 1; i < ofs->numlayer; i++) {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001819 err = ovl_check_layer(sb, ofs,
Amir Goldstein94375f92019-11-15 14:12:40 +02001820 ofs->layers[i].mnt->mnt_root,
Amir Goldstein146d62e2019-04-18 17:42:08 +03001821 "lowerdir");
1822 if (err)
1823 return err;
1824 }
1825
1826 return 0;
1827}
1828
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001829static struct dentry *ovl_get_root(struct super_block *sb,
1830 struct dentry *upperdentry,
1831 struct ovl_entry *oe)
1832{
1833 struct dentry *root;
Amir Goldstein62c832e2019-11-19 15:31:46 +02001834 struct ovl_path *lowerpath = &oe->lowerstack[0];
1835 unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
1836 int fsid = lowerpath->layer->fsid;
1837 struct ovl_inode_params oip = {
1838 .upperdentry = upperdentry,
1839 .lowerpath = lowerpath,
1840 };
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001841
1842 root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1843 if (!root)
1844 return NULL;
1845
1846 root->d_fsdata = oe;
1847
1848 if (upperdentry) {
Amir Goldstein62c832e2019-11-19 15:31:46 +02001849 /* Root inode uses upper st_ino/i_ino */
1850 ino = d_inode(upperdentry)->i_ino;
1851 fsid = 0;
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001852 ovl_dentry_set_upper_alias(root);
Miklos Szeredi610afc02020-09-02 10:58:49 +02001853 if (ovl_is_impuredir(sb, upperdentry))
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001854 ovl_set_flag(OVL_IMPURE, d_inode(root));
1855 }
1856
1857 /* Root is always merge -> can have whiteouts */
1858 ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
1859 ovl_dentry_set_flag(OVL_E_CONNECTED, root);
1860 ovl_set_upperdata(d_inode(root));
Amir Goldstein62c832e2019-11-19 15:31:46 +02001861 ovl_inode_init(d_inode(root), &oip, ino, fsid);
Miklos Szeredif4288842020-03-17 15:04:22 +01001862 ovl_dentry_update_reval(root, upperdentry, DCACHE_OP_WEAK_REVALIDATE);
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001863
1864 return root;
1865}
1866
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001867static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1868{
Kees Cook33006cd2017-03-29 14:02:19 -07001869 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001870 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001871 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001872 struct ovl_fs *ofs;
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001873 struct ovl_layer *layers;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001874 struct cred *cred;
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001875 char *splitlower = NULL;
1876 unsigned int numlower;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001877 int err;
1878
Miklos Szeredif4288842020-03-17 15:04:22 +01001879 sb->s_d_op = &ovl_dentry_operations;
1880
Erez Zadokf45827e82014-10-24 00:14:38 +02001881 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001882 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1883 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001884 goto out;
1885
Miklos Szerediad204482017-11-10 09:39:16 +01001886 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001887 if (!cred)
1888 goto out_err;
1889
Chengguang Xuc21c8392020-04-24 10:55:17 +08001890 /* Is there a reason anyone would want not to share whiteouts? */
1891 ofs->share_whiteout = true;
1892
Miklos Szerediad204482017-11-10 09:39:16 +01001893 ofs->config.index = ovl_index_def;
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +03001894 ofs->config.uuid = true;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001895 ofs->config.nfs_export = ovl_nfs_export_def;
Amir Goldstein795939a2018-03-29 09:08:18 +03001896 ofs->config.xino = ovl_xino_def();
Vivek Goyald5791042018-05-11 11:49:27 -04001897 ofs->config.metacopy = ovl_metacopy_def;
Miklos Szerediad204482017-11-10 09:39:16 +01001898 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001899 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001900 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001901
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001902 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001903 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001904 if (!silent)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001905 pr_err("missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001906 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001907 }
1908
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001909 err = -ENOMEM;
1910 splitlower = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1911 if (!splitlower)
1912 goto out_err;
1913
1914 numlower = ovl_split_lowerdirs(splitlower);
1915 if (numlower > OVL_MAX_STACK) {
1916 pr_err("too many lower directories, limit is %d\n",
1917 OVL_MAX_STACK);
1918 goto out_err;
1919 }
1920
1921 layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL);
1922 if (!layers)
1923 goto out_err;
1924
1925 ofs->layers = layers;
1926 /* Layer 0 is reserved for upper even if there's no upper */
1927 ofs->numlayer = 1;
1928
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001929 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001930 sb->s_maxbytes = MAX_LFS_FILESIZE;
Amir Goldstein4d314f72020-02-21 16:34:43 +02001931 atomic_long_set(&ofs->last_ino, 1);
Amir Goldsteine487d882017-11-07 13:55:04 +02001932 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
Amir Goldstein53afcd32020-02-21 16:34:42 +02001933 if (ofs->config.xino != OVL_XINO_OFF) {
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001934 ofs->xino_mode = BITS_PER_LONG - 32;
Amir Goldstein53afcd32020-02-21 16:34:42 +02001935 if (!ofs->xino_mode) {
1936 pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
1937 ofs->config.xino = OVL_XINO_OFF;
1938 }
1939 }
Amir Goldstein795939a2018-03-29 09:08:18 +03001940
Amir Goldstein146d62e2019-04-18 17:42:08 +03001941 /* alloc/destroy_inode needed for setting up traps in inode cache */
1942 sb->s_op = &ovl_super_operations;
1943
Miklos Szerediad204482017-11-10 09:39:16 +01001944 if (ofs->config.upperdir) {
1945 if (!ofs->config.workdir) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001946 pr_err("missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001947 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001948 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001949
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001950 err = ovl_get_upper(sb, ofs, &layers[0], &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001951 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001952 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001953
Amir Goldstein146d62e2019-04-18 17:42:08 +03001954 err = ovl_get_workdir(sb, ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001955 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001956 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001957
Miklos Szerediad204482017-11-10 09:39:16 +01001958 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001959 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001960
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001961 sb->s_stack_depth = ovl_upper_mnt(ofs)->mnt_sb->s_stack_depth;
1962 sb->s_time_gran = ovl_upper_mnt(ofs)->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001963
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001964 }
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001965 oe = ovl_get_lowerstack(sb, splitlower, numlower, ofs, layers);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001966 err = PTR_ERR(oe);
1967 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001968 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001969
hujianyang71cbad72015-01-15 13:20:57 +08001970 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001971 if (!ovl_upper_mnt(ofs))
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001972 sb->s_flags |= SB_RDONLY;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001973
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +03001974 if (!ofs->config.uuid && ofs->numfs > 1) {
1975 pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=on.\n");
1976 ofs->config.uuid = true;
1977 }
1978
Amir Goldstein470c1562020-07-13 17:19:43 +03001979 if (!ovl_force_readonly(ofs) && ofs->config.index) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001980 err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001981 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001982 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001983
Amir Goldstein972d0092017-09-19 12:14:18 +03001984 /* Force r/o mount with no index dir */
Amir Goldstein20396362020-06-21 09:37:59 +03001985 if (!ofs->indexdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001986 sb->s_flags |= SB_RDONLY;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001987 }
1988
Amir Goldstein146d62e2019-04-18 17:42:08 +03001989 err = ovl_check_overlapping_layers(sb, ofs);
1990 if (err)
1991 goto out_free_oe;
1992
Amir Goldstein972d0092017-09-19 12:14:18 +03001993 /* Show index=off in /proc/mounts for forced r/o mount */
Amir Goldsteinf168f102018-01-19 11:26:53 +02001994 if (!ofs->indexdir) {
Miklos Szerediad204482017-11-10 09:39:16 +01001995 ofs->config.index = false;
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001996 if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001997 pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
Amir Goldsteinf168f102018-01-19 11:26:53 +02001998 ofs->config.nfs_export = false;
1999 }
2000 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03002001
Vivek Goyald5791042018-05-11 11:49:27 -04002002 if (ofs->config.metacopy && ofs->config.nfs_export) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08002003 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 -04002004 ofs->config.nfs_export = false;
2005 }
2006
Amir Goldstein8383f172017-10-02 11:31:42 +03002007 if (ofs->config.nfs_export)
2008 sb->s_export_op = &ovl_export_operations;
2009
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03002010 /* Never override disk quota limits or use reserved space */
2011 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
2012
Vivek Goyal655042c2016-10-14 03:03:36 +02002013 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Vivek Goyal655042c2016-10-14 03:03:36 +02002014 sb->s_xattr = ovl_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01002015 sb->s_fs_info = ofs;
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002016 sb->s_flags |= SB_POSIXACL;
Konstantin Khlebnikov32b1924b22020-04-09 11:29:47 +03002017 sb->s_iflags |= SB_I_SKIP_SYNC;
Vivek Goyal655042c2016-10-14 03:03:36 +02002018
Miklos Szeredic6fe6252017-11-10 09:39:15 +01002019 err = -ENOMEM;
Amir Goldstein2effc5c2019-11-19 17:49:17 +02002020 root_dentry = ovl_get_root(sb, upperpath.dentry, oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002021 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01002022 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002023
2024 mntput(upperpath.mnt);
Miklos Szeredib8e42a62020-06-04 10:48:19 +02002025 kfree(splitlower);
Miklos Szeredied06e062015-12-09 16:11:59 +01002026
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002027 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002028
2029 return 0;
2030
Miklos Szeredi4155c102017-11-10 09:39:15 +01002031out_free_oe:
2032 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05002033 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01002034out_err:
Miklos Szeredib8e42a62020-06-04 10:48:19 +02002035 kfree(splitlower);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002036 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01002037 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002038out:
2039 return err;
2040}
2041
2042static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
2043 const char *dev_name, void *raw_data)
2044{
2045 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
2046}
2047
2048static struct file_system_type ovl_fs_type = {
2049 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01002050 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002051 .mount = ovl_mount,
2052 .kill_sb = kill_anon_super,
2053};
Miklos Szeredief94b182014-11-20 16:39:59 +01002054MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002055
Amir Goldstein13cf1992017-06-12 09:54:40 +03002056static void ovl_inode_init_once(void *foo)
2057{
2058 struct ovl_inode *oi = foo;
2059
2060 inode_init_once(&oi->vfs_inode);
2061}
2062
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002063static int __init ovl_init(void)
2064{
Amir Goldstein13cf1992017-06-12 09:54:40 +03002065 int err;
2066
2067 ovl_inode_cachep = kmem_cache_create("ovl_inode",
2068 sizeof(struct ovl_inode), 0,
2069 (SLAB_RECLAIM_ACCOUNT|
2070 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2071 ovl_inode_init_once);
2072 if (ovl_inode_cachep == NULL)
2073 return -ENOMEM;
2074
Jiufei Xue2406a302019-11-20 17:45:26 +08002075 err = ovl_aio_request_cache_init();
2076 if (!err) {
2077 err = register_filesystem(&ovl_fs_type);
2078 if (!err)
2079 return 0;
2080
2081 ovl_aio_request_cache_destroy();
2082 }
2083 kmem_cache_destroy(ovl_inode_cachep);
Amir Goldstein13cf1992017-06-12 09:54:40 +03002084
2085 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002086}
2087
2088static void __exit ovl_exit(void)
2089{
2090 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03002091
2092 /*
2093 * Make sure all delayed rcu free inodes are flushed before we
2094 * destroy cache.
2095 */
2096 rcu_barrier();
2097 kmem_cache_destroy(ovl_inode_cachep);
Jiufei Xue2406a302019-11-20 17:45:26 +08002098 ovl_aio_request_cache_destroy();
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002099}
2100
2101module_init(ovl_init);
2102module_exit(ovl_exit);