blob: 6366adf81fa316104e8810fcae00ed3160dd4656 [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{
Miklos Szeredicef4cbf2020-11-12 11:31:55 +010082 struct dentry *real = NULL, *lower;
Miklos Szeredid101a122016-03-26 16:14:37 -040083
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
Miklos Szeredicef4cbf2020-11-12 11:31:55 +0100101 lower = ovl_dentry_lowerdata(dentry);
102 if (!lower)
Miklos Szeredid101a122016-03-26 16:14:37 -0400103 goto bug;
Miklos Szeredicef4cbf2020-11-12 11:31:55 +0100104 real = lower;
Miklos Szeredid101a122016-03-26 16:14:37 -0400105
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100106 /* Handle recursion */
Miklos Szeredifb160432018-07-18 15:44:44 +0200107 real = d_real(real, inode);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100108
Miklos Szeredid101a122016-03-26 16:14:37 -0400109 if (!inode || inode == d_inode(real))
110 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400111bug:
Miklos Szeredicef4cbf2020-11-12 11:31:55 +0100112 WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n",
113 __func__, dentry, inode ? inode->i_sb->s_id : "NULL",
114 inode ? inode->i_ino : 0, real,
115 real && d_inode(real) ? d_inode(real)->i_ino : 0);
Miklos Szeredid101a122016-03-26 16:14:37 -0400116 return dentry;
117}
118
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100119static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
120{
121 int ret = 1;
122
123 if (weak) {
124 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
125 ret = d->d_op->d_weak_revalidate(d, flags);
126 } else if (d->d_flags & DCACHE_OP_REVALIDATE) {
127 ret = d->d_op->d_revalidate(d, flags);
128 if (!ret) {
129 if (!(flags & LOOKUP_RCU))
130 d_invalidate(d);
131 ret = -ESTALE;
132 }
133 }
134 return ret;
135}
136
137static int ovl_dentry_revalidate_common(struct dentry *dentry,
138 unsigned int flags, bool weak)
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200139{
140 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredibccece12020-03-17 15:04:22 +0100141 struct dentry *upper;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200142 unsigned int i;
143 int ret = 1;
144
Miklos Szeredibccece12020-03-17 15:04:22 +0100145 upper = ovl_dentry_upper(dentry);
146 if (upper)
147 ret = ovl_revalidate_real(upper, flags, weak);
148
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100149 for (i = 0; ret > 0 && i < oe->numlower; i++) {
150 ret = ovl_revalidate_real(oe->lowerstack[i].dentry, flags,
151 weak);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200152 }
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100153 return ret;
154}
155
156static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
157{
158 return ovl_dentry_revalidate_common(dentry, flags, false);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200159}
160
161static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
162{
Miklos Szeredi3bb7df92020-03-17 15:04:22 +0100163 return ovl_dentry_revalidate_common(dentry, flags, true);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200164}
165
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200166static const struct dentry_operations ovl_dentry_operations = {
167 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400168 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200169 .d_revalidate = ovl_dentry_revalidate,
170 .d_weak_revalidate = ovl_dentry_weak_revalidate,
171};
172
Amir Goldstein13cf1992017-06-12 09:54:40 +0300173static struct kmem_cache *ovl_inode_cachep;
174
175static struct inode *ovl_alloc_inode(struct super_block *sb)
176{
177 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
178
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900179 if (!oi)
180 return NULL;
181
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200182 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200183 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200184 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200185 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200186 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200187 oi->lower = NULL;
Vivek Goyal2664bd02018-05-11 11:49:30 -0400188 oi->lowerdata = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300189 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200190
Amir Goldstein13cf1992017-06-12 09:54:40 +0300191 return &oi->vfs_inode;
192}
193
Al Viro0b269de2019-04-15 22:52:17 -0400194static void ovl_free_inode(struct inode *inode)
Amir Goldstein13cf1992017-06-12 09:54:40 +0300195{
Al Viro0b269de2019-04-15 22:52:17 -0400196 struct ovl_inode *oi = OVL_I(inode);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300197
Al Viro0b269de2019-04-15 22:52:17 -0400198 kfree(oi->redirect);
199 mutex_destroy(&oi->lock);
200 kmem_cache_free(ovl_inode_cachep, oi);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300201}
202
203static void ovl_destroy_inode(struct inode *inode)
204{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200205 struct ovl_inode *oi = OVL_I(inode);
206
207 dput(oi->__upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200208 iput(oi->lower);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400209 if (S_ISDIR(inode->i_mode))
210 ovl_dir_cache_free(inode);
211 else
212 iput(oi->lowerdata);
Amir Goldstein13cf1992017-06-12 09:54:40 +0300213}
214
Miklos Szerediad204482017-11-10 09:39:16 +0100215static void ovl_free_fs(struct ovl_fs *ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200216{
Miklos Szeredidf820f82020-06-04 10:48:19 +0200217 struct vfsmount **mounts;
Miklos Szeredidd662662014-12-13 00:59:43 +0100218 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200219
Amir Goldstein0be0bfd2019-07-12 15:24:34 +0300220 iput(ofs->workbasedir_trap);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300221 iput(ofs->indexdir_trap);
222 iput(ofs->workdir_trap);
Chengguang Xuc21c8392020-04-24 10:55:17 +0800223 dput(ofs->whiteout);
Miklos Szerediad204482017-11-10 09:39:16 +0100224 dput(ofs->indexdir);
225 dput(ofs->workdir);
226 if (ofs->workdir_locked)
227 ovl_inuse_unlock(ofs->workbasedir);
228 dput(ofs->workbasedir);
229 if (ofs->upperdir_locked)
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200230 ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root);
Miklos Szeredidf820f82020-06-04 10:48:19 +0200231
232 /* Hack! Reuse ofs->layers as a vfsmount array before freeing it */
233 mounts = (struct vfsmount **) ofs->layers;
Miklos Szeredib8e42a62020-06-04 10:48:19 +0200234 for (i = 0; i < ofs->numlayer; i++) {
Amir Goldstein94375f92019-11-15 14:12:40 +0200235 iput(ofs->layers[i].trap);
Miklos Szeredidf820f82020-06-04 10:48:19 +0200236 mounts[i] = ofs->layers[i].mnt;
Amir Goldstein146d62e2019-04-18 17:42:08 +0300237 }
Miklos Szeredidf820f82020-06-04 10:48:19 +0200238 kern_unmount_array(mounts, ofs->numlayer);
Amir Goldstein94375f92019-11-15 14:12:40 +0200239 kfree(ofs->layers);
Amir Goldsteinb7bf9902020-01-14 22:17:25 +0200240 for (i = 0; i < ofs->numfs; i++)
Amir Goldstein07f1e592020-01-14 21:59:22 +0200241 free_anon_bdev(ofs->fs[i].pseudo_dev);
242 kfree(ofs->fs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200243
Miklos Szerediad204482017-11-10 09:39:16 +0100244 kfree(ofs->config.lowerdir);
245 kfree(ofs->config.upperdir);
246 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100247 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100248 if (ofs->creator_cred)
249 put_cred(ofs->creator_cred);
250 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200251}
252
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100253static void ovl_put_super(struct super_block *sb)
254{
255 struct ovl_fs *ofs = sb->s_fs_info;
256
257 ovl_free_fs(ofs);
258}
259
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800260/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200261static int ovl_sync_fs(struct super_block *sb, int wait)
262{
Miklos Szerediad204482017-11-10 09:39:16 +0100263 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200264 struct super_block *upper_sb;
265 int ret;
266
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200267 if (!ovl_upper_mnt(ofs))
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200268 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800269
Vivek Goyalc86243b02020-08-31 14:15:29 -0400270 if (!ovl_should_sync(ofs))
271 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800272 /*
Konstantin Khlebnikov32b1924b22020-04-09 11:29:47 +0300273 * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
274 * All the super blocks will be iterated, including upper_sb.
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800275 *
276 * If this is a syncfs(2) call, then we do need to call
277 * sync_filesystem() on upper_sb, but enough if we do it when being
278 * called with wait == 1.
279 */
280 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200281 return 0;
282
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200283 upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800284
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200285 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800286 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200287 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800288
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200289 return ret;
290}
291
Andy Whitcroftcc259632014-10-24 00:14:38 +0200292/**
293 * ovl_statfs
294 * @sb: The overlayfs super block
295 * @buf: The struct kstatfs to fill in with stats
296 *
297 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100298 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200299 */
300static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
301{
302 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
303 struct dentry *root_dentry = dentry->d_sb->s_root;
304 struct path path;
305 int err;
306
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100307 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200308
309 err = vfs_statfs(&path, buf);
310 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100311 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200312 buf->f_type = OVERLAYFS_SUPER_MAGIC;
313 }
314
315 return err;
316}
317
Amir Goldstein02bcd152017-06-21 15:28:36 +0300318/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100319static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300320{
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200321 return (!ovl_upper_mnt(ofs) || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300322}
323
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100324static const char *ovl_redirect_mode_def(void)
325{
326 return ovl_redirect_dir_def ? "on" : "off";
327}
328
Amir Goldstein795939a2018-03-29 09:08:18 +0300329static const char * const ovl_xino_str[] = {
330 "off",
331 "auto",
332 "on",
333};
334
335static inline int ovl_xino_def(void)
336{
337 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
338}
339
Erez Zadokf45827e82014-10-24 00:14:38 +0200340/**
341 * ovl_show_options
342 *
343 * Prints the mount options for a given superblock.
344 * Returns zero; does not fail.
345 */
346static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
347{
348 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100349 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200350
Miklos Szerediad204482017-11-10 09:39:16 +0100351 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
352 if (ofs->config.upperdir) {
353 seq_show_option(m, "upperdir", ofs->config.upperdir);
354 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100355 }
Miklos Szerediad204482017-11-10 09:39:16 +0100356 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200357 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100358 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
359 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100360 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100361 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300362 if (!ofs->config.uuid)
363 seq_puts(m, ",uuid=off");
Amir Goldsteinf168f102018-01-19 11:26:53 +0200364 if (ofs->config.nfs_export != ovl_nfs_export_def)
365 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
366 "on" : "off");
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200367 if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(sb))
Amir Goldstein795939a2018-03-29 09:08:18 +0300368 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
Vivek Goyald5791042018-05-11 11:49:27 -0400369 if (ofs->config.metacopy != ovl_metacopy_def)
370 seq_printf(m, ",metacopy=%s",
371 ofs->config.metacopy ? "on" : "off");
Vivek Goyalc86243b02020-08-31 14:15:29 -0400372 if (ofs->config.ovl_volatile)
373 seq_puts(m, ",volatile");
Erez Zadokf45827e82014-10-24 00:14:38 +0200374 return 0;
375}
376
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900377static int ovl_remount(struct super_block *sb, int *flags, char *data)
378{
Miklos Szerediad204482017-11-10 09:39:16 +0100379 struct ovl_fs *ofs = sb->s_fs_info;
Chengguang Xu399c1092020-04-22 12:28:43 +0800380 struct super_block *upper_sb;
381 int ret = 0;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900382
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800383 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900384 return -EROFS;
385
Chengguang Xu399c1092020-04-22 12:28:43 +0800386 if (*flags & SB_RDONLY && !sb_rdonly(sb)) {
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200387 upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
Vivek Goyalc86243b02020-08-31 14:15:29 -0400388 if (ovl_should_sync(ofs)) {
389 down_read(&upper_sb->s_umount);
390 ret = sync_filesystem(upper_sb);
391 up_read(&upper_sb->s_umount);
392 }
Chengguang Xu399c1092020-04-22 12:28:43 +0800393 }
394
395 return ret;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900396}
397
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200398static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300399 .alloc_inode = ovl_alloc_inode,
Al Viro0b269de2019-04-15 22:52:17 -0400400 .free_inode = ovl_free_inode,
Amir Goldstein13cf1992017-06-12 09:54:40 +0300401 .destroy_inode = ovl_destroy_inode,
402 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200403 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200404 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200405 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200406 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900407 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408};
409
410enum {
411 OPT_LOWERDIR,
412 OPT_UPPERDIR,
413 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200414 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100415 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300416 OPT_INDEX_ON,
417 OPT_INDEX_OFF,
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300418 OPT_UUID_ON,
419 OPT_UUID_OFF,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200420 OPT_NFS_EXPORT_ON,
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +0100421 OPT_USERXATTR,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200422 OPT_NFS_EXPORT_OFF,
Amir Goldstein795939a2018-03-29 09:08:18 +0300423 OPT_XINO_ON,
424 OPT_XINO_OFF,
425 OPT_XINO_AUTO,
Vivek Goyald5791042018-05-11 11:49:27 -0400426 OPT_METACOPY_ON,
427 OPT_METACOPY_OFF,
Vivek Goyalc86243b02020-08-31 14:15:29 -0400428 OPT_VOLATILE,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200429 OPT_ERR,
430};
431
432static const match_table_t ovl_tokens = {
433 {OPT_LOWERDIR, "lowerdir=%s"},
434 {OPT_UPPERDIR, "upperdir=%s"},
435 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200436 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100437 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300438 {OPT_INDEX_ON, "index=on"},
439 {OPT_INDEX_OFF, "index=off"},
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +0100440 {OPT_USERXATTR, "userxattr"},
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300441 {OPT_UUID_ON, "uuid=on"},
442 {OPT_UUID_OFF, "uuid=off"},
Amir Goldsteinf168f102018-01-19 11:26:53 +0200443 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
444 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
Amir Goldstein795939a2018-03-29 09:08:18 +0300445 {OPT_XINO_ON, "xino=on"},
446 {OPT_XINO_OFF, "xino=off"},
447 {OPT_XINO_AUTO, "xino=auto"},
Vivek Goyald5791042018-05-11 11:49:27 -0400448 {OPT_METACOPY_ON, "metacopy=on"},
449 {OPT_METACOPY_OFF, "metacopy=off"},
Vivek Goyalc86243b02020-08-31 14:15:29 -0400450 {OPT_VOLATILE, "volatile"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200451 {OPT_ERR, NULL}
452};
453
Miklos Szeredi91c77942014-11-20 16:40:00 +0100454static char *ovl_next_opt(char **s)
455{
456 char *sbegin = *s;
457 char *p;
458
459 if (sbegin == NULL)
460 return NULL;
461
462 for (p = sbegin; *p; p++) {
463 if (*p == '\\') {
464 p++;
465 if (!*p)
466 break;
467 } else if (*p == ',') {
468 *p = '\0';
469 *s = p + 1;
470 return sbegin;
471 }
472 }
473 *s = NULL;
474 return sbegin;
475}
476
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100477static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
478{
479 if (strcmp(mode, "on") == 0) {
480 config->redirect_dir = true;
481 /*
482 * Does not make sense to have redirect creation without
483 * redirect following.
484 */
485 config->redirect_follow = true;
486 } else if (strcmp(mode, "follow") == 0) {
487 config->redirect_follow = true;
488 } else if (strcmp(mode, "off") == 0) {
489 if (ovl_redirect_always_follow)
490 config->redirect_follow = true;
491 } else if (strcmp(mode, "nofollow") != 0) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800492 pr_err("bad mount option \"redirect_dir=%s\"\n",
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100493 mode);
494 return -EINVAL;
495 }
496
497 return 0;
498}
499
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200500static int ovl_parse_opt(char *opt, struct ovl_config *config)
501{
502 char *p;
Vivek Goyald5791042018-05-11 11:49:27 -0400503 int err;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100504 bool metacopy_opt = false, redirect_opt = false;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300505 bool nfs_export_opt = false, index_opt = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200506
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100507 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
508 if (!config->redirect_mode)
509 return -ENOMEM;
510
Miklos Szeredi91c77942014-11-20 16:40:00 +0100511 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200512 int token;
513 substring_t args[MAX_OPT_ARGS];
514
515 if (!*p)
516 continue;
517
518 token = match_token(p, ovl_tokens, args);
519 switch (token) {
520 case OPT_UPPERDIR:
521 kfree(config->upperdir);
522 config->upperdir = match_strdup(&args[0]);
523 if (!config->upperdir)
524 return -ENOMEM;
525 break;
526
527 case OPT_LOWERDIR:
528 kfree(config->lowerdir);
529 config->lowerdir = match_strdup(&args[0]);
530 if (!config->lowerdir)
531 return -ENOMEM;
532 break;
533
534 case OPT_WORKDIR:
535 kfree(config->workdir);
536 config->workdir = match_strdup(&args[0]);
537 if (!config->workdir)
538 return -ENOMEM;
539 break;
540
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200541 case OPT_DEFAULT_PERMISSIONS:
542 config->default_permissions = true;
543 break;
544
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100545 case OPT_REDIRECT_DIR:
546 kfree(config->redirect_mode);
547 config->redirect_mode = match_strdup(&args[0]);
548 if (!config->redirect_mode)
549 return -ENOMEM;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100550 redirect_opt = true;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100551 break;
552
Amir Goldstein02bcd152017-06-21 15:28:36 +0300553 case OPT_INDEX_ON:
554 config->index = true;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300555 index_opt = true;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300556 break;
557
558 case OPT_INDEX_OFF:
559 config->index = false;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300560 index_opt = true;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300561 break;
562
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300563 case OPT_UUID_ON:
564 config->uuid = true;
565 break;
566
567 case OPT_UUID_OFF:
568 config->uuid = false;
569 break;
570
Amir Goldsteinf168f102018-01-19 11:26:53 +0200571 case OPT_NFS_EXPORT_ON:
572 config->nfs_export = true;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300573 nfs_export_opt = true;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200574 break;
575
576 case OPT_NFS_EXPORT_OFF:
577 config->nfs_export = false;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300578 nfs_export_opt = true;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200579 break;
580
Amir Goldstein795939a2018-03-29 09:08:18 +0300581 case OPT_XINO_ON:
582 config->xino = OVL_XINO_ON;
583 break;
584
585 case OPT_XINO_OFF:
586 config->xino = OVL_XINO_OFF;
587 break;
588
589 case OPT_XINO_AUTO:
590 config->xino = OVL_XINO_AUTO;
591 break;
592
Vivek Goyald5791042018-05-11 11:49:27 -0400593 case OPT_METACOPY_ON:
594 config->metacopy = true;
Miklos Szeredid47748e2018-11-01 21:31:39 +0100595 metacopy_opt = true;
Vivek Goyald5791042018-05-11 11:49:27 -0400596 break;
597
598 case OPT_METACOPY_OFF:
599 config->metacopy = false;
Amir Goldsteinb0def882020-04-09 18:58:34 +0300600 metacopy_opt = true;
Vivek Goyald5791042018-05-11 11:49:27 -0400601 break;
602
Vivek Goyalc86243b02020-08-31 14:15:29 -0400603 case OPT_VOLATILE:
604 config->ovl_volatile = true;
605 break;
606
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +0100607 case OPT_USERXATTR:
608 config->userxattr = true;
609 break;
610
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200611 default:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800612 pr_err("unrecognized mount option \"%s\" or missing value\n",
613 p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200614 return -EINVAL;
615 }
616 }
hujianyang71cbad72015-01-15 13:20:57 +0800617
Amir Goldsteinf0e1266e2020-07-13 17:19:44 +0300618 /* Workdir/index are useless in non-upper mount */
619 if (!config->upperdir) {
620 if (config->workdir) {
621 pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
622 config->workdir);
623 kfree(config->workdir);
624 config->workdir = NULL;
625 }
626 if (config->index && index_opt) {
627 pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
628 index_opt = false;
629 }
630 config->index = false;
hujianyang71cbad72015-01-15 13:20:57 +0800631 }
632
Vivek Goyalc86243b02020-08-31 14:15:29 -0400633 if (!config->upperdir && config->ovl_volatile) {
634 pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
635 config->ovl_volatile = false;
636 }
637
Vivek Goyald5791042018-05-11 11:49:27 -0400638 err = ovl_parse_redirect_mode(config, config->redirect_mode);
639 if (err)
640 return err;
641
Miklos Szeredid47748e2018-11-01 21:31:39 +0100642 /*
643 * This is to make the logic below simpler. It doesn't make any other
644 * difference, since config->redirect_dir is only used for upper.
645 */
646 if (!config->upperdir && config->redirect_follow)
647 config->redirect_dir = true;
648
649 /* Resolve metacopy -> redirect_dir dependency */
650 if (config->metacopy && !config->redirect_dir) {
651 if (metacopy_opt && redirect_opt) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800652 pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
Miklos Szeredid47748e2018-11-01 21:31:39 +0100653 config->redirect_mode);
654 return -EINVAL;
655 }
656 if (redirect_opt) {
657 /*
658 * There was an explicit redirect_dir=... that resulted
659 * in this conflict.
660 */
lijiazi1bd0a3a2019-12-16 19:12:32 +0800661 pr_info("disabling metacopy due to redirect_dir=%s\n",
Miklos Szeredid47748e2018-11-01 21:31:39 +0100662 config->redirect_mode);
663 config->metacopy = false;
664 } else {
665 /* Automatically enable redirect otherwise. */
666 config->redirect_follow = config->redirect_dir = true;
667 }
Vivek Goyald5791042018-05-11 11:49:27 -0400668 }
669
Amir Goldsteinb0def882020-04-09 18:58:34 +0300670 /* Resolve nfs_export -> index dependency */
671 if (config->nfs_export && !config->index) {
Amir Goldsteinf0e1266e2020-07-13 17:19:44 +0300672 if (!config->upperdir && config->redirect_follow) {
673 pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
674 config->nfs_export = false;
675 } else if (nfs_export_opt && index_opt) {
Amir Goldsteinb0def882020-04-09 18:58:34 +0300676 pr_err("conflicting options: nfs_export=on,index=off\n");
677 return -EINVAL;
Amir Goldsteinf0e1266e2020-07-13 17:19:44 +0300678 } else if (index_opt) {
Amir Goldsteinb0def882020-04-09 18:58:34 +0300679 /*
680 * There was an explicit index=off that resulted
681 * in this conflict.
682 */
683 pr_info("disabling nfs_export due to index=off\n");
684 config->nfs_export = false;
685 } else {
686 /* Automatically enable index otherwise. */
687 config->index = true;
688 }
689 }
690
691 /* Resolve nfs_export -> !metacopy dependency */
692 if (config->nfs_export && config->metacopy) {
693 if (nfs_export_opt && metacopy_opt) {
694 pr_err("conflicting options: nfs_export=on,metacopy=on\n");
695 return -EINVAL;
696 }
697 if (metacopy_opt) {
698 /*
699 * There was an explicit metacopy=on that resulted
700 * in this conflict.
701 */
702 pr_info("disabling nfs_export due to metacopy=on\n");
703 config->nfs_export = false;
704 } else {
705 /*
706 * There was an explicit nfs_export=on that resulted
707 * in this conflict.
708 */
709 pr_info("disabling metacopy due to nfs_export=on\n");
710 config->metacopy = false;
711 }
712 }
713
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +0100714
715 /* Resolve userxattr -> !redirect && !metacopy dependency */
716 if (config->userxattr) {
717 if (config->redirect_follow && redirect_opt) {
718 pr_err("conflicting options: userxattr,redirect_dir=%s\n",
719 config->redirect_mode);
720 return -EINVAL;
721 }
722 if (config->metacopy && metacopy_opt) {
723 pr_err("conflicting options: userxattr,metacopy=on\n");
724 return -EINVAL;
725 }
726 /*
727 * Silently disable default setting of redirect and metacopy.
728 * This shall be the default in the future as well: these
729 * options must be explicitly enabled if used together with
730 * userxattr.
731 */
732 config->redirect_dir = config->redirect_follow = false;
733 config->metacopy = false;
734 }
735
Vivek Goyald5791042018-05-11 11:49:27 -0400736 return 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200737}
738
739#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300740#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200741
Miklos Szerediad204482017-11-10 09:39:16 +0100742static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300743 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200744{
Miklos Szerediad204482017-11-10 09:39:16 +0100745 struct inode *dir = ofs->workbasedir->d_inode;
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200746 struct vfsmount *mnt = ovl_upper_mnt(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200747 struct dentry *work;
748 int err;
749 bool retried = false;
750
Al Viro59551022016-01-22 15:40:57 -0500751 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200752retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100753 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200754
755 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200756 struct iattr attr = {
757 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000758 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200759 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200760
761 if (work->d_inode) {
762 err = -EEXIST;
763 if (retried)
764 goto out_dput;
765
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300766 if (persist)
767 goto out_unlock;
768
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200769 retried = true;
Amir Goldstein235ce9e2020-08-30 23:28:03 +0300770 err = ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200771 dput(work);
Amir Goldstein235ce9e2020-08-30 23:28:03 +0300772 if (err == -EINVAL) {
773 work = ERR_PTR(err);
774 goto out_unlock;
775 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200776 goto retry;
777 }
778
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300779 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
780 err = PTR_ERR(work);
781 if (IS_ERR(work))
782 goto out_err;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200783
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200784 /*
785 * Try to remove POSIX ACL xattrs from workdir. We are good if:
786 *
787 * a) success (there was a POSIX ACL xattr and was removed)
788 * b) -ENODATA (there was no POSIX ACL xattr)
789 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
790 *
791 * There are various other error values that could effectively
792 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
793 * if the xattr name is too long), but the set of filesystems
794 * allowed as upper are limited to "normal" ones, where checking
795 * for the above two errors is sufficient.
796 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200797 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200798 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200799 goto out_dput;
800
801 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200802 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200803 goto out_dput;
804
805 /* Clear any inherited mode bits */
806 inode_lock(work->d_inode);
807 err = notify_change(work, &attr, NULL);
808 inode_unlock(work->d_inode);
809 if (err)
810 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300811 } else {
812 err = PTR_ERR(work);
813 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200814 }
815out_unlock:
youngjun2068cf72020-06-07 02:04:06 -0700816 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200817 return work;
818
819out_dput:
820 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300821out_err:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800822 pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100823 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300824 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200825 goto out_unlock;
826}
827
Miklos Szeredi91c77942014-11-20 16:40:00 +0100828static void ovl_unescape(char *s)
829{
830 char *d = s;
831
832 for (;; s++, d++) {
833 if (*s == '\\')
834 s++;
835 *d = *s;
836 if (!*s)
837 break;
838 }
839}
840
Miklos Szerediab508822014-12-13 00:59:49 +0100841static int ovl_mount_dir_noesc(const char *name, struct path *path)
842{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100843 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100844
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100845 if (!*name) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800846 pr_err("empty lowerdir\n");
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100847 goto out;
848 }
Miklos Szerediab508822014-12-13 00:59:49 +0100849 err = kern_path(name, LOOKUP_FOLLOW, path);
850 if (err) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800851 pr_err("failed to resolve '%s': %i\n", name, err);
Miklos Szerediab508822014-12-13 00:59:49 +0100852 goto out;
853 }
854 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200855 if (ovl_dentry_weird(path->dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800856 pr_err("filesystem on '%s' not supported\n", name);
Miklos Szerediab508822014-12-13 00:59:49 +0100857 goto out_put;
858 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100859 if (!d_is_dir(path->dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800860 pr_err("'%s' not a directory\n", name);
Miklos Szerediab508822014-12-13 00:59:49 +0100861 goto out_put;
862 }
863 return 0;
864
865out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100866 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100867out:
868 return err;
869}
870
871static int ovl_mount_dir(const char *name, struct path *path)
872{
873 int err = -ENOMEM;
874 char *tmp = kstrdup(name, GFP_KERNEL);
875
876 if (tmp) {
877 ovl_unescape(tmp);
878 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200879
Miklos Szeredibccece12020-03-17 15:04:22 +0100880 if (!err && path->dentry->d_flags & DCACHE_OP_REAL) {
Miklos Szeredi7925dad2020-03-17 15:04:22 +0100881 pr_err("filesystem on '%s' not supported as upperdir\n",
882 tmp);
883 path_put_init(path);
884 err = -EINVAL;
885 }
Miklos Szerediab508822014-12-13 00:59:49 +0100886 kfree(tmp);
887 }
888 return err;
889}
890
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100891static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
892 const char *name)
893{
894 struct kstatfs statfs;
895 int err = vfs_statfs(path, &statfs);
896
897 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +0800898 pr_err("statfs failed on '%s'\n", name);
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100899 else
900 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
901
902 return err;
903}
904
905static int ovl_lower_dir(const char *name, struct path *path,
Miklos Szeredif4288842020-03-17 15:04:22 +0100906 struct ovl_fs *ofs, int *stack_depth)
Miklos Szerediab508822014-12-13 00:59:49 +0100907{
Amir Goldsteine487d882017-11-07 13:55:04 +0200908 int fh_type;
Miklos Szerediab508822014-12-13 00:59:49 +0100909 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100910
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100911 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100912 if (err)
Miklos Szeredib8e42a62020-06-04 10:48:19 +0200913 return err;
Miklos Szerediab508822014-12-13 00:59:49 +0100914
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100915 err = ovl_check_namelen(path, ofs, name);
916 if (err)
Miklos Szeredib8e42a62020-06-04 10:48:19 +0200917 return err;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100918
Miklos Szerediab508822014-12-13 00:59:49 +0100919 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
920
Amir Goldstein02bcd152017-06-21 15:28:36 +0300921 /*
Amir Goldsteinf168f102018-01-19 11:26:53 +0200922 * The inodes index feature and NFS export need to encode and decode
923 * file handles, so they require that all layers support them.
Amir Goldstein02bcd152017-06-21 15:28:36 +0300924 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200925 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
Amir Goldsteinf168f102018-01-19 11:26:53 +0200926 if ((ofs->config.nfs_export ||
Amir Goldsteine487d882017-11-07 13:55:04 +0200927 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300928 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200929 ofs->config.nfs_export = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +0800930 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 +0200931 name);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300932 }
933
Amir Goldsteine487d882017-11-07 13:55:04 +0200934 /* Check if lower fs has 32bit inode numbers */
935 if (fh_type != FILEID_INO32_GEN)
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200936 ofs->xino_mode = -1;
Amir Goldsteine487d882017-11-07 13:55:04 +0200937
Miklos Szerediab508822014-12-13 00:59:49 +0100938 return 0;
Miklos Szerediab508822014-12-13 00:59:49 +0100939}
940
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200941/* Workdir should not be subdir of upperdir and vice versa */
942static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
943{
944 bool ok = false;
945
946 if (workdir != upperdir) {
947 ok = (lock_rename(workdir, upperdir) == NULL);
948 unlock_rename(workdir, upperdir);
949 }
950 return ok;
951}
952
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100953static unsigned int ovl_split_lowerdirs(char *str)
954{
955 unsigned int ctr = 1;
956 char *s, *d;
957
958 for (s = d = str;; s++, d++) {
959 if (*s == '\\') {
960 s++;
961 } else if (*s == ':') {
962 *d = '\0';
963 ctr++;
964 continue;
965 }
966 *d = *s;
967 if (!*s)
968 break;
969 }
970 return ctr;
971}
972
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200973static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200974ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
975 struct dentry *dentry, struct inode *inode,
976 const char *name, void *buffer, size_t size)
977{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200978 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200979}
980
981static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200982ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
983 struct dentry *dentry, struct inode *inode,
984 const char *name, const void *value,
985 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200986{
987 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200988 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200989 struct posix_acl *acl = NULL;
990 int err;
991
992 /* Check that everything is OK before copy-up */
993 if (value) {
994 acl = posix_acl_from_xattr(&init_user_ns, value, size);
995 if (IS_ERR(acl))
996 return PTR_ERR(acl);
997 }
998 err = -EOPNOTSUPP;
999 if (!IS_POSIXACL(d_inode(workdir)))
1000 goto out_acl_release;
1001 if (!realinode->i_op->set_acl)
1002 goto out_acl_release;
1003 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
1004 err = acl ? -EACCES : 0;
1005 goto out_acl_release;
1006 }
1007 err = -EPERM;
1008 if (!inode_owner_or_capable(inode))
1009 goto out_acl_release;
1010
1011 posix_acl_release(acl);
1012
Miklos Szeredifd3220d2016-10-31 14:42:14 +01001013 /*
1014 * Check if sgid bit needs to be cleared (actual setacl operation will
1015 * be done with mounter's capabilities and so that won't do it for us).
1016 */
1017 if (unlikely(inode->i_mode & S_ISGID) &&
1018 handler->flags == ACL_TYPE_ACCESS &&
1019 !in_group_p(inode->i_gid) &&
1020 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
1021 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
1022
1023 err = ovl_setattr(dentry, &iattr);
1024 if (err)
1025 return err;
1026 }
1027
Miklos Szeredi1d88f182017-07-20 11:08:21 +02001028 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +02001029 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001030 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +02001031
1032 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +02001033
1034out_acl_release:
1035 posix_acl_release(acl);
1036 return err;
1037}
1038
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001039static int ovl_own_xattr_get(const struct xattr_handler *handler,
1040 struct dentry *dentry, struct inode *inode,
1041 const char *name, void *buffer, size_t size)
1042{
Amir Goldstein48fab5d2016-11-16 11:22:39 +02001043 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001044}
1045
Miklos Szeredid837a492016-07-29 12:05:24 +02001046static int ovl_own_xattr_set(const struct xattr_handler *handler,
1047 struct dentry *dentry, struct inode *inode,
1048 const char *name, const void *value,
1049 size_t size, int flags)
1050{
Amir Goldstein48fab5d2016-11-16 11:22:39 +02001051 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +02001052}
1053
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001054static int ovl_other_xattr_get(const struct xattr_handler *handler,
1055 struct dentry *dentry, struct inode *inode,
1056 const char *name, void *buffer, size_t size)
1057{
Miklos Szeredi1d88f182017-07-20 11:08:21 +02001058 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001059}
1060
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +02001061static int ovl_other_xattr_set(const struct xattr_handler *handler,
1062 struct dentry *dentry, struct inode *inode,
1063 const char *name, const void *value,
1064 size_t size, int flags)
1065{
Miklos Szeredi1d88f182017-07-20 11:08:21 +02001066 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +02001067}
1068
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +02001069static const struct xattr_handler __maybe_unused
1070ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +02001071 .name = XATTR_NAME_POSIX_ACL_ACCESS,
1072 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001073 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +02001074 .set = ovl_posix_acl_xattr_set,
1075};
1076
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +02001077static const struct xattr_handler __maybe_unused
1078ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +02001079 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
1080 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001081 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +02001082 .set = ovl_posix_acl_xattr_set,
1083};
1084
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +01001085static const struct xattr_handler ovl_own_trusted_xattr_handler = {
1086 .prefix = OVL_XATTR_TRUSTED_PREFIX,
1087 .get = ovl_own_xattr_get,
1088 .set = ovl_own_xattr_set,
1089};
1090
1091static const struct xattr_handler ovl_own_user_xattr_handler = {
1092 .prefix = OVL_XATTR_USER_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001093 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +02001094 .set = ovl_own_xattr_set,
1095};
1096
1097static const struct xattr_handler ovl_other_xattr_handler = {
1098 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +02001099 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +02001100 .set = ovl_other_xattr_set,
1101};
1102
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +01001103static const struct xattr_handler *ovl_trusted_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +02001104#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +02001105 &ovl_posix_acl_access_xattr_handler,
1106 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +02001107#endif
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +01001108 &ovl_own_trusted_xattr_handler,
1109 &ovl_other_xattr_handler,
1110 NULL
1111};
1112
1113static const struct xattr_handler *ovl_user_xattr_handlers[] = {
1114#ifdef CONFIG_FS_POSIX_ACL
1115 &ovl_posix_acl_access_xattr_handler,
1116 &ovl_posix_acl_default_xattr_handler,
1117#endif
1118 &ovl_own_user_xattr_handler,
Miklos Szeredid837a492016-07-29 12:05:24 +02001119 &ovl_other_xattr_handler,
1120 NULL
1121};
1122
Amir Goldstein146d62e2019-04-18 17:42:08 +03001123static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
1124 struct inode **ptrap, const char *name)
1125{
1126 struct inode *trap;
1127 int err;
1128
1129 trap = ovl_get_trap_inode(sb, dir);
Arnd Bergmann1dac6f5b2019-06-17 14:39:29 +02001130 err = PTR_ERR_OR_ZERO(trap);
1131 if (err) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001132 if (err == -ELOOP)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001133 pr_err("conflicting %s path\n", name);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001134 return err;
1135 }
1136
1137 *ptrap = trap;
1138 return 0;
1139}
1140
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001141/*
1142 * Determine how we treat concurrent use of upperdir/workdir based on the
1143 * index feature. This is papering over mount leaks of container runtimes,
1144 * for example, an old overlay mount is leaked and now its upperdir is
1145 * attempted to be used as a lower layer in a new overlay mount.
1146 */
1147static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
1148{
1149 if (ofs->config.index) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001150 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 +03001151 name);
1152 return -EBUSY;
1153 } else {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001154 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 +03001155 name);
1156 return 0;
1157 }
1158}
1159
Amir Goldstein146d62e2019-04-18 17:42:08 +03001160static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001161 struct ovl_layer *upper_layer, struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001162{
Miklos Szeredi50649752017-11-10 09:39:15 +01001163 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001164 int err;
1165
Miklos Szerediad204482017-11-10 09:39:16 +01001166 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001167 if (err)
1168 goto out;
1169
1170 /* Upper fs should not be r/o */
1171 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001172 pr_err("upper fs is r/o, try multi-lower layers mount\n");
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001173 err = -EINVAL;
1174 goto out;
1175 }
1176
Miklos Szerediad204482017-11-10 09:39:16 +01001177 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001178 if (err)
1179 goto out;
1180
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001181 err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
Amir Goldstein146d62e2019-04-18 17:42:08 +03001182 "upperdir");
1183 if (err)
1184 goto out;
1185
Miklos Szeredi50649752017-11-10 09:39:15 +01001186 upper_mnt = clone_private_mount(upperpath);
1187 err = PTR_ERR(upper_mnt);
1188 if (IS_ERR(upper_mnt)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001189 pr_err("failed to clone upperpath\n");
Miklos Szeredi50649752017-11-10 09:39:15 +01001190 goto out;
1191 }
1192
1193 /* Don't inherit atime flags */
1194 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001195 upper_layer->mnt = upper_mnt;
1196 upper_layer->idx = 0;
1197 upper_layer->fsid = 0;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001198
Jeffle Xu654255f2020-04-23 19:06:55 +08001199 /*
1200 * Inherit SB_NOSEC flag from upperdir.
1201 *
1202 * This optimization changes behavior when a security related attribute
1203 * (suid/sgid/security.*) is changed on an underlying layer. This is
1204 * okay because we don't yet have guarantees in that case, but it will
1205 * need careful treatment once we want to honour changes to underlying
1206 * filesystems.
1207 */
1208 if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
1209 sb->s_flags |= SB_NOSEC;
1210
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001211 if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
Miklos Szeredi8c257412018-09-10 11:43:29 +02001212 ofs->upperdir_locked = true;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001213 } else {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001214 err = ovl_report_in_use(ofs, "upperdir");
1215 if (err)
1216 goto out;
Miklos Szeredi8c257412018-09-10 11:43:29 +02001217 }
1218
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001219 err = 0;
1220out:
1221 return err;
1222}
1223
Amir Goldsteincad218a2020-02-20 09:00:19 +02001224/*
1225 * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
1226 * negative values if error is encountered.
1227 */
1228static int ovl_check_rename_whiteout(struct dentry *workdir)
1229{
1230 struct inode *dir = d_inode(workdir);
1231 struct dentry *temp;
1232 struct dentry *dest;
1233 struct dentry *whiteout;
1234 struct name_snapshot name;
1235 int err;
1236
1237 inode_lock_nested(dir, I_MUTEX_PARENT);
1238
1239 temp = ovl_create_temp(workdir, OVL_CATTR(S_IFREG | 0));
1240 err = PTR_ERR(temp);
1241 if (IS_ERR(temp))
1242 goto out_unlock;
1243
1244 dest = ovl_lookup_temp(workdir);
1245 err = PTR_ERR(dest);
1246 if (IS_ERR(dest)) {
1247 dput(temp);
1248 goto out_unlock;
1249 }
1250
1251 /* Name is inline and stable - using snapshot as a copy helper */
1252 take_dentry_name_snapshot(&name, temp);
1253 err = ovl_do_rename(dir, temp, dir, dest, RENAME_WHITEOUT);
1254 if (err) {
1255 if (err == -EINVAL)
1256 err = 0;
1257 goto cleanup_temp;
1258 }
1259
1260 whiteout = lookup_one_len(name.name.name, workdir, name.name.len);
1261 err = PTR_ERR(whiteout);
1262 if (IS_ERR(whiteout))
1263 goto cleanup_temp;
1264
1265 err = ovl_is_whiteout(whiteout);
1266
1267 /* Best effort cleanup of whiteout and temp file */
1268 if (err)
1269 ovl_cleanup(dir, whiteout);
1270 dput(whiteout);
1271
1272cleanup_temp:
1273 ovl_cleanup(dir, temp);
1274 release_dentry_name_snapshot(&name);
1275 dput(temp);
1276 dput(dest);
1277
1278out_unlock:
1279 inode_unlock(dir);
1280
1281 return err;
1282}
1283
Vivek Goyalc86243b02020-08-31 14:15:29 -04001284static struct dentry *ovl_lookup_or_create(struct dentry *parent,
1285 const char *name, umode_t mode)
1286{
1287 size_t len = strlen(name);
1288 struct dentry *child;
1289
1290 inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
1291 child = lookup_one_len(name, parent, len);
1292 if (!IS_ERR(child) && !child->d_inode)
1293 child = ovl_create_real(parent->d_inode, child,
1294 OVL_CATTR(mode));
1295 inode_unlock(parent->d_inode);
1296 dput(parent);
1297
1298 return child;
1299}
1300
1301/*
1302 * Creates $workdir/work/incompat/volatile/dirty file if it is not already
1303 * present.
1304 */
1305static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
1306{
1307 unsigned int ctr;
1308 struct dentry *d = dget(ofs->workbasedir);
1309 static const char *const volatile_path[] = {
1310 OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
1311 };
1312 const char *const *name = volatile_path;
1313
1314 for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
1315 d = ovl_lookup_or_create(d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
1316 if (IS_ERR(d))
1317 return PTR_ERR(d);
1318 }
1319 dput(d);
1320 return 0;
1321}
1322
Amir Goldstein146d62e2019-04-18 17:42:08 +03001323static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1324 struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001325{
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001326 struct vfsmount *mnt = ovl_upper_mnt(ofs);
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001327 struct dentry *temp, *workdir;
Amir Goldsteind80172c2020-02-20 09:03:08 +02001328 bool rename_whiteout;
1329 bool d_type;
Amir Goldsteine487d882017-11-07 13:55:04 +02001330 int fh_type;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001331 int err;
1332
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001333 err = mnt_want_write(mnt);
1334 if (err)
1335 return err;
1336
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001337 workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1338 err = PTR_ERR(workdir);
1339 if (IS_ERR_OR_NULL(workdir))
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001340 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001341
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001342 ofs->workdir = workdir;
1343
Amir Goldstein146d62e2019-04-18 17:42:08 +03001344 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1345 if (err)
1346 goto out;
1347
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001348 /*
1349 * Upper should support d_type, else whiteouts are visible. Given
1350 * workdir and upper are on same fs, we can do iterate_dir() on
1351 * workdir. This check requires successful creation of workdir in
1352 * previous step.
1353 */
1354 err = ovl_check_d_type_supported(workpath);
1355 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001356 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001357
Amir Goldsteind80172c2020-02-20 09:03:08 +02001358 d_type = err;
1359 if (!d_type)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001360 pr_warn("upper fs needs to support d_type.\n");
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001361
1362 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +01001363 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1364 ofs->tmpfile = !IS_ERR(temp);
1365 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001366 dput(temp);
1367 else
lijiazi1bd0a3a2019-12-16 19:12:32 +08001368 pr_warn("upper fs does not support tmpfile.\n");
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001369
Amir Goldsteincad218a2020-02-20 09:00:19 +02001370
1371 /* Check if upper/work fs supports RENAME_WHITEOUT */
1372 err = ovl_check_rename_whiteout(ofs->workdir);
1373 if (err < 0)
1374 goto out;
1375
Amir Goldsteind80172c2020-02-20 09:03:08 +02001376 rename_whiteout = err;
1377 if (!rename_whiteout)
Amir Goldsteincad218a2020-02-20 09:00:19 +02001378 pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
1379
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001380 /*
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +01001381 * Check if upper/work fs supports (trusted|user).overlay.* xattr
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001382 */
Miklos Szeredi610afc02020-09-02 10:58:49 +02001383 err = ovl_do_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001384 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +01001385 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +03001386 ofs->config.index = false;
Vivek Goyald5791042018-05-11 11:49:27 -04001387 ofs->config.metacopy = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001388 pr_warn("upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001389 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001390 } else {
Miklos Szeredi610afc02020-09-02 10:58:49 +02001391 ovl_do_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001392 }
1393
Amir Goldsteind80172c2020-02-20 09:03:08 +02001394 /*
1395 * We allowed sub-optimal upper fs configuration and don't want to break
1396 * users over kernel upgrade, but we never allowed remote upper fs, so
1397 * we can enforce strict requirements for remote upper fs.
1398 */
1399 if (ovl_dentry_remote(ofs->workdir) &&
1400 (!d_type || !rename_whiteout || ofs->noxattr)) {
1401 pr_err("upper fs missing required features.\n");
1402 err = -EINVAL;
1403 goto out;
1404 }
1405
Vivek Goyalc86243b02020-08-31 14:15:29 -04001406 /*
1407 * For volatile mount, create a incompat/volatile/dirty file to keep
1408 * track of it.
1409 */
1410 if (ofs->config.ovl_volatile) {
1411 err = ovl_create_volatile_dirty(ofs);
1412 if (err < 0) {
1413 pr_err("Failed to create volatile/dirty file.\n");
1414 goto out;
1415 }
1416 }
1417
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001418 /* Check if upper/work fs supports file handles */
Amir Goldsteine487d882017-11-07 13:55:04 +02001419 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1420 if (ofs->config.index && !fh_type) {
Miklos Szerediad204482017-11-10 09:39:16 +01001421 ofs->config.index = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001422 pr_warn("upper fs does not support file handles, falling back to index=off.\n");
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001423 }
1424
Amir Goldsteine487d882017-11-07 13:55:04 +02001425 /* Check if upper fs has 32bit inode numbers */
1426 if (fh_type != FILEID_INO32_GEN)
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001427 ofs->xino_mode = -1;
Amir Goldsteine487d882017-11-07 13:55:04 +02001428
Amir Goldsteinf168f102018-01-19 11:26:53 +02001429 /* NFS export of r/w mount depends on index */
1430 if (ofs->config.nfs_export && !ofs->config.index) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001431 pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
Amir Goldsteinf168f102018-01-19 11:26:53 +02001432 ofs->config.nfs_export = false;
1433 }
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001434out:
1435 mnt_drop_write(mnt);
1436 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001437}
1438
Amir Goldstein146d62e2019-04-18 17:42:08 +03001439static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1440 struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001441{
1442 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001443 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001444
Miklos Szerediad204482017-11-10 09:39:16 +01001445 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001446 if (err)
1447 goto out;
1448
1449 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001450 if (upperpath->mnt != workpath.mnt) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001451 pr_err("workdir and upperdir must reside under the same mount\n");
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001452 goto out;
1453 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001454 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001455 pr_err("workdir and upperdir must be separate subtrees\n");
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001456 goto out;
1457 }
1458
Miklos Szeredi8c257412018-09-10 11:43:29 +02001459 ofs->workbasedir = dget(workpath.dentry);
1460
Miklos Szeredi8c257412018-09-10 11:43:29 +02001461 if (ovl_inuse_trylock(ofs->workbasedir)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001462 ofs->workdir_locked = true;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001463 } else {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001464 err = ovl_report_in_use(ofs, "workdir");
1465 if (err)
1466 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001467 }
1468
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001469 err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
1470 "workdir");
1471 if (err)
1472 goto out;
1473
Amir Goldstein146d62e2019-04-18 17:42:08 +03001474 err = ovl_make_workdir(sb, ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001475
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001476out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001477 path_put(&workpath);
1478
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001479 return err;
1480}
1481
Amir Goldstein146d62e2019-04-18 17:42:08 +03001482static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1483 struct ovl_entry *oe, struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001484{
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001485 struct vfsmount *mnt = ovl_upper_mnt(ofs);
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001486 struct dentry *indexdir;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001487 int err;
1488
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001489 err = mnt_want_write(mnt);
1490 if (err)
1491 return err;
1492
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001493 /* Verify lower root is upper root origin */
Miklos Szeredi610afc02020-09-02 10:58:49 +02001494 err = ovl_verify_origin(ofs, upperpath->dentry,
1495 oe->lowerstack[0].dentry, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001496 if (err) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001497 pr_err("failed to verify upper root origin\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001498 goto out;
1499 }
1500
Amir Goldstein470c1562020-07-13 17:19:43 +03001501 /* index dir will act also as workdir */
1502 iput(ofs->workdir_trap);
1503 ofs->workdir_trap = NULL;
1504 dput(ofs->workdir);
1505 ofs->workdir = NULL;
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001506 indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1507 if (IS_ERR(indexdir)) {
1508 err = PTR_ERR(indexdir);
1509 } else if (indexdir) {
1510 ofs->indexdir = indexdir;
1511 ofs->workdir = dget(indexdir);
Amir Goldstein20396362020-06-21 09:37:59 +03001512
Amir Goldstein146d62e2019-04-18 17:42:08 +03001513 err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1514 "indexdir");
1515 if (err)
1516 goto out;
1517
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001518 /*
1519 * Verify upper root is exclusively associated with index dir.
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +01001520 * Older kernels stored upper fh in ".overlay.origin"
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001521 * xattr. If that xattr exists, verify that it is a match to
1522 * upper dir file handle. In any case, verify or set xattr
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +01001523 * ".overlay.upper" to indicate that index may have
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001524 * directory entries.
1525 */
Miklos Szeredi610afc02020-09-02 10:58:49 +02001526 if (ovl_check_origin_xattr(ofs, ofs->indexdir)) {
1527 err = ovl_verify_set_fh(ofs, ofs->indexdir,
1528 OVL_XATTR_ORIGIN,
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001529 upperpath->dentry, true, false);
1530 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001531 pr_err("failed to verify index dir 'origin' xattr\n");
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001532 }
Miklos Szeredi610afc02020-09-02 10:58:49 +02001533 err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry,
1534 true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001535 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001536 pr_err("failed to verify index dir 'upper' xattr\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001537
1538 /* Cleanup bad/stale/orphan index entries */
1539 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001540 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001541 }
Miklos Szerediad204482017-11-10 09:39:16 +01001542 if (err || !ofs->indexdir)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001543 pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001544
1545out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001546 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001547 return err;
1548}
1549
Amir Goldstein9df085f2018-09-03 09:12:09 +03001550static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
Amir Goldstein51486262018-03-28 20:22:41 +03001551{
1552 unsigned int i;
Amir Goldstein9df085f2018-09-03 09:12:09 +03001553
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001554 if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
Amir Goldstein9df085f2018-09-03 09:12:09 +03001555 return true;
1556
Amir Goldsteina888db32020-07-08 16:16:13 +03001557 /*
1558 * We allow using single lower with null uuid for index and nfs_export
1559 * for example to support those features with single lower squashfs.
1560 * To avoid regressions in setups of overlay with re-formatted lower
1561 * squashfs, do not allow decoding origin with lower null uuid unless
1562 * user opted-in to one of the new features that require following the
1563 * lower inode of non-dir upper.
1564 */
1565 if (!ofs->config.index && !ofs->config.metacopy && !ofs->config.xino &&
1566 uuid_is_null(uuid))
1567 return false;
1568
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001569 for (i = 0; i < ofs->numfs; i++) {
Amir Goldstein9df085f2018-09-03 09:12:09 +03001570 /*
1571 * We use uuid to associate an overlay lower file handle with a
1572 * lower layer, so we can accept lower fs with null uuid as long
1573 * as all lower layers with null uuid are on the same fs.
Amir Goldstein7e63c872019-11-14 22:28:41 +02001574 * if we detect multiple lower fs with the same uuid, we
1575 * disable lower file handle decoding on all of them.
Amir Goldstein9df085f2018-09-03 09:12:09 +03001576 */
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001577 if (ofs->fs[i].is_lower &&
1578 uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
Amir Goldstein07f1e592020-01-14 21:59:22 +02001579 ofs->fs[i].bad_uuid = true;
Amir Goldstein9df085f2018-09-03 09:12:09 +03001580 return false;
Amir Goldstein7e63c872019-11-14 22:28:41 +02001581 }
Amir Goldstein9df085f2018-09-03 09:12:09 +03001582 }
1583 return true;
1584}
1585
1586/* Get a unique fsid for the layer */
1587static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1588{
1589 struct super_block *sb = path->mnt->mnt_sb;
1590 unsigned int i;
Amir Goldstein51486262018-03-28 20:22:41 +03001591 dev_t dev;
1592 int err;
Amir Goldstein7e63c872019-11-14 22:28:41 +02001593 bool bad_uuid = false;
Amir Goldstein51486262018-03-28 20:22:41 +03001594
Amir Goldstein07f1e592020-01-14 21:59:22 +02001595 for (i = 0; i < ofs->numfs; i++) {
1596 if (ofs->fs[i].sb == sb)
1597 return i;
Amir Goldstein51486262018-03-28 20:22:41 +03001598 }
1599
Amir Goldstein9df085f2018-09-03 09:12:09 +03001600 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
Amir Goldstein7e63c872019-11-14 22:28:41 +02001601 bad_uuid = true;
1602 if (ofs->config.index || ofs->config.nfs_export) {
1603 ofs->config.index = false;
1604 ofs->config.nfs_export = false;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001605 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 +02001606 uuid_is_null(&sb->s_uuid) ? "null" :
1607 "conflicting",
1608 path->dentry);
1609 }
Amir Goldstein9df085f2018-09-03 09:12:09 +03001610 }
1611
Amir Goldstein51486262018-03-28 20:22:41 +03001612 err = get_anon_bdev(&dev);
1613 if (err) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001614 pr_err("failed to get anonymous bdev for lowerpath\n");
Amir Goldstein51486262018-03-28 20:22:41 +03001615 return err;
1616 }
1617
Amir Goldstein07f1e592020-01-14 21:59:22 +02001618 ofs->fs[ofs->numfs].sb = sb;
1619 ofs->fs[ofs->numfs].pseudo_dev = dev;
1620 ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
Amir Goldstein51486262018-03-28 20:22:41 +03001621
Amir Goldstein07f1e592020-01-14 21:59:22 +02001622 return ofs->numfs++;
Amir Goldstein51486262018-03-28 20:22:41 +03001623}
1624
Amir Goldstein94375f92019-11-15 14:12:40 +02001625static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001626 struct path *stack, unsigned int numlower,
1627 struct ovl_layer *layers)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001628{
1629 int err;
1630 unsigned int i;
1631
1632 err = -ENOMEM;
Amir Goldstein07f1e592020-01-14 21:59:22 +02001633 ofs->fs = kcalloc(numlower + 1, sizeof(struct ovl_sb), GFP_KERNEL);
1634 if (ofs->fs == NULL)
Amir Goldstein51486262018-03-28 20:22:41 +03001635 goto out;
1636
Amir Goldstein07f1e592020-01-14 21:59:22 +02001637 /* idx/fsid 0 are reserved for upper fs even with lower only overlay */
1638 ofs->numfs++;
1639
Amir Goldstein07f1e592020-01-14 21:59:22 +02001640 /*
Amir Goldsteinb7bf9902020-01-14 22:17:25 +02001641 * All lower layers that share the same fs as upper layer, use the same
1642 * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower
1643 * only overlay to simplify ovl_fs_free().
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001644 * is_lower will be set if upper fs is shared with a lower layer.
Amir Goldstein07f1e592020-01-14 21:59:22 +02001645 */
Amir Goldsteinb7bf9902020-01-14 22:17:25 +02001646 err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
1647 if (err) {
1648 pr_err("failed to get anonymous bdev for upper fs\n");
1649 goto out;
1650 }
1651
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001652 if (ovl_upper_mnt(ofs)) {
1653 ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001654 ofs->fs[0].is_lower = false;
Amir Goldstein07f1e592020-01-14 21:59:22 +02001655 }
1656
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001657 for (i = 0; i < numlower; i++) {
1658 struct vfsmount *mnt;
Amir Goldstein146d62e2019-04-18 17:42:08 +03001659 struct inode *trap;
Amir Goldstein51486262018-03-28 20:22:41 +03001660 int fsid;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001661
Amir Goldstein9df085f2018-09-03 09:12:09 +03001662 err = fsid = ovl_get_fsid(ofs, &stack[i]);
Amir Goldstein51486262018-03-28 20:22:41 +03001663 if (err < 0)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001664 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001665
youngjun24f14002020-06-16 17:30:43 +09001666 /*
1667 * Check if lower root conflicts with this overlay layers before
1668 * checking if it is in-use as upperdir/workdir of "another"
1669 * mount, because we do not bother to check in ovl_is_inuse() if
1670 * the upperdir/workdir is in fact in-use by our
1671 * upperdir/workdir.
1672 */
Amir Goldstein146d62e2019-04-18 17:42:08 +03001673 err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1674 if (err)
1675 goto out;
1676
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001677 if (ovl_is_inuse(stack[i].dentry)) {
1678 err = ovl_report_in_use(ofs, "lowerdir");
youngjun24f14002020-06-16 17:30:43 +09001679 if (err) {
1680 iput(trap);
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001681 goto out;
youngjun24f14002020-06-16 17:30:43 +09001682 }
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001683 }
1684
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001685 mnt = clone_private_mount(&stack[i]);
1686 err = PTR_ERR(mnt);
1687 if (IS_ERR(mnt)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001688 pr_err("failed to clone lowerpath\n");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001689 iput(trap);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001690 goto out;
1691 }
Amir Goldstein51486262018-03-28 20:22:41 +03001692
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001693 /*
1694 * Make lower layers R/O. That way fchmod/fchown on lower file
1695 * will fail instead of modifying lower fs.
1696 */
1697 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1698
Miklos Szeredi13464162020-01-24 09:46:45 +01001699 layers[ofs->numlayer].trap = trap;
1700 layers[ofs->numlayer].mnt = mnt;
1701 layers[ofs->numlayer].idx = ofs->numlayer;
1702 layers[ofs->numlayer].fsid = fsid;
1703 layers[ofs->numlayer].fs = &ofs->fs[fsid];
Amir Goldstein94375f92019-11-15 14:12:40 +02001704 ofs->numlayer++;
Amir Goldstein1b81ddd2019-11-16 18:52:20 +02001705 ofs->fs[fsid].is_lower = true;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001706 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001707
Amir Goldstein795939a2018-03-29 09:08:18 +03001708 /*
1709 * When all layers on same fs, overlay can use real inode numbers.
Amir Goldstein926e94d2020-02-21 16:34:45 +02001710 * With mount option "xino=<on|auto>", mounter declares that there are
1711 * enough free high bits in underlying fs to hold the unique fsid.
Amir Goldstein795939a2018-03-29 09:08:18 +03001712 * If overlayfs does encounter underlying inodes using the high xino
1713 * bits reserved for fsid, it emits a warning and uses the original
Amir Goldsteindfe51d42020-02-21 16:34:44 +02001714 * inode number or a non persistent inode number allocated from a
1715 * dedicated range.
Amir Goldstein795939a2018-03-29 09:08:18 +03001716 */
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001717 if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001718 if (ofs->config.xino == OVL_XINO_ON)
1719 pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
1720 ofs->xino_mode = 0;
Amir Goldstein53afcd32020-02-21 16:34:42 +02001721 } else if (ofs->config.xino == OVL_XINO_OFF) {
1722 ofs->xino_mode = -1;
Amir Goldstein926e94d2020-02-21 16:34:45 +02001723 } else if (ofs->xino_mode < 0) {
Amir Goldstein795939a2018-03-29 09:08:18 +03001724 /*
Amir Goldstein07f1e592020-01-14 21:59:22 +02001725 * This is a roundup of number of bits needed for encoding
Amir Goldsteindfe51d42020-02-21 16:34:44 +02001726 * fsid, where fsid 0 is reserved for upper fs (even with
1727 * lower only overlay) +1 extra bit is reserved for the non
1728 * persistent inode number range that is used for resolving
1729 * xino lower bits overflow.
Amir Goldstein795939a2018-03-29 09:08:18 +03001730 */
Amir Goldsteindfe51d42020-02-21 16:34:44 +02001731 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
1732 ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
Amir Goldstein795939a2018-03-29 09:08:18 +03001733 }
1734
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001735 if (ofs->xino_mode > 0) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001736 pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001737 ofs->xino_mode);
Amir Goldstein795939a2018-03-29 09:08:18 +03001738 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001739
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001740 err = 0;
1741out:
1742 return err;
1743}
1744
Miklos Szeredi4155c102017-11-10 09:39:15 +01001745static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001746 const char *lower, unsigned int numlower,
1747 struct ovl_fs *ofs, struct ovl_layer *layers)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001748{
1749 int err;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001750 struct path *stack = NULL;
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001751 unsigned int i;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001752 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001753
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001754 if (!ofs->config.upperdir && numlower == 1) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001755 pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001756 return ERR_PTR(-EINVAL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001757 }
1758
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001759 stack = kcalloc(numlower, sizeof(struct path), GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001760 if (!stack)
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001761 return ERR_PTR(-ENOMEM);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001762
1763 err = -EINVAL;
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001764 for (i = 0; i < numlower; i++) {
1765 err = ovl_lower_dir(lower, &stack[i], ofs, &sb->s_stack_depth);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001766 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001767 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001768
1769 lower = strchr(lower, '\0') + 1;
1770 }
1771
1772 err = -EINVAL;
1773 sb->s_stack_depth++;
1774 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001775 pr_err("maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001776 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001777 }
1778
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001779 err = ovl_get_layers(sb, ofs, stack, numlower, layers);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001780 if (err)
1781 goto out_err;
1782
1783 err = -ENOMEM;
1784 oe = ovl_alloc_entry(numlower);
1785 if (!oe)
1786 goto out_err;
1787
1788 for (i = 0; i < numlower; i++) {
1789 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Amir Goldstein94375f92019-11-15 14:12:40 +02001790 oe->lowerstack[i].layer = &ofs->layers[i+1];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001791 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001792
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001793out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001794 for (i = 0; i < numlower; i++)
1795 path_put(&stack[i]);
1796 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001797
1798 return oe;
1799
1800out_err:
1801 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001802 goto out;
1803}
1804
Amir Goldstein146d62e2019-04-18 17:42:08 +03001805/*
1806 * Check if this layer root is a descendant of:
1807 * - another layer of this overlayfs instance
1808 * - upper/work dir of any overlayfs instance
Amir Goldstein146d62e2019-04-18 17:42:08 +03001809 */
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001810static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
1811 struct dentry *dentry, const char *name)
Amir Goldstein146d62e2019-04-18 17:42:08 +03001812{
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001813 struct dentry *next = dentry, *parent;
Amir Goldstein146d62e2019-04-18 17:42:08 +03001814 int err = 0;
1815
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001816 if (!dentry)
Amir Goldstein146d62e2019-04-18 17:42:08 +03001817 return 0;
1818
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001819 parent = dget_parent(next);
1820
1821 /* Walk back ancestors to root (inclusive) looking for traps */
1822 while (!err && parent != next) {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001823 if (ovl_lookup_trap_inode(sb, parent)) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03001824 err = -ELOOP;
lijiazi1bd0a3a2019-12-16 19:12:32 +08001825 pr_err("overlapping %s path\n", name);
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001826 } else if (ovl_is_inuse(parent)) {
1827 err = ovl_report_in_use(ofs, name);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001828 }
Amir Goldstein146d62e2019-04-18 17:42:08 +03001829 next = parent;
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001830 parent = dget_parent(next);
1831 dput(next);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001832 }
1833
Miklos Szeredi9179c21d2019-06-18 15:06:16 +02001834 dput(parent);
Amir Goldstein146d62e2019-04-18 17:42:08 +03001835
1836 return err;
1837}
1838
1839/*
1840 * Check if any of the layers or work dirs overlap.
1841 */
1842static int ovl_check_overlapping_layers(struct super_block *sb,
1843 struct ovl_fs *ofs)
1844{
1845 int i, err;
1846
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001847 if (ovl_upper_mnt(ofs)) {
1848 err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001849 "upperdir");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001850 if (err)
1851 return err;
1852
1853 /*
1854 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1855 * this instance and covers overlapping work and index dirs,
1856 * unless work or index dir have been moved since created inside
1857 * workbasedir. In that case, we already have their traps in
1858 * inode cache and we will catch that case on lookup.
1859 */
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001860 err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir");
Amir Goldstein146d62e2019-04-18 17:42:08 +03001861 if (err)
1862 return err;
1863 }
1864
Amir Goldstein94375f92019-11-15 14:12:40 +02001865 for (i = 1; i < ofs->numlayer; i++) {
Amir Goldstein0be0bfd2019-07-12 15:24:34 +03001866 err = ovl_check_layer(sb, ofs,
Amir Goldstein94375f92019-11-15 14:12:40 +02001867 ofs->layers[i].mnt->mnt_root,
Amir Goldstein146d62e2019-04-18 17:42:08 +03001868 "lowerdir");
1869 if (err)
1870 return err;
1871 }
1872
1873 return 0;
1874}
1875
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001876static struct dentry *ovl_get_root(struct super_block *sb,
1877 struct dentry *upperdentry,
1878 struct ovl_entry *oe)
1879{
1880 struct dentry *root;
Amir Goldstein62c832e2019-11-19 15:31:46 +02001881 struct ovl_path *lowerpath = &oe->lowerstack[0];
1882 unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
1883 int fsid = lowerpath->layer->fsid;
1884 struct ovl_inode_params oip = {
1885 .upperdentry = upperdentry,
1886 .lowerpath = lowerpath,
1887 };
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001888
1889 root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1890 if (!root)
1891 return NULL;
1892
1893 root->d_fsdata = oe;
1894
1895 if (upperdentry) {
Amir Goldstein62c832e2019-11-19 15:31:46 +02001896 /* Root inode uses upper st_ino/i_ino */
1897 ino = d_inode(upperdentry)->i_ino;
1898 fsid = 0;
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001899 ovl_dentry_set_upper_alias(root);
Miklos Szeredi610afc02020-09-02 10:58:49 +02001900 if (ovl_is_impuredir(sb, upperdentry))
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001901 ovl_set_flag(OVL_IMPURE, d_inode(root));
1902 }
1903
1904 /* Root is always merge -> can have whiteouts */
1905 ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
1906 ovl_dentry_set_flag(OVL_E_CONNECTED, root);
1907 ovl_set_upperdata(d_inode(root));
Amir Goldstein62c832e2019-11-19 15:31:46 +02001908 ovl_inode_init(d_inode(root), &oip, ino, fsid);
Miklos Szeredif4288842020-03-17 15:04:22 +01001909 ovl_dentry_update_reval(root, upperdentry, DCACHE_OP_WEAK_REVALIDATE);
Amir Goldstein2effc5c2019-11-19 17:49:17 +02001910
1911 return root;
1912}
1913
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001914static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1915{
Kees Cook33006cd2017-03-29 14:02:19 -07001916 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001917 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001918 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001919 struct ovl_fs *ofs;
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001920 struct ovl_layer *layers;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001921 struct cred *cred;
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001922 char *splitlower = NULL;
1923 unsigned int numlower;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001924 int err;
1925
Miklos Szeredif4288842020-03-17 15:04:22 +01001926 sb->s_d_op = &ovl_dentry_operations;
1927
Erez Zadokf45827e82014-10-24 00:14:38 +02001928 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001929 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1930 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001931 goto out;
1932
Miklos Szerediad204482017-11-10 09:39:16 +01001933 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001934 if (!cred)
1935 goto out_err;
1936
Chengguang Xuc21c8392020-04-24 10:55:17 +08001937 /* Is there a reason anyone would want not to share whiteouts? */
1938 ofs->share_whiteout = true;
1939
Miklos Szerediad204482017-11-10 09:39:16 +01001940 ofs->config.index = ovl_index_def;
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +03001941 ofs->config.uuid = true;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001942 ofs->config.nfs_export = ovl_nfs_export_def;
Amir Goldstein795939a2018-03-29 09:08:18 +03001943 ofs->config.xino = ovl_xino_def();
Vivek Goyald5791042018-05-11 11:49:27 -04001944 ofs->config.metacopy = ovl_metacopy_def;
Miklos Szerediad204482017-11-10 09:39:16 +01001945 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001946 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001947 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001948
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001949 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001950 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001951 if (!silent)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001952 pr_err("missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001953 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001954 }
1955
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001956 err = -ENOMEM;
1957 splitlower = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1958 if (!splitlower)
1959 goto out_err;
1960
1961 numlower = ovl_split_lowerdirs(splitlower);
1962 if (numlower > OVL_MAX_STACK) {
1963 pr_err("too many lower directories, limit is %d\n",
1964 OVL_MAX_STACK);
1965 goto out_err;
1966 }
1967
1968 layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL);
1969 if (!layers)
1970 goto out_err;
1971
1972 ofs->layers = layers;
1973 /* Layer 0 is reserved for upper even if there's no upper */
1974 ofs->numlayer = 1;
1975
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001976 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001977 sb->s_maxbytes = MAX_LFS_FILESIZE;
Amir Goldstein4d314f72020-02-21 16:34:43 +02001978 atomic_long_set(&ofs->last_ino, 1);
Amir Goldsteine487d882017-11-07 13:55:04 +02001979 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
Amir Goldstein53afcd32020-02-21 16:34:42 +02001980 if (ofs->config.xino != OVL_XINO_OFF) {
Amir Goldstein0f831ec2019-11-16 18:14:41 +02001981 ofs->xino_mode = BITS_PER_LONG - 32;
Amir Goldstein53afcd32020-02-21 16:34:42 +02001982 if (!ofs->xino_mode) {
1983 pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
1984 ofs->config.xino = OVL_XINO_OFF;
1985 }
1986 }
Amir Goldstein795939a2018-03-29 09:08:18 +03001987
Amir Goldstein146d62e2019-04-18 17:42:08 +03001988 /* alloc/destroy_inode needed for setting up traps in inode cache */
1989 sb->s_op = &ovl_super_operations;
1990
Miklos Szerediad204482017-11-10 09:39:16 +01001991 if (ofs->config.upperdir) {
1992 if (!ofs->config.workdir) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001993 pr_err("missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001994 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001995 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001996
Miklos Szeredib8e42a62020-06-04 10:48:19 +02001997 err = ovl_get_upper(sb, ofs, &layers[0], &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001998 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001999 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02002000
Amir Goldstein146d62e2019-04-18 17:42:08 +03002001 err = ovl_get_workdir(sb, ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01002002 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01002003 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01002004
Miklos Szerediad204482017-11-10 09:39:16 +01002005 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002006 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01002007
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02002008 sb->s_stack_depth = ovl_upper_mnt(ofs)->mnt_sb->s_stack_depth;
2009 sb->s_time_gran = ovl_upper_mnt(ofs)->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01002010
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002011 }
Miklos Szeredib8e42a62020-06-04 10:48:19 +02002012 oe = ovl_get_lowerstack(sb, splitlower, numlower, ofs, layers);
Miklos Szeredi4155c102017-11-10 09:39:15 +01002013 err = PTR_ERR(oe);
2014 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01002015 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002016
hujianyang71cbad72015-01-15 13:20:57 +08002017 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02002018 if (!ovl_upper_mnt(ofs))
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002019 sb->s_flags |= SB_RDONLY;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002020
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +03002021 if (!ofs->config.uuid && ofs->numfs > 1) {
2022 pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=on.\n");
2023 ofs->config.uuid = true;
2024 }
2025
Amir Goldstein470c1562020-07-13 17:19:43 +03002026 if (!ovl_force_readonly(ofs) && ofs->config.index) {
Amir Goldstein146d62e2019-04-18 17:42:08 +03002027 err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03002028 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01002029 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01002030
Amir Goldstein972d0092017-09-19 12:14:18 +03002031 /* Force r/o mount with no index dir */
Amir Goldstein20396362020-06-21 09:37:59 +03002032 if (!ofs->indexdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002033 sb->s_flags |= SB_RDONLY;
Amir Goldstein02bcd152017-06-21 15:28:36 +03002034 }
2035
Amir Goldstein146d62e2019-04-18 17:42:08 +03002036 err = ovl_check_overlapping_layers(sb, ofs);
2037 if (err)
2038 goto out_free_oe;
2039
Amir Goldstein972d0092017-09-19 12:14:18 +03002040 /* Show index=off in /proc/mounts for forced r/o mount */
Amir Goldsteinf168f102018-01-19 11:26:53 +02002041 if (!ofs->indexdir) {
Miklos Szerediad204482017-11-10 09:39:16 +01002042 ofs->config.index = false;
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02002043 if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08002044 pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
Amir Goldsteinf168f102018-01-19 11:26:53 +02002045 ofs->config.nfs_export = false;
2046 }
2047 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03002048
Vivek Goyald5791042018-05-11 11:49:27 -04002049 if (ofs->config.metacopy && ofs->config.nfs_export) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08002050 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 -04002051 ofs->config.nfs_export = false;
2052 }
2053
Amir Goldstein8383f172017-10-02 11:31:42 +03002054 if (ofs->config.nfs_export)
2055 sb->s_export_op = &ovl_export_operations;
2056
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03002057 /* Never override disk quota limits or use reserved space */
2058 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
2059
Vivek Goyal655042c2016-10-14 03:03:36 +02002060 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredi2d2f2d72020-12-14 15:26:14 +01002061 sb->s_xattr = ofs->config.userxattr ? ovl_user_xattr_handlers :
2062 ovl_trusted_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01002063 sb->s_fs_info = ofs;
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002064 sb->s_flags |= SB_POSIXACL;
Konstantin Khlebnikov32b1924b22020-04-09 11:29:47 +03002065 sb->s_iflags |= SB_I_SKIP_SYNC;
Vivek Goyal655042c2016-10-14 03:03:36 +02002066
Miklos Szeredic6fe6252017-11-10 09:39:15 +01002067 err = -ENOMEM;
Amir Goldstein2effc5c2019-11-19 17:49:17 +02002068 root_dentry = ovl_get_root(sb, upperpath.dentry, oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002069 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01002070 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002071
2072 mntput(upperpath.mnt);
Miklos Szeredib8e42a62020-06-04 10:48:19 +02002073 kfree(splitlower);
Miklos Szeredied06e062015-12-09 16:11:59 +01002074
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002075 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002076
2077 return 0;
2078
Miklos Szeredi4155c102017-11-10 09:39:15 +01002079out_free_oe:
2080 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05002081 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01002082out_err:
Miklos Szeredib8e42a62020-06-04 10:48:19 +02002083 kfree(splitlower);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002084 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01002085 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002086out:
2087 return err;
2088}
2089
2090static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
2091 const char *dev_name, void *raw_data)
2092{
2093 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
2094}
2095
2096static struct file_system_type ovl_fs_type = {
2097 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01002098 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002099 .mount = ovl_mount,
2100 .kill_sb = kill_anon_super,
2101};
Miklos Szeredief94b182014-11-20 16:39:59 +01002102MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002103
Amir Goldstein13cf1992017-06-12 09:54:40 +03002104static void ovl_inode_init_once(void *foo)
2105{
2106 struct ovl_inode *oi = foo;
2107
2108 inode_init_once(&oi->vfs_inode);
2109}
2110
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002111static int __init ovl_init(void)
2112{
Amir Goldstein13cf1992017-06-12 09:54:40 +03002113 int err;
2114
2115 ovl_inode_cachep = kmem_cache_create("ovl_inode",
2116 sizeof(struct ovl_inode), 0,
2117 (SLAB_RECLAIM_ACCOUNT|
2118 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2119 ovl_inode_init_once);
2120 if (ovl_inode_cachep == NULL)
2121 return -ENOMEM;
2122
Jiufei Xue2406a302019-11-20 17:45:26 +08002123 err = ovl_aio_request_cache_init();
2124 if (!err) {
2125 err = register_filesystem(&ovl_fs_type);
2126 if (!err)
2127 return 0;
2128
2129 ovl_aio_request_cache_destroy();
2130 }
2131 kmem_cache_destroy(ovl_inode_cachep);
Amir Goldstein13cf1992017-06-12 09:54:40 +03002132
2133 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002134}
2135
2136static void __exit ovl_exit(void)
2137{
2138 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03002139
2140 /*
2141 * Make sure all delayed rcu free inodes are flushed before we
2142 * destroy cache.
2143 */
2144 rcu_barrier();
2145 kmem_cache_destroy(ovl_inode_cachep);
Jiufei Xue2406a302019-11-20 17:45:26 +08002146 ovl_aio_request_cache_destroy();
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002147}
2148
2149module_init(ovl_init);
2150module_exit(ovl_exit);