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