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