blob: e38ee0fed24a2f7a343f294a793b6abea93f6f97 [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 Zadokf45827e82014-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 Zadokf45827e82014-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 Zadokf45827e82014-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,
David Howells4bacc9c2015-06-18 14:32:31 +0100319 .d_select_inode = ovl_d_select_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320};
321
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200322static const struct dentry_operations ovl_reval_dentry_operations = {
323 .d_release = ovl_dentry_release,
324 .d_revalidate = ovl_dentry_revalidate,
325 .d_weak_revalidate = ovl_dentry_weak_revalidate,
326};
327
Miklos Szeredidd662662014-12-13 00:59:43 +0100328static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200329{
Miklos Szeredidd662662014-12-13 00:59:43 +0100330 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
331 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
332
333 if (oe)
334 oe->numlower = numlower;
335
336 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200337}
338
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200339static bool ovl_dentry_remote(struct dentry *dentry)
340{
341 return dentry->d_flags &
342 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
343}
344
345static bool ovl_dentry_weird(struct dentry *dentry)
346{
347 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
348 DCACHE_MANAGE_TRANSIT |
349 DCACHE_OP_HASH |
350 DCACHE_OP_COMPARE);
351}
352
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200353static inline struct dentry *ovl_lookup_real(struct dentry *dir,
354 struct qstr *name)
355{
356 struct dentry *dentry;
357
358 mutex_lock(&dir->d_inode->i_mutex);
359 dentry = lookup_one_len(name->name, dir, name->len);
360 mutex_unlock(&dir->d_inode->i_mutex);
361
362 if (IS_ERR(dentry)) {
363 if (PTR_ERR(dentry) == -ENOENT)
364 dentry = NULL;
365 } else if (!dentry->d_inode) {
366 dput(dentry);
367 dentry = NULL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200368 } else if (ovl_dentry_weird(dentry)) {
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200369 dput(dentry);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200370 /* Don't support traversing automounts and other weirdness */
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200371 dentry = ERR_PTR(-EREMOTE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200372 }
373 return dentry;
374}
375
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100376/*
377 * Returns next layer in stack starting from top.
378 * Returns -1 if this is the last layer.
379 */
380int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
381{
382 struct ovl_entry *oe = dentry->d_fsdata;
383
384 BUG_ON(idx < 0);
385 if (idx == 0) {
386 ovl_path_upper(dentry, path);
387 if (path->dentry)
388 return oe->numlower ? 1 : -1;
389 idx++;
390 }
391 BUG_ON(idx > oe->numlower);
392 *path = oe->lowerstack[idx - 1];
393
394 return (idx < oe->numlower) ? idx + 1 : -1;
395}
396
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
398 unsigned int flags)
399{
400 struct ovl_entry *oe;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100401 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
402 struct path *stack = NULL;
403 struct dentry *upperdir, *upperdentry = NULL;
404 unsigned int ctr = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200405 struct inode *inode = NULL;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100406 bool upperopaque = false;
407 struct dentry *this, *prev = NULL;
408 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200409 int err;
410
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100411 upperdir = ovl_upperdentry_dereference(poe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200412 if (upperdir) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100413 this = ovl_lookup_real(upperdir, &dentry->d_name);
414 err = PTR_ERR(this);
415 if (IS_ERR(this))
416 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200417
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100418 if (this) {
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200419 if (unlikely(ovl_dentry_remote(this))) {
420 dput(this);
421 err = -EREMOTE;
422 goto out;
423 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100424 if (ovl_is_whiteout(this)) {
425 dput(this);
426 this = NULL;
427 upperopaque = true;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100428 } else if (poe->numlower && ovl_is_opaquedir(this)) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100429 upperopaque = true;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200430 }
431 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100432 upperdentry = prev = this;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200433 }
434
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100435 if (!upperopaque && poe->numlower) {
436 err = -ENOMEM;
437 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
438 if (!stack)
439 goto out_put_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200440 }
441
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100442 for (i = 0; !upperopaque && i < poe->numlower; i++) {
443 bool opaque = false;
444 struct path lowerpath = poe->lowerstack[i];
445
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100446 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
447 err = PTR_ERR(this);
Miklos Szeredi09e10322014-12-13 00:59:45 +0100448 if (IS_ERR(this)) {
449 /*
450 * If it's positive, then treat ENAMETOOLONG as ENOENT.
451 */
452 if (err == -ENAMETOOLONG && (upperdentry || ctr))
453 continue;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100454 goto out_put;
Miklos Szeredi09e10322014-12-13 00:59:45 +0100455 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100456 if (!this)
457 continue;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100458 if (ovl_is_whiteout(this)) {
459 dput(this);
460 break;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100461 }
462 /*
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100463 * Only makes sense to check opaque dir if this is not the
464 * lowermost layer.
465 */
466 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
467 opaque = true;
hujianyanga425c032015-01-06 16:10:01 +0800468
469 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
470 !S_ISDIR(this->d_inode->i_mode))) {
471 /*
472 * FIXME: check for upper-opaqueness maybe better done
473 * in remove code.
474 */
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100475 if (prev == upperdentry)
476 upperopaque = true;
477 dput(this);
478 break;
479 }
hujianyanga425c032015-01-06 16:10:01 +0800480 /*
481 * If this is a non-directory then stop here.
482 */
483 if (!S_ISDIR(this->d_inode->i_mode))
484 opaque = true;
485
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100486 stack[ctr].dentry = this;
487 stack[ctr].mnt = lowerpath.mnt;
488 ctr++;
489 prev = this;
490 if (opaque)
491 break;
492 }
493
494 oe = ovl_alloc_entry(ctr);
495 err = -ENOMEM;
496 if (!oe)
497 goto out_put;
498
499 if (upperdentry || ctr) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200500 struct dentry *realdentry;
501
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100502 realdentry = upperdentry ? upperdentry : stack[0].dentry;
503
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200504 err = -ENOMEM;
505 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
506 oe);
507 if (!inode)
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100508 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200509 ovl_copyattr(realdentry->d_inode, inode);
510 }
511
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100512 oe->opaque = upperopaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200513 oe->__upperdentry = upperdentry;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100514 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
515 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200516 dentry->d_fsdata = oe;
517 d_add(dentry, inode);
518
519 return NULL;
520
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100521out_free_oe:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200522 kfree(oe);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100523out_put:
524 for (i = 0; i < ctr; i++)
525 dput(stack[i].dentry);
526 kfree(stack);
527out_put_upper:
528 dput(upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200529out:
530 return ERR_PTR(err);
531}
532
533struct file *ovl_path_open(struct path *path, int flags)
534{
535 return dentry_open(path, flags, current_cred());
536}
537
538static void ovl_put_super(struct super_block *sb)
539{
540 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100541 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200542
543 dput(ufs->workdir);
544 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100545 for (i = 0; i < ufs->numlower; i++)
546 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300547 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200548
Erez Zadokf45827e82014-10-24 00:14:38 +0200549 kfree(ufs->config.lowerdir);
550 kfree(ufs->config.upperdir);
551 kfree(ufs->config.workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200552 kfree(ufs);
553}
554
Andy Whitcroftcc259632014-10-24 00:14:38 +0200555/**
556 * ovl_statfs
557 * @sb: The overlayfs super block
558 * @buf: The struct kstatfs to fill in with stats
559 *
560 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100561 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200562 */
563static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
564{
565 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
566 struct dentry *root_dentry = dentry->d_sb->s_root;
567 struct path path;
568 int err;
569
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100570 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200571
572 err = vfs_statfs(&path, buf);
573 if (!err) {
574 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
575 buf->f_type = OVERLAYFS_SUPER_MAGIC;
576 }
577
578 return err;
579}
580
Erez Zadokf45827e82014-10-24 00:14:38 +0200581/**
582 * ovl_show_options
583 *
584 * Prints the mount options for a given superblock.
585 * Returns zero; does not fail.
586 */
587static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
588{
589 struct super_block *sb = dentry->d_sb;
590 struct ovl_fs *ufs = sb->s_fs_info;
591
Kees Cooka068acf2015-09-04 15:44:57 -0700592 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100593 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700594 seq_show_option(m, "upperdir", ufs->config.upperdir);
595 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100596 }
Erez Zadokf45827e82014-10-24 00:14:38 +0200597 return 0;
598}
599
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900600static int ovl_remount(struct super_block *sb, int *flags, char *data)
601{
602 struct ovl_fs *ufs = sb->s_fs_info;
603
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200604 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900605 return -EROFS;
606
607 return 0;
608}
609
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200610static const struct super_operations ovl_super_operations = {
611 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200612 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200613 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900614 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200615};
616
617enum {
618 OPT_LOWERDIR,
619 OPT_UPPERDIR,
620 OPT_WORKDIR,
621 OPT_ERR,
622};
623
624static const match_table_t ovl_tokens = {
625 {OPT_LOWERDIR, "lowerdir=%s"},
626 {OPT_UPPERDIR, "upperdir=%s"},
627 {OPT_WORKDIR, "workdir=%s"},
628 {OPT_ERR, NULL}
629};
630
Miklos Szeredi91c77942014-11-20 16:40:00 +0100631static char *ovl_next_opt(char **s)
632{
633 char *sbegin = *s;
634 char *p;
635
636 if (sbegin == NULL)
637 return NULL;
638
639 for (p = sbegin; *p; p++) {
640 if (*p == '\\') {
641 p++;
642 if (!*p)
643 break;
644 } else if (*p == ',') {
645 *p = '\0';
646 *s = p + 1;
647 return sbegin;
648 }
649 }
650 *s = NULL;
651 return sbegin;
652}
653
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200654static int ovl_parse_opt(char *opt, struct ovl_config *config)
655{
656 char *p;
657
Miklos Szeredi91c77942014-11-20 16:40:00 +0100658 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200659 int token;
660 substring_t args[MAX_OPT_ARGS];
661
662 if (!*p)
663 continue;
664
665 token = match_token(p, ovl_tokens, args);
666 switch (token) {
667 case OPT_UPPERDIR:
668 kfree(config->upperdir);
669 config->upperdir = match_strdup(&args[0]);
670 if (!config->upperdir)
671 return -ENOMEM;
672 break;
673
674 case OPT_LOWERDIR:
675 kfree(config->lowerdir);
676 config->lowerdir = match_strdup(&args[0]);
677 if (!config->lowerdir)
678 return -ENOMEM;
679 break;
680
681 case OPT_WORKDIR:
682 kfree(config->workdir);
683 config->workdir = match_strdup(&args[0]);
684 if (!config->workdir)
685 return -ENOMEM;
686 break;
687
688 default:
hujianyangbead55e2015-01-15 13:17:36 +0800689 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200690 return -EINVAL;
691 }
692 }
hujianyang71cbad72015-01-15 13:20:57 +0800693
694 /* Workdir is useless in non-upper mount */
695 if (!config->upperdir && config->workdir) {
696 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
697 config->workdir);
698 kfree(config->workdir);
699 config->workdir = NULL;
700 }
701
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200702 return 0;
703}
704
705#define OVL_WORKDIR_NAME "work"
706
707static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
708 struct dentry *dentry)
709{
710 struct inode *dir = dentry->d_inode;
711 struct dentry *work;
712 int err;
713 bool retried = false;
714
715 err = mnt_want_write(mnt);
716 if (err)
717 return ERR_PTR(err);
718
719 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
720retry:
721 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
722 strlen(OVL_WORKDIR_NAME));
723
724 if (!IS_ERR(work)) {
725 struct kstat stat = {
726 .mode = S_IFDIR | 0,
727 };
728
729 if (work->d_inode) {
730 err = -EEXIST;
731 if (retried)
732 goto out_dput;
733
734 retried = true;
735 ovl_cleanup(dir, work);
736 dput(work);
737 goto retry;
738 }
739
740 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
741 if (err)
742 goto out_dput;
743 }
744out_unlock:
745 mutex_unlock(&dir->i_mutex);
746 mnt_drop_write(mnt);
747
748 return work;
749
750out_dput:
751 dput(work);
752 work = ERR_PTR(err);
753 goto out_unlock;
754}
755
Miklos Szeredi91c77942014-11-20 16:40:00 +0100756static void ovl_unescape(char *s)
757{
758 char *d = s;
759
760 for (;; s++, d++) {
761 if (*s == '\\')
762 s++;
763 *d = *s;
764 if (!*s)
765 break;
766 }
767}
768
Miklos Szerediab508822014-12-13 00:59:49 +0100769static int ovl_mount_dir_noesc(const char *name, struct path *path)
770{
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100771 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100772
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100773 if (!*name) {
774 pr_err("overlayfs: empty lowerdir\n");
775 goto out;
776 }
Miklos Szerediab508822014-12-13 00:59:49 +0100777 err = kern_path(name, LOOKUP_FOLLOW, path);
778 if (err) {
779 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
780 goto out;
781 }
782 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200783 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100784 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
785 goto out_put;
786 }
787 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
788 pr_err("overlayfs: '%s' not a directory\n", name);
789 goto out_put;
790 }
791 return 0;
792
793out_put:
794 path_put(path);
795out:
796 return err;
797}
798
799static int ovl_mount_dir(const char *name, struct path *path)
800{
801 int err = -ENOMEM;
802 char *tmp = kstrdup(name, GFP_KERNEL);
803
804 if (tmp) {
805 ovl_unescape(tmp);
806 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200807
808 if (!err)
809 if (ovl_dentry_remote(path->dentry)) {
810 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
811 tmp);
812 path_put(path);
813 err = -EINVAL;
814 }
Miklos Szerediab508822014-12-13 00:59:49 +0100815 kfree(tmp);
816 }
817 return err;
818}
819
820static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200821 int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100822{
823 int err;
824 struct kstatfs statfs;
825
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100826 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100827 if (err)
828 goto out;
829
830 err = vfs_statfs(path, &statfs);
831 if (err) {
832 pr_err("overlayfs: statfs failed on '%s'\n", name);
833 goto out_put;
834 }
835 *namelen = max(*namelen, statfs.f_namelen);
836 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
837
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200838 if (ovl_dentry_remote(path->dentry))
839 *remote = true;
840
Miklos Szerediab508822014-12-13 00:59:49 +0100841 return 0;
842
843out_put:
844 path_put(path);
845out:
846 return err;
847}
848
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200849/* Workdir should not be subdir of upperdir and vice versa */
850static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
851{
852 bool ok = false;
853
854 if (workdir != upperdir) {
855 ok = (lock_rename(workdir, upperdir) == NULL);
856 unlock_rename(workdir, upperdir);
857 }
858 return ok;
859}
860
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100861static unsigned int ovl_split_lowerdirs(char *str)
862{
863 unsigned int ctr = 1;
864 char *s, *d;
865
866 for (s = d = str;; s++, d++) {
867 if (*s == '\\') {
868 s++;
869 } else if (*s == ':') {
870 *d = '\0';
871 ctr++;
872 continue;
873 }
874 *d = *s;
875 if (!*s)
876 break;
877 }
878 return ctr;
879}
880
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200881static int ovl_fill_super(struct super_block *sb, void *data, int silent)
882{
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100883 struct path upperpath = { NULL, NULL };
884 struct path workpath = { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200885 struct dentry *root_dentry;
886 struct ovl_entry *oe;
887 struct ovl_fs *ufs;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100888 struct path *stack = NULL;
889 char *lowertmp;
890 char *lower;
891 unsigned int numlower;
892 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100893 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200894 bool remote = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200895 int err;
896
Erez Zadokf45827e82014-10-24 00:14:38 +0200897 err = -ENOMEM;
898 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
899 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200900 goto out;
901
Erez Zadokf45827e82014-10-24 00:14:38 +0200902 err = ovl_parse_opt((char *) data, &ufs->config);
903 if (err)
904 goto out_free_config;
905
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200906 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100907 if (!ufs->config.lowerdir) {
908 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200909 goto out_free_config;
910 }
911
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100912 sb->s_stack_depth = 0;
913 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100914 if (!ufs->config.workdir) {
915 pr_err("overlayfs: missing 'workdir'\n");
916 goto out_free_config;
917 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200918
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100919 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
920 if (err)
921 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100922
hujianyang71cbad72015-01-15 13:20:57 +0800923 /* Upper fs should not be r/o */
924 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
925 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
926 err = -EINVAL;
927 goto out_put_upperpath;
928 }
929
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100930 err = ovl_mount_dir(ufs->config.workdir, &workpath);
931 if (err)
932 goto out_put_upperpath;
933
hujianyang2f83fd82015-01-06 12:52:13 +0800934 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100935 if (upperpath.mnt != workpath.mnt) {
936 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
937 goto out_put_workpath;
938 }
939 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
940 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
941 goto out_put_workpath;
942 }
943 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100944 }
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100945 err = -ENOMEM;
946 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
947 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100948 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200949
950 err = -EINVAL;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100951 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +0800952 if (stacklen > OVL_MAX_STACK) {
953 pr_err("overlayfs: too many lower directries, limit is %d\n",
954 OVL_MAX_STACK);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100955 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +0800956 } else if (!ufs->config.upperdir && stacklen == 1) {
957 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
958 goto out_free_lowertmp;
959 }
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100960
961 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
962 if (!stack)
963 goto out_free_lowertmp;
964
965 lower = lowertmp;
966 for (numlower = 0; numlower < stacklen; numlower++) {
967 err = ovl_lower_dir(lower, &stack[numlower],
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200968 &ufs->lower_namelen, &sb->s_stack_depth,
969 &remote);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100970 if (err)
971 goto out_put_lowerpath;
972
973 lower = strchr(lower, '\0') + 1;
974 }
975
976 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100977 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200978 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
979 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100980 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200981 }
982
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100983 if (ufs->config.upperdir) {
984 ufs->upper_mnt = clone_private_mount(&upperpath);
985 err = PTR_ERR(ufs->upper_mnt);
986 if (IS_ERR(ufs->upper_mnt)) {
987 pr_err("overlayfs: failed to clone upperpath\n");
988 goto out_put_lowerpath;
989 }
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100990
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100991 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
992 err = PTR_ERR(ufs->workdir);
993 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200994 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
995 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
996 sb->s_flags |= MS_RDONLY;
997 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100998 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200999 }
1000
hujianyang2f83fd82015-01-06 12:52:13 +08001001 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001002 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001003 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001004 goto out_put_workdir;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001005 for (i = 0; i < numlower; i++) {
1006 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001007
hujianyang2f83fd82015-01-06 12:52:13 +08001008 err = PTR_ERR(mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001009 if (IS_ERR(mnt)) {
1010 pr_err("overlayfs: failed to clone lowerpath\n");
1011 goto out_put_lower_mnt;
1012 }
1013 /*
1014 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1015 * will fail instead of modifying lower fs.
1016 */
1017 mnt->mnt_flags |= MNT_READONLY;
1018
1019 ufs->lower_mnt[ufs->numlower] = mnt;
1020 ufs->numlower++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001021 }
1022
hujianyang71cbad72015-01-15 13:20:57 +08001023 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1024 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001025 sb->s_flags |= MS_RDONLY;
1026
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001027 if (remote)
1028 sb->s_d_op = &ovl_reval_dentry_operations;
1029 else
1030 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001031
1032 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001033 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001034 if (!oe)
1035 goto out_put_lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001036
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001037 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001038 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001039 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001040
1041 mntput(upperpath.mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001042 for (i = 0; i < numlower; i++)
1043 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001044 path_put(&workpath);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001045 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001046
1047 oe->__upperdentry = upperpath.dentry;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001048 for (i = 0; i < numlower; i++) {
1049 oe->lowerstack[i].dentry = stack[i].dentry;
1050 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1051 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001052 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001053
1054 root_dentry->d_fsdata = oe;
1055
Andy Whitcroftcc259632014-10-24 00:14:38 +02001056 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001057 sb->s_op = &ovl_super_operations;
1058 sb->s_root = root_dentry;
1059 sb->s_fs_info = ufs;
1060
1061 return 0;
1062
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001063out_free_oe:
1064 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001065out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001066 for (i = 0; i < ufs->numlower; i++)
1067 mntput(ufs->lower_mnt[i]);
1068 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001069out_put_workdir:
1070 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001071 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001072out_put_lowerpath:
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001073 for (i = 0; i < numlower; i++)
1074 path_put(&stack[i]);
1075 kfree(stack);
1076out_free_lowertmp:
1077 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001078out_put_workpath:
1079 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001080out_put_upperpath:
1081 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001082out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001083 kfree(ufs->config.lowerdir);
1084 kfree(ufs->config.upperdir);
1085 kfree(ufs->config.workdir);
1086 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001087out:
1088 return err;
1089}
1090
1091static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1092 const char *dev_name, void *raw_data)
1093{
1094 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1095}
1096
1097static struct file_system_type ovl_fs_type = {
1098 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001099 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001100 .mount = ovl_mount,
1101 .kill_sb = kill_anon_super,
1102};
Miklos Szeredief94b182014-11-20 16:39:59 +01001103MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001104
1105static int __init ovl_init(void)
1106{
1107 return register_filesystem(&ovl_fs_type);
1108}
1109
1110static void __exit ovl_exit(void)
1111{
1112 unregister_filesystem(&ovl_fs_type);
1113}
1114
1115module_init(ovl_init);
1116module_exit(ovl_exit);