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 shash_desc *md5 = NULL;
43 rc = cifs_alloc_hash("md5", &md5);
45 goto symlink_hash_err;
47 rc = crypto_shash_init(md5);
49 cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
50 goto symlink_hash_err;
52 rc = crypto_shash_update(md5, link_str, link_len);
54 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
55 goto symlink_hash_err;
57 rc = crypto_shash_final(md5, md5_hash);
59 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
67 parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
71 unsigned int link_len;
77 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
80 md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
81 link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
83 rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
87 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
90 rc = symlink_hash(link_len, link_str, md5_hash);
92 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
96 scnprintf(md5_str2, sizeof(md5_str2),
97 CIFS_MF_SYMLINK_MD5_FORMAT,
98 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
100 if (strncmp(md5_str1, md5_str2, 17) != 0)
104 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
109 *_link_len = link_len;
114 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
117 unsigned int link_len;
121 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
124 link_len = strlen(link_str);
126 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
127 return -ENAMETOOLONG;
129 rc = symlink_hash(link_len, link_str, md5_hash);
131 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
135 scnprintf(buf, buf_len,
136 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
138 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
140 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
141 memcpy(buf + ofs, link_str, link_len);
144 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
149 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
158 couldbe_mf_symlink(const struct cifs_fattr *fattr)
160 if (!S_ISREG(fattr->cf_mode))
161 /* it's not a symlink */
164 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
165 /* it's not a symlink */
172 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
173 struct cifs_sb_info *cifs_sb, const char *fromName,
178 unsigned int bytes_written = 0;
180 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
184 rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
188 if (tcon->ses->server->ops->create_mf_symlink)
189 rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
190 cifs_sb, fromName, buf, &bytes_written);
197 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
205 query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
206 struct cifs_sb_info *cifs_sb, const unsigned char *path,
211 unsigned int link_len = 0;
212 unsigned int bytes_read = 0;
214 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
218 if (tcon->ses->server->ops->query_mf_symlink)
219 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
220 cifs_sb, path, buf, &bytes_read);
227 if (bytes_read == 0) { /* not a symlink */
232 rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
239 check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
240 struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
241 const unsigned char *path)
245 unsigned int link_len = 0;
246 unsigned int bytes_read = 0;
248 if (!couldbe_mf_symlink(fattr))
249 /* it's not a symlink */
252 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
256 if (tcon->ses->server->ops->query_mf_symlink)
257 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
258 cifs_sb, path, buf, &bytes_read);
265 if (bytes_read == 0) /* not a symlink */
268 rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
270 /* it's not a symlink */
278 /* it is a symlink */
279 fattr->cf_eof = link_len;
280 fattr->cf_mode &= ~S_IFMT;
281 fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
282 fattr->cf_dtype = DT_LNK;
288 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
290 * SMB 1.0 Protocol specific functions
294 cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
295 struct cifs_sb_info *cifs_sb, const unsigned char *path,
296 char *pbuf, unsigned int *pbytes_read)
301 struct cifs_open_parms oparms;
302 struct cifs_io_parms io_parms = {0};
303 int buf_type = CIFS_NO_BUFFER;
304 FILE_ALL_INFO file_info;
307 oparms.cifs_sb = cifs_sb;
308 oparms.desired_access = GENERIC_READ;
309 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
310 oparms.disposition = FILE_OPEN;
313 oparms.reconnect = false;
315 rc = CIFS_open(xid, &oparms, &oplock, &file_info);
319 if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
321 /* it's not a symlink */
325 io_parms.netfid = fid.netfid;
326 io_parms.pid = current->tgid;
327 io_parms.tcon = tcon;
329 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
331 rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
333 CIFSSMBClose(xid, tcon, fid.netfid);
338 cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
339 struct cifs_sb_info *cifs_sb, const unsigned char *path,
340 char *pbuf, unsigned int *pbytes_written)
345 struct cifs_open_parms oparms;
346 struct cifs_io_parms io_parms = {0};
349 oparms.cifs_sb = cifs_sb;
350 oparms.desired_access = GENERIC_WRITE;
351 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
352 oparms.disposition = FILE_CREATE;
355 oparms.reconnect = false;
357 rc = CIFS_open(xid, &oparms, &oplock, NULL);
361 io_parms.netfid = fid.netfid;
362 io_parms.pid = current->tgid;
363 io_parms.tcon = tcon;
365 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
367 rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
368 CIFSSMBClose(xid, tcon, fid.netfid);
371 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
374 * SMB 2.1/SMB3 Protocol specific functions
377 smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
378 struct cifs_sb_info *cifs_sb, const unsigned char *path,
379 char *pbuf, unsigned int *pbytes_read)
383 struct cifs_open_parms oparms;
384 struct cifs_io_parms io_parms = {0};
385 int buf_type = CIFS_NO_BUFFER;
387 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
388 struct smb2_file_all_info *pfile_info = NULL;
391 oparms.cifs_sb = cifs_sb;
392 oparms.desired_access = GENERIC_READ;
393 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
394 oparms.disposition = FILE_OPEN;
396 oparms.reconnect = false;
398 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
399 if (utf16_path == NULL)
402 pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
405 if (pfile_info == NULL) {
410 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL,
413 goto qmf_out_open_fail;
415 if (pfile_info->EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
416 /* it's not a symlink */
417 rc = -ENOENT; /* Is there a better rc to return? */
421 io_parms.netfid = fid.netfid;
422 io_parms.pid = current->tgid;
423 io_parms.tcon = tcon;
425 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
426 io_parms.persistent_fid = fid.persistent_fid;
427 io_parms.volatile_fid = fid.volatile_fid;
428 rc = SMB2_read(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
430 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
438 smb3_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
439 struct cifs_sb_info *cifs_sb, const unsigned char *path,
440 char *pbuf, unsigned int *pbytes_written)
444 struct cifs_open_parms oparms;
445 struct cifs_io_parms io_parms = {0};
447 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
450 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
452 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
457 oparms.cifs_sb = cifs_sb;
458 oparms.desired_access = GENERIC_WRITE;
459 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
460 oparms.disposition = FILE_CREATE;
462 oparms.reconnect = false;
464 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
471 io_parms.netfid = fid.netfid;
472 io_parms.pid = current->tgid;
473 io_parms.tcon = tcon;
475 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
476 io_parms.persistent_fid = fid.persistent_fid;
477 io_parms.volatile_fid = fid.volatile_fid;
479 /* iov[0] is reserved for smb header */
480 iov[1].iov_base = pbuf;
481 iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
483 rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
485 /* Make sure we wrote all of the symlink data */
486 if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
489 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
496 * M-F Symlink Functions - End
500 cifs_hardlink(struct dentry *old_file, struct inode *inode,
501 struct dentry *direntry)
505 const char *from_name, *to_name;
507 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
508 struct tcon_link *tlink;
509 struct cifs_tcon *tcon;
510 struct TCP_Server_Info *server;
511 struct cifsInodeInfo *cifsInode;
513 if (unlikely(cifs_forced_shutdown(cifs_sb)))
516 tlink = cifs_sb_tlink(cifs_sb);
518 return PTR_ERR(tlink);
519 tcon = tlink_tcon(tlink);
522 page1 = alloc_dentry_path();
523 page2 = alloc_dentry_path();
525 from_name = build_path_from_dentry(old_file, page1);
526 if (IS_ERR(from_name)) {
527 rc = PTR_ERR(from_name);
530 to_name = build_path_from_dentry(direntry, page2);
531 if (IS_ERR(to_name)) {
532 rc = PTR_ERR(to_name);
536 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
538 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
540 cifs_remap(cifs_sb));
544 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
545 server = tcon->ses->server;
546 if (!server->ops->create_hardlink) {
550 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
552 if ((rc == -EIO) || (rc == -EINVAL))
556 d_drop(direntry); /* force new lookup from server of target */
559 * if source file is cached (oplocked) revalidate will not go to server
560 * until the file is closed or oplock broken so update nlinks locally
562 if (d_really_is_positive(old_file)) {
563 cifsInode = CIFS_I(d_inode(old_file));
565 spin_lock(&d_inode(old_file)->i_lock);
566 inc_nlink(d_inode(old_file));
567 spin_unlock(&d_inode(old_file)->i_lock);
570 * parent dir timestamps will update from srv within a
571 * second, would it really be worth it to set the parent
572 * dir cifs inode time to zero to force revalidate
573 * (faster) for it too?
577 * if not oplocked will force revalidate to get info on source
578 * file from srv. Note Samba server prior to 4.2 has bug -
579 * not updating src file ctime on hardlinks but Windows servers
585 * Will update parent dir timestamps from srv within a second.
586 * Would it really be worth it to set the parent dir (cifs
587 * inode) time field to zero to force revalidate on parent
588 * directory faster ie
590 * CIFS_I(inode)->time = 0;
595 free_dentry_path(page1);
596 free_dentry_path(page2);
598 cifs_put_tlink(tlink);
603 cifs_get_link(struct dentry *direntry, struct inode *inode,
604 struct delayed_call *done)
608 const char *full_path;
610 char *target_path = NULL;
611 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
612 struct tcon_link *tlink = NULL;
613 struct cifs_tcon *tcon;
614 struct TCP_Server_Info *server;
617 return ERR_PTR(-ECHILD);
621 tlink = cifs_sb_tlink(cifs_sb);
624 return ERR_CAST(tlink);
626 tcon = tlink_tcon(tlink);
627 server = tcon->ses->server;
629 page = alloc_dentry_path();
630 full_path = build_path_from_dentry(direntry, page);
631 if (IS_ERR(full_path)) {
633 cifs_put_tlink(tlink);
634 free_dentry_path(page);
635 return ERR_CAST(full_path);
638 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
642 * First try Minshall+French Symlinks, if configured
643 * and fallback to UNIX Extensions Symlinks.
645 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
646 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
649 if (rc != 0 && server->ops->query_symlink) {
650 struct cifsInodeInfo *cifsi = CIFS_I(inode);
651 bool reparse_point = false;
653 if (cifsi->cifsAttrs & ATTR_REPARSE)
654 reparse_point = true;
656 rc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
657 &target_path, reparse_point);
660 free_dentry_path(page);
662 cifs_put_tlink(tlink);
667 set_delayed_call(done, kfree_link, target_path);
672 cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode,
673 struct dentry *direntry, const char *symname)
675 int rc = -EOPNOTSUPP;
677 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
678 struct tcon_link *tlink;
679 struct cifs_tcon *pTcon;
680 const char *full_path;
682 struct inode *newinode = NULL;
684 if (unlikely(cifs_forced_shutdown(cifs_sb)))
687 page = alloc_dentry_path();
693 tlink = cifs_sb_tlink(cifs_sb);
698 pTcon = tlink_tcon(tlink);
700 full_path = build_path_from_dentry(direntry, page);
701 if (IS_ERR(full_path)) {
702 rc = PTR_ERR(full_path);
706 cifs_dbg(FYI, "Full path: %s\n", full_path);
707 cifs_dbg(FYI, "symname is %s\n", symname);
709 /* BB what if DFS and this volume is on different share? BB */
710 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
711 rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
712 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
713 else if (pTcon->unix_ext)
714 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
716 cifs_remap(cifs_sb));
717 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
719 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
720 cifs_sb_target->local_nls); */
723 if (pTcon->posix_extensions)
724 rc = smb311_posix_get_inode_info(&newinode, full_path, inode->i_sb, xid);
725 else if (pTcon->unix_ext)
726 rc = cifs_get_inode_info_unix(&newinode, full_path,
729 rc = cifs_get_inode_info(&newinode, full_path, NULL,
730 inode->i_sb, xid, NULL);
733 cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
736 d_instantiate(direntry, newinode);
740 free_dentry_path(page);
741 cifs_put_tlink(tlink);