1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * 4 * Copyright (C) 2011 Novell Inc. 5 */ 6 7 #include <linux/kernel.h> 8 #include <linux/uuid.h> 9 #include <linux/fs.h> 10 #include <linux/fsverity.h> 11 #include <linux/namei.h> 12 #include <linux/posix_acl.h> 13 #include <linux/posix_acl_xattr.h> 14 #include "ovl_entry.h" 15 16 #undef pr_fmt 17 #define pr_fmt(fmt) "overlayfs: " fmt 18 19 #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH 20 21 enum ovl_path_type { 22 __OVL_PATH_UPPER = (1 << 0), 23 __OVL_PATH_MERGE = (1 << 1), 24 __OVL_PATH_ORIGIN = (1 << 2), 25 }; 26 27 #define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER) 28 #define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE) 29 #define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN) 30 31 #define OVL_XATTR_NAMESPACE "overlay." 32 #define OVL_XATTR_TRUSTED_PREFIX XATTR_TRUSTED_PREFIX OVL_XATTR_NAMESPACE 33 #define OVL_XATTR_TRUSTED_PREFIX_LEN (sizeof(OVL_XATTR_TRUSTED_PREFIX) - 1) 34 #define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE 35 #define OVL_XATTR_USER_PREFIX_LEN (sizeof(OVL_XATTR_USER_PREFIX) - 1) 36 37 #define OVL_XATTR_ESCAPE_PREFIX OVL_XATTR_NAMESPACE 38 #define OVL_XATTR_ESCAPE_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_PREFIX) - 1) 39 #define OVL_XATTR_ESCAPE_TRUSTED_PREFIX OVL_XATTR_TRUSTED_PREFIX OVL_XATTR_ESCAPE_PREFIX 40 #define OVL_XATTR_ESCAPE_TRUSTED_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_TRUSTED_PREFIX) - 1) 41 #define OVL_XATTR_ESCAPE_USER_PREFIX OVL_XATTR_USER_PREFIX OVL_XATTR_ESCAPE_PREFIX 42 #define OVL_XATTR_ESCAPE_USER_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_USER_PREFIX) - 1) 43 44 enum ovl_xattr { 45 OVL_XATTR_OPAQUE, 46 OVL_XATTR_REDIRECT, 47 OVL_XATTR_ORIGIN, 48 OVL_XATTR_IMPURE, 49 OVL_XATTR_NLINK, 50 OVL_XATTR_UPPER, 51 OVL_XATTR_UUID, 52 OVL_XATTR_METACOPY, 53 OVL_XATTR_PROTATTR, 54 OVL_XATTR_XWHITEOUT, 55 }; 56 57 enum ovl_inode_flag { 58 /* Pure upper dir that may contain non pure upper entries */ 59 OVL_IMPURE, 60 /* Non-merge dir that may contain whiteout entries */ 61 OVL_WHITEOUTS, 62 OVL_INDEX, 63 OVL_UPPERDATA, 64 /* Inode number will remain constant over copy up. */ 65 OVL_CONST_INO, 66 OVL_HAS_DIGEST, 67 OVL_VERIFIED_DIGEST, 68 }; 69 70 enum ovl_entry_flag { 71 OVL_E_UPPER_ALIAS, 72 OVL_E_OPAQUE, 73 OVL_E_CONNECTED, 74 /* Lower stack may contain xwhiteout entries */ 75 OVL_E_XWHITEOUTS, 76 }; 77 78 enum { 79 /* 80 * "off" mode is never used after mount options are verified. 81 * In effect it translates to either "follow", "nofollow" or 82 * "followdata" (with data-only layers). 83 */ 84 OVL_REDIRECT_OFF, 85 OVL_REDIRECT_FOLLOW, 86 OVL_REDIRECT_FOLLOWDATA, 87 OVL_REDIRECT_NOFOLLOW, 88 OVL_REDIRECT_ON, 89 }; 90 91 enum { 92 OVL_UUID_OFF, 93 OVL_UUID_NULL, 94 OVL_UUID_AUTO, 95 OVL_UUID_ON, 96 }; 97 98 enum { 99 OVL_XINO_OFF, 100 OVL_XINO_AUTO, 101 OVL_XINO_ON, 102 }; 103 104 enum { 105 OVL_VERITY_OFF, 106 OVL_VERITY_ON, 107 OVL_VERITY_REQUIRE, 108 }; 109 110 enum { 111 OVL_FSYNC_VOLATILE, 112 OVL_FSYNC_AUTO, 113 OVL_FSYNC_STRICT, 114 }; 115 116 /* 117 * The tuple (fh,uuid) is a universal unique identifier for a copy up origin, 118 * where: 119 * origin.fh - exported file handle of the lower file 120 * origin.uuid - uuid of the lower filesystem 121 */ 122 #define OVL_FH_VERSION 0 123 #define OVL_FH_MAGIC 0xfb 124 125 /* CPU byte order required for fid decoding: */ 126 #define OVL_FH_FLAG_BIG_ENDIAN (1 << 0) 127 #define OVL_FH_FLAG_ANY_ENDIAN (1 << 1) 128 /* Is the real inode encoded in fid an upper inode? */ 129 #define OVL_FH_FLAG_PATH_UPPER (1 << 2) 130 131 #define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \ 132 OVL_FH_FLAG_PATH_UPPER) 133 134 #if defined(__LITTLE_ENDIAN) 135 #define OVL_FH_FLAG_CPU_ENDIAN 0 136 #elif defined(__BIG_ENDIAN) 137 #define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN 138 #else 139 #error Endianness not defined 140 #endif 141 142 /* The type used to be returned by overlay exportfs for misaligned fid */ 143 #define OVL_FILEID_V0 0xfb 144 /* The type returned by overlay exportfs for 32bit aligned fid */ 145 #define OVL_FILEID_V1 0xf8 146 147 /* On-disk format for "origin" file handle */ 148 struct ovl_fb { 149 u8 version; /* 0 */ 150 u8 magic; /* 0xfb */ 151 u8 len; /* size of this header + size of fid */ 152 u8 flags; /* OVL_FH_FLAG_* */ 153 u8 type; /* fid_type of fid */ 154 uuid_t uuid; /* uuid of filesystem */ 155 u32 fid[]; /* file identifier should be 32bit aligned in-memory */ 156 } __packed; 157 158 /* In-memory and on-wire format for overlay file handle */ 159 struct ovl_fh { 160 u8 padding[3]; /* make sure fb.fid is 32bit aligned */ 161 union { 162 struct ovl_fb fb; 163 DECLARE_FLEX_ARRAY(u8, buf); 164 }; 165 } __packed; 166 167 #define OVL_FH_WIRE_OFFSET offsetof(struct ovl_fh, fb) 168 #define OVL_FH_LEN(fh) (OVL_FH_WIRE_OFFSET + (fh)->fb.len) 169 #define OVL_FH_FID_OFFSET (OVL_FH_WIRE_OFFSET + \ 170 offsetof(struct ovl_fb, fid)) 171 172 /* On-disk format for "metacopy" xattr (if non-zero size) */ 173 struct ovl_metacopy { 174 u8 version; /* 0 */ 175 u8 len; /* size of this header + used digest bytes */ 176 u8 flags; 177 u8 digest_algo; /* FS_VERITY_HASH_ALG_* constant, 0 for no digest */ 178 u8 digest[FS_VERITY_MAX_DIGEST_SIZE]; /* Only the used part on disk */ 179 } __packed; 180 181 #define OVL_METACOPY_MAX_SIZE (sizeof(struct ovl_metacopy)) 182 #define OVL_METACOPY_MIN_SIZE (OVL_METACOPY_MAX_SIZE - FS_VERITY_MAX_DIGEST_SIZE) 183 #define OVL_METACOPY_INIT { 0, OVL_METACOPY_MIN_SIZE } 184 185 static inline int ovl_metadata_digest_size(const struct ovl_metacopy *metacopy) 186 { 187 if (metacopy->len < OVL_METACOPY_MIN_SIZE) 188 return 0; 189 return (int)metacopy->len - OVL_METACOPY_MIN_SIZE; 190 } 191 192 /* No atime modification on underlying */ 193 #define OVL_OPEN_FLAGS (O_NOATIME) 194 195 extern const char *const ovl_xattr_table[][2]; 196 static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox) 197 { 198 return ovl_xattr_table[ox][ofs->config.userxattr]; 199 } 200 201 /* 202 * When changing ownership of an upper object map the intended ownership 203 * according to the upper layer's idmapping. When an upper mount idmaps files 204 * that are stored on-disk as owned by id 1001 to id 1000 this means stat on 205 * this object will report it as being owned by id 1000 when calling stat via 206 * the upper mount. 207 * In order to change ownership of an object so stat reports id 1000 when 208 * called on an idmapped upper mount the value written to disk - i.e., the 209 * value stored in ia_*id - must 1001. The mount mapping helper will thus take 210 * care to map 1000 to 1001. 211 * The mnt idmapping helpers are nops if the upper layer isn't idmapped. 212 */ 213 static inline int ovl_do_notify_change(struct ovl_fs *ofs, 214 struct dentry *upperdentry, 215 struct iattr *attr) 216 { 217 return notify_change(ovl_upper_mnt_idmap(ofs), upperdentry, attr, NULL); 218 } 219 220 static inline int ovl_do_rmdir(struct ovl_fs *ofs, 221 struct inode *dir, struct dentry *dentry) 222 { 223 int err = vfs_rmdir(ovl_upper_mnt_idmap(ofs), dir, dentry, NULL); 224 225 pr_debug("rmdir(%pd2) = %i\n", dentry, err); 226 return err; 227 } 228 229 static inline int ovl_do_unlink(struct ovl_fs *ofs, struct inode *dir, 230 struct dentry *dentry) 231 { 232 int err = vfs_unlink(ovl_upper_mnt_idmap(ofs), dir, dentry, NULL); 233 234 pr_debug("unlink(%pd2) = %i\n", dentry, err); 235 return err; 236 } 237 238 static inline int ovl_do_link(struct ovl_fs *ofs, struct dentry *old_dentry, 239 struct inode *dir, struct dentry *new_dentry) 240 { 241 int err = vfs_link(old_dentry, ovl_upper_mnt_idmap(ofs), dir, 242 new_dentry, NULL); 243 244 pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err); 245 return err; 246 } 247 248 static inline int ovl_do_create(struct ovl_fs *ofs, 249 struct inode *dir, struct dentry *dentry, 250 umode_t mode) 251 { 252 int err = vfs_create(ovl_upper_mnt_idmap(ofs), dentry, mode, NULL); 253 254 pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err); 255 return err; 256 } 257 258 static inline struct dentry *ovl_do_mkdir(struct ovl_fs *ofs, 259 struct inode *dir, 260 struct dentry *dentry, 261 umode_t mode) 262 { 263 struct dentry *ret; 264 265 /* vfs_mkdir() drops @dentry on failure and may replace it on success */ 266 pr_debug("mkdir(%pd2, 0%o)\n", dentry, mode); 267 ret = vfs_mkdir(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, NULL); 268 pr_debug("...mkdir = %i\n", PTR_ERR_OR_ZERO(ret)); 269 return ret; 270 } 271 272 static inline int ovl_do_mknod(struct ovl_fs *ofs, 273 struct inode *dir, struct dentry *dentry, 274 umode_t mode, dev_t dev) 275 { 276 int err = vfs_mknod(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, dev, NULL); 277 278 pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err); 279 return err; 280 } 281 282 static inline int ovl_do_symlink(struct ovl_fs *ofs, 283 struct inode *dir, struct dentry *dentry, 284 const char *oldname) 285 { 286 int err = vfs_symlink(ovl_upper_mnt_idmap(ofs), dir, dentry, oldname, NULL); 287 288 pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err); 289 return err; 290 } 291 292 static inline ssize_t ovl_do_getxattr(const struct path *path, const char *name, 293 void *value, size_t size) 294 { 295 int err, len; 296 297 WARN_ON(path->dentry->d_sb != path->mnt->mnt_sb); 298 299 err = vfs_getxattr(mnt_idmap(path->mnt), path->dentry, 300 name, value, size); 301 len = (value && err > 0) ? err : 0; 302 303 pr_debug("getxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n", 304 path->dentry, name, min(len, 48), value, size, err); 305 return err; 306 } 307 308 static inline ssize_t ovl_getxattr_upper(struct ovl_fs *ofs, 309 struct dentry *upperdentry, 310 enum ovl_xattr ox, void *value, 311 size_t size) 312 { 313 struct path upperpath = { 314 .dentry = upperdentry, 315 .mnt = ovl_upper_mnt(ofs), 316 }; 317 318 return ovl_do_getxattr(&upperpath, ovl_xattr(ofs, ox), value, size); 319 } 320 321 static inline ssize_t ovl_path_getxattr(struct ovl_fs *ofs, 322 const struct path *path, 323 enum ovl_xattr ox, void *value, 324 size_t size) 325 { 326 return ovl_do_getxattr(path, ovl_xattr(ofs, ox), value, size); 327 } 328 329 static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry, 330 const char *name, const void *value, 331 size_t size, int flags) 332 { 333 /* Use vfs_setxattr(), not __vfs_setxattr(): it idmaps the security.capability rootid. */ 334 int err = vfs_setxattr(ovl_upper_mnt_idmap(ofs), dentry, name, 335 value, size, flags); 336 337 pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, %d) = %i\n", 338 dentry, name, min((int)size, 48), value, size, flags, err); 339 return err; 340 } 341 342 static inline int ovl_setxattr(struct ovl_fs *ofs, struct dentry *dentry, 343 enum ovl_xattr ox, const void *value, 344 size_t size) 345 { 346 return ovl_do_setxattr(ofs, dentry, ovl_xattr(ofs, ox), value, size, 0); 347 } 348 349 static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry, 350 const char *name) 351 { 352 int err = vfs_removexattr(ovl_upper_mnt_idmap(ofs), dentry, name); 353 pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err); 354 return err; 355 } 356 357 static inline int ovl_removexattr(struct ovl_fs *ofs, struct dentry *dentry, 358 enum ovl_xattr ox) 359 { 360 return ovl_do_removexattr(ofs, dentry, ovl_xattr(ofs, ox)); 361 } 362 363 static inline int ovl_do_set_acl(struct ovl_fs *ofs, struct dentry *dentry, 364 const char *acl_name, struct posix_acl *acl) 365 { 366 return vfs_set_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name, acl); 367 } 368 369 static inline int ovl_do_remove_acl(struct ovl_fs *ofs, struct dentry *dentry, 370 const char *acl_name) 371 { 372 return vfs_remove_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name); 373 } 374 375 static inline int ovl_do_rename_rd(struct renamedata *rd) 376 { 377 int err; 378 379 pr_debug("rename(%pd2, %pd2, 0x%x)\n", rd->old_dentry, rd->new_dentry, 380 rd->flags); 381 err = vfs_rename(rd); 382 if (err) { 383 pr_debug("...rename(%pd2, %pd2, ...) = %i\n", 384 rd->old_dentry, rd->new_dentry, err); 385 } 386 return err; 387 } 388 389 static inline int ovl_do_rename(struct ovl_fs *ofs, struct dentry *olddir, 390 struct dentry *olddentry, struct dentry *newdir, 391 struct dentry *newdentry, unsigned int flags) 392 { 393 struct renamedata rd = { 394 .mnt_idmap = ovl_upper_mnt_idmap(ofs), 395 .old_parent = olddir, 396 .old_dentry = olddentry, 397 .new_parent = newdir, 398 .new_dentry = newdentry, 399 .flags = flags, 400 }; 401 402 return ovl_do_rename_rd(&rd); 403 } 404 405 static inline struct file *ovl_do_tmpfile(struct ovl_fs *ofs, 406 struct dentry *dentry, umode_t mode) 407 { 408 struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = dentry }; 409 struct file *file = kernel_tmpfile_open(ovl_upper_mnt_idmap(ofs), &path, 410 mode, O_LARGEFILE | O_WRONLY, 411 current_cred()); 412 int err = PTR_ERR_OR_ZERO(file); 413 414 pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err); 415 return file; 416 } 417 418 static inline struct dentry *ovl_lookup_upper_unlocked(struct ovl_fs *ofs, 419 const char *name, 420 struct dentry *base, 421 int len) 422 { 423 return lookup_one_unlocked(ovl_upper_mnt_idmap(ofs), 424 &QSTR_LEN(name, len), base); 425 } 426 427 static inline struct dentry *ovl_start_creating_upper(struct ovl_fs *ofs, 428 struct dentry *parent, 429 struct qstr *name) 430 { 431 return start_creating(ovl_upper_mnt_idmap(ofs), 432 parent, name); 433 } 434 435 static inline struct dentry *ovl_start_removing_upper(struct ovl_fs *ofs, 436 struct dentry *parent, 437 struct qstr *name) 438 { 439 return start_removing(ovl_upper_mnt_idmap(ofs), 440 parent, name); 441 } 442 443 static inline bool ovl_open_flags_need_copy_up(int flags) 444 { 445 if (!flags) 446 return false; 447 448 return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)); 449 } 450 451 /* util.c */ 452 int ovl_get_write_access(struct dentry *dentry); 453 void ovl_put_write_access(struct dentry *dentry); 454 void ovl_start_write(struct dentry *dentry); 455 void ovl_end_write(struct dentry *dentry); 456 int ovl_want_write(struct dentry *dentry); 457 void ovl_drop_write(struct dentry *dentry); 458 struct dentry *ovl_workdir(struct dentry *dentry); 459 const struct cred *ovl_override_creds(struct super_block *sb); 460 461 EXTEND_CLASS(override_creds, _ovl, ovl_override_creds(sb), struct super_block *sb) 462 463 #define with_ovl_creds(sb) \ 464 scoped_class(override_creds_ovl, __UNIQUE_ID(label), sb) 465 466 static inline const struct cred *ovl_creds(struct super_block *sb) 467 { 468 return OVL_FS(sb)->creator_cred; 469 } 470 471 int ovl_can_decode_fh(struct super_block *sb); 472 struct dentry *ovl_indexdir(struct super_block *sb); 473 bool ovl_index_all(struct super_block *sb); 474 bool ovl_verify_lower(struct super_block *sb); 475 struct ovl_path *ovl_stack_alloc(unsigned int n); 476 void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n); 477 void ovl_stack_put(struct ovl_path *stack, unsigned int n); 478 void ovl_stack_free(struct ovl_path *stack, unsigned int n); 479 struct ovl_entry *ovl_alloc_entry(unsigned int numlower); 480 void ovl_free_entry(struct ovl_entry *oe); 481 bool ovl_dentry_remote(struct dentry *dentry); 482 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry); 483 void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry, 484 struct ovl_entry *oe); 485 void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry, 486 struct ovl_entry *oe, unsigned int mask); 487 bool ovl_dentry_weird(struct dentry *dentry); 488 489 static inline bool ovl_dentry_casefolded(struct dentry *dentry) 490 { 491 return sb_has_encoding(dentry->d_sb) && IS_CASEFOLDED(d_inode(dentry)); 492 } 493 494 enum ovl_path_type ovl_path_type(struct dentry *dentry); 495 void ovl_path_upper(struct dentry *dentry, struct path *path); 496 void ovl_path_lower(struct dentry *dentry, struct path *path); 497 void ovl_path_lowerdata(struct dentry *dentry, struct path *path); 498 struct inode *ovl_i_path_real(struct inode *inode, struct path *path); 499 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path); 500 enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path); 501 struct dentry *ovl_dentry_upper(struct dentry *dentry); 502 struct dentry *ovl_dentry_lower(struct dentry *dentry); 503 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry); 504 int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath); 505 const struct ovl_layer *ovl_i_layer_lower(struct inode *inode); 506 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry); 507 struct dentry *ovl_dentry_real(struct dentry *dentry); 508 struct dentry *ovl_i_dentry_upper(struct inode *inode); 509 struct inode *ovl_inode_upper(struct inode *inode); 510 struct inode *ovl_inode_lower(struct inode *inode); 511 struct inode *ovl_inode_lowerdata(struct inode *inode); 512 struct inode *ovl_inode_real(struct inode *inode); 513 struct inode *ovl_inode_realdata(struct inode *inode); 514 const char *ovl_lowerdata_redirect(struct inode *inode); 515 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode); 516 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache); 517 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry); 518 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry); 519 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry); 520 bool ovl_dentry_is_opaque(struct dentry *dentry); 521 bool ovl_dentry_is_whiteout(struct dentry *dentry); 522 void ovl_dentry_set_opaque(struct dentry *dentry); 523 bool ovl_dentry_has_xwhiteouts(struct dentry *dentry); 524 void ovl_dentry_set_xwhiteouts(struct dentry *dentry); 525 void ovl_layer_set_xwhiteouts(struct ovl_fs *ofs, 526 const struct ovl_layer *layer); 527 bool ovl_dentry_has_upper_alias(struct dentry *dentry); 528 void ovl_dentry_set_upper_alias(struct dentry *dentry); 529 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags); 530 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags); 531 bool ovl_has_upperdata(struct inode *inode); 532 void ovl_set_upperdata(struct inode *inode); 533 const char *ovl_dentry_get_redirect(struct dentry *dentry); 534 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect); 535 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry); 536 void ovl_dir_modified(struct dentry *dentry, bool impurity); 537 u64 ovl_inode_version_get(struct inode *inode); 538 bool ovl_is_whiteout(struct dentry *dentry); 539 bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path); 540 struct file *ovl_path_open_realfile(const struct file *user_file, 541 const struct path *path, int flags); 542 543 static inline struct file *ovl_path_open(const struct path *path, int flags) 544 { 545 return ovl_path_open_realfile(NULL, path, flags); 546 } 547 548 int ovl_copy_up_start(struct dentry *dentry, int flags); 549 void ovl_copy_up_end(struct dentry *dentry); 550 bool ovl_already_copied_up(struct dentry *dentry, int flags); 551 char ovl_get_dir_xattr_val(struct ovl_fs *ofs, const struct path *path, 552 enum ovl_xattr ox); 553 bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path); 554 bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path); 555 bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs, 556 const struct path *upperpath); 557 558 static inline bool ovl_upper_is_whiteout(struct ovl_fs *ofs, 559 struct dentry *upperdentry) 560 { 561 struct path upperpath = { 562 .dentry = upperdentry, 563 .mnt = ovl_upper_mnt(ofs), 564 }; 565 return ovl_path_is_whiteout(ofs, &upperpath); 566 } 567 568 static inline bool ovl_check_origin_xattr(struct ovl_fs *ofs, 569 struct dentry *upperdentry) 570 { 571 struct path upperpath = { 572 .dentry = upperdentry, 573 .mnt = ovl_upper_mnt(ofs), 574 }; 575 return ovl_path_check_origin_xattr(ofs, &upperpath); 576 } 577 578 int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry, 579 enum ovl_xattr ox, const void *value, size_t size, 580 int xerr); 581 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry); 582 583 static inline int ovl_set_impure_dir(struct dentry *dentry) 584 { 585 return ovl_set_impure(dentry, ovl_dentry_upper(dentry)); 586 } 587 588 bool ovl_inuse_trylock(struct dentry *dentry); 589 void ovl_inuse_unlock(struct dentry *dentry); 590 bool ovl_is_inuse(struct dentry *dentry); 591 void ovl_inuse_unlock_all(struct ovl_fs *ofs); 592 bool ovl_need_index(struct dentry *dentry); 593 int ovl_nlink_start(struct dentry *dentry); 594 void ovl_nlink_end(struct dentry *dentry); 595 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path, 596 struct ovl_metacopy *data); 597 int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, 598 struct ovl_metacopy *metacopy); 599 bool ovl_is_metacopy_dentry(struct dentry *dentry); 600 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding); 601 int ovl_ensure_verity_loaded(const struct path *path); 602 int ovl_validate_verity(struct ovl_fs *ofs, 603 const struct path *metapath, 604 const struct path *datapath); 605 int ovl_get_verity_digest(struct ovl_fs *ofs, const struct path *src, 606 struct ovl_metacopy *metacopy); 607 int ovl_sync_status(struct ovl_fs *ofs); 608 609 static inline void ovl_set_flag(unsigned long flag, struct inode *inode) 610 { 611 set_bit(flag, &OVL_I(inode)->flags); 612 } 613 614 static inline void ovl_clear_flag(unsigned long flag, struct inode *inode) 615 { 616 clear_bit(flag, &OVL_I(inode)->flags); 617 } 618 619 static inline bool ovl_test_flag(unsigned long flag, struct inode *inode) 620 { 621 return test_bit(flag, &OVL_I(inode)->flags); 622 } 623 624 static inline bool ovl_is_impuredir(struct super_block *sb, 625 struct dentry *upperdentry) 626 { 627 struct ovl_fs *ofs = OVL_FS(sb); 628 struct path upperpath = { 629 .dentry = upperdentry, 630 .mnt = ovl_upper_mnt(ofs), 631 }; 632 633 return ovl_get_dir_xattr_val(ofs, &upperpath, OVL_XATTR_IMPURE) == 'y'; 634 } 635 636 static inline char ovl_get_opaquedir_val(struct ovl_fs *ofs, 637 const struct path *path) 638 { 639 return ovl_get_dir_xattr_val(ofs, path, OVL_XATTR_OPAQUE); 640 } 641 642 /* Are redirects followed into lower layers? */ 643 static inline bool ovl_redirect_follow_lower(struct ovl_fs *ofs) 644 { 645 return ofs->config.redirect_mode == OVL_REDIRECT_FOLLOW || 646 ofs->config.redirect_mode == OVL_REDIRECT_ON; 647 } 648 649 /* Are redirects followed into lower or lowerdata layers? */ 650 static inline bool ovl_redirect_follow(struct ovl_fs *ofs) 651 { 652 return ofs->config.redirect_mode != OVL_REDIRECT_NOFOLLOW; 653 } 654 655 /* Are redirects created in upper dir? */ 656 static inline bool ovl_redirect_dir(struct ovl_fs *ofs) 657 { 658 return ofs->config.redirect_mode == OVL_REDIRECT_ON; 659 } 660 661 static inline bool ovl_origin_uuid(struct ovl_fs *ofs) 662 { 663 return ofs->config.uuid != OVL_UUID_OFF; 664 } 665 666 static inline bool ovl_has_fsid(struct ovl_fs *ofs) 667 { 668 return ofs->config.uuid == OVL_UUID_ON || 669 ofs->config.uuid == OVL_UUID_AUTO; 670 } 671 672 /* 673 * With xino=auto, we do best effort to keep all inodes on same st_dev and 674 * d_ino consistent with st_ino. 675 * With xino=on, we do the same effort but we warn if we failed. 676 */ 677 static inline bool ovl_xino_warn(struct ovl_fs *ofs) 678 { 679 return ofs->config.xino == OVL_XINO_ON; 680 } 681 682 static inline bool ovl_should_sync(struct ovl_fs *ofs) 683 { 684 return ofs->config.fsync_mode != OVL_FSYNC_VOLATILE; 685 } 686 687 static inline bool ovl_should_sync_metadata(struct ovl_fs *ofs) 688 { 689 return ofs->config.fsync_mode == OVL_FSYNC_STRICT; 690 } 691 692 static inline bool ovl_is_volatile(struct ovl_config *config) 693 { 694 return config->fsync_mode == OVL_FSYNC_VOLATILE; 695 } 696 697 /* 698 * To avoid regressions in existing setups with overlay lower offline changes, 699 * we allow lower changes only if none of the new features are used. 700 */ 701 static inline bool ovl_allow_offline_changes(struct ovl_fs *ofs) 702 { 703 return (!ofs->config.index && !ofs->config.metacopy && 704 !ovl_redirect_dir(ofs) && !ovl_xino_warn(ofs)); 705 } 706 707 /* All layers on same fs? */ 708 static inline bool ovl_same_fs(struct ovl_fs *ofs) 709 { 710 return ofs->xino_mode == 0; 711 } 712 713 /* All overlay inodes have same st_dev? */ 714 static inline bool ovl_same_dev(struct ovl_fs *ofs) 715 { 716 return ofs->xino_mode >= 0; 717 } 718 719 static inline unsigned int ovl_xino_bits(struct ovl_fs *ofs) 720 { 721 return ovl_same_dev(ofs) ? ofs->xino_mode : 0; 722 } 723 724 static inline void ovl_inode_lock(struct inode *inode) 725 { 726 mutex_lock(&OVL_I(inode)->lock); 727 } 728 729 static inline int ovl_inode_lock_interruptible(struct inode *inode) 730 { 731 return mutex_lock_interruptible(&OVL_I(inode)->lock); 732 } 733 734 static inline void ovl_inode_unlock(struct inode *inode) 735 { 736 mutex_unlock(&OVL_I(inode)->lock); 737 } 738 739 740 /* namei.c */ 741 int ovl_check_fb_len(struct ovl_fb *fb, int fb_len); 742 743 static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len) 744 { 745 if (fh_len < sizeof(struct ovl_fh)) 746 return -EINVAL; 747 748 return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET); 749 } 750 751 bool ovl_uuid_match(struct ovl_fs *ofs, const struct super_block *sb, 752 const uuid_t *uuid); 753 struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh, 754 struct vfsmount *mnt, bool connected); 755 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected, 756 struct dentry *upperdentry, struct ovl_path **stackp); 757 int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry, 758 enum ovl_xattr ox, const struct ovl_fh *fh, 759 bool is_upper, bool set); 760 int ovl_verify_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry, 761 enum ovl_xattr ox, struct dentry *real, 762 bool is_upper, bool set); 763 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index, 764 bool connected); 765 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index); 766 int ovl_get_index_name_fh(const struct ovl_fh *fh, struct qstr *name); 767 int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin, 768 struct qstr *name); 769 struct dentry *ovl_get_index_origin(struct ovl_fs *ofs, struct dentry *origin); 770 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper, 771 struct dentry *origin, bool verify); 772 int ovl_path_next(int idx, struct dentry *dentry, struct path *path, 773 const struct ovl_layer **layer); 774 int ovl_verify_lowerdata(struct dentry *dentry); 775 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 776 unsigned int flags); 777 bool ovl_lower_positive(struct dentry *dentry); 778 779 static inline int ovl_verify_origin_fh(struct ovl_fs *ofs, struct dentry *upper, 780 const struct ovl_fh *fh, bool set) 781 { 782 return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, fh, false, set); 783 } 784 785 static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper, 786 struct dentry *origin, bool set) 787 { 788 return ovl_verify_origin_xattr(ofs, upper, OVL_XATTR_ORIGIN, origin, 789 false, set); 790 } 791 792 static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index, 793 struct dentry *upper, bool set) 794 { 795 return ovl_verify_origin_xattr(ofs, index, OVL_XATTR_UPPER, upper, 796 true, set); 797 } 798 799 /* readdir.c */ 800 extern const struct file_operations ovl_dir_operations; 801 struct file *ovl_dir_real_file(const struct file *file, bool want_upper); 802 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list); 803 void ovl_cleanup_whiteouts(struct ovl_fs *ofs, struct dentry *upper, 804 struct list_head *list); 805 void ovl_cache_free(struct list_head *list); 806 void ovl_dir_cache_free(struct inode *inode); 807 int ovl_check_d_type_supported(const struct path *realpath); 808 int ovl_workdir_cleanup(struct ovl_fs *ofs, struct dentry *parent, 809 struct vfsmount *mnt, struct dentry *dentry, int level); 810 int ovl_indexdir_cleanup(struct ovl_fs *ofs); 811 812 /* 813 * Can we iterate real dir directly? 814 * 815 * Non-merge dir may contain whiteouts from a time it was a merge upper, before 816 * lower dir was removed under it and possibly before it was rotated from upper 817 * to lower layer. 818 */ 819 static inline bool ovl_dir_is_real(struct inode *dir) 820 { 821 return !ovl_test_flag(OVL_WHITEOUTS, dir); 822 } 823 824 /* inode.c */ 825 int ovl_set_nlink_upper(struct dentry *dentry); 826 int ovl_set_nlink_lower(struct dentry *dentry); 827 unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry, 828 struct dentry *upperdentry, bool indexed, 829 unsigned int fallback); 830 int ovl_permission(const struct mnt_idmap *idmap, struct inode *inode, 831 int mask); 832 833 #ifdef CONFIG_FS_POSIX_ACL 834 struct posix_acl *do_ovl_get_acl(const struct mnt_idmap *idmap, 835 struct inode *inode, int type, 836 bool rcu, bool noperm); 837 static inline struct posix_acl *ovl_get_inode_acl(struct inode *inode, int type, 838 bool rcu) 839 { 840 return do_ovl_get_acl(&nop_mnt_idmap, inode, type, rcu, true); 841 } 842 static inline struct posix_acl *ovl_get_acl(const struct mnt_idmap *idmap, 843 struct dentry *dentry, int type) 844 { 845 return do_ovl_get_acl(idmap, d_inode(dentry), type, false, false); 846 } 847 int ovl_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry, 848 struct posix_acl *acl, int type); 849 struct posix_acl *ovl_get_acl_path(const struct path *path, 850 const char *acl_name, bool noperm); 851 #else 852 #define ovl_get_inode_acl NULL 853 #define ovl_get_acl NULL 854 #define ovl_set_acl NULL 855 static inline struct posix_acl *ovl_get_acl_path(const struct path *path, 856 const char *acl_name, 857 bool noperm) 858 { 859 return NULL; 860 } 861 #endif 862 863 int ovl_update_time(struct inode *inode, enum fs_update_time type, 864 unsigned int flags); 865 bool ovl_is_private_xattr(struct super_block *sb, const char *name); 866 867 struct ovl_inode_params { 868 struct inode *newinode; 869 struct dentry *upperdentry; 870 struct ovl_entry *oe; 871 bool index; 872 char *redirect; 873 char *lowerdata_redirect; 874 }; 875 void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip, 876 unsigned long ino, int fsid); 877 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev); 878 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real, 879 bool is_upper); 880 bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir); 881 struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir); 882 struct inode *ovl_get_inode(struct super_block *sb, 883 struct ovl_inode_params *oip); 884 void ovl_copyattr(struct inode *to); 885 886 /* vfs fileattr flags read from overlay.protattr xattr to ovl inode */ 887 #define OVL_PROT_I_FLAGS_MASK (S_APPEND | S_IMMUTABLE) 888 /* vfs fileattr flags copied from real to ovl inode */ 889 #define OVL_FATTR_I_FLAGS_MASK (OVL_PROT_I_FLAGS_MASK | S_SYNC | S_NOATIME) 890 /* vfs inode flags copied from real to ovl inode */ 891 #define OVL_COPY_I_FLAGS_MASK (OVL_FATTR_I_FLAGS_MASK | S_CASEFOLD) 892 893 /* 894 * fileattr flags copied from lower to upper inode on copy up. 895 * We cannot copy up immutable/append-only flags, because that would prevent 896 * linking temp inode to upper dir, so we store them in xattr instead. 897 */ 898 #define OVL_COPY_FS_FLAGS_MASK (FS_SYNC_FL | FS_NOATIME_FL) 899 #define OVL_COPY_FSX_FLAGS_MASK (FS_XFLAG_SYNC | FS_XFLAG_NOATIME) 900 #define OVL_PROT_FS_FLAGS_MASK (FS_APPEND_FL | FS_IMMUTABLE_FL) 901 #define OVL_PROT_FSX_FLAGS_MASK (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE) 902 903 void ovl_check_protattr(struct inode *inode, struct dentry *upper); 904 int ovl_set_protattr(struct inode *inode, struct dentry *upper, 905 struct file_kattr *fa); 906 907 static inline void ovl_copyflags(struct inode *from, struct inode *to) 908 { 909 unsigned int mask = OVL_COPY_I_FLAGS_MASK; 910 911 inode_set_flags(to, from->i_flags & mask, mask); 912 } 913 914 /* dir.c */ 915 extern const struct inode_operations ovl_dir_inode_operations; 916 int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct dentry *dir, 917 struct dentry *dentry, const struct qstr *name); 918 struct ovl_cattr { 919 dev_t rdev; 920 umode_t mode; 921 const char *link; 922 struct dentry *hardlink; 923 }; 924 925 #define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) }) 926 927 struct dentry *ovl_create_real(struct ovl_fs *ofs, 928 struct dentry *parent, struct dentry *newdentry, 929 struct qstr *qname, 930 struct ovl_cattr *attr); 931 int ovl_cleanup(struct ovl_fs *ofs, struct dentry *workdir, struct dentry *dentry); 932 #define OVL_TEMPNAME_SIZE 20 933 void ovl_tempname(char name[OVL_TEMPNAME_SIZE]); 934 struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir, 935 struct ovl_cattr *attr); 936 937 /* file.c */ 938 extern const struct file_operations ovl_file_operations; 939 int ovl_real_fileattr_get(const struct path *realpath, struct file_kattr *fa); 940 int ovl_real_fileattr_set(const struct path *realpath, struct file_kattr *fa); 941 int ovl_fileattr_get(struct dentry *dentry, struct file_kattr *fa); 942 int ovl_fileattr_set(const struct mnt_idmap *idmap, 943 struct dentry *dentry, struct file_kattr *fa); 944 struct ovl_file; 945 struct ovl_file *ovl_file_alloc(struct file *realfile); 946 void ovl_file_free(struct ovl_file *of); 947 948 /* copy_up.c */ 949 int ovl_copy_up(struct dentry *dentry); 950 int ovl_copy_up_with_data(struct dentry *dentry); 951 int ovl_maybe_copy_up(struct dentry *dentry, int flags); 952 int ovl_copy_xattr(struct super_block *sb, const struct path *path, struct dentry *new); 953 int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upper, struct kstat *stat); 954 struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct inode *realinode, 955 bool is_upper); 956 struct ovl_fh *ovl_get_origin_fh(struct ovl_fs *ofs, struct dentry *origin); 957 int ovl_set_origin_fh(struct ovl_fs *ofs, const struct ovl_fh *fh, 958 struct dentry *upper); 959 960 /* export.c */ 961 extern const struct export_operations ovl_export_operations; 962 extern const struct export_operations ovl_export_fid_operations; 963 964 /* super.c */ 965 int ovl_fill_super(struct super_block *sb, struct fs_context *fc); 966 967 /* Will this overlay be forced to mount/remount ro? */ 968 static inline bool ovl_force_readonly(struct ovl_fs *ofs) 969 { 970 return (!ovl_upper_mnt(ofs) || !ofs->workdir); 971 } 972 973 /* xattr.c */ 974 975 const struct xattr_handler * const *ovl_xattr_handlers(struct ovl_fs *ofs); 976 int ovl_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, 977 struct iattr *attr); 978 int ovl_getattr(const struct mnt_idmap *idmap, const struct path *path, 979 struct kstat *stat, u32 request_mask, unsigned int flags); 980 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size); 981