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 #include "smb2proto.h"
35 * M-F Symlink Functions - Begin
38 #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
39 #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
40 #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
41 #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
42 #define CIFS_MF_SYMLINK_FILE_SIZE \
43 (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
45 #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
46 #define CIFS_MF_SYMLINK_MD5_FORMAT "%16phN\n"
47 #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) md5_hash
50 symlink_hash(unsigned int link_len
, const char *link_str
, u8
*md5_hash
)
54 struct crypto_shash
*md5
;
55 struct sdesc
*sdescmd5
;
57 md5
= crypto_alloc_shash("md5", 0, 0);
60 cifs_dbg(VFS
, "%s: Crypto md5 allocation error %d\n",
64 size
= sizeof(struct shash_desc
) + crypto_shash_descsize(md5
);
65 sdescmd5
= kmalloc(size
, GFP_KERNEL
);
68 goto symlink_hash_err
;
70 sdescmd5
->shash
.tfm
= md5
;
71 sdescmd5
->shash
.flags
= 0x0;
73 rc
= crypto_shash_init(&sdescmd5
->shash
);
75 cifs_dbg(VFS
, "%s: Could not init md5 shash\n", __func__
);
76 goto symlink_hash_err
;
78 rc
= crypto_shash_update(&sdescmd5
->shash
, link_str
, link_len
);
80 cifs_dbg(VFS
, "%s: Could not update with link_str\n", __func__
);
81 goto symlink_hash_err
;
83 rc
= crypto_shash_final(&sdescmd5
->shash
, md5_hash
);
85 cifs_dbg(VFS
, "%s: Could not generate md5 hash\n", __func__
);
88 crypto_free_shash(md5
);
95 parse_mf_symlink(const u8
*buf
, unsigned int buf_len
, unsigned int *_link_len
,
99 unsigned int link_len
;
100 const char *md5_str1
;
101 const char *link_str
;
105 if (buf_len
!= CIFS_MF_SYMLINK_FILE_SIZE
)
108 md5_str1
= (const char *)&buf
[CIFS_MF_SYMLINK_MD5_OFFSET
];
109 link_str
= (const char *)&buf
[CIFS_MF_SYMLINK_LINK_OFFSET
];
111 rc
= sscanf(buf
, CIFS_MF_SYMLINK_LEN_FORMAT
, &link_len
);
115 rc
= symlink_hash(link_len
, link_str
, md5_hash
);
117 cifs_dbg(FYI
, "%s: MD5 hash failure: %d\n", __func__
, rc
);
121 snprintf(md5_str2
, sizeof(md5_str2
),
122 CIFS_MF_SYMLINK_MD5_FORMAT
,
123 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash
));
125 if (strncmp(md5_str1
, md5_str2
, 17) != 0)
129 *_link_str
= kstrndup(link_str
, link_len
, GFP_KERNEL
);
134 *_link_len
= link_len
;
139 format_mf_symlink(u8
*buf
, unsigned int buf_len
, const char *link_str
)
142 unsigned int link_len
;
146 if (buf_len
!= CIFS_MF_SYMLINK_FILE_SIZE
)
149 link_len
= strlen(link_str
);
151 if (link_len
> CIFS_MF_SYMLINK_LINK_MAXLEN
)
152 return -ENAMETOOLONG
;
154 rc
= symlink_hash(link_len
, link_str
, md5_hash
);
156 cifs_dbg(FYI
, "%s: MD5 hash failure: %d\n", __func__
, rc
);
160 snprintf(buf
, buf_len
,
161 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT
,
163 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash
));
165 ofs
= CIFS_MF_SYMLINK_LINK_OFFSET
;
166 memcpy(buf
+ ofs
, link_str
, link_len
);
169 if (ofs
< CIFS_MF_SYMLINK_FILE_SIZE
) {
174 while (ofs
< CIFS_MF_SYMLINK_FILE_SIZE
) {
183 couldbe_mf_symlink(const struct cifs_fattr
*fattr
)
185 if (!S_ISREG(fattr
->cf_mode
))
186 /* it's not a symlink */
189 if (fattr
->cf_eof
!= CIFS_MF_SYMLINK_FILE_SIZE
)
190 /* it's not a symlink */
197 create_mf_symlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
198 struct cifs_sb_info
*cifs_sb
, const char *fromName
,
203 unsigned int bytes_written
= 0;
205 buf
= kmalloc(CIFS_MF_SYMLINK_FILE_SIZE
, GFP_KERNEL
);
209 rc
= format_mf_symlink(buf
, CIFS_MF_SYMLINK_FILE_SIZE
, toName
);
213 if (tcon
->ses
->server
->ops
->create_mf_symlink
)
214 rc
= tcon
->ses
->server
->ops
->create_mf_symlink(xid
, tcon
,
215 cifs_sb
, fromName
, buf
, &bytes_written
);
222 if (bytes_written
!= CIFS_MF_SYMLINK_FILE_SIZE
)
230 query_mf_symlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
231 struct cifs_sb_info
*cifs_sb
, const unsigned char *path
,
236 unsigned int link_len
= 0;
237 unsigned int bytes_read
= 0;
239 buf
= kmalloc(CIFS_MF_SYMLINK_FILE_SIZE
, GFP_KERNEL
);
243 if (tcon
->ses
->server
->ops
->query_mf_symlink
)
244 rc
= tcon
->ses
->server
->ops
->query_mf_symlink(xid
, tcon
,
245 cifs_sb
, path
, buf
, &bytes_read
);
252 if (bytes_read
== 0) { /* not a symlink */
257 rc
= parse_mf_symlink(buf
, bytes_read
, &link_len
, symlinkinfo
);
264 check_mf_symlink(unsigned int xid
, struct cifs_tcon
*tcon
,
265 struct cifs_sb_info
*cifs_sb
, struct cifs_fattr
*fattr
,
266 const unsigned char *path
)
270 unsigned int link_len
= 0;
271 unsigned int bytes_read
= 0;
273 if (!couldbe_mf_symlink(fattr
))
274 /* it's not a symlink */
277 buf
= kmalloc(CIFS_MF_SYMLINK_FILE_SIZE
, GFP_KERNEL
);
281 if (tcon
->ses
->server
->ops
->query_mf_symlink
)
282 rc
= tcon
->ses
->server
->ops
->query_mf_symlink(xid
, tcon
,
283 cifs_sb
, path
, buf
, &bytes_read
);
290 if (bytes_read
== 0) /* not a symlink */
293 rc
= parse_mf_symlink(buf
, bytes_read
, &link_len
, NULL
);
295 /* it's not a symlink */
303 /* it is a symlink */
304 fattr
->cf_eof
= link_len
;
305 fattr
->cf_mode
&= ~S_IFMT
;
306 fattr
->cf_mode
|= S_IFLNK
| S_IRWXU
| S_IRWXG
| S_IRWXO
;
307 fattr
->cf_dtype
= DT_LNK
;
314 * SMB 1.0 Protocol specific functions
318 cifs_query_mf_symlink(unsigned int xid
, struct cifs_tcon
*tcon
,
319 struct cifs_sb_info
*cifs_sb
, const unsigned char *path
,
320 char *pbuf
, unsigned int *pbytes_read
)
325 struct cifs_open_parms oparms
;
326 struct cifs_io_parms io_parms
;
327 int buf_type
= CIFS_NO_BUFFER
;
328 FILE_ALL_INFO file_info
;
331 oparms
.cifs_sb
= cifs_sb
;
332 oparms
.desired_access
= GENERIC_READ
;
333 oparms
.create_options
= CREATE_NOT_DIR
;
334 oparms
.disposition
= FILE_OPEN
;
337 oparms
.reconnect
= false;
339 rc
= CIFS_open(xid
, &oparms
, &oplock
, &file_info
);
343 if (file_info
.EndOfFile
!= cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE
)) {
345 /* it's not a symlink */
349 io_parms
.netfid
= fid
.netfid
;
350 io_parms
.pid
= current
->tgid
;
351 io_parms
.tcon
= tcon
;
353 io_parms
.length
= CIFS_MF_SYMLINK_FILE_SIZE
;
355 rc
= CIFSSMBRead(xid
, &io_parms
, pbytes_read
, &pbuf
, &buf_type
);
357 CIFSSMBClose(xid
, tcon
, fid
.netfid
);
362 cifs_create_mf_symlink(unsigned int xid
, struct cifs_tcon
*tcon
,
363 struct cifs_sb_info
*cifs_sb
, const unsigned char *path
,
364 char *pbuf
, unsigned int *pbytes_written
)
369 struct cifs_open_parms oparms
;
370 struct cifs_io_parms io_parms
;
371 int create_options
= CREATE_NOT_DIR
;
373 if (backup_cred(cifs_sb
))
374 create_options
|= CREATE_OPEN_BACKUP_INTENT
;
377 oparms
.cifs_sb
= cifs_sb
;
378 oparms
.desired_access
= GENERIC_WRITE
;
379 oparms
.create_options
= create_options
;
380 oparms
.disposition
= FILE_CREATE
;
383 oparms
.reconnect
= false;
385 rc
= CIFS_open(xid
, &oparms
, &oplock
, NULL
);
389 io_parms
.netfid
= fid
.netfid
;
390 io_parms
.pid
= current
->tgid
;
391 io_parms
.tcon
= tcon
;
393 io_parms
.length
= CIFS_MF_SYMLINK_FILE_SIZE
;
395 rc
= CIFSSMBWrite(xid
, &io_parms
, pbytes_written
, pbuf
);
396 CIFSSMBClose(xid
, tcon
, fid
.netfid
);
401 * SMB 2.1/SMB3 Protocol specific functions
404 smb3_query_mf_symlink(unsigned int xid
, struct cifs_tcon
*tcon
,
405 struct cifs_sb_info
*cifs_sb
, const unsigned char *path
,
406 char *pbuf
, unsigned int *pbytes_read
)
410 struct cifs_open_parms oparms
;
411 struct cifs_io_parms io_parms
;
412 int buf_type
= CIFS_NO_BUFFER
;
414 __u8 oplock
= SMB2_OPLOCK_LEVEL_II
;
415 struct smb2_file_all_info
*pfile_info
= NULL
;
418 oparms
.cifs_sb
= cifs_sb
;
419 oparms
.desired_access
= GENERIC_READ
;
420 oparms
.create_options
= CREATE_NOT_DIR
;
421 if (backup_cred(cifs_sb
))
422 oparms
.create_options
|= CREATE_OPEN_BACKUP_INTENT
;
423 oparms
.disposition
= FILE_OPEN
;
425 oparms
.reconnect
= false;
427 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
428 if (utf16_path
== NULL
)
431 pfile_info
= kzalloc(sizeof(struct smb2_file_all_info
) + PATH_MAX
* 2,
434 if (pfile_info
== NULL
) {
439 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, pfile_info
, NULL
);
441 goto qmf_out_open_fail
;
443 if (pfile_info
->EndOfFile
!= cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE
)) {
444 /* it's not a symlink */
445 rc
= -ENOENT
; /* Is there a better rc to return? */
449 io_parms
.netfid
= fid
.netfid
;
450 io_parms
.pid
= current
->tgid
;
451 io_parms
.tcon
= tcon
;
453 io_parms
.length
= CIFS_MF_SYMLINK_FILE_SIZE
;
454 io_parms
.persistent_fid
= fid
.persistent_fid
;
455 io_parms
.volatile_fid
= fid
.volatile_fid
;
456 rc
= SMB2_read(xid
, &io_parms
, pbytes_read
, &pbuf
, &buf_type
);
458 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
466 smb3_create_mf_symlink(unsigned int xid
, struct cifs_tcon
*tcon
,
467 struct cifs_sb_info
*cifs_sb
, const unsigned char *path
,
468 char *pbuf
, unsigned int *pbytes_written
)
472 struct cifs_open_parms oparms
;
473 struct cifs_io_parms io_parms
;
474 int create_options
= CREATE_NOT_DIR
;
476 __u8 oplock
= SMB2_OPLOCK_LEVEL_EXCLUSIVE
;
479 if (backup_cred(cifs_sb
))
480 create_options
|= CREATE_OPEN_BACKUP_INTENT
;
482 cifs_dbg(FYI
, "%s: path: %s\n", __func__
, path
);
484 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
489 oparms
.cifs_sb
= cifs_sb
;
490 oparms
.desired_access
= GENERIC_WRITE
;
491 oparms
.create_options
= create_options
;
492 oparms
.disposition
= FILE_CREATE
;
494 oparms
.reconnect
= false;
496 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, NULL
, NULL
);
502 io_parms
.netfid
= fid
.netfid
;
503 io_parms
.pid
= current
->tgid
;
504 io_parms
.tcon
= tcon
;
506 io_parms
.length
= CIFS_MF_SYMLINK_FILE_SIZE
;
507 io_parms
.persistent_fid
= fid
.persistent_fid
;
508 io_parms
.volatile_fid
= fid
.volatile_fid
;
510 /* iov[0] is reserved for smb header */
511 iov
[1].iov_base
= pbuf
;
512 iov
[1].iov_len
= CIFS_MF_SYMLINK_FILE_SIZE
;
514 rc
= SMB2_write(xid
, &io_parms
, pbytes_written
, iov
, 1);
516 /* Make sure we wrote all of the symlink data */
517 if ((rc
== 0) && (*pbytes_written
!= CIFS_MF_SYMLINK_FILE_SIZE
))
520 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
527 * M-F Symlink Functions - End
531 cifs_hardlink(struct dentry
*old_file
, struct inode
*inode
,
532 struct dentry
*direntry
)
536 char *from_name
= NULL
;
537 char *to_name
= NULL
;
538 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
539 struct tcon_link
*tlink
;
540 struct cifs_tcon
*tcon
;
541 struct TCP_Server_Info
*server
;
542 struct cifsInodeInfo
*cifsInode
;
544 tlink
= cifs_sb_tlink(cifs_sb
);
546 return PTR_ERR(tlink
);
547 tcon
= tlink_tcon(tlink
);
551 from_name
= build_path_from_dentry(old_file
);
552 to_name
= build_path_from_dentry(direntry
);
553 if ((from_name
== NULL
) || (to_name
== NULL
)) {
559 rc
= CIFSUnixCreateHardLink(xid
, tcon
, from_name
, to_name
,
561 cifs_remap(cifs_sb
));
563 server
= tcon
->ses
->server
;
564 if (!server
->ops
->create_hardlink
) {
568 rc
= server
->ops
->create_hardlink(xid
, tcon
, from_name
, to_name
,
570 if ((rc
== -EIO
) || (rc
== -EINVAL
))
574 d_drop(direntry
); /* force new lookup from server of target */
577 * if source file is cached (oplocked) revalidate will not go to server
578 * until the file is closed or oplock broken so update nlinks locally
580 if (d_really_is_positive(old_file
)) {
581 cifsInode
= CIFS_I(d_inode(old_file
));
583 spin_lock(&d_inode(old_file
)->i_lock
);
584 inc_nlink(d_inode(old_file
));
585 spin_unlock(&d_inode(old_file
)->i_lock
);
588 * parent dir timestamps will update from srv within a
589 * second, would it really be worth it to set the parent
590 * dir cifs inode time to zero to force revalidate
591 * (faster) for it too?
595 * if not oplocked will force revalidate to get info on source
596 * file from srv. Note Samba server prior to 4.2 has bug -
597 * not updating src file ctime on hardlinks but Windows servers
603 * Will update parent dir timestamps from srv within a second.
604 * Would it really be worth it to set the parent dir (cifs
605 * inode) time field to zero to force revalidate on parent
606 * directory faster ie
608 * CIFS_I(inode)->time = 0;
616 cifs_put_tlink(tlink
);
621 cifs_get_link(struct dentry
*direntry
, struct inode
*inode
,
622 struct delayed_call
*done
)
626 char *full_path
= NULL
;
627 char *target_path
= NULL
;
628 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
629 struct tcon_link
*tlink
= NULL
;
630 struct cifs_tcon
*tcon
;
631 struct TCP_Server_Info
*server
;
634 return ERR_PTR(-ECHILD
);
638 tlink
= cifs_sb_tlink(cifs_sb
);
641 return ERR_CAST(tlink
);
643 tcon
= tlink_tcon(tlink
);
644 server
= tcon
->ses
->server
;
646 full_path
= build_path_from_dentry(direntry
);
649 cifs_put_tlink(tlink
);
650 return ERR_PTR(-ENOMEM
);
653 cifs_dbg(FYI
, "Full path: %s inode = 0x%p\n", full_path
, inode
);
657 * First try Minshall+French Symlinks, if configured
658 * and fallback to UNIX Extensions Symlinks.
660 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MF_SYMLINKS
)
661 rc
= query_mf_symlink(xid
, tcon
, cifs_sb
, full_path
,
664 if (rc
!= 0 && server
->ops
->query_symlink
)
665 rc
= server
->ops
->query_symlink(xid
, tcon
, full_path
,
666 &target_path
, cifs_sb
);
670 cifs_put_tlink(tlink
);
675 set_delayed_call(done
, kfree_link
, target_path
);
680 cifs_symlink(struct inode
*inode
, struct dentry
*direntry
, const char *symname
)
682 int rc
= -EOPNOTSUPP
;
684 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
685 struct tcon_link
*tlink
;
686 struct cifs_tcon
*pTcon
;
687 char *full_path
= NULL
;
688 struct inode
*newinode
= NULL
;
692 tlink
= cifs_sb_tlink(cifs_sb
);
697 pTcon
= tlink_tcon(tlink
);
699 full_path
= build_path_from_dentry(direntry
);
700 if (full_path
== NULL
) {
705 cifs_dbg(FYI
, "Full path: %s\n", full_path
);
706 cifs_dbg(FYI
, "symname is %s\n", symname
);
708 /* BB what if DFS and this volume is on different share? BB */
709 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MF_SYMLINKS
)
710 rc
= create_mf_symlink(xid
, pTcon
, cifs_sb
, full_path
, symname
);
711 else if (pTcon
->unix_ext
)
712 rc
= CIFSUnixCreateSymLink(xid
, pTcon
, full_path
, symname
,
714 cifs_remap(cifs_sb
));
716 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
717 cifs_sb_target->local_nls); */
721 rc
= cifs_get_inode_info_unix(&newinode
, full_path
,
724 rc
= cifs_get_inode_info(&newinode
, full_path
, NULL
,
725 inode
->i_sb
, xid
, NULL
);
728 cifs_dbg(FYI
, "Create symlink ok, getinodeinfo fail rc = %d\n",
731 d_instantiate(direntry
, newinode
);
736 cifs_put_tlink(tlink
);