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"
32 #define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
33 #define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
34 #define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
35 #define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
36 #define CIFS_MF_SYMLINK_FILE_SIZE \
37 (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
39 #define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
40 #define CIFS_MF_SYMLINK_MD5_FORMAT \
41 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n"
42 #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) \
43 md5_hash[0], md5_hash[1], md5_hash[2], md5_hash[3], \
44 md5_hash[4], md5_hash[5], md5_hash[6], md5_hash[7], \
45 md5_hash[8], md5_hash[9], md5_hash[10], md5_hash[11],\
46 md5_hash[12], md5_hash[13], md5_hash[14], md5_hash[15]
49 symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
53 struct crypto_shash *md5;
54 struct sdesc *sdescmd5;
56 md5 = crypto_alloc_shash("md5", 0, 0);
58 cERROR(1, "%s: Crypto md5 allocation error %d\n", __func__, rc);
61 size = sizeof(struct shash_desc) + crypto_shash_descsize(md5);
62 sdescmd5 = kmalloc(size, GFP_KERNEL);
65 cERROR(1, "%s: Memory allocation failure\n", __func__);
66 goto symlink_hash_err;
68 sdescmd5->shash.tfm = md5;
69 sdescmd5->shash.flags = 0x0;
71 rc = crypto_shash_init(&sdescmd5->shash);
73 cERROR(1, "%s: Could not init md5 shash\n", __func__);
74 goto symlink_hash_err;
76 crypto_shash_update(&sdescmd5->shash, link_str, link_len);
77 rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
80 crypto_free_shash(md5);
87 CIFSParseMFSymlink(const u8 *buf,
89 unsigned int *_link_len,
93 unsigned int link_len;
99 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
102 md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
103 link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
105 rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
109 rc = symlink_hash(link_len, link_str, md5_hash);
111 cFYI(1, "%s: MD5 hash failure: %d\n", __func__, rc);
115 snprintf(md5_str2, sizeof(md5_str2),
116 CIFS_MF_SYMLINK_MD5_FORMAT,
117 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
119 if (strncmp(md5_str1, md5_str2, 17) != 0)
123 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
128 *_link_len = link_len;
133 CIFSFormatMFSymlink(u8 *buf, unsigned int buf_len, const char *link_str)
136 unsigned int link_len;
140 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
143 link_len = strlen(link_str);
145 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
146 return -ENAMETOOLONG;
148 rc = symlink_hash(link_len, link_str, md5_hash);
150 cFYI(1, "%s: MD5 hash failure: %d\n", __func__, rc);
154 snprintf(buf, buf_len,
155 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
157 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
159 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
160 memcpy(buf + ofs, link_str, link_len);
163 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
168 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
177 CIFSCreateMFSymLink(const int xid, struct cifsTconInfo *tcon,
178 const char *fromName, const char *toName,
179 const struct nls_table *nls_codepage, int remap)
185 unsigned int bytes_written = 0;
187 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
191 rc = CIFSFormatMFSymlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
197 rc = CIFSSMBOpen(xid, tcon, fromName, FILE_CREATE, GENERIC_WRITE,
198 CREATE_NOT_DIR, &netfid, &oplock, NULL,
199 nls_codepage, remap);
205 rc = CIFSSMBWrite(xid, tcon, netfid,
206 CIFS_MF_SYMLINK_FILE_SIZE /* length */,
208 &bytes_written, buf, NULL, 0);
209 CIFSSMBClose(xid, tcon, netfid);
214 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
221 CIFSQueryMFSymLink(const int xid, struct cifsTconInfo *tcon,
222 const unsigned char *searchName, char **symlinkinfo,
223 const struct nls_table *nls_codepage, int remap)
230 unsigned int bytes_read = 0;
231 int buf_type = CIFS_NO_BUFFER;
232 unsigned int link_len = 0;
233 FILE_ALL_INFO file_info;
235 rc = CIFSSMBOpen(xid, tcon, searchName, FILE_OPEN, GENERIC_READ,
236 CREATE_NOT_DIR, &netfid, &oplock, &file_info,
237 nls_codepage, remap);
241 if (file_info.EndOfFile != CIFS_MF_SYMLINK_FILE_SIZE) {
242 CIFSSMBClose(xid, tcon, netfid);
243 /* it's not a symlink */
247 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
252 rc = CIFSSMBRead(xid, tcon, netfid,
253 CIFS_MF_SYMLINK_FILE_SIZE /* length */,
255 &bytes_read, &pbuf, &buf_type);
256 CIFSSMBClose(xid, tcon, netfid);
262 rc = CIFSParseMFSymlink(buf, bytes_read, &link_len, symlinkinfo);
271 CIFSCouldBeMFSymlink(const struct cifs_fattr *fattr)
273 if (!(fattr->cf_mode & S_IFREG))
274 /* it's not a symlink */
277 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
278 /* it's not a symlink */
285 CIFSCheckMFSymlink(struct cifs_fattr *fattr,
286 const unsigned char *path,
287 struct cifs_sb_info *cifs_sb, int xid)
292 struct tcon_link *tlink;
293 struct cifsTconInfo *pTcon;
296 unsigned int bytes_read = 0;
297 int buf_type = CIFS_NO_BUFFER;
298 unsigned int link_len = 0;
299 FILE_ALL_INFO file_info;
301 if (!CIFSCouldBeMFSymlink(fattr))
302 /* it's not a symlink */
305 tlink = cifs_sb_tlink(cifs_sb);
307 return PTR_ERR(tlink);
308 pTcon = tlink_tcon(tlink);
310 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
311 CREATE_NOT_DIR, &netfid, &oplock, &file_info,
313 cifs_sb->mnt_cifs_flags &
314 CIFS_MOUNT_MAP_SPECIAL_CHR);
318 if (file_info.EndOfFile != CIFS_MF_SYMLINK_FILE_SIZE) {
319 CIFSSMBClose(xid, pTcon, netfid);
320 /* it's not a symlink */
324 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
331 rc = CIFSSMBRead(xid, pTcon, netfid,
332 CIFS_MF_SYMLINK_FILE_SIZE /* length */,
334 &bytes_read, &pbuf, &buf_type);
335 CIFSSMBClose(xid, pTcon, netfid);
341 rc = CIFSParseMFSymlink(buf, bytes_read, &link_len, NULL);
344 /* it's not a symlink */
352 /* it is a symlink */
353 fattr->cf_eof = link_len;
354 fattr->cf_mode &= ~S_IFMT;
355 fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
356 fattr->cf_dtype = DT_LNK;
358 cifs_put_tlink(tlink);
363 cifs_hardlink(struct dentry *old_file, struct inode *inode,
364 struct dentry *direntry)
368 char *fromName = NULL;
370 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
371 struct tcon_link *tlink;
372 struct cifsTconInfo *pTcon;
373 struct cifsInodeInfo *cifsInode;
375 tlink = cifs_sb_tlink(cifs_sb);
377 return PTR_ERR(tlink);
378 pTcon = tlink_tcon(tlink);
382 fromName = build_path_from_dentry(old_file);
383 toName = build_path_from_dentry(direntry);
384 if ((fromName == NULL) || (toName == NULL)) {
390 rc = CIFSUnixCreateHardLink(xid, pTcon, fromName, toName,
392 cifs_sb->mnt_cifs_flags &
393 CIFS_MOUNT_MAP_SPECIAL_CHR);
395 rc = CIFSCreateHardLink(xid, pTcon, fromName, toName,
397 cifs_sb->mnt_cifs_flags &
398 CIFS_MOUNT_MAP_SPECIAL_CHR);
399 if ((rc == -EIO) || (rc == -EINVAL))
403 d_drop(direntry); /* force new lookup from server of target */
405 /* if source file is cached (oplocked) revalidate will not go to server
406 until the file is closed or oplock broken so update nlinks locally */
407 if (old_file->d_inode) {
408 cifsInode = CIFS_I(old_file->d_inode);
410 old_file->d_inode->i_nlink++;
411 /* BB should we make this contingent on superblock flag NOATIME? */
412 /* old_file->d_inode->i_ctime = CURRENT_TIME;*/
413 /* parent dir timestamps will update from srv
414 within a second, would it really be worth it
415 to set the parent dir cifs inode time to zero
416 to force revalidate (faster) for it too? */
418 /* if not oplocked will force revalidate to get info
419 on source file from srv */
422 /* Will update parent dir timestamps from srv within a second.
423 Would it really be worth it to set the parent dir (cifs
424 inode) time field to zero to force revalidate on parent
426 CIFS_I(inode)->time = 0; */
433 cifs_put_tlink(tlink);
438 cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
440 struct inode *inode = direntry->d_inode;
443 char *full_path = NULL;
444 char *target_path = NULL;
445 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
446 struct tcon_link *tlink = NULL;
447 struct cifsTconInfo *tcon;
451 tlink = cifs_sb_tlink(cifs_sb);
457 tcon = tlink_tcon(tlink);
460 * For now, we just handle symlinks with unix extensions enabled.
461 * Eventually we should handle NTFS reparse points, and MacOS
462 * symlink support. For instance...
464 * rc = CIFSSMBQueryReparseLinkInfo(...)
466 * For now, just return -EACCES when the server doesn't support posix
467 * extensions. Note that we still allow querying symlinks when posix
468 * extensions are manually disabled. We could disable these as well
469 * but there doesn't seem to be any harm in allowing the client to
472 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
473 && !(tcon->ses->capabilities & CAP_UNIX)) {
478 full_path = build_path_from_dentry(direntry);
482 cFYI(1, "Full path: %s inode = 0x%p", full_path, inode);
486 * First try Minshall+French Symlinks, if configured
487 * and fallback to UNIX Extensions Symlinks.
489 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
490 rc = CIFSQueryMFSymLink(xid, tcon, full_path, &target_path,
492 cifs_sb->mnt_cifs_flags &
493 CIFS_MOUNT_MAP_SPECIAL_CHR);
495 if ((rc != 0) && (tcon->ses->capabilities & CAP_UNIX))
496 rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, &target_path,
503 target_path = ERR_PTR(rc);
508 cifs_put_tlink(tlink);
509 nd_set_link(nd, target_path);
514 cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
516 int rc = -EOPNOTSUPP;
518 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
519 struct tcon_link *tlink;
520 struct cifsTconInfo *pTcon;
521 char *full_path = NULL;
522 struct inode *newinode = NULL;
526 tlink = cifs_sb_tlink(cifs_sb);
531 pTcon = tlink_tcon(tlink);
533 full_path = build_path_from_dentry(direntry);
534 if (full_path == NULL) {
539 cFYI(1, "Full path: %s", full_path);
540 cFYI(1, "symname is %s", symname);
542 /* BB what if DFS and this volume is on different share? BB */
543 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
544 rc = CIFSCreateMFSymLink(xid, pTcon, full_path, symname,
546 cifs_sb->mnt_cifs_flags &
547 CIFS_MOUNT_MAP_SPECIAL_CHR);
548 else if (pTcon->unix_ext)
549 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
552 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
553 cifs_sb_target->local_nls); */
557 rc = cifs_get_inode_info_unix(&newinode, full_path,
560 rc = cifs_get_inode_info(&newinode, full_path, NULL,
561 inode->i_sb, xid, NULL);
564 cFYI(1, "Create symlink ok, getinodeinfo fail rc = %d",
567 d_instantiate(direntry, newinode);
572 cifs_put_tlink(tlink);
577 void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *cookie)
579 char *p = nd_get_link(nd);