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