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