xj | b04a402 | 2021-11-25 15:01:52 +0800 | [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 <uapi/linux/magic.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/namei.h> |
| 13 | #include <linux/xattr.h> |
| 14 | #include <linux/mount.h> |
| 15 | #include <linux/parser.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/statfs.h> |
| 18 | #include <linux/seq_file.h> |
| 19 | #include <linux/posix_acl_xattr.h> |
| 20 | #include <linux/exportfs.h> |
| 21 | #include "overlayfs.h" |
| 22 | |
| 23 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 24 | MODULE_DESCRIPTION("Overlay filesystem"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
| 27 | |
| 28 | struct ovl_dir_cache; |
| 29 | |
| 30 | #define OVL_MAX_STACK 500 |
| 31 | |
| 32 | static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR); |
| 33 | module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644); |
| 34 | MODULE_PARM_DESC(ovl_redirect_dir_def, |
| 35 | "Default to on or off for the redirect_dir feature"); |
| 36 | |
| 37 | static bool ovl_redirect_always_follow = |
| 38 | IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW); |
| 39 | module_param_named(redirect_always_follow, ovl_redirect_always_follow, |
| 40 | bool, 0644); |
| 41 | MODULE_PARM_DESC(ovl_redirect_always_follow, |
| 42 | "Follow redirects even if redirect_dir feature is turned off"); |
| 43 | |
| 44 | static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX); |
| 45 | module_param_named(index, ovl_index_def, bool, 0644); |
| 46 | MODULE_PARM_DESC(ovl_index_def, |
| 47 | "Default to on or off for the inodes index feature"); |
| 48 | |
| 49 | static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT); |
| 50 | module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644); |
| 51 | MODULE_PARM_DESC(ovl_nfs_export_def, |
| 52 | "Default to on or off for the NFS export feature"); |
| 53 | |
| 54 | static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO); |
| 55 | module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644); |
| 56 | MODULE_PARM_DESC(ovl_xino_auto_def, |
| 57 | "Auto enable xino feature"); |
| 58 | |
| 59 | static bool __read_mostly ovl_override_creds_def = true; |
| 60 | module_param_named(override_creds, ovl_override_creds_def, bool, 0644); |
| 61 | MODULE_PARM_DESC(ovl_override_creds_def, |
| 62 | "Use mounter's credentials for accesses"); |
| 63 | |
| 64 | static void ovl_entry_stack_free(struct ovl_entry *oe) |
| 65 | { |
| 66 | unsigned int i; |
| 67 | |
| 68 | for (i = 0; i < oe->numlower; i++) |
| 69 | dput(oe->lowerstack[i].dentry); |
| 70 | } |
| 71 | |
| 72 | static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY); |
| 73 | module_param_named(metacopy, ovl_metacopy_def, bool, 0644); |
| 74 | MODULE_PARM_DESC(ovl_metacopy_def, |
| 75 | "Default to on or off for the metadata only copy up feature"); |
| 76 | |
| 77 | static void ovl_dentry_release(struct dentry *dentry) |
| 78 | { |
| 79 | struct ovl_entry *oe = dentry->d_fsdata; |
| 80 | |
| 81 | if (oe) { |
| 82 | ovl_entry_stack_free(oe); |
| 83 | kfree_rcu(oe, rcu); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static struct dentry *ovl_d_real(struct dentry *dentry, |
| 88 | const struct inode *inode) |
| 89 | { |
| 90 | struct dentry *real; |
| 91 | |
| 92 | /* It's an overlay file */ |
| 93 | if (inode && d_inode(dentry) == inode) |
| 94 | return dentry; |
| 95 | |
| 96 | if (!d_is_reg(dentry)) { |
| 97 | if (!inode || inode == d_inode(dentry)) |
| 98 | return dentry; |
| 99 | goto bug; |
| 100 | } |
| 101 | |
| 102 | real = ovl_dentry_upper(dentry); |
| 103 | if (real && (inode == d_inode(real))) |
| 104 | return real; |
| 105 | |
| 106 | if (real && !inode && ovl_has_upperdata(d_inode(dentry))) |
| 107 | return real; |
| 108 | |
| 109 | real = ovl_dentry_lowerdata(dentry); |
| 110 | if (!real) |
| 111 | goto bug; |
| 112 | |
| 113 | /* Handle recursion */ |
| 114 | real = d_real(real, inode); |
| 115 | |
| 116 | if (!inode || inode == d_inode(real)) |
| 117 | return real; |
| 118 | bug: |
| 119 | WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry, |
| 120 | inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); |
| 121 | return dentry; |
| 122 | } |
| 123 | |
| 124 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
| 125 | { |
| 126 | struct ovl_entry *oe = dentry->d_fsdata; |
| 127 | unsigned int i; |
| 128 | int ret = 1; |
| 129 | |
| 130 | for (i = 0; i < oe->numlower; i++) { |
| 131 | struct dentry *d = oe->lowerstack[i].dentry; |
| 132 | |
| 133 | if (d->d_flags & DCACHE_OP_REVALIDATE) { |
| 134 | ret = d->d_op->d_revalidate(d, flags); |
| 135 | if (ret < 0) |
| 136 | return ret; |
| 137 | if (!ret) { |
| 138 | if (!(flags & LOOKUP_RCU)) |
| 139 | d_invalidate(d); |
| 140 | return -ESTALE; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) |
| 148 | { |
| 149 | struct ovl_entry *oe = dentry->d_fsdata; |
| 150 | unsigned int i; |
| 151 | int ret = 1; |
| 152 | |
| 153 | for (i = 0; i < oe->numlower; i++) { |
| 154 | struct dentry *d = oe->lowerstack[i].dentry; |
| 155 | |
| 156 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { |
| 157 | ret = d->d_op->d_weak_revalidate(d, flags); |
| 158 | if (ret <= 0) |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | static const struct dentry_operations ovl_dentry_operations = { |
| 166 | .d_release = ovl_dentry_release, |
| 167 | .d_real = ovl_d_real, |
| 168 | }; |
| 169 | |
| 170 | static const struct dentry_operations ovl_reval_dentry_operations = { |
| 171 | .d_release = ovl_dentry_release, |
| 172 | .d_real = ovl_d_real, |
| 173 | .d_revalidate = ovl_dentry_revalidate, |
| 174 | .d_weak_revalidate = ovl_dentry_weak_revalidate, |
| 175 | }; |
| 176 | |
| 177 | static struct kmem_cache *ovl_inode_cachep; |
| 178 | |
| 179 | static struct inode *ovl_alloc_inode(struct super_block *sb) |
| 180 | { |
| 181 | struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL); |
| 182 | |
| 183 | if (!oi) |
| 184 | return NULL; |
| 185 | |
| 186 | oi->cache = NULL; |
| 187 | oi->redirect = NULL; |
| 188 | oi->version = 0; |
| 189 | oi->flags = 0; |
| 190 | oi->__upperdentry = NULL; |
| 191 | oi->lower = NULL; |
| 192 | oi->lowerdata = NULL; |
| 193 | mutex_init(&oi->lock); |
| 194 | |
| 195 | return &oi->vfs_inode; |
| 196 | } |
| 197 | |
| 198 | static void ovl_i_callback(struct rcu_head *head) |
| 199 | { |
| 200 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 201 | |
| 202 | kmem_cache_free(ovl_inode_cachep, OVL_I(inode)); |
| 203 | } |
| 204 | |
| 205 | static void ovl_destroy_inode(struct inode *inode) |
| 206 | { |
| 207 | struct ovl_inode *oi = OVL_I(inode); |
| 208 | |
| 209 | dput(oi->__upperdentry); |
| 210 | iput(oi->lower); |
| 211 | if (S_ISDIR(inode->i_mode)) |
| 212 | ovl_dir_cache_free(inode); |
| 213 | else |
| 214 | iput(oi->lowerdata); |
| 215 | kfree(oi->redirect); |
| 216 | mutex_destroy(&oi->lock); |
| 217 | |
| 218 | call_rcu(&inode->i_rcu, ovl_i_callback); |
| 219 | } |
| 220 | |
| 221 | static void ovl_free_fs(struct ovl_fs *ofs) |
| 222 | { |
| 223 | unsigned i; |
| 224 | |
| 225 | iput(ofs->workbasedir_trap); |
| 226 | iput(ofs->indexdir_trap); |
| 227 | iput(ofs->workdir_trap); |
| 228 | iput(ofs->upperdir_trap); |
| 229 | dput(ofs->indexdir); |
| 230 | dput(ofs->workdir); |
| 231 | if (ofs->workdir_locked) |
| 232 | ovl_inuse_unlock(ofs->workbasedir); |
| 233 | dput(ofs->workbasedir); |
| 234 | if (ofs->upperdir_locked) |
| 235 | ovl_inuse_unlock(ofs->upper_mnt->mnt_root); |
| 236 | mntput(ofs->upper_mnt); |
| 237 | for (i = 0; i < ofs->numlower; i++) { |
| 238 | iput(ofs->lower_layers[i].trap); |
| 239 | mntput(ofs->lower_layers[i].mnt); |
| 240 | } |
| 241 | for (i = 0; i < ofs->numlowerfs; i++) |
| 242 | free_anon_bdev(ofs->lower_fs[i].pseudo_dev); |
| 243 | kfree(ofs->lower_layers); |
| 244 | kfree(ofs->lower_fs); |
| 245 | |
| 246 | kfree(ofs->config.lowerdir); |
| 247 | kfree(ofs->config.upperdir); |
| 248 | kfree(ofs->config.workdir); |
| 249 | kfree(ofs->config.redirect_mode); |
| 250 | if (ofs->creator_cred) |
| 251 | put_cred(ofs->creator_cred); |
| 252 | kfree(ofs); |
| 253 | } |
| 254 | |
| 255 | static void ovl_put_super(struct super_block *sb) |
| 256 | { |
| 257 | struct ovl_fs *ofs = sb->s_fs_info; |
| 258 | |
| 259 | ovl_free_fs(ofs); |
| 260 | } |
| 261 | |
| 262 | /* Sync real dirty inodes in upper filesystem (if it exists) */ |
| 263 | static int ovl_sync_fs(struct super_block *sb, int wait) |
| 264 | { |
| 265 | struct ovl_fs *ofs = sb->s_fs_info; |
| 266 | struct super_block *upper_sb; |
| 267 | int ret; |
| 268 | |
| 269 | if (!ofs->upper_mnt) |
| 270 | return 0; |
| 271 | |
| 272 | /* |
| 273 | * If this is a sync(2) call or an emergency sync, all the super blocks |
| 274 | * will be iterated, including upper_sb, so no need to do anything. |
| 275 | * |
| 276 | * If this is a syncfs(2) call, then we do need to call |
| 277 | * sync_filesystem() on upper_sb, but enough if we do it when being |
| 278 | * called with wait == 1. |
| 279 | */ |
| 280 | if (!wait) |
| 281 | return 0; |
| 282 | |
| 283 | upper_sb = ofs->upper_mnt->mnt_sb; |
| 284 | |
| 285 | down_read(&upper_sb->s_umount); |
| 286 | ret = sync_filesystem(upper_sb); |
| 287 | up_read(&upper_sb->s_umount); |
| 288 | |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * ovl_statfs |
| 294 | * @sb: The overlayfs super block |
| 295 | * @buf: The struct kstatfs to fill in with stats |
| 296 | * |
| 297 | * Get the filesystem statistics. As writes always target the upper layer |
| 298 | * filesystem pass the statfs to the upper filesystem (if it exists) |
| 299 | */ |
| 300 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 301 | { |
| 302 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 303 | struct dentry *root_dentry = dentry->d_sb->s_root; |
| 304 | struct path path; |
| 305 | int err; |
| 306 | |
| 307 | ovl_path_real(root_dentry, &path); |
| 308 | |
| 309 | err = vfs_statfs(&path, buf); |
| 310 | if (!err) { |
| 311 | buf->f_namelen = ofs->namelen; |
| 312 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
| 313 | } |
| 314 | |
| 315 | return err; |
| 316 | } |
| 317 | |
| 318 | /* Will this overlay be forced to mount/remount ro? */ |
| 319 | static bool ovl_force_readonly(struct ovl_fs *ofs) |
| 320 | { |
| 321 | return (!ofs->upper_mnt || !ofs->workdir); |
| 322 | } |
| 323 | |
| 324 | static const char *ovl_redirect_mode_def(void) |
| 325 | { |
| 326 | return ovl_redirect_dir_def ? "on" : "off"; |
| 327 | } |
| 328 | |
| 329 | enum { |
| 330 | OVL_XINO_OFF, |
| 331 | OVL_XINO_AUTO, |
| 332 | OVL_XINO_ON, |
| 333 | }; |
| 334 | |
| 335 | static const char * const ovl_xino_str[] = { |
| 336 | "off", |
| 337 | "auto", |
| 338 | "on", |
| 339 | }; |
| 340 | |
| 341 | static inline int ovl_xino_def(void) |
| 342 | { |
| 343 | return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * ovl_show_options |
| 348 | * |
| 349 | * Prints the mount options for a given superblock. |
| 350 | * Returns zero; does not fail. |
| 351 | */ |
| 352 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) |
| 353 | { |
| 354 | struct super_block *sb = dentry->d_sb; |
| 355 | struct ovl_fs *ofs = sb->s_fs_info; |
| 356 | |
| 357 | seq_show_option(m, "lowerdir", ofs->config.lowerdir); |
| 358 | if (ofs->config.upperdir) { |
| 359 | seq_show_option(m, "upperdir", ofs->config.upperdir); |
| 360 | seq_show_option(m, "workdir", ofs->config.workdir); |
| 361 | } |
| 362 | if (ofs->config.default_permissions) |
| 363 | seq_puts(m, ",default_permissions"); |
| 364 | if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0) |
| 365 | seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode); |
| 366 | if (ofs->config.index != ovl_index_def) |
| 367 | seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off"); |
| 368 | if (ofs->config.nfs_export != ovl_nfs_export_def) |
| 369 | seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ? |
| 370 | "on" : "off"); |
| 371 | if (ofs->config.xino != ovl_xino_def()) |
| 372 | seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]); |
| 373 | if (ofs->config.metacopy != ovl_metacopy_def) |
| 374 | seq_printf(m, ",metacopy=%s", |
| 375 | ofs->config.metacopy ? "on" : "off"); |
| 376 | if (ofs->config.override_creds != ovl_override_creds_def) |
| 377 | seq_show_option(m, "override_creds", |
| 378 | ofs->config.override_creds ? "on" : "off"); |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
| 383 | { |
| 384 | struct ovl_fs *ofs = sb->s_fs_info; |
| 385 | |
| 386 | if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs)) |
| 387 | return -EROFS; |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static const struct super_operations ovl_super_operations = { |
| 393 | .alloc_inode = ovl_alloc_inode, |
| 394 | .destroy_inode = ovl_destroy_inode, |
| 395 | .drop_inode = generic_delete_inode, |
| 396 | .put_super = ovl_put_super, |
| 397 | .sync_fs = ovl_sync_fs, |
| 398 | .statfs = ovl_statfs, |
| 399 | .show_options = ovl_show_options, |
| 400 | .remount_fs = ovl_remount, |
| 401 | }; |
| 402 | |
| 403 | enum { |
| 404 | OPT_LOWERDIR, |
| 405 | OPT_UPPERDIR, |
| 406 | OPT_WORKDIR, |
| 407 | OPT_DEFAULT_PERMISSIONS, |
| 408 | OPT_REDIRECT_DIR, |
| 409 | OPT_INDEX_ON, |
| 410 | OPT_INDEX_OFF, |
| 411 | OPT_NFS_EXPORT_ON, |
| 412 | OPT_NFS_EXPORT_OFF, |
| 413 | OPT_XINO_ON, |
| 414 | OPT_XINO_OFF, |
| 415 | OPT_XINO_AUTO, |
| 416 | OPT_METACOPY_ON, |
| 417 | OPT_METACOPY_OFF, |
| 418 | OPT_OVERRIDE_CREDS_ON, |
| 419 | OPT_OVERRIDE_CREDS_OFF, |
| 420 | OPT_ERR, |
| 421 | }; |
| 422 | |
| 423 | static const match_table_t ovl_tokens = { |
| 424 | {OPT_LOWERDIR, "lowerdir=%s"}, |
| 425 | {OPT_UPPERDIR, "upperdir=%s"}, |
| 426 | {OPT_WORKDIR, "workdir=%s"}, |
| 427 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
| 428 | {OPT_REDIRECT_DIR, "redirect_dir=%s"}, |
| 429 | {OPT_INDEX_ON, "index=on"}, |
| 430 | {OPT_INDEX_OFF, "index=off"}, |
| 431 | {OPT_NFS_EXPORT_ON, "nfs_export=on"}, |
| 432 | {OPT_NFS_EXPORT_OFF, "nfs_export=off"}, |
| 433 | {OPT_XINO_ON, "xino=on"}, |
| 434 | {OPT_XINO_OFF, "xino=off"}, |
| 435 | {OPT_XINO_AUTO, "xino=auto"}, |
| 436 | {OPT_METACOPY_ON, "metacopy=on"}, |
| 437 | {OPT_METACOPY_OFF, "metacopy=off"}, |
| 438 | {OPT_OVERRIDE_CREDS_ON, "override_creds=on"}, |
| 439 | {OPT_OVERRIDE_CREDS_OFF, "override_creds=off"}, |
| 440 | {OPT_ERR, NULL} |
| 441 | }; |
| 442 | |
| 443 | static char *ovl_next_opt(char **s) |
| 444 | { |
| 445 | char *sbegin = *s; |
| 446 | char *p; |
| 447 | |
| 448 | if (sbegin == NULL) |
| 449 | return NULL; |
| 450 | |
| 451 | for (p = sbegin; *p; p++) { |
| 452 | if (*p == '\\') { |
| 453 | p++; |
| 454 | if (!*p) |
| 455 | break; |
| 456 | } else if (*p == ',') { |
| 457 | *p = '\0'; |
| 458 | *s = p + 1; |
| 459 | return sbegin; |
| 460 | } |
| 461 | } |
| 462 | *s = NULL; |
| 463 | return sbegin; |
| 464 | } |
| 465 | |
| 466 | static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode) |
| 467 | { |
| 468 | if (strcmp(mode, "on") == 0) { |
| 469 | config->redirect_dir = true; |
| 470 | /* |
| 471 | * Does not make sense to have redirect creation without |
| 472 | * redirect following. |
| 473 | */ |
| 474 | config->redirect_follow = true; |
| 475 | } else if (strcmp(mode, "follow") == 0) { |
| 476 | config->redirect_follow = true; |
| 477 | } else if (strcmp(mode, "off") == 0) { |
| 478 | if (ovl_redirect_always_follow) |
| 479 | config->redirect_follow = true; |
| 480 | } else if (strcmp(mode, "nofollow") != 0) { |
| 481 | pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n", |
| 482 | mode); |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
| 490 | { |
| 491 | char *p; |
| 492 | int err; |
| 493 | bool metacopy_opt = false, redirect_opt = false; |
| 494 | |
| 495 | config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL); |
| 496 | if (!config->redirect_mode) |
| 497 | return -ENOMEM; |
| 498 | config->override_creds = ovl_override_creds_def; |
| 499 | |
| 500 | while ((p = ovl_next_opt(&opt)) != NULL) { |
| 501 | int token; |
| 502 | substring_t args[MAX_OPT_ARGS]; |
| 503 | |
| 504 | if (!*p) |
| 505 | continue; |
| 506 | |
| 507 | token = match_token(p, ovl_tokens, args); |
| 508 | switch (token) { |
| 509 | case OPT_UPPERDIR: |
| 510 | kfree(config->upperdir); |
| 511 | config->upperdir = match_strdup(&args[0]); |
| 512 | if (!config->upperdir) |
| 513 | return -ENOMEM; |
| 514 | break; |
| 515 | |
| 516 | case OPT_LOWERDIR: |
| 517 | kfree(config->lowerdir); |
| 518 | config->lowerdir = match_strdup(&args[0]); |
| 519 | if (!config->lowerdir) |
| 520 | return -ENOMEM; |
| 521 | break; |
| 522 | |
| 523 | case OPT_WORKDIR: |
| 524 | kfree(config->workdir); |
| 525 | config->workdir = match_strdup(&args[0]); |
| 526 | if (!config->workdir) |
| 527 | return -ENOMEM; |
| 528 | break; |
| 529 | |
| 530 | case OPT_DEFAULT_PERMISSIONS: |
| 531 | config->default_permissions = true; |
| 532 | break; |
| 533 | |
| 534 | case OPT_REDIRECT_DIR: |
| 535 | kfree(config->redirect_mode); |
| 536 | config->redirect_mode = match_strdup(&args[0]); |
| 537 | if (!config->redirect_mode) |
| 538 | return -ENOMEM; |
| 539 | redirect_opt = true; |
| 540 | break; |
| 541 | |
| 542 | case OPT_INDEX_ON: |
| 543 | config->index = true; |
| 544 | break; |
| 545 | |
| 546 | case OPT_INDEX_OFF: |
| 547 | config->index = false; |
| 548 | break; |
| 549 | |
| 550 | case OPT_NFS_EXPORT_ON: |
| 551 | config->nfs_export = true; |
| 552 | break; |
| 553 | |
| 554 | case OPT_NFS_EXPORT_OFF: |
| 555 | config->nfs_export = false; |
| 556 | break; |
| 557 | |
| 558 | case OPT_XINO_ON: |
| 559 | config->xino = OVL_XINO_ON; |
| 560 | break; |
| 561 | |
| 562 | case OPT_XINO_OFF: |
| 563 | config->xino = OVL_XINO_OFF; |
| 564 | break; |
| 565 | |
| 566 | case OPT_XINO_AUTO: |
| 567 | config->xino = OVL_XINO_AUTO; |
| 568 | break; |
| 569 | |
| 570 | case OPT_METACOPY_ON: |
| 571 | config->metacopy = true; |
| 572 | metacopy_opt = true; |
| 573 | break; |
| 574 | |
| 575 | case OPT_METACOPY_OFF: |
| 576 | config->metacopy = false; |
| 577 | break; |
| 578 | |
| 579 | case OPT_OVERRIDE_CREDS_ON: |
| 580 | config->override_creds = true; |
| 581 | break; |
| 582 | |
| 583 | case OPT_OVERRIDE_CREDS_OFF: |
| 584 | config->override_creds = false; |
| 585 | break; |
| 586 | |
| 587 | default: |
| 588 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
| 589 | return -EINVAL; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /* Workdir is useless in non-upper mount */ |
| 594 | if (!config->upperdir && config->workdir) { |
| 595 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", |
| 596 | config->workdir); |
| 597 | kfree(config->workdir); |
| 598 | config->workdir = NULL; |
| 599 | } |
| 600 | |
| 601 | err = ovl_parse_redirect_mode(config, config->redirect_mode); |
| 602 | if (err) |
| 603 | return err; |
| 604 | |
| 605 | /* |
| 606 | * This is to make the logic below simpler. It doesn't make any other |
| 607 | * difference, since config->redirect_dir is only used for upper. |
| 608 | */ |
| 609 | if (!config->upperdir && config->redirect_follow) |
| 610 | config->redirect_dir = true; |
| 611 | |
| 612 | /* Resolve metacopy -> redirect_dir dependency */ |
| 613 | if (config->metacopy && !config->redirect_dir) { |
| 614 | if (metacopy_opt && redirect_opt) { |
| 615 | pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n", |
| 616 | config->redirect_mode); |
| 617 | return -EINVAL; |
| 618 | } |
| 619 | if (redirect_opt) { |
| 620 | /* |
| 621 | * There was an explicit redirect_dir=... that resulted |
| 622 | * in this conflict. |
| 623 | */ |
| 624 | pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n", |
| 625 | config->redirect_mode); |
| 626 | config->metacopy = false; |
| 627 | } else { |
| 628 | /* Automatically enable redirect otherwise. */ |
| 629 | config->redirect_follow = config->redirect_dir = true; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | #define OVL_WORKDIR_NAME "work" |
| 637 | #define OVL_INDEXDIR_NAME "index" |
| 638 | |
| 639 | static struct dentry *ovl_workdir_create(struct ovl_fs *ofs, |
| 640 | const char *name, bool persist) |
| 641 | { |
| 642 | struct inode *dir = ofs->workbasedir->d_inode; |
| 643 | struct vfsmount *mnt = ofs->upper_mnt; |
| 644 | struct dentry *work; |
| 645 | int err; |
| 646 | bool retried = false; |
| 647 | bool locked = false; |
| 648 | |
| 649 | inode_lock_nested(dir, I_MUTEX_PARENT); |
| 650 | locked = true; |
| 651 | |
| 652 | retry: |
| 653 | work = lookup_one_len(name, ofs->workbasedir, strlen(name)); |
| 654 | |
| 655 | if (!IS_ERR(work)) { |
| 656 | struct iattr attr = { |
| 657 | .ia_valid = ATTR_MODE, |
| 658 | .ia_mode = S_IFDIR | 0, |
| 659 | }; |
| 660 | |
| 661 | if (work->d_inode) { |
| 662 | err = -EEXIST; |
| 663 | if (retried) |
| 664 | goto out_dput; |
| 665 | |
| 666 | if (persist) |
| 667 | goto out_unlock; |
| 668 | |
| 669 | retried = true; |
| 670 | ovl_workdir_cleanup(dir, mnt, work, 0); |
| 671 | dput(work); |
| 672 | goto retry; |
| 673 | } |
| 674 | |
| 675 | work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode)); |
| 676 | err = PTR_ERR(work); |
| 677 | if (IS_ERR(work)) |
| 678 | goto out_err; |
| 679 | |
| 680 | /* |
| 681 | * Try to remove POSIX ACL xattrs from workdir. We are good if: |
| 682 | * |
| 683 | * a) success (there was a POSIX ACL xattr and was removed) |
| 684 | * b) -ENODATA (there was no POSIX ACL xattr) |
| 685 | * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported) |
| 686 | * |
| 687 | * There are various other error values that could effectively |
| 688 | * mean that the xattr doesn't exist (e.g. -ERANGE is returned |
| 689 | * if the xattr name is too long), but the set of filesystems |
| 690 | * allowed as upper are limited to "normal" ones, where checking |
| 691 | * for the above two errors is sufficient. |
| 692 | */ |
| 693 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT); |
| 694 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
| 695 | goto out_dput; |
| 696 | |
| 697 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS); |
| 698 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
| 699 | goto out_dput; |
| 700 | |
| 701 | /* Clear any inherited mode bits */ |
| 702 | inode_lock(work->d_inode); |
| 703 | err = notify_change(work, &attr, NULL); |
| 704 | inode_unlock(work->d_inode); |
| 705 | if (err) |
| 706 | goto out_dput; |
| 707 | } else { |
| 708 | err = PTR_ERR(work); |
| 709 | goto out_err; |
| 710 | } |
| 711 | out_unlock: |
| 712 | if (locked) |
| 713 | inode_unlock(dir); |
| 714 | |
| 715 | return work; |
| 716 | |
| 717 | out_dput: |
| 718 | dput(work); |
| 719 | out_err: |
| 720 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", |
| 721 | ofs->config.workdir, name, -err); |
| 722 | work = NULL; |
| 723 | goto out_unlock; |
| 724 | } |
| 725 | |
| 726 | static void ovl_unescape(char *s) |
| 727 | { |
| 728 | char *d = s; |
| 729 | |
| 730 | for (;; s++, d++) { |
| 731 | if (*s == '\\') |
| 732 | s++; |
| 733 | *d = *s; |
| 734 | if (!*s) |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
| 740 | { |
| 741 | int err = -EINVAL; |
| 742 | |
| 743 | if (!*name) { |
| 744 | pr_err("overlayfs: empty lowerdir\n"); |
| 745 | goto out; |
| 746 | } |
| 747 | err = kern_path(name, LOOKUP_FOLLOW, path); |
| 748 | if (err) { |
| 749 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); |
| 750 | goto out; |
| 751 | } |
| 752 | err = -EINVAL; |
| 753 | if (ovl_dentry_weird(path->dentry)) { |
| 754 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
| 755 | goto out_put; |
| 756 | } |
| 757 | if (!d_is_dir(path->dentry)) { |
| 758 | pr_err("overlayfs: '%s' not a directory\n", name); |
| 759 | goto out_put; |
| 760 | } |
| 761 | return 0; |
| 762 | |
| 763 | out_put: |
| 764 | path_put_init(path); |
| 765 | out: |
| 766 | return err; |
| 767 | } |
| 768 | |
| 769 | static int ovl_mount_dir(const char *name, struct path *path) |
| 770 | { |
| 771 | int err = -ENOMEM; |
| 772 | char *tmp = kstrdup(name, GFP_KERNEL); |
| 773 | |
| 774 | if (tmp) { |
| 775 | ovl_unescape(tmp); |
| 776 | err = ovl_mount_dir_noesc(tmp, path); |
| 777 | |
| 778 | if (!err) |
| 779 | if (ovl_dentry_remote(path->dentry)) { |
| 780 | pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", |
| 781 | tmp); |
| 782 | path_put_init(path); |
| 783 | err = -EINVAL; |
| 784 | } |
| 785 | kfree(tmp); |
| 786 | } |
| 787 | return err; |
| 788 | } |
| 789 | |
| 790 | static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs, |
| 791 | const char *name) |
| 792 | { |
| 793 | struct kstatfs statfs; |
| 794 | int err = vfs_statfs(path, &statfs); |
| 795 | |
| 796 | if (err) |
| 797 | pr_err("overlayfs: statfs failed on '%s'\n", name); |
| 798 | else |
| 799 | ofs->namelen = max(ofs->namelen, statfs.f_namelen); |
| 800 | |
| 801 | return err; |
| 802 | } |
| 803 | |
| 804 | static int ovl_lower_dir(const char *name, struct path *path, |
| 805 | struct ovl_fs *ofs, int *stack_depth, bool *remote) |
| 806 | { |
| 807 | int fh_type; |
| 808 | int err; |
| 809 | |
| 810 | err = ovl_mount_dir_noesc(name, path); |
| 811 | if (err) |
| 812 | goto out; |
| 813 | |
| 814 | err = ovl_check_namelen(path, ofs, name); |
| 815 | if (err) |
| 816 | goto out_put; |
| 817 | |
| 818 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
| 819 | |
| 820 | if (ovl_dentry_remote(path->dentry)) |
| 821 | *remote = true; |
| 822 | |
| 823 | /* |
| 824 | * The inodes index feature and NFS export need to encode and decode |
| 825 | * file handles, so they require that all layers support them. |
| 826 | */ |
| 827 | fh_type = ovl_can_decode_fh(path->dentry->d_sb); |
| 828 | if ((ofs->config.nfs_export || |
| 829 | (ofs->config.index && ofs->config.upperdir)) && !fh_type) { |
| 830 | ofs->config.index = false; |
| 831 | ofs->config.nfs_export = false; |
| 832 | pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n", |
| 833 | name); |
| 834 | } |
| 835 | |
| 836 | /* Check if lower fs has 32bit inode numbers */ |
| 837 | if (fh_type != FILEID_INO32_GEN) |
| 838 | ofs->xino_bits = 0; |
| 839 | |
| 840 | return 0; |
| 841 | |
| 842 | out_put: |
| 843 | path_put_init(path); |
| 844 | out: |
| 845 | return err; |
| 846 | } |
| 847 | |
| 848 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 849 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) |
| 850 | { |
| 851 | bool ok = false; |
| 852 | |
| 853 | if (workdir != upperdir) { |
| 854 | ok = (lock_rename(workdir, upperdir) == NULL); |
| 855 | unlock_rename(workdir, upperdir); |
| 856 | } |
| 857 | return ok; |
| 858 | } |
| 859 | |
| 860 | static unsigned int ovl_split_lowerdirs(char *str) |
| 861 | { |
| 862 | unsigned int ctr = 1; |
| 863 | char *s, *d; |
| 864 | |
| 865 | for (s = d = str;; s++, d++) { |
| 866 | if (*s == '\\') { |
| 867 | s++; |
| 868 | } else if (*s == ':') { |
| 869 | *d = '\0'; |
| 870 | ctr++; |
| 871 | continue; |
| 872 | } |
| 873 | *d = *s; |
| 874 | if (!*s) |
| 875 | break; |
| 876 | } |
| 877 | return ctr; |
| 878 | } |
| 879 | |
| 880 | static int __maybe_unused |
| 881 | ovl_posix_acl_xattr_get(const struct xattr_handler *handler, |
| 882 | struct dentry *dentry, struct inode *inode, |
| 883 | const char *name, void *buffer, size_t size) |
| 884 | { |
| 885 | return ovl_xattr_get(dentry, inode, handler->name, buffer, size); |
| 886 | } |
| 887 | |
| 888 | static int __maybe_unused |
| 889 | __ovl_posix_acl_xattr_get(const struct xattr_handler *handler, |
| 890 | struct dentry *dentry, struct inode *inode, |
| 891 | const char *name, void *buffer, size_t size) |
| 892 | { |
| 893 | return __ovl_xattr_get(dentry, inode, handler->name, buffer, size); |
| 894 | } |
| 895 | |
| 896 | static int __maybe_unused |
| 897 | ovl_posix_acl_xattr_set(const struct xattr_handler *handler, |
| 898 | struct dentry *dentry, struct inode *inode, |
| 899 | const char *name, const void *value, |
| 900 | size_t size, int flags) |
| 901 | { |
| 902 | struct dentry *workdir = ovl_workdir(dentry); |
| 903 | struct inode *realinode = ovl_inode_real(inode); |
| 904 | struct posix_acl *acl = NULL; |
| 905 | int err; |
| 906 | |
| 907 | /* Check that everything is OK before copy-up */ |
| 908 | if (value) { |
| 909 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
| 910 | if (IS_ERR(acl)) |
| 911 | return PTR_ERR(acl); |
| 912 | } |
| 913 | err = -EOPNOTSUPP; |
| 914 | if (!IS_POSIXACL(d_inode(workdir))) |
| 915 | goto out_acl_release; |
| 916 | if (!realinode->i_op->set_acl) |
| 917 | goto out_acl_release; |
| 918 | if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) { |
| 919 | err = acl ? -EACCES : 0; |
| 920 | goto out_acl_release; |
| 921 | } |
| 922 | err = -EPERM; |
| 923 | if (!inode_owner_or_capable(inode)) |
| 924 | goto out_acl_release; |
| 925 | |
| 926 | posix_acl_release(acl); |
| 927 | |
| 928 | /* |
| 929 | * Check if sgid bit needs to be cleared (actual setacl operation will |
| 930 | * be done with mounter's capabilities and so that won't do it for us). |
| 931 | */ |
| 932 | if (unlikely(inode->i_mode & S_ISGID) && |
| 933 | handler->flags == ACL_TYPE_ACCESS && |
| 934 | !in_group_p(inode->i_gid) && |
| 935 | !capable_wrt_inode_uidgid(inode, CAP_FSETID)) { |
| 936 | struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; |
| 937 | |
| 938 | err = ovl_setattr(dentry, &iattr); |
| 939 | if (err) |
| 940 | return err; |
| 941 | } |
| 942 | |
| 943 | err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags); |
| 944 | if (!err) |
| 945 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 946 | |
| 947 | return err; |
| 948 | |
| 949 | out_acl_release: |
| 950 | posix_acl_release(acl); |
| 951 | return err; |
| 952 | } |
| 953 | |
| 954 | static int ovl_own_xattr_get(const struct xattr_handler *handler, |
| 955 | struct dentry *dentry, struct inode *inode, |
| 956 | const char *name, void *buffer, size_t size) |
| 957 | { |
| 958 | return -EOPNOTSUPP; |
| 959 | } |
| 960 | |
| 961 | static int ovl_own_xattr_set(const struct xattr_handler *handler, |
| 962 | struct dentry *dentry, struct inode *inode, |
| 963 | const char *name, const void *value, |
| 964 | size_t size, int flags) |
| 965 | { |
| 966 | return -EOPNOTSUPP; |
| 967 | } |
| 968 | |
| 969 | static int ovl_other_xattr_get(const struct xattr_handler *handler, |
| 970 | struct dentry *dentry, struct inode *inode, |
| 971 | const char *name, void *buffer, size_t size) |
| 972 | { |
| 973 | return ovl_xattr_get(dentry, inode, name, buffer, size); |
| 974 | } |
| 975 | |
| 976 | static int __ovl_other_xattr_get(const struct xattr_handler *handler, |
| 977 | struct dentry *dentry, struct inode *inode, |
| 978 | const char *name, void *buffer, size_t size) |
| 979 | { |
| 980 | return __ovl_xattr_get(dentry, inode, name, buffer, size); |
| 981 | } |
| 982 | |
| 983 | static int ovl_other_xattr_set(const struct xattr_handler *handler, |
| 984 | struct dentry *dentry, struct inode *inode, |
| 985 | const char *name, const void *value, |
| 986 | size_t size, int flags) |
| 987 | { |
| 988 | return ovl_xattr_set(dentry, inode, name, value, size, flags); |
| 989 | } |
| 990 | |
| 991 | static const struct xattr_handler __maybe_unused |
| 992 | ovl_posix_acl_access_xattr_handler = { |
| 993 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
| 994 | .flags = ACL_TYPE_ACCESS, |
| 995 | .get = ovl_posix_acl_xattr_get, |
| 996 | .__get = __ovl_posix_acl_xattr_get, |
| 997 | .set = ovl_posix_acl_xattr_set, |
| 998 | }; |
| 999 | |
| 1000 | static const struct xattr_handler __maybe_unused |
| 1001 | ovl_posix_acl_default_xattr_handler = { |
| 1002 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
| 1003 | .flags = ACL_TYPE_DEFAULT, |
| 1004 | .get = ovl_posix_acl_xattr_get, |
| 1005 | .__get = __ovl_posix_acl_xattr_get, |
| 1006 | .set = ovl_posix_acl_xattr_set, |
| 1007 | }; |
| 1008 | |
| 1009 | static const struct xattr_handler ovl_own_xattr_handler = { |
| 1010 | .prefix = OVL_XATTR_PREFIX, |
| 1011 | .get = ovl_own_xattr_get, |
| 1012 | .set = ovl_own_xattr_set, |
| 1013 | }; |
| 1014 | |
| 1015 | static const struct xattr_handler ovl_other_xattr_handler = { |
| 1016 | .prefix = "", /* catch all */ |
| 1017 | .get = ovl_other_xattr_get, |
| 1018 | .__get = __ovl_other_xattr_get, |
| 1019 | .set = ovl_other_xattr_set, |
| 1020 | }; |
| 1021 | |
| 1022 | static const struct xattr_handler *ovl_xattr_handlers[] = { |
| 1023 | #ifdef CONFIG_FS_POSIX_ACL |
| 1024 | &ovl_posix_acl_access_xattr_handler, |
| 1025 | &ovl_posix_acl_default_xattr_handler, |
| 1026 | #endif |
| 1027 | &ovl_own_xattr_handler, |
| 1028 | &ovl_other_xattr_handler, |
| 1029 | NULL |
| 1030 | }; |
| 1031 | |
| 1032 | static int ovl_setup_trap(struct super_block *sb, struct dentry *dir, |
| 1033 | struct inode **ptrap, const char *name) |
| 1034 | { |
| 1035 | struct inode *trap; |
| 1036 | int err; |
| 1037 | |
| 1038 | trap = ovl_get_trap_inode(sb, dir); |
| 1039 | err = PTR_ERR_OR_ZERO(trap); |
| 1040 | if (err) { |
| 1041 | if (err == -ELOOP) |
| 1042 | pr_err("overlayfs: conflicting %s path\n", name); |
| 1043 | return err; |
| 1044 | } |
| 1045 | |
| 1046 | *ptrap = trap; |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Determine how we treat concurrent use of upperdir/workdir based on the |
| 1052 | * index feature. This is papering over mount leaks of container runtimes, |
| 1053 | * for example, an old overlay mount is leaked and now its upperdir is |
| 1054 | * attempted to be used as a lower layer in a new overlay mount. |
| 1055 | */ |
| 1056 | static int ovl_report_in_use(struct ovl_fs *ofs, const char *name) |
| 1057 | { |
| 1058 | if (ofs->config.index) { |
| 1059 | pr_err("overlayfs: %s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n", |
| 1060 | name); |
| 1061 | return -EBUSY; |
| 1062 | } else { |
| 1063 | pr_warn("overlayfs: %s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n", |
| 1064 | name); |
| 1065 | return 0; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs, |
| 1070 | struct path *upperpath) |
| 1071 | { |
| 1072 | struct vfsmount *upper_mnt; |
| 1073 | int err; |
| 1074 | |
| 1075 | err = ovl_mount_dir(ofs->config.upperdir, upperpath); |
| 1076 | if (err) |
| 1077 | goto out; |
| 1078 | |
| 1079 | /* Upper fs should not be r/o */ |
| 1080 | if (sb_rdonly(upperpath->mnt->mnt_sb)) { |
| 1081 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); |
| 1082 | err = -EINVAL; |
| 1083 | goto out; |
| 1084 | } |
| 1085 | |
| 1086 | err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir); |
| 1087 | if (err) |
| 1088 | goto out; |
| 1089 | |
| 1090 | err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap, |
| 1091 | "upperdir"); |
| 1092 | if (err) |
| 1093 | goto out; |
| 1094 | |
| 1095 | upper_mnt = clone_private_mount(upperpath); |
| 1096 | err = PTR_ERR(upper_mnt); |
| 1097 | if (IS_ERR(upper_mnt)) { |
| 1098 | pr_err("overlayfs: failed to clone upperpath\n"); |
| 1099 | goto out; |
| 1100 | } |
| 1101 | |
| 1102 | /* Don't inherit atime flags */ |
| 1103 | upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); |
| 1104 | ofs->upper_mnt = upper_mnt; |
| 1105 | |
| 1106 | if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) { |
| 1107 | ofs->upperdir_locked = true; |
| 1108 | } else { |
| 1109 | err = ovl_report_in_use(ofs, "upperdir"); |
| 1110 | if (err) |
| 1111 | goto out; |
| 1112 | } |
| 1113 | |
| 1114 | err = 0; |
| 1115 | out: |
| 1116 | return err; |
| 1117 | } |
| 1118 | |
| 1119 | static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, |
| 1120 | struct path *workpath) |
| 1121 | { |
| 1122 | struct vfsmount *mnt = ofs->upper_mnt; |
| 1123 | struct dentry *temp; |
| 1124 | int fh_type; |
| 1125 | int err; |
| 1126 | |
| 1127 | err = mnt_want_write(mnt); |
| 1128 | if (err) |
| 1129 | return err; |
| 1130 | |
| 1131 | ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false); |
| 1132 | if (!ofs->workdir) |
| 1133 | goto out; |
| 1134 | |
| 1135 | err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir"); |
| 1136 | if (err) |
| 1137 | goto out; |
| 1138 | |
| 1139 | /* |
| 1140 | * Upper should support d_type, else whiteouts are visible. Given |
| 1141 | * workdir and upper are on same fs, we can do iterate_dir() on |
| 1142 | * workdir. This check requires successful creation of workdir in |
| 1143 | * previous step. |
| 1144 | */ |
| 1145 | err = ovl_check_d_type_supported(workpath); |
| 1146 | if (err < 0) |
| 1147 | goto out; |
| 1148 | |
| 1149 | /* |
| 1150 | * We allowed this configuration and don't want to break users over |
| 1151 | * kernel upgrade. So warn instead of erroring out. |
| 1152 | */ |
| 1153 | if (!err) |
| 1154 | pr_warn("overlayfs: upper fs needs to support d_type.\n"); |
| 1155 | |
| 1156 | /* Check if upper/work fs supports O_TMPFILE */ |
| 1157 | temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0); |
| 1158 | ofs->tmpfile = !IS_ERR(temp); |
| 1159 | if (ofs->tmpfile) |
| 1160 | dput(temp); |
| 1161 | else |
| 1162 | pr_warn("overlayfs: upper fs does not support tmpfile.\n"); |
| 1163 | |
| 1164 | /* |
| 1165 | * Check if upper/work fs supports trusted.overlay.* xattr |
| 1166 | */ |
| 1167 | err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0); |
| 1168 | if (err) { |
| 1169 | ofs->noxattr = true; |
| 1170 | ofs->config.index = false; |
| 1171 | ofs->config.metacopy = false; |
| 1172 | pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n"); |
| 1173 | err = 0; |
| 1174 | } else { |
| 1175 | vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE); |
| 1176 | } |
| 1177 | |
| 1178 | /* Check if upper/work fs supports file handles */ |
| 1179 | fh_type = ovl_can_decode_fh(ofs->workdir->d_sb); |
| 1180 | if (ofs->config.index && !fh_type) { |
| 1181 | ofs->config.index = false; |
| 1182 | pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n"); |
| 1183 | } |
| 1184 | |
| 1185 | /* Check if upper fs has 32bit inode numbers */ |
| 1186 | if (fh_type != FILEID_INO32_GEN) |
| 1187 | ofs->xino_bits = 0; |
| 1188 | |
| 1189 | /* NFS export of r/w mount depends on index */ |
| 1190 | if (ofs->config.nfs_export && !ofs->config.index) { |
| 1191 | pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n"); |
| 1192 | ofs->config.nfs_export = false; |
| 1193 | } |
| 1194 | out: |
| 1195 | mnt_drop_write(mnt); |
| 1196 | return err; |
| 1197 | } |
| 1198 | |
| 1199 | static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs, |
| 1200 | struct path *upperpath) |
| 1201 | { |
| 1202 | int err; |
| 1203 | struct path workpath = { }; |
| 1204 | |
| 1205 | err = ovl_mount_dir(ofs->config.workdir, &workpath); |
| 1206 | if (err) |
| 1207 | goto out; |
| 1208 | |
| 1209 | err = -EINVAL; |
| 1210 | if (upperpath->mnt != workpath.mnt) { |
| 1211 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); |
| 1212 | goto out; |
| 1213 | } |
| 1214 | if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) { |
| 1215 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); |
| 1216 | goto out; |
| 1217 | } |
| 1218 | |
| 1219 | ofs->workbasedir = dget(workpath.dentry); |
| 1220 | |
| 1221 | if (ovl_inuse_trylock(ofs->workbasedir)) { |
| 1222 | ofs->workdir_locked = true; |
| 1223 | } else { |
| 1224 | err = ovl_report_in_use(ofs, "workdir"); |
| 1225 | if (err) |
| 1226 | goto out; |
| 1227 | } |
| 1228 | |
| 1229 | err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap, |
| 1230 | "workdir"); |
| 1231 | if (err) |
| 1232 | goto out; |
| 1233 | |
| 1234 | err = ovl_make_workdir(sb, ofs, &workpath); |
| 1235 | |
| 1236 | out: |
| 1237 | path_put(&workpath); |
| 1238 | |
| 1239 | return err; |
| 1240 | } |
| 1241 | |
| 1242 | static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs, |
| 1243 | struct ovl_entry *oe, struct path *upperpath) |
| 1244 | { |
| 1245 | struct vfsmount *mnt = ofs->upper_mnt; |
| 1246 | int err; |
| 1247 | |
| 1248 | err = mnt_want_write(mnt); |
| 1249 | if (err) |
| 1250 | return err; |
| 1251 | |
| 1252 | /* Verify lower root is upper root origin */ |
| 1253 | err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry, |
| 1254 | true); |
| 1255 | if (err) { |
| 1256 | pr_err("overlayfs: failed to verify upper root origin\n"); |
| 1257 | goto out; |
| 1258 | } |
| 1259 | |
| 1260 | ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true); |
| 1261 | if (ofs->indexdir) { |
| 1262 | err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap, |
| 1263 | "indexdir"); |
| 1264 | if (err) |
| 1265 | goto out; |
| 1266 | |
| 1267 | /* |
| 1268 | * Verify upper root is exclusively associated with index dir. |
| 1269 | * Older kernels stored upper fh in "trusted.overlay.origin" |
| 1270 | * xattr. If that xattr exists, verify that it is a match to |
| 1271 | * upper dir file handle. In any case, verify or set xattr |
| 1272 | * "trusted.overlay.upper" to indicate that index may have |
| 1273 | * directory entries. |
| 1274 | */ |
| 1275 | if (ovl_check_origin_xattr(ofs->indexdir)) { |
| 1276 | err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN, |
| 1277 | upperpath->dentry, true, false); |
| 1278 | if (err) |
| 1279 | pr_err("overlayfs: failed to verify index dir 'origin' xattr\n"); |
| 1280 | } |
| 1281 | err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true); |
| 1282 | if (err) |
| 1283 | pr_err("overlayfs: failed to verify index dir 'upper' xattr\n"); |
| 1284 | |
| 1285 | /* Cleanup bad/stale/orphan index entries */ |
| 1286 | if (!err) |
| 1287 | err = ovl_indexdir_cleanup(ofs); |
| 1288 | } |
| 1289 | if (err || !ofs->indexdir) |
| 1290 | pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n"); |
| 1291 | |
| 1292 | out: |
| 1293 | mnt_drop_write(mnt); |
| 1294 | return err; |
| 1295 | } |
| 1296 | |
| 1297 | /* Get a unique fsid for the layer */ |
| 1298 | static int ovl_get_fsid(struct ovl_fs *ofs, struct super_block *sb) |
| 1299 | { |
| 1300 | unsigned int i; |
| 1301 | dev_t dev; |
| 1302 | int err; |
| 1303 | |
| 1304 | /* fsid 0 is reserved for upper fs even with non upper overlay */ |
| 1305 | if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb) |
| 1306 | return 0; |
| 1307 | |
| 1308 | for (i = 0; i < ofs->numlowerfs; i++) { |
| 1309 | if (ofs->lower_fs[i].sb == sb) |
| 1310 | return i + 1; |
| 1311 | } |
| 1312 | |
| 1313 | err = get_anon_bdev(&dev); |
| 1314 | if (err) { |
| 1315 | pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n"); |
| 1316 | return err; |
| 1317 | } |
| 1318 | |
| 1319 | ofs->lower_fs[ofs->numlowerfs].sb = sb; |
| 1320 | ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev; |
| 1321 | ofs->numlowerfs++; |
| 1322 | |
| 1323 | return ofs->numlowerfs; |
| 1324 | } |
| 1325 | |
| 1326 | static int ovl_get_lower_layers(struct super_block *sb, struct ovl_fs *ofs, |
| 1327 | struct path *stack, unsigned int numlower) |
| 1328 | { |
| 1329 | int err; |
| 1330 | unsigned int i; |
| 1331 | |
| 1332 | err = -ENOMEM; |
| 1333 | ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer), |
| 1334 | GFP_KERNEL); |
| 1335 | if (ofs->lower_layers == NULL) |
| 1336 | goto out; |
| 1337 | |
| 1338 | ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb), |
| 1339 | GFP_KERNEL); |
| 1340 | if (ofs->lower_fs == NULL) |
| 1341 | goto out; |
| 1342 | |
| 1343 | for (i = 0; i < numlower; i++) { |
| 1344 | struct vfsmount *mnt; |
| 1345 | struct inode *trap; |
| 1346 | int fsid; |
| 1347 | |
| 1348 | err = fsid = ovl_get_fsid(ofs, stack[i].mnt->mnt_sb); |
| 1349 | if (err < 0) |
| 1350 | goto out; |
| 1351 | |
| 1352 | err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir"); |
| 1353 | if (err) |
| 1354 | goto out; |
| 1355 | |
| 1356 | if (ovl_is_inuse(stack[i].dentry)) { |
| 1357 | err = ovl_report_in_use(ofs, "lowerdir"); |
| 1358 | if (err) |
| 1359 | goto out; |
| 1360 | } |
| 1361 | |
| 1362 | mnt = clone_private_mount(&stack[i]); |
| 1363 | err = PTR_ERR(mnt); |
| 1364 | if (IS_ERR(mnt)) { |
| 1365 | pr_err("overlayfs: failed to clone lowerpath\n"); |
| 1366 | iput(trap); |
| 1367 | goto out; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * Make lower layers R/O. That way fchmod/fchown on lower file |
| 1372 | * will fail instead of modifying lower fs. |
| 1373 | */ |
| 1374 | mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; |
| 1375 | |
| 1376 | ofs->lower_layers[ofs->numlower].trap = trap; |
| 1377 | ofs->lower_layers[ofs->numlower].mnt = mnt; |
| 1378 | ofs->lower_layers[ofs->numlower].idx = i + 1; |
| 1379 | ofs->lower_layers[ofs->numlower].fsid = fsid; |
| 1380 | if (fsid) { |
| 1381 | ofs->lower_layers[ofs->numlower].fs = |
| 1382 | &ofs->lower_fs[fsid - 1]; |
| 1383 | } |
| 1384 | ofs->numlower++; |
| 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * When all layers on same fs, overlay can use real inode numbers. |
| 1389 | * With mount option "xino=on", mounter declares that there are enough |
| 1390 | * free high bits in underlying fs to hold the unique fsid. |
| 1391 | * If overlayfs does encounter underlying inodes using the high xino |
| 1392 | * bits reserved for fsid, it emits a warning and uses the original |
| 1393 | * inode number. |
| 1394 | */ |
| 1395 | if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) { |
| 1396 | ofs->xino_bits = 0; |
| 1397 | ofs->config.xino = OVL_XINO_OFF; |
| 1398 | } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) { |
| 1399 | /* |
| 1400 | * This is a roundup of number of bits needed for numlowerfs+1 |
| 1401 | * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for |
| 1402 | * upper fs even with non upper overlay. |
| 1403 | */ |
| 1404 | BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31); |
| 1405 | ofs->xino_bits = ilog2(ofs->numlowerfs) + 1; |
| 1406 | } |
| 1407 | |
| 1408 | if (ofs->xino_bits) { |
| 1409 | pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n", |
| 1410 | ofs->xino_bits); |
| 1411 | } |
| 1412 | |
| 1413 | err = 0; |
| 1414 | out: |
| 1415 | return err; |
| 1416 | } |
| 1417 | |
| 1418 | static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb, |
| 1419 | struct ovl_fs *ofs) |
| 1420 | { |
| 1421 | int err; |
| 1422 | char *lowertmp, *lower; |
| 1423 | struct path *stack = NULL; |
| 1424 | unsigned int stacklen, numlower = 0, i; |
| 1425 | bool remote = false; |
| 1426 | struct ovl_entry *oe; |
| 1427 | |
| 1428 | err = -ENOMEM; |
| 1429 | lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL); |
| 1430 | if (!lowertmp) |
| 1431 | goto out_err; |
| 1432 | |
| 1433 | err = -EINVAL; |
| 1434 | stacklen = ovl_split_lowerdirs(lowertmp); |
| 1435 | if (stacklen > OVL_MAX_STACK) { |
| 1436 | pr_err("overlayfs: too many lower directories, limit is %d\n", |
| 1437 | OVL_MAX_STACK); |
| 1438 | goto out_err; |
| 1439 | } else if (!ofs->config.upperdir && stacklen == 1) { |
| 1440 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); |
| 1441 | goto out_err; |
| 1442 | } else if (!ofs->config.upperdir && ofs->config.nfs_export && |
| 1443 | ofs->config.redirect_follow) { |
| 1444 | pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n"); |
| 1445 | ofs->config.nfs_export = false; |
| 1446 | } |
| 1447 | |
| 1448 | err = -ENOMEM; |
| 1449 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); |
| 1450 | if (!stack) |
| 1451 | goto out_err; |
| 1452 | |
| 1453 | err = -EINVAL; |
| 1454 | lower = lowertmp; |
| 1455 | for (numlower = 0; numlower < stacklen; numlower++) { |
| 1456 | err = ovl_lower_dir(lower, &stack[numlower], ofs, |
| 1457 | &sb->s_stack_depth, &remote); |
| 1458 | if (err) |
| 1459 | goto out_err; |
| 1460 | |
| 1461 | lower = strchr(lower, '\0') + 1; |
| 1462 | } |
| 1463 | |
| 1464 | err = -EINVAL; |
| 1465 | sb->s_stack_depth++; |
| 1466 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
| 1467 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); |
| 1468 | goto out_err; |
| 1469 | } |
| 1470 | |
| 1471 | err = ovl_get_lower_layers(sb, ofs, stack, numlower); |
| 1472 | if (err) |
| 1473 | goto out_err; |
| 1474 | |
| 1475 | err = -ENOMEM; |
| 1476 | oe = ovl_alloc_entry(numlower); |
| 1477 | if (!oe) |
| 1478 | goto out_err; |
| 1479 | |
| 1480 | for (i = 0; i < numlower; i++) { |
| 1481 | oe->lowerstack[i].dentry = dget(stack[i].dentry); |
| 1482 | oe->lowerstack[i].layer = &ofs->lower_layers[i]; |
| 1483 | } |
| 1484 | |
| 1485 | if (remote) |
| 1486 | sb->s_d_op = &ovl_reval_dentry_operations; |
| 1487 | else |
| 1488 | sb->s_d_op = &ovl_dentry_operations; |
| 1489 | |
| 1490 | out: |
| 1491 | for (i = 0; i < numlower; i++) |
| 1492 | path_put(&stack[i]); |
| 1493 | kfree(stack); |
| 1494 | kfree(lowertmp); |
| 1495 | |
| 1496 | return oe; |
| 1497 | |
| 1498 | out_err: |
| 1499 | oe = ERR_PTR(err); |
| 1500 | goto out; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * Check if this layer root is a descendant of: |
| 1505 | * - another layer of this overlayfs instance |
| 1506 | * - upper/work dir of any overlayfs instance |
| 1507 | */ |
| 1508 | static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs, |
| 1509 | struct dentry *dentry, const char *name) |
| 1510 | { |
| 1511 | struct dentry *next = dentry, *parent; |
| 1512 | int err = 0; |
| 1513 | |
| 1514 | if (!dentry) |
| 1515 | return 0; |
| 1516 | |
| 1517 | parent = dget_parent(next); |
| 1518 | |
| 1519 | /* Walk back ancestors to root (inclusive) looking for traps */ |
| 1520 | while (!err && parent != next) { |
| 1521 | if (ovl_lookup_trap_inode(sb, parent)) { |
| 1522 | err = -ELOOP; |
| 1523 | pr_err("overlayfs: overlapping %s path\n", name); |
| 1524 | } else if (ovl_is_inuse(parent)) { |
| 1525 | err = ovl_report_in_use(ofs, name); |
| 1526 | } |
| 1527 | next = parent; |
| 1528 | parent = dget_parent(next); |
| 1529 | dput(next); |
| 1530 | } |
| 1531 | |
| 1532 | dput(parent); |
| 1533 | |
| 1534 | return err; |
| 1535 | } |
| 1536 | |
| 1537 | /* |
| 1538 | * Check if any of the layers or work dirs overlap. |
| 1539 | */ |
| 1540 | static int ovl_check_overlapping_layers(struct super_block *sb, |
| 1541 | struct ovl_fs *ofs) |
| 1542 | { |
| 1543 | int i, err; |
| 1544 | |
| 1545 | if (ofs->upper_mnt) { |
| 1546 | err = ovl_check_layer(sb, ofs, ofs->upper_mnt->mnt_root, |
| 1547 | "upperdir"); |
| 1548 | if (err) |
| 1549 | return err; |
| 1550 | |
| 1551 | /* |
| 1552 | * Checking workbasedir avoids hitting ovl_is_inuse(parent) of |
| 1553 | * this instance and covers overlapping work and index dirs, |
| 1554 | * unless work or index dir have been moved since created inside |
| 1555 | * workbasedir. In that case, we already have their traps in |
| 1556 | * inode cache and we will catch that case on lookup. |
| 1557 | */ |
| 1558 | err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir"); |
| 1559 | if (err) |
| 1560 | return err; |
| 1561 | } |
| 1562 | |
| 1563 | for (i = 0; i < ofs->numlower; i++) { |
| 1564 | err = ovl_check_layer(sb, ofs, |
| 1565 | ofs->lower_layers[i].mnt->mnt_root, |
| 1566 | "lowerdir"); |
| 1567 | if (err) |
| 1568 | return err; |
| 1569 | } |
| 1570 | |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
| 1575 | { |
| 1576 | struct path upperpath = { }; |
| 1577 | struct dentry *root_dentry; |
| 1578 | struct ovl_entry *oe; |
| 1579 | struct ovl_fs *ofs; |
| 1580 | struct cred *cred; |
| 1581 | int err; |
| 1582 | |
| 1583 | err = -ENOMEM; |
| 1584 | ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
| 1585 | if (!ofs) |
| 1586 | goto out; |
| 1587 | |
| 1588 | ofs->creator_cred = cred = prepare_creds(); |
| 1589 | if (!cred) |
| 1590 | goto out_err; |
| 1591 | |
| 1592 | ofs->config.index = ovl_index_def; |
| 1593 | ofs->config.nfs_export = ovl_nfs_export_def; |
| 1594 | ofs->config.xino = ovl_xino_def(); |
| 1595 | ofs->config.metacopy = ovl_metacopy_def; |
| 1596 | err = ovl_parse_opt((char *) data, &ofs->config); |
| 1597 | if (err) |
| 1598 | goto out_err; |
| 1599 | |
| 1600 | err = -EINVAL; |
| 1601 | if (!ofs->config.lowerdir) { |
| 1602 | if (!silent) |
| 1603 | pr_err("overlayfs: missing 'lowerdir'\n"); |
| 1604 | goto out_err; |
| 1605 | } |
| 1606 | |
| 1607 | sb->s_stack_depth = 0; |
| 1608 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 1609 | /* Assume underlaying fs uses 32bit inodes unless proven otherwise */ |
| 1610 | if (ofs->config.xino != OVL_XINO_OFF) |
| 1611 | ofs->xino_bits = BITS_PER_LONG - 32; |
| 1612 | |
| 1613 | /* alloc/destroy_inode needed for setting up traps in inode cache */ |
| 1614 | sb->s_op = &ovl_super_operations; |
| 1615 | |
| 1616 | if (ofs->config.upperdir) { |
| 1617 | if (!ofs->config.workdir) { |
| 1618 | pr_err("overlayfs: missing 'workdir'\n"); |
| 1619 | goto out_err; |
| 1620 | } |
| 1621 | |
| 1622 | err = ovl_get_upper(sb, ofs, &upperpath); |
| 1623 | if (err) |
| 1624 | goto out_err; |
| 1625 | |
| 1626 | err = ovl_get_workdir(sb, ofs, &upperpath); |
| 1627 | if (err) |
| 1628 | goto out_err; |
| 1629 | |
| 1630 | if (!ofs->workdir) |
| 1631 | sb->s_flags |= SB_RDONLY; |
| 1632 | |
| 1633 | sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth; |
| 1634 | sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran; |
| 1635 | |
| 1636 | } |
| 1637 | oe = ovl_get_lowerstack(sb, ofs); |
| 1638 | err = PTR_ERR(oe); |
| 1639 | if (IS_ERR(oe)) |
| 1640 | goto out_err; |
| 1641 | |
| 1642 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
| 1643 | if (!ofs->upper_mnt) |
| 1644 | sb->s_flags |= SB_RDONLY; |
| 1645 | |
| 1646 | if (!(ovl_force_readonly(ofs)) && ofs->config.index) { |
| 1647 | err = ovl_get_indexdir(sb, ofs, oe, &upperpath); |
| 1648 | if (err) |
| 1649 | goto out_free_oe; |
| 1650 | |
| 1651 | /* Force r/o mount with no index dir */ |
| 1652 | if (!ofs->indexdir) { |
| 1653 | dput(ofs->workdir); |
| 1654 | ofs->workdir = NULL; |
| 1655 | sb->s_flags |= SB_RDONLY; |
| 1656 | } |
| 1657 | |
| 1658 | } |
| 1659 | |
| 1660 | err = ovl_check_overlapping_layers(sb, ofs); |
| 1661 | if (err) |
| 1662 | goto out_free_oe; |
| 1663 | |
| 1664 | /* Show index=off in /proc/mounts for forced r/o mount */ |
| 1665 | if (!ofs->indexdir) { |
| 1666 | ofs->config.index = false; |
| 1667 | if (ofs->upper_mnt && ofs->config.nfs_export) { |
| 1668 | pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n"); |
| 1669 | ofs->config.nfs_export = false; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | if (ofs->config.metacopy && ofs->config.nfs_export) { |
| 1674 | pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n"); |
| 1675 | ofs->config.nfs_export = false; |
| 1676 | } |
| 1677 | |
| 1678 | if (ofs->config.nfs_export) |
| 1679 | sb->s_export_op = &ovl_export_operations; |
| 1680 | |
| 1681 | /* Never override disk quota limits or use reserved space */ |
| 1682 | cap_lower(cred->cap_effective, CAP_SYS_RESOURCE); |
| 1683 | |
| 1684 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
| 1685 | sb->s_xattr = ovl_xattr_handlers; |
| 1686 | sb->s_fs_info = ofs; |
| 1687 | sb->s_flags |= SB_POSIXACL; |
| 1688 | |
| 1689 | err = -ENOMEM; |
| 1690 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); |
| 1691 | if (!root_dentry) |
| 1692 | goto out_free_oe; |
| 1693 | |
| 1694 | root_dentry->d_fsdata = oe; |
| 1695 | |
| 1696 | mntput(upperpath.mnt); |
| 1697 | if (upperpath.dentry) { |
| 1698 | ovl_dentry_set_upper_alias(root_dentry); |
| 1699 | if (ovl_is_impuredir(upperpath.dentry)) |
| 1700 | ovl_set_flag(OVL_IMPURE, d_inode(root_dentry)); |
| 1701 | } |
| 1702 | |
| 1703 | /* Root is always merge -> can have whiteouts */ |
| 1704 | ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry)); |
| 1705 | ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry); |
| 1706 | ovl_set_upperdata(d_inode(root_dentry)); |
| 1707 | ovl_inode_init(d_inode(root_dentry), upperpath.dentry, |
| 1708 | ovl_dentry_lower(root_dentry), NULL); |
| 1709 | |
| 1710 | sb->s_root = root_dentry; |
| 1711 | return 0; |
| 1712 | |
| 1713 | out_free_oe: |
| 1714 | ovl_entry_stack_free(oe); |
| 1715 | kfree(oe); |
| 1716 | out_err: |
| 1717 | path_put(&upperpath); |
| 1718 | ovl_free_fs(ofs); |
| 1719 | out: |
| 1720 | return err; |
| 1721 | } |
| 1722 | |
| 1723 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, |
| 1724 | const char *dev_name, void *raw_data) |
| 1725 | { |
| 1726 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); |
| 1727 | } |
| 1728 | |
| 1729 | static struct file_system_type ovl_fs_type = { |
| 1730 | .owner = THIS_MODULE, |
| 1731 | .name = "overlay", |
| 1732 | .mount = ovl_mount, |
| 1733 | .kill_sb = kill_anon_super, |
| 1734 | }; |
| 1735 | MODULE_ALIAS_FS("overlay"); |
| 1736 | |
| 1737 | static void ovl_inode_init_once(void *foo) |
| 1738 | { |
| 1739 | struct ovl_inode *oi = foo; |
| 1740 | |
| 1741 | inode_init_once(&oi->vfs_inode); |
| 1742 | } |
| 1743 | |
| 1744 | static int __init ovl_init(void) |
| 1745 | { |
| 1746 | int err; |
| 1747 | |
| 1748 | ovl_inode_cachep = kmem_cache_create("ovl_inode", |
| 1749 | sizeof(struct ovl_inode), 0, |
| 1750 | (SLAB_RECLAIM_ACCOUNT| |
| 1751 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), |
| 1752 | ovl_inode_init_once); |
| 1753 | if (ovl_inode_cachep == NULL) |
| 1754 | return -ENOMEM; |
| 1755 | |
| 1756 | err = register_filesystem(&ovl_fs_type); |
| 1757 | if (err) |
| 1758 | kmem_cache_destroy(ovl_inode_cachep); |
| 1759 | |
| 1760 | return err; |
| 1761 | } |
| 1762 | |
| 1763 | static void __exit ovl_exit(void) |
| 1764 | { |
| 1765 | unregister_filesystem(&ovl_fs_type); |
| 1766 | |
| 1767 | /* |
| 1768 | * Make sure all delayed rcu free inodes are flushed before we |
| 1769 | * destroy cache. |
| 1770 | */ |
| 1771 | rcu_barrier(); |
| 1772 | kmem_cache_destroy(ovl_inode_cachep); |
| 1773 | |
| 1774 | } |
| 1775 | |
| 1776 | module_init(ovl_init); |
| 1777 | module_exit(ovl_exit); |