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