4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/stat.h>
23 #include <linux/slab.h>
24 #include <linux/namei.h>
28 #include "cifsproto.h"
29 #include "cifs_debug.h"
30 #include "cifs_fs_sb.h"
31 #ifdef CONFIG_CIFS_SMB2
32 #include "smb2proto.h"
36 * M-F Symlink Functions - Begin
39 #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
40 #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
41 #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
42 #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
43 #define CIFS_MF_SYMLINK_FILE_SIZE \
44 (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
46 #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
47 #define CIFS_MF_SYMLINK_MD5_FORMAT \
48 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n"
49 #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) \
50 md5_hash[0], md5_hash[1], md5_hash[2], md5_hash[3], \
51 md5_hash[4], md5_hash[5], md5_hash[6], md5_hash[7], \
52 md5_hash[8], md5_hash[9], md5_hash[10], md5_hash[11],\
53 md5_hash[12], md5_hash[13], md5_hash[14], md5_hash[15]
56 symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
60 struct crypto_shash *md5;
61 struct sdesc *sdescmd5;
63 md5 = crypto_alloc_shash("md5", 0, 0);
66 cifs_dbg(VFS, "%s: Crypto md5 allocation error %d\n",
70 size = sizeof(struct shash_desc) + crypto_shash_descsize(md5);
71 sdescmd5 = kmalloc(size, GFP_KERNEL);
74 goto symlink_hash_err;
76 sdescmd5->shash.tfm = md5;
77 sdescmd5->shash.flags = 0x0;
79 rc = crypto_shash_init(&sdescmd5->shash);
81 cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
82 goto symlink_hash_err;
84 rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len);
86 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
87 goto symlink_hash_err;
89 rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
91 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
94 crypto_free_shash(md5);
101 parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
105 unsigned int link_len;
106 const char *md5_str1;
107 const char *link_str;
111 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
114 md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
115 link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
117 rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
121 rc = symlink_hash(link_len, link_str, md5_hash);
123 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
127 snprintf(md5_str2, sizeof(md5_str2),
128 CIFS_MF_SYMLINK_MD5_FORMAT,
129 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
131 if (strncmp(md5_str1, md5_str2, 17) != 0)
135 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
140 *_link_len = link_len;
145 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
148 unsigned int link_len;
152 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
155 link_len = strlen(link_str);
157 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
158 return -ENAMETOOLONG;
160 rc = symlink_hash(link_len, link_str, md5_hash);
162 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
166 snprintf(buf, buf_len,
167 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
169 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
171 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
172 memcpy(buf + ofs, link_str, link_len);
175 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
180 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
189 couldbe_mf_symlink(const struct cifs_fattr *fattr)
191 if (!S_ISREG(fattr->cf_mode))
192 /* it's not a symlink */
195 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
196 /* it's not a symlink */
203 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
204 struct cifs_sb_info *cifs_sb, const char *fromName,
209 unsigned int bytes_written = 0;
211 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
215 rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
219 if (tcon->ses->server->ops->create_mf_symlink)
220 rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
221 cifs_sb, fromName, buf, &bytes_written);
228 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
236 query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
237 struct cifs_sb_info *cifs_sb, const unsigned char *path,
242 unsigned int link_len = 0;
243 unsigned int bytes_read = 0;
245 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
249 if (tcon->ses->server->ops->query_mf_symlink)
250 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
251 cifs_sb, path, buf, &bytes_read);
258 if (bytes_read == 0) { /* not a symlink */
263 rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
270 check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
271 struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
272 const unsigned char *path)
276 unsigned int link_len = 0;
277 unsigned int bytes_read = 0;
279 if (!couldbe_mf_symlink(fattr))
280 /* it's not a symlink */
283 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
287 if (tcon->ses->server->ops->query_mf_symlink)
288 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
289 cifs_sb, path, buf, &bytes_read);
296 if (bytes_read == 0) /* not a symlink */
299 rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
301 /* it's not a symlink */
309 /* it is a symlink */
310 fattr->cf_eof = link_len;
311 fattr->cf_mode &= ~S_IFMT;
312 fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
313 fattr->cf_dtype = DT_LNK;
320 * SMB 1.0 Protocol specific functions
324 cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
325 struct cifs_sb_info *cifs_sb, const unsigned char *path,
326 char *pbuf, unsigned int *pbytes_read)
331 struct cifs_open_parms oparms;
332 struct cifs_io_parms io_parms;
333 int buf_type = CIFS_NO_BUFFER;
334 FILE_ALL_INFO file_info;
337 oparms.cifs_sb = cifs_sb;
338 oparms.desired_access = GENERIC_READ;
339 oparms.create_options = CREATE_NOT_DIR;
340 oparms.disposition = FILE_OPEN;
343 oparms.reconnect = false;
345 rc = CIFS_open(xid, &oparms, &oplock, &file_info);
349 if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
351 /* it's not a symlink */
355 io_parms.netfid = fid.netfid;
356 io_parms.pid = current->tgid;
357 io_parms.tcon = tcon;
359 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
361 rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
363 CIFSSMBClose(xid, tcon, fid.netfid);
368 cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
369 struct cifs_sb_info *cifs_sb, const unsigned char *path,
370 char *pbuf, unsigned int *pbytes_written)
375 struct cifs_open_parms oparms;
376 struct cifs_io_parms io_parms;
377 int create_options = CREATE_NOT_DIR;
379 if (backup_cred(cifs_sb))
380 create_options |= CREATE_OPEN_BACKUP_INTENT;
383 oparms.cifs_sb = cifs_sb;
384 oparms.desired_access = GENERIC_WRITE;
385 oparms.create_options = create_options;
386 oparms.disposition = FILE_CREATE;
389 oparms.reconnect = false;
391 rc = CIFS_open(xid, &oparms, &oplock, NULL);
395 io_parms.netfid = fid.netfid;
396 io_parms.pid = current->tgid;
397 io_parms.tcon = tcon;
399 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
401 rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf, NULL, 0);
402 CIFSSMBClose(xid, tcon, fid.netfid);
407 * SMB 2.1/SMB3 Protocol specific functions
409 #ifdef CONFIG_CIFS_SMB2
411 smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
412 struct cifs_sb_info *cifs_sb, const unsigned char *path,
413 char *pbuf, unsigned int *pbytes_read)
417 struct cifs_open_parms oparms;
418 struct cifs_io_parms io_parms;
419 int buf_type = CIFS_NO_BUFFER;
421 __u8 oplock = SMB2_OPLOCK_LEVEL_II;
422 struct smb2_file_all_info *pfile_info = NULL;
425 oparms.cifs_sb = cifs_sb;
426 oparms.desired_access = GENERIC_READ;
427 oparms.create_options = CREATE_NOT_DIR;
428 if (backup_cred(cifs_sb))
429 oparms.create_options |= CREATE_OPEN_BACKUP_INTENT;
430 oparms.disposition = FILE_OPEN;
432 oparms.reconnect = false;
434 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
435 if (utf16_path == NULL)
438 pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
441 if (pfile_info == NULL) {
446 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL);
448 goto qmf_out_open_fail;
450 if (pfile_info->EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
451 /* it's not a symlink */
452 rc = -ENOENT; /* Is there a better rc to return? */
456 io_parms.netfid = fid.netfid;
457 io_parms.pid = current->tgid;
458 io_parms.tcon = tcon;
460 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
461 io_parms.persistent_fid = fid.persistent_fid;
462 io_parms.volatile_fid = fid.volatile_fid;
463 rc = SMB2_read(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
465 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
473 smb3_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
474 struct cifs_sb_info *cifs_sb, const unsigned char *path,
475 char *pbuf, unsigned int *pbytes_written)
479 struct cifs_open_parms oparms;
480 struct cifs_io_parms io_parms;
481 int create_options = CREATE_NOT_DIR;
483 __u8 oplock = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
486 if (backup_cred(cifs_sb))
487 create_options |= CREATE_OPEN_BACKUP_INTENT;
489 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
491 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
496 oparms.cifs_sb = cifs_sb;
497 oparms.desired_access = GENERIC_WRITE;
498 oparms.create_options = create_options;
499 oparms.disposition = FILE_CREATE;
501 oparms.reconnect = false;
503 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
509 io_parms.netfid = fid.netfid;
510 io_parms.pid = current->tgid;
511 io_parms.tcon = tcon;
513 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
514 io_parms.persistent_fid = fid.persistent_fid;
515 io_parms.volatile_fid = fid.volatile_fid;
517 /* iov[0] is reserved for smb header */
518 iov[1].iov_base = pbuf;
519 iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
521 rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
523 /* Make sure we wrote all of the symlink data */
524 if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
527 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
532 #endif /* CONFIG_CIFS_SMB2 */
535 * M-F Symlink Functions - End
539 cifs_hardlink(struct dentry *old_file, struct inode *inode,
540 struct dentry *direntry)
544 char *from_name = NULL;
545 char *to_name = NULL;
546 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
547 struct tcon_link *tlink;
548 struct cifs_tcon *tcon;
549 struct TCP_Server_Info *server;
550 struct cifsInodeInfo *cifsInode;
552 tlink = cifs_sb_tlink(cifs_sb);
554 return PTR_ERR(tlink);
555 tcon = tlink_tcon(tlink);
559 from_name = build_path_from_dentry(old_file);
560 to_name = build_path_from_dentry(direntry);
561 if ((from_name == NULL) || (to_name == NULL)) {
567 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
569 cifs_sb->mnt_cifs_flags &
570 CIFS_MOUNT_MAP_SPECIAL_CHR);
572 server = tcon->ses->server;
573 if (!server->ops->create_hardlink) {
577 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
579 if ((rc == -EIO) || (rc == -EINVAL))
583 d_drop(direntry); /* force new lookup from server of target */
586 * if source file is cached (oplocked) revalidate will not go to server
587 * until the file is closed or oplock broken so update nlinks locally
589 if (old_file->d_inode) {
590 cifsInode = CIFS_I(old_file->d_inode);
592 spin_lock(&old_file->d_inode->i_lock);
593 inc_nlink(old_file->d_inode);
594 spin_unlock(&old_file->d_inode->i_lock);
596 * BB should we make this contingent on superblock flag
599 /* old_file->d_inode->i_ctime = CURRENT_TIME; */
601 * parent dir timestamps will update from srv within a
602 * second, would it really be worth it to set the parent
603 * dir cifs inode time to zero to force revalidate
604 * (faster) for it too?
608 * if not oplocked will force revalidate to get info on source
614 * Will update parent dir timestamps from srv within a second.
615 * Would it really be worth it to set the parent dir (cifs
616 * inode) time field to zero to force revalidate on parent
617 * directory faster ie
619 * CIFS_I(inode)->time = 0;
627 cifs_put_tlink(tlink);
632 cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
634 struct inode *inode = direntry->d_inode;
637 char *full_path = NULL;
638 char *target_path = NULL;
639 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
640 struct tcon_link *tlink = NULL;
641 struct cifs_tcon *tcon;
642 struct TCP_Server_Info *server;
646 tlink = cifs_sb_tlink(cifs_sb);
652 tcon = tlink_tcon(tlink);
653 server = tcon->ses->server;
655 full_path = build_path_from_dentry(direntry);
659 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
663 * First try Minshall+French Symlinks, if configured
664 * and fallback to UNIX Extensions Symlinks.
666 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
667 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
670 if (rc != 0 && server->ops->query_symlink)
671 rc = server->ops->query_symlink(xid, tcon, full_path,
672 &target_path, cifs_sb);
678 target_path = ERR_PTR(rc);
683 cifs_put_tlink(tlink);
684 nd_set_link(nd, target_path);
689 cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
691 int rc = -EOPNOTSUPP;
693 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
694 struct tcon_link *tlink;
695 struct cifs_tcon *pTcon;
696 char *full_path = NULL;
697 struct inode *newinode = NULL;
701 tlink = cifs_sb_tlink(cifs_sb);
706 pTcon = tlink_tcon(tlink);
708 full_path = build_path_from_dentry(direntry);
709 if (full_path == NULL) {
714 cifs_dbg(FYI, "Full path: %s\n", full_path);
715 cifs_dbg(FYI, "symname is %s\n", symname);
717 /* BB what if DFS and this volume is on different share? BB */
718 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
719 rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
720 else if (pTcon->unix_ext)
721 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
724 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
725 cifs_sb_target->local_nls); */
729 rc = cifs_get_inode_info_unix(&newinode, full_path,
732 rc = cifs_get_inode_info(&newinode, full_path, NULL,
733 inode->i_sb, xid, NULL);
736 cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
739 d_instantiate(direntry, newinode);
744 cifs_put_tlink(tlink);