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