blob: 5d972e6cd3fe97fcae5c782b59c72c0007668566 [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 Szeredia78d9f0d2014-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 Szeredid101a122016-03-26 16:14:37 -0400298static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode)
299{
300 struct dentry *real;
301
302 if (d_is_dir(dentry)) {
303 if (!inode || inode == d_inode(dentry))
304 return dentry;
305 goto bug;
306 }
307
308 real = ovl_dentry_upper(dentry);
309 if (real && (!inode || inode == d_inode(real)))
310 return real;
311
312 real = ovl_dentry_lower(dentry);
313 if (!real)
314 goto bug;
315
316 if (!inode || inode == d_inode(real))
317 return real;
318
319 /* Handle recursion */
320 if (real->d_flags & DCACHE_OP_REAL)
321 return real->d_op->d_real(real, inode);
322
323bug:
324 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
325 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
326 return dentry;
327}
328
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200329static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
330{
331 struct ovl_entry *oe = dentry->d_fsdata;
332 unsigned int i;
333 int ret = 1;
334
335 for (i = 0; i < oe->numlower; i++) {
336 struct dentry *d = oe->lowerstack[i].dentry;
337
338 if (d->d_flags & DCACHE_OP_REVALIDATE) {
339 ret = d->d_op->d_revalidate(d, flags);
340 if (ret < 0)
341 return ret;
342 if (!ret) {
343 if (!(flags & LOOKUP_RCU))
344 d_invalidate(d);
345 return -ESTALE;
346 }
347 }
348 }
349 return 1;
350}
351
352static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
353{
354 struct ovl_entry *oe = dentry->d_fsdata;
355 unsigned int i;
356 int ret = 1;
357
358 for (i = 0; i < oe->numlower; i++) {
359 struct dentry *d = oe->lowerstack[i].dentry;
360
361 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
362 ret = d->d_op->d_weak_revalidate(d, flags);
363 if (ret <= 0)
364 break;
365 }
366 }
367 return ret;
368}
369
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200370static const struct dentry_operations ovl_dentry_operations = {
371 .d_release = ovl_dentry_release,
David Howells4bacc9c2015-06-18 14:32:31 +0100372 .d_select_inode = ovl_d_select_inode,
Miklos Szeredid101a122016-03-26 16:14:37 -0400373 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200374};
375
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200376static const struct dentry_operations ovl_reval_dentry_operations = {
377 .d_release = ovl_dentry_release,
Konstantin Khlebnikovb5891cf2016-01-31 16:22:16 +0300378 .d_select_inode = ovl_d_select_inode,
Miklos Szeredid101a122016-03-26 16:14:37 -0400379 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200380 .d_revalidate = ovl_dentry_revalidate,
381 .d_weak_revalidate = ovl_dentry_weak_revalidate,
382};
383
Miklos Szeredidd662662014-12-13 00:59:43 +0100384static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200385{
Miklos Szeredidd662662014-12-13 00:59:43 +0100386 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
387 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
388
389 if (oe)
390 oe->numlower = numlower;
391
392 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200393}
394
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200395static bool ovl_dentry_remote(struct dentry *dentry)
396{
397 return dentry->d_flags &
398 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
399}
400
401static bool ovl_dentry_weird(struct dentry *dentry)
402{
403 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
404 DCACHE_MANAGE_TRANSIT |
405 DCACHE_OP_HASH |
406 DCACHE_OP_COMPARE);
407}
408
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200409static inline struct dentry *ovl_lookup_real(struct dentry *dir,
410 struct qstr *name)
411{
412 struct dentry *dentry;
413
Al Viro59551022016-01-22 15:40:57 -0500414 inode_lock(dir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200415 dentry = lookup_one_len(name->name, dir, name->len);
Al Viro59551022016-01-22 15:40:57 -0500416 inode_unlock(dir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200417
418 if (IS_ERR(dentry)) {
419 if (PTR_ERR(dentry) == -ENOENT)
420 dentry = NULL;
421 } else if (!dentry->d_inode) {
422 dput(dentry);
423 dentry = NULL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200424 } else if (ovl_dentry_weird(dentry)) {
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200425 dput(dentry);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200426 /* Don't support traversing automounts and other weirdness */
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200427 dentry = ERR_PTR(-EREMOTE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200428 }
429 return dentry;
430}
431
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100432/*
433 * Returns next layer in stack starting from top.
434 * Returns -1 if this is the last layer.
435 */
436int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
437{
438 struct ovl_entry *oe = dentry->d_fsdata;
439
440 BUG_ON(idx < 0);
441 if (idx == 0) {
442 ovl_path_upper(dentry, path);
443 if (path->dentry)
444 return oe->numlower ? 1 : -1;
445 idx++;
446 }
447 BUG_ON(idx > oe->numlower);
448 *path = oe->lowerstack[idx - 1];
449
450 return (idx < oe->numlower) ? idx + 1 : -1;
451}
452
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200453struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
454 unsigned int flags)
455{
456 struct ovl_entry *oe;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100457 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
458 struct path *stack = NULL;
459 struct dentry *upperdir, *upperdentry = NULL;
460 unsigned int ctr = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200461 struct inode *inode = NULL;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100462 bool upperopaque = false;
463 struct dentry *this, *prev = NULL;
464 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200465 int err;
466
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100467 upperdir = ovl_upperdentry_dereference(poe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200468 if (upperdir) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100469 this = ovl_lookup_real(upperdir, &dentry->d_name);
470 err = PTR_ERR(this);
471 if (IS_ERR(this))
472 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200473
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100474 if (this) {
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200475 if (unlikely(ovl_dentry_remote(this))) {
476 dput(this);
477 err = -EREMOTE;
478 goto out;
479 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100480 if (ovl_is_whiteout(this)) {
481 dput(this);
482 this = NULL;
483 upperopaque = true;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100484 } else if (poe->numlower && ovl_is_opaquedir(this)) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100485 upperopaque = true;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200486 }
487 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100488 upperdentry = prev = this;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200489 }
490
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100491 if (!upperopaque && poe->numlower) {
492 err = -ENOMEM;
493 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
494 if (!stack)
495 goto out_put_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200496 }
497
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100498 for (i = 0; !upperopaque && i < poe->numlower; i++) {
499 bool opaque = false;
500 struct path lowerpath = poe->lowerstack[i];
501
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100502 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
503 err = PTR_ERR(this);
Miklos Szeredi09e10322014-12-13 00:59:45 +0100504 if (IS_ERR(this)) {
505 /*
506 * If it's positive, then treat ENAMETOOLONG as ENOENT.
507 */
508 if (err == -ENAMETOOLONG && (upperdentry || ctr))
509 continue;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100510 goto out_put;
Miklos Szeredi09e10322014-12-13 00:59:45 +0100511 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100512 if (!this)
513 continue;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100514 if (ovl_is_whiteout(this)) {
515 dput(this);
516 break;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100517 }
518 /*
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100519 * Only makes sense to check opaque dir if this is not the
520 * lowermost layer.
521 */
522 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
523 opaque = true;
hujianyanga425c032015-01-06 16:10:01 +0800524
525 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
526 !S_ISDIR(this->d_inode->i_mode))) {
527 /*
528 * FIXME: check for upper-opaqueness maybe better done
529 * in remove code.
530 */
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100531 if (prev == upperdentry)
532 upperopaque = true;
533 dput(this);
534 break;
535 }
hujianyanga425c032015-01-06 16:10:01 +0800536 /*
537 * If this is a non-directory then stop here.
538 */
539 if (!S_ISDIR(this->d_inode->i_mode))
540 opaque = true;
541
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100542 stack[ctr].dentry = this;
543 stack[ctr].mnt = lowerpath.mnt;
544 ctr++;
545 prev = this;
546 if (opaque)
547 break;
548 }
549
550 oe = ovl_alloc_entry(ctr);
551 err = -ENOMEM;
552 if (!oe)
553 goto out_put;
554
555 if (upperdentry || ctr) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200556 struct dentry *realdentry;
557
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100558 realdentry = upperdentry ? upperdentry : stack[0].dentry;
559
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200560 err = -ENOMEM;
561 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
562 oe);
563 if (!inode)
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100564 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200565 ovl_copyattr(realdentry->d_inode, inode);
566 }
567
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100568 oe->opaque = upperopaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200569 oe->__upperdentry = upperdentry;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100570 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
571 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200572 dentry->d_fsdata = oe;
573 d_add(dentry, inode);
574
575 return NULL;
576
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100577out_free_oe:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200578 kfree(oe);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100579out_put:
580 for (i = 0; i < ctr; i++)
581 dput(stack[i].dentry);
582 kfree(stack);
583out_put_upper:
584 dput(upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200585out:
586 return ERR_PTR(err);
587}
588
589struct file *ovl_path_open(struct path *path, int flags)
590{
591 return dentry_open(path, flags, current_cred());
592}
593
594static void ovl_put_super(struct super_block *sb)
595{
596 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100597 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200598
599 dput(ufs->workdir);
600 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100601 for (i = 0; i < ufs->numlower; i++)
602 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300603 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200604
Erez Zadokf45827e82014-10-24 00:14:38 +0200605 kfree(ufs->config.lowerdir);
606 kfree(ufs->config.upperdir);
607 kfree(ufs->config.workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200608 kfree(ufs);
609}
610
Andy Whitcroftcc259632014-10-24 00:14:38 +0200611/**
612 * ovl_statfs
613 * @sb: The overlayfs super block
614 * @buf: The struct kstatfs to fill in with stats
615 *
616 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100617 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200618 */
619static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
620{
621 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
622 struct dentry *root_dentry = dentry->d_sb->s_root;
623 struct path path;
624 int err;
625
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100626 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200627
628 err = vfs_statfs(&path, buf);
629 if (!err) {
630 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
631 buf->f_type = OVERLAYFS_SUPER_MAGIC;
632 }
633
634 return err;
635}
636
Erez Zadokf45827e82014-10-24 00:14:38 +0200637/**
638 * ovl_show_options
639 *
640 * Prints the mount options for a given superblock.
641 * Returns zero; does not fail.
642 */
643static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
644{
645 struct super_block *sb = dentry->d_sb;
646 struct ovl_fs *ufs = sb->s_fs_info;
647
Kees Cooka068acf2015-09-04 15:44:57 -0700648 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100649 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700650 seq_show_option(m, "upperdir", ufs->config.upperdir);
651 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100652 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200653 if (ufs->config.default_permissions)
654 seq_puts(m, ",default_permissions");
Erez Zadokf45827e82014-10-24 00:14:38 +0200655 return 0;
656}
657
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900658static int ovl_remount(struct super_block *sb, int *flags, char *data)
659{
660 struct ovl_fs *ufs = sb->s_fs_info;
661
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200662 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900663 return -EROFS;
664
665 return 0;
666}
667
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200668static const struct super_operations ovl_super_operations = {
669 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200670 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200671 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900672 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200673};
674
675enum {
676 OPT_LOWERDIR,
677 OPT_UPPERDIR,
678 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200679 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200680 OPT_ERR,
681};
682
683static const match_table_t ovl_tokens = {
684 {OPT_LOWERDIR, "lowerdir=%s"},
685 {OPT_UPPERDIR, "upperdir=%s"},
686 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200687 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688 {OPT_ERR, NULL}
689};
690
Miklos Szeredi91c77942014-11-20 16:40:00 +0100691static char *ovl_next_opt(char **s)
692{
693 char *sbegin = *s;
694 char *p;
695
696 if (sbegin == NULL)
697 return NULL;
698
699 for (p = sbegin; *p; p++) {
700 if (*p == '\\') {
701 p++;
702 if (!*p)
703 break;
704 } else if (*p == ',') {
705 *p = '\0';
706 *s = p + 1;
707 return sbegin;
708 }
709 }
710 *s = NULL;
711 return sbegin;
712}
713
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200714static int ovl_parse_opt(char *opt, struct ovl_config *config)
715{
716 char *p;
717
Miklos Szeredi91c77942014-11-20 16:40:00 +0100718 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200719 int token;
720 substring_t args[MAX_OPT_ARGS];
721
722 if (!*p)
723 continue;
724
725 token = match_token(p, ovl_tokens, args);
726 switch (token) {
727 case OPT_UPPERDIR:
728 kfree(config->upperdir);
729 config->upperdir = match_strdup(&args[0]);
730 if (!config->upperdir)
731 return -ENOMEM;
732 break;
733
734 case OPT_LOWERDIR:
735 kfree(config->lowerdir);
736 config->lowerdir = match_strdup(&args[0]);
737 if (!config->lowerdir)
738 return -ENOMEM;
739 break;
740
741 case OPT_WORKDIR:
742 kfree(config->workdir);
743 config->workdir = match_strdup(&args[0]);
744 if (!config->workdir)
745 return -ENOMEM;
746 break;
747
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200748 case OPT_DEFAULT_PERMISSIONS:
749 config->default_permissions = true;
750 break;
751
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200752 default:
hujianyangbead55e2015-01-15 13:17:36 +0800753 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200754 return -EINVAL;
755 }
756 }
hujianyang71cbad72015-01-15 13:20:57 +0800757
758 /* Workdir is useless in non-upper mount */
759 if (!config->upperdir && config->workdir) {
760 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
761 config->workdir);
762 kfree(config->workdir);
763 config->workdir = NULL;
764 }
765
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200766 return 0;
767}
768
769#define OVL_WORKDIR_NAME "work"
770
771static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
772 struct dentry *dentry)
773{
774 struct inode *dir = dentry->d_inode;
775 struct dentry *work;
776 int err;
777 bool retried = false;
778
779 err = mnt_want_write(mnt);
780 if (err)
781 return ERR_PTR(err);
782
Al Viro59551022016-01-22 15:40:57 -0500783 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200784retry:
785 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
786 strlen(OVL_WORKDIR_NAME));
787
788 if (!IS_ERR(work)) {
789 struct kstat stat = {
790 .mode = S_IFDIR | 0,
791 };
792
793 if (work->d_inode) {
794 err = -EEXIST;
795 if (retried)
796 goto out_dput;
797
798 retried = true;
799 ovl_cleanup(dir, work);
800 dput(work);
801 goto retry;
802 }
803
804 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
805 if (err)
806 goto out_dput;
807 }
808out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500809 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200810 mnt_drop_write(mnt);
811
812 return work;
813
814out_dput:
815 dput(work);
816 work = ERR_PTR(err);
817 goto out_unlock;
818}
819
Miklos Szeredi91c77942014-11-20 16:40:00 +0100820static void ovl_unescape(char *s)
821{
822 char *d = s;
823
824 for (;; s++, d++) {
825 if (*s == '\\')
826 s++;
827 *d = *s;
828 if (!*s)
829 break;
830 }
831}
832
Miklos Szerediab508822014-12-13 00:59:49 +0100833static int ovl_mount_dir_noesc(const char *name, struct path *path)
834{
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100835 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100836
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100837 if (!*name) {
838 pr_err("overlayfs: empty lowerdir\n");
839 goto out;
840 }
Miklos Szerediab508822014-12-13 00:59:49 +0100841 err = kern_path(name, LOOKUP_FOLLOW, path);
842 if (err) {
843 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
844 goto out;
845 }
846 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200847 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100848 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
849 goto out_put;
850 }
851 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
852 pr_err("overlayfs: '%s' not a directory\n", name);
853 goto out_put;
854 }
855 return 0;
856
857out_put:
858 path_put(path);
859out:
860 return err;
861}
862
863static int ovl_mount_dir(const char *name, struct path *path)
864{
865 int err = -ENOMEM;
866 char *tmp = kstrdup(name, GFP_KERNEL);
867
868 if (tmp) {
869 ovl_unescape(tmp);
870 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200871
872 if (!err)
873 if (ovl_dentry_remote(path->dentry)) {
874 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
875 tmp);
876 path_put(path);
877 err = -EINVAL;
878 }
Miklos Szerediab508822014-12-13 00:59:49 +0100879 kfree(tmp);
880 }
881 return err;
882}
883
884static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200885 int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100886{
887 int err;
888 struct kstatfs statfs;
889
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100890 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100891 if (err)
892 goto out;
893
894 err = vfs_statfs(path, &statfs);
895 if (err) {
896 pr_err("overlayfs: statfs failed on '%s'\n", name);
897 goto out_put;
898 }
899 *namelen = max(*namelen, statfs.f_namelen);
900 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
901
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200902 if (ovl_dentry_remote(path->dentry))
903 *remote = true;
904
Miklos Szerediab508822014-12-13 00:59:49 +0100905 return 0;
906
907out_put:
908 path_put(path);
909out:
910 return err;
911}
912
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200913/* Workdir should not be subdir of upperdir and vice versa */
914static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
915{
916 bool ok = false;
917
918 if (workdir != upperdir) {
919 ok = (lock_rename(workdir, upperdir) == NULL);
920 unlock_rename(workdir, upperdir);
921 }
922 return ok;
923}
924
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100925static unsigned int ovl_split_lowerdirs(char *str)
926{
927 unsigned int ctr = 1;
928 char *s, *d;
929
930 for (s = d = str;; s++, d++) {
931 if (*s == '\\') {
932 s++;
933 } else if (*s == ':') {
934 *d = '\0';
935 ctr++;
936 continue;
937 }
938 *d = *s;
939 if (!*s)
940 break;
941 }
942 return ctr;
943}
944
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200945static int ovl_fill_super(struct super_block *sb, void *data, int silent)
946{
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100947 struct path upperpath = { NULL, NULL };
948 struct path workpath = { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200949 struct dentry *root_dentry;
950 struct ovl_entry *oe;
951 struct ovl_fs *ufs;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100952 struct path *stack = NULL;
953 char *lowertmp;
954 char *lower;
955 unsigned int numlower;
956 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100957 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200958 bool remote = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200959 int err;
960
Erez Zadokf45827e82014-10-24 00:14:38 +0200961 err = -ENOMEM;
962 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
963 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200964 goto out;
965
Erez Zadokf45827e82014-10-24 00:14:38 +0200966 err = ovl_parse_opt((char *) data, &ufs->config);
967 if (err)
968 goto out_free_config;
969
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200970 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100971 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300972 if (!silent)
973 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200974 goto out_free_config;
975 }
976
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100977 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100978 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100979 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100980 if (!ufs->config.workdir) {
981 pr_err("overlayfs: missing 'workdir'\n");
982 goto out_free_config;
983 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200984
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100985 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
986 if (err)
987 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100988
hujianyang71cbad72015-01-15 13:20:57 +0800989 /* Upper fs should not be r/o */
990 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
991 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
992 err = -EINVAL;
993 goto out_put_upperpath;
994 }
995
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100996 err = ovl_mount_dir(ufs->config.workdir, &workpath);
997 if (err)
998 goto out_put_upperpath;
999
hujianyang2f83fd82015-01-06 12:52:13 +08001000 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001001 if (upperpath.mnt != workpath.mnt) {
1002 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1003 goto out_put_workpath;
1004 }
1005 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1006 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1007 goto out_put_workpath;
1008 }
1009 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +01001010 }
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001011 err = -ENOMEM;
1012 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1013 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001014 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001015
1016 err = -EINVAL;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001017 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +08001018 if (stacklen > OVL_MAX_STACK) {
1019 pr_err("overlayfs: too many lower directries, limit is %d\n",
1020 OVL_MAX_STACK);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001021 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +08001022 } else if (!ufs->config.upperdir && stacklen == 1) {
1023 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1024 goto out_free_lowertmp;
1025 }
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001026
1027 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1028 if (!stack)
1029 goto out_free_lowertmp;
1030
1031 lower = lowertmp;
1032 for (numlower = 0; numlower < stacklen; numlower++) {
1033 err = ovl_lower_dir(lower, &stack[numlower],
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001034 &ufs->lower_namelen, &sb->s_stack_depth,
1035 &remote);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001036 if (err)
1037 goto out_put_lowerpath;
1038
1039 lower = strchr(lower, '\0') + 1;
1040 }
1041
1042 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +01001043 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +02001044 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1045 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001046 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +02001047 }
1048
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001049 if (ufs->config.upperdir) {
1050 ufs->upper_mnt = clone_private_mount(&upperpath);
1051 err = PTR_ERR(ufs->upper_mnt);
1052 if (IS_ERR(ufs->upper_mnt)) {
1053 pr_err("overlayfs: failed to clone upperpath\n");
1054 goto out_put_lowerpath;
1055 }
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001056
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001057 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1058 err = PTR_ERR(ufs->workdir);
1059 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +02001060 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1061 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1062 sb->s_flags |= MS_RDONLY;
1063 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001064 }
Vivek Goyal45aebea2016-02-22 09:28:34 -05001065
1066 /*
1067 * Upper should support d_type, else whiteouts are visible.
1068 * Given workdir and upper are on same fs, we can do
1069 * iterate_dir() on workdir.
1070 */
1071 err = ovl_check_d_type_supported(&workpath);
1072 if (err < 0)
1073 goto out_put_workdir;
1074
1075 if (!err) {
1076 pr_err("overlayfs: upper fs needs to support d_type.\n");
1077 err = -EINVAL;
1078 goto out_put_workdir;
1079 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001080 }
1081
hujianyang2f83fd82015-01-06 12:52:13 +08001082 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001083 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001084 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001085 goto out_put_workdir;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001086 for (i = 0; i < numlower; i++) {
1087 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001088
hujianyang2f83fd82015-01-06 12:52:13 +08001089 err = PTR_ERR(mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001090 if (IS_ERR(mnt)) {
1091 pr_err("overlayfs: failed to clone lowerpath\n");
1092 goto out_put_lower_mnt;
1093 }
1094 /*
1095 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1096 * will fail instead of modifying lower fs.
1097 */
1098 mnt->mnt_flags |= MNT_READONLY;
1099
1100 ufs->lower_mnt[ufs->numlower] = mnt;
1101 ufs->numlower++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001102 }
1103
hujianyang71cbad72015-01-15 13:20:57 +08001104 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1105 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001106 sb->s_flags |= MS_RDONLY;
1107
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001108 if (remote)
1109 sb->s_d_op = &ovl_reval_dentry_operations;
1110 else
1111 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001112
1113 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001114 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001115 if (!oe)
1116 goto out_put_lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001117
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001118 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001119 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001120 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001121
1122 mntput(upperpath.mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001123 for (i = 0; i < numlower; i++)
1124 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001125 path_put(&workpath);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001126 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001127
1128 oe->__upperdentry = upperpath.dentry;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001129 for (i = 0; i < numlower; i++) {
1130 oe->lowerstack[i].dentry = stack[i].dentry;
1131 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1132 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001133 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001134
1135 root_dentry->d_fsdata = oe;
1136
Miklos Szeredied06e062015-12-09 16:11:59 +01001137 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1138 root_dentry->d_inode);
1139
Andy Whitcroftcc259632014-10-24 00:14:38 +02001140 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001141 sb->s_op = &ovl_super_operations;
1142 sb->s_root = root_dentry;
1143 sb->s_fs_info = ufs;
1144
1145 return 0;
1146
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001147out_free_oe:
1148 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001149out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001150 for (i = 0; i < ufs->numlower; i++)
1151 mntput(ufs->lower_mnt[i]);
1152 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001153out_put_workdir:
1154 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001155 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001156out_put_lowerpath:
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001157 for (i = 0; i < numlower; i++)
1158 path_put(&stack[i]);
1159 kfree(stack);
1160out_free_lowertmp:
1161 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001162out_put_workpath:
1163 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001164out_put_upperpath:
1165 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001166out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001167 kfree(ufs->config.lowerdir);
1168 kfree(ufs->config.upperdir);
1169 kfree(ufs->config.workdir);
1170 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001171out:
1172 return err;
1173}
1174
1175static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1176 const char *dev_name, void *raw_data)
1177{
1178 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1179}
1180
1181static struct file_system_type ovl_fs_type = {
1182 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001183 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001184 .mount = ovl_mount,
1185 .kill_sb = kill_anon_super,
1186};
Miklos Szeredief94b182014-11-20 16:39:59 +01001187MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001188
1189static int __init ovl_init(void)
1190{
1191 return register_filesystem(&ovl_fs_type);
1192}
1193
1194static void __exit ovl_exit(void)
1195{
1196 unregister_filesystem(&ovl_fs_type);
1197}
1198
1199module_init(ovl_init);
1200module_exit(ovl_exit);