• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  */
8 #include <linux/fs.h>
9 #include <linux/stat.h>
10 #include <linux/slab.h>
11 #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"
18 #include "cifs_unicode.h"
19 #include "smb2proto.h"
20 #include "cifs_ioctl.h"
21 
22 /*
23  * M-F Symlink Functions - Begin
24  */
25 
26 #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"
34 #define CIFS_MF_SYMLINK_MD5_FORMAT "%16phN\n"
35 #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) md5_hash
36 
37 static int
symlink_hash(unsigned int link_len,const char * link_str,u8 * md5_hash)38 symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash)
39 {
40 	int rc;
41 	struct crypto_shash *md5 = NULL;
42 	struct sdesc *sdescmd5 = NULL;
43 
44 	rc = cifs_alloc_hash("md5", &md5, &sdescmd5);
45 	if (rc)
46 		goto symlink_hash_err;
47 
48 	rc = crypto_shash_init(&sdescmd5->shash);
49 	if (rc) {
50 		cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__);
51 		goto symlink_hash_err;
52 	}
53 	rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len);
54 	if (rc) {
55 		cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
56 		goto symlink_hash_err;
57 	}
58 	rc = crypto_shash_final(&sdescmd5->shash, md5_hash);
59 	if (rc)
60 		cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
61 
62 symlink_hash_err:
63 	cifs_free_hash(&md5, &sdescmd5);
64 	return rc;
65 }
66 
67 static int
parse_mf_symlink(const u8 * buf,unsigned int buf_len,unsigned int * _link_len,char ** _link_str)68 parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len,
69 		 char **_link_str)
70 {
71 	int rc;
72 	unsigned int link_len;
73 	const char *md5_str1;
74 	const char *link_str;
75 	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 
88 	if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
89 		return -EINVAL;
90 
91 	rc = symlink_hash(link_len, link_str, md5_hash);
92 	if (rc) {
93 		cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
94 		return rc;
95 	}
96 
97 	scnprintf(md5_str2, sizeof(md5_str2),
98 		  CIFS_MF_SYMLINK_MD5_FORMAT,
99 		  CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
100 
101 	if (strncmp(md5_str1, md5_str2, 17) != 0)
102 		return -EINVAL;
103 
104 	if (_link_str) {
105 		*_link_str = kstrndup(link_str, link_len, GFP_KERNEL);
106 		if (!*_link_str)
107 			return -ENOMEM;
108 	}
109 
110 	*_link_len = link_len;
111 	return 0;
112 }
113 
114 static int
format_mf_symlink(u8 * buf,unsigned int buf_len,const char * link_str)115 format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str)
116 {
117 	int rc;
118 	unsigned int link_len;
119 	unsigned int ofs;
120 	u8 md5_hash[16];
121 
122 	if (buf_len != CIFS_MF_SYMLINK_FILE_SIZE)
123 		return -EINVAL;
124 
125 	link_len = strlen(link_str);
126 
127 	if (link_len > CIFS_MF_SYMLINK_LINK_MAXLEN)
128 		return -ENAMETOOLONG;
129 
130 	rc = symlink_hash(link_len, link_str, md5_hash);
131 	if (rc) {
132 		cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc);
133 		return rc;
134 	}
135 
136 	scnprintf(buf, buf_len,
137 		  CIFS_MF_SYMLINK_LEN_FORMAT CIFS_MF_SYMLINK_MD5_FORMAT,
138 		  link_len,
139 		  CIFS_MF_SYMLINK_MD5_ARGS(md5_hash));
140 
141 	ofs = CIFS_MF_SYMLINK_LINK_OFFSET;
142 	memcpy(buf + ofs, link_str, link_len);
143 
144 	ofs += link_len;
145 	if (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
146 		buf[ofs] = '\n';
147 		ofs++;
148 	}
149 
150 	while (ofs < CIFS_MF_SYMLINK_FILE_SIZE) {
151 		buf[ofs] = ' ';
152 		ofs++;
153 	}
154 
155 	return 0;
156 }
157 
158 bool
couldbe_mf_symlink(const struct cifs_fattr * fattr)159 couldbe_mf_symlink(const struct cifs_fattr *fattr)
160 {
161 	if (!S_ISREG(fattr->cf_mode))
162 		/* it's not a symlink */
163 		return false;
164 
165 	if (fattr->cf_eof != CIFS_MF_SYMLINK_FILE_SIZE)
166 		/* it's not a symlink */
167 		return false;
168 
169 	return true;
170 }
171 
172 static int
create_mf_symlink(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * fromName,const char * toName)173 create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
174 		  struct cifs_sb_info *cifs_sb, const char *fromName,
175 		  const char *toName)
176 {
177 	int rc;
178 	u8 *buf;
179 	unsigned int bytes_written = 0;
180 
181 	buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
182 	if (!buf)
183 		return -ENOMEM;
184 
185 	rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName);
186 	if (rc)
187 		goto out;
188 
189 	if (tcon->ses->server->ops->create_mf_symlink)
190 		rc = tcon->ses->server->ops->create_mf_symlink(xid, tcon,
191 					cifs_sb, fromName, buf, &bytes_written);
192 	else
193 		rc = -EOPNOTSUPP;
194 
195 	if (rc)
196 		goto out;
197 
198 	if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE)
199 		rc = -EIO;
200 out:
201 	kfree(buf);
202 	return rc;
203 }
204 
205 static int
query_mf_symlink(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const unsigned char * path,char ** symlinkinfo)206 query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon,
207 		 struct cifs_sb_info *cifs_sb, const unsigned char *path,
208 		 char **symlinkinfo)
209 {
210 	int rc;
211 	u8 *buf = NULL;
212 	unsigned int link_len = 0;
213 	unsigned int bytes_read = 0;
214 
215 	buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
216 	if (!buf)
217 		return -ENOMEM;
218 
219 	if (tcon->ses->server->ops->query_mf_symlink)
220 		rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
221 					      cifs_sb, path, buf, &bytes_read);
222 	else
223 		rc = -ENOSYS;
224 
225 	if (rc)
226 		goto out;
227 
228 	if (bytes_read == 0) { /* not a symlink */
229 		rc = -EINVAL;
230 		goto out;
231 	}
232 
233 	rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo);
234 out:
235 	kfree(buf);
236 	return rc;
237 }
238 
239 int
check_mf_symlink(unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,struct cifs_fattr * fattr,const unsigned char * path)240 check_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
241 		 struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
242 		 const unsigned char *path)
243 {
244 	int rc;
245 	u8 *buf = NULL;
246 	unsigned int link_len = 0;
247 	unsigned int bytes_read = 0;
248 
249 	if (!couldbe_mf_symlink(fattr))
250 		/* it's not a symlink */
251 		return 0;
252 
253 	buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL);
254 	if (!buf)
255 		return -ENOMEM;
256 
257 	if (tcon->ses->server->ops->query_mf_symlink)
258 		rc = tcon->ses->server->ops->query_mf_symlink(xid, tcon,
259 					      cifs_sb, path, buf, &bytes_read);
260 	else
261 		rc = -ENOSYS;
262 
263 	if (rc)
264 		goto out;
265 
266 	if (bytes_read == 0) /* not a symlink */
267 		goto out;
268 
269 	rc = parse_mf_symlink(buf, bytes_read, &link_len, NULL);
270 	if (rc == -EINVAL) {
271 		/* it's not a symlink */
272 		rc = 0;
273 		goto out;
274 	}
275 
276 	if (rc != 0)
277 		goto out;
278 
279 	/* it is a symlink */
280 	fattr->cf_eof = link_len;
281 	fattr->cf_mode &= ~S_IFMT;
282 	fattr->cf_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
283 	fattr->cf_dtype = DT_LNK;
284 out:
285 	kfree(buf);
286 	return rc;
287 }
288 
289 /*
290  * SMB 1.0 Protocol specific functions
291  */
292 
293 int
cifs_query_mf_symlink(unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const unsigned char * path,char * pbuf,unsigned int * pbytes_read)294 cifs_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
295 		      struct cifs_sb_info *cifs_sb, const unsigned char *path,
296 		      char *pbuf, unsigned int *pbytes_read)
297 {
298 	int rc;
299 	int oplock = 0;
300 	struct cifs_fid fid;
301 	struct cifs_open_parms oparms;
302 	struct cifs_io_parms io_parms = {0};
303 	int buf_type = CIFS_NO_BUFFER;
304 	FILE_ALL_INFO file_info;
305 
306 	oparms.tcon = tcon;
307 	oparms.cifs_sb = cifs_sb;
308 	oparms.desired_access = GENERIC_READ;
309 	oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
310 	oparms.disposition = FILE_OPEN;
311 	oparms.path = path;
312 	oparms.fid = &fid;
313 	oparms.reconnect = false;
314 
315 	rc = CIFS_open(xid, &oparms, &oplock, &file_info);
316 	if (rc)
317 		return rc;
318 
319 	if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
320 		rc = -ENOENT;
321 		/* it's not a symlink */
322 		goto out;
323 	}
324 
325 	io_parms.netfid = fid.netfid;
326 	io_parms.pid = current->tgid;
327 	io_parms.tcon = tcon;
328 	io_parms.offset = 0;
329 	io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
330 
331 	rc = CIFSSMBRead(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
332 out:
333 	CIFSSMBClose(xid, tcon, fid.netfid);
334 	return rc;
335 }
336 
337 int
cifs_create_mf_symlink(unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const unsigned char * path,char * pbuf,unsigned int * pbytes_written)338 cifs_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
339 		       struct cifs_sb_info *cifs_sb, const unsigned char *path,
340 		       char *pbuf, unsigned int *pbytes_written)
341 {
342 	int rc;
343 	int oplock = 0;
344 	struct cifs_fid fid;
345 	struct cifs_open_parms oparms;
346 	struct cifs_io_parms io_parms = {0};
347 
348 	oparms.tcon = tcon;
349 	oparms.cifs_sb = cifs_sb;
350 	oparms.desired_access = GENERIC_WRITE;
351 	oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
352 	oparms.disposition = FILE_CREATE;
353 	oparms.path = path;
354 	oparms.fid = &fid;
355 	oparms.reconnect = false;
356 
357 	rc = CIFS_open(xid, &oparms, &oplock, NULL);
358 	if (rc)
359 		return rc;
360 
361 	io_parms.netfid = fid.netfid;
362 	io_parms.pid = current->tgid;
363 	io_parms.tcon = tcon;
364 	io_parms.offset = 0;
365 	io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
366 
367 	rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf);
368 	CIFSSMBClose(xid, tcon, fid.netfid);
369 	return rc;
370 }
371 
372 /*
373  * SMB 2.1/SMB3 Protocol specific functions
374  */
375 int
smb3_query_mf_symlink(unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const unsigned char * path,char * pbuf,unsigned int * pbytes_read)376 smb3_query_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
377 		      struct cifs_sb_info *cifs_sb, const unsigned char *path,
378 		      char *pbuf, unsigned int *pbytes_read)
379 {
380 	int rc;
381 	struct cifs_fid fid;
382 	struct cifs_open_parms oparms;
383 	struct cifs_io_parms io_parms = {0};
384 	int buf_type = CIFS_NO_BUFFER;
385 	__le16 *utf16_path;
386 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
387 	struct smb2_file_all_info *pfile_info = NULL;
388 
389 	oparms.tcon = tcon;
390 	oparms.cifs_sb = cifs_sb;
391 	oparms.desired_access = GENERIC_READ;
392 	oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
393 	oparms.disposition = FILE_OPEN;
394 	oparms.fid = &fid;
395 	oparms.reconnect = false;
396 
397 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
398 	if (utf16_path == NULL)
399 		return -ENOMEM;
400 
401 	pfile_info = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
402 			     GFP_KERNEL);
403 
404 	if (pfile_info == NULL) {
405 		kfree(utf16_path);
406 		return  -ENOMEM;
407 	}
408 
409 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL,
410 		       NULL, NULL);
411 	if (rc)
412 		goto qmf_out_open_fail;
413 
414 	if (pfile_info->EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) {
415 		/* it's not a symlink */
416 		rc = -ENOENT; /* Is there a better rc to return? */
417 		goto qmf_out;
418 	}
419 
420 	io_parms.netfid = fid.netfid;
421 	io_parms.pid = current->tgid;
422 	io_parms.tcon = tcon;
423 	io_parms.offset = 0;
424 	io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
425 	io_parms.persistent_fid = fid.persistent_fid;
426 	io_parms.volatile_fid = fid.volatile_fid;
427 	rc = SMB2_read(xid, &io_parms, pbytes_read, &pbuf, &buf_type);
428 qmf_out:
429 	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
430 qmf_out_open_fail:
431 	kfree(utf16_path);
432 	kfree(pfile_info);
433 	return rc;
434 }
435 
436 int
smb3_create_mf_symlink(unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const unsigned char * path,char * pbuf,unsigned int * pbytes_written)437 smb3_create_mf_symlink(unsigned int xid, struct cifs_tcon *tcon,
438 		       struct cifs_sb_info *cifs_sb, const unsigned char *path,
439 		       char *pbuf, unsigned int *pbytes_written)
440 {
441 	int rc;
442 	struct cifs_fid fid;
443 	struct cifs_open_parms oparms;
444 	struct cifs_io_parms io_parms = {0};
445 	__le16 *utf16_path;
446 	__u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
447 	struct kvec iov[2];
448 
449 	cifs_dbg(FYI, "%s: path: %s\n", __func__, path);
450 
451 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
452 	if (!utf16_path)
453 		return -ENOMEM;
454 
455 	oparms.tcon = tcon;
456 	oparms.cifs_sb = cifs_sb;
457 	oparms.desired_access = GENERIC_WRITE;
458 	oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
459 	oparms.disposition = FILE_CREATE;
460 	oparms.fid = &fid;
461 	oparms.reconnect = false;
462 	oparms.mode = 0644;
463 
464 	rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
465 		       NULL, NULL);
466 	if (rc) {
467 		kfree(utf16_path);
468 		return rc;
469 	}
470 
471 	io_parms.netfid = fid.netfid;
472 	io_parms.pid = current->tgid;
473 	io_parms.tcon = tcon;
474 	io_parms.offset = 0;
475 	io_parms.length = CIFS_MF_SYMLINK_FILE_SIZE;
476 	io_parms.persistent_fid = fid.persistent_fid;
477 	io_parms.volatile_fid = fid.volatile_fid;
478 
479 	/* iov[0] is reserved for smb header */
480 	iov[1].iov_base = pbuf;
481 	iov[1].iov_len = CIFS_MF_SYMLINK_FILE_SIZE;
482 
483 	rc = SMB2_write(xid, &io_parms, pbytes_written, iov, 1);
484 
485 	/* Make sure we wrote all of the symlink data */
486 	if ((rc == 0) && (*pbytes_written != CIFS_MF_SYMLINK_FILE_SIZE))
487 		rc = -EIO;
488 
489 	SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
490 
491 	kfree(utf16_path);
492 	return rc;
493 }
494 
495 /*
496  * M-F Symlink Functions - End
497  */
498 
499 int
cifs_hardlink(struct dentry * old_file,struct inode * inode,struct dentry * direntry)500 cifs_hardlink(struct dentry *old_file, struct inode *inode,
501 	      struct dentry *direntry)
502 {
503 	int rc = -EACCES;
504 	unsigned int xid;
505 	const char *from_name, *to_name;
506 	void *page1, *page2;
507 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
508 	struct tcon_link *tlink;
509 	struct cifs_tcon *tcon;
510 	struct TCP_Server_Info *server;
511 	struct cifsInodeInfo *cifsInode;
512 
513 	if (unlikely(cifs_forced_shutdown(cifs_sb)))
514 		return -EIO;
515 
516 	tlink = cifs_sb_tlink(cifs_sb);
517 	if (IS_ERR(tlink))
518 		return PTR_ERR(tlink);
519 	tcon = tlink_tcon(tlink);
520 
521 	xid = get_xid();
522 	page1 = alloc_dentry_path();
523 	page2 = alloc_dentry_path();
524 
525 	from_name = build_path_from_dentry(old_file, page1);
526 	if (IS_ERR(from_name)) {
527 		rc = PTR_ERR(from_name);
528 		goto cifs_hl_exit;
529 	}
530 	to_name = build_path_from_dentry(direntry, page2);
531 	if (IS_ERR(to_name)) {
532 		rc = PTR_ERR(to_name);
533 		goto cifs_hl_exit;
534 	}
535 
536 	if (tcon->unix_ext)
537 		rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name,
538 					    cifs_sb->local_nls,
539 					    cifs_remap(cifs_sb));
540 	else {
541 		server = tcon->ses->server;
542 		if (!server->ops->create_hardlink) {
543 			rc = -ENOSYS;
544 			goto cifs_hl_exit;
545 		}
546 		rc = server->ops->create_hardlink(xid, tcon, from_name, to_name,
547 						  cifs_sb);
548 		if ((rc == -EIO) || (rc == -EINVAL))
549 			rc = -EOPNOTSUPP;
550 	}
551 
552 	d_drop(direntry);	/* force new lookup from server of target */
553 
554 	/*
555 	 * if source file is cached (oplocked) revalidate will not go to server
556 	 * until the file is closed or oplock broken so update nlinks locally
557 	 */
558 	if (d_really_is_positive(old_file)) {
559 		cifsInode = CIFS_I(d_inode(old_file));
560 		if (rc == 0) {
561 			spin_lock(&d_inode(old_file)->i_lock);
562 			inc_nlink(d_inode(old_file));
563 			spin_unlock(&d_inode(old_file)->i_lock);
564 
565 			/*
566 			 * parent dir timestamps will update from srv within a
567 			 * second, would it really be worth it to set the parent
568 			 * dir cifs inode time to zero to force revalidate
569 			 * (faster) for it too?
570 			 */
571 		}
572 		/*
573 		 * if not oplocked will force revalidate to get info on source
574 		 * file from srv.  Note Samba server prior to 4.2 has bug -
575 		 * not updating src file ctime on hardlinks but Windows servers
576 		 * handle it properly
577 		 */
578 		cifsInode->time = 0;
579 
580 		/*
581 		 * Will update parent dir timestamps from srv within a second.
582 		 * Would it really be worth it to set the parent dir (cifs
583 		 * inode) time field to zero to force revalidate on parent
584 		 * directory faster ie
585 		 *
586 		 * CIFS_I(inode)->time = 0;
587 		 */
588 	}
589 
590 cifs_hl_exit:
591 	free_dentry_path(page1);
592 	free_dentry_path(page2);
593 	free_xid(xid);
594 	cifs_put_tlink(tlink);
595 	return rc;
596 }
597 
598 const char *
cifs_get_link(struct dentry * direntry,struct inode * inode,struct delayed_call * done)599 cifs_get_link(struct dentry *direntry, struct inode *inode,
600 	      struct delayed_call *done)
601 {
602 	int rc = -ENOMEM;
603 	unsigned int xid;
604 	const char *full_path;
605 	void *page;
606 	char *target_path = NULL;
607 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
608 	struct tcon_link *tlink = NULL;
609 	struct cifs_tcon *tcon;
610 	struct TCP_Server_Info *server;
611 
612 	if (!direntry)
613 		return ERR_PTR(-ECHILD);
614 
615 	xid = get_xid();
616 
617 	tlink = cifs_sb_tlink(cifs_sb);
618 	if (IS_ERR(tlink)) {
619 		free_xid(xid);
620 		return ERR_CAST(tlink);
621 	}
622 	tcon = tlink_tcon(tlink);
623 	server = tcon->ses->server;
624 
625 	page = alloc_dentry_path();
626 	full_path = build_path_from_dentry(direntry, page);
627 	if (IS_ERR(full_path)) {
628 		free_xid(xid);
629 		cifs_put_tlink(tlink);
630 		free_dentry_path(page);
631 		return ERR_CAST(full_path);
632 	}
633 
634 	cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
635 
636 	rc = -EACCES;
637 	/*
638 	 * First try Minshall+French Symlinks, if configured
639 	 * and fallback to UNIX Extensions Symlinks.
640 	 */
641 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
642 		rc = query_mf_symlink(xid, tcon, cifs_sb, full_path,
643 				      &target_path);
644 
645 	if (rc != 0 && server->ops->query_symlink) {
646 		struct cifsInodeInfo *cifsi = CIFS_I(inode);
647 		bool reparse_point = false;
648 
649 		if (cifsi->cifsAttrs & ATTR_REPARSE)
650 			reparse_point = true;
651 
652 		rc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
653 						&target_path, reparse_point);
654 	}
655 
656 	free_dentry_path(page);
657 	free_xid(xid);
658 	cifs_put_tlink(tlink);
659 	if (rc != 0) {
660 		kfree(target_path);
661 		return ERR_PTR(rc);
662 	}
663 	set_delayed_call(done, kfree_link, target_path);
664 	return target_path;
665 }
666 
667 int
cifs_symlink(struct user_namespace * mnt_userns,struct inode * inode,struct dentry * direntry,const char * symname)668 cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode,
669 	     struct dentry *direntry, const char *symname)
670 {
671 	int rc = -EOPNOTSUPP;
672 	unsigned int xid;
673 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
674 	struct tcon_link *tlink;
675 	struct cifs_tcon *pTcon;
676 	const char *full_path;
677 	void *page;
678 	struct inode *newinode = NULL;
679 
680 	if (unlikely(cifs_forced_shutdown(cifs_sb)))
681 		return -EIO;
682 
683 	page = alloc_dentry_path();
684 	if (!page)
685 		return -ENOMEM;
686 
687 	xid = get_xid();
688 
689 	tlink = cifs_sb_tlink(cifs_sb);
690 	if (IS_ERR(tlink)) {
691 		rc = PTR_ERR(tlink);
692 		goto symlink_exit;
693 	}
694 	pTcon = tlink_tcon(tlink);
695 
696 	full_path = build_path_from_dentry(direntry, page);
697 	if (IS_ERR(full_path)) {
698 		rc = PTR_ERR(full_path);
699 		goto symlink_exit;
700 	}
701 
702 	cifs_dbg(FYI, "Full path: %s\n", full_path);
703 	cifs_dbg(FYI, "symname is %s\n", symname);
704 
705 	/* BB what if DFS and this volume is on different share? BB */
706 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
707 		rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname);
708 	else if (pTcon->unix_ext)
709 		rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname,
710 					   cifs_sb->local_nls,
711 					   cifs_remap(cifs_sb));
712 	/* else
713 	   rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName,
714 					cifs_sb_target->local_nls); */
715 
716 	if (rc == 0) {
717 		if (pTcon->posix_extensions)
718 			rc = smb311_posix_get_inode_info(&newinode, full_path, inode->i_sb, xid);
719 		else if (pTcon->unix_ext)
720 			rc = cifs_get_inode_info_unix(&newinode, full_path,
721 						      inode->i_sb, xid);
722 		else
723 			rc = cifs_get_inode_info(&newinode, full_path, NULL,
724 						 inode->i_sb, xid, NULL);
725 
726 		if (rc != 0) {
727 			cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n",
728 				 rc);
729 		} else {
730 			d_instantiate(direntry, newinode);
731 		}
732 	}
733 symlink_exit:
734 	free_dentry_path(page);
735 	cifs_put_tlink(tlink);
736 	free_xid(xid);
737 	return rc;
738 }
739