blob: fdb1d660bd136ed2587746c860926c1ad5dbe321 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * SMB2 version specific operations
4 *
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7
8#include <linux/pagemap.h>
9#include <linux/vfs.h>
10#include <linux/falloc.h>
11#include <linux/scatterlist.h>
12#include <linux/uuid.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020013#include <linux/sort.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include <crypto/aead.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020015#include <linux/fiemap.h>
16#include "cifsfs.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017#include "cifsglob.h"
18#include "smb2pdu.h"
19#include "smb2proto.h"
20#include "cifsproto.h"
21#include "cifs_debug.h"
22#include "cifs_unicode.h"
23#include "smb2status.h"
24#include "smb2glob.h"
25#include "cifs_ioctl.h"
26#include "smbdirect.h"
27
David Brazdil0f672f62019-12-10 10:32:29 +000028/* Change credits for different ops and return the total number of credits */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029static int
30change_conf(struct TCP_Server_Info *server)
31{
32 server->credits += server->echo_credits + server->oplock_credits;
33 server->oplock_credits = server->echo_credits = 0;
34 switch (server->credits) {
35 case 0:
David Brazdil0f672f62019-12-10 10:32:29 +000036 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 case 1:
38 server->echoes = false;
39 server->oplocks = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040 break;
41 case 2:
42 server->echoes = true;
43 server->oplocks = false;
44 server->echo_credits = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000045 break;
46 default:
47 server->echoes = true;
48 if (enable_oplocks) {
49 server->oplocks = true;
50 server->oplock_credits = 1;
51 } else
52 server->oplocks = false;
53
54 server->echo_credits = 1;
55 }
56 server->credits -= server->echo_credits + server->oplock_credits;
David Brazdil0f672f62019-12-10 10:32:29 +000057 return server->credits + server->echo_credits + server->oplock_credits;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058}
59
60static void
David Brazdil0f672f62019-12-10 10:32:29 +000061smb2_add_credits(struct TCP_Server_Info *server,
62 const struct cifs_credits *credits, const int optype)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063{
David Brazdil0f672f62019-12-10 10:32:29 +000064 int *val, rc = -1;
65 unsigned int add = credits->value;
66 unsigned int instance = credits->instance;
67 bool reconnect_detected = false;
68
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069 spin_lock(&server->req_lock);
70 val = server->ops->get_credits_field(server, optype);
David Brazdil0f672f62019-12-10 10:32:29 +000071
72 /* eg found case where write overlapping reconnect messed up credits */
73 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
74 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
Olivier Deprez157378f2022-04-04 15:47:50 +020075 server->hostname, *val, add);
David Brazdil0f672f62019-12-10 10:32:29 +000076 if ((instance == 0) || (instance == server->reconnect_instance))
77 *val += add;
78 else
79 reconnect_detected = true;
80
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081 if (*val > 65000) {
82 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
Olivier Deprez157378f2022-04-04 15:47:50 +020083 pr_warn_once("server overflowed SMB3 credits\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 }
85 server->in_flight--;
86 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
87 rc = change_conf(server);
88 /*
89 * Sometimes server returns 0 credits on oplock break ack - we need to
90 * rebalance credits in this case.
91 */
92 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
93 server->oplocks) {
94 if (server->credits > 1) {
95 server->credits--;
96 server->oplock_credits++;
97 }
98 }
99 spin_unlock(&server->req_lock);
100 wake_up(&server->request_q);
David Brazdil0f672f62019-12-10 10:32:29 +0000101
102 if (reconnect_detected)
103 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
104 add, instance);
105
106 if (server->tcpStatus == CifsNeedReconnect
107 || server->tcpStatus == CifsExiting)
108 return;
109
110 switch (rc) {
111 case -1:
112 /* change_conf hasn't been executed */
113 break;
114 case 0:
115 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
116 break;
117 case 1:
118 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
119 break;
120 case 2:
121 cifs_dbg(FYI, "disabling oplocks\n");
122 break;
123 default:
Olivier Deprez157378f2022-04-04 15:47:50 +0200124 trace_smb3_add_credits(server->CurrentMid,
125 server->hostname, rc, add);
David Brazdil0f672f62019-12-10 10:32:29 +0000126 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
127 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128}
129
130static void
131smb2_set_credits(struct TCP_Server_Info *server, const int val)
132{
133 spin_lock(&server->req_lock);
134 server->credits = val;
David Brazdil0f672f62019-12-10 10:32:29 +0000135 if (val == 1)
136 server->reconnect_instance++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137 spin_unlock(&server->req_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000138 /* don't log while holding the lock */
139 if (val == 1)
140 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141}
142
143static int *
144smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
145{
146 switch (optype) {
147 case CIFS_ECHO_OP:
148 return &server->echo_credits;
149 case CIFS_OBREAK_OP:
150 return &server->oplock_credits;
151 default:
152 return &server->credits;
153 }
154}
155
156static unsigned int
157smb2_get_credits(struct mid_q_entry *mid)
158{
Olivier Deprez0e641232021-09-23 10:07:05 +0200159 return mid->credits_received;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160}
161
162static int
163smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
David Brazdil0f672f62019-12-10 10:32:29 +0000164 unsigned int *num, struct cifs_credits *credits)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165{
166 int rc = 0;
167 unsigned int scredits;
168
169 spin_lock(&server->req_lock);
170 while (1) {
171 if (server->credits <= 0) {
172 spin_unlock(&server->req_lock);
173 cifs_num_waiters_inc(server);
174 rc = wait_event_killable(server->request_q,
David Brazdil0f672f62019-12-10 10:32:29 +0000175 has_credits(server, &server->credits, 1));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000176 cifs_num_waiters_dec(server);
177 if (rc)
178 return rc;
179 spin_lock(&server->req_lock);
180 } else {
181 if (server->tcpStatus == CifsExiting) {
182 spin_unlock(&server->req_lock);
183 return -ENOENT;
184 }
185
186 scredits = server->credits;
187 /* can deadlock with reopen */
David Brazdil0f672f62019-12-10 10:32:29 +0000188 if (scredits <= 8) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189 *num = SMB2_MAX_BUFFER_SIZE;
David Brazdil0f672f62019-12-10 10:32:29 +0000190 credits->value = 0;
191 credits->instance = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000192 break;
193 }
194
David Brazdil0f672f62019-12-10 10:32:29 +0000195 /* leave some credits for reopen and other ops */
196 scredits -= 8;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000197 *num = min_t(unsigned int, size,
198 scredits * SMB2_MAX_BUFFER_SIZE);
199
David Brazdil0f672f62019-12-10 10:32:29 +0000200 credits->value =
201 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
202 credits->instance = server->reconnect_instance;
203 server->credits -= credits->value;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000204 server->in_flight++;
David Brazdil0f672f62019-12-10 10:32:29 +0000205 if (server->in_flight > server->max_in_flight)
206 server->max_in_flight = server->in_flight;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000207 break;
208 }
209 }
210 spin_unlock(&server->req_lock);
211 return rc;
212}
213
David Brazdil0f672f62019-12-10 10:32:29 +0000214static int
215smb2_adjust_credits(struct TCP_Server_Info *server,
216 struct cifs_credits *credits,
217 const unsigned int payload_size)
218{
219 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
220
221 if (!credits->value || credits->value == new_val)
222 return 0;
223
224 if (credits->value < new_val) {
225 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
226 credits->value, new_val);
227 return -ENOTSUPP;
228 }
229
230 spin_lock(&server->req_lock);
231
232 if (server->reconnect_instance != credits->instance) {
233 spin_unlock(&server->req_lock);
234 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
235 credits->value - new_val);
236 return -EAGAIN;
237 }
238
239 server->credits += credits->value - new_val;
240 spin_unlock(&server->req_lock);
241 wake_up(&server->request_q);
242 credits->value = new_val;
243 return 0;
244}
245
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000246static __u64
247smb2_get_next_mid(struct TCP_Server_Info *server)
248{
249 __u64 mid;
250 /* for SMB2 we need the current value */
251 spin_lock(&GlobalMid_Lock);
252 mid = server->CurrentMid++;
253 spin_unlock(&GlobalMid_Lock);
254 return mid;
255}
256
David Brazdil0f672f62019-12-10 10:32:29 +0000257static void
258smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
259{
260 spin_lock(&GlobalMid_Lock);
261 if (server->CurrentMid >= val)
262 server->CurrentMid -= val;
263 spin_unlock(&GlobalMid_Lock);
264}
265
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266static struct mid_q_entry *
Olivier Deprez0e641232021-09-23 10:07:05 +0200267__smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268{
269 struct mid_q_entry *mid;
270 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
271 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
272
273 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
David Brazdil0f672f62019-12-10 10:32:29 +0000274 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275 return NULL;
276 }
277
278 spin_lock(&GlobalMid_Lock);
279 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
280 if ((mid->mid == wire_mid) &&
281 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
282 (mid->command == shdr->Command)) {
283 kref_get(&mid->refcount);
Olivier Deprez0e641232021-09-23 10:07:05 +0200284 if (dequeue) {
285 list_del_init(&mid->qhead);
286 mid->mid_flags |= MID_DELETED;
287 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288 spin_unlock(&GlobalMid_Lock);
289 return mid;
290 }
291 }
292 spin_unlock(&GlobalMid_Lock);
293 return NULL;
294}
295
Olivier Deprez0e641232021-09-23 10:07:05 +0200296static struct mid_q_entry *
297smb2_find_mid(struct TCP_Server_Info *server, char *buf)
298{
299 return __smb2_find_mid(server, buf, false);
300}
301
302static struct mid_q_entry *
303smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
304{
305 return __smb2_find_mid(server, buf, true);
306}
307
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000308static void
309smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
310{
311#ifdef CONFIG_CIFS_DEBUG2
312 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
313
David Brazdil0f672f62019-12-10 10:32:29 +0000314 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
316 shdr->ProcessId);
David Brazdil0f672f62019-12-10 10:32:29 +0000317 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000318 server->ops->calc_smb_size(buf, server));
319#endif
320}
321
322static bool
323smb2_need_neg(struct TCP_Server_Info *server)
324{
325 return server->max_read == 0;
326}
327
328static int
329smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
330{
331 int rc;
David Brazdil0f672f62019-12-10 10:32:29 +0000332
Olivier Deprez157378f2022-04-04 15:47:50 +0200333 cifs_ses_server(ses)->CurrentMid = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000334 rc = SMB2_negotiate(xid, ses);
335 /* BB we probably don't need to retry with modern servers */
336 if (rc == -EAGAIN)
337 rc = -EHOSTDOWN;
338 return rc;
339}
340
341static unsigned int
342smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
343{
344 struct TCP_Server_Info *server = tcon->ses->server;
345 unsigned int wsize;
346
347 /* start with specified wsize, or default */
348 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
349 wsize = min_t(unsigned int, wsize, server->max_write);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000350 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
351 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
352
353 return wsize;
354}
355
356static unsigned int
David Brazdil0f672f62019-12-10 10:32:29 +0000357smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
358{
359 struct TCP_Server_Info *server = tcon->ses->server;
360 unsigned int wsize;
361
362 /* start with specified wsize, or default */
363 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
364 wsize = min_t(unsigned int, wsize, server->max_write);
365#ifdef CONFIG_CIFS_SMB_DIRECT
366 if (server->rdma) {
367 if (server->sign)
Olivier Deprez157378f2022-04-04 15:47:50 +0200368 /*
369 * Account for SMB2 data transfer packet header and
370 * possible encryption header
371 */
David Brazdil0f672f62019-12-10 10:32:29 +0000372 wsize = min_t(unsigned int,
Olivier Deprez157378f2022-04-04 15:47:50 +0200373 wsize,
374 server->smbd_conn->max_fragmented_send_size -
375 SMB2_READWRITE_PDU_HEADER_SIZE -
376 sizeof(struct smb2_transform_hdr));
David Brazdil0f672f62019-12-10 10:32:29 +0000377 else
378 wsize = min_t(unsigned int,
379 wsize, server->smbd_conn->max_readwrite_size);
380 }
381#endif
382 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
383 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
384
385 return wsize;
386}
387
388static unsigned int
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
390{
391 struct TCP_Server_Info *server = tcon->ses->server;
392 unsigned int rsize;
393
394 /* start with specified rsize, or default */
395 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
396 rsize = min_t(unsigned int, rsize, server->max_read);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000397
398 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
399 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
400
401 return rsize;
402}
403
David Brazdil0f672f62019-12-10 10:32:29 +0000404static unsigned int
405smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
406{
407 struct TCP_Server_Info *server = tcon->ses->server;
408 unsigned int rsize;
409
410 /* start with specified rsize, or default */
411 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
412 rsize = min_t(unsigned int, rsize, server->max_read);
413#ifdef CONFIG_CIFS_SMB_DIRECT
414 if (server->rdma) {
415 if (server->sign)
Olivier Deprez157378f2022-04-04 15:47:50 +0200416 /*
417 * Account for SMB2 data transfer packet header and
418 * possible encryption header
419 */
David Brazdil0f672f62019-12-10 10:32:29 +0000420 rsize = min_t(unsigned int,
Olivier Deprez157378f2022-04-04 15:47:50 +0200421 rsize,
422 server->smbd_conn->max_fragmented_recv_size -
423 SMB2_READWRITE_PDU_HEADER_SIZE -
424 sizeof(struct smb2_transform_hdr));
David Brazdil0f672f62019-12-10 10:32:29 +0000425 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}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000436
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
Olivier Deprez0e641232021-09-23 10:07:05 +0200480 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
481 if ((bytes_left > 8) || p->Next)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000482 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
483
484
485 /*
486 * Second pass: extract info to internal structure
487 */
488
489 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
490 if (!*iface_list) {
491 rc = -ENOMEM;
492 goto out;
493 }
494
495 info = *iface_list;
496 bytes_left = buf_len;
497 p = buf;
498 while (bytes_left >= sizeof(*p)) {
499 info->speed = le64_to_cpu(p->LinkSpeed);
Olivier Deprez0e641232021-09-23 10:07:05 +0200500 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
501 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502
503 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
504 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
505 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
506 le32_to_cpu(p->Capability));
507
508 switch (p->Family) {
509 /*
510 * The kernel and wire socket structures have the same
511 * layout and use network byte order but make the
512 * conversion explicit in case either one changes.
513 */
514 case INTERNETWORK:
515 addr4 = (struct sockaddr_in *)&info->sockaddr;
516 p4 = (struct iface_info_ipv4 *)p->Buffer;
517 addr4->sin_family = AF_INET;
518 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
519
520 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
521 addr4->sin_port = cpu_to_be16(CIFS_PORT);
522
523 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
524 &addr4->sin_addr);
525 break;
526 case INTERNETWORKV6:
527 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
528 p6 = (struct iface_info_ipv6 *)p->Buffer;
529 addr6->sin6_family = AF_INET6;
530 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
531
532 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
533 addr6->sin6_flowinfo = 0;
534 addr6->sin6_scope_id = 0;
535 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
536
537 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
538 &addr6->sin6_addr);
539 break;
540 default:
541 cifs_dbg(VFS,
542 "%s: skipping unsupported socket family\n",
543 __func__);
544 goto next_iface;
545 }
546
547 (*iface_count)++;
548 info++;
549next_iface:
550 next = le32_to_cpu(p->Next);
551 if (!next)
552 break;
553 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
554 bytes_left -= next;
555 }
556
557 if (!*iface_count) {
558 rc = -EINVAL;
559 goto out;
560 }
561
562out:
563 if (rc) {
564 kfree(*iface_list);
565 *iface_count = 0;
566 *iface_list = NULL;
567 }
568 return rc;
569}
570
Olivier Deprez157378f2022-04-04 15:47:50 +0200571static int compare_iface(const void *ia, const void *ib)
572{
573 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
574 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
575
576 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
577}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000578
579static int
580SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
581{
582 int rc;
583 unsigned int ret_data_len = 0;
584 struct network_interface_info_ioctl_rsp *out_buf = NULL;
585 struct cifs_server_iface *iface_list;
586 size_t iface_count;
587 struct cifs_ses *ses = tcon->ses;
588
589 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
590 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
591 NULL /* no data input */, 0 /* no data input */,
David Brazdil0f672f62019-12-10 10:32:29 +0000592 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000593 if (rc == -EOPNOTSUPP) {
594 cifs_dbg(FYI,
595 "server does not support query network interfaces\n");
596 goto out;
597 } else if (rc != 0) {
David Brazdil0f672f62019-12-10 10:32:29 +0000598 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000599 goto out;
600 }
601
602 rc = parse_server_interfaces(out_buf, ret_data_len,
603 &iface_list, &iface_count);
604 if (rc)
605 goto out;
606
Olivier Deprez157378f2022-04-04 15:47:50 +0200607 /* sort interfaces from fastest to slowest */
608 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
609
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000610 spin_lock(&ses->iface_lock);
611 kfree(ses->iface_list);
612 ses->iface_list = iface_list;
613 ses->iface_count = iface_count;
614 ses->iface_last_update = jiffies;
615 spin_unlock(&ses->iface_lock);
616
617out:
618 kfree(out_buf);
619 return rc;
620}
621
622static void
623smb2_close_cached_fid(struct kref *ref)
624{
625 struct cached_fid *cfid = container_of(ref, struct cached_fid,
626 refcount);
627
628 if (cfid->is_valid) {
629 cifs_dbg(FYI, "clear cached root file handle\n");
630 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
631 cfid->fid->volatile_fid);
632 cfid->is_valid = false;
David Brazdil0f672f62019-12-10 10:32:29 +0000633 cfid->file_all_info_is_valid = false;
Olivier Deprez157378f2022-04-04 15:47:50 +0200634 cfid->has_lease = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635 }
636}
637
638void close_shroot(struct cached_fid *cfid)
639{
640 mutex_lock(&cfid->fid_mutex);
641 kref_put(&cfid->refcount, smb2_close_cached_fid);
642 mutex_unlock(&cfid->fid_mutex);
643}
644
Olivier Deprez157378f2022-04-04 15:47:50 +0200645void close_shroot_lease_locked(struct cached_fid *cfid)
646{
647 if (cfid->has_lease) {
648 cfid->has_lease = false;
649 kref_put(&cfid->refcount, smb2_close_cached_fid);
650 }
651}
652
653void close_shroot_lease(struct cached_fid *cfid)
654{
655 mutex_lock(&cfid->fid_mutex);
656 close_shroot_lease_locked(cfid);
657 mutex_unlock(&cfid->fid_mutex);
658}
659
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000660void
661smb2_cached_lease_break(struct work_struct *work)
662{
663 struct cached_fid *cfid = container_of(work,
664 struct cached_fid, lease_break);
665
Olivier Deprez157378f2022-04-04 15:47:50 +0200666 close_shroot_lease(cfid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000667}
668
669/*
670 * Open the directory at the root of a share
671 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200672int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
673 struct cifs_sb_info *cifs_sb,
674 struct cached_fid **cfid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000675{
David Brazdil0f672f62019-12-10 10:32:29 +0000676 struct cifs_ses *ses = tcon->ses;
677 struct TCP_Server_Info *server = ses->server;
678 struct cifs_open_parms oparms;
679 struct smb2_create_rsp *o_rsp = NULL;
680 struct smb2_query_info_rsp *qi_rsp = NULL;
681 int resp_buftype[2];
682 struct smb_rqst rqst[2];
683 struct kvec rsp_iov[2];
684 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
685 struct kvec qi_iov[1];
686 int rc, flags = 0;
687 __le16 utf16_path = 0; /* Null - since an open of top of share */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000688 u8 oplock = SMB2_OPLOCK_LEVEL_II;
Olivier Deprez157378f2022-04-04 15:47:50 +0200689 struct cifs_fid *pfid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000690
691 mutex_lock(&tcon->crfid.fid_mutex);
692 if (tcon->crfid.is_valid) {
693 cifs_dbg(FYI, "found a cached root file handle\n");
Olivier Deprez157378f2022-04-04 15:47:50 +0200694 *cfid = &tcon->crfid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000695 kref_get(&tcon->crfid.refcount);
696 mutex_unlock(&tcon->crfid.fid_mutex);
697 return 0;
698 }
699
David Brazdil0f672f62019-12-10 10:32:29 +0000700 /*
701 * We do not hold the lock for the open because in case
702 * SMB2_open needs to reconnect, it will end up calling
703 * cifs_mark_open_files_invalid() which takes the lock again
704 * thus causing a deadlock
705 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000706
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000707 mutex_unlock(&tcon->crfid.fid_mutex);
David Brazdil0f672f62019-12-10 10:32:29 +0000708
709 if (smb3_encryption_required(tcon))
710 flags |= CIFS_TRANSFORM_REQ;
711
Olivier Deprez0e641232021-09-23 10:07:05 +0200712 if (!server->ops->new_lease_key)
713 return -EIO;
714
Olivier Deprez157378f2022-04-04 15:47:50 +0200715 pfid = tcon->crfid.fid;
Olivier Deprez0e641232021-09-23 10:07:05 +0200716 server->ops->new_lease_key(pfid);
717
David Brazdil0f672f62019-12-10 10:32:29 +0000718 memset(rqst, 0, sizeof(rqst));
719 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
720 memset(rsp_iov, 0, sizeof(rsp_iov));
721
722 /* Open */
723 memset(&open_iov, 0, sizeof(open_iov));
724 rqst[0].rq_iov = open_iov;
725 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
726
727 oparms.tcon = tcon;
Olivier Deprez157378f2022-04-04 15:47:50 +0200728 oparms.create_options = cifs_create_options(cifs_sb, 0);
David Brazdil0f672f62019-12-10 10:32:29 +0000729 oparms.desired_access = FILE_READ_ATTRIBUTES;
730 oparms.disposition = FILE_OPEN;
731 oparms.fid = pfid;
732 oparms.reconnect = false;
733
Olivier Deprez157378f2022-04-04 15:47:50 +0200734 rc = SMB2_open_init(tcon, server,
735 &rqst[0], &oplock, &oparms, &utf16_path);
David Brazdil0f672f62019-12-10 10:32:29 +0000736 if (rc)
737 goto oshr_free;
738 smb2_set_next_command(tcon, &rqst[0]);
739
740 memset(&qi_iov, 0, sizeof(qi_iov));
741 rqst[1].rq_iov = qi_iov;
742 rqst[1].rq_nvec = 1;
743
Olivier Deprez157378f2022-04-04 15:47:50 +0200744 rc = SMB2_query_info_init(tcon, server,
745 &rqst[1], COMPOUND_FID,
David Brazdil0f672f62019-12-10 10:32:29 +0000746 COMPOUND_FID, FILE_ALL_INFORMATION,
747 SMB2_O_INFO_FILE, 0,
748 sizeof(struct smb2_file_all_info) +
749 PATH_MAX * 2, 0, NULL);
750 if (rc)
751 goto oshr_free;
752
753 smb2_set_related(&rqst[1]);
754
Olivier Deprez157378f2022-04-04 15:47:50 +0200755 rc = compound_send_recv(xid, ses, server,
756 flags, 2, rqst,
David Brazdil0f672f62019-12-10 10:32:29 +0000757 resp_buftype, rsp_iov);
758 mutex_lock(&tcon->crfid.fid_mutex);
759
760 /*
761 * Now we need to check again as the cached root might have
762 * been successfully re-opened from a concurrent process
763 */
764
765 if (tcon->crfid.is_valid) {
766 /* work was already done */
767
768 /* stash fids for close() later */
769 struct cifs_fid fid = {
770 .persistent_fid = pfid->persistent_fid,
771 .volatile_fid = pfid->volatile_fid,
772 };
773
774 /*
775 * caller expects this func to set pfid to a valid
776 * cached root, so we copy the existing one and get a
777 * reference.
778 */
779 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
780 kref_get(&tcon->crfid.refcount);
781
782 mutex_unlock(&tcon->crfid.fid_mutex);
783
784 if (rc == 0) {
785 /* close extra handle outside of crit sec */
786 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
787 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200788 rc = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000789 goto oshr_free;
790 }
791
792 /* Cached root is still invalid, continue normaly */
793
794 if (rc) {
795 if (rc == -EREMCHG) {
796 tcon->need_reconnect = true;
Olivier Deprez157378f2022-04-04 15:47:50 +0200797 pr_warn_once("server share %s deleted\n",
798 tcon->treeName);
David Brazdil0f672f62019-12-10 10:32:29 +0000799 }
800 goto oshr_exit;
801 }
802
803 atomic_inc(&tcon->num_remote_opens);
804
805 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
806 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
807 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
808#ifdef CONFIG_CIFS_DEBUG2
809 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
810#endif /* CIFS_DEBUG2 */
811
812 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
813 tcon->crfid.tcon = tcon;
814 tcon->crfid.is_valid = true;
815 kref_init(&tcon->crfid.refcount);
816
817 /* BB TBD check to see if oplock level check can be removed below */
818 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
819 kref_get(&tcon->crfid.refcount);
Olivier Deprez157378f2022-04-04 15:47:50 +0200820 tcon->crfid.has_lease = true;
David Brazdil0f672f62019-12-10 10:32:29 +0000821 smb2_parse_contexts(server, o_rsp,
822 &oparms.fid->epoch,
Olivier Deprez157378f2022-04-04 15:47:50 +0200823 oparms.fid->lease_key, &oplock,
824 NULL, NULL);
David Brazdil0f672f62019-12-10 10:32:29 +0000825 } else
826 goto oshr_exit;
827
828 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
829 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
830 goto oshr_exit;
831 if (!smb2_validate_and_copy_iov(
832 le16_to_cpu(qi_rsp->OutputBufferOffset),
833 sizeof(struct smb2_file_all_info),
834 &rsp_iov[1], sizeof(struct smb2_file_all_info),
835 (char *)&tcon->crfid.file_all_info))
Olivier Deprez157378f2022-04-04 15:47:50 +0200836 tcon->crfid.file_all_info_is_valid = true;
David Brazdil0f672f62019-12-10 10:32:29 +0000837
838oshr_exit:
839 mutex_unlock(&tcon->crfid.fid_mutex);
840oshr_free:
841 SMB2_open_free(&rqst[0]);
842 SMB2_query_info_free(&rqst[1]);
843 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
844 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Olivier Deprez157378f2022-04-04 15:47:50 +0200845 if (rc == 0)
846 *cfid = &tcon->crfid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000847 return rc;
848}
849
850static void
Olivier Deprez157378f2022-04-04 15:47:50 +0200851smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
852 struct cifs_sb_info *cifs_sb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000853{
854 int rc;
855 __le16 srch_path = 0; /* Null - open root of share */
856 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
857 struct cifs_open_parms oparms;
858 struct cifs_fid fid;
859 bool no_cached_open = tcon->nohandlecache;
Olivier Deprez157378f2022-04-04 15:47:50 +0200860 struct cached_fid *cfid = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000861
862 oparms.tcon = tcon;
863 oparms.desired_access = FILE_READ_ATTRIBUTES;
864 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +0200865 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000866 oparms.fid = &fid;
867 oparms.reconnect = false;
868
Olivier Deprez157378f2022-04-04 15:47:50 +0200869 if (no_cached_open) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000870 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
Olivier Deprez157378f2022-04-04 15:47:50 +0200871 NULL, NULL);
872 } else {
873 rc = open_shroot(xid, tcon, cifs_sb, &cfid);
874 if (rc == 0)
875 memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
876 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000877 if (rc)
878 return;
879
880 SMB3_request_interfaces(xid, tcon);
881
882 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
883 FS_ATTRIBUTE_INFORMATION);
884 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
885 FS_DEVICE_INFORMATION);
886 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
887 FS_VOLUME_INFORMATION);
888 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
889 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
890 if (no_cached_open)
891 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
892 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200893 close_shroot(cfid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000894}
895
896static void
Olivier Deprez157378f2022-04-04 15:47:50 +0200897smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
898 struct cifs_sb_info *cifs_sb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000899{
900 int rc;
901 __le16 srch_path = 0; /* Null - open root of share */
902 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
903 struct cifs_open_parms oparms;
904 struct cifs_fid fid;
905
906 oparms.tcon = tcon;
907 oparms.desired_access = FILE_READ_ATTRIBUTES;
908 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +0200909 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000910 oparms.fid = &fid;
911 oparms.reconnect = false;
912
Olivier Deprez157378f2022-04-04 15:47:50 +0200913 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
914 NULL, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000915 if (rc)
916 return;
917
918 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
919 FS_ATTRIBUTE_INFORMATION);
920 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
921 FS_DEVICE_INFORMATION);
922 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000923}
924
925static int
926smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
927 struct cifs_sb_info *cifs_sb, const char *full_path)
928{
929 int rc;
930 __le16 *utf16_path;
931 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
932 struct cifs_open_parms oparms;
933 struct cifs_fid fid;
934
935 if ((*full_path == 0) && tcon->crfid.is_valid)
936 return 0;
937
938 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
939 if (!utf16_path)
940 return -ENOMEM;
941
942 oparms.tcon = tcon;
943 oparms.desired_access = FILE_READ_ATTRIBUTES;
944 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +0200945 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000946 oparms.fid = &fid;
947 oparms.reconnect = false;
948
Olivier Deprez157378f2022-04-04 15:47:50 +0200949 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
950 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951 if (rc) {
952 kfree(utf16_path);
953 return rc;
954 }
955
956 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
957 kfree(utf16_path);
958 return rc;
959}
960
961static int
962smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
963 struct cifs_sb_info *cifs_sb, const char *full_path,
964 u64 *uniqueid, FILE_ALL_INFO *data)
965{
966 *uniqueid = le64_to_cpu(data->IndexNumber);
967 return 0;
968}
969
970static int
971smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
972 struct cifs_fid *fid, FILE_ALL_INFO *data)
973{
974 int rc;
975 struct smb2_file_all_info *smb2_data;
976
977 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
978 GFP_KERNEL);
979 if (smb2_data == NULL)
980 return -ENOMEM;
981
982 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
983 smb2_data);
984 if (!rc)
985 move_smb2_info_to_cifs(data, smb2_data);
986 kfree(smb2_data);
987 return rc;
988}
989
990#ifdef CONFIG_CIFS_XATTR
991static ssize_t
992move_smb2_ea_to_cifs(char *dst, size_t dst_size,
993 struct smb2_file_full_ea_info *src, size_t src_size,
994 const unsigned char *ea_name)
995{
996 int rc = 0;
997 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
998 char *name, *value;
999 size_t buf_size = dst_size;
1000 size_t name_len, value_len, user_name_len;
1001
1002 while (src_size > 0) {
1003 name = &src->ea_data[0];
1004 name_len = (size_t)src->ea_name_length;
1005 value = &src->ea_data[src->ea_name_length + 1];
1006 value_len = (size_t)le16_to_cpu(src->ea_value_length);
1007
David Brazdil0f672f62019-12-10 10:32:29 +00001008 if (name_len == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001009 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001010
1011 if (src_size < 8 + name_len + 1 + value_len) {
1012 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1013 rc = -EIO;
1014 goto out;
1015 }
1016
1017 if (ea_name) {
1018 if (ea_name_len == name_len &&
1019 memcmp(ea_name, name, name_len) == 0) {
1020 rc = value_len;
1021 if (dst_size == 0)
1022 goto out;
1023 if (dst_size < value_len) {
1024 rc = -ERANGE;
1025 goto out;
1026 }
1027 memcpy(dst, value, value_len);
1028 goto out;
1029 }
1030 } else {
1031 /* 'user.' plus a terminating null */
1032 user_name_len = 5 + 1 + name_len;
1033
1034 if (buf_size == 0) {
1035 /* skip copy - calc size only */
1036 rc += user_name_len;
1037 } else if (dst_size >= user_name_len) {
1038 dst_size -= user_name_len;
1039 memcpy(dst, "user.", 5);
1040 dst += 5;
1041 memcpy(dst, src->ea_data, name_len);
1042 dst += name_len;
1043 *dst = 0;
1044 ++dst;
1045 rc += user_name_len;
1046 } else {
1047 /* stop before overrun buffer */
1048 rc = -ERANGE;
1049 break;
1050 }
1051 }
1052
1053 if (!src->next_entry_offset)
1054 break;
1055
1056 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1057 /* stop before overrun buffer */
1058 rc = -ERANGE;
1059 break;
1060 }
1061 src_size -= le32_to_cpu(src->next_entry_offset);
1062 src = (void *)((char *)src +
1063 le32_to_cpu(src->next_entry_offset));
1064 }
1065
1066 /* didn't find the named attribute */
1067 if (ea_name)
1068 rc = -ENODATA;
1069
1070out:
1071 return (ssize_t)rc;
1072}
1073
1074static ssize_t
1075smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1076 const unsigned char *path, const unsigned char *ea_name,
1077 char *ea_data, size_t buf_size,
1078 struct cifs_sb_info *cifs_sb)
1079{
1080 int rc;
1081 __le16 *utf16_path;
David Brazdil0f672f62019-12-10 10:32:29 +00001082 struct kvec rsp_iov = {NULL, 0};
1083 int buftype = CIFS_NO_BUFFER;
1084 struct smb2_query_info_rsp *rsp;
1085 struct smb2_file_full_ea_info *info = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001086
1087 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1088 if (!utf16_path)
1089 return -ENOMEM;
1090
David Brazdil0f672f62019-12-10 10:32:29 +00001091 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1092 FILE_READ_EA,
1093 FILE_FULL_EA_INFORMATION,
1094 SMB2_O_INFO_FILE,
1095 CIFSMaxBufSize -
1096 MAX_SMB2_CREATE_RESPONSE_SIZE -
1097 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1098 &rsp_iov, &buftype, cifs_sb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001099 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00001100 /*
1101 * If ea_name is NULL (listxattr) and there are no EAs,
1102 * return 0 as it's not an error. Otherwise, the specified
1103 * ea_name was not found.
1104 */
1105 if (!ea_name && rc == -ENODATA)
1106 rc = 0;
1107 goto qeas_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001108 }
1109
David Brazdil0f672f62019-12-10 10:32:29 +00001110 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1111 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1112 le32_to_cpu(rsp->OutputBufferLength),
1113 &rsp_iov,
1114 sizeof(struct smb2_file_full_ea_info));
1115 if (rc)
1116 goto qeas_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001117
David Brazdil0f672f62019-12-10 10:32:29 +00001118 info = (struct smb2_file_full_ea_info *)(
1119 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1120 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1121 le32_to_cpu(rsp->OutputBufferLength), ea_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001122
David Brazdil0f672f62019-12-10 10:32:29 +00001123 qeas_exit:
1124 kfree(utf16_path);
1125 free_rsp_buf(buftype, rsp_iov.iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001126 return rc;
1127}
1128
1129
1130static int
1131smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1132 const char *path, const char *ea_name, const void *ea_value,
1133 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1134 struct cifs_sb_info *cifs_sb)
1135{
David Brazdil0f672f62019-12-10 10:32:29 +00001136 struct cifs_ses *ses = tcon->ses;
Olivier Deprez157378f2022-04-04 15:47:50 +02001137 struct TCP_Server_Info *server = cifs_pick_channel(ses);
David Brazdil0f672f62019-12-10 10:32:29 +00001138 __le16 *utf16_path = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001139 int ea_name_len = strlen(ea_name);
Olivier Deprez157378f2022-04-04 15:47:50 +02001140 int flags = CIFS_CP_CREATE_CLOSE_OP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001141 int len;
David Brazdil0f672f62019-12-10 10:32:29 +00001142 struct smb_rqst rqst[3];
1143 int resp_buftype[3];
1144 struct kvec rsp_iov[3];
1145 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1146 struct cifs_open_parms oparms;
1147 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1148 struct cifs_fid fid;
1149 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1150 unsigned int size[1];
1151 void *data[1];
1152 struct smb2_file_full_ea_info *ea = NULL;
1153 struct kvec close_iov[1];
Olivier Deprez0e641232021-09-23 10:07:05 +02001154 struct smb2_query_info_rsp *rsp;
1155 int rc, used_len = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001156
1157 if (smb3_encryption_required(tcon))
1158 flags |= CIFS_TRANSFORM_REQ;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001159
1160 if (ea_name_len > 255)
1161 return -EINVAL;
1162
1163 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1164 if (!utf16_path)
1165 return -ENOMEM;
1166
David Brazdil0f672f62019-12-10 10:32:29 +00001167 memset(rqst, 0, sizeof(rqst));
1168 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1169 memset(rsp_iov, 0, sizeof(rsp_iov));
1170
1171 if (ses->server->ops->query_all_EAs) {
1172 if (!ea_value) {
1173 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1174 ea_name, NULL, 0,
1175 cifs_sb);
1176 if (rc == -ENODATA)
1177 goto sea_exit;
Olivier Deprez0e641232021-09-23 10:07:05 +02001178 } else {
1179 /* If we are adding a attribute we should first check
1180 * if there will be enough space available to store
1181 * the new EA. If not we should not add it since we
1182 * would not be able to even read the EAs back.
1183 */
1184 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1185 FILE_READ_EA,
1186 FILE_FULL_EA_INFORMATION,
1187 SMB2_O_INFO_FILE,
1188 CIFSMaxBufSize -
1189 MAX_SMB2_CREATE_RESPONSE_SIZE -
1190 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1191 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1192 if (rc == 0) {
1193 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1194 used_len = le32_to_cpu(rsp->OutputBufferLength);
1195 }
1196 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1197 resp_buftype[1] = CIFS_NO_BUFFER;
1198 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1199 rc = 0;
1200
1201 /* Use a fudge factor of 256 bytes in case we collide
1202 * with a different set_EAs command.
1203 */
1204 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1205 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1206 used_len + ea_name_len + ea_value_len + 1) {
1207 rc = -ENOSPC;
1208 goto sea_exit;
1209 }
David Brazdil0f672f62019-12-10 10:32:29 +00001210 }
1211 }
1212
1213 /* Open */
1214 memset(&open_iov, 0, sizeof(open_iov));
1215 rqst[0].rq_iov = open_iov;
1216 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1217
1218 memset(&oparms, 0, sizeof(oparms));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001219 oparms.tcon = tcon;
1220 oparms.desired_access = FILE_WRITE_EA;
1221 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02001222 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001223 oparms.fid = &fid;
1224 oparms.reconnect = false;
1225
Olivier Deprez157378f2022-04-04 15:47:50 +02001226 rc = SMB2_open_init(tcon, server,
1227 &rqst[0], &oplock, &oparms, utf16_path);
David Brazdil0f672f62019-12-10 10:32:29 +00001228 if (rc)
1229 goto sea_exit;
1230 smb2_set_next_command(tcon, &rqst[0]);
1231
1232
1233 /* Set Info */
1234 memset(&si_iov, 0, sizeof(si_iov));
1235 rqst[1].rq_iov = si_iov;
1236 rqst[1].rq_nvec = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001237
Olivier Deprez0e641232021-09-23 10:07:05 +02001238 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001239 ea = kzalloc(len, GFP_KERNEL);
1240 if (ea == NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +00001241 rc = -ENOMEM;
1242 goto sea_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001243 }
1244
1245 ea->ea_name_length = ea_name_len;
1246 ea->ea_value_length = cpu_to_le16(ea_value_len);
1247 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1248 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1249
David Brazdil0f672f62019-12-10 10:32:29 +00001250 size[0] = len;
1251 data[0] = ea;
1252
Olivier Deprez157378f2022-04-04 15:47:50 +02001253 rc = SMB2_set_info_init(tcon, server,
1254 &rqst[1], COMPOUND_FID,
David Brazdil0f672f62019-12-10 10:32:29 +00001255 COMPOUND_FID, current->tgid,
1256 FILE_FULL_EA_INFORMATION,
1257 SMB2_O_INFO_FILE, 0, data, size);
1258 smb2_set_next_command(tcon, &rqst[1]);
1259 smb2_set_related(&rqst[1]);
1260
1261
1262 /* Close */
1263 memset(&close_iov, 0, sizeof(close_iov));
1264 rqst[2].rq_iov = close_iov;
1265 rqst[2].rq_nvec = 1;
Olivier Deprez157378f2022-04-04 15:47:50 +02001266 rc = SMB2_close_init(tcon, server,
1267 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
David Brazdil0f672f62019-12-10 10:32:29 +00001268 smb2_set_related(&rqst[2]);
1269
Olivier Deprez157378f2022-04-04 15:47:50 +02001270 rc = compound_send_recv(xid, ses, server,
1271 flags, 3, rqst,
David Brazdil0f672f62019-12-10 10:32:29 +00001272 resp_buftype, rsp_iov);
1273 /* no need to bump num_remote_opens because handle immediately closed */
1274
1275 sea_exit:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001276 kfree(ea);
David Brazdil0f672f62019-12-10 10:32:29 +00001277 kfree(utf16_path);
1278 SMB2_open_free(&rqst[0]);
1279 SMB2_set_info_free(&rqst[1]);
1280 SMB2_close_free(&rqst[2]);
1281 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1282 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1283 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001284 return rc;
1285}
1286#endif
1287
1288static bool
1289smb2_can_echo(struct TCP_Server_Info *server)
1290{
1291 return server->echoes;
1292}
1293
1294static void
1295smb2_clear_stats(struct cifs_tcon *tcon)
1296{
1297 int i;
David Brazdil0f672f62019-12-10 10:32:29 +00001298
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001299 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1300 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1301 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1302 }
1303}
1304
1305static void
1306smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1307{
1308 seq_puts(m, "\n\tShare Capabilities:");
1309 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1310 seq_puts(m, " DFS,");
1311 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1312 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1313 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1314 seq_puts(m, " SCALEOUT,");
1315 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1316 seq_puts(m, " CLUSTER,");
1317 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1318 seq_puts(m, " ASYMMETRIC,");
1319 if (tcon->capabilities == 0)
1320 seq_puts(m, " None");
1321 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1322 seq_puts(m, " Aligned,");
1323 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1324 seq_puts(m, " Partition Aligned,");
1325 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1326 seq_puts(m, " SSD,");
1327 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1328 seq_puts(m, " TRIM-support,");
1329
1330 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1331 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1332 if (tcon->perf_sector_size)
1333 seq_printf(m, "\tOptimal sector size: 0x%x",
1334 tcon->perf_sector_size);
1335 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1336}
1337
1338static void
1339smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1340{
1341 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1342 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1343
1344 /*
1345 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1346 * totals (requests sent) since those SMBs are per-session not per tcon
1347 */
1348 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1349 (long long)(tcon->bytes_read),
1350 (long long)(tcon->bytes_written));
David Brazdil0f672f62019-12-10 10:32:29 +00001351 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1352 atomic_read(&tcon->num_local_opens),
1353 atomic_read(&tcon->num_remote_opens));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001354 seq_printf(m, "\nTreeConnects: %d total %d failed",
1355 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1356 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1357 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1358 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1359 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1360 seq_printf(m, "\nCreates: %d total %d failed",
1361 atomic_read(&sent[SMB2_CREATE_HE]),
1362 atomic_read(&failed[SMB2_CREATE_HE]));
1363 seq_printf(m, "\nCloses: %d total %d failed",
1364 atomic_read(&sent[SMB2_CLOSE_HE]),
1365 atomic_read(&failed[SMB2_CLOSE_HE]));
1366 seq_printf(m, "\nFlushes: %d total %d failed",
1367 atomic_read(&sent[SMB2_FLUSH_HE]),
1368 atomic_read(&failed[SMB2_FLUSH_HE]));
1369 seq_printf(m, "\nReads: %d total %d failed",
1370 atomic_read(&sent[SMB2_READ_HE]),
1371 atomic_read(&failed[SMB2_READ_HE]));
1372 seq_printf(m, "\nWrites: %d total %d failed",
1373 atomic_read(&sent[SMB2_WRITE_HE]),
1374 atomic_read(&failed[SMB2_WRITE_HE]));
1375 seq_printf(m, "\nLocks: %d total %d failed",
1376 atomic_read(&sent[SMB2_LOCK_HE]),
1377 atomic_read(&failed[SMB2_LOCK_HE]));
1378 seq_printf(m, "\nIOCTLs: %d total %d failed",
1379 atomic_read(&sent[SMB2_IOCTL_HE]),
1380 atomic_read(&failed[SMB2_IOCTL_HE]));
1381 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1382 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1383 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1384 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1385 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1386 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1387 seq_printf(m, "\nQueryInfos: %d total %d failed",
1388 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1389 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1390 seq_printf(m, "\nSetInfos: %d total %d failed",
1391 atomic_read(&sent[SMB2_SET_INFO_HE]),
1392 atomic_read(&failed[SMB2_SET_INFO_HE]));
1393 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1394 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1395 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1396}
1397
1398static void
1399smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1400{
1401 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1402 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1403
1404 cfile->fid.persistent_fid = fid->persistent_fid;
1405 cfile->fid.volatile_fid = fid->volatile_fid;
Olivier Deprez0e641232021-09-23 10:07:05 +02001406 cfile->fid.access = fid->access;
David Brazdil0f672f62019-12-10 10:32:29 +00001407#ifdef CONFIG_CIFS_DEBUG2
1408 cfile->fid.mid = fid->mid;
1409#endif /* CIFS_DEBUG2 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001410 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1411 &fid->purge_cache);
1412 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1413 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1414}
1415
1416static void
1417smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1418 struct cifs_fid *fid)
1419{
1420 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1421}
1422
Olivier Deprez157378f2022-04-04 15:47:50 +02001423static void
1424smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1425 struct cifsFileInfo *cfile)
1426{
1427 struct smb2_file_network_open_info file_inf;
1428 struct inode *inode;
1429 int rc;
1430
1431 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1432 cfile->fid.volatile_fid, &file_inf);
1433 if (rc)
1434 return;
1435
1436 inode = d_inode(cfile->dentry);
1437
1438 spin_lock(&inode->i_lock);
1439 CIFS_I(inode)->time = jiffies;
1440
1441 /* Creation time should not need to be updated on close */
1442 if (file_inf.LastWriteTime)
1443 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1444 if (file_inf.ChangeTime)
1445 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1446 if (file_inf.LastAccessTime)
1447 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1448
1449 /*
1450 * i_blocks is not related to (i_size / i_blksize),
1451 * but instead 512 byte (2**9) size is required for
1452 * calculating num blocks.
1453 */
1454 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1455 inode->i_blocks =
1456 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1457
1458 /* End of file and Attributes should not have to be updated on close */
1459 spin_unlock(&inode->i_lock);
1460}
1461
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001462static int
1463SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1464 u64 persistent_fid, u64 volatile_fid,
1465 struct copychunk_ioctl *pcchunk)
1466{
1467 int rc;
1468 unsigned int ret_data_len;
1469 struct resume_key_req *res_key;
1470
1471 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1472 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
David Brazdil0f672f62019-12-10 10:32:29 +00001473 NULL, 0 /* no input */, CIFSMaxBufSize,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001474 (char **)&res_key, &ret_data_len);
1475
1476 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00001477 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001478 goto req_res_key_exit;
1479 }
1480 if (ret_data_len < sizeof(struct resume_key_req)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001481 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001482 rc = -EINVAL;
1483 goto req_res_key_exit;
1484 }
1485 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1486
1487req_res_key_exit:
1488 kfree(res_key);
1489 return rc;
1490}
1491
Olivier Deprez157378f2022-04-04 15:47:50 +02001492struct iqi_vars {
David Brazdil0f672f62019-12-10 10:32:29 +00001493 struct smb_rqst rqst[3];
David Brazdil0f672f62019-12-10 10:32:29 +00001494 struct kvec rsp_iov[3];
1495 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
David Brazdil0f672f62019-12-10 10:32:29 +00001496 struct kvec qi_iov[1];
1497 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1498 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1499 struct kvec close_iov[1];
Olivier Deprez157378f2022-04-04 15:47:50 +02001500};
1501
1502static int
1503smb2_ioctl_query_info(const unsigned int xid,
1504 struct cifs_tcon *tcon,
1505 struct cifs_sb_info *cifs_sb,
1506 __le16 *path, int is_dir,
1507 unsigned long p)
1508{
1509 struct iqi_vars *vars;
1510 struct smb_rqst *rqst;
1511 struct kvec *rsp_iov;
1512 struct cifs_ses *ses = tcon->ses;
1513 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1514 char __user *arg = (char __user *)p;
1515 struct smb_query_info qi;
1516 struct smb_query_info __user *pqi;
1517 int rc = 0;
1518 int flags = CIFS_CP_CREATE_CLOSE_OP;
1519 struct smb2_query_info_rsp *qi_rsp = NULL;
1520 struct smb2_ioctl_rsp *io_rsp = NULL;
1521 void *buffer = NULL;
1522 int resp_buftype[3];
1523 struct cifs_open_parms oparms;
1524 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1525 struct cifs_fid fid;
David Brazdil0f672f62019-12-10 10:32:29 +00001526 unsigned int size[2];
1527 void *data[2];
Olivier Deprez157378f2022-04-04 15:47:50 +02001528 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
David Brazdil0f672f62019-12-10 10:32:29 +00001529
Olivier Deprez157378f2022-04-04 15:47:50 +02001530 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1531 if (vars == NULL)
1532 return -ENOMEM;
1533 rqst = &vars->rqst[0];
1534 rsp_iov = &vars->rsp_iov[0];
1535
David Brazdil0f672f62019-12-10 10:32:29 +00001536 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
David Brazdil0f672f62019-12-10 10:32:29 +00001537
1538 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
Olivier Deprez157378f2022-04-04 15:47:50 +02001539 goto e_fault;
David Brazdil0f672f62019-12-10 10:32:29 +00001540
Olivier Deprez157378f2022-04-04 15:47:50 +02001541 if (qi.output_buffer_length > 1024) {
1542 kfree(vars);
David Brazdil0f672f62019-12-10 10:32:29 +00001543 return -EINVAL;
Olivier Deprez157378f2022-04-04 15:47:50 +02001544 }
David Brazdil0f672f62019-12-10 10:32:29 +00001545
Olivier Deprez157378f2022-04-04 15:47:50 +02001546 if (!ses || !server) {
1547 kfree(vars);
David Brazdil0f672f62019-12-10 10:32:29 +00001548 return -EIO;
Olivier Deprez157378f2022-04-04 15:47:50 +02001549 }
David Brazdil0f672f62019-12-10 10:32:29 +00001550
1551 if (smb3_encryption_required(tcon))
1552 flags |= CIFS_TRANSFORM_REQ;
1553
Olivier Deprez157378f2022-04-04 15:47:50 +02001554 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1555 qi.output_buffer_length);
1556 if (IS_ERR(buffer)) {
1557 kfree(vars);
1558 return PTR_ERR(buffer);
David Brazdil0f672f62019-12-10 10:32:29 +00001559 }
1560
1561 /* Open */
Olivier Deprez157378f2022-04-04 15:47:50 +02001562 rqst[0].rq_iov = &vars->open_iov[0];
David Brazdil0f672f62019-12-10 10:32:29 +00001563 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1564
1565 memset(&oparms, 0, sizeof(oparms));
1566 oparms.tcon = tcon;
1567 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02001568 oparms.create_options = cifs_create_options(cifs_sb, create_options);
David Brazdil0f672f62019-12-10 10:32:29 +00001569 oparms.fid = &fid;
1570 oparms.reconnect = false;
1571
1572 if (qi.flags & PASSTHRU_FSCTL) {
1573 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1574 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1575 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1576 break;
1577 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1578 oparms.desired_access = GENERIC_ALL;
1579 break;
1580 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1581 oparms.desired_access = GENERIC_READ;
1582 break;
1583 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1584 oparms.desired_access = GENERIC_WRITE;
1585 break;
1586 }
1587 } else if (qi.flags & PASSTHRU_SET_INFO) {
1588 oparms.desired_access = GENERIC_WRITE;
1589 } else {
1590 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1591 }
1592
Olivier Deprez157378f2022-04-04 15:47:50 +02001593 rc = SMB2_open_init(tcon, server,
1594 &rqst[0], &oplock, &oparms, path);
David Brazdil0f672f62019-12-10 10:32:29 +00001595 if (rc)
1596 goto iqinf_exit;
1597 smb2_set_next_command(tcon, &rqst[0]);
1598
1599 /* Query */
1600 if (qi.flags & PASSTHRU_FSCTL) {
1601 /* Can eventually relax perm check since server enforces too */
1602 if (!capable(CAP_SYS_ADMIN))
1603 rc = -EPERM;
1604 else {
Olivier Deprez157378f2022-04-04 15:47:50 +02001605 rqst[1].rq_iov = &vars->io_iov[0];
David Brazdil0f672f62019-12-10 10:32:29 +00001606 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1607
Olivier Deprez157378f2022-04-04 15:47:50 +02001608 rc = SMB2_ioctl_init(tcon, server,
1609 &rqst[1],
David Brazdil0f672f62019-12-10 10:32:29 +00001610 COMPOUND_FID, COMPOUND_FID,
1611 qi.info_type, true, buffer,
1612 qi.output_buffer_length,
Olivier Deprez0e641232021-09-23 10:07:05 +02001613 CIFSMaxBufSize -
1614 MAX_SMB2_CREATE_RESPONSE_SIZE -
1615 MAX_SMB2_CLOSE_RESPONSE_SIZE);
David Brazdil0f672f62019-12-10 10:32:29 +00001616 }
1617 } else if (qi.flags == PASSTHRU_SET_INFO) {
1618 /* Can eventually relax perm check since server enforces too */
1619 if (!capable(CAP_SYS_ADMIN))
1620 rc = -EPERM;
1621 else {
Olivier Deprez157378f2022-04-04 15:47:50 +02001622 rqst[1].rq_iov = &vars->si_iov[0];
David Brazdil0f672f62019-12-10 10:32:29 +00001623 rqst[1].rq_nvec = 1;
1624
1625 size[0] = 8;
1626 data[0] = buffer;
1627
Olivier Deprez157378f2022-04-04 15:47:50 +02001628 rc = SMB2_set_info_init(tcon, server,
1629 &rqst[1],
David Brazdil0f672f62019-12-10 10:32:29 +00001630 COMPOUND_FID, COMPOUND_FID,
1631 current->tgid,
1632 FILE_END_OF_FILE_INFORMATION,
1633 SMB2_O_INFO_FILE, 0, data, size);
1634 }
1635 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001636 rqst[1].rq_iov = &vars->qi_iov[0];
David Brazdil0f672f62019-12-10 10:32:29 +00001637 rqst[1].rq_nvec = 1;
1638
Olivier Deprez157378f2022-04-04 15:47:50 +02001639 rc = SMB2_query_info_init(tcon, server,
1640 &rqst[1], COMPOUND_FID,
David Brazdil0f672f62019-12-10 10:32:29 +00001641 COMPOUND_FID, qi.file_info_class,
1642 qi.info_type, qi.additional_information,
1643 qi.input_buffer_length,
1644 qi.output_buffer_length, buffer);
1645 } else { /* unknown flags */
Olivier Deprez157378f2022-04-04 15:47:50 +02001646 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1647 qi.flags);
David Brazdil0f672f62019-12-10 10:32:29 +00001648 rc = -EINVAL;
1649 }
1650
1651 if (rc)
1652 goto iqinf_exit;
1653 smb2_set_next_command(tcon, &rqst[1]);
1654 smb2_set_related(&rqst[1]);
1655
1656 /* Close */
Olivier Deprez157378f2022-04-04 15:47:50 +02001657 rqst[2].rq_iov = &vars->close_iov[0];
David Brazdil0f672f62019-12-10 10:32:29 +00001658 rqst[2].rq_nvec = 1;
1659
Olivier Deprez157378f2022-04-04 15:47:50 +02001660 rc = SMB2_close_init(tcon, server,
1661 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
David Brazdil0f672f62019-12-10 10:32:29 +00001662 if (rc)
1663 goto iqinf_exit;
1664 smb2_set_related(&rqst[2]);
1665
Olivier Deprez157378f2022-04-04 15:47:50 +02001666 rc = compound_send_recv(xid, ses, server,
1667 flags, 3, rqst,
David Brazdil0f672f62019-12-10 10:32:29 +00001668 resp_buftype, rsp_iov);
1669 if (rc)
1670 goto iqinf_exit;
1671
1672 /* No need to bump num_remote_opens since handle immediately closed */
1673 if (qi.flags & PASSTHRU_FSCTL) {
1674 pqi = (struct smb_query_info __user *)arg;
1675 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1676 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1677 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1678 if (qi.input_buffer_length > 0 &&
Olivier Deprez0e641232021-09-23 10:07:05 +02001679 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1680 > rsp_iov[1].iov_len)
1681 goto e_fault;
1682
1683 if (copy_to_user(&pqi->input_buffer_length,
1684 &qi.input_buffer_length,
1685 sizeof(qi.input_buffer_length)))
1686 goto e_fault;
1687
David Brazdil0f672f62019-12-10 10:32:29 +00001688 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1689 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
Olivier Deprez0e641232021-09-23 10:07:05 +02001690 qi.input_buffer_length))
1691 goto e_fault;
David Brazdil0f672f62019-12-10 10:32:29 +00001692 } else {
1693 pqi = (struct smb_query_info __user *)arg;
1694 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1695 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1696 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
Olivier Deprez0e641232021-09-23 10:07:05 +02001697 if (copy_to_user(&pqi->input_buffer_length,
1698 &qi.input_buffer_length,
1699 sizeof(qi.input_buffer_length)))
1700 goto e_fault;
1701
1702 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1703 qi.input_buffer_length))
1704 goto e_fault;
David Brazdil0f672f62019-12-10 10:32:29 +00001705 }
1706
1707 iqinf_exit:
Olivier Deprez157378f2022-04-04 15:47:50 +02001708 cifs_small_buf_release(rqst[0].rq_iov[0].iov_base);
1709 cifs_small_buf_release(rqst[1].rq_iov[0].iov_base);
1710 cifs_small_buf_release(rqst[2].rq_iov[0].iov_base);
David Brazdil0f672f62019-12-10 10:32:29 +00001711 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1712 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1713 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Olivier Deprez157378f2022-04-04 15:47:50 +02001714 kfree(vars);
1715 kfree(buffer);
David Brazdil0f672f62019-12-10 10:32:29 +00001716 return rc;
Olivier Deprez0e641232021-09-23 10:07:05 +02001717
1718e_fault:
1719 rc = -EFAULT;
1720 goto iqinf_exit;
David Brazdil0f672f62019-12-10 10:32:29 +00001721}
1722
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001723static ssize_t
1724smb2_copychunk_range(const unsigned int xid,
1725 struct cifsFileInfo *srcfile,
1726 struct cifsFileInfo *trgtfile, u64 src_off,
1727 u64 len, u64 dest_off)
1728{
1729 int rc;
1730 unsigned int ret_data_len;
1731 struct copychunk_ioctl *pcchunk;
1732 struct copychunk_ioctl_rsp *retbuf = NULL;
1733 struct cifs_tcon *tcon;
1734 int chunks_copied = 0;
1735 bool chunk_sizes_updated = false;
1736 ssize_t bytes_written, total_bytes_written = 0;
1737
1738 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1739
1740 if (pcchunk == NULL)
1741 return -ENOMEM;
1742
David Brazdil0f672f62019-12-10 10:32:29 +00001743 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001744 /* Request a key from the server to identify the source of the copy */
1745 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1746 srcfile->fid.persistent_fid,
1747 srcfile->fid.volatile_fid, pcchunk);
1748
1749 /* Note: request_res_key sets res_key null only if rc !=0 */
1750 if (rc)
1751 goto cchunk_out;
1752
1753 /* For now array only one chunk long, will make more flexible later */
1754 pcchunk->ChunkCount = cpu_to_le32(1);
1755 pcchunk->Reserved = 0;
1756 pcchunk->Reserved2 = 0;
1757
1758 tcon = tlink_tcon(trgtfile->tlink);
1759
1760 while (len > 0) {
1761 pcchunk->SourceOffset = cpu_to_le64(src_off);
1762 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1763 pcchunk->Length =
1764 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1765
1766 /* Request server copy to target from src identified by key */
Olivier Deprez0e641232021-09-23 10:07:05 +02001767 kfree(retbuf);
1768 retbuf = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001769 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1770 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1771 true /* is_fsctl */, (char *)pcchunk,
David Brazdil0f672f62019-12-10 10:32:29 +00001772 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1773 (char **)&retbuf, &ret_data_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001774 if (rc == 0) {
1775 if (ret_data_len !=
1776 sizeof(struct copychunk_ioctl_rsp)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001777 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001778 rc = -EIO;
1779 goto cchunk_out;
1780 }
1781 if (retbuf->TotalBytesWritten == 0) {
1782 cifs_dbg(FYI, "no bytes copied\n");
1783 rc = -EIO;
1784 goto cchunk_out;
1785 }
1786 /*
1787 * Check if server claimed to write more than we asked
1788 */
1789 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1790 le32_to_cpu(pcchunk->Length)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001791 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001792 rc = -EIO;
1793 goto cchunk_out;
1794 }
1795 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001796 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001797 rc = -EIO;
1798 goto cchunk_out;
1799 }
1800 chunks_copied++;
1801
1802 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1803 src_off += bytes_written;
1804 dest_off += bytes_written;
1805 len -= bytes_written;
1806 total_bytes_written += bytes_written;
1807
1808 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1809 le32_to_cpu(retbuf->ChunksWritten),
1810 le32_to_cpu(retbuf->ChunkBytesWritten),
1811 bytes_written);
1812 } else if (rc == -EINVAL) {
1813 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1814 goto cchunk_out;
1815
1816 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1817 le32_to_cpu(retbuf->ChunksWritten),
1818 le32_to_cpu(retbuf->ChunkBytesWritten),
1819 le32_to_cpu(retbuf->TotalBytesWritten));
1820
1821 /*
1822 * Check if this is the first request using these sizes,
1823 * (ie check if copy succeed once with original sizes
1824 * and check if the server gave us different sizes after
1825 * we already updated max sizes on previous request).
1826 * if not then why is the server returning an error now
1827 */
1828 if ((chunks_copied != 0) || chunk_sizes_updated)
1829 goto cchunk_out;
1830
1831 /* Check that server is not asking us to grow size */
1832 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1833 tcon->max_bytes_chunk)
1834 tcon->max_bytes_chunk =
1835 le32_to_cpu(retbuf->ChunkBytesWritten);
1836 else
1837 goto cchunk_out; /* server gave us bogus size */
1838
1839 /* No need to change MaxChunks since already set to 1 */
1840 chunk_sizes_updated = true;
1841 } else
1842 goto cchunk_out;
1843 }
1844
1845cchunk_out:
1846 kfree(pcchunk);
1847 kfree(retbuf);
1848 if (rc)
1849 return rc;
1850 else
1851 return total_bytes_written;
1852}
1853
1854static int
1855smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1856 struct cifs_fid *fid)
1857{
1858 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1859}
1860
1861static unsigned int
1862smb2_read_data_offset(char *buf)
1863{
1864 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
David Brazdil0f672f62019-12-10 10:32:29 +00001865
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001866 return rsp->DataOffset;
1867}
1868
1869static unsigned int
1870smb2_read_data_length(char *buf, bool in_remaining)
1871{
1872 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1873
1874 if (in_remaining)
1875 return le32_to_cpu(rsp->DataRemaining);
1876
1877 return le32_to_cpu(rsp->DataLength);
1878}
1879
1880
1881static int
1882smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1883 struct cifs_io_parms *parms, unsigned int *bytes_read,
1884 char **buf, int *buf_type)
1885{
1886 parms->persistent_fid = pfid->persistent_fid;
1887 parms->volatile_fid = pfid->volatile_fid;
1888 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1889}
1890
1891static int
1892smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1893 struct cifs_io_parms *parms, unsigned int *written,
1894 struct kvec *iov, unsigned long nr_segs)
1895{
1896
1897 parms->persistent_fid = pfid->persistent_fid;
1898 parms->volatile_fid = pfid->volatile_fid;
1899 return SMB2_write(xid, parms, written, iov, nr_segs);
1900}
1901
1902/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1903static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1904 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1905{
1906 struct cifsInodeInfo *cifsi;
1907 int rc;
1908
1909 cifsi = CIFS_I(inode);
1910
1911 /* if file already sparse don't bother setting sparse again */
1912 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1913 return true; /* already sparse */
1914
1915 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1916 return true; /* already not sparse */
1917
1918 /*
1919 * Can't check for sparse support on share the usual way via the
1920 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1921 * since Samba server doesn't set the flag on the share, yet
1922 * supports the set sparse FSCTL and returns sparse correctly
1923 * in the file attributes. If we fail setting sparse though we
1924 * mark that server does not support sparse files for this share
1925 * to avoid repeatedly sending the unsupported fsctl to server
1926 * if the file is repeatedly extended.
1927 */
1928 if (tcon->broken_sparse_sup)
1929 return false;
1930
1931 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1932 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1933 true /* is_fctl */,
David Brazdil0f672f62019-12-10 10:32:29 +00001934 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001935 if (rc) {
1936 tcon->broken_sparse_sup = true;
1937 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1938 return false;
1939 }
1940
1941 if (setsparse)
1942 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1943 else
1944 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1945
1946 return true;
1947}
1948
1949static int
1950smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1951 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1952{
1953 __le64 eof = cpu_to_le64(size);
1954 struct inode *inode;
1955
1956 /*
1957 * If extending file more than one page make sparse. Many Linux fs
1958 * make files sparse by default when extending via ftruncate
1959 */
1960 inode = d_inode(cfile->dentry);
1961
1962 if (!set_alloc && (size > inode->i_size + 8192)) {
1963 __u8 set_sparse = 1;
1964
1965 /* whether set sparse succeeds or not, extend the file */
1966 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1967 }
1968
1969 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
David Brazdil0f672f62019-12-10 10:32:29 +00001970 cfile->fid.volatile_fid, cfile->pid, &eof);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001971}
1972
1973static int
1974smb2_duplicate_extents(const unsigned int xid,
1975 struct cifsFileInfo *srcfile,
1976 struct cifsFileInfo *trgtfile, u64 src_off,
1977 u64 len, u64 dest_off)
1978{
1979 int rc;
1980 unsigned int ret_data_len;
Olivier Deprez157378f2022-04-04 15:47:50 +02001981 struct inode *inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001982 struct duplicate_extents_to_file dup_ext_buf;
1983 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1984
1985 /* server fileays advertise duplicate extent support with this flag */
1986 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1987 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1988 return -EOPNOTSUPP;
1989
1990 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1991 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1992 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1993 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1994 dup_ext_buf.ByteCount = cpu_to_le64(len);
David Brazdil0f672f62019-12-10 10:32:29 +00001995 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001996 src_off, dest_off, len);
1997
Olivier Deprez157378f2022-04-04 15:47:50 +02001998 inode = d_inode(trgtfile->dentry);
1999 if (inode->i_size < dest_off + len) {
2000 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2001 if (rc)
2002 goto duplicate_extents_out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002003
Olivier Deprez157378f2022-04-04 15:47:50 +02002004 /*
2005 * Although also could set plausible allocation size (i_blocks)
2006 * here in addition to setting the file size, in reflink
2007 * it is likely that the target file is sparse. Its allocation
2008 * size will be queried on next revalidate, but it is important
2009 * to make sure that file's cached size is updated immediately
2010 */
2011 cifs_setsize(inode, dest_off + len);
2012 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002013 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2014 trgtfile->fid.volatile_fid,
2015 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2016 true /* is_fsctl */,
2017 (char *)&dup_ext_buf,
2018 sizeof(struct duplicate_extents_to_file),
David Brazdil0f672f62019-12-10 10:32:29 +00002019 CIFSMaxBufSize, NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002020 &ret_data_len);
2021
2022 if (ret_data_len > 0)
David Brazdil0f672f62019-12-10 10:32:29 +00002023 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002024
2025duplicate_extents_out:
2026 return rc;
2027}
2028
2029static int
2030smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2031 struct cifsFileInfo *cfile)
2032{
2033 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2034 cfile->fid.volatile_fid);
2035}
2036
2037static int
2038smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2039 struct cifsFileInfo *cfile)
2040{
2041 struct fsctl_set_integrity_information_req integr_info;
2042 unsigned int ret_data_len;
2043
2044 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2045 integr_info.Flags = 0;
2046 integr_info.Reserved = 0;
2047
2048 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2049 cfile->fid.volatile_fid,
2050 FSCTL_SET_INTEGRITY_INFORMATION,
2051 true /* is_fsctl */,
2052 (char *)&integr_info,
2053 sizeof(struct fsctl_set_integrity_information_req),
David Brazdil0f672f62019-12-10 10:32:29 +00002054 CIFSMaxBufSize, NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002055 &ret_data_len);
2056
2057}
2058
2059/* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2060#define GMT_TOKEN_SIZE 50
2061
David Brazdil0f672f62019-12-10 10:32:29 +00002062#define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2063
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002064/*
2065 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2066 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2067 */
2068static int
2069smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2070 struct cifsFileInfo *cfile, void __user *ioc_buf)
2071{
2072 char *retbuf = NULL;
2073 unsigned int ret_data_len = 0;
2074 int rc;
David Brazdil0f672f62019-12-10 10:32:29 +00002075 u32 max_response_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002076 struct smb_snapshot_array snapshot_in;
2077
David Brazdil0f672f62019-12-10 10:32:29 +00002078 /*
2079 * On the first query to enumerate the list of snapshots available
2080 * for this volume the buffer begins with 0 (number of snapshots
2081 * which can be returned is zero since at that point we do not know
2082 * how big the buffer needs to be). On the second query,
2083 * it (ret_data_len) is set to number of snapshots so we can
2084 * know to set the maximum response size larger (see below).
2085 */
2086 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2087 return -EFAULT;
2088
2089 /*
2090 * Note that for snapshot queries that servers like Azure expect that
2091 * the first query be minimal size (and just used to get the number/size
2092 * of previous versions) so response size must be specified as EXACTLY
2093 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2094 * of eight bytes.
2095 */
2096 if (ret_data_len == 0)
2097 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2098 else
2099 max_response_size = CIFSMaxBufSize;
2100
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002101 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2102 cfile->fid.volatile_fid,
2103 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2104 true /* is_fsctl */,
David Brazdil0f672f62019-12-10 10:32:29 +00002105 NULL, 0 /* no input data */, max_response_size,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002106 (char **)&retbuf,
2107 &ret_data_len);
2108 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2109 rc, ret_data_len);
2110 if (rc)
2111 return rc;
2112
2113 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2114 /* Fixup buffer */
2115 if (copy_from_user(&snapshot_in, ioc_buf,
2116 sizeof(struct smb_snapshot_array))) {
2117 rc = -EFAULT;
2118 kfree(retbuf);
2119 return rc;
2120 }
2121
2122 /*
2123 * Check for min size, ie not large enough to fit even one GMT
2124 * token (snapshot). On the first ioctl some users may pass in
2125 * smaller size (or zero) to simply get the size of the array
2126 * so the user space caller can allocate sufficient memory
2127 * and retry the ioctl again with larger array size sufficient
2128 * to hold all of the snapshot GMT tokens on the second try.
2129 */
2130 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2131 ret_data_len = sizeof(struct smb_snapshot_array);
2132
2133 /*
2134 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2135 * the snapshot array (of 50 byte GMT tokens) each
2136 * representing an available previous version of the data
2137 */
2138 if (ret_data_len > (snapshot_in.snapshot_array_size +
2139 sizeof(struct smb_snapshot_array)))
2140 ret_data_len = snapshot_in.snapshot_array_size +
2141 sizeof(struct smb_snapshot_array);
2142
2143 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2144 rc = -EFAULT;
2145 }
2146
2147 kfree(retbuf);
2148 return rc;
2149}
2150
Olivier Deprez157378f2022-04-04 15:47:50 +02002151
2152
2153static int
2154smb3_notify(const unsigned int xid, struct file *pfile,
2155 void __user *ioc_buf)
2156{
2157 struct smb3_notify notify;
2158 struct dentry *dentry = pfile->f_path.dentry;
2159 struct inode *inode = file_inode(pfile);
2160 struct cifs_sb_info *cifs_sb;
2161 struct cifs_open_parms oparms;
2162 struct cifs_fid fid;
2163 struct cifs_tcon *tcon;
2164 unsigned char *path = NULL;
2165 __le16 *utf16_path = NULL;
2166 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2167 int rc = 0;
2168
2169 path = build_path_from_dentry(dentry);
2170 if (path == NULL)
2171 return -ENOMEM;
2172
2173 cifs_sb = CIFS_SB(inode->i_sb);
2174
2175 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2176 if (utf16_path == NULL) {
2177 rc = -ENOMEM;
2178 goto notify_exit;
2179 }
2180
2181 if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2182 rc = -EFAULT;
2183 goto notify_exit;
2184 }
2185
2186 tcon = cifs_sb_master_tcon(cifs_sb);
2187 oparms.tcon = tcon;
2188 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2189 oparms.disposition = FILE_OPEN;
2190 oparms.create_options = cifs_create_options(cifs_sb, 0);
2191 oparms.fid = &fid;
2192 oparms.reconnect = false;
2193
2194 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2195 NULL);
2196 if (rc)
2197 goto notify_exit;
2198
2199 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2200 notify.watch_tree, notify.completion_filter);
2201
2202 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2203
2204 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2205
2206notify_exit:
2207 kfree(path);
2208 kfree(utf16_path);
2209 return rc;
2210}
2211
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002212static int
2213smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2214 const char *path, struct cifs_sb_info *cifs_sb,
2215 struct cifs_fid *fid, __u16 search_flags,
2216 struct cifs_search_info *srch_inf)
2217{
2218 __le16 *utf16_path;
Olivier Deprez157378f2022-04-04 15:47:50 +02002219 struct smb_rqst rqst[2];
2220 struct kvec rsp_iov[2];
2221 int resp_buftype[2];
2222 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2223 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2224 int rc, flags = 0;
2225 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002226 struct cifs_open_parms oparms;
Olivier Deprez157378f2022-04-04 15:47:50 +02002227 struct smb2_query_directory_rsp *qd_rsp = NULL;
2228 struct smb2_create_rsp *op_rsp = NULL;
2229 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002230
2231 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2232 if (!utf16_path)
2233 return -ENOMEM;
2234
Olivier Deprez157378f2022-04-04 15:47:50 +02002235 if (smb3_encryption_required(tcon))
2236 flags |= CIFS_TRANSFORM_REQ;
2237
2238 memset(rqst, 0, sizeof(rqst));
2239 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2240 memset(rsp_iov, 0, sizeof(rsp_iov));
2241
2242 /* Open */
2243 memset(&open_iov, 0, sizeof(open_iov));
2244 rqst[0].rq_iov = open_iov;
2245 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2246
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002247 oparms.tcon = tcon;
2248 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2249 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02002250 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002251 oparms.fid = fid;
2252 oparms.reconnect = false;
2253
Olivier Deprez157378f2022-04-04 15:47:50 +02002254 rc = SMB2_open_init(tcon, server,
2255 &rqst[0], &oplock, &oparms, utf16_path);
2256 if (rc)
2257 goto qdf_free;
2258 smb2_set_next_command(tcon, &rqst[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002259
Olivier Deprez157378f2022-04-04 15:47:50 +02002260 /* Query directory */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002261 srch_inf->entries_in_buffer = 0;
2262 srch_inf->index_of_last_entry = 2;
2263
Olivier Deprez157378f2022-04-04 15:47:50 +02002264 memset(&qd_iov, 0, sizeof(qd_iov));
2265 rqst[1].rq_iov = qd_iov;
2266 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2267
2268 rc = SMB2_query_directory_init(xid, tcon, server,
2269 &rqst[1],
2270 COMPOUND_FID, COMPOUND_FID,
2271 0, srch_inf->info_level);
2272 if (rc)
2273 goto qdf_free;
2274
2275 smb2_set_related(&rqst[1]);
2276
2277 rc = compound_send_recv(xid, tcon->ses, server,
2278 flags, 2, rqst,
2279 resp_buftype, rsp_iov);
2280
2281 /* If the open failed there is nothing to do */
2282 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2283 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2284 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2285 goto qdf_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002286 }
Olivier Deprez157378f2022-04-04 15:47:50 +02002287 fid->persistent_fid = op_rsp->PersistentFileId;
2288 fid->volatile_fid = op_rsp->VolatileFileId;
2289
2290 /* Anything else than ENODATA means a genuine error */
2291 if (rc && rc != -ENODATA) {
2292 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2293 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2294 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2295 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2296 goto qdf_free;
2297 }
2298
2299 atomic_inc(&tcon->num_remote_opens);
2300
2301 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2302 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2303 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2304 tcon->tid, tcon->ses->Suid, 0, 0);
2305 srch_inf->endOfSearch = true;
2306 rc = 0;
2307 goto qdf_free;
2308 }
2309
2310 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2311 srch_inf);
2312 if (rc) {
2313 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2314 tcon->ses->Suid, 0, 0, rc);
2315 goto qdf_free;
2316 }
2317 resp_buftype[1] = CIFS_NO_BUFFER;
2318
2319 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2320 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2321
2322 qdf_free:
2323 kfree(utf16_path);
2324 SMB2_open_free(&rqst[0]);
2325 SMB2_query_directory_free(&rqst[1]);
2326 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2327 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002328 return rc;
2329}
2330
2331static int
2332smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2333 struct cifs_fid *fid, __u16 search_flags,
2334 struct cifs_search_info *srch_inf)
2335{
2336 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2337 fid->volatile_fid, 0, srch_inf);
2338}
2339
2340static int
2341smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2342 struct cifs_fid *fid)
2343{
2344 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2345}
2346
2347/*
David Brazdil0f672f62019-12-10 10:32:29 +00002348 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2349 * the number of credits and return true. Otherwise - return false.
2350 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002351static bool
David Brazdil0f672f62019-12-10 10:32:29 +00002352smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002353{
2354 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2355
2356 if (shdr->Status != STATUS_PENDING)
2357 return false;
2358
David Brazdil0f672f62019-12-10 10:32:29 +00002359 if (shdr->CreditRequest) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002360 spin_lock(&server->req_lock);
2361 server->credits += le16_to_cpu(shdr->CreditRequest);
2362 spin_unlock(&server->req_lock);
2363 wake_up(&server->request_q);
2364 }
2365
2366 return true;
2367}
2368
2369static bool
2370smb2_is_session_expired(char *buf)
2371{
2372 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2373
2374 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2375 shdr->Status != STATUS_USER_SESSION_DELETED)
2376 return false;
2377
2378 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2379 le16_to_cpu(shdr->Command),
2380 le64_to_cpu(shdr->MessageId));
2381 cifs_dbg(FYI, "Session expired or deleted\n");
2382
2383 return true;
2384}
2385
Olivier Deprez157378f2022-04-04 15:47:50 +02002386static bool
2387smb2_is_status_io_timeout(char *buf)
2388{
2389 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2390
2391 if (shdr->Status == STATUS_IO_TIMEOUT)
2392 return true;
2393 else
2394 return false;
2395}
2396
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002397static int
2398smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2399 struct cifsInodeInfo *cinode)
2400{
2401 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2402 return SMB2_lease_break(0, tcon, cinode->lease_key,
2403 smb2_get_lease_state(cinode));
2404
2405 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2406 fid->volatile_fid,
2407 CIFS_CACHE_READ(cinode) ? 1 : 0);
2408}
2409
David Brazdil0f672f62019-12-10 10:32:29 +00002410void
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002411smb2_set_related(struct smb_rqst *rqst)
2412{
2413 struct smb2_sync_hdr *shdr;
2414
2415 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
David Brazdil0f672f62019-12-10 10:32:29 +00002416 if (shdr == NULL) {
2417 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2418 return;
2419 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002420 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2421}
2422
2423char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2424
David Brazdil0f672f62019-12-10 10:32:29 +00002425void
2426smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002427{
2428 struct smb2_sync_hdr *shdr;
David Brazdil0f672f62019-12-10 10:32:29 +00002429 struct cifs_ses *ses = tcon->ses;
2430 struct TCP_Server_Info *server = ses->server;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002431 unsigned long len = smb_rqst_len(server, rqst);
David Brazdil0f672f62019-12-10 10:32:29 +00002432 int i, num_padding;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002433
2434 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
David Brazdil0f672f62019-12-10 10:32:29 +00002435 if (shdr == NULL) {
2436 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2437 return;
2438 }
2439
2440 /* SMB headers in a compound are 8 byte aligned. */
2441
2442 /* No padding needed */
2443 if (!(len & 7))
2444 goto finished;
2445
2446 num_padding = 8 - (len & 7);
2447 if (!smb3_encryption_required(tcon)) {
2448 /*
2449 * If we do not have encryption then we can just add an extra
2450 * iov for the padding.
2451 */
2452 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2453 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2454 rqst->rq_nvec++;
2455 len += num_padding;
2456 } else {
2457 /*
2458 * We can not add a small padding iov for the encryption case
2459 * because the encryption framework can not handle the padding
2460 * iovs.
2461 * We have to flatten this into a single buffer and add
2462 * the padding to it.
2463 */
2464 for (i = 1; i < rqst->rq_nvec; i++) {
2465 memcpy(rqst->rq_iov[0].iov_base +
2466 rqst->rq_iov[0].iov_len,
2467 rqst->rq_iov[i].iov_base,
2468 rqst->rq_iov[i].iov_len);
2469 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2470 }
2471 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2472 0, num_padding);
2473 rqst->rq_iov[0].iov_len += num_padding;
2474 len += num_padding;
2475 rqst->rq_nvec = 1;
2476 }
2477
2478 finished:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002479 shdr->NextCommand = cpu_to_le32(len);
2480}
2481
David Brazdil0f672f62019-12-10 10:32:29 +00002482/*
2483 * Passes the query info response back to the caller on success.
2484 * Caller need to free this with free_rsp_buf().
2485 */
2486int
2487smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2488 __le16 *utf16_path, u32 desired_access,
2489 u32 class, u32 type, u32 output_len,
2490 struct kvec *rsp, int *buftype,
2491 struct cifs_sb_info *cifs_sb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002492{
David Brazdil0f672f62019-12-10 10:32:29 +00002493 struct cifs_ses *ses = tcon->ses;
Olivier Deprez157378f2022-04-04 15:47:50 +02002494 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2495 int flags = CIFS_CP_CREATE_CLOSE_OP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002496 struct smb_rqst rqst[3];
2497 int resp_buftype[3];
2498 struct kvec rsp_iov[3];
2499 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2500 struct kvec qi_iov[1];
2501 struct kvec close_iov[1];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002502 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2503 struct cifs_open_parms oparms;
2504 struct cifs_fid fid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002505 int rc;
2506
2507 if (smb3_encryption_required(tcon))
2508 flags |= CIFS_TRANSFORM_REQ;
2509
2510 memset(rqst, 0, sizeof(rqst));
David Brazdil0f672f62019-12-10 10:32:29 +00002511 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002512 memset(rsp_iov, 0, sizeof(rsp_iov));
2513
2514 memset(&open_iov, 0, sizeof(open_iov));
2515 rqst[0].rq_iov = open_iov;
2516 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2517
2518 oparms.tcon = tcon;
David Brazdil0f672f62019-12-10 10:32:29 +00002519 oparms.desired_access = desired_access;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002520 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02002521 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002522 oparms.fid = &fid;
2523 oparms.reconnect = false;
2524
Olivier Deprez157378f2022-04-04 15:47:50 +02002525 rc = SMB2_open_init(tcon, server,
2526 &rqst[0], &oplock, &oparms, utf16_path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002527 if (rc)
David Brazdil0f672f62019-12-10 10:32:29 +00002528 goto qic_exit;
2529 smb2_set_next_command(tcon, &rqst[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002530
2531 memset(&qi_iov, 0, sizeof(qi_iov));
2532 rqst[1].rq_iov = qi_iov;
2533 rqst[1].rq_nvec = 1;
2534
Olivier Deprez157378f2022-04-04 15:47:50 +02002535 rc = SMB2_query_info_init(tcon, server,
2536 &rqst[1], COMPOUND_FID, COMPOUND_FID,
David Brazdil0f672f62019-12-10 10:32:29 +00002537 class, type, 0,
2538 output_len, 0,
2539 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002540 if (rc)
David Brazdil0f672f62019-12-10 10:32:29 +00002541 goto qic_exit;
2542 smb2_set_next_command(tcon, &rqst[1]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002543 smb2_set_related(&rqst[1]);
2544
2545 memset(&close_iov, 0, sizeof(close_iov));
2546 rqst[2].rq_iov = close_iov;
2547 rqst[2].rq_nvec = 1;
2548
Olivier Deprez157378f2022-04-04 15:47:50 +02002549 rc = SMB2_close_init(tcon, server,
2550 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002551 if (rc)
David Brazdil0f672f62019-12-10 10:32:29 +00002552 goto qic_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002553 smb2_set_related(&rqst[2]);
2554
Olivier Deprez157378f2022-04-04 15:47:50 +02002555 rc = compound_send_recv(xid, ses, server,
2556 flags, 3, rqst,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002557 resp_buftype, rsp_iov);
David Brazdil0f672f62019-12-10 10:32:29 +00002558 if (rc) {
2559 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2560 if (rc == -EREMCHG) {
2561 tcon->need_reconnect = true;
Olivier Deprez157378f2022-04-04 15:47:50 +02002562 pr_warn_once("server share %s deleted\n",
2563 tcon->treeName);
David Brazdil0f672f62019-12-10 10:32:29 +00002564 }
2565 goto qic_exit;
2566 }
2567 *rsp = rsp_iov[1];
2568 *buftype = resp_buftype[1];
2569
2570 qic_exit:
2571 SMB2_open_free(&rqst[0]);
2572 SMB2_query_info_free(&rqst[1]);
2573 SMB2_close_free(&rqst[2]);
2574 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2575 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2576 return rc;
2577}
2578
2579static int
2580smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
Olivier Deprez157378f2022-04-04 15:47:50 +02002581 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
David Brazdil0f672f62019-12-10 10:32:29 +00002582{
2583 struct smb2_query_info_rsp *rsp;
2584 struct smb2_fs_full_size_info *info = NULL;
2585 __le16 utf16_path = 0; /* Null - open root of share */
2586 struct kvec rsp_iov = {NULL, 0};
2587 int buftype = CIFS_NO_BUFFER;
2588 int rc;
2589
2590
2591 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2592 FILE_READ_ATTRIBUTES,
2593 FS_FULL_SIZE_INFORMATION,
2594 SMB2_O_INFO_FILESYSTEM,
2595 sizeof(struct smb2_fs_full_size_info),
Olivier Deprez157378f2022-04-04 15:47:50 +02002596 &rsp_iov, &buftype, cifs_sb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002597 if (rc)
2598 goto qfs_exit;
2599
David Brazdil0f672f62019-12-10 10:32:29 +00002600 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002601 buf->f_type = SMB2_MAGIC_NUMBER;
2602 info = (struct smb2_fs_full_size_info *)(
2603 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2604 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2605 le32_to_cpu(rsp->OutputBufferLength),
David Brazdil0f672f62019-12-10 10:32:29 +00002606 &rsp_iov,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002607 sizeof(struct smb2_fs_full_size_info));
2608 if (!rc)
2609 smb2_copy_fs_info_to_kstatfs(info, buf);
2610
2611qfs_exit:
David Brazdil0f672f62019-12-10 10:32:29 +00002612 free_rsp_buf(buftype, rsp_iov.iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002613 return rc;
2614}
2615
2616static int
2617smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
Olivier Deprez157378f2022-04-04 15:47:50 +02002618 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002619{
2620 int rc;
2621 __le16 srch_path = 0; /* Null - open root of share */
2622 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2623 struct cifs_open_parms oparms;
2624 struct cifs_fid fid;
2625
2626 if (!tcon->posix_extensions)
Olivier Deprez157378f2022-04-04 15:47:50 +02002627 return smb2_queryfs(xid, tcon, cifs_sb, buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002628
2629 oparms.tcon = tcon;
2630 oparms.desired_access = FILE_READ_ATTRIBUTES;
2631 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02002632 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002633 oparms.fid = &fid;
2634 oparms.reconnect = false;
2635
Olivier Deprez157378f2022-04-04 15:47:50 +02002636 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2637 NULL, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002638 if (rc)
2639 return rc;
2640
2641 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2642 fid.volatile_fid, buf);
2643 buf->f_type = SMB2_MAGIC_NUMBER;
2644 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2645 return rc;
2646}
2647
2648static bool
2649smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2650{
2651 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2652 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2653}
2654
2655static int
2656smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2657 __u64 length, __u32 type, int lock, int unlock, bool wait)
2658{
2659 if (unlock && !lock)
2660 type = SMB2_LOCKFLAG_UNLOCK;
2661 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2662 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2663 current->tgid, length, offset, type, wait);
2664}
2665
2666static void
2667smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2668{
2669 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2670}
2671
2672static void
2673smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2674{
2675 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2676}
2677
2678static void
2679smb2_new_lease_key(struct cifs_fid *fid)
2680{
2681 generate_random_uuid(fid->lease_key);
2682}
2683
2684static int
2685smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2686 const char *search_name,
2687 struct dfs_info3_param **target_nodes,
2688 unsigned int *num_of_nodes,
2689 const struct nls_table *nls_codepage, int remap)
2690{
2691 int rc;
2692 __le16 *utf16_path = NULL;
2693 int utf16_path_len = 0;
2694 struct cifs_tcon *tcon;
2695 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2696 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2697 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2698
David Brazdil0f672f62019-12-10 10:32:29 +00002699 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002700
2701 /*
2702 * Try to use the IPC tcon, otherwise just use any
2703 */
2704 tcon = ses->tcon_ipc;
2705 if (tcon == NULL) {
2706 spin_lock(&cifs_tcp_ses_lock);
2707 tcon = list_first_entry_or_null(&ses->tcon_list,
2708 struct cifs_tcon,
2709 tcon_list);
2710 if (tcon)
2711 tcon->tc_count++;
2712 spin_unlock(&cifs_tcp_ses_lock);
2713 }
2714
2715 if (tcon == NULL) {
2716 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2717 ses);
2718 rc = -ENOTCONN;
2719 goto out;
2720 }
2721
2722 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2723 &utf16_path_len,
2724 nls_codepage, remap);
2725 if (!utf16_path) {
2726 rc = -ENOMEM;
2727 goto out;
2728 }
2729
2730 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2731 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2732 if (!dfs_req) {
2733 rc = -ENOMEM;
2734 goto out;
2735 }
2736
2737 /* Highest DFS referral version understood */
2738 dfs_req->MaxReferralLevel = DFS_VERSION;
2739
2740 /* Path to resolve in an UTF-16 null-terminated string */
2741 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2742
2743 do {
2744 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2745 FSCTL_DFS_GET_REFERRALS,
2746 true /* is_fsctl */,
David Brazdil0f672f62019-12-10 10:32:29 +00002747 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002748 (char **)&dfs_rsp, &dfs_rsp_size);
2749 } while (rc == -EAGAIN);
2750
2751 if (rc) {
2752 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
David Brazdil0f672f62019-12-10 10:32:29 +00002753 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002754 goto out;
2755 }
2756
2757 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2758 num_of_nodes, target_nodes,
2759 nls_codepage, remap, search_name,
2760 true /* is_unicode */);
2761 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00002762 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002763 goto out;
2764 }
2765
2766 out:
2767 if (tcon && !tcon->ipc) {
2768 /* ipc tcons are not refcounted */
2769 spin_lock(&cifs_tcp_ses_lock);
2770 tcon->tc_count--;
2771 spin_unlock(&cifs_tcp_ses_lock);
2772 }
2773 kfree(utf16_path);
2774 kfree(dfs_req);
2775 kfree(dfs_rsp);
2776 return rc;
2777}
David Brazdil0f672f62019-12-10 10:32:29 +00002778
2779static int
2780parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2781 u32 plen, char **target_path,
2782 struct cifs_sb_info *cifs_sb)
2783{
2784 unsigned int len;
2785
2786 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2787 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2788
2789 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2790 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2791 le64_to_cpu(symlink_buf->InodeType));
2792 return -EOPNOTSUPP;
2793 }
2794
2795 *target_path = cifs_strndup_from_utf16(
2796 symlink_buf->PathBuffer,
2797 len, true, cifs_sb->local_nls);
2798 if (!(*target_path))
2799 return -ENOMEM;
2800
2801 convert_delimiter(*target_path, '/');
2802 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2803
2804 return 0;
2805}
2806
2807static int
2808parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2809 u32 plen, char **target_path,
2810 struct cifs_sb_info *cifs_sb)
2811{
2812 unsigned int sub_len;
2813 unsigned int sub_offset;
2814
2815 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2816
2817 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2818 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2819 if (sub_offset + 20 > plen ||
2820 sub_offset + sub_len + 20 > plen) {
2821 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2822 return -EIO;
2823 }
2824
2825 *target_path = cifs_strndup_from_utf16(
2826 symlink_buf->PathBuffer + sub_offset,
2827 sub_len, true, cifs_sb->local_nls);
2828 if (!(*target_path))
2829 return -ENOMEM;
2830
2831 convert_delimiter(*target_path, '/');
2832 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2833
2834 return 0;
2835}
2836
2837static int
2838parse_reparse_point(struct reparse_data_buffer *buf,
2839 u32 plen, char **target_path,
2840 struct cifs_sb_info *cifs_sb)
2841{
2842 if (plen < sizeof(struct reparse_data_buffer)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002843 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2844 plen);
David Brazdil0f672f62019-12-10 10:32:29 +00002845 return -EIO;
2846 }
2847
2848 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2849 sizeof(struct reparse_data_buffer)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002850 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2851 plen);
David Brazdil0f672f62019-12-10 10:32:29 +00002852 return -EIO;
2853 }
2854
2855 /* See MS-FSCC 2.1.2 */
2856 switch (le32_to_cpu(buf->ReparseTag)) {
2857 case IO_REPARSE_TAG_NFS:
2858 return parse_reparse_posix(
2859 (struct reparse_posix_data *)buf,
2860 plen, target_path, cifs_sb);
2861 case IO_REPARSE_TAG_SYMLINK:
2862 return parse_reparse_symlink(
2863 (struct reparse_symlink_data_buffer *)buf,
2864 plen, target_path, cifs_sb);
2865 default:
Olivier Deprez157378f2022-04-04 15:47:50 +02002866 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
2867 le32_to_cpu(buf->ReparseTag));
David Brazdil0f672f62019-12-10 10:32:29 +00002868 return -EOPNOTSUPP;
2869 }
2870}
2871
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002872#define SMB2_SYMLINK_STRUCT_SIZE \
2873 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2874
2875static int
2876smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
David Brazdil0f672f62019-12-10 10:32:29 +00002877 struct cifs_sb_info *cifs_sb, const char *full_path,
2878 char **target_path, bool is_reparse_point)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002879{
2880 int rc;
David Brazdil0f672f62019-12-10 10:32:29 +00002881 __le16 *utf16_path = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002882 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2883 struct cifs_open_parms oparms;
2884 struct cifs_fid fid;
2885 struct kvec err_iov = {NULL, 0};
2886 struct smb2_err_rsp *err_buf = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002887 struct smb2_symlink_err_rsp *symlink;
Olivier Deprez157378f2022-04-04 15:47:50 +02002888 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002889 unsigned int sub_len;
2890 unsigned int sub_offset;
2891 unsigned int print_len;
2892 unsigned int print_offset;
Olivier Deprez157378f2022-04-04 15:47:50 +02002893 int flags = CIFS_CP_CREATE_CLOSE_OP;
David Brazdil0f672f62019-12-10 10:32:29 +00002894 struct smb_rqst rqst[3];
2895 int resp_buftype[3];
2896 struct kvec rsp_iov[3];
2897 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2898 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2899 struct kvec close_iov[1];
2900 struct smb2_create_rsp *create_rsp;
2901 struct smb2_ioctl_rsp *ioctl_rsp;
2902 struct reparse_data_buffer *reparse_buf;
Olivier Deprez157378f2022-04-04 15:47:50 +02002903 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002904 u32 plen;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002905
2906 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2907
David Brazdil0f672f62019-12-10 10:32:29 +00002908 *target_path = NULL;
2909
2910 if (smb3_encryption_required(tcon))
2911 flags |= CIFS_TRANSFORM_REQ;
2912
2913 memset(rqst, 0, sizeof(rqst));
2914 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2915 memset(rsp_iov, 0, sizeof(rsp_iov));
2916
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002917 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2918 if (!utf16_path)
2919 return -ENOMEM;
2920
David Brazdil0f672f62019-12-10 10:32:29 +00002921 /* Open */
2922 memset(&open_iov, 0, sizeof(open_iov));
2923 rqst[0].rq_iov = open_iov;
2924 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2925
2926 memset(&oparms, 0, sizeof(oparms));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002927 oparms.tcon = tcon;
2928 oparms.desired_access = FILE_READ_ATTRIBUTES;
2929 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02002930 oparms.create_options = cifs_create_options(cifs_sb, create_options);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002931 oparms.fid = &fid;
2932 oparms.reconnect = false;
2933
Olivier Deprez157378f2022-04-04 15:47:50 +02002934 rc = SMB2_open_init(tcon, server,
2935 &rqst[0], &oplock, &oparms, utf16_path);
David Brazdil0f672f62019-12-10 10:32:29 +00002936 if (rc)
2937 goto querty_exit;
2938 smb2_set_next_command(tcon, &rqst[0]);
2939
2940
2941 /* IOCTL */
2942 memset(&io_iov, 0, sizeof(io_iov));
2943 rqst[1].rq_iov = io_iov;
2944 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2945
Olivier Deprez157378f2022-04-04 15:47:50 +02002946 rc = SMB2_ioctl_init(tcon, server,
2947 &rqst[1], fid.persistent_fid,
David Brazdil0f672f62019-12-10 10:32:29 +00002948 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
Olivier Deprez0e641232021-09-23 10:07:05 +02002949 true /* is_fctl */, NULL, 0,
2950 CIFSMaxBufSize -
2951 MAX_SMB2_CREATE_RESPONSE_SIZE -
2952 MAX_SMB2_CLOSE_RESPONSE_SIZE);
David Brazdil0f672f62019-12-10 10:32:29 +00002953 if (rc)
2954 goto querty_exit;
2955
2956 smb2_set_next_command(tcon, &rqst[1]);
2957 smb2_set_related(&rqst[1]);
2958
2959
2960 /* Close */
2961 memset(&close_iov, 0, sizeof(close_iov));
2962 rqst[2].rq_iov = close_iov;
2963 rqst[2].rq_nvec = 1;
2964
Olivier Deprez157378f2022-04-04 15:47:50 +02002965 rc = SMB2_close_init(tcon, server,
2966 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
David Brazdil0f672f62019-12-10 10:32:29 +00002967 if (rc)
2968 goto querty_exit;
2969
2970 smb2_set_related(&rqst[2]);
2971
Olivier Deprez157378f2022-04-04 15:47:50 +02002972 rc = compound_send_recv(xid, tcon->ses, server,
2973 flags, 3, rqst,
David Brazdil0f672f62019-12-10 10:32:29 +00002974 resp_buftype, rsp_iov);
2975
2976 create_rsp = rsp_iov[0].iov_base;
2977 if (create_rsp && create_rsp->sync_hdr.Status)
2978 err_iov = rsp_iov[0];
2979 ioctl_rsp = rsp_iov[1].iov_base;
2980
2981 /*
2982 * Open was successful and we got an ioctl response.
2983 */
2984 if ((rc == 0) && (is_reparse_point)) {
2985 /* See MS-FSCC 2.3.23 */
2986
2987 reparse_buf = (struct reparse_data_buffer *)
2988 ((char *)ioctl_rsp +
2989 le32_to_cpu(ioctl_rsp->OutputOffset));
2990 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2991
2992 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2993 rsp_iov[1].iov_len) {
2994 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2995 plen);
2996 rc = -EIO;
2997 goto querty_exit;
2998 }
2999
3000 rc = parse_reparse_point(reparse_buf, plen, target_path,
3001 cifs_sb);
3002 goto querty_exit;
3003 }
3004
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003005 if (!rc || !err_iov.iov_base) {
3006 rc = -ENOENT;
David Brazdil0f672f62019-12-10 10:32:29 +00003007 goto querty_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003008 }
3009
3010 err_buf = err_iov.iov_base;
3011 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
3012 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
David Brazdil0f672f62019-12-10 10:32:29 +00003013 rc = -EINVAL;
3014 goto querty_exit;
3015 }
3016
3017 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
3018 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
3019 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
3020 rc = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003021 goto querty_exit;
3022 }
3023
3024 /* open must fail on symlink - reset rc */
3025 rc = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003026 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3027 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
3028 print_len = le16_to_cpu(symlink->PrintNameLength);
3029 print_offset = le16_to_cpu(symlink->PrintNameOffset);
3030
3031 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
David Brazdil0f672f62019-12-10 10:32:29 +00003032 rc = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003033 goto querty_exit;
3034 }
3035
3036 if (err_iov.iov_len <
3037 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
David Brazdil0f672f62019-12-10 10:32:29 +00003038 rc = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003039 goto querty_exit;
3040 }
3041
3042 *target_path = cifs_strndup_from_utf16(
3043 (char *)symlink->PathBuffer + sub_offset,
3044 sub_len, true, cifs_sb->local_nls);
3045 if (!(*target_path)) {
3046 rc = -ENOMEM;
3047 goto querty_exit;
3048 }
3049 convert_delimiter(*target_path, '/');
3050 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3051
3052 querty_exit:
David Brazdil0f672f62019-12-10 10:32:29 +00003053 cifs_dbg(FYI, "query symlink rc %d\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003054 kfree(utf16_path);
David Brazdil0f672f62019-12-10 10:32:29 +00003055 SMB2_open_free(&rqst[0]);
3056 SMB2_ioctl_free(&rqst[1]);
3057 SMB2_close_free(&rqst[2]);
3058 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3059 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3060 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003061 return rc;
3062}
3063
Olivier Deprez157378f2022-04-04 15:47:50 +02003064int
3065smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3066 struct cifs_sb_info *cifs_sb, const char *full_path,
3067 __u32 *tag)
3068{
3069 int rc;
3070 __le16 *utf16_path = NULL;
3071 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3072 struct cifs_open_parms oparms;
3073 struct cifs_fid fid;
3074 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3075 int flags = CIFS_CP_CREATE_CLOSE_OP;
3076 struct smb_rqst rqst[3];
3077 int resp_buftype[3];
3078 struct kvec rsp_iov[3];
3079 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3080 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3081 struct kvec close_iov[1];
3082 struct smb2_ioctl_rsp *ioctl_rsp;
3083 struct reparse_data_buffer *reparse_buf;
3084 u32 plen;
3085
3086 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3087
3088 if (smb3_encryption_required(tcon))
3089 flags |= CIFS_TRANSFORM_REQ;
3090
3091 memset(rqst, 0, sizeof(rqst));
3092 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3093 memset(rsp_iov, 0, sizeof(rsp_iov));
3094
3095 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3096 if (!utf16_path)
3097 return -ENOMEM;
3098
3099 /*
3100 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3101 * to see if there is a handle already open that we can use
3102 */
3103 memset(&open_iov, 0, sizeof(open_iov));
3104 rqst[0].rq_iov = open_iov;
3105 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3106
3107 memset(&oparms, 0, sizeof(oparms));
3108 oparms.tcon = tcon;
3109 oparms.desired_access = FILE_READ_ATTRIBUTES;
3110 oparms.disposition = FILE_OPEN;
3111 oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3112 oparms.fid = &fid;
3113 oparms.reconnect = false;
3114
3115 rc = SMB2_open_init(tcon, server,
3116 &rqst[0], &oplock, &oparms, utf16_path);
3117 if (rc)
3118 goto query_rp_exit;
3119 smb2_set_next_command(tcon, &rqst[0]);
3120
3121
3122 /* IOCTL */
3123 memset(&io_iov, 0, sizeof(io_iov));
3124 rqst[1].rq_iov = io_iov;
3125 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3126
3127 rc = SMB2_ioctl_init(tcon, server,
3128 &rqst[1], COMPOUND_FID,
3129 COMPOUND_FID, FSCTL_GET_REPARSE_POINT,
3130 true /* is_fctl */, NULL, 0,
3131 CIFSMaxBufSize -
3132 MAX_SMB2_CREATE_RESPONSE_SIZE -
3133 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3134 if (rc)
3135 goto query_rp_exit;
3136
3137 smb2_set_next_command(tcon, &rqst[1]);
3138 smb2_set_related(&rqst[1]);
3139
3140
3141 /* Close */
3142 memset(&close_iov, 0, sizeof(close_iov));
3143 rqst[2].rq_iov = close_iov;
3144 rqst[2].rq_nvec = 1;
3145
3146 rc = SMB2_close_init(tcon, server,
3147 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3148 if (rc)
3149 goto query_rp_exit;
3150
3151 smb2_set_related(&rqst[2]);
3152
3153 rc = compound_send_recv(xid, tcon->ses, server,
3154 flags, 3, rqst,
3155 resp_buftype, rsp_iov);
3156
3157 ioctl_rsp = rsp_iov[1].iov_base;
3158
3159 /*
3160 * Open was successful and we got an ioctl response.
3161 */
3162 if (rc == 0) {
3163 /* See MS-FSCC 2.3.23 */
3164
3165 reparse_buf = (struct reparse_data_buffer *)
3166 ((char *)ioctl_rsp +
3167 le32_to_cpu(ioctl_rsp->OutputOffset));
3168 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3169
3170 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3171 rsp_iov[1].iov_len) {
3172 cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3173 plen);
3174 rc = -EIO;
3175 goto query_rp_exit;
3176 }
3177 *tag = le32_to_cpu(reparse_buf->ReparseTag);
3178 }
3179
3180 query_rp_exit:
3181 kfree(utf16_path);
3182 SMB2_open_free(&rqst[0]);
3183 SMB2_ioctl_free(&rqst[1]);
3184 SMB2_close_free(&rqst[2]);
3185 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3186 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3187 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3188 return rc;
3189}
3190
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003191static struct cifs_ntsd *
3192get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3193 const struct cifs_fid *cifsfid, u32 *pacllen)
3194{
3195 struct cifs_ntsd *pntsd = NULL;
3196 unsigned int xid;
3197 int rc = -EOPNOTSUPP;
3198 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3199
3200 if (IS_ERR(tlink))
3201 return ERR_CAST(tlink);
3202
3203 xid = get_xid();
3204 cifs_dbg(FYI, "trying to get acl\n");
3205
3206 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3207 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
3208 free_xid(xid);
3209
3210 cifs_put_tlink(tlink);
3211
3212 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3213 if (rc)
3214 return ERR_PTR(rc);
3215 return pntsd;
3216
3217}
3218
3219static struct cifs_ntsd *
3220get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3221 const char *path, u32 *pacllen)
3222{
3223 struct cifs_ntsd *pntsd = NULL;
3224 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3225 unsigned int xid;
3226 int rc;
3227 struct cifs_tcon *tcon;
3228 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3229 struct cifs_fid fid;
3230 struct cifs_open_parms oparms;
3231 __le16 *utf16_path;
3232
3233 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3234 if (IS_ERR(tlink))
3235 return ERR_CAST(tlink);
3236
3237 tcon = tlink_tcon(tlink);
3238 xid = get_xid();
3239
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003240 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3241 if (!utf16_path) {
3242 rc = -ENOMEM;
3243 free_xid(xid);
3244 return ERR_PTR(rc);
3245 }
3246
3247 oparms.tcon = tcon;
3248 oparms.desired_access = READ_CONTROL;
3249 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02003250 /*
3251 * When querying an ACL, even if the file is a symlink we want to open
3252 * the source not the target, and so the protocol requires that the
3253 * client specify this flag when opening a reparse point
3254 */
3255 oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003256 oparms.fid = &fid;
3257 oparms.reconnect = false;
3258
Olivier Deprez157378f2022-04-04 15:47:50 +02003259 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3260 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003261 kfree(utf16_path);
3262 if (!rc) {
3263 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3264 fid.volatile_fid, (void **)&pntsd, pacllen);
3265 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3266 }
3267
3268 cifs_put_tlink(tlink);
3269 free_xid(xid);
3270
3271 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3272 if (rc)
3273 return ERR_PTR(rc);
3274 return pntsd;
3275}
3276
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003277static int
3278set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3279 struct inode *inode, const char *path, int aclflag)
3280{
3281 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3282 unsigned int xid;
3283 int rc, access_flags = 0;
3284 struct cifs_tcon *tcon;
3285 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3286 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3287 struct cifs_fid fid;
3288 struct cifs_open_parms oparms;
3289 __le16 *utf16_path;
3290
3291 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3292 if (IS_ERR(tlink))
3293 return PTR_ERR(tlink);
3294
3295 tcon = tlink_tcon(tlink);
3296 xid = get_xid();
3297
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003298 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3299 access_flags = WRITE_OWNER;
3300 else
3301 access_flags = WRITE_DAC;
3302
3303 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3304 if (!utf16_path) {
3305 rc = -ENOMEM;
3306 free_xid(xid);
3307 return rc;
3308 }
3309
3310 oparms.tcon = tcon;
3311 oparms.desired_access = access_flags;
Olivier Deprez157378f2022-04-04 15:47:50 +02003312 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003313 oparms.disposition = FILE_OPEN;
3314 oparms.path = path;
3315 oparms.fid = &fid;
3316 oparms.reconnect = false;
3317
Olivier Deprez157378f2022-04-04 15:47:50 +02003318 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3319 NULL, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003320 kfree(utf16_path);
3321 if (!rc) {
3322 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3323 fid.volatile_fid, pnntsd, acllen, aclflag);
3324 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3325 }
3326
3327 cifs_put_tlink(tlink);
3328 free_xid(xid);
3329 return rc;
3330}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003331
3332/* Retrieve an ACL from the server */
3333static struct cifs_ntsd *
3334get_smb2_acl(struct cifs_sb_info *cifs_sb,
3335 struct inode *inode, const char *path,
3336 u32 *pacllen)
3337{
3338 struct cifs_ntsd *pntsd = NULL;
3339 struct cifsFileInfo *open_file = NULL;
3340
3341 if (inode)
3342 open_file = find_readable_file(CIFS_I(inode), true);
3343 if (!open_file)
3344 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3345
3346 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3347 cifsFileInfo_put(open_file);
3348 return pntsd;
3349}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003350
3351static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3352 loff_t offset, loff_t len, bool keep_size)
3353{
David Brazdil0f672f62019-12-10 10:32:29 +00003354 struct cifs_ses *ses = tcon->ses;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003355 struct inode *inode;
3356 struct cifsInodeInfo *cifsi;
3357 struct cifsFileInfo *cfile = file->private_data;
3358 struct file_zero_data_information fsctl_buf;
3359 long rc;
3360 unsigned int xid;
David Brazdil0f672f62019-12-10 10:32:29 +00003361 __le64 eof;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003362
3363 xid = get_xid();
3364
3365 inode = d_inode(cfile->dentry);
3366 cifsi = CIFS_I(inode);
3367
David Brazdil0f672f62019-12-10 10:32:29 +00003368 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3369 ses->Suid, offset, len);
3370
Olivier Deprez0e641232021-09-23 10:07:05 +02003371 /*
3372 * We zero the range through ioctl, so we need remove the page caches
3373 * first, otherwise the data may be inconsistent with the server.
3374 */
3375 truncate_pagecache_range(inode, offset, offset + len - 1);
David Brazdil0f672f62019-12-10 10:32:29 +00003376
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003377 /* if file not oplocked can't be sure whether asking to extend size */
3378 if (!CIFS_CACHE_READ(cifsi))
3379 if (keep_size == false) {
3380 rc = -EOPNOTSUPP;
David Brazdil0f672f62019-12-10 10:32:29 +00003381 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3382 tcon->tid, ses->Suid, offset, len, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003383 free_xid(xid);
3384 return rc;
3385 }
3386
David Brazdil0f672f62019-12-10 10:32:29 +00003387 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003388
3389 fsctl_buf.FileOffset = cpu_to_le64(offset);
3390 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3391
3392 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
David Brazdil0f672f62019-12-10 10:32:29 +00003393 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3394 (char *)&fsctl_buf,
3395 sizeof(struct file_zero_data_information),
3396 0, NULL, NULL);
3397 if (rc)
3398 goto zero_range_exit;
3399
3400 /*
3401 * do we also need to change the size of the file?
3402 */
3403 if (keep_size == false && i_size_read(inode) < offset + len) {
3404 eof = cpu_to_le64(offset + len);
3405 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3406 cfile->fid.volatile_fid, cfile->pid, &eof);
3407 }
3408
3409 zero_range_exit:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003410 free_xid(xid);
David Brazdil0f672f62019-12-10 10:32:29 +00003411 if (rc)
3412 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3413 ses->Suid, offset, len, rc);
3414 else
3415 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3416 ses->Suid, offset, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003417 return rc;
3418}
3419
3420static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3421 loff_t offset, loff_t len)
3422{
3423 struct inode *inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003424 struct cifsFileInfo *cfile = file->private_data;
3425 struct file_zero_data_information fsctl_buf;
3426 long rc;
3427 unsigned int xid;
3428 __u8 set_sparse = 1;
3429
3430 xid = get_xid();
3431
3432 inode = d_inode(cfile->dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003433
3434 /* Need to make file sparse, if not already, before freeing range. */
3435 /* Consider adding equivalent for compressed since it could also work */
3436 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3437 rc = -EOPNOTSUPP;
3438 free_xid(xid);
3439 return rc;
3440 }
3441
Olivier Deprez0e641232021-09-23 10:07:05 +02003442 /*
3443 * We implement the punch hole through ioctl, so we need remove the page
3444 * caches first, otherwise the data may be inconsistent with the server.
3445 */
3446 truncate_pagecache_range(inode, offset, offset + len - 1);
3447
David Brazdil0f672f62019-12-10 10:32:29 +00003448 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003449
3450 fsctl_buf.FileOffset = cpu_to_le64(offset);
3451 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3452
3453 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3454 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3455 true /* is_fctl */, (char *)&fsctl_buf,
David Brazdil0f672f62019-12-10 10:32:29 +00003456 sizeof(struct file_zero_data_information),
3457 CIFSMaxBufSize, NULL, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003458 free_xid(xid);
3459 return rc;
3460}
3461
Olivier Deprez157378f2022-04-04 15:47:50 +02003462static int smb3_simple_fallocate_write_range(unsigned int xid,
3463 struct cifs_tcon *tcon,
3464 struct cifsFileInfo *cfile,
3465 loff_t off, loff_t len,
3466 char *buf)
3467{
3468 struct cifs_io_parms io_parms = {0};
3469 int nbytes;
3470 int rc = 0;
3471 struct kvec iov[2];
3472
3473 io_parms.netfid = cfile->fid.netfid;
3474 io_parms.pid = current->tgid;
3475 io_parms.tcon = tcon;
3476 io_parms.persistent_fid = cfile->fid.persistent_fid;
3477 io_parms.volatile_fid = cfile->fid.volatile_fid;
3478
3479 while (len) {
3480 io_parms.offset = off;
3481 io_parms.length = len;
3482 if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3483 io_parms.length = SMB2_MAX_BUFFER_SIZE;
3484 /* iov[0] is reserved for smb header */
3485 iov[1].iov_base = buf;
3486 iov[1].iov_len = io_parms.length;
3487 rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3488 if (rc)
3489 break;
3490 if (nbytes > len)
3491 return -EINVAL;
3492 buf += nbytes;
3493 off += nbytes;
3494 len -= nbytes;
3495 }
3496 return rc;
3497}
3498
3499static int smb3_simple_fallocate_range(unsigned int xid,
3500 struct cifs_tcon *tcon,
3501 struct cifsFileInfo *cfile,
3502 loff_t off, loff_t len)
3503{
3504 struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3505 u32 out_data_len;
3506 char *buf = NULL;
3507 loff_t l;
3508 int rc;
3509
3510 in_data.file_offset = cpu_to_le64(off);
3511 in_data.length = cpu_to_le64(len);
3512 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3513 cfile->fid.volatile_fid,
3514 FSCTL_QUERY_ALLOCATED_RANGES, true,
3515 (char *)&in_data, sizeof(in_data),
3516 1024 * sizeof(struct file_allocated_range_buffer),
3517 (char **)&out_data, &out_data_len);
3518 if (rc)
3519 goto out;
3520
3521 buf = kzalloc(1024 * 1024, GFP_KERNEL);
3522 if (buf == NULL) {
3523 rc = -ENOMEM;
3524 goto out;
3525 }
3526
3527 tmp_data = out_data;
3528 while (len) {
3529 /*
3530 * The rest of the region is unmapped so write it all.
3531 */
3532 if (out_data_len == 0) {
3533 rc = smb3_simple_fallocate_write_range(xid, tcon,
3534 cfile, off, len, buf);
3535 goto out;
3536 }
3537
3538 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3539 rc = -EINVAL;
3540 goto out;
3541 }
3542
3543 if (off < le64_to_cpu(tmp_data->file_offset)) {
3544 /*
3545 * We are at a hole. Write until the end of the region
3546 * or until the next allocated data,
3547 * whichever comes next.
3548 */
3549 l = le64_to_cpu(tmp_data->file_offset) - off;
3550 if (len < l)
3551 l = len;
3552 rc = smb3_simple_fallocate_write_range(xid, tcon,
3553 cfile, off, l, buf);
3554 if (rc)
3555 goto out;
3556 off = off + l;
3557 len = len - l;
3558 if (len == 0)
3559 goto out;
3560 }
3561 /*
3562 * We are at a section of allocated data, just skip forward
3563 * until the end of the data or the end of the region
3564 * we are supposed to fallocate, whichever comes first.
3565 */
3566 l = le64_to_cpu(tmp_data->length);
3567 if (len < l)
3568 l = len;
3569 off += l;
3570 len -= l;
3571
3572 tmp_data = &tmp_data[1];
3573 out_data_len -= sizeof(struct file_allocated_range_buffer);
3574 }
3575
3576 out:
3577 kfree(out_data);
3578 kfree(buf);
3579 return rc;
3580}
3581
3582
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003583static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3584 loff_t off, loff_t len, bool keep_size)
3585{
3586 struct inode *inode;
3587 struct cifsInodeInfo *cifsi;
3588 struct cifsFileInfo *cfile = file->private_data;
3589 long rc = -EOPNOTSUPP;
3590 unsigned int xid;
David Brazdil0f672f62019-12-10 10:32:29 +00003591 __le64 eof;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003592
3593 xid = get_xid();
3594
3595 inode = d_inode(cfile->dentry);
3596 cifsi = CIFS_I(inode);
3597
David Brazdil0f672f62019-12-10 10:32:29 +00003598 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3599 tcon->ses->Suid, off, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003600 /* if file not oplocked can't be sure whether asking to extend size */
3601 if (!CIFS_CACHE_READ(cifsi))
3602 if (keep_size == false) {
David Brazdil0f672f62019-12-10 10:32:29 +00003603 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3604 tcon->tid, tcon->ses->Suid, off, len, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003605 free_xid(xid);
3606 return rc;
3607 }
3608
3609 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02003610 * Extending the file
3611 */
3612 if ((keep_size == false) && i_size_read(inode) < off + len) {
3613 rc = inode_newsize_ok(inode, off + len);
3614 if (rc)
3615 goto out;
3616
3617 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3618 smb2_set_sparse(xid, tcon, cfile, inode, false);
3619
3620 eof = cpu_to_le64(off + len);
3621 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3622 cfile->fid.volatile_fid, cfile->pid, &eof);
3623 if (rc == 0) {
3624 cifsi->server_eof = off + len;
3625 cifs_setsize(inode, off + len);
3626 cifs_truncate_page(inode->i_mapping, inode->i_size);
3627 truncate_setsize(inode, off + len);
3628 }
3629 goto out;
3630 }
3631
3632 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003633 * Files are non-sparse by default so falloc may be a no-op
Olivier Deprez157378f2022-04-04 15:47:50 +02003634 * Must check if file sparse. If not sparse, and since we are not
3635 * extending then no need to do anything since file already allocated
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003636 */
3637 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003638 rc = 0;
3639 goto out;
3640 }
3641
3642 if (keep_size == true) {
3643 /*
3644 * We can not preallocate pages beyond the end of the file
3645 * in SMB2
3646 */
3647 if (off >= i_size_read(inode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003648 rc = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02003649 goto out;
3650 }
3651 /*
3652 * For fallocates that are partially beyond the end of file,
3653 * clamp len so we only fallocate up to the end of file.
3654 */
3655 if (off + len > i_size_read(inode)) {
3656 len = i_size_read(inode) - off;
3657 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003658 }
3659
3660 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3661 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02003662 * At this point, we are trying to fallocate an internal
3663 * regions of a sparse file. Since smb2 does not have a
3664 * fallocate command we have two otions on how to emulate this.
3665 * We can either turn the entire file to become non-sparse
3666 * which we only do if the fallocate is for virtually
3667 * the whole file, or we can overwrite the region with zeroes
3668 * using SMB2_write, which could be prohibitevly expensive
3669 * if len is large.
3670 */
3671 /*
3672 * We are only trying to fallocate a small region so
3673 * just write it with zero.
3674 */
3675 if (len <= 1024 * 1024) {
3676 rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3677 off, len);
3678 goto out;
3679 }
3680
3681 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003682 * Check if falloc starts within first few pages of file
3683 * and ends within a few pages of the end of file to
3684 * ensure that most of file is being forced to be
3685 * fallocated now. If so then setting whole file sparse
3686 * ie potentially making a few extra pages at the beginning
3687 * or end of the file non-sparse via set_sparse is harmless.
3688 */
3689 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3690 rc = -EOPNOTSUPP;
Olivier Deprez157378f2022-04-04 15:47:50 +02003691 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00003692 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003693 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003694
Olivier Deprez157378f2022-04-04 15:47:50 +02003695 smb2_set_sparse(xid, tcon, cfile, inode, false);
3696 rc = 0;
3697
3698out:
David Brazdil0f672f62019-12-10 10:32:29 +00003699 if (rc)
3700 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3701 tcon->ses->Suid, off, len, rc);
3702 else
3703 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3704 tcon->ses->Suid, off, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003705
3706 free_xid(xid);
3707 return rc;
3708}
3709
David Brazdil0f672f62019-12-10 10:32:29 +00003710static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3711{
3712 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3713 struct cifsInodeInfo *cifsi;
3714 struct inode *inode;
3715 int rc = 0;
3716 struct file_allocated_range_buffer in_data, *out_data = NULL;
3717 u32 out_data_len;
3718 unsigned int xid;
3719
3720 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3721 return generic_file_llseek(file, offset, whence);
3722
3723 inode = d_inode(cfile->dentry);
3724 cifsi = CIFS_I(inode);
3725
3726 if (offset < 0 || offset >= i_size_read(inode))
3727 return -ENXIO;
3728
3729 xid = get_xid();
3730 /*
3731 * We need to be sure that all dirty pages are written as they
3732 * might fill holes on the server.
3733 * Note that we also MUST flush any written pages since at least
3734 * some servers (Windows2016) will not reflect recent writes in
3735 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3736 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003737 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
David Brazdil0f672f62019-12-10 10:32:29 +00003738 if (wrcfile) {
3739 filemap_write_and_wait(inode->i_mapping);
3740 smb2_flush_file(xid, tcon, &wrcfile->fid);
3741 cifsFileInfo_put(wrcfile);
3742 }
3743
3744 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3745 if (whence == SEEK_HOLE)
3746 offset = i_size_read(inode);
3747 goto lseek_exit;
3748 }
3749
3750 in_data.file_offset = cpu_to_le64(offset);
3751 in_data.length = cpu_to_le64(i_size_read(inode));
3752
3753 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3754 cfile->fid.volatile_fid,
3755 FSCTL_QUERY_ALLOCATED_RANGES, true,
3756 (char *)&in_data, sizeof(in_data),
3757 sizeof(struct file_allocated_range_buffer),
3758 (char **)&out_data, &out_data_len);
3759 if (rc == -E2BIG)
3760 rc = 0;
3761 if (rc)
3762 goto lseek_exit;
3763
3764 if (whence == SEEK_HOLE && out_data_len == 0)
3765 goto lseek_exit;
3766
3767 if (whence == SEEK_DATA && out_data_len == 0) {
3768 rc = -ENXIO;
3769 goto lseek_exit;
3770 }
3771
3772 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3773 rc = -EINVAL;
3774 goto lseek_exit;
3775 }
3776 if (whence == SEEK_DATA) {
3777 offset = le64_to_cpu(out_data->file_offset);
3778 goto lseek_exit;
3779 }
3780 if (offset < le64_to_cpu(out_data->file_offset))
3781 goto lseek_exit;
3782
3783 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3784
3785 lseek_exit:
3786 free_xid(xid);
3787 kfree(out_data);
3788 if (!rc)
3789 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3790 else
3791 return rc;
3792}
3793
3794static int smb3_fiemap(struct cifs_tcon *tcon,
3795 struct cifsFileInfo *cfile,
3796 struct fiemap_extent_info *fei, u64 start, u64 len)
3797{
3798 unsigned int xid;
3799 struct file_allocated_range_buffer in_data, *out_data;
3800 u32 out_data_len;
3801 int i, num, rc, flags, last_blob;
3802 u64 next;
3803
Olivier Deprez157378f2022-04-04 15:47:50 +02003804 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3805 if (rc)
3806 return rc;
David Brazdil0f672f62019-12-10 10:32:29 +00003807
3808 xid = get_xid();
3809 again:
3810 in_data.file_offset = cpu_to_le64(start);
3811 in_data.length = cpu_to_le64(len);
3812
3813 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3814 cfile->fid.volatile_fid,
3815 FSCTL_QUERY_ALLOCATED_RANGES, true,
3816 (char *)&in_data, sizeof(in_data),
3817 1024 * sizeof(struct file_allocated_range_buffer),
3818 (char **)&out_data, &out_data_len);
3819 if (rc == -E2BIG) {
3820 last_blob = 0;
3821 rc = 0;
3822 } else
3823 last_blob = 1;
3824 if (rc)
3825 goto out;
3826
Olivier Deprez0e641232021-09-23 10:07:05 +02003827 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003828 rc = -EINVAL;
3829 goto out;
3830 }
3831 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3832 rc = -EINVAL;
3833 goto out;
3834 }
3835
3836 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3837 for (i = 0; i < num; i++) {
3838 flags = 0;
3839 if (i == num - 1 && last_blob)
3840 flags |= FIEMAP_EXTENT_LAST;
3841
3842 rc = fiemap_fill_next_extent(fei,
3843 le64_to_cpu(out_data[i].file_offset),
3844 le64_to_cpu(out_data[i].file_offset),
3845 le64_to_cpu(out_data[i].length),
3846 flags);
3847 if (rc < 0)
3848 goto out;
3849 if (rc == 1) {
3850 rc = 0;
3851 goto out;
3852 }
3853 }
3854
3855 if (!last_blob) {
3856 next = le64_to_cpu(out_data[num - 1].file_offset) +
3857 le64_to_cpu(out_data[num - 1].length);
3858 len = len - (next - start);
3859 start = next;
3860 goto again;
3861 }
3862
3863 out:
3864 free_xid(xid);
3865 kfree(out_data);
3866 return rc;
3867}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003868
3869static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3870 loff_t off, loff_t len)
3871{
3872 /* KEEP_SIZE already checked for by do_fallocate */
3873 if (mode & FALLOC_FL_PUNCH_HOLE)
3874 return smb3_punch_hole(file, tcon, off, len);
3875 else if (mode & FALLOC_FL_ZERO_RANGE) {
3876 if (mode & FALLOC_FL_KEEP_SIZE)
3877 return smb3_zero_range(file, tcon, off, len, true);
3878 return smb3_zero_range(file, tcon, off, len, false);
3879 } else if (mode == FALLOC_FL_KEEP_SIZE)
3880 return smb3_simple_falloc(file, tcon, off, len, true);
3881 else if (mode == 0)
3882 return smb3_simple_falloc(file, tcon, off, len, false);
3883
3884 return -EOPNOTSUPP;
3885}
3886
3887static void
3888smb2_downgrade_oplock(struct TCP_Server_Info *server,
Olivier Deprez0e641232021-09-23 10:07:05 +02003889 struct cifsInodeInfo *cinode, __u32 oplock,
3890 unsigned int epoch, bool *purge_cache)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003891{
Olivier Deprez0e641232021-09-23 10:07:05 +02003892 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003893}
3894
3895static void
Olivier Deprez0e641232021-09-23 10:07:05 +02003896smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3897 unsigned int epoch, bool *purge_cache);
3898
3899static void
3900smb3_downgrade_oplock(struct TCP_Server_Info *server,
3901 struct cifsInodeInfo *cinode, __u32 oplock,
3902 unsigned int epoch, bool *purge_cache)
David Brazdil0f672f62019-12-10 10:32:29 +00003903{
Olivier Deprez0e641232021-09-23 10:07:05 +02003904 unsigned int old_state = cinode->oplock;
3905 unsigned int old_epoch = cinode->epoch;
3906 unsigned int new_state;
3907
3908 if (epoch > old_epoch) {
3909 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3910 cinode->epoch = epoch;
3911 }
3912
3913 new_state = cinode->oplock;
3914 *purge_cache = false;
3915
3916 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3917 (new_state & CIFS_CACHE_READ_FLG) == 0)
3918 *purge_cache = true;
3919 else if (old_state == new_state && (epoch - old_epoch > 1))
3920 *purge_cache = true;
David Brazdil0f672f62019-12-10 10:32:29 +00003921}
3922
3923static void
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003924smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3925 unsigned int epoch, bool *purge_cache)
3926{
3927 oplock &= 0xFF;
3928 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3929 return;
3930 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3931 cinode->oplock = CIFS_CACHE_RHW_FLG;
3932 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3933 &cinode->vfs_inode);
3934 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3935 cinode->oplock = CIFS_CACHE_RW_FLG;
3936 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3937 &cinode->vfs_inode);
3938 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3939 cinode->oplock = CIFS_CACHE_READ_FLG;
3940 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3941 &cinode->vfs_inode);
3942 } else
3943 cinode->oplock = 0;
3944}
3945
3946static void
3947smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3948 unsigned int epoch, bool *purge_cache)
3949{
3950 char message[5] = {0};
David Brazdil0f672f62019-12-10 10:32:29 +00003951 unsigned int new_oplock = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003952
3953 oplock &= 0xFF;
3954 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3955 return;
3956
David Brazdil0f672f62019-12-10 10:32:29 +00003957 /* Check if the server granted an oplock rather than a lease */
3958 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3959 return smb2_set_oplock_level(cinode, oplock, epoch,
3960 purge_cache);
3961
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003962 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
David Brazdil0f672f62019-12-10 10:32:29 +00003963 new_oplock |= CIFS_CACHE_READ_FLG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003964 strcat(message, "R");
3965 }
3966 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
David Brazdil0f672f62019-12-10 10:32:29 +00003967 new_oplock |= CIFS_CACHE_HANDLE_FLG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003968 strcat(message, "H");
3969 }
3970 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
David Brazdil0f672f62019-12-10 10:32:29 +00003971 new_oplock |= CIFS_CACHE_WRITE_FLG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003972 strcat(message, "W");
3973 }
David Brazdil0f672f62019-12-10 10:32:29 +00003974 if (!new_oplock)
3975 strncpy(message, "None", sizeof(message));
3976
3977 cinode->oplock = new_oplock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003978 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3979 &cinode->vfs_inode);
3980}
3981
3982static void
3983smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3984 unsigned int epoch, bool *purge_cache)
3985{
3986 unsigned int old_oplock = cinode->oplock;
3987
3988 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3989
3990 if (purge_cache) {
3991 *purge_cache = false;
3992 if (old_oplock == CIFS_CACHE_READ_FLG) {
3993 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3994 (epoch - cinode->epoch > 0))
3995 *purge_cache = true;
3996 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3997 (epoch - cinode->epoch > 1))
3998 *purge_cache = true;
3999 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4000 (epoch - cinode->epoch > 1))
4001 *purge_cache = true;
4002 else if (cinode->oplock == 0 &&
4003 (epoch - cinode->epoch > 0))
4004 *purge_cache = true;
4005 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
4006 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4007 (epoch - cinode->epoch > 0))
4008 *purge_cache = true;
4009 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4010 (epoch - cinode->epoch > 1))
4011 *purge_cache = true;
4012 }
4013 cinode->epoch = epoch;
4014 }
4015}
4016
4017static bool
4018smb2_is_read_op(__u32 oplock)
4019{
4020 return oplock == SMB2_OPLOCK_LEVEL_II;
4021}
4022
4023static bool
4024smb21_is_read_op(__u32 oplock)
4025{
4026 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
4027 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
4028}
4029
4030static __le32
4031map_oplock_to_lease(u8 oplock)
4032{
4033 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4034 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
4035 else if (oplock == SMB2_OPLOCK_LEVEL_II)
4036 return SMB2_LEASE_READ_CACHING;
4037 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
4038 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
4039 SMB2_LEASE_WRITE_CACHING;
4040 return 0;
4041}
4042
4043static char *
4044smb2_create_lease_buf(u8 *lease_key, u8 oplock)
4045{
4046 struct create_lease *buf;
4047
4048 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
4049 if (!buf)
4050 return NULL;
4051
4052 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4053 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4054
4055 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4056 (struct create_lease, lcontext));
4057 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4058 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4059 (struct create_lease, Name));
4060 buf->ccontext.NameLength = cpu_to_le16(4);
4061 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4062 buf->Name[0] = 'R';
4063 buf->Name[1] = 'q';
4064 buf->Name[2] = 'L';
4065 buf->Name[3] = 's';
4066 return (char *)buf;
4067}
4068
4069static char *
4070smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4071{
4072 struct create_lease_v2 *buf;
4073
4074 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4075 if (!buf)
4076 return NULL;
4077
4078 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4079 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4080
4081 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4082 (struct create_lease_v2, lcontext));
4083 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4084 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4085 (struct create_lease_v2, Name));
4086 buf->ccontext.NameLength = cpu_to_le16(4);
4087 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4088 buf->Name[0] = 'R';
4089 buf->Name[1] = 'q';
4090 buf->Name[2] = 'L';
4091 buf->Name[3] = 's';
4092 return (char *)buf;
4093}
4094
4095static __u8
4096smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4097{
4098 struct create_lease *lc = (struct create_lease *)buf;
4099
4100 *epoch = 0; /* not used */
4101 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4102 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4103 return le32_to_cpu(lc->lcontext.LeaseState);
4104}
4105
4106static __u8
4107smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4108{
4109 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4110
4111 *epoch = le16_to_cpu(lc->lcontext.Epoch);
4112 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4113 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4114 if (lease_key)
4115 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4116 return le32_to_cpu(lc->lcontext.LeaseState);
4117}
4118
4119static unsigned int
4120smb2_wp_retry_size(struct inode *inode)
4121{
4122 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
4123 SMB2_MAX_BUFFER_SIZE);
4124}
4125
4126static bool
4127smb2_dir_needs_close(struct cifsFileInfo *cfile)
4128{
4129 return !cfile->invalidHandle;
4130}
4131
4132static void
4133fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
David Brazdil0f672f62019-12-10 10:32:29 +00004134 struct smb_rqst *old_rq, __le16 cipher_type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004135{
4136 struct smb2_sync_hdr *shdr =
4137 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
4138
4139 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4140 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4141 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4142 tr_hdr->Flags = cpu_to_le16(0x01);
Olivier Deprez157378f2022-04-04 15:47:50 +02004143 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4144 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4145 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
David Brazdil0f672f62019-12-10 10:32:29 +00004146 else
Olivier Deprez157378f2022-04-04 15:47:50 +02004147 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004148 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4149}
4150
4151/* We can not use the normal sg_set_buf() as we will sometimes pass a
4152 * stack object as buf.
4153 */
4154static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
4155 unsigned int buflen)
4156{
David Brazdil0f672f62019-12-10 10:32:29 +00004157 void *addr;
4158 /*
4159 * VMAP_STACK (at least) puts stack into the vmalloc address space
4160 */
4161 if (is_vmalloc_addr(buf))
4162 addr = vmalloc_to_page(buf);
4163 else
4164 addr = virt_to_page(buf);
4165 sg_set_page(sg, addr, buflen, offset_in_page(buf));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004166}
4167
4168/* Assumes the first rqst has a transform header as the first iov.
4169 * I.e.
4170 * rqst[0].rq_iov[0] is transform header
4171 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4172 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
4173 */
4174static struct scatterlist *
4175init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
4176{
4177 unsigned int sg_len;
4178 struct scatterlist *sg;
4179 unsigned int i;
4180 unsigned int j;
4181 unsigned int idx = 0;
4182 int skip;
4183
4184 sg_len = 1;
4185 for (i = 0; i < num_rqst; i++)
4186 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
4187
4188 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4189 if (!sg)
4190 return NULL;
4191
4192 sg_init_table(sg, sg_len);
4193 for (i = 0; i < num_rqst; i++) {
4194 for (j = 0; j < rqst[i].rq_nvec; j++) {
4195 /*
4196 * The first rqst has a transform header where the
4197 * first 20 bytes are not part of the encrypted blob
4198 */
4199 skip = (i == 0) && (j == 0) ? 20 : 0;
4200 smb2_sg_set_buf(&sg[idx++],
4201 rqst[i].rq_iov[j].iov_base + skip,
4202 rqst[i].rq_iov[j].iov_len - skip);
David Brazdil0f672f62019-12-10 10:32:29 +00004203 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004204
4205 for (j = 0; j < rqst[i].rq_npages; j++) {
4206 unsigned int len, offset;
4207
4208 rqst_page_get_length(&rqst[i], j, &len, &offset);
4209 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4210 }
4211 }
4212 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
4213 return sg;
4214}
4215
4216static int
4217smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4218{
4219 struct cifs_ses *ses;
4220 u8 *ses_enc_key;
4221
4222 spin_lock(&cifs_tcp_ses_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02004223 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4224 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4225 if (ses->Suid == ses_id) {
4226 ses_enc_key = enc ? ses->smb3encryptionkey :
4227 ses->smb3decryptionkey;
4228 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4229 spin_unlock(&cifs_tcp_ses_lock);
4230 return 0;
4231 }
4232 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004233 }
4234 spin_unlock(&cifs_tcp_ses_lock);
4235
Olivier Deprez0e641232021-09-23 10:07:05 +02004236 return -EAGAIN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004237}
4238/*
4239 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4240 * iov[0] - transform header (associate data),
4241 * iov[1-N] - SMB2 header and pages - data to encrypt.
4242 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4243 * untouched.
4244 */
4245static int
4246crypt_message(struct TCP_Server_Info *server, int num_rqst,
4247 struct smb_rqst *rqst, int enc)
4248{
4249 struct smb2_transform_hdr *tr_hdr =
4250 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4251 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4252 int rc = 0;
4253 struct scatterlist *sg;
4254 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Olivier Deprez157378f2022-04-04 15:47:50 +02004255 u8 key[SMB3_ENC_DEC_KEY_SIZE];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004256 struct aead_request *req;
4257 char *iv;
4258 unsigned int iv_len;
4259 DECLARE_CRYPTO_WAIT(wait);
4260 struct crypto_aead *tfm;
4261 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4262
4263 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4264 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00004265 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004266 enc ? "en" : "de");
Olivier Deprez0e641232021-09-23 10:07:05 +02004267 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004268 }
4269
4270 rc = smb3_crypto_aead_allocate(server);
4271 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00004272 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004273 return rc;
4274 }
4275
4276 tfm = enc ? server->secmech.ccmaesencrypt :
4277 server->secmech.ccmaesdecrypt;
Olivier Deprez157378f2022-04-04 15:47:50 +02004278
4279 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4280 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4281 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4282 else
4283 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4284
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004285 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00004286 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004287 return rc;
4288 }
4289
4290 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4291 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00004292 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004293 return rc;
4294 }
4295
4296 req = aead_request_alloc(tfm, GFP_KERNEL);
4297 if (!req) {
David Brazdil0f672f62019-12-10 10:32:29 +00004298 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004299 return -ENOMEM;
4300 }
4301
4302 if (!enc) {
4303 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4304 crypt_len += SMB2_SIGNATURE_SIZE;
4305 }
4306
4307 sg = init_sg(num_rqst, rqst, sign);
4308 if (!sg) {
David Brazdil0f672f62019-12-10 10:32:29 +00004309 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004310 rc = -ENOMEM;
4311 goto free_req;
4312 }
4313
4314 iv_len = crypto_aead_ivsize(tfm);
4315 iv = kzalloc(iv_len, GFP_KERNEL);
4316 if (!iv) {
David Brazdil0f672f62019-12-10 10:32:29 +00004317 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004318 rc = -ENOMEM;
4319 goto free_sg;
4320 }
David Brazdil0f672f62019-12-10 10:32:29 +00004321
Olivier Deprez157378f2022-04-04 15:47:50 +02004322 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4323 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4324 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
David Brazdil0f672f62019-12-10 10:32:29 +00004325 else {
4326 iv[0] = 3;
Olivier Deprez157378f2022-04-04 15:47:50 +02004327 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
David Brazdil0f672f62019-12-10 10:32:29 +00004328 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004329
4330 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4331 aead_request_set_ad(req, assoc_data_len);
4332
4333 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4334 crypto_req_done, &wait);
4335
4336 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4337 : crypto_aead_decrypt(req), &wait);
4338
4339 if (!rc && enc)
4340 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4341
4342 kfree(iv);
4343free_sg:
4344 kfree(sg);
4345free_req:
4346 kfree(req);
4347 return rc;
4348}
4349
4350void
4351smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4352{
4353 int i, j;
4354
4355 for (i = 0; i < num_rqst; i++) {
4356 if (rqst[i].rq_pages) {
4357 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4358 put_page(rqst[i].rq_pages[j]);
4359 kfree(rqst[i].rq_pages);
4360 }
4361 }
4362}
4363
4364/*
4365 * This function will initialize new_rq and encrypt the content.
4366 * The first entry, new_rq[0], only contains a single iov which contains
4367 * a smb2_transform_hdr and is pre-allocated by the caller.
4368 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4369 *
4370 * The end result is an array of smb_rqst structures where the first structure
4371 * only contains a single iov for the transform header which we then can pass
4372 * to crypt_message().
4373 *
4374 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4375 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4376 */
4377static int
4378smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4379 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4380{
4381 struct page **pages;
4382 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4383 unsigned int npages;
4384 unsigned int orig_len = 0;
4385 int i, j;
4386 int rc = -ENOMEM;
4387
4388 for (i = 1; i < num_rqst; i++) {
4389 npages = old_rq[i - 1].rq_npages;
4390 pages = kmalloc_array(npages, sizeof(struct page *),
4391 GFP_KERNEL);
4392 if (!pages)
4393 goto err_free;
4394
4395 new_rq[i].rq_pages = pages;
4396 new_rq[i].rq_npages = npages;
4397 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4398 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4399 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4400 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4401 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4402
4403 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4404
4405 for (j = 0; j < npages; j++) {
4406 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4407 if (!pages[j])
4408 goto err_free;
4409 }
4410
4411 /* copy pages form the old */
4412 for (j = 0; j < npages; j++) {
4413 char *dst, *src;
4414 unsigned int offset, len;
4415
4416 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4417
4418 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4419 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4420
4421 memcpy(dst, src, len);
4422 kunmap(new_rq[i].rq_pages[j]);
4423 kunmap(old_rq[i - 1].rq_pages[j]);
4424 }
4425 }
4426
4427 /* fill the 1st iov with a transform header */
David Brazdil0f672f62019-12-10 10:32:29 +00004428 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004429
4430 rc = crypt_message(server, num_rqst, new_rq, 1);
David Brazdil0f672f62019-12-10 10:32:29 +00004431 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004432 if (rc)
4433 goto err_free;
4434
4435 return rc;
4436
4437err_free:
4438 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4439 return rc;
4440}
4441
4442static int
4443smb3_is_transform_hdr(void *buf)
4444{
4445 struct smb2_transform_hdr *trhdr = buf;
4446
4447 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4448}
4449
4450static int
4451decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4452 unsigned int buf_data_size, struct page **pages,
Olivier Deprez0e641232021-09-23 10:07:05 +02004453 unsigned int npages, unsigned int page_data_size,
4454 bool is_offloaded)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004455{
4456 struct kvec iov[2];
4457 struct smb_rqst rqst = {NULL};
4458 int rc;
4459
4460 iov[0].iov_base = buf;
4461 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4462 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4463 iov[1].iov_len = buf_data_size;
4464
4465 rqst.rq_iov = iov;
4466 rqst.rq_nvec = 2;
4467 rqst.rq_pages = pages;
4468 rqst.rq_npages = npages;
4469 rqst.rq_pagesz = PAGE_SIZE;
4470 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4471
4472 rc = crypt_message(server, 1, &rqst, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00004473 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004474
4475 if (rc)
4476 return rc;
4477
4478 memmove(buf, iov[1].iov_base, buf_data_size);
4479
Olivier Deprez0e641232021-09-23 10:07:05 +02004480 if (!is_offloaded)
4481 server->total_read = buf_data_size + page_data_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004482
4483 return rc;
4484}
4485
4486static int
4487read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4488 unsigned int npages, unsigned int len)
4489{
4490 int i;
4491 int length;
4492
4493 for (i = 0; i < npages; i++) {
4494 struct page *page = pages[i];
4495 size_t n;
4496
4497 n = len;
4498 if (len >= PAGE_SIZE) {
4499 /* enough data to fill the page */
4500 n = PAGE_SIZE;
4501 len -= n;
4502 } else {
4503 zero_user(page, len, PAGE_SIZE - len);
4504 len = 0;
4505 }
4506 length = cifs_read_page_from_socket(server, page, 0, n);
4507 if (length < 0)
4508 return length;
4509 server->total_read += length;
4510 }
4511
4512 return 0;
4513}
4514
4515static int
4516init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4517 unsigned int cur_off, struct bio_vec **page_vec)
4518{
4519 struct bio_vec *bvec;
4520 int i;
4521
4522 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4523 if (!bvec)
4524 return -ENOMEM;
4525
4526 for (i = 0; i < npages; i++) {
4527 bvec[i].bv_page = pages[i];
4528 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4529 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4530 data_size -= bvec[i].bv_len;
4531 }
4532
4533 if (data_size != 0) {
4534 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4535 kfree(bvec);
4536 return -EIO;
4537 }
4538
4539 *page_vec = bvec;
4540 return 0;
4541}
4542
4543static int
4544handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4545 char *buf, unsigned int buf_len, struct page **pages,
Olivier Deprez0e641232021-09-23 10:07:05 +02004546 unsigned int npages, unsigned int page_data_size,
4547 bool is_offloaded)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004548{
4549 unsigned int data_offset;
4550 unsigned int data_len;
4551 unsigned int cur_off;
4552 unsigned int cur_page_idx;
4553 unsigned int pad_len;
4554 struct cifs_readdata *rdata = mid->callback_data;
4555 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4556 struct bio_vec *bvec = NULL;
4557 struct iov_iter iter;
4558 struct kvec iov;
4559 int length;
4560 bool use_rdma_mr = false;
4561
4562 if (shdr->Command != SMB2_READ) {
David Brazdil0f672f62019-12-10 10:32:29 +00004563 cifs_server_dbg(VFS, "only big read responses are supported\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004564 return -ENOTSUPP;
4565 }
4566
4567 if (server->ops->is_session_expired &&
4568 server->ops->is_session_expired(buf)) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004569 if (!is_offloaded)
4570 cifs_reconnect(server);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004571 return -1;
4572 }
4573
4574 if (server->ops->is_status_pending &&
David Brazdil0f672f62019-12-10 10:32:29 +00004575 server->ops->is_status_pending(buf, server))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004576 return -1;
4577
David Brazdil0f672f62019-12-10 10:32:29 +00004578 /* set up first two iov to get credits */
4579 rdata->iov[0].iov_base = buf;
4580 rdata->iov[0].iov_len = 0;
4581 rdata->iov[1].iov_base = buf;
4582 rdata->iov[1].iov_len =
4583 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4584 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4585 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4586 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4587 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4588
4589 rdata->result = server->ops->map_error(buf, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004590 if (rdata->result != 0) {
4591 cifs_dbg(FYI, "%s: server returned error %d\n",
4592 __func__, rdata->result);
David Brazdil0f672f62019-12-10 10:32:29 +00004593 /* normal error on read response */
Olivier Deprez0e641232021-09-23 10:07:05 +02004594 if (is_offloaded)
4595 mid->mid_state = MID_RESPONSE_RECEIVED;
4596 else
4597 dequeue_mid(mid, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004598 return 0;
4599 }
4600
4601 data_offset = server->ops->read_data_offset(buf);
4602#ifdef CONFIG_CIFS_SMB_DIRECT
4603 use_rdma_mr = rdata->mr;
4604#endif
4605 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4606
4607 if (data_offset < server->vals->read_rsp_size) {
4608 /*
4609 * win2k8 sometimes sends an offset of 0 when the read
4610 * is beyond the EOF. Treat it as if the data starts just after
4611 * the header.
4612 */
4613 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4614 __func__, data_offset);
4615 data_offset = server->vals->read_rsp_size;
4616 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4617 /* data_offset is beyond the end of smallbuf */
4618 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4619 __func__, data_offset);
4620 rdata->result = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02004621 if (is_offloaded)
4622 mid->mid_state = MID_RESPONSE_MALFORMED;
4623 else
4624 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004625 return 0;
4626 }
4627
4628 pad_len = data_offset - server->vals->read_rsp_size;
4629
4630 if (buf_len <= data_offset) {
4631 /* read response payload is in pages */
4632 cur_page_idx = pad_len / PAGE_SIZE;
4633 cur_off = pad_len % PAGE_SIZE;
4634
4635 if (cur_page_idx != 0) {
4636 /* data offset is beyond the 1st page of response */
4637 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4638 __func__, data_offset);
4639 rdata->result = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02004640 if (is_offloaded)
4641 mid->mid_state = MID_RESPONSE_MALFORMED;
4642 else
4643 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004644 return 0;
4645 }
4646
4647 if (data_len > page_data_size - pad_len) {
4648 /* data_len is corrupt -- discard frame */
4649 rdata->result = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02004650 if (is_offloaded)
4651 mid->mid_state = MID_RESPONSE_MALFORMED;
4652 else
4653 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004654 return 0;
4655 }
4656
4657 rdata->result = init_read_bvec(pages, npages, page_data_size,
4658 cur_off, &bvec);
4659 if (rdata->result != 0) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004660 if (is_offloaded)
4661 mid->mid_state = MID_RESPONSE_MALFORMED;
4662 else
4663 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004664 return 0;
4665 }
4666
David Brazdil0f672f62019-12-10 10:32:29 +00004667 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004668 } else if (buf_len >= data_offset + data_len) {
4669 /* read response payload is in buf */
4670 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4671 iov.iov_base = buf + data_offset;
4672 iov.iov_len = data_len;
David Brazdil0f672f62019-12-10 10:32:29 +00004673 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004674 } else {
4675 /* read response payload cannot be in both buf and pages */
4676 WARN_ONCE(1, "buf can not contain only a part of read data");
4677 rdata->result = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02004678 if (is_offloaded)
4679 mid->mid_state = MID_RESPONSE_MALFORMED;
4680 else
4681 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004682 return 0;
4683 }
4684
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004685 length = rdata->copy_into_pages(server, rdata, &iter);
4686
4687 kfree(bvec);
4688
4689 if (length < 0)
4690 return length;
4691
Olivier Deprez0e641232021-09-23 10:07:05 +02004692 if (is_offloaded)
4693 mid->mid_state = MID_RESPONSE_RECEIVED;
4694 else
4695 dequeue_mid(mid, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004696 return length;
4697}
4698
David Brazdil0f672f62019-12-10 10:32:29 +00004699struct smb2_decrypt_work {
4700 struct work_struct decrypt;
4701 struct TCP_Server_Info *server;
4702 struct page **ppages;
4703 char *buf;
4704 unsigned int npages;
4705 unsigned int len;
4706};
4707
4708
4709static void smb2_decrypt_offload(struct work_struct *work)
4710{
4711 struct smb2_decrypt_work *dw = container_of(work,
4712 struct smb2_decrypt_work, decrypt);
4713 int i, rc;
4714 struct mid_q_entry *mid;
4715
4716 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02004717 dw->ppages, dw->npages, dw->len, true);
David Brazdil0f672f62019-12-10 10:32:29 +00004718 if (rc) {
4719 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4720 goto free_pages;
4721 }
4722
4723 dw->server->lstrp = jiffies;
Olivier Deprez0e641232021-09-23 10:07:05 +02004724 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
David Brazdil0f672f62019-12-10 10:32:29 +00004725 if (mid == NULL)
4726 cifs_dbg(FYI, "mid not found\n");
4727 else {
4728 mid->decrypted = true;
4729 rc = handle_read_data(dw->server, mid, dw->buf,
4730 dw->server->vals->read_rsp_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02004731 dw->ppages, dw->npages, dw->len,
4732 true);
4733 if (rc >= 0) {
4734#ifdef CONFIG_CIFS_STATS2
4735 mid->when_received = jiffies;
4736#endif
4737 mid->callback(mid);
4738 } else {
4739 spin_lock(&GlobalMid_Lock);
4740 if (dw->server->tcpStatus == CifsNeedReconnect) {
4741 mid->mid_state = MID_RETRY_NEEDED;
4742 spin_unlock(&GlobalMid_Lock);
4743 mid->callback(mid);
4744 } else {
4745 mid->mid_state = MID_REQUEST_SUBMITTED;
4746 mid->mid_flags &= ~(MID_DELETED);
4747 list_add_tail(&mid->qhead,
4748 &dw->server->pending_mid_q);
4749 spin_unlock(&GlobalMid_Lock);
4750 }
4751 }
David Brazdil0f672f62019-12-10 10:32:29 +00004752 cifs_mid_q_entry_release(mid);
4753 }
4754
4755free_pages:
4756 for (i = dw->npages-1; i >= 0; i--)
4757 put_page(dw->ppages[i]);
4758
4759 kfree(dw->ppages);
4760 cifs_small_buf_release(dw->buf);
4761 kfree(dw);
4762}
4763
4764
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004765static int
David Brazdil0f672f62019-12-10 10:32:29 +00004766receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4767 int *num_mids)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004768{
4769 char *buf = server->smallbuf;
4770 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4771 unsigned int npages;
4772 struct page **pages;
4773 unsigned int len;
4774 unsigned int buflen = server->pdu_size;
4775 int rc;
4776 int i = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00004777 struct smb2_decrypt_work *dw;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004778
David Brazdil0f672f62019-12-10 10:32:29 +00004779 *num_mids = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004780 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4781 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4782
4783 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4784 if (rc < 0)
4785 return rc;
4786 server->total_read += rc;
4787
4788 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4789 server->vals->read_rsp_size;
4790 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4791
4792 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4793 if (!pages) {
4794 rc = -ENOMEM;
4795 goto discard_data;
4796 }
4797
4798 for (; i < npages; i++) {
4799 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4800 if (!pages[i]) {
4801 rc = -ENOMEM;
4802 goto discard_data;
4803 }
4804 }
4805
4806 /* read read data into pages */
4807 rc = read_data_into_pages(server, pages, npages, len);
4808 if (rc)
4809 goto free_pages;
4810
4811 rc = cifs_discard_remaining_data(server);
4812 if (rc)
4813 goto free_pages;
4814
David Brazdil0f672f62019-12-10 10:32:29 +00004815 /*
4816 * For large reads, offload to different thread for better performance,
4817 * use more cores decrypting which can be expensive
4818 */
4819
4820 if ((server->min_offload) && (server->in_flight > 1) &&
4821 (server->pdu_size >= server->min_offload)) {
4822 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4823 if (dw == NULL)
4824 goto non_offloaded_decrypt;
4825
4826 dw->buf = server->smallbuf;
4827 server->smallbuf = (char *)cifs_small_buf_get();
4828
4829 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4830
4831 dw->npages = npages;
4832 dw->server = server;
4833 dw->ppages = pages;
4834 dw->len = len;
4835 queue_work(decrypt_wq, &dw->decrypt);
4836 *num_mids = 0; /* worker thread takes care of finding mid */
4837 return -1;
4838 }
4839
4840non_offloaded_decrypt:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004841 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02004842 pages, npages, len, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004843 if (rc)
4844 goto free_pages;
4845
4846 *mid = smb2_find_mid(server, buf);
4847 if (*mid == NULL)
4848 cifs_dbg(FYI, "mid not found\n");
4849 else {
4850 cifs_dbg(FYI, "mid found\n");
4851 (*mid)->decrypted = true;
4852 rc = handle_read_data(server, *mid, buf,
4853 server->vals->read_rsp_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02004854 pages, npages, len, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004855 }
4856
4857free_pages:
4858 for (i = i - 1; i >= 0; i--)
4859 put_page(pages[i]);
4860 kfree(pages);
4861 return rc;
4862discard_data:
4863 cifs_discard_remaining_data(server);
4864 goto free_pages;
4865}
4866
4867static int
4868receive_encrypted_standard(struct TCP_Server_Info *server,
4869 struct mid_q_entry **mids, char **bufs,
4870 int *num_mids)
4871{
4872 int ret, length;
4873 char *buf = server->smallbuf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004874 struct smb2_sync_hdr *shdr;
4875 unsigned int pdu_length = server->pdu_size;
4876 unsigned int buf_size;
4877 struct mid_q_entry *mid_entry;
4878 int next_is_large;
4879 char *next_buffer = NULL;
4880
4881 *num_mids = 0;
4882
4883 /* switch to large buffer if too big for a small one */
4884 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4885 server->large_buf = true;
4886 memcpy(server->bigbuf, buf, server->total_read);
4887 buf = server->bigbuf;
4888 }
4889
4890 /* now read the rest */
4891 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4892 pdu_length - HEADER_SIZE(server) + 1);
4893 if (length < 0)
4894 return length;
4895 server->total_read += length;
4896
4897 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
Olivier Deprez0e641232021-09-23 10:07:05 +02004898 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004899 if (length)
4900 return length;
4901
4902 next_is_large = server->large_buf;
David Brazdil0f672f62019-12-10 10:32:29 +00004903one_more:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004904 shdr = (struct smb2_sync_hdr *)buf;
4905 if (shdr->NextCommand) {
David Brazdil0f672f62019-12-10 10:32:29 +00004906 if (next_is_large)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004907 next_buffer = (char *)cifs_buf_get();
David Brazdil0f672f62019-12-10 10:32:29 +00004908 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004909 next_buffer = (char *)cifs_small_buf_get();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004910 memcpy(next_buffer,
David Brazdil0f672f62019-12-10 10:32:29 +00004911 buf + le32_to_cpu(shdr->NextCommand),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004912 pdu_length - le32_to_cpu(shdr->NextCommand));
4913 }
4914
4915 mid_entry = smb2_find_mid(server, buf);
4916 if (mid_entry == NULL)
4917 cifs_dbg(FYI, "mid not found\n");
4918 else {
4919 cifs_dbg(FYI, "mid found\n");
4920 mid_entry->decrypted = true;
4921 mid_entry->resp_buf_size = server->pdu_size;
4922 }
4923
4924 if (*num_mids >= MAX_COMPOUND) {
David Brazdil0f672f62019-12-10 10:32:29 +00004925 cifs_server_dbg(VFS, "too many PDUs in compound\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004926 return -1;
4927 }
4928 bufs[*num_mids] = buf;
4929 mids[(*num_mids)++] = mid_entry;
4930
4931 if (mid_entry && mid_entry->handle)
4932 ret = mid_entry->handle(server, mid_entry);
4933 else
4934 ret = cifs_handle_standard(server, mid_entry);
4935
4936 if (ret == 0 && shdr->NextCommand) {
4937 pdu_length -= le32_to_cpu(shdr->NextCommand);
4938 server->large_buf = next_is_large;
4939 if (next_is_large)
David Brazdil0f672f62019-12-10 10:32:29 +00004940 server->bigbuf = buf = next_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004941 else
David Brazdil0f672f62019-12-10 10:32:29 +00004942 server->smallbuf = buf = next_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004943 goto one_more;
David Brazdil0f672f62019-12-10 10:32:29 +00004944 } else if (ret != 0) {
4945 /*
4946 * ret != 0 here means that we didn't get to handle_mid() thus
4947 * server->smallbuf and server->bigbuf are still valid. We need
4948 * to free next_buffer because it is not going to be used
4949 * anywhere.
4950 */
4951 if (next_is_large)
4952 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4953 else
4954 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004955 }
4956
4957 return ret;
4958}
4959
4960static int
4961smb3_receive_transform(struct TCP_Server_Info *server,
4962 struct mid_q_entry **mids, char **bufs, int *num_mids)
4963{
4964 char *buf = server->smallbuf;
4965 unsigned int pdu_length = server->pdu_size;
4966 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4967 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4968
4969 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4970 sizeof(struct smb2_sync_hdr)) {
David Brazdil0f672f62019-12-10 10:32:29 +00004971 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004972 pdu_length);
4973 cifs_reconnect(server);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004974 return -ECONNABORTED;
4975 }
4976
4977 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
David Brazdil0f672f62019-12-10 10:32:29 +00004978 cifs_server_dbg(VFS, "Transform message is broken\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004979 cifs_reconnect(server);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004980 return -ECONNABORTED;
4981 }
4982
4983 /* TODO: add support for compounds containing READ. */
David Brazdil0f672f62019-12-10 10:32:29 +00004984 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4985 return receive_encrypted_read(server, &mids[0], num_mids);
4986 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004987
4988 return receive_encrypted_standard(server, mids, bufs, num_mids);
4989}
4990
4991int
4992smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4993{
4994 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4995
4996 return handle_read_data(server, mid, buf, server->pdu_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02004997 NULL, 0, 0, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004998}
4999
5000static int
5001smb2_next_header(char *buf)
5002{
5003 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
5004 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
5005
5006 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
5007 return sizeof(struct smb2_transform_hdr) +
5008 le32_to_cpu(t_hdr->OriginalMessageSize);
5009
5010 return le32_to_cpu(hdr->NextCommand);
5011}
5012
David Brazdil0f672f62019-12-10 10:32:29 +00005013static int
5014smb2_make_node(unsigned int xid, struct inode *inode,
5015 struct dentry *dentry, struct cifs_tcon *tcon,
5016 char *full_path, umode_t mode, dev_t dev)
5017{
5018 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5019 int rc = -EPERM;
David Brazdil0f672f62019-12-10 10:32:29 +00005020 FILE_ALL_INFO *buf = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02005021 struct cifs_io_parms io_parms = {0};
David Brazdil0f672f62019-12-10 10:32:29 +00005022 __u32 oplock = 0;
5023 struct cifs_fid fid;
5024 struct cifs_open_parms oparms;
5025 unsigned int bytes_written;
5026 struct win_dev *pdev;
5027 struct kvec iov[2];
5028
5029 /*
5030 * Check if mounted with mount parm 'sfu' mount parm.
5031 * SFU emulation should work with all servers, but only
5032 * supports block and char device (no socket & fifo),
5033 * and was used by default in earlier versions of Windows
5034 */
5035 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
5036 goto out;
5037
5038 /*
5039 * TODO: Add ability to create instead via reparse point. Windows (e.g.
5040 * their current NFS server) uses this approach to expose special files
5041 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
5042 */
5043
5044 if (!S_ISCHR(mode) && !S_ISBLK(mode))
5045 goto out;
5046
5047 cifs_dbg(FYI, "sfu compat create special file\n");
5048
5049 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
5050 if (buf == NULL) {
5051 rc = -ENOMEM;
5052 goto out;
5053 }
5054
David Brazdil0f672f62019-12-10 10:32:29 +00005055 oparms.tcon = tcon;
5056 oparms.cifs_sb = cifs_sb;
5057 oparms.desired_access = GENERIC_WRITE;
Olivier Deprez157378f2022-04-04 15:47:50 +02005058 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
5059 CREATE_OPTION_SPECIAL);
David Brazdil0f672f62019-12-10 10:32:29 +00005060 oparms.disposition = FILE_CREATE;
5061 oparms.path = full_path;
5062 oparms.fid = &fid;
5063 oparms.reconnect = false;
5064
5065 if (tcon->ses->server->oplocks)
5066 oplock = REQ_OPLOCK;
5067 else
5068 oplock = 0;
5069 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
5070 if (rc)
5071 goto out;
5072
5073 /*
5074 * BB Do not bother to decode buf since no local inode yet to put
5075 * timestamps in, but we can reuse it safely.
5076 */
5077
5078 pdev = (struct win_dev *)buf;
5079 io_parms.pid = current->tgid;
5080 io_parms.tcon = tcon;
5081 io_parms.offset = 0;
5082 io_parms.length = sizeof(struct win_dev);
5083 iov[1].iov_base = buf;
5084 iov[1].iov_len = sizeof(struct win_dev);
5085 if (S_ISCHR(mode)) {
5086 memcpy(pdev->type, "IntxCHR", 8);
5087 pdev->major = cpu_to_le64(MAJOR(dev));
5088 pdev->minor = cpu_to_le64(MINOR(dev));
5089 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5090 &bytes_written, iov, 1);
5091 } else if (S_ISBLK(mode)) {
5092 memcpy(pdev->type, "IntxBLK", 8);
5093 pdev->major = cpu_to_le64(MAJOR(dev));
5094 pdev->minor = cpu_to_le64(MINOR(dev));
5095 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5096 &bytes_written, iov, 1);
5097 }
5098 tcon->ses->server->ops->close(xid, tcon, &fid);
5099 d_drop(dentry);
5100
5101 /* FIXME: add code here to set EAs */
5102out:
5103 kfree(buf);
5104 return rc;
5105}
5106
5107
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005108struct smb_version_operations smb20_operations = {
5109 .compare_fids = smb2_compare_fids,
5110 .setup_request = smb2_setup_request,
5111 .setup_async_request = smb2_setup_async_request,
5112 .check_receive = smb2_check_receive,
5113 .add_credits = smb2_add_credits,
5114 .set_credits = smb2_set_credits,
5115 .get_credits_field = smb2_get_credits_field,
5116 .get_credits = smb2_get_credits,
5117 .wait_mtu_credits = cifs_wait_mtu_credits,
5118 .get_next_mid = smb2_get_next_mid,
David Brazdil0f672f62019-12-10 10:32:29 +00005119 .revert_current_mid = smb2_revert_current_mid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005120 .read_data_offset = smb2_read_data_offset,
5121 .read_data_length = smb2_read_data_length,
5122 .map_error = map_smb2_to_linux_error,
5123 .find_mid = smb2_find_mid,
5124 .check_message = smb2_check_message,
5125 .dump_detail = smb2_dump_detail,
5126 .clear_stats = smb2_clear_stats,
5127 .print_stats = smb2_print_stats,
5128 .is_oplock_break = smb2_is_valid_oplock_break,
5129 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5130 .downgrade_oplock = smb2_downgrade_oplock,
5131 .need_neg = smb2_need_neg,
5132 .negotiate = smb2_negotiate,
5133 .negotiate_wsize = smb2_negotiate_wsize,
5134 .negotiate_rsize = smb2_negotiate_rsize,
5135 .sess_setup = SMB2_sess_setup,
5136 .logoff = SMB2_logoff,
5137 .tree_connect = SMB2_tcon,
5138 .tree_disconnect = SMB2_tdis,
5139 .qfs_tcon = smb2_qfs_tcon,
5140 .is_path_accessible = smb2_is_path_accessible,
5141 .can_echo = smb2_can_echo,
5142 .echo = SMB2_echo,
5143 .query_path_info = smb2_query_path_info,
5144 .get_srv_inum = smb2_get_srv_inum,
5145 .query_file_info = smb2_query_file_info,
5146 .set_path_size = smb2_set_path_size,
5147 .set_file_size = smb2_set_file_size,
5148 .set_file_info = smb2_set_file_info,
5149 .set_compression = smb2_set_compression,
5150 .mkdir = smb2_mkdir,
5151 .mkdir_setinfo = smb2_mkdir_setinfo,
5152 .rmdir = smb2_rmdir,
5153 .unlink = smb2_unlink,
5154 .rename = smb2_rename_path,
5155 .create_hardlink = smb2_create_hardlink,
5156 .query_symlink = smb2_query_symlink,
5157 .query_mf_symlink = smb3_query_mf_symlink,
5158 .create_mf_symlink = smb3_create_mf_symlink,
5159 .open = smb2_open_file,
5160 .set_fid = smb2_set_fid,
5161 .close = smb2_close_file,
5162 .flush = smb2_flush_file,
5163 .async_readv = smb2_async_readv,
5164 .async_writev = smb2_async_writev,
5165 .sync_read = smb2_sync_read,
5166 .sync_write = smb2_sync_write,
5167 .query_dir_first = smb2_query_dir_first,
5168 .query_dir_next = smb2_query_dir_next,
5169 .close_dir = smb2_close_dir,
5170 .calc_smb_size = smb2_calc_size,
5171 .is_status_pending = smb2_is_status_pending,
5172 .is_session_expired = smb2_is_session_expired,
5173 .oplock_response = smb2_oplock_response,
5174 .queryfs = smb2_queryfs,
5175 .mand_lock = smb2_mand_lock,
5176 .mand_unlock_range = smb2_unlock_range,
5177 .push_mand_locks = smb2_push_mandatory_locks,
5178 .get_lease_key = smb2_get_lease_key,
5179 .set_lease_key = smb2_set_lease_key,
5180 .new_lease_key = smb2_new_lease_key,
5181 .calc_signature = smb2_calc_signature,
5182 .is_read_op = smb2_is_read_op,
5183 .set_oplock_level = smb2_set_oplock_level,
5184 .create_lease_buf = smb2_create_lease_buf,
5185 .parse_lease_buf = smb2_parse_lease_buf,
5186 .copychunk_range = smb2_copychunk_range,
5187 .wp_retry_size = smb2_wp_retry_size,
5188 .dir_needs_close = smb2_dir_needs_close,
5189 .get_dfs_refer = smb2_get_dfs_refer,
5190 .select_sectype = smb2_select_sectype,
5191#ifdef CONFIG_CIFS_XATTR
5192 .query_all_EAs = smb2_query_eas,
5193 .set_EA = smb2_set_ea,
5194#endif /* CIFS_XATTR */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005195 .get_acl = get_smb2_acl,
5196 .get_acl_by_fid = get_smb2_acl_by_fid,
5197 .set_acl = set_smb2_acl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005198 .next_header = smb2_next_header,
David Brazdil0f672f62019-12-10 10:32:29 +00005199 .ioctl_query_info = smb2_ioctl_query_info,
5200 .make_node = smb2_make_node,
5201 .fiemap = smb3_fiemap,
5202 .llseek = smb3_llseek,
Olivier Deprez157378f2022-04-04 15:47:50 +02005203 .is_status_io_timeout = smb2_is_status_io_timeout,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005204};
5205
5206struct smb_version_operations smb21_operations = {
5207 .compare_fids = smb2_compare_fids,
5208 .setup_request = smb2_setup_request,
5209 .setup_async_request = smb2_setup_async_request,
5210 .check_receive = smb2_check_receive,
5211 .add_credits = smb2_add_credits,
5212 .set_credits = smb2_set_credits,
5213 .get_credits_field = smb2_get_credits_field,
5214 .get_credits = smb2_get_credits,
5215 .wait_mtu_credits = smb2_wait_mtu_credits,
David Brazdil0f672f62019-12-10 10:32:29 +00005216 .adjust_credits = smb2_adjust_credits,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005217 .get_next_mid = smb2_get_next_mid,
David Brazdil0f672f62019-12-10 10:32:29 +00005218 .revert_current_mid = smb2_revert_current_mid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005219 .read_data_offset = smb2_read_data_offset,
5220 .read_data_length = smb2_read_data_length,
5221 .map_error = map_smb2_to_linux_error,
5222 .find_mid = smb2_find_mid,
5223 .check_message = smb2_check_message,
5224 .dump_detail = smb2_dump_detail,
5225 .clear_stats = smb2_clear_stats,
5226 .print_stats = smb2_print_stats,
5227 .is_oplock_break = smb2_is_valid_oplock_break,
5228 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Olivier Deprez0e641232021-09-23 10:07:05 +02005229 .downgrade_oplock = smb2_downgrade_oplock,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005230 .need_neg = smb2_need_neg,
5231 .negotiate = smb2_negotiate,
5232 .negotiate_wsize = smb2_negotiate_wsize,
5233 .negotiate_rsize = smb2_negotiate_rsize,
5234 .sess_setup = SMB2_sess_setup,
5235 .logoff = SMB2_logoff,
5236 .tree_connect = SMB2_tcon,
5237 .tree_disconnect = SMB2_tdis,
5238 .qfs_tcon = smb2_qfs_tcon,
5239 .is_path_accessible = smb2_is_path_accessible,
5240 .can_echo = smb2_can_echo,
5241 .echo = SMB2_echo,
5242 .query_path_info = smb2_query_path_info,
5243 .get_srv_inum = smb2_get_srv_inum,
5244 .query_file_info = smb2_query_file_info,
5245 .set_path_size = smb2_set_path_size,
5246 .set_file_size = smb2_set_file_size,
5247 .set_file_info = smb2_set_file_info,
5248 .set_compression = smb2_set_compression,
5249 .mkdir = smb2_mkdir,
5250 .mkdir_setinfo = smb2_mkdir_setinfo,
5251 .rmdir = smb2_rmdir,
5252 .unlink = smb2_unlink,
5253 .rename = smb2_rename_path,
5254 .create_hardlink = smb2_create_hardlink,
5255 .query_symlink = smb2_query_symlink,
5256 .query_mf_symlink = smb3_query_mf_symlink,
5257 .create_mf_symlink = smb3_create_mf_symlink,
5258 .open = smb2_open_file,
5259 .set_fid = smb2_set_fid,
5260 .close = smb2_close_file,
5261 .flush = smb2_flush_file,
5262 .async_readv = smb2_async_readv,
5263 .async_writev = smb2_async_writev,
5264 .sync_read = smb2_sync_read,
5265 .sync_write = smb2_sync_write,
5266 .query_dir_first = smb2_query_dir_first,
5267 .query_dir_next = smb2_query_dir_next,
5268 .close_dir = smb2_close_dir,
5269 .calc_smb_size = smb2_calc_size,
5270 .is_status_pending = smb2_is_status_pending,
5271 .is_session_expired = smb2_is_session_expired,
5272 .oplock_response = smb2_oplock_response,
5273 .queryfs = smb2_queryfs,
5274 .mand_lock = smb2_mand_lock,
5275 .mand_unlock_range = smb2_unlock_range,
5276 .push_mand_locks = smb2_push_mandatory_locks,
5277 .get_lease_key = smb2_get_lease_key,
5278 .set_lease_key = smb2_set_lease_key,
5279 .new_lease_key = smb2_new_lease_key,
5280 .calc_signature = smb2_calc_signature,
5281 .is_read_op = smb21_is_read_op,
5282 .set_oplock_level = smb21_set_oplock_level,
5283 .create_lease_buf = smb2_create_lease_buf,
5284 .parse_lease_buf = smb2_parse_lease_buf,
5285 .copychunk_range = smb2_copychunk_range,
5286 .wp_retry_size = smb2_wp_retry_size,
5287 .dir_needs_close = smb2_dir_needs_close,
5288 .enum_snapshots = smb3_enum_snapshots,
Olivier Deprez157378f2022-04-04 15:47:50 +02005289 .notify = smb3_notify,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005290 .get_dfs_refer = smb2_get_dfs_refer,
5291 .select_sectype = smb2_select_sectype,
5292#ifdef CONFIG_CIFS_XATTR
5293 .query_all_EAs = smb2_query_eas,
5294 .set_EA = smb2_set_ea,
5295#endif /* CIFS_XATTR */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005296 .get_acl = get_smb2_acl,
5297 .get_acl_by_fid = get_smb2_acl_by_fid,
5298 .set_acl = set_smb2_acl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005299 .next_header = smb2_next_header,
David Brazdil0f672f62019-12-10 10:32:29 +00005300 .ioctl_query_info = smb2_ioctl_query_info,
5301 .make_node = smb2_make_node,
5302 .fiemap = smb3_fiemap,
5303 .llseek = smb3_llseek,
Olivier Deprez157378f2022-04-04 15:47:50 +02005304 .is_status_io_timeout = smb2_is_status_io_timeout,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005305};
5306
5307struct smb_version_operations smb30_operations = {
5308 .compare_fids = smb2_compare_fids,
5309 .setup_request = smb2_setup_request,
5310 .setup_async_request = smb2_setup_async_request,
5311 .check_receive = smb2_check_receive,
5312 .add_credits = smb2_add_credits,
5313 .set_credits = smb2_set_credits,
5314 .get_credits_field = smb2_get_credits_field,
5315 .get_credits = smb2_get_credits,
5316 .wait_mtu_credits = smb2_wait_mtu_credits,
David Brazdil0f672f62019-12-10 10:32:29 +00005317 .adjust_credits = smb2_adjust_credits,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005318 .get_next_mid = smb2_get_next_mid,
David Brazdil0f672f62019-12-10 10:32:29 +00005319 .revert_current_mid = smb2_revert_current_mid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005320 .read_data_offset = smb2_read_data_offset,
5321 .read_data_length = smb2_read_data_length,
5322 .map_error = map_smb2_to_linux_error,
5323 .find_mid = smb2_find_mid,
5324 .check_message = smb2_check_message,
5325 .dump_detail = smb2_dump_detail,
5326 .clear_stats = smb2_clear_stats,
5327 .print_stats = smb2_print_stats,
5328 .dump_share_caps = smb2_dump_share_caps,
5329 .is_oplock_break = smb2_is_valid_oplock_break,
5330 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Olivier Deprez0e641232021-09-23 10:07:05 +02005331 .downgrade_oplock = smb3_downgrade_oplock,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005332 .need_neg = smb2_need_neg,
5333 .negotiate = smb2_negotiate,
David Brazdil0f672f62019-12-10 10:32:29 +00005334 .negotiate_wsize = smb3_negotiate_wsize,
5335 .negotiate_rsize = smb3_negotiate_rsize,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005336 .sess_setup = SMB2_sess_setup,
5337 .logoff = SMB2_logoff,
5338 .tree_connect = SMB2_tcon,
5339 .tree_disconnect = SMB2_tdis,
5340 .qfs_tcon = smb3_qfs_tcon,
5341 .is_path_accessible = smb2_is_path_accessible,
5342 .can_echo = smb2_can_echo,
5343 .echo = SMB2_echo,
5344 .query_path_info = smb2_query_path_info,
Olivier Deprez157378f2022-04-04 15:47:50 +02005345 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5346 .query_reparse_tag = smb2_query_reparse_tag,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005347 .get_srv_inum = smb2_get_srv_inum,
5348 .query_file_info = smb2_query_file_info,
5349 .set_path_size = smb2_set_path_size,
5350 .set_file_size = smb2_set_file_size,
5351 .set_file_info = smb2_set_file_info,
5352 .set_compression = smb2_set_compression,
5353 .mkdir = smb2_mkdir,
5354 .mkdir_setinfo = smb2_mkdir_setinfo,
5355 .rmdir = smb2_rmdir,
5356 .unlink = smb2_unlink,
5357 .rename = smb2_rename_path,
5358 .create_hardlink = smb2_create_hardlink,
5359 .query_symlink = smb2_query_symlink,
5360 .query_mf_symlink = smb3_query_mf_symlink,
5361 .create_mf_symlink = smb3_create_mf_symlink,
5362 .open = smb2_open_file,
5363 .set_fid = smb2_set_fid,
5364 .close = smb2_close_file,
Olivier Deprez157378f2022-04-04 15:47:50 +02005365 .close_getattr = smb2_close_getattr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005366 .flush = smb2_flush_file,
5367 .async_readv = smb2_async_readv,
5368 .async_writev = smb2_async_writev,
5369 .sync_read = smb2_sync_read,
5370 .sync_write = smb2_sync_write,
5371 .query_dir_first = smb2_query_dir_first,
5372 .query_dir_next = smb2_query_dir_next,
5373 .close_dir = smb2_close_dir,
5374 .calc_smb_size = smb2_calc_size,
5375 .is_status_pending = smb2_is_status_pending,
5376 .is_session_expired = smb2_is_session_expired,
5377 .oplock_response = smb2_oplock_response,
5378 .queryfs = smb2_queryfs,
5379 .mand_lock = smb2_mand_lock,
5380 .mand_unlock_range = smb2_unlock_range,
5381 .push_mand_locks = smb2_push_mandatory_locks,
5382 .get_lease_key = smb2_get_lease_key,
5383 .set_lease_key = smb2_set_lease_key,
5384 .new_lease_key = smb2_new_lease_key,
5385 .generate_signingkey = generate_smb30signingkey,
5386 .calc_signature = smb3_calc_signature,
5387 .set_integrity = smb3_set_integrity,
5388 .is_read_op = smb21_is_read_op,
5389 .set_oplock_level = smb3_set_oplock_level,
5390 .create_lease_buf = smb3_create_lease_buf,
5391 .parse_lease_buf = smb3_parse_lease_buf,
5392 .copychunk_range = smb2_copychunk_range,
5393 .duplicate_extents = smb2_duplicate_extents,
5394 .validate_negotiate = smb3_validate_negotiate,
5395 .wp_retry_size = smb2_wp_retry_size,
5396 .dir_needs_close = smb2_dir_needs_close,
5397 .fallocate = smb3_fallocate,
5398 .enum_snapshots = smb3_enum_snapshots,
Olivier Deprez157378f2022-04-04 15:47:50 +02005399 .notify = smb3_notify,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005400 .init_transform_rq = smb3_init_transform_rq,
5401 .is_transform_hdr = smb3_is_transform_hdr,
5402 .receive_transform = smb3_receive_transform,
5403 .get_dfs_refer = smb2_get_dfs_refer,
5404 .select_sectype = smb2_select_sectype,
5405#ifdef CONFIG_CIFS_XATTR
5406 .query_all_EAs = smb2_query_eas,
5407 .set_EA = smb2_set_ea,
5408#endif /* CIFS_XATTR */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005409 .get_acl = get_smb2_acl,
5410 .get_acl_by_fid = get_smb2_acl_by_fid,
5411 .set_acl = set_smb2_acl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005412 .next_header = smb2_next_header,
David Brazdil0f672f62019-12-10 10:32:29 +00005413 .ioctl_query_info = smb2_ioctl_query_info,
5414 .make_node = smb2_make_node,
5415 .fiemap = smb3_fiemap,
5416 .llseek = smb3_llseek,
Olivier Deprez157378f2022-04-04 15:47:50 +02005417 .is_status_io_timeout = smb2_is_status_io_timeout,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005418};
5419
5420struct smb_version_operations smb311_operations = {
5421 .compare_fids = smb2_compare_fids,
5422 .setup_request = smb2_setup_request,
5423 .setup_async_request = smb2_setup_async_request,
5424 .check_receive = smb2_check_receive,
5425 .add_credits = smb2_add_credits,
5426 .set_credits = smb2_set_credits,
5427 .get_credits_field = smb2_get_credits_field,
5428 .get_credits = smb2_get_credits,
5429 .wait_mtu_credits = smb2_wait_mtu_credits,
David Brazdil0f672f62019-12-10 10:32:29 +00005430 .adjust_credits = smb2_adjust_credits,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005431 .get_next_mid = smb2_get_next_mid,
David Brazdil0f672f62019-12-10 10:32:29 +00005432 .revert_current_mid = smb2_revert_current_mid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005433 .read_data_offset = smb2_read_data_offset,
5434 .read_data_length = smb2_read_data_length,
5435 .map_error = map_smb2_to_linux_error,
5436 .find_mid = smb2_find_mid,
5437 .check_message = smb2_check_message,
5438 .dump_detail = smb2_dump_detail,
5439 .clear_stats = smb2_clear_stats,
5440 .print_stats = smb2_print_stats,
5441 .dump_share_caps = smb2_dump_share_caps,
5442 .is_oplock_break = smb2_is_valid_oplock_break,
5443 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Olivier Deprez0e641232021-09-23 10:07:05 +02005444 .downgrade_oplock = smb3_downgrade_oplock,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005445 .need_neg = smb2_need_neg,
5446 .negotiate = smb2_negotiate,
David Brazdil0f672f62019-12-10 10:32:29 +00005447 .negotiate_wsize = smb3_negotiate_wsize,
5448 .negotiate_rsize = smb3_negotiate_rsize,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005449 .sess_setup = SMB2_sess_setup,
5450 .logoff = SMB2_logoff,
5451 .tree_connect = SMB2_tcon,
5452 .tree_disconnect = SMB2_tdis,
5453 .qfs_tcon = smb3_qfs_tcon,
5454 .is_path_accessible = smb2_is_path_accessible,
5455 .can_echo = smb2_can_echo,
5456 .echo = SMB2_echo,
5457 .query_path_info = smb2_query_path_info,
Olivier Deprez157378f2022-04-04 15:47:50 +02005458 .query_reparse_tag = smb2_query_reparse_tag,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005459 .get_srv_inum = smb2_get_srv_inum,
5460 .query_file_info = smb2_query_file_info,
5461 .set_path_size = smb2_set_path_size,
5462 .set_file_size = smb2_set_file_size,
5463 .set_file_info = smb2_set_file_info,
5464 .set_compression = smb2_set_compression,
5465 .mkdir = smb2_mkdir,
5466 .mkdir_setinfo = smb2_mkdir_setinfo,
5467 .posix_mkdir = smb311_posix_mkdir,
5468 .rmdir = smb2_rmdir,
5469 .unlink = smb2_unlink,
5470 .rename = smb2_rename_path,
5471 .create_hardlink = smb2_create_hardlink,
5472 .query_symlink = smb2_query_symlink,
5473 .query_mf_symlink = smb3_query_mf_symlink,
5474 .create_mf_symlink = smb3_create_mf_symlink,
5475 .open = smb2_open_file,
5476 .set_fid = smb2_set_fid,
5477 .close = smb2_close_file,
Olivier Deprez157378f2022-04-04 15:47:50 +02005478 .close_getattr = smb2_close_getattr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005479 .flush = smb2_flush_file,
5480 .async_readv = smb2_async_readv,
5481 .async_writev = smb2_async_writev,
5482 .sync_read = smb2_sync_read,
5483 .sync_write = smb2_sync_write,
5484 .query_dir_first = smb2_query_dir_first,
5485 .query_dir_next = smb2_query_dir_next,
5486 .close_dir = smb2_close_dir,
5487 .calc_smb_size = smb2_calc_size,
5488 .is_status_pending = smb2_is_status_pending,
5489 .is_session_expired = smb2_is_session_expired,
5490 .oplock_response = smb2_oplock_response,
5491 .queryfs = smb311_queryfs,
5492 .mand_lock = smb2_mand_lock,
5493 .mand_unlock_range = smb2_unlock_range,
5494 .push_mand_locks = smb2_push_mandatory_locks,
5495 .get_lease_key = smb2_get_lease_key,
5496 .set_lease_key = smb2_set_lease_key,
5497 .new_lease_key = smb2_new_lease_key,
5498 .generate_signingkey = generate_smb311signingkey,
5499 .calc_signature = smb3_calc_signature,
5500 .set_integrity = smb3_set_integrity,
5501 .is_read_op = smb21_is_read_op,
5502 .set_oplock_level = smb3_set_oplock_level,
5503 .create_lease_buf = smb3_create_lease_buf,
5504 .parse_lease_buf = smb3_parse_lease_buf,
5505 .copychunk_range = smb2_copychunk_range,
5506 .duplicate_extents = smb2_duplicate_extents,
5507/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5508 .wp_retry_size = smb2_wp_retry_size,
5509 .dir_needs_close = smb2_dir_needs_close,
5510 .fallocate = smb3_fallocate,
5511 .enum_snapshots = smb3_enum_snapshots,
Olivier Deprez157378f2022-04-04 15:47:50 +02005512 .notify = smb3_notify,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005513 .init_transform_rq = smb3_init_transform_rq,
5514 .is_transform_hdr = smb3_is_transform_hdr,
5515 .receive_transform = smb3_receive_transform,
5516 .get_dfs_refer = smb2_get_dfs_refer,
5517 .select_sectype = smb2_select_sectype,
5518#ifdef CONFIG_CIFS_XATTR
5519 .query_all_EAs = smb2_query_eas,
5520 .set_EA = smb2_set_ea,
5521#endif /* CIFS_XATTR */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005522 .get_acl = get_smb2_acl,
5523 .get_acl_by_fid = get_smb2_acl_by_fid,
5524 .set_acl = set_smb2_acl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005525 .next_header = smb2_next_header,
David Brazdil0f672f62019-12-10 10:32:29 +00005526 .ioctl_query_info = smb2_ioctl_query_info,
5527 .make_node = smb2_make_node,
5528 .fiemap = smb3_fiemap,
5529 .llseek = smb3_llseek,
Olivier Deprez157378f2022-04-04 15:47:50 +02005530 .is_status_io_timeout = smb2_is_status_io_timeout,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005531};
5532
5533struct smb_version_values smb20_values = {
5534 .version_string = SMB20_VERSION_STRING,
5535 .protocol_id = SMB20_PROT_ID,
5536 .req_capabilities = 0, /* MBZ */
5537 .large_lock_type = 0,
5538 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5539 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5540 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5541 .header_size = sizeof(struct smb2_sync_hdr),
5542 .header_preamble_size = 0,
5543 .max_header_size = MAX_SMB2_HDR_SIZE,
5544 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5545 .lock_cmd = SMB2_LOCK,
5546 .cap_unix = 0,
5547 .cap_nt_find = SMB2_NT_FIND,
5548 .cap_large_files = SMB2_LARGE_FILES,
5549 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5550 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5551 .create_lease_size = sizeof(struct create_lease),
5552};
5553
5554struct smb_version_values smb21_values = {
5555 .version_string = SMB21_VERSION_STRING,
5556 .protocol_id = SMB21_PROT_ID,
5557 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5558 .large_lock_type = 0,
5559 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5560 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5561 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5562 .header_size = sizeof(struct smb2_sync_hdr),
5563 .header_preamble_size = 0,
5564 .max_header_size = MAX_SMB2_HDR_SIZE,
5565 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5566 .lock_cmd = SMB2_LOCK,
5567 .cap_unix = 0,
5568 .cap_nt_find = SMB2_NT_FIND,
5569 .cap_large_files = SMB2_LARGE_FILES,
5570 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5571 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5572 .create_lease_size = sizeof(struct create_lease),
5573};
5574
5575struct smb_version_values smb3any_values = {
5576 .version_string = SMB3ANY_VERSION_STRING,
5577 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5578 .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,
5579 .large_lock_type = 0,
5580 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5581 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5582 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5583 .header_size = sizeof(struct smb2_sync_hdr),
5584 .header_preamble_size = 0,
5585 .max_header_size = MAX_SMB2_HDR_SIZE,
5586 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5587 .lock_cmd = SMB2_LOCK,
5588 .cap_unix = 0,
5589 .cap_nt_find = SMB2_NT_FIND,
5590 .cap_large_files = SMB2_LARGE_FILES,
5591 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5592 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5593 .create_lease_size = sizeof(struct create_lease_v2),
5594};
5595
5596struct smb_version_values smbdefault_values = {
5597 .version_string = SMBDEFAULT_VERSION_STRING,
5598 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5599 .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,
5600 .large_lock_type = 0,
5601 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5602 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5603 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5604 .header_size = sizeof(struct smb2_sync_hdr),
5605 .header_preamble_size = 0,
5606 .max_header_size = MAX_SMB2_HDR_SIZE,
5607 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5608 .lock_cmd = SMB2_LOCK,
5609 .cap_unix = 0,
5610 .cap_nt_find = SMB2_NT_FIND,
5611 .cap_large_files = SMB2_LARGE_FILES,
5612 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5613 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5614 .create_lease_size = sizeof(struct create_lease_v2),
5615};
5616
5617struct smb_version_values smb30_values = {
5618 .version_string = SMB30_VERSION_STRING,
5619 .protocol_id = SMB30_PROT_ID,
5620 .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,
5621 .large_lock_type = 0,
5622 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5623 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5624 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5625 .header_size = sizeof(struct smb2_sync_hdr),
5626 .header_preamble_size = 0,
5627 .max_header_size = MAX_SMB2_HDR_SIZE,
5628 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5629 .lock_cmd = SMB2_LOCK,
5630 .cap_unix = 0,
5631 .cap_nt_find = SMB2_NT_FIND,
5632 .cap_large_files = SMB2_LARGE_FILES,
5633 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5634 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5635 .create_lease_size = sizeof(struct create_lease_v2),
5636};
5637
5638struct smb_version_values smb302_values = {
5639 .version_string = SMB302_VERSION_STRING,
5640 .protocol_id = SMB302_PROT_ID,
5641 .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,
5642 .large_lock_type = 0,
5643 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5644 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5645 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5646 .header_size = sizeof(struct smb2_sync_hdr),
5647 .header_preamble_size = 0,
5648 .max_header_size = MAX_SMB2_HDR_SIZE,
5649 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5650 .lock_cmd = SMB2_LOCK,
5651 .cap_unix = 0,
5652 .cap_nt_find = SMB2_NT_FIND,
5653 .cap_large_files = SMB2_LARGE_FILES,
5654 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5655 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5656 .create_lease_size = sizeof(struct create_lease_v2),
5657};
5658
5659struct smb_version_values smb311_values = {
5660 .version_string = SMB311_VERSION_STRING,
5661 .protocol_id = SMB311_PROT_ID,
5662 .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,
5663 .large_lock_type = 0,
5664 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5665 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5666 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5667 .header_size = sizeof(struct smb2_sync_hdr),
5668 .header_preamble_size = 0,
5669 .max_header_size = MAX_SMB2_HDR_SIZE,
5670 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5671 .lock_cmd = SMB2_LOCK,
5672 .cap_unix = 0,
5673 .cap_nt_find = SMB2_NT_FIND,
5674 .cap_large_files = SMB2_LARGE_FILES,
5675 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5676 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5677 .create_lease_size = sizeof(struct create_lease_v2),
5678};