blob: e4852cb1ec64dc6f4f2d565745096fc40a932d73 [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"
21
22MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23MODULE_DESCRIPTION("Overlay filesystem");
24MODULE_LICENSE("GPL");
25
Miklos Szeredie9be9d52014-10-24 00:14:38 +020026
27struct ovl_dir_cache;
28
Miklos Szeredia78d9f02014-12-13 00:59:52 +010029#define OVL_MAX_STACK 500
30
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010031static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
32module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
33MODULE_PARM_DESC(ovl_redirect_dir_def,
34 "Default to on or off for the redirect_dir feature");
Miklos Szeredie9be9d52014-10-24 00:14:38 +020035
Amir Goldstein02bcd152017-06-21 15:28:36 +030036static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
37module_param_named(index, ovl_index_def, bool, 0644);
38MODULE_PARM_DESC(ovl_index_def,
39 "Default to on or off for the inodes index feature");
40
Miklos Szeredi4155c102017-11-10 09:39:15 +010041static void ovl_entry_stack_free(struct ovl_entry *oe)
42{
43 unsigned int i;
44
45 for (i = 0; i < oe->numlower; i++)
46 dput(oe->lowerstack[i].dentry);
47}
48
Miklos Szeredie9be9d52014-10-24 00:14:38 +020049static void ovl_dentry_release(struct dentry *dentry)
50{
51 struct ovl_entry *oe = dentry->d_fsdata;
52
53 if (oe) {
Miklos Szeredi4155c102017-11-10 09:39:15 +010054 ovl_entry_stack_free(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020055 kfree_rcu(oe, rcu);
56 }
57}
58
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030059static int ovl_check_append_only(struct inode *inode, int flag)
60{
61 /*
62 * This test was moot in vfs may_open() because overlay inode does
63 * not have the S_APPEND flag, so re-check on real upper inode
64 */
65 if (IS_APPEND(inode)) {
66 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
67 return -EPERM;
68 if (flag & O_TRUNC)
69 return -EPERM;
70 }
71
72 return 0;
73}
74
Miklos Szeredi2d902672016-06-30 08:53:27 +020075static struct dentry *ovl_d_real(struct dentry *dentry,
76 const struct inode *inode,
Miklos Szeredi495e6422017-09-04 21:42:22 +020077 unsigned int open_flags, unsigned int flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040078{
79 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030080 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040081
Miklos Szeredicd913042017-09-05 12:53:11 +020082 if (flags & D_REAL_UPPER)
83 return ovl_dentry_upper(dentry);
84
Miklos Szeredica4c8a32016-12-16 11:02:55 +010085 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040086 if (!inode || inode == d_inode(dentry))
87 return dentry;
88 goto bug;
89 }
90
Miklos Szeredi2d902672016-06-30 08:53:27 +020091 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030092 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +020093 if (err)
94 return ERR_PTR(err);
95 }
96
Miklos Szeredid101a122016-03-26 16:14:37 -040097 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030098 if (real && (!inode || inode == d_inode(real))) {
99 if (!inode) {
100 err = ovl_check_append_only(d_inode(real), open_flags);
101 if (err)
102 return ERR_PTR(err);
103 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400104 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300105 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400106
107 real = ovl_dentry_lower(dentry);
108 if (!real)
109 goto bug;
110
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100111 /* Handle recursion */
Miklos Szeredi495e6422017-09-04 21:42:22 +0200112 real = d_real(real, inode, open_flags, 0);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100113
Miklos Szeredid101a122016-03-26 16:14:37 -0400114 if (!inode || inode == d_inode(real))
115 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400116bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200117 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400118 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
119 return dentry;
120}
121
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200122static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
123{
124 struct ovl_entry *oe = dentry->d_fsdata;
125 unsigned int i;
126 int ret = 1;
127
128 for (i = 0; i < oe->numlower; i++) {
129 struct dentry *d = oe->lowerstack[i].dentry;
130
131 if (d->d_flags & DCACHE_OP_REVALIDATE) {
132 ret = d->d_op->d_revalidate(d, flags);
133 if (ret < 0)
134 return ret;
135 if (!ret) {
136 if (!(flags & LOOKUP_RCU))
137 d_invalidate(d);
138 return -ESTALE;
139 }
140 }
141 }
142 return 1;
143}
144
145static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
146{
147 struct ovl_entry *oe = dentry->d_fsdata;
148 unsigned int i;
149 int ret = 1;
150
151 for (i = 0; i < oe->numlower; i++) {
152 struct dentry *d = oe->lowerstack[i].dentry;
153
154 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
155 ret = d->d_op->d_weak_revalidate(d, flags);
156 if (ret <= 0)
157 break;
158 }
159 }
160 return ret;
161}
162
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200163static const struct dentry_operations ovl_dentry_operations = {
164 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400165 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200166};
167
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200168static const struct dentry_operations ovl_reval_dentry_operations = {
169 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400170 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200171 .d_revalidate = ovl_dentry_revalidate,
172 .d_weak_revalidate = ovl_dentry_weak_revalidate,
173};
174
Amir Goldstein13cf1992017-06-12 09:54:40 +0300175static struct kmem_cache *ovl_inode_cachep;
176
177static struct inode *ovl_alloc_inode(struct super_block *sb)
178{
179 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
180
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900181 if (!oi)
182 return NULL;
183
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200184 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200185 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200186 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200187 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200188 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200189 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300190 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200191
Amir Goldstein13cf1992017-06-12 09:54:40 +0300192 return &oi->vfs_inode;
193}
194
195static void ovl_i_callback(struct rcu_head *head)
196{
197 struct inode *inode = container_of(head, struct inode, i_rcu);
198
199 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
200}
201
202static void ovl_destroy_inode(struct inode *inode)
203{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200204 struct ovl_inode *oi = OVL_I(inode);
205
206 dput(oi->__upperdentry);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200207 kfree(oi->redirect);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200208 ovl_dir_cache_free(inode);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300209 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200210
Amir Goldstein13cf1992017-06-12 09:54:40 +0300211 call_rcu(&inode->i_rcu, ovl_i_callback);
212}
213
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100214static void ovl_free_fs(struct ovl_fs *ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215{
Miklos Szeredidd662662014-12-13 00:59:43 +0100216 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200217
Amir Goldstein02bcd152017-06-21 15:28:36 +0300218 dput(ufs->indexdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200219 dput(ufs->workdir);
Amir Goldstein85fdee12017-09-29 10:21:21 +0300220 if (ufs->workdir_locked)
221 ovl_inuse_unlock(ufs->workbasedir);
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300222 dput(ufs->workbasedir);
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100223 if (ufs->upperdir_locked)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300224 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200225 mntput(ufs->upper_mnt);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +0200226 for (i = 0; i < ufs->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500227 mntput(ufs->lower_layers[i].mnt);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +0200228 free_anon_bdev(ufs->lower_layers[i].pseudo_dev);
229 }
Chandan Rajendrab9343632017-07-24 01:57:54 -0500230 kfree(ufs->lower_layers);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200231
Erez Zadokf45827e82014-10-24 00:14:38 +0200232 kfree(ufs->config.lowerdir);
233 kfree(ufs->config.upperdir);
234 kfree(ufs->config.workdir);
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100235 if (ufs->creator_cred)
236 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200237 kfree(ufs);
238}
239
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100240static void ovl_put_super(struct super_block *sb)
241{
242 struct ovl_fs *ofs = sb->s_fs_info;
243
244 ovl_free_fs(ofs);
245}
246
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200247static int ovl_sync_fs(struct super_block *sb, int wait)
248{
249 struct ovl_fs *ufs = sb->s_fs_info;
250 struct super_block *upper_sb;
251 int ret;
252
253 if (!ufs->upper_mnt)
254 return 0;
255 upper_sb = ufs->upper_mnt->mnt_sb;
256 if (!upper_sb->s_op->sync_fs)
257 return 0;
258
259 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
260 down_read(&upper_sb->s_umount);
261 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
262 up_read(&upper_sb->s_umount);
263 return ret;
264}
265
Andy Whitcroftcc259632014-10-24 00:14:38 +0200266/**
267 * ovl_statfs
268 * @sb: The overlayfs super block
269 * @buf: The struct kstatfs to fill in with stats
270 *
271 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100272 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200273 */
274static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
275{
276 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
277 struct dentry *root_dentry = dentry->d_sb->s_root;
278 struct path path;
279 int err;
280
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100281 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200282
283 err = vfs_statfs(&path, buf);
284 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100285 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200286 buf->f_type = OVERLAYFS_SUPER_MAGIC;
287 }
288
289 return err;
290}
291
Amir Goldstein02bcd152017-06-21 15:28:36 +0300292/* Will this overlay be forced to mount/remount ro? */
293static bool ovl_force_readonly(struct ovl_fs *ufs)
294{
295 return (!ufs->upper_mnt || !ufs->workdir);
296}
297
Erez Zadokf45827e82014-10-24 00:14:38 +0200298/**
299 * ovl_show_options
300 *
301 * Prints the mount options for a given superblock.
302 * Returns zero; does not fail.
303 */
304static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
305{
306 struct super_block *sb = dentry->d_sb;
307 struct ovl_fs *ufs = sb->s_fs_info;
308
Kees Cooka068acf2015-09-04 15:44:57 -0700309 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100310 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700311 seq_show_option(m, "upperdir", ufs->config.upperdir);
312 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100313 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200314 if (ufs->config.default_permissions)
315 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200316 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
317 seq_printf(m, ",redirect_dir=%s",
318 ufs->config.redirect_dir ? "on" : "off");
Amir Goldstein02bcd152017-06-21 15:28:36 +0300319 if (ufs->config.index != ovl_index_def)
320 seq_printf(m, ",index=%s",
321 ufs->config.index ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200322 return 0;
323}
324
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900325static int ovl_remount(struct super_block *sb, int *flags, char *data)
326{
327 struct ovl_fs *ufs = sb->s_fs_info;
328
Amir Goldstein02bcd152017-06-21 15:28:36 +0300329 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900330 return -EROFS;
331
332 return 0;
333}
334
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200335static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300336 .alloc_inode = ovl_alloc_inode,
337 .destroy_inode = ovl_destroy_inode,
338 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200340 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200341 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200342 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900343 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200344};
345
346enum {
347 OPT_LOWERDIR,
348 OPT_UPPERDIR,
349 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200350 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100351 OPT_REDIRECT_DIR_ON,
352 OPT_REDIRECT_DIR_OFF,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300353 OPT_INDEX_ON,
354 OPT_INDEX_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200355 OPT_ERR,
356};
357
358static const match_table_t ovl_tokens = {
359 {OPT_LOWERDIR, "lowerdir=%s"},
360 {OPT_UPPERDIR, "upperdir=%s"},
361 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200362 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100363 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
364 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300365 {OPT_INDEX_ON, "index=on"},
366 {OPT_INDEX_OFF, "index=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200367 {OPT_ERR, NULL}
368};
369
Miklos Szeredi91c77942014-11-20 16:40:00 +0100370static char *ovl_next_opt(char **s)
371{
372 char *sbegin = *s;
373 char *p;
374
375 if (sbegin == NULL)
376 return NULL;
377
378 for (p = sbegin; *p; p++) {
379 if (*p == '\\') {
380 p++;
381 if (!*p)
382 break;
383 } else if (*p == ',') {
384 *p = '\0';
385 *s = p + 1;
386 return sbegin;
387 }
388 }
389 *s = NULL;
390 return sbegin;
391}
392
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200393static int ovl_parse_opt(char *opt, struct ovl_config *config)
394{
395 char *p;
396
Miklos Szeredi91c77942014-11-20 16:40:00 +0100397 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200398 int token;
399 substring_t args[MAX_OPT_ARGS];
400
401 if (!*p)
402 continue;
403
404 token = match_token(p, ovl_tokens, args);
405 switch (token) {
406 case OPT_UPPERDIR:
407 kfree(config->upperdir);
408 config->upperdir = match_strdup(&args[0]);
409 if (!config->upperdir)
410 return -ENOMEM;
411 break;
412
413 case OPT_LOWERDIR:
414 kfree(config->lowerdir);
415 config->lowerdir = match_strdup(&args[0]);
416 if (!config->lowerdir)
417 return -ENOMEM;
418 break;
419
420 case OPT_WORKDIR:
421 kfree(config->workdir);
422 config->workdir = match_strdup(&args[0]);
423 if (!config->workdir)
424 return -ENOMEM;
425 break;
426
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200427 case OPT_DEFAULT_PERMISSIONS:
428 config->default_permissions = true;
429 break;
430
Miklos Szeredia6c60652016-12-16 11:02:56 +0100431 case OPT_REDIRECT_DIR_ON:
432 config->redirect_dir = true;
433 break;
434
435 case OPT_REDIRECT_DIR_OFF:
436 config->redirect_dir = false;
437 break;
438
Amir Goldstein02bcd152017-06-21 15:28:36 +0300439 case OPT_INDEX_ON:
440 config->index = true;
441 break;
442
443 case OPT_INDEX_OFF:
444 config->index = false;
445 break;
446
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200447 default:
hujianyangbead55e2015-01-15 13:17:36 +0800448 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200449 return -EINVAL;
450 }
451 }
hujianyang71cbad72015-01-15 13:20:57 +0800452
453 /* Workdir is useless in non-upper mount */
454 if (!config->upperdir && config->workdir) {
455 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
456 config->workdir);
457 kfree(config->workdir);
458 config->workdir = NULL;
459 }
460
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200461 return 0;
462}
463
464#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300465#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200466
Miklos Szeredi6e882562017-11-10 09:39:15 +0100467static struct dentry *ovl_workdir_create(struct ovl_fs *ufs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300468 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200469{
Miklos Szeredi6e882562017-11-10 09:39:15 +0100470 struct inode *dir = ufs->workbasedir->d_inode;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300471 struct vfsmount *mnt = ufs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200472 struct dentry *work;
473 int err;
474 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300475 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476
477 err = mnt_want_write(mnt);
478 if (err)
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300479 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480
Al Viro59551022016-01-22 15:40:57 -0500481 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300482 locked = true;
483
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200484retry:
Miklos Szeredi6e882562017-11-10 09:39:15 +0100485 work = lookup_one_len(name, ufs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200486
487 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200488 struct iattr attr = {
489 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000490 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200491 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200492
493 if (work->d_inode) {
494 err = -EEXIST;
495 if (retried)
496 goto out_dput;
497
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300498 if (persist)
499 goto out_unlock;
500
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200501 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200502 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200503 dput(work);
504 goto retry;
505 }
506
Al Viro32a3d842016-12-04 17:33:17 +0000507 err = ovl_create_real(dir, work,
508 &(struct cattr){.mode = S_IFDIR | 0},
509 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200510 if (err)
511 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200512
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200513 /*
514 * Try to remove POSIX ACL xattrs from workdir. We are good if:
515 *
516 * a) success (there was a POSIX ACL xattr and was removed)
517 * b) -ENODATA (there was no POSIX ACL xattr)
518 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
519 *
520 * There are various other error values that could effectively
521 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
522 * if the xattr name is too long), but the set of filesystems
523 * allowed as upper are limited to "normal" ones, where checking
524 * for the above two errors is sufficient.
525 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200526 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200527 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200528 goto out_dput;
529
530 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200531 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200532 goto out_dput;
533
534 /* Clear any inherited mode bits */
535 inode_lock(work->d_inode);
536 err = notify_change(work, &attr, NULL);
537 inode_unlock(work->d_inode);
538 if (err)
539 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300540 } else {
541 err = PTR_ERR(work);
542 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200543 }
544out_unlock:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200545 mnt_drop_write(mnt);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300546 if (locked)
547 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200548
549 return work;
550
551out_dput:
552 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300553out_err:
554 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
555 ufs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300556 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200557 goto out_unlock;
558}
559
Miklos Szeredi91c77942014-11-20 16:40:00 +0100560static void ovl_unescape(char *s)
561{
562 char *d = s;
563
564 for (;; s++, d++) {
565 if (*s == '\\')
566 s++;
567 *d = *s;
568 if (!*s)
569 break;
570 }
571}
572
Miklos Szerediab508822014-12-13 00:59:49 +0100573static int ovl_mount_dir_noesc(const char *name, struct path *path)
574{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100575 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100576
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100577 if (!*name) {
578 pr_err("overlayfs: empty lowerdir\n");
579 goto out;
580 }
Miklos Szerediab508822014-12-13 00:59:49 +0100581 err = kern_path(name, LOOKUP_FOLLOW, path);
582 if (err) {
583 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
584 goto out;
585 }
586 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200587 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100588 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
589 goto out_put;
590 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100591 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100592 pr_err("overlayfs: '%s' not a directory\n", name);
593 goto out_put;
594 }
595 return 0;
596
597out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100598 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100599out:
600 return err;
601}
602
603static int ovl_mount_dir(const char *name, struct path *path)
604{
605 int err = -ENOMEM;
606 char *tmp = kstrdup(name, GFP_KERNEL);
607
608 if (tmp) {
609 ovl_unescape(tmp);
610 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200611
612 if (!err)
613 if (ovl_dentry_remote(path->dentry)) {
614 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
615 tmp);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100616 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200617 err = -EINVAL;
618 }
Miklos Szerediab508822014-12-13 00:59:49 +0100619 kfree(tmp);
620 }
621 return err;
622}
623
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100624static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
625 const char *name)
626{
627 struct kstatfs statfs;
628 int err = vfs_statfs(path, &statfs);
629
630 if (err)
631 pr_err("overlayfs: statfs failed on '%s'\n", name);
632 else
633 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
634
635 return err;
636}
637
638static int ovl_lower_dir(const char *name, struct path *path,
639 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100640{
641 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100642
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100643 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100644 if (err)
645 goto out;
646
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100647 err = ovl_check_namelen(path, ofs, name);
648 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100649 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100650
Miklos Szerediab508822014-12-13 00:59:49 +0100651 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
652
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200653 if (ovl_dentry_remote(path->dentry))
654 *remote = true;
655
Amir Goldstein02bcd152017-06-21 15:28:36 +0300656 /*
657 * The inodes index feature needs to encode and decode file
658 * handles, so it requires that all layers support them.
659 */
660 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
661 ofs->config.index = false;
662 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
663 }
664
Miklos Szerediab508822014-12-13 00:59:49 +0100665 return 0;
666
667out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100668 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100669out:
670 return err;
671}
672
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200673/* Workdir should not be subdir of upperdir and vice versa */
674static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
675{
676 bool ok = false;
677
678 if (workdir != upperdir) {
679 ok = (lock_rename(workdir, upperdir) == NULL);
680 unlock_rename(workdir, upperdir);
681 }
682 return ok;
683}
684
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100685static unsigned int ovl_split_lowerdirs(char *str)
686{
687 unsigned int ctr = 1;
688 char *s, *d;
689
690 for (s = d = str;; s++, d++) {
691 if (*s == '\\') {
692 s++;
693 } else if (*s == ':') {
694 *d = '\0';
695 ctr++;
696 continue;
697 }
698 *d = *s;
699 if (!*s)
700 break;
701 }
702 return ctr;
703}
704
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200705static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200706ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
707 struct dentry *dentry, struct inode *inode,
708 const char *name, void *buffer, size_t size)
709{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200710 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200711}
712
713static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200714ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
715 struct dentry *dentry, struct inode *inode,
716 const char *name, const void *value,
717 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200718{
719 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200720 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200721 struct posix_acl *acl = NULL;
722 int err;
723
724 /* Check that everything is OK before copy-up */
725 if (value) {
726 acl = posix_acl_from_xattr(&init_user_ns, value, size);
727 if (IS_ERR(acl))
728 return PTR_ERR(acl);
729 }
730 err = -EOPNOTSUPP;
731 if (!IS_POSIXACL(d_inode(workdir)))
732 goto out_acl_release;
733 if (!realinode->i_op->set_acl)
734 goto out_acl_release;
735 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
736 err = acl ? -EACCES : 0;
737 goto out_acl_release;
738 }
739 err = -EPERM;
740 if (!inode_owner_or_capable(inode))
741 goto out_acl_release;
742
743 posix_acl_release(acl);
744
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100745 /*
746 * Check if sgid bit needs to be cleared (actual setacl operation will
747 * be done with mounter's capabilities and so that won't do it for us).
748 */
749 if (unlikely(inode->i_mode & S_ISGID) &&
750 handler->flags == ACL_TYPE_ACCESS &&
751 !in_group_p(inode->i_gid) &&
752 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
753 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
754
755 err = ovl_setattr(dentry, &iattr);
756 if (err)
757 return err;
758 }
759
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200760 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200761 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200762 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200763
764 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200765
766out_acl_release:
767 posix_acl_release(acl);
768 return err;
769}
770
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200771static int ovl_own_xattr_get(const struct xattr_handler *handler,
772 struct dentry *dentry, struct inode *inode,
773 const char *name, void *buffer, size_t size)
774{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200775 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200776}
777
Miklos Szeredid837a492016-07-29 12:05:24 +0200778static int ovl_own_xattr_set(const struct xattr_handler *handler,
779 struct dentry *dentry, struct inode *inode,
780 const char *name, const void *value,
781 size_t size, int flags)
782{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200783 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200784}
785
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200786static int ovl_other_xattr_get(const struct xattr_handler *handler,
787 struct dentry *dentry, struct inode *inode,
788 const char *name, void *buffer, size_t size)
789{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200790 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200791}
792
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200793static int ovl_other_xattr_set(const struct xattr_handler *handler,
794 struct dentry *dentry, struct inode *inode,
795 const char *name, const void *value,
796 size_t size, int flags)
797{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200798 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200799}
800
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200801static const struct xattr_handler __maybe_unused
802ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200803 .name = XATTR_NAME_POSIX_ACL_ACCESS,
804 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200805 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200806 .set = ovl_posix_acl_xattr_set,
807};
808
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200809static const struct xattr_handler __maybe_unused
810ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200811 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
812 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200813 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200814 .set = ovl_posix_acl_xattr_set,
815};
816
817static const struct xattr_handler ovl_own_xattr_handler = {
818 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200819 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200820 .set = ovl_own_xattr_set,
821};
822
823static const struct xattr_handler ovl_other_xattr_handler = {
824 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200825 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200826 .set = ovl_other_xattr_set,
827};
828
829static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200830#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200831 &ovl_posix_acl_access_xattr_handler,
832 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200833#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200834 &ovl_own_xattr_handler,
835 &ovl_other_xattr_handler,
836 NULL
837};
838
Miklos Szeredi50649752017-11-10 09:39:15 +0100839static int ovl_get_upper(struct ovl_fs *ufs, struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100840{
Miklos Szeredi50649752017-11-10 09:39:15 +0100841 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100842 int err;
843
844 err = ovl_mount_dir(ufs->config.upperdir, upperpath);
845 if (err)
846 goto out;
847
848 /* Upper fs should not be r/o */
849 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
850 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
851 err = -EINVAL;
852 goto out;
853 }
854
855 err = ovl_check_namelen(upperpath, ufs, ufs->config.upperdir);
856 if (err)
857 goto out;
858
859 err = -EBUSY;
860 if (ovl_inuse_trylock(upperpath->dentry)) {
861 ufs->upperdir_locked = true;
862 } else if (ufs->config.index) {
863 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
864 goto out;
865 } else {
866 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
867 }
Miklos Szeredi50649752017-11-10 09:39:15 +0100868
869 upper_mnt = clone_private_mount(upperpath);
870 err = PTR_ERR(upper_mnt);
871 if (IS_ERR(upper_mnt)) {
872 pr_err("overlayfs: failed to clone upperpath\n");
873 goto out;
874 }
875
876 /* Don't inherit atime flags */
877 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
878 ufs->upper_mnt = upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100879 err = 0;
880out:
881 return err;
882}
883
Miklos Szeredibca44b52017-11-10 09:39:15 +0100884static int ovl_make_workdir(struct ovl_fs *ufs, struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100885{
886 struct dentry *temp;
887 int err;
888
Miklos Szeredi6e882562017-11-10 09:39:15 +0100889 ufs->workdir = ovl_workdir_create(ufs, OVL_WORKDIR_NAME, false);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100890 if (!ufs->workdir)
891 return 0;
892
893 /*
894 * Upper should support d_type, else whiteouts are visible. Given
895 * workdir and upper are on same fs, we can do iterate_dir() on
896 * workdir. This check requires successful creation of workdir in
897 * previous step.
898 */
899 err = ovl_check_d_type_supported(workpath);
900 if (err < 0)
901 return err;
902
903 /*
904 * We allowed this configuration and don't want to break users over
905 * kernel upgrade. So warn instead of erroring out.
906 */
907 if (!err)
908 pr_warn("overlayfs: upper fs needs to support d_type.\n");
909
910 /* Check if upper/work fs supports O_TMPFILE */
911 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
912 ufs->tmpfile = !IS_ERR(temp);
913 if (ufs->tmpfile)
914 dput(temp);
915 else
916 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
917
918 /*
919 * Check if upper/work fs supports trusted.overlay.* xattr
920 */
921 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
922 if (err) {
923 ufs->noxattr = true;
924 pr_warn("overlayfs: upper fs does not support xattr.\n");
925 } else {
926 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
927 }
928
929 /* Check if upper/work fs supports file handles */
930 if (ufs->config.index &&
931 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
932 ufs->config.index = false;
933 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
934 }
935
936 return 0;
937}
938
Miklos Szeredibca44b52017-11-10 09:39:15 +0100939static int ovl_get_workdir(struct ovl_fs *ufs, struct path *upperpath)
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100940{
941 int err;
Miklos Szeredibca44b52017-11-10 09:39:15 +0100942 struct path workpath = { };
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100943
Miklos Szeredibca44b52017-11-10 09:39:15 +0100944 err = ovl_mount_dir(ufs->config.workdir, &workpath);
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100945 if (err)
946 goto out;
947
948 err = -EINVAL;
Miklos Szeredibca44b52017-11-10 09:39:15 +0100949 if (upperpath->mnt != workpath.mnt) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100950 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
951 goto out;
952 }
Miklos Szeredibca44b52017-11-10 09:39:15 +0100953 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100954 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
955 goto out;
956 }
957
958 err = -EBUSY;
Miklos Szeredibca44b52017-11-10 09:39:15 +0100959 if (ovl_inuse_trylock(workpath.dentry)) {
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100960 ufs->workdir_locked = true;
961 } else if (ufs->config.index) {
962 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
963 goto out;
964 } else {
965 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
966 }
967
Miklos Szeredibca44b52017-11-10 09:39:15 +0100968 ufs->workbasedir = dget(workpath.dentry);
969 err = ovl_make_workdir(ufs, &workpath);
970 if (err)
971 goto out;
972
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100973 err = 0;
974out:
Miklos Szeredibca44b52017-11-10 09:39:15 +0100975 path_put(&workpath);
976
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100977 return err;
978}
979
Miklos Szeredi6e882562017-11-10 09:39:15 +0100980static int ovl_get_indexdir(struct ovl_fs *ufs, struct ovl_entry *oe,
Miklos Szeredi95e6d412017-11-09 10:23:29 +0100981 struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +0100982{
983 int err;
984
985 /* Verify lower root is upper root origin */
986 err = ovl_verify_origin(upperpath->dentry,
987 oe->lowerstack[0].layer->mnt,
988 oe->lowerstack[0].dentry,
989 false, true);
990 if (err) {
991 pr_err("overlayfs: failed to verify upper root origin\n");
992 goto out;
993 }
994
Miklos Szeredi6e882562017-11-10 09:39:15 +0100995 ufs->indexdir = ovl_workdir_create(ufs, OVL_INDEXDIR_NAME, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +0100996 if (ufs->indexdir) {
997 /* Verify upper root is index dir origin */
998 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
999 upperpath->dentry, true, true);
1000 if (err)
1001 pr_err("overlayfs: failed to verify index dir origin\n");
1002
1003 /* Cleanup bad/stale/orphan index entries */
1004 if (!err)
1005 err = ovl_indexdir_cleanup(ufs->indexdir,
1006 ufs->upper_mnt,
1007 oe->lowerstack,
1008 oe->numlower);
1009 }
1010 if (err || !ufs->indexdir)
1011 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1012
1013out:
1014 return err;
1015}
1016
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001017static int ovl_get_lower_layers(struct ovl_fs *ufs, struct path *stack,
1018 unsigned int numlower)
1019{
1020 int err;
1021 unsigned int i;
1022
1023 err = -ENOMEM;
1024 ufs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1025 GFP_KERNEL);
1026 if (ufs->lower_layers == NULL)
1027 goto out;
1028 for (i = 0; i < numlower; i++) {
1029 struct vfsmount *mnt;
1030 dev_t dev;
1031
1032 err = get_anon_bdev(&dev);
1033 if (err) {
1034 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1035 goto out;
1036 }
1037
1038 mnt = clone_private_mount(&stack[i]);
1039 err = PTR_ERR(mnt);
1040 if (IS_ERR(mnt)) {
1041 pr_err("overlayfs: failed to clone lowerpath\n");
1042 free_anon_bdev(dev);
1043 goto out;
1044 }
1045 /*
1046 * Make lower layers R/O. That way fchmod/fchown on lower file
1047 * will fail instead of modifying lower fs.
1048 */
1049 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1050
1051 ufs->lower_layers[ufs->numlower].mnt = mnt;
1052 ufs->lower_layers[ufs->numlower].pseudo_dev = dev;
1053 ufs->numlower++;
1054
1055 /* Check if all lower layers are on same sb */
1056 if (i == 0)
1057 ufs->same_sb = mnt->mnt_sb;
1058 else if (ufs->same_sb != mnt->mnt_sb)
1059 ufs->same_sb = NULL;
1060 }
1061 err = 0;
1062out:
1063 return err;
1064}
1065
Miklos Szeredi4155c102017-11-10 09:39:15 +01001066static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1067 struct ovl_fs *ufs)
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001068{
1069 int err;
1070 char *lowertmp, *lower;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001071 struct path *stack = NULL;
1072 unsigned int stacklen, numlower = 0, i;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001073 bool remote = false;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001074 struct ovl_entry *oe;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001075
1076 err = -ENOMEM;
1077 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1078 if (!lowertmp)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001079 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001080
1081 err = -EINVAL;
1082 stacklen = ovl_split_lowerdirs(lowertmp);
1083 if (stacklen > OVL_MAX_STACK) {
1084 pr_err("overlayfs: too many lower directories, limit is %d\n",
1085 OVL_MAX_STACK);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001086 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001087 } else if (!ufs->config.upperdir && stacklen == 1) {
1088 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001089 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001090 }
1091
1092 err = -ENOMEM;
1093 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1094 if (!stack)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001095 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001096
1097 err = -EINVAL;
1098 lower = lowertmp;
1099 for (numlower = 0; numlower < stacklen; numlower++) {
1100 err = ovl_lower_dir(lower, &stack[numlower], ufs,
1101 &sb->s_stack_depth, &remote);
1102 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001103 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001104
1105 lower = strchr(lower, '\0') + 1;
1106 }
1107
1108 err = -EINVAL;
1109 sb->s_stack_depth++;
1110 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1111 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi4155c102017-11-10 09:39:15 +01001112 goto out_err;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001113 }
1114
Miklos Szeredi4155c102017-11-10 09:39:15 +01001115 err = ovl_get_lower_layers(ufs, stack, numlower);
1116 if (err)
1117 goto out_err;
1118
1119 err = -ENOMEM;
1120 oe = ovl_alloc_entry(numlower);
1121 if (!oe)
1122 goto out_err;
1123
1124 for (i = 0; i < numlower; i++) {
1125 oe->lowerstack[i].dentry = dget(stack[i].dentry);
1126 oe->lowerstack[i].layer = &ufs->lower_layers[i];
1127 }
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001128
1129 if (remote)
1130 sb->s_d_op = &ovl_reval_dentry_operations;
1131 else
1132 sb->s_d_op = &ovl_dentry_operations;
1133
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001134out:
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001135 for (i = 0; i < numlower; i++)
1136 path_put(&stack[i]);
1137 kfree(stack);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001138 kfree(lowertmp);
1139
1140 return oe;
1141
1142out_err:
1143 oe = ERR_PTR(err);
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001144 goto out;
1145}
1146
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001147static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1148{
Kees Cook33006cd2017-03-29 14:02:19 -07001149 struct path upperpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001150 struct dentry *root_dentry;
Miklos Szeredi4155c102017-11-10 09:39:15 +01001151 struct ovl_entry *oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001152 struct ovl_fs *ufs;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001153 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001154 int err;
1155
Erez Zadokf45827e82014-10-24 00:14:38 +02001156 err = -ENOMEM;
1157 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1158 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001159 goto out;
1160
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001161 ufs->creator_cred = cred = prepare_creds();
1162 if (!cred)
1163 goto out_err;
1164
Miklos Szeredi688ea0e2016-12-16 11:02:57 +01001165 ufs->config.redirect_dir = ovl_redirect_dir_def;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001166 ufs->config.index = ovl_index_def;
Erez Zadokf45827e82014-10-24 00:14:38 +02001167 err = ovl_parse_opt((char *) data, &ufs->config);
1168 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001169 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001170
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001171 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001172 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001173 if (!silent)
1174 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001175 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001176 }
1177
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001178 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001179 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001180 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001181 if (!ufs->config.workdir) {
1182 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001183 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001184 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001185
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001186 err = ovl_get_upper(ufs, &upperpath);
1187 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001188 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001189
Miklos Szeredibca44b52017-11-10 09:39:15 +01001190 err = ovl_get_workdir(ufs, &upperpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001191 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001192 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001193
Miklos Szeredi6e882562017-11-10 09:39:15 +01001194 if (!ufs->workdir)
1195 sb->s_flags |= MS_RDONLY;
1196
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001197 sb->s_stack_depth = ufs->upper_mnt->mnt_sb->s_stack_depth;
1198 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
1199
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001200 }
Miklos Szeredi4155c102017-11-10 09:39:15 +01001201 oe = ovl_get_lowerstack(sb, ufs);
1202 err = PTR_ERR(oe);
1203 if (IS_ERR(oe))
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001204 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001205
hujianyang71cbad72015-01-15 13:20:57 +08001206 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1207 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001208 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001209 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1210 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001211
Amir Goldstein02bcd152017-06-21 15:28:36 +03001212 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
Miklos Szeredi6e882562017-11-10 09:39:15 +01001213 err = ovl_get_indexdir(ufs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001214 if (err)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001215 goto out_free_oe;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001216
1217 if (!ufs->indexdir)
1218 sb->s_flags |= MS_RDONLY;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001219 }
1220
1221 /* Show index=off/on in /proc/mounts for any of the reasons above */
1222 if (!ufs->indexdir)
1223 ufs->config.index = false;
1224
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001225 /* Never override disk quota limits or use reserved space */
1226 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1227
Vivek Goyal655042c2016-10-14 03:03:36 +02001228 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1229 sb->s_op = &ovl_super_operations;
1230 sb->s_xattr = ovl_xattr_handlers;
1231 sb->s_fs_info = ufs;
1232 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1233
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001234 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001235 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001236 if (!root_dentry)
Miklos Szeredi4155c102017-11-10 09:39:15 +01001237 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001238
1239 mntput(upperpath.mnt);
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001240 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001241 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001242 if (ovl_is_impuredir(upperpath.dentry))
1243 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001244 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001245
1246 root_dentry->d_fsdata = oe;
1247
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001248 /* Root is always merge -> can have whiteouts */
1249 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001250 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1251 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001252
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001253 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001254
1255 return 0;
1256
Miklos Szeredi4155c102017-11-10 09:39:15 +01001257out_free_oe:
1258 ovl_entry_stack_free(oe);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001259 kfree(oe);
Miklos Szeredi4155c102017-11-10 09:39:15 +01001260out_err:
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001261 path_put(&upperpath);
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001262 ovl_free_fs(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001263out:
1264 return err;
1265}
1266
1267static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1268 const char *dev_name, void *raw_data)
1269{
1270 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1271}
1272
1273static struct file_system_type ovl_fs_type = {
1274 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001275 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001276 .mount = ovl_mount,
1277 .kill_sb = kill_anon_super,
1278};
Miklos Szeredief94b182014-11-20 16:39:59 +01001279MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001280
Amir Goldstein13cf1992017-06-12 09:54:40 +03001281static void ovl_inode_init_once(void *foo)
1282{
1283 struct ovl_inode *oi = foo;
1284
1285 inode_init_once(&oi->vfs_inode);
1286}
1287
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001288static int __init ovl_init(void)
1289{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001290 int err;
1291
1292 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1293 sizeof(struct ovl_inode), 0,
1294 (SLAB_RECLAIM_ACCOUNT|
1295 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1296 ovl_inode_init_once);
1297 if (ovl_inode_cachep == NULL)
1298 return -ENOMEM;
1299
1300 err = register_filesystem(&ovl_fs_type);
1301 if (err)
1302 kmem_cache_destroy(ovl_inode_cachep);
1303
1304 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001305}
1306
1307static void __exit ovl_exit(void)
1308{
1309 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001310
1311 /*
1312 * Make sure all delayed rcu free inodes are flushed before we
1313 * destroy cache.
1314 */
1315 rcu_barrier();
1316 kmem_cache_destroy(ovl_inode_cachep);
1317
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001318}
1319
1320module_init(ovl_init);
1321module_exit(ovl_exit);