]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/overlayfs/super.c
ovl: use path_put_init() in error paths for ovl_fill_super()
[thirdparty/kernel/linux.git] / fs / overlayfs / super.c
CommitLineData
e9be9d5e
MS
1/*
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
5b825c3a 10#include <uapi/linux/magic.h>
e9be9d5e
MS
11#include <linux/fs.h>
12#include <linux/namei.h>
13#include <linux/xattr.h>
e9be9d5e 14#include <linux/mount.h>
e9be9d5e
MS
15#include <linux/parser.h>
16#include <linux/module.h>
cc259639 17#include <linux/statfs.h>
f45827e8 18#include <linux/seq_file.h>
d837a49b 19#include <linux/posix_acl_xattr.h>
e9be9d5e
MS
20#include "overlayfs.h"
21
22MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23MODULE_DESCRIPTION("Overlay filesystem");
24MODULE_LICENSE("GPL");
25
e9be9d5e
MS
26
27struct ovl_dir_cache;
28
a78d9f0d
MS
29#define OVL_MAX_STACK 500
30
688ea0e5
MS
31static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
32module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
33MODULE_PARM_DESC(ovl_redirect_dir_def,
34 "Default to on or off for the redirect_dir feature");
e9be9d5e 35
02bcd157
AG
36static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
37module_param_named(index, ovl_index_def, bool, 0644);
38MODULE_PARM_DESC(ovl_index_def,
39 "Default to on or off for the inodes index feature");
40
e9be9d5e
MS
41static void ovl_dentry_release(struct dentry *dentry)
42{
43 struct ovl_entry *oe = dentry->d_fsdata;
44
45 if (oe) {
dd662667
MS
46 unsigned int i;
47
dd662667
MS
48 for (i = 0; i < oe->numlower; i++)
49 dput(oe->lowerstack[i].dentry);
e9be9d5e
MS
50 kfree_rcu(oe, rcu);
51 }
52}
53
b0990fbb
AG
54static int ovl_check_append_only(struct inode *inode, int flag)
55{
56 /*
57 * This test was moot in vfs may_open() because overlay inode does
58 * not have the S_APPEND flag, so re-check on real upper inode
59 */
60 if (IS_APPEND(inode)) {
61 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
62 return -EPERM;
63 if (flag & O_TRUNC)
64 return -EPERM;
65 }
66
67 return 0;
68}
69
2d902671
MS
70static struct dentry *ovl_d_real(struct dentry *dentry,
71 const struct inode *inode,
495e6429 72 unsigned int open_flags, unsigned int flags)
d101a125
MS
73{
74 struct dentry *real;
b0990fbb 75 int err;
d101a125 76
cd91304e
MS
77 if (flags & D_REAL_UPPER)
78 return ovl_dentry_upper(dentry);
79
ca4c8a3a 80 if (!d_is_reg(dentry)) {
d101a125
MS
81 if (!inode || inode == d_inode(dentry))
82 return dentry;
83 goto bug;
84 }
85
2d902671 86 if (open_flags) {
b0990fbb 87 err = ovl_open_maybe_copy_up(dentry, open_flags);
2d902671
MS
88 if (err)
89 return ERR_PTR(err);
90 }
91
d101a125 92 real = ovl_dentry_upper(dentry);
b0990fbb
AG
93 if (real && (!inode || inode == d_inode(real))) {
94 if (!inode) {
95 err = ovl_check_append_only(d_inode(real), open_flags);
96 if (err)
97 return ERR_PTR(err);
98 }
d101a125 99 return real;
b0990fbb 100 }
d101a125
MS
101
102 real = ovl_dentry_lower(dentry);
103 if (!real)
104 goto bug;
105
c4fcfc16 106 /* Handle recursion */
495e6429 107 real = d_real(real, inode, open_flags, 0);
c4fcfc16 108
d101a125
MS
109 if (!inode || inode == d_inode(real))
110 return real;
d101a125 111bug:
656189d2 112 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
d101a125
MS
113 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
114 return dentry;
115}
116
7c03b5d4
MS
117static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
118{
119 struct ovl_entry *oe = dentry->d_fsdata;
120 unsigned int i;
121 int ret = 1;
122
123 for (i = 0; i < oe->numlower; i++) {
124 struct dentry *d = oe->lowerstack[i].dentry;
125
126 if (d->d_flags & DCACHE_OP_REVALIDATE) {
127 ret = d->d_op->d_revalidate(d, flags);
128 if (ret < 0)
129 return ret;
130 if (!ret) {
131 if (!(flags & LOOKUP_RCU))
132 d_invalidate(d);
133 return -ESTALE;
134 }
135 }
136 }
137 return 1;
138}
139
140static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
141{
142 struct ovl_entry *oe = dentry->d_fsdata;
143 unsigned int i;
144 int ret = 1;
145
146 for (i = 0; i < oe->numlower; i++) {
147 struct dentry *d = oe->lowerstack[i].dentry;
148
149 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
150 ret = d->d_op->d_weak_revalidate(d, flags);
151 if (ret <= 0)
152 break;
153 }
154 }
155 return ret;
156}
157
e9be9d5e
MS
158static const struct dentry_operations ovl_dentry_operations = {
159 .d_release = ovl_dentry_release,
d101a125 160 .d_real = ovl_d_real,
e9be9d5e
MS
161};
162
7c03b5d4
MS
163static const struct dentry_operations ovl_reval_dentry_operations = {
164 .d_release = ovl_dentry_release,
d101a125 165 .d_real = ovl_d_real,
7c03b5d4
MS
166 .d_revalidate = ovl_dentry_revalidate,
167 .d_weak_revalidate = ovl_dentry_weak_revalidate,
168};
169
13cf199d
AG
170static struct kmem_cache *ovl_inode_cachep;
171
172static struct inode *ovl_alloc_inode(struct super_block *sb)
173{
174 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
175
b3885bd6
HN
176 if (!oi)
177 return NULL;
178
04a01ac7 179 oi->cache = NULL;
cf31c463 180 oi->redirect = NULL;
04a01ac7 181 oi->version = 0;
13c72075 182 oi->flags = 0;
09d8b586 183 oi->__upperdentry = NULL;
25b7713a 184 oi->lower = NULL;
a015dafc 185 mutex_init(&oi->lock);
25b7713a 186
13cf199d
AG
187 return &oi->vfs_inode;
188}
189
190static void ovl_i_callback(struct rcu_head *head)
191{
192 struct inode *inode = container_of(head, struct inode, i_rcu);
193
194 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
195}
196
197static void ovl_destroy_inode(struct inode *inode)
198{
09d8b586
MS
199 struct ovl_inode *oi = OVL_I(inode);
200
201 dput(oi->__upperdentry);
cf31c463 202 kfree(oi->redirect);
4edb83bb 203 ovl_dir_cache_free(inode);
a015dafc 204 mutex_destroy(&oi->lock);
09d8b586 205
13cf199d
AG
206 call_rcu(&inode->i_rcu, ovl_i_callback);
207}
208
e9be9d5e
MS
209static void ovl_put_super(struct super_block *sb)
210{
211 struct ovl_fs *ufs = sb->s_fs_info;
dd662667 212 unsigned i;
e9be9d5e 213
02bcd157 214 dput(ufs->indexdir);
e9be9d5e 215 dput(ufs->workdir);
85fdee1e
AG
216 if (ufs->workdir_locked)
217 ovl_inuse_unlock(ufs->workbasedir);
2cac0c00 218 dput(ufs->workbasedir);
85fdee1e 219 if (ufs->upper_mnt && ufs->upperdir_locked)
2cac0c00 220 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
e9be9d5e 221 mntput(ufs->upper_mnt);
2a9c6d06 222 for (i = 0; i < ufs->numlower; i++) {
b9343632 223 mntput(ufs->lower_layers[i].mnt);
2a9c6d06
CR
224 free_anon_bdev(ufs->lower_layers[i].pseudo_dev);
225 }
b9343632 226 kfree(ufs->lower_layers);
e9be9d5e 227
f45827e8
EZ
228 kfree(ufs->config.lowerdir);
229 kfree(ufs->config.upperdir);
230 kfree(ufs->config.workdir);
3fe6e52f 231 put_cred(ufs->creator_cred);
e9be9d5e
MS
232 kfree(ufs);
233}
234
e593b2bf
AG
235static int ovl_sync_fs(struct super_block *sb, int wait)
236{
237 struct ovl_fs *ufs = sb->s_fs_info;
238 struct super_block *upper_sb;
239 int ret;
240
241 if (!ufs->upper_mnt)
242 return 0;
243 upper_sb = ufs->upper_mnt->mnt_sb;
244 if (!upper_sb->s_op->sync_fs)
245 return 0;
246
247 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
248 down_read(&upper_sb->s_umount);
249 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
250 up_read(&upper_sb->s_umount);
251 return ret;
252}
253
cc259639
AW
254/**
255 * ovl_statfs
256 * @sb: The overlayfs super block
257 * @buf: The struct kstatfs to fill in with stats
258 *
259 * Get the filesystem statistics. As writes always target the upper layer
4ebc5818 260 * filesystem pass the statfs to the upper filesystem (if it exists)
cc259639
AW
261 */
262static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
263{
264 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
265 struct dentry *root_dentry = dentry->d_sb->s_root;
266 struct path path;
267 int err;
268
4ebc5818 269 ovl_path_real(root_dentry, &path);
cc259639
AW
270
271 err = vfs_statfs(&path, buf);
272 if (!err) {
6b2d5fe4 273 buf->f_namelen = ofs->namelen;
cc259639
AW
274 buf->f_type = OVERLAYFS_SUPER_MAGIC;
275 }
276
277 return err;
278}
279
02bcd157
AG
280/* Will this overlay be forced to mount/remount ro? */
281static bool ovl_force_readonly(struct ovl_fs *ufs)
282{
283 return (!ufs->upper_mnt || !ufs->workdir);
284}
285
f45827e8
EZ
286/**
287 * ovl_show_options
288 *
289 * Prints the mount options for a given superblock.
290 * Returns zero; does not fail.
291 */
292static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
293{
294 struct super_block *sb = dentry->d_sb;
295 struct ovl_fs *ufs = sb->s_fs_info;
296
a068acf2 297 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
53a08cb9 298 if (ufs->config.upperdir) {
a068acf2
KC
299 seq_show_option(m, "upperdir", ufs->config.upperdir);
300 seq_show_option(m, "workdir", ufs->config.workdir);
53a08cb9 301 }
8d3095f4
MS
302 if (ufs->config.default_permissions)
303 seq_puts(m, ",default_permissions");
c5bef3a7
AG
304 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
305 seq_printf(m, ",redirect_dir=%s",
306 ufs->config.redirect_dir ? "on" : "off");
02bcd157
AG
307 if (ufs->config.index != ovl_index_def)
308 seq_printf(m, ",index=%s",
309 ufs->config.index ? "on" : "off");
f45827e8
EZ
310 return 0;
311}
312
3cdf6fe9
SL
313static int ovl_remount(struct super_block *sb, int *flags, char *data)
314{
315 struct ovl_fs *ufs = sb->s_fs_info;
316
02bcd157 317 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
3cdf6fe9
SL
318 return -EROFS;
319
320 return 0;
321}
322
e9be9d5e 323static const struct super_operations ovl_super_operations = {
13cf199d
AG
324 .alloc_inode = ovl_alloc_inode,
325 .destroy_inode = ovl_destroy_inode,
326 .drop_inode = generic_delete_inode,
e9be9d5e 327 .put_super = ovl_put_super,
e593b2bf 328 .sync_fs = ovl_sync_fs,
cc259639 329 .statfs = ovl_statfs,
f45827e8 330 .show_options = ovl_show_options,
3cdf6fe9 331 .remount_fs = ovl_remount,
e9be9d5e
MS
332};
333
334enum {
335 OPT_LOWERDIR,
336 OPT_UPPERDIR,
337 OPT_WORKDIR,
8d3095f4 338 OPT_DEFAULT_PERMISSIONS,
a6c60655
MS
339 OPT_REDIRECT_DIR_ON,
340 OPT_REDIRECT_DIR_OFF,
02bcd157
AG
341 OPT_INDEX_ON,
342 OPT_INDEX_OFF,
e9be9d5e
MS
343 OPT_ERR,
344};
345
346static const match_table_t ovl_tokens = {
347 {OPT_LOWERDIR, "lowerdir=%s"},
348 {OPT_UPPERDIR, "upperdir=%s"},
349 {OPT_WORKDIR, "workdir=%s"},
8d3095f4 350 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
a6c60655
MS
351 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
352 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
02bcd157
AG
353 {OPT_INDEX_ON, "index=on"},
354 {OPT_INDEX_OFF, "index=off"},
e9be9d5e
MS
355 {OPT_ERR, NULL}
356};
357
91c77947
MS
358static char *ovl_next_opt(char **s)
359{
360 char *sbegin = *s;
361 char *p;
362
363 if (sbegin == NULL)
364 return NULL;
365
366 for (p = sbegin; *p; p++) {
367 if (*p == '\\') {
368 p++;
369 if (!*p)
370 break;
371 } else if (*p == ',') {
372 *p = '\0';
373 *s = p + 1;
374 return sbegin;
375 }
376 }
377 *s = NULL;
378 return sbegin;
379}
380
e9be9d5e
MS
381static int ovl_parse_opt(char *opt, struct ovl_config *config)
382{
383 char *p;
384
91c77947 385 while ((p = ovl_next_opt(&opt)) != NULL) {
e9be9d5e
MS
386 int token;
387 substring_t args[MAX_OPT_ARGS];
388
389 if (!*p)
390 continue;
391
392 token = match_token(p, ovl_tokens, args);
393 switch (token) {
394 case OPT_UPPERDIR:
395 kfree(config->upperdir);
396 config->upperdir = match_strdup(&args[0]);
397 if (!config->upperdir)
398 return -ENOMEM;
399 break;
400
401 case OPT_LOWERDIR:
402 kfree(config->lowerdir);
403 config->lowerdir = match_strdup(&args[0]);
404 if (!config->lowerdir)
405 return -ENOMEM;
406 break;
407
408 case OPT_WORKDIR:
409 kfree(config->workdir);
410 config->workdir = match_strdup(&args[0]);
411 if (!config->workdir)
412 return -ENOMEM;
413 break;
414
8d3095f4
MS
415 case OPT_DEFAULT_PERMISSIONS:
416 config->default_permissions = true;
417 break;
418
a6c60655
MS
419 case OPT_REDIRECT_DIR_ON:
420 config->redirect_dir = true;
421 break;
422
423 case OPT_REDIRECT_DIR_OFF:
424 config->redirect_dir = false;
425 break;
426
02bcd157
AG
427 case OPT_INDEX_ON:
428 config->index = true;
429 break;
430
431 case OPT_INDEX_OFF:
432 config->index = false;
433 break;
434
e9be9d5e 435 default:
bead55ef 436 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
e9be9d5e
MS
437 return -EINVAL;
438 }
439 }
71cbad7e 440
441 /* Workdir is useless in non-upper mount */
442 if (!config->upperdir && config->workdir) {
443 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
444 config->workdir);
445 kfree(config->workdir);
446 config->workdir = NULL;
447 }
448
e9be9d5e
MS
449 return 0;
450}
451
452#define OVL_WORKDIR_NAME "work"
02bcd157 453#define OVL_INDEXDIR_NAME "index"
e9be9d5e 454
6b8aa129
AG
455static struct dentry *ovl_workdir_create(struct super_block *sb,
456 struct ovl_fs *ufs,
457 struct dentry *dentry,
458 const char *name, bool persist)
e9be9d5e
MS
459{
460 struct inode *dir = dentry->d_inode;
6b8aa129 461 struct vfsmount *mnt = ufs->upper_mnt;
e9be9d5e
MS
462 struct dentry *work;
463 int err;
464 bool retried = false;
6b8aa129 465 bool locked = false;
e9be9d5e
MS
466
467 err = mnt_want_write(mnt);
468 if (err)
6b8aa129 469 goto out_err;
e9be9d5e 470
5955102c 471 inode_lock_nested(dir, I_MUTEX_PARENT);
6b8aa129
AG
472 locked = true;
473
e9be9d5e 474retry:
6b8aa129 475 work = lookup_one_len(name, dentry, strlen(name));
e9be9d5e
MS
476
477 if (!IS_ERR(work)) {
c11b9fdd
MS
478 struct iattr attr = {
479 .ia_valid = ATTR_MODE,
32a3d848 480 .ia_mode = S_IFDIR | 0,
c11b9fdd 481 };
e9be9d5e
MS
482
483 if (work->d_inode) {
484 err = -EEXIST;
485 if (retried)
486 goto out_dput;
487
6b8aa129
AG
488 if (persist)
489 goto out_unlock;
490
e9be9d5e 491 retried = true;
eea2fb48 492 ovl_workdir_cleanup(dir, mnt, work, 0);
e9be9d5e
MS
493 dput(work);
494 goto retry;
495 }
496
32a3d848
AV
497 err = ovl_create_real(dir, work,
498 &(struct cattr){.mode = S_IFDIR | 0},
499 NULL, true);
e9be9d5e
MS
500 if (err)
501 goto out_dput;
c11b9fdd 502
cb348edb
MS
503 /*
504 * Try to remove POSIX ACL xattrs from workdir. We are good if:
505 *
506 * a) success (there was a POSIX ACL xattr and was removed)
507 * b) -ENODATA (there was no POSIX ACL xattr)
508 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
509 *
510 * There are various other error values that could effectively
511 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
512 * if the xattr name is too long), but the set of filesystems
513 * allowed as upper are limited to "normal" ones, where checking
514 * for the above two errors is sufficient.
515 */
c11b9fdd 516 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
e1ff3dd1 517 if (err && err != -ENODATA && err != -EOPNOTSUPP)
c11b9fdd
MS
518 goto out_dput;
519
520 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
e1ff3dd1 521 if (err && err != -ENODATA && err != -EOPNOTSUPP)
c11b9fdd
MS
522 goto out_dput;
523
524 /* Clear any inherited mode bits */
525 inode_lock(work->d_inode);
526 err = notify_change(work, &attr, NULL);
527 inode_unlock(work->d_inode);
528 if (err)
529 goto out_dput;
6b8aa129
AG
530 } else {
531 err = PTR_ERR(work);
532 goto out_err;
e9be9d5e
MS
533 }
534out_unlock:
e9be9d5e 535 mnt_drop_write(mnt);
6b8aa129
AG
536 if (locked)
537 inode_unlock(dir);
e9be9d5e
MS
538
539 return work;
540
541out_dput:
542 dput(work);
6b8aa129
AG
543out_err:
544 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
545 ufs->config.workdir, name, -err);
546 sb->s_flags |= MS_RDONLY;
547 work = NULL;
e9be9d5e
MS
548 goto out_unlock;
549}
550
91c77947
MS
551static void ovl_unescape(char *s)
552{
553 char *d = s;
554
555 for (;; s++, d++) {
556 if (*s == '\\')
557 s++;
558 *d = *s;
559 if (!*s)
560 break;
561 }
562}
563
ab508822
MS
564static int ovl_mount_dir_noesc(const char *name, struct path *path)
565{
a78d9f0d 566 int err = -EINVAL;
ab508822 567
a78d9f0d
MS
568 if (!*name) {
569 pr_err("overlayfs: empty lowerdir\n");
570 goto out;
571 }
ab508822
MS
572 err = kern_path(name, LOOKUP_FOLLOW, path);
573 if (err) {
574 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
575 goto out;
576 }
577 err = -EINVAL;
7c03b5d4 578 if (ovl_dentry_weird(path->dentry)) {
ab508822
MS
579 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
580 goto out_put;
581 }
2b8c30e9 582 if (!d_is_dir(path->dentry)) {
ab508822
MS
583 pr_err("overlayfs: '%s' not a directory\n", name);
584 goto out_put;
585 }
586 return 0;
587
588out_put:
8aafcb59 589 path_put_init(path);
ab508822
MS
590out:
591 return err;
592}
593
594static int ovl_mount_dir(const char *name, struct path *path)
595{
596 int err = -ENOMEM;
597 char *tmp = kstrdup(name, GFP_KERNEL);
598
599 if (tmp) {
600 ovl_unescape(tmp);
601 err = ovl_mount_dir_noesc(tmp, path);
7c03b5d4
MS
602
603 if (!err)
604 if (ovl_dentry_remote(path->dentry)) {
605 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
606 tmp);
8aafcb59 607 path_put_init(path);
7c03b5d4
MS
608 err = -EINVAL;
609 }
ab508822
MS
610 kfree(tmp);
611 }
612 return err;
613}
614
6b2d5fe4
MS
615static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
616 const char *name)
ab508822 617{
ab508822 618 struct kstatfs statfs;
6b2d5fe4
MS
619 int err = vfs_statfs(path, &statfs);
620
621 if (err)
622 pr_err("overlayfs: statfs failed on '%s'\n", name);
623 else
624 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
625
626 return err;
627}
628
629static int ovl_lower_dir(const char *name, struct path *path,
630 struct ovl_fs *ofs, int *stack_depth, bool *remote)
631{
632 int err;
ab508822 633
a78d9f0d 634 err = ovl_mount_dir_noesc(name, path);
ab508822
MS
635 if (err)
636 goto out;
637
6b2d5fe4
MS
638 err = ovl_check_namelen(path, ofs, name);
639 if (err)
ab508822 640 goto out_put;
6b2d5fe4 641
ab508822
MS
642 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
643
7c03b5d4
MS
644 if (ovl_dentry_remote(path->dentry))
645 *remote = true;
646
02bcd157
AG
647 /*
648 * The inodes index feature needs to encode and decode file
649 * handles, so it requires that all layers support them.
650 */
651 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
652 ofs->config.index = false;
653 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
654 }
655
ab508822
MS
656 return 0;
657
658out_put:
8aafcb59 659 path_put_init(path);
ab508822
MS
660out:
661 return err;
662}
663
e9be9d5e
MS
664/* Workdir should not be subdir of upperdir and vice versa */
665static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
666{
667 bool ok = false;
668
669 if (workdir != upperdir) {
670 ok = (lock_rename(workdir, upperdir) == NULL);
671 unlock_rename(workdir, upperdir);
672 }
673 return ok;
674}
675
a78d9f0d
MS
676static unsigned int ovl_split_lowerdirs(char *str)
677{
678 unsigned int ctr = 1;
679 char *s, *d;
680
681 for (s = d = str;; s++, d++) {
682 if (*s == '\\') {
683 s++;
684 } else if (*s == ':') {
685 *d = '\0';
686 ctr++;
687 continue;
688 }
689 *d = *s;
690 if (!*s)
691 break;
692 }
693 return ctr;
694}
695
0eb45fc3
AG
696static int __maybe_unused
697ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
698 struct dentry *dentry, struct inode *inode,
699 const char *name, void *buffer, size_t size)
700{
1d88f183 701 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
0eb45fc3
AG
702}
703
0c97be22
AG
704static int __maybe_unused
705ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
706 struct dentry *dentry, struct inode *inode,
707 const char *name, const void *value,
708 size_t size, int flags)
d837a49b
MS
709{
710 struct dentry *workdir = ovl_workdir(dentry);
09d8b586 711 struct inode *realinode = ovl_inode_real(inode);
d837a49b
MS
712 struct posix_acl *acl = NULL;
713 int err;
714
715 /* Check that everything is OK before copy-up */
716 if (value) {
717 acl = posix_acl_from_xattr(&init_user_ns, value, size);
718 if (IS_ERR(acl))
719 return PTR_ERR(acl);
720 }
721 err = -EOPNOTSUPP;
722 if (!IS_POSIXACL(d_inode(workdir)))
723 goto out_acl_release;
724 if (!realinode->i_op->set_acl)
725 goto out_acl_release;
726 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
727 err = acl ? -EACCES : 0;
728 goto out_acl_release;
729 }
730 err = -EPERM;
731 if (!inode_owner_or_capable(inode))
732 goto out_acl_release;
733
734 posix_acl_release(acl);
735
fd3220d3
MS
736 /*
737 * Check if sgid bit needs to be cleared (actual setacl operation will
738 * be done with mounter's capabilities and so that won't do it for us).
739 */
740 if (unlikely(inode->i_mode & S_ISGID) &&
741 handler->flags == ACL_TYPE_ACCESS &&
742 !in_group_p(inode->i_gid) &&
743 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
744 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
745
746 err = ovl_setattr(dentry, &iattr);
747 if (err)
748 return err;
749 }
750
1d88f183 751 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
ce31513a 752 if (!err)
09d8b586 753 ovl_copyattr(ovl_inode_real(inode), inode);
ce31513a
MS
754
755 return err;
d837a49b
MS
756
757out_acl_release:
758 posix_acl_release(acl);
759 return err;
760}
761
0eb45fc3
AG
762static int ovl_own_xattr_get(const struct xattr_handler *handler,
763 struct dentry *dentry, struct inode *inode,
764 const char *name, void *buffer, size_t size)
765{
48fab5d7 766 return -EOPNOTSUPP;
0eb45fc3
AG
767}
768
d837a49b
MS
769static int ovl_own_xattr_set(const struct xattr_handler *handler,
770 struct dentry *dentry, struct inode *inode,
771 const char *name, const void *value,
772 size_t size, int flags)
773{
48fab5d7 774 return -EOPNOTSUPP;
d837a49b
MS
775}
776
0eb45fc3
AG
777static int ovl_other_xattr_get(const struct xattr_handler *handler,
778 struct dentry *dentry, struct inode *inode,
779 const char *name, void *buffer, size_t size)
780{
1d88f183 781 return ovl_xattr_get(dentry, inode, name, buffer, size);
0eb45fc3
AG
782}
783
0e585ccc
AG
784static int ovl_other_xattr_set(const struct xattr_handler *handler,
785 struct dentry *dentry, struct inode *inode,
786 const char *name, const void *value,
787 size_t size, int flags)
788{
1d88f183 789 return ovl_xattr_set(dentry, inode, name, value, size, flags);
0e585ccc
AG
790}
791
0c97be22
AG
792static const struct xattr_handler __maybe_unused
793ovl_posix_acl_access_xattr_handler = {
d837a49b
MS
794 .name = XATTR_NAME_POSIX_ACL_ACCESS,
795 .flags = ACL_TYPE_ACCESS,
0eb45fc3 796 .get = ovl_posix_acl_xattr_get,
d837a49b
MS
797 .set = ovl_posix_acl_xattr_set,
798};
799
0c97be22
AG
800static const struct xattr_handler __maybe_unused
801ovl_posix_acl_default_xattr_handler = {
d837a49b
MS
802 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
803 .flags = ACL_TYPE_DEFAULT,
0eb45fc3 804 .get = ovl_posix_acl_xattr_get,
d837a49b
MS
805 .set = ovl_posix_acl_xattr_set,
806};
807
808static const struct xattr_handler ovl_own_xattr_handler = {
809 .prefix = OVL_XATTR_PREFIX,
0eb45fc3 810 .get = ovl_own_xattr_get,
d837a49b
MS
811 .set = ovl_own_xattr_set,
812};
813
814static const struct xattr_handler ovl_other_xattr_handler = {
815 .prefix = "", /* catch all */
0eb45fc3 816 .get = ovl_other_xattr_get,
d837a49b
MS
817 .set = ovl_other_xattr_set,
818};
819
820static const struct xattr_handler *ovl_xattr_handlers[] = {
0c97be22 821#ifdef CONFIG_FS_POSIX_ACL
d837a49b
MS
822 &ovl_posix_acl_access_xattr_handler,
823 &ovl_posix_acl_default_xattr_handler,
0c97be22 824#endif
d837a49b
MS
825 &ovl_own_xattr_handler,
826 &ovl_other_xattr_handler,
827 NULL
828};
829
e9be9d5e
MS
830static int ovl_fill_super(struct super_block *sb, void *data, int silent)
831{
33006cdf
KC
832 struct path upperpath = { };
833 struct path workpath = { };
e9be9d5e
MS
834 struct dentry *root_dentry;
835 struct ovl_entry *oe;
836 struct ovl_fs *ufs;
a78d9f0d
MS
837 struct path *stack = NULL;
838 char *lowertmp;
839 char *lower;
840 unsigned int numlower;
841 unsigned int stacklen = 0;
dd662667 842 unsigned int i;
7c03b5d4 843 bool remote = false;
51f8f3c4 844 struct cred *cred;
e9be9d5e
MS
845 int err;
846
f45827e8
EZ
847 err = -ENOMEM;
848 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
849 if (!ufs)
e9be9d5e
MS
850 goto out;
851
688ea0e5 852 ufs->config.redirect_dir = ovl_redirect_dir_def;
02bcd157 853 ufs->config.index = ovl_index_def;
f45827e8
EZ
854 err = ovl_parse_opt((char *) data, &ufs->config);
855 if (err)
856 goto out_free_config;
857
e9be9d5e 858 err = -EINVAL;
53a08cb9 859 if (!ufs->config.lowerdir) {
07f2af7b
KK
860 if (!silent)
861 pr_err("overlayfs: missing 'lowerdir'\n");
e9be9d5e
MS
862 goto out_free_config;
863 }
864
53a08cb9 865 sb->s_stack_depth = 0;
cf9a6784 866 sb->s_maxbytes = MAX_LFS_FILESIZE;
53a08cb9 867 if (ufs->config.upperdir) {
53a08cb9
MS
868 if (!ufs->config.workdir) {
869 pr_err("overlayfs: missing 'workdir'\n");
870 goto out_free_config;
871 }
e9be9d5e 872
53a08cb9
MS
873 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
874 if (err)
875 goto out_free_config;
e9be9d5e 876
71cbad7e 877 /* Upper fs should not be r/o */
bc98a42c 878 if (sb_rdonly(upperpath.mnt->mnt_sb)) {
71cbad7e 879 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
880 err = -EINVAL;
881 goto out_put_upperpath;
882 }
883
6b2d5fe4
MS
884 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
885 if (err)
886 goto out_put_upperpath;
887
2cac0c00 888 err = -EBUSY;
85fdee1e
AG
889 if (ovl_inuse_trylock(upperpath.dentry)) {
890 ufs->upperdir_locked = true;
891 } else if (ufs->config.index) {
892 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
2cac0c00 893 goto out_put_upperpath;
85fdee1e
AG
894 } else {
895 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
2cac0c00
AG
896 }
897
53a08cb9
MS
898 err = ovl_mount_dir(ufs->config.workdir, &workpath);
899 if (err)
2cac0c00 900 goto out_unlock_upperdentry;
53a08cb9 901
2f83fd8c 902 err = -EINVAL;
53a08cb9
MS
903 if (upperpath.mnt != workpath.mnt) {
904 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
905 goto out_put_workpath;
906 }
907 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
908 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
909 goto out_put_workpath;
910 }
2cac0c00
AG
911
912 err = -EBUSY;
85fdee1e
AG
913 if (ovl_inuse_trylock(workpath.dentry)) {
914 ufs->workdir_locked = true;
915 } else if (ufs->config.index) {
916 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
2cac0c00 917 goto out_put_workpath;
85fdee1e
AG
918 } else {
919 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
2cac0c00
AG
920 }
921
922 ufs->workbasedir = workpath.dentry;
53a08cb9 923 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
cc259639 924 }
a78d9f0d
MS
925 err = -ENOMEM;
926 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
927 if (!lowertmp)
2cac0c00 928 goto out_unlock_workdentry;
69c433ed 929
a78d9f0d
MS
930 err = -EINVAL;
931 stacklen = ovl_split_lowerdirs(lowertmp);
6be4506e 932 if (stacklen > OVL_MAX_STACK) {
fd36570a 933 pr_err("overlayfs: too many lower directories, limit is %d\n",
6be4506e 934 OVL_MAX_STACK);
a78d9f0d 935 goto out_free_lowertmp;
6be4506e 936 } else if (!ufs->config.upperdir && stacklen == 1) {
937 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
938 goto out_free_lowertmp;
939 }
a78d9f0d 940
313684c4 941 err = -ENOMEM;
a78d9f0d
MS
942 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
943 if (!stack)
944 goto out_free_lowertmp;
945
313684c4 946 err = -EINVAL;
a78d9f0d
MS
947 lower = lowertmp;
948 for (numlower = 0; numlower < stacklen; numlower++) {
6b2d5fe4
MS
949 err = ovl_lower_dir(lower, &stack[numlower], ufs,
950 &sb->s_stack_depth, &remote);
a78d9f0d
MS
951 if (err)
952 goto out_put_lowerpath;
953
954 lower = strchr(lower, '\0') + 1;
955 }
956
69c433ed 957 err = -EINVAL;
ab508822 958 sb->s_stack_depth++;
69c433ed
MS
959 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
960 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
3b7a9a24 961 goto out_put_lowerpath;
69c433ed
MS
962 }
963
53a08cb9
MS
964 if (ufs->config.upperdir) {
965 ufs->upper_mnt = clone_private_mount(&upperpath);
966 err = PTR_ERR(ufs->upper_mnt);
967 if (IS_ERR(ufs->upper_mnt)) {
968 pr_err("overlayfs: failed to clone upperpath\n");
969 goto out_put_lowerpath;
970 }
2cac0c00 971
d719e8f2
MS
972 /* Don't inherit atime flags */
973 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
974
975 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
3b7a9a24 976
6b8aa129
AG
977 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
978 OVL_WORKDIR_NAME, false);
45aebeaf
VG
979 /*
980 * Upper should support d_type, else whiteouts are visible.
981 * Given workdir and upper are on same fs, we can do
21765194
VG
982 * iterate_dir() on workdir. This check requires successful
983 * creation of workdir in previous step.
45aebeaf 984 */
21765194 985 if (ufs->workdir) {
e7f52429
AG
986 struct dentry *temp;
987
21765194
VG
988 err = ovl_check_d_type_supported(&workpath);
989 if (err < 0)
990 goto out_put_workdir;
45aebeaf 991
e7c0b599
VG
992 /*
993 * We allowed this configuration and don't want to
994 * break users over kernel upgrade. So warn instead
995 * of erroring out.
996 */
997 if (!err)
998 pr_warn("overlayfs: upper fs needs to support d_type.\n");
e7f52429
AG
999
1000 /* Check if upper/work fs supports O_TMPFILE */
1001 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
1002 ufs->tmpfile = !IS_ERR(temp);
1003 if (ufs->tmpfile)
1004 dput(temp);
1005 else
1006 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
82b749b2
AG
1007
1008 /*
1009 * Check if upper/work fs supports trusted.overlay.*
1010 * xattr
1011 */
1012 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
1013 "0", 1, 0);
1014 if (err) {
1015 ufs->noxattr = true;
1016 pr_warn("overlayfs: upper fs does not support xattr.\n");
1017 } else {
1018 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1019 }
02bcd157
AG
1020
1021 /* Check if upper/work fs supports file handles */
1022 if (ufs->config.index &&
1023 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1024 ufs->config.index = false;
1025 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1026 }
45aebeaf 1027 }
e9be9d5e
MS
1028 }
1029
2f83fd8c 1030 err = -ENOMEM;
b9343632
CR
1031 ufs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1032 GFP_KERNEL);
1033 if (ufs->lower_layers == NULL)
3b7a9a24 1034 goto out_put_workdir;
a78d9f0d 1035 for (i = 0; i < numlower; i++) {
b9343632 1036 struct vfsmount *mnt;
2a9c6d06
CR
1037 dev_t dev;
1038
1039 err = get_anon_bdev(&dev);
1040 if (err) {
1041 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1042 goto out_put_lower_layers;
1043 }
dd662667 1044
b9343632 1045 mnt = clone_private_mount(&stack[i]);
2f83fd8c 1046 err = PTR_ERR(mnt);
a78d9f0d
MS
1047 if (IS_ERR(mnt)) {
1048 pr_err("overlayfs: failed to clone lowerpath\n");
2a9c6d06 1049 free_anon_bdev(dev);
b9343632 1050 goto out_put_lower_layers;
a78d9f0d
MS
1051 }
1052 /*
b9343632 1053 * Make lower layers R/O. That way fchmod/fchown on lower file
a78d9f0d
MS
1054 * will fail instead of modifying lower fs.
1055 */
d719e8f2 1056 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
dd662667 1057
b9343632 1058 ufs->lower_layers[ufs->numlower].mnt = mnt;
2a9c6d06 1059 ufs->lower_layers[ufs->numlower].pseudo_dev = dev;
a78d9f0d 1060 ufs->numlower++;
7bcd74b9
AG
1061
1062 /* Check if all lower layers are on same sb */
1063 if (i == 0)
1064 ufs->same_sb = mnt->mnt_sb;
1065 else if (ufs->same_sb != mnt->mnt_sb)
1066 ufs->same_sb = NULL;
a78d9f0d 1067 }
e9be9d5e 1068
71cbad7e 1069 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1070 if (!ufs->upper_mnt)
e9be9d5e 1071 sb->s_flags |= MS_RDONLY;
7bcd74b9
AG
1072 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1073 ufs->same_sb = NULL;
e9be9d5e 1074
b9343632
CR
1075 err = -ENOMEM;
1076 oe = ovl_alloc_entry(numlower);
1077 if (!oe)
1078 goto out_put_lower_layers;
1079
1080 for (i = 0; i < numlower; i++) {
1081 oe->lowerstack[i].dentry = stack[i].dentry;
1082 oe->lowerstack[i].layer = &(ufs->lower_layers[i]);
1083 }
1084
02bcd157 1085 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
8b88a2e6 1086 /* Verify lower root is upper root origin */
b9343632
CR
1087 err = ovl_verify_origin(upperpath.dentry,
1088 oe->lowerstack[0].layer->mnt,
1089 oe->lowerstack[0].dentry,
1090 false, true);
8b88a2e6
AG
1091 if (err) {
1092 pr_err("overlayfs: failed to verify upper root origin\n");
b9343632 1093 goto out_free_oe;
8b88a2e6
AG
1094 }
1095
02bcd157
AG
1096 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1097 OVL_INDEXDIR_NAME, true);
54fb347e
AG
1098 if (ufs->indexdir) {
1099 /* Verify upper root is index dir origin */
1100 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
1101 upperpath.dentry, true, true);
1102 if (err)
1103 pr_err("overlayfs: failed to verify index dir origin\n");
415543d5 1104
caf70cb2 1105 /* Cleanup bad/stale/orphan index entries */
415543d5
AG
1106 if (!err)
1107 err = ovl_indexdir_cleanup(ufs->indexdir,
1108 ufs->upper_mnt,
b9343632
CR
1109 oe->lowerstack,
1110 numlower);
54fb347e
AG
1111 }
1112 if (err || !ufs->indexdir)
02bcd157 1113 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
54fb347e
AG
1114 if (err)
1115 goto out_put_indexdir;
02bcd157
AG
1116 }
1117
1118 /* Show index=off/on in /proc/mounts for any of the reasons above */
1119 if (!ufs->indexdir)
1120 ufs->config.index = false;
1121
7c03b5d4
MS
1122 if (remote)
1123 sb->s_d_op = &ovl_reval_dentry_operations;
1124 else
1125 sb->s_d_op = &ovl_dentry_operations;
e9be9d5e 1126
8fc646b4 1127 err = -ENOMEM;
51f8f3c4
KK
1128 ufs->creator_cred = cred = prepare_creds();
1129 if (!cred)
02bcd157 1130 goto out_put_indexdir;
3fe6e52f 1131
51f8f3c4
KK
1132 /* Never override disk quota limits or use reserved space */
1133 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1134
655042cc
VG
1135 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1136 sb->s_op = &ovl_super_operations;
1137 sb->s_xattr = ovl_xattr_handlers;
1138 sb->s_fs_info = ufs;
1139 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1140
ca4c8a3a 1141 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
e9be9d5e 1142 if (!root_dentry)
b9343632 1143 goto out_put_cred;
e9be9d5e
MS
1144
1145 mntput(upperpath.mnt);
a78d9f0d
MS
1146 for (i = 0; i < numlower; i++)
1147 mntput(stack[i].mnt);
b9343632 1148 kfree(stack);
2cac0c00 1149 mntput(workpath.mnt);
a78d9f0d 1150 kfree(lowertmp);
e9be9d5e 1151
f3a15685 1152 if (upperpath.dentry) {
55acc661 1153 oe->has_upper = true;
13c72075
MS
1154 if (ovl_is_impuredir(upperpath.dentry))
1155 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
f3a15685 1156 }
e9be9d5e
MS
1157
1158 root_dentry->d_fsdata = oe;
1159
b79e05aa
AG
1160 /* Root is always merge -> can have whiteouts */
1161 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
09d8b586
MS
1162 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1163 ovl_dentry_lower(root_dentry));
ed06e069 1164
e9be9d5e 1165 sb->s_root = root_dentry;
e9be9d5e
MS
1166
1167 return 0;
1168
3fe6e52f
AM
1169out_put_cred:
1170 put_cred(ufs->creator_cred);
02bcd157
AG
1171out_put_indexdir:
1172 dput(ufs->indexdir);
b9343632
CR
1173out_free_oe:
1174 kfree(oe);
1175out_put_lower_layers:
2a9c6d06
CR
1176 for (i = 0; i < ufs->numlower; i++) {
1177 if (ufs->lower_layers[i].mnt)
1178 free_anon_bdev(ufs->lower_layers[i].pseudo_dev);
b9343632 1179 mntput(ufs->lower_layers[i].mnt);
2a9c6d06 1180 }
b9343632 1181 kfree(ufs->lower_layers);
3b7a9a24
MS
1182out_put_workdir:
1183 dput(ufs->workdir);
e9be9d5e 1184 mntput(ufs->upper_mnt);
e9be9d5e 1185out_put_lowerpath:
a78d9f0d
MS
1186 for (i = 0; i < numlower; i++)
1187 path_put(&stack[i]);
1188 kfree(stack);
1189out_free_lowertmp:
1190 kfree(lowertmp);
2cac0c00 1191out_unlock_workdentry:
85fdee1e
AG
1192 if (ufs->workdir_locked)
1193 ovl_inuse_unlock(workpath.dentry);
3b7a9a24
MS
1194out_put_workpath:
1195 path_put(&workpath);
2cac0c00 1196out_unlock_upperdentry:
85fdee1e
AG
1197 if (ufs->upperdir_locked)
1198 ovl_inuse_unlock(upperpath.dentry);
e9be9d5e
MS
1199out_put_upperpath:
1200 path_put(&upperpath);
e9be9d5e 1201out_free_config:
f45827e8
EZ
1202 kfree(ufs->config.lowerdir);
1203 kfree(ufs->config.upperdir);
1204 kfree(ufs->config.workdir);
1205 kfree(ufs);
e9be9d5e
MS
1206out:
1207 return err;
1208}
1209
1210static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1211 const char *dev_name, void *raw_data)
1212{
1213 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1214}
1215
1216static struct file_system_type ovl_fs_type = {
1217 .owner = THIS_MODULE,
ef94b186 1218 .name = "overlay",
e9be9d5e
MS
1219 .mount = ovl_mount,
1220 .kill_sb = kill_anon_super,
1221};
ef94b186 1222MODULE_ALIAS_FS("overlay");
e9be9d5e 1223
13cf199d
AG
1224static void ovl_inode_init_once(void *foo)
1225{
1226 struct ovl_inode *oi = foo;
1227
1228 inode_init_once(&oi->vfs_inode);
1229}
1230
e9be9d5e
MS
1231static int __init ovl_init(void)
1232{
13cf199d
AG
1233 int err;
1234
1235 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1236 sizeof(struct ovl_inode), 0,
1237 (SLAB_RECLAIM_ACCOUNT|
1238 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1239 ovl_inode_init_once);
1240 if (ovl_inode_cachep == NULL)
1241 return -ENOMEM;
1242
1243 err = register_filesystem(&ovl_fs_type);
1244 if (err)
1245 kmem_cache_destroy(ovl_inode_cachep);
1246
1247 return err;
e9be9d5e
MS
1248}
1249
1250static void __exit ovl_exit(void)
1251{
1252 unregister_filesystem(&ovl_fs_type);
13cf199d
AG
1253
1254 /*
1255 * Make sure all delayed rcu free inodes are flushed before we
1256 * destroy cache.
1257 */
1258 rcu_barrier();
1259 kmem_cache_destroy(ovl_inode_cachep);
1260
e9be9d5e
MS
1261}
1262
1263module_init(ovl_init);
1264module_exit(ovl_exit);