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> |
| 13 | #include <linux/security.h> |
| 14 | #include <linux/mount.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/parser.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/sched.h> |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 19 | #include <linux/statfs.h> |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 21 | #include "overlayfs.h" |
| 22 | |
| 23 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 24 | MODULE_DESCRIPTION("Overlay filesystem"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 27 | #define OVERLAYFS_SUPER_MAGIC 0x794c7630 |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 28 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 29 | struct ovl_config { |
| 30 | char *lowerdir; |
| 31 | char *upperdir; |
| 32 | char *workdir; |
| 33 | }; |
| 34 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 35 | /* private information held for overlayfs's superblock */ |
| 36 | struct ovl_fs { |
| 37 | struct vfsmount *upper_mnt; |
| 38 | struct vfsmount *lower_mnt; |
| 39 | struct dentry *workdir; |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 40 | long lower_namelen; |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 41 | /* pathnames of lower and upper dirs, for show_options */ |
| 42 | struct ovl_config config; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct ovl_dir_cache; |
| 46 | |
| 47 | /* private information held for every overlayfs dentry */ |
| 48 | struct ovl_entry { |
| 49 | struct dentry *__upperdentry; |
| 50 | struct dentry *lowerdentry; |
| 51 | struct ovl_dir_cache *cache; |
| 52 | union { |
| 53 | struct { |
| 54 | u64 version; |
| 55 | bool opaque; |
| 56 | }; |
| 57 | struct rcu_head rcu; |
| 58 | }; |
| 59 | }; |
| 60 | |
| 61 | const char *ovl_opaque_xattr = "trusted.overlay.opaque"; |
| 62 | |
| 63 | |
| 64 | enum ovl_path_type ovl_path_type(struct dentry *dentry) |
| 65 | { |
| 66 | struct ovl_entry *oe = dentry->d_fsdata; |
| 67 | |
| 68 | if (oe->__upperdentry) { |
| 69 | if (oe->lowerdentry) { |
| 70 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 71 | return OVL_PATH_MERGE; |
| 72 | else |
| 73 | return OVL_PATH_UPPER; |
| 74 | } else { |
| 75 | if (oe->opaque) |
| 76 | return OVL_PATH_UPPER; |
| 77 | else |
| 78 | return OVL_PATH_PURE_UPPER; |
| 79 | } |
| 80 | } else { |
| 81 | return OVL_PATH_LOWER; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) |
| 86 | { |
Miklos Szeredi | 71d5092 | 2014-11-20 16:40:01 +0100 | [diff] [blame] | 87 | return lockless_dereference(oe->__upperdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void ovl_path_upper(struct dentry *dentry, struct path *path) |
| 91 | { |
| 92 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 93 | struct ovl_entry *oe = dentry->d_fsdata; |
| 94 | |
| 95 | path->mnt = ofs->upper_mnt; |
| 96 | path->dentry = ovl_upperdentry_dereference(oe); |
| 97 | } |
| 98 | |
| 99 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) |
| 100 | { |
| 101 | |
| 102 | enum ovl_path_type type = ovl_path_type(dentry); |
| 103 | |
| 104 | if (type == OVL_PATH_LOWER) |
| 105 | ovl_path_lower(dentry, path); |
| 106 | else |
| 107 | ovl_path_upper(dentry, path); |
| 108 | |
| 109 | return type; |
| 110 | } |
| 111 | |
| 112 | struct dentry *ovl_dentry_upper(struct dentry *dentry) |
| 113 | { |
| 114 | struct ovl_entry *oe = dentry->d_fsdata; |
| 115 | |
| 116 | return ovl_upperdentry_dereference(oe); |
| 117 | } |
| 118 | |
| 119 | struct dentry *ovl_dentry_lower(struct dentry *dentry) |
| 120 | { |
| 121 | struct ovl_entry *oe = dentry->d_fsdata; |
| 122 | |
| 123 | return oe->lowerdentry; |
| 124 | } |
| 125 | |
| 126 | struct dentry *ovl_dentry_real(struct dentry *dentry) |
| 127 | { |
| 128 | struct ovl_entry *oe = dentry->d_fsdata; |
| 129 | struct dentry *realdentry; |
| 130 | |
| 131 | realdentry = ovl_upperdentry_dereference(oe); |
| 132 | if (!realdentry) |
| 133 | realdentry = oe->lowerdentry; |
| 134 | |
| 135 | return realdentry; |
| 136 | } |
| 137 | |
| 138 | struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper) |
| 139 | { |
| 140 | struct dentry *realdentry; |
| 141 | |
| 142 | realdentry = ovl_upperdentry_dereference(oe); |
| 143 | if (realdentry) { |
| 144 | *is_upper = true; |
| 145 | } else { |
| 146 | realdentry = oe->lowerdentry; |
| 147 | *is_upper = false; |
| 148 | } |
| 149 | return realdentry; |
| 150 | } |
| 151 | |
| 152 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) |
| 153 | { |
| 154 | struct ovl_entry *oe = dentry->d_fsdata; |
| 155 | |
| 156 | return oe->cache; |
| 157 | } |
| 158 | |
| 159 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) |
| 160 | { |
| 161 | struct ovl_entry *oe = dentry->d_fsdata; |
| 162 | |
| 163 | oe->cache = cache; |
| 164 | } |
| 165 | |
| 166 | void ovl_path_lower(struct dentry *dentry, struct path *path) |
| 167 | { |
| 168 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 169 | struct ovl_entry *oe = dentry->d_fsdata; |
| 170 | |
| 171 | path->mnt = ofs->lower_mnt; |
| 172 | path->dentry = oe->lowerdentry; |
| 173 | } |
| 174 | |
| 175 | int ovl_want_write(struct dentry *dentry) |
| 176 | { |
| 177 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 178 | return mnt_want_write(ofs->upper_mnt); |
| 179 | } |
| 180 | |
| 181 | void ovl_drop_write(struct dentry *dentry) |
| 182 | { |
| 183 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 184 | mnt_drop_write(ofs->upper_mnt); |
| 185 | } |
| 186 | |
| 187 | struct dentry *ovl_workdir(struct dentry *dentry) |
| 188 | { |
| 189 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 190 | return ofs->workdir; |
| 191 | } |
| 192 | |
| 193 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
| 194 | { |
| 195 | struct ovl_entry *oe = dentry->d_fsdata; |
| 196 | return oe->opaque; |
| 197 | } |
| 198 | |
| 199 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) |
| 200 | { |
| 201 | struct ovl_entry *oe = dentry->d_fsdata; |
| 202 | oe->opaque = opaque; |
| 203 | } |
| 204 | |
| 205 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) |
| 206 | { |
| 207 | struct ovl_entry *oe = dentry->d_fsdata; |
| 208 | |
| 209 | WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex)); |
| 210 | WARN_ON(oe->__upperdentry); |
| 211 | BUG_ON(!upperdentry->d_inode); |
| 212 | /* |
| 213 | * Make sure upperdentry is consistent before making it visible to |
| 214 | * ovl_upperdentry_dereference(). |
| 215 | */ |
| 216 | smp_wmb(); |
| 217 | oe->__upperdentry = upperdentry; |
| 218 | } |
| 219 | |
| 220 | void ovl_dentry_version_inc(struct dentry *dentry) |
| 221 | { |
| 222 | struct ovl_entry *oe = dentry->d_fsdata; |
| 223 | |
| 224 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); |
| 225 | oe->version++; |
| 226 | } |
| 227 | |
| 228 | u64 ovl_dentry_version_get(struct dentry *dentry) |
| 229 | { |
| 230 | struct ovl_entry *oe = dentry->d_fsdata; |
| 231 | |
| 232 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); |
| 233 | return oe->version; |
| 234 | } |
| 235 | |
| 236 | bool ovl_is_whiteout(struct dentry *dentry) |
| 237 | { |
| 238 | struct inode *inode = dentry->d_inode; |
| 239 | |
| 240 | return inode && IS_WHITEOUT(inode); |
| 241 | } |
| 242 | |
| 243 | static bool ovl_is_opaquedir(struct dentry *dentry) |
| 244 | { |
| 245 | int res; |
| 246 | char val; |
| 247 | struct inode *inode = dentry->d_inode; |
| 248 | |
| 249 | if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) |
| 250 | return false; |
| 251 | |
| 252 | res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1); |
| 253 | if (res == 1 && val == 'y') |
| 254 | return true; |
| 255 | |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | static void ovl_dentry_release(struct dentry *dentry) |
| 260 | { |
| 261 | struct ovl_entry *oe = dentry->d_fsdata; |
| 262 | |
| 263 | if (oe) { |
| 264 | dput(oe->__upperdentry); |
| 265 | dput(oe->lowerdentry); |
| 266 | kfree_rcu(oe, rcu); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static const struct dentry_operations ovl_dentry_operations = { |
| 271 | .d_release = ovl_dentry_release, |
| 272 | }; |
| 273 | |
| 274 | static struct ovl_entry *ovl_alloc_entry(void) |
| 275 | { |
| 276 | return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL); |
| 277 | } |
| 278 | |
| 279 | static inline struct dentry *ovl_lookup_real(struct dentry *dir, |
| 280 | struct qstr *name) |
| 281 | { |
| 282 | struct dentry *dentry; |
| 283 | |
| 284 | mutex_lock(&dir->d_inode->i_mutex); |
| 285 | dentry = lookup_one_len(name->name, dir, name->len); |
| 286 | mutex_unlock(&dir->d_inode->i_mutex); |
| 287 | |
| 288 | if (IS_ERR(dentry)) { |
| 289 | if (PTR_ERR(dentry) == -ENOENT) |
| 290 | dentry = NULL; |
| 291 | } else if (!dentry->d_inode) { |
| 292 | dput(dentry); |
| 293 | dentry = NULL; |
| 294 | } |
| 295 | return dentry; |
| 296 | } |
| 297 | |
| 298 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
| 299 | unsigned int flags) |
| 300 | { |
| 301 | struct ovl_entry *oe; |
| 302 | struct dentry *upperdir; |
| 303 | struct dentry *lowerdir; |
| 304 | struct dentry *upperdentry = NULL; |
| 305 | struct dentry *lowerdentry = NULL; |
| 306 | struct inode *inode = NULL; |
| 307 | int err; |
| 308 | |
| 309 | err = -ENOMEM; |
| 310 | oe = ovl_alloc_entry(); |
| 311 | if (!oe) |
| 312 | goto out; |
| 313 | |
| 314 | upperdir = ovl_dentry_upper(dentry->d_parent); |
| 315 | lowerdir = ovl_dentry_lower(dentry->d_parent); |
| 316 | |
| 317 | if (upperdir) { |
| 318 | upperdentry = ovl_lookup_real(upperdir, &dentry->d_name); |
| 319 | err = PTR_ERR(upperdentry); |
| 320 | if (IS_ERR(upperdentry)) |
| 321 | goto out_put_dir; |
| 322 | |
| 323 | if (lowerdir && upperdentry) { |
| 324 | if (ovl_is_whiteout(upperdentry)) { |
| 325 | dput(upperdentry); |
| 326 | upperdentry = NULL; |
| 327 | oe->opaque = true; |
| 328 | } else if (ovl_is_opaquedir(upperdentry)) { |
| 329 | oe->opaque = true; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | if (lowerdir && !oe->opaque) { |
| 334 | lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name); |
| 335 | err = PTR_ERR(lowerdentry); |
| 336 | if (IS_ERR(lowerdentry)) |
| 337 | goto out_dput_upper; |
| 338 | } |
| 339 | |
| 340 | if (lowerdentry && upperdentry && |
| 341 | (!S_ISDIR(upperdentry->d_inode->i_mode) || |
| 342 | !S_ISDIR(lowerdentry->d_inode->i_mode))) { |
| 343 | dput(lowerdentry); |
| 344 | lowerdentry = NULL; |
| 345 | oe->opaque = true; |
| 346 | } |
| 347 | |
| 348 | if (lowerdentry || upperdentry) { |
| 349 | struct dentry *realdentry; |
| 350 | |
| 351 | realdentry = upperdentry ? upperdentry : lowerdentry; |
| 352 | err = -ENOMEM; |
| 353 | inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, |
| 354 | oe); |
| 355 | if (!inode) |
| 356 | goto out_dput; |
| 357 | ovl_copyattr(realdentry->d_inode, inode); |
| 358 | } |
| 359 | |
| 360 | oe->__upperdentry = upperdentry; |
| 361 | oe->lowerdentry = lowerdentry; |
| 362 | |
| 363 | dentry->d_fsdata = oe; |
| 364 | d_add(dentry, inode); |
| 365 | |
| 366 | return NULL; |
| 367 | |
| 368 | out_dput: |
| 369 | dput(lowerdentry); |
| 370 | out_dput_upper: |
| 371 | dput(upperdentry); |
| 372 | out_put_dir: |
| 373 | kfree(oe); |
| 374 | out: |
| 375 | return ERR_PTR(err); |
| 376 | } |
| 377 | |
| 378 | struct file *ovl_path_open(struct path *path, int flags) |
| 379 | { |
| 380 | return dentry_open(path, flags, current_cred()); |
| 381 | } |
| 382 | |
| 383 | static void ovl_put_super(struct super_block *sb) |
| 384 | { |
| 385 | struct ovl_fs *ufs = sb->s_fs_info; |
| 386 | |
| 387 | dput(ufs->workdir); |
| 388 | mntput(ufs->upper_mnt); |
| 389 | mntput(ufs->lower_mnt); |
| 390 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 391 | kfree(ufs->config.lowerdir); |
| 392 | kfree(ufs->config.upperdir); |
| 393 | kfree(ufs->config.workdir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 394 | kfree(ufs); |
| 395 | } |
| 396 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 397 | /** |
| 398 | * ovl_statfs |
| 399 | * @sb: The overlayfs super block |
| 400 | * @buf: The struct kstatfs to fill in with stats |
| 401 | * |
| 402 | * Get the filesystem statistics. As writes always target the upper layer |
| 403 | * filesystem pass the statfs to the same filesystem. |
| 404 | */ |
| 405 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 406 | { |
| 407 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 408 | struct dentry *root_dentry = dentry->d_sb->s_root; |
| 409 | struct path path; |
| 410 | int err; |
| 411 | |
| 412 | ovl_path_upper(root_dentry, &path); |
| 413 | |
| 414 | err = vfs_statfs(&path, buf); |
| 415 | if (!err) { |
| 416 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); |
| 417 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
| 418 | } |
| 419 | |
| 420 | return err; |
| 421 | } |
| 422 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 423 | /** |
| 424 | * ovl_show_options |
| 425 | * |
| 426 | * Prints the mount options for a given superblock. |
| 427 | * Returns zero; does not fail. |
| 428 | */ |
| 429 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) |
| 430 | { |
| 431 | struct super_block *sb = dentry->d_sb; |
| 432 | struct ovl_fs *ufs = sb->s_fs_info; |
| 433 | |
| 434 | seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir); |
| 435 | seq_printf(m, ",upperdir=%s", ufs->config.upperdir); |
| 436 | seq_printf(m, ",workdir=%s", ufs->config.workdir); |
| 437 | return 0; |
| 438 | } |
| 439 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 440 | static const struct super_operations ovl_super_operations = { |
| 441 | .put_super = ovl_put_super, |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 442 | .statfs = ovl_statfs, |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 443 | .show_options = ovl_show_options, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | enum { |
| 447 | OPT_LOWERDIR, |
| 448 | OPT_UPPERDIR, |
| 449 | OPT_WORKDIR, |
| 450 | OPT_ERR, |
| 451 | }; |
| 452 | |
| 453 | static const match_table_t ovl_tokens = { |
| 454 | {OPT_LOWERDIR, "lowerdir=%s"}, |
| 455 | {OPT_UPPERDIR, "upperdir=%s"}, |
| 456 | {OPT_WORKDIR, "workdir=%s"}, |
| 457 | {OPT_ERR, NULL} |
| 458 | }; |
| 459 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 460 | static char *ovl_next_opt(char **s) |
| 461 | { |
| 462 | char *sbegin = *s; |
| 463 | char *p; |
| 464 | |
| 465 | if (sbegin == NULL) |
| 466 | return NULL; |
| 467 | |
| 468 | for (p = sbegin; *p; p++) { |
| 469 | if (*p == '\\') { |
| 470 | p++; |
| 471 | if (!*p) |
| 472 | break; |
| 473 | } else if (*p == ',') { |
| 474 | *p = '\0'; |
| 475 | *s = p + 1; |
| 476 | return sbegin; |
| 477 | } |
| 478 | } |
| 479 | *s = NULL; |
| 480 | return sbegin; |
| 481 | } |
| 482 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 483 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
| 484 | { |
| 485 | char *p; |
| 486 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 487 | while ((p = ovl_next_opt(&opt)) != NULL) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 488 | int token; |
| 489 | substring_t args[MAX_OPT_ARGS]; |
| 490 | |
| 491 | if (!*p) |
| 492 | continue; |
| 493 | |
| 494 | token = match_token(p, ovl_tokens, args); |
| 495 | switch (token) { |
| 496 | case OPT_UPPERDIR: |
| 497 | kfree(config->upperdir); |
| 498 | config->upperdir = match_strdup(&args[0]); |
| 499 | if (!config->upperdir) |
| 500 | return -ENOMEM; |
| 501 | break; |
| 502 | |
| 503 | case OPT_LOWERDIR: |
| 504 | kfree(config->lowerdir); |
| 505 | config->lowerdir = match_strdup(&args[0]); |
| 506 | if (!config->lowerdir) |
| 507 | return -ENOMEM; |
| 508 | break; |
| 509 | |
| 510 | case OPT_WORKDIR: |
| 511 | kfree(config->workdir); |
| 512 | config->workdir = match_strdup(&args[0]); |
| 513 | if (!config->workdir) |
| 514 | return -ENOMEM; |
| 515 | break; |
| 516 | |
| 517 | default: |
| 518 | return -EINVAL; |
| 519 | } |
| 520 | } |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | #define OVL_WORKDIR_NAME "work" |
| 525 | |
| 526 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, |
| 527 | struct dentry *dentry) |
| 528 | { |
| 529 | struct inode *dir = dentry->d_inode; |
| 530 | struct dentry *work; |
| 531 | int err; |
| 532 | bool retried = false; |
| 533 | |
| 534 | err = mnt_want_write(mnt); |
| 535 | if (err) |
| 536 | return ERR_PTR(err); |
| 537 | |
| 538 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 539 | retry: |
| 540 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, |
| 541 | strlen(OVL_WORKDIR_NAME)); |
| 542 | |
| 543 | if (!IS_ERR(work)) { |
| 544 | struct kstat stat = { |
| 545 | .mode = S_IFDIR | 0, |
| 546 | }; |
| 547 | |
| 548 | if (work->d_inode) { |
| 549 | err = -EEXIST; |
| 550 | if (retried) |
| 551 | goto out_dput; |
| 552 | |
| 553 | retried = true; |
| 554 | ovl_cleanup(dir, work); |
| 555 | dput(work); |
| 556 | goto retry; |
| 557 | } |
| 558 | |
| 559 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); |
| 560 | if (err) |
| 561 | goto out_dput; |
| 562 | } |
| 563 | out_unlock: |
| 564 | mutex_unlock(&dir->i_mutex); |
| 565 | mnt_drop_write(mnt); |
| 566 | |
| 567 | return work; |
| 568 | |
| 569 | out_dput: |
| 570 | dput(work); |
| 571 | work = ERR_PTR(err); |
| 572 | goto out_unlock; |
| 573 | } |
| 574 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 575 | static void ovl_unescape(char *s) |
| 576 | { |
| 577 | char *d = s; |
| 578 | |
| 579 | for (;; s++, d++) { |
| 580 | if (*s == '\\') |
| 581 | s++; |
| 582 | *d = *s; |
| 583 | if (!*s) |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 588 | static int ovl_mount_dir(const char *name, struct path *path) |
| 589 | { |
| 590 | int err; |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 591 | char *tmp = kstrdup(name, GFP_KERNEL); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 592 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 593 | if (!tmp) |
| 594 | return -ENOMEM; |
| 595 | |
| 596 | ovl_unescape(tmp); |
| 597 | err = kern_path(tmp, LOOKUP_FOLLOW, path); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 598 | if (err) { |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 599 | pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 600 | err = -EINVAL; |
| 601 | } |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 602 | kfree(tmp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 603 | return err; |
| 604 | } |
| 605 | |
| 606 | static bool ovl_is_allowed_fs_type(struct dentry *root) |
| 607 | { |
| 608 | const struct dentry_operations *dop = root->d_op; |
| 609 | |
| 610 | /* |
| 611 | * We don't support: |
| 612 | * - automount filesystems |
| 613 | * - filesystems with revalidate (FIXME for lower layer) |
| 614 | * - filesystems with case insensitive names |
| 615 | */ |
| 616 | if (dop && |
| 617 | (dop->d_manage || dop->d_automount || |
| 618 | dop->d_revalidate || dop->d_weak_revalidate || |
| 619 | dop->d_compare || dop->d_hash)) { |
| 620 | return false; |
| 621 | } |
| 622 | return true; |
| 623 | } |
| 624 | |
| 625 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 626 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) |
| 627 | { |
| 628 | bool ok = false; |
| 629 | |
| 630 | if (workdir != upperdir) { |
| 631 | ok = (lock_rename(workdir, upperdir) == NULL); |
| 632 | unlock_rename(workdir, upperdir); |
| 633 | } |
| 634 | return ok; |
| 635 | } |
| 636 | |
| 637 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
| 638 | { |
| 639 | struct path lowerpath; |
| 640 | struct path upperpath; |
| 641 | struct path workpath; |
| 642 | struct inode *root_inode; |
| 643 | struct dentry *root_dentry; |
| 644 | struct ovl_entry *oe; |
| 645 | struct ovl_fs *ufs; |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 646 | struct kstatfs statfs; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 647 | int err; |
| 648 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 649 | err = -ENOMEM; |
| 650 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
| 651 | if (!ufs) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 652 | goto out; |
| 653 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 654 | err = ovl_parse_opt((char *) data, &ufs->config); |
| 655 | if (err) |
| 656 | goto out_free_config; |
| 657 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 658 | /* FIXME: workdir is not needed for a R/O mount */ |
| 659 | err = -EINVAL; |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 660 | if (!ufs->config.upperdir || !ufs->config.lowerdir || |
| 661 | !ufs->config.workdir) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 662 | pr_err("overlayfs: missing upperdir or lowerdir or workdir\n"); |
| 663 | goto out_free_config; |
| 664 | } |
| 665 | |
| 666 | err = -ENOMEM; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 667 | oe = ovl_alloc_entry(); |
| 668 | if (oe == NULL) |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 669 | goto out_free_config; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 670 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 671 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 672 | if (err) |
| 673 | goto out_free_oe; |
| 674 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 675 | err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 676 | if (err) |
| 677 | goto out_put_upperpath; |
| 678 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 679 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 680 | if (err) |
| 681 | goto out_put_lowerpath; |
| 682 | |
| 683 | err = -EINVAL; |
| 684 | if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) || |
| 685 | !S_ISDIR(lowerpath.dentry->d_inode->i_mode) || |
| 686 | !S_ISDIR(workpath.dentry->d_inode->i_mode)) { |
| 687 | pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n"); |
| 688 | goto out_put_workpath; |
| 689 | } |
| 690 | |
| 691 | if (upperpath.mnt != workpath.mnt) { |
| 692 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); |
| 693 | goto out_put_workpath; |
| 694 | } |
| 695 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { |
| 696 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); |
| 697 | goto out_put_workpath; |
| 698 | } |
| 699 | |
| 700 | if (!ovl_is_allowed_fs_type(upperpath.dentry)) { |
| 701 | pr_err("overlayfs: filesystem of upperdir is not supported\n"); |
| 702 | goto out_put_workpath; |
| 703 | } |
| 704 | |
| 705 | if (!ovl_is_allowed_fs_type(lowerpath.dentry)) { |
| 706 | pr_err("overlayfs: filesystem of lowerdir is not supported\n"); |
| 707 | goto out_put_workpath; |
| 708 | } |
| 709 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 710 | err = vfs_statfs(&lowerpath, &statfs); |
| 711 | if (err) { |
| 712 | pr_err("overlayfs: statfs failed on lowerpath\n"); |
| 713 | goto out_put_workpath; |
| 714 | } |
| 715 | ufs->lower_namelen = statfs.f_namelen; |
| 716 | |
Miklos Szeredi | 69c433e | 2014-10-24 00:14:39 +0200 | [diff] [blame] | 717 | sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth, |
| 718 | lowerpath.mnt->mnt_sb->s_stack_depth) + 1; |
| 719 | |
| 720 | err = -EINVAL; |
| 721 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
| 722 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); |
| 723 | goto out_put_workpath; |
| 724 | } |
| 725 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 726 | ufs->upper_mnt = clone_private_mount(&upperpath); |
| 727 | err = PTR_ERR(ufs->upper_mnt); |
| 728 | if (IS_ERR(ufs->upper_mnt)) { |
| 729 | pr_err("overlayfs: failed to clone upperpath\n"); |
| 730 | goto out_put_workpath; |
| 731 | } |
| 732 | |
| 733 | ufs->lower_mnt = clone_private_mount(&lowerpath); |
| 734 | err = PTR_ERR(ufs->lower_mnt); |
| 735 | if (IS_ERR(ufs->lower_mnt)) { |
| 736 | pr_err("overlayfs: failed to clone lowerpath\n"); |
| 737 | goto out_put_upper_mnt; |
| 738 | } |
| 739 | |
| 740 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
| 741 | err = PTR_ERR(ufs->workdir); |
| 742 | if (IS_ERR(ufs->workdir)) { |
| 743 | pr_err("overlayfs: failed to create directory %s/%s\n", |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 744 | ufs->config.workdir, OVL_WORKDIR_NAME); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 745 | goto out_put_lower_mnt; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Make lower_mnt R/O. That way fchmod/fchown on lower file |
| 750 | * will fail instead of modifying lower fs. |
| 751 | */ |
| 752 | ufs->lower_mnt->mnt_flags |= MNT_READONLY; |
| 753 | |
| 754 | /* If the upper fs is r/o, we mark overlayfs r/o too */ |
| 755 | if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY) |
| 756 | sb->s_flags |= MS_RDONLY; |
| 757 | |
| 758 | sb->s_d_op = &ovl_dentry_operations; |
| 759 | |
| 760 | err = -ENOMEM; |
| 761 | root_inode = ovl_new_inode(sb, S_IFDIR, oe); |
| 762 | if (!root_inode) |
| 763 | goto out_put_workdir; |
| 764 | |
| 765 | root_dentry = d_make_root(root_inode); |
| 766 | if (!root_dentry) |
| 767 | goto out_put_workdir; |
| 768 | |
| 769 | mntput(upperpath.mnt); |
| 770 | mntput(lowerpath.mnt); |
| 771 | path_put(&workpath); |
| 772 | |
| 773 | oe->__upperdentry = upperpath.dentry; |
| 774 | oe->lowerdentry = lowerpath.dentry; |
| 775 | |
| 776 | root_dentry->d_fsdata = oe; |
| 777 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 778 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 779 | sb->s_op = &ovl_super_operations; |
| 780 | sb->s_root = root_dentry; |
| 781 | sb->s_fs_info = ufs; |
| 782 | |
| 783 | return 0; |
| 784 | |
| 785 | out_put_workdir: |
| 786 | dput(ufs->workdir); |
| 787 | out_put_lower_mnt: |
| 788 | mntput(ufs->lower_mnt); |
| 789 | out_put_upper_mnt: |
| 790 | mntput(ufs->upper_mnt); |
| 791 | out_put_workpath: |
| 792 | path_put(&workpath); |
| 793 | out_put_lowerpath: |
| 794 | path_put(&lowerpath); |
| 795 | out_put_upperpath: |
| 796 | path_put(&upperpath); |
| 797 | out_free_oe: |
| 798 | kfree(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 799 | out_free_config: |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 800 | kfree(ufs->config.lowerdir); |
| 801 | kfree(ufs->config.upperdir); |
| 802 | kfree(ufs->config.workdir); |
| 803 | kfree(ufs); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 804 | out: |
| 805 | return err; |
| 806 | } |
| 807 | |
| 808 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, |
| 809 | const char *dev_name, void *raw_data) |
| 810 | { |
| 811 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); |
| 812 | } |
| 813 | |
| 814 | static struct file_system_type ovl_fs_type = { |
| 815 | .owner = THIS_MODULE, |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 816 | .name = "overlay", |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 817 | .mount = ovl_mount, |
| 818 | .kill_sb = kill_anon_super, |
| 819 | }; |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 820 | MODULE_ALIAS_FS("overlay"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 821 | |
| 822 | static int __init ovl_init(void) |
| 823 | { |
| 824 | return register_filesystem(&ovl_fs_type); |
| 825 | } |
| 826 | |
| 827 | static void __exit ovl_exit(void) |
| 828 | { |
| 829 | unregister_filesystem(&ovl_fs_type); |
| 830 | } |
| 831 | |
| 832 | module_init(ovl_init); |
| 833 | module_exit(ovl_exit); |