ovl: automatically enable redirect_dir on metacopy=on
[linux-2.6-block.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>
e487d889 20#include <linux/exportfs.h>
e9be9d5e
MS
21#include "overlayfs.h"
22
23MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24MODULE_DESCRIPTION("Overlay filesystem");
25MODULE_LICENSE("GPL");
26
e9be9d5e
MS
27
28struct ovl_dir_cache;
29
a78d9f0d
MS
30#define OVL_MAX_STACK 500
31
688ea0e5
MS
32static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
33module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
34MODULE_PARM_DESC(ovl_redirect_dir_def,
35 "Default to on or off for the redirect_dir feature");
e9be9d5e 36
438c84c2
MS
37static bool ovl_redirect_always_follow =
38 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
39module_param_named(redirect_always_follow, ovl_redirect_always_follow,
40 bool, 0644);
41MODULE_PARM_DESC(ovl_redirect_always_follow,
42 "Follow redirects even if redirect_dir feature is turned off");
43
02bcd157
AG
44static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
45module_param_named(index, ovl_index_def, bool, 0644);
46MODULE_PARM_DESC(ovl_index_def,
47 "Default to on or off for the inodes index feature");
48
f168f109
AG
49static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
50module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
51MODULE_PARM_DESC(ovl_nfs_export_def,
52 "Default to on or off for the NFS export feature");
53
795939a9
AG
54static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
55module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
56MODULE_PARM_DESC(ovl_xino_auto_def,
57 "Auto enable xino feature");
58
4155c10a
MS
59static void ovl_entry_stack_free(struct ovl_entry *oe)
60{
61 unsigned int i;
62
63 for (i = 0; i < oe->numlower; i++)
64 dput(oe->lowerstack[i].dentry);
65}
66
d5791044
VG
67static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
68module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
69MODULE_PARM_DESC(ovl_metacopy_def,
70 "Default to on or off for the metadata only copy up feature");
71
e9be9d5e
MS
72static void ovl_dentry_release(struct dentry *dentry)
73{
74 struct ovl_entry *oe = dentry->d_fsdata;
75
76 if (oe) {
4155c10a 77 ovl_entry_stack_free(oe);
e9be9d5e
MS
78 kfree_rcu(oe, rcu);
79 }
80}
81
2d902671 82static struct dentry *ovl_d_real(struct dentry *dentry,
fb16043b 83 const struct inode *inode)
d101a125
MS
84{
85 struct dentry *real;
86
e8c985ba
MS
87 /* It's an overlay file */
88 if (inode && d_inode(dentry) == inode)
89 return dentry;
90
ca4c8a3a 91 if (!d_is_reg(dentry)) {
d101a125
MS
92 if (!inode || inode == d_inode(dentry))
93 return dentry;
94 goto bug;
95 }
96
97 real = ovl_dentry_upper(dentry);
2c3d7358 98 if (real && (inode == d_inode(real)))
d101a125
MS
99 return real;
100
2c3d7358
VG
101 if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
102 return real;
103
104 real = ovl_dentry_lowerdata(dentry);
d101a125
MS
105 if (!real)
106 goto bug;
107
c4fcfc16 108 /* Handle recursion */
fb16043b 109 real = d_real(real, inode);
c4fcfc16 110
d101a125
MS
111 if (!inode || inode == d_inode(real))
112 return real;
d101a125 113bug:
656189d2 114 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
d101a125
MS
115 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
116 return dentry;
117}
118
7c03b5d4
MS
119static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
120{
121 struct ovl_entry *oe = dentry->d_fsdata;
122 unsigned int i;
123 int ret = 1;
124
125 for (i = 0; i < oe->numlower; i++) {
126 struct dentry *d = oe->lowerstack[i].dentry;
127
128 if (d->d_flags & DCACHE_OP_REVALIDATE) {
129 ret = d->d_op->d_revalidate(d, flags);
130 if (ret < 0)
131 return ret;
132 if (!ret) {
133 if (!(flags & LOOKUP_RCU))
134 d_invalidate(d);
135 return -ESTALE;
136 }
137 }
138 }
139 return 1;
140}
141
142static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
143{
144 struct ovl_entry *oe = dentry->d_fsdata;
145 unsigned int i;
146 int ret = 1;
147
148 for (i = 0; i < oe->numlower; i++) {
149 struct dentry *d = oe->lowerstack[i].dentry;
150
151 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
152 ret = d->d_op->d_weak_revalidate(d, flags);
153 if (ret <= 0)
154 break;
155 }
156 }
157 return ret;
158}
159
e9be9d5e
MS
160static const struct dentry_operations ovl_dentry_operations = {
161 .d_release = ovl_dentry_release,
d101a125 162 .d_real = ovl_d_real,
e9be9d5e
MS
163};
164
7c03b5d4
MS
165static const struct dentry_operations ovl_reval_dentry_operations = {
166 .d_release = ovl_dentry_release,
d101a125 167 .d_real = ovl_d_real,
7c03b5d4
MS
168 .d_revalidate = ovl_dentry_revalidate,
169 .d_weak_revalidate = ovl_dentry_weak_revalidate,
170};
171
13cf199d
AG
172static struct kmem_cache *ovl_inode_cachep;
173
174static struct inode *ovl_alloc_inode(struct super_block *sb)
175{
176 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
177
b3885bd6
HN
178 if (!oi)
179 return NULL;
180
04a01ac7 181 oi->cache = NULL;
cf31c463 182 oi->redirect = NULL;
04a01ac7 183 oi->version = 0;
13c72075 184 oi->flags = 0;
09d8b586 185 oi->__upperdentry = NULL;
25b7713a 186 oi->lower = NULL;
2664bd08 187 oi->lowerdata = NULL;
a015dafc 188 mutex_init(&oi->lock);
25b7713a 189
13cf199d
AG
190 return &oi->vfs_inode;
191}
192
193static void ovl_i_callback(struct rcu_head *head)
194{
195 struct inode *inode = container_of(head, struct inode, i_rcu);
196
197 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
198}
199
200static void ovl_destroy_inode(struct inode *inode)
201{
09d8b586
MS
202 struct ovl_inode *oi = OVL_I(inode);
203
204 dput(oi->__upperdentry);
31747eda 205 iput(oi->lower);
2664bd08
VG
206 if (S_ISDIR(inode->i_mode))
207 ovl_dir_cache_free(inode);
208 else
209 iput(oi->lowerdata);
cf31c463 210 kfree(oi->redirect);
a015dafc 211 mutex_destroy(&oi->lock);
09d8b586 212
13cf199d
AG
213 call_rcu(&inode->i_rcu, ovl_i_callback);
214}
215
ad204488 216static void ovl_free_fs(struct ovl_fs *ofs)
e9be9d5e 217{
dd662667 218 unsigned i;
e9be9d5e 219
ad204488
MS
220 dput(ofs->indexdir);
221 dput(ofs->workdir);
222 if (ofs->workdir_locked)
223 ovl_inuse_unlock(ofs->workbasedir);
224 dput(ofs->workbasedir);
225 if (ofs->upperdir_locked)
226 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
227 mntput(ofs->upper_mnt);
5148626b 228 for (i = 0; i < ofs->numlower; i++)
ad204488 229 mntput(ofs->lower_layers[i].mnt);
5148626b
AG
230 for (i = 0; i < ofs->numlowerfs; i++)
231 free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
ad204488 232 kfree(ofs->lower_layers);
5148626b 233 kfree(ofs->lower_fs);
ad204488
MS
234
235 kfree(ofs->config.lowerdir);
236 kfree(ofs->config.upperdir);
237 kfree(ofs->config.workdir);
438c84c2 238 kfree(ofs->config.redirect_mode);
ad204488
MS
239 if (ofs->creator_cred)
240 put_cred(ofs->creator_cred);
241 kfree(ofs);
e9be9d5e
MS
242}
243
a9075cdb
MS
244static void ovl_put_super(struct super_block *sb)
245{
246 struct ovl_fs *ofs = sb->s_fs_info;
247
248 ovl_free_fs(ofs);
249}
250
e8d4bfe3 251/* Sync real dirty inodes in upper filesystem (if it exists) */
e593b2bf
AG
252static int ovl_sync_fs(struct super_block *sb, int wait)
253{
ad204488 254 struct ovl_fs *ofs = sb->s_fs_info;
e593b2bf
AG
255 struct super_block *upper_sb;
256 int ret;
257
ad204488 258 if (!ofs->upper_mnt)
e593b2bf 259 return 0;
e8d4bfe3
CX
260
261 /*
262 * If this is a sync(2) call or an emergency sync, all the super blocks
263 * will be iterated, including upper_sb, so no need to do anything.
264 *
265 * If this is a syncfs(2) call, then we do need to call
266 * sync_filesystem() on upper_sb, but enough if we do it when being
267 * called with wait == 1.
268 */
269 if (!wait)
e593b2bf
AG
270 return 0;
271
e8d4bfe3
CX
272 upper_sb = ofs->upper_mnt->mnt_sb;
273
e593b2bf 274 down_read(&upper_sb->s_umount);
e8d4bfe3 275 ret = sync_filesystem(upper_sb);
e593b2bf 276 up_read(&upper_sb->s_umount);
e8d4bfe3 277
e593b2bf
AG
278 return ret;
279}
280
cc259639
AW
281/**
282 * ovl_statfs
283 * @sb: The overlayfs super block
284 * @buf: The struct kstatfs to fill in with stats
285 *
286 * Get the filesystem statistics. As writes always target the upper layer
4ebc5818 287 * filesystem pass the statfs to the upper filesystem (if it exists)
cc259639
AW
288 */
289static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
290{
291 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
292 struct dentry *root_dentry = dentry->d_sb->s_root;
293 struct path path;
294 int err;
295
4ebc5818 296 ovl_path_real(root_dentry, &path);
cc259639
AW
297
298 err = vfs_statfs(&path, buf);
299 if (!err) {
6b2d5fe4 300 buf->f_namelen = ofs->namelen;
cc259639
AW
301 buf->f_type = OVERLAYFS_SUPER_MAGIC;
302 }
303
304 return err;
305}
306
02bcd157 307/* Will this overlay be forced to mount/remount ro? */
ad204488 308static bool ovl_force_readonly(struct ovl_fs *ofs)
02bcd157 309{
ad204488 310 return (!ofs->upper_mnt || !ofs->workdir);
02bcd157
AG
311}
312
438c84c2
MS
313static const char *ovl_redirect_mode_def(void)
314{
315 return ovl_redirect_dir_def ? "on" : "off";
316}
317
795939a9
AG
318enum {
319 OVL_XINO_OFF,
320 OVL_XINO_AUTO,
321 OVL_XINO_ON,
322};
323
324static const char * const ovl_xino_str[] = {
325 "off",
326 "auto",
327 "on",
328};
329
330static inline int ovl_xino_def(void)
331{
332 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
333}
334
f45827e8
EZ
335/**
336 * ovl_show_options
337 *
338 * Prints the mount options for a given superblock.
339 * Returns zero; does not fail.
340 */
341static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
342{
343 struct super_block *sb = dentry->d_sb;
ad204488 344 struct ovl_fs *ofs = sb->s_fs_info;
f45827e8 345
ad204488
MS
346 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
347 if (ofs->config.upperdir) {
348 seq_show_option(m, "upperdir", ofs->config.upperdir);
349 seq_show_option(m, "workdir", ofs->config.workdir);
53a08cb9 350 }
ad204488 351 if (ofs->config.default_permissions)
8d3095f4 352 seq_puts(m, ",default_permissions");
438c84c2
MS
353 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
354 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
ad204488 355 if (ofs->config.index != ovl_index_def)
438c84c2 356 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
f168f109
AG
357 if (ofs->config.nfs_export != ovl_nfs_export_def)
358 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
359 "on" : "off");
795939a9
AG
360 if (ofs->config.xino != ovl_xino_def())
361 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
d5791044
VG
362 if (ofs->config.metacopy != ovl_metacopy_def)
363 seq_printf(m, ",metacopy=%s",
364 ofs->config.metacopy ? "on" : "off");
f45827e8
EZ
365 return 0;
366}
367
3cdf6fe9
SL
368static int ovl_remount(struct super_block *sb, int *flags, char *data)
369{
ad204488 370 struct ovl_fs *ofs = sb->s_fs_info;
3cdf6fe9 371
1751e8a6 372 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
3cdf6fe9
SL
373 return -EROFS;
374
375 return 0;
376}
377
e9be9d5e 378static const struct super_operations ovl_super_operations = {
13cf199d
AG
379 .alloc_inode = ovl_alloc_inode,
380 .destroy_inode = ovl_destroy_inode,
381 .drop_inode = generic_delete_inode,
e9be9d5e 382 .put_super = ovl_put_super,
e593b2bf 383 .sync_fs = ovl_sync_fs,
cc259639 384 .statfs = ovl_statfs,
f45827e8 385 .show_options = ovl_show_options,
3cdf6fe9 386 .remount_fs = ovl_remount,
e9be9d5e
MS
387};
388
389enum {
390 OPT_LOWERDIR,
391 OPT_UPPERDIR,
392 OPT_WORKDIR,
8d3095f4 393 OPT_DEFAULT_PERMISSIONS,
438c84c2 394 OPT_REDIRECT_DIR,
02bcd157
AG
395 OPT_INDEX_ON,
396 OPT_INDEX_OFF,
f168f109
AG
397 OPT_NFS_EXPORT_ON,
398 OPT_NFS_EXPORT_OFF,
795939a9
AG
399 OPT_XINO_ON,
400 OPT_XINO_OFF,
401 OPT_XINO_AUTO,
d5791044
VG
402 OPT_METACOPY_ON,
403 OPT_METACOPY_OFF,
e9be9d5e
MS
404 OPT_ERR,
405};
406
407static const match_table_t ovl_tokens = {
408 {OPT_LOWERDIR, "lowerdir=%s"},
409 {OPT_UPPERDIR, "upperdir=%s"},
410 {OPT_WORKDIR, "workdir=%s"},
8d3095f4 411 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
438c84c2 412 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
02bcd157
AG
413 {OPT_INDEX_ON, "index=on"},
414 {OPT_INDEX_OFF, "index=off"},
f168f109
AG
415 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
416 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
795939a9
AG
417 {OPT_XINO_ON, "xino=on"},
418 {OPT_XINO_OFF, "xino=off"},
419 {OPT_XINO_AUTO, "xino=auto"},
d5791044
VG
420 {OPT_METACOPY_ON, "metacopy=on"},
421 {OPT_METACOPY_OFF, "metacopy=off"},
e9be9d5e
MS
422 {OPT_ERR, NULL}
423};
424
91c77947
MS
425static char *ovl_next_opt(char **s)
426{
427 char *sbegin = *s;
428 char *p;
429
430 if (sbegin == NULL)
431 return NULL;
432
433 for (p = sbegin; *p; p++) {
434 if (*p == '\\') {
435 p++;
436 if (!*p)
437 break;
438 } else if (*p == ',') {
439 *p = '\0';
440 *s = p + 1;
441 return sbegin;
442 }
443 }
444 *s = NULL;
445 return sbegin;
446}
447
438c84c2
MS
448static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
449{
450 if (strcmp(mode, "on") == 0) {
451 config->redirect_dir = true;
452 /*
453 * Does not make sense to have redirect creation without
454 * redirect following.
455 */
456 config->redirect_follow = true;
457 } else if (strcmp(mode, "follow") == 0) {
458 config->redirect_follow = true;
459 } else if (strcmp(mode, "off") == 0) {
460 if (ovl_redirect_always_follow)
461 config->redirect_follow = true;
462 } else if (strcmp(mode, "nofollow") != 0) {
463 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
464 mode);
465 return -EINVAL;
466 }
467
468 return 0;
469}
470
e9be9d5e
MS
471static int ovl_parse_opt(char *opt, struct ovl_config *config)
472{
473 char *p;
d5791044 474 int err;
d47748e5 475 bool metacopy_opt = false, redirect_opt = false;
e9be9d5e 476
438c84c2
MS
477 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
478 if (!config->redirect_mode)
479 return -ENOMEM;
480
91c77947 481 while ((p = ovl_next_opt(&opt)) != NULL) {
e9be9d5e
MS
482 int token;
483 substring_t args[MAX_OPT_ARGS];
484
485 if (!*p)
486 continue;
487
488 token = match_token(p, ovl_tokens, args);
489 switch (token) {
490 case OPT_UPPERDIR:
491 kfree(config->upperdir);
492 config->upperdir = match_strdup(&args[0]);
493 if (!config->upperdir)
494 return -ENOMEM;
495 break;
496
497 case OPT_LOWERDIR:
498 kfree(config->lowerdir);
499 config->lowerdir = match_strdup(&args[0]);
500 if (!config->lowerdir)
501 return -ENOMEM;
502 break;
503
504 case OPT_WORKDIR:
505 kfree(config->workdir);
506 config->workdir = match_strdup(&args[0]);
507 if (!config->workdir)
508 return -ENOMEM;
509 break;
510
8d3095f4
MS
511 case OPT_DEFAULT_PERMISSIONS:
512 config->default_permissions = true;
513 break;
514
438c84c2
MS
515 case OPT_REDIRECT_DIR:
516 kfree(config->redirect_mode);
517 config->redirect_mode = match_strdup(&args[0]);
518 if (!config->redirect_mode)
519 return -ENOMEM;
d47748e5 520 redirect_opt = true;
a6c60655
MS
521 break;
522
02bcd157
AG
523 case OPT_INDEX_ON:
524 config->index = true;
525 break;
526
527 case OPT_INDEX_OFF:
528 config->index = false;
529 break;
530
f168f109
AG
531 case OPT_NFS_EXPORT_ON:
532 config->nfs_export = true;
533 break;
534
535 case OPT_NFS_EXPORT_OFF:
536 config->nfs_export = false;
537 break;
538
795939a9
AG
539 case OPT_XINO_ON:
540 config->xino = OVL_XINO_ON;
541 break;
542
543 case OPT_XINO_OFF:
544 config->xino = OVL_XINO_OFF;
545 break;
546
547 case OPT_XINO_AUTO:
548 config->xino = OVL_XINO_AUTO;
549 break;
550
d5791044
VG
551 case OPT_METACOPY_ON:
552 config->metacopy = true;
d47748e5 553 metacopy_opt = true;
d5791044
VG
554 break;
555
556 case OPT_METACOPY_OFF:
557 config->metacopy = false;
558 break;
559
e9be9d5e 560 default:
bead55ef 561 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
e9be9d5e
MS
562 return -EINVAL;
563 }
564 }
71cbad7e 565
566 /* Workdir is useless in non-upper mount */
567 if (!config->upperdir && config->workdir) {
568 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
569 config->workdir);
570 kfree(config->workdir);
571 config->workdir = NULL;
572 }
573
d5791044
VG
574 err = ovl_parse_redirect_mode(config, config->redirect_mode);
575 if (err)
576 return err;
577
d47748e5
MS
578 /*
579 * This is to make the logic below simpler. It doesn't make any other
580 * difference, since config->redirect_dir is only used for upper.
581 */
582 if (!config->upperdir && config->redirect_follow)
583 config->redirect_dir = true;
584
585 /* Resolve metacopy -> redirect_dir dependency */
586 if (config->metacopy && !config->redirect_dir) {
587 if (metacopy_opt && redirect_opt) {
588 pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n",
589 config->redirect_mode);
590 return -EINVAL;
591 }
592 if (redirect_opt) {
593 /*
594 * There was an explicit redirect_dir=... that resulted
595 * in this conflict.
596 */
597 pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n",
598 config->redirect_mode);
599 config->metacopy = false;
600 } else {
601 /* Automatically enable redirect otherwise. */
602 config->redirect_follow = config->redirect_dir = true;
603 }
d5791044
VG
604 }
605
606 return 0;
e9be9d5e
MS
607}
608
609#define OVL_WORKDIR_NAME "work"
02bcd157 610#define OVL_INDEXDIR_NAME "index"
e9be9d5e 611
ad204488 612static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
6b8aa129 613 const char *name, bool persist)
e9be9d5e 614{
ad204488
MS
615 struct inode *dir = ofs->workbasedir->d_inode;
616 struct vfsmount *mnt = ofs->upper_mnt;
e9be9d5e
MS
617 struct dentry *work;
618 int err;
619 bool retried = false;
6b8aa129 620 bool locked = false;
e9be9d5e 621
5955102c 622 inode_lock_nested(dir, I_MUTEX_PARENT);
6b8aa129
AG
623 locked = true;
624
e9be9d5e 625retry:
ad204488 626 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
e9be9d5e
MS
627
628 if (!IS_ERR(work)) {
c11b9fdd
MS
629 struct iattr attr = {
630 .ia_valid = ATTR_MODE,
32a3d848 631 .ia_mode = S_IFDIR | 0,
c11b9fdd 632 };
e9be9d5e
MS
633
634 if (work->d_inode) {
635 err = -EEXIST;
636 if (retried)
637 goto out_dput;
638
6b8aa129
AG
639 if (persist)
640 goto out_unlock;
641
e9be9d5e 642 retried = true;
eea2fb48 643 ovl_workdir_cleanup(dir, mnt, work, 0);
e9be9d5e
MS
644 dput(work);
645 goto retry;
646 }
647
95a1c815
MS
648 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
649 err = PTR_ERR(work);
650 if (IS_ERR(work))
651 goto out_err;
c11b9fdd 652
cb348edb
MS
653 /*
654 * Try to remove POSIX ACL xattrs from workdir. We are good if:
655 *
656 * a) success (there was a POSIX ACL xattr and was removed)
657 * b) -ENODATA (there was no POSIX ACL xattr)
658 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
659 *
660 * There are various other error values that could effectively
661 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
662 * if the xattr name is too long), but the set of filesystems
663 * allowed as upper are limited to "normal" ones, where checking
664 * for the above two errors is sufficient.
665 */
c11b9fdd 666 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
e1ff3dd1 667 if (err && err != -ENODATA && err != -EOPNOTSUPP)
c11b9fdd
MS
668 goto out_dput;
669
670 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
e1ff3dd1 671 if (err && err != -ENODATA && err != -EOPNOTSUPP)
c11b9fdd
MS
672 goto out_dput;
673
674 /* Clear any inherited mode bits */
675 inode_lock(work->d_inode);
676 err = notify_change(work, &attr, NULL);
677 inode_unlock(work->d_inode);
678 if (err)
679 goto out_dput;
6b8aa129
AG
680 } else {
681 err = PTR_ERR(work);
682 goto out_err;
e9be9d5e
MS
683 }
684out_unlock:
6b8aa129
AG
685 if (locked)
686 inode_unlock(dir);
e9be9d5e
MS
687
688 return work;
689
690out_dput:
691 dput(work);
6b8aa129
AG
692out_err:
693 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
ad204488 694 ofs->config.workdir, name, -err);
6b8aa129 695 work = NULL;
e9be9d5e
MS
696 goto out_unlock;
697}
698
91c77947
MS
699static void ovl_unescape(char *s)
700{
701 char *d = s;
702
703 for (;; s++, d++) {
704 if (*s == '\\')
705 s++;
706 *d = *s;
707 if (!*s)
708 break;
709 }
710}
711
ab508822
MS
712static int ovl_mount_dir_noesc(const char *name, struct path *path)
713{
a78d9f0d 714 int err = -EINVAL;
ab508822 715
a78d9f0d
MS
716 if (!*name) {
717 pr_err("overlayfs: empty lowerdir\n");
718 goto out;
719 }
ab508822
MS
720 err = kern_path(name, LOOKUP_FOLLOW, path);
721 if (err) {
722 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
723 goto out;
724 }
725 err = -EINVAL;
7c03b5d4 726 if (ovl_dentry_weird(path->dentry)) {
ab508822
MS
727 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
728 goto out_put;
729 }
2b8c30e9 730 if (!d_is_dir(path->dentry)) {
ab508822
MS
731 pr_err("overlayfs: '%s' not a directory\n", name);
732 goto out_put;
733 }
734 return 0;
735
736out_put:
8aafcb59 737 path_put_init(path);
ab508822
MS
738out:
739 return err;
740}
741
742static int ovl_mount_dir(const char *name, struct path *path)
743{
744 int err = -ENOMEM;
745 char *tmp = kstrdup(name, GFP_KERNEL);
746
747 if (tmp) {
748 ovl_unescape(tmp);
749 err = ovl_mount_dir_noesc(tmp, path);
7c03b5d4
MS
750
751 if (!err)
752 if (ovl_dentry_remote(path->dentry)) {
753 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
754 tmp);
8aafcb59 755 path_put_init(path);
7c03b5d4
MS
756 err = -EINVAL;
757 }
ab508822
MS
758 kfree(tmp);
759 }
760 return err;
761}
762
6b2d5fe4
MS
763static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
764 const char *name)
ab508822 765{
ab508822 766 struct kstatfs statfs;
6b2d5fe4
MS
767 int err = vfs_statfs(path, &statfs);
768
769 if (err)
770 pr_err("overlayfs: statfs failed on '%s'\n", name);
771 else
772 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
773
774 return err;
775}
776
777static int ovl_lower_dir(const char *name, struct path *path,
778 struct ovl_fs *ofs, int *stack_depth, bool *remote)
779{
e487d889 780 int fh_type;
6b2d5fe4 781 int err;
ab508822 782
a78d9f0d 783 err = ovl_mount_dir_noesc(name, path);
ab508822
MS
784 if (err)
785 goto out;
786
6b2d5fe4
MS
787 err = ovl_check_namelen(path, ofs, name);
788 if (err)
ab508822 789 goto out_put;
6b2d5fe4 790
ab508822
MS
791 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
792
7c03b5d4
MS
793 if (ovl_dentry_remote(path->dentry))
794 *remote = true;
795
02bcd157 796 /*
f168f109
AG
797 * The inodes index feature and NFS export need to encode and decode
798 * file handles, so they require that all layers support them.
02bcd157 799 */
e487d889 800 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
f168f109 801 if ((ofs->config.nfs_export ||
e487d889 802 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
02bcd157 803 ofs->config.index = false;
f168f109
AG
804 ofs->config.nfs_export = false;
805 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
806 name);
02bcd157
AG
807 }
808
e487d889
AG
809 /* Check if lower fs has 32bit inode numbers */
810 if (fh_type != FILEID_INO32_GEN)
811 ofs->xino_bits = 0;
812
ab508822
MS
813 return 0;
814
815out_put:
8aafcb59 816 path_put_init(path);
ab508822
MS
817out:
818 return err;
819}
820
e9be9d5e
MS
821/* Workdir should not be subdir of upperdir and vice versa */
822static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
823{
824 bool ok = false;
825
826 if (workdir != upperdir) {
827 ok = (lock_rename(workdir, upperdir) == NULL);
828 unlock_rename(workdir, upperdir);
829 }
830 return ok;
831}
832
a78d9f0d
MS
833static unsigned int ovl_split_lowerdirs(char *str)
834{
835 unsigned int ctr = 1;
836 char *s, *d;
837
838 for (s = d = str;; s++, d++) {
839 if (*s == '\\') {
840 s++;
841 } else if (*s == ':') {
842 *d = '\0';
843 ctr++;
844 continue;
845 }
846 *d = *s;
847 if (!*s)
848 break;
849 }
850 return ctr;
851}
852
0eb45fc3
AG
853static int __maybe_unused
854ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
855 struct dentry *dentry, struct inode *inode,
856 const char *name, void *buffer, size_t size)
857{
1d88f183 858 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
0eb45fc3
AG
859}
860
0c97be22
AG
861static int __maybe_unused
862ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
863 struct dentry *dentry, struct inode *inode,
864 const char *name, const void *value,
865 size_t size, int flags)
d837a49b
MS
866{
867 struct dentry *workdir = ovl_workdir(dentry);
09d8b586 868 struct inode *realinode = ovl_inode_real(inode);
d837a49b
MS
869 struct posix_acl *acl = NULL;
870 int err;
871
872 /* Check that everything is OK before copy-up */
873 if (value) {
874 acl = posix_acl_from_xattr(&init_user_ns, value, size);
875 if (IS_ERR(acl))
876 return PTR_ERR(acl);
877 }
878 err = -EOPNOTSUPP;
879 if (!IS_POSIXACL(d_inode(workdir)))
880 goto out_acl_release;
881 if (!realinode->i_op->set_acl)
882 goto out_acl_release;
883 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
884 err = acl ? -EACCES : 0;
885 goto out_acl_release;
886 }
887 err = -EPERM;
888 if (!inode_owner_or_capable(inode))
889 goto out_acl_release;
890
891 posix_acl_release(acl);
892
fd3220d3
MS
893 /*
894 * Check if sgid bit needs to be cleared (actual setacl operation will
895 * be done with mounter's capabilities and so that won't do it for us).
896 */
897 if (unlikely(inode->i_mode & S_ISGID) &&
898 handler->flags == ACL_TYPE_ACCESS &&
899 !in_group_p(inode->i_gid) &&
900 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
901 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
902
903 err = ovl_setattr(dentry, &iattr);
904 if (err)
905 return err;
906 }
907
1d88f183 908 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
ce31513a 909 if (!err)
09d8b586 910 ovl_copyattr(ovl_inode_real(inode), inode);
ce31513a
MS
911
912 return err;
d837a49b
MS
913
914out_acl_release:
915 posix_acl_release(acl);
916 return err;
917}
918
0eb45fc3
AG
919static int ovl_own_xattr_get(const struct xattr_handler *handler,
920 struct dentry *dentry, struct inode *inode,
921 const char *name, void *buffer, size_t size)
922{
48fab5d7 923 return -EOPNOTSUPP;
0eb45fc3
AG
924}
925
d837a49b
MS
926static int ovl_own_xattr_set(const struct xattr_handler *handler,
927 struct dentry *dentry, struct inode *inode,
928 const char *name, const void *value,
929 size_t size, int flags)
930{
48fab5d7 931 return -EOPNOTSUPP;
d837a49b
MS
932}
933
0eb45fc3
AG
934static int ovl_other_xattr_get(const struct xattr_handler *handler,
935 struct dentry *dentry, struct inode *inode,
936 const char *name, void *buffer, size_t size)
937{
1d88f183 938 return ovl_xattr_get(dentry, inode, name, buffer, size);
0eb45fc3
AG
939}
940
0e585ccc
AG
941static int ovl_other_xattr_set(const struct xattr_handler *handler,
942 struct dentry *dentry, struct inode *inode,
943 const char *name, const void *value,
944 size_t size, int flags)
945{
1d88f183 946 return ovl_xattr_set(dentry, inode, name, value, size, flags);
0e585ccc
AG
947}
948
0c97be22
AG
949static const struct xattr_handler __maybe_unused
950ovl_posix_acl_access_xattr_handler = {
d837a49b
MS
951 .name = XATTR_NAME_POSIX_ACL_ACCESS,
952 .flags = ACL_TYPE_ACCESS,
0eb45fc3 953 .get = ovl_posix_acl_xattr_get,
d837a49b
MS
954 .set = ovl_posix_acl_xattr_set,
955};
956
0c97be22
AG
957static const struct xattr_handler __maybe_unused
958ovl_posix_acl_default_xattr_handler = {
d837a49b
MS
959 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
960 .flags = ACL_TYPE_DEFAULT,
0eb45fc3 961 .get = ovl_posix_acl_xattr_get,
d837a49b
MS
962 .set = ovl_posix_acl_xattr_set,
963};
964
965static const struct xattr_handler ovl_own_xattr_handler = {
966 .prefix = OVL_XATTR_PREFIX,
0eb45fc3 967 .get = ovl_own_xattr_get,
d837a49b
MS
968 .set = ovl_own_xattr_set,
969};
970
971static const struct xattr_handler ovl_other_xattr_handler = {
972 .prefix = "", /* catch all */
0eb45fc3 973 .get = ovl_other_xattr_get,
d837a49b
MS
974 .set = ovl_other_xattr_set,
975};
976
977static const struct xattr_handler *ovl_xattr_handlers[] = {
0c97be22 978#ifdef CONFIG_FS_POSIX_ACL
d837a49b
MS
979 &ovl_posix_acl_access_xattr_handler,
980 &ovl_posix_acl_default_xattr_handler,
0c97be22 981#endif
d837a49b
MS
982 &ovl_own_xattr_handler,
983 &ovl_other_xattr_handler,
984 NULL
985};
986
ad204488 987static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath)
6ee8acf0 988{
5064975e 989 struct vfsmount *upper_mnt;
6ee8acf0
MS
990 int err;
991
ad204488 992 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
6ee8acf0
MS
993 if (err)
994 goto out;
995
996 /* Upper fs should not be r/o */
997 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
998 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
999 err = -EINVAL;
1000 goto out;
1001 }
1002
ad204488 1003 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
6ee8acf0
MS
1004 if (err)
1005 goto out;
1006
5064975e
MS
1007 upper_mnt = clone_private_mount(upperpath);
1008 err = PTR_ERR(upper_mnt);
1009 if (IS_ERR(upper_mnt)) {
1010 pr_err("overlayfs: failed to clone upperpath\n");
1011 goto out;
1012 }
1013
1014 /* Don't inherit atime flags */
1015 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
ad204488 1016 ofs->upper_mnt = upper_mnt;
8c25741a
MS
1017
1018 err = -EBUSY;
1019 if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
1020 ofs->upperdir_locked = true;
1021 } else if (ofs->config.index) {
1022 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
1023 goto out;
1024 } else {
1025 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1026 }
1027
6ee8acf0
MS
1028 err = 0;
1029out:
1030 return err;
1031}
1032
ad204488 1033static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
8ed61dc3 1034{
2ba9d57e 1035 struct vfsmount *mnt = ofs->upper_mnt;
8ed61dc3 1036 struct dentry *temp;
e487d889 1037 int fh_type;
8ed61dc3
MS
1038 int err;
1039
2ba9d57e
AG
1040 err = mnt_want_write(mnt);
1041 if (err)
1042 return err;
1043
ad204488
MS
1044 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1045 if (!ofs->workdir)
2ba9d57e 1046 goto out;
8ed61dc3
MS
1047
1048 /*
1049 * Upper should support d_type, else whiteouts are visible. Given
1050 * workdir and upper are on same fs, we can do iterate_dir() on
1051 * workdir. This check requires successful creation of workdir in
1052 * previous step.
1053 */
1054 err = ovl_check_d_type_supported(workpath);
1055 if (err < 0)
2ba9d57e 1056 goto out;
8ed61dc3
MS
1057
1058 /*
1059 * We allowed this configuration and don't want to break users over
1060 * kernel upgrade. So warn instead of erroring out.
1061 */
1062 if (!err)
1063 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1064
1065 /* Check if upper/work fs supports O_TMPFILE */
ad204488
MS
1066 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1067 ofs->tmpfile = !IS_ERR(temp);
1068 if (ofs->tmpfile)
8ed61dc3
MS
1069 dput(temp);
1070 else
1071 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1072
1073 /*
1074 * Check if upper/work fs supports trusted.overlay.* xattr
1075 */
ad204488 1076 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
8ed61dc3 1077 if (err) {
ad204488 1078 ofs->noxattr = true;
a683737b 1079 ofs->config.index = false;
d5791044
VG
1080 ofs->config.metacopy = false;
1081 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
2ba9d57e 1082 err = 0;
8ed61dc3 1083 } else {
ad204488 1084 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
8ed61dc3
MS
1085 }
1086
1087 /* Check if upper/work fs supports file handles */
e487d889
AG
1088 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1089 if (ofs->config.index && !fh_type) {
ad204488 1090 ofs->config.index = false;
8ed61dc3
MS
1091 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1092 }
1093
e487d889
AG
1094 /* Check if upper fs has 32bit inode numbers */
1095 if (fh_type != FILEID_INO32_GEN)
1096 ofs->xino_bits = 0;
1097
f168f109
AG
1098 /* NFS export of r/w mount depends on index */
1099 if (ofs->config.nfs_export && !ofs->config.index) {
1100 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1101 ofs->config.nfs_export = false;
1102 }
2ba9d57e
AG
1103out:
1104 mnt_drop_write(mnt);
1105 return err;
8ed61dc3
MS
1106}
1107
ad204488 1108static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)
520d7c86
MS
1109{
1110 int err;
bca44b52 1111 struct path workpath = { };
520d7c86 1112
ad204488 1113 err = ovl_mount_dir(ofs->config.workdir, &workpath);
520d7c86
MS
1114 if (err)
1115 goto out;
1116
1117 err = -EINVAL;
bca44b52 1118 if (upperpath->mnt != workpath.mnt) {
520d7c86
MS
1119 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1120 goto out;
1121 }
bca44b52 1122 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
520d7c86
MS
1123 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1124 goto out;
1125 }
1126
8c25741a
MS
1127 ofs->workbasedir = dget(workpath.dentry);
1128
520d7c86 1129 err = -EBUSY;
8c25741a 1130 if (ovl_inuse_trylock(ofs->workbasedir)) {
ad204488
MS
1131 ofs->workdir_locked = true;
1132 } else if (ofs->config.index) {
520d7c86
MS
1133 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1134 goto out;
1135 } else {
1136 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1137 }
1138
ad204488 1139 err = ovl_make_workdir(ofs, &workpath);
bca44b52
MS
1140 if (err)
1141 goto out;
1142
520d7c86
MS
1143 err = 0;
1144out:
bca44b52
MS
1145 path_put(&workpath);
1146
520d7c86
MS
1147 return err;
1148}
1149
ad204488 1150static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,
95e6d417 1151 struct path *upperpath)
f7e3a7d9 1152{
2ba9d57e 1153 struct vfsmount *mnt = ofs->upper_mnt;
f7e3a7d9
MS
1154 int err;
1155
2ba9d57e
AG
1156 err = mnt_want_write(mnt);
1157 if (err)
1158 return err;
1159
f7e3a7d9 1160 /* Verify lower root is upper root origin */
d9768076 1161 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
05122443 1162 true);
f7e3a7d9
MS
1163 if (err) {
1164 pr_err("overlayfs: failed to verify upper root origin\n");
1165 goto out;
1166 }
1167
ad204488
MS
1168 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1169 if (ofs->indexdir) {
ad1d615c
AG
1170 /*
1171 * Verify upper root is exclusively associated with index dir.
1172 * Older kernels stored upper fh in "trusted.overlay.origin"
1173 * xattr. If that xattr exists, verify that it is a match to
1174 * upper dir file handle. In any case, verify or set xattr
1175 * "trusted.overlay.upper" to indicate that index may have
1176 * directory entries.
1177 */
1178 if (ovl_check_origin_xattr(ofs->indexdir)) {
1179 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1180 upperpath->dentry, true, false);
1181 if (err)
1182 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1183 }
1184 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
f7e3a7d9 1185 if (err)
ad1d615c 1186 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
f7e3a7d9
MS
1187
1188 /* Cleanup bad/stale/orphan index entries */
1189 if (!err)
1eff1a1d 1190 err = ovl_indexdir_cleanup(ofs);
f7e3a7d9 1191 }
ad204488 1192 if (err || !ofs->indexdir)
f7e3a7d9
MS
1193 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1194
1195out:
2ba9d57e 1196 mnt_drop_write(mnt);
f7e3a7d9
MS
1197 return err;
1198}
1199
9df085f3
AG
1200static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
1201{
1202 unsigned int i;
1203
1204 if (!ofs->config.nfs_export && !(ofs->config.index && ofs->upper_mnt))
1205 return true;
1206
1207 for (i = 0; i < ofs->numlowerfs; i++) {
1208 /*
1209 * We use uuid to associate an overlay lower file handle with a
1210 * lower layer, so we can accept lower fs with null uuid as long
1211 * as all lower layers with null uuid are on the same fs.
1212 */
1213 if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid))
1214 return false;
1215 }
1216 return true;
1217}
1218
5148626b 1219/* Get a unique fsid for the layer */
9df085f3 1220static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
5148626b 1221{
9df085f3 1222 struct super_block *sb = path->mnt->mnt_sb;
5148626b
AG
1223 unsigned int i;
1224 dev_t dev;
1225 int err;
1226
1227 /* fsid 0 is reserved for upper fs even with non upper overlay */
1228 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1229 return 0;
1230
1231 for (i = 0; i < ofs->numlowerfs; i++) {
1232 if (ofs->lower_fs[i].sb == sb)
1233 return i + 1;
1234 }
1235
9df085f3
AG
1236 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1237 ofs->config.index = false;
1238 ofs->config.nfs_export = false;
1239 pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1240 uuid_is_null(&sb->s_uuid) ? "null" : "conflicting",
1241 path->dentry);
1242 }
1243
5148626b
AG
1244 err = get_anon_bdev(&dev);
1245 if (err) {
1246 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1247 return err;
1248 }
1249
1250 ofs->lower_fs[ofs->numlowerfs].sb = sb;
1251 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1252 ofs->numlowerfs++;
1253
1254 return ofs->numlowerfs;
1255}
1256
ad204488 1257static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack,
520d7c86
MS
1258 unsigned int numlower)
1259{
1260 int err;
1261 unsigned int i;
1262
1263 err = -ENOMEM;
ad204488 1264 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
520d7c86 1265 GFP_KERNEL);
ad204488 1266 if (ofs->lower_layers == NULL)
520d7c86 1267 goto out;
5148626b
AG
1268
1269 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1270 GFP_KERNEL);
1271 if (ofs->lower_fs == NULL)
1272 goto out;
1273
520d7c86
MS
1274 for (i = 0; i < numlower; i++) {
1275 struct vfsmount *mnt;
5148626b 1276 int fsid;
520d7c86 1277
9df085f3 1278 err = fsid = ovl_get_fsid(ofs, &stack[i]);
5148626b 1279 if (err < 0)
520d7c86 1280 goto out;
520d7c86
MS
1281
1282 mnt = clone_private_mount(&stack[i]);
1283 err = PTR_ERR(mnt);
1284 if (IS_ERR(mnt)) {
1285 pr_err("overlayfs: failed to clone lowerpath\n");
520d7c86
MS
1286 goto out;
1287 }
5148626b 1288
520d7c86
MS
1289 /*
1290 * Make lower layers R/O. That way fchmod/fchown on lower file
1291 * will fail instead of modifying lower fs.
1292 */
1293 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1294
ad204488 1295 ofs->lower_layers[ofs->numlower].mnt = mnt;
d583ed7d 1296 ofs->lower_layers[ofs->numlower].idx = i + 1;
5148626b
AG
1297 ofs->lower_layers[ofs->numlower].fsid = fsid;
1298 if (fsid) {
1299 ofs->lower_layers[ofs->numlower].fs =
1300 &ofs->lower_fs[fsid - 1];
1301 }
ad204488 1302 ofs->numlower++;
520d7c86 1303 }
e487d889 1304
795939a9
AG
1305 /*
1306 * When all layers on same fs, overlay can use real inode numbers.
1307 * With mount option "xino=on", mounter declares that there are enough
1308 * free high bits in underlying fs to hold the unique fsid.
1309 * If overlayfs does encounter underlying inodes using the high xino
1310 * bits reserved for fsid, it emits a warning and uses the original
1311 * inode number.
1312 */
1313 if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
e487d889 1314 ofs->xino_bits = 0;
795939a9
AG
1315 ofs->config.xino = OVL_XINO_OFF;
1316 } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1317 /*
1318 * This is a roundup of number of bits needed for numlowerfs+1
1319 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
1320 * upper fs even with non upper overlay.
1321 */
1322 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1323 ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1324 }
1325
1326 if (ofs->xino_bits) {
1327 pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1328 ofs->xino_bits);
1329 }
e487d889 1330
520d7c86
MS
1331 err = 0;
1332out:
1333 return err;
1334}
1335
4155c10a 1336static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
ad204488 1337 struct ovl_fs *ofs)
53dbb0b4
MS
1338{
1339 int err;
1340 char *lowertmp, *lower;
4155c10a
MS
1341 struct path *stack = NULL;
1342 unsigned int stacklen, numlower = 0, i;
53dbb0b4 1343 bool remote = false;
4155c10a 1344 struct ovl_entry *oe;
53dbb0b4
MS
1345
1346 err = -ENOMEM;
ad204488 1347 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
53dbb0b4 1348 if (!lowertmp)
4155c10a 1349 goto out_err;
53dbb0b4
MS
1350
1351 err = -EINVAL;
1352 stacklen = ovl_split_lowerdirs(lowertmp);
1353 if (stacklen > OVL_MAX_STACK) {
1354 pr_err("overlayfs: too many lower directories, limit is %d\n",
1355 OVL_MAX_STACK);
4155c10a 1356 goto out_err;
ad204488 1357 } else if (!ofs->config.upperdir && stacklen == 1) {
53dbb0b4 1358 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
4155c10a 1359 goto out_err;
f168f109
AG
1360 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1361 ofs->config.redirect_follow) {
1362 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1363 ofs->config.nfs_export = false;
53dbb0b4
MS
1364 }
1365
1366 err = -ENOMEM;
1367 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1368 if (!stack)
4155c10a 1369 goto out_err;
53dbb0b4
MS
1370
1371 err = -EINVAL;
1372 lower = lowertmp;
1373 for (numlower = 0; numlower < stacklen; numlower++) {
ad204488 1374 err = ovl_lower_dir(lower, &stack[numlower], ofs,
53dbb0b4
MS
1375 &sb->s_stack_depth, &remote);
1376 if (err)
4155c10a 1377 goto out_err;
53dbb0b4
MS
1378
1379 lower = strchr(lower, '\0') + 1;
1380 }
1381
1382 err = -EINVAL;
1383 sb->s_stack_depth++;
1384 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1385 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
4155c10a 1386 goto out_err;
53dbb0b4
MS
1387 }
1388
ad204488 1389 err = ovl_get_lower_layers(ofs, stack, numlower);
4155c10a
MS
1390 if (err)
1391 goto out_err;
1392
1393 err = -ENOMEM;
1394 oe = ovl_alloc_entry(numlower);
1395 if (!oe)
1396 goto out_err;
1397
1398 for (i = 0; i < numlower; i++) {
1399 oe->lowerstack[i].dentry = dget(stack[i].dentry);
ad204488 1400 oe->lowerstack[i].layer = &ofs->lower_layers[i];
4155c10a 1401 }
53dbb0b4
MS
1402
1403 if (remote)
1404 sb->s_d_op = &ovl_reval_dentry_operations;
1405 else
1406 sb->s_d_op = &ovl_dentry_operations;
1407
53dbb0b4 1408out:
53dbb0b4
MS
1409 for (i = 0; i < numlower; i++)
1410 path_put(&stack[i]);
1411 kfree(stack);
4155c10a
MS
1412 kfree(lowertmp);
1413
1414 return oe;
1415
1416out_err:
1417 oe = ERR_PTR(err);
53dbb0b4
MS
1418 goto out;
1419}
1420
e9be9d5e
MS
1421static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1422{
33006cdf 1423 struct path upperpath = { };
e9be9d5e 1424 struct dentry *root_dentry;
4155c10a 1425 struct ovl_entry *oe;
ad204488 1426 struct ovl_fs *ofs;
51f8f3c4 1427 struct cred *cred;
e9be9d5e
MS
1428 int err;
1429
f45827e8 1430 err = -ENOMEM;
ad204488
MS
1431 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1432 if (!ofs)
e9be9d5e
MS
1433 goto out;
1434
ad204488 1435 ofs->creator_cred = cred = prepare_creds();
c6fe6254
MS
1436 if (!cred)
1437 goto out_err;
1438
ad204488 1439 ofs->config.index = ovl_index_def;
f168f109 1440 ofs->config.nfs_export = ovl_nfs_export_def;
795939a9 1441 ofs->config.xino = ovl_xino_def();
d5791044 1442 ofs->config.metacopy = ovl_metacopy_def;
ad204488 1443 err = ovl_parse_opt((char *) data, &ofs->config);
f45827e8 1444 if (err)
a9075cdb 1445 goto out_err;
f45827e8 1446
e9be9d5e 1447 err = -EINVAL;
ad204488 1448 if (!ofs->config.lowerdir) {
07f2af7b
KK
1449 if (!silent)
1450 pr_err("overlayfs: missing 'lowerdir'\n");
a9075cdb 1451 goto out_err;
e9be9d5e
MS
1452 }
1453
53a08cb9 1454 sb->s_stack_depth = 0;
cf9a6784 1455 sb->s_maxbytes = MAX_LFS_FILESIZE;
e487d889 1456 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
795939a9
AG
1457 if (ofs->config.xino != OVL_XINO_OFF)
1458 ofs->xino_bits = BITS_PER_LONG - 32;
1459
ad204488
MS
1460 if (ofs->config.upperdir) {
1461 if (!ofs->config.workdir) {
53a08cb9 1462 pr_err("overlayfs: missing 'workdir'\n");
a9075cdb 1463 goto out_err;
53a08cb9 1464 }
e9be9d5e 1465
ad204488 1466 err = ovl_get_upper(ofs, &upperpath);
53a08cb9 1467 if (err)
a9075cdb 1468 goto out_err;
2cac0c00 1469
ad204488 1470 err = ovl_get_workdir(ofs, &upperpath);
8ed61dc3 1471 if (err)
a9075cdb 1472 goto out_err;
c6fe6254 1473
ad204488 1474 if (!ofs->workdir)
1751e8a6 1475 sb->s_flags |= SB_RDONLY;
6e88256e 1476
ad204488
MS
1477 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1478 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
c6fe6254 1479
e9be9d5e 1480 }
ad204488 1481 oe = ovl_get_lowerstack(sb, ofs);
4155c10a
MS
1482 err = PTR_ERR(oe);
1483 if (IS_ERR(oe))
a9075cdb 1484 goto out_err;
e9be9d5e 1485
71cbad7e 1486 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
ad204488 1487 if (!ofs->upper_mnt)
1751e8a6 1488 sb->s_flags |= SB_RDONLY;
e9be9d5e 1489
ad204488
MS
1490 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1491 err = ovl_get_indexdir(ofs, oe, &upperpath);
54fb347e 1492 if (err)
4155c10a 1493 goto out_free_oe;
6e88256e 1494
972d0093
AG
1495 /* Force r/o mount with no index dir */
1496 if (!ofs->indexdir) {
1497 dput(ofs->workdir);
1498 ofs->workdir = NULL;
1751e8a6 1499 sb->s_flags |= SB_RDONLY;
972d0093
AG
1500 }
1501
02bcd157
AG
1502 }
1503
972d0093 1504 /* Show index=off in /proc/mounts for forced r/o mount */
f168f109 1505 if (!ofs->indexdir) {
ad204488 1506 ofs->config.index = false;
f168f109
AG
1507 if (ofs->upper_mnt && ofs->config.nfs_export) {
1508 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1509 ofs->config.nfs_export = false;
1510 }
1511 }
02bcd157 1512
d5791044
VG
1513 if (ofs->config.metacopy && ofs->config.nfs_export) {
1514 pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1515 ofs->config.nfs_export = false;
1516 }
1517
8383f174
AG
1518 if (ofs->config.nfs_export)
1519 sb->s_export_op = &ovl_export_operations;
1520
51f8f3c4
KK
1521 /* Never override disk quota limits or use reserved space */
1522 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1523
655042cc
VG
1524 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1525 sb->s_op = &ovl_super_operations;
1526 sb->s_xattr = ovl_xattr_handlers;
ad204488 1527 sb->s_fs_info = ofs;
de2a4a50 1528 sb->s_flags |= SB_POSIXACL;
655042cc 1529
c6fe6254 1530 err = -ENOMEM;
ca4c8a3a 1531 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
e9be9d5e 1532 if (!root_dentry)
4155c10a 1533 goto out_free_oe;
e9be9d5e 1534
c62520a8
AG
1535 root_dentry->d_fsdata = oe;
1536
e9be9d5e 1537 mntput(upperpath.mnt);
f3a15685 1538 if (upperpath.dentry) {
c62520a8 1539 ovl_dentry_set_upper_alias(root_dentry);
13c72075
MS
1540 if (ovl_is_impuredir(upperpath.dentry))
1541 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
f3a15685 1542 }
e9be9d5e 1543
b79e05aa
AG
1544 /* Root is always merge -> can have whiteouts */
1545 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
2ca3c148 1546 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
0c288874 1547 ovl_set_upperdata(d_inode(root_dentry));
09d8b586 1548 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
2664bd08 1549 ovl_dentry_lower(root_dentry), NULL);
ed06e069 1550
e9be9d5e 1551 sb->s_root = root_dentry;
e9be9d5e
MS
1552
1553 return 0;
1554
4155c10a
MS
1555out_free_oe:
1556 ovl_entry_stack_free(oe);
b9343632 1557 kfree(oe);
4155c10a 1558out_err:
e9be9d5e 1559 path_put(&upperpath);
ad204488 1560 ovl_free_fs(ofs);
e9be9d5e
MS
1561out:
1562 return err;
1563}
1564
1565static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1566 const char *dev_name, void *raw_data)
1567{
1568 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1569}
1570
1571static struct file_system_type ovl_fs_type = {
1572 .owner = THIS_MODULE,
ef94b186 1573 .name = "overlay",
e9be9d5e
MS
1574 .mount = ovl_mount,
1575 .kill_sb = kill_anon_super,
1576};
ef94b186 1577MODULE_ALIAS_FS("overlay");
e9be9d5e 1578
13cf199d
AG
1579static void ovl_inode_init_once(void *foo)
1580{
1581 struct ovl_inode *oi = foo;
1582
1583 inode_init_once(&oi->vfs_inode);
1584}
1585
e9be9d5e
MS
1586static int __init ovl_init(void)
1587{
13cf199d
AG
1588 int err;
1589
1590 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1591 sizeof(struct ovl_inode), 0,
1592 (SLAB_RECLAIM_ACCOUNT|
1593 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1594 ovl_inode_init_once);
1595 if (ovl_inode_cachep == NULL)
1596 return -ENOMEM;
1597
1598 err = register_filesystem(&ovl_fs_type);
1599 if (err)
1600 kmem_cache_destroy(ovl_inode_cachep);
1601
1602 return err;
e9be9d5e
MS
1603}
1604
1605static void __exit ovl_exit(void)
1606{
1607 unregister_filesystem(&ovl_fs_type);
13cf199d
AG
1608
1609 /*
1610 * Make sure all delayed rcu free inodes are flushed before we
1611 * destroy cache.
1612 */
1613 rcu_barrier();
1614 kmem_cache_destroy(ovl_inode_cachep);
1615
e9be9d5e
MS
1616}
1617
1618module_init(ovl_init);
1619module_exit(ovl_exit);