1 // SPDX-License-Identifier: LGPL-2.1
5 * Copyright (C) International Business Machines Corp., 2002,2008
6 * Author(s): Steve French (sfrench@us.ibm.com)
10 #include <linux/stat.h>
11 #include <linux/slab.h>
12 #include <linux/namei.h>
16 #include "cifsproto.h"
17 #include "cifs_debug.h"
18 #include "cifs_fs_sb.h"
19 #include "cifs_unicode.h"
20 #include "smb2proto.h"
21 #include "cifs_ioctl.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 crypto_shash *md5 = NULL;
43 struct sdesc *sdescmd5 = NULL;
45 rc = cifs_alloc_hash("md5", &md5, &sdescmd5);
47 goto symlink_hash_err;
49 rc = crypto_shash_init(&sdescmd5->shash);
51 cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
52 goto symlink_hash_err;
54 rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len);
56 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
57 goto symlink_hash_err;
59 rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
61 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
64 cifs_free_hash(&md5, &sdescmd5);
69 parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
73 unsigned int link_len;
79 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
82 md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
83 link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
85 rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
89 rc = symlink_hash(link_len, link_str, md5_hash);
91 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
95 scnprintf(md5_str2, sizeof(md5_str2),
96 CIFS_MF_SYMLINK_MD5_FORMAT,
97 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
99 if (strncmp(md5_str1, md5_str2, 17) != 0)
103 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
108 *_link_len = link_len;
113 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
116 unsigned int link_len;
120 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
123 link_len = strlen(link_str);
125 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
126 return -ENAMETOOLONG;
128 rc = symlink_hash(link_len, link_str, md5_hash);
130 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
134 scnprintf(buf, buf_len,
135 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
137 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
139 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
140 memcpy(buf + ofs, link_str, link_len);
143 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
148 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
157 couldbe_mf_symlink(const struct cifs_fattr *fattr)
159 if (!S_ISREG(fattr->cf_mode))
160 /* it's not a symlink */
163 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
164 /* it's not a symlink */
171 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
172 struct cifs_sb_info *cifs_sb, const char *fromName,
177 unsigned int bytes_written = 0;
179 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
183 rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
187 if (tcon->ses->server->ops->create_mf_symlink)
188 rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
189 cifs_sb, fromName, buf, &bytes_written);
196 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
204 query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
205 struct cifs_sb_info *cifs_sb, const unsigned char *path,
210 unsigned int link_len = 0;
211 unsigned int bytes_read = 0;
213 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
217 if (tcon->ses->server->ops->query_mf_symlink)
218 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
219 cifs_sb, path, buf, &bytes_read);
226 if (bytes_read == 0) { /* not a symlink */
231 rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
238 check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
239 struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
240 const unsigned char *path)
244 unsigned int link_len = 0;
245 unsigned int bytes_read = 0;
247 if (!couldbe_mf_symlink(fattr))
248 /* it's not a symlink */
251 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
255 if (tcon->ses->server->ops->query_mf_symlink)
256 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
257 cifs_sb, path, buf, &bytes_read);
264 if (bytes_read == 0) /* not a symlink */
267 rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
269 /* it's not a symlink */
277 /* it is a symlink */
278 fattr->cf_eof = link_len;
279 fattr->cf_mode &= ~S_IFMT;
280 fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
281 fattr->cf_dtype = DT_LNK;
288 * SMB 1.0 Protocol specific functions
292 cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
293 struct cifs_sb_info *cifs_sb, const unsigned char *path,
294 char *pbuf, unsigned int *pbytes_read)
299 struct cifs_open_parms oparms;
300 struct cifs_io_parms io_parms = {0};
301 int buf_type = CIFS_NO_BUFFER;
302 FILE_ALL_INFO file_info;
305 oparms.cifs_sb = cifs_sb;
306 oparms.desired_access = GENERIC_READ;
307 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
308 oparms.disposition = FILE_OPEN;
311 oparms.reconnect = false;
313 rc = CIFS_open(xid, &oparms, &oplock, &file_info);
317 if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
319 /* it's not a symlink */
323 io_parms.netfid = fid.netfid;
324 io_parms.pid = current->tgid;
325 io_parms.tcon = tcon;
327 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
329 rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
331 CIFSSMBClose(xid, tcon, fid.netfid);
336 cifs_create_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_written)
343 struct cifs_open_parms oparms;
344 struct cifs_io_parms io_parms = {0};
347 oparms.cifs_sb = cifs_sb;
348 oparms.desired_access = GENERIC_WRITE;
349 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
350 oparms.disposition = FILE_CREATE;
353 oparms.reconnect = false;
355 rc = CIFS_open(xid, &oparms, &oplock, NULL);
359 io_parms.netfid = fid.netfid;
360 io_parms.pid = current->tgid;
361 io_parms.tcon = tcon;
363 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
365 rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
366 CIFSSMBClose(xid, tcon, fid.netfid);
371 * SMB 2.1/SMB3 Protocol specific functions
374 smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
375 struct cifs_sb_info *cifs_sb, const unsigned char *path,
376 char *pbuf, unsigned int *pbytes_read)
380 struct cifs_open_parms oparms;
381 struct cifs_io_parms io_parms = {0};
382 int buf_type = CIFS_NO_BUFFER;
384 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
385 struct smb2_file_all_info *pfile_info = NULL;
388 oparms.cifs_sb = cifs_sb;
389 oparms.desired_access = GENERIC_READ;
390 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
391 oparms.disposition = FILE_OPEN;
393 oparms.reconnect = false;
395 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
396 if (utf16_path == NULL)
399 pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
402 if (pfile_info == NULL) {
407 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL,
410 goto qmf_out_open_fail;
412 if (pfile_info->EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
413 /* it's not a symlink */
414 rc = -ENOENT; /* Is there a better rc to return? */
418 io_parms.netfid = fid.netfid;
419 io_parms.pid = current->tgid;
420 io_parms.tcon = tcon;
422 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
423 io_parms.persistent_fid = fid.persistent_fid;
424 io_parms.volatile_fid = fid.volatile_fid;
425 rc = SMB2_read(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
427 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
435 smb3_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
436 struct cifs_sb_info *cifs_sb, const unsigned char *path,
437 char *pbuf, unsigned int *pbytes_written)
441 struct cifs_open_parms oparms;
442 struct cifs_io_parms io_parms = {0};
444 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
447 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
449 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
454 oparms.cifs_sb = cifs_sb;
455 oparms.desired_access = GENERIC_WRITE;
456 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
457 oparms.disposition = FILE_CREATE;
459 oparms.reconnect = false;
461 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
468 io_parms.netfid = fid.netfid;
469 io_parms.pid = current->tgid;
470 io_parms.tcon = tcon;
472 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
473 io_parms.persistent_fid = fid.persistent_fid;
474 io_parms.volatile_fid = fid.volatile_fid;
476 /* iov[0] is reserved for smb header */
477 iov[1].iov_base = pbuf;
478 iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
480 rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
482 /* Make sure we wrote all of the symlink data */
483 if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
486 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
493 * M-F Symlink Functions - End
497 cifs_hardlink(struct dentry *old_file, struct inode *inode,
498 struct dentry *direntry)
502 const char *from_name, *to_name;
504 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
505 struct tcon_link *tlink;
506 struct cifs_tcon *tcon;
507 struct TCP_Server_Info *server;
508 struct cifsInodeInfo *cifsInode;
510 if (unlikely(cifs_forced_shutdown(cifs_sb)))
513 tlink = cifs_sb_tlink(cifs_sb);
515 return PTR_ERR(tlink);
516 tcon = tlink_tcon(tlink);
519 page1 = alloc_dentry_path();
520 page2 = alloc_dentry_path();
522 from_name = build_path_from_dentry(old_file, page1);
523 if (IS_ERR(from_name)) {
524 rc = PTR_ERR(from_name);
527 to_name = build_path_from_dentry(direntry, page2);
528 if (IS_ERR(to_name)) {
529 rc = PTR_ERR(to_name);
534 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
536 cifs_remap(cifs_sb));
538 server = tcon->ses->server;
539 if (!server->ops->create_hardlink) {
543 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
545 if ((rc == -EIO) || (rc == -EINVAL))
549 d_drop(direntry); /* force new lookup from server of target */
552 * if source file is cached (oplocked) revalidate will not go to server
553 * until the file is closed or oplock broken so update nlinks locally
555 if (d_really_is_positive(old_file)) {
556 cifsInode = CIFS_I(d_inode(old_file));
558 spin_lock(&d_inode(old_file)->i_lock);
559 inc_nlink(d_inode(old_file));
560 spin_unlock(&d_inode(old_file)->i_lock);
563 * parent dir timestamps will update from srv within a
564 * second, would it really be worth it to set the parent
565 * dir cifs inode time to zero to force revalidate
566 * (faster) for it too?
570 * if not oplocked will force revalidate to get info on source
571 * file from srv. Note Samba server prior to 4.2 has bug -
572 * not updating src file ctime on hardlinks but Windows servers
578 * Will update parent dir timestamps from srv within a second.
579 * Would it really be worth it to set the parent dir (cifs
580 * inode) time field to zero to force revalidate on parent
581 * directory faster ie
583 * CIFS_I(inode)->time = 0;
588 free_dentry_path(page1);
589 free_dentry_path(page2);
591 cifs_put_tlink(tlink);
596 cifs_get_link(struct dentry *direntry, struct inode *inode,
597 struct delayed_call *done)
601 const char *full_path;
603 char *target_path = NULL;
604 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
605 struct tcon_link *tlink = NULL;
606 struct cifs_tcon *tcon;
607 struct TCP_Server_Info *server;
610 return ERR_PTR(-ECHILD);
614 tlink = cifs_sb_tlink(cifs_sb);
617 return ERR_CAST(tlink);
619 tcon = tlink_tcon(tlink);
620 server = tcon->ses->server;
622 page = alloc_dentry_path();
623 full_path = build_path_from_dentry(direntry, page);
624 if (IS_ERR(full_path)) {
626 cifs_put_tlink(tlink);
627 free_dentry_path(page);
628 return ERR_CAST(full_path);
631 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
635 * First try Minshall+French Symlinks, if configured
636 * and fallback to UNIX Extensions Symlinks.
638 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
639 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
642 if (rc != 0 && server->ops->query_symlink) {
643 struct cifsInodeInfo *cifsi = CIFS_I(inode);
644 bool reparse_point = false;
646 if (cifsi->cifsAttrs & ATTR_REPARSE)
647 reparse_point = true;
649 rc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
650 &target_path, reparse_point);
653 free_dentry_path(page);
655 cifs_put_tlink(tlink);
660 set_delayed_call(done, kfree_link, target_path);
665 cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode,
666 struct dentry *direntry, const char *symname)
668 int rc = -EOPNOTSUPP;
670 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
671 struct tcon_link *tlink;
672 struct cifs_tcon *pTcon;
673 const char *full_path;
675 struct inode *newinode = NULL;
677 if (unlikely(cifs_forced_shutdown(cifs_sb)))
680 page = alloc_dentry_path();
686 tlink = cifs_sb_tlink(cifs_sb);
691 pTcon = tlink_tcon(tlink);
693 full_path = build_path_from_dentry(direntry, page);
694 if (IS_ERR(full_path)) {
695 rc = PTR_ERR(full_path);
699 cifs_dbg(FYI, "Full path: %s\n", full_path);
700 cifs_dbg(FYI, "symname is %s\n", symname);
702 /* BB what if DFS and this volume is on different share? BB */
703 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
704 rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
705 else if (pTcon->unix_ext)
706 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
708 cifs_remap(cifs_sb));
710 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
711 cifs_sb_target->local_nls); */
714 if (pTcon->posix_extensions)
715 rc = smb311_posix_get_inode_info(&newinode, full_path, inode->i_sb, xid);
716 else if (pTcon->unix_ext)
717 rc = cifs_get_inode_info_unix(&newinode, full_path,
720 rc = cifs_get_inode_info(&newinode, full_path, NULL,
721 inode->i_sb, xid, NULL);
724 cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
727 d_instantiate(direntry, newinode);
731 free_dentry_path(page);
732 cifs_put_tlink(tlink);