blob: 0eb9d3b41c547488f5d239309b133323bd1ae324 [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>
Andrew Mortone458bcd2016-01-20 15:01:13 -080018#include <linux/pagemap.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020019#include <linux/sched.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020020#include <linux/statfs.h>
Erez Zadokf45827e82014-10-24 00:14:38 +020021#include <linux/seq_file.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022#include "overlayfs.h"
23
24MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
25MODULE_DESCRIPTION("Overlay filesystem");
26MODULE_LICENSE("GPL");
27
Miklos Szeredief94b182014-11-20 16:39:59 +010028#define OVERLAYFS_SUPER_MAGIC 0x794c7630
Andy Whitcroftcc259632014-10-24 00:14:38 +020029
Erez Zadokf45827e82014-10-24 00:14:38 +020030struct ovl_config {
31 char *lowerdir;
32 char *upperdir;
33 char *workdir;
34};
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
Miklos Szeredidd662662014-12-13 00:59:43 +010079 if (oe->numlower) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020080 if (S_ISDIR(dentry->d_inode->i_mode))
Miklos Szeredi1afaba12014-12-13 00:59:42 +010081 type |= __OVL_PATH_MERGE;
82 } else if (!oe->opaque) {
83 type |= __OVL_PATH_PURE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020084 }
Miklos Szeredi9d7459d2014-12-13 00:59:44 +010085 } else {
86 if (oe->numlower > 1)
87 type |= __OVL_PATH_MERGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020088 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +010089 return type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020090}
91
92static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
93{
Miklos Szeredi71d50922014-11-20 16:40:01 +010094 return lockless_dereference(oe->__upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020095}
96
97void ovl_path_upper(struct dentry *dentry, struct path *path)
98{
99 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
100 struct ovl_entry *oe = dentry->d_fsdata;
101
102 path->mnt = ofs->upper_mnt;
103 path->dentry = ovl_upperdentry_dereference(oe);
104}
105
106enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
107{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200108 enum ovl_path_type type = ovl_path_type(dentry);
109
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100110 if (!OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200111 ovl_path_lower(dentry, path);
112 else
113 ovl_path_upper(dentry, path);
114
115 return type;
116}
117
118struct dentry *ovl_dentry_upper(struct dentry *dentry)
119{
120 struct ovl_entry *oe = dentry->d_fsdata;
121
122 return ovl_upperdentry_dereference(oe);
123}
124
125struct dentry *ovl_dentry_lower(struct dentry *dentry)
126{
127 struct ovl_entry *oe = dentry->d_fsdata;
128
Miklos Szeredidd662662014-12-13 00:59:43 +0100129 return __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200130}
131
132struct dentry *ovl_dentry_real(struct dentry *dentry)
133{
134 struct ovl_entry *oe = dentry->d_fsdata;
135 struct dentry *realdentry;
136
137 realdentry = ovl_upperdentry_dereference(oe);
138 if (!realdentry)
Miklos Szeredidd662662014-12-13 00:59:43 +0100139 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200140
141 return realdentry;
142}
143
144struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
145{
146 struct dentry *realdentry;
147
148 realdentry = ovl_upperdentry_dereference(oe);
149 if (realdentry) {
150 *is_upper = true;
151 } else {
Miklos Szeredidd662662014-12-13 00:59:43 +0100152 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200153 *is_upper = false;
154 }
155 return realdentry;
156}
157
158struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
159{
160 struct ovl_entry *oe = dentry->d_fsdata;
161
162 return oe->cache;
163}
164
165void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
166{
167 struct ovl_entry *oe = dentry->d_fsdata;
168
169 oe->cache = cache;
170}
171
172void ovl_path_lower(struct dentry *dentry, struct path *path)
173{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200174 struct ovl_entry *oe = dentry->d_fsdata;
175
Miklos Szeredidd662662014-12-13 00:59:43 +0100176 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200177}
178
179int ovl_want_write(struct dentry *dentry)
180{
181 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
182 return mnt_want_write(ofs->upper_mnt);
183}
184
185void ovl_drop_write(struct dentry *dentry)
186{
187 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
188 mnt_drop_write(ofs->upper_mnt);
189}
190
191struct dentry *ovl_workdir(struct dentry *dentry)
192{
193 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
194 return ofs->workdir;
195}
196
197bool ovl_dentry_is_opaque(struct dentry *dentry)
198{
199 struct ovl_entry *oe = dentry->d_fsdata;
200 return oe->opaque;
201}
202
203void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
204{
205 struct ovl_entry *oe = dentry->d_fsdata;
206 oe->opaque = opaque;
207}
208
209void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
210{
211 struct ovl_entry *oe = dentry->d_fsdata;
212
213 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
214 WARN_ON(oe->__upperdentry);
215 BUG_ON(!upperdentry->d_inode);
216 /*
217 * Make sure upperdentry is consistent before making it visible to
218 * ovl_upperdentry_dereference().
219 */
220 smp_wmb();
221 oe->__upperdentry = upperdentry;
222}
223
224void ovl_dentry_version_inc(struct dentry *dentry)
225{
226 struct ovl_entry *oe = dentry->d_fsdata;
227
228 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
229 oe->version++;
230}
231
232u64 ovl_dentry_version_get(struct dentry *dentry)
233{
234 struct ovl_entry *oe = dentry->d_fsdata;
235
236 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
237 return oe->version;
238}
239
240bool ovl_is_whiteout(struct dentry *dentry)
241{
242 struct inode *inode = dentry->d_inode;
243
244 return inode && IS_WHITEOUT(inode);
245}
246
247static bool ovl_is_opaquedir(struct dentry *dentry)
248{
249 int res;
250 char val;
251 struct inode *inode = dentry->d_inode;
252
253 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
254 return false;
255
hujianyangcead89b2014-11-24 18:25:21 +0800256 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200257 if (res == 1 && val == 'y')
258 return true;
259
260 return false;
261}
262
263static void ovl_dentry_release(struct dentry *dentry)
264{
265 struct ovl_entry *oe = dentry->d_fsdata;
266
267 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +0100268 unsigned int i;
269
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200270 dput(oe->__upperdentry);
Miklos Szeredidd662662014-12-13 00:59:43 +0100271 for (i = 0; i < oe->numlower; i++)
272 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200273 kfree_rcu(oe, rcu);
274 }
275}
276
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200277static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
278{
279 struct ovl_entry *oe = dentry->d_fsdata;
280 unsigned int i;
281 int ret = 1;
282
283 for (i = 0; i < oe->numlower; i++) {
284 struct dentry *d = oe->lowerstack[i].dentry;
285
286 if (d->d_flags & DCACHE_OP_REVALIDATE) {
287 ret = d->d_op->d_revalidate(d, flags);
288 if (ret < 0)
289 return ret;
290 if (!ret) {
291 if (!(flags & LOOKUP_RCU))
292 d_invalidate(d);
293 return -ESTALE;
294 }
295 }
296 }
297 return 1;
298}
299
300static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
301{
302 struct ovl_entry *oe = dentry->d_fsdata;
303 unsigned int i;
304 int ret = 1;
305
306 for (i = 0; i < oe->numlower; i++) {
307 struct dentry *d = oe->lowerstack[i].dentry;
308
309 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
310 ret = d->d_op->d_weak_revalidate(d, flags);
311 if (ret <= 0)
312 break;
313 }
314 }
315 return ret;
316}
317
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200318static const struct dentry_operations ovl_dentry_operations = {
319 .d_release = ovl_dentry_release,
David Howells4bacc9c2015-06-18 14:32:31 +0100320 .d_select_inode = ovl_d_select_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200321};
322
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200323static const struct dentry_operations ovl_reval_dentry_operations = {
324 .d_release = ovl_dentry_release,
325 .d_revalidate = ovl_dentry_revalidate,
326 .d_weak_revalidate = ovl_dentry_weak_revalidate,
327};
328
Miklos Szeredidd662662014-12-13 00:59:43 +0100329static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330{
Miklos Szeredidd662662014-12-13 00:59:43 +0100331 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
332 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
333
334 if (oe)
335 oe->numlower = numlower;
336
337 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338}
339
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200340static bool ovl_dentry_remote(struct dentry *dentry)
341{
342 return dentry->d_flags &
343 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
344}
345
346static bool ovl_dentry_weird(struct dentry *dentry)
347{
348 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
349 DCACHE_MANAGE_TRANSIT |
350 DCACHE_OP_HASH |
351 DCACHE_OP_COMPARE);
352}
353
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200354static inline struct dentry *ovl_lookup_real(struct dentry *dir,
355 struct qstr *name)
356{
357 struct dentry *dentry;
358
359 mutex_lock(&dir->d_inode->i_mutex);
360 dentry = lookup_one_len(name->name, dir, name->len);
361 mutex_unlock(&dir->d_inode->i_mutex);
362
363 if (IS_ERR(dentry)) {
364 if (PTR_ERR(dentry) == -ENOENT)
365 dentry = NULL;
366 } else if (!dentry->d_inode) {
367 dput(dentry);
368 dentry = NULL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200369 } else if (ovl_dentry_weird(dentry)) {
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200370 dput(dentry);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200371 /* Don't support traversing automounts and other weirdness */
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200372 dentry = ERR_PTR(-EREMOTE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200373 }
374 return dentry;
375}
376
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100377/*
378 * Returns next layer in stack starting from top.
379 * Returns -1 if this is the last layer.
380 */
381int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
382{
383 struct ovl_entry *oe = dentry->d_fsdata;
384
385 BUG_ON(idx < 0);
386 if (idx == 0) {
387 ovl_path_upper(dentry, path);
388 if (path->dentry)
389 return oe->numlower ? 1 : -1;
390 idx++;
391 }
392 BUG_ON(idx > oe->numlower);
393 *path = oe->lowerstack[idx - 1];
394
395 return (idx < oe->numlower) ? idx + 1 : -1;
396}
397
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200398struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
399 unsigned int flags)
400{
401 struct ovl_entry *oe;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100402 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
403 struct path *stack = NULL;
404 struct dentry *upperdir, *upperdentry = NULL;
405 unsigned int ctr = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406 struct inode *inode = NULL;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100407 bool upperopaque = false;
408 struct dentry *this, *prev = NULL;
409 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200410 int err;
411
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100412 upperdir = ovl_upperdentry_dereference(poe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200413 if (upperdir) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100414 this = ovl_lookup_real(upperdir, &dentry->d_name);
415 err = PTR_ERR(this);
416 if (IS_ERR(this))
417 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200418
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100419 if (this) {
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200420 if (unlikely(ovl_dentry_remote(this))) {
421 dput(this);
422 err = -EREMOTE;
423 goto out;
424 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100425 if (ovl_is_whiteout(this)) {
426 dput(this);
427 this = NULL;
428 upperopaque = true;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100429 } else if (poe->numlower && ovl_is_opaquedir(this)) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100430 upperopaque = true;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200431 }
432 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100433 upperdentry = prev = this;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200434 }
435
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100436 if (!upperopaque && poe->numlower) {
437 err = -ENOMEM;
438 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
439 if (!stack)
440 goto out_put_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200441 }
442
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100443 for (i = 0; !upperopaque && i < poe->numlower; i++) {
444 bool opaque = false;
445 struct path lowerpath = poe->lowerstack[i];
446
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100447 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
448 err = PTR_ERR(this);
Miklos Szeredi09e10322014-12-13 00:59:45 +0100449 if (IS_ERR(this)) {
450 /*
451 * If it's positive, then treat ENAMETOOLONG as ENOENT.
452 */
453 if (err == -ENAMETOOLONG && (upperdentry || ctr))
454 continue;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100455 goto out_put;
Miklos Szeredi09e10322014-12-13 00:59:45 +0100456 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100457 if (!this)
458 continue;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100459 if (ovl_is_whiteout(this)) {
460 dput(this);
461 break;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100462 }
463 /*
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100464 * Only makes sense to check opaque dir if this is not the
465 * lowermost layer.
466 */
467 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
468 opaque = true;
hujianyanga425c032015-01-06 16:10:01 +0800469
470 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
471 !S_ISDIR(this->d_inode->i_mode))) {
472 /*
473 * FIXME: check for upper-opaqueness maybe better done
474 * in remove code.
475 */
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100476 if (prev == upperdentry)
477 upperopaque = true;
478 dput(this);
479 break;
480 }
hujianyanga425c032015-01-06 16:10:01 +0800481 /*
482 * If this is a non-directory then stop here.
483 */
484 if (!S_ISDIR(this->d_inode->i_mode))
485 opaque = true;
486
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100487 stack[ctr].dentry = this;
488 stack[ctr].mnt = lowerpath.mnt;
489 ctr++;
490 prev = this;
491 if (opaque)
492 break;
493 }
494
495 oe = ovl_alloc_entry(ctr);
496 err = -ENOMEM;
497 if (!oe)
498 goto out_put;
499
500 if (upperdentry || ctr) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200501 struct dentry *realdentry;
502
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100503 realdentry = upperdentry ? upperdentry : stack[0].dentry;
504
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200505 err = -ENOMEM;
506 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
507 oe);
508 if (!inode)
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100509 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200510 ovl_copyattr(realdentry->d_inode, inode);
511 }
512
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100513 oe->opaque = upperopaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200514 oe->__upperdentry = upperdentry;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100515 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
516 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200517 dentry->d_fsdata = oe;
518 d_add(dentry, inode);
519
520 return NULL;
521
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100522out_free_oe:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200523 kfree(oe);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100524out_put:
525 for (i = 0; i < ctr; i++)
526 dput(stack[i].dentry);
527 kfree(stack);
528out_put_upper:
529 dput(upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200530out:
531 return ERR_PTR(err);
532}
533
534struct file *ovl_path_open(struct path *path, int flags)
535{
536 return dentry_open(path, flags, current_cred());
537}
538
539static void ovl_put_super(struct super_block *sb)
540{
541 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100542 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200543
544 dput(ufs->workdir);
545 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100546 for (i = 0; i < ufs->numlower; i++)
547 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300548 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200549
Erez Zadokf45827e82014-10-24 00:14:38 +0200550 kfree(ufs->config.lowerdir);
551 kfree(ufs->config.upperdir);
552 kfree(ufs->config.workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200553 kfree(ufs);
554}
555
Andy Whitcroftcc259632014-10-24 00:14:38 +0200556/**
557 * ovl_statfs
558 * @sb: The overlayfs super block
559 * @buf: The struct kstatfs to fill in with stats
560 *
561 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100562 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200563 */
564static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
565{
566 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
567 struct dentry *root_dentry = dentry->d_sb->s_root;
568 struct path path;
569 int err;
570
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100571 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200572
573 err = vfs_statfs(&path, buf);
574 if (!err) {
575 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
576 buf->f_type = OVERLAYFS_SUPER_MAGIC;
577 }
578
579 return err;
580}
581
Erez Zadokf45827e82014-10-24 00:14:38 +0200582/**
583 * ovl_show_options
584 *
585 * Prints the mount options for a given superblock.
586 * Returns zero; does not fail.
587 */
588static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
589{
590 struct super_block *sb = dentry->d_sb;
591 struct ovl_fs *ufs = sb->s_fs_info;
592
Kees Cooka068acf2015-09-04 15:44:57 -0700593 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100594 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700595 seq_show_option(m, "upperdir", ufs->config.upperdir);
596 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100597 }
Erez Zadokf45827e82014-10-24 00:14:38 +0200598 return 0;
599}
600
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900601static int ovl_remount(struct super_block *sb, int *flags, char *data)
602{
603 struct ovl_fs *ufs = sb->s_fs_info;
604
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200605 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900606 return -EROFS;
607
608 return 0;
609}
610
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200611static const struct super_operations ovl_super_operations = {
612 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200613 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200614 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900615 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200616};
617
618enum {
619 OPT_LOWERDIR,
620 OPT_UPPERDIR,
621 OPT_WORKDIR,
622 OPT_ERR,
623};
624
625static const match_table_t ovl_tokens = {
626 {OPT_LOWERDIR, "lowerdir=%s"},
627 {OPT_UPPERDIR, "upperdir=%s"},
628 {OPT_WORKDIR, "workdir=%s"},
629 {OPT_ERR, NULL}
630};
631
Miklos Szeredi91c77942014-11-20 16:40:00 +0100632static char *ovl_next_opt(char **s)
633{
634 char *sbegin = *s;
635 char *p;
636
637 if (sbegin == NULL)
638 return NULL;
639
640 for (p = sbegin; *p; p++) {
641 if (*p == '\\') {
642 p++;
643 if (!*p)
644 break;
645 } else if (*p == ',') {
646 *p = '\0';
647 *s = p + 1;
648 return sbegin;
649 }
650 }
651 *s = NULL;
652 return sbegin;
653}
654
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200655static int ovl_parse_opt(char *opt, struct ovl_config *config)
656{
657 char *p;
658
Miklos Szeredi91c77942014-11-20 16:40:00 +0100659 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200660 int token;
661 substring_t args[MAX_OPT_ARGS];
662
663 if (!*p)
664 continue;
665
666 token = match_token(p, ovl_tokens, args);
667 switch (token) {
668 case OPT_UPPERDIR:
669 kfree(config->upperdir);
670 config->upperdir = match_strdup(&args[0]);
671 if (!config->upperdir)
672 return -ENOMEM;
673 break;
674
675 case OPT_LOWERDIR:
676 kfree(config->lowerdir);
677 config->lowerdir = match_strdup(&args[0]);
678 if (!config->lowerdir)
679 return -ENOMEM;
680 break;
681
682 case OPT_WORKDIR:
683 kfree(config->workdir);
684 config->workdir = match_strdup(&args[0]);
685 if (!config->workdir)
686 return -ENOMEM;
687 break;
688
689 default:
hujianyangbead55e2015-01-15 13:17:36 +0800690 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200691 return -EINVAL;
692 }
693 }
hujianyang71cbad72015-01-15 13:20:57 +0800694
695 /* Workdir is useless in non-upper mount */
696 if (!config->upperdir && config->workdir) {
697 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
698 config->workdir);
699 kfree(config->workdir);
700 config->workdir = NULL;
701 }
702
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200703 return 0;
704}
705
706#define OVL_WORKDIR_NAME "work"
707
708static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
709 struct dentry *dentry)
710{
711 struct inode *dir = dentry->d_inode;
712 struct dentry *work;
713 int err;
714 bool retried = false;
715
716 err = mnt_want_write(mnt);
717 if (err)
718 return ERR_PTR(err);
719
720 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
721retry:
722 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
723 strlen(OVL_WORKDIR_NAME));
724
725 if (!IS_ERR(work)) {
726 struct kstat stat = {
727 .mode = S_IFDIR | 0,
728 };
729
730 if (work->d_inode) {
731 err = -EEXIST;
732 if (retried)
733 goto out_dput;
734
735 retried = true;
736 ovl_cleanup(dir, work);
737 dput(work);
738 goto retry;
739 }
740
741 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
742 if (err)
743 goto out_dput;
744 }
745out_unlock:
746 mutex_unlock(&dir->i_mutex);
747 mnt_drop_write(mnt);
748
749 return work;
750
751out_dput:
752 dput(work);
753 work = ERR_PTR(err);
754 goto out_unlock;
755}
756
Miklos Szeredi91c77942014-11-20 16:40:00 +0100757static void ovl_unescape(char *s)
758{
759 char *d = s;
760
761 for (;; s++, d++) {
762 if (*s == '\\')
763 s++;
764 *d = *s;
765 if (!*s)
766 break;
767 }
768}
769
Miklos Szerediab508822014-12-13 00:59:49 +0100770static int ovl_mount_dir_noesc(const char *name, struct path *path)
771{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100772 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100773
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100774 if (!*name) {
775 pr_err("overlayfs: empty lowerdir\n");
776 goto out;
777 }
Miklos Szerediab508822014-12-13 00:59:49 +0100778 err = kern_path(name, LOOKUP_FOLLOW, path);
779 if (err) {
780 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
781 goto out;
782 }
783 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200784 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100785 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
786 goto out_put;
787 }
788 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
789 pr_err("overlayfs: '%s' not a directory\n", name);
790 goto out_put;
791 }
792 return 0;
793
794out_put:
795 path_put(path);
796out:
797 return err;
798}
799
800static int ovl_mount_dir(const char *name, struct path *path)
801{
802 int err = -ENOMEM;
803 char *tmp = kstrdup(name, GFP_KERNEL);
804
805 if (tmp) {
806 ovl_unescape(tmp);
807 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200808
809 if (!err)
810 if (ovl_dentry_remote(path->dentry)) {
811 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
812 tmp);
813 path_put(path);
814 err = -EINVAL;
815 }
Miklos Szerediab508822014-12-13 00:59:49 +0100816 kfree(tmp);
817 }
818 return err;
819}
820
821static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200822 int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100823{
824 int err;
825 struct kstatfs statfs;
826
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100827 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100828 if (err)
829 goto out;
830
831 err = vfs_statfs(path, &statfs);
832 if (err) {
833 pr_err("overlayfs: statfs failed on '%s'\n", name);
834 goto out_put;
835 }
836 *namelen = max(*namelen, statfs.f_namelen);
837 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
838
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200839 if (ovl_dentry_remote(path->dentry))
840 *remote = true;
841
Miklos Szerediab508822014-12-13 00:59:49 +0100842 return 0;
843
844out_put:
845 path_put(path);
846out:
847 return err;
848}
849
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200850/* Workdir should not be subdir of upperdir and vice versa */
851static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
852{
853 bool ok = false;
854
855 if (workdir != upperdir) {
856 ok = (lock_rename(workdir, upperdir) == NULL);
857 unlock_rename(workdir, upperdir);
858 }
859 return ok;
860}
861
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100862static unsigned int ovl_split_lowerdirs(char *str)
863{
864 unsigned int ctr = 1;
865 char *s, *d;
866
867 for (s = d = str;; s++, d++) {
868 if (*s == '\\') {
869 s++;
870 } else if (*s == ':') {
871 *d = '\0';
872 ctr++;
873 continue;
874 }
875 *d = *s;
876 if (!*s)
877 break;
878 }
879 return ctr;
880}
881
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200882static int ovl_fill_super(struct super_block *sb, void *data, int silent)
883{
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100884 struct path upperpath = { NULL, NULL };
885 struct path workpath = { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200886 struct dentry *root_dentry;
887 struct ovl_entry *oe;
888 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100889 struct path *stack = NULL;
890 char *lowertmp;
891 char *lower;
892 unsigned int numlower;
893 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100894 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200895 bool remote = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200896 int err;
897
Erez Zadokf45827e82014-10-24 00:14:38 +0200898 err = -ENOMEM;
899 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
900 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200901 goto out;
902
Erez Zadokf45827e82014-10-24 00:14:38 +0200903 err = ovl_parse_opt((char *) data, &ufs->config);
904 if (err)
905 goto out_free_config;
906
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200907 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100908 if (!ufs->config.lowerdir) {
909 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200910 goto out_free_config;
911 }
912
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100913 sb->s_stack_depth = 0;
914 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100915 if (!ufs->config.workdir) {
916 pr_err("overlayfs: missing 'workdir'\n");
917 goto out_free_config;
918 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200919
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100920 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
921 if (err)
922 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100923
hujianyang71cbad72015-01-15 13:20:57 +0800924 /* Upper fs should not be r/o */
925 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
926 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
927 err = -EINVAL;
928 goto out_put_upperpath;
929 }
930
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100931 err = ovl_mount_dir(ufs->config.workdir, &workpath);
932 if (err)
933 goto out_put_upperpath;
934
hujianyang2f83fd82015-01-06 12:52:13 +0800935 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100936 if (upperpath.mnt != workpath.mnt) {
937 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
938 goto out_put_workpath;
939 }
940 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
941 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
942 goto out_put_workpath;
943 }
944 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100945 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100946 err = -ENOMEM;
947 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
948 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100949 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200950
951 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100952 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800953 if (stacklen > OVL_MAX_STACK) {
954 pr_err("overlayfs: too many lower directries, limit is %d\n",
955 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100956 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800957 } else if (!ufs->config.upperdir && stacklen == 1) {
958 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
959 goto out_free_lowertmp;
960 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100961
962 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
963 if (!stack)
964 goto out_free_lowertmp;
965
966 lower = lowertmp;
967 for (numlower = 0; numlower < stacklen; numlower++) {
968 err = ovl_lower_dir(lower, &stack[numlower],
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200969 &ufs->lower_namelen, &sb->s_stack_depth,
970 &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100971 if (err)
972 goto out_put_lowerpath;
973
974 lower = strchr(lower, '\0') + 1;
975 }
976
977 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100978 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200979 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
980 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100981 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200982 }
983
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100984 if (ufs->config.upperdir) {
985 ufs->upper_mnt = clone_private_mount(&upperpath);
986 err = PTR_ERR(ufs->upper_mnt);
987 if (IS_ERR(ufs->upper_mnt)) {
988 pr_err("overlayfs: failed to clone upperpath\n");
989 goto out_put_lowerpath;
990 }
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100991
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100992 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
993 err = PTR_ERR(ufs->workdir);
994 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200995 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
996 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
997 sb->s_flags |= MS_RDONLY;
998 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100999 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001000 }
1001
hujianyang2f83fd82015-01-06 12:52:13 +08001002 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001003 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001004 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001005 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001006 for (i = 0; i < numlower; i++) {
1007 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001008
hujianyang2f83fd82015-01-06 12:52:13 +08001009 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001010 if (IS_ERR(mnt)) {
1011 pr_err("overlayfs: failed to clone lowerpath\n");
1012 goto out_put_lower_mnt;
1013 }
1014 /*
1015 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1016 * will fail instead of modifying lower fs.
1017 */
1018 mnt->mnt_flags |= MNT_READONLY;
1019
1020 ufs->lower_mnt[ufs->numlower] = mnt;
1021 ufs->numlower++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001022 }
1023
hujianyang71cbad72015-01-15 13:20:57 +08001024 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1025 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001026 sb->s_flags |= MS_RDONLY;
1027
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001028 if (remote)
1029 sb->s_d_op = &ovl_reval_dentry_operations;
1030 else
1031 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001032
1033 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001034 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001035 if (!oe)
1036 goto out_put_lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001037
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001038 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001039 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001040 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001041
1042 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001043 for (i = 0; i < numlower; i++)
1044 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001045 path_put(&workpath);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001046 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001047
1048 oe->__upperdentry = upperpath.dentry;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001049 for (i = 0; i < numlower; i++) {
1050 oe->lowerstack[i].dentry = stack[i].dentry;
1051 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1052 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001053 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001054
1055 root_dentry->d_fsdata = oe;
1056
Andy Whitcroftcc259632014-10-24 00:14:38 +02001057 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001058 sb->s_op = &ovl_super_operations;
1059 sb->s_root = root_dentry;
1060 sb->s_fs_info = ufs;
1061
1062 return 0;
1063
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001064out_free_oe:
1065 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001066out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001067 for (i = 0; i < ufs->numlower; i++)
1068 mntput(ufs->lower_mnt[i]);
1069 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001070out_put_workdir:
1071 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001072 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001073out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001074 for (i = 0; i < numlower; i++)
1075 path_put(&stack[i]);
1076 kfree(stack);
1077out_free_lowertmp:
1078 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001079out_put_workpath:
1080 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001081out_put_upperpath:
1082 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001083out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001084 kfree(ufs->config.lowerdir);
1085 kfree(ufs->config.upperdir);
1086 kfree(ufs->config.workdir);
1087 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001088out:
1089 return err;
1090}
1091
1092static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1093 const char *dev_name, void *raw_data)
1094{
1095 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1096}
1097
1098static struct file_system_type ovl_fs_type = {
1099 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001100 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001101 .mount = ovl_mount,
1102 .kill_sb = kill_anon_super,
1103};
Miklos Szeredief94b182014-11-20 16:39:59 +01001104MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001105
1106static int __init ovl_init(void)
1107{
1108 return register_filesystem(&ovl_fs_type);
1109}
1110
1111static void __exit ovl_exit(void)
1112{
1113 unregister_filesystem(&ovl_fs_type);
1114}
1115
1116module_init(ovl_init);
1117module_exit(ovl_exit);