Steve French | 929be90 | 2021-06-18 00:31:49 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Steve French | 366781c | 2008-01-25 10:12:41 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2002,2008 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 18 | #include "cifs_unicode.h" |
Steve French | 5ab9757 | 2014-09-15 04:49:28 -0500 | [diff] [blame] | 19 | #include "smb2proto.h" |
Steve French | 087f757 | 2021-04-29 00:18:43 -0500 | [diff] [blame] | 20 | #include "cifs_ioctl.h" |
Stefan Metzmacher | c69c1b6 | 2010-07-31 09:14:16 +0200 | [diff] [blame] | 21 | |
Sachin Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 22 | /* |
| 23 | * M-F Symlink Functions - Begin |
| 24 | */ |
| 25 | |
Stefan Metzmacher | c69c1b6 | 2010-07-31 09:14:16 +0200 | [diff] [blame] | 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" |
Rasmus Villemoes | c6fc663 | 2016-11-30 23:40:32 +0100 | [diff] [blame] | 34 | #define CIFS_MF_SYMLINK_MD5_FORMAT "%16phN\n" |
| 35 | #define CIFS_MF_SYMLINK_MD5_ARGS(md5_hash) md5_hash |
Stefan Metzmacher | c69c1b6 | 2010-07-31 09:14:16 +0200 | [diff] [blame] | 36 | |
| 37 | static int |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 38 | symlink_hash(unsigned int link_len, const char *link_str, u8 *md5_hash) |
| 39 | { |
| 40 | int rc; |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 41 | struct crypto_shash *md5 = NULL; |
| 42 | struct sdesc *sdescmd5 = NULL; |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 43 | |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 44 | rc = cifs_alloc_hash("md5", &md5, &sdescmd5); |
| 45 | if (rc) |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 46 | goto symlink_hash_err; |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 47 | |
| 48 | rc = crypto_shash_init(&sdescmd5->shash); |
| 49 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 50 | cifs_dbg(VFS, "%s: Could not init md5 shash\n", __func__); |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 51 | goto symlink_hash_err; |
| 52 | } |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 53 | rc = crypto_shash_update(&sdescmd5->shash, link_str, link_len); |
| 54 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 55 | cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 56 | goto symlink_hash_err; |
| 57 | } |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 58 | rc = crypto_shash_final(&sdescmd5->shash, md5_hash); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 59 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 60 | cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__); |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 61 | |
| 62 | symlink_hash_err: |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 63 | cifs_free_hash(&md5, &sdescmd5); |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 64 | return rc; |
| 65 | } |
| 66 | |
| 67 | static int |
Sachin Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 68 | parse_mf_symlink(const u8 *buf, unsigned int buf_len, unsigned int *_link_len, |
| 69 | char **_link_str) |
Stefan Metzmacher | c69c1b6 | 2010-07-31 09:14:16 +0200 | [diff] [blame] | 70 | { |
| 71 | int rc; |
| 72 | unsigned int link_len; |
| 73 | const char *md5_str1; |
| 74 | const char *link_str; |
Stefan Metzmacher | c69c1b6 | 2010-07-31 09:14:16 +0200 | [diff] [blame] | 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 | |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 88 | rc = symlink_hash(link_len, link_str, md5_hash); |
| 89 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 90 | cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc); |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 91 | return rc; |
| 92 | } |
Stefan Metzmacher | c69c1b6 | 2010-07-31 09:14:16 +0200 | [diff] [blame] | 93 | |
Ronnie Sahlberg | 74ea5f9 | 2019-02-09 09:51:11 +1000 | [diff] [blame] | 94 | scnprintf(md5_str2, sizeof(md5_str2), |
| 95 | CIFS_MF_SYMLINK_MD5_FORMAT, |
| 96 | CIFS_MF_SYMLINK_MD5_ARGS(md5_hash)); |
Stefan Metzmacher | c69c1b6 | 2010-07-31 09:14:16 +0200 | [diff] [blame] | 97 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Stefan Metzmacher | 0fd43ae | 2010-08-05 21:13:44 +0200 | [diff] [blame] | 111 | static int |
Sachin Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 112 | format_mf_symlink(u8 *buf, unsigned int buf_len, const char *link_str) |
Stefan Metzmacher | 18bddd1 | 2010-08-03 11:24:22 +0200 | [diff] [blame] | 113 | { |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 114 | int rc; |
Stefan Metzmacher | 18bddd1 | 2010-08-03 11:24:22 +0200 | [diff] [blame] | 115 | unsigned int link_len; |
| 116 | unsigned int ofs; |
Stefan Metzmacher | 18bddd1 | 2010-08-03 11:24:22 +0200 | [diff] [blame] | 117 | 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 French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 127 | rc = symlink_hash(link_len, link_str, md5_hash); |
| 128 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 129 | cifs_dbg(FYI, "%s: MD5 hash failure: %d\n", __func__, rc); |
Steve French | 93c100c | 2011-01-25 19:28:43 +0000 | [diff] [blame] | 130 | return rc; |
| 131 | } |
Stefan Metzmacher | 18bddd1 | 2010-08-03 11:24:22 +0200 | [diff] [blame] | 132 | |
Ronnie Sahlberg | 74ea5f9 | 2019-02-09 09:51:11 +1000 | [diff] [blame] | 133 | 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 Metzmacher | 18bddd1 | 2010-08-03 11:24:22 +0200 | [diff] [blame] | 137 | |
| 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 Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 155 | bool |
| 156 | couldbe_mf_symlink(const struct cifs_fattr *fattr) |
| 157 | { |
Sachin Prabhu | a9a315d | 2014-01-31 14:27:16 +0000 | [diff] [blame] | 158 | if (!S_ISREG(fattr->cf_mode)) |
Sachin Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 159 | /* 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 Metzmacher | 8713d01 | 2010-08-05 21:15:22 +0200 | [diff] [blame] | 169 | static int |
Sachin Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 170 | create_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon, |
Sachin Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 171 | struct cifs_sb_info *cifs_sb, const char *fromName, |
| 172 | const char *toName) |
Stefan Metzmacher | 8713d01 | 2010-08-05 21:15:22 +0200 | [diff] [blame] | 173 | { |
| 174 | int rc; |
Stefan Metzmacher | 8713d01 | 2010-08-05 21:15:22 +0200 | [diff] [blame] | 175 | 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 Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 182 | rc = format_mf_symlink(buf, CIFS_MF_SYMLINK_FILE_SIZE, toName); |
Sachin Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 183 | if (rc) |
| 184 | goto out; |
Stefan Metzmacher | 8713d01 | 2010-08-05 21:15:22 +0200 | [diff] [blame] | 185 | |
Steve French | da80659 | 2014-09-14 23:27:09 -0500 | [diff] [blame] | 186 | 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 Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 192 | if (rc) |
| 193 | goto out; |
Stefan Metzmacher | 8713d01 | 2010-08-05 21:15:22 +0200 | [diff] [blame] | 194 | |
| 195 | if (bytes_written != CIFS_MF_SYMLINK_FILE_SIZE) |
Sachin Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 196 | rc = -EIO; |
| 197 | out: |
| 198 | kfree(buf); |
| 199 | return rc; |
Stefan Metzmacher | 8713d01 | 2010-08-05 21:15:22 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static int |
Sachin Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 203 | query_mf_symlink(const unsigned int xid, struct cifs_tcon *tcon, |
Sachin Prabhu | 8205d1b | 2013-11-25 17:09:51 +0000 | [diff] [blame] | 204 | struct cifs_sb_info *cifs_sb, const unsigned char *path, |
| 205 | char **symlinkinfo) |
Stefan Metzmacher | 0fd43ae | 2010-08-05 21:13:44 +0200 | [diff] [blame] | 206 | { |
| 207 | int rc; |
Sachin Prabhu | 8205d1b | 2013-11-25 17:09:51 +0000 | [diff] [blame] | 208 | u8 *buf = NULL; |
Stefan Metzmacher | 0fd43ae | 2010-08-05 21:13:44 +0200 | [diff] [blame] | 209 | unsigned int link_len = 0; |
Sachin Prabhu | 8205d1b | 2013-11-25 17:09:51 +0000 | [diff] [blame] | 210 | unsigned int bytes_read = 0; |
Stefan Metzmacher | 0fd43ae | 2010-08-05 21:13:44 +0200 | [diff] [blame] | 211 | |
| 212 | buf = kmalloc(CIFS_MF_SYMLINK_FILE_SIZE, GFP_KERNEL); |
| 213 | if (!buf) |
| 214 | return -ENOMEM; |
Stefan Metzmacher | 0fd43ae | 2010-08-05 21:13:44 +0200 | [diff] [blame] | 215 | |
Sachin Prabhu | 8205d1b | 2013-11-25 17:09:51 +0000 | [diff] [blame] | 216 | 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 Metzmacher | 0fd43ae | 2010-08-05 21:13:44 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Sachin Prabhu | cb084b1 | 2013-11-25 17:09:50 +0000 | [diff] [blame] | 230 | rc = parse_mf_symlink(buf, bytes_read, &link_len, symlinkinfo); |
Sachin Prabhu | 8205d1b | 2013-11-25 17:09:51 +0000 | [diff] [blame] | 231 | out: |
Stefan Metzmacher | 0fd43ae | 2010-08-05 21:13:44 +0200 | [diff] [blame] | 232 | kfree(buf); |
Sachin Prabhu | 8205d1b | 2013-11-25 17:09:51 +0000 | [diff] [blame] | 233 | return rc; |
Stefan Metzmacher | 0fd43ae | 2010-08-05 21:13:44 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Sachin Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 236 | int |
| 237 | check_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 Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 240 | { |
Sachin Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 241 | int rc; |
| 242 | u8 *buf = NULL; |
| 243 | unsigned int link_len = 0; |
| 244 | unsigned int bytes_read = 0; |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 245 | |
Sachin Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 246 | if (!couldbe_mf_symlink(fattr)) |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 247 | /* it's not a symlink */ |
Sachin Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 248 | return 0; |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 249 | |
Sachin Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 250 | 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; |
| 281 | out: |
| 282 | kfree(buf); |
| 283 | return rc; |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Sachin Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 286 | /* |
| 287 | * SMB 1.0 Protocol specific functions |
| 288 | */ |
| 289 | |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 290 | int |
Sachin Prabhu | b5be1a1 | 2013-11-25 17:09:49 +0000 | [diff] [blame] | 291 | cifs_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 Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 294 | { |
| 295 | int rc; |
| 296 | int oplock = 0; |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 297 | struct cifs_fid fid; |
| 298 | struct cifs_open_parms oparms; |
Aurelien Aptel | 7c06514 | 2020-06-04 17:23:55 +0200 | [diff] [blame] | 299 | struct cifs_io_parms io_parms = {0}; |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 300 | int buf_type = CIFS_NO_BUFFER; |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 301 | FILE_ALL_INFO file_info; |
| 302 | |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 303 | oparms.tcon = tcon; |
| 304 | oparms.cifs_sb = cifs_sb; |
| 305 | oparms.desired_access = GENERIC_READ; |
Amir Goldstein | 0f06093 | 2020-02-03 21:46:43 +0200 | [diff] [blame] | 306 | oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR); |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 307 | 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 Prabhu | b5be1a1 | 2013-11-25 17:09:49 +0000 | [diff] [blame] | 313 | if (rc) |
Steve French | 1b24408 | 2013-07-11 19:17:40 -0500 | [diff] [blame] | 314 | return rc; |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 315 | |
Steve French | 364d429 | 2014-09-16 06:40:25 -0500 | [diff] [blame] | 316 | if (file_info.EndOfFile != cpu_to_le64(CIFS_MF_SYMLINK_FILE_SIZE)) { |
| 317 | rc = -ENOENT; |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 318 | /* it's not a symlink */ |
Sachin Prabhu | b5be1a1 | 2013-11-25 17:09:49 +0000 | [diff] [blame] | 319 | goto out; |
Steve French | 364d429 | 2014-09-16 06:40:25 -0500 | [diff] [blame] | 320 | } |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 321 | |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 322 | io_parms.netfid = fid.netfid; |
Steve French | 1b24408 | 2013-07-11 19:17:40 -0500 | [diff] [blame] | 323 | io_parms.pid = current->tgid; |
Sachin Prabhu | b5be1a1 | 2013-11-25 17:09:49 +0000 | [diff] [blame] | 324 | io_parms.tcon = tcon; |
Steve French | 1b24408 | 2013-07-11 19:17:40 -0500 | [diff] [blame] | 325 | 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 Prabhu | b5be1a1 | 2013-11-25 17:09:49 +0000 | [diff] [blame] | 329 | out: |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 330 | CIFSSMBClose(xid, tcon, fid.netfid); |
Steve French | 1b24408 | 2013-07-11 19:17:40 -0500 | [diff] [blame] | 331 | return rc; |
| 332 | } |
| 333 | |
Steve French | 1b24408 | 2013-07-11 19:17:40 -0500 | [diff] [blame] | 334 | int |
Sachin Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 335 | cifs_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 Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 341 | struct cifs_fid fid; |
| 342 | struct cifs_open_parms oparms; |
Aurelien Aptel | 7c06514 | 2020-06-04 17:23:55 +0200 | [diff] [blame] | 343 | struct cifs_io_parms io_parms = {0}; |
Sachin Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 344 | |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 345 | oparms.tcon = tcon; |
| 346 | oparms.cifs_sb = cifs_sb; |
| 347 | oparms.desired_access = GENERIC_WRITE; |
Amir Goldstein | 0f06093 | 2020-02-03 21:46:43 +0200 | [diff] [blame] | 348 | oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR); |
Björn Baumbach | a1d0b84c | 2014-06-10 12:03:26 +0200 | [diff] [blame] | 349 | oparms.disposition = FILE_CREATE; |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 350 | oparms.path = path; |
| 351 | oparms.fid = &fid; |
| 352 | oparms.reconnect = false; |
| 353 | |
| 354 | rc = CIFS_open(xid, &oparms, &oplock, NULL); |
Sachin Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 355 | if (rc) |
| 356 | return rc; |
| 357 | |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 358 | io_parms.netfid = fid.netfid; |
Sachin Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 359 | 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 Viro | dbbab32 | 2016-09-05 17:53:43 -0400 | [diff] [blame] | 364 | rc = CIFSSMBWrite(xid, &io_parms, pbytes_written, pbuf); |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 365 | CIFSSMBClose(xid, tcon, fid.netfid); |
Sachin Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 366 | return rc; |
| 367 | } |
| 368 | |
Steve French | c22870e | 2014-09-16 07:18:19 -0500 | [diff] [blame] | 369 | /* |
| 370 | * SMB 2.1/SMB3 Protocol specific functions |
| 371 | */ |
Steve French | c22870e | 2014-09-16 07:18:19 -0500 | [diff] [blame] | 372 | int |
| 373 | smb3_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 Aptel | 7c06514 | 2020-06-04 17:23:55 +0200 | [diff] [blame] | 380 | struct cifs_io_parms io_parms = {0}; |
Steve French | c22870e | 2014-09-16 07:18:19 -0500 | [diff] [blame] | 381 | int buf_type = CIFS_NO_BUFFER; |
| 382 | __le16 *utf16_path; |
Steve French | 2278315 | 2018-07-27 22:01:49 -0500 | [diff] [blame] | 383 | __u8 oplock = SMB2_OPLOCK_LEVEL_NONE; |
Steve French | c22870e | 2014-09-16 07:18:19 -0500 | [diff] [blame] | 384 | 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 Goldstein | 0f06093 | 2020-02-03 21:46:43 +0200 | [diff] [blame] | 389 | oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR); |
Steve French | c22870e | 2014-09-16 07:18:19 -0500 | [diff] [blame] | 390 | 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 Sahlberg | 9d874c3 | 2018-06-08 13:21:18 +1000 | [diff] [blame] | 406 | rc = SMB2_open(xid, &oparms, utf16_path, &oplock, pfile_info, NULL, |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 407 | NULL, NULL); |
Steve French | c22870e | 2014-09-16 07:18:19 -0500 | [diff] [blame] | 408 | 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); |
| 425 | qmf_out: |
| 426 | SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid); |
| 427 | qmf_out_open_fail: |
| 428 | kfree(utf16_path); |
| 429 | kfree(pfile_info); |
| 430 | return rc; |
| 431 | } |
| 432 | |
Steve French | 5ab9757 | 2014-09-15 04:49:28 -0500 | [diff] [blame] | 433 | int |
| 434 | smb3_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 Aptel | 352d96f | 2020-05-31 12:38:22 -0500 | [diff] [blame] | 441 | struct cifs_io_parms io_parms = {0}; |
Steve French | 5ab9757 | 2014-09-15 04:49:28 -0500 | [diff] [blame] | 442 | __le16 *utf16_path; |
Steve French | 2278315 | 2018-07-27 22:01:49 -0500 | [diff] [blame] | 443 | __u8 oplock = SMB2_OPLOCK_LEVEL_NONE; |
Steve French | 5ab9757 | 2014-09-15 04:49:28 -0500 | [diff] [blame] | 444 | struct kvec iov[2]; |
| 445 | |
Steve French | 5ab9757 | 2014-09-15 04:49:28 -0500 | [diff] [blame] | 446 | 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 Goldstein | 0f06093 | 2020-02-03 21:46:43 +0200 | [diff] [blame] | 455 | oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR); |
Steve French | 5ab9757 | 2014-09-15 04:49:28 -0500 | [diff] [blame] | 456 | oparms.disposition = FILE_CREATE; |
| 457 | oparms.fid = &fid; |
| 458 | oparms.reconnect = false; |
| 459 | |
Ronnie Sahlberg | 9d874c3 | 2018-06-08 13:21:18 +1000 | [diff] [blame] | 460 | rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, |
Aurelien Aptel | 69dda30 | 2020-03-02 17:53:22 +0100 | [diff] [blame] | 461 | NULL, NULL); |
Steve French | 5ab9757 | 2014-09-15 04:49:28 -0500 | [diff] [blame] | 462 | 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 French | 5ab9757 | 2014-09-15 04:49:28 -0500 | [diff] [blame] | 490 | |
Sachin Prabhu | 0f8dce1 | 2013-11-25 17:09:53 +0000 | [diff] [blame] | 491 | /* |
| 492 | * M-F Symlink Functions - End |
| 493 | */ |
Stefan Metzmacher | 8bfb50a | 2010-07-31 09:15:10 +0200 | [diff] [blame] | 494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | int |
| 496 | cifs_hardlink(struct dentry *old_file, struct inode *inode, |
| 497 | struct dentry *direntry) |
| 498 | { |
| 499 | int rc = -EACCES; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 500 | unsigned int xid; |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 501 | const char *from_name, *to_name; |
| 502 | void *page1, *page2; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 503 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 504 | struct tcon_link *tlink; |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 505 | struct cifs_tcon *tcon; |
| 506 | struct TCP_Server_Info *server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | struct cifsInodeInfo *cifsInode; |
| 508 | |
Steve French | 087f757 | 2021-04-29 00:18:43 -0500 | [diff] [blame] | 509 | if (unlikely(cifs_forced_shutdown(cifs_sb))) |
| 510 | return -EIO; |
| 511 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 512 | tlink = cifs_sb_tlink(cifs_sb); |
| 513 | if (IS_ERR(tlink)) |
| 514 | return PTR_ERR(tlink); |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 515 | tcon = tlink_tcon(tlink); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 516 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 517 | xid = get_xid(); |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 518 | page1 = alloc_dentry_path(); |
| 519 | page2 = alloc_dentry_path(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 521 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | goto cifs_hl_exit; |
| 530 | } |
| 531 | |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 532 | if (tcon->unix_ext) |
| 533 | rc = CIFSUnixCreateHardLink(xid, tcon, from_name, to_name, |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 534 | cifs_sb->local_nls, |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 535 | cifs_remap(cifs_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | else { |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 537 | server = tcon->ses->server; |
Christian Engelmayer | abf9767 | 2014-01-11 01:57:22 +0100 | [diff] [blame] | 538 | if (!server->ops->create_hardlink) { |
| 539 | rc = -ENOSYS; |
| 540 | goto cifs_hl_exit; |
| 541 | } |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 542 | rc = server->ops->create_hardlink(xid, tcon, from_name, to_name, |
| 543 | cifs_sb); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 544 | if ((rc == -EIO) || (rc == -EINVAL)) |
| 545 | rc = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Steve French | 31ec35d | 2006-11-16 20:54:20 +0000 | [diff] [blame] | 548 | d_drop(direntry); /* force new lookup from server of target */ |
| 549 | |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 550 | /* |
| 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 Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 554 | if (d_really_is_positive(old_file)) { |
| 555 | cifsInode = CIFS_I(d_inode(old_file)); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 556 | if (rc == 0) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 557 | 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 French | ff273cb | 2014-10-17 17:17:12 -0500 | [diff] [blame] | 560 | |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 561 | /* |
| 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 French | 31ec35d | 2006-11-16 20:54:20 +0000 | [diff] [blame] | 567 | } |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 568 | /* |
| 569 | * if not oplocked will force revalidate to get info on source |
Steve French | ff273cb | 2014-10-17 17:17:12 -0500 | [diff] [blame] | 570 | * 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 French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 573 | */ |
Steve French | 31ec35d | 2006-11-16 20:54:20 +0000 | [diff] [blame] | 574 | cifsInode->time = 0; |
| 575 | |
Steve French | d6e906f | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 576 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | cifs_hl_exit: |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 587 | free_dentry_path(page1); |
| 588 | free_dentry_path(page2); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 589 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 590 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | return rc; |
| 592 | } |
| 593 | |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 594 | const char * |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 595 | cifs_get_link(struct dentry *direntry, struct inode *inode, |
| 596 | struct delayed_call *done) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { |
Jeff Layton | 8b6427a | 2009-05-19 09:57:03 -0400 | [diff] [blame] | 598 | int rc = -ENOMEM; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 599 | unsigned int xid; |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 600 | const char *full_path; |
| 601 | void *page; |
Jeff Layton | 8b6427a | 2009-05-19 09:57:03 -0400 | [diff] [blame] | 602 | char *target_path = NULL; |
| 603 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 604 | struct tcon_link *tlink = NULL; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 605 | struct cifs_tcon *tcon; |
Pavel Shilovsky | b42bf88 | 2013-08-14 19:25:21 +0400 | [diff] [blame] | 606 | struct TCP_Server_Info *server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 608 | if (!direntry) |
| 609 | return ERR_PTR(-ECHILD); |
| 610 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 611 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 613 | tlink = cifs_sb_tlink(cifs_sb); |
| 614 | if (IS_ERR(tlink)) { |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 615 | free_xid(xid); |
| 616 | return ERR_CAST(tlink); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 617 | } |
| 618 | tcon = tlink_tcon(tlink); |
Pavel Shilovsky | b42bf88 | 2013-08-14 19:25:21 +0400 | [diff] [blame] | 619 | server = tcon->ses->server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 621 | page = alloc_dentry_path(); |
| 622 | full_path = build_path_from_dentry(direntry, page); |
| 623 | if (IS_ERR(full_path)) { |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 624 | free_xid(xid); |
| 625 | cifs_put_tlink(tlink); |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 626 | free_dentry_path(page); |
| 627 | return ERR_CAST(full_path); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 628 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 630 | cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 632 | 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 Prabhu | 8205d1b | 2013-11-25 17:09:51 +0000 | [diff] [blame] | 638 | rc = query_mf_symlink(xid, tcon, cifs_sb, full_path, |
| 639 | &target_path); |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 640 | |
Ronnie Sahlberg | ebaf546 | 2019-04-10 08:44:46 +1000 | [diff] [blame] | 641 | 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 Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 651 | |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 652 | free_dentry_path(page); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 653 | free_xid(xid); |
| 654 | cifs_put_tlink(tlink); |
Jeff Layton | 460b969 | 2009-04-30 07:17:56 -0400 | [diff] [blame] | 655 | if (rc != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | kfree(target_path); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 657 | return ERR_PTR(rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 659 | set_delayed_call(done, kfree_link, target_path); |
| 660 | return target_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | int |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 664 | cifs_symlink(struct user_namespace *mnt_userns, struct inode *inode, |
| 665 | struct dentry *direntry, const char *symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | { |
| 667 | int rc = -EOPNOTSUPP; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 668 | unsigned int xid; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 669 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 670 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 671 | struct cifs_tcon *pTcon; |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 672 | const char *full_path; |
Khaled ROMDHANI | bae4c0c | 2021-05-04 16:38:55 +0100 | [diff] [blame] | 673 | void *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | struct inode *newinode = NULL; |
| 675 | |
Steve French | 087f757 | 2021-04-29 00:18:43 -0500 | [diff] [blame] | 676 | if (unlikely(cifs_forced_shutdown(cifs_sb))) |
| 677 | return -EIO; |
| 678 | |
Khaled ROMDHANI | bae4c0c | 2021-05-04 16:38:55 +0100 | [diff] [blame] | 679 | page = alloc_dentry_path(); |
| 680 | if (!page) |
| 681 | return -ENOMEM; |
| 682 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 683 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 685 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 692 | full_path = build_path_from_dentry(direntry, page); |
| 693 | if (IS_ERR(full_path)) { |
| 694 | rc = PTR_ERR(full_path); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 695 | goto symlink_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 698 | cifs_dbg(FYI, "Full path: %s\n", full_path); |
| 699 | cifs_dbg(FYI, "symname is %s\n", symname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | /* BB what if DFS and this volume is on different share? BB */ |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 702 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) |
Sachin Prabhu | cbb0aba | 2013-11-25 17:09:52 +0000 | [diff] [blame] | 703 | rc = create_mf_symlink(xid, pTcon, cifs_sb, full_path, symname); |
Stefan Metzmacher | 1b12b9c | 2010-08-05 21:19:56 +0200 | [diff] [blame] | 704 | else if (pTcon->unix_ext) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | rc = CIFSUnixCreateSymLink(xid, pTcon, full_path, symname, |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 706 | cifs_sb->local_nls, |
| 707 | cifs_remap(cifs_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | /* else |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 709 | rc = CIFSCreateReparseSymLink(xid, pTcon, fromName, toName, |
| 710 | cifs_sb_target->local_nls); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | if (rc == 0) { |
Steve French | d313852 | 2020-06-11 22:43:01 -0500 | [diff] [blame] | 713 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | rc = cifs_get_inode_info_unix(&newinode, full_path, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 717 | inode->i_sb, xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | else |
| 719 | rc = cifs_get_inode_info(&newinode, full_path, NULL, |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 720 | inode->i_sb, xid, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
| 722 | if (rc != 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 723 | cifs_dbg(FYI, "Create symlink ok, getinodeinfo fail rc = %d\n", |
| 724 | rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | d_instantiate(direntry, newinode); |
| 727 | } |
| 728 | } |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 729 | symlink_exit: |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 730 | free_dentry_path(page); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 731 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 732 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | return rc; |
| 734 | } |