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