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