SMB3: Allow persistent handle timeout to be configurable on mount
[linux-block.git] / fs / cifs / smb2ops.c
CommitLineData
1080ef75
SF
1/*
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
3a3bab50 20#include <linux/pagemap.h>
6fc05c25 21#include <linux/vfs.h>
f29ebb47 22#include <linux/falloc.h>
026e93dc 23#include <linux/scatterlist.h>
4fa8e504 24#include <linux/uuid.h>
026e93dc 25#include <crypto/aead.h>
1080ef75 26#include "cifsglob.h"
2dc7e1c0
PS
27#include "smb2pdu.h"
28#include "smb2proto.h"
28ea5290
PS
29#include "cifsproto.h"
30#include "cifs_debug.h"
b42bf888 31#include "cifs_unicode.h"
2e44b288 32#include "smb2status.h"
6fc05c25 33#include "smb2glob.h"
834170c8 34#include "cifs_ioctl.h"
09902f8d 35#include "smbdirect.h"
28ea5290 36
ef68e831 37/* Change credits for different ops and return the total number of credits */
28ea5290
PS
38static int
39change_conf(struct TCP_Server_Info *server)
40{
41 server->credits += server->echo_credits + server->oplock_credits;
42 server->oplock_credits = server->echo_credits = 0;
43 switch (server->credits) {
44 case 0:
ef68e831 45 return 0;
28ea5290
PS
46 case 1:
47 server->echoes = false;
48 server->oplocks = false;
28ea5290
PS
49 break;
50 case 2:
51 server->echoes = true;
52 server->oplocks = false;
53 server->echo_credits = 1;
28ea5290
PS
54 break;
55 default:
56 server->echoes = true;
e0ddde9d
SF
57 if (enable_oplocks) {
58 server->oplocks = true;
59 server->oplock_credits = 1;
60 } else
61 server->oplocks = false;
62
28ea5290 63 server->echo_credits = 1;
28ea5290
PS
64 }
65 server->credits -= server->echo_credits + server->oplock_credits;
ef68e831 66 return server->credits + server->echo_credits + server->oplock_credits;
28ea5290
PS
67}
68
69static void
335b7b62
PS
70smb2_add_credits(struct TCP_Server_Info *server,
71 const struct cifs_credits *credits, const int optype)
28ea5290 72{
ef68e831 73 int *val, rc = -1;
335b7b62
PS
74 unsigned int add = credits->value;
75 unsigned int instance = credits->instance;
76 bool reconnect_detected = false;
ef68e831 77
28ea5290
PS
78 spin_lock(&server->req_lock);
79 val = server->ops->get_credits_field(server, optype);
b340a4d4
SF
80
81 /* eg found case where write overlapping reconnect messed up credits */
82 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
83 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
84 server->hostname, *val);
335b7b62
PS
85 if ((instance == 0) || (instance == server->reconnect_instance))
86 *val += add;
87 else
88 reconnect_detected = true;
b340a4d4 89
141891f4
SF
90 if (*val > 65000) {
91 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
92 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
93 }
28ea5290 94 server->in_flight--;
ec2e4523 95 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
28ea5290 96 rc = change_conf(server);
983c88a4
PS
97 /*
98 * Sometimes server returns 0 credits on oplock break ack - we need to
99 * rebalance credits in this case.
100 */
101 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
102 server->oplocks) {
103 if (server->credits > 1) {
104 server->credits--;
105 server->oplock_credits++;
106 }
107 }
28ea5290
PS
108 spin_unlock(&server->req_lock);
109 wake_up(&server->request_q);
ef68e831 110
335b7b62
PS
111 if (reconnect_detected)
112 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
113 add, instance);
114
82e0457a
PS
115 if (server->tcpStatus == CifsNeedReconnect
116 || server->tcpStatus == CifsExiting)
ef68e831
PS
117 return;
118
119 switch (rc) {
120 case -1:
121 /* change_conf hasn't been executed */
122 break;
123 case 0:
124 cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
125 break;
126 case 1:
127 cifs_dbg(VFS, "disabling echoes and oplocks\n");
128 break;
129 case 2:
130 cifs_dbg(FYI, "disabling oplocks\n");
131 break;
132 default:
133 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
134 }
28ea5290
PS
135}
136
137static void
138smb2_set_credits(struct TCP_Server_Info *server, const int val)
139{
140 spin_lock(&server->req_lock);
141 server->credits = val;
9e1a37da
SF
142 if (val == 1)
143 server->reconnect_instance++;
28ea5290 144 spin_unlock(&server->req_lock);
6e4d3bbe
SF
145 /* don't log while holding the lock */
146 if (val == 1)
147 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
28ea5290
PS
148}
149
150static int *
151smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
152{
153 switch (optype) {
154 case CIFS_ECHO_OP:
155 return &server->echo_credits;
156 case CIFS_OBREAK_OP:
157 return &server->oplock_credits;
158 default:
159 return &server->credits;
160 }
161}
162
163static unsigned int
164smb2_get_credits(struct mid_q_entry *mid)
165{
49f466bd 166 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
31473fc4 167
3d3003fc
PS
168 if (mid->mid_state == MID_RESPONSE_RECEIVED
169 || mid->mid_state == MID_RESPONSE_MALFORMED)
170 return le16_to_cpu(shdr->CreditRequest);
171
172 return 0;
28ea5290 173}
2dc7e1c0 174
cb7e9eab
PS
175static int
176smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
335b7b62 177 unsigned int *num, struct cifs_credits *credits)
cb7e9eab
PS
178{
179 int rc = 0;
180 unsigned int scredits;
181
182 spin_lock(&server->req_lock);
183 while (1) {
184 if (server->credits <= 0) {
185 spin_unlock(&server->req_lock);
186 cifs_num_waiters_inc(server);
187 rc = wait_event_killable(server->request_q,
b227d215 188 has_credits(server, &server->credits, 1));
cb7e9eab
PS
189 cifs_num_waiters_dec(server);
190 if (rc)
191 return rc;
192 spin_lock(&server->req_lock);
193 } else {
194 if (server->tcpStatus == CifsExiting) {
195 spin_unlock(&server->req_lock);
196 return -ENOENT;
197 }
198
199 scredits = server->credits;
200 /* can deadlock with reopen */
acc58d0b 201 if (scredits <= 8) {
cb7e9eab 202 *num = SMB2_MAX_BUFFER_SIZE;
335b7b62
PS
203 credits->value = 0;
204 credits->instance = 0;
cb7e9eab
PS
205 break;
206 }
207
acc58d0b
PS
208 /* leave some credits for reopen and other ops */
209 scredits -= 8;
cb7e9eab
PS
210 *num = min_t(unsigned int, size,
211 scredits * SMB2_MAX_BUFFER_SIZE);
212
335b7b62
PS
213 credits->value =
214 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
215 credits->instance = server->reconnect_instance;
216 server->credits -= credits->value;
cb7e9eab
PS
217 server->in_flight++;
218 break;
219 }
220 }
221 spin_unlock(&server->req_lock);
222 return rc;
223}
224
9a1c67e8
PS
225static int
226smb2_adjust_credits(struct TCP_Server_Info *server,
227 struct cifs_credits *credits,
228 const unsigned int payload_size)
229{
230 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
231
232 if (!credits->value || credits->value == new_val)
233 return 0;
234
235 if (credits->value < new_val) {
236 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
237 credits->value, new_val);
238 return -ENOTSUPP;
239 }
240
241 spin_lock(&server->req_lock);
242
243 if (server->reconnect_instance != credits->instance) {
244 spin_unlock(&server->req_lock);
245 cifs_dbg(VFS, "trying to return %d credits to old session\n",
246 credits->value - new_val);
247 return -EAGAIN;
248 }
249
250 server->credits += credits->value - new_val;
251 spin_unlock(&server->req_lock);
252 wake_up(&server->request_q);
253 credits->value = new_val;
254 return 0;
255}
256
2dc7e1c0
PS
257static __u64
258smb2_get_next_mid(struct TCP_Server_Info *server)
259{
260 __u64 mid;
261 /* for SMB2 we need the current value */
262 spin_lock(&GlobalMid_Lock);
263 mid = server->CurrentMid++;
264 spin_unlock(&GlobalMid_Lock);
265 return mid;
266}
1080ef75 267
c781af7e
PS
268static void
269smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
270{
271 spin_lock(&GlobalMid_Lock);
272 if (server->CurrentMid >= val)
273 server->CurrentMid -= val;
274 spin_unlock(&GlobalMid_Lock);
275}
276
093b2bda
PS
277static struct mid_q_entry *
278smb2_find_mid(struct TCP_Server_Info *server, char *buf)
279{
280 struct mid_q_entry *mid;
49f466bd 281 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
31473fc4 282 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
093b2bda 283
31473fc4 284 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
373512ec
SF
285 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
286 return NULL;
287 }
288
093b2bda
PS
289 spin_lock(&GlobalMid_Lock);
290 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
9235d098 291 if ((mid->mid == wire_mid) &&
093b2bda 292 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
31473fc4 293 (mid->command == shdr->Command)) {
696e420b 294 kref_get(&mid->refcount);
093b2bda
PS
295 spin_unlock(&GlobalMid_Lock);
296 return mid;
297 }
298 }
299 spin_unlock(&GlobalMid_Lock);
300 return NULL;
301}
302
303static void
14547f7d 304smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
093b2bda
PS
305{
306#ifdef CONFIG_CIFS_DEBUG2
49f466bd 307 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
093b2bda 308
f96637be 309 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
31473fc4
PS
310 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
311 shdr->ProcessId);
14547f7d 312 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
71992e62 313 server->ops->calc_smb_size(buf, server));
093b2bda
PS
314#endif
315}
316
ec2e4523
PS
317static bool
318smb2_need_neg(struct TCP_Server_Info *server)
319{
320 return server->max_read == 0;
321}
322
323static int
324smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
325{
326 int rc;
327 ses->server->CurrentMid = 0;
328 rc = SMB2_negotiate(xid, ses);
329 /* BB we probably don't need to retry with modern servers */
330 if (rc == -EAGAIN)
331 rc = -EHOSTDOWN;
332 return rc;
333}
334
3a3bab50
PS
335static unsigned int
336smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
337{
338 struct TCP_Server_Info *server = tcon->ses->server;
339 unsigned int wsize;
340
341 /* start with specified wsize, or default */
342 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
343 wsize = min_t(unsigned int, wsize, server->max_write);
09902f8d 344#ifdef CONFIG_CIFS_SMB_DIRECT
bb4c0419
LL
345 if (server->rdma) {
346 if (server->sign)
347 wsize = min_t(unsigned int,
348 wsize, server->smbd_conn->max_fragmented_send_size);
349 else
350 wsize = min_t(unsigned int,
09902f8d 351 wsize, server->smbd_conn->max_readwrite_size);
bb4c0419 352 }
09902f8d 353#endif
cb7e9eab
PS
354 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
355 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 356
3a3bab50
PS
357 return wsize;
358}
359
3d621230
SF
360static unsigned int
361smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
362{
363 struct TCP_Server_Info *server = tcon->ses->server;
364 unsigned int wsize;
365
366 /* start with specified wsize, or default */
367 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
368 wsize = min_t(unsigned int, wsize, server->max_write);
369#ifdef CONFIG_CIFS_SMB_DIRECT
370 if (server->rdma) {
371 if (server->sign)
372 wsize = min_t(unsigned int,
373 wsize, server->smbd_conn->max_fragmented_send_size);
374 else
375 wsize = min_t(unsigned int,
376 wsize, server->smbd_conn->max_readwrite_size);
377 }
378#endif
379 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
380 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
381
382 return wsize;
383}
384
3a3bab50
PS
385static unsigned int
386smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
387{
388 struct TCP_Server_Info *server = tcon->ses->server;
389 unsigned int rsize;
390
391 /* start with specified rsize, or default */
392 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
393 rsize = min_t(unsigned int, rsize, server->max_read);
09902f8d 394#ifdef CONFIG_CIFS_SMB_DIRECT
bb4c0419
LL
395 if (server->rdma) {
396 if (server->sign)
397 rsize = min_t(unsigned int,
398 rsize, server->smbd_conn->max_fragmented_recv_size);
399 else
400 rsize = min_t(unsigned int,
09902f8d 401 rsize, server->smbd_conn->max_readwrite_size);
bb4c0419 402 }
09902f8d 403#endif
bed9da02
PS
404
405 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
406 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 407
3a3bab50
PS
408 return rsize;
409}
410
3d621230
SF
411static unsigned int
412smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
413{
414 struct TCP_Server_Info *server = tcon->ses->server;
415 unsigned int rsize;
416
417 /* start with specified rsize, or default */
418 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
419 rsize = min_t(unsigned int, rsize, server->max_read);
420#ifdef CONFIG_CIFS_SMB_DIRECT
421 if (server->rdma) {
422 if (server->sign)
423 rsize = min_t(unsigned int,
424 rsize, server->smbd_conn->max_fragmented_recv_size);
425 else
426 rsize = min_t(unsigned int,
427 rsize, server->smbd_conn->max_readwrite_size);
428 }
429#endif
430
431 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
432 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
433
434 return rsize;
435}
fe856be4
AA
436
437static int
438parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
439 size_t buf_len,
440 struct cifs_server_iface **iface_list,
441 size_t *iface_count)
442{
443 struct network_interface_info_ioctl_rsp *p;
444 struct sockaddr_in *addr4;
445 struct sockaddr_in6 *addr6;
446 struct iface_info_ipv4 *p4;
447 struct iface_info_ipv6 *p6;
448 struct cifs_server_iface *info;
449 ssize_t bytes_left;
450 size_t next = 0;
451 int nb_iface = 0;
452 int rc = 0;
453
454 *iface_list = NULL;
455 *iface_count = 0;
456
457 /*
458 * Fist pass: count and sanity check
459 */
460
461 bytes_left = buf_len;
462 p = buf;
463 while (bytes_left >= sizeof(*p)) {
464 nb_iface++;
465 next = le32_to_cpu(p->Next);
466 if (!next) {
467 bytes_left -= sizeof(*p);
468 break;
469 }
470 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
471 bytes_left -= next;
472 }
473
474 if (!nb_iface) {
475 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
476 rc = -EINVAL;
477 goto out;
478 }
479
480 if (bytes_left || p->Next)
481 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
482
483
484 /*
485 * Second pass: extract info to internal structure
486 */
487
488 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
489 if (!*iface_list) {
490 rc = -ENOMEM;
491 goto out;
492 }
493
494 info = *iface_list;
495 bytes_left = buf_len;
496 p = buf;
497 while (bytes_left >= sizeof(*p)) {
498 info->speed = le64_to_cpu(p->LinkSpeed);
499 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
500 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
501
502 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
503 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
504 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
505 le32_to_cpu(p->Capability));
506
507 switch (p->Family) {
508 /*
509 * The kernel and wire socket structures have the same
510 * layout and use network byte order but make the
511 * conversion explicit in case either one changes.
512 */
513 case INTERNETWORK:
514 addr4 = (struct sockaddr_in *)&info->sockaddr;
515 p4 = (struct iface_info_ipv4 *)p->Buffer;
516 addr4->sin_family = AF_INET;
517 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
518
519 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
520 addr4->sin_port = cpu_to_be16(CIFS_PORT);
521
522 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
523 &addr4->sin_addr);
524 break;
525 case INTERNETWORKV6:
526 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
527 p6 = (struct iface_info_ipv6 *)p->Buffer;
528 addr6->sin6_family = AF_INET6;
529 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
530
531 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
532 addr6->sin6_flowinfo = 0;
533 addr6->sin6_scope_id = 0;
534 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
535
536 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
537 &addr6->sin6_addr);
538 break;
539 default:
540 cifs_dbg(VFS,
541 "%s: skipping unsupported socket family\n",
542 __func__);
543 goto next_iface;
544 }
545
546 (*iface_count)++;
547 info++;
548next_iface:
549 next = le32_to_cpu(p->Next);
550 if (!next)
551 break;
552 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
553 bytes_left -= next;
554 }
555
556 if (!*iface_count) {
557 rc = -EINVAL;
558 goto out;
559 }
560
561out:
562 if (rc) {
563 kfree(*iface_list);
564 *iface_count = 0;
565 *iface_list = NULL;
566 }
567 return rc;
568}
569
570
c481e9fe
SF
571static int
572SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
573{
574 int rc;
575 unsigned int ret_data_len = 0;
fe856be4
AA
576 struct network_interface_info_ioctl_rsp *out_buf = NULL;
577 struct cifs_server_iface *iface_list;
578 size_t iface_count;
579 struct cifs_ses *ses = tcon->ses;
c481e9fe
SF
580
581 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
582 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
583 NULL /* no data input */, 0 /* no data input */,
153322f7 584 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
c3ed4402
SF
585 if (rc == -EOPNOTSUPP) {
586 cifs_dbg(FYI,
587 "server does not support query network interfaces\n");
588 goto out;
589 } else if (rc != 0) {
9ffc5412 590 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
fe856be4 591 goto out;
9ffc5412 592 }
fe856be4
AA
593
594 rc = parse_server_interfaces(out_buf, ret_data_len,
595 &iface_list, &iface_count);
596 if (rc)
597 goto out;
598
599 spin_lock(&ses->iface_lock);
600 kfree(ses->iface_list);
601 ses->iface_list = iface_list;
602 ses->iface_count = iface_count;
603 ses->iface_last_update = jiffies;
604 spin_unlock(&ses->iface_lock);
605
606out:
24df1483 607 kfree(out_buf);
c481e9fe
SF
608 return rc;
609}
c481e9fe 610
9da6ec77
RS
611static void
612smb2_close_cached_fid(struct kref *ref)
a93864d9 613{
9da6ec77
RS
614 struct cached_fid *cfid = container_of(ref, struct cached_fid,
615 refcount);
616
a93864d9
RS
617 if (cfid->is_valid) {
618 cifs_dbg(FYI, "clear cached root file handle\n");
619 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
620 cfid->fid->volatile_fid);
621 cfid->is_valid = false;
b0f6df73 622 cfid->file_all_info_is_valid = false;
a93864d9 623 }
9da6ec77
RS
624}
625
626void close_shroot(struct cached_fid *cfid)
627{
628 mutex_lock(&cfid->fid_mutex);
629 kref_put(&cfid->refcount, smb2_close_cached_fid);
a93864d9
RS
630 mutex_unlock(&cfid->fid_mutex);
631}
632
9da6ec77
RS
633void
634smb2_cached_lease_break(struct work_struct *work)
635{
636 struct cached_fid *cfid = container_of(work,
637 struct cached_fid, lease_break);
638
639 close_shroot(cfid);
640}
641
3d4ef9a1
SF
642/*
643 * Open the directory at the root of a share
644 */
645int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
646{
b0f6df73
RS
647 struct cifs_ses *ses = tcon->ses;
648 struct TCP_Server_Info *server = ses->server;
649 struct cifs_open_parms oparms;
650 struct smb2_create_rsp *o_rsp = NULL;
651 struct smb2_query_info_rsp *qi_rsp = NULL;
652 int resp_buftype[2];
653 struct smb_rqst rqst[2];
654 struct kvec rsp_iov[2];
655 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
656 struct kvec qi_iov[1];
657 int rc, flags = 0;
658 __le16 utf16_path = 0; /* Null - since an open of top of share */
a93864d9 659 u8 oplock = SMB2_OPLOCK_LEVEL_II;
3d4ef9a1 660
a93864d9
RS
661 mutex_lock(&tcon->crfid.fid_mutex);
662 if (tcon->crfid.is_valid) {
3d4ef9a1 663 cifs_dbg(FYI, "found a cached root file handle\n");
a93864d9 664 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
9da6ec77 665 kref_get(&tcon->crfid.refcount);
a93864d9 666 mutex_unlock(&tcon->crfid.fid_mutex);
3d4ef9a1
SF
667 return 0;
668 }
669
b0f6df73
RS
670 if (smb3_encryption_required(tcon))
671 flags |= CIFS_TRANSFORM_REQ;
672
673 memset(rqst, 0, sizeof(rqst));
674 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
675 memset(rsp_iov, 0, sizeof(rsp_iov));
676
677 /* Open */
678 memset(&open_iov, 0, sizeof(open_iov));
679 rqst[0].rq_iov = open_iov;
680 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
681
682 oparms.tcon = tcon;
683 oparms.create_options = 0;
684 oparms.desired_access = FILE_READ_ATTRIBUTES;
685 oparms.disposition = FILE_OPEN;
686 oparms.fid = pfid;
687 oparms.reconnect = false;
688
689 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
690 if (rc)
691 goto oshr_exit;
692 smb2_set_next_command(tcon, &rqst[0]);
693
694 memset(&qi_iov, 0, sizeof(qi_iov));
695 rqst[1].rq_iov = qi_iov;
696 rqst[1].rq_nvec = 1;
697
698 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
699 COMPOUND_FID, FILE_ALL_INFORMATION,
700 SMB2_O_INFO_FILE, 0,
701 sizeof(struct smb2_file_all_info) +
702 PATH_MAX * 2, 0, NULL);
703 if (rc)
704 goto oshr_exit;
705
706 smb2_set_related(&rqst[1]);
707
708 rc = compound_send_recv(xid, ses, flags, 2, rqst,
709 resp_buftype, rsp_iov);
710 if (rc)
711 goto oshr_exit;
712
713 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
714 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
715 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
716#ifdef CONFIG_CIFS_DEBUG2
717 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
718#endif /* CIFS_DEBUG2 */
719
b0f6df73
RS
720 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
721 tcon->crfid.tcon = tcon;
722 tcon->crfid.is_valid = true;
723 kref_init(&tcon->crfid.refcount);
b0f6df73 724
2f94a312
RS
725 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
726 kref_get(&tcon->crfid.refcount);
727 oplock = smb2_parse_lease_state(server, o_rsp,
728 &oparms.fid->epoch,
729 oparms.fid->lease_key);
730 } else
731 goto oshr_exit;
b0f6df73
RS
732
733 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
734 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
735 goto oshr_exit;
736 rc = smb2_validate_and_copy_iov(
737 le16_to_cpu(qi_rsp->OutputBufferOffset),
738 sizeof(struct smb2_file_all_info),
739 &rsp_iov[1], sizeof(struct smb2_file_all_info),
740 (char *)&tcon->crfid.file_all_info);
741 if (rc)
742 goto oshr_exit;
743 tcon->crfid.file_all_info_is_valid = 1;
744
745 oshr_exit:
a93864d9 746 mutex_unlock(&tcon->crfid.fid_mutex);
b0f6df73
RS
747 SMB2_open_free(&rqst[0]);
748 SMB2_query_info_free(&rqst[1]);
749 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
750 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3d4ef9a1
SF
751 return rc;
752}
753
af6a12ea
SF
754static void
755smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
756{
757 int rc;
758 __le16 srch_path = 0; /* Null - open root of share */
759 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
760 struct cifs_open_parms oparms;
761 struct cifs_fid fid;
3d4ef9a1 762 bool no_cached_open = tcon->nohandlecache;
af6a12ea
SF
763
764 oparms.tcon = tcon;
765 oparms.desired_access = FILE_READ_ATTRIBUTES;
766 oparms.disposition = FILE_OPEN;
767 oparms.create_options = 0;
768 oparms.fid = &fid;
769 oparms.reconnect = false;
770
3d4ef9a1 771 if (no_cached_open)
9d874c36
RS
772 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
773 NULL);
3d4ef9a1
SF
774 else
775 rc = open_shroot(xid, tcon, &fid);
776
af6a12ea
SF
777 if (rc)
778 return;
779
c481e9fe 780 SMB3_request_interfaces(xid, tcon);
c481e9fe 781
af6a12ea
SF
782 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
783 FS_ATTRIBUTE_INFORMATION);
784 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
785 FS_DEVICE_INFORMATION);
21ba3845
SF
786 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
787 FS_VOLUME_INFORMATION);
af6a12ea
SF
788 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
789 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
3d4ef9a1
SF
790 if (no_cached_open)
791 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
9da6ec77
RS
792 else
793 close_shroot(&tcon->crfid);
794
af6a12ea
SF
795 return;
796}
797
34f62640
SF
798static void
799smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
800{
801 int rc;
802 __le16 srch_path = 0; /* Null - open root of share */
803 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
804 struct cifs_open_parms oparms;
805 struct cifs_fid fid;
806
807 oparms.tcon = tcon;
808 oparms.desired_access = FILE_READ_ATTRIBUTES;
809 oparms.disposition = FILE_OPEN;
810 oparms.create_options = 0;
811 oparms.fid = &fid;
812 oparms.reconnect = false;
813
9d874c36 814 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
34f62640
SF
815 if (rc)
816 return;
817
2167114c
SF
818 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
819 FS_ATTRIBUTE_INFORMATION);
820 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
821 FS_DEVICE_INFORMATION);
34f62640
SF
822 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
823 return;
824}
825
2503a0db
PS
826static int
827smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
828 struct cifs_sb_info *cifs_sb, const char *full_path)
829{
830 int rc;
2503a0db 831 __le16 *utf16_path;
2e44b288 832 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
833 struct cifs_open_parms oparms;
834 struct cifs_fid fid;
2503a0db 835
a93864d9 836 if ((*full_path == 0) && tcon->crfid.is_valid)
3d4ef9a1
SF
837 return 0;
838
2503a0db
PS
839 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
840 if (!utf16_path)
841 return -ENOMEM;
842
064f6047
PS
843 oparms.tcon = tcon;
844 oparms.desired_access = FILE_READ_ATTRIBUTES;
845 oparms.disposition = FILE_OPEN;
5e19697b
SF
846 if (backup_cred(cifs_sb))
847 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
848 else
849 oparms.create_options = 0;
064f6047 850 oparms.fid = &fid;
9cbc0b73 851 oparms.reconnect = false;
064f6047 852
9d874c36 853 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2503a0db
PS
854 if (rc) {
855 kfree(utf16_path);
856 return rc;
857 }
858
064f6047 859 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2503a0db
PS
860 kfree(utf16_path);
861 return rc;
862}
863
be4cb9e3
PS
864static int
865smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
866 struct cifs_sb_info *cifs_sb, const char *full_path,
867 u64 *uniqueid, FILE_ALL_INFO *data)
868{
869 *uniqueid = le64_to_cpu(data->IndexNumber);
870 return 0;
871}
872
b7546bc5
PS
873static int
874smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
875 struct cifs_fid *fid, FILE_ALL_INFO *data)
876{
877 int rc;
878 struct smb2_file_all_info *smb2_data;
879
1bbe4997 880 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
b7546bc5
PS
881 GFP_KERNEL);
882 if (smb2_data == NULL)
883 return -ENOMEM;
884
885 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
886 smb2_data);
887 if (!rc)
888 move_smb2_info_to_cifs(data, smb2_data);
889 kfree(smb2_data);
890 return rc;
891}
892
1368f155 893#ifdef CONFIG_CIFS_XATTR
95907fea
RS
894static ssize_t
895move_smb2_ea_to_cifs(char *dst, size_t dst_size,
896 struct smb2_file_full_ea_info *src, size_t src_size,
897 const unsigned char *ea_name)
898{
899 int rc = 0;
900 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
901 char *name, *value;
0c5d6cb6 902 size_t buf_size = dst_size;
95907fea
RS
903 size_t name_len, value_len, user_name_len;
904
905 while (src_size > 0) {
906 name = &src->ea_data[0];
907 name_len = (size_t)src->ea_name_length;
908 value = &src->ea_data[src->ea_name_length + 1];
909 value_len = (size_t)le16_to_cpu(src->ea_value_length);
910
911 if (name_len == 0) {
912 break;
913 }
914
915 if (src_size < 8 + name_len + 1 + value_len) {
916 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
917 rc = -EIO;
918 goto out;
919 }
920
921 if (ea_name) {
922 if (ea_name_len == name_len &&
923 memcmp(ea_name, name, name_len) == 0) {
924 rc = value_len;
925 if (dst_size == 0)
926 goto out;
927 if (dst_size < value_len) {
928 rc = -ERANGE;
929 goto out;
930 }
931 memcpy(dst, value, value_len);
932 goto out;
933 }
934 } else {
935 /* 'user.' plus a terminating null */
936 user_name_len = 5 + 1 + name_len;
937
0c5d6cb6
RS
938 if (buf_size == 0) {
939 /* skip copy - calc size only */
940 rc += user_name_len;
941 } else if (dst_size >= user_name_len) {
95907fea
RS
942 dst_size -= user_name_len;
943 memcpy(dst, "user.", 5);
944 dst += 5;
945 memcpy(dst, src->ea_data, name_len);
946 dst += name_len;
947 *dst = 0;
948 ++dst;
0c5d6cb6 949 rc += user_name_len;
95907fea
RS
950 } else {
951 /* stop before overrun buffer */
952 rc = -ERANGE;
953 break;
954 }
955 }
956
957 if (!src->next_entry_offset)
958 break;
959
960 if (src_size < le32_to_cpu(src->next_entry_offset)) {
961 /* stop before overrun buffer */
962 rc = -ERANGE;
963 break;
964 }
965 src_size -= le32_to_cpu(src->next_entry_offset);
966 src = (void *)((char *)src +
967 le32_to_cpu(src->next_entry_offset));
968 }
969
970 /* didn't find the named attribute */
971 if (ea_name)
972 rc = -ENODATA;
973
974out:
975 return (ssize_t)rc;
976}
977
978static ssize_t
979smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
980 const unsigned char *path, const unsigned char *ea_name,
981 char *ea_data, size_t buf_size,
982 struct cifs_sb_info *cifs_sb)
983{
984 int rc;
985 __le16 *utf16_path;
f9793b6f
RS
986 struct kvec rsp_iov = {NULL, 0};
987 int buftype = CIFS_NO_BUFFER;
988 struct smb2_query_info_rsp *rsp;
989 struct smb2_file_full_ea_info *info = NULL;
95907fea
RS
990
991 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
992 if (!utf16_path)
993 return -ENOMEM;
994
f9793b6f
RS
995 rc = smb2_query_info_compound(xid, tcon, utf16_path,
996 FILE_READ_EA,
997 FILE_FULL_EA_INFORMATION,
998 SMB2_O_INFO_FILE,
c4627e66
RS
999 CIFSMaxBufSize -
1000 MAX_SMB2_CREATE_RESPONSE_SIZE -
1001 MAX_SMB2_CLOSE_RESPONSE_SIZE,
f9793b6f 1002 &rsp_iov, &buftype, cifs_sb);
95907fea 1003 if (rc) {
f9793b6f
RS
1004 /*
1005 * If ea_name is NULL (listxattr) and there are no EAs,
1006 * return 0 as it's not an error. Otherwise, the specified
1007 * ea_name was not found.
1008 */
1009 if (!ea_name && rc == -ENODATA)
1010 rc = 0;
1011 goto qeas_exit;
95907fea
RS
1012 }
1013
f9793b6f
RS
1014 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1015 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1016 le32_to_cpu(rsp->OutputBufferLength),
1017 &rsp_iov,
1018 sizeof(struct smb2_file_full_ea_info));
1019 if (rc)
1020 goto qeas_exit;
95907fea 1021
f9793b6f
RS
1022 info = (struct smb2_file_full_ea_info *)(
1023 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1024 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1025 le32_to_cpu(rsp->OutputBufferLength), ea_name);
95907fea 1026
f9793b6f
RS
1027 qeas_exit:
1028 kfree(utf16_path);
1029 free_rsp_buf(buftype, rsp_iov.iov_base);
95907fea
RS
1030 return rc;
1031}
1032
5517554e
RS
1033
1034static int
1035smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1036 const char *path, const char *ea_name, const void *ea_value,
1037 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1038 struct cifs_sb_info *cifs_sb)
1039{
0967e545 1040 struct cifs_ses *ses = tcon->ses;
0967e545 1041 __le16 *utf16_path = NULL;
5517554e 1042 int ea_name_len = strlen(ea_name);
0967e545 1043 int flags = 0;
5517554e 1044 int len;
0967e545
RS
1045 struct smb_rqst rqst[3];
1046 int resp_buftype[3];
1047 struct kvec rsp_iov[3];
1048 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1049 struct cifs_open_parms oparms;
1050 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1051 struct cifs_fid fid;
1052 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1053 unsigned int size[1];
1054 void *data[1];
1055 struct smb2_file_full_ea_info *ea = NULL;
1056 struct kvec close_iov[1];
1057 int rc;
1058
1059 if (smb3_encryption_required(tcon))
1060 flags |= CIFS_TRANSFORM_REQ;
5517554e
RS
1061
1062 if (ea_name_len > 255)
1063 return -EINVAL;
1064
1065 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1066 if (!utf16_path)
1067 return -ENOMEM;
1068
0967e545
RS
1069 memset(rqst, 0, sizeof(rqst));
1070 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1071 memset(rsp_iov, 0, sizeof(rsp_iov));
1072
21094641
RS
1073 if (ses->server->ops->query_all_EAs) {
1074 if (!ea_value) {
1075 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1076 ea_name, NULL, 0,
1077 cifs_sb);
1078 if (rc == -ENODATA)
1079 goto sea_exit;
1080 }
1081 }
1082
0967e545
RS
1083 /* Open */
1084 memset(&open_iov, 0, sizeof(open_iov));
1085 rqst[0].rq_iov = open_iov;
1086 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1087
1088 memset(&oparms, 0, sizeof(oparms));
5517554e
RS
1089 oparms.tcon = tcon;
1090 oparms.desired_access = FILE_WRITE_EA;
1091 oparms.disposition = FILE_OPEN;
5e19697b
SF
1092 if (backup_cred(cifs_sb))
1093 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1094 else
1095 oparms.create_options = 0;
5517554e
RS
1096 oparms.fid = &fid;
1097 oparms.reconnect = false;
1098
0967e545
RS
1099 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1100 if (rc)
1101 goto sea_exit;
e77fe73c 1102 smb2_set_next_command(tcon, &rqst[0]);
0967e545
RS
1103
1104
1105 /* Set Info */
1106 memset(&si_iov, 0, sizeof(si_iov));
1107 rqst[1].rq_iov = si_iov;
1108 rqst[1].rq_nvec = 1;
5517554e
RS
1109
1110 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1111 ea = kzalloc(len, GFP_KERNEL);
1112 if (ea == NULL) {
0967e545
RS
1113 rc = -ENOMEM;
1114 goto sea_exit;
5517554e
RS
1115 }
1116
1117 ea->ea_name_length = ea_name_len;
1118 ea->ea_value_length = cpu_to_le16(ea_value_len);
1119 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1120 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1121
0967e545
RS
1122 size[0] = len;
1123 data[0] = ea;
1124
1125 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1126 COMPOUND_FID, current->tgid,
1127 FILE_FULL_EA_INFORMATION,
1128 SMB2_O_INFO_FILE, 0, data, size);
e77fe73c 1129 smb2_set_next_command(tcon, &rqst[1]);
0967e545 1130 smb2_set_related(&rqst[1]);
6aa0c114 1131
5517554e 1132
0967e545
RS
1133 /* Close */
1134 memset(&close_iov, 0, sizeof(close_iov));
1135 rqst[2].rq_iov = close_iov;
1136 rqst[2].rq_nvec = 1;
1137 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1138 smb2_set_related(&rqst[2]);
1139
1140 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1141 resp_buftype, rsp_iov);
1142
1143 sea_exit:
1144 kfree(ea);
1145 kfree(utf16_path);
1146 SMB2_open_free(&rqst[0]);
1147 SMB2_set_info_free(&rqst[1]);
1148 SMB2_close_free(&rqst[2]);
1149 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1150 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1151 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
5517554e
RS
1152 return rc;
1153}
1368f155 1154#endif
5517554e 1155
9094fad1
PS
1156static bool
1157smb2_can_echo(struct TCP_Server_Info *server)
1158{
1159 return server->echoes;
1160}
1161
d60622eb
PS
1162static void
1163smb2_clear_stats(struct cifs_tcon *tcon)
1164{
d60622eb
PS
1165 int i;
1166 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1167 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1168 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1169 }
d60622eb
PS
1170}
1171
769ee6a4
SF
1172static void
1173smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1174{
1175 seq_puts(m, "\n\tShare Capabilities:");
1176 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1177 seq_puts(m, " DFS,");
1178 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1179 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1180 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1181 seq_puts(m, " SCALEOUT,");
1182 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1183 seq_puts(m, " CLUSTER,");
1184 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1185 seq_puts(m, " ASYMMETRIC,");
1186 if (tcon->capabilities == 0)
1187 seq_puts(m, " None");
af6a12ea
SF
1188 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1189 seq_puts(m, " Aligned,");
1190 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1191 seq_puts(m, " Partition Aligned,");
1192 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1193 seq_puts(m, " SSD,");
1194 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1195 seq_puts(m, " TRIM-support,");
1196
769ee6a4 1197 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
e0386e44 1198 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
af6a12ea
SF
1199 if (tcon->perf_sector_size)
1200 seq_printf(m, "\tOptimal sector size: 0x%x",
1201 tcon->perf_sector_size);
e0386e44 1202 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
769ee6a4
SF
1203}
1204
d60622eb
PS
1205static void
1206smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1207{
d60622eb
PS
1208 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1209 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1995d28f
SF
1210
1211 /*
1212 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1213 * totals (requests sent) since those SMBs are per-session not per tcon
1214 */
52ce1ac4
SF
1215 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1216 (long long)(tcon->bytes_read),
1217 (long long)(tcon->bytes_written));
fae8044c
SF
1218 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1219 atomic_read(&tcon->num_local_opens),
1220 atomic_read(&tcon->num_remote_opens));
1995d28f 1221 seq_printf(m, "\nTreeConnects: %d total %d failed",
d60622eb
PS
1222 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1223 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1995d28f 1224 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
d60622eb
PS
1225 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1226 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1995d28f 1227 seq_printf(m, "\nCreates: %d total %d failed",
d60622eb
PS
1228 atomic_read(&sent[SMB2_CREATE_HE]),
1229 atomic_read(&failed[SMB2_CREATE_HE]));
1995d28f 1230 seq_printf(m, "\nCloses: %d total %d failed",
d60622eb
PS
1231 atomic_read(&sent[SMB2_CLOSE_HE]),
1232 atomic_read(&failed[SMB2_CLOSE_HE]));
1995d28f 1233 seq_printf(m, "\nFlushes: %d total %d failed",
d60622eb
PS
1234 atomic_read(&sent[SMB2_FLUSH_HE]),
1235 atomic_read(&failed[SMB2_FLUSH_HE]));
1995d28f 1236 seq_printf(m, "\nReads: %d total %d failed",
d60622eb
PS
1237 atomic_read(&sent[SMB2_READ_HE]),
1238 atomic_read(&failed[SMB2_READ_HE]));
1995d28f 1239 seq_printf(m, "\nWrites: %d total %d failed",
d60622eb
PS
1240 atomic_read(&sent[SMB2_WRITE_HE]),
1241 atomic_read(&failed[SMB2_WRITE_HE]));
1995d28f 1242 seq_printf(m, "\nLocks: %d total %d failed",
d60622eb
PS
1243 atomic_read(&sent[SMB2_LOCK_HE]),
1244 atomic_read(&failed[SMB2_LOCK_HE]));
1995d28f 1245 seq_printf(m, "\nIOCTLs: %d total %d failed",
d60622eb
PS
1246 atomic_read(&sent[SMB2_IOCTL_HE]),
1247 atomic_read(&failed[SMB2_IOCTL_HE]));
1995d28f 1248 seq_printf(m, "\nQueryDirectories: %d total %d failed",
d60622eb
PS
1249 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1250 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1995d28f 1251 seq_printf(m, "\nChangeNotifies: %d total %d failed",
d60622eb
PS
1252 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1253 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1995d28f 1254 seq_printf(m, "\nQueryInfos: %d total %d failed",
d60622eb
PS
1255 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1256 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1995d28f 1257 seq_printf(m, "\nSetInfos: %d total %d failed",
d60622eb
PS
1258 atomic_read(&sent[SMB2_SET_INFO_HE]),
1259 atomic_read(&failed[SMB2_SET_INFO_HE]));
1260 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1261 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1262 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
d60622eb
PS
1263}
1264
f0df737e
PS
1265static void
1266smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1267{
2b0143b5 1268 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
53ef1016
PS
1269 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1270
f0df737e
PS
1271 cfile->fid.persistent_fid = fid->persistent_fid;
1272 cfile->fid.volatile_fid = fid->volatile_fid;
dfe33f9a
SF
1273#ifdef CONFIG_CIFS_DEBUG2
1274 cfile->fid.mid = fid->mid;
1275#endif /* CIFS_DEBUG2 */
42873b0a
PS
1276 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1277 &fid->purge_cache);
18cceb6a 1278 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
94f87371 1279 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
f0df737e
PS
1280}
1281
760ad0ca 1282static void
f0df737e
PS
1283smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1284 struct cifs_fid *fid)
1285{
760ad0ca 1286 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
f0df737e
PS
1287}
1288
41c1358e
SF
1289static int
1290SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1291 u64 persistent_fid, u64 volatile_fid,
1292 struct copychunk_ioctl *pcchunk)
1293{
1294 int rc;
1295 unsigned int ret_data_len;
1296 struct resume_key_req *res_key;
1297
1298 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1299 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
153322f7 1300 NULL, 0 /* no input */, CIFSMaxBufSize,
41c1358e
SF
1301 (char **)&res_key, &ret_data_len);
1302
1303 if (rc) {
1304 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1305 goto req_res_key_exit;
1306 }
1307 if (ret_data_len < sizeof(struct resume_key_req)) {
1308 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1309 rc = -EINVAL;
1310 goto req_res_key_exit;
1311 }
1312 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1313
1314req_res_key_exit:
1315 kfree(res_key);
1316 return rc;
1317}
1318
f5b05d62
RS
1319static int
1320smb2_ioctl_query_info(const unsigned int xid,
8d8b26e5
RS
1321 struct cifs_tcon *tcon,
1322 __le16 *path, int is_dir,
f5b05d62
RS
1323 unsigned long p)
1324{
f5b05d62
RS
1325 struct cifs_ses *ses = tcon->ses;
1326 char __user *arg = (char __user *)p;
1327 struct smb_query_info qi;
1328 struct smb_query_info __user *pqi;
1329 int rc = 0;
1330 int flags = 0;
f5778c39
RS
1331 struct smb2_query_info_rsp *qi_rsp = NULL;
1332 struct smb2_ioctl_rsp *io_rsp = NULL;
8d8b26e5
RS
1333 void *buffer = NULL;
1334 struct smb_rqst rqst[3];
1335 int resp_buftype[3];
1336 struct kvec rsp_iov[3];
1337 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1338 struct cifs_open_parms oparms;
1339 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1340 struct cifs_fid fid;
1341 struct kvec qi_iov[1];
f5778c39 1342 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
8d8b26e5
RS
1343 struct kvec close_iov[1];
1344
1345 memset(rqst, 0, sizeof(rqst));
1346 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1347 memset(rsp_iov, 0, sizeof(rsp_iov));
f5b05d62
RS
1348
1349 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1350 return -EFAULT;
1351
1352 if (qi.output_buffer_length > 1024)
1353 return -EINVAL;
1354
1355 if (!ses || !(ses->server))
1356 return -EIO;
1357
1358 if (smb3_encryption_required(tcon))
1359 flags |= CIFS_TRANSFORM_REQ;
1360
1361 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1362 if (buffer == NULL)
1363 return -ENOMEM;
1364
1365 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1366 qi.output_buffer_length)) {
8d8b26e5
RS
1367 rc = -EFAULT;
1368 goto iqinf_exit;
f5b05d62
RS
1369 }
1370
8d8b26e5
RS
1371 /* Open */
1372 memset(&open_iov, 0, sizeof(open_iov));
1373 rqst[0].rq_iov = open_iov;
1374 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1375
1376 memset(&oparms, 0, sizeof(oparms));
1377 oparms.tcon = tcon;
1378 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1379 oparms.disposition = FILE_OPEN;
1380 if (is_dir)
1381 oparms.create_options = CREATE_NOT_FILE;
1382 else
1383 oparms.create_options = CREATE_NOT_DIR;
1384 oparms.fid = &fid;
1385 oparms.reconnect = false;
1386
1387 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1388 if (rc)
1389 goto iqinf_exit;
e77fe73c 1390 smb2_set_next_command(tcon, &rqst[0]);
8d8b26e5
RS
1391
1392 /* Query */
31ba4331
SF
1393 if (qi.flags & PASSTHRU_FSCTL) {
1394 /* Can eventually relax perm check since server enforces too */
1395 if (!capable(CAP_SYS_ADMIN))
1396 rc = -EPERM;
f5778c39
RS
1397 else {
1398 memset(&io_iov, 0, sizeof(io_iov));
1399 rqst[1].rq_iov = io_iov;
1400 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1401
1402 rc = SMB2_ioctl_init(tcon, &rqst[1],
1403 COMPOUND_FID, COMPOUND_FID,
1404 qi.info_type, true, NULL,
153322f7 1405 0, CIFSMaxBufSize);
f5778c39 1406 }
31ba4331
SF
1407 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1408 memset(&qi_iov, 0, sizeof(qi_iov));
1409 rqst[1].rq_iov = qi_iov;
1410 rqst[1].rq_nvec = 1;
1411
1412 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1413 COMPOUND_FID, qi.file_info_class,
1414 qi.info_type, qi.additional_information,
f5b05d62
RS
1415 qi.input_buffer_length,
1416 qi.output_buffer_length, buffer);
31ba4331
SF
1417 } else { /* unknown flags */
1418 cifs_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1419 rc = -EINVAL;
1420 }
1421
f5b05d62
RS
1422 if (rc)
1423 goto iqinf_exit;
e77fe73c 1424 smb2_set_next_command(tcon, &rqst[1]);
8d8b26e5
RS
1425 smb2_set_related(&rqst[1]);
1426
1427 /* Close */
1428 memset(&close_iov, 0, sizeof(close_iov));
1429 rqst[2].rq_iov = close_iov;
1430 rqst[2].rq_nvec = 1;
f5b05d62 1431
8d8b26e5 1432 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
f5b05d62
RS
1433 if (rc)
1434 goto iqinf_exit;
8d8b26e5 1435 smb2_set_related(&rqst[2]);
f5b05d62 1436
8d8b26e5
RS
1437 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1438 resp_buftype, rsp_iov);
1439 if (rc)
1440 goto iqinf_exit;
f5778c39
RS
1441 if (qi.flags & PASSTHRU_FSCTL) {
1442 pqi = (struct smb_query_info __user *)arg;
1443 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1444 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1445 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1446 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1447 sizeof(qi.input_buffer_length))) {
1448 rc = -EFAULT;
1449 goto iqinf_exit;
1450 }
1451 if (copy_to_user(pqi + 1, &io_rsp[1], qi.input_buffer_length)) {
1452 rc = -EFAULT;
1453 goto iqinf_exit;
1454 }
1455 } else {
1456 pqi = (struct smb_query_info __user *)arg;
1457 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1458 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1459 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1460 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1461 sizeof(qi.input_buffer_length))) {
1462 rc = -EFAULT;
1463 goto iqinf_exit;
1464 }
1465 if (copy_to_user(pqi + 1, qi_rsp->Buffer, qi.input_buffer_length)) {
1466 rc = -EFAULT;
1467 goto iqinf_exit;
1468 }
f5b05d62
RS
1469 }
1470
1471 iqinf_exit:
8d8b26e5
RS
1472 kfree(buffer);
1473 SMB2_open_free(&rqst[0]);
f5778c39
RS
1474 if (qi.flags & PASSTHRU_FSCTL)
1475 SMB2_ioctl_free(&rqst[1]);
1476 else
1477 SMB2_query_info_free(&rqst[1]);
1478
8d8b26e5
RS
1479 SMB2_close_free(&rqst[2]);
1480 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1481 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1482 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
f5b05d62
RS
1483 return rc;
1484}
1485
620d8745 1486static ssize_t
312bbc59 1487smb2_copychunk_range(const unsigned int xid,
41c1358e
SF
1488 struct cifsFileInfo *srcfile,
1489 struct cifsFileInfo *trgtfile, u64 src_off,
1490 u64 len, u64 dest_off)
1491{
1492 int rc;
1493 unsigned int ret_data_len;
1494 struct copychunk_ioctl *pcchunk;
9bf0c9cd
SF
1495 struct copychunk_ioctl_rsp *retbuf = NULL;
1496 struct cifs_tcon *tcon;
1497 int chunks_copied = 0;
1498 bool chunk_sizes_updated = false;
620d8745 1499 ssize_t bytes_written, total_bytes_written = 0;
41c1358e
SF
1500
1501 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1502
1503 if (pcchunk == NULL)
1504 return -ENOMEM;
1505
312bbc59 1506 cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
41c1358e
SF
1507 /* Request a key from the server to identify the source of the copy */
1508 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1509 srcfile->fid.persistent_fid,
1510 srcfile->fid.volatile_fid, pcchunk);
1511
1512 /* Note: request_res_key sets res_key null only if rc !=0 */
1513 if (rc)
9bf0c9cd 1514 goto cchunk_out;
41c1358e
SF
1515
1516 /* For now array only one chunk long, will make more flexible later */
bc09d141 1517 pcchunk->ChunkCount = cpu_to_le32(1);
41c1358e 1518 pcchunk->Reserved = 0;
41c1358e
SF
1519 pcchunk->Reserved2 = 0;
1520
9bf0c9cd 1521 tcon = tlink_tcon(trgtfile->tlink);
41c1358e 1522
9bf0c9cd
SF
1523 while (len > 0) {
1524 pcchunk->SourceOffset = cpu_to_le64(src_off);
1525 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1526 pcchunk->Length =
1527 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
41c1358e 1528
9bf0c9cd
SF
1529 /* Request server copy to target from src identified by key */
1530 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1531 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
63a83b86 1532 true /* is_fsctl */, (char *)pcchunk,
153322f7
SF
1533 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1534 (char **)&retbuf, &ret_data_len);
9bf0c9cd
SF
1535 if (rc == 0) {
1536 if (ret_data_len !=
1537 sizeof(struct copychunk_ioctl_rsp)) {
1538 cifs_dbg(VFS, "invalid cchunk response size\n");
1539 rc = -EIO;
1540 goto cchunk_out;
1541 }
1542 if (retbuf->TotalBytesWritten == 0) {
1543 cifs_dbg(FYI, "no bytes copied\n");
1544 rc = -EIO;
1545 goto cchunk_out;
1546 }
1547 /*
1548 * Check if server claimed to write more than we asked
1549 */
1550 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1551 le32_to_cpu(pcchunk->Length)) {
1552 cifs_dbg(VFS, "invalid copy chunk response\n");
1553 rc = -EIO;
1554 goto cchunk_out;
1555 }
1556 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1557 cifs_dbg(VFS, "invalid num chunks written\n");
1558 rc = -EIO;
1559 goto cchunk_out;
1560 }
1561 chunks_copied++;
1562
620d8745
SP
1563 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1564 src_off += bytes_written;
1565 dest_off += bytes_written;
1566 len -= bytes_written;
1567 total_bytes_written += bytes_written;
9bf0c9cd 1568
620d8745 1569 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
9bf0c9cd
SF
1570 le32_to_cpu(retbuf->ChunksWritten),
1571 le32_to_cpu(retbuf->ChunkBytesWritten),
620d8745 1572 bytes_written);
9bf0c9cd
SF
1573 } else if (rc == -EINVAL) {
1574 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1575 goto cchunk_out;
1576
1577 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1578 le32_to_cpu(retbuf->ChunksWritten),
1579 le32_to_cpu(retbuf->ChunkBytesWritten),
1580 le32_to_cpu(retbuf->TotalBytesWritten));
1581
1582 /*
1583 * Check if this is the first request using these sizes,
1584 * (ie check if copy succeed once with original sizes
1585 * and check if the server gave us different sizes after
1586 * we already updated max sizes on previous request).
1587 * if not then why is the server returning an error now
1588 */
1589 if ((chunks_copied != 0) || chunk_sizes_updated)
1590 goto cchunk_out;
1591
1592 /* Check that server is not asking us to grow size */
1593 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1594 tcon->max_bytes_chunk)
1595 tcon->max_bytes_chunk =
1596 le32_to_cpu(retbuf->ChunkBytesWritten);
1597 else
1598 goto cchunk_out; /* server gave us bogus size */
1599
1600 /* No need to change MaxChunks since already set to 1 */
1601 chunk_sizes_updated = true;
2477bc58
SP
1602 } else
1603 goto cchunk_out;
9bf0c9cd 1604 }
41c1358e 1605
9bf0c9cd 1606cchunk_out:
41c1358e 1607 kfree(pcchunk);
24df1483 1608 kfree(retbuf);
620d8745
SP
1609 if (rc)
1610 return rc;
1611 else
1612 return total_bytes_written;
41c1358e
SF
1613}
1614
7a5cfb19
PS
1615static int
1616smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1617 struct cifs_fid *fid)
1618{
1619 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1620}
1621
09a4707e
PS
1622static unsigned int
1623smb2_read_data_offset(char *buf)
1624{
1625 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1626 return rsp->DataOffset;
1627}
1628
1629static unsigned int
74dcf418 1630smb2_read_data_length(char *buf, bool in_remaining)
09a4707e
PS
1631{
1632 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
74dcf418
LL
1633
1634 if (in_remaining)
1635 return le32_to_cpu(rsp->DataRemaining);
1636
09a4707e
PS
1637 return le32_to_cpu(rsp->DataLength);
1638}
1639
d8e05039
PS
1640
1641static int
db8b631d 1642smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
d8e05039
PS
1643 struct cifs_io_parms *parms, unsigned int *bytes_read,
1644 char **buf, int *buf_type)
1645{
db8b631d
SF
1646 parms->persistent_fid = pfid->persistent_fid;
1647 parms->volatile_fid = pfid->volatile_fid;
d8e05039
PS
1648 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1649}
1650
009d3443 1651static int
db8b631d 1652smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
009d3443
PS
1653 struct cifs_io_parms *parms, unsigned int *written,
1654 struct kvec *iov, unsigned long nr_segs)
1655{
1656
db8b631d
SF
1657 parms->persistent_fid = pfid->persistent_fid;
1658 parms->volatile_fid = pfid->volatile_fid;
009d3443
PS
1659 return SMB2_write(xid, parms, written, iov, nr_segs);
1660}
1661
d43cc793
SF
1662/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1663static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1664 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1665{
1666 struct cifsInodeInfo *cifsi;
1667 int rc;
1668
1669 cifsi = CIFS_I(inode);
1670
1671 /* if file already sparse don't bother setting sparse again */
1672 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1673 return true; /* already sparse */
1674
1675 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1676 return true; /* already not sparse */
1677
1678 /*
1679 * Can't check for sparse support on share the usual way via the
1680 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1681 * since Samba server doesn't set the flag on the share, yet
1682 * supports the set sparse FSCTL and returns sparse correctly
1683 * in the file attributes. If we fail setting sparse though we
1684 * mark that server does not support sparse files for this share
1685 * to avoid repeatedly sending the unsupported fsctl to server
1686 * if the file is repeatedly extended.
1687 */
1688 if (tcon->broken_sparse_sup)
1689 return false;
1690
1691 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1692 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
63a83b86 1693 true /* is_fctl */,
153322f7 1694 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
d43cc793
SF
1695 if (rc) {
1696 tcon->broken_sparse_sup = true;
1697 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1698 return false;
1699 }
1700
1701 if (setsparse)
1702 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1703 else
1704 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1705
1706 return true;
1707}
1708
c839ff24
PS
1709static int
1710smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1711 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1712{
1713 __le64 eof = cpu_to_le64(size);
3d1a3745
SF
1714 struct inode *inode;
1715
1716 /*
1717 * If extending file more than one page make sparse. Many Linux fs
1718 * make files sparse by default when extending via ftruncate
1719 */
2b0143b5 1720 inode = d_inode(cfile->dentry);
3d1a3745
SF
1721
1722 if (!set_alloc && (size > inode->i_size + 8192)) {
3d1a3745 1723 __u8 set_sparse = 1;
d43cc793
SF
1724
1725 /* whether set sparse succeeds or not, extend the file */
1726 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
3d1a3745
SF
1727 }
1728
c839ff24 1729 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3764cbd1 1730 cfile->fid.volatile_fid, cfile->pid, &eof);
c839ff24
PS
1731}
1732
02b16665
SF
1733static int
1734smb2_duplicate_extents(const unsigned int xid,
1735 struct cifsFileInfo *srcfile,
1736 struct cifsFileInfo *trgtfile, u64 src_off,
1737 u64 len, u64 dest_off)
1738{
1739 int rc;
1740 unsigned int ret_data_len;
02b16665
SF
1741 struct duplicate_extents_to_file dup_ext_buf;
1742 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1743
1744 /* server fileays advertise duplicate extent support with this flag */
1745 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1746 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1747 return -EOPNOTSUPP;
1748
1749 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1750 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1751 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1752 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1753 dup_ext_buf.ByteCount = cpu_to_le64(len);
1754 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1755 src_off, dest_off, len);
1756
1757 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1758 if (rc)
1759 goto duplicate_extents_out;
1760
1761 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1762 trgtfile->fid.volatile_fid,
1763 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
63a83b86 1764 true /* is_fsctl */,
51146625 1765 (char *)&dup_ext_buf,
02b16665 1766 sizeof(struct duplicate_extents_to_file),
153322f7 1767 CIFSMaxBufSize, NULL,
02b16665
SF
1768 &ret_data_len);
1769
1770 if (ret_data_len > 0)
1771 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1772
1773duplicate_extents_out:
1774 return rc;
1775}
02b16665 1776
64a5cfa6
SF
1777static int
1778smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1779 struct cifsFileInfo *cfile)
1780{
1781 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1782 cfile->fid.volatile_fid);
1783}
1784
b3152e2c
SF
1785static int
1786smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1787 struct cifsFileInfo *cfile)
1788{
1789 struct fsctl_set_integrity_information_req integr_info;
b3152e2c
SF
1790 unsigned int ret_data_len;
1791
1792 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1793 integr_info.Flags = 0;
1794 integr_info.Reserved = 0;
1795
1796 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1797 cfile->fid.volatile_fid,
1798 FSCTL_SET_INTEGRITY_INFORMATION,
63a83b86 1799 true /* is_fsctl */,
51146625 1800 (char *)&integr_info,
b3152e2c 1801 sizeof(struct fsctl_set_integrity_information_req),
153322f7 1802 CIFSMaxBufSize, NULL,
b3152e2c
SF
1803 &ret_data_len);
1804
1805}
1806
e02789a5
SF
1807/* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1808#define GMT_TOKEN_SIZE 50
1809
153322f7
SF
1810#define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1811
e02789a5
SF
1812/*
1813 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1814 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1815 */
834170c8
SF
1816static int
1817smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1818 struct cifsFileInfo *cfile, void __user *ioc_buf)
1819{
1820 char *retbuf = NULL;
1821 unsigned int ret_data_len = 0;
1822 int rc;
153322f7 1823 u32 max_response_size;
834170c8
SF
1824 struct smb_snapshot_array snapshot_in;
1825
153322f7
SF
1826 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1827 return -EFAULT;
1828
1829 /*
1830 * Note that for snapshot queries that servers like Azure expect that
1831 * the first query be minimal size (and just used to get the number/size
1832 * of previous versions) so response size must be specified as EXACTLY
1833 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
1834 * of eight bytes.
1835 */
1836 if (ret_data_len == 0)
1837 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
1838 else
1839 max_response_size = CIFSMaxBufSize;
1840
834170c8
SF
1841 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1842 cfile->fid.volatile_fid,
1843 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
63a83b86 1844 true /* is_fsctl */,
153322f7 1845 NULL, 0 /* no input data */, max_response_size,
834170c8
SF
1846 (char **)&retbuf,
1847 &ret_data_len);
1848 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1849 rc, ret_data_len);
1850 if (rc)
1851 return rc;
1852
1853 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1854 /* Fixup buffer */
1855 if (copy_from_user(&snapshot_in, ioc_buf,
1856 sizeof(struct smb_snapshot_array))) {
1857 rc = -EFAULT;
1858 kfree(retbuf);
1859 return rc;
1860 }
834170c8 1861
e02789a5
SF
1862 /*
1863 * Check for min size, ie not large enough to fit even one GMT
1864 * token (snapshot). On the first ioctl some users may pass in
1865 * smaller size (or zero) to simply get the size of the array
1866 * so the user space caller can allocate sufficient memory
1867 * and retry the ioctl again with larger array size sufficient
1868 * to hold all of the snapshot GMT tokens on the second try.
1869 */
1870 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1871 ret_data_len = sizeof(struct smb_snapshot_array);
1872
1873 /*
1874 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1875 * the snapshot array (of 50 byte GMT tokens) each
1876 * representing an available previous version of the data
1877 */
1878 if (ret_data_len > (snapshot_in.snapshot_array_size +
1879 sizeof(struct smb_snapshot_array)))
1880 ret_data_len = snapshot_in.snapshot_array_size +
1881 sizeof(struct smb_snapshot_array);
834170c8
SF
1882
1883 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1884 rc = -EFAULT;
1885 }
1886
1887 kfree(retbuf);
1888 return rc;
1889}
1890
d324f08d
PS
1891static int
1892smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1893 const char *path, struct cifs_sb_info *cifs_sb,
1894 struct cifs_fid *fid, __u16 search_flags,
1895 struct cifs_search_info *srch_inf)
1896{
1897 __le16 *utf16_path;
1898 int rc;
2e44b288 1899 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047 1900 struct cifs_open_parms oparms;
d324f08d
PS
1901
1902 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1903 if (!utf16_path)
1904 return -ENOMEM;
1905
064f6047
PS
1906 oparms.tcon = tcon;
1907 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1908 oparms.disposition = FILE_OPEN;
5e19697b
SF
1909 if (backup_cred(cifs_sb))
1910 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1911 else
1912 oparms.create_options = 0;
064f6047 1913 oparms.fid = fid;
9cbc0b73 1914 oparms.reconnect = false;
064f6047 1915
9d874c36 1916 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
d324f08d
PS
1917 kfree(utf16_path);
1918 if (rc) {
dcd87838 1919 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
d324f08d
PS
1920 return rc;
1921 }
1922
1923 srch_inf->entries_in_buffer = 0;
0595751f 1924 srch_inf->index_of_last_entry = 2;
d324f08d 1925
064f6047
PS
1926 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1927 fid->volatile_fid, 0, srch_inf);
d324f08d 1928 if (rc) {
dcd87838 1929 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
064f6047 1930 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
d324f08d
PS
1931 }
1932 return rc;
1933}
1934
1935static int
1936smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1937 struct cifs_fid *fid, __u16 search_flags,
1938 struct cifs_search_info *srch_inf)
1939{
1940 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1941 fid->volatile_fid, 0, srch_inf);
1942}
1943
1944static int
1945smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1946 struct cifs_fid *fid)
1947{
1948 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1949}
1950
2e44b288
PS
1951/*
1952* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1953* the number of credits and return true. Otherwise - return false.
1954*/
1955static bool
66265f13 1956smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2e44b288 1957{
49f466bd 1958 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2e44b288 1959
31473fc4 1960 if (shdr->Status != STATUS_PENDING)
2e44b288
PS
1961 return false;
1962
66265f13 1963 if (shdr->CreditRequest) {
2e44b288 1964 spin_lock(&server->req_lock);
31473fc4 1965 server->credits += le16_to_cpu(shdr->CreditRequest);
2e44b288
PS
1966 spin_unlock(&server->req_lock);
1967 wake_up(&server->request_q);
1968 }
1969
1970 return true;
1971}
1972
511c54a2
PS
1973static bool
1974smb2_is_session_expired(char *buf)
1975{
49f466bd 1976 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
511c54a2 1977
d81243c6
MS
1978 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1979 shdr->Status != STATUS_USER_SESSION_DELETED)
511c54a2
PS
1980 return false;
1981
e68a932b
SF
1982 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
1983 le16_to_cpu(shdr->Command),
1984 le64_to_cpu(shdr->MessageId));
d81243c6 1985 cifs_dbg(FYI, "Session expired or deleted\n");
e68a932b 1986
511c54a2
PS
1987 return true;
1988}
1989
983c88a4
PS
1990static int
1991smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1992 struct cifsInodeInfo *cinode)
1993{
0822f514
PS
1994 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1995 return SMB2_lease_break(0, tcon, cinode->lease_key,
1996 smb2_get_lease_state(cinode));
1997
983c88a4
PS
1998 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1999 fid->volatile_fid,
18cceb6a 2000 CIFS_CACHE_READ(cinode) ? 1 : 0);
983c88a4
PS
2001}
2002
c5a5f38f 2003void
730928c8
RS
2004smb2_set_related(struct smb_rqst *rqst)
2005{
2006 struct smb2_sync_hdr *shdr;
2007
2008 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2009 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2010}
2011
2012char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2013
c5a5f38f 2014void
e77fe73c 2015smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
730928c8
RS
2016{
2017 struct smb2_sync_hdr *shdr;
e77fe73c
RS
2018 struct cifs_ses *ses = tcon->ses;
2019 struct TCP_Server_Info *server = ses->server;
730928c8 2020 unsigned long len = smb_rqst_len(server, rqst);
e77fe73c 2021 int i, num_padding;
730928c8
RS
2022
2023 /* SMB headers in a compound are 8 byte aligned. */
e77fe73c
RS
2024
2025 /* No padding needed */
2026 if (!(len & 7))
2027 goto finished;
2028
2029 num_padding = 8 - (len & 7);
2030 if (!smb3_encryption_required(tcon)) {
2031 /*
2032 * If we do not have encryption then we can just add an extra
2033 * iov for the padding.
2034 */
2035 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2036 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2037 rqst->rq_nvec++;
2038 len += num_padding;
2039 } else {
2040 /*
2041 * We can not add a small padding iov for the encryption case
2042 * because the encryption framework can not handle the padding
2043 * iovs.
2044 * We have to flatten this into a single buffer and add
2045 * the padding to it.
2046 */
2047 for (i = 1; i < rqst->rq_nvec; i++) {
2048 memcpy(rqst->rq_iov[0].iov_base +
2049 rqst->rq_iov[0].iov_len,
2050 rqst->rq_iov[i].iov_base,
2051 rqst->rq_iov[i].iov_len);
2052 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
271b9c0c 2053 }
e77fe73c
RS
2054 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2055 0, num_padding);
2056 rqst->rq_iov[0].iov_len += num_padding;
2057 len += num_padding;
2058 rqst->rq_nvec = 1;
730928c8
RS
2059 }
2060
e77fe73c 2061 finished:
730928c8
RS
2062 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2063 shdr->NextCommand = cpu_to_le32(len);
2064}
2065
07d3b2e4
RS
2066/*
2067 * Passes the query info response back to the caller on success.
2068 * Caller need to free this with free_rsp_buf().
2069 */
f9793b6f 2070int
07d3b2e4
RS
2071smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2072 __le16 *utf16_path, u32 desired_access,
2073 u32 class, u32 type, u32 output_len,
f9793b6f
RS
2074 struct kvec *rsp, int *buftype,
2075 struct cifs_sb_info *cifs_sb)
6fc05c25 2076{
07d3b2e4 2077 struct cifs_ses *ses = tcon->ses;
07d3b2e4 2078 int flags = 0;
730928c8
RS
2079 struct smb_rqst rqst[3];
2080 int resp_buftype[3];
2081 struct kvec rsp_iov[3];
4d8dfafc 2082 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
730928c8
RS
2083 struct kvec qi_iov[1];
2084 struct kvec close_iov[1];
6fc05c25 2085 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
2086 struct cifs_open_parms oparms;
2087 struct cifs_fid fid;
730928c8
RS
2088 int rc;
2089
2090 if (smb3_encryption_required(tcon))
2091 flags |= CIFS_TRANSFORM_REQ;
2092
2093 memset(rqst, 0, sizeof(rqst));
c5a5f38f 2094 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
730928c8
RS
2095 memset(rsp_iov, 0, sizeof(rsp_iov));
2096
2097 memset(&open_iov, 0, sizeof(open_iov));
2098 rqst[0].rq_iov = open_iov;
4d8dfafc 2099 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
064f6047
PS
2100
2101 oparms.tcon = tcon;
07d3b2e4 2102 oparms.desired_access = desired_access;
064f6047 2103 oparms.disposition = FILE_OPEN;
f9793b6f
RS
2104 if (cifs_sb && backup_cred(cifs_sb))
2105 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2106 else
2107 oparms.create_options = 0;
064f6047 2108 oparms.fid = &fid;
9cbc0b73 2109 oparms.reconnect = false;
6fc05c25 2110
07d3b2e4 2111 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
6fc05c25 2112 if (rc)
07d3b2e4 2113 goto qic_exit;
e77fe73c 2114 smb2_set_next_command(tcon, &rqst[0]);
730928c8
RS
2115
2116 memset(&qi_iov, 0, sizeof(qi_iov));
2117 rqst[1].rq_iov = qi_iov;
2118 rqst[1].rq_nvec = 1;
2119
2120 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
07d3b2e4
RS
2121 class, type, 0,
2122 output_len, 0,
f5b05d62 2123 NULL);
730928c8 2124 if (rc)
07d3b2e4 2125 goto qic_exit;
e77fe73c 2126 smb2_set_next_command(tcon, &rqst[1]);
730928c8
RS
2127 smb2_set_related(&rqst[1]);
2128
2129 memset(&close_iov, 0, sizeof(close_iov));
2130 rqst[2].rq_iov = close_iov;
2131 rqst[2].rq_nvec = 1;
2132
2133 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2134 if (rc)
07d3b2e4 2135 goto qic_exit;
730928c8
RS
2136 smb2_set_related(&rqst[2]);
2137
2138 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2139 resp_buftype, rsp_iov);
f9793b6f
RS
2140 if (rc) {
2141 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
07d3b2e4 2142 goto qic_exit;
f9793b6f 2143 }
07d3b2e4
RS
2144 *rsp = rsp_iov[1];
2145 *buftype = resp_buftype[1];
2146
2147 qic_exit:
2148 SMB2_open_free(&rqst[0]);
2149 SMB2_query_info_free(&rqst[1]);
2150 SMB2_close_free(&rqst[2]);
2151 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2152 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2153 return rc;
2154}
2155
2156static int
2157smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2158 struct kstatfs *buf)
2159{
2160 struct smb2_query_info_rsp *rsp;
2161 struct smb2_fs_full_size_info *info = NULL;
2162 __le16 utf16_path = 0; /* Null - open root of share */
2163 struct kvec rsp_iov = {NULL, 0};
2164 int buftype = CIFS_NO_BUFFER;
2165 int rc;
2166
2167
2168 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2169 FILE_READ_ATTRIBUTES,
2170 FS_FULL_SIZE_INFORMATION,
2171 SMB2_O_INFO_FILESYSTEM,
2172 sizeof(struct smb2_fs_full_size_info),
f9793b6f 2173 &rsp_iov, &buftype, NULL);
730928c8
RS
2174 if (rc)
2175 goto qfs_exit;
2176
07d3b2e4 2177 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
6fc05c25 2178 buf->f_type = SMB2_MAGIC_NUMBER;
730928c8
RS
2179 info = (struct smb2_fs_full_size_info *)(
2180 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2181 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2182 le32_to_cpu(rsp->OutputBufferLength),
07d3b2e4 2183 &rsp_iov,
730928c8
RS
2184 sizeof(struct smb2_fs_full_size_info));
2185 if (!rc)
2186 smb2_copy_fs_info_to_kstatfs(info, buf);
2187
2188qfs_exit:
07d3b2e4 2189 free_rsp_buf(buftype, rsp_iov.iov_base);
6fc05c25
PS
2190 return rc;
2191}
2192
2d304217
SF
2193static int
2194smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2195 struct kstatfs *buf)
2196{
2197 int rc;
2198 __le16 srch_path = 0; /* Null - open root of share */
2199 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2200 struct cifs_open_parms oparms;
2201 struct cifs_fid fid;
2202
2203 if (!tcon->posix_extensions)
2204 return smb2_queryfs(xid, tcon, buf);
2205
2206 oparms.tcon = tcon;
2207 oparms.desired_access = FILE_READ_ATTRIBUTES;
2208 oparms.disposition = FILE_OPEN;
2209 oparms.create_options = 0;
2210 oparms.fid = &fid;
2211 oparms.reconnect = false;
2212
2213 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2214 if (rc)
2215 return rc;
2216
2217 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2218 fid.volatile_fid, buf);
2219 buf->f_type = SMB2_MAGIC_NUMBER;
2220 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2221 return rc;
2222}
2d304217 2223
027e8eec
PS
2224static bool
2225smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2226{
2227 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2228 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2229}
2230
f7ba7fe6
PS
2231static int
2232smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2233 __u64 length, __u32 type, int lock, int unlock, bool wait)
2234{
2235 if (unlock && !lock)
2236 type = SMB2_LOCKFLAG_UNLOCK;
2237 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2238 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2239 current->tgid, length, offset, type, wait);
2240}
2241
b8c32dbb
PS
2242static void
2243smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2244{
2245 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2246}
2247
2248static void
2249smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2250{
2251 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2252}
2253
2254static void
2255smb2_new_lease_key(struct cifs_fid *fid)
2256{
fa70b87c 2257 generate_random_uuid(fid->lease_key);
b8c32dbb
PS
2258}
2259
9d49640a
AA
2260static int
2261smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2262 const char *search_name,
2263 struct dfs_info3_param **target_nodes,
2264 unsigned int *num_of_nodes,
2265 const struct nls_table *nls_codepage, int remap)
2266{
2267 int rc;
2268 __le16 *utf16_path = NULL;
2269 int utf16_path_len = 0;
2270 struct cifs_tcon *tcon;
2271 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2272 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2273 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2274
2275 cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
2276
2277 /*
63a83b86 2278 * Try to use the IPC tcon, otherwise just use any
9d49640a 2279 */
63a83b86
AA
2280 tcon = ses->tcon_ipc;
2281 if (tcon == NULL) {
2282 spin_lock(&cifs_tcp_ses_lock);
2283 tcon = list_first_entry_or_null(&ses->tcon_list,
2284 struct cifs_tcon,
2285 tcon_list);
2286 if (tcon)
2287 tcon->tc_count++;
2288 spin_unlock(&cifs_tcp_ses_lock);
2289 }
2290
2291 if (tcon == NULL) {
9d49640a
AA
2292 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2293 ses);
2294 rc = -ENOTCONN;
2295 goto out;
2296 }
2297
2298 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2299 &utf16_path_len,
2300 nls_codepage, remap);
2301 if (!utf16_path) {
2302 rc = -ENOMEM;
2303 goto out;
2304 }
2305
2306 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2307 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2308 if (!dfs_req) {
2309 rc = -ENOMEM;
2310 goto out;
2311 }
2312
2313 /* Highest DFS referral version understood */
2314 dfs_req->MaxReferralLevel = DFS_VERSION;
2315
2316 /* Path to resolve in an UTF-16 null-terminated string */
2317 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2318
2319 do {
9d49640a
AA
2320 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2321 FSCTL_DFS_GET_REFERRALS,
63a83b86 2322 true /* is_fsctl */,
153322f7 2323 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
9d49640a 2324 (char **)&dfs_rsp, &dfs_rsp_size);
9d49640a
AA
2325 } while (rc == -EAGAIN);
2326
2327 if (rc) {
2564f2ff 2328 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
5702591f 2329 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
9d49640a
AA
2330 goto out;
2331 }
2332
2333 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2334 num_of_nodes, target_nodes,
2335 nls_codepage, remap, search_name,
2336 true /* is_unicode */);
2337 if (rc) {
2338 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
2339 goto out;
2340 }
2341
2342 out:
63a83b86
AA
2343 if (tcon && !tcon->ipc) {
2344 /* ipc tcons are not refcounted */
9d49640a
AA
2345 spin_lock(&cifs_tcp_ses_lock);
2346 tcon->tc_count--;
2347 spin_unlock(&cifs_tcp_ses_lock);
2348 }
2349 kfree(utf16_path);
2350 kfree(dfs_req);
2351 kfree(dfs_rsp);
2352 return rc;
2353}
7893242e
PS
2354#define SMB2_SYMLINK_STRUCT_SIZE \
2355 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2356
b42bf888
PS
2357static int
2358smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2359 const char *full_path, char **target_path,
2360 struct cifs_sb_info *cifs_sb)
2361{
2362 int rc;
2363 __le16 *utf16_path;
2364 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2365 struct cifs_open_parms oparms;
2366 struct cifs_fid fid;
91cb74f5 2367 struct kvec err_iov = {NULL, 0};
9d874c36
RS
2368 struct smb2_err_rsp *err_buf = NULL;
2369 int resp_buftype;
b42bf888 2370 struct smb2_symlink_err_rsp *symlink;
7893242e
PS
2371 unsigned int sub_len;
2372 unsigned int sub_offset;
2373 unsigned int print_len;
2374 unsigned int print_offset;
b42bf888
PS
2375
2376 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2377
2378 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2379 if (!utf16_path)
2380 return -ENOMEM;
2381
2382 oparms.tcon = tcon;
2383 oparms.desired_access = FILE_READ_ATTRIBUTES;
2384 oparms.disposition = FILE_OPEN;
5e19697b
SF
2385 if (backup_cred(cifs_sb))
2386 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2387 else
2388 oparms.create_options = 0;
b42bf888
PS
2389 oparms.fid = &fid;
2390 oparms.reconnect = false;
2391
9d874c36
RS
2392 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_iov,
2393 &resp_buftype);
0d568cd3 2394 if (!rc || !err_iov.iov_base) {
9d874c36 2395 rc = -ENOENT;
ff361fda 2396 goto free_path;
b42bf888 2397 }
7893242e 2398
91cb74f5 2399 err_buf = err_iov.iov_base;
7893242e 2400 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1fc6ad2f 2401 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
9d874c36
RS
2402 rc = -ENOENT;
2403 goto querty_exit;
7893242e
PS
2404 }
2405
b42bf888
PS
2406 /* open must fail on symlink - reset rc */
2407 rc = 0;
2408 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2409 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2410 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
7893242e
PS
2411 print_len = le16_to_cpu(symlink->PrintNameLength);
2412 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2413
1fc6ad2f 2414 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
9d874c36
RS
2415 rc = -ENOENT;
2416 goto querty_exit;
7893242e
PS
2417 }
2418
1fc6ad2f
RS
2419 if (err_iov.iov_len <
2420 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
9d874c36
RS
2421 rc = -ENOENT;
2422 goto querty_exit;
7893242e
PS
2423 }
2424
b42bf888
PS
2425 *target_path = cifs_strndup_from_utf16(
2426 (char *)symlink->PathBuffer + sub_offset,
2427 sub_len, true, cifs_sb->local_nls);
2428 if (!(*target_path)) {
9d874c36
RS
2429 rc = -ENOMEM;
2430 goto querty_exit;
b42bf888
PS
2431 }
2432 convert_delimiter(*target_path, '/');
2433 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
9d874c36
RS
2434
2435 querty_exit:
2436 free_rsp_buf(resp_buftype, err_buf);
ff361fda 2437 free_path:
b42bf888
PS
2438 kfree(utf16_path);
2439 return rc;
2440}
2441
84908426 2442#ifdef CONFIG_CIFS_ACL
2f1afe25
SP
2443static struct cifs_ntsd *
2444get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2445 const struct cifs_fid *cifsfid, u32 *pacllen)
2446{
2447 struct cifs_ntsd *pntsd = NULL;
2448 unsigned int xid;
2449 int rc = -EOPNOTSUPP;
2450 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2451
2452 if (IS_ERR(tlink))
2453 return ERR_CAST(tlink);
2454
2455 xid = get_xid();
2456 cifs_dbg(FYI, "trying to get acl\n");
2457
2458 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2459 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2460 free_xid(xid);
2461
2462 cifs_put_tlink(tlink);
2463
2464 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2465 if (rc)
2466 return ERR_PTR(rc);
2467 return pntsd;
2468
2469}
2470
2471static struct cifs_ntsd *
2472get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2473 const char *path, u32 *pacllen)
2474{
2475 struct cifs_ntsd *pntsd = NULL;
2476 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2477 unsigned int xid;
2478 int rc;
2479 struct cifs_tcon *tcon;
2480 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2481 struct cifs_fid fid;
2482 struct cifs_open_parms oparms;
2483 __le16 *utf16_path;
2484
2485 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2486 if (IS_ERR(tlink))
2487 return ERR_CAST(tlink);
2488
2489 tcon = tlink_tcon(tlink);
2490 xid = get_xid();
2491
2492 if (backup_cred(cifs_sb))
709340a0 2493 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2f1afe25
SP
2494 else
2495 oparms.create_options = 0;
2496
2497 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
cfe89091
SF
2498 if (!utf16_path) {
2499 rc = -ENOMEM;
2500 free_xid(xid);
2501 return ERR_PTR(rc);
2502 }
2f1afe25
SP
2503
2504 oparms.tcon = tcon;
2505 oparms.desired_access = READ_CONTROL;
2506 oparms.disposition = FILE_OPEN;
2507 oparms.fid = &fid;
2508 oparms.reconnect = false;
2509
9d874c36 2510 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2f1afe25
SP
2511 kfree(utf16_path);
2512 if (!rc) {
2513 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2514 fid.volatile_fid, (void **)&pntsd, pacllen);
2515 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2516 }
2517
2518 cifs_put_tlink(tlink);
2519 free_xid(xid);
2520
2521 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2522 if (rc)
2523 return ERR_PTR(rc);
2524 return pntsd;
2525}
2526
366ed846
SP
2527#ifdef CONFIG_CIFS_ACL
2528static int
2529set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2530 struct inode *inode, const char *path, int aclflag)
2531{
2532 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2533 unsigned int xid;
2534 int rc, access_flags = 0;
2535 struct cifs_tcon *tcon;
2536 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2537 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2538 struct cifs_fid fid;
2539 struct cifs_open_parms oparms;
2540 __le16 *utf16_path;
2541
2542 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2543 if (IS_ERR(tlink))
2544 return PTR_ERR(tlink);
2545
2546 tcon = tlink_tcon(tlink);
2547 xid = get_xid();
2548
2549 if (backup_cred(cifs_sb))
2550 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2551 else
2552 oparms.create_options = 0;
2553
2554 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2555 access_flags = WRITE_OWNER;
2556 else
2557 access_flags = WRITE_DAC;
2558
2559 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
cfe89091
SF
2560 if (!utf16_path) {
2561 rc = -ENOMEM;
2562 free_xid(xid);
2563 return rc;
2564 }
366ed846
SP
2565
2566 oparms.tcon = tcon;
2567 oparms.desired_access = access_flags;
2568 oparms.disposition = FILE_OPEN;
2569 oparms.path = path;
2570 oparms.fid = &fid;
2571 oparms.reconnect = false;
2572
9d874c36 2573 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
366ed846
SP
2574 kfree(utf16_path);
2575 if (!rc) {
2576 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2577 fid.volatile_fid, pnntsd, acllen, aclflag);
2578 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2579 }
2580
2581 cifs_put_tlink(tlink);
2582 free_xid(xid);
2583 return rc;
2584}
2585#endif /* CIFS_ACL */
2586
2f1afe25
SP
2587/* Retrieve an ACL from the server */
2588static struct cifs_ntsd *
2589get_smb2_acl(struct cifs_sb_info *cifs_sb,
2590 struct inode *inode, const char *path,
2591 u32 *pacllen)
2592{
2593 struct cifs_ntsd *pntsd = NULL;
2594 struct cifsFileInfo *open_file = NULL;
2595
2596 if (inode)
2597 open_file = find_readable_file(CIFS_I(inode), true);
2598 if (!open_file)
2599 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2600
2601 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2602 cifsFileInfo_put(open_file);
2603 return pntsd;
2604}
84908426 2605#endif
2f1afe25 2606
30175628
SF
2607static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2608 loff_t offset, loff_t len, bool keep_size)
2609{
72c419d9 2610 struct cifs_ses *ses = tcon->ses;
30175628
SF
2611 struct inode *inode;
2612 struct cifsInodeInfo *cifsi;
2613 struct cifsFileInfo *cfile = file->private_data;
2614 struct file_zero_data_information fsctl_buf;
72c419d9
RS
2615 struct smb_rqst rqst[2];
2616 int resp_buftype[2];
2617 struct kvec rsp_iov[2];
2618 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2619 struct kvec si_iov[1];
2620 unsigned int size[1];
2621 void *data[1];
30175628
SF
2622 long rc;
2623 unsigned int xid;
72c419d9
RS
2624 int num = 0, flags = 0;
2625 __le64 eof;
30175628
SF
2626
2627 xid = get_xid();
2628
2b0143b5 2629 inode = d_inode(cfile->dentry);
30175628
SF
2630 cifsi = CIFS_I(inode);
2631
779ede04
SF
2632 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2633 ses->Suid, offset, len);
2634
2635
30175628
SF
2636 /* if file not oplocked can't be sure whether asking to extend size */
2637 if (!CIFS_CACHE_READ(cifsi))
cfe89091
SF
2638 if (keep_size == false) {
2639 rc = -EOPNOTSUPP;
779ede04
SF
2640 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
2641 tcon->tid, ses->Suid, offset, len, rc);
cfe89091
SF
2642 free_xid(xid);
2643 return rc;
2644 }
30175628 2645
2bb93d24 2646 /*
30175628
SF
2647 * Must check if file sparse since fallocate -z (zero range) assumes
2648 * non-sparse allocation
2649 */
cfe89091
SF
2650 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
2651 rc = -EOPNOTSUPP;
779ede04
SF
2652 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
2653 ses->Suid, offset, len, rc);
cfe89091
SF
2654 free_xid(xid);
2655 return rc;
2656 }
30175628 2657
30175628
SF
2658 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2659
2660 fsctl_buf.FileOffset = cpu_to_le64(offset);
2661 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2662
72c419d9
RS
2663 if (smb3_encryption_required(tcon))
2664 flags |= CIFS_TRANSFORM_REQ;
2665
2666 memset(rqst, 0, sizeof(rqst));
2667 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2668 memset(rsp_iov, 0, sizeof(rsp_iov));
2669
2670
2671 memset(&io_iov, 0, sizeof(io_iov));
2672 rqst[num].rq_iov = io_iov;
2673 rqst[num].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2674 rc = SMB2_ioctl_init(tcon, &rqst[num++], cfile->fid.persistent_fid,
2675 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2676 true /* is_fctl */, (char *)&fsctl_buf,
153322f7
SF
2677 sizeof(struct file_zero_data_information),
2678 CIFSMaxBufSize);
72c419d9
RS
2679 if (rc)
2680 goto zero_range_exit;
2681
2682 /*
2683 * do we also need to change the size of the file?
2684 */
2685 if (keep_size == false && i_size_read(inode) < offset + len) {
2686 smb2_set_next_command(tcon, &rqst[0]);
2687
2688 memset(&si_iov, 0, sizeof(si_iov));
2689 rqst[num].rq_iov = si_iov;
2690 rqst[num].rq_nvec = 1;
2691
2692 eof = cpu_to_le64(offset + len);
2693 size[0] = 8; /* sizeof __le64 */
2694 data[0] = &eof;
2695
2696 rc = SMB2_set_info_init(tcon, &rqst[num++],
2697 cfile->fid.persistent_fid,
2698 cfile->fid.persistent_fid,
2699 current->tgid,
2700 FILE_END_OF_FILE_INFORMATION,
2701 SMB2_O_INFO_FILE, 0, data, size);
2702 smb2_set_related(&rqst[1]);
2703 }
2704
2705 rc = compound_send_recv(xid, ses, flags, num, rqst,
2706 resp_buftype, rsp_iov);
2707
2708 zero_range_exit:
2709 SMB2_ioctl_free(&rqst[0]);
2710 SMB2_set_info_free(&rqst[1]);
2711 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2712 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
30175628 2713 free_xid(xid);
779ede04
SF
2714 if (rc)
2715 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
2716 ses->Suid, offset, len, rc);
2717 else
2718 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
2719 ses->Suid, offset, len);
30175628
SF
2720 return rc;
2721}
2722
31742c5a
SF
2723static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2724 loff_t offset, loff_t len)
2725{
2726 struct inode *inode;
2727 struct cifsInodeInfo *cifsi;
2728 struct cifsFileInfo *cfile = file->private_data;
2729 struct file_zero_data_information fsctl_buf;
2730 long rc;
2731 unsigned int xid;
2732 __u8 set_sparse = 1;
2733
2734 xid = get_xid();
2735
2b0143b5 2736 inode = d_inode(cfile->dentry);
31742c5a
SF
2737 cifsi = CIFS_I(inode);
2738
2739 /* Need to make file sparse, if not already, before freeing range. */
2740 /* Consider adding equivalent for compressed since it could also work */
cfe89091
SF
2741 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2742 rc = -EOPNOTSUPP;
2743 free_xid(xid);
2744 return rc;
2745 }
31742c5a
SF
2746
2747 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2748
2749 fsctl_buf.FileOffset = cpu_to_le64(offset);
2750 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2751
2752 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2753 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
63a83b86 2754 true /* is_fctl */, (char *)&fsctl_buf,
153322f7
SF
2755 sizeof(struct file_zero_data_information),
2756 CIFSMaxBufSize, NULL, NULL);
31742c5a
SF
2757 free_xid(xid);
2758 return rc;
2759}
2760
9ccf3216
SF
2761static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2762 loff_t off, loff_t len, bool keep_size)
2763{
2764 struct inode *inode;
2765 struct cifsInodeInfo *cifsi;
2766 struct cifsFileInfo *cfile = file->private_data;
2767 long rc = -EOPNOTSUPP;
2768 unsigned int xid;
f1699479 2769 __le64 eof;
9ccf3216
SF
2770
2771 xid = get_xid();
2772
2b0143b5 2773 inode = d_inode(cfile->dentry);
9ccf3216
SF
2774 cifsi = CIFS_I(inode);
2775
779ede04
SF
2776 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2777 tcon->ses->Suid, off, len);
9ccf3216
SF
2778 /* if file not oplocked can't be sure whether asking to extend size */
2779 if (!CIFS_CACHE_READ(cifsi))
cfe89091 2780 if (keep_size == false) {
779ede04
SF
2781 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2782 tcon->tid, tcon->ses->Suid, off, len, rc);
cfe89091
SF
2783 free_xid(xid);
2784 return rc;
2785 }
9ccf3216
SF
2786
2787 /*
2788 * Files are non-sparse by default so falloc may be a no-op
2789 * Must check if file sparse. If not sparse, and not extending
2790 * then no need to do anything since file already allocated
2791 */
2792 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2793 if (keep_size == true)
cfe89091 2794 rc = 0;
9ccf3216
SF
2795 /* check if extending file */
2796 else if (i_size_read(inode) >= off + len)
2797 /* not extending file and already not sparse */
cfe89091 2798 rc = 0;
9ccf3216
SF
2799 /* BB: in future add else clause to extend file */
2800 else
cfe89091 2801 rc = -EOPNOTSUPP;
779ede04
SF
2802 if (rc)
2803 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2804 tcon->tid, tcon->ses->Suid, off, len, rc);
2805 else
2806 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid,
2807 tcon->tid, tcon->ses->Suid, off, len);
cfe89091
SF
2808 free_xid(xid);
2809 return rc;
9ccf3216
SF
2810 }
2811
2812 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2813 /*
2814 * Check if falloc starts within first few pages of file
2815 * and ends within a few pages of the end of file to
2816 * ensure that most of file is being forced to be
2817 * fallocated now. If so then setting whole file sparse
2818 * ie potentially making a few extra pages at the beginning
2819 * or end of the file non-sparse via set_sparse is harmless.
2820 */
cfe89091
SF
2821 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2822 rc = -EOPNOTSUPP;
779ede04
SF
2823 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2824 tcon->tid, tcon->ses->Suid, off, len, rc);
cfe89091
SF
2825 free_xid(xid);
2826 return rc;
2827 }
9ccf3216 2828
f1699479
RS
2829 smb2_set_sparse(xid, tcon, cfile, inode, false);
2830 rc = 0;
2831 } else {
2832 smb2_set_sparse(xid, tcon, cfile, inode, false);
2833 rc = 0;
2834 if (i_size_read(inode) < off + len) {
2835 eof = cpu_to_le64(off + len);
2836 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2837 cfile->fid.volatile_fid, cfile->pid,
2838 &eof);
2839 }
9ccf3216 2840 }
9ccf3216 2841
779ede04
SF
2842 if (rc)
2843 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
2844 tcon->ses->Suid, off, len, rc);
2845 else
2846 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
2847 tcon->ses->Suid, off, len);
9ccf3216
SF
2848
2849 free_xid(xid);
2850 return rc;
2851}
2852
2853
31742c5a
SF
2854static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
2855 loff_t off, loff_t len)
2856{
2857 /* KEEP_SIZE already checked for by do_fallocate */
2858 if (mode & FALLOC_FL_PUNCH_HOLE)
2859 return smb3_punch_hole(file, tcon, off, len);
30175628
SF
2860 else if (mode & FALLOC_FL_ZERO_RANGE) {
2861 if (mode & FALLOC_FL_KEEP_SIZE)
2862 return smb3_zero_range(file, tcon, off, len, true);
2863 return smb3_zero_range(file, tcon, off, len, false);
9ccf3216
SF
2864 } else if (mode == FALLOC_FL_KEEP_SIZE)
2865 return smb3_simple_falloc(file, tcon, off, len, true);
2866 else if (mode == 0)
2867 return smb3_simple_falloc(file, tcon, off, len, false);
31742c5a
SF
2868
2869 return -EOPNOTSUPP;
2870}
2871
c11f1df5
SP
2872static void
2873smb2_downgrade_oplock(struct TCP_Server_Info *server,
2874 struct cifsInodeInfo *cinode, bool set_level2)
2875{
2876 if (set_level2)
2877 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
2878 0, NULL);
2879 else
2880 server->ops->set_oplock_level(cinode, 0, 0, NULL);
2881}
2882
7b9b9edb
PS
2883static void
2884smb21_downgrade_oplock(struct TCP_Server_Info *server,
2885 struct cifsInodeInfo *cinode, bool set_level2)
2886{
2887 server->ops->set_oplock_level(cinode,
2888 set_level2 ? SMB2_LEASE_READ_CACHING_HE :
2889 0, 0, NULL);
2890}
2891
53ef1016 2892static void
42873b0a
PS
2893smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2894 unsigned int epoch, bool *purge_cache)
53ef1016
PS
2895{
2896 oplock &= 0xFF;
2897 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2898 return;
2899 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
42873b0a 2900 cinode->oplock = CIFS_CACHE_RHW_FLG;
53ef1016
PS
2901 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
2902 &cinode->vfs_inode);
2903 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
42873b0a 2904 cinode->oplock = CIFS_CACHE_RW_FLG;
53ef1016
PS
2905 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
2906 &cinode->vfs_inode);
2907 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
2908 cinode->oplock = CIFS_CACHE_READ_FLG;
2909 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
2910 &cinode->vfs_inode);
2911 } else
2912 cinode->oplock = 0;
2913}
2914
2915static void
42873b0a
PS
2916smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2917 unsigned int epoch, bool *purge_cache)
53ef1016
PS
2918{
2919 char message[5] = {0};
2920
2921 oplock &= 0xFF;
2922 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2923 return;
2924
2925 cinode->oplock = 0;
2926 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
2927 cinode->oplock |= CIFS_CACHE_READ_FLG;
2928 strcat(message, "R");
2929 }
2930 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
2931 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
2932 strcat(message, "H");
2933 }
2934 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2935 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
2936 strcat(message, "W");
2937 }
2938 if (!cinode->oplock)
2939 strcat(message, "None");
2940 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2941 &cinode->vfs_inode);
2942}
2943
42873b0a
PS
2944static void
2945smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2946 unsigned int epoch, bool *purge_cache)
2947{
2948 unsigned int old_oplock = cinode->oplock;
2949
2950 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2951
2952 if (purge_cache) {
2953 *purge_cache = false;
2954 if (old_oplock == CIFS_CACHE_READ_FLG) {
2955 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2956 (epoch - cinode->epoch > 0))
2957 *purge_cache = true;
2958 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2959 (epoch - cinode->epoch > 1))
2960 *purge_cache = true;
2961 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2962 (epoch - cinode->epoch > 1))
2963 *purge_cache = true;
2964 else if (cinode->oplock == 0 &&
2965 (epoch - cinode->epoch > 0))
2966 *purge_cache = true;
2967 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2968 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2969 (epoch - cinode->epoch > 0))
2970 *purge_cache = true;
2971 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2972 (epoch - cinode->epoch > 1))
2973 *purge_cache = true;
2974 }
2975 cinode->epoch = epoch;
2976 }
2977}
2978
53ef1016
PS
2979static bool
2980smb2_is_read_op(__u32 oplock)
2981{
2982 return oplock == SMB2_OPLOCK_LEVEL_II;
2983}
2984
2985static bool
2986smb21_is_read_op(__u32 oplock)
2987{
2988 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2989 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2990}
2991
f047390a
PS
2992static __le32
2993map_oplock_to_lease(u8 oplock)
2994{
2995 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2996 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2997 else if (oplock == SMB2_OPLOCK_LEVEL_II)
2998 return SMB2_LEASE_READ_CACHING;
2999 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3000 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3001 SMB2_LEASE_WRITE_CACHING;
3002 return 0;
3003}
3004
a41a28bd
PS
3005static char *
3006smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3007{
3008 struct create_lease *buf;
3009
3010 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3011 if (!buf)
3012 return NULL;
3013
729c0c9d 3014 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
f047390a 3015 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
a41a28bd
PS
3016
3017 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3018 (struct create_lease, lcontext));
3019 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3020 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3021 (struct create_lease, Name));
3022 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 3023 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
a41a28bd
PS
3024 buf->Name[0] = 'R';
3025 buf->Name[1] = 'q';
3026 buf->Name[2] = 'L';
3027 buf->Name[3] = 's';
3028 return (char *)buf;
3029}
3030
f047390a
PS
3031static char *
3032smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3033{
3034 struct create_lease_v2 *buf;
3035
3036 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3037 if (!buf)
3038 return NULL;
3039
729c0c9d 3040 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
f047390a
PS
3041 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3042
3043 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3044 (struct create_lease_v2, lcontext));
3045 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3046 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3047 (struct create_lease_v2, Name));
3048 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 3049 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
f047390a
PS
3050 buf->Name[0] = 'R';
3051 buf->Name[1] = 'q';
3052 buf->Name[2] = 'L';
3053 buf->Name[3] = 's';
3054 return (char *)buf;
3055}
3056
b5c7cde3 3057static __u8
96164ab2 3058smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
b5c7cde3
PS
3059{
3060 struct create_lease *lc = (struct create_lease *)buf;
3061
42873b0a 3062 *epoch = 0; /* not used */
b5c7cde3
PS
3063 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3064 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3065 return le32_to_cpu(lc->lcontext.LeaseState);
3066}
3067
f047390a 3068static __u8
96164ab2 3069smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
f047390a
PS
3070{
3071 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3072
42873b0a 3073 *epoch = le16_to_cpu(lc->lcontext.Epoch);
f047390a
PS
3074 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3075 return SMB2_OPLOCK_LEVEL_NOCHANGE;
96164ab2 3076 if (lease_key)
729c0c9d 3077 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
f047390a
PS
3078 return le32_to_cpu(lc->lcontext.LeaseState);
3079}
3080
7f6c5008
PS
3081static unsigned int
3082smb2_wp_retry_size(struct inode *inode)
3083{
3084 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3085 SMB2_MAX_BUFFER_SIZE);
3086}
3087
52755808
PS
3088static bool
3089smb2_dir_needs_close(struct cifsFileInfo *cfile)
3090{
3091 return !cfile->invalidHandle;
3092}
3093
026e93dc 3094static void
977b6170
RS
3095fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3096 struct smb_rqst *old_rq)
026e93dc
PS
3097{
3098 struct smb2_sync_hdr *shdr =
c713c877 3099 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
026e93dc
PS
3100
3101 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3102 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3103 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3104 tr_hdr->Flags = cpu_to_le16(0x01);
3105 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3106 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
026e93dc
PS
3107}
3108
262916bc
RS
3109/* We can not use the normal sg_set_buf() as we will sometimes pass a
3110 * stack object as buf.
3111 */
3112static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3113 unsigned int buflen)
3114{
3115 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
3116}
3117
b2c96de7
RS
3118/* Assumes the first rqst has a transform header as the first iov.
3119 * I.e.
3120 * rqst[0].rq_iov[0] is transform header
3121 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3122 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
977b6170 3123 */
026e93dc 3124static struct scatterlist *
b2c96de7 3125init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
026e93dc 3126{
b2c96de7 3127 unsigned int sg_len;
026e93dc
PS
3128 struct scatterlist *sg;
3129 unsigned int i;
3130 unsigned int j;
b2c96de7
RS
3131 unsigned int idx = 0;
3132 int skip;
3133
3134 sg_len = 1;
3135 for (i = 0; i < num_rqst; i++)
3136 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
026e93dc
PS
3137
3138 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3139 if (!sg)
3140 return NULL;
3141
3142 sg_init_table(sg, sg_len);
b2c96de7
RS
3143 for (i = 0; i < num_rqst; i++) {
3144 for (j = 0; j < rqst[i].rq_nvec; j++) {
3145 /*
3146 * The first rqst has a transform header where the
3147 * first 20 bytes are not part of the encrypted blob
3148 */
3149 skip = (i == 0) && (j == 0) ? 20 : 0;
3150 smb2_sg_set_buf(&sg[idx++],
3151 rqst[i].rq_iov[j].iov_base + skip,
3152 rqst[i].rq_iov[j].iov_len - skip);
e77fe73c 3153 }
b2c96de7
RS
3154
3155 for (j = 0; j < rqst[i].rq_npages; j++) {
3156 unsigned int len, offset;
d5f07fb3 3157
b2c96de7
RS
3158 rqst_page_get_length(&rqst[i], j, &len, &offset);
3159 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3160 }
026e93dc 3161 }
b2c96de7 3162 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
026e93dc
PS
3163 return sg;
3164}
3165
61cfac6f
PS
3166static int
3167smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3168{
3169 struct cifs_ses *ses;
3170 u8 *ses_enc_key;
3171
3172 spin_lock(&cifs_tcp_ses_lock);
3173 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3174 if (ses->Suid != ses_id)
3175 continue;
3176 ses_enc_key = enc ? ses->smb3encryptionkey :
3177 ses->smb3decryptionkey;
3178 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3179 spin_unlock(&cifs_tcp_ses_lock);
3180 return 0;
3181 }
3182 spin_unlock(&cifs_tcp_ses_lock);
3183
3184 return 1;
3185}
026e93dc 3186/*
c713c877
RS
3187 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3188 * iov[0] - transform header (associate data),
3189 * iov[1-N] - SMB2 header and pages - data to encrypt.
3190 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
026e93dc
PS
3191 * untouched.
3192 */
3193static int
b2c96de7
RS
3194crypt_message(struct TCP_Server_Info *server, int num_rqst,
3195 struct smb_rqst *rqst, int enc)
026e93dc
PS
3196{
3197 struct smb2_transform_hdr *tr_hdr =
b2c96de7 3198 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
1fc6ad2f 3199 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
026e93dc
PS
3200 int rc = 0;
3201 struct scatterlist *sg;
3202 u8 sign[SMB2_SIGNATURE_SIZE] = {};
61cfac6f 3203 u8 key[SMB3_SIGN_KEY_SIZE];
026e93dc
PS
3204 struct aead_request *req;
3205 char *iv;
3206 unsigned int iv_len;
a5186b85 3207 DECLARE_CRYPTO_WAIT(wait);
026e93dc
PS
3208 struct crypto_aead *tfm;
3209 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3210
61cfac6f
PS
3211 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3212 if (rc) {
3213 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3214 enc ? "en" : "de");
026e93dc
PS
3215 return 0;
3216 }
3217
3218 rc = smb3_crypto_aead_allocate(server);
3219 if (rc) {
3220 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3221 return rc;
3222 }
3223
3224 tfm = enc ? server->secmech.ccmaesencrypt :
3225 server->secmech.ccmaesdecrypt;
61cfac6f 3226 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
026e93dc
PS
3227 if (rc) {
3228 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3229 return rc;
3230 }
3231
3232 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3233 if (rc) {
3234 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3235 return rc;
3236 }
3237
3238 req = aead_request_alloc(tfm, GFP_KERNEL);
3239 if (!req) {
3240 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
3241 return -ENOMEM;
3242 }
3243
3244 if (!enc) {
3245 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3246 crypt_len += SMB2_SIGNATURE_SIZE;
3247 }
3248
b2c96de7 3249 sg = init_sg(num_rqst, rqst, sign);
026e93dc 3250 if (!sg) {
517a6e43
CJ
3251 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
3252 rc = -ENOMEM;
026e93dc
PS
3253 goto free_req;
3254 }
3255
3256 iv_len = crypto_aead_ivsize(tfm);
3257 iv = kzalloc(iv_len, GFP_KERNEL);
3258 if (!iv) {
3259 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
517a6e43 3260 rc = -ENOMEM;
026e93dc
PS
3261 goto free_sg;
3262 }
3263 iv[0] = 3;
3264 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3265
3266 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3267 aead_request_set_ad(req, assoc_data_len);
3268
3269 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
a5186b85 3270 crypto_req_done, &wait);
026e93dc 3271
a5186b85
GBY
3272 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3273 : crypto_aead_decrypt(req), &wait);
026e93dc
PS
3274
3275 if (!rc && enc)
3276 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3277
3278 kfree(iv);
3279free_sg:
3280 kfree(sg);
3281free_req:
3282 kfree(req);
3283 return rc;
3284}
3285
b2c96de7
RS
3286void
3287smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3288{
3289 int i, j;
3290
3291 for (i = 0; i < num_rqst; i++) {
3292 if (rqst[i].rq_pages) {
3293 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3294 put_page(rqst[i].rq_pages[j]);
3295 kfree(rqst[i].rq_pages);
3296 }
3297 }
3298}
3299
3300/*
3301 * This function will initialize new_rq and encrypt the content.
3302 * The first entry, new_rq[0], only contains a single iov which contains
3303 * a smb2_transform_hdr and is pre-allocated by the caller.
3304 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3305 *
3306 * The end result is an array of smb_rqst structures where the first structure
3307 * only contains a single iov for the transform header which we then can pass
3308 * to crypt_message().
3309 *
3310 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3311 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3312 */
026e93dc 3313static int
b2c96de7
RS
3314smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3315 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
026e93dc 3316{
026e93dc 3317 struct page **pages;
b2c96de7
RS
3318 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3319 unsigned int npages;
3320 unsigned int orig_len = 0;
3321 int i, j;
026e93dc
PS
3322 int rc = -ENOMEM;
3323
b2c96de7
RS
3324 for (i = 1; i < num_rqst; i++) {
3325 npages = old_rq[i - 1].rq_npages;
3326 pages = kmalloc_array(npages, sizeof(struct page *),
3327 GFP_KERNEL);
3328 if (!pages)
3329 goto err_free;
3330
3331 new_rq[i].rq_pages = pages;
3332 new_rq[i].rq_npages = npages;
3333 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3334 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3335 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3336 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3337 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3338
3339 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3340
3341 for (j = 0; j < npages; j++) {
3342 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3343 if (!pages[j])
3344 goto err_free;
3345 }
026e93dc 3346
b2c96de7
RS
3347 /* copy pages form the old */
3348 for (j = 0; j < npages; j++) {
3349 char *dst, *src;
3350 unsigned int offset, len;
977b6170 3351
b2c96de7 3352 rqst_page_get_length(&new_rq[i], j, &len, &offset);
026e93dc 3353
b2c96de7
RS
3354 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3355 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
026e93dc 3356
b2c96de7
RS
3357 memcpy(dst, src, len);
3358 kunmap(new_rq[i].rq_pages[j]);
3359 kunmap(old_rq[i - 1].rq_pages[j]);
3360 }
3361 }
35e2cc1b 3362
b2c96de7 3363 /* fill the 1st iov with a transform header */
977b6170 3364 fill_transform_hdr(tr_hdr, orig_len, old_rq);
d5f07fb3 3365
b2c96de7 3366 rc = crypt_message(server, num_rqst, new_rq, 1);
026e93dc
PS
3367 cifs_dbg(FYI, "encrypt message returned %d", rc);
3368 if (rc)
b2c96de7 3369 goto err_free;
026e93dc
PS
3370
3371 return rc;
3372
b2c96de7
RS
3373err_free:
3374 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
026e93dc
PS
3375 return rc;
3376}
3377
4326ed2f
PS
3378static int
3379smb3_is_transform_hdr(void *buf)
3380{
3381 struct smb2_transform_hdr *trhdr = buf;
3382
3383 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3384}
3385
3386static int
3387decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3388 unsigned int buf_data_size, struct page **pages,
3389 unsigned int npages, unsigned int page_data_size)
3390{
c713c877 3391 struct kvec iov[2];
4326ed2f 3392 struct smb_rqst rqst = {NULL};
4326ed2f
PS
3393 int rc;
3394
c713c877
RS
3395 iov[0].iov_base = buf;
3396 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3397 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3398 iov[1].iov_len = buf_data_size;
4326ed2f
PS
3399
3400 rqst.rq_iov = iov;
c713c877 3401 rqst.rq_nvec = 2;
4326ed2f
PS
3402 rqst.rq_pages = pages;
3403 rqst.rq_npages = npages;
3404 rqst.rq_pagesz = PAGE_SIZE;
3405 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3406
b2c96de7 3407 rc = crypt_message(server, 1, &rqst, 0);
4326ed2f
PS
3408 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
3409
3410 if (rc)
3411 return rc;
3412
c713c877 3413 memmove(buf, iov[1].iov_base, buf_data_size);
977b6170
RS
3414
3415 server->total_read = buf_data_size + page_data_size;
4326ed2f
PS
3416
3417 return rc;
3418}
3419
c42a6abe
PS
3420static int
3421read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3422 unsigned int npages, unsigned int len)
3423{
3424 int i;
3425 int length;
3426
3427 for (i = 0; i < npages; i++) {
3428 struct page *page = pages[i];
3429 size_t n;
3430
3431 n = len;
3432 if (len >= PAGE_SIZE) {
3433 /* enough data to fill the page */
3434 n = PAGE_SIZE;
3435 len -= n;
3436 } else {
3437 zero_user(page, len, PAGE_SIZE - len);
3438 len = 0;
3439 }
1dbe3466 3440 length = cifs_read_page_from_socket(server, page, 0, n);
c42a6abe
PS
3441 if (length < 0)
3442 return length;
3443 server->total_read += length;
3444 }
3445
3446 return 0;
3447}
3448
3449static int
3450init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3451 unsigned int cur_off, struct bio_vec **page_vec)
3452{
3453 struct bio_vec *bvec;
3454 int i;
3455
3456 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3457 if (!bvec)
3458 return -ENOMEM;
3459
3460 for (i = 0; i < npages; i++) {
3461 bvec[i].bv_page = pages[i];
3462 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3463 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3464 data_size -= bvec[i].bv_len;
3465 }
3466
3467 if (data_size != 0) {
3468 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3469 kfree(bvec);
3470 return -EIO;
3471 }
3472
3473 *page_vec = bvec;
3474 return 0;
3475}
3476
4326ed2f
PS
3477static int
3478handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3479 char *buf, unsigned int buf_len, struct page **pages,
3480 unsigned int npages, unsigned int page_data_size)
3481{
3482 unsigned int data_offset;
3483 unsigned int data_len;
c42a6abe
PS
3484 unsigned int cur_off;
3485 unsigned int cur_page_idx;
3486 unsigned int pad_len;
4326ed2f 3487 struct cifs_readdata *rdata = mid->callback_data;
49f466bd 3488 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4326ed2f
PS
3489 struct bio_vec *bvec = NULL;
3490 struct iov_iter iter;
3491 struct kvec iov;
3492 int length;
74dcf418 3493 bool use_rdma_mr = false;
4326ed2f
PS
3494
3495 if (shdr->Command != SMB2_READ) {
3496 cifs_dbg(VFS, "only big read responses are supported\n");
3497 return -ENOTSUPP;
3498 }
3499
511c54a2
PS
3500 if (server->ops->is_session_expired &&
3501 server->ops->is_session_expired(buf)) {
3502 cifs_reconnect(server);
3503 wake_up(&server->response_q);
3504 return -1;
3505 }
3506
4326ed2f 3507 if (server->ops->is_status_pending &&
66265f13 3508 server->ops->is_status_pending(buf, server))
4326ed2f
PS
3509 return -1;
3510
ec678eae
PS
3511 /* set up first two iov to get credits */
3512 rdata->iov[0].iov_base = buf;
bb1bccb6
PS
3513 rdata->iov[0].iov_len = 0;
3514 rdata->iov[1].iov_base = buf;
ec678eae 3515 rdata->iov[1].iov_len =
bb1bccb6 3516 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
ec678eae
PS
3517 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3518 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3519 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3520 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3521
3522 rdata->result = server->ops->map_error(buf, true);
4326ed2f
PS
3523 if (rdata->result != 0) {
3524 cifs_dbg(FYI, "%s: server returned error %d\n",
3525 __func__, rdata->result);
ec678eae
PS
3526 /* normal error on read response */
3527 dequeue_mid(mid, false);
4326ed2f
PS
3528 return 0;
3529 }
3530
1fc6ad2f 3531 data_offset = server->ops->read_data_offset(buf);
74dcf418
LL
3532#ifdef CONFIG_CIFS_SMB_DIRECT
3533 use_rdma_mr = rdata->mr;
3534#endif
3535 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4326ed2f
PS
3536
3537 if (data_offset < server->vals->read_rsp_size) {
3538 /*
3539 * win2k8 sometimes sends an offset of 0 when the read
3540 * is beyond the EOF. Treat it as if the data starts just after
3541 * the header.
3542 */
3543 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3544 __func__, data_offset);
3545 data_offset = server->vals->read_rsp_size;
3546 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3547 /* data_offset is beyond the end of smallbuf */
3548 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3549 __func__, data_offset);
3550 rdata->result = -EIO;
3551 dequeue_mid(mid, rdata->result);
3552 return 0;
3553 }
3554
c42a6abe
PS
3555 pad_len = data_offset - server->vals->read_rsp_size;
3556
4326ed2f
PS
3557 if (buf_len <= data_offset) {
3558 /* read response payload is in pages */
c42a6abe
PS
3559 cur_page_idx = pad_len / PAGE_SIZE;
3560 cur_off = pad_len % PAGE_SIZE;
3561
3562 if (cur_page_idx != 0) {
3563 /* data offset is beyond the 1st page of response */
3564 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3565 __func__, data_offset);
3566 rdata->result = -EIO;
3567 dequeue_mid(mid, rdata->result);
3568 return 0;
3569 }
3570
3571 if (data_len > page_data_size - pad_len) {
3572 /* data_len is corrupt -- discard frame */
3573 rdata->result = -EIO;
3574 dequeue_mid(mid, rdata->result);
3575 return 0;
3576 }
3577
3578 rdata->result = init_read_bvec(pages, npages, page_data_size,
3579 cur_off, &bvec);
3580 if (rdata->result != 0) {
3581 dequeue_mid(mid, rdata->result);
3582 return 0;
3583 }
3584
aa563d7b 3585 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4326ed2f
PS
3586 } else if (buf_len >= data_offset + data_len) {
3587 /* read response payload is in buf */
3588 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3589 iov.iov_base = buf + data_offset;
3590 iov.iov_len = data_len;
aa563d7b 3591 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4326ed2f
PS
3592 } else {
3593 /* read response payload cannot be in both buf and pages */
3594 WARN_ONCE(1, "buf can not contain only a part of read data");
3595 rdata->result = -EIO;
3596 dequeue_mid(mid, rdata->result);
3597 return 0;
3598 }
3599
4326ed2f
PS
3600 length = rdata->copy_into_pages(server, rdata, &iter);
3601
3602 kfree(bvec);
3603
3604 if (length < 0)
3605 return length;
3606
3607 dequeue_mid(mid, false);
3608 return length;
3609}
3610
c42a6abe
PS
3611static int
3612receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3613{
3614 char *buf = server->smallbuf;
3615 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3616 unsigned int npages;
3617 struct page **pages;
3618 unsigned int len;
1fc6ad2f 3619 unsigned int buflen = server->pdu_size;
c42a6abe
PS
3620 int rc;
3621 int i = 0;
3622
1fc6ad2f 3623 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
c42a6abe
PS
3624 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3625
3626 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3627 if (rc < 0)
3628 return rc;
3629 server->total_read += rc;
3630
1fc6ad2f 3631 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
93012bf9 3632 server->vals->read_rsp_size;
c42a6abe
PS
3633 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3634
3635 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3636 if (!pages) {
3637 rc = -ENOMEM;
3638 goto discard_data;
3639 }
3640
3641 for (; i < npages; i++) {
3642 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3643 if (!pages[i]) {
3644 rc = -ENOMEM;
3645 goto discard_data;
3646 }
3647 }
3648
3649 /* read read data into pages */
3650 rc = read_data_into_pages(server, pages, npages, len);
3651 if (rc)
3652 goto free_pages;
3653
350be257 3654 rc = cifs_discard_remaining_data(server);
c42a6abe
PS
3655 if (rc)
3656 goto free_pages;
3657
1fc6ad2f 3658 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
c42a6abe
PS
3659 pages, npages, len);
3660 if (rc)
3661 goto free_pages;
3662
3663 *mid = smb2_find_mid(server, buf);
3664 if (*mid == NULL)
3665 cifs_dbg(FYI, "mid not found\n");
3666 else {
3667 cifs_dbg(FYI, "mid found\n");
3668 (*mid)->decrypted = true;
3669 rc = handle_read_data(server, *mid, buf,
3670 server->vals->read_rsp_size,
3671 pages, npages, len);
3672 }
3673
3674free_pages:
3675 for (i = i - 1; i >= 0; i--)
3676 put_page(pages[i]);
3677 kfree(pages);
3678 return rc;
3679discard_data:
350be257 3680 cifs_discard_remaining_data(server);
c42a6abe
PS
3681 goto free_pages;
3682}
3683
4326ed2f
PS
3684static int
3685receive_encrypted_standard(struct TCP_Server_Info *server,
b24df3e3
RS
3686 struct mid_q_entry **mids, char **bufs,
3687 int *num_mids)
4326ed2f 3688{
b24df3e3 3689 int ret, length;
4326ed2f 3690 char *buf = server->smallbuf;
b24df3e3
RS
3691 char *tmpbuf;
3692 struct smb2_sync_hdr *shdr;
2e96467d 3693 unsigned int pdu_length = server->pdu_size;
4326ed2f
PS
3694 unsigned int buf_size;
3695 struct mid_q_entry *mid_entry;
b24df3e3
RS
3696 int next_is_large;
3697 char *next_buffer = NULL;
3698
3699 *num_mids = 0;
4326ed2f
PS
3700
3701 /* switch to large buffer if too big for a small one */
1fc6ad2f 3702 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4326ed2f
PS
3703 server->large_buf = true;
3704 memcpy(server->bigbuf, buf, server->total_read);
3705 buf = server->bigbuf;
3706 }
3707
3708 /* now read the rest */
3709 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
1fc6ad2f 3710 pdu_length - HEADER_SIZE(server) + 1);
4326ed2f
PS
3711 if (length < 0)
3712 return length;
3713 server->total_read += length;
3714
1fc6ad2f 3715 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4326ed2f
PS
3716 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3717 if (length)
3718 return length;
3719
b24df3e3
RS
3720 next_is_large = server->large_buf;
3721 one_more:
3722 shdr = (struct smb2_sync_hdr *)buf;
3723 if (shdr->NextCommand) {
3724 if (next_is_large) {
3725 tmpbuf = server->bigbuf;
3726 next_buffer = (char *)cifs_buf_get();
3727 } else {
3728 tmpbuf = server->smallbuf;
3729 next_buffer = (char *)cifs_small_buf_get();
3730 }
3731 memcpy(next_buffer,
3732 tmpbuf + le32_to_cpu(shdr->NextCommand),
3733 pdu_length - le32_to_cpu(shdr->NextCommand));
3734 }
3735
4326ed2f
PS
3736 mid_entry = smb2_find_mid(server, buf);
3737 if (mid_entry == NULL)
3738 cifs_dbg(FYI, "mid not found\n");
3739 else {
3740 cifs_dbg(FYI, "mid found\n");
3741 mid_entry->decrypted = true;
b24df3e3 3742 mid_entry->resp_buf_size = server->pdu_size;
4326ed2f
PS
3743 }
3744
b24df3e3
RS
3745 if (*num_mids >= MAX_COMPOUND) {
3746 cifs_dbg(VFS, "too many PDUs in compound\n");
3747 return -1;
3748 }
3749 bufs[*num_mids] = buf;
3750 mids[(*num_mids)++] = mid_entry;
4326ed2f
PS
3751
3752 if (mid_entry && mid_entry->handle)
b24df3e3
RS
3753 ret = mid_entry->handle(server, mid_entry);
3754 else
3755 ret = cifs_handle_standard(server, mid_entry);
3756
3757 if (ret == 0 && shdr->NextCommand) {
3758 pdu_length -= le32_to_cpu(shdr->NextCommand);
3759 server->large_buf = next_is_large;
3760 if (next_is_large)
3761 server->bigbuf = next_buffer;
3762 else
3763 server->smallbuf = next_buffer;
3764
3765 buf += le32_to_cpu(shdr->NextCommand);
3766 goto one_more;
3767 }
4326ed2f 3768
b24df3e3 3769 return ret;
4326ed2f
PS
3770}
3771
3772static int
b24df3e3
RS
3773smb3_receive_transform(struct TCP_Server_Info *server,
3774 struct mid_q_entry **mids, char **bufs, int *num_mids)
4326ed2f
PS
3775{
3776 char *buf = server->smallbuf;
2e96467d 3777 unsigned int pdu_length = server->pdu_size;
4326ed2f
PS
3778 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3779 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3780
1fc6ad2f 3781 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4326ed2f
PS
3782 sizeof(struct smb2_sync_hdr)) {
3783 cifs_dbg(VFS, "Transform message is too small (%u)\n",
3784 pdu_length);
3785 cifs_reconnect(server);
3786 wake_up(&server->response_q);
3787 return -ECONNABORTED;
3788 }
3789
1fc6ad2f 3790 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4326ed2f
PS
3791 cifs_dbg(VFS, "Transform message is broken\n");
3792 cifs_reconnect(server);
3793 wake_up(&server->response_q);
3794 return -ECONNABORTED;
3795 }
3796
b24df3e3 3797 /* TODO: add support for compounds containing READ. */
6d2f84ee
PA
3798 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
3799 *num_mids = 1;
b24df3e3 3800 return receive_encrypted_read(server, &mids[0]);
6d2f84ee 3801 }
4326ed2f 3802
b24df3e3 3803 return receive_encrypted_standard(server, mids, bufs, num_mids);
4326ed2f
PS
3804}
3805
3806int
3807smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
3808{
3809 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
3810
1fc6ad2f 3811 return handle_read_data(server, mid, buf, server->pdu_size,
4326ed2f
PS
3812 NULL, 0, 0);
3813}
3814
8ce79ec3
RS
3815static int
3816smb2_next_header(char *buf)
3817{
3818 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
3819 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
3820
3821 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
3822 return sizeof(struct smb2_transform_hdr) +
3823 le32_to_cpu(t_hdr->OriginalMessageSize);
3824
3825 return le32_to_cpu(hdr->NextCommand);
3826}
3827
c847dccf
AA
3828static int
3829smb2_make_node(unsigned int xid, struct inode *inode,
3830 struct dentry *dentry, struct cifs_tcon *tcon,
3831 char *full_path, umode_t mode, dev_t dev)
3832{
3833 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3834 int rc = -EPERM;
3835 int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
3836 FILE_ALL_INFO *buf = NULL;
3837 struct cifs_io_parms io_parms;
3838 __u32 oplock = 0;
3839 struct cifs_fid fid;
3840 struct cifs_open_parms oparms;
3841 unsigned int bytes_written;
3842 struct win_dev *pdev;
3843 struct kvec iov[2];
3844
3845 /*
3846 * Check if mounted with mount parm 'sfu' mount parm.
3847 * SFU emulation should work with all servers, but only
3848 * supports block and char device (no socket & fifo),
3849 * and was used by default in earlier versions of Windows
3850 */
3851 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
3852 goto out;
3853
3854 /*
3855 * TODO: Add ability to create instead via reparse point. Windows (e.g.
3856 * their current NFS server) uses this approach to expose special files
3857 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
3858 */
3859
3860 if (!S_ISCHR(mode) && !S_ISBLK(mode))
3861 goto out;
3862
3863 cifs_dbg(FYI, "sfu compat create special file\n");
3864
3865 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
3866 if (buf == NULL) {
3867 rc = -ENOMEM;
3868 goto out;
3869 }
3870
3871 if (backup_cred(cifs_sb))
3872 create_options |= CREATE_OPEN_BACKUP_INTENT;
3873
3874 oparms.tcon = tcon;
3875 oparms.cifs_sb = cifs_sb;
3876 oparms.desired_access = GENERIC_WRITE;
3877 oparms.create_options = create_options;
3878 oparms.disposition = FILE_CREATE;
3879 oparms.path = full_path;
3880 oparms.fid = &fid;
3881 oparms.reconnect = false;
3882
3883 if (tcon->ses->server->oplocks)
3884 oplock = REQ_OPLOCK;
3885 else
3886 oplock = 0;
3887 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
3888 if (rc)
3889 goto out;
3890
3891 /*
3892 * BB Do not bother to decode buf since no local inode yet to put
3893 * timestamps in, but we can reuse it safely.
3894 */
3895
3896 pdev = (struct win_dev *)buf;
3897 io_parms.pid = current->tgid;
3898 io_parms.tcon = tcon;
3899 io_parms.offset = 0;
3900 io_parms.length = sizeof(struct win_dev);
3901 iov[1].iov_base = buf;
3902 iov[1].iov_len = sizeof(struct win_dev);
3903 if (S_ISCHR(mode)) {
3904 memcpy(pdev->type, "IntxCHR", 8);
3905 pdev->major = cpu_to_le64(MAJOR(dev));
3906 pdev->minor = cpu_to_le64(MINOR(dev));
3907 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
3908 &bytes_written, iov, 1);
3909 } else if (S_ISBLK(mode)) {
3910 memcpy(pdev->type, "IntxBLK", 8);
3911 pdev->major = cpu_to_le64(MAJOR(dev));
3912 pdev->minor = cpu_to_le64(MINOR(dev));
3913 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
3914 &bytes_written, iov, 1);
3915 }
3916 tcon->ses->server->ops->close(xid, tcon, &fid);
3917 d_drop(dentry);
3918
3919 /* FIXME: add code here to set EAs */
3920out:
3921 kfree(buf);
3922 return rc;
3923}
3924
3925
53ef1016 3926struct smb_version_operations smb20_operations = {
027e8eec 3927 .compare_fids = smb2_compare_fids,
2dc7e1c0 3928 .setup_request = smb2_setup_request,
c95b8eed 3929 .setup_async_request = smb2_setup_async_request,
2dc7e1c0 3930 .check_receive = smb2_check_receive,
28ea5290
PS
3931 .add_credits = smb2_add_credits,
3932 .set_credits = smb2_set_credits,
3933 .get_credits_field = smb2_get_credits_field,
3934 .get_credits = smb2_get_credits,
cb7e9eab 3935 .wait_mtu_credits = cifs_wait_mtu_credits,
2dc7e1c0 3936 .get_next_mid = smb2_get_next_mid,
c781af7e 3937 .revert_current_mid = smb2_revert_current_mid,
09a4707e
PS
3938 .read_data_offset = smb2_read_data_offset,
3939 .read_data_length = smb2_read_data_length,
3940 .map_error = map_smb2_to_linux_error,
093b2bda
PS
3941 .find_mid = smb2_find_mid,
3942 .check_message = smb2_check_message,
3943 .dump_detail = smb2_dump_detail,
d60622eb
PS
3944 .clear_stats = smb2_clear_stats,
3945 .print_stats = smb2_print_stats,
983c88a4 3946 .is_oplock_break = smb2_is_valid_oplock_break,
38bd4906 3947 .handle_cancelled_mid = smb2_handle_cancelled_mid,
c11f1df5 3948 .downgrade_oplock = smb2_downgrade_oplock,
ec2e4523
PS
3949 .need_neg = smb2_need_neg,
3950 .negotiate = smb2_negotiate,
3a3bab50
PS
3951 .negotiate_wsize = smb2_negotiate_wsize,
3952 .negotiate_rsize = smb2_negotiate_rsize,
5478f9ba
PS
3953 .sess_setup = SMB2_sess_setup,
3954 .logoff = SMB2_logoff,
faaf946a
PS
3955 .tree_connect = SMB2_tcon,
3956 .tree_disconnect = SMB2_tdis,
34f62640 3957 .qfs_tcon = smb2_qfs_tcon,
2503a0db 3958 .is_path_accessible = smb2_is_path_accessible,
9094fad1
PS
3959 .can_echo = smb2_can_echo,
3960 .echo = SMB2_echo,
be4cb9e3
PS
3961 .query_path_info = smb2_query_path_info,
3962 .get_srv_inum = smb2_get_srv_inum,
b7546bc5 3963 .query_file_info = smb2_query_file_info,
c839ff24
PS
3964 .set_path_size = smb2_set_path_size,
3965 .set_file_size = smb2_set_file_size,
1feeaac7 3966 .set_file_info = smb2_set_file_info,
64a5cfa6 3967 .set_compression = smb2_set_compression,
a0e73183
PS
3968 .mkdir = smb2_mkdir,
3969 .mkdir_setinfo = smb2_mkdir_setinfo,
1a500f01 3970 .rmdir = smb2_rmdir,
cbe6f439 3971 .unlink = smb2_unlink,
35143eb5 3972 .rename = smb2_rename_path,
568798cc 3973 .create_hardlink = smb2_create_hardlink,
b42bf888 3974 .query_symlink = smb2_query_symlink,
5b23c97d
SP
3975 .query_mf_symlink = smb3_query_mf_symlink,
3976 .create_mf_symlink = smb3_create_mf_symlink,
f0df737e
PS
3977 .open = smb2_open_file,
3978 .set_fid = smb2_set_fid,
3979 .close = smb2_close_file,
7a5cfb19 3980 .flush = smb2_flush_file,
09a4707e 3981 .async_readv = smb2_async_readv,
33319141 3982 .async_writev = smb2_async_writev,
d8e05039 3983 .sync_read = smb2_sync_read,
009d3443 3984 .sync_write = smb2_sync_write,
d324f08d
PS
3985 .query_dir_first = smb2_query_dir_first,
3986 .query_dir_next = smb2_query_dir_next,
3987 .close_dir = smb2_close_dir,
3988 .calc_smb_size = smb2_calc_size,
2e44b288 3989 .is_status_pending = smb2_is_status_pending,
511c54a2 3990 .is_session_expired = smb2_is_session_expired,
983c88a4 3991 .oplock_response = smb2_oplock_response,
6fc05c25 3992 .queryfs = smb2_queryfs,
f7ba7fe6
PS
3993 .mand_lock = smb2_mand_lock,
3994 .mand_unlock_range = smb2_unlock_range,
b140799a 3995 .push_mand_locks = smb2_push_mandatory_locks,
b8c32dbb
PS
3996 .get_lease_key = smb2_get_lease_key,
3997 .set_lease_key = smb2_set_lease_key,
3998 .new_lease_key = smb2_new_lease_key,
38107d45 3999 .calc_signature = smb2_calc_signature,
53ef1016
PS
4000 .is_read_op = smb2_is_read_op,
4001 .set_oplock_level = smb2_set_oplock_level,
a41a28bd 4002 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 4003 .parse_lease_buf = smb2_parse_lease_buf,
312bbc59 4004 .copychunk_range = smb2_copychunk_range,
7f6c5008 4005 .wp_retry_size = smb2_wp_retry_size,
52755808 4006 .dir_needs_close = smb2_dir_needs_close,
9d49640a 4007 .get_dfs_refer = smb2_get_dfs_refer,
ef65aaed 4008 .select_sectype = smb2_select_sectype,
95907fea
RS
4009#ifdef CONFIG_CIFS_XATTR
4010 .query_all_EAs = smb2_query_eas,
5517554e 4011 .set_EA = smb2_set_ea,
95907fea 4012#endif /* CIFS_XATTR */
2f1afe25
SP
4013#ifdef CONFIG_CIFS_ACL
4014 .get_acl = get_smb2_acl,
4015 .get_acl_by_fid = get_smb2_acl_by_fid,
366ed846 4016 .set_acl = set_smb2_acl,
2f1afe25 4017#endif /* CIFS_ACL */
8ce79ec3 4018 .next_header = smb2_next_header,
f5b05d62 4019 .ioctl_query_info = smb2_ioctl_query_info,
c847dccf 4020 .make_node = smb2_make_node,
38107d45
SF
4021};
4022
53ef1016
PS
4023struct smb_version_operations smb21_operations = {
4024 .compare_fids = smb2_compare_fids,
4025 .setup_request = smb2_setup_request,
4026 .setup_async_request = smb2_setup_async_request,
4027 .check_receive = smb2_check_receive,
4028 .add_credits = smb2_add_credits,
4029 .set_credits = smb2_set_credits,
4030 .get_credits_field = smb2_get_credits_field,
4031 .get_credits = smb2_get_credits,
cb7e9eab 4032 .wait_mtu_credits = smb2_wait_mtu_credits,
9a1c67e8 4033 .adjust_credits = smb2_adjust_credits,
53ef1016 4034 .get_next_mid = smb2_get_next_mid,
c781af7e 4035 .revert_current_mid = smb2_revert_current_mid,
53ef1016
PS
4036 .read_data_offset = smb2_read_data_offset,
4037 .read_data_length = smb2_read_data_length,
4038 .map_error = map_smb2_to_linux_error,
4039 .find_mid = smb2_find_mid,
4040 .check_message = smb2_check_message,
4041 .dump_detail = smb2_dump_detail,
4042 .clear_stats = smb2_clear_stats,
4043 .print_stats = smb2_print_stats,
4044 .is_oplock_break = smb2_is_valid_oplock_break,
38bd4906 4045 .handle_cancelled_mid = smb2_handle_cancelled_mid,
7b9b9edb 4046 .downgrade_oplock = smb21_downgrade_oplock,
53ef1016
PS
4047 .need_neg = smb2_need_neg,
4048 .negotiate = smb2_negotiate,
4049 .negotiate_wsize = smb2_negotiate_wsize,
4050 .negotiate_rsize = smb2_negotiate_rsize,
4051 .sess_setup = SMB2_sess_setup,
4052 .logoff = SMB2_logoff,
4053 .tree_connect = SMB2_tcon,
4054 .tree_disconnect = SMB2_tdis,
34f62640 4055 .qfs_tcon = smb2_qfs_tcon,
53ef1016
PS
4056 .is_path_accessible = smb2_is_path_accessible,
4057 .can_echo = smb2_can_echo,
4058 .echo = SMB2_echo,
4059 .query_path_info = smb2_query_path_info,
4060 .get_srv_inum = smb2_get_srv_inum,
4061 .query_file_info = smb2_query_file_info,
4062 .set_path_size = smb2_set_path_size,
4063 .set_file_size = smb2_set_file_size,
4064 .set_file_info = smb2_set_file_info,
64a5cfa6 4065 .set_compression = smb2_set_compression,
53ef1016
PS
4066 .mkdir = smb2_mkdir,
4067 .mkdir_setinfo = smb2_mkdir_setinfo,
4068 .rmdir = smb2_rmdir,
4069 .unlink = smb2_unlink,
4070 .rename = smb2_rename_path,
4071 .create_hardlink = smb2_create_hardlink,
4072 .query_symlink = smb2_query_symlink,
c22870ea 4073 .query_mf_symlink = smb3_query_mf_symlink,
5ab97578 4074 .create_mf_symlink = smb3_create_mf_symlink,
53ef1016
PS
4075 .open = smb2_open_file,
4076 .set_fid = smb2_set_fid,
4077 .close = smb2_close_file,
4078 .flush = smb2_flush_file,
4079 .async_readv = smb2_async_readv,
4080 .async_writev = smb2_async_writev,
4081 .sync_read = smb2_sync_read,
4082 .sync_write = smb2_sync_write,
4083 .query_dir_first = smb2_query_dir_first,
4084 .query_dir_next = smb2_query_dir_next,
4085 .close_dir = smb2_close_dir,
4086 .calc_smb_size = smb2_calc_size,
4087 .is_status_pending = smb2_is_status_pending,
511c54a2 4088 .is_session_expired = smb2_is_session_expired,
53ef1016
PS
4089 .oplock_response = smb2_oplock_response,
4090 .queryfs = smb2_queryfs,
4091 .mand_lock = smb2_mand_lock,
4092 .mand_unlock_range = smb2_unlock_range,
4093 .push_mand_locks = smb2_push_mandatory_locks,
4094 .get_lease_key = smb2_get_lease_key,
4095 .set_lease_key = smb2_set_lease_key,
4096 .new_lease_key = smb2_new_lease_key,
4097 .calc_signature = smb2_calc_signature,
4098 .is_read_op = smb21_is_read_op,
4099 .set_oplock_level = smb21_set_oplock_level,
a41a28bd 4100 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 4101 .parse_lease_buf = smb2_parse_lease_buf,
312bbc59 4102 .copychunk_range = smb2_copychunk_range,
7f6c5008 4103 .wp_retry_size = smb2_wp_retry_size,
52755808 4104 .dir_needs_close = smb2_dir_needs_close,
834170c8 4105 .enum_snapshots = smb3_enum_snapshots,
9d49640a 4106 .get_dfs_refer = smb2_get_dfs_refer,
ef65aaed 4107 .select_sectype = smb2_select_sectype,
95907fea
RS
4108#ifdef CONFIG_CIFS_XATTR
4109 .query_all_EAs = smb2_query_eas,
5517554e 4110 .set_EA = smb2_set_ea,
95907fea 4111#endif /* CIFS_XATTR */
2f1afe25
SP
4112#ifdef CONFIG_CIFS_ACL
4113 .get_acl = get_smb2_acl,
4114 .get_acl_by_fid = get_smb2_acl_by_fid,
366ed846 4115 .set_acl = set_smb2_acl,
2f1afe25 4116#endif /* CIFS_ACL */
8ce79ec3 4117 .next_header = smb2_next_header,
f5b05d62 4118 .ioctl_query_info = smb2_ioctl_query_info,
c847dccf 4119 .make_node = smb2_make_node,
53ef1016 4120};
38107d45
SF
4121
4122struct smb_version_operations smb30_operations = {
4123 .compare_fids = smb2_compare_fids,
4124 .setup_request = smb2_setup_request,
4125 .setup_async_request = smb2_setup_async_request,
4126 .check_receive = smb2_check_receive,
4127 .add_credits = smb2_add_credits,
4128 .set_credits = smb2_set_credits,
4129 .get_credits_field = smb2_get_credits_field,
4130 .get_credits = smb2_get_credits,
cb7e9eab 4131 .wait_mtu_credits = smb2_wait_mtu_credits,
9a1c67e8 4132 .adjust_credits = smb2_adjust_credits,
38107d45 4133 .get_next_mid = smb2_get_next_mid,
c781af7e 4134 .revert_current_mid = smb2_revert_current_mid,
38107d45
SF
4135 .read_data_offset = smb2_read_data_offset,
4136 .read_data_length = smb2_read_data_length,
4137 .map_error = map_smb2_to_linux_error,
4138 .find_mid = smb2_find_mid,
4139 .check_message = smb2_check_message,
4140 .dump_detail = smb2_dump_detail,
4141 .clear_stats = smb2_clear_stats,
4142 .print_stats = smb2_print_stats,
769ee6a4 4143 .dump_share_caps = smb2_dump_share_caps,
38107d45 4144 .is_oplock_break = smb2_is_valid_oplock_break,
38bd4906 4145 .handle_cancelled_mid = smb2_handle_cancelled_mid,
7b9b9edb 4146 .downgrade_oplock = smb21_downgrade_oplock,
38107d45
SF
4147 .need_neg = smb2_need_neg,
4148 .negotiate = smb2_negotiate,
3d621230
SF
4149 .negotiate_wsize = smb3_negotiate_wsize,
4150 .negotiate_rsize = smb3_negotiate_rsize,
38107d45
SF
4151 .sess_setup = SMB2_sess_setup,
4152 .logoff = SMB2_logoff,
4153 .tree_connect = SMB2_tcon,
4154 .tree_disconnect = SMB2_tdis,
af6a12ea 4155 .qfs_tcon = smb3_qfs_tcon,
38107d45
SF
4156 .is_path_accessible = smb2_is_path_accessible,
4157 .can_echo = smb2_can_echo,
4158 .echo = SMB2_echo,
4159 .query_path_info = smb2_query_path_info,
4160 .get_srv_inum = smb2_get_srv_inum,
4161 .query_file_info = smb2_query_file_info,
4162 .set_path_size = smb2_set_path_size,
4163 .set_file_size = smb2_set_file_size,
4164 .set_file_info = smb2_set_file_info,
64a5cfa6 4165 .set_compression = smb2_set_compression,
38107d45
SF
4166 .mkdir = smb2_mkdir,
4167 .mkdir_setinfo = smb2_mkdir_setinfo,
4168 .rmdir = smb2_rmdir,
4169 .unlink = smb2_unlink,
4170 .rename = smb2_rename_path,
4171 .create_hardlink = smb2_create_hardlink,
b42bf888 4172 .query_symlink = smb2_query_symlink,
c22870ea 4173 .query_mf_symlink = smb3_query_mf_symlink,
5ab97578 4174 .create_mf_symlink = smb3_create_mf_symlink,
38107d45
SF
4175 .open = smb2_open_file,
4176 .set_fid = smb2_set_fid,
4177 .close = smb2_close_file,
4178 .flush = smb2_flush_file,
4179 .async_readv = smb2_async_readv,
4180 .async_writev = smb2_async_writev,
4181 .sync_read = smb2_sync_read,
4182 .sync_write = smb2_sync_write,
4183 .query_dir_first = smb2_query_dir_first,
4184 .query_dir_next = smb2_query_dir_next,
4185 .close_dir = smb2_close_dir,
4186 .calc_smb_size = smb2_calc_size,
4187 .is_status_pending = smb2_is_status_pending,
511c54a2 4188 .is_session_expired = smb2_is_session_expired,
38107d45
SF
4189 .oplock_response = smb2_oplock_response,
4190 .queryfs = smb2_queryfs,
4191 .mand_lock = smb2_mand_lock,
4192 .mand_unlock_range = smb2_unlock_range,
4193 .push_mand_locks = smb2_push_mandatory_locks,
4194 .get_lease_key = smb2_get_lease_key,
4195 .set_lease_key = smb2_set_lease_key,
4196 .new_lease_key = smb2_new_lease_key,
373512ec 4197 .generate_signingkey = generate_smb30signingkey,
38107d45 4198 .calc_signature = smb3_calc_signature,
b3152e2c 4199 .set_integrity = smb3_set_integrity,
53ef1016 4200 .is_read_op = smb21_is_read_op,
42873b0a 4201 .set_oplock_level = smb3_set_oplock_level,
f047390a
PS
4202 .create_lease_buf = smb3_create_lease_buf,
4203 .parse_lease_buf = smb3_parse_lease_buf,
312bbc59 4204 .copychunk_range = smb2_copychunk_range,
ca9e7a1c 4205 .duplicate_extents = smb2_duplicate_extents,
ff1c038a 4206 .validate_negotiate = smb3_validate_negotiate,
7f6c5008 4207 .wp_retry_size = smb2_wp_retry_size,
52755808 4208 .dir_needs_close = smb2_dir_needs_close,
31742c5a 4209 .fallocate = smb3_fallocate,
834170c8 4210 .enum_snapshots = smb3_enum_snapshots,
026e93dc 4211 .init_transform_rq = smb3_init_transform_rq,
4326ed2f
PS
4212 .is_transform_hdr = smb3_is_transform_hdr,
4213 .receive_transform = smb3_receive_transform,
9d49640a 4214 .get_dfs_refer = smb2_get_dfs_refer,
ef65aaed 4215 .select_sectype = smb2_select_sectype,
95907fea
RS
4216#ifdef CONFIG_CIFS_XATTR
4217 .query_all_EAs = smb2_query_eas,
5517554e 4218 .set_EA = smb2_set_ea,
95907fea 4219#endif /* CIFS_XATTR */
2f1afe25
SP
4220#ifdef CONFIG_CIFS_ACL
4221 .get_acl = get_smb2_acl,
4222 .get_acl_by_fid = get_smb2_acl_by_fid,
366ed846 4223 .set_acl = set_smb2_acl,
2f1afe25 4224#endif /* CIFS_ACL */
8ce79ec3 4225 .next_header = smb2_next_header,
f5b05d62 4226 .ioctl_query_info = smb2_ioctl_query_info,
c847dccf 4227 .make_node = smb2_make_node,
1080ef75
SF
4228};
4229
aab1893d
SF
4230struct smb_version_operations smb311_operations = {
4231 .compare_fids = smb2_compare_fids,
4232 .setup_request = smb2_setup_request,
4233 .setup_async_request = smb2_setup_async_request,
4234 .check_receive = smb2_check_receive,
4235 .add_credits = smb2_add_credits,
4236 .set_credits = smb2_set_credits,
4237 .get_credits_field = smb2_get_credits_field,
4238 .get_credits = smb2_get_credits,
4239 .wait_mtu_credits = smb2_wait_mtu_credits,
9a1c67e8 4240 .adjust_credits = smb2_adjust_credits,
aab1893d 4241 .get_next_mid = smb2_get_next_mid,
c781af7e 4242 .revert_current_mid = smb2_revert_current_mid,
aab1893d
SF
4243 .read_data_offset = smb2_read_data_offset,
4244 .read_data_length = smb2_read_data_length,
4245 .map_error = map_smb2_to_linux_error,
4246 .find_mid = smb2_find_mid,
4247 .check_message = smb2_check_message,
4248 .dump_detail = smb2_dump_detail,
4249 .clear_stats = smb2_clear_stats,
4250 .print_stats = smb2_print_stats,
4251 .dump_share_caps = smb2_dump_share_caps,
4252 .is_oplock_break = smb2_is_valid_oplock_break,
38bd4906 4253 .handle_cancelled_mid = smb2_handle_cancelled_mid,
7b9b9edb 4254 .downgrade_oplock = smb21_downgrade_oplock,
aab1893d
SF
4255 .need_neg = smb2_need_neg,
4256 .negotiate = smb2_negotiate,
3d621230
SF
4257 .negotiate_wsize = smb3_negotiate_wsize,
4258 .negotiate_rsize = smb3_negotiate_rsize,
aab1893d
SF
4259 .sess_setup = SMB2_sess_setup,
4260 .logoff = SMB2_logoff,
4261 .tree_connect = SMB2_tcon,
4262 .tree_disconnect = SMB2_tdis,
4263 .qfs_tcon = smb3_qfs_tcon,
4264 .is_path_accessible = smb2_is_path_accessible,
4265 .can_echo = smb2_can_echo,
4266 .echo = SMB2_echo,
4267 .query_path_info = smb2_query_path_info,
4268 .get_srv_inum = smb2_get_srv_inum,
4269 .query_file_info = smb2_query_file_info,
4270 .set_path_size = smb2_set_path_size,
4271 .set_file_size = smb2_set_file_size,
4272 .set_file_info = smb2_set_file_info,
4273 .set_compression = smb2_set_compression,
4274 .mkdir = smb2_mkdir,
4275 .mkdir_setinfo = smb2_mkdir_setinfo,
bea851b8 4276 .posix_mkdir = smb311_posix_mkdir,
aab1893d
SF
4277 .rmdir = smb2_rmdir,
4278 .unlink = smb2_unlink,
4279 .rename = smb2_rename_path,
4280 .create_hardlink = smb2_create_hardlink,
4281 .query_symlink = smb2_query_symlink,
4282 .query_mf_symlink = smb3_query_mf_symlink,
4283 .create_mf_symlink = smb3_create_mf_symlink,
4284 .open = smb2_open_file,
4285 .set_fid = smb2_set_fid,
4286 .close = smb2_close_file,
4287 .flush = smb2_flush_file,
4288 .async_readv = smb2_async_readv,
4289 .async_writev = smb2_async_writev,
4290 .sync_read = smb2_sync_read,
4291 .sync_write = smb2_sync_write,
4292 .query_dir_first = smb2_query_dir_first,
4293 .query_dir_next = smb2_query_dir_next,
4294 .close_dir = smb2_close_dir,
4295 .calc_smb_size = smb2_calc_size,
4296 .is_status_pending = smb2_is_status_pending,
511c54a2 4297 .is_session_expired = smb2_is_session_expired,
aab1893d 4298 .oplock_response = smb2_oplock_response,
2d304217 4299 .queryfs = smb311_queryfs,
aab1893d
SF
4300 .mand_lock = smb2_mand_lock,
4301 .mand_unlock_range = smb2_unlock_range,
4302 .push_mand_locks = smb2_push_mandatory_locks,
4303 .get_lease_key = smb2_get_lease_key,
4304 .set_lease_key = smb2_set_lease_key,
4305 .new_lease_key = smb2_new_lease_key,
373512ec 4306 .generate_signingkey = generate_smb311signingkey,
aab1893d 4307 .calc_signature = smb3_calc_signature,
b3152e2c 4308 .set_integrity = smb3_set_integrity,
aab1893d
SF
4309 .is_read_op = smb21_is_read_op,
4310 .set_oplock_level = smb3_set_oplock_level,
4311 .create_lease_buf = smb3_create_lease_buf,
4312 .parse_lease_buf = smb3_parse_lease_buf,
312bbc59 4313 .copychunk_range = smb2_copychunk_range,
02b16665 4314 .duplicate_extents = smb2_duplicate_extents,
aab1893d
SF
4315/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4316 .wp_retry_size = smb2_wp_retry_size,
4317 .dir_needs_close = smb2_dir_needs_close,
4318 .fallocate = smb3_fallocate,
834170c8 4319 .enum_snapshots = smb3_enum_snapshots,
026e93dc 4320 .init_transform_rq = smb3_init_transform_rq,
4326ed2f
PS
4321 .is_transform_hdr = smb3_is_transform_hdr,
4322 .receive_transform = smb3_receive_transform,
9d49640a 4323 .get_dfs_refer = smb2_get_dfs_refer,
ef65aaed 4324 .select_sectype = smb2_select_sectype,
95907fea
RS
4325#ifdef CONFIG_CIFS_XATTR
4326 .query_all_EAs = smb2_query_eas,
5517554e 4327 .set_EA = smb2_set_ea,
95907fea 4328#endif /* CIFS_XATTR */
c1777df1
RS
4329#ifdef CONFIG_CIFS_ACL
4330 .get_acl = get_smb2_acl,
4331 .get_acl_by_fid = get_smb2_acl_by_fid,
4332 .set_acl = set_smb2_acl,
4333#endif /* CIFS_ACL */
8ce79ec3 4334 .next_header = smb2_next_header,
f5b05d62 4335 .ioctl_query_info = smb2_ioctl_query_info,
c847dccf 4336 .make_node = smb2_make_node,
aab1893d 4337};
aab1893d 4338
dd446b16
SF
4339struct smb_version_values smb20_values = {
4340 .version_string = SMB20_VERSION_STRING,
4341 .protocol_id = SMB20_PROT_ID,
4342 .req_capabilities = 0, /* MBZ */
4343 .large_lock_type = 0,
4344 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4345 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4346 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
977b6170
RS
4347 .header_size = sizeof(struct smb2_sync_hdr),
4348 .header_preamble_size = 0,
dd446b16
SF
4349 .max_header_size = MAX_SMB2_HDR_SIZE,
4350 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4351 .lock_cmd = SMB2_LOCK,
4352 .cap_unix = 0,
4353 .cap_nt_find = SMB2_NT_FIND,
4354 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
4355 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4356 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 4357 .create_lease_size = sizeof(struct create_lease),
dd446b16
SF
4358};
4359
1080ef75
SF
4360struct smb_version_values smb21_values = {
4361 .version_string = SMB21_VERSION_STRING,
e4aa25e7
SF
4362 .protocol_id = SMB21_PROT_ID,
4363 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
4364 .large_lock_type = 0,
4365 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4366 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4367 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
977b6170
RS
4368 .header_size = sizeof(struct smb2_sync_hdr),
4369 .header_preamble_size = 0,
e4aa25e7
SF
4370 .max_header_size = MAX_SMB2_HDR_SIZE,
4371 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4372 .lock_cmd = SMB2_LOCK,
4373 .cap_unix = 0,
4374 .cap_nt_find = SMB2_NT_FIND,
4375 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
4376 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4377 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 4378 .create_lease_size = sizeof(struct create_lease),
e4aa25e7
SF
4379};
4380
9764c02f
SF
4381struct smb_version_values smb3any_values = {
4382 .version_string = SMB3ANY_VERSION_STRING,
4383 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
f8015683 4384 .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,
9764c02f
SF
4385 .large_lock_type = 0,
4386 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4387 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4388 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
977b6170
RS
4389 .header_size = sizeof(struct smb2_sync_hdr),
4390 .header_preamble_size = 0,
9764c02f
SF
4391 .max_header_size = MAX_SMB2_HDR_SIZE,
4392 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4393 .lock_cmd = SMB2_LOCK,
4394 .cap_unix = 0,
4395 .cap_nt_find = SMB2_NT_FIND,
4396 .cap_large_files = SMB2_LARGE_FILES,
4397 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4398 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4399 .create_lease_size = sizeof(struct create_lease_v2),
4400};
4401
4402struct smb_version_values smbdefault_values = {
4403 .version_string = SMBDEFAULT_VERSION_STRING,
4404 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
f8015683 4405 .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,
9764c02f
SF
4406 .large_lock_type = 0,
4407 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4408 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4409 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
977b6170
RS
4410 .header_size = sizeof(struct smb2_sync_hdr),
4411 .header_preamble_size = 0,
9764c02f
SF
4412 .max_header_size = MAX_SMB2_HDR_SIZE,
4413 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4414 .lock_cmd = SMB2_LOCK,
4415 .cap_unix = 0,
4416 .cap_nt_find = SMB2_NT_FIND,
4417 .cap_large_files = SMB2_LARGE_FILES,
4418 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4419 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4420 .create_lease_size = sizeof(struct create_lease_v2),
4421};
4422
e4aa25e7
SF
4423struct smb_version_values smb30_values = {
4424 .version_string = SMB30_VERSION_STRING,
4425 .protocol_id = SMB30_PROT_ID,
f8015683 4426 .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,
027e8eec
PS
4427 .large_lock_type = 0,
4428 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4429 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4430 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
977b6170
RS
4431 .header_size = sizeof(struct smb2_sync_hdr),
4432 .header_preamble_size = 0,
093b2bda 4433 .max_header_size = MAX_SMB2_HDR_SIZE,
09a4707e 4434 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2dc7e1c0 4435 .lock_cmd = SMB2_LOCK,
29e20f9c
PS
4436 .cap_unix = 0,
4437 .cap_nt_find = SMB2_NT_FIND,
4438 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
4439 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4440 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 4441 .create_lease_size = sizeof(struct create_lease_v2),
1080ef75 4442};
20b6d8b4
SF
4443
4444struct smb_version_values smb302_values = {
4445 .version_string = SMB302_VERSION_STRING,
4446 .protocol_id = SMB302_PROT_ID,
f8015683 4447 .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,
20b6d8b4
SF
4448 .large_lock_type = 0,
4449 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4450 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4451 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
977b6170
RS
4452 .header_size = sizeof(struct smb2_sync_hdr),
4453 .header_preamble_size = 0,
20b6d8b4
SF
4454 .max_header_size = MAX_SMB2_HDR_SIZE,
4455 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4456 .lock_cmd = SMB2_LOCK,
4457 .cap_unix = 0,
4458 .cap_nt_find = SMB2_NT_FIND,
4459 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
4460 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4461 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 4462 .create_lease_size = sizeof(struct create_lease_v2),
20b6d8b4 4463};
5f7fbf73 4464
5f7fbf73
SF
4465struct smb_version_values smb311_values = {
4466 .version_string = SMB311_VERSION_STRING,
4467 .protocol_id = SMB311_PROT_ID,
f8015683 4468 .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,
5f7fbf73
SF
4469 .large_lock_type = 0,
4470 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4471 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4472 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
977b6170
RS
4473 .header_size = sizeof(struct smb2_sync_hdr),
4474 .header_preamble_size = 0,
5f7fbf73
SF
4475 .max_header_size = MAX_SMB2_HDR_SIZE,
4476 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4477 .lock_cmd = SMB2_LOCK,
4478 .cap_unix = 0,
4479 .cap_nt_find = SMB2_NT_FIND,
4480 .cap_large_files = SMB2_LARGE_FILES,
4481 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4482 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4483 .create_lease_size = sizeof(struct create_lease_v2),
4484};