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 
reparse_mkdev(void * ptr)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 
wsl_make_kuid(struct cifs_sb_info * cifs_sb,void * ptr)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 
wsl_make_kgid(struct cifs_sb_info * cifs_sb,void * ptr)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 
reparse_mode_nfs_type(mode_t mode)50 static inline u64 reparse_mode_nfs_type(mode_t mode)
51 {
52 	switch (mode & S_IFMT) {
53 	case S_IFBLK: return NFS_SPECFILE_BLK;
54 	case S_IFCHR: return NFS_SPECFILE_CHR;
55 	case S_IFIFO: return NFS_SPECFILE_FIFO;
56 	case S_IFSOCK: return NFS_SPECFILE_SOCK;
57 	}
58 	return 0;
59 }
60 
reparse_mode_wsl_tag(mode_t mode)61 static inline u32 reparse_mode_wsl_tag(mode_t mode)
62 {
63 	switch (mode & S_IFMT) {
64 	case S_IFBLK: return IO_REPARSE_TAG_LX_BLK;
65 	case S_IFCHR: return IO_REPARSE_TAG_LX_CHR;
66 	case S_IFIFO: return IO_REPARSE_TAG_LX_FIFO;
67 	case S_IFSOCK: return IO_REPARSE_TAG_AF_UNIX;
68 	}
69 	return 0;
70 }
71 
72 /*
73  * Match a reparse point inode if reparse tag and ctime haven't changed.
74  *
75  * Windows Server updates ctime of reparse points when their data have changed.
76  * The server doesn't allow changing reparse tags from existing reparse points,
77  * though it's worth checking.
78  */
reparse_inode_match(struct inode * inode,struct cifs_fattr * fattr)79 static inline bool reparse_inode_match(struct inode *inode,
80 				       struct cifs_fattr *fattr)
81 {
82 	struct cifsInodeInfo *cinode = CIFS_I(inode);
83 	struct timespec64 ctime = inode_get_ctime(inode);
84 
85 	/*
86 	 * Do not match reparse tags when client or server doesn't support
87 	 * FSCTL_GET_REPARSE_POINT.  @fattr->cf_cifstag should contain correct
88 	 * reparse tag from query dir response but the client won't be able to
89 	 * read the reparse point data anyway.  This spares us a revalidation.
90 	 */
91 	if (cinode->reparse_tag != IO_REPARSE_TAG_INTERNAL &&
92 	    cinode->reparse_tag != fattr->cf_cifstag)
93 		return false;
94 	return (cinode->cifsAttrs & ATTR_REPARSE) &&
95 		timespec64_equal(&ctime, &fattr->cf_ctime);
96 }
97 
cifs_open_data_reparse(struct cifs_open_info_data * data)98 static inline bool cifs_open_data_reparse(struct cifs_open_info_data *data)
99 {
100 	u32 attrs;
101 	bool ret;
102 
103 	if (data->contains_posix_file_info) {
104 		struct smb311_posix_qinfo *fi = &data->posix_fi;
105 
106 		attrs = le32_to_cpu(fi->DosAttributes);
107 		if (data->reparse_point) {
108 			attrs |= ATTR_REPARSE;
109 			fi->DosAttributes = cpu_to_le32(attrs);
110 		}
111 
112 	} else {
113 		struct smb2_file_all_info *fi = &data->fi;
114 
115 		attrs = le32_to_cpu(fi->Attributes);
116 		if (data->reparse_point) {
117 			attrs |= ATTR_REPARSE;
118 			fi->Attributes = cpu_to_le32(attrs);
119 		}
120 	}
121 
122 	ret = attrs & ATTR_REPARSE;
123 
124 	return ret;
125 }
126 
127 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
128 				 struct cifs_fattr *fattr,
129 				 struct cifs_open_info_data *data);
130 int smb2_create_reparse_symlink(const unsigned int xid, struct inode *inode,
131 				struct dentry *dentry, struct cifs_tcon *tcon,
132 				const char *full_path, const char *symname);
133 int smb2_mknod_reparse(unsigned int xid, struct inode *inode,
134 		       struct dentry *dentry, struct cifs_tcon *tcon,
135 		       const char *full_path, umode_t mode, dev_t dev);
136 int smb2_parse_reparse_point(struct cifs_sb_info *cifs_sb,
137 			     const char *full_path,
138 			     struct kvec *rsp_iov,
139 			     struct cifs_open_info_data *data);
140 
141 #endif /* _CIFS_REPARSE_H */
142