blob: 7346518846c5a55e3875a3567201ca9bba86ea6a [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 Szeredicf31c462017-07-04 22:03:16 +0200172 oi->redirect = NULL;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200173 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200174 oi->lower = NULL;
175
Amir Goldstein13cf1992017-06-12 09:54:40 +0300176 return &oi->vfs_inode;
177}
178
179static void ovl_i_callback(struct rcu_head *head)
180{
181 struct inode *inode = container_of(head, struct inode, i_rcu);
182
183 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
184}
185
186static void ovl_destroy_inode(struct inode *inode)
187{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200188 struct ovl_inode *oi = OVL_I(inode);
189
190 dput(oi->__upperdentry);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200191 kfree(oi->redirect);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200192
Amir Goldstein13cf1992017-06-12 09:54:40 +0300193 call_rcu(&inode->i_rcu, ovl_i_callback);
194}
195
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196static void ovl_put_super(struct super_block *sb)
197{
198 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100199 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200200
201 dput(ufs->workdir);
202 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100203 for (i = 0; i < ufs->numlower; i++)
204 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300205 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200206
Erez Zadokf45827e82014-10-24 00:14:38 +0200207 kfree(ufs->config.lowerdir);
208 kfree(ufs->config.upperdir);
209 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200210 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200211 kfree(ufs);
212}
213
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200214static int ovl_sync_fs(struct super_block *sb, int wait)
215{
216 struct ovl_fs *ufs = sb->s_fs_info;
217 struct super_block *upper_sb;
218 int ret;
219
220 if (!ufs->upper_mnt)
221 return 0;
222 upper_sb = ufs->upper_mnt->mnt_sb;
223 if (!upper_sb->s_op->sync_fs)
224 return 0;
225
226 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
227 down_read(&upper_sb->s_umount);
228 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
229 up_read(&upper_sb->s_umount);
230 return ret;
231}
232
Andy Whitcroftcc259632014-10-24 00:14:38 +0200233/**
234 * ovl_statfs
235 * @sb: The overlayfs super block
236 * @buf: The struct kstatfs to fill in with stats
237 *
238 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100239 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200240 */
241static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
242{
243 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
244 struct dentry *root_dentry = dentry->d_sb->s_root;
245 struct path path;
246 int err;
247
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100248 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200249
250 err = vfs_statfs(&path, buf);
251 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100252 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200253 buf->f_type = OVERLAYFS_SUPER_MAGIC;
254 }
255
256 return err;
257}
258
Erez Zadokf45827e82014-10-24 00:14:38 +0200259/**
260 * ovl_show_options
261 *
262 * Prints the mount options for a given superblock.
263 * Returns zero; does not fail.
264 */
265static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
266{
267 struct super_block *sb = dentry->d_sb;
268 struct ovl_fs *ufs = sb->s_fs_info;
269
Kees Cooka068acf2015-09-04 15:44:57 -0700270 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100271 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700272 seq_show_option(m, "upperdir", ufs->config.upperdir);
273 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100274 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200275 if (ufs->config.default_permissions)
276 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200277 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
278 seq_printf(m, ",redirect_dir=%s",
279 ufs->config.redirect_dir ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200280 return 0;
281}
282
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900283static int ovl_remount(struct super_block *sb, int *flags, char *data)
284{
285 struct ovl_fs *ufs = sb->s_fs_info;
286
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200287 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900288 return -EROFS;
289
290 return 0;
291}
292
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200293static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300294 .alloc_inode = ovl_alloc_inode,
295 .destroy_inode = ovl_destroy_inode,
296 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200297 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200298 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200299 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200300 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900301 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200302};
303
304enum {
305 OPT_LOWERDIR,
306 OPT_UPPERDIR,
307 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200308 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100309 OPT_REDIRECT_DIR_ON,
310 OPT_REDIRECT_DIR_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311 OPT_ERR,
312};
313
314static const match_table_t ovl_tokens = {
315 {OPT_LOWERDIR, "lowerdir=%s"},
316 {OPT_UPPERDIR, "upperdir=%s"},
317 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200318 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100319 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
320 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200321 {OPT_ERR, NULL}
322};
323
Miklos Szeredi91c77942014-11-20 16:40:00 +0100324static char *ovl_next_opt(char **s)
325{
326 char *sbegin = *s;
327 char *p;
328
329 if (sbegin == NULL)
330 return NULL;
331
332 for (p = sbegin; *p; p++) {
333 if (*p == '\\') {
334 p++;
335 if (!*p)
336 break;
337 } else if (*p == ',') {
338 *p = '\0';
339 *s = p + 1;
340 return sbegin;
341 }
342 }
343 *s = NULL;
344 return sbegin;
345}
346
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200347static int ovl_parse_opt(char *opt, struct ovl_config *config)
348{
349 char *p;
350
Miklos Szeredi91c77942014-11-20 16:40:00 +0100351 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200352 int token;
353 substring_t args[MAX_OPT_ARGS];
354
355 if (!*p)
356 continue;
357
358 token = match_token(p, ovl_tokens, args);
359 switch (token) {
360 case OPT_UPPERDIR:
361 kfree(config->upperdir);
362 config->upperdir = match_strdup(&args[0]);
363 if (!config->upperdir)
364 return -ENOMEM;
365 break;
366
367 case OPT_LOWERDIR:
368 kfree(config->lowerdir);
369 config->lowerdir = match_strdup(&args[0]);
370 if (!config->lowerdir)
371 return -ENOMEM;
372 break;
373
374 case OPT_WORKDIR:
375 kfree(config->workdir);
376 config->workdir = match_strdup(&args[0]);
377 if (!config->workdir)
378 return -ENOMEM;
379 break;
380
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200381 case OPT_DEFAULT_PERMISSIONS:
382 config->default_permissions = true;
383 break;
384
Miklos Szeredia6c60652016-12-16 11:02:56 +0100385 case OPT_REDIRECT_DIR_ON:
386 config->redirect_dir = true;
387 break;
388
389 case OPT_REDIRECT_DIR_OFF:
390 config->redirect_dir = false;
391 break;
392
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200393 default:
hujianyangbead55e2015-01-15 13:17:36 +0800394 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200395 return -EINVAL;
396 }
397 }
hujianyang71cbad72015-01-15 13:20:57 +0800398
399 /* Workdir is useless in non-upper mount */
400 if (!config->upperdir && config->workdir) {
401 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
402 config->workdir);
403 kfree(config->workdir);
404 config->workdir = NULL;
405 }
406
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200407 return 0;
408}
409
410#define OVL_WORKDIR_NAME "work"
411
412static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
413 struct dentry *dentry)
414{
415 struct inode *dir = dentry->d_inode;
416 struct dentry *work;
417 int err;
418 bool retried = false;
419
420 err = mnt_want_write(mnt);
421 if (err)
422 return ERR_PTR(err);
423
Al Viro59551022016-01-22 15:40:57 -0500424 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200425retry:
426 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
427 strlen(OVL_WORKDIR_NAME));
428
429 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200430 struct iattr attr = {
431 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000432 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200433 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200434
435 if (work->d_inode) {
436 err = -EEXIST;
437 if (retried)
438 goto out_dput;
439
440 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200441 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200442 dput(work);
443 goto retry;
444 }
445
Al Viro32a3d842016-12-04 17:33:17 +0000446 err = ovl_create_real(dir, work,
447 &(struct cattr){.mode = S_IFDIR | 0},
448 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200449 if (err)
450 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200451
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200452 /*
453 * Try to remove POSIX ACL xattrs from workdir. We are good if:
454 *
455 * a) success (there was a POSIX ACL xattr and was removed)
456 * b) -ENODATA (there was no POSIX ACL xattr)
457 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
458 *
459 * There are various other error values that could effectively
460 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
461 * if the xattr name is too long), but the set of filesystems
462 * allowed as upper are limited to "normal" ones, where checking
463 * for the above two errors is sufficient.
464 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200465 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200466 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200467 goto out_dput;
468
469 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200470 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200471 goto out_dput;
472
473 /* Clear any inherited mode bits */
474 inode_lock(work->d_inode);
475 err = notify_change(work, &attr, NULL);
476 inode_unlock(work->d_inode);
477 if (err)
478 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200479 }
480out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500481 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200482 mnt_drop_write(mnt);
483
484 return work;
485
486out_dput:
487 dput(work);
488 work = ERR_PTR(err);
489 goto out_unlock;
490}
491
Miklos Szeredi91c77942014-11-20 16:40:00 +0100492static void ovl_unescape(char *s)
493{
494 char *d = s;
495
496 for (;; s++, d++) {
497 if (*s == '\\')
498 s++;
499 *d = *s;
500 if (!*s)
501 break;
502 }
503}
504
Miklos Szerediab508822014-12-13 00:59:49 +0100505static int ovl_mount_dir_noesc(const char *name, struct path *path)
506{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100507 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100508
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100509 if (!*name) {
510 pr_err("overlayfs: empty lowerdir\n");
511 goto out;
512 }
Miklos Szerediab508822014-12-13 00:59:49 +0100513 err = kern_path(name, LOOKUP_FOLLOW, path);
514 if (err) {
515 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
516 goto out;
517 }
518 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200519 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100520 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
521 goto out_put;
522 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100523 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100524 pr_err("overlayfs: '%s' not a directory\n", name);
525 goto out_put;
526 }
527 return 0;
528
529out_put:
530 path_put(path);
531out:
532 return err;
533}
534
535static int ovl_mount_dir(const char *name, struct path *path)
536{
537 int err = -ENOMEM;
538 char *tmp = kstrdup(name, GFP_KERNEL);
539
540 if (tmp) {
541 ovl_unescape(tmp);
542 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200543
544 if (!err)
545 if (ovl_dentry_remote(path->dentry)) {
546 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
547 tmp);
548 path_put(path);
549 err = -EINVAL;
550 }
Miklos Szerediab508822014-12-13 00:59:49 +0100551 kfree(tmp);
552 }
553 return err;
554}
555
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100556static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
557 const char *name)
558{
559 struct kstatfs statfs;
560 int err = vfs_statfs(path, &statfs);
561
562 if (err)
563 pr_err("overlayfs: statfs failed on '%s'\n", name);
564 else
565 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
566
567 return err;
568}
569
570static int ovl_lower_dir(const char *name, struct path *path,
571 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100572{
573 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100574
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100575 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100576 if (err)
577 goto out;
578
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100579 err = ovl_check_namelen(path, ofs, name);
580 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100581 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100582
Miklos Szerediab508822014-12-13 00:59:49 +0100583 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
584
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200585 if (ovl_dentry_remote(path->dentry))
586 *remote = true;
587
Miklos Szerediab508822014-12-13 00:59:49 +0100588 return 0;
589
590out_put:
591 path_put(path);
592out:
593 return err;
594}
595
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200596/* Workdir should not be subdir of upperdir and vice versa */
597static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
598{
599 bool ok = false;
600
601 if (workdir != upperdir) {
602 ok = (lock_rename(workdir, upperdir) == NULL);
603 unlock_rename(workdir, upperdir);
604 }
605 return ok;
606}
607
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100608static unsigned int ovl_split_lowerdirs(char *str)
609{
610 unsigned int ctr = 1;
611 char *s, *d;
612
613 for (s = d = str;; s++, d++) {
614 if (*s == '\\') {
615 s++;
616 } else if (*s == ':') {
617 *d = '\0';
618 ctr++;
619 continue;
620 }
621 *d = *s;
622 if (!*s)
623 break;
624 }
625 return ctr;
626}
627
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200628static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200629ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
630 struct dentry *dentry, struct inode *inode,
631 const char *name, void *buffer, size_t size)
632{
633 return ovl_xattr_get(dentry, handler->name, buffer, size);
634}
635
636static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200637ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
638 struct dentry *dentry, struct inode *inode,
639 const char *name, const void *value,
640 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200641{
642 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200643 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200644 struct posix_acl *acl = NULL;
645 int err;
646
647 /* Check that everything is OK before copy-up */
648 if (value) {
649 acl = posix_acl_from_xattr(&init_user_ns, value, size);
650 if (IS_ERR(acl))
651 return PTR_ERR(acl);
652 }
653 err = -EOPNOTSUPP;
654 if (!IS_POSIXACL(d_inode(workdir)))
655 goto out_acl_release;
656 if (!realinode->i_op->set_acl)
657 goto out_acl_release;
658 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
659 err = acl ? -EACCES : 0;
660 goto out_acl_release;
661 }
662 err = -EPERM;
663 if (!inode_owner_or_capable(inode))
664 goto out_acl_release;
665
666 posix_acl_release(acl);
667
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100668 /*
669 * Check if sgid bit needs to be cleared (actual setacl operation will
670 * be done with mounter's capabilities and so that won't do it for us).
671 */
672 if (unlikely(inode->i_mode & S_ISGID) &&
673 handler->flags == ACL_TYPE_ACCESS &&
674 !in_group_p(inode->i_gid) &&
675 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
676 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
677
678 err = ovl_setattr(dentry, &iattr);
679 if (err)
680 return err;
681 }
682
Miklos Szeredice315132016-09-01 11:12:00 +0200683 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
684 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200685 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200686
687 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200688
689out_acl_release:
690 posix_acl_release(acl);
691 return err;
692}
693
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200694static int ovl_own_xattr_get(const struct xattr_handler *handler,
695 struct dentry *dentry, struct inode *inode,
696 const char *name, void *buffer, size_t size)
697{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200698 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200699}
700
Miklos Szeredid837a492016-07-29 12:05:24 +0200701static int ovl_own_xattr_set(const struct xattr_handler *handler,
702 struct dentry *dentry, struct inode *inode,
703 const char *name, const void *value,
704 size_t size, int flags)
705{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200706 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200707}
708
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200709static int ovl_other_xattr_get(const struct xattr_handler *handler,
710 struct dentry *dentry, struct inode *inode,
711 const char *name, void *buffer, size_t size)
712{
713 return ovl_xattr_get(dentry, name, buffer, size);
714}
715
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200716static int ovl_other_xattr_set(const struct xattr_handler *handler,
717 struct dentry *dentry, struct inode *inode,
718 const char *name, const void *value,
719 size_t size, int flags)
720{
721 return ovl_xattr_set(dentry, name, value, size, flags);
722}
723
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200724static const struct xattr_handler __maybe_unused
725ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200726 .name = XATTR_NAME_POSIX_ACL_ACCESS,
727 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200728 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200729 .set = ovl_posix_acl_xattr_set,
730};
731
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200732static const struct xattr_handler __maybe_unused
733ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200734 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
735 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200736 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200737 .set = ovl_posix_acl_xattr_set,
738};
739
740static const struct xattr_handler ovl_own_xattr_handler = {
741 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200742 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200743 .set = ovl_own_xattr_set,
744};
745
746static const struct xattr_handler ovl_other_xattr_handler = {
747 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200748 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200749 .set = ovl_other_xattr_set,
750};
751
752static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200753#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200754 &ovl_posix_acl_access_xattr_handler,
755 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200756#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200757 &ovl_own_xattr_handler,
758 &ovl_other_xattr_handler,
759 NULL
760};
761
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200762static int ovl_fill_super(struct super_block *sb, void *data, int silent)
763{
Kees Cook33006cd2017-03-29 14:02:19 -0700764 struct path upperpath = { };
765 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200766 struct dentry *root_dentry;
767 struct ovl_entry *oe;
768 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100769 struct path *stack = NULL;
770 char *lowertmp;
771 char *lower;
772 unsigned int numlower;
773 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100774 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200775 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300776 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200777 int err;
778
Erez Zadokf45827e82014-10-24 00:14:38 +0200779 err = -ENOMEM;
780 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
781 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200782 goto out;
783
Amir Goldstein39d3d602017-01-17 06:34:56 +0200784 init_waitqueue_head(&ufs->copyup_wq);
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100785 ufs->config.redirect_dir = ovl_redirect_dir_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200786 err = ovl_parse_opt((char *) data, &ufs->config);
787 if (err)
788 goto out_free_config;
789
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200790 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100791 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300792 if (!silent)
793 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200794 goto out_free_config;
795 }
796
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100797 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100798 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100799 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100800 if (!ufs->config.workdir) {
801 pr_err("overlayfs: missing 'workdir'\n");
802 goto out_free_config;
803 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200804
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100805 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
806 if (err)
807 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100808
hujianyang71cbad72015-01-15 13:20:57 +0800809 /* Upper fs should not be r/o */
810 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
811 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
812 err = -EINVAL;
813 goto out_put_upperpath;
814 }
815
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100816 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
817 if (err)
818 goto out_put_upperpath;
819
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100820 err = ovl_mount_dir(ufs->config.workdir, &workpath);
821 if (err)
822 goto out_put_upperpath;
823
hujianyang2f83fd82015-01-06 12:52:13 +0800824 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100825 if (upperpath.mnt != workpath.mnt) {
826 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
827 goto out_put_workpath;
828 }
829 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
830 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
831 goto out_put_workpath;
832 }
833 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100834 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100835 err = -ENOMEM;
836 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
837 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100838 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200839
840 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100841 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800842 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100843 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800844 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100845 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800846 } else if (!ufs->config.upperdir && stacklen == 1) {
847 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
848 goto out_free_lowertmp;
849 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100850
Geliang Tang313684c2016-11-18 23:02:46 +0800851 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100852 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
853 if (!stack)
854 goto out_free_lowertmp;
855
Geliang Tang313684c2016-11-18 23:02:46 +0800856 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100857 lower = lowertmp;
858 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100859 err = ovl_lower_dir(lower, &stack[numlower], ufs,
860 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100861 if (err)
862 goto out_put_lowerpath;
863
864 lower = strchr(lower, '\0') + 1;
865 }
866
867 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100868 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200869 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
870 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100871 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200872 }
873
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100874 if (ufs->config.upperdir) {
875 ufs->upper_mnt = clone_private_mount(&upperpath);
876 err = PTR_ERR(ufs->upper_mnt);
877 if (IS_ERR(ufs->upper_mnt)) {
878 pr_err("overlayfs: failed to clone upperpath\n");
879 goto out_put_lowerpath;
880 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200881 /* Don't inherit atime flags */
882 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
883
884 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100885
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100886 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
887 err = PTR_ERR(ufs->workdir);
888 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200889 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
890 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
891 sb->s_flags |= MS_RDONLY;
892 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100893 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500894
895 /*
896 * Upper should support d_type, else whiteouts are visible.
897 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400898 * iterate_dir() on workdir. This check requires successful
899 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500900 */
Vivek Goyal21765192016-05-20 09:04:26 -0400901 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200902 struct dentry *temp;
903
Vivek Goyal21765192016-05-20 09:04:26 -0400904 err = ovl_check_d_type_supported(&workpath);
905 if (err < 0)
906 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500907
Vivek Goyale7c0b592016-07-01 10:02:44 -0400908 /*
909 * We allowed this configuration and don't want to
910 * break users over kernel upgrade. So warn instead
911 * of erroring out.
912 */
913 if (!err)
914 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200915
916 /* Check if upper/work fs supports O_TMPFILE */
917 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
918 ufs->tmpfile = !IS_ERR(temp);
919 if (ufs->tmpfile)
920 dput(temp);
921 else
922 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +0300923
924 /*
925 * Check if upper/work fs supports trusted.overlay.*
926 * xattr
927 */
928 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
929 "0", 1, 0);
930 if (err) {
931 ufs->noxattr = true;
932 pr_warn("overlayfs: upper fs does not support xattr.\n");
933 } else {
934 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
935 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500936 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200937 }
938
hujianyang2f83fd82015-01-06 12:52:13 +0800939 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100940 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +0100941 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100942 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100943 for (i = 0; i < numlower; i++) {
944 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +0100945
hujianyang2f83fd82015-01-06 12:52:13 +0800946 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100947 if (IS_ERR(mnt)) {
948 pr_err("overlayfs: failed to clone lowerpath\n");
949 goto out_put_lower_mnt;
950 }
951 /*
952 * Make lower_mnt R/O. That way fchmod/fchown on lower file
953 * will fail instead of modifying lower fs.
954 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200955 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100956
957 ufs->lower_mnt[ufs->numlower] = mnt;
958 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400959
960 /* Check if all lower layers are on same sb */
961 if (i == 0)
962 ufs->same_sb = mnt->mnt_sb;
963 else if (ufs->same_sb != mnt->mnt_sb)
964 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200965 }
966
hujianyang71cbad72015-01-15 13:20:57 +0800967 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
968 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200969 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400970 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
971 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200972
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200973 if (remote)
974 sb->s_d_op = &ovl_reval_dentry_operations;
975 else
976 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200977
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300978 ufs->creator_cred = cred = prepare_creds();
979 if (!cred)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200980 goto out_put_lower_mnt;
981
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300982 /* Never override disk quota limits or use reserved space */
983 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
984
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200985 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100986 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100987 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200988 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200989
Vivek Goyal655042c2016-10-14 03:03:36 +0200990 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
991 sb->s_op = &ovl_super_operations;
992 sb->s_xattr = ovl_xattr_handlers;
993 sb->s_fs_info = ufs;
994 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
995
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100996 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200997 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100998 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200999
1000 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001001 for (i = 0; i < numlower; i++)
1002 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001003 path_put(&workpath);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001004 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001005
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001006 if (upperpath.dentry) {
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001007 oe->impure = ovl_is_impuredir(upperpath.dentry);
1008 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001009 for (i = 0; i < numlower; i++) {
1010 oe->lowerstack[i].dentry = stack[i].dentry;
1011 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1012 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001013 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001014
1015 root_dentry->d_fsdata = oe;
1016
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001017 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1018 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001019
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001020 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001021
1022 return 0;
1023
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001024out_free_oe:
1025 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001026out_put_cred:
1027 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001028out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001029 for (i = 0; i < ufs->numlower; i++)
1030 mntput(ufs->lower_mnt[i]);
1031 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001032out_put_workdir:
1033 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001034 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001035out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001036 for (i = 0; i < numlower; i++)
1037 path_put(&stack[i]);
1038 kfree(stack);
1039out_free_lowertmp:
1040 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001041out_put_workpath:
1042 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001043out_put_upperpath:
1044 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001045out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001046 kfree(ufs->config.lowerdir);
1047 kfree(ufs->config.upperdir);
1048 kfree(ufs->config.workdir);
1049 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001050out:
1051 return err;
1052}
1053
1054static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1055 const char *dev_name, void *raw_data)
1056{
1057 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1058}
1059
1060static struct file_system_type ovl_fs_type = {
1061 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001062 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001063 .mount = ovl_mount,
1064 .kill_sb = kill_anon_super,
1065};
Miklos Szeredief94b182014-11-20 16:39:59 +01001066MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001067
Amir Goldstein13cf1992017-06-12 09:54:40 +03001068static void ovl_inode_init_once(void *foo)
1069{
1070 struct ovl_inode *oi = foo;
1071
1072 inode_init_once(&oi->vfs_inode);
1073}
1074
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001075static int __init ovl_init(void)
1076{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001077 int err;
1078
1079 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1080 sizeof(struct ovl_inode), 0,
1081 (SLAB_RECLAIM_ACCOUNT|
1082 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1083 ovl_inode_init_once);
1084 if (ovl_inode_cachep == NULL)
1085 return -ENOMEM;
1086
1087 err = register_filesystem(&ovl_fs_type);
1088 if (err)
1089 kmem_cache_destroy(ovl_inode_cachep);
1090
1091 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001092}
1093
1094static void __exit ovl_exit(void)
1095{
1096 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001097
1098 /*
1099 * Make sure all delayed rcu free inodes are flushed before we
1100 * destroy cache.
1101 */
1102 rcu_barrier();
1103 kmem_cache_destroy(ovl_inode_cachep);
1104
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001105}
1106
1107module_init(ovl_init);
1108module_exit(ovl_exit);