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