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