1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2008
9 #include <linux/stat.h>
10 #include <linux/slab.h>
11 #include <linux/namei.h>
15 #include "cifsproto.h"
16 #include "cifs_debug.h"
17 #include "cifs_fs_sb.h"
18 #include "cifs_unicode.h"
19 #include "smb2proto.h"
20 #include "cifs_ioctl.h"
21 #include "fs_context.h"
24 * M-F Symlink Functions - Begin
27 #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
28 #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
29 #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
30 #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
31 #define CIFS_MF_SYMLINK_FILE_SIZE \
32 (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
34 #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
35 #define CIFS_MF_SYMLINK_MD5_FORMAT "%16phN\n"
36 #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) md5_hash
39 symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
42 struct shash_desc *md5 = NULL;
44 rc = cifs_alloc_hash("md5", &md5);
48 rc = crypto_shash_digest(md5, link_str, link_len, md5_hash);
50 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
56 parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
60 unsigned int link_len;
66 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
69 md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
70 link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
72 rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
76 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
79 rc = symlink_hash(link_len, link_str, md5_hash);
81 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
85 scnprintf(md5_str2, sizeof(md5_str2),
86 CIFS_MF_SYMLINK_MD5_FORMAT,
87 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
89 if (strncmp(md5_str1, md5_str2, 17) != 0)
93 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
98 *_link_len = link_len;
103 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
106 unsigned int link_len;
110 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
113 link_len = strlen(link_str);
115 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
116 return -ENAMETOOLONG;
118 rc = symlink_hash(link_len, link_str, md5_hash);
120 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
124 scnprintf(buf, buf_len,
125 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
127 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
129 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
130 memcpy(buf + ofs, link_str, link_len);
133 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
138 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
147 couldbe_mf_symlink(const struct cifs_fattr *fattr)
149 if (!S_ISREG(fattr->cf_mode))
150 /* it's not a symlink */
153 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
154 /* it's not a symlink */
161 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
162 struct cifs_sb_info *cifs_sb, const char *fromName,
167 unsigned int bytes_written = 0;
169 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
173 rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
177 if (tcon->ses->server->ops->create_mf_symlink)
178 rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
179 cifs_sb, fromName, buf, &bytes_written);
186 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
194 check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
195 struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
196 const unsigned char *path)
200 unsigned int link_len = 0;
201 unsigned int bytes_read = 0;
202 char *symlink = NULL;
204 if (!couldbe_mf_symlink(fattr))
205 /* it's not a symlink */
208 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
212 if (tcon->ses->server->ops->query_mf_symlink)
213 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
214 cifs_sb, path, buf, &bytes_read);
221 if (bytes_read == 0) /* not a symlink */
224 rc = parse_mf_symlink(buf, bytes_read, &link_len, &symlink);
226 /* it's not a symlink */
234 /* it is a symlink */
235 fattr->cf_eof = link_len;
236 fattr->cf_mode &= ~S_IFMT;
237 fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
238 fattr->cf_dtype = DT_LNK;
239 fattr->cf_symlink_target = symlink;
245 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
247 * SMB 1.0 Protocol specific functions
251 cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
252 struct cifs_sb_info *cifs_sb, const unsigned char *path,
253 char *pbuf, unsigned int *pbytes_read)
258 struct cifs_open_parms oparms;
259 struct cifs_io_parms io_parms = {0};
260 int buf_type = CIFS_NO_BUFFER;
261 FILE_ALL_INFO file_info;
263 oparms = (struct cifs_open_parms) {
266 .desired_access = GENERIC_READ,
267 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
268 .disposition = FILE_OPEN,
273 rc = CIFS_open(xid, &oparms, &oplock, &file_info);
277 if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
279 /* it's not a symlink */
283 io_parms.netfid = fid.netfid;
284 io_parms.pid = current->tgid;
285 io_parms.tcon = tcon;
287 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
289 rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
291 CIFSSMBClose(xid, tcon, fid.netfid);
296 cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
297 struct cifs_sb_info *cifs_sb, const unsigned char *path,
298 char *pbuf, unsigned int *pbytes_written)
303 struct cifs_open_parms oparms;
304 struct cifs_io_parms io_parms = {0};
306 oparms = (struct cifs_open_parms) {
309 .desired_access = GENERIC_WRITE,
310 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
311 .disposition = FILE_CREATE,
316 rc = CIFS_open(xid, &oparms, &oplock, NULL);
320 io_parms.netfid = fid.netfid;
321 io_parms.pid = current->tgid;
322 io_parms.tcon = tcon;
324 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
326 rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
327 CIFSSMBClose(xid, tcon, fid.netfid);
330 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
333 * SMB 2.1/SMB3 Protocol specific functions
336 smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
337 struct cifs_sb_info *cifs_sb, const unsigned char *path,
338 char *pbuf, unsigned int *pbytes_read)
342 struct cifs_open_parms oparms;
343 struct cifs_io_parms io_parms = {0};
344 int buf_type = CIFS_NO_BUFFER;
346 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
347 struct smb2_file_all_info *pfile_info = NULL;
349 oparms = (struct cifs_open_parms) {
353 .desired_access = GENERIC_READ,
354 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
355 .disposition = FILE_OPEN,
359 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
360 if (utf16_path == NULL)
363 pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
366 if (pfile_info == NULL) {
371 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL,
374 goto qmf_out_open_fail;
376 if (pfile_info->EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
377 /* it's not a symlink */
378 rc = -ENOENT; /* Is there a better rc to return? */
382 io_parms.netfid = fid.netfid;
383 io_parms.pid = current->tgid;
384 io_parms.tcon = tcon;
386 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
387 io_parms.persistent_fid = fid.persistent_fid;
388 io_parms.volatile_fid = fid.volatile_fid;
389 rc = SMB2_read(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
391 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
399 smb3_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
400 struct cifs_sb_info *cifs_sb, const unsigned char *path,
401 char *pbuf, unsigned int *pbytes_written)
405 struct cifs_open_parms oparms;
406 struct cifs_io_parms io_parms = {0};
408 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
411 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
413 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
417 oparms = (struct cifs_open_parms) {
421 .desired_access = GENERIC_WRITE,
422 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
423 .disposition = FILE_CREATE,
428 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
435 io_parms.netfid = fid.netfid;
436 io_parms.pid = current->tgid;
437 io_parms.tcon = tcon;
439 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
440 io_parms.persistent_fid = fid.persistent_fid;
441 io_parms.volatile_fid = fid.volatile_fid;
443 /* iov[0] is reserved for smb header */
444 iov[1].iov_base = pbuf;
445 iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
447 rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
449 /* Make sure we wrote all of the symlink data */
450 if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
453 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
460 * M-F Symlink Functions - End
464 cifs_hardlink(struct dentry *old_file, struct inode *inode,
465 struct dentry *direntry)
469 const char *from_name, *to_name;
471 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
472 struct tcon_link *tlink;
473 struct cifs_tcon *tcon;
474 struct TCP_Server_Info *server;
475 struct cifsInodeInfo *cifsInode;
477 if (unlikely(cifs_forced_shutdown(cifs_sb)))
480 tlink = cifs_sb_tlink(cifs_sb);
482 return PTR_ERR(tlink);
483 tcon = tlink_tcon(tlink);
486 page1 = alloc_dentry_path();
487 page2 = alloc_dentry_path();
489 from_name = build_path_from_dentry(old_file, page1);
490 if (IS_ERR(from_name)) {
491 rc = PTR_ERR(from_name);
494 to_name = build_path_from_dentry(direntry, page2);
495 if (IS_ERR(to_name)) {
496 rc = PTR_ERR(to_name);
500 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
502 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
504 cifs_remap(cifs_sb));
508 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
509 server = tcon->ses->server;
510 if (!server->ops->create_hardlink) {
514 rc = server->ops->create_hardlink(xid, tcon, old_file,
515 from_name, to_name, cifs_sb);
516 if ((rc == -EIO) || (rc == -EINVAL))
520 d_drop(direntry); /* force new lookup from server of target */
523 * if source file is cached (oplocked) revalidate will not go to server
524 * until the file is closed or oplock broken so update nlinks locally
526 if (d_really_is_positive(old_file)) {
527 cifsInode = CIFS_I(d_inode(old_file));
529 spin_lock(&d_inode(old_file)->i_lock);
530 inc_nlink(d_inode(old_file));
531 spin_unlock(&d_inode(old_file)->i_lock);
534 * parent dir timestamps will update from srv within a
535 * second, would it really be worth it to set the parent
536 * dir cifs inode time to zero to force revalidate
537 * (faster) for it too?
541 * if not oplocked will force revalidate to get info on source
542 * file from srv. Note Samba server prior to 4.2 has bug -
543 * not updating src file ctime on hardlinks but Windows servers
549 * Will update parent dir timestamps from srv within a second.
550 * Would it really be worth it to set the parent dir (cifs
551 * inode) time field to zero to force revalidate on parent
552 * directory faster ie
554 * CIFS_I(inode)->time = 0;
559 free_dentry_path(page1);
560 free_dentry_path(page2);
562 cifs_put_tlink(tlink);
567 cifs_symlink(struct mnt_idmap *idmap, struct inode *inode,
568 struct dentry *direntry, const char *symname)
570 int rc = -EOPNOTSUPP;
572 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
573 struct TCP_Server_Info *server;
574 struct tcon_link *tlink;
575 struct cifs_tcon *pTcon;
576 const char *full_path;
578 struct inode *newinode = NULL;
580 if (unlikely(cifs_forced_shutdown(cifs_sb)))
583 page = alloc_dentry_path();
589 tlink = cifs_sb_tlink(cifs_sb);
592 /* BB could be clearer if skipped put_tlink on error here, but harmless */
595 pTcon = tlink_tcon(tlink);
596 server = cifs_pick_channel(pTcon->ses);
598 full_path = build_path_from_dentry(direntry, page);
599 if (IS_ERR(full_path)) {
600 rc = PTR_ERR(full_path);
604 cifs_dbg(FYI, "Full path: %s\n", full_path);
605 cifs_dbg(FYI, "symname is %s\n", symname);
607 /* BB what if DFS and this volume is on different share? BB */
609 switch (get_cifs_symlink_type(cifs_sb)) {
610 case CIFS_SYMLINK_TYPE_DEFAULT:
611 /* should not happen, get_cifs_symlink_type() resolves the default */
614 case CIFS_SYMLINK_TYPE_NONE:
617 case CIFS_SYMLINK_TYPE_UNIX:
618 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
619 if (pTcon->unix_ext) {
620 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path,
623 cifs_remap(cifs_sb));
625 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
628 case CIFS_SYMLINK_TYPE_MFSYMLINKS:
629 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
630 rc = create_mf_symlink(xid, pTcon, cifs_sb,
635 case CIFS_SYMLINK_TYPE_SFU:
636 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
637 rc = __cifs_sfu_make_node(xid, inode, direntry, pTcon,
643 case CIFS_SYMLINK_TYPE_NATIVE:
644 case CIFS_SYMLINK_TYPE_NFS:
645 case CIFS_SYMLINK_TYPE_WSL:
646 if (server->ops->create_reparse_symlink) {
647 rc = server->ops->create_reparse_symlink(xid, inode,
658 if (pTcon->posix_extensions) {
659 rc = smb311_posix_get_inode_info(&newinode, full_path,
660 NULL, inode->i_sb, xid);
661 } else if (pTcon->unix_ext) {
662 rc = cifs_get_inode_info_unix(&newinode, full_path,
665 rc = cifs_get_inode_info(&newinode, full_path, NULL,
666 inode->i_sb, xid, NULL);
670 cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
673 d_instantiate(direntry, newinode);
677 free_dentry_path(page);
678 cifs_put_tlink(tlink);