blob: c1344c8a952017fcbe8463ee2e27fac9e4a58516 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <uapi/linux/magic.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011#include <linux/fs.h>
12#include <linux/namei.h>
13#include <linux/xattr.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020014#include <linux/mount.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020015#include <linux/parser.h>
16#include <linux/module.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020017#include <linux/statfs.h>
Erez Zadokf45827e82014-10-24 00:14:38 +020018#include <linux/seq_file.h>
Miklos Szeredid837a492016-07-29 12:05:24 +020019#include <linux/posix_acl_xattr.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include "overlayfs.h"
21
22MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23MODULE_DESCRIPTION("Overlay filesystem");
24MODULE_LICENSE("GPL");
25
Miklos Szeredie9be9d52014-10-24 00:14:38 +020026
27struct ovl_dir_cache;
28
Miklos Szeredia78d9f02014-12-13 00:59:52 +010029#define OVL_MAX_STACK 500
30
Miklos Szeredi688ea0e2016-12-16 11:02:57 +010031static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
32module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
33MODULE_PARM_DESC(ovl_redirect_dir_def,
34 "Default to on or off for the redirect_dir feature");
Miklos Szeredie9be9d52014-10-24 00:14:38 +020035
Amir Goldstein02bcd152017-06-21 15:28:36 +030036static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
37module_param_named(index, ovl_index_def, bool, 0644);
38MODULE_PARM_DESC(ovl_index_def,
39 "Default to on or off for the inodes index feature");
40
Miklos Szeredie9be9d52014-10-24 00:14:38 +020041static void ovl_dentry_release(struct dentry *dentry)
42{
43 struct ovl_entry *oe = dentry->d_fsdata;
44
45 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +010046 unsigned int i;
47
Miklos Szeredidd662662014-12-13 00:59:43 +010048 for (i = 0; i < oe->numlower; i++)
49 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020050 kfree_rcu(oe, rcu);
51 }
52}
53
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030054static int ovl_check_append_only(struct inode *inode, int flag)
55{
56 /*
57 * This test was moot in vfs may_open() because overlay inode does
58 * not have the S_APPEND flag, so re-check on real upper inode
59 */
60 if (IS_APPEND(inode)) {
61 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
62 return -EPERM;
63 if (flag & O_TRUNC)
64 return -EPERM;
65 }
66
67 return 0;
68}
69
Miklos Szeredi2d902672016-06-30 08:53:27 +020070static struct dentry *ovl_d_real(struct dentry *dentry,
71 const struct inode *inode,
Miklos Szeredi495e6422017-09-04 21:42:22 +020072 unsigned int open_flags, unsigned int flags)
Miklos Szeredid101a122016-03-26 16:14:37 -040073{
74 struct dentry *real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030075 int err;
Miklos Szeredid101a122016-03-26 16:14:37 -040076
Miklos Szeredicd913042017-09-05 12:53:11 +020077 if (flags & D_REAL_UPPER)
78 return ovl_dentry_upper(dentry);
79
Miklos Szeredica4c8a32016-12-16 11:02:55 +010080 if (!d_is_reg(dentry)) {
Miklos Szeredid101a122016-03-26 16:14:37 -040081 if (!inode || inode == d_inode(dentry))
82 return dentry;
83 goto bug;
84 }
85
Miklos Szeredi2d902672016-06-30 08:53:27 +020086 if (open_flags) {
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030087 err = ovl_open_maybe_copy_up(dentry, open_flags);
Miklos Szeredi2d902672016-06-30 08:53:27 +020088 if (err)
89 return ERR_PTR(err);
90 }
91
Miklos Szeredid101a122016-03-26 16:14:37 -040092 real = ovl_dentry_upper(dentry);
Amir Goldsteinb0990fb2017-04-08 14:49:07 +030093 if (real && (!inode || inode == d_inode(real))) {
94 if (!inode) {
95 err = ovl_check_append_only(d_inode(real), open_flags);
96 if (err)
97 return ERR_PTR(err);
98 }
Miklos Szeredid101a122016-03-26 16:14:37 -040099 return real;
Amir Goldsteinb0990fb2017-04-08 14:49:07 +0300100 }
Miklos Szeredid101a122016-03-26 16:14:37 -0400101
102 real = ovl_dentry_lower(dentry);
103 if (!real)
104 goto bug;
105
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100106 /* Handle recursion */
Miklos Szeredi495e6422017-09-04 21:42:22 +0200107 real = d_real(real, inode, open_flags, 0);
Miklos Szeredic4fcfc12016-11-29 10:20:24 +0100108
Miklos Szeredid101a122016-03-26 16:14:37 -0400109 if (!inode || inode == d_inode(real))
110 return real;
Miklos Szeredid101a122016-03-26 16:14:37 -0400111bug:
Miklos Szeredi656189d2016-07-29 12:05:24 +0200112 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
Miklos Szeredid101a122016-03-26 16:14:37 -0400113 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
114 return dentry;
115}
116
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200117static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
118{
119 struct ovl_entry *oe = dentry->d_fsdata;
120 unsigned int i;
121 int ret = 1;
122
123 for (i = 0; i < oe->numlower; i++) {
124 struct dentry *d = oe->lowerstack[i].dentry;
125
126 if (d->d_flags & DCACHE_OP_REVALIDATE) {
127 ret = d->d_op->d_revalidate(d, flags);
128 if (ret < 0)
129 return ret;
130 if (!ret) {
131 if (!(flags & LOOKUP_RCU))
132 d_invalidate(d);
133 return -ESTALE;
134 }
135 }
136 }
137 return 1;
138}
139
140static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
141{
142 struct ovl_entry *oe = dentry->d_fsdata;
143 unsigned int i;
144 int ret = 1;
145
146 for (i = 0; i < oe->numlower; i++) {
147 struct dentry *d = oe->lowerstack[i].dentry;
148
149 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
150 ret = d->d_op->d_weak_revalidate(d, flags);
151 if (ret <= 0)
152 break;
153 }
154 }
155 return ret;
156}
157
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200158static const struct dentry_operations ovl_dentry_operations = {
159 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400160 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200161};
162
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200163static const struct dentry_operations ovl_reval_dentry_operations = {
164 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400165 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200166 .d_revalidate = ovl_dentry_revalidate,
167 .d_weak_revalidate = ovl_dentry_weak_revalidate,
168};
169
Amir Goldstein13cf1992017-06-12 09:54:40 +0300170static struct kmem_cache *ovl_inode_cachep;
171
172static struct inode *ovl_alloc_inode(struct super_block *sb)
173{
174 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
175
Hirofumi Nakagawab3885bd2017-09-26 03:09:53 +0900176 if (!oi)
177 return NULL;
178
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200179 oi->cache = NULL;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200180 oi->redirect = NULL;
Miklos Szeredi04a01ac2017-07-04 22:03:16 +0200181 oi->version = 0;
Miklos Szeredi13c72072017-07-04 22:03:16 +0200182 oi->flags = 0;
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200183 oi->__upperdentry = NULL;
Miklos Szeredi25b77132017-07-04 22:03:16 +0200184 oi->lower = NULL;
Amir Goldsteina015daf2017-06-21 15:28:51 +0300185 mutex_init(&oi->lock);
Miklos Szeredi25b77132017-07-04 22:03:16 +0200186
Amir Goldstein13cf1992017-06-12 09:54:40 +0300187 return &oi->vfs_inode;
188}
189
190static void ovl_i_callback(struct rcu_head *head)
191{
192 struct inode *inode = container_of(head, struct inode, i_rcu);
193
194 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
195}
196
197static void ovl_destroy_inode(struct inode *inode)
198{
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200199 struct ovl_inode *oi = OVL_I(inode);
200
201 dput(oi->__upperdentry);
Miklos Szeredicf31c462017-07-04 22:03:16 +0200202 kfree(oi->redirect);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200203 ovl_dir_cache_free(inode);
Amir Goldsteina015daf2017-06-21 15:28:51 +0300204 mutex_destroy(&oi->lock);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200205
Amir Goldstein13cf1992017-06-12 09:54:40 +0300206 call_rcu(&inode->i_rcu, ovl_i_callback);
207}
208
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100209static void ovl_free_fs(struct ovl_fs *ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210{
Miklos Szeredidd662662014-12-13 00:59:43 +0100211 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200212
Amir Goldstein02bcd152017-06-21 15:28:36 +0300213 dput(ufs->indexdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200214 dput(ufs->workdir);
Amir Goldstein85fdee12017-09-29 10:21:21 +0300215 if (ufs->workdir_locked)
216 ovl_inuse_unlock(ufs->workbasedir);
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300217 dput(ufs->workbasedir);
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100218 if (ufs->upperdir_locked)
Amir Goldstein2cac0c02017-06-21 15:28:33 +0300219 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200220 mntput(ufs->upper_mnt);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +0200221 for (i = 0; i < ufs->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500222 mntput(ufs->lower_layers[i].mnt);
Chandan Rajendra2a9c6d02017-11-01 20:12:49 +0200223 free_anon_bdev(ufs->lower_layers[i].pseudo_dev);
224 }
Chandan Rajendrab9343632017-07-24 01:57:54 -0500225 kfree(ufs->lower_layers);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200226
Erez Zadokf45827e82014-10-24 00:14:38 +0200227 kfree(ufs->config.lowerdir);
228 kfree(ufs->config.upperdir);
229 kfree(ufs->config.workdir);
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100230 if (ufs->creator_cred)
231 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200232 kfree(ufs);
233}
234
Miklos Szeredia9075cd2017-11-10 09:39:15 +0100235static void ovl_put_super(struct super_block *sb)
236{
237 struct ovl_fs *ofs = sb->s_fs_info;
238
239 ovl_free_fs(ofs);
240}
241
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200242static int ovl_sync_fs(struct super_block *sb, int wait)
243{
244 struct ovl_fs *ufs = sb->s_fs_info;
245 struct super_block *upper_sb;
246 int ret;
247
248 if (!ufs->upper_mnt)
249 return 0;
250 upper_sb = ufs->upper_mnt->mnt_sb;
251 if (!upper_sb->s_op->sync_fs)
252 return 0;
253
254 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
255 down_read(&upper_sb->s_umount);
256 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
257 up_read(&upper_sb->s_umount);
258 return ret;
259}
260
Andy Whitcroftcc259632014-10-24 00:14:38 +0200261/**
262 * ovl_statfs
263 * @sb: The overlayfs super block
264 * @buf: The struct kstatfs to fill in with stats
265 *
266 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100267 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200268 */
269static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
270{
271 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
272 struct dentry *root_dentry = dentry->d_sb->s_root;
273 struct path path;
274 int err;
275
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100276 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200277
278 err = vfs_statfs(&path, buf);
279 if (!err) {
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100280 buf->f_namelen = ofs->namelen;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200281 buf->f_type = OVERLAYFS_SUPER_MAGIC;
282 }
283
284 return err;
285}
286
Amir Goldstein02bcd152017-06-21 15:28:36 +0300287/* Will this overlay be forced to mount/remount ro? */
288static bool ovl_force_readonly(struct ovl_fs *ufs)
289{
290 return (!ufs->upper_mnt || !ufs->workdir);
291}
292
Erez Zadokf45827e82014-10-24 00:14:38 +0200293/**
294 * ovl_show_options
295 *
296 * Prints the mount options for a given superblock.
297 * Returns zero; does not fail.
298 */
299static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
300{
301 struct super_block *sb = dentry->d_sb;
302 struct ovl_fs *ufs = sb->s_fs_info;
303
Kees Cooka068acf2015-09-04 15:44:57 -0700304 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100305 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700306 seq_show_option(m, "upperdir", ufs->config.upperdir);
307 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100308 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200309 if (ufs->config.default_permissions)
310 seq_puts(m, ",default_permissions");
Amir Goldsteinc5bef3a2016-11-22 11:47:09 +0200311 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
312 seq_printf(m, ",redirect_dir=%s",
313 ufs->config.redirect_dir ? "on" : "off");
Amir Goldstein02bcd152017-06-21 15:28:36 +0300314 if (ufs->config.index != ovl_index_def)
315 seq_printf(m, ",index=%s",
316 ufs->config.index ? "on" : "off");
Erez Zadokf45827e82014-10-24 00:14:38 +0200317 return 0;
318}
319
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900320static int ovl_remount(struct super_block *sb, int *flags, char *data)
321{
322 struct ovl_fs *ufs = sb->s_fs_info;
323
Amir Goldstein02bcd152017-06-21 15:28:36 +0300324 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900325 return -EROFS;
326
327 return 0;
328}
329
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330static const struct super_operations ovl_super_operations = {
Amir Goldstein13cf1992017-06-12 09:54:40 +0300331 .alloc_inode = ovl_alloc_inode,
332 .destroy_inode = ovl_destroy_inode,
333 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200334 .put_super = ovl_put_super,
Amir Goldsteine593b2b2017-01-23 14:32:21 +0200335 .sync_fs = ovl_sync_fs,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200336 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200337 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900338 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339};
340
341enum {
342 OPT_LOWERDIR,
343 OPT_UPPERDIR,
344 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200345 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredia6c60652016-12-16 11:02:56 +0100346 OPT_REDIRECT_DIR_ON,
347 OPT_REDIRECT_DIR_OFF,
Amir Goldstein02bcd152017-06-21 15:28:36 +0300348 OPT_INDEX_ON,
349 OPT_INDEX_OFF,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350 OPT_ERR,
351};
352
353static const match_table_t ovl_tokens = {
354 {OPT_LOWERDIR, "lowerdir=%s"},
355 {OPT_UPPERDIR, "upperdir=%s"},
356 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200357 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredia6c60652016-12-16 11:02:56 +0100358 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
359 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
Amir Goldstein02bcd152017-06-21 15:28:36 +0300360 {OPT_INDEX_ON, "index=on"},
361 {OPT_INDEX_OFF, "index=off"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362 {OPT_ERR, NULL}
363};
364
Miklos Szeredi91c77942014-11-20 16:40:00 +0100365static char *ovl_next_opt(char **s)
366{
367 char *sbegin = *s;
368 char *p;
369
370 if (sbegin == NULL)
371 return NULL;
372
373 for (p = sbegin; *p; p++) {
374 if (*p == '\\') {
375 p++;
376 if (!*p)
377 break;
378 } else if (*p == ',') {
379 *p = '\0';
380 *s = p + 1;
381 return sbegin;
382 }
383 }
384 *s = NULL;
385 return sbegin;
386}
387
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200388static int ovl_parse_opt(char *opt, struct ovl_config *config)
389{
390 char *p;
391
Miklos Szeredi91c77942014-11-20 16:40:00 +0100392 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200393 int token;
394 substring_t args[MAX_OPT_ARGS];
395
396 if (!*p)
397 continue;
398
399 token = match_token(p, ovl_tokens, args);
400 switch (token) {
401 case OPT_UPPERDIR:
402 kfree(config->upperdir);
403 config->upperdir = match_strdup(&args[0]);
404 if (!config->upperdir)
405 return -ENOMEM;
406 break;
407
408 case OPT_LOWERDIR:
409 kfree(config->lowerdir);
410 config->lowerdir = match_strdup(&args[0]);
411 if (!config->lowerdir)
412 return -ENOMEM;
413 break;
414
415 case OPT_WORKDIR:
416 kfree(config->workdir);
417 config->workdir = match_strdup(&args[0]);
418 if (!config->workdir)
419 return -ENOMEM;
420 break;
421
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200422 case OPT_DEFAULT_PERMISSIONS:
423 config->default_permissions = true;
424 break;
425
Miklos Szeredia6c60652016-12-16 11:02:56 +0100426 case OPT_REDIRECT_DIR_ON:
427 config->redirect_dir = true;
428 break;
429
430 case OPT_REDIRECT_DIR_OFF:
431 config->redirect_dir = false;
432 break;
433
Amir Goldstein02bcd152017-06-21 15:28:36 +0300434 case OPT_INDEX_ON:
435 config->index = true;
436 break;
437
438 case OPT_INDEX_OFF:
439 config->index = false;
440 break;
441
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200442 default:
hujianyangbead55e2015-01-15 13:17:36 +0800443 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200444 return -EINVAL;
445 }
446 }
hujianyang71cbad72015-01-15 13:20:57 +0800447
448 /* Workdir is useless in non-upper mount */
449 if (!config->upperdir && config->workdir) {
450 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
451 config->workdir);
452 kfree(config->workdir);
453 config->workdir = NULL;
454 }
455
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200456 return 0;
457}
458
459#define OVL_WORKDIR_NAME "work"
Amir Goldstein02bcd152017-06-21 15:28:36 +0300460#define OVL_INDEXDIR_NAME "index"
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200461
Miklos Szeredi6e882562017-11-10 09:39:15 +0100462static struct dentry *ovl_workdir_create(struct ovl_fs *ufs,
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300463 const char *name, bool persist)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200464{
Miklos Szeredi6e882562017-11-10 09:39:15 +0100465 struct inode *dir = ufs->workbasedir->d_inode;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300466 struct vfsmount *mnt = ufs->upper_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200467 struct dentry *work;
468 int err;
469 bool retried = false;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300470 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200471
472 err = mnt_want_write(mnt);
473 if (err)
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300474 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200475
Al Viro59551022016-01-22 15:40:57 -0500476 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300477 locked = true;
478
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200479retry:
Miklos Szeredi6e882562017-11-10 09:39:15 +0100480 work = lookup_one_len(name, ufs->workbasedir, strlen(name));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200481
482 if (!IS_ERR(work)) {
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200483 struct iattr attr = {
484 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000485 .ia_mode = S_IFDIR | 0,
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200486 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200487
488 if (work->d_inode) {
489 err = -EEXIST;
490 if (retried)
491 goto out_dput;
492
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300493 if (persist)
494 goto out_unlock;
495
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200496 retried = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200497 ovl_workdir_cleanup(dir, mnt, work, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200498 dput(work);
499 goto retry;
500 }
501
Al Viro32a3d842016-12-04 17:33:17 +0000502 err = ovl_create_real(dir, work,
503 &(struct cattr){.mode = S_IFDIR | 0},
504 NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200505 if (err)
506 goto out_dput;
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200507
Miklos Szeredicb348ed2016-10-04 14:40:44 +0200508 /*
509 * Try to remove POSIX ACL xattrs from workdir. We are good if:
510 *
511 * a) success (there was a POSIX ACL xattr and was removed)
512 * b) -ENODATA (there was no POSIX ACL xattr)
513 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
514 *
515 * There are various other error values that could effectively
516 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
517 * if the xattr name is too long), but the set of filesystems
518 * allowed as upper are limited to "normal" ones, where checking
519 * for the above two errors is sufficient.
520 */
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200521 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200522 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200523 goto out_dput;
524
525 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
Miklos Szeredie1ff3dd2016-09-05 13:55:20 +0200526 if (err && err != -ENODATA && err != -EOPNOTSUPP)
Miklos Szeredic11b9fd2016-09-01 11:11:59 +0200527 goto out_dput;
528
529 /* Clear any inherited mode bits */
530 inode_lock(work->d_inode);
531 err = notify_change(work, &attr, NULL);
532 inode_unlock(work->d_inode);
533 if (err)
534 goto out_dput;
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300535 } else {
536 err = PTR_ERR(work);
537 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200538 }
539out_unlock:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200540 mnt_drop_write(mnt);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300541 if (locked)
542 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200543
544 return work;
545
546out_dput:
547 dput(work);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300548out_err:
549 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
550 ufs->config.workdir, name, -err);
Amir Goldstein6b8aa122017-06-21 15:28:35 +0300551 work = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200552 goto out_unlock;
553}
554
Miklos Szeredi91c77942014-11-20 16:40:00 +0100555static void ovl_unescape(char *s)
556{
557 char *d = s;
558
559 for (;; s++, d++) {
560 if (*s == '\\')
561 s++;
562 *d = *s;
563 if (!*s)
564 break;
565 }
566}
567
Miklos Szerediab508822014-12-13 00:59:49 +0100568static int ovl_mount_dir_noesc(const char *name, struct path *path)
569{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100570 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100571
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100572 if (!*name) {
573 pr_err("overlayfs: empty lowerdir\n");
574 goto out;
575 }
Miklos Szerediab508822014-12-13 00:59:49 +0100576 err = kern_path(name, LOOKUP_FOLLOW, path);
577 if (err) {
578 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
579 goto out;
580 }
581 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200582 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100583 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
584 goto out_put;
585 }
Miklos Szeredi2b8c30e2016-12-16 11:02:56 +0100586 if (!d_is_dir(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100587 pr_err("overlayfs: '%s' not a directory\n", name);
588 goto out_put;
589 }
590 return 0;
591
592out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100593 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100594out:
595 return err;
596}
597
598static int ovl_mount_dir(const char *name, struct path *path)
599{
600 int err = -ENOMEM;
601 char *tmp = kstrdup(name, GFP_KERNEL);
602
603 if (tmp) {
604 ovl_unescape(tmp);
605 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200606
607 if (!err)
608 if (ovl_dentry_remote(path->dentry)) {
609 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
610 tmp);
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100611 path_put_init(path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200612 err = -EINVAL;
613 }
Miklos Szerediab508822014-12-13 00:59:49 +0100614 kfree(tmp);
615 }
616 return err;
617}
618
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100619static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
620 const char *name)
621{
622 struct kstatfs statfs;
623 int err = vfs_statfs(path, &statfs);
624
625 if (err)
626 pr_err("overlayfs: statfs failed on '%s'\n", name);
627 else
628 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
629
630 return err;
631}
632
633static int ovl_lower_dir(const char *name, struct path *path,
634 struct ovl_fs *ofs, int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100635{
636 int err;
Miklos Szerediab508822014-12-13 00:59:49 +0100637
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100638 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100639 if (err)
640 goto out;
641
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100642 err = ovl_check_namelen(path, ofs, name);
643 if (err)
Miklos Szerediab508822014-12-13 00:59:49 +0100644 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100645
Miklos Szerediab508822014-12-13 00:59:49 +0100646 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
647
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200648 if (ovl_dentry_remote(path->dentry))
649 *remote = true;
650
Amir Goldstein02bcd152017-06-21 15:28:36 +0300651 /*
652 * The inodes index feature needs to encode and decode file
653 * handles, so it requires that all layers support them.
654 */
655 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
656 ofs->config.index = false;
657 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
658 }
659
Miklos Szerediab508822014-12-13 00:59:49 +0100660 return 0;
661
662out_put:
Miklos Szeredi8aafcb52017-11-09 10:23:28 +0100663 path_put_init(path);
Miklos Szerediab508822014-12-13 00:59:49 +0100664out:
665 return err;
666}
667
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200668/* Workdir should not be subdir of upperdir and vice versa */
669static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
670{
671 bool ok = false;
672
673 if (workdir != upperdir) {
674 ok = (lock_rename(workdir, upperdir) == NULL);
675 unlock_rename(workdir, upperdir);
676 }
677 return ok;
678}
679
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100680static unsigned int ovl_split_lowerdirs(char *str)
681{
682 unsigned int ctr = 1;
683 char *s, *d;
684
685 for (s = d = str;; s++, d++) {
686 if (*s == '\\') {
687 s++;
688 } else if (*s == ':') {
689 *d = '\0';
690 ctr++;
691 continue;
692 }
693 *d = *s;
694 if (!*s)
695 break;
696 }
697 return ctr;
698}
699
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200700static int __maybe_unused
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200701ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
702 struct dentry *dentry, struct inode *inode,
703 const char *name, void *buffer, size_t size)
704{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200705 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200706}
707
708static int __maybe_unused
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200709ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
710 struct dentry *dentry, struct inode *inode,
711 const char *name, const void *value,
712 size_t size, int flags)
Miklos Szeredid837a492016-07-29 12:05:24 +0200713{
714 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200715 struct inode *realinode = ovl_inode_real(inode);
Miklos Szeredid837a492016-07-29 12:05:24 +0200716 struct posix_acl *acl = NULL;
717 int err;
718
719 /* Check that everything is OK before copy-up */
720 if (value) {
721 acl = posix_acl_from_xattr(&init_user_ns, value, size);
722 if (IS_ERR(acl))
723 return PTR_ERR(acl);
724 }
725 err = -EOPNOTSUPP;
726 if (!IS_POSIXACL(d_inode(workdir)))
727 goto out_acl_release;
728 if (!realinode->i_op->set_acl)
729 goto out_acl_release;
730 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
731 err = acl ? -EACCES : 0;
732 goto out_acl_release;
733 }
734 err = -EPERM;
735 if (!inode_owner_or_capable(inode))
736 goto out_acl_release;
737
738 posix_acl_release(acl);
739
Miklos Szeredifd3220d2016-10-31 14:42:14 +0100740 /*
741 * Check if sgid bit needs to be cleared (actual setacl operation will
742 * be done with mounter's capabilities and so that won't do it for us).
743 */
744 if (unlikely(inode->i_mode & S_ISGID) &&
745 handler->flags == ACL_TYPE_ACCESS &&
746 !in_group_p(inode->i_gid) &&
747 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
748 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
749
750 err = ovl_setattr(dentry, &iattr);
751 if (err)
752 return err;
753 }
754
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200755 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
Miklos Szeredice315132016-09-01 11:12:00 +0200756 if (!err)
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200757 ovl_copyattr(ovl_inode_real(inode), inode);
Miklos Szeredice315132016-09-01 11:12:00 +0200758
759 return err;
Miklos Szeredid837a492016-07-29 12:05:24 +0200760
761out_acl_release:
762 posix_acl_release(acl);
763 return err;
764}
765
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200766static int ovl_own_xattr_get(const struct xattr_handler *handler,
767 struct dentry *dentry, struct inode *inode,
768 const char *name, void *buffer, size_t size)
769{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200770 return -EOPNOTSUPP;
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200771}
772
Miklos Szeredid837a492016-07-29 12:05:24 +0200773static int ovl_own_xattr_set(const struct xattr_handler *handler,
774 struct dentry *dentry, struct inode *inode,
775 const char *name, const void *value,
776 size_t size, int flags)
777{
Amir Goldstein48fab5d2016-11-16 11:22:39 +0200778 return -EOPNOTSUPP;
Miklos Szeredid837a492016-07-29 12:05:24 +0200779}
780
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200781static int ovl_other_xattr_get(const struct xattr_handler *handler,
782 struct dentry *dentry, struct inode *inode,
783 const char *name, void *buffer, size_t size)
784{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200785 return ovl_xattr_get(dentry, inode, name, buffer, size);
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200786}
787
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200788static int ovl_other_xattr_set(const struct xattr_handler *handler,
789 struct dentry *dentry, struct inode *inode,
790 const char *name, const void *value,
791 size_t size, int flags)
792{
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200793 return ovl_xattr_set(dentry, inode, name, value, size, flags);
Andreas Gruenbacher0e585cc2016-08-22 17:22:11 +0200794}
795
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200796static const struct xattr_handler __maybe_unused
797ovl_posix_acl_access_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200798 .name = XATTR_NAME_POSIX_ACL_ACCESS,
799 .flags = ACL_TYPE_ACCESS,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200800 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200801 .set = ovl_posix_acl_xattr_set,
802};
803
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200804static const struct xattr_handler __maybe_unused
805ovl_posix_acl_default_xattr_handler = {
Miklos Szeredid837a492016-07-29 12:05:24 +0200806 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
807 .flags = ACL_TYPE_DEFAULT,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200808 .get = ovl_posix_acl_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200809 .set = ovl_posix_acl_xattr_set,
810};
811
812static const struct xattr_handler ovl_own_xattr_handler = {
813 .prefix = OVL_XATTR_PREFIX,
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200814 .get = ovl_own_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200815 .set = ovl_own_xattr_set,
816};
817
818static const struct xattr_handler ovl_other_xattr_handler = {
819 .prefix = "", /* catch all */
Andreas Gruenbacher0eb45fc2016-08-22 17:52:55 +0200820 .get = ovl_other_xattr_get,
Miklos Szeredid837a492016-07-29 12:05:24 +0200821 .set = ovl_other_xattr_set,
822};
823
824static const struct xattr_handler *ovl_xattr_handlers[] = {
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200825#ifdef CONFIG_FS_POSIX_ACL
Miklos Szeredid837a492016-07-29 12:05:24 +0200826 &ovl_posix_acl_access_xattr_handler,
827 &ovl_posix_acl_default_xattr_handler,
Andreas Gruenbacher0c97be22016-08-22 16:36:49 +0200828#endif
Miklos Szeredid837a492016-07-29 12:05:24 +0200829 &ovl_own_xattr_handler,
830 &ovl_other_xattr_handler,
831 NULL
832};
833
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100834static int ovl_get_upperpath(struct ovl_fs *ufs, struct path *upperpath)
835{
836 int err;
837
838 err = ovl_mount_dir(ufs->config.upperdir, upperpath);
839 if (err)
840 goto out;
841
842 /* Upper fs should not be r/o */
843 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
844 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
845 err = -EINVAL;
846 goto out;
847 }
848
849 err = ovl_check_namelen(upperpath, ufs, ufs->config.upperdir);
850 if (err)
851 goto out;
852
853 err = -EBUSY;
854 if (ovl_inuse_trylock(upperpath->dentry)) {
855 ufs->upperdir_locked = true;
856 } else if (ufs->config.index) {
857 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
858 goto out;
859 } else {
860 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
861 }
862 err = 0;
863out:
864 return err;
865}
866
Miklos Szeredi87ad4472017-11-09 10:23:28 +0100867static int ovl_get_workpath(struct ovl_fs *ufs, struct path *upperpath,
868 struct path *workpath)
869{
870 int err;
871
872 err = ovl_mount_dir(ufs->config.workdir, workpath);
873 if (err)
874 goto out;
875
876 err = -EINVAL;
877 if (upperpath->mnt != workpath->mnt) {
878 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
879 goto out;
880 }
881 if (!ovl_workdir_ok(workpath->dentry, upperpath->dentry)) {
882 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
883 goto out;
884 }
885
886 err = -EBUSY;
887 if (ovl_inuse_trylock(workpath->dentry)) {
888 ufs->workdir_locked = true;
889 } else if (ufs->config.index) {
890 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
891 goto out;
892 } else {
893 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
894 }
895
Miklos Szeredi95e6d412017-11-09 10:23:29 +0100896 ufs->workbasedir = dget(workpath->dentry);
Miklos Szeredi87ad4472017-11-09 10:23:28 +0100897 err = 0;
898out:
899 return err;
900}
901
Miklos Szeredi21a3b312017-11-09 10:23:28 +0100902static int ovl_get_upper(struct ovl_fs *ufs, struct path *upperpath)
903{
904 struct vfsmount *upper_mnt;
905
906 upper_mnt = clone_private_mount(upperpath);
907 if (IS_ERR(upper_mnt)) {
908 pr_err("overlayfs: failed to clone upperpath\n");
909 return PTR_ERR(upper_mnt);
910 }
911
912 /* Don't inherit atime flags */
913 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
914 ufs->upper_mnt = upper_mnt;
915
916 return 0;
917}
918
Miklos Szeredi6e882562017-11-10 09:39:15 +0100919static int ovl_get_workdir(struct ovl_fs *ufs, struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100920{
921 struct dentry *temp;
922 int err;
923
Miklos Szeredi6e882562017-11-10 09:39:15 +0100924 ufs->workdir = ovl_workdir_create(ufs, OVL_WORKDIR_NAME, false);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100925 if (!ufs->workdir)
926 return 0;
927
928 /*
929 * Upper should support d_type, else whiteouts are visible. Given
930 * workdir and upper are on same fs, we can do iterate_dir() on
931 * workdir. This check requires successful creation of workdir in
932 * previous step.
933 */
934 err = ovl_check_d_type_supported(workpath);
935 if (err < 0)
936 return err;
937
938 /*
939 * We allowed this configuration and don't want to break users over
940 * kernel upgrade. So warn instead of erroring out.
941 */
942 if (!err)
943 pr_warn("overlayfs: upper fs needs to support d_type.\n");
944
945 /* Check if upper/work fs supports O_TMPFILE */
946 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
947 ufs->tmpfile = !IS_ERR(temp);
948 if (ufs->tmpfile)
949 dput(temp);
950 else
951 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
952
953 /*
954 * Check if upper/work fs supports trusted.overlay.* xattr
955 */
956 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
957 if (err) {
958 ufs->noxattr = true;
959 pr_warn("overlayfs: upper fs does not support xattr.\n");
960 } else {
961 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
962 }
963
964 /* Check if upper/work fs supports file handles */
965 if (ufs->config.index &&
966 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
967 ufs->config.index = false;
968 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
969 }
970
971 return 0;
972}
973
Miklos Szeredi6e882562017-11-10 09:39:15 +0100974static int ovl_get_indexdir(struct ovl_fs *ufs, struct ovl_entry *oe,
Miklos Szeredi95e6d412017-11-09 10:23:29 +0100975 struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +0100976{
977 int err;
978
979 /* Verify lower root is upper root origin */
980 err = ovl_verify_origin(upperpath->dentry,
981 oe->lowerstack[0].layer->mnt,
982 oe->lowerstack[0].dentry,
983 false, true);
984 if (err) {
985 pr_err("overlayfs: failed to verify upper root origin\n");
986 goto out;
987 }
988
Miklos Szeredi6e882562017-11-10 09:39:15 +0100989 ufs->indexdir = ovl_workdir_create(ufs, OVL_INDEXDIR_NAME, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +0100990 if (ufs->indexdir) {
991 /* Verify upper root is index dir origin */
992 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
993 upperpath->dentry, true, true);
994 if (err)
995 pr_err("overlayfs: failed to verify index dir origin\n");
996
997 /* Cleanup bad/stale/orphan index entries */
998 if (!err)
999 err = ovl_indexdir_cleanup(ufs->indexdir,
1000 ufs->upper_mnt,
1001 oe->lowerstack,
1002 oe->numlower);
1003 }
1004 if (err || !ufs->indexdir)
1005 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1006
1007out:
1008 return err;
1009}
1010
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001011static int ovl_get_lowerstack(struct super_block *sb, struct ovl_fs *ufs,
1012 struct path **stackp, unsigned int *stacklenp)
1013{
1014 int err;
1015 char *lowertmp, *lower;
1016 struct path *stack;
1017 unsigned int stacklen, numlower, i;
1018 bool remote = false;
1019
1020 err = -ENOMEM;
1021 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1022 if (!lowertmp)
1023 goto out;
1024
1025 err = -EINVAL;
1026 stacklen = ovl_split_lowerdirs(lowertmp);
1027 if (stacklen > OVL_MAX_STACK) {
1028 pr_err("overlayfs: too many lower directories, limit is %d\n",
1029 OVL_MAX_STACK);
1030 goto out;
1031 } else if (!ufs->config.upperdir && stacklen == 1) {
1032 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1033 goto out;
1034 }
1035
1036 err = -ENOMEM;
1037 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1038 if (!stack)
1039 goto out;
1040
1041 err = -EINVAL;
1042 lower = lowertmp;
1043 for (numlower = 0; numlower < stacklen; numlower++) {
1044 err = ovl_lower_dir(lower, &stack[numlower], ufs,
1045 &sb->s_stack_depth, &remote);
1046 if (err)
1047 goto out_free_stack;
1048
1049 lower = strchr(lower, '\0') + 1;
1050 }
1051
1052 err = -EINVAL;
1053 sb->s_stack_depth++;
1054 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1055 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1056 goto out_free_stack;
1057 }
1058
1059 *stackp = stack;
1060 *stacklenp = numlower;
1061
1062 if (remote)
1063 sb->s_d_op = &ovl_reval_dentry_operations;
1064 else
1065 sb->s_d_op = &ovl_dentry_operations;
1066
1067 err = 0;
1068
1069out:
1070 kfree(lowertmp);
1071 return err;
1072
1073out_free_stack:
1074 for (i = 0; i < numlower; i++)
1075 path_put(&stack[i]);
1076 kfree(stack);
1077 goto out;
1078}
1079
Miklos Szeredic0d91fb2017-11-09 10:23:28 +01001080static int ovl_get_lower_layers(struct ovl_fs *ufs, struct path *stack,
1081 unsigned int numlower)
1082{
1083 int err;
1084 unsigned int i;
1085
1086 err = -ENOMEM;
1087 ufs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1088 GFP_KERNEL);
1089 if (ufs->lower_layers == NULL)
1090 goto out;
1091 for (i = 0; i < numlower; i++) {
1092 struct vfsmount *mnt;
1093 dev_t dev;
1094
1095 err = get_anon_bdev(&dev);
1096 if (err) {
1097 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1098 goto out;
1099 }
1100
1101 mnt = clone_private_mount(&stack[i]);
1102 err = PTR_ERR(mnt);
1103 if (IS_ERR(mnt)) {
1104 pr_err("overlayfs: failed to clone lowerpath\n");
1105 free_anon_bdev(dev);
1106 goto out;
1107 }
1108 /*
1109 * Make lower layers R/O. That way fchmod/fchown on lower file
1110 * will fail instead of modifying lower fs.
1111 */
1112 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1113
1114 ufs->lower_layers[ufs->numlower].mnt = mnt;
1115 ufs->lower_layers[ufs->numlower].pseudo_dev = dev;
1116 ufs->numlower++;
1117
1118 /* Check if all lower layers are on same sb */
1119 if (i == 0)
1120 ufs->same_sb = mnt->mnt_sb;
1121 else if (ufs->same_sb != mnt->mnt_sb)
1122 ufs->same_sb = NULL;
1123 }
1124 err = 0;
1125out:
1126 return err;
1127}
1128
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001129static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1130{
Kees Cook33006cd2017-03-29 14:02:19 -07001131 struct path upperpath = { };
1132 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001133 struct dentry *root_dentry;
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001134 struct ovl_entry *oe = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001135 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001136 struct path *stack = NULL;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001137 unsigned int numlower = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +01001138 unsigned int i;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001139 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001140 int err;
1141
Erez Zadokf45827e82014-10-24 00:14:38 +02001142 err = -ENOMEM;
1143 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1144 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001145 goto out;
1146
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001147 ufs->creator_cred = cred = prepare_creds();
1148 if (!cred)
1149 goto out_err;
1150
Miklos Szeredi688ea0e2016-12-16 11:02:57 +01001151 ufs->config.redirect_dir = ovl_redirect_dir_def;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001152 ufs->config.index = ovl_index_def;
Erez Zadokf45827e82014-10-24 00:14:38 +02001153 err = ovl_parse_opt((char *) data, &ufs->config);
1154 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001155 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001156
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001157 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001158 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001159 if (!silent)
1160 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001161 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001162 }
1163
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001164 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001165 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001166 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001167 if (!ufs->config.workdir) {
1168 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001169 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001170 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001171
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +01001172 err = ovl_get_upperpath(ufs, &upperpath);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001173 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001174 goto out_err;
Amir Goldstein2cac0c02017-06-21 15:28:33 +03001175
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001176 err = ovl_get_upper(ufs, &upperpath);
1177 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001178 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001179
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001180 err = ovl_get_workpath(ufs, &upperpath, &workpath);
1181 if (err)
1182 goto out_err;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001183
Miklos Szeredi6e882562017-11-10 09:39:15 +01001184 err = ovl_get_workdir(ufs, &workpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001185 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001186 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001187
Miklos Szeredi6e882562017-11-10 09:39:15 +01001188 if (!ufs->workdir)
1189 sb->s_flags |= MS_RDONLY;
1190
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001191 sb->s_stack_depth = ufs->upper_mnt->mnt_sb->s_stack_depth;
1192 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
1193
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001194 }
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001195 err = ovl_get_lowerstack(sb, ufs, &stack, &numlower);
1196 if (err)
1197 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001198
Miklos Szeredic0d91fb2017-11-09 10:23:28 +01001199 err = ovl_get_lower_layers(ufs, stack, numlower);
1200 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001201 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001202
hujianyang71cbad72015-01-15 13:20:57 +08001203 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1204 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001205 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001206 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1207 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001208
Chandan Rajendrab9343632017-07-24 01:57:54 -05001209 err = -ENOMEM;
1210 oe = ovl_alloc_entry(numlower);
1211 if (!oe)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001212 goto out_err;
Chandan Rajendrab9343632017-07-24 01:57:54 -05001213
1214 for (i = 0; i < numlower; i++) {
1215 oe->lowerstack[i].dentry = stack[i].dentry;
1216 oe->lowerstack[i].layer = &(ufs->lower_layers[i]);
1217 }
1218
Amir Goldstein02bcd152017-06-21 15:28:36 +03001219 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
Miklos Szeredi6e882562017-11-10 09:39:15 +01001220 err = ovl_get_indexdir(ufs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001221 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001222 goto out_err;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001223
1224 if (!ufs->indexdir)
1225 sb->s_flags |= MS_RDONLY;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001226 }
1227
1228 /* Show index=off/on in /proc/mounts for any of the reasons above */
1229 if (!ufs->indexdir)
1230 ufs->config.index = false;
1231
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001232 /* Never override disk quota limits or use reserved space */
1233 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1234
Vivek Goyal655042c2016-10-14 03:03:36 +02001235 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1236 sb->s_op = &ovl_super_operations;
1237 sb->s_xattr = ovl_xattr_handlers;
1238 sb->s_fs_info = ufs;
1239 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1240
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001241 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001242 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001243 if (!root_dentry)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001244 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001245
1246 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001247 for (i = 0; i < numlower; i++)
1248 mntput(stack[i].mnt);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001249 kfree(stack);
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001250 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001251
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001252 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001253 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001254 if (ovl_is_impuredir(upperpath.dentry))
1255 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001256 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001257
1258 root_dentry->d_fsdata = oe;
1259
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001260 /* Root is always merge -> can have whiteouts */
1261 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001262 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1263 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001264
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001265 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001266
1267 return 0;
1268
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001269out_err:
Chandan Rajendrab9343632017-07-24 01:57:54 -05001270 kfree(oe);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001271 for (i = 0; i < numlower; i++)
1272 path_put(&stack[i]);
1273 kfree(stack);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001274 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001275 path_put(&upperpath);
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001276 ovl_free_fs(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001277out:
1278 return err;
1279}
1280
1281static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1282 const char *dev_name, void *raw_data)
1283{
1284 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1285}
1286
1287static struct file_system_type ovl_fs_type = {
1288 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001289 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001290 .mount = ovl_mount,
1291 .kill_sb = kill_anon_super,
1292};
Miklos Szeredief94b182014-11-20 16:39:59 +01001293MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001294
Amir Goldstein13cf1992017-06-12 09:54:40 +03001295static void ovl_inode_init_once(void *foo)
1296{
1297 struct ovl_inode *oi = foo;
1298
1299 inode_init_once(&oi->vfs_inode);
1300}
1301
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001302static int __init ovl_init(void)
1303{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001304 int err;
1305
1306 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1307 sizeof(struct ovl_inode), 0,
1308 (SLAB_RECLAIM_ACCOUNT|
1309 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1310 ovl_inode_init_once);
1311 if (ovl_inode_cachep == NULL)
1312 return -ENOMEM;
1313
1314 err = register_filesystem(&ovl_fs_type);
1315 if (err)
1316 kmem_cache_destroy(ovl_inode_cachep);
1317
1318 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001319}
1320
1321static void __exit ovl_exit(void)
1322{
1323 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001324
1325 /*
1326 * Make sure all delayed rcu free inodes are flushed before we
1327 * destroy cache.
1328 */
1329 rcu_barrier();
1330 kmem_cache_destroy(ovl_inode_cachep);
1331
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001332}
1333
1334module_init(ovl_init);
1335module_exit(ovl_exit);