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