blob: 852e54ee82c28279c00e6013b19916f3815a8b70 [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8#include <linux/fs.h>
9#include <linux/stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/namei.h>
12#include "cifsfs.h"
13#include "cifspdu.h"
14#include "cifsglob.h"
15#include "cifsproto.h"
16#include "cifs_debug.h"
17#include "cifs_fs_sb.h"
Steve French2baa2682014-09-27 02:19:01 -050018#include "cifs_unicode.h"
Steve French5ab97572014-09-15 04:49:28 -050019#include "smb2proto.h"
Steve French087f7572021-04-29 00:18:43 -050020#include "cifs_ioctl.h"
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020021
Sachin Prabhu0f8dce12013-11-25 17:09:53 +000022/*
23 * M-F Symlink Functions - Begin
24 */
25
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020026#define CIFS_MF_SYMLINK_LEN_OFFSET (4+1)
27#define CIFS_MF_SYMLINK_MD5_OFFSET (CIFS_MF_SYMLINK_LEN_OFFSET+(4+1))
28#define CIFS_MF_SYMLINK_LINK_OFFSET (CIFS_MF_SYMLINK_MD5_OFFSET+(32+1))
29#define CIFS_MF_SYMLINK_LINK_MAXLEN (1024)
30#define CIFS_MF_SYMLINK_FILE_SIZE \
31 (CIFS_MF_SYMLINK_LINK_OFFSET + CIFS_MF_SYMLINK_LINK_MAXLEN)
32
33#define CIFS_MF_SYMLINK_LEN_FORMAT "XSym\n%04u\n"
Rasmus Villemoesc6fc6632016-11-30 23:40:32 +010034#define CIFS_MF_SYMLINK_MD5_FORMAT "%16phN\n"
35#define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) md5_hash
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020036
37static int
Steve French93c100c2011-01-25 19:28:43 +000038symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
39{
40 int rc;
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010041 struct crypto_shash *md5 = NULL;
42 struct sdesc *sdescmd5 = NULL;
Steve French93c100c2011-01-25 19:28:43 +000043
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010044 rc = cifs_alloc_hash("md5", &md5, &sdescmd5);
45 if (rc)
Steve French93c100c2011-01-25 19:28:43 +000046 goto symlink_hash_err;
Steve French93c100c2011-01-25 19:28:43 +000047
48 rc = crypto_shash_init(&sdescmd5->shash);
49 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -050050 cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
Steve French93c100c2011-01-25 19:28:43 +000051 goto symlink_hash_err;
52 }
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050053 rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len);
54 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -050055 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050056 goto symlink_hash_err;
57 }
Steve French93c100c2011-01-25 19:28:43 +000058 rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050059 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -050060 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
Steve French93c100c2011-01-25 19:28:43 +000061
62symlink_hash_err:
Aurelien Aptel82fb82b2018-02-16 19:19:27 +010063 cifs_free_hash(&md5, &sdescmd5);
Steve French93c100c2011-01-25 19:28:43 +000064 return rc;
65}
66
67static int
Sachin Prabhucb084b12013-11-25 17:09:50 +000068parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
69 char **_link_str)
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020070{
71 int rc;
72 unsigned int link_len;
73 const char *md5_str1;
74 const char *link_str;
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020075 u8 md5_hash[16];
76 char md5_str2[34];
77
78 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
79 return -EINVAL;
80
81 md5_str1 = (const char *)&buf[CIFS_MF_SYMLINK_MD5_OFFSET];
82 link_str = (const char *)&buf[CIFS_MF_SYMLINK_LINK_OFFSET];
83
84 rc = sscanf(buf, CIFS_MF_SYMLINK_LEN_FORMAT, &link_len);
85 if (rc != 1)
86 return -EINVAL;
87
Steve French93c100c2011-01-25 19:28:43 +000088 rc = symlink_hash(link_len, link_str, md5_hash);
89 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -050090 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
Steve French93c100c2011-01-25 19:28:43 +000091 return rc;
92 }
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020093
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +100094 scnprintf(md5_str2, sizeof(md5_str2),
95 CIFS_MF_SYMLINK_MD5_FORMAT,
96 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
Stefan Metzmacherc69c1b62010-07-31 09:14:16 +020097
98 if (strncmp(md5_str1, md5_str2, 17) != 0)
99 return -EINVAL;
100
101 if (_link_str) {
102 *_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
103 if (!*_link_str)
104 return -ENOMEM;
105 }
106
107 *_link_len = link_len;
108 return 0;
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200111static int
Sachin Prabhucb084b12013-11-25 17:09:50 +0000112format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
Stefan Metzmacher18bddd12010-08-03 11:24:22 +0200113{
Steve French93c100c2011-01-25 19:28:43 +0000114 int rc;
Stefan Metzmacher18bddd12010-08-03 11:24:22 +0200115 unsigned int link_len;
116 unsigned int ofs;
Stefan Metzmacher18bddd12010-08-03 11:24:22 +0200117 u8 md5_hash[16];
118
119 if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
120 return -EINVAL;
121
122 link_len = strlen(link_str);
123
124 if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
125 return -ENAMETOOLONG;
126
Steve French93c100c2011-01-25 19:28:43 +0000127 rc = symlink_hash(link_len, link_str, md5_hash);
128 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500129 cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
Steve French93c100c2011-01-25 19:28:43 +0000130 return rc;
131 }
Stefan Metzmacher18bddd12010-08-03 11:24:22 +0200132
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000133 scnprintf(buf, buf_len,
134 CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
135 link_len,
136 CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
Stefan Metzmacher18bddd12010-08-03 11:24:22 +0200137
138 ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
139 memcpy(buf + ofs, link_str, link_len);
140
141 ofs += link_len;
142 if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
143 buf[ofs] = '\n';
144 ofs++;
145 }
146
147 while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
148 buf[ofs] = ' ';
149 ofs++;
150 }
151
152 return 0;
153}
154
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000155bool
156couldbe_mf_symlink(const struct cifs_fattr *fattr)
157{
Sachin Prabhua9a315d2014-01-31 14:27:16 +0000158 if (!S_ISREG(fattr->cf_mode))
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000159 /* it's not a symlink */
160 return false;
161
162 if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
163 /* it's not a symlink */
164 return false;
165
166 return true;
167}
168
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200169static int
Sachin Prabhucb084b12013-11-25 17:09:50 +0000170create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000171 struct cifs_sb_info *cifs_sb, const char *fromName,
172 const char *toName)
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200173{
174 int rc;
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200175 u8 *buf;
176 unsigned int bytes_written = 0;
177
178 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
179 if (!buf)
180 return -ENOMEM;
181
Sachin Prabhucb084b12013-11-25 17:09:50 +0000182 rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000183 if (rc)
184 goto out;
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200185
Steve Frenchda806592014-09-14 23:27:09 -0500186 if (tcon->ses->server->ops->create_mf_symlink)
187 rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
188 cifs_sb, fromName, buf, &bytes_written);
189 else
190 rc = -EOPNOTSUPP;
191
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000192 if (rc)
193 goto out;
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200194
195 if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000196 rc = -EIO;
197out:
198 kfree(buf);
199 return rc;
Stefan Metzmacher8713d012010-08-05 21:15:22 +0200200}
201
202static int
Sachin Prabhucb084b12013-11-25 17:09:50 +0000203query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000204 struct cifs_sb_info *cifs_sb, const unsigned char *path,
205 char **symlinkinfo)
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200206{
207 int rc;
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000208 u8 *buf = NULL;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200209 unsigned int link_len = 0;
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000210 unsigned int bytes_read = 0;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200211
212 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
213 if (!buf)
214 return -ENOMEM;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200215
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000216 if (tcon->ses->server->ops->query_mf_symlink)
217 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
218 cifs_sb, path, buf, &bytes_read);
219 else
220 rc = -ENOSYS;
221
222 if (rc)
223 goto out;
224
225 if (bytes_read == 0) { /* not a symlink */
226 rc = -EINVAL;
227 goto out;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200228 }
229
Sachin Prabhucb084b12013-11-25 17:09:50 +0000230 rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000231out:
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200232 kfree(buf);
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000233 return rc;
Stefan Metzmacher0fd43ae2010-08-05 21:13:44 +0200234}
235
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000236int
237check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
238 struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
239 const unsigned char *path)
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200240{
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000241 int rc;
242 u8 *buf = NULL;
243 unsigned int link_len = 0;
244 unsigned int bytes_read = 0;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200245
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000246 if (!couldbe_mf_symlink(fattr))
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200247 /* it's not a symlink */
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000248 return 0;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200249
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000250 buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
251 if (!buf)
252 return -ENOMEM;
253
254 if (tcon->ses->server->ops->query_mf_symlink)
255 rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
256 cifs_sb, path, buf, &bytes_read);
257 else
258 rc = -ENOSYS;
259
260 if (rc)
261 goto out;
262
263 if (bytes_read == 0) /* not a symlink */
264 goto out;
265
266 rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
267 if (rc == -EINVAL) {
268 /* it's not a symlink */
269 rc = 0;
270 goto out;
271 }
272
273 if (rc != 0)
274 goto out;
275
276 /* it is a symlink */
277 fattr->cf_eof = link_len;
278 fattr->cf_mode &= ~S_IFMT;
279 fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
280 fattr->cf_dtype = DT_LNK;
281out:
282 kfree(buf);
283 return rc;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200284}
285
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000286/*
287 * SMB 1.0 Protocol specific functions
288 */
289
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200290int
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000291cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
292 struct cifs_sb_info *cifs_sb, const unsigned char *path,
293 char *pbuf, unsigned int *pbytes_read)
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200294{
295 int rc;
296 int oplock = 0;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400297 struct cifs_fid fid;
298 struct cifs_open_parms oparms;
Aurelien Aptel7c065142020-06-04 17:23:55 +0200299 struct cifs_io_parms io_parms = {0};
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200300 int buf_type = CIFS_NO_BUFFER;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200301 FILE_ALL_INFO file_info;
302
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400303 oparms.tcon = tcon;
304 oparms.cifs_sb = cifs_sb;
305 oparms.desired_access = GENERIC_READ;
Amir Goldstein0f060932020-02-03 21:46:43 +0200306 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400307 oparms.disposition = FILE_OPEN;
308 oparms.path = path;
309 oparms.fid = &fid;
310 oparms.reconnect = false;
311
312 rc = CIFS_open(xid, &oparms, &oplock, &file_info);
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000313 if (rc)
Steve French1b244082013-07-11 19:17:40 -0500314 return rc;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200315
Steve French364d4292014-09-16 06:40:25 -0500316 if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
317 rc = -ENOENT;
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200318 /* it's not a symlink */
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000319 goto out;
Steve French364d4292014-09-16 06:40:25 -0500320 }
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200321
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400322 io_parms.netfid = fid.netfid;
Steve French1b244082013-07-11 19:17:40 -0500323 io_parms.pid = current->tgid;
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000324 io_parms.tcon = tcon;
Steve French1b244082013-07-11 19:17:40 -0500325 io_parms.offset = 0;
326 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
327
328 rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
Sachin Prabhub5be1a12013-11-25 17:09:49 +0000329out:
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400330 CIFSSMBClose(xid, tcon, fid.netfid);
Steve French1b244082013-07-11 19:17:40 -0500331 return rc;
332}
333
Steve French1b244082013-07-11 19:17:40 -0500334int
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000335cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
336 struct cifs_sb_info *cifs_sb, const unsigned char *path,
337 char *pbuf, unsigned int *pbytes_written)
338{
339 int rc;
340 int oplock = 0;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400341 struct cifs_fid fid;
342 struct cifs_open_parms oparms;
Aurelien Aptel7c065142020-06-04 17:23:55 +0200343 struct cifs_io_parms io_parms = {0};
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000344
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400345 oparms.tcon = tcon;
346 oparms.cifs_sb = cifs_sb;
347 oparms.desired_access = GENERIC_WRITE;
Amir Goldstein0f060932020-02-03 21:46:43 +0200348 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
Björn Baumbacha1d0b84c2014-06-10 12:03:26 +0200349 oparms.disposition = FILE_CREATE;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400350 oparms.path = path;
351 oparms.fid = &fid;
352 oparms.reconnect = false;
353
354 rc = CIFS_open(xid, &oparms, &oplock, NULL);
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000355 if (rc)
356 return rc;
357
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400358 io_parms.netfid = fid.netfid;
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000359 io_parms.pid = current->tgid;
360 io_parms.tcon = tcon;
361 io_parms.offset = 0;
362 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
363
Al Virodbbab322016-09-05 17:53:43 -0400364 rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400365 CIFSSMBClose(xid, tcon, fid.netfid);
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000366 return rc;
367}
368
Steve Frenchc22870e2014-09-16 07:18:19 -0500369/*
370 * SMB 2.1/SMB3 Protocol specific functions
371 */
Steve Frenchc22870e2014-09-16 07:18:19 -0500372int
373smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
374 struct cifs_sb_info *cifs_sb, const unsigned char *path,
375 char *pbuf, unsigned int *pbytes_read)
376{
377 int rc;
378 struct cifs_fid fid;
379 struct cifs_open_parms oparms;
Aurelien Aptel7c065142020-06-04 17:23:55 +0200380 struct cifs_io_parms io_parms = {0};
Steve Frenchc22870e2014-09-16 07:18:19 -0500381 int buf_type = CIFS_NO_BUFFER;
382 __le16 *utf16_path;
Steve French22783152018-07-27 22:01:49 -0500383 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Steve Frenchc22870e2014-09-16 07:18:19 -0500384 struct smb2_file_all_info *pfile_info = NULL;
385
386 oparms.tcon = tcon;
387 oparms.cifs_sb = cifs_sb;
388 oparms.desired_access = GENERIC_READ;
Amir Goldstein0f060932020-02-03 21:46:43 +0200389 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
Steve Frenchc22870e2014-09-16 07:18:19 -0500390 oparms.disposition = FILE_OPEN;
391 oparms.fid = &fid;
392 oparms.reconnect = false;
393
394 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
395 if (utf16_path == NULL)
396 return -ENOMEM;
397
398 pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
399 GFP_KERNEL);
400
401 if (pfile_info == NULL) {
402 kfree(utf16_path);
403 return -ENOMEM;
404 }
405
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000406 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL,
Aurelien Aptel69dda302020-03-02 17:53:22 +0100407 NULL, NULL);
Steve Frenchc22870e2014-09-16 07:18:19 -0500408 if (rc)
409 goto qmf_out_open_fail;
410
411 if (pfile_info->EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
412 /* it's not a symlink */
413 rc = -ENOENT; /* Is there a better rc to return? */
414 goto qmf_out;
415 }
416
417 io_parms.netfid = fid.netfid;
418 io_parms.pid = current->tgid;
419 io_parms.tcon = tcon;
420 io_parms.offset = 0;
421 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
422 io_parms.persistent_fid = fid.persistent_fid;
423 io_parms.volatile_fid = fid.volatile_fid;
424 rc = SMB2_read(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
425qmf_out:
426 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
427qmf_out_open_fail:
428 kfree(utf16_path);
429 kfree(pfile_info);
430 return rc;
431}
432
Steve French5ab97572014-09-15 04:49:28 -0500433int
434smb3_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
435 struct cifs_sb_info *cifs_sb, const unsigned char *path,
436 char *pbuf, unsigned int *pbytes_written)
437{
438 int rc;
439 struct cifs_fid fid;
440 struct cifs_open_parms oparms;
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500441 struct cifs_io_parms io_parms = {0};
Steve French5ab97572014-09-15 04:49:28 -0500442 __le16 *utf16_path;
Steve French22783152018-07-27 22:01:49 -0500443 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Steve French5ab97572014-09-15 04:49:28 -0500444 struct kvec iov[2];
445
Steve French5ab97572014-09-15 04:49:28 -0500446 cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
447
448 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
449 if (!utf16_path)
450 return -ENOMEM;
451
452 oparms.tcon = tcon;
453 oparms.cifs_sb = cifs_sb;
454 oparms.desired_access = GENERIC_WRITE;
Amir Goldstein0f060932020-02-03 21:46:43 +0200455 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
Steve French5ab97572014-09-15 04:49:28 -0500456 oparms.disposition = FILE_CREATE;
457 oparms.fid = &fid;
458 oparms.reconnect = false;
459
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000460 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
Aurelien Aptel69dda302020-03-02 17:53:22 +0100461 NULL, NULL);
Steve French5ab97572014-09-15 04:49:28 -0500462 if (rc) {
463 kfree(utf16_path);
464 return rc;
465 }
466
467 io_parms.netfid = fid.netfid;
468 io_parms.pid = current->tgid;
469 io_parms.tcon = tcon;
470 io_parms.offset = 0;
471 io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
472 io_parms.persistent_fid = fid.persistent_fid;
473 io_parms.volatile_fid = fid.volatile_fid;
474
475 /* iov[0] is reserved for smb header */
476 iov[1].iov_base = pbuf;
477 iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
478
479 rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
480
481 /* Make sure we wrote all of the symlink data */
482 if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
483 rc = -EIO;
484
485 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
486
487 kfree(utf16_path);
488 return rc;
489}
Steve French5ab97572014-09-15 04:49:28 -0500490
Sachin Prabhu0f8dce12013-11-25 17:09:53 +0000491/*
492 * M-F Symlink Functions - End
493 */
Stefan Metzmacher8bfb50a2010-07-31 09:15:10 +0200494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495int
496cifs_hardlink(struct dentry *old_file, struct inode *inode,
497 struct dentry *direntry)
498{
499 int rc = -EACCES;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400500 unsigned int xid;
Al Virof6a9bc32021-03-05 17:36:04 -0500501 const char *from_name, *to_name;
502 void *page1, *page2;
Jeff Layton7ffec372010-09-29 19:51:11 -0400503 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
504 struct tcon_link *tlink;
Steve Frenchd6e906f2012-09-18 16:20:31 -0700505 struct cifs_tcon *tcon;
506 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct cifsInodeInfo *cifsInode;
508
Steve French087f7572021-04-29 00:18:43 -0500509 if (unlikely(cifs_forced_shutdown(cifs_sb)))
510 return -EIO;
511
Jeff Layton7ffec372010-09-29 19:51:11 -0400512 tlink = cifs_sb_tlink(cifs_sb);
513 if (IS_ERR(tlink))
514 return PTR_ERR(tlink);
Steve Frenchd6e906f2012-09-18 16:20:31 -0700515 tcon = tlink_tcon(tlink);
Jeff Layton7ffec372010-09-29 19:51:11 -0400516
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400517 xid = get_xid();
Al Virof6a9bc32021-03-05 17:36:04 -0500518 page1 = alloc_dentry_path();
519 page2 = alloc_dentry_path();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Al Virof6a9bc32021-03-05 17:36:04 -0500521 from_name = build_path_from_dentry(old_file, page1);
522 if (IS_ERR(from_name)) {
523 rc = PTR_ERR(from_name);
524 goto cifs_hl_exit;
525 }
526 to_name = build_path_from_dentry(direntry, page2);
527 if (IS_ERR(to_name)) {
528 rc = PTR_ERR(to_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto cifs_hl_exit;
530 }
531
Steve Frenchd6e906f2012-09-18 16:20:31 -0700532 if (tcon->unix_ext)
533 rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
Jeff Layton7ffec372010-09-29 19:51:11 -0400534 cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -0500535 cifs_remap(cifs_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 else {
Steve Frenchd6e906f2012-09-18 16:20:31 -0700537 server = tcon->ses->server;
Christian Engelmayerabf97672014-01-11 01:57:22 +0100538 if (!server->ops->create_hardlink) {
539 rc = -ENOSYS;
540 goto cifs_hl_exit;
541 }
Steve Frenchd6e906f2012-09-18 16:20:31 -0700542 rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
543 cifs_sb);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000544 if ((rc == -EIO) || (rc == -EINVAL))
545 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
Steve French31ec35d2006-11-16 20:54:20 +0000548 d_drop(direntry); /* force new lookup from server of target */
549
Steve Frenchd6e906f2012-09-18 16:20:31 -0700550 /*
551 * if source file is cached (oplocked) revalidate will not go to server
552 * until the file is closed or oplock broken so update nlinks locally
553 */
David Howells2b0143b2015-03-17 22:25:59 +0000554 if (d_really_is_positive(old_file)) {
555 cifsInode = CIFS_I(d_inode(old_file));
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000556 if (rc == 0) {
David Howells2b0143b2015-03-17 22:25:59 +0000557 spin_lock(&d_inode(old_file)->i_lock);
558 inc_nlink(d_inode(old_file));
559 spin_unlock(&d_inode(old_file)->i_lock);
Steve Frenchff273cb2014-10-17 17:17:12 -0500560
Steve Frenchd6e906f2012-09-18 16:20:31 -0700561 /*
562 * parent dir timestamps will update from srv within a
563 * second, would it really be worth it to set the parent
564 * dir cifs inode time to zero to force revalidate
565 * (faster) for it too?
566 */
Steve French31ec35d2006-11-16 20:54:20 +0000567 }
Steve Frenchd6e906f2012-09-18 16:20:31 -0700568 /*
569 * if not oplocked will force revalidate to get info on source
Steve Frenchff273cb2014-10-17 17:17:12 -0500570 * file from srv. Note Samba server prior to 4.2 has bug -
571 * not updating src file ctime on hardlinks but Windows servers
572 * handle it properly
Steve Frenchd6e906f2012-09-18 16:20:31 -0700573 */
Steve French31ec35d2006-11-16 20:54:20 +0000574 cifsInode->time = 0;
575
Steve Frenchd6e906f2012-09-18 16:20:31 -0700576 /*
577 * Will update parent dir timestamps from srv within a second.
578 * Would it really be worth it to set the parent dir (cifs
579 * inode) time field to zero to force revalidate on parent
580 * directory faster ie
581 *
582 * CIFS_I(inode)->time = 0;
583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586cifs_hl_exit:
Al Virof6a9bc32021-03-05 17:36:04 -0500587 free_dentry_path(page1);
588 free_dentry_path(page2);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400589 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400590 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return rc;
592}
593
Al Viro680baac2015-05-02 13:32:22 -0400594const char *
Al Virofceef392015-12-29 15:58:39 -0500595cifs_get_link(struct dentry *direntry, struct inode *inode,
596 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Jeff Layton8b6427a2009-05-19 09:57:03 -0400598 int rc = -ENOMEM;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400599 unsigned int xid;
Al Virof6a9bc32021-03-05 17:36:04 -0500600 const char *full_path;
601 void *page;
Jeff Layton8b6427a2009-05-19 09:57:03 -0400602 char *target_path = NULL;
603 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -0400604 struct tcon_link *tlink = NULL;
Steve French96daf2b2011-05-27 04:34:02 +0000605 struct cifs_tcon *tcon;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400606 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Al Viro6b255392015-11-17 10:20:54 -0500608 if (!direntry)
609 return ERR_PTR(-ECHILD);
610
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400611 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Jeff Layton7ffec372010-09-29 19:51:11 -0400613 tlink = cifs_sb_tlink(cifs_sb);
614 if (IS_ERR(tlink)) {
Al Viro680baac2015-05-02 13:32:22 -0400615 free_xid(xid);
616 return ERR_CAST(tlink);
Jeff Layton7ffec372010-09-29 19:51:11 -0400617 }
618 tcon = tlink_tcon(tlink);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400619 server = tcon->ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Al Virof6a9bc32021-03-05 17:36:04 -0500621 page = alloc_dentry_path();
622 full_path = build_path_from_dentry(direntry, page);
623 if (IS_ERR(full_path)) {
Al Viro680baac2015-05-02 13:32:22 -0400624 free_xid(xid);
625 cifs_put_tlink(tlink);
Al Virof6a9bc32021-03-05 17:36:04 -0500626 free_dentry_path(page);
627 return ERR_CAST(full_path);
Al Viro680baac2015-05-02 13:32:22 -0400628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Joe Perchesf96637b2013-05-04 22:12:25 -0500630 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200632 rc = -EACCES;
633 /*
634 * First try Minshall+French Symlinks, if configured
635 * and fallback to UNIX Extensions Symlinks.
636 */
637 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
Sachin Prabhu8205d1b2013-11-25 17:09:51 +0000638 rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
639 &target_path);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200640
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +1000641 if (rc != 0 && server->ops->query_symlink) {
642 struct cifsInodeInfo *cifsi = CIFS_I(inode);
643 bool reparse_point = false;
644
645 if (cifsi->cifsAttrs & ATTR_REPARSE)
646 reparse_point = true;
647
648 rc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
649 &target_path, reparse_point);
650 }
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200651
Al Virof6a9bc32021-03-05 17:36:04 -0500652 free_dentry_path(page);
Al Viro680baac2015-05-02 13:32:22 -0400653 free_xid(xid);
654 cifs_put_tlink(tlink);
Jeff Layton460b9692009-04-30 07:17:56 -0400655 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 kfree(target_path);
Al Viro680baac2015-05-02 13:32:22 -0400657 return ERR_PTR(rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Al Virofceef392015-12-29 15:58:39 -0500659 set_delayed_call(done, kfree_link, target_path);
660 return target_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663int
Christian Brauner549c7292021-01-21 14:19:43 +0100664cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode,
665 struct dentry *direntry, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 int rc = -EOPNOTSUPP;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400668 unsigned int xid;
Jeff Layton7ffec372010-09-29 19:51:11 -0400669 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
670 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +0000671 struct cifs_tcon *pTcon;
Al Virof6a9bc32021-03-05 17:36:04 -0500672 const char *full_path;
Khaled ROMDHANIbae4c0c2021-05-04 16:38:55 +0100673 void *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct inode *newinode = NULL;
675
Steve French087f7572021-04-29 00:18:43 -0500676 if (unlikely(cifs_forced_shutdown(cifs_sb)))
677 return -EIO;
678
Khaled ROMDHANIbae4c0c2021-05-04 16:38:55 +0100679 page = alloc_dentry_path();
680 if (!page)
681 return -ENOMEM;
682
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400683 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Jeff Layton7ffec372010-09-29 19:51:11 -0400685 tlink = cifs_sb_tlink(cifs_sb);
686 if (IS_ERR(tlink)) {
687 rc = PTR_ERR(tlink);
688 goto symlink_exit;
689 }
690 pTcon = tlink_tcon(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Al Virof6a9bc32021-03-05 17:36:04 -0500692 full_path = build_path_from_dentry(direntry, page);
693 if (IS_ERR(full_path)) {
694 rc = PTR_ERR(full_path);
Jeff Layton7ffec372010-09-29 19:51:11 -0400695 goto symlink_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Joe Perchesf96637b2013-05-04 22:12:25 -0500698 cifs_dbg(FYI, "Full path: %s\n", full_path);
699 cifs_dbg(FYI, "symname is %s\n", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /* BB what if DFS and this volume is on different share? BB */
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200702 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
Sachin Prabhucbb0aba2013-11-25 17:09:52 +0000703 rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200704 else if (pTcon->unix_ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +0900706 cifs_sb->local_nls,
707 cifs_remap(cifs_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* else
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000709 rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
710 cifs_sb_target->local_nls); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (rc == 0) {
Steve Frenchd3138522020-06-11 22:43:01 -0500713 if (pTcon->posix_extensions)
714 rc = smb311_posix_get_inode_info(&newinode, full_path, inode->i_sb, xid);
715 else if (pTcon->unix_ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 rc = cifs_get_inode_info_unix(&newinode, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000717 inode->i_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 else
719 rc = cifs_get_inode_info(&newinode, full_path, NULL,
Steve French8b1327f2008-03-14 22:37:16 +0000720 inode->i_sb, xid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (rc != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500723 cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
724 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 d_instantiate(direntry, newinode);
727 }
728 }
Jeff Layton7ffec372010-09-29 19:51:11 -0400729symlink_exit:
Al Virof6a9bc32021-03-05 17:36:04 -0500730 free_dentry_path(page);
Jeff Layton7ffec372010-09-29 19:51:11 -0400731 cifs_put_tlink(tlink);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400732 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return rc;
734}