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