blob: 03202ad7b481befafe82600e828064e4dc9abc2c [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 Szeredi50649752017-11-10 09:39:15 +0100834static int ovl_get_upper(struct ovl_fs *ufs, struct path *upperpath)
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100835{
Miklos Szeredi50649752017-11-10 09:39:15 +0100836 struct vfsmount *upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100837 int err;
838
839 err = ovl_mount_dir(ufs->config.upperdir, upperpath);
840 if (err)
841 goto out;
842
843 /* Upper fs should not be r/o */
844 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
845 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
846 err = -EINVAL;
847 goto out;
848 }
849
850 err = ovl_check_namelen(upperpath, ufs, ufs->config.upperdir);
851 if (err)
852 goto out;
853
854 err = -EBUSY;
855 if (ovl_inuse_trylock(upperpath->dentry)) {
856 ufs->upperdir_locked = true;
857 } else if (ufs->config.index) {
858 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
859 goto out;
860 } else {
861 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
862 }
Miklos Szeredi50649752017-11-10 09:39:15 +0100863
864 upper_mnt = clone_private_mount(upperpath);
865 err = PTR_ERR(upper_mnt);
866 if (IS_ERR(upper_mnt)) {
867 pr_err("overlayfs: failed to clone upperpath\n");
868 goto out;
869 }
870
871 /* Don't inherit atime flags */
872 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
873 ufs->upper_mnt = upper_mnt;
Miklos Szeredi6ee8acf2017-11-09 10:23:28 +0100874 err = 0;
875out:
876 return err;
877}
878
Miklos Szeredi6e882562017-11-10 09:39:15 +0100879static int ovl_get_workdir(struct ovl_fs *ufs, struct path *workpath)
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100880{
881 struct dentry *temp;
882 int err;
883
Miklos Szeredi6e882562017-11-10 09:39:15 +0100884 ufs->workdir = ovl_workdir_create(ufs, OVL_WORKDIR_NAME, false);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +0100885 if (!ufs->workdir)
886 return 0;
887
888 /*
889 * Upper should support d_type, else whiteouts are visible. Given
890 * workdir and upper are on same fs, we can do iterate_dir() on
891 * workdir. This check requires successful creation of workdir in
892 * previous step.
893 */
894 err = ovl_check_d_type_supported(workpath);
895 if (err < 0)
896 return err;
897
898 /*
899 * We allowed this configuration and don't want to break users over
900 * kernel upgrade. So warn instead of erroring out.
901 */
902 if (!err)
903 pr_warn("overlayfs: upper fs needs to support d_type.\n");
904
905 /* Check if upper/work fs supports O_TMPFILE */
906 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
907 ufs->tmpfile = !IS_ERR(temp);
908 if (ufs->tmpfile)
909 dput(temp);
910 else
911 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
912
913 /*
914 * Check if upper/work fs supports trusted.overlay.* xattr
915 */
916 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
917 if (err) {
918 ufs->noxattr = true;
919 pr_warn("overlayfs: upper fs does not support xattr.\n");
920 } else {
921 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
922 }
923
924 /* Check if upper/work fs supports file handles */
925 if (ufs->config.index &&
926 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
927 ufs->config.index = false;
928 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
929 }
930
931 return 0;
932}
933
Miklos Szeredi520d7c82017-11-10 09:39:15 +0100934static int ovl_get_workpath(struct ovl_fs *ufs, struct path *upperpath,
935 struct path *workpath)
936{
937 int err;
938
939 err = ovl_mount_dir(ufs->config.workdir, workpath);
940 if (err)
941 goto out;
942
943 err = -EINVAL;
944 if (upperpath->mnt != workpath->mnt) {
945 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
946 goto out;
947 }
948 if (!ovl_workdir_ok(workpath->dentry, upperpath->dentry)) {
949 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
950 goto out;
951 }
952
953 err = -EBUSY;
954 if (ovl_inuse_trylock(workpath->dentry)) {
955 ufs->workdir_locked = true;
956 } else if (ufs->config.index) {
957 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
958 goto out;
959 } else {
960 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
961 }
962
963 ufs->workbasedir = dget(workpath->dentry);
964 err = 0;
965out:
966 return err;
967}
968
Miklos Szeredi6e882562017-11-10 09:39:15 +0100969static int ovl_get_indexdir(struct ovl_fs *ufs, struct ovl_entry *oe,
Miklos Szeredi95e6d412017-11-09 10:23:29 +0100970 struct path *upperpath)
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +0100971{
972 int err;
973
974 /* Verify lower root is upper root origin */
975 err = ovl_verify_origin(upperpath->dentry,
976 oe->lowerstack[0].layer->mnt,
977 oe->lowerstack[0].dentry,
978 false, true);
979 if (err) {
980 pr_err("overlayfs: failed to verify upper root origin\n");
981 goto out;
982 }
983
Miklos Szeredi6e882562017-11-10 09:39:15 +0100984 ufs->indexdir = ovl_workdir_create(ufs, OVL_INDEXDIR_NAME, true);
Miklos Szeredif7e3a7d2017-11-09 10:23:28 +0100985 if (ufs->indexdir) {
986 /* Verify upper root is index dir origin */
987 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
988 upperpath->dentry, true, true);
989 if (err)
990 pr_err("overlayfs: failed to verify index dir origin\n");
991
992 /* Cleanup bad/stale/orphan index entries */
993 if (!err)
994 err = ovl_indexdir_cleanup(ufs->indexdir,
995 ufs->upper_mnt,
996 oe->lowerstack,
997 oe->numlower);
998 }
999 if (err || !ufs->indexdir)
1000 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1001
1002out:
1003 return err;
1004}
1005
Miklos Szeredi520d7c82017-11-10 09:39:15 +01001006static int ovl_get_lower_layers(struct ovl_fs *ufs, struct path *stack,
1007 unsigned int numlower)
1008{
1009 int err;
1010 unsigned int i;
1011
1012 err = -ENOMEM;
1013 ufs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1014 GFP_KERNEL);
1015 if (ufs->lower_layers == NULL)
1016 goto out;
1017 for (i = 0; i < numlower; i++) {
1018 struct vfsmount *mnt;
1019 dev_t dev;
1020
1021 err = get_anon_bdev(&dev);
1022 if (err) {
1023 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1024 goto out;
1025 }
1026
1027 mnt = clone_private_mount(&stack[i]);
1028 err = PTR_ERR(mnt);
1029 if (IS_ERR(mnt)) {
1030 pr_err("overlayfs: failed to clone lowerpath\n");
1031 free_anon_bdev(dev);
1032 goto out;
1033 }
1034 /*
1035 * Make lower layers R/O. That way fchmod/fchown on lower file
1036 * will fail instead of modifying lower fs.
1037 */
1038 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1039
1040 ufs->lower_layers[ufs->numlower].mnt = mnt;
1041 ufs->lower_layers[ufs->numlower].pseudo_dev = dev;
1042 ufs->numlower++;
1043
1044 /* Check if all lower layers are on same sb */
1045 if (i == 0)
1046 ufs->same_sb = mnt->mnt_sb;
1047 else if (ufs->same_sb != mnt->mnt_sb)
1048 ufs->same_sb = NULL;
1049 }
1050 err = 0;
1051out:
1052 return err;
1053}
1054
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001055static int ovl_get_lowerstack(struct super_block *sb, struct ovl_fs *ufs,
1056 struct path **stackp, unsigned int *stacklenp)
1057{
1058 int err;
1059 char *lowertmp, *lower;
1060 struct path *stack;
1061 unsigned int stacklen, numlower, i;
1062 bool remote = false;
1063
1064 err = -ENOMEM;
1065 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1066 if (!lowertmp)
1067 goto out;
1068
1069 err = -EINVAL;
1070 stacklen = ovl_split_lowerdirs(lowertmp);
1071 if (stacklen > OVL_MAX_STACK) {
1072 pr_err("overlayfs: too many lower directories, limit is %d\n",
1073 OVL_MAX_STACK);
1074 goto out;
1075 } else if (!ufs->config.upperdir && stacklen == 1) {
1076 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1077 goto out;
1078 }
1079
1080 err = -ENOMEM;
1081 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1082 if (!stack)
1083 goto out;
1084
1085 err = -EINVAL;
1086 lower = lowertmp;
1087 for (numlower = 0; numlower < stacklen; numlower++) {
1088 err = ovl_lower_dir(lower, &stack[numlower], ufs,
1089 &sb->s_stack_depth, &remote);
1090 if (err)
1091 goto out_free_stack;
1092
1093 lower = strchr(lower, '\0') + 1;
1094 }
1095
1096 err = -EINVAL;
1097 sb->s_stack_depth++;
1098 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1099 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1100 goto out_free_stack;
1101 }
1102
1103 *stackp = stack;
1104 *stacklenp = numlower;
1105
1106 if (remote)
1107 sb->s_d_op = &ovl_reval_dentry_operations;
1108 else
1109 sb->s_d_op = &ovl_dentry_operations;
1110
1111 err = 0;
1112
1113out:
1114 kfree(lowertmp);
1115 return err;
1116
1117out_free_stack:
1118 for (i = 0; i < numlower; i++)
1119 path_put(&stack[i]);
1120 kfree(stack);
1121 goto out;
1122}
1123
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001124static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1125{
Kees Cook33006cd2017-03-29 14:02:19 -07001126 struct path upperpath = { };
1127 struct path workpath = { };
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001128 struct dentry *root_dentry;
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001129 struct ovl_entry *oe = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001130 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001131 struct path *stack = NULL;
Miklos Szeredi53dbb0b2017-11-09 10:23:28 +01001132 unsigned int numlower = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +01001133 unsigned int i;
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001134 struct cred *cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001135 int err;
1136
Erez Zadokf45827e82014-10-24 00:14:38 +02001137 err = -ENOMEM;
1138 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1139 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001140 goto out;
1141
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001142 ufs->creator_cred = cred = prepare_creds();
1143 if (!cred)
1144 goto out_err;
1145
Miklos Szeredi688ea0e2016-12-16 11:02:57 +01001146 ufs->config.redirect_dir = ovl_redirect_dir_def;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001147 ufs->config.index = ovl_index_def;
Erez Zadokf45827e82014-10-24 00:14:38 +02001148 err = ovl_parse_opt((char *) data, &ufs->config);
1149 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001150 goto out_err;
Erez Zadokf45827e82014-10-24 00:14:38 +02001151
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001152 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001153 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +03001154 if (!silent)
1155 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001156 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001157 }
1158
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001159 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001160 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001161 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001162 if (!ufs->config.workdir) {
1163 pr_err("overlayfs: missing 'workdir'\n");
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001164 goto out_err;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001165 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001166
Miklos Szeredi21a3b312017-11-09 10:23:28 +01001167 err = ovl_get_upper(ufs, &upperpath);
1168 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001169 goto out_err;
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001170
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001171 err = ovl_get_workpath(ufs, &upperpath, &workpath);
1172 if (err)
1173 goto out_err;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001174
Miklos Szeredi6e882562017-11-10 09:39:15 +01001175 err = ovl_get_workdir(ufs, &workpath);
Miklos Szeredi8ed61dc2017-11-09 10:23:28 +01001176 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001177 goto out_err;
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001178
Miklos Szeredi6e882562017-11-10 09:39:15 +01001179 if (!ufs->workdir)
1180 sb->s_flags |= MS_RDONLY;
1181
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001182 sb->s_stack_depth = ufs->upper_mnt->mnt_sb->s_stack_depth;
1183 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
1184
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001185 }
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001186 err = ovl_get_lowerstack(sb, ufs, &stack, &numlower);
1187 if (err)
1188 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001189
Miklos Szeredic0d91fb2017-11-09 10:23:28 +01001190 err = ovl_get_lower_layers(ufs, stack, numlower);
1191 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001192 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001193
hujianyang71cbad72015-01-15 13:20:57 +08001194 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1195 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001196 sb->s_flags |= MS_RDONLY;
Amir Goldstein7bcd74b2017-03-22 08:42:21 -04001197 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1198 ufs->same_sb = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001199
Chandan Rajendrab9343632017-07-24 01:57:54 -05001200 err = -ENOMEM;
1201 oe = ovl_alloc_entry(numlower);
1202 if (!oe)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001203 goto out_err;
Chandan Rajendrab9343632017-07-24 01:57:54 -05001204
1205 for (i = 0; i < numlower; i++) {
1206 oe->lowerstack[i].dentry = stack[i].dentry;
1207 oe->lowerstack[i].layer = &(ufs->lower_layers[i]);
1208 }
1209
Amir Goldstein02bcd152017-06-21 15:28:36 +03001210 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
Miklos Szeredi6e882562017-11-10 09:39:15 +01001211 err = ovl_get_indexdir(ufs, oe, &upperpath);
Amir Goldstein54fb3472017-06-21 15:28:38 +03001212 if (err)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001213 goto out_err;
Miklos Szeredi6e882562017-11-10 09:39:15 +01001214
1215 if (!ufs->indexdir)
1216 sb->s_flags |= MS_RDONLY;
Amir Goldstein02bcd152017-06-21 15:28:36 +03001217 }
1218
1219 /* Show index=off/on in /proc/mounts for any of the reasons above */
1220 if (!ufs->indexdir)
1221 ufs->config.index = false;
1222
Konstantin Khlebnikov51f8f3c2017-01-10 21:30:21 +03001223 /* Never override disk quota limits or use reserved space */
1224 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1225
Vivek Goyal655042c2016-10-14 03:03:36 +02001226 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1227 sb->s_op = &ovl_super_operations;
1228 sb->s_xattr = ovl_xattr_handlers;
1229 sb->s_fs_info = ufs;
1230 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1231
Miklos Szeredic6fe6252017-11-10 09:39:15 +01001232 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +01001233 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001234 if (!root_dentry)
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001235 goto out_err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001236
1237 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001238 for (i = 0; i < numlower; i++)
1239 mntput(stack[i].mnt);
Chandan Rajendrab9343632017-07-24 01:57:54 -05001240 kfree(stack);
Miklos Szeredi95e6d412017-11-09 10:23:29 +01001241 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001242
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001243 if (upperpath.dentry) {
Miklos Szeredi55acc662017-07-04 22:03:18 +02001244 oe->has_upper = true;
Miklos Szeredi13c72072017-07-04 22:03:16 +02001245 if (ovl_is_impuredir(upperpath.dentry))
1246 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001247 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001248
1249 root_dentry->d_fsdata = oe;
1250
Amir Goldsteinb79e05a2017-06-25 16:37:17 +03001251 /* Root is always merge -> can have whiteouts */
1252 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001253 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1254 ovl_dentry_lower(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001255
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001256 sb->s_root = root_dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001257
1258 return 0;
1259
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001260out_err:
Chandan Rajendrab9343632017-07-24 01:57:54 -05001261 kfree(oe);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001262 for (i = 0; i < numlower; i++)
1263 path_put(&stack[i]);
1264 kfree(stack);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001265 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001266 path_put(&upperpath);
Miklos Szeredia9075cd2017-11-10 09:39:15 +01001267 ovl_free_fs(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001268out:
1269 return err;
1270}
1271
1272static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1273 const char *dev_name, void *raw_data)
1274{
1275 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1276}
1277
1278static struct file_system_type ovl_fs_type = {
1279 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001280 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001281 .mount = ovl_mount,
1282 .kill_sb = kill_anon_super,
1283};
Miklos Szeredief94b182014-11-20 16:39:59 +01001284MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001285
Amir Goldstein13cf1992017-06-12 09:54:40 +03001286static void ovl_inode_init_once(void *foo)
1287{
1288 struct ovl_inode *oi = foo;
1289
1290 inode_init_once(&oi->vfs_inode);
1291}
1292
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001293static int __init ovl_init(void)
1294{
Amir Goldstein13cf1992017-06-12 09:54:40 +03001295 int err;
1296
1297 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1298 sizeof(struct ovl_inode), 0,
1299 (SLAB_RECLAIM_ACCOUNT|
1300 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1301 ovl_inode_init_once);
1302 if (ovl_inode_cachep == NULL)
1303 return -ENOMEM;
1304
1305 err = register_filesystem(&ovl_fs_type);
1306 if (err)
1307 kmem_cache_destroy(ovl_inode_cachep);
1308
1309 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001310}
1311
1312static void __exit ovl_exit(void)
1313{
1314 unregister_filesystem(&ovl_fs_type);
Amir Goldstein13cf1992017-06-12 09:54:40 +03001315
1316 /*
1317 * Make sure all delayed rcu free inodes are flushed before we
1318 * destroy cache.
1319 */
1320 rcu_barrier();
1321 kmem_cache_destroy(ovl_inode_cachep);
1322
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001323}
1324
1325module_init(ovl_init);
1326module_exit(ovl_exit);