blob: daa7115adc5f059ac8341ad7a14338d626797a4b [file] [log] [blame]
Steve French1080ef72011-02-24 18:07:19 +00001/*
2 * SMB2 version specific operations
3 *
4 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License v2 as published
8 * by the Free Software Foundation.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -070020#include <linux/pagemap.h>
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070021#include <linux/vfs.h>
Steve Frenchf29ebb42014-07-19 21:44:58 -050022#include <linux/falloc.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070023#include <linux/scatterlist.h>
Tobias Regnery4fa8e502017-03-30 12:34:14 +020024#include <linux/uuid.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070025#include <crypto/aead.h>
Steve French1080ef72011-02-24 18:07:19 +000026#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040027#include "smb2pdu.h"
28#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040029#include "cifsproto.h"
30#include "cifs_debug.h"
Pavel Shilovskyb42bf882013-08-14 19:25:21 +040031#include "cifs_unicode.h"
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070032#include "smb2status.h"
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070033#include "smb2glob.h"
Steve French834170c2016-09-30 21:14:26 -050034#include "cifs_ioctl.h"
Long Li09902f82017-11-22 17:38:39 -070035#include "smbdirect.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040036
37static int
38change_conf(struct TCP_Server_Info *server)
39{
40 server->credits += server->echo_credits + server->oplock_credits;
41 server->oplock_credits = server->echo_credits = 0;
42 switch (server->credits) {
43 case 0:
44 return -1;
45 case 1:
46 server->echoes = false;
47 server->oplocks = false;
Joe Perchesf96637b2013-05-04 22:12:25 -050048 cifs_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040049 break;
50 case 2:
51 server->echoes = true;
52 server->oplocks = false;
53 server->echo_credits = 1;
Joe Perchesf96637b2013-05-04 22:12:25 -050054 cifs_dbg(FYI, "disabling oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040055 break;
56 default:
57 server->echoes = true;
Steve Frenche0ddde92015-09-22 09:29:38 -050058 if (enable_oplocks) {
59 server->oplocks = true;
60 server->oplock_credits = 1;
61 } else
62 server->oplocks = false;
63
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040064 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040065 }
66 server->credits -= server->echo_credits + server->oplock_credits;
67 return 0;
68}
69
70static void
71smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
72 const int optype)
73{
74 int *val, rc = 0;
75 spin_lock(&server->req_lock);
76 val = server->ops->get_credits_field(server, optype);
Steve Frenchb340a4d2018-09-01 01:10:17 -050077
78 /* eg found case where write overlapping reconnect messed up credits */
79 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
80 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
81 server->hostname, *val);
82
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040083 *val += add;
Steve French141891f2016-09-23 00:44:16 -050084 if (*val > 65000) {
85 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
86 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
87 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040088 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040089 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040090 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070091 /*
92 * Sometimes server returns 0 credits on oplock break ack - we need to
93 * rebalance credits in this case.
94 */
95 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
96 server->oplocks) {
97 if (server->credits > 1) {
98 server->credits--;
99 server->oplock_credits++;
100 }
101 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400102 spin_unlock(&server->req_lock);
103 wake_up(&server->request_q);
104 if (rc)
105 cifs_reconnect(server);
106}
107
108static void
109smb2_set_credits(struct TCP_Server_Info *server, const int val)
110{
111 spin_lock(&server->req_lock);
112 server->credits = val;
Steve French9e1a37d2018-09-19 02:38:17 -0500113 if (val == 1)
114 server->reconnect_instance++;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400115 spin_unlock(&server->req_lock);
Steve French6e4d3bb2018-09-22 11:25:04 -0500116 /* don't log while holding the lock */
117 if (val == 1)
118 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400119}
120
121static int *
122smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
123{
124 switch (optype) {
125 case CIFS_ECHO_OP:
126 return &server->echo_credits;
127 case CIFS_OBREAK_OP:
128 return &server->oplock_credits;
129 default:
130 return &server->credits;
131 }
132}
133
134static unsigned int
135smb2_get_credits(struct mid_q_entry *mid)
136{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000137 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700138
139 return le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400140}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400141
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400142static int
143smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
144 unsigned int *num, unsigned int *credits)
145{
146 int rc = 0;
147 unsigned int scredits;
148
149 spin_lock(&server->req_lock);
150 while (1) {
151 if (server->credits <= 0) {
152 spin_unlock(&server->req_lock);
153 cifs_num_waiters_inc(server);
154 rc = wait_event_killable(server->request_q,
155 has_credits(server, &server->credits));
156 cifs_num_waiters_dec(server);
157 if (rc)
158 return rc;
159 spin_lock(&server->req_lock);
160 } else {
161 if (server->tcpStatus == CifsExiting) {
162 spin_unlock(&server->req_lock);
163 return -ENOENT;
164 }
165
166 scredits = server->credits;
167 /* can deadlock with reopen */
168 if (scredits == 1) {
169 *num = SMB2_MAX_BUFFER_SIZE;
170 *credits = 0;
171 break;
172 }
173
174 /* leave one credit for a possible reopen */
175 scredits--;
176 *num = min_t(unsigned int, size,
177 scredits * SMB2_MAX_BUFFER_SIZE);
178
179 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
180 server->credits -= *credits;
181 server->in_flight++;
182 break;
183 }
184 }
185 spin_unlock(&server->req_lock);
186 return rc;
187}
188
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400189static __u64
190smb2_get_next_mid(struct TCP_Server_Info *server)
191{
192 __u64 mid;
193 /* for SMB2 we need the current value */
194 spin_lock(&GlobalMid_Lock);
195 mid = server->CurrentMid++;
196 spin_unlock(&GlobalMid_Lock);
197 return mid;
198}
Steve French1080ef72011-02-24 18:07:19 +0000199
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400200static struct mid_q_entry *
201smb2_find_mid(struct TCP_Server_Info *server, char *buf)
202{
203 struct mid_q_entry *mid;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000204 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700205 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400206
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700207 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
Steve French373512e2015-12-18 13:05:30 -0600208 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
209 return NULL;
210 }
211
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400212 spin_lock(&GlobalMid_Lock);
213 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Sachin Prabhu9235d092014-12-09 17:37:00 +0000214 if ((mid->mid == wire_mid) &&
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400215 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700216 (mid->command == shdr->Command)) {
Lars Persson696e4202018-06-25 14:05:25 +0200217 kref_get(&mid->refcount);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400218 spin_unlock(&GlobalMid_Lock);
219 return mid;
220 }
221 }
222 spin_unlock(&GlobalMid_Lock);
223 return NULL;
224}
225
226static void
Ronnie Sahlberg14547f72018-04-22 14:45:53 -0600227smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400228{
229#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000230 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400231
Joe Perchesf96637b2013-05-04 22:12:25 -0500232 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700233 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
234 shdr->ProcessId);
Ronnie Sahlberg14547f72018-04-22 14:45:53 -0600235 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
Steve French71992e622018-05-06 15:58:51 -0500236 server->ops->calc_smb_size(buf, server));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400237#endif
238}
239
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400240static bool
241smb2_need_neg(struct TCP_Server_Info *server)
242{
243 return server->max_read == 0;
244}
245
246static int
247smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
248{
249 int rc;
250 ses->server->CurrentMid = 0;
251 rc = SMB2_negotiate(xid, ses);
252 /* BB we probably don't need to retry with modern servers */
253 if (rc == -EAGAIN)
254 rc = -EHOSTDOWN;
255 return rc;
256}
257
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700258static unsigned int
259smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
260{
261 struct TCP_Server_Info *server = tcon->ses->server;
262 unsigned int wsize;
263
264 /* start with specified wsize, or default */
265 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
266 wsize = min_t(unsigned int, wsize, server->max_write);
Long Li09902f82017-11-22 17:38:39 -0700267#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700268 if (server->rdma) {
269 if (server->sign)
270 wsize = min_t(unsigned int,
271 wsize, server->smbd_conn->max_fragmented_send_size);
272 else
273 wsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700274 wsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700275 }
Long Li09902f82017-11-22 17:38:39 -0700276#endif
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400277 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
278 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700279
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700280 return wsize;
281}
282
283static unsigned int
Steve French3d621232018-09-25 15:33:47 -0500284smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
285{
286 struct TCP_Server_Info *server = tcon->ses->server;
287 unsigned int wsize;
288
289 /* start with specified wsize, or default */
290 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
291 wsize = min_t(unsigned int, wsize, server->max_write);
292#ifdef CONFIG_CIFS_SMB_DIRECT
293 if (server->rdma) {
294 if (server->sign)
295 wsize = min_t(unsigned int,
296 wsize, server->smbd_conn->max_fragmented_send_size);
297 else
298 wsize = min_t(unsigned int,
299 wsize, server->smbd_conn->max_readwrite_size);
300 }
301#endif
302 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
303 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
304
305 return wsize;
306}
307
308static unsigned int
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700309smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
310{
311 struct TCP_Server_Info *server = tcon->ses->server;
312 unsigned int rsize;
313
314 /* start with specified rsize, or default */
315 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
316 rsize = min_t(unsigned int, rsize, server->max_read);
Long Li09902f82017-11-22 17:38:39 -0700317#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700318 if (server->rdma) {
319 if (server->sign)
320 rsize = min_t(unsigned int,
321 rsize, server->smbd_conn->max_fragmented_recv_size);
322 else
323 rsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700324 rsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700325 }
Long Li09902f82017-11-22 17:38:39 -0700326#endif
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400327
328 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
329 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700330
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700331 return rsize;
332}
333
Steve French3d621232018-09-25 15:33:47 -0500334static unsigned int
335smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
336{
337 struct TCP_Server_Info *server = tcon->ses->server;
338 unsigned int rsize;
339
340 /* start with specified rsize, or default */
341 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
342 rsize = min_t(unsigned int, rsize, server->max_read);
343#ifdef CONFIG_CIFS_SMB_DIRECT
344 if (server->rdma) {
345 if (server->sign)
346 rsize = min_t(unsigned int,
347 rsize, server->smbd_conn->max_fragmented_recv_size);
348 else
349 rsize = min_t(unsigned int,
350 rsize, server->smbd_conn->max_readwrite_size);
351 }
352#endif
353
354 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
355 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
356
357 return rsize;
358}
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200359
360static int
361parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
362 size_t buf_len,
363 struct cifs_server_iface **iface_list,
364 size_t *iface_count)
365{
366 struct network_interface_info_ioctl_rsp *p;
367 struct sockaddr_in *addr4;
368 struct sockaddr_in6 *addr6;
369 struct iface_info_ipv4 *p4;
370 struct iface_info_ipv6 *p6;
371 struct cifs_server_iface *info;
372 ssize_t bytes_left;
373 size_t next = 0;
374 int nb_iface = 0;
375 int rc = 0;
376
377 *iface_list = NULL;
378 *iface_count = 0;
379
380 /*
381 * Fist pass: count and sanity check
382 */
383
384 bytes_left = buf_len;
385 p = buf;
386 while (bytes_left >= sizeof(*p)) {
387 nb_iface++;
388 next = le32_to_cpu(p->Next);
389 if (!next) {
390 bytes_left -= sizeof(*p);
391 break;
392 }
393 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
394 bytes_left -= next;
395 }
396
397 if (!nb_iface) {
398 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
399 rc = -EINVAL;
400 goto out;
401 }
402
403 if (bytes_left || p->Next)
404 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
405
406
407 /*
408 * Second pass: extract info to internal structure
409 */
410
411 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
412 if (!*iface_list) {
413 rc = -ENOMEM;
414 goto out;
415 }
416
417 info = *iface_list;
418 bytes_left = buf_len;
419 p = buf;
420 while (bytes_left >= sizeof(*p)) {
421 info->speed = le64_to_cpu(p->LinkSpeed);
422 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
423 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
424
425 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
426 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
427 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
428 le32_to_cpu(p->Capability));
429
430 switch (p->Family) {
431 /*
432 * The kernel and wire socket structures have the same
433 * layout and use network byte order but make the
434 * conversion explicit in case either one changes.
435 */
436 case INTERNETWORK:
437 addr4 = (struct sockaddr_in *)&info->sockaddr;
438 p4 = (struct iface_info_ipv4 *)p->Buffer;
439 addr4->sin_family = AF_INET;
440 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
441
442 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
443 addr4->sin_port = cpu_to_be16(CIFS_PORT);
444
445 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
446 &addr4->sin_addr);
447 break;
448 case INTERNETWORKV6:
449 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
450 p6 = (struct iface_info_ipv6 *)p->Buffer;
451 addr6->sin6_family = AF_INET6;
452 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
453
454 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
455 addr6->sin6_flowinfo = 0;
456 addr6->sin6_scope_id = 0;
457 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
458
459 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
460 &addr6->sin6_addr);
461 break;
462 default:
463 cifs_dbg(VFS,
464 "%s: skipping unsupported socket family\n",
465 __func__);
466 goto next_iface;
467 }
468
469 (*iface_count)++;
470 info++;
471next_iface:
472 next = le32_to_cpu(p->Next);
473 if (!next)
474 break;
475 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
476 bytes_left -= next;
477 }
478
479 if (!*iface_count) {
480 rc = -EINVAL;
481 goto out;
482 }
483
484out:
485 if (rc) {
486 kfree(*iface_list);
487 *iface_count = 0;
488 *iface_list = NULL;
489 }
490 return rc;
491}
492
493
Steve Frenchc481e9f2013-10-14 01:21:53 -0500494static int
495SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
496{
497 int rc;
498 unsigned int ret_data_len = 0;
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200499 struct network_interface_info_ioctl_rsp *out_buf = NULL;
500 struct cifs_server_iface *iface_list;
501 size_t iface_count;
502 struct cifs_ses *ses = tcon->ses;
Steve Frenchc481e9f2013-10-14 01:21:53 -0500503
504 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
505 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
506 NULL /* no data input */, 0 /* no data input */,
507 (char **)&out_buf, &ret_data_len);
Steve Frenchc3ed4402018-06-28 22:53:39 -0500508 if (rc == -EOPNOTSUPP) {
509 cifs_dbg(FYI,
510 "server does not support query network interfaces\n");
511 goto out;
512 } else if (rc != 0) {
Steve French9ffc5412014-10-16 15:13:14 -0500513 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200514 goto out;
Steve French9ffc5412014-10-16 15:13:14 -0500515 }
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200516
517 rc = parse_server_interfaces(out_buf, ret_data_len,
518 &iface_list, &iface_count);
519 if (rc)
520 goto out;
521
522 spin_lock(&ses->iface_lock);
523 kfree(ses->iface_list);
524 ses->iface_list = iface_list;
525 ses->iface_count = iface_count;
526 ses->iface_last_update = jiffies;
527 spin_unlock(&ses->iface_lock);
528
529out:
Steve French24df1482016-09-29 04:20:23 -0500530 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500531 return rc;
532}
Steve Frenchc481e9f2013-10-14 01:21:53 -0500533
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000534static void
535smb2_close_cached_fid(struct kref *ref)
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000536{
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000537 struct cached_fid *cfid = container_of(ref, struct cached_fid,
538 refcount);
539
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000540 if (cfid->is_valid) {
541 cifs_dbg(FYI, "clear cached root file handle\n");
542 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
543 cfid->fid->volatile_fid);
544 cfid->is_valid = false;
545 }
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000546}
547
548void close_shroot(struct cached_fid *cfid)
549{
550 mutex_lock(&cfid->fid_mutex);
551 kref_put(&cfid->refcount, smb2_close_cached_fid);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000552 mutex_unlock(&cfid->fid_mutex);
553}
554
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000555void
556smb2_cached_lease_break(struct work_struct *work)
557{
558 struct cached_fid *cfid = container_of(work,
559 struct cached_fid, lease_break);
560
561 close_shroot(cfid);
562}
563
Steve French3d4ef9a2018-04-25 22:19:09 -0500564/*
565 * Open the directory at the root of a share
566 */
567int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
568{
569 struct cifs_open_parms oparams;
570 int rc;
571 __le16 srch_path = 0; /* Null - since an open of top of share */
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000572 u8 oplock = SMB2_OPLOCK_LEVEL_II;
Steve French3d4ef9a2018-04-25 22:19:09 -0500573
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000574 mutex_lock(&tcon->crfid.fid_mutex);
575 if (tcon->crfid.is_valid) {
Steve French3d4ef9a2018-04-25 22:19:09 -0500576 cifs_dbg(FYI, "found a cached root file handle\n");
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000577 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000578 kref_get(&tcon->crfid.refcount);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000579 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500580 return 0;
581 }
582
583 oparams.tcon = tcon;
584 oparams.create_options = 0;
585 oparams.desired_access = FILE_READ_ATTRIBUTES;
586 oparams.disposition = FILE_OPEN;
587 oparams.fid = pfid;
588 oparams.reconnect = false;
589
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000590 rc = SMB2_open(xid, &oparams, &srch_path, &oplock, NULL, NULL, NULL);
Steve French3d4ef9a2018-04-25 22:19:09 -0500591 if (rc == 0) {
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000592 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
593 tcon->crfid.tcon = tcon;
594 tcon->crfid.is_valid = true;
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000595 kref_init(&tcon->crfid.refcount);
596 kref_get(&tcon->crfid.refcount);
Steve French3d4ef9a2018-04-25 22:19:09 -0500597 }
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000598 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500599 return rc;
600}
601
Steve French34f62642013-10-09 02:07:00 -0500602static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500603smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
604{
605 int rc;
606 __le16 srch_path = 0; /* Null - open root of share */
607 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
608 struct cifs_open_parms oparms;
609 struct cifs_fid fid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500610 bool no_cached_open = tcon->nohandlecache;
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500611
612 oparms.tcon = tcon;
613 oparms.desired_access = FILE_READ_ATTRIBUTES;
614 oparms.disposition = FILE_OPEN;
615 oparms.create_options = 0;
616 oparms.fid = &fid;
617 oparms.reconnect = false;
618
Steve French3d4ef9a2018-04-25 22:19:09 -0500619 if (no_cached_open)
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000620 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
621 NULL);
Steve French3d4ef9a2018-04-25 22:19:09 -0500622 else
623 rc = open_shroot(xid, tcon, &fid);
624
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500625 if (rc)
626 return;
627
Steve Frenchc481e9f2013-10-14 01:21:53 -0500628 SMB3_request_interfaces(xid, tcon);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500629
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500630 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
631 FS_ATTRIBUTE_INFORMATION);
632 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
633 FS_DEVICE_INFORMATION);
634 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steve French21ba3842018-06-24 23:18:52 -0500635 FS_VOLUME_INFORMATION);
636 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500637 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
Steve French3d4ef9a2018-04-25 22:19:09 -0500638 if (no_cached_open)
639 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000640 else
641 close_shroot(&tcon->crfid);
642
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500643 return;
644}
645
646static void
Steve French34f62642013-10-09 02:07:00 -0500647smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
648{
649 int rc;
650 __le16 srch_path = 0; /* Null - open root of share */
651 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
652 struct cifs_open_parms oparms;
653 struct cifs_fid fid;
654
655 oparms.tcon = tcon;
656 oparms.desired_access = FILE_READ_ATTRIBUTES;
657 oparms.disposition = FILE_OPEN;
658 oparms.create_options = 0;
659 oparms.fid = &fid;
660 oparms.reconnect = false;
661
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000662 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
Steve French34f62642013-10-09 02:07:00 -0500663 if (rc)
664 return;
665
Steven French21671142013-10-09 13:36:35 -0500666 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
667 FS_ATTRIBUTE_INFORMATION);
668 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
669 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500670 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
671 return;
672}
673
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400674static int
675smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
676 struct cifs_sb_info *cifs_sb, const char *full_path)
677{
678 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400679 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700680 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400681 struct cifs_open_parms oparms;
682 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400683
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000684 if ((*full_path == 0) && tcon->crfid.is_valid)
Steve French3d4ef9a2018-04-25 22:19:09 -0500685 return 0;
686
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400687 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
688 if (!utf16_path)
689 return -ENOMEM;
690
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400691 oparms.tcon = tcon;
692 oparms.desired_access = FILE_READ_ATTRIBUTES;
693 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -0500694 if (backup_cred(cifs_sb))
695 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
696 else
697 oparms.create_options = 0;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400698 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400699 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400700
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000701 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400702 if (rc) {
703 kfree(utf16_path);
704 return rc;
705 }
706
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400707 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400708 kfree(utf16_path);
709 return rc;
710}
711
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400712static int
713smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
714 struct cifs_sb_info *cifs_sb, const char *full_path,
715 u64 *uniqueid, FILE_ALL_INFO *data)
716{
717 *uniqueid = le64_to_cpu(data->IndexNumber);
718 return 0;
719}
720
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700721static int
722smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
723 struct cifs_fid *fid, FILE_ALL_INFO *data)
724{
725 int rc;
726 struct smb2_file_all_info *smb2_data;
727
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400728 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700729 GFP_KERNEL);
730 if (smb2_data == NULL)
731 return -ENOMEM;
732
733 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
734 smb2_data);
735 if (!rc)
736 move_smb2_info_to_cifs(data, smb2_data);
737 kfree(smb2_data);
738 return rc;
739}
740
Arnd Bergmann1368f152017-09-05 11:24:15 +0200741#ifdef CONFIG_CIFS_XATTR
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000742static ssize_t
743move_smb2_ea_to_cifs(char *dst, size_t dst_size,
744 struct smb2_file_full_ea_info *src, size_t src_size,
745 const unsigned char *ea_name)
746{
747 int rc = 0;
748 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
749 char *name, *value;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +1000750 size_t buf_size = dst_size;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000751 size_t name_len, value_len, user_name_len;
752
753 while (src_size > 0) {
754 name = &src->ea_data[0];
755 name_len = (size_t)src->ea_name_length;
756 value = &src->ea_data[src->ea_name_length + 1];
757 value_len = (size_t)le16_to_cpu(src->ea_value_length);
758
759 if (name_len == 0) {
760 break;
761 }
762
763 if (src_size < 8 + name_len + 1 + value_len) {
764 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
765 rc = -EIO;
766 goto out;
767 }
768
769 if (ea_name) {
770 if (ea_name_len == name_len &&
771 memcmp(ea_name, name, name_len) == 0) {
772 rc = value_len;
773 if (dst_size == 0)
774 goto out;
775 if (dst_size < value_len) {
776 rc = -ERANGE;
777 goto out;
778 }
779 memcpy(dst, value, value_len);
780 goto out;
781 }
782 } else {
783 /* 'user.' plus a terminating null */
784 user_name_len = 5 + 1 + name_len;
785
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +1000786 if (buf_size == 0) {
787 /* skip copy - calc size only */
788 rc += user_name_len;
789 } else if (dst_size >= user_name_len) {
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000790 dst_size -= user_name_len;
791 memcpy(dst, "user.", 5);
792 dst += 5;
793 memcpy(dst, src->ea_data, name_len);
794 dst += name_len;
795 *dst = 0;
796 ++dst;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +1000797 rc += user_name_len;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000798 } else {
799 /* stop before overrun buffer */
800 rc = -ERANGE;
801 break;
802 }
803 }
804
805 if (!src->next_entry_offset)
806 break;
807
808 if (src_size < le32_to_cpu(src->next_entry_offset)) {
809 /* stop before overrun buffer */
810 rc = -ERANGE;
811 break;
812 }
813 src_size -= le32_to_cpu(src->next_entry_offset);
814 src = (void *)((char *)src +
815 le32_to_cpu(src->next_entry_offset));
816 }
817
818 /* didn't find the named attribute */
819 if (ea_name)
820 rc = -ENODATA;
821
822out:
823 return (ssize_t)rc;
824}
825
826static ssize_t
827smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
828 const unsigned char *path, const unsigned char *ea_name,
829 char *ea_data, size_t buf_size,
830 struct cifs_sb_info *cifs_sb)
831{
832 int rc;
833 __le16 *utf16_path;
834 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
835 struct cifs_open_parms oparms;
836 struct cifs_fid fid;
837 struct smb2_file_full_ea_info *smb2_data;
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000838 int ea_buf_size = SMB2_MIN_EA_BUF;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000839
840 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
841 if (!utf16_path)
842 return -ENOMEM;
843
844 oparms.tcon = tcon;
845 oparms.desired_access = FILE_READ_EA;
846 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -0500847 if (backup_cred(cifs_sb))
848 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
849 else
850 oparms.create_options = 0;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000851 oparms.fid = &fid;
852 oparms.reconnect = false;
853
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000854 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000855 kfree(utf16_path);
856 if (rc) {
857 cifs_dbg(FYI, "open failed rc=%d\n", rc);
858 return rc;
859 }
860
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +1000861 while (1) {
862 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
863 if (smb2_data == NULL) {
864 SMB2_close(xid, tcon, fid.persistent_fid,
865 fid.volatile_fid);
866 return -ENOMEM;
867 }
868
869 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
870 fid.volatile_fid,
871 ea_buf_size, smb2_data);
872
873 if (rc != -E2BIG)
874 break;
875
876 kfree(smb2_data);
877 ea_buf_size <<= 1;
878
879 if (ea_buf_size > SMB2_MAX_EA_BUF) {
880 cifs_dbg(VFS, "EA size is too large\n");
881 SMB2_close(xid, tcon, fid.persistent_fid,
882 fid.volatile_fid);
883 return -ENOMEM;
884 }
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000885 }
886
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000887 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
888
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300889 /*
890 * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
891 * not an error. Otherwise, the specified ea_name was not found.
892 */
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000893 if (!rc)
894 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
895 SMB2_MAX_EA_BUF, ea_name);
Paulo Alcantaraae2cd7f2018-05-04 11:25:26 -0300896 else if (!ea_name && rc == -ENODATA)
897 rc = 0;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000898
899 kfree(smb2_data);
900 return rc;
901}
902
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000903
904static int
905smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
906 const char *path, const char *ea_name, const void *ea_value,
907 const __u16 ea_value_len, const struct nls_table *nls_codepage,
908 struct cifs_sb_info *cifs_sb)
909{
Ronnie Sahlberg0967e542018-11-06 22:52:43 +1000910 struct cifs_ses *ses = tcon->ses;
911 struct TCP_Server_Info *server = ses->server;
912 __le16 *utf16_path = NULL;
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000913 int ea_name_len = strlen(ea_name);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +1000914 int flags = 0;
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000915 int len;
Ronnie Sahlberg0967e542018-11-06 22:52:43 +1000916 struct smb_rqst rqst[3];
917 int resp_buftype[3];
918 struct kvec rsp_iov[3];
919 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
920 struct cifs_open_parms oparms;
921 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
922 struct cifs_fid fid;
923 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
924 unsigned int size[1];
925 void *data[1];
926 struct smb2_file_full_ea_info *ea = NULL;
927 struct kvec close_iov[1];
928 int rc;
929
930 if (smb3_encryption_required(tcon))
931 flags |= CIFS_TRANSFORM_REQ;
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000932
933 if (ea_name_len > 255)
934 return -EINVAL;
935
936 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
937 if (!utf16_path)
938 return -ENOMEM;
939
Ronnie Sahlberg0967e542018-11-06 22:52:43 +1000940 memset(rqst, 0, sizeof(rqst));
941 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
942 memset(rsp_iov, 0, sizeof(rsp_iov));
943
944 /* Open */
945 memset(&open_iov, 0, sizeof(open_iov));
946 rqst[0].rq_iov = open_iov;
947 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
948
949 memset(&oparms, 0, sizeof(oparms));
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000950 oparms.tcon = tcon;
951 oparms.desired_access = FILE_WRITE_EA;
952 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -0500953 if (backup_cred(cifs_sb))
954 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
955 else
956 oparms.create_options = 0;
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000957 oparms.fid = &fid;
958 oparms.reconnect = false;
959
Ronnie Sahlberg0967e542018-11-06 22:52:43 +1000960 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
961 if (rc)
962 goto sea_exit;
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -0600963 smb2_set_next_command(ses->server, &rqst[0], 0);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +1000964
965
966 /* Set Info */
967 memset(&si_iov, 0, sizeof(si_iov));
968 rqst[1].rq_iov = si_iov;
969 rqst[1].rq_nvec = 1;
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000970
971 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
972 ea = kzalloc(len, GFP_KERNEL);
973 if (ea == NULL) {
Ronnie Sahlberg0967e542018-11-06 22:52:43 +1000974 rc = -ENOMEM;
975 goto sea_exit;
Ronnie Sahlberg55175542017-08-24 11:24:56 +1000976 }
977
978 ea->ea_name_length = ea_name_len;
979 ea->ea_value_length = cpu_to_le16(ea_value_len);
980 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
981 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
982
Ronnie Sahlberg0967e542018-11-06 22:52:43 +1000983 size[0] = len;
984 data[0] = ea;
985
986 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
987 COMPOUND_FID, current->tgid,
988 FILE_FULL_EA_INFORMATION,
989 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -0600990 smb2_set_next_command(server, &rqst[1], 0);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +1000991 smb2_set_related(&rqst[1]);
992
993
994 /* Close */
995 memset(&close_iov, 0, sizeof(close_iov));
996 rqst[2].rq_iov = close_iov;
997 rqst[2].rq_nvec = 1;
998 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
999 smb2_set_related(&rqst[2]);
1000
1001 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1002 resp_buftype, rsp_iov);
1003
1004 sea_exit:
Paulo Alcantara6aa0c112018-07-04 14:16:16 -03001005 kfree(ea);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001006 kfree(utf16_path);
1007 SMB2_open_free(&rqst[0]);
1008 SMB2_set_info_free(&rqst[1]);
1009 SMB2_close_free(&rqst[2]);
1010 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1011 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1012 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001013 return rc;
1014}
Arnd Bergmann1368f152017-09-05 11:24:15 +02001015#endif
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001016
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001017static bool
1018smb2_can_echo(struct TCP_Server_Info *server)
1019{
1020 return server->echoes;
1021}
1022
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001023static void
1024smb2_clear_stats(struct cifs_tcon *tcon)
1025{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001026 int i;
1027 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1028 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1029 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1030 }
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001031}
1032
1033static void
Steve French769ee6a2013-06-19 14:15:30 -05001034smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1035{
1036 seq_puts(m, "\n\tShare Capabilities:");
1037 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1038 seq_puts(m, " DFS,");
1039 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1040 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1041 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1042 seq_puts(m, " SCALEOUT,");
1043 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1044 seq_puts(m, " CLUSTER,");
1045 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1046 seq_puts(m, " ASYMMETRIC,");
1047 if (tcon->capabilities == 0)
1048 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001049 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1050 seq_puts(m, " Aligned,");
1051 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1052 seq_puts(m, " Partition Aligned,");
1053 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1054 seq_puts(m, " SSD,");
1055 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1056 seq_puts(m, " TRIM-support,");
1057
Steve French769ee6a2013-06-19 14:15:30 -05001058 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steve Frenche0386e42018-05-20 01:27:03 -05001059 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001060 if (tcon->perf_sector_size)
1061 seq_printf(m, "\tOptimal sector size: 0x%x",
1062 tcon->perf_sector_size);
Steve Frenche0386e42018-05-20 01:27:03 -05001063 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
Steve French769ee6a2013-06-19 14:15:30 -05001064}
1065
1066static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001067smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1068{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001069 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1070 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
Steve French1995d282018-07-27 15:14:04 -05001071
1072 /*
1073 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1074 * totals (requests sent) since those SMBs are per-session not per tcon
1075 */
Steve French52ce1ac2018-07-31 01:46:47 -05001076 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1077 (long long)(tcon->bytes_read),
1078 (long long)(tcon->bytes_written));
Steve Frenchfae80442018-10-19 17:14:32 -05001079 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1080 atomic_read(&tcon->num_local_opens),
1081 atomic_read(&tcon->num_remote_opens));
Steve French1995d282018-07-27 15:14:04 -05001082 seq_printf(m, "\nTreeConnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001083 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1084 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001085 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001086 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1087 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001088 seq_printf(m, "\nCreates: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001089 atomic_read(&sent[SMB2_CREATE_HE]),
1090 atomic_read(&failed[SMB2_CREATE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001091 seq_printf(m, "\nCloses: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001092 atomic_read(&sent[SMB2_CLOSE_HE]),
1093 atomic_read(&failed[SMB2_CLOSE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001094 seq_printf(m, "\nFlushes: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001095 atomic_read(&sent[SMB2_FLUSH_HE]),
1096 atomic_read(&failed[SMB2_FLUSH_HE]));
Steve French1995d282018-07-27 15:14:04 -05001097 seq_printf(m, "\nReads: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001098 atomic_read(&sent[SMB2_READ_HE]),
1099 atomic_read(&failed[SMB2_READ_HE]));
Steve French1995d282018-07-27 15:14:04 -05001100 seq_printf(m, "\nWrites: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001101 atomic_read(&sent[SMB2_WRITE_HE]),
1102 atomic_read(&failed[SMB2_WRITE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001103 seq_printf(m, "\nLocks: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001104 atomic_read(&sent[SMB2_LOCK_HE]),
1105 atomic_read(&failed[SMB2_LOCK_HE]));
Steve French1995d282018-07-27 15:14:04 -05001106 seq_printf(m, "\nIOCTLs: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001107 atomic_read(&sent[SMB2_IOCTL_HE]),
1108 atomic_read(&failed[SMB2_IOCTL_HE]));
Steve French1995d282018-07-27 15:14:04 -05001109 seq_printf(m, "\nQueryDirectories: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001110 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1111 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001112 seq_printf(m, "\nChangeNotifies: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001113 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1114 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001115 seq_printf(m, "\nQueryInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001116 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1117 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
Steve French1995d282018-07-27 15:14:04 -05001118 seq_printf(m, "\nSetInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001119 atomic_read(&sent[SMB2_SET_INFO_HE]),
1120 atomic_read(&failed[SMB2_SET_INFO_HE]));
1121 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1122 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1123 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001124}
1125
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001126static void
1127smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1128{
David Howells2b0143b2015-03-17 22:25:59 +00001129 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001130 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1131
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001132 cfile->fid.persistent_fid = fid->persistent_fid;
1133 cfile->fid.volatile_fid = fid->volatile_fid;
Steve Frenchdfe33f92018-10-30 19:50:31 -05001134#ifdef CONFIG_CIFS_DEBUG2
1135 cfile->fid.mid = fid->mid;
1136#endif /* CIFS_DEBUG2 */
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001137 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1138 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001139 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +02001140 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001141}
1142
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001143static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001144smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1145 struct cifs_fid *fid)
1146{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001147 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001148}
1149
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001150static int
Steve French41c13582013-11-14 00:05:36 -06001151SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1152 u64 persistent_fid, u64 volatile_fid,
1153 struct copychunk_ioctl *pcchunk)
1154{
1155 int rc;
1156 unsigned int ret_data_len;
1157 struct resume_key_req *res_key;
1158
1159 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1160 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1161 NULL, 0 /* no input */,
1162 (char **)&res_key, &ret_data_len);
1163
1164 if (rc) {
1165 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1166 goto req_res_key_exit;
1167 }
1168 if (ret_data_len < sizeof(struct resume_key_req)) {
1169 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1170 rc = -EINVAL;
1171 goto req_res_key_exit;
1172 }
1173 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1174
1175req_res_key_exit:
1176 kfree(res_key);
1177 return rc;
1178}
1179
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001180static int
1181smb2_ioctl_query_info(const unsigned int xid,
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001182 struct cifs_tcon *tcon,
1183 __le16 *path, int is_dir,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001184 unsigned long p)
1185{
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001186 struct cifs_ses *ses = tcon->ses;
1187 char __user *arg = (char __user *)p;
1188 struct smb_query_info qi;
1189 struct smb_query_info __user *pqi;
1190 int rc = 0;
1191 int flags = 0;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001192 struct smb2_query_info_rsp *rsp = NULL;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001193 void *buffer = NULL;
1194 struct smb_rqst rqst[3];
1195 int resp_buftype[3];
1196 struct kvec rsp_iov[3];
1197 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1198 struct cifs_open_parms oparms;
1199 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1200 struct cifs_fid fid;
1201 struct kvec qi_iov[1];
1202 struct kvec close_iov[1];
1203
1204 memset(rqst, 0, sizeof(rqst));
1205 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1206 memset(rsp_iov, 0, sizeof(rsp_iov));
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001207
1208 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1209 return -EFAULT;
1210
1211 if (qi.output_buffer_length > 1024)
1212 return -EINVAL;
1213
1214 if (!ses || !(ses->server))
1215 return -EIO;
1216
1217 if (smb3_encryption_required(tcon))
1218 flags |= CIFS_TRANSFORM_REQ;
1219
1220 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1221 if (buffer == NULL)
1222 return -ENOMEM;
1223
1224 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1225 qi.output_buffer_length)) {
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001226 rc = -EFAULT;
1227 goto iqinf_exit;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001228 }
1229
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001230 /* Open */
1231 memset(&open_iov, 0, sizeof(open_iov));
1232 rqst[0].rq_iov = open_iov;
1233 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001234
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001235 memset(&oparms, 0, sizeof(oparms));
1236 oparms.tcon = tcon;
1237 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1238 oparms.disposition = FILE_OPEN;
1239 if (is_dir)
1240 oparms.create_options = CREATE_NOT_FILE;
1241 else
1242 oparms.create_options = CREATE_NOT_DIR;
1243 oparms.fid = &fid;
1244 oparms.reconnect = false;
1245
1246 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1247 if (rc)
1248 goto iqinf_exit;
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06001249 smb2_set_next_command(ses->server, &rqst[0], 0);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001250
1251 /* Query */
1252 memset(&qi_iov, 0, sizeof(qi_iov));
1253 rqst[1].rq_iov = qi_iov;
1254 rqst[1].rq_nvec = 1;
1255
1256 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001257 qi.file_info_class, qi.info_type,
1258 qi.additional_information,
1259 qi.input_buffer_length,
1260 qi.output_buffer_length, buffer);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001261 if (rc)
1262 goto iqinf_exit;
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06001263 smb2_set_next_command(ses->server, &rqst[1], 0);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001264 smb2_set_related(&rqst[1]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001265
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001266 /* Close */
1267 memset(&close_iov, 0, sizeof(close_iov));
1268 rqst[2].rq_iov = close_iov;
1269 rqst[2].rq_nvec = 1;
1270
1271 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001272 if (rc)
1273 goto iqinf_exit;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001274 smb2_set_related(&rqst[2]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001275
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001276 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1277 resp_buftype, rsp_iov);
1278 if (rc)
1279 goto iqinf_exit;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001280 pqi = (struct smb_query_info __user *)arg;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001281 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001282 if (le32_to_cpu(rsp->OutputBufferLength) < qi.input_buffer_length)
1283 qi.input_buffer_length = le32_to_cpu(rsp->OutputBufferLength);
1284 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1285 sizeof(qi.input_buffer_length))) {
1286 rc = -EFAULT;
1287 goto iqinf_exit;
1288 }
1289 if (copy_to_user(pqi + 1, rsp->Buffer, qi.input_buffer_length)) {
1290 rc = -EFAULT;
1291 goto iqinf_exit;
1292 }
1293
1294 iqinf_exit:
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001295 kfree(buffer);
1296 SMB2_open_free(&rqst[0]);
1297 SMB2_query_info_free(&rqst[1]);
1298 SMB2_close_free(&rqst[2]);
1299 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1300 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1301 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001302 return rc;
1303}
1304
Sachin Prabhu620d8742017-02-10 16:03:51 +05301305static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -05001306smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -06001307 struct cifsFileInfo *srcfile,
1308 struct cifsFileInfo *trgtfile, u64 src_off,
1309 u64 len, u64 dest_off)
1310{
1311 int rc;
1312 unsigned int ret_data_len;
1313 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -06001314 struct copychunk_ioctl_rsp *retbuf = NULL;
1315 struct cifs_tcon *tcon;
1316 int chunks_copied = 0;
1317 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +05301318 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -06001319
1320 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1321
1322 if (pcchunk == NULL)
1323 return -ENOMEM;
1324
Sachin Prabhu312bbc52017-04-04 02:12:04 -05001325 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
Steve French41c13582013-11-14 00:05:36 -06001326 /* Request a key from the server to identify the source of the copy */
1327 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1328 srcfile->fid.persistent_fid,
1329 srcfile->fid.volatile_fid, pcchunk);
1330
1331 /* Note: request_res_key sets res_key null only if rc !=0 */
1332 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -06001333 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001334
1335 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -08001336 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -06001337 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -06001338 pcchunk->Reserved2 = 0;
1339
Steve French9bf0c9c2013-11-16 18:05:28 -06001340 tcon = tlink_tcon(trgtfile->tlink);
1341
1342 while (len > 0) {
1343 pcchunk->SourceOffset = cpu_to_le64(src_off);
1344 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1345 pcchunk->Length =
1346 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1347
1348 /* Request server copy to target from src identified by key */
1349 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -06001350 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001351 true /* is_fsctl */, (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -06001352 sizeof(struct copychunk_ioctl), (char **)&retbuf,
1353 &ret_data_len);
1354 if (rc == 0) {
1355 if (ret_data_len !=
1356 sizeof(struct copychunk_ioctl_rsp)) {
1357 cifs_dbg(VFS, "invalid cchunk response size\n");
1358 rc = -EIO;
1359 goto cchunk_out;
1360 }
1361 if (retbuf->TotalBytesWritten == 0) {
1362 cifs_dbg(FYI, "no bytes copied\n");
1363 rc = -EIO;
1364 goto cchunk_out;
1365 }
1366 /*
1367 * Check if server claimed to write more than we asked
1368 */
1369 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1370 le32_to_cpu(pcchunk->Length)) {
1371 cifs_dbg(VFS, "invalid copy chunk response\n");
1372 rc = -EIO;
1373 goto cchunk_out;
1374 }
1375 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1376 cifs_dbg(VFS, "invalid num chunks written\n");
1377 rc = -EIO;
1378 goto cchunk_out;
1379 }
1380 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -06001381
Sachin Prabhu620d8742017-02-10 16:03:51 +05301382 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1383 src_off += bytes_written;
1384 dest_off += bytes_written;
1385 len -= bytes_written;
1386 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001387
Sachin Prabhu620d8742017-02-10 16:03:51 +05301388 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -06001389 le32_to_cpu(retbuf->ChunksWritten),
1390 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +05301391 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -06001392 } else if (rc == -EINVAL) {
1393 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1394 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001395
Steve French9bf0c9c2013-11-16 18:05:28 -06001396 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1397 le32_to_cpu(retbuf->ChunksWritten),
1398 le32_to_cpu(retbuf->ChunkBytesWritten),
1399 le32_to_cpu(retbuf->TotalBytesWritten));
1400
1401 /*
1402 * Check if this is the first request using these sizes,
1403 * (ie check if copy succeed once with original sizes
1404 * and check if the server gave us different sizes after
1405 * we already updated max sizes on previous request).
1406 * if not then why is the server returning an error now
1407 */
1408 if ((chunks_copied != 0) || chunk_sizes_updated)
1409 goto cchunk_out;
1410
1411 /* Check that server is not asking us to grow size */
1412 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1413 tcon->max_bytes_chunk)
1414 tcon->max_bytes_chunk =
1415 le32_to_cpu(retbuf->ChunkBytesWritten);
1416 else
1417 goto cchunk_out; /* server gave us bogus size */
1418
1419 /* No need to change MaxChunks since already set to 1 */
1420 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +00001421 } else
1422 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -06001423 }
1424
1425cchunk_out:
Steve French41c13582013-11-14 00:05:36 -06001426 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -05001427 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +05301428 if (rc)
1429 return rc;
1430 else
1431 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001432}
1433
1434static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001435smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1436 struct cifs_fid *fid)
1437{
1438 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1439}
1440
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001441static unsigned int
1442smb2_read_data_offset(char *buf)
1443{
1444 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1445 return rsp->DataOffset;
1446}
1447
1448static unsigned int
Long Li74dcf412017-11-22 17:38:46 -07001449smb2_read_data_length(char *buf, bool in_remaining)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001450{
1451 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Long Li74dcf412017-11-22 17:38:46 -07001452
1453 if (in_remaining)
1454 return le32_to_cpu(rsp->DataRemaining);
1455
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001456 return le32_to_cpu(rsp->DataLength);
1457}
1458
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001459
1460static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001461smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001462 struct cifs_io_parms *parms, unsigned int *bytes_read,
1463 char **buf, int *buf_type)
1464{
Steve Frenchdb8b6312014-09-22 05:13:55 -05001465 parms->persistent_fid = pfid->persistent_fid;
1466 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001467 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1468}
1469
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001470static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001471smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001472 struct cifs_io_parms *parms, unsigned int *written,
1473 struct kvec *iov, unsigned long nr_segs)
1474{
1475
Steve Frenchdb8b6312014-09-22 05:13:55 -05001476 parms->persistent_fid = pfid->persistent_fid;
1477 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001478 return SMB2_write(xid, parms, written, iov, nr_segs);
1479}
1480
Steve Frenchd43cc792014-08-13 17:16:29 -05001481/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1482static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1483 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1484{
1485 struct cifsInodeInfo *cifsi;
1486 int rc;
1487
1488 cifsi = CIFS_I(inode);
1489
1490 /* if file already sparse don't bother setting sparse again */
1491 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1492 return true; /* already sparse */
1493
1494 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1495 return true; /* already not sparse */
1496
1497 /*
1498 * Can't check for sparse support on share the usual way via the
1499 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1500 * since Samba server doesn't set the flag on the share, yet
1501 * supports the set sparse FSCTL and returns sparse correctly
1502 * in the file attributes. If we fail setting sparse though we
1503 * mark that server does not support sparse files for this share
1504 * to avoid repeatedly sending the unsupported fsctl to server
1505 * if the file is repeatedly extended.
1506 */
1507 if (tcon->broken_sparse_sup)
1508 return false;
1509
1510 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1511 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001512 true /* is_fctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001513 &setsparse, 1, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -05001514 if (rc) {
1515 tcon->broken_sparse_sup = true;
1516 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1517 return false;
1518 }
1519
1520 if (setsparse)
1521 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1522 else
1523 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1524
1525 return true;
1526}
1527
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001528static int
1529smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1530 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1531{
1532 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05001533 struct inode *inode;
1534
1535 /*
1536 * If extending file more than one page make sparse. Many Linux fs
1537 * make files sparse by default when extending via ftruncate
1538 */
David Howells2b0143b2015-03-17 22:25:59 +00001539 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05001540
1541 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05001542 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05001543
Steve Frenchd43cc792014-08-13 17:16:29 -05001544 /* whether set sparse succeeds or not, extend the file */
1545 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05001546 }
1547
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001548 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10001549 cfile->fid.volatile_fid, cfile->pid, &eof);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001550}
1551
Steve French02b16662015-06-27 21:18:36 -07001552static int
1553smb2_duplicate_extents(const unsigned int xid,
1554 struct cifsFileInfo *srcfile,
1555 struct cifsFileInfo *trgtfile, u64 src_off,
1556 u64 len, u64 dest_off)
1557{
1558 int rc;
1559 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -07001560 struct duplicate_extents_to_file dup_ext_buf;
1561 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1562
1563 /* server fileays advertise duplicate extent support with this flag */
1564 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1565 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1566 return -EOPNOTSUPP;
1567
1568 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1569 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1570 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1571 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1572 dup_ext_buf.ByteCount = cpu_to_le64(len);
1573 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1574 src_off, dest_off, len);
1575
1576 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1577 if (rc)
1578 goto duplicate_extents_out;
1579
1580 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1581 trgtfile->fid.volatile_fid,
1582 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001583 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001584 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07001585 sizeof(struct duplicate_extents_to_file),
Steve French24df1482016-09-29 04:20:23 -05001586 NULL,
Steve French02b16662015-06-27 21:18:36 -07001587 &ret_data_len);
1588
1589 if (ret_data_len > 0)
1590 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1591
1592duplicate_extents_out:
1593 return rc;
1594}
Steve French02b16662015-06-27 21:18:36 -07001595
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001596static int
Steve French64a5cfa2013-10-14 15:31:32 -05001597smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1598 struct cifsFileInfo *cfile)
1599{
1600 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1601 cfile->fid.volatile_fid);
1602}
1603
1604static int
Steve Frenchb3152e22015-06-24 03:17:02 -05001605smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1606 struct cifsFileInfo *cfile)
1607{
1608 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05001609 unsigned int ret_data_len;
1610
1611 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1612 integr_info.Flags = 0;
1613 integr_info.Reserved = 0;
1614
1615 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1616 cfile->fid.volatile_fid,
1617 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001618 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001619 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05001620 sizeof(struct fsctl_set_integrity_information_req),
Steve French24df1482016-09-29 04:20:23 -05001621 NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05001622 &ret_data_len);
1623
1624}
1625
Steve Frenche02789a2018-08-09 14:33:12 -05001626/* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1627#define GMT_TOKEN_SIZE 50
1628
1629/*
1630 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1631 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1632 */
Steve Frenchb3152e22015-06-24 03:17:02 -05001633static int
Steve French834170c2016-09-30 21:14:26 -05001634smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1635 struct cifsFileInfo *cfile, void __user *ioc_buf)
1636{
1637 char *retbuf = NULL;
1638 unsigned int ret_data_len = 0;
1639 int rc;
1640 struct smb_snapshot_array snapshot_in;
1641
1642 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1643 cfile->fid.volatile_fid,
1644 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001645 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001646 NULL, 0 /* no input data */,
Steve French834170c2016-09-30 21:14:26 -05001647 (char **)&retbuf,
1648 &ret_data_len);
1649 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1650 rc, ret_data_len);
1651 if (rc)
1652 return rc;
1653
1654 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1655 /* Fixup buffer */
1656 if (copy_from_user(&snapshot_in, ioc_buf,
1657 sizeof(struct smb_snapshot_array))) {
1658 rc = -EFAULT;
1659 kfree(retbuf);
1660 return rc;
1661 }
Steve French834170c2016-09-30 21:14:26 -05001662
Steve Frenche02789a2018-08-09 14:33:12 -05001663 /*
1664 * Check for min size, ie not large enough to fit even one GMT
1665 * token (snapshot). On the first ioctl some users may pass in
1666 * smaller size (or zero) to simply get the size of the array
1667 * so the user space caller can allocate sufficient memory
1668 * and retry the ioctl again with larger array size sufficient
1669 * to hold all of the snapshot GMT tokens on the second try.
1670 */
1671 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1672 ret_data_len = sizeof(struct smb_snapshot_array);
1673
1674 /*
1675 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1676 * the snapshot array (of 50 byte GMT tokens) each
1677 * representing an available previous version of the data
1678 */
1679 if (ret_data_len > (snapshot_in.snapshot_array_size +
1680 sizeof(struct smb_snapshot_array)))
1681 ret_data_len = snapshot_in.snapshot_array_size +
1682 sizeof(struct smb_snapshot_array);
Steve French834170c2016-09-30 21:14:26 -05001683
1684 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1685 rc = -EFAULT;
1686 }
1687
1688 kfree(retbuf);
1689 return rc;
1690}
1691
1692static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001693smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1694 const char *path, struct cifs_sb_info *cifs_sb,
1695 struct cifs_fid *fid, __u16 search_flags,
1696 struct cifs_search_info *srch_inf)
1697{
1698 __le16 *utf16_path;
1699 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001700 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001701 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001702
1703 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1704 if (!utf16_path)
1705 return -ENOMEM;
1706
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001707 oparms.tcon = tcon;
1708 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1709 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -05001710 if (backup_cred(cifs_sb))
1711 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1712 else
1713 oparms.create_options = 0;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001714 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001715 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001716
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001717 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001718 kfree(utf16_path);
1719 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001720 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001721 return rc;
1722 }
1723
1724 srch_inf->entries_in_buffer = 0;
Aurelien Aptel05957512018-05-17 16:35:07 +02001725 srch_inf->index_of_last_entry = 2;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001726
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001727 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1728 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001729 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001730 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001731 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001732 }
1733 return rc;
1734}
1735
1736static int
1737smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1738 struct cifs_fid *fid, __u16 search_flags,
1739 struct cifs_search_info *srch_inf)
1740{
1741 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1742 fid->volatile_fid, 0, srch_inf);
1743}
1744
1745static int
1746smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1747 struct cifs_fid *fid)
1748{
1749 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1750}
1751
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001752/*
1753* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1754* the number of credits and return true. Otherwise - return false.
1755*/
1756static bool
1757smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1758{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001759 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001760
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001761 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001762 return false;
1763
1764 if (!length) {
1765 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001766 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001767 spin_unlock(&server->req_lock);
1768 wake_up(&server->request_q);
1769 }
1770
1771 return true;
1772}
1773
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001774static bool
1775smb2_is_session_expired(char *buf)
1776{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001777 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001778
Mark Symsd81243c2018-05-24 09:47:31 +01001779 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1780 shdr->Status != STATUS_USER_SESSION_DELETED)
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001781 return false;
1782
Steve Frenche68a9322018-07-30 14:23:58 -05001783 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
1784 le16_to_cpu(shdr->Command),
1785 le64_to_cpu(shdr->MessageId));
Mark Symsd81243c2018-05-24 09:47:31 +01001786 cifs_dbg(FYI, "Session expired or deleted\n");
Steve Frenche68a9322018-07-30 14:23:58 -05001787
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001788 return true;
1789}
1790
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001791static int
1792smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1793 struct cifsInodeInfo *cinode)
1794{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07001795 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1796 return SMB2_lease_break(0, tcon, cinode->lease_key,
1797 smb2_get_lease_state(cinode));
1798
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001799 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1800 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001801 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001802}
1803
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10001804void
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001805smb2_set_related(struct smb_rqst *rqst)
1806{
1807 struct smb2_sync_hdr *shdr;
1808
1809 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1810 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1811}
1812
1813char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
1814
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10001815void
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06001816smb2_set_next_command(struct TCP_Server_Info *server, struct smb_rqst *rqst,
1817 bool has_space_for_padding)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001818{
1819 struct smb2_sync_hdr *shdr;
1820 unsigned long len = smb_rqst_len(server, rqst);
1821
1822 /* SMB headers in a compound are 8 byte aligned. */
1823 if (len & 7) {
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06001824 if (has_space_for_padding) {
1825 len = rqst->rq_iov[rqst->rq_nvec - 1].iov_len;
1826 rqst->rq_iov[rqst->rq_nvec - 1].iov_len =
1827 (len + 7) & ~7;
1828 } else {
1829 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
1830 rqst->rq_iov[rqst->rq_nvec].iov_len = 8 - (len & 7);
1831 rqst->rq_nvec++;
1832 }
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001833 len = smb_rqst_len(server, rqst);
1834 }
1835
1836 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
1837 shdr->NextCommand = cpu_to_le32(len);
1838}
1839
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001840/*
1841 * Passes the query info response back to the caller on success.
1842 * Caller need to free this with free_rsp_buf().
1843 */
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001844static int
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001845smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
1846 __le16 *utf16_path, u32 desired_access,
1847 u32 class, u32 type, u32 output_len,
1848 struct kvec *rsp, int *buftype)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001849{
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001850 struct cifs_ses *ses = tcon->ses;
1851 struct TCP_Server_Info *server = ses->server;
1852 int flags = 0;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001853 struct smb_rqst rqst[3];
1854 int resp_buftype[3];
1855 struct kvec rsp_iov[3];
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10001856 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001857 struct kvec qi_iov[1];
1858 struct kvec close_iov[1];
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001859 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001860 struct cifs_open_parms oparms;
1861 struct cifs_fid fid;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001862 int rc;
1863
1864 if (smb3_encryption_required(tcon))
1865 flags |= CIFS_TRANSFORM_REQ;
1866
1867 memset(rqst, 0, sizeof(rqst));
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10001868 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001869 memset(rsp_iov, 0, sizeof(rsp_iov));
1870
1871 memset(&open_iov, 0, sizeof(open_iov));
1872 rqst[0].rq_iov = open_iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10001873 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001874
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001875 oparms.tcon = tcon;
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001876 oparms.desired_access = desired_access;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001877 oparms.disposition = FILE_OPEN;
1878 oparms.create_options = 0;
1879 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001880 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001881
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001882 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001883 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001884 goto qic_exit;
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06001885 smb2_set_next_command(server, &rqst[0], 0);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001886
1887 memset(&qi_iov, 0, sizeof(qi_iov));
1888 rqst[1].rq_iov = qi_iov;
1889 rqst[1].rq_nvec = 1;
1890
1891 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001892 class, type, 0,
1893 output_len, 0,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001894 NULL);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001895 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001896 goto qic_exit;
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06001897 smb2_set_next_command(server, &rqst[1], 0);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001898 smb2_set_related(&rqst[1]);
1899
1900 memset(&close_iov, 0, sizeof(close_iov));
1901 rqst[2].rq_iov = close_iov;
1902 rqst[2].rq_nvec = 1;
1903
1904 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1905 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001906 goto qic_exit;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001907 smb2_set_related(&rqst[2]);
1908
1909 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1910 resp_buftype, rsp_iov);
1911 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001912 goto qic_exit;
1913
1914 *rsp = rsp_iov[1];
1915 *buftype = resp_buftype[1];
1916
1917 qic_exit:
1918 SMB2_open_free(&rqst[0]);
1919 SMB2_query_info_free(&rqst[1]);
1920 SMB2_close_free(&rqst[2]);
1921 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1922 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1923 return rc;
1924}
1925
1926static int
1927smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1928 struct kstatfs *buf)
1929{
1930 struct smb2_query_info_rsp *rsp;
1931 struct smb2_fs_full_size_info *info = NULL;
1932 __le16 utf16_path = 0; /* Null - open root of share */
1933 struct kvec rsp_iov = {NULL, 0};
1934 int buftype = CIFS_NO_BUFFER;
1935 int rc;
1936
1937
1938 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
1939 FILE_READ_ATTRIBUTES,
1940 FS_FULL_SIZE_INFORMATION,
1941 SMB2_O_INFO_FILESYSTEM,
1942 sizeof(struct smb2_fs_full_size_info),
1943 &rsp_iov, &buftype);
1944 if (rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001945 goto qfs_exit;
1946
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001947 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001948 buf->f_type = SMB2_MAGIC_NUMBER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001949 info = (struct smb2_fs_full_size_info *)(
1950 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1951 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1952 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001953 &rsp_iov,
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10001954 sizeof(struct smb2_fs_full_size_info));
1955 if (!rc)
1956 smb2_copy_fs_info_to_kstatfs(info, buf);
1957
1958qfs_exit:
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06001959 free_rsp_buf(buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001960 return rc;
1961}
1962
Steve French2d304212018-06-24 23:28:12 -05001963static int
1964smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1965 struct kstatfs *buf)
1966{
1967 int rc;
1968 __le16 srch_path = 0; /* Null - open root of share */
1969 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1970 struct cifs_open_parms oparms;
1971 struct cifs_fid fid;
1972
1973 if (!tcon->posix_extensions)
1974 return smb2_queryfs(xid, tcon, buf);
1975
1976 oparms.tcon = tcon;
1977 oparms.desired_access = FILE_READ_ATTRIBUTES;
1978 oparms.disposition = FILE_OPEN;
1979 oparms.create_options = 0;
1980 oparms.fid = &fid;
1981 oparms.reconnect = false;
1982
1983 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
1984 if (rc)
1985 return rc;
1986
1987 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
1988 fid.volatile_fid, buf);
1989 buf->f_type = SMB2_MAGIC_NUMBER;
1990 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1991 return rc;
1992}
Steve French2d304212018-06-24 23:28:12 -05001993
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001994static bool
1995smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1996{
1997 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1998 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1999}
2000
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002001static int
2002smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2003 __u64 length, __u32 type, int lock, int unlock, bool wait)
2004{
2005 if (unlock && !lock)
2006 type = SMB2_LOCKFLAG_UNLOCK;
2007 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2008 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2009 current->tgid, length, offset, type, wait);
2010}
2011
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002012static void
2013smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2014{
2015 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2016}
2017
2018static void
2019smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2020{
2021 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2022}
2023
2024static void
2025smb2_new_lease_key(struct cifs_fid *fid)
2026{
Steve Frenchfa70b872016-09-22 00:39:34 -05002027 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002028}
2029
Aurelien Aptel9d496402017-02-13 16:16:49 +01002030static int
2031smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2032 const char *search_name,
2033 struct dfs_info3_param **target_nodes,
2034 unsigned int *num_of_nodes,
2035 const struct nls_table *nls_codepage, int remap)
2036{
2037 int rc;
2038 __le16 *utf16_path = NULL;
2039 int utf16_path_len = 0;
2040 struct cifs_tcon *tcon;
2041 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2042 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2043 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2044
2045 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
2046
2047 /*
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002048 * Try to use the IPC tcon, otherwise just use any
Aurelien Aptel9d496402017-02-13 16:16:49 +01002049 */
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002050 tcon = ses->tcon_ipc;
2051 if (tcon == NULL) {
2052 spin_lock(&cifs_tcp_ses_lock);
2053 tcon = list_first_entry_or_null(&ses->tcon_list,
2054 struct cifs_tcon,
2055 tcon_list);
2056 if (tcon)
2057 tcon->tc_count++;
2058 spin_unlock(&cifs_tcp_ses_lock);
2059 }
Aurelien Aptel9d496402017-02-13 16:16:49 +01002060
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002061 if (tcon == NULL) {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002062 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2063 ses);
2064 rc = -ENOTCONN;
2065 goto out;
2066 }
2067
2068 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2069 &utf16_path_len,
2070 nls_codepage, remap);
2071 if (!utf16_path) {
2072 rc = -ENOMEM;
2073 goto out;
2074 }
2075
2076 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2077 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2078 if (!dfs_req) {
2079 rc = -ENOMEM;
2080 goto out;
2081 }
2082
2083 /* Highest DFS referral version understood */
2084 dfs_req->MaxReferralLevel = DFS_VERSION;
2085
2086 /* Path to resolve in an UTF-16 null-terminated string */
2087 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2088
2089 do {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002090 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2091 FSCTL_DFS_GET_REFERRALS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002092 true /* is_fsctl */,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002093 (char *)dfs_req, dfs_req_size,
2094 (char **)&dfs_rsp, &dfs_rsp_size);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002095 } while (rc == -EAGAIN);
2096
2097 if (rc) {
Steve French2564f2f2018-03-21 23:16:36 -05002098 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
Aurelien Aptel57025912017-11-21 14:47:56 +01002099 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002100 goto out;
2101 }
2102
2103 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2104 num_of_nodes, target_nodes,
2105 nls_codepage, remap, search_name,
2106 true /* is_unicode */);
2107 if (rc) {
2108 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
2109 goto out;
2110 }
2111
2112 out:
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002113 if (tcon && !tcon->ipc) {
2114 /* ipc tcons are not refcounted */
Aurelien Aptel9d496402017-02-13 16:16:49 +01002115 spin_lock(&cifs_tcp_ses_lock);
2116 tcon->tc_count--;
2117 spin_unlock(&cifs_tcp_ses_lock);
2118 }
2119 kfree(utf16_path);
2120 kfree(dfs_req);
2121 kfree(dfs_rsp);
2122 return rc;
2123}
Pavel Shilovsky78932422016-07-24 10:37:38 +03002124#define SMB2_SYMLINK_STRUCT_SIZE \
2125 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2126
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002127static int
2128smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2129 const char *full_path, char **target_path,
2130 struct cifs_sb_info *cifs_sb)
2131{
2132 int rc;
2133 __le16 *utf16_path;
2134 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2135 struct cifs_open_parms oparms;
2136 struct cifs_fid fid;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002137 struct kvec err_iov = {NULL, 0};
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002138 struct smb2_err_rsp *err_buf = NULL;
2139 int resp_buftype;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002140 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002141 unsigned int sub_len;
2142 unsigned int sub_offset;
2143 unsigned int print_len;
2144 unsigned int print_offset;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002145
2146 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2147
2148 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2149 if (!utf16_path)
2150 return -ENOMEM;
2151
2152 oparms.tcon = tcon;
2153 oparms.desired_access = FILE_READ_ATTRIBUTES;
2154 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -05002155 if (backup_cred(cifs_sb))
2156 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2157 else
2158 oparms.create_options = 0;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002159 oparms.fid = &fid;
2160 oparms.reconnect = false;
2161
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002162 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
2163 &resp_buftype);
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05002164 if (!rc || !err_iov.iov_base) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002165 rc = -ENOENT;
Dan Carpenterff361fd2018-06-19 15:25:30 +03002166 goto free_path;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002167 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03002168
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002169 err_buf = err_iov.iov_base;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002170 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002171 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002172 rc = -ENOENT;
2173 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002174 }
2175
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002176 /* open must fail on symlink - reset rc */
2177 rc = 0;
2178 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2179 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2180 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03002181 print_len = le16_to_cpu(symlink->PrintNameLength);
2182 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2183
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002184 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002185 rc = -ENOENT;
2186 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002187 }
2188
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002189 if (err_iov.iov_len <
2190 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002191 rc = -ENOENT;
2192 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002193 }
2194
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002195 *target_path = cifs_strndup_from_utf16(
2196 (char *)symlink->PathBuffer + sub_offset,
2197 sub_len, true, cifs_sb->local_nls);
2198 if (!(*target_path)) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002199 rc = -ENOMEM;
2200 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002201 }
2202 convert_delimiter(*target_path, '/');
2203 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002204
2205 querty_exit:
2206 free_rsp_buf(resp_buftype, err_buf);
Dan Carpenterff361fd2018-06-19 15:25:30 +03002207 free_path:
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002208 kfree(utf16_path);
2209 return rc;
2210}
2211
Arnd Bergmann84908422017-06-27 17:06:13 +02002212#ifdef CONFIG_CIFS_ACL
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002213static struct cifs_ntsd *
2214get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2215 const struct cifs_fid *cifsfid, u32 *pacllen)
2216{
2217 struct cifs_ntsd *pntsd = NULL;
2218 unsigned int xid;
2219 int rc = -EOPNOTSUPP;
2220 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2221
2222 if (IS_ERR(tlink))
2223 return ERR_CAST(tlink);
2224
2225 xid = get_xid();
2226 cifs_dbg(FYI, "trying to get acl\n");
2227
2228 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2229 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2230 free_xid(xid);
2231
2232 cifs_put_tlink(tlink);
2233
2234 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2235 if (rc)
2236 return ERR_PTR(rc);
2237 return pntsd;
2238
2239}
2240
2241static struct cifs_ntsd *
2242get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2243 const char *path, u32 *pacllen)
2244{
2245 struct cifs_ntsd *pntsd = NULL;
2246 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2247 unsigned int xid;
2248 int rc;
2249 struct cifs_tcon *tcon;
2250 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2251 struct cifs_fid fid;
2252 struct cifs_open_parms oparms;
2253 __le16 *utf16_path;
2254
2255 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2256 if (IS_ERR(tlink))
2257 return ERR_CAST(tlink);
2258
2259 tcon = tlink_tcon(tlink);
2260 xid = get_xid();
2261
2262 if (backup_cred(cifs_sb))
Colin Ian King709340a2017-07-05 13:47:34 +01002263 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002264 else
2265 oparms.create_options = 0;
2266
2267 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05002268 if (!utf16_path) {
2269 rc = -ENOMEM;
2270 free_xid(xid);
2271 return ERR_PTR(rc);
2272 }
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002273
2274 oparms.tcon = tcon;
2275 oparms.desired_access = READ_CONTROL;
2276 oparms.disposition = FILE_OPEN;
2277 oparms.fid = &fid;
2278 oparms.reconnect = false;
2279
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002280 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002281 kfree(utf16_path);
2282 if (!rc) {
2283 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2284 fid.volatile_fid, (void **)&pntsd, pacllen);
2285 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2286 }
2287
2288 cifs_put_tlink(tlink);
2289 free_xid(xid);
2290
2291 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2292 if (rc)
2293 return ERR_PTR(rc);
2294 return pntsd;
2295}
2296
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002297#ifdef CONFIG_CIFS_ACL
2298static int
2299set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2300 struct inode *inode, const char *path, int aclflag)
2301{
2302 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2303 unsigned int xid;
2304 int rc, access_flags = 0;
2305 struct cifs_tcon *tcon;
2306 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2307 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2308 struct cifs_fid fid;
2309 struct cifs_open_parms oparms;
2310 __le16 *utf16_path;
2311
2312 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2313 if (IS_ERR(tlink))
2314 return PTR_ERR(tlink);
2315
2316 tcon = tlink_tcon(tlink);
2317 xid = get_xid();
2318
2319 if (backup_cred(cifs_sb))
2320 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2321 else
2322 oparms.create_options = 0;
2323
2324 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2325 access_flags = WRITE_OWNER;
2326 else
2327 access_flags = WRITE_DAC;
2328
2329 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05002330 if (!utf16_path) {
2331 rc = -ENOMEM;
2332 free_xid(xid);
2333 return rc;
2334 }
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002335
2336 oparms.tcon = tcon;
2337 oparms.desired_access = access_flags;
2338 oparms.disposition = FILE_OPEN;
2339 oparms.path = path;
2340 oparms.fid = &fid;
2341 oparms.reconnect = false;
2342
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002343 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002344 kfree(utf16_path);
2345 if (!rc) {
2346 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2347 fid.volatile_fid, pnntsd, acllen, aclflag);
2348 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2349 }
2350
2351 cifs_put_tlink(tlink);
2352 free_xid(xid);
2353 return rc;
2354}
2355#endif /* CIFS_ACL */
2356
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002357/* Retrieve an ACL from the server */
2358static struct cifs_ntsd *
2359get_smb2_acl(struct cifs_sb_info *cifs_sb,
2360 struct inode *inode, const char *path,
2361 u32 *pacllen)
2362{
2363 struct cifs_ntsd *pntsd = NULL;
2364 struct cifsFileInfo *open_file = NULL;
2365
2366 if (inode)
2367 open_file = find_readable_file(CIFS_I(inode), true);
2368 if (!open_file)
2369 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2370
2371 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2372 cifsFileInfo_put(open_file);
2373 return pntsd;
2374}
Arnd Bergmann84908422017-06-27 17:06:13 +02002375#endif
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002376
Steve French30175622014-08-17 18:16:40 -05002377static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2378 loff_t offset, loff_t len, bool keep_size)
2379{
2380 struct inode *inode;
2381 struct cifsInodeInfo *cifsi;
2382 struct cifsFileInfo *cfile = file->private_data;
2383 struct file_zero_data_information fsctl_buf;
2384 long rc;
2385 unsigned int xid;
2386
2387 xid = get_xid();
2388
David Howells2b0143b2015-03-17 22:25:59 +00002389 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05002390 cifsi = CIFS_I(inode);
2391
2392 /* if file not oplocked can't be sure whether asking to extend size */
2393 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05002394 if (keep_size == false) {
2395 rc = -EOPNOTSUPP;
2396 free_xid(xid);
2397 return rc;
2398 }
Steve French30175622014-08-17 18:16:40 -05002399
Steve French2bb93d22014-08-20 18:56:29 -05002400 /*
Steve French30175622014-08-17 18:16:40 -05002401 * Must check if file sparse since fallocate -z (zero range) assumes
2402 * non-sparse allocation
2403 */
Steve Frenchcfe89092018-05-19 02:04:55 -05002404 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2405 rc = -EOPNOTSUPP;
2406 free_xid(xid);
2407 return rc;
2408 }
Steve French30175622014-08-17 18:16:40 -05002409
2410 /*
2411 * need to make sure we are not asked to extend the file since the SMB3
2412 * fsctl does not change the file size. In the future we could change
2413 * this to zero the first part of the range then set the file size
2414 * which for a non sparse file would zero the newly extended range
2415 */
2416 if (keep_size == false)
Steve Frenchcfe89092018-05-19 02:04:55 -05002417 if (i_size_read(inode) < offset + len) {
2418 rc = -EOPNOTSUPP;
2419 free_xid(xid);
2420 return rc;
2421 }
Steve French30175622014-08-17 18:16:40 -05002422
2423 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2424
2425 fsctl_buf.FileOffset = cpu_to_le64(offset);
2426 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2427
2428 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2429 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002430 true /* is_fctl */, (char *)&fsctl_buf,
Steve French30175622014-08-17 18:16:40 -05002431 sizeof(struct file_zero_data_information), NULL, NULL);
2432 free_xid(xid);
2433 return rc;
2434}
2435
Steve French31742c52014-08-17 08:38:47 -05002436static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2437 loff_t offset, loff_t len)
2438{
2439 struct inode *inode;
2440 struct cifsInodeInfo *cifsi;
2441 struct cifsFileInfo *cfile = file->private_data;
2442 struct file_zero_data_information fsctl_buf;
2443 long rc;
2444 unsigned int xid;
2445 __u8 set_sparse = 1;
2446
2447 xid = get_xid();
2448
David Howells2b0143b2015-03-17 22:25:59 +00002449 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05002450 cifsi = CIFS_I(inode);
2451
2452 /* Need to make file sparse, if not already, before freeing range. */
2453 /* Consider adding equivalent for compressed since it could also work */
Steve Frenchcfe89092018-05-19 02:04:55 -05002454 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2455 rc = -EOPNOTSUPP;
2456 free_xid(xid);
2457 return rc;
2458 }
Steve French31742c52014-08-17 08:38:47 -05002459
2460 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2461
2462 fsctl_buf.FileOffset = cpu_to_le64(offset);
2463 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2464
2465 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2466 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002467 true /* is_fctl */, (char *)&fsctl_buf,
Steve French31742c52014-08-17 08:38:47 -05002468 sizeof(struct file_zero_data_information), NULL, NULL);
2469 free_xid(xid);
2470 return rc;
2471}
2472
Steve French9ccf3212014-10-18 17:01:15 -05002473static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2474 loff_t off, loff_t len, bool keep_size)
2475{
2476 struct inode *inode;
2477 struct cifsInodeInfo *cifsi;
2478 struct cifsFileInfo *cfile = file->private_data;
2479 long rc = -EOPNOTSUPP;
2480 unsigned int xid;
2481
2482 xid = get_xid();
2483
David Howells2b0143b2015-03-17 22:25:59 +00002484 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05002485 cifsi = CIFS_I(inode);
2486
2487 /* if file not oplocked can't be sure whether asking to extend size */
2488 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05002489 if (keep_size == false) {
2490 free_xid(xid);
2491 return rc;
2492 }
Steve French9ccf3212014-10-18 17:01:15 -05002493
2494 /*
2495 * Files are non-sparse by default so falloc may be a no-op
2496 * Must check if file sparse. If not sparse, and not extending
2497 * then no need to do anything since file already allocated
2498 */
2499 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2500 if (keep_size == true)
Steve Frenchcfe89092018-05-19 02:04:55 -05002501 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05002502 /* check if extending file */
2503 else if (i_size_read(inode) >= off + len)
2504 /* not extending file and already not sparse */
Steve Frenchcfe89092018-05-19 02:04:55 -05002505 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05002506 /* BB: in future add else clause to extend file */
2507 else
Steve Frenchcfe89092018-05-19 02:04:55 -05002508 rc = -EOPNOTSUPP;
2509 free_xid(xid);
2510 return rc;
Steve French9ccf3212014-10-18 17:01:15 -05002511 }
2512
2513 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2514 /*
2515 * Check if falloc starts within first few pages of file
2516 * and ends within a few pages of the end of file to
2517 * ensure that most of file is being forced to be
2518 * fallocated now. If so then setting whole file sparse
2519 * ie potentially making a few extra pages at the beginning
2520 * or end of the file non-sparse via set_sparse is harmless.
2521 */
Steve Frenchcfe89092018-05-19 02:04:55 -05002522 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2523 rc = -EOPNOTSUPP;
2524 free_xid(xid);
2525 return rc;
2526 }
Steve French9ccf3212014-10-18 17:01:15 -05002527
2528 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
2529 }
2530 /* BB: else ... in future add code to extend file and set sparse */
2531
2532
2533 free_xid(xid);
2534 return rc;
2535}
2536
2537
Steve French31742c52014-08-17 08:38:47 -05002538static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
2539 loff_t off, loff_t len)
2540{
2541 /* KEEP_SIZE already checked for by do_fallocate */
2542 if (mode & FALLOC_FL_PUNCH_HOLE)
2543 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05002544 else if (mode & FALLOC_FL_ZERO_RANGE) {
2545 if (mode & FALLOC_FL_KEEP_SIZE)
2546 return smb3_zero_range(file, tcon, off, len, true);
2547 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05002548 } else if (mode == FALLOC_FL_KEEP_SIZE)
2549 return smb3_simple_falloc(file, tcon, off, len, true);
2550 else if (mode == 0)
2551 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05002552
2553 return -EOPNOTSUPP;
2554}
2555
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002556static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002557smb2_downgrade_oplock(struct TCP_Server_Info *server,
2558 struct cifsInodeInfo *cinode, bool set_level2)
2559{
2560 if (set_level2)
2561 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
2562 0, NULL);
2563 else
2564 server->ops->set_oplock_level(cinode, 0, 0, NULL);
2565}
2566
2567static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002568smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2569 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002570{
2571 oplock &= 0xFF;
2572 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2573 return;
2574 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002575 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002576 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
2577 &cinode->vfs_inode);
2578 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002579 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002580 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
2581 &cinode->vfs_inode);
2582 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
2583 cinode->oplock = CIFS_CACHE_READ_FLG;
2584 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
2585 &cinode->vfs_inode);
2586 } else
2587 cinode->oplock = 0;
2588}
2589
2590static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002591smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2592 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002593{
2594 char message[5] = {0};
2595
2596 oplock &= 0xFF;
2597 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2598 return;
2599
2600 cinode->oplock = 0;
2601 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
2602 cinode->oplock |= CIFS_CACHE_READ_FLG;
2603 strcat(message, "R");
2604 }
2605 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
2606 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
2607 strcat(message, "H");
2608 }
2609 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2610 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
2611 strcat(message, "W");
2612 }
2613 if (!cinode->oplock)
2614 strcat(message, "None");
2615 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2616 &cinode->vfs_inode);
2617}
2618
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002619static void
2620smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2621 unsigned int epoch, bool *purge_cache)
2622{
2623 unsigned int old_oplock = cinode->oplock;
2624
2625 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2626
2627 if (purge_cache) {
2628 *purge_cache = false;
2629 if (old_oplock == CIFS_CACHE_READ_FLG) {
2630 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2631 (epoch - cinode->epoch > 0))
2632 *purge_cache = true;
2633 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2634 (epoch - cinode->epoch > 1))
2635 *purge_cache = true;
2636 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2637 (epoch - cinode->epoch > 1))
2638 *purge_cache = true;
2639 else if (cinode->oplock == 0 &&
2640 (epoch - cinode->epoch > 0))
2641 *purge_cache = true;
2642 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2643 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2644 (epoch - cinode->epoch > 0))
2645 *purge_cache = true;
2646 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2647 (epoch - cinode->epoch > 1))
2648 *purge_cache = true;
2649 }
2650 cinode->epoch = epoch;
2651 }
2652}
2653
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002654static bool
2655smb2_is_read_op(__u32 oplock)
2656{
2657 return oplock == SMB2_OPLOCK_LEVEL_II;
2658}
2659
2660static bool
2661smb21_is_read_op(__u32 oplock)
2662{
2663 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2664 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2665}
2666
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002667static __le32
2668map_oplock_to_lease(u8 oplock)
2669{
2670 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2671 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2672 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2673 return SMB2_LEASE_READ_CACHING;
2674 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2675 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2676 SMB2_LEASE_WRITE_CACHING;
2677 return 0;
2678}
2679
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002680static char *
2681smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2682{
2683 struct create_lease *buf;
2684
2685 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2686 if (!buf)
2687 return NULL;
2688
Stefano Brivio729c0c92018-07-05 15:10:02 +02002689 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002690 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002691
2692 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2693 (struct create_lease, lcontext));
2694 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2695 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2696 (struct create_lease, Name));
2697 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002698 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002699 buf->Name[0] = 'R';
2700 buf->Name[1] = 'q';
2701 buf->Name[2] = 'L';
2702 buf->Name[3] = 's';
2703 return (char *)buf;
2704}
2705
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002706static char *
2707smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2708{
2709 struct create_lease_v2 *buf;
2710
2711 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2712 if (!buf)
2713 return NULL;
2714
Stefano Brivio729c0c92018-07-05 15:10:02 +02002715 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002716 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2717
2718 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2719 (struct create_lease_v2, lcontext));
2720 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2721 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2722 (struct create_lease_v2, Name));
2723 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07002724 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002725 buf->Name[0] = 'R';
2726 buf->Name[1] = 'q';
2727 buf->Name[2] = 'L';
2728 buf->Name[3] = 's';
2729 return (char *)buf;
2730}
2731
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002732static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002733smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002734{
2735 struct create_lease *lc = (struct create_lease *)buf;
2736
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002737 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002738 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2739 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2740 return le32_to_cpu(lc->lcontext.LeaseState);
2741}
2742
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002743static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002744smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002745{
2746 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2747
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002748 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002749 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2750 return SMB2_OPLOCK_LEVEL_NOCHANGE;
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06002751 if (lease_key)
Stefano Brivio729c0c92018-07-05 15:10:02 +02002752 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002753 return le32_to_cpu(lc->lcontext.LeaseState);
2754}
2755
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002756static unsigned int
2757smb2_wp_retry_size(struct inode *inode)
2758{
2759 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2760 SMB2_MAX_BUFFER_SIZE);
2761}
2762
Pavel Shilovsky52755802014-08-18 20:49:57 +04002763static bool
2764smb2_dir_needs_close(struct cifsFileInfo *cfile)
2765{
2766 return !cfile->invalidHandle;
2767}
2768
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002769static void
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002770fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
2771 struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002772{
2773 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002774 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002775
2776 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2777 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2778 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2779 tr_hdr->Flags = cpu_to_le16(0x01);
2780 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2781 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002782}
2783
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11002784/* We can not use the normal sg_set_buf() as we will sometimes pass a
2785 * stack object as buf.
2786 */
2787static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2788 unsigned int buflen)
2789{
2790 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
2791}
2792
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002793/* Assumes the first rqst has a transform header as the first iov.
2794 * I.e.
2795 * rqst[0].rq_iov[0] is transform header
2796 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
2797 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10002798 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002799static struct scatterlist *
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002800init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002801{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002802 unsigned int sg_len;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002803 struct scatterlist *sg;
2804 unsigned int i;
2805 unsigned int j;
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002806 unsigned int idx = 0;
2807 int skip;
2808
2809 sg_len = 1;
2810 for (i = 0; i < num_rqst; i++)
2811 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002812
2813 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2814 if (!sg)
2815 return NULL;
2816
2817 sg_init_table(sg, sg_len);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002818 for (i = 0; i < num_rqst; i++) {
2819 for (j = 0; j < rqst[i].rq_nvec; j++) {
2820 /*
2821 * The first rqst has a transform header where the
2822 * first 20 bytes are not part of the encrypted blob
2823 */
2824 skip = (i == 0) && (j == 0) ? 20 : 0;
2825 smb2_sg_set_buf(&sg[idx++],
2826 rqst[i].rq_iov[j].iov_base + skip,
2827 rqst[i].rq_iov[j].iov_len - skip);
2828 }
Steve Frenchd5f07fb2018-06-05 17:46:24 -05002829
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002830 for (j = 0; j < rqst[i].rq_npages; j++) {
2831 unsigned int len, offset;
2832
2833 rqst_page_get_length(&rqst[i], j, &len, &offset);
2834 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
2835 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002836 }
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002837 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002838 return sg;
2839}
2840
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002841static int
2842smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2843{
2844 struct cifs_ses *ses;
2845 u8 *ses_enc_key;
2846
2847 spin_lock(&cifs_tcp_ses_lock);
2848 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2849 if (ses->Suid != ses_id)
2850 continue;
2851 ses_enc_key = enc ? ses->smb3encryptionkey :
2852 ses->smb3decryptionkey;
2853 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2854 spin_unlock(&cifs_tcp_ses_lock);
2855 return 0;
2856 }
2857 spin_unlock(&cifs_tcp_ses_lock);
2858
2859 return 1;
2860}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002861/*
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10002862 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
2863 * iov[0] - transform header (associate data),
2864 * iov[1-N] - SMB2 header and pages - data to encrypt.
2865 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002866 * untouched.
2867 */
2868static int
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002869crypt_message(struct TCP_Server_Info *server, int num_rqst,
2870 struct smb_rqst *rqst, int enc)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002871{
2872 struct smb2_transform_hdr *tr_hdr =
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002873 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002874 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002875 int rc = 0;
2876 struct scatterlist *sg;
2877 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002878 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002879 struct aead_request *req;
2880 char *iv;
2881 unsigned int iv_len;
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002882 DECLARE_CRYPTO_WAIT(wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002883 struct crypto_aead *tfm;
2884 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2885
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002886 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2887 if (rc) {
2888 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2889 enc ? "en" : "de");
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002890 return 0;
2891 }
2892
2893 rc = smb3_crypto_aead_allocate(server);
2894 if (rc) {
2895 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2896 return rc;
2897 }
2898
2899 tfm = enc ? server->secmech.ccmaesencrypt :
2900 server->secmech.ccmaesdecrypt;
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08002901 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002902 if (rc) {
2903 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2904 return rc;
2905 }
2906
2907 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2908 if (rc) {
2909 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2910 return rc;
2911 }
2912
2913 req = aead_request_alloc(tfm, GFP_KERNEL);
2914 if (!req) {
2915 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2916 return -ENOMEM;
2917 }
2918
2919 if (!enc) {
2920 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2921 crypt_len += SMB2_SIGNATURE_SIZE;
2922 }
2923
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002924 sg = init_sg(num_rqst, rqst, sign);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002925 if (!sg) {
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002926 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2927 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002928 goto free_req;
2929 }
2930
2931 iv_len = crypto_aead_ivsize(tfm);
2932 iv = kzalloc(iv_len, GFP_KERNEL);
2933 if (!iv) {
2934 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02002935 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002936 goto free_sg;
2937 }
2938 iv[0] = 3;
2939 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2940
2941 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2942 aead_request_set_ad(req, assoc_data_len);
2943
2944 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002945 crypto_req_done, &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002946
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01002947 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
2948 : crypto_aead_decrypt(req), &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002949
2950 if (!rc && enc)
2951 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2952
2953 kfree(iv);
2954free_sg:
2955 kfree(sg);
2956free_req:
2957 kfree(req);
2958 return rc;
2959}
2960
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002961void
2962smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002963{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002964 int i, j;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002965
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002966 for (i = 0; i < num_rqst; i++) {
2967 if (rqst[i].rq_pages) {
2968 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
2969 put_page(rqst[i].rq_pages[j]);
2970 kfree(rqst[i].rq_pages);
2971 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002972 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002973}
2974
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002975/*
2976 * This function will initialize new_rq and encrypt the content.
2977 * The first entry, new_rq[0], only contains a single iov which contains
2978 * a smb2_transform_hdr and is pre-allocated by the caller.
2979 * This function then populates new_rq[1+] with the content from olq_rq[0+].
2980 *
2981 * The end result is an array of smb_rqst structures where the first structure
2982 * only contains a single iov for the transform header which we then can pass
2983 * to crypt_message().
2984 *
2985 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
2986 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
2987 */
2988static int
2989smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
2990 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002991{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002992 struct page **pages;
2993 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
2994 unsigned int npages;
2995 unsigned int orig_len = 0;
2996 int i, j;
2997 int rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002998
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10002999 for (i = 1; i < num_rqst; i++) {
3000 npages = old_rq[i - 1].rq_npages;
3001 pages = kmalloc_array(npages, sizeof(struct page *),
3002 GFP_KERNEL);
3003 if (!pages)
3004 goto err_free;
3005
3006 new_rq[i].rq_pages = pages;
3007 new_rq[i].rq_npages = npages;
3008 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3009 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3010 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3011 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3012 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3013
3014 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3015
3016 for (j = 0; j < npages; j++) {
3017 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3018 if (!pages[j])
3019 goto err_free;
3020 }
3021
3022 /* copy pages form the old */
3023 for (j = 0; j < npages; j++) {
3024 char *dst, *src;
3025 unsigned int offset, len;
3026
3027 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3028
3029 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3030 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3031
3032 memcpy(dst, src, len);
3033 kunmap(new_rq[i].rq_pages[j]);
3034 kunmap(old_rq[i - 1].rq_pages[j]);
3035 }
3036 }
3037
3038 /* fill the 1st iov with a transform header */
3039 fill_transform_hdr(tr_hdr, orig_len, old_rq);
3040
3041 rc = crypt_message(server, num_rqst, new_rq, 1);
3042 cifs_dbg(FYI, "encrypt message returned %d", rc);
3043 if (rc)
3044 goto err_free;
3045
3046 return rc;
3047
3048err_free:
3049 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3050 return rc;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003051}
3052
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003053static int
3054smb3_is_transform_hdr(void *buf)
3055{
3056 struct smb2_transform_hdr *trhdr = buf;
3057
3058 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3059}
3060
3061static int
3062decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3063 unsigned int buf_data_size, struct page **pages,
3064 unsigned int npages, unsigned int page_data_size)
3065{
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003066 struct kvec iov[2];
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003067 struct smb_rqst rqst = {NULL};
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003068 int rc;
3069
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003070 iov[0].iov_base = buf;
3071 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3072 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3073 iov[1].iov_len = buf_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003074
3075 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003076 rqst.rq_nvec = 2;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003077 rqst.rq_pages = pages;
3078 rqst.rq_npages = npages;
3079 rqst.rq_pagesz = PAGE_SIZE;
3080 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3081
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003082 rc = crypt_message(server, 1, &rqst, 0);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003083 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
3084
3085 if (rc)
3086 return rc;
3087
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003088 memmove(buf, iov[1].iov_base, buf_data_size);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003089
3090 server->total_read = buf_data_size + page_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003091
3092 return rc;
3093}
3094
3095static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003096read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3097 unsigned int npages, unsigned int len)
3098{
3099 int i;
3100 int length;
3101
3102 for (i = 0; i < npages; i++) {
3103 struct page *page = pages[i];
3104 size_t n;
3105
3106 n = len;
3107 if (len >= PAGE_SIZE) {
3108 /* enough data to fill the page */
3109 n = PAGE_SIZE;
3110 len -= n;
3111 } else {
3112 zero_user(page, len, PAGE_SIZE - len);
3113 len = 0;
3114 }
Long Li1dbe3462018-05-30 12:47:55 -07003115 length = cifs_read_page_from_socket(server, page, 0, n);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003116 if (length < 0)
3117 return length;
3118 server->total_read += length;
3119 }
3120
3121 return 0;
3122}
3123
3124static int
3125init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3126 unsigned int cur_off, struct bio_vec **page_vec)
3127{
3128 struct bio_vec *bvec;
3129 int i;
3130
3131 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3132 if (!bvec)
3133 return -ENOMEM;
3134
3135 for (i = 0; i < npages; i++) {
3136 bvec[i].bv_page = pages[i];
3137 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3138 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3139 data_size -= bvec[i].bv_len;
3140 }
3141
3142 if (data_size != 0) {
3143 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3144 kfree(bvec);
3145 return -EIO;
3146 }
3147
3148 *page_vec = bvec;
3149 return 0;
3150}
3151
3152static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003153handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3154 char *buf, unsigned int buf_len, struct page **pages,
3155 unsigned int npages, unsigned int page_data_size)
3156{
3157 unsigned int data_offset;
3158 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003159 unsigned int cur_off;
3160 unsigned int cur_page_idx;
3161 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003162 struct cifs_readdata *rdata = mid->callback_data;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003163 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003164 struct bio_vec *bvec = NULL;
3165 struct iov_iter iter;
3166 struct kvec iov;
3167 int length;
Long Li74dcf412017-11-22 17:38:46 -07003168 bool use_rdma_mr = false;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003169
3170 if (shdr->Command != SMB2_READ) {
3171 cifs_dbg(VFS, "only big read responses are supported\n");
3172 return -ENOTSUPP;
3173 }
3174
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003175 if (server->ops->is_session_expired &&
3176 server->ops->is_session_expired(buf)) {
3177 cifs_reconnect(server);
3178 wake_up(&server->response_q);
3179 return -1;
3180 }
3181
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003182 if (server->ops->is_status_pending &&
3183 server->ops->is_status_pending(buf, server, 0))
3184 return -1;
3185
3186 rdata->result = server->ops->map_error(buf, false);
3187 if (rdata->result != 0) {
3188 cifs_dbg(FYI, "%s: server returned error %d\n",
3189 __func__, rdata->result);
3190 dequeue_mid(mid, rdata->result);
3191 return 0;
3192 }
3193
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003194 data_offset = server->ops->read_data_offset(buf);
Long Li74dcf412017-11-22 17:38:46 -07003195#ifdef CONFIG_CIFS_SMB_DIRECT
3196 use_rdma_mr = rdata->mr;
3197#endif
3198 data_len = server->ops->read_data_length(buf, use_rdma_mr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003199
3200 if (data_offset < server->vals->read_rsp_size) {
3201 /*
3202 * win2k8 sometimes sends an offset of 0 when the read
3203 * is beyond the EOF. Treat it as if the data starts just after
3204 * the header.
3205 */
3206 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3207 __func__, data_offset);
3208 data_offset = server->vals->read_rsp_size;
3209 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3210 /* data_offset is beyond the end of smallbuf */
3211 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3212 __func__, data_offset);
3213 rdata->result = -EIO;
3214 dequeue_mid(mid, rdata->result);
3215 return 0;
3216 }
3217
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003218 pad_len = data_offset - server->vals->read_rsp_size;
3219
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003220 if (buf_len <= data_offset) {
3221 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003222 cur_page_idx = pad_len / PAGE_SIZE;
3223 cur_off = pad_len % PAGE_SIZE;
3224
3225 if (cur_page_idx != 0) {
3226 /* data offset is beyond the 1st page of response */
3227 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3228 __func__, data_offset);
3229 rdata->result = -EIO;
3230 dequeue_mid(mid, rdata->result);
3231 return 0;
3232 }
3233
3234 if (data_len > page_data_size - pad_len) {
3235 /* data_len is corrupt -- discard frame */
3236 rdata->result = -EIO;
3237 dequeue_mid(mid, rdata->result);
3238 return 0;
3239 }
3240
3241 rdata->result = init_read_bvec(pages, npages, page_data_size,
3242 cur_off, &bvec);
3243 if (rdata->result != 0) {
3244 dequeue_mid(mid, rdata->result);
3245 return 0;
3246 }
3247
David Howellsaa563d72018-10-20 00:57:56 +01003248 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003249 } else if (buf_len >= data_offset + data_len) {
3250 /* read response payload is in buf */
3251 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3252 iov.iov_base = buf + data_offset;
3253 iov.iov_len = data_len;
David Howellsaa563d72018-10-20 00:57:56 +01003254 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003255 } else {
3256 /* read response payload cannot be in both buf and pages */
3257 WARN_ONCE(1, "buf can not contain only a part of read data");
3258 rdata->result = -EIO;
3259 dequeue_mid(mid, rdata->result);
3260 return 0;
3261 }
3262
3263 /* set up first iov for signature check */
3264 rdata->iov[0].iov_base = buf;
3265 rdata->iov[0].iov_len = 4;
3266 rdata->iov[1].iov_base = buf + 4;
3267 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
3268 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3269 rdata->iov[0].iov_base, server->vals->read_rsp_size);
3270
3271 length = rdata->copy_into_pages(server, rdata, &iter);
3272
3273 kfree(bvec);
3274
3275 if (length < 0)
3276 return length;
3277
3278 dequeue_mid(mid, false);
3279 return length;
3280}
3281
3282static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003283receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3284{
3285 char *buf = server->smallbuf;
3286 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3287 unsigned int npages;
3288 struct page **pages;
3289 unsigned int len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003290 unsigned int buflen = server->pdu_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003291 int rc;
3292 int i = 0;
3293
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003294 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003295 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3296
3297 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3298 if (rc < 0)
3299 return rc;
3300 server->total_read += rc;
3301
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003302 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003303 server->vals->read_rsp_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003304 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3305
3306 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3307 if (!pages) {
3308 rc = -ENOMEM;
3309 goto discard_data;
3310 }
3311
3312 for (; i < npages; i++) {
3313 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3314 if (!pages[i]) {
3315 rc = -ENOMEM;
3316 goto discard_data;
3317 }
3318 }
3319
3320 /* read read data into pages */
3321 rc = read_data_into_pages(server, pages, npages, len);
3322 if (rc)
3323 goto free_pages;
3324
Pavel Shilovsky350be252017-04-10 10:31:33 -07003325 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003326 if (rc)
3327 goto free_pages;
3328
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003329 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003330 pages, npages, len);
3331 if (rc)
3332 goto free_pages;
3333
3334 *mid = smb2_find_mid(server, buf);
3335 if (*mid == NULL)
3336 cifs_dbg(FYI, "mid not found\n");
3337 else {
3338 cifs_dbg(FYI, "mid found\n");
3339 (*mid)->decrypted = true;
3340 rc = handle_read_data(server, *mid, buf,
3341 server->vals->read_rsp_size,
3342 pages, npages, len);
3343 }
3344
3345free_pages:
3346 for (i = i - 1; i >= 0; i--)
3347 put_page(pages[i]);
3348 kfree(pages);
3349 return rc;
3350discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07003351 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003352 goto free_pages;
3353}
3354
3355static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003356receive_encrypted_standard(struct TCP_Server_Info *server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003357 struct mid_q_entry **mids, char **bufs,
3358 int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003359{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003360 int ret, length;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003361 char *buf = server->smallbuf;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003362 char *tmpbuf;
3363 struct smb2_sync_hdr *shdr;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10003364 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003365 unsigned int buf_size;
3366 struct mid_q_entry *mid_entry;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003367 int next_is_large;
3368 char *next_buffer = NULL;
3369
3370 *num_mids = 0;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003371
3372 /* switch to large buffer if too big for a small one */
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003373 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003374 server->large_buf = true;
3375 memcpy(server->bigbuf, buf, server->total_read);
3376 buf = server->bigbuf;
3377 }
3378
3379 /* now read the rest */
3380 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003381 pdu_length - HEADER_SIZE(server) + 1);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003382 if (length < 0)
3383 return length;
3384 server->total_read += length;
3385
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003386 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003387 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3388 if (length)
3389 return length;
3390
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003391 next_is_large = server->large_buf;
3392 one_more:
3393 shdr = (struct smb2_sync_hdr *)buf;
3394 if (shdr->NextCommand) {
3395 if (next_is_large) {
3396 tmpbuf = server->bigbuf;
3397 next_buffer = (char *)cifs_buf_get();
3398 } else {
3399 tmpbuf = server->smallbuf;
3400 next_buffer = (char *)cifs_small_buf_get();
3401 }
3402 memcpy(next_buffer,
3403 tmpbuf + le32_to_cpu(shdr->NextCommand),
3404 pdu_length - le32_to_cpu(shdr->NextCommand));
3405 }
3406
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003407 mid_entry = smb2_find_mid(server, buf);
3408 if (mid_entry == NULL)
3409 cifs_dbg(FYI, "mid not found\n");
3410 else {
3411 cifs_dbg(FYI, "mid found\n");
3412 mid_entry->decrypted = true;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003413 mid_entry->resp_buf_size = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003414 }
3415
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003416 if (*num_mids >= MAX_COMPOUND) {
3417 cifs_dbg(VFS, "too many PDUs in compound\n");
3418 return -1;
3419 }
3420 bufs[*num_mids] = buf;
3421 mids[(*num_mids)++] = mid_entry;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003422
3423 if (mid_entry && mid_entry->handle)
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003424 ret = mid_entry->handle(server, mid_entry);
3425 else
3426 ret = cifs_handle_standard(server, mid_entry);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003427
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003428 if (ret == 0 && shdr->NextCommand) {
3429 pdu_length -= le32_to_cpu(shdr->NextCommand);
3430 server->large_buf = next_is_large;
3431 if (next_is_large)
3432 server->bigbuf = next_buffer;
3433 else
3434 server->smallbuf = next_buffer;
3435
3436 buf += le32_to_cpu(shdr->NextCommand);
3437 goto one_more;
3438 }
3439
3440 return ret;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003441}
3442
3443static int
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003444smb3_receive_transform(struct TCP_Server_Info *server,
3445 struct mid_q_entry **mids, char **bufs, int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003446{
3447 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10003448 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003449 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3450 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3451
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003452 if (pdu_length < sizeof(struct smb2_transform_hdr) +
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003453 sizeof(struct smb2_sync_hdr)) {
3454 cifs_dbg(VFS, "Transform message is too small (%u)\n",
3455 pdu_length);
3456 cifs_reconnect(server);
3457 wake_up(&server->response_q);
3458 return -ECONNABORTED;
3459 }
3460
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003461 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003462 cifs_dbg(VFS, "Transform message is broken\n");
3463 cifs_reconnect(server);
3464 wake_up(&server->response_q);
3465 return -ECONNABORTED;
3466 }
3467
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003468 /* TODO: add support for compounds containing READ. */
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003469 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003470 return receive_encrypted_read(server, &mids[0]);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003471
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003472 return receive_encrypted_standard(server, mids, bufs, num_mids);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003473}
3474
3475int
3476smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
3477{
3478 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
3479
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003480 return handle_read_data(server, mid, buf, server->pdu_size,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003481 NULL, 0, 0);
3482}
3483
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003484static int
3485smb2_next_header(char *buf)
3486{
3487 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
3488 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
3489
3490 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
3491 return sizeof(struct smb2_transform_hdr) +
3492 le32_to_cpu(t_hdr->OriginalMessageSize);
3493
3494 return le32_to_cpu(hdr->NextCommand);
3495}
3496
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003497struct smb_version_operations smb20_operations = {
3498 .compare_fids = smb2_compare_fids,
3499 .setup_request = smb2_setup_request,
3500 .setup_async_request = smb2_setup_async_request,
3501 .check_receive = smb2_check_receive,
3502 .add_credits = smb2_add_credits,
3503 .set_credits = smb2_set_credits,
3504 .get_credits_field = smb2_get_credits_field,
3505 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003506 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003507 .get_next_mid = smb2_get_next_mid,
3508 .read_data_offset = smb2_read_data_offset,
3509 .read_data_length = smb2_read_data_length,
3510 .map_error = map_smb2_to_linux_error,
3511 .find_mid = smb2_find_mid,
3512 .check_message = smb2_check_message,
3513 .dump_detail = smb2_dump_detail,
3514 .clear_stats = smb2_clear_stats,
3515 .print_stats = smb2_print_stats,
3516 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003517 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003518 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003519 .need_neg = smb2_need_neg,
3520 .negotiate = smb2_negotiate,
3521 .negotiate_wsize = smb2_negotiate_wsize,
3522 .negotiate_rsize = smb2_negotiate_rsize,
3523 .sess_setup = SMB2_sess_setup,
3524 .logoff = SMB2_logoff,
3525 .tree_connect = SMB2_tcon,
3526 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05003527 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003528 .is_path_accessible = smb2_is_path_accessible,
3529 .can_echo = smb2_can_echo,
3530 .echo = SMB2_echo,
3531 .query_path_info = smb2_query_path_info,
3532 .get_srv_inum = smb2_get_srv_inum,
3533 .query_file_info = smb2_query_file_info,
3534 .set_path_size = smb2_set_path_size,
3535 .set_file_size = smb2_set_file_size,
3536 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05003537 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003538 .mkdir = smb2_mkdir,
3539 .mkdir_setinfo = smb2_mkdir_setinfo,
3540 .rmdir = smb2_rmdir,
3541 .unlink = smb2_unlink,
3542 .rename = smb2_rename_path,
3543 .create_hardlink = smb2_create_hardlink,
3544 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01003545 .query_mf_symlink = smb3_query_mf_symlink,
3546 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003547 .open = smb2_open_file,
3548 .set_fid = smb2_set_fid,
3549 .close = smb2_close_file,
3550 .flush = smb2_flush_file,
3551 .async_readv = smb2_async_readv,
3552 .async_writev = smb2_async_writev,
3553 .sync_read = smb2_sync_read,
3554 .sync_write = smb2_sync_write,
3555 .query_dir_first = smb2_query_dir_first,
3556 .query_dir_next = smb2_query_dir_next,
3557 .close_dir = smb2_close_dir,
3558 .calc_smb_size = smb2_calc_size,
3559 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003560 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003561 .oplock_response = smb2_oplock_response,
3562 .queryfs = smb2_queryfs,
3563 .mand_lock = smb2_mand_lock,
3564 .mand_unlock_range = smb2_unlock_range,
3565 .push_mand_locks = smb2_push_mandatory_locks,
3566 .get_lease_key = smb2_get_lease_key,
3567 .set_lease_key = smb2_set_lease_key,
3568 .new_lease_key = smb2_new_lease_key,
3569 .calc_signature = smb2_calc_signature,
3570 .is_read_op = smb2_is_read_op,
3571 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003572 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003573 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003574 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003575 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04003576 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003577 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303578 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003579#ifdef CONFIG_CIFS_XATTR
3580 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003581 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003582#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003583#ifdef CONFIG_CIFS_ACL
3584 .get_acl = get_smb2_acl,
3585 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003586 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003587#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003588 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003589 .ioctl_query_info = smb2_ioctl_query_info,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003590};
3591
Steve French1080ef72011-02-24 18:07:19 +00003592struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07003593 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003594 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04003595 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003596 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04003597 .add_credits = smb2_add_credits,
3598 .set_credits = smb2_set_credits,
3599 .get_credits_field = smb2_get_credits_field,
3600 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003601 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003602 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003603 .read_data_offset = smb2_read_data_offset,
3604 .read_data_length = smb2_read_data_length,
3605 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04003606 .find_mid = smb2_find_mid,
3607 .check_message = smb2_check_message,
3608 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04003609 .clear_stats = smb2_clear_stats,
3610 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003611 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003612 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003613 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04003614 .need_neg = smb2_need_neg,
3615 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07003616 .negotiate_wsize = smb2_negotiate_wsize,
3617 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04003618 .sess_setup = SMB2_sess_setup,
3619 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04003620 .tree_connect = SMB2_tcon,
3621 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05003622 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04003623 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04003624 .can_echo = smb2_can_echo,
3625 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04003626 .query_path_info = smb2_query_path_info,
3627 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07003628 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07003629 .set_path_size = smb2_set_path_size,
3630 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07003631 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05003632 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04003633 .mkdir = smb2_mkdir,
3634 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04003635 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07003636 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07003637 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07003638 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003639 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05003640 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05003641 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07003642 .open = smb2_open_file,
3643 .set_fid = smb2_set_fid,
3644 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07003645 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003646 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07003647 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07003648 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07003649 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07003650 .query_dir_first = smb2_query_dir_first,
3651 .query_dir_next = smb2_query_dir_next,
3652 .close_dir = smb2_close_dir,
3653 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07003654 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003655 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07003656 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07003657 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07003658 .mand_lock = smb2_mand_lock,
3659 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07003660 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07003661 .get_lease_key = smb2_get_lease_key,
3662 .set_lease_key = smb2_set_lease_key,
3663 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06003664 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003665 .is_read_op = smb21_is_read_op,
3666 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003667 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003668 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003669 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003670 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04003671 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05003672 .enum_snapshots = smb3_enum_snapshots,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003673 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303674 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003675#ifdef CONFIG_CIFS_XATTR
3676 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003677 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003678#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003679#ifdef CONFIG_CIFS_ACL
3680 .get_acl = get_smb2_acl,
3681 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003682 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003683#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003684 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003685 .ioctl_query_info = smb2_ioctl_query_info,
Steve French38107d42012-12-08 22:08:06 -06003686};
3687
Steve French38107d42012-12-08 22:08:06 -06003688struct smb_version_operations smb30_operations = {
3689 .compare_fids = smb2_compare_fids,
3690 .setup_request = smb2_setup_request,
3691 .setup_async_request = smb2_setup_async_request,
3692 .check_receive = smb2_check_receive,
3693 .add_credits = smb2_add_credits,
3694 .set_credits = smb2_set_credits,
3695 .get_credits_field = smb2_get_credits_field,
3696 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04003697 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06003698 .get_next_mid = smb2_get_next_mid,
3699 .read_data_offset = smb2_read_data_offset,
3700 .read_data_length = smb2_read_data_length,
3701 .map_error = map_smb2_to_linux_error,
3702 .find_mid = smb2_find_mid,
3703 .check_message = smb2_check_message,
3704 .dump_detail = smb2_dump_detail,
3705 .clear_stats = smb2_clear_stats,
3706 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05003707 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06003708 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003709 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003710 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06003711 .need_neg = smb2_need_neg,
3712 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05003713 .negotiate_wsize = smb3_negotiate_wsize,
3714 .negotiate_rsize = smb3_negotiate_rsize,
Steve French38107d42012-12-08 22:08:06 -06003715 .sess_setup = SMB2_sess_setup,
3716 .logoff = SMB2_logoff,
3717 .tree_connect = SMB2_tcon,
3718 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05003719 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06003720 .is_path_accessible = smb2_is_path_accessible,
3721 .can_echo = smb2_can_echo,
3722 .echo = SMB2_echo,
3723 .query_path_info = smb2_query_path_info,
3724 .get_srv_inum = smb2_get_srv_inum,
3725 .query_file_info = smb2_query_file_info,
3726 .set_path_size = smb2_set_path_size,
3727 .set_file_size = smb2_set_file_size,
3728 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05003729 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06003730 .mkdir = smb2_mkdir,
3731 .mkdir_setinfo = smb2_mkdir_setinfo,
3732 .rmdir = smb2_rmdir,
3733 .unlink = smb2_unlink,
3734 .rename = smb2_rename_path,
3735 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003736 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05003737 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05003738 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06003739 .open = smb2_open_file,
3740 .set_fid = smb2_set_fid,
3741 .close = smb2_close_file,
3742 .flush = smb2_flush_file,
3743 .async_readv = smb2_async_readv,
3744 .async_writev = smb2_async_writev,
3745 .sync_read = smb2_sync_read,
3746 .sync_write = smb2_sync_write,
3747 .query_dir_first = smb2_query_dir_first,
3748 .query_dir_next = smb2_query_dir_next,
3749 .close_dir = smb2_close_dir,
3750 .calc_smb_size = smb2_calc_size,
3751 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003752 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06003753 .oplock_response = smb2_oplock_response,
3754 .queryfs = smb2_queryfs,
3755 .mand_lock = smb2_mand_lock,
3756 .mand_unlock_range = smb2_unlock_range,
3757 .push_mand_locks = smb2_push_mandatory_locks,
3758 .get_lease_key = smb2_get_lease_key,
3759 .set_lease_key = smb2_set_lease_key,
3760 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003761 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06003762 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003763 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003764 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003765 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003766 .create_lease_buf = smb3_create_lease_buf,
3767 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003768 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05003769 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06003770 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003771 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04003772 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05003773 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003774 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003775 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003776 .is_transform_hdr = smb3_is_transform_hdr,
3777 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003778 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303779 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003780#ifdef CONFIG_CIFS_XATTR
3781 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003782 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003783#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003784#ifdef CONFIG_CIFS_ACL
3785 .get_acl = get_smb2_acl,
3786 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003787 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003788#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003789 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003790 .ioctl_query_info = smb2_ioctl_query_info,
Steve French1080ef72011-02-24 18:07:19 +00003791};
3792
Steve Frenchaab18932015-06-23 23:37:11 -05003793struct smb_version_operations smb311_operations = {
3794 .compare_fids = smb2_compare_fids,
3795 .setup_request = smb2_setup_request,
3796 .setup_async_request = smb2_setup_async_request,
3797 .check_receive = smb2_check_receive,
3798 .add_credits = smb2_add_credits,
3799 .set_credits = smb2_set_credits,
3800 .get_credits_field = smb2_get_credits_field,
3801 .get_credits = smb2_get_credits,
3802 .wait_mtu_credits = smb2_wait_mtu_credits,
3803 .get_next_mid = smb2_get_next_mid,
3804 .read_data_offset = smb2_read_data_offset,
3805 .read_data_length = smb2_read_data_length,
3806 .map_error = map_smb2_to_linux_error,
3807 .find_mid = smb2_find_mid,
3808 .check_message = smb2_check_message,
3809 .dump_detail = smb2_dump_detail,
3810 .clear_stats = smb2_clear_stats,
3811 .print_stats = smb2_print_stats,
3812 .dump_share_caps = smb2_dump_share_caps,
3813 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08003814 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05003815 .downgrade_oplock = smb2_downgrade_oplock,
3816 .need_neg = smb2_need_neg,
3817 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05003818 .negotiate_wsize = smb3_negotiate_wsize,
3819 .negotiate_rsize = smb3_negotiate_rsize,
Steve Frenchaab18932015-06-23 23:37:11 -05003820 .sess_setup = SMB2_sess_setup,
3821 .logoff = SMB2_logoff,
3822 .tree_connect = SMB2_tcon,
3823 .tree_disconnect = SMB2_tdis,
3824 .qfs_tcon = smb3_qfs_tcon,
3825 .is_path_accessible = smb2_is_path_accessible,
3826 .can_echo = smb2_can_echo,
3827 .echo = SMB2_echo,
3828 .query_path_info = smb2_query_path_info,
3829 .get_srv_inum = smb2_get_srv_inum,
3830 .query_file_info = smb2_query_file_info,
3831 .set_path_size = smb2_set_path_size,
3832 .set_file_size = smb2_set_file_size,
3833 .set_file_info = smb2_set_file_info,
3834 .set_compression = smb2_set_compression,
3835 .mkdir = smb2_mkdir,
3836 .mkdir_setinfo = smb2_mkdir_setinfo,
Steve Frenchbea851b2018-06-14 21:56:32 -05003837 .posix_mkdir = smb311_posix_mkdir,
Steve Frenchaab18932015-06-23 23:37:11 -05003838 .rmdir = smb2_rmdir,
3839 .unlink = smb2_unlink,
3840 .rename = smb2_rename_path,
3841 .create_hardlink = smb2_create_hardlink,
3842 .query_symlink = smb2_query_symlink,
3843 .query_mf_symlink = smb3_query_mf_symlink,
3844 .create_mf_symlink = smb3_create_mf_symlink,
3845 .open = smb2_open_file,
3846 .set_fid = smb2_set_fid,
3847 .close = smb2_close_file,
3848 .flush = smb2_flush_file,
3849 .async_readv = smb2_async_readv,
3850 .async_writev = smb2_async_writev,
3851 .sync_read = smb2_sync_read,
3852 .sync_write = smb2_sync_write,
3853 .query_dir_first = smb2_query_dir_first,
3854 .query_dir_next = smb2_query_dir_next,
3855 .close_dir = smb2_close_dir,
3856 .calc_smb_size = smb2_calc_size,
3857 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003858 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05003859 .oplock_response = smb2_oplock_response,
Steve French2d304212018-06-24 23:28:12 -05003860 .queryfs = smb311_queryfs,
Steve Frenchaab18932015-06-23 23:37:11 -05003861 .mand_lock = smb2_mand_lock,
3862 .mand_unlock_range = smb2_unlock_range,
3863 .push_mand_locks = smb2_push_mandatory_locks,
3864 .get_lease_key = smb2_get_lease_key,
3865 .set_lease_key = smb2_set_lease_key,
3866 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06003867 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05003868 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05003869 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05003870 .is_read_op = smb21_is_read_op,
3871 .set_oplock_level = smb3_set_oplock_level,
3872 .create_lease_buf = smb3_create_lease_buf,
3873 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05003874 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07003875 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05003876/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3877 .wp_retry_size = smb2_wp_retry_size,
3878 .dir_needs_close = smb2_dir_needs_close,
3879 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05003880 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003881 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003882 .is_transform_hdr = smb3_is_transform_hdr,
3883 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01003884 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05303885 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003886#ifdef CONFIG_CIFS_XATTR
3887 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10003888 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10003889#endif /* CIFS_XATTR */
Ronnie Sahlbergc1777df2018-08-10 11:03:55 +10003890#ifdef CONFIG_CIFS_ACL
3891 .get_acl = get_smb2_acl,
3892 .get_acl_by_fid = get_smb2_acl_by_fid,
3893 .set_acl = set_smb2_acl,
3894#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10003895 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05003896 .ioctl_query_info = smb2_ioctl_query_info,
Steve Frenchaab18932015-06-23 23:37:11 -05003897};
Steve Frenchaab18932015-06-23 23:37:11 -05003898
Steve Frenchdd446b12012-11-28 23:21:06 -06003899struct smb_version_values smb20_values = {
3900 .version_string = SMB20_VERSION_STRING,
3901 .protocol_id = SMB20_PROT_ID,
3902 .req_capabilities = 0, /* MBZ */
3903 .large_lock_type = 0,
3904 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3905 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3906 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003907 .header_size = sizeof(struct smb2_sync_hdr),
3908 .header_preamble_size = 0,
Steve Frenchdd446b12012-11-28 23:21:06 -06003909 .max_header_size = MAX_SMB2_HDR_SIZE,
3910 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3911 .lock_cmd = SMB2_LOCK,
3912 .cap_unix = 0,
3913 .cap_nt_find = SMB2_NT_FIND,
3914 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003915 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3916 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003917 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06003918};
3919
Steve French1080ef72011-02-24 18:07:19 +00003920struct smb_version_values smb21_values = {
3921 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003922 .protocol_id = SMB21_PROT_ID,
3923 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3924 .large_lock_type = 0,
3925 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3926 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3927 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003928 .header_size = sizeof(struct smb2_sync_hdr),
3929 .header_preamble_size = 0,
Steve Frenche4aa25e2012-10-01 12:26:22 -05003930 .max_header_size = MAX_SMB2_HDR_SIZE,
3931 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3932 .lock_cmd = SMB2_LOCK,
3933 .cap_unix = 0,
3934 .cap_nt_find = SMB2_NT_FIND,
3935 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003936 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3937 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003938 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05003939};
3940
Steve French9764c022017-09-17 10:41:35 -05003941struct smb_version_values smb3any_values = {
3942 .version_string = SMB3ANY_VERSION_STRING,
3943 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05003944 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
Steve French9764c022017-09-17 10:41:35 -05003945 .large_lock_type = 0,
3946 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3947 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3948 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003949 .header_size = sizeof(struct smb2_sync_hdr),
3950 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05003951 .max_header_size = MAX_SMB2_HDR_SIZE,
3952 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3953 .lock_cmd = SMB2_LOCK,
3954 .cap_unix = 0,
3955 .cap_nt_find = SMB2_NT_FIND,
3956 .cap_large_files = SMB2_LARGE_FILES,
3957 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3958 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3959 .create_lease_size = sizeof(struct create_lease_v2),
3960};
3961
3962struct smb_version_values smbdefault_values = {
3963 .version_string = SMBDEFAULT_VERSION_STRING,
3964 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05003965 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
Steve French9764c022017-09-17 10:41:35 -05003966 .large_lock_type = 0,
3967 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3968 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3969 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003970 .header_size = sizeof(struct smb2_sync_hdr),
3971 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05003972 .max_header_size = MAX_SMB2_HDR_SIZE,
3973 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3974 .lock_cmd = SMB2_LOCK,
3975 .cap_unix = 0,
3976 .cap_nt_find = SMB2_NT_FIND,
3977 .cap_large_files = SMB2_LARGE_FILES,
3978 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3979 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3980 .create_lease_size = sizeof(struct create_lease_v2),
3981};
3982
Steve Frenche4aa25e2012-10-01 12:26:22 -05003983struct smb_version_values smb30_values = {
3984 .version_string = SMB30_VERSION_STRING,
3985 .protocol_id = SMB30_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05003986 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07003987 .large_lock_type = 0,
3988 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3989 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3990 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003991 .header_size = sizeof(struct smb2_sync_hdr),
3992 .header_preamble_size = 0,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04003993 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003994 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04003995 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003996 .cap_unix = 0,
3997 .cap_nt_find = SMB2_NT_FIND,
3998 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04003999 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4000 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004001 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00004002};
Steve French20b6d8b2013-06-12 22:48:41 -05004003
4004struct smb_version_values smb302_values = {
4005 .version_string = SMB302_VERSION_STRING,
4006 .protocol_id = SMB302_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05004007 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
Steve French20b6d8b2013-06-12 22:48:41 -05004008 .large_lock_type = 0,
4009 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4010 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4011 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004012 .header_size = sizeof(struct smb2_sync_hdr),
4013 .header_preamble_size = 0,
Steve French20b6d8b2013-06-12 22:48:41 -05004014 .max_header_size = MAX_SMB2_HDR_SIZE,
4015 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4016 .lock_cmd = SMB2_LOCK,
4017 .cap_unix = 0,
4018 .cap_nt_find = SMB2_NT_FIND,
4019 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04004020 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4021 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004022 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05004023};
Steve French5f7fbf72014-12-17 22:52:58 -06004024
Steve French5f7fbf72014-12-17 22:52:58 -06004025struct smb_version_values smb311_values = {
4026 .version_string = SMB311_VERSION_STRING,
4027 .protocol_id = SMB311_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05004028 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
Steve French5f7fbf72014-12-17 22:52:58 -06004029 .large_lock_type = 0,
4030 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4031 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4032 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004033 .header_size = sizeof(struct smb2_sync_hdr),
4034 .header_preamble_size = 0,
Steve French5f7fbf72014-12-17 22:52:58 -06004035 .max_header_size = MAX_SMB2_HDR_SIZE,
4036 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4037 .lock_cmd = SMB2_LOCK,
4038 .cap_unix = 0,
4039 .cap_nt_find = SMB2_NT_FIND,
4040 .cap_large_files = SMB2_LARGE_FILES,
4041 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4042 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4043 .create_lease_size = sizeof(struct create_lease_v2),
4044};