xref: /linux/fs/smb/client/reparse.c (revision 26ba30221c03364d6ed9910be8da4c1fd871b07b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2024 Paulo Alcantara <pc@manguebit.com>
4  */
5 
6 #include <linux/fs.h>
7 #include <linux/stat.h>
8 #include <linux/slab.h>
9 #include "cifsglob.h"
10 #include "smb2proto.h"
11 #include "cifsproto.h"
12 #include "cifs_unicode.h"
13 #include "cifs_debug.h"
14 #include "fs_context.h"
15 #include "reparse.h"
16 
17 static int mknod_nfs(unsigned int xid, struct inode *inode,
18 		     struct dentry *dentry, struct cifs_tcon *tcon,
19 		     const char *full_path, umode_t mode, dev_t dev,
20 		     const char *symname);
21 
22 static int mknod_wsl(unsigned int xid, struct inode *inode,
23 		     struct dentry *dentry, struct cifs_tcon *tcon,
24 		     const char *full_path, umode_t mode, dev_t dev,
25 		     const char *symname);
26 
27 static int create_native_symlink(const unsigned int xid, struct inode *inode,
28 				 struct dentry *dentry, struct cifs_tcon *tcon,
29 				 const char *full_path, const char *symname);
30 
31 static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb,
32 					   const unsigned int xid,
33 					   const char *full_path,
34 					   const char *symname,
35 					   bool *directory);
36 
37 int create_reparse_symlink(const unsigned int xid, struct inode *inode,
38 				struct dentry *dentry, struct cifs_tcon *tcon,
39 				const char *full_path, const char *symname)
40 {
41 	switch (cifs_symlink_type(CIFS_SB(inode->i_sb))) {
42 	case CIFS_SYMLINK_TYPE_NATIVE:
43 		return create_native_symlink(xid, inode, dentry, tcon, full_path, symname);
44 	case CIFS_SYMLINK_TYPE_NFS:
45 		return mknod_nfs(xid, inode, dentry, tcon, full_path, S_IFLNK, 0, symname);
46 	case CIFS_SYMLINK_TYPE_WSL:
47 		return mknod_wsl(xid, inode, dentry, tcon, full_path, S_IFLNK, 0, symname);
48 	default:
49 		return -EOPNOTSUPP;
50 	}
51 }
52 
53 static int create_native_symlink(const unsigned int xid, struct inode *inode,
54 				 struct dentry *dentry, struct cifs_tcon *tcon,
55 				 const char *full_path, const char *symname)
56 {
57 	struct reparse_symlink_data_buffer *buf = NULL;
58 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode);
59 	const char *symroot = cifs_sb->ctx->symlinkroot;
60 	struct cifs_open_info_data data = {};
61 	char sep = CIFS_DIR_SEP(cifs_sb);
62 	char *symlink_target = NULL;
63 	u16 len, plen, poff, slen;
64 	unsigned int sbflags;
65 	__le16 *path = NULL;
66 	struct inode *new;
67 	char *sym = NULL;
68 	struct kvec iov;
69 	bool directory;
70 	int path_len;
71 	int rc = 0;
72 
73 	if (strlen(symname) > REPARSE_SYM_PATH_MAX)
74 		return -ENAMETOOLONG;
75 
76 	symlink_target = kstrdup(symname, GFP_KERNEL);
77 	if (!symlink_target) {
78 		rc = -ENOMEM;
79 		goto out;
80 	}
81 
82 	data = (struct cifs_open_info_data) {
83 		.reparse_point = true,
84 		.reparse = { .tag = IO_REPARSE_TAG_SYMLINK, },
85 		.symlink_target = symlink_target,
86 	};
87 
88 	sbflags = cifs_sb_flags(cifs_sb);
89 	if (!(sbflags & CIFS_MOUNT_POSIX_PATHS) && symroot && symname[0] == '/') {
90 		/*
91 		 * This is a request to create an absolute symlink on the server
92 		 * which does not support POSIX paths, and expects symlink in
93 		 * NT-style path. So convert absolute Linux symlink target path
94 		 * to the absolute NT-style path. Root of the NT-style path for
95 		 * symlinks is specified in "symlinkroot" mount option. This will
96 		 * ensure compatibility of this symlink stored in absolute form
97 		 * on the SMB server.
98 		 */
99 		if (!strstarts(symname, symroot)) {
100 			/*
101 			 * If the absolute Linux symlink target path is not
102 			 * inside "symlinkroot" location then there is no way
103 			 * to convert such Linux symlink to NT-style path.
104 			 */
105 			cifs_dbg(VFS,
106 				 "absolute symlink '%s' cannot be converted to NT format "
107 				 "because it is outside of symlinkroot='%s'\n",
108 				 symname, symroot);
109 			rc = -EINVAL;
110 			goto out;
111 		}
112 		len = strlen(symroot);
113 		if (symroot[len - 1] != '/')
114 			len++;
115 		if (symname[len] >= 'a' && symname[len] <= 'z' &&
116 		    (symname[len+1] == '/' || symname[len+1] == '\0')) {
117 			/*
118 			 * Symlink points to Linux target /symlinkroot/x/path/...
119 			 * where 'x' is the lowercase local Windows drive.
120 			 * NT-style path for 'x' has common form \??\X:\path\...
121 			 * with uppercase local Windows drive.
122 			 */
123 			int common_path_len = strlen(symname+len+1)+1;
124 			sym = kzalloc(6+common_path_len, GFP_KERNEL);
125 			if (!sym) {
126 				rc = -ENOMEM;
127 				goto out;
128 			}
129 			memcpy(sym, "\\??\\", 4);
130 			sym[4] = symname[len] - ('a'-'A');
131 			sym[5] = ':';
132 			memcpy(sym+6, symname+len+1, common_path_len);
133 		} else {
134 			/* Unhandled absolute symlink. Report an error. */
135 			cifs_dbg(
136 				 VFS,
137 				 "absolute symlink '%s' cannot be converted to NT format "
138 				 "because it points to unknown target\n",
139 				 symname);
140 			rc = -EINVAL;
141 			goto out;
142 		}
143 	} else {
144 		/*
145 		 * This is request to either create an absolute symlink on
146 		 * server which expects POSIX paths or it is an request to
147 		 * create a relative symlink from the current directory.
148 		 * These paths have same format as relative SMB symlinks,
149 		 * so no conversion is needed. So just take symname as-is.
150 		 */
151 		sym = kstrdup(symname, GFP_KERNEL);
152 		if (!sym) {
153 			rc = -ENOMEM;
154 			goto out;
155 		}
156 	}
157 
158 	if (sep == '\\')
159 		convert_delimiter(sym, sep);
160 
161 	/*
162 	 * For absolute NT symlinks it is required to pass also leading
163 	 * backslash and to not mangle NT object prefix "\\??\\" and not to
164 	 * mangle colon in drive letter. But cifs_convert_path_to_utf16()
165 	 * removes leading backslash and replaces '?' and ':'. So temporary
166 	 * mask these characters in NT object prefix by '_' and then change
167 	 * them back.
168 	 */
169 	if (!(sbflags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/')
170 		sym[0] = sym[1] = sym[2] = sym[5] = '_';
171 
172 	/*
173 	 * On a POSIX paths mount the symlink target is stored verbatim, so
174 	 * convert it with cifs_strndup_to_utf16().  cifs_convert_path_to_utf16()
175 	 * must not be used here: it strips a leading path separator (it is
176 	 * meant for share-relative SMB paths), which would corrupt an absolute
177 	 * POSIX symlink target such as "/foo/bar".  Using NO_MAP_UNI_RSVD also
178 	 * matches the readback path in smb2_parse_native_symlink().
179 	 */
180 	if (sbflags & CIFS_MOUNT_POSIX_PATHS)
181 		path = cifs_strndup_to_utf16(sym, strlen(sym), &path_len,
182 					     cifs_sb->local_nls,
183 					     NO_MAP_UNI_RSVD);
184 	else
185 		path = cifs_convert_path_to_utf16(sym, cifs_sb);
186 
187 	if (!path) {
188 		rc = -ENOMEM;
189 		goto out;
190 	}
191 
192 	if (!(sbflags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
193 		sym[0] = '\\';
194 		sym[1] = sym[2] = '?';
195 		sym[5] = ':';
196 		path[0] = cpu_to_le16('\\');
197 		path[1] = path[2] = cpu_to_le16('?');
198 		path[5] = cpu_to_le16(':');
199 	}
200 
201 	/*
202 	 * SMB distinguish between symlink to directory and symlink to file.
203 	 * They cannot be exchanged (symlink of file type which points to
204 	 * directory cannot be resolved and vice-versa). Try to detect if
205 	 * the symlink target could be a directory or not. When detection
206 	 * fails then treat symlink as a file (non-directory) symlink.
207 	 */
208 	directory = false;
209 	rc = detect_directory_symlink_target(cifs_sb, xid, full_path, symname, &directory);
210 	if (rc < 0)
211 		goto out;
212 
213 	slen = 2 * UniStrnlen((wchar_t *)path, REPARSE_SYM_PATH_MAX);
214 	poff = 0;
215 	plen = slen;
216 	if (!(sbflags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
217 		/*
218 		 * For absolute NT symlinks skip leading "\\??\\" in PrintName as
219 		 * PrintName is user visible location in DOS/Win32 format (not in NT format).
220 		 */
221 		poff = 4;
222 		plen -= 2 * poff;
223 	}
224 	len = sizeof(*buf) + plen + slen;
225 	buf = kzalloc(len, GFP_KERNEL);
226 	if (!buf) {
227 		rc = -ENOMEM;
228 		goto out;
229 	}
230 
231 	buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_SYMLINK);
232 	buf->ReparseDataLength = cpu_to_le16(len - sizeof(struct reparse_data_buffer));
233 
234 	buf->SubstituteNameOffset = cpu_to_le16(plen);
235 	buf->SubstituteNameLength = cpu_to_le16(slen);
236 	memcpy(&buf->PathBuffer[plen], path, slen);
237 
238 	buf->PrintNameOffset = 0;
239 	buf->PrintNameLength = cpu_to_le16(plen);
240 	memcpy(buf->PathBuffer, path+poff, plen);
241 
242 	buf->Flags = cpu_to_le32(*symname != '/' ? SYMLINK_FLAG_RELATIVE : 0);
243 
244 	iov.iov_base = buf;
245 	iov.iov_len = len;
246 	new = tcon->ses->server->ops->create_reparse_inode(
247 				     &data, inode->i_sb, xid,
248 				     tcon, full_path, directory,
249 				     &iov, NULL);
250 	if (!IS_ERR(new))
251 		d_instantiate(dentry, new);
252 	else
253 		rc = PTR_ERR(new);
254 out:
255 	kfree(sym);
256 	kfree(path);
257 	cifs_free_open_info(&data);
258 	kfree(buf);
259 	return rc;
260 }
261 
262 static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb,
263 					   const unsigned int xid,
264 					   const char *full_path,
265 					   const char *symname,
266 					   bool *directory)
267 {
268 	char sep = CIFS_DIR_SEP(cifs_sb);
269 	struct cifs_open_parms oparms;
270 	struct tcon_link *tlink;
271 	struct cifs_tcon *tcon;
272 	const char *basename;
273 	struct cifs_fid fid;
274 	char *resolved_path;
275 	int full_path_len;
276 	int basename_len;
277 	int symname_len;
278 	char *path_sep;
279 	__u32 oplock;
280 	int open_rc;
281 
282 	/*
283 	 * First do some simple check. If the original Linux symlink target ends
284 	 * with slash, or last path component is dot or dot-dot then it is for
285 	 * sure symlink to the directory.
286 	 */
287 	basename = kbasename(symname);
288 	basename_len = strlen(basename);
289 	if (basename_len == 0 || /* symname ends with slash */
290 	    (basename_len == 1 && basename[0] == '.') || /* last component is "." */
291 	    (basename_len == 2 && basename[0] == '.' && basename[1] == '.')) { /* or ".." */
292 		*directory = true;
293 		return 0;
294 	}
295 
296 	/*
297 	 * For absolute symlinks it is not possible to determine
298 	 * if it should point to directory or file.
299 	 */
300 	if (symname[0] == '/') {
301 		cifs_dbg(FYI,
302 			 "%s: cannot determinate if the symlink target path '%s' "
303 			 "is directory or not, creating '%s' as file symlink\n",
304 			 __func__, symname, full_path);
305 		return 0;
306 	}
307 
308 	/*
309 	 * If it was not detected as directory yet and the symlink is relative
310 	 * then try to resolve the path on the SMB server, check if the path
311 	 * exists and determinate if it is a directory or not.
312 	 */
313 
314 	full_path_len = strlen(full_path);
315 	symname_len = strlen(symname);
316 
317 	tlink = cifs_sb_tlink(cifs_sb);
318 	if (IS_ERR(tlink))
319 		return PTR_ERR(tlink);
320 
321 	resolved_path = kzalloc(full_path_len + symname_len + 1, GFP_KERNEL);
322 	if (!resolved_path) {
323 		cifs_put_tlink(tlink);
324 		return -ENOMEM;
325 	}
326 
327 	/*
328 	 * Compose the resolved SMB symlink path from the SMB full path
329 	 * and Linux target symlink path.
330 	 */
331 	memcpy(resolved_path, full_path, full_path_len+1);
332 	path_sep = strrchr(resolved_path, sep);
333 	if (path_sep)
334 		path_sep++;
335 	else
336 		path_sep = resolved_path;
337 	memcpy(path_sep, symname, symname_len+1);
338 	if (sep == '\\')
339 		convert_delimiter(path_sep, sep);
340 
341 	tcon = tlink_tcon(tlink);
342 	oparms = CIFS_OPARMS(cifs_sb, tcon, resolved_path,
343 			     FILE_READ_ATTRIBUTES, FILE_OPEN, 0, ACL_NO_MODE);
344 	oparms.fid = &fid;
345 
346 	/* Try to open as a directory (NOT_FILE) */
347 	oplock = 0;
348 	oparms.create_options = cifs_create_options(cifs_sb,
349 						    CREATE_NOT_FILE | OPEN_REPARSE_POINT);
350 	open_rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
351 	if (open_rc == 0) {
352 		/* Successful open means that the target path is definitely a directory. */
353 		*directory = true;
354 		tcon->ses->server->ops->close(xid, tcon, &fid);
355 	} else if (open_rc == -ENOTDIR) {
356 		/* -ENOTDIR means that the target path is definitely a file. */
357 		*directory = false;
358 	} else if (open_rc == -ENOENT) {
359 		/* -ENOENT means that the target path does not exist. */
360 		cifs_dbg(FYI,
361 			 "%s: symlink target path '%s' does not exist, "
362 			 "creating '%s' as file symlink\n",
363 			 __func__, symname, full_path);
364 	} else {
365 		/* Try to open as a file (NOT_DIR) */
366 		oplock = 0;
367 		oparms.create_options = cifs_create_options(cifs_sb,
368 							    CREATE_NOT_DIR | OPEN_REPARSE_POINT);
369 		open_rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
370 		if (open_rc == 0) {
371 			/* Successful open means that the target path is definitely a file. */
372 			*directory = false;
373 			tcon->ses->server->ops->close(xid, tcon, &fid);
374 		} else if (open_rc == -EISDIR) {
375 			/* -EISDIR means that the target path is definitely a directory. */
376 			*directory = true;
377 		} else {
378 			/*
379 			 * This code branch is called when we do not have a permission to
380 			 * open the resolved_path or some other client/process denied
381 			 * opening the resolved_path.
382 			 *
383 			 * TODO: Try to use ops->query_dir_first on the parent directory
384 			 * of resolved_path, search for basename of resolved_path and
385 			 * check if the ATTR_DIRECTORY is set in fi.Attributes. In some
386 			 * case this could work also when opening of the path is denied.
387 			 */
388 			cifs_dbg(FYI,
389 				 "%s: cannot determinate if the symlink target path '%s' "
390 				 "is directory or not, creating '%s' as file symlink\n",
391 				 __func__, symname, full_path);
392 		}
393 	}
394 
395 	kfree(resolved_path);
396 	cifs_put_tlink(tlink);
397 	return 0;
398 }
399 
400 static int create_native_socket(const unsigned int xid, struct inode *inode,
401 				struct dentry *dentry, struct cifs_tcon *tcon,
402 				const char *full_path)
403 {
404 	struct reparse_data_buffer buf = {
405 		.ReparseTag = cpu_to_le32(IO_REPARSE_TAG_AF_UNIX),
406 		.ReparseDataLength = cpu_to_le16(0),
407 	};
408 	struct cifs_open_info_data data = {
409 		.reparse_point = true,
410 		.reparse = { .tag = IO_REPARSE_TAG_AF_UNIX, .buf = &buf, },
411 	};
412 	struct kvec iov = {
413 		.iov_base = &buf,
414 		.iov_len = sizeof(buf),
415 	};
416 	struct inode *new;
417 	int rc = 0;
418 
419 	new = tcon->ses->server->ops->create_reparse_inode(
420 				     &data, inode->i_sb, xid,
421 				     tcon, full_path, false, &iov, NULL);
422 	if (!IS_ERR(new))
423 		d_instantiate(dentry, new);
424 	else
425 		rc = PTR_ERR(new);
426 	cifs_free_open_info(&data);
427 	return rc;
428 }
429 
430 static int nfs_set_reparse_buf(struct reparse_nfs_data_buffer *buf,
431 			       mode_t mode, dev_t dev,
432 			       __le16 *symname_utf16,
433 			       int symname_utf16_len,
434 			       struct kvec *iov)
435 {
436 	u64 type;
437 	u16 len, dlen;
438 
439 	len = sizeof(*buf);
440 
441 	switch ((type = reparse_mode_nfs_type(mode))) {
442 	case NFS_SPECFILE_BLK:
443 	case NFS_SPECFILE_CHR:
444 		dlen = 2 * sizeof(__le32);
445 		((__le32 *)buf->DataBuffer)[0] = cpu_to_le32(MAJOR(dev));
446 		((__le32 *)buf->DataBuffer)[1] = cpu_to_le32(MINOR(dev));
447 		break;
448 	case NFS_SPECFILE_LNK:
449 		dlen = symname_utf16_len;
450 		memcpy(buf->DataBuffer, symname_utf16, symname_utf16_len);
451 		break;
452 	case NFS_SPECFILE_FIFO:
453 	case NFS_SPECFILE_SOCK:
454 		dlen = 0;
455 		break;
456 	default:
457 		return -EOPNOTSUPP;
458 	}
459 
460 	buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_NFS);
461 	buf->Reserved = 0;
462 	buf->InodeType = cpu_to_le64(type);
463 	buf->ReparseDataLength = cpu_to_le16(len + dlen -
464 					     sizeof(struct reparse_data_buffer));
465 	iov->iov_base = buf;
466 	iov->iov_len = len + dlen;
467 	return 0;
468 }
469 
470 static int mknod_nfs(unsigned int xid, struct inode *inode,
471 		     struct dentry *dentry, struct cifs_tcon *tcon,
472 		     const char *full_path, umode_t mode, dev_t dev,
473 		     const char *symname)
474 {
475 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
476 	struct cifs_open_info_data data;
477 	struct reparse_nfs_data_buffer *p = NULL;
478 	__le16 *symname_utf16 = NULL;
479 	int symname_utf16_len = 0;
480 	struct inode *new;
481 	struct kvec iov;
482 	__u8 buf[sizeof(*p) + sizeof(__le64)];
483 	int rc;
484 
485 	if (S_ISLNK(mode)) {
486 		symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
487 						      &symname_utf16_len,
488 						      cifs_sb->local_nls,
489 						      NO_MAP_UNI_RSVD);
490 		if (!symname_utf16) {
491 			rc = -ENOMEM;
492 			goto out;
493 		}
494 		symname_utf16_len -= 2; /* symlink is without trailing wide-nul */
495 		p = kzalloc(sizeof(*p) + symname_utf16_len, GFP_KERNEL);
496 		if (!p) {
497 			rc = -ENOMEM;
498 			goto out;
499 		}
500 	} else {
501 		p = (struct reparse_nfs_data_buffer *)buf;
502 	}
503 	rc = nfs_set_reparse_buf(p, mode, dev, symname_utf16, symname_utf16_len, &iov);
504 	if (rc)
505 		goto out;
506 
507 	data = (struct cifs_open_info_data) {
508 		.reparse_point = true,
509 		.reparse = { .tag = IO_REPARSE_TAG_NFS, .buf = (struct reparse_data_buffer *)p, },
510 		.symlink_target = kstrdup(symname, GFP_KERNEL),
511 	};
512 
513 	new = tcon->ses->server->ops->create_reparse_inode(
514 				     &data, inode->i_sb, xid,
515 				     tcon, full_path, false, &iov, NULL);
516 	if (!IS_ERR(new))
517 		d_instantiate(dentry, new);
518 	else
519 		rc = PTR_ERR(new);
520 	cifs_free_open_info(&data);
521 out:
522 	if (S_ISLNK(mode)) {
523 		kfree(symname_utf16);
524 		kfree(p);
525 	}
526 	return rc;
527 }
528 
529 static int wsl_set_reparse_buf(struct reparse_data_buffer **buf,
530 			       mode_t mode, const char *symname,
531 			       struct cifs_sb_info *cifs_sb,
532 			       struct kvec *iov)
533 {
534 	struct reparse_wsl_symlink_data_buffer *symlink_buf;
535 	__le16 *symname_utf16;
536 	int symname_utf16_len;
537 	int symname_utf8_maxlen;
538 	int symname_utf8_len;
539 	size_t buf_len;
540 	u32 tag;
541 
542 	switch ((tag = reparse_mode_wsl_tag(mode))) {
543 	case IO_REPARSE_TAG_LX_BLK:
544 	case IO_REPARSE_TAG_LX_CHR:
545 	case IO_REPARSE_TAG_LX_FIFO:
546 	case IO_REPARSE_TAG_AF_UNIX:
547 		buf_len = sizeof(struct reparse_data_buffer);
548 		*buf = kzalloc(buf_len, GFP_KERNEL);
549 		if (!*buf)
550 			return -ENOMEM;
551 		break;
552 	case IO_REPARSE_TAG_LX_SYMLINK:
553 		symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
554 						      &symname_utf16_len,
555 						      cifs_sb->local_nls,
556 						      NO_MAP_UNI_RSVD);
557 		if (!symname_utf16)
558 			return -ENOMEM;
559 		symname_utf8_maxlen = symname_utf16_len/2*3;
560 		symlink_buf = kzalloc(sizeof(struct reparse_wsl_symlink_data_buffer) +
561 				      symname_utf8_maxlen, GFP_KERNEL);
562 		if (!symlink_buf) {
563 			kfree(symname_utf16);
564 			return -ENOMEM;
565 		}
566 		/* Version field must be set to 2 (MS-FSCC 2.1.2.7) */
567 		symlink_buf->Version = cpu_to_le32(2);
568 		/* Target for Version 2 is in UTF-8 but without trailing null-term byte */
569 		symname_utf8_len = utf16s_to_utf8s((wchar_t *)symname_utf16, symname_utf16_len/2,
570 						   UTF16_LITTLE_ENDIAN,
571 						   symlink_buf->Target,
572 						   symname_utf8_maxlen);
573 		*buf = (struct reparse_data_buffer *)symlink_buf;
574 		buf_len = sizeof(struct reparse_wsl_symlink_data_buffer) + symname_utf8_len;
575 		kfree(symname_utf16);
576 		break;
577 	default:
578 		return -EOPNOTSUPP;
579 	}
580 
581 	(*buf)->ReparseTag = cpu_to_le32(tag);
582 	(*buf)->Reserved = 0;
583 	(*buf)->ReparseDataLength = cpu_to_le16(buf_len - sizeof(struct reparse_data_buffer));
584 	iov->iov_base = *buf;
585 	iov->iov_len = buf_len;
586 	return 0;
587 }
588 
589 static struct smb2_create_ea_ctx *ea_create_context(u32 dlen, size_t *cc_len)
590 {
591 	struct smb2_create_ea_ctx *cc;
592 
593 	*cc_len = round_up(sizeof(*cc) + dlen, 8);
594 	cc = kzalloc(*cc_len, GFP_KERNEL);
595 	if (!cc)
596 		return ERR_PTR(-ENOMEM);
597 
598 	cc->ctx.NameOffset = cpu_to_le16(offsetof(struct smb2_create_ea_ctx,
599 						  name));
600 	cc->ctx.NameLength = cpu_to_le16(4);
601 	memcpy(cc->name, SMB2_CREATE_EA_BUFFER, strlen(SMB2_CREATE_EA_BUFFER));
602 	cc->ctx.DataOffset = cpu_to_le16(offsetof(struct smb2_create_ea_ctx, ea));
603 	cc->ctx.DataLength = cpu_to_le32(dlen);
604 	return cc;
605 }
606 
607 struct wsl_xattr {
608 	const char	*name;
609 	__le64		value;
610 	u16		size;
611 	u32		next;
612 };
613 
614 static int wsl_set_xattrs(struct inode *inode, umode_t _mode,
615 			  dev_t _dev, struct kvec *iov)
616 {
617 	struct smb2_file_full_ea_info *ea;
618 	struct smb2_create_ea_ctx *cc;
619 	struct smb3_fs_context *ctx = CIFS_SB(inode->i_sb)->ctx;
620 	__le64 uid = cpu_to_le64(from_kuid(current_user_ns(), ctx->linux_uid));
621 	__le64 gid = cpu_to_le64(from_kgid(current_user_ns(), ctx->linux_gid));
622 	__le64 dev = cpu_to_le64(((u64)MINOR(_dev) << 32) | MAJOR(_dev));
623 	__le64 mode = cpu_to_le64(_mode);
624 	struct wsl_xattr xattrs[] = {
625 		{ .name = SMB2_WSL_XATTR_UID,  .value = uid,  .size = SMB2_WSL_XATTR_UID_SIZE, },
626 		{ .name = SMB2_WSL_XATTR_GID,  .value = gid,  .size = SMB2_WSL_XATTR_GID_SIZE, },
627 		{ .name = SMB2_WSL_XATTR_MODE, .value = mode, .size = SMB2_WSL_XATTR_MODE_SIZE, },
628 		{ .name = SMB2_WSL_XATTR_DEV,  .value = dev, .size = SMB2_WSL_XATTR_DEV_SIZE, },
629 	};
630 	size_t cc_len;
631 	u32 dlen = 0, next = 0;
632 	int i, num_xattrs;
633 	u8 name_size = SMB2_WSL_XATTR_NAME_LEN + 1;
634 
635 	memset(iov, 0, sizeof(*iov));
636 
637 	/* Exclude $LXDEV xattr for non-device files */
638 	if (!S_ISBLK(_mode) && !S_ISCHR(_mode))
639 		num_xattrs = ARRAY_SIZE(xattrs) - 1;
640 	else
641 		num_xattrs = ARRAY_SIZE(xattrs);
642 
643 	for (i = 0; i < num_xattrs; i++) {
644 		xattrs[i].next = ALIGN(sizeof(*ea) + name_size +
645 				       xattrs[i].size, 4);
646 		dlen += xattrs[i].next;
647 	}
648 
649 	cc = ea_create_context(dlen, &cc_len);
650 	if (IS_ERR(cc))
651 		return PTR_ERR(cc);
652 
653 	ea = &cc->ea;
654 	for (i = 0; i < num_xattrs; i++) {
655 		ea = (void *)((u8 *)ea + next);
656 		next = xattrs[i].next;
657 		ea->next_entry_offset = cpu_to_le32(next);
658 
659 		ea->ea_name_length = name_size - 1;
660 		ea->ea_value_length = cpu_to_le16(xattrs[i].size);
661 		memcpy(ea->ea_data, xattrs[i].name, name_size);
662 		memcpy(&ea->ea_data[name_size],
663 		       &xattrs[i].value, xattrs[i].size);
664 	}
665 	ea->next_entry_offset = 0;
666 
667 	iov->iov_base = cc;
668 	iov->iov_len = cc_len;
669 	return 0;
670 }
671 
672 static int mknod_wsl(unsigned int xid, struct inode *inode,
673 		     struct dentry *dentry, struct cifs_tcon *tcon,
674 		     const char *full_path, umode_t mode, dev_t dev,
675 		     const char *symname)
676 {
677 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
678 	struct cifs_open_info_data data;
679 	struct reparse_data_buffer *buf;
680 	struct smb2_create_ea_ctx *cc;
681 	struct inode *new;
682 	unsigned int len;
683 	struct kvec reparse_iov, xattr_iov;
684 	int rc;
685 
686 	rc = wsl_set_reparse_buf(&buf, mode, symname, cifs_sb, &reparse_iov);
687 	if (rc)
688 		return rc;
689 
690 	rc = wsl_set_xattrs(inode, mode, dev, &xattr_iov);
691 	if (rc) {
692 		kfree(buf);
693 		return rc;
694 	}
695 
696 	data = (struct cifs_open_info_data) {
697 		.reparse_point = true,
698 		.reparse = { .tag = le32_to_cpu(buf->ReparseTag), .buf = buf, },
699 		.symlink_target = kstrdup(symname, GFP_KERNEL),
700 	};
701 
702 	cc = xattr_iov.iov_base;
703 	len = le32_to_cpu(cc->ctx.DataLength);
704 	memcpy(data.wsl.eas, &cc->ea, len);
705 	data.wsl.eas_len = len;
706 
707 	new = tcon->ses->server->ops->create_reparse_inode(
708 				     &data, inode->i_sb,
709 				     xid, tcon, full_path, false,
710 				     &reparse_iov, &xattr_iov);
711 	if (!IS_ERR(new))
712 		d_instantiate(dentry, new);
713 	else
714 		rc = PTR_ERR(new);
715 	cifs_free_open_info(&data);
716 	kfree(xattr_iov.iov_base);
717 	kfree(buf);
718 	return rc;
719 }
720 
721 int mknod_reparse(unsigned int xid, struct inode *inode,
722 		       struct dentry *dentry, struct cifs_tcon *tcon,
723 		       const char *full_path, umode_t mode, dev_t dev)
724 {
725 	struct smb3_fs_context *ctx = CIFS_SB(inode->i_sb)->ctx;
726 
727 	if (S_ISSOCK(mode) && !ctx->nonativesocket && ctx->reparse_type != CIFS_REPARSE_TYPE_NONE)
728 		return create_native_socket(xid, inode, dentry, tcon, full_path);
729 
730 	switch (ctx->reparse_type) {
731 	case CIFS_REPARSE_TYPE_NFS:
732 		return mknod_nfs(xid, inode, dentry, tcon, full_path, mode, dev, NULL);
733 	case CIFS_REPARSE_TYPE_WSL:
734 		return mknod_wsl(xid, inode, dentry, tcon, full_path, mode, dev, NULL);
735 	default:
736 		return -EOPNOTSUPP;
737 	}
738 }
739 
740 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
741 static int parse_reparse_nfs(struct reparse_nfs_data_buffer *buf,
742 			       struct cifs_sb_info *cifs_sb,
743 			       struct cifs_open_info_data *data)
744 {
745 	unsigned int len;
746 	u64 type;
747 
748 	len = le16_to_cpu(buf->ReparseDataLength);
749 	if (len < sizeof(buf->InodeType)) {
750 		cifs_dbg(VFS, "srv returned malformed nfs buffer\n");
751 		return smb_EIO2(smb_eio_trace_reparse_nfs_too_short,
752 				len, sizeof(buf->InodeType));
753 	}
754 
755 	len -= sizeof(buf->InodeType);
756 
757 	switch ((type = le64_to_cpu(buf->InodeType))) {
758 	case NFS_SPECFILE_LNK:
759 		if (len == 0 || (len % 2)) {
760 			cifs_dbg(VFS, "srv returned malformed nfs symlink buffer\n");
761 			return smb_EIO1(smb_eio_trace_reparse_nfs_symbuf, len);
762 		}
763 		/*
764 		 * Check that buffer does not contain UTF-16 null codepoint
765 		 * because Linux cannot process symlink with null byte.
766 		 */
767 		if (UniStrnlen((wchar_t *)buf->DataBuffer, len/2) != len/2) {
768 			cifs_dbg(VFS, "srv returned null byte in nfs symlink target location\n");
769 			return smb_EIO1(smb_eio_trace_reparse_nfs_nul, len);
770 		}
771 		data->symlink_target = cifs_strndup_from_utf16(buf->DataBuffer,
772 							       len, true,
773 							       cifs_sb->local_nls);
774 		if (!data->symlink_target)
775 			return -ENOMEM;
776 		cifs_dbg(FYI, "%s: target path: %s\n",
777 			 __func__, data->symlink_target);
778 		break;
779 	case NFS_SPECFILE_CHR:
780 	case NFS_SPECFILE_BLK:
781 		/* DataBuffer for block and char devices contains two 32-bit numbers */
782 		if (len != 8) {
783 			cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
784 			return smb_EIO1(smb_eio_trace_reparse_nfs_dev, len);
785 		}
786 		break;
787 	case NFS_SPECFILE_FIFO:
788 	case NFS_SPECFILE_SOCK:
789 		/* DataBuffer for fifos and sockets is empty */
790 		if (len != 0) {
791 			cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
792 			return smb_EIO1(smb_eio_trace_reparse_nfs_sockfifo, len);
793 		}
794 		break;
795 	default:
796 		cifs_dbg(VFS, "%s: unhandled inode type: 0x%llx\n",
797 			 __func__, type);
798 		return -EOPNOTSUPP;
799 	}
800 	return 0;
801 }
802 
803 int smb2_parse_native_symlink(char **target, const char *buf, unsigned int len,
804 			      bool relative,
805 			      const char *full_path,
806 			      struct cifs_sb_info *cifs_sb)
807 {
808 	const char *symroot = cifs_sb->ctx->symlinkroot;
809 	char sep = CIFS_DIR_SEP(cifs_sb);
810 	char *linux_target = NULL;
811 	char *smb_target = NULL;
812 	int symlinkroot_len;
813 	int abs_path_len;
814 	char *abs_path;
815 	int levels;
816 	int rc, ulen;
817 	int i;
818 
819 	/* Check that length it valid */
820 	if (!len || (len % 2)) {
821 		cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
822 		rc = smb_EIO1(smb_eio_trace_reparse_native_nul, len);
823 		goto out;
824 	}
825 
826 	/*
827 	 * Check that buffer does not contain UTF-16 null codepoint
828 	 * because Linux cannot process symlink with null byte.
829 	 */
830 	ulen = UniStrnlen((wchar_t *)buf, len/2);
831 	if (ulen != len/2) {
832 		cifs_dbg(VFS, "srv returned null byte in native symlink target location\n");
833 		rc = smb_EIO2(smb_eio_trace_reparse_native_nul, ulen, len);
834 		goto out;
835 	}
836 
837 	smb_target = cifs_strndup_from_utf16(buf, len, true, cifs_sb->local_nls);
838 	if (!smb_target) {
839 		rc = -ENOMEM;
840 		goto out;
841 	}
842 
843 	if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_POSIX_PATHS) &&
844 	    symroot && !relative) {
845 		/*
846 		 * This is an absolute symlink from the server which does not
847 		 * support POSIX paths, so the symlink is in NT-style path.
848 		 * So convert it to absolute Linux symlink target path. Root of
849 		 * the NT-style path for symlinks is specified in "symlinkroot"
850 		 * mount option.
851 		 *
852 		 * Root of the DOS and Win32 paths is at NT path \??\
853 		 * It means that DOS/Win32 path C:\folder\file.txt is
854 		 * NT path \??\C:\folder\file.txt
855 		 *
856 		 * NT systems have some well-known object symlinks in their NT
857 		 * hierarchy, which is needed to take into account when resolving
858 		 * other symlinks. Most commonly used symlink paths are:
859 		 * \?? -> \GLOBAL??
860 		 * \DosDevices -> \??
861 		 * \GLOBAL??\GLOBALROOT -> \
862 		 * \GLOBAL??\Global -> \GLOBAL??
863 		 * \GLOBAL??\NUL -> \Device\Null
864 		 * \GLOBAL??\UNC -> \Device\Mup
865 		 * \GLOBAL??\PhysicalDrive0 -> \Device\Harddisk0\DR0 (for each harddisk)
866 		 * \GLOBAL??\A: -> \Device\Floppy0 (if A: is the first floppy)
867 		 * \GLOBAL??\C: -> \Device\HarddiskVolume1 (if C: is the first harddisk)
868 		 * \GLOBAL??\D: -> \Device\CdRom0 (if D: is first cdrom)
869 		 * \SystemRoot -> \Device\Harddisk0\Partition1\WINDOWS (or where is NT system installed)
870 		 * \Volume{...} -> \Device\HarddiskVolume1 (where ... is system generated guid)
871 		 *
872 		 * In most common cases, absolute NT symlinks points to path on
873 		 * DOS/Win32 drive letter, system-specific Volume or on UNC share.
874 		 * Here are few examples of commonly used absolute NT symlinks
875 		 * created by mklink.exe tool:
876 		 * \??\C:\folder\file.txt
877 		 * \??\\C:\folder\file.txt
878 		 * \??\UNC\server\share\file.txt
879 		 * \??\\UNC\server\share\file.txt
880 		 * \??\Volume{b75e2c83-0000-0000-0000-602f00000000}\folder\file.txt
881 		 *
882 		 * It means that the most common path prefix \??\ is also NT path
883 		 * symlink (to \GLOBAL??). It is less common that second path
884 		 * separator is double backslash, but it is valid.
885 		 *
886 		 * Volume guid is randomly generated by the target system and so
887 		 * only the target system knows the mapping between guid and the
888 		 * hardisk number. Over SMB it is not possible to resolve this
889 		 * mapping, therefore symlinks pointing to target location of
890 		 * volume guids are totally unusable over SMB.
891 		 *
892 		 * For now parse only symlink paths available for DOS and Win32.
893 		 * Those are paths with \??\ prefix or paths which points to \??\
894 		 * via other NT symlink (\DosDevices\, \GLOBAL??\, ...).
895 		 */
896 		abs_path = smb_target;
897 globalroot:
898 		if (strstarts(abs_path, "\\??\\"))
899 			abs_path += sizeof("\\??\\")-1;
900 		else if (strstarts(abs_path, "\\DosDevices\\"))
901 			abs_path += sizeof("\\DosDevices\\")-1;
902 		else if (strstarts(abs_path, "\\GLOBAL??\\"))
903 			abs_path += sizeof("\\GLOBAL??\\")-1;
904 		else
905 			goto out_unhandled_target;
906 
907 		/* Sometimes path separator after \?? is double backslash */
908 		if (abs_path[0] == '\\')
909 			abs_path++;
910 
911 		while (strstarts(abs_path, "Global\\"))
912 			abs_path += sizeof("Global\\")-1;
913 
914 		if (strstarts(abs_path, "GLOBALROOT\\")) {
915 			/* Label globalroot requires path with leading '\\', so do not trim '\\' */
916 			abs_path += sizeof("GLOBALROOT")-1;
917 			goto globalroot;
918 		}
919 
920 		/* For now parse only paths to drive letters */
921 		if (((abs_path[0] >= 'A' && abs_path[0] <= 'Z') ||
922 		     (abs_path[0] >= 'a' && abs_path[0] <= 'z')) &&
923 		    abs_path[1] == ':' &&
924 		    (abs_path[2] == '\\' || abs_path[2] == '\0')) {
925 			/* Convert drive letter to lowercase and drop colon */
926 			char drive_letter = abs_path[0];
927 			if (drive_letter >= 'A' && drive_letter <= 'Z')
928 				drive_letter += 'a'-'A';
929 			abs_path++;
930 			abs_path[0] = drive_letter;
931 		} else {
932 			goto out_unhandled_target;
933 		}
934 
935 		abs_path_len = strlen(abs_path)+1;
936 		symlinkroot_len = strlen(symroot);
937 		if (symroot[symlinkroot_len - 1] == '/')
938 			symlinkroot_len--;
939 		linux_target = kmalloc(symlinkroot_len + 1 + abs_path_len, GFP_KERNEL);
940 		if (!linux_target) {
941 			rc = -ENOMEM;
942 			goto out;
943 		}
944 		memcpy(linux_target, symroot, symlinkroot_len);
945 		linux_target[symlinkroot_len] = '/';
946 		memcpy(linux_target + symlinkroot_len + 1, abs_path, abs_path_len);
947 	} else if (smb_target[0] == sep && relative) {
948 		/*
949 		 * This is a relative SMB symlink from the top of the share,
950 		 * which is the top level directory of the Linux mount point.
951 		 * Linux does not support such relative symlinks, so convert
952 		 * it to the relative symlink from the current directory.
953 		 * full_path is the SMB path to the symlink (from which is
954 		 * extracted current directory) and smb_target is the SMB path
955 		 * where symlink points, therefore full_path must always be on
956 		 * the SMB share.
957 		 */
958 		int smb_target_len = strlen(smb_target)+1;
959 		levels = 0;
960 		for (i = 1; full_path[i]; i++) { /* i=1 to skip leading sep */
961 			if (full_path[i] == sep)
962 				levels++;
963 		}
964 		linux_target = kmalloc(levels*3 + smb_target_len, GFP_KERNEL);
965 		if (!linux_target) {
966 			rc = -ENOMEM;
967 			goto out;
968 		}
969 		for (i = 0; i < levels; i++) {
970 			linux_target[i*3 + 0] = '.';
971 			linux_target[i*3 + 1] = '.';
972 			linux_target[i*3 + 2] = sep;
973 		}
974 		memcpy(linux_target + levels*3, smb_target+1, smb_target_len); /* +1 to skip leading sep */
975 	} else {
976 		/*
977 		 * This is either an absolute symlink in POSIX-style format
978 		 * or relative SMB symlink from the current directory.
979 		 * These paths have same format as Linux symlinks, so no
980 		 * conversion is needed.
981 		 */
982 out_unhandled_target:
983 		linux_target = smb_target;
984 		smb_target = NULL;
985 	}
986 
987 	if (sep == '\\')
988 		convert_delimiter(linux_target, '/');
989 
990 	rc = 0;
991 	*target = linux_target;
992 
993 	cifs_dbg(FYI, "%s: symlink target: %s\n", __func__, *target);
994 
995 out:
996 	if (rc != 0)
997 		kfree(linux_target);
998 	kfree(smb_target);
999 	return rc;
1000 }
1001 
1002 static int parse_reparse_native_symlink(struct reparse_symlink_data_buffer *sym,
1003 				 u32 plen,
1004 				 struct cifs_sb_info *cifs_sb,
1005 				 const char *full_path,
1006 				 struct cifs_open_info_data *data)
1007 {
1008 	unsigned int len;
1009 	unsigned int offs;
1010 
1011 	/* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
1012 
1013 	offs = le16_to_cpu(sym->SubstituteNameOffset);
1014 	len = le16_to_cpu(sym->SubstituteNameLength);
1015 	if (offs + 20 > plen || offs + len + 20 > plen) {
1016 		cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
1017 		return smb_EIO2(smb_eio_trace_reparse_native_sym_len,
1018 				offs << 16 | len, plen);
1019 	}
1020 
1021 	return smb2_parse_native_symlink(&data->symlink_target,
1022 					 sym->PathBuffer + offs,
1023 					 len,
1024 					 le32_to_cpu(sym->Flags) & SYMLINK_FLAG_RELATIVE,
1025 					 full_path,
1026 					 cifs_sb);
1027 }
1028 
1029 static int parse_reparse_wsl_symlink(struct reparse_wsl_symlink_data_buffer *buf,
1030 				     struct cifs_sb_info *cifs_sb,
1031 				     struct cifs_open_info_data *data)
1032 {
1033 	int len = le16_to_cpu(buf->ReparseDataLength);
1034 	int data_offset = offsetof(typeof(*buf), Target) - offsetof(typeof(*buf), Version);
1035 	int symname_utf8_len;
1036 	__le16 *symname_utf16;
1037 	int symname_utf16_len;
1038 
1039 	if (len <= data_offset) {
1040 		cifs_dbg(VFS, "srv returned malformed wsl symlink buffer\n");
1041 		return smb_EIO2(smb_eio_trace_reparse_wsl_symbuf,
1042 				len, data_offset);
1043 	}
1044 
1045 	/* MS-FSCC 2.1.2.7 defines layout of the Target field only for Version 2. */
1046 	u32 version = le32_to_cpu(buf->Version);
1047 
1048 	if (version != 2) {
1049 		cifs_dbg(VFS, "srv returned unsupported wsl symlink version %u\n", version);
1050 		return smb_EIO1(smb_eio_trace_reparse_wsl_ver, version);
1051 	}
1052 
1053 	/* Target for Version 2 is in UTF-8 but without trailing null-term byte */
1054 	symname_utf8_len = len - data_offset;
1055 	/*
1056 	 * Check that buffer does not contain null byte
1057 	 * because Linux cannot process symlink with null byte.
1058 	 */
1059 	size_t ulen = strnlen(buf->Target, symname_utf8_len);
1060 
1061 	if (ulen != symname_utf8_len) {
1062 		cifs_dbg(VFS, "srv returned null byte in wsl symlink target location\n");
1063 		return smb_EIO2(smb_eio_trace_reparse_wsl_ver,
1064 				ulen, symname_utf8_len);
1065 	}
1066 	symname_utf16 = kzalloc(symname_utf8_len * 2, GFP_KERNEL);
1067 	if (!symname_utf16)
1068 		return -ENOMEM;
1069 	symname_utf16_len = utf8s_to_utf16s(buf->Target, symname_utf8_len,
1070 					    UTF16_LITTLE_ENDIAN,
1071 					    (wchar_t *) symname_utf16, symname_utf8_len * 2);
1072 	if (symname_utf16_len < 0) {
1073 		kfree(symname_utf16);
1074 		return symname_utf16_len;
1075 	}
1076 	symname_utf16_len *= 2; /* utf8s_to_utf16s() returns number of u16 items, not byte length */
1077 
1078 	data->symlink_target = cifs_strndup_from_utf16((u8 *)symname_utf16,
1079 						       symname_utf16_len, true,
1080 						       cifs_sb->local_nls);
1081 	kfree(symname_utf16);
1082 	if (!data->symlink_target)
1083 		return -ENOMEM;
1084 
1085 	return 0;
1086 }
1087 
1088 int parse_reparse_point(struct reparse_data_buffer *buf,
1089 			u32 plen, struct cifs_sb_info *cifs_sb,
1090 			const char *full_path,
1091 			struct cifs_open_info_data *data)
1092 {
1093 	data->reparse.buf = buf;
1094 
1095 	/* See MS-FSCC 2.1.2 */
1096 	switch (le32_to_cpu(buf->ReparseTag)) {
1097 	case IO_REPARSE_TAG_NFS:
1098 		return parse_reparse_nfs((struct reparse_nfs_data_buffer *)buf,
1099 					   cifs_sb, data);
1100 	case IO_REPARSE_TAG_SYMLINK:
1101 		return parse_reparse_native_symlink(
1102 			(struct reparse_symlink_data_buffer *)buf,
1103 			plen, cifs_sb, full_path, data);
1104 	case IO_REPARSE_TAG_LX_SYMLINK:
1105 		return parse_reparse_wsl_symlink(
1106 			(struct reparse_wsl_symlink_data_buffer *)buf,
1107 			cifs_sb, data);
1108 	case IO_REPARSE_TAG_AF_UNIX:
1109 	case IO_REPARSE_TAG_LX_FIFO:
1110 	case IO_REPARSE_TAG_LX_CHR:
1111 	case IO_REPARSE_TAG_LX_BLK: {
1112 		u16 dlen = le16_to_cpu(buf->ReparseDataLength);
1113 
1114 		if (dlen != 0) {
1115 			u32 rtag = le32_to_cpu(buf->ReparseTag);
1116 			cifs_dbg(VFS, "srv returned malformed buffer for reparse point: 0x%08x\n",
1117 				 rtag);
1118 			return smb_EIO2(smb_eio_trace_reparse_data_len, dlen, rtag);
1119 		}
1120 		return 0;
1121 	}
1122 	default:
1123 		return -EOPNOTSUPP;
1124 	}
1125 }
1126 
1127 struct reparse_data_buffer *smb2_get_reparse_point_buffer(const struct kvec *rsp_iov,
1128 							  u32 *plen)
1129 {
1130 	struct smb2_ioctl_rsp *io = rsp_iov->iov_base;
1131 	*plen = le32_to_cpu(io->OutputCount);
1132 	return (struct reparse_data_buffer *)((u8 *)io +
1133 					      le32_to_cpu(io->OutputOffset));
1134 }
1135 
1136 static bool wsl_to_fattr(struct cifs_open_info_data *data,
1137 			 struct cifs_sb_info *cifs_sb,
1138 			 u32 tag, struct cifs_fattr *fattr)
1139 {
1140 	struct smb2_file_full_ea_info *ea;
1141 	bool have_xattr_dev = false;
1142 	u32 next = 0;
1143 
1144 	switch (tag) {
1145 	case IO_REPARSE_TAG_LX_SYMLINK:
1146 		fattr->cf_mode |= S_IFLNK;
1147 		break;
1148 	case IO_REPARSE_TAG_LX_FIFO:
1149 		fattr->cf_mode |= S_IFIFO;
1150 		break;
1151 	case IO_REPARSE_TAG_AF_UNIX:
1152 		fattr->cf_mode |= S_IFSOCK;
1153 		break;
1154 	case IO_REPARSE_TAG_LX_CHR:
1155 		fattr->cf_mode |= S_IFCHR;
1156 		break;
1157 	case IO_REPARSE_TAG_LX_BLK:
1158 		fattr->cf_mode |= S_IFBLK;
1159 		break;
1160 	}
1161 
1162 	if (!data->wsl.eas_len)
1163 		goto out;
1164 
1165 	ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
1166 	do {
1167 		const char *name;
1168 		void *v;
1169 		u8 nlen;
1170 
1171 		ea = (void *)((u8 *)ea + next);
1172 		next = le32_to_cpu(ea->next_entry_offset);
1173 		if (!le16_to_cpu(ea->ea_value_length))
1174 			continue;
1175 
1176 		name = ea->ea_data;
1177 		nlen = ea->ea_name_length;
1178 		v = (void *)((u8 *)ea->ea_data + ea->ea_name_length + 1);
1179 
1180 		if (!strncmp(name, SMB2_WSL_XATTR_UID, nlen))
1181 			fattr->cf_uid = wsl_make_kuid(cifs_sb, v);
1182 		else if (!strncmp(name, SMB2_WSL_XATTR_GID, nlen))
1183 			fattr->cf_gid = wsl_make_kgid(cifs_sb, v);
1184 		else if (!strncmp(name, SMB2_WSL_XATTR_MODE, nlen)) {
1185 			/* File type in reparse point tag and in xattr mode must match. */
1186 			if (S_DT(fattr->cf_mode) != S_DT(le32_to_cpu(*(__le32 *)v)))
1187 				return false;
1188 			fattr->cf_mode = (umode_t)le32_to_cpu(*(__le32 *)v);
1189 		} else if (!strncmp(name, SMB2_WSL_XATTR_DEV, nlen)) {
1190 			fattr->cf_rdev = reparse_mkdev(v);
1191 			have_xattr_dev = true;
1192 		}
1193 	} while (next);
1194 out:
1195 
1196 	/* Major and minor numbers for char and block devices are mandatory. */
1197 	if (!have_xattr_dev && (tag == IO_REPARSE_TAG_LX_CHR || tag == IO_REPARSE_TAG_LX_BLK))
1198 		return false;
1199 
1200 	return true;
1201 }
1202 
1203 static bool posix_reparse_to_fattr(struct cifs_sb_info *cifs_sb,
1204 				   struct cifs_fattr *fattr,
1205 				   struct cifs_open_info_data *data)
1206 {
1207 	struct reparse_nfs_data_buffer *buf = (struct reparse_nfs_data_buffer *)data->reparse.buf;
1208 
1209 	if (buf == NULL)
1210 		return true;
1211 
1212 	if (le16_to_cpu(buf->ReparseDataLength) < sizeof(buf->InodeType)) {
1213 		WARN_ON_ONCE(1);
1214 		return false;
1215 	}
1216 
1217 	switch (le64_to_cpu(buf->InodeType)) {
1218 	case NFS_SPECFILE_CHR:
1219 		if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1220 			WARN_ON_ONCE(1);
1221 			return false;
1222 		}
1223 		fattr->cf_mode |= S_IFCHR;
1224 		fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1225 		break;
1226 	case NFS_SPECFILE_BLK:
1227 		if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1228 			WARN_ON_ONCE(1);
1229 			return false;
1230 		}
1231 		fattr->cf_mode |= S_IFBLK;
1232 		fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1233 		break;
1234 	case NFS_SPECFILE_FIFO:
1235 		fattr->cf_mode |= S_IFIFO;
1236 		break;
1237 	case NFS_SPECFILE_SOCK:
1238 		fattr->cf_mode |= S_IFSOCK;
1239 		break;
1240 	case NFS_SPECFILE_LNK:
1241 		fattr->cf_mode |= S_IFLNK;
1242 		break;
1243 	default:
1244 		WARN_ON_ONCE(1);
1245 		return false;
1246 	}
1247 	return true;
1248 }
1249 
1250 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
1251 				 struct cifs_fattr *fattr,
1252 				 struct cifs_open_info_data *data)
1253 {
1254 	u32 tag = data->reparse.tag;
1255 	bool ok;
1256 
1257 	switch (tag) {
1258 	case IO_REPARSE_TAG_LX_SYMLINK:
1259 	case IO_REPARSE_TAG_LX_FIFO:
1260 	case IO_REPARSE_TAG_AF_UNIX:
1261 	case IO_REPARSE_TAG_LX_CHR:
1262 	case IO_REPARSE_TAG_LX_BLK:
1263 		ok = wsl_to_fattr(data, cifs_sb, tag, fattr);
1264 		if (!ok)
1265 			return false;
1266 		break;
1267 	case IO_REPARSE_TAG_NFS:
1268 		ok = posix_reparse_to_fattr(cifs_sb, fattr, data);
1269 		if (!ok)
1270 			return false;
1271 		break;
1272 	case 0: /* SMB1 symlink */
1273 	case IO_REPARSE_TAG_SYMLINK:
1274 		fattr->cf_mode |= S_IFLNK;
1275 		break;
1276 	default:
1277 		if (!(fattr->cf_cifsattrs & ATTR_DIRECTORY))
1278 			return false;
1279 		if (!IS_REPARSE_TAG_NAME_SURROGATE(tag) &&
1280 		    tag != IO_REPARSE_TAG_INTERNAL)
1281 			return false;
1282 		/* See cifs_create_junction_fattr() */
1283 		fattr->cf_mode = S_IFDIR | 0711;
1284 		break;
1285 	}
1286 
1287 	fattr->cf_dtype = S_DT(fattr->cf_mode);
1288 	return true;
1289 }
1290