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