blob: 1b865716110a18b7dd8b94789e81197c9f49d88a [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 Szeredi02b69b22016-12-16 11:02:56 +010044 kfree(oe->redirect);
Miklos Szeredidd662662014-12-13 00:59:43 +010045 for (i = 0; i < oe->numlower; i++)
46 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020047 kfree_rcu(oe, rcu);
48 }
49}
50
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030051static int ovl_check_append_only(struct inode *inode, int flag)
52{
53 /*
54 * This test was moot in vfs may_open() because overlay inode does
55 * not have the S_APPEND flag, so re-check on real upper inode
56 */
57 if (IS_APPEND(inode)) {
58 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
59 return -EPERM;
60 if (flag & O_TRUNC)
61 return -EPERM;
62 }
63
64 return 0;
65}
66
Miklos Szeredi2d902672016-06-30 08:53:27 +020067static struct dentry *ovl_d_real(struct dentry *dentry,
68 const struct inode *inode,
69 unsigned int open_flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040070{
71 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030072 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040073
Miklos Szeredica4c8a32016-12-16 11:02:55 +010074 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040075 if (!inode || inode == d_inode(dentry))
76 return dentry;
77 goto bug;
78 }
79
Miklos Szeredi2d902672016-06-30 08:53:27 +020080 if (d_is_negative(dentry))
81 return dentry;
82
83 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030084 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +020085 if (err)
86 return ERR_PTR(err);
87 }
88
Miklos Szeredid101a122016-03-26 16:14:37 -040089 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030090 if (real && (!inode || inode == d_inode(real))) {
91 if (!inode) {
92 err = ovl_check_append_only(d_inode(real), open_flags);
93 if (err)
94 return ERR_PTR(err);
95 }
Miklos Szeredid101a122016-03-26 16:14:37 -040096 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030097 }
Miklos Szeredid101a122016-03-26 16:14:37 -040098
99 real = ovl_dentry_lower(dentry);
100 if (!real)
101 goto bug;
102
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100103 /* Handle recursion */
104 real = d_real(real, inode, open_flags);
105
Miklos Szeredid101a122016-03-26 16:14:37 -0400106 if (!inode || inode == d_inode(real))
107 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400108bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200109 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400110 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
111 return dentry;
112}
113
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200114static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
115{
116 struct ovl_entry *oe = dentry->d_fsdata;
117 unsigned int i;
118 int ret = 1;
119
120 for (i = 0; i < oe->numlower; i++) {
121 struct dentry *d = oe->lowerstack[i].dentry;
122
123 if (d->d_flags & DCACHE_OP_REVALIDATE) {
124 ret = d->d_op->d_revalidate(d, flags);
125 if (ret < 0)
126 return ret;
127 if (!ret) {
128 if (!(flags & LOOKUP_RCU))
129 d_invalidate(d);
130 return -ESTALE;
131 }
132 }
133 }
134 return 1;
135}
136
137static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
138{
139 struct ovl_entry *oe = dentry->d_fsdata;
140 unsigned int i;
141 int ret = 1;
142
143 for (i = 0; i < oe->numlower; i++) {
144 struct dentry *d = oe->lowerstack[i].dentry;
145
146 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
147 ret = d->d_op->d_weak_revalidate(d, flags);
148 if (ret <= 0)
149 break;
150 }
151 }
152 return ret;
153}
154
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200155static const struct dentry_operations ovl_dentry_operations = {
156 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400157 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200158};
159
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200160static const struct dentry_operations ovl_reval_dentry_operations = {
161 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400162 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200163 .d_revalidate = ovl_dentry_revalidate,
164 .d_weak_revalidate = ovl_dentry_weak_revalidate,
165};
166
Amir Goldstein13cf1992017-06-12 09:54:40 +0300167static struct kmem_cache *ovl_inode_cachep;
168
169static struct inode *ovl_alloc_inode(struct super_block *sb)
170{
171 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
172
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);
191
Amir Goldstein13cf1992017-06-12 09:54:40 +0300192 call_rcu(&inode->i_rcu, ovl_i_callback);
193}
194
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200195static void ovl_put_super(struct super_block *sb)
196{
197 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100198 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199
200 dput(ufs->workdir);
201 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100202 for (i = 0; i < ufs->numlower; i++)
203 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300204 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200205
Erez Zadokf45827e82014-10-24 00:14:38 +0200206 kfree(ufs->config.lowerdir);
207 kfree(ufs->config.upperdir);
208 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200209 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210 kfree(ufs);
211}
212
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200213static int ovl_sync_fs(struct super_block *sb, int wait)
214{
215 struct ovl_fs *ufs = sb->s_fs_info;
216 struct super_block *upper_sb;
217 int ret;
218
219 if (!ufs->upper_mnt)
220 return 0;
221 upper_sb = ufs->upper_mnt->mnt_sb;
222 if (!upper_sb->s_op->sync_fs)
223 return 0;
224
225 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
226 down_read(&upper_sb->s_umount);
227 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
228 up_read(&upper_sb->s_umount);
229 return ret;
230}
231
Andy Whitcroftcc259632014-10-24 00:14:38 +0200232/**
233 * ovl_statfs
234 * @sb: The overlayfs super block
235 * @buf: The struct kstatfs to fill in with stats
236 *
237 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100238 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200239 */
240static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
241{
242 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
243 struct dentry *root_dentry = dentry->d_sb->s_root;
244 struct path path;
245 int err;
246
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100247 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200248
249 err = vfs_statfs(&path, buf);
250 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100251 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200252 buf->f_type = OVERLAYFS_SUPER_MAGIC;
253 }
254
255 return err;
256}
257
Erez Zadokf45827e82014-10-24 00:14:38 +0200258/**
259 * ovl_show_options
260 *
261 * Prints the mount options for a given superblock.
262 * Returns zero; does not fail.
263 */
264static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
265{
266 struct super_block *sb = dentry->d_sb;
267 struct ovl_fs *ufs = sb->s_fs_info;
268
Kees Cooka068acf2015-09-04 15:44:57 -0700269 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100270 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700271 seq_show_option(m, "upperdir", ufs->config.upperdir);
272 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100273 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200274 if (ufs->config.default_permissions)
275 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200276 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
277 seq_printf(m, ",redirect_dir=%s",
278 ufs->config.redirect_dir ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200279 return 0;
280}
281
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900282static int ovl_remount(struct super_block *sb, int *flags, char *data)
283{
284 struct ovl_fs *ufs = sb->s_fs_info;
285
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200286 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900287 return -EROFS;
288
289 return 0;
290}
291
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200292static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300293 .alloc_inode = ovl_alloc_inode,
294 .destroy_inode = ovl_destroy_inode,
295 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200296 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200297 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200298 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200299 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900300 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200301};
302
303enum {
304 OPT_LOWERDIR,
305 OPT_UPPERDIR,
306 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200307 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100308 OPT_REDIRECT_DIR_ON,
309 OPT_REDIRECT_DIR_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200310 OPT_ERR,
311};
312
313static const match_table_t ovl_tokens = {
314 {OPT_LOWERDIR, "lowerdir=%s"},
315 {OPT_UPPERDIR, "upperdir=%s"},
316 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200317 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100318 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
319 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320 {OPT_ERR, NULL}
321};
322
Miklos Szeredi91c77942014-11-20 16:40:00 +0100323static char *ovl_next_opt(char **s)
324{
325 char *sbegin = *s;
326 char *p;
327
328 if (sbegin == NULL)
329 return NULL;
330
331 for (p = sbegin; *p; p++) {
332 if (*p == '\\') {
333 p++;
334 if (!*p)
335 break;
336 } else if (*p == ',') {
337 *p = '\0';
338 *s = p + 1;
339 return sbegin;
340 }
341 }
342 *s = NULL;
343 return sbegin;
344}
345
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200346static int ovl_parse_opt(char *opt, struct ovl_config *config)
347{
348 char *p;
349
Miklos Szeredi91c77942014-11-20 16:40:00 +0100350 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351 int token;
352 substring_t args[MAX_OPT_ARGS];
353
354 if (!*p)
355 continue;
356
357 token = match_token(p, ovl_tokens, args);
358 switch (token) {
359 case OPT_UPPERDIR:
360 kfree(config->upperdir);
361 config->upperdir = match_strdup(&args[0]);
362 if (!config->upperdir)
363 return -ENOMEM;
364 break;
365
366 case OPT_LOWERDIR:
367 kfree(config->lowerdir);
368 config->lowerdir = match_strdup(&args[0]);
369 if (!config->lowerdir)
370 return -ENOMEM;
371 break;
372
373 case OPT_WORKDIR:
374 kfree(config->workdir);
375 config->workdir = match_strdup(&args[0]);
376 if (!config->workdir)
377 return -ENOMEM;
378 break;
379
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200380 case OPT_DEFAULT_PERMISSIONS:
381 config->default_permissions = true;
382 break;
383
Miklos Szeredia6c60652016-12-16 11:02:56 +0100384 case OPT_REDIRECT_DIR_ON:
385 config->redirect_dir = true;
386 break;
387
388 case OPT_REDIRECT_DIR_OFF:
389 config->redirect_dir = false;
390 break;
391
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200392 default:
hujianyangbead55e2015-01-15 13:17:36 +0800393 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200394 return -EINVAL;
395 }
396 }
hujianyang71cbad72015-01-15 13:20:57 +0800397
398 /* Workdir is useless in non-upper mount */
399 if (!config->upperdir && config->workdir) {
400 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
401 config->workdir);
402 kfree(config->workdir);
403 config->workdir = NULL;
404 }
405
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406 return 0;
407}
408
409#define OVL_WORKDIR_NAME "work"
410
411static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
412 struct dentry *dentry)
413{
414 struct inode *dir = dentry->d_inode;
415 struct dentry *work;
416 int err;
417 bool retried = false;
418
419 err = mnt_want_write(mnt);
420 if (err)
421 return ERR_PTR(err);
422
Al Viro59551022016-01-22 15:40:57 -0500423 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200424retry:
425 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
426 strlen(OVL_WORKDIR_NAME));
427
428 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200429 struct iattr attr = {
430 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000431 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200432 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200433
434 if (work->d_inode) {
435 err = -EEXIST;
436 if (retried)
437 goto out_dput;
438
439 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200440 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200441 dput(work);
442 goto retry;
443 }
444
Al Viro32a3d842016-12-04 17:33:17 +0000445 err = ovl_create_real(dir, work,
446 &(struct cattr){.mode = S_IFDIR | 0},
447 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200448 if (err)
449 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200450
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200451 /*
452 * Try to remove POSIX ACL xattrs from workdir. We are good if:
453 *
454 * a) success (there was a POSIX ACL xattr and was removed)
455 * b) -ENODATA (there was no POSIX ACL xattr)
456 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
457 *
458 * There are various other error values that could effectively
459 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
460 * if the xattr name is too long), but the set of filesystems
461 * allowed as upper are limited to "normal" ones, where checking
462 * for the above two errors is sufficient.
463 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200464 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200465 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200466 goto out_dput;
467
468 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200469 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200470 goto out_dput;
471
472 /* Clear any inherited mode bits */
473 inode_lock(work->d_inode);
474 err = notify_change(work, &attr, NULL);
475 inode_unlock(work->d_inode);
476 if (err)
477 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200478 }
479out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500480 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200481 mnt_drop_write(mnt);
482
483 return work;
484
485out_dput:
486 dput(work);
487 work = ERR_PTR(err);
488 goto out_unlock;
489}
490
Miklos Szeredi91c77942014-11-20 16:40:00 +0100491static void ovl_unescape(char *s)
492{
493 char *d = s;
494
495 for (;; s++, d++) {
496 if (*s == '\\')
497 s++;
498 *d = *s;
499 if (!*s)
500 break;
501 }
502}
503
Miklos Szerediab508822014-12-13 00:59:49 +0100504static int ovl_mount_dir_noesc(const char *name, struct path *path)
505{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100506 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100507
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100508 if (!*name) {
509 pr_err("overlayfs: empty lowerdir\n");
510 goto out;
511 }
Miklos Szerediab508822014-12-13 00:59:49 +0100512 err = kern_path(name, LOOKUP_FOLLOW, path);
513 if (err) {
514 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
515 goto out;
516 }
517 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200518 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100519 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
520 goto out_put;
521 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100522 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100523 pr_err("overlayfs: '%s' not a directory\n", name);
524 goto out_put;
525 }
526 return 0;
527
528out_put:
529 path_put(path);
530out:
531 return err;
532}
533
534static int ovl_mount_dir(const char *name, struct path *path)
535{
536 int err = -ENOMEM;
537 char *tmp = kstrdup(name, GFP_KERNEL);
538
539 if (tmp) {
540 ovl_unescape(tmp);
541 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200542
543 if (!err)
544 if (ovl_dentry_remote(path->dentry)) {
545 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
546 tmp);
547 path_put(path);
548 err = -EINVAL;
549 }
Miklos Szerediab508822014-12-13 00:59:49 +0100550 kfree(tmp);
551 }
552 return err;
553}
554
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100555static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
556 const char *name)
557{
558 struct kstatfs statfs;
559 int err = vfs_statfs(path, &statfs);
560
561 if (err)
562 pr_err("overlayfs: statfs failed on '%s'\n", name);
563 else
564 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
565
566 return err;
567}
568
569static int ovl_lower_dir(const char *name, struct path *path,
570 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100571{
572 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100573
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100574 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100575 if (err)
576 goto out;
577
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100578 err = ovl_check_namelen(path, ofs, name);
579 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100580 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100581
Miklos Szerediab508822014-12-13 00:59:49 +0100582 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
583
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200584 if (ovl_dentry_remote(path->dentry))
585 *remote = true;
586
Miklos Szerediab508822014-12-13 00:59:49 +0100587 return 0;
588
589out_put:
590 path_put(path);
591out:
592 return err;
593}
594
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200595/* Workdir should not be subdir of upperdir and vice versa */
596static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
597{
598 bool ok = false;
599
600 if (workdir != upperdir) {
601 ok = (lock_rename(workdir, upperdir) == NULL);
602 unlock_rename(workdir, upperdir);
603 }
604 return ok;
605}
606
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100607static unsigned int ovl_split_lowerdirs(char *str)
608{
609 unsigned int ctr = 1;
610 char *s, *d;
611
612 for (s = d = str;; s++, d++) {
613 if (*s == '\\') {
614 s++;
615 } else if (*s == ':') {
616 *d = '\0';
617 ctr++;
618 continue;
619 }
620 *d = *s;
621 if (!*s)
622 break;
623 }
624 return ctr;
625}
626
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200627static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200628ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
629 struct dentry *dentry, struct inode *inode,
630 const char *name, void *buffer, size_t size)
631{
632 return ovl_xattr_get(dentry, handler->name, buffer, size);
633}
634
635static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200636ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
637 struct dentry *dentry, struct inode *inode,
638 const char *name, const void *value,
639 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200640{
641 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200642 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200643 struct posix_acl *acl = NULL;
644 int err;
645
646 /* Check that everything is OK before copy-up */
647 if (value) {
648 acl = posix_acl_from_xattr(&init_user_ns, value, size);
649 if (IS_ERR(acl))
650 return PTR_ERR(acl);
651 }
652 err = -EOPNOTSUPP;
653 if (!IS_POSIXACL(d_inode(workdir)))
654 goto out_acl_release;
655 if (!realinode->i_op->set_acl)
656 goto out_acl_release;
657 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
658 err = acl ? -EACCES : 0;
659 goto out_acl_release;
660 }
661 err = -EPERM;
662 if (!inode_owner_or_capable(inode))
663 goto out_acl_release;
664
665 posix_acl_release(acl);
666
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100667 /*
668 * Check if sgid bit needs to be cleared (actual setacl operation will
669 * be done with mounter's capabilities and so that won't do it for us).
670 */
671 if (unlikely(inode->i_mode & S_ISGID) &&
672 handler->flags == ACL_TYPE_ACCESS &&
673 !in_group_p(inode->i_gid) &&
674 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
675 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
676
677 err = ovl_setattr(dentry, &iattr);
678 if (err)
679 return err;
680 }
681
Miklos Szeredice315132016-09-01 11:12:00 +0200682 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
683 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200684 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200685
686 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200687
688out_acl_release:
689 posix_acl_release(acl);
690 return err;
691}
692
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200693static int ovl_own_xattr_get(const struct xattr_handler *handler,
694 struct dentry *dentry, struct inode *inode,
695 const char *name, void *buffer, size_t size)
696{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200697 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200698}
699
Miklos Szeredid837a492016-07-29 12:05:24 +0200700static int ovl_own_xattr_set(const struct xattr_handler *handler,
701 struct dentry *dentry, struct inode *inode,
702 const char *name, const void *value,
703 size_t size, int flags)
704{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200705 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200706}
707
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200708static int ovl_other_xattr_get(const struct xattr_handler *handler,
709 struct dentry *dentry, struct inode *inode,
710 const char *name, void *buffer, size_t size)
711{
712 return ovl_xattr_get(dentry, name, buffer, size);
713}
714
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200715static int ovl_other_xattr_set(const struct xattr_handler *handler,
716 struct dentry *dentry, struct inode *inode,
717 const char *name, const void *value,
718 size_t size, int flags)
719{
720 return ovl_xattr_set(dentry, name, value, size, flags);
721}
722
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200723static const struct xattr_handler __maybe_unused
724ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200725 .name = XATTR_NAME_POSIX_ACL_ACCESS,
726 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200727 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200728 .set = ovl_posix_acl_xattr_set,
729};
730
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200731static const struct xattr_handler __maybe_unused
732ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200733 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
734 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200735 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200736 .set = ovl_posix_acl_xattr_set,
737};
738
739static const struct xattr_handler ovl_own_xattr_handler = {
740 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200741 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200742 .set = ovl_own_xattr_set,
743};
744
745static const struct xattr_handler ovl_other_xattr_handler = {
746 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200747 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200748 .set = ovl_other_xattr_set,
749};
750
751static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200752#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200753 &ovl_posix_acl_access_xattr_handler,
754 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200755#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200756 &ovl_own_xattr_handler,
757 &ovl_other_xattr_handler,
758 NULL
759};
760
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200761static int ovl_fill_super(struct super_block *sb, void *data, int silent)
762{
Kees Cook33006cd2017-03-29 14:02:19 -0700763 struct path upperpath = { };
764 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200765 struct dentry *root_dentry;
766 struct ovl_entry *oe;
767 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100768 struct path *stack = NULL;
769 char *lowertmp;
770 char *lower;
771 unsigned int numlower;
772 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100773 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200774 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300775 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200776 int err;
777
Erez Zadokf45827e82014-10-24 00:14:38 +0200778 err = -ENOMEM;
779 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
780 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200781 goto out;
782
Amir Goldstein39d3d602017-01-17 06:34:56 +0200783 init_waitqueue_head(&ufs->copyup_wq);
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100784 ufs->config.redirect_dir = ovl_redirect_dir_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200785 err = ovl_parse_opt((char *) data, &ufs->config);
786 if (err)
787 goto out_free_config;
788
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200789 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100790 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300791 if (!silent)
792 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200793 goto out_free_config;
794 }
795
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100796 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100797 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100798 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100799 if (!ufs->config.workdir) {
800 pr_err("overlayfs: missing 'workdir'\n");
801 goto out_free_config;
802 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200803
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100804 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
805 if (err)
806 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100807
hujianyang71cbad72015-01-15 13:20:57 +0800808 /* Upper fs should not be r/o */
809 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
810 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
811 err = -EINVAL;
812 goto out_put_upperpath;
813 }
814
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100815 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
816 if (err)
817 goto out_put_upperpath;
818
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100819 err = ovl_mount_dir(ufs->config.workdir, &workpath);
820 if (err)
821 goto out_put_upperpath;
822
hujianyang2f83fd82015-01-06 12:52:13 +0800823 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100824 if (upperpath.mnt != workpath.mnt) {
825 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
826 goto out_put_workpath;
827 }
828 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
829 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
830 goto out_put_workpath;
831 }
832 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100833 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100834 err = -ENOMEM;
835 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
836 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100837 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200838
839 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100840 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800841 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100842 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800843 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100844 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800845 } else if (!ufs->config.upperdir && stacklen == 1) {
846 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
847 goto out_free_lowertmp;
848 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100849
Geliang Tang313684c2016-11-18 23:02:46 +0800850 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100851 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
852 if (!stack)
853 goto out_free_lowertmp;
854
Geliang Tang313684c2016-11-18 23:02:46 +0800855 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100856 lower = lowertmp;
857 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100858 err = ovl_lower_dir(lower, &stack[numlower], ufs,
859 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100860 if (err)
861 goto out_put_lowerpath;
862
863 lower = strchr(lower, '\0') + 1;
864 }
865
866 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100867 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200868 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
869 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100870 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200871 }
872
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100873 if (ufs->config.upperdir) {
874 ufs->upper_mnt = clone_private_mount(&upperpath);
875 err = PTR_ERR(ufs->upper_mnt);
876 if (IS_ERR(ufs->upper_mnt)) {
877 pr_err("overlayfs: failed to clone upperpath\n");
878 goto out_put_lowerpath;
879 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200880 /* Don't inherit atime flags */
881 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
882
883 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100884
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100885 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
886 err = PTR_ERR(ufs->workdir);
887 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200888 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
889 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
890 sb->s_flags |= MS_RDONLY;
891 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100892 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500893
894 /*
895 * Upper should support d_type, else whiteouts are visible.
896 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400897 * iterate_dir() on workdir. This check requires successful
898 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500899 */
Vivek Goyal21765192016-05-20 09:04:26 -0400900 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200901 struct dentry *temp;
902
Vivek Goyal21765192016-05-20 09:04:26 -0400903 err = ovl_check_d_type_supported(&workpath);
904 if (err < 0)
905 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500906
Vivek Goyale7c0b592016-07-01 10:02:44 -0400907 /*
908 * We allowed this configuration and don't want to
909 * break users over kernel upgrade. So warn instead
910 * of erroring out.
911 */
912 if (!err)
913 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200914
915 /* Check if upper/work fs supports O_TMPFILE */
916 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
917 ufs->tmpfile = !IS_ERR(temp);
918 if (ufs->tmpfile)
919 dput(temp);
920 else
921 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +0300922
923 /*
924 * Check if upper/work fs supports trusted.overlay.*
925 * xattr
926 */
927 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
928 "0", 1, 0);
929 if (err) {
930 ufs->noxattr = true;
931 pr_warn("overlayfs: upper fs does not support xattr.\n");
932 } else {
933 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
934 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500935 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200936 }
937
hujianyang2f83fd82015-01-06 12:52:13 +0800938 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100939 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +0100940 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100941 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100942 for (i = 0; i < numlower; i++) {
943 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +0100944
hujianyang2f83fd82015-01-06 12:52:13 +0800945 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100946 if (IS_ERR(mnt)) {
947 pr_err("overlayfs: failed to clone lowerpath\n");
948 goto out_put_lower_mnt;
949 }
950 /*
951 * Make lower_mnt R/O. That way fchmod/fchown on lower file
952 * will fail instead of modifying lower fs.
953 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200954 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100955
956 ufs->lower_mnt[ufs->numlower] = mnt;
957 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400958
959 /* Check if all lower layers are on same sb */
960 if (i == 0)
961 ufs->same_sb = mnt->mnt_sb;
962 else if (ufs->same_sb != mnt->mnt_sb)
963 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200964 }
965
hujianyang71cbad72015-01-15 13:20:57 +0800966 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
967 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200968 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400969 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
970 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200971
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200972 if (remote)
973 sb->s_d_op = &ovl_reval_dentry_operations;
974 else
975 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200976
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300977 ufs->creator_cred = cred = prepare_creds();
978 if (!cred)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200979 goto out_put_lower_mnt;
980
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300981 /* Never override disk quota limits or use reserved space */
982 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
983
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200984 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100985 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100986 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200987 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200988
Vivek Goyal655042c2016-10-14 03:03:36 +0200989 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
990 sb->s_op = &ovl_super_operations;
991 sb->s_xattr = ovl_xattr_handlers;
992 sb->s_fs_info = ufs;
993 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
994
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100995 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200996 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100997 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200998
999 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001000 for (i = 0; i < numlower; i++)
1001 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001002 path_put(&workpath);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001003 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001004
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001005 if (upperpath.dentry) {
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001006 oe->impure = ovl_is_impuredir(upperpath.dentry);
1007 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001008 for (i = 0; i < numlower; i++) {
1009 oe->lowerstack[i].dentry = stack[i].dentry;
1010 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1011 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001012 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001013
1014 root_dentry->d_fsdata = oe;
1015
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001016 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1017 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001018
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001019 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001020
1021 return 0;
1022
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001023out_free_oe:
1024 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001025out_put_cred:
1026 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001027out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001028 for (i = 0; i < ufs->numlower; i++)
1029 mntput(ufs->lower_mnt[i]);
1030 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001031out_put_workdir:
1032 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001033 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001034out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001035 for (i = 0; i < numlower; i++)
1036 path_put(&stack[i]);
1037 kfree(stack);
1038out_free_lowertmp:
1039 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001040out_put_workpath:
1041 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001042out_put_upperpath:
1043 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001044out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001045 kfree(ufs->config.lowerdir);
1046 kfree(ufs->config.upperdir);
1047 kfree(ufs->config.workdir);
1048 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001049out:
1050 return err;
1051}
1052
1053static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1054 const char *dev_name, void *raw_data)
1055{
1056 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1057}
1058
1059static struct file_system_type ovl_fs_type = {
1060 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001061 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001062 .mount = ovl_mount,
1063 .kill_sb = kill_anon_super,
1064};
Miklos Szeredief94b182014-11-20 16:39:59 +01001065MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001066
Amir Goldstein13cf1992017-06-12 09:54:40 +03001067static void ovl_inode_init_once(void *foo)
1068{
1069 struct ovl_inode *oi = foo;
1070
1071 inode_init_once(&oi->vfs_inode);
1072}
1073
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001074static int __init ovl_init(void)
1075{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001076 int err;
1077
1078 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1079 sizeof(struct ovl_inode), 0,
1080 (SLAB_RECLAIM_ACCOUNT|
1081 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1082 ovl_inode_init_once);
1083 if (ovl_inode_cachep == NULL)
1084 return -ENOMEM;
1085
1086 err = register_filesystem(&ovl_fs_type);
1087 if (err)
1088 kmem_cache_destroy(ovl_inode_cachep);
1089
1090 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001091}
1092
1093static void __exit ovl_exit(void)
1094{
1095 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001096
1097 /*
1098 * Make sure all delayed rcu free inodes are flushed before we
1099 * destroy cache.
1100 */
1101 rcu_barrier();
1102 kmem_cache_destroy(ovl_inode_cachep);
1103
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001104}
1105
1106module_init(ovl_init);
1107module_exit(ovl_exit);