blob: 72368b656b33ca67fe85f04321ebb03ecbfa5f3a [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,
Olivier Deprez92d4c212022-12-06 15:05:30 +0100590 FSCTL_QUERY_NETWORK_INTERFACE_INFO,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000591 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) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001003 name_len = (size_t)src->ea_name_length;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001004 value_len = (size_t)le16_to_cpu(src->ea_value_length);
1005
David Brazdil0f672f62019-12-10 10:32:29 +00001006 if (name_len == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001007 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001008
1009 if (src_size < 8 + name_len + 1 + value_len) {
1010 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1011 rc = -EIO;
1012 goto out;
1013 }
1014
Olivier Deprez92d4c212022-12-06 15:05:30 +01001015 name = &src->ea_data[0];
1016 value = &src->ea_data[src->ea_name_length + 1];
1017
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001018 if (ea_name) {
1019 if (ea_name_len == name_len &&
1020 memcmp(ea_name, name, name_len) == 0) {
1021 rc = value_len;
1022 if (dst_size == 0)
1023 goto out;
1024 if (dst_size < value_len) {
1025 rc = -ERANGE;
1026 goto out;
1027 }
1028 memcpy(dst, value, value_len);
1029 goto out;
1030 }
1031 } else {
1032 /* 'user.' plus a terminating null */
1033 user_name_len = 5 + 1 + name_len;
1034
1035 if (buf_size == 0) {
1036 /* skip copy - calc size only */
1037 rc += user_name_len;
1038 } else if (dst_size >= user_name_len) {
1039 dst_size -= user_name_len;
1040 memcpy(dst, "user.", 5);
1041 dst += 5;
1042 memcpy(dst, src->ea_data, name_len);
1043 dst += name_len;
1044 *dst = 0;
1045 ++dst;
1046 rc += user_name_len;
1047 } else {
1048 /* stop before overrun buffer */
1049 rc = -ERANGE;
1050 break;
1051 }
1052 }
1053
1054 if (!src->next_entry_offset)
1055 break;
1056
1057 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1058 /* stop before overrun buffer */
1059 rc = -ERANGE;
1060 break;
1061 }
1062 src_size -= le32_to_cpu(src->next_entry_offset);
1063 src = (void *)((char *)src +
1064 le32_to_cpu(src->next_entry_offset));
1065 }
1066
1067 /* didn't find the named attribute */
1068 if (ea_name)
1069 rc = -ENODATA;
1070
1071out:
1072 return (ssize_t)rc;
1073}
1074
1075static ssize_t
1076smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1077 const unsigned char *path, const unsigned char *ea_name,
1078 char *ea_data, size_t buf_size,
1079 struct cifs_sb_info *cifs_sb)
1080{
1081 int rc;
1082 __le16 *utf16_path;
David Brazdil0f672f62019-12-10 10:32:29 +00001083 struct kvec rsp_iov = {NULL, 0};
1084 int buftype = CIFS_NO_BUFFER;
1085 struct smb2_query_info_rsp *rsp;
1086 struct smb2_file_full_ea_info *info = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001087
1088 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1089 if (!utf16_path)
1090 return -ENOMEM;
1091
David Brazdil0f672f62019-12-10 10:32:29 +00001092 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1093 FILE_READ_EA,
1094 FILE_FULL_EA_INFORMATION,
1095 SMB2_O_INFO_FILE,
1096 CIFSMaxBufSize -
1097 MAX_SMB2_CREATE_RESPONSE_SIZE -
1098 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1099 &rsp_iov, &buftype, cifs_sb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001100 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00001101 /*
1102 * If ea_name is NULL (listxattr) and there are no EAs,
1103 * return 0 as it's not an error. Otherwise, the specified
1104 * ea_name was not found.
1105 */
1106 if (!ea_name && rc == -ENODATA)
1107 rc = 0;
1108 goto qeas_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001109 }
1110
David Brazdil0f672f62019-12-10 10:32:29 +00001111 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1112 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1113 le32_to_cpu(rsp->OutputBufferLength),
1114 &rsp_iov,
1115 sizeof(struct smb2_file_full_ea_info));
1116 if (rc)
1117 goto qeas_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001118
David Brazdil0f672f62019-12-10 10:32:29 +00001119 info = (struct smb2_file_full_ea_info *)(
1120 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1121 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1122 le32_to_cpu(rsp->OutputBufferLength), ea_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001123
David Brazdil0f672f62019-12-10 10:32:29 +00001124 qeas_exit:
1125 kfree(utf16_path);
1126 free_rsp_buf(buftype, rsp_iov.iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001127 return rc;
1128}
1129
1130
1131static int
1132smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1133 const char *path, const char *ea_name, const void *ea_value,
1134 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1135 struct cifs_sb_info *cifs_sb)
1136{
David Brazdil0f672f62019-12-10 10:32:29 +00001137 struct cifs_ses *ses = tcon->ses;
Olivier Deprez157378f2022-04-04 15:47:50 +02001138 struct TCP_Server_Info *server = cifs_pick_channel(ses);
David Brazdil0f672f62019-12-10 10:32:29 +00001139 __le16 *utf16_path = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001140 int ea_name_len = strlen(ea_name);
Olivier Deprez157378f2022-04-04 15:47:50 +02001141 int flags = CIFS_CP_CREATE_CLOSE_OP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001142 int len;
David Brazdil0f672f62019-12-10 10:32:29 +00001143 struct smb_rqst rqst[3];
1144 int resp_buftype[3];
1145 struct kvec rsp_iov[3];
1146 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1147 struct cifs_open_parms oparms;
1148 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1149 struct cifs_fid fid;
1150 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1151 unsigned int size[1];
1152 void *data[1];
1153 struct smb2_file_full_ea_info *ea = NULL;
1154 struct kvec close_iov[1];
Olivier Deprez0e641232021-09-23 10:07:05 +02001155 struct smb2_query_info_rsp *rsp;
1156 int rc, used_len = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001157
1158 if (smb3_encryption_required(tcon))
1159 flags |= CIFS_TRANSFORM_REQ;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001160
1161 if (ea_name_len > 255)
1162 return -EINVAL;
1163
1164 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1165 if (!utf16_path)
1166 return -ENOMEM;
1167
David Brazdil0f672f62019-12-10 10:32:29 +00001168 memset(rqst, 0, sizeof(rqst));
1169 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1170 memset(rsp_iov, 0, sizeof(rsp_iov));
1171
1172 if (ses->server->ops->query_all_EAs) {
1173 if (!ea_value) {
1174 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1175 ea_name, NULL, 0,
1176 cifs_sb);
1177 if (rc == -ENODATA)
1178 goto sea_exit;
Olivier Deprez0e641232021-09-23 10:07:05 +02001179 } else {
1180 /* If we are adding a attribute we should first check
1181 * if there will be enough space available to store
1182 * the new EA. If not we should not add it since we
1183 * would not be able to even read the EAs back.
1184 */
1185 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1186 FILE_READ_EA,
1187 FILE_FULL_EA_INFORMATION,
1188 SMB2_O_INFO_FILE,
1189 CIFSMaxBufSize -
1190 MAX_SMB2_CREATE_RESPONSE_SIZE -
1191 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1192 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1193 if (rc == 0) {
1194 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1195 used_len = le32_to_cpu(rsp->OutputBufferLength);
1196 }
1197 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1198 resp_buftype[1] = CIFS_NO_BUFFER;
1199 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1200 rc = 0;
1201
1202 /* Use a fudge factor of 256 bytes in case we collide
1203 * with a different set_EAs command.
1204 */
1205 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1206 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1207 used_len + ea_name_len + ea_value_len + 1) {
1208 rc = -ENOSPC;
1209 goto sea_exit;
1210 }
David Brazdil0f672f62019-12-10 10:32:29 +00001211 }
1212 }
1213
1214 /* Open */
1215 memset(&open_iov, 0, sizeof(open_iov));
1216 rqst[0].rq_iov = open_iov;
1217 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1218
1219 memset(&oparms, 0, sizeof(oparms));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001220 oparms.tcon = tcon;
1221 oparms.desired_access = FILE_WRITE_EA;
1222 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02001223 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001224 oparms.fid = &fid;
1225 oparms.reconnect = false;
1226
Olivier Deprez157378f2022-04-04 15:47:50 +02001227 rc = SMB2_open_init(tcon, server,
1228 &rqst[0], &oplock, &oparms, utf16_path);
David Brazdil0f672f62019-12-10 10:32:29 +00001229 if (rc)
1230 goto sea_exit;
1231 smb2_set_next_command(tcon, &rqst[0]);
1232
1233
1234 /* Set Info */
1235 memset(&si_iov, 0, sizeof(si_iov));
1236 rqst[1].rq_iov = si_iov;
1237 rqst[1].rq_nvec = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001238
Olivier Deprez0e641232021-09-23 10:07:05 +02001239 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001240 ea = kzalloc(len, GFP_KERNEL);
1241 if (ea == NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +00001242 rc = -ENOMEM;
1243 goto sea_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001244 }
1245
1246 ea->ea_name_length = ea_name_len;
1247 ea->ea_value_length = cpu_to_le16(ea_value_len);
1248 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1249 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1250
David Brazdil0f672f62019-12-10 10:32:29 +00001251 size[0] = len;
1252 data[0] = ea;
1253
Olivier Deprez157378f2022-04-04 15:47:50 +02001254 rc = SMB2_set_info_init(tcon, server,
1255 &rqst[1], COMPOUND_FID,
David Brazdil0f672f62019-12-10 10:32:29 +00001256 COMPOUND_FID, current->tgid,
1257 FILE_FULL_EA_INFORMATION,
1258 SMB2_O_INFO_FILE, 0, data, size);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001259 if (rc)
1260 goto sea_exit;
David Brazdil0f672f62019-12-10 10:32:29 +00001261 smb2_set_next_command(tcon, &rqst[1]);
1262 smb2_set_related(&rqst[1]);
1263
1264
1265 /* Close */
1266 memset(&close_iov, 0, sizeof(close_iov));
1267 rqst[2].rq_iov = close_iov;
1268 rqst[2].rq_nvec = 1;
Olivier Deprez157378f2022-04-04 15:47:50 +02001269 rc = SMB2_close_init(tcon, server,
1270 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001271 if (rc)
1272 goto sea_exit;
David Brazdil0f672f62019-12-10 10:32:29 +00001273 smb2_set_related(&rqst[2]);
1274
Olivier Deprez157378f2022-04-04 15:47:50 +02001275 rc = compound_send_recv(xid, ses, server,
1276 flags, 3, rqst,
David Brazdil0f672f62019-12-10 10:32:29 +00001277 resp_buftype, rsp_iov);
1278 /* no need to bump num_remote_opens because handle immediately closed */
1279
1280 sea_exit:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001281 kfree(ea);
David Brazdil0f672f62019-12-10 10:32:29 +00001282 kfree(utf16_path);
1283 SMB2_open_free(&rqst[0]);
1284 SMB2_set_info_free(&rqst[1]);
1285 SMB2_close_free(&rqst[2]);
1286 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1287 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1288 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001289 return rc;
1290}
1291#endif
1292
1293static bool
1294smb2_can_echo(struct TCP_Server_Info *server)
1295{
1296 return server->echoes;
1297}
1298
1299static void
1300smb2_clear_stats(struct cifs_tcon *tcon)
1301{
1302 int i;
David Brazdil0f672f62019-12-10 10:32:29 +00001303
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001304 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1305 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1306 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1307 }
1308}
1309
1310static void
1311smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1312{
1313 seq_puts(m, "\n\tShare Capabilities:");
1314 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1315 seq_puts(m, " DFS,");
1316 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1317 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1318 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1319 seq_puts(m, " SCALEOUT,");
1320 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1321 seq_puts(m, " CLUSTER,");
1322 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1323 seq_puts(m, " ASYMMETRIC,");
1324 if (tcon->capabilities == 0)
1325 seq_puts(m, " None");
1326 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1327 seq_puts(m, " Aligned,");
1328 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1329 seq_puts(m, " Partition Aligned,");
1330 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1331 seq_puts(m, " SSD,");
1332 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1333 seq_puts(m, " TRIM-support,");
1334
1335 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1336 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1337 if (tcon->perf_sector_size)
1338 seq_printf(m, "\tOptimal sector size: 0x%x",
1339 tcon->perf_sector_size);
1340 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1341}
1342
1343static void
1344smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1345{
1346 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1347 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1348
1349 /*
1350 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1351 * totals (requests sent) since those SMBs are per-session not per tcon
1352 */
1353 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1354 (long long)(tcon->bytes_read),
1355 (long long)(tcon->bytes_written));
David Brazdil0f672f62019-12-10 10:32:29 +00001356 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1357 atomic_read(&tcon->num_local_opens),
1358 atomic_read(&tcon->num_remote_opens));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001359 seq_printf(m, "\nTreeConnects: %d total %d failed",
1360 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1361 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1362 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1363 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1364 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1365 seq_printf(m, "\nCreates: %d total %d failed",
1366 atomic_read(&sent[SMB2_CREATE_HE]),
1367 atomic_read(&failed[SMB2_CREATE_HE]));
1368 seq_printf(m, "\nCloses: %d total %d failed",
1369 atomic_read(&sent[SMB2_CLOSE_HE]),
1370 atomic_read(&failed[SMB2_CLOSE_HE]));
1371 seq_printf(m, "\nFlushes: %d total %d failed",
1372 atomic_read(&sent[SMB2_FLUSH_HE]),
1373 atomic_read(&failed[SMB2_FLUSH_HE]));
1374 seq_printf(m, "\nReads: %d total %d failed",
1375 atomic_read(&sent[SMB2_READ_HE]),
1376 atomic_read(&failed[SMB2_READ_HE]));
1377 seq_printf(m, "\nWrites: %d total %d failed",
1378 atomic_read(&sent[SMB2_WRITE_HE]),
1379 atomic_read(&failed[SMB2_WRITE_HE]));
1380 seq_printf(m, "\nLocks: %d total %d failed",
1381 atomic_read(&sent[SMB2_LOCK_HE]),
1382 atomic_read(&failed[SMB2_LOCK_HE]));
1383 seq_printf(m, "\nIOCTLs: %d total %d failed",
1384 atomic_read(&sent[SMB2_IOCTL_HE]),
1385 atomic_read(&failed[SMB2_IOCTL_HE]));
1386 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1387 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1388 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1389 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1390 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1391 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1392 seq_printf(m, "\nQueryInfos: %d total %d failed",
1393 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1394 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1395 seq_printf(m, "\nSetInfos: %d total %d failed",
1396 atomic_read(&sent[SMB2_SET_INFO_HE]),
1397 atomic_read(&failed[SMB2_SET_INFO_HE]));
1398 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1399 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1400 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1401}
1402
1403static void
1404smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1405{
1406 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1407 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1408
1409 cfile->fid.persistent_fid = fid->persistent_fid;
1410 cfile->fid.volatile_fid = fid->volatile_fid;
Olivier Deprez0e641232021-09-23 10:07:05 +02001411 cfile->fid.access = fid->access;
David Brazdil0f672f62019-12-10 10:32:29 +00001412#ifdef CONFIG_CIFS_DEBUG2
1413 cfile->fid.mid = fid->mid;
1414#endif /* CIFS_DEBUG2 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001415 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1416 &fid->purge_cache);
1417 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1418 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1419}
1420
1421static void
1422smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1423 struct cifs_fid *fid)
1424{
1425 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1426}
1427
Olivier Deprez157378f2022-04-04 15:47:50 +02001428static void
1429smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1430 struct cifsFileInfo *cfile)
1431{
1432 struct smb2_file_network_open_info file_inf;
1433 struct inode *inode;
1434 int rc;
1435
1436 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1437 cfile->fid.volatile_fid, &file_inf);
1438 if (rc)
1439 return;
1440
1441 inode = d_inode(cfile->dentry);
1442
1443 spin_lock(&inode->i_lock);
1444 CIFS_I(inode)->time = jiffies;
1445
1446 /* Creation time should not need to be updated on close */
1447 if (file_inf.LastWriteTime)
1448 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1449 if (file_inf.ChangeTime)
1450 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1451 if (file_inf.LastAccessTime)
1452 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1453
1454 /*
1455 * i_blocks is not related to (i_size / i_blksize),
1456 * but instead 512 byte (2**9) size is required for
1457 * calculating num blocks.
1458 */
1459 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1460 inode->i_blocks =
1461 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1462
1463 /* End of file and Attributes should not have to be updated on close */
1464 spin_unlock(&inode->i_lock);
1465}
1466
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001467static int
1468SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1469 u64 persistent_fid, u64 volatile_fid,
1470 struct copychunk_ioctl *pcchunk)
1471{
1472 int rc;
1473 unsigned int ret_data_len;
1474 struct resume_key_req *res_key;
1475
1476 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
Olivier Deprez92d4c212022-12-06 15:05:30 +01001477 FSCTL_SRV_REQUEST_RESUME_KEY, NULL, 0 /* no input */,
1478 CIFSMaxBufSize, (char **)&res_key, &ret_data_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001479
1480 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00001481 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001482 goto req_res_key_exit;
1483 }
1484 if (ret_data_len < sizeof(struct resume_key_req)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001485 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001486 rc = -EINVAL;
1487 goto req_res_key_exit;
1488 }
1489 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1490
1491req_res_key_exit:
1492 kfree(res_key);
1493 return rc;
1494}
1495
Olivier Deprez157378f2022-04-04 15:47:50 +02001496struct iqi_vars {
David Brazdil0f672f62019-12-10 10:32:29 +00001497 struct smb_rqst rqst[3];
David Brazdil0f672f62019-12-10 10:32:29 +00001498 struct kvec rsp_iov[3];
1499 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
David Brazdil0f672f62019-12-10 10:32:29 +00001500 struct kvec qi_iov[1];
1501 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1502 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1503 struct kvec close_iov[1];
Olivier Deprez157378f2022-04-04 15:47:50 +02001504};
1505
1506static int
1507smb2_ioctl_query_info(const unsigned int xid,
1508 struct cifs_tcon *tcon,
1509 struct cifs_sb_info *cifs_sb,
1510 __le16 *path, int is_dir,
1511 unsigned long p)
1512{
1513 struct iqi_vars *vars;
1514 struct smb_rqst *rqst;
1515 struct kvec *rsp_iov;
1516 struct cifs_ses *ses = tcon->ses;
1517 struct TCP_Server_Info *server = cifs_pick_channel(ses);
1518 char __user *arg = (char __user *)p;
1519 struct smb_query_info qi;
1520 struct smb_query_info __user *pqi;
1521 int rc = 0;
1522 int flags = CIFS_CP_CREATE_CLOSE_OP;
1523 struct smb2_query_info_rsp *qi_rsp = NULL;
1524 struct smb2_ioctl_rsp *io_rsp = NULL;
1525 void *buffer = NULL;
1526 int resp_buftype[3];
1527 struct cifs_open_parms oparms;
1528 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1529 struct cifs_fid fid;
David Brazdil0f672f62019-12-10 10:32:29 +00001530 unsigned int size[2];
1531 void *data[2];
Olivier Deprez157378f2022-04-04 15:47:50 +02001532 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
Olivier Deprez92d4c212022-12-06 15:05:30 +01001533 void (*free_req1_func)(struct smb_rqst *r);
David Brazdil0f672f62019-12-10 10:32:29 +00001534
Olivier Deprez157378f2022-04-04 15:47:50 +02001535 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1536 if (vars == NULL)
1537 return -ENOMEM;
1538 rqst = &vars->rqst[0];
1539 rsp_iov = &vars->rsp_iov[0];
1540
David Brazdil0f672f62019-12-10 10:32:29 +00001541 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
David Brazdil0f672f62019-12-10 10:32:29 +00001542
Olivier Deprez92d4c212022-12-06 15:05:30 +01001543 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info))) {
1544 rc = -EFAULT;
1545 goto free_vars;
1546 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001547 if (qi.output_buffer_length > 1024) {
Olivier Deprez92d4c212022-12-06 15:05:30 +01001548 rc = -EINVAL;
1549 goto free_vars;
Olivier Deprez157378f2022-04-04 15:47:50 +02001550 }
David Brazdil0f672f62019-12-10 10:32:29 +00001551
Olivier Deprez157378f2022-04-04 15:47:50 +02001552 if (!ses || !server) {
Olivier Deprez92d4c212022-12-06 15:05:30 +01001553 rc = -EIO;
1554 goto free_vars;
Olivier Deprez157378f2022-04-04 15:47:50 +02001555 }
David Brazdil0f672f62019-12-10 10:32:29 +00001556
1557 if (smb3_encryption_required(tcon))
1558 flags |= CIFS_TRANSFORM_REQ;
1559
Olivier Deprez92d4c212022-12-06 15:05:30 +01001560 if (qi.output_buffer_length) {
1561 buffer = memdup_user(arg + sizeof(struct smb_query_info), qi.output_buffer_length);
1562 if (IS_ERR(buffer)) {
1563 rc = PTR_ERR(buffer);
1564 goto free_vars;
1565 }
David Brazdil0f672f62019-12-10 10:32:29 +00001566 }
1567
1568 /* Open */
Olivier Deprez157378f2022-04-04 15:47:50 +02001569 rqst[0].rq_iov = &vars->open_iov[0];
David Brazdil0f672f62019-12-10 10:32:29 +00001570 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1571
1572 memset(&oparms, 0, sizeof(oparms));
1573 oparms.tcon = tcon;
1574 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02001575 oparms.create_options = cifs_create_options(cifs_sb, create_options);
David Brazdil0f672f62019-12-10 10:32:29 +00001576 oparms.fid = &fid;
1577 oparms.reconnect = false;
1578
1579 if (qi.flags & PASSTHRU_FSCTL) {
1580 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1581 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1582 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1583 break;
1584 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1585 oparms.desired_access = GENERIC_ALL;
1586 break;
1587 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1588 oparms.desired_access = GENERIC_READ;
1589 break;
1590 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1591 oparms.desired_access = GENERIC_WRITE;
1592 break;
1593 }
1594 } else if (qi.flags & PASSTHRU_SET_INFO) {
1595 oparms.desired_access = GENERIC_WRITE;
1596 } else {
1597 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1598 }
1599
Olivier Deprez157378f2022-04-04 15:47:50 +02001600 rc = SMB2_open_init(tcon, server,
1601 &rqst[0], &oplock, &oparms, path);
David Brazdil0f672f62019-12-10 10:32:29 +00001602 if (rc)
Olivier Deprez92d4c212022-12-06 15:05:30 +01001603 goto free_output_buffer;
David Brazdil0f672f62019-12-10 10:32:29 +00001604 smb2_set_next_command(tcon, &rqst[0]);
1605
1606 /* Query */
1607 if (qi.flags & PASSTHRU_FSCTL) {
1608 /* Can eventually relax perm check since server enforces too */
Olivier Deprez92d4c212022-12-06 15:05:30 +01001609 if (!capable(CAP_SYS_ADMIN)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001610 rc = -EPERM;
Olivier Deprez92d4c212022-12-06 15:05:30 +01001611 goto free_open_req;
David Brazdil0f672f62019-12-10 10:32:29 +00001612 }
Olivier Deprez92d4c212022-12-06 15:05:30 +01001613 rqst[1].rq_iov = &vars->io_iov[0];
1614 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1615
1616 rc = SMB2_ioctl_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1617 qi.info_type, buffer, qi.output_buffer_length,
1618 CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1619 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1620 free_req1_func = SMB2_ioctl_free;
David Brazdil0f672f62019-12-10 10:32:29 +00001621 } else if (qi.flags == PASSTHRU_SET_INFO) {
1622 /* Can eventually relax perm check since server enforces too */
Olivier Deprez92d4c212022-12-06 15:05:30 +01001623 if (!capable(CAP_SYS_ADMIN)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001624 rc = -EPERM;
Olivier Deprez92d4c212022-12-06 15:05:30 +01001625 goto free_open_req;
David Brazdil0f672f62019-12-10 10:32:29 +00001626 }
Olivier Deprez92d4c212022-12-06 15:05:30 +01001627 if (qi.output_buffer_length < 8) {
1628 rc = -EINVAL;
1629 goto free_open_req;
1630 }
1631 rqst[1].rq_iov = &vars->si_iov[0];
1632 rqst[1].rq_nvec = 1;
1633
1634 /* MS-FSCC 2.4.13 FileEndOfFileInformation */
1635 size[0] = 8;
1636 data[0] = buffer;
1637
1638 rc = SMB2_set_info_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1639 current->tgid, FILE_END_OF_FILE_INFORMATION,
1640 SMB2_O_INFO_FILE, 0, data, size);
1641 free_req1_func = SMB2_set_info_free;
David Brazdil0f672f62019-12-10 10:32:29 +00001642 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001643 rqst[1].rq_iov = &vars->qi_iov[0];
David Brazdil0f672f62019-12-10 10:32:29 +00001644 rqst[1].rq_nvec = 1;
1645
Olivier Deprez157378f2022-04-04 15:47:50 +02001646 rc = SMB2_query_info_init(tcon, server,
1647 &rqst[1], COMPOUND_FID,
David Brazdil0f672f62019-12-10 10:32:29 +00001648 COMPOUND_FID, qi.file_info_class,
1649 qi.info_type, qi.additional_information,
1650 qi.input_buffer_length,
1651 qi.output_buffer_length, buffer);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001652 free_req1_func = SMB2_query_info_free;
David Brazdil0f672f62019-12-10 10:32:29 +00001653 } else { /* unknown flags */
Olivier Deprez157378f2022-04-04 15:47:50 +02001654 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1655 qi.flags);
David Brazdil0f672f62019-12-10 10:32:29 +00001656 rc = -EINVAL;
1657 }
1658
1659 if (rc)
Olivier Deprez92d4c212022-12-06 15:05:30 +01001660 goto free_open_req;
David Brazdil0f672f62019-12-10 10:32:29 +00001661 smb2_set_next_command(tcon, &rqst[1]);
1662 smb2_set_related(&rqst[1]);
1663
1664 /* Close */
Olivier Deprez157378f2022-04-04 15:47:50 +02001665 rqst[2].rq_iov = &vars->close_iov[0];
David Brazdil0f672f62019-12-10 10:32:29 +00001666 rqst[2].rq_nvec = 1;
1667
Olivier Deprez157378f2022-04-04 15:47:50 +02001668 rc = SMB2_close_init(tcon, server,
1669 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
David Brazdil0f672f62019-12-10 10:32:29 +00001670 if (rc)
Olivier Deprez92d4c212022-12-06 15:05:30 +01001671 goto free_req_1;
David Brazdil0f672f62019-12-10 10:32:29 +00001672 smb2_set_related(&rqst[2]);
1673
Olivier Deprez157378f2022-04-04 15:47:50 +02001674 rc = compound_send_recv(xid, ses, server,
1675 flags, 3, rqst,
David Brazdil0f672f62019-12-10 10:32:29 +00001676 resp_buftype, rsp_iov);
1677 if (rc)
Olivier Deprez92d4c212022-12-06 15:05:30 +01001678 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00001679
1680 /* No need to bump num_remote_opens since handle immediately closed */
1681 if (qi.flags & PASSTHRU_FSCTL) {
1682 pqi = (struct smb_query_info __user *)arg;
1683 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1684 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1685 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1686 if (qi.input_buffer_length > 0 &&
Olivier Deprez0e641232021-09-23 10:07:05 +02001687 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
Olivier Deprez92d4c212022-12-06 15:05:30 +01001688 > rsp_iov[1].iov_len) {
1689 rc = -EFAULT;
1690 goto out;
1691 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001692
1693 if (copy_to_user(&pqi->input_buffer_length,
1694 &qi.input_buffer_length,
Olivier Deprez92d4c212022-12-06 15:05:30 +01001695 sizeof(qi.input_buffer_length))) {
1696 rc = -EFAULT;
1697 goto out;
1698 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001699
David Brazdil0f672f62019-12-10 10:32:29 +00001700 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1701 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
Olivier Deprez0e641232021-09-23 10:07:05 +02001702 qi.input_buffer_length))
Olivier Deprez92d4c212022-12-06 15:05:30 +01001703 rc = -EFAULT;
David Brazdil0f672f62019-12-10 10:32:29 +00001704 } else {
1705 pqi = (struct smb_query_info __user *)arg;
1706 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1707 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1708 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
Olivier Deprez0e641232021-09-23 10:07:05 +02001709 if (copy_to_user(&pqi->input_buffer_length,
1710 &qi.input_buffer_length,
Olivier Deprez92d4c212022-12-06 15:05:30 +01001711 sizeof(qi.input_buffer_length))) {
1712 rc = -EFAULT;
1713 goto out;
1714 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001715
1716 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1717 qi.input_buffer_length))
Olivier Deprez92d4c212022-12-06 15:05:30 +01001718 rc = -EFAULT;
David Brazdil0f672f62019-12-10 10:32:29 +00001719 }
1720
Olivier Deprez92d4c212022-12-06 15:05:30 +01001721out:
David Brazdil0f672f62019-12-10 10:32:29 +00001722 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1723 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1724 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001725 SMB2_close_free(&rqst[2]);
1726free_req_1:
1727 free_req1_func(&rqst[1]);
1728free_open_req:
1729 SMB2_open_free(&rqst[0]);
1730free_output_buffer:
Olivier Deprez157378f2022-04-04 15:47:50 +02001731 kfree(buffer);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001732free_vars:
1733 kfree(vars);
David Brazdil0f672f62019-12-10 10:32:29 +00001734 return rc;
1735}
1736
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001737static ssize_t
1738smb2_copychunk_range(const unsigned int xid,
1739 struct cifsFileInfo *srcfile,
1740 struct cifsFileInfo *trgtfile, u64 src_off,
1741 u64 len, u64 dest_off)
1742{
1743 int rc;
1744 unsigned int ret_data_len;
1745 struct copychunk_ioctl *pcchunk;
1746 struct copychunk_ioctl_rsp *retbuf = NULL;
1747 struct cifs_tcon *tcon;
1748 int chunks_copied = 0;
1749 bool chunk_sizes_updated = false;
1750 ssize_t bytes_written, total_bytes_written = 0;
Olivier Deprez92d4c212022-12-06 15:05:30 +01001751 struct inode *inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001752
1753 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1754
Olivier Deprez92d4c212022-12-06 15:05:30 +01001755 /*
1756 * We need to flush all unwritten data before we can send the
1757 * copychunk ioctl to the server.
1758 */
1759 inode = d_inode(trgtfile->dentry);
1760 filemap_write_and_wait(inode->i_mapping);
1761
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001762 if (pcchunk == NULL)
1763 return -ENOMEM;
1764
David Brazdil0f672f62019-12-10 10:32:29 +00001765 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001766 /* Request a key from the server to identify the source of the copy */
1767 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1768 srcfile->fid.persistent_fid,
1769 srcfile->fid.volatile_fid, pcchunk);
1770
1771 /* Note: request_res_key sets res_key null only if rc !=0 */
1772 if (rc)
1773 goto cchunk_out;
1774
1775 /* For now array only one chunk long, will make more flexible later */
1776 pcchunk->ChunkCount = cpu_to_le32(1);
1777 pcchunk->Reserved = 0;
1778 pcchunk->Reserved2 = 0;
1779
1780 tcon = tlink_tcon(trgtfile->tlink);
1781
1782 while (len > 0) {
1783 pcchunk->SourceOffset = cpu_to_le64(src_off);
1784 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1785 pcchunk->Length =
1786 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1787
1788 /* Request server copy to target from src identified by key */
Olivier Deprez0e641232021-09-23 10:07:05 +02001789 kfree(retbuf);
1790 retbuf = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001791 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1792 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Olivier Deprez92d4c212022-12-06 15:05:30 +01001793 (char *)pcchunk, sizeof(struct copychunk_ioctl),
1794 CIFSMaxBufSize, (char **)&retbuf, &ret_data_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001795 if (rc == 0) {
1796 if (ret_data_len !=
1797 sizeof(struct copychunk_ioctl_rsp)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001798 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001799 rc = -EIO;
1800 goto cchunk_out;
1801 }
1802 if (retbuf->TotalBytesWritten == 0) {
1803 cifs_dbg(FYI, "no bytes copied\n");
1804 rc = -EIO;
1805 goto cchunk_out;
1806 }
1807 /*
1808 * Check if server claimed to write more than we asked
1809 */
1810 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1811 le32_to_cpu(pcchunk->Length)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001812 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001813 rc = -EIO;
1814 goto cchunk_out;
1815 }
1816 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001817 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001818 rc = -EIO;
1819 goto cchunk_out;
1820 }
1821 chunks_copied++;
1822
1823 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1824 src_off += bytes_written;
1825 dest_off += bytes_written;
1826 len -= bytes_written;
1827 total_bytes_written += bytes_written;
1828
1829 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1830 le32_to_cpu(retbuf->ChunksWritten),
1831 le32_to_cpu(retbuf->ChunkBytesWritten),
1832 bytes_written);
1833 } else if (rc == -EINVAL) {
1834 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1835 goto cchunk_out;
1836
1837 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1838 le32_to_cpu(retbuf->ChunksWritten),
1839 le32_to_cpu(retbuf->ChunkBytesWritten),
1840 le32_to_cpu(retbuf->TotalBytesWritten));
1841
1842 /*
1843 * Check if this is the first request using these sizes,
1844 * (ie check if copy succeed once with original sizes
1845 * and check if the server gave us different sizes after
1846 * we already updated max sizes on previous request).
1847 * if not then why is the server returning an error now
1848 */
1849 if ((chunks_copied != 0) || chunk_sizes_updated)
1850 goto cchunk_out;
1851
1852 /* Check that server is not asking us to grow size */
1853 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1854 tcon->max_bytes_chunk)
1855 tcon->max_bytes_chunk =
1856 le32_to_cpu(retbuf->ChunkBytesWritten);
1857 else
1858 goto cchunk_out; /* server gave us bogus size */
1859
1860 /* No need to change MaxChunks since already set to 1 */
1861 chunk_sizes_updated = true;
1862 } else
1863 goto cchunk_out;
1864 }
1865
1866cchunk_out:
1867 kfree(pcchunk);
1868 kfree(retbuf);
1869 if (rc)
1870 return rc;
1871 else
1872 return total_bytes_written;
1873}
1874
1875static int
1876smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1877 struct cifs_fid *fid)
1878{
1879 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1880}
1881
1882static unsigned int
1883smb2_read_data_offset(char *buf)
1884{
1885 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
David Brazdil0f672f62019-12-10 10:32:29 +00001886
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001887 return rsp->DataOffset;
1888}
1889
1890static unsigned int
1891smb2_read_data_length(char *buf, bool in_remaining)
1892{
1893 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1894
1895 if (in_remaining)
1896 return le32_to_cpu(rsp->DataRemaining);
1897
1898 return le32_to_cpu(rsp->DataLength);
1899}
1900
1901
1902static int
1903smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1904 struct cifs_io_parms *parms, unsigned int *bytes_read,
1905 char **buf, int *buf_type)
1906{
1907 parms->persistent_fid = pfid->persistent_fid;
1908 parms->volatile_fid = pfid->volatile_fid;
1909 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1910}
1911
1912static int
1913smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1914 struct cifs_io_parms *parms, unsigned int *written,
1915 struct kvec *iov, unsigned long nr_segs)
1916{
1917
1918 parms->persistent_fid = pfid->persistent_fid;
1919 parms->volatile_fid = pfid->volatile_fid;
1920 return SMB2_write(xid, parms, written, iov, nr_segs);
1921}
1922
1923/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1924static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1925 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1926{
1927 struct cifsInodeInfo *cifsi;
1928 int rc;
1929
1930 cifsi = CIFS_I(inode);
1931
1932 /* if file already sparse don't bother setting sparse again */
1933 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1934 return true; /* already sparse */
1935
1936 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1937 return true; /* already not sparse */
1938
1939 /*
1940 * Can't check for sparse support on share the usual way via the
1941 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1942 * since Samba server doesn't set the flag on the share, yet
1943 * supports the set sparse FSCTL and returns sparse correctly
1944 * in the file attributes. If we fail setting sparse though we
1945 * mark that server does not support sparse files for this share
1946 * to avoid repeatedly sending the unsupported fsctl to server
1947 * if the file is repeatedly extended.
1948 */
1949 if (tcon->broken_sparse_sup)
1950 return false;
1951
1952 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1953 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
David Brazdil0f672f62019-12-10 10:32:29 +00001954 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001955 if (rc) {
1956 tcon->broken_sparse_sup = true;
1957 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1958 return false;
1959 }
1960
1961 if (setsparse)
1962 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1963 else
1964 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1965
1966 return true;
1967}
1968
1969static int
1970smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1971 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1972{
1973 __le64 eof = cpu_to_le64(size);
1974 struct inode *inode;
1975
1976 /*
1977 * If extending file more than one page make sparse. Many Linux fs
1978 * make files sparse by default when extending via ftruncate
1979 */
1980 inode = d_inode(cfile->dentry);
1981
1982 if (!set_alloc && (size > inode->i_size + 8192)) {
1983 __u8 set_sparse = 1;
1984
1985 /* whether set sparse succeeds or not, extend the file */
1986 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1987 }
1988
1989 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
David Brazdil0f672f62019-12-10 10:32:29 +00001990 cfile->fid.volatile_fid, cfile->pid, &eof);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001991}
1992
1993static int
1994smb2_duplicate_extents(const unsigned int xid,
1995 struct cifsFileInfo *srcfile,
1996 struct cifsFileInfo *trgtfile, u64 src_off,
1997 u64 len, u64 dest_off)
1998{
1999 int rc;
2000 unsigned int ret_data_len;
Olivier Deprez157378f2022-04-04 15:47:50 +02002001 struct inode *inode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002002 struct duplicate_extents_to_file dup_ext_buf;
2003 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
2004
2005 /* server fileays advertise duplicate extent support with this flag */
2006 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
2007 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
2008 return -EOPNOTSUPP;
2009
2010 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
2011 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
2012 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
2013 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
2014 dup_ext_buf.ByteCount = cpu_to_le64(len);
David Brazdil0f672f62019-12-10 10:32:29 +00002015 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002016 src_off, dest_off, len);
2017
Olivier Deprez157378f2022-04-04 15:47:50 +02002018 inode = d_inode(trgtfile->dentry);
2019 if (inode->i_size < dest_off + len) {
2020 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2021 if (rc)
2022 goto duplicate_extents_out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002023
Olivier Deprez157378f2022-04-04 15:47:50 +02002024 /*
2025 * Although also could set plausible allocation size (i_blocks)
2026 * here in addition to setting the file size, in reflink
2027 * it is likely that the target file is sparse. Its allocation
2028 * size will be queried on next revalidate, but it is important
2029 * to make sure that file's cached size is updated immediately
2030 */
2031 cifs_setsize(inode, dest_off + len);
2032 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002033 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2034 trgtfile->fid.volatile_fid,
2035 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002036 (char *)&dup_ext_buf,
2037 sizeof(struct duplicate_extents_to_file),
David Brazdil0f672f62019-12-10 10:32:29 +00002038 CIFSMaxBufSize, NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002039 &ret_data_len);
2040
2041 if (ret_data_len > 0)
David Brazdil0f672f62019-12-10 10:32:29 +00002042 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002043
2044duplicate_extents_out:
2045 return rc;
2046}
2047
2048static int
2049smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2050 struct cifsFileInfo *cfile)
2051{
2052 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2053 cfile->fid.volatile_fid);
2054}
2055
2056static int
2057smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2058 struct cifsFileInfo *cfile)
2059{
2060 struct fsctl_set_integrity_information_req integr_info;
2061 unsigned int ret_data_len;
2062
2063 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2064 integr_info.Flags = 0;
2065 integr_info.Reserved = 0;
2066
2067 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2068 cfile->fid.volatile_fid,
2069 FSCTL_SET_INTEGRITY_INFORMATION,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002070 (char *)&integr_info,
2071 sizeof(struct fsctl_set_integrity_information_req),
David Brazdil0f672f62019-12-10 10:32:29 +00002072 CIFSMaxBufSize, NULL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002073 &ret_data_len);
2074
2075}
2076
2077/* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2078#define GMT_TOKEN_SIZE 50
2079
David Brazdil0f672f62019-12-10 10:32:29 +00002080#define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2081
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002082/*
2083 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2084 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2085 */
2086static int
2087smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2088 struct cifsFileInfo *cfile, void __user *ioc_buf)
2089{
2090 char *retbuf = NULL;
2091 unsigned int ret_data_len = 0;
2092 int rc;
David Brazdil0f672f62019-12-10 10:32:29 +00002093 u32 max_response_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002094 struct smb_snapshot_array snapshot_in;
2095
David Brazdil0f672f62019-12-10 10:32:29 +00002096 /*
2097 * On the first query to enumerate the list of snapshots available
2098 * for this volume the buffer begins with 0 (number of snapshots
2099 * which can be returned is zero since at that point we do not know
2100 * how big the buffer needs to be). On the second query,
2101 * it (ret_data_len) is set to number of snapshots so we can
2102 * know to set the maximum response size larger (see below).
2103 */
2104 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2105 return -EFAULT;
2106
2107 /*
2108 * Note that for snapshot queries that servers like Azure expect that
2109 * the first query be minimal size (and just used to get the number/size
2110 * of previous versions) so response size must be specified as EXACTLY
2111 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2112 * of eight bytes.
2113 */
2114 if (ret_data_len == 0)
2115 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2116 else
2117 max_response_size = CIFSMaxBufSize;
2118
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002119 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2120 cfile->fid.volatile_fid,
2121 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
David Brazdil0f672f62019-12-10 10:32:29 +00002122 NULL, 0 /* no input data */, max_response_size,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002123 (char **)&retbuf,
2124 &ret_data_len);
2125 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2126 rc, ret_data_len);
2127 if (rc)
2128 return rc;
2129
2130 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2131 /* Fixup buffer */
2132 if (copy_from_user(&snapshot_in, ioc_buf,
2133 sizeof(struct smb_snapshot_array))) {
2134 rc = -EFAULT;
2135 kfree(retbuf);
2136 return rc;
2137 }
2138
2139 /*
2140 * Check for min size, ie not large enough to fit even one GMT
2141 * token (snapshot). On the first ioctl some users may pass in
2142 * smaller size (or zero) to simply get the size of the array
2143 * so the user space caller can allocate sufficient memory
2144 * and retry the ioctl again with larger array size sufficient
2145 * to hold all of the snapshot GMT tokens on the second try.
2146 */
2147 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2148 ret_data_len = sizeof(struct smb_snapshot_array);
2149
2150 /*
2151 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2152 * the snapshot array (of 50 byte GMT tokens) each
2153 * representing an available previous version of the data
2154 */
2155 if (ret_data_len > (snapshot_in.snapshot_array_size +
2156 sizeof(struct smb_snapshot_array)))
2157 ret_data_len = snapshot_in.snapshot_array_size +
2158 sizeof(struct smb_snapshot_array);
2159
2160 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2161 rc = -EFAULT;
2162 }
2163
2164 kfree(retbuf);
2165 return rc;
2166}
2167
Olivier Deprez157378f2022-04-04 15:47:50 +02002168
2169
2170static int
2171smb3_notify(const unsigned int xid, struct file *pfile,
2172 void __user *ioc_buf)
2173{
2174 struct smb3_notify notify;
2175 struct dentry *dentry = pfile->f_path.dentry;
2176 struct inode *inode = file_inode(pfile);
2177 struct cifs_sb_info *cifs_sb;
2178 struct cifs_open_parms oparms;
2179 struct cifs_fid fid;
2180 struct cifs_tcon *tcon;
2181 unsigned char *path = NULL;
2182 __le16 *utf16_path = NULL;
2183 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2184 int rc = 0;
2185
2186 path = build_path_from_dentry(dentry);
2187 if (path == NULL)
2188 return -ENOMEM;
2189
2190 cifs_sb = CIFS_SB(inode->i_sb);
2191
2192 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2193 if (utf16_path == NULL) {
2194 rc = -ENOMEM;
2195 goto notify_exit;
2196 }
2197
2198 if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2199 rc = -EFAULT;
2200 goto notify_exit;
2201 }
2202
2203 tcon = cifs_sb_master_tcon(cifs_sb);
2204 oparms.tcon = tcon;
2205 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2206 oparms.disposition = FILE_OPEN;
2207 oparms.create_options = cifs_create_options(cifs_sb, 0);
2208 oparms.fid = &fid;
2209 oparms.reconnect = false;
2210
2211 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2212 NULL);
2213 if (rc)
2214 goto notify_exit;
2215
2216 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2217 notify.watch_tree, notify.completion_filter);
2218
2219 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2220
2221 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2222
2223notify_exit:
2224 kfree(path);
2225 kfree(utf16_path);
2226 return rc;
2227}
2228
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002229static int
2230smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2231 const char *path, struct cifs_sb_info *cifs_sb,
2232 struct cifs_fid *fid, __u16 search_flags,
2233 struct cifs_search_info *srch_inf)
2234{
2235 __le16 *utf16_path;
Olivier Deprez157378f2022-04-04 15:47:50 +02002236 struct smb_rqst rqst[2];
2237 struct kvec rsp_iov[2];
2238 int resp_buftype[2];
2239 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2240 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2241 int rc, flags = 0;
2242 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002243 struct cifs_open_parms oparms;
Olivier Deprez157378f2022-04-04 15:47:50 +02002244 struct smb2_query_directory_rsp *qd_rsp = NULL;
2245 struct smb2_create_rsp *op_rsp = NULL;
2246 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002247
2248 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2249 if (!utf16_path)
2250 return -ENOMEM;
2251
Olivier Deprez157378f2022-04-04 15:47:50 +02002252 if (smb3_encryption_required(tcon))
2253 flags |= CIFS_TRANSFORM_REQ;
2254
2255 memset(rqst, 0, sizeof(rqst));
2256 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2257 memset(rsp_iov, 0, sizeof(rsp_iov));
2258
2259 /* Open */
2260 memset(&open_iov, 0, sizeof(open_iov));
2261 rqst[0].rq_iov = open_iov;
2262 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2263
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002264 oparms.tcon = tcon;
2265 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2266 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02002267 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002268 oparms.fid = fid;
2269 oparms.reconnect = false;
2270
Olivier Deprez157378f2022-04-04 15:47:50 +02002271 rc = SMB2_open_init(tcon, server,
2272 &rqst[0], &oplock, &oparms, utf16_path);
2273 if (rc)
2274 goto qdf_free;
2275 smb2_set_next_command(tcon, &rqst[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002276
Olivier Deprez157378f2022-04-04 15:47:50 +02002277 /* Query directory */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002278 srch_inf->entries_in_buffer = 0;
2279 srch_inf->index_of_last_entry = 2;
2280
Olivier Deprez157378f2022-04-04 15:47:50 +02002281 memset(&qd_iov, 0, sizeof(qd_iov));
2282 rqst[1].rq_iov = qd_iov;
2283 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2284
2285 rc = SMB2_query_directory_init(xid, tcon, server,
2286 &rqst[1],
2287 COMPOUND_FID, COMPOUND_FID,
2288 0, srch_inf->info_level);
2289 if (rc)
2290 goto qdf_free;
2291
2292 smb2_set_related(&rqst[1]);
2293
2294 rc = compound_send_recv(xid, tcon->ses, server,
2295 flags, 2, rqst,
2296 resp_buftype, rsp_iov);
2297
2298 /* If the open failed there is nothing to do */
2299 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2300 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2301 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2302 goto qdf_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002303 }
Olivier Deprez157378f2022-04-04 15:47:50 +02002304 fid->persistent_fid = op_rsp->PersistentFileId;
2305 fid->volatile_fid = op_rsp->VolatileFileId;
2306
2307 /* Anything else than ENODATA means a genuine error */
2308 if (rc && rc != -ENODATA) {
2309 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2310 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2311 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2312 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2313 goto qdf_free;
2314 }
2315
2316 atomic_inc(&tcon->num_remote_opens);
2317
2318 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2319 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2320 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2321 tcon->tid, tcon->ses->Suid, 0, 0);
2322 srch_inf->endOfSearch = true;
2323 rc = 0;
2324 goto qdf_free;
2325 }
2326
2327 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2328 srch_inf);
2329 if (rc) {
2330 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2331 tcon->ses->Suid, 0, 0, rc);
2332 goto qdf_free;
2333 }
2334 resp_buftype[1] = CIFS_NO_BUFFER;
2335
2336 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2337 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2338
2339 qdf_free:
2340 kfree(utf16_path);
2341 SMB2_open_free(&rqst[0]);
2342 SMB2_query_directory_free(&rqst[1]);
2343 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2344 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002345 return rc;
2346}
2347
2348static int
2349smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2350 struct cifs_fid *fid, __u16 search_flags,
2351 struct cifs_search_info *srch_inf)
2352{
2353 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2354 fid->volatile_fid, 0, srch_inf);
2355}
2356
2357static int
2358smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2359 struct cifs_fid *fid)
2360{
2361 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2362}
2363
2364/*
David Brazdil0f672f62019-12-10 10:32:29 +00002365 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2366 * the number of credits and return true. Otherwise - return false.
2367 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002368static bool
David Brazdil0f672f62019-12-10 10:32:29 +00002369smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002370{
2371 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2372
2373 if (shdr->Status != STATUS_PENDING)
2374 return false;
2375
David Brazdil0f672f62019-12-10 10:32:29 +00002376 if (shdr->CreditRequest) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002377 spin_lock(&server->req_lock);
2378 server->credits += le16_to_cpu(shdr->CreditRequest);
2379 spin_unlock(&server->req_lock);
2380 wake_up(&server->request_q);
2381 }
2382
2383 return true;
2384}
2385
2386static bool
2387smb2_is_session_expired(char *buf)
2388{
2389 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2390
2391 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2392 shdr->Status != STATUS_USER_SESSION_DELETED)
2393 return false;
2394
2395 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2396 le16_to_cpu(shdr->Command),
2397 le64_to_cpu(shdr->MessageId));
2398 cifs_dbg(FYI, "Session expired or deleted\n");
2399
2400 return true;
2401}
2402
Olivier Deprez157378f2022-04-04 15:47:50 +02002403static bool
2404smb2_is_status_io_timeout(char *buf)
2405{
2406 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2407
2408 if (shdr->Status == STATUS_IO_TIMEOUT)
2409 return true;
2410 else
2411 return false;
2412}
2413
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002414static int
2415smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2416 struct cifsInodeInfo *cinode)
2417{
2418 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2419 return SMB2_lease_break(0, tcon, cinode->lease_key,
2420 smb2_get_lease_state(cinode));
2421
2422 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2423 fid->volatile_fid,
2424 CIFS_CACHE_READ(cinode) ? 1 : 0);
2425}
2426
David Brazdil0f672f62019-12-10 10:32:29 +00002427void
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002428smb2_set_related(struct smb_rqst *rqst)
2429{
2430 struct smb2_sync_hdr *shdr;
2431
2432 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
David Brazdil0f672f62019-12-10 10:32:29 +00002433 if (shdr == NULL) {
2434 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2435 return;
2436 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002437 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2438}
2439
2440char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2441
David Brazdil0f672f62019-12-10 10:32:29 +00002442void
2443smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002444{
2445 struct smb2_sync_hdr *shdr;
David Brazdil0f672f62019-12-10 10:32:29 +00002446 struct cifs_ses *ses = tcon->ses;
2447 struct TCP_Server_Info *server = ses->server;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002448 unsigned long len = smb_rqst_len(server, rqst);
David Brazdil0f672f62019-12-10 10:32:29 +00002449 int i, num_padding;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002450
2451 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
David Brazdil0f672f62019-12-10 10:32:29 +00002452 if (shdr == NULL) {
2453 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2454 return;
2455 }
2456
2457 /* SMB headers in a compound are 8 byte aligned. */
2458
2459 /* No padding needed */
2460 if (!(len & 7))
2461 goto finished;
2462
2463 num_padding = 8 - (len & 7);
2464 if (!smb3_encryption_required(tcon)) {
2465 /*
2466 * If we do not have encryption then we can just add an extra
2467 * iov for the padding.
2468 */
2469 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2470 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2471 rqst->rq_nvec++;
2472 len += num_padding;
2473 } else {
2474 /*
2475 * We can not add a small padding iov for the encryption case
2476 * because the encryption framework can not handle the padding
2477 * iovs.
2478 * We have to flatten this into a single buffer and add
2479 * the padding to it.
2480 */
2481 for (i = 1; i < rqst->rq_nvec; i++) {
2482 memcpy(rqst->rq_iov[0].iov_base +
2483 rqst->rq_iov[0].iov_len,
2484 rqst->rq_iov[i].iov_base,
2485 rqst->rq_iov[i].iov_len);
2486 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2487 }
2488 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2489 0, num_padding);
2490 rqst->rq_iov[0].iov_len += num_padding;
2491 len += num_padding;
2492 rqst->rq_nvec = 1;
2493 }
2494
2495 finished:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002496 shdr->NextCommand = cpu_to_le32(len);
2497}
2498
David Brazdil0f672f62019-12-10 10:32:29 +00002499/*
2500 * Passes the query info response back to the caller on success.
2501 * Caller need to free this with free_rsp_buf().
2502 */
2503int
2504smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2505 __le16 *utf16_path, u32 desired_access,
2506 u32 class, u32 type, u32 output_len,
2507 struct kvec *rsp, int *buftype,
2508 struct cifs_sb_info *cifs_sb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002509{
David Brazdil0f672f62019-12-10 10:32:29 +00002510 struct cifs_ses *ses = tcon->ses;
Olivier Deprez157378f2022-04-04 15:47:50 +02002511 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2512 int flags = CIFS_CP_CREATE_CLOSE_OP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002513 struct smb_rqst rqst[3];
2514 int resp_buftype[3];
2515 struct kvec rsp_iov[3];
2516 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2517 struct kvec qi_iov[1];
2518 struct kvec close_iov[1];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002519 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2520 struct cifs_open_parms oparms;
2521 struct cifs_fid fid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002522 int rc;
2523
2524 if (smb3_encryption_required(tcon))
2525 flags |= CIFS_TRANSFORM_REQ;
2526
2527 memset(rqst, 0, sizeof(rqst));
David Brazdil0f672f62019-12-10 10:32:29 +00002528 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002529 memset(rsp_iov, 0, sizeof(rsp_iov));
2530
2531 memset(&open_iov, 0, sizeof(open_iov));
2532 rqst[0].rq_iov = open_iov;
2533 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2534
2535 oparms.tcon = tcon;
David Brazdil0f672f62019-12-10 10:32:29 +00002536 oparms.desired_access = desired_access;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002537 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02002538 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002539 oparms.fid = &fid;
2540 oparms.reconnect = false;
2541
Olivier Deprez157378f2022-04-04 15:47:50 +02002542 rc = SMB2_open_init(tcon, server,
2543 &rqst[0], &oplock, &oparms, utf16_path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002544 if (rc)
David Brazdil0f672f62019-12-10 10:32:29 +00002545 goto qic_exit;
2546 smb2_set_next_command(tcon, &rqst[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002547
2548 memset(&qi_iov, 0, sizeof(qi_iov));
2549 rqst[1].rq_iov = qi_iov;
2550 rqst[1].rq_nvec = 1;
2551
Olivier Deprez157378f2022-04-04 15:47:50 +02002552 rc = SMB2_query_info_init(tcon, server,
2553 &rqst[1], COMPOUND_FID, COMPOUND_FID,
David Brazdil0f672f62019-12-10 10:32:29 +00002554 class, type, 0,
2555 output_len, 0,
2556 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002557 if (rc)
David Brazdil0f672f62019-12-10 10:32:29 +00002558 goto qic_exit;
2559 smb2_set_next_command(tcon, &rqst[1]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002560 smb2_set_related(&rqst[1]);
2561
2562 memset(&close_iov, 0, sizeof(close_iov));
2563 rqst[2].rq_iov = close_iov;
2564 rqst[2].rq_nvec = 1;
2565
Olivier Deprez157378f2022-04-04 15:47:50 +02002566 rc = SMB2_close_init(tcon, server,
2567 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002568 if (rc)
David Brazdil0f672f62019-12-10 10:32:29 +00002569 goto qic_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002570 smb2_set_related(&rqst[2]);
2571
Olivier Deprez157378f2022-04-04 15:47:50 +02002572 rc = compound_send_recv(xid, ses, server,
2573 flags, 3, rqst,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002574 resp_buftype, rsp_iov);
David Brazdil0f672f62019-12-10 10:32:29 +00002575 if (rc) {
2576 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2577 if (rc == -EREMCHG) {
2578 tcon->need_reconnect = true;
Olivier Deprez157378f2022-04-04 15:47:50 +02002579 pr_warn_once("server share %s deleted\n",
2580 tcon->treeName);
David Brazdil0f672f62019-12-10 10:32:29 +00002581 }
2582 goto qic_exit;
2583 }
2584 *rsp = rsp_iov[1];
2585 *buftype = resp_buftype[1];
2586
2587 qic_exit:
2588 SMB2_open_free(&rqst[0]);
2589 SMB2_query_info_free(&rqst[1]);
2590 SMB2_close_free(&rqst[2]);
2591 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2592 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2593 return rc;
2594}
2595
2596static int
2597smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
Olivier Deprez157378f2022-04-04 15:47:50 +02002598 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
David Brazdil0f672f62019-12-10 10:32:29 +00002599{
2600 struct smb2_query_info_rsp *rsp;
2601 struct smb2_fs_full_size_info *info = NULL;
2602 __le16 utf16_path = 0; /* Null - open root of share */
2603 struct kvec rsp_iov = {NULL, 0};
2604 int buftype = CIFS_NO_BUFFER;
2605 int rc;
2606
2607
2608 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2609 FILE_READ_ATTRIBUTES,
2610 FS_FULL_SIZE_INFORMATION,
2611 SMB2_O_INFO_FILESYSTEM,
2612 sizeof(struct smb2_fs_full_size_info),
Olivier Deprez157378f2022-04-04 15:47:50 +02002613 &rsp_iov, &buftype, cifs_sb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002614 if (rc)
2615 goto qfs_exit;
2616
David Brazdil0f672f62019-12-10 10:32:29 +00002617 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002618 buf->f_type = SMB2_MAGIC_NUMBER;
2619 info = (struct smb2_fs_full_size_info *)(
2620 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2621 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2622 le32_to_cpu(rsp->OutputBufferLength),
David Brazdil0f672f62019-12-10 10:32:29 +00002623 &rsp_iov,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002624 sizeof(struct smb2_fs_full_size_info));
2625 if (!rc)
2626 smb2_copy_fs_info_to_kstatfs(info, buf);
2627
2628qfs_exit:
David Brazdil0f672f62019-12-10 10:32:29 +00002629 free_rsp_buf(buftype, rsp_iov.iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002630 return rc;
2631}
2632
2633static int
2634smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
Olivier Deprez157378f2022-04-04 15:47:50 +02002635 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002636{
2637 int rc;
2638 __le16 srch_path = 0; /* Null - open root of share */
2639 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2640 struct cifs_open_parms oparms;
2641 struct cifs_fid fid;
2642
2643 if (!tcon->posix_extensions)
Olivier Deprez157378f2022-04-04 15:47:50 +02002644 return smb2_queryfs(xid, tcon, cifs_sb, buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002645
2646 oparms.tcon = tcon;
2647 oparms.desired_access = FILE_READ_ATTRIBUTES;
2648 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02002649 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002650 oparms.fid = &fid;
2651 oparms.reconnect = false;
2652
Olivier Deprez157378f2022-04-04 15:47:50 +02002653 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2654 NULL, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002655 if (rc)
2656 return rc;
2657
2658 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2659 fid.volatile_fid, buf);
2660 buf->f_type = SMB2_MAGIC_NUMBER;
2661 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2662 return rc;
2663}
2664
2665static bool
2666smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2667{
2668 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2669 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2670}
2671
2672static int
2673smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2674 __u64 length, __u32 type, int lock, int unlock, bool wait)
2675{
2676 if (unlock && !lock)
2677 type = SMB2_LOCKFLAG_UNLOCK;
2678 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2679 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2680 current->tgid, length, offset, type, wait);
2681}
2682
2683static void
2684smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2685{
2686 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2687}
2688
2689static void
2690smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2691{
2692 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2693}
2694
2695static void
2696smb2_new_lease_key(struct cifs_fid *fid)
2697{
2698 generate_random_uuid(fid->lease_key);
2699}
2700
2701static int
2702smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2703 const char *search_name,
2704 struct dfs_info3_param **target_nodes,
2705 unsigned int *num_of_nodes,
2706 const struct nls_table *nls_codepage, int remap)
2707{
2708 int rc;
2709 __le16 *utf16_path = NULL;
2710 int utf16_path_len = 0;
2711 struct cifs_tcon *tcon;
2712 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2713 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2714 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2715
David Brazdil0f672f62019-12-10 10:32:29 +00002716 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002717
2718 /*
2719 * Try to use the IPC tcon, otherwise just use any
2720 */
2721 tcon = ses->tcon_ipc;
2722 if (tcon == NULL) {
2723 spin_lock(&cifs_tcp_ses_lock);
2724 tcon = list_first_entry_or_null(&ses->tcon_list,
2725 struct cifs_tcon,
2726 tcon_list);
2727 if (tcon)
2728 tcon->tc_count++;
2729 spin_unlock(&cifs_tcp_ses_lock);
2730 }
2731
2732 if (tcon == NULL) {
2733 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2734 ses);
2735 rc = -ENOTCONN;
2736 goto out;
2737 }
2738
2739 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2740 &utf16_path_len,
2741 nls_codepage, remap);
2742 if (!utf16_path) {
2743 rc = -ENOMEM;
2744 goto out;
2745 }
2746
2747 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2748 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2749 if (!dfs_req) {
2750 rc = -ENOMEM;
2751 goto out;
2752 }
2753
2754 /* Highest DFS referral version understood */
2755 dfs_req->MaxReferralLevel = DFS_VERSION;
2756
2757 /* Path to resolve in an UTF-16 null-terminated string */
2758 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2759
2760 do {
2761 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2762 FSCTL_DFS_GET_REFERRALS,
David Brazdil0f672f62019-12-10 10:32:29 +00002763 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002764 (char **)&dfs_rsp, &dfs_rsp_size);
2765 } while (rc == -EAGAIN);
2766
2767 if (rc) {
2768 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
David Brazdil0f672f62019-12-10 10:32:29 +00002769 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002770 goto out;
2771 }
2772
2773 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2774 num_of_nodes, target_nodes,
2775 nls_codepage, remap, search_name,
2776 true /* is_unicode */);
2777 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00002778 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002779 goto out;
2780 }
2781
2782 out:
2783 if (tcon && !tcon->ipc) {
2784 /* ipc tcons are not refcounted */
2785 spin_lock(&cifs_tcp_ses_lock);
2786 tcon->tc_count--;
2787 spin_unlock(&cifs_tcp_ses_lock);
2788 }
2789 kfree(utf16_path);
2790 kfree(dfs_req);
2791 kfree(dfs_rsp);
2792 return rc;
2793}
David Brazdil0f672f62019-12-10 10:32:29 +00002794
2795static int
2796parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2797 u32 plen, char **target_path,
2798 struct cifs_sb_info *cifs_sb)
2799{
2800 unsigned int len;
2801
2802 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2803 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2804
2805 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2806 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2807 le64_to_cpu(symlink_buf->InodeType));
2808 return -EOPNOTSUPP;
2809 }
2810
2811 *target_path = cifs_strndup_from_utf16(
2812 symlink_buf->PathBuffer,
2813 len, true, cifs_sb->local_nls);
2814 if (!(*target_path))
2815 return -ENOMEM;
2816
2817 convert_delimiter(*target_path, '/');
2818 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2819
2820 return 0;
2821}
2822
2823static int
2824parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2825 u32 plen, char **target_path,
2826 struct cifs_sb_info *cifs_sb)
2827{
2828 unsigned int sub_len;
2829 unsigned int sub_offset;
2830
2831 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2832
2833 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2834 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2835 if (sub_offset + 20 > plen ||
2836 sub_offset + sub_len + 20 > plen) {
2837 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2838 return -EIO;
2839 }
2840
2841 *target_path = cifs_strndup_from_utf16(
2842 symlink_buf->PathBuffer + sub_offset,
2843 sub_len, true, cifs_sb->local_nls);
2844 if (!(*target_path))
2845 return -ENOMEM;
2846
2847 convert_delimiter(*target_path, '/');
2848 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2849
2850 return 0;
2851}
2852
2853static int
2854parse_reparse_point(struct reparse_data_buffer *buf,
2855 u32 plen, char **target_path,
2856 struct cifs_sb_info *cifs_sb)
2857{
2858 if (plen < sizeof(struct reparse_data_buffer)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002859 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2860 plen);
David Brazdil0f672f62019-12-10 10:32:29 +00002861 return -EIO;
2862 }
2863
2864 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2865 sizeof(struct reparse_data_buffer)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002866 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2867 plen);
David Brazdil0f672f62019-12-10 10:32:29 +00002868 return -EIO;
2869 }
2870
2871 /* See MS-FSCC 2.1.2 */
2872 switch (le32_to_cpu(buf->ReparseTag)) {
2873 case IO_REPARSE_TAG_NFS:
2874 return parse_reparse_posix(
2875 (struct reparse_posix_data *)buf,
2876 plen, target_path, cifs_sb);
2877 case IO_REPARSE_TAG_SYMLINK:
2878 return parse_reparse_symlink(
2879 (struct reparse_symlink_data_buffer *)buf,
2880 plen, target_path, cifs_sb);
2881 default:
Olivier Deprez157378f2022-04-04 15:47:50 +02002882 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
2883 le32_to_cpu(buf->ReparseTag));
David Brazdil0f672f62019-12-10 10:32:29 +00002884 return -EOPNOTSUPP;
2885 }
2886}
2887
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002888#define SMB2_SYMLINK_STRUCT_SIZE \
2889 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2890
2891static int
2892smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
David Brazdil0f672f62019-12-10 10:32:29 +00002893 struct cifs_sb_info *cifs_sb, const char *full_path,
2894 char **target_path, bool is_reparse_point)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002895{
2896 int rc;
David Brazdil0f672f62019-12-10 10:32:29 +00002897 __le16 *utf16_path = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002898 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2899 struct cifs_open_parms oparms;
2900 struct cifs_fid fid;
2901 struct kvec err_iov = {NULL, 0};
2902 struct smb2_err_rsp *err_buf = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002903 struct smb2_symlink_err_rsp *symlink;
Olivier Deprez157378f2022-04-04 15:47:50 +02002904 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002905 unsigned int sub_len;
2906 unsigned int sub_offset;
2907 unsigned int print_len;
2908 unsigned int print_offset;
Olivier Deprez157378f2022-04-04 15:47:50 +02002909 int flags = CIFS_CP_CREATE_CLOSE_OP;
David Brazdil0f672f62019-12-10 10:32:29 +00002910 struct smb_rqst rqst[3];
2911 int resp_buftype[3];
2912 struct kvec rsp_iov[3];
2913 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2914 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2915 struct kvec close_iov[1];
2916 struct smb2_create_rsp *create_rsp;
2917 struct smb2_ioctl_rsp *ioctl_rsp;
2918 struct reparse_data_buffer *reparse_buf;
Olivier Deprez157378f2022-04-04 15:47:50 +02002919 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002920 u32 plen;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002921
2922 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2923
David Brazdil0f672f62019-12-10 10:32:29 +00002924 *target_path = NULL;
2925
2926 if (smb3_encryption_required(tcon))
2927 flags |= CIFS_TRANSFORM_REQ;
2928
2929 memset(rqst, 0, sizeof(rqst));
2930 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2931 memset(rsp_iov, 0, sizeof(rsp_iov));
2932
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002933 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2934 if (!utf16_path)
2935 return -ENOMEM;
2936
David Brazdil0f672f62019-12-10 10:32:29 +00002937 /* Open */
2938 memset(&open_iov, 0, sizeof(open_iov));
2939 rqst[0].rq_iov = open_iov;
2940 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2941
2942 memset(&oparms, 0, sizeof(oparms));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002943 oparms.tcon = tcon;
2944 oparms.desired_access = FILE_READ_ATTRIBUTES;
2945 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02002946 oparms.create_options = cifs_create_options(cifs_sb, create_options);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002947 oparms.fid = &fid;
2948 oparms.reconnect = false;
2949
Olivier Deprez157378f2022-04-04 15:47:50 +02002950 rc = SMB2_open_init(tcon, server,
2951 &rqst[0], &oplock, &oparms, utf16_path);
David Brazdil0f672f62019-12-10 10:32:29 +00002952 if (rc)
2953 goto querty_exit;
2954 smb2_set_next_command(tcon, &rqst[0]);
2955
2956
2957 /* IOCTL */
2958 memset(&io_iov, 0, sizeof(io_iov));
2959 rqst[1].rq_iov = io_iov;
2960 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2961
Olivier Deprez157378f2022-04-04 15:47:50 +02002962 rc = SMB2_ioctl_init(tcon, server,
2963 &rqst[1], fid.persistent_fid,
Olivier Deprez92d4c212022-12-06 15:05:30 +01002964 fid.volatile_fid, FSCTL_GET_REPARSE_POINT, NULL, 0,
Olivier Deprez0e641232021-09-23 10:07:05 +02002965 CIFSMaxBufSize -
2966 MAX_SMB2_CREATE_RESPONSE_SIZE -
2967 MAX_SMB2_CLOSE_RESPONSE_SIZE);
David Brazdil0f672f62019-12-10 10:32:29 +00002968 if (rc)
2969 goto querty_exit;
2970
2971 smb2_set_next_command(tcon, &rqst[1]);
2972 smb2_set_related(&rqst[1]);
2973
2974
2975 /* Close */
2976 memset(&close_iov, 0, sizeof(close_iov));
2977 rqst[2].rq_iov = close_iov;
2978 rqst[2].rq_nvec = 1;
2979
Olivier Deprez157378f2022-04-04 15:47:50 +02002980 rc = SMB2_close_init(tcon, server,
2981 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
David Brazdil0f672f62019-12-10 10:32:29 +00002982 if (rc)
2983 goto querty_exit;
2984
2985 smb2_set_related(&rqst[2]);
2986
Olivier Deprez157378f2022-04-04 15:47:50 +02002987 rc = compound_send_recv(xid, tcon->ses, server,
2988 flags, 3, rqst,
David Brazdil0f672f62019-12-10 10:32:29 +00002989 resp_buftype, rsp_iov);
2990
2991 create_rsp = rsp_iov[0].iov_base;
2992 if (create_rsp && create_rsp->sync_hdr.Status)
2993 err_iov = rsp_iov[0];
2994 ioctl_rsp = rsp_iov[1].iov_base;
2995
2996 /*
2997 * Open was successful and we got an ioctl response.
2998 */
2999 if ((rc == 0) && (is_reparse_point)) {
3000 /* See MS-FSCC 2.3.23 */
3001
3002 reparse_buf = (struct reparse_data_buffer *)
3003 ((char *)ioctl_rsp +
3004 le32_to_cpu(ioctl_rsp->OutputOffset));
3005 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3006
3007 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3008 rsp_iov[1].iov_len) {
3009 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
3010 plen);
3011 rc = -EIO;
3012 goto querty_exit;
3013 }
3014
3015 rc = parse_reparse_point(reparse_buf, plen, target_path,
3016 cifs_sb);
3017 goto querty_exit;
3018 }
3019
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003020 if (!rc || !err_iov.iov_base) {
3021 rc = -ENOENT;
David Brazdil0f672f62019-12-10 10:32:29 +00003022 goto querty_exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003023 }
3024
3025 err_buf = err_iov.iov_base;
3026 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
3027 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
David Brazdil0f672f62019-12-10 10:32:29 +00003028 rc = -EINVAL;
3029 goto querty_exit;
3030 }
3031
3032 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
3033 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
3034 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
3035 rc = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003036 goto querty_exit;
3037 }
3038
3039 /* open must fail on symlink - reset rc */
3040 rc = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003041 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3042 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
3043 print_len = le16_to_cpu(symlink->PrintNameLength);
3044 print_offset = le16_to_cpu(symlink->PrintNameOffset);
3045
3046 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
David Brazdil0f672f62019-12-10 10:32:29 +00003047 rc = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003048 goto querty_exit;
3049 }
3050
3051 if (err_iov.iov_len <
3052 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
David Brazdil0f672f62019-12-10 10:32:29 +00003053 rc = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003054 goto querty_exit;
3055 }
3056
3057 *target_path = cifs_strndup_from_utf16(
3058 (char *)symlink->PathBuffer + sub_offset,
3059 sub_len, true, cifs_sb->local_nls);
3060 if (!(*target_path)) {
3061 rc = -ENOMEM;
3062 goto querty_exit;
3063 }
3064 convert_delimiter(*target_path, '/');
3065 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3066
3067 querty_exit:
David Brazdil0f672f62019-12-10 10:32:29 +00003068 cifs_dbg(FYI, "query symlink rc %d\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003069 kfree(utf16_path);
David Brazdil0f672f62019-12-10 10:32:29 +00003070 SMB2_open_free(&rqst[0]);
3071 SMB2_ioctl_free(&rqst[1]);
3072 SMB2_close_free(&rqst[2]);
3073 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3074 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3075 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003076 return rc;
3077}
3078
Olivier Deprez157378f2022-04-04 15:47:50 +02003079int
3080smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3081 struct cifs_sb_info *cifs_sb, const char *full_path,
3082 __u32 *tag)
3083{
3084 int rc;
3085 __le16 *utf16_path = NULL;
3086 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3087 struct cifs_open_parms oparms;
3088 struct cifs_fid fid;
3089 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3090 int flags = CIFS_CP_CREATE_CLOSE_OP;
3091 struct smb_rqst rqst[3];
3092 int resp_buftype[3];
3093 struct kvec rsp_iov[3];
3094 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3095 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3096 struct kvec close_iov[1];
3097 struct smb2_ioctl_rsp *ioctl_rsp;
3098 struct reparse_data_buffer *reparse_buf;
3099 u32 plen;
3100
3101 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3102
3103 if (smb3_encryption_required(tcon))
3104 flags |= CIFS_TRANSFORM_REQ;
3105
3106 memset(rqst, 0, sizeof(rqst));
3107 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3108 memset(rsp_iov, 0, sizeof(rsp_iov));
3109
3110 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3111 if (!utf16_path)
3112 return -ENOMEM;
3113
3114 /*
3115 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3116 * to see if there is a handle already open that we can use
3117 */
3118 memset(&open_iov, 0, sizeof(open_iov));
3119 rqst[0].rq_iov = open_iov;
3120 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3121
3122 memset(&oparms, 0, sizeof(oparms));
3123 oparms.tcon = tcon;
3124 oparms.desired_access = FILE_READ_ATTRIBUTES;
3125 oparms.disposition = FILE_OPEN;
3126 oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3127 oparms.fid = &fid;
3128 oparms.reconnect = false;
3129
3130 rc = SMB2_open_init(tcon, server,
3131 &rqst[0], &oplock, &oparms, utf16_path);
3132 if (rc)
3133 goto query_rp_exit;
3134 smb2_set_next_command(tcon, &rqst[0]);
3135
3136
3137 /* IOCTL */
3138 memset(&io_iov, 0, sizeof(io_iov));
3139 rqst[1].rq_iov = io_iov;
3140 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3141
3142 rc = SMB2_ioctl_init(tcon, server,
3143 &rqst[1], COMPOUND_FID,
Olivier Deprez92d4c212022-12-06 15:05:30 +01003144 COMPOUND_FID, FSCTL_GET_REPARSE_POINT, NULL, 0,
Olivier Deprez157378f2022-04-04 15:47:50 +02003145 CIFSMaxBufSize -
3146 MAX_SMB2_CREATE_RESPONSE_SIZE -
3147 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3148 if (rc)
3149 goto query_rp_exit;
3150
3151 smb2_set_next_command(tcon, &rqst[1]);
3152 smb2_set_related(&rqst[1]);
3153
3154
3155 /* Close */
3156 memset(&close_iov, 0, sizeof(close_iov));
3157 rqst[2].rq_iov = close_iov;
3158 rqst[2].rq_nvec = 1;
3159
3160 rc = SMB2_close_init(tcon, server,
3161 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3162 if (rc)
3163 goto query_rp_exit;
3164
3165 smb2_set_related(&rqst[2]);
3166
3167 rc = compound_send_recv(xid, tcon->ses, server,
3168 flags, 3, rqst,
3169 resp_buftype, rsp_iov);
3170
3171 ioctl_rsp = rsp_iov[1].iov_base;
3172
3173 /*
3174 * Open was successful and we got an ioctl response.
3175 */
3176 if (rc == 0) {
3177 /* See MS-FSCC 2.3.23 */
3178
3179 reparse_buf = (struct reparse_data_buffer *)
3180 ((char *)ioctl_rsp +
3181 le32_to_cpu(ioctl_rsp->OutputOffset));
3182 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3183
3184 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3185 rsp_iov[1].iov_len) {
3186 cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3187 plen);
3188 rc = -EIO;
3189 goto query_rp_exit;
3190 }
3191 *tag = le32_to_cpu(reparse_buf->ReparseTag);
3192 }
3193
3194 query_rp_exit:
3195 kfree(utf16_path);
3196 SMB2_open_free(&rqst[0]);
3197 SMB2_ioctl_free(&rqst[1]);
3198 SMB2_close_free(&rqst[2]);
3199 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3200 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3201 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3202 return rc;
3203}
3204
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003205static struct cifs_ntsd *
3206get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3207 const struct cifs_fid *cifsfid, u32 *pacllen)
3208{
3209 struct cifs_ntsd *pntsd = NULL;
3210 unsigned int xid;
3211 int rc = -EOPNOTSUPP;
3212 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3213
3214 if (IS_ERR(tlink))
3215 return ERR_CAST(tlink);
3216
3217 xid = get_xid();
3218 cifs_dbg(FYI, "trying to get acl\n");
3219
3220 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3221 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
3222 free_xid(xid);
3223
3224 cifs_put_tlink(tlink);
3225
3226 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3227 if (rc)
3228 return ERR_PTR(rc);
3229 return pntsd;
3230
3231}
3232
3233static struct cifs_ntsd *
3234get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3235 const char *path, u32 *pacllen)
3236{
3237 struct cifs_ntsd *pntsd = NULL;
3238 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3239 unsigned int xid;
3240 int rc;
3241 struct cifs_tcon *tcon;
3242 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3243 struct cifs_fid fid;
3244 struct cifs_open_parms oparms;
3245 __le16 *utf16_path;
3246
3247 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3248 if (IS_ERR(tlink))
3249 return ERR_CAST(tlink);
3250
3251 tcon = tlink_tcon(tlink);
3252 xid = get_xid();
3253
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003254 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3255 if (!utf16_path) {
3256 rc = -ENOMEM;
3257 free_xid(xid);
3258 return ERR_PTR(rc);
3259 }
3260
3261 oparms.tcon = tcon;
3262 oparms.desired_access = READ_CONTROL;
3263 oparms.disposition = FILE_OPEN;
Olivier Deprez157378f2022-04-04 15:47:50 +02003264 /*
3265 * When querying an ACL, even if the file is a symlink we want to open
3266 * the source not the target, and so the protocol requires that the
3267 * client specify this flag when opening a reparse point
3268 */
3269 oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003270 oparms.fid = &fid;
3271 oparms.reconnect = false;
3272
Olivier Deprez157378f2022-04-04 15:47:50 +02003273 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3274 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003275 kfree(utf16_path);
3276 if (!rc) {
3277 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3278 fid.volatile_fid, (void **)&pntsd, pacllen);
3279 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3280 }
3281
3282 cifs_put_tlink(tlink);
3283 free_xid(xid);
3284
3285 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3286 if (rc)
3287 return ERR_PTR(rc);
3288 return pntsd;
3289}
3290
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003291static int
3292set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3293 struct inode *inode, const char *path, int aclflag)
3294{
3295 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3296 unsigned int xid;
3297 int rc, access_flags = 0;
3298 struct cifs_tcon *tcon;
3299 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3300 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3301 struct cifs_fid fid;
3302 struct cifs_open_parms oparms;
3303 __le16 *utf16_path;
3304
3305 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3306 if (IS_ERR(tlink))
3307 return PTR_ERR(tlink);
3308
3309 tcon = tlink_tcon(tlink);
3310 xid = get_xid();
3311
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003312 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3313 access_flags = WRITE_OWNER;
3314 else
3315 access_flags = WRITE_DAC;
3316
3317 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3318 if (!utf16_path) {
3319 rc = -ENOMEM;
3320 free_xid(xid);
3321 return rc;
3322 }
3323
3324 oparms.tcon = tcon;
3325 oparms.desired_access = access_flags;
Olivier Deprez157378f2022-04-04 15:47:50 +02003326 oparms.create_options = cifs_create_options(cifs_sb, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003327 oparms.disposition = FILE_OPEN;
3328 oparms.path = path;
3329 oparms.fid = &fid;
3330 oparms.reconnect = false;
3331
Olivier Deprez157378f2022-04-04 15:47:50 +02003332 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3333 NULL, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003334 kfree(utf16_path);
3335 if (!rc) {
3336 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3337 fid.volatile_fid, pnntsd, acllen, aclflag);
3338 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3339 }
3340
3341 cifs_put_tlink(tlink);
3342 free_xid(xid);
3343 return rc;
3344}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003345
3346/* Retrieve an ACL from the server */
3347static struct cifs_ntsd *
3348get_smb2_acl(struct cifs_sb_info *cifs_sb,
3349 struct inode *inode, const char *path,
3350 u32 *pacllen)
3351{
3352 struct cifs_ntsd *pntsd = NULL;
3353 struct cifsFileInfo *open_file = NULL;
3354
3355 if (inode)
3356 open_file = find_readable_file(CIFS_I(inode), true);
3357 if (!open_file)
3358 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3359
3360 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3361 cifsFileInfo_put(open_file);
3362 return pntsd;
3363}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003364
3365static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3366 loff_t offset, loff_t len, bool keep_size)
3367{
David Brazdil0f672f62019-12-10 10:32:29 +00003368 struct cifs_ses *ses = tcon->ses;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003369 struct inode *inode;
3370 struct cifsInodeInfo *cifsi;
3371 struct cifsFileInfo *cfile = file->private_data;
3372 struct file_zero_data_information fsctl_buf;
3373 long rc;
3374 unsigned int xid;
David Brazdil0f672f62019-12-10 10:32:29 +00003375 __le64 eof;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003376
3377 xid = get_xid();
3378
3379 inode = d_inode(cfile->dentry);
3380 cifsi = CIFS_I(inode);
3381
David Brazdil0f672f62019-12-10 10:32:29 +00003382 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3383 ses->Suid, offset, len);
3384
Olivier Deprez0e641232021-09-23 10:07:05 +02003385 /*
3386 * We zero the range through ioctl, so we need remove the page caches
3387 * first, otherwise the data may be inconsistent with the server.
3388 */
3389 truncate_pagecache_range(inode, offset, offset + len - 1);
David Brazdil0f672f62019-12-10 10:32:29 +00003390
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003391 /* if file not oplocked can't be sure whether asking to extend size */
3392 if (!CIFS_CACHE_READ(cifsi))
3393 if (keep_size == false) {
3394 rc = -EOPNOTSUPP;
David Brazdil0f672f62019-12-10 10:32:29 +00003395 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3396 tcon->tid, ses->Suid, offset, len, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003397 free_xid(xid);
3398 return rc;
3399 }
3400
David Brazdil0f672f62019-12-10 10:32:29 +00003401 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003402
3403 fsctl_buf.FileOffset = cpu_to_le64(offset);
3404 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3405
3406 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
Olivier Deprez92d4c212022-12-06 15:05:30 +01003407 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
David Brazdil0f672f62019-12-10 10:32:29 +00003408 (char *)&fsctl_buf,
3409 sizeof(struct file_zero_data_information),
3410 0, NULL, NULL);
3411 if (rc)
3412 goto zero_range_exit;
3413
3414 /*
3415 * do we also need to change the size of the file?
3416 */
3417 if (keep_size == false && i_size_read(inode) < offset + len) {
3418 eof = cpu_to_le64(offset + len);
3419 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3420 cfile->fid.volatile_fid, cfile->pid, &eof);
3421 }
3422
3423 zero_range_exit:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003424 free_xid(xid);
David Brazdil0f672f62019-12-10 10:32:29 +00003425 if (rc)
3426 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3427 ses->Suid, offset, len, rc);
3428 else
3429 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3430 ses->Suid, offset, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003431 return rc;
3432}
3433
3434static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3435 loff_t offset, loff_t len)
3436{
Olivier Deprez92d4c212022-12-06 15:05:30 +01003437 struct inode *inode = file_inode(file);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003438 struct cifsFileInfo *cfile = file->private_data;
3439 struct file_zero_data_information fsctl_buf;
3440 long rc;
3441 unsigned int xid;
3442 __u8 set_sparse = 1;
3443
3444 xid = get_xid();
3445
Olivier Deprez92d4c212022-12-06 15:05:30 +01003446 inode_lock(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003447 /* Need to make file sparse, if not already, before freeing range. */
3448 /* Consider adding equivalent for compressed since it could also work */
3449 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3450 rc = -EOPNOTSUPP;
Olivier Deprez92d4c212022-12-06 15:05:30 +01003451 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003452 }
3453
Olivier Deprez0e641232021-09-23 10:07:05 +02003454 /*
3455 * We implement the punch hole through ioctl, so we need remove the page
3456 * caches first, otherwise the data may be inconsistent with the server.
3457 */
3458 truncate_pagecache_range(inode, offset, offset + len - 1);
3459
David Brazdil0f672f62019-12-10 10:32:29 +00003460 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003461
3462 fsctl_buf.FileOffset = cpu_to_le64(offset);
3463 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3464
3465 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3466 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Olivier Deprez92d4c212022-12-06 15:05:30 +01003467 (char *)&fsctl_buf,
David Brazdil0f672f62019-12-10 10:32:29 +00003468 sizeof(struct file_zero_data_information),
3469 CIFSMaxBufSize, NULL, NULL);
Olivier Deprez92d4c212022-12-06 15:05:30 +01003470out:
3471 inode_unlock(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003472 free_xid(xid);
3473 return rc;
3474}
3475
Olivier Deprez157378f2022-04-04 15:47:50 +02003476static int smb3_simple_fallocate_write_range(unsigned int xid,
3477 struct cifs_tcon *tcon,
3478 struct cifsFileInfo *cfile,
3479 loff_t off, loff_t len,
3480 char *buf)
3481{
3482 struct cifs_io_parms io_parms = {0};
3483 int nbytes;
3484 int rc = 0;
3485 struct kvec iov[2];
3486
3487 io_parms.netfid = cfile->fid.netfid;
3488 io_parms.pid = current->tgid;
3489 io_parms.tcon = tcon;
3490 io_parms.persistent_fid = cfile->fid.persistent_fid;
3491 io_parms.volatile_fid = cfile->fid.volatile_fid;
3492
3493 while (len) {
3494 io_parms.offset = off;
3495 io_parms.length = len;
3496 if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3497 io_parms.length = SMB2_MAX_BUFFER_SIZE;
3498 /* iov[0] is reserved for smb header */
3499 iov[1].iov_base = buf;
3500 iov[1].iov_len = io_parms.length;
3501 rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3502 if (rc)
3503 break;
3504 if (nbytes > len)
3505 return -EINVAL;
3506 buf += nbytes;
3507 off += nbytes;
3508 len -= nbytes;
3509 }
3510 return rc;
3511}
3512
3513static int smb3_simple_fallocate_range(unsigned int xid,
3514 struct cifs_tcon *tcon,
3515 struct cifsFileInfo *cfile,
3516 loff_t off, loff_t len)
3517{
3518 struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3519 u32 out_data_len;
3520 char *buf = NULL;
3521 loff_t l;
3522 int rc;
3523
3524 in_data.file_offset = cpu_to_le64(off);
3525 in_data.length = cpu_to_le64(len);
3526 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3527 cfile->fid.volatile_fid,
Olivier Deprez92d4c212022-12-06 15:05:30 +01003528 FSCTL_QUERY_ALLOCATED_RANGES,
Olivier Deprez157378f2022-04-04 15:47:50 +02003529 (char *)&in_data, sizeof(in_data),
3530 1024 * sizeof(struct file_allocated_range_buffer),
3531 (char **)&out_data, &out_data_len);
3532 if (rc)
3533 goto out;
3534
3535 buf = kzalloc(1024 * 1024, GFP_KERNEL);
3536 if (buf == NULL) {
3537 rc = -ENOMEM;
3538 goto out;
3539 }
3540
3541 tmp_data = out_data;
3542 while (len) {
3543 /*
3544 * The rest of the region is unmapped so write it all.
3545 */
3546 if (out_data_len == 0) {
3547 rc = smb3_simple_fallocate_write_range(xid, tcon,
3548 cfile, off, len, buf);
3549 goto out;
3550 }
3551
3552 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3553 rc = -EINVAL;
3554 goto out;
3555 }
3556
3557 if (off < le64_to_cpu(tmp_data->file_offset)) {
3558 /*
3559 * We are at a hole. Write until the end of the region
3560 * or until the next allocated data,
3561 * whichever comes next.
3562 */
3563 l = le64_to_cpu(tmp_data->file_offset) - off;
3564 if (len < l)
3565 l = len;
3566 rc = smb3_simple_fallocate_write_range(xid, tcon,
3567 cfile, off, l, buf);
3568 if (rc)
3569 goto out;
3570 off = off + l;
3571 len = len - l;
3572 if (len == 0)
3573 goto out;
3574 }
3575 /*
3576 * We are at a section of allocated data, just skip forward
3577 * until the end of the data or the end of the region
3578 * we are supposed to fallocate, whichever comes first.
3579 */
3580 l = le64_to_cpu(tmp_data->length);
3581 if (len < l)
3582 l = len;
3583 off += l;
3584 len -= l;
3585
3586 tmp_data = &tmp_data[1];
3587 out_data_len -= sizeof(struct file_allocated_range_buffer);
3588 }
3589
3590 out:
3591 kfree(out_data);
3592 kfree(buf);
3593 return rc;
3594}
3595
3596
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003597static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3598 loff_t off, loff_t len, bool keep_size)
3599{
3600 struct inode *inode;
3601 struct cifsInodeInfo *cifsi;
3602 struct cifsFileInfo *cfile = file->private_data;
3603 long rc = -EOPNOTSUPP;
3604 unsigned int xid;
David Brazdil0f672f62019-12-10 10:32:29 +00003605 __le64 eof;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003606
3607 xid = get_xid();
3608
3609 inode = d_inode(cfile->dentry);
3610 cifsi = CIFS_I(inode);
3611
David Brazdil0f672f62019-12-10 10:32:29 +00003612 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3613 tcon->ses->Suid, off, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003614 /* if file not oplocked can't be sure whether asking to extend size */
3615 if (!CIFS_CACHE_READ(cifsi))
3616 if (keep_size == false) {
David Brazdil0f672f62019-12-10 10:32:29 +00003617 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3618 tcon->tid, tcon->ses->Suid, off, len, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003619 free_xid(xid);
3620 return rc;
3621 }
3622
3623 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02003624 * Extending the file
3625 */
3626 if ((keep_size == false) && i_size_read(inode) < off + len) {
3627 rc = inode_newsize_ok(inode, off + len);
3628 if (rc)
3629 goto out;
3630
Olivier Deprez92d4c212022-12-06 15:05:30 +01003631 if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
Olivier Deprez157378f2022-04-04 15:47:50 +02003632 smb2_set_sparse(xid, tcon, cfile, inode, false);
3633
3634 eof = cpu_to_le64(off + len);
3635 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3636 cfile->fid.volatile_fid, cfile->pid, &eof);
3637 if (rc == 0) {
3638 cifsi->server_eof = off + len;
3639 cifs_setsize(inode, off + len);
3640 cifs_truncate_page(inode->i_mapping, inode->i_size);
3641 truncate_setsize(inode, off + len);
3642 }
3643 goto out;
3644 }
3645
3646 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003647 * Files are non-sparse by default so falloc may be a no-op
Olivier Deprez157378f2022-04-04 15:47:50 +02003648 * Must check if file sparse. If not sparse, and since we are not
3649 * extending then no need to do anything since file already allocated
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003650 */
3651 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003652 rc = 0;
3653 goto out;
3654 }
3655
3656 if (keep_size == true) {
3657 /*
3658 * We can not preallocate pages beyond the end of the file
3659 * in SMB2
3660 */
3661 if (off >= i_size_read(inode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003662 rc = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02003663 goto out;
3664 }
3665 /*
3666 * For fallocates that are partially beyond the end of file,
3667 * clamp len so we only fallocate up to the end of file.
3668 */
3669 if (off + len > i_size_read(inode)) {
3670 len = i_size_read(inode) - off;
3671 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003672 }
3673
3674 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3675 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02003676 * At this point, we are trying to fallocate an internal
3677 * regions of a sparse file. Since smb2 does not have a
3678 * fallocate command we have two otions on how to emulate this.
3679 * We can either turn the entire file to become non-sparse
3680 * which we only do if the fallocate is for virtually
3681 * the whole file, or we can overwrite the region with zeroes
3682 * using SMB2_write, which could be prohibitevly expensive
3683 * if len is large.
3684 */
3685 /*
3686 * We are only trying to fallocate a small region so
3687 * just write it with zero.
3688 */
3689 if (len <= 1024 * 1024) {
3690 rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3691 off, len);
3692 goto out;
3693 }
3694
3695 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003696 * Check if falloc starts within first few pages of file
3697 * and ends within a few pages of the end of file to
3698 * ensure that most of file is being forced to be
3699 * fallocated now. If so then setting whole file sparse
3700 * ie potentially making a few extra pages at the beginning
3701 * or end of the file non-sparse via set_sparse is harmless.
3702 */
3703 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3704 rc = -EOPNOTSUPP;
Olivier Deprez157378f2022-04-04 15:47:50 +02003705 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00003706 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003707 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003708
Olivier Deprez157378f2022-04-04 15:47:50 +02003709 smb2_set_sparse(xid, tcon, cfile, inode, false);
3710 rc = 0;
3711
3712out:
David Brazdil0f672f62019-12-10 10:32:29 +00003713 if (rc)
3714 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3715 tcon->ses->Suid, off, len, rc);
3716 else
3717 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3718 tcon->ses->Suid, off, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003719
3720 free_xid(xid);
3721 return rc;
3722}
3723
David Brazdil0f672f62019-12-10 10:32:29 +00003724static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3725{
3726 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3727 struct cifsInodeInfo *cifsi;
3728 struct inode *inode;
3729 int rc = 0;
3730 struct file_allocated_range_buffer in_data, *out_data = NULL;
3731 u32 out_data_len;
3732 unsigned int xid;
3733
3734 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3735 return generic_file_llseek(file, offset, whence);
3736
3737 inode = d_inode(cfile->dentry);
3738 cifsi = CIFS_I(inode);
3739
3740 if (offset < 0 || offset >= i_size_read(inode))
3741 return -ENXIO;
3742
3743 xid = get_xid();
3744 /*
3745 * We need to be sure that all dirty pages are written as they
3746 * might fill holes on the server.
3747 * Note that we also MUST flush any written pages since at least
3748 * some servers (Windows2016) will not reflect recent writes in
3749 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3750 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003751 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
David Brazdil0f672f62019-12-10 10:32:29 +00003752 if (wrcfile) {
3753 filemap_write_and_wait(inode->i_mapping);
3754 smb2_flush_file(xid, tcon, &wrcfile->fid);
3755 cifsFileInfo_put(wrcfile);
3756 }
3757
3758 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3759 if (whence == SEEK_HOLE)
3760 offset = i_size_read(inode);
3761 goto lseek_exit;
3762 }
3763
3764 in_data.file_offset = cpu_to_le64(offset);
3765 in_data.length = cpu_to_le64(i_size_read(inode));
3766
3767 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3768 cfile->fid.volatile_fid,
Olivier Deprez92d4c212022-12-06 15:05:30 +01003769 FSCTL_QUERY_ALLOCATED_RANGES,
David Brazdil0f672f62019-12-10 10:32:29 +00003770 (char *)&in_data, sizeof(in_data),
3771 sizeof(struct file_allocated_range_buffer),
3772 (char **)&out_data, &out_data_len);
3773 if (rc == -E2BIG)
3774 rc = 0;
3775 if (rc)
3776 goto lseek_exit;
3777
3778 if (whence == SEEK_HOLE && out_data_len == 0)
3779 goto lseek_exit;
3780
3781 if (whence == SEEK_DATA && out_data_len == 0) {
3782 rc = -ENXIO;
3783 goto lseek_exit;
3784 }
3785
3786 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3787 rc = -EINVAL;
3788 goto lseek_exit;
3789 }
3790 if (whence == SEEK_DATA) {
3791 offset = le64_to_cpu(out_data->file_offset);
3792 goto lseek_exit;
3793 }
3794 if (offset < le64_to_cpu(out_data->file_offset))
3795 goto lseek_exit;
3796
3797 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3798
3799 lseek_exit:
3800 free_xid(xid);
3801 kfree(out_data);
3802 if (!rc)
3803 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3804 else
3805 return rc;
3806}
3807
3808static int smb3_fiemap(struct cifs_tcon *tcon,
3809 struct cifsFileInfo *cfile,
3810 struct fiemap_extent_info *fei, u64 start, u64 len)
3811{
3812 unsigned int xid;
3813 struct file_allocated_range_buffer in_data, *out_data;
3814 u32 out_data_len;
3815 int i, num, rc, flags, last_blob;
3816 u64 next;
3817
Olivier Deprez157378f2022-04-04 15:47:50 +02003818 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3819 if (rc)
3820 return rc;
David Brazdil0f672f62019-12-10 10:32:29 +00003821
3822 xid = get_xid();
3823 again:
3824 in_data.file_offset = cpu_to_le64(start);
3825 in_data.length = cpu_to_le64(len);
3826
3827 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3828 cfile->fid.volatile_fid,
Olivier Deprez92d4c212022-12-06 15:05:30 +01003829 FSCTL_QUERY_ALLOCATED_RANGES,
David Brazdil0f672f62019-12-10 10:32:29 +00003830 (char *)&in_data, sizeof(in_data),
3831 1024 * sizeof(struct file_allocated_range_buffer),
3832 (char **)&out_data, &out_data_len);
3833 if (rc == -E2BIG) {
3834 last_blob = 0;
3835 rc = 0;
3836 } else
3837 last_blob = 1;
3838 if (rc)
3839 goto out;
3840
Olivier Deprez0e641232021-09-23 10:07:05 +02003841 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003842 rc = -EINVAL;
3843 goto out;
3844 }
3845 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3846 rc = -EINVAL;
3847 goto out;
3848 }
3849
3850 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3851 for (i = 0; i < num; i++) {
3852 flags = 0;
3853 if (i == num - 1 && last_blob)
3854 flags |= FIEMAP_EXTENT_LAST;
3855
3856 rc = fiemap_fill_next_extent(fei,
3857 le64_to_cpu(out_data[i].file_offset),
3858 le64_to_cpu(out_data[i].file_offset),
3859 le64_to_cpu(out_data[i].length),
3860 flags);
3861 if (rc < 0)
3862 goto out;
3863 if (rc == 1) {
3864 rc = 0;
3865 goto out;
3866 }
3867 }
3868
3869 if (!last_blob) {
3870 next = le64_to_cpu(out_data[num - 1].file_offset) +
3871 le64_to_cpu(out_data[num - 1].length);
3872 len = len - (next - start);
3873 start = next;
3874 goto again;
3875 }
3876
3877 out:
3878 free_xid(xid);
3879 kfree(out_data);
3880 return rc;
3881}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003882
3883static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3884 loff_t off, loff_t len)
3885{
3886 /* KEEP_SIZE already checked for by do_fallocate */
3887 if (mode & FALLOC_FL_PUNCH_HOLE)
3888 return smb3_punch_hole(file, tcon, off, len);
3889 else if (mode & FALLOC_FL_ZERO_RANGE) {
3890 if (mode & FALLOC_FL_KEEP_SIZE)
3891 return smb3_zero_range(file, tcon, off, len, true);
3892 return smb3_zero_range(file, tcon, off, len, false);
3893 } else if (mode == FALLOC_FL_KEEP_SIZE)
3894 return smb3_simple_falloc(file, tcon, off, len, true);
3895 else if (mode == 0)
3896 return smb3_simple_falloc(file, tcon, off, len, false);
3897
3898 return -EOPNOTSUPP;
3899}
3900
3901static void
3902smb2_downgrade_oplock(struct TCP_Server_Info *server,
Olivier Deprez0e641232021-09-23 10:07:05 +02003903 struct cifsInodeInfo *cinode, __u32 oplock,
3904 unsigned int epoch, bool *purge_cache)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003905{
Olivier Deprez0e641232021-09-23 10:07:05 +02003906 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003907}
3908
3909static void
Olivier Deprez0e641232021-09-23 10:07:05 +02003910smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3911 unsigned int epoch, bool *purge_cache);
3912
3913static void
3914smb3_downgrade_oplock(struct TCP_Server_Info *server,
3915 struct cifsInodeInfo *cinode, __u32 oplock,
3916 unsigned int epoch, bool *purge_cache)
David Brazdil0f672f62019-12-10 10:32:29 +00003917{
Olivier Deprez0e641232021-09-23 10:07:05 +02003918 unsigned int old_state = cinode->oplock;
3919 unsigned int old_epoch = cinode->epoch;
3920 unsigned int new_state;
3921
3922 if (epoch > old_epoch) {
3923 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3924 cinode->epoch = epoch;
3925 }
3926
3927 new_state = cinode->oplock;
3928 *purge_cache = false;
3929
3930 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3931 (new_state & CIFS_CACHE_READ_FLG) == 0)
3932 *purge_cache = true;
3933 else if (old_state == new_state && (epoch - old_epoch > 1))
3934 *purge_cache = true;
David Brazdil0f672f62019-12-10 10:32:29 +00003935}
3936
3937static void
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003938smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3939 unsigned int epoch, bool *purge_cache)
3940{
3941 oplock &= 0xFF;
3942 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3943 return;
3944 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3945 cinode->oplock = CIFS_CACHE_RHW_FLG;
3946 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3947 &cinode->vfs_inode);
3948 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3949 cinode->oplock = CIFS_CACHE_RW_FLG;
3950 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3951 &cinode->vfs_inode);
3952 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3953 cinode->oplock = CIFS_CACHE_READ_FLG;
3954 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3955 &cinode->vfs_inode);
3956 } else
3957 cinode->oplock = 0;
3958}
3959
3960static void
3961smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3962 unsigned int epoch, bool *purge_cache)
3963{
3964 char message[5] = {0};
David Brazdil0f672f62019-12-10 10:32:29 +00003965 unsigned int new_oplock = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003966
3967 oplock &= 0xFF;
3968 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3969 return;
3970
David Brazdil0f672f62019-12-10 10:32:29 +00003971 /* Check if the server granted an oplock rather than a lease */
3972 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3973 return smb2_set_oplock_level(cinode, oplock, epoch,
3974 purge_cache);
3975
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003976 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
David Brazdil0f672f62019-12-10 10:32:29 +00003977 new_oplock |= CIFS_CACHE_READ_FLG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003978 strcat(message, "R");
3979 }
3980 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
David Brazdil0f672f62019-12-10 10:32:29 +00003981 new_oplock |= CIFS_CACHE_HANDLE_FLG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003982 strcat(message, "H");
3983 }
3984 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
David Brazdil0f672f62019-12-10 10:32:29 +00003985 new_oplock |= CIFS_CACHE_WRITE_FLG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003986 strcat(message, "W");
3987 }
David Brazdil0f672f62019-12-10 10:32:29 +00003988 if (!new_oplock)
3989 strncpy(message, "None", sizeof(message));
3990
3991 cinode->oplock = new_oplock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003992 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3993 &cinode->vfs_inode);
3994}
3995
3996static void
3997smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3998 unsigned int epoch, bool *purge_cache)
3999{
4000 unsigned int old_oplock = cinode->oplock;
4001
4002 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
4003
4004 if (purge_cache) {
4005 *purge_cache = false;
4006 if (old_oplock == CIFS_CACHE_READ_FLG) {
4007 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
4008 (epoch - cinode->epoch > 0))
4009 *purge_cache = true;
4010 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4011 (epoch - cinode->epoch > 1))
4012 *purge_cache = true;
4013 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4014 (epoch - cinode->epoch > 1))
4015 *purge_cache = true;
4016 else if (cinode->oplock == 0 &&
4017 (epoch - cinode->epoch > 0))
4018 *purge_cache = true;
4019 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
4020 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4021 (epoch - cinode->epoch > 0))
4022 *purge_cache = true;
4023 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4024 (epoch - cinode->epoch > 1))
4025 *purge_cache = true;
4026 }
4027 cinode->epoch = epoch;
4028 }
4029}
4030
Olivier Deprez92d4c212022-12-06 15:05:30 +01004031#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004032static bool
4033smb2_is_read_op(__u32 oplock)
4034{
4035 return oplock == SMB2_OPLOCK_LEVEL_II;
4036}
Olivier Deprez92d4c212022-12-06 15:05:30 +01004037#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004038
4039static bool
4040smb21_is_read_op(__u32 oplock)
4041{
4042 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
4043 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
4044}
4045
4046static __le32
4047map_oplock_to_lease(u8 oplock)
4048{
4049 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4050 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
4051 else if (oplock == SMB2_OPLOCK_LEVEL_II)
4052 return SMB2_LEASE_READ_CACHING;
4053 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
4054 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
4055 SMB2_LEASE_WRITE_CACHING;
4056 return 0;
4057}
4058
4059static char *
4060smb2_create_lease_buf(u8 *lease_key, u8 oplock)
4061{
4062 struct create_lease *buf;
4063
4064 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
4065 if (!buf)
4066 return NULL;
4067
4068 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4069 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4070
4071 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4072 (struct create_lease, lcontext));
4073 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4074 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4075 (struct create_lease, Name));
4076 buf->ccontext.NameLength = cpu_to_le16(4);
4077 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4078 buf->Name[0] = 'R';
4079 buf->Name[1] = 'q';
4080 buf->Name[2] = 'L';
4081 buf->Name[3] = 's';
4082 return (char *)buf;
4083}
4084
4085static char *
4086smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4087{
4088 struct create_lease_v2 *buf;
4089
4090 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4091 if (!buf)
4092 return NULL;
4093
4094 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4095 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4096
4097 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4098 (struct create_lease_v2, lcontext));
4099 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4100 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4101 (struct create_lease_v2, Name));
4102 buf->ccontext.NameLength = cpu_to_le16(4);
4103 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4104 buf->Name[0] = 'R';
4105 buf->Name[1] = 'q';
4106 buf->Name[2] = 'L';
4107 buf->Name[3] = 's';
4108 return (char *)buf;
4109}
4110
4111static __u8
4112smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4113{
4114 struct create_lease *lc = (struct create_lease *)buf;
4115
4116 *epoch = 0; /* not used */
4117 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4118 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4119 return le32_to_cpu(lc->lcontext.LeaseState);
4120}
4121
4122static __u8
4123smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
4124{
4125 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4126
4127 *epoch = le16_to_cpu(lc->lcontext.Epoch);
4128 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4129 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4130 if (lease_key)
4131 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4132 return le32_to_cpu(lc->lcontext.LeaseState);
4133}
4134
4135static unsigned int
4136smb2_wp_retry_size(struct inode *inode)
4137{
4138 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
4139 SMB2_MAX_BUFFER_SIZE);
4140}
4141
4142static bool
4143smb2_dir_needs_close(struct cifsFileInfo *cfile)
4144{
4145 return !cfile->invalidHandle;
4146}
4147
4148static void
4149fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
David Brazdil0f672f62019-12-10 10:32:29 +00004150 struct smb_rqst *old_rq, __le16 cipher_type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004151{
4152 struct smb2_sync_hdr *shdr =
4153 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
4154
4155 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4156 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4157 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4158 tr_hdr->Flags = cpu_to_le16(0x01);
Olivier Deprez157378f2022-04-04 15:47:50 +02004159 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4160 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4161 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
David Brazdil0f672f62019-12-10 10:32:29 +00004162 else
Olivier Deprez157378f2022-04-04 15:47:50 +02004163 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004164 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4165}
4166
4167/* We can not use the normal sg_set_buf() as we will sometimes pass a
4168 * stack object as buf.
4169 */
4170static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
4171 unsigned int buflen)
4172{
David Brazdil0f672f62019-12-10 10:32:29 +00004173 void *addr;
4174 /*
4175 * VMAP_STACK (at least) puts stack into the vmalloc address space
4176 */
4177 if (is_vmalloc_addr(buf))
4178 addr = vmalloc_to_page(buf);
4179 else
4180 addr = virt_to_page(buf);
4181 sg_set_page(sg, addr, buflen, offset_in_page(buf));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004182}
4183
4184/* Assumes the first rqst has a transform header as the first iov.
4185 * I.e.
4186 * rqst[0].rq_iov[0] is transform header
4187 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4188 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
4189 */
4190static struct scatterlist *
4191init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
4192{
4193 unsigned int sg_len;
4194 struct scatterlist *sg;
4195 unsigned int i;
4196 unsigned int j;
4197 unsigned int idx = 0;
4198 int skip;
4199
4200 sg_len = 1;
4201 for (i = 0; i < num_rqst; i++)
4202 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
4203
4204 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4205 if (!sg)
4206 return NULL;
4207
4208 sg_init_table(sg, sg_len);
4209 for (i = 0; i < num_rqst; i++) {
4210 for (j = 0; j < rqst[i].rq_nvec; j++) {
4211 /*
4212 * The first rqst has a transform header where the
4213 * first 20 bytes are not part of the encrypted blob
4214 */
4215 skip = (i == 0) && (j == 0) ? 20 : 0;
4216 smb2_sg_set_buf(&sg[idx++],
4217 rqst[i].rq_iov[j].iov_base + skip,
4218 rqst[i].rq_iov[j].iov_len - skip);
David Brazdil0f672f62019-12-10 10:32:29 +00004219 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004220
4221 for (j = 0; j < rqst[i].rq_npages; j++) {
4222 unsigned int len, offset;
4223
4224 rqst_page_get_length(&rqst[i], j, &len, &offset);
4225 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4226 }
4227 }
4228 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
4229 return sg;
4230}
4231
4232static int
4233smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4234{
4235 struct cifs_ses *ses;
4236 u8 *ses_enc_key;
4237
4238 spin_lock(&cifs_tcp_ses_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02004239 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4240 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4241 if (ses->Suid == ses_id) {
4242 ses_enc_key = enc ? ses->smb3encryptionkey :
4243 ses->smb3decryptionkey;
4244 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4245 spin_unlock(&cifs_tcp_ses_lock);
4246 return 0;
4247 }
4248 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004249 }
4250 spin_unlock(&cifs_tcp_ses_lock);
4251
Olivier Deprez0e641232021-09-23 10:07:05 +02004252 return -EAGAIN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004253}
4254/*
4255 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4256 * iov[0] - transform header (associate data),
4257 * iov[1-N] - SMB2 header and pages - data to encrypt.
4258 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4259 * untouched.
4260 */
4261static int
4262crypt_message(struct TCP_Server_Info *server, int num_rqst,
4263 struct smb_rqst *rqst, int enc)
4264{
4265 struct smb2_transform_hdr *tr_hdr =
4266 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4267 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4268 int rc = 0;
4269 struct scatterlist *sg;
4270 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Olivier Deprez157378f2022-04-04 15:47:50 +02004271 u8 key[SMB3_ENC_DEC_KEY_SIZE];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004272 struct aead_request *req;
4273 char *iv;
4274 unsigned int iv_len;
4275 DECLARE_CRYPTO_WAIT(wait);
4276 struct crypto_aead *tfm;
4277 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4278
4279 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4280 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00004281 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004282 enc ? "en" : "de");
Olivier Deprez0e641232021-09-23 10:07:05 +02004283 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004284 }
4285
4286 rc = smb3_crypto_aead_allocate(server);
4287 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00004288 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004289 return rc;
4290 }
4291
4292 tfm = enc ? server->secmech.ccmaesencrypt :
4293 server->secmech.ccmaesdecrypt;
Olivier Deprez157378f2022-04-04 15:47:50 +02004294
4295 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4296 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4297 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4298 else
4299 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4300
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004301 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00004302 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004303 return rc;
4304 }
4305
4306 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4307 if (rc) {
David Brazdil0f672f62019-12-10 10:32:29 +00004308 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004309 return rc;
4310 }
4311
4312 req = aead_request_alloc(tfm, GFP_KERNEL);
4313 if (!req) {
David Brazdil0f672f62019-12-10 10:32:29 +00004314 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004315 return -ENOMEM;
4316 }
4317
4318 if (!enc) {
4319 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4320 crypt_len += SMB2_SIGNATURE_SIZE;
4321 }
4322
4323 sg = init_sg(num_rqst, rqst, sign);
4324 if (!sg) {
David Brazdil0f672f62019-12-10 10:32:29 +00004325 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004326 rc = -ENOMEM;
4327 goto free_req;
4328 }
4329
4330 iv_len = crypto_aead_ivsize(tfm);
4331 iv = kzalloc(iv_len, GFP_KERNEL);
4332 if (!iv) {
David Brazdil0f672f62019-12-10 10:32:29 +00004333 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004334 rc = -ENOMEM;
4335 goto free_sg;
4336 }
David Brazdil0f672f62019-12-10 10:32:29 +00004337
Olivier Deprez157378f2022-04-04 15:47:50 +02004338 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4339 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4340 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
David Brazdil0f672f62019-12-10 10:32:29 +00004341 else {
4342 iv[0] = 3;
Olivier Deprez157378f2022-04-04 15:47:50 +02004343 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
David Brazdil0f672f62019-12-10 10:32:29 +00004344 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004345
4346 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4347 aead_request_set_ad(req, assoc_data_len);
4348
4349 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4350 crypto_req_done, &wait);
4351
4352 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4353 : crypto_aead_decrypt(req), &wait);
4354
4355 if (!rc && enc)
4356 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4357
4358 kfree(iv);
4359free_sg:
4360 kfree(sg);
4361free_req:
4362 kfree(req);
4363 return rc;
4364}
4365
4366void
4367smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4368{
4369 int i, j;
4370
4371 for (i = 0; i < num_rqst; i++) {
4372 if (rqst[i].rq_pages) {
4373 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4374 put_page(rqst[i].rq_pages[j]);
4375 kfree(rqst[i].rq_pages);
4376 }
4377 }
4378}
4379
4380/*
4381 * This function will initialize new_rq and encrypt the content.
4382 * The first entry, new_rq[0], only contains a single iov which contains
4383 * a smb2_transform_hdr and is pre-allocated by the caller.
4384 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4385 *
4386 * The end result is an array of smb_rqst structures where the first structure
4387 * only contains a single iov for the transform header which we then can pass
4388 * to crypt_message().
4389 *
4390 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4391 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4392 */
4393static int
4394smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4395 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4396{
4397 struct page **pages;
4398 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4399 unsigned int npages;
4400 unsigned int orig_len = 0;
4401 int i, j;
4402 int rc = -ENOMEM;
4403
4404 for (i = 1; i < num_rqst; i++) {
4405 npages = old_rq[i - 1].rq_npages;
4406 pages = kmalloc_array(npages, sizeof(struct page *),
4407 GFP_KERNEL);
4408 if (!pages)
4409 goto err_free;
4410
4411 new_rq[i].rq_pages = pages;
4412 new_rq[i].rq_npages = npages;
4413 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4414 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4415 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4416 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4417 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4418
4419 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4420
4421 for (j = 0; j < npages; j++) {
4422 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4423 if (!pages[j])
4424 goto err_free;
4425 }
4426
4427 /* copy pages form the old */
4428 for (j = 0; j < npages; j++) {
4429 char *dst, *src;
4430 unsigned int offset, len;
4431
4432 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4433
4434 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4435 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4436
4437 memcpy(dst, src, len);
4438 kunmap(new_rq[i].rq_pages[j]);
4439 kunmap(old_rq[i - 1].rq_pages[j]);
4440 }
4441 }
4442
4443 /* fill the 1st iov with a transform header */
David Brazdil0f672f62019-12-10 10:32:29 +00004444 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004445
4446 rc = crypt_message(server, num_rqst, new_rq, 1);
David Brazdil0f672f62019-12-10 10:32:29 +00004447 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004448 if (rc)
4449 goto err_free;
4450
4451 return rc;
4452
4453err_free:
4454 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4455 return rc;
4456}
4457
4458static int
4459smb3_is_transform_hdr(void *buf)
4460{
4461 struct smb2_transform_hdr *trhdr = buf;
4462
4463 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4464}
4465
4466static int
4467decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4468 unsigned int buf_data_size, struct page **pages,
Olivier Deprez0e641232021-09-23 10:07:05 +02004469 unsigned int npages, unsigned int page_data_size,
4470 bool is_offloaded)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004471{
4472 struct kvec iov[2];
4473 struct smb_rqst rqst = {NULL};
4474 int rc;
4475
4476 iov[0].iov_base = buf;
4477 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4478 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4479 iov[1].iov_len = buf_data_size;
4480
4481 rqst.rq_iov = iov;
4482 rqst.rq_nvec = 2;
4483 rqst.rq_pages = pages;
4484 rqst.rq_npages = npages;
4485 rqst.rq_pagesz = PAGE_SIZE;
4486 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4487
4488 rc = crypt_message(server, 1, &rqst, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00004489 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004490
4491 if (rc)
4492 return rc;
4493
4494 memmove(buf, iov[1].iov_base, buf_data_size);
4495
Olivier Deprez0e641232021-09-23 10:07:05 +02004496 if (!is_offloaded)
4497 server->total_read = buf_data_size + page_data_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004498
4499 return rc;
4500}
4501
4502static int
4503read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4504 unsigned int npages, unsigned int len)
4505{
4506 int i;
4507 int length;
4508
4509 for (i = 0; i < npages; i++) {
4510 struct page *page = pages[i];
4511 size_t n;
4512
4513 n = len;
4514 if (len >= PAGE_SIZE) {
4515 /* enough data to fill the page */
4516 n = PAGE_SIZE;
4517 len -= n;
4518 } else {
4519 zero_user(page, len, PAGE_SIZE - len);
4520 len = 0;
4521 }
4522 length = cifs_read_page_from_socket(server, page, 0, n);
4523 if (length < 0)
4524 return length;
4525 server->total_read += length;
4526 }
4527
4528 return 0;
4529}
4530
4531static int
4532init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4533 unsigned int cur_off, struct bio_vec **page_vec)
4534{
4535 struct bio_vec *bvec;
4536 int i;
4537
4538 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4539 if (!bvec)
4540 return -ENOMEM;
4541
4542 for (i = 0; i < npages; i++) {
4543 bvec[i].bv_page = pages[i];
4544 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4545 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4546 data_size -= bvec[i].bv_len;
4547 }
4548
4549 if (data_size != 0) {
4550 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4551 kfree(bvec);
4552 return -EIO;
4553 }
4554
4555 *page_vec = bvec;
4556 return 0;
4557}
4558
4559static int
4560handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4561 char *buf, unsigned int buf_len, struct page **pages,
Olivier Deprez0e641232021-09-23 10:07:05 +02004562 unsigned int npages, unsigned int page_data_size,
4563 bool is_offloaded)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004564{
4565 unsigned int data_offset;
4566 unsigned int data_len;
4567 unsigned int cur_off;
4568 unsigned int cur_page_idx;
4569 unsigned int pad_len;
4570 struct cifs_readdata *rdata = mid->callback_data;
4571 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4572 struct bio_vec *bvec = NULL;
4573 struct iov_iter iter;
4574 struct kvec iov;
4575 int length;
4576 bool use_rdma_mr = false;
4577
4578 if (shdr->Command != SMB2_READ) {
David Brazdil0f672f62019-12-10 10:32:29 +00004579 cifs_server_dbg(VFS, "only big read responses are supported\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004580 return -ENOTSUPP;
4581 }
4582
4583 if (server->ops->is_session_expired &&
4584 server->ops->is_session_expired(buf)) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004585 if (!is_offloaded)
4586 cifs_reconnect(server);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004587 return -1;
4588 }
4589
4590 if (server->ops->is_status_pending &&
David Brazdil0f672f62019-12-10 10:32:29 +00004591 server->ops->is_status_pending(buf, server))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004592 return -1;
4593
David Brazdil0f672f62019-12-10 10:32:29 +00004594 /* set up first two iov to get credits */
4595 rdata->iov[0].iov_base = buf;
4596 rdata->iov[0].iov_len = 0;
4597 rdata->iov[1].iov_base = buf;
4598 rdata->iov[1].iov_len =
4599 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4600 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4601 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4602 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4603 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4604
4605 rdata->result = server->ops->map_error(buf, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004606 if (rdata->result != 0) {
4607 cifs_dbg(FYI, "%s: server returned error %d\n",
4608 __func__, rdata->result);
David Brazdil0f672f62019-12-10 10:32:29 +00004609 /* normal error on read response */
Olivier Deprez0e641232021-09-23 10:07:05 +02004610 if (is_offloaded)
4611 mid->mid_state = MID_RESPONSE_RECEIVED;
4612 else
4613 dequeue_mid(mid, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004614 return 0;
4615 }
4616
4617 data_offset = server->ops->read_data_offset(buf);
4618#ifdef CONFIG_CIFS_SMB_DIRECT
4619 use_rdma_mr = rdata->mr;
4620#endif
4621 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4622
4623 if (data_offset < server->vals->read_rsp_size) {
4624 /*
4625 * win2k8 sometimes sends an offset of 0 when the read
4626 * is beyond the EOF. Treat it as if the data starts just after
4627 * the header.
4628 */
4629 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4630 __func__, data_offset);
4631 data_offset = server->vals->read_rsp_size;
4632 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4633 /* data_offset is beyond the end of smallbuf */
4634 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4635 __func__, data_offset);
4636 rdata->result = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02004637 if (is_offloaded)
4638 mid->mid_state = MID_RESPONSE_MALFORMED;
4639 else
4640 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004641 return 0;
4642 }
4643
4644 pad_len = data_offset - server->vals->read_rsp_size;
4645
4646 if (buf_len <= data_offset) {
4647 /* read response payload is in pages */
4648 cur_page_idx = pad_len / PAGE_SIZE;
4649 cur_off = pad_len % PAGE_SIZE;
4650
4651 if (cur_page_idx != 0) {
4652 /* data offset is beyond the 1st page of response */
4653 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4654 __func__, data_offset);
4655 rdata->result = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02004656 if (is_offloaded)
4657 mid->mid_state = MID_RESPONSE_MALFORMED;
4658 else
4659 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004660 return 0;
4661 }
4662
4663 if (data_len > page_data_size - pad_len) {
4664 /* data_len is corrupt -- discard frame */
4665 rdata->result = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02004666 if (is_offloaded)
4667 mid->mid_state = MID_RESPONSE_MALFORMED;
4668 else
4669 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004670 return 0;
4671 }
4672
4673 rdata->result = init_read_bvec(pages, npages, page_data_size,
4674 cur_off, &bvec);
4675 if (rdata->result != 0) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004676 if (is_offloaded)
4677 mid->mid_state = MID_RESPONSE_MALFORMED;
4678 else
4679 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004680 return 0;
4681 }
4682
David Brazdil0f672f62019-12-10 10:32:29 +00004683 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004684 } else if (buf_len >= data_offset + data_len) {
4685 /* read response payload is in buf */
4686 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4687 iov.iov_base = buf + data_offset;
4688 iov.iov_len = data_len;
David Brazdil0f672f62019-12-10 10:32:29 +00004689 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004690 } else {
4691 /* read response payload cannot be in both buf and pages */
4692 WARN_ONCE(1, "buf can not contain only a part of read data");
4693 rdata->result = -EIO;
Olivier Deprez0e641232021-09-23 10:07:05 +02004694 if (is_offloaded)
4695 mid->mid_state = MID_RESPONSE_MALFORMED;
4696 else
4697 dequeue_mid(mid, rdata->result);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004698 return 0;
4699 }
4700
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004701 length = rdata->copy_into_pages(server, rdata, &iter);
4702
4703 kfree(bvec);
4704
4705 if (length < 0)
4706 return length;
4707
Olivier Deprez0e641232021-09-23 10:07:05 +02004708 if (is_offloaded)
4709 mid->mid_state = MID_RESPONSE_RECEIVED;
4710 else
4711 dequeue_mid(mid, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004712 return length;
4713}
4714
David Brazdil0f672f62019-12-10 10:32:29 +00004715struct smb2_decrypt_work {
4716 struct work_struct decrypt;
4717 struct TCP_Server_Info *server;
4718 struct page **ppages;
4719 char *buf;
4720 unsigned int npages;
4721 unsigned int len;
4722};
4723
4724
4725static void smb2_decrypt_offload(struct work_struct *work)
4726{
4727 struct smb2_decrypt_work *dw = container_of(work,
4728 struct smb2_decrypt_work, decrypt);
4729 int i, rc;
4730 struct mid_q_entry *mid;
4731
4732 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02004733 dw->ppages, dw->npages, dw->len, true);
David Brazdil0f672f62019-12-10 10:32:29 +00004734 if (rc) {
4735 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4736 goto free_pages;
4737 }
4738
4739 dw->server->lstrp = jiffies;
Olivier Deprez0e641232021-09-23 10:07:05 +02004740 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
David Brazdil0f672f62019-12-10 10:32:29 +00004741 if (mid == NULL)
4742 cifs_dbg(FYI, "mid not found\n");
4743 else {
4744 mid->decrypted = true;
4745 rc = handle_read_data(dw->server, mid, dw->buf,
4746 dw->server->vals->read_rsp_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02004747 dw->ppages, dw->npages, dw->len,
4748 true);
4749 if (rc >= 0) {
4750#ifdef CONFIG_CIFS_STATS2
4751 mid->when_received = jiffies;
4752#endif
4753 mid->callback(mid);
4754 } else {
4755 spin_lock(&GlobalMid_Lock);
4756 if (dw->server->tcpStatus == CifsNeedReconnect) {
4757 mid->mid_state = MID_RETRY_NEEDED;
4758 spin_unlock(&GlobalMid_Lock);
4759 mid->callback(mid);
4760 } else {
4761 mid->mid_state = MID_REQUEST_SUBMITTED;
4762 mid->mid_flags &= ~(MID_DELETED);
4763 list_add_tail(&mid->qhead,
4764 &dw->server->pending_mid_q);
4765 spin_unlock(&GlobalMid_Lock);
4766 }
4767 }
David Brazdil0f672f62019-12-10 10:32:29 +00004768 cifs_mid_q_entry_release(mid);
4769 }
4770
4771free_pages:
4772 for (i = dw->npages-1; i >= 0; i--)
4773 put_page(dw->ppages[i]);
4774
4775 kfree(dw->ppages);
4776 cifs_small_buf_release(dw->buf);
4777 kfree(dw);
4778}
4779
4780
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004781static int
David Brazdil0f672f62019-12-10 10:32:29 +00004782receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4783 int *num_mids)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004784{
4785 char *buf = server->smallbuf;
4786 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4787 unsigned int npages;
4788 struct page **pages;
4789 unsigned int len;
4790 unsigned int buflen = server->pdu_size;
4791 int rc;
4792 int i = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00004793 struct smb2_decrypt_work *dw;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004794
David Brazdil0f672f62019-12-10 10:32:29 +00004795 *num_mids = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004796 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4797 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4798
4799 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4800 if (rc < 0)
4801 return rc;
4802 server->total_read += rc;
4803
4804 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4805 server->vals->read_rsp_size;
4806 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4807
4808 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4809 if (!pages) {
4810 rc = -ENOMEM;
4811 goto discard_data;
4812 }
4813
4814 for (; i < npages; i++) {
4815 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4816 if (!pages[i]) {
4817 rc = -ENOMEM;
4818 goto discard_data;
4819 }
4820 }
4821
4822 /* read read data into pages */
4823 rc = read_data_into_pages(server, pages, npages, len);
4824 if (rc)
4825 goto free_pages;
4826
4827 rc = cifs_discard_remaining_data(server);
4828 if (rc)
4829 goto free_pages;
4830
David Brazdil0f672f62019-12-10 10:32:29 +00004831 /*
4832 * For large reads, offload to different thread for better performance,
4833 * use more cores decrypting which can be expensive
4834 */
4835
4836 if ((server->min_offload) && (server->in_flight > 1) &&
4837 (server->pdu_size >= server->min_offload)) {
4838 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4839 if (dw == NULL)
4840 goto non_offloaded_decrypt;
4841
4842 dw->buf = server->smallbuf;
4843 server->smallbuf = (char *)cifs_small_buf_get();
4844
4845 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4846
4847 dw->npages = npages;
4848 dw->server = server;
4849 dw->ppages = pages;
4850 dw->len = len;
4851 queue_work(decrypt_wq, &dw->decrypt);
4852 *num_mids = 0; /* worker thread takes care of finding mid */
4853 return -1;
4854 }
4855
4856non_offloaded_decrypt:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004857 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02004858 pages, npages, len, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004859 if (rc)
4860 goto free_pages;
4861
4862 *mid = smb2_find_mid(server, buf);
4863 if (*mid == NULL)
4864 cifs_dbg(FYI, "mid not found\n");
4865 else {
4866 cifs_dbg(FYI, "mid found\n");
4867 (*mid)->decrypted = true;
4868 rc = handle_read_data(server, *mid, buf,
4869 server->vals->read_rsp_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02004870 pages, npages, len, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004871 }
4872
4873free_pages:
4874 for (i = i - 1; i >= 0; i--)
4875 put_page(pages[i]);
4876 kfree(pages);
4877 return rc;
4878discard_data:
4879 cifs_discard_remaining_data(server);
4880 goto free_pages;
4881}
4882
4883static int
4884receive_encrypted_standard(struct TCP_Server_Info *server,
4885 struct mid_q_entry **mids, char **bufs,
4886 int *num_mids)
4887{
4888 int ret, length;
4889 char *buf = server->smallbuf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004890 struct smb2_sync_hdr *shdr;
4891 unsigned int pdu_length = server->pdu_size;
4892 unsigned int buf_size;
4893 struct mid_q_entry *mid_entry;
4894 int next_is_large;
4895 char *next_buffer = NULL;
4896
4897 *num_mids = 0;
4898
4899 /* switch to large buffer if too big for a small one */
4900 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4901 server->large_buf = true;
4902 memcpy(server->bigbuf, buf, server->total_read);
4903 buf = server->bigbuf;
4904 }
4905
4906 /* now read the rest */
4907 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4908 pdu_length - HEADER_SIZE(server) + 1);
4909 if (length < 0)
4910 return length;
4911 server->total_read += length;
4912
4913 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
Olivier Deprez0e641232021-09-23 10:07:05 +02004914 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004915 if (length)
4916 return length;
4917
4918 next_is_large = server->large_buf;
David Brazdil0f672f62019-12-10 10:32:29 +00004919one_more:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004920 shdr = (struct smb2_sync_hdr *)buf;
4921 if (shdr->NextCommand) {
David Brazdil0f672f62019-12-10 10:32:29 +00004922 if (next_is_large)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004923 next_buffer = (char *)cifs_buf_get();
David Brazdil0f672f62019-12-10 10:32:29 +00004924 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004925 next_buffer = (char *)cifs_small_buf_get();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004926 memcpy(next_buffer,
David Brazdil0f672f62019-12-10 10:32:29 +00004927 buf + le32_to_cpu(shdr->NextCommand),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004928 pdu_length - le32_to_cpu(shdr->NextCommand));
4929 }
4930
4931 mid_entry = smb2_find_mid(server, buf);
4932 if (mid_entry == NULL)
4933 cifs_dbg(FYI, "mid not found\n");
4934 else {
4935 cifs_dbg(FYI, "mid found\n");
4936 mid_entry->decrypted = true;
4937 mid_entry->resp_buf_size = server->pdu_size;
4938 }
4939
4940 if (*num_mids >= MAX_COMPOUND) {
David Brazdil0f672f62019-12-10 10:32:29 +00004941 cifs_server_dbg(VFS, "too many PDUs in compound\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004942 return -1;
4943 }
4944 bufs[*num_mids] = buf;
4945 mids[(*num_mids)++] = mid_entry;
4946
4947 if (mid_entry && mid_entry->handle)
4948 ret = mid_entry->handle(server, mid_entry);
4949 else
4950 ret = cifs_handle_standard(server, mid_entry);
4951
4952 if (ret == 0 && shdr->NextCommand) {
4953 pdu_length -= le32_to_cpu(shdr->NextCommand);
4954 server->large_buf = next_is_large;
4955 if (next_is_large)
David Brazdil0f672f62019-12-10 10:32:29 +00004956 server->bigbuf = buf = next_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004957 else
David Brazdil0f672f62019-12-10 10:32:29 +00004958 server->smallbuf = buf = next_buffer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004959 goto one_more;
David Brazdil0f672f62019-12-10 10:32:29 +00004960 } else if (ret != 0) {
4961 /*
4962 * ret != 0 here means that we didn't get to handle_mid() thus
4963 * server->smallbuf and server->bigbuf are still valid. We need
4964 * to free next_buffer because it is not going to be used
4965 * anywhere.
4966 */
4967 if (next_is_large)
4968 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4969 else
4970 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004971 }
4972
4973 return ret;
4974}
4975
4976static int
4977smb3_receive_transform(struct TCP_Server_Info *server,
4978 struct mid_q_entry **mids, char **bufs, int *num_mids)
4979{
4980 char *buf = server->smallbuf;
4981 unsigned int pdu_length = server->pdu_size;
4982 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4983 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4984
4985 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4986 sizeof(struct smb2_sync_hdr)) {
David Brazdil0f672f62019-12-10 10:32:29 +00004987 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004988 pdu_length);
4989 cifs_reconnect(server);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004990 return -ECONNABORTED;
4991 }
4992
4993 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
David Brazdil0f672f62019-12-10 10:32:29 +00004994 cifs_server_dbg(VFS, "Transform message is broken\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004995 cifs_reconnect(server);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004996 return -ECONNABORTED;
4997 }
4998
4999 /* TODO: add support for compounds containing READ. */
David Brazdil0f672f62019-12-10 10:32:29 +00005000 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
5001 return receive_encrypted_read(server, &mids[0], num_mids);
5002 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005003
5004 return receive_encrypted_standard(server, mids, bufs, num_mids);
5005}
5006
5007int
5008smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
5009{
5010 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
5011
5012 return handle_read_data(server, mid, buf, server->pdu_size,
Olivier Deprez0e641232021-09-23 10:07:05 +02005013 NULL, 0, 0, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005014}
5015
5016static int
5017smb2_next_header(char *buf)
5018{
5019 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
5020 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
5021
5022 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
5023 return sizeof(struct smb2_transform_hdr) +
5024 le32_to_cpu(t_hdr->OriginalMessageSize);
5025
5026 return le32_to_cpu(hdr->NextCommand);
5027}
5028
David Brazdil0f672f62019-12-10 10:32:29 +00005029static int
5030smb2_make_node(unsigned int xid, struct inode *inode,
5031 struct dentry *dentry, struct cifs_tcon *tcon,
5032 char *full_path, umode_t mode, dev_t dev)
5033{
5034 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5035 int rc = -EPERM;
David Brazdil0f672f62019-12-10 10:32:29 +00005036 FILE_ALL_INFO *buf = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02005037 struct cifs_io_parms io_parms = {0};
David Brazdil0f672f62019-12-10 10:32:29 +00005038 __u32 oplock = 0;
5039 struct cifs_fid fid;
5040 struct cifs_open_parms oparms;
5041 unsigned int bytes_written;
5042 struct win_dev *pdev;
5043 struct kvec iov[2];
5044
5045 /*
5046 * Check if mounted with mount parm 'sfu' mount parm.
5047 * SFU emulation should work with all servers, but only
5048 * supports block and char device (no socket & fifo),
5049 * and was used by default in earlier versions of Windows
5050 */
5051 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
5052 goto out;
5053
5054 /*
5055 * TODO: Add ability to create instead via reparse point. Windows (e.g.
5056 * their current NFS server) uses this approach to expose special files
5057 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
5058 */
5059
5060 if (!S_ISCHR(mode) && !S_ISBLK(mode))
5061 goto out;
5062
5063 cifs_dbg(FYI, "sfu compat create special file\n");
5064
5065 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
5066 if (buf == NULL) {
5067 rc = -ENOMEM;
5068 goto out;
5069 }
5070
David Brazdil0f672f62019-12-10 10:32:29 +00005071 oparms.tcon = tcon;
5072 oparms.cifs_sb = cifs_sb;
5073 oparms.desired_access = GENERIC_WRITE;
Olivier Deprez157378f2022-04-04 15:47:50 +02005074 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
5075 CREATE_OPTION_SPECIAL);
David Brazdil0f672f62019-12-10 10:32:29 +00005076 oparms.disposition = FILE_CREATE;
5077 oparms.path = full_path;
5078 oparms.fid = &fid;
5079 oparms.reconnect = false;
5080
5081 if (tcon->ses->server->oplocks)
5082 oplock = REQ_OPLOCK;
5083 else
5084 oplock = 0;
5085 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
5086 if (rc)
5087 goto out;
5088
5089 /*
5090 * BB Do not bother to decode buf since no local inode yet to put
5091 * timestamps in, but we can reuse it safely.
5092 */
5093
5094 pdev = (struct win_dev *)buf;
5095 io_parms.pid = current->tgid;
5096 io_parms.tcon = tcon;
5097 io_parms.offset = 0;
5098 io_parms.length = sizeof(struct win_dev);
5099 iov[1].iov_base = buf;
5100 iov[1].iov_len = sizeof(struct win_dev);
5101 if (S_ISCHR(mode)) {
5102 memcpy(pdev->type, "IntxCHR", 8);
5103 pdev->major = cpu_to_le64(MAJOR(dev));
5104 pdev->minor = cpu_to_le64(MINOR(dev));
5105 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5106 &bytes_written, iov, 1);
5107 } else if (S_ISBLK(mode)) {
5108 memcpy(pdev->type, "IntxBLK", 8);
5109 pdev->major = cpu_to_le64(MAJOR(dev));
5110 pdev->minor = cpu_to_le64(MINOR(dev));
5111 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5112 &bytes_written, iov, 1);
5113 }
5114 tcon->ses->server->ops->close(xid, tcon, &fid);
5115 d_drop(dentry);
5116
5117 /* FIXME: add code here to set EAs */
5118out:
5119 kfree(buf);
5120 return rc;
5121}
5122
Olivier Deprez92d4c212022-12-06 15:05:30 +01005123#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005124struct smb_version_operations smb20_operations = {
5125 .compare_fids = smb2_compare_fids,
5126 .setup_request = smb2_setup_request,
5127 .setup_async_request = smb2_setup_async_request,
5128 .check_receive = smb2_check_receive,
5129 .add_credits = smb2_add_credits,
5130 .set_credits = smb2_set_credits,
5131 .get_credits_field = smb2_get_credits_field,
5132 .get_credits = smb2_get_credits,
5133 .wait_mtu_credits = cifs_wait_mtu_credits,
5134 .get_next_mid = smb2_get_next_mid,
David Brazdil0f672f62019-12-10 10:32:29 +00005135 .revert_current_mid = smb2_revert_current_mid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005136 .read_data_offset = smb2_read_data_offset,
5137 .read_data_length = smb2_read_data_length,
5138 .map_error = map_smb2_to_linux_error,
5139 .find_mid = smb2_find_mid,
5140 .check_message = smb2_check_message,
5141 .dump_detail = smb2_dump_detail,
5142 .clear_stats = smb2_clear_stats,
5143 .print_stats = smb2_print_stats,
5144 .is_oplock_break = smb2_is_valid_oplock_break,
5145 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5146 .downgrade_oplock = smb2_downgrade_oplock,
5147 .need_neg = smb2_need_neg,
5148 .negotiate = smb2_negotiate,
5149 .negotiate_wsize = smb2_negotiate_wsize,
5150 .negotiate_rsize = smb2_negotiate_rsize,
5151 .sess_setup = SMB2_sess_setup,
5152 .logoff = SMB2_logoff,
5153 .tree_connect = SMB2_tcon,
5154 .tree_disconnect = SMB2_tdis,
5155 .qfs_tcon = smb2_qfs_tcon,
5156 .is_path_accessible = smb2_is_path_accessible,
5157 .can_echo = smb2_can_echo,
5158 .echo = SMB2_echo,
5159 .query_path_info = smb2_query_path_info,
5160 .get_srv_inum = smb2_get_srv_inum,
5161 .query_file_info = smb2_query_file_info,
5162 .set_path_size = smb2_set_path_size,
5163 .set_file_size = smb2_set_file_size,
5164 .set_file_info = smb2_set_file_info,
5165 .set_compression = smb2_set_compression,
5166 .mkdir = smb2_mkdir,
5167 .mkdir_setinfo = smb2_mkdir_setinfo,
5168 .rmdir = smb2_rmdir,
5169 .unlink = smb2_unlink,
5170 .rename = smb2_rename_path,
5171 .create_hardlink = smb2_create_hardlink,
5172 .query_symlink = smb2_query_symlink,
5173 .query_mf_symlink = smb3_query_mf_symlink,
5174 .create_mf_symlink = smb3_create_mf_symlink,
5175 .open = smb2_open_file,
5176 .set_fid = smb2_set_fid,
5177 .close = smb2_close_file,
5178 .flush = smb2_flush_file,
5179 .async_readv = smb2_async_readv,
5180 .async_writev = smb2_async_writev,
5181 .sync_read = smb2_sync_read,
5182 .sync_write = smb2_sync_write,
5183 .query_dir_first = smb2_query_dir_first,
5184 .query_dir_next = smb2_query_dir_next,
5185 .close_dir = smb2_close_dir,
5186 .calc_smb_size = smb2_calc_size,
5187 .is_status_pending = smb2_is_status_pending,
5188 .is_session_expired = smb2_is_session_expired,
5189 .oplock_response = smb2_oplock_response,
5190 .queryfs = smb2_queryfs,
5191 .mand_lock = smb2_mand_lock,
5192 .mand_unlock_range = smb2_unlock_range,
5193 .push_mand_locks = smb2_push_mandatory_locks,
5194 .get_lease_key = smb2_get_lease_key,
5195 .set_lease_key = smb2_set_lease_key,
5196 .new_lease_key = smb2_new_lease_key,
5197 .calc_signature = smb2_calc_signature,
5198 .is_read_op = smb2_is_read_op,
5199 .set_oplock_level = smb2_set_oplock_level,
5200 .create_lease_buf = smb2_create_lease_buf,
5201 .parse_lease_buf = smb2_parse_lease_buf,
5202 .copychunk_range = smb2_copychunk_range,
5203 .wp_retry_size = smb2_wp_retry_size,
5204 .dir_needs_close = smb2_dir_needs_close,
5205 .get_dfs_refer = smb2_get_dfs_refer,
5206 .select_sectype = smb2_select_sectype,
5207#ifdef CONFIG_CIFS_XATTR
5208 .query_all_EAs = smb2_query_eas,
5209 .set_EA = smb2_set_ea,
5210#endif /* CIFS_XATTR */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005211 .get_acl = get_smb2_acl,
5212 .get_acl_by_fid = get_smb2_acl_by_fid,
5213 .set_acl = set_smb2_acl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005214 .next_header = smb2_next_header,
David Brazdil0f672f62019-12-10 10:32:29 +00005215 .ioctl_query_info = smb2_ioctl_query_info,
5216 .make_node = smb2_make_node,
5217 .fiemap = smb3_fiemap,
5218 .llseek = smb3_llseek,
Olivier Deprez157378f2022-04-04 15:47:50 +02005219 .is_status_io_timeout = smb2_is_status_io_timeout,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005220};
Olivier Deprez92d4c212022-12-06 15:05:30 +01005221#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005222
5223struct smb_version_operations smb21_operations = {
5224 .compare_fids = smb2_compare_fids,
5225 .setup_request = smb2_setup_request,
5226 .setup_async_request = smb2_setup_async_request,
5227 .check_receive = smb2_check_receive,
5228 .add_credits = smb2_add_credits,
5229 .set_credits = smb2_set_credits,
5230 .get_credits_field = smb2_get_credits_field,
5231 .get_credits = smb2_get_credits,
5232 .wait_mtu_credits = smb2_wait_mtu_credits,
David Brazdil0f672f62019-12-10 10:32:29 +00005233 .adjust_credits = smb2_adjust_credits,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005234 .get_next_mid = smb2_get_next_mid,
David Brazdil0f672f62019-12-10 10:32:29 +00005235 .revert_current_mid = smb2_revert_current_mid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005236 .read_data_offset = smb2_read_data_offset,
5237 .read_data_length = smb2_read_data_length,
5238 .map_error = map_smb2_to_linux_error,
5239 .find_mid = smb2_find_mid,
5240 .check_message = smb2_check_message,
5241 .dump_detail = smb2_dump_detail,
5242 .clear_stats = smb2_clear_stats,
5243 .print_stats = smb2_print_stats,
5244 .is_oplock_break = smb2_is_valid_oplock_break,
5245 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Olivier Deprez0e641232021-09-23 10:07:05 +02005246 .downgrade_oplock = smb2_downgrade_oplock,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005247 .need_neg = smb2_need_neg,
5248 .negotiate = smb2_negotiate,
5249 .negotiate_wsize = smb2_negotiate_wsize,
5250 .negotiate_rsize = smb2_negotiate_rsize,
5251 .sess_setup = SMB2_sess_setup,
5252 .logoff = SMB2_logoff,
5253 .tree_connect = SMB2_tcon,
5254 .tree_disconnect = SMB2_tdis,
5255 .qfs_tcon = smb2_qfs_tcon,
5256 .is_path_accessible = smb2_is_path_accessible,
5257 .can_echo = smb2_can_echo,
5258 .echo = SMB2_echo,
5259 .query_path_info = smb2_query_path_info,
5260 .get_srv_inum = smb2_get_srv_inum,
5261 .query_file_info = smb2_query_file_info,
5262 .set_path_size = smb2_set_path_size,
5263 .set_file_size = smb2_set_file_size,
5264 .set_file_info = smb2_set_file_info,
5265 .set_compression = smb2_set_compression,
5266 .mkdir = smb2_mkdir,
5267 .mkdir_setinfo = smb2_mkdir_setinfo,
5268 .rmdir = smb2_rmdir,
5269 .unlink = smb2_unlink,
5270 .rename = smb2_rename_path,
5271 .create_hardlink = smb2_create_hardlink,
5272 .query_symlink = smb2_query_symlink,
5273 .query_mf_symlink = smb3_query_mf_symlink,
5274 .create_mf_symlink = smb3_create_mf_symlink,
5275 .open = smb2_open_file,
5276 .set_fid = smb2_set_fid,
5277 .close = smb2_close_file,
5278 .flush = smb2_flush_file,
5279 .async_readv = smb2_async_readv,
5280 .async_writev = smb2_async_writev,
5281 .sync_read = smb2_sync_read,
5282 .sync_write = smb2_sync_write,
5283 .query_dir_first = smb2_query_dir_first,
5284 .query_dir_next = smb2_query_dir_next,
5285 .close_dir = smb2_close_dir,
5286 .calc_smb_size = smb2_calc_size,
5287 .is_status_pending = smb2_is_status_pending,
5288 .is_session_expired = smb2_is_session_expired,
5289 .oplock_response = smb2_oplock_response,
5290 .queryfs = smb2_queryfs,
5291 .mand_lock = smb2_mand_lock,
5292 .mand_unlock_range = smb2_unlock_range,
5293 .push_mand_locks = smb2_push_mandatory_locks,
5294 .get_lease_key = smb2_get_lease_key,
5295 .set_lease_key = smb2_set_lease_key,
5296 .new_lease_key = smb2_new_lease_key,
5297 .calc_signature = smb2_calc_signature,
5298 .is_read_op = smb21_is_read_op,
5299 .set_oplock_level = smb21_set_oplock_level,
5300 .create_lease_buf = smb2_create_lease_buf,
5301 .parse_lease_buf = smb2_parse_lease_buf,
5302 .copychunk_range = smb2_copychunk_range,
5303 .wp_retry_size = smb2_wp_retry_size,
5304 .dir_needs_close = smb2_dir_needs_close,
5305 .enum_snapshots = smb3_enum_snapshots,
Olivier Deprez157378f2022-04-04 15:47:50 +02005306 .notify = smb3_notify,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005307 .get_dfs_refer = smb2_get_dfs_refer,
5308 .select_sectype = smb2_select_sectype,
5309#ifdef CONFIG_CIFS_XATTR
5310 .query_all_EAs = smb2_query_eas,
5311 .set_EA = smb2_set_ea,
5312#endif /* CIFS_XATTR */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005313 .get_acl = get_smb2_acl,
5314 .get_acl_by_fid = get_smb2_acl_by_fid,
5315 .set_acl = set_smb2_acl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005316 .next_header = smb2_next_header,
David Brazdil0f672f62019-12-10 10:32:29 +00005317 .ioctl_query_info = smb2_ioctl_query_info,
5318 .make_node = smb2_make_node,
5319 .fiemap = smb3_fiemap,
5320 .llseek = smb3_llseek,
Olivier Deprez157378f2022-04-04 15:47:50 +02005321 .is_status_io_timeout = smb2_is_status_io_timeout,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005322};
5323
5324struct smb_version_operations smb30_operations = {
5325 .compare_fids = smb2_compare_fids,
5326 .setup_request = smb2_setup_request,
5327 .setup_async_request = smb2_setup_async_request,
5328 .check_receive = smb2_check_receive,
5329 .add_credits = smb2_add_credits,
5330 .set_credits = smb2_set_credits,
5331 .get_credits_field = smb2_get_credits_field,
5332 .get_credits = smb2_get_credits,
5333 .wait_mtu_credits = smb2_wait_mtu_credits,
David Brazdil0f672f62019-12-10 10:32:29 +00005334 .adjust_credits = smb2_adjust_credits,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005335 .get_next_mid = smb2_get_next_mid,
David Brazdil0f672f62019-12-10 10:32:29 +00005336 .revert_current_mid = smb2_revert_current_mid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005337 .read_data_offset = smb2_read_data_offset,
5338 .read_data_length = smb2_read_data_length,
5339 .map_error = map_smb2_to_linux_error,
5340 .find_mid = smb2_find_mid,
5341 .check_message = smb2_check_message,
5342 .dump_detail = smb2_dump_detail,
5343 .clear_stats = smb2_clear_stats,
5344 .print_stats = smb2_print_stats,
5345 .dump_share_caps = smb2_dump_share_caps,
5346 .is_oplock_break = smb2_is_valid_oplock_break,
5347 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Olivier Deprez0e641232021-09-23 10:07:05 +02005348 .downgrade_oplock = smb3_downgrade_oplock,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005349 .need_neg = smb2_need_neg,
5350 .negotiate = smb2_negotiate,
David Brazdil0f672f62019-12-10 10:32:29 +00005351 .negotiate_wsize = smb3_negotiate_wsize,
5352 .negotiate_rsize = smb3_negotiate_rsize,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005353 .sess_setup = SMB2_sess_setup,
5354 .logoff = SMB2_logoff,
5355 .tree_connect = SMB2_tcon,
5356 .tree_disconnect = SMB2_tdis,
5357 .qfs_tcon = smb3_qfs_tcon,
5358 .is_path_accessible = smb2_is_path_accessible,
5359 .can_echo = smb2_can_echo,
5360 .echo = SMB2_echo,
5361 .query_path_info = smb2_query_path_info,
Olivier Deprez157378f2022-04-04 15:47:50 +02005362 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5363 .query_reparse_tag = smb2_query_reparse_tag,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005364 .get_srv_inum = smb2_get_srv_inum,
5365 .query_file_info = smb2_query_file_info,
5366 .set_path_size = smb2_set_path_size,
5367 .set_file_size = smb2_set_file_size,
5368 .set_file_info = smb2_set_file_info,
5369 .set_compression = smb2_set_compression,
5370 .mkdir = smb2_mkdir,
5371 .mkdir_setinfo = smb2_mkdir_setinfo,
5372 .rmdir = smb2_rmdir,
5373 .unlink = smb2_unlink,
5374 .rename = smb2_rename_path,
5375 .create_hardlink = smb2_create_hardlink,
5376 .query_symlink = smb2_query_symlink,
5377 .query_mf_symlink = smb3_query_mf_symlink,
5378 .create_mf_symlink = smb3_create_mf_symlink,
5379 .open = smb2_open_file,
5380 .set_fid = smb2_set_fid,
5381 .close = smb2_close_file,
Olivier Deprez157378f2022-04-04 15:47:50 +02005382 .close_getattr = smb2_close_getattr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005383 .flush = smb2_flush_file,
5384 .async_readv = smb2_async_readv,
5385 .async_writev = smb2_async_writev,
5386 .sync_read = smb2_sync_read,
5387 .sync_write = smb2_sync_write,
5388 .query_dir_first = smb2_query_dir_first,
5389 .query_dir_next = smb2_query_dir_next,
5390 .close_dir = smb2_close_dir,
5391 .calc_smb_size = smb2_calc_size,
5392 .is_status_pending = smb2_is_status_pending,
5393 .is_session_expired = smb2_is_session_expired,
5394 .oplock_response = smb2_oplock_response,
5395 .queryfs = smb2_queryfs,
5396 .mand_lock = smb2_mand_lock,
5397 .mand_unlock_range = smb2_unlock_range,
5398 .push_mand_locks = smb2_push_mandatory_locks,
5399 .get_lease_key = smb2_get_lease_key,
5400 .set_lease_key = smb2_set_lease_key,
5401 .new_lease_key = smb2_new_lease_key,
5402 .generate_signingkey = generate_smb30signingkey,
5403 .calc_signature = smb3_calc_signature,
5404 .set_integrity = smb3_set_integrity,
5405 .is_read_op = smb21_is_read_op,
5406 .set_oplock_level = smb3_set_oplock_level,
5407 .create_lease_buf = smb3_create_lease_buf,
5408 .parse_lease_buf = smb3_parse_lease_buf,
5409 .copychunk_range = smb2_copychunk_range,
5410 .duplicate_extents = smb2_duplicate_extents,
5411 .validate_negotiate = smb3_validate_negotiate,
5412 .wp_retry_size = smb2_wp_retry_size,
5413 .dir_needs_close = smb2_dir_needs_close,
5414 .fallocate = smb3_fallocate,
5415 .enum_snapshots = smb3_enum_snapshots,
Olivier Deprez157378f2022-04-04 15:47:50 +02005416 .notify = smb3_notify,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005417 .init_transform_rq = smb3_init_transform_rq,
5418 .is_transform_hdr = smb3_is_transform_hdr,
5419 .receive_transform = smb3_receive_transform,
5420 .get_dfs_refer = smb2_get_dfs_refer,
5421 .select_sectype = smb2_select_sectype,
5422#ifdef CONFIG_CIFS_XATTR
5423 .query_all_EAs = smb2_query_eas,
5424 .set_EA = smb2_set_ea,
5425#endif /* CIFS_XATTR */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005426 .get_acl = get_smb2_acl,
5427 .get_acl_by_fid = get_smb2_acl_by_fid,
5428 .set_acl = set_smb2_acl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005429 .next_header = smb2_next_header,
David Brazdil0f672f62019-12-10 10:32:29 +00005430 .ioctl_query_info = smb2_ioctl_query_info,
5431 .make_node = smb2_make_node,
5432 .fiemap = smb3_fiemap,
5433 .llseek = smb3_llseek,
Olivier Deprez157378f2022-04-04 15:47:50 +02005434 .is_status_io_timeout = smb2_is_status_io_timeout,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005435};
5436
5437struct smb_version_operations smb311_operations = {
5438 .compare_fids = smb2_compare_fids,
5439 .setup_request = smb2_setup_request,
5440 .setup_async_request = smb2_setup_async_request,
5441 .check_receive = smb2_check_receive,
5442 .add_credits = smb2_add_credits,
5443 .set_credits = smb2_set_credits,
5444 .get_credits_field = smb2_get_credits_field,
5445 .get_credits = smb2_get_credits,
5446 .wait_mtu_credits = smb2_wait_mtu_credits,
David Brazdil0f672f62019-12-10 10:32:29 +00005447 .adjust_credits = smb2_adjust_credits,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005448 .get_next_mid = smb2_get_next_mid,
David Brazdil0f672f62019-12-10 10:32:29 +00005449 .revert_current_mid = smb2_revert_current_mid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005450 .read_data_offset = smb2_read_data_offset,
5451 .read_data_length = smb2_read_data_length,
5452 .map_error = map_smb2_to_linux_error,
5453 .find_mid = smb2_find_mid,
5454 .check_message = smb2_check_message,
5455 .dump_detail = smb2_dump_detail,
5456 .clear_stats = smb2_clear_stats,
5457 .print_stats = smb2_print_stats,
5458 .dump_share_caps = smb2_dump_share_caps,
5459 .is_oplock_break = smb2_is_valid_oplock_break,
5460 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Olivier Deprez0e641232021-09-23 10:07:05 +02005461 .downgrade_oplock = smb3_downgrade_oplock,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005462 .need_neg = smb2_need_neg,
5463 .negotiate = smb2_negotiate,
David Brazdil0f672f62019-12-10 10:32:29 +00005464 .negotiate_wsize = smb3_negotiate_wsize,
5465 .negotiate_rsize = smb3_negotiate_rsize,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005466 .sess_setup = SMB2_sess_setup,
5467 .logoff = SMB2_logoff,
5468 .tree_connect = SMB2_tcon,
5469 .tree_disconnect = SMB2_tdis,
5470 .qfs_tcon = smb3_qfs_tcon,
5471 .is_path_accessible = smb2_is_path_accessible,
5472 .can_echo = smb2_can_echo,
5473 .echo = SMB2_echo,
5474 .query_path_info = smb2_query_path_info,
Olivier Deprez157378f2022-04-04 15:47:50 +02005475 .query_reparse_tag = smb2_query_reparse_tag,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005476 .get_srv_inum = smb2_get_srv_inum,
5477 .query_file_info = smb2_query_file_info,
5478 .set_path_size = smb2_set_path_size,
5479 .set_file_size = smb2_set_file_size,
5480 .set_file_info = smb2_set_file_info,
5481 .set_compression = smb2_set_compression,
5482 .mkdir = smb2_mkdir,
5483 .mkdir_setinfo = smb2_mkdir_setinfo,
5484 .posix_mkdir = smb311_posix_mkdir,
5485 .rmdir = smb2_rmdir,
5486 .unlink = smb2_unlink,
5487 .rename = smb2_rename_path,
5488 .create_hardlink = smb2_create_hardlink,
5489 .query_symlink = smb2_query_symlink,
5490 .query_mf_symlink = smb3_query_mf_symlink,
5491 .create_mf_symlink = smb3_create_mf_symlink,
5492 .open = smb2_open_file,
5493 .set_fid = smb2_set_fid,
5494 .close = smb2_close_file,
Olivier Deprez157378f2022-04-04 15:47:50 +02005495 .close_getattr = smb2_close_getattr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005496 .flush = smb2_flush_file,
5497 .async_readv = smb2_async_readv,
5498 .async_writev = smb2_async_writev,
5499 .sync_read = smb2_sync_read,
5500 .sync_write = smb2_sync_write,
5501 .query_dir_first = smb2_query_dir_first,
5502 .query_dir_next = smb2_query_dir_next,
5503 .close_dir = smb2_close_dir,
5504 .calc_smb_size = smb2_calc_size,
5505 .is_status_pending = smb2_is_status_pending,
5506 .is_session_expired = smb2_is_session_expired,
5507 .oplock_response = smb2_oplock_response,
5508 .queryfs = smb311_queryfs,
5509 .mand_lock = smb2_mand_lock,
5510 .mand_unlock_range = smb2_unlock_range,
5511 .push_mand_locks = smb2_push_mandatory_locks,
5512 .get_lease_key = smb2_get_lease_key,
5513 .set_lease_key = smb2_set_lease_key,
5514 .new_lease_key = smb2_new_lease_key,
5515 .generate_signingkey = generate_smb311signingkey,
5516 .calc_signature = smb3_calc_signature,
5517 .set_integrity = smb3_set_integrity,
5518 .is_read_op = smb21_is_read_op,
5519 .set_oplock_level = smb3_set_oplock_level,
5520 .create_lease_buf = smb3_create_lease_buf,
5521 .parse_lease_buf = smb3_parse_lease_buf,
5522 .copychunk_range = smb2_copychunk_range,
5523 .duplicate_extents = smb2_duplicate_extents,
5524/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5525 .wp_retry_size = smb2_wp_retry_size,
5526 .dir_needs_close = smb2_dir_needs_close,
5527 .fallocate = smb3_fallocate,
5528 .enum_snapshots = smb3_enum_snapshots,
Olivier Deprez157378f2022-04-04 15:47:50 +02005529 .notify = smb3_notify,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005530 .init_transform_rq = smb3_init_transform_rq,
5531 .is_transform_hdr = smb3_is_transform_hdr,
5532 .receive_transform = smb3_receive_transform,
5533 .get_dfs_refer = smb2_get_dfs_refer,
5534 .select_sectype = smb2_select_sectype,
5535#ifdef CONFIG_CIFS_XATTR
5536 .query_all_EAs = smb2_query_eas,
5537 .set_EA = smb2_set_ea,
5538#endif /* CIFS_XATTR */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005539 .get_acl = get_smb2_acl,
5540 .get_acl_by_fid = get_smb2_acl_by_fid,
5541 .set_acl = set_smb2_acl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005542 .next_header = smb2_next_header,
David Brazdil0f672f62019-12-10 10:32:29 +00005543 .ioctl_query_info = smb2_ioctl_query_info,
5544 .make_node = smb2_make_node,
5545 .fiemap = smb3_fiemap,
5546 .llseek = smb3_llseek,
Olivier Deprez157378f2022-04-04 15:47:50 +02005547 .is_status_io_timeout = smb2_is_status_io_timeout,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005548};
5549
Olivier Deprez92d4c212022-12-06 15:05:30 +01005550#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005551struct smb_version_values smb20_values = {
5552 .version_string = SMB20_VERSION_STRING,
5553 .protocol_id = SMB20_PROT_ID,
5554 .req_capabilities = 0, /* MBZ */
5555 .large_lock_type = 0,
5556 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5557 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5558 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5559 .header_size = sizeof(struct smb2_sync_hdr),
5560 .header_preamble_size = 0,
5561 .max_header_size = MAX_SMB2_HDR_SIZE,
5562 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5563 .lock_cmd = SMB2_LOCK,
5564 .cap_unix = 0,
5565 .cap_nt_find = SMB2_NT_FIND,
5566 .cap_large_files = SMB2_LARGE_FILES,
5567 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5568 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5569 .create_lease_size = sizeof(struct create_lease),
5570};
Olivier Deprez92d4c212022-12-06 15:05:30 +01005571#endif /* ALLOW_INSECURE_LEGACY */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005572
5573struct smb_version_values smb21_values = {
5574 .version_string = SMB21_VERSION_STRING,
5575 .protocol_id = SMB21_PROT_ID,
5576 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5577 .large_lock_type = 0,
5578 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5579 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5580 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5581 .header_size = sizeof(struct smb2_sync_hdr),
5582 .header_preamble_size = 0,
5583 .max_header_size = MAX_SMB2_HDR_SIZE,
5584 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5585 .lock_cmd = SMB2_LOCK,
5586 .cap_unix = 0,
5587 .cap_nt_find = SMB2_NT_FIND,
5588 .cap_large_files = SMB2_LARGE_FILES,
5589 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5590 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5591 .create_lease_size = sizeof(struct create_lease),
5592};
5593
5594struct smb_version_values smb3any_values = {
5595 .version_string = SMB3ANY_VERSION_STRING,
5596 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5597 .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,
5598 .large_lock_type = 0,
5599 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5600 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5601 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5602 .header_size = sizeof(struct smb2_sync_hdr),
5603 .header_preamble_size = 0,
5604 .max_header_size = MAX_SMB2_HDR_SIZE,
5605 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5606 .lock_cmd = SMB2_LOCK,
5607 .cap_unix = 0,
5608 .cap_nt_find = SMB2_NT_FIND,
5609 .cap_large_files = SMB2_LARGE_FILES,
5610 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5611 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5612 .create_lease_size = sizeof(struct create_lease_v2),
5613};
5614
5615struct smb_version_values smbdefault_values = {
5616 .version_string = SMBDEFAULT_VERSION_STRING,
5617 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5618 .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,
5619 .large_lock_type = 0,
5620 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5621 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5622 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5623 .header_size = sizeof(struct smb2_sync_hdr),
5624 .header_preamble_size = 0,
5625 .max_header_size = MAX_SMB2_HDR_SIZE,
5626 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5627 .lock_cmd = SMB2_LOCK,
5628 .cap_unix = 0,
5629 .cap_nt_find = SMB2_NT_FIND,
5630 .cap_large_files = SMB2_LARGE_FILES,
5631 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5632 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5633 .create_lease_size = sizeof(struct create_lease_v2),
5634};
5635
5636struct smb_version_values smb30_values = {
5637 .version_string = SMB30_VERSION_STRING,
5638 .protocol_id = SMB30_PROT_ID,
5639 .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,
5640 .large_lock_type = 0,
5641 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5642 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5643 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5644 .header_size = sizeof(struct smb2_sync_hdr),
5645 .header_preamble_size = 0,
5646 .max_header_size = MAX_SMB2_HDR_SIZE,
5647 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5648 .lock_cmd = SMB2_LOCK,
5649 .cap_unix = 0,
5650 .cap_nt_find = SMB2_NT_FIND,
5651 .cap_large_files = SMB2_LARGE_FILES,
5652 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5653 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5654 .create_lease_size = sizeof(struct create_lease_v2),
5655};
5656
5657struct smb_version_values smb302_values = {
5658 .version_string = SMB302_VERSION_STRING,
5659 .protocol_id = SMB302_PROT_ID,
5660 .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,
5661 .large_lock_type = 0,
5662 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5663 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5664 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5665 .header_size = sizeof(struct smb2_sync_hdr),
5666 .header_preamble_size = 0,
5667 .max_header_size = MAX_SMB2_HDR_SIZE,
5668 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5669 .lock_cmd = SMB2_LOCK,
5670 .cap_unix = 0,
5671 .cap_nt_find = SMB2_NT_FIND,
5672 .cap_large_files = SMB2_LARGE_FILES,
5673 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5674 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5675 .create_lease_size = sizeof(struct create_lease_v2),
5676};
5677
5678struct smb_version_values smb311_values = {
5679 .version_string = SMB311_VERSION_STRING,
5680 .protocol_id = SMB311_PROT_ID,
5681 .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,
5682 .large_lock_type = 0,
5683 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5684 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5685 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5686 .header_size = sizeof(struct smb2_sync_hdr),
5687 .header_preamble_size = 0,
5688 .max_header_size = MAX_SMB2_HDR_SIZE,
5689 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5690 .lock_cmd = SMB2_LOCK,
5691 .cap_unix = 0,
5692 .cap_nt_find = SMB2_NT_FIND,
5693 .cap_large_files = SMB2_LARGE_FILES,
5694 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5695 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5696 .create_lease_size = sizeof(struct create_lease_v2),
5697};