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