Mention branches and keyring.
[releases.git] / cifs / link.c
1 /*
2  *   fs/cifs/link.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
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.
11  *
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.
16  *
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
20  */
21 #include <linux/fs.h>
22 #include <linux/stat.h>
23 #include <linux/slab.h>
24 #include <linux/namei.h>
25 #include "cifsfs.h"
26 #include "cifspdu.h"
27 #include "cifsglob.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"
34 #endif
35
36 /*
37  * M-F Symlink Functions - Begin
38  */
39
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)
46
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]
55
56 static int
57 symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
58 {
59         int rc;
60         unsigned int size;
61         struct crypto_shash *md5;
62         struct sdesc *sdescmd5;
63
64         md5 = crypto_alloc_shash("md5", 0, 0);
65         if (IS_ERR(md5)) {
66                 rc = PTR_ERR(md5);
67                 cifs_dbg(VFS, "%s: Crypto md5 allocation error %d\n",
68                          __func__, rc);
69                 return rc;
70         }
71         size = sizeof(struct shash_desc) + crypto_shash_descsize(md5);
72         sdescmd5 = kmalloc(size, GFP_KERNEL);
73         if (!sdescmd5) {
74                 rc = -ENOMEM;
75                 goto symlink_hash_err;
76         }
77         sdescmd5->shash.tfm = md5;
78         sdescmd5->shash.flags = 0x0;
79
80         rc = crypto_shash_init(&sdescmd5->shash);
81         if (rc) {
82                 cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
83                 goto symlink_hash_err;
84         }
85         rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len);
86         if (rc) {
87                 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
88                 goto symlink_hash_err;
89         }
90         rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
91         if (rc)
92                 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
93
94 symlink_hash_err:
95         crypto_free_shash(md5);
96         kfree(sdescmd5);
97
98         return rc;
99 }
100
101 static int
102 parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
103                  char **_link_str)
104 {
105         int rc;
106         unsigned int link_len;
107         const char *md5_str1;
108         const char *link_str;
109         u8 md5_hash[16];
110         char md5_str2[34];
111
112         if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
113                 return -EINVAL;
114
115         md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
116         link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
117
118         rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
119         if (rc != 1)
120                 return -EINVAL;
121
122         if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
123                 return -EINVAL;
124
125         rc = symlink_hash(link_len, link_str, md5_hash);
126         if (rc) {
127                 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
128                 return rc;
129         }
130
131         snprintf(md5_str2, sizeof(md5_str2),
132                  CIFS_MF_SYMLINK_MD5_FORMAT,
133                  CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
134
135         if (strncmp(md5_str1, md5_str2, 17) != 0)
136                 return -EINVAL;
137
138         if (_link_str) {
139                 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
140                 if (!*_link_str)
141                         return -ENOMEM;
142         }
143
144         *_link_len = link_len;
145         return 0;
146 }
147
148 static int
149 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
150 {
151         int rc;
152         unsigned int link_len;
153         unsigned int ofs;
154         u8 md5_hash[16];
155
156         if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
157                 return -EINVAL;
158
159         link_len = strlen(link_str);
160
161         if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
162                 return -ENAMETOOLONG;
163
164         rc = symlink_hash(link_len, link_str, md5_hash);
165         if (rc) {
166                 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
167                 return rc;
168         }
169
170         snprintf(buf, buf_len,
171                  CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
172                  link_len,
173                  CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
174
175         ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
176         memcpy(buf + ofs, link_str, link_len);
177
178         ofs += link_len;
179         if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
180                 buf[ofs] = '\n';
181                 ofs++;
182         }
183
184         while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
185                 buf[ofs] = ' ';
186                 ofs++;
187         }
188
189         return 0;
190 }
191
192 bool
193 couldbe_mf_symlink(const struct cifs_fattr *fattr)
194 {
195         if (!S_ISREG(fattr->cf_mode))
196                 /* it's not a symlink */
197                 return false;
198
199         if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
200                 /* it's not a symlink */
201                 return false;
202
203         return true;
204 }
205
206 static int
207 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
208                   struct cifs_sb_info *cifs_sb, const char *fromName,
209                   const char *toName)
210 {
211         int rc;
212         u8 *buf;
213         unsigned int bytes_written = 0;
214
215         buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
216         if (!buf)
217                 return -ENOMEM;
218
219         rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
220         if (rc)
221                 goto out;
222
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);
226         else
227                 rc = -EOPNOTSUPP;
228
229         if (rc)
230                 goto out;
231
232         if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
233                 rc = -EIO;
234 out:
235         kfree(buf);
236         return rc;
237 }
238
239 static int
240 query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
241                  struct cifs_sb_info *cifs_sb, const unsigned char *path,
242                  char **symlinkinfo)
243 {
244         int rc;
245         u8 *buf = NULL;
246         unsigned int link_len = 0;
247         unsigned int bytes_read = 0;
248
249         buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
250         if (!buf)
251                 return -ENOMEM;
252
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);
256         else
257                 rc = -ENOSYS;
258
259         if (rc)
260                 goto out;
261
262         if (bytes_read == 0) { /* not a symlink */
263                 rc = -EINVAL;
264                 goto out;
265         }
266
267         rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
268 out:
269         kfree(buf);
270         return rc;
271 }
272
273 int
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)
277 {
278         int rc;
279         u8 *buf = NULL;
280         unsigned int link_len = 0;
281         unsigned int bytes_read = 0;
282
283         if (!couldbe_mf_symlink(fattr))
284                 /* it's not a symlink */
285                 return 0;
286
287         buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
288         if (!buf)
289                 return -ENOMEM;
290
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);
294         else
295                 rc = -ENOSYS;
296
297         if (rc)
298                 goto out;
299
300         if (bytes_read == 0) /* not a symlink */
301                 goto out;
302
303         rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
304         if (rc == -EINVAL) {
305                 /* it's not a symlink */
306                 rc = 0;
307                 goto out;
308         }
309
310         if (rc != 0)
311                 goto out;
312
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;
318 out:
319         kfree(buf);
320         return rc;
321 }
322
323 /*
324  * SMB 1.0 Protocol specific functions
325  */
326
327 int
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)
331 {
332         int rc;
333         int oplock = 0;
334         struct cifs_fid fid;
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;
339
340         oparms.tcon = tcon;
341         oparms.cifs_sb = cifs_sb;
342         oparms.desired_access = GENERIC_READ;
343         oparms.create_options = CREATE_NOT_DIR;
344         oparms.disposition = FILE_OPEN;
345         oparms.path = path;
346         oparms.fid = &fid;
347         oparms.reconnect = false;
348
349         rc = CIFS_open(xid, &oparms, &oplock, &file_info);
350         if (rc)
351                 return rc;
352
353         if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
354                 rc = -ENOENT;
355                 /* it's not a symlink */
356                 goto out;
357         }
358
359         io_parms.netfid = fid.netfid;
360         io_parms.pid = current->tgid;
361         io_parms.tcon = tcon;
362         io_parms.offset = 0;
363         io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
364
365         rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
366 out:
367         CIFSSMBClose(xid, tcon, fid.netfid);
368         return rc;
369 }
370
371 int
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)
375 {
376         int rc;
377         int oplock = 0;
378         struct cifs_fid fid;
379         struct cifs_open_parms oparms;
380         struct cifs_io_parms io_parms;
381         int create_options = CREATE_NOT_DIR;
382
383         if (backup_cred(cifs_sb))
384                 create_options |= CREATE_OPEN_BACKUP_INTENT;
385
386         oparms.tcon = tcon;
387         oparms.cifs_sb = cifs_sb;
388         oparms.desired_access = GENERIC_WRITE;
389         oparms.create_options = create_options;
390         oparms.disposition = FILE_CREATE;
391         oparms.path = path;
392         oparms.fid = &fid;
393         oparms.reconnect = false;
394
395         rc = CIFS_open(xid, &oparms, &oplock, NULL);
396         if (rc)
397                 return rc;
398
399         io_parms.netfid = fid.netfid;
400         io_parms.pid = current->tgid;
401         io_parms.tcon = tcon;
402         io_parms.offset = 0;
403         io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
404
405         rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
406         CIFSSMBClose(xid, tcon, fid.netfid);
407         return rc;
408 }
409
410 /*
411  * SMB 2.1/SMB3 Protocol specific functions
412  */
413 #ifdef CONFIG_CIFS_SMB2
414 int
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)
418 {
419         int rc;
420         struct cifs_fid fid;
421         struct cifs_open_parms oparms;
422         struct cifs_io_parms io_parms;
423         int buf_type = CIFS_NO_BUFFER;
424         __le16 *utf16_path;
425         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
426         struct smb2_file_all_info *pfile_info = NULL;
427
428         oparms.tcon = tcon;
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;
435         oparms.fid = &fid;
436         oparms.reconnect = false;
437
438         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
439         if (utf16_path == NULL)
440                 return -ENOMEM;
441
442         pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
443                              GFP_KERNEL);
444
445         if (pfile_info == NULL) {
446                 kfree(utf16_path);
447                 return  -ENOMEM;
448         }
449
450         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL);
451         if (rc)
452                 goto qmf_out_open_fail;
453
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? */
457                 goto qmf_out;
458         }
459
460         io_parms.netfid = fid.netfid;
461         io_parms.pid = current->tgid;
462         io_parms.tcon = tcon;
463         io_parms.offset = 0;
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);
468 qmf_out:
469         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
470 qmf_out_open_fail:
471         kfree(utf16_path);
472         kfree(pfile_info);
473         return rc;
474 }
475
476 int
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)
480 {
481         int rc;
482         struct cifs_fid fid;
483         struct cifs_open_parms oparms;
484         struct cifs_io_parms io_parms;
485         int create_options = CREATE_NOT_DIR;
486         __le16 *utf16_path;
487         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
488         struct kvec iov[2];
489
490         if (backup_cred(cifs_sb))
491                 create_options |= CREATE_OPEN_BACKUP_INTENT;
492
493         cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
494
495         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
496         if (!utf16_path)
497                 return -ENOMEM;
498
499         oparms.tcon = tcon;
500         oparms.cifs_sb = cifs_sb;
501         oparms.desired_access = GENERIC_WRITE;
502         oparms.create_options = create_options;
503         oparms.disposition = FILE_CREATE;
504         oparms.fid = &fid;
505         oparms.reconnect = false;
506
507         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
508         if (rc) {
509                 kfree(utf16_path);
510                 return rc;
511         }
512
513         io_parms.netfid = fid.netfid;
514         io_parms.pid = current->tgid;
515         io_parms.tcon = tcon;
516         io_parms.offset = 0;
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;
520
521         /* iov[0] is reserved for smb header */
522         iov[1].iov_base = pbuf;
523         iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
524
525         rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
526
527         /* Make sure we wrote all of the symlink data */
528         if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
529                 rc = -EIO;
530
531         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
532
533         kfree(utf16_path);
534         return rc;
535 }
536 #endif /* CONFIG_CIFS_SMB2 */
537
538 /*
539  * M-F Symlink Functions - End
540  */
541
542 int
543 cifs_hardlink(struct dentry *old_file, struct inode *inode,
544               struct dentry *direntry)
545 {
546         int rc = -EACCES;
547         unsigned int xid;
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;
555
556         tlink = cifs_sb_tlink(cifs_sb);
557         if (IS_ERR(tlink))
558                 return PTR_ERR(tlink);
559         tcon = tlink_tcon(tlink);
560
561         xid = get_xid();
562
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)) {
566                 rc = -ENOMEM;
567                 goto cifs_hl_exit;
568         }
569
570         if (tcon->unix_ext)
571                 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
572                                             cifs_sb->local_nls,
573                                             cifs_remap(cifs_sb));
574         else {
575                 server = tcon->ses->server;
576                 if (!server->ops->create_hardlink) {
577                         rc = -ENOSYS;
578                         goto cifs_hl_exit;
579                 }
580                 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
581                                                   cifs_sb);
582                 if ((rc == -EIO) || (rc == -EINVAL))
583                         rc = -EOPNOTSUPP;
584         }
585
586         d_drop(direntry);       /* force new lookup from server of target */
587
588         /*
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
591          */
592         if (d_really_is_positive(old_file)) {
593                 cifsInode = CIFS_I(d_inode(old_file));
594                 if (rc == 0) {
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);
598
599                         /*
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?
604                          */
605                 }
606                 /*
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
610                  * handle it properly
611                  */
612                 cifsInode->time = 0;
613
614                 /*
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
619                  *
620                  * CIFS_I(inode)->time = 0;
621                  */
622         }
623
624 cifs_hl_exit:
625         kfree(from_name);
626         kfree(to_name);
627         free_xid(xid);
628         cifs_put_tlink(tlink);
629         return rc;
630 }
631
632 const char *
633 cifs_get_link(struct dentry *direntry, struct inode *inode,
634               struct delayed_call *done)
635 {
636         int rc = -ENOMEM;
637         unsigned int xid;
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;
644
645         if (!direntry)
646                 return ERR_PTR(-ECHILD);
647
648         xid = get_xid();
649
650         tlink = cifs_sb_tlink(cifs_sb);
651         if (IS_ERR(tlink)) {
652                 free_xid(xid);
653                 return ERR_CAST(tlink);
654         }
655         tcon = tlink_tcon(tlink);
656         server = tcon->ses->server;
657
658         full_path = build_path_from_dentry(direntry);
659         if (!full_path) {
660                 free_xid(xid);
661                 cifs_put_tlink(tlink);
662                 return ERR_PTR(-ENOMEM);
663         }
664
665         cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
666
667         rc = -EACCES;
668         /*
669          * First try Minshall+French Symlinks, if configured
670          * and fallback to UNIX Extensions Symlinks.
671          */
672         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
673                 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
674                                       &target_path);
675
676         if (rc != 0 && server->ops->query_symlink)
677                 rc = server->ops->query_symlink(xid, tcon, full_path,
678                                                 &target_path, cifs_sb);
679
680         kfree(full_path);
681         free_xid(xid);
682         cifs_put_tlink(tlink);
683         if (rc != 0) {
684                 kfree(target_path);
685                 return ERR_PTR(rc);
686         }
687         set_delayed_call(done, kfree_link, target_path);
688         return target_path;
689 }
690
691 int
692 cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
693 {
694         int rc = -EOPNOTSUPP;
695         unsigned int xid;
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;
701
702         xid = get_xid();
703
704         tlink = cifs_sb_tlink(cifs_sb);
705         if (IS_ERR(tlink)) {
706                 rc = PTR_ERR(tlink);
707                 goto symlink_exit;
708         }
709         pTcon = tlink_tcon(tlink);
710
711         full_path = build_path_from_dentry(direntry);
712         if (full_path == NULL) {
713                 rc = -ENOMEM;
714                 goto symlink_exit;
715         }
716
717         cifs_dbg(FYI, "Full path: %s\n", full_path);
718         cifs_dbg(FYI, "symname is %s\n", symname);
719
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,
725                                            cifs_sb->local_nls,
726                                            cifs_remap(cifs_sb));
727         /* else
728            rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
729                                         cifs_sb_target->local_nls); */
730
731         if (rc == 0) {
732                 if (pTcon->unix_ext)
733                         rc = cifs_get_inode_info_unix(&newinode, full_path,
734                                                       inode->i_sb, xid);
735                 else
736                         rc = cifs_get_inode_info(&newinode, full_path, NULL,
737                                                  inode->i_sb, xid, NULL);
738
739                 if (rc != 0) {
740                         cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
741                                  rc);
742                 } else {
743                         d_instantiate(direntry, newinode);
744                 }
745         }
746 symlink_exit:
747         kfree(full_path);
748         cifs_put_tlink(tlink);
749         free_xid(xid);
750         return rc;
751 }