1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
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"
23 * M-F Symlink Functions - Begin
26 #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
27 #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
28 #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
29 #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
30 #define CIFS_MF_SYMLINK_FILE_SIZE \
31 (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
33 #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
34 #define CIFS_MF_SYMLINK_MD5_FORMAT "%16phN\n"
35 #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) md5_hash
38 symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
41 struct crypto_shash *md5 = NULL;
42 struct sdesc *sdescmd5 = NULL;
44 rc = cifs_alloc_hash("md5", &md5, &sdescmd5);
46 goto symlink_hash_err;
48 rc = crypto_shash_init(&sdescmd5->shash);
50 cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
51 goto symlink_hash_err;
53 rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len);
55 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
56 goto symlink_hash_err;
58 rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
60 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
63 cifs_free_hash(&md5, &sdescmd5);
68 parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
72 unsigned int link_len;
78 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
81 md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
82 link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
84 rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
88 rc = symlink_hash(link_len, link_str, md5_hash);
90 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
94 scnprintf(md5_str2, sizeof(md5_str2),
95 CIFS_MF_SYMLINK_MD5_FORMAT,
96 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
98 if (strncmp(md5_str1, md5_str2, 17) != 0)
102 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
107 *_link_len = link_len;
112 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
115 unsigned int link_len;
119 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
122 link_len = strlen(link_str);
124 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
125 return -ENAMETOOLONG;
127 rc = symlink_hash(link_len, link_str, md5_hash);
129 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
133 scnprintf(buf, buf_len,
134 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
136 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
138 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
139 memcpy(buf + ofs, link_str, link_len);
142 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
147 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
156 couldbe_mf_symlink(const struct cifs_fattr *fattr)
158 if (!S_ISREG(fattr->cf_mode))
159 /* it's not a symlink */
162 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
163 /* it's not a symlink */
170 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
171 struct cifs_sb_info *cifs_sb, const char *fromName,
176 unsigned int bytes_written = 0;
178 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
182 rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
186 if (tcon->ses->server->ops->create_mf_symlink)
187 rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
188 cifs_sb, fromName, buf, &bytes_written);
195 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
203 query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
204 struct cifs_sb_info *cifs_sb, const unsigned char *path,
209 unsigned int link_len = 0;
210 unsigned int bytes_read = 0;
212 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
216 if (tcon->ses->server->ops->query_mf_symlink)
217 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
218 cifs_sb, path, buf, &bytes_read);
225 if (bytes_read == 0) { /* not a symlink */
230 rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
237 check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
238 struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
239 const unsigned char *path)
243 unsigned int link_len = 0;
244 unsigned int bytes_read = 0;
246 if (!couldbe_mf_symlink(fattr))
247 /* it's not a symlink */
250 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
254 if (tcon->ses->server->ops->query_mf_symlink)
255 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
256 cifs_sb, path, buf, &bytes_read);
263 if (bytes_read == 0) /* not a symlink */
266 rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
268 /* it's not a symlink */
276 /* it is a symlink */
277 fattr->cf_eof = link_len;
278 fattr->cf_mode &= ~S_IFMT;
279 fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
280 fattr->cf_dtype = DT_LNK;
287 * SMB 1.0 Protocol specific functions
291 cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
292 struct cifs_sb_info *cifs_sb, const unsigned char *path,
293 char *pbuf, unsigned int *pbytes_read)
298 struct cifs_open_parms oparms;
299 struct cifs_io_parms io_parms = {0};
300 int buf_type = CIFS_NO_BUFFER;
301 FILE_ALL_INFO file_info;
304 oparms.cifs_sb = cifs_sb;
305 oparms.desired_access = GENERIC_READ;
306 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
307 oparms.disposition = FILE_OPEN;
310 oparms.reconnect = false;
312 rc = CIFS_open(xid, &oparms, &oplock, &file_info);
316 if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
318 /* it's not a symlink */
322 io_parms.netfid = fid.netfid;
323 io_parms.pid = current->tgid;
324 io_parms.tcon = tcon;
326 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
328 rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
330 CIFSSMBClose(xid, tcon, fid.netfid);
335 cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
336 struct cifs_sb_info *cifs_sb, const unsigned char *path,
337 char *pbuf, unsigned int *pbytes_written)
342 struct cifs_open_parms oparms;
343 struct cifs_io_parms io_parms = {0};
346 oparms.cifs_sb = cifs_sb;
347 oparms.desired_access = GENERIC_WRITE;
348 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
349 oparms.disposition = FILE_CREATE;
352 oparms.reconnect = false;
354 rc = CIFS_open(xid, &oparms, &oplock, NULL);
358 io_parms.netfid = fid.netfid;
359 io_parms.pid = current->tgid;
360 io_parms.tcon = tcon;
362 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
364 rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
365 CIFSSMBClose(xid, tcon, fid.netfid);
370 * SMB 2.1/SMB3 Protocol specific functions
373 smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
374 struct cifs_sb_info *cifs_sb, const unsigned char *path,
375 char *pbuf, unsigned int *pbytes_read)
379 struct cifs_open_parms oparms;
380 struct cifs_io_parms io_parms = {0};
381 int buf_type = CIFS_NO_BUFFER;
383 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
384 struct smb2_file_all_info *pfile_info = NULL;
387 oparms.cifs_sb = cifs_sb;
388 oparms.desired_access = GENERIC_READ;
389 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
390 oparms.disposition = FILE_OPEN;
392 oparms.reconnect = false;
394 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
395 if (utf16_path == NULL)
398 pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
401 if (pfile_info == NULL) {
406 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL,
409 goto qmf_out_open_fail;
411 if (pfile_info->EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
412 /* it's not a symlink */
413 rc = -ENOENT; /* Is there a better rc to return? */
417 io_parms.netfid = fid.netfid;
418 io_parms.pid = current->tgid;
419 io_parms.tcon = tcon;
421 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
422 io_parms.persistent_fid = fid.persistent_fid;
423 io_parms.volatile_fid = fid.volatile_fid;
424 rc = SMB2_read(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
426 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
434 smb3_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
435 struct cifs_sb_info *cifs_sb, const unsigned char *path,
436 char *pbuf, unsigned int *pbytes_written)
440 struct cifs_open_parms oparms;
441 struct cifs_io_parms io_parms = {0};
443 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
446 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
448 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
453 oparms.cifs_sb = cifs_sb;
454 oparms.desired_access = GENERIC_WRITE;
455 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
456 oparms.disposition = FILE_CREATE;
458 oparms.reconnect = false;
460 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
467 io_parms.netfid = fid.netfid;
468 io_parms.pid = current->tgid;
469 io_parms.tcon = tcon;
471 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
472 io_parms.persistent_fid = fid.persistent_fid;
473 io_parms.volatile_fid = fid.volatile_fid;
475 /* iov[0] is reserved for smb header */
476 iov[1].iov_base = pbuf;
477 iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
479 rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
481 /* Make sure we wrote all of the symlink data */
482 if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
485 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
492 * M-F Symlink Functions - End
496 cifs_hardlink(struct dentry *old_file, struct inode *inode,
497 struct dentry *direntry)
501 const char *from_name, *to_name;
503 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
504 struct tcon_link *tlink;
505 struct cifs_tcon *tcon;
506 struct TCP_Server_Info *server;
507 struct cifsInodeInfo *cifsInode;
509 if (unlikely(cifs_forced_shutdown(cifs_sb)))
512 tlink = cifs_sb_tlink(cifs_sb);
514 return PTR_ERR(tlink);
515 tcon = tlink_tcon(tlink);
518 page1 = alloc_dentry_path();
519 page2 = alloc_dentry_path();
521 from_name = build_path_from_dentry(old_file, page1);
522 if (IS_ERR(from_name)) {
523 rc = PTR_ERR(from_name);
526 to_name = build_path_from_dentry(direntry, page2);
527 if (IS_ERR(to_name)) {
528 rc = PTR_ERR(to_name);
533 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
535 cifs_remap(cifs_sb));
537 server = tcon->ses->server;
538 if (!server->ops->create_hardlink) {
542 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
544 if ((rc == -EIO) || (rc == -EINVAL))
548 d_drop(direntry); /* force new lookup from server of target */
551 * if source file is cached (oplocked) revalidate will not go to server
552 * until the file is closed or oplock broken so update nlinks locally
554 if (d_really_is_positive(old_file)) {
555 cifsInode = CIFS_I(d_inode(old_file));
557 spin_lock(&d_inode(old_file)->i_lock);
558 inc_nlink(d_inode(old_file));
559 spin_unlock(&d_inode(old_file)->i_lock);
562 * parent dir timestamps will update from srv within a
563 * second, would it really be worth it to set the parent
564 * dir cifs inode time to zero to force revalidate
565 * (faster) for it too?
569 * if not oplocked will force revalidate to get info on source
570 * file from srv. Note Samba server prior to 4.2 has bug -
571 * not updating src file ctime on hardlinks but Windows servers
577 * Will update parent dir timestamps from srv within a second.
578 * Would it really be worth it to set the parent dir (cifs
579 * inode) time field to zero to force revalidate on parent
580 * directory faster ie
582 * CIFS_I(inode)->time = 0;
587 free_dentry_path(page1);
588 free_dentry_path(page2);
590 cifs_put_tlink(tlink);
595 cifs_get_link(struct dentry *direntry, struct inode *inode,
596 struct delayed_call *done)
600 const char *full_path;
602 char *target_path = NULL;
603 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
604 struct tcon_link *tlink = NULL;
605 struct cifs_tcon *tcon;
606 struct TCP_Server_Info *server;
609 return ERR_PTR(-ECHILD);
613 tlink = cifs_sb_tlink(cifs_sb);
616 return ERR_CAST(tlink);
618 tcon = tlink_tcon(tlink);
619 server = tcon->ses->server;
621 page = alloc_dentry_path();
622 full_path = build_path_from_dentry(direntry, page);
623 if (IS_ERR(full_path)) {
625 cifs_put_tlink(tlink);
626 free_dentry_path(page);
627 return ERR_CAST(full_path);
630 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
634 * First try Minshall+French Symlinks, if configured
635 * and fallback to UNIX Extensions Symlinks.
637 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
638 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
641 if (rc != 0 && server->ops->query_symlink) {
642 struct cifsInodeInfo *cifsi = CIFS_I(inode);
643 bool reparse_point = false;
645 if (cifsi->cifsAttrs & ATTR_REPARSE)
646 reparse_point = true;
648 rc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
649 &target_path, reparse_point);
652 free_dentry_path(page);
654 cifs_put_tlink(tlink);
659 set_delayed_call(done, kfree_link, target_path);
664 cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode,
665 struct dentry *direntry, const char *symname)
667 int rc = -EOPNOTSUPP;
669 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
670 struct tcon_link *tlink;
671 struct cifs_tcon *pTcon;
672 const char *full_path;
674 struct inode *newinode = NULL;
676 if (unlikely(cifs_forced_shutdown(cifs_sb)))
679 page = alloc_dentry_path();
685 tlink = cifs_sb_tlink(cifs_sb);
690 pTcon = tlink_tcon(tlink);
692 full_path = build_path_from_dentry(direntry, page);
693 if (IS_ERR(full_path)) {
694 rc = PTR_ERR(full_path);
698 cifs_dbg(FYI, "Full path: %s\n", full_path);
699 cifs_dbg(FYI, "symname is %s\n", symname);
701 /* BB what if DFS and this volume is on different share? BB */
702 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
703 rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
704 else if (pTcon->unix_ext)
705 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
707 cifs_remap(cifs_sb));
709 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
710 cifs_sb_target->local_nls); */
713 if (pTcon->posix_extensions)
714 rc = smb311_posix_get_inode_info(&newinode, full_path, inode->i_sb, xid);
715 else if (pTcon->unix_ext)
716 rc = cifs_get_inode_info_unix(&newinode, full_path,
719 rc = cifs_get_inode_info(&newinode, full_path, NULL,
720 inode->i_sb, xid, NULL);
723 cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
726 d_instantiate(direntry, newinode);
730 free_dentry_path(page);
731 cifs_put_tlink(tlink);