blob: 7c7b946b063f91624f1a467fd099f921d3f6027b [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 Szeredi13c72072017-07-04 22:03:16 +0200173 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200174 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200175 oi->lower = NULL;
176
Amir Goldstein13cf1992017-06-12 09:54:40 +0300177 return &oi->vfs_inode;
178}
179
180static void ovl_i_callback(struct rcu_head *head)
181{
182 struct inode *inode = container_of(head, struct inode, i_rcu);
183
184 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
185}
186
187static void ovl_destroy_inode(struct inode *inode)
188{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200189 struct ovl_inode *oi = OVL_I(inode);
190
191 dput(oi->__upperdentry);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200192 kfree(oi->redirect);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200193
Amir Goldstein13cf1992017-06-12 09:54:40 +0300194 call_rcu(&inode->i_rcu, ovl_i_callback);
195}
196
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200197static void ovl_put_super(struct super_block *sb)
198{
199 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100200 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200201
202 dput(ufs->workdir);
203 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100204 for (i = 0; i < ufs->numlower; i++)
205 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300206 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207
Erez Zadokf45827e82014-10-24 00:14:38 +0200208 kfree(ufs->config.lowerdir);
209 kfree(ufs->config.upperdir);
210 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200211 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200212 kfree(ufs);
213}
214
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200215static int ovl_sync_fs(struct super_block *sb, int wait)
216{
217 struct ovl_fs *ufs = sb->s_fs_info;
218 struct super_block *upper_sb;
219 int ret;
220
221 if (!ufs->upper_mnt)
222 return 0;
223 upper_sb = ufs->upper_mnt->mnt_sb;
224 if (!upper_sb->s_op->sync_fs)
225 return 0;
226
227 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
228 down_read(&upper_sb->s_umount);
229 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
230 up_read(&upper_sb->s_umount);
231 return ret;
232}
233
Andy Whitcroftcc259632014-10-24 00:14:38 +0200234/**
235 * ovl_statfs
236 * @sb: The overlayfs super block
237 * @buf: The struct kstatfs to fill in with stats
238 *
239 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100240 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200241 */
242static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
243{
244 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
245 struct dentry *root_dentry = dentry->d_sb->s_root;
246 struct path path;
247 int err;
248
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100249 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200250
251 err = vfs_statfs(&path, buf);
252 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100253 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200254 buf->f_type = OVERLAYFS_SUPER_MAGIC;
255 }
256
257 return err;
258}
259
Erez Zadokf45827e82014-10-24 00:14:38 +0200260/**
261 * ovl_show_options
262 *
263 * Prints the mount options for a given superblock.
264 * Returns zero; does not fail.
265 */
266static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
267{
268 struct super_block *sb = dentry->d_sb;
269 struct ovl_fs *ufs = sb->s_fs_info;
270
Kees Cooka068acf2015-09-04 15:44:57 -0700271 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100272 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700273 seq_show_option(m, "upperdir", ufs->config.upperdir);
274 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100275 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200276 if (ufs->config.default_permissions)
277 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200278 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
279 seq_printf(m, ",redirect_dir=%s",
280 ufs->config.redirect_dir ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200281 return 0;
282}
283
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900284static int ovl_remount(struct super_block *sb, int *flags, char *data)
285{
286 struct ovl_fs *ufs = sb->s_fs_info;
287
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200288 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900289 return -EROFS;
290
291 return 0;
292}
293
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200294static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300295 .alloc_inode = ovl_alloc_inode,
296 .destroy_inode = ovl_destroy_inode,
297 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200298 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200299 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200300 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200301 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900302 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200303};
304
305enum {
306 OPT_LOWERDIR,
307 OPT_UPPERDIR,
308 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200309 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100310 OPT_REDIRECT_DIR_ON,
311 OPT_REDIRECT_DIR_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200312 OPT_ERR,
313};
314
315static const match_table_t ovl_tokens = {
316 {OPT_LOWERDIR, "lowerdir=%s"},
317 {OPT_UPPERDIR, "upperdir=%s"},
318 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200319 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100320 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
321 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322 {OPT_ERR, NULL}
323};
324
Miklos Szeredi91c77942014-11-20 16:40:00 +0100325static char *ovl_next_opt(char **s)
326{
327 char *sbegin = *s;
328 char *p;
329
330 if (sbegin == NULL)
331 return NULL;
332
333 for (p = sbegin; *p; p++) {
334 if (*p == '\\') {
335 p++;
336 if (!*p)
337 break;
338 } else if (*p == ',') {
339 *p = '\0';
340 *s = p + 1;
341 return sbegin;
342 }
343 }
344 *s = NULL;
345 return sbegin;
346}
347
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200348static int ovl_parse_opt(char *opt, struct ovl_config *config)
349{
350 char *p;
351
Miklos Szeredi91c77942014-11-20 16:40:00 +0100352 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200353 int token;
354 substring_t args[MAX_OPT_ARGS];
355
356 if (!*p)
357 continue;
358
359 token = match_token(p, ovl_tokens, args);
360 switch (token) {
361 case OPT_UPPERDIR:
362 kfree(config->upperdir);
363 config->upperdir = match_strdup(&args[0]);
364 if (!config->upperdir)
365 return -ENOMEM;
366 break;
367
368 case OPT_LOWERDIR:
369 kfree(config->lowerdir);
370 config->lowerdir = match_strdup(&args[0]);
371 if (!config->lowerdir)
372 return -ENOMEM;
373 break;
374
375 case OPT_WORKDIR:
376 kfree(config->workdir);
377 config->workdir = match_strdup(&args[0]);
378 if (!config->workdir)
379 return -ENOMEM;
380 break;
381
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200382 case OPT_DEFAULT_PERMISSIONS:
383 config->default_permissions = true;
384 break;
385
Miklos Szeredia6c60652016-12-16 11:02:56 +0100386 case OPT_REDIRECT_DIR_ON:
387 config->redirect_dir = true;
388 break;
389
390 case OPT_REDIRECT_DIR_OFF:
391 config->redirect_dir = false;
392 break;
393
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200394 default:
hujianyangbead55e2015-01-15 13:17:36 +0800395 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200396 return -EINVAL;
397 }
398 }
hujianyang71cbad72015-01-15 13:20:57 +0800399
400 /* Workdir is useless in non-upper mount */
401 if (!config->upperdir && config->workdir) {
402 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
403 config->workdir);
404 kfree(config->workdir);
405 config->workdir = NULL;
406 }
407
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408 return 0;
409}
410
411#define OVL_WORKDIR_NAME "work"
412
413static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
414 struct dentry *dentry)
415{
416 struct inode *dir = dentry->d_inode;
417 struct dentry *work;
418 int err;
419 bool retried = false;
420
421 err = mnt_want_write(mnt);
422 if (err)
423 return ERR_PTR(err);
424
Al Viro59551022016-01-22 15:40:57 -0500425 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200426retry:
427 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
428 strlen(OVL_WORKDIR_NAME));
429
430 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200431 struct iattr attr = {
432 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000433 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200434 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200435
436 if (work->d_inode) {
437 err = -EEXIST;
438 if (retried)
439 goto out_dput;
440
441 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200442 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200443 dput(work);
444 goto retry;
445 }
446
Al Viro32a3d842016-12-04 17:33:17 +0000447 err = ovl_create_real(dir, work,
448 &(struct cattr){.mode = S_IFDIR | 0},
449 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200450 if (err)
451 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200452
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200453 /*
454 * Try to remove POSIX ACL xattrs from workdir. We are good if:
455 *
456 * a) success (there was a POSIX ACL xattr and was removed)
457 * b) -ENODATA (there was no POSIX ACL xattr)
458 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
459 *
460 * There are various other error values that could effectively
461 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
462 * if the xattr name is too long), but the set of filesystems
463 * allowed as upper are limited to "normal" ones, where checking
464 * for the above two errors is sufficient.
465 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200466 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200467 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200468 goto out_dput;
469
470 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200471 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200472 goto out_dput;
473
474 /* Clear any inherited mode bits */
475 inode_lock(work->d_inode);
476 err = notify_change(work, &attr, NULL);
477 inode_unlock(work->d_inode);
478 if (err)
479 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480 }
481out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500482 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200483 mnt_drop_write(mnt);
484
485 return work;
486
487out_dput:
488 dput(work);
489 work = ERR_PTR(err);
490 goto out_unlock;
491}
492
Miklos Szeredi91c77942014-11-20 16:40:00 +0100493static void ovl_unescape(char *s)
494{
495 char *d = s;
496
497 for (;; s++, d++) {
498 if (*s == '\\')
499 s++;
500 *d = *s;
501 if (!*s)
502 break;
503 }
504}
505
Miklos Szerediab508822014-12-13 00:59:49 +0100506static int ovl_mount_dir_noesc(const char *name, struct path *path)
507{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100508 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100509
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100510 if (!*name) {
511 pr_err("overlayfs: empty lowerdir\n");
512 goto out;
513 }
Miklos Szerediab508822014-12-13 00:59:49 +0100514 err = kern_path(name, LOOKUP_FOLLOW, path);
515 if (err) {
516 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
517 goto out;
518 }
519 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200520 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100521 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
522 goto out_put;
523 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100524 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100525 pr_err("overlayfs: '%s' not a directory\n", name);
526 goto out_put;
527 }
528 return 0;
529
530out_put:
531 path_put(path);
532out:
533 return err;
534}
535
536static int ovl_mount_dir(const char *name, struct path *path)
537{
538 int err = -ENOMEM;
539 char *tmp = kstrdup(name, GFP_KERNEL);
540
541 if (tmp) {
542 ovl_unescape(tmp);
543 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200544
545 if (!err)
546 if (ovl_dentry_remote(path->dentry)) {
547 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
548 tmp);
549 path_put(path);
550 err = -EINVAL;
551 }
Miklos Szerediab508822014-12-13 00:59:49 +0100552 kfree(tmp);
553 }
554 return err;
555}
556
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100557static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
558 const char *name)
559{
560 struct kstatfs statfs;
561 int err = vfs_statfs(path, &statfs);
562
563 if (err)
564 pr_err("overlayfs: statfs failed on '%s'\n", name);
565 else
566 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
567
568 return err;
569}
570
571static int ovl_lower_dir(const char *name, struct path *path,
572 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100573{
574 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100575
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100576 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100577 if (err)
578 goto out;
579
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100580 err = ovl_check_namelen(path, ofs, name);
581 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100582 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100583
Miklos Szerediab508822014-12-13 00:59:49 +0100584 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
585
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200586 if (ovl_dentry_remote(path->dentry))
587 *remote = true;
588
Miklos Szerediab508822014-12-13 00:59:49 +0100589 return 0;
590
591out_put:
592 path_put(path);
593out:
594 return err;
595}
596
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200597/* Workdir should not be subdir of upperdir and vice versa */
598static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
599{
600 bool ok = false;
601
602 if (workdir != upperdir) {
603 ok = (lock_rename(workdir, upperdir) == NULL);
604 unlock_rename(workdir, upperdir);
605 }
606 return ok;
607}
608
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100609static unsigned int ovl_split_lowerdirs(char *str)
610{
611 unsigned int ctr = 1;
612 char *s, *d;
613
614 for (s = d = str;; s++, d++) {
615 if (*s == '\\') {
616 s++;
617 } else if (*s == ':') {
618 *d = '\0';
619 ctr++;
620 continue;
621 }
622 *d = *s;
623 if (!*s)
624 break;
625 }
626 return ctr;
627}
628
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200629static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200630ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
631 struct dentry *dentry, struct inode *inode,
632 const char *name, void *buffer, size_t size)
633{
634 return ovl_xattr_get(dentry, handler->name, buffer, size);
635}
636
637static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200638ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
639 struct dentry *dentry, struct inode *inode,
640 const char *name, const void *value,
641 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200642{
643 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200644 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200645 struct posix_acl *acl = NULL;
646 int err;
647
648 /* Check that everything is OK before copy-up */
649 if (value) {
650 acl = posix_acl_from_xattr(&init_user_ns, value, size);
651 if (IS_ERR(acl))
652 return PTR_ERR(acl);
653 }
654 err = -EOPNOTSUPP;
655 if (!IS_POSIXACL(d_inode(workdir)))
656 goto out_acl_release;
657 if (!realinode->i_op->set_acl)
658 goto out_acl_release;
659 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
660 err = acl ? -EACCES : 0;
661 goto out_acl_release;
662 }
663 err = -EPERM;
664 if (!inode_owner_or_capable(inode))
665 goto out_acl_release;
666
667 posix_acl_release(acl);
668
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100669 /*
670 * Check if sgid bit needs to be cleared (actual setacl operation will
671 * be done with mounter's capabilities and so that won't do it for us).
672 */
673 if (unlikely(inode->i_mode & S_ISGID) &&
674 handler->flags == ACL_TYPE_ACCESS &&
675 !in_group_p(inode->i_gid) &&
676 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
677 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
678
679 err = ovl_setattr(dentry, &iattr);
680 if (err)
681 return err;
682 }
683
Miklos Szeredice315132016-09-01 11:12:00 +0200684 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
685 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200686 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200687
688 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200689
690out_acl_release:
691 posix_acl_release(acl);
692 return err;
693}
694
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200695static int ovl_own_xattr_get(const struct xattr_handler *handler,
696 struct dentry *dentry, struct inode *inode,
697 const char *name, void *buffer, size_t size)
698{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200699 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200700}
701
Miklos Szeredid837a492016-07-29 12:05:24 +0200702static int ovl_own_xattr_set(const struct xattr_handler *handler,
703 struct dentry *dentry, struct inode *inode,
704 const char *name, const void *value,
705 size_t size, int flags)
706{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200707 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200708}
709
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200710static int ovl_other_xattr_get(const struct xattr_handler *handler,
711 struct dentry *dentry, struct inode *inode,
712 const char *name, void *buffer, size_t size)
713{
714 return ovl_xattr_get(dentry, name, buffer, size);
715}
716
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200717static int ovl_other_xattr_set(const struct xattr_handler *handler,
718 struct dentry *dentry, struct inode *inode,
719 const char *name, const void *value,
720 size_t size, int flags)
721{
722 return ovl_xattr_set(dentry, name, value, size, flags);
723}
724
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200725static const struct xattr_handler __maybe_unused
726ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200727 .name = XATTR_NAME_POSIX_ACL_ACCESS,
728 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200729 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200730 .set = ovl_posix_acl_xattr_set,
731};
732
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200733static const struct xattr_handler __maybe_unused
734ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200735 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
736 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200737 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200738 .set = ovl_posix_acl_xattr_set,
739};
740
741static const struct xattr_handler ovl_own_xattr_handler = {
742 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200743 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200744 .set = ovl_own_xattr_set,
745};
746
747static const struct xattr_handler ovl_other_xattr_handler = {
748 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200749 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200750 .set = ovl_other_xattr_set,
751};
752
753static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200754#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200755 &ovl_posix_acl_access_xattr_handler,
756 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200757#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200758 &ovl_own_xattr_handler,
759 &ovl_other_xattr_handler,
760 NULL
761};
762
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200763static int ovl_fill_super(struct super_block *sb, void *data, int silent)
764{
Kees Cook33006cd2017-03-29 14:02:19 -0700765 struct path upperpath = { };
766 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200767 struct dentry *root_dentry;
768 struct ovl_entry *oe;
769 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100770 struct path *stack = NULL;
771 char *lowertmp;
772 char *lower;
773 unsigned int numlower;
774 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100775 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200776 bool remote = false;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300777 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200778 int err;
779
Erez Zadokf45827e82014-10-24 00:14:38 +0200780 err = -ENOMEM;
781 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
782 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200783 goto out;
784
Amir Goldstein39d3d602017-01-17 06:34:56 +0200785 init_waitqueue_head(&ufs->copyup_wq);
Miklos Szeredi688ea0e2016-12-16 11:02:57 +0100786 ufs->config.redirect_dir = ovl_redirect_dir_def;
Erez Zadokf45827e82014-10-24 00:14:38 +0200787 err = ovl_parse_opt((char *) data, &ufs->config);
788 if (err)
789 goto out_free_config;
790
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200791 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100792 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300793 if (!silent)
794 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200795 goto out_free_config;
796 }
797
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100798 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100799 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100800 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100801 if (!ufs->config.workdir) {
802 pr_err("overlayfs: missing 'workdir'\n");
803 goto out_free_config;
804 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200805
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100806 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
807 if (err)
808 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100809
hujianyang71cbad72015-01-15 13:20:57 +0800810 /* Upper fs should not be r/o */
811 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
812 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
813 err = -EINVAL;
814 goto out_put_upperpath;
815 }
816
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100817 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
818 if (err)
819 goto out_put_upperpath;
820
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100821 err = ovl_mount_dir(ufs->config.workdir, &workpath);
822 if (err)
823 goto out_put_upperpath;
824
hujianyang2f83fd82015-01-06 12:52:13 +0800825 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100826 if (upperpath.mnt != workpath.mnt) {
827 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
828 goto out_put_workpath;
829 }
830 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
831 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
832 goto out_put_workpath;
833 }
834 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100835 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100836 err = -ENOMEM;
837 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
838 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100839 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200840
841 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100842 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800843 if (stacklen > OVL_MAX_STACK) {
Colin Ian Kingfd365702016-08-18 16:58:35 +0100844 pr_err("overlayfs: too many lower directories, limit is %d\n",
hujianyang6be45062015-01-15 13:19:21 +0800845 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100846 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800847 } else if (!ufs->config.upperdir && stacklen == 1) {
848 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
849 goto out_free_lowertmp;
850 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100851
Geliang Tang313684c2016-11-18 23:02:46 +0800852 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100853 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
854 if (!stack)
855 goto out_free_lowertmp;
856
Geliang Tang313684c2016-11-18 23:02:46 +0800857 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100858 lower = lowertmp;
859 for (numlower = 0; numlower < stacklen; numlower++) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100860 err = ovl_lower_dir(lower, &stack[numlower], ufs,
861 &sb->s_stack_depth, &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100862 if (err)
863 goto out_put_lowerpath;
864
865 lower = strchr(lower, '\0') + 1;
866 }
867
868 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100869 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200870 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
871 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100872 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200873 }
874
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100875 if (ufs->config.upperdir) {
876 ufs->upper_mnt = clone_private_mount(&upperpath);
877 err = PTR_ERR(ufs->upper_mnt);
878 if (IS_ERR(ufs->upper_mnt)) {
879 pr_err("overlayfs: failed to clone upperpath\n");
880 goto out_put_lowerpath;
881 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200882 /* Don't inherit atime flags */
883 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
884
885 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100886
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100887 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
888 err = PTR_ERR(ufs->workdir);
889 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200890 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
891 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
892 sb->s_flags |= MS_RDONLY;
893 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100894 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500895
896 /*
897 * Upper should support d_type, else whiteouts are visible.
898 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -0400899 * iterate_dir() on workdir. This check requires successful
900 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -0500901 */
Vivek Goyal21765192016-05-20 09:04:26 -0400902 if (ufs->workdir) {
Amir Goldsteine7f52422017-01-17 06:34:53 +0200903 struct dentry *temp;
904
Vivek Goyal21765192016-05-20 09:04:26 -0400905 err = ovl_check_d_type_supported(&workpath);
906 if (err < 0)
907 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -0500908
Vivek Goyale7c0b592016-07-01 10:02:44 -0400909 /*
910 * We allowed this configuration and don't want to
911 * break users over kernel upgrade. So warn instead
912 * of erroring out.
913 */
914 if (!err)
915 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Amir Goldsteine7f52422017-01-17 06:34:53 +0200916
917 /* Check if upper/work fs supports O_TMPFILE */
918 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
919 ufs->tmpfile = !IS_ERR(temp);
920 if (ufs->tmpfile)
921 dput(temp);
922 else
923 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
Amir Goldstein82b749b2017-05-17 00:12:40 +0300924
925 /*
926 * Check if upper/work fs supports trusted.overlay.*
927 * xattr
928 */
929 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
930 "0", 1, 0);
931 if (err) {
932 ufs->noxattr = true;
933 pr_warn("overlayfs: upper fs does not support xattr.\n");
934 } else {
935 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
936 }
Vivek Goyal45aebea2016-02-22 09:28:34 -0500937 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200938 }
939
hujianyang2f83fd82015-01-06 12:52:13 +0800940 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100941 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +0100942 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100943 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100944 for (i = 0; i < numlower; i++) {
945 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +0100946
hujianyang2f83fd82015-01-06 12:52:13 +0800947 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100948 if (IS_ERR(mnt)) {
949 pr_err("overlayfs: failed to clone lowerpath\n");
950 goto out_put_lower_mnt;
951 }
952 /*
953 * Make lower_mnt R/O. That way fchmod/fchown on lower file
954 * will fail instead of modifying lower fs.
955 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200956 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100957
958 ufs->lower_mnt[ufs->numlower] = mnt;
959 ufs->numlower++;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400960
961 /* Check if all lower layers are on same sb */
962 if (i == 0)
963 ufs->same_sb = mnt->mnt_sb;
964 else if (ufs->same_sb != mnt->mnt_sb)
965 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200966 }
967
hujianyang71cbad72015-01-15 13:20:57 +0800968 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
969 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200970 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -0400971 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
972 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200973
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200974 if (remote)
975 sb->s_d_op = &ovl_reval_dentry_operations;
976 else
977 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200978
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300979 ufs->creator_cred = cred = prepare_creds();
980 if (!cred)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200981 goto out_put_lower_mnt;
982
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +0300983 /* Never override disk quota limits or use reserved space */
984 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
985
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200986 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100987 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100988 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200989 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200990
Vivek Goyal655042c2016-10-14 03:03:36 +0200991 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
992 sb->s_op = &ovl_super_operations;
993 sb->s_xattr = ovl_xattr_handlers;
994 sb->s_fs_info = ufs;
995 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
996
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100997 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200998 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100999 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001000
1001 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001002 for (i = 0; i < numlower; i++)
1003 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001004 path_put(&workpath);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001005 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001006
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001007 if (upperpath.dentry) {
Miklos Szeredi13c72072017-07-04 22:03:16 +02001008 if (ovl_is_impuredir(upperpath.dentry))
1009 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001010 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001011 for (i = 0; i < numlower; i++) {
1012 oe->lowerstack[i].dentry = stack[i].dentry;
1013 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1014 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001015 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001016
1017 root_dentry->d_fsdata = oe;
1018
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001019 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1020 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001021
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001022 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001023
1024 return 0;
1025
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001026out_free_oe:
1027 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001028out_put_cred:
1029 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001030out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001031 for (i = 0; i < ufs->numlower; i++)
1032 mntput(ufs->lower_mnt[i]);
1033 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001034out_put_workdir:
1035 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001036 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001037out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001038 for (i = 0; i < numlower; i++)
1039 path_put(&stack[i]);
1040 kfree(stack);
1041out_free_lowertmp:
1042 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001043out_put_workpath:
1044 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001045out_put_upperpath:
1046 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001047out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001048 kfree(ufs->config.lowerdir);
1049 kfree(ufs->config.upperdir);
1050 kfree(ufs->config.workdir);
1051 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001052out:
1053 return err;
1054}
1055
1056static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1057 const char *dev_name, void *raw_data)
1058{
1059 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1060}
1061
1062static struct file_system_type ovl_fs_type = {
1063 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001064 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001065 .mount = ovl_mount,
1066 .kill_sb = kill_anon_super,
1067};
Miklos Szeredief94b182014-11-20 16:39:59 +01001068MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001069
Amir Goldstein13cf1992017-06-12 09:54:40 +03001070static void ovl_inode_init_once(void *foo)
1071{
1072 struct ovl_inode *oi = foo;
1073
1074 inode_init_once(&oi->vfs_inode);
1075}
1076
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001077static int __init ovl_init(void)
1078{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001079 int err;
1080
1081 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1082 sizeof(struct ovl_inode), 0,
1083 (SLAB_RECLAIM_ACCOUNT|
1084 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1085 ovl_inode_init_once);
1086 if (ovl_inode_cachep == NULL)
1087 return -ENOMEM;
1088
1089 err = register_filesystem(&ovl_fs_type);
1090 if (err)
1091 kmem_cache_destroy(ovl_inode_cachep);
1092
1093 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001094}
1095
1096static void __exit ovl_exit(void)
1097{
1098 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001099
1100 /*
1101 * Make sure all delayed rcu free inodes are flushed before we
1102 * destroy cache.
1103 */
1104 rcu_barrier();
1105 kmem_cache_destroy(ovl_inode_cachep);
1106
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001107}
1108
1109module_init(ovl_init);
1110module_exit(ovl_exit);