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"
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 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
91 rc = symlink_hash(link_len, link_str, md5_hash);
93 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
97 scnprintf(md5_str2, sizeof(md5_str2),
98 CIFS_MF_SYMLINK_MD5_FORMAT,
99 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
101 if (strncmp(md5_str1, md5_str2, 17) != 0)
105 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
110 *_link_len = link_len;
115 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
118 unsigned int link_len;
122 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
125 link_len = strlen(link_str);
127 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
128 return -ENAMETOOLONG;
130 rc = symlink_hash(link_len, link_str, md5_hash);
132 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
136 scnprintf(buf, buf_len,
137 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
139 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
141 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
142 memcpy(buf + ofs, link_str, link_len);
145 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
150 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
159 couldbe_mf_symlink(const struct cifs_fattr *fattr)
161 if (!S_ISREG(fattr->cf_mode))
162 /* it's not a symlink */
165 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
166 /* it's not a symlink */
173 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
174 struct cifs_sb_info *cifs_sb, const char *fromName,
179 unsigned int bytes_written = 0;
181 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
185 rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
189 if (tcon->ses->server->ops->create_mf_symlink)
190 rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
191 cifs_sb, fromName, buf, &bytes_written);
198 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
206 query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
207 struct cifs_sb_info *cifs_sb, const unsigned char *path,
212 unsigned int link_len = 0;
213 unsigned int bytes_read = 0;
215 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
219 if (tcon->ses->server->ops->query_mf_symlink)
220 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
221 cifs_sb, path, buf, &bytes_read);
228 if (bytes_read == 0) { /* not a symlink */
233 rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
240 check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
241 struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
242 const unsigned char *path)
246 unsigned int link_len = 0;
247 unsigned int bytes_read = 0;
249 if (!couldbe_mf_symlink(fattr))
250 /* it's not a symlink */
253 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
257 if (tcon->ses->server->ops->query_mf_symlink)
258 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
259 cifs_sb, path, buf, &bytes_read);
266 if (bytes_read == 0) /* not a symlink */
269 rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
271 /* it's not a symlink */
279 /* it is a symlink */
280 fattr->cf_eof = link_len;
281 fattr->cf_mode &= ~S_IFMT;
282 fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
283 fattr->cf_dtype = DT_LNK;
289 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
291 * SMB 1.0 Protocol specific functions
295 cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
296 struct cifs_sb_info *cifs_sb, const unsigned char *path,
297 char *pbuf, unsigned int *pbytes_read)
302 struct cifs_open_parms oparms;
303 struct cifs_io_parms io_parms = {0};
304 int buf_type = CIFS_NO_BUFFER;
305 FILE_ALL_INFO file_info;
308 oparms.cifs_sb = cifs_sb;
309 oparms.desired_access = GENERIC_READ;
310 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
311 oparms.disposition = FILE_OPEN;
314 oparms.reconnect = false;
316 rc = CIFS_open(xid, &oparms, &oplock, &file_info);
320 if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
322 /* it's not a symlink */
326 io_parms.netfid = fid.netfid;
327 io_parms.pid = current->tgid;
328 io_parms.tcon = tcon;
330 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
332 rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
334 CIFSSMBClose(xid, tcon, fid.netfid);
339 cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
340 struct cifs_sb_info *cifs_sb, const unsigned char *path,
341 char *pbuf, unsigned int *pbytes_written)
346 struct cifs_open_parms oparms;
347 struct cifs_io_parms io_parms = {0};
350 oparms.cifs_sb = cifs_sb;
351 oparms.desired_access = GENERIC_WRITE;
352 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
353 oparms.disposition = FILE_CREATE;
356 oparms.reconnect = false;
358 rc = CIFS_open(xid, &oparms, &oplock, NULL);
362 io_parms.netfid = fid.netfid;
363 io_parms.pid = current->tgid;
364 io_parms.tcon = tcon;
366 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
368 rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
369 CIFSSMBClose(xid, tcon, fid.netfid);
372 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
375 * SMB 2.1/SMB3 Protocol specific functions
378 smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
379 struct cifs_sb_info *cifs_sb, const unsigned char *path,
380 char *pbuf, unsigned int *pbytes_read)
384 struct cifs_open_parms oparms;
385 struct cifs_io_parms io_parms = {0};
386 int buf_type = CIFS_NO_BUFFER;
388 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
389 struct smb2_file_all_info *pfile_info = NULL;
392 oparms.cifs_sb = cifs_sb;
393 oparms.desired_access = GENERIC_READ;
394 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
395 oparms.disposition = FILE_OPEN;
397 oparms.reconnect = false;
399 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
400 if (utf16_path == NULL)
403 pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
406 if (pfile_info == NULL) {
411 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL,
414 goto qmf_out_open_fail;
416 if (pfile_info->EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
417 /* it's not a symlink */
418 rc = -ENOENT; /* Is there a better rc to return? */
422 io_parms.netfid = fid.netfid;
423 io_parms.pid = current->tgid;
424 io_parms.tcon = tcon;
426 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
427 io_parms.persistent_fid = fid.persistent_fid;
428 io_parms.volatile_fid = fid.volatile_fid;
429 rc = SMB2_read(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
431 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
439 smb3_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
440 struct cifs_sb_info *cifs_sb, const unsigned char *path,
441 char *pbuf, unsigned int *pbytes_written)
445 struct cifs_open_parms oparms;
446 struct cifs_io_parms io_parms = {0};
448 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
451 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
453 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
458 oparms.cifs_sb = cifs_sb;
459 oparms.desired_access = GENERIC_WRITE;
460 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
461 oparms.disposition = FILE_CREATE;
463 oparms.reconnect = false;
465 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
472 io_parms.netfid = fid.netfid;
473 io_parms.pid = current->tgid;
474 io_parms.tcon = tcon;
476 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
477 io_parms.persistent_fid = fid.persistent_fid;
478 io_parms.volatile_fid = fid.volatile_fid;
480 /* iov[0] is reserved for smb header */
481 iov[1].iov_base = pbuf;
482 iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
484 rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
486 /* Make sure we wrote all of the symlink data */
487 if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
490 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
497 * M-F Symlink Functions - End
501 cifs_hardlink(struct dentry *old_file, struct inode *inode,
502 struct dentry *direntry)
506 const char *from_name, *to_name;
508 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
509 struct tcon_link *tlink;
510 struct cifs_tcon *tcon;
511 struct TCP_Server_Info *server;
512 struct cifsInodeInfo *cifsInode;
514 if (unlikely(cifs_forced_shutdown(cifs_sb)))
517 tlink = cifs_sb_tlink(cifs_sb);
519 return PTR_ERR(tlink);
520 tcon = tlink_tcon(tlink);
523 page1 = alloc_dentry_path();
524 page2 = alloc_dentry_path();
526 from_name = build_path_from_dentry(old_file, page1);
527 if (IS_ERR(from_name)) {
528 rc = PTR_ERR(from_name);
531 to_name = build_path_from_dentry(direntry, page2);
532 if (IS_ERR(to_name)) {
533 rc = PTR_ERR(to_name);
537 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
539 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
541 cifs_remap(cifs_sb));
545 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
546 server = tcon->ses->server;
547 if (!server->ops->create_hardlink) {
551 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
553 if ((rc == -EIO) || (rc == -EINVAL))
557 d_drop(direntry); /* force new lookup from server of target */
560 * if source file is cached (oplocked) revalidate will not go to server
561 * until the file is closed or oplock broken so update nlinks locally
563 if (d_really_is_positive(old_file)) {
564 cifsInode = CIFS_I(d_inode(old_file));
566 spin_lock(&d_inode(old_file)->i_lock);
567 inc_nlink(d_inode(old_file));
568 spin_unlock(&d_inode(old_file)->i_lock);
571 * parent dir timestamps will update from srv within a
572 * second, would it really be worth it to set the parent
573 * dir cifs inode time to zero to force revalidate
574 * (faster) for it too?
578 * if not oplocked will force revalidate to get info on source
579 * file from srv. Note Samba server prior to 4.2 has bug -
580 * not updating src file ctime on hardlinks but Windows servers
586 * Will update parent dir timestamps from srv within a second.
587 * Would it really be worth it to set the parent dir (cifs
588 * inode) time field to zero to force revalidate on parent
589 * directory faster ie
591 * CIFS_I(inode)->time = 0;
596 free_dentry_path(page1);
597 free_dentry_path(page2);
599 cifs_put_tlink(tlink);
604 cifs_get_link(struct dentry *direntry, struct inode *inode,
605 struct delayed_call *done)
609 const char *full_path;
611 char *target_path = NULL;
612 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
613 struct tcon_link *tlink = NULL;
614 struct cifs_tcon *tcon;
615 struct TCP_Server_Info *server;
618 return ERR_PTR(-ECHILD);
622 tlink = cifs_sb_tlink(cifs_sb);
625 return ERR_CAST(tlink);
627 tcon = tlink_tcon(tlink);
628 server = tcon->ses->server;
630 page = alloc_dentry_path();
631 full_path = build_path_from_dentry(direntry, page);
632 if (IS_ERR(full_path)) {
634 cifs_put_tlink(tlink);
635 free_dentry_path(page);
636 return ERR_CAST(full_path);
639 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
643 * First try Minshall+French Symlinks, if configured
644 * and fallback to UNIX Extensions Symlinks.
646 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
647 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
650 if (rc != 0 && server->ops->query_symlink) {
651 struct cifsInodeInfo *cifsi = CIFS_I(inode);
652 bool reparse_point = false;
654 if (cifsi->cifsAttrs & ATTR_REPARSE)
655 reparse_point = true;
657 rc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
658 &target_path, reparse_point);
661 free_dentry_path(page);
663 cifs_put_tlink(tlink);
668 set_delayed_call(done, kfree_link, target_path);
673 cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode,
674 struct dentry *direntry, const char *symname)
676 int rc = -EOPNOTSUPP;
678 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
679 struct tcon_link *tlink;
680 struct cifs_tcon *pTcon;
681 const char *full_path;
683 struct inode *newinode = NULL;
685 if (unlikely(cifs_forced_shutdown(cifs_sb)))
688 page = alloc_dentry_path();
694 tlink = cifs_sb_tlink(cifs_sb);
699 pTcon = tlink_tcon(tlink);
701 full_path = build_path_from_dentry(direntry, page);
702 if (IS_ERR(full_path)) {
703 rc = PTR_ERR(full_path);
707 cifs_dbg(FYI, "Full path: %s\n", full_path);
708 cifs_dbg(FYI, "symname is %s\n", symname);
710 /* BB what if DFS and this volume is on different share? BB */
711 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
712 rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
713 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
714 else if (pTcon->unix_ext)
715 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
717 cifs_remap(cifs_sb));
718 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
720 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
721 cifs_sb_target->local_nls); */
724 if (pTcon->posix_extensions)
725 rc = smb311_posix_get_inode_info(&newinode, full_path, inode->i_sb, xid);
726 else if (pTcon->unix_ext)
727 rc = cifs_get_inode_info_unix(&newinode, full_path,
730 rc = cifs_get_inode_info(&newinode, full_path, NULL,
731 inode->i_sb, xid, NULL);
734 cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
737 d_instantiate(direntry, newinode);
741 free_dentry_path(page);
742 cifs_put_tlink(tlink);