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