blob: fea7bd496f2e53e3fd3dd23a2739a90d72a7edfc [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
37static void ovl_dentry_release(struct dentry *dentry)
38{
39 struct ovl_entry *oe = dentry->d_fsdata;
40
41 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +010042 unsigned int i;
43
Miklos Szeredidd662662014-12-13 00:59:43 +010044 for (i = 0; i < oe->numlower; i++)
45 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020046 kfree_rcu(oe, rcu);
47 }
48}
49
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030050static int ovl_check_append_only(struct inode *inode, int flag)
51{
52 /*
53 * This test was moot in vfs may_open() because overlay inode does
54 * not have the S_APPEND flag, so re-check on real upper inode
55 */
56 if (IS_APPEND(inode)) {
57 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
58 return -EPERM;
59 if (flag & O_TRUNC)
60 return -EPERM;
61 }
62
63 return 0;
64}
65
Miklos Szeredi2d902672016-06-30 08:53:27 +020066static struct dentry *ovl_d_real(struct dentry *dentry,
67 const struct inode *inode,
68 unsigned int open_flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040069{
70 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030071 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040072
Miklos Szeredica4c8a32016-12-16 11:02:55 +010073 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040074 if (!inode || inode == d_inode(dentry))
75 return dentry;
76 goto bug;
77 }
78
Miklos Szeredi2d902672016-06-30 08:53:27 +020079 if (d_is_negative(dentry))
80 return dentry;
81
82 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030083 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +020084 if (err)
85 return ERR_PTR(err);
86 }
87
Miklos Szeredid101a122016-03-26 16:14:37 -040088 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030089 if (real && (!inode || inode == d_inode(real))) {
90 if (!inode) {
91 err = ovl_check_append_only(d_inode(real), open_flags);
92 if (err)
93 return ERR_PTR(err);
94 }
Miklos Szeredid101a122016-03-26 16:14:37 -040095 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030096 }
Miklos Szeredid101a122016-03-26 16:14:37 -040097
98 real = ovl_dentry_lower(dentry);
99 if (!real)
100 goto bug;
101
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100102 /* Handle recursion */
103 real = d_real(real, inode, open_flags);
104
Miklos Szeredid101a122016-03-26 16:14:37 -0400105 if (!inode || inode == d_inode(real))
106 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400107bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200108 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400109 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
110 return dentry;
111}
112
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200113static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
114{
115 struct ovl_entry *oe = dentry->d_fsdata;
116 unsigned int i;
117 int ret = 1;
118
119 for (i = 0; i < oe->numlower; i++) {
120 struct dentry *d = oe->lowerstack[i].dentry;
121
122 if (d->d_flags & DCACHE_OP_REVALIDATE) {
123 ret = d->d_op->d_revalidate(d, flags);
124 if (ret < 0)
125 return ret;
126 if (!ret) {
127 if (!(flags & LOOKUP_RCU))
128 d_invalidate(d);
129 return -ESTALE;
130 }
131 }
132 }
133 return 1;
134}
135
136static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
137{
138 struct ovl_entry *oe = dentry->d_fsdata;
139 unsigned int i;
140 int ret = 1;
141
142 for (i = 0; i < oe->numlower; i++) {
143 struct dentry *d = oe->lowerstack[i].dentry;
144
145 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
146 ret = d->d_op->d_weak_revalidate(d, flags);
147 if (ret <= 0)
148 break;
149 }
150 }
151 return ret;
152}
153
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200154static const struct dentry_operations ovl_dentry_operations = {
155 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400156 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200157};
158
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200159static const struct dentry_operations ovl_reval_dentry_operations = {
160 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400161 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200162 .d_revalidate = ovl_dentry_revalidate,
163 .d_weak_revalidate = ovl_dentry_weak_revalidate,
164};
165
Amir Goldstein13cf1992017-06-12 09:54:40 +0300166static struct kmem_cache *ovl_inode_cachep;
167
168static struct inode *ovl_alloc_inode(struct super_block *sb)
169{
170 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
171
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200172 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200173 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200174 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200175 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200176 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200177 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300178 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200179
Amir Goldstein13cf1992017-06-12 09:54:40 +0300180 return &oi->vfs_inode;
181}
182
183static void ovl_i_callback(struct rcu_head *head)
184{
185 struct inode *inode = container_of(head, struct inode, i_rcu);
186
187 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
188}
189
190static void ovl_destroy_inode(struct inode *inode)
191{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200192 struct ovl_inode *oi = OVL_I(inode);
193
194 dput(oi->__upperdentry);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200195 kfree(oi->redirect);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300196 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200197
Amir Goldstein13cf1992017-06-12 09:54:40 +0300198 call_rcu(&inode->i_rcu, ovl_i_callback);
199}
200
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200201static void ovl_put_super(struct super_block *sb)
202{
203 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100204 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200205
206 dput(ufs->workdir);
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300207 ovl_inuse_unlock(ufs->workbasedir);
208 dput(ufs->workbasedir);
209 if (ufs->upper_mnt)
210 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200211 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100212 for (i = 0; i < ufs->numlower; i++)
213 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300214 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215
Erez Zadokf45827e82014-10-24 00:14:38 +0200216 kfree(ufs->config.lowerdir);
217 kfree(ufs->config.upperdir);
218 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200219 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200220 kfree(ufs);
221}
222
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200223static int ovl_sync_fs(struct super_block *sb, int wait)
224{
225 struct ovl_fs *ufs = sb->s_fs_info;
226 struct super_block *upper_sb;
227 int ret;
228
229 if (!ufs->upper_mnt)
230 return 0;
231 upper_sb = ufs->upper_mnt->mnt_sb;
232 if (!upper_sb->s_op->sync_fs)
233 return 0;
234
235 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
236 down_read(&upper_sb->s_umount);
237 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
238 up_read(&upper_sb->s_umount);
239 return ret;
240}
241
Andy Whitcroftcc259632014-10-24 00:14:38 +0200242/**
243 * ovl_statfs
244 * @sb: The overlayfs super block
245 * @buf: The struct kstatfs to fill in with stats
246 *
247 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100248 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200249 */
250static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
251{
252 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
253 struct dentry *root_dentry = dentry->d_sb->s_root;
254 struct path path;
255 int err;
256
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100257 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200258
259 err = vfs_statfs(&path, buf);
260 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100261 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200262 buf->f_type = OVERLAYFS_SUPER_MAGIC;
263 }
264
265 return err;
266}
267
Erez Zadokf45827e82014-10-24 00:14:38 +0200268/**
269 * ovl_show_options
270 *
271 * Prints the mount options for a given superblock.
272 * Returns zero; does not fail.
273 */
274static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
275{
276 struct super_block *sb = dentry->d_sb;
277 struct ovl_fs *ufs = sb->s_fs_info;
278
Kees Cooka068acf2015-09-04 15:44:57 -0700279 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100280 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700281 seq_show_option(m, "upperdir", ufs->config.upperdir);
282 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100283 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200284 if (ufs->config.default_permissions)
285 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200286 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
287 seq_printf(m, ",redirect_dir=%s",
288 ufs->config.redirect_dir ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200289 return 0;
290}
291
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900292static int ovl_remount(struct super_block *sb, int *flags, char *data)
293{
294 struct ovl_fs *ufs = sb->s_fs_info;
295
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200296 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900297 return -EROFS;
298
299 return 0;
300}
301
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200302static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300303 .alloc_inode = ovl_alloc_inode,
304 .destroy_inode = ovl_destroy_inode,
305 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200306 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200307 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200308 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200309 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900310 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311};
312
313enum {
314 OPT_LOWERDIR,
315 OPT_UPPERDIR,
316 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200317 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100318 OPT_REDIRECT_DIR_ON,
319 OPT_REDIRECT_DIR_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320 OPT_ERR,
321};
322
323static const match_table_t ovl_tokens = {
324 {OPT_LOWERDIR, "lowerdir=%s"},
325 {OPT_UPPERDIR, "upperdir=%s"},
326 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200327 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100328 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
329 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330 {OPT_ERR, NULL}
331};
332
Miklos Szeredi91c77942014-11-20 16:40:00 +0100333static char *ovl_next_opt(char **s)
334{
335 char *sbegin = *s;
336 char *p;
337
338 if (sbegin == NULL)
339 return NULL;
340
341 for (p = sbegin; *p; p++) {
342 if (*p == '\\') {
343 p++;
344 if (!*p)
345 break;
346 } else if (*p == ',') {
347 *p = '\0';
348 *s = p + 1;
349 return sbegin;
350 }
351 }
352 *s = NULL;
353 return sbegin;
354}
355
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200356static int ovl_parse_opt(char *opt, struct ovl_config *config)
357{
358 char *p;
359
Miklos Szeredi91c77942014-11-20 16:40:00 +0100360 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200361 int token;
362 substring_t args[MAX_OPT_ARGS];
363
364 if (!*p)
365 continue;
366
367 token = match_token(p, ovl_tokens, args);
368 switch (token) {
369 case OPT_UPPERDIR:
370 kfree(config->upperdir);
371 config->upperdir = match_strdup(&args[0]);
372 if (!config->upperdir)
373 return -ENOMEM;
374 break;
375
376 case OPT_LOWERDIR:
377 kfree(config->lowerdir);
378 config->lowerdir = match_strdup(&args[0]);
379 if (!config->lowerdir)
380 return -ENOMEM;
381 break;
382
383 case OPT_WORKDIR:
384 kfree(config->workdir);
385 config->workdir = match_strdup(&args[0]);
386 if (!config->workdir)
387 return -ENOMEM;
388 break;
389
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200390 case OPT_DEFAULT_PERMISSIONS:
391 config->default_permissions = true;
392 break;
393
Miklos Szeredia6c60652016-12-16 11:02:56 +0100394 case OPT_REDIRECT_DIR_ON:
395 config->redirect_dir = true;
396 break;
397
398 case OPT_REDIRECT_DIR_OFF:
399 config->redirect_dir = false;
400 break;
401
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200402 default:
hujianyangbead55e2015-01-15 13:17:36 +0800403 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200404 return -EINVAL;
405 }
406 }
hujianyang71cbad72015-01-15 13:20:57 +0800407
408 /* Workdir is useless in non-upper mount */
409 if (!config->upperdir && config->workdir) {
410 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
411 config->workdir);
412 kfree(config->workdir);
413 config->workdir = NULL;
414 }
415
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200416 return 0;
417}
418
419#define OVL_WORKDIR_NAME "work"
420
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300421static struct dentry *ovl_workdir_create(struct super_block *sb,
422 struct ovl_fs *ufs,
423 struct dentry *dentry,
424 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200425{
426 struct inode *dir = dentry->d_inode;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300427 struct vfsmount *mnt = ufs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200428 struct dentry *work;
429 int err;
430 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300431 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200432
433 err = mnt_want_write(mnt);
434 if (err)
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300435 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200436
Al Viro59551022016-01-22 15:40:57 -0500437 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300438 locked = true;
439
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200440retry:
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300441 work = lookup_one_len(name, dentry, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200442
443 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200444 struct iattr attr = {
445 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000446 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200447 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200448
449 if (work->d_inode) {
450 err = -EEXIST;
451 if (retried)
452 goto out_dput;
453
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300454 if (persist)
455 goto out_unlock;
456
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200457 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200458 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200459 dput(work);
460 goto retry;
461 }
462
Al Viro32a3d842016-12-04 17:33:17 +0000463 err = ovl_create_real(dir, work,
464 &(struct cattr){.mode = S_IFDIR | 0},
465 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200466 if (err)
467 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200468
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200469 /*
470 * Try to remove POSIX ACL xattrs from workdir. We are good if:
471 *
472 * a) success (there was a POSIX ACL xattr and was removed)
473 * b) -ENODATA (there was no POSIX ACL xattr)
474 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
475 *
476 * There are various other error values that could effectively
477 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
478 * if the xattr name is too long), but the set of filesystems
479 * allowed as upper are limited to "normal" ones, where checking
480 * for the above two errors is sufficient.
481 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200482 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200483 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200484 goto out_dput;
485
486 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200487 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200488 goto out_dput;
489
490 /* Clear any inherited mode bits */
491 inode_lock(work->d_inode);
492 err = notify_change(work, &attr, NULL);
493 inode_unlock(work->d_inode);
494 if (err)
495 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300496 } else {
497 err = PTR_ERR(work);
498 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200499 }
500out_unlock:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200501 mnt_drop_write(mnt);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300502 if (locked)
503 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200504
505 return work;
506
507out_dput:
508 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300509out_err:
510 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
511 ufs->config.workdir, name, -err);
512 sb->s_flags |= MS_RDONLY;
513 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200514 goto out_unlock;
515}
516
Miklos Szeredi91c77942014-11-20 16:40:00 +0100517static void ovl_unescape(char *s)
518{
519 char *d = s;
520
521 for (;; s++, d++) {
522 if (*s == '\\')
523 s++;
524 *d = *s;
525 if (!*s)
526 break;
527 }
528}
529
Miklos Szerediab508822014-12-13 00:59:49 +0100530static int ovl_mount_dir_noesc(const char *name, struct path *path)
531{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100532 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100533
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100534 if (!*name) {
535 pr_err("overlayfs: empty lowerdir\n");
536 goto out;
537 }
Miklos Szerediab508822014-12-13 00:59:49 +0100538 err = kern_path(name, LOOKUP_FOLLOW, path);
539 if (err) {
540 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
541 goto out;
542 }
543 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200544 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100545 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
546 goto out_put;
547 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100548 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100549 pr_err("overlayfs: '%s' not a directory\n", name);
550 goto out_put;
551 }
552 return 0;
553
554out_put:
555 path_put(path);
556out:
557 return err;
558}
559
560static int ovl_mount_dir(const char *name, struct path *path)
561{
562 int err = -ENOMEM;
563 char *tmp = kstrdup(name, GFP_KERNEL);
564
565 if (tmp) {
566 ovl_unescape(tmp);
567 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200568
569 if (!err)
570 if (ovl_dentry_remote(path->dentry)) {
571 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
572 tmp);
573 path_put(path);
574 err = -EINVAL;
575 }
Miklos Szerediab508822014-12-13 00:59:49 +0100576 kfree(tmp);
577 }
578 return err;
579}
580
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100581static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
582 const char *name)
583{
584 struct kstatfs statfs;
585 int err = vfs_statfs(path, &statfs);
586
587 if (err)
588 pr_err("overlayfs: statfs failed on '%s'\n", name);
589 else
590 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
591
592 return err;
593}
594
595static int ovl_lower_dir(const char *name, struct path *path,
596 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100597{
598 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100599
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100600 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100601 if (err)
602 goto out;
603
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100604 err = ovl_check_namelen(path, ofs, name);
605 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100606 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100607
Miklos Szerediab508822014-12-13 00:59:49 +0100608 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
609
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200610 if (ovl_dentry_remote(path->dentry))
611 *remote = true;
612
Miklos Szerediab508822014-12-13 00:59:49 +0100613 return 0;
614
615out_put:
616 path_put(path);
617out:
618 return err;
619}
620
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200621/* Workdir should not be subdir of upperdir and vice versa */
622static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
623{
624 bool ok = false;
625
626 if (workdir != upperdir) {
627 ok = (lock_rename(workdir, upperdir) == NULL);
628 unlock_rename(workdir, upperdir);
629 }
630 return ok;
631}
632
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100633static unsigned int ovl_split_lowerdirs(char *str)
634{
635 unsigned int ctr = 1;
636 char *s, *d;
637
638 for (s = d = str;; s++, d++) {
639 if (*s == '\\') {
640 s++;
641 } else if (*s == ':') {
642 *d = '\0';
643 ctr++;
644 continue;
645 }
646 *d = *s;
647 if (!*s)
648 break;
649 }
650 return ctr;
651}
652
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200653static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200654ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
655 struct dentry *dentry, struct inode *inode,
656 const char *name, void *buffer, size_t size)
657{
658 return ovl_xattr_get(dentry, handler->name, buffer, size);
659}
660
661static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200662ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
663 struct dentry *dentry, struct inode *inode,
664 const char *name, const void *value,
665 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200666{
667 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200668 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200669 struct posix_acl *acl = NULL;
670 int err;
671
672 /* Check that everything is OK before copy-up */
673 if (value) {
674 acl = posix_acl_from_xattr(&init_user_ns, value, size);
675 if (IS_ERR(acl))
676 return PTR_ERR(acl);
677 }
678 err = -EOPNOTSUPP;
679 if (!IS_POSIXACL(d_inode(workdir)))
680 goto out_acl_release;
681 if (!realinode->i_op->set_acl)
682 goto out_acl_release;
683 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
684 err = acl ? -EACCES : 0;
685 goto out_acl_release;
686 }
687 err = -EPERM;
688 if (!inode_owner_or_capable(inode))
689 goto out_acl_release;
690
691 posix_acl_release(acl);
692
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100693 /*
694 * Check if sgid bit needs to be cleared (actual setacl operation will
695 * be done with mounter's capabilities and so that won't do it for us).
696 */
697 if (unlikely(inode->i_mode & S_ISGID) &&
698 handler->flags == ACL_TYPE_ACCESS &&
699 !in_group_p(inode->i_gid) &&
700 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
701 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
702
703 err = ovl_setattr(dentry, &iattr);
704 if (err)
705 return err;
706 }
707
Miklos Szeredice315132016-09-01 11:12:00 +0200708 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
709 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200710 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200711
712 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200713
714out_acl_release:
715 posix_acl_release(acl);
716 return err;
717}
718
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200719static int ovl_own_xattr_get(const struct xattr_handler *handler,
720 struct dentry *dentry, struct inode *inode,
721 const char *name, void *buffer, size_t size)
722{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200723 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200724}
725
Miklos Szeredid837a492016-07-29 12:05:24 +0200726static int ovl_own_xattr_set(const struct xattr_handler *handler,
727 struct dentry *dentry, struct inode *inode,
728 const char *name, const void *value,
729 size_t size, int flags)
730{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200731 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200732}
733
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200734static int ovl_other_xattr_get(const struct xattr_handler *handler,
735 struct dentry *dentry, struct inode *inode,
736 const char *name, void *buffer, size_t size)
737{
738 return ovl_xattr_get(dentry, name, buffer, size);
739}
740
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200741static int ovl_other_xattr_set(const struct xattr_handler *handler,
742 struct dentry *dentry, struct inode *inode,
743 const char *name, const void *value,
744 size_t size, int flags)
745{
746 return ovl_xattr_set(dentry, name, value, size, flags);
747}
748
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200749static const struct xattr_handler __maybe_unused
750ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200751 .name = XATTR_NAME_POSIX_ACL_ACCESS,
752 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200753 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200754 .set = ovl_posix_acl_xattr_set,
755};
756
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200757static const struct xattr_handler __maybe_unused
758ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200759 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
760 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200761 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200762 .set = ovl_posix_acl_xattr_set,
763};
764
765static const struct xattr_handler ovl_own_xattr_handler = {
766 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200767 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200768 .set = ovl_own_xattr_set,
769};
770
771static const struct xattr_handler ovl_other_xattr_handler = {
772 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200773 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200774 .set = ovl_other_xattr_set,
775};
776
777static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200778#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200779 &ovl_posix_acl_access_xattr_handler,
780 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200781#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200782 &ovl_own_xattr_handler,
783 &ovl_other_xattr_handler,
784 NULL
785};
786
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200787static int ovl_fill_super(struct super_block *sb, void *data, int silent)
788{
Kees Cook33006cd2017-03-29 14:02:19 -0700789 struct path upperpath = { };
790 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200791 struct dentry *root_dentry;
792 struct ovl_entry *oe;
793 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100794 struct path *stack = NULL;
795 char *lowertmp;
796 char *lower;
797 unsigned int numlower;
798 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100799 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200800 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300801 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200802 int err;
803
Erez Zadokf45827e82014-10-24 00:14:38 +0200804 err = -ENOMEM;
805 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
806 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200807 goto out;
808
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100809 ufs->config.redirect_dir = ovl_redirect_dir_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200810 err = ovl_parse_opt((char *) data, &ufs->config);
811 if (err)
812 goto out_free_config;
813
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200814 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100815 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300816 if (!silent)
817 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200818 goto out_free_config;
819 }
820
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100821 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100822 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100823 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100824 if (!ufs->config.workdir) {
825 pr_err("overlayfs: missing 'workdir'\n");
826 goto out_free_config;
827 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200828
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100829 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
830 if (err)
831 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100832
hujianyang71cbad72015-01-15 13:20:57 +0800833 /* Upper fs should not be r/o */
834 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
835 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
836 err = -EINVAL;
837 goto out_put_upperpath;
838 }
839
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100840 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
841 if (err)
842 goto out_put_upperpath;
843
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300844 err = -EBUSY;
845 if (!ovl_inuse_trylock(upperpath.dentry)) {
846 pr_err("overlayfs: upperdir is in-use by another mount\n");
847 goto out_put_upperpath;
848 }
849
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100850 err = ovl_mount_dir(ufs->config.workdir, &workpath);
851 if (err)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300852 goto out_unlock_upperdentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100853
hujianyang2f83fd82015-01-06 12:52:13 +0800854 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100855 if (upperpath.mnt != workpath.mnt) {
856 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
857 goto out_put_workpath;
858 }
859 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
860 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
861 goto out_put_workpath;
862 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300863
864 err = -EBUSY;
865 if (!ovl_inuse_trylock(workpath.dentry)) {
866 pr_err("overlayfs: workdir is in-use by another mount\n");
867 goto out_put_workpath;
868 }
869
870 ufs->workbasedir = workpath.dentry;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100871 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100872 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100873 err = -ENOMEM;
874 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
875 if (!lowertmp)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300876 goto out_unlock_workdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200877
878 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100879 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800880 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100881 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800882 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100883 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800884 } else if (!ufs->config.upperdir && stacklen == 1) {
885 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
886 goto out_free_lowertmp;
887 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100888
Geliang Tang313684c2016-11-18 23:02:46 +0800889 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100890 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
891 if (!stack)
892 goto out_free_lowertmp;
893
Geliang Tang313684c2016-11-18 23:02:46 +0800894 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100895 lower = lowertmp;
896 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100897 err = ovl_lower_dir(lower, &stack[numlower], ufs,
898 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100899 if (err)
900 goto out_put_lowerpath;
901
902 lower = strchr(lower, '\0') + 1;
903 }
904
905 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100906 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200907 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
908 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100909 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200910 }
911
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100912 if (ufs->config.upperdir) {
913 ufs->upper_mnt = clone_private_mount(&upperpath);
914 err = PTR_ERR(ufs->upper_mnt);
915 if (IS_ERR(ufs->upper_mnt)) {
916 pr_err("overlayfs: failed to clone upperpath\n");
917 goto out_put_lowerpath;
918 }
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300919
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200920 /* Don't inherit atime flags */
921 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
922
923 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100924
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300925 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
926 OVL_WORKDIR_NAME, false);
Vivek Goyal45aebea2016-02-22 09:28:34 -0500927 /*
928 * Upper should support d_type, else whiteouts are visible.
929 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400930 * iterate_dir() on workdir. This check requires successful
931 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500932 */
Vivek Goyal21765192016-05-20 09:04:26 -0400933 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200934 struct dentry *temp;
935
Vivek Goyal21765192016-05-20 09:04:26 -0400936 err = ovl_check_d_type_supported(&workpath);
937 if (err < 0)
938 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500939
Vivek Goyale7c0b592016-07-01 10:02:44 -0400940 /*
941 * We allowed this configuration and don't want to
942 * break users over kernel upgrade. So warn instead
943 * of erroring out.
944 */
945 if (!err)
946 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200947
948 /* Check if upper/work fs supports O_TMPFILE */
949 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
950 ufs->tmpfile = !IS_ERR(temp);
951 if (ufs->tmpfile)
952 dput(temp);
953 else
954 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +0300955
956 /*
957 * Check if upper/work fs supports trusted.overlay.*
958 * xattr
959 */
960 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
961 "0", 1, 0);
962 if (err) {
963 ufs->noxattr = true;
964 pr_warn("overlayfs: upper fs does not support xattr.\n");
965 } else {
966 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
967 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500968 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200969 }
970
hujianyang2f83fd82015-01-06 12:52:13 +0800971 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100972 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +0100973 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100974 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100975 for (i = 0; i < numlower; i++) {
976 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +0100977
hujianyang2f83fd82015-01-06 12:52:13 +0800978 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100979 if (IS_ERR(mnt)) {
980 pr_err("overlayfs: failed to clone lowerpath\n");
981 goto out_put_lower_mnt;
982 }
983 /*
984 * Make lower_mnt R/O. That way fchmod/fchown on lower file
985 * will fail instead of modifying lower fs.
986 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200987 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100988
989 ufs->lower_mnt[ufs->numlower] = mnt;
990 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400991
992 /* Check if all lower layers are on same sb */
993 if (i == 0)
994 ufs->same_sb = mnt->mnt_sb;
995 else if (ufs->same_sb != mnt->mnt_sb)
996 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200997 }
998
hujianyang71cbad72015-01-15 13:20:57 +0800999 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1000 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001001 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001002 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1003 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001004
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001005 if (remote)
1006 sb->s_d_op = &ovl_reval_dentry_operations;
1007 else
1008 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001009
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001010 ufs->creator_cred = cred = prepare_creds();
1011 if (!cred)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001012 goto out_put_lower_mnt;
1013
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001014 /* Never override disk quota limits or use reserved space */
1015 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1016
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001017 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001018 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001019 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001020 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001021
Vivek Goyal655042c2016-10-14 03:03:36 +02001022 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1023 sb->s_op = &ovl_super_operations;
1024 sb->s_xattr = ovl_xattr_handlers;
1025 sb->s_fs_info = ufs;
1026 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1027
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001028 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001029 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001030 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001031
1032 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001033 for (i = 0; i < numlower; i++)
1034 mntput(stack[i].mnt);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001035 mntput(workpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001036 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001037
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001038 if (upperpath.dentry) {
Miklos Szeredi13c72072017-07-04 22:03:16 +02001039 if (ovl_is_impuredir(upperpath.dentry))
1040 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001041 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001042 for (i = 0; i < numlower; i++) {
1043 oe->lowerstack[i].dentry = stack[i].dentry;
1044 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1045 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001046 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001047
1048 root_dentry->d_fsdata = oe;
1049
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001050 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1051 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001052
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001053 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001054
1055 return 0;
1056
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001057out_free_oe:
1058 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001059out_put_cred:
1060 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001061out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001062 for (i = 0; i < ufs->numlower; i++)
1063 mntput(ufs->lower_mnt[i]);
1064 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001065out_put_workdir:
1066 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001067 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001068out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001069 for (i = 0; i < numlower; i++)
1070 path_put(&stack[i]);
1071 kfree(stack);
1072out_free_lowertmp:
1073 kfree(lowertmp);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001074out_unlock_workdentry:
1075 ovl_inuse_unlock(workpath.dentry);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001076out_put_workpath:
1077 path_put(&workpath);
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001078out_unlock_upperdentry:
1079 ovl_inuse_unlock(upperpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001080out_put_upperpath:
1081 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001082out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001083 kfree(ufs->config.lowerdir);
1084 kfree(ufs->config.upperdir);
1085 kfree(ufs->config.workdir);
1086 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001087out:
1088 return err;
1089}
1090
1091static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1092 const char *dev_name, void *raw_data)
1093{
1094 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1095}
1096
1097static struct file_system_type ovl_fs_type = {
1098 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001099 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001100 .mount = ovl_mount,
1101 .kill_sb = kill_anon_super,
1102};
Miklos Szeredief94b182014-11-20 16:39:59 +01001103MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001104
Amir Goldstein13cf1992017-06-12 09:54:40 +03001105static void ovl_inode_init_once(void *foo)
1106{
1107 struct ovl_inode *oi = foo;
1108
1109 inode_init_once(&oi->vfs_inode);
1110}
1111
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001112static int __init ovl_init(void)
1113{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001114 int err;
1115
1116 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1117 sizeof(struct ovl_inode), 0,
1118 (SLAB_RECLAIM_ACCOUNT|
1119 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1120 ovl_inode_init_once);
1121 if (ovl_inode_cachep == NULL)
1122 return -ENOMEM;
1123
1124 err = register_filesystem(&ovl_fs_type);
1125 if (err)
1126 kmem_cache_destroy(ovl_inode_cachep);
1127
1128 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001129}
1130
1131static void __exit ovl_exit(void)
1132{
1133 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001134
1135 /*
1136 * Make sure all delayed rcu free inodes are flushed before we
1137 * destroy cache.
1138 */
1139 rcu_barrier();
1140 kmem_cache_destroy(ovl_inode_cachep);
1141
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001142}
1143
1144module_init(ovl_init);
1145module_exit(ovl_exit);