blob: 092d150643c12061407be0ecab554a38fe88d9fc [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>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include "overlayfs.h"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010021#include "ovl_entry.h"
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022
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
Amir Goldstein02bcd152017-06-21 15:28:36 +030037static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
38module_param_named(index, ovl_index_def, bool, 0644);
39MODULE_PARM_DESC(ovl_index_def,
40 "Default to on or off for the inodes index feature");
41
Miklos Szeredie9be9d52014-10-24 00:14:38 +020042static void ovl_dentry_release(struct dentry *dentry)
43{
44 struct ovl_entry *oe = dentry->d_fsdata;
45
46 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +010047 unsigned int i;
48
Miklos Szeredidd662662014-12-13 00:59:43 +010049 for (i = 0; i < oe->numlower; i++)
50 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020051 kfree_rcu(oe, rcu);
52 }
53}
54
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030055static int ovl_check_append_only(struct inode *inode, int flag)
56{
57 /*
58 * This test was moot in vfs may_open() because overlay inode does
59 * not have the S_APPEND flag, so re-check on real upper inode
60 */
61 if (IS_APPEND(inode)) {
62 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
63 return -EPERM;
64 if (flag & O_TRUNC)
65 return -EPERM;
66 }
67
68 return 0;
69}
70
Miklos Szeredi2d902672016-06-30 08:53:27 +020071static struct dentry *ovl_d_real(struct dentry *dentry,
72 const struct inode *inode,
Miklos Szeredi495e6422017-09-04 21:42:22 +020073 unsigned int open_flags, unsigned int flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040074{
75 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030076 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040077
Miklos Szeredicd913042017-09-05 12:53:11 +020078 if (flags & D_REAL_UPPER)
79 return ovl_dentry_upper(dentry);
80
Miklos Szeredica4c8a32016-12-16 11:02:55 +010081 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040082 if (!inode || inode == d_inode(dentry))
83 return dentry;
84 goto bug;
85 }
86
Miklos Szeredi2d902672016-06-30 08:53:27 +020087 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030088 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +020089 if (err)
90 return ERR_PTR(err);
91 }
92
Miklos Szeredid101a122016-03-26 16:14:37 -040093 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030094 if (real && (!inode || inode == d_inode(real))) {
95 if (!inode) {
96 err = ovl_check_append_only(d_inode(real), open_flags);
97 if (err)
98 return ERR_PTR(err);
99 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400100 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300101 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400102
103 real = ovl_dentry_lower(dentry);
104 if (!real)
105 goto bug;
106
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100107 /* Handle recursion */
Miklos Szeredi495e6422017-09-04 21:42:22 +0200108 real = d_real(real, inode, open_flags, 0);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100109
Miklos Szeredid101a122016-03-26 16:14:37 -0400110 if (!inode || inode == d_inode(real))
111 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400112bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200113 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400114 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
115 return dentry;
116}
117
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200118static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
119{
120 struct ovl_entry *oe = dentry->d_fsdata;
121 unsigned int i;
122 int ret = 1;
123
124 for (i = 0; i < oe->numlower; i++) {
125 struct dentry *d = oe->lowerstack[i].dentry;
126
127 if (d->d_flags & DCACHE_OP_REVALIDATE) {
128 ret = d->d_op->d_revalidate(d, flags);
129 if (ret < 0)
130 return ret;
131 if (!ret) {
132 if (!(flags & LOOKUP_RCU))
133 d_invalidate(d);
134 return -ESTALE;
135 }
136 }
137 }
138 return 1;
139}
140
141static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
142{
143 struct ovl_entry *oe = dentry->d_fsdata;
144 unsigned int i;
145 int ret = 1;
146
147 for (i = 0; i < oe->numlower; i++) {
148 struct dentry *d = oe->lowerstack[i].dentry;
149
150 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
151 ret = d->d_op->d_weak_revalidate(d, flags);
152 if (ret <= 0)
153 break;
154 }
155 }
156 return ret;
157}
158
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200159static const struct dentry_operations ovl_dentry_operations = {
160 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400161 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200162};
163
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200164static const struct dentry_operations ovl_reval_dentry_operations = {
165 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400166 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200167 .d_revalidate = ovl_dentry_revalidate,
168 .d_weak_revalidate = ovl_dentry_weak_revalidate,
169};
170
Amir Goldstein13cf1992017-06-12 09:54:40 +0300171static struct kmem_cache *ovl_inode_cachep;
172
173static struct inode *ovl_alloc_inode(struct super_block *sb)
174{
175 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
176
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200177 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200178 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200179 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200180 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200181 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200182 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300183 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200184
Amir Goldstein13cf1992017-06-12 09:54:40 +0300185 return &oi->vfs_inode;
186}
187
188static void ovl_i_callback(struct rcu_head *head)
189{
190 struct inode *inode = container_of(head, struct inode, i_rcu);
191
192 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
193}
194
195static void ovl_destroy_inode(struct inode *inode)
196{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200197 struct ovl_inode *oi = OVL_I(inode);
198
199 dput(oi->__upperdentry);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200200 kfree(oi->redirect);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200201 ovl_dir_cache_free(inode);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300202 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200203
Amir Goldstein13cf1992017-06-12 09:54:40 +0300204 call_rcu(&inode->i_rcu, ovl_i_callback);
205}
206
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207static void ovl_put_super(struct super_block *sb)
208{
209 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100210 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200211
Amir Goldstein02bcd152017-06-21 15:28:36 +0300212 dput(ufs->indexdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200213 dput(ufs->workdir);
Amir Goldstein85fdee12017-09-29 10:21:21 +0300214 if (ufs->workdir_locked)
215 ovl_inuse_unlock(ufs->workbasedir);
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300216 dput(ufs->workbasedir);
Amir Goldstein85fdee12017-09-29 10:21:21 +0300217 if (ufs->upper_mnt && ufs->upperdir_locked)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300218 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200219 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100220 for (i = 0; i < ufs->numlower; i++)
221 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300222 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200223
Erez Zadokf45827e82014-10-24 00:14:38 +0200224 kfree(ufs->config.lowerdir);
225 kfree(ufs->config.upperdir);
226 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200227 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200228 kfree(ufs);
229}
230
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200231static int ovl_sync_fs(struct super_block *sb, int wait)
232{
233 struct ovl_fs *ufs = sb->s_fs_info;
234 struct super_block *upper_sb;
235 int ret;
236
237 if (!ufs->upper_mnt)
238 return 0;
239 upper_sb = ufs->upper_mnt->mnt_sb;
240 if (!upper_sb->s_op->sync_fs)
241 return 0;
242
243 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
244 down_read(&upper_sb->s_umount);
245 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
246 up_read(&upper_sb->s_umount);
247 return ret;
248}
249
Andy Whitcroftcc259632014-10-24 00:14:38 +0200250/**
251 * ovl_statfs
252 * @sb: The overlayfs super block
253 * @buf: The struct kstatfs to fill in with stats
254 *
255 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100256 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200257 */
258static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
259{
260 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
261 struct dentry *root_dentry = dentry->d_sb->s_root;
262 struct path path;
263 int err;
264
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100265 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200266
267 err = vfs_statfs(&path, buf);
268 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100269 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200270 buf->f_type = OVERLAYFS_SUPER_MAGIC;
271 }
272
273 return err;
274}
275
Amir Goldstein02bcd152017-06-21 15:28:36 +0300276/* Will this overlay be forced to mount/remount ro? */
277static bool ovl_force_readonly(struct ovl_fs *ufs)
278{
279 return (!ufs->upper_mnt || !ufs->workdir);
280}
281
Erez Zadokf45827e82014-10-24 00:14:38 +0200282/**
283 * ovl_show_options
284 *
285 * Prints the mount options for a given superblock.
286 * Returns zero; does not fail.
287 */
288static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
289{
290 struct super_block *sb = dentry->d_sb;
291 struct ovl_fs *ufs = sb->s_fs_info;
292
Kees Cooka068acf2015-09-04 15:44:57 -0700293 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100294 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700295 seq_show_option(m, "upperdir", ufs->config.upperdir);
296 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100297 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200298 if (ufs->config.default_permissions)
299 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200300 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
301 seq_printf(m, ",redirect_dir=%s",
302 ufs->config.redirect_dir ? "on" : "off");
Amir Goldstein02bcd152017-06-21 15:28:36 +0300303 if (ufs->config.index != ovl_index_def)
304 seq_printf(m, ",index=%s",
305 ufs->config.index ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200306 return 0;
307}
308
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900309static int ovl_remount(struct super_block *sb, int *flags, char *data)
310{
311 struct ovl_fs *ufs = sb->s_fs_info;
312
Amir Goldstein02bcd152017-06-21 15:28:36 +0300313 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900314 return -EROFS;
315
316 return 0;
317}
318
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200319static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300320 .alloc_inode = ovl_alloc_inode,
321 .destroy_inode = ovl_destroy_inode,
322 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200323 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200324 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200325 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200326 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900327 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328};
329
330enum {
331 OPT_LOWERDIR,
332 OPT_UPPERDIR,
333 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200334 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100335 OPT_REDIRECT_DIR_ON,
336 OPT_REDIRECT_DIR_OFF,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300337 OPT_INDEX_ON,
338 OPT_INDEX_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339 OPT_ERR,
340};
341
342static const match_table_t ovl_tokens = {
343 {OPT_LOWERDIR, "lowerdir=%s"},
344 {OPT_UPPERDIR, "upperdir=%s"},
345 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200346 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100347 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
348 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300349 {OPT_INDEX_ON, "index=on"},
350 {OPT_INDEX_OFF, "index=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351 {OPT_ERR, NULL}
352};
353
Miklos Szeredi91c77942014-11-20 16:40:00 +0100354static char *ovl_next_opt(char **s)
355{
356 char *sbegin = *s;
357 char *p;
358
359 if (sbegin == NULL)
360 return NULL;
361
362 for (p = sbegin; *p; p++) {
363 if (*p == '\\') {
364 p++;
365 if (!*p)
366 break;
367 } else if (*p == ',') {
368 *p = '\0';
369 *s = p + 1;
370 return sbegin;
371 }
372 }
373 *s = NULL;
374 return sbegin;
375}
376
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200377static int ovl_parse_opt(char *opt, struct ovl_config *config)
378{
379 char *p;
380
Miklos Szeredi91c77942014-11-20 16:40:00 +0100381 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200382 int token;
383 substring_t args[MAX_OPT_ARGS];
384
385 if (!*p)
386 continue;
387
388 token = match_token(p, ovl_tokens, args);
389 switch (token) {
390 case OPT_UPPERDIR:
391 kfree(config->upperdir);
392 config->upperdir = match_strdup(&args[0]);
393 if (!config->upperdir)
394 return -ENOMEM;
395 break;
396
397 case OPT_LOWERDIR:
398 kfree(config->lowerdir);
399 config->lowerdir = match_strdup(&args[0]);
400 if (!config->lowerdir)
401 return -ENOMEM;
402 break;
403
404 case OPT_WORKDIR:
405 kfree(config->workdir);
406 config->workdir = match_strdup(&args[0]);
407 if (!config->workdir)
408 return -ENOMEM;
409 break;
410
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200411 case OPT_DEFAULT_PERMISSIONS:
412 config->default_permissions = true;
413 break;
414
Miklos Szeredia6c60652016-12-16 11:02:56 +0100415 case OPT_REDIRECT_DIR_ON:
416 config->redirect_dir = true;
417 break;
418
419 case OPT_REDIRECT_DIR_OFF:
420 config->redirect_dir = false;
421 break;
422
Amir Goldstein02bcd152017-06-21 15:28:36 +0300423 case OPT_INDEX_ON:
424 config->index = true;
425 break;
426
427 case OPT_INDEX_OFF:
428 config->index = false;
429 break;
430
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200431 default:
hujianyangbead55e2015-01-15 13:17:36 +0800432 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200433 return -EINVAL;
434 }
435 }
hujianyang71cbad72015-01-15 13:20:57 +0800436
437 /* Workdir is useless in non-upper mount */
438 if (!config->upperdir && config->workdir) {
439 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
440 config->workdir);
441 kfree(config->workdir);
442 config->workdir = NULL;
443 }
444
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445 return 0;
446}
447
448#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300449#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200450
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300451static struct dentry *ovl_workdir_create(struct super_block *sb,
452 struct ovl_fs *ufs,
453 struct dentry *dentry,
454 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200455{
456 struct inode *dir = dentry->d_inode;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300457 struct vfsmount *mnt = ufs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200458 struct dentry *work;
459 int err;
460 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300461 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200462
463 err = mnt_want_write(mnt);
464 if (err)
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300465 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200466
Al Viro59551022016-01-22 15:40:57 -0500467 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300468 locked = true;
469
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200470retry:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300471 work = lookup_one_len(name, dentry, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200472
473 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200474 struct iattr attr = {
475 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000476 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200477 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200478
479 if (work->d_inode) {
480 err = -EEXIST;
481 if (retried)
482 goto out_dput;
483
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300484 if (persist)
485 goto out_unlock;
486
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200487 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200488 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200489 dput(work);
490 goto retry;
491 }
492
Al Viro32a3d842016-12-04 17:33:17 +0000493 err = ovl_create_real(dir, work,
494 &(struct cattr){.mode = S_IFDIR | 0},
495 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200496 if (err)
497 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200498
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200499 /*
500 * Try to remove POSIX ACL xattrs from workdir. We are good if:
501 *
502 * a) success (there was a POSIX ACL xattr and was removed)
503 * b) -ENODATA (there was no POSIX ACL xattr)
504 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
505 *
506 * There are various other error values that could effectively
507 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
508 * if the xattr name is too long), but the set of filesystems
509 * allowed as upper are limited to "normal" ones, where checking
510 * for the above two errors is sufficient.
511 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200512 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200513 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200514 goto out_dput;
515
516 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200517 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200518 goto out_dput;
519
520 /* Clear any inherited mode bits */
521 inode_lock(work->d_inode);
522 err = notify_change(work, &attr, NULL);
523 inode_unlock(work->d_inode);
524 if (err)
525 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300526 } else {
527 err = PTR_ERR(work);
528 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200529 }
530out_unlock:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200531 mnt_drop_write(mnt);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300532 if (locked)
533 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200534
535 return work;
536
537out_dput:
538 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300539out_err:
540 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
541 ufs->config.workdir, name, -err);
542 sb->s_flags |= MS_RDONLY;
543 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544 goto out_unlock;
545}
546
Miklos Szeredi91c77942014-11-20 16:40:00 +0100547static void ovl_unescape(char *s)
548{
549 char *d = s;
550
551 for (;; s++, d++) {
552 if (*s == '\\')
553 s++;
554 *d = *s;
555 if (!*s)
556 break;
557 }
558}
559
Miklos Szerediab508822014-12-13 00:59:49 +0100560static int ovl_mount_dir_noesc(const char *name, struct path *path)
561{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100562 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100563
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100564 if (!*name) {
565 pr_err("overlayfs: empty lowerdir\n");
566 goto out;
567 }
Miklos Szerediab508822014-12-13 00:59:49 +0100568 err = kern_path(name, LOOKUP_FOLLOW, path);
569 if (err) {
570 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
571 goto out;
572 }
573 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200574 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100575 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
576 goto out_put;
577 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100578 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100579 pr_err("overlayfs: '%s' not a directory\n", name);
580 goto out_put;
581 }
582 return 0;
583
584out_put:
585 path_put(path);
586out:
587 return err;
588}
589
590static int ovl_mount_dir(const char *name, struct path *path)
591{
592 int err = -ENOMEM;
593 char *tmp = kstrdup(name, GFP_KERNEL);
594
595 if (tmp) {
596 ovl_unescape(tmp);
597 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200598
599 if (!err)
600 if (ovl_dentry_remote(path->dentry)) {
601 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
602 tmp);
603 path_put(path);
604 err = -EINVAL;
605 }
Miklos Szerediab508822014-12-13 00:59:49 +0100606 kfree(tmp);
607 }
608 return err;
609}
610
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100611static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
612 const char *name)
613{
614 struct kstatfs statfs;
615 int err = vfs_statfs(path, &statfs);
616
617 if (err)
618 pr_err("overlayfs: statfs failed on '%s'\n", name);
619 else
620 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
621
622 return err;
623}
624
625static int ovl_lower_dir(const char *name, struct path *path,
626 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100627{
628 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100629
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100630 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100631 if (err)
632 goto out;
633
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100634 err = ovl_check_namelen(path, ofs, name);
635 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100636 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100637
Miklos Szerediab508822014-12-13 00:59:49 +0100638 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
639
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200640 if (ovl_dentry_remote(path->dentry))
641 *remote = true;
642
Amir Goldstein02bcd152017-06-21 15:28:36 +0300643 /*
644 * The inodes index feature needs to encode and decode file
645 * handles, so it requires that all layers support them.
646 */
647 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
648 ofs->config.index = false;
649 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
650 }
651
Miklos Szerediab508822014-12-13 00:59:49 +0100652 return 0;
653
654out_put:
655 path_put(path);
656out:
657 return err;
658}
659
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200660/* Workdir should not be subdir of upperdir and vice versa */
661static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
662{
663 bool ok = false;
664
665 if (workdir != upperdir) {
666 ok = (lock_rename(workdir, upperdir) == NULL);
667 unlock_rename(workdir, upperdir);
668 }
669 return ok;
670}
671
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100672static unsigned int ovl_split_lowerdirs(char *str)
673{
674 unsigned int ctr = 1;
675 char *s, *d;
676
677 for (s = d = str;; s++, d++) {
678 if (*s == '\\') {
679 s++;
680 } else if (*s == ':') {
681 *d = '\0';
682 ctr++;
683 continue;
684 }
685 *d = *s;
686 if (!*s)
687 break;
688 }
689 return ctr;
690}
691
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200692static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200693ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
694 struct dentry *dentry, struct inode *inode,
695 const char *name, void *buffer, size_t size)
696{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200697 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200698}
699
700static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200701ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
702 struct dentry *dentry, struct inode *inode,
703 const char *name, const void *value,
704 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200705{
706 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200707 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200708 struct posix_acl *acl = NULL;
709 int err;
710
711 /* Check that everything is OK before copy-up */
712 if (value) {
713 acl = posix_acl_from_xattr(&init_user_ns, value, size);
714 if (IS_ERR(acl))
715 return PTR_ERR(acl);
716 }
717 err = -EOPNOTSUPP;
718 if (!IS_POSIXACL(d_inode(workdir)))
719 goto out_acl_release;
720 if (!realinode->i_op->set_acl)
721 goto out_acl_release;
722 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
723 err = acl ? -EACCES : 0;
724 goto out_acl_release;
725 }
726 err = -EPERM;
727 if (!inode_owner_or_capable(inode))
728 goto out_acl_release;
729
730 posix_acl_release(acl);
731
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100732 /*
733 * Check if sgid bit needs to be cleared (actual setacl operation will
734 * be done with mounter's capabilities and so that won't do it for us).
735 */
736 if (unlikely(inode->i_mode & S_ISGID) &&
737 handler->flags == ACL_TYPE_ACCESS &&
738 !in_group_p(inode->i_gid) &&
739 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
740 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
741
742 err = ovl_setattr(dentry, &iattr);
743 if (err)
744 return err;
745 }
746
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200747 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200748 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200749 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200750
751 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200752
753out_acl_release:
754 posix_acl_release(acl);
755 return err;
756}
757
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200758static int ovl_own_xattr_get(const struct xattr_handler *handler,
759 struct dentry *dentry, struct inode *inode,
760 const char *name, void *buffer, size_t size)
761{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200762 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200763}
764
Miklos Szeredid837a492016-07-29 12:05:24 +0200765static int ovl_own_xattr_set(const struct xattr_handler *handler,
766 struct dentry *dentry, struct inode *inode,
767 const char *name, const void *value,
768 size_t size, int flags)
769{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200770 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200771}
772
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200773static int ovl_other_xattr_get(const struct xattr_handler *handler,
774 struct dentry *dentry, struct inode *inode,
775 const char *name, void *buffer, size_t size)
776{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200777 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200778}
779
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200780static int ovl_other_xattr_set(const struct xattr_handler *handler,
781 struct dentry *dentry, struct inode *inode,
782 const char *name, const void *value,
783 size_t size, int flags)
784{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200785 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200786}
787
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200788static const struct xattr_handler __maybe_unused
789ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200790 .name = XATTR_NAME_POSIX_ACL_ACCESS,
791 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200792 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200793 .set = ovl_posix_acl_xattr_set,
794};
795
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200796static const struct xattr_handler __maybe_unused
797ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200798 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
799 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200800 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200801 .set = ovl_posix_acl_xattr_set,
802};
803
804static const struct xattr_handler ovl_own_xattr_handler = {
805 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200806 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200807 .set = ovl_own_xattr_set,
808};
809
810static const struct xattr_handler ovl_other_xattr_handler = {
811 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200812 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200813 .set = ovl_other_xattr_set,
814};
815
816static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200817#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200818 &ovl_posix_acl_access_xattr_handler,
819 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200820#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200821 &ovl_own_xattr_handler,
822 &ovl_other_xattr_handler,
823 NULL
824};
825
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200826static int ovl_fill_super(struct super_block *sb, void *data, int silent)
827{
Kees Cook33006cd2017-03-29 14:02:19 -0700828 struct path upperpath = { };
829 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200830 struct dentry *root_dentry;
831 struct ovl_entry *oe;
832 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100833 struct path *stack = NULL;
834 char *lowertmp;
835 char *lower;
836 unsigned int numlower;
837 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100838 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200839 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300840 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200841 int err;
842
Erez Zadokf45827e82014-10-24 00:14:38 +0200843 err = -ENOMEM;
844 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
845 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200846 goto out;
847
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100848 ufs->config.redirect_dir = ovl_redirect_dir_def;
Amir Goldstein02bcd152017-06-21 15:28:36 +0300849 ufs->config.index = ovl_index_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200850 err = ovl_parse_opt((char *) data, &ufs->config);
851 if (err)
852 goto out_free_config;
853
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200854 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100855 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300856 if (!silent)
857 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200858 goto out_free_config;
859 }
860
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100861 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100862 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100863 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100864 if (!ufs->config.workdir) {
865 pr_err("overlayfs: missing 'workdir'\n");
866 goto out_free_config;
867 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200868
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100869 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
870 if (err)
871 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100872
hujianyang71cbad72015-01-15 13:20:57 +0800873 /* Upper fs should not be r/o */
David Howellsbc98a422017-07-17 08:45:34 +0100874 if (sb_rdonly(upperpath.mnt->mnt_sb)) {
hujianyang71cbad72015-01-15 13:20:57 +0800875 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
876 err = -EINVAL;
877 goto out_put_upperpath;
878 }
879
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100880 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
881 if (err)
882 goto out_put_upperpath;
883
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300884 err = -EBUSY;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300885 if (ovl_inuse_trylock(upperpath.dentry)) {
886 ufs->upperdir_locked = true;
887 } else if (ufs->config.index) {
888 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300889 goto out_put_upperpath;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300890 } else {
891 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300892 }
893
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100894 err = ovl_mount_dir(ufs->config.workdir, &workpath);
895 if (err)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300896 goto out_unlock_upperdentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100897
hujianyang2f83fd82015-01-06 12:52:13 +0800898 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100899 if (upperpath.mnt != workpath.mnt) {
900 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
901 goto out_put_workpath;
902 }
903 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
904 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
905 goto out_put_workpath;
906 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300907
908 err = -EBUSY;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300909 if (ovl_inuse_trylock(workpath.dentry)) {
910 ufs->workdir_locked = true;
911 } else if (ufs->config.index) {
912 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300913 goto out_put_workpath;
Amir Goldstein85fdee12017-09-29 10:21:21 +0300914 } else {
915 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300916 }
917
918 ufs->workbasedir = workpath.dentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100919 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100920 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100921 err = -ENOMEM;
922 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
923 if (!lowertmp)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300924 goto out_unlock_workdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200925
926 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100927 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800928 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100929 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800930 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100931 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800932 } else if (!ufs->config.upperdir && stacklen == 1) {
933 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
934 goto out_free_lowertmp;
935 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100936
Geliang Tang313684c2016-11-18 23:02:46 +0800937 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100938 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
939 if (!stack)
940 goto out_free_lowertmp;
941
Geliang Tang313684c2016-11-18 23:02:46 +0800942 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100943 lower = lowertmp;
944 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100945 err = ovl_lower_dir(lower, &stack[numlower], ufs,
946 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100947 if (err)
948 goto out_put_lowerpath;
949
950 lower = strchr(lower, '\0') + 1;
951 }
952
953 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100954 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200955 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
956 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100957 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200958 }
959
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100960 if (ufs->config.upperdir) {
961 ufs->upper_mnt = clone_private_mount(&upperpath);
962 err = PTR_ERR(ufs->upper_mnt);
963 if (IS_ERR(ufs->upper_mnt)) {
964 pr_err("overlayfs: failed to clone upperpath\n");
965 goto out_put_lowerpath;
966 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300967
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200968 /* Don't inherit atime flags */
969 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
970
971 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100972
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300973 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
974 OVL_WORKDIR_NAME, false);
Vivek Goyal45aebea2016-02-22 09:28:34 -0500975 /*
976 * Upper should support d_type, else whiteouts are visible.
977 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400978 * iterate_dir() on workdir. This check requires successful
979 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500980 */
Vivek Goyal21765192016-05-20 09:04:26 -0400981 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200982 struct dentry *temp;
983
Vivek Goyal21765192016-05-20 09:04:26 -0400984 err = ovl_check_d_type_supported(&workpath);
985 if (err < 0)
986 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500987
Vivek Goyale7c0b592016-07-01 10:02:44 -0400988 /*
989 * We allowed this configuration and don't want to
990 * break users over kernel upgrade. So warn instead
991 * of erroring out.
992 */
993 if (!err)
994 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200995
996 /* Check if upper/work fs supports O_TMPFILE */
997 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
998 ufs->tmpfile = !IS_ERR(temp);
999 if (ufs->tmpfile)
1000 dput(temp);
1001 else
1002 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +03001003
1004 /*
1005 * Check if upper/work fs supports trusted.overlay.*
1006 * xattr
1007 */
1008 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
1009 "0", 1, 0);
1010 if (err) {
1011 ufs->noxattr = true;
1012 pr_warn("overlayfs: upper fs does not support xattr.\n");
1013 } else {
1014 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1015 }
Amir Goldstein02bcd152017-06-21 15:28:36 +03001016
1017 /* Check if upper/work fs supports file handles */
1018 if (ufs->config.index &&
1019 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1020 ufs->config.index = false;
1021 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1022 }
Vivek Goyal45aebea2016-02-22 09:28:34 -05001023 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001024 }
1025
hujianyang2f83fd82015-01-06 12:52:13 +08001026 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001027 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001028 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001029 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001030 for (i = 0; i < numlower; i++) {
1031 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001032
hujianyang2f83fd82015-01-06 12:52:13 +08001033 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001034 if (IS_ERR(mnt)) {
1035 pr_err("overlayfs: failed to clone lowerpath\n");
1036 goto out_put_lower_mnt;
1037 }
1038 /*
1039 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1040 * will fail instead of modifying lower fs.
1041 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001042 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001043
1044 ufs->lower_mnt[ufs->numlower] = mnt;
1045 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001046
1047 /* Check if all lower layers are on same sb */
1048 if (i == 0)
1049 ufs->same_sb = mnt->mnt_sb;
1050 else if (ufs->same_sb != mnt->mnt_sb)
1051 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001052 }
1053
hujianyang71cbad72015-01-15 13:20:57 +08001054 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1055 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001056 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001057 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1058 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001059
Amir Goldstein02bcd152017-06-21 15:28:36 +03001060 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001061 /* Verify lower root is upper root origin */
1062 err = ovl_verify_origin(upperpath.dentry, ufs->lower_mnt[0],
Amir Goldstein54fb3472017-06-21 15:28:38 +03001063 stack[0].dentry, false, true);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +03001064 if (err) {
1065 pr_err("overlayfs: failed to verify upper root origin\n");
1066 goto out_put_lower_mnt;
1067 }
1068
Amir Goldstein02bcd152017-06-21 15:28:36 +03001069 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1070 OVL_INDEXDIR_NAME, true);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001071 if (ufs->indexdir) {
1072 /* Verify upper root is index dir origin */
1073 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
1074 upperpath.dentry, true, true);
1075 if (err)
1076 pr_err("overlayfs: failed to verify index dir origin\n");
Amir Goldstein415543d2017-06-21 15:28:42 +03001077
Amir Goldsteincaf70cb2017-06-21 13:46:12 +03001078 /* Cleanup bad/stale/orphan index entries */
Amir Goldstein415543d2017-06-21 15:28:42 +03001079 if (!err)
1080 err = ovl_indexdir_cleanup(ufs->indexdir,
1081 ufs->upper_mnt,
1082 stack, numlower);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001083 }
1084 if (err || !ufs->indexdir)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001085 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
Amir Goldstein54fb3472017-06-21 15:28:38 +03001086 if (err)
1087 goto out_put_indexdir;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001088 }
1089
1090 /* Show index=off/on in /proc/mounts for any of the reasons above */
1091 if (!ufs->indexdir)
1092 ufs->config.index = false;
1093
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001094 if (remote)
1095 sb->s_d_op = &ovl_reval_dentry_operations;
1096 else
1097 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001098
Amir Goldstein8fc646b2017-07-11 15:58:35 +03001099 err = -ENOMEM;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001100 ufs->creator_cred = cred = prepare_creds();
1101 if (!cred)
Amir Goldstein02bcd152017-06-21 15:28:36 +03001102 goto out_put_indexdir;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001103
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001104 /* Never override disk quota limits or use reserved space */
1105 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1106
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001107 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001108 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001109 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001110 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001111
Vivek Goyal655042c2016-10-14 03:03:36 +02001112 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1113 sb->s_op = &ovl_super_operations;
1114 sb->s_xattr = ovl_xattr_handlers;
1115 sb->s_fs_info = ufs;
1116 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1117
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001118 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001119 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001120 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001121
1122 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001123 for (i = 0; i < numlower; i++)
1124 mntput(stack[i].mnt);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001125 mntput(workpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001126 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001127
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001128 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001129 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001130 if (ovl_is_impuredir(upperpath.dentry))
1131 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001132 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001133 for (i = 0; i < numlower; i++) {
1134 oe->lowerstack[i].dentry = stack[i].dentry;
1135 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1136 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001137 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001138
1139 root_dentry->d_fsdata = oe;
1140
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001141 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1142 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001143
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001144 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001145
1146 return 0;
1147
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001148out_free_oe:
1149 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001150out_put_cred:
1151 put_cred(ufs->creator_cred);
Amir Goldstein02bcd152017-06-21 15:28:36 +03001152out_put_indexdir:
1153 dput(ufs->indexdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001154out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001155 for (i = 0; i < ufs->numlower; i++)
1156 mntput(ufs->lower_mnt[i]);
1157 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001158out_put_workdir:
1159 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001160 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001161out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001162 for (i = 0; i < numlower; i++)
1163 path_put(&stack[i]);
1164 kfree(stack);
1165out_free_lowertmp:
1166 kfree(lowertmp);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001167out_unlock_workdentry:
Amir Goldstein85fdee12017-09-29 10:21:21 +03001168 if (ufs->workdir_locked)
1169 ovl_inuse_unlock(workpath.dentry);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001170out_put_workpath:
1171 path_put(&workpath);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001172out_unlock_upperdentry:
Amir Goldstein85fdee12017-09-29 10:21:21 +03001173 if (ufs->upperdir_locked)
1174 ovl_inuse_unlock(upperpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001175out_put_upperpath:
1176 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001177out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001178 kfree(ufs->config.lowerdir);
1179 kfree(ufs->config.upperdir);
1180 kfree(ufs->config.workdir);
1181 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001182out:
1183 return err;
1184}
1185
1186static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1187 const char *dev_name, void *raw_data)
1188{
1189 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1190}
1191
1192static struct file_system_type ovl_fs_type = {
1193 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001194 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001195 .mount = ovl_mount,
1196 .kill_sb = kill_anon_super,
1197};
Miklos Szeredief94b182014-11-20 16:39:59 +01001198MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001199
Amir Goldstein13cf1992017-06-12 09:54:40 +03001200static void ovl_inode_init_once(void *foo)
1201{
1202 struct ovl_inode *oi = foo;
1203
1204 inode_init_once(&oi->vfs_inode);
1205}
1206
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001207static int __init ovl_init(void)
1208{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001209 int err;
1210
1211 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1212 sizeof(struct ovl_inode), 0,
1213 (SLAB_RECLAIM_ACCOUNT|
1214 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1215 ovl_inode_init_once);
1216 if (ovl_inode_cachep == NULL)
1217 return -ENOMEM;
1218
1219 err = register_filesystem(&ovl_fs_type);
1220 if (err)
1221 kmem_cache_destroy(ovl_inode_cachep);
1222
1223 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001224}
1225
1226static void __exit ovl_exit(void)
1227{
1228 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001229
1230 /*
1231 * Make sure all delayed rcu free inodes are flushed before we
1232 * destroy cache.
1233 */
1234 rcu_barrier();
1235 kmem_cache_destroy(ovl_inode_cachep);
1236
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001237}
1238
1239module_init(ovl_init);
1240module_exit(ovl_exit);