blob: 2e0fc93c2c06646ccf5c67c43836f9d4989da501 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <uapi/linux/magic.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011#include <linux/fs.h>
12#include <linux/namei.h>
13#include <linux/xattr.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020014#include <linux/mount.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020015#include <linux/parser.h>
16#include <linux/module.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020017#include <linux/statfs.h>
Erez Zadokf45827e82014-10-24 00:14:38 +020018#include <linux/seq_file.h>
Miklos Szeredid837a492016-07-29 12:05:24 +020019#include <linux/posix_acl_xattr.h>
Amir Goldsteine487d882017-11-07 13:55:04 +020020#include <linux/exportfs.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020021#include "overlayfs.h"
22
23MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24MODULE_DESCRIPTION("Overlay filesystem");
25MODULE_LICENSE("GPL");
26
Miklos Szeredie9be9d52014-10-24 00:14:38 +020027
28struct ovl_dir_cache;
29
Miklos Szeredia78d9f02014-12-13 00:59:52 +010030#define OVL_MAX_STACK 500
31
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010032static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
33module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
34MODULE_PARM_DESC(ovl_redirect_dir_def,
35 "Default to on or off for the redirect_dir feature");
Miklos Szeredie9be9d52014-10-24 00:14:38 +020036
Miklos Szeredi438c84c2017-12-11 11:28:10 +010037static bool ovl_redirect_always_follow =
38 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
39module_param_named(redirect_always_follow, ovl_redirect_always_follow,
40 bool, 0644);
41MODULE_PARM_DESC(ovl_redirect_always_follow,
42 "Follow redirects even if redirect_dir feature is turned off");
43
Amir Goldstein02bcd152017-06-21 15:28:36 +030044static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
45module_param_named(index, ovl_index_def, bool, 0644);
46MODULE_PARM_DESC(ovl_index_def,
47 "Default to on or off for the inodes index feature");
48
Amir Goldsteinf168f102018-01-19 11:26:53 +020049static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
50module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
51MODULE_PARM_DESC(ovl_nfs_export_def,
52 "Default to on or off for the NFS export feature");
53
Amir Goldstein795939a2018-03-29 09:08:18 +030054static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
55module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
56MODULE_PARM_DESC(ovl_xino_auto_def,
57 "Auto enable xino feature");
58
Miklos Szeredi4155c102017-11-10 09:39:15 +010059static void ovl_entry_stack_free(struct ovl_entry *oe)
60{
61 unsigned int i;
62
63 for (i = 0; i < oe->numlower; i++)
64 dput(oe->lowerstack[i].dentry);
65}
66
Vivek Goyald5791042018-05-11 11:49:27 -040067static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
68module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
69MODULE_PARM_DESC(ovl_metacopy_def,
70 "Default to on or off for the metadata only copy up feature");
71
Miklos Szeredie9be9d52014-10-24 00:14:38 +020072static void ovl_dentry_release(struct dentry *dentry)
73{
74 struct ovl_entry *oe = dentry->d_fsdata;
75
76 if (oe) {
Miklos Szeredi4155c102017-11-10 09:39:15 +010077 ovl_entry_stack_free(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020078 kfree_rcu(oe, rcu);
79 }
80}
81
Miklos Szeredi2d902672016-06-30 08:53:27 +020082static struct dentry *ovl_d_real(struct dentry *dentry,
Miklos Szeredifb160432018-07-18 15:44:44 +020083 const struct inode *inode)
Miklos Szeredid101a122016-03-26 16:14:37 -040084{
85 struct dentry *real;
86
Miklos Szeredie8c985b2018-07-18 15:44:41 +020087 /* It's an overlay file */
88 if (inode && d_inode(dentry) == inode)
89 return dentry;
90
Miklos Szeredica4c8a32016-12-16 11:02:55 +010091 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040092 if (!inode || inode == d_inode(dentry))
93 return dentry;
94 goto bug;
95 }
96
97 real = ovl_dentry_upper(dentry);
Vivek Goyal2c3d7352018-05-11 11:49:31 -040098 if (real && (inode == d_inode(real)))
Miklos Szeredid101a122016-03-26 16:14:37 -040099 return real;
100
Vivek Goyal2c3d7352018-05-11 11:49:31 -0400101 if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
102 return real;
103
104 real = ovl_dentry_lowerdata(dentry);
Miklos Szeredid101a122016-03-26 16:14:37 -0400105 if (!real)
106 goto bug;
107
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100108 /* Handle recursion */
Miklos Szeredifb160432018-07-18 15:44:44 +0200109 real = d_real(real, inode);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100110
Miklos Szeredid101a122016-03-26 16:14:37 -0400111 if (!inode || inode == d_inode(real))
112 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400113bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200114 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400115 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
116 return dentry;
117}
118
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200119static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
120{
121 struct ovl_entry *oe = dentry->d_fsdata;
122 unsigned int i;
123 int ret = 1;
124
125 for (i = 0; i < oe->numlower; i++) {
126 struct dentry *d = oe->lowerstack[i].dentry;
127
128 if (d->d_flags & DCACHE_OP_REVALIDATE) {
129 ret = d->d_op->d_revalidate(d, flags);
130 if (ret < 0)
131 return ret;
132 if (!ret) {
133 if (!(flags & LOOKUP_RCU))
134 d_invalidate(d);
135 return -ESTALE;
136 }
137 }
138 }
139 return 1;
140}
141
142static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
143{
144 struct ovl_entry *oe = dentry->d_fsdata;
145 unsigned int i;
146 int ret = 1;
147
148 for (i = 0; i < oe->numlower; i++) {
149 struct dentry *d = oe->lowerstack[i].dentry;
150
151 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
152 ret = d->d_op->d_weak_revalidate(d, flags);
153 if (ret <= 0)
154 break;
155 }
156 }
157 return ret;
158}
159
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200160static const struct dentry_operations ovl_dentry_operations = {
161 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400162 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200163};
164
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200165static const struct dentry_operations ovl_reval_dentry_operations = {
166 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400167 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200168 .d_revalidate = ovl_dentry_revalidate,
169 .d_weak_revalidate = ovl_dentry_weak_revalidate,
170};
171
Amir Goldstein13cf1992017-06-12 09:54:40 +0300172static struct kmem_cache *ovl_inode_cachep;
173
174static struct inode *ovl_alloc_inode(struct super_block *sb)
175{
176 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
177
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900178 if (!oi)
179 return NULL;
180
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200181 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200182 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200183 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200184 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200185 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200186 oi->lower = NULL;
Vivek Goyal2664bd02018-05-11 11:49:30 -0400187 oi->lowerdata = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300188 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200189
Amir Goldstein13cf1992017-06-12 09:54:40 +0300190 return &oi->vfs_inode;
191}
192
193static void ovl_i_callback(struct rcu_head *head)
194{
195 struct inode *inode = container_of(head, struct inode, i_rcu);
196
197 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
198}
199
200static void ovl_destroy_inode(struct inode *inode)
201{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200202 struct ovl_inode *oi = OVL_I(inode);
203
204 dput(oi->__upperdentry);
Amir Goldstein31747ed2018-01-14 18:35:40 +0200205 iput(oi->lower);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400206 if (S_ISDIR(inode->i_mode))
207 ovl_dir_cache_free(inode);
208 else
209 iput(oi->lowerdata);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200210 kfree(oi->redirect);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300211 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200212
Amir Goldstein13cf1992017-06-12 09:54:40 +0300213 call_rcu(&inode->i_rcu, ovl_i_callback);
214}
215
Miklos Szerediad204482017-11-10 09:39:16 +0100216static void ovl_free_fs(struct ovl_fs *ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200217{
Miklos Szeredidd662662014-12-13 00:59:43 +0100218 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200219
Miklos Szerediad204482017-11-10 09:39:16 +0100220 dput(ofs->indexdir);
221 dput(ofs->workdir);
222 if (ofs->workdir_locked)
223 ovl_inuse_unlock(ofs->workbasedir);
224 dput(ofs->workbasedir);
225 if (ofs->upperdir_locked)
226 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
227 mntput(ofs->upper_mnt);
Amir Goldstein51486262018-03-28 20:22:41 +0300228 for (i = 0; i < ofs->numlower; i++)
Miklos Szerediad204482017-11-10 09:39:16 +0100229 mntput(ofs->lower_layers[i].mnt);
Amir Goldstein51486262018-03-28 20:22:41 +0300230 for (i = 0; i < ofs->numlowerfs; i++)
231 free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
Miklos Szerediad204482017-11-10 09:39:16 +0100232 kfree(ofs->lower_layers);
Amir Goldstein51486262018-03-28 20:22:41 +0300233 kfree(ofs->lower_fs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200234
Miklos Szerediad204482017-11-10 09:39:16 +0100235 kfree(ofs->config.lowerdir);
236 kfree(ofs->config.upperdir);
237 kfree(ofs->config.workdir);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100238 kfree(ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100239 if (ofs->creator_cred)
240 put_cred(ofs->creator_cred);
241 kfree(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242}
243
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100244static void ovl_put_super(struct super_block *sb)
245{
246 struct ovl_fs *ofs = sb->s_fs_info;
247
248 ovl_free_fs(ofs);
249}
250
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800251/* Sync real dirty inodes in upper filesystem (if it exists) */
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200252static int ovl_sync_fs(struct super_block *sb, int wait)
253{
Miklos Szerediad204482017-11-10 09:39:16 +0100254 struct ovl_fs *ofs = sb->s_fs_info;
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200255 struct super_block *upper_sb;
256 int ret;
257
Miklos Szerediad204482017-11-10 09:39:16 +0100258 if (!ofs->upper_mnt)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200259 return 0;
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800260
261 /*
262 * If this is a sync(2) call or an emergency sync, all the super blocks
263 * will be iterated, including upper_sb, so no need to do anything.
264 *
265 * If this is a syncfs(2) call, then we do need to call
266 * sync_filesystem() on upper_sb, but enough if we do it when being
267 * called with wait == 1.
268 */
269 if (!wait)
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200270 return 0;
271
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800272 upper_sb = ofs->upper_mnt->mnt_sb;
273
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200274 down_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800275 ret = sync_filesystem(upper_sb);
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200276 up_read(&upper_sb->s_umount);
Chengguang Xue8d4bfe2017-11-29 10:01:32 +0800277
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200278 return ret;
279}
280
Andy Whitcroftcc259632014-10-24 00:14:38 +0200281/**
282 * ovl_statfs
283 * @sb: The overlayfs super block
284 * @buf: The struct kstatfs to fill in with stats
285 *
286 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100287 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200288 */
289static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
290{
291 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
292 struct dentry *root_dentry = dentry->d_sb->s_root;
293 struct path path;
294 int err;
295
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100296 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200297
298 err = vfs_statfs(&path, buf);
299 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100300 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200301 buf->f_type = OVERLAYFS_SUPER_MAGIC;
302 }
303
304 return err;
305}
306
Amir Goldstein02bcd152017-06-21 15:28:36 +0300307/* Will this overlay be forced to mount/remount ro? */
Miklos Szerediad204482017-11-10 09:39:16 +0100308static bool ovl_force_readonly(struct ovl_fs *ofs)
Amir Goldstein02bcd152017-06-21 15:28:36 +0300309{
Miklos Szerediad204482017-11-10 09:39:16 +0100310 return (!ofs->upper_mnt || !ofs->workdir);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300311}
312
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100313static const char *ovl_redirect_mode_def(void)
314{
315 return ovl_redirect_dir_def ? "on" : "off";
316}
317
Amir Goldstein795939a2018-03-29 09:08:18 +0300318enum {
319 OVL_XINO_OFF,
320 OVL_XINO_AUTO,
321 OVL_XINO_ON,
322};
323
324static const char * const ovl_xino_str[] = {
325 "off",
326 "auto",
327 "on",
328};
329
330static inline int ovl_xino_def(void)
331{
332 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
333}
334
Erez Zadokf45827e82014-10-24 00:14:38 +0200335/**
336 * ovl_show_options
337 *
338 * Prints the mount options for a given superblock.
339 * Returns zero; does not fail.
340 */
341static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
342{
343 struct super_block *sb = dentry->d_sb;
Miklos Szerediad204482017-11-10 09:39:16 +0100344 struct ovl_fs *ofs = sb->s_fs_info;
Erez Zadokf45827e82014-10-24 00:14:38 +0200345
Miklos Szerediad204482017-11-10 09:39:16 +0100346 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
347 if (ofs->config.upperdir) {
348 seq_show_option(m, "upperdir", ofs->config.upperdir);
349 seq_show_option(m, "workdir", ofs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100350 }
Miklos Szerediad204482017-11-10 09:39:16 +0100351 if (ofs->config.default_permissions)
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200352 seq_puts(m, ",default_permissions");
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100353 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
354 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
Miklos Szerediad204482017-11-10 09:39:16 +0100355 if (ofs->config.index != ovl_index_def)
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100356 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
Amir Goldsteinf168f102018-01-19 11:26:53 +0200357 if (ofs->config.nfs_export != ovl_nfs_export_def)
358 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
359 "on" : "off");
Amir Goldstein795939a2018-03-29 09:08:18 +0300360 if (ofs->config.xino != ovl_xino_def())
361 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
Vivek Goyald5791042018-05-11 11:49:27 -0400362 if (ofs->config.metacopy != ovl_metacopy_def)
363 seq_printf(m, ",metacopy=%s",
364 ofs->config.metacopy ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200365 return 0;
366}
367
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900368static int ovl_remount(struct super_block *sb, int *flags, char *data)
369{
Miklos Szerediad204482017-11-10 09:39:16 +0100370 struct ovl_fs *ofs = sb->s_fs_info;
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900371
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800372 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900373 return -EROFS;
374
375 return 0;
376}
377
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200378static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300379 .alloc_inode = ovl_alloc_inode,
380 .destroy_inode = ovl_destroy_inode,
381 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200382 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200383 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200384 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200385 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900386 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200387};
388
389enum {
390 OPT_LOWERDIR,
391 OPT_UPPERDIR,
392 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200393 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100394 OPT_REDIRECT_DIR,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300395 OPT_INDEX_ON,
396 OPT_INDEX_OFF,
Amir Goldsteinf168f102018-01-19 11:26:53 +0200397 OPT_NFS_EXPORT_ON,
398 OPT_NFS_EXPORT_OFF,
Amir Goldstein795939a2018-03-29 09:08:18 +0300399 OPT_XINO_ON,
400 OPT_XINO_OFF,
401 OPT_XINO_AUTO,
Vivek Goyald5791042018-05-11 11:49:27 -0400402 OPT_METACOPY_ON,
403 OPT_METACOPY_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200404 OPT_ERR,
405};
406
407static const match_table_t ovl_tokens = {
408 {OPT_LOWERDIR, "lowerdir=%s"},
409 {OPT_UPPERDIR, "upperdir=%s"},
410 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200411 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100412 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300413 {OPT_INDEX_ON, "index=on"},
414 {OPT_INDEX_OFF, "index=off"},
Amir Goldsteinf168f102018-01-19 11:26:53 +0200415 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
416 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
Amir Goldstein795939a2018-03-29 09:08:18 +0300417 {OPT_XINO_ON, "xino=on"},
418 {OPT_XINO_OFF, "xino=off"},
419 {OPT_XINO_AUTO, "xino=auto"},
Vivek Goyald5791042018-05-11 11:49:27 -0400420 {OPT_METACOPY_ON, "metacopy=on"},
421 {OPT_METACOPY_OFF, "metacopy=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200422 {OPT_ERR, NULL}
423};
424
Miklos Szeredi91c77942014-11-20 16:40:00 +0100425static char *ovl_next_opt(char **s)
426{
427 char *sbegin = *s;
428 char *p;
429
430 if (sbegin == NULL)
431 return NULL;
432
433 for (p = sbegin; *p; p++) {
434 if (*p == '\\') {
435 p++;
436 if (!*p)
437 break;
438 } else if (*p == ',') {
439 *p = '\0';
440 *s = p + 1;
441 return sbegin;
442 }
443 }
444 *s = NULL;
445 return sbegin;
446}
447
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100448static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
449{
450 if (strcmp(mode, "on") == 0) {
451 config->redirect_dir = true;
452 /*
453 * Does not make sense to have redirect creation without
454 * redirect following.
455 */
456 config->redirect_follow = true;
457 } else if (strcmp(mode, "follow") == 0) {
458 config->redirect_follow = true;
459 } else if (strcmp(mode, "off") == 0) {
460 if (ovl_redirect_always_follow)
461 config->redirect_follow = true;
462 } else if (strcmp(mode, "nofollow") != 0) {
463 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
464 mode);
465 return -EINVAL;
466 }
467
468 return 0;
469}
470
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200471static int ovl_parse_opt(char *opt, struct ovl_config *config)
472{
473 char *p;
Vivek Goyald5791042018-05-11 11:49:27 -0400474 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200475
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100476 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
477 if (!config->redirect_mode)
478 return -ENOMEM;
479
Miklos Szeredi91c77942014-11-20 16:40:00 +0100480 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200481 int token;
482 substring_t args[MAX_OPT_ARGS];
483
484 if (!*p)
485 continue;
486
487 token = match_token(p, ovl_tokens, args);
488 switch (token) {
489 case OPT_UPPERDIR:
490 kfree(config->upperdir);
491 config->upperdir = match_strdup(&args[0]);
492 if (!config->upperdir)
493 return -ENOMEM;
494 break;
495
496 case OPT_LOWERDIR:
497 kfree(config->lowerdir);
498 config->lowerdir = match_strdup(&args[0]);
499 if (!config->lowerdir)
500 return -ENOMEM;
501 break;
502
503 case OPT_WORKDIR:
504 kfree(config->workdir);
505 config->workdir = match_strdup(&args[0]);
506 if (!config->workdir)
507 return -ENOMEM;
508 break;
509
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200510 case OPT_DEFAULT_PERMISSIONS:
511 config->default_permissions = true;
512 break;
513
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100514 case OPT_REDIRECT_DIR:
515 kfree(config->redirect_mode);
516 config->redirect_mode = match_strdup(&args[0]);
517 if (!config->redirect_mode)
518 return -ENOMEM;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100519 break;
520
Amir Goldstein02bcd152017-06-21 15:28:36 +0300521 case OPT_INDEX_ON:
522 config->index = true;
523 break;
524
525 case OPT_INDEX_OFF:
526 config->index = false;
527 break;
528
Amir Goldsteinf168f102018-01-19 11:26:53 +0200529 case OPT_NFS_EXPORT_ON:
530 config->nfs_export = true;
531 break;
532
533 case OPT_NFS_EXPORT_OFF:
534 config->nfs_export = false;
535 break;
536
Amir Goldstein795939a2018-03-29 09:08:18 +0300537 case OPT_XINO_ON:
538 config->xino = OVL_XINO_ON;
539 break;
540
541 case OPT_XINO_OFF:
542 config->xino = OVL_XINO_OFF;
543 break;
544
545 case OPT_XINO_AUTO:
546 config->xino = OVL_XINO_AUTO;
547 break;
548
Vivek Goyald5791042018-05-11 11:49:27 -0400549 case OPT_METACOPY_ON:
550 config->metacopy = true;
551 break;
552
553 case OPT_METACOPY_OFF:
554 config->metacopy = false;
555 break;
556
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200557 default:
hujianyangbead55e2015-01-15 13:17:36 +0800558 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200559 return -EINVAL;
560 }
561 }
hujianyang71cbad72015-01-15 13:20:57 +0800562
563 /* Workdir is useless in non-upper mount */
564 if (!config->upperdir && config->workdir) {
565 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
566 config->workdir);
567 kfree(config->workdir);
568 config->workdir = NULL;
569 }
570
Vivek Goyald5791042018-05-11 11:49:27 -0400571 err = ovl_parse_redirect_mode(config, config->redirect_mode);
572 if (err)
573 return err;
574
575 /* metacopy feature with upper requires redirect_dir=on */
576 if (config->upperdir && config->metacopy && !config->redirect_dir) {
577 pr_warn("overlayfs: metadata only copy up requires \"redirect_dir=on\", falling back to metacopy=off.\n");
578 config->metacopy = false;
579 } else if (config->metacopy && !config->redirect_follow) {
580 pr_warn("overlayfs: metadata only copy up requires \"redirect_dir=follow\" on non-upper mount, falling back to metacopy=off.\n");
581 config->metacopy = false;
582 }
583
584 return 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200585}
586
587#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300588#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200589
Miklos Szerediad204482017-11-10 09:39:16 +0100590static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300591 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200592{
Miklos Szerediad204482017-11-10 09:39:16 +0100593 struct inode *dir = ofs->workbasedir->d_inode;
594 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200595 struct dentry *work;
596 int err;
597 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300598 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200599
Al Viro59551022016-01-22 15:40:57 -0500600 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300601 locked = true;
602
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200603retry:
Miklos Szerediad204482017-11-10 09:39:16 +0100604 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200605
606 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200607 struct iattr attr = {
608 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000609 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200610 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200611
612 if (work->d_inode) {
613 err = -EEXIST;
614 if (retried)
615 goto out_dput;
616
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300617 if (persist)
618 goto out_unlock;
619
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200620 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200621 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200622 dput(work);
623 goto retry;
624 }
625
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300626 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
627 err = PTR_ERR(work);
628 if (IS_ERR(work))
629 goto out_err;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200630
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200631 /*
632 * Try to remove POSIX ACL xattrs from workdir. We are good if:
633 *
634 * a) success (there was a POSIX ACL xattr and was removed)
635 * b) -ENODATA (there was no POSIX ACL xattr)
636 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
637 *
638 * There are various other error values that could effectively
639 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
640 * if the xattr name is too long), but the set of filesystems
641 * allowed as upper are limited to "normal" ones, where checking
642 * for the above two errors is sufficient.
643 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200644 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200645 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200646 goto out_dput;
647
648 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200649 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200650 goto out_dput;
651
652 /* Clear any inherited mode bits */
653 inode_lock(work->d_inode);
654 err = notify_change(work, &attr, NULL);
655 inode_unlock(work->d_inode);
656 if (err)
657 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300658 } else {
659 err = PTR_ERR(work);
660 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200661 }
662out_unlock:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300663 if (locked)
664 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200665
666 return work;
667
668out_dput:
669 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300670out_err:
671 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
Miklos Szerediad204482017-11-10 09:39:16 +0100672 ofs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300673 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200674 goto out_unlock;
675}
676
Miklos Szeredi91c77942014-11-20 16:40:00 +0100677static void ovl_unescape(char *s)
678{
679 char *d = s;
680
681 for (;; s++, d++) {
682 if (*s == '\\')
683 s++;
684 *d = *s;
685 if (!*s)
686 break;
687 }
688}
689
Miklos Szerediab508822014-12-13 00:59:49 +0100690static int ovl_mount_dir_noesc(const char *name, struct path *path)
691{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100692 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100693
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100694 if (!*name) {
695 pr_err("overlayfs: empty lowerdir\n");
696 goto out;
697 }
Miklos Szerediab508822014-12-13 00:59:49 +0100698 err = kern_path(name, LOOKUP_FOLLOW, path);
699 if (err) {
700 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
701 goto out;
702 }
703 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200704 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100705 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
706 goto out_put;
707 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100708 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100709 pr_err("overlayfs: '%s' not a directory\n", name);
710 goto out_put;
711 }
712 return 0;
713
714out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100715 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100716out:
717 return err;
718}
719
720static int ovl_mount_dir(const char *name, struct path *path)
721{
722 int err = -ENOMEM;
723 char *tmp = kstrdup(name, GFP_KERNEL);
724
725 if (tmp) {
726 ovl_unescape(tmp);
727 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200728
729 if (!err)
730 if (ovl_dentry_remote(path->dentry)) {
731 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
732 tmp);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100733 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200734 err = -EINVAL;
735 }
Miklos Szerediab508822014-12-13 00:59:49 +0100736 kfree(tmp);
737 }
738 return err;
739}
740
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100741static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
742 const char *name)
743{
744 struct kstatfs statfs;
745 int err = vfs_statfs(path, &statfs);
746
747 if (err)
748 pr_err("overlayfs: statfs failed on '%s'\n", name);
749 else
750 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
751
752 return err;
753}
754
755static int ovl_lower_dir(const char *name, struct path *path,
756 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100757{
Amir Goldsteine487d882017-11-07 13:55:04 +0200758 int fh_type;
Miklos Szerediab508822014-12-13 00:59:49 +0100759 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100760
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100761 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100762 if (err)
763 goto out;
764
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100765 err = ovl_check_namelen(path, ofs, name);
766 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100767 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100768
Miklos Szerediab508822014-12-13 00:59:49 +0100769 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
770
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200771 if (ovl_dentry_remote(path->dentry))
772 *remote = true;
773
Amir Goldstein02bcd152017-06-21 15:28:36 +0300774 /*
Amir Goldsteinf168f102018-01-19 11:26:53 +0200775 * The inodes index feature and NFS export need to encode and decode
776 * file handles, so they require that all layers support them.
Amir Goldstein02bcd152017-06-21 15:28:36 +0300777 */
Amir Goldsteine487d882017-11-07 13:55:04 +0200778 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
Amir Goldsteinf168f102018-01-19 11:26:53 +0200779 if ((ofs->config.nfs_export ||
Amir Goldsteine487d882017-11-07 13:55:04 +0200780 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
Amir Goldstein02bcd152017-06-21 15:28:36 +0300781 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +0200782 ofs->config.nfs_export = false;
783 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
784 name);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300785 }
786
Amir Goldsteine487d882017-11-07 13:55:04 +0200787 /* Check if lower fs has 32bit inode numbers */
788 if (fh_type != FILEID_INO32_GEN)
789 ofs->xino_bits = 0;
790
Miklos Szerediab508822014-12-13 00:59:49 +0100791 return 0;
792
793out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100794 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100795out:
796 return err;
797}
798
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200799/* Workdir should not be subdir of upperdir and vice versa */
800static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
801{
802 bool ok = false;
803
804 if (workdir != upperdir) {
805 ok = (lock_rename(workdir, upperdir) == NULL);
806 unlock_rename(workdir, upperdir);
807 }
808 return ok;
809}
810
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100811static unsigned int ovl_split_lowerdirs(char *str)
812{
813 unsigned int ctr = 1;
814 char *s, *d;
815
816 for (s = d = str;; s++, d++) {
817 if (*s == '\\') {
818 s++;
819 } else if (*s == ':') {
820 *d = '\0';
821 ctr++;
822 continue;
823 }
824 *d = *s;
825 if (!*s)
826 break;
827 }
828 return ctr;
829}
830
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200831static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200832ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
833 struct dentry *dentry, struct inode *inode,
834 const char *name, void *buffer, size_t size)
835{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200836 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200837}
838
839static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200840ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
841 struct dentry *dentry, struct inode *inode,
842 const char *name, const void *value,
843 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200844{
845 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200846 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200847 struct posix_acl *acl = NULL;
848 int err;
849
850 /* Check that everything is OK before copy-up */
851 if (value) {
852 acl = posix_acl_from_xattr(&init_user_ns, value, size);
853 if (IS_ERR(acl))
854 return PTR_ERR(acl);
855 }
856 err = -EOPNOTSUPP;
857 if (!IS_POSIXACL(d_inode(workdir)))
858 goto out_acl_release;
859 if (!realinode->i_op->set_acl)
860 goto out_acl_release;
861 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
862 err = acl ? -EACCES : 0;
863 goto out_acl_release;
864 }
865 err = -EPERM;
866 if (!inode_owner_or_capable(inode))
867 goto out_acl_release;
868
869 posix_acl_release(acl);
870
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100871 /*
872 * Check if sgid bit needs to be cleared (actual setacl operation will
873 * be done with mounter's capabilities and so that won't do it for us).
874 */
875 if (unlikely(inode->i_mode & S_ISGID) &&
876 handler->flags == ACL_TYPE_ACCESS &&
877 !in_group_p(inode->i_gid) &&
878 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
879 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
880
881 err = ovl_setattr(dentry, &iattr);
882 if (err)
883 return err;
884 }
885
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200886 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200887 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200888 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200889
890 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200891
892out_acl_release:
893 posix_acl_release(acl);
894 return err;
895}
896
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200897static int ovl_own_xattr_get(const struct xattr_handler *handler,
898 struct dentry *dentry, struct inode *inode,
899 const char *name, void *buffer, size_t size)
900{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200901 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200902}
903
Miklos Szeredid837a492016-07-29 12:05:24 +0200904static int ovl_own_xattr_set(const struct xattr_handler *handler,
905 struct dentry *dentry, struct inode *inode,
906 const char *name, const void *value,
907 size_t size, int flags)
908{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200909 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200910}
911
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200912static int ovl_other_xattr_get(const struct xattr_handler *handler,
913 struct dentry *dentry, struct inode *inode,
914 const char *name, void *buffer, size_t size)
915{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200916 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200917}
918
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200919static int ovl_other_xattr_set(const struct xattr_handler *handler,
920 struct dentry *dentry, struct inode *inode,
921 const char *name, const void *value,
922 size_t size, int flags)
923{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200924 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200925}
926
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200927static const struct xattr_handler __maybe_unused
928ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200929 .name = XATTR_NAME_POSIX_ACL_ACCESS,
930 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200931 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200932 .set = ovl_posix_acl_xattr_set,
933};
934
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200935static const struct xattr_handler __maybe_unused
936ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200937 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
938 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200939 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200940 .set = ovl_posix_acl_xattr_set,
941};
942
943static const struct xattr_handler ovl_own_xattr_handler = {
944 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200945 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200946 .set = ovl_own_xattr_set,
947};
948
949static const struct xattr_handler ovl_other_xattr_handler = {
950 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200951 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200952 .set = ovl_other_xattr_set,
953};
954
955static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200956#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200957 &ovl_posix_acl_access_xattr_handler,
958 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200959#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200960 &ovl_own_xattr_handler,
961 &ovl_other_xattr_handler,
962 NULL
963};
964
Miklos Szerediad204482017-11-10 09:39:16 +0100965static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100966{
Miklos Szeredi50649752017-11-10 09:39:15 +0100967 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100968 int err;
969
Miklos Szerediad204482017-11-10 09:39:16 +0100970 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100971 if (err)
972 goto out;
973
974 /* Upper fs should not be r/o */
975 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
976 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
977 err = -EINVAL;
978 goto out;
979 }
980
Miklos Szerediad204482017-11-10 09:39:16 +0100981 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100982 if (err)
983 goto out;
984
985 err = -EBUSY;
986 if (ovl_inuse_trylock(upperpath->dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +0100987 ofs->upperdir_locked = true;
988 } else if (ofs->config.index) {
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100989 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
990 goto out;
991 } else {
992 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
993 }
Miklos Szeredi50649752017-11-10 09:39:15 +0100994
995 upper_mnt = clone_private_mount(upperpath);
996 err = PTR_ERR(upper_mnt);
997 if (IS_ERR(upper_mnt)) {
998 pr_err("overlayfs: failed to clone upperpath\n");
999 goto out;
1000 }
1001
1002 /* Don't inherit atime flags */
1003 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
Miklos Szerediad204482017-11-10 09:39:16 +01001004 ofs->upper_mnt = upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001005 err = 0;
1006out:
1007 return err;
1008}
1009
Miklos Szerediad204482017-11-10 09:39:16 +01001010static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001011{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001012 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001013 struct dentry *temp;
Amir Goldsteine487d882017-11-07 13:55:04 +02001014 int fh_type;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001015 int err;
1016
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001017 err = mnt_want_write(mnt);
1018 if (err)
1019 return err;
1020
Miklos Szerediad204482017-11-10 09:39:16 +01001021 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1022 if (!ofs->workdir)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001023 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001024
1025 /*
1026 * Upper should support d_type, else whiteouts are visible. Given
1027 * workdir and upper are on same fs, we can do iterate_dir() on
1028 * workdir. This check requires successful creation of workdir in
1029 * previous step.
1030 */
1031 err = ovl_check_d_type_supported(workpath);
1032 if (err < 0)
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001033 goto out;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001034
1035 /*
1036 * We allowed this configuration and don't want to break users over
1037 * kernel upgrade. So warn instead of erroring out.
1038 */
1039 if (!err)
1040 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1041
1042 /* Check if upper/work fs supports O_TMPFILE */
Miklos Szerediad204482017-11-10 09:39:16 +01001043 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1044 ofs->tmpfile = !IS_ERR(temp);
1045 if (ofs->tmpfile)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001046 dput(temp);
1047 else
1048 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1049
1050 /*
1051 * Check if upper/work fs supports trusted.overlay.* xattr
1052 */
Miklos Szerediad204482017-11-10 09:39:16 +01001053 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001054 if (err) {
Miklos Szerediad204482017-11-10 09:39:16 +01001055 ofs->noxattr = true;
Amir Goldsteina6837372017-09-19 12:14:18 +03001056 ofs->config.index = false;
Vivek Goyald5791042018-05-11 11:49:27 -04001057 ofs->config.metacopy = false;
1058 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001059 err = 0;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001060 } else {
Miklos Szerediad204482017-11-10 09:39:16 +01001061 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001062 }
1063
1064 /* Check if upper/work fs supports file handles */
Amir Goldsteine487d882017-11-07 13:55:04 +02001065 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1066 if (ofs->config.index && !fh_type) {
Miklos Szerediad204482017-11-10 09:39:16 +01001067 ofs->config.index = false;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001068 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1069 }
1070
Amir Goldsteine487d882017-11-07 13:55:04 +02001071 /* Check if upper fs has 32bit inode numbers */
1072 if (fh_type != FILEID_INO32_GEN)
1073 ofs->xino_bits = 0;
1074
Amir Goldsteinf168f102018-01-19 11:26:53 +02001075 /* NFS export of r/w mount depends on index */
1076 if (ofs->config.nfs_export && !ofs->config.index) {
1077 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1078 ofs->config.nfs_export = false;
1079 }
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001080out:
1081 mnt_drop_write(mnt);
1082 return err;
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001083}
1084
Miklos Szerediad204482017-11-10 09:39:16 +01001085static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001086{
1087 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001088 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001089
Miklos Szerediad204482017-11-10 09:39:16 +01001090 err = ovl_mount_dir(ofs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001091 if (err)
1092 goto out;
1093
1094 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001095 if (upperpath->mnt != workpath.mnt) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001096 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1097 goto out;
1098 }
Miklos Szeredibca44b52017-11-10 09:39:15 +01001099 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001100 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1101 goto out;
1102 }
1103
1104 err = -EBUSY;
Miklos Szeredibca44b52017-11-10 09:39:15 +01001105 if (ovl_inuse_trylock(workpath.dentry)) {
Miklos Szerediad204482017-11-10 09:39:16 +01001106 ofs->workdir_locked = true;
1107 } else if (ofs->config.index) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001108 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1109 goto out;
1110 } else {
1111 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1112 }
1113
Miklos Szerediad204482017-11-10 09:39:16 +01001114 ofs->workbasedir = dget(workpath.dentry);
1115 err = ovl_make_workdir(ofs, &workpath);
Miklos Szeredibca44b52017-11-10 09:39:15 +01001116 if (err)
1117 goto out;
1118
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001119 err = 0;
1120out:
Miklos Szeredibca44b52017-11-10 09:39:15 +01001121 path_put(&workpath);
1122
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001123 return err;
1124}
1125
Miklos Szerediad204482017-11-10 09:39:16 +01001126static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001127 struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001128{
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001129 struct vfsmount *mnt = ofs->upper_mnt;
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001130 int err;
1131
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001132 err = mnt_want_write(mnt);
1133 if (err)
1134 return err;
1135
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001136 /* Verify lower root is upper root origin */
Amir Goldsteind9768072017-09-24 13:00:19 +03001137 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
Amir Goldstein05122442018-01-11 08:25:32 +02001138 true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001139 if (err) {
1140 pr_err("overlayfs: failed to verify upper root origin\n");
1141 goto out;
1142 }
1143
Miklos Szerediad204482017-11-10 09:39:16 +01001144 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1145 if (ofs->indexdir) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001146 /*
1147 * Verify upper root is exclusively associated with index dir.
1148 * Older kernels stored upper fh in "trusted.overlay.origin"
1149 * xattr. If that xattr exists, verify that it is a match to
1150 * upper dir file handle. In any case, verify or set xattr
1151 * "trusted.overlay.upper" to indicate that index may have
1152 * directory entries.
1153 */
1154 if (ovl_check_origin_xattr(ofs->indexdir)) {
1155 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1156 upperpath->dentry, true, false);
1157 if (err)
1158 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1159 }
1160 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001161 if (err)
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001162 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001163
1164 /* Cleanup bad/stale/orphan index entries */
1165 if (!err)
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001166 err = ovl_indexdir_cleanup(ofs);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001167 }
Miklos Szerediad204482017-11-10 09:39:16 +01001168 if (err || !ofs->indexdir)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001169 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1170
1171out:
Amir Goldstein2ba9d572018-01-03 18:54:41 +02001172 mnt_drop_write(mnt);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +01001173 return err;
1174}
1175
Amir Goldstein51486262018-03-28 20:22:41 +03001176/* Get a unique fsid for the layer */
1177static int ovl_get_fsid(struct ovl_fs *ofs, struct super_block *sb)
1178{
1179 unsigned int i;
1180 dev_t dev;
1181 int err;
1182
1183 /* fsid 0 is reserved for upper fs even with non upper overlay */
1184 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1185 return 0;
1186
1187 for (i = 0; i < ofs->numlowerfs; i++) {
1188 if (ofs->lower_fs[i].sb == sb)
1189 return i + 1;
1190 }
1191
1192 err = get_anon_bdev(&dev);
1193 if (err) {
1194 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1195 return err;
1196 }
1197
1198 ofs->lower_fs[ofs->numlowerfs].sb = sb;
1199 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1200 ofs->numlowerfs++;
1201
1202 return ofs->numlowerfs;
1203}
1204
Miklos Szerediad204482017-11-10 09:39:16 +01001205static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack,
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001206 unsigned int numlower)
1207{
1208 int err;
1209 unsigned int i;
1210
1211 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001212 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001213 GFP_KERNEL);
Miklos Szerediad204482017-11-10 09:39:16 +01001214 if (ofs->lower_layers == NULL)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001215 goto out;
Amir Goldstein51486262018-03-28 20:22:41 +03001216
1217 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1218 GFP_KERNEL);
1219 if (ofs->lower_fs == NULL)
1220 goto out;
1221
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001222 for (i = 0; i < numlower; i++) {
1223 struct vfsmount *mnt;
Amir Goldstein51486262018-03-28 20:22:41 +03001224 int fsid;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001225
Amir Goldstein51486262018-03-28 20:22:41 +03001226 err = fsid = ovl_get_fsid(ofs, stack[i].mnt->mnt_sb);
1227 if (err < 0)
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001228 goto out;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001229
1230 mnt = clone_private_mount(&stack[i]);
1231 err = PTR_ERR(mnt);
1232 if (IS_ERR(mnt)) {
1233 pr_err("overlayfs: failed to clone lowerpath\n");
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001234 goto out;
1235 }
Amir Goldstein51486262018-03-28 20:22:41 +03001236
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001237 /*
1238 * Make lower layers R/O. That way fchmod/fchown on lower file
1239 * will fail instead of modifying lower fs.
1240 */
1241 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1242
Miklos Szerediad204482017-11-10 09:39:16 +01001243 ofs->lower_layers[ofs->numlower].mnt = mnt;
Amir Goldsteind583ed72017-11-08 19:23:36 +02001244 ofs->lower_layers[ofs->numlower].idx = i + 1;
Amir Goldstein51486262018-03-28 20:22:41 +03001245 ofs->lower_layers[ofs->numlower].fsid = fsid;
1246 if (fsid) {
1247 ofs->lower_layers[ofs->numlower].fs =
1248 &ofs->lower_fs[fsid - 1];
1249 }
Miklos Szerediad204482017-11-10 09:39:16 +01001250 ofs->numlower++;
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001251 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001252
Amir Goldstein795939a2018-03-29 09:08:18 +03001253 /*
1254 * When all layers on same fs, overlay can use real inode numbers.
1255 * With mount option "xino=on", mounter declares that there are enough
1256 * free high bits in underlying fs to hold the unique fsid.
1257 * If overlayfs does encounter underlying inodes using the high xino
1258 * bits reserved for fsid, it emits a warning and uses the original
1259 * inode number.
1260 */
1261 if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
Amir Goldsteine487d882017-11-07 13:55:04 +02001262 ofs->xino_bits = 0;
Amir Goldstein795939a2018-03-29 09:08:18 +03001263 ofs->config.xino = OVL_XINO_OFF;
1264 } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1265 /*
1266 * This is a roundup of number of bits needed for numlowerfs+1
1267 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
1268 * upper fs even with non upper overlay.
1269 */
1270 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1271 ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1272 }
1273
1274 if (ofs->xino_bits) {
1275 pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1276 ofs->xino_bits);
1277 }
Amir Goldsteine487d882017-11-07 13:55:04 +02001278
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001279 err = 0;
1280out:
1281 return err;
1282}
1283
Miklos Szeredi4155c102017-11-10 09:39:15 +01001284static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
Miklos Szerediad204482017-11-10 09:39:16 +01001285 struct ovl_fs *ofs)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001286{
1287 int err;
1288 char *lowertmp, *lower;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001289 struct path *stack = NULL;
1290 unsigned int stacklen, numlower = 0, i;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001291 bool remote = false;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001292 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001293
1294 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001295 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001296 if (!lowertmp)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001297 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001298
1299 err = -EINVAL;
1300 stacklen = ovl_split_lowerdirs(lowertmp);
1301 if (stacklen > OVL_MAX_STACK) {
1302 pr_err("overlayfs: too many lower directories, limit is %d\n",
1303 OVL_MAX_STACK);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001304 goto out_err;
Miklos Szerediad204482017-11-10 09:39:16 +01001305 } else if (!ofs->config.upperdir && stacklen == 1) {
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001306 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001307 goto out_err;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001308 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1309 ofs->config.redirect_follow) {
1310 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1311 ofs->config.nfs_export = false;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001312 }
1313
1314 err = -ENOMEM;
1315 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1316 if (!stack)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001317 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001318
1319 err = -EINVAL;
1320 lower = lowertmp;
1321 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szerediad204482017-11-10 09:39:16 +01001322 err = ovl_lower_dir(lower, &stack[numlower], ofs,
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001323 &sb->s_stack_depth, &remote);
1324 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001325 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001326
1327 lower = strchr(lower, '\0') + 1;
1328 }
1329
1330 err = -EINVAL;
1331 sb->s_stack_depth++;
1332 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1333 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001334 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001335 }
1336
Miklos Szerediad204482017-11-10 09:39:16 +01001337 err = ovl_get_lower_layers(ofs, stack, numlower);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001338 if (err)
1339 goto out_err;
1340
1341 err = -ENOMEM;
1342 oe = ovl_alloc_entry(numlower);
1343 if (!oe)
1344 goto out_err;
1345
1346 for (i = 0; i < numlower; i++) {
1347 oe->lowerstack[i].dentry = dget(stack[i].dentry);
Miklos Szerediad204482017-11-10 09:39:16 +01001348 oe->lowerstack[i].layer = &ofs->lower_layers[i];
Miklos Szeredi4155c102017-11-10 09:39:15 +01001349 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001350
1351 if (remote)
1352 sb->s_d_op = &ovl_reval_dentry_operations;
1353 else
1354 sb->s_d_op = &ovl_dentry_operations;
1355
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001356out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001357 for (i = 0; i < numlower; i++)
1358 path_put(&stack[i]);
1359 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001360 kfree(lowertmp);
1361
1362 return oe;
1363
1364out_err:
1365 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001366 goto out;
1367}
1368
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001369static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1370{
Kees Cook33006cd2017-03-29 14:02:19 -07001371 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001372 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001373 struct ovl_entry *oe;
Miklos Szerediad204482017-11-10 09:39:16 +01001374 struct ovl_fs *ofs;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001375 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001376 int err;
1377
Erez Zadokf45827e82014-10-24 00:14:38 +02001378 err = -ENOMEM;
Miklos Szerediad204482017-11-10 09:39:16 +01001379 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1380 if (!ofs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001381 goto out;
1382
Miklos Szerediad204482017-11-10 09:39:16 +01001383 ofs->creator_cred = cred = prepare_creds();
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001384 if (!cred)
1385 goto out_err;
1386
Miklos Szerediad204482017-11-10 09:39:16 +01001387 ofs->config.index = ovl_index_def;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001388 ofs->config.nfs_export = ovl_nfs_export_def;
Amir Goldstein795939a2018-03-29 09:08:18 +03001389 ofs->config.xino = ovl_xino_def();
Vivek Goyald5791042018-05-11 11:49:27 -04001390 ofs->config.metacopy = ovl_metacopy_def;
Miklos Szerediad204482017-11-10 09:39:16 +01001391 err = ovl_parse_opt((char *) data, &ofs->config);
Erez Zadokf45827e82014-10-24 00:14:38 +02001392 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001393 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001394
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001395 err = -EINVAL;
Miklos Szerediad204482017-11-10 09:39:16 +01001396 if (!ofs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001397 if (!silent)
1398 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001399 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001400 }
1401
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001402 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001403 sb->s_maxbytes = MAX_LFS_FILESIZE;
Amir Goldsteine487d882017-11-07 13:55:04 +02001404 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
Amir Goldstein795939a2018-03-29 09:08:18 +03001405 if (ofs->config.xino != OVL_XINO_OFF)
1406 ofs->xino_bits = BITS_PER_LONG - 32;
1407
Miklos Szerediad204482017-11-10 09:39:16 +01001408 if (ofs->config.upperdir) {
1409 if (!ofs->config.workdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001410 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001411 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001412 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001413
Miklos Szerediad204482017-11-10 09:39:16 +01001414 err = ovl_get_upper(ofs, &upperpath);
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001415 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001416 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001417
Miklos Szerediad204482017-11-10 09:39:16 +01001418 err = ovl_get_workdir(ofs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001419 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001420 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001421
Miklos Szerediad204482017-11-10 09:39:16 +01001422 if (!ofs->workdir)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001423 sb->s_flags |= SB_RDONLY;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001424
Miklos Szerediad204482017-11-10 09:39:16 +01001425 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1426 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001427
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001428 }
Miklos Szerediad204482017-11-10 09:39:16 +01001429 oe = ovl_get_lowerstack(sb, ofs);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001430 err = PTR_ERR(oe);
1431 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001432 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001433
hujianyang71cbad72015-01-15 13:20:57 +08001434 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
Miklos Szerediad204482017-11-10 09:39:16 +01001435 if (!ofs->upper_mnt)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001436 sb->s_flags |= SB_RDONLY;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001437
Miklos Szerediad204482017-11-10 09:39:16 +01001438 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1439 err = ovl_get_indexdir(ofs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001440 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001441 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001442
Amir Goldstein972d0092017-09-19 12:14:18 +03001443 /* Force r/o mount with no index dir */
1444 if (!ofs->indexdir) {
1445 dput(ofs->workdir);
1446 ofs->workdir = NULL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001447 sb->s_flags |= SB_RDONLY;
Amir Goldstein972d0092017-09-19 12:14:18 +03001448 }
1449
Amir Goldstein02bcd152017-06-21 15:28:36 +03001450 }
1451
Amir Goldstein972d0092017-09-19 12:14:18 +03001452 /* Show index=off in /proc/mounts for forced r/o mount */
Amir Goldsteinf168f102018-01-19 11:26:53 +02001453 if (!ofs->indexdir) {
Miklos Szerediad204482017-11-10 09:39:16 +01001454 ofs->config.index = false;
Amir Goldsteinf168f102018-01-19 11:26:53 +02001455 if (ofs->upper_mnt && ofs->config.nfs_export) {
1456 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1457 ofs->config.nfs_export = false;
1458 }
1459 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001460
Vivek Goyald5791042018-05-11 11:49:27 -04001461 if (ofs->config.metacopy && ofs->config.nfs_export) {
1462 pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1463 ofs->config.nfs_export = false;
1464 }
1465
Amir Goldstein8383f172017-10-02 11:31:42 +03001466 if (ofs->config.nfs_export)
1467 sb->s_export_op = &ovl_export_operations;
1468
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001469 /* Never override disk quota limits or use reserved space */
1470 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1471
Vivek Goyal655042c2016-10-14 03:03:36 +02001472 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1473 sb->s_op = &ovl_super_operations;
1474 sb->s_xattr = ovl_xattr_handlers;
Miklos Szerediad204482017-11-10 09:39:16 +01001475 sb->s_fs_info = ofs;
Miklos Szeredide2a4a52018-07-18 15:44:43 +02001476 sb->s_flags |= SB_POSIXACL;
Vivek Goyal655042c2016-10-14 03:03:36 +02001477
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001478 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001479 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001480 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001481 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001482
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001483 root_dentry->d_fsdata = oe;
1484
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001485 mntput(upperpath.mnt);
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001486 if (upperpath.dentry) {
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001487 ovl_dentry_set_upper_alias(root_dentry);
Miklos Szeredi13c72072017-07-04 22:03:16 +02001488 if (ovl_is_impuredir(upperpath.dentry))
1489 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001490 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001491
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001492 /* Root is always merge -> can have whiteouts */
1493 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Amir Goldstein2ca3c142018-01-30 13:31:09 +02001494 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
Vivek Goyal0c288872018-05-11 11:49:28 -04001495 ovl_set_upperdata(d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001496 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
Vivek Goyal2664bd02018-05-11 11:49:30 -04001497 ovl_dentry_lower(root_dentry), NULL);
Miklos Szeredied06e062015-12-09 16:11:59 +01001498
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001499 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001500
1501 return 0;
1502
Miklos Szeredi4155c102017-11-10 09:39:15 +01001503out_free_oe:
1504 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001505 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001506out_err:
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001507 path_put(&upperpath);
Miklos Szerediad204482017-11-10 09:39:16 +01001508 ovl_free_fs(ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001509out:
1510 return err;
1511}
1512
1513static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1514 const char *dev_name, void *raw_data)
1515{
1516 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1517}
1518
1519static struct file_system_type ovl_fs_type = {
1520 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001521 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001522 .mount = ovl_mount,
1523 .kill_sb = kill_anon_super,
1524};
Miklos Szeredief94b182014-11-20 16:39:59 +01001525MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001526
Amir Goldstein13cf1992017-06-12 09:54:40 +03001527static void ovl_inode_init_once(void *foo)
1528{
1529 struct ovl_inode *oi = foo;
1530
1531 inode_init_once(&oi->vfs_inode);
1532}
1533
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001534static int __init ovl_init(void)
1535{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001536 int err;
1537
1538 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1539 sizeof(struct ovl_inode), 0,
1540 (SLAB_RECLAIM_ACCOUNT|
1541 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1542 ovl_inode_init_once);
1543 if (ovl_inode_cachep == NULL)
1544 return -ENOMEM;
1545
1546 err = register_filesystem(&ovl_fs_type);
1547 if (err)
1548 kmem_cache_destroy(ovl_inode_cachep);
1549
1550 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001551}
1552
1553static void __exit ovl_exit(void)
1554{
1555 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001556
1557 /*
1558 * Make sure all delayed rcu free inodes are flushed before we
1559 * destroy cache.
1560 */
1561 rcu_barrier();
1562 kmem_cache_destroy(ovl_inode_cachep);
1563
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001564}
1565
1566module_init(ovl_init);
1567module_exit(ovl_exit);