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