blob: 8a08c582bc22e400a16f395609200ed105d3933f [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>
12#include <linux/xattr.h>
13#include <linux/security.h>
14#include <linux/mount.h>
15#include <linux/slab.h>
16#include <linux/parser.h>
17#include <linux/module.h>
18#include <linux/sched.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020019#include <linux/statfs.h>
Erez Zadokf45827e2014-10-24 00:14:38 +020020#include <linux/seq_file.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020021#include "overlayfs.h"
22
23MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24MODULE_DESCRIPTION("Overlay filesystem");
25MODULE_LICENSE("GPL");
26
Miklos Szeredief94b182014-11-20 16:39:59 +010027#define OVERLAYFS_SUPER_MAGIC 0x794c7630
Andy Whitcroftcc259632014-10-24 00:14:38 +020028
Erez Zadokf45827e2014-10-24 00:14:38 +020029struct ovl_config {
30 char *lowerdir;
31 char *upperdir;
32 char *workdir;
33};
34
Miklos Szeredie9be9d52014-10-24 00:14:38 +020035/* private information held for overlayfs's superblock */
36struct ovl_fs {
37 struct vfsmount *upper_mnt;
Miklos Szeredidd662662014-12-13 00:59:43 +010038 unsigned numlower;
39 struct vfsmount **lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020040 struct dentry *workdir;
Andy Whitcroftcc259632014-10-24 00:14:38 +020041 long lower_namelen;
Erez Zadokf45827e2014-10-24 00:14:38 +020042 /* pathnames of lower and upper dirs, for show_options */
43 struct ovl_config config;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020044};
45
46struct ovl_dir_cache;
47
48/* private information held for every overlayfs dentry */
49struct ovl_entry {
50 struct dentry *__upperdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020051 struct ovl_dir_cache *cache;
52 union {
53 struct {
54 u64 version;
55 bool opaque;
56 };
57 struct rcu_head rcu;
58 };
Miklos Szeredidd662662014-12-13 00:59:43 +010059 unsigned numlower;
60 struct path lowerstack[];
Miklos Szeredie9be9d52014-10-24 00:14:38 +020061};
62
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +010063#define OVL_MAX_STACK 500
64
Miklos Szeredidd662662014-12-13 00:59:43 +010065static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
66{
67 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
68}
Miklos Szeredie9be9d52014-10-24 00:14:38 +020069
70enum ovl_path_type ovl_path_type(struct dentry *dentry)
71{
72 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi1afaba12014-12-13 00:59:42 +010073 enum ovl_path_type type = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020074
75 if (oe->__upperdentry) {
Miklos Szeredi1afaba12014-12-13 00:59:42 +010076 type = __OVL_PATH_UPPER;
77
Miklos Szeredidd662662014-12-13 00:59:43 +010078 if (oe->numlower) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020079 if (S_ISDIR(dentry->d_inode->i_mode))
Miklos Szeredi1afaba12014-12-13 00:59:42 +010080 type |= __OVL_PATH_MERGE;
81 } else if (!oe->opaque) {
82 type |= __OVL_PATH_PURE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020083 }
Miklos Szeredi9d7459d2014-12-13 00:59:44 +010084 } else {
85 if (oe->numlower > 1)
86 type |= __OVL_PATH_MERGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020087 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +010088 return type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020089}
90
91static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
92{
Miklos Szeredi71d50922014-11-20 16:40:01 +010093 return lockless_dereference(oe->__upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020094}
95
96void ovl_path_upper(struct dentry *dentry, struct path *path)
97{
98 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
99 struct ovl_entry *oe = dentry->d_fsdata;
100
101 path->mnt = ofs->upper_mnt;
102 path->dentry = ovl_upperdentry_dereference(oe);
103}
104
105enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
106{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200107 enum ovl_path_type type = ovl_path_type(dentry);
108
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100109 if (!OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200110 ovl_path_lower(dentry, path);
111 else
112 ovl_path_upper(dentry, path);
113
114 return type;
115}
116
117struct dentry *ovl_dentry_upper(struct dentry *dentry)
118{
119 struct ovl_entry *oe = dentry->d_fsdata;
120
121 return ovl_upperdentry_dereference(oe);
122}
123
124struct dentry *ovl_dentry_lower(struct dentry *dentry)
125{
126 struct ovl_entry *oe = dentry->d_fsdata;
127
Miklos Szeredidd662662014-12-13 00:59:43 +0100128 return __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200129}
130
131struct dentry *ovl_dentry_real(struct dentry *dentry)
132{
133 struct ovl_entry *oe = dentry->d_fsdata;
134 struct dentry *realdentry;
135
136 realdentry = ovl_upperdentry_dereference(oe);
137 if (!realdentry)
Miklos Szeredidd662662014-12-13 00:59:43 +0100138 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200139
140 return realdentry;
141}
142
143struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
144{
145 struct dentry *realdentry;
146
147 realdentry = ovl_upperdentry_dereference(oe);
148 if (realdentry) {
149 *is_upper = true;
150 } else {
Miklos Szeredidd662662014-12-13 00:59:43 +0100151 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200152 *is_upper = false;
153 }
154 return realdentry;
155}
156
157struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
158{
159 struct ovl_entry *oe = dentry->d_fsdata;
160
161 return oe->cache;
162}
163
164void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
165{
166 struct ovl_entry *oe = dentry->d_fsdata;
167
168 oe->cache = cache;
169}
170
171void ovl_path_lower(struct dentry *dentry, struct path *path)
172{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200173 struct ovl_entry *oe = dentry->d_fsdata;
174
Miklos Szeredidd662662014-12-13 00:59:43 +0100175 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200176}
177
178int ovl_want_write(struct dentry *dentry)
179{
180 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
181 return mnt_want_write(ofs->upper_mnt);
182}
183
184void ovl_drop_write(struct dentry *dentry)
185{
186 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
187 mnt_drop_write(ofs->upper_mnt);
188}
189
190struct dentry *ovl_workdir(struct dentry *dentry)
191{
192 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
193 return ofs->workdir;
194}
195
196bool ovl_dentry_is_opaque(struct dentry *dentry)
197{
198 struct ovl_entry *oe = dentry->d_fsdata;
199 return oe->opaque;
200}
201
202void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
203{
204 struct ovl_entry *oe = dentry->d_fsdata;
205 oe->opaque = opaque;
206}
207
208void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
209{
210 struct ovl_entry *oe = dentry->d_fsdata;
211
212 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
213 WARN_ON(oe->__upperdentry);
214 BUG_ON(!upperdentry->d_inode);
215 /*
216 * Make sure upperdentry is consistent before making it visible to
217 * ovl_upperdentry_dereference().
218 */
219 smp_wmb();
220 oe->__upperdentry = upperdentry;
221}
222
223void ovl_dentry_version_inc(struct dentry *dentry)
224{
225 struct ovl_entry *oe = dentry->d_fsdata;
226
227 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
228 oe->version++;
229}
230
231u64 ovl_dentry_version_get(struct dentry *dentry)
232{
233 struct ovl_entry *oe = dentry->d_fsdata;
234
235 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
236 return oe->version;
237}
238
239bool ovl_is_whiteout(struct dentry *dentry)
240{
241 struct inode *inode = dentry->d_inode;
242
243 return inode && IS_WHITEOUT(inode);
244}
245
246static bool ovl_is_opaquedir(struct dentry *dentry)
247{
248 int res;
249 char val;
250 struct inode *inode = dentry->d_inode;
251
252 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
253 return false;
254
hujianyangcead89b2014-11-24 18:25:21 +0800255 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256 if (res == 1 && val == 'y')
257 return true;
258
259 return false;
260}
261
262static void ovl_dentry_release(struct dentry *dentry)
263{
264 struct ovl_entry *oe = dentry->d_fsdata;
265
266 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +0100267 unsigned int i;
268
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200269 dput(oe->__upperdentry);
Miklos Szeredidd662662014-12-13 00:59:43 +0100270 for (i = 0; i < oe->numlower; i++)
271 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200272 kfree_rcu(oe, rcu);
273 }
274}
275
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200276static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
277{
278 struct ovl_entry *oe = dentry->d_fsdata;
279 unsigned int i;
280 int ret = 1;
281
282 for (i = 0; i < oe->numlower; i++) {
283 struct dentry *d = oe->lowerstack[i].dentry;
284
285 if (d->d_flags & DCACHE_OP_REVALIDATE) {
286 ret = d->d_op->d_revalidate(d, flags);
287 if (ret < 0)
288 return ret;
289 if (!ret) {
290 if (!(flags & LOOKUP_RCU))
291 d_invalidate(d);
292 return -ESTALE;
293 }
294 }
295 }
296 return 1;
297}
298
299static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
300{
301 struct ovl_entry *oe = dentry->d_fsdata;
302 unsigned int i;
303 int ret = 1;
304
305 for (i = 0; i < oe->numlower; i++) {
306 struct dentry *d = oe->lowerstack[i].dentry;
307
308 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
309 ret = d->d_op->d_weak_revalidate(d, flags);
310 if (ret <= 0)
311 break;
312 }
313 }
314 return ret;
315}
316
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200317static const struct dentry_operations ovl_dentry_operations = {
318 .d_release = ovl_dentry_release,
319};
320
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200321static const struct dentry_operations ovl_reval_dentry_operations = {
322 .d_release = ovl_dentry_release,
323 .d_revalidate = ovl_dentry_revalidate,
324 .d_weak_revalidate = ovl_dentry_weak_revalidate,
325};
326
Miklos Szeredidd662662014-12-13 00:59:43 +0100327static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328{
Miklos Szeredidd662662014-12-13 00:59:43 +0100329 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
330 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
331
332 if (oe)
333 oe->numlower = numlower;
334
335 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200336}
337
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200338static bool ovl_dentry_remote(struct dentry *dentry)
339{
340 return dentry->d_flags &
341 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
342}
343
344static bool ovl_dentry_weird(struct dentry *dentry)
345{
346 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
347 DCACHE_MANAGE_TRANSIT |
348 DCACHE_OP_HASH |
349 DCACHE_OP_COMPARE);
350}
351
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200352static inline struct dentry *ovl_lookup_real(struct dentry *dir,
353 struct qstr *name)
354{
355 struct dentry *dentry;
356
357 mutex_lock(&dir->d_inode->i_mutex);
358 dentry = lookup_one_len(name->name, dir, name->len);
359 mutex_unlock(&dir->d_inode->i_mutex);
360
361 if (IS_ERR(dentry)) {
362 if (PTR_ERR(dentry) == -ENOENT)
363 dentry = NULL;
364 } else if (!dentry->d_inode) {
365 dput(dentry);
366 dentry = NULL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200367 } else if (ovl_dentry_weird(dentry)) {
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200368 dput(dentry);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200369 /* Don't support traversing automounts and other weirdness */
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200370 dentry = ERR_PTR(-EREMOTE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371 }
372 return dentry;
373}
374
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100375/*
376 * Returns next layer in stack starting from top.
377 * Returns -1 if this is the last layer.
378 */
379int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
380{
381 struct ovl_entry *oe = dentry->d_fsdata;
382
383 BUG_ON(idx < 0);
384 if (idx == 0) {
385 ovl_path_upper(dentry, path);
386 if (path->dentry)
387 return oe->numlower ? 1 : -1;
388 idx++;
389 }
390 BUG_ON(idx > oe->numlower);
391 *path = oe->lowerstack[idx - 1];
392
393 return (idx < oe->numlower) ? idx + 1 : -1;
394}
395
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200396struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
397 unsigned int flags)
398{
399 struct ovl_entry *oe;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100400 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
401 struct path *stack = NULL;
402 struct dentry *upperdir, *upperdentry = NULL;
403 unsigned int ctr = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200404 struct inode *inode = NULL;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100405 bool upperopaque = false;
406 struct dentry *this, *prev = NULL;
407 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408 int err;
409
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100410 upperdir = ovl_upperdentry_dereference(poe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200411 if (upperdir) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100412 this = ovl_lookup_real(upperdir, &dentry->d_name);
413 err = PTR_ERR(this);
414 if (IS_ERR(this))
415 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200416
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100417 if (this) {
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200418 if (unlikely(ovl_dentry_remote(this))) {
419 dput(this);
420 err = -EREMOTE;
421 goto out;
422 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100423 if (ovl_is_whiteout(this)) {
424 dput(this);
425 this = NULL;
426 upperopaque = true;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100427 } else if (poe->numlower && ovl_is_opaquedir(this)) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100428 upperopaque = true;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200429 }
430 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100431 upperdentry = prev = this;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200432 }
433
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100434 if (!upperopaque && poe->numlower) {
435 err = -ENOMEM;
436 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
437 if (!stack)
438 goto out_put_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200439 }
440
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100441 for (i = 0; !upperopaque && i < poe->numlower; i++) {
442 bool opaque = false;
443 struct path lowerpath = poe->lowerstack[i];
444
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100445 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
446 err = PTR_ERR(this);
Miklos Szeredi09e10322014-12-13 00:59:45 +0100447 if (IS_ERR(this)) {
448 /*
449 * If it's positive, then treat ENAMETOOLONG as ENOENT.
450 */
451 if (err == -ENAMETOOLONG && (upperdentry || ctr))
452 continue;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100453 goto out_put;
Miklos Szeredi09e10322014-12-13 00:59:45 +0100454 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100455 if (!this)
456 continue;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100457 if (ovl_is_whiteout(this)) {
458 dput(this);
459 break;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100460 }
461 /*
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100462 * Only makes sense to check opaque dir if this is not the
463 * lowermost layer.
464 */
465 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
466 opaque = true;
hujianyanga425c032015-01-06 16:10:01 +0800467
468 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
469 !S_ISDIR(this->d_inode->i_mode))) {
470 /*
471 * FIXME: check for upper-opaqueness maybe better done
472 * in remove code.
473 */
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100474 if (prev == upperdentry)
475 upperopaque = true;
476 dput(this);
477 break;
478 }
hujianyanga425c032015-01-06 16:10:01 +0800479 /*
480 * If this is a non-directory then stop here.
481 */
482 if (!S_ISDIR(this->d_inode->i_mode))
483 opaque = true;
484
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100485 stack[ctr].dentry = this;
486 stack[ctr].mnt = lowerpath.mnt;
487 ctr++;
488 prev = this;
489 if (opaque)
490 break;
491 }
492
493 oe = ovl_alloc_entry(ctr);
494 err = -ENOMEM;
495 if (!oe)
496 goto out_put;
497
498 if (upperdentry || ctr) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200499 struct dentry *realdentry;
500
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100501 realdentry = upperdentry ? upperdentry : stack[0].dentry;
502
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200503 err = -ENOMEM;
504 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
505 oe);
506 if (!inode)
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100507 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200508 ovl_copyattr(realdentry->d_inode, inode);
509 }
510
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100511 oe->opaque = upperopaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200512 oe->__upperdentry = upperdentry;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100513 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
514 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200515 dentry->d_fsdata = oe;
516 d_add(dentry, inode);
517
518 return NULL;
519
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100520out_free_oe:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200521 kfree(oe);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100522out_put:
523 for (i = 0; i < ctr; i++)
524 dput(stack[i].dentry);
525 kfree(stack);
526out_put_upper:
527 dput(upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200528out:
529 return ERR_PTR(err);
530}
531
532struct file *ovl_path_open(struct path *path, int flags)
533{
534 return dentry_open(path, flags, current_cred());
535}
536
537static void ovl_put_super(struct super_block *sb)
538{
539 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100540 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200541
542 dput(ufs->workdir);
543 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100544 for (i = 0; i < ufs->numlower; i++)
545 mntput(ufs->lower_mnt[i]);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200546
Erez Zadokf45827e2014-10-24 00:14:38 +0200547 kfree(ufs->config.lowerdir);
548 kfree(ufs->config.upperdir);
549 kfree(ufs->config.workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200550 kfree(ufs);
551}
552
Andy Whitcroftcc259632014-10-24 00:14:38 +0200553/**
554 * ovl_statfs
555 * @sb: The overlayfs super block
556 * @buf: The struct kstatfs to fill in with stats
557 *
558 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100559 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200560 */
561static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
562{
563 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
564 struct dentry *root_dentry = dentry->d_sb->s_root;
565 struct path path;
566 int err;
567
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100568 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200569
570 err = vfs_statfs(&path, buf);
571 if (!err) {
572 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
573 buf->f_type = OVERLAYFS_SUPER_MAGIC;
574 }
575
576 return err;
577}
578
Erez Zadokf45827e2014-10-24 00:14:38 +0200579/**
580 * ovl_show_options
581 *
582 * Prints the mount options for a given superblock.
583 * Returns zero; does not fail.
584 */
585static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
586{
587 struct super_block *sb = dentry->d_sb;
588 struct ovl_fs *ufs = sb->s_fs_info;
589
590 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100591 if (ufs->config.upperdir) {
592 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
593 seq_printf(m, ",workdir=%s", ufs->config.workdir);
594 }
Erez Zadokf45827e2014-10-24 00:14:38 +0200595 return 0;
596}
597
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900598static int ovl_remount(struct super_block *sb, int *flags, char *data)
599{
600 struct ovl_fs *ufs = sb->s_fs_info;
601
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200602 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900603 return -EROFS;
604
605 return 0;
606}
607
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200608static const struct super_operations ovl_super_operations = {
609 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200610 .statfs = ovl_statfs,
Erez Zadokf45827e2014-10-24 00:14:38 +0200611 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900612 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200613};
614
615enum {
616 OPT_LOWERDIR,
617 OPT_UPPERDIR,
618 OPT_WORKDIR,
619 OPT_ERR,
620};
621
622static const match_table_t ovl_tokens = {
623 {OPT_LOWERDIR, "lowerdir=%s"},
624 {OPT_UPPERDIR, "upperdir=%s"},
625 {OPT_WORKDIR, "workdir=%s"},
626 {OPT_ERR, NULL}
627};
628
Miklos Szeredi91c77942014-11-20 16:40:00 +0100629static char *ovl_next_opt(char **s)
630{
631 char *sbegin = *s;
632 char *p;
633
634 if (sbegin == NULL)
635 return NULL;
636
637 for (p = sbegin; *p; p++) {
638 if (*p == '\\') {
639 p++;
640 if (!*p)
641 break;
642 } else if (*p == ',') {
643 *p = '\0';
644 *s = p + 1;
645 return sbegin;
646 }
647 }
648 *s = NULL;
649 return sbegin;
650}
651
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200652static int ovl_parse_opt(char *opt, struct ovl_config *config)
653{
654 char *p;
655
Miklos Szeredi91c77942014-11-20 16:40:00 +0100656 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200657 int token;
658 substring_t args[MAX_OPT_ARGS];
659
660 if (!*p)
661 continue;
662
663 token = match_token(p, ovl_tokens, args);
664 switch (token) {
665 case OPT_UPPERDIR:
666 kfree(config->upperdir);
667 config->upperdir = match_strdup(&args[0]);
668 if (!config->upperdir)
669 return -ENOMEM;
670 break;
671
672 case OPT_LOWERDIR:
673 kfree(config->lowerdir);
674 config->lowerdir = match_strdup(&args[0]);
675 if (!config->lowerdir)
676 return -ENOMEM;
677 break;
678
679 case OPT_WORKDIR:
680 kfree(config->workdir);
681 config->workdir = match_strdup(&args[0]);
682 if (!config->workdir)
683 return -ENOMEM;
684 break;
685
686 default:
hujianyangbead55e2015-01-15 13:17:36 +0800687 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688 return -EINVAL;
689 }
690 }
hujianyang71cbad72015-01-15 13:20:57 +0800691
692 /* Workdir is useless in non-upper mount */
693 if (!config->upperdir && config->workdir) {
694 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
695 config->workdir);
696 kfree(config->workdir);
697 config->workdir = NULL;
698 }
699
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200700 return 0;
701}
702
703#define OVL_WORKDIR_NAME "work"
704
705static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
706 struct dentry *dentry)
707{
708 struct inode *dir = dentry->d_inode;
709 struct dentry *work;
710 int err;
711 bool retried = false;
712
713 err = mnt_want_write(mnt);
714 if (err)
715 return ERR_PTR(err);
716
717 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
718retry:
719 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
720 strlen(OVL_WORKDIR_NAME));
721
722 if (!IS_ERR(work)) {
723 struct kstat stat = {
724 .mode = S_IFDIR | 0,
725 };
726
727 if (work->d_inode) {
728 err = -EEXIST;
729 if (retried)
730 goto out_dput;
731
732 retried = true;
733 ovl_cleanup(dir, work);
734 dput(work);
735 goto retry;
736 }
737
738 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
739 if (err)
740 goto out_dput;
741 }
742out_unlock:
743 mutex_unlock(&dir->i_mutex);
744 mnt_drop_write(mnt);
745
746 return work;
747
748out_dput:
749 dput(work);
750 work = ERR_PTR(err);
751 goto out_unlock;
752}
753
Miklos Szeredi91c77942014-11-20 16:40:00 +0100754static void ovl_unescape(char *s)
755{
756 char *d = s;
757
758 for (;; s++, d++) {
759 if (*s == '\\')
760 s++;
761 *d = *s;
762 if (!*s)
763 break;
764 }
765}
766
Miklos Szerediab508822014-12-13 00:59:49 +0100767static int ovl_mount_dir_noesc(const char *name, struct path *path)
768{
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100769 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100770
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100771 if (!*name) {
772 pr_err("overlayfs: empty lowerdir\n");
773 goto out;
774 }
Miklos Szerediab508822014-12-13 00:59:49 +0100775 err = kern_path(name, LOOKUP_FOLLOW, path);
776 if (err) {
777 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
778 goto out;
779 }
780 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200781 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100782 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
783 goto out_put;
784 }
785 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
786 pr_err("overlayfs: '%s' not a directory\n", name);
787 goto out_put;
788 }
789 return 0;
790
791out_put:
792 path_put(path);
793out:
794 return err;
795}
796
797static int ovl_mount_dir(const char *name, struct path *path)
798{
799 int err = -ENOMEM;
800 char *tmp = kstrdup(name, GFP_KERNEL);
801
802 if (tmp) {
803 ovl_unescape(tmp);
804 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200805
806 if (!err)
807 if (ovl_dentry_remote(path->dentry)) {
808 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
809 tmp);
810 path_put(path);
811 err = -EINVAL;
812 }
Miklos Szerediab508822014-12-13 00:59:49 +0100813 kfree(tmp);
814 }
815 return err;
816}
817
818static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200819 int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100820{
821 int err;
822 struct kstatfs statfs;
823
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100824 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100825 if (err)
826 goto out;
827
828 err = vfs_statfs(path, &statfs);
829 if (err) {
830 pr_err("overlayfs: statfs failed on '%s'\n", name);
831 goto out_put;
832 }
833 *namelen = max(*namelen, statfs.f_namelen);
834 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
835
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200836 if (ovl_dentry_remote(path->dentry))
837 *remote = true;
838
Miklos Szerediab508822014-12-13 00:59:49 +0100839 return 0;
840
841out_put:
842 path_put(path);
843out:
844 return err;
845}
846
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200847/* Workdir should not be subdir of upperdir and vice versa */
848static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
849{
850 bool ok = false;
851
852 if (workdir != upperdir) {
853 ok = (lock_rename(workdir, upperdir) == NULL);
854 unlock_rename(workdir, upperdir);
855 }
856 return ok;
857}
858
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100859static unsigned int ovl_split_lowerdirs(char *str)
860{
861 unsigned int ctr = 1;
862 char *s, *d;
863
864 for (s = d = str;; s++, d++) {
865 if (*s == '\\') {
866 s++;
867 } else if (*s == ':') {
868 *d = '\0';
869 ctr++;
870 continue;
871 }
872 *d = *s;
873 if (!*s)
874 break;
875 }
876 return ctr;
877}
878
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200879static int ovl_fill_super(struct super_block *sb, void *data, int silent)
880{
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100881 struct path upperpath = { NULL, NULL };
882 struct path workpath = { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200883 struct dentry *root_dentry;
884 struct ovl_entry *oe;
885 struct ovl_fs *ufs;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100886 struct path *stack = NULL;
887 char *lowertmp;
888 char *lower;
889 unsigned int numlower;
890 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100891 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200892 bool remote = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200893 int err;
894
Erez Zadokf45827e2014-10-24 00:14:38 +0200895 err = -ENOMEM;
896 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
897 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200898 goto out;
899
Erez Zadokf45827e2014-10-24 00:14:38 +0200900 err = ovl_parse_opt((char *) data, &ufs->config);
901 if (err)
902 goto out_free_config;
903
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200904 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100905 if (!ufs->config.lowerdir) {
906 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200907 goto out_free_config;
908 }
909
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100910 sb->s_stack_depth = 0;
911 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100912 if (!ufs->config.workdir) {
913 pr_err("overlayfs: missing 'workdir'\n");
914 goto out_free_config;
915 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200916
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100917 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
918 if (err)
919 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100920
hujianyang71cbad72015-01-15 13:20:57 +0800921 /* Upper fs should not be r/o */
922 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
923 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
924 err = -EINVAL;
925 goto out_put_upperpath;
926 }
927
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100928 err = ovl_mount_dir(ufs->config.workdir, &workpath);
929 if (err)
930 goto out_put_upperpath;
931
hujianyang2f83fd82015-01-06 12:52:13 +0800932 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100933 if (upperpath.mnt != workpath.mnt) {
934 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
935 goto out_put_workpath;
936 }
937 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
938 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
939 goto out_put_workpath;
940 }
941 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100942 }
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100943 err = -ENOMEM;
944 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
945 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100946 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200947
948 err = -EINVAL;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100949 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800950 if (stacklen > OVL_MAX_STACK) {
951 pr_err("overlayfs: too many lower directries, limit is %d\n",
952 OVL_MAX_STACK);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100953 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800954 } else if (!ufs->config.upperdir && stacklen == 1) {
955 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
956 goto out_free_lowertmp;
957 }
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100958
959 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
960 if (!stack)
961 goto out_free_lowertmp;
962
963 lower = lowertmp;
964 for (numlower = 0; numlower < stacklen; numlower++) {
965 err = ovl_lower_dir(lower, &stack[numlower],
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200966 &ufs->lower_namelen, &sb->s_stack_depth,
967 &remote);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100968 if (err)
969 goto out_put_lowerpath;
970
971 lower = strchr(lower, '\0') + 1;
972 }
973
974 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100975 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200976 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
977 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100978 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200979 }
980
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100981 if (ufs->config.upperdir) {
982 ufs->upper_mnt = clone_private_mount(&upperpath);
983 err = PTR_ERR(ufs->upper_mnt);
984 if (IS_ERR(ufs->upper_mnt)) {
985 pr_err("overlayfs: failed to clone upperpath\n");
986 goto out_put_lowerpath;
987 }
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100988
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100989 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
990 err = PTR_ERR(ufs->workdir);
991 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200992 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
993 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
994 sb->s_flags |= MS_RDONLY;
995 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100996 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200997 }
998
hujianyang2f83fd82015-01-06 12:52:13 +0800999 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001000 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001001 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001002 goto out_put_workdir;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001003 for (i = 0; i < numlower; i++) {
1004 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001005
hujianyang2f83fd82015-01-06 12:52:13 +08001006 err = PTR_ERR(mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001007 if (IS_ERR(mnt)) {
1008 pr_err("overlayfs: failed to clone lowerpath\n");
1009 goto out_put_lower_mnt;
1010 }
1011 /*
1012 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1013 * will fail instead of modifying lower fs.
1014 */
1015 mnt->mnt_flags |= MNT_READONLY;
1016
1017 ufs->lower_mnt[ufs->numlower] = mnt;
1018 ufs->numlower++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001019 }
1020
hujianyang71cbad72015-01-15 13:20:57 +08001021 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1022 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001023 sb->s_flags |= MS_RDONLY;
1024
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001025 if (remote)
1026 sb->s_d_op = &ovl_reval_dentry_operations;
1027 else
1028 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001029
1030 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001031 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001032 if (!oe)
1033 goto out_put_lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001034
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001035 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001036 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001037 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001038
1039 mntput(upperpath.mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001040 for (i = 0; i < numlower; i++)
1041 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001042 path_put(&workpath);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001043 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001044
1045 oe->__upperdentry = upperpath.dentry;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001046 for (i = 0; i < numlower; i++) {
1047 oe->lowerstack[i].dentry = stack[i].dentry;
1048 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1049 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001050
1051 root_dentry->d_fsdata = oe;
1052
Andy Whitcroftcc259632014-10-24 00:14:38 +02001053 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001054 sb->s_op = &ovl_super_operations;
1055 sb->s_root = root_dentry;
1056 sb->s_fs_info = ufs;
1057
1058 return 0;
1059
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001060out_free_oe:
1061 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001062out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001063 for (i = 0; i < ufs->numlower; i++)
1064 mntput(ufs->lower_mnt[i]);
1065 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001066out_put_workdir:
1067 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001068 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001069out_put_lowerpath:
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001070 for (i = 0; i < numlower; i++)
1071 path_put(&stack[i]);
1072 kfree(stack);
1073out_free_lowertmp:
1074 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001075out_put_workpath:
1076 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001077out_put_upperpath:
1078 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001079out_free_config:
Erez Zadokf45827e2014-10-24 00:14:38 +02001080 kfree(ufs->config.lowerdir);
1081 kfree(ufs->config.upperdir);
1082 kfree(ufs->config.workdir);
1083 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001084out:
1085 return err;
1086}
1087
1088static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1089 const char *dev_name, void *raw_data)
1090{
1091 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1092}
1093
1094static struct file_system_type ovl_fs_type = {
1095 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001096 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001097 .mount = ovl_mount,
1098 .kill_sb = kill_anon_super,
1099};
Miklos Szeredief94b182014-11-20 16:39:59 +01001100MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001101
1102static int __init ovl_init(void)
1103{
1104 return register_filesystem(&ovl_fs_type);
1105}
1106
1107static void __exit ovl_exit(void)
1108{
1109 unregister_filesystem(&ovl_fs_type);
1110}
1111
1112module_init(ovl_init);
1113module_exit(ovl_exit);