reparse.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2024 Paulo Alcantara <pc@manguebit.com>
  4. */
  5. #ifndef _CIFS_REPARSE_H
  6. #define _CIFS_REPARSE_H
  7. #include <linux/fs.h>
  8. #include <linux/stat.h>
  9. #include <linux/uidgid.h>
  10. #include "fs_context.h"
  11. #include "cifsglob.h"
  12. #define REPARSE_SYM_PATH_MAX 4060
  13. /*
  14. * Used only by cifs.ko to ignore reparse points from files when client or
  15. * server doesn't support FSCTL_GET_REPARSE_POINT.
  16. */
  17. #define IO_REPARSE_TAG_INTERNAL ((__u32)~0U)
  18. static inline dev_t reparse_mkdev(void *ptr)
  19. {
  20. u64 v = le64_to_cpu(*(__le64 *)ptr);
  21. return MKDEV(v & 0xffffffff, v >> 32);
  22. }
  23. static inline kuid_t wsl_make_kuid(struct cifs_sb_info *cifs_sb,
  24. void *ptr)
  25. {
  26. u32 uid = le32_to_cpu(*(__le32 *)ptr);
  27. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  28. return cifs_sb->ctx->linux_uid;
  29. return make_kuid(current_user_ns(), uid);
  30. }
  31. static inline kgid_t wsl_make_kgid(struct cifs_sb_info *cifs_sb,
  32. void *ptr)
  33. {
  34. u32 gid = le32_to_cpu(*(__le32 *)ptr);
  35. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  36. return cifs_sb->ctx->linux_gid;
  37. return make_kgid(current_user_ns(), gid);
  38. }
  39. static inline u64 reparse_mode_nfs_type(mode_t mode)
  40. {
  41. switch (mode & S_IFMT) {
  42. case S_IFBLK: return NFS_SPECFILE_BLK;
  43. case S_IFCHR: return NFS_SPECFILE_CHR;
  44. case S_IFIFO: return NFS_SPECFILE_FIFO;
  45. case S_IFSOCK: return NFS_SPECFILE_SOCK;
  46. }
  47. return 0;
  48. }
  49. static inline u32 reparse_mode_wsl_tag(mode_t mode)
  50. {
  51. switch (mode & S_IFMT) {
  52. case S_IFBLK: return IO_REPARSE_TAG_LX_BLK;
  53. case S_IFCHR: return IO_REPARSE_TAG_LX_CHR;
  54. case S_IFIFO: return IO_REPARSE_TAG_LX_FIFO;
  55. case S_IFSOCK: return IO_REPARSE_TAG_AF_UNIX;
  56. }
  57. return 0;
  58. }
  59. /*
  60. * Match a reparse point inode if reparse tag and ctime haven't changed.
  61. *
  62. * Windows Server updates ctime of reparse points when their data have changed.
  63. * The server doesn't allow changing reparse tags from existing reparse points,
  64. * though it's worth checking.
  65. */
  66. static inline bool reparse_inode_match(struct inode *inode,
  67. struct cifs_fattr *fattr)
  68. {
  69. struct cifsInodeInfo *cinode = CIFS_I(inode);
  70. struct timespec64 ctime = inode_get_ctime(inode);
  71. /*
  72. * Do not match reparse tags when client or server doesn't support
  73. * FSCTL_GET_REPARSE_POINT. @fattr->cf_cifstag should contain correct
  74. * reparse tag from query dir response but the client won't be able to
  75. * read the reparse point data anyway. This spares us a revalidation.
  76. */
  77. if (cinode->reparse_tag != IO_REPARSE_TAG_INTERNAL &&
  78. cinode->reparse_tag != fattr->cf_cifstag)
  79. return false;
  80. return (cinode->cifsAttrs & ATTR_REPARSE) &&
  81. timespec64_equal(&ctime, &fattr->cf_ctime);
  82. }
  83. static inline bool cifs_open_data_reparse(struct cifs_open_info_data *data)
  84. {
  85. u32 attrs;
  86. bool ret;
  87. if (data->contains_posix_file_info) {
  88. struct smb311_posix_qinfo *fi = &data->posix_fi;
  89. attrs = le32_to_cpu(fi->DosAttributes);
  90. if (data->reparse_point) {
  91. attrs |= ATTR_REPARSE;
  92. fi->DosAttributes = cpu_to_le32(attrs);
  93. }
  94. } else {
  95. struct smb2_file_all_info *fi = &data->fi;
  96. attrs = le32_to_cpu(fi->Attributes);
  97. if (data->reparse_point) {
  98. attrs |= ATTR_REPARSE;
  99. fi->Attributes = cpu_to_le32(attrs);
  100. }
  101. }
  102. ret = attrs & ATTR_REPARSE;
  103. return ret;
  104. }
  105. bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
  106. struct cifs_fattr *fattr,
  107. struct cifs_open_info_data *data);
  108. int smb2_create_reparse_symlink(const unsigned int xid, struct inode *inode,
  109. struct dentry *dentry, struct cifs_tcon *tcon,
  110. const char *full_path, const char *symname);
  111. int smb2_mknod_reparse(unsigned int xid, struct inode *inode,
  112. struct dentry *dentry, struct cifs_tcon *tcon,
  113. const char *full_path, umode_t mode, dev_t dev);
  114. int smb2_parse_reparse_point(struct cifs_sb_info *cifs_sb,
  115. const char *full_path,
  116. struct kvec *rsp_iov,
  117. struct cifs_open_info_data *data);
  118. #endif /* _CIFS_REPARSE_H */