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 #include "cifs_unicode.h"
32 #ifdef CONFIG_CIFS_SMB2
33 #include "smb2proto.h"
37 * M-F Symlink Functions - Begin
40 #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
41 #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
42 #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
43 #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
44 #define CIFS_MF_SYMLINK_FILE_SIZE \
45 (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
47 #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
48 #define CIFS_MF_SYMLINK_MD5_FORMAT \
49 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n"
50 #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) \
51 md5_hash[0], md5_hash[1], md5_hash[2], md5_hash[3], \
52 md5_hash[4], md5_hash[5], md5_hash[6], md5_hash[7], \
53 md5_hash[8], md5_hash[9], md5_hash[10], md5_hash[11],\
54 md5_hash[12], md5_hash[13], md5_hash[14], md5_hash[15]
57 symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
61 struct crypto_shash *md5;
62 struct sdesc *sdescmd5;
64 md5 = crypto_alloc_shash("md5", 0, 0);
67 cifs_dbg(VFS, "%s: Crypto md5 allocation error %d\n",
71 size = sizeof(struct shash_desc) + crypto_shash_descsize(md5);
72 sdescmd5 = kmalloc(size, GFP_KERNEL);
75 goto symlink_hash_err;
77 sdescmd5->shash.tfm = md5;
78 sdescmd5->shash.flags = 0x0;
80 rc = crypto_shash_init(&sdescmd5->shash);
82 cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
83 goto symlink_hash_err;
85 rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len);
87 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
88 goto symlink_hash_err;
90 rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
92 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
95 crypto_free_shash(md5);
102 parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
106 unsigned int link_len;
107 const char *md5_str1;
108 const char *link_str;
112 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
115 md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
116 link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
118 rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
122 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
125 rc = symlink_hash(link_len, link_str, md5_hash);
127 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
131 snprintf(md5_str2, sizeof(md5_str2),
132 CIFS_MF_SYMLINK_MD5_FORMAT,
133 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
135 if (strncmp(md5_str1, md5_str2, 17) != 0)
139 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
144 *_link_len = link_len;
149 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
152 unsigned int link_len;
156 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
159 link_len = strlen(link_str);
161 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
162 return -ENAMETOOLONG;
164 rc = symlink_hash(link_len, link_str, md5_hash);
166 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
170 snprintf(buf, buf_len,
171 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
173 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
175 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
176 memcpy(buf + ofs, link_str, link_len);
179 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
184 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
193 couldbe_mf_symlink(const struct cifs_fattr *fattr)
195 if (!S_ISREG(fattr->cf_mode))
196 /* it's not a symlink */
199 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
200 /* it's not a symlink */
207 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
208 struct cifs_sb_info *cifs_sb, const char *fromName,
213 unsigned int bytes_written = 0;
215 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
219 rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
223 if (tcon->ses->server->ops->create_mf_symlink)
224 rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
225 cifs_sb, fromName, buf, &bytes_written);
232 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
240 query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
241 struct cifs_sb_info *cifs_sb, const unsigned char *path,
246 unsigned int link_len = 0;
247 unsigned int bytes_read = 0;
249 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
253 if (tcon->ses->server->ops->query_mf_symlink)
254 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
255 cifs_sb, path, buf, &bytes_read);
262 if (bytes_read == 0) { /* not a symlink */
267 rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
274 check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
275 struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
276 const unsigned char *path)
280 unsigned int link_len = 0;
281 unsigned int bytes_read = 0;
283 if (!couldbe_mf_symlink(fattr))
284 /* it's not a symlink */
287 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
291 if (tcon->ses->server->ops->query_mf_symlink)
292 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
293 cifs_sb, path, buf, &bytes_read);
300 if (bytes_read == 0) /* not a symlink */
303 rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
305 /* it's not a symlink */
313 /* it is a symlink */
314 fattr->cf_eof = link_len;
315 fattr->cf_mode &= ~S_IFMT;
316 fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
317 fattr->cf_dtype = DT_LNK;
324 * SMB 1.0 Protocol specific functions
328 cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
329 struct cifs_sb_info *cifs_sb, const unsigned char *path,
330 char *pbuf, unsigned int *pbytes_read)
335 struct cifs_open_parms oparms;
336 struct cifs_io_parms io_parms;
337 int buf_type = CIFS_NO_BUFFER;
338 FILE_ALL_INFO file_info;
341 oparms.cifs_sb = cifs_sb;
342 oparms.desired_access = GENERIC_READ;
343 oparms.create_options = CREATE_NOT_DIR;
344 oparms.disposition = FILE_OPEN;
347 oparms.reconnect = false;
349 rc = CIFS_open(xid, &oparms, &oplock, &file_info);
353 if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
355 /* it's not a symlink */
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 = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
367 CIFSSMBClose(xid, tcon, fid.netfid);
372 cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
373 struct cifs_sb_info *cifs_sb, const unsigned char *path,
374 char *pbuf, unsigned int *pbytes_written)
379 struct cifs_open_parms oparms;
380 struct cifs_io_parms io_parms;
381 int create_options = CREATE_NOT_DIR;
383 if (backup_cred(cifs_sb))
384 create_options |= CREATE_OPEN_BACKUP_INTENT;
387 oparms.cifs_sb = cifs_sb;
388 oparms.desired_access = GENERIC_WRITE;
389 oparms.create_options = create_options;
390 oparms.disposition = FILE_CREATE;
393 oparms.reconnect = false;
395 rc = CIFS_open(xid, &oparms, &oplock, NULL);
399 io_parms.netfid = fid.netfid;
400 io_parms.pid = current->tgid;
401 io_parms.tcon = tcon;
403 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
405 rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
406 CIFSSMBClose(xid, tcon, fid.netfid);
411 * SMB 2.1/SMB3 Protocol specific functions
413 #ifdef CONFIG_CIFS_SMB2
415 smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
416 struct cifs_sb_info *cifs_sb, const unsigned char *path,
417 char *pbuf, unsigned int *pbytes_read)
421 struct cifs_open_parms oparms;
422 struct cifs_io_parms io_parms;
423 int buf_type = CIFS_NO_BUFFER;
425 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
426 struct smb2_file_all_info *pfile_info = NULL;
429 oparms.cifs_sb = cifs_sb;
430 oparms.desired_access = GENERIC_READ;
431 oparms.create_options = CREATE_NOT_DIR;
432 if (backup_cred(cifs_sb))
433 oparms.create_options |= CREATE_OPEN_BACKUP_INTENT;
434 oparms.disposition = FILE_OPEN;
436 oparms.reconnect = false;
438 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
439 if (utf16_path == NULL)
442 pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
445 if (pfile_info == NULL) {
450 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL);
452 goto qmf_out_open_fail;
454 if (pfile_info->EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
455 /* it's not a symlink */
456 rc = -ENOENT; /* Is there a better rc to return? */
460 io_parms.netfid = fid.netfid;
461 io_parms.pid = current->tgid;
462 io_parms.tcon = tcon;
464 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
465 io_parms.persistent_fid = fid.persistent_fid;
466 io_parms.volatile_fid = fid.volatile_fid;
467 rc = SMB2_read(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
469 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
477 smb3_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
478 struct cifs_sb_info *cifs_sb, const unsigned char *path,
479 char *pbuf, unsigned int *pbytes_written)
483 struct cifs_open_parms oparms;
484 struct cifs_io_parms io_parms;
485 int create_options = CREATE_NOT_DIR;
487 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
490 if (backup_cred(cifs_sb))
491 create_options |= CREATE_OPEN_BACKUP_INTENT;
493 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
495 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
500 oparms.cifs_sb = cifs_sb;
501 oparms.desired_access = GENERIC_WRITE;
502 oparms.create_options = create_options;
503 oparms.disposition = FILE_CREATE;
505 oparms.reconnect = false;
507 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
513 io_parms.netfid = fid.netfid;
514 io_parms.pid = current->tgid;
515 io_parms.tcon = tcon;
517 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
518 io_parms.persistent_fid = fid.persistent_fid;
519 io_parms.volatile_fid = fid.volatile_fid;
521 /* iov[0] is reserved for smb header */
522 iov[1].iov_base = pbuf;
523 iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
525 rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
527 /* Make sure we wrote all of the symlink data */
528 if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
531 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
536 #endif /* CONFIG_CIFS_SMB2 */
539 * M-F Symlink Functions - End
543 cifs_hardlink(struct dentry *old_file, struct inode *inode,
544 struct dentry *direntry)
548 char *from_name = NULL;
549 char *to_name = NULL;
550 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
551 struct tcon_link *tlink;
552 struct cifs_tcon *tcon;
553 struct TCP_Server_Info *server;
554 struct cifsInodeInfo *cifsInode;
556 tlink = cifs_sb_tlink(cifs_sb);
558 return PTR_ERR(tlink);
559 tcon = tlink_tcon(tlink);
563 from_name = build_path_from_dentry(old_file);
564 to_name = build_path_from_dentry(direntry);
565 if ((from_name == NULL) || (to_name == NULL)) {
571 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
573 cifs_remap(cifs_sb));
575 server = tcon->ses->server;
576 if (!server->ops->create_hardlink) {
580 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
582 if ((rc == -EIO) || (rc == -EINVAL))
586 d_drop(direntry); /* force new lookup from server of target */
589 * if source file is cached (oplocked) revalidate will not go to server
590 * until the file is closed or oplock broken so update nlinks locally
592 if (d_really_is_positive(old_file)) {
593 cifsInode = CIFS_I(d_inode(old_file));
595 spin_lock(&d_inode(old_file)->i_lock);
596 inc_nlink(d_inode(old_file));
597 spin_unlock(&d_inode(old_file)->i_lock);
600 * parent dir timestamps will update from srv within a
601 * second, would it really be worth it to set the parent
602 * dir cifs inode time to zero to force revalidate
603 * (faster) for it too?
607 * if not oplocked will force revalidate to get info on source
608 * file from srv. Note Samba server prior to 4.2 has bug -
609 * not updating src file ctime on hardlinks but Windows servers
615 * Will update parent dir timestamps from srv within a second.
616 * Would it really be worth it to set the parent dir (cifs
617 * inode) time field to zero to force revalidate on parent
618 * directory faster ie
620 * CIFS_I(inode)->time = 0;
628 cifs_put_tlink(tlink);
633 cifs_get_link(struct dentry *direntry, struct inode *inode,
634 struct delayed_call *done)
638 char *full_path = NULL;
639 char *target_path = NULL;
640 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
641 struct tcon_link *tlink = NULL;
642 struct cifs_tcon *tcon;
643 struct TCP_Server_Info *server;
646 return ERR_PTR(-ECHILD);
650 tlink = cifs_sb_tlink(cifs_sb);
653 return ERR_CAST(tlink);
655 tcon = tlink_tcon(tlink);
656 server = tcon->ses->server;
658 full_path = build_path_from_dentry(direntry);
661 cifs_put_tlink(tlink);
662 return ERR_PTR(-ENOMEM);
665 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
669 * First try Minshall+French Symlinks, if configured
670 * and fallback to UNIX Extensions Symlinks.
672 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
673 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
676 if (rc != 0 && server->ops->query_symlink)
677 rc = server->ops->query_symlink(xid, tcon, full_path,
678 &target_path, cifs_sb);
682 cifs_put_tlink(tlink);
687 set_delayed_call(done, kfree_link, target_path);
692 cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
694 int rc = -EOPNOTSUPP;
696 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
697 struct tcon_link *tlink;
698 struct cifs_tcon *pTcon;
699 char *full_path = NULL;
700 struct inode *newinode = NULL;
704 tlink = cifs_sb_tlink(cifs_sb);
709 pTcon = tlink_tcon(tlink);
711 full_path = build_path_from_dentry(direntry);
712 if (full_path == NULL) {
717 cifs_dbg(FYI, "Full path: %s\n", full_path);
718 cifs_dbg(FYI, "symname is %s\n", symname);
720 /* BB what if DFS and this volume is on different share? BB */
721 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
722 rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
723 else if (pTcon->unix_ext)
724 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
726 cifs_remap(cifs_sb));
728 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
729 cifs_sb_target->local_nls); */
733 rc = cifs_get_inode_info_unix(&newinode, full_path,
736 rc = cifs_get_inode_info(&newinode, full_path, NULL,
737 inode->i_sb, xid, NULL);
740 cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
743 d_instantiate(direntry, newinode);
748 cifs_put_tlink(tlink);