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