blob: 6b0111ae2ceb3513b15db06231e521fd28d11028 [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
10#include <linux/fs.h>
11#include <linux/namei.h>
Miklos Szeredicf9a6782015-12-11 16:30:49 +010012#include <linux/pagemap.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020013#include <linux/xattr.h>
14#include <linux/security.h>
15#include <linux/mount.h>
16#include <linux/slab.h>
17#include <linux/parser.h>
18#include <linux/module.h>
Andrew Mortone458bcd2016-01-20 15:01:13 -080019#include <linux/pagemap.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include <linux/sched.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020021#include <linux/statfs.h>
Erez Zadokf45827e82014-10-24 00:14:38 +020022#include <linux/seq_file.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020023#include "overlayfs.h"
24
25MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
26MODULE_DESCRIPTION("Overlay filesystem");
27MODULE_LICENSE("GPL");
28
Erez Zadokf45827e82014-10-24 00:14:38 +020029struct ovl_config {
30 char *lowerdir;
31 char *upperdir;
32 char *workdir;
Miklos Szeredi8d3095f2015-10-12 17:11:44 +020033 bool default_permissions;
Erez Zadokf45827e82014-10-24 00:14:38 +020034};
35
Miklos Szeredie9be9d52014-10-24 00:14:38 +020036/* private information held for overlayfs's superblock */
37struct ovl_fs {
38 struct vfsmount *upper_mnt;
Miklos Szeredidd662662014-12-13 00:59:43 +010039 unsigned numlower;
40 struct vfsmount **lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020041 struct dentry *workdir;
Andy Whitcroftcc259632014-10-24 00:14:38 +020042 long lower_namelen;
Erez Zadokf45827e82014-10-24 00:14:38 +020043 /* pathnames of lower and upper dirs, for show_options */
44 struct ovl_config config;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020045};
46
47struct ovl_dir_cache;
48
49/* private information held for every overlayfs dentry */
50struct ovl_entry {
51 struct dentry *__upperdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020052 struct ovl_dir_cache *cache;
53 union {
54 struct {
55 u64 version;
56 bool opaque;
57 };
58 struct rcu_head rcu;
59 };
Miklos Szeredidd662662014-12-13 00:59:43 +010060 unsigned numlower;
61 struct path lowerstack[];
Miklos Szeredie9be9d52014-10-24 00:14:38 +020062};
63
Miklos Szeredia78d9f02014-12-13 00:59:52 +010064#define OVL_MAX_STACK 500
65
Miklos Szeredidd662662014-12-13 00:59:43 +010066static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
67{
68 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
69}
Miklos Szeredie9be9d52014-10-24 00:14:38 +020070
71enum ovl_path_type ovl_path_type(struct dentry *dentry)
72{
73 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi1afaba12014-12-13 00:59:42 +010074 enum ovl_path_type type = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020075
76 if (oe->__upperdentry) {
Miklos Szeredi1afaba12014-12-13 00:59:42 +010077 type = __OVL_PATH_UPPER;
78
Konstantin Khlebnikov45d11732016-01-31 16:17:53 +030079 /*
80 * Non-dir dentry can hold lower dentry from previous
81 * location. Its purity depends only on opaque flag.
82 */
83 if (oe->numlower && S_ISDIR(dentry->d_inode->i_mode))
84 type |= __OVL_PATH_MERGE;
85 else if (!oe->opaque)
Miklos Szeredi1afaba12014-12-13 00:59:42 +010086 type |= __OVL_PATH_PURE;
Miklos Szeredi9d7459d2014-12-13 00:59:44 +010087 } else {
88 if (oe->numlower > 1)
89 type |= __OVL_PATH_MERGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020090 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +010091 return type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020092}
93
94static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
95{
Miklos Szeredi71d50922014-11-20 16:40:01 +010096 return lockless_dereference(oe->__upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020097}
98
99void ovl_path_upper(struct dentry *dentry, struct path *path)
100{
101 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
102 struct ovl_entry *oe = dentry->d_fsdata;
103
104 path->mnt = ofs->upper_mnt;
105 path->dentry = ovl_upperdentry_dereference(oe);
106}
107
108enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
109{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200110 enum ovl_path_type type = ovl_path_type(dentry);
111
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100112 if (!OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200113 ovl_path_lower(dentry, path);
114 else
115 ovl_path_upper(dentry, path);
116
117 return type;
118}
119
120struct dentry *ovl_dentry_upper(struct dentry *dentry)
121{
122 struct ovl_entry *oe = dentry->d_fsdata;
123
124 return ovl_upperdentry_dereference(oe);
125}
126
127struct dentry *ovl_dentry_lower(struct dentry *dentry)
128{
129 struct ovl_entry *oe = dentry->d_fsdata;
130
Miklos Szeredidd662662014-12-13 00:59:43 +0100131 return __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132}
133
134struct dentry *ovl_dentry_real(struct dentry *dentry)
135{
136 struct ovl_entry *oe = dentry->d_fsdata;
137 struct dentry *realdentry;
138
139 realdentry = ovl_upperdentry_dereference(oe);
140 if (!realdentry)
Miklos Szeredidd662662014-12-13 00:59:43 +0100141 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200142
143 return realdentry;
144}
145
146struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
147{
148 struct dentry *realdentry;
149
150 realdentry = ovl_upperdentry_dereference(oe);
151 if (realdentry) {
152 *is_upper = true;
153 } else {
Miklos Szeredidd662662014-12-13 00:59:43 +0100154 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200155 *is_upper = false;
156 }
157 return realdentry;
158}
159
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200160struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
161 bool is_upper)
162{
163 if (is_upper) {
164 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
165
166 return ofs->upper_mnt;
167 } else {
168 return oe->numlower ? oe->lowerstack[0].mnt : NULL;
169 }
170}
171
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200172struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
173{
174 struct ovl_entry *oe = dentry->d_fsdata;
175
176 return oe->cache;
177}
178
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200179bool ovl_is_default_permissions(struct inode *inode)
180{
181 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
182
183 return ofs->config.default_permissions;
184}
185
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200186void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
187{
188 struct ovl_entry *oe = dentry->d_fsdata;
189
190 oe->cache = cache;
191}
192
193void ovl_path_lower(struct dentry *dentry, struct path *path)
194{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200195 struct ovl_entry *oe = dentry->d_fsdata;
196
Miklos Szeredidd662662014-12-13 00:59:43 +0100197 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200198}
199
200int ovl_want_write(struct dentry *dentry)
201{
202 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
203 return mnt_want_write(ofs->upper_mnt);
204}
205
206void ovl_drop_write(struct dentry *dentry)
207{
208 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
209 mnt_drop_write(ofs->upper_mnt);
210}
211
212struct dentry *ovl_workdir(struct dentry *dentry)
213{
214 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
215 return ofs->workdir;
216}
217
218bool ovl_dentry_is_opaque(struct dentry *dentry)
219{
220 struct ovl_entry *oe = dentry->d_fsdata;
221 return oe->opaque;
222}
223
224void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
225{
226 struct ovl_entry *oe = dentry->d_fsdata;
227 oe->opaque = opaque;
228}
229
230void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
231{
232 struct ovl_entry *oe = dentry->d_fsdata;
233
Al Viro59551022016-01-22 15:40:57 -0500234 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200235 WARN_ON(oe->__upperdentry);
236 BUG_ON(!upperdentry->d_inode);
237 /*
238 * Make sure upperdentry is consistent before making it visible to
239 * ovl_upperdentry_dereference().
240 */
241 smp_wmb();
242 oe->__upperdentry = upperdentry;
243}
244
245void ovl_dentry_version_inc(struct dentry *dentry)
246{
247 struct ovl_entry *oe = dentry->d_fsdata;
248
Al Viro59551022016-01-22 15:40:57 -0500249 WARN_ON(!inode_is_locked(dentry->d_inode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200250 oe->version++;
251}
252
253u64 ovl_dentry_version_get(struct dentry *dentry)
254{
255 struct ovl_entry *oe = dentry->d_fsdata;
256
Al Viro59551022016-01-22 15:40:57 -0500257 WARN_ON(!inode_is_locked(dentry->d_inode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200258 return oe->version;
259}
260
261bool ovl_is_whiteout(struct dentry *dentry)
262{
263 struct inode *inode = dentry->d_inode;
264
265 return inode && IS_WHITEOUT(inode);
266}
267
268static bool ovl_is_opaquedir(struct dentry *dentry)
269{
270 int res;
271 char val;
272 struct inode *inode = dentry->d_inode;
273
274 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
275 return false;
276
hujianyangcead89b2014-11-24 18:25:21 +0800277 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200278 if (res == 1 && val == 'y')
279 return true;
280
281 return false;
282}
283
284static void ovl_dentry_release(struct dentry *dentry)
285{
286 struct ovl_entry *oe = dentry->d_fsdata;
287
288 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +0100289 unsigned int i;
290
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200291 dput(oe->__upperdentry);
Miklos Szeredidd662662014-12-13 00:59:43 +0100292 for (i = 0; i < oe->numlower; i++)
293 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200294 kfree_rcu(oe, rcu);
295 }
296}
297
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200298static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
299{
300 struct ovl_entry *oe = dentry->d_fsdata;
301 unsigned int i;
302 int ret = 1;
303
304 for (i = 0; i < oe->numlower; i++) {
305 struct dentry *d = oe->lowerstack[i].dentry;
306
307 if (d->d_flags & DCACHE_OP_REVALIDATE) {
308 ret = d->d_op->d_revalidate(d, flags);
309 if (ret < 0)
310 return ret;
311 if (!ret) {
312 if (!(flags & LOOKUP_RCU))
313 d_invalidate(d);
314 return -ESTALE;
315 }
316 }
317 }
318 return 1;
319}
320
321static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
322{
323 struct ovl_entry *oe = dentry->d_fsdata;
324 unsigned int i;
325 int ret = 1;
326
327 for (i = 0; i < oe->numlower; i++) {
328 struct dentry *d = oe->lowerstack[i].dentry;
329
330 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
331 ret = d->d_op->d_weak_revalidate(d, flags);
332 if (ret <= 0)
333 break;
334 }
335 }
336 return ret;
337}
338
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339static const struct dentry_operations ovl_dentry_operations = {
340 .d_release = ovl_dentry_release,
David Howells4bacc9c2015-06-18 14:32:31 +0100341 .d_select_inode = ovl_d_select_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200342};
343
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200344static const struct dentry_operations ovl_reval_dentry_operations = {
345 .d_release = ovl_dentry_release,
Konstantin Khlebnikovb5891cf2016-01-31 16:22:16 +0300346 .d_select_inode = ovl_d_select_inode,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200347 .d_revalidate = ovl_dentry_revalidate,
348 .d_weak_revalidate = ovl_dentry_weak_revalidate,
349};
350
Miklos Szeredidd662662014-12-13 00:59:43 +0100351static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200352{
Miklos Szeredidd662662014-12-13 00:59:43 +0100353 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
354 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
355
356 if (oe)
357 oe->numlower = numlower;
358
359 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360}
361
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200362static bool ovl_dentry_remote(struct dentry *dentry)
363{
364 return dentry->d_flags &
365 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
366}
367
368static bool ovl_dentry_weird(struct dentry *dentry)
369{
370 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
371 DCACHE_MANAGE_TRANSIT |
372 DCACHE_OP_HASH |
373 DCACHE_OP_COMPARE);
374}
375
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200376static inline struct dentry *ovl_lookup_real(struct dentry *dir,
377 struct qstr *name)
378{
379 struct dentry *dentry;
380
Al Viro59551022016-01-22 15:40:57 -0500381 inode_lock(dir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200382 dentry = lookup_one_len(name->name, dir, name->len);
Al Viro59551022016-01-22 15:40:57 -0500383 inode_unlock(dir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200384
385 if (IS_ERR(dentry)) {
386 if (PTR_ERR(dentry) == -ENOENT)
387 dentry = NULL;
388 } else if (!dentry->d_inode) {
389 dput(dentry);
390 dentry = NULL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200391 } else if (ovl_dentry_weird(dentry)) {
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200392 dput(dentry);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200393 /* Don't support traversing automounts and other weirdness */
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200394 dentry = ERR_PTR(-EREMOTE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200395 }
396 return dentry;
397}
398
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100399/*
400 * Returns next layer in stack starting from top.
401 * Returns -1 if this is the last layer.
402 */
403int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
404{
405 struct ovl_entry *oe = dentry->d_fsdata;
406
407 BUG_ON(idx < 0);
408 if (idx == 0) {
409 ovl_path_upper(dentry, path);
410 if (path->dentry)
411 return oe->numlower ? 1 : -1;
412 idx++;
413 }
414 BUG_ON(idx > oe->numlower);
415 *path = oe->lowerstack[idx - 1];
416
417 return (idx < oe->numlower) ? idx + 1 : -1;
418}
419
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200420struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
421 unsigned int flags)
422{
423 struct ovl_entry *oe;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100424 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
425 struct path *stack = NULL;
426 struct dentry *upperdir, *upperdentry = NULL;
427 unsigned int ctr = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200428 struct inode *inode = NULL;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100429 bool upperopaque = false;
430 struct dentry *this, *prev = NULL;
431 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200432 int err;
433
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100434 upperdir = ovl_upperdentry_dereference(poe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200435 if (upperdir) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100436 this = ovl_lookup_real(upperdir, &dentry->d_name);
437 err = PTR_ERR(this);
438 if (IS_ERR(this))
439 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200440
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100441 if (this) {
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200442 if (unlikely(ovl_dentry_remote(this))) {
443 dput(this);
444 err = -EREMOTE;
445 goto out;
446 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100447 if (ovl_is_whiteout(this)) {
448 dput(this);
449 this = NULL;
450 upperopaque = true;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100451 } else if (poe->numlower && ovl_is_opaquedir(this)) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100452 upperopaque = true;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200453 }
454 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100455 upperdentry = prev = this;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200456 }
457
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100458 if (!upperopaque && poe->numlower) {
459 err = -ENOMEM;
460 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
461 if (!stack)
462 goto out_put_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200463 }
464
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100465 for (i = 0; !upperopaque && i < poe->numlower; i++) {
466 bool opaque = false;
467 struct path lowerpath = poe->lowerstack[i];
468
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100469 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
470 err = PTR_ERR(this);
Miklos Szeredi09e10322014-12-13 00:59:45 +0100471 if (IS_ERR(this)) {
472 /*
473 * If it's positive, then treat ENAMETOOLONG as ENOENT.
474 */
475 if (err == -ENAMETOOLONG && (upperdentry || ctr))
476 continue;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100477 goto out_put;
Miklos Szeredi09e10322014-12-13 00:59:45 +0100478 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100479 if (!this)
480 continue;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100481 if (ovl_is_whiteout(this)) {
482 dput(this);
483 break;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100484 }
485 /*
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100486 * Only makes sense to check opaque dir if this is not the
487 * lowermost layer.
488 */
489 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
490 opaque = true;
hujianyanga425c032015-01-06 16:10:01 +0800491
492 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
493 !S_ISDIR(this->d_inode->i_mode))) {
494 /*
495 * FIXME: check for upper-opaqueness maybe better done
496 * in remove code.
497 */
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100498 if (prev == upperdentry)
499 upperopaque = true;
500 dput(this);
501 break;
502 }
hujianyanga425c032015-01-06 16:10:01 +0800503 /*
504 * If this is a non-directory then stop here.
505 */
506 if (!S_ISDIR(this->d_inode->i_mode))
507 opaque = true;
508
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100509 stack[ctr].dentry = this;
510 stack[ctr].mnt = lowerpath.mnt;
511 ctr++;
512 prev = this;
513 if (opaque)
514 break;
515 }
516
517 oe = ovl_alloc_entry(ctr);
518 err = -ENOMEM;
519 if (!oe)
520 goto out_put;
521
522 if (upperdentry || ctr) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200523 struct dentry *realdentry;
524
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100525 realdentry = upperdentry ? upperdentry : stack[0].dentry;
526
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200527 err = -ENOMEM;
528 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
529 oe);
530 if (!inode)
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100531 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200532 ovl_copyattr(realdentry->d_inode, inode);
533 }
534
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100535 oe->opaque = upperopaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200536 oe->__upperdentry = upperdentry;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100537 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
538 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200539 dentry->d_fsdata = oe;
540 d_add(dentry, inode);
541
542 return NULL;
543
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100544out_free_oe:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200545 kfree(oe);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100546out_put:
547 for (i = 0; i < ctr; i++)
548 dput(stack[i].dentry);
549 kfree(stack);
550out_put_upper:
551 dput(upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200552out:
553 return ERR_PTR(err);
554}
555
556struct file *ovl_path_open(struct path *path, int flags)
557{
558 return dentry_open(path, flags, current_cred());
559}
560
561static void ovl_put_super(struct super_block *sb)
562{
563 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100564 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200565
566 dput(ufs->workdir);
567 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100568 for (i = 0; i < ufs->numlower; i++)
569 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300570 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200571
Erez Zadokf45827e82014-10-24 00:14:38 +0200572 kfree(ufs->config.lowerdir);
573 kfree(ufs->config.upperdir);
574 kfree(ufs->config.workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200575 kfree(ufs);
576}
577
Andy Whitcroftcc259632014-10-24 00:14:38 +0200578/**
579 * ovl_statfs
580 * @sb: The overlayfs super block
581 * @buf: The struct kstatfs to fill in with stats
582 *
583 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100584 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200585 */
586static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
587{
588 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
589 struct dentry *root_dentry = dentry->d_sb->s_root;
590 struct path path;
591 int err;
592
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100593 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200594
595 err = vfs_statfs(&path, buf);
596 if (!err) {
597 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
598 buf->f_type = OVERLAYFS_SUPER_MAGIC;
599 }
600
601 return err;
602}
603
Erez Zadokf45827e82014-10-24 00:14:38 +0200604/**
605 * ovl_show_options
606 *
607 * Prints the mount options for a given superblock.
608 * Returns zero; does not fail.
609 */
610static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
611{
612 struct super_block *sb = dentry->d_sb;
613 struct ovl_fs *ufs = sb->s_fs_info;
614
Kees Cooka068acf2015-09-04 15:44:57 -0700615 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100616 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700617 seq_show_option(m, "upperdir", ufs->config.upperdir);
618 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100619 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200620 if (ufs->config.default_permissions)
621 seq_puts(m, ",default_permissions");
Erez Zadokf45827e82014-10-24 00:14:38 +0200622 return 0;
623}
624
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900625static int ovl_remount(struct super_block *sb, int *flags, char *data)
626{
627 struct ovl_fs *ufs = sb->s_fs_info;
628
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200629 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900630 return -EROFS;
631
632 return 0;
633}
634
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200635static const struct super_operations ovl_super_operations = {
636 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200637 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200638 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900639 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200640};
641
642enum {
643 OPT_LOWERDIR,
644 OPT_UPPERDIR,
645 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200646 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200647 OPT_ERR,
648};
649
650static const match_table_t ovl_tokens = {
651 {OPT_LOWERDIR, "lowerdir=%s"},
652 {OPT_UPPERDIR, "upperdir=%s"},
653 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200654 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200655 {OPT_ERR, NULL}
656};
657
Miklos Szeredi91c77942014-11-20 16:40:00 +0100658static char *ovl_next_opt(char **s)
659{
660 char *sbegin = *s;
661 char *p;
662
663 if (sbegin == NULL)
664 return NULL;
665
666 for (p = sbegin; *p; p++) {
667 if (*p == '\\') {
668 p++;
669 if (!*p)
670 break;
671 } else if (*p == ',') {
672 *p = '\0';
673 *s = p + 1;
674 return sbegin;
675 }
676 }
677 *s = NULL;
678 return sbegin;
679}
680
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200681static int ovl_parse_opt(char *opt, struct ovl_config *config)
682{
683 char *p;
684
Miklos Szeredi91c77942014-11-20 16:40:00 +0100685 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200686 int token;
687 substring_t args[MAX_OPT_ARGS];
688
689 if (!*p)
690 continue;
691
692 token = match_token(p, ovl_tokens, args);
693 switch (token) {
694 case OPT_UPPERDIR:
695 kfree(config->upperdir);
696 config->upperdir = match_strdup(&args[0]);
697 if (!config->upperdir)
698 return -ENOMEM;
699 break;
700
701 case OPT_LOWERDIR:
702 kfree(config->lowerdir);
703 config->lowerdir = match_strdup(&args[0]);
704 if (!config->lowerdir)
705 return -ENOMEM;
706 break;
707
708 case OPT_WORKDIR:
709 kfree(config->workdir);
710 config->workdir = match_strdup(&args[0]);
711 if (!config->workdir)
712 return -ENOMEM;
713 break;
714
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200715 case OPT_DEFAULT_PERMISSIONS:
716 config->default_permissions = true;
717 break;
718
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200719 default:
hujianyangbead55e2015-01-15 13:17:36 +0800720 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200721 return -EINVAL;
722 }
723 }
hujianyang71cbad72015-01-15 13:20:57 +0800724
725 /* Workdir is useless in non-upper mount */
726 if (!config->upperdir && config->workdir) {
727 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
728 config->workdir);
729 kfree(config->workdir);
730 config->workdir = NULL;
731 }
732
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200733 return 0;
734}
735
736#define OVL_WORKDIR_NAME "work"
737
738static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
739 struct dentry *dentry)
740{
741 struct inode *dir = dentry->d_inode;
742 struct dentry *work;
743 int err;
744 bool retried = false;
745
746 err = mnt_want_write(mnt);
747 if (err)
748 return ERR_PTR(err);
749
Al Viro59551022016-01-22 15:40:57 -0500750 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200751retry:
752 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
753 strlen(OVL_WORKDIR_NAME));
754
755 if (!IS_ERR(work)) {
756 struct kstat stat = {
757 .mode = S_IFDIR | 0,
758 };
759
760 if (work->d_inode) {
761 err = -EEXIST;
762 if (retried)
763 goto out_dput;
764
765 retried = true;
766 ovl_cleanup(dir, work);
767 dput(work);
768 goto retry;
769 }
770
771 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
772 if (err)
773 goto out_dput;
774 }
775out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500776 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200777 mnt_drop_write(mnt);
778
779 return work;
780
781out_dput:
782 dput(work);
783 work = ERR_PTR(err);
784 goto out_unlock;
785}
786
Miklos Szeredi91c77942014-11-20 16:40:00 +0100787static void ovl_unescape(char *s)
788{
789 char *d = s;
790
791 for (;; s++, d++) {
792 if (*s == '\\')
793 s++;
794 *d = *s;
795 if (!*s)
796 break;
797 }
798}
799
Miklos Szerediab508822014-12-13 00:59:49 +0100800static int ovl_mount_dir_noesc(const char *name, struct path *path)
801{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100802 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100803
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100804 if (!*name) {
805 pr_err("overlayfs: empty lowerdir\n");
806 goto out;
807 }
Miklos Szerediab508822014-12-13 00:59:49 +0100808 err = kern_path(name, LOOKUP_FOLLOW, path);
809 if (err) {
810 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
811 goto out;
812 }
813 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200814 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100815 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
816 goto out_put;
817 }
818 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
819 pr_err("overlayfs: '%s' not a directory\n", name);
820 goto out_put;
821 }
822 return 0;
823
824out_put:
825 path_put(path);
826out:
827 return err;
828}
829
830static int ovl_mount_dir(const char *name, struct path *path)
831{
832 int err = -ENOMEM;
833 char *tmp = kstrdup(name, GFP_KERNEL);
834
835 if (tmp) {
836 ovl_unescape(tmp);
837 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200838
839 if (!err)
840 if (ovl_dentry_remote(path->dentry)) {
841 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
842 tmp);
843 path_put(path);
844 err = -EINVAL;
845 }
Miklos Szerediab508822014-12-13 00:59:49 +0100846 kfree(tmp);
847 }
848 return err;
849}
850
851static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200852 int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100853{
854 int err;
855 struct kstatfs statfs;
856
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100857 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100858 if (err)
859 goto out;
860
861 err = vfs_statfs(path, &statfs);
862 if (err) {
863 pr_err("overlayfs: statfs failed on '%s'\n", name);
864 goto out_put;
865 }
866 *namelen = max(*namelen, statfs.f_namelen);
867 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
868
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200869 if (ovl_dentry_remote(path->dentry))
870 *remote = true;
871
Miklos Szerediab508822014-12-13 00:59:49 +0100872 return 0;
873
874out_put:
875 path_put(path);
876out:
877 return err;
878}
879
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200880/* Workdir should not be subdir of upperdir and vice versa */
881static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
882{
883 bool ok = false;
884
885 if (workdir != upperdir) {
886 ok = (lock_rename(workdir, upperdir) == NULL);
887 unlock_rename(workdir, upperdir);
888 }
889 return ok;
890}
891
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100892static unsigned int ovl_split_lowerdirs(char *str)
893{
894 unsigned int ctr = 1;
895 char *s, *d;
896
897 for (s = d = str;; s++, d++) {
898 if (*s == '\\') {
899 s++;
900 } else if (*s == ':') {
901 *d = '\0';
902 ctr++;
903 continue;
904 }
905 *d = *s;
906 if (!*s)
907 break;
908 }
909 return ctr;
910}
911
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200912static int ovl_fill_super(struct super_block *sb, void *data, int silent)
913{
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100914 struct path upperpath = { NULL, NULL };
915 struct path workpath = { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200916 struct dentry *root_dentry;
917 struct ovl_entry *oe;
918 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100919 struct path *stack = NULL;
920 char *lowertmp;
921 char *lower;
922 unsigned int numlower;
923 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100924 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200925 bool remote = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200926 int err;
927
Erez Zadokf45827e82014-10-24 00:14:38 +0200928 err = -ENOMEM;
929 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
930 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200931 goto out;
932
Erez Zadokf45827e82014-10-24 00:14:38 +0200933 err = ovl_parse_opt((char *) data, &ufs->config);
934 if (err)
935 goto out_free_config;
936
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200937 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100938 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300939 if (!silent)
940 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200941 goto out_free_config;
942 }
943
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100944 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100945 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100946 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100947 if (!ufs->config.workdir) {
948 pr_err("overlayfs: missing 'workdir'\n");
949 goto out_free_config;
950 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200951
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100952 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
953 if (err)
954 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100955
hujianyang71cbad72015-01-15 13:20:57 +0800956 /* Upper fs should not be r/o */
957 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
958 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
959 err = -EINVAL;
960 goto out_put_upperpath;
961 }
962
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100963 err = ovl_mount_dir(ufs->config.workdir, &workpath);
964 if (err)
965 goto out_put_upperpath;
966
hujianyang2f83fd82015-01-06 12:52:13 +0800967 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100968 if (upperpath.mnt != workpath.mnt) {
969 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
970 goto out_put_workpath;
971 }
972 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
973 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
974 goto out_put_workpath;
975 }
976 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100977 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100978 err = -ENOMEM;
979 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
980 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100981 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200982
983 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100984 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800985 if (stacklen > OVL_MAX_STACK) {
986 pr_err("overlayfs: too many lower directries, limit is %d\n",
987 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100988 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800989 } else if (!ufs->config.upperdir && stacklen == 1) {
990 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
991 goto out_free_lowertmp;
992 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100993
994 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
995 if (!stack)
996 goto out_free_lowertmp;
997
998 lower = lowertmp;
999 for (numlower = 0; numlower < stacklen; numlower++) {
1000 err = ovl_lower_dir(lower, &stack[numlower],
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001001 &ufs->lower_namelen, &sb->s_stack_depth,
1002 &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001003 if (err)
1004 goto out_put_lowerpath;
1005
1006 lower = strchr(lower, '\0') + 1;
1007 }
1008
1009 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +01001010 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +02001011 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1012 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001013 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +02001014 }
1015
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001016 if (ufs->config.upperdir) {
1017 ufs->upper_mnt = clone_private_mount(&upperpath);
1018 err = PTR_ERR(ufs->upper_mnt);
1019 if (IS_ERR(ufs->upper_mnt)) {
1020 pr_err("overlayfs: failed to clone upperpath\n");
1021 goto out_put_lowerpath;
1022 }
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001023
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001024 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1025 err = PTR_ERR(ufs->workdir);
1026 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +02001027 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1028 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1029 sb->s_flags |= MS_RDONLY;
1030 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001031 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001032 }
1033
hujianyang2f83fd82015-01-06 12:52:13 +08001034 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001035 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001036 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001037 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001038 for (i = 0; i < numlower; i++) {
1039 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001040
hujianyang2f83fd82015-01-06 12:52:13 +08001041 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001042 if (IS_ERR(mnt)) {
1043 pr_err("overlayfs: failed to clone lowerpath\n");
1044 goto out_put_lower_mnt;
1045 }
1046 /*
1047 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1048 * will fail instead of modifying lower fs.
1049 */
1050 mnt->mnt_flags |= MNT_READONLY;
1051
1052 ufs->lower_mnt[ufs->numlower] = mnt;
1053 ufs->numlower++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001054 }
1055
hujianyang71cbad72015-01-15 13:20:57 +08001056 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1057 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001058 sb->s_flags |= MS_RDONLY;
1059
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001060 if (remote)
1061 sb->s_d_op = &ovl_reval_dentry_operations;
1062 else
1063 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001064
1065 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001066 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001067 if (!oe)
1068 goto out_put_lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001069
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001070 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001071 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001072 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001073
1074 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001075 for (i = 0; i < numlower; i++)
1076 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001077 path_put(&workpath);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001078 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001079
1080 oe->__upperdentry = upperpath.dentry;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001081 for (i = 0; i < numlower; i++) {
1082 oe->lowerstack[i].dentry = stack[i].dentry;
1083 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1084 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001085 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001086
1087 root_dentry->d_fsdata = oe;
1088
Miklos Szeredied06e062015-12-09 16:11:59 +01001089 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1090 root_dentry->d_inode);
1091
Andy Whitcroftcc259632014-10-24 00:14:38 +02001092 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001093 sb->s_op = &ovl_super_operations;
1094 sb->s_root = root_dentry;
1095 sb->s_fs_info = ufs;
1096
1097 return 0;
1098
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001099out_free_oe:
1100 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001101out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001102 for (i = 0; i < ufs->numlower; i++)
1103 mntput(ufs->lower_mnt[i]);
1104 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001105out_put_workdir:
1106 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001107 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001108out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001109 for (i = 0; i < numlower; i++)
1110 path_put(&stack[i]);
1111 kfree(stack);
1112out_free_lowertmp:
1113 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001114out_put_workpath:
1115 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001116out_put_upperpath:
1117 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001118out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001119 kfree(ufs->config.lowerdir);
1120 kfree(ufs->config.upperdir);
1121 kfree(ufs->config.workdir);
1122 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001123out:
1124 return err;
1125}
1126
1127static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1128 const char *dev_name, void *raw_data)
1129{
1130 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1131}
1132
1133static struct file_system_type ovl_fs_type = {
1134 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001135 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001136 .mount = ovl_mount,
1137 .kill_sb = kill_anon_super,
1138};
Miklos Szeredief94b182014-11-20 16:39:59 +01001139MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001140
1141static int __init ovl_init(void)
1142{
1143 return register_filesystem(&ovl_fs_type);
1144}
1145
1146static void __exit ovl_exit(void)
1147{
1148 unregister_filesystem(&ovl_fs_type);
1149}
1150
1151module_init(ovl_init);
1152module_exit(ovl_exit);