blob: 60b5a11ee11b3d778b7f6e4850a70ac5cd10f293 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/link.c
3 *
Steve French366781c2008-01-25 10:12:41 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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"
Steve French2baa2682014-09-27 02:19:01 -050031#include "cifs_unicode.h"
Steve French5ab97572014-09-15 04:49:28 -050032#include "smb2proto.h"
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020033
Sachin Prabhu0f8dce12013-11-25 17:09:53 +000034/*
35 * M-F Symlink Functions - Begin
36 */
37
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020038#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)
44
45#define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
Rasmus Villemoesc6fc6632016-11-30 23:40:32 +010046#define CIFS_MF_SYMLINK_MD5_FORMAT "%16phN\n"
47#define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) md5_hash
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020048
49static int
Steve French93c100c2011-01-25 19:28:43 +000050symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
51{
52 int rc;
53 unsigned int size;
54 struct crypto_shash *md5;
55 struct sdesc *sdescmd5;
56
57 md5 = crypto_alloc_shash("md5", 0, 0);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060058 if (IS_ERR(md5)) {
Jeff Laytonffeb4142011-01-29 07:03:02 -050059 rc = PTR_ERR(md5);
Joe Perchesf96637b2013-05-04 22:12:25 -050060 cifs_dbg(VFS, "%s: Crypto md5 allocation error %d\n",
61 __func__, rc);
Jeff Laytonffeb4142011-01-29 07:03:02 -050062 return rc;
Steve French93c100c2011-01-25 19:28:43 +000063 }
64 size = sizeof(struct shash_desc) + crypto_shash_descsize(md5);
65 sdescmd5 = kmalloc(size, GFP_KERNEL);
66 if (!sdescmd5) {
67 rc = -ENOMEM;
Steve French93c100c2011-01-25 19:28:43 +000068 goto symlink_hash_err;
69 }
70 sdescmd5->shash.tfm = md5;
71 sdescmd5->shash.flags = 0x0;
72
73 rc = crypto_shash_init(&sdescmd5->shash);
74 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -050075 cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
Steve French93c100c2011-01-25 19:28:43 +000076 goto symlink_hash_err;
77 }
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050078 rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len);
79 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -050080 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050081 goto symlink_hash_err;
82 }
Steve French93c100c2011-01-25 19:28:43 +000083 rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050084 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -050085 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Steve French93c100c2011-01-25 19:28:43 +000086
87symlink_hash_err:
88 crypto_free_shash(md5);
89 kfree(sdescmd5);
90
91 return rc;
92}
93
94static int
Sachin Prabhucb084b12013-11-25 17:09:50 +000095parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
96 char **_link_str)
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020097{
98 int rc;
99 unsigned int link_len;
100 const char *md5_str1;
101 const char *link_str;
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +0200102 u8 md5_hash[16];
103 char md5_str2[34];
104
105 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
106 return -EINVAL;
107
108 md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
109 link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
110
111 rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
112 if (rc != 1)
113 return -EINVAL;
114
Steve French93c100c2011-01-25 19:28:43 +0000115 rc = symlink_hash(link_len, link_str, md5_hash);
116 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500117 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
Steve French93c100c2011-01-25 19:28:43 +0000118 return rc;
119 }
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +0200120
121 snprintf(md5_str2, sizeof(md5_str2),
122 CIFS_MF_SYMLINK_MD5_FORMAT,
123 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
124
125 if (strncmp(md5_str1, md5_str2, 17) != 0)
126 return -EINVAL;
127
128 if (_link_str) {
129 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
130 if (!*_link_str)
131 return -ENOMEM;
132 }
133
134 *_link_len = link_len;
135 return 0;
136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200138static int
Sachin Prabhucb084b12013-11-25 17:09:50 +0000139format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
Stefan Metzmacher18bddd12010-08-03 11:24:22 +0200140{
Steve French93c100c2011-01-25 19:28:43 +0000141 int rc;
Stefan Metzmacher18bddd12010-08-03 11:24:22 +0200142 unsigned int link_len;
143 unsigned int ofs;
Stefan Metzmacher18bddd12010-08-03 11:24:22 +0200144 u8 md5_hash[16];
145
146 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
147 return -EINVAL;
148
149 link_len = strlen(link_str);
150
151 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
152 return -ENAMETOOLONG;
153
Steve French93c100c2011-01-25 19:28:43 +0000154 rc = symlink_hash(link_len, link_str, md5_hash);
155 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500156 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
Steve French93c100c2011-01-25 19:28:43 +0000157 return rc;
158 }
Stefan Metzmacher18bddd12010-08-03 11:24:22 +0200159
160 snprintf(buf, buf_len,
161 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
162 link_len,
163 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
164
165 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
166 memcpy(buf + ofs, link_str, link_len);
167
168 ofs += link_len;
169 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
170 buf[ofs] = '\n';
171 ofs++;
172 }
173
174 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
175 buf[ofs] = ' ';
176 ofs++;
177 }
178
179 return 0;
180}
181
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000182bool
183couldbe_mf_symlink(const struct cifs_fattr *fattr)
184{
Sachin Prabhua9a315d2014-01-31 14:27:16 +0000185 if (!S_ISREG(fattr->cf_mode))
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000186 /* it's not a symlink */
187 return false;
188
189 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
190 /* it's not a symlink */
191 return false;
192
193 return true;
194}
195
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200196static int
Sachin Prabhucb084b12013-11-25 17:09:50 +0000197create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000198 struct cifs_sb_info *cifs_sb, const char *fromName,
199 const char *toName)
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200200{
201 int rc;
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200202 u8 *buf;
203 unsigned int bytes_written = 0;
204
205 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
206 if (!buf)
207 return -ENOMEM;
208
Sachin Prabhucb084b12013-11-25 17:09:50 +0000209 rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000210 if (rc)
211 goto out;
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200212
Steve Frenchda806592014-09-14 23:27:09 -0500213 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);
216 else
217 rc = -EOPNOTSUPP;
218
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000219 if (rc)
220 goto out;
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200221
222 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000223 rc = -EIO;
224out:
225 kfree(buf);
226 return rc;
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200227}
228
229static int
Sachin Prabhucb084b12013-11-25 17:09:50 +0000230query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000231 struct cifs_sb_info *cifs_sb, const unsigned char *path,
232 char **symlinkinfo)
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200233{
234 int rc;
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000235 u8 *buf = NULL;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200236 unsigned int link_len = 0;
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000237 unsigned int bytes_read = 0;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200238
239 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
240 if (!buf)
241 return -ENOMEM;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200242
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000243 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);
246 else
247 rc = -ENOSYS;
248
249 if (rc)
250 goto out;
251
252 if (bytes_read == 0) { /* not a symlink */
253 rc = -EINVAL;
254 goto out;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200255 }
256
Sachin Prabhucb084b12013-11-25 17:09:50 +0000257 rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000258out:
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200259 kfree(buf);
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000260 return rc;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200261}
262
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000263int
264check_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)
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200267{
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000268 int rc;
269 u8 *buf = NULL;
270 unsigned int link_len = 0;
271 unsigned int bytes_read = 0;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200272
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000273 if (!couldbe_mf_symlink(fattr))
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200274 /* it's not a symlink */
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000275 return 0;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200276
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000277 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
278 if (!buf)
279 return -ENOMEM;
280
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);
284 else
285 rc = -ENOSYS;
286
287 if (rc)
288 goto out;
289
290 if (bytes_read == 0) /* not a symlink */
291 goto out;
292
293 rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
294 if (rc == -EINVAL) {
295 /* it's not a symlink */
296 rc = 0;
297 goto out;
298 }
299
300 if (rc != 0)
301 goto out;
302
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;
308out:
309 kfree(buf);
310 return rc;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200311}
312
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000313/*
314 * SMB 1.0 Protocol specific functions
315 */
316
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200317int
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000318cifs_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)
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200321{
322 int rc;
323 int oplock = 0;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400324 struct cifs_fid fid;
325 struct cifs_open_parms oparms;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +0000326 struct cifs_io_parms io_parms;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200327 int buf_type = CIFS_NO_BUFFER;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200328 FILE_ALL_INFO file_info;
329
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400330 oparms.tcon = tcon;
331 oparms.cifs_sb = cifs_sb;
332 oparms.desired_access = GENERIC_READ;
333 oparms.create_options = CREATE_NOT_DIR;
334 oparms.disposition = FILE_OPEN;
335 oparms.path = path;
336 oparms.fid = &fid;
337 oparms.reconnect = false;
338
339 rc = CIFS_open(xid, &oparms, &oplock, &file_info);
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000340 if (rc)
Steve French1b244082013-07-11 19:17:40 -0500341 return rc;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200342
Steve French364d4292014-09-16 06:40:25 -0500343 if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
344 rc = -ENOENT;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200345 /* it's not a symlink */
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000346 goto out;
Steve French364d4292014-09-16 06:40:25 -0500347 }
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200348
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400349 io_parms.netfid = fid.netfid;
Steve French1b244082013-07-11 19:17:40 -0500350 io_parms.pid = current->tgid;
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000351 io_parms.tcon = tcon;
Steve French1b244082013-07-11 19:17:40 -0500352 io_parms.offset = 0;
353 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
354
355 rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000356out:
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400357 CIFSSMBClose(xid, tcon, fid.netfid);
Steve French1b244082013-07-11 19:17:40 -0500358 return rc;
359}
360
Steve French1b244082013-07-11 19:17:40 -0500361int
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000362cifs_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)
365{
366 int rc;
367 int oplock = 0;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400368 struct cifs_fid fid;
369 struct cifs_open_parms oparms;
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000370 struct cifs_io_parms io_parms;
371 int create_options = CREATE_NOT_DIR;
372
373 if (backup_cred(cifs_sb))
374 create_options |= CREATE_OPEN_BACKUP_INTENT;
375
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400376 oparms.tcon = tcon;
377 oparms.cifs_sb = cifs_sb;
378 oparms.desired_access = GENERIC_WRITE;
379 oparms.create_options = create_options;
Björn Baumbacha1d0b84c2014-06-10 12:03:26 +0200380 oparms.disposition = FILE_CREATE;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400381 oparms.path = path;
382 oparms.fid = &fid;
383 oparms.reconnect = false;
384
385 rc = CIFS_open(xid, &oparms, &oplock, NULL);
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000386 if (rc)
387 return rc;
388
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400389 io_parms.netfid = fid.netfid;
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000390 io_parms.pid = current->tgid;
391 io_parms.tcon = tcon;
392 io_parms.offset = 0;
393 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
394
Al Virodbbab322016-09-05 17:53:43 -0400395 rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400396 CIFSSMBClose(xid, tcon, fid.netfid);
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000397 return rc;
398}
399
Steve Frenchc22870e2014-09-16 07:18:19 -0500400/*
401 * SMB 2.1/SMB3 Protocol specific functions
402 */
Steve Frenchc22870e2014-09-16 07:18:19 -0500403int
404smb3_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)
407{
408 int rc;
409 struct cifs_fid fid;
410 struct cifs_open_parms oparms;
411 struct cifs_io_parms io_parms;
412 int buf_type = CIFS_NO_BUFFER;
413 __le16 *utf16_path;
414 __u8 oplock = SMB2_OPLOCK_LEVEL_II;
415 struct smb2_file_all_info *pfile_info = NULL;
416
417 oparms.tcon = tcon;
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;
424 oparms.fid = &fid;
425 oparms.reconnect = false;
426
427 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
428 if (utf16_path == NULL)
429 return -ENOMEM;
430
431 pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
432 GFP_KERNEL);
433
434 if (pfile_info == NULL) {
435 kfree(utf16_path);
436 return -ENOMEM;
437 }
438
439 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL);
440 if (rc)
441 goto qmf_out_open_fail;
442
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? */
446 goto qmf_out;
447 }
448
449 io_parms.netfid = fid.netfid;
450 io_parms.pid = current->tgid;
451 io_parms.tcon = tcon;
452 io_parms.offset = 0;
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);
457qmf_out:
458 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
459qmf_out_open_fail:
460 kfree(utf16_path);
461 kfree(pfile_info);
462 return rc;
463}
464
Steve French5ab97572014-09-15 04:49:28 -0500465int
466smb3_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)
469{
470 int rc;
471 struct cifs_fid fid;
472 struct cifs_open_parms oparms;
473 struct cifs_io_parms io_parms;
474 int create_options = CREATE_NOT_DIR;
475 __le16 *utf16_path;
476 __u8 oplock = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
477 struct kvec iov[2];
478
479 if (backup_cred(cifs_sb))
480 create_options |= CREATE_OPEN_BACKUP_INTENT;
481
482 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
483
484 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
485 if (!utf16_path)
486 return -ENOMEM;
487
488 oparms.tcon = tcon;
489 oparms.cifs_sb = cifs_sb;
490 oparms.desired_access = GENERIC_WRITE;
491 oparms.create_options = create_options;
492 oparms.disposition = FILE_CREATE;
493 oparms.fid = &fid;
494 oparms.reconnect = false;
495
496 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
497 if (rc) {
498 kfree(utf16_path);
499 return rc;
500 }
501
502 io_parms.netfid = fid.netfid;
503 io_parms.pid = current->tgid;
504 io_parms.tcon = tcon;
505 io_parms.offset = 0;
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;
509
510 /* iov[0] is reserved for smb header */
511 iov[1].iov_base = pbuf;
512 iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
513
514 rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
515
516 /* Make sure we wrote all of the symlink data */
517 if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
518 rc = -EIO;
519
520 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
521
522 kfree(utf16_path);
523 return rc;
524}
Steve French5ab97572014-09-15 04:49:28 -0500525
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000526/*
527 * M-F Symlink Functions - End
528 */
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530int
531cifs_hardlink(struct dentry *old_file, struct inode *inode,
532 struct dentry *direntry)
533{
534 int rc = -EACCES;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400535 unsigned int xid;
Steve Frenchd6e906f2012-09-18 16:20:31 -0700536 char *from_name = NULL;
537 char *to_name = NULL;
Jeff Layton7ffec372010-09-29 19:51:11 -0400538 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
539 struct tcon_link *tlink;
Steve Frenchd6e906f2012-09-18 16:20:31 -0700540 struct cifs_tcon *tcon;
541 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct cifsInodeInfo *cifsInode;
543
Jeff Layton7ffec372010-09-29 19:51:11 -0400544 tlink = cifs_sb_tlink(cifs_sb);
545 if (IS_ERR(tlink))
546 return PTR_ERR(tlink);
Steve Frenchd6e906f2012-09-18 16:20:31 -0700547 tcon = tlink_tcon(tlink);
Jeff Layton7ffec372010-09-29 19:51:11 -0400548
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400549 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Steve Frenchd6e906f2012-09-18 16:20:31 -0700551 from_name = build_path_from_dentry(old_file);
552 to_name = build_path_from_dentry(direntry);
553 if ((from_name == NULL) || (to_name == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 rc = -ENOMEM;
555 goto cifs_hl_exit;
556 }
557
Steve Frenchd6e906f2012-09-18 16:20:31 -0700558 if (tcon->unix_ext)
559 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
Jeff Layton7ffec372010-09-29 19:51:11 -0400560 cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -0500561 cifs_remap(cifs_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 else {
Steve Frenchd6e906f2012-09-18 16:20:31 -0700563 server = tcon->ses->server;
Christian Engelmayerabf97672014-01-11 01:57:22 +0100564 if (!server->ops->create_hardlink) {
565 rc = -ENOSYS;
566 goto cifs_hl_exit;
567 }
Steve Frenchd6e906f2012-09-18 16:20:31 -0700568 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
569 cifs_sb);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000570 if ((rc == -EIO) || (rc == -EINVAL))
571 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
Steve French31ec35d2006-11-16 20:54:20 +0000574 d_drop(direntry); /* force new lookup from server of target */
575
Steve Frenchd6e906f2012-09-18 16:20:31 -0700576 /*
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
579 */
David Howells2b0143b2015-03-17 22:25:59 +0000580 if (d_really_is_positive(old_file)) {
581 cifsInode = CIFS_I(d_inode(old_file));
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000582 if (rc == 0) {
David Howells2b0143b2015-03-17 22:25:59 +0000583 spin_lock(&d_inode(old_file)->i_lock);
584 inc_nlink(d_inode(old_file));
585 spin_unlock(&d_inode(old_file)->i_lock);
Steve Frenchff273cb2014-10-17 17:17:12 -0500586
Steve Frenchd6e906f2012-09-18 16:20:31 -0700587 /*
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?
592 */
Steve French31ec35d2006-11-16 20:54:20 +0000593 }
Steve Frenchd6e906f2012-09-18 16:20:31 -0700594 /*
595 * if not oplocked will force revalidate to get info on source
Steve Frenchff273cb2014-10-17 17:17:12 -0500596 * file from srv. Note Samba server prior to 4.2 has bug -
597 * not updating src file ctime on hardlinks but Windows servers
598 * handle it properly
Steve Frenchd6e906f2012-09-18 16:20:31 -0700599 */
Steve French31ec35d2006-11-16 20:54:20 +0000600 cifsInode->time = 0;
601
Steve Frenchd6e906f2012-09-18 16:20:31 -0700602 /*
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
607 *
608 * CIFS_I(inode)->time = 0;
609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612cifs_hl_exit:
Steve Frenchd6e906f2012-09-18 16:20:31 -0700613 kfree(from_name);
614 kfree(to_name);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400615 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400616 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return rc;
618}
619
Al Viro680baac2015-05-02 13:32:22 -0400620const char *
Al Virofceef392015-12-29 15:58:39 -0500621cifs_get_link(struct dentry *direntry, struct inode *inode,
622 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Jeff Layton8b6427a2009-05-19 09:57:03 -0400624 int rc = -ENOMEM;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400625 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 char *full_path = NULL;
Jeff Layton8b6427a2009-05-19 09:57:03 -0400627 char *target_path = NULL;
628 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -0400629 struct tcon_link *tlink = NULL;
Steve French96daf2b2011-05-27 04:34:02 +0000630 struct cifs_tcon *tcon;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400631 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Al Viro6b255392015-11-17 10:20:54 -0500633 if (!direntry)
634 return ERR_PTR(-ECHILD);
635
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400636 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Jeff Layton7ffec372010-09-29 19:51:11 -0400638 tlink = cifs_sb_tlink(cifs_sb);
639 if (IS_ERR(tlink)) {
Al Viro680baac2015-05-02 13:32:22 -0400640 free_xid(xid);
641 return ERR_CAST(tlink);
Jeff Layton7ffec372010-09-29 19:51:11 -0400642 }
643 tcon = tlink_tcon(tlink);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400644 server = tcon->ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Jeff Layton8b6427a2009-05-19 09:57:03 -0400646 full_path = build_path_from_dentry(direntry);
Al Viro680baac2015-05-02 13:32:22 -0400647 if (!full_path) {
648 free_xid(xid);
649 cifs_put_tlink(tlink);
650 return ERR_PTR(-ENOMEM);
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Joe Perchesf96637b2013-05-04 22:12:25 -0500653 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200655 rc = -EACCES;
656 /*
657 * First try Minshall+French Symlinks, if configured
658 * and fallback to UNIX Extensions Symlinks.
659 */
660 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000661 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
662 &target_path);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200663
Sachin Prabhu0ecdb4f2013-11-27 13:27:12 +0000664 if (rc != 0 && server->ops->query_symlink)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400665 rc = server->ops->query_symlink(xid, tcon, full_path,
666 &target_path, cifs_sb);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200667
Jeff Layton8b6427a2009-05-19 09:57:03 -0400668 kfree(full_path);
Al Viro680baac2015-05-02 13:32:22 -0400669 free_xid(xid);
670 cifs_put_tlink(tlink);
Jeff Layton460b9692009-04-30 07:17:56 -0400671 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 kfree(target_path);
Al Viro680baac2015-05-02 13:32:22 -0400673 return ERR_PTR(rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Al Virofceef392015-12-29 15:58:39 -0500675 set_delayed_call(done, kfree_link, target_path);
676 return target_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679int
680cifs_symlink(struct inode *inode, struct dentry *direntry, const char *symname)
681{
682 int rc = -EOPNOTSUPP;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400683 unsigned int xid;
Jeff Layton7ffec372010-09-29 19:51:11 -0400684 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
685 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +0000686 struct cifs_tcon *pTcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 char *full_path = NULL;
688 struct inode *newinode = NULL;
689
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400690 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Jeff Layton7ffec372010-09-29 19:51:11 -0400692 tlink = cifs_sb_tlink(cifs_sb);
693 if (IS_ERR(tlink)) {
694 rc = PTR_ERR(tlink);
695 goto symlink_exit;
696 }
697 pTcon = tlink_tcon(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Steve French7f573562005-08-30 11:32:14 -0700699 full_path = build_path_from_dentry(direntry);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000700 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +0530701 rc = -ENOMEM;
Jeff Layton7ffec372010-09-29 19:51:11 -0400702 goto symlink_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Joe Perchesf96637b2013-05-04 22:12:25 -0500705 cifs_dbg(FYI, "Full path: %s\n", full_path);
706 cifs_dbg(FYI, "symname is %s\n", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /* BB what if DFS and this volume is on different share? BB */
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200709 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000710 rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200711 else if (pTcon->unix_ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +0900713 cifs_sb->local_nls,
714 cifs_remap(cifs_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* else
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000716 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
717 cifs_sb_target->local_nls); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (rc == 0) {
Steve Frenchc18c8422007-07-18 23:21:09 +0000720 if (pTcon->unix_ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 rc = cifs_get_inode_info_unix(&newinode, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000722 inode->i_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 else
724 rc = cifs_get_inode_info(&newinode, full_path, NULL,
Steve French8b1327f2008-03-14 22:37:16 +0000725 inode->i_sb, xid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (rc != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500728 cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
729 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 d_instantiate(direntry, newinode);
732 }
733 }
Jeff Layton7ffec372010-09-29 19:51:11 -0400734symlink_exit:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800735 kfree(full_path);
Jeff Layton7ffec372010-09-29 19:51:11 -0400736 cifs_put_tlink(tlink);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400737 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return rc;
739}